dedup

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

dup-gc.c (2047B)


      1 #include <sys/types.h>
      2 #include <sys/stat.h>
      3 
      4 #include <err.h>
      5 #include <fcntl.h>
      6 #include <limits.h>
      7 #include <stdint.h>
      8 #include <stdio.h>
      9 #include <stdlib.h>
     10 #include <unistd.h>
     11 
     12 #include "arg.h"
     13 #include "block.h"
     14 #include "config.h"
     15 #include "key.h"
     16 #include "lock.h"
     17 #include "misc.h"
     18 #include "snap.h"
     19 #include "state.h"
     20 
     21 struct param param;
     22 int verbose;
     23 char *argv0;
     24 
     25 static void
     26 loadstate(char *repo)
     27 {
     28 	char path[PATH_MAX];
     29 	int fd;
     30 
     31 	if (snprintf(path, sizeof(path), "%s/state", repo) >= sizeof(path))
     32 		errx(1, "snprintf: %s: path too long", path);
     33 	fd = open(path, O_RDONLY);
     34 	if (fd < 0)
     35 		err(1, "open: %s", path);
     36 	if (readstate(fd, &param) < 0)
     37 		printerr("readstate: %s", path);
     38 	if (close(fd) < 0)
     39 		err(1, "close: %s", path);
     40 }
     41 
     42 static void
     43 loadkey(char *keyfile)
     44 {
     45 	int fd;
     46 
     47 	if (keyfile == NULL)
     48 		return;
     49 
     50 	fd = open(keyfile, O_RDONLY);
     51 	if (fd < 0)
     52 		err(1, "open: %s", keyfile);
     53 	if (readkey(fd, param.key, sizeof(param.key)) < 0)
     54 		printerr("readkey: %s", keyfile);
     55 	param.keyloaded = 1;
     56 	if (close(fd) < 0)
     57 		err(1, "close: %s", keyfile);
     58 }
     59 
     60 static void
     61 usage(void)
     62 {
     63 	fprintf(stderr, "usage: %s [-v] [-k keyfile] [repo]\n", argv0);
     64 	exit(1);
     65 }
     66 
     67 int
     68 main(int argc, char *argv[])
     69 {
     70 	char path[PATH_MAX];
     71 	struct bctx *bctx;
     72 	char *keyfile = NULL;
     73 	char *repo;
     74 	int lfd;
     75 
     76 	ARGBEGIN {
     77 	case 'k':
     78 		keyfile = EARGF(usage());
     79 		break;
     80 	case 'v':
     81 		verbose++;
     82 		break;
     83 	default:
     84 		usage();
     85 	} ARGEND
     86 
     87 	switch (argc) {
     88 	case 0:
     89 		repo = ".";
     90 		break;
     91 	case 1:
     92 		repo = argv[0];
     93 		break;
     94 	default:
     95 		usage();
     96 	};
     97 
     98 	if (snprintf(path, sizeof(path), "%s/%s",
     99 	             repo, STORAGEPATH) >= sizeof(path))
    100 		errx(1, "snprintf: %s: path too long", path);
    101 
    102 	if ((lfd = lockrepo(repo)) < 0)
    103 		errx(1, "failed to lock repository");
    104 
    105 	loadkey(keyfile);
    106 	loadstate(repo);
    107 
    108 	if (bopen(path, B_RDWR, 0600, &bctx) < 0)
    109 		printerr("bopen: %s", path);
    110 	if (bgc(bctx) < 0)
    111 		printerr("bgc: %s", path);
    112 	if (bclose(bctx) < 0)
    113 		printerr("bclose: %s", path);
    114 
    115 	if (unlockrepo(lfd) < 0)
    116 		errx(1, "failed to unlock repository");
    117 	return 0;
    118 }