OLD | NEW |
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 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/syncable.h" | 5 #include "chrome/browser/sync/syncable/syncable.h" |
6 | 6 |
7 #include <algorithm> | 7 #include <algorithm> |
8 #include <cstring> | 8 #include <cstring> |
9 #include <functional> | 9 #include <functional> |
10 #include <iomanip> | 10 #include <iomanip> |
11 #include <iterator> | 11 #include <iterator> |
12 #include <limits> | 12 #include <limits> |
13 #include <set> | 13 #include <set> |
14 #include <string> | 14 #include <string> |
15 | 15 |
16 #include "base/compiler_specific.h" | 16 #include "base/compiler_specific.h" |
| 17 #include "base/debug/trace_event.h" |
17 #include "base/file_util.h" | 18 #include "base/file_util.h" |
18 #include "base/hash_tables.h" | 19 #include "base/hash_tables.h" |
19 #include "base/location.h" | 20 #include "base/location.h" |
20 #include "base/logging.h" | 21 #include "base/logging.h" |
21 #include "base/memory/scoped_ptr.h" | 22 #include "base/memory/scoped_ptr.h" |
22 #include "base/perftimer.h" | 23 #include "base/perftimer.h" |
23 #include "base/stl_util.h" | 24 #include "base/stl_util.h" |
24 #include "base/string_number_conversions.h" | 25 #include "base/string_number_conversions.h" |
25 #include "base/string_util.h" | 26 #include "base/string_util.h" |
26 #include "base/time.h" | 27 #include "base/time.h" |
(...skipping 17 matching lines...) Expand all Loading... |
44 enum InvariantCheckLevel { | 45 enum InvariantCheckLevel { |
45 OFF = 0, | 46 OFF = 0, |
46 VERIFY_IN_MEMORY = 1, | 47 VERIFY_IN_MEMORY = 1, |
47 FULL_DB_VERIFICATION = 2 | 48 FULL_DB_VERIFICATION = 2 |
48 }; | 49 }; |
49 | 50 |
50 static const InvariantCheckLevel kInvariantCheckLevel = VERIFY_IN_MEMORY; | 51 static const InvariantCheckLevel kInvariantCheckLevel = VERIFY_IN_MEMORY; |
51 | 52 |
52 // Max number of milliseconds to spend checking syncable entry invariants | 53 // Max number of milliseconds to spend checking syncable entry invariants |
53 static const int kInvariantCheckMaxMs = 50; | 54 static const int kInvariantCheckMaxMs = 50; |
| 55 |
| 56 // This function checks to see if the given list of Metahandles has any nodes |
| 57 // whose PREV_ID, PARENT_ID or NEXT_ID values refer to ID values that do not |
| 58 // actually exist. Returns true on success. |
| 59 // |
| 60 // This function is "Unsafe" because it does not attempt to acquire any locks |
| 61 // that may be protecting this list that gets passed in. The caller is |
| 62 // responsible for ensuring that no one modifies this list while the function is |
| 63 // running. |
| 64 bool VerifyReferenceIntegrityUnsafe(const syncable::MetahandlesIndex &index) { |
| 65 TRACE_EVENT0("sync", "SyncDatabaseIntegrityCheck"); |
| 66 using namespace syncable; |
| 67 typedef base::hash_set<std::string> IdsSet; |
| 68 |
| 69 IdsSet ids_set; |
| 70 bool is_ok = true; |
| 71 |
| 72 for (MetahandlesIndex::const_iterator it = index.begin(); |
| 73 it != index.end(); ++it) { |
| 74 EntryKernel* entry = *it; |
| 75 bool is_duplicate_id = !(ids_set.insert(entry->ref(ID).value()).second); |
| 76 is_ok = is_ok && !is_duplicate_id; |
| 77 } |
| 78 |
| 79 IdsSet::iterator end = ids_set.end(); |
| 80 for (MetahandlesIndex::const_iterator it = index.begin(); |
| 81 it != index.end(); ++it) { |
| 82 EntryKernel* entry = *it; |
| 83 bool prev_exists = (ids_set.find(entry->ref(PREV_ID).value()) != end); |
| 84 bool parent_exists = (ids_set.find(entry->ref(PARENT_ID).value()) != end); |
| 85 bool next_exists = (ids_set.find(entry->ref(NEXT_ID).value()) != end); |
| 86 is_ok = is_ok && prev_exists && parent_exists && next_exists; |
| 87 } |
| 88 return is_ok; |
| 89 } |
| 90 |
54 } // namespace | 91 } // namespace |
55 | 92 |
56 using std::string; | 93 using std::string; |
57 | 94 |
58 namespace syncable { | 95 namespace syncable { |
59 | 96 |
60 #define ENUM_CASE(x) case x: return #x; break | 97 #define ENUM_CASE(x) case x: return #x; break |
61 | 98 |
62 std::string WriterTagToString(WriterTag writer_tag) { | 99 std::string WriterTagToString(WriterTag writer_tag) { |
63 switch (writer_tag) { | 100 switch (writer_tag) { |
(...skipping 406 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
470 const string& dir_name, const FilePath& backing_filepath) { | 507 const string& dir_name, const FilePath& backing_filepath) { |
471 return new DirectoryBackingStore(dir_name, backing_filepath); | 508 return new DirectoryBackingStore(dir_name, backing_filepath); |
472 } | 509 } |
473 | 510 |
474 DirOpenResult Directory::OpenImpl( | 511 DirOpenResult Directory::OpenImpl( |
475 const FilePath& file_path, | 512 const FilePath& file_path, |
476 const string& name, | 513 const string& name, |
477 DirectoryChangeDelegate* delegate, | 514 DirectoryChangeDelegate* delegate, |
478 const browser_sync::WeakHandle<TransactionObserver>& | 515 const browser_sync::WeakHandle<TransactionObserver>& |
479 transaction_observer) { | 516 transaction_observer) { |
| 517 TRACE_EVENT0("sync", "SyncDatabaseOpen"); |
480 DCHECK_EQ(static_cast<DirectoryBackingStore*>(NULL), store_); | 518 DCHECK_EQ(static_cast<DirectoryBackingStore*>(NULL), store_); |
481 FilePath db_path(file_path); | 519 FilePath db_path(file_path); |
482 file_util::AbsolutePath(&db_path); | 520 file_util::AbsolutePath(&db_path); |
483 store_ = CreateBackingStore(name, db_path); | 521 store_ = CreateBackingStore(name, db_path); |
484 | 522 |
485 KernelLoadInfo info; | 523 KernelLoadInfo info; |
486 // Temporary indices before kernel_ initialized in case Load fails. We 0(1) | 524 // Temporary indices before kernel_ initialized in case Load fails. We 0(1) |
487 // swap these later. | 525 // swap these later. |
488 MetahandlesIndex metas_bucket; | 526 MetahandlesIndex metas_bucket; |
489 DirOpenResult result = store_->Load(&metas_bucket, &info); | 527 DirOpenResult result = store_->Load(&metas_bucket, &info); |
490 if (OPENED != result) | 528 if (OPENED != result) |
491 return result; | 529 return result; |
492 | 530 |
| 531 if (!VerifyReferenceIntegrityUnsafe(metas_bucket)) |
| 532 return FAILED_LOGICAL_CORRUPTION; |
| 533 |
493 kernel_ = new Kernel(db_path, name, info, delegate, transaction_observer); | 534 kernel_ = new Kernel(db_path, name, info, delegate, transaction_observer); |
494 kernel_->metahandles_index->swap(metas_bucket); | 535 kernel_->metahandles_index->swap(metas_bucket); |
495 InitializeIndices(); | 536 InitializeIndices(); |
496 return OPENED; | 537 return OPENED; |
497 } | 538 } |
498 | 539 |
499 void Directory::Close() { | 540 void Directory::Close() { |
500 if (store_) | 541 if (store_) |
501 delete store_; | 542 delete store_; |
502 store_ = NULL; | 543 store_ = NULL; |
(...skipping 1523 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
2026 if (entry->ref(NEXT_ID).IsRoot() || | 2067 if (entry->ref(NEXT_ID).IsRoot() || |
2027 entry->ref(NEXT_ID) != entry->ref(PREV_ID)) { | 2068 entry->ref(NEXT_ID) != entry->ref(PREV_ID)) { |
2028 return entry; | 2069 return entry; |
2029 } | 2070 } |
2030 } | 2071 } |
2031 // There were no children in the linked list. | 2072 // There were no children in the linked list. |
2032 return NULL; | 2073 return NULL; |
2033 } | 2074 } |
2034 | 2075 |
2035 } // namespace syncable | 2076 } // namespace syncable |
OLD | NEW |