From 7e8b11cfaa78bdffe13bf4963347f4709e97ee76 Mon Sep 17 00:00:00 2001 From: Alon Zakai Date: Fri, 19 May 2023 11:21:52 -0700 Subject: [PATCH] WasmFS: Open and close files in the JS API _wasmfs_write_file (#19397) The memory backend appears to allow lockedFile.write without .open first, but other backends like Node do not. In general, it seems correct to open the file before writing and close it after, and the read JS API call does so, so this fixes the write one. --- system/lib/wasmfs/js_api.cpp | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/system/lib/wasmfs/js_api.cpp b/system/lib/wasmfs/js_api.cpp index fe33dd4ff6dd7..f9de0e65ad37f 100644 --- a/system/lib/wasmfs/js_api.cpp +++ b/system/lib/wasmfs/js_api.cpp @@ -88,11 +88,24 @@ int _wasmfs_write_file(char* pathname, char* data, size_t data_size) { } auto lockedFile = dataFile->locked(); + int err = lockedFile.open(O_WRONLY); + if (err < 0) { + emscripten_console_error("Fatal error in FS.writeFile"); + abort(); + } + auto offset = lockedFile.getSize(); auto result = lockedFile.write((uint8_t*)data, data_size, offset); if (result != __WASI_ERRNO_SUCCESS) { return 0; } + + err = lockedFile.close(); + if (err < 0) { + emscripten_console_error("Fatal error in FS.writeFile"); + abort(); + } + return data_size; }