mirror of
https://github.com/ziglang/zig.git
synced 2025-12-06 06:13:07 +00:00
wasi.c: fix pread/pwrite
When stream is NULL it means reads should read 0 bytes and writes should fake success with no side effects.
This commit is contained in:
parent
5c6679922d
commit
5bc95a6fa2
@ -520,12 +520,15 @@ uint32_t wasi_snapshot_preview1_fd_read(uint32_t fd, uint32_t iovs, uint32_t iov
|
||||
default: panic("unimplemented: fd_read special file");
|
||||
}
|
||||
|
||||
if (fds[fd].stream == NULL) {
|
||||
store32_align2(res_size_ptr, 0);
|
||||
return wasi_errno_success;
|
||||
}
|
||||
|
||||
size_t size = 0;
|
||||
for (uint32_t i = 0; i < iovs_len; i += 1) {
|
||||
uint32_t len = load32_align2(&iovs_ptr[i].len);
|
||||
size_t read_size = 0;
|
||||
if (fds[fd].stream != NULL)
|
||||
read_size = fread(&m[load32_align2(&iovs_ptr[i].ptr)], 1, len, fds[fd].stream);
|
||||
size_t read_size = fread(&m[load32_align2(&iovs_ptr[i].ptr)], 1, len, fds[fd].stream);
|
||||
size += read_size;
|
||||
if (read_size < len) break;
|
||||
}
|
||||
@ -633,8 +636,10 @@ uint32_t wasi_snapshot_preview1_fd_pwrite(uint32_t fd, uint32_t iovs, uint32_t i
|
||||
}
|
||||
|
||||
fpos_t pos;
|
||||
if (fds[fd].stream != NULL) {
|
||||
if (fgetpos(fds[fd].stream, &pos) < 0) return wasi_errno_io;
|
||||
if (fseek(fds[fd].stream, offset, SEEK_SET) < 0) return wasi_errno_io;
|
||||
}
|
||||
|
||||
size_t size = 0;
|
||||
for (uint32_t i = 0; i < iovs_len; i += 1) {
|
||||
@ -648,7 +653,9 @@ uint32_t wasi_snapshot_preview1_fd_pwrite(uint32_t fd, uint32_t iovs, uint32_t i
|
||||
if (written_size < len) break;
|
||||
}
|
||||
|
||||
if (fds[fd].stream != NULL) {
|
||||
if (fsetpos(fds[fd].stream, &pos) < 0) return wasi_errno_io;
|
||||
}
|
||||
|
||||
if (size > 0) {
|
||||
time_t now = time(NULL);
|
||||
@ -964,6 +971,11 @@ uint32_t wasi_snapshot_preview1_fd_pread(uint32_t fd, uint32_t iovs, uint32_t io
|
||||
default: panic("unimplemented: fd_pread special file");
|
||||
}
|
||||
|
||||
if (fds[fd].stream == NULL) {
|
||||
store32_align2(res_size_ptr, 0);
|
||||
return wasi_errno_success;
|
||||
}
|
||||
|
||||
fpos_t pos;
|
||||
if (fgetpos(fds[fd].stream, &pos) < 0) return wasi_errno_io;
|
||||
if (fseek(fds[fd].stream, offset, SEEK_SET) < 0) return wasi_errno_io;
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user