diff options
author | nona@chromium.org <nona@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2013-06-10 12:13:23 +0000 |
---|---|---|
committer | nona@chromium.org <nona@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2013-06-10 12:13:23 +0000 |
commit | 393d5237d4be6b1cde17bfa15bb03c5cb9e25f23 (patch) | |
tree | 247b5b85f9e5cc1e3ab843010b04c3112d4ac5a5 | |
parent | ad2406e52c2500e391d8d027c7acefca92123689 (diff) | |
download | chromium_src-393d5237d4be6b1cde17bfa15bb03c5cb9e25f23.zip chromium_src-393d5237d4be6b1cde17bfa15bb03c5cb9e25f23.tar.gz chromium_src-393d5237d4be6b1cde17bfa15bb03c5cb9e25f23.tar.bz2 |
Revert 205192 "drive: Move cache file scan code to FileCache"
Compile failure.
../../chrome/browser/chromeos/drive/file_cache_unittest.cc:1254:3:error: 'message_loop_' was not declared in this scope
> drive: Move cache file scan code to FileCache
>
> BUG=248013
> TEST=unit_tests
>
> Review URL: https://chromiumcodereview.appspot.com/16721004
TBR=hashimoto@chromium.org
Review URL: https://codereview.chromium.org/16427006
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@205194 0039d316-1c4b-4281-b951-d872f2087c98
-rw-r--r-- | chrome/browser/chromeos/drive/file_cache.cc | 127 | ||||
-rw-r--r-- | chrome/browser/chromeos/drive/file_cache_metadata.cc | 136 | ||||
-rw-r--r-- | chrome/browser/chromeos/drive/file_cache_metadata.h | 9 | ||||
-rw-r--r-- | chrome/browser/chromeos/drive/file_cache_metadata_unittest.cc | 121 | ||||
-rw-r--r-- | chrome/browser/chromeos/drive/file_cache_unittest.cc | 48 |
5 files changed, 233 insertions, 208 deletions
diff --git a/chrome/browser/chromeos/drive/file_cache.cc b/chrome/browser/chromeos/drive/file_cache.cc index f026999..96d963c 100644 --- a/chrome/browser/chromeos/drive/file_cache.cc +++ b/chrome/browser/chromeos/drive/file_cache.cc @@ -27,10 +27,6 @@ namespace drive { namespace internal { namespace { -typedef std::map<std::string, FileCacheEntry> CacheMap; - -typedef std::map<std::string, base::FilePath> ResourceIdToFilePathMap; - const base::FilePath::CharType kFileCacheMetaDir[] = FILE_PATH_LITERAL("meta"); const base::FilePath::CharType kFileCachePersistentDir[] = FILE_PATH_LITERAL("persistent"); @@ -40,109 +36,6 @@ const base::FilePath::CharType kFileCacheTmpDownloadsDir[] = const base::FilePath::CharType kFileCacheTmpDocumentsDir[] = FILE_PATH_LITERAL("tmp/documents"); -// Scans cache subdirectory and build or update |cache_map| with found files. -// -// The resource IDs and file paths of discovered files are collected as a -// ResourceIdToFilePathMap, if these are processed properly. -void ScanCacheDirectory(const std::vector<base::FilePath>& cache_paths, - FileCache::CacheSubDirectoryType sub_dir_type, - CacheMap* cache_map, - ResourceIdToFilePathMap* processed_file_map) { - DCHECK(cache_map); - DCHECK(processed_file_map); - - base::FileEnumerator enumerator(cache_paths[sub_dir_type], - false, // not recursive - base::FileEnumerator::FILES, - util::kWildCard); - for (base::FilePath current = enumerator.Next(); !current.empty(); - current = enumerator.Next()) { - // Extract resource_id and md5 from filename. - std::string resource_id; - std::string md5; - std::string extra_extension; - util::ParseCacheFilePath(current, &resource_id, &md5, &extra_extension); - - // Determine cache state. - FileCacheEntry cache_entry; - cache_entry.set_md5(md5); - if (sub_dir_type == FileCache::CACHE_TYPE_PERSISTENT) - cache_entry.set_is_persistent(true); - - if (extra_extension == util::kMountedArchiveFileExtension) { - // Mounted archives in cache should be unmounted upon logout/shutdown. - // But if we encounter a mounted file at start, delete it and create an - // entry with not PRESENT state. - DCHECK(sub_dir_type == FileCache::CACHE_TYPE_PERSISTENT); - file_util::Delete(current, false); - } else { - // The cache file is present. - cache_entry.set_is_present(true); - - // Adds the dirty bit if |md5| indicates that the file is dirty, and - // the file is in the persistent directory. - if (md5 == util::kLocallyModifiedFileExtension) { - if (sub_dir_type == FileCache::CACHE_TYPE_PERSISTENT) { - cache_entry.set_is_dirty(true); - } else { - LOG(WARNING) << "Removing a dirty file in tmp directory: " - << current.value(); - file_util::Delete(current, false); - continue; - } - } - } - - // Create and insert new entry into cache map. - cache_map->insert(std::make_pair(resource_id, cache_entry)); - processed_file_map->insert(std::make_pair(resource_id, current)); - } -} - -void ScanCachePaths(const std::vector<base::FilePath>& cache_paths, - CacheMap* cache_map) { - DVLOG(1) << "Scanning directories"; - - // Scan cache persistent and tmp directories to enumerate all files and create - // corresponding entries for cache map. - ResourceIdToFilePathMap persistent_file_map; - ScanCacheDirectory(cache_paths, - FileCache::CACHE_TYPE_PERSISTENT, - cache_map, - &persistent_file_map); - ResourceIdToFilePathMap tmp_file_map; - ScanCacheDirectory(cache_paths, - FileCache::CACHE_TYPE_TMP, - cache_map, - &tmp_file_map); - - // On DB corruption, keep only dirty-and-committed files in persistent - // directory. Other files are deleted or moved to temporary directory. - for (ResourceIdToFilePathMap::const_iterator iter = - persistent_file_map.begin(); - iter != persistent_file_map.end(); ++iter) { - const std::string& resource_id = iter->first; - const base::FilePath& file_path = iter->second; - - CacheMap::iterator cache_map_iter = cache_map->find(resource_id); - if (cache_map_iter != cache_map->end()) { - FileCacheEntry* cache_entry = &cache_map_iter->second; - const bool is_dirty = cache_entry->is_dirty(); - if (!is_dirty) { - // If the file is not dirty, move to temporary directory. - base::FilePath new_file_path = - cache_paths[FileCache::CACHE_TYPE_TMP].Append( - file_path.BaseName()); - DLOG(WARNING) << "Moving: " << file_path.value() - << " to: " << new_file_path.value(); - file_util::Move(file_path, new_file_path); - cache_entry->set_is_persistent(false); - } - } - } - DVLOG(1) << "Directory scan finished"; -} - // Create cache directory paths and set permissions. bool InitCachePaths(const std::vector<base::FilePath>& cache_paths) { if (cache_paths.size() < FileCache::NUM_CACHE_TYPES) { @@ -914,25 +807,7 @@ bool FileCache::InitializeOnBlockingPool() { return false; metadata_.reset(new FileCacheMetadata(blocking_task_runner_)); - - switch (metadata_->Initialize(cache_paths_[CACHE_TYPE_META])) { - case FileCacheMetadata::INITIALIZE_FAILED: - return false; - - case FileCacheMetadata::INITIALIZE_OPENED: // Do nothing. - break; - - case FileCacheMetadata::INITIALIZE_CREATED: { - CacheMap cache_map; - ScanCachePaths(cache_paths_, &cache_map); - for (CacheMap::const_iterator it = cache_map.begin(); - it != cache_map.end(); ++it) { - metadata_->AddOrUpdateCacheEntry(it->first, it->second); - } - break; - } - } - return true; + return metadata_->Initialize(cache_paths_); } void FileCache::DestroyOnBlockingPool() { diff --git a/chrome/browser/chromeos/drive/file_cache_metadata.cc b/chrome/browser/chromeos/drive/file_cache_metadata.cc index 8817946..ab1ad88 100644 --- a/chrome/browser/chromeos/drive/file_cache_metadata.cc +++ b/chrome/browser/chromeos/drive/file_cache_metadata.cc @@ -11,6 +11,7 @@ #include "base/sequenced_task_runner.h" #include "base/threading/thread_restrictions.h" #include "chrome/browser/chromeos/drive/drive.pb.h" +#include "chrome/browser/chromeos/drive/file_cache.h" #include "chrome/browser/chromeos/drive/file_system_util.h" #include "third_party/leveldatabase/src/include/leveldb/db.h" @@ -19,6 +20,8 @@ namespace internal { namespace { +typedef std::map<std::string, FileCacheEntry> CacheMap; + enum DBOpenStatus { DB_OPEN_SUCCESS, DB_OPEN_FAILURE_CORRUPTION, @@ -27,6 +30,112 @@ enum DBOpenStatus { DB_OPEN_MAX_VALUE, }; +// A map table of resource ID to file path. +typedef std::map<std::string, base::FilePath> ResourceIdToFilePathMap; + +// Scans cache subdirectory and build or update |cache_map| with found files. +// +// The resource IDs and file paths of discovered files are collected as a +// ResourceIdToFilePathMap, if these are processed properly. +void ScanCacheDirectory(const std::vector<base::FilePath>& cache_paths, + FileCache::CacheSubDirectoryType sub_dir_type, + CacheMap* cache_map, + ResourceIdToFilePathMap* processed_file_map) { + DCHECK(cache_map); + DCHECK(processed_file_map); + + base::FileEnumerator enumerator(cache_paths[sub_dir_type], + false, // not recursive + base::FileEnumerator::FILES, + util::kWildCard); + for (base::FilePath current = enumerator.Next(); !current.empty(); + current = enumerator.Next()) { + // Extract resource_id and md5 from filename. + std::string resource_id; + std::string md5; + std::string extra_extension; + util::ParseCacheFilePath(current, &resource_id, &md5, &extra_extension); + + // Determine cache state. + FileCacheEntry cache_entry; + cache_entry.set_md5(md5); + if (sub_dir_type == FileCache::CACHE_TYPE_PERSISTENT) + cache_entry.set_is_persistent(true); + + if (extra_extension == util::kMountedArchiveFileExtension) { + // Mounted archives in cache should be unmounted upon logout/shutdown. + // But if we encounter a mounted file at start, delete it and create an + // entry with not PRESENT state. + DCHECK(sub_dir_type == FileCache::CACHE_TYPE_PERSISTENT); + file_util::Delete(current, false); + } else { + // The cache file is present. + cache_entry.set_is_present(true); + + // Adds the dirty bit if |md5| indicates that the file is dirty, and + // the file is in the persistent directory. + if (md5 == util::kLocallyModifiedFileExtension) { + if (sub_dir_type == FileCache::CACHE_TYPE_PERSISTENT) { + cache_entry.set_is_dirty(true); + } else { + LOG(WARNING) << "Removing a dirty file in tmp directory: " + << current.value(); + file_util::Delete(current, false); + continue; + } + } + } + + // Create and insert new entry into cache map. + cache_map->insert(std::make_pair(resource_id, cache_entry)); + processed_file_map->insert(std::make_pair(resource_id, current)); + } +} + +void ScanCachePaths(const std::vector<base::FilePath>& cache_paths, + CacheMap* cache_map) { + DVLOG(1) << "Scanning directories"; + + // Scan cache persistent and tmp directories to enumerate all files and create + // corresponding entries for cache map. + ResourceIdToFilePathMap persistent_file_map; + ScanCacheDirectory(cache_paths, + FileCache::CACHE_TYPE_PERSISTENT, + cache_map, + &persistent_file_map); + ResourceIdToFilePathMap tmp_file_map; + ScanCacheDirectory(cache_paths, + FileCache::CACHE_TYPE_TMP, + cache_map, + &tmp_file_map); + + // On DB corruption, keep only dirty-and-committed files in persistent + // directory. Other files are deleted or moved to temporary directory. + for (ResourceIdToFilePathMap::const_iterator iter = + persistent_file_map.begin(); + iter != persistent_file_map.end(); ++iter) { + const std::string& resource_id = iter->first; + const base::FilePath& file_path = iter->second; + + CacheMap::iterator cache_map_iter = cache_map->find(resource_id); + if (cache_map_iter != cache_map->end()) { + FileCacheEntry* cache_entry = &cache_map_iter->second; + const bool is_dirty = cache_entry->is_dirty(); + if (!is_dirty) { + // If the file is not dirty, move to temporary directory. + base::FilePath new_file_path = + cache_paths[FileCache::CACHE_TYPE_TMP].Append( + file_path.BaseName()); + DLOG(WARNING) << "Moving: " << file_path.value() + << " to: " << new_file_path.value(); + file_util::Move(file_path, new_file_path); + cache_entry->set_is_persistent(false); + } + } + } + DVLOG(1) << "Directory scan finished"; +} + // Returns true if |md5| matches the one in |cache_entry| with some // exceptions. See the function definition for details. bool CheckIfMd5Matches(const std::string& md5, @@ -119,14 +228,15 @@ FileCacheMetadata::~FileCacheMetadata() { AssertOnSequencedWorkerPool(); } -FileCacheMetadata::InitializeResult FileCacheMetadata::Initialize( - const base::FilePath& db_directory_path) { +bool FileCacheMetadata::Initialize( + const std::vector<base::FilePath>& cache_paths) { AssertOnSequencedWorkerPool(); - const base::FilePath db_path = db_directory_path.Append(kCacheMetadataDBPath); + const base::FilePath db_path = + cache_paths[FileCache::CACHE_TYPE_META].Append(kCacheMetadataDBPath); DVLOG(1) << "db path=" << db_path.value(); - bool created = !file_util::PathExists(db_path); + bool scan_cache = !file_util::PathExists(db_path); leveldb::DB* level_db = NULL; leveldb::Options options; @@ -149,17 +259,29 @@ FileCacheMetadata::InitializeResult FileCacheMetadata::Initialize( UMA_HISTOGRAM_ENUMERATION("Drive.CacheDBOpenStatus", DB_OPEN_FAILURE_UNRECOVERABLE, DB_OPEN_MAX_VALUE); - return INITIALIZE_FAILED; + // Failed to open the cache metadata DB. Drive will be disabled. + return false; } - created = true; + scan_cache = true; } UMA_HISTOGRAM_ENUMERATION("Drive.CacheDBOpenStatus", uma_status, DB_OPEN_MAX_VALUE); DCHECK(level_db); level_db_.reset(level_db); - return created ? INITIALIZE_CREATED : INITIALIZE_OPENED; + // We scan the cache directories to initialize the cache database if we + // were previously using the cache map. + if (scan_cache) { + CacheMap cache_map; + ScanCachePaths(cache_paths, &cache_map); + for (CacheMap::const_iterator it = cache_map.begin(); + it != cache_map.end(); ++it) { + AddOrUpdateCacheEntry(it->first, it->second); + } + } + + return true; } void FileCacheMetadata::AddOrUpdateCacheEntry( diff --git a/chrome/browser/chromeos/drive/file_cache_metadata.h b/chrome/browser/chromeos/drive/file_cache_metadata.h index c75ebf3..c2c4f1e 100644 --- a/chrome/browser/chromeos/drive/file_cache_metadata.h +++ b/chrome/browser/chromeos/drive/file_cache_metadata.h @@ -33,13 +33,6 @@ class FileCacheMetadata { // Database path. static const base::FilePath::CharType* kCacheMetadataDBPath; - // Result of Initialize(). - enum InitializeResult { - INITIALIZE_FAILED, // Could not open nor create DB. - INITIALIZE_OPENED, // Opened an existing DB. - INITIALIZE_CREATED, // Created a new DB. - }; - // Object to iterate over entries stored in the cache metadata. class Iterator { public: @@ -79,7 +72,7 @@ class FileCacheMetadata { ~FileCacheMetadata(); // Initialize the cache metadata store. Returns true on success. - InitializeResult Initialize(const base::FilePath& db_directory_path); + bool Initialize(const std::vector<base::FilePath>& cache_paths); // Adds a new cache entry corresponding to |resource_id| if it doesn't // exist, otherwise update the existing entry. void AddOrUpdateCacheEntry(const std::string& resource_id, diff --git a/chrome/browser/chromeos/drive/file_cache_metadata_unittest.cc b/chrome/browser/chromeos/drive/file_cache_metadata_unittest.cc index f74e256..cfae77f 100644 --- a/chrome/browser/chromeos/drive/file_cache_metadata_unittest.cc +++ b/chrome/browser/chromeos/drive/file_cache_metadata_unittest.cc @@ -7,6 +7,7 @@ #include "base/file_util.h" #include "base/files/scoped_temp_dir.h" #include "chrome/browser/chromeos/drive/drive.pb.h" +#include "chrome/browser/chromeos/drive/file_cache.h" #include "chrome/browser/chromeos/drive/file_system_util.h" #include "chrome/browser/chromeos/drive/test_util.h" #include "chrome/browser/google_apis/test_util.h" @@ -22,23 +23,57 @@ class FileCacheMetadataTest : public testing::Test { virtual void SetUp() OVERRIDE { // Create cache directories. ASSERT_TRUE(temp_dir_.CreateUniqueTempDir()); - metadata_.reset(new FileCacheMetadata(NULL)); + cache_paths_ = FileCache::GetCachePaths(temp_dir_.path()); + ASSERT_TRUE(FileCache::CreateCacheDirectories(cache_paths_)); + + persistent_directory_ = cache_paths_[FileCache::CACHE_TYPE_PERSISTENT]; + tmp_directory_ = cache_paths_[FileCache::CACHE_TYPE_TMP]; } virtual void TearDown() OVERRIDE { metadata_.reset(); } + // Sets up the FileCacheMetadata object. + void SetUpCacheMetadata() { + metadata_.reset(new FileCacheMetadata(NULL)); + ASSERT_TRUE(metadata_->Initialize(cache_paths_)); + } + + // Sets up the cache directories with various files. + // Should be called before SetUpCacheMetadata(). + void SetUpCacheWithVariousFiles() { + // Create some files in persistent directory. + // + CreateFile(persistent_directory_.AppendASCII("id_foo.md5foo")); + CreateFile(persistent_directory_.AppendASCII("id_bar.local")); + + // Create some files in tmp directory. + // + CreateFile(tmp_directory_.AppendASCII("id_qux.md5qux")); + // "id_quux" is invalid as we shouldn't have a dirty file in "tmp". + CreateFile(tmp_directory_.AppendASCII("id_quux.local")); + } + + // Create a file at |file_path|. + void CreateFile(const base::FilePath& file_path) { + const std::string kFoo = "foo"; + ASSERT_TRUE(google_apis::test_util::WriteStringToFile(file_path, kFoo)) + << ": " << file_path.value(); + } + protected: base::ScopedTempDir temp_dir_; scoped_ptr<FileCacheMetadata> metadata_; + std::vector<base::FilePath> cache_paths_; + base::FilePath persistent_directory_; + base::FilePath tmp_directory_; }; // Test all the methods of FileCacheMetadata except for // RemoveTemporaryFiles. TEST_F(FileCacheMetadataTest, CacheTest) { - ASSERT_EQ(FileCacheMetadata::INITIALIZE_CREATED, - metadata_->Initialize(temp_dir_.path())); + SetUpCacheMetadata(); // Save an initial entry. std::string test_resource_id("test_resource_id"); @@ -139,26 +174,74 @@ TEST_F(FileCacheMetadataTest, CacheTest) { EXPECT_TRUE(cache_entry.is_present()); } -TEST_F(FileCacheMetadataTest, Initialize) { +TEST_F(FileCacheMetadataTest, CorruptDB) { + using file_util::PathExists; + SetUpCacheWithVariousFiles(); + const base::FilePath db_path = - temp_dir_.path().Append(FileCacheMetadata::kCacheMetadataDBPath); + cache_paths_[FileCache::CACHE_TYPE_META].Append( + FileCacheMetadata::kCacheMetadataDBPath); - // Try to open a bogus file. + // Write a bogus file. ASSERT_TRUE( google_apis::test_util::WriteStringToFile(db_path, "Hello world")); - ASSERT_EQ(FileCacheMetadata::INITIALIZE_CREATED, - metadata_->Initialize(temp_dir_.path())); - - // Open an existing DB. - metadata_.reset(new FileCacheMetadata(NULL)); - EXPECT_EQ(FileCacheMetadata::INITIALIZE_OPENED, - metadata_->Initialize(temp_dir_.path())); - - // Try to open a nonexistent path. - base::FilePath non_existent_path(FILE_PATH_LITERAL("/somewhere/nonexistent")); - metadata_.reset(new FileCacheMetadata(NULL)); - EXPECT_EQ(FileCacheMetadata::INITIALIZE_FAILED, - metadata_->Initialize(non_existent_path)); + + // Some files are removed during cache initialization. Make sure these + // exist beforehand. + EXPECT_TRUE(PathExists(tmp_directory_.AppendASCII("id_quux.local"))); + + SetUpCacheMetadata(); + + // Check contents in "persistent" directory. + // + // "id_foo" is moved to temporary directory. + FileCacheEntry cache_entry; + ASSERT_TRUE(metadata_->GetCacheEntry("id_foo", "md5foo", &cache_entry)); + EXPECT_EQ("md5foo", cache_entry.md5()); + EXPECT_FALSE(cache_entry.is_persistent()); + EXPECT_TRUE(test_util::CacheStatesEqual( + test_util::ToCacheEntry(test_util::TEST_CACHE_STATE_PRESENT), + cache_entry)); + EXPECT_TRUE(PathExists(tmp_directory_.AppendASCII("id_foo.md5foo"))); + + // "id_bar" is present and dirty. + ASSERT_TRUE(metadata_->GetCacheEntry("id_bar", "", &cache_entry)); + EXPECT_EQ("local", cache_entry.md5()); + EXPECT_EQ(FileCache::CACHE_TYPE_PERSISTENT, + FileCache::GetSubDirectoryType(cache_entry)); + EXPECT_TRUE(test_util::CacheStatesEqual( + test_util::ToCacheEntry(test_util::TEST_CACHE_STATE_PRESENT | + test_util::TEST_CACHE_STATE_DIRTY | + test_util::TEST_CACHE_STATE_PERSISTENT), + cache_entry)); + EXPECT_TRUE(PathExists(persistent_directory_.AppendASCII("id_bar.local"))); + + // Check contents in "tmp" directory. + // + // "id_qux" is just present in tmp directory. + ASSERT_TRUE(metadata_->GetCacheEntry("id_qux", "md5qux", &cache_entry)); + EXPECT_EQ("md5qux", cache_entry.md5()); + EXPECT_EQ(FileCache::CACHE_TYPE_TMP, + FileCache::GetSubDirectoryType(cache_entry)); + EXPECT_TRUE(test_util::CacheStatesEqual( + test_util::ToCacheEntry(test_util::TEST_CACHE_STATE_PRESENT), + cache_entry)); + EXPECT_TRUE(PathExists(tmp_directory_.AppendASCII("id_qux.md5qux"))); + + // "id_quux" should be removed during cache initialization. + EXPECT_FALSE(metadata_->GetCacheEntry("id_quux", "md5qux", &cache_entry)); +} + +// Don't use TEST_F, as we don't want SetUp() and TearDown() for this test. +TEST(FileCacheMetadataExtraTest, CannotOpenDB) { + // Create nonexistent cache paths, so the initialization fails due to the + // failure of opening the DB. + std::vector<base::FilePath> cache_paths = + FileCache::GetCachePaths( + base::FilePath::FromUTF8Unsafe("/somewhere/nonexistent")); + + scoped_ptr<FileCacheMetadata> metadata(new FileCacheMetadata(NULL)); + EXPECT_FALSE(metadata->Initialize(cache_paths)); } } // namespace internal diff --git a/chrome/browser/chromeos/drive/file_cache_unittest.cc b/chrome/browser/chromeos/drive/file_cache_unittest.cc index 188a504..ad21ac7 100644 --- a/chrome/browser/chromeos/drive/file_cache_unittest.cc +++ b/chrome/browser/chromeos/drive/file_cache_unittest.cc @@ -1223,54 +1223,6 @@ class FileCacheTest : public testing::Test { scoped_ptr<FakeFreeDiskSpaceGetter> fake_free_disk_space_getter_; }; -TEST_F(FileCacheTest, ScanCacheFile) { - // Set up files in cache directories. - const base::FilePath persistent_directory = - cache_->GetCacheDirectoryPath(FileCache::CACHE_TYPE_PERSISTENT); - ASSERT_TRUE(google_apis::test_util::WriteStringToFile( - persistent_directory.AppendASCII("id_foo.md5foo"), "foo")); - ASSERT_TRUE(google_apis::test_util::WriteStringToFile( - persistent_directory.AppendASCII("id_bar.local"), "bar")); - - const base::FilePath tmp_directory = - cache_->GetCacheDirectoryPath(FileCache::CACHE_TYPE_TMP); - ASSERT_TRUE(google_apis::test_util::WriteStringToFile( - tmp_directory.AppendASCII("id_qux.md5qux"), "qux")); - ASSERT_TRUE(google_apis::test_util::WriteStringToFile( - tmp_directory.AppendASCII("id_quux.local"), "quux")); - - // Remove the existing DB. - ASSERT_TRUE(file_util::Delete( - cache_->GetCacheDirectoryPath(FileCache::CACHE_TYPE_META), - true /* recursive */)); - - // Create a new cache and initialize it. - cache_.reset(new FileCache(temp_dir_.path(), - base::MessageLoopProxy::current(), - fake_free_disk_space_getter_.get())); - bool success = false; - cache_->RequestInitialize( - google_apis::test_util::CreateCopyResultCallback(&success)); - message_loop_.RunUntilIdle(); - ASSERT_TRUE(success); - - // Check contents of the cache. - FileCacheEntry cache_entry; - EXPECT_TRUE(cache_->GetCacheEntry("id_foo", std::string(), &cache_entry)); - EXPECT_TRUE(cache_entry.is_present()); - EXPECT_EQ("md5foo", cache_entry.md5()); - - EXPECT_TRUE(cache_->GetCacheEntry("id_bar", std::string(), &cache_entry)); - EXPECT_TRUE(cache_entry.is_present()); - EXPECT_TRUE(cache_entry.is_dirty()); - - EXPECT_TRUE(cache_->GetCacheEntry("id_qux", std::string(), &cache_entry)); - EXPECT_EQ("md5qux", cache_entry.md5()); - - EXPECT_FALSE(cache_->GetCacheEntry("id_quux", std::string(), &cache_entry)); - -} - TEST_F(FileCacheTest, FreeDiskSpaceIfNeededFor) { base::FilePath src_file; ASSERT_TRUE(file_util::CreateTemporaryFileInDir(temp_dir_.path(), &src_file)); |