commit 742ebd2af9a3c9215afc63c276db4e9e1ba0e73c
parent 09d67581b67a79e9948d48e20b2f0f64e7fd0123
Author: sin <sin@2f30.org>
Date: Tue, 19 Feb 2019 09:23:35 +0000
Add error wrapper for lseek
Diffstat:
M | dedup.c | | | 25 | ++++++++++++++++++------- |
1 file changed, 18 insertions(+), 7 deletions(-)
diff --git a/dedup.c b/dedup.c
@@ -225,6 +225,17 @@ str2bin(char *s, uint8_t *d)
sscanf(s, "%2hhx", &d[i]);
}
+off_t
+xlseek(int fd, off_t offset, int whence)
+{
+ int ret;
+
+ ret = lseek(fd, offset, whence);
+ if (ret < 0)
+ err(1, "lseek");
+ return ret;
+}
+
ssize_t
xread(int fd, void *buf, size_t nbytes)
{
@@ -305,7 +316,7 @@ flush_cache(void)
if (!cache_dirty)
return;
- lseek(cfd, 0, SEEK_SET);
+ xlseek(cfd, 0, SEEK_SET);
RB_FOREACH(cent, cache, &cache_head)
xwrite(cfd, ¢->bdescr, sizeof(cent->bdescr));
}
@@ -336,11 +347,11 @@ append_ent(struct ent *ent)
{
/* Update index header */
enthdr.nents++;
- lseek(ifd, 0, SEEK_SET);
+ xlseek(ifd, 0, SEEK_SET);
xwrite(ifd, &enthdr, sizeof(enthdr));
/* Append entry */
- lseek(ifd, 0, SEEK_END);
+ xlseek(ifd, 0, SEEK_END);
ent->size = sizeof(*ent);
ent->size += ent->nblks * sizeof(ent->bdescr[0]);
xwrite(ifd, ent, ent->size);
@@ -394,7 +405,7 @@ hash_blk(uint8_t *buf, size_t size, uint8_t *md)
void
read_blk(uint8_t *buf, struct bdescr *bdescr)
{
- lseek(sfd, bdescr->offset, SEEK_SET);
+ xlseek(sfd, bdescr->offset, SEEK_SET);
if (xread(sfd, buf, bdescr->size) == 0)
errx(1, "read: unexpected EOF");
}
@@ -402,7 +413,7 @@ read_blk(uint8_t *buf, struct bdescr *bdescr)
void
append_blk(uint8_t *buf, struct bdescr *bdescr)
{
- lseek(sfd, enthdr.store_size, SEEK_SET);
+ xlseek(sfd, enthdr.store_size, SEEK_SET);
xwrite(sfd, buf, bdescr->size);
enthdr.store_size += bdescr->size;
}
@@ -620,7 +631,7 @@ walk(int (*fn)(struct ent *, void *), void *arg)
uint64_t i;
ent = alloc_ent();
- lseek(ifd, sizeof(enthdr), SEEK_SET);
+ xlseek(ifd, sizeof(enthdr), SEEK_SET);
for (i = 0; i < enthdr.nents; i++) {
if (xread(ifd, ent, sizeof(*ent)) == 0)
errx(1, "read: unexpected EOF");
@@ -647,7 +658,7 @@ init_cache(void)
avg = 0;
nents = cache_nents();
- lseek(cfd, 0, SEEK_SET);
+ xlseek(cfd, 0, SEEK_SET);
for (i = 0; i < nents; i++) {
struct cent *cent;