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 <err.h>
#include <errno.h>
#include <fcntl.h>
#include <inttypes.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) {
ssize_t n = pread(fd, buf, nbytes, offset);
if (n == -1) {
err(1, "pread() failed");
}
if (n != nbytes) {
errx(1, "Incomplete read");
while (nbytes) {
ssize_t n = pread(fd, buf, nbytes, offset);
if (n == -1) {
if (errno == EINTR) {
continue;
}
err(1, "pread() failed");
}
buf = (char *)buf + n;
nbytes -= n;
offset += n;
}
}
void xwrite(int fd, void *buf, size_t nbytes) {
ssize_t n = write(fd, buf, nbytes);
if (n == -1) {
err(1, "write() failed");
}
if (n != nbytes) {
errx(1, "Incomplete write");
while (nbytes) {
ssize_t n = write(fd, buf, nbytes);
if (n == -1) {
if (errno == EINTR || errno == EAGAIN) {
continue;
}
err(1, "write() failed");
}
buf = (char *)buf + n;
nbytes -= n;
}
}
void xpwrite(int fd, void *buf, size_t nbytes, off_t offset) {
ssize_t n = pwrite(fd, buf, nbytes, offset);
if (n == -1) {
err(1, "pwrite() failed");
}
if (n != nbytes) {
errx(1, "Incomplete write");
while (nbytes) {
ssize_t n = pwrite(fd, buf, nbytes, offset);
if (n == -1) {
if (errno == EINTR) {
continue;
}
err(1, "pwrite() failed");
}
buf = (char *)buf + n;
nbytes -= n;
offset += n;
}
}