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