zig

fork of https://codeberg.org/ziglang/zig
Log | Files | Refs | README | LICENSE

commit e000db278295e6ca61e1d078ae32f83a9abca6da (tree)
parent 12633467747de2349f7cc0f5068e9749801476b0
Author: Jacob Young <jacobly0@users.noreply.github.com>
Date:   Wed, 30 Nov 2022 01:26:25 -0500

wasi: implement file truncation

The way this is implemented destroys the contents of the file, so
revisit if this causes issues in the future.

Diffstat:
Mstage1/wasi.c | 15+++++++++++----
1 file changed, 11 insertions(+), 4 deletions(-)

diff --git a/stage1/wasi.c b/stage1/wasi.c @@ -555,10 +555,17 @@ uint32_t wasi_snapshot_preview1_fd_filestat_set_size(uint32_t fd, uint64_t size) if (fseek(fds[fd].stream, 0, SEEK_END) < 0) return wasi_errno_io; long old_size = ftell(fds[fd].stream); if (old_size < 0) return wasi_errno_io; - if (size > (unsigned long)old_size) { - if (fseek(fds[fd].stream, size - 1, SEEK_SET) < 0) return wasi_errno_io; - fputc(0, fds[fd].stream); - } else if (size < (unsigned long)old_size) panic("unimplemented"); + if (size != (unsigned long)old_size) { + if (size > 0 && fseek(fds[fd].stream, size - 1, SEEK_SET) < 0) return wasi_errno_io; + if (size < (unsigned long)old_size) { + // Note that this destroys the contents on resize might have to save truncated + // file in memory if this becomes an issue. + FILE *trunc = fopen(des[fds[fd].de].host_path, "wb"); + if (trunc == NULL) return wasi_errno_io; + fclose(trunc); + } + if (size > 0) fputc(0, fds[fd].stream); + } if (fsetpos(fds[fd].stream, &pos) < 0) return wasi_errno_io; return wasi_errno_success; }