File: mkcomposefs.c

package info (click to toggle)
composefs 1.0.8-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 11,040 kB
  • sloc: ansic: 9,004; sh: 416; python: 225; makefile: 5
file content (1705 lines) | stat: -rw-r--r-- 41,897 bytes parent folder | download
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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
/* lcfs
   Copyright (C) 2021 Giuseppe Scrivano <giuseppe@scrivano.org>

   SPDX-License-Identifier: GPL-2.0-or-later OR Apache-2.0
*/

#define _GNU_SOURCE

#include "config.h"

#include "libcomposefs/lcfs-writer.h"
#include "libcomposefs/lcfs-utils.h"
#include "libcomposefs/lcfs-internal.h"

#include <stdio.h>
#include <linux/limits.h>
#include <string.h>
#include <stdlib.h>
#include <stdarg.h>
#include <err.h>
#include <errno.h>
#include <unistd.h>
#include <fcntl.h>
#include <getopt.h>
#include <sys/types.h>
#include <sys/ioctl.h>
#include <linux/fsverity.h>
#include <linux/fs.h>
#include <pthread.h>
#include <sched.h>
#include <sys/sysinfo.h>

static void oom(void)
{
	errx(EXIT_FAILURE, "Out of memory");
}

static __attribute__((format(printf, 1, 2))) char *make_error(const char *fmt, ...)
{
	va_list ap;
	char *res;

	va_start(ap, fmt);
	if (vasprintf(&res, fmt, ap) < 0)
		oom();
	va_end(ap);

	return res;
}

#define OPT_SKIP_XATTRS 102
#define OPT_USE_EPOCH 103
#define OPT_SKIP_DEVICES 104
#define OPT_DIGEST_STORE 108
#define OPT_PRINT_DIGEST 109
#define OPT_PRINT_DIGEST_ONLY 111
#define OPT_USER_XATTRS 112
#define OPT_FROM_FILE 113
#define OPT_MIN_VERSION 114
#define OPT_THREADS 115
#define OPT_MAX_VERSION 116

static size_t split_at(const char **start, size_t *length, char split_char,
		       bool *partial)
{
	char *end = memchr(*start, split_char, *length);
	if (end == NULL) {
		size_t part_len = *length;
		*start = *start + *length;
		;
		*length = 0;
		if (partial)
			*partial = true;
		return part_len;
	}

	size_t part_len = end - *start;
	*start += part_len + 1;
	*length -= part_len + 1;
	if (partial)
		*partial = false;

	return part_len;
}

enum {
	FIELD_PATH,
	FIELD_SIZE,
	FIELD_MODE,
	FIELD_NLINK,
	FIELD_UID,
	FIELD_GID,
	FIELD_RDEV,
	FIELD_MTIME,
	FIELD_PAYLOAD,
	FIELD_CONTENT,
	FIELD_DIGEST,

	FIELD_XATTRS_START,
};

const char *names[] = {
	"PATH",		"SIZE",	 "MODE",    "NLINK",   "UID",	 "GID",
	"RDEV",		"MTIME", "PAYLOAD", "CONTENT", "DIGEST",

	"XATTRS_START",
};

static char *unescape_string(const char *escaped, size_t escaped_size,
			     size_t *unescaped_size, char **err)
{
	const char *escaped_end = escaped + escaped_size;
	cleanup_free char *res = malloc(escaped_size + 1);
	if (res == NULL)
		oom();
	char *out = res;

	*err = NULL;

	while (escaped < escaped_end) {
		char c = *escaped++;
		if (c == '\\') {
			if (escaped >= escaped_end) {
				*err = make_error("No character after escape");
				return NULL;
			}
			c = *escaped++;
			switch (c) {
			case '\\':
				*out++ = '\\';
				break;
			case 'n':
				*out++ = '\n';
				break;
			case 'r':
				*out++ = '\r';
				break;
			case 't':
				*out++ = '\t';
				break;
			case 'x':
				if (escaped >= escaped_end) {
					*err = make_error(
						"No hex characters after hex escape");
					return NULL;
				}
				int x1 = hexdigit(*escaped++);
				if (escaped >= escaped_end) {
					*err = make_error(
						"No hex characters after hex escape");
					return NULL;
				}
				int x2 = hexdigit(*escaped++);
				if (x1 < 0 || x2 < 0) {
					*err = make_error(
						"Invalid hex characters after hex escape");

					return NULL;
				}

				*out++ = x1 << 4 | x2;
				break;
			default: {
				*err = make_error("Unsupported escape type %c", c);
				return NULL;
			}
			}
		} else {
			*out++ = c;
		}
	}

	if (unescaped_size)
		*unescaped_size = out - res;

	*out = 0; /* Null terminate */

	return steal_pointer(&res);
}

static char *unescape_optional_string(const char *escaped, size_t escaped_size,
				      size_t *unescaped_size, char **err)
{
	*err = NULL;
	/* Optional */
	if (escaped_size == 1 && escaped[0] == '-')
		return NULL;

	return unescape_string(escaped, escaped_size, unescaped_size, err);
}

static struct lcfs_node_s *lookup_parent_path(struct lcfs_node_s *node,
					      const char *path, const char **name_out)
{
	while (*path == '/')
		path++;

	const char *start = path;
	while (*path != 0 && *path != '/')
		path++;

	if (*path == 0) {
		*name_out = start;
		return node;
	}

	cleanup_free char *name = strndup(start, path - start);
	if (name == NULL)
		oom();

	struct lcfs_node_s *child = lcfs_node_lookup_child(node, name);
	if (child == NULL)
		return NULL;

	return lookup_parent_path(child, path, name_out);
}

static struct lcfs_node_s *lookup_path(struct lcfs_node_s *node, const char *path)
{
	while (*path == '/')
		path++;

	if (*path == 0)
		return node;

	const char *start = path;
	while (*path != 0 && *path != '/')
		path++;

	cleanup_free char *name = strndup(start, path - start);
	if (name == NULL)
		oom();

	struct lcfs_node_s *child = lcfs_node_lookup_child(node, name);
	if (child == NULL)
		return NULL;

	return lookup_path(child, path);
}

