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.
5 #include "sync/syncable/on_disk_directory_backing_store.h"
7 #include "base/logging.h"
8 #include "base/stl_util.h"
9 #include "base/metrics/histogram.h"
10 #include "sync/syncable/syncable-inl.h"
17 enum HistogramResultEnum
{
26 OnDiskDirectoryBackingStore::OnDiskDirectoryBackingStore(
27 const std::string
& dir_name
, const FilePath
& backing_filepath
)
28 : DirectoryBackingStore(dir_name
),
29 allow_failure_for_test_(false),
30 backing_filepath_(backing_filepath
) {
31 db_
->set_exclusive_locking();
32 db_
->set_page_size(4096);
35 OnDiskDirectoryBackingStore::~OnDiskDirectoryBackingStore() { }
37 DirOpenResult
OnDiskDirectoryBackingStore::TryLoad(
38 MetahandlesIndex
* entry_bucket
,
39 JournalIndex
* delete_journals
,
40 Directory::KernelLoadInfo
* kernel_load_info
) {
41 DCHECK(CalledOnValidThread());
42 if (!db_
->is_open()) {
43 if (!db_
->Open(backing_filepath_
))
44 return FAILED_OPEN_DATABASE
;
47 if (!InitializeTables())
48 return FAILED_OPEN_DATABASE
;
50 if (!DropDeletedEntries())
51 return FAILED_DATABASE_CORRUPT
;
52 if (!LoadEntries(entry_bucket
))
53 return FAILED_DATABASE_CORRUPT
;
54 if (!LoadDeleteJournals(delete_journals
))
55 return FAILED_DATABASE_CORRUPT
;
56 if (!LoadInfo(kernel_load_info
))
57 return FAILED_DATABASE_CORRUPT
;
58 if (!VerifyReferenceIntegrity(*entry_bucket
))
59 return FAILED_DATABASE_CORRUPT
;
65 DirOpenResult
OnDiskDirectoryBackingStore::Load(
66 MetahandlesIndex
* entry_bucket
,
67 JournalIndex
* delete_journals
,
68 Directory::KernelLoadInfo
* kernel_load_info
) {
69 DirOpenResult result
= TryLoad(entry_bucket
, delete_journals
,
71 if (result
== OPENED
) {
72 UMA_HISTOGRAM_ENUMERATION(
73 "Sync.DirectoryOpenResult", FIRST_TRY_SUCCESS
, RESULT_COUNT
);
77 ReportFirstTryOpenFailure();
79 // The fallback: delete the current database and return a fresh one. We can
80 // fetch the user's data from the cloud.
81 STLDeleteElements(entry_bucket
);
82 STLDeleteElements(delete_journals
);
83 db_
.reset(new sql::Connection
);
84 file_util::Delete(backing_filepath_
, false);
86 result
= TryLoad(entry_bucket
, delete_journals
, kernel_load_info
);
87 if (result
== OPENED
) {
88 UMA_HISTOGRAM_ENUMERATION(
89 "Sync.DirectoryOpenResult", SECOND_TRY_SUCCESS
, RESULT_COUNT
);
91 UMA_HISTOGRAM_ENUMERATION(
92 "Sync.DirectoryOpenResult", SECOND_TRY_FAILURE
, RESULT_COUNT
);
98 void OnDiskDirectoryBackingStore::ReportFirstTryOpenFailure() {
99 // In debug builds, the last thing we want is to silently clear the database.
100 // It's full of evidence that might help us determine what went wrong. It
101 // might be sqlite's fault, but it could also be a bug in sync. We crash
102 // immediately so a developer can investigate.
104 // Developers: If you're not interested in debugging this right now, just move
105 // aside the 'Sync Data' directory in your profile. This is similar to what
106 // the code would do if this DCHECK were disabled.
107 NOTREACHED() << "Crashing to preserve corrupt sync database";
110 } // namespace syncable
111 } // namespace syncer