Source/WTF/ChangeLog

 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 NOBODY (OOPS!).
 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
317 [WTF] Add holdLock() overload for WTF::DataMutex

Source/WebKit/ChangeLog

 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 NOBODY (OOPS!).
 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 Darin Adler <darin@apple.com>
215
316 Remove StringBuilder::appendNumber

Source/WTF/wtf/FileSystem.cpp

@@bool hardLinkOrCopyFile(const String& targetPath, const String& linkPath)
663663 return !ec;
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{
668675 std::error_code ec;

Source/WTF/wtf/FileSystem.h

@@WTF_EXPORT_PRIVATE bool appendFileContentsToFileHandle(const String& path, Platf
169169WTF_EXPORT_PRIVATE bool hardLink(const String& targetPath, const String& linkPath);
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)
174175WTF_EXPORT_PRIVATE bool lockFile(PlatformFileHandle, OptionSet<FileLockMode>);

Source/WebKit/NetworkProcess/cache/NetworkCacheBlobStorage.cpp

@@void BlobStorage::synchronize()
6565 if (type != DirectoryEntryType::File)
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
7879 LOG(NetworkCacheStorage, "(NetworkProcess) blob synchronization completed approximateSize=%zu", approximateSize());

@@unsigned BlobStorage::shareCount(const String& path)
141142{
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
152152}

Tools/ChangeLog

 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 NOBODY (OOPS!).
 7
 8 Add API test coverage.
 9
 10 * TestWebKitAPI/Tests/WTF/FileSystem.cpp:
 11 (TestWebKitAPI::TEST_F):
 12
1132021-05-13 Darin Adler <darin@apple.com>
214
315 Remove StringBuilder::appendNumber

Tools/TestWebKitAPI/Tests/WTF/FileSystem.cpp

@@TEST_F(FileSystemTest, createHardLinkOrCopyFile)
659659 EXPECT_EQ(linkFileSize, fileSize);
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{
664697 auto modificationTime = getFileModificationTime(path);