Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(7053)

Unified Diff: chrome/browser/sync_file_system/drive/metadata_db_migration_util.cc

Issue 15951007: SyncFS: Factor out DriveMetadataDB migration functions (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Created 7 years, 7 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
Index: chrome/browser/sync_file_system/drive/metadata_db_migration_util.cc
diff --git a/chrome/browser/sync_file_system/drive/metadata_db_migration_util.cc b/chrome/browser/sync_file_system/drive/metadata_db_migration_util.cc
index ae395cb15a73a749c278bc3f12c3cc1613ed57d4..3312a2f6767bcdae5a9d5b4ce5d2596ad3eb4f70 100644
--- a/chrome/browser/sync_file_system/drive/metadata_db_migration_util.cc
+++ b/chrome/browser/sync_file_system/drive/metadata_db_migration_util.cc
@@ -9,6 +9,8 @@
#include "base/string_util.h"
#include "googleurl/src/gurl.h"
#include "third_party/leveldatabase/src/include/leveldb/write_batch.h"
+#include "webkit/browser/fileapi/file_system_url.h"
+#include "webkit/common/fileapi/file_system_types.h"
namespace sync_file_system {
namespace drive {
@@ -24,6 +26,31 @@ std::string RemovePrefix(const std::string& str, const std::string& prefix) {
return str;
}
+const base::FilePath::CharType kV0FormatPathPrefix[] =
+ FILE_PATH_LITERAL("drive/");
+
+bool ParseV0FormatFileSystemURLString(const GURL& url,
calvinlo 2013/05/29 08:36:52 nit: possible to add a test for this too? It'll a
nhiroki 2013/05/30 06:15:48 Done.
+ GURL* origin,
+ base::FilePath* path) {
+ fileapi::FileSystemType mount_type;
+ base::FilePath virtual_path;
+
+ if (!fileapi::FileSystemURL::ParseFileSystemSchemeURL(
+ url, origin, &mount_type, &virtual_path) ||
+ mount_type != fileapi::kFileSystemTypeExternal) {
+ NOTREACHED() << "Failed to parse filesystem scheme URL";
+ return false;
+ }
+
+ base::FilePath::StringType prefix =
+ base::FilePath(kV0FormatPathPrefix).NormalizePathSeparators().value();
+ if (virtual_path.value().substr(0, prefix.size()) != prefix)
+ return false;
+
+ *path = base::FilePath(virtual_path.value().substr(prefix.size()));
+ return true;
+}
+
} // namespace
std::string AddWapiFilePrefix(const std::string& resource_id) {
@@ -68,6 +95,76 @@ std::string RemoveWapiIdPrefix(const std::string& resource_id) {
return resource_id;
}
+SyncStatusCode MigrateDatabaseFromV0ToV1(leveldb::DB* db) {
+ // Version 0 database format:
+ // key: "CHANGE_STAMP"
+ // value: <Largest Changestamp>
+ //
+ // key: "SYNC_ROOT_DIR"
+ // value: <Resource ID of the sync root directory>
+ //
+ // key: "METADATA: " +
+ // <FileSystemURL serialized by SerializeSyncableFileSystemURL>
+ // value: <Serialized DriveMetadata>
+ //
+ // key: "BSYNC_ORIGIN: " + <URL string of a batch sync origin>
+ // value: <Resource ID of the drive directory for the origin>
+ //
+ // key: "ISYNC_ORIGIN: " + <URL string of a incremental sync origin>
+ // value: <Resource ID of the drive directory for the origin>
+ //
+ // Version 1 database format (changed keys/fields are marked with '*'):
+ // * key: "VERSION" (new)
+ // * value: 1
+ //
+ // key: "CHANGE_STAMP"
+ // value: <Largest Changestamp>
+ //
+ // key: "SYNC_ROOT_DIR"
+ // value: <Resource ID of the sync root directory>
+ //
+ // * key: "METADATA: " + <Origin and URL> (changed)
+ // * value: <Serialized DriveMetadata>
+ //
+ // key: "BSYNC_ORIGIN: " + <URL string of a batch sync origin>
+ // value: <Resource ID of the drive directory for the origin>
+ //
+ // key: "ISYNC_ORIGIN: " + <URL string of a incremental sync origin>
+ // value: <Resource ID of the drive directory for the origin>
+ //
+ // key: "DISABLED_ORIGIN: " + <URL string of a disabled origin>
+ // value: <Resource ID of the drive directory for the origin>
+
+ const char kDatabaseVersionKey[] = "VERSION";
+ const char kDriveMetadataKeyPrefix[] = "METADATA: ";
+ const char kMetadataKeySeparator = ' ';
+
+ leveldb::WriteBatch write_batch;
+ write_batch.Put(kDatabaseVersionKey, "1");
+
+ scoped_ptr<leveldb::Iterator> itr(db->NewIterator(leveldb::ReadOptions()));
+ for (itr->Seek(kDriveMetadataKeyPrefix); itr->Valid(); itr->Next()) {
+ std::string key = itr->key().ToString();
+ if (!StartsWithASCII(key, kDriveMetadataKeyPrefix, true))
+ break;
+ std::string serialized_url(RemovePrefix(key, kDriveMetadataKeyPrefix));
+
+ GURL origin;
+ base::FilePath path;
+ bool success = ParseV0FormatFileSystemURLString(
+ GURL(serialized_url), &origin, &path);
+ DCHECK(success) << serialized_url;
+ std::string new_key = kDriveMetadataKeyPrefix + origin.spec() +
+ kMetadataKeySeparator + path.AsUTF8Unsafe();
+
+ write_batch.Put(new_key, itr->value());
+ write_batch.Delete(key);
+ }
+
+ return LevelDBStatusToSyncStatusCode(
+ db->Write(leveldb::WriteOptions(), &write_batch));
+}
+
SyncStatusCode MigrateDatabaseFromV1ToV2(leveldb::DB* db) {
// Strips prefix of WAPI resource ID.
// (i.e. "file:xxxx" => "xxxx", "folder:yyyy" => "yyyy")

Powered by Google App Engine
This is Rietveld 408576698