summaryrefslogtreecommitdiffstats
path: root/chrome/browser/chromeos/gdata
diff options
context:
space:
mode:
authorachuith@chromium.org <achuith@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98>2012-09-19 21:34:43 +0000
committerachuith@chromium.org <achuith@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98>2012-09-19 21:34:43 +0000
commitd46a4cdb31cea518f6878e8451129cdf810d5686 (patch)
tree05a2be4290c5cc6d4d7f894975d910cc8c75d4b5 /chrome/browser/chromeos/gdata
parent1cd0a5072ea5fc94a08c50b90189f1e3b3dff006 (diff)
downloadchromium_src-d46a4cdb31cea518f6878e8451129cdf810d5686.zip
chromium_src-d46a4cdb31cea518f6878e8451129cdf810d5686.tar.gz
chromium_src-d46a4cdb31cea518f6878e8451129cdf810d5686.tar.bz2
Support for recovering from corrupt cache database.
BUG=137545,148752 TEST=unit tests. Review URL: https://codereview.chromium.org/10941027 git-svn-id: svn://svn.chromium.org/chrome/trunk/src@157611 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'chrome/browser/chromeos/gdata')
-rw-r--r--chrome/browser/chromeos/gdata/drive_cache_metadata.cc31
-rw-r--r--chrome/browser/chromeos/gdata/drive_cache_metadata.h3
-rw-r--r--chrome/browser/chromeos/gdata/drive_cache_metadata_unittest.cc22
3 files changed, 44 insertions, 12 deletions
diff --git a/chrome/browser/chromeos/gdata/drive_cache_metadata.cc b/chrome/browser/chromeos/gdata/drive_cache_metadata.cc
index f5ce959..28527df 100644
--- a/chrome/browser/chromeos/gdata/drive_cache_metadata.cc
+++ b/chrome/browser/chromeos/gdata/drive_cache_metadata.cc
@@ -19,9 +19,6 @@ namespace {
// A map table of resource ID to file path.
typedef std::map<std::string, FilePath> ResourceIdToFilePathMap;
-const FilePath::CharType kDriveCacheMetadataDBPath[] =
- FILE_PATH_LITERAL("cache_metadata.db");
-
// Returns true if |file_path| is a valid symbolic link as |sub_dir_type|.
// Otherwise, returns false with the reason.
bool IsValidSymbolicLink(const FilePath& file_path,
@@ -459,25 +456,32 @@ void DriveCacheMetadataDB::Initialize(
kDriveCacheMetadataDBPath);
DVLOG(1) << "db path=" << db_path.value();
- const bool db_exists = file_util::PathExists(db_path);
+ bool scan_cache = !file_util::PathExists(db_path);
leveldb::DB* level_db = NULL;
leveldb::Options options;
options.create_if_missing = true;
leveldb::Status db_status = leveldb::DB::Open(options, db_path.value(),
&level_db);
+
+ // Delete the db and scan the physical cache. This will fix a corrupt db, but
+ // perhaps not other causes of failed DB::Open.
+ if (!db_status.ok()) {
+ DVLOG(1) << "Detected corrupt db";
+ const bool deleted = file_util::Delete(db_path, true);
+ DCHECK(deleted);
+ db_status = leveldb::DB::Open(options, db_path.value(), &level_db);
+ // TODO(satorux): Handle the situation where DB::Open fails because of lack
+ // of disk space, permissions, or other causes. crbug.com/150840.
+ CHECK(db_status.ok()); // Must succeed or we'll crash later.
+ scan_cache = true;
+ }
DCHECK(level_db);
- // TODO(achuith,hashimoto,satorux): If db cannot be opened, we should try to
- // recover it. If that fails, we should just delete it and either rescan or
- // refetch the feed. crbug.com/137545.
- DCHECK(db_status.ok());
level_db_.reset(level_db);
// We scan the cache directories to initialize the cache database if we
// were previously using the cache map.
- // TODO(achuith,hashimoto,satorux): Delete ScanCachePaths in M23.
- // crbug.com/137542
- if (!db_exists) {
+ if (scan_cache) {
CacheMap cache_map;
ScanCachePaths(cache_paths, &cache_map);
InsertMapIntoDB(cache_map);
@@ -579,6 +583,11 @@ void DriveCacheMetadataDB::ForceRescanForTesting(
} // namespace
+// static
+const FilePath::CharType* DriveCacheMetadata::kDriveCacheMetadataDBPath =
+ FILE_PATH_LITERAL("cache_metadata.db");
+
+
DriveCacheMetadata::DriveCacheMetadata(
base::SequencedTaskRunner* blocking_task_runner)
: blocking_task_runner_(blocking_task_runner) {
diff --git a/chrome/browser/chromeos/gdata/drive_cache_metadata.h b/chrome/browser/chromeos/gdata/drive_cache_metadata.h
index 47d54a6..66e2e55 100644
--- a/chrome/browser/chromeos/gdata/drive_cache_metadata.h
+++ b/chrome/browser/chromeos/gdata/drive_cache_metadata.h
@@ -28,6 +28,9 @@ class DriveCacheMetadata {
// A map table of cache file's resource id to its CacheEntry* entry.
typedef std::map<std::string, DriveCacheEntry> CacheMap;
+ // Database path.
+ static const FilePath::CharType* kDriveCacheMetadataDBPath;
+
virtual ~DriveCacheMetadata();
// Creates DriveCacheMetadata instance.
diff --git a/chrome/browser/chromeos/gdata/drive_cache_metadata_unittest.cc b/chrome/browser/chromeos/gdata/drive_cache_metadata_unittest.cc
index bfcffb5..cefe04c 100644
--- a/chrome/browser/chromeos/gdata/drive_cache_metadata_unittest.cc
+++ b/chrome/browser/chromeos/gdata/drive_cache_metadata_unittest.cc
@@ -347,7 +347,7 @@ TEST_F(DriveCacheMetadataTest, Initialization) {
}
// Test DriveCacheMetadata::RemoveTemporaryFiles.
-TEST_F(DriveCacheMetadataTest, RemoveTemporaryFilesTest) {
+TEST_F(DriveCacheMetadataTest, RemoveTemporaryFiles) {
SetUpCacheMetadata();
DriveCacheMetadata::CacheMap cache_map;
@@ -388,4 +388,24 @@ TEST_F(DriveCacheMetadataTest, RemoveTemporaryFilesTest) {
EXPECT_FALSE(metadata_->GetCacheEntry("<resource_id_4>", "", &cache_entry));
}
+TEST_F(DriveCacheMetadataTest, CorruptDB) {
+ SetUpCacheWithVariousFiles();
+
+ const FilePath db_path = cache_paths_[DriveCache::CACHE_TYPE_META].Append(
+ DriveCacheMetadata::kDriveCacheMetadataDBPath);
+
+ // Write a bogus file.
+ std::string text("Hello world");
+ file_util::WriteFile(db_path, text.c_str(), text.length());
+
+ SetUpCacheMetadata();
+
+ // "id_foo" is present and pinned.
+ DriveCacheEntry cache_entry;
+ ASSERT_TRUE(metadata_->GetCacheEntry("id_foo", "md5foo", &cache_entry));
+ EXPECT_EQ("md5foo", cache_entry.md5());
+ EXPECT_EQ(DriveCache::CACHE_TYPE_PERSISTENT,
+ DriveCache::GetSubDirectoryType(cache_entry));
+}
+
} // namespace gdata