static uint64_t parse_int_field(const char *str, size_t length, int base, char **err)
{
	cleanup_free char *s = strndup(str, length);
	if (s == NULL)
		oom();

	char *endptr = NULL;
	unsigned long long v = strtoull(s, &endptr, base);
	if (*s == 0 || *endptr != 0) {
		*err = make_error("Invalid integer %s", s);
		return 0;
	}

	return (uint64_t)v;
}

static char *parse_mtime(const char *str, size_t length, struct timespec *mtime)
{
	char *err = NULL;
	const char *mtime_sec_s = str;
	size_t mtime_sec_len = split_at(&str, &length, '.', NULL);
	uint64_t mtime_sec = parse_int_field(mtime_sec_s, mtime_sec_len, 10, &err);
	if (mtime_sec == 0 && err)
		return err;
	uint64_t mtime_nsec = parse_int_field(str, length, 10, &err);
	if (mtime_nsec == 0 && err)
		return err;
	mtime->tv_sec = mtime_sec;
	mtime->tv_nsec = mtime_nsec;
	return NULL;
}

static char *parse_xattr(const char *data, size_t data_len, struct lcfs_node_s *node)
{
	const char *xattr_name = data;
	bool is_partial = false;
	size_t xattr_name_len = split_at(&data, &data_len, '=', &is_partial);
	if (is_partial) {
		return make_error("Missing = in xattr");
	}

	char *err = NULL;
	cleanup_free char *key =
		unescape_string(xattr_name, xattr_name_len, NULL, &err);
	if (key == NULL && err)
		return err;
	size_t value_len;
	cleanup_free char *value = unescape_string(data, data_len, &value_len, &err);
	if (value == NULL && err)
		return err;

	if (lcfs_node_set_xattr(node, key, value, value_len) != 0)
		return make_error("Can't set xattr");
	return NULL;
}

typedef struct hardlink_fixup hardlink_fixup;
struct hardlink_fixup {
	struct lcfs_node_s *node;
	char *target_path;
	hardlink_fixup *next;
};

typedef struct dump_info dump_info;
struct dump_info {
	struct lcfs_node_s *root;
	hardlink_fixup *hardlink_fixups;
};

typedef struct field_info field_info;
struct field_info {
	const char *data;
	size_t len;
};

static char *tree_add_node(dump_info *info, const char *path, struct lcfs_node_s *node)
{
	if (strcmp(path, "/") == 0) {
		if (!lcfs_node_dirp(node))
			return make_error("Root must be a directory");

		if (info->root == NULL)
			info->root = lcfs_node_ref(node);
		else
			return make_error("Can't have multiple roots");
	} else {
		const char *name;
		struct lcfs_node_s *parent;

		if (info->root == NULL)
			return make_error("Root node not present");

		parent = lookup_parent_path(info->root, path, &name);

		if (parent == NULL)
			return make_error("Parent directory missing for %s", path);

		if (!lcfs_node_dirp(parent))
			return make_error("Parent must be a directory for %s", path);
		if (strcmp(name, ".") == 0 || strcmp(name, "..") == 0) {
			return make_error("Invalid . or .. in path: %s", path);
		}

		int r = lcfs_node_add_child(parent, node, name);
		if (r < 0) {
			if (r == -EEXIST)
				return make_error("Path %s already exist", path);
			return make_error("Can't add child");
		}
		/* add_child took ownership, ref again */
		lcfs_node_ref(node);
	}
	return NULL;
}

static void tree_add_hardlink_fixup(dump_info *info, char *target_path,
				    struct lcfs_node_s *node)
{
	hardlink_fixup *fixup = calloc(1, sizeof(hardlink_fixup));
	if (fixup == NULL)
		oom();

	fixup->node = node;
	fixup->target_path = target_path; /* Takes ownership */

	fixup->next = info->hardlink_fixups;
	info->hardlink_fixups = fixup;
}

static char *tree_resolve_hardlinks(dump_info *info)
{
	hardlink_fixup *fixup = info->hardlink_fixups;
	while (fixup != NULL) {
		hardlink_fixup *next = fixup->next;
		if (fixup->target_path == NULL)
			return make_error("No target path for the hardlink");
		struct lcfs_node_s *target =
			lookup_path(info->root, fixup->target_path);
		if (target == NULL)
			return make_error("No target at %s for hardlink",
					  fixup->target_path);

		/* Don't override existing value from image for target nlink */
		uint32_t old_nlink = lcfs_node_get_nlink(target);

		if (fixup->node == target) {
			return make_error("Self-referential hardlink %s",
					  fixup->target_path);
		}
		lcfs_node_make_hardlink(fixup->node, target);

		lcfs_node_set_nlink(target, old_nlink);

		free(fixup->target_path);
		free(fixup);

		fixup = next;
	}
	return NULL;
}

