Title: [277522] trunk
Revision
277522
Author
[email protected]
Date
2021-05-14 17:26:58 -0700 (Fri, 14 May 2021)

Log Message

Port WTF::FileSystem::realPath() to std::filesystem
https://bugs.webkit.org/show_bug.cgi?id=225828

Reviewed by Darin Adler.

Source/WTF:

Port WTF::FileSystem::realPath() to std::filesystem (in particular std::filesystem::canonical())
so that we no longer need platform-specific code.

* wtf/FileSystem.cpp:
(WTF::FileSystemImpl::realPath):
* wtf/posix/FileSystemPOSIX.cpp:
* wtf/win/FileSystemWin.cpp:

Tools:

Add API test coverage.

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

Modified Paths

Diff

Modified: trunk/Source/WTF/ChangeLog (277521 => 277522)


--- trunk/Source/WTF/ChangeLog	2021-05-15 00:18:38 UTC (rev 277521)
+++ trunk/Source/WTF/ChangeLog	2021-05-15 00:26:58 UTC (rev 277522)
@@ -1,5 +1,20 @@
 2021-05-14  Chris Dumez  <[email protected]>
 
+        Port WTF::FileSystem::realPath() to std::filesystem
+        https://bugs.webkit.org/show_bug.cgi?id=225828
+
+        Reviewed by Darin Adler.
+
+        Port WTF::FileSystem::realPath() to std::filesystem (in particular std::filesystem::canonical())
+        so that we no longer need platform-specific code.
+
+        * wtf/FileSystem.cpp:
+        (WTF::FileSystemImpl::realPath):
+        * wtf/posix/FileSystemPOSIX.cpp:
+        * wtf/win/FileSystemWin.cpp:
+
+2021-05-14  Chris Dumez  <[email protected]>
+
         Drop "get" prefix from WTF::FileSystem's getFileModificationTime() / getFileCreationTime()
         https://bugs.webkit.org/show_bug.cgi?id=225812
 

Modified: trunk/Source/WTF/wtf/FileSystem.cpp (277521 => 277522)


--- trunk/Source/WTF/wtf/FileSystem.cpp	2021-05-15 00:18:38 UTC (rev 277521)
+++ trunk/Source/WTF/wtf/FileSystem.cpp	2021-05-15 00:26:58 UTC (rev 277522)
@@ -754,6 +754,13 @@
     return fromStdFileSystemPath(toStdFileSystemPath(path).parent_path());
 }
 
+String realPath(const String& path)
+{
+    std::error_code ec;
+    auto canonicalPath = std::filesystem::canonical(toStdFileSystemPath(path), ec);
+    return ec ? path : fromStdFileSystemPath(canonicalPath);
+}
+
 String pathByAppendingComponent(const String& path, const String& component)
 {
     return fromStdFileSystemPath(toStdFileSystemPath(path) / toStdFileSystemPath(component));

Modified: trunk/Source/WTF/wtf/posix/FileSystemPOSIX.cpp (277521 => 277522)


--- trunk/Source/WTF/wtf/posix/FileSystemPOSIX.cpp	2021-05-15 00:18:38 UTC (rev 277521)
+++ trunk/Source/WTF/wtf/posix/FileSystemPOSIX.cpp	2021-05-15 00:26:58 UTC (rev 277522)
@@ -236,13 +236,5 @@
     return fileStat.st_dev;
 }
 
-String realPath(const String& filePath)
-{
-    CString fsRep = fileSystemRepresentation(filePath);
-    char resolvedName[PATH_MAX];
-    const char* result = realpath(fsRep.data(), resolvedName);
-    return result ? String::fromUTF8(result) : filePath;
-}
-
 } // namespace FileSystemImpl
 } // namespace WTF

Modified: trunk/Source/WTF/wtf/win/FileSystemWin.cpp (277521 => 277522)


--- trunk/Source/WTF/wtf/win/FileSystemWin.cpp	2021-05-15 00:18:38 UTC (rev 277521)
+++ trunk/Source/WTF/wtf/win/FileSystemWin.cpp	2021-05-15 00:26:58 UTC (rev 277522)
@@ -124,24 +124,6 @@
     return WallTime::fromRawSeconds(time);
 }
 
-static String getFinalPathName(const String& path)
-{
-    auto handle = openFile(path, FileOpenMode::Read);
-    if (!isHandleValid(handle))
-        return String();
-
-    // VOLUME_NAME_DOS can return a \\?\ prefixed path, so it can be longer than MAX_PATH
-    Vector<UChar> buffer(32768);
-    if (::GetFinalPathNameByHandleW(handle, wcharFrom(buffer.data()), buffer.size(), VOLUME_NAME_DOS) >= 32768) {
-        closeFile(handle);
-        return String();
-    }
-    closeFile(handle);
-
-    buffer.shrink(wcslen(wcharFrom(buffer.data())));
-    return String::adopt(WTFMove(buffer));
-}
-
 #if !USE(CF)
 
 CString fileSystemRepresentation(const String& path)
