[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" |
Matt Menke | b0f4c7c | 2018-07-06 19:25:30 | [diff] [blame] | 12 | #include "base/metrics/field_trial_params.h" |
| 13 | #include "base/strings/string_number_conversions.h" |
[email protected] | be528af | 2013-06-11 07:39:48 | [diff] [blame] | 14 | #include "base/strings/string_util.h" |
| 15 | #include "base/strings/stringprintf.h" |
[email protected] | 74f778e | 2014-03-14 21:11:46 | [diff] [blame] | 16 | #include "base/strings/utf_string_conversions.h" |
Gabriel Charette | 44db142 | 2018-08-06 11:19:33 | [diff] [blame] | 17 | #include "base/task/post_task.h" |
[email protected] | 398ad13 | 2013-04-02 15:11:01 | [diff] [blame] | 18 | #include "base/threading/thread_restrictions.h" |
Fabrice de Gans-Riberi | 7de4737 | 2018-05-08 20:23:47 | [diff] [blame] | 19 | #include "build/build_config.h" |
[email protected] | 398ad13 | 2013-04-02 15:11:01 | [diff] [blame] | 20 | |
| 21 | namespace { |
| 22 | |
| 23 | const int kMaxOldFolders = 100; |
| 24 | |
| 25 | // Returns a fully qualified name from path and name, using a given name prefix |
| 26 | // and index number. For instance, if the arguments are "/foo", "bar" and 5, it |
| 27 | // will return "/foo/old_bar_005". |
| 28 | base::FilePath GetPrefixedName(const base::FilePath& path, |
| 29 | const std::string& name, |
| 30 | int index) { |
| 31 | std::string tmp = base::StringPrintf("%s%s_%03d", "old_", |
| 32 | name.c_str(), index); |
| 33 | return path.AppendASCII(tmp); |
| 34 | } |
| 35 | |
| 36 | // This is a simple callback to cleanup old caches. |
| 37 | void CleanupCallback(const base::FilePath& path, const std::string& name) { |
| 38 | for (int i = 0; i < kMaxOldFolders; i++) { |
| 39 | base::FilePath to_delete = GetPrefixedName(path, name, i); |
| 40 | disk_cache::DeleteCache(to_delete, true); |
| 41 | } |
| 42 | } |
| 43 | |
| 44 | // Returns a full path to rename the current cache, in order to delete it. path |
| 45 | // is the current folder location, and name is the current folder name. |
| 46 | base::FilePath GetTempCacheName(const base::FilePath& path, |
| 47 | const std::string& name) { |
| 48 | // We'll attempt to have up to kMaxOldFolders folders for deletion. |
| 49 | for (int i = 0; i < kMaxOldFolders; i++) { |
| 50 | base::FilePath to_delete = GetPrefixedName(path, name, i); |
[email protected] | 756748414 | 2013-07-11 17:36:07 | [diff] [blame] | 51 | if (!base::PathExists(to_delete)) |
[email protected] | 398ad13 | 2013-04-02 15:11:01 | [diff] [blame] | 52 | return to_delete; |
| 53 | } |
| 54 | return base::FilePath(); |
| 55 | } |
| 56 | |
avi | d0181f3 | 2015-12-10 19:41:47 | [diff] [blame] | 57 | int64_t PreferredCacheSizeInternal(int64_t available) { |
[email protected] | 18b5b26 | 2013-11-19 18:41:37 | [diff] [blame] | 58 | using disk_cache::kDefaultCacheSize; |
| 59 | // Return 80% of the available space if there is not enough space to use |
| 60 | // kDefaultCacheSize. |
| 61 | if (available < kDefaultCacheSize * 10 / 8) |
| 62 | return available * 8 / 10; |
| 63 | |
| 64 | // Return kDefaultCacheSize if it uses 10% to 80% of the available space. |
| 65 | if (available < kDefaultCacheSize * 10) |
| 66 | return kDefaultCacheSize; |
| 67 | |
| 68 | // Return 10% of the available space if the target size |
| 69 | // (2.5 * kDefaultCacheSize) is more than 10%. |
avi | d0181f3 | 2015-12-10 19:41:47 | [diff] [blame] | 70 | if (available < static_cast<int64_t>(kDefaultCacheSize) * 25) |
[email protected] | 18b5b26 | 2013-11-19 18:41:37 | [diff] [blame] | 71 | return available / 10; |
| 72 | |
| 73 | // Return the target size (2.5 * kDefaultCacheSize) if it uses 10% to 1% |
| 74 | // of the available space. |
avi | d0181f3 | 2015-12-10 19:41:47 | [diff] [blame] | 75 | if (available < static_cast<int64_t>(kDefaultCacheSize) * 250) |
[email protected] | 18b5b26 | 2013-11-19 18:41:37 | [diff] [blame] | 76 | return kDefaultCacheSize * 5 / 2; |
| 77 | |
| 78 | // Return 1% of the available space. |
| 79 | return available / 100; |
| 80 | } |
| 81 | |
[email protected] | 398ad13 | 2013-04-02 15:11:01 | [diff] [blame] | 82 | } // namespace |
| 83 | |
| 84 | namespace disk_cache { |
| 85 | |
[email protected] | 18b5b26 | 2013-11-19 18:41:37 | [diff] [blame] | 86 | const int kDefaultCacheSize = 80 * 1024 * 1024; |
| 87 | |
Matt Menke | b0f4c7c | 2018-07-06 19:25:30 | [diff] [blame] | 88 | const base::Feature kChangeDiskCacheSizeExperiment{ |
| 89 | "ChangeDiskCacheSize", base::FEATURE_DISABLED_BY_DEFAULT}; |
| 90 | |
[email protected] | 76ed979ee | 2013-07-17 15:50:45 | [diff] [blame] | 91 | void DeleteCache(const base::FilePath& path, bool remove_folder) { |
| 92 | if (remove_folder) { |
| 93 | if (!base::DeleteFile(path, /* recursive */ true)) |
| 94 | LOG(WARNING) << "Unable to delete cache folder."; |
| 95 | return; |
| 96 | } |
| 97 | |
| 98 | base::FileEnumerator iter( |
| 99 | path, |
| 100 | /* recursive */ false, |
| 101 | base::FileEnumerator::FILES | base::FileEnumerator::DIRECTORIES); |
| 102 | for (base::FilePath file = iter.Next(); !file.value().empty(); |
| 103 | file = iter.Next()) { |
| 104 | if (!base::DeleteFile(file, /* recursive */ true)) { |
| 105 | LOG(WARNING) << "Unable to delete cache."; |
| 106 | return; |
| 107 | } |
| 108 | } |
| 109 | } |
| 110 | |
[email protected] | 398ad13 | 2013-04-02 15:11:01 | [diff] [blame] | 111 | // In order to process a potentially large number of files, we'll rename the |
| 112 | // cache directory to old_ + original_name + number, (located on the same parent |
| 113 | // directory), and use a worker thread to delete all the files on all the stale |
| 114 | // cache directories. The whole process can still fail if we are not able to |
| 115 | // rename the cache directory (for instance due to a sharing violation), and in |
| 116 | // that case a cache for this profile (on the desired path) cannot be created. |
| 117 | bool DelayedCacheCleanup(const base::FilePath& full_path) { |
| 118 | // GetTempCacheName() and MoveCache() use synchronous file |
| 119 | // operations. |
| 120 | base::ThreadRestrictions::ScopedAllowIO allow_io; |
| 121 | |
| 122 | base::FilePath current_path = full_path.StripTrailingSeparators(); |
| 123 | |
| 124 | base::FilePath path = current_path.DirName(); |
| 125 | base::FilePath name = current_path.BaseName(); |
Fabrice de Gans-Riberi | 7de4737 | 2018-05-08 20:23:47 | [diff] [blame] | 126 | #if defined(OS_WIN) |
[email protected] | 398ad13 | 2013-04-02 15:11:01 | [diff] [blame] | 127 | // We created this file so it should only contain ASCII. |
[email protected] | 74f778e | 2014-03-14 21:11:46 | [diff] [blame] | 128 | std::string name_str = base::UTF16ToASCII(name.value()); |
Fabrice de Gans-Riberi | 7de4737 | 2018-05-08 20:23:47 | [diff] [blame] | 129 | #elif defined(OS_POSIX) || defined(OS_FUCHSIA) |
| 130 | std::string name_str = name.value(); |
[email protected] | 398ad13 | 2013-04-02 15:11:01 | [diff] [blame] | 131 | #endif |
| 132 | |
| 133 | base::FilePath to_delete = GetTempCacheName(path, name_str); |
| 134 | if (to_delete.empty()) { |
| 135 | LOG(ERROR) << "Unable to get another cache folder"; |
| 136 | return false; |
| 137 | } |
| 138 | |
| 139 | if (!disk_cache::MoveCache(full_path, to_delete)) { |
| 140 | LOG(ERROR) << "Unable to move cache folder " << full_path.value() << " to " |
| 141 | << to_delete.value(); |
| 142 | return false; |
| 143 | } |
| 144 | |
fdoray | 2f60799d | 2017-05-03 22:54:58 | [diff] [blame] | 145 | base::PostTaskWithTraits(FROM_HERE, |
Gabriel Charette | b10aeeb | 2018-07-26 20:15:00 | [diff] [blame] | 146 | {base::MayBlock(), base::TaskPriority::BEST_EFFORT, |
fdoray | 2f60799d | 2017-05-03 22:54:58 | [diff] [blame] | 147 | base::TaskShutdownBehavior::CONTINUE_ON_SHUTDOWN}, |
| 148 | base::Bind(&CleanupCallback, path, name_str)); |
[email protected] | 398ad13 | 2013-04-02 15:11:01 | [diff] [blame] | 149 | return true; |
| 150 | } |
| 151 | |
[email protected] | 18b5b26 | 2013-11-19 18:41:37 | [diff] [blame] | 152 | // Returns the preferred maximum number of bytes for the cache given the |
| 153 | // number of available bytes. |
avi | d0181f3 | 2015-12-10 19:41:47 | [diff] [blame] | 154 | int PreferredCacheSize(int64_t available) { |
Matt Menke | b0f4c7c | 2018-07-06 19:25:30 | [diff] [blame] | 155 | // Percent of cache size to use, relative to the default size. "100" means to |
| 156 | // use 100% of the default size. |
| 157 | int percent_relative_size; |
| 158 | std::map<std::string, std::string> params; |
| 159 | if (!base::GetFieldTrialParamsByFeature( |
| 160 | disk_cache::kChangeDiskCacheSizeExperiment, ¶ms) || |
| 161 | !base::StringToInt(params["percent_relative_size"], |
| 162 | &percent_relative_size) || |
| 163 | percent_relative_size <= 0) { |
| 164 | percent_relative_size = 100; |
| 165 | } |
| 166 | |
| 167 | // Cap scaling, as a safety check, to avoid overflow. |
| 168 | if (percent_relative_size > 200) |
| 169 | percent_relative_size = 200; |
| 170 | |
| 171 | int64_t scaled_default_disk_cache_size = |
| 172 | static_cast<int64_t>(disk_cache::kDefaultCacheSize) * |
| 173 | percent_relative_size / 100; |
[email protected] | 18b5b26 | 2013-11-19 18:41:37 | [diff] [blame] | 174 | if (available < 0) |
Matt Menke | b0f4c7c | 2018-07-06 19:25:30 | [diff] [blame] | 175 | return static_cast<int32_t>(scaled_default_disk_cache_size); |
| 176 | |
| 177 | int64_t preferred_cache_size = PreferredCacheSizeInternal(available); |
| 178 | |
| 179 | // If the preferred cache size is less 20% of the available space, scale for |
| 180 | // the field trial, capping the scaled value at 20% of the available space. |
| 181 | if (preferred_cache_size < available / 5) { |
| 182 | preferred_cache_size = preferred_cache_size * percent_relative_size / 100; |
| 183 | if (preferred_cache_size > available / 5) |
| 184 | preferred_cache_size = available / 5; |
| 185 | } |
[email protected] | 18b5b26 | 2013-11-19 18:41:37 | [diff] [blame] | 186 | |
[email protected] | 18b5b26 | 2013-11-19 18:41:37 | [diff] [blame] | 187 | // Limit cache size to somewhat less than kint32max to avoid potential |
| 188 | // integer overflows in cache backend implementations. |
Matt Menke | b0f4c7c | 2018-07-06 19:25:30 | [diff] [blame] | 189 | DCHECK_LT(scaled_default_disk_cache_size * 4, |
| 190 | std::numeric_limits<int32_t>::max()); |
avi | d0181f3 | 2015-12-10 19:41:47 | [diff] [blame] | 191 | return static_cast<int32_t>( |
Matt Menke | b0f4c7c | 2018-07-06 19:25:30 | [diff] [blame] | 192 | std::min(preferred_cache_size, |
| 193 | static_cast<int64_t>(scaled_default_disk_cache_size * 4))); |
[email protected] | 18b5b26 | 2013-11-19 18:41:37 | [diff] [blame] | 194 | } |
| 195 | |
[email protected] | 398ad13 | 2013-04-02 15:11:01 | [diff] [blame] | 196 | } // namespace disk_cache |