static char *tree_from_dump_line(dump_info *info, const char *line,
				 size_t line_len, bool strict_mode)
{
	int ret;

	/* At least honggfuzz very quickly discovered that split_line() sloppily allows
	 * embedded NUL characters, and its generated dumpfiles contain a lot of them
	 * and make them unreadable by default.
	 * We didn't document support for embedded NULs, and it only introduces
	 * ambiguity in parsing, so let's just reject this early on.
	 */
	char *embedded_nul_offset = memchr(line, 0, line_len);
	if (embedded_nul_offset != NULL) {
		size_t off = embedded_nul_offset - line;
		return make_error("Invalid embedded NUL character at position %lld",
				  (unsigned long long)off);
	}

	/* Split out all fixed fields */
	field_info fields[FIELD_XATTRS_START];
	for (int i = 0; i < FIELD_XATTRS_START; i++) {
		fields[i].data = line;
		fields[i].len = split_at(&line, &line_len, ' ', NULL);
	}

	char *err = NULL;
	cleanup_free char *path = unescape_string(
		fields[FIELD_PATH].data, fields[FIELD_PATH].len, NULL, &err);
	if (path == NULL && err)
		return err;
	if (!*path) {
		return make_error("Invalid empty path");
	}

	bool is_hardlink = false;
	/* First char in mode is @ if hardlink */
	if (fields[FIELD_MODE].len > 0 && fields[FIELD_MODE].data[0] == '@') {
		is_hardlink = true;
		fields[FIELD_MODE].len -= 1;
		fields[FIELD_MODE].data += 1;
	}
	uint64_t mode = parse_int_field(fields[FIELD_MODE].data,
					fields[FIELD_MODE].len, 8, &err);
	if (mode == 0 && err)
		return err;

	cleanup_node struct lcfs_node_s *node = lcfs_node_new();
	if (node == NULL) {
		oom();
	}
	if (lcfs_node_try_set_mode(node, mode) < 0) {
		return make_error("Invalid mode %o", (unsigned int)mode);
	}
	unsigned int type = mode & S_IFMT;

	err = tree_add_node(info, path, node);
	if (err)
		return err;

	/* For hardlinks, bail out early and handle in a fixup at the
         * end when we can resolve the target path. */
	if (is_hardlink) {
		if (lcfs_node_dirp(node))
			return make_error("Directories can't be hardlinked");
		err = NULL;
		cleanup_free char *target_path =
			unescape_optional_string(fields[FIELD_PAYLOAD].data,
						 fields[FIELD_PAYLOAD].len,
						 NULL, &err);
		if (target_path == NULL && err)
			return err;
		tree_add_hardlink_fixup(info, steal_pointer(&target_path), node);
		return NULL;
	}

	/* Handle regular files/dir data from fixed fields */
	uint64_t size = parse_int_field(fields[FIELD_SIZE].data,
					fields[FIELD_SIZE].len, 10, &err);
	if (size == 0 && err)
		return err;
	uint64_t nlink = parse_int_field(fields[FIELD_NLINK].data,
					 fields[FIELD_NLINK].len, 10, &err);
	if (nlink == 0 && err)
		return err;
	uint64_t uid = parse_int_field(fields[FIELD_UID].data,
				       fields[FIELD_UID].len, 10, &err);
	if (uid == 0 && err)
		return err;
	uint64_t gid = parse_int_field(fields[FIELD_GID].data,
				       fields[FIELD_GID].len, 10, &err);
	if (uid == 0 && err)
		return err;
	uint64_t rdev = parse_int_field(fields[FIELD_RDEV].data,
					fields[FIELD_RDEV].len, 10, &err);
	if (uid == 0 && err)
		return err;

	struct timespec mtime;
	err = parse_mtime(fields[FIELD_MTIME].data, fields[FIELD_MTIME].len, &mtime);
	if (err)
		return err;

	cleanup_free char *payload =
		unescape_optional_string(fields[FIELD_PAYLOAD].data,
					 fields[FIELD_PAYLOAD].len, NULL, &err);
	if (payload == NULL && err)
		return err;
	size_t content_len;
	cleanup_free char *content =
		unescape_optional_string(fields[FIELD_CONTENT].data,
					 fields[FIELD_CONTENT].len,
					 &content_len, &err);
	if (content == NULL && err)
		return err;
#ifndef FUZZER // Bypass this data-dependency when fuzzing to increase coverage
	if (content && content_len != size)
		return make_error("Invalid content size %lld, must match size %lld",
				  (long long)content_len, (long long)size);
#endif

	cleanup_free char *digest = unescape_optional_string(
		fields[FIELD_DIGEST].data, fields[FIELD_DIGEST].len, NULL, &err);
	if (digest == NULL && err)
		return err;

	if (type != S_IFLNK)
		lcfs_node_set_size(node, size);
	lcfs_node_set_nlink(node, nlink);
	lcfs_node_set_uid(node, uid);
	lcfs_node_set_gid(node, gid);
	if (type == S_IFCHR || type == S_IFBLK) {
		lcfs_node_set_rdev64(node, rdev);
	} else if (strict_mode && rdev != 0) {
		return make_error("Non-zero devnum on non-device");
	}
	lcfs_node_set_mtime(node, &mtime);
	// Validate that symlinks are non-empty
	if (type == S_IFLNK) {
		if (lcfs_node_set_symlink_payload(node, payload) < 0) {
			return make_error("Invalid symlink");
		}
		if (strict_mode) {
			size_t payload_len = strlen(payload);
			if (size != payload_len) {
				return make_error(
					"Invalid symlink size %lld, must match size %lld",
					(long long)payload_len, (long long)size);
			}
			if (content && content_len > 0) {
				return make_error("Symlink cannot have content");
			}
		}
	} else {
		if (lcfs_node_set_payload(node, payload) < 0)
			return make_error("Invalid payload");
		if (content) {
			if (content_len > LCFS_INLINE_CONTENT_MAX) {
				return make_error(
					"Inline content size %lld exceeds maximum %lld",
					(long long)content_len,
					(long long)LCFS_INLINE_CONTENT_MAX);
			}
			ret = lcfs_node_set_content(node, (uint8_t *)content,
						    content_len);
			if (ret < 0)
				oom();
		}
	}

	if (digest) {
		uint8_t raw[LCFS_DIGEST_SIZE];
		digest_to_raw(digest, raw, LCFS_DIGEST_SIZE);
		lcfs_node_set_fsverity_digest(node, raw);
	}

	// Because we call lcfs_node_set_xattr() in the loop below
	// which searches all previous values for duplicates, we
	// get O(N^2) behavior. Cap the maximum size of the input line.
	if (line_len > UINT16_MAX) {
		return make_error("Too many xattrs");
	}

	/* Handle trailing xattrs */
	while (line_len > 0) {
		const char *xattr = line;
		size_t xattr_len = split_at(&line, &line_len, ' ', NULL);

		err = parse_xattr(xattr, xattr_len, node);
		if (err)
			return err;
	}
	return NULL;
}

struct buffer {
	char *buf;
	size_t size;
	size_t capacity;
};

static void buffer_ensure_space(struct buffer *buf, size_t free_size_needed)
{
	size_t min_capacity = buf->size + free_size_needed;
	if (buf->capacity >= min_capacity)
		return;

	/* No space, grow */
	if (buf->capacity == 0)
		buf->capacity = 64 * 1024;
	else
		buf->capacity = buf->capacity * 2;

	if (buf->capacity < min_capacity)
		buf->capacity = min_capacity;

	buf->buf = realloc(buf->buf, buf->capacity);
	if (buf->buf == NULL)
		oom();
}

/* Fills buffer and returns the amount read. 0 on file end */
static size_t buffer_fill(struct buffer *buf, FILE *input)
{
	/* Grow buffer if needed */
	buffer_ensure_space(buf, 1);

	size_t bytes_read =
		fread(buf->buf + buf->size, 1, buf->capacity - buf->size, input);
	if (bytes_read == 0 && ferror(input))
		errx(EXIT_FAILURE, "Error reading from file");
	buf->size += bytes_read;

	return bytes_read;
}

