| 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 "components/drive/resource_metadata_storage.h" | 5 #include "components/drive/resource_metadata_storage.h" |
| 6 | 6 |
| 7 #include <stddef.h> | 7 #include <stddef.h> |
| 8 | 8 |
| 9 #include <map> | 9 #include <map> |
| 10 #include <set> | 10 #include <set> |
| (...skipping 256 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 267 | 267 |
| 268 if (!base::PathExists(resource_map_path)) | 268 if (!base::PathExists(resource_map_path)) |
| 269 return false; | 269 return false; |
| 270 | 270 |
| 271 // Open DB. | 271 // Open DB. |
| 272 leveldb::DB* db = NULL; | 272 leveldb::DB* db = NULL; |
| 273 leveldb::Options options; | 273 leveldb::Options options; |
| 274 options.max_open_files = 0; // Use minimum. | 274 options.max_open_files = 0; // Use minimum. |
| 275 options.create_if_missing = false; | 275 options.create_if_missing = false; |
| 276 options.reuse_logs = leveldb_env::kDefaultLogReuseOptionValue; | 276 options.reuse_logs = leveldb_env::kDefaultLogReuseOptionValue; |
| 277 if (!leveldb::DB::Open(options, resource_map_path.AsUTF8Unsafe(), &db).ok()) | 277 if (!leveldb_env::DBRegistry::Open(options, resource_map_path.AsUTF8Unsafe(),
&db).ok()) |
| 278 return false; | 278 return false; |
| 279 std::unique_ptr<leveldb::DB> resource_map(db); | 279 std::unique_ptr<leveldb::DB> resource_map(db); |
| 280 | 280 |
| 281 // Check DB version. | 281 // Check DB version. |
| 282 std::string serialized_header; | 282 std::string serialized_header; |
| 283 ResourceMetadataHeader header; | 283 ResourceMetadataHeader header; |
| 284 if (!resource_map->Get(leveldb::ReadOptions(), | 284 if (!resource_map->Get(leveldb::ReadOptions(), |
| 285 leveldb::Slice(GetHeaderDBKey()), | 285 leveldb::Slice(GetHeaderDBKey()), |
| 286 &serialized_header).ok() || | 286 &serialized_header).ok() || |
| 287 !header.ParseFromString(serialized_header)) | 287 !header.ParseFromString(serialized_header)) |
| (...skipping 266 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 554 // Try to open the existing DB. | 554 // Try to open the existing DB. |
| 555 leveldb::DB* db = NULL; | 555 leveldb::DB* db = NULL; |
| 556 leveldb::Options options; | 556 leveldb::Options options; |
| 557 options.max_open_files = 0; // Use minimum. | 557 options.max_open_files = 0; // Use minimum. |
| 558 options.create_if_missing = false; | 558 options.create_if_missing = false; |
| 559 options.reuse_logs = leveldb_env::kDefaultLogReuseOptionValue; | 559 options.reuse_logs = leveldb_env::kDefaultLogReuseOptionValue; |
| 560 | 560 |
| 561 DBInitStatus open_existing_result = DB_INIT_NOT_FOUND; | 561 DBInitStatus open_existing_result = DB_INIT_NOT_FOUND; |
| 562 leveldb::Status status; | 562 leveldb::Status status; |
| 563 if (base::PathExists(resource_map_path)) { | 563 if (base::PathExists(resource_map_path)) { |
| 564 status = leveldb::DB::Open(options, resource_map_path.AsUTF8Unsafe(), &db); | 564 status = leveldb_env::DBRegistry::Open(options, resource_map_path.AsUTF8Unsa
fe(), &db); |
| 565 open_existing_result = LevelDBStatusToDBInitStatus(status); | 565 open_existing_result = LevelDBStatusToDBInitStatus(status); |
| 566 } | 566 } |
| 567 | 567 |
| 568 if (open_existing_result == DB_INIT_SUCCESS) { | 568 if (open_existing_result == DB_INIT_SUCCESS) { |
| 569 resource_map_.reset(db); | 569 resource_map_.reset(db); |
| 570 | 570 |
| 571 // Check the validity of existing DB. | 571 // Check the validity of existing DB. |
| 572 int db_version = -1; | 572 int db_version = -1; |
| 573 ResourceMetadataHeader header; | 573 ResourceMetadataHeader header; |
| 574 if (GetHeader(&header) == FILE_ERROR_OK) | 574 if (GetHeader(&header) == FILE_ERROR_OK) |
| (...skipping 28 matching lines...) Expand all Loading... |
| 603 // deleted once the new DB creation succeeds, or is restored later in | 603 // deleted once the new DB creation succeeds, or is restored later in |
| 604 // UpgradeOldDB() when the creation fails. | 604 // UpgradeOldDB() when the creation fails. |
| 605 MoveIfPossible(resource_map_path, preserved_resource_map_path); | 605 MoveIfPossible(resource_map_path, preserved_resource_map_path); |
| 606 | 606 |
| 607 // Create DB. | 607 // Create DB. |
| 608 options.max_open_files = 0; // Use minimum. | 608 options.max_open_files = 0; // Use minimum. |
| 609 options.create_if_missing = true; | 609 options.create_if_missing = true; |
| 610 options.error_if_exists = true; | 610 options.error_if_exists = true; |
| 611 options.reuse_logs = leveldb_env::kDefaultLogReuseOptionValue; | 611 options.reuse_logs = leveldb_env::kDefaultLogReuseOptionValue; |
| 612 | 612 |
| 613 status = leveldb::DB::Open(options, resource_map_path.AsUTF8Unsafe(), &db); | 613 status = leveldb_env::DBRegistry::Open(options, resource_map_path.AsUTF8Unsa
fe(), &db); |
| 614 if (status.ok()) { | 614 if (status.ok()) { |
| 615 resource_map_.reset(db); | 615 resource_map_.reset(db); |
| 616 | 616 |
| 617 // Set up header and trash the old DB. | 617 // Set up header and trash the old DB. |
| 618 if (PutHeader(GetDefaultHeaderEntry()) == FILE_ERROR_OK && | 618 if (PutHeader(GetDefaultHeaderEntry()) == FILE_ERROR_OK && |
| 619 MoveIfPossible(preserved_resource_map_path, | 619 MoveIfPossible(preserved_resource_map_path, |
| 620 trashed_resource_map_path)) { | 620 trashed_resource_map_path)) { |
| 621 init_result = open_existing_result == DB_INIT_NOT_FOUND ? | 621 init_result = open_existing_result == DB_INIT_NOT_FOUND ? |
| 622 DB_INIT_CREATED_NEW_DB : DB_INIT_REPLACED_EXISTING_DB_WITH_NEW_DB; | 622 DB_INIT_CREATED_NEW_DB : DB_INIT_REPLACED_EXISTING_DB_WITH_NEW_DB; |
| 623 } else { | 623 } else { |
| (...skipping 46 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 670 // Trashed DB may be broken, repair it first. | 670 // Trashed DB may be broken, repair it first. |
| 671 leveldb::Status status; | 671 leveldb::Status status; |
| 672 status = leveldb::RepairDB(trashed_resource_map_path.AsUTF8Unsafe(), options); | 672 status = leveldb::RepairDB(trashed_resource_map_path.AsUTF8Unsafe(), options); |
| 673 if (!status.ok()) { | 673 if (!status.ok()) { |
| 674 LOG(ERROR) << "Failed to repair trashed DB: " << status.ToString(); | 674 LOG(ERROR) << "Failed to repair trashed DB: " << status.ToString(); |
| 675 return; | 675 return; |
| 676 } | 676 } |
| 677 | 677 |
| 678 // Open it. | 678 // Open it. |
| 679 leveldb::DB* db = NULL; | 679 leveldb::DB* db = NULL; |
| 680 status = leveldb::DB::Open(options, trashed_resource_map_path.AsUTF8Unsafe(), | 680 status = leveldb_env::DBRegistry::Open(options, trashed_resource_map_path.AsUT
F8Unsafe(), |
| 681 &db); | 681 &db); |
| 682 if (!status.ok()) { | 682 if (!status.ok()) { |
| 683 LOG(ERROR) << "Failed to open trashed DB: " << status.ToString(); | 683 LOG(ERROR) << "Failed to open trashed DB: " << status.ToString(); |
| 684 return; | 684 return; |
| 685 } | 685 } |
| 686 std::unique_ptr<leveldb::DB> resource_map(db); | 686 std::unique_ptr<leveldb::DB> resource_map(db); |
| 687 | 687 |
| 688 // Check DB version. | 688 // Check DB version. |
| 689 std::string serialized_header; | 689 std::string serialized_header; |
| 690 ResourceMetadataHeader header; | 690 ResourceMetadataHeader header; |
| (...skipping 388 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1079 RecordCheckValidityFailure( | 1079 RecordCheckValidityFailure( |
| 1080 CHECK_VALIDITY_FAILURE_CHILD_ENTRY_COUNT_MISMATCH); | 1080 CHECK_VALIDITY_FAILURE_CHILD_ENTRY_COUNT_MISMATCH); |
| 1081 return false; | 1081 return false; |
| 1082 } | 1082 } |
| 1083 | 1083 |
| 1084 return true; | 1084 return true; |
| 1085 } | 1085 } |
| 1086 | 1086 |
| 1087 } // namespace internal | 1087 } // namespace internal |
| 1088 } // namespace drive | 1088 } // namespace drive |
| OLD | NEW |