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
,
28 const base::FilePath
& backing_file_path
)
29 : DirectoryBackingStore(dir_name
),
30 allow_failure_for_test_(false),
31 backing_file_path_(backing_file_path
) {
34 OnDiskDirectoryBackingStore::~OnDiskDirectoryBackingStore() {
37 DirOpenResult
OnDiskDirectoryBackingStore::TryLoad(
38 Directory::MetahandlesMap
* handles_map
,
39 JournalIndex
* delete_journals
,
40 MetahandleSet
* metahandles_to_purge
,
41 Directory::KernelLoadInfo
* kernel_load_info
) {
42 DCHECK(CalledOnValidThread());
45 if (!Open(backing_file_path_
))
46 return FAILED_OPEN_DATABASE
;
49 if (!InitializeTables())
50 return FAILED_OPEN_DATABASE
;
52 if (!LoadEntries(handles_map
, metahandles_to_purge
))
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(handles_map
))
59 return FAILED_DATABASE_CORRUPT
;
64 DirOpenResult
OnDiskDirectoryBackingStore::Load(
65 Directory::MetahandlesMap
* handles_map
,
66 JournalIndex
* delete_journals
,
67 MetahandleSet
* metahandles_to_purge
,
68 Directory::KernelLoadInfo
* kernel_load_info
) {
69 DCHECK(CalledOnValidThread());
70 DirOpenResult result
= TryLoad(handles_map
, delete_journals
,
71 metahandles_to_purge
, kernel_load_info
);
72 if (result
== OPENED
) {
73 UMA_HISTOGRAM_ENUMERATION("Sync.DirectoryOpenResult", FIRST_TRY_SUCCESS
,
78 ReportFirstTryOpenFailure();
80 // The fallback: delete the current database and return a fresh one. We can
81 // fetch the user's data from the cloud.
82 STLDeleteValues(handles_map
);
83 STLDeleteElements(delete_journals
);
85 ResetAndCreateConnection();
87 base::DeleteFile(backing_file_path_
, false);
89 result
= TryLoad(handles_map
, delete_journals
, metahandles_to_purge
,
91 if (result
== OPENED
) {
92 UMA_HISTOGRAM_ENUMERATION("Sync.DirectoryOpenResult", SECOND_TRY_SUCCESS
,
95 UMA_HISTOGRAM_ENUMERATION("Sync.DirectoryOpenResult", SECOND_TRY_FAILURE
,
102 void OnDiskDirectoryBackingStore::ReportFirstTryOpenFailure() {
103 // In debug builds, the last thing we want is to silently clear the database.
104 // It's full of evidence that might help us determine what went wrong. It
105 // might be sqlite's fault, but it could also be a bug in sync. We crash
106 // immediately so a developer can investigate.
108 // Developers: If you're not interested in debugging this right now, just move
109 // aside the 'Sync Data' directory in your profile. This is similar to what
110 // the code would do if this DCHECK were disabled.
111 NOTREACHED() << "Crashing to preserve corrupt sync database";
114 const base::FilePath
& OnDiskDirectoryBackingStore::backing_file_path() const {
115 DCHECK(CalledOnValidThread());
116 return backing_file_path_
;
119 } // namespace syncable
120 } // namespace syncer