static void buffer_reset(struct buffer *buf)
{
	/* NOTE: Leaves buffer data as is, just modified size */
	buf->size = 0;
}

static void buffer_add(struct buffer *buf, const char *src, size_t len)
{
	buffer_ensure_space(buf, len);

	/* memmove, as src may be in the buf */
	memmove(buf->buf + buf->size, src, len);
	buf->size += len;
}

static void buffer_free(struct buffer *buf)
{
	free(buf->buf);
}

static struct lcfs_node_s *tree_from_dump(FILE *input, char **out_err)
{
	dump_info info = { NULL };

	struct buffer buf = { NULL };

	// For now a hidden environment variable, may be promoted to a stable CLI
	// option once we're happy with semantics.
	bool strict_mode = getenv("CFS_PARSE_STRICT") != NULL;

	while (!feof(input)) {
		size_t bytes_read = buffer_fill(&buf, input);
		bool short_read = bytes_read == 0;

		const char *data = buf.buf;
		size_t remaining_data = buf.size;
		buffer_reset(&buf);

		while (remaining_data > 0) {
			const char *line = data;
			bool partial;
			size_t line_len =
				split_at(&data, &remaining_data, '\n', &partial);

			if (!partial || short_read) {
				char *err = tree_from_dump_line(
					&info, line, line_len, strict_mode);
				if (err != NULL) {
					*out_err = err;
					buffer_free(&buf);
					return NULL;
				}
			} else {
				/* Last line didn't have a newline and
				 * this wasn't a short read, so keep
				 * this for next read.
				 */
				buffer_add(&buf, line, line_len);
			}
		}
	}
	// Handle no trailing newline
	if (buf.size > 0 && !strict_mode) {
		char *err = tree_from_dump_line(&info, buf.buf, buf.size, strict_mode);
		if (err != NULL) {
			*out_err = err;
			buffer_free(&buf);
			return NULL;
		}
	} else if (buf.size > 0) {
		*out_err = make_error("Missing trailing newline");
		return NULL;
	}

	buffer_free(&buf);

	/* Fixup hardlinks now that we have all other files */
	char *err = tree_resolve_hardlinks(&info);
	if (err) {
		*out_err = err;
		return NULL;
	}

	return info.root;
}

static ssize_t write_cb(void *_file, void *buf, size_t count)
{
	FILE *file = _file;

	return fwrite(buf, 1, count, file);
}

#ifdef FUZZER
static int LLVMFuzzerTestOneInput(uint8_t *buf, size_t len)
{
	assert(buf);
	struct lcfs_node_s *tree;
	char *err = NULL;
	FILE *f = fmemopen(buf, len, "r");
	assert(f);
	tree = tree_from_dump(f, &err);
	bool is_err = err != NULL;
	free(err);
	fclose(f);
	if (!tree) {
		return 1;
	}
	if (is_err) {
		lcfs_node_unref(tree);
		return 1;
	}
	struct lcfs_write_options_s options = { 0 };
	char *outbuf = NULL;
	size_t outsize = 0;
	f = open_memstream(&outbuf, &outsize);
	assert(f);
	options.format = LCFS_FORMAT_EROFS;
	options.version = LCFS_DEFAULT_VERSION_MAX;
	options.file = f;
	options.file_write_cb = write_cb;
	if (lcfs_write_to(tree, &options) < 0) {
		lcfs_node_unref(tree);
		fclose(f);
		return 1;
	}
	lcfs_node_unref(tree);
	fclose(f);
	assert(outbuf);
	assert(outsize > 0);
	// And verify we can re-parse it
	tree = lcfs_load_node_from_image((const uint8_t *)outbuf, outsize);
	assert(tree);
	lcfs_node_unref(tree);
	return 0;
}

int main(int argc, char **argv)
{
	if (argc > 1) {
		char buffer[4096];
		size_t bytes_read;
		FILE *src = fopen(argv[1], "r");
		assert(src);
		char *buf;
		size_t len;
		FILE *dest = open_memstream(&buf, &len);
		assert(dest);
		while ((bytes_read = fread(buffer, 1, sizeof(buffer), src)) > 0) {
			if (fwrite(buffer, 1, bytes_read, dest) != bytes_read) {
				err(EXIT_FAILURE, "copying input");
			}
		}
		fclose(src);
		fclose(dest);
		LLVMFuzzerTestOneInput((void *)buf, len);
		return 0;
	}
	extern void HF_ITER(uint8_t * *buf, size_t * len);
	for (;;) {
		size_t len;
		uint8_t *buf;
		HF_ITER(&buf, &len);
		LLVMFuzzerTestOneInput(buf, len);
	}
}
#else
static int ensure_dir(const char *path, mode_t mode)
{
	struct stat buf;

	/* We check this ahead of time, otherwise
	   the mkdir call can fail in the read-only
	   case with EROFS instead of EEXIST on some
	   filesystems (such as NFS) */
	if (stat(path, &buf) == 0) {
		if (!S_ISDIR(buf.st_mode)) {
			errno = ENOTDIR;
			return -1;
		}

		return 0;
	}

	if (mkdir(path, mode) == -1 && errno != EEXIST)
		return -1;

	return 0;
}

static int join_paths(char **out, const char *path1, const char *path2)
{
	const char *sep = (path1[0] == '\0') ? "" : "/";
	int len = strlen(path1);

	while (len && path1[len - 1] == '/')
		len--;

	return asprintf(out, "%.*s%s%s", len, path1, sep, path2);
}

static void cleanup_unlink_freep(void *pp)
{
	char *filename = *(char **)pp;
	if (!filename)
		return;
	PROTECT_ERRNO;
	(void)unlink(filename);
	free(filename);
}

#define cleanup_unlink_free __attribute__((cleanup(cleanup_unlink_freep)))
static int mkdir_parents(const char *pathname, int mode)
{
	cleanup_free char *fn = strdup(pathname);
	if (fn == NULL) {
		errno = ENOMEM;
		return -1;
	}

	char *p = fn;
	while (*p == '/')
		p++;

	do {
		while (*p && *p != '/')
			p++;

		if (!*p)
			break;
		*p = '\0';

		if (ensure_dir(fn, mode) != 0) {
			return -1;
		}

		*p++ = '/';
		while (*p && *p == '/')
			p++;
	} while (p);

	return 0;
}

