Skip to content

Commit cb506c8

Browse files
committed
FileSystem::openFile() should return a FileHandle
https://bugs.webkit.org/show_bug.cgi?id=289509 Reviewed by Geoffrey Garen. FileSystem::openFile() should return a FileHandle instead of a PlatformFileHandle, FileHandle being a RAII wrapper to make sure we never forget to close file descriptors. In a follow-up, I will look into cleaning up FileSystem.h, now that some of the API is only needed by FileHandle. * Source/JavaScriptCore/API/JSScript.mm: (-[JSScript readCache]): * Source/JavaScriptCore/jsc.cpp: (JSC_DEFINE_HOST_FUNCTION): * Source/JavaScriptCore/runtime/JSGlobalObject.cpp: (JSC::JSC_DEFINE_HOST_FUNCTION): * Source/JavaScriptCore/wasm/WasmStreamingParser.cpp: (JSC::Wasm::dumpWasmSource): * Source/WTF/WTF.xcodeproj/project.pbxproj: * Source/WTF/wtf/CMakeLists.txt: * Source/WTF/wtf/FileHandle.cpp: Added. (WTF::FileSystemImpl::FileHandle::FileHandle): (WTF::FileSystemImpl::FileHandle::~FileHandle): (WTF::FileSystemImpl::FileHandle::operator=): (WTF::FileSystemImpl::FileHandle::readAll): (WTF::FileSystemImpl::FileHandle::read): (WTF::FileSystemImpl::FileHandle::write): (WTF::FileSystemImpl::FileHandle::lock): (WTF::FileSystemImpl::FileHandle::truncate): (WTF::FileSystemImpl::FileHandle::closeIfNecessary): (WTF::FileSystemImpl::FileHandle::size): (WTF::FileSystemImpl::FileHandle::seek): (WTF::FileSystemImpl::FileHandle::flush): (WTF::FileSystemImpl::FileHandle::id): * Source/WTF/wtf/FileHandle.h: Added. (WTF::FileSystemImpl::FileHandle::FileHandle): (WTF::FileSystemImpl::FileHandle::platformHandle const): (WTF::FileSystemImpl::FileHandle::isValid const): (WTF::FileSystemImpl::FileHandle::operator bool const): * Source/WTF/wtf/FileSystem.cpp: (WTF::FileSystemImpl::appendFileContentsToFileHandle): (WTF::FileSystemImpl::MappedFileData::MappedFileData): (WTF::FileSystemImpl::openAndLockFile): (WTF::FileSystemImpl::createMappedFileData): (WTF::FileSystemImpl::mapToFile): (WTF::FileSystemImpl::readOrMakeSalt): (WTF::FileSystemImpl::readEntireFile): (WTF::FileSystemImpl::overwriteEntireFile): (WTF::FileSystemImpl::createTemporaryFile): (WTF::FileSystemImpl::unlockAndCloseFile): Deleted. * Source/WTF/wtf/FileSystem.h: (WTF::FileSystemImpl::openTemporaryFile): * Source/WTF/wtf/cocoa/FileSystemCocoa.mm: (WTF::FileSystemImpl::openTemporaryFile): * Source/WTF/wtf/posix/FileSystemPOSIX.cpp: (WTF::FileSystemImpl::openFile): (WTF::FileSystemImpl::openTemporaryFile): * Source/WTF/wtf/win/FileSystemWin.cpp: (WTF::FileSystemImpl::openTemporaryFile): (WTF::FileSystemImpl::openFile): * Source/WebCore/Headers.cmake: * Source/WebCore/Modules/filesystemaccess/FileSystemStorageConnection.h: (WebCore::FileSystemStorageConnection::SyncAccessHandleInfo::isolatedCopy): * Source/WebCore/Modules/filesystemaccess/FileSystemSyncAccessHandle.cpp: (WebCore::FileSystemSyncAccessHandle::create): (WebCore::FileSystemSyncAccessHandle::FileSystemSyncAccessHandle): (WebCore::FileSystemSyncAccessHandle::truncate): (WebCore::FileSystemSyncAccessHandle::getSize): (WebCore::FileSystemSyncAccessHandle::flush): (WebCore::FileSystemSyncAccessHandle::read): (WebCore::FileSystemSyncAccessHandle::write): * Source/WebCore/Modules/filesystemaccess/FileSystemSyncAccessHandle.h: * Source/WebCore/Modules/model-element/scenekit/SceneKitModelLoaderUSD.mm: (WebCore::writeToTemporaryFile): * Source/WebCore/Modules/webdatabase/OriginLock.cpp: * Source/WebCore/Modules/webdatabase/OriginLock.h: (): Deleted. * Source/WebCore/Sources.txt: * Source/WebCore/WebCore.xcodeproj/project.pbxproj: * Source/WebCore/bindings/js/GCController.cpp: (WebCore::GCController::dumpHeapForVM): * Source/WebCore/contentextensions/SerializedNFA.cpp: (WebCore::ContentExtensions::writeAllToFile): (WebCore::ContentExtensions::SerializedNFA::serialize): * Source/WebCore/html/FileInputType.cpp: (WebCore::FileInputType::filesChosen): * Source/WebCore/loader/appcache/ApplicationCacheStorage.cpp: (WebCore::ApplicationCacheStorage::writeDataToUniqueFileInDirectory): * Source/WebCore/platform/FileHandle.cpp: Removed. * Source/WebCore/platform/FileHandle.h: Removed. * Source/WebCore/platform/FileStream.cpp: (WebCore::FileStream::openForRead): (WebCore::FileStream::close): (WebCore::FileStream::read): (WebCore::FileStream::FileStream): Deleted. (WebCore::FileStream::~FileStream): Deleted. * Source/WebCore/platform/FileStream.h: * Source/WebCore/platform/cocoa/FileMonitorCocoa.mm: (WebCore::FileMonitor::FileMonitor): * Source/WebCore/platform/graphics/GraphicsLayer.cpp: (showGraphicsLayerTree): * Source/WebCore/platform/graphics/cg/ImageUtilitiesCG.cpp: (WebCore::transcodeImage): * Source/WebCore/platform/network/BlobRegistryImpl.cpp: (WebCore::writeFilePathsOrDataBuffersToFile): (WebCore::BlobRegistryImpl::writeBlobsToTemporaryFilesForIndexedDB): * Source/WebCore/platform/network/curl/CookieJarDB.cpp: (WebCore::CookieJarDB::flagDatabaseCorruption): * Source/WebCore/platform/network/curl/CurlFormDataStream.cpp: (WebCore::CurlFormDataStream::readFromFile): * Source/WebCore/platform/network/curl/CurlFormDataStream.h: * Source/WebCore/storage/StorageUtilities.cpp: (WebCore::StorageUtilities::readOriginFromFile): (WebCore::StorageUtilities::writeOriginToFile): * Source/WebCore/testing/Internals.cpp: (WebCore::Internals::createTemporaryFile): * Source/WebCore/workers/service/server/SWScriptStorage.cpp: (WebCore::SWScriptStorage::store): * Source/WebGPU/WGSL/wgslc.cpp: (runWGSL): * Source/WebKit/NetworkProcess/NetworkDataTaskBlob.cpp: (WebKit::NetworkDataTaskBlob::consumeData): (WebKit::NetworkDataTaskBlob::download): (WebKit::NetworkDataTaskBlob::writeDownload): (WebKit::NetworkDataTaskBlob::cleanDownloadFiles): (WebKit::NetworkDataTaskBlob::didFinishDownload): (WebKit::NetworkDataTaskBlob::didFinish): * Source/WebKit/NetworkProcess/NetworkDataTaskBlob.h: * Source/WebKit/NetworkProcess/NetworkDataTaskDataURL.cpp: (WebKit::NetworkDataTaskDataURL::downloadDecodedData): * Source/WebKit/NetworkProcess/ServiceWorker/ServiceWorkerDownloadTask.cpp: (WebKit::ServiceWorkerDownloadTask::cancel): (WebKit::ServiceWorkerDownloadTask::setPendingDownloadLocation): (WebKit::ServiceWorkerDownloadTask::didReceiveData): (WebKit::ServiceWorkerDownloadTask::didFinish): (WebKit::ServiceWorkerDownloadTask::didFailDownload): * Source/WebKit/NetworkProcess/ServiceWorker/ServiceWorkerDownloadTask.h: * Source/WebKit/NetworkProcess/cache/NetworkCache.cpp: (WebKit::NetworkCache::Cache::dumpContentsToFile): * Source/WebKit/NetworkProcess/cache/NetworkCacheData.cpp: (WebKit::NetworkCache::Data::mapToFile const): (WebKit::NetworkCache::mapFile): (WebKit::NetworkCache::adoptAndMapFile): * Source/WebKit/NetworkProcess/cache/NetworkCacheData.h: (WebKit::NetworkCache::Data::Data): * Source/WebKit/NetworkProcess/cache/NetworkCacheDataCocoa.mm: (WebKit::NetworkCache::Data::adoptMap): * Source/WebKit/NetworkProcess/cache/NetworkCacheDataCurl.cpp: (WebKit::NetworkCache::Data::adoptMap): * Source/WebKit/NetworkProcess/cache/NetworkCacheDataGLib.cpp: (WebKit::NetworkCache::Data::Data): (WebKit::NetworkCache::Data::adoptMap): (WebKit::NetworkCache::Data::tryCreateSharedMemory const): (WebKit::NetworkCache::MapWrapper::~MapWrapper): Deleted. * Source/WebKit/NetworkProcess/cache/NetworkCacheIOChannel.h: (WebKit::NetworkCache::IOChannel::WTF_GUARDED_BY_LOCK): Deleted. * Source/WebKit/NetworkProcess/cache/NetworkCacheIOChannelCurl.cpp: (WebKit::NetworkCache::IOChannel::~IOChannel): (WebKit::NetworkCache::IOChannel::read): (WebKit::NetworkCache::IOChannel::write): * Source/WebKit/NetworkProcess/curl/NetworkDataTaskCurl.cpp: (WebKit::NetworkDataTaskCurl::invokeDidReceiveResponse): (WebKit::NetworkDataTaskCurl::deleteDownloadFile): * Source/WebKit/NetworkProcess/curl/NetworkDataTaskCurl.h: * Source/WebKit/NetworkProcess/storage/BackgroundFetchStoreManager.cpp: (WebKit::BackgroundFetchStoreManager::storeFetchResponseBodyChunk): * Source/WebKit/NetworkProcess/storage/CacheStorageManager.cpp: (WebKit::readCachesList): * Source/WebKit/NetworkProcess/storage/FileSystemStorageHandle.cpp: (WebKit::FileSystemStorageHandle::create): (WebKit::FileSystemStorageHandle::createSyncAccessHandle): (WebKit::FileSystemStorageHandle::createWritable): (WebKit::FileSystemStorageHandle::closeWritable): (WebKit::FileSystemStorageHandle::executeCommandForWritableInternal): (WebKit::FileSystemStorageHandle::computeCommandSpace): * Source/WebKit/NetworkProcess/storage/FileSystemStorageHandle.h: * Source/WebKit/Platform/IPC/SharedFileHandle.cpp: (IPC::SharedFileHandle::create): * Source/WebKit/Platform/IPC/SharedFileHandle.h: (IPC::SharedFileHandle::release): (IPC::SharedFileHandle::SharedFileHandle): * Source/WebKit/Platform/IPC/cocoa/SharedFileHandleCocoa.cpp: (IPC::SharedFileHandle::create): (IPC::SharedFileHandle::SharedFileHandle): (IPC::SharedFileHandle::toMachSendRight const): * Source/WebKit/Shared/PersistencyUtils.cpp: (WebKit::writeToDisk): * Source/WebKit/Shared/WebMemorySampler.cpp: (WebKit::WebMemorySampler::stop): (WebKit::WebMemorySampler::initializeTempLogFile): (WebKit::WebMemorySampler::writeHeaders): (WebKit::WebMemorySampler::sampleTimerFired): (WebKit::WebMemorySampler::appendCurrentMemoryUsageToFile): * Source/WebKit/Shared/WebMemorySampler.h: (): Deleted. * Source/WebKit/Shared/mac/AuxiliaryProcessMac.mm: (WebKit::fileContents): (WebKit::writeSandboxDataToCacheFile): * Source/WebKit/UIProcess/API/APIContentRuleListStore.cpp: (API::writeDataToFile): (API::compiledToFile): (API::ContentRuleListStore::invalidateContentRuleListVersion): (API::ContentRuleListStore::corruptContentRuleListHeader): (API::ContentRuleListStore::invalidateContentRuleListHeader): * Source/WebKit/UIProcess/Cocoa/SystemPreviewControllerCocoa.mm: * Source/WebKit/UIProcess/Extensions/WebExtension.cpp: (WebKit::convertChromeExtensionToTemporaryZipFile): * Source/WebKit/UIProcess/Inspector/win/WebInspectorUIProxyWin.cpp: (WebKit::WebInspectorUIProxy::showSavePanelForSingleFile): * Source/WebKit/UIProcess/WebFullScreenManagerProxy.cpp: (WebKit::WebFullScreenManagerProxy::prepareQuickLookImageURL const): * Source/WebKit/UIProcess/ios/WKModelView.mm: (-[WKModelView createFileForModel:]): * Source/WebKit/WebProcess/Model/mac/ARKitInlinePreviewModelPlayerMac.mm: (WebKit::ARKitInlinePreviewModelPlayerMac::createFile): * Tools/TestWebKitAPI/Tests/WTF/FileSystem.cpp: (TestWebKitAPI::createTestFile): (TestWebKitAPI::TEST_F(FileSystemTest, openExistingFileTruncate)): (TestWebKitAPI::TEST_F(FileSystemTest, openExistingFileTruncateFailIfFileExists)): (TestWebKitAPI::TEST_F(FileSystemTest, openExistingFileReadWrite)): (TestWebKitAPI::TEST_F(FileSystemTest, openExistingFileReadWriteFailIfFileExists)): (TestWebKitAPI::TEST_F(FileSystemTest, openExistingFileReadOnly)): (TestWebKitAPI::TEST_F(FileSystemTest, openNonExistingFileTruncate)): (TestWebKitAPI::TEST_F(FileSystemTest, openNonExistingFileTruncateFailIfFileExists)): (TestWebKitAPI::TEST_F(FileSystemTest, openNonExistingFileReadWrite)): (TestWebKitAPI::TEST_F(FileSystemTest, openNonExistingFileReadWriteFailIfFileExists)): (TestWebKitAPI::TEST_F(FileSystemTest, openNonExistingFileReadOnly)): (TestWebKitAPI::TEST_F(FileSystemTest, deleteEmptyDirectoryContainingDSStoreFile)): (TestWebKitAPI::TEST_F(FileSystemTest, deleteEmptyDirectoryOnNonEmptyDirectory)): (TestWebKitAPI::TEST_F(FileSystemTest, moveDirectory)): (TestWebKitAPI::runGetFileModificationTimeTest): (TestWebKitAPI::TEST_F(FileSystemTest, readEntireFile)): * Tools/TestWebKitAPI/Tests/WebCore/FileMonitor.cpp: (TestWebKitAPI::TEST_F(FileMonitorTest, DetectDeleteButNotSubsequentChange)): * Tools/TestWebKitAPI/Tests/WebCore/SharedBufferTest.cpp: (TestWebKitAPI::FragmentedSharedBufferTest::SetUp): * Tools/TestWebKitAPI/Tests/WebCore/cocoa/DatabaseTrackerTest.mm: (TestWebKitAPI::createFileAtPath): * Tools/TestWebKitAPI/Tests/WebCore/gstreamer/GstElementHarness.cpp: (TestWebKitAPI::TEST_F(GStreamerTest, harnessParseMP4)): (TestWebKitAPI::TEST_F(GStreamerTest, harnessDecodeMP4Video)): Canonical link: https://commits.webkit.org/292119@main
1 parent 49ccab1 commit cb506c8