@@ -375,11 +357,6 @@
     return fileInformation.dwVolumeSerialNumber;
 }
 
-String realPath(const String& filePath)
-{
-    return getFinalPathName(filePath);
-}
-
 String createTemporaryDirectory()
 {
     return generateTemporaryPath([](const String& proposedPath) {

Modified: trunk/Tools/ChangeLog (277521 => 277522)


--- trunk/Tools/ChangeLog	2021-05-15 00:18:38 UTC (rev 277521)
+++ trunk/Tools/ChangeLog	2021-05-15 00:26:58 UTC (rev 277522)
@@ -1,5 +1,17 @@
 2021-05-14  Chris Dumez  <[email protected]>
 
+        Port WTF::FileSystem::realPath() to std::filesystem
+        https://bugs.webkit.org/show_bug.cgi?id=225828
+
+        Reviewed by Darin Adler.
+
+        Add API test coverage.
+
+        * TestWebKitAPI/Tests/WTF/FileSystem.cpp:
+        (TestWebKitAPI::TEST_F):
+
+2021-05-14  Chris Dumez  <[email protected]>
+
         Drop "get" prefix from WTF::FileSystem's getFileModificationTime() / getFileCreationTime()
         https://bugs.webkit.org/show_bug.cgi?id=225812
 

Modified: trunk/Tools/TestWebKitAPI/Tests/WTF/FileSystem.cpp (277521 => 277522)


--- trunk/Tools/TestWebKitAPI/Tests/WTF/FileSystem.cpp	2021-05-15 00:18:38 UTC (rev 277521)
+++ trunk/Tools/TestWebKitAPI/Tests/WTF/FileSystem.cpp	2021-05-15 00:26:58 UTC (rev 277522)
@@ -870,4 +870,30 @@
     EXPECT_TRUE(FileSystem::deleteNonEmptyDirectory(tempEmptyFolderPath()));
 }
 
+TEST_F(FileSystemTest, realPath)
+{
+    auto doesNotExistPath = FileSystem::pathByAppendingComponent(tempEmptyFolderPath(), "does-not-exist");
+    EXPECT_STREQ(FileSystem::realPath(doesNotExistPath).utf8().data(), doesNotExistPath.utf8().data());
+
+    auto resolvedTempFilePath = FileSystem::realPath(tempFilePath());
+    EXPECT_STREQ(FileSystem::realPath(resolvedTempFilePath).utf8().data(), resolvedTempFilePath.utf8().data());
+    EXPECT_STREQ(FileSystem::realPath(tempFileSymlinkPath()).utf8().data(), resolvedTempFilePath.utf8().data()); // Should resolve file symlink.
+
+    auto resolvedTempEmptyFolderPath = FileSystem::realPath(tempEmptyFolderPath());
+    EXPECT_STREQ(FileSystem::realPath(resolvedTempEmptyFolderPath).utf8().data(), resolvedTempEmptyFolderPath.utf8().data());
+    EXPECT_STREQ(FileSystem::realPath(tempEmptyFolderSymlinkPath()).utf8().data(), resolvedTempEmptyFolderPath.utf8().data()); // Should resolve directory symlink.
+
+    // Symlink to symlink case.
+    auto symlinkToSymlinkPath = FileSystem::pathByAppendingComponent(tempEmptyFolderPath(), "symlinkToSymlink");
+    EXPECT_TRUE(FileSystem::createSymbolicLink(tempFileSymlinkPath(), symlinkToSymlinkPath));
+    EXPECT_STREQ(FileSystem::realPath(symlinkToSymlinkPath).utf8().data(), resolvedTempFilePath.utf8().data()); // Should resolve all symlinks.
+
+    auto subFolderPath = FileSystem::pathByAppendingComponent(tempEmptyFolderPath(), "subfolder");
+    FileSystem::makeAllDirectories(subFolderPath);
+    auto resolvedSubFolderPath = FileSystem::realPath(subFolderPath);
+    EXPECT_STREQ(FileSystem::realPath(FileSystem::pathByAppendingComponent(subFolderPath, "..")).utf8().data(), resolvedTempEmptyFolderPath.utf8().data()); // Should resolve "..".
+    EXPECT_STREQ(FileSystem::realPath(FileSystem::pathByAppendingComponents(subFolderPath, { "..", "subfolder" })).utf8().data(), resolvedSubFolderPath.utf8().data()); // Should resolve "..".
+    EXPECT_STREQ(FileSystem::realPath(FileSystem::pathByAppendingComponents(subFolderPath, { "..", ".", ".", "subfolder" })).utf8().data(), resolvedSubFolderPath.utf8().data()); // Should resolve ".." and "."
+}
+
 } // namespace TestWebKitAPI
_______________________________________________
webkit-changes mailing list
[email protected]
https://lists.webkit.org/mailman/listinfo/webkit-changes

Reply via email to