Home | History | Annotate | Download | only in disk_cache
      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/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"
     14 
     15 namespace {
     16 
     17 const int kMaxOldFolders = 100;
     18 
     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,
     24                                int index) {
     25   std::string tmp = base::StringPrintf("%s%s_%03d", "old_",
     26                                        name.c_str(), index);
     27   return path.AppendASCII(tmp);
     28 }
     29 
     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);
     35   }
     36 }
     37 
     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))
     46       return to_delete;
     47   }
     48   return base::FilePath();
     49 }
     50 
     51 }  // namespace
     52 
     53 namespace disk_cache {
     54 
     55 void DeleteCache(const base::FilePath& path, bool remove_folder) {
     56   if (remove_folder) {
     57     if (!base::DeleteFile(path, /* recursive */ true))
     58       LOG(WARNING) << "Unable to delete cache folder.";
     59     return;
     60   }
     61 
     62   base::FileEnumerator iter(
     63       path,
     64       /* recursive */ false,
     65       base::FileEnumerator::FILES | base::FileEnumerator::DIRECTORIES);
     66   for (base::FilePath file = iter.Next(); !file.value().empty();
     67        file = iter.Next()) {
     68     if (!base::DeleteFile(file, /* recursive */ true)) {
     69       LOG(WARNING) << "Unable to delete cache.";
     70       return;
     71     }
     72   }
     73 }
     74 
     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
     83   // operations.
     84   base::ThreadRestrictions::ScopedAllowIO allow_io;
     85 
     86   base::FilePath current_path = full_path.StripTrailingSeparators();
     87 
     88   base::FilePath path = current_path.DirName();
     89   base::FilePath name = current_path.BaseName();
     90 #if defined(OS_POSIX)
     91   std::string name_str = name.value();
     92 #elif defined(OS_WIN)
     93   // We created this file so it should only contain ASCII.
     94   std::string name_str = WideToASCII(name.value());
     95 #endif
     96 
     97   base::FilePath to_delete = GetTempCacheName(path, name_str);
     98   if (to_delete.empty()) {
     99     LOG(ERROR) << "Unable to get another cache folder";
    100     return false;
    101   }
    102 
    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();
    106     return false;
    107   }
    108 
    109   base::WorkerPool::PostTask(
    110       FROM_HERE, base::Bind(&CleanupCallback, path, name_str), true);
    111   return true;
    112 }
    113 
    114 }  // namespace disk_cache
    115