Title: [289996] trunk
Revision
289996
Author
sihui_...@apple.com
Date
2022-02-16 20:25:14 -0800 (Wed, 16 Feb 2022)

Log Message

Use FileSystem::readEntireFile everywhere
https://bugs.webkit.org/show_bug.cgi?id=233818
<rdar://problem/86324895>

Reviewed by Chris Dumez.

Source/WebKit:

* NetworkProcess/cache/CacheStorageEngine.cpp:
(WebKit::CacheStorage::Engine::readSizeFile):
* Shared/PersistencyUtils.cpp:
(WebKit::createForFile):

Tools:

* TestWebKitAPI/Tests/WebCore/FileMonitor.cpp:
(TestWebKitAPI::readContentsOfFile):

Modified Paths

Diff

Modified: trunk/Source/WebKit/ChangeLog (289995 => 289996)


--- trunk/Source/WebKit/ChangeLog	2022-02-17 04:23:29 UTC (rev 289995)
+++ trunk/Source/WebKit/ChangeLog	2022-02-17 04:25:14 UTC (rev 289996)
@@ -1,3 +1,16 @@
+2022-02-16  Sihui Liu  <sihui_...@apple.com>
+
+        Use FileSystem::readEntireFile everywhere
+        https://bugs.webkit.org/show_bug.cgi?id=233818
+        <rdar://problem/86324895>
+
+        Reviewed by Chris Dumez.
+
+        * NetworkProcess/cache/CacheStorageEngine.cpp:
+        (WebKit::CacheStorage::Engine::readSizeFile):
+        * Shared/PersistencyUtils.cpp:
+        (WebKit::createForFile):
+
 2022-02-16  Per Arne Vollan  <pvol...@apple.com>
 
         Send icons to the WebContent process for rendering of the attachment element

Modified: trunk/Source/WebKit/NetworkProcess/cache/CacheStorageEngine.cpp (289995 => 289996)


--- trunk/Source/WebKit/NetworkProcess/cache/CacheStorageEngine.cpp	2022-02-17 04:23:29 UTC (rev 289995)
+++ trunk/Source/WebKit/NetworkProcess/cache/CacheStorageEngine.cpp	2022-02-17 04:25:14 UTC (rev 289996)
@@ -549,29 +549,11 @@
     ASSERT(!RunLoop::isMain());
 
     Locker locker { globalSizeFileLock };
-    auto fileHandle = FileSystem::openFile(path, FileSystem::FileOpenMode::Read);
-    auto closeFileHandle = makeScopeExit([&] {
-        FileSystem::closeFile(fileHandle);
-    });
-
-    if (!FileSystem::isHandleValid(fileHandle))
+    auto buffer = FileSystem::readEntireFile(path);
+    if (!buffer)
         return std::nullopt;
 
