dedup

data deduplication program
git clone git://git.2f30.org/dedup.git
Log | Files | Refs | LICENSE

commit d0b30d153d3b50be6952cfdf5613239ad46d576f
parent 6f277b62999ea260b734120ad894fa3d16357daf
Author: sin <sin@2f30.org>
Date:   Wed, 21 Mar 2018 09:44:12 +0000

Wrapper to check for read/write failures

Diffstat:
dedup.c | 46+++++++++++++++++++++++++++++++++-------------
1 file changed, 33 insertions(+), 13 deletions(-)

diff --git a/dedup.c b/dedup.c @@ -88,19 +88,41 @@ dump_blk(struct blk *blk) fprintf(stderr, "blk->sz: %lld\n", (unsigned long long)blk->sz); } +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; +} + +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; +} + void append_ent(struct ent *ent) { /* Update index header */ enthdr.nents++; lseek(ifd, 0, SEEK_SET); - write(ifd, &enthdr, sizeof(enthdr)); + xwrite(ifd, &enthdr, sizeof(enthdr)); /* Append entry */ lseek(ifd, 0, SEEK_END); ent->sz = sizeof(*ent); ent->sz += ent->nblks * sizeof(ent->blks[0]); - write(ifd, ent, ent->sz); + xwrite(ifd, ent, ent->sz); } struct ent * @@ -142,14 +164,14 @@ void read_blk(struct blk *blk, off_t blkidx) { lseek(sfd, blkidx * sizeof(*blk), SEEK_SET); - read(sfd, blk, sizeof(*blk)); + xread(sfd, blk, sizeof(*blk)); } void append_blk(struct blk *blk) { lseek(sfd, 0, SEEK_END); - write(sfd, blk, sizeof(*blk)); + xwrite(sfd, blk, sizeof(*blk)); } int @@ -182,7 +204,7 @@ dedup(int fd) ent = alloc_ent(); SHA256_Init(&ctx); - while ((n = read(fd, blk.data, BLKSIZ)) > 0) { + while ((n = xread(fd, blk.data, BLKSIZ)) > 0) { uint64_t blkidx; blk.sz = n; @@ -207,8 +229,6 @@ dedup(int fd) ent->blks[ent->nblks++] = blkidx; } } - if (n < 0) - err(1, "read"); /* Calculate hash and add this entry to the index */ SHA256_Final(ent->md, &ctx); @@ -236,9 +256,9 @@ extract(unsigned char *id, int fd) lseek(ifd, sizeof(enthdr), SEEK_SET); for (i = 0; i < enthdr.nents; i++) { ent = alloc_ent(); - read(ifd, ent, sizeof(*ent)); + xread(ifd, ent, sizeof(*ent)); ent = grow_ent(ent, ent->nblks); - read(ifd, ent->blks, ent->nblks * sizeof(ent->blks[0])); + xread(ifd, ent->blks, ent->nblks * sizeof(ent->blks[0])); if (memcmp(ent->md, md, sizeof(ent->md)) == 0) { uint64_t j; @@ -246,7 +266,7 @@ extract(unsigned char *id, int fd) struct blk blk; read_blk(&blk, ent->blks[j]); - write(1, blk.data, blk.sz); + xwrite(1, blk.data, blk.sz); } break; } @@ -270,7 +290,7 @@ init(void) if (fstat(ifd, &sb) == -1) err(1, "stat index"); if (sb.st_size != 0) - read(ifd, &enthdr, sizeof(enthdr)); + xread(ifd, &enthdr, sizeof(enthdr)); } void @@ -292,9 +312,9 @@ dump_index(void) lseek(ifd, sizeof(enthdr), SEEK_SET); for (i = 0; i < enthdr.nents; i++) { ent = alloc_ent(); - read(ifd, ent, sizeof(*ent)); + xread(ifd, ent, sizeof(*ent)); ent = grow_ent(ent, ent->nblks); - read(ifd, ent->blks, ent->nblks * sizeof(ent->blks[0])); + xread(ifd, ent->blks, ent->nblks * sizeof(ent->blks[0])); dump_ent(ent); free(ent); }