More CopyDirectory tests and fixes

BUG=themes stopped working on Linux and Mac
TEST=unittests

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

git-svn-id: svn://svn.chromium.org/chrome/trunk/src@29076 0039d316-1c4b-4281-b951-d872f2087c98


CrOS-Libchrome-Original-Commit: bc6a901f7d54c1307fcffc82181aedcf694470f7
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 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 @@
   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 @@
   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 @@
   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 =