Skip to content

Commit

Permalink
WasmFS: Open and close files in the JS API _wasmfs_write_file (#19397)
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
kripken authored May 19, 2023
1 parent 7cff8f1 commit 7e8b11c
Showing 1 changed file with 13 additions and 0 deletions.
13 changes: 13 additions & 0 deletions system/lib/wasmfs/js_api.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand Down

0 comments on commit 7e8b11c

Please sign in to comment.