static int write_to_fd(int fd, const char *content, ssize_t len)
{
	ssize_t res;

	while (len > 0) {
		res = write(fd, content, len);
		if (res < 0 && errno == EINTR)
			continue;
		if (res <= 0) {
			if (res == 0) /* Unexpected short write, should not happen when writing to a file */
				errno = ENOSPC;
			return -1;
		}
		len -= res;
		content += res;
	}

	return 0;
}
static pthread_mutex_t mutex_thread_access = PTHREAD_MUTEX_INITIALIZER;
static bool try_copy_file_range = true;
static bool is_copy_file_range_available(void)
{
	bool ret = true;
	pthread_mutex_lock(&mutex_thread_access);
	ret = try_copy_file_range;
	pthread_mutex_unlock(&mutex_thread_access);

	return ret;
}

static void disable_copy_file_range(void)
{
	pthread_mutex_lock(&mutex_thread_access);
	try_copy_file_range = false;
	pthread_mutex_unlock(&mutex_thread_access);
}

#define BUFSIZE 8192
static int copy_file_data_classic(int sfd, int dfd)
{
	char buffer[BUFSIZE];
	ssize_t bytes_read;

	while (true) {
		bytes_read = read(sfd, buffer, BUFSIZE);
		if (bytes_read == -1) {
			if (errno == EINTR)
				continue;
			return -1;
		}

		if (bytes_read == 0)
			break;

		if (write_to_fd(dfd, buffer, bytes_read) != 0)
			return -1;
	}

	return 0;
}

static int copy_file_data_range(int sfd, int dfd)
{
	struct stat stat;

	if (fstat(sfd, &stat) == -1)
		return -1;

	off_t len, ret;
	len = stat.st_size;

	if (len == 0)
		return 0;

	do {
		ret = copy_file_range(sfd, NULL, dfd, NULL, len, 0);
		if (ret < 0 && errno == EINTR)
			continue;
		if (ret == -1)
			return -1;
		// This is an implementation problem in copy_file_range. Handle it and return error so that classic copy can be retried
		if (ret == 0 && len > 0) {
			// Setting this error code to trigger a classic copy
			// https://github.com/rust-lang/rust/blob/0e5f5207881066973486e6a480fa46cfa22947e9/library/std/src/sys/pal/unix/kernel_copy.rs#L622
			// fallback to work around several kernel bugs where copy_file_range will fail to
			// copy any bytes and return 0 instead of an error if
			// - reading virtual files from the proc filesystem which appear to have 0 size
			//   but are not empty. noted in coreutils to affect kernels at least up to 5.6.19.
			// - copying from an overlay filesystem in docker. reported to occur on fedora 32.
			errno = EINVAL; // EINVAL Either fd_in or fd_out is not a regular file.
			return -1;
		}
		if (ret == 0)
			break;

		len -= ret;
	} while (len > 0 && ret > 0);

	return 0;
}

static int copy_file_data(int sfd, int dfd)
{
	bool use_copy_classic = !is_copy_file_range_available();
	// https://github.com/rust-lang/rust/blob/0e5f5207881066973486e6a480fa46cfa22947e9/library/std/src/sys/pal/unix/kernel_copy.rs#L622
	// https://gitlab.gnome.org/GNOME/libglnx/-/blob/202b294e6079e23242e65e0426f8639841d1210b/glnx-fdio.c#L846
	// https://github.com/systemd/systemd/blob/e71b40fd0026c0884ca26eb4f0a9fbe4d9285cfa/src/shared/copy.c#L338
	// https://lwn.net/Articles/846403/
	int ret = -1;
	if (!use_copy_classic) {
		ret = copy_file_data_range(sfd, dfd);
		// Write was successful
		if (0 == ret)
			return 0;

		// https://github.com/rust-lang/rust/blob/0e5f5207881066973486e6a480fa46cfa22947e9/library/std/src/sys/pal/unix/kernel_copy.rs#L622
		// Try fallback io::copy if either:
		// - Kernel version is < 4.5 (ENOSYS¹)
		// - Files are mounted on different fs (EXDEV)
		// - copy_file_range is broken in various ways on RHEL/CentOS 7 (EOPNOTSUPP)
		// - copy_file_range file is immutable or syscall is blocked by seccomp¹ (EPERM)
		// - copy_file_range cannot be used with pipes or device nodes (EINVAL)
		// - the writer fd was opened with O_APPEND (EBADF²)
		// and no bytes were written successfully yet. (All these errnos should
		// not be returned if something was already written, but they happen in
		// the wild, see #91152.)
		//
		// ¹ these cases should be detected by the initial probe but we handle them here
		//   anyway in case syscall interception changes during runtime
		// ² actually invalid file descriptors would cause this too, but in that case
		//   the fallback code path is expected to encounter the same error again

		// Disable copy file range for the entire run because,
		// the rest of the files as part of this run will also have the similar file system.
		if (ret < 0 && (errno == ENOSYS || errno == EXDEV)) {
			disable_copy_file_range();
			use_copy_classic = true;
		}

		// Try classic for this file but copy_file_range could work for the next file.
		if (ret < 0 && (errno == EOPNOTSUPP || errno == EPERM ||
				errno == EINVAL || errno == EBADF)) {
			use_copy_classic = true;
		}
	}

	if (use_copy_classic) {
		ret = copy_file_data_classic(sfd, dfd);
	}
	return ret;
}

