Commit 480fc059 authored by backer@chromium.org's avatar backer@chromium.org

Revert 95973 - Handle inconsistency between DB and Files in ObfuscatedFileSystemFileUtil

BUG=91328
TEST='ObfuscatedFileSystemFileUtilTest.*'

Review URL: http://codereview.chromium.org/7540022

TBR=tzik@chromium.org
Review URL: http://codereview.chromium.org/7600022

git-svn-id: svn://svn.chromium.org/chrome/trunk/src@95984 0039d316-1c4b-4281-b951-d872f2087c98
parent 64ffdc73
...@@ -79,7 +79,7 @@ PlatformFileError ObfuscatedFileSystemFileUtil::CreateOrOpen( ...@@ -79,7 +79,7 @@ PlatformFileError ObfuscatedFileSystemFileUtil::CreateOrOpen(
return base::PLATFORM_FILE_ERROR_FAILED; return base::PLATFORM_FILE_ERROR_FAILED;
FileId file_id; FileId file_id;
if (!db->GetFileWithPath(virtual_path, &file_id)) { if (!db->GetFileWithPath(virtual_path, &file_id)) {
// The file doesn't exist in the database. // The file doesn't exist.
if (!(file_flags & (base::PLATFORM_FILE_CREATE | if (!(file_flags & (base::PLATFORM_FILE_CREATE |
base::PLATFORM_FILE_CREATE_ALWAYS | base::PLATFORM_FILE_OPEN_ALWAYS))) base::PLATFORM_FILE_CREATE_ALWAYS | base::PLATFORM_FILE_OPEN_ALWAYS)))
return base::PLATFORM_FILE_ERROR_NOT_FOUND; return base::PLATFORM_FILE_ERROR_NOT_FOUND;
...@@ -88,20 +88,9 @@ PlatformFileError ObfuscatedFileSystemFileUtil::CreateOrOpen( ...@@ -88,20 +88,9 @@ PlatformFileError ObfuscatedFileSystemFileUtil::CreateOrOpen(
return base::PLATFORM_FILE_ERROR_NOT_FOUND; return base::PLATFORM_FILE_ERROR_NOT_FOUND;
FileInfo file_info; FileInfo file_info;
InitFileInfo(&file_info, parent_id, virtual_path.BaseName().value()); InitFileInfo(&file_info, parent_id, virtual_path.BaseName().value());
// There may be a file without database entry out of inconsistency between
// actual filesystem and database. We use PLATFORM_FILE_CREATE_ALWAYS
// to truncate such file.
file_flags &= ~(base::PLATFORM_FILE_OPEN |
base::PLATFORM_FILE_CREATE |
base::PLATFORM_FILE_OPEN_ALWAYS |
base::PLATFORM_FILE_OPEN_TRUNCATED);
file_flags |= base::PLATFORM_FILE_CREATE_ALWAYS;
PlatformFileError error = CreateFile( PlatformFileError error = CreateFile(
context, context->src_origin_url(), context->src_type(), FilePath(), context, context->src_origin_url(), context->src_type(), FilePath(),
&file_info, file_flags, file_handle); &file_info, file_flags, file_handle);
if (error == base::PLATFORM_FILE_ERROR_EXISTS)
error = base::PLATFORM_FILE_OK;
if (created && base::PLATFORM_FILE_OK == error) if (created && base::PLATFORM_FILE_OK == error)
*created = true; *created = true;
return error; return error;
...@@ -806,21 +795,6 @@ PlatformFileError ObfuscatedFileSystemFileUtil::CreateFile( ...@@ -806,21 +795,6 @@ PlatformFileError ObfuscatedFileSystemFileUtil::CreateFile(
DCHECK(!file_flags); // file_flags is only used by CreateOrOpen. DCHECK(!file_flags); // file_flags is only used by CreateOrOpen.
error = underlying_file_util_->EnsureFileExists( error = underlying_file_util_->EnsureFileExists(
context, path, &created); context, path, &created);
// If the file already exists, we found a stray file out of
// inconsistency between database and filesystem. We should truncate
// it to empty.
if (error == base::PLATFORM_FILE_OK && !created) {
PlatformFile file = base::CreatePlatformFile(
path, base::PLATFORM_FILE_WRITE | base::PLATFORM_FILE_CREATE_ALWAYS,
&created, &error);
if (error == base::PLATFORM_FILE_ERROR_EXISTS)
error = base::PLATFORM_FILE_OK;
if (error == base::PLATFORM_FILE_OK) {
if (!base::ClosePlatformFile(file))
error = base::PLATFORM_FILE_ERROR_FAILED;
}
}
} }
} }
if (error != base::PLATFORM_FILE_OK) if (error != base::PLATFORM_FILE_OK)
...@@ -1058,27 +1032,14 @@ FilePath ObfuscatedFileSystemFileUtil::GetDirectoryForOrigin( ...@@ -1058,27 +1032,14 @@ FilePath ObfuscatedFileSystemFileUtil::GetDirectoryForOrigin(
return FilePath(); return FilePath();
FilePath directory_name; FilePath directory_name;
std::string id = GetOriginIdentifierFromURL(origin); std::string id = GetOriginIdentifierFromURL(origin);
if (!create && !origin_database_->HasOriginPath(id))
bool exists_in_db = origin_database_->HasOriginPath(id);
if (!exists_in_db && !create)
return FilePath(); return FilePath();
if (!origin_database_->GetPathForOrigin(id, &directory_name)) if (!origin_database_->GetPathForOrigin(id, &directory_name))
return FilePath(); return FilePath();
FilePath path = file_system_directory_.Append(directory_name); FilePath path = file_system_directory_.Append(directory_name);
bool exists_in_fs = file_util::DirectoryExists(path); if (!file_util::DirectoryExists(path) &&
if (!exists_in_db && exists_in_fs) { (!create || !file_util::CreateDirectory(path)))
if (!file_util::Delete(path, true)) return FilePath();
return FilePath();
else
exists_in_fs = false;
}
if (!exists_in_fs) {
if (!create || !file_util::CreateDirectory(path))
return FilePath();
}
return path; return path;
} }
......
...@@ -999,77 +999,3 @@ TEST_F(ObfuscatedFileSystemFileUtilTest, TestOriginEnumerator) { ...@@ -999,77 +999,3 @@ TEST_F(ObfuscatedFileSystemFileUtilTest, TestOriginEnumerator) {
inserter(diff, diff.begin())); inserter(diff, diff.begin()));
EXPECT_TRUE(diff.empty()); EXPECT_TRUE(diff.empty());
} }
TEST_F(ObfuscatedFileSystemFileUtilTest, TestInconsistency) {
const FilePath kPath1 = FilePath().AppendASCII("hoge");
const FilePath kPath2 = FilePath().AppendASCII("fuga");
scoped_ptr<FileSystemOperationContext> context;
base::PlatformFile file;
base::PlatformFileInfo file_info;
FilePath data_path;
bool created = false;
// Create a non-empty file.
context.reset(NewContext());
EXPECT_EQ(base::PLATFORM_FILE_OK,
ofsfu()->EnsureFileExists(context.get(), kPath1, &created));
EXPECT_TRUE(created);
context.reset(NewContext());
EXPECT_EQ(base::PLATFORM_FILE_OK,
ofsfu()->Truncate(context.get(), kPath1, 10));
context.reset(NewContext());
EXPECT_EQ(base::PLATFORM_FILE_OK,
ofsfu()->GetFileInfo(
context.get(), kPath1, &file_info, &data_path));
EXPECT_EQ(10, file_info.size);
// Destroy database to make inconsistency between database and filesystem.
ofsfu()->DestroyDirectoryDatabase(origin_url(), type());
// Try to get file info of broken file.
context.reset(NewContext());
EXPECT_FALSE(ofsfu()->PathExists(context.get(), kPath1));
context.reset(NewContext());
EXPECT_EQ(base::PLATFORM_FILE_OK,
ofsfu()->EnsureFileExists(context.get(), kPath1, &created));
EXPECT_TRUE(created);
context.reset(NewContext());
EXPECT_EQ(base::PLATFORM_FILE_OK,
ofsfu()->GetFileInfo(
context.get(), kPath1, &file_info, &data_path));
EXPECT_EQ(0, file_info.size);
// Make another broken file to |kPath2|.
context.reset(NewContext());
EXPECT_EQ(base::PLATFORM_FILE_OK,
ofsfu()->EnsureFileExists(context.get(), kPath2, &created));
EXPECT_TRUE(created);
// Destroy again.
ofsfu()->DestroyDirectoryDatabase(origin_url(), type());
// Repair broken |kPath1|.
context.reset(NewContext());
EXPECT_EQ(base::PLATFORM_FILE_OK,
ofsfu()->Touch(context.get(), kPath1, base::Time::Now(),
base::Time::Now()));
// Copy from sound |kPath1| to broken |kPath2|.
context.reset(NewContext());
EXPECT_EQ(base::PLATFORM_FILE_OK,
ofsfu()->CopyOrMoveFile(context.get(), kPath1, kPath2,
true /* copy */));
ofsfu()->DestroyDirectoryDatabase(origin_url(), type());
context.reset(NewContext());
EXPECT_EQ(base::PLATFORM_FILE_OK,
ofsfu()->CreateOrOpen(
context.get(), kPath1,
base::PLATFORM_FILE_WRITE | base::PLATFORM_FILE_CREATE,
&file, &created));
EXPECT_TRUE(created);
EXPECT_TRUE(base::GetPlatformFileInfo(file, &file_info));
EXPECT_EQ(0, file_info.size);
EXPECT_TRUE(base::ClosePlatformFile(file));
}
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment