From 127ace39b94fabe8d0e26e27b736869597b0fd93 Mon Sep 17 00:00:00 2001 From: Casper Beyer Date: Tue, 30 Jun 2020 02:36:22 +0800 Subject: [PATCH] feat(wasi) implement fd_filestat_set_size (denoland/deno#6558) --- wasi/README.md | 2 +- wasi/snapshot_preview1.ts | 13 ++++++++++++- wasi/testdata/std_fs_file_set_len.rs | 17 +++++++++++++++++ 3 files changed, 30 insertions(+), 2 deletions(-) create mode 100644 wasi/testdata/std_fs_file_set_len.rs diff --git a/wasi/README.md b/wasi/README.md index 6b89db1de484..530a9d8276af 100644 --- a/wasi/README.md +++ b/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/wasi/snapshot_preview1.ts b/wasi/snapshot_preview1.ts index d52b81635ef8..4fe4e02b46f0 100644 --- a/wasi/snapshot_preview1.ts +++ b/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/wasi/testdata/std_fs_file_set_len.rs b/wasi/testdata/std_fs_file_set_len.rs new file mode 100644 index 000000000000..27f1054c7025 --- /dev/null +++ b/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); +}