dedup

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

commit 27d4cd35a9abcf80ec50ae3a55fff5fc22dd3e99
parent e11aca811109d7e85694e8fbe2773ef5b9068e19
Author: sin <sin@2f30.org>
Date:   Wed, 21 Mar 2018 10:21:23 +0000

Implement check function

Diffstat:
Mdedup.c | 36+++++++++++++++++++++++++++++-------
1 file changed, 29 insertions(+), 7 deletions(-)

diff --git a/dedup.c b/dedup.c @@ -307,14 +307,16 @@ term(void) } void -dump_index(void) +check(void) { - struct ent *ent; - uint64_t i; + uint64_t i, j; - dump_enthdr(&enthdr); lseek(ifd, sizeof(enthdr), SEEK_SET); for (i = 0; i < enthdr.nents; i++) { + unsigned char md[SHA256_DIGEST_LENGTH]; + SHA256_CTX ctx; + struct ent *ent; + ent = alloc_ent(); if (xread(ifd, ent, sizeof(*ent)) == 0) errx(1, "unexpected EOF"); @@ -322,7 +324,19 @@ dump_index(void) if (xread(ifd, ent->blks, ent->nblks * sizeof(ent->blks[0])) == 0) errx(1, "unexpected EOF"); - dump_ent(ent); + + SHA256_Init(&ctx); + for (j = 0; j < ent->nblks; j++) { + struct blk blk; + + read_blk(&blk, ent->blks[j]); + SHA256_Update(&ctx, blk.data, blk.sz); + } + SHA256_Final(md, &ctx); + + if (memcmp(ent->md, md, sizeof(ent->md)) != 0) + errx(1, "hash mismatch"); + free(ent); } } @@ -347,7 +361,7 @@ list(void) void usage(void) { - fprintf(stderr, "usage: %s [-lv] [-e id]\n", argv0); + fprintf(stderr, "usage: %s [-clv] [-e id]\n", argv0); exit(1); } @@ -355,9 +369,12 @@ int main(int argc, char *argv[]) { unsigned char *id = NULL; - int lflag = 0; + int lflag = 0, cflag = 0; ARGBEGIN { + case 'c': + cflag = 1; + break; case 'e': id = EARGF(usage()); break; @@ -373,6 +390,11 @@ main(int argc, char *argv[]) init(); + if (cflag) { + check(); + return 0; + } + if (lflag) { list(); return 0;