Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(51)

Side by Side Diff: chrome/browser/sync/syncable/directory_backing_store.cc

Issue 1733008: Delete and recreate the sync database if it is corrupt. (Closed)
Patch Set: Use scoped_sqlite_db_ptr Created 10 years, 7 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 // Copyright (c) 2009 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2010 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "chrome/browser/sync/syncable/directory_backing_store.h" 5 #include "chrome/browser/sync/syncable/directory_backing_store.h"
6 6
7 #include "build/build_config.h" 7 #include "build/build_config.h"
8 8
9 #if defined(OS_MACOSX) 9 #if defined(OS_MACOSX)
10 #include <CoreFoundation/CoreFoundation.h> 10 #include <CoreFoundation/CoreFoundation.h>
11 #endif 11 #endif
12 12
13 #include <limits> 13 #include <limits>
14 14
15 #include "base/file_util.h"
15 #include "base/hash_tables.h" 16 #include "base/hash_tables.h"
16 #include "base/logging.h" 17 #include "base/logging.h"
17 #include "chrome/browser/sync/protocol/bookmark_specifics.pb.h" 18 #include "chrome/browser/sync/protocol/bookmark_specifics.pb.h"
18 #include "chrome/browser/sync/protocol/service_constants.h" 19 #include "chrome/browser/sync/protocol/service_constants.h"
19 #include "chrome/browser/sync/protocol/sync.pb.h" 20 #include "chrome/browser/sync/protocol/sync.pb.h"
20 #include "chrome/browser/sync/syncable/syncable-inl.h" 21 #include "chrome/browser/sync/syncable/syncable-inl.h"
21 #include "chrome/browser/sync/syncable/syncable_columns.h" 22 #include "chrome/browser/sync/syncable/syncable_columns.h"
22 #include "chrome/browser/sync/util/crypto_helpers.h" 23 #include "chrome/browser/sync/util/crypto_helpers.h"
23 #include "chrome/common/sqlite_utils.h" 24 #include "chrome/common/sqlite_utils.h"
24 #include "third_party/sqlite/preprocessed/sqlite3.h" 25 #include "third_party/sqlite/preprocessed/sqlite3.h"
(...skipping 147 matching lines...) Expand 10 before | Expand all | Expand 10 after
172 } 173 }
173 if (NULL != save_dbhandle_) { 174 if (NULL != save_dbhandle_) {
174 sqlite3_close(save_dbhandle_); 175 sqlite3_close(save_dbhandle_);
175 save_dbhandle_ = NULL; 176 save_dbhandle_ = NULL;
176 } 177 }
177 } 178 }
178 179
179 bool DirectoryBackingStore::OpenAndConfigureHandleHelper( 180 bool DirectoryBackingStore::OpenAndConfigureHandleHelper(
180 sqlite3** handle) const { 181 sqlite3** handle) const {
181 if (SQLITE_OK == OpenSqliteDb(backing_filepath_, handle)) { 182 if (SQLITE_OK == OpenSqliteDb(backing_filepath_, handle)) {
183 sqlite_utils::scoped_sqlite_db_ptr scoped_handle(*handle);
182 sqlite3_busy_timeout(*handle, std::numeric_limits<int>::max()); 184 sqlite3_busy_timeout(*handle, std::numeric_limits<int>::max());
183 { 185 {
184 SQLStatement statement; 186 SQLStatement statement;
185 statement.prepare(*handle, "PRAGMA fullfsync = 1"); 187 statement.prepare(*handle, "PRAGMA fullfsync = 1");
186 if (SQLITE_DONE != statement.step()) { 188 if (SQLITE_DONE != statement.step()) {
187 LOG(FATAL) << sqlite3_errmsg(*handle); 189 LOG(ERROR) << sqlite3_errmsg(*handle);
190 return false;
188 } 191 }
189 } 192 }
190 { 193 {
191 SQLStatement statement; 194 SQLStatement statement;
192 statement.prepare(*handle, "PRAGMA synchronous = 2"); 195 statement.prepare(*handle, "PRAGMA synchronous = 2");
193 if (SQLITE_DONE != statement.step()) { 196 if (SQLITE_DONE != statement.step()) {
194 LOG(FATAL) << sqlite3_errmsg(*handle); 197 LOG(ERROR) << sqlite3_errmsg(*handle);
198 return false;
195 } 199 }
196 } 200 }
197 sqlite3_busy_timeout(*handle, kDirectoryBackingStoreBusyTimeoutMs); 201 sqlite3_busy_timeout(*handle, kDirectoryBackingStoreBusyTimeoutMs);
198 #if defined(OS_WIN) 202 #if defined(OS_WIN)
199 // Do not index this file. Scanning can occur every time we close the file, 203 // Do not index this file. Scanning can occur every time we close the file,
200 // which causes long delays in SQLite's file locking. 204 // which causes long delays in SQLite's file locking.
201 const DWORD attrs = GetFileAttributes(backing_filepath_.value().c_str()); 205 const DWORD attrs = GetFileAttributes(backing_filepath_.value().c_str());
202 const BOOL attrs_set = 206 const BOOL attrs_set =
203 SetFileAttributes(backing_filepath_.value().c_str(), 207 SetFileAttributes(backing_filepath_.value().c_str(),
204 attrs | FILE_ATTRIBUTE_NOT_CONTENT_INDEXED); 208 attrs | FILE_ATTRIBUTE_NOT_CONTENT_INDEXED);
205 #endif 209 #endif
206 210
211 scoped_handle.release();
tim (not reviewing) 2010/04/27 14:25:03 this is necessary?
albertb 2010/04/27 15:05:41 Yes. If the scoped ptr holds a valid db handle, it
207 return true; 212 return true;
208 } 213 }
209 return false; 214 return false;
210 } 215 }
211 216
212 DirOpenResult DirectoryBackingStore::Load(MetahandlesIndex* entry_bucket, 217 DirOpenResult DirectoryBackingStore::Load(MetahandlesIndex* entry_bucket,
213 ExtendedAttributes* xattrs_bucket, 218 ExtendedAttributes* xattrs_bucket,
214 Directory::KernelLoadInfo* kernel_load_info) { 219 Directory::KernelLoadInfo* kernel_load_info) {
215 if (!BeginLoad()) 220 if (!BeginLoad())
216 return FAILED_OPEN_DATABASE; 221 return FAILED_OPEN_DATABASE;
217 222
218 DirOpenResult result = InitializeTables(); 223 DirOpenResult result = InitializeTables();
219 if (OPENED != result) 224 if (OPENED != result)
220 return result; 225 return result;
221 226
222 DropDeletedEntries(); 227 DropDeletedEntries();
223 LoadEntries(entry_bucket); 228 LoadEntries(entry_bucket);
224 LoadExtendedAttributes(xattrs_bucket); 229 LoadExtendedAttributes(xattrs_bucket);
225 LoadInfo(kernel_load_info); 230 LoadInfo(kernel_load_info);
226 231
227 EndLoad(); 232 EndLoad();
228 return OPENED; 233 return OPENED;
229 } 234 }
230 235
231 bool DirectoryBackingStore::BeginLoad() { 236 bool DirectoryBackingStore::BeginLoad() {
232 DCHECK(load_dbhandle_ == NULL); 237 DCHECK(load_dbhandle_ == NULL);
238 bool ret = OpenAndConfigureHandleHelper(&load_dbhandle_);
239 if (ret)
240 return ret;
241 // Something's gone wrong. Nuke the database and try again.
242 LOG(ERROR) << "Sync database " << backing_filepath_.value()
243 << " corrupt. Deleting and recreating.";
244 file_util::Delete(backing_filepath_, false);
233 return OpenAndConfigureHandleHelper(&load_dbhandle_); 245 return OpenAndConfigureHandleHelper(&load_dbhandle_);
234 } 246 }
235 247
236 void DirectoryBackingStore::EndLoad() { 248 void DirectoryBackingStore::EndLoad() {
237 sqlite3_close(load_dbhandle_); 249 sqlite3_close(load_dbhandle_);
238 load_dbhandle_ = NULL; // No longer used. 250 load_dbhandle_ = NULL; // No longer used.
239 } 251 }
240 252
241 bool DirectoryBackingStore::SaveChanges( 253 bool DirectoryBackingStore::SaveChanges(
242 const Directory::SaveChangesSnapshot& snapshot) { 254 const Directory::SaveChangesSnapshot& snapshot) {
(...skipping 727 matching lines...) Expand 10 before | Expand all | Expand 10 after
970 "name TEXT, " 982 "name TEXT, "
971 "store_birthday TEXT, " 983 "store_birthday TEXT, "
972 "db_create_version TEXT, " 984 "db_create_version TEXT, "
973 "db_create_time INT, " 985 "db_create_time INT, "
974 "next_id INT default -2, " 986 "next_id INT default -2, "
975 "cache_guid TEXT)"); 987 "cache_guid TEXT)");
976 return ExecQuery(load_dbhandle_, query.c_str()); 988 return ExecQuery(load_dbhandle_, query.c_str());
977 } 989 }
978 990
979 } // namespace syncable 991 } // namespace syncable
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698