diff --git a/std/wasi/README.md b/std/wasi/README.md index 6b89db1de4..530a9d8276 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 d52b81635e..4fe4e02b46 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 0000000000..27f1054c70 --- /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); +}