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.
5 #include "chrome/browser/sync_file_system/drive_backend/metadata_db_migration_util.h"
7 #include "base/files/file_path.h"
8 #include "base/memory/scoped_ptr.h"
9 #include "base/strings/string_util.h"
10 #include "chrome/browser/sync_file_system/drive_backend/drive_backend_util.h"
11 #include "storage/common/fileapi/file_system_types.h"
12 #include "storage/common/fileapi/file_system_util.h"
13 #include "third_party/leveldatabase/src/include/leveldb/db.h"
14 #include "third_party/leveldatabase/src/include/leveldb/write_batch.h"
17 namespace sync_file_system
{
18 namespace drive_backend
{
20 SyncStatusCode
MigrateDatabaseFromV4ToV3(leveldb::DB
* db
) {
21 // Rollback from version 4 to version 3.
22 // Please see metadata_database_index.cc for version 3 format, and
23 // metadata_database_index_on_disk.cc for version 4 format.
25 const char kDatabaseVersionKey
[] = "VERSION";
26 const char kServiceMetadataKey
[] = "SERVICE";
27 const char kFileMetadataKeyPrefix
[] = "FILE: ";
28 const char kFileTrackerKeyPrefix
[] = "TRACKER: ";
30 // Key prefixes used in version 4.
31 const char kAppRootIDByAppIDKeyPrefix
[] = "APP_ROOT: ";
32 const char kActiveTrackerIDByFileIDKeyPrefix
[] = "ACTIVE_FILE: ";
33 const char kTrackerIDByFileIDKeyPrefix
[] = "TRACKER_FILE: ";
34 const char kMultiTrackerByFileIDKeyPrefix
[] = "MULTI_FILE: ";
35 const char kActiveTrackerIDByParentAndTitleKeyPrefix
[] = "ACTIVE_PATH: ";
36 const char kTrackerIDByParentAndTitleKeyPrefix
[] = "TRACKER_PATH: ";
37 const char kMultiBackingParentAndTitleKeyPrefix
[] = "MULTI_PATH: ";
38 const char kDirtyIDKeyPrefix
[] = "DIRTY: ";
39 const char kDemotedDirtyIDKeyPrefix
[] = "DEMOTED_DIRTY: ";
41 leveldb::WriteBatch write_batch
;
42 write_batch
.Put(kDatabaseVersionKey
, "3");
44 scoped_ptr
<leveldb::Iterator
> itr(db
->NewIterator(leveldb::ReadOptions()));
45 for (itr
->SeekToFirst(); itr
->Valid(); itr
->Next()) {
46 std::string key
= itr
->key().ToString();
48 // Do nothing for valid entries in both versions.
49 if (base::StartsWith(key
, kServiceMetadataKey
,
50 base::CompareCase::SENSITIVE
) ||
51 base::StartsWith(key
, kFileMetadataKeyPrefix
,
52 base::CompareCase::SENSITIVE
) ||
53 base::StartsWith(key
, kFileTrackerKeyPrefix
,
54 base::CompareCase::SENSITIVE
)) {
58 // Drop entries used in version 4 only.
59 if (base::StartsWith(key
, kAppRootIDByAppIDKeyPrefix
,
60 base::CompareCase::SENSITIVE
) ||
61 base::StartsWith(key
, kActiveTrackerIDByFileIDKeyPrefix
,
62 base::CompareCase::SENSITIVE
) ||
63 base::StartsWith(key
, kTrackerIDByFileIDKeyPrefix
,
64 base::CompareCase::SENSITIVE
) ||
65 base::StartsWith(key
, kMultiTrackerByFileIDKeyPrefix
,
66 base::CompareCase::SENSITIVE
) ||
67 base::StartsWith(key
, kActiveTrackerIDByParentAndTitleKeyPrefix
,
68 base::CompareCase::SENSITIVE
) ||
69 base::StartsWith(key
, kTrackerIDByParentAndTitleKeyPrefix
,
70 base::CompareCase::SENSITIVE
) ||
71 base::StartsWith(key
, kMultiBackingParentAndTitleKeyPrefix
,
72 base::CompareCase::SENSITIVE
) ||
73 base::StartsWith(key
, kDirtyIDKeyPrefix
,
74 base::CompareCase::SENSITIVE
) ||
75 base::StartsWith(key
, kDemotedDirtyIDKeyPrefix
,
76 base::CompareCase::SENSITIVE
)) {
77 write_batch
.Delete(key
);
81 DVLOG(3) << "Unknown key: " << key
<< " was found.";
84 return LevelDBStatusToSyncStatusCode(
85 db
->Write(leveldb::WriteOptions(), &write_batch
));
88 } // namespace drive_backend
89 } // namespace sync_file_system