From 4cde7fdc9a52088f8dfe3e23569d6be2faa80d0f Mon Sep 17 00:00:00 2001 From: Casper Beyer Date: Mon, 29 Jun 2020 22:44:08 +0800 Subject: [PATCH] feat(std/wasi): implement fd_datasync (#6556) --- std/wasi/README.md | 2 +- std/wasi/snapshot_preview1.ts | 13 ++++++++++++- std/wasi/testdata/std_fs_file_sync_data.rs | 15 +++++++++++++++ 3 files changed, 28 insertions(+), 2 deletions(-) create mode 100644 std/wasi/testdata/std_fs_file_sync_data.rs 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!"); +}