Title: [277446] trunk
Revision
277446
Author
[email protected]
Date
2021-05-13 12:43:37 -0700 (Thu, 13 May 2021)

Log Message

Introduce FileSystem::hardLinkCount()
https://bugs.webkit.org/show_bug.cgi?id=225767

Reviewed by Darin Adler.

Source/WebKit:

Leverage the FileSystem API instead of having platform-specific code.

* NetworkProcess/cache/NetworkCacheBlobStorage.cpp:
(WebKit::NetworkCache::BlobStorage::synchronize):
(WebKit::NetworkCache::BlobStorage::shareCount):

Source/WTF:

Introduce FileSystem::hardLinkCount() to replace our platform-specific implementation
in NetworkCacheBlobStorage.

* wtf/FileSystem.cpp:
(WTF::FileSystemImpl::hardLinkCount):
* wtf/FileSystem.h:

Tools:

Add API test coverage.

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

Modified Paths

Diff

Modified: trunk/Source/WTF/ChangeLog (277445 => 277446)


--- trunk/Source/WTF/ChangeLog	2021-05-13 19:38:44 UTC (rev 277445)
+++ trunk/Source/WTF/ChangeLog	2021-05-13 19:43:37 UTC (rev 277446)
@@ -1,3 +1,17 @@
+2021-05-13  Chris Dumez  <[email protected]>
+
+        Introduce FileSystem::hardLinkCount()
+        https://bugs.webkit.org/show_bug.cgi?id=225767
+
+        Reviewed by Darin Adler.
+
+        Introduce FileSystem::hardLinkCount() to replace our platform-specific implementation
+        in NetworkCacheBlobStorage.
+
+        * wtf/FileSystem.cpp:
+        (WTF::FileSystemImpl::hardLinkCount):
+        * wtf/FileSystem.h:
+
 2021-05-13  Alicia Boya GarcĂ­a  <[email protected]>
 
         [WTF] Add holdLock() overload for WTF::DataMutex

Modified: trunk/Source/WTF/wtf/FileSystem.cpp (277445 => 277446)


--- trunk/Source/WTF/wtf/FileSystem.cpp	2021-05-13 19:38:44 UTC (rev 277445)
+++ trunk/Source/WTF/wtf/FileSystem.cpp	2021-05-13 19:43:37 UTC (rev 277446)
@@ -663,6 +663,13 @@
     return !ec;
 }
 
+Optional<uint64_t> hardLinkCount(const String& path)
+{
+    std::error_code ec;
+    uint64_t linkCount = std::filesystem::hard_link_count(toStdFileSystemPath(path), ec);
+    return ec ? WTF::nullopt : makeOptional(linkCount);
+}
+
 bool deleteNonEmptyDirectory(const String& path)
 {
     std::error_code ec;

Modified: trunk/Source/WTF/wtf/FileSystem.h (277445 => 277446)


--- trunk/Source/WTF/wtf/FileSystem.h	2021-05-13 19:38:44 UTC (rev 277445)
+++ trunk/Source/WTF/wtf/FileSystem.h	2021-05-13 19:43:37 UTC (rev 277446)
@@ -169,6 +169,7 @@
 WTF_EXPORT_PRIVATE bool hardLink(const String& targetPath, const String& linkPath);
 // Hard links a file if possible, copies it if not.
 WTF_EXPORT_PRIVATE bool hardLinkOrCopyFile(const String& targetPath, const String& linkPath);
+WTF_EXPORT_PRIVATE Optional<uint64_t> hardLinkCount(const String& path);
 
 #if USE(FILE_LOCK)
 WTF_EXPORT_PRIVATE bool lockFile(PlatformFileHandle, OptionSet<FileLockMode>);

Modified: trunk/Source/WebKit/ChangeLog (277445 => 277446)


--- trunk/Source/WebKit/ChangeLog	2021-05-13 19:38:44 UTC (rev 277445)
+++ trunk/Source/WebKit/ChangeLog	2021-05-13 19:43:37 UTC (rev 277446)
@@ -1,3 +1,16 @@
+2021-05-13  Chris Dumez  <[email protected]>
+
+        Introduce FileSystem::hardLinkCount()
+        https://bugs.webkit.org/show_bug.cgi?id=225767
+
+        Reviewed by Darin Adler.
+
+        Leverage the FileSystem API instead of having platform-specific code.
+
+        * NetworkProcess/cache/NetworkCacheBlobStorage.cpp:
+        (WebKit::NetworkCache::BlobStorage::synchronize):
+        (WebKit::NetworkCache::BlobStorage::shareCount):
+
 2021-05-13  Sam Weinig  <[email protected]>
 
         CGDisplayList debug configuration fails to build

Modified: trunk/Source/WebKit/NetworkProcess/cache/NetworkCacheBlobStorage.cpp (277445 => 277446)


--- trunk/Source/WebKit/NetworkProcess/cache/NetworkCacheBlobStorage.cpp	2021-05-13 19:38:44 UTC (rev 277445)
+++ trunk/Source/WebKit/NetworkProcess/cache/NetworkCacheBlobStorage.cpp	2021-05-13 19:43:37 UTC (rev 277446)
@@ -65,14 +65,15 @@
         if (type != DirectoryEntryType::File)
             return;
         auto path = FileSystem::pathByAppendingComponent(blobDirectory, name);
-        auto filePath = FileSystem::fileSystemRepresentation(path);
-        struct stat stat;
-        ::stat(filePath.data(), &stat);
+        auto linkCount = FileSystem::hardLinkCount(path);
         // No clients left for this blob.
-        if (stat.st_nlink == 1)
-            unlink(filePath.data());
-        else
-            m_approximateSize += stat.st_size;
+        if (linkCount && *linkCount == 1)
+            FileSystem::deleteFile(path);
+        else {
+            long long fileSize = 0;
+            FileSystem::getFileSize(path, fileSize);
+            m_approximateSize += fileSize;
+        }
     });
 
     LOG(NetworkCacheStorage, "(NetworkProcess) blob synchronization completed approximateSize=%zu", approximateSize());