-    auto fileSize = FileSystem::fileSize(path).value_or(0);
-    if (!fileSize)
-        return std::nullopt;
-
-    unsigned bytesToRead;
-    if (!WTF::convertSafely(fileSize, bytesToRead))
-        return std::nullopt;
-
-    // FIXME: No reason we need a heap buffer to read an arbitrary number of bytes when we only support small files that contain numerals.
-    Vector<char> buffer(bytesToRead);
-    unsigned totalBytesRead = FileSystem::readFromFile(fileHandle, buffer.data(), buffer.size());
-    if (totalBytesRead != bytesToRead)
-        return std::nullopt;
-
-    return parseInteger<uint64_t>({ buffer.data(), totalBytesRead });
+    return parseInteger<uint64_t>({ buffer->data(), static_cast<unsigned>(buffer->size()) });
 }
 
 class ReadOriginsTaskCounter : public RefCounted<ReadOriginsTaskCounter> {

Modified: trunk/Source/WebKit/Shared/PersistencyUtils.cpp (289995 => 289996)


--- trunk/Source/WebKit/Shared/PersistencyUtils.cpp	2022-02-17 04:23:29 UTC (rev 289995)
+++ trunk/Source/WebKit/Shared/PersistencyUtils.cpp	2022-02-17 04:25:14 UTC (rev 289996)
@@ -39,32 +39,11 @@
 {
     ASSERT(!RunLoop::isMain());
 
-    auto handle = FileSystem::openAndLockFile(path, FileSystem::FileOpenMode::Read);
-    if (handle == FileSystem::invalidPlatformFileHandle)
+    auto buffer = FileSystem::readEntireFile(path);
+    if (!buffer)
         return nullptr;
 
-    auto fileSize = FileSystem::fileSize(handle).value_or(0);
-    if (!fileSize) {
-        FileSystem::unlockAndCloseFile(handle);
-        return nullptr;
-    }
-
-    size_t bytesToRead;
-    if (!WTF::convertSafely(fileSize, bytesToRead)) {
-        FileSystem::unlockAndCloseFile(handle);
-        return nullptr;
-    }
-
-    Vector<uint8_t> buffer(bytesToRead);
-    size_t totalBytesRead = FileSystem::readFromFile(handle, buffer.data(), buffer.size());
-
-    FileSystem::unlockAndCloseFile(handle);
-
-    if (totalBytesRead != bytesToRead)
-        return nullptr;
-
-    // FIXME: We should try to modify the constructor to pass &&.
-    return KeyedDecoder::decoder(buffer.data(), buffer.size());
+    return KeyedDecoder::decoder(buffer->data(), buffer->size());
 }
 
 void writeToDisk(std::unique_ptr<KeyedEncoder>&& encoder, String&& path)

Modified: trunk/Tools/ChangeLog (289995 => 289996)


--- trunk/Tools/ChangeLog	2022-02-17 04:23:29 UTC (rev 289995)
+++ trunk/Tools/ChangeLog	2022-02-17 04:25:14 UTC (rev 289996)
@@ -1,3 +1,14 @@
+2022-02-16  Sihui Liu  <sihui_...@apple.com>
+
+        Use FileSystem::readEntireFile everywhere
+        https://bugs.webkit.org/show_bug.cgi?id=233818
+        <rdar://problem/86324895>
+
+        Reviewed by Chris Dumez.
+
+        * TestWebKitAPI/Tests/WebCore/FileMonitor.cpp:
+        (TestWebKitAPI::readContentsOfFile):
+
 2022-02-16  Jonathan Bedard  <jbed...@apple.com>
 
         Broken pipes during iOS simulator testing

Modified: trunk/Tools/TestWebKitAPI/Tests/WebCore/FileMonitor.cpp (289995 => 289996)


--- trunk/Tools/TestWebKitAPI/Tests/WebCore/FileMonitor.cpp	2022-02-17 04:23:29 UTC (rev 289995)
+++ trunk/Tools/TestWebKitAPI/Tests/WebCore/FileMonitor.cpp	2022-02-17 04:25:14 UTC (rev 289996)
@@ -106,32 +106,15 @@
 
 static String readContentsOfFile(const String& path)
 {
-    constexpr int bufferSize = 1024;
-
-    auto source = FileSystem::openFile(path, FileSystem::FileOpenMode::Read);
-    if (!FileSystem::isHandleValid(source))
+    auto buffer = FileSystem::readEntireFile(path);
+    if (!buffer)
         return emptyString();
 
-    StringBuffer<LChar> buffer(bufferSize);
+    String result(static_cast<const LChar*>(buffer->data()), buffer->size());
+    if (result.endsWith("\n"))
+        return result.substring(0, result.length() - 1);
 
-    auto fileCloser = WTF::makeScopeExit([source]() {
-        FileSystem::PlatformFileHandle handle = source;
-        FileSystem::closeFile(handle);
-    });
-
-    // Since we control the test files, we know we only need one read
-    int readBytes = FileSystem::readFromFile(source, buffer.characters(), bufferSize);
-    if (readBytes < 0)
-        return emptyString();
-
-    // Strip the trailing carriage return from the file:
-    if (readBytes > 1) {
-        int lastByte = readBytes - 1;
-        if (buffer[lastByte] == '\n')
-            buffer.shrink(lastByte);
-    }
-    ASSERT(readBytes < bufferSize);
-    return String::adopt(WTFMove(buffer));
+    return result;
 }
 
 TEST_F(FileMonitorTest, DetectChange)
_______________________________________________
webkit-changes mailing list
webkit-changes@lists.webkit.org
https://lists.webkit.org/mailman/listinfo/webkit-changes

Reply via email to