diff options
-rw-r--r-- | base/file_util_posix.cc | 35 | ||||
-rw-r--r-- | base/file_util_unittest.cc | 116 | ||||
-rw-r--r-- | base/file_util_win.cc | 3 |
3 files changed, 145 insertions, 9 deletions
diff --git a/base/file_util_posix.cc b/base/file_util_posix.cc index 3ba351e..f011580 100644 --- a/base/file_util_posix.cc +++ b/base/file_util_posix.cc @@ -170,6 +170,19 @@ bool Delete(const FilePath& path, bool recursive) { } bool Move(const FilePath& from_path, const FilePath& to_path) { + // Windows compatibility: if to_path exists, from_path and to_path + // must be the same type, either both files, or both directories. + stat_wrapper_t to_file_info; + if (CallStat(to_path.value().c_str(), &to_file_info) == 0) { + stat_wrapper_t from_file_info; + if (CallStat(from_path.value().c_str(), &from_file_info) == 0) { + if (S_ISDIR(to_file_info.st_mode) != S_ISDIR(from_file_info.st_mode)) + return false; + } else { + return false; + } + } + if (rename(from_path.value().c_str(), to_path.value().c_str()) == 0) return true; @@ -228,22 +241,26 @@ bool CopyDirectory(const FilePath& from_path, FileEnumerator traversal(from_path, recursive, traverse_type); // We have to mimic windows behavior here. |to_path| may not exist yet, - // start the loop with |to_path|. If this is a recursive copy and - // the destination already exists, we have to copy the source directory - // as well. + // start the loop with |to_path|. FileEnumerator::FindInfo info; FilePath current = from_path; - FilePath from_path_base = from_path; - if (recursive && stat(to_path.value().c_str(), &info.stat) == 0) { - // If the destination already exists, then the top level of source - // needs to be copied. - from_path_base = from_path.DirName(); - } if (stat(from_path.value().c_str(), &info.stat) < 0) { LOG(ERROR) << "CopyDirectory() couldn't stat source directory: " << from_path.value() << " errno = " << errno; success = false; } + struct stat to_path_stat; + FilePath from_path_base = from_path; + if (recursive && stat(to_path.value().c_str(), &to_path_stat) == 0 && + S_ISDIR(to_path_stat.st_mode)) { + // If the destination already exists and is a directory, then the + // top level of source needs to be copied. + from_path_base = from_path.DirName(); + } + + // The Windows version of this function assumes that non-recursive calls + // will always have a directory for from_path. + DCHECK(recursive || S_ISDIR(info.stat.st_mode)); while (success && !current.empty()) { // current is the source path, including from_path, so paste diff --git a/base/file_util_unittest.cc b/base/file_util_unittest.cc index 6ac55f6..cde98a0 100644 --- a/base/file_util_unittest.cc +++ b/base/file_util_unittest.cc @@ -380,6 +380,63 @@ TEST_F(FileUtilTest, Delete) { EXPECT_FALSE(file_util::PathExists(subdir_path)); } +TEST_F(FileUtilTest, MoveFileNew) { + // Create a file + FilePath file_name_from = + test_dir_.Append(FILE_PATH_LITERAL("Move_Test_File.txt")); + CreateTextFile(file_name_from, L"Gooooooooooooooooooooogle"); + ASSERT_TRUE(file_util::PathExists(file_name_from)); + + // The destination + FilePath file_name_to = + test_dir_.Append(FILE_PATH_LITERAL("Move_Test_File_Destination.txt")); + ASSERT_FALSE(file_util::PathExists(file_name_to)); + + EXPECT_TRUE(file_util::Move(file_name_from, file_name_to)); + + // Check everything has been moved. + EXPECT_FALSE(file_util::PathExists(file_name_from)); + EXPECT_TRUE(file_util::PathExists(file_name_to)); +} + +TEST_F(FileUtilTest, MoveFileExists) { + // Create a file + FilePath file_name_from = + test_dir_.Append(FILE_PATH_LITERAL("Move_Test_File.txt")); + CreateTextFile(file_name_from, L"Gooooooooooooooooooooogle"); + ASSERT_TRUE(file_util::PathExists(file_name_from)); + + // The destination name + FilePath file_name_to = + test_dir_.Append(FILE_PATH_LITERAL("Move_Test_File_Destination.txt")); + CreateTextFile(file_name_to, L"Old file content"); + ASSERT_TRUE(file_util::PathExists(file_name_to)); + + EXPECT_TRUE(file_util::Move(file_name_from, file_name_to)); + + // Check everything has been moved. + EXPECT_FALSE(file_util::PathExists(file_name_from)); + EXPECT_TRUE(file_util::PathExists(file_name_to)); + EXPECT_TRUE(L"Gooooooooooooooooooooogle" == ReadTextFile(file_name_to)); +} + +TEST_F(FileUtilTest, MoveFileDirExists) { + // Create a file + FilePath file_name_from = + test_dir_.Append(FILE_PATH_LITERAL("Move_Test_File.txt")); + CreateTextFile(file_name_from, L"Gooooooooooooooooooooogle"); + ASSERT_TRUE(file_util::PathExists(file_name_from)); + + // The destination directory + FilePath dir_name_to = + test_dir_.Append(FILE_PATH_LITERAL("Destination")); + file_util::CreateDirectory(dir_name_to); + ASSERT_TRUE(file_util::PathExists(dir_name_to)); + + EXPECT_FALSE(file_util::Move(file_name_from, dir_name_to)); +} + + TEST_F(FileUtilTest, MoveNew) { // Create a directory FilePath dir_name_from = @@ -645,6 +702,65 @@ TEST_F(FileUtilTest, CopyDirectoryExists) { EXPECT_FALSE(file_util::PathExists(subdir_name_to)); } +TEST_F(FileUtilTest, CopyFileWithCopyDirectoryRecursiveToNew) { + // Create a file + FilePath file_name_from = + test_dir_.Append(FILE_PATH_LITERAL("Copy_Test_File.txt")); + CreateTextFile(file_name_from, L"Gooooooooooooooooooooogle"); + ASSERT_TRUE(file_util::PathExists(file_name_from)); + + // The destination name + FilePath file_name_to = + test_dir_.Append(FILE_PATH_LITERAL("Copy_Test_File_Destination.txt")); + ASSERT_FALSE(file_util::PathExists(file_name_to)); + + EXPECT_TRUE(file_util::CopyDirectory(file_name_from, file_name_to, true)); + + // Check the has been copied + EXPECT_TRUE(file_util::PathExists(file_name_to)); +} + +TEST_F(FileUtilTest, CopyFileWithCopyDirectoryRecursiveToExisting) { + // Create a file + FilePath file_name_from = + test_dir_.Append(FILE_PATH_LITERAL("Copy_Test_File.txt")); + CreateTextFile(file_name_from, L"Gooooooooooooooooooooogle"); + ASSERT_TRUE(file_util::PathExists(file_name_from)); + + // The destination name + FilePath file_name_to = + test_dir_.Append(FILE_PATH_LITERAL("Copy_Test_File_Destination.txt")); + CreateTextFile(file_name_to, L"Old file content"); + ASSERT_TRUE(file_util::PathExists(file_name_to)); + + EXPECT_TRUE(file_util::CopyDirectory(file_name_from, file_name_to, true)); + + // Check the has been copied + EXPECT_TRUE(file_util::PathExists(file_name_to)); + EXPECT_TRUE(L"Gooooooooooooooooooooogle" == ReadTextFile(file_name_to)); +} + +TEST_F(FileUtilTest, CopyFileWithCopyDirectoryRecursiveToExistingDirectory) { + // Create a file + FilePath file_name_from = + test_dir_.Append(FILE_PATH_LITERAL("Copy_Test_File.txt")); + CreateTextFile(file_name_from, L"Gooooooooooooooooooooogle"); + ASSERT_TRUE(file_util::PathExists(file_name_from)); + + // The destination + FilePath dir_name_to = + test_dir_.Append(FILE_PATH_LITERAL("Destination")); + file_util::CreateDirectory(dir_name_to); + ASSERT_TRUE(file_util::PathExists(dir_name_to)); + FilePath file_name_to = + dir_name_to.Append(FILE_PATH_LITERAL("Copy_Test_File.txt")); + + EXPECT_TRUE(file_util::CopyDirectory(file_name_from, dir_name_to, true)); + + // Check the has been copied + EXPECT_TRUE(file_util::PathExists(file_name_to)); +} + TEST_F(FileUtilTest, CopyFile) { // Create a directory FilePath dir_name_from = diff --git a/base/file_util_win.cc b/base/file_util_win.cc index 972a7b8..2d4a694 100644 --- a/base/file_util_win.cc +++ b/base/file_util_win.cc @@ -175,6 +175,9 @@ bool CopyDirectory(const FilePath& from_path, const FilePath& to_path, if (recursive) return ShellCopy(from_path, to_path, true); + // The following code assumes that from path is a directory. + DCHECK(DirectoryExists(from_path)); + // Instead of creating a new directory, we copy the old one to include the // security information of the folder as part of the copy. if (!PathExists(to_path)) { |