dedup

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

dup-rm.c (2590B)


      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 rm(struct sctx *sctx, struct bctx *bctx)
     62 {
     63 	unsigned char md[MDSIZE];
     64 	int n;
     65 
     66 	while ((n = sget(sctx, md)) == MDSIZE) {
     67 		if (brm(bctx, md) < 0)
     68 			printerr("brm");
     69 	}
     70 	if (n < 0)
     71 		printerr("sget");
     72 }
     73 
     74 static void
     75 usage(void)
     76 {
     77 	fprintf(stderr, "usage: %s [-v] [-k keyfile] [-r repo] name\n", argv0);
     78 	exit(1);
     79 }
     80 
     81 int
     82 main(int argc, char *argv[])
     83 {
     84 	char spath[PATH_MAX];
     85 	char bpath[PATH_MAX];
     86 	struct sctx *sctx;
     87 	struct bctx *bctx;
     88 	char *keyfile = NULL;
     89 	char *repo = ".";
     90 	int lfd;
     91 
     92 	ARGBEGIN {
     93 	case 'k':
     94 		keyfile = EARGF(usage());
     95 		break;
     96 	case 'r':
     97 		repo = EARGF(usage());
     98 		break;
     99 	case 'v':
    100 		verbose++;
    101 		break;
    102 	default:
    103 		usage();
    104 	} ARGEND
    105 
    106 	if (argc != 1)
    107 		usage();
    108 
    109 	if (snprintf(spath, sizeof(spath), "%s/archive/%s",
    110 	             repo, argv[0]) >= sizeof(spath))
    111 		errx(1, "snprintf: %s: path too long", spath);
    112 	if (snprintf(bpath, sizeof(bpath), "%s/storage",
    113 	             repo) >= sizeof(bpath))
    114 		errx(1, "snprintf: %s: path too long", bpath);
    115 
    116 	if ((lfd = lockrepo(repo)) < 0)
    117 		errx(1, "failed to lock repository");
    118 
    119 	loadkey(keyfile);
    120 	loadstate(repo);
    121 
    122 	if (sopen(spath, S_READ, 0600, &sctx) < 0)
    123 		printerr("sopen: %s", spath);
    124 	if (bopen(bpath, B_RDWR, 0600, &bctx) < 0)
    125 		printerr("bopen: %s", bpath);
    126 
    127 	rm(sctx, bctx);
    128 
    129 	if (bclose(bctx) < 0)
    130 		printerr("bclose: %s", bpath);
    131 	if (sclose(sctx) < 0)
    132 		printerr("sclose: %s", spath);
    133 	if (unlink(spath) < 0)
    134 		err(1, "unlink: %s", spath);
    135 
    136 	if (unlockrepo(lfd) < 0)
    137 		errx(1, "failed to unlock repository");
    138 	
    139 	return 0;
    140 }