Skip to content

Commit

Permalink
feat(wasi) implement fd_filestat_set_size (denoland/deno#6558)
Browse files Browse the repository at this point in the history
  • Loading branch information
caspervonb committed Jan 31, 2021
1 parent 9ce030e commit 533e1d8
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 2 deletions.
2 changes: 1 addition & 1 deletion wasi/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
13 changes: 12 additions & 1 deletion wasi/snapshot_preview1.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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: (
Expand Down
17 changes: 17 additions & 0 deletions wasi/testdata/std_fs_file_set_len.rs
Original file line number Diff line number Diff line change
@@ -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);
}

0 comments on commit 533e1d8

Please sign in to comment.