| OLD | NEW |
| 1 // Copyright 2016 The Chromium Authors. All rights reserved. | 1 // Copyright 2016 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 "extensions/browser/value_store/lazy_leveldb.h" | 5 #include "extensions/browser/value_store/lazy_leveldb.h" |
| 6 | 6 |
| 7 #include "base/files/file_util.h" | 7 #include "base/files/file_util.h" |
| 8 #include "base/json/json_reader.h" | 8 #include "base/json/json_reader.h" |
| 9 #include "base/memory/ptr_util.h" | 9 #include "base/memory/ptr_util.h" |
| 10 #include "base/strings/string_util.h" | 10 #include "base/strings/string_util.h" |
| (...skipping 164 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 175 ValueStore::BackingStoreRestoreStatus restore_status = | 175 ValueStore::BackingStoreRestoreStatus restore_status = |
| 176 ValueStore::RESTORE_NONE; | 176 ValueStore::RESTORE_NONE; |
| 177 | 177 |
| 178 leveldb::Options repair_options; | 178 leveldb::Options repair_options; |
| 179 repair_options.create_if_missing = true; | 179 repair_options.create_if_missing = true; |
| 180 repair_options.paranoid_checks = true; | 180 repair_options.paranoid_checks = true; |
| 181 | 181 |
| 182 // RepairDB can drop an unbounded number of leveldb tables (key/value sets). | 182 // RepairDB can drop an unbounded number of leveldb tables (key/value sets). |
| 183 s = leveldb::RepairDB(db_path_.AsUTF8Unsafe(), repair_options); | 183 s = leveldb::RepairDB(db_path_.AsUTF8Unsafe(), repair_options); |
| 184 | 184 |
| 185 leveldb::DB* db = nullptr; | |
| 186 if (s.ok()) { | 185 if (s.ok()) { |
| 187 restore_status = ValueStore::DB_RESTORE_REPAIR_SUCCESS; | 186 restore_status = ValueStore::DB_RESTORE_REPAIR_SUCCESS; |
| 188 s = leveldb::DB::Open(open_options_, db_path_.AsUTF8Unsafe(), &db); | 187 s = leveldb_env::OpenDB(open_options_, db_path_.AsUTF8Unsafe(), &db_); |
| 189 } | 188 } |
| 190 | 189 |
| 191 if (!s.ok()) { | 190 if (!s.ok()) { |
| 192 if (DeleteDbFile()) { | 191 if (DeleteDbFile()) { |
| 193 restore_status = ValueStore::DB_RESTORE_DELETE_SUCCESS; | 192 restore_status = ValueStore::DB_RESTORE_DELETE_SUCCESS; |
| 194 s = leveldb::DB::Open(open_options_, db_path_.AsUTF8Unsafe(), &db); | 193 s = leveldb_env::OpenDB(open_options_, db_path_.AsUTF8Unsafe(), &db_); |
| 195 } else { | 194 } else { |
| 196 restore_status = ValueStore::DB_RESTORE_DELETE_FAILURE; | 195 restore_status = ValueStore::DB_RESTORE_DELETE_FAILURE; |
| 197 } | 196 } |
| 198 } | 197 } |
| 199 | 198 |
| 200 if (s.ok()) | 199 if (!s.ok()) |
| 201 db_.reset(db); | |
| 202 else | |
| 203 db_unrecoverable_ = true; | 200 db_unrecoverable_ = true; |
| 204 | 201 |
| 205 if (s.ok() && key) { | 202 if (s.ok() && key) { |
| 206 s = DeleteValue(db_.get(), *key); | 203 s = DeleteValue(db_.get(), *key); |
| 207 if (s.ok()) { | 204 if (s.ok()) { |
| 208 restore_status = ValueStore::VALUE_RESTORE_DELETE_SUCCESS; | 205 restore_status = ValueStore::VALUE_RESTORE_DELETE_SUCCESS; |
| 209 } else if (s.IsIOError()) { | 206 } else if (s.IsIOError()) { |
| 210 restore_status = ValueStore::VALUE_RESTORE_DELETE_FAILURE; | 207 restore_status = ValueStore::VALUE_RESTORE_DELETE_FAILURE; |
| 211 } else { | 208 } else { |
| 212 db_.reset(db); | 209 db_.reset(); |
| 213 if (!DeleteDbFile()) | 210 if (!DeleteDbFile()) |
| 214 db_unrecoverable_ = true; | 211 db_unrecoverable_ = true; |
| 215 restore_status = ValueStore::DB_RESTORE_DELETE_FAILURE; | 212 restore_status = ValueStore::DB_RESTORE_DELETE_FAILURE; |
| 216 } | 213 } |
| 217 } | 214 } |
| 218 | 215 |
| 219 // Only log for the final and most extreme form of database restoration. | 216 // Only log for the final and most extreme form of database restoration. |
| 220 LogRestoreStatus(restore_status); | 217 LogRestoreStatus(restore_status); |
| 221 | 218 |
| 222 return restore_status; | 219 return restore_status; |
| 223 } | 220 } |
| 224 | 221 |
| 225 ValueStore::Status LazyLevelDb::EnsureDbIsOpen() { | 222 ValueStore::Status LazyLevelDb::EnsureDbIsOpen() { |
| 226 base::ThreadRestrictions::AssertIOAllowed(); | 223 base::ThreadRestrictions::AssertIOAllowed(); |
| 227 | 224 |
| 228 if (db_) | 225 if (db_) |
| 229 return ValueStore::Status(); | 226 return ValueStore::Status(); |
| 230 | 227 |
| 231 if (db_unrecoverable_) { | 228 if (db_unrecoverable_) { |
| 232 return ValueStore::Status(ValueStore::CORRUPTION, | 229 return ValueStore::Status(ValueStore::CORRUPTION, |
| 233 ValueStore::DB_RESTORE_DELETE_FAILURE, | 230 ValueStore::DB_RESTORE_DELETE_FAILURE, |
| 234 "Database corrupted"); | 231 "Database corrupted"); |
| 235 } | 232 } |
| 236 | 233 |
| 237 leveldb::DB* db = nullptr; | |
| 238 leveldb::Status ldb_status = | 234 leveldb::Status ldb_status = |
| 239 leveldb::DB::Open(open_options_, db_path_.AsUTF8Unsafe(), &db); | 235 leveldb_env::OpenDB(open_options_, db_path_.AsUTF8Unsafe(), &db_); |
| 240 open_histogram_->Add(leveldb_env::GetLevelDBStatusUMAValue(ldb_status)); | 236 open_histogram_->Add(leveldb_env::GetLevelDBStatusUMAValue(ldb_status)); |
| 241 ValueStore::Status status = ToValueStoreError(ldb_status); | 237 ValueStore::Status status = ToValueStoreError(ldb_status); |
| 242 if (ldb_status.ok()) { | 238 if (ldb_status.IsCorruption()) { |
| 243 db_.reset(db); | |
| 244 } else if (ldb_status.IsCorruption()) { | |
| 245 status.restore_status = FixCorruption(nullptr); | 239 status.restore_status = FixCorruption(nullptr); |
| 246 if (status.restore_status != ValueStore::DB_RESTORE_DELETE_FAILURE) { | 240 if (status.restore_status != ValueStore::DB_RESTORE_DELETE_FAILURE) { |
| 247 status.code = ValueStore::OK; | 241 status.code = ValueStore::OK; |
| 248 status.message = kRestoredDuringOpen; | 242 status.message = kRestoredDuringOpen; |
| 249 } | 243 } |
| 250 } | 244 } |
| 251 | 245 |
| 252 return status; | 246 return status; |
| 253 } | 247 } |
| 254 | 248 |
| (...skipping 25 matching lines...) Expand all Loading... |
| 280 | 274 |
| 281 ValueStore::Status LazyLevelDb::CreateIterator( | 275 ValueStore::Status LazyLevelDb::CreateIterator( |
| 282 const leveldb::ReadOptions& read_options, | 276 const leveldb::ReadOptions& read_options, |
| 283 std::unique_ptr<leveldb::Iterator>* iterator) { | 277 std::unique_ptr<leveldb::Iterator>* iterator) { |
| 284 ValueStore::Status status = EnsureDbIsOpen(); | 278 ValueStore::Status status = EnsureDbIsOpen(); |
| 285 if (!status.ok()) | 279 if (!status.ok()) |
| 286 return status; | 280 return status; |
| 287 *iterator = base::WrapUnique(db_->NewIterator(read_options)); | 281 *iterator = base::WrapUnique(db_->NewIterator(read_options)); |
| 288 return ValueStore::Status(); | 282 return ValueStore::Status(); |
| 289 } | 283 } |
| OLD | NEW |