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 struct IdPtrHashFunc { | |
57 size_t operator()(const syncable::Id* id) const { | |
58 return BASE_HASH_NAMESPACE::hash<std::string>()(id->value()); | |
59 } | |
60 }; | |
61 | |
62 struct IdPtrCompareFunc { | |
63 bool operator()(const syncable::Id* id1, const syncable::Id* id2) const { | |
64 return *id1 == *id2; | |
65 } | |
66 }; | |
67 | |
68 // This function checks to see if the given list of Metahandles has any nodes | |
69 // whose PREV_ID, PARENT_ID or NEXT_ID values refer to ID values that do not | |
70 // actually exist. Returns true on success. | |
71 bool VerifyReferenceIntegrity(const syncable::MetahandlesIndex &index) { | |
tim (not reviewing)
2011/11/14 19:23:44
Should append 'Unsafe' to the function name, since
rlarocque
2011/11/14 22:05:11
I decided against making this a member of Director
| |
72 using namespace syncable; | |
73 typedef base::hash_set<const Id*, IdPtrHashFunc, IdPtrCompareFunc> IdsSet; | |
74 | |
75 IdsSet ids_set; | |
76 bool is_ok = true; | |
77 | |
78 ids_set.resize(index.size()); | |
79 for (MetahandlesIndex::iterator it = index.begin(); | |
80 it != index.end(); ++it) { | |
81 EntryKernel* entry = *it; | |
82 bool is_duplicate_id = !(ids_set.insert(&entry->ref(ID)).second); | |
83 is_ok = is_ok && !is_duplicate_id; | |
84 } | |
85 | |
86 IdsSet::iterator end = ids_set.end(); | |
87 for (MetahandlesIndex::iterator it = index.begin(); | |
88 it != index.end(); ++it) { | |
89 EntryKernel* entry = *it; | |
90 bool prev_exists = (ids_set.find(&(entry->ref(PREV_ID))) != end); | |
91 bool parent_exists = (ids_set.find(&(entry->ref(PARENT_ID))) != end); | |
92 bool next_exists = (ids_set.find(&(entry->ref(NEXT_ID))) != end); | |
93 is_ok = is_ok && prev_exists && parent_exists && next_exists; | |
94 } | |
95 return is_ok; | |
96 } | |
97 | |
54 } // namespace | 98 } // namespace |
55 | 99 |
56 using std::string; | 100 using std::string; |
57 | 101 |
58 namespace syncable { | 102 namespace syncable { |
59 | 103 |
60 #define ENUM_CASE(x) case x: return #x; break | 104 #define ENUM_CASE(x) case x: return #x; break |
61 | 105 |
62 std::string WriterTagToString(WriterTag writer_tag) { | 106 std::string WriterTagToString(WriterTag writer_tag) { |
63 switch (writer_tag) { | 107 switch (writer_tag) { |
(...skipping 406 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
470 store_ = CreateBackingStore(name, db_path); | 514 store_ = CreateBackingStore(name, db_path); |
471 | 515 |
472 KernelLoadInfo info; | 516 KernelLoadInfo info; |
473 // Temporary indices before kernel_ initialized in case Load fails. We 0(1) | 517 // Temporary indices before kernel_ initialized in case Load fails. We 0(1) |
474 // swap these later. | 518 // swap these later. |
475 MetahandlesIndex metas_bucket; | 519 MetahandlesIndex metas_bucket; |
476 DirOpenResult result = store_->Load(&metas_bucket, &info); | 520 DirOpenResult result = store_->Load(&metas_bucket, &info); |
477 if (OPENED != result) | 521 if (OPENED != result) |
478 return result; | 522 return result; |
479 | 523 |
524 if (!VerifyReferenceIntegrity(metas_bucket)) { | |
tim (not reviewing)
2011/11/14 19:23:44
style nit: file pattern is no { } on single line i
| |
525 return FAILED_LOGICAL_CORRUPTION; | |
526 } | |
527 | |
480 kernel_ = new Kernel(db_path, name, info, delegate); | 528 kernel_ = new Kernel(db_path, name, info, delegate); |
481 kernel_->metahandles_index->swap(metas_bucket); | 529 kernel_->metahandles_index->swap(metas_bucket); |
482 InitializeIndices(); | 530 InitializeIndices(); |
483 return OPENED; | 531 return OPENED; |
484 } | 532 } |
485 | 533 |
486 void Directory::Close() { | 534 void Directory::Close() { |
487 if (store_) | 535 if (store_) |
488 delete store_; | 536 delete store_; |
489 store_ = NULL; | 537 store_ = NULL; |
(...skipping 1529 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
2019 if (entry->ref(NEXT_ID).IsRoot() || | 2067 if (entry->ref(NEXT_ID).IsRoot() || |
2020 entry->ref(NEXT_ID) != entry->ref(PREV_ID)) { | 2068 entry->ref(NEXT_ID) != entry->ref(PREV_ID)) { |
2021 return entry; | 2069 return entry; |
2022 } | 2070 } |
2023 } | 2071 } |
2024 // There were no children in the linked list. | 2072 // There were no children in the linked list. |
2025 return NULL; | 2073 return NULL; |
2026 } | 2074 } |
2027 | 2075 |
2028 } // namespace syncable | 2076 } // namespace syncable |
OLD | NEW |