Title: [286883] trunk
Revision
286883
Author
[email protected]
Date
2021-12-10 16:35:24 -0800 (Fri, 10 Dec 2021)

Log Message

Add FileSystem function to read a file at a path
https://bugs.webkit.org/show_bug.cgi?id=234103

Reviewed by Alex Christensen.

Source/_javascript_Core:

Use FileSystem::readEntireFile.

* inspector/remote/socket/RemoteInspectorSocket.cpp:
(Inspector::RemoteInspector::backendCommands const):

Source/WebCore:

Use FileSystem::readEntireFile.

* platform/network/curl/CurlCacheEntry.cpp:
(WebCore::CurlCacheEntry::readCachedData):
(WebCore::CurlCacheEntry::loadResponseHeaders):
(WebCore::CurlCacheEntry::loadFileToBuffer): Deleted.
* platform/network/curl/CurlCacheEntry.h:
* platform/network/curl/CurlCacheManager.cpp:
(WebCore::CurlCacheManager::loadIndex):
* rendering/RenderThemeWin.cpp:
(WebCore::RenderThemeWin::stringWithContentsOfFile):
(WebCore::fillBufferWithContentsOfFile): Deleted.

Source/WTF:

Add FileSystem::readEntireFile which takes a path and attempts to read the whole contents
of the file into a Vector<uint8_t>. If the file is not found or is empty then it returns
nullopt. Internally it manages the opening and closing of the file to prevent file handles
from leaking.

Modify FileSystem::readEntireFile which takes a handle to continue reading until the entire
file has been read. Previously it could've just done a partial read as
FileSystem::readFromFile does not guarantee it will read all the bytes requested.

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

Tools:

Add tests for readEntireFile.

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

Modified Paths

Diff

Modified: trunk/Source/_javascript_Core/ChangeLog (286882 => 286883)


--- trunk/Source/_javascript_Core/ChangeLog	2021-12-10 23:17:11 UTC (rev 286882)
+++ trunk/Source/_javascript_Core/ChangeLog	2021-12-11 00:35:24 UTC (rev 286883)
@@ -1,3 +1,15 @@
+2021-12-10  Don Olmstead  <[email protected]>
+
+        Add FileSystem function to read a file at a path
+        https://bugs.webkit.org/show_bug.cgi?id=234103
+
+        Reviewed by Alex Christensen.
+
+        Use FileSystem::readEntireFile.
+
+        * inspector/remote/socket/RemoteInspectorSocket.cpp:
+        (Inspector::RemoteInspector::backendCommands const):
+
 2021-12-10  Tadeu Zagallo  <[email protected]>
 
         Remove Mac-specific ARM64EHash implementation

Modified: trunk/Source/_javascript_Core/inspector/remote/socket/RemoteInspectorSocket.cpp (286882 => 286883)


--- trunk/Source/_javascript_Core/inspector/remote/socket/RemoteInspectorSocket.cpp	2021-12-10 23:17:11 UTC (rev 286882)
+++ trunk/Source/_javascript_Core/inspector/remote/socket/RemoteInspectorSocket.cpp	2021-12-11 00:35:24 UTC (rev 286883)
@@ -263,19 +263,9 @@
     if (m_backendCommandsPath.isEmpty())
         return { };
 
-    auto handle = FileSystem::openFile(m_backendCommandsPath, FileSystem::FileOpenMode::Read);
-    if (!FileSystem::isHandleValid(handle))
-        return { };
+    auto contents = FileSystem::readEntireFile(m_backendCommandsPath);
 
-    String result;
-    if (auto size = FileSystem::fileSize(handle)) {
-        Vector<LChar> buffer(*size);
-        int bytesRead = FileSystem::readFromFile(handle, buffer.data(), *size);
-        if (bytesRead >= 0 && static_cast<uint64_t>(bytesRead) == *size)
-            result = String::adopt(WTFMove(buffer));
-    }
-    FileSystem::closeFile(handle);
-    return result;
+    return contents ? String::adopt(WTFMove(*contents)) : emptyString();
 }
 
 // RemoteInspectorConnectionClient handlers

Modified: trunk/Source/WTF/ChangeLog (286882 => 286883)


