dup-check.c - dedup - deduplicating backup program
HTML git clone git://bitreich.org/dedup/ git://enlrupgkhuxnvlhsf6lc3fziv5h2hhfrinws65d7roiv6bfj7d652fid.onion/dedup/
DIR Log
DIR Files
DIR Refs
DIR Tags
DIR README
DIR LICENSE
---
dup-check.c (2750B)
---
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 <stdio.h>
8 #include <stdlib.h>
9 #include <unistd.h>
10
11 #include <sodium.h>
12
13 #include "arg.h"
14 #include "block.h"
15 #include "config.h"
16 #include "key.h"
17 #include "lock.h"
18 #include "misc.h"
19 #include "snap.h"
20 #include "state.h"
21
22 struct param param;
23 int verbose;
24 char *argv0;
25
26 static void
27 loadstate(char *repo)
28 {
29 char path[PATH_MAX];
30 int fd;
31
32 if (snprintf(path, sizeof(path), "%s/state", repo) >= sizeof(path))
33 errx(1, "snprintf: %s: path too long", path);
34 fd = open(path, O_RDONLY);
35 if (fd < 0)
36 err(1, "open: %s", path);
37 if (readstate(fd, ¶m) < 0)
38 printerr("readstate: %s", path);
39 if (close(fd) < 0)
40 err(1, "close: %s", path);
41 }
42
43 static void
44 loadkey(char *keyfile)
45 {
46 int fd;
47
48 if (keyfile == NULL)
49 return;
50
51 fd = open(keyfile, O_RDONLY);
52 if (fd < 0)
53 err(1, "open: %s", keyfile);
54 if (readkey(fd, param.key, sizeof(param.key)) < 0)
55 printerr("readkey: %s", keyfile);
56 param.keyloaded = 1;
57 if (close(fd) < 0)
58 err(1, "close: %s", keyfile);
59 }
60
61 static void
62 check(struct sctx *sctx, struct bctx *bctx)
63 {
64 unsigned char md[MDSIZE];
65 int n;
66
67 if (sodium_init() < 0)
68 errx(1, "sodium_init: failed");
69
70 while ((n = sget(sctx, md)) == MDSIZE) {
71 char mdstr[MDSIZE * 2 + 1];
72 int r;
73
74 if ((r = bcheck(bctx, md)) < 0) {
75 printerr("bcheck");
76 } else if (r > 0) {
77 sodium_bin2hex(mdstr, sizeof(mdstr), md, MDSIZE);
78 puts(mdstr);
79 }
80 }
81 if (n < 0)
82 printerr("sget");
83 }
84
85 static void
86 usage(void)
87 {
88 fprintf(stderr, "usage: %s [-v] [-k keyfile] [-r repo] name\n", argv0);
89 exit(1);
90 }
91
92 int
93 main(int argc, char *argv[])
94 {
95 char spath[PATH_MAX];
96 char bpath[PATH_MAX];
97 struct sctx *sctx;
98 struct bctx *bctx;
99 char *keyfile = NULL;
100 char *repo = ".";
101 int lfd;
102
103 ARGBEGIN {
104 case 'k':
105 keyfile = EARGF(usage());
106 break;
107 case 'r':
108 repo = EARGF(usage());
109 break;
110 case 'v':
111 verbose++;
112 break;
113 default:
114 usage();
115 } ARGEND
116
117 if (argc != 1)
118 usage();
119
120 if (snprintf(spath, sizeof(spath), "%s/archive/%s",
121 repo, argv[0]) >= sizeof(spath))
122 errx(1, "snprintf: %s: path too long", spath);
123 if (snprintf(bpath, sizeof(bpath), "%s/storage",
124 repo) >= sizeof(bpath))
125 errx(1, "snprintf: %s: path too long", bpath);
126
127 if ((lfd = lockrepo(repo)) < 0)
128 errx(1, "failed to lock repository");
129
130 loadkey(keyfile);
131 loadstate(repo);
132
133 if (sopen(spath, S_READ, 0600, &sctx) < 0)
134 printerr("sopen: %s", spath);
135 if (bopen(bpath, B_READ, 0600, &bctx) < 0)
136 printerr("bopen: %s", bpath);
137
138 check(sctx, bctx);
139
140 if (bclose(bctx) < 0)
141 printerr("bclose: %s", bpath);
142 if (sclose(sctx) < 0)
143 printerr("sclose: %s", spath);
144
145 if (unlockrepo(lfd) < 0)
146 errx(1, "failed to unlock repository");
147
148 return 0;
149 }