[email protected] | 398ad13 | 2013-04-02 15:11:01 | [diff] [blame] | 1 | // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
| 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
| 5 | #include "net/disk_cache/cache_util.h" |
| 6 | |
avi | d0181f3 | 2015-12-10 19:41:47 | [diff] [blame] | 7 | #include <limits> |
| 8 | |
[email protected] | 76ed979ee | 2013-07-17 15:50:45 | [diff] [blame] | 9 | #include "base/files/file_enumerator.h" |
thestig | d8df033 | 2014-09-04 06:33:29 | [diff] [blame] | 10 | #include "base/files/file_util.h" |
[email protected] | 398ad13 | 2013-04-02 15:11:01 | [diff] [blame] | 11 | #include "base/location.h" |
[email protected] | be528af | 2013-06-11 07:39:48 | [diff] [blame] | 12 | #include "base/strings/string_util.h" |
| 13 | #include "base/strings/stringprintf.h" |
[email protected] | 74f778e | 2014-03-14 21:11:46 | [diff] [blame] | 14 | #include "base/strings/utf_string_conversions.h" |
fdoray | 974b027 | 2017-02-28 03:00:41 | [diff] [blame] | 15 | #include "base/task_scheduler/post_task.h" |
[email protected] | 398ad13 | 2013-04-02 15:11:01 | [diff] [blame] | 16 | #include "base/threading/thread_restrictions.h" |
[email protected] | 398ad13 | 2013-04-02 15:11:01 | [diff] [blame] | 17 | |
| 18 | namespace { |
| 19 | |
| 20 | const int kMaxOldFolders = 100; |
| 21 | |
| 22 | // Returns a fully qualified name from path and name, using a given name prefix |
| 23 | // and index number. For instance, if the arguments are "/foo", "bar" and 5, it |
| 24 | // will return "/foo/old_bar_005". |
| 25 | base::FilePath GetPrefixedName(const base::FilePath& path, |
| 26 | const std::string& name, |
| 27 | int index) { |
| 28 | std::string tmp = base::StringPrintf("%s%s_%03d", "old_", |
| 29 | name.c_str(), index); |
| 30 | return path.AppendASCII(tmp); |
| 31 | } |
| 32 | |
| 33 | // This is a simple callback to cleanup old caches. |
| 34 | void CleanupCallback(const base::FilePath& path, const std::string& name) { |
| 35 | for (int i = 0; i < kMaxOldFolders; i++) { |
| 36 | base::FilePath to_delete = GetPrefixedName(path, name, i); |
| 37 | disk_cache::DeleteCache(to_delete, true); |
| 38 | } |
| 39 | } |
| 40 | |
| 41 | // Returns a full path to rename the current cache, in order to delete it. path |
| 42 | // is the current folder location, and name is the current folder name. |
| 43 | base::FilePath GetTempCacheName(const base::FilePath& path, |
| 44 | const std::string& name) { |
| 45 | // We'll attempt to have up to kMaxOldFolders folders for deletion. |
| 46 | for (int i = 0; i < kMaxOldFolders; i++) { |
| 47 | base::FilePath to_delete = GetPrefixedName(path, name, i); |
[email protected] | 756748414 | 2013-07-11 17:36:07 | [diff] [blame] | 48 | if (!base::PathExists(to_delete)) |
[email protected] | 398ad13 | 2013-04-02 15:11:01 | [diff] [blame] | 49 | return to_delete; |
| 50 | } |
| 51 | return base::FilePath(); |
| 52 | } |
| 53 | |
avi | d0181f3 | 2015-12-10 19:41:47 | [diff] [blame] | 54 | int64_t PreferredCacheSizeInternal(int64_t available) { |
[email protected] | 18b5b26 | 2013-11-19 18:41:37 | [diff] [blame] | 55 | using disk_cache::kDefaultCacheSize; |
| 56 | // Return 80% of the available space if there is not enough space to use |
| 57 | // kDefaultCacheSize. |
| 58 | if (available < kDefaultCacheSize * 10 / 8) |
| 59 | return available * 8 / 10; |
| 60 | |
| 61 | // Return kDefaultCacheSize if it uses 10% to 80% of the available space. |
| 62 | if (available < kDefaultCacheSize * 10) |
| 63 | return kDefaultCacheSize; |
| 64 | |
| 65 | // Return 10% of the available space if the target size |
| 66 | // (2.5 * kDefaultCacheSize) is more than 10%. |
avi | d0181f3 | 2015-12-10 19:41:47 | [diff] [blame] | 67 | if (available < static_cast<int64_t>(kDefaultCacheSize) * 25) |
[email protected] | 18b5b26 | 2013-11-19 18:41:37 | [diff] [blame] | 68 | return available / 10; |
| 69 | |
| 70 | // Return the target size (2.5 * kDefaultCacheSize) if it uses 10% to 1% |
| 71 | // of the available space. |
avi | d0181f3 | 2015-12-10 19:41:47 | [diff] [blame] | 72 | if (available < static_cast<int64_t>(kDefaultCacheSize) * 250) |
[email protected] | 18b5b26 | 2013-11-19 18:41:37 | [diff] [blame] | 73 | return kDefaultCacheSize * 5 / 2; |
| 74 | |
| 75 | // Return 1% of the available space. |
| 76 | return available / 100; |
| 77 | } |
| 78 | |
[email protected] | 398ad13 | 2013-04-02 15:11:01 | [diff] [blame] | 79 | } // namespace |
| 80 | |
| 81 | namespace disk_cache { |
| 82 | |
[email protected] | 18b5b26 | 2013-11-19 18:41:37 | [diff] [blame] | 83 | const int kDefaultCacheSize = 80 * 1024 * 1024; |
| 84 | |
[email protected] | 76ed979ee | 2013-07-17 15:50:45 | [diff] [blame] | 85 | void DeleteCache(const base::FilePath& path, bool remove_folder) { |
| 86 | if (remove_folder) { |
| 87 | if (!base::DeleteFile(path, /* recursive */ true)) |
| 88 | LOG(WARNING) << "Unable to delete cache folder."; |
| 89 | return; |
| 90 | } |
| 91 | |
| 92 | base::FileEnumerator iter( |
| 93 | path, |
| 94 | /* recursive */ false, |
| 95 | base::FileEnumerator::FILES | base::FileEnumerator::DIRECTORIES); |
| 96 | for (base::FilePath file = iter.Next(); !file.value().empty(); |
| 97 | file = iter.Next()) { |
| 98 | if (!base::DeleteFile(file, /* recursive */ true)) { |
| 99 | LOG(WARNING) << "Unable to delete cache."; |
| 100 | return; |
| 101 | } |
| 102 | } |
| 103 | } |
| 104 | |
[email protected] | 398ad13 | 2013-04-02 15:11:01 | [diff] [blame] | 105 | // In order to process a potentially large number of files, we'll rename the |
| 106 | // cache directory to old_ + original_name + number, (located on the same parent |
| 107 | // directory), and use a worker thread to delete all the files on all the stale |
| 108 | // cache directories. The whole process can still fail if we are not able to |
| 109 | // rename the cache directory (for instance due to a sharing violation), and in |
| 110 | // that case a cache for this profile (on the desired path) cannot be created. |
| 111 | bool DelayedCacheCleanup(const base::FilePath& full_path) { |
| 112 | // GetTempCacheName() and MoveCache() use synchronous file |
| 113 | // operations. |
| 114 | base::ThreadRestrictions::ScopedAllowIO allow_io; |
| 115 | |
| 116 | base::FilePath current_path = full_path.StripTrailingSeparators(); |
| 117 | |
| 118 | base::FilePath path = current_path.DirName(); |
| 119 | base::FilePath name = current_path.BaseName(); |
| 120 | #if defined(OS_POSIX) |
| 121 | std::string name_str = name.value(); |
| 122 | #elif defined(OS_WIN) |
| 123 | // We created this file so it should only contain ASCII. |
[email protected] | 74f778e | 2014-03-14 21:11:46 | [diff] [blame] | 124 | std::string name_str = base::UTF16ToASCII(name.value()); |
[email protected] | 398ad13 | 2013-04-02 15:11:01 | [diff] [blame] | 125 | #endif |
| 126 | |
| 127 | base::FilePath to_delete = GetTempCacheName(path, name_str); |
| 128 | if (to_delete.empty()) { |
| 129 | LOG(ERROR) << "Unable to get another cache folder"; |
| 130 | return false; |
| 131 | } |
| 132 | |
| 133 | if (!disk_cache::MoveCache(full_path, to_delete)) { |
| 134 | LOG(ERROR) << "Unable to move cache folder " << full_path.value() << " to " |
| 135 | << to_delete.value(); |
| 136 | return false; |
| 137 | } |
| 138 | |
fdoray | 2f60799d | 2017-05-03 22:54:58 | [diff] [blame^] | 139 | base::PostTaskWithTraits(FROM_HERE, |
| 140 | {base::MayBlock(), base::TaskPriority::BACKGROUND, |
| 141 | base::TaskShutdownBehavior::CONTINUE_ON_SHUTDOWN}, |
| 142 | base::Bind(&CleanupCallback, path, name_str)); |
[email protected] | 398ad13 | 2013-04-02 15:11:01 | [diff] [blame] | 143 | return true; |
| 144 | } |
| 145 | |
[email protected] | 18b5b26 | 2013-11-19 18:41:37 | [diff] [blame] | 146 | // Returns the preferred maximum number of bytes for the cache given the |
| 147 | // number of available bytes. |
avi | d0181f3 | 2015-12-10 19:41:47 | [diff] [blame] | 148 | int PreferredCacheSize(int64_t available) { |
[email protected] | 18b5b26 | 2013-11-19 18:41:37 | [diff] [blame] | 149 | if (available < 0) |
| 150 | return kDefaultCacheSize; |
| 151 | |
[email protected] | 18b5b26 | 2013-11-19 18:41:37 | [diff] [blame] | 152 | // Limit cache size to somewhat less than kint32max to avoid potential |
| 153 | // integer overflows in cache backend implementations. |
avi | d0181f3 | 2015-12-10 19:41:47 | [diff] [blame] | 154 | DCHECK_LT(kDefaultCacheSize * 4, std::numeric_limits<int32_t>::max()); |
| 155 | return static_cast<int32_t>( |
| 156 | std::min(PreferredCacheSizeInternal(available), |
| 157 | static_cast<int64_t>(kDefaultCacheSize * 4))); |
[email protected] | 18b5b26 | 2013-11-19 18:41:37 | [diff] [blame] | 158 | } |
| 159 | |
[email protected] | 398ad13 | 2013-04-02 15:11:01 | [diff] [blame] | 160 | } // namespace disk_cache |