Commit 2abc4055 authored by Sam McNally's avatar Sam McNally Committed by Commit Bot

Add support for atomic directory moves over the filesystem API.

Bug: 172187
Change-Id: I0e42fe87ed76a44bff4c2a34065255e9440870a2
Reviewed-on: https://chromium-review.googlesource.com/1180822Reviewed-by: default avatarHiroki Nakagawa <nhiroki@chromium.org>
Commit-Queue: Sam McNally <sammc@chromium.org>
Cr-Commit-Position: refs/heads/master@{#584927}
parent 00fe56f1
......@@ -253,15 +253,16 @@ base::File::Error NativeFileUtil::CopyOrMoveFile(
base::File::Error error = NativeFileUtil::GetFileInfo(src_path, &info);
if (error != base::File::FILE_OK)
return error;
if (info.is_directory)
if (info.is_directory && mode != MOVE)
return base::File::FILE_ERROR_NOT_A_FILE;
bool src_is_directory = info.is_directory;
base::Time last_modified = info.last_modified;
error = NativeFileUtil::GetFileInfo(dest_path, &info);
if (error != base::File::FILE_OK &&
error != base::File::FILE_ERROR_NOT_FOUND)
return error;
if (info.is_directory)
if (error == base::File::FILE_OK && (info.is_directory || src_is_directory))
return base::File::FILE_ERROR_INVALID_OPERATION;
if (error == base::File::FILE_ERROR_NOT_FOUND) {
error = NativeFileUtil::GetFileInfo(dest_path.DirName(), &info);
......
......@@ -326,10 +326,20 @@ TEST_F(NativeFileUtilTest, MoveFile) {
Path("nonexists"), Path("file"),
FileSystemOperation::OPTION_NONE, move));
// Source is not a file.
EXPECT_EQ(base::File::FILE_ERROR_NOT_A_FILE,
base::FilePath dir2 = Path("dir2");
ASSERT_EQ(base::File::FILE_OK,
NativeFileUtil::CreateDirectory(dir2, false, false));
ASSERT_TRUE(base::DirectoryExists(dir2));
// Source is a directory, destination is a file.
EXPECT_EQ(base::File::FILE_ERROR_INVALID_OPERATION,
NativeFileUtil::CopyOrMoveFile(
dir, to_file, FileSystemOperation::OPTION_NONE, move));
// Source is a directory, destination is a directory.
EXPECT_EQ(base::File::FILE_ERROR_INVALID_OPERATION,
NativeFileUtil::CopyOrMoveFile(
dir, Path("file"), FileSystemOperation::OPTION_NONE, move));
dir, dir2, FileSystemOperation::OPTION_NONE, move));
ASSERT_EQ(base::File::FILE_OK,
NativeFileUtil::EnsureFileExists(from_file, &created));
ASSERT_TRUE(FileExists(from_file));
......@@ -353,6 +363,35 @@ TEST_F(NativeFileUtilTest, MoveFile) {
FileSystemOperation::OPTION_NONE, move));
}
TEST_F(NativeFileUtilTest, MoveFile_Directory) {
base::FilePath from_directory = Path("fromdirectory");
base::FilePath to_directory = Path("todirectory");
base::FilePath from_file = from_directory.AppendASCII("fromfile");
base::FilePath to_file = to_directory.AppendASCII("fromfile");
ASSERT_TRUE(base::CreateDirectory(from_directory));
const NativeFileUtil::CopyOrMoveMode move = NativeFileUtil::MOVE;
bool created = false;
ASSERT_EQ(base::File::FILE_OK,
NativeFileUtil::EnsureFileExists(
from_directory.AppendASCII("fromfile"), &created));
ASSERT_TRUE(created);
ASSERT_EQ(base::File::FILE_OK, NativeFileUtil::Truncate(from_file, 1020));
EXPECT_TRUE(FileExists(from_file));
EXPECT_EQ(1020, GetSize(from_file));
ASSERT_EQ(base::File::FILE_OK, NativeFileUtil::CopyOrMoveFile(
from_directory, to_directory,
FileSystemOperation::OPTION_NONE, move));
EXPECT_FALSE(base::DirectoryExists(from_directory));
EXPECT_FALSE(FileExists(from_file));
EXPECT_TRUE(base::DirectoryExists(to_directory));
EXPECT_TRUE(FileExists(to_file));
EXPECT_EQ(1020, GetSize(to_file));
}
TEST_F(NativeFileUtilTest, PreserveLastModified) {
base::FilePath from_file = Path("fromfile");
base::FilePath to_file1 = Path("tofile1");
......
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