-
Notifications
You must be signed in to change notification settings - Fork 118
/
Copy pathcasync-tool.c
4020 lines (3226 loc) · 150 KB
/
casync-tool.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
/* SPDX-License-Identifier: LGPL-2.1+ */
#include <assert.h>
#include <errno.h>
#include <fcntl.h>
#include <getopt.h>
#include <poll.h>
#include <signal.h>
#include <stdio.h>
#include <stdlib.h>
#include <sys/file.h>
#include <sys/stat.h>
#include <sys/sysmacros.h>
#include <time.h>
#include "caformat-util.h"
#include "caformat.h"
#include "cafuse.h"
#include "caindex.h"
#include "canbd.h"
#include "caprotocol.h"
#include "caremote.h"
#include "castore.h"
#include "casync.h"
#include "compressor.h"
#include "def.h"
#include "gc.h"
#include "notify.h"
#include "parse-util.h"
#include "signal-handler.h"
#include "time-util.h"
#include "util.h"
#if HAVE_UDEV
#include <libudev.h>
#include "udev-util.h"
#endif
static enum arg_what {
WHAT_ARCHIVE,
WHAT_ARCHIVE_INDEX,
WHAT_BLOB,
WHAT_BLOB_INDEX,
WHAT_DIRECTORY,
_WHAT_INVALID = -1,
} arg_what = _WHAT_INVALID;
static int arg_log_level = -1;
static bool arg_verbose = false;
static bool arg_dry_run = false;
static bool arg_exclude_nodump = true;
static bool arg_exclude_submounts = false;
static bool arg_exclude_file = true;
static bool arg_reflink = true;
static bool arg_hardlink = false;
static bool arg_punch_holes = true;
static bool arg_delete = true;
static bool arg_undo_immutable = false;
static bool arg_recursive = true;
static bool arg_seed_output = true;
static char *arg_store = NULL;
static char **arg_extra_stores = NULL;
static char **arg_seeds = NULL;
static char *arg_cache = NULL;
static bool arg_cache_auto = false;
static size_t arg_chunk_size_min = 0;
static size_t arg_chunk_size_avg = 0;
static size_t arg_chunk_size_max = 0;
static uint64_t arg_rate_limit_bps = UINT64_MAX;
static uint64_t arg_with = 0;
static uint64_t arg_without = 0;
static uid_t arg_uid_shift = 0, arg_uid_range = 0x10000U;
static bool arg_uid_shift_apply = false;
static bool arg_mkdir = true;
static CaDigestType arg_digest = CA_DIGEST_DEFAULT;
static CaCompressionType arg_compression = CA_COMPRESSION_DEFAULT;
static void help(void) {
printf("%1$s [OPTIONS...] make [ARCHIVE|ARCHIVE_INDEX|BLOB_INDEX] [PATH]\n"
"%1$s [OPTIONS...] extract [ARCHIVE|ARCHIVE_INDEX|BLOB_INDEX] [PATH]\n"
"%1$s [OPTIONS...] list [ARCHIVE|ARCHIVE_INDEX|DIRECTORY]\n"
"%1$s [OPTIONS...] mtree [ARCHIVE|ARCHIVE_INDEX|DIRECTORY]\n"
"%1$s [OPTIONS...] stat [ARCHIVE|ARCHIVE_INDEX|DIRECTORY] [PATH]\n"
"%1$s [OPTIONS...] digest [ARCHIVE|BLOB|ARCHIVE_INDEX|BLOB_INDEX|DIRECTORY]\n"
#if HAVE_FUSE
"%1$s [OPTIONS...] mount [ARCHIVE|ARCHIVE_INDEX] PATH\n"
#endif
"%1$s [OPTIONS...] mkdev [BLOB|BLOB_INDEX] [NODE]\n"
"%1$s [OPTIONS...] gc BLOB_INDEX|ARCHIVE_INDEX...\n"
"\n"
"Content-Addressable Data Synchronization Tool\n\n"
" -h --help Show this help\n"
" --version Show brief version information\n"
" -l --log-level=LEVEL Set log level (debug, info, err)\n"
" -v --verbose Show terse status information during runtime\n"
" -n --dry-run When garbage collecting, only print what would\n"
" be done\n"
" --store=PATH The primary chunk store to use\n"
" --extra-store=PATH Additional chunk store to look for chunks in\n"
" --chunk-size=[MIN:]AVG[:MAX]\n"
" The minimal/average/maximum number of bytes in a\n"
" chunk\n"
" --digest=DIGEST Pick digest algorithm (sha512-256 or sha256)\n"
" --compression=COMPRESSION\n"
" Pick compression algorithm (zstd, xz or gzip)\n"
" --seed=PATH Additional file or directory to use as seed\n"
" --cache=PATH Directory to use as encoder cache\n"
" -c --cache-auto Pick encoder cache directory automatically\n"
" --rate-limit-bps=LIMIT Maximum bandwidth in bytes/s for remote\n"
" communication\n"
" --exclude-nodump=no Don't exclude files with chattr(1)'s +d 'nodump'\n"
" flag when creating archive\n"
" --exclude-submounts=yes Exclude submounts when creating archive\n"
" --exclude-file=no Don't respect .caexclude files in file tree\n"
" --reflink=no Don't create reflinks from seeds when extracting\n"
" --hardlink=yes Create hardlinks from seeds when extracting\n"
" --punch-holes=no Don't create sparse files when extracting\n"
" --delete=no Don't delete existing files not listed in archive\n"
" after extraction\n"
" --undo-immutable=yes When removing existing files, undo chattr(1)'s +i\n"
" 'immutable' flag when extracting\n"
" --seed-output=no Don't implicitly add pre-existing output as seed\n"
" when extracting\n"
" --recursive=no List non-recursively\n"
#if HAVE_FUSE
" --mkdir=no Don't automatically create mount directory if it\n"
" is missing\n"
#endif
" --uid-shift=yes|SHIFT Shift UIDs/GIDs\n"
" --uid-range=RANGE Restrict UIDs/GIDs to range\n\n"
"Input/output selector:\n"
" --what=archive Operate on archive file\n"
" --what=archive-index Operate on archive index file\n"
" --what=blob Operate on blob file\n"
" --what=blob-index Operate on blob index file\n"
" --what=directory Operate on directory\n\n"
" --what=help Print allowed values\n\n"
"Archive feature sets:\n"
" --with=best Store most accurate information\n"
" --with=unix Store UNIX baseline information\n"
" --with=fat Store FAT information\n"
" --with=chattr Store chattr(1) file attributes\n"
" --with=fat-attrs Store FAT file attributes\n"
" --with=privileged Store file data that requires privileges to\n"
" restore\n"
" --with=fuse Store file data that can exposed again via\n"
" 'casync mount'\n"
" (and similar: --without=fat-attrs, --without=privileged, ...)\n"
" --without=all Disable all optional attributes\n\n"
"Individual archive features:\n"
" --with=16bit-uids Store reduced 16bit UID/GID information\n"
" --with=32bit-uids Store full 32bit UID/GID information\n"
" --with=user-names Store user and group names\n"
" --with=sec-time Store timestamps with 1s granularity\n"
" --with=usec-time Store timestamps with 1µs granularity\n"
" --with=nsec-time Store timestamps with 1ns granularity\n"
" --with=2sec-time Store timestamps with 2s granularity\n"
" --with=read-only Store per-file read only flag\n"
" --with=permissions Store full per-file UNIX permissions\n"
" --with=symlinks Store symbolic links\n"
" --with=device-nodes Store block and character device nodes\n"
" --with=fifos Store named pipe nodes\n"
" --with=sockets Store AF_UNIX file system socket nodes\n"
" --with=flag-hidden Store FAT \"hidden\" file flag\n"
" --with=flag-system Store FAT \"system\" file flag\n"
" --with=flag-archive Store FAT \"archive\" file flag\n"
" --with=flag-append Store \"append-only\" file flag\n"
" --with=flag-noatime Store \"disable access time\" file flag\n"
" --with=flag-compr Store \"enable compression\" file flag\n"
" --with=flag-nocow Store \"disable copy-on-write\" file flag\n"
" --with=flag-nodump Store \"disable dumping\" file flag\n"
" --with=flag-dirsync Store \"synchronous\" directory flag\n"
" --with=flag-immutable Store \"immutable\" file flag\n"
" --with=flag-sync Store \"synchronous\" file flag\n"
" --with=flag-nocomp Store \"disable compression\" file flag\n"
" --with=flag-projinherit Store \"project quota inheritance\" flag\n"
" --with=subvolume Store btrfs subvolume information\n"
" --with=subvolume-ro Store btrfs subvolume read-only property\n"
" --with=xattrs Store extended file attributes\n"
" --with=acl Store file access control lists\n"
" --with=selinux Store SELinux file labels\n"
" --with=fcaps Store file capabilities\n"
" --with=quota-projid Store ext4/XFS quota project ID\n"
" (and similar: --without=16bit-uids, --without=32bit-uids, ...)\n",
program_invocation_short_name);
}
static void version(void) {
printf("%s " PACKAGE_VERSION "\n",
program_invocation_short_name);
}
static int parse_chunk_sizes(const char *v, size_t *ret_min, size_t *ret_avg, size_t *ret_max) {
uint64_t a, b, c;
char *k;
int r;
assert(v);
assert(ret_min);
assert(ret_max);
if (streq(v, "auto")) {
*ret_min = 0;
*ret_avg = 0;
*ret_max = 0;
return 0;
}
k = strchr(v, ':');
if (k) {
char *j, *p;
j = strchr(k+1, ':');
if (!j) {
log_error("--chunk-size= requires either a single average chunk size or a triplet of minimum, average and maximum chunk size.");
return -EINVAL;
}
p = strndupa(v, k - v);
r = parse_size(p, &a);
if (r < 0)
return log_error_errno(r, "Can't parse minimum chunk size: %s", v);
if (a < CA_CHUNK_SIZE_LIMIT_MIN) {
log_error("Minimum chunk size must be >= %zu.", CA_CHUNK_SIZE_LIMIT_MIN);
return -ERANGE;
}
p = strndupa(k + 1, j - k - 1);
r = parse_size(p, &b);
if (r < 0)
return log_error_errno(r, "Can't parse average chunk size: %s", v);
if (b < a) {
log_error("Average chunk size must be larger than minimum chunk size.");
return -EINVAL;
}
r = parse_size(j + 1, &c);
if (r < 0)
return log_error_errno(r, "Can't parse maximum chunk size: %s", v);
if (c < b) {
log_error("Average chunk size must be smaller than maximum chunk size.");
return -EINVAL;
}
if (c > CA_CHUNK_SIZE_LIMIT_MAX) {
log_error("Maximum chunk size must be <= %zu.", CA_CHUNK_SIZE_LIMIT_MAX);
return -ERANGE;
}
} else {
r = parse_size(v, &b);
if (r < 0)
return log_error_errno(r, "Can't parse average chunk size: %s", v);
if (b < CA_CHUNK_SIZE_LIMIT_MIN) {
log_error("Average chunk size must be >= %zu.", CA_CHUNK_SIZE_LIMIT_MIN);
return -ERANGE;
}
if (b > CA_CHUNK_SIZE_LIMIT_MAX) {
log_error("Average chunk size must be <= %zu.", CA_CHUNK_SIZE_LIMIT_MAX);
return -ERANGE;
}
a = 0;
c = 0;
}
*ret_min = a;
*ret_avg = b;
*ret_max = c;
return 0;
}
static int parse_what_selector(const char *arg, enum arg_what *what) {
if (streq(arg, "archive"))
*what = WHAT_ARCHIVE;
else if (streq(arg, "archive-index"))
*what = WHAT_ARCHIVE_INDEX;
else if (streq(arg, "blob"))
*what = WHAT_BLOB;
else if (streq(arg, "blob-index"))
*what = WHAT_BLOB_INDEX;
else if (streq(arg, "directory"))
*what = WHAT_DIRECTORY;
else if (streq(arg, "help")) {
printf("Allowed --what= selectors:\n"
"archive-index\n"
"blob\n"
"blob-index\n"
"directory\n");
return 0;
} else {
log_error("Failed to parse --what= selector: %s", arg);
return -EINVAL;
}
return 1;
}
static int dump_with_flags(void) {
uint64_t i;
int r;
puts("Supported --with= and --without= flags:");
for (i = 0; i < sizeof(uint64_t)*8; i++) {
_cleanup_free_ char *s = NULL;
uint64_t flag = UINT64_C(1) << i;
if (!(flag & SUPPORTED_WITH_MASK))
continue;
r = ca_with_feature_flags_format(flag, &s);
if (r < 0)
return log_error_errno(r, "Failed to convert feature flag to string: %m");
puts(s);
}
return 0;
}
static int parse_argv(int argc, char *argv[]) {
enum {
ARG_STORE = 0x100,
ARG_EXTRA_STORE,
ARG_CHUNK_SIZE,
ARG_SEED,
ARG_CACHE,
ARG_RATE_LIMIT_BPS,
ARG_WITH,
ARG_WITHOUT,
ARG_WHAT,
ARG_EXCLUDE_NODUMP,
ARG_EXCLUDE_SUBMOUNTS,
ARG_EXCLUDE_FILE,
ARG_UNDO_IMMUTABLE,
ARG_PUNCH_HOLES,
ARG_REFLINK,
ARG_HARDLINK,
ARG_SEED_OUTPUT,
ARG_DELETE,
ARG_UID_SHIFT,
ARG_UID_RANGE,
ARG_RECURSIVE,
ARG_MKDIR,
ARG_DIGEST,
ARG_COMPRESSION,
ARG_VERSION,
};
static const struct option options[] = {
{ "help", no_argument, NULL, 'h' },
{ "version", no_argument, NULL, ARG_VERSION },
{ "log-level", required_argument, NULL, 'l' },
{ "verbose", no_argument, NULL, 'v' },
{ "dry-run", no_argument, NULL, 'n' },
{ "store", required_argument, NULL, ARG_STORE },
{ "extra-store", required_argument, NULL, ARG_EXTRA_STORE },
{ "chunk-size", required_argument, NULL, ARG_CHUNK_SIZE },
{ "seed", required_argument, NULL, ARG_SEED },
{ "cache", required_argument, NULL, ARG_CACHE },
{ "cache-auto", no_argument, NULL, 'c' },
{ "rate-limit-bps", required_argument, NULL, ARG_RATE_LIMIT_BPS },
{ "with", required_argument, NULL, ARG_WITH },
{ "without", required_argument, NULL, ARG_WITHOUT },
{ "what", required_argument, NULL, ARG_WHAT },
{ "exclude-nodump", required_argument, NULL, ARG_EXCLUDE_NODUMP },
{ "exclude-submounts", required_argument, NULL, ARG_EXCLUDE_SUBMOUNTS },
{ "exclude-file", required_argument, NULL, ARG_EXCLUDE_FILE },
{ "undo-immutable", required_argument, NULL, ARG_UNDO_IMMUTABLE },
{ "delete", required_argument, NULL, ARG_DELETE },
{ "punch-holes", required_argument, NULL, ARG_PUNCH_HOLES },
{ "reflink", required_argument, NULL, ARG_REFLINK },
{ "hardlink", required_argument, NULL, ARG_HARDLINK },
{ "seed-output", required_argument, NULL, ARG_SEED_OUTPUT },
{ "uid-shift", required_argument, NULL, ARG_UID_SHIFT },
{ "uid-range", required_argument, NULL, ARG_UID_RANGE },
{ "recursive", required_argument, NULL, ARG_RECURSIVE },
{ "mkdir", required_argument, NULL, ARG_MKDIR },
{ "digest", required_argument, NULL, ARG_DIGEST },
{ "compression", required_argument, NULL, ARG_COMPRESSION },
{}
};
int c, r;
assert(argc >= 0);
assert(argv);
if (getenv_bool("CASYNC_VERBOSE") > 0)
arg_verbose = true;
while ((c = getopt_long(argc, argv, "hl:vnc", options, NULL)) >= 0) {
switch (c) {
case 'h':
help();
return 0;
case ARG_VERSION:
version();
return 0;
case 'l':
r = set_log_level_from_string(optarg);
if (r < 0)
return log_error_errno(r, "Failed to parse log level \"%s\": %m", optarg);
arg_log_level = r;
break;
case 'v':
arg_verbose = true;
break;
case 'n':
arg_dry_run = true;
break;
case ARG_STORE:
r = free_and_strdup(&arg_store, optarg);
if (r < 0)
return log_oom();
break;
case ARG_EXTRA_STORE:
r = strv_extend(&arg_extra_stores, optarg);
if (r < 0)
return log_oom();
break;
case ARG_CHUNK_SIZE:
r = parse_chunk_sizes(optarg,
&arg_chunk_size_min,
&arg_chunk_size_avg,
&arg_chunk_size_max);
if (r < 0)
return r;
break;
case ARG_SEED:
r = strv_extend(&arg_seeds, optarg);
if (r < 0)
return log_oom();
break;
case ARG_CACHE:
r = free_and_strdup(&arg_cache, optarg);
if (r < 0)
return log_oom();
break;
case 'c':
arg_cache_auto = true;
break;
case ARG_RATE_LIMIT_BPS:
r = parse_size(optarg, &arg_rate_limit_bps);
if (r < 0)
return log_error_errno(r, "Unable to parse rate limit %s: %m", optarg);
if (arg_rate_limit_bps == 0) {
log_error("Rate limit size cannot be zero.");
return -EINVAL;
}
break;
case ARG_WITH: {
uint64_t u;
if (streq(optarg, "help"))
return dump_with_flags();
r = ca_with_feature_flags_parse_one(optarg, &u);
if (r < 0) {
log_error("Failed to parse --with= feature flag: %s", optarg);
return -EINVAL;
}
arg_with |= u;
break;
}
case ARG_WITHOUT: {
uint64_t u;
if (streq(optarg, "help"))
return dump_with_flags();
r = ca_with_feature_flags_parse_one(optarg, &u);
if (r < 0) {
log_error("Failed to parse --without= feature flag: %s", optarg);
return -EINVAL;
}
arg_without |= u;
break;
}
case ARG_WHAT:
r = parse_what_selector(optarg, &arg_what);
if (r <= 0)
return r;
break;
case ARG_EXCLUDE_NODUMP:
r = parse_boolean(optarg);
if (r < 0)
return log_error_errno(r, "Failed to parse --exclude-nodump= parameter: %s", optarg);
arg_exclude_nodump = r;
break;
case ARG_EXCLUDE_SUBMOUNTS:
r = parse_boolean(optarg);
if (r < 0)
return log_error_errno(r, "Failed to parse --exclude-submounts= parameter: %s", optarg);
arg_exclude_submounts = r;
break;
case ARG_EXCLUDE_FILE:
r = parse_boolean(optarg);
if (r < 0)
return log_error_errno(r, "Failed to parse --exclude-file= parameter: %s", optarg);
arg_exclude_file = r;
break;
case ARG_UNDO_IMMUTABLE:
r = parse_boolean(optarg);
if (r < 0) {
log_error("Failed to parse --undo-immutable= parameter: %s", optarg);
return r;
}
arg_undo_immutable = r;
break;
case ARG_PUNCH_HOLES:
r = parse_boolean(optarg);
if (r < 0) {
log_error("Failed to parse --punch-holes= parameter: %s", optarg);
return r;
}
arg_punch_holes = r;
break;
case ARG_REFLINK:
r = parse_boolean(optarg);
if (r < 0) {
log_error("Failed to parse --reflink= parameter: %s", optarg);
return r;
}
arg_reflink = r;
break;
case ARG_HARDLINK:
r = parse_boolean(optarg);
if (r < 0) {
log_error("Failed to parse --hardlink= parameter: %s", optarg);
return r;
}
arg_hardlink = r;
break;
case ARG_DELETE:
r = parse_boolean(optarg);
if (r < 0) {
log_error("Failed to parse --delete= parameter: %s", optarg);
return r;
}
arg_delete = r;
break;
case ARG_SEED_OUTPUT:
r = parse_boolean(optarg);
if (r < 0) {
log_error("Failed to parse --seed-output= parameter: %s", optarg);
return r;
}
arg_seed_output = r;
break;
case ARG_MKDIR:
r = parse_boolean(optarg);
if (r < 0)
return log_error_errno(r, "Failed to parse --mkdir= parameter: %s", optarg);
arg_mkdir = r;
break;
case ARG_UID_SHIFT: {
uid_t uid;
r = parse_uid(optarg, &uid);
if (r < 0) {
r = parse_boolean(optarg);
if (r < 0)
return log_error_errno(r, "Failed to parse --uid-shift= parameter: %s", optarg);
arg_uid_shift_apply = r;
} else {
arg_uid_shift = uid;
arg_uid_shift_apply = true;
}
break;
}
case ARG_UID_RANGE: {
uint64_t u;
/* The valid values for the range are 1..0x100000000. However, we store this in a 32bit uid_t,
* which requires us to map 0x100000000 to 0. */
r = safe_atou64(optarg, &u);
if (r < 0 || u == 0 || u > UINT64_C(0x100000000)) {
r = parse_boolean(optarg);
if (r < 0)
return log_error_errno(r, "Failed to parse --uid-range= parameter: %s", optarg);
arg_uid_shift_apply = r;
} else {
if (u == UINT64_C(0x100000000))
arg_uid_range = 0;
else
arg_uid_range = (uid_t) u;
arg_uid_shift_apply = true;
}
break;
}
case ARG_RECURSIVE:
r = parse_boolean(optarg);
if (r < 0)
return log_error_errno(r, "Failed to parse --recursive= parameter: %s", optarg);
arg_recursive = r;
break;
case ARG_DIGEST: {
CaDigestType t;
t = ca_digest_type_from_string(optarg);
if (t < 0)
return log_error_errno(t, "Failed to parse --digest= parameter: %s", optarg);
arg_digest = t;
break;
}
case ARG_COMPRESSION: {
CaCompressionType cc;
cc = ca_compression_type_from_string(optarg);
if (cc < 0)
return log_error_errno(cc, "Failed to parse --compression= parameter: %s", optarg);
if (!compressor_is_supported(cc))
return log_error_errno(EINVAL,
"Compiled without support for %s compression",
ca_compression_type_to_string(cc));
arg_compression = cc;
break;
}
case '?':
return -EINVAL;
default:
assert(false);
}
}
/* Propagate some settings to helpers we fork off */
if (arg_log_level >= 0) {
char buffer[DECIMAL_STR_MAX(int)];
snprintf(buffer, sizeof(buffer), "%d", arg_log_level);
(void) setenv("CASYNC_LOG_LEVEL", buffer, 1);
}
if (arg_verbose)
(void) putenv((char*) "CASYNC_VERBOSE=1");
else
unsetenv("CASYNC_VERBOSE");
return 1;
}
static int set_default_store(const char *index_path) {
const char *e;
int r;
if (arg_store)
return 0;
e = getenv("CASYNC_STORE");
if (e)
/* If the default store is set via an environment variable, use that */
arg_store = strdup(e);
else if (index_path) {
/* Otherwise, derive it from the index file path */
r = ca_locator_patch_last_component(index_path, "default.castr", &arg_store);
if (r < 0)
return log_error_errno(r, "Failed to automatically derive store location from index: %m");
} else
/* And if we don't know any, then place it in the current directory */
arg_store = strdup("default.castr");
if (!arg_store)
return log_oom();
return 1;
}
static int load_seeds_and_extra_stores(CaSync *s) {
char **i;
int r;
assert(s);
STRV_FOREACH(i, arg_extra_stores) {
r = ca_sync_add_store_auto(s, *i);
if (r < 0)
log_error("Failed to add extra store %s, ignoring: %m", *i);
}
STRV_FOREACH(i, arg_seeds) {
r = ca_sync_add_seed_path(s, *i);
if (r < 0)
log_error("Failed to add seed %s, ignoring: %m", *i);
}
return 0;
}
static uint64_t combined_with_flags(uint64_t default_with_flags) {
return (arg_with == 0 ? default_with_flags : arg_with) & ~arg_without;
}
static int load_feature_flags(CaSync *s, uint64_t default_with_flags) {
uint64_t flags;
int r;
assert(s);
flags = combined_with_flags(default_with_flags);
if (arg_exclude_nodump)
flags |= CA_FORMAT_EXCLUDE_NODUMP;
if (arg_exclude_submounts)
flags |= CA_FORMAT_EXCLUDE_SUBMOUNTS;
if (arg_exclude_file)
flags |= CA_FORMAT_EXCLUDE_FILE;
flags |= ca_feature_flags_from_digest_type(arg_digest);
r = ca_sync_set_feature_flags(s, flags);
if (r < 0 && r != -ENOTTY) /* only encoder syncs have a feature flags field */
return log_error_errno(r, "Failed to set feature flags: %m");
r = ca_sync_set_feature_flags_mask(s, flags);
if (r < 0 && r != -ENOTTY) /* only decoder syncs have a feature flags mask field */
return log_error_errno(r, "Failed to set feature flags mask: %m");
if (arg_uid_shift_apply) {
r = ca_sync_set_uid_shift(s, arg_uid_shift);
if (r < 0)
return log_error_errno(r, "Failed to set UID shift: %m");
r = ca_sync_set_uid_range(s, arg_uid_range);
if (r < 0)
return log_error_errno(r, "Failed to set UID range: %m");
}
r = ca_sync_set_undo_immutable(s, arg_undo_immutable);
if (r < 0 && r != -ENOTTY)
return log_error_errno(r, "Failed to set undo immutable flag: %m");
r = ca_sync_set_compression_type(s, arg_compression);
if (r < 0 && r != -ENOTTY)
return log_error_errno(r, "Failed to set compression: %m");
r = ca_sync_set_delete(s, arg_delete);
if (r < 0 && r != -ENOTTY)
return log_error_errno(r, "Failed to set deletion flag: %m");
return 0;
}
static int load_chunk_size(CaSync *s) {
uint64_t cavg, cmin, cmax;
int r;
if (arg_chunk_size_avg != 0) {
r = ca_sync_set_chunk_size_avg(s, arg_chunk_size_avg);
if (r < 0)
return log_error_errno(r, "Failed to set average chunk size to %zu: %m", arg_chunk_size_avg);
}
if (arg_chunk_size_min != 0) {
r = ca_sync_set_chunk_size_min(s, arg_chunk_size_min);
if (r < 0)
return log_error_errno(r, "Failed to set minimum chunk size to %zu: %m", arg_chunk_size_min);
}
if (arg_chunk_size_max != 0) {
r = ca_sync_set_chunk_size_max(s, arg_chunk_size_max);
if (r < 0)
return log_error_errno(r, "Failed to set maximum chunk size to %zu: %m", arg_chunk_size_max);
}
if (!arg_verbose)
return 1;
r = ca_sync_get_chunk_size_avg(s, &cavg);
if (r < 0)
return log_error_errno(r, "Failed to read average chunk size: %m");
r = ca_sync_get_chunk_size_min(s, &cmin);
if (r < 0)
return log_error_errno(r, "Failed to read minimum chunk size: %m");
r = ca_sync_get_chunk_size_max(s, &cmax);
if (r < 0)
return log_error_errno(r, "Failed to read maximum chunk size: %m");
log_info("Selected chunk sizes: min=%" PRIu64 "..avg=%" PRIu64 "..max=%" PRIu64, cmin, cavg, cmax);
return 1;
}
static int verbose_print_feature_flags(CaSync *s) {
static bool printed = false;
uint64_t flags;
_cleanup_free_ char *t = NULL;
int r;
assert(s);
if (!arg_verbose)
return 0;
if (printed)
return 0;
r = ca_sync_get_feature_flags(s, &flags);
if (r == -ENODATA) /* we don't know them yet? */
return 0;
if (r < 0)
return log_error_errno(r, "Failed to query feature flags: %m");
r = ca_with_feature_flags_format(flags, &t);
if (r < 0)
return log_error_errno(r, "Failed to format feature flags: %m");
log_info("Using feature flags: %s", strnone(t));
log_info("Excluding files and directories with chattr(1) -d flag: %s", yes_no(flags & CA_FORMAT_EXCLUDE_NODUMP));
log_info("Excluding submounts: %s", yes_no(flags & CA_FORMAT_EXCLUDE_SUBMOUNTS));
log_info("Excluding files and directories listed in .caexclude: %s", yes_no(flags & CA_FORMAT_EXCLUDE_FILE));
log_info("Digest algorithm: %s", ca_digest_type_to_string(ca_feature_flags_to_digest_type(flags)));
printed = true;
return 0;
}
static int verbose_print_path(CaSync *s, const char *verb) {
_cleanup_free_ char *path = NULL;
int r;
if (!arg_verbose)
return 0;
r = ca_sync_current_path(s, &path);
if (r == -ENOTDIR) /* Root isn't a directory */
return 0;
if (r < 0)
return log_error_errno(r, "Failed to query current path: %m");
log_info("%s%s%s", verb ?: "", verb ? " " : "", isempty(path) ? "./" : path);
return 1;
}
static int verbose_print_done_make(CaSync *s) {
uint64_t n_chunks = UINT64_MAX, size = UINT64_MAX, n_reused = UINT64_MAX, covering,
n_cache_hits = UINT64_MAX, n_cache_misses = UINT64_MAX, n_cache_invalidated = UINT64_MAX, n_cache_added = UINT64_MAX;
char buffer[FORMAT_BYTES_MAX];
int r;
assert(s);
if (!arg_verbose)
return 0;
r = ca_sync_get_covering_feature_flags(s, &covering);
if (r != -ENODATA) {
_cleanup_free_ char *t = NULL;
uint64_t selected, too_much;
if (r < 0)
return log_error_errno(r, "Failed to determine covering flags: %m");
r = ca_sync_get_feature_flags(s, &selected);
if (r < 0)
return log_error_errno(r, "Failed to determine used flags: %m");
r = ca_with_feature_flags_format(selected, &t);
if (r < 0)
return log_error_errno(r, "Failed to format feature flags: %m");
log_info("Selected feature flags: %s", strnone(t));
too_much = selected & ~covering;
if (too_much != 0) {
t = mfree(t);
r = ca_with_feature_flags_format(too_much, &t);
if (r < 0)
return log_error_errno(r, "Failed to format feature flags: %m");
log_info("Selected feature flags not actually applicable to backing file systems: %s", strnone(t));
}
}
r = ca_sync_current_archive_chunks(s, &n_chunks);
if (r < 0 && r != -ENODATA)
return log_error_errno(r, "Failed to determine number of chunks: %m");
r = ca_sync_current_archive_reused_chunks(s, &n_reused);
if (r < 0 && r != -ENODATA)
return log_error_errno(r, "Failed to determine number of reused chunks: %m");
r = ca_sync_current_archive_offset(s, &size);
if (r < 0 && r != -ENODATA)
return log_error_errno(r, "Failed to determine archive size: %m");
if (size != UINT64_MAX)
log_info("Archive size: %s", format_bytes(buffer, sizeof(buffer), size));
if (n_chunks != UINT64_MAX)
log_info("Number of chunks: %" PRIu64, n_chunks);
if (n_reused != UINT64_MAX) {
if (n_chunks != UINT64_MAX && n_chunks > 0)
log_info("Reused (non-cached) chunks: %"PRIu64 " (%"PRIu64 "%%)",
n_reused, n_reused * 100U / n_chunks);
else
log_info("Reused (non-cached) chunks: %" PRIu64, n_reused);
}
if (size != UINT64_MAX && n_chunks != UINT64_MAX)
log_info("Effective average chunk size: %s", format_bytes(buffer, sizeof(buffer), size / n_chunks));
r = ca_sync_current_cache_hits(s, &n_cache_hits);
if (r < 0 && r != -ENODATA)
return log_error_errno(r, "Failed to read number of cache hits: %m");
r = ca_sync_current_cache_misses(s, &n_cache_misses);
if (r < 0 && r != -ENODATA)
return log_error_errno(r, "Failed to read number of cache misses: %m");
r = ca_sync_current_cache_invalidated(s, &n_cache_invalidated);
if (r < 0 && r != -ENODATA)
return log_error_errno(r, "Failed to read number of invalidated cache items: %m");
r = ca_sync_current_cache_added(s, &n_cache_added);
if (r < 0 && r != -ENODATA)
return log_error_errno(r, "Failed to read number of added cache items: %m");
if (n_cache_hits != UINT64_MAX && n_cache_misses != UINT64_MAX && n_cache_invalidated != UINT64_MAX && n_cache_added != UINT64_MAX)
log_info("Cache hits: %" PRIu64 ", misses: %" PRIu64 ", invalidated: %" PRIu64 ", added: %" PRIu64, n_cache_hits, n_cache_misses, n_cache_invalidated, n_cache_added);
return 1;
}
static int verbose_print_done_extract(CaSync *s) {
char buffer[FORMAT_BYTES_MAX];
uint64_t n_bytes, n_requests;
uint64_t n_local_requests = UINT64_MAX, n_seed_requests = UINT64_MAX, n_remote_requests = UINT64_MAX;
uint64_t n_local_bytes = UINT64_MAX, n_seed_bytes = UINT64_MAX, n_remote_bytes = UINT64_MAX;
uint64_t total_requests = 0, total_bytes = 0;