mirror of
https://github.com/ziglang/zig.git
synced 2024-11-27 07:32:44 +00:00
wasi: implement file truncation
The way this is implemented destroys the contents of the file, so revisit if this causes issues in the future.
This commit is contained in:
parent
1263346774
commit
e000db2782
@ -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;
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user