Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(258)

Side by Side Diff: net/disk_cache/cache_util.cc

Issue 12794003: Initialize the simple cache backend at runtime. (Closed) Base URL: http://git.chromium.org/chromium/src.git@master
Patch Set: upload from git Created 7 years, 9 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(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/stringprintf.h"
10 #include "base/threading/thread_restrictions.h"
11 #include "base/threading/worker_pool.h"
12
13 namespace {
14
15 const int kMaxOldFolders = 100;
16
17 // Returns a fully qualified name from path and name, using a given name prefix
18 // and index number. For instance, if the arguments are "/foo", "bar" and 5, it
19 // will return "/foo/old_bar_005".
20 base::FilePath GetPrefixedName(const base::FilePath& path,
21 const std::string& name,
22 int index) {
23 std::string tmp = base::StringPrintf("%s%s_%03d", "old_",
24 name.c_str(), index);
25 return path.AppendASCII(tmp);
26 }
27
28 // This is a simple callback to cleanup old caches.
29 void CleanupCallback(const base::FilePath& path, const std::string& name) {
30 for (int i = 0; i < kMaxOldFolders; i++) {
31 base::FilePath to_delete = GetPrefixedName(path, name, i);
32 disk_cache::DeleteCache(to_delete, true);
33 }
34 }
35
36 // Returns a full path to rename the current cache, in order to delete it. path
37 // is the current folder location, and name is the current folder name.
38 base::FilePath GetTempCacheName(const base::FilePath& path,
39 const std::string& name) {
40 // We'll attempt to have up to kMaxOldFolders folders for deletion.
41 for (int i = 0; i < kMaxOldFolders; i++) {
42 base::FilePath to_delete = GetPrefixedName(path, name, i);
43 if (!file_util::PathExists(to_delete))
44 return to_delete;
45 }
46 return base::FilePath();
47 }
48
49 } // namespace
50
51 namespace disk_cache {
52
53 // In order to process a potentially large number of files, we'll rename the
54 // cache directory to old_ + original_name + number, (located on the same parent
55 // directory), and spawn a worker thread to delete all the files on all the
56 // stale cache directories. The whole process can still fail if we are not able
57 // to rename the cache directory (for instance due to a sharing violation), and
58 // in that case a cache for this profile (on the desired path) cannot be
59 // 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
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698