Upstream version 10.39.225.0
[platform/framework/web/crosswalk.git] / src / chrome / browser / sync_file_system / drive_backend / metadata_db_migration_util.cc
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 "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"
15 #include "url/gurl.h"
16
17 namespace sync_file_system {
18 namespace drive_backend {
19
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.
24
25   const char kDatabaseVersionKey[] = "VERSION";
26   const char kServiceMetadataKey[] = "SERVICE";
27   const char kFileMetadataKeyPrefix[] = "FILE: ";
28   const char kFileTrackerKeyPrefix[] = "TRACKER: ";
29
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: ";
40
41   leveldb::WriteBatch write_batch;
42   write_batch.Put(kDatabaseVersionKey, "3");
43
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();
47
48     // Do nothing for valid entries in both versions.
49     if (StartsWithASCII(key, kServiceMetadataKey, true) ||
50         StartsWithASCII(key, kFileMetadataKeyPrefix, true) ||
51         StartsWithASCII(key, kFileTrackerKeyPrefix, true)) {
52       continue;
53     }
54
55     // Drop entries used in version 4 only.
56     if (StartsWithASCII(key, kAppRootIDByAppIDKeyPrefix, true) ||
57         StartsWithASCII(key, kActiveTrackerIDByFileIDKeyPrefix, true) ||
58         StartsWithASCII(key, kTrackerIDByFileIDKeyPrefix, true) ||
59         StartsWithASCII(key, kMultiTrackerByFileIDKeyPrefix, true) ||
60         StartsWithASCII(key, kActiveTrackerIDByParentAndTitleKeyPrefix, true) ||
61         StartsWithASCII(key, kTrackerIDByParentAndTitleKeyPrefix, true) ||
62         StartsWithASCII(key, kMultiBackingParentAndTitleKeyPrefix, true) ||
63         StartsWithASCII(key, kDirtyIDKeyPrefix, true) ||
64         StartsWithASCII(key, kDemotedDirtyIDKeyPrefix, true)) {
65       write_batch.Delete(key);
66       continue;
67     }
68
69     DVLOG(3) << "Unknown key: " << key << " was found.";
70   }
71
72   return LevelDBStatusToSyncStatusCode(
73       db->Write(leveldb::WriteOptions(), &write_batch));
74 }
75
76 }  // namespace drive_backend
77 }  // namespace sync_file_system