87 files changed

Lines changed: 716 additions & 926 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

Source/JavaScriptCore/API/JSScript.mm

Lines changed: 13 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -161,15 +161,12 @@ - (void)readCache
161161

162162
String cacheFilename = [m_cachePath path];
163163

164-
auto fd = FileSystem::openAndLockFile(cacheFilename, FileSystem::FileOpenMode::Read, {FileSystem::FileLockMode::Exclusive, FileSystem::FileLockMode::Nonblocking});
165-
if (!FileSystem::isHandleValid(fd))
164+
auto handle = FileSystem::openAndLockFile(cacheFilename, FileSystem::FileOpenMode::Read, { FileSystem::FileLockMode::Exclusive, FileSystem::FileLockMode::Nonblocking });
165+
if (!handle)
166166
return;
167-
auto closeFD = makeScopeExit([&] {
168-
FileSystem::unlockAndCloseFile(fd);
169-
});
170167

171168
bool success;
172-
FileSystem::MappedFileData mappedFile(fd, FileSystem::MappedFileMode::Private, success);
169+
FileSystem::MappedFileData mappedFile(handle.platformHandle(), FileSystem::MappedFileMode::Private, success);
173170
if (!success)
174171
return;
175172

@@ -205,7 +202,7 @@ - (void)readCache
205202
if (isCachedBytecodeStillValid(vm, cachedBytecode.copyRef(), key, m_type == kJSScriptTypeProgram ? JSC::SourceCodeType::ProgramType : JSC::SourceCodeType::ModuleType))
206203
m_cachedBytecode = WTFMove(cachedBytecode);
207204
else
208-
FileSystem::truncateFile(fd, 0);
205+
handle.truncate(0);
209206
}
210207

