OLD | NEW |
---|---|
(Empty) | |
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 | |
3 // found in the LICENSE file. | |
4 | |
5 #include "chrome/browser/sync_file_system/drive/metadata_db_migration_util.h" | |
6 | |
7 #include "base/files/file_path.h" | |
8 #include "base/memory/scoped_ptr.h" | |
9 #include "base/string_util.h" | |
10 #include "googleurl/src/gurl.h" | |
11 #include "third_party/leveldatabase/src/include/leveldb/write_batch.h" | |
12 | |
13 namespace sync_file_system { | |
14 namespace drive { | |
15 | |
16 namespace { | |
17 | |
18 const char kWapiFileIdPrefix[] = "file:"; | |
19 const char kWapiFolderIdPrefix[] = "folder:"; | |
20 | |
21 std::string RemovePrefix(const std::string& str, const std::string& prefix) { | |
22 if (StartsWithASCII(str, prefix, true)) | |
23 return std::string(str.begin() + prefix.size(), str.end()); | |
24 return str; | |
25 } | |
26 | |
27 } // namespace | |
28 | |
29 std::string AddWapiFilePrefix(const std::string& resource_id) { | |
30 DCHECK(!StartsWithASCII(resource_id, kWapiFileIdPrefix, true)); | |
kinuko
2013/05/29 07:52:34
We can probably remove this DCHECK, it's not very
nhiroki
2013/05/29 07:58:59
Hmm... if resource ID with prefix reaches here, it
kinuko
2013/05/29 08:14:54
Ok then please keep them.
| |
31 DCHECK(!StartsWithASCII(resource_id, kWapiFolderIdPrefix, true)); | |
32 | |
33 if (resource_id.empty() || | |
34 StartsWithASCII(resource_id, kWapiFileIdPrefix, true) || | |
35 StartsWithASCII(resource_id, kWapiFolderIdPrefix, true)) | |
36 return resource_id; | |
37 return kWapiFileIdPrefix + resource_id; | |
38 } | |
39 | |
40 std::string AddWapiFolderPrefix(const std::string& resource_id) { | |
41 DCHECK(!StartsWithASCII(resource_id, kWapiFileIdPrefix, true)); | |
42 DCHECK(!StartsWithASCII(resource_id, kWapiFolderIdPrefix, true)); | |
kinuko
2013/05/29 07:52:34
ditto
| |
43 | |
44 if (resource_id.empty() || | |
45 StartsWithASCII(resource_id, kWapiFileIdPrefix, true) || | |
46 StartsWithASCII(resource_id, kWapiFolderIdPrefix, true)) | |
47 return resource_id; | |
48 return kWapiFolderIdPrefix + resource_id; | |
49 } | |
50 | |
51 std::string AddWapiIdPrefix(const std::string& resource_id, | |
52 DriveMetadata_ResourceType type) { | |
53 switch (type) { | |
54 case DriveMetadata_ResourceType_RESOURCE_TYPE_FILE: | |
55 return AddWapiFilePrefix(resource_id); | |
56 case DriveMetadata_ResourceType_RESOURCE_TYPE_FOLDER: | |
57 return AddWapiFolderPrefix(resource_id); | |
58 } | |
59 NOTREACHED(); | |
60 return resource_id; | |
61 } | |
62 | |
63 std::string RemoveWapiIdPrefix(const std::string& resource_id) { | |
64 if (StartsWithASCII(resource_id, kWapiFileIdPrefix, true)) | |
65 return RemovePrefix(resource_id, kWapiFileIdPrefix); | |
66 if (StartsWithASCII(resource_id, kWapiFolderIdPrefix, true)) | |
67 return RemovePrefix(resource_id, kWapiFolderIdPrefix); | |
68 return resource_id; | |
69 } | |
70 | |
71 SyncStatusCode MigrateDatabaseFromV1ToV2(leveldb::DB* db) { | |
72 // Strips prefix of WAPI resource ID. | |
73 // (i.e. "file:xxxx" => "xxxx", "folder:yyyy" => "yyyy") | |
74 // | |
75 // Version 2 database format (changed keys/fields are marked with '*'): | |
76 // key: "VERSION" | |
77 // * value: 2 | |
78 // | |
79 // key: "CHANGE_STAMP" | |
80 // value: <Largest Changestamp> | |
81 // | |
82 // key: "SYNC_ROOT_DIR" | |
83 // * value: <Resource ID of the sync root directory> (striped) | |
84 // | |
85 // key: "METADATA: " + <Origin and URL> | |
86 // * value: <Serialized DriveMetadata> (striped) | |
87 // | |
88 // key: "ISYNC_ORIGIN: " + <URL string of a incremental sync origin> | |
89 // * value: <Resource ID of the drive directory for the origin> (striped) | |
90 // | |
91 // key: "DISABLED_ORIGIN: " + <URL string of a disabled origin> | |
92 // * value: <Resource ID of the drive directory for the origin> (striped) | |
93 | |
94 const char kDatabaseVersionKey[] = "VERSION"; | |
95 const char kSyncRootDirectoryKey[] = "SYNC_ROOT_DIR"; | |
96 const char kDriveMetadataKeyPrefix[] = "METADATA: "; | |
97 const char kDriveIncrementalSyncOriginKeyPrefix[] = "ISYNC_ORIGIN: "; | |
98 const char kDriveDisabledOriginKeyPrefix[] = "DISABLED_ORIGIN: "; | |
99 | |
100 leveldb::WriteBatch write_batch; | |
101 write_batch.Put(kDatabaseVersionKey, "2"); | |
102 | |
103 scoped_ptr<leveldb::Iterator> itr(db->NewIterator(leveldb::ReadOptions())); | |
104 for (itr->SeekToFirst(); itr->Valid(); itr->Next()) { | |
105 std::string key = itr->key().ToString(); | |
106 | |
107 // Strip resource id for the sync root directory. | |
108 if (StartsWithASCII(key, kSyncRootDirectoryKey, true)) { | |
109 write_batch.Put(key, RemoveWapiIdPrefix(itr->value().ToString())); | |
110 continue; | |
111 } | |
112 | |
113 // Strip resource ids in the drive metadata. | |
114 if (StartsWithASCII(key, kDriveMetadataKeyPrefix, true)) { | |
115 DriveMetadata metadata; | |
116 bool success = metadata.ParseFromString(itr->value().ToString()); | |
117 DCHECK(success); | |
118 | |
119 metadata.set_resource_id(RemoveWapiIdPrefix(metadata.resource_id())); | |
120 std::string metadata_string; | |
121 metadata.SerializeToString(&metadata_string); | |
122 | |
123 write_batch.Put(key, metadata_string); | |
124 continue; | |
125 } | |
126 | |
127 // Strip resource ids of the incremental sync origins. | |
128 if (StartsWithASCII(key, kDriveIncrementalSyncOriginKeyPrefix, true)) { | |
129 write_batch.Put(key, RemoveWapiIdPrefix(itr->value().ToString())); | |
130 continue; | |
131 } | |
132 | |
133 // Strip resource ids of the disabled sync origins. | |
134 if (StartsWithASCII(key, kDriveDisabledOriginKeyPrefix, true)) { | |
135 write_batch.Put(key, RemoveWapiIdPrefix(itr->value().ToString())); | |
136 continue; | |
137 } | |
138 } | |
139 | |
140 return LevelDBStatusToSyncStatusCode( | |
141 db->Write(leveldb::WriteOptions(), &write_batch)); | |
142 } | |
143 | |
144 } // namespace drive | |
145 } // namespace sync_file_system | |
OLD | NEW |