OLD | NEW |
(Empty) | |
| 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. |
| 4 |
| 5 #include "net/disk_cache/cache_util.h" |
| 6 |
| 7 #include "base/file_util.h" |
| 8 #include "base/location.h" |
| 9 #include "base/string_util.h" |
| 10 #include "base/stringprintf.h" |
| 11 #include "base/threading/thread_restrictions.h" |
| 12 #include "base/threading/worker_pool.h" |
| 13 |
| 14 namespace { |
| 15 |
| 16 const int kMaxOldFolders = 100; |
| 17 |
| 18 // Returns a fully qualified name from path and name, using a given name prefix |
| 19 // and index number. For instance, if the arguments are "/foo", "bar" and 5, it |
| 20 // will return "/foo/old_bar_005". |
| 21 base::FilePath GetPrefixedName(const base::FilePath& path, |
| 22 const std::string& name, |
| 23 int index) { |
| 24 std::string tmp = base::StringPrintf("%s%s_%03d", "old_", |
| 25 name.c_str(), index); |
| 26 return path.AppendASCII(tmp); |
| 27 } |
| 28 |
| 29 // This is a simple callback to cleanup old caches. |
| 30 void CleanupCallback(const base::FilePath& path, const std::string& name) { |
| 31 for (int i = 0; i < kMaxOldFolders; i++) { |
| 32 base::FilePath to_delete = GetPrefixedName(path, name, i); |
| 33 disk_cache::DeleteCache(to_delete, true); |
| 34 } |
| 35 } |
| 36 |
| 37 // Returns a full path to rename the current cache, in order to delete it. path |
| 38 // is the current folder location, and name is the current folder name. |
| 39 base::FilePath GetTempCacheName(const base::FilePath& path, |
| 40 const std::string& name) { |
| 41 // We'll attempt to have up to kMaxOldFolders folders for deletion. |
| 42 for (int i = 0; i < kMaxOldFolders; i++) { |
| 43 base::FilePath to_delete = GetPrefixedName(path, name, i); |
| 44 if (!file_util::PathExists(to_delete)) |
| 45 return to_delete; |
| 46 } |
| 47 return base::FilePath(); |
| 48 } |
| 49 |
| 50 } // namespace |
| 51 |
| 52 namespace disk_cache { |
| 53 |
| 54 // In order to process a potentially large number of files, we'll rename the |
| 55 // cache directory to old_ + original_name + number, (located on the same parent |
| 56 // directory), and use a worker thread to delete all the files on all the stale |
| 57 // cache directories. The whole process can still fail if we are not able to |
| 58 // rename the cache directory (for instance due to a sharing violation), and in |
| 59 // that case a cache for this profile (on the desired path) cannot be created. |
| 60 bool DelayedCacheCleanup(const base::FilePath& full_path) { |
| 61 // GetTempCacheName() and MoveCache() use synchronous file |
| 62 // operations. |
| 63 base::ThreadRestrictions::ScopedAllowIO allow_io; |
| 64 |
| 65 base::FilePath current_path = full_path.StripTrailingSeparators(); |
| 66 |
| 67 base::FilePath path = current_path.DirName(); |
| 68 base::FilePath name = current_path.BaseName(); |
| 69 #if defined(OS_POSIX) |
| 70 std::string name_str = name.value(); |
| 71 #elif defined(OS_WIN) |
| 72 // We created this file so it should only contain ASCII. |
| 73 std::string name_str = WideToASCII(name.value()); |
| 74 #endif |
| 75 |
| 76 base::FilePath to_delete = GetTempCacheName(path, name_str); |
| 77 if (to_delete.empty()) { |
| 78 LOG(ERROR) << "Unable to get another cache folder"; |
| 79 return false; |
| 80 } |
| 81 |
| 82 if (!disk_cache::MoveCache(full_path, to_delete)) { |
| 83 LOG(ERROR) << "Unable to move cache folder " << full_path.value() << " to " |
| 84 << to_delete.value(); |
| 85 return false; |
| 86 } |
| 87 |
| 88 base::WorkerPool::PostTask( |
| 89 FROM_HERE, base::Bind(&CleanupCallback, path, name_str), true); |
| 90 return true; |
| 91 } |
| 92 |
| 93 } // namespace disk_cache |
OLD | NEW |