dedup

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

commit a3b3fb6126e50d105436b50fc3c077d4e33f3959
parent 6cb387cea51dc694f225ec04bdf1c5d02caf9dcb
Author: sin <sin@2f30.org>
Date:   Thu, 18 Apr 2019 00:46:56 +0100

Factor out some more helpers

Diffstat:
Mdcheck.c | 61+++++--------------------------------------------------------
Mdedup.h | 3+++
Mdinfo.c | 61+++++--------------------------------------------------------
Mdinit.c | 4++--
Mdlist.c | 61+++++--------------------------------------------------------
Mdpack.c | 88++++++++++++-------------------------------------------------------------------
Mdunpack.c | 61+++++--------------------------------------------------------
Mutils.c | 51+++++++++++++++++++++++++++++++++++++++++++++++++++
8 files changed, 89 insertions(+), 301 deletions(-)

diff --git a/dcheck.c b/dcheck.c @@ -193,59 +193,6 @@ walk_snap(int (*fn)(struct snap *, void *), void *arg) } static void -match_ver(uint64_t v) -{ - uint8_t maj, min; - - min = v & VER_MIN_MASK; - maj = (v >> VER_MAJ_SHIFT) & VER_MAJ_MASK; - if (maj == VER_MAJ && min == VER_MIN) - return; - errx(1, "format version mismatch: expected %u.%u but got %u.%u", - VER_MAJ, VER_MIN, maj, min); -} - -static void -load_blk_hdr(void) -{ - uint64_t v; - - xlseek(sfd, 0, SEEK_SET); - read_blk_hdr(sfd, &blk_hdr); - match_ver(blk_hdr.flags); - - v = blk_hdr.flags >> COMPR_ALGO_SHIFT; - v &= COMPR_ALGO_MASK; - compr_algo = v; - - if (compr_algo < 0 || compr_algo >= NR_COMPRS) - errx(1, "unsupported compression algorithm: %d", compr_algo); - - if (verbose > 0) - fprintf(stderr, "Compression algorithm: %s\n", - compr_type2name(compr_algo)); - - v = blk_hdr.flags >> HASH_ALGO_SHIFT; - v &= HASH_ALGO_MASK; - hash_algo = v; - - if (hash_algo < 0 || hash_algo >= NR_HASHES) - errx(1, "unsupported hash algorithm: %d", hash_algo); - - if (verbose > 0) - fprintf(stderr, "Hash algorithm: %s\n", - hash_type2name(hash_algo)); -} - -static void -load_snap_hdr(void) -{ - xlseek(ifd, 0, SEEK_SET); - read_snap_hdr(ifd, &snap_hdr); - match_ver(snap_hdr.flags); -} - -static void init(void) { ifd = open(SNAPSF, O_RDONLY, 0600); @@ -260,15 +207,17 @@ init(void) flock(sfd, LOCK_NB | LOCK_EX) < 0) err(1, "flock"); - load_snap_hdr(); - load_blk_hdr(); + xlseek(ifd, 0, SEEK_SET); + load_snap_hdr(ifd, &snap_hdr); + xlseek(sfd, 0, SEEK_SET); + load_blk_hdr(sfd, &blk_hdr, &compr_algo, &hash_algo); } static void term(void) { - close(sfd); close(ifd); + close(sfd); } static void diff --git a/dedup.h b/dedup.h @@ -208,3 +208,6 @@ ssize_t xread(int fd, void *buf, size_t nbytes); ssize_t xwrite(int fd, const void *buf, size_t nbytes); void init_blk_hdr(struct blk_hdr *hdr, int compr_algo, int hash_algo); void init_snap_hdr(struct snap_hdr *hdr); +void match_ver(uint64_t v); +void load_blk_hdr(int fd, struct blk_hdr *hdr, int *compr_algo, int *hash_algo); +void load_snap_hdr(int fd, struct snap_hdr *hdr); diff --git a/dinfo.c b/dinfo.c @@ -50,59 +50,6 @@ print_info(struct stats *st) } static void -match_ver(uint64_t v) -{ - uint8_t maj, min; - - min = v & VER_MIN_MASK; - maj = (v >> VER_MAJ_SHIFT) & VER_MAJ_MASK; - if (maj == VER_MAJ && min == VER_MIN) - return; - errx(1, "format version mismatch: expected %u.%u but got %u.%u", - VER_MAJ, VER_MIN, maj, min); -} - -static void -load_blk_hdr(void) -{ - uint64_t v; - - xlseek(sfd, 0, SEEK_SET); - read_blk_hdr(sfd, &blk_hdr); - match_ver(blk_hdr.flags); - - v = blk_hdr.flags >> COMPR_ALGO_SHIFT; - v &= COMPR_ALGO_MASK; - compr_algo = v; - - if (compr_algo < 0 || compr_algo >= NR_COMPRS) - errx(1, "unsupported compression algorithm: %d", compr_algo); - - if (verbose > 0) - fprintf(stderr, "Compression algorithm: %s\n", - compr_type2name(compr_algo)); - - v = blk_hdr.flags >> HASH_ALGO_SHIFT; - v &= HASH_ALGO_MASK; - hash_algo = v; - - if (hash_algo < 0 || hash_algo >= NR_HASHES) - errx(1, "unsupported hash algorithm: %d", hash_algo); - - if (verbose > 0) - fprintf(stderr, "Hash algorithm: %s\n", - hash_type2name(hash_algo)); -} - -static void -load_snap_hdr(void) -{ - xlseek(ifd, 0, SEEK_SET); - read_snap_hdr(ifd, &snap_hdr); - match_ver(snap_hdr.flags); -} - -static void init(void) { ifd = open(SNAPSF, O_RDONLY, 0600); @@ -117,15 +64,17 @@ init(void) flock(sfd, LOCK_NB | LOCK_EX) < 0) err(1, "flock"); - load_snap_hdr(); - load_blk_hdr(); + xlseek(ifd, 0, SEEK_SET); + load_snap_hdr(ifd, &snap_hdr); + xlseek(sfd, 0, SEEK_SET); + load_blk_hdr(sfd, &blk_hdr, &compr_algo, &hash_algo); } static void term(void) { - close(sfd); close(ifd); + close(sfd); } static void diff --git a/dinit.c b/dinit.c @@ -57,11 +57,11 @@ term(void) xlseek(sfd, 0, SEEK_SET); write_blk_hdr(sfd, &blk_hdr); - fsync(sfd); fsync(ifd); + fsync(sfd); - close(sfd); close(ifd); + close(sfd); } static void diff --git a/dlist.c b/dlist.c @@ -112,59 +112,6 @@ walk_snap(int (*fn)(struct snap *, void *), void *arg) } static void -match_ver(uint64_t v) -{ - uint8_t maj, min; - - min = v & VER_MIN_MASK; - maj = (v >> VER_MAJ_SHIFT) & VER_MAJ_MASK; - if (maj == VER_MAJ && min == VER_MIN) - return; - errx(1, "format version mismatch: expected %u.%u but got %u.%u", - VER_MAJ, VER_MIN, maj, min); -} - -static void -load_blk_hdr(void) -{ - uint64_t v; - - xlseek(sfd, 0, SEEK_SET); - read_blk_hdr(sfd, &blk_hdr); - match_ver(blk_hdr.flags); - - v = blk_hdr.flags >> COMPR_ALGO_SHIFT; - v &= COMPR_ALGO_MASK; - compr_algo = v; - - if (compr_algo < 0 || compr_algo >= NR_COMPRS) - errx(1, "unsupported compression algorithm: %d", compr_algo); - - if (verbose > 0) - fprintf(stderr, "Compression algorithm: %s\n", - compr_type2name(compr_algo)); - - v = blk_hdr.flags >> HASH_ALGO_SHIFT; - v &= HASH_ALGO_MASK; - hash_algo = v; - - if (hash_algo < 0 || hash_algo >= NR_HASHES) - errx(1, "unsupported hash algorithm: %d", hash_algo); - - if (verbose > 0) - fprintf(stderr, "Hash algorithm: %s\n", - hash_type2name(hash_algo)); -} - -static void -load_snap_hdr(void) -{ - xlseek(ifd, 0, SEEK_SET); - read_snap_hdr(ifd, &snap_hdr); - match_ver(snap_hdr.flags); -} - -static void init(void) { ifd = open(SNAPSF, O_RDONLY, 0600); @@ -179,15 +126,17 @@ init(void) flock(sfd, LOCK_NB | LOCK_EX) < 0) err(1, "flock"); - load_snap_hdr(); - load_blk_hdr(); + xlseek(ifd, 0, SEEK_SET); + load_snap_hdr(ifd, &snap_hdr); + xlseek(sfd, 0, SEEK_SET); + load_blk_hdr(sfd, &blk_hdr, &compr_algo, &hash_algo); } static void term(void) { - close(sfd); close(ifd); + close(sfd); } static void diff --git a/dpack.c b/dpack.c @@ -275,73 +275,6 @@ walk_snap(int (*fn)(struct snap *, void *), void *arg) } static void -match_ver(uint64_t v) -{ - uint8_t maj, min; - - min = v & VER_MIN_MASK; - maj = (v >> VER_MAJ_SHIFT) & VER_MAJ_MASK; - if (maj == VER_MAJ && min == VER_MIN) - return; - errx(1, "format version mismatch: expected %u.%u but got %u.%u", - VER_MAJ, VER_MIN, maj, min); -} - -static void -load_blk_hdr(void) -{ - uint64_t v; - - xlseek(sfd, 0, SEEK_SET); - read_blk_hdr(sfd, &blk_hdr); - match_ver(blk_hdr.flags); - - v = blk_hdr.flags >> COMPR_ALGO_SHIFT; - v &= COMPR_ALGO_MASK; - compr_algo = v; - - if (compr_algo < 0 || compr_algo >= NR_COMPRS) - errx(1, "unsupported compression algorithm: %d", compr_algo); - - if (verbose > 0) - fprintf(stderr, "Compression algorithm: %s\n", - compr_type2name(compr_algo)); - - v = blk_hdr.flags >> HASH_ALGO_SHIFT; - v &= HASH_ALGO_MASK; - hash_algo = v; - - if (hash_algo < 0 || hash_algo >= NR_HASHES) - errx(1, "unsupported hash algorithm: %d", hash_algo); - - if (verbose > 0) - fprintf(stderr, "Hash algorithm: %s\n", - hash_type2name(hash_algo)); -} - -static void -save_blk_hdr(void) -{ - xlseek(sfd, 0, SEEK_SET); - write_blk_hdr(sfd, &blk_hdr); -} - -static void -load_snap_hdr(void) -{ - xlseek(ifd, 0, SEEK_SET); - read_snap_hdr(ifd, &snap_hdr); - match_ver(snap_hdr.flags); -} - -static void -save_snap_hdr(void) -{ - xlseek(ifd, 0, SEEK_SET); - write_snap_hdr(ifd, &snap_hdr); -} - -static void init(void) { ifd = open(SNAPSF, O_RDWR, 0600); @@ -356,8 +289,11 @@ init(void) flock(sfd, LOCK_NB | LOCK_EX) < 0) err(1, "flock"); - load_snap_hdr(); - load_blk_hdr(); + + xlseek(ifd, 0, SEEK_SET); + load_snap_hdr(ifd, &snap_hdr); + xlseek(sfd, 0, SEEK_SET); + load_blk_hdr(sfd, &blk_hdr, &compr_algo, &hash_algo); icache = alloc_icache(); walk_snap(build_icache, NULL); @@ -366,16 +302,18 @@ init(void) static void term(void) { - free_icache(icache); - - save_blk_hdr(); - save_snap_hdr(); + xlseek(ifd, 0, SEEK_SET); + write_snap_hdr(ifd, &snap_hdr); + xlseek(sfd, 0, SEEK_SET); + write_blk_hdr(sfd, &blk_hdr); - fsync(sfd); fsync(ifd); + fsync(sfd); - close(sfd); close(ifd); + close(sfd); + + free_icache(icache); } static void diff --git a/dunpack.c b/dunpack.c @@ -159,59 +159,6 @@ walk_snap(int (*fn)(struct snap *, void *), void *arg) } static void -match_ver(uint64_t v) -{ - uint8_t maj, min; - - min = v & VER_MIN_MASK; - maj = (v >> VER_MAJ_SHIFT) & VER_MAJ_MASK; - if (maj == VER_MAJ && min == VER_MIN) - return; - errx(1, "format version mismatch: expected %u.%u but got %u.%u", - VER_MAJ, VER_MIN, maj, min); -} - -static void -load_blk_hdr(void) -{ - uint64_t v; - - xlseek(sfd, 0, SEEK_SET); - read_blk_hdr(sfd, &blk_hdr); - match_ver(blk_hdr.flags); - - v = blk_hdr.flags >> COMPR_ALGO_SHIFT; - v &= COMPR_ALGO_MASK; - compr_algo = v; - - if (compr_algo < 0 || compr_algo >= NR_COMPRS) - errx(1, "unsupported compression algorithm: %d", compr_algo); - - if (verbose > 0) - fprintf(stderr, "Compression algorithm: %s\n", - compr_type2name(compr_algo)); - - v = blk_hdr.flags >> HASH_ALGO_SHIFT; - v &= HASH_ALGO_MASK; - hash_algo = v; - - if (hash_algo < 0 || hash_algo >= NR_HASHES) - errx(1, "unsupported hash algorithm: %d", hash_algo); - - if (verbose > 0) - fprintf(stderr, "Hash algorithm: %s\n", - hash_type2name(hash_algo)); -} - -static void -load_snap_hdr(void) -{ - xlseek(ifd, 0, SEEK_SET); - read_snap_hdr(ifd, &snap_hdr); - match_ver(snap_hdr.flags); -} - -static void init(void) { ifd = open(SNAPSF, O_RDONLY, 0600); @@ -226,15 +173,17 @@ init(void) flock(sfd, LOCK_NB | LOCK_EX) < 0) err(1, "flock"); - load_snap_hdr(); - load_blk_hdr(); + xlseek(ifd, 0, SEEK_SET); + load_snap_hdr(ifd, &snap_hdr); + xlseek(sfd, 0, SEEK_SET); + load_blk_hdr(sfd, &blk_hdr, &compr_algo, &hash_algo); } static void term(void) { - close(sfd); close(ifd); + close(sfd); } static void diff --git a/utils.c b/utils.c @@ -85,3 +85,54 @@ init_snap_hdr(struct snap_hdr *hdr) hdr->size = SNAP_HDR_SIZE; hdr->st.min_blk_size = UINT64_MAX; } + +void +match_ver(uint64_t v) +{ + uint8_t maj, min; + + min = v & VER_MIN_MASK; + maj = (v >> VER_MAJ_SHIFT) & VER_MAJ_MASK; + if (maj == VER_MAJ && min == VER_MIN) + return; + errx(1, "format version mismatch: expected %u.%u but got %u.%u", + VER_MAJ, VER_MIN, maj, min); +} + +void +load_blk_hdr(int fd, struct blk_hdr *hdr, int *compr_algo, int *hash_algo) +{ + uint64_t v; + + read_blk_hdr(fd, hdr); + match_ver(hdr->flags); + + v = hdr->flags >> COMPR_ALGO_SHIFT; + v &= COMPR_ALGO_MASK; + *compr_algo = v; + + if (*compr_algo < 0 || *compr_algo >= NR_COMPRS) + errx(1, "unsupported compression algorithm: %d", *compr_algo); + + if (verbose > 0) + fprintf(stderr, "Compression algorithm: %s\n", + compr_type2name(*compr_algo)); + + v = hdr->flags >> HASH_ALGO_SHIFT; + v &= HASH_ALGO_MASK; + *hash_algo = v; + + if (*hash_algo < 0 || *hash_algo >= NR_HASHES) + errx(1, "unsupported hash algorithm: %d", *hash_algo); + + if (verbose > 0) + fprintf(stderr, "Hash algorithm: %s\n", + hash_type2name(*hash_algo)); +} + +void +load_snap_hdr(int fd, struct snap_hdr *hdr) +{ + read_snap_hdr(fd, hdr); + match_ver(hdr->flags); +}