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 393 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
457 } | 494 } |
458 | 495 |
459 DirectoryBackingStore* Directory::CreateBackingStore( | 496 DirectoryBackingStore* Directory::CreateBackingStore( |
460 const string& dir_name, const FilePath& backing_filepath) { | 497 const string& dir_name, const FilePath& backing_filepath) { |
461 return new DirectoryBackingStore(dir_name, backing_filepath); | 498 return new DirectoryBackingStore(dir_name, backing_filepath); |
462 } | 499 } |
463 | 500 |
464 DirOpenResult Directory::OpenImpl(const FilePath& file_path, | 501 DirOpenResult Directory::OpenImpl(const FilePath& file_path, |
465 const string& name, | 502 const string& name, |
466 DirectoryChangeDelegate* delegate) { | 503 DirectoryChangeDelegate* delegate) { |
| 504 TRACE_EVENT0("sync", "SyncDatabaseOpen"); |
467 DCHECK_EQ(static_cast<DirectoryBackingStore*>(NULL), store_); | 505 DCHECK_EQ(static_cast<DirectoryBackingStore*>(NULL), store_); |
468 FilePath db_path(file_path); | 506 FilePath db_path(file_path); |
469 file_util::AbsolutePath(&db_path); | 507 file_util::AbsolutePath(&db_path); |
470 store_ = CreateBackingStore(name, db_path); | 508 store_ = CreateBackingStore(name, db_path); |
471 | 509 |
472 KernelLoadInfo info; | 510 KernelLoadInfo info; |
473 // Temporary indices before kernel_ initialized in case Load fails. We 0(1) | 511 // Temporary indices before kernel_ initialized in case Load fails. We 0(1) |
474 // swap these later. | 512 // swap these later. |
475 MetahandlesIndex metas_bucket; | 513 MetahandlesIndex metas_bucket; |
476 DirOpenResult result = store_->Load(&metas_bucket, &info); | 514 DirOpenResult result = store_->Load(&metas_bucket, &info); |
477 if (OPENED != result) | 515 if (OPENED != result) |
478 return result; | 516 return result; |
479 | 517 |
| 518 if (!VerifyReferenceIntegrityUnsafe(metas_bucket)) |
| 519 return FAILED_LOGICAL_CORRUPTION; |
| 520 |
480 kernel_ = new Kernel(db_path, name, info, delegate); | 521 kernel_ = new Kernel(db_path, name, info, delegate); |
481 kernel_->metahandles_index->swap(metas_bucket); | 522 kernel_->metahandles_index->swap(metas_bucket); |
482 InitializeIndices(); | 523 InitializeIndices(); |
483 return OPENED; | 524 return OPENED; |
484 } | 525 } |
485 | 526 |
486 void Directory::Close() { | 527 void Directory::Close() { |
487 if (store_) | 528 if (store_) |
488 delete store_; | 529 delete store_; |
489 store_ = NULL; | 530 store_ = NULL; |
(...skipping 1488 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1978 CHECK(result); | 2019 CHECK(result); |
1979 for (iterator i = GetParentChildIndexLowerBound(lock, parent_id), | 2020 for (iterator i = GetParentChildIndexLowerBound(lock, parent_id), |
1980 end = GetParentChildIndexUpperBound(lock, parent_id); | 2021 end = GetParentChildIndexUpperBound(lock, parent_id); |
1981 i != end; ++i) { | 2022 i != end; ++i) { |
1982 DCHECK_EQ(parent_id, (*i)->ref(PARENT_ID)); | 2023 DCHECK_EQ(parent_id, (*i)->ref(PARENT_ID)); |
1983 result->push_back((*i)->ref(META_HANDLE)); | 2024 result->push_back((*i)->ref(META_HANDLE)); |
1984 } | 2025 } |
1985 } | 2026 } |
1986 | 2027 |
1987 } // namespace syncable | 2028 } // namespace syncable |
OLD | NEW |