Make xread/xwrite more robust - dedup - deduplicating backup program HTML git clone git://bitreich.org/dedup/ git://enlrupgkhuxnvlhsf6lc3fziv5h2hhfrinws65d7roiv6bfj7d652fid.onion/dedup/ DIR Log DIR Files DIR Refs DIR Tags DIR README DIR LICENSE --- DIR commit 6a3036917037d4f8688c8c6b3edcfeff96f0f775 DIR parent 9220cd260336728228b3ece0f09864b171fd9217 HTML Author: sin <sin@2f30.org> Date: Wed, 21 Mar 2018 23:18:11 +0000 Make xread/xwrite more robust Should work on FIFOs etc. Diffstat: M TODO | 1 - M dedup.c | 42 ++++++++++++++++++++++--------- 2 files changed, 30 insertions(+), 13 deletions(-) --- DIR diff --git a/TODO b/TODO @@ -1,4 +1,3 @@ endianness agnostic version field in entry header lseek64 support -loop around in xread/xwrite so it works on FIFOs etc. DIR diff --git a/dedup.c b/dedup.c @@ -112,23 +112,41 @@ str2bin(char *s, uint8_t *d) ssize_t xread(int fd, void *buf, size_t nbytes) { - ssize_t n; - - n = read(fd, buf, nbytes); - if (n < 0) - err(1, "read"); - return n; + unsigned char *bp = buf; + ssize_t total = 0; + + while (nbytes > 0) { + ssize_t n; + + n = read(fd, &bp[total], nbytes); + if (n < 0) + err(1, "read"); + else if (n == 0) + return total; + total += n; + nbytes -= n; + } + return total; } ssize_t xwrite(int fd, const void *buf, size_t nbytes) { - ssize_t n; - - n = write(fd, buf, nbytes); - if (n < 0) - err(1, "write"); - return n; + const unsigned char *bp = buf; + ssize_t total = 0; + + while (nbytes > 0) { + ssize_t n; + + n = write(fd, &bp[total], nbytes); + if (n < 0) + err(1, "write"); + else if (n == 0) + return total; + total += n; + nbytes -= n; + } + return total; } int