commit 6a3036917037d4f8688c8c6b3edcfeff96f0f775
parent 9220cd260336728228b3ece0f09864b171fd9217
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(-)
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.
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