mirror of
https://github.com/jart/cosmopolitan.git
synced 2025-06-27 06:48:31 +00:00
Fix writev() on the New Technology (#117)
This commit is contained in:
parent
6cd1037692
commit
83d0c3b870
8 changed files with 113 additions and 31 deletions
|
@ -26,35 +26,37 @@
|
|||
#include "libc/nt/struct/overlapped.h"
|
||||
#include "libc/sysv/errfuns.h"
|
||||
|
||||
static textwindows ssize_t sys_read_nt_impl(struct Fd *fd, void *data,
|
||||
size_t size, ssize_t offset) {
|
||||
uint32_t got;
|
||||
struct NtOverlapped overlap;
|
||||
if (ReadFile(fd->handle, data, clampio(size), &got,
|
||||
offset2overlap(offset, &overlap))) {
|
||||
return got;
|
||||
} else if (GetLastError() == kNtErrorBrokenPipe) {
|
||||
return 0;
|
||||
} else {
|
||||
return __winerr();
|
||||
}
|
||||
}
|
||||
|
||||
textwindows ssize_t sys_read_nt(struct Fd *fd, const struct iovec *iov,
|
||||
size_t iovlen, ssize_t opt_offset) {
|
||||
ssize_t rc;
|
||||
uint32_t size;
|
||||
size_t i, total;
|
||||
uint32_t got, size;
|
||||
struct NtOverlapped overlap;
|
||||
while (iovlen && !iov[0].iov_len) iov++, iovlen--;
|
||||
if (iovlen) {
|
||||
for (total = i = 0; i < iovlen; ++i) {
|
||||
size = clampio(iov[i].iov_len);
|
||||
if (ReadFile(fd->handle, iov[i].iov_base, size, &got,
|
||||
offset2overlap(opt_offset, &overlap))) {
|
||||
total += got;
|
||||
if (opt_offset != -1) opt_offset += got;
|
||||
if (got < iov[i].iov_len) break;
|
||||
} else if (GetLastError() == kNtErrorBrokenPipe) {
|
||||
break; /* read() doesn't EPIPE lool */
|
||||
} else {
|
||||
return __winerr();
|
||||
}
|
||||
if (!iov[i].iov_len) continue;
|
||||
rc = sys_read_nt_impl(fd, iov[i].iov_base, iov[i].iov_len, opt_offset);
|
||||
if (rc == -1) return -1;
|
||||
total += rc;
|
||||
if (opt_offset != -1) opt_offset += rc;
|
||||
if (rc < iov[i].iov_len) break;
|
||||
}
|
||||
return total;
|
||||
} else {
|
||||
if (ReadFile(fd->handle, NULL, 0, &got,
|
||||
offset2overlap(opt_offset, &overlap))) {
|
||||
return got;
|
||||
} else if (GetLastError() == kNtErrorBrokenPipe) {
|
||||
return 0;
|
||||
} else {
|
||||
return __winerr();
|
||||
}
|
||||
return sys_read_nt_impl(fd, NULL, 0, opt_offset);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -25,8 +25,22 @@
|
|||
#include "libc/nt/struct/overlapped.h"
|
||||
#include "libc/sysv/errfuns.h"
|
||||
|
||||
static textwindows ssize_t sys_write_nt_impl(struct Fd *fd, void *data,
|
||||
size_t size, ssize_t offset) {
|
||||
uint32_t sent;
|
||||
struct NtOverlapped overlap;
|
||||
if (WriteFile(fd->handle, data, clampio(size), &sent,
|
||||
offset2overlap(offset, &overlap))) {
|
||||
/* TODO(jart): Trigger SIGPIPE on kNtErrorBrokenPipe */
|
||||
return sent;
|
||||
} else {
|
||||
return __winerr();
|
||||
}
|
||||
}
|
||||
|
||||
textwindows ssize_t sys_write_nt(struct Fd *fd, const struct iovec *iov,
|
||||
size_t iovlen, ssize_t opt_offset) {
|
||||
ssize_t rc;
|
||||
size_t i, total;
|
||||
uint32_t size, wrote;
|
||||
struct NtOverlapped overlap;
|
||||
|
@ -34,15 +48,11 @@ textwindows ssize_t sys_write_nt(struct Fd *fd, const struct iovec *iov,
|
|||
if (iovlen) {
|
||||
for (total = i = 0; i < iovlen; ++i) {
|
||||
if (!iov[i].iov_len) continue;
|
||||
size = clampio(iov[0].iov_len);
|
||||
if (WriteFile(fd->handle, iov[i].iov_base, size, &wrote,
|
||||
offset2overlap(opt_offset, &overlap))) {
|
||||
total += wrote;
|
||||
if (opt_offset != -1) opt_offset += wrote;
|
||||
if (wrote < iov[i].iov_len) break;
|
||||
} else {
|
||||
return __winerr();
|
||||
}
|
||||
rc = sys_write_nt_impl(fd, iov[i].iov_base, iov[i].iov_len, opt_offset);
|
||||
if (rc == -1) return -1;
|
||||
total += rc;
|
||||
if (opt_offset != -1) opt_offset += rc;
|
||||
if (rc < iov[i].iov_len) break;
|
||||
}
|
||||
if (!total) assert(!__iovec_size(iov, iovlen));
|
||||
return total;
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue