dedup

deduplicating backup program
git clone git://git.2f30.org/dedup
Log | Files | Refs | README | LICENSE

commit 34990ad2e3066a5fd23cebc22b997dff63ac272a
parent cf95ada626787205b2152ce6fe00e343c4413637
Author: sin <sin@2f30.org>
Date:   Wed, 21 Mar 2018 16:03:26 +0000

Plug some leaks

Diffstat:
Mdedup.c | 49+++++++++++++++++++++++++++++++++++++------------
1 file changed, 37 insertions(+), 12 deletions(-)

diff --git a/dedup.c b/dedup.c @@ -180,6 +180,17 @@ flush_cache(void) } void +free_cache(void) +{ + struct cache_ent *ent, *tmp; + + RB_FOREACH_SAFE(ent, cache, &cache_head, tmp) { + RB_REMOVE(cache, &cache_head, ent); + free(ent); + } +} + +void append_ent(struct ent *ent) { /* Update index header */ @@ -279,29 +290,38 @@ extract(char *id, int fd) nblks = storefile_nblks(); lseek(ifd, sizeof(enthdr), SEEK_SET); for (i = 0; i < enthdr.nents; i++) { + uint64_t j; struct ent *ent; + /* Load index entry */ ent = alloc_ent(); if (xread(ifd, ent, sizeof(*ent)) == 0) errx(1, "unexpected EOF"); + + if (memcmp(ent->md, md, sizeof(ent->md)) != 0) { + free(ent); + /* Skip over index entry block table */ + lseek(ifd, ent->nblks * sizeof(ent->blks[0]), SEEK_CUR); + continue; + } + + /* Load index entry block table */ ent = grow_ent(ent, ent->nblks); if (xread(ifd, ent->blks, ent->nblks * sizeof(ent->blks[0])) == 0) errx(1, "unexpected EOF"); - if (memcmp(ent->md, md, sizeof(ent->md)) == 0) { - uint64_t j; - - for (j = 0; j < ent->nblks; j++) { - struct blk blk; - - if (ent->blks[j] > nblks) - errx(1, "index is corrupted"); - read_blk(&blk, ent->blks[j]); - xwrite(fd, blk.data, blk.sz); - } - break; + + /* Blast file blocks to file descriptor */ + for (j = 0; j < ent->nblks; j++) { + struct blk blk; + + if (ent->blks[j] > nblks) + errx(1, "index is corrupted"); + read_blk(&blk, ent->blks[j]); + xwrite(fd, blk.data, blk.sz); } free(ent); + break; } } @@ -485,6 +505,8 @@ init(void) void term(void) { + free_cache(); + fsync(ifd); fsync(sfd); fsync(cfd); @@ -531,11 +553,13 @@ main(int argc, char *argv[]) if (cflag) { check(); + term(); return 0; } if (lflag) { list(); + term(); return 0; } @@ -560,4 +584,5 @@ main(int argc, char *argv[]) } term(); + return 0; }