diff --git a/std/wasi/README.md b/std/wasi/README.md index b81279dbfb..6b89db1de4 100644 --- a/std/wasi/README.md +++ b/std/wasi/README.md @@ -15,7 +15,7 @@ This module provides an implementation of the WebAssembly System Interface - [ ] fd_advise - [ ] fd_allocate - [x] fd_close -- [ ] fd_datasync +- [x] fd_datasync - [x] fd_fdstat_get - [ ] fd_fdstat_set_flags - [ ] fd_fdstat_set_rights diff --git a/std/wasi/snapshot_preview1.ts b/std/wasi/snapshot_preview1.ts index 78b29b968e..d52b81635e 100644 --- a/std/wasi/snapshot_preview1.ts +++ b/std/wasi/snapshot_preview1.ts @@ -492,7 +492,18 @@ export default class Module { }, fd_datasync: (fd: number): number => { - return ERRNO_NOSYS; + const entry = this.fds[fd]; + if (!entry) { + return ERRNO_BADF; + } + + try { + Deno.fdatasyncSync(entry.handle.rid); + } catch (err) { + return errno(err); + } + + return ERRNO_SUCCESS; }, fd_fdstat_get: (fd: number, stat_out: number): number => { diff --git a/std/wasi/testdata/std_fs_file_sync_data.rs b/std/wasi/testdata/std_fs_file_sync_data.rs new file mode 100644 index 0000000000..f19f8e9b7f --- /dev/null +++ b/std/wasi/testdata/std_fs_file_sync_data.rs @@ -0,0 +1,15 @@ +// { "preopens": { "/scratch": "scratch" } } + +use std::io::Write; + +fn main() { + let mut file = std::fs::File::create("/scratch/file").unwrap(); + + assert!(file.write(b"Hello").is_ok()); + assert!(file.sync_data().is_ok()); + assert_eq!(std::fs::read("/scratch/file").unwrap(), b"Hello"); + + assert!(file.write(b", world!").is_ok()); + assert!(file.sync_data().is_ok()); + assert_eq!(std::fs::read("/scratch/file").unwrap(), b"Hello, world!"); +}