| OLD | NEW |
| 1 // Copyright 2013 The Chromium Authors. All rights reserved. | 1 // Copyright 2013 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/chromeos/drive/resource_metadata_storage.h" | 5 #include "chrome/browser/chromeos/drive/resource_metadata_storage.h" |
| 6 | 6 |
| 7 #include <map> | 7 #include <map> |
| 8 #include <set> | 8 #include <set> |
| 9 | 9 |
| 10 #include "base/bind.h" | 10 #include "base/bind.h" |
| (...skipping 249 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 260 } | 260 } |
| 261 | 261 |
| 262 if (!base::PathExists(resource_map_path)) | 262 if (!base::PathExists(resource_map_path)) |
| 263 return false; | 263 return false; |
| 264 | 264 |
| 265 // Open DB. | 265 // Open DB. |
| 266 leveldb::DB* db = NULL; | 266 leveldb::DB* db = NULL; |
| 267 leveldb::Options options; | 267 leveldb::Options options; |
| 268 options.max_open_files = 0; // Use minimum. | 268 options.max_open_files = 0; // Use minimum. |
| 269 options.create_if_missing = false; | 269 options.create_if_missing = false; |
| 270 options.reuse_logs = leveldb_env::kDefaultLogReuseOptionValue; |
| 270 if (!leveldb::DB::Open(options, resource_map_path.AsUTF8Unsafe(), &db).ok()) | 271 if (!leveldb::DB::Open(options, resource_map_path.AsUTF8Unsafe(), &db).ok()) |
| 271 return false; | 272 return false; |
| 272 scoped_ptr<leveldb::DB> resource_map(db); | 273 scoped_ptr<leveldb::DB> resource_map(db); |
| 273 | 274 |
| 274 // Check DB version. | 275 // Check DB version. |
| 275 std::string serialized_header; | 276 std::string serialized_header; |
| 276 ResourceMetadataHeader header; | 277 ResourceMetadataHeader header; |
| 277 if (!resource_map->Get(leveldb::ReadOptions(), | 278 if (!resource_map->Get(leveldb::ReadOptions(), |
| 278 leveldb::Slice(GetHeaderDBKey()), | 279 leveldb::Slice(GetHeaderDBKey()), |
| 279 &serialized_header).ok() || | 280 &serialized_header).ok() || |
| (...skipping 262 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 542 !base::DeleteFile(trashed_resource_map_path, true /* recursive */)) { | 543 !base::DeleteFile(trashed_resource_map_path, true /* recursive */)) { |
| 543 LOG(ERROR) << "Failed to remove unneeded DBs."; | 544 LOG(ERROR) << "Failed to remove unneeded DBs."; |
| 544 return false; | 545 return false; |
| 545 } | 546 } |
| 546 | 547 |
| 547 // Try to open the existing DB. | 548 // Try to open the existing DB. |
| 548 leveldb::DB* db = NULL; | 549 leveldb::DB* db = NULL; |
| 549 leveldb::Options options; | 550 leveldb::Options options; |
| 550 options.max_open_files = 0; // Use minimum. | 551 options.max_open_files = 0; // Use minimum. |
| 551 options.create_if_missing = false; | 552 options.create_if_missing = false; |
| 553 options.reuse_logs = leveldb_env::kDefaultLogReuseOptionValue; |
| 552 | 554 |
| 553 DBInitStatus open_existing_result = DB_INIT_NOT_FOUND; | 555 DBInitStatus open_existing_result = DB_INIT_NOT_FOUND; |
| 554 leveldb::Status status; | 556 leveldb::Status status; |
| 555 if (base::PathExists(resource_map_path)) { | 557 if (base::PathExists(resource_map_path)) { |
| 556 status = leveldb::DB::Open(options, resource_map_path.AsUTF8Unsafe(), &db); | 558 status = leveldb::DB::Open(options, resource_map_path.AsUTF8Unsafe(), &db); |
| 557 open_existing_result = LevelDBStatusToDBInitStatus(status); | 559 open_existing_result = LevelDBStatusToDBInitStatus(status); |
| 558 } | 560 } |
| 559 | 561 |
| 560 if (open_existing_result == DB_INIT_SUCCESS) { | 562 if (open_existing_result == DB_INIT_SUCCESS) { |
| 561 resource_map_.reset(db); | 563 resource_map_.reset(db); |
| (...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 593 if (!resource_map_) { | 595 if (!resource_map_) { |
| 594 // Move the existing DB to the preservation path. The moved old DB is | 596 // Move the existing DB to the preservation path. The moved old DB is |
| 595 // deleted once the new DB creation succeeds, or is restored later in | 597 // deleted once the new DB creation succeeds, or is restored later in |
| 596 // UpgradeOldDB() when the creation fails. | 598 // UpgradeOldDB() when the creation fails. |
| 597 MoveIfPossible(resource_map_path, preserved_resource_map_path); | 599 MoveIfPossible(resource_map_path, preserved_resource_map_path); |
| 598 | 600 |
| 599 // Create DB. | 601 // Create DB. |
| 600 options.max_open_files = 0; // Use minimum. | 602 options.max_open_files = 0; // Use minimum. |
| 601 options.create_if_missing = true; | 603 options.create_if_missing = true; |
| 602 options.error_if_exists = true; | 604 options.error_if_exists = true; |
| 605 options.reuse_logs = leveldb_env::kDefaultLogReuseOptionValue; |
| 603 | 606 |
| 604 status = leveldb::DB::Open(options, resource_map_path.AsUTF8Unsafe(), &db); | 607 status = leveldb::DB::Open(options, resource_map_path.AsUTF8Unsafe(), &db); |
| 605 if (status.ok()) { | 608 if (status.ok()) { |
| 606 resource_map_.reset(db); | 609 resource_map_.reset(db); |
| 607 | 610 |
| 608 // Set up header and trash the old DB. | 611 // Set up header and trash the old DB. |
| 609 if (PutHeader(GetDefaultHeaderEntry()) == FILE_ERROR_OK && | 612 if (PutHeader(GetDefaultHeaderEntry()) == FILE_ERROR_OK && |
| 610 MoveIfPossible(preserved_resource_map_path, | 613 MoveIfPossible(preserved_resource_map_path, |
| 611 trashed_resource_map_path)) { | 614 trashed_resource_map_path)) { |
| 612 init_result = open_existing_result == DB_INIT_NOT_FOUND ? | 615 init_result = open_existing_result == DB_INIT_NOT_FOUND ? |
| (...skipping 18 matching lines...) Expand all Loading... |
| 631 RecoveredCacheInfoMap* out_info) { | 634 RecoveredCacheInfoMap* out_info) { |
| 632 const base::FilePath trashed_resource_map_path = | 635 const base::FilePath trashed_resource_map_path = |
| 633 directory_path_.Append(kTrashedResourceMapDBName); | 636 directory_path_.Append(kTrashedResourceMapDBName); |
| 634 | 637 |
| 635 if (!base::PathExists(trashed_resource_map_path)) | 638 if (!base::PathExists(trashed_resource_map_path)) |
| 636 return; | 639 return; |
| 637 | 640 |
| 638 leveldb::Options options; | 641 leveldb::Options options; |
| 639 options.max_open_files = 0; // Use minimum. | 642 options.max_open_files = 0; // Use minimum. |
| 640 options.create_if_missing = false; | 643 options.create_if_missing = false; |
| 644 options.reuse_logs = leveldb_env::kDefaultLogReuseOptionValue; |
| 641 | 645 |
| 642 // Trashed DB may be broken, repair it first. | 646 // Trashed DB may be broken, repair it first. |
| 643 leveldb::Status status; | 647 leveldb::Status status; |
| 644 status = leveldb::RepairDB(trashed_resource_map_path.AsUTF8Unsafe(), options); | 648 status = leveldb::RepairDB(trashed_resource_map_path.AsUTF8Unsafe(), options); |
| 645 if (!status.ok()) { | 649 if (!status.ok()) { |
| 646 LOG(ERROR) << "Failed to repair trashed DB: " << status.ToString(); | 650 LOG(ERROR) << "Failed to repair trashed DB: " << status.ToString(); |
| 647 return; | 651 return; |
| 648 } | 652 } |
| 649 | 653 |
| 650 // Open it. | 654 // Open it. |
| (...skipping 400 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1051 RecordCheckValidityFailure( | 1055 RecordCheckValidityFailure( |
| 1052 CHECK_VALIDITY_FAILURE_CHILD_ENTRY_COUNT_MISMATCH); | 1056 CHECK_VALIDITY_FAILURE_CHILD_ENTRY_COUNT_MISMATCH); |
| 1053 return false; | 1057 return false; |
| 1054 } | 1058 } |
| 1055 | 1059 |
| 1056 return true; | 1060 return true; |
| 1057 } | 1061 } |
| 1058 | 1062 |
| 1059 } // namespace internal | 1063 } // namespace internal |
| 1060 } // namespace drive | 1064 } // namespace drive |
| OLD | NEW |