diff --git a/ccan/read_write_all/read_write_all.c b/ccan/read_write_all/read_write_all.c index 7f0b70b9d..c2d42d24c 100644 --- a/ccan/read_write_all/read_write_all.c +++ b/ccan/read_write_all/read_write_all.c @@ -9,10 +9,12 @@ bool write_all(int fd, const void *data, size_t size) ssize_t done; done = write(fd, data, size); - if (done < 0 && errno == EINTR) - continue; - if (done <= 0) + if (done < 0) { + if (errno == EINTR) { + continue; + } return false; + } data = (const char *)data + done; size -= done; } @@ -26,12 +28,20 @@ bool read_all(int fd, void *data, size_t size) ssize_t done; done = read(fd, data, size); - if (done < 0 && errno == EINTR) - continue; - if (done <= 0) + + switch (done) { + case -1: + if (errno == EINTR) { + continue; + } return false; - data = (char *)data + done; - size -= done; + case 0: + errno = EBADMSG; + return false; + default: + data = (char *)data + done; + size -= done; + } } return true; diff --git a/ccan/read_write_all/read_write_all.h b/ccan/read_write_all/read_write_all.h index aa2635aaf..e6bf1acb4 100644 --- a/ccan/read_write_all/read_write_all.h +++ b/ccan/read_write_all/read_write_all.h @@ -4,7 +4,21 @@ #include #include +/** + * Write `size` bytes from `data` to the file descriptor `fd`, retrying on + * transient errors. + * If the data cannot be fully written, then false is returned and errno is set + * to indicate the error. + */ bool write_all(int fd, const void *data, size_t size); + +/** + * Read `size` bytes from the file descriptor `fd` into `data`, retrying on + * transient errors. + * If `size` bytes cannot be read then false is returned and errno is set + * to indicate the error, in which case the contents of `data` is undefined. + * If EOF occurs before `size` bytes were read, then errno is set to EBADMSG. + */ bool read_all(int fd, void *data, size_t size); #endif /* _CCAN_READ_WRITE_H */