URI: 
       Separate pack/unpack from write/read - dedup - deduplicating backup program
  HTML git clone git://bitreich.org/dedup/ git://enlrupgkhuxnvlhsf6lc3fziv5h2hhfrinws65d7roiv6bfj7d652fid.onion/dedup/
   DIR Log
   DIR Files
   DIR Refs
   DIR Tags
   DIR README
   DIR LICENSE
       ---
   DIR commit c14172d9472f2d53300a19b3fa27ddcbaba185d6
   DIR parent 7c3a4a21592d3c11e418e00964caadaec81613a5
  HTML Author: sin <sin@2f30.org>
       Date:   Sun, 12 May 2019 19:36:07 +0100
       
       Separate pack/unpack from write/read
       
       Diffstat:
         M bstorage.c                          |      36 ++++++++++++++++----------------
       
       1 file changed, 18 insertions(+), 18 deletions(-)
       ---
   DIR diff --git a/bstorage.c b/bstorage.c
       @@ -122,18 +122,12 @@ bhash(void *buf, size_t n, unsigned char *md)
                return crypto_generichash(md, MDSIZE, buf, n, NULL, 0);
        }
        
       -/* Read block header */
       +/* Unpack block header */
        static int
       -unpackbhdr(int fd, struct bhdr *bhdr)
       +unpackbhdr(unsigned char *buf, struct bhdr *bhdr)
        {
       -        unsigned char buf[BHDRSIZE];
                int n;
        
       -        if (xread(fd, buf, BHDRSIZE) != BHDRSIZE) {
       -                seterr("failed to read block header: %s", strerror(errno));
       -                return -1;
       -        }
       -
                n = unpack(buf, "'16qq",
                           bhdr->magic,
                           &bhdr->flags,
       @@ -145,9 +139,8 @@ unpackbhdr(int fd, struct bhdr *bhdr)
        
        /* Write block header */
        static int
       -packbhdr(int fd, struct bhdr *bhdr)
       +packbhdr(unsigned char *buf, struct bhdr *bhdr)
        {
       -        unsigned char buf[BHDRSIZE];
                int n;
        
                n = pack(buf, "'16qq",
       @@ -156,14 +149,10 @@ packbhdr(int fd, struct bhdr *bhdr)
                         bhdr->nbd);
        
                assert(n == BHDRSIZE);
       -        if (xwrite(fd, buf, n) != n) {
       -                seterr("failed to write block header: %s", strerror(errno));
       -                return -1;
       -        }
                return n;
        }
        
       -/* Read block descriptor */
       +/* Unpack block descriptor */
        static int
        unpackbd(int fd, struct bd *bd)
        {
       @@ -298,6 +287,7 @@ initbdcache(struct sctx *sctx)
        static int
        bscreat(struct bctx *bctx, char *path, int mode)
        {
       +        unsigned char buf[BHDRSIZE];
                struct sctx *sctx;
                struct bhdr *bhdr;
                int fd;
       @@ -330,9 +320,11 @@ bscreat(struct bctx *bctx, char *path, int mode)
                bhdr->flags = (VMAJ << VMAJSHIFT) | VMIN;
                bhdr->nbd = 0;
        
       -        if (packbhdr(fd, bhdr) < 0) {
       +        packbhdr(buf, bhdr);
       +        if (xwrite(fd, buf, BHDRSIZE) != BHDRSIZE) {
                        free(sctx);
                        close(fd);
       +                seterr("failed to write block header: %s", strerror(errno));
                        return -1;
                }
                return 0;
       @@ -342,6 +334,7 @@ bscreat(struct bctx *bctx, char *path, int mode)
        static int
        bsopen(struct bctx *bctx, char *path, int flags, int mode)
        {
       +        unsigned char buf[BHDRSIZE];
                struct sctx *sctx;
                struct bhdr *bhdr;
                int fd, algo;
       @@ -381,11 +374,13 @@ bsopen(struct bctx *bctx, char *path, int flags, int mode)
                SLIST_INIT(&sctx->gchead);
                bhdr = &sctx->bhdr;
        
       -        if (unpackbhdr(fd, bhdr) < 0) {
       +        if (xread(fd, buf, BHDRSIZE) != BHDRSIZE) {
                        free(sctx);
                        close(fd);
       +                seterr("failed to read block header: %s", strerror(errno));
                        return -1;
                }
       +        unpackbhdr(buf, bhdr);
        
                if (memcmp(bhdr->magic, BHDRMAGIC, NBHDRMAGIC) != 0) {
                        free(sctx);
       @@ -662,6 +657,7 @@ bscheck(struct bctx *bctx, unsigned char *md)
        static int
        bssync(struct bctx *bctx)
        {
       +        unsigned char buf[BHDRSIZE];
                struct sctx *sctx;
                struct bhdr *bhdr;
        
       @@ -673,9 +669,13 @@ bssync(struct bctx *bctx)
                        seterr("lseek: %s", strerror(errno));
                        return -1;
                }
       +
                bhdr = &sctx->bhdr;
       -        if (packbhdr(sctx->fd, bhdr) < 0)
       +        packbhdr(buf, bhdr);
       +        if (xwrite(sctx->fd, buf, BHDRSIZE) != BHDRSIZE) {
       +                seterr("failed to write block header: %s", strerror(errno));
                        return -1;
       +        }
                fsync(sctx->fd);
                return 0;
        }