Chromium Code Reviews| Index: webkit/fileapi/file_system_directory_database.cc |
| diff --git a/webkit/fileapi/file_system_directory_database.cc b/webkit/fileapi/file_system_directory_database.cc |
| index 217b98ffbff74be110ffd358b3425f8b5ed586b2..7699eec75f3bad214efeff59e59d64dcddd1ac50 100644 |
| --- a/webkit/fileapi/file_system_directory_database.cc |
| +++ b/webkit/fileapi/file_system_directory_database.cc |
| @@ -6,12 +6,14 @@ |
| #include <math.h> |
| +#include "base/file_util.h" |
| #include "base/location.h" |
| #include "base/pickle.h" |
| #include "base/string_number_conversions.h" |
| #include "base/string_util.h" |
| #include "base/sys_string_conversions.h" |
| #include "third_party/leveldatabase/src/include/leveldb/iterator.h" |
| +#include "third_party/leveldatabase/src/include/leveldb/status.h" |
| #include "third_party/leveldatabase/src/include/leveldb/write_batch.h" |
| #include "webkit/fileapi/file_system_util.h" |
| @@ -70,6 +72,7 @@ bool FileInfoFromPickle( |
| return false; |
| } |
| +const FilePath::CharType kDirectoryDatabaseName[] = FILE_PATH_LITERAL("Paths"); |
| const char kChildLookupPrefix[] = "CHILD_OF:"; |
| const char kChildLookupSeparator[] = ":"; |
| const char kLastFileIdKey[] = "LAST_FILE_ID"; |
| @@ -117,12 +120,9 @@ FileSystemDirectoryDatabase::FileInfo::FileInfo() : parent_id(0) { |
| FileSystemDirectoryDatabase::FileInfo::~FileInfo() { |
| } |
| -FileSystemDirectoryDatabase::FileSystemDirectoryDatabase(const FilePath& path) { |
| -#if defined(OS_POSIX) |
| - path_ = path.value(); |
| -#elif defined(OS_WIN) |
| - path_ = base::SysWideToUTF8(path.value()); |
| -#endif |
| +FileSystemDirectoryDatabase::FileSystemDirectoryDatabase( |
| + const FilePath& sandbox_directory) |
| + : sandbox_directory_(sandbox_directory) { |
| } |
| FileSystemDirectoryDatabase::~FileSystemDirectoryDatabase() { |
| @@ -130,7 +130,7 @@ FileSystemDirectoryDatabase::~FileSystemDirectoryDatabase() { |
| bool FileSystemDirectoryDatabase::GetChildWithName( |
| FileId parent_id, const FilePath::StringType& name, FileId* child_id) { |
| - if (!Init()) |
| + if (!Init(FAIL_ON_CORRUPTION)) |
| return false; |
| DCHECK(child_id); |
| std::string child_key = GetChildLookupKey(parent_id, name); |
| @@ -171,7 +171,7 @@ bool FileSystemDirectoryDatabase::GetFileWithPath( |
| bool FileSystemDirectoryDatabase::ListChildren( |
| FileId parent_id, std::vector<FileId>* children) { |
| // Check to add later: fail if parent is a file, at least in debug builds. |
| - if (!Init()) |
| + if (!Init(FAIL_ON_CORRUPTION)) |
| return false; |
| DCHECK(children); |
| std::string child_key_prefix = GetChildListingKeyPrefix(parent_id); |
| @@ -194,7 +194,7 @@ bool FileSystemDirectoryDatabase::ListChildren( |
| } |
| bool FileSystemDirectoryDatabase::GetFileInfo(FileId file_id, FileInfo* info) { |
| - if (!Init()) |
| + if (!Init(FAIL_ON_CORRUPTION)) |
| return false; |
| DCHECK(info); |
| std::string file_key = GetFileLookupKey(file_id); |
| @@ -221,7 +221,7 @@ bool FileSystemDirectoryDatabase::GetFileInfo(FileId file_id, FileInfo* info) { |
| bool FileSystemDirectoryDatabase::AddFileInfo( |
| const FileInfo& info, FileId* file_id) { |
| - if (!Init()) |
| + if (!Init(FAIL_ON_CORRUPTION)) |
| return false; |
| DCHECK(file_id); |
| std::string child_key = GetChildLookupKey(info.parent_id, info.name); |
| @@ -263,7 +263,7 @@ bool FileSystemDirectoryDatabase::AddFileInfo( |
| } |
| bool FileSystemDirectoryDatabase::RemoveFileInfo(FileId file_id) { |
| - if (!Init()) |
| + if (!Init(FAIL_ON_CORRUPTION)) |
| return false; |
| leveldb::WriteBatch batch; |
| if (!RemoveFileInfoHelper(file_id, &batch)) |
| @@ -280,7 +280,7 @@ bool FileSystemDirectoryDatabase::UpdateFileInfo( |
| FileId file_id, const FileInfo& new_info) { |
| // TODO: We should also check to see that this doesn't create a loop, but |
| // perhaps only in a debug build. |
| - if (!Init()) |
| + if (!Init(FAIL_ON_CORRUPTION)) |
| return false; |
| DCHECK(file_id); // You can't remove the root, ever. Just delete the DB. |
| FileInfo old_info; |
| @@ -364,7 +364,7 @@ bool FileSystemDirectoryDatabase::OverwritingMoveFile( |
| } |
| bool FileSystemDirectoryDatabase::GetNextInteger(int64* next) { |
| - if (!Init()) |
| + if (!Init(FAIL_ON_CORRUPTION)) |
| return false; |
| DCHECK(next); |
| std::string int_string; |
| @@ -413,20 +413,40 @@ bool FileSystemDirectoryDatabase::DestroyDatabase(const FilePath& path) { |
| return false; |
| } |
| -bool FileSystemDirectoryDatabase::Init() { |
| - if (db_.get()) |
| - return true; |
| - |
| - leveldb::Options options; |
| - options.create_if_missing = true; |
| - leveldb::DB* db; |
| - leveldb::Status status = leveldb::DB::Open(options, path_, &db); |
| - if (status.ok()) { |
| - db_.reset(db); |
| - return true; |
| - } |
| - HandleError(FROM_HERE, status); |
| - return false; |
| +bool FileSystemDirectoryDatabase::Init(RecoveryOption recovery_option) { |
| + if (db_.get()) |
| + return true; |
| + |
| + std::string path; |
| +#if defined(OS_POSIX) |
| + path = sandbox_directory_.Append(kDirectoryDatabaseName).value(); |
| +#elif defined(OS_WIN) |
| + path = base::SysWideToUTF8( |
| + sandbox_directory_.Append(kDirectoryDatabaseName).value()); |
|
kinuko
2012/03/23 04:01:33
Can we use AsUTF8Unsafe() and remove ifdefs?
kinuko
2012/03/23 06:27:21
(We chatted offline) ok this does something slight
tzik
2012/03/23 07:09:00
On linux, it calls SysNativeMBToWide() and WideToU
|
| +#else |
| + NOTREACHED() |
| +#endif |
| + |
| + leveldb::Options options; |
| + options.create_if_missing = true; |
| + leveldb::DB* db; |
| + leveldb::Status status = leveldb::DB::Open(options, path, &db); |
| + // TODO(tzik): Collect status metrics here. |
| + if (status.ok()) { |
| + db_.reset(db); |
| + return true; |
| + } |
| + HandleError(FROM_HERE, status); |
| + |
| + if (recovery_option == FAIL_ON_CORRUPTION) |
| + return false; |
| + |
| + DCHECK_EQ(FAIL_ON_CORRUPTION, recovery_option); |
|
kinuko
2012/03/23 04:01:33
DELETE_ON_CORRUPTION?
tzik
2012/03/23 07:09:00
Done.
|
| + if (!file_util::Delete(sandbox_directory_, true)) |
| + return false; |
| + if (!file_util::CreateDirectory(sandbox_directory_)) |
| + return false; |
| + return Init(FAIL_ON_CORRUPTION); |
| } |
| bool FileSystemDirectoryDatabase::StoreDefaultValues() { |
| @@ -456,7 +476,7 @@ bool FileSystemDirectoryDatabase::StoreDefaultValues() { |
| } |
| bool FileSystemDirectoryDatabase::GetLastFileId(FileId* file_id) { |
| - if (!Init()) |
| + if (!Init(FAIL_ON_CORRUPTION)) |
| return false; |
| DCHECK(file_id); |
| std::string id_string; |