handle partial reads/writes

This commit is contained in:
Thomas Lindner 2023-02-12 23:50:44 +01:00
parent fc2b116dc2
commit 9281b75737

View file

@ -25,6 +25,7 @@
#include <ufs/ufs/dir.h> #include <ufs/ufs/dir.h>
#include <err.h> #include <err.h>
#include <errno.h>
#include <fcntl.h> #include <fcntl.h>
#include <inttypes.h> #include <inttypes.h>
#include <stdio.h> #include <stdio.h>
@ -430,31 +431,45 @@ void *xmalloc(size_t size) {
} }
void xpread(int fd, void *buf, size_t nbytes, off_t offset) { void xpread(int fd, void *buf, size_t nbytes, off_t offset) {
while (nbytes) {
ssize_t n = pread(fd, buf, nbytes, offset); ssize_t n = pread(fd, buf, nbytes, offset);
if (n == -1) { if (n == -1) {
if (errno == EINTR) {
continue;
}
err(1, "pread() failed"); err(1, "pread() failed");
} }
if (n != nbytes) { buf = (char *)buf + n;
errx(1, "Incomplete read"); nbytes -= n;
offset += n;
} }
} }
void xwrite(int fd, void *buf, size_t nbytes) { void xwrite(int fd, void *buf, size_t nbytes) {
while (nbytes) {
ssize_t n = write(fd, buf, nbytes); ssize_t n = write(fd, buf, nbytes);
if (n == -1) { if (n == -1) {
if (errno == EINTR || errno == EAGAIN) {
continue;
}
err(1, "write() failed"); err(1, "write() failed");
} }
if (n != nbytes) { buf = (char *)buf + n;
errx(1, "Incomplete write"); nbytes -= n;
} }
} }
void xpwrite(int fd, void *buf, size_t nbytes, off_t offset) { void xpwrite(int fd, void *buf, size_t nbytes, off_t offset) {
while (nbytes) {
ssize_t n = pwrite(fd, buf, nbytes, offset); ssize_t n = pwrite(fd, buf, nbytes, offset);
if (n == -1) { if (n == -1) {
if (errno == EINTR) {
continue;
}
err(1, "pwrite() failed"); err(1, "pwrite() failed");
} }
if (n != nbytes) { buf = (char *)buf + n;
errx(1, "Incomplete write"); nbytes -= n;
offset += n;
} }
} }