-
Notifications
You must be signed in to change notification settings - Fork 42
/
main.c
1495 lines (1357 loc) · 38.6 KB
/
main.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: GPL-2.0+
/*
* Copyright (C) 2018-2019 HUAWEI, Inc.
* http://www.huawei.com/
* Created by Li Guifu <[email protected]>
*/
#define _GNU_SOURCE
#include <ctype.h>
#include <time.h>
#include <sys/time.h>
#include <stdlib.h>
#include <limits.h>
#include <libgen.h>
#include <sys/stat.h>
#include <getopt.h>
#include "erofs/config.h"
#include "erofs/print.h"
#include "erofs/cache.h"
#include "erofs/diskbuf.h"
#include "erofs/inode.h"
#include "erofs/tar.h"
#include "erofs/compress.h"
#include "erofs/dedupe.h"
#include "erofs/xattr.h"
#include "erofs/exclude.h"
#include "erofs/block_list.h"
#include "erofs/compress_hints.h"
#include "erofs/blobchunk.h"
#include "erofs/fragments.h"
#include "erofs/rebuild.h"
#include "../lib/liberofs_private.h"
#include "../lib/liberofs_uuid.h"
#include "../lib/compressor.h"
static struct option long_options[] = {
{"version", no_argument, 0, 'V'},
{"help", no_argument, 0, 'h'},
{"exclude-path", required_argument, NULL, 2},
{"exclude-regex", required_argument, NULL, 3},
#ifdef HAVE_LIBSELINUX
{"file-contexts", required_argument, NULL, 4},
#endif
{"force-uid", required_argument, NULL, 5},
{"force-gid", required_argument, NULL, 6},
{"all-root", no_argument, NULL, 7},
#ifndef NDEBUG
{"random-pclusterblks", no_argument, NULL, 8},
{"random-algorithms", no_argument, NULL, 18},
#endif
{"max-extent-bytes", required_argument, NULL, 9},
{"compress-hints", required_argument, NULL, 10},
{"chunksize", required_argument, NULL, 11},
{"quiet", no_argument, 0, 12},
{"blobdev", required_argument, NULL, 13},
{"ignore-mtime", no_argument, NULL, 14},
{"preserve-mtime", no_argument, NULL, 15},
{"uid-offset", required_argument, NULL, 16},
{"gid-offset", required_argument, NULL, 17},
{"tar", optional_argument, NULL, 20},
{"aufs", no_argument, NULL, 21},
{"mount-point", required_argument, NULL, 512},
{"xattr-prefix", required_argument, NULL, 19},
#ifdef WITH_ANDROID
{"product-out", required_argument, NULL, 513},
{"fs-config-file", required_argument, NULL, 514},
{"block-list-file", required_argument, NULL, 515},
#endif
{"ovlfs-strip", optional_argument, NULL, 516},
{"offset", required_argument, NULL, 517},
#ifdef HAVE_ZLIB
{"gzip", no_argument, NULL, 518},
{"ungzip", optional_argument, NULL, 518},
#endif
#ifdef HAVE_LIBLZMA
{"unlzma", optional_argument, NULL, 519},
{"unxz", optional_argument, NULL, 519},
#endif
#ifdef EROFS_MT_ENABLED
{"workers", required_argument, NULL, 520},
#endif
{"zfeature-bits", required_argument, NULL, 521},
{"clean", optional_argument, NULL, 522},
{"incremental", optional_argument, NULL, 523},
{"root-xattr-isize", required_argument, NULL, 524},
{"mkfs-time", no_argument, NULL, 525},
{"all-time", no_argument, NULL, 526},
{"sort", required_argument, NULL, 527},
{0, 0, 0, 0},
};
static void print_available_compressors(FILE *f, const char *delim)
{
int i = 0;
bool comma = false;
const struct erofs_algorithm *s;
while ((s = z_erofs_list_available_compressors(&i)) != NULL) {
if (comma)
fputs(delim, f);
fputs(s->name, f);
comma = true;
}
fputc('\n', f);
}
static void usage(int argc, char **argv)
{
int i = 0;
const struct erofs_algorithm *s;
// " 1 2 3 4 5 6 7 8 "
// "12345678901234567890123456789012345678901234567890123456789012345678901234567890\n"
printf(
"Usage: %s [OPTIONS] FILE SOURCE(s)\n"
"Generate EROFS image (FILE) from SOURCE(s).\n"
"\n"
"General options:\n"
" -V, --version print the version number of mkfs.erofs and exit\n"
" -h, --help display this help and exit\n"
"\n"
" -b# set block size to # (# = page size by default)\n"
" -d<0-9> set output verbosity; 0=quiet, 9=verbose (default=%i)\n"
" -x# set xattr tolerance to # (< 0, disable xattrs; default 2)\n"
" -zX[,level=Y] X=compressor (Y=compression level, Z=dictionary size, optional)\n"
" [,dictsize=Z] alternative compressors can be separated by colons(:)\n"
" [:...] supported compressors and their option ranges are:\n",
argv[0], EROFS_WARN);
while ((s = z_erofs_list_available_compressors(&i)) != NULL) {
const char spaces[] = " ";
printf("%s%s\n", spaces, s->name);
if (s->c->setlevel) {
if (!strcmp(s->name, "lzma"))
/* A little kludge to show the range as disjointed
* "0-9,100-109" instead of a continuous "0-109", and to
* state what those two subranges respectively mean. */
printf("%s [,level=<0-9,100-109>]\t0-9=normal, 100-109=extreme (default=%i)\n",
spaces, s->c->default_level);
else
printf("%s [,level=<0-%i>]\t\t(default=%i)\n",
spaces, s->c->best_level, s->c->default_level);
}
if (s->c->setdictsize) {
if (s->c->default_dictsize)
printf("%s [,dictsize=<dictsize>]\t(default=%u, max=%u)\n",
spaces, s->c->default_dictsize, s->c->max_dictsize);
else
printf("%s [,dictsize=<dictsize>]\t(default=<auto>, max=%u)\n",
spaces, s->c->max_dictsize);
}
}
printf(
" -C# specify the size of compress physical cluster in bytes\n"
" -EX[,...] X=extended options\n"
" -L volume-label set the volume label (maximum 15 bytes)\n"
" -T# specify a fixed UNIX timestamp # as build time\n"
" --all-time the timestamp is also applied to all files (default)\n"
" --mkfs-time the timestamp is applied as build time only\n"
" -UX use a given filesystem UUID\n"
" --all-root make all files owned by root\n"
" --blobdev=X specify an extra device X to store chunked data\n"
" --chunksize=# generate chunk-based files with #-byte chunks\n"
" --clean=X run full clean build (default) or:\n"
" --incremental=X run incremental build\n"
" (X = data|rvsp; data=full data, rvsp=space is allocated\n"
" and filled with zeroes)\n"
" --compress-hints=X specify a file to configure per-file compression strategy\n"
" --exclude-path=X avoid including file X (X = exact literal path)\n"
" --exclude-regex=X avoid including files that match X (X = regular expression)\n"
#ifdef HAVE_LIBSELINUX
" --file-contexts=X specify a file contexts file to setup selinux labels\n"
#endif
" --force-uid=# set all file uids to # (# = UID)\n"
" --force-gid=# set all file gids to # (# = GID)\n"
" --uid-offset=# add offset # to all file uids (# = id offset)\n"
" --gid-offset=# add offset # to all file gids (# = id offset)\n"
" --ignore-mtime use build time instead of strict per-file modification time\n"
" --max-extent-bytes=# set maximum decompressed extent size # in bytes\n"
" --mount-point=X X=prefix of target fs path (default: /)\n"
" --preserve-mtime keep per-file modification time strictly\n"
" --offset=# skip # bytes at the beginning of IMAGE.\n"
" --root-xattr-isize=# ensure the inline xattr size of the root directory is # bytes at least\n"
" --aufs replace aufs special files with overlayfs metadata\n"
" --sort=<path,none> data sorting order for tarballs as input (default: path)\n"
" --tar=X generate a full or index-only image from a tarball(-ish) source\n"
" (X = f|i|headerball; f=full mode, i=index mode,\n"
" headerball=file data is omited in the source stream)\n"
" --ovlfs-strip=<0,1> strip overlayfs metadata in the target image (e.g. whiteouts)\n"
" --quiet quiet execution (do not write anything to standard output.)\n"
#ifndef NDEBUG
" --random-pclusterblks randomize pclusterblks for big pcluster (debugging only)\n"
" --random-algorithms randomize per-file algorithms (debugging only)\n"
#endif
#ifdef HAVE_ZLIB
" --ungzip[=X] try to filter the tarball stream through gzip\n"
" (and optionally dump the raw stream to X together)\n"
#endif
#ifdef HAVE_LIBLZMA
" --unxz[=X] try to filter the tarball stream through xz/lzma/lzip\n"
" (and optionally dump the raw stream to X together)\n"
#endif
#ifdef EROFS_MT_ENABLED
" --workers=# set the number of worker threads to # (default: %u)\n"
#endif
" --xattr-prefix=X X=extra xattr name prefix\n"
" --zfeature-bits=# toggle filesystem compression features according to given bits #\n"
#ifdef WITH_ANDROID
"\n"
"Android-specific options:\n"
" --product-out=X X=product_out directory\n"
" --fs-config-file=X X=fs_config file\n"
" --block-list-file=X X=block_list file\n"
#endif
#ifdef EROFS_MT_ENABLED
, erofs_get_available_processors() /* --workers= */
#endif
);
}
static void version(void)
{
printf("mkfs.erofs (erofs-utils) %s\navailable compressors: ",
cfg.c_version);
print_available_compressors(stdout, ", ");
}
static unsigned int pclustersize_packed, pclustersize_max;
static struct erofs_tarfile erofstar = {
.global.xattrs = LIST_HEAD_INIT(erofstar.global.xattrs)
};
static bool tar_mode, rebuild_mode, incremental_mode;
enum {
EROFS_MKFS_DATA_IMPORT_DEFAULT,
EROFS_MKFS_DATA_IMPORT_FULLDATA,
EROFS_MKFS_DATA_IMPORT_RVSP,
EROFS_MKFS_DATA_IMPORT_SPARSE,
} dataimport_mode;
static unsigned int rebuild_src_count;
static LIST_HEAD(rebuild_src_list);
static u8 fixeduuid[16];
static bool valid_fixeduuid;
static int erofs_mkfs_feat_set_legacy_compress(bool en, const char *val,
unsigned int vallen)
{
if (vallen)
return -EINVAL;
/* disable compacted indexes and 0padding */
cfg.c_legacy_compress = en;
return 0;
}
static int erofs_mkfs_feat_set_ztailpacking(bool en, const char *val,
unsigned int vallen)
{
if (vallen)
return -EINVAL;
cfg.c_ztailpacking = en;
return 0;
}
static int erofs_mkfs_feat_set_fragments(bool en, const char *val,
unsigned int vallen)
{
if (!en) {
if (vallen)
return -EINVAL;
cfg.c_fragments = false;
return 0;
}
if (vallen) {
char *endptr;
u64 i = strtoull(val, &endptr, 0);
if (endptr - val != vallen) {
erofs_err("invalid pcluster size %s for the packed file", val);
return -EINVAL;
}
pclustersize_packed = i;
}
cfg.c_fragments = true;
return 0;
}
static int erofs_mkfs_feat_set_all_fragments(bool en, const char *val,
unsigned int vallen)
{
cfg.c_all_fragments = en;
return erofs_mkfs_feat_set_fragments(en, val, vallen);
}
static int erofs_mkfs_feat_set_dedupe(bool en, const char *val,
unsigned int vallen)
{
if (vallen)
return -EINVAL;
cfg.c_dedupe = en;
return 0;
}
static struct {
char *feat;
int (*set)(bool en, const char *val, unsigned int len);
} z_erofs_mkfs_features[] = {
{"legacy-compress", erofs_mkfs_feat_set_legacy_compress},
{"ztailpacking", erofs_mkfs_feat_set_ztailpacking},
{"fragments", erofs_mkfs_feat_set_fragments},
{"all-fragments", erofs_mkfs_feat_set_all_fragments},
{"dedupe", erofs_mkfs_feat_set_dedupe},
{NULL, NULL},
};
static int parse_extended_opts(const char *opts)
{
#define MATCH_EXTENTED_OPT(opt, token, keylen) \
(keylen == strlen(opt) && !memcmp(token, opt, keylen))
const char *token, *next, *tokenend, *value __maybe_unused;
unsigned int keylen, vallen;
value = NULL;
for (token = opts; *token != '\0'; token = next) {
bool clear = false;
const char *p = strchr(token, ',');
next = NULL;
if (p) {
next = p + 1;
} else {
p = token + strlen(token);
next = p;
}
tokenend = memchr(token, '=', p - token);
if (tokenend) {
keylen = tokenend - token;
vallen = p - tokenend - 1;
if (!vallen)
return -EINVAL;
value = tokenend + 1;
} else {
keylen = p - token;
vallen = 0;
}
if (token[0] == '^') {
if (keylen < 2)
return -EINVAL;
++token;
--keylen;
clear = true;
}
if (MATCH_EXTENTED_OPT("force-inode-compact", token, keylen)) {
if (vallen)
return -EINVAL;
cfg.c_force_inodeversion = FORCE_INODE_COMPACT;
cfg.c_ignore_mtime = true;
} else if (MATCH_EXTENTED_OPT("force-inode-extended", token, keylen)) {
if (vallen)
return -EINVAL;
cfg.c_force_inodeversion = FORCE_INODE_EXTENDED;
} else if (MATCH_EXTENTED_OPT("nosbcrc", token, keylen)) {
if (vallen)
return -EINVAL;
erofs_sb_clear_sb_chksum(&g_sbi);
} else if (MATCH_EXTENTED_OPT("noinline_data", token, keylen)) {
if (vallen)
return -EINVAL;
cfg.c_inline_data = false;
} else if (MATCH_EXTENTED_OPT("inline_data", token, keylen)) {
if (vallen)
return -EINVAL;
cfg.c_inline_data = !clear;
} else if (MATCH_EXTENTED_OPT("force-inode-blockmap", token, keylen)) {
if (vallen)
return -EINVAL;
cfg.c_force_chunkformat = FORCE_INODE_BLOCK_MAP;
} else if (MATCH_EXTENTED_OPT("force-chunk-indexes", token, keylen)) {
if (vallen)
return -EINVAL;
cfg.c_force_chunkformat = FORCE_INODE_CHUNK_INDEXES;
} else if (MATCH_EXTENTED_OPT("xattr-name-filter", token, keylen)) {
if (vallen)
return -EINVAL;
cfg.c_xattr_name_filter = !clear;
} else {
int i, err;
for (i = 0; z_erofs_mkfs_features[i].feat; ++i) {
if (!MATCH_EXTENTED_OPT(z_erofs_mkfs_features[i].feat,
token, keylen))
continue;
err = z_erofs_mkfs_features[i].set(!clear, value, vallen);
if (err)
return err;
break;
}
if (!z_erofs_mkfs_features[i].feat) {
erofs_err("unknown extended option %.*s",
(int)(p - token), token);
return -EINVAL;
}
}
}
return 0;
}
static int mkfs_apply_zfeature_bits(uintmax_t bits)
{
int i;
for (i = 0; bits; ++i) {
int err;
if (!z_erofs_mkfs_features[i].feat) {
erofs_err("unsupported zfeature bit %u", i);
return -EINVAL;
}
err = z_erofs_mkfs_features[i].set(bits & 1, NULL, 0);
if (err) {
erofs_err("failed to apply zfeature %s",
z_erofs_mkfs_features[i].feat);
return err;
}
bits >>= 1;
}
return 0;
}
static void mkfs_parse_tar_cfg(char *cfg)
{
char *p;
tar_mode = true;
if (!cfg)
return;
p = strchr(cfg, ',');
if (p) {
*p = '\0';
if ((*++p) != '\0')
erofstar.mapfile = strdup(p);
}
if (!strcmp(cfg, "headerball"))
erofstar.headeronly_mode = true;
if (erofstar.headeronly_mode || !strcmp(optarg, "i") ||
!strcmp(optarg, "0"))
erofstar.index_mode = true;
}
static int mkfs_parse_one_compress_alg(char *alg,
struct erofs_compr_opts *copts)
{
char *p, *q, *opt, *endptr;
copts->level = -1;
copts->dict_size = 0;
p = strchr(alg, ',');
if (p) {
copts->alg = strndup(alg, p - alg);
/* support old '-zlzma,9' form */
if (isdigit(*(p + 1))) {
copts->level = strtol(p + 1, &endptr, 10);
if (*endptr && *endptr != ',') {
erofs_err("invalid compression level %s",
p + 1);
return -EINVAL;
}
return 0;
}
} else {
copts->alg = strdup(alg);
return 0;
}
opt = p + 1;
while (opt) {
q = strchr(opt, ',');
if (q)
*q = '\0';
if ((p = strstr(opt, "level="))) {
p += strlen("level=");
copts->level = strtol(p, &endptr, 10);
if ((endptr == p) || (*endptr && *endptr != ',')) {
erofs_err("invalid compression level %s", p);
return -EINVAL;
}
} else if ((p = strstr(opt, "dictsize="))) {
p += strlen("dictsize=");
copts->dict_size = strtoul(p, &endptr, 10);
if (*endptr == 'k' || *endptr == 'K')
copts->dict_size <<= 10;
else if (*endptr == 'm' || *endptr == 'M')
copts->dict_size <<= 20;
else if ((endptr == p) || (*endptr && *endptr != ',')) {
erofs_err("invalid compression dictsize %s", p);
return -EINVAL;
}
} else {
erofs_err("invalid compression option %s", opt);
return -EINVAL;
}
opt = q ? q + 1 : NULL;
}
return 0;
}
static int mkfs_parse_compress_algs(char *algs)
{
unsigned int i;
char *s;
int ret;
for (s = strtok(algs, ":"), i = 0; s; s = strtok(NULL, ":"), ++i) {
if (i >= EROFS_MAX_COMPR_CFGS - 1) {
erofs_err("too many algorithm types");
return -EINVAL;
}
ret = mkfs_parse_one_compress_alg(s, &cfg.c_compr_opts[i]);
if (ret)
return ret;
}
return 0;
}
static void erofs_rebuild_cleanup(void)
{
struct erofs_sb_info *src, *n;
list_for_each_entry_safe(src, n, &rebuild_src_list, list) {
list_del(&src->list);
erofs_put_super(src);
erofs_dev_close(src);
free(src);
}
rebuild_src_count = 0;
}
static int mkfs_parse_options_cfg(int argc, char *argv[])
{
char *endptr;
int opt, i, err;
bool quiet = false;
int tarerofs_decoder = 0;
bool has_timestamp = false;
while ((opt = getopt_long(argc, argv, "C:E:L:T:U:b:d:x:z:Vh",
long_options, NULL)) != -1) {
switch (opt) {
case 'z':
i = mkfs_parse_compress_algs(optarg);
if (i)
return i;
break;
case 'b':
i = atoi(optarg);
if (i < 512 || i > EROFS_MAX_BLOCK_SIZE) {
erofs_err("invalid block size %s", optarg);
return -EINVAL;
}
g_sbi.blkszbits = ilog2(i);
break;
case 'd':
i = atoi(optarg);
if (i < EROFS_MSG_MIN || i > EROFS_MSG_MAX) {
erofs_err("invalid debug level %d", i);
return -EINVAL;
}
cfg.c_dbg_lvl = i;
break;
case 'x':
i = strtol(optarg, &endptr, 0);
if (*endptr != '\0') {
erofs_err("invalid xattr tolerance %s", optarg);
return -EINVAL;
}
cfg.c_inline_xattr_tolerance = i;
break;
case 'E':
opt = parse_extended_opts(optarg);
if (opt)
return opt;
break;
case 'L':
if (optarg == NULL ||
strlen(optarg) > (sizeof(g_sbi.volume_name) - 1u)) {
erofs_err("invalid volume label");
return -EINVAL;
}
strncpy(g_sbi.volume_name, optarg,
sizeof(g_sbi.volume_name));
break;
case 'T':
cfg.c_unix_timestamp = strtoull(optarg, &endptr, 0);
if (cfg.c_unix_timestamp == -1 || *endptr != '\0') {
erofs_err("invalid UNIX timestamp %s", optarg);
return -EINVAL;
}
has_timestamp = true;
break;
case 'U':
if (erofs_uuid_parse(optarg, fixeduuid)) {
erofs_err("invalid UUID %s", optarg);
return -EINVAL;
}
valid_fixeduuid = true;
break;
case 2:
opt = erofs_parse_exclude_path(optarg, false);
if (opt) {
erofs_err("failed to parse exclude path: %s",
erofs_strerror(opt));
return opt;
}
break;
case 3:
opt = erofs_parse_exclude_path(optarg, true);
if (opt) {
erofs_err("failed to parse exclude regex: %s",
erofs_strerror(opt));
return opt;
}
break;
case 4:
opt = erofs_selabel_open(optarg);
if (opt && opt != -EBUSY)
return opt;
break;
case 5:
cfg.c_uid = strtoul(optarg, &endptr, 0);
if (cfg.c_uid == -1 || *endptr != '\0') {
erofs_err("invalid uid %s", optarg);
return -EINVAL;
}
break;
case 6:
cfg.c_gid = strtoul(optarg, &endptr, 0);
if (cfg.c_gid == -1 || *endptr != '\0') {
erofs_err("invalid gid %s", optarg);
return -EINVAL;
}
break;
case 7:
cfg.c_uid = cfg.c_gid = 0;
break;
#ifndef NDEBUG
case 8:
cfg.c_random_pclusterblks = true;
break;
case 18:
cfg.c_random_algorithms = true;
break;
#endif
case 9:
cfg.c_max_decompressed_extent_bytes =
strtoul(optarg, &endptr, 0);
if (*endptr != '\0') {
erofs_err("invalid maximum uncompressed extent size %s",
optarg);
return -EINVAL;
}
break;
case 10:
cfg.c_compress_hints_file = optarg;
break;
case 512:
cfg.mount_point = optarg;
/* all trailing '/' should be deleted */
opt = strlen(cfg.mount_point);
if (opt && optarg[opt - 1] == '/')
optarg[opt - 1] = '\0';
break;
#ifdef WITH_ANDROID
case 513:
cfg.target_out_path = optarg;
break;
case 514:
cfg.fs_config_file = optarg;
break;
case 515:
cfg.block_list_file = optarg;
break;
#endif
case 'C':
i = strtoull(optarg, &endptr, 0);
if (*endptr != '\0') {
erofs_err("invalid physical clustersize %s",
optarg);
return -EINVAL;
}
pclustersize_max = i;
break;
case 11:
i = strtol(optarg, &endptr, 0);
if (*endptr != '\0') {
erofs_err("invalid chunksize %s", optarg);
return -EINVAL;
}
cfg.c_chunkbits = ilog2(i);
if ((1 << cfg.c_chunkbits) != i) {
erofs_err("chunksize %s must be a power of two",
optarg);
return -EINVAL;
}
erofs_sb_set_chunked_file(&g_sbi);
break;
case 12:
quiet = true;
break;
case 13:
cfg.c_blobdev_path = optarg;
break;
case 14:
cfg.c_ignore_mtime = true;
break;
case 15:
cfg.c_ignore_mtime = false;
break;
case 16:
errno = 0;
cfg.c_uid_offset = strtoll(optarg, &endptr, 0);
if (errno || *endptr != '\0') {
erofs_err("invalid uid offset %s", optarg);
return -EINVAL;
}
break;
case 17:
errno = 0;
cfg.c_gid_offset = strtoll(optarg, &endptr, 0);
if (errno || *endptr != '\0') {
erofs_err("invalid gid offset %s", optarg);
return -EINVAL;
}
break;
case 19:
errno = 0;
opt = erofs_xattr_insert_name_prefix(optarg);
if (opt) {
erofs_err("failed to parse xattr name prefix: %s",
erofs_strerror(opt));
return opt;
}
cfg.c_extra_ea_name_prefixes = true;
break;
case 20:
mkfs_parse_tar_cfg(optarg);
break;
case 21:
erofstar.aufs = true;
break;
case 516:
if (!optarg || !strcmp(optarg, "1"))
cfg.c_ovlfs_strip = true;
else
cfg.c_ovlfs_strip = false;
break;
case 517:
g_sbi.bdev.offset = strtoull(optarg, &endptr, 0);
if (*endptr != '\0') {
erofs_err("invalid disk offset %s", optarg);
return -EINVAL;
}
break;
case 518:
case 519:
if (optarg)
erofstar.dumpfile = strdup(optarg);
tarerofs_decoder = EROFS_IOS_DECODER_GZIP + (opt - 518);
break;
#ifdef EROFS_MT_ENABLED
case 520: {
unsigned int processors;
cfg.c_mt_workers = strtoul(optarg, &endptr, 0);
if (errno || *endptr != '\0') {
erofs_err("invalid worker number %s", optarg);
return -EINVAL;
}
processors = erofs_get_available_processors();
if (cfg.c_mt_workers > processors)
erofs_warn("%d workers exceed %d processors, potentially impacting performance.",
cfg.c_mt_workers, processors);
break;
}
#endif
case 521:
i = strtol(optarg, &endptr, 0);
if (errno || *endptr != '\0') {
erofs_err("invalid zfeature bits %s", optarg);
return -EINVAL;
}
err = mkfs_apply_zfeature_bits(i);
if (err)
return err;
break;
case 522:
case 523:
if (!optarg || !strcmp(optarg, "data")) {
dataimport_mode = EROFS_MKFS_DATA_IMPORT_FULLDATA;
} else if (!strcmp(optarg, "rvsp")) {
dataimport_mode = EROFS_MKFS_DATA_IMPORT_RVSP;
} else {
dataimport_mode = strtol(optarg, &endptr, 0);
if (errno || *endptr != '\0') {
erofs_err("invalid --%s=%s",
opt == 523 ? "incremental" : "clean", optarg);
return -EINVAL;
}
}
incremental_mode = (opt == 523);
break;
case 524:
cfg.c_root_xattr_isize = strtoull(optarg, &endptr, 0);
if (*endptr != '\0') {
erofs_err("invalid the minimum inline xattr size %s", optarg);
return -EINVAL;
}
break;
case 525:
cfg.c_timeinherit = TIMESTAMP_NONE;
break;
case 526:
cfg.c_timeinherit = TIMESTAMP_FIXED;
break;
case 527:
if (!strcmp(optarg, "none"))
erofstar.try_no_reorder = true;
break;
case 'V':
version();
exit(0);
case 'h':
usage(argc, argv);
exit(0);
default: /* '?' */
return -EINVAL;
}
}
if (cfg.c_blobdev_path && cfg.c_chunkbits < g_sbi.blkszbits) {
erofs_err("--blobdev must be used together with --chunksize");
return -EINVAL;
}
/* TODO: can be implemented with (deviceslot) mapped_blkaddr */
if (cfg.c_blobdev_path &&
cfg.c_force_chunkformat == FORCE_INODE_BLOCK_MAP) {
erofs_err("--blobdev cannot work with block map currently");
return -EINVAL;
}
if (optind >= argc) {
erofs_err("missing argument: FILE");
return -EINVAL;
}
cfg.c_img_path = strdup(argv[optind++]);
if (!cfg.c_img_path)
return -ENOMEM;
if (optind >= argc) {
if (!tar_mode) {
erofs_err("missing argument: SOURCE(s)");
return -EINVAL;
} else {
int dupfd;
dupfd = dup(STDIN_FILENO);
if (dupfd < 0) {
erofs_err("failed to duplicate STDIN_FILENO: %s",
strerror(errno));
return -errno;
}
err = erofs_iostream_open(&erofstar.ios, dupfd,
tarerofs_decoder);
if (err)
return err;
}
} else {
struct stat st;
cfg.c_src_path = realpath(argv[optind++], NULL);
if (!cfg.c_src_path) {
erofs_err("failed to parse source directory: %s",
erofs_strerror(-errno));
return -ENOENT;
}
if (tar_mode) {
int fd = open(cfg.c_src_path, O_RDONLY);
if (fd < 0) {
erofs_err("failed to open file: %s", cfg.c_src_path);
return -errno;
}
err = erofs_iostream_open(&erofstar.ios, fd,
tarerofs_decoder);
if (err)
return err;
if (erofstar.dumpfile) {
fd = open(erofstar.dumpfile,
O_WRONLY | O_CREAT | O_TRUNC, 0644);
if (fd < 0) {
erofs_err("failed to open dumpfile: %s",
erofstar.dumpfile);
return -errno;
}
erofstar.ios.dumpfd = fd;
}
} else {
err = lstat(cfg.c_src_path, &st);
if (err)
return -errno;
if (S_ISDIR(st.st_mode))
erofs_set_fs_root(cfg.c_src_path);
else
rebuild_mode = true;
}
if (rebuild_mode) {
char *srcpath = cfg.c_src_path;
struct erofs_sb_info *src;
do {
src = calloc(1, sizeof(struct erofs_sb_info));
if (!src) {
erofs_rebuild_cleanup();
return -ENOMEM;
}
err = erofs_dev_open(src, srcpath, O_RDONLY);
if (err) {
free(src);
erofs_rebuild_cleanup();
return err;
}
/* extra device index starts from 1 */
src->dev = ++rebuild_src_count;
list_add(&src->list, &rebuild_src_list);
} while (optind < argc && (srcpath = argv[optind++]));
} else if (optind < argc) {
erofs_err("unexpected argument: %s\n", argv[optind]);
return -EINVAL;
}
}
if (quiet) {
cfg.c_dbg_lvl = EROFS_ERR;
cfg.c_showprogress = false;
}
if (pclustersize_max) {
if (pclustersize_max < erofs_blksiz(&g_sbi) ||
pclustersize_max % erofs_blksiz(&g_sbi)) {
erofs_err("invalid physical clustersize %u",
pclustersize_max);
return -EINVAL;
}
cfg.c_mkfs_pclustersize_max = pclustersize_max;
cfg.c_mkfs_pclustersize_def = cfg.c_mkfs_pclustersize_max;
}
if (cfg.c_chunkbits && cfg.c_chunkbits < g_sbi.blkszbits) {
erofs_err("chunksize %u must be larger than block size",
1u << cfg.c_chunkbits);
return -EINVAL;
}
if (pclustersize_packed) {
if (pclustersize_packed < erofs_blksiz(&g_sbi) ||
pclustersize_packed % erofs_blksiz(&g_sbi)) {
erofs_err("invalid pcluster size for the packed file %u",
pclustersize_packed);
return -EINVAL;
}
cfg.c_mkfs_pclustersize_packed = pclustersize_packed;
}
if (has_timestamp && cfg.c_timeinherit == TIMESTAMP_UNSPECIFIED)