static int copy_file_with_dirs_if_needed(const char *src, const char *dst_base,
					 const char *dst, bool try_enable_fsverity)
{
	cleanup_free char *pathbuf = NULL;
	cleanup_unlink_free char *tmppath = NULL;
	int ret, res;
	errint_t err;
	cleanup_fd int sfd = -1;
	cleanup_fd int dfd = -1;
	struct stat statbuf;

	ret = join_paths(&pathbuf, dst_base, dst);
	if (ret < 0)
		return ret;

	ret = mkdir_parents(pathbuf, 0755);
	if (ret < 0)
		return ret;

	if (lstat(pathbuf, &statbuf) == 0)
		return 0; /* Already exists, no need to copy */

	ret = join_paths(&tmppath, dst_base, ".tmpXXXXXX");
	if (ret < 0)
		return ret;

	dfd = mkostemp(tmppath, O_CLOEXEC);
	if (dfd == -1)
		return -1;

	sfd = open(src, O_CLOEXEC | O_RDONLY);
	if (sfd == -1) {
		return -1;
	}

	// First try reflinking, which is fast and efficient if available.
	if (ioctl(dfd, FICLONE, sfd) != 0) {
		// Fall back to copying bits by hand
		res = copy_file_data(sfd, dfd);
		if (res < 0) {
			return res;
		}
	}
	cleanup_fdp(&sfd);

	/* Make sure file is readable by all */
	res = fchmod(dfd, 0644);
	if (res < 0) {
		return res;
	}

	res = fsync(dfd);
	if (res < 0) {
		return res;
	}
	cleanup_fdp(&dfd);

	if (try_enable_fsverity) {
		/* Try to enable fsverity */
		dfd = open(tmppath, O_CLOEXEC | O_RDONLY);
		if (dfd < 0) {
			return -1;
		}

		if (fstat(dfd, &statbuf) == 0) {
			err = lcfs_fd_enable_fsverity(dfd);
			if (err < 0) {
				/* Ignore errors, we're only trying to enable it */
			}
		}
	}

	res = rename(tmppath, pathbuf);
	if (res < 0) {
		return res;
	}
	// Avoid a spurious extra unlink() from the cleanup
	free(steal_pointer(&tmppath));

	return 0;
}

struct work_item {
	struct lcfs_node_s *node;
	char *path;
};

struct work_collection {
	struct work_item *items;
	int count;
	int capacity;
};

static int add_to_work_collection(struct work_collection *collection,
				  struct lcfs_node_s *node, const char *path)
{
	if (!collection) {
		errno = EINVAL;
		return -1;
	}
	if (collection->count == collection->capacity) {
		int new_capacity =
			collection->count == 0 ? 16 : collection->capacity * 2;
		struct work_item *new_children;
		new_children =
			reallocarray(collection->items,
				     sizeof(*collection->items), new_capacity);
		if (new_children == NULL) {
			errno = ENOMEM;
			return -1;
		}

		collection->items = new_children;
		collection->capacity = new_capacity;
	}
	collection->items[collection->count].path = strdup(path);
	if (collection->items[collection->count].path == NULL) {
		errno = ENOMEM;
		return -1;
	}
	collection->items[collection->count].node = lcfs_node_ref(node);

	++collection->count;

	return 0;
}

static void cleanup_work_items(struct work_collection *collection)
{
	if (!collection)
		return;

	for (int i = 0; i < collection->count; ++i) {
		free(collection->items[i].path);
		lcfs_node_unref(collection->items[i].node);
	}

	free(collection->items);
}

static int construct_copy_data(struct lcfs_node_s *node,
			       struct work_collection *collection, char *path)
{
	cleanup_free char *tmp_path = NULL;
	const char *fname = lcfs_node_get_name(node);
	if (fname) {
		if (join_paths(&tmp_path, path, fname) < 0)
			return -1;

		path = tmp_path;
	}

	if (lcfs_node_dirp(node)) {
		const size_t n_children = lcfs_node_get_n_children(node);
		for (size_t i = 0; i < n_children; i++) {
			if (construct_copy_data(lcfs_node_get_child(node, i),
						collection, path) < 0) {
				return -1;
			}
		}
	} else if ((lcfs_node_get_mode(node) & S_IFMT) == S_IFREG &&
		   lcfs_node_get_content(node) == NULL &&
		   lcfs_node_get_payload(node) != NULL) {
		if (add_to_work_collection(collection, node, path) < 0) {
			return -1;
		}
	}

	return 0;
}

static int construct_compute_data(struct lcfs_node_s *node,
				  struct work_collection *collection,
				  const char *path)
{
	cleanup_free char *tmp_path = NULL;
	const char *fname = lcfs_node_get_name(node);

	if (fname) {
		if (join_paths(&tmp_path, path, fname) < 0)
			return -1;

		path = tmp_path;
	}

	if ((node->inode.st_mode & S_IFMT) == S_IFREG) {
		if (add_to_work_collection(collection, node, path) < 0) {
			return -1;
		}
	}

	if (!lcfs_node_dirp(node))
		return 0;

	size_t n_children = lcfs_node_get_n_children(node);
	for (size_t i = 0; i < n_children; i++) {
		struct lcfs_node_s *child = lcfs_node_get_child(node, i);
		if (construct_compute_data(child, collection, path) < 0) {
			return -1;
		}
	}

	return 0;
}

struct work_item_iterator {
	pthread_mutex_t *mutex_node_iterator;
	int current_item;
	int errorcode;
	bool cancel_request;
};

static struct work_item *get_next_work_item(struct work_collection *collection,
					    struct work_item_iterator *iterator)
{
	if (!iterator || !collection)
		return NULL;

	bool cancel = false;
	struct work_item *ret = NULL;

	pthread_mutex_lock(iterator->mutex_node_iterator);
	if (iterator->cancel_request)
		cancel = true;
	else if (iterator->current_item < collection->count) {
		ret = &(collection->items[iterator->current_item]);
		iterator->current_item++;
	}
	pthread_mutex_unlock(iterator->mutex_node_iterator);
	return cancel ? NULL : ret;
}

static void request_cancel(struct work_item_iterator *iterator, int errorcode)
{
	pthread_mutex_lock(iterator->mutex_node_iterator);
	// Record only the first cancels error code
	if (!iterator->cancel_request) {
		iterator->cancel_request = true;
		iterator->errorcode = errorcode;
	}
	pthread_mutex_unlock(iterator->mutex_node_iterator);
}

typedef int (*THREAD_PROCESS_PROC)(struct work_item *, void *);

static int process_copy(struct work_item *item, void *digest_store_path)
{
	return copy_file_with_dirs_if_needed(item->path,
					     (const char *)digest_store_path,
					     lcfs_node_get_payload(item->node),
					     true);
}

static int process_compute(struct work_item *item, void *data)
{
	int buildflag = (int)(long)data;
	return lcfs_node_set_from_content(item->node, AT_FDCWD, item->path, buildflag);
}

struct thread_data {
	THREAD_PROCESS_PROC proc;
	struct work_collection *collection;
	struct work_item_iterator *iterator;
	void *data;
};

