Skip to content

Commit 3f94742

Browse files
authored
[WasmFS] Implement fdatasync (#16675)
As an alias for `__wasi_fd_sync` (i.e. `fsync`). This flushes data more conservatively than necessary, but it's not worth maintaining a separate code path to avoid flushing file metadata right now.
1 parent d26e762 commit 3f94742

File tree

4 files changed

+36
-0
lines changed

4 files changed

+36
-0
lines changed

system/lib/wasmfs/syscalls.cpp

+5
Original file line numberDiff line numberDiff line change
@@ -285,6 +285,11 @@ __wasi_errno_t __wasi_fd_sync(__wasi_fd_t fd) {
285285
return __WASI_ERRNO_SUCCESS;
286286
}
287287

288+
int __syscall_fdatasync(int fd) {
289+
// TODO: Optimize this to avoid unnecessarily flushing unnecessary metadata.
290+
return __wasi_fd_sync(fd);
291+
}
292+
288293
backend_t wasmfs_get_backend_by_fd(int fd) {
289294
auto openFile = wasmFS.getFileTable().locked().getEntry(fd);
290295
if (!openFile) {

tests/test_other.py

+5
Original file line numberDiff line numberDiff line change
@@ -11616,6 +11616,11 @@ def test_unistd_create(self):
1161611616
self.set_setting('WASMFS')
1161711617
self.do_run_in_out_file_test('wasmfs/wasmfs_create.c')
1161811618

11619+
def test_unistd_fdatasync(self):
11620+
# TODO: Remove this test in favor of unistd/misc.c
11621+
self.set_setting('WASMFS')
11622+
self.do_run_in_out_file_test('wasmfs/wasmfs_fdatasync.c')
11623+
1161911624
@also_with_wasmfs
1162011625
def test_unistd_seek(self):
1162111626
self.do_run_in_out_file_test('wasmfs/wasmfs_seek.c')

tests/wasmfs/wasmfs_fdatasync.c

+25
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
/*
2+
* Copyright 2022 The Emscripten Authors. All rights reserved.
3+
* Emscripten is available under two separate licenses, the MIT license and the
4+
* University of Illinois/NCSA Open Source License. Both these licenses can be
5+
* found in the LICENSE file.
6+
*/
7+
8+
#include <assert.h>
9+
#include <fcntl.h>
10+
#include <stdio.h>
11+
#include <sys/stat.h>
12+
#include <sys/types.h>
13+
#include <unistd.h>
14+
15+
int main() {
16+
int fd = creat("foo.txt", 0777);
17+
assert(fd > 0);
18+
19+
int err = fdatasync(fd);
20+
assert(err == 0);
21+
22+
close(fd);
23+
24+
printf("ok\n");
25+
}

tests/wasmfs/wasmfs_fdatasync.out

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
ok

0 commit comments

Comments
 (0)