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

Side by Side 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, 6 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 unified diff | Download patch | Annotate | Revision Log
OLDNEW
1 // Copyright 2013 The Chromium Authors. All rights reserved. 1 // Copyright 2013 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "chrome/browser/sync_file_system/drive/metadata_db_migration_util.h" 5 #include "chrome/browser/sync_file_system/drive/metadata_db_migration_util.h"
6 6
7 #include "base/files/file_path.h" 7 #include "base/files/file_path.h"
8 #include "base/memory/scoped_ptr.h" 8 #include "base/memory/scoped_ptr.h"
9 #include "base/string_util.h" 9 #include "base/string_util.h"
10 #include "googleurl/src/gurl.h" 10 #include "googleurl/src/gurl.h"
11 #include "third_party/leveldatabase/src/include/leveldb/write_batch.h" 11 #include "third_party/leveldatabase/src/include/leveldb/write_batch.h"
12 #include "webkit/browser/fileapi/file_system_url.h"
13 #include "webkit/common/fileapi/file_system_types.h"
12 14
13 namespace sync_file_system { 15 namespace sync_file_system {
14 namespace drive { 16 namespace drive {
15 17
16 namespace { 18 namespace {
17 19
18 const char kWapiFileIdPrefix[] = "file:"; 20 const char kWapiFileIdPrefix[] = "file:";
19 const char kWapiFolderIdPrefix[] = "folder:"; 21 const char kWapiFolderIdPrefix[] = "folder:";
20 22
21 std::string RemovePrefix(const std::string& str, const std::string& prefix) { 23 std::string RemovePrefix(const std::string& str, const std::string& prefix) {
22 if (StartsWithASCII(str, prefix, true)) 24 if (StartsWithASCII(str, prefix, true))
23 return std::string(str.begin() + prefix.size(), str.end()); 25 return std::string(str.begin() + prefix.size(), str.end());
24 return str; 26 return str;
25 } 27 }
26 28
29 const base::FilePath::CharType kV0FormatPathPrefix[] =
30 FILE_PATH_LITERAL("drive/");
31
32 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.
33 GURL* origin,
34 base::FilePath* path) {
35 fileapi::FileSystemType mount_type;
36 base::FilePath virtual_path;
37
38 if (!fileapi::FileSystemURL::ParseFileSystemSchemeURL(
39 url, origin, &mount_type, &virtual_path) ||
40 mount_type != fileapi::kFileSystemTypeExternal) {
41 NOTREACHED() << "Failed to parse filesystem scheme URL";
42 return false;
43 }
44
45 base::FilePath::StringType prefix =
46 base::FilePath(kV0FormatPathPrefix).NormalizePathSeparators().value();
47 if (virtual_path.value().substr(0, prefix.size()) != prefix)
48 return false;
49
50 *path = base::FilePath(virtual_path.value().substr(prefix.size()));
51 return true;
52 }
53
27 } // namespace 54 } // namespace
28 55
29 std::string AddWapiFilePrefix(const std::string& resource_id) { 56 std::string AddWapiFilePrefix(const std::string& resource_id) {
30 DCHECK(!StartsWithASCII(resource_id, kWapiFileIdPrefix, true)); 57 DCHECK(!StartsWithASCII(resource_id, kWapiFileIdPrefix, true));
31 DCHECK(!StartsWithASCII(resource_id, kWapiFolderIdPrefix, true)); 58 DCHECK(!StartsWithASCII(resource_id, kWapiFolderIdPrefix, true));
32 59
33 if (resource_id.empty() || 60 if (resource_id.empty() ||
34 StartsWithASCII(resource_id, kWapiFileIdPrefix, true) || 61 StartsWithASCII(resource_id, kWapiFileIdPrefix, true) ||
35 StartsWithASCII(resource_id, kWapiFolderIdPrefix, true)) 62 StartsWithASCII(resource_id, kWapiFolderIdPrefix, true))
36 return resource_id; 63 return resource_id;
(...skipping 24 matching lines...) Expand all
61 } 88 }
62 89
63 std::string RemoveWapiIdPrefix(const std::string& resource_id) { 90 std::string RemoveWapiIdPrefix(const std::string& resource_id) {
64 if (StartsWithASCII(resource_id, kWapiFileIdPrefix, true)) 91 if (StartsWithASCII(resource_id, kWapiFileIdPrefix, true))
65 return RemovePrefix(resource_id, kWapiFileIdPrefix); 92 return RemovePrefix(resource_id, kWapiFileIdPrefix);
66 if (StartsWithASCII(resource_id, kWapiFolderIdPrefix, true)) 93 if (StartsWithASCII(resource_id, kWapiFolderIdPrefix, true))
67 return RemovePrefix(resource_id, kWapiFolderIdPrefix); 94 return RemovePrefix(resource_id, kWapiFolderIdPrefix);
68 return resource_id; 95 return resource_id;
69 } 96 }
70 97
98 SyncStatusCode MigrateDatabaseFromV0ToV1(leveldb::DB* db) {
99 // Version 0 database format:
100 // key: "CHANGE_STAMP"
101 // value: <Largest Changestamp>
102 //
103 // key: "SYNC_ROOT_DIR"
104 // value: <Resource ID of the sync root directory>
105 //
106 // key: "METADATA: " +
107 // <FileSystemURL serialized by SerializeSyncableFileSystemURL>
108 // value: <Serialized DriveMetadata>
109 //
110 // key: "BSYNC_ORIGIN: " + <URL string of a batch sync origin>
111 // value: <Resource ID of the drive directory for the origin>
112 //
113 // key: "ISYNC_ORIGIN: " + <URL string of a incremental sync origin>
114 // value: <Resource ID of the drive directory for the origin>
115 //
116 // Version 1 database format (changed keys/fields are marked with '*'):
117 // * key: "VERSION" (new)
118 // * value: 1
119 //
120 // key: "CHANGE_STAMP"
121 // value: <Largest Changestamp>
122 //
123 // key: "SYNC_ROOT_DIR"
124 // value: <Resource ID of the sync root directory>
125 //
126 // * key: "METADATA: " + <Origin and URL> (changed)
127 // * value: <Serialized DriveMetadata>
128 //
129 // key: "BSYNC_ORIGIN: " + <URL string of a batch sync origin>
130 // value: <Resource ID of the drive directory for the origin>
131 //
132 // key: "ISYNC_ORIGIN: " + <URL string of a incremental sync origin>
133 // value: <Resource ID of the drive directory for the origin>
134 //
135 // key: "DISABLED_ORIGIN: " + <URL string of a disabled origin>
136 // value: <Resource ID of the drive directory for the origin>
137
138 const char kDatabaseVersionKey[] = "VERSION";
139 const char kDriveMetadataKeyPrefix[] = "METADATA: ";
140 const char kMetadataKeySeparator = ' ';
141
142 leveldb::WriteBatch write_batch;
143 write_batch.Put(kDatabaseVersionKey, "1");
144
145 scoped_ptr<leveldb::Iterator> itr(db->NewIterator(leveldb::ReadOptions()));
146 for (itr->Seek(kDriveMetadataKeyPrefix); itr->Valid(); itr->Next()) {
147 std::string key = itr->key().ToString();
148 if (!StartsWithASCII(key, kDriveMetadataKeyPrefix, true))
149 break;
150 std::string serialized_url(RemovePrefix(key, kDriveMetadataKeyPrefix));
151
152 GURL origin;
153 base::FilePath path;
154 bool success = ParseV0FormatFileSystemURLString(
155 GURL(serialized_url), &origin, &path);
156 DCHECK(success) << serialized_url;
157 std::string new_key = kDriveMetadataKeyPrefix + origin.spec() +
158 kMetadataKeySeparator + path.AsUTF8Unsafe();
159
160 write_batch.Put(new_key, itr->value());
161 write_batch.Delete(key);
162 }
163
164 return LevelDBStatusToSyncStatusCode(
165 db->Write(leveldb::WriteOptions(), &write_batch));
166 }
167
71 SyncStatusCode MigrateDatabaseFromV1ToV2(leveldb::DB* db) { 168 SyncStatusCode MigrateDatabaseFromV1ToV2(leveldb::DB* db) {
72 // Strips prefix of WAPI resource ID. 169 // Strips prefix of WAPI resource ID.
73 // (i.e. "file:xxxx" => "xxxx", "folder:yyyy" => "yyyy") 170 // (i.e. "file:xxxx" => "xxxx", "folder:yyyy" => "yyyy")
74 // 171 //
75 // Version 2 database format (changed keys/fields are marked with '*'): 172 // Version 2 database format (changed keys/fields are marked with '*'):
76 // key: "VERSION" 173 // key: "VERSION"
77 // * value: 2 174 // * value: 2
78 // 175 //
79 // key: "CHANGE_STAMP" 176 // key: "CHANGE_STAMP"
80 // value: <Largest Changestamp> 177 // value: <Largest Changestamp>
(...skipping 55 matching lines...) Expand 10 before | Expand all | Expand 10 after
136 continue; 233 continue;
137 } 234 }
138 } 235 }
139 236
140 return LevelDBStatusToSyncStatusCode( 237 return LevelDBStatusToSyncStatusCode(
141 db->Write(leveldb::WriteOptions(), &write_batch)); 238 db->Write(leveldb::WriteOptions(), &write_batch));
142 } 239 }
143 240
144 } // namespace drive 241 } // namespace drive
145 } // namespace sync_file_system 242 } // namespace sync_file_system
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698