Commit e000db2782

Jacob Young <jacobly0@users.noreply.github.com>
2022-11-30 07:26:25
wasi: implement file truncation
The way this is implemented destroys the contents of the file, so revisit if this causes issues in the future.
1 parent 1263346
Changed files (1)
stage1
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;
 }