summaryrefslogtreecommitdiffstats
path: root/base
diff options
context:
space:
mode:
authorhuanr@chromium.org <huanr@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98>2009-03-11 18:37:48 +0000
committerhuanr@chromium.org <huanr@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98>2009-03-11 18:37:48 +0000
commit2c59af7dcafb6e26fee53be61c180cfc7a310ed5 (patch)
tree7ab0fc2ac460c64762d181cc366dc4f66497be58 /base
parent7971feac373553f6d0e8db78ac08130694c7b4f3 (diff)
downloadchromium_src-2c59af7dcafb6e26fee53be61c180cfc7a310ed5.zip
chromium_src-2c59af7dcafb6e26fee53be61c180cfc7a310ed5.tar.gz
chromium_src-2c59af7dcafb6e26fee53be61c180cfc7a310ed5.tar.bz2
file_util::Move fails on Windows if moving a directory
across volumes. This change adds a CopyAndDeleteDirectory function, and Move falls back to CopyAndDeleteDirectory if moving directory fails. BUG=8505 Review URL: http://codereview.chromium.org/43069 git-svn-id: svn://svn.chromium.org/chrome/trunk/src@11460 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'base')
-rw-r--r--base/file_util.h6
-rw-r--r--base/file_util_unittest.cc30
-rw-r--r--base/file_util_win.cc27
3 files changed, 61 insertions, 2 deletions
diff --git a/base/file_util.h b/base/file_util.h
index 297d257..70e24e1 100644
--- a/base/file_util.h
+++ b/base/file_util.h
@@ -254,6 +254,12 @@ bool UpdateShortcutLink(const wchar_t *source, const wchar_t *destination,
// Return true if the given directory is empty
bool IsDirectoryEmpty(const std::wstring& dir_path);
+// Copy from_path to to_path recursively and then delete from_path recursively.
+// Returns true if all operations succeed.
+// This function simulates Move(), but unlike Move() it works across volumes.
+// This fuction is not transactional.
+bool CopyAndDeleteDirectory(const FilePath& from_path,
+ const FilePath& to_path);
#endif
// Get the temporary directory provided by the system.
diff --git a/base/file_util_unittest.cc b/base/file_util_unittest.cc
index 5a28a95..0153e9b 100644
--- a/base/file_util_unittest.cc
+++ b/base/file_util_unittest.cc
@@ -685,6 +685,36 @@ TEST_F(FileUtilTest, CreateShortcutTest) {
DeleteFile(link_file.value().c_str());
CoUninitialize();
}
+
+TEST_F(FileUtilTest, CopyAndDeleteDirectoryTest) {
+ // Create a directory
+ FilePath dir_name_from =
+ test_dir_.Append(FILE_PATH_LITERAL("CopyAndDelete_From_Subdir"));
+ file_util::CreateDirectory(dir_name_from);
+ ASSERT_TRUE(file_util::PathExists(dir_name_from));
+
+ // Create a file under the directory
+ FilePath file_name_from =
+ dir_name_from.Append(FILE_PATH_LITERAL("CopyAndDelete_Test_File.txt"));
+ CreateTextFile(file_name_from, L"Gooooooooooooooooooooogle");
+ ASSERT_TRUE(file_util::PathExists(file_name_from));
+
+ // Move the directory by using CopyAndDeleteDirectory
+ FilePath dir_name_to = test_dir_.Append(
+ FILE_PATH_LITERAL("CopyAndDelete_To_Subdir"));
+ FilePath file_name_to =
+ dir_name_to.Append(FILE_PATH_LITERAL("CopyAndDelete_Test_File.txt"));
+
+ ASSERT_FALSE(file_util::PathExists(dir_name_to));
+
+ EXPECT_TRUE(file_util::CopyAndDeleteDirectory(dir_name_from, dir_name_to));
+
+ // Check everything has been moved.
+ EXPECT_FALSE(file_util::PathExists(dir_name_from));
+ EXPECT_FALSE(file_util::PathExists(file_name_from));
+ EXPECT_TRUE(file_util::PathExists(dir_name_to));
+ EXPECT_TRUE(file_util::PathExists(file_name_to));
+}
#endif
TEST_F(FileUtilTest, CreateTemporaryFileNameTest) {
diff --git a/base/file_util_win.cc b/base/file_util_win.cc
index 22c24cd..45c0afde 100644
--- a/base/file_util_win.cc
+++ b/base/file_util_win.cc
@@ -101,8 +101,16 @@ bool Move(const FilePath& from_path, const FilePath& to_path) {
to_path.value().length() >= MAX_PATH) {
return false;
}
- return (MoveFileEx(from_path.value().c_str(), to_path.value().c_str(),
- MOVEFILE_COPY_ALLOWED | MOVEFILE_REPLACE_EXISTING) != 0);
+ if (MoveFileEx(from_path.value().c_str(), to_path.value().c_str(),
+ MOVEFILE_COPY_ALLOWED | MOVEFILE_REPLACE_EXISTING) != 0)
+ return true;
+ if (DirectoryExists(from_path)) {
+ // MoveFileEx fails if moving directory across volumes. We will simulate
+ // the move by using Copy and Delete. Ideally we could check whether
+ // from_path and to_path are indeed in different volumes.
+ return CopyAndDeleteDirectory(from_path, to_path);
+ }
+ return false;
}
bool CopyFile(const FilePath& from_path, const FilePath& to_path) {
@@ -167,6 +175,21 @@ bool CopyDirectory(const FilePath& from_path, const FilePath& to_path,
return ShellCopy(directory, to_path, false);
}
+bool CopyAndDeleteDirectory(const FilePath& from_path,
+ const FilePath& to_path) {
+ if (CopyDirectory(from_path, to_path, true)) {
+ if (Delete(from_path, true)) {
+ return true;
+ }
+ // Like Move, this function is not transactional, so we just
+ // leave the copied bits behind if deleting from_path fails.
+ // If to_path exists previously then we have already overwritten
+ // it by now, we don't get better off by deleting the new bits.
+ }
+ return false;
+}
+
+
bool PathExists(const FilePath& path) {
return (GetFileAttributes(path.value().c_str()) != INVALID_FILE_ATTRIBUTES);
}