diff options
author | hidehiko@chromium.org <hidehiko@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2013-09-26 08:22:42 +0000 |
---|---|---|
committer | hidehiko@chromium.org <hidehiko@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2013-09-26 08:22:42 +0000 |
commit | f951750af63e60af3e82466c85ddfbc73c26120f (patch) | |
tree | 5358dccef0f72e268dda39c0b35d79dabed38813 /webkit | |
parent | 85d0c0169a3890aca6471eed3574ad56cf3fe4d4 (diff) | |
download | chromium_src-f951750af63e60af3e82466c85ddfbc73c26120f.zip chromium_src-f951750af63e60af3e82466c85ddfbc73c26120f.tar.gz chromium_src-f951750af63e60af3e82466c85ddfbc73c26120f.tar.bz2 |
Implement preserving last modified time on copy or move.
This CL is the implementation of preserving last modified time on Copy or Move
operations for native file system.
BUG=282107
TEST=Ran content_unittests and unit_tests
Review URL: https://codereview.chromium.org/24368002
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@225394 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'webkit')
-rw-r--r-- | webkit/browser/fileapi/local_file_util.cc | 7 | ||||
-rw-r--r-- | webkit/browser/fileapi/native_file_util.cc | 18 | ||||
-rw-r--r-- | webkit/browser/fileapi/native_file_util.h | 8 | ||||
-rw-r--r-- | webkit/browser/fileapi/native_file_util_unittest.cc | 101 | ||||
-rw-r--r-- | webkit/browser/fileapi/obfuscated_file_util.cc | 7 |
5 files changed, 103 insertions, 38 deletions
diff --git a/webkit/browser/fileapi/local_file_util.cc b/webkit/browser/fileapi/local_file_util.cc index 6958f47..3357c69 100644 --- a/webkit/browser/fileapi/local_file_util.cc +++ b/webkit/browser/fileapi/local_file_util.cc @@ -209,8 +209,8 @@ PlatformFileError LocalFileUtil::CopyOrMoveFile( if (error != base::PLATFORM_FILE_OK) return error; - // TODO(hidehiko): Support option. - return NativeFileUtil::CopyOrMoveFile(src_file_path, dest_file_path, copy); + return NativeFileUtil::CopyOrMoveFile( + src_file_path, dest_file_path, option, copy); } PlatformFileError LocalFileUtil::CopyInForeignFile( @@ -225,7 +225,8 @@ PlatformFileError LocalFileUtil::CopyInForeignFile( GetLocalFilePath(context, dest_url, &dest_file_path); if (error != base::PLATFORM_FILE_OK) return error; - return NativeFileUtil::CopyOrMoveFile(src_file_path, dest_file_path, true); + return NativeFileUtil::CopyOrMoveFile( + src_file_path, dest_file_path, FileSystemOperation::OPTION_NONE, true); } PlatformFileError LocalFileUtil::DeleteFile( diff --git a/webkit/browser/fileapi/native_file_util.cc b/webkit/browser/fileapi/native_file_util.cc index 32e9467..eec9670 100644 --- a/webkit/browser/fileapi/native_file_util.cc +++ b/webkit/browser/fileapi/native_file_util.cc @@ -205,6 +205,7 @@ bool NativeFileUtil::DirectoryExists(const base::FilePath& path) { PlatformFileError NativeFileUtil::CopyOrMoveFile( const base::FilePath& src_path, const base::FilePath& dest_path, + FileSystemOperation::CopyOrMoveOption option, bool copy) { base::PlatformFileInfo info; base::PlatformFileError error = NativeFileUtil::GetFileInfo(src_path, &info); @@ -212,6 +213,7 @@ PlatformFileError NativeFileUtil::CopyOrMoveFile( return error; if (info.is_directory) return base::PLATFORM_FILE_ERROR_NOT_A_FILE; + base::Time last_modified = info.last_modified; error = NativeFileUtil::GetFileInfo(dest_path, &info); if (error != base::PLATFORM_FILE_OK && @@ -228,13 +230,19 @@ PlatformFileError NativeFileUtil::CopyOrMoveFile( } if (copy) { - if (base::CopyFile(src_path, dest_path)) - return base::PLATFORM_FILE_OK; + if (!base::CopyFile(src_path, dest_path)) + return base::PLATFORM_FILE_ERROR_FAILED; } else { - if (base::Move(src_path, dest_path)) - return base::PLATFORM_FILE_OK; + if (!base::Move(src_path, dest_path)) + return base::PLATFORM_FILE_ERROR_FAILED; } - return base::PLATFORM_FILE_ERROR_FAILED; + + // Preserve the last modified time. Do not return error here even if + // the setting is failed, because the copy itself is successfully done. + if (option == FileSystemOperation::OPTION_PRESERVE_LAST_MODIFIED) + file_util::SetLastModifiedTime(dest_path, last_modified); + + return base::PLATFORM_FILE_OK; } PlatformFileError NativeFileUtil::DeleteFile(const base::FilePath& path) { diff --git a/webkit/browser/fileapi/native_file_util.h b/webkit/browser/fileapi/native_file_util.h index 425e742..58e6bed 100644 --- a/webkit/browser/fileapi/native_file_util.h +++ b/webkit/browser/fileapi/native_file_util.h @@ -53,9 +53,11 @@ class WEBKIT_STORAGE_BROWSER_EXPORT_PRIVATE NativeFileUtil { int64 length); static bool PathExists(const base::FilePath& path); static bool DirectoryExists(const base::FilePath& path); - static base::PlatformFileError CopyOrMoveFile(const base::FilePath& src_path, - const base::FilePath& dest_path, - bool copy); + static base::PlatformFileError CopyOrMoveFile( + const base::FilePath& src_path, + const base::FilePath& dest_path, + FileSystemOperation::CopyOrMoveOption option, + bool copy); static base::PlatformFileError DeleteFile(const base::FilePath& path); static base::PlatformFileError DeleteDirectory(const base::FilePath& path); diff --git a/webkit/browser/fileapi/native_file_util_unittest.cc b/webkit/browser/fileapi/native_file_util_unittest.cc index 1ed699e..e2b07e89 100644 --- a/webkit/browser/fileapi/native_file_util_unittest.cc +++ b/webkit/browser/fileapi/native_file_util_unittest.cc @@ -225,10 +225,12 @@ TEST_F(NativeFileUtilTest, CopyFile) { EXPECT_EQ(1020, GetSize(from_file)); ASSERT_EQ(base::PLATFORM_FILE_OK, - NativeFileUtil::CopyOrMoveFile(from_file, to_file1, true)); + NativeFileUtil::CopyOrMoveFile( + from_file, to_file1, FileSystemOperation::OPTION_NONE, true)); ASSERT_EQ(base::PLATFORM_FILE_OK, - NativeFileUtil::CopyOrMoveFile(from_file, to_file2, true)); + NativeFileUtil::CopyOrMoveFile( + from_file, to_file2, FileSystemOperation::OPTION_NONE, true)); EXPECT_TRUE(FileExists(from_file)); EXPECT_EQ(1020, GetSize(from_file)); @@ -243,32 +245,37 @@ TEST_F(NativeFileUtilTest, CopyFile) { ASSERT_TRUE(base::DirectoryExists(dir)); base::FilePath to_dir_file = dir.AppendASCII("file"); ASSERT_EQ(base::PLATFORM_FILE_OK, - NativeFileUtil::CopyOrMoveFile(from_file, to_dir_file, true)); + NativeFileUtil::CopyOrMoveFile( + from_file, to_dir_file, + FileSystemOperation::OPTION_NONE, true)); EXPECT_TRUE(FileExists(to_dir_file)); EXPECT_EQ(1020, GetSize(to_dir_file)); // Following tests are error checking. // Source doesn't exist. EXPECT_EQ(base::PLATFORM_FILE_ERROR_NOT_FOUND, - NativeFileUtil::CopyOrMoveFile(Path("nonexists"), Path("file"), - true)); + NativeFileUtil::CopyOrMoveFile( + Path("nonexists"), Path("file"), + FileSystemOperation::OPTION_NONE, true)); // Source is not a file. EXPECT_EQ(base::PLATFORM_FILE_ERROR_NOT_A_FILE, - NativeFileUtil::CopyOrMoveFile(dir, Path("file"), true)); + NativeFileUtil::CopyOrMoveFile( + dir, Path("file"), FileSystemOperation::OPTION_NONE, true)); // Destination is not a file. EXPECT_EQ(base::PLATFORM_FILE_ERROR_INVALID_OPERATION, - NativeFileUtil::CopyOrMoveFile(from_file, dir, true)); + NativeFileUtil::CopyOrMoveFile( + from_file, dir, FileSystemOperation::OPTION_NONE, true)); // Destination's parent doesn't exist. EXPECT_EQ(base::PLATFORM_FILE_ERROR_NOT_FOUND, - NativeFileUtil::CopyOrMoveFile(from_file, - Path("nodir").AppendASCII("file"), - true)); + NativeFileUtil::CopyOrMoveFile( + from_file, Path("nodir").AppendASCII("file"), + FileSystemOperation::OPTION_NONE, true)); // Destination's parent is a file. EXPECT_EQ(base::PLATFORM_FILE_ERROR_NOT_FOUND, - NativeFileUtil::CopyOrMoveFile(from_file, - Path("tofile1").AppendASCII("file"), - true)); + NativeFileUtil::CopyOrMoveFile( + from_file, Path("tofile1").AppendASCII("file"), + FileSystemOperation::OPTION_NONE, true)); } TEST_F(NativeFileUtilTest, MoveFile) { @@ -285,7 +292,8 @@ TEST_F(NativeFileUtilTest, MoveFile) { EXPECT_EQ(1020, GetSize(from_file)); ASSERT_EQ(base::PLATFORM_FILE_OK, - NativeFileUtil::CopyOrMoveFile(from_file, to_file, false)); + NativeFileUtil::CopyOrMoveFile( + from_file, to_file, FileSystemOperation::OPTION_NONE, false)); EXPECT_FALSE(FileExists(from_file)); EXPECT_TRUE(FileExists(to_file)); @@ -302,7 +310,9 @@ TEST_F(NativeFileUtilTest, MoveFile) { ASSERT_TRUE(base::DirectoryExists(dir)); base::FilePath to_dir_file = dir.AppendASCII("file"); ASSERT_EQ(base::PLATFORM_FILE_OK, - NativeFileUtil::CopyOrMoveFile(from_file, to_dir_file, false)); + NativeFileUtil::CopyOrMoveFile( + from_file, to_dir_file, + FileSystemOperation::OPTION_NONE, false)); EXPECT_FALSE(FileExists(from_file)); EXPECT_TRUE(FileExists(to_dir_file)); EXPECT_EQ(1020, GetSize(to_dir_file)); @@ -310,32 +320,73 @@ TEST_F(NativeFileUtilTest, MoveFile) { // Following is error checking. // Source doesn't exist. EXPECT_EQ(base::PLATFORM_FILE_ERROR_NOT_FOUND, - NativeFileUtil::CopyOrMoveFile(Path("nonexists"), Path("file"), - false)); + NativeFileUtil::CopyOrMoveFile( + Path("nonexists"), Path("file"), + FileSystemOperation::OPTION_NONE, false)); // Source is not a file. EXPECT_EQ(base::PLATFORM_FILE_ERROR_NOT_A_FILE, - NativeFileUtil::CopyOrMoveFile(dir, Path("file"), false)); + NativeFileUtil::CopyOrMoveFile( + dir, Path("file"), FileSystemOperation::OPTION_NONE, false)); ASSERT_EQ(base::PLATFORM_FILE_OK, NativeFileUtil::EnsureFileExists(from_file, &created)); ASSERT_TRUE(FileExists(from_file)); // Destination is not a file. EXPECT_EQ(base::PLATFORM_FILE_ERROR_INVALID_OPERATION, - NativeFileUtil::CopyOrMoveFile(from_file, dir, false)); + NativeFileUtil::CopyOrMoveFile( + from_file, dir, FileSystemOperation::OPTION_NONE, false)); ASSERT_EQ(base::PLATFORM_FILE_OK, NativeFileUtil::EnsureFileExists(from_file, &created)); ASSERT_TRUE(FileExists(from_file)); // Destination's parent doesn't exist. EXPECT_EQ(base::PLATFORM_FILE_ERROR_NOT_FOUND, - NativeFileUtil::CopyOrMoveFile(from_file, - Path("nodir").AppendASCII("file"), - false)); + NativeFileUtil::CopyOrMoveFile( + from_file, Path("nodir").AppendASCII("file"), + FileSystemOperation::OPTION_NONE, false)); // Destination's parent is a file. EXPECT_EQ(base::PLATFORM_FILE_ERROR_NOT_FOUND, - NativeFileUtil::CopyOrMoveFile(from_file, - Path("tofile1").AppendASCII("file"), - false)); + NativeFileUtil::CopyOrMoveFile( + from_file, Path("tofile1").AppendASCII("file"), + FileSystemOperation::OPTION_NONE, false)); +} + +TEST_F(NativeFileUtilTest, PreserveLastModified) { + base::FilePath from_file = Path("fromfile"); + base::FilePath to_file1 = Path("tofile1"); + base::FilePath to_file2 = Path("tofile2"); + bool created = false; + ASSERT_EQ(base::PLATFORM_FILE_OK, + NativeFileUtil::EnsureFileExists(from_file, &created)); + ASSERT_TRUE(created); + EXPECT_TRUE(FileExists(from_file)); + + base::PlatformFileInfo file_info1; + ASSERT_EQ(base::PLATFORM_FILE_OK, + NativeFileUtil::GetFileInfo(from_file, &file_info1)); + + // Test for copy. + ASSERT_EQ(base::PLATFORM_FILE_OK, + NativeFileUtil::CopyOrMoveFile( + from_file, to_file1, + FileSystemOperation::OPTION_PRESERVE_LAST_MODIFIED, true)); + + base::PlatformFileInfo file_info2; + ASSERT_TRUE(FileExists(to_file1)); + ASSERT_EQ(base::PLATFORM_FILE_OK, + NativeFileUtil::GetFileInfo(to_file1, &file_info2)); + EXPECT_EQ(file_info1.last_modified, file_info2.last_modified); + + // Test for move. + ASSERT_EQ(base::PLATFORM_FILE_OK, + NativeFileUtil::CopyOrMoveFile( + from_file, to_file2, + FileSystemOperation::OPTION_PRESERVE_LAST_MODIFIED, false)); + + ASSERT_TRUE(FileExists(to_file2)); + ASSERT_EQ(base::PLATFORM_FILE_OK, + NativeFileUtil::GetFileInfo(to_file2, &file_info2)); + EXPECT_EQ(file_info1.last_modified, file_info2.last_modified); } } // namespace fileapi diff --git a/webkit/browser/fileapi/obfuscated_file_util.cc b/webkit/browser/fileapi/obfuscated_file_util.cc index 221f90d..2643d7e 100644 --- a/webkit/browser/fileapi/obfuscated_file_util.cc +++ b/webkit/browser/fileapi/obfuscated_file_util.cc @@ -597,6 +597,7 @@ PlatformFileError ObfuscatedFileUtil::CopyOrMoveFile( error = NativeFileUtil::CopyOrMoveFile( src_local_path, dest_local_path, + option, true /* copy */); } else { // non-overwrite error = CreateFile(context, src_local_path, @@ -702,7 +703,8 @@ PlatformFileError ObfuscatedFileUtil::CopyInForeignFile( base::FilePath dest_local_path = DataPathToLocalPath( dest_url.origin(), dest_url.type(), dest_file_info.data_path); error = NativeFileUtil::CopyOrMoveFile( - src_file_path, dest_local_path, true); + src_file_path, dest_local_path, + FileSystemOperation::OPTION_NONE, true); } else { error = CreateFile(context, src_file_path, dest_url.origin(), dest_url.type(), @@ -1100,7 +1102,8 @@ PlatformFileError ObfuscatedFileUtil::CreateFile( DCHECK(!file_flags); DCHECK(!handle); error = NativeFileUtil::CopyOrMoveFile( - src_file_path, dest_local_path, true /* copy */); + src_file_path, dest_local_path, + FileSystemOperation::OPTION_NONE, true /* copy */); created = true; } else { if (base::PathExists(dest_local_path)) { |