forked from archiecobbs/s3backer
-
Notifications
You must be signed in to change notification settings - Fork 0
/
test_io.c
356 lines (303 loc) · 10.6 KB
/
test_io.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
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
/*
* s3backer - FUSE-based single file backing store via Amazon S3
*
* Copyright 2008-2011 Archie L. Cobbs <[email protected]>
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This program 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 General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
* 02110-1301, USA.
*/
#include "s3backer.h"
#include "http_io.h"
#include "block_part.h"
#include "test_io.h"
/* Do we want random errors? */
#define RANDOM_ERROR_PERCENT 0
/* Internal state */
struct test_io_private {
struct http_io_conf *config;
u_char zero_block[0];
};
/* s3backer_store functions */
static int test_io_meta_data(struct s3backer_store *s3b, off_t *file_sizep, u_int *block_sizep);
static int test_io_set_mounted(struct s3backer_store *s3b, int *old_valuep, int new_value);
static int test_io_read_block(struct s3backer_store *s3b, s3b_block_t block_num, void *dest,
u_char *actual_md5, const u_char *expect_md5, int strict);
static int test_io_write_block(struct s3backer_store *s3b, s3b_block_t block_num, const void *src, u_char *md5,
check_cancel_t *check_cancel, void *check_cancel_arg);
static int test_io_read_block_part(struct s3backer_store *s3b, s3b_block_t block_num, u_int off, u_int len, void *dest);
static int test_io_write_block_part(struct s3backer_store *s3b, s3b_block_t block_num, u_int off, u_int len, const void *src);
static int test_io_list_blocks(struct s3backer_store *s3b, block_list_func_t *callback, void *arg);
static int test_io_flush(struct s3backer_store *s3b);
static void test_io_destroy(struct s3backer_store *s3b);
/*
* Constructor
*
* On error, returns NULL and sets `errno'.
*/
struct s3backer_store *
test_io_create(struct http_io_conf *config)
{
struct s3backer_store *s3b;
struct test_io_private *priv;
/* Initialize structures */
if ((s3b = calloc(1, sizeof(*s3b))) == NULL)
return NULL;
s3b->meta_data = test_io_meta_data;
s3b->set_mounted = test_io_set_mounted;
s3b->read_block = test_io_read_block;
s3b->write_block = test_io_write_block;
s3b->read_block_part = test_io_read_block_part;
s3b->write_block_part = test_io_write_block_part;
s3b->list_blocks = test_io_list_blocks;
s3b->flush = test_io_flush;
s3b->destroy = test_io_destroy;
if ((priv = calloc(1, sizeof(*priv) + config->block_size)) == NULL) {
free(s3b);
errno = ENOMEM;
return NULL;
}
priv->config = config;
s3b->data = priv;
/* Random initialization */
srandom((u_int)time(NULL));
/* Done */
return s3b;
}
static int
test_io_meta_data(struct s3backer_store *s3b, off_t *file_sizep, u_int *block_sizep)
{
return 0;
}
static int
test_io_set_mounted(struct s3backer_store *s3b, int *old_valuep, int new_value)
{
if (old_valuep != NULL)
*old_valuep = 0;
return 0;
}
static int
test_io_flush(struct s3backer_store *const s3b)
{
return 0;
}
static void
test_io_destroy(struct s3backer_store *const s3b)
{
struct test_io_private *const priv = s3b->data;
free(priv);
free(s3b);
}
static int
test_io_read_block(struct s3backer_store *const s3b, s3b_block_t block_num, void *dest,
u_char *actual_md5, const u_char *expect_md5, int strict)
{
struct test_io_private *const priv = s3b->data;
struct http_io_conf *const config = priv->config;
u_char md5[MD5_DIGEST_LENGTH];
char path[PATH_MAX];
int zero_block;
MD5_CTX ctx;
int fd;
int r;
/* Logging */
if (config->debug)
(*config->log)(LOG_DEBUG, "test_io: read %0*jx started", S3B_BLOCK_NUM_DIGITS, (uintmax_t)block_num);
/* Random delay */
usleep((random() % 200) * 1000);
/* Random error */
if ((random() % 100) < RANDOM_ERROR_PERCENT) {
(*config->log)(LOG_ERR, "test_io: random failure reading %0*jx", S3B_BLOCK_NUM_DIGITS, (uintmax_t)block_num);
return EAGAIN;
}
/* Generate path */
snprintf(path, sizeof(path), "%s/%s%0*jx", config->bucket, config->prefix, S3B_BLOCK_NUM_DIGITS, (uintmax_t)block_num);
/* Read block */
if ((fd = open(path, O_RDONLY)) != -1) {
int total;
/* Read file */
for (total = 0; total < config->block_size; total += r) {
if ((r = read(fd, (char *)dest + total, config->block_size - total)) == -1) {
r = errno;
(*config->log)(LOG_ERR, "can't read %s: %s", path, strerror(r));
close(fd);
return r;
}
if (r == 0)
break;
}
close(fd);
/* Check for short read */
if (total != config->block_size) {
(*config->log)(LOG_ERR, "%s: file is truncated (only read %d out of %u bytes)", path, total, config->block_size);
return EIO;
}
/* Done */
r = 0;
} else
r = errno;
/* Convert ENOENT into a read of all zeroes */
if ((zero_block = (r == ENOENT))) {
memset(dest, 0, config->block_size);
r = 0;
}
/* Check for other error */
if (r != 0) {
(*config->log)(LOG_ERR, "can't open %s: %s", path, strerror(r));
return r;
}
/* Compute MD5 */
if (zero_block)
memset(md5, 0, MD5_DIGEST_LENGTH);
else {
MD5_Init(&ctx);
MD5_Update(&ctx, dest, config->block_size);
MD5_Final(md5, &ctx);
}
if (actual_md5 != NULL)
memcpy(actual_md5, md5, MD5_DIGEST_LENGTH);
/* Check expected MD5 */
if (expect_md5 != NULL) {
const int match = memcmp(md5, expect_md5, MD5_DIGEST_LENGTH);
if (strict) {
if (!match) {
(*config->log)(LOG_ERR, "%s: wrong MD5 checksum?!", path);
return EINVAL;
}
} else if (match)
r = EEXIST;
}
/* Logging */
if (config->debug) {
(*config->log)(LOG_DEBUG, "test_io: read %0*jx complete%s%s", S3B_BLOCK_NUM_DIGITS, (uintmax_t)block_num,
zero_block ? " (zero)" : "", r == EEXIST ? " (expected md5 match)" : "");
}
/* Done */
return r;
}
static int
test_io_write_block(struct s3backer_store *const s3b, s3b_block_t block_num, const void *src, u_char *caller_md5,
check_cancel_t *check_cancel, void *check_cancel_arg)
{
struct test_io_private *const priv = s3b->data;
struct http_io_conf *const config = priv->config;
u_char md5[MD5_DIGEST_LENGTH];
char temp[PATH_MAX];
char path[PATH_MAX];
MD5_CTX ctx;
int total;
int fd;
int r;
/* Check for zero block */
if (src != NULL && memcmp(src, priv->zero_block, config->block_size) == 0)
src = NULL;
/* Compute MD5 */
if (src != NULL) {
MD5_Init(&ctx);
MD5_Update(&ctx, src, config->block_size);
MD5_Final(md5, &ctx);
} else
memset(md5, 0, MD5_DIGEST_LENGTH);
/* Return MD5 to caller */
if (caller_md5 != NULL)
memcpy(caller_md5, md5, MD5_DIGEST_LENGTH);
/* Logging */
if (config->debug) {
(*config->log)(LOG_DEBUG, "test_io: write %0*jx started%s",
S3B_BLOCK_NUM_DIGITS, (uintmax_t)block_num, src == NULL ? " (zero block)" : "");
}
/* Random delay */
usleep((random() % 200) * 1000);
/* Random error */
if ((random() % 100) < RANDOM_ERROR_PERCENT) {
(*config->log)(LOG_ERR, "test_io: random failure writing %0*jx", S3B_BLOCK_NUM_DIGITS, (uintmax_t)block_num);
return EAGAIN;
}
/* Generate path */
snprintf(path, sizeof(path), "%s/%s%0*jx", config->bucket, config->prefix, S3B_BLOCK_NUM_DIGITS, (uintmax_t)block_num);
/* Delete zero blocks */
if (src == NULL) {
if (unlink(path) == -1 && errno != ENOENT) {
r = errno;
(*config->log)(LOG_ERR, "can't unlink %s: %s", path, strerror(r));
return r;
}
return 0;
}
/* Write into temporary file */
snprintf(temp, sizeof(temp), "%s.XXXXXX", path);
if ((fd = mkstemp(temp)) == -1) {
r = errno;
(*config->log)(LOG_ERR, "%s: %s", temp, strerror(r));
return r;
}
for (total = 0; total < config->block_size; total += r) {
if ((r = write(fd, (const char *)src + total, config->block_size - total)) == -1) {
r = errno;
(*config->log)(LOG_ERR, "can't write %s: %s", temp, strerror(r));
close(fd);
(void)unlink(temp);
return r;
}
}
close(fd);
/* Rename file */
if (rename(temp, path) == -1) {
r = errno;
(*config->log)(LOG_ERR, "can't rename %s: %s", temp, strerror(r));
(void)unlink(temp);
return r;
}
/* Logging */
if (config->debug)
(*config->log)(LOG_DEBUG, "test_io: write %0*jx complete", S3B_BLOCK_NUM_DIGITS, (uintmax_t)block_num);
/* Done */
return 0;
}
static int
test_io_read_block_part(struct s3backer_store *s3b, s3b_block_t block_num, u_int off, u_int len, void *dest)
{
struct test_io_private *const priv = s3b->data;
struct http_io_conf *const config = priv->config;
return block_part_read_block_part(s3b, block_num, config->block_size, off, len, dest);
}
static int
test_io_write_block_part(struct s3backer_store *s3b, s3b_block_t block_num, u_int off, u_int len, const void *src)
{
struct test_io_private *const priv = s3b->data;
struct http_io_conf *const config = priv->config;
return block_part_write_block_part(s3b, block_num, config->block_size, off, len, src);
}
static int
test_io_list_blocks(struct s3backer_store *s3b, block_list_func_t *callback, void *arg)
{
struct test_io_private *const priv = s3b->data;
struct http_io_conf *const config = priv->config;
s3b_block_t block_num, reversed_block_num;
struct dirent *dent;
DIR *dir;
int i;
/* Open directory */
if ((dir = opendir(config->bucket)) == NULL)
return errno;
/* Scan directory */
for (i = 0; (dent = readdir(dir)) != NULL; i++) {
if (http_io_parse_block(config, dent->d_name, &block_num, &reversed_block_num) == 0)
(*callback)(arg, block_num);
}
/* Close directory */
closedir(dir);
/* Done */
return 0;
}