15821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Copyright (c) 2011 The Chromium Authors. All rights reserved.
25821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Use of this source code is governed by a BSD-style license that can be
35821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// found in the LICENSE file.
45821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
55821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "net/disk_cache/cache_util.h"
65821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
7868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)#include "base/files/file_enumerator.h"
81320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci#include "base/files/file_util.h"
95821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "base/logging.h"
105e3f23d412006dc4db4e659864679f29341e113fTorne (Richard Coles)#include "base/strings/string_util.h"
115821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
125821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)namespace disk_cache {
135821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
142a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)bool MoveCache(const base::FilePath& from_path, const base::FilePath& to_path) {
155821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#if defined(OS_CHROMEOS)
165821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // For ChromeOS, we don't actually want to rename the cache
175821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // directory, because if we do, then it'll get recreated through the
185821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // encrypted filesystem (with encrypted names), and we won't be able
195821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // to see these directories anymore in an unmounted encrypted
205821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // filesystem, so we just move each item in the cache to a new
215821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // directory.
22a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)  if (!base::CreateDirectory(to_path)) {
235821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    LOG(ERROR) << "Unable to create destination cache directory.";
245821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    return false;
255821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  }
26868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  base::FileEnumerator iter(from_path, false /* not recursive */,
27868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)      base::FileEnumerator::DIRECTORIES | base::FileEnumerator::FILES);
282a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  for (base::FilePath name = iter.Next(); !name.value().empty();
292a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)       name = iter.Next()) {
302a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    base::FilePath destination = to_path.Append(name.BaseName());
31eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch    if (!base::Move(name, destination)) {
325821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      LOG(ERROR) << "Unable to move cache item.";
335821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      return false;
345821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    }
355821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  }
365821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  return true;
375821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#else
38eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch  return base::Move(from_path, to_path);
395821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#endif
405821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
415821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
422a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)bool DeleteCacheFile(const base::FilePath& name) {
437dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch  return base::DeleteFile(name, false);
445821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
455821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
465821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}  // namespace disk_cache
47