--- trunk/Source/WTF/ChangeLog	2021-12-10 23:17:11 UTC (rev 286882)
+++ trunk/Source/WTF/ChangeLog	2021-12-11 00:35:24 UTC (rev 286883)
@@ -1,3 +1,23 @@
+2021-12-10  Don Olmstead  <[email protected]>
+
+        Add FileSystem function to read a file at a path
+        https://bugs.webkit.org/show_bug.cgi?id=234103
+
+        Reviewed by Alex Christensen.
+
+        Add FileSystem::readEntireFile which takes a path and attempts to read the whole contents
+        of the file into a Vector<uint8_t>. If the file is not found or is empty then it returns
+        nullopt. Internally it manages the opening and closing of the file to prevent file handles
+        from leaking.
+
+        Modify FileSystem::readEntireFile which takes a handle to continue reading until the entire
+        file has been read. Previously it could've just done a partial read as
+        FileSystem::readFromFile does not guarantee it will read all the bytes requested.
+
+        * wtf/FileSystem.cpp:
+        (WTF::FileSystemImpl::readEntireFile):
+        * wtf/FileSystem.h:
+
 2021-12-10  Antoine Quint  <[email protected]>
 
         Expose the maximum device frame rate to the Web Animations model

Modified: trunk/Source/WTF/wtf/FileSystem.cpp (286882 => 286883)


--- trunk/Source/WTF/wtf/FileSystem.cpp	2021-12-10 23:17:11 UTC (rev 286882)
+++ trunk/Source/WTF/wtf/FileSystem.cpp	2021-12-11 00:35:24 UTC (rev 286883)
@@ -523,12 +523,17 @@
     if (!size)
         return std::nullopt;
 
-    unsigned bytesToRead;
+    size_t bytesToRead;
     if (!WTF::convertSafely(size, bytesToRead))
         return std::nullopt;
 
     Vector<uint8_t> buffer(bytesToRead);
-    unsigned totalBytesRead = FileSystem::readFromFile(handle, buffer.data(), buffer.size());
+    size_t totalBytesRead = 0;
+    int bytesRead;
+
+    while ((bytesRead = FileSystem::readFromFile(handle, buffer.data() + totalBytesRead, bytesToRead - totalBytesRead)) > 0)
+        totalBytesRead += bytesRead;
+
     if (totalBytesRead != bytesToRead)
         return std::nullopt;
 
@@ -535,6 +540,15 @@
     return buffer;
 }
 
