dedup

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

dup-list.c (1736B)


      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_md(FILE *fp, uint8_t *md, size_t size)
     29 {
     30 	size_t i;
     31 
     32 	for (i = 0; i < size; i++)
     33 		fprintf(fp, "%02x", md[i]);
     34 }
     35 
     36 static int
     37 list(struct snap *snap, void *arg)
     38 {
     39 	print_md(stdout, snap->md, sizeof(snap->md));
     40 	if (snap->msg[0] != '\0')
     41 		printf("\t%s\n", snap->msg);
     42 	else
     43 		putchar('\n');
     44 	return WALK_CONTINUE;
     45 }
     46 
     47 static void
     48 init(void)
     49 {
     50 	ifd = open(SNAPSF, O_RDONLY, 0600);
     51 	if (ifd < 0)
     52 		err(1, "open %s", SNAPSF);
     53 
     54 	sfd = open(STOREF, O_RDONLY, 0600);
     55 	if (sfd < 0)
     56 		err(1, "open %s", STOREF);
     57 
     58 	if (flock(ifd, LOCK_NB | LOCK_EX) < 0 ||
     59 	    flock(sfd, LOCK_NB | LOCK_EX) < 0)
     60 		err(1, "flock");
     61 
     62 	xlseek(ifd, 0, SEEK_SET);
     63 	load_snap_hdr(ifd, &snap_hdr);
     64 	xlseek(sfd, 0, SEEK_SET);
     65 	load_blk_hdr(sfd, &blk_hdr, &compr_algo, &hash_algo);
     66 }
     67 
     68 static void
     69 term(void)
     70 {
     71 	close(ifd);
     72 	close(sfd);
     73 }
     74 
     75 static void
     76 usage(void)
     77 {
     78 	fprintf(stderr, "usage: %s [-v] [repo]\n", argv0);
     79 	exit(1);
     80 }
     81 
     82 int
     83 main(int argc, char *argv[])
     84 {
     85 	char *repo = NULL;
     86 
     87 	ARGBEGIN {
     88 	case 'v':
     89 		verbose++;
     90 		break;
     91 	default:
     92 		usage();
     93 	} ARGEND
     94 
     95 	switch (argc) {
     96 	case 0:
     97 		repo = ".";
     98 		break;
     99 	case 1:
    100 		repo = argv[0];
    101 		break;
    102 	default:
    103 		usage();
    104 	};
    105 
    106 	if (chdir(repo) < 0)
    107 		err(1, "chdir: %s", repo);
    108 
    109 	init();
    110 	walk_snap(ifd, &snap_hdr, list, NULL);
    111 	term();
    112 	return 0;
    113 }