-
Notifications
You must be signed in to change notification settings - Fork 4
/
prune.c
149 lines (138 loc) · 3.38 KB
/
prune.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <dirent.h>
#include <unistd.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <errno.h>
#include "crypto.h"
#include "bloom.h"
#include "sha3.h"
#include "binhex.h"
#include "store.h"
int prune_in_dir(DIR *d, const char *dirname, struct bloom **filters, size_t nfilters, int list_keeps)
{
struct dirent *de;
while ((de = readdir(d))) {
unsigned char hash[HASHLEN];
if (!hex2bin(hash, de->d_name, HASHLEN))
continue;
for (int i=0; i<nfilters; i++) {
if (bloom_query(filters[i], hash, HASHLEN)) {
if (list_keeps)
printf("%s%s\n", dirname, de->d_name);
goto keep;
}
}
if (!list_keeps) printf("%s%s\n", dirname, de->d_name);
keep:
;
}
return 0;
}
int do_prune(int base_fd, struct bloom **filters, size_t nfilters, int list_keeps)
{
for (int i=0; i<4096; i++) {
char dirname[BLOBNAME_SIZE];
snprintf(dirname, sizeof dirname, "objects/%.3x/", i);
int fd = openat(base_fd, dirname, O_RDONLY|O_DIRECTORY|O_CLOEXEC);
if (fd < 0) {
if (errno != ENOENT) {
fprintf(stderr, "error opening %s: ", dirname);
perror(0);
}
continue;
}
DIR *d = fdopendir(fd);
if (!d) {
fprintf(stderr, "error opening %s: ", dirname);
perror(0);
close(fd);
continue;
}
prune_in_dir(d, dirname, filters, nfilters, list_keeps);
closedir(d);
}
return 0;
}
static void prune_usage(char *progname)
{
printf("usage: %s prune [options] <summary_file> ...\n", progname);
}
int prune_main(int argc, char **argv, char *progname)
{
int c, d;
int list_keeps = 0;
void (*usage)(char *) = prune_usage;
while ((c=getopt(argc, argv, "v")) >= 0) switch (c) {
case 'v':
list_keeps = 1;
break;
case '?':
usage(progname);
return 1;
}
if (optind == argc) {
usage(progname);
return 1;
}
int nblooms = argc-optind+1;
struct bloom **b = calloc(sizeof (struct bloom *), nblooms);
char *z = strrchr(argv[optind], '/');
if (z) {
*z = 0;
d = open(argv[optind], O_RDONLY|O_DIRECTORY|O_CLOEXEC);
*z = '/';
if (d<0) {
perror("opening directory");
return 1;
}
} else {
d = AT_FDCWD;
}
b[0] = bloom_create(8, 16*(argc-optind));
for (int i=optind; i<argc; i++) {
int j = i-optind + 1;
FILE *f = fopen(argv[i], "rbe");
if (!f) {
fprintf(stderr, "error opening %s: ", argv[i]);
perror(0);
return 1;
}
char buf[256];
unsigned char bloom_hash[HASHLEN];
while (fgets(buf, sizeof buf, f) && strncmp(buf, "bloom ", 6));
if (feof(f) || !hex2bin(bloom_hash, buf+6, HASHLEN)) {
fprintf(stderr, "no bloom filter found in %s\n", argv[i]);
return 1;
}
fclose(f);
char bloom_filename[BLOBNAME_SIZE];
gen_blob_name(bloom_filename, bloom_hash);
int fd = openat(d, bloom_filename, O_RDONLY|O_CLOEXEC);
f = fdopen(fd, "rb");
if (fd<0 || !f) {
fprintf(stderr, "error opening bloom filter for %s (%s): ",
argv[i], bloom_filename);
perror(0);
return 1;
}
struct stat st;
fstat(fd, &st);
b[j] = malloc(sizeof *b + st.st_size);
b[j]->l = st.st_size - 32;
fread(b[j]->bits, 1, st.st_size, f);
fclose(f);
unsigned char computed_hash[HASHLEN];
sha3(b[j]->bits, st.st_size, computed_hash, HASHLEN);
if (memcmp(computed_hash, bloom_hash, HASHLEN)) {
fprintf(stderr, "hash mismatch in bloom filter for %s (%s)\n",
argv[i], bloom_filename);
return 1;
}
bloom_add(b[0], bloom_hash, HASHLEN);
}
do_prune(d, b, nblooms, list_keeps);
return 0;
}