| 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 251 matching lines...) Expand 10 before | Expand all | Expand 10 after  Loading... | 
|   262     // be successful. Discard the imperfect new DB and restore the old DB. |   262     // be successful. Discard the imperfect new DB and restore the old DB. | 
|   263     if (!base::DeleteFile(resource_map_path, false /* recursive */) || |   263     if (!base::DeleteFile(resource_map_path, false /* recursive */) || | 
|   264         !base::Move(preserved_resource_map_path, resource_map_path)) |   264         !base::Move(preserved_resource_map_path, resource_map_path)) | 
|   265       return false; |   265       return false; | 
|   266   } |   266   } | 
|   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   std::unique_ptr<leveldb::DB> resource_map; | 
|   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   leveldb::Status status = leveldb_env::OpenDB( | 
 |   278       options, resource_map_path.AsUTF8Unsafe(), &resource_map); | 
 |   279   if (!status.ok()) | 
|   278     return false; |   280     return false; | 
|   279   std::unique_ptr<leveldb::DB> resource_map(db); |  | 
|   280  |   281  | 
|   281   // Check DB version. |   282   // Check DB version. | 
|   282   std::string serialized_header; |   283   std::string serialized_header; | 
|   283   ResourceMetadataHeader header; |   284   ResourceMetadataHeader header; | 
|   284   if (!resource_map->Get(leveldb::ReadOptions(), |   285   if (!resource_map->Get(leveldb::ReadOptions(), | 
|   285                          leveldb::Slice(GetHeaderDBKey()), |   286                          leveldb::Slice(GetHeaderDBKey()), | 
|   286                          &serialized_header).ok() || |   287                          &serialized_header).ok() || | 
|   287       !header.ParseFromString(serialized_header)) |   288       !header.ParseFromString(serialized_header)) | 
|   288     return false; |   289     return false; | 
|   289   UMA_HISTOGRAM_SPARSE_SLOWLY("Drive.MetadataDBVersionBeforeUpgradeCheck", |   290   UMA_HISTOGRAM_SPARSE_SLOWLY("Drive.MetadataDBVersionBeforeUpgradeCheck", | 
| (...skipping 255 matching lines...) Expand 10 before | Expand all | Expand 10 after  Loading... | 
|   545       directory_path_.Append(kTrashedResourceMapDBName); |   546       directory_path_.Append(kTrashedResourceMapDBName); | 
|   546  |   547  | 
|   547   // Discard unneeded DBs. |   548   // Discard unneeded DBs. | 
|   548   if (!base::DeleteFile(preserved_resource_map_path, true /* recursive */) || |   549   if (!base::DeleteFile(preserved_resource_map_path, true /* recursive */) || | 
|   549       !base::DeleteFile(trashed_resource_map_path, true /* recursive */)) { |   550       !base::DeleteFile(trashed_resource_map_path, true /* recursive */)) { | 
|   550     LOG(ERROR) << "Failed to remove unneeded DBs."; |   551     LOG(ERROR) << "Failed to remove unneeded DBs."; | 
|   551     return false; |   552     return false; | 
|   552   } |   553   } | 
|   553  |   554  | 
|   554   // Try to open the existing DB. |   555   // Try to open the existing DB. | 
|   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::OpenDB(options, resource_map_path.AsUTF8Unsafe(), | 
 |   565                                  &resource_map_); | 
|   565     open_existing_result = LevelDBStatusToDBInitStatus(status); |   566     open_existing_result = LevelDBStatusToDBInitStatus(status); | 
|   566   } |   567   } | 
|   567  |   568  | 
|   568   if (open_existing_result == DB_INIT_SUCCESS) { |   569   if (open_existing_result == DB_INIT_SUCCESS) { | 
|   569     resource_map_.reset(db); |  | 
|   570  |  | 
|   571     // Check the validity of existing DB. |   570     // Check the validity of existing DB. | 
|   572     int db_version = -1; |   571     int db_version = -1; | 
|   573     ResourceMetadataHeader header; |   572     ResourceMetadataHeader header; | 
|   574     if (GetHeader(&header) == FILE_ERROR_OK) |   573     if (GetHeader(&header) == FILE_ERROR_OK) | 
|   575       db_version = header.version(); |   574       db_version = header.version(); | 
|   576  |   575  | 
|   577     bool should_discard_db = true; |   576     bool should_discard_db = true; | 
|   578     if (db_version != kDBVersion) { |   577     if (db_version != kDBVersion) { | 
|   579       open_existing_result = DB_INIT_INCOMPATIBLE; |   578       open_existing_result = DB_INIT_INCOMPATIBLE; | 
|   580       DVLOG(1) << "Reject incompatible DB."; |   579       DVLOG(1) << "Reject incompatible DB."; | 
| (...skipping 22 matching lines...) Expand all  Loading... | 
|   603     // deleted once the new DB creation succeeds, or is restored later in |   602     // deleted once the new DB creation succeeds, or is restored later in | 
|   604     // UpgradeOldDB() when the creation fails. |   603     // UpgradeOldDB() when the creation fails. | 
|   605     MoveIfPossible(resource_map_path, preserved_resource_map_path); |   604     MoveIfPossible(resource_map_path, preserved_resource_map_path); | 
|   606  |   605  | 
|   607     // Create DB. |   606     // Create DB. | 
|   608     options.max_open_files = 0;  // Use minimum. |   607     options.max_open_files = 0;  // Use minimum. | 
|   609     options.create_if_missing = true; |   608     options.create_if_missing = true; | 
|   610     options.error_if_exists = true; |   609     options.error_if_exists = true; | 
|   611     options.reuse_logs = leveldb_env::kDefaultLogReuseOptionValue; |   610     options.reuse_logs = leveldb_env::kDefaultLogReuseOptionValue; | 
|   612  |   611  | 
|   613     status = leveldb::DB::Open(options, resource_map_path.AsUTF8Unsafe(), &db); |   612     status = leveldb_env::OpenDB(options, resource_map_path.AsUTF8Unsafe(), | 
 |   613                                  &resource_map_); | 
|   614     if (status.ok()) { |   614     if (status.ok()) { | 
|   615       resource_map_.reset(db); |  | 
|   616  |  | 
|   617       // Set up header and trash the old DB. |   615       // Set up header and trash the old DB. | 
|   618       if (PutHeader(GetDefaultHeaderEntry()) == FILE_ERROR_OK && |   616       if (PutHeader(GetDefaultHeaderEntry()) == FILE_ERROR_OK && | 
|   619           MoveIfPossible(preserved_resource_map_path, |   617           MoveIfPossible(preserved_resource_map_path, | 
|   620                          trashed_resource_map_path)) { |   618                          trashed_resource_map_path)) { | 
|   621         init_result = open_existing_result == DB_INIT_NOT_FOUND ? |   619         init_result = open_existing_result == DB_INIT_NOT_FOUND ? | 
|   622             DB_INIT_CREATED_NEW_DB : DB_INIT_REPLACED_EXISTING_DB_WITH_NEW_DB; |   620             DB_INIT_CREATED_NEW_DB : DB_INIT_REPLACED_EXISTING_DB_WITH_NEW_DB; | 
|   623       } else { |   621       } else { | 
|   624         init_result = DB_INIT_FAILED; |   622         init_result = DB_INIT_FAILED; | 
|   625         resource_map_.reset(); |   623         resource_map_.reset(); | 
|   626       } |   624       } | 
| (...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after  Loading... | 
|   669  |   667  | 
|   670   // Trashed DB may be broken, repair it first. |   668   // Trashed DB may be broken, repair it first. | 
|   671   leveldb::Status status; |   669   leveldb::Status status; | 
|   672   status = leveldb::RepairDB(trashed_resource_map_path.AsUTF8Unsafe(), options); |   670   status = leveldb::RepairDB(trashed_resource_map_path.AsUTF8Unsafe(), options); | 
|   673   if (!status.ok()) { |   671   if (!status.ok()) { | 
|   674     LOG(ERROR) << "Failed to repair trashed DB: " << status.ToString(); |   672     LOG(ERROR) << "Failed to repair trashed DB: " << status.ToString(); | 
|   675     return; |   673     return; | 
|   676   } |   674   } | 
|   677  |   675  | 
|   678   // Open it. |   676   // Open it. | 
|   679   leveldb::DB* db = NULL; |   677   std::unique_ptr<leveldb::DB> resource_map; | 
|   680   status = leveldb::DB::Open(options, trashed_resource_map_path.AsUTF8Unsafe(), |   678   status = leveldb_env::OpenDB( | 
|   681                              &db); |   679       options, trashed_resource_map_path.AsUTF8Unsafe(), &resource_map); | 
|   682   if (!status.ok()) { |   680   if (!status.ok()) { | 
|   683     LOG(ERROR) << "Failed to open trashed DB: " << status.ToString(); |   681     LOG(ERROR) << "Failed to open trashed DB: " << status.ToString(); | 
|   684     return; |   682     return; | 
|   685   } |   683   } | 
|   686   std::unique_ptr<leveldb::DB> resource_map(db); |  | 
|   687  |   684  | 
|   688   // Check DB version. |   685   // Check DB version. | 
|   689   std::string serialized_header; |   686   std::string serialized_header; | 
|   690   ResourceMetadataHeader header; |   687   ResourceMetadataHeader header; | 
|   691   if (!resource_map->Get(leveldb::ReadOptions(), |   688   if (!resource_map->Get(leveldb::ReadOptions(), | 
|   692                          leveldb::Slice(GetHeaderDBKey()), |   689                          leveldb::Slice(GetHeaderDBKey()), | 
|   693                          &serialized_header).ok() || |   690                          &serialized_header).ok() || | 
|   694       !header.ParseFromString(serialized_header) || |   691       !header.ParseFromString(serialized_header) || | 
|   695       header.version() != kDBVersion) { |   692       header.version() != kDBVersion) { | 
|   696     LOG(ERROR) << "Incompatible DB version: " << header.version(); |   693     LOG(ERROR) << "Incompatible DB version: " << header.version(); | 
| (...skipping 382 matching lines...) Expand 10 before | Expand all | Expand 10 after  Loading... | 
|  1079     RecordCheckValidityFailure( |  1076     RecordCheckValidityFailure( | 
|  1080         CHECK_VALIDITY_FAILURE_CHILD_ENTRY_COUNT_MISMATCH); |  1077         CHECK_VALIDITY_FAILURE_CHILD_ENTRY_COUNT_MISMATCH); | 
|  1081     return false; |  1078     return false; | 
|  1082   } |  1079   } | 
|  1083  |  1080  | 
|  1084   return true; |  1081   return true; | 
|  1085 } |  1082 } | 
|  1086  |  1083  | 
|  1087 }  // namespace internal |  1084 }  // namespace internal | 
|  1088 }  // namespace drive |  1085 }  // namespace drive | 
| OLD | NEW |