-
Notifications
You must be signed in to change notification settings - Fork 0
/
scrub.c
200 lines (161 loc) · 4.1 KB
/
scrub.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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
/*
* schizo, a set of tools for managing split disk images
* Copyright (C) 2021 Lennert Buytenhek
*
* This library is free software; you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License version
* 2.1 as published by the Free Software Foundation.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License version 2.1 for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License version 2.1 along with this library; if not, write to the
* Free Software Foundation, Inc., 51 Franklin Street - Fifth Floor,
* Boston, MA 02110-1301, USA.
*/
#define _GNU_SOURCE
#include <stdio.h>
#include <stdlib.h>
#include <fcntl.h>
#include <gcrypt.h>
#include <limits.h>
#include <string.h>
#include "enumerate_chunks.h"
#include "rw.h"
#include "schizo.h"
static struct repo *r;
static uint64_t num;
static uint64_t num_mismatch;
struct scrub_thread_state {
int corruptdir;
uint8_t *buf;
size_t buf_size;
uint64_t num;
uint64_t num_mismatch;
};
static void scrub_thread_init(void *_sts)
{
struct scrub_thread_state *sts = _sts;
sts->corruptdir = r->corruptdir;
sts->buf = NULL;
sts->buf_size = 0;
sts->num = 0;
sts->num_mismatch = 0;
}
static int try_open_corruptdir(struct scrub_thread_state *sts)
{
int dir;
dir = openat(r->repodir, "corrupt", O_DIRECTORY);
if (dir < 0) {
if (errno == ENOENT) {
int ret;
ret = mkdirat(r->repodir, "corrupt", 0777);
if (ret < 0 && errno != EEXIST) {
perror("mkdirat");
return -1;
}
dir = openat(r->repodir, "corrupt", O_DIRECTORY);
}
if (dir < 0) {
perror("openat");
return -1;
}
}
sts->corruptdir = dir;
return 0;
}
static int use_corruptdir(struct scrub_thread_state *sts)
{
if (sts->corruptdir == -1 && try_open_corruptdir(sts) < 0)
sts->corruptdir = -2;
if (sts->corruptdir == -2)
return 0;
return 1;
}
static void scrub_chunk(void *_sts, int section, const char *dir,
int dirfd, const char *name, const uint8_t *hash)
{
struct scrub_thread_state *sts = _sts;
int fd;
int corrupt;
struct stat statbuf;
uint8_t hash2[hash_size];
fd = openat(dirfd, name, O_RDONLY);
if (fd < 0) {
perror("openat");
return;
}
corrupt = 0;
if (fstat(fd, &statbuf) < 0) {
perror("fstat");
goto out;
}
if (statbuf.st_size > SSIZE_MAX) {
fprintf(stderr, "chunk %s/%s too big\n", dir, name);
goto out;
}
if (sts->buf_size < statbuf.st_size) {
if (sts->buf != NULL) {
free(sts->buf);
sts->buf = NULL;
}
sts->buf = malloc(statbuf.st_size);
if (sts->buf == NULL) {
fprintf(stderr, "out of memory scrubbing "
"%s/%s, %zd bytes\n", dir, name,
statbuf.st_size);
goto out;
}
sts->buf_size = statbuf.st_size;
}
if (xpread(fd, sts->buf, statbuf.st_size, 0) != statbuf.st_size) {
fprintf(stderr, "short read scrubbing %s/%s\n", dir, name);
corrupt = 1;
goto out;
}
sts->num++;
gcry_md_hash_buffer(hash_algo, hash2, sts->buf, statbuf.st_size);
if (memcmp(hash, hash2, hash_size)) {
fprintf(stderr, "hash mismatch in %s/%s\n", dir, name);
sts->num_mismatch++;
corrupt = 1;
}
out:
if (corrupt && use_corruptdir(sts)) {
renameat2(dirfd, name, sts->corruptdir, name,
RENAME_NOREPLACE);
}
close(fd);
}
static void scrub_thread_deinit(void *_sts)
{
struct scrub_thread_state *sts = _sts;
if (sts->corruptdir != -1 && sts->corruptdir != r->corruptdir)
close(sts->corruptdir);
num += sts->num;
num_mismatch += sts->num_mismatch;
}
int scrub(int argc, char *argv[])
{
int i;
if (argc)
return -1;
for (i = 0; i < rs.num_repos; i++) {
r = rs.repos[i];
num = 0;
num_mismatch = 0;
enumerate_chunks(r, hash_size,
sizeof(struct scrub_thread_state),
4 * sysconf(_SC_NPROCESSORS_ONLN),
scrub_thread_init, NULL, scrub_chunk,
scrub_thread_deinit);
printf("scrubbed %" PRId64 " chunks", num);
if (num_mismatch)
printf(", %" PRId64 " mismatches", num_mismatch);
printf("\n");
}
return num_mismatch ? 1 : 0;
}