-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy paths3backup.c
1353 lines (1158 loc) · 38.6 KB
/
s3backup.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
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* file: s3backup.c
* Peter Desnoyers, 2020
*/
#define _GNU_SOURCE /* O_PATH etc. */
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <dirent.h>
#include <fcntl.h>
#include <sys/types.h>
#include <sys/uio.h>
#include <sys/stat.h>
#include <assert.h>
#include <stdint.h>
#include <argp.h>
#include <sys/time.h>
#include <libs3.h>
#include <limits.h>
#include <uuid/uuid.h>
#include <avl.h>
#include "s3fs.h"
char zeros[512];
int fds[64];
char *filenames[64];
int me;
/* --------- argument parsing ---------- */
static char args_doc[] = "OBJECT DIR";
// TODO: exclude by pattern
// TODO: read config file
static struct argp_option options[] = {
{"bucket", 'b', "NAME", 0, "bucket for all objects"},
{"incremental", 'i', "NAME", 0, "incremental backup"},
{"protocol", 'p', "http/https", 0, "S3 protocol"},
{"tag", 't', "NAME", 0, "tag for root entry"},
{"local", 'l', 0, 0, "in/out to files (not S3)"},
{"verbose", 'v', 0, 0, "verbose messages"},
{"max", 'm', "SIZE", 0, "stop writing after SIZE (K/M/G)"},
{"hostname", 'h', "HOST", 0, "S3 hostname"},
{"access-key", 'a', "KEY", 0, "S3 access key"},
{"secret-key", 's', "KEY", 0, "S3 secret key"},
{"noio", 'n', 0, 0, "no output (test only)"},
{"exclude", 'e', "FILE", 0, "exclude file"},
{ 0 }
};
struct state {
/* ---- */
int n;
char **names;
char *__me;
int me;
int verbose;
int noio;
/* -- LibS3 stuff -- */
S3BucketContext bkt_ctx;
S3PutProperties put_prop;
int retries; /* retry/backoff on S3 failure */
int sleep;
S3Status status; /* from last operation */
char *msg; /* on error */
off_t content_length; /* for HEAD */
char *upload_id; /* for multipart upload */
int part; /* currentmultipart part # */
char *buf; /* data buffer */
int bufsiz; /* size of buffer */
int len; /* amount of data in it */
int offset; /* data already transmitted */
off_t total; /* for progress messages */
double start; /* for timing */
char *xml; /* for CompleteMultipartUpload */
char *etags[10000];
int xml_len;
int xml_offset;
void *recv_buf;
int recv_wanted;
int recv_got;
/* -- argp stuff -- */
char *bucket;
char *new_name;
FILE *out_fp;
char *old_name;
int in_fd;
char *dir;
int local;
char *exclude[16];
char *hostname;
int protocol;
char *access_key;
char *secret_key;
char *tag;
int incremental;
off_t stopafter; /* in sectors */
/* here's where we accumulate directory information
*/
struct s3dirloc *dir_locs;
int n_dirs;
int max_dirs;
int tmpdir_fd;
};
off_t parseint(char *s)
{
off_t val = strtol(s, &s, 0);
if (toupper(*s) == 'G')
val *= (1024*1024*1024);
if (toupper(*s) == 'M')
val *= (1024*1024);
if (toupper(*s) == 'K')
val *= 1024;
return val;
}
static error_t parse_opt(int key, char *arg, struct argp_state *state)
{
struct state *s = state->input;
switch (key) {
case ARGP_KEY_INIT:
memset(s, 0, sizeof(*s));
s->tag = "--root--";
s->protocol = S3ProtocolHTTPS;
s->access_key = getenv("S3_ACCESS_KEY_ID");
s->secret_key = getenv("S3_SECRET_ACCESS_KEY");
s->hostname = getenv("S3_HOSTNAME");
s->names = &s->__me;
s->stopafter = 1ULL << 50;
break;
case 'm':
s->stopafter = parseint(arg) / 512;
break;
case 'n':
s->noio = 1;
break;
case 'v':
s->verbose = 1;
break;
case 'b':
s->bucket = arg;
break;
case 'i':
s->old_name = arg;
s->incremental = 1;
break;
case 'h':
s->hostname = arg;
break;
case 'p':
if (!strcmp(arg, "http"))
s->protocol = S3ProtocolHTTP;
else if (!strcmp(arg, "https"))
s->protocol = S3ProtocolHTTPS;
else {
fprintf(stderr, "Illegal S3 protocol: %s\n", arg);
argp_usage(state);
}
break;
case 'a':
s->access_key = arg;
break;
case 's':
s->secret_key = arg;
break;
case 't':
s->tag = arg;
break;
case 'l':
s->local = 1;
break;
case 'e':
for (int i = 0; i < 16; i++) {
if (s->exclude[i] == NULL) {
s->exclude[i] = arg;
break;
}
}
break;
case ARGP_KEY_ARG:
if (state->arg_num == 0)
s->new_name = arg;
else if (state->arg_num == 1)
s->dir = arg;
else
argp_usage(state);
break;
case ARGP_KEY_END:
if (s->local) {
if ((s->out_fp = fopen(s->new_name, "wb")) == NULL) {
fprintf(stderr, "Failed to open %s: %s\n", s->new_name, strerror(errno));
exit(1);
}
if ((s->in_fd = open(s->old_name, O_RDONLY)) < 0) {
fprintf(stderr, "Failed to open %s: %s\n", s->old_name, strerror(errno));
exit(1);
}
}
break;
}
return 0;
}
static struct argp argp = { options, parse_opt, NULL, args_doc};
/* ---------------- caching directories ---------------------------*/
struct dir {
union s3offset off;
size_t nbytes;
void *data;
};
avl_tree_t *dir_cache;
int dir_cmp(const void *_d1, const void *_d2)
{
const struct dir *d1 = _d1, *d2 = _d2;
int64_t diff = (int64_t)d1->off.n - (int64_t)d2->off.n;
return (diff == 0) ? 0 : ((diff < 0) ? -1 : 1);
}
void dir_free(void *d)
{
free(d);
}
void cache_dir(union s3offset off, size_t nbytes, void *data)
{
if (nbytes == 0)
return;
struct dir *d = malloc(sizeof(*d));
d->off = off;
d->nbytes = nbytes;
d->data = data;
avl_node_t *node = avl_search(dir_cache, d);
assert(node == NULL);
node = avl_insert(dir_cache, d);
}
struct dir *get_dir(union s3offset off, size_t nbytes)
{
if (nbytes == 0)
return NULL;
struct dir _d = {.off = off};
avl_node_t *node = avl_search(dir_cache, &_d);
if (node == NULL)
return NULL;
struct dir *d = node->item;
return d->data;
}
/* ---------------- now we have the S3 read/write stuff ---------- */
static void print_error(struct state *s)
{
if (s->status < S3StatusErrorAccessDenied) {
fprintf(stderr, "\nERROR: %s\n", S3_get_status_name(s->status));
}
else {
fprintf(stderr, "\nERROR: %s\n", S3_get_status_name(s->status));
fprintf(stderr, "%s\n", s->msg);
}
}
static void response_complete(S3Status status,
const S3ErrorDetails *error,
void *data)
{
struct state *s = data;
s->status = status;
if (error != NULL) {
char *template = "Message: %s\n"
"Resource: %s\n"
"Further Details: %s\n";
const char *m = error->message ? error->message : "";
const char *r = error->resource ? error->resource : "";
const char *f = error->furtherDetails ? error->furtherDetails : "";
if (s->msg != NULL)
free(s->msg);
int len = strlen(template) + strlen(m) + strlen(r) + strlen(f);
s->msg = malloc(len+20);
sprintf(s->msg, template, m, r, f);
}
}
int should_retry(struct state *s)
{
if (s->retries--) {
printf("retrying...\n");
sleep(s->sleep);
s->sleep++;
return 1;
}
return 0;
}
S3Status response_properties(const S3ResponseProperties *p, void *data)
{
struct state *s = data;
s->content_length = p->contentLength;
return S3StatusOK;
}
double gettime(void)
{
struct timeval tv;
gettimeofday(&tv, NULL);
return tv.tv_sec + tv.tv_usec/1.0e6;
}
S3Status response_properties_etag(const S3ResponseProperties *p, void *data)
{
struct state *s = data;
double now = gettime();
double gb = s->total / (1024.0*1024*1024);
double mb_s = s->total / (1e6 * (now - s->start));
s->etags[s->part] = strdup(p->eTag); /* note - already quoted */
printf("%llu bytes uploaded (%.3f GiB, %.3f MB/s)\n",
(unsigned long long)s->total, gb, mb_s);
return S3StatusOK;
}
int part_data_callback(int size, char *buf, void *data)
{
struct state *s = data;
int len = s->len - s->offset;
if (size < len)
len = size;
memcpy(buf, s->buf + s->offset, len);
s->offset += len;
s->total += len;
return len;
}
void upload_part(struct state *s)
{
S3PutObjectHandler h = {
.responseHandler.propertiesCallback = response_properties_etag,
.responseHandler.completeCallback = response_complete,
.putObjectDataCallback = part_data_callback
};
s->part++;
do {
S3_upload_part(&s->bkt_ctx,
s->new_name,
&s->put_prop,
&h,
s->part,
s->upload_id,
s->len,
NULL, /* requestContext */
0, /* timeout (ms) */
s);
} while (S3_status_is_retryable(s->status) && should_retry(s));
if (s->status != S3StatusOK) {
print_error(s);
exit(1);
}
if (s->bufsiz < 500 * 1024 * 1024) {
s->bufsiz = s->bufsiz * 2;
s->buf = realloc(s->buf, s->bufsiz);
}
s->len = s->offset = 0;
}
S3Status multipart_init_response(const char *upload_id, void *data)
{
struct state *s = data;
s->upload_id = strdup(upload_id);
return S3StatusOK;
}
void put_init(struct state *s)
{
char *key = s->new_name;
s->bufsiz = 5 * 1024 * 1024;
s->buf = malloc(s->bufsiz);
s->part = 0;
S3MultipartInitialHandler init_handler = {
.responseHandler.propertiesCallback = response_properties,
.responseHandler.completeCallback = response_complete,
.responseXmlCallback = multipart_init_response};
do {
S3_initiate_multipart(&s->bkt_ctx,
key,
NULL, /* putProperties */
&init_handler,
0, /* requestContext */
0, /* timeout (ms) */
s);
} while (S3_status_is_retryable(s->status) && should_retry(s));
if (s->upload_id == 0 || s->status != S3StatusOK) {
print_error(s);
exit(1);
}
}
void put_write(struct state *s, char *buf, int len)
{
static int init_done;
if (!init_done) {
put_init(s);
init_done = 1;
}
if (s->len + len >= s->bufsiz) {
int bytes = s->bufsiz - s->len;
memcpy(s->buf + s->len, buf, bytes);
s->len += bytes;
upload_part(s);
buf += bytes;
len -= bytes;
}
memcpy(s->buf + s->len, buf, len);
s->len += len;
}
S3Status recv_data_callback(int size, const char *buf, void *data)
{
struct state *s = data;
/* don't overrun the buffer - should never happen
*/
if (size + s->recv_got > s->recv_wanted)
return S3StatusAbortedByCallback;
memcpy(s->recv_buf + s->recv_got, buf, size);
s->recv_got += size;
return S3StatusOK;
}
int s3_get_range(struct state *s, char *key,
char *buf, uint64_t len, uint64_t offset)
{
S3GetObjectHandler h = {
.responseHandler.propertiesCallback = response_properties,
.responseHandler.completeCallback = response_complete,
.getObjectDataCallback = recv_data_callback
};
s->recv_buf = buf;
s->recv_wanted = len;
s->recv_got = 0;
do {
S3_get_object(&s->bkt_ctx,
key,
NULL, /* no conditions */
offset,
len,
0, /* requestContext */
0, /* timeoutMs */
&h,
s);
} while (S3_status_is_retryable(s->status) && should_retry(s));
if (s->status != S3StatusOK) {
print_error(s);
exit(1);
}
return len;
}
int s3_init(struct state *s)
{
S3Status status = S3_initialize("Multi-Version Backup V0.1",
S3_INIT_ALL, s->hostname);
if (status != S3StatusOK) {
fprintf(stderr, "Failed to initialize libs3: %s\n",
S3_get_status_name(status));
exit(-1);
}
s->retries = 5;
s->sleep = 1;
s->bkt_ctx = (S3BucketContext){ s->hostname,
s->bucket,
s->protocol,
S3UriStylePath,
s->access_key,
s->secret_key,
0, /* security token */
0 }; /* authRegion */
s->put_prop = (S3PutProperties) { NULL, /* binary/octet-stream */
NULL, /* MD5 */
NULL, /* cache control */
NULL, /* content disposition */
NULL, /* content encoding */
-1, /* expires (never) */
S3CannedAclPrivate,
0, /* metaproperties count */
NULL, /* metaproperty list */
0}; /* use server encryption */
}
static int xml_data_callback(int size, char *buf, void *data)
{
struct state *s = data;
int remaining = s->xml_len - s->xml_offset;
int bytes = (size < remaining) ? size : remaining;
memcpy(buf, s->xml + s->xml_offset, bytes);
s->xml_offset += bytes;
return bytes;
}
int s3_finalize(struct state *s)
{
if (s->len)
upload_part(s); /* write the last part */
S3MultipartCommitHandler h = {
.responseHandler.propertiesCallback = response_properties,
.responseHandler.completeCallback = response_complete,
.putObjectDataCallback = xml_data_callback,
.responseXmlCallback = NULL
};
/* entry for each part looks like this:
* "<Part><ETag>E</ETag><PartNumber>#</PartNumber></Part>\n"
* or 52 characters + etag + part#
* plus another 64 or so for <<CompleteMultipartUpload> ...
*/
int etag_total = 0;
for (int i = 0; i <= s->part; i++)
if (s->etags[i])
etag_total += strlen(s->etags[i]);
int total = 150 + etag_total + (4 + 52) * s->part;
char *msg = malloc(total), *ptr = msg;
/* we seem to need all of this to get Minio to work...
*/
ptr += sprintf(ptr, "<?xml version=\"1.0\" encoding=\"UTF-8\"?>"
"<CompleteMultipartUpload "
"xmlns=\"http://s3.amazonaws.com/doc/2006-03-01/\">");
for (int i = 0; i <= s->part; i++)
if (s->etags[i])
ptr += sprintf(ptr, "<Part><ETag>%s</ETag>"
"<PartNumber>%d</PartNumber></Part>\n",
s->etags[i], i);
ptr += sprintf(ptr, "</CompleteMultipartUpload>");
s->xml = msg;
s->xml_len = strlen(msg);
s->xml_offset = 0;
do {
S3_complete_multipart_upload(&s->bkt_ctx,
s->new_name,
&h,
s->upload_id,
s->xml_len,
NULL, /* requestContext */
0, /* timeout (ms) */
s);
} while (S3_status_is_retryable(s->status) && should_retry(s));
if (s->status != S3StatusOK) {
print_error(s);
exit(1);
}
}
/* ------------ read local files (debug) -------------- */
/* Hack. Will crash w/ over 128 filenames
*/
struct name_fd {
char *name;
int fd;
} fd_cache[128];
int get_fd(struct state *s, char *file)
{
int i;
if (fd_cache[0].name == NULL) { /* horrible hack */
fd_cache[0].name = s->old_name;
fd_cache[0].fd = s->in_fd;
}
for (i = 0; fd_cache[i].name != NULL; i++)
if (!strcmp(file, fd_cache[i].name))
return (fd_cache[i].fd);
fd_cache[i].name = strdup(file);
if ((fd_cache[i].fd = open(file, O_RDONLY)) < 0) {
fprintf(stderr, "Can't open %s : %s\n", file, strerror(errno));
exit(1);
}
return fd_cache[i].fd;
}
/* ------- functions for file system code to use ------- */
/* ------- write to / read from S3 or file as necessary ------ */
/* ------- exit on all errors --------*/
off_t total_written;
void do_write(struct state *s, void *ptr, off_t len)
{
assert(len >= 0);
total_written += len;
if (len == 0 || s->noio)
return;
if (s->local) {
if (len > 0 && fwrite(ptr, len, 1, s->out_fp) < 1) {
fprintf(stderr, "Write %s: %s\n", s->new_name, strerror(errno));
exit(1);
}
}
else
put_write(s, ptr, len);
}
void do_read(struct state *s, char *key, void *ptr, off_t len, off_t offset)
{
if (s->local) {
int fd = get_fd(s, key);
if (pread(fd, ptr, len, offset) < 0) {
fprintf(stderr, "Error reading %s : %s\n", key, strerror(errno));
exit(1);
}
}
else {
s3_get_range(s, key, ptr, len, offset);
return;
}
}
off_t do_size(struct state *s, char *key)
{
if (s->local) {
struct stat sb;
if (stat(key, &sb) < 0) {
fprintf(stderr, "can't access %s : %s\n", key, strerror(errno));
exit(1);
}
return sb.st_size;
}
else {
s->content_length = 0;
S3ResponseHandler h = {.propertiesCallback = response_properties,
.completeCallback = response_complete};
do {
S3_head_object(&s->bkt_ctx,
key,
NULL, /* RequestContext */
0, /* timeout */
&h,
s);
} while (S3_status_is_retryable(s->status) && should_retry(s));
return s->content_length;
}
}
void do_done(struct state *s)
{
if (!s->local)
s3_finalize(s);
}
/* -------------- next we have the filesystem code itself ------------- */
off_t round_up(off_t a, off_t b)
{
return b * ((a + b - 1) / b);
}
/* kind of a kludge, but it gives us reasonable error messages.
*/
char _path[PATH_MAX];
char *path; /* this is really a kludge */
/* another kludge - accumulate file system statistics here
*/
struct s3statfs stats;
/*
* offset - current offset (in sectors) in output
* fd - descriptor of link (opened with O_PATH | O_NOFOLLOW)
* _d3e - place to put dirent for this object
* _d3e_p - returns pointer to next dirent after we store this one
* name - name of this entry
* sb - stat results for fd
*/
int store_link(struct state *s, off_t offset, int fd, struct s3dirent *_d3e,
struct s3dirent **_d3e_p, char *name, struct stat *sb)
{
char buf[PATH_MAX];
int nbytes;
off_t _off0 = offset;
/* man 2 readlinkat:
* "Since Linux 2.6.39, pathname can be an empty string, in which case
* the call operates on the symbolic link referred to by dirfd (which
* should have been obtained using open(2) with the O_PATH and
* O_NOFOLLOW flags)."
*/
memset(buf, 0, sizeof(buf));
if ((nbytes = readlinkat(fd, "", buf, sizeof(buf))) < 0) {
fprintf(stderr, "readlink error %s/%s : %s\n", path, name, strerror(errno));
exit(1);
}
int n = round_up(nbytes, 512);
do_write(s, buf, n);
*_d3e = (struct s3dirent){.mode = sb->st_mode, .uid = sb->st_uid,
.gid = sb->st_gid,
.ctime = sb->st_ctime,
#ifdef S3_USE_INUM
.ino = sb->st_ino,
#endif
.off = {.s.sector = offset, .s.object = s->me},
.bytes = nbytes};
strcpy(_d3e->name, name);
_d3e->namelen = strlen(name);
if (_d3e_p)
*_d3e_p = next_de(_d3e);
stats.symlinks++;
stats.sym_sectors += n/512;
assert((total_written & 511) == 0);
offset += n/512;
assert(offset*512 == total_written);
if (s->verbose)
printf("L %lld %lld %s/%s\n", (long long)(offset-_off0), (long long)sb->st_size,
path, name);
return offset;
}
/*
* offset - current offset (in sectors) in output
* fd - descriptor of current object (file / directory / ...)
* _d3e - place to put dirent for this object
* _d3e_p - returns pointer to next dirent after we store this one
* name - name of this entry
* sb - stat results for fd
*/
int store_file(struct state *s, off_t offset, int fd, struct s3dirent *_d3e,
struct s3dirent **_d3e_p, char *name, struct stat *sb)
{
off_t nbytes = 0, _off0 = offset;
int len;
char buf[16*1024];
if (s->noio)
nbytes = sb->st_size;
else while ((len = read(fd, buf, sizeof(buf))) > 0) {
do_write(s, buf, len);
nbytes += len;
}
off_t total = round_up(nbytes, 512);
do_write(s, zeros, total-nbytes);
assert((total_written & 511) == 0);
*_d3e = (struct s3dirent){.mode = sb->st_mode, .uid = sb->st_uid,
.gid = sb->st_gid,
.ctime = sb->st_ctime,
#ifdef S3_USE_INUM
.ino = sb->st_ino,
#endif
.off = {.s.sector = offset, .s.object = s->me},
.bytes = nbytes};
strcpy(_d3e->name, name);
_d3e->namelen = strlen(name);
if (_d3e_p)
*_d3e_p = next_de(_d3e);
stats.files++;
stats.file_sectors += total/512;
offset += (total / 512);
assert(offset*512 == total_written);
if (s->verbose)
printf("F %lld %lld %s/%s\n", (long long)(offset-_off0), (long long)sb->st_size,
path, name);
return offset;
}
/* Write data to a file. Used for storing metadata after directory
* traversal is finished.
* TODO: can get rid of this if we stash dir locations in a temp file
*/
int store_data(struct state *s, off_t offset, struct s3dirent *_d3e,
struct s3dirent **_d3e_p, char *name, void *data, size_t nbytes)
{
off_t _off0 = offset;
if (!s->noio)
do_write(s, data, nbytes);
off_t total = round_up(nbytes, 512);
if (!s->noio)
do_write(s, zeros, total-nbytes);
assert((total_written & 511) == 0);
*_d3e = (struct s3dirent){.mode = 0, .uid = 0, .gid = 0, .ctime = 0,
#ifdef S3_USE_INUM
.ino = 0,
#endif
.off = {.s.sector = offset, .s.object = s->me},
.bytes = nbytes};
strcpy(_d3e->name, name);
_d3e->namelen = strlen(name);
if (_d3e_p)
*_d3e_p = next_de(_d3e);
stats.files++;
stats.file_sectors += total/512;
offset += (total / 512);
assert(offset*512 == total_written);
if (s->verbose)
printf("d %lld %lld %s/%s\n", (long long)(offset-_off0),
(long long)nbytes, path, name);
return offset;
}
struct s3dirent *lookup(void *start, int dirlen, char *name)
{
if (start == NULL || dirlen == 0)
return NULL;
int len = strlen(name);
for (struct s3dirent *_de = start; (void*)_de - start < dirlen; ) {
if (_de->namelen == len && !memcmp(_de->name, name, len))
return _de;
_de = next_de(_de);
}
return NULL;
}
int unchanged(struct s3dirent *old, struct stat *sb)
{
return (old != NULL) &&
old->mode == sb->st_mode &&
old->bytes == sb->st_size &&
old->ctime == sb->st_ctime &&
#ifdef S3_USE_INUM
old->ino == sb->st_ino &&
#endif
old->uid == sb->st_uid &&
old->gid == sb->st_gid;
}
/* very much a kludge - stash directory locations.
* TODO: much cleaner to use a temp file like dir data
*/
void dirstash_init(struct state *s)
{
s->max_dirs = 1000;
s->dir_locs = malloc(s->max_dirs * sizeof(*(s->dir_locs)));
s->n_dirs = 0;
}
void dirstash_loc(struct state *s, union s3offset off, size_t len)
{
if (s->n_dirs >= s->max_dirs) {
s->max_dirs *= 2;
s->dir_locs = realloc(s->dir_locs, s->max_dirs * sizeof(*(s->dir_locs)));
}
s->dir_locs[s->n_dirs++] = (struct s3dirloc){.off = off, .bytes = len};
}
/* device node or empty directory. no contents
*/
int store_node(struct state *s, struct s3dirent *_d3e, struct s3dirent **_d3e_p,
char *name, struct stat *sb, struct s3dirent *old_de)
{
*_d3e = (struct s3dirent){.mode = sb->st_mode, .uid = sb->st_uid,
.gid = sb->st_gid,
.ctime = sb->st_ctime,
#ifdef S3_USE_INUM
.ino = sb->st_ino,
#endif
.off = {.n = 0},
.bytes = 0};
/* if it's a device node, abuse de->bytes to hold device number
*/
if (S_ISCHR(sb->st_mode) || S_ISBLK(sb->st_mode))
_d3e->bytes = sb->st_rdev;
strcpy(_d3e->name, name);
_d3e->namelen = strlen(name);
if (_d3e_p)
*_d3e_p = next_de(_d3e);
}
int exclude(struct state *s, char *dir, char *file)
{
char path[strlen(dir)+strlen(file)+10];
sprintf(path, "%s/%s", dir, file);
for (int i = 0; i < 16 && s->exclude[i]; i++)
if (!strcmp(s->exclude[i], path))
return 1;
return 0;
}
/*
* offset - current offset (in sectors) in output
* fd - descriptor of current object (file / directory / ...)
* _d3e - place to put dirent for this object
* _d3e_p - returns pointer to next dirent after we store this one
* sb - stat results for fd
* name - name of this entry
* old_de - dirent for this directory in old version (or NULL)
*/
int store_dir(struct state *s, off_t offset, int fd, struct s3dirent *_d3e,
struct s3dirent **_d3e_p, char *name,
struct stat *sb, struct s3dirent *old_de)
{
int max = 128*1024;
void *orig_d3e = malloc(max);
struct s3dirent *d3e = orig_d3e;
char *p = _path + strlen(_path);
sprintf(p, "/%s", name); /* extend path */
void *start = NULL;
int len = 0;
if (old_de) {
if (old_de->off.s.object >= s->me) { /* #ancestors */
fprintf(stderr, "%s: corrupt ancester index %d\n",
_path, old_de->off.s.object);
exit(1);
}
start = get_dir(old_de->off, old_de->bytes);
len = old_de->bytes;
}
/* to do - keep track if we make it through the directory with everything
* unchanged, and if so store the old entry and return.
* probably want to push that function down for store_file and store_link
*/
DIR *d = fdopendir(fd);
struct dirent *de;
for (de = readdir(d); de != NULL; de = readdir(d)) {
if (strcmp(de->d_name, ".") == 0 ||
strcmp(de->d_name, "..") == 0)
continue;
if (exclude(s, path, de->d_name)) {
fprintf(stderr, "excluding %s/%s\n", path, de->d_name);
continue;
}
/* really big backups can be split into multiple increments
*/
if (offset >= s->stopafter)
break;
/* handle really large directories
*/
int de3_len = sizeof(*d3e) + strlen(de->d_name);