| ... | ... | @@ -555,10 +555,17 @@ uint32_t wasi_snapshot_preview1_fd_filestat_set_size(uint32_t fd, uint64_t size) |
| 555 | 555 | if (fseek(fds[fd].stream, 0, SEEK_END) < 0) return wasi_errno_io; |
| 556 | 556 | long old_size = ftell(fds[fd].stream); |
| 557 | 557 | if (old_size < 0) return wasi_errno_io; |
| 558 | | if (size > (unsigned long)old_size) { |
| 559 | | if (fseek(fds[fd].stream, size - 1, SEEK_SET) < 0) return wasi_errno_io; |
| 560 | | fputc(0, fds[fd].stream); |
| 561 | | } else if (size < (unsigned long)old_size) panic("unimplemented"); |
| 558 | if (size != (unsigned long)old_size) { |
| 559 | if (size > 0 && fseek(fds[fd].stream, size - 1, SEEK_SET) < 0) return wasi_errno_io; |
| 560 | if (size < (unsigned long)old_size) { |
| 561 | // Note that this destroys the contents on resize might have to save truncated |
| 562 | // file in memory if this becomes an issue. |
| 563 | FILE *trunc = fopen(des[fds[fd].de].host_path, "wb"); |
| 564 | if (trunc == NULL) return wasi_errno_io; |
| 565 | fclose(trunc); |
| 566 | } |
| 567 | if (size > 0) fputc(0, fds[fd].stream); |
| 568 | } |
| 562 | 569 | if (fsetpos(fds[fd].stream, &pos) < 0) return wasi_errno_io; |
| 563 | 570 | return wasi_errno_success; |
| 564 | 571 | } |