Re-order some functions - dedup - deduplicating backup program
 (HTM) git clone git://bitreich.org/dedup/ git://enlrupgkhuxnvlhsf6lc3fziv5h2hhfrinws65d7roiv6bfj7d652fid.onion/dedup/
 (DIR) Log
 (DIR) Files
 (DIR) Refs
 (DIR) Tags
 (DIR) README
 (DIR) LICENSE
       ---
 (DIR) commit 6a723da8930261861bc8eec788c0affc47ae8b3f
 (DIR) parent ccaebcb0909f2585ee339cce7f70cfbc4849b186
 (HTM) Author: sin <sin@2f30.org>
       Date:   Wed, 21 Mar 2018 14:50:16 +0000
       
       Re-order some functions
       
       Diffstat:
         M dedup.c                             |     162 +++++++++++++++---------------
       
       1 file changed, 81 insertions(+), 81 deletions(-)
       ---
 (DIR) diff --git a/dedup.c b/dedup.c
       @@ -100,6 +100,15 @@ dump_blk(struct blk *blk)
                fprintf(stderr, "blk->sz: %lld\n", (unsigned long long)blk->sz);
        }
        
       +void
       +str2bin(char *s, uint8_t *d)
       +{
       +        size_t i, len = strlen(s) / 2;
       +
       +        for (i = 0; i < len; i++, s += 2)
       +                sscanf(s, "%2hhx", &d[i]);
       +}
       +
        ssize_t
        xread(int fd, void *buf, size_t nbytes)
        {
       @@ -261,6 +270,42 @@ lookup_blk(struct blk *blk, uint64_t *blkidx)
        }
        
        void
       +extract(char *id, int fd)
       +{
       +        unsigned char md[SHA256_DIGEST_LENGTH];
       +        uint64_t nblks, i;
       +
       +        str2bin(id, md);
       +        nblks = storefile_nblks();
       +        lseek(ifd, sizeof(enthdr), SEEK_SET);
       +        for (i = 0; i < enthdr.nents; i++) {
       +                struct ent *ent;
       +
       +                ent = alloc_ent();
       +                if (xread(ifd, ent, sizeof(*ent)) == 0)
       +                        errx(1, "unexpected EOF");
       +                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;
       +                }
       +                free(ent);
       +        }
       +}
       +
       +void
        dedup(int fd)
        {
                struct blk blk;
       @@ -307,24 +352,15 @@ dedup(int fd)
        }
        
        void
       -str2bin(char *s, uint8_t *d)
       -{
       -        size_t i, len = strlen(s) / 2;
       -
       -        for (i = 0; i < len; i++, s += 2)
       -                sscanf(s, "%2hhx", &d[i]);
       -}
       -
       -void
       -extract(char *id, int fd)
       +check(void)
        {
       -        unsigned char md[SHA256_DIGEST_LENGTH];
       -        uint64_t nblks, i;
       +        uint64_t nblks, i, j;
        
       -        str2bin(id, md);
                nblks = storefile_nblks();
                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();
       @@ -334,24 +370,46 @@ extract(char *id, int fd)
                        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;
       +                SHA256_Init(&ctx);
       +                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;
       +                        if (ent->blks[j] > nblks)
       +                                errx(1, "index is corrupted");
       +                        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);
                }
        }
        
        void
       +list(void)
       +{
       +        uint64_t i;
       +
       +        lseek(ifd, sizeof(enthdr), SEEK_SET);
       +        for (i = 0; i < enthdr.nents; i++) {
       +                struct ent ent;
       +                size_t i;
       +
       +                if (xread(ifd, &ent, sizeof(ent)) == 0)
       +                        errx(1, "unexpected EOF");
       +
       +                for (i = 0; i < sizeof(ent.md); i++)
       +                        printf("%02x", ent.md[i]);
       +                putchar('\n');
       +                lseek(ifd, ent.nblks * sizeof(ent.blks[0]), SEEK_CUR);
       +        }
       +}
       +
       +void
        rebuild_cache(void)
        {
                uint64_t nblks, i;
       @@ -437,64 +495,6 @@ term(void)
        }
        
        void
       -check(void)
       -{
       -        uint64_t nblks, i, j;
       -
       -        nblks = storefile_nblks();
       -        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");
       -                ent = grow_ent(ent, ent->nblks);
       -                if (xread(ifd, ent->blks,
       -                    ent->nblks * sizeof(ent->blks[0])) == 0)
       -                        errx(1, "unexpected EOF");
       -
       -                SHA256_Init(&ctx);
       -                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]);
       -                        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);
       -        }
       -}
       -
       -void
       -list(void)
       -{
       -        uint64_t i;
       -
       -        lseek(ifd, sizeof(enthdr), SEEK_SET);
       -        for (i = 0; i < enthdr.nents; i++) {
       -                struct ent ent;
       -                size_t i;
       -
       -                if (xread(ifd, &ent, sizeof(ent)) == 0)
       -                        errx(1, "unexpected EOF");
       -
       -                for (i = 0; i < sizeof(ent.md); i++)
       -                        printf("%02x", ent.md[i]);
       -                putchar('\n');
       -                lseek(ifd, ent.nblks * sizeof(ent.blks[0]), SEEK_CUR);
       -        }
       -}
       -
       -void
        usage(void)
        {
                fprintf(stderr, "usage: %s [-clv] [-e id] [file]\n", argv0);