@@ -141,12 +142,11 @@
 {
     ASSERT(!RunLoop::isMain());
 
-    auto linkPath = FileSystem::fileSystemRepresentation(path);
-    struct stat stat;
-    if (::stat(linkPath.data(), &stat) < 0)
+    auto linkCount = FileSystem::hardLinkCount(path);
+    if (!linkCount)
         return 0;
     // Link count is 2 in the single client case (the blob file and a link).
-    return stat.st_nlink - 1;
+    return *linkCount - 1;
 }
 
 }

Modified: trunk/Tools/ChangeLog (277445 => 277446)


--- trunk/Tools/ChangeLog	2021-05-13 19:38:44 UTC (rev 277445)
+++ trunk/Tools/ChangeLog	2021-05-13 19:43:37 UTC (rev 277446)
@@ -1,3 +1,15 @@
+2021-05-13  Chris Dumez  <[email protected]>
+
+        Introduce FileSystem::hardLinkCount()
+        https://bugs.webkit.org/show_bug.cgi?id=225767
+
+        Reviewed by Darin Adler.
+
+        Add API test coverage.
+
+        * TestWebKitAPI/Tests/WTF/FileSystem.cpp:
+        (TestWebKitAPI::TEST_F):
+
 2021-05-13  Jonathan Bedard  <[email protected]>
 
         [run-api-tests] Use Python 3 (Part 1)

Modified: trunk/Tools/TestWebKitAPI/Tests/WTF/FileSystem.cpp (277445 => 277446)


--- trunk/Tools/TestWebKitAPI/Tests/WTF/FileSystem.cpp	2021-05-13 19:38:44 UTC (rev 277445)
+++ trunk/Tools/TestWebKitAPI/Tests/WTF/FileSystem.cpp	2021-05-13 19:43:37 UTC (rev 277446)
@@ -659,6 +659,39 @@
     EXPECT_EQ(linkFileSize, fileSize);
 }
 
+TEST_F(FileSystemTest, hardLinkCount)
+{
+    auto linkCount = FileSystem::hardLinkCount(tempFilePath());
+    ASSERT_TRUE(!!linkCount);
+    EXPECT_EQ(*linkCount, 1U);
+
+    auto hardlink1Path = FileSystem::pathByAppendingComponent(tempEmptyFolderPath(), "tempFile-hardlink1");
+    EXPECT_TRUE(FileSystem::hardLink(tempFilePath(), hardlink1Path));
+    linkCount = FileSystem::hardLinkCount(tempFilePath());
+    ASSERT_TRUE(!!linkCount);
+    EXPECT_EQ(*linkCount, 2U);
+
+    auto hardlink2Path = FileSystem::pathByAppendingComponent(tempEmptyFolderPath(), "tempFile-hardlink2");
+    EXPECT_TRUE(FileSystem::hardLink(tempFilePath(), hardlink2Path));
+    linkCount = FileSystem::hardLinkCount(tempFilePath());
+    ASSERT_TRUE(!!linkCount);
+    EXPECT_EQ(*linkCount, 3U);
+
+    EXPECT_TRUE(FileSystem::deleteFile(hardlink1Path));
+    linkCount = FileSystem::hardLinkCount(tempFilePath());
+    ASSERT_TRUE(!!linkCount);
+    EXPECT_EQ(*linkCount, 2U);
+
+    EXPECT_TRUE(FileSystem::deleteFile(hardlink2Path));
+    linkCount = FileSystem::hardLinkCount(tempFilePath());
+    ASSERT_TRUE(!!linkCount);
+    EXPECT_EQ(*linkCount, 1U);
+
+    EXPECT_TRUE(FileSystem::deleteFile(tempFilePath()));
+    linkCount = FileSystem::hardLinkCount(tempFilePath());
+    EXPECT_TRUE(!linkCount);
+}
+
 static void runGetFileModificationTimeTest(const String& path, Function<Optional<WallTime>(const String&)>&& getFileModificationTime)
 {
     auto modificationTime = getFileModificationTime(path);
_______________________________________________
webkit-changes mailing list
[email protected]
https://lists.webkit.org/mailman/listinfo/webkit-changes

Reply via email to