From e000db278295e6ca61e1d078ae32f83a9abca6da Mon Sep 17 00:00:00 2001 From: Jacob Young Date: Wed, 30 Nov 2022 01:26:25 -0500 Subject: [PATCH] wasi: implement file truncation The way this is implemented destroys the contents of the file, so revisit if this causes issues in the future. --- stage1/wasi.c | 15 +++++++++++---- 1 file changed, 11 insertions(+), 4 deletions(-) diff --git a/stage1/wasi.c b/stage1/wasi.c index 8ff04d5ef4..c378fc3a20 100644 --- 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; }