211208
- (BOOL)cacheBytecodeWithError:(NSError **)error
@@ -307,41 +304,33 @@ - (BOOL)writeCache:(String&)error
307304
NSString *cachePathString = [m_cachePath path];
308305
const char* cacheFileName = cachePathString.UTF8String;
309306
const char* tempFileName = [cachePathString stringByAppendingString:@".tmp"].UTF8String;
310-
int fd = open(cacheFileName, O_CREAT | O_WRONLY | O_EXLOCK | O_NONBLOCK, 0600);
311-
if (fd == -1) {
307+
auto fileHandle = FileSystem::FileHandle::adopt(open(cacheFileName, O_CREAT | O_WRONLY | O_EXLOCK | O_NONBLOCK, 0600));
308+
if (!fileHandle) {
312309
error = makeString("Could not open or lock the bytecode cache file. It's likely another VM or process is already using it. Error: "_s, safeStrerror(errno).span());
313310
return NO;
314311
}
315312

316-
auto closeFD = makeScopeExit([&] {
317-
close(fd);
318-
});
319-
320-
int tempFD = open(tempFileName, O_CREAT | O_RDWR | O_EXLOCK | O_NONBLOCK, 0600);
321-
if (tempFD == -1) {
313+
auto tempFileHandle = FileSystem::FileHandle::adopt(open(tempFileName, O_CREAT | O_RDWR | O_EXLOCK | O_NONBLOCK, 0600));
314+
if (!tempFileHandle) {
322315
error = makeString("Could not open or lock the bytecode cache temp file. Error: "_s, safeStrerror(errno).span());
323316
return NO;
324317
}
325318

326-
auto closeTempFD = makeScopeExit([&] {
327-
close(tempFD);
328-
});
329-
330319
JSC::BytecodeCacheError cacheError;
331320
JSC::SourceCode sourceCode = [self sourceCode];
332321
JSC::VM& vm = *toJS([m_virtualMachine JSContextGroupRef]);
333322
switch (m_type) {
334323
case kJSScriptTypeModule:
335-
m_cachedBytecode = JSC::generateModuleBytecode(vm, sourceCode, tempFD, cacheError);
324+
m_cachedBytecode = JSC::generateModuleBytecode(vm, sourceCode, tempFileHandle, cacheError);
336325
break;
337326
case kJSScriptTypeProgram:
338-
m_cachedBytecode = JSC::generateProgramBytecode(vm, sourceCode, tempFD, cacheError);
327+
m_cachedBytecode = JSC::generateProgramBytecode(vm, sourceCode, tempFileHandle, cacheError);
339328
break;
340329
}
341330

342331
if (cacheError.isValid()) {
343332
m_cachedBytecode = JSC::CachedBytecode::create();
344-
FileSystem::truncateFile(fd, 0);
333+
fileHandle.truncate(0);
345334
error = makeString("Unable to generate bytecode for this JSScript because: "_s, cacheError.message());
346335
return NO;
347336
}
@@ -350,9 +339,9 @@ - (BOOL)writeCache:(String&)error
350339
SHA1 sha1;
351340
sha1.addBytes(m_cachedBytecode->span());
352341
sha1.computeHash(computedHash);
353-
FileSystem::writeToFile(tempFD, computedHash);
342+
tempFileHandle.write(computedHash);
354343

355-
fsync(tempFD);
344+
tempFileHandle.flush();
356345
rename(tempFileName, cacheFileName);
357346
return YES;
358347
}

Source/JavaScriptCore/jsc.cpp

Lines changed: 12 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1331,15 +1331,11 @@ class ShellSourceProvider final : public StringSourceProvider {
13311331
});
13321332

