dedup

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

dup-info.c (2945B)


      1 #include <sys/types.h>
      2 #include <sys/stat.h>
      3 #include <sys/file.h>
      4 
      5 #include <err.h>
      6 #include <fcntl.h>
      7 #include <stdio.h>
      8 #include <stdint.h>
      9 #include <stdlib.h>
     10 #include <string.h>
     11 #include <unistd.h>
     12 
     13 #include "arg.h"
     14 #include "blake2.h"
     15 #include "dedup.h"
     16 
     17 static struct snap_hdr snap_hdr;
     18 static struct blk_hdr blk_hdr;
     19 static int ifd;
     20 static int sfd;
     21 static int hash_algo = HASH_BLAKE2B;
     22 static int compr_algo = COMPR_LZ4;
     23 
     24 int verbose;
     25 char *argv0;
     26 
     27 static void
     28 print_info(int tflag)
     29 {
     30 	struct stats *st = &snap_hdr.st;
     31 
     32 	if (verbose > 0) {
     33 		fprintf(stderr, "Compression algorithm: %s\n",
     34 		        compr_type2name(compr_algo));
     35 		fprintf(stderr, "Hash algorithm: %s\n",
     36 		        hash_type2name(hash_algo));
     37 	}
     38 
     39 	if (st->nr_blks == 0)
     40 		return;
     41 
     42 	if (!tflag) {
     43 		fprintf(stderr, "Original size: %llu bytes\n",
     44 		        (unsigned long long)st->orig_size);
     45 		fprintf(stderr, "Compressed size: %llu bytes\n",
     46 		        (unsigned long long)st->compr_size);
     47 		fprintf(stderr, "Deduplicated size: %llu bytes\n",
     48 		        (unsigned long long)st->dedup_size);
     49 		fprintf(stderr, "Deduplication ratio: %.2f\n",
     50 		        (double)st->orig_size / st->dedup_size);
     51 		fprintf(stderr, "Min/avg/max block size: %llu/%llu/%llu bytes\n",
     52 		        (unsigned long long)st->min_blk_size,
     53 		        (unsigned long long)st->dedup_size / st->nr_blks,
     54 		        (unsigned long long)st->max_blk_size);
     55 		fprintf(stderr, "Number of unique blocks: %llu\n",
     56 		        (unsigned long long)st->nr_blks);
     57 	} else {
     58 		/* terse mode */
     59 		fprintf(stderr, "%llu %llu %llu %.2f %llu %llu %llu %llu\n",
     60 		        (unsigned long long)st->orig_size,
     61 		        (unsigned long long)st->compr_size,
     62 		        (unsigned long long)st->dedup_size,
     63 		        (double)st->orig_size / st->dedup_size,
     64 		        (unsigned long long)st->min_blk_size,
     65 		        (unsigned long long)st->dedup_size / st->nr_blks,
     66 		        (unsigned long long)st->max_blk_size,
     67 		        (unsigned long long)st->nr_blks);
     68 	}
     69 }
     70 
     71 static void
     72 init(void)
     73 {
     74 	ifd = open(SNAPSF, O_RDONLY, 0600);
     75 	if (ifd < 0)
     76 		err(1, "open %s", SNAPSF);
     77 
     78 	sfd = open(STOREF, O_RDONLY, 0600);
     79 	if (sfd < 0)
     80 		err(1, "open %s", STOREF);
     81 
     82 	if (flock(ifd, LOCK_NB | LOCK_EX) < 0 ||
     83 	    flock(sfd, LOCK_NB | LOCK_EX) < 0)
     84 		err(1, "flock");
     85 
     86 	xlseek(ifd, 0, SEEK_SET);
     87 	load_snap_hdr(ifd, &snap_hdr);
     88 	xlseek(sfd, 0, SEEK_SET);
     89 	load_blk_hdr(sfd, &blk_hdr, &compr_algo, &hash_algo);
     90 }
     91 
     92 static void
     93 term(void)
     94 {
     95 	close(ifd);
     96 	close(sfd);
     97 }
     98 
     99 static void
    100 usage(void)
    101 {
    102 	fprintf(stderr, "usage: %s [-tv] [repo]\n", argv0);
    103 	exit(1);
    104 }
    105 
    106 int
    107 main(int argc, char *argv[])
    108 {
    109 	char *repo = NULL;
    110 	int tflag = 0;
    111 
    112 	ARGBEGIN {
    113 	case 't':
    114 		tflag = 1;
    115 		break;
    116 	case 'v':
    117 		verbose++;
    118 		break;
    119 	default:
    120 		usage();
    121 	} ARGEND
    122 
    123 	switch (argc) {
    124 	case 0:
    125 		repo = ".";
    126 		break;
    127 	case 1:
    128 		repo = argv[0];
    129 		break;
    130 	default:
    131 		usage();
    132 	};
    133 
    134 	if (chdir(repo) < 0)
    135 		err(1, "chdir: %s", repo);
    136 
    137 	init();
    138 	print_info(tflag);
    139 	term();
    140 	return 0;
    141 }