dedup

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

commit faf2fab2ac2febc631159913683d1c3dbc5166b3
parent 01c8fce481e7a253ac9a96c4bbc877aab2945586
Author: sin <sin@2f30.org>
Date:   Sun, 12 May 2019 16:36:27 +0100

Rework additional data handling

Diffstat:
Msnap.c | 75++++++++++++++++++++++++++++++++++++---------------------------------------
1 file changed, 36 insertions(+), 39 deletions(-)

diff --git a/snap.c b/snap.c @@ -61,18 +61,12 @@ struct sctx { struct shdr shdr; /* snapshot header */ }; -/* Read snapshot header */ +/* Unpack snapshot header */ static int -unpackshdr(int fd, struct shdr *shdr) +unpackshdr(unsigned char *buf, struct shdr *shdr) { - unsigned char buf[SHDRSIZE]; int n; - if (xread(fd, buf, sizeof(buf)) != sizeof(buf)) { - seterr("failed to read snapshot header: %s", strerror(errno)); - return -1; - } - n = unpack(buf, "'16'24qq", shdr->magic, shdr->header, @@ -83,11 +77,10 @@ unpackshdr(int fd, struct shdr *shdr) return n; } -/* Write snapshot header */ +/* Pack snapshot header */ static int -packshdr(int fd, struct shdr *shdr) +packshdr(unsigned char *buf, struct shdr *shdr) { - unsigned char buf[SHDRSIZE]; int n; n = pack(buf, "'16'24qq", @@ -97,36 +90,25 @@ packshdr(int fd, struct shdr *shdr) shdr->nbd); assert(n == SHDRSIZE); - if (xwrite(fd, buf, n) != n) { - seterr("failed to write snapshot header: %s", strerror(errno)); - return -1; - } return n; } static int initmdhead(struct sctx *sctx) { - unsigned char ad[SHDRSIZE]; struct shdr *shdr; uint64_t i; - if (lseek(sctx->fd, 0, SEEK_SET) < 0) { - seterr("lseek: %s", strerror(errno)); - return -1; - } - - if (xread(sctx->fd, ad, sizeof(ad)) != sizeof(ad)) { - seterr("failed to read snapshot header: %s\n", strerror(errno)); - return -1; - } - shdr = &sctx->shdr; if (sctx->crypto) { + unsigned char ad[SHDRSIZE]; crypto_secretstream_xchacha20poly1305_state state; struct shdr *shdr; shdr = &sctx->shdr; + if (packshdr(ad, shdr) < 0) + return -1; + if (crypto_secretstream_xchacha20poly1305_init_pull(&state, shdr->header, param.key) != 0) { @@ -196,6 +178,7 @@ err0: int screat(char *path, int mode, struct sctx **sctx) { + unsigned char buf[SHDRSIZE]; struct shdr *shdr; int crypto; int fd; @@ -249,18 +232,24 @@ screat(char *path, int mode, struct sctx **sctx) shdr->flags = (VMAJ << VMAJSHIFT) | VMIN; shdr->nbd = 0; - if (packshdr(fd, shdr) < 0) { + if (packshdr(buf, shdr) < 0) { free(*sctx); close(fd); return -1; } + if (xwrite(fd, buf, SHDRSIZE) != SHDRSIZE) { + seterr("failed to write snapshot header: %s", strerror(errno)); + return -1; + } + return 0; } int sopen(char *path, int flags, int mode, struct sctx **sctx) { + unsigned char buf[SHDRSIZE]; struct shdr *shdr; int crypto; int fd; @@ -312,7 +301,12 @@ sopen(char *path, int flags, int mode, struct sctx **sctx) shdr = &(*sctx)->shdr; - if (unpackshdr(fd, shdr) < 0) { + if (xread(fd, buf, SHDRSIZE) != SHDRSIZE) { + seterr("failed to read snapshot header: %s", strerror(errno)); + return -1; + } + + if (unpackshdr(buf, shdr) < 0) { free(sctx); close(fd); return -1; @@ -419,23 +413,18 @@ ssync(struct sctx *sctx) shdr = &sctx->shdr; if (sctx->crypto) { - unsigned char ad[SHDRSIZE]; + unsigned char hdr[SHDRSIZE]; crypto_secretstream_xchacha20poly1305_state state; crypto_secretstream_xchacha20poly1305_init_push(&state, shdr->header, param.key); - if (packshdr(sctx->fd, shdr) < 0) + if (packshdr(hdr, shdr) < 0) return -1; - if (lseek(sctx->fd, 0, SEEK_SET) < 0) { - seterr("lseek: %s", strerror(errno)); - return -1; - } - - if (xread(sctx->fd, ad, sizeof(ad)) != sizeof(ad)) { - seterr("failed to read snapshot header: %s\n", strerror(errno)); + if (xwrite(sctx->fd, hdr, SHDRSIZE) != SHDRSIZE) { + seterr("failed to write snapshot header: %s", strerror(errno)); return -1; } @@ -451,7 +440,7 @@ ssync(struct sctx *sctx) crypto_secretstream_xchacha20poly1305_push(&state, buf, NULL, mdnode->md, MDSIZE, - ad, sizeof(ad), tag); + hdr, SHDRSIZE, tag); if (xwrite(sctx->fd, buf, sizeof(buf)) != sizeof(buf)) { seterr("failed to write block hash: %s", strerror(errno)); @@ -459,8 +448,16 @@ ssync(struct sctx *sctx) } } } else { - if (packshdr(sctx->fd, shdr) < 0) + unsigned char hdr[SHDRSIZE]; + + if (packshdr(hdr, shdr) < 0) return -1; + + if (xwrite(sctx->fd, hdr, SHDRSIZE) != SHDRSIZE) { + seterr("failed to write snapshot header: %s", strerror(errno)); + return -1; + } + TAILQ_FOREACH(mdnode, &sctx->mdhead, e) { if (xwrite(sctx->fd, mdnode->md, MDSIZE) != MDSIZE) { seterr("failed to write block hash: %s",