From 3c5459d849502963ee44efc7c3137db6e9fc63b2 Mon Sep 17 00:00:00 2001 From: Casper Beyer Date: Tue, 30 Jun 2020 02:36:22 +0800 Subject: [PATCH] feat(std/wasi) implement fd_filestat_set_size (#6558) --- std/wasi/README.md | 2 +- std/wasi/snapshot_preview1.ts | 13 ++++++++++++- std/wasi/testdata/std_fs_file_set_len.rs | 17 +++++++++++++++++ 3 files changed, 30 insertions(+), 2 deletions(-) create mode 100644 std/wasi/testdata/std_fs_file_set_len.rs diff --git a/std/wasi/README.md b/std/wasi/README.md index 6b89db1de4849e..530a9d8276af60 100644 --- a/std/wasi/README.md +++ b/std/wasi/README.md @@ -20,7 +20,7 @@ This module provides an implementation of the WebAssembly System Interface - [ ] fd_fdstat_set_flags - [ ] fd_fdstat_set_rights - [x] fd_filestat_get -- [ ] fd_filestat_set_size +- [x] fd_filestat_set_size - [x] fd_filestat_set_times - [x] fd_pread - [x] fd_prestat_get diff --git a/std/wasi/snapshot_preview1.ts b/std/wasi/snapshot_preview1.ts index d52b81635ef87f..4fe4e02b46f0f9 100644 --- a/std/wasi/snapshot_preview1.ts +++ b/std/wasi/snapshot_preview1.ts @@ -607,7 +607,18 @@ export default class Module { }, fd_filestat_set_size: (fd: number, size: bigint): number => { - return ERRNO_NOSYS; + const entry = this.fds[fd]; + if (!entry) { + return ERRNO_BADF; + } + + try { + Deno.ftruncateSync(entry.handle.rid, Number(size)); + } catch (err) { + return errno(err); + } + + return ERRNO_SUCCESS; }, fd_filestat_set_times: ( diff --git a/std/wasi/testdata/std_fs_file_set_len.rs b/std/wasi/testdata/std_fs_file_set_len.rs new file mode 100644 index 00000000000000..27f1054c70250f --- /dev/null +++ b/std/wasi/testdata/std_fs_file_set_len.rs @@ -0,0 +1,17 @@ +// { "preopens": { "/scratch": "scratch" } } + +fn main() { + let file = std::fs::File::create("/scratch/file").unwrap(); + + assert!(file.set_len(0).is_ok()); + assert_eq!(file.metadata().unwrap().len(), 0); + + assert!(file.set_len(5).is_ok()); + assert_eq!(file.metadata().unwrap().len(), 5); + + assert!(file.set_len(25).is_ok()); + assert_eq!(file.metadata().unwrap().len(), 25); + + assert!(file.set_len(0).is_ok()); + assert_eq!(file.metadata().unwrap().len(), 0); +}