Title: [277219] trunk
Revision
277219
Author
[email protected]
Date
2021-05-07 18:05:02 -0700 (Fri, 07 May 2021)

Log Message

Port FileSystem::pathGetFileName() & Filesystem::directoryName() to std::filesystem
https://bugs.webkit.org/show_bug.cgi?id=225524

Reviewed by Darin Adler.

Source/WTF:

Port FileSystem::pathGetFileName() & Filesystem::directoryName() to std::filesystem so
that we can get rid of platform-specific implementions.

* wtf/FileSystem.cpp:
(WTF::FileSystemImpl::pathGetFileName):
(WTF::FileSystemImpl::directoryName):
* wtf/glib/FileSystemGlib.cpp:
(WTF::FileSystemImpl::pathGetFileName): Deleted.
(WTF::FileSystemImpl::directoryName): Deleted.
* wtf/posix/FileSystemPOSIX.cpp:
(WTF::FileSystemImpl::pathGetFileName): Deleted.
(WTF::FileSystemImpl::directoryName): Deleted.
* wtf/win/FileSystemWin.cpp:
(WTF::FileSystemImpl::pathGetFileName): Deleted.
(WTF::FileSystemImpl::directoryName): Deleted.

Tools:

Add API test coverage.

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

Modified Paths

Diff

Modified: trunk/Source/WTF/ChangeLog (277218 => 277219)


--- trunk/Source/WTF/ChangeLog	2021-05-08 01:04:02 UTC (rev 277218)
+++ trunk/Source/WTF/ChangeLog	2021-05-08 01:05:02 UTC (rev 277219)
@@ -1,3 +1,26 @@
+2021-05-07  Chris Dumez  <[email protected]>
+
+        Port FileSystem::pathGetFileName() & Filesystem::directoryName() to std::filesystem
+        https://bugs.webkit.org/show_bug.cgi?id=225524
+
+        Reviewed by Darin Adler.
+
+        Port FileSystem::pathGetFileName() & Filesystem::directoryName() to std::filesystem so
+        that we can get rid of platform-specific implementions.
+
+        * wtf/FileSystem.cpp:
+        (WTF::FileSystemImpl::pathGetFileName):
+        (WTF::FileSystemImpl::directoryName):
+        * wtf/glib/FileSystemGlib.cpp:
+        (WTF::FileSystemImpl::pathGetFileName): Deleted.
+        (WTF::FileSystemImpl::directoryName): Deleted.
+        * wtf/posix/FileSystemPOSIX.cpp:
+        (WTF::FileSystemImpl::pathGetFileName): Deleted.
+        (WTF::FileSystemImpl::directoryName): Deleted.
+        * wtf/win/FileSystemWin.cpp:
+        (WTF::FileSystemImpl::pathGetFileName): Deleted.
+        (WTF::FileSystemImpl::directoryName): Deleted.
+
 2021-05-07  Tim Horton  <[email protected]>
 
         Add an experimental alternative display-list-based RemoteLayerBackingStore implementation

Modified: trunk/Source/WTF/wtf/FileSystem.cpp (277218 => 277219)


--- trunk/Source/WTF/wtf/FileSystem.cpp	2021-05-08 01:04:02 UTC (rev 277218)
+++ trunk/Source/WTF/wtf/FileSystem.cpp	2021-05-08 01:05:02 UTC (rev 277219)
@@ -721,5 +721,17 @@
     return fileMetadataPotentiallyFollowingSymlinks(path, ShouldFollowSymbolicLinks::Yes);
 }
 
+String pathGetFileName(const String& path)
+{
+    std::filesystem::path fsPath = fileSystemRepresentation(path).data();
+    return String::fromUTF8(fsPath.filename().u8string().c_str());
+}
+
+String directoryName(const String& path)
+{
+    std::filesystem::path fsPath = fileSystemRepresentation(path).data();
+    return String::fromUTF8(fsPath.parent_path().u8string().c_str());
+}
+
 } // namespace FileSystemImpl
 } // namespace WTF

Modified: trunk/Source/WTF/wtf/glib/FileSystemGlib.cpp (277218 => 277219)


--- trunk/Source/WTF/wtf/glib/FileSystemGlib.cpp	2021-05-08 01:04:02 UTC (rev 277218)
+++ trunk/Source/WTF/wtf/glib/FileSystemGlib.cpp	2021-05-08 01:05:02 UTC (rev 277219)
@@ -157,26 +157,6 @@
     return stringFromFileSystemRepresentation(g_get_home_dir());
 }
 
-String pathGetFileName(const String& path)
-{
-    auto filename = fileSystemRepresentation(path);
-    if (!validRepresentation(filename))
-        return path;
-
-    GUniquePtr<gchar> baseName(g_path_get_basename(filename.data()));
-    return String::fromUTF8(baseName.get());
-}
-
-String directoryName(const String& path)
-{
-    auto filename = fileSystemRepresentation(path);
-    if (!validRepresentation(filename))
-        return String();
-
-    GUniquePtr<char> dirname(g_path_get_dirname(filename.data()));
-    return String::fromUTF8(dirname.get());
-}
-
 Vector<String> listDirectory(const String& path, const String& filter)
 {
     Vector<String> entries;

Modified: trunk/Source/WTF/wtf/posix/FileSystemPOSIX.cpp (277218 => 277219)


--- trunk/Source/WTF/wtf/posix/FileSystemPOSIX.cpp	2021-05-08 01:04:02 UTC (rev 277218)
+++ trunk/Source/WTF/wtf/posix/FileSystemPOSIX.cpp	2021-05-08 01:05:02 UTC (rev 277219)
@@ -202,21 +202,6 @@
     return builder.toString();
 }
 