static void *thread_proc(void *data)
{
	struct thread_data *info = (struct thread_data *)data;

	while (true) {
		struct work_item *item =
			get_next_work_item(info->collection, info->iterator);

		if (!item)
			return 0;

		if (!item->node) {
			request_cancel(info->iterator, EINVAL);
			return 0;
		}

		if (info->proc(item, info->data) != 0) {
			request_cancel(info->iterator, errno);
			return 0;
		}
	}
	return 0;
}

static int execute_in_threads(const int requested_threads,
			      struct work_collection *collection,
			      THREAD_PROCESS_PROC proc, void *data)
{
	struct work_item_iterator iterator;
	iterator.mutex_node_iterator = &mutex_thread_access;
	iterator.current_item = 0;
	iterator.errorcode = 0;
	iterator.cancel_request = false;

	struct thread_data thread_info;
	thread_info.data = data;
	thread_info.proc = proc;
	thread_info.collection = collection;
	thread_info.iterator = &iterator;

	int ret = -1;
	cleanup_free pthread_t *threads = NULL;
	const int thread_count = requested_threads - 1;
	if (thread_count >= 1) {
		threads = calloc(thread_count, sizeof(pthread_t));
		if (threads == NULL) {
			errno = ENOMEM;
			return -1;
		}

		for (int i = 0; i < thread_count; i++) {
			ret = pthread_create(&threads[i], NULL, thread_proc,
					     &thread_info);
			if (ret != 0) {
				request_cancel(&iterator, ret);
				for (int j = 0; j < i; ++j) {
					// not checking return as it is already in an error case
					pthread_join(threads[j], NULL);
				}
				errno = ret;
				return -1;
			}
		}
	}

	// Let this thread also process items instead of waiting for the worker threads
	thread_proc(&thread_info);

	if (thread_count >= 1) {
		for (int i = 0; i < thread_count; i++) {
			ret = pthread_join(threads[i], NULL);
			if (ret != 0) {
				// set the error code and continue joining threads
				request_cancel(&iterator, ret);
			}
		}
	}
	if (iterator.cancel_request) {
		errno = iterator.errorcode;
	}
	return iterator.cancel_request ? -1 : 0;
}

static int compute_digest(const int thread_count, struct lcfs_node_s *node,
			  const char *path, int buildflag)
{
	struct work_collection collection;
	collection.items = NULL;
	collection.capacity = 0;
	collection.count = 0;

	if (construct_compute_data(node, &collection, path) < 0) {
		return -1;
	}

	int ret = execute_in_threads(thread_count, &collection, process_compute,
				     (void *)(long)buildflag);
	cleanup_work_items(&collection);

	return ret;
}

static int fill_store(const int thread_count, struct lcfs_node_s *node,
		      const char *path, const char *digest_store_path)
{
	struct work_collection collection;
	collection.items = NULL;
	collection.capacity = 0;
	collection.count = 0;

	if (construct_copy_data(node, &collection, (char *)path) < 0) {
		return -1;
	}

	int ret = execute_in_threads(thread_count, &collection, process_copy,
				     (void *)digest_store_path);
	cleanup_work_items(&collection);
	return ret;
}

static int get_cpu_count(void)
{
	cpu_set_t set;

	if (sched_getaffinity(0, sizeof(set), &set) == 0)
		return CPU_COUNT(&set);

	return get_nprocs();
}

static void usage(const char *argv0)
{
	const char *bin = gnu_basename(argv0);
	fprintf(stderr,
		"Usage: %s [OPTIONS] SOURCE IMAGE\n"
		"Options:\n"
		"  --digest-store=PATH   Store content files in this directory\n"
		"  --use-epoch           Make all mtimes zero\n"
		"  --skip-devices        Don't store device nodes\n"
		"  --skip-xattrs         Don't store file xattrs\n"
		"  --user-xattrs         Only store user.* xattrs\n"
		"  --print-digest        Print the digest of the image\n"
		"  --print-digest-only   Print the digest of the image, don't write image\n"
		"  --from-file           The source is a dump file, not a directory\n"
		"  --min-version=N       Use this minimal format version (default=%d)\n"
		"  --max-version=N       Use this maximum format version (default=%d)\n"
		"  --threads=N           Use this to override the default number of threads used to calculate digest and copy files (default=%d)\n",
		bin, LCFS_DEFAULT_VERSION_MIN, LCFS_DEFAULT_VERSION_MAX,
		get_cpu_count());
}

