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