⚠ Archived content — this site is no longer maintained.   Current WebKit documentation is at docs.webkit.org.

Changeset 277446 in webkit


Ignore:
Timestamp:
May 13, 2021, 12:43:37 PM (5 years ago)
Author:
Chris Dumez
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):

Location:
trunk
Files:
7 edited

Legend:

Unmodified
Added
Removed
  • trunk/Source/WTF/ChangeLog

    r277438 r277446  
     12021-05-13  Chris Dumez  <cdumez@apple.com>
     2
     3        Introduce FileSystem::hardLinkCount()
     4        https://bugs.webkit.org/show_bug.cgi?id=225767
     5
     6        Reviewed by Darin Adler.
     7
     8        Introduce FileSystem::hardLinkCount() to replace our platform-specific implementation
     9        in NetworkCacheBlobStorage.
     10
     11        * wtf/FileSystem.cpp:
     12        (WTF::FileSystemImpl::hardLinkCount):
     13        * wtf/FileSystem.h:
     14
    1152021-05-13  Alicia Boya García  <aboya@igalia.com>
    216
  • trunk/Source/WTF/wtf/FileSystem.cpp

    r277357 r277446  
    664664}
    665665
     666Optional<uint64_t> hardLinkCount(const String& path)
     667{
     668    std::error_code ec;
     669    uint64_t linkCount = std::filesystem::hard_link_count(toStdFileSystemPath(path), ec);
     670    return ec ? WTF::nullopt : makeOptional(linkCount);
     671}
     672
    666673bool deleteNonEmptyDirectory(const String& path)
    667674{
  • trunk/Source/WTF/wtf/FileSystem.h

    r277357 r277446  
    170170// Hard links a file if possible, copies it if not.
    171171WTF_EXPORT_PRIVATE bool hardLinkOrCopyFile(const String& targetPath, const String& linkPath);
     172WTF_EXPORT_PRIVATE Optional<uint64_t> hardLinkCount(const String& path);
    172173
    173174#if USE(FILE_LOCK)
  • trunk/Source/WebKit/ChangeLog

    r277439 r277446  
     12021-05-13  Chris Dumez  <cdumez@apple.com>
     2
     3        Introduce FileSystem::hardLinkCount()
     4        https://bugs.webkit.org/show_bug.cgi?id=225767
     5
     6        Reviewed by Darin Adler.
     7
     8        Leverage the FileSystem API instead of having platform-specific code.
     9
     10        * NetworkProcess/cache/NetworkCacheBlobStorage.cpp:
     11        (WebKit::NetworkCache::BlobStorage::synchronize):
     12        (WebKit::NetworkCache::BlobStorage::shareCount):
     13
    1142021-05-13  Sam Weinig  <weinig@apple.com>
    215
  • trunk/Source/WebKit/NetworkProcess/cache/NetworkCacheBlobStorage.cpp

    r254703 r277446  
    6666            return;
    6767        auto path = FileSystem::pathByAppendingComponent(blobDirectory, name);
    68         auto filePath = FileSystem::fileSystemRepresentation(path);
    69         struct stat stat;
    70         ::stat(filePath.data(), &stat);
     68        auto linkCount = FileSystem::hardLinkCount(path);
    7169        // No clients left for this blob.
    72         if (stat.st_nlink == 1)
    73             unlink(filePath.data());
    74         else
    75             m_approximateSize += stat.st_size;
     70        if (linkCount && *linkCount == 1)
     71            FileSystem::deleteFile(path);
     72        else {
     73            long long fileSize = 0;
     74            FileSystem::getFileSize(path, fileSize);
     75            m_approximateSize += fileSize;
     76        }
    7677    });
    7778
     
    142143    ASSERT(!RunLoop::isMain());
    143144
    144     auto linkPath = FileSystem::fileSystemRepresentation(path);
    145     struct stat stat;
    146     if (::stat(linkPath.data(), &stat) < 0)
     145    auto linkCount = FileSystem::hardLinkCount(path);
     146    if (!linkCount)
    147147        return 0;
    148148    // Link count is 2 in the single client case (the blob file and a link).
    149     return stat.st_nlink - 1;
     149    return *linkCount - 1;
    150150}
    151151
  • trunk/Tools/ChangeLog

    r277444 r277446  
     12021-05-13  Chris Dumez  <cdumez@apple.com>
     2
     3        Introduce FileSystem::hardLinkCount()
     4        https://bugs.webkit.org/show_bug.cgi?id=225767
     5
     6        Reviewed by Darin Adler.
     7
     8        Add API test coverage.
     9
     10        * TestWebKitAPI/Tests/WTF/FileSystem.cpp:
     11        (TestWebKitAPI::TEST_F):
     12
    1132021-05-13  Jonathan Bedard  <jbedard@apple.com>
    214
  • trunk/Tools/TestWebKitAPI/Tests/WTF/FileSystem.cpp

    r277357 r277446  
    660660}
    661661
     662TEST_F(FileSystemTest, hardLinkCount)
     663{
     664    auto linkCount = FileSystem::hardLinkCount(tempFilePath());
     665    ASSERT_TRUE(!!linkCount);
     666    EXPECT_EQ(*linkCount, 1U);
     667
     668    auto hardlink1Path = FileSystem::pathByAppendingComponent(tempEmptyFolderPath(), "tempFile-hardlink1");
     669    EXPECT_TRUE(FileSystem::hardLink(tempFilePath(), hardlink1Path));
     670    linkCount = FileSystem::hardLinkCount(tempFilePath());
     671    ASSERT_TRUE(!!linkCount);
     672    EXPECT_EQ(*linkCount, 2U);
     673
     674    auto hardlink2Path = FileSystem::pathByAppendingComponent(tempEmptyFolderPath(), "tempFile-hardlink2");
     675    EXPECT_TRUE(FileSystem::hardLink(tempFilePath(), hardlink2Path));
     676    linkCount = FileSystem::hardLinkCount(tempFilePath());
     677    ASSERT_TRUE(!!linkCount);
     678    EXPECT_EQ(*linkCount, 3U);
     679
     680    EXPECT_TRUE(FileSystem::deleteFile(hardlink1Path));
     681    linkCount = FileSystem::hardLinkCount(tempFilePath());
     682    ASSERT_TRUE(!!linkCount);
     683    EXPECT_EQ(*linkCount, 2U);
     684
     685    EXPECT_TRUE(FileSystem::deleteFile(hardlink2Path));
     686    linkCount = FileSystem::hardLinkCount(tempFilePath());
     687    ASSERT_TRUE(!!linkCount);
     688    EXPECT_EQ(*linkCount, 1U);
     689
     690    EXPECT_TRUE(FileSystem::deleteFile(tempFilePath()));
     691    linkCount = FileSystem::hardLinkCount(tempFilePath());
     692    EXPECT_TRUE(!linkCount);
     693}
     694
    662695static void runGetFileModificationTimeTest(const String& path, Function<Optional<WallTime>(const String&)>&& getFileModificationTime)
    663696{
Note: See TracChangeset for help on using the changeset viewer.