dedup

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

commit 6b656b00b35636ec242e9a9af7b8ede7467515a3
parent 58d3303c15c12898c7653fdb1d0a7b902067056e
Author: sin <sin@2f30.org>
Date:   Fri, 12 Apr 2019 13:37:04 +0100

Factor out compressors into separate files

Diffstat:
MMakefile | 6++++++
Acompress-lz4.c | 54++++++++++++++++++++++++++++++++++++++++++++++++++++++
Acompress-none.c | 41+++++++++++++++++++++++++++++++++++++++++
Acompress-snappy.c | 54++++++++++++++++++++++++++++++++++++++++++++++++++++++
Mcompress.c | 149-------------------------------------------------------------------------------
Mdedup.h | 27+++++++++++++++++++++++++++
6 files changed, 182 insertions(+), 149 deletions(-)

diff --git a/Makefile b/Makefile @@ -20,6 +20,9 @@ SRC = \ blake2s-ref.c \ blake2sp-ref.c \ chunker.c \ + compress-lz4.c \ + compress-none.c \ + compress-snappy.c \ compress.c \ hash.c \ icache.c \ @@ -35,6 +38,9 @@ OBJ = \ blake2s-ref.o \ blake2sp-ref.o \ chunker.o \ + compress-lz4.o \ + compress-none.o \ + compress-snappy.o \ compress.o \ hash.o \ icache.o \ diff --git a/compress-lz4.c b/compress-lz4.c @@ -0,0 +1,54 @@ +#include <sys/types.h> + +#include <err.h> +#include <stdint.h> +#include <string.h> + +#include <lz4.h> + +#include "blake2.h" +#include "dedup.h" + +int +lz4_init(struct compr_ctx *ctx) +{ + return 0; +} + +size_t +lz4_size(struct compr_ctx *ctx, size_t n) +{ + return LZ4_compressBound(n); +} + +size_t +lz4_compr(struct compr_ctx *ctx, const void *in, void *out, + size_t insize, size_t outsize) +{ + int n; + + n = LZ4_compress_default((char *)in, (char *)out, insize, + outsize); + if (n < 0) + errx(1, "LZ4_compress_default failed"); + return n; +} + +size_t +lz4_decompr(struct compr_ctx *ctx, const void *in, void *out, + size_t insize, size_t outsize) +{ + int n; + + n = LZ4_decompress_safe((char *)in, (char *)out, insize, + outsize); + if (n < 0) + errx(1, "LZ4_decompress_safe failed"); + return n; +} + +int +lz4_final(struct compr_ctx *ctx) +{ + return 0; +} diff --git a/compress-none.c b/compress-none.c @@ -0,0 +1,41 @@ +#include <sys/types.h> + +#include <stdint.h> +#include <string.h> + +#include "blake2.h" +#include "dedup.h" + +int +none_init(struct compr_ctx *ctx) +{ + return 0; +} + +size_t +none_size(struct compr_ctx *ctx, size_t n) +{ + return n; +} + +size_t +none_compr(struct compr_ctx *ctx, const void *in, void *out, + size_t insize, size_t outsize) +{ + memcpy(out, in, insize); + return insize; +} + +size_t +none_decompr(struct compr_ctx *ctx, const void *in, void *out, + size_t insize, size_t outsize) +{ + memcpy(out, in, insize); + return insize; +} + +int +none_final(struct compr_ctx *ctx) +{ + return 0; +} diff --git a/compress-snappy.c b/compress-snappy.c @@ -0,0 +1,54 @@ +#include <sys/types.h> + +#include <err.h> +#include <stdint.h> +#include <string.h> + +#include <snappy-c.h> + +#include "blake2.h" +#include "dedup.h" + +int +snappy_init(struct compr_ctx *ctx) +{ + return 0; +} + +size_t +snappy_size(struct compr_ctx *ctx, size_t n) +{ + return snappy_max_compressed_length(n); +} + +size_t +snappy_compr(struct compr_ctx *ctx, const void *in, void *out, + size_t insize, size_t outsize) +{ + size_t n = outsize; + snappy_status ret; + + ret = snappy_compress((char *)in, insize, (char *)out, &n); + if (ret != SNAPPY_OK) + errx(1, "snappy_compress failed: %d", ret); + return n; +} + +size_t +snappy_decompr(struct compr_ctx *ctx, const void *in, void *out, + size_t insize, size_t outsize) +{ + size_t n = outsize; + snappy_status ret; + + ret = snappy_uncompress((char *)in, insize, (char *)out, &n); + if (ret != SNAPPY_OK) + errx(1, "snappy_uncompress failed: %d", ret); + return n; +} + +int +snappy_final(struct compr_ctx *ctx) +{ + return 0; +} diff --git a/compress.c b/compress.c @@ -5,36 +5,9 @@ #include <string.h> #include <strings.h> -#include <lz4.h> -#include <snappy-c.h> - #include "blake2.h" #include "dedup.h" -static int none_init(struct compr_ctx *ctx); -static size_t none_size(struct compr_ctx *ctx, size_t n); -static size_t none_compr(struct compr_ctx *ctx, const void *in, void *out, - size_t insize, size_t outsize); -static size_t none_decompr(struct compr_ctx *ctx, const void *in, void *out, - size_t insize, size_t outsize); -static int none_final(struct compr_ctx *ctx); - -static int lz4_init(struct compr_ctx *ctx); -static size_t lz4_size(struct compr_ctx *ctx, size_t n); -static size_t lz4_compr(struct compr_ctx *ctx, const void *in, void *out, - size_t insize, size_t outsize); -static size_t lz4_decompr(struct compr_ctx *ctx, const void *in, void *out, - size_t insize, size_t outsize); -static int lz4_final(struct compr_ctx *ctx); - -static int snappy_init(struct compr_ctx *ctx); -static size_t snappy_size(struct compr_ctx *ctx, size_t n); -static size_t snappy_compr(struct compr_ctx *ctx, const void *in, void *out, - size_t insize, size_t outsize); -static size_t snappy_decompr(struct compr_ctx *ctx, const void *in, void *out, - size_t insize, size_t outsize); -static int snappy_final(struct compr_ctx *ctx); - static struct compr_ops { int (*init)(struct compr_ctx *ctx); size_t (*size)(struct compr_ctx *ctx, size_t n); @@ -77,128 +50,6 @@ static struct algomap { { .name = NULL, }, }; -static int -none_init(struct compr_ctx *ctx) -{ - return 0; -} - -static size_t -none_size(struct compr_ctx *ctx, size_t n) -{ - return n; -} - -static size_t -none_compr(struct compr_ctx *ctx, const void *in, void *out, - size_t insize, size_t outsize) -{ - memcpy(out, in, insize); - return insize; -} - -static size_t -none_decompr(struct compr_ctx *ctx, const void *in, void *out, - size_t insize, size_t outsize) -{ - memcpy(out, in, insize); - return insize; -} - -static int -none_final(struct compr_ctx *ctx) -{ - return 0; -} - -static int -lz4_init(struct compr_ctx *ctx) -{ - return 0; -} - -static size_t -lz4_size(struct compr_ctx *ctx, size_t n) -{ - return LZ4_compressBound(n); -} - -static size_t -lz4_compr(struct compr_ctx *ctx, const void *in, void *out, - size_t insize, size_t outsize) -{ - int n; - - n = LZ4_compress_default((char *)in, (char *)out, insize, - outsize); - if (n < 0) - errx(1, "LZ4_compress_default failed"); - return n; -} - -static size_t -lz4_decompr(struct compr_ctx *ctx, const void *in, void *out, - size_t insize, size_t outsize) -{ - int n; - - n = LZ4_decompress_safe((char *)in, (char *)out, insize, - outsize); - if (n < 0) - errx(1, "LZ4_decompress_safe failed"); - return n; -} - -static int -lz4_final(struct compr_ctx *ctx) -{ - return 0; -} - -static int -snappy_init(struct compr_ctx *ctx) -{ - return 0; -} - -static size_t -snappy_size(struct compr_ctx *ctx, size_t n) -{ - return snappy_max_compressed_length(n); -} - -static size_t -snappy_compr(struct compr_ctx *ctx, const void *in, void *out, - size_t insize, size_t outsize) -{ - size_t n = outsize; - snappy_status ret; - - ret = snappy_compress((char *)in, insize, (char *)out, &n); - if (ret != SNAPPY_OK) - errx(1, "snappy_compress failed: %d", ret); - return n; -} - -static size_t -snappy_decompr(struct compr_ctx *ctx, const void *in, void *out, - size_t insize, size_t outsize) -{ - size_t n = outsize; - snappy_status ret; - - ret = snappy_uncompress((char *)in, insize, (char *)out, &n); - if (ret != SNAPPY_OK) - errx(1, "snappy_uncompress failed: %d", ret); - return n; -} - -static int -snappy_final(struct compr_ctx *ctx) -{ - return 0; -} - int compr_init(struct compr_ctx *ctx, int type) { diff --git a/dedup.h b/dedup.h @@ -108,6 +108,33 @@ ssize_t fill_chunker(struct chunker *chunker); uint8_t *get_chunk(struct chunker *chunker, size_t *chunk_size); void drain_chunker(struct chunker *chunker); +/* compress-none.c */ +int none_init(struct compr_ctx *ctx); +size_t none_size(struct compr_ctx *ctx, size_t n); +size_t none_compr(struct compr_ctx *ctx, const void *in, void *out, + size_t insize, size_t outsize); +size_t none_decompr(struct compr_ctx *ctx, const void *in, void *out, + size_t insize, size_t outsize); +int none_final(struct compr_ctx *ctx); + +/* compress-lz4.c */ +int lz4_init(struct compr_ctx *ctx); +size_t lz4_size(struct compr_ctx *ctx, size_t n); +size_t lz4_compr(struct compr_ctx *ctx, const void *in, void *out, + size_t insize, size_t outsize); +size_t lz4_decompr(struct compr_ctx *ctx, const void *in, void *out, + size_t insize, size_t outsize); +int lz4_final(struct compr_ctx *ctx); + +/* compress-snappy.c */ +int snappy_init(struct compr_ctx *ctx); +size_t snappy_size(struct compr_ctx *ctx, size_t n); +size_t snappy_compr(struct compr_ctx *ctx, const void *in, void *out, + size_t insize, size_t outsize); +size_t snappy_decompr(struct compr_ctx *ctx, const void *in, void *out, + size_t insize, size_t outsize); +int snappy_final(struct compr_ctx *ctx); + /* compress.c */ int compr_init(struct compr_ctx *ctx, int type); int compr_size(struct compr_ctx *ctx, size_t n);