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.
5 #include "net/disk_cache/cache_util.h"
7 #include "base/file_util.h"
8 #include "base/files/file_enumerator.h"
9 #include "base/location.h"
10 #include "base/strings/string_util.h"
11 #include "base/strings/stringprintf.h"
12 #include "base/threading/thread_restrictions.h"
13 #include "base/threading/worker_pool.h"
17 const int kMaxOldFolders
= 100;
19 // Returns a fully qualified name from path and name, using a given name prefix
20 // and index number. For instance, if the arguments are "/foo", "bar" and 5, it
21 // will return "/foo/old_bar_005".
22 base::FilePath
GetPrefixedName(const base::FilePath
& path
,
23 const std::string
& name
,
25 std::string tmp
= base::StringPrintf("%s%s_%03d", "old_",
27 return path
.AppendASCII(tmp
);
30 // This is a simple callback to cleanup old caches.
31 void CleanupCallback(const base::FilePath
& path
, const std::string
& name
) {
32 for (int i
= 0; i
< kMaxOldFolders
; i
++) {
33 base::FilePath to_delete
= GetPrefixedName(path
, name
, i
);
34 disk_cache::DeleteCache(to_delete
, true);
38 // Returns a full path to rename the current cache, in order to delete it. path
39 // is the current folder location, and name is the current folder name.
40 base::FilePath
GetTempCacheName(const base::FilePath
& path
,
41 const std::string
& name
) {
42 // We'll attempt to have up to kMaxOldFolders folders for deletion.
43 for (int i
= 0; i
< kMaxOldFolders
; i
++) {
44 base::FilePath to_delete
= GetPrefixedName(path
, name
, i
);
45 if (!base::PathExists(to_delete
))
48 return base::FilePath();
53 namespace disk_cache
{
55 void DeleteCache(const base::FilePath
& path
, bool remove_folder
) {
57 if (!base::DeleteFile(path
, /* recursive */ true))
58 LOG(WARNING
) << "Unable to delete cache folder.";
62 base::FileEnumerator
iter(
64 /* recursive */ false,
65 base::FileEnumerator::FILES
| base::FileEnumerator::DIRECTORIES
);
66 for (base::FilePath file
= iter
.Next(); !file
.value().empty();
68 if (!base::DeleteFile(file
, /* recursive */ true)) {
69 LOG(WARNING
) << "Unable to delete cache.";
75 // In order to process a potentially large number of files, we'll rename the
76 // cache directory to old_ + original_name + number, (located on the same parent
77 // directory), and use a worker thread to delete all the files on all the stale
78 // cache directories. The whole process can still fail if we are not able to
79 // rename the cache directory (for instance due to a sharing violation), and in
80 // that case a cache for this profile (on the desired path) cannot be created.
81 bool DelayedCacheCleanup(const base::FilePath
& full_path
) {
82 // GetTempCacheName() and MoveCache() use synchronous file
84 base::ThreadRestrictions::ScopedAllowIO allow_io
;
86 base::FilePath current_path
= full_path
.StripTrailingSeparators();
88 base::FilePath path
= current_path
.DirName();
89 base::FilePath name
= current_path
.BaseName();
91 std::string name_str
= name
.value();
93 // We created this file so it should only contain ASCII.
94 std::string name_str
= WideToASCII(name
.value());
97 base::FilePath to_delete
= GetTempCacheName(path
, name_str
);
98 if (to_delete
.empty()) {
99 LOG(ERROR
) << "Unable to get another cache folder";
103 if (!disk_cache::MoveCache(full_path
, to_delete
)) {
104 LOG(ERROR
) << "Unable to move cache folder " << full_path
.value() << " to "
105 << to_delete
.value();
109 base::WorkerPool::PostTask(
110 FROM_HERE
, base::Bind(&CleanupCallback
, path
, name_str
), true);
114 } // namespace disk_cache