dedup

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

commit 115fc777eeeef685fee0f6ef3b7d2c182b03c1ed
parent 222099ba0a96a08af154641dbab342243afd1807
Author: sin <sin@2f30.org>
Date:   Thu,  2 May 2019 14:26:53 +0100

Rework bcput/bcget

Diffstat:
Mbcompress.c | 54++++++++++++++++++++++++++++++++++--------------------
1 file changed, 34 insertions(+), 20 deletions(-)

diff --git a/bcompress.c b/bcompress.c @@ -151,26 +151,31 @@ bcput(struct bctx *bctx, void *buf, size_t n, unsigned char *md) char *cbuf; size_t cn; + /* Calculate compressed block size */ cctx = bctx->cctx; - switch (cctx->type) { - case CDNONETYPE: + if (cctx->type == CDNONETYPE) { cn = n; - cbuf = malloc(CDSIZE + cn); - if (cbuf == NULL) - return -1; - memcpy(&cbuf[CDSIZE], buf, cn); - break; - case CDSNAPPYTYPE: + } else if (cctx->type == CDSNAPPYTYPE) { cn = snappy_max_compressed_length(n); - cbuf = malloc(CDSIZE + cn); - if (cbuf == NULL) - return -1; + } else { + return -1; + } + + /* Allocate compressed block */ + cbuf = malloc(CDSIZE + cn); + if (cbuf == NULL) + return -1; + + /* Compress block */ + if (cctx->type == CDNONETYPE) { + memcpy(&cbuf[CDSIZE], buf, cn); + } else if (cctx->type == CDSNAPPYTYPE) { if (snappy_compress(buf, n, &cbuf[CDSIZE], &cn) != SNAPPY_OK) { free(cbuf); return -1; } - break; - default: + } else { + free(cbuf); return -1; } @@ -197,34 +202,44 @@ bcget(struct bctx *bctx, unsigned char *md, void *buf, size_t *n) char *cbuf; size_t cn, un, size; + /* Calculate maximum compressed block size */ size = *n; cn = snappy_max_compressed_length(size); if (cn > size) size = cn; size += CDSIZE; + + /* Allocate compressed block */ cbuf = malloc(size); if (cbuf == NULL) return -1; + /* Read compressed block */ bops = bstorageops(); if (bops->get(bctx, md, cbuf, &size) < 0) { free(cbuf); return -1; } + /* Unpack compression descriptor */ unpackcd(cbuf, &cd); - switch (cd.type) { - case CDNONETYPE: + + /* Decompress block */ + if (cd.type == CDNONETYPE) { un = cd.size; if (*n < un) { free(cbuf); return -1; } memcpy(buf, &cbuf[CDSIZE], un); - break; - case CDSNAPPYTYPE: + } else if (cd.type == CDSNAPPYTYPE) { if (snappy_uncompressed_length(&cbuf[CDSIZE], cd.size, - &un) != SNAPPY_OK || *n < un) { + &un) != SNAPPY_OK) { + free(cbuf); + return -1; + } + + if (*n < un) { free(cbuf); return -1; } @@ -234,8 +249,7 @@ bcget(struct bctx *bctx, unsigned char *md, void *buf, size_t *n) free(cbuf); return -1; } - break; - default: + } else { free(cbuf); return -1; }