RESOLVED FIXED234103
Add FileSystem function to read a file at a path
https://bugs.webkit.org/show_bug.cgi?id=234103
Summary Add FileSystem function to read a file at a path
Don Olmstead
Reported 2021-12-09 14:41:27 PST
There's lots of code that wants to load a file into a buffer.
Attachments
WIP Patch (10.21 KB, patch)
2021-12-09 14:41 PST, Don Olmstead
no flags
Patch (4.28 KB, patch)
2021-12-09 17:07 PST, Don Olmstead
no flags
Patch (14.64 KB, patch)
2021-12-09 17:11 PST, Don Olmstead
no flags
Patch (14.55 KB, patch)
2021-12-09 17:14 PST, Don Olmstead
achristensen: commit-queue-
Patch (14.99 KB, patch)
2021-12-09 22:02 PST, Don Olmstead
no flags
Patch (15.38 KB, patch)
2021-12-10 11:17 PST, Don Olmstead
no flags
Patch (15.33 KB, patch)
2021-12-10 11:23 PST, Don Olmstead
achristensen: review-
Patch (16.09 KB, patch)
2021-12-10 14:01 PST, Don Olmstead
ews-feeder: commit-queue-
Patch (16.08 KB, patch)
2021-12-10 14:09 PST, Don Olmstead
achristensen: review+
Patch (15.98 KB, patch)
2021-12-10 15:08 PST, Don Olmstead
no flags
Don Olmstead
Comment 1 2021-12-09 14:41:54 PST Comment hidden (obsolete)
Don Olmstead
Comment 2 2021-12-09 17:07:17 PST Comment hidden (obsolete)
Don Olmstead
Comment 3 2021-12-09 17:11:36 PST Comment hidden (obsolete)
Don Olmstead
Comment 4 2021-12-09 17:14:35 PST
Alex Christensen
Comment 5 2021-12-09 17:17:33 PST
Comment on attachment 446639 [details] Patch View in context: https://bugs.webkit.org/attachment.cgi?id=446639&action=review > Source/WTF/wtf/FileSystem.cpp:543 > + auto handle = FileSystem::openFile(path, FileSystem::FileOpenMode::Read); if (FileSystem::isHandleValid(handle)) return std::nullopt;
Don Olmstead
Comment 6 2021-12-09 17:19:53 PST
Comment on attachment 446639 [details] Patch View in context: https://bugs.webkit.org/attachment.cgi?id=446639&action=review >> Source/WTF/wtf/FileSystem.cpp:543 >> + auto handle = FileSystem::openFile(path, FileSystem::FileOpenMode::Read); > > if (FileSystem::isHandleValid(handle)) return std::nullopt; The first line of `readEntireFile` is that check so I didn't want to do that check twice.
Alex Christensen
Comment 7 2021-12-09 17:39:34 PST
ok
Don Olmstead
Comment 8 2021-12-09 22:02:09 PST
Created attachment 446660 [details] Patch Adding another usage
Sihui Liu
Comment 9 2021-12-10 11:01:05 PST
Comment on attachment 446660 [details] Patch View in context: https://bugs.webkit.org/attachment.cgi?id=446660&action=review > Source/JavaScriptCore/inspector/remote/socket/RemoteInspectorSocket.cpp:268 > + return contents.has_value() ? String::adopt(WTFMove(contents.value())) : emptyString(); You may remove has_value(), std::optional supports operator bool; and you already use "content ? ... : ... " below. > Source/WTF/wtf/FileSystem.cpp:538 > +std::optional<Vector<uint8_t>> readEntireFileAt(const String& path) We don't use "At" for functions that takes path string parameter in FileSystem.h/.cpp, so it seems better to just use "readEntireFile". > Source/WebCore/platform/network/curl/CurlCacheEntry.cpp:118 > + if (!buffer.has_value()) { has_value() not needed. > Source/WebCore/platform/network/curl/CurlCacheEntry.cpp:158 > + if (!buffer.has_value()) { Ditto. > Source/WebCore/platform/network/curl/CurlCacheEntry.cpp:-227 > -bool CurlCacheEntry::loadFileToBuffer(const String& filepath, Vector<uint8_t>& buffer) Do you need to remove its definition in header file? > Source/WebCore/platform/network/curl/CurlCacheManager.cpp:106 > + if (!buffer.has_value()) { has_value() not needed. > Tools/TestWebKitAPI/Tests/WTF/FileSystem.cpp:830 > + EXPECT_FALSE(FileSystem::readEntireFile(FileSystem::invalidPlatformFileHandle).has_value()); > + EXPECT_FALSE(FileSystem::readEntireFileAt(emptyString()).has_value()); > + EXPECT_FALSE(FileSystem::readEntireFileAt(FileSystem::pathByAppendingComponent(tempEmptyFolderPath(), "does-not-exist")).has_value()); > + EXPECT_FALSE(FileSystem::readEntireFileAt(tempEmptyFilePath()).has_value()); > + > + auto buffer = FileSystem::readEntireFileAt(tempFilePath()); > + EXPECT_TRUE(buffer.has_value()); Ditto
Alex Christensen
Comment 10 2021-12-10 11:17:02 PST
Comment on attachment 446660 [details] Patch View in context: https://bugs.webkit.org/attachment.cgi?id=446660&action=review > Source/WTF/wtf/FileSystem.cpp:540 > + if (path.isEmpty()) Also, I'm not sure if this does anything. Won't openFile return an invalid file handle if given an empty path?
Don Olmstead
Comment 11 2021-12-10 11:17:32 PST Comment hidden (obsolete)
Don Olmstead
Comment 12 2021-12-10 11:23:16 PST
Created attachment 446768 [details] Patch Addressing both reviewer feedback
Don Olmstead
Comment 13 2021-12-10 11:24:19 PST
(In reply to Alex Christensen from comment #10) > Comment on attachment 446660 [details] > Patch > > View in context: > https://bugs.webkit.org/attachment.cgi?id=446660&action=review > > > Source/WTF/wtf/FileSystem.cpp:540 > > + if (path.isEmpty()) > > Also, I'm not sure if this does anything. Won't openFile return an invalid > file handle if given an empty path? The tests say yes so removed.
Alex Christensen
Comment 14 2021-12-10 12:04:57 PST
Comment on attachment 446768 [details] Patch View in context: https://bugs.webkit.org/attachment.cgi?id=446768&action=review > Source/WTF/wtf/FileSystem.cpp:541 > + auto contents = readEntireFile(handle); readEntireFile calls readFromFile once. readFromFile can return a positive number of bytes indicating that the file was partially read successfully and you need to call it again to read the rest. I think it would be worth fixing that while making more functionality rely on it just working. Otherwise we will increase the number of times a file read is reported to fail, especially with larger files. It would probably also be worth returning failure if the large allocation fails instead of crashing.
Don Olmstead
Comment 15 2021-12-10 14:01:16 PST
Created attachment 446807 [details] Patch Read in a loop until all the contents are read.
Don Olmstead
Comment 16 2021-12-10 14:09:00 PST
Alex Christensen
Comment 17 2021-12-10 14:54:20 PST
Comment on attachment 446808 [details] Patch View in context: https://bugs.webkit.org/attachment.cgi?id=446808&action=review > Source/WTF/wtf/FileSystem.cpp:531 > + unsigned totalBytesRead = 0; nit: this might limit the capability to 4GB. size_t would increase that to the size of the virtual address space. > Source/WebCore/platform/network/curl/CurlCacheEntry.cpp:123 > + if (auto bufferSize = buffer.value().size()) nit: buffer->size() would also work > Source/WebCore/platform/network/curl/CurlCacheEntry.cpp:124 > + job->getInternal()->client()->didReceiveBuffer(job, SharedBuffer::create(WTFMove(buffer.value())), bufferSize); nit: WTFMove(*buffer) would also work. I don't actually care about this though. > Source/WebCore/rendering/RenderThemeWin.cpp:1028 > - return String(); > + return emptyString(); Why was this change made? I guess it makes it more consistent with the !USE(CF) path, but why not change that to be a null string?
Don Olmstead
Comment 18 2021-12-10 15:08:19 PST
EWS
Comment 19 2021-12-10 16:36:37 PST
Committed r286883 (245113@main): <https://commits.webkit.org/245113@main> All reviewed patches have been landed. Closing bug and clearing flags on attachment 446825 [details].
Radar WebKit Bug Importer
Comment 20 2021-12-10 16:37:21 PST
Note You need to log in before you can comment on or make changes to this bug.