blob: fa186759bc40028be3b04f68668be1d42722b476 [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"
[email protected]be528af2013-06-11 07:39:4812#include "base/strings/string_util.h"
13#include "base/strings/stringprintf.h"
[email protected]74f778e2014-03-14 21:11:4614#include "base/strings/utf_string_conversions.h"
fdoray974b0272017-02-28 03:00:4115#include "base/task_scheduler/post_task.h"
[email protected]398ad132013-04-02 15:11:0116#include "base/threading/thread_restrictions.h"
[email protected]398ad132013-04-02 15:11:0117
18namespace {
19
20const 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".
25base::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.
34void 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.
43base::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]7567484142013-07-11 17:36:0748 if (!base::PathExists(to_delete))
[email protected]398ad132013-04-02 15:11:0149 return to_delete;
50 }
51 return base::FilePath();
52}
53
avid0181f32015-12-10 19:41:4754int64_t PreferredCacheSizeInternal(int64_t available) {
[email protected]18b5b262013-11-19 18:41:3755 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%.
avid0181f32015-12-10 19:41:4767 if (available < static_cast<int64_t>(kDefaultCacheSize) * 25)
[email protected]18b5b262013-11-19 18:41:3768 return available / 10;
69
70 // Return the target size (2.5 * kDefaultCacheSize) if it uses 10% to 1%
71 // of the available space.
avid0181f32015-12-10 19:41:4772 if (available < static_cast<int64_t>(kDefaultCacheSize) * 250)
[email protected]18b5b262013-11-19 18:41:3773 return kDefaultCacheSize * 5 / 2;
74
75 // Return 1% of the available space.
76 return available / 100;
77}
78
[email protected]398ad132013-04-02 15:11:0179} // namespace
80
81namespace disk_cache {
82
[email protected]18b5b262013-11-19 18:41:3783const int kDefaultCacheSize = 80 * 1024 * 1024;
84
[email protected]76ed979ee2013-07-17 15:50:4585void 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]398ad132013-04-02 15:11:01105// 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.
111bool 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]74f778e2014-03-14 21:11:46124 std::string name_str = base::UTF16ToASCII(name.value());
[email protected]398ad132013-04-02 15:11:01125#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
fdoray2f60799d2017-05-03 22:54:58139 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]398ad132013-04-02 15:11:01143 return true;
144}
145
[email protected]18b5b262013-11-19 18:41:37146// Returns the preferred maximum number of bytes for the cache given the
147// number of available bytes.
avid0181f32015-12-10 19:41:47148int PreferredCacheSize(int64_t available) {
[email protected]18b5b262013-11-19 18:41:37149 if (available < 0)
150 return kDefaultCacheSize;
151
[email protected]18b5b262013-11-19 18:41:37152 // Limit cache size to somewhat less than kint32max to avoid potential
153 // integer overflows in cache backend implementations.
avid0181f32015-12-10 19:41:47154 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]18b5b262013-11-19 18:41:37158}
159
[email protected]398ad132013-04-02 15:11:01160} // namespace disk_cache