1 // Copyright 2012 The Chromium Authors
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
7 #include <limits>
8
9 #include "base/files/file_enumerator.h"
10 #include "base/files/file_path.h"
11 #include "base/files/file_util.h"
12 #include "base/files/safe_base_name.h"
13 #include "base/functional/bind.h"
14 #include "base/location.h"
15 #include "base/metrics/field_trial_params.h"
16 #include "base/numerics/clamped_math.h"
17 #include "base/numerics/ostream_operators.h"
18 #include "base/strings/strcat.h"
19 #include "base/strings/string_util.h"
20 #include "base/strings/stringprintf.h"
21 #include "base/strings/utf_string_conversions.h"
22 #include "base/task/bind_post_task.h"
23 #include "base/task/thread_pool.h"
24 #include "base/threading/thread_restrictions.h"
25 #include "build/build_config.h"
26
27 namespace {
28
29 const int kMaxOldFolders = 100;
30
31 // Returns a fully qualified name from path and name, using a given name prefix
32 // and index number. For instance, if the arguments are "/foo", "bar" and 5, it
33 // will return "/foo/old_bar_005".
GetPrefixedName(const base::FilePath & path,const base::SafeBaseName & basename,int index)34 base::FilePath GetPrefixedName(const base::FilePath& path,
35 const base::SafeBaseName& basename,
36 int index) {
37 const std::string index_str = base::StringPrintf("_%03d", index);
38 const base::FilePath::StringType filename = base::StrCat({
39 FILE_PATH_LITERAL("old_"), basename.path().value(),
40 #if BUILDFLAG(IS_WIN)
41 base::ASCIIToWide(index_str)
42 #else
43 index_str
44 #endif
45 });
46 return path.Append(filename);
47 }
48
GetTempCacheName(const base::FilePath & dirname,const base::SafeBaseName & basename)49 base::FilePath GetTempCacheName(const base::FilePath& dirname,
50 const base::SafeBaseName& basename) {
51 // We'll attempt to have up to kMaxOldFolders folders for deletion.
52 for (int i = 0; i < kMaxOldFolders; i++) {
53 base::FilePath to_delete = GetPrefixedName(dirname, basename, i);
54 if (!base::PathExists(to_delete))
55 return to_delete;
56 }
57 return base::FilePath();
58 }
59
CleanupTemporaryDirectories(const base::FilePath & path)60 void CleanupTemporaryDirectories(const base::FilePath& path) {
61 const base::FilePath dirname = path.DirName();
62 const std::optional<base::SafeBaseName> basename =
63 base::SafeBaseName::Create(path);
64 if (!basename.has_value()) {
65 return;
66 }
67 for (int i = 0; i < kMaxOldFolders; i++) {
68 base::FilePath to_delete = GetPrefixedName(dirname, *basename, i);
69 disk_cache::DeleteCache(to_delete, /*remove_folder=*/true);
70 }
71 }
72
MoveDirectoryToTemporaryDirectory(const base::FilePath & path)73 bool MoveDirectoryToTemporaryDirectory(const base::FilePath& path) {
74 const base::FilePath dirname = path.DirName();
75 const std::optional<base::SafeBaseName> basename =
76 base::SafeBaseName::Create(path);
77 if (!basename.has_value()) {
78 return false;
79 }
80 const base::FilePath destination = GetTempCacheName(dirname, *basename);
81 if (destination.empty()) {
82 return false;
83 }
84 return disk_cache::MoveCache(path, destination);
85 }
86
87 // In order to process a potentially large number of files, we'll rename the
88 // cache directory to old_ + original_name + number, (located on the same parent
89 // directory), and use a worker thread to delete all the files on all the stale
90 // cache directories. The whole process can still fail if we are not able to
91 // rename the cache directory (for instance due to a sharing violation), and in
92 // that case a cache for this profile (on the desired path) cannot be created.
CleanupDirectoryInternal(const base::FilePath & path)93 bool CleanupDirectoryInternal(const base::FilePath& path) {
94 const base::FilePath path_to_pass = path.StripTrailingSeparators();
95 bool result = MoveDirectoryToTemporaryDirectory(path_to_pass);
96
97 base::ThreadPool::PostTask(
98 FROM_HERE,
99 {base::MayBlock(), base::TaskPriority::BEST_EFFORT,
100 base::TaskShutdownBehavior::CONTINUE_ON_SHUTDOWN},
101 base::BindOnce(&CleanupTemporaryDirectories, path_to_pass));
102
103 return result;
104 }
105
PreferredCacheSizeInternal(int64_t available)106 int64_t PreferredCacheSizeInternal(int64_t available) {
107 using disk_cache::kDefaultCacheSize;
108 // Return 80% of the available space if there is not enough space to use
109 // kDefaultCacheSize.
110 if (available < kDefaultCacheSize * 10 / 8)
111 return available * 8 / 10;
112
113 // Return kDefaultCacheSize if it uses 10% to 80% of the available space.
114 if (available < kDefaultCacheSize * 10)
115 return kDefaultCacheSize;
116
117 // Return 10% of the available space if the target size
118 // (2.5 * kDefaultCacheSize) is more than 10%.
119 if (available < static_cast<int64_t>(kDefaultCacheSize) * 25)
120 return available / 10;
121
122 // Return the target size (2.5 * kDefaultCacheSize) if it uses 10% to 1%
123 // of the available space.
124 if (available < static_cast<int64_t>(kDefaultCacheSize) * 250)
125 return kDefaultCacheSize * 5 / 2;
126
127 // Return 1% of the available space.
128 return available / 100;
129 }
130
131 } // namespace
132
133 namespace disk_cache {
134
135 const int kDefaultCacheSize = 80 * 1024 * 1024;
136
137 BASE_FEATURE(kChangeDiskCacheSizeExperiment,
138 "ChangeDiskCacheSize",
139 base::FEATURE_DISABLED_BY_DEFAULT);
140
DeleteCache(const base::FilePath & path,bool remove_folder)141 void DeleteCache(const base::FilePath& path, bool remove_folder) {
142 if (remove_folder) {
143 if (!base::DeletePathRecursively(path))
144 LOG(WARNING) << "Unable to delete cache folder.";
145 return;
146 }
147
148 base::FileEnumerator iter(
149 path,
150 /* recursive */ false,
151 base::FileEnumerator::FILES | base::FileEnumerator::DIRECTORIES);
152 for (base::FilePath file = iter.Next(); !file.value().empty();
153 file = iter.Next()) {
154 if (!base::DeletePathRecursively(file)) {
155 LOG(WARNING) << "Unable to delete cache.";
156 return;
157 }
158 }
159 }
160
CleanupDirectory(const base::FilePath & path,base::OnceCallback<void (bool)> callback)161 void CleanupDirectory(const base::FilePath& path,
162 base::OnceCallback<void(bool)> callback) {
163 auto task_runner = base::ThreadPool::CreateSequencedTaskRunner(
164 {base::MayBlock(), base::TaskPriority::USER_BLOCKING,
165 base::TaskShutdownBehavior::CONTINUE_ON_SHUTDOWN});
166
167 task_runner->PostTaskAndReplyWithResult(
168 FROM_HERE, base::BindOnce(CleanupDirectoryInternal, path),
169 std::move(callback));
170 }
171
CleanupDirectorySync(const base::FilePath & path)172 bool CleanupDirectorySync(const base::FilePath& path) {
173 base::ScopedAllowBlocking allow_blocking;
174
175 return CleanupDirectoryInternal(path);
176 }
177
178 // Returns the preferred maximum number of bytes for the cache given the
179 // number of available bytes.
PreferredCacheSize(int64_t available,net::CacheType type)180 int PreferredCacheSize(int64_t available, net::CacheType type) {
181 // Percent of cache size to use, relative to the default size. "100" means to
182 // use 100% of the default size.
183 int percent_relative_size = 100;
184
185 if (base::FeatureList::IsEnabled(
186 disk_cache::kChangeDiskCacheSizeExperiment) &&
187 type == net::DISK_CACHE) {
188 percent_relative_size = base::GetFieldTrialParamByFeatureAsInt(
189 disk_cache::kChangeDiskCacheSizeExperiment, "percent_relative_size",
190 100 /* default value */);
191 }
192
193 // Cap scaling, as a safety check, to avoid overflow.
194 if (percent_relative_size > 400)
195 percent_relative_size = 400;
196 else if (percent_relative_size < 100)
197 percent_relative_size = 100;
198
199 base::ClampedNumeric<int64_t> scaled_default_disk_cache_size =
200 (base::ClampedNumeric<int64_t>(disk_cache::kDefaultCacheSize) *
201 percent_relative_size) /
202 100;
203
204 base::ClampedNumeric<int64_t> preferred_cache_size =
205 scaled_default_disk_cache_size;
206
207 // If available disk space is known, use it to compute a better value for
208 // preferred_cache_size.
209 if (available >= 0) {
210 preferred_cache_size = PreferredCacheSizeInternal(available);
211
212 // If the preferred cache size is less than 20% of the available space,
213 // scale for the field trial, capping the scaled value at 20% of the
214 // available space.
215 if (preferred_cache_size < available / 5) {
216 const base::ClampedNumeric<int64_t> clamped_available(available);
217 preferred_cache_size =
218 std::min((preferred_cache_size * percent_relative_size) / 100,
219 clamped_available / 5);
220 }
221 }
222
223 // Limit cache size to somewhat less than kint32max to avoid potential
224 // integer overflows in cache backend implementations.
225 //
226 // Note: the 4x limit is of course far below that; historically it came
227 // from the blockfile backend with the following explanation:
228 // "Let's not use more than the default size while we tune-up the performance
229 // of bigger caches. "
230 base::ClampedNumeric<int64_t> size_limit = scaled_default_disk_cache_size * 4;
231 // Native code entries can be large, so we would like a larger cache.
232 // Make the size limit 50% larger in that case.
233 if (type == net::GENERATED_NATIVE_CODE_CACHE) {
234 size_limit = (size_limit / 2) * 3;
235 } else if (type == net::GENERATED_WEBUI_BYTE_CODE_CACHE) {
236 size_limit = std::min(
237 size_limit, base::ClampedNumeric<int64_t>(kMaxWebUICodeCacheSize));
238 }
239
240 DCHECK_LT(size_limit, std::numeric_limits<int32_t>::max());
241 return static_cast<int32_t>(std::min(preferred_cache_size, size_limit));
242 }
243
244 } // namespace disk_cache
245