OLD | NEW |
| (Empty) |
1 // Copyright (c) 2012 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 "sync/syncable/on_disk_directory_backing_store.h" | |
6 | |
7 #include "base/logging.h" | |
8 #include "base/stl_util.h" | |
9 #include "base/metrics/histogram.h" | |
10 #include "sync/syncable/syncable-inl.h" | |
11 | |
12 namespace syncer { | |
13 namespace syncable { | |
14 | |
15 namespace { | |
16 | |
17 enum HistogramResultEnum { | |
18 FIRST_TRY_SUCCESS, | |
19 SECOND_TRY_SUCCESS, | |
20 SECOND_TRY_FAILURE, | |
21 RESULT_COUNT | |
22 }; | |
23 | |
24 } // namespace | |
25 | |
26 OnDiskDirectoryBackingStore::OnDiskDirectoryBackingStore( | |
27 const std::string& dir_name, | |
28 const base::FilePath& backing_file_path) | |
29 : DirectoryBackingStore(dir_name), backing_file_path_(backing_file_path) { | |
30 DCHECK(backing_file_path_.IsAbsolute()); | |
31 } | |
32 | |
33 OnDiskDirectoryBackingStore::~OnDiskDirectoryBackingStore() { | |
34 } | |
35 | |
36 DirOpenResult OnDiskDirectoryBackingStore::TryLoad( | |
37 Directory::MetahandlesMap* handles_map, | |
38 JournalIndex* delete_journals, | |
39 MetahandleSet* metahandles_to_purge, | |
40 Directory::KernelLoadInfo* kernel_load_info) { | |
41 DCHECK(CalledOnValidThread()); | |
42 | |
43 if (!IsOpen()) { | |
44 if (!Open(backing_file_path_)) | |
45 return FAILED_OPEN_DATABASE; | |
46 } | |
47 | |
48 if (!InitializeTables()) | |
49 return FAILED_OPEN_DATABASE; | |
50 | |
51 if (!LoadEntries(handles_map, metahandles_to_purge)) | |
52 return FAILED_DATABASE_CORRUPT; | |
53 if (!LoadDeleteJournals(delete_journals)) | |
54 return FAILED_DATABASE_CORRUPT; | |
55 if (!LoadInfo(kernel_load_info)) | |
56 return FAILED_DATABASE_CORRUPT; | |
57 if (!VerifyReferenceIntegrity(handles_map)) | |
58 return FAILED_DATABASE_CORRUPT; | |
59 | |
60 return OPENED; | |
61 } | |
62 | |
63 DirOpenResult OnDiskDirectoryBackingStore::Load( | |
64 Directory::MetahandlesMap* handles_map, | |
65 JournalIndex* delete_journals, | |
66 MetahandleSet* metahandles_to_purge, | |
67 Directory::KernelLoadInfo* kernel_load_info) { | |
68 DCHECK(CalledOnValidThread()); | |
69 DirOpenResult result = TryLoad(handles_map, delete_journals, | |
70 metahandles_to_purge, kernel_load_info); | |
71 if (result == OPENED) { | |
72 UMA_HISTOGRAM_ENUMERATION("Sync.DirectoryOpenResult", FIRST_TRY_SUCCESS, | |
73 RESULT_COUNT); | |
74 return OPENED; | |
75 } | |
76 | |
77 ReportFirstTryOpenFailure(); | |
78 | |
79 // The fallback: delete the current database and return a fresh one. We can | |
80 // fetch the user's data from the cloud. | |
81 STLDeleteValues(handles_map); | |
82 STLDeleteElements(delete_journals); | |
83 | |
84 ResetAndCreateConnection(); | |
85 | |
86 base::DeleteFile(backing_file_path_, false); | |
87 | |
88 result = TryLoad(handles_map, delete_journals, metahandles_to_purge, | |
89 kernel_load_info); | |
90 if (result == OPENED) { | |
91 UMA_HISTOGRAM_ENUMERATION("Sync.DirectoryOpenResult", SECOND_TRY_SUCCESS, | |
92 RESULT_COUNT); | |
93 } else { | |
94 UMA_HISTOGRAM_ENUMERATION("Sync.DirectoryOpenResult", SECOND_TRY_FAILURE, | |
95 RESULT_COUNT); | |
96 } | |
97 | |
98 return result; | |
99 } | |
100 | |
101 void OnDiskDirectoryBackingStore::ReportFirstTryOpenFailure() { | |
102 // In debug builds, the last thing we want is to silently clear the database. | |
103 // It's full of evidence that might help us determine what went wrong. It | |
104 // might be sqlite's fault, but it could also be a bug in sync. We crash | |
105 // immediately so a developer can investigate. | |
106 // | |
107 // Developers: If you're not interested in debugging this right now, just move | |
108 // aside the 'Sync Data' directory in your profile. This is similar to what | |
109 // the code would do if this DCHECK were disabled. | |
110 NOTREACHED() << "Crashing to preserve corrupt sync database"; | |
111 } | |
112 | |
113 const base::FilePath& OnDiskDirectoryBackingStore::backing_file_path() const { | |
114 DCHECK(CalledOnValidThread()); | |
115 return backing_file_path_; | |
116 } | |
117 | |
118 } // namespace syncable | |
119 } // namespace syncer | |
OLD | NEW |