+std::optional<Vector<uint8_t>> readEntireFile(const String& path)
+{
+    auto handle = FileSystem::openFile(path, FileSystem::FileOpenMode::Read);
+    auto contents = readEntireFile(handle);
+    FileSystem::closeFile(handle);
+
+    return contents;
+}
+
 void deleteAllFilesModifiedSince(const String& directory, WallTime time)
 {
     // This function may delete directory folder.

Modified: trunk/Source/WTF/wtf/FileSystem.h (286882 => 286883)


--- trunk/Source/WTF/wtf/FileSystem.h	2021-12-10 23:17:11 UTC (rev 286882)
+++ trunk/Source/WTF/wtf/FileSystem.h	2021-12-11 00:35:24 UTC (rev 286883)
@@ -148,6 +148,7 @@
 using Salt = std::array<uint8_t, 8>;
 WTF_EXPORT_PRIVATE std::optional<Salt> readOrMakeSalt(const String& path);
 WTF_EXPORT_PRIVATE std::optional<Vector<uint8_t>> readEntireFile(PlatformFileHandle);
+WTF_EXPORT_PRIVATE std::optional<Vector<uint8_t>> readEntireFile(const String& path);
 
 // Prefix is what the filename should be prefixed with, not the full path.
 WTF_EXPORT_PRIVATE String openTemporaryFile(const String& prefix, PlatformFileHandle&, const String& suffix = { });

Modified: trunk/Source/WebCore/ChangeLog (286882 => 286883)


--- trunk/Source/WebCore/ChangeLog	2021-12-10 23:17:11 UTC (rev 286882)
+++ trunk/Source/WebCore/ChangeLog	2021-12-11 00:35:24 UTC (rev 286883)
@@ -1,3 +1,23 @@
+2021-12-10  Don Olmstead  <[email protected]>
+
+        Add FileSystem function to read a file at a path
+        https://bugs.webkit.org/show_bug.cgi?id=234103
+
+        Reviewed by Alex Christensen.
+
+        Use FileSystem::readEntireFile.
+
+        * platform/network/curl/CurlCacheEntry.cpp:
+        (WebCore::CurlCacheEntry::readCachedData):
+        (WebCore::CurlCacheEntry::loadResponseHeaders):
+        (WebCore::CurlCacheEntry::loadFileToBuffer): Deleted.
+        * platform/network/curl/CurlCacheEntry.h:
+        * platform/network/curl/CurlCacheManager.cpp:
+        (WebCore::CurlCacheManager::loadIndex):
+        * rendering/RenderThemeWin.cpp:
+        (WebCore::RenderThemeWin::stringWithContentsOfFile):
+        (WebCore::fillBufferWithContentsOfFile): Deleted.
+
 2021-12-10  Antoine Quint  <[email protected]>
 
         Expose the maximum device frame rate to the Web Animations model

Modified: trunk/Source/WebCore/platform/network/curl/CurlCacheEntry.cpp (286882 => 286883)


--- trunk/Source/WebCore/platform/network/curl/CurlCacheEntry.cpp	2021-12-10 23:17:11 UTC (rev 286882)
+++ trunk/Source/WebCore/platform/network/curl/CurlCacheEntry.cpp	2021-12-11 00:35:24 UTC (rev 286883)
@@ -114,12 +114,14 @@
 {
     ASSERT(job->client());
 
-    Vector<uint8_t> buffer;
-    if (!loadFileToBuffer(m_contentFilename, buffer))
+    auto buffer = FileSystem::readEntireFile(m_contentFilename);
+    if (!buffer) {
+        LOG(Network, "Cache Error: Could not open %s to read cached content\n", m_contentFilename.latin1().data());
         return false;
+    }
 
-    if (auto bufferSize = buffer.size())
-        job->getInternal()->client()->didReceiveBuffer(job, SharedBuffer::create(WTFMove(buffer)), bufferSize);
+    if (auto bufferSize = buffer->size())
+        job->getInternal()->client()->didReceiveBuffer(job, SharedBuffer::create(WTFMove(*buffer)), bufferSize);
 
     return true;
 }
@@ -152,11 +154,13 @@
 
 bool CurlCacheEntry::loadResponseHeaders()
 {
-    Vector<uint8_t> buffer;
-    if (!loadFileToBuffer(m_headerFilename, buffer))
+    auto buffer = FileSystem::readEntireFile(m_headerFilename);
+    if (!buffer) {
+        LOG(Network, "Cache Error: Could not open %s to read cached headers\n", m_headerFilename.latin1().data());
         return false;
+    }
 
-    String headerContent = String(buffer.data(), buffer.size());
+    String headerContent = String::adopt(WTFMove(*buffer));
     Vector<String> headerFields = headerContent.split('\n');
 
     Vector<String>::const_iterator it = headerFields.begin();
@@ -224,44 +228,6 @@
     m_basename = baseNameBuilder.toString();
 }
 
-bool CurlCacheEntry::loadFileToBuffer(const String& filepath, Vector<uint8_t>& buffer)
-{
-    // Open the file
-    FileSystem::PlatformFileHandle inputFile = FileSystem::openFile(filepath, FileSystem::FileOpenMode::Read);
-    if (!FileSystem::isHandleValid(inputFile)) {
-        LOG(Network, "Cache Error: Could not open %s for read\n", filepath.latin1().data());
-        return false;
-    }
-
-    auto filesize = FileSystem::fileSize(filepath);
-    if (!filesize) {
-        LOG(Network, "Cache Error: Could not get file size of %s\n", filepath.latin1().data());
-        FileSystem::closeFile(inputFile);
-        return false;
-    }
-
-    // Load the file content into buffer
-    buffer.resize(*filesize);
-    int bufferPosition = 0;
-    int bufferReadSize = 4096;
-    int bytesRead = 0;
-    while (*filesize > bufferPosition) {
-        if (*filesize - bufferPosition < bufferReadSize)
-            bufferReadSize = *filesize - bufferPosition;
-
-        bytesRead = FileSystem::readFromFile(inputFile, buffer.data() + bufferPosition, bufferReadSize);
-        if (bytesRead != bufferReadSize) {
-            LOG(Network, "Cache Error: Could not read from %s\n", filepath.latin1().data());
-            FileSystem::closeFile(inputFile);
-            return false;
-        }
-
-        bufferPosition += bufferReadSize;
-    }
-    FileSystem::closeFile(inputFile);
-    return true;
-}
-
 void CurlCacheEntry::invalidate()
 {
     closeContentFile();

Modified: trunk/Source/WebCore/platform/network/curl/CurlCacheEntry.h (286882 => 286883)


--- trunk/Source/WebCore/platform/network/curl/CurlCacheEntry.h	2021-12-10 23:17:11 UTC (rev 286882)
+++ trunk/Source/WebCore/platform/network/curl/CurlCacheEntry.h	2021-12-11 00:35:24 UTC (rev 286883)
@@ -89,7 +89,6 @@
     ResourceHandle* m_job;
 
     void generateBaseFilename(const CString& url);
-    bool loadFileToBuffer(const String& filepath, Vector<uint8_t>& buffer);
     bool loadResponseHeaders();
 
     bool openContentFile();

Modified: trunk/Source/WebCore/platform/network/curl/CurlCacheManager.cpp (286882 => 286883)


--- trunk/Source/WebCore/platform/network/curl/CurlCacheManager.cpp	2021-12-10 23:17:11 UTC (rev 286882)
+++ trunk/Source/WebCore/platform/network/curl/CurlCacheManager.cpp	2021-12-11 00:35:24 UTC (rev 286883)
@@ -101,40 +101,16 @@
     if (m_disabled)
         return;
 
-    String indexFilePath(m_cacheDir);
-    indexFilePath.append("index.dat");
-
-    FileSystem::PlatformFileHandle indexFile = FileSystem::openFile(indexFilePath, FileSystem::FileOpenMode::Read);
-    if (!FileSystem::isHandleValid(indexFile)) {
-        LOG(Network, "Cache Warning: Could not open %s for read\n", indexFilePath.latin1().data());
+    String indexFilePath = FileSystem::pathByAppendingComponent(m_cacheDir, "index.dat"_s);
+    auto buffer = FileSystem::readEntireFile(indexFilePath);
+    if (!buffer) {
+        LOG(Network, "Cache Error: Could not read %s\n", indexFilePath.latin1().data());
         return;
     }
 
-    auto filesize = FileSystem::fileSize(indexFilePath);
-    if (!filesize) {
-        LOG(Network, "Cache Error: Could not get file size of %s\n", indexFilePath.latin1().data());
-        FileSystem::closeFile(indexFile);
-        return;
-    }
-
-    // Load the file content into buffer
-    Vector<char> buffer;
-    buffer.resize(*filesize);
-    int bufferPosition = 0;
-    int bufferReadSize = IO_BUFFERSIZE;
-    while (*filesize > bufferPosition) {
-        if (*filesize - bufferPosition < bufferReadSize)
-            bufferReadSize = *filesize - bufferPosition;
-
-        FileSystem::readFromFile(indexFile, buffer.data() + bufferPosition, bufferReadSize);
-        bufferPosition += bufferReadSize;
-    }
-    FileSystem::closeFile(indexFile);
-
     // Create strings from buffer
-    String headerContent = String(buffer.data(), buffer.size());
+    auto headerContent = String::adopt(WTFMove(*buffer));
     Vector<String> indexURLs = headerContent.split('\n');
-    buffer.clear();
 
     // Add entries to index
     Vector<String>::const_iterator it = indexURLs.begin();

Modified: trunk/Source/WebCore/rendering/RenderThemeWin.cpp (286882 => 286883)


--- trunk/Source/WebCore/rendering/RenderThemeWin.cpp	2021-12-10 23:17:11 UTC (rev 286882)
+++ trunk/Source/WebCore/rendering/RenderThemeWin.cpp	2021-12-11 00:35:24 UTC (rev 286883)
@@ -1020,30 +1020,6 @@
 }
 
 #if ENABLE(VIDEO)
-static void fillBufferWithContentsOfFile(FileSystem::PlatformFileHandle file, long long filesize, Vector<uint8_t>& buffer)
-{
-    // Load the file content into buffer
-    buffer.resize(filesize + 1);
-
-    int bufferPosition = 0;
-    int bufferReadSize = 4096;
-    int bytesRead = 0;
-    while (filesize > bufferPosition) {
-        if (filesize - bufferPosition < bufferReadSize)
-            bufferReadSize = filesize - bufferPosition;
-
-        bytesRead = FileSystem::readFromFile(file, buffer.data() + bufferPosition, bufferReadSize);
-        if (bytesRead != bufferReadSize) {
-            buffer.clear();
-            return;
-        }
-
-        bufferPosition += bufferReadSize;
-    }
-
-    buffer[filesize] = 0;
-}
-
 String RenderThemeWin::stringWithContentsOfFile(const String& name, const String& type)
 {
 #if USE(CF)
@@ -1055,21 +1031,9 @@
     if (!CFURLGetFileSystemRepresentation(requestedURLRef.get(), false, requestedFilePath, MAX_PATH))
         return String();
 
-    FileSystem::PlatformFileHandle requestedFileHandle = FileSystem::openFile(requestedFilePath, FileSystem::FileOpenMode::Read);
-    if (!FileSystem::isHandleValid(requestedFileHandle))
-        return String();
+    auto contents = FileSystem::readEntireFile(requestedFilePath);
 
-    auto filesize = FileSystem::fileSize(requestedFileHandle);
-    if (!filesize) {
-        FileSystem::closeFile(requestedFileHandle);
-        return String();
-    }
-
-    Vector<uint8_t> fileContents;
-    fillBufferWithContentsOfFile(requestedFileHandle, *filesize, fileContents);
-    FileSystem::closeFile(requestedFileHandle);
-
-    return String(fileContents.data(), *filesize);
+    return contents ? String::adopt(WTFMove(*contents)) : String();
 #else
     return emptyString();
 #endif

Modified: trunk/Tools/ChangeLog (286882 => 286883)


--- trunk/Tools/ChangeLog	2021-12-10 23:17:11 UTC (rev 286882)
+++ trunk/Tools/ChangeLog	2021-12-11 00:35:24 UTC (rev 286883)
@@ -1,3 +1,15 @@
+2021-12-10  Don Olmstead  <[email protected]>
+
+        Add FileSystem function to read a file at a path
+        https://bugs.webkit.org/show_bug.cgi?id=234103
+
+        Reviewed by Alex Christensen.
+
+        Add tests for readEntireFile.
+
+        * TestWebKitAPI/Tests/WTF/FileSystem.cpp:
+        (TestWebKitAPI::TEST_F):
+
 2021-12-10  Alex Christensen  <[email protected]>
 
         Move if-domain and unless-domain conversion to WKContentRuleList parsing

Modified: trunk/Tools/TestWebKitAPI/Tests/WTF/FileSystem.cpp (286882 => 286883)


--- trunk/Tools/TestWebKitAPI/Tests/WTF/FileSystem.cpp	2021-12-10 23:17:11 UTC (rev 286882)
+++ trunk/Tools/TestWebKitAPI/Tests/WTF/FileSystem.cpp	2021-12-11 00:35:24 UTC (rev 286883)
@@ -819,4 +819,17 @@
     EXPECT_STREQ(FileSystem::realPath(FileSystem::pathByAppendingComponents(subFolderPath, { "..", ".", ".", "subfolder" })).utf8().data(), resolvedSubFolderPath.utf8().data()); // Should resolve ".." and "."
 }
 
+TEST_F(FileSystemTest, readEntireFile)
+{
+    EXPECT_FALSE(FileSystem::readEntireFile(FileSystem::invalidPlatformFileHandle));
+    EXPECT_FALSE(FileSystem::readEntireFile(emptyString()));
+    EXPECT_FALSE(FileSystem::readEntireFile(FileSystem::pathByAppendingComponent(tempEmptyFolderPath(), "does-not-exist")));
+    EXPECT_FALSE(FileSystem::readEntireFile(tempEmptyFilePath()));
+
+    auto buffer = FileSystem::readEntireFile(tempFilePath());
+    EXPECT_TRUE(buffer);
+    auto contents = String::adopt(WTFMove(buffer.value()));
+    EXPECT_STREQ(contents.utf8().data(), FileSystemTestData);
+}
+
 } // namespace TestWebKitAPI
_______________________________________________
webkit-changes mailing list
[email protected]
https://lists.webkit.org/mailman/listinfo/webkit-changes

Reply via email to