diff options
Diffstat (limited to 'net/disk_cache')
-rw-r--r-- | net/disk_cache/backend_impl.cc | 2 | ||||
-rw-r--r-- | net/disk_cache/cache_util.h | 6 | ||||
-rw-r--r-- | net/disk_cache/cache_util_posix.cc | 28 | ||||
-rw-r--r-- | net/disk_cache/cache_util_unittest.cc | 90 |
4 files changed, 123 insertions, 3 deletions
diff --git a/net/disk_cache/backend_impl.cc b/net/disk_cache/backend_impl.cc index fbc1e6a..5086e2b 100644 --- a/net/disk_cache/backend_impl.cc +++ b/net/disk_cache/backend_impl.cc @@ -133,7 +133,7 @@ bool DelayedCacheCleanup(const FilePath& full_path) { } if (!disk_cache::MoveCache(full_path, to_delete)) { - LOG(ERROR) << "Unable to rename cache folder"; + LOG(ERROR) << "Unable to move cache folder"; return false; } diff --git a/net/disk_cache/cache_util.h b/net/disk_cache/cache_util.h index 455bf30..04b9a07 100644 --- a/net/disk_cache/cache_util.h +++ b/net/disk_cache/cache_util.h @@ -13,7 +13,11 @@ class FilePath; namespace disk_cache { // Moves the cache files from the given path to another location. -// Returns true if successful, false otherwise. +// Fails if the destination exists already, or if it doesn't have +// permission for the operation. This is basically a rename operation +// for the cache directory. Returns true if successful. On ChromeOS, +// this moves the cache contents, and leaves the empty cache +// directory. bool MoveCache(const FilePath& from_path, const FilePath& to_path); // Deletes the cache files stored on |path|, and optionally also attempts to diff --git a/net/disk_cache/cache_util_posix.cc b/net/disk_cache/cache_util_posix.cc index b393747..bc87746 100644 --- a/net/disk_cache/cache_util_posix.cc +++ b/net/disk_cache/cache_util_posix.cc @@ -11,8 +11,34 @@ namespace disk_cache { bool MoveCache(const FilePath& from_path, const FilePath& to_path) { - // Just use the version from base. +#if defined(OS_CHROMEOS) + // For ChromeOS, we don't actually want to rename the cache + // directory, because if we do, then it'll get recreated through the + // encrypted filesystem (with encrypted names), and we won't be able + // to see these directories anymore in an unmounted encrypted + // filesystem, so we just move each item in the cache to a new + // directory. + if (!file_util::CreateDirectory(to_path)) { + LOG(ERROR) << "Unable to create destination cache directory."; + return false; + } + file_util::FileEnumerator iter( + from_path, + /* recursive */ false, + static_cast<file_util::FileEnumerator::FILE_TYPE>( + file_util::FileEnumerator::DIRECTORIES | + file_util::FileEnumerator::FILES)); + for (FilePath name = iter.Next(); !name.value().empty(); name = iter.Next()) { + FilePath destination = to_path.Append(name.BaseName()); + if (!file_util::Move(name, destination)) { + LOG(ERROR) << "Unable to move cache item."; + return false; + } + } + return true; +#else return file_util::Move(from_path, to_path); +#endif } void DeleteCache(const FilePath& path, bool remove_folder) { diff --git a/net/disk_cache/cache_util_unittest.cc b/net/disk_cache/cache_util_unittest.cc new file mode 100644 index 0000000..e616480 --- /dev/null +++ b/net/disk_cache/cache_util_unittest.cc @@ -0,0 +1,90 @@ +// Copyright (c) 2010 The Chromium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +#include "net/disk_cache/cache_util.h" +#include "base/file_util.h" +#include "base/scoped_temp_dir.h" +#include "testing/gtest/include/gtest/gtest.h" +#include "testing/platform_test.h" + +namespace disk_cache { + +class CacheUtilTest : public PlatformTest { + public: + void SetUp() { + PlatformTest::SetUp(); + ASSERT_TRUE(tmp_dir_.CreateUniqueTempDir()); + cache_dir_ = tmp_dir_.path().Append(FILE_PATH_LITERAL("Cache")); + file1_ = FilePath(cache_dir_.Append(FILE_PATH_LITERAL("file01"))); + file2_ = FilePath(cache_dir_.Append(FILE_PATH_LITERAL(".file02"))); + dir1_ = FilePath(cache_dir_.Append(FILE_PATH_LITERAL("dir01"))); + ASSERT_TRUE(file_util::CreateDirectory(cache_dir_)); + FILE *fp = file_util::OpenFile(file1_, "w"); + ASSERT_TRUE(fp != NULL); + file_util::CloseFile(fp); + fp = file_util::OpenFile(file2_, "w"); + ASSERT_TRUE(fp != NULL); + file_util::CloseFile(fp); + ASSERT_TRUE(file_util::CreateDirectory(dir1_)); + dest_dir_ = tmp_dir_.path().Append(FILE_PATH_LITERAL("old_Cache_001")); + dest_file1_ = FilePath(dest_dir_.Append(FILE_PATH_LITERAL("file01"))); + dest_file2_ = FilePath(dest_dir_.Append(FILE_PATH_LITERAL(".file02"))); + dest_dir1_ = FilePath(dest_dir_.Append(FILE_PATH_LITERAL("dir01"))); + } + + protected: + ScopedTempDir tmp_dir_; + FilePath cache_dir_; + FilePath file1_; + FilePath file2_; + FilePath dir1_; + FilePath dest_dir_; + FilePath dest_file1_; + FilePath dest_file2_; + FilePath dest_dir1_; +}; + +TEST_F(CacheUtilTest, MoveCache) { + EXPECT_TRUE(disk_cache::MoveCache(cache_dir_, dest_dir_)); + EXPECT_TRUE(file_util::PathExists(dest_dir_)); + EXPECT_TRUE(file_util::PathExists(dest_file1_)); + EXPECT_TRUE(file_util::PathExists(dest_file2_)); + EXPECT_TRUE(file_util::PathExists(dest_dir1_)); +#if defined(OS_CHROMEOS) + EXPECT_TRUE(file_util::PathExists(cache_dir_)); // old cache dir stays +#else + EXPECT_FALSE(file_util::PathExists(cache_dir_)); // old cache is gone +#endif + EXPECT_FALSE(file_util::PathExists(file1_)); + EXPECT_FALSE(file_util::PathExists(file2_)); + EXPECT_FALSE(file_util::PathExists(dir1_)); +} + +TEST_F(CacheUtilTest, DeleteCache) { + // DeleteCache won't delete subdirs, so let's not start with this + // one around. + file_util::Delete(dir1_, false); + disk_cache::DeleteCache(cache_dir_, false); + EXPECT_TRUE(file_util::PathExists(cache_dir_)); // cache dir stays + EXPECT_FALSE(file_util::PathExists(file1_)); + EXPECT_FALSE(file_util::PathExists(file2_)); +} + +TEST_F(CacheUtilTest, DeleteCacheAndDir) { + // DeleteCache won't delete subdirs, so let's not start with this + // one around. + file_util::Delete(dir1_, false); + disk_cache::DeleteCache(cache_dir_, true); + EXPECT_FALSE(file_util::PathExists(cache_dir_)); // cache dir is gone + EXPECT_FALSE(file_util::PathExists(file1_)); + EXPECT_FALSE(file_util::PathExists(file2_)); +} + +TEST_F(CacheUtilTest, DeleteCacheFile) { + EXPECT_TRUE(disk_cache::DeleteCacheFile(file1_)); + EXPECT_FALSE(file_util::PathExists(file1_)); + EXPECT_TRUE(file_util::PathExists(cache_dir_)); // cache dir stays +} + +} // namespace disk_cache |