13331333
String filename = cachePath();
1334-
auto fd = FileSystem::openAndLockFile(filename, FileSystem::FileOpenMode::ReadWrite, { FileSystem::FileLockMode::Exclusive, FileSystem::FileLockMode::Nonblocking });
1335-
if (!FileSystem::isHandleValid(fd))
1334+
auto handle = FileSystem::openAndLockFile(filename, FileSystem::FileOpenMode::ReadWrite, { FileSystem::FileLockMode::Exclusive, FileSystem::FileLockMode::Nonblocking });
1335+
if (!handle)
13361336
return;
13371337

1338-
auto closeFD = makeScopeExit([&] {
1339-
FileSystem::unlockAndCloseFile(fd);
1340-
});
1341-
1342-
auto fileSize = FileSystem::fileSize(fd);
1338+
auto fileSize = handle.size();
13431339
if (!fileSize)
13441340
return;
13451341

@@ -1349,13 +1345,13 @@ class ShellSourceProvider final : public StringSourceProvider {
13491345
return;
13501346
}
13511347

1352-
if (!FileSystem::truncateFile(fd, m_cachedBytecode->sizeForUpdate()))
1348+
if (!handle.truncate(m_cachedBytecode->sizeForUpdate()))
13531349
return;
13541350

13551351
m_cachedBytecode->commitUpdates([&] (off_t offset, std::span<const uint8_t> data) {
1356-
long long result = FileSystem::seekFile(fd, offset, FileSystem::FileSeekOrigin::Beginning);
1352+
long long result = handle.seek(offset, FileSystem::FileSeekOrigin::Beginning);
13571353
ASSERT_UNUSED(result, result != -1);
1358-
size_t bytesWritten = static_cast<size_t>(FileSystem::writeToFile(fd, data));
1354+
size_t bytesWritten = static_cast<size_t>(handle.write(data));
13591355
ASSERT_UNUSED(bytesWritten, bytesWritten == data.size());
13601356
});
13611357
}
@@ -1379,16 +1375,12 @@ class ShellSourceProvider final : public StringSourceProvider {
13791375
if (filename.isNull())
13801376
return;
13811377

1382-
auto fd = FileSystem::openAndLockFile(filename, FileSystem::FileOpenMode::Read, {FileSystem::FileLockMode::Shared, FileSystem::FileLockMode::Nonblocking});
1383-
if (!FileSystem::isHandleValid(fd))
1378+
auto handle = FileSystem::openAndLockFile(filename, FileSystem::FileOpenMode::Read, { FileSystem::FileLockMode::Shared, FileSystem::FileLockMode::Nonblocking });
1379+
if (!handle)
13841380
return;
13851381

1386-
auto closeFD = makeScopeExit([&] {
1387-
FileSystem::unlockAndCloseFile(fd);
1388-
});
1389-
13901382
bool success;
1391-
FileSystem::MappedFileData mappedFileData(fd, FileSystem::MappedFileMode::Private, success);
1383+
FileSystem::MappedFileData mappedFileData(handle.platformHandle(), FileSystem::MappedFileMode::Private, success);
13921384

13931385
if (!success)
13941386
return;
@@ -2095,18 +2087,16 @@ JSC_DEFINE_HOST_FUNCTION(functionWriteFile, (JSGlobalObject* globalObject, CallF
20952087
}
20962088

20972089
auto handle = FileSystem::openFile(fileName, FileSystem::FileOpenMode::Truncate);
2098-
if (!FileSystem::isHandleValid(handle))
2090+
if (!handle)
20992091
return throwVMError(globalObject, scope, "Could not open file."_s);
21002092

21012093
int size = std::visit(WTF::makeVisitor([&](const String& string) {
21022094
CString utf8 = string.utf8();
2103-
return FileSystem::writeToFile(handle, byteCast<uint8_t>(utf8.span()));
2095+
return handle.write(byteCast<uint8_t>(utf8.span()));
21042096
}, [&] (const std::span<const uint8_t>& data) {
2105-
return FileSystem::writeToFile(handle, data);
2097+
return handle.write(data);
21062098
}), data);
21072099

2108-
FileSystem::closeFile(handle);
2109-
21102100
return JSValue::encode(jsNumber(size));
21112101
}
21122102

Source/JavaScriptCore/runtime/CachedTypes.cpp

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -96,9 +96,9 @@ class Encoder {
9696
ptrdiff_t m_offset;
9797
};
9898

99-
Encoder(VM& vm, FileSystem::PlatformFileHandle fd = FileSystem::invalidPlatformFileHandle)
99+
Encoder(VM& vm, FileSystem::FileHandle& fileHandle)
100100
: m_vm(vm)
101-
, m_fd(fd)
101+
, m_fileHandle(fileHandle)
102102
, m_baseOffset(0)
103103
, m_currentPage(nullptr)
104104
{
@@ -160,7 +160,7 @@ class Encoder {
160160
return nullptr;
161161
m_currentPage->alignEnd();
162162

163-
if (FileSystem::isHandleValid(m_fd)) {
163+
if (m_fileHandle) {
164164
return releaseMapped(error);
165165
}
166166

@@ -177,13 +177,13 @@ class Encoder {
177177
RefPtr<CachedBytecode> releaseMapped(BytecodeCacheError& error)
178178
{
179179
size_t size = m_baseOffset + m_currentPage->size();
180-
if (!FileSystem::truncateFile(m_fd, size)) {
180+
if (!m_fileHandle.truncate(size)) {
181181
error = BytecodeCacheError::StandardError(errno);
182182
return nullptr;
183183
}
184184

185185
for (const auto& page : m_pages) {
186-
int bytesWritten = FileSystem::writeToFile(m_fd, page.span());
186+
int bytesWritten = m_fileHandle.write(page.span());
187187
if (bytesWritten == -1) {
188188
error = BytecodeCacheError::StandardError(errno);
189189
return nullptr;
@@ -196,7 +196,7 @@ class Encoder {
196196
}
197197

198198
bool success;
199-
FileSystem::MappedFileData mappedFileData(m_fd, FileSystem::MappedFileMode::Private, success);
199+
FileSystem::MappedFileData mappedFileData(m_fileHandle.platformHandle(), FileSystem::MappedFileMode::Private, success);
200200
if (!success) {
201201
error = BytecodeCacheError::StandardError(errno);
202202
return nullptr;
@@ -276,7 +276,7 @@ class Encoder {
276276
}
277277

278278
VM& m_vm;
279-
FileSystem::PlatformFileHandle m_fd;
279+
FileSystem::FileHandle& m_fileHandle;
280280
ptrdiff_t m_baseOffset;
281281
Page* m_currentPage;
282282
Vector<Page> m_pages;
@@ -2596,11 +2596,11 @@ void encodeCodeBlock(Encoder& encoder, const SourceCodeKey& key, const UnlinkedC
25962596
entry->encode(encoder, { key, jsCast<const UnlinkedCodeBlockType*>(codeBlock) });
25972597
}
25982598

2599-
RefPtr<CachedBytecode> encodeCodeBlock(VM& vm, const SourceCodeKey& key, const UnlinkedCodeBlock* codeBlock, FileSystem::PlatformFileHandle fd, BytecodeCacheError& error)
2599+
RefPtr<CachedBytecode> encodeCodeBlock(VM& vm, const SourceCodeKey& key, const UnlinkedCodeBlock* codeBlock, FileSystem::FileHandle& fileHandle, BytecodeCacheError& error)
26002600
{
26012601
const ClassInfo* classInfo = codeBlock->classInfo();
26022602

2603-
Encoder encoder(vm, fd);
2603+
Encoder encoder(vm, fileHandle);
26042604
if (classInfo == UnlinkedProgramCodeBlock::info())
26052605
encodeCodeBlock<UnlinkedProgramCodeBlock>(encoder, key, codeBlock);
26062606
else if (classInfo == UnlinkedModuleProgramCodeBlock::info())
@@ -2614,12 +2614,14 @@ RefPtr<CachedBytecode> encodeCodeBlock(VM& vm, const SourceCodeKey& key, const U
26142614
RefPtr<CachedBytecode> encodeCodeBlock(VM& vm, const SourceCodeKey& key, const UnlinkedCodeBlock* codeBlock)
26152615
{
26162616
BytecodeCacheError error;
2617-
return encodeCodeBlock(vm, key, codeBlock, FileSystem::invalidPlatformFileHandle, error);
2617+
FileSystem::FileHandle invalidFileHandle;
2618+
return encodeCodeBlock(vm, key, codeBlock, invalidFileHandle, error);
26182619
}
26192620

26202621
RefPtr<CachedBytecode> encodeFunctionCodeBlock(VM& vm, const UnlinkedFunctionCodeBlock* codeBlock, BytecodeCacheError& error)
26212622
{
2622-
Encoder encoder(vm);
2623+
FileSystem::FileHandle invalidFileHandle;
2624+
Encoder encoder(vm, invalidFileHandle);
26232625
encoder.malloc<CachedFunctionCodeBlock>()->encode(encoder, *codeBlock);
26242626
return encoder.release(error);
26252627
}

Source/JavaScriptCore/runtime/CachedTypes.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,7 @@ class Decoder : public RefCounted<Decoder> {
111111
};
112112

113113
JS_EXPORT_PRIVATE RefPtr<CachedBytecode> encodeCodeBlock(VM&, const SourceCodeKey&, const UnlinkedCodeBlock*);
114-
JS_EXPORT_PRIVATE RefPtr<CachedBytecode> encodeCodeBlock(VM&, const SourceCodeKey&, const UnlinkedCodeBlock*, FileSystem::PlatformFileHandle fd, BytecodeCacheError&);
114+
JS_EXPORT_PRIVATE RefPtr<CachedBytecode> encodeCodeBlock(VM&, const SourceCodeKey&, const UnlinkedCodeBlock*, FileSystem::FileHandle&, BytecodeCacheError&);
115115

116116
UnlinkedCodeBlock* decodeCodeBlockImpl(VM&, const SourceCodeKey&, Ref<CachedBytecode>);
117117

Source/JavaScriptCore/runtime/CodeCache.cpp

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -306,10 +306,9 @@ SourceCodeKey sourceCodeKeyForSerializedModule(VM& vm, const SourceCode& sourceC
306306
return sourceCodeKeyForSerializedBytecode(vm, sourceCode, SourceCodeType::ModuleType, StrictModeLexicallyScopedFeature, scriptMode, { });
307307
}
308308

309-
RefPtr<CachedBytecode> serializeBytecode(VM& vm, UnlinkedCodeBlock* codeBlock, const SourceCode& source, SourceCodeType codeType, LexicallyScopedFeatures lexicallyScopedFeatures, JSParserScriptMode scriptMode, FileSystem::PlatformFileHandle fd, BytecodeCacheError& error, OptionSet<CodeGenerationMode> codeGenerationMode)
309+
RefPtr<CachedBytecode> serializeBytecode(VM& vm, UnlinkedCodeBlock* codeBlock, const SourceCode& source, SourceCodeType codeType, LexicallyScopedFeatures lexicallyScopedFeatures, JSParserScriptMode scriptMode, FileSystem::FileHandle& fileHandle, BytecodeCacheError& error, OptionSet<CodeGenerationMode> codeGenerationMode)
310310
{
311-
return encodeCodeBlock(vm,
312-
sourceCodeKeyForSerializedBytecode(vm, source, codeType, lexicallyScopedFeatures, scriptMode, codeGenerationMode), codeBlock, fd, error);
311+
return encodeCodeBlock(vm, sourceCodeKeyForSerializedBytecode(vm, source, codeType, lexicallyScopedFeatures, scriptMode, codeGenerationMode), codeBlock, fileHandle, error);
313312
}
314313

315314
}

Source/JavaScriptCore/runtime/CodeCache.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -265,7 +265,7 @@ UnlinkedProgramCodeBlock* recursivelyGenerateUnlinkedCodeBlockForProgram(VM&, co
265265
UnlinkedModuleProgramCodeBlock* recursivelyGenerateUnlinkedCodeBlockForModuleProgram(VM&, const SourceCode&, LexicallyScopedFeatures, JSParserScriptMode, OptionSet<CodeGenerationMode>, ParserError&, EvalContextType);
266266

267267
void writeCodeBlock(const SourceCodeKey&, const SourceCodeValue&);
268-
RefPtr<CachedBytecode> serializeBytecode(VM&, UnlinkedCodeBlock*, const SourceCode&, SourceCodeType, LexicallyScopedFeatures, JSParserScriptMode, FileSystem::PlatformFileHandle fd, BytecodeCacheError&, OptionSet<CodeGenerationMode>);
268+
RefPtr<CachedBytecode> serializeBytecode(VM&, UnlinkedCodeBlock*, const SourceCode&, SourceCodeType, LexicallyScopedFeatures, JSParserScriptMode, FileSystem::FileHandle&, BytecodeCacheError&, OptionSet<CodeGenerationMode>);
269269
SourceCodeKey sourceCodeKeyForSerializedProgram(VM&, const SourceCode&);
270270
SourceCodeKey sourceCodeKeyForSerializedModule(VM&, const SourceCode&);
271271

Source/JavaScriptCore/runtime/Completion.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ bool checkModuleSyntax(JSGlobalObject* globalObject, const SourceCode& source, P
8787
return !!moduleAnalyzer.analyze(*moduleProgramNode);
8888
}
8989

90-
RefPtr<CachedBytecode> generateProgramBytecode(VM& vm, const SourceCode& source, FileSystem::PlatformFileHandle fd, BytecodeCacheError& error)
90+
RefPtr<CachedBytecode> generateProgramBytecode(VM& vm, const SourceCode& source, FileSystem::FileHandle& fileHandle, BytecodeCacheError& error)
9191
{
9292
JSLockHolder lock(vm);
9393
RELEASE_ASSERT(vm.atomStringTable() == Thread::currentSingleton().atomStringTable());
@@ -103,10 +103,10 @@ RefPtr<CachedBytecode> generateProgramBytecode(VM& vm, const SourceCode& source,
103103
if (!unlinkedCodeBlock)
104104
return nullptr;
105105

106-
return serializeBytecode(vm, unlinkedCodeBlock, source, SourceCodeType::ProgramType, lexicallyScopedFeatures, scriptMode, fd, error, { });
106+
return serializeBytecode(vm, unlinkedCodeBlock, source, SourceCodeType::ProgramType, lexicallyScopedFeatures, scriptMode, fileHandle, error, { });
107107
}
108108

109-
RefPtr<CachedBytecode> generateModuleBytecode(VM& vm, const SourceCode& source, FileSystem::PlatformFileHandle fd, BytecodeCacheError& error)
109+
RefPtr<CachedBytecode> generateModuleBytecode(VM& vm, const SourceCode& source, FileSystem::FileHandle& fileHandle, BytecodeCacheError& error)
110110
{
111111
JSLockHolder lock(vm);
112112
RELEASE_ASSERT(vm.atomStringTable() == Thread::currentSingleton().atomStringTable());
@@ -121,7 +121,7 @@ RefPtr<CachedBytecode> generateModuleBytecode(VM& vm, const SourceCode& source,
121121
error = parserError;
122122
if (!unlinkedCodeBlock)
123123
return nullptr;
124-
return serializeBytecode(vm, unlinkedCodeBlock, source, SourceCodeType::ModuleType, lexicallyScopedFeatures, scriptMode, fd, error, { });
124+
return serializeBytecode(vm, unlinkedCodeBlock, source, SourceCodeType::ModuleType, lexicallyScopedFeatures, scriptMode, fileHandle, error, { });
125125
}
126126

127127
JSValue evaluate(JSGlobalObject* globalObject, const SourceCode& source, JSValue thisValue, NakedPtr<Exception>& returnedException)

Source/JavaScriptCore/runtime/Completion.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,8 +46,8 @@ JS_EXPORT_PRIVATE bool checkSyntax(VM&, const SourceCode&, ParserError&);
4646
JS_EXPORT_PRIVATE bool checkSyntax(JSGlobalObject*, const SourceCode&, JSValue* exception = nullptr);
4747
JS_EXPORT_PRIVATE bool checkModuleSyntax(JSGlobalObject*, const SourceCode&, ParserError&);
4848

49-
JS_EXPORT_PRIVATE RefPtr<CachedBytecode> generateProgramBytecode(VM&, const SourceCode&, FileSystem::PlatformFileHandle fd, BytecodeCacheError&);
50-
JS_EXPORT_PRIVATE RefPtr<CachedBytecode> generateModuleBytecode(VM&, const SourceCode&, FileSystem::PlatformFileHandle fd, BytecodeCacheError&);
49+
JS_EXPORT_PRIVATE RefPtr<CachedBytecode> generateProgramBytecode(VM&, const SourceCode&, FileSystem::FileHandle&, BytecodeCacheError&);
50+
JS_EXPORT_PRIVATE RefPtr<CachedBytecode> generateModuleBytecode(VM&, const SourceCode&, FileSystem::FileHandle&, BytecodeCacheError&);
5151

5252
JS_EXPORT_PRIVATE JSValue evaluate(JSGlobalObject*, const SourceCode&, JSValue thisValue, NakedPtr<Exception>& returnedException);
5353
inline JSValue evaluate(JSGlobalObject* globalObject, const SourceCode& sourceCode, JSValue thisValue = JSValue())

Source/JavaScriptCore/runtime/JSGlobalObject.cpp

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -465,15 +465,14 @@ JSC_DEFINE_HOST_FUNCTION(dumpAndClearSamplingProfilerSamples, (JSGlobalObject* g
465465
auto jsonData = json->toJSONString();
466466
{
467467
auto [tempFilePath, fileHandle] = FileSystem::openTemporaryFile(filenamePrefix);
468-
if (!FileSystem::isHandleValid(fileHandle)) {
468+
if (!fileHandle) {
469469
dataLogLn("Dumping sampling profiler samples failed to open temporary file");
470470
return JSValue::encode(jsUndefined());
471471
}
472472

473473
CString utf8String = jsonData.utf8();
474474

475-
FileSystem::writeToFile(fileHandle, byteCast<uint8_t>(utf8String.span()));
476-
FileSystem::closeFile(fileHandle);
475+
fileHandle.write(byteCast<uint8_t>(utf8String.span()));
477476
dataLogLn("Dumped sampling profiler samples to ", tempFilePath);
478477
}
479478

Source/JavaScriptCore/wasm/WasmStreamingParser.cpp

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -84,13 +84,12 @@ static void dumpWasmSource(const Vector<uint8_t>& source)
8484
FileSystem::FileOpenMode::Truncate,
8585
FileSystem::FileAccessPermission::All,
8686
/* failIfFileExists = */ true);
87-
if (fileHandle == FileSystem::invalidPlatformFileHandle) {
87+
if (!fileHandle) {
8888
dataLogLn("Error dumping wasm");
8989
return;
9090
}
9191
dataLogLn("Dumping ", source.size(), " wasm source bytes to ", WTF::makeString(unsafeSpan(file), (count - 1), ".wasm"_s));
92-
FileSystem::writeToFile(fileHandle, source.span());
93-
FileSystem::closeFile(fileHandle);
92+
fileHandle.write(source.span());
9493
}
9594
#endif
9695

0 commit comments

Comments
 (0)