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

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

Issue 8475017: Check node references during sync DB init (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Respond to review comments Created 9 years, 1 month 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 | Annotate | Revision Log
OLDNEW
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
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 //
72 // This function is "Unsafe" because it does not attempt to acquire any locks
73 // that may be protecting this list that gets passed in. The caller is
74 // responsible for ensuring that no one modifies this list while the function is
75 // running.
76 bool VerifyReferenceIntegrityUnsafe(const syncable::MetahandlesIndex &index) {
77 using namespace syncable;
78 typedef base::hash_set<const Id*, IdPtrHashFunc, IdPtrCompareFunc> IdsSet;
79
80 IdsSet ids_set;
81 bool is_ok = true;
82
83 ids_set.resize(index.size());
84 for (MetahandlesIndex::iterator it = index.begin();
85 it != index.end(); ++it) {
86 EntryKernel* entry = *it;
87 bool is_duplicate_id = !(ids_set.insert(&entry->ref(ID)).second);
88 is_ok = is_ok && !is_duplicate_id;
89 }
90
91 IdsSet::iterator end = ids_set.end();
92 for (MetahandlesIndex::iterator it = index.begin();
93 it != index.end(); ++it) {
94 EntryKernel* entry = *it;
95 bool prev_exists = (ids_set.find(&(entry->ref(PREV_ID))) != end);
96 bool parent_exists = (ids_set.find(&(entry->ref(PARENT_ID))) != end);
97 bool next_exists = (ids_set.find(&(entry->ref(NEXT_ID))) != end);
98 is_ok = is_ok && prev_exists && parent_exists && next_exists;
99 }
100 return is_ok;
101 }
102
54 } // namespace 103 } // namespace
55 104
56 using std::string; 105 using std::string;
57 106
58 namespace syncable { 107 namespace syncable {
59 108
60 #define ENUM_CASE(x) case x: return #x; break 109 #define ENUM_CASE(x) case x: return #x; break
61 110
62 std::string WriterTagToString(WriterTag writer_tag) { 111 std::string WriterTagToString(WriterTag writer_tag) {
63 switch (writer_tag) { 112 switch (writer_tag) {
(...skipping 406 matching lines...) Expand 10 before | Expand all | Expand 10 after
470 store_ = CreateBackingStore(name, db_path); 519 store_ = CreateBackingStore(name, db_path);
471 520
472 KernelLoadInfo info; 521 KernelLoadInfo info;
473 // Temporary indices before kernel_ initialized in case Load fails. We 0(1) 522 // Temporary indices before kernel_ initialized in case Load fails. We 0(1)
474 // swap these later. 523 // swap these later.
475 MetahandlesIndex metas_bucket; 524 MetahandlesIndex metas_bucket;
476 DirOpenResult result = store_->Load(&metas_bucket, &info); 525 DirOpenResult result = store_->Load(&metas_bucket, &info);
477 if (OPENED != result) 526 if (OPENED != result)
478 return result; 527 return result;
479 528
529 if (!VerifyReferenceIntegrityUnsafe(metas_bucket)) {
530 return FAILED_LOGICAL_CORRUPTION;
531 }
532
480 kernel_ = new Kernel(db_path, name, info, delegate); 533 kernel_ = new Kernel(db_path, name, info, delegate);
481 kernel_->metahandles_index->swap(metas_bucket); 534 kernel_->metahandles_index->swap(metas_bucket);
482 InitializeIndices(); 535 InitializeIndices();
483 return OPENED; 536 return OPENED;
484 } 537 }
485 538
486 void Directory::Close() { 539 void Directory::Close() {
487 if (store_) 540 if (store_)
488 delete store_; 541 delete store_;
489 store_ = NULL; 542 store_ = NULL;
(...skipping 1529 matching lines...) Expand 10 before | Expand all | Expand 10 after
2019 if (entry->ref(NEXT_ID).IsRoot() || 2072 if (entry->ref(NEXT_ID).IsRoot() ||
2020 entry->ref(NEXT_ID) != entry->ref(PREV_ID)) { 2073 entry->ref(NEXT_ID) != entry->ref(PREV_ID)) {
2021 return entry; 2074 return entry;
2022 } 2075 }
2023 } 2076 }
2024 // There were no children in the linked list. 2077 // There were no children in the linked list.
2025 return NULL; 2078 return NULL;
2026 } 2079 }
2027 2080
2028 } // namespace syncable 2081 } // namespace syncable
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698