Title: [276936] trunk
Revision
276936
Author
[email protected]
Date
2021-05-03 16:53:22 -0700 (Mon, 03 May 2021)

Log Message

Restore pre-r276879 behavior for FileSystem::moveFile()
https://bugs.webkit.org/show_bug.cgi?id=225307

Reviewed by Sam Weinig.

Source/WTF:

Update FileSystem::moveFile() so that it now supports again moving a file across different
volumes by copying the file over and then deleting the source file.

I have verified locally that the function is now able to move a file to a different volumes.
Sadly, I don't think having an API test is feasible here as it requires pre-determined
volumes to be available.

* wtf/FileSystem.cpp:
(WTF::FileSystemImpl::moveFile):

Tools:

Add API test to make sure that FileSystem::moveFile() is able to move directories since this
was the case prior to r276879.

* TestWebKitAPI/Tests/WTF/FileSystem.cpp:
(TestWebKitAPI::TEST_F):

Modified Paths

Diff

Modified: trunk/Source/WTF/ChangeLog (276935 => 276936)


--- trunk/Source/WTF/ChangeLog	2021-05-03 23:41:50 UTC (rev 276935)
+++ trunk/Source/WTF/ChangeLog	2021-05-03 23:53:22 UTC (rev 276936)
@@ -1,5 +1,22 @@
 2021-05-03  Chris Dumez  <[email protected]>
 
+        Restore pre-r276879 behavior for FileSystem::moveFile()
+        https://bugs.webkit.org/show_bug.cgi?id=225307
+
+        Reviewed by Sam Weinig.
+
+        Update FileSystem::moveFile() so that it now supports again moving a file across different
+        volumes by copying the file over and then deleting the source file.
+
+        I have verified locally that the function is now able to move a file to a different volumes.
+        Sadly, I don't think having an API test is feasible here as it requires pre-determined
+        volumes to be available.
+
+        * wtf/FileSystem.cpp:
+        (WTF::FileSystemImpl::moveFile):
+
+2021-05-03  Chris Dumez  <[email protected]>
+
         Unreviewed attempt to fix JSCOnly build with recent clang after r276879
         https://bugs.webkit.org/show_bug.cgi?id=225327
 

Modified: trunk/Source/WTF/wtf/FileSystem.cpp (276935 => 276936)


--- trunk/Source/WTF/wtf/FileSystem.cpp	2021-05-03 23:41:50 UTC (rev 276935)
+++ trunk/Source/WTF/wtf/FileSystem.cpp	2021-05-03 23:53:22 UTC (rev 276936)
@@ -532,9 +532,20 @@
 
 bool moveFile(const String& oldPath, const String& newPath)
 {
+    std::filesystem::path fsOldPath = fileSystemRepresentation(oldPath).data();
+    std::filesystem::path fsNewPath = fileSystemRepresentation(newPath).data();
+
     std::error_code ec;
-    std::filesystem::rename(fileSystemRepresentation(oldPath).data(), fileSystemRepresentation(newPath).data(), ec);
-    return !ec;
+    std::filesystem::rename(fsOldPath, fsNewPath, ec);
+    if (!ec)
+        return true;
+
+    // Fall back to copying and then deleting source as rename() does not work across volumes.
+    ec = { };
+    std::filesystem::copy(fsOldPath, fsNewPath, std::filesystem::copy_options::overwrite_existing | std::filesystem::copy_options::recursive, ec);
+    if (ec)
+        return false;
+    return std::filesystem::remove_all(fsOldPath, ec);
 }
 
 bool getFileSize(const String& path, long long& result)

Modified: trunk/Tools/ChangeLog (276935 => 276936)


--- trunk/Tools/ChangeLog	2021-05-03 23:41:50 UTC (rev 276935)
+++ trunk/Tools/ChangeLog	2021-05-03 23:53:22 UTC (rev 276936)
@@ -1,3 +1,16 @@
+2021-05-03  Chris Dumez  <[email protected]>
+
+        Restore pre-r276879 behavior for FileSystem::moveFile()
+        https://bugs.webkit.org/show_bug.cgi?id=225307
+
+        Reviewed by Sam Weinig.
+
+        Add API test to make sure that FileSystem::moveFile() is able to move directories since this
+        was the case prior to r276879.
+
+        * TestWebKitAPI/Tests/WTF/FileSystem.cpp:
+        (TestWebKitAPI::TEST_F):
+
 2021-05-03  Jonathan Bedard  <[email protected]>
 
         [webkitpy] Support pickling platforminfo

Modified: trunk/Tools/TestWebKitAPI/Tests/WTF/FileSystem.cpp (276935 => 276936)


--- trunk/Tools/TestWebKitAPI/Tests/WTF/FileSystem.cpp	2021-05-03 23:41:50 UTC (rev 276935)
+++ trunk/Tools/TestWebKitAPI/Tests/WTF/FileSystem.cpp	2021-05-03 23:53:22 UTC (rev 276936)
@@ -328,6 +328,32 @@
     EXPECT_GT(fileSize, 0);
 }
 
+TEST_F(FileSystemTest, moveDirectory)
+{
+    FileSystem::PlatformFileHandle temporaryFile;
+    auto temporaryTestFolder = FileSystem::openTemporaryFile("moveDirectoryTest", temporaryFile);
+    FileSystem::closeFile(temporaryFile);
+
+    EXPECT_TRUE(FileSystem::deleteFile(temporaryTestFolder));
+    EXPECT_TRUE(FileSystem::makeAllDirectories(temporaryTestFolder));
+    auto testFilePath = FileSystem::pathByAppendingComponent(temporaryTestFolder, "testFile");
+    auto fileHandle = FileSystem::openFile(testFilePath, FileSystem::FileOpenMode::Write);
+    FileSystem::writeToFile(fileHandle, FileSystemTestData, strlen(FileSystemTestData));
+    FileSystem::closeFile(fileHandle);
+
+    EXPECT_TRUE(FileSystem::fileExists(testFilePath));
+
+    auto destinationPath = FileSystem::pathByAppendingComponent(tempEmptyFolderPath(), "moveDirectoryTest");
+    EXPECT_TRUE(FileSystem::moveFile(temporaryTestFolder, destinationPath));
+    EXPECT_FALSE(FileSystem::fileExists(temporaryTestFolder));
+    EXPECT_FALSE(FileSystem::fileExists(testFilePath));
+    EXPECT_TRUE(FileSystem::fileExists(destinationPath));
+    EXPECT_TRUE(FileSystem::fileExists(FileSystem::pathByAppendingComponent(destinationPath, "testFile")));
+
+    EXPECT_FALSE(FileSystem::deleteEmptyDirectory(destinationPath));
+    EXPECT_TRUE(FileSystem::fileExists(destinationPath));
+}
+
 TEST_F(FileSystemTest, getFileSize)
 {
     EXPECT_TRUE(FileSystem::fileExists(tempFilePath()));
_______________________________________________
webkit-changes mailing list
[email protected]
https://lists.webkit.org/mailman/listinfo/webkit-changes

Reply via email to