Use seterr() when reading/write the state file - 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 8faa8deed4fb6d7d6f5c90ad39715aac27254444
 (DIR) parent a8beae9784fa77d57a05f35c0980d7504aea2fde
 (HTM) Author: sin <sin@2f30.org>
       Date:   Sun, 12 May 2019 11:57:16 +0100
       
       Use seterr() when reading/write the state file
       
       Diffstat:
         M dup-check.c                         |       2 +-
         M dup-gc.c                            |       2 +-
         M dup-init.c                          |       2 +-
         M dup-pack.c                          |       2 +-
         M dup-rm.c                            |       2 +-
         M dup-unpack.c                        |       2 +-
         M state.c                             |      33 +++++++++++++++++++++----------
       
       7 files changed, 29 insertions(+), 16 deletions(-)
       ---
 (DIR) diff --git a/dup-check.c b/dup-check.c
       @@ -34,7 +34,7 @@ loadstate(char *repo)
                if (fd < 0)
                        err(1, "open: %s", path);
                if (readstate(fd, &param) < 0)
       -                errx(1, "readstate: failed");
       +                printerr("readstate: %s", path);
                if (close(fd) < 0)
                        err(1, "close: %s", path);
        }
 (DIR) diff --git a/dup-gc.c b/dup-gc.c
       @@ -32,7 +32,7 @@ loadstate(char *repo)
                if (fd < 0)
                        err(1, "open: %s", path);
                if (readstate(fd, &param) < 0)
       -                errx(1, "readstate: failed");
       +                printerr("readstate: %s", path);
                if (close(fd) < 0)
                        err(1, "close: %s", path);
        }
 (DIR) diff --git a/dup-init.c b/dup-init.c
       @@ -33,7 +33,7 @@ savestate(char *repo)
                if (fd < 0)
                        err(1, "open: %s", path);
                if (writestate(fd, &param) < 0)
       -                errx(1, "writestate: failed");
       +                printerr("writestate: %s", path);
                if (close(fd) < 0)
                        err(1, "close: %s", path);
        }
 (DIR) diff --git a/dup-pack.c b/dup-pack.c
       @@ -33,7 +33,7 @@ loadstate(char *repo)
                if (fd < 0)
                        err(1, "open: %s", path);
                if (readstate(fd, &param) < 0)
       -                errx(1, "readstate: failed");
       +                printerr("readstate: %s", path);
                if (close(fd) < 0)
                        err(1, "close: %s", path);
        }
 (DIR) diff --git a/dup-rm.c b/dup-rm.c
       @@ -32,7 +32,7 @@ loadstate(char *repo)
                if (fd < 0)
                        err(1, "open: %s", path);
                if (readstate(fd, &param) < 0)
       -                errx(1, "readstate: failed");
       +                printerr("readstate: %s", path);
                if (close(fd) < 0)
                        err(1, "close: %s", path);
        }
 (DIR) diff --git a/dup-unpack.c b/dup-unpack.c
       @@ -33,7 +33,7 @@ loadstate(char *repo)
                if (fd < 0)
                        err(1, "open: %s", path);
                if (readstate(fd, &param) < 0)
       -                errx(1, "readstate: failed");
       +                printerr("readstate: %s", path);
                if (close(fd) < 0)
                        err(1, "close: %s", path);
        }
 (DIR) diff --git a/state.c b/state.c
       @@ -1,4 +1,5 @@
        #include <assert.h>
       +#include <errno.h>
        #include <stdint.h>
        #include <stdio.h>
        #include <string.h>
       @@ -42,8 +43,10 @@ unpackshdr(int fd, struct shdr *shdr)
                unsigned char buf[SHDRSIZE];
                int n;
        
       -        if (xread(fd, buf, sizeof(buf)) != sizeof(buf))
       +        if (xread(fd, buf, sizeof(buf)) != sizeof(buf)) {
       +                seterr("failed to read state header: %s", strerror(errno));
                        return -1;
       +        }
        
                n = unpack(buf, "q", &shdr->flags);
                assert(n == sizeof(buf));
       @@ -59,8 +62,10 @@ packshdr(int fd, struct shdr *shdr)
        
                n = pack(buf, "q", shdr->flags);
                assert(n == SHDRSIZE);
       -        if (xwrite(fd, buf, n) != n)
       +        if (xwrite(fd, buf, n) != n) {
       +                seterr("failed to write state header: %s", strerror(errno));
                        return -1;
       +        }
                return n;
        }
        
       @@ -73,22 +78,26 @@ writestate(int fd, struct param *par)
                shdr.flags = (VMAJ << VMAJSHIFT) | VMIN;
        
                /* Set compression type */
       -        if (strcasecmp(par->calgo, "none") == 0)
       +        if (strcasecmp(par->calgo, "none") == 0) {
                        shdr.flags |= CNONETYPE << CALGOSHIFT;
       -        else if (strcasecmp(par->calgo, "snappy") == 0)
       +        } else if (strcasecmp(par->calgo, "snappy") == 0) {
                        shdr.flags |= CSNAPPYTYPE << CALGOSHIFT;
       -        else if (strcasecmp(par->calgo, "lz4") == 0)
       +        } else if (strcasecmp(par->calgo, "lz4") == 0) {
                        shdr.flags |= CLZ4TYPE << CALGOSHIFT;
       -        else
       +        } else {
       +                seterr("invalid compression type: %s", par->calgo);
                        return -1;
       +        }
        
                /* Set encryption type */
       -        if (strcasecmp(par->ealgo, "none") == 0)
       +        if (strcasecmp(par->ealgo, "none") == 0) {
                        shdr.flags |= ENONETYPE << EALGOSHIFT;
       -        else if (strcasecmp(par->ealgo, "XChaCha20-Poly1305") == 0)
       +        } else if (strcasecmp(par->ealgo, "XChaCha20-Poly1305") == 0) {
                        shdr.flags |= ECHACHATYPE << EALGOSHIFT;
       -        else
       +        } else {
       +                seterr("invalid encryption type: %s", par->ealgo);
                        return -1;
       +        }
        
                if (packshdr(fd, &shdr) < 0)
                        return -1;
       @@ -105,8 +114,10 @@ readstate(int fd, struct param *par)
                        return -1;
        
                /* If the major version is different, the format is incompatible */
       -        if (((shdr.flags >> VMAJSHIFT) & VMAJMASK) != VMAJ)
       +        if (((shdr.flags >> VMAJSHIFT) & VMAJMASK) != VMAJ) {
       +                seterr("state header version mismatch");
                        return -1;
       +        }
        
                /* Populate param compression algo */
                algo = (shdr.flags >> CALGOSHIFT) & CALGOMASK;
       @@ -121,6 +132,7 @@ readstate(int fd, struct param *par)
                        par->calgo = "lz4";
                        break;
                default:
       +                seterr("invalid compression type: %d", algo);
                        return -1;
                }
        
       @@ -134,6 +146,7 @@ readstate(int fd, struct param *par)
                        par->ealgo = "XChaCha20-Poly1305";
                        break;
                default:
       +                seterr("invalid encryption type: %d", algo);
                        return -1;
                }