Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(367)

Unified Diff: webkit/fileapi/file_system_directory_database.cc

Issue 9663021: Add database recovery for FileSystemOriginDatabase (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: rebase Created 8 years, 9 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
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 db95d636e1b08e2fc44dd58dcceea0701376c56f..6eb1df9567d42a6126b7af8d932938d048af22a5 100644
--- a/webkit/fileapi/file_system_directory_database.cc
+++ b/webkit/fileapi/file_system_directory_database.cc
@@ -6,12 +6,12 @@
#include <math.h>
+#include "base/file_util.h"
#include "base/location.h"
#include "base/metrics/histogram.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/db.h"
#include "third_party/leveldatabase/src/include/leveldb/write_batch.h"
#include "webkit/fileapi/file_system_util.h"
@@ -28,13 +28,9 @@ bool PickleFromFileInfo(
base::Time::FromDoubleT(floor(info.modification_time.ToDoubleT()));
std::string name;
-#if defined(OS_POSIX)
- data_path = info.data_path.value();
- name = info.name;
-#elif defined(OS_WIN)
- data_path = base::SysWideToUTF8(info.data_path.value());
- name = base::SysWideToUTF8(info.name);
-#endif
+ data_path = fileapi::FilePathToString(info.data_path);
+ name = fileapi::FilePathToString(FilePath(info.name));
+
if (pickle->WriteInt64(info.parent_id) &&
pickle->WriteString(data_path) &&
pickle->WriteString(name) &&
@@ -57,13 +53,9 @@ bool FileInfoFromPickle(
pickle.ReadString(&iter, &data_path) &&
pickle.ReadString(&iter, &name) &&
pickle.ReadInt64(&iter, &internal_time)) {
-#if defined(OS_POSIX)
- info->data_path = FilePath(data_path);
- info->name = name;
-#elif defined(OS_WIN)
- info->data_path = FilePath(base::SysUTF8ToWide(data_path));
- info->name = base::SysUTF8ToWide(name);
-#endif
+
+ info->data_path = fileapi::StringToFilePath(data_path);
+ info->name = fileapi::StringToFilePath(name).value();
info->modification_time = base::Time::FromInternalValue(internal_time);
return true;
}
@@ -71,6 +63,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";
@@ -88,11 +81,7 @@ std::string GetChildLookupKey(
fileapi::FileSystemDirectoryDatabase::FileId parent_id,
const FilePath::StringType& child_name) {
std::string name;
-#if defined(OS_POSIX)
- name = child_name;
-#elif defined(OS_WIN)
- name = base::SysWideToUTF8(child_name);
-#endif
+ name = fileapi::FilePathToString(FilePath(child_name));
return std::string(kChildLookupPrefix) + base::Int64ToString(parent_id) +
std::string(kChildLookupSeparator) + name;
}
@@ -126,12 +115,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& filesystem_data_directory)
+ : filesystem_data_directory_(filesystem_data_directory) {
}
FileSystemDirectoryDatabase::~FileSystemDirectoryDatabase() {
@@ -139,7 +125,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);
@@ -180,7 +166,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);
@@ -203,7 +189,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);
@@ -230,7 +216,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);
@@ -272,7 +258,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))
@@ -289,7 +275,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;
@@ -373,7 +359,7 @@ bool FileSystemDirectoryDatabase::OverwritingMoveFile(
}
bool FileSystemDirectoryDatabase::GetNextInteger(int64* next) {
- if (!Init())
+ if (!Init(FAIL_ON_CORRUPTION))
return false;
DCHECK(next);
std::string int_string;
@@ -408,12 +394,7 @@ bool FileSystemDirectoryDatabase::GetNextInteger(int64* next) {
// static
bool FileSystemDirectoryDatabase::DestroyDatabase(const FilePath& path) {
- std::string name;
-#if defined(OS_POSIX)
- name = path.value();
-#elif defined(OS_WIN)
- name = base::SysWideToUTF8(path.value());
-#endif
+ std::string name = FilePathToString(path.Append(kDirectoryDatabaseName));
leveldb::Status status = leveldb::DestroyDB(name, leveldb::Options());
if (status.ok())
return true;
@@ -422,21 +403,33 @@ 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);
- ReportInitStatus(status);
- 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 =
+ FilePathToString(filesystem_data_directory_.Append(
+ kDirectoryDatabaseName));
+ leveldb::Options options;
+ options.create_if_missing = true;
+ leveldb::DB* db;
+ leveldb::Status status = leveldb::DB::Open(options, path, &db);
+ ReportInitStatus(status);
+ if (status.ok()) {
+ db_.reset(db);
+ return true;
+ }
+ HandleError(FROM_HERE, status);
+
+ if (recovery_option == FAIL_ON_CORRUPTION)
+ return false;
+
+ DCHECK_EQ(DELETE_ON_CORRUPTION, recovery_option);
+ if (!file_util::Delete(filesystem_data_directory_, true))
+ return false;
+ if (!file_util::CreateDirectory(filesystem_data_directory_))
+ return false;
+ return Init(FAIL_ON_CORRUPTION);
}
void FileSystemDirectoryDatabase::ReportInitStatus(
@@ -484,7 +477,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;
« no previous file with comments | « webkit/fileapi/file_system_directory_database.h ('k') | webkit/fileapi/file_system_directory_database_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698