int main(int argc, char **argv)
{
	const struct option longopts[] = {
		{ .name = "skip-xattrs",
		  .has_arg = no_argument,
		  .flag = NULL,
		  .val = OPT_SKIP_XATTRS },
		{ .name = "user-xattrs",
		  .has_arg = no_argument,
		  .flag = NULL,
		  .val = OPT_USER_XATTRS },
		{ .name = "skip-devices",
		  .has_arg = no_argument,
		  .flag = NULL,
		  .val = OPT_SKIP_DEVICES },
		{ .name = "use-epoch", .has_arg = no_argument, .flag = NULL, .val = OPT_USE_EPOCH },
		{ .name = "digest-store",
		  .has_arg = required_argument,
		  .flag = NULL,
		  .val = OPT_DIGEST_STORE },
		{ .name = "print-digest",
		  .has_arg = no_argument,
		  .flag = NULL,
		  .val = OPT_PRINT_DIGEST },
		{ .name = "print-digest-only",
		  .has_arg = no_argument,
		  .flag = NULL,
		  .val = OPT_PRINT_DIGEST_ONLY },
		{ .name = "from-file", .has_arg = no_argument, .flag = NULL, .val = OPT_FROM_FILE },
		{ .name = "max-version",
		  .has_arg = required_argument,
		  .flag = NULL,
		  .val = OPT_MAX_VERSION },
		{ .name = "min-version",
		  .has_arg = required_argument,
		  .flag = NULL,
		  .val = OPT_MIN_VERSION },
		{ .name = "threads",
		  .has_arg = required_argument,
		  .flag = NULL,
		  .val = OPT_THREADS },
		{},
	};
	struct lcfs_write_options_s options = { 0 };
	const char *bin = argv[0];
	int buildflags = 0;
	bool print_digest = false;
	bool print_digest_only = false;
	bool from_file = false;
	struct lcfs_node_s *root;
	const char *out = NULL;
	const char *src_path = NULL;
	const char *digest_store_path = NULL;
	cleanup_free char *pathbuf = NULL;
	uint8_t digest[LCFS_DIGEST_SIZE];
	int opt;
	FILE *out_file;
	char *failed_path;
	bool version_set = false;
	long min_version = 0;
	long max_version = 0;
	char *end;
	int threads = get_cpu_count();

#ifdef FUZZER
#endif

	/* We always compute the digest and reference by digest */
	buildflags |= LCFS_BUILD_COMPUTE_DIGEST | LCFS_BUILD_BY_DIGEST;

	while ((opt = getopt_long(argc, argv, ":CR", longopts, NULL)) != -1) {
		switch (opt) {
		case OPT_USE_EPOCH:
			buildflags |= LCFS_BUILD_USE_EPOCH;
			break;
		case OPT_SKIP_XATTRS:
			buildflags |= LCFS_BUILD_SKIP_XATTRS;
			break;
		case OPT_USER_XATTRS:
			buildflags |= LCFS_BUILD_USER_XATTRS;
			break;
		case OPT_SKIP_DEVICES:
			buildflags |= LCFS_BUILD_SKIP_DEVICES;
			break;
		case OPT_DIGEST_STORE:
			digest_store_path = optarg;
			break;
		case OPT_PRINT_DIGEST:
			print_digest = true;
			break;
		case OPT_PRINT_DIGEST_ONLY:
			print_digest = print_digest_only = true;
			break;
		case OPT_FROM_FILE:
			from_file = true;
			break;
		case OPT_MIN_VERSION:
			version_set = true;
			min_version = strtol(optarg, &end, 10);
			if (*optarg == 0 || *end != 0) {
				fprintf(stderr, "Invalid min version %s\n", optarg);
				exit(EXIT_FAILURE);
			}
			break;
		case OPT_MAX_VERSION:
			version_set = true;
			max_version = strtol(optarg, &end, 10);
			if (*optarg == 0 || *end != 0) {
				fprintf(stderr, "Invalid max version %s\n", optarg);
				exit(EXIT_FAILURE);
			}
			break;

		case OPT_THREADS:
			threads = strtol(optarg, &end, 10);
			if (*optarg == 0 || *end != 0) {
				fprintf(stderr, "Invalid threads count %s\n", optarg);
				exit(EXIT_FAILURE);
			}
			if (threads <= 0) {
				fprintf(stderr, "Invalid threads count %d\n", threads);
				exit(EXIT_FAILURE);
			}
			break;
		case ':':
			fprintf(stderr, "option needs a value\n");
			exit(EXIT_FAILURE);
		default:
			usage(bin);
			exit(1);
		}
	}

	if (!version_set) {
		min_version = LCFS_DEFAULT_VERSION_MIN;
		max_version = LCFS_DEFAULT_VERSION_MAX;
	}

	argv += optind;
	argc -= optind;

	if (argc < 1) {
		fprintf(stderr, "No source path specified\n");
		usage(bin);
		exit(1);
	}
	src_path = argv[0];

	if (src_path[0] == '\0')
		errx(EXIT_FAILURE, "Empty source path specified");

	if (argc > 2) {
		fprintf(stderr, "Too many arguments\n");
		usage(bin);
		exit(1);
	}
	if (argc == 1) {
		if (!print_digest_only) {
			fprintf(stderr, "No destination path specified\n");
			usage(bin);
			exit(1);
		}
	} else if (!print_digest_only) {
		assert(argc == 2);
		out = argv[1];
	} else {
		fprintf(stderr,
			"Cannot specify destination path with --print-digest-only\n");
		usage(bin);
		exit(1);
	}

	assert(out || print_digest_only);

	if (print_digest_only) {
		out_file = NULL;
	} else if (strcmp(out, "-") == 0) {
		if (isatty(1))
			errx(EXIT_FAILURE, "stdout is a tty.  Refusing to use it");
		out_file = stdout;
	} else {
		out_file = fopen(out, "we");
		if (out_file == NULL)
			err(EXIT_FAILURE, "failed to open output file");
	}

	if (from_file) {
		FILE *input = NULL;
		bool close_input = false;
		if (strcmp(src_path, "-") == 0) {
			input = stdin;
		} else {
			input = fopen(src_path, "r");
			if (input == NULL)
				err(EXIT_FAILURE, "open `%s`", src_path);
			close_input = true;
		}

		char *err = NULL;
		root = tree_from_dump(input, &err);
		if (root == NULL) {
			if (err)
				errx(EXIT_FAILURE, "%s", err);
			else
				errx(EXIT_FAILURE, "No files in dump file");
		}

		if (close_input)
			fclose(input);
	} else {
		// Digest calculation and inline will be done in parallel
		int buildflag_copy = buildflags;
		buildflag_copy &= ~LCFS_BUILD_COMPUTE_DIGEST;
		buildflag_copy &= ~LCFS_BUILD_BY_DIGEST;
		buildflag_copy |= LCFS_BUILD_NO_INLINE;

		root = lcfs_build(AT_FDCWD, src_path, buildflag_copy, &failed_path);
		if (root == NULL)
			err(EXIT_FAILURE, "error accessing %s", failed_path);

		if (compute_digest(threads, root, src_path, buildflags) < 0)
			err(EXIT_FAILURE, "error computing digest");

		if (digest_store_path &&
		    fill_store(threads, root, src_path, digest_store_path) < 0)
			err(EXIT_FAILURE, "cannot fill store");
	}

	if (out_file) {
		options.file = out_file;
		options.file_write_cb = write_cb;
	}
	if (print_digest)
		options.digest_out = digest;

	options.format = LCFS_FORMAT_EROFS;
	options.version = (int)min_version;
	options.max_version = (int)max_version;

	if (lcfs_write_to(root, &options) < 0)
		err(EXIT_FAILURE, "cannot write file");

	if (print_digest) {
		char digest_str[LCFS_DIGEST_SIZE * 2 + 1] = { 0 };
		digest_to_string(digest, digest_str);
		printf("%s\n", digest_str);
	}

	if (out_file && fclose(out_file) == EOF)
		err(EXIT_FAILURE, "close output file");

	lcfs_node_unref(root);
	return 0;
}
#endif