-String pathGetFileName(const String& path)
-{
-    return path.substring(path.reverseFind('/') + 1);
-}
-
-String directoryName(const String& path)
-{
-    CString fsRep = fileSystemRepresentation(path);
-
-    if (!fsRep.data() || fsRep.data()[0] == '\0')
-        return String();
-
-    return String::fromUTF8(dirname(fsRep.mutableData()));
-}
-
 Vector<String> listDirectory(const String& path, const String& filter)
 {
     Vector<String> entries;

Modified: trunk/Source/WTF/wtf/win/FileSystemWin.cpp (277218 => 277219)


--- trunk/Source/WTF/wtf/win/FileSystemWin.cpp	2021-05-08 01:04:02 UTC (rev 277218)
+++ trunk/Source/WTF/wtf/win/FileSystemWin.cpp	2021-05-08 01:05:02 UTC (rev 277219)
@@ -189,22 +189,6 @@
     return "";
 }
 
-String pathGetFileName(const String& path)
-{
-    return String(::PathFindFileName(path.wideCharacters().data()));
-}
-
-String directoryName(const String& path)
-{
-    String name = path.left(path.length() - pathGetFileName(path).length());
-    if (name.characterStartingAt(name.length() - 1) == '\\'
-        || name.characterStartingAt(name.length() - 1) == '/') {
-        // Remove any trailing "\" or "/"
-        name.truncate(name.length() - 1);
-    }
-    return name;
-}
-
 static String bundleName()
 {
     static const NeverDestroyed<String> name = [] {

Modified: trunk/Tools/ChangeLog (277218 => 277219)


--- trunk/Tools/ChangeLog	2021-05-08 01:04:02 UTC (rev 277218)
+++ trunk/Tools/ChangeLog	2021-05-08 01:05:02 UTC (rev 277219)
@@ -1,3 +1,15 @@
+2021-05-07  Chris Dumez  <[email protected]>
+
+        Port FileSystem::pathGetFileName() & Filesystem::directoryName() to std::filesystem
+        https://bugs.webkit.org/show_bug.cgi?id=225524
+
+        Reviewed by Darin Adler.
+
+        Add API test coverage.
+
+        * TestWebKitAPI/Tests/WTF/FileSystem.cpp:
+        (TestWebKitAPI::TEST_F):
+
 2021-05-07  Brent Fulgham  <[email protected]>
 
         [iOS] Make AccessibilityReduceMotion test case work on iOS

Modified: trunk/Tools/TestWebKitAPI/Tests/WTF/FileSystem.cpp (277218 => 277219)


--- trunk/Tools/TestWebKitAPI/Tests/WTF/FileSystem.cpp	2021-05-08 01:04:02 UTC (rev 277218)
+++ trunk/Tools/TestWebKitAPI/Tests/WTF/FileSystem.cpp	2021-05-08 01:05:02 UTC (rev 277219)
@@ -706,4 +706,42 @@
     });
 }
 
+TEST_F(FileSystemTest, pathGetFileName)
+{
+    auto testPath = FileSystem::pathByAppendingComponents(tempEmptyFolderPath(), { "subfolder", "filename.txt" });
+    EXPECT_STREQ("filename.txt", FileSystem::pathGetFileName(testPath).utf8().data());
+
+#if OS(UNIX)
+    EXPECT_STREQ(".", FileSystem::pathGetFileName(".").utf8().data());
+    EXPECT_STREQ("..", FileSystem::pathGetFileName("..").utf8().data());
+    EXPECT_STREQ("", FileSystem::pathGetFileName("/").utf8().data());
+    EXPECT_STREQ(".", FileSystem::pathGetFileName("/foo/.").utf8().data());
+    EXPECT_STREQ("..", FileSystem::pathGetFileName("/foo/..").utf8().data());
+    EXPECT_STREQ("", FileSystem::pathGetFileName("/foo/").utf8().data());
+    EXPECT_STREQ("host", FileSystem::pathGetFileName("//host").utf8().data());
+#endif
+#if OS(WINDOWS)
+    EXPECT_STREQ("", FileSystem::pathGetFileName("C:\\").utf8().data());
+    EXPECT_STREQ("foo", FileSystem::pathGetFileName("C:\\foo").utf8().data());
+    EXPECT_STREQ("", FileSystem::pathGetFileName("C:\\foo\\").utf8().data());
+    EXPECT_STREQ("bar.txt", FileSystem::pathGetFileName("C:\\foo\\bar.txt").utf8().data());
+#endif
+}
+
+TEST_F(FileSystemTest, directoryName)
+{
+    auto testPath = FileSystem::pathByAppendingComponents(tempEmptyFolderPath(), { "subfolder", "filename.txt" });
+    EXPECT_STREQ(FileSystem::pathByAppendingComponent(tempEmptyFolderPath(), "subfolder").utf8().data(), FileSystem::directoryName(testPath).utf8().data());
+#if OS(UNIX)
+    EXPECT_STREQ("/var/tmp", FileSystem::directoryName("/var/tmp/example.txt").utf8().data());
+    EXPECT_STREQ("/var/tmp", FileSystem::directoryName("/var/tmp/").utf8().data());
+    EXPECT_STREQ("/var/tmp", FileSystem::directoryName("/var/tmp/.").utf8().data());
+    EXPECT_STREQ("/", FileSystem::directoryName("/").utf8().data());
+#endif
+#if OS(WINDOWS)
+    EXPECT_STREQ("C:\\foo", FileSystem::directoryName("C:\\foo\\example.txt").utf8().data());
+    EXPECT_STREQ("C:\\", FileSystem::directoryName("C:\\").utf8().data());
+#endif
+}
+
 } // namespace TestWebKitAPI
_______________________________________________
webkit-changes mailing list
[email protected]
https://lists.webkit.org/mailman/listinfo/webkit-changes

Reply via email to