Improve docs of more system calls

This change also found a few POSIX compliance bugs with errnos. Another
bug was discovered where, on Windows, pread() and pwrite() could modify
the file position in cases where ReadFile() returned an error e.g. when
seeking past the end of file. We also have more tests!
This commit is contained in:
Justine Tunney 2022-10-02 22:14:33 -07:00
parent af24c19db3
commit ccbae7799e
No known key found for this signature in database
GPG key ID: BE714B4575D6E328
39 changed files with 589 additions and 175 deletions

View file

@ -37,10 +37,12 @@
static textwindows ssize_t sys_read_nt_impl(struct Fd *fd, void *data,
size_t size, ssize_t offset) {
bool32 ok;
int64_t p;
uint32_t got, avail;
struct NtOverlapped overlap;
// our terrible polling mechanism
if (GetFileType(fd->handle) == kNtFileTypePipe) {
for (;;) {
if (!PeekNamedPipe(fd->handle, 0, 0, 0, &avail, 0)) break;
@ -62,11 +64,15 @@ static textwindows ssize_t sys_read_nt_impl(struct Fd *fd, void *data,
_npassert(SetFilePointerEx(fd->handle, 0, &p, SEEK_CUR));
}
if (ReadFile(fd->handle, data, _clampio(size), &got,
_offset2overlap(fd->handle, offset, &overlap))) {
if (offset != -1) {
_npassert(SetFilePointerEx(fd->handle, p, 0, SEEK_SET));
}
ok = ReadFile(fd->handle, data, _clampio(size), &got,
_offset2overlap(fd->handle, offset, &overlap));
if (offset != -1) {
// windows clobbers file pointer even on error
_npassert(SetFilePointerEx(fd->handle, p, 0, SEEK_SET));
}
if (ok) {
return got;
}