/
fs-metawrap.c
623 lines (536 loc) · 16.2 KB
/
fs-metawrap.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
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
/* Copyright (c) 2013-2016 Dovecot authors, see the included COPYING file */
#include "lib.h"
#include "array.h"
#include "str.h"
#include "strescape.h"
#include "istream.h"
#include "istream-private.h"
#include "istream-concat.h"
#include "istream-metawrap.h"
#include "ostream.h"
#include "ostream-metawrap.h"
#include "iostream-temp.h"
#include "fs-api-private.h"
struct metawrap_fs {
struct fs fs;
bool wrap_metadata;
};
struct metawrap_fs_file {
struct fs_file file;
struct metawrap_fs *fs;
struct fs_file *super, *super_read;
enum fs_open_mode open_mode;
struct istream *input;
bool metadata_read;
struct ostream *super_output;
struct ostream *temp_output;
string_t *metadata_header;
uoff_t metadata_write_size;
bool metadata_changed_since_write;
};
struct metawrap_fs_iter {
struct fs_iter iter;
struct fs_iter *super;
};
static struct fs *fs_metawrap_alloc(void)
{
struct metawrap_fs *fs;
fs = i_new(struct metawrap_fs, 1);
fs->fs = fs_class_metawrap;
return &fs->fs;
}
static int
fs_metawrap_init(struct fs *_fs, const char *args, const
struct fs_settings *set)
{
struct metawrap_fs *fs = (struct metawrap_fs *)_fs;
const char *parent_name, *parent_args, *error;
if (*args == '\0') {
fs_set_error(_fs, "Parent filesystem not given as parameter");
return -1;
}
parent_args = strchr(args, ':');
if (parent_args == NULL) {
parent_name = args;
parent_args = "";
} else {
parent_name = t_strdup_until(args, parent_args);
parent_args++;
}
if (fs_init(parent_name, parent_args, set, &_fs->parent, &error) < 0) {
fs_set_error(_fs, "%s", error);
return -1;
}
if ((fs_get_properties(_fs->parent) & FS_PROPERTY_METADATA) == 0)
fs->wrap_metadata = TRUE;
return 0;
}
static void fs_metawrap_deinit(struct fs *_fs)
{
struct metawrap_fs *fs = (struct metawrap_fs *)_fs;
if (_fs->parent != NULL)
fs_deinit(&_fs->parent);
i_free(fs);
}
static enum fs_properties fs_metawrap_get_properties(struct fs *_fs)
{
const struct metawrap_fs *fs = (const struct metawrap_fs *)_fs;
enum fs_properties props;
props = fs_get_properties(_fs->parent);
if (fs->wrap_metadata) {
/* we don't have a quick stat() to see the file's size,
because of the metadata header */
props &= ~FS_PROPERTY_STAT;
/* Copying can copy the whole metadata. */
props |= FS_PROPERTY_COPY_METADATA;
}
return props;
}
static struct fs_file *
fs_metawrap_file_init(struct fs *_fs, const char *path,
enum fs_open_mode mode, enum fs_open_flags flags)
{
struct metawrap_fs *fs = (struct metawrap_fs *)_fs;
struct metawrap_fs_file *file;
file = i_new(struct metawrap_fs_file, 1);
file->file.fs = _fs;
file->file.path = i_strdup(path);
file->fs = fs;
file->open_mode = mode;
/* avoid unnecessarily creating two seekable streams */
flags &= ~FS_OPEN_FLAG_SEEKABLE;
file->super = fs_file_init(_fs->parent, path, mode | flags);
if (file->fs->wrap_metadata && mode == FS_OPEN_MODE_READONLY &&
(flags & FS_OPEN_FLAG_ASYNC) == 0) {
/* use async stream for super, so fs_read_stream() won't create
another seekable stream unneededly */
file->super_read = fs_file_init(_fs->parent, path, mode | flags |
FS_OPEN_FLAG_ASYNC);
} else {
file->super_read = file->super;
}
fs_metadata_init(&file->file);
return &file->file;
}
static void fs_metawrap_file_deinit(struct fs_file *_file)
{
struct metawrap_fs_file *file = (struct metawrap_fs_file *)_file;
if (file->super_read != file->super && file->super_read != NULL)
fs_file_deinit(&file->super_read);
if (file->metadata_header != NULL)
str_free(&file->metadata_header);
fs_file_deinit(&file->super);
i_free(file->file.path);
i_free(file);
}
static void fs_metawrap_file_close(struct fs_file *_file)
{
struct metawrap_fs_file *file = (struct metawrap_fs_file *)_file;
if (file->input != NULL)
i_stream_unref(&file->input);
if (file->super_read != NULL)
fs_file_close(file->super_read);
if (file->super != NULL)
fs_file_close(file->super);
}
static const char *fs_metawrap_file_get_path(struct fs_file *_file)
{
struct metawrap_fs_file *file = (struct metawrap_fs_file *)_file;
return fs_file_path(file->super);
}
static void
fs_metawrap_set_async_callback(struct fs_file *_file,
fs_file_async_callback_t *callback,
void *context)
{
struct metawrap_fs_file *file = (struct metawrap_fs_file *)_file;
fs_file_set_async_callback(file->super, callback, context);
}
static int fs_metawrap_wait_async(struct fs *_fs)
{
return fs_wait_async(_fs->parent);
}
static void
fs_metawrap_set_metadata(struct fs_file *_file, const char *key,
const char *value)
{
struct metawrap_fs_file *file = (struct metawrap_fs_file *)_file;
if (!file->fs->wrap_metadata)
fs_set_metadata(file->super, key, value);
else {
fs_default_set_metadata(_file, key, value);
file->metadata_changed_since_write = TRUE;
}
}
static int
fs_metawrap_get_metadata(struct fs_file *_file,
const ARRAY_TYPE(fs_metadata) **metadata_r)
{
struct metawrap_fs_file *file = (struct metawrap_fs_file *)_file;
ssize_t ret;
char c;
if (!file->fs->wrap_metadata)
return fs_get_metadata(file->super, metadata_r);
if (file->metadata_read) {
/* we have the metadata */
} else if (file->input == NULL) {
if (fs_read(_file, &c, 1) < 0)
return -1;
} else {
/* use the existing istream to read it */
while ((ret = i_stream_read(file->input)) == 0) {
if (file->metadata_read)
break;
i_assert(!file->input->blocking);
if (fs_wait_async(_file->fs) < 0)
return -1;
}
if (ret == -1 && file->input->stream_errno != 0) {
fs_set_error(_file->fs, "read(%s) failed: %s",
i_stream_get_name(file->input),
i_stream_get_error(file->input));
return -1;
}
i_assert(file->metadata_read);
}
*metadata_r = &_file->metadata;
return 0;
}
static bool fs_metawrap_prefetch(struct fs_file *_file, uoff_t length)
{
struct metawrap_fs_file *file = (struct metawrap_fs_file *)_file;
if (!file->fs->wrap_metadata)
return fs_prefetch(file->super, length);
else
return fs_prefetch(file->super_read, length);
}
static ssize_t fs_metawrap_read(struct fs_file *_file, void *buf, size_t size)
{
struct metawrap_fs_file *file = (struct metawrap_fs_file *)_file;
if (!file->fs->wrap_metadata)
return fs_read(file->super, buf, size);
return fs_read_via_stream(_file, buf, size);
}
static void
fs_metawrap_callback(const char *key, const char *value, void *context)
{
struct metawrap_fs_file *file = context;
if (key == NULL) {
file->metadata_read = TRUE;
return;
}
T_BEGIN {
key = str_tabunescape(t_strdup_noconst(key));
value = str_tabunescape(t_strdup_noconst(value));
fs_default_set_metadata(&file->file, key, value);
} T_END;
}
static struct istream *
fs_metawrap_read_stream(struct fs_file *_file, size_t max_buffer_size)
{
struct metawrap_fs_file *file = (struct metawrap_fs_file *)_file;
struct istream *input;
if (!file->fs->wrap_metadata)
return fs_read_stream(file->super, max_buffer_size);
if (file->input != NULL) {
i_stream_ref(file->input);
i_stream_seek(file->input, 0);
return file->input;
}
input = fs_read_stream(file->super_read, max_buffer_size);
file->input = i_stream_create_metawrap(input, fs_metawrap_callback, file);
i_stream_unref(&input);
i_stream_ref(file->input);
return file->input;
}
static int fs_metawrap_write(struct fs_file *_file, const void *data, size_t size)
{
struct metawrap_fs_file *file = (struct metawrap_fs_file *)_file;
if (!file->fs->wrap_metadata)
return fs_write(file->super, data, size);
return fs_write_via_stream(_file, data, size);
}
static void
fs_metawrap_append_metadata(struct metawrap_fs_file *file, string_t *str)
{
const struct fs_metadata *metadata;
array_foreach(&file->file.metadata, metadata) {
if (strncmp(metadata->key, FS_METADATA_INTERNAL_PREFIX,
strlen(FS_METADATA_INTERNAL_PREFIX)) == 0)
continue;
str_append_tabescaped(str, metadata->key);
str_append_c(str, ':');
str_append_tabescaped(str, metadata->value);
str_append_c(str, '\n');
}
str_append_c(str, '\n');
}
static void fs_metawrap_write_metadata(void *context)
{
struct metawrap_fs_file *file = context;
string_t *str = t_str_new(256);
ssize_t ret;
fs_metawrap_append_metadata(file, str);
file->metadata_write_size = str_len(str);
ret = o_stream_send(file->file.output, str_data(str), str_len(str));
if (ret < 0)
o_stream_close(file->file.output);
else
i_assert((size_t)ret == str_len(str));
file->metadata_changed_since_write = FALSE;
}
static void fs_metawrap_write_stream(struct fs_file *_file)
{
struct metawrap_fs_file *file = (struct metawrap_fs_file *)_file;
i_assert(_file->output == NULL);
if (!file->fs->wrap_metadata) {
file->super_output = fs_write_stream(file->super);
_file->output = file->super_output;
} else {
file->temp_output =
iostream_temp_create_named(_file->fs->temp_path_prefix,
IOSTREAM_TEMP_FLAG_TRY_FD_DUP,
fs_file_path(_file));
_file->output = o_stream_create_metawrap(file->temp_output,
fs_metawrap_write_metadata, file);
}
}
static struct istream *
fs_metawrap_create_updated_istream(struct metawrap_fs_file *file,
struct istream *input)
{
struct istream *input2, *inputs[3];
if (file->metadata_header != NULL)
str_truncate(file->metadata_header, 0);
else
file->metadata_header = str_new(default_pool, 1024);
fs_metawrap_append_metadata(file, file->metadata_header);
inputs[0] = i_stream_create_from_data(str_data(file->metadata_header),
str_len(file->metadata_header));
i_stream_seek(input, file->metadata_write_size);
inputs[1] = i_stream_create_limit(input, (uoff_t)-1);
inputs[2] = NULL;
input2 = i_stream_create_concat(inputs);
i_stream_unref(&inputs[0]);
i_stream_unref(&inputs[1]);
file->metadata_write_size = str_len(file->metadata_header);
return input2;
}
static int fs_metawrap_write_stream_finish(struct fs_file *_file, bool success)
{
struct metawrap_fs_file *file = (struct metawrap_fs_file *)_file;
struct istream *input;
int ret;
if (_file->output != NULL) {
if (_file->output->closed)
success = FALSE;
if (_file->output == file->super_output)
_file->output = NULL;
else
o_stream_unref(&_file->output);
}
if (!success) {
if (file->super_output != NULL) {
/* no metawrap */
i_assert(file->temp_output == NULL);
fs_write_stream_abort(file->super, &file->super_output);
} else if (file->temp_output == NULL) {
/* finishing up */
i_assert(file->super_output == NULL);
fs_write_stream_abort(file->super, &file->super_output);
} else {
o_stream_destroy(&file->temp_output);
}
return -1;
}
if (file->super_output != NULL) {
/* no metawrap */
i_assert(file->temp_output == NULL);
return fs_write_stream_finish(file->super, &file->super_output);
}
if (file->temp_output == NULL) {
/* finishing up */
i_assert(file->super_output == NULL);
return fs_write_stream_finish_async(file->super);
}
/* finish writing the temporary file */
input = iostream_temp_finish(&file->temp_output, IO_BLOCK_SIZE);
if (file->metadata_changed_since_write) {
/* we'll need to recreate the metadata. do this by creating a
new istream combining the new metadata header and the
old body. */
struct istream *input2 = input;
input = fs_metawrap_create_updated_istream(file, input);
i_stream_unref(&input2);
}
file->super_output = fs_write_stream(file->super);
(void)o_stream_send_istream(file->super_output, input);
if (input->stream_errno != 0) {
fs_set_error(_file->fs, "read(%s) failed: %s",
i_stream_get_name(input),
i_stream_get_error(input));
fs_write_stream_abort(file->super, &file->super_output);
ret = -1;
} else if (file->super_output->stream_errno != 0) {
fs_set_error(_file->fs, "write(%s) failed: %s",
o_stream_get_name(file->super_output),
o_stream_get_error(file->super_output));
fs_write_stream_abort(file->super, &file->super_output);
ret = -1;
} else {
i_assert(i_stream_is_eof(input));
ret = fs_write_stream_finish(file->super, &file->super_output);
}
i_stream_unref(&input);
return ret;
}
static int
fs_metawrap_lock(struct fs_file *_file, unsigned int secs, struct fs_lock **lock_r)
{
struct metawrap_fs_file *file = (struct metawrap_fs_file *)_file;
return fs_lock(file->super, secs, lock_r);
}
static void fs_metawrap_unlock(struct fs_lock *_lock ATTR_UNUSED)
{
i_unreached();
}
static int fs_metawrap_exists(struct fs_file *_file)
{
struct metawrap_fs_file *file = (struct metawrap_fs_file *)_file;
return fs_exists(file->super);
}
static int fs_metawrap_stat(struct fs_file *_file, struct stat *st_r)
{
struct metawrap_fs_file *file = (struct metawrap_fs_file *)_file;
struct istream *input;
uoff_t input_size;
ssize_t ret;
if (!file->fs->wrap_metadata)
return fs_stat(file->super, st_r);
if (file->metadata_write_size != 0) {
/* fs_stat() after a write. we can do this quickly. */
if (fs_stat(file->super, st_r) < 0)
return -1;
if ((uoff_t)st_r->st_size < file->metadata_write_size) {
fs_set_error(_file->fs,
"Just-written %s shrank unexpectedly "
"(%"PRIuUOFF_T" < %"PRIuUOFF_T")",
fs_file_path(_file), st_r->st_size,
file->metadata_write_size);
return -1;
}
st_r->st_size -= file->metadata_write_size;
return 0;
}
if (file->input == NULL)
input = fs_read_stream(_file, IO_BLOCK_SIZE);
else {
input = file->input;
i_stream_ref(input);
}
if ((ret = i_stream_get_size(input, TRUE, &input_size)) < 0) {
fs_set_error(_file->fs, "i_stream_get_size(%s) failed: %s",
fs_file_path(_file), i_stream_get_error(input));
i_stream_unref(&input);
return -1;
}
i_stream_unref(&input);
if (ret == 0) {
/* we shouldn't get here */
fs_set_error(_file->fs, "i_stream_get_size(%s) returned size as unknown",
fs_file_path(_file));
errno = EIO;
return -1;
}
if (fs_stat(file->super, st_r) < 0) {
i_assert(errno != EAGAIN); /* read should have caught this */
return -1;
}
st_r->st_size = input_size;
return 0;
}
static int fs_metawrap_copy(struct fs_file *_src, struct fs_file *_dest)
{
struct metawrap_fs_file *src = (struct metawrap_fs_file *)_src;
struct metawrap_fs_file *dest = (struct metawrap_fs_file *)_dest;
if (!dest->fs->wrap_metadata || !_dest->metadata_changed) {
if (_src != NULL)
return fs_copy(src->super, dest->super);
else
return fs_copy_finish_async(dest->super);
}
return fs_default_copy(_src, _dest);
}
static int fs_metawrap_rename(struct fs_file *_src, struct fs_file *_dest)
{
struct metawrap_fs_file *src = (struct metawrap_fs_file *)_src;
struct metawrap_fs_file *dest = (struct metawrap_fs_file *)_dest;
return fs_rename(src->super, dest->super);
}
static int fs_metawrap_delete(struct fs_file *_file)
{
struct metawrap_fs_file *file = (struct metawrap_fs_file *)_file;
return fs_delete(file->super);
}
static struct fs_iter *
fs_metawrap_iter_init(struct fs *_fs, const char *path,
enum fs_iter_flags flags)
{
struct metawrap_fs_iter *iter;
iter = i_new(struct metawrap_fs_iter, 1);
iter->iter.fs = _fs;
iter->iter.flags = flags;
iter->super = fs_iter_init(_fs->parent, path, flags);
return &iter->iter;
}
static const char *fs_metawrap_iter_next(struct fs_iter *_iter)
{
struct metawrap_fs_iter *iter = (struct metawrap_fs_iter *)_iter;
const char *fname;
iter->super->async_callback = _iter->async_callback;
iter->super->async_context = _iter->async_context;
fname = fs_iter_next(iter->super);
_iter->async_have_more = iter->super->async_have_more;
return fname;
}
static int fs_metawrap_iter_deinit(struct fs_iter *_iter)
{
struct metawrap_fs_iter *iter = (struct metawrap_fs_iter *)_iter;
int ret;
ret = fs_iter_deinit(&iter->super);
i_free(iter);
return ret;
}
const struct fs fs_class_metawrap = {
.name = "metawrap",
.v = {
fs_metawrap_alloc,
fs_metawrap_init,
fs_metawrap_deinit,
fs_metawrap_get_properties,
fs_metawrap_file_init,
fs_metawrap_file_deinit,
fs_metawrap_file_close,
fs_metawrap_file_get_path,
fs_metawrap_set_async_callback,
fs_metawrap_wait_async,
fs_metawrap_set_metadata,
fs_metawrap_get_metadata,
fs_metawrap_prefetch,
fs_metawrap_read,
fs_metawrap_read_stream,
fs_metawrap_write,
fs_metawrap_write_stream,
fs_metawrap_write_stream_finish,
fs_metawrap_lock,
fs_metawrap_unlock,
fs_metawrap_exists,
fs_metawrap_stat,
fs_metawrap_copy,
fs_metawrap_rename,
fs_metawrap_delete,
fs_metawrap_iter_init,
fs_metawrap_iter_next,
fs_metawrap_iter_deinit
}
};