blob: 5dea20dbda63319935943664a42a4e04f1176a4a [file] [log] [blame]
[email protected]398ad132013-04-02 15:11:011// 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
avid0181f32015-12-10 19:41:477#include <limits>
8
[email protected]76ed979ee2013-07-17 15:50:459#include "base/files/file_enumerator.h"
thestigd8df0332014-09-04 06:33:2910#include "base/files/file_util.h"
[email protected]398ad132013-04-02 15:11:0111#include "base/location.h"
Matt Menkeb0f4c7c2018-07-06 19:25:3012#include "base/metrics/field_trial_params.h"
13#include "base/strings/string_number_conversions.h"
[email protected]be528af2013-06-11 07:39:4814#include "base/strings/string_util.h"
15#include "base/strings/stringprintf.h"
[email protected]74f778e2014-03-14 21:11:4616#include "base/strings/utf_string_conversions.h"
Gabriel Charette44db1422018-08-06 11:19:3317#include "base/task/post_task.h"
[email protected]398ad132013-04-02 15:11:0118#include "base/threading/thread_restrictions.h"
Fabrice de Gans-Riberi7de47372018-05-08 20:23:4719#include "build/build_config.h"
[email protected]398ad132013-04-02 15:11:0120
21namespace {
22
23const 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".
28base::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.
37void 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.
46base::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]7567484142013-07-11 17:36:0751 if (!base::PathExists(to_delete))
[email protected]398ad132013-04-02 15:11:0152 return to_delete;
53 }
54 return base::FilePath();
55}
56
avid0181f32015-12-10 19:41:4757int64_t PreferredCacheSizeInternal(int64_t available) {
[email protected]18b5b262013-11-19 18:41:3758 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%.
avid0181f32015-12-10 19:41:4770 if (available < static_cast<int64_t>(kDefaultCacheSize) * 25)
[email protected]18b5b262013-11-19 18:41:3771 return available / 10;
72
73 // Return the target size (2.5 * kDefaultCacheSize) if it uses 10% to 1%
74 // of the available space.
avid0181f32015-12-10 19:41:4775 if (available < static_cast<int64_t>(kDefaultCacheSize) * 250)
[email protected]18b5b262013-11-19 18:41:3776 return kDefaultCacheSize * 5 / 2;
77
78 // Return 1% of the available space.
79 return available / 100;
80}
81
[email protected]398ad132013-04-02 15:11:0182} // namespace
83
84namespace disk_cache {
85
[email protected]18b5b262013-11-19 18:41:3786const int kDefaultCacheSize = 80 * 1024 * 1024;
87
Matt Menkeb0f4c7c2018-07-06 19:25:3088const base::Feature kChangeDiskCacheSizeExperiment{
89 "ChangeDiskCacheSize", base::FEATURE_DISABLED_BY_DEFAULT};
90
[email protected]76ed979ee2013-07-17 15:50:4591void 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]398ad132013-04-02 15:11:01111// 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.
117bool 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-Riberi7de47372018-05-08 20:23:47126#if defined(OS_WIN)
[email protected]398ad132013-04-02 15:11:01127 // We created this file so it should only contain ASCII.
[email protected]74f778e2014-03-14 21:11:46128 std::string name_str = base::UTF16ToASCII(name.value());
Fabrice de Gans-Riberi7de47372018-05-08 20:23:47129#elif defined(OS_POSIX) || defined(OS_FUCHSIA)
130 std::string name_str = name.value();
[email protected]398ad132013-04-02 15:11:01131#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
fdoray2f60799d2017-05-03 22:54:58145 base::PostTaskWithTraits(FROM_HERE,
Gabriel Charetteb10aeeb2018-07-26 20:15:00146 {base::MayBlock(), base::TaskPriority::BEST_EFFORT,
fdoray2f60799d2017-05-03 22:54:58147 base::TaskShutdownBehavior::CONTINUE_ON_SHUTDOWN},
148 base::Bind(&CleanupCallback, path, name_str));
[email protected]398ad132013-04-02 15:11:01149 return true;
150}
151
[email protected]18b5b262013-11-19 18:41:37152// Returns the preferred maximum number of bytes for the cache given the
153// number of available bytes.
avid0181f32015-12-10 19:41:47154int PreferredCacheSize(int64_t available) {
Matt Menkeb0f4c7c2018-07-06 19:25:30155 // 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, &params) ||
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]18b5b262013-11-19 18:41:37174 if (available < 0)
Matt Menkeb0f4c7c2018-07-06 19:25:30175 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]18b5b262013-11-19 18:41:37186
[email protected]18b5b262013-11-19 18:41:37187 // Limit cache size to somewhat less than kint32max to avoid potential
188 // integer overflows in cache backend implementations.
Matt Menkeb0f4c7c2018-07-06 19:25:30189 DCHECK_LT(scaled_default_disk_cache_size * 4,
190 std::numeric_limits<int32_t>::max());
avid0181f32015-12-10 19:41:47191 return static_cast<int32_t>(
Matt Menkeb0f4c7c2018-07-06 19:25:30192 std::min(preferred_cache_size,
193 static_cast<int64_t>(scaled_default_disk_cache_size * 4)));
[email protected]18b5b262013-11-19 18:41:37194}
195
[email protected]398ad132013-04-02 15:11:01196} // namespace disk_cache