| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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 "storage/browser/fileapi/sandbox_origin_database.h" | 5 #include "storage/browser/fileapi/sandbox_origin_database.h" |
| 6 | 6 |
| 7 #include <stdint.h> | 7 #include <stdint.h> |
| 8 | 8 |
| 9 #include <set> | 9 #include <set> |
| 10 #include <utility> | 10 #include <utility> |
| (...skipping 313 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 324 std::string number_string; | 324 std::string number_string; |
| 325 leveldb::Status status = | 325 leveldb::Status status = |
| 326 db_->Get(leveldb::ReadOptions(), LastPathKey(), &number_string); | 326 db_->Get(leveldb::ReadOptions(), LastPathKey(), &number_string); |
| 327 if (status.ok()) | 327 if (status.ok()) |
| 328 return base::StringToInt(number_string, number); | 328 return base::StringToInt(number_string, number); |
| 329 if (!status.IsNotFound()) { | 329 if (!status.IsNotFound()) { |
| 330 HandleError(FROM_HERE, status); | 330 HandleError(FROM_HERE, status); |
| 331 return false; | 331 return false; |
| 332 } | 332 } |
| 333 // Verify that this is a totally new database, and initialize it. | 333 // Verify that this is a totally new database, and initialize it. |
| 334 scoped_ptr<leveldb::Iterator> iter(db_->NewIterator(leveldb::ReadOptions())); | 334 { |
| 335 iter->SeekToFirst(); | 335 // Scope the iterator to ensure it is deleted before database is closed. |
| 336 if (iter->Valid()) { // DB was not empty, but had no last path number! | 336 scoped_ptr<leveldb::Iterator> iter( |
| 337 LOG(ERROR) << "File system origin database is corrupt!"; | 337 db_->NewIterator(leveldb::ReadOptions())); |
| 338 return false; | 338 iter->SeekToFirst(); |
| 339 if (iter->Valid()) { // DB was not empty, but had no last path number! |
| 340 LOG(ERROR) << "File system origin database is corrupt!"; |
| 341 return false; |
| 342 } |
| 339 } | 343 } |
| 340 // This is always the first write into the database. If we ever add a | 344 // This is always the first write into the database. If we ever add a |
| 341 // version number, they should go in in a single transaction. | 345 // version number, they should go in in a single transaction. |
| 342 status = | 346 status = db_->Put(leveldb::WriteOptions(), LastPathKey(), std::string("-1")); |
| 343 db_->Put(leveldb::WriteOptions(), LastPathKey(), std::string("-1")); | |
| 344 if (!status.ok()) { | 347 if (!status.ok()) { |
| 345 HandleError(FROM_HERE, status); | 348 HandleError(FROM_HERE, status); |
| 346 return false; | 349 return false; |
| 347 } | 350 } |
| 348 *number = -1; | 351 *number = -1; |
| 349 return true; | 352 return true; |
| 350 } | 353 } |
| 351 | 354 |
| 352 } // namespace storage | 355 } // namespace storage |
| OLD | NEW |