composefs 0.4.0

Rust library for the composefs filesystem
Documentation
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
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583
2584
2585
2586
2587
2588
2589
2590
2591
2592
2593
2594
2595
2596
2597
2598
2599
2600
2601
2602
2603
2604
2605
2606
2607
2608
2609
2610
2611
2612
2613
2614
2615
2616
2617
2618
2619
2620
2621
2622
2623
2624
2625
2626
2627
2628
2629
2630
2631
2632
2633
2634
2635
2636
2637
2638
2639
2640
2641
2642
2643
2644
2645
2646
2647
2648
2649
2650
2651
2652
2653
2654
2655
2656
2657
2658
2659
2660
2661
2662
2663
2664
2665
2666
2667
2668
2669
2670
2671
2672
2673
2674
2675
2676
2677
2678
2679
2680
2681
2682
2683
2684
2685
2686
2687
2688
2689
2690
2691
2692
2693
2694
2695
2696
2697
2698
2699
2700
2701
2702
2703
2704
2705
2706
2707
2708
2709
2710
2711
2712
2713
2714
2715
2716
2717
2718
2719
2720
2721
2722
2723
2724
2725
2726
2727
2728
2729
2730
2731
2732
2733
2734
2735
2736
2737
2738
2739
2740
2741
2742
2743
2744
2745
2746
2747
2748
2749
2750
2751
2752
2753
2754
2755
2756
2757
2758
2759
2760
2761
2762
2763
2764
2765
2766
2767
2768
2769
2770
2771
2772
2773
2774
2775
2776
2777
2778
2779
2780
2781
2782
2783
2784
2785
2786
2787
2788
2789
2790
2791
2792
2793
2794
2795
2796
2797
2798
2799
2800
2801
2802
2803
2804
2805
2806
2807
2808
2809
2810
2811
2812
2813
2814
2815
2816
2817
2818
2819
2820
2821
2822
2823
2824
2825
2826
2827
2828
2829
2830
2831
2832
2833
2834
2835
2836
2837
2838
2839
2840
2841
2842
2843
2844
2845
2846
2847
2848
2849
2850
2851
2852
2853
2854
2855
2856
2857
2858
2859
2860
2861
2862
2863
2864
2865
2866
2867
2868
2869
2870
2871
2872
2873
2874
2875
2876
2877
2878
2879
2880
2881
2882
2883
2884
2885
2886
2887
2888
2889
2890
2891
2892
2893
2894
2895
2896
2897
2898
2899
2900
2901
2902
2903
2904
2905
2906
2907
2908
2909
2910
2911
2912
2913
2914
2915
2916
2917
2918
2919
2920
2921
2922
2923
2924
2925
2926
2927
2928
2929
2930
2931
2932
2933
2934
2935
2936
2937
2938
2939
2940
2941
2942
2943
2944
2945
2946
2947
2948
2949
2950
2951
2952
2953
2954
2955
2956
2957
2958
2959
2960
2961
2962
2963
2964
2965
2966
2967
2968
2969
2970
2971
2972
2973
2974
2975
2976
2977
2978
2979
2980
2981
2982
2983
2984
2985
2986
2987
2988
2989
2990
2991
2992
2993
2994
2995
2996
2997
2998
2999
3000
3001
3002
3003
3004
3005
3006
3007
3008
3009
3010
3011
3012
3013
3014
3015
3016
3017
3018
3019
3020
3021
3022
3023
3024
3025
3026
3027
3028
3029
3030
3031
3032
3033
3034
3035
3036
3037
3038
3039
3040
3041
3042
3043
3044
3045
3046
3047
3048
3049
3050
3051
3052
3053
3054
3055
3056
3057
3058
3059
3060
3061
3062
3063
3064
3065
3066
3067
3068
3069
3070
3071
3072
3073
3074
3075
3076
3077
3078
3079
3080
3081
3082
3083
3084
3085
3086
3087
3088
3089
3090
3091
3092
3093
3094
3095
3096
3097
3098
3099
3100
3101
3102
3103
3104
3105
3106
3107
3108
3109
3110
3111
3112
3113
3114
3115
3116
3117
3118
3119
3120
3121
3122
3123
3124
3125
3126
3127
3128
3129
3130
3131
3132
3133
3134
3135
3136
3137
3138
3139
3140
3141
3142
3143
3144
3145
3146
3147
3148
3149
3150
3151
3152
3153
3154
3155
3156
3157
3158
3159
3160
3161
3162
3163
3164
3165
3166
3167
3168
3169
3170
3171
3172
3173
3174
3175
3176
3177
3178
3179
3180
3181
3182
3183
3184
3185
3186
3187
3188
3189
3190
3191
3192
3193
3194
3195
3196
3197
3198
3199
3200
3201
3202
3203
3204
3205
3206
3207
3208
3209
3210
3211
3212
3213
3214
3215
3216
3217
3218
3219
3220
3221
3222
3223
3224
3225
3226
3227
3228
3229
3230
3231
3232
3233
3234
3235
3236
3237
3238
3239
3240
3241
3242
3243
3244
3245
3246
3247
3248
3249
3250
3251
3252
3253
3254
3255
3256
3257
3258
3259
3260
3261
3262
3263
3264
3265
3266
3267
3268
3269
3270
3271
3272
3273
3274
3275
3276
3277
3278
3279
3280
3281
3282
3283
3284
3285
3286
3287
3288
3289
3290
3291
3292
3293
3294
3295
3296
3297
3298
3299
3300
3301
3302
3303
3304
3305
3306
3307
3308
3309
3310
3311
3312
3313
3314
3315
3316
3317
3318
3319
3320
3321
3322
3323
3324
3325
3326
3327
3328
3329
3330
3331
3332
3333
3334
3335
3336
3337
3338
3339
3340
3341
3342
3343
3344
3345
3346
3347
3348
3349
3350
3351
3352
3353
3354
3355
3356
3357
3358
3359
3360
3361
3362
3363
3364
3365
3366
3367
3368
3369
3370
3371
3372
3373
3374
3375
3376
3377
3378
3379
3380
3381
3382
3383
3384
3385
3386
3387
3388
3389
3390
3391
3392
3393
3394
3395
3396
3397
3398
3399
3400
3401
3402
3403
3404
3405
3406
3407
3408
3409
3410
3411
3412
3413
3414
3415
3416
3417
3418
3419
3420
3421
3422
3423
3424
3425
3426
3427
3428
3429
3430
3431
3432
3433
3434
3435
3436
3437
3438
3439
3440
3441
3442
3443
3444
3445
3446
3447
3448
3449
3450
3451
3452
3453
3454
3455
3456
3457
3458
3459
3460
3461
3462
3463
3464
3465
3466
3467
3468
3469
3470
3471
3472
3473
3474
3475
3476
3477
3478
3479
3480
3481
3482
3483
3484
3485
3486
3487
3488
3489
3490
3491
3492
3493
3494
3495
3496
3497
3498
3499
3500
3501
3502
3503
3504
3505
3506
3507
3508
3509
3510
3511
3512
3513
3514
3515
3516
3517
3518
3519
3520
3521
3522
3523
3524
3525
3526
3527
3528
3529
3530
3531
3532
3533
3534
3535
3536
3537
3538
3539
3540
3541
3542
3543
3544
3545
3546
3547
3548
3549
3550
3551
3552
3553
3554
3555
3556
3557
3558
3559
3560
3561
3562
3563
3564
3565
3566
3567
3568
3569
3570
3571
3572
3573
3574
3575
3576
3577
3578
3579
3580
3581
3582
3583
3584
3585
3586
3587
3588
3589
3590
3591
3592
3593
3594
3595
3596
3597
3598
3599
3600
3601
3602
3603
3604
3605
3606
3607
3608
3609
3610
3611
3612
3613
3614
3615
3616
3617
3618
3619
3620
3621
3622
3623
3624
3625
3626
3627
3628
3629
3630
3631
3632
3633
3634
3635
3636
3637
3638
3639
3640
3641
3642
3643
3644
3645
3646
3647
3648
3649
3650
3651
3652
3653
3654
3655
3656
3657
3658
3659
3660
3661
3662
3663
3664
3665
3666
3667
3668
3669
3670
3671
3672
3673
3674
3675
3676
3677
3678
3679
3680
3681
3682
3683
3684
3685
3686
3687
3688
3689
3690
3691
3692
3693
3694
3695
3696
3697
3698
3699
3700
3701
3702
3703
3704
3705
3706
3707
3708
3709
3710
3711
3712
3713
3714
3715
3716
3717
3718
3719
3720
3721
3722
3723
3724
3725
3726
3727
3728
3729
3730
3731
3732
3733
3734
3735
3736
3737
3738
3739
3740
3741
3742
3743
3744
3745
3746
3747
3748
3749
3750
3751
3752
3753
3754
3755
3756
3757
3758
3759
3760
3761
3762
3763
3764
3765
3766
3767
3768
3769
3770
3771
3772
3773
3774
3775
3776
3777
3778
3779
3780
3781
3782
3783
3784
3785
3786
3787
3788
3789
3790
3791
3792
3793
3794
3795
3796
3797
3798
3799
3800
3801
3802
3803
3804
3805
3806
3807
3808
3809
3810
3811
3812
3813
3814
3815
3816
3817
3818
3819
3820
3821
3822
3823
3824
3825
3826
3827
3828
3829
3830
3831
3832
3833
3834
3835
3836
3837
3838
3839
3840
3841
3842
3843
3844
3845
3846
3847
3848
3849
3850
3851
3852
3853
3854
3855
3856
3857
3858
3859
3860
3861
3862
3863
3864
3865
3866
3867
3868
3869
3870
3871
3872
3873
3874
3875
3876
3877
3878
3879
3880
3881
3882
3883
3884
3885
3886
3887
3888
3889
3890
3891
3892
3893
3894
3895
3896
3897
3898
3899
3900
3901
3902
3903
3904
3905
3906
3907
3908
3909
3910
3911
3912
3913
3914
3915
3916
3917
3918
3919
3920
3921
3922
3923
3924
3925
3926
3927
3928
3929
3930
3931
3932
3933
3934
3935
3936
3937
3938
3939
3940
3941
3942
3943
3944
3945
3946
3947
3948
3949
3950
3951
3952
3953
3954
3955
3956
3957
3958
3959
3960
3961
3962
3963
3964
3965
3966
3967
3968
3969
3970
3971
3972
3973
3974
3975
3976
3977
3978
3979
3980
3981
3982
3983
3984
3985
3986
3987
3988
3989
3990
3991
3992
3993
3994
3995
3996
3997
3998
3999
4000
4001
4002
4003
4004
4005
4006
4007
4008
4009
4010
4011
4012
4013
4014
4015
4016
4017
4018
4019
4020
4021
4022
4023
4024
4025
4026
4027
4028
4029
4030
4031
4032
4033
4034
4035
4036
4037
4038
4039
4040
4041
4042
4043
4044
4045
4046
4047
4048
4049
4050
4051
4052
4053
4054
4055
4056
4057
4058
4059
4060
4061
4062
4063
4064
4065
4066
4067
4068
4069
4070
4071
4072
4073
4074
4075
4076
4077
4078
4079
4080
4081
4082
4083
4084
4085
4086
4087
4088
4089
4090
4091
4092
4093
4094
4095
4096
4097
4098
4099
4100
4101
4102
4103
4104
4105
4106
4107
4108
4109
4110
4111
4112
4113
4114
4115
4116
4117
4118
4119
4120
4121
4122
4123
4124
4125
4126
4127
4128
4129
4130
4131
4132
4133
4134
4135
4136
4137
4138
4139
4140
4141
4142
4143
4144
4145
4146
4147
4148
4149
4150
4151
4152
4153
4154
4155
4156
4157
4158
4159
4160
4161
4162
4163
4164
4165
4166
4167
4168
4169
4170
4171
4172
4173
4174
4175
4176
4177
4178
4179
4180
4181
4182
4183
4184
4185
4186
4187
4188
4189
4190
4191
4192
4193
4194
4195
4196
4197
4198
4199
4200
4201
4202
4203
4204
4205
4206
4207
4208
4209
4210
4211
4212
4213
4214
4215
4216
4217
4218
4219
4220
4221
4222
4223
4224
4225
4226
4227
4228
4229
4230
4231
4232
4233
4234
4235
4236
4237
4238
4239
4240
4241
4242
4243
4244
4245
4246
4247
4248
4249
4250
4251
4252
4253
4254
4255
4256
4257
4258
4259
4260
4261
4262
4263
4264
4265
4266
4267
4268
4269
4270
4271
4272
4273
4274
4275
4276
4277
4278
4279
4280
4281
4282
4283
4284
4285
4286
4287
4288
4289
4290
4291
4292
4293
4294
4295
4296
4297
4298
4299
4300
4301
4302
4303
4304
4305
4306
4307
4308
4309
4310
4311
4312
4313
4314
4315
4316
4317
4318
4319
4320
4321
4322
4323
4324
4325
4326
4327
4328
4329
4330
4331
4332
4333
4334
4335
4336
4337
4338
4339
4340
4341
4342
4343
4344
4345
4346
4347
4348
4349
4350
4351
4352
4353
4354
4355
4356
4357
4358
4359
4360
4361
4362
4363
4364
4365
4366
4367
4368
4369
4370
4371
4372
4373
4374
4375
4376
4377
4378
4379
4380
4381
4382
4383
4384
4385
4386
4387
4388
4389
4390
4391
4392
4393
4394
4395
4396
4397
4398
4399
4400
4401
4402
4403
4404
4405
4406
4407
4408
4409
4410
4411
4412
4413
4414
4415
4416
4417
4418
4419
4420
4421
4422
4423
4424
4425
4426
4427
4428
4429
4430
4431
4432
4433
4434
4435
4436
4437
4438
4439
4440
4441
4442
4443
4444
4445
4446
4447
4448
4449
4450
4451
4452
4453
4454
4455
4456
4457
4458
4459
4460
4461
4462
4463
4464
4465
4466
4467
4468
4469
4470
4471
4472
4473
4474
4475
4476
4477
4478
4479
4480
4481
4482
4483
4484
4485
4486
4487
4488
4489
4490
4491
4492
4493
4494
4495
4496
4497
4498
4499
4500
4501
4502
4503
4504
4505
4506
4507
4508
4509
4510
4511
4512
4513
4514
4515
4516
4517
4518
4519
4520
4521
4522
4523
4524
4525
4526
4527
4528
4529
4530
4531
4532
4533
4534
4535
4536
4537
4538
4539
4540
4541
4542
4543
4544
4545
4546
4547
4548
4549
4550
4551
4552
4553
4554
4555
4556
4557
4558
4559
4560
4561
4562
4563
4564
4565
4566
4567
4568
4569
4570
4571
4572
4573
4574
4575
4576
4577
4578
4579
4580
4581
4582
4583
4584
4585
4586
4587
4588
4589
4590
4591
4592
4593
4594
4595
4596
4597
4598
4599
4600
4601
4602
4603
4604
4605
4606
4607
4608
4609
4610
4611
4612
4613
4614
4615
4616
4617
4618
4619
4620
4621
4622
4623
4624
4625
4626
4627
4628
4629
4630
4631
4632
4633
4634
4635
4636
4637
4638
4639
4640
4641
4642
4643
4644
4645
4646
4647
4648
4649
4650
4651
4652
4653
4654
4655
4656
4657
4658
4659
4660
4661
4662
4663
4664
4665
4666
4667
4668
4669
4670
4671
4672
4673
4674
4675
4676
4677
4678
4679
4680
4681
4682
4683
4684
4685
4686
4687
4688
4689
4690
4691
4692
4693
4694
4695
4696
4697
4698
4699
4700
4701
4702
4703
4704
4705
4706
4707
4708
4709
4710
4711
4712
4713
4714
4715
4716
4717
4718
4719
4720
4721
4722
4723
4724
4725
4726
4727
4728
4729
4730
4731
4732
4733
4734
4735
4736
4737
4738
4739
4740
4741
4742
4743
4744
4745
4746
4747
4748
4749
4750
4751
4752
4753
4754
4755
4756
4757
4758
4759
4760
4761
4762
4763
4764
4765
4766
4767
4768
4769
4770
4771
4772
4773
4774
4775
4776
4777
4778
4779
4780
4781
4782
4783
4784
4785
4786
4787
4788
4789
4790
4791
4792
4793
4794
4795
4796
4797
4798
4799
4800
4801
4802
4803
4804
4805
4806
4807
4808
4809
4810
4811
4812
4813
4814
4815
4816
4817
4818
4819
4820
4821
4822
4823
4824
4825
4826
4827
4828
4829
4830
4831
4832
4833
4834
4835
4836
4837
4838
4839
4840
4841
4842
4843
4844
4845
4846
4847
4848
4849
4850
4851
4852
4853
4854
4855
4856
4857
4858
4859
4860
4861
4862
4863
4864
4865
4866
4867
4868
4869
4870
4871
4872
4873
4874
4875
4876
4877
4878
4879
4880
4881
4882
4883
4884
4885
4886
4887
4888
4889
4890
4891
4892
4893
4894
4895
4896
4897
4898
4899
4900
4901
4902
4903
4904
4905
4906
4907
4908
4909
4910
4911
4912
4913
4914
4915
4916
4917
4918
4919
4920
4921
4922
4923
4924
4925
4926
4927
4928
4929
4930
4931
4932
4933
4934
4935
4936
4937
4938
4939
4940
4941
4942
4943
4944
4945
4946
4947
4948
4949
4950
4951
4952
4953
4954
4955
4956
4957
4958
4959
4960
4961
4962
4963
4964
4965
4966
4967
4968
4969
4970
4971
4972
4973
4974
4975
4976
4977
4978
4979
4980
4981
4982
4983
4984
4985
4986
4987
4988
4989
4990
4991
4992
4993
4994
4995
4996
4997
4998
4999
5000
5001
5002
5003
5004
5005
5006
5007
5008
5009
5010
5011
5012
5013
5014
5015
5016
5017
5018
5019
5020
5021
5022
5023
5024
5025
5026
5027
5028
5029
5030
5031
5032
5033
5034
5035
5036
5037
5038
5039
5040
5041
5042
5043
5044
5045
5046
5047
5048
5049
5050
5051
5052
5053
5054
5055
5056
5057
5058
5059
5060
5061
5062
5063
5064
5065
5066
5067
5068
5069
5070
5071
5072
5073
5074
5075
5076
5077
5078
5079
5080
5081
5082
5083
5084
5085
5086
5087
5088
5089
5090
5091
5092
5093
5094
5095
5096
5097
5098
5099
5100
5101
5102
5103
5104
5105
5106
5107
5108
5109
//! Content-addressable repository for composefs objects.
//!
//! This module provides a repository abstraction for storing and retrieving
//! content-addressed objects, splitstreams, and images with fs-verity
//! verification and garbage collection support.
//!
//! # Repository Layout
//!
//! A composefs repository is a directory with the following structure:
//!
//! ```text
//! repository/
//! ├── objects/                  # Content-addressed object storage
//! │   ├── 4e/                   # First byte of fs-verity hash (hex)
//! │   │   └── 67eaccd9fd...     # Remaining bytes of hash
//! │   └── ...
//! ├── images/                   # Composefs (erofs) image tracking
//! │   ├── 4e67eaccd9fd... → ../objects/4e/67eaccd9fd...
//! │   └── refs/
//! │       └── myimage → ../../4e67eaccd9fd...
//! └── streams/                  # Splitstream storage
//!     ├── oci-config-sha256:... → ../objects/XX/YYY...
//!     ├── oci-layer-sha256:... → ../objects/XX/YYY...
//!     └── refs/                 # Named references (GC roots)
//!         └── mytarball → ../../oci-layer-sha256:...
//! ```
//!
//! # Object Storage
//!
//! All content is stored in `objects/` using fs-verity hashes as filenames,
//! split into 256 subdirectories (`00`-`ff`) by the first byte for filesystem
//! efficiency. Objects are immutable and deduplicated by content. Every file
//! must have fs-verity enabled (except in "insecure" mode).
//!
//! # Images vs Streams
//!
//! The repository distinguishes between two types of derived content:
//!
//! - **Images** (`images/`): Composefs/erofs filesystem images that can be mounted.
//!   These are tracked separately for security: only images produced by the repository
//!   (via mkcomposefs) should be mounted, to avoid exposing the kernel's filesystem
//!   code to untrusted data.
//!
//! - **Streams** (`streams/`): Splitstreams storing arbitrary data (e.g., OCI
//!   image layers and configs). Symlinks map content identifiers to objects.
//!
//! # References (GC Roots)
//!
//! Both `images/refs/` and `streams/refs/` contain named symlinks that serve as
//! garbage collection roots. Any object reachable from a ref is protected from GC.
//! Refs can be organized hierarchically (e.g., `refs/myapp/layer1`).
//!
//! See [`Repository::name_stream`] for creating stream refs.
//!
//! # Garbage Collection
//!
//! The repository supports garbage collection via [`Repository::gc()`]. Objects
//! not reachable from any reference are deleted. The GC algorithm:
//!
//! 1. Walks all references in `images/refs/` and `streams/refs/` to find roots
//! 2. Transitively follows stream references to find all reachable objects
//! 3. Deletes unreferenced objects, images, and streams
//!
//! # fs-verity Integration
//!
//! When running on a filesystem that supports fs-verity (ext4, btrfs, etc.), objects
//! are stored with fs-verity enabled, providing kernel-level integrity verification.
//! In "insecure" mode, fs-verity is not required, allowing operation on filesystems
//! like tmpfs or overlayfs.
//!
//! # Concurrency
//!
//! The repository uses advisory file locking (flock) to coordinate concurrent access.
//! Opening a repository acquires a shared lock, while garbage collection requires
//! an exclusive lock. This ensures GC cannot run while other processes have the
//! repository open.
//!
//! For more details, see the [repository design documentation](../../../doc/repository.md).

use std::{
    collections::{HashMap, HashSet},
    ffi::{CStr, CString, OsStr, OsString},
    fmt,
    fs::{File, canonicalize},
    io::{BufRead, Read, Write},
    os::{
        fd::{AsFd, BorrowedFd, OwnedFd},
        unix::ffi::OsStrExt,
    },
    path::{Path, PathBuf},
    sync::Arc,
    thread::available_parallelism,
};

use log::{debug, trace};
use tokio::sync::Semaphore;

use anyhow::{Context, Result, bail, ensure};
use fn_error_context::context;
use once_cell::sync::OnceCell;
use rustix::{
    fs::{
        Access, AtFlags, CWD, Dir, FileType, FlockOperation, Mode, OFlags, StatVfsMountFlags,
        accessat, flock, fstatvfs, linkat, mkdirat, openat, readlinkat, statat, syncfs, unlinkat,
    },
    io::{Errno, Result as ErrnoResult},
};

use crate::{
    fsverity::{
        Algorithm, CompareVerityError, DEFAULT_LG_BLOCKSIZE, EnableVerityError, FsVerityHashValue,
        FsVerityHasher, MeasureVerityError, compute_verity, enable_verity_maybe_copy,
        ensure_verity_equal, has_verity, measure_verity, measure_verity_opt,
    },
    mount::{composefs_fsmount, mount_at},
    shared_internals::IO_BUF_CAPACITY,
    splitstream::{SplitStreamReader, SplitStreamWriter},
    util::{ErrnoFilter, proc_self_fd, reopen_tmpfile_ro, replace_symlinkat},
};

/// The filename used for repository metadata.
pub const REPO_METADATA_FILENAME: &str = "meta.json";

/// Errors that can occur when opening a repository.
#[derive(Debug, thiserror::Error)]
pub enum RepositoryOpenError {
    /// `meta.json` is missing and the directory does not appear to be
    /// an existing repository.
    #[error(
        "{REPO_METADATA_FILENAME} not found; this repository must be initialized with `cfsctl init`"
    )]
    MetadataMissing,
    /// `meta.json` is missing but `objects/` exists, indicating an
    /// old-format repository that predates `meta.json`.
    #[error(
        "{REPO_METADATA_FILENAME} not found; this appears to be an old-format repository — use Repository::open_upgrade() or `cfsctl init` to migrate"
    )]
    OldFormatRepository,
    /// `meta.json` exists but could not be parsed.
    #[error("failed to parse {REPO_METADATA_FILENAME}")]
    MetadataInvalid(#[source] serde_json::Error),
    /// The algorithm in `meta.json` does not match the expected type.
    #[error("repository algorithm {found} does not match expected {expected}")]
    AlgorithmMismatch {
        /// The algorithm found in `meta.json`.
        found: Algorithm,
        /// The algorithm expected for this repository type.
        expected: Algorithm,
    },
    /// The repository format version is newer than this tool supports.
    #[error(
        "unsupported repository format version {found} (this tool supports up to {REPO_FORMAT_VERSION})"
    )]
    UnsupportedVersion {
        /// The version found in `meta.json`.
        found: u32,
    },
    /// The repository requires features this tool does not understand.
    #[error("repository requires unknown incompatible features: {0:?}")]
    IncompatibleFeatures(Vec<String>),
    /// An I/O error occurred while opening or probing the repository.
    #[error(transparent)]
    Io(std::io::Error),
}

impl From<Errno> for RepositoryOpenError {
    fn from(e: Errno) -> Self {
        Self::Io(e.into())
    }
}

impl From<std::io::Error> for RepositoryOpenError {
    fn from(e: std::io::Error) -> Self {
        Self::Io(e)
    }
}

/// The current repository format version.
///
/// This is a simple integer that is bumped only for fundamental,
/// incompatible changes to the repository layout.  Finer-grained
/// evolution uses the [`FeatureFlags`] system instead.
pub const REPO_FORMAT_VERSION: u32 = 1;

/// Set of feature flags understood by this version of the code.
///
/// When reading a repository whose metadata lists features not in
/// these sets, the rules are:
///
/// - Unknown **compatible** features are silently ignored.
/// - Unknown **read-only compatible** features allow read operations
///   but prevent any writes (adding objects, creating images, GC, …).
/// - Unknown **incompatible** features cause the repository to be
///   rejected entirely.
///
/// There are currently no defined features.
pub mod known_features {
    /// Compatible features understood by this version.
    pub const COMPAT: &[&str] = &[];
    /// Read-only compatible features understood by this version.
    pub const RO_COMPAT: &[&str] = &[];
    /// Incompatible features understood by this version.
    pub const INCOMPAT: &[&str] = &[];
}

/// Feature flags for a composefs repository.
///
/// Inspired by the ext4/XFS/EROFS on-disk feature model:
///
/// - **compatible**: old tools that don't understand these can still
///   fully read and write the repository.
/// - **read_only_compatible**: old tools can read but must not write.
/// - **incompatible**: old tools must refuse to open the repository.
#[derive(Debug, Clone, Default, PartialEq, Eq, serde::Serialize, serde::Deserialize)]
#[serde(rename_all = "kebab-case")]
pub struct FeatureFlags {
    /// Features that can be safely ignored by older tools.
    #[serde(default)]
    pub compatible: Vec<String>,

    /// Features that allow reading but prevent writing by older tools.
    #[serde(default)]
    pub read_only_compatible: Vec<String>,

    /// Features that require newer tools; older tools must refuse entirely.
    #[serde(default)]
    pub incompatible: Vec<String>,
}

/// Result of checking repository feature compatibility.
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum FeatureCheck {
    /// All features are understood; full read-write access.
    ReadWrite,
    /// Unknown read-only-compatible features present; read access only.
    /// The vec contains the unknown feature names.
    ReadOnly(Vec<String>),
}

impl FeatureFlags {
    /// Check these flags against the known feature sets.
    ///
    /// Returns an error if any unknown incompatible features are present.
    /// Returns [`FeatureCheck::ReadOnly`] if unknown ro-compat features
    /// are present. Returns [`FeatureCheck::ReadWrite`] otherwise.
    pub fn check(&self) -> Result<FeatureCheck, RepositoryOpenError> {
        // Check incompatible features first
        let unknown_incompat: Vec<String> = self
            .incompatible
            .iter()
            .filter(|f| !known_features::INCOMPAT.contains(&f.as_str()))
            .cloned()
            .collect();
        if !unknown_incompat.is_empty() {
            return Err(RepositoryOpenError::IncompatibleFeatures(unknown_incompat));
        }

        // Check ro-compat features
        let unknown_ro: Vec<String> = self
            .read_only_compatible
            .iter()
            .filter(|f| !known_features::RO_COMPAT.contains(&f.as_str()))
            .cloned()
            .collect();
        if !unknown_ro.is_empty() {
            return Ok(FeatureCheck::ReadOnly(unknown_ro));
        }

        // Compatible features are ignored by definition
        Ok(FeatureCheck::ReadWrite)
    }
}

/// Repository metadata stored in `meta.json` at the repository root.
///
/// This file records the repository's format version, digest algorithm,
/// and feature flags so that tools can detect misconfigured invocations
/// (e.g. opening a sha256 repo with `--hash sha512`) and so the
/// algorithm doesn't need to be specified on every command.
///
/// The versioning model is inspired by Linux filesystem superblocks
/// (ext4, XFS, EROFS): a base version integer for fundamental layout
/// changes, plus three tiers of feature flags for finer-grained
/// evolution.
#[derive(Debug, Clone, PartialEq, Eq, serde::Serialize, serde::Deserialize)]
pub struct RepoMetadata {
    /// Base repository format version.  Tools must refuse to operate
    /// on a repository whose version exceeds what they understand.
    pub version: u32,

    /// The fs-verity algorithm configuration for this repository.
    pub algorithm: Algorithm,

    /// Feature flags.
    #[serde(default)]
    pub features: FeatureFlags,
}

impl RepoMetadata {
    /// Build metadata for a repository using the given hash type.
    pub fn for_hash<ObjectID: FsVerityHashValue>() -> Self {
        Self {
            version: REPO_FORMAT_VERSION,
            algorithm: Algorithm::for_hash::<ObjectID>(),
            features: FeatureFlags::default(),
        }
    }

    /// Build metadata from an explicit [`Algorithm`].
    pub fn new(algorithm: Algorithm) -> Self {
        Self {
            version: REPO_FORMAT_VERSION,
            algorithm,
            features: FeatureFlags::default(),
        }
    }

    /// Check whether this metadata is compatible with the given hash type.
    ///
    /// Validates the base version, feature flags, and algorithm.
    /// Returns a [`FeatureCheck`] indicating read-write or read-only access.
    pub fn check_compatible<ObjectID: FsVerityHashValue>(
        &self,
    ) -> Result<FeatureCheck, RepositoryOpenError> {
        if self.version > REPO_FORMAT_VERSION {
            return Err(RepositoryOpenError::UnsupportedVersion {
                found: self.version,
            });
        }
        if !self.algorithm.is_compatible::<ObjectID>() {
            return Err(RepositoryOpenError::AlgorithmMismatch {
                found: self.algorithm,
                expected: Algorithm::for_hash::<ObjectID>(),
            });
        }
        let access = self.features.check()?;
        Ok(access)
    }

    /// Serialize to pretty-printed JSON with a trailing newline.
    pub fn to_json(&self) -> Result<Vec<u8>> {
        let mut buf = serde_json::to_vec_pretty(self).context("serializing repository metadata")?;
        buf.push(b'\n');
        Ok(buf)
    }

    /// Deserialize from JSON bytes.
    #[context("Parsing repository metadata JSON")]
    pub fn from_json(data: &[u8]) -> Result<Self> {
        serde_json::from_slice(data).context("deserializing repository metadata")
    }
}

/// Read the fs-verity algorithm from a repository's `meta.json`.
///
/// This is the public API for determining which algorithm a repository
/// uses before opening it (needed to choose the correct `ObjectID`
/// generic parameter for [`Repository::open_path`]).
///
/// Returns `Ok(None)` when `meta.json` is absent.
#[context("Reading repository algorithm")]
pub fn read_repo_algorithm(repo_fd: &impl AsFd) -> Result<Option<Algorithm>> {
    Ok(read_repo_metadata(repo_fd)?.map(|m| m.algorithm))
}

/// Read `meta.json` from a repository directory fd, if it exists.
///
/// Returns `Ok(None)` when the file is absent.
#[context("Reading repository metadata")]
pub(crate) fn read_repo_metadata(repo_fd: &impl AsFd) -> Result<Option<RepoMetadata>> {
    match openat(
        repo_fd,
        REPO_METADATA_FILENAME,
        OFlags::RDONLY | OFlags::CLOEXEC,
        Mode::empty(),
    ) {
        Ok(fd) => {
            let meta = serde_json::from_reader(std::io::BufReader::new(File::from(fd)))
                .context("parsing meta.json")?;
            Ok(Some(meta))
        }
        Err(Errno::NOENT) => Ok(None),
        Err(e) => Err(e).context("opening meta.json")?,
    }
}

/// Enable fs-verity on an fd, dispatching to the correct hash type
/// based on the [`Algorithm`].
fn enable_verity_for_algorithm(
    dirfd: &impl AsFd,
    fd: BorrowedFd,
    algorithm: &Algorithm,
) -> Result<()> {
    match algorithm {
        Algorithm::Sha256 { .. } => {
            enable_verity_maybe_copy::<crate::fsverity::Sha256HashValue>(dirfd, fd)
                .context("enabling verity (sha256)")?;
        }
        Algorithm::Sha512 { .. } => {
            enable_verity_maybe_copy::<crate::fsverity::Sha512HashValue>(dirfd, fd)
                .context("enabling verity (sha512)")?;
        }
    }
    Ok(())
}

/// Remove algorithm-specific data from a repository directory.
///
/// Deletes `streams/`, `images/`, and `meta.json` but preserves
/// `objects/` (content-addressed blobs that are algorithm-agnostic).
/// This prepares the repository for re-initialization with a
/// (potentially different) algorithm via [`Repository::init_path`].
///
/// After calling this, streams and images will need to be re-imported.
#[context("Resetting repository metadata at {}", path.as_ref().display())]
pub fn reset_metadata(path: impl AsRef<Path>) -> Result<()> {
    let path = path.as_ref();
    for dir in ["streams", "images"] {
        let p = path.join(dir);
        if p.exists() {
            std::fs::remove_dir_all(&p).with_context(|| format!("removing {}", p.display()))?;
        }
    }
    let meta_path = path.join(REPO_METADATA_FILENAME);
    if meta_path.exists() {
        std::fs::remove_file(&meta_path)
            .with_context(|| format!("removing {}", meta_path.display()))?;
    }
    Ok(())
}

/// Return the default path for the user-owned composefs repository.
pub fn user_path() -> Result<PathBuf> {
    let home = std::env::var("HOME").with_context(|| "$HOME must be set when in user mode")?;
    Ok(PathBuf::from(home).join(".var/lib/composefs"))
}

/// Return the default path for the system-global composefs repository.
pub fn system_path() -> PathBuf {
    PathBuf::from("/sysroot/composefs")
}

/// Write `meta.json` into a repository directory fd.
///
/// This atomically writes (via O_TMPFILE + linkat) the metadata file.
/// It will fail if the file already exists.
///
/// If `enable_verity` is true, fs-verity is enabled on `meta.json`
/// before linking it into place.  This signals to future
/// [`Repository::open_path`] callers that verity is required on all
/// objects.
#[context("Writing repository metadata")]
pub(crate) fn write_repo_metadata(
    repo_fd: &impl AsFd,
    meta: &RepoMetadata,
    enable_verity: bool,
) -> Result<()> {
    let data = meta.to_json()?;

    // Try O_TMPFILE for atomic creation
    match openat(
        repo_fd,
        ".",
        OFlags::WRONLY | OFlags::TMPFILE | OFlags::CLOEXEC,
        Mode::from_raw_mode(0o644),
    ) {
        Ok(fd) => {
            let mut file = File::from(fd);
            file.write_all(&data)
                .context("writing metadata to tmpfile")?;
            file.sync_all().context("syncing metadata tmpfile")?;

            let ro_fd = reopen_tmpfile_ro(file).context("re-opening tmpfile read-only")?;

            if enable_verity {
                enable_verity_for_algorithm(repo_fd, ro_fd.as_fd(), &meta.algorithm)
                    .context("enabling verity on meta.json")?;
            }

            linkat(
                CWD,
                proc_self_fd(&ro_fd),
                repo_fd,
                REPO_METADATA_FILENAME,
                AtFlags::SYMLINK_FOLLOW,
            )
            .context("linking meta.json into repository")?;
        }
        Err(Errno::OPNOTSUPP | Errno::NOSYS) => {
            // Fallback: direct create (no tmpfs O_TMPFILE support).
            // Use O_EXCL to avoid overwriting, and fsync to ensure the
            // file is complete on disk before we consider init done.
            let fd = openat(
                repo_fd,
                REPO_METADATA_FILENAME,
                OFlags::WRONLY | OFlags::CREATE | OFlags::EXCL | OFlags::CLOEXEC,
                Mode::from_raw_mode(0o644),
            )
            .context("creating meta.json")?;
            let mut file = File::from(fd);
            file.write_all(&data).context("writing meta.json")?;
            file.sync_all().context("syncing meta.json to disk")?;

            if enable_verity {
                let ro_fd = openat(
                    repo_fd,
                    REPO_METADATA_FILENAME,
                    OFlags::RDONLY | OFlags::CLOEXEC,
                    Mode::empty(),
                )
                .context("re-opening meta.json for verity")?;
                drop(file);
                enable_verity_for_algorithm(repo_fd, ro_fd.as_fd(), &meta.algorithm)
                    .context("enabling verity on meta.json")?;
            }
        }
        Err(e) => {
            return Err(e).context("creating tmpfile for meta.json")?;
        }
    }
    Ok(())
}

/// Infer repository metadata by examining existing objects.
///
/// Walks `objects/` to find any stored object, determines the hash
/// algorithm from the filename length, and probes for fs-verity.
///
/// Returns `(Algorithm, has_verity)` or an error if the objects
/// directory is empty or the algorithm can't be determined.
fn infer_metadata(repo_fd: &OwnedFd) -> Result<(Algorithm, bool)> {
    let objects_fd = openat(
        repo_fd,
        "objects",
        OFlags::RDONLY | OFlags::CLOEXEC,
        Mode::empty(),
    )
    .context("opening objects/ directory")?;

    let dir = Dir::read_from(&objects_fd).context("reading objects/ directory")?;

    for entry in dir {
        let entry = entry.context("reading objects/ directory entry")?;
        let subdir_name = entry.file_name().to_bytes();

        if subdir_name == b"." || subdir_name == b".." {
            continue;
        }

        // Each subdirectory should be a 2-char hex prefix
        if subdir_name.len() != 2 {
            continue;
        }

        let subdir_fd = openat(
            &objects_fd,
            entry.file_name(),
            OFlags::RDONLY | OFlags::CLOEXEC,
            Mode::empty(),
        )
        .with_context(|| {
            format!(
                "opening objects/{} subdirectory",
                entry.file_name().to_string_lossy()
            )
        })?;

        let subdir = Dir::read_from(&subdir_fd).context("reading object subdirectory")?;
        for obj_entry in subdir {
            let obj_entry = obj_entry.context("reading object subdirectory entry")?;
            let obj_name = obj_entry.file_name().to_bytes();

            if obj_name == b"." || obj_name == b".." {
                continue;
            }

            // Infer algorithm from filename length.
            // Objects are stored as objects/XX/<remaining_hex>, where XX is the first
            // byte (2 hex chars). The filename is the remaining bytes in hex.
            // SHA-256: 32 bytes total → 62 hex char filename
            // SHA-512: 64 bytes total → 126 hex char filename
            let algorithm = match obj_name.len() {
                62 => Algorithm::Sha256 {
                    lg_blocksize: DEFAULT_LG_BLOCKSIZE,
                },
                126 => Algorithm::Sha512 {
                    lg_blocksize: DEFAULT_LG_BLOCKSIZE,
                },
                _ => continue,
            };

            let obj_fd = openat(
                &subdir_fd,
                obj_entry.file_name(),
                OFlags::RDONLY | OFlags::CLOEXEC,
                Mode::empty(),
            )
            .with_context(|| {
                format!(
                    "opening object file {}",
                    obj_entry.file_name().to_string_lossy()
                )
            })?;

            let has_verity =
                has_verity(&obj_fd, algorithm).context("probing fs-verity on object")?;

            return Ok((algorithm, has_verity));
        }
    }

    bail!("no objects found in repository — cannot infer metadata");
}

/// Infer the repository algorithm by examining existing object filenames.
///
/// This is useful when `meta.json` is missing (old-format repos) and the
/// caller needs to determine the hash type before constructing a typed
/// [`Repository`].  For example, the CLI uses this to pick the correct
/// `ObjectID` generic parameter before calling [`Repository::open_upgrade`].
///
/// Returns the inferred [`Algorithm`], or an error if the objects
/// directory is empty or contains no recognizable filenames.
pub fn infer_repo_algorithm(repo_fd: &OwnedFd) -> Result<Algorithm> {
    Ok(infer_metadata(repo_fd)?.0)
}

/// How an object was stored in the repository.
///
/// Returned by [`Repository::ensure_object_from_file`] to indicate
/// whether the operation used zero-copy reflinks, a regular copy, or found
/// an existing object.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum ObjectStoreMethod {
    /// Object was stored via reflink (zero-copy, FICLONE ioctl).
    Reflinked,
    /// Object was stored via hardlink (zero-copy, source file linked directly).
    Hardlinked,
    /// Object was stored via regular file copy (reflink not supported).
    Copied,
    /// Object already existed in the repository (deduplicated).
    AlreadyPresent,
}

/// Per-operation context for [`Repository::ensure_object_from_file`].
///
/// Create one of these at the start of a bulk import operation (e.g. importing
/// all layers of a container image) and pass it to every
/// `ensure_object_from_file` call.  The context caches which
/// `(source_device, dest_device)` pairs do not support reflinks, so that
/// after the first `EOPNOTSUPP` / `EXDEV` on a given pair, subsequent
/// calls skip the FICLONE probe entirely.
///
/// This correctly handles multi-store imports where layers may come from
/// different filesystems: a reflink failure on ext4→xfs does not suppress
/// reflink attempts for a later xfs→xfs pair.
#[derive(Debug, Default)]
pub struct ImportContext {
    /// Device-ID pairs where FICLONE has already failed.  Stored as
    /// `(source_dev, dest_dev)` from `fstat().st_dev`.
    reflink_unsupported_devs: Vec<(u64, u64)>,
}

impl ImportContext {
    /// Check whether reflinks are known to be unsupported for this
    /// source→destination device pair.
    pub(crate) fn is_reflink_unsupported(&self, src_dev: u64, dst_dev: u64) -> bool {
        self.reflink_unsupported_devs
            .iter()
            .any(|&(s, d)| s == src_dev && d == dst_dev)
    }

    /// Record that reflinks are unsupported for this device pair.
    pub(crate) fn mark_reflink_unsupported(&mut self, src_dev: u64, dst_dev: u64) {
        if !self.is_reflink_unsupported(src_dev, dst_dev) {
            self.reflink_unsupported_devs.push((src_dev, dst_dev));
        }
    }
}

/// Call openat() on the named subdirectory of "dirfd", possibly creating it first.
///
/// We assume that the directory will probably exist (ie: we try the open first), and on ENOENT, we
/// mkdirat() and retry.
fn ensure_dir_and_openat(dirfd: impl AsFd, filename: &str, flags: OFlags) -> ErrnoResult<OwnedFd> {
    match openat(
        &dirfd,
        filename,
        flags | OFlags::CLOEXEC | OFlags::DIRECTORY,
        0o666.into(),
    ) {
        Ok(file) => Ok(file),
        Err(Errno::NOENT) => match mkdirat(&dirfd, filename, 0o777.into()) {
            Ok(()) | Err(Errno::EXIST) => openat(
                dirfd,
                filename,
                flags | OFlags::CLOEXEC | OFlags::DIRECTORY,
                0o666.into(),
            ),
            Err(other) => Err(other),
        },
        Err(other) => Err(other),
    }
}

/// Create a directory under `dirfd` if it doesn't already exist.
///
/// Returns `Ok(())` on success or if the directory already exists.
/// Propagates all other errors from `mkdirat`.
fn ensure_dir_at(dirfd: impl AsFd, path: &str, mode: Mode) -> ErrnoResult<()> {
    match mkdirat(dirfd, path, mode) {
        Ok(()) | Err(Errno::EXIST) => Ok(()),
        Err(e) => Err(e),
    }
}

/// A zero-sized proof token confirming that a [`Repository`] is writable.
///
/// Obtained by calling [`Repository::ensure_writable`], which performs a fast
/// `faccessat(2)` check.  Write methods on `Repository` require this token
/// internally so that the writable check is performed exactly once per
/// top-level operation rather than at every leaf call.
///
/// Because the type is zero-sized, passing it around has no runtime cost.
#[derive(Debug, Clone, Copy)]
pub(crate) struct WritableRepo;

/// A content-addressable repository for composefs objects.
///
/// Stores content-addressed objects, splitstreams, and images with fsverity
/// verification. Objects are stored by their fsverity digest, streams by SHA256
/// content hash, and both support named references for persistence across
/// garbage collection.
pub struct Repository<ObjectID: FsVerityHashValue> {
    repository: OwnedFd,
    objects: OnceCell<OwnedFd>,
    write_semaphore: OnceCell<Arc<Semaphore>>,
    insecure: bool,
    metadata: RepoMetadata,
    /// When true, SplitStreamWriter::done() writes old-format (pre-repr(C))
    /// headers. Used to test backward compatibility with splitstreams
    /// written before #[repr(C)] was added to SplitstreamHeader.
    #[cfg(any(test, feature = "test"))]
    write_old_splitstream_format: std::sync::atomic::AtomicBool,
    _data: std::marker::PhantomData<ObjectID>,
}

impl<ObjectID: FsVerityHashValue> std::fmt::Debug for Repository<ObjectID> {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        f.debug_struct("Repository")
            .field("repository", &self.repository)
            .field("objects", &self.objects)
            .field("insecure", &self.insecure)
            .finish_non_exhaustive()
    }
}

impl<ObjectID: FsVerityHashValue> Drop for Repository<ObjectID> {
    fn drop(&mut self) {
        flock(&self.repository, FlockOperation::Unlock).expect("repository unlock failed");
    }
}

/// For Repository::gc_category
enum GCCategoryWalkMode {
    RefsOnly,
    AllEntries,
}

/// Statistics from a garbage collection operation.
///
/// Returned by [`Repository::gc`] to report what was (or would be) removed.
#[derive(Debug, Clone, Default, PartialEq, Eq)]
pub struct GcResult {
    /// Number of unreferenced objects removed (or that would be removed)
    pub objects_removed: u64,
    /// Total bytes of object data removed (or that would be removed)
    pub objects_bytes: u64,
    /// Number of broken symlinks removed in images/
    pub images_pruned: u64,
    /// Number of broken symlinks removed in streams/
    pub streams_pruned: u64,
}

/// A structured error found during a filesystem consistency check.
///
/// Each variant corresponds to a specific kind of repository integrity problem.
/// The `Display` implementation produces a kebab-case error type prefix followed
/// by the path/context and any relevant details, suitable for both human display
/// and structured logging.
#[derive(Debug, Clone, serde::Serialize, thiserror::Error)]
#[serde(tag = "type", rename_all = "kebab-case")]
#[non_exhaustive]
#[allow(missing_docs)]
pub enum FsckError {
    #[error("fsck: object-invalid-name: {path}: {detail}")]
    ObjectInvalidName { path: String, detail: String },

    #[error("fsck: object-open-failed: {path}: {detail}")]
    ObjectOpenFailed { path: String, detail: String },

    #[error("fsck: object-digest-mismatch: {path}: measured {measured}")]
    ObjectDigestMismatch { path: String, measured: String },

    #[error("fsck: object-verity-failed: {path}: {detail}")]
    ObjectVerityFailed { path: String, detail: String },

    #[error("fsck: object-verity-missing: {path}")]
    ObjectVerityMissing { path: String },

    #[error("fsck: entry-not-symlink: {path}")]
    EntryNotSymlink { path: String },

    #[error("fsck: broken-symlink: {path}")]
    BrokenSymlink { path: String },

    #[error("fsck: stat-failed: {path}: {detail}")]
    StatFailed { path: String, detail: String },

    #[error("fsck: unexpected-file-type: {path}: {detail}")]
    UnexpectedFileType { path: String, detail: String },

    #[error("fsck: stream-open-failed: {path}: {detail}")]
    StreamOpenFailed { path: String, detail: String },

    #[error("fsck: missing-object-ref: {path}: {object_id}")]
    #[serde(rename_all = "camelCase")]
    MissingObjectRef { path: String, object_id: String },

    #[error("fsck: stream-read-failed: {path}: {detail}")]
    StreamReadFailed { path: String, detail: String },

    #[error("fsck: missing-named-ref: {path}: ref {ref_name}: {object_id}")]
    #[serde(rename_all = "camelCase")]
    MissingNamedRef {
        path: String,
        ref_name: String,
        object_id: String,
    },

    #[error("fsck: object-check-failed: {path}: {object_id}: {detail}")]
    #[serde(rename_all = "camelCase")]
    ObjectCheckFailed {
        path: String,
        object_id: String,
        detail: String,
    },

    #[error("fsck: image-open-failed: {path}: {detail}")]
    ImageOpenFailed { path: String, detail: String },

    #[error("fsck: image-read-failed: {path}: {detail}")]
    ImageReadFailed { path: String, detail: String },

    #[error("fsck: image-invalid: {path}: {detail}")]
    ImageInvalid { path: String, detail: String },

    #[error("fsck: image-missing-object: {path}: {object_id}")]
    #[serde(rename_all = "camelCase")]
    ImageMissingObject { path: String, object_id: String },

    #[error("fsck: metadata-parse-failed: meta.json: {detail}")]
    MetadataParseFailed { detail: String },

    #[error(
        "fsck: metadata-algorithm-mismatch: meta.json: expected {expected}, repository opened as {actual}"
    )]
    MetadataAlgorithmMismatch { expected: String, actual: String },
}

/// Results from a filesystem consistency check.
///
/// Returned by [`Repository::fsck`] to report repository integrity status.
#[derive(Debug, Clone, Default, serde::Serialize)]
#[serde(rename_all = "camelCase")]
pub struct FsckResult {
    pub(crate) has_metadata: bool,
    pub(crate) objects_checked: u64,
    pub(crate) objects_corrupted: u64,
    pub(crate) streams_checked: u64,
    pub(crate) streams_corrupted: u64,
    pub(crate) images_checked: u64,
    pub(crate) images_corrupted: u64,
    pub(crate) broken_links: u64,
    pub(crate) missing_objects: u64,
    pub(crate) errors: Vec<FsckError>,
}

impl FsckResult {
    /// Whether the repository has a `meta.json` file.
    pub fn has_metadata(&self) -> bool {
        self.has_metadata
    }

    /// Returns true if no corruption or errors were found.
    pub fn is_ok(&self) -> bool {
        debug_assert!(
            self.objects_corrupted == 0
                && self.streams_corrupted == 0
                && self.images_corrupted == 0
                && self.broken_links == 0
                && self.missing_objects == 0
                || !self.errors.is_empty(),
            "corruption counters are non-zero but no error messages recorded"
        );
        self.errors.is_empty()
    }

    /// Number of objects verified.
    pub fn objects_checked(&self) -> u64 {
        self.objects_checked
    }

    /// Number of objects with bad fsverity digests.
    pub fn objects_corrupted(&self) -> u64 {
        self.objects_corrupted
    }

    /// Number of streams verified.
    pub fn streams_checked(&self) -> u64 {
        self.streams_checked
    }

    /// Number of streams with issues (bad header, missing refs, etc.).
    pub fn streams_corrupted(&self) -> u64 {
        self.streams_corrupted
    }

    /// Number of images verified.
    pub fn images_checked(&self) -> u64 {
        self.images_checked
    }

    /// Number of images with issues.
    pub fn images_corrupted(&self) -> u64 {
        self.images_corrupted
    }

    /// Number of broken symlinks found.
    pub fn broken_links(&self) -> u64 {
        self.broken_links
    }

    /// Number of missing objects referenced by streams.
    pub fn missing_objects(&self) -> u64 {
        self.missing_objects
    }

    /// Errors found during the check.
    pub fn errors(&self) -> &[FsckError] {
        &self.errors
    }
}

impl fmt::Display for FsckResult {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        let metadata_errors = self.errors.iter().any(|e| {
            matches!(
                e,
                FsckError::MetadataParseFailed { .. } | FsckError::MetadataAlgorithmMismatch { .. }
            )
        });
        if metadata_errors {
            writeln!(f, "meta.json: error")?;
        } else if self.has_metadata {
            writeln!(f, "meta.json: ok")?;
        } else {
            writeln!(f, "meta.json: absent")?;
        }
        writeln!(
            f,
            "objects: {}/{} ok",
            self.objects_checked.saturating_sub(self.objects_corrupted),
            self.objects_checked
        )?;
        writeln!(
            f,
            "streams: {}/{} ok",
            self.streams_checked.saturating_sub(self.streams_corrupted),
            self.streams_checked
        )?;
        writeln!(
            f,
            "images: {}/{} ok",
            self.images_checked.saturating_sub(self.images_corrupted),
            self.images_checked
        )?;
        if self.broken_links > 0 {
            writeln!(f, "broken symlinks: {}", self.broken_links)?;
        }
        if self.missing_objects > 0 {
            writeln!(f, "missing objects: {}", self.missing_objects)?;
        }
        if self.errors.is_empty() {
            writeln!(f, "status: ok")?;
        } else {
            writeln!(f, "status: {} error(s)", self.errors.len())?;
            for err in &self.errors {
                writeln!(f, "  - {err}")?;
            }
        }
        Ok(())
    }
}

impl<ObjectID: FsVerityHashValue> Repository<ObjectID> {
    /// Enable or disable writing old-format splitstream headers.
    ///
    /// When enabled, all splitstreams created via [`create_stream`] will be
    /// written with the pre-`repr(C)` header layout, simulating data written
    /// by composefs-rs versions before the `#[repr(C)]` fix.
    #[cfg(any(test, feature = "test"))]
    pub fn set_write_old_splitstream_format(&self, enabled: bool) {
        self.write_old_splitstream_format
            .store(enabled, std::sync::atomic::Ordering::Relaxed);
    }

    /// Whether splitstream writers should use the old (pre-repr(C)) header format.
    #[cfg(any(test, feature = "test"))]
    pub(crate) fn write_old_splitstream_format(&self) -> bool {
        self.write_old_splitstream_format
            .load(std::sync::atomic::Ordering::Relaxed)
    }

    /// Return the objects directory.
    pub fn objects_dir(&self) -> ErrnoResult<&OwnedFd> {
        self.objects
            .get_or_try_init(|| ensure_dir_and_openat(&self.repository, "objects", OFlags::PATH))
    }

    /// Return a shared semaphore for limiting concurrent object writes.
    ///
    /// This semaphore is lazily initialized with `available_parallelism()` permits,
    /// and shared across all operations on this repository. Use this to limit
    /// concurrent I/O when processing multiple files or layers in parallel.
    pub fn write_semaphore(&self) -> Arc<Semaphore> {
        self.write_semaphore
            .get_or_init(|| {
                let max_concurrent = available_parallelism().map(|n| n.get()).unwrap_or(4);
                Arc::new(Semaphore::new(max_concurrent))
            })
            .clone()
    }

    /// Initialize a new repository at the target path and open it.
    ///
    /// Creates the directory (mode 0700) if it does not exist, writes
    /// `meta.json` for the given `algorithm`, and returns the opened
    /// repository together with a flag indicating whether this was a
    /// fresh initialization (`true`) or an idempotent open of an
    /// existing repository with the same algorithm (`false`).
    ///
    /// The `algorithm` must be compatible with this repository's
    /// `ObjectID` type (e.g. `Algorithm::Sha512` for
    /// `Repository<Sha512HashValue>`).
    ///
    /// If `enable_verity` is true, fs-verity is enabled on `meta.json`,
    /// signaling that all objects must also have verity.
    ///
    /// If `meta.json` already exists with a different algorithm, an
    /// error is returned.
    #[context("Initializing repository at {}", path.as_ref().display())]
    pub fn init_path(
        dirfd: impl AsFd,
        path: impl AsRef<Path>,
        algorithm: Algorithm,
        enable_verity: bool,
    ) -> Result<(Self, bool)> {
        let path = path.as_ref();

        if !algorithm.is_compatible::<ObjectID>() {
            bail!(
                "algorithm {} is not compatible with this repository type (expected {})",
                algorithm,
                Algorithm::for_hash::<ObjectID>(),
            );
        }

        mkdirat(&dirfd, path, Mode::from_raw_mode(0o700))
            .or_else(|e| if e == Errno::EXIST { Ok(()) } else { Err(e) })
            .with_context(|| format!("creating repository directory {}", path.display()))?;

        let repo_fd = openat(
            &dirfd,
            path,
            OFlags::RDONLY | OFlags::CLOEXEC,
            Mode::empty(),
        )
        .with_context(|| format!("opening repository directory {}", path.display()))?;

        let meta = RepoMetadata::new(algorithm);

        // Try to write meta.json.  If it already exists, check for
        // idempotency: same algorithm is fine, different is an error.
        if let Err(write_err) = write_repo_metadata(&repo_fd, &meta, enable_verity) {
            match read_repo_metadata(&repo_fd)? {
                Some(existing) if existing == meta => {
                    // Idempotent: same config, already initialized.
                    let repo = Self::open_path(dirfd, path)?;
                    return Ok((repo, false));
                }
                Some(existing) => {
                    bail!(
                        "repository already initialized with algorithm '{}'; \
                         cannot re-initialize with '{}'",
                        existing.algorithm,
                        meta.algorithm,
                    );
                }
                None => {
                    // meta.json doesn't exist, so the write failure
                    // was something else — propagate original error.
                    return Err(write_err);
                }
            }
        }

        drop(repo_fd);
        let repo = Self::open_path(dirfd, path)?;
        Ok((repo, true))
    }

    /// Open a repository at the target directory and path.
    ///
    /// `meta.json` is read, parsed, and validated against this
    /// repository's `ObjectID` type.  Parsing or compatibility errors
    /// are propagated immediately so that broken metadata is never
    /// silently ignored.
    ///
    /// The repository's security mode is auto-detected: if `meta.json`
    /// has fs-verity enabled the repo requires verity on all objects
    /// (secure mode).  Otherwise the repository operates in insecure
    /// mode.  Use [`set_insecure`] to override after opening.
    pub fn open_path(
        dirfd: impl AsFd,
        path: impl AsRef<Path>,
    ) -> Result<Self, RepositoryOpenError> {
        let path = path.as_ref();

        // O_PATH isn't enough because flock()
        let repository = openat(dirfd, path, OFlags::RDONLY | OFlags::CLOEXEC, Mode::empty())?;

        flock(&repository, FlockOperation::LockShared)?;

        // Read, parse, and validate meta.json up front so that broken
        // or incompatible metadata is caught immediately rather than
        // being discovered lazily on first use.
        let (metadata, has_verity) = Self::read_and_probe_metadata(&repository)?;
        metadata.check_compatible::<ObjectID>()?;

        Ok(Self {
            repository,
            objects: OnceCell::new(),
            write_semaphore: OnceCell::new(),
            insecure: !has_verity,
            metadata,
            #[cfg(any(test, feature = "test"))]
            write_old_splitstream_format: std::sync::atomic::AtomicBool::new(false),
            _data: std::marker::PhantomData,
        })
    }

    /// Open a repository, upgrading old-format repos that lack `meta.json`.
    ///
    /// This method first tries [`open_path`](Self::open_path). If that fails
    /// with [`OldFormatRepository`](RepositoryOpenError::OldFormatRepository),
    /// it infers the algorithm and verity mode from existing objects,
    /// writes `meta.json`, and retries the open.
    ///
    /// This is the non-destructive upgrade path for repositories created
    /// by composefs-rs versions that predated `meta.json`.
    ///
    /// Returns `(repo, upgraded)` where `upgraded` is true if `meta.json`
    /// was written.
    pub fn open_upgrade(dirfd: impl AsFd, path: impl AsRef<Path>) -> Result<(Self, bool)> {
        let path = path.as_ref();

        match Self::open_path(&dirfd, path) {
            Ok(repo) => Ok((repo, false)),
            Err(RepositoryOpenError::OldFormatRepository) => {
                let repo_fd = openat(
                    &dirfd,
                    path,
                    OFlags::RDONLY | OFlags::CLOEXEC,
                    Mode::empty(),
                )
                .with_context(|| format!("opening repository directory {}", path.display()))?;

                let (algorithm, has_verity) = infer_metadata(&repo_fd)?;

                if !algorithm.is_compatible::<ObjectID>() {
                    bail!(
                        "inferred algorithm {} is not compatible with this repository type \
                         (expected {})",
                        algorithm,
                        Algorithm::for_hash::<ObjectID>(),
                    );
                }

                let meta = RepoMetadata::new(algorithm);
                write_repo_metadata(&repo_fd, &meta, has_verity)?;

                drop(repo_fd);

                let repo = Self::open_path(&dirfd, path)
                    .context("opening repository after writing meta.json")?;

                Ok((repo, true))
            }
            Err(other) => Err(other.into()),
        }
    }

    /// Read, parse, and probe verity on `meta.json`.
    ///
    /// Returns `Ok((metadata, has_verity))` when the file exists,
    /// and `Err` when absent or on I/O / parse failures.
    fn read_and_probe_metadata(
        repo_fd: &OwnedFd,
    ) -> Result<(RepoMetadata, bool), RepositoryOpenError> {
        let meta_fd = match openat(
            repo_fd,
            REPO_METADATA_FILENAME,
            OFlags::RDONLY | OFlags::CLOEXEC,
            Mode::empty(),
        ) {
            Ok(fd) => fd,
            Err(Errno::NOENT) => {
                // Detect old-format repositories that have objects/ but
                // no meta.json.  Use filter_errno so non-ENOENT errors
                // from statat are propagated.
                return Err(
                    match statat(repo_fd, "objects", AtFlags::empty()).filter_errno(Errno::NOENT) {
                        Ok(Some(_)) => RepositoryOpenError::OldFormatRepository,
                        Ok(None) => RepositoryOpenError::MetadataMissing,
                        Err(e) => e.into(),
                    },
                );
            }
            Err(e) => return Err(e.into()),
        };

        // Clone the fd: one for reading, one for the verity probe.
        let read_fd = meta_fd.try_clone()?;
        let meta: RepoMetadata =
            serde_json::from_reader(std::io::BufReader::new(File::from(read_fd)))
                .map_err(RepositoryOpenError::MetadataInvalid)?;

        // Probe verity on the original fd.
        let has_verity = measure_verity_opt::<ObjectID>(&meta_fd)
            .map_err(|e| std::io::Error::other(e.to_string()))?
            .is_some();

        Ok((meta, has_verity))
    }

    /// Open the default user-owned composefs repository.
    #[context("Opening user repository")]
    pub fn open_user() -> Result<Self> {
        Ok(Self::open_path(CWD, user_path()?)?)
    }

    /// Open the default system-global composefs repository.
    #[context("Opening system repository")]
    pub fn open_system() -> Result<Self> {
        Ok(Self::open_path(CWD, system_path())?)
    }

    fn ensure_dir(&self, dir: impl AsRef<Path>) -> ErrnoResult<()> {
        mkdirat(&self.repository, dir.as_ref(), 0o755.into()).or_else(|e| match e {
            Errno::EXIST => Ok(()),
            _ => Err(e),
        })
    }

    /// Asynchronously ensures an object exists in the repository.
    ///
    /// Same as `ensure_object` but runs the operation on a blocking thread pool
    /// to avoid blocking async tasks. Returns the fsverity digest of the object.
    ///
    /// For performance reasons, this function does *not* call fsync() or similar.  After you're
    /// done with everything, call `Repository::sync_async()`.
    #[context("Ensuring object asynchronously")]
    pub async fn ensure_object_async(self: &Arc<Self>, data: Vec<u8>) -> Result<ObjectID> {
        let writable = self.ensure_writable_token()?;
        let self_ = Arc::clone(self);
        tokio::task::spawn_blocking(move || self_.ensure_object_impl(&data, &writable)).await?
    }

    /// Import an object by streaming from a file descriptor into a
    /// tmpfile, without buffering the entire file in memory.
    ///
    /// In insecure mode the verity digest is computed while copying,
    /// avoiding a second read pass.
    #[context("Ensuring object from file descriptor")]
    pub(crate) fn ensure_object_from_fd(&self, source: OwnedFd, size: u64) -> Result<ObjectID> {
        let writable = self.ensure_writable_token()?;
        let tmpfile_fd = self.create_object_tmpfile_impl(&writable)?;

        if self.insecure {
            // Insecure mode: compute verity digest while copying, avoiding
            // a second read of the data in finalize_object_tmpfile_impl.
            let mut hasher = FsVerityHasher::<ObjectID>::new();
            let mut src = std::io::BufReader::with_capacity(IO_BUF_CAPACITY, File::from(source));
            let mut dst = File::from(tmpfile_fd.try_clone()?);

            loop {
                let buf = src.fill_buf()?;
                if buf.is_empty() {
                    break;
                }
                let chunk = &buf[..buf.len().min(FsVerityHasher::<ObjectID>::BLOCK_SIZE)];
                hasher.add_block(chunk);
                dst.write_all(chunk)?;
                let n = chunk.len();
                src.consume(n);
            }
            drop(dst);

            let id = hasher.digest();
            let ro_fd = reopen_tmpfile_ro(File::from(tmpfile_fd))
                .context("Re-opening tmpfile as read-only")?;
            let objects_dir = self.objects_dir().context("Getting objects directory")?;
            let (id, _method) = self.link_tmpfile_as_object(objects_dir, &ro_fd, &id, size)?;
            Ok(id)
        } else {
            // Secure mode: let std::io::copy use copy_file_range for
            // potential reflinks, then finalize_object_tmpfile_impl
            // enables kernel verity and measures the digest.
            let mut src = File::from(source);
            let mut dst = File::from(tmpfile_fd.try_clone()?);
            let copied = std::io::copy(&mut src, &mut dst)?;
            ensure!(copied == size, "Expected {size} bytes, got {copied}");
            drop(dst);

            let (id, _method) =
                self.finalize_object_tmpfile_impl(File::from(tmpfile_fd), size, &writable)?;
            Ok(id)
        }
    }

    /// Create an O_TMPFILE in the objects directory for streaming writes.
    ///
    /// Returns the file descriptor for writing. The caller should write data to this fd,
    /// then call [`finalize_object_tmpfile`](Self::finalize_object_tmpfile) to compute
    /// the verity digest, enable fs-verity, and link the file into the objects directory.
    #[context("Creating object tmpfile")]
    pub fn create_object_tmpfile(&self) -> Result<OwnedFd> {
        let writable = self.ensure_writable_token()?;
        self.create_object_tmpfile_impl(&writable)
    }

    #[context("Creating object tmpfile")]
    pub(crate) fn create_object_tmpfile_impl(&self, _writable: &WritableRepo) -> Result<OwnedFd> {
        let objects_dir = self
            .objects_dir()
            .context("Getting objects directory for tmpfile creation")?;
        let fd = openat(
            objects_dir,
            ".",
            OFlags::RDWR | OFlags::TMPFILE | OFlags::CLOEXEC,
            Mode::from_raw_mode(0o644),
        )
        .context("Opening temp file in objects directory")?;
        Ok(fd)
    }

    /// Ensure an object exists by reflinking or hardlinking from a source file.
    ///
    /// The fallback chain is: reflink -> hardlink -> copy.
    ///
    /// - **Reflink** (FICLONE): zero-copy clone on btrfs/XFS. Uses a tmpfile.
    /// - **Hardlink**: enables fs-verity on the source file in-place, then
    ///   hardlinks it directly into the objects directory. This avoids all data
    ///   copying on filesystems like ext4 that don't support reflinks.
    /// - **Copy**: regular data copy into a tmpfile as last resort.
    ///
    /// The `ctx` argument accumulates knowledge across calls in the same
    /// import operation.  After the first reflink attempt fails with
    /// `EOPNOTSUPP` / `EXDEV`, the context records this so that subsequent
    /// calls skip straight to the hardlink path.
    ///
    /// This is particularly useful for importing from containers-storage where
    /// we already have the file on disk and want to avoid copying data.
    pub fn ensure_object_from_file(
        &self,
        src: &std::fs::File,
        size: u64,
        ctx: &mut ImportContext,
    ) -> Result<(ObjectID, ObjectStoreMethod)> {
        self.ensure_object_from_file_inner(src, size, true, ctx)
    }

    /// Like [`ensure_object_from_file`](Self::ensure_object_from_file) but
    /// errors if neither reflink nor hardlink succeeds, instead of falling back
    /// to a regular copy.
    ///
    /// Intended for bootc's unified storage path where the composefs repo and
    /// containers-storage are always on the same filesystem, so zero-copy
    /// should always be possible.
    pub fn ensure_object_from_file_zerocopy(
        &self,
        src: &std::fs::File,
        size: u64,
        ctx: &mut ImportContext,
    ) -> Result<(ObjectID, ObjectStoreMethod)> {
        self.ensure_object_from_file_inner(src, size, false, ctx)
    }

    /// Inner implementation for [`ensure_object_from_file`](Self::ensure_object_from_file) and
    /// [`ensure_object_from_file_zerocopy`](Self::ensure_object_from_file_zerocopy).
    ///
    /// When `allow_copy` is false, the copy fallback returns an error instead.
    fn ensure_object_from_file_inner(
        &self,
        src: &std::fs::File,
        size: u64,
        allow_copy: bool,
        ctx: &mut ImportContext,
    ) -> Result<(ObjectID, ObjectStoreMethod)> {
        use rustix::fs::{fstat, ioctl_ficlone};

        let writable = self.ensure_writable_token()?;

        // Determine the source and destination device IDs so we can look up
        // whether this particular filesystem pair supports reflinks.
        let src_dev = fstat(src)?.st_dev;
        let dst_dev = fstat(self.objects_dir()?)?.st_dev;

        // Try reflink first, unless a previous call on this device pair
        // already discovered that FICLONE is unsupported.
        if !ctx.is_reflink_unsupported(src_dev, dst_dev) {
            let tmpfile_fd = self.create_object_tmpfile_impl(&writable)?;
            let tmpfile = File::from(tmpfile_fd);

            match ioctl_ficlone(&tmpfile, src) {
                Ok(()) => {
                    // Reflink succeeded — verify size matches
                    let stat = fstat(&tmpfile)?;
                    anyhow::ensure!(
                        stat.st_size as u64 == size,
                        "Reflink size mismatch: expected {}, got {}",
                        size,
                        stat.st_size
                    );

                    let (object_id, method) = self.finalize_object_tmpfile(tmpfile, size)?;
                    let method = match method {
                        ObjectStoreMethod::Copied => ObjectStoreMethod::Reflinked,
                        other => other,
                    };
                    return Ok((object_id, method));
                }
                Err(Errno::OPNOTSUPP | Errno::XDEV) => {
                    // Record for this device pair so subsequent calls skip.
                    ctx.mark_reflink_unsupported(src_dev, dst_dev);
                    drop(tmpfile);
                }
                Err(e) => {
                    return Err(e).context("Reflinking source file to objects directory")?;
                }
            }
        }

        // Try hardlink: enable verity on the source in-place, then link it
        // directly into objects/. This avoids all data copying.
        match self.try_hardlink_object(src, size) {
            Ok(result) => return Ok(result),
            Err(_) if allow_copy => {
                // Hardlink failed, fall through to copy.
                // Common causes: cross-mount (overlay bind mount), EPERM,
                // or verity enablement failure.
            }
            Err(e) => {
                return Err(e).context(
                    "reflink and hardlink both failed; copy fallback is disabled (zerocopy mode)",
                );
            }
        }

        // Final fallback: copy data into a new tmpfile.
        let tmpfile_fd = self.create_object_tmpfile_impl(&writable)?;
        let mut tmpfile = File::from(tmpfile_fd);
        {
            use std::io::{Seek, SeekFrom};
            let mut src_clone = src.try_clone()?;
            src_clone.seek(SeekFrom::Start(0))?;
            std::io::copy(&mut src_clone, &mut tmpfile)?;
        }

        let (object_id, method) = self.finalize_object_tmpfile(tmpfile, size)?;
        Ok((object_id, method))
    }

    /// Try to hardlink a source file directly into the objects directory.
    ///
    /// Enables fs-verity on the source file in-place, measures the digest to
    /// determine the object ID, then hardlinks the source into `objects/<hash>`.
    ///
    /// Returns an error if verity cannot be enabled, the digest cannot be
    /// measured, or the hardlink fails (e.g. cross-device).
    fn try_hardlink_object(
        &self,
        src: &std::fs::File,
        size: u64,
    ) -> Result<(ObjectID, ObjectStoreMethod)> {
        use crate::fsverity::enable_verity_with_retry;
        use rustix::thread::{CapabilitySet, capabilities};

        // AT_EMPTY_PATH linkat requires CAP_DAC_READ_SEARCH.  Check upfront
        // so callers get a clear error instead of a confusing ENOENT.
        let has_cap = capabilities(None)
            .map(|caps| caps.effective.contains(CapabilitySet::DAC_READ_SEARCH))
            .unwrap_or(false);
        if !has_cap {
            anyhow::bail!(
                "hardlinking objects requires CAP_DAC_READ_SEARCH \
                 (run as root or use the copy fallback)"
            );
        }

        let objects_dir = self.objects_dir()?;

        // Enable fs-verity on the source file in-place.
        // This is safe because the caller (bootc/containers-storage) owns the
        // source files and they are immutable image data.
        // AlreadyEnabled is fine — the file was already verity-protected.
        let verity_enabled = match enable_verity_with_retry::<ObjectID>(src) {
            Ok(()) => true,
            Err(EnableVerityError::AlreadyEnabled) => true,
            Err(EnableVerityError::FilesystemNotSupported) if self.insecure => false,
            Err(e) => {
                return Err(e).context("enabling verity on source file for hardlink")?;
            }
        };

        // Get the object ID from the verity digest (kernel-measured or userspace-computed)
        let id: ObjectID = if verity_enabled {
            measure_verity(src).context("measuring verity digest on source file")?
        } else {
            // Insecure mode on a filesystem without verity: compute digest in userspace
            let mut reader = std::io::BufReader::new(
                src.try_clone()
                    .context("cloning fd for digest computation")?,
            );
            Self::compute_verity_digest(&mut reader)
                .context("computing verity digest in insecure mode")?
        };

        // Check if object already exists (dedup)
        let path = id.to_object_pathname();
        match statat(objects_dir, &path, AtFlags::empty()) {
            Ok(stat) if stat.st_size as u64 == size => {
                return Ok((id, ObjectStoreMethod::AlreadyPresent));
            }
            _ => {}
        }

        // Ensure parent directory exists (e.g. objects/4e/)
        let parent_dir = id.to_object_dir();
        ensure_dir_at(objects_dir, &parent_dir, Mode::from_raw_mode(0o755))
            .context("creating object parent directory")?;

        // Hardlink the source file directly into objects/<hash>.
        // Use AT_EMPTY_PATH to link by fd, which avoids the kernel's
        // may_linkat() restriction that rejects AT_SYMLINK_FOLLOW on
        // /proc/self/fd/<N> magic symlinks for non-root mounts.
        // AT_EMPTY_PATH requires CAP_DAC_READ_SEARCH, which is available
        // when running as root (the expected case for containers-storage).
        match linkat(src, "", objects_dir, &path, AtFlags::EMPTY_PATH) {
            Ok(()) => Ok((id, ObjectStoreMethod::Hardlinked)),
            Err(Errno::EXIST) => Ok((id, ObjectStoreMethod::AlreadyPresent)),
            Err(e) => Err(e).context("hardlinking source file into objects directory")?,
        }
    }

    /// Finalize a tmpfile as an object.
    ///
    /// This method should be called from a blocking context (e.g., `spawn_blocking`)
    /// as it performs synchronous I/O operations.
    ///
    /// This method:
    /// 1. Re-opens the file as read-only
    /// 2. Enables fs-verity on the file (kernel computes digest)
    /// 3. Reads the digest from the kernel
    /// 4. Checks if object already exists (deduplication)
    /// 5. Links the file into the objects directory
    ///
    /// By letting the kernel compute the digest during verity enable, we avoid
    /// reading the file an extra time in userspace.
    #[context("Finalizing object tempfile")]
    pub fn finalize_object_tmpfile(
        &self,
        file: File,
        size: u64,
    ) -> Result<(ObjectID, ObjectStoreMethod)> {
        let writable = self.ensure_writable_token()?;
        self.finalize_object_tmpfile_impl(file, size, &writable)
    }

    #[context("Finalizing object tempfile")]
    pub(crate) fn finalize_object_tmpfile_impl(
        &self,
        file: File,
        size: u64,
        _writable: &WritableRepo,
    ) -> Result<(ObjectID, ObjectStoreMethod)> {
        let ro_fd =
            reopen_tmpfile_ro(file).context("Re-opening tmpfile as read-only for verity")?;

        // Get objects_dir early since we may need it for verity copy
        let objects_dir = self
            .objects_dir()
            .context("Getting objects directory for finalization")?;

        // Enable verity - the kernel reads the file and computes the digest.
        // Use enable_verity_maybe_copy to handle the case where forked processes
        // have inherited writable fds to this file.
        let (ro_fd, verity_enabled) =
            match enable_verity_maybe_copy::<ObjectID>(objects_dir, ro_fd.as_fd()) {
                Ok(None) => (ro_fd, true),
                Ok(Some(new_fd)) => (new_fd, true),
                Err(EnableVerityError::FilesystemNotSupported) if self.insecure => (ro_fd, false),
                Err(EnableVerityError::AlreadyEnabled) => (ro_fd, true),
                Err(other) => return Err(other).context("Enabling verity on tmpfile")?,
            };

        // Get the digest - either from kernel (fast) or compute in userspace (fallback)
        let id: ObjectID = if verity_enabled {
            measure_verity(&ro_fd).context("Measuring verity digest")?
        } else {
            // Insecure mode: compute digest in userspace from ro_fd
            let mut reader = std::io::BufReader::new(File::from(
                ro_fd
                    .try_clone()
                    .context("Cloning fd for digest computation")?,
            ));
            Self::compute_verity_digest(&mut reader)
                .context("Computing verity digest in insecure mode")?
        };

        self.link_tmpfile_as_object(objects_dir, &ro_fd, &id, size)
    }

    /// Link a read-only tmpfile into the objects directory with dedup check.
    ///
    /// If an object with the same digest and size already exists, the
    /// tmpfile is discarded and `AlreadyPresent` is returned.
    fn link_tmpfile_as_object(
        &self,
        objects_dir: &OwnedFd,
        ro_fd: &impl AsFd,
        id: &ObjectID,
        size: u64,
    ) -> Result<(ObjectID, ObjectStoreMethod)> {
        let path = id.to_object_pathname();

        match statat(objects_dir, &path, AtFlags::empty()) {
            Ok(stat) if stat.st_size as u64 == size => {
                return Ok((id.clone(), ObjectStoreMethod::AlreadyPresent));
            }
            _ => {}
        }

        let parent_dir = id.to_object_dir();
        ensure_dir_at(objects_dir, &parent_dir, Mode::from_raw_mode(0o755))
            .context("creating object parent directory")?;

        match linkat(
            CWD,
            proc_self_fd(ro_fd),
            objects_dir,
            &path,
            AtFlags::SYMLINK_FOLLOW,
        ) {
            Ok(()) => Ok((id.clone(), ObjectStoreMethod::Copied)),
            Err(Errno::EXIST) => Ok((id.clone(), ObjectStoreMethod::AlreadyPresent)),
            Err(e) => Err(e).context("Linking tmpfile into objects directory")?,
        }
    }

    /// Compute fs-verity digest in userspace by reading from a buffered source.
    /// Used as fallback when kernel verity is not available (insecure mode).
    #[context("Computing verity digest in userspace")]
    fn compute_verity_digest(reader: &mut impl std::io::BufRead) -> Result<ObjectID> {
        let mut hasher = FsVerityHasher::<ObjectID>::new();

        loop {
            let buf = reader
                .fill_buf()
                .context("Reading buffer for verity computation")?;
            if buf.is_empty() {
                break;
            }
            // add_block expects at most one block at a time
            let chunk_size = buf.len().min(FsVerityHasher::<ObjectID>::BLOCK_SIZE);
            hasher.add_block(&buf[..chunk_size]);
            reader.consume(chunk_size);
        }

        Ok(hasher.digest())
    }

    /// Store an object with a pre-computed fs-verity ID.
    ///
    /// This is an internal helper that stores data assuming the caller has already
    /// computed the correct fs-verity digest. The digest is verified after storage.
    #[context("Storing object with ID {id:?}")]
    fn store_object_with_id(
        &self,
        data: &[u8],
        id: &ObjectID,
        _writable: &WritableRepo,
    ) -> Result<()> {
        let dirfd = self
            .objects_dir()
            .context("Getting objects directory for storage")?;
        let path = id.to_object_pathname();

        // the usual case is that the file will already exist
        match openat(
            dirfd,
            &path,
            OFlags::RDONLY | OFlags::CLOEXEC,
            Mode::empty(),
        ) {
            Ok(fd) => {
                // measure the existing file to ensure that it's correct
                // TODO: try to replace file if it's broken?
                match ensure_verity_equal(&fd, id) {
                    Ok(()) => {}
                    Err(CompareVerityError::Measure(MeasureVerityError::VerityMissing))
                        if self.insecure =>
                    {
                        match enable_verity_maybe_copy::<ObjectID>(dirfd, fd.as_fd()) {
                            Ok(Some(fd)) => ensure_verity_equal(&fd, id)
                                .context("Verifying verity after enabling (copied)")?,
                            Ok(None) => ensure_verity_equal(&fd, id)
                                .context("Verifying verity after enabling (original)")?,
                            Err(other) => {
                                Err(other).context("Enabling verity on existing object")?
                            }
                        }
                    }
                    Err(CompareVerityError::Measure(
                        MeasureVerityError::FilesystemNotSupported,
                    )) if self.insecure => {}
                    Err(other) => Err(other).context("Verifying existing object integrity")?,
                }
                return Ok(());
            }
            Err(Errno::NOENT) => {
                // in this case we'll create the file
            }
            Err(other) => {
                return Err(other).context("Checking for existing object in repository")?;
            }
        }

        let fd = ensure_dir_and_openat(dirfd, &id.to_object_dir(), OFlags::RDWR | OFlags::TMPFILE)
            .with_context(|| "Creating tempfile in object subdirectory")?;
        let mut file = File::from(fd);
        file.write_all(data).context("Writing data to tmpfile")?;
        // NB: We should do fdatasync() or fsync() here, but doing this for each file forces the
        // creation of a massive number of journal commits and is a performance disaster.  We need
        // to coordinate this at a higher level.  See .write_stream().
        let ro_fd = reopen_tmpfile_ro(file).context("Re-opening file as read-only for verity")?;

        let ro_fd = match enable_verity_maybe_copy::<ObjectID>(dirfd, ro_fd.as_fd()) {
            Ok(maybe_fd) => {
                let ro_fd = maybe_fd.unwrap_or(ro_fd);
                match ensure_verity_equal(&ro_fd, id) {
                    Ok(()) => ro_fd,
                    Err(CompareVerityError::Measure(
                        MeasureVerityError::VerityMissing
                        | MeasureVerityError::FilesystemNotSupported,
                    )) if self.insecure => ro_fd,
                    Err(other) => Err(other).context("Double-checking verity digest")?,
                }
            }
            Err(EnableVerityError::FilesystemNotSupported) if self.insecure => ro_fd,
            Err(other) => Err(other).context("Enabling verity digest")?,
        };

        match linkat(
            CWD,
            proc_self_fd(&ro_fd),
            dirfd,
            path,
            AtFlags::SYMLINK_FOLLOW,
        ) {
            Ok(()) => {}
            Err(Errno::EXIST) => {
                // TODO: strictly, we should measure the newly-appeared file
            }
            Err(other) => {
                return Err(other).context("Linking created object file");
            }
        }

        Ok(())
    }

    /// Given a blob of data, store it in the repository.
    ///
    /// For performance reasons, this function does *not* call fsync() or similar.  After you're
    /// done with everything, call `Repository::sync()`.
    #[context("Ensuring object exists in repository")]
    pub fn ensure_object(&self, data: &[u8]) -> Result<ObjectID> {
        let writable = self.ensure_writable_token()?;
        self.ensure_object_impl(data, &writable)
    }

    /// Like [`ensure_object`] but requires a [`WritableRepo`] token
    /// instead of performing the check itself.
    ///
    /// This exists so that [`SplitStreamWriter`] (which carries a token)
    /// can store objects without redundant `faccessat` calls.
    #[context("Ensuring object exists in repository")]
    pub(crate) fn ensure_object_impl(
        &self,
        data: &[u8],
        writable: &WritableRepo,
    ) -> Result<ObjectID> {
        let id: ObjectID = compute_verity(data);
        self.store_object_with_id(data, &id, writable)?;
        Ok(id)
    }

    #[context("Opening file '{filename}' with verity verification")]
    fn open_with_verity(&self, filename: &str, expected_verity: &ObjectID) -> Result<OwnedFd> {
        let fd = self
            .openat(filename, OFlags::RDONLY)
            .with_context(|| format!("Opening file '{filename}' in repository"))?;
        match ensure_verity_equal(&fd, expected_verity) {
            Ok(()) => {}
            Err(CompareVerityError::Measure(
                MeasureVerityError::VerityMissing | MeasureVerityError::FilesystemNotSupported,
            )) if self.insecure => {}
            Err(other) => Err(other).context("Verifying file verity digest")?,
        }
        Ok(fd)
    }

    /// Returns whether the repository is in insecure mode.
    ///
    /// This is auto-detected from whether `meta.json` has fs-verity
    /// enabled, but can be overridden with [`set_insecure`].
    pub fn is_insecure(&self) -> bool {
        self.insecure
    }

    /// Mark this repository as insecure, disabling verification of
    /// fs-verity digests.  This allows operation on filesystems
    /// without verity support.
    pub fn set_insecure(&mut self) -> &mut Self {
        self.insecure = true;
        self
    }

    /// Require that this repository has fs-verity enabled.
    ///
    /// Returns an error if the repository was not initialized with
    /// verity on `meta.json`, since there is no mechanism to
    /// retroactively enable verity on existing objects.
    pub fn require_verity(&self) -> Result<()> {
        if self.insecure {
            bail!(
                "repository was not initialized with fs-verity \
                 (hint: re-create with `cfsctl init` on a \
                 verity-capable filesystem)"
            );
        }
        Ok(())
    }

    /// Fast pre-flight check that the repository is writable.
    ///
    /// Uses `faccessat(W_OK)` to catch read-only mounts and permission
    /// issues before starting expensive network or I/O work.  Callers
    /// that want to fail early (e.g. before downloading an image) should
    /// call this; individual write methods already check internally.
    pub fn ensure_writable(&self) -> Result<()> {
        self.ensure_writable_token()?;
        Ok(())
    }

    /// Like [`ensure_writable`] but returns a proof token for internal use.
    pub(crate) fn ensure_writable_token(&self) -> Result<WritableRepo> {
        // fstatvfs catches read-only mounts (ST_RDONLY).  faccessat(W_OK)
        // alone is insufficient because it only checks DAC permission bits
        // and root bypasses those, so a root process on a read-only
        // bind-mounted repo would pass the faccessat check.  Conversely,
        // fstatvfs alone misses writable filesystems where the caller lacks
        // write permission (e.g. a repo owned by another user), so we follow
        // up with faccessat to catch that case.
        let st = fstatvfs(&self.repository).context("Repository is not writable")?;
        if st.f_flag.contains(StatVfsMountFlags::RDONLY) {
            anyhow::bail!("Repository is not writable: read-only file system");
        }
        accessat(&self.repository, ".", Access::WRITE_OK, AtFlags::empty())
            .context("Repository is not writable")?;
        Ok(WritableRepo)
    }

    /// Creates a SplitStreamWriter for writing a split stream.
    /// You should write the data to the returned object and then pass it to .store_stream() to
    /// store the result.
    ///
    /// The writable check is performed here so that callers cannot obtain
    /// a writer without first verifying the repository is writable.
    /// The [`WritableRepo`] token is carried by the writer so that
    /// subsequent object writes skip redundant checks.
    pub fn create_stream(
        self: &Arc<Self>,
        content_type: u64,
    ) -> Result<SplitStreamWriter<ObjectID>> {
        let writable = self.ensure_writable_token()?;
        Ok(SplitStreamWriter::new(self, content_type, writable))
    }

    fn format_object_path(id: &ObjectID) -> String {
        format!("objects/{}", id.to_object_pathname())
    }

    fn format_stream_path(content_identifier: &str) -> String {
        format!("streams/{content_identifier}")
    }

    /// Check if the provided splitstream is present in the repository;
    /// if so, return its fsverity digest.
    #[context("Checking if stream '{content_identifier}' exists")]
    pub fn has_stream(&self, content_identifier: &str) -> Result<Option<ObjectID>> {
        let stream_path = Self::format_stream_path(content_identifier);

        match readlinkat(&self.repository, &stream_path, []) {
            Ok(target) => {
                let bytes = target.as_bytes();
                ensure!(
                    bytes.starts_with(b"../"),
                    "stream symlink has incorrect prefix"
                );
                Ok(Some(
                    ObjectID::from_object_pathname(bytes)
                        .context("Parsing object ID from stream symlink target")?,
                ))
            }
            Err(Errno::NOENT) => Ok(None),
            Err(err) => Err(err).context("Reading stream symlink")?,
        }
    }

    /// Write the given splitstream to the repository with the provided content identifier and
    /// optional reference name.
    ///
    /// This call contains an internal barrier that guarantees that, in event of a crash, either:
    ///  - the named stream (by `content_identifier`) will not be available; or
    ///  - the stream and all of its linked data will be available
    ///
    /// In other words: it will not be possible to boot a system which contained a stream named
    /// `content_identifier` but is missing linked streams or objects from that stream.
    #[context("Writing stream '{content_identifier}' to repository")]
    pub fn write_stream(
        &self,
        writer: SplitStreamWriter<ObjectID>,
        content_identifier: &str,
        reference: Option<&str>,
    ) -> Result<ObjectID> {
        let writable = *writer.writable();
        let object_id = writer.done().context("Finalizing split stream writer")?;

        // Right now we have:
        //   - all of the linked external objects and streams; and
        //   - the binary data of this splitstream itself
        //
        // in the filesystem but but not yet guaranteed to be synced to disk.  This is OK because
        // nobody knows that the binary data of the splitstream is a splitstream yet: it could just
        // as well be a random data file contained in an OS image or something.
        //
        // We need to make sure that all of that makes it to the disk before the splitstream is
        // visible as a splitstream.
        self.sync()?;

        let stream_path = Self::format_stream_path(content_identifier);
        let object_path = Self::format_object_path(&object_id);
        self.symlink_impl(&stream_path, &object_path, &writable)?;

        if let Some(name) = reference {
            let reference_path = format!("streams/refs/{name}");
            self.symlink_impl(&reference_path, &stream_path, &writable)?;
        }

        Ok(object_id)
    }

    /// Register an already-stored object as a named stream.
    ///
    /// This is useful when using `SplitStreamBuilder` which stores the splitstream
    /// directly via `finish()`. After calling `finish()`, call this method to
    /// sync all data to disk and create the stream symlink.
    ///
    /// This method ensures atomicity: the stream symlink is only created after
    /// all objects have been synced to disk.
    #[context("Registering stream '{content_identifier}' with object ID {object_id:?}")]
    pub async fn register_stream(
        self: &Arc<Self>,
        object_id: &ObjectID,
        content_identifier: &str,
        reference: Option<&str>,
    ) -> Result<()> {
        let writable = self.ensure_writable_token()?;
        self.sync_async().await?;

        let stream_path = Self::format_stream_path(content_identifier);
        let object_path = Self::format_object_path(object_id);
        self.symlink_impl(&stream_path, &object_path, &writable)?;

        if let Some(name) = reference {
            let reference_path = format!("streams/refs/{name}");
            self.symlink_impl(&reference_path, &stream_path, &writable)?;
        }

        Ok(())
    }

    /// Async version of `write_stream` for use with parallel object storage.
    ///
    /// This method awaits any pending parallel object storage tasks before
    /// finalizing the stream. Use this when you've called `write_external_parallel()`
    /// on the writer.
    #[context("Writing stream '{content_identifier}' to repository (async)")]
    pub async fn write_stream_async(
        self: &Arc<Self>,
        writer: SplitStreamWriter<ObjectID>,
        content_identifier: &str,
        reference: Option<&str>,
    ) -> Result<ObjectID> {
        let writable = *writer.writable();
        let object_id = writer
            .done_async()
            .await
            .context("Finalizing split stream writer (async)")?;

        self.sync_async().await?;

        let stream_path = Self::format_stream_path(content_identifier);
        let object_path = Self::format_object_path(&object_id);
        self.symlink_impl(&stream_path, &object_path, &writable)?;

        if let Some(name) = reference {
            let reference_path = format!("streams/refs/{name}");
            self.symlink_impl(&reference_path, &stream_path, &writable)?;
        }

        Ok(object_id)
    }

    /// Check if a splitstream with a given name exists in the "refs" in the repository.
    #[context("Checking if named stream '{name}' exists")]
    pub fn has_named_stream(&self, name: &str) -> Result<bool> {
        let stream_path = format!("streams/refs/{name}");

        Ok(statat(&self.repository, &stream_path, AtFlags::empty())
            .filter_errno(Errno::NOENT)
            .with_context(|| format!("Looking for stream '{name}' in repository"))?
            .map(|s| FileType::from_raw_mode(s.st_mode).is_symlink())
            .unwrap_or(false))
    }

    /// Assign a named reference to a stream, making it a GC root.
    ///
    /// Creates a symlink at `streams/refs/{name}` pointing to the stream identified
    /// by `content_identifier`. The stream must already exist in the repository.
    ///
    /// Named references serve two purposes:
    /// 1. They provide human-readable names for streams
    /// 2. They act as GC roots - streams reachable from refs are not garbage collected
    ///
    /// The `name` can include path separators to organize refs hierarchically
    /// (e.g., `myapp/layer1`), and intermediate directories are created automatically.
    #[context("Naming stream '{content_identifier}' as '{name}'")]
    pub fn name_stream(&self, content_identifier: &str, name: &str) -> Result<()> {
        let writable = self.ensure_writable_token()?;
        let stream_path = Self::format_stream_path(content_identifier);
        let reference_path = format!("streams/refs/{name}");
        self.symlink_impl(&reference_path, &stream_path, &writable)?;
        Ok(())
    }

    /// Ensures that the stream with a given content identifier digest exists in the repository.
    ///
    /// This tries to find the stream by the content identifier.  If the stream is already in the
    /// repository, the object ID (fs-verity digest) is read from the symlink.  If the stream is
    /// not already in the repository, a `SplitStreamWriter` is created and passed to `callback`.
    /// On return, the object ID of the stream will be calculated and it will be written to disk
    /// (if it wasn't already created by someone else in the meantime).
    ///
    /// In both cases, if `reference` is provided, it is used to provide a fixed name for the
    /// object.  Any object that doesn't have a fixed reference to it is subject to garbage
    /// collection.  It is an error if this reference already exists.
    ///
    /// On success, the object ID of the new object is returned.  It is expected that this object
    /// ID will be used when referring to the stream from other linked streams.
    #[context("Ensuring stream '{content_identifier}' exists")]
    pub fn ensure_stream<T: Default>(
        self: &Arc<Self>,
        content_identifier: &str,
        content_type: u64,
        callback: impl FnOnce(&mut SplitStreamWriter<ObjectID>) -> Result<T>,
        reference: Option<&str>,
    ) -> Result<(ObjectID, T)> {
        let writable = self.ensure_writable_token()?;
        let stream_path = Self::format_stream_path(content_identifier);

        let (object_id, extra) = match self.has_stream(content_identifier)? {
            Some(id) => (id, T::default()),
            None => {
                let mut writer = self.create_stream(content_type)?;
                let extra = callback(&mut writer).context("Writing stream content via callback")?;
                let id = self.write_stream(writer, content_identifier, reference)?;
                (id, extra)
            }
        };

        if let Some(name) = reference {
            let reference_path = format!("streams/refs/{name}");
            self.symlink_impl(&reference_path, &stream_path, &writable)?;
        }

        Ok((object_id, extra))
    }

    /// Open a splitstream with the given name.
    #[context("Opening stream '{content_identifier}'")]
    pub fn open_stream(
        &self,
        content_identifier: &str,
        verity: Option<&ObjectID>,
        expected_content_type: Option<u64>,
    ) -> Result<SplitStreamReader<ObjectID>> {
        let file = File::from(if let Some(verity_hash) = verity {
            self.open_object(verity_hash)
                .with_context(|| format!("Opening object '{verity_hash:?}'"))?
        } else {
            let filename = Self::format_stream_path(content_identifier);
            self.openat(&filename, OFlags::RDONLY)
                .with_context(|| format!("Opening ref '{filename}'"))?
        });

        SplitStreamReader::new(file, expected_content_type)
    }

    /// Given an object identifier (a digest), return a read-only file descriptor
    /// for its contents. The fsverity digest is verified (if the repository is not in `insecure` mode).
    #[context("Opening object {id:?}")]
    pub fn open_object(&self, id: &ObjectID) -> Result<OwnedFd> {
        self.open_with_verity(&Self::format_object_path(id), id)
    }

    /// Read the contents of an object into a Vec
    #[context("Reading object {id:?} into memory")]
    pub fn read_object(&self, id: &ObjectID) -> Result<Vec<u8>> {
        let mut data = vec![];
        File::from(self.open_object(id)?)
            .read_to_end(&mut data)
            .context("Reading object data")?;
        Ok(data)
    }

    /// Merges a splitstream into a single continuous stream.
    ///
    /// Opens the named splitstream, resolves all object references, and writes
    /// the complete merged content to the provided writer. Optionally verifies
    /// the splitstream's fsverity digest matches the expected value.
    #[context("Merging splitstream '{content_identifier}'")]
    pub fn merge_splitstream(
        &self,
        content_identifier: &str,
        verity: Option<&ObjectID>,
        expected_content_type: Option<u64>,
        output: &mut impl Write,
    ) -> Result<()> {
        let mut split_stream =
            self.open_stream(content_identifier, verity, expected_content_type)?;
        split_stream.cat(self, output)
    }

    /// Write `data into the repository as an image with the given `name`.
    ///
    /// The fsverity digest is returned.
    ///
    /// # Integrity
    ///
    /// This function is not safe for untrusted users.
    #[context("Writing image to repository")]
    pub fn write_image(&self, name: Option<&str>, data: &[u8]) -> Result<ObjectID> {
        let writable = self.ensure_writable_token()?;
        let object_id = self.ensure_object_impl(data, &writable)?;

        let object_path = Self::format_object_path(&object_id);
        let image_path = format!("images/{}", object_id.to_hex());

        self.symlink_impl(&image_path, &object_path, &writable)?;

        if let Some(reference) = name {
            let ref_path = format!("images/refs/{reference}");
            self.symlink_impl(&ref_path, &image_path, &writable)?;
        }

        Ok(object_id)
    }

    /// Import the data from the provided read into the repository as an image.
    ///
    /// The fsverity digest is returned.
    ///
    /// # Integrity
    ///
    /// This function is not safe for untrusted users.
    #[context("Importing image '{name}' from reader")]
    pub fn import_image<R: Read>(&self, name: &str, image: &mut R) -> Result<ObjectID> {
        let mut data = vec![];
        image
            .read_to_end(&mut data)
            .context("Reading image data from input")?;
        self.write_image(Some(name), &data)
    }

    /// Returns the fd of the image and whether or not verity should be
    /// enabled when mounting it.
    #[context("Opening image '{name}'")]
    pub fn open_image(&self, name: &str) -> Result<(OwnedFd, bool)> {
        let image = self
            .openat(&format!("images/{name}"), OFlags::RDONLY)
            .with_context(|| format!("Opening ref 'images/{name}'"))?;

        if name.contains("/") {
            return Ok((image, true));
        }

        // A name with no slashes in it is taken to be a sha256 fs-verity digest
        match measure_verity::<ObjectID>(&image) {
            Ok(found)
                if found
                    == FsVerityHashValue::from_hex(name)
                        .context("Parsing expected verity hash from image name")? =>
            {
                Ok((image, true))
            }
            Ok(_) => bail!("fs-verity content mismatch"),
            Err(MeasureVerityError::VerityMissing | MeasureVerityError::FilesystemNotSupported)
                if self.insecure =>
            {
                Ok((image, false))
            }
            Err(other) => Err(other).context("Measuring image verity digest")?,
        }
    }

    /// Create a detached mount of an image. This file descriptor can then
    /// be attached via e.g. `move_mount`.
    #[context("Mounting image '{name}'")]
    pub fn mount(&self, name: &str) -> Result<OwnedFd> {
        let (image, enable_verity) = self.open_image(name)?;

        composefs_fsmount(
            image,
            name,
            self.objects_dir()
                .context("Getting objects directory for mount")?,
            enable_verity,
        )
        .context("Creating filesystem mount")
    }

    /// Mount the image with the provided digest at the target path.
    #[context("Mounting image '{name}' at path")]
    pub fn mount_at(&self, name: &str, mountpoint: impl AsRef<Path>) -> Result<()> {
        mount_at(
            self.mount(name)?,
            CWD,
            &canonicalize(mountpoint).context("Canonicalizing mountpoint path")?,
        )
        .context("Attaching mount at target path")
    }

    /// Creates a relative symlink within the repository.
    ///
    /// Computes the correct relative path from the symlink location to the target,
    /// creating any necessary intermediate directories. Atomically replaces any
    /// existing symlink at the specified name.
    pub fn symlink(
        &self,
        name: impl AsRef<Path> + std::fmt::Debug,
        target: impl AsRef<Path> + std::fmt::Debug,
    ) -> anyhow::Result<()> {
        let writable = self.ensure_writable_token()?;
        self.symlink_impl(name, target, &writable)
    }

    #[context("Creating symlink from {name:?} to {target:?}")]
    pub(crate) fn symlink_impl(
        &self,
        name: impl AsRef<Path> + std::fmt::Debug,
        target: impl AsRef<Path> + std::fmt::Debug,
        _writable: &WritableRepo,
    ) -> anyhow::Result<()> {
        let name = name.as_ref();

        let mut symlink_components = name.parent().unwrap().components().peekable();
        let mut target_components = target.as_ref().components().peekable();

        let mut symlink_ancestor = PathBuf::new();

        // remove common leading components
        while symlink_components.peek() == target_components.peek() {
            symlink_ancestor.push(symlink_components.next().unwrap());
            target_components.next().unwrap();
        }

        let mut relative = PathBuf::new();
        // prepend a "../" for each ancestor of the symlink
        // and create those ancestors as we do so
        for symlink_component in symlink_components {
            symlink_ancestor.push(symlink_component);
            self.ensure_dir(&symlink_ancestor)?;
            relative.push("..");
        }

        // now build the relative path from the remaining components of the target
        for target_component in target_components {
            relative.push(target_component);
        }

        // Atomically replace existing symlink
        Ok(replace_symlinkat(&relative, &self.repository, name)?)
    }

    #[context("Reading symlink hash value from {name:?}")]
    fn read_symlink_hashvalue(dirfd: &OwnedFd, name: &CStr) -> Result<ObjectID> {
        let link_content = readlinkat(dirfd, name, []).context("Reading symlink target")?;
        ObjectID::from_object_pathname(link_content.to_bytes())
            .context("Parsing object ID from symlink target")
    }

    #[context("Walking symlink directory")]
    fn walk_symlinkdir(fd: OwnedFd, entry_digests: &mut HashSet<OsString>) -> Result<()> {
        for item in Dir::read_from(&fd).context("Reading directory entries")? {
            let entry = item.context("Reading directory entry")?;
            // NB: the underlying filesystem must support returning filetype via direntry
            // that's a reasonable assumption, since it must also support fsverity...
            match entry.file_type() {
                FileType::Directory => {
                    let filename = entry.file_name();
                    if filename != c"." && filename != c".." {
                        let dirfd = openat(
                            &fd,
                            filename,
                            OFlags::RDONLY | OFlags::CLOEXEC,
                            Mode::empty(),
                        )
                        .context("Opening subdirectory for walking")?;
                        Self::walk_symlinkdir(dirfd, entry_digests)?;
                    }
                }
                FileType::Symlink => {
                    let link_content = readlinkat(&fd, entry.file_name(), [])
                        .context("Reading symlink content")?;
                    let linked_path = Path::new(OsStr::from_bytes(link_content.as_bytes()));
                    if let Some(entry_name) = linked_path.file_name() {
                        entry_digests.insert(entry_name.to_os_string());
                    } else {
                        // Does not have a proper file base name (i.e. "..")
                        // TODO: this case needs to be checked in fsck implementation
                        continue;
                    }
                }
                _ => {
                    bail!("Unexpected file type encountered");
                }
            }
        }

        Ok(())
    }

    /// Open the provided path in the repository.
    fn openat(&self, name: &str, flags: OFlags) -> ErrnoResult<OwnedFd> {
        // Unconditionally add CLOEXEC as we always want it.
        openat(
            &self.repository,
            name,
            flags | OFlags::CLOEXEC,
            Mode::empty(),
        )
    }

    // For a GC category (images / streams), return underlying entry digests and
    // object IDs for each entry
    // Under RefsOnly mode, only entries explicitly referenced in `<category>/refs`
    // directory structure would be walked and returned
    // Under AllEntries mode, all entires will be returned
    // Note that this function assumes all`*/refs/` links link to 1st level entries
    // and all 1st level entries link to object store
    // TODO: fsck the above noted assumption
    #[context("Walking GC category '{category}'")]
    fn gc_category(
        &self,
        category: &str,
        mode: GCCategoryWalkMode,
    ) -> Result<Vec<(ObjectID, String)>> {
        let Some(category_fd) = self
            .openat(category, OFlags::RDONLY | OFlags::DIRECTORY)
            .filter_errno(Errno::NOENT)
            .context(format!("Opening {category} dir in repository"))?
        else {
            return Ok(Vec::new());
        };

        let mut entry_digests = HashSet::new();
        match mode {
            GCCategoryWalkMode::RefsOnly => {
                if let Some(refs) = openat(
                    &category_fd,
                    "refs",
                    OFlags::RDONLY | OFlags::DIRECTORY | OFlags::CLOEXEC,
                    Mode::empty(),
                )
                .filter_errno(Errno::NOENT)
                .context(format!("Opening {category}/refs dir in repository"))?
                {
                    Self::walk_symlinkdir(refs, &mut entry_digests)
                        .context("Walking refs symlink directory")?;
                }
            }
            GCCategoryWalkMode::AllEntries => {
                // All first-level link entries should be directly object references
                for item in Dir::read_from(&category_fd).context("Reading category directory")? {
                    let entry = item.context("Reading category directory entry")?;
                    let filename = entry.file_name();
                    if filename != c"refs" && filename != c"." && filename != c".." {
                        if entry.file_type() != FileType::Symlink {
                            bail!("category directory contains non-symlink");
                        }
                        entry_digests.insert(OsString::from(&OsStr::from_bytes(
                            entry.file_name().to_bytes(),
                        )));
                    }
                }
            }
        }

        let objects = entry_digests
            .into_iter()
            .map(|entry_fn| {
                Ok((
                    Self::read_symlink_hashvalue(
                        &category_fd,
                        CString::new(entry_fn.as_bytes())
                            .context("Creating CString from filename")?
                            .as_c_str(),
                    )
                    .context("Reading symlink hash value")?,
                    entry_fn
                        .to_str()
                        .context("str conversion fails")?
                        .to_owned(),
                ))
            })
            .collect::<Result<_>>()?;

        Ok(objects)
    }

    // Remove all broken links from a directory, may operate recursively
    /// Remove broken symlinks from a directory.
    /// If `dry_run` is true, counts but does not remove. Returns the count.
    #[context("Cleaning up broken links")]
    fn cleanup_broken_links(fd: &OwnedFd, recursive: bool, dry_run: bool) -> Result<u64> {
        let mut count = 0;
        for item in Dir::read_from(fd).context("Reading directory for broken links cleanup")? {
            let entry = item.context("Reading directory entry for broken links cleanup")?;
            match entry.file_type() {
                FileType::Directory => {
                    if !recursive {
                        continue;
                    }
                    let filename = entry.file_name();
                    if filename != c"." && filename != c".." {
                        let dirfd = openat(
                            fd,
                            filename,
                            OFlags::RDONLY | OFlags::CLOEXEC,
                            Mode::empty(),
                        )
                        .context("Opening subdirectory for recursive broken link cleanup")?;
                        count += Self::cleanup_broken_links(&dirfd, recursive, dry_run)
                            .context("Cleaning up broken links in subdirectory")?;
                    }
                }

                FileType::Symlink => {
                    let filename = entry.file_name();
                    let result = statat(fd, filename, AtFlags::empty())
                        .filter_errno(Errno::NOENT)
                        .context("Testing for broken links")?;
                    if result.is_none() {
                        count += 1;
                        if !dry_run {
                            unlinkat(fd, filename, AtFlags::empty())
                                .context("Unlinking broken symlink")?;
                        }
                    }
                }

                _ => {
                    bail!("Unexpected file type encountered");
                }
            }
        }
        Ok(count)
    }

    /// Clean up broken links in a gc category. Returns count of links removed.
    #[context("Cleaning up broken links in {category} category")]
    fn cleanup_gc_category(&self, category: &'static str, dry_run: bool) -> Result<u64> {
        let Some(category_fd) = self
            .openat(category, OFlags::RDONLY | OFlags::DIRECTORY)
            .filter_errno(Errno::NOENT)
            .context(format!("Opening {category} dir in repository"))?
        else {
            return Ok(0);
        };
        // Always cleanup first-level first, then the refs
        let mut count = Self::cleanup_broken_links(&category_fd, false, dry_run)
            .with_context(|| format!("Cleaning up broken links in {category}/"))?;
        let ref_fd = openat(
            &category_fd,
            "refs",
            OFlags::RDONLY | OFlags::DIRECTORY | OFlags::CLOEXEC,
            Mode::empty(),
        )
        .filter_errno(Errno::NOENT)
        .context(format!("Opening {category}/refs to clean up broken links"))?;
        if let Some(ref dirfd) = ref_fd {
            count += Self::cleanup_broken_links(dirfd, true, dry_run).with_context(|| {
                format!("Cleaning up broken links recursively in {category}/refs")
            })?;
        }
        Ok(count)
    }

    // Traverse split streams to resolve all linked objects
    #[context("Walking streams starting from '{stream_name}'")]
    fn walk_streams(
        &self,
        stream_name_map: &HashMap<ObjectID, String>,
        stream_name: &str,
        walked_streams: &mut HashSet<String>,
        objects: &mut HashSet<ObjectID>,
    ) -> Result<()> {
        if walked_streams.contains(stream_name) {
            return Ok(());
        }
        walked_streams.insert(stream_name.to_owned());

        let mut split_stream = self
            .open_stream(stream_name, None, None)
            .context("Opening stream for walking")?;
        // Plain object references, add to live objects set
        split_stream
            .get_object_refs(|id| {
                trace!("   with {id:?}");
                objects.insert(id.clone());
            })
            .context("Getting object references from stream")?;
        // Collect all stream names from named references table to be walked next
        let streams_to_walk: Vec<_> = split_stream.iter_named_refs().collect();
        // Note that stream name from the named references table is not stream name in repository
        // In practice repository name is often table name prefixed with stream types (e.g. oci-config-<table name>)
        // Here we always match objectID to be absolutely sure
        for (stream_name_in_table, stream_object_id) in streams_to_walk {
            trace!(
                "   named reference stream {stream_name_in_table} lives, with {stream_object_id:?}"
            );
            objects.insert(stream_object_id.clone());
            if let Some(stream_name_in_repo) = stream_name_map.get(stream_object_id) {
                self.walk_streams(
                    stream_name_map,
                    stream_name_in_repo,
                    walked_streams,
                    objects,
                )
                .context("Walking referenced stream")?;
            } else {
                // stream is in table but not in repo, the repo is potentially broken, issue a warning
                trace!(
                    "broken repo: named reference stream {stream_name_in_table} not found as stream in repo"
                );
            }
        }
        Ok(())
    }

    /// Given an image, return the set of all objects referenced by it.
    #[context("Collecting objects for image '{name}'")]
    pub fn objects_for_image(&self, name: &str) -> Result<HashSet<ObjectID>> {
        let (image, _) = self.open_image(name)?;
        let mut data = vec![];
        std::fs::File::from(image)
            .read_to_end(&mut data)
            .context("Reading image data")?;
        crate::erofs::reader::collect_objects(&data)
            .context("Collecting objects from erofs image data")
    }

    /// Makes sure all content is written to the repository.
    ///
    /// This is currently just syncfs() on the repository's root directory because we don't have
    /// any better options at present.  This blocks until the data is written out.
    #[context("Syncing repository to disk")]
    pub fn sync(&self) -> Result<()> {
        syncfs(&self.repository).context("Syncing filesystem")?;
        Ok(())
    }

    /// Makes sure all content is written to the repository.
    ///
    /// This is currently just syncfs() on the repository's root directory because we don't have
    /// any better options at present.  This won't return until the data is written out.
    #[context("Syncing repository to disk (async)")]
    pub async fn sync_async(self: &Arc<Self>) -> Result<()> {
        let self_ = Arc::clone(self);
        tokio::task::spawn_blocking(move || self_.sync())
            .await
            .context("Spawning blocking sync task")?
    }

    /// Perform garbage collection, removing unreferenced objects.
    ///
    /// Objects reachable from `images/refs/` or `streams/refs/` are preserved,
    /// plus any `additional_roots` (looked up in both images and streams).
    /// Returns statistics about what was removed.
    ///
    /// # Locking
    ///
    /// An exclusive lock is held for the duration of this operation.
    #[context("Running garbage collection")]
    pub fn gc(&self, additional_roots: &[&str]) -> Result<GcResult> {
        self.ensure_writable_token()?;
        flock(&self.repository, FlockOperation::LockExclusive)
            .context("Acquiring exclusive lock for GC")?;
        self.gc_impl(additional_roots, false)
    }

    /// Preview what garbage collection would remove, without deleting.
    ///
    /// Returns the same statistics that [`gc`](Self::gc) would return,
    /// but no files are actually deleted.
    ///
    /// # Locking
    ///
    /// A shared lock is held for the duration of this operation (readers
    /// are not blocked).
    #[context("Running garbage collection dry run")]
    pub fn gc_dry_run(&self, additional_roots: &[&str]) -> Result<GcResult> {
        // Shared lock is sufficient since we don't modify anything
        flock(&self.repository, FlockOperation::LockShared)
            .context("Acquiring shared lock for GC dry run")?;
        self.gc_impl(additional_roots, true)
    }

    /// Internal GC implementation (lock must already be held).
    #[context("GC implementation (dry_run: {dry_run})")]
    fn gc_impl(&self, additional_roots: &[&str], dry_run: bool) -> Result<GcResult> {
        let mut result = GcResult::default();
        let mut live_objects = HashSet::new();

        // Build set of additional roots (checked in both images and streams)
        let extra_roots: HashSet<_> = additional_roots.iter().map(|s| s.to_string()).collect();

        // Collect images: those in images/refs plus caller-specified roots
        let all_images = self
            .gc_category("images", GCCategoryWalkMode::AllEntries)
            .context("Collecting all images")?;
        let root_images: Vec<_> = self
            .gc_category("images", GCCategoryWalkMode::RefsOnly)
            .context("Collecting image refs")?
            .into_iter()
            .chain(
                all_images
                    .into_iter()
                    .filter(|(_, name)| extra_roots.contains(name)),
            )
            .collect();

        for ref image in root_images {
            trace!("{image:?} lives as an image");
            live_objects.insert(image.0.clone());
            self.objects_for_image(&image.1)
                .with_context(|| format!("Collecting objects for image {}", image.1))?
                .iter()
                .for_each(|id| {
                    trace!("   with {id:?}");
                    live_objects.insert(id.clone());
                });
        }

        // Collect all streams for the name map, then filter to roots
        let all_streams = self
            .gc_category("streams", GCCategoryWalkMode::AllEntries)
            .context("Collecting all streams")?;
        let stream_name_map: HashMap<_, _> = all_streams.iter().cloned().collect();
        let root_streams: Vec<_> = self
            .gc_category("streams", GCCategoryWalkMode::RefsOnly)
            .context("Collecting stream refs")?
            .into_iter()
            .chain(
                all_streams
                    .into_iter()
                    .filter(|(_, name)| extra_roots.contains(name)),
            )
            .collect();

        let mut walked_streams = HashSet::new();
        for stream in root_streams {
            trace!("{stream:?} lives as a stream");
            live_objects.insert(stream.0.clone());
            self.walk_streams(
                &stream_name_map,
                &stream.1,
                &mut walked_streams,
                &mut live_objects,
            )
            .with_context(|| format!("Walking stream {}", stream.1))?;
        }

        // Walk all objects and remove unreferenced ones
        for first_byte in 0x0..=0xff {
            let dirfd = match self.openat(
                &format!("objects/{first_byte:02x}"),
                OFlags::RDONLY | OFlags::DIRECTORY,
            ) {
                Ok(fd) => fd,
                Err(Errno::NOENT) => continue,
                Err(e) => Err(e)?,
            };
            for item in Dir::read_from(&dirfd)
                .with_context(|| format!("Reading objects/{first_byte:02x} directory"))?
            {
                let entry = item.context("Reading object directory entry")?;
                let filename = entry.file_name();
                if filename != c"." && filename != c".." {
                    let id =
                        ObjectID::from_object_dir_and_basename(first_byte, filename.to_bytes())
                            .context("Parsing object ID from directory entry")?;
                    if !live_objects.contains(&id) {
                        // Get file size before removing
                        if let Ok(stat) = statat(&dirfd, filename, AtFlags::empty()) {
                            result.objects_bytes += stat.st_size as u64;
                        }
                        result.objects_removed += 1;

                        debug!(
                            "{}: objects/{first_byte:02x}/{filename:?}",
                            if dry_run { "would remove" } else { "removing" },
                        );

                        if !dry_run {
                            unlinkat(&dirfd, filename, AtFlags::empty()).with_context(|| {
                                format!("Unlinking object {first_byte:02x}/{filename:?}")
                            })?;
                        }
                    } else {
                        trace!("objects/{first_byte:02x}/{filename:?} lives");
                    }
                }
            }
        }

        // Clean up broken symlinks
        result.images_pruned = self
            .cleanup_gc_category("images", dry_run)
            .context("Cleaning up broken image symlinks")?;
        result.streams_pruned = self
            .cleanup_gc_category("streams", dry_run)
            .context("Cleaning up broken stream symlinks")?;

        // Downgrade to shared lock if we had exclusive (for actual GC)
        if !dry_run {
            flock(&self.repository, FlockOperation::LockShared)
                .context("Downgrading to shared lock after GC")?;
        }
        Ok(result)
    }

    /// Check the structural integrity of the repository.
    ///
    /// Walks all objects, streams, and images in the repository, verifying:
    /// - Object fsverity digests match their path-derived identifiers
    /// - Stream and image symlinks resolve to existing objects
    /// - Stream/image refs resolve to valid entries
    /// - Splitstreams have valid headers and reference only existing objects
    ///
    /// Object directories are checked in parallel using `spawn_blocking`,
    /// with concurrency bounded by `available_parallelism()`.
    ///
    /// Returns a [`FsckResult`] summarizing the findings. Does not modify
    /// any repository contents.
    #[context("Running filesystem consistency check")]
    pub async fn fsck(&self) -> Result<FsckResult> {
        let mut result = FsckResult::default();

        // Phase 0: Validate meta.json if present
        self.fsck_metadata(&mut result);

        // Phase 1: Verify all objects (parallel across object subdirectories)
        self.fsck_objects(&mut result)
            .await
            .context("Checking objects")?;

        // Phase 2: Verify stream symlinks and splitstream integrity
        self.fsck_category("streams", &mut result)
            .context("Checking streams")?;

        // Phase 3: Verify image symlinks
        self.fsck_category("images", &mut result)
            .context("Checking images")?;

        Ok(result)
    }

    /// Validate `meta.json`.
    ///
    /// Since `open_path` already requires `meta.json` to exist and be
    /// parseable, this re-reads from disk to verify on-disk integrity
    /// and checks algorithm compatibility.
    fn fsck_metadata(&self, result: &mut FsckResult) {
        match read_repo_metadata(&self.repository) {
            Ok(Some(meta)) => {
                result.has_metadata = true;
                if let Err(e) = meta.check_compatible::<ObjectID>() {
                    result.errors.push(FsckError::MetadataAlgorithmMismatch {
                        expected: meta.algorithm.to_string(),
                        actual: ObjectID::ALGORITHM.hash_name().to_string(),
                    });
                    log::warn!("meta.json algorithm mismatch: {e}");
                }
            }
            Ok(None) => {
                // Should not happen since open_path requires meta.json,
                // but report it if the file was removed after open.
                result.errors.push(FsckError::MetadataParseFailed {
                    detail: format!(
                        "{REPO_METADATA_FILENAME} not found; \
                         expected because repository was opened successfully"
                    ),
                });
            }
            Err(e) => {
                result.errors.push(FsckError::MetadataParseFailed {
                    detail: format!("{e:#}"),
                });
            }
        }
    }

    /// Verify all objects in the repository have correct fsverity digests.
    ///
    /// Each `objects/XX/` subdirectory is checked on a blocking thread via
    /// `tokio::task::spawn_blocking`, with bounded concurrency to avoid
    /// overwhelming the system with I/O.
    async fn fsck_objects(&self, result: &mut FsckResult) -> Result<()> {
        // Cap at available CPUs; the work is a mix of I/O (reading objects)
        // and CPU (computing verity hashes).
        let max_concurrent = available_parallelism().map(|n| n.get()).unwrap_or(4);
        let insecure = self.insecure;

        let mut joinset = tokio::task::JoinSet::new();
        let mut partial_results = Vec::new();

        for first_byte in 0x00..=0xffu8 {
            // Drain completed tasks if we're at the concurrency limit
            while joinset.len() >= max_concurrent {
                partial_results.push(joinset.join_next().await.unwrap()??);
            }

            let dirfd = match self.openat(
                &format!("objects/{first_byte:02x}"),
                OFlags::RDONLY | OFlags::DIRECTORY,
            ) {
                Ok(fd) => fd,
                Err(Errno::NOENT) => continue,
                Err(e) => {
                    Err(e).with_context(|| format!("Opening objects/{first_byte:02x} directory"))?
                }
            };

            joinset
                .spawn_blocking(move || fsck_object_dir::<ObjectID>(dirfd, first_byte, insecure));
        }

        // Drain remaining tasks
        while let Some(output) = joinset.join_next().await {
            partial_results.push(output??);
        }

        // Fold all per-directory results into the main result
        for partial in partial_results {
            result.objects_checked += partial.objects_checked;
            result.objects_corrupted += partial.objects_corrupted;
            result.errors.extend(partial.errors);
        }

        Ok(())
    }

    /// Verify symlink integrity and splitstream/image validity for a category
    /// ("streams" or "images").
    #[context("Checking {category} integrity")]
    fn fsck_category(&self, category: &str, result: &mut FsckResult) -> Result<()> {
        let is_streams = category == "streams";

        let Some(category_fd) = self
            .openat(category, OFlags::RDONLY | OFlags::DIRECTORY)
            .filter_errno(Errno::NOENT)
            .with_context(|| format!("Opening {category} directory"))?
        else {
            return Ok(());
        };

        // Check first-level symlinks: each should point to an existing object
        for item in
            Dir::read_from(&category_fd).with_context(|| format!("Reading {category} directory"))?
        {
            let entry = item.context("Reading directory entry")?;
            let filename = entry.file_name();
            if filename == c"." || filename == c".." || filename == c"refs" {
                continue;
            }

            if is_streams {
                result.streams_checked += 1;
            } else {
                result.images_checked += 1;
            }

            if entry.file_type() != FileType::Symlink {
                if is_streams {
                    result.streams_corrupted += 1;
                } else {
                    result.images_corrupted += 1;
                }
                result.errors.push(FsckError::EntryNotSymlink {
                    path: format!(
                        "{category}/{}",
                        String::from_utf8_lossy(filename.to_bytes())
                    ),
                });
                continue;
            }

            // Check the symlink resolves (follows through to the object)
            match statat(&category_fd, filename, AtFlags::empty()) {
                Ok(_) => {}
                Err(Errno::NOENT) => {
                    result.broken_links += 1;
                    if is_streams {
                        result.streams_corrupted += 1;
                    } else {
                        result.images_corrupted += 1;
                    }
                    result.errors.push(FsckError::BrokenSymlink {
                        path: format!(
                            "{category}/{}",
                            String::from_utf8_lossy(filename.to_bytes())
                        ),
                    });
                    continue;
                }
                Err(e) => {
                    result.errors.push(FsckError::StatFailed {
                        path: format!(
                            "{category}/{}",
                            String::from_utf8_lossy(filename.to_bytes())
                        ),
                        detail: e.to_string(),
                    });
                    continue;
                }
            }

            let name = String::from_utf8_lossy(filename.to_bytes()).to_string();
            if is_streams {
                // Validate splitstream contents
                self.fsck_splitstream(&name, result);
            } else {
                // Validate erofs image structure and object references
                self.fsck_image(&name, result);
            }
        }

        // Check refs/ symlinks
        let refs_fd = match openat(
            &category_fd,
            c"refs",
            OFlags::RDONLY | OFlags::DIRECTORY | OFlags::CLOEXEC,
            Mode::empty(),
        )
        .filter_errno(Errno::NOENT)
        .with_context(|| format!("Opening {category}/refs directory"))?
        {
            Some(fd) => fd,
            None => return Ok(()),
        };

        self.fsck_refs_dir(&refs_fd, category, "", result)
            .with_context(|| format!("Checking {category}/refs"))
    }

    /// Recursively verify that all ref symlinks resolve to valid entries in the
    /// parent category directory.
    fn fsck_refs_dir(
        &self,
        refs_fd: &OwnedFd,
        category: &str,
        prefix: &str,
        result: &mut FsckResult,
    ) -> Result<()> {
        for item in Dir::read_from(refs_fd)
            .with_context(|| format!("Reading {category}/refs/{prefix} directory"))?
        {
            let entry = item.context("Reading refs directory entry")?;
            let filename = entry.file_name();
            if filename == c"." || filename == c".." {
                continue;
            }

            let name = String::from_utf8_lossy(filename.to_bytes()).to_string();
            let display_path = if prefix.is_empty() {
                format!("{category}/refs/{name}")
            } else {
                format!("{category}/refs/{prefix}/{name}")
            };

            match entry.file_type() {
                FileType::Directory => {
                    let subdir = openat(
                        refs_fd,
                        filename,
                        OFlags::RDONLY | OFlags::DIRECTORY | OFlags::CLOEXEC,
                        Mode::empty(),
                    )
                    .with_context(|| format!("Opening {display_path}"))?;
                    let sub_prefix = if prefix.is_empty() {
                        name.clone()
                    } else {
                        format!("{prefix}/{name}")
                    };
                    self.fsck_refs_dir(&subdir, category, &sub_prefix, result)?;
                }
                FileType::Symlink => {
                    // The ref should ultimately resolve to a file (following
                    // the chain: refs/X -> ../../entry -> ../objects/XX/YY)
                    match statat(refs_fd, filename, AtFlags::empty()) {
                        Ok(_) => {}
                        Err(Errno::NOENT) => {
                            result.broken_links += 1;
                            result.errors.push(FsckError::BrokenSymlink {
                                path: display_path.clone(),
                            });
                        }
                        Err(e) => {
                            result.errors.push(FsckError::StatFailed {
                                path: display_path.clone(),
                                detail: e.to_string(),
                            });
                        }
                    }
                }
                other => {
                    result.errors.push(FsckError::UnexpectedFileType {
                        path: display_path.clone(),
                        detail: format!("{other:?}"),
                    });
                }
            }
        }
        Ok(())
    }

    /// Validate a single splitstream: check header and object references.
    fn fsck_splitstream(&self, stream_name: &str, result: &mut FsckResult) {
        let stream_path = format!("streams/{stream_name}");
        let mut split_stream = match self.open_stream(stream_name, None, None) {
            Ok(s) => s,
            Err(e) => {
                result.streams_corrupted += 1;
                result.errors.push(FsckError::StreamOpenFailed {
                    path: stream_path,
                    detail: e.to_string(),
                });
                return;
            }
        };

        // Check that all object_refs point to existing objects
        let check_result = split_stream.get_object_refs(|id| {
            let obj_path = Self::format_object_path(id);
            match self.openat(&obj_path, OFlags::RDONLY) {
                Ok(_) => {}
                Err(Errno::NOENT) => {
                    result.missing_objects += 1;
                    result.errors.push(FsckError::MissingObjectRef {
                        path: stream_path.clone(),
                        object_id: id.to_hex(),
                    });
                }
                Err(e) => {
                    result.errors.push(FsckError::ObjectCheckFailed {
                        path: stream_path.clone(),
                        object_id: id.to_hex(),
                        detail: e.to_string(),
                    });
                }
            }
        });
        if let Err(e) = check_result {
            result.streams_corrupted += 1;
            result.errors.push(FsckError::StreamReadFailed {
                path: stream_path,
                detail: e.to_string(),
            });
            return;
        }

        // Check that all named refs (stream refs) point to existing objects
        for (ref_name, ref_id) in split_stream.iter_named_refs() {
            // The named ref's object should exist
            let obj_path = Self::format_object_path(ref_id);
            match self.openat(&obj_path, OFlags::RDONLY) {
                Ok(_) => {}
                Err(Errno::NOENT) => {
                    result.missing_objects += 1;
                    result.errors.push(FsckError::MissingNamedRef {
                        path: stream_path.clone(),
                        ref_name: ref_name.to_string(),
                        object_id: ref_id.to_hex(),
                    });
                }
                Err(e) => {
                    result.errors.push(FsckError::ObjectCheckFailed {
                        path: stream_path.clone(),
                        object_id: ref_id.to_hex(),
                        detail: format!("checking named ref '{ref_name}': {e}"),
                    });
                }
            }
            // The stream entry itself should also exist (but don't double-count).
            // Note: the named ref name may not correspond to an actual stream
            // entry.  OCI images use named refs with keys like
            // "config:sha256:..." or layer diff_ids that aren't stream names.
            // We only warn if the object itself is missing (handled above);
            // a missing stream entry with an existing object is benign.
        }
    }

    /// Validate a single erofs image: parse structure, enforce composefs
    /// invariants, and verify all referenced objects exist.
    fn fsck_image(&self, image_name: &str, result: &mut FsckResult) {
        // Read the image data
        let image_path = format!("images/{image_name}");
        let mut data = vec![];
        let fd = match self.openat(&image_path, OFlags::RDONLY) {
            Ok(fd) => fd,
            Err(e) => {
                result.images_corrupted += 1;
                result.errors.push(FsckError::ImageOpenFailed {
                    path: image_path,
                    detail: e.to_string(),
                });
                return;
            }
        };
        if let Err(e) = File::from(fd).read_to_end(&mut data) {
            result.images_corrupted += 1;
            result.errors.push(FsckError::ImageReadFailed {
                path: image_path,
                detail: e.to_string(),
            });
            return;
        }

        // Parse the erofs image with composefs-specific structural validation
        // (header magic, superblock, no unsupported features, etc.) and walk
        // the directory tree to collect all referenced object IDs.
        let objects = match crate::erofs::reader::collect_objects::<ObjectID>(&data) {
            Ok(objects) => objects,
            Err(e) => {
                result.images_corrupted += 1;
                result.errors.push(FsckError::ImageInvalid {
                    path: image_path,
                    detail: e.to_string(),
                });
                return;
            }
        };

        // Verify all referenced objects exist
        for obj_id in &objects {
            let path = Self::format_object_path(obj_id);
            match self.openat(&path, OFlags::RDONLY) {
                Ok(_) => {}
                Err(Errno::NOENT) => {
                    result.missing_objects += 1;
                    result.errors.push(FsckError::ImageMissingObject {
                        path: image_path.clone(),
                        object_id: obj_id.to_hex(),
                    });
                }
                Err(e) => {
                    result.errors.push(FsckError::ObjectCheckFailed {
                        path: image_path.clone(),
                        object_id: obj_id.to_hex(),
                        detail: e.to_string(),
                    });
                }
            }
        }
    }

    /// Returns a borrowed file descriptor for the repository root.
    ///
    /// This allows low-level operations on the repository directory.
    pub fn repo_fd(&self) -> BorrowedFd<'_> {
        self.repository.as_fd()
    }

    /// Return the repository metadata parsed from `meta.json` at open time.
    ///
    /// The metadata was already validated against this repository's
    /// `ObjectID` type when the repository was opened, so no further
    /// compatibility check is needed.
    pub fn metadata(&self) -> &RepoMetadata {
        &self.metadata
    }

    /// Lists all named stream references under a given prefix.
    ///
    /// Returns (name, target) pairs where name is relative to the prefix.
    pub fn list_stream_refs(&self, prefix: &str) -> Result<Vec<(String, String)>> {
        let ref_path = format!("streams/refs/{prefix}");

        let dir_fd = match self.openat(&ref_path, OFlags::RDONLY | OFlags::DIRECTORY) {
            Ok(fd) => fd,
            Err(Errno::NOENT) => return Ok(Vec::new()),
            Err(e) => return Err(e.into()),
        };

        let mut refs = Vec::new();
        for item in Dir::read_from(&dir_fd)? {
            let entry = item?;
            let name_bytes = entry.file_name().to_bytes();

            if name_bytes == b"." || name_bytes == b".." {
                continue;
            }

            let name = match std::str::from_utf8(name_bytes) {
                Ok(s) => s.to_string(),
                Err(_) => continue,
            };

            if let Ok(target) = readlinkat(&dir_fd, name_bytes, vec![])
                && let Ok(target_str) = target.into_string()
            {
                refs.push((name, target_str));
            }
        }

        Ok(refs)
    }
}

/// Verify each object in a single `objects/XX/` subdirectory.
///
/// This is a free function (not a method) so it can be used with
/// `spawn_blocking` — it captures only owned/`Send` values.
fn fsck_object_dir<ObjectID: FsVerityHashValue>(
    dirfd: OwnedFd,
    first_byte: u8,
    insecure: bool,
) -> Result<FsckResult> {
    let mut result = FsckResult::default();

    for item in Dir::read_from(&dirfd)
        .with_context(|| format!("Reading objects/{first_byte:02x} directory"))?
    {
        let entry = item.context("Reading object directory entry")?;
        let filename = entry.file_name();
        if filename == c"." || filename == c".." {
            continue;
        }

        result.objects_checked += 1;

        let expected_id =
            match ObjectID::from_object_dir_and_basename(first_byte, filename.to_bytes()) {
                Ok(id) => id,
                Err(e) => {
                    result.objects_corrupted += 1;
                    result.errors.push(FsckError::ObjectInvalidName {
                        path: format!(
                            "objects/{first_byte:02x}/{}",
                            String::from_utf8_lossy(filename.to_bytes())
                        ),
                        detail: e.to_string(),
                    });
                    continue;
                }
            };

        let fd = match openat(
            &dirfd,
            filename,
            OFlags::RDONLY | OFlags::CLOEXEC,
            Mode::empty(),
        ) {
            Ok(fd) => fd,
            Err(e) => {
                result.objects_corrupted += 1;
                result.errors.push(FsckError::ObjectOpenFailed {
                    path: format!(
                        "objects/{first_byte:02x}/{}",
                        String::from_utf8_lossy(filename.to_bytes())
                    ),
                    detail: e.to_string(),
                });
                continue;
            }
        };

        let Some(measured) =
            fsck_measure_object::<ObjectID>(fd, &expected_id, insecure, &mut result)
        else {
            continue;
        };

        if measured != expected_id {
            result.objects_corrupted += 1;
            result.errors.push(FsckError::ObjectDigestMismatch {
                path: format!("objects/{}", expected_id.to_object_pathname()),
                measured: measured.to_hex(),
            });
        }
    }
    Ok(result)
}

/// Measure the verity digest of a single object file.
///
/// Returns `Some(digest)` on success, or `None` after recording the error
/// in `result` (so the caller can `continue`).
fn fsck_measure_object<ObjectID: FsVerityHashValue>(
    fd: OwnedFd,
    expected_id: &ObjectID,
    insecure: bool,
    result: &mut FsckResult,
) -> Option<ObjectID> {
    if let Ok(digest) = measure_verity::<ObjectID>(&fd) {
        return Some(digest);
    }

    // Kernel measurement failed — in insecure mode, try userspace computation
    if insecure {
        match Repository::<ObjectID>::compute_verity_digest(&mut std::io::BufReader::new(
            File::from(fd),
        )) {
            Ok(digest) => return Some(digest),
            Err(e) => {
                result.objects_corrupted += 1;
                result.errors.push(FsckError::ObjectVerityFailed {
                    path: format!("objects/{}", expected_id.to_object_pathname()),
                    detail: e.to_string(),
                });
                return None;
            }
        }
    }

    // Not insecure — verity is required but missing/unsupported
    result.objects_corrupted += 1;
    result.errors.push(FsckError::ObjectVerityMissing {
        path: format!("objects/{}", expected_id.to_object_pathname()),
    });
    None
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::fsverity::{Sha256HashValue, Sha512HashValue};
    use crate::test::tempdir;
    use rustix::fs::{CWD, statat};
    use tempfile::TempDir;

    /// Create a test repository in insecure mode (no fs-verity required).
    fn create_test_repo(path: &Path) -> Result<Arc<Repository<Sha512HashValue>>> {
        let (repo, _) = Repository::init_path(CWD, path, Algorithm::SHA512, false)?;
        Ok(Arc::new(repo))
    }

    /// Generate deterministic test data of a given size.
    fn generate_test_data(size: u64, seed: u8) -> Vec<u8> {
        (0..size)
            .map(|i| ((i as u8).wrapping_add(seed)).wrapping_mul(17))
            .collect()
    }

    fn read_links_in_repo<P>(tmp: &TempDir, repo_sub_path: P) -> Result<Option<PathBuf>>
    where
        P: AsRef<Path>,
    {
        let full_path = tmp.path().join("repo").join(repo_sub_path);
        match readlinkat(CWD, &full_path, Vec::new()) {
            Ok(result) => Ok(Some(PathBuf::from(result.to_str()?))),
            Err(rustix::io::Errno::NOENT) => Ok(None),
            Err(e) => Err(e.into()),
        }
    }

    // Does not follow symlinks
    fn test_path_exists_in_repo<P>(tmp: &TempDir, repo_sub_path: P) -> Result<bool>
    where
        P: AsRef<Path>,
    {
        let full_path = tmp.path().join("repo").join(repo_sub_path);
        match statat(CWD, &full_path, AtFlags::SYMLINK_NOFOLLOW) {
            Ok(_) => Ok(true),
            Err(rustix::io::Errno::NOENT) => Ok(false),
            Err(e) => Err(e.into()),
        }
    }

    fn test_object_exists(tmp: &TempDir, obj: &Sha512HashValue) -> Result<bool> {
        let digest = obj.to_hex();
        let (first_two, remainder) = digest.split_at(2);
        test_path_exists_in_repo(tmp, &format!("objects/{first_two}/{remainder}"))
    }

    #[test]
    fn test_gc_removes_one_stream() -> Result<()> {
        let tmp = tempdir();
        let repo = create_test_repo(&tmp.path().join("repo"))?;

        let obj1 = generate_test_data(32 * 1024, 0xAE);
        let obj2 = generate_test_data(64 * 1024, 0xEA);

        let obj1_id = repo.ensure_object(&obj1)?;
        let obj2_id: Sha512HashValue = compute_verity(&obj2);

        let mut writer = repo.create_stream(0)?;
        writer.write_external(&obj2)?;
        let _stream_id = repo.write_stream(writer, "test-stream", None)?;

        repo.sync()?;

        assert!(test_object_exists(&tmp, &obj1_id)?);
        assert!(test_object_exists(&tmp, &obj2_id)?);
        assert!(test_path_exists_in_repo(&tmp, "streams/test-stream")?);
        let link_target =
            read_links_in_repo(&tmp, "streams/test-stream")?.expect("link is not broken");
        assert!(test_path_exists_in_repo(
            &tmp,
            PathBuf::from("streams").join(&link_target)
        )?);

        // Now perform gc - should remove 2 objects (obj1 + obj2) and 1 stream symlink
        let result = repo.gc(&[])?;

        assert!(!test_object_exists(&tmp, &obj1_id)?);
        assert!(!test_object_exists(&tmp, &obj2_id)?);
        assert!(!test_path_exists_in_repo(&tmp, "streams/test-stream")?);

        // Verify GcResult: 3 objects removed (obj1, obj2, splitstream), stream symlink pruned
        assert_eq!(result.objects_removed, 3);
        assert!(result.objects_bytes > 0);
        assert_eq!(result.streams_pruned, 1);
        assert_eq!(result.images_pruned, 0);
        Ok(())
    }

    #[test]
    fn test_gc_keeps_one_stream() -> Result<()> {
        let tmp = tempdir();
        let repo = create_test_repo(&tmp.path().join("repo"))?;

        let obj1 = generate_test_data(32 * 1024, 0xAE);
        let obj2 = generate_test_data(64 * 1024, 0xEA);

        let obj1_id = repo.ensure_object(&obj1)?;
        let obj2_id: Sha512HashValue = compute_verity(&obj2);

        let mut writer = repo.create_stream(0)?;
        writer.write_external(&obj2)?;
        let _stream_id = repo.write_stream(writer, "test-stream", None)?;

        repo.sync()?;

        assert!(test_object_exists(&tmp, &obj1_id)?);
        assert!(test_object_exists(&tmp, &obj2_id)?);
        assert!(test_path_exists_in_repo(&tmp, "streams/test-stream")?);
        let link_target =
            read_links_in_repo(&tmp, "streams/test-stream")?.expect("link is not broken");
        assert!(test_path_exists_in_repo(
            &tmp,
            PathBuf::from("streams").join(&link_target)
        )?);

        // Now perform gc - should remove only obj1, keep obj2 and stream
        let result = repo.gc(&["test-stream"])?;

        assert!(!test_object_exists(&tmp, &obj1_id)?);
        assert!(test_object_exists(&tmp, &obj2_id)?);
        assert!(test_path_exists_in_repo(&tmp, "streams/test-stream")?);
        let link_target =
            read_links_in_repo(&tmp, "streams/test-stream")?.expect("link is not broken");
        assert!(test_path_exists_in_repo(
            &tmp,
            PathBuf::from("streams").join(&link_target)
        )?);

        // Verify GcResult: only 1 object removed, no symlinks pruned
        assert_eq!(result.objects_removed, 1);
        assert!(result.objects_bytes > 0);
        assert_eq!(result.streams_pruned, 0);
        assert_eq!(result.images_pruned, 0);
        Ok(())
    }

    #[test]
    fn test_gc_keeps_one_stream_from_refs() -> Result<()> {
        let tmp = tempdir();
        let repo = create_test_repo(&tmp.path().join("repo"))?;

        let obj1 = generate_test_data(32 * 1024, 0xAE);
        let obj2 = generate_test_data(64 * 1024, 0xEA);

        let obj1_id = repo.ensure_object(&obj1)?;
        let obj2_id: Sha512HashValue = compute_verity(&obj2);

        let mut writer = repo.create_stream(0)?;
        writer.write_external(&obj2)?;
        let _stream_id = repo.write_stream(writer, "test-stream", Some("ref-name"))?;

        repo.sync()?;

        assert!(test_object_exists(&tmp, &obj1_id)?);
        assert!(test_object_exists(&tmp, &obj2_id)?);
        assert!(test_path_exists_in_repo(&tmp, "streams/test-stream")?);
        let link_target =
            read_links_in_repo(&tmp, "streams/test-stream")?.expect("link is not broken");
        assert!(test_path_exists_in_repo(
            &tmp,
            PathBuf::from("streams").join(&link_target)
        )?);

        // Now perform gc - stream is kept via ref, only obj1 removed
        let result = repo.gc(&[])?;

        assert!(!test_object_exists(&tmp, &obj1_id)?);
        assert!(test_object_exists(&tmp, &obj2_id)?);
        assert!(test_path_exists_in_repo(&tmp, "streams/test-stream")?);
        let link_target =
            read_links_in_repo(&tmp, "streams/test-stream")?.expect("link is not broken");
        assert!(test_path_exists_in_repo(
            &tmp,
            PathBuf::from("streams").join(&link_target)
        )?);

        // Verify GcResult: 1 object removed, no symlinks pruned (stream has ref)
        assert_eq!(result.objects_removed, 1);
        assert!(result.objects_bytes > 0);
        assert_eq!(result.streams_pruned, 0);
        assert_eq!(result.images_pruned, 0);
        Ok(())
    }

    #[test]
    fn test_gc_keeps_one_stream_from_two_overlapped() -> Result<()> {
        let tmp = tempdir();
        let repo = create_test_repo(&tmp.path().join("repo"))?;

        let obj1 = generate_test_data(32 * 1024, 0xAE);
        let obj2 = generate_test_data(64 * 1024, 0xEA);
        let obj3 = generate_test_data(64 * 1024, 0xAA);
        let obj4 = generate_test_data(64 * 1024, 0xEE);

        let obj1_id = repo.ensure_object(&obj1)?;
        let obj2_id: Sha512HashValue = compute_verity(&obj2);
        let obj3_id: Sha512HashValue = compute_verity(&obj3);
        let obj4_id: Sha512HashValue = compute_verity(&obj4);

        let mut writer1 = repo.create_stream(0)?;
        writer1.write_external(&obj2)?;
        writer1.write_external(&obj3)?;
        let _stream1_id = repo.write_stream(writer1, "test-stream1", None)?;

        let mut writer2 = repo.create_stream(0)?;
        writer2.write_external(&obj2)?;
        writer2.write_external(&obj4)?;
        let _stream2_id = repo.write_stream(writer2, "test-stream2", None)?;

        repo.sync()?;

        assert!(test_object_exists(&tmp, &obj1_id)?);
        assert!(test_object_exists(&tmp, &obj2_id)?);
        assert!(test_object_exists(&tmp, &obj3_id)?);
        assert!(test_object_exists(&tmp, &obj4_id)?);
        assert!(test_path_exists_in_repo(&tmp, "streams/test-stream1")?);
        let link_target =
            read_links_in_repo(&tmp, "streams/test-stream1")?.expect("link is not broken");
        assert!(test_path_exists_in_repo(
            &tmp,
            PathBuf::from("streams").join(&link_target)
        )?);
        assert!(test_path_exists_in_repo(&tmp, "streams/test-stream2")?);
        let link_target =
            read_links_in_repo(&tmp, "streams/test-stream2")?.expect("link is not broken");
        assert!(test_path_exists_in_repo(
            &tmp,
            PathBuf::from("streams").join(&link_target)
        )?);

        // Now perform gc - keep stream1, remove obj1, obj4, and stream2
        let result = repo.gc(&["test-stream1"])?;

        assert!(!test_object_exists(&tmp, &obj1_id)?);
        assert!(test_object_exists(&tmp, &obj2_id)?);
        assert!(test_object_exists(&tmp, &obj3_id)?);
        assert!(!test_object_exists(&tmp, &obj4_id)?);
        assert!(test_path_exists_in_repo(&tmp, "streams/test-stream1")?);
        let link_target =
            read_links_in_repo(&tmp, "streams/test-stream1")?.expect("link is not broken");
        assert!(test_path_exists_in_repo(
            &tmp,
            PathBuf::from("streams").join(&link_target)
        )?);
        assert!(!test_path_exists_in_repo(&tmp, "streams/test-stream2")?);

        // Verify GcResult: 3 objects removed (obj1, obj4, stream2's splitstream), 1 stream pruned
        assert_eq!(result.objects_removed, 3);
        assert!(result.objects_bytes > 0);
        assert_eq!(result.streams_pruned, 1);
        assert_eq!(result.images_pruned, 0);
        Ok(())
    }

    #[test]
    fn test_gc_keeps_named_references() -> Result<()> {
        let tmp = tempdir();
        let repo = create_test_repo(&tmp.path().join("repo"))?;

        let obj1 = generate_test_data(32 * 1024, 0xAE);
        let obj2 = generate_test_data(64 * 1024, 0xEA);

        let obj1_id = repo.ensure_object(&obj1)?;
        let obj2_id: Sha512HashValue = compute_verity(&obj2);

        let mut writer1 = repo.create_stream(0)?;
        writer1.write_external(&obj2)?;
        let stream1_id = repo.write_stream(writer1, "test-stream1", None)?;

        let mut writer2 = repo.create_stream(0)?;
        writer2.add_named_stream_ref("test-stream1", &stream1_id);
        let _stream2_id = repo.write_stream(writer2, "test-stream2", None)?;

        repo.sync()?;

        assert!(test_object_exists(&tmp, &obj1_id)?);
        assert!(test_object_exists(&tmp, &obj2_id)?);
        assert!(test_path_exists_in_repo(&tmp, "streams/test-stream1")?);
        let link_target =
            read_links_in_repo(&tmp, "streams/test-stream1")?.expect("link is not broken");
        assert!(test_path_exists_in_repo(
            &tmp,
            PathBuf::from("streams").join(&link_target)
        )?);
        assert!(test_path_exists_in_repo(&tmp, "streams/test-stream2")?);
        let link_target =
            read_links_in_repo(&tmp, "streams/test-stream2")?.expect("link is not broken");
        assert!(test_path_exists_in_repo(
            &tmp,
            PathBuf::from("streams").join(&link_target)
        )?);

        // Now perform gc - stream2 refs stream1, both kept, only obj1 removed
        let result = repo.gc(&["test-stream2"])?;

        assert!(!test_object_exists(&tmp, &obj1_id)?);
        assert!(test_object_exists(&tmp, &obj2_id)?);
        assert!(test_path_exists_in_repo(&tmp, "streams/test-stream1")?);
        let link_target =
            read_links_in_repo(&tmp, "streams/test-stream1")?.expect("link is not broken");
        assert!(test_path_exists_in_repo(
            &tmp,
            PathBuf::from("streams").join(&link_target)
        )?);
        assert!(test_path_exists_in_repo(&tmp, "streams/test-stream2")?);
        let link_target =
            read_links_in_repo(&tmp, "streams/test-stream2")?.expect("link is not broken");
        assert!(test_path_exists_in_repo(
            &tmp,
            PathBuf::from("streams").join(&link_target)
        )?);

        // Verify GcResult: 1 object removed, no symlinks pruned
        assert_eq!(result.objects_removed, 1);
        assert!(result.objects_bytes > 0);
        assert_eq!(result.streams_pruned, 0);
        assert_eq!(result.images_pruned, 0);
        Ok(())
    }

    #[test]
    fn test_gc_keeps_named_references_with_different_table_name() -> Result<()> {
        let tmp = tempdir();
        let repo = create_test_repo(&tmp.path().join("repo"))?;

        let obj1 = generate_test_data(32 * 1024, 0xAE);
        let obj2 = generate_test_data(64 * 1024, 0xEA);

        let obj1_id = repo.ensure_object(&obj1)?;
        let obj2_id: Sha512HashValue = compute_verity(&obj2);

        let mut writer1 = repo.create_stream(0)?;
        writer1.write_external(&obj2)?;
        let stream1_id = repo.write_stream(writer1, "test-stream1", None)?;

        let mut writer2 = repo.create_stream(0)?;
        writer2.add_named_stream_ref("different-table-name-for-test-stream1", &stream1_id);
        let _stream2_id = repo.write_stream(writer2, "test-stream2", None)?;

        repo.sync()?;

        assert!(test_object_exists(&tmp, &obj1_id)?);
        assert!(test_object_exists(&tmp, &obj2_id)?);
        assert!(test_path_exists_in_repo(&tmp, "streams/test-stream1")?);
        let link_target =
            read_links_in_repo(&tmp, "streams/test-stream1")?.expect("link is not broken");
        assert!(test_path_exists_in_repo(
            &tmp,
            PathBuf::from("streams").join(&link_target)
        )?);
        assert!(test_path_exists_in_repo(&tmp, "streams/test-stream2")?);
        let link_target =
            read_links_in_repo(&tmp, "streams/test-stream2")?.expect("link is not broken");
        assert!(test_path_exists_in_repo(
            &tmp,
            PathBuf::from("streams").join(&link_target)
        )?);

        // Now perform gc - different table name, but same object ID links them
        let result = repo.gc(&["test-stream2"])?;

        assert!(!test_object_exists(&tmp, &obj1_id)?);
        assert!(test_object_exists(&tmp, &obj2_id)?);
        assert!(test_path_exists_in_repo(&tmp, "streams/test-stream1")?);
        let link_target =
            read_links_in_repo(&tmp, "streams/test-stream1")?.expect("link is not broken");
        assert!(test_path_exists_in_repo(
            &tmp,
            PathBuf::from("streams").join(&link_target)
        )?);
        assert!(test_path_exists_in_repo(&tmp, "streams/test-stream2")?);
        let link_target =
            read_links_in_repo(&tmp, "streams/test-stream2")?.expect("link is not broken");
        assert!(test_path_exists_in_repo(
            &tmp,
            PathBuf::from("streams").join(&link_target)
        )?);

        // Verify GcResult: 1 object removed, no symlinks pruned
        assert_eq!(result.objects_removed, 1);
        assert!(result.objects_bytes > 0);
        assert_eq!(result.streams_pruned, 0);
        assert_eq!(result.images_pruned, 0);
        Ok(())
    }

    #[test]
    fn test_gc_keeps_one_named_reference_from_two_overlapped() -> Result<()> {
        let tmp = tempdir();
        let repo = create_test_repo(&tmp.path().join("repo"))?;

        let obj1 = generate_test_data(32 * 1024, 0xAE);
        let obj2 = generate_test_data(64 * 1024, 0xEA);
        let obj3 = generate_test_data(64 * 1024, 0xAA);
        let obj4 = generate_test_data(64 * 1024, 0xEE);

        let obj1_id = repo.ensure_object(&obj1)?;
        let obj2_id: Sha512HashValue = compute_verity(&obj2);
        let obj3_id: Sha512HashValue = compute_verity(&obj3);
        let obj4_id: Sha512HashValue = compute_verity(&obj4);

        let mut writer = repo.create_stream(0)?;
        writer.write_external(&obj2)?;
        let stream1_id = repo.write_stream(writer, "test-stream1", None)?;

        let mut writer = repo.create_stream(0)?;
        writer.write_external(&obj3)?;
        let stream2_id = repo.write_stream(writer, "test-stream2", None)?;

        let mut writer = repo.create_stream(0)?;
        writer.write_external(&obj4)?;
        let stream3_id = repo.write_stream(writer, "test-stream3", None)?;

        let mut writer = repo.create_stream(0)?;
        writer.add_named_stream_ref("test-stream1", &stream1_id);
        writer.add_named_stream_ref("test-stream2", &stream2_id);
        let _ref_stream1_id = repo.write_stream(writer, "ref-stream1", None)?;

        let mut writer = repo.create_stream(0)?;
        writer.add_named_stream_ref("test-stream1", &stream1_id);
        writer.add_named_stream_ref("test-stream3", &stream3_id);
        let _ref_stream2_id = repo.write_stream(writer, "ref-stream2", None)?;

        repo.sync()?;

        assert!(test_object_exists(&tmp, &obj1_id)?);
        assert!(test_object_exists(&tmp, &obj2_id)?);
        assert!(test_object_exists(&tmp, &obj3_id)?);
        assert!(test_object_exists(&tmp, &obj4_id)?);
        assert!(test_path_exists_in_repo(&tmp, "streams/test-stream1")?);
        let link_target =
            read_links_in_repo(&tmp, "streams/test-stream1")?.expect("link is not broken");
        assert!(test_path_exists_in_repo(
            &tmp,
            PathBuf::from("streams").join(&link_target)
        )?);
        assert!(test_path_exists_in_repo(&tmp, "streams/test-stream2")?);
        let link_target =
            read_links_in_repo(&tmp, "streams/test-stream2")?.expect("link is not broken");
        assert!(test_path_exists_in_repo(
            &tmp,
            PathBuf::from("streams").join(&link_target)
        )?);
        assert!(test_path_exists_in_repo(&tmp, "streams/test-stream3")?);
        let link_target =
            read_links_in_repo(&tmp, "streams/test-stream3")?.expect("link is not broken");
        assert!(test_path_exists_in_repo(
            &tmp,
            PathBuf::from("streams").join(&link_target)
        )?);
        assert!(test_path_exists_in_repo(&tmp, "streams/ref-stream1")?);
        let link_target =
            read_links_in_repo(&tmp, "streams/ref-stream1")?.expect("link is not broken");
        assert!(test_path_exists_in_repo(
            &tmp,
            PathBuf::from("streams").join(&link_target)
        )?);
        assert!(test_path_exists_in_repo(&tmp, "streams/ref-stream2")?);
        let link_target =
            read_links_in_repo(&tmp, "streams/ref-stream2")?.expect("link is not broken");
        assert!(test_path_exists_in_repo(
            &tmp,
            PathBuf::from("streams").join(&link_target)
        )?);

        // Now perform gc - ref-stream1 refs stream1+stream2, so keep those and their objects
        let result = repo.gc(&["ref-stream1"])?;

        assert!(!test_object_exists(&tmp, &obj1_id)?);
        assert!(test_object_exists(&tmp, &obj2_id)?);
        assert!(test_object_exists(&tmp, &obj3_id)?);
        assert!(!test_object_exists(&tmp, &obj4_id)?);
        assert!(test_path_exists_in_repo(&tmp, "streams/test-stream1")?);
        let link_target =
            read_links_in_repo(&tmp, "streams/test-stream1")?.expect("link is not broken");
        assert!(test_path_exists_in_repo(
            &tmp,
            PathBuf::from("streams").join(&link_target)
        )?);
        assert!(test_path_exists_in_repo(&tmp, "streams/test-stream2")?);
        let link_target =
            read_links_in_repo(&tmp, "streams/test-stream2")?.expect("link is not broken");
        assert!(test_path_exists_in_repo(
            &tmp,
            PathBuf::from("streams").join(&link_target)
        )?);
        assert!(!test_path_exists_in_repo(&tmp, "streams/test-stream3")?);
        assert!(test_path_exists_in_repo(&tmp, "streams/ref-stream1")?);
        let link_target =
            read_links_in_repo(&tmp, "streams/ref-stream1")?.expect("link is not broken");
        assert!(test_path_exists_in_repo(
            &tmp,
            PathBuf::from("streams").join(&link_target)
        )?);
        assert!(!test_path_exists_in_repo(&tmp, "streams/ref-stream2")?);

        // Verify GcResult: objects removed include obj1, obj4, plus splitstreams for stream3 and ref-stream2
        assert_eq!(result.objects_removed, 4);
        assert!(result.objects_bytes > 0);
        assert_eq!(result.streams_pruned, 2);
        assert_eq!(result.images_pruned, 0);

        Ok(())
    }

    use crate::tree::{FileSystem, Inode, LeafContent, RegularFile, Stat};

    /// Create a default root stat for test filesystems
    fn test_root_stat() -> Stat {
        Stat {
            st_mode: 0o755,
            st_uid: 0,
            st_gid: 0,
            st_mtim_sec: 0,
            xattrs: Default::default(),
        }
    }

    /// Make a test in-memory filesystem that only contains one externally referenced object
    fn make_test_fs(obj: &Sha512HashValue, size: u64) -> FileSystem<Sha512HashValue> {
        let mut fs: FileSystem<Sha512HashValue> = FileSystem::new(test_root_stat());
        let leaf_id = fs.push_leaf(
            Stat {
                st_mode: 0o644,
                st_uid: 0,
                st_gid: 0,
                st_mtim_sec: 0,
                xattrs: Default::default(),
            },
            LeafContent::Regular(RegularFile::External(obj.clone(), size)),
        );
        let inode = Inode::leaf(leaf_id);
        fs.root.insert(OsStr::new("data"), inode);
        fs
    }

    #[test]
    fn test_gc_removes_one_image() -> Result<()> {
        let tmp = tempdir();
        let repo = create_test_repo(&tmp.path().join("repo"))?;

        let obj1_size: u64 = 32 * 1024;
        let obj1 = generate_test_data(obj1_size, 0xAE);
        let obj2_size: u64 = 64 * 1024;
        let obj2 = generate_test_data(obj2_size, 0xEA);

        let obj1_id = repo.ensure_object(&obj1)?;
        let obj2_id = repo.ensure_object(&obj2)?;

        let fs = make_test_fs(&obj2_id, obj2_size);
        let image1 = fs.commit_image(&repo, None)?;
        let image1_path = format!("images/{}", image1.to_hex());

        repo.sync()?;

        assert!(test_object_exists(&tmp, &obj1_id)?);
        assert!(test_object_exists(&tmp, &obj2_id)?);
        assert!(test_path_exists_in_repo(&tmp, &image1_path)?);
        let link_target = read_links_in_repo(&tmp, &image1_path)?.expect("link is not broken");
        assert!(test_path_exists_in_repo(
            &tmp,
            PathBuf::from("images").join(&link_target)
        )?);

        // Now perform gc - no refs, so image and both objects removed
        let result = repo.gc(&[])?;

        assert!(!test_object_exists(&tmp, &obj1_id)?);
        assert!(!test_object_exists(&tmp, &obj2_id)?);
        assert!(!test_path_exists_in_repo(&tmp, &image1_path)?);

        // Verify GcResult: 3 objects removed (obj1, obj2, image erofs), 1 image pruned
        assert_eq!(result.objects_removed, 3);
        assert!(result.objects_bytes > 0);
        assert_eq!(result.images_pruned, 1);
        assert_eq!(result.streams_pruned, 0);
        Ok(())
    }

    #[test]
    fn test_gc_keeps_one_image() -> Result<()> {
        let tmp = tempdir();
        let repo = create_test_repo(&tmp.path().join("repo"))?;

        let obj1_size: u64 = 32 * 1024;
        let obj1 = generate_test_data(obj1_size, 0xAE);
        let obj2_size: u64 = 64 * 1024;
        let obj2 = generate_test_data(obj2_size, 0xEA);

        let obj1_id = repo.ensure_object(&obj1)?;
        let obj2_id = repo.ensure_object(&obj2)?;

        let fs = make_test_fs(&obj2_id, obj2_size);
        let image1 = fs.commit_image(&repo, None)?;
        let image1_path = format!("images/{}", image1.to_hex());

        repo.sync()?;

        assert!(test_object_exists(&tmp, &obj1_id)?);
        assert!(test_object_exists(&tmp, &obj2_id)?);
        assert!(test_path_exists_in_repo(&tmp, &image1_path)?);
        let link_target = read_links_in_repo(&tmp, &image1_path)?.expect("link is not broken");
        assert!(test_path_exists_in_repo(
            &tmp,
            PathBuf::from("images").join(&link_target)
        )?);

        // Now perform gc - keep image via additional_roots
        let image1_hex = image1.to_hex();
        let result = repo.gc(&[image1_hex.as_str()])?;

        assert!(!test_object_exists(&tmp, &obj1_id)?);
        assert!(test_object_exists(&tmp, &obj2_id)?);
        assert!(test_path_exists_in_repo(&tmp, &image1_path)?);
        let link_target = read_links_in_repo(&tmp, &image1_path)?.expect("link is not broken");
        assert!(test_path_exists_in_repo(
            &tmp,
            PathBuf::from("images").join(&link_target)
        )?);

        // Verify GcResult: 1 object removed (obj1), no symlinks pruned
        assert_eq!(result.objects_removed, 1);
        assert!(result.objects_bytes > 0);
        assert_eq!(result.images_pruned, 0);
        assert_eq!(result.streams_pruned, 0);
        Ok(())
    }

    #[test]
    fn test_gc_keeps_one_image_from_refs() -> Result<()> {
        let tmp = tempdir();
        let repo = create_test_repo(&tmp.path().join("repo"))?;

        let obj1_size: u64 = 32 * 1024;
        let obj1 = generate_test_data(obj1_size, 0xAE);
        let obj2_size: u64 = 64 * 1024;
        let obj2 = generate_test_data(obj2_size, 0xEA);

        let obj1_id = repo.ensure_object(&obj1)?;
        let obj2_id = repo.ensure_object(&obj2)?;

        let fs = make_test_fs(&obj2_id, obj2_size);
        let image1 = fs.commit_image(&repo, Some("ref-name"))?;
        let image1_path = format!("images/{}", image1.to_hex());

        repo.sync()?;

        assert!(test_object_exists(&tmp, &obj1_id)?);
        assert!(test_object_exists(&tmp, &obj2_id)?);
        assert!(test_path_exists_in_repo(&tmp, &image1_path)?);
        let link_target = read_links_in_repo(&tmp, &image1_path)?.expect("link is not broken");
        assert!(test_path_exists_in_repo(
            &tmp,
            PathBuf::from("images").join(&link_target)
        )?);

        // Now perform gc - image kept via ref, only obj1 removed
        let result = repo.gc(&[])?;

        assert!(!test_object_exists(&tmp, &obj1_id)?);
        assert!(test_object_exists(&tmp, &obj2_id)?);
        assert!(test_path_exists_in_repo(&tmp, &image1_path)?);
        let link_target = read_links_in_repo(&tmp, &image1_path)?.expect("link is not broken");
        assert!(test_path_exists_in_repo(
            &tmp,
            PathBuf::from("images").join(&link_target)
        )?);

        // Verify GcResult: 1 object removed, no symlinks pruned (image has ref)
        assert_eq!(result.objects_removed, 1);
        assert!(result.objects_bytes > 0);
        assert_eq!(result.images_pruned, 0);
        assert_eq!(result.streams_pruned, 0);
        Ok(())
    }

    fn make_test_fs_with_two_files(
        obj1: &Sha512HashValue,
        size1: u64,
        obj2: &Sha512HashValue,
        size2: u64,
    ) -> FileSystem<Sha512HashValue> {
        let mut fs = make_test_fs(obj1, size1);
        let leaf_id = fs.push_leaf(
            Stat {
                st_mode: 0o644,
                st_uid: 0,
                st_gid: 0,
                st_mtim_sec: 0,
                xattrs: Default::default(),
            },
            LeafContent::Regular(RegularFile::External(obj2.clone(), size2)),
        );
        let inode = Inode::leaf(leaf_id);
        fs.root.insert(OsStr::new("extra_data"), inode);
        fs
    }

    #[test]
    fn test_gc_keeps_one_image_from_two_overlapped() -> Result<()> {
        let tmp = tempdir();
        let repo = create_test_repo(&tmp.path().join("repo"))?;

        let obj1_size: u64 = 32 * 1024;
        let obj1 = generate_test_data(obj1_size, 0xAE);
        let obj2_size: u64 = 64 * 1024;
        let obj2 = generate_test_data(obj2_size, 0xEA);
        let obj3_size: u64 = 64 * 1024;
        let obj3 = generate_test_data(obj2_size, 0xAA);
        let obj4_size: u64 = 64 * 1024;
        let obj4 = generate_test_data(obj2_size, 0xEE);

        let obj1_id = repo.ensure_object(&obj1)?;
        let obj2_id = repo.ensure_object(&obj2)?;
        let obj3_id = repo.ensure_object(&obj3)?;
        let obj4_id = repo.ensure_object(&obj4)?;

        let fs = make_test_fs_with_two_files(&obj2_id, obj2_size, &obj3_id, obj3_size);
        let image1 = fs.commit_image(&repo, None)?;
        let image1_path = format!("images/{}", image1.to_hex());

        let fs = make_test_fs_with_two_files(&obj2_id, obj2_size, &obj4_id, obj4_size);
        let image2 = fs.commit_image(&repo, None)?;
        let image2_path = format!("images/{}", image2.to_hex());

        repo.sync()?;

        assert!(test_object_exists(&tmp, &obj1_id)?);
        assert!(test_object_exists(&tmp, &obj2_id)?);
        assert!(test_object_exists(&tmp, &obj3_id)?);
        assert!(test_object_exists(&tmp, &obj4_id)?);
        assert!(test_path_exists_in_repo(&tmp, &image1_path)?);
        let link_target = read_links_in_repo(&tmp, &image1_path)?.expect("link is not broken");
        assert!(test_path_exists_in_repo(
            &tmp,
            PathBuf::from("images").join(&link_target)
        )?);
        assert!(test_path_exists_in_repo(&tmp, &image2_path)?);
        let link_target = read_links_in_repo(&tmp, &image2_path)?.expect("link is not broken");
        assert!(test_path_exists_in_repo(
            &tmp,
            PathBuf::from("images").join(&link_target)
        )?);

        // Now perform gc - keep image1, remove image2 and its unique objects
        let image1_hex = image1.to_hex();
        let result = repo.gc(&[image1_hex.as_str()])?;

        assert!(!test_object_exists(&tmp, &obj1_id)?);
        assert!(test_object_exists(&tmp, &obj2_id)?);
        assert!(test_object_exists(&tmp, &obj3_id)?);
        assert!(!test_object_exists(&tmp, &obj4_id)?);
        assert!(test_path_exists_in_repo(&tmp, &image1_path)?);
        let link_target = read_links_in_repo(&tmp, &image1_path)?.expect("link is not broken");
        assert!(test_path_exists_in_repo(
            &tmp,
            PathBuf::from("images").join(&link_target)
        )?);
        assert!(!test_path_exists_in_repo(&tmp, &image2_path)?);

        // Verify GcResult: 3 objects removed (obj1, obj4, image2 erofs), 1 image pruned
        assert_eq!(result.objects_removed, 3);
        assert!(result.objects_bytes > 0);
        assert_eq!(result.images_pruned, 1);
        assert_eq!(result.streams_pruned, 0);
        Ok(())
    }

    #[test]
    fn test_ensure_object_from_file() -> Result<()> {
        use std::io::{Seek, SeekFrom, Write};

        let tmp = tempdir();
        let repo = create_test_repo(&tmp.path().join("repo"))?;
        let mut ctx = ImportContext::default();

        let test_data = generate_test_data(64 * 1024, 0xBE);
        let mut temp_file = crate::test::tempfile();
        temp_file.write_all(&test_data)?;
        temp_file.seek(SeekFrom::Start(0))?;

        // First store should return Copied or Reflinked (depending on fs)
        let (object_id, method) =
            repo.ensure_object_from_file(&temp_file, test_data.len() as u64, &mut ctx)?;
        assert_ne!(method, ObjectStoreMethod::AlreadyPresent);
        assert!(test_object_exists(&tmp, &object_id)?);

        // Read back and verify contents match
        let stored_data = repo.read_object(&object_id)?;
        assert_eq!(stored_data, test_data);

        // Second store of same data should return AlreadyPresent
        temp_file.seek(SeekFrom::Start(0))?;
        let (object_id_2, method_2) =
            repo.ensure_object_from_file(&temp_file, test_data.len() as u64, &mut ctx)?;
        assert_eq!(object_id, object_id_2);
        assert_eq!(method_2, ObjectStoreMethod::AlreadyPresent);

        Ok(())
    }

    // ==================== Fsck Tests ====================

    #[tokio::test]
    async fn test_fsck_empty_repo() -> Result<()> {
        let tmp = tempdir();
        let repo = create_test_repo(&tmp.path().join("repo"))?;

        let result = repo.fsck().await?;

        assert!(result.is_ok());
        assert_eq!(result.objects_checked, 0);
        assert_eq!(result.objects_corrupted, 0);
        assert_eq!(result.streams_checked, 0);
        assert_eq!(result.streams_corrupted, 0);
        assert_eq!(result.images_checked, 0);
        assert_eq!(result.images_corrupted, 0);
        assert_eq!(result.broken_links, 0);
        assert_eq!(result.missing_objects, 0);
        assert!(result.errors.is_empty());
        Ok(())
    }

    #[tokio::test]
    async fn test_fsck_healthy_repo_with_objects() -> Result<()> {
        let tmp = tempdir();
        let repo = create_test_repo(&tmp.path().join("repo"))?;

        let obj1 = generate_test_data(32 * 1024, 0xAE);
        let obj2 = generate_test_data(64 * 1024, 0xEA);

        let _obj1_id = repo.ensure_object(&obj1)?;
        let _obj2_id: Sha512HashValue = compute_verity(&obj2);

        let mut writer = repo.create_stream(0)?;
        writer.write_external(&obj2)?;
        let _stream_id = repo.write_stream(writer, "test-stream", None)?;
        repo.sync()?;

        let result = repo.fsck().await?;

        assert!(result.is_ok(), "fsck should pass: {result}");
        // 3 objects: obj1, obj2, and the splitstream object
        assert!(result.objects_checked >= 3);
        assert_eq!(result.objects_corrupted, 0);
        assert_eq!(result.streams_checked, 1);
        assert_eq!(result.streams_corrupted, 0);
        assert_eq!(result.broken_links, 0);
        assert_eq!(result.missing_objects, 0);
        assert!(result.errors.is_empty());
        Ok(())
    }

    #[tokio::test]
    async fn test_fsck_detects_corrupted_object() -> Result<()> {
        let tmp = tempdir();
        let repo = create_test_repo(&tmp.path().join("repo"))?;

        let obj = generate_test_data(32 * 1024, 0xAE);
        let obj_id = repo.ensure_object(&obj)?;
        repo.sync()?;

        // Corrupt the object by replacing the file (objects may be
        // immutable due to fs-verity, so we delete and recreate).
        let hex = obj_id.to_hex();
        let (dir, file) = hex.split_at(2);
        let obj_path = tmp
            .path()
            .join("repo")
            .join(format!("objects/{dir}/{file}"));
        std::fs::remove_file(&obj_path)?;
        std::fs::write(&obj_path, b"corrupted data")?;

        let result = repo.fsck().await?;

        assert!(!result.is_ok(), "fsck should detect corruption");
        assert!(
            result.objects_corrupted > 0,
            "should report corrupted objects"
        );
        assert!(
            result
                .errors
                .iter()
                .any(|e| e.to_string().contains("object-digest-mismatch")),
            "errors should mention digest mismatch: {:?}",
            result.errors
        );
        Ok(())
    }

    #[tokio::test]
    async fn test_fsck_detects_broken_stream_link() -> Result<()> {
        let tmp = tempdir();
        let repo = create_test_repo(&tmp.path().join("repo"))?;

        let obj = generate_test_data(64 * 1024, 0xEA);
        let _obj_verity: Sha512HashValue = compute_verity(&obj);

        let mut writer = repo.create_stream(0)?;
        writer.write_external(&obj)?;
        let _stream_id = repo.write_stream(writer, "test-stream", None)?;
        repo.sync()?;

        // The stream symlink points to a splitstream object. Find and
        // read the symlink target, then delete the backing object.
        let stream_symlink = tmp.path().join("repo/streams/test-stream");
        let link_target = std::fs::read_link(&stream_symlink)?;
        // link_target is relative to streams/, e.g. "../objects/XX/YY..."
        let backing_path = tmp.path().join("repo/streams").join(&link_target);
        std::fs::remove_file(&backing_path)?;

        let result = repo.fsck().await?;

        assert!(!result.is_ok(), "fsck should detect broken link");
        assert!(
            result.broken_links > 0,
            "should report broken links: {result}"
        );
        Ok(())
    }

    #[tokio::test]
    async fn test_fsck_detects_missing_stream_object_ref() -> Result<()> {
        let tmp = tempdir();
        let repo = create_test_repo(&tmp.path().join("repo"))?;

        let obj = generate_test_data(64 * 1024, 0xEA);
        let obj_verity: Sha512HashValue = compute_verity(&obj);

        // Create a stream with an external reference to the object.
        // write_external calls ensure_object internally, so the object
        // will exist.
        let mut writer = repo.create_stream(0)?;
        writer.write_external(&obj)?;
        let _stream_id = repo.write_stream(writer, "test-stream", None)?;
        repo.sync()?;

        // Delete the referenced object (but leave the splitstream intact)
        let hex = obj_verity.to_hex();
        let (dir, file) = hex.split_at(2);
        let obj_path = tmp
            .path()
            .join("repo")
            .join(format!("objects/{dir}/{file}"));
        std::fs::remove_file(&obj_path)?;

        let result = repo.fsck().await?;

        assert!(!result.is_ok(), "fsck should detect missing object ref");
        assert!(
            result.missing_objects > 0,
            "should report missing objects: {result}"
        );
        assert!(
            result
                .errors
                .iter()
                .any(|e| e.to_string().contains("missing-object-ref")),
            "errors should mention missing object: {:?}",
            result.errors
        );
        Ok(())
    }

    // ==================== Additional Fsck Gap Tests ====================

    fn open_test_repo_dir(tmp: &tempfile::TempDir) -> cap_std::fs::Dir {
        cap_std::fs::Dir::open_ambient_dir(tmp.path().join("repo"), cap_std::ambient_authority())
            .unwrap()
    }

    #[tokio::test]
    async fn test_fsck_detects_non_symlink_in_streams() -> Result<()> {
        // Exercises fsck_category non-symlink detection (line ~1695).
        // The code checks entry.file_type() != FileType::Symlink and reports
        // "not a symlink" for regular files or directories in streams/.
        let tmp = tempdir();
        let repo = create_test_repo(&tmp.path().join("repo"))?;
        repo.sync()?;

        // Create a regular file directly in streams/ (not a symlink)
        let dir = open_test_repo_dir(&tmp);
        dir.create_dir_all("streams")?;
        dir.write("streams/bogus-entry", b"not a symlink")?;

        let result = repo.fsck().await?;

        assert!(!result.is_ok(), "fsck should detect non-symlink in streams");
        assert_eq!(result.streams_corrupted, 1);
        assert!(
            result
                .errors
                .iter()
                .any(|e| e.to_string().contains("entry-not-symlink")),
            "errors should mention non-symlink: {:?}",
            result.errors
        );
        Ok(())
    }

    #[tokio::test]
    async fn test_fsck_detects_non_symlink_in_images() -> Result<()> {
        // Exercises fsck_category non-symlink detection for the "images"
        // category (same code path as streams, but counting images_corrupted).
        let tmp = tempdir();
        let repo = create_test_repo(&tmp.path().join("repo"))?;
        repo.sync()?;

        let dir = open_test_repo_dir(&tmp);
        dir.create_dir_all("images")?;
        dir.write("images/bogus-image", b"not a symlink")?;

        let result = repo.fsck().await?;

        assert!(!result.is_ok(), "fsck should detect non-symlink in images");
        assert_eq!(result.images_corrupted, 1);
        assert!(
            result
                .errors
                .iter()
                .any(|e| e.to_string().contains("entry-not-symlink")),
            "errors should mention non-symlink: {:?}",
            result.errors
        );
        Ok(())
    }

    #[tokio::test]
    async fn test_fsck_detects_broken_ref_symlink() -> Result<()> {
        // Exercises fsck_refs_dir broken symlink detection (line ~1804).
        // Creates a ref symlink that points to a non-existent stream entry,
        // so following the chain refs/X -> ../../stream-entry -> object fails.
        let tmp = tempdir();
        let repo = create_test_repo(&tmp.path().join("repo"))?;
        repo.sync()?;

        // Create refs directory under streams
        let dir = open_test_repo_dir(&tmp);
        dir.create_dir_all("streams/refs")?;

        // Create a dangling symlink in refs/
        dir.symlink("../nonexistent-stream", "streams/refs/broken-ref")?;

        let result = repo.fsck().await?;

        assert!(!result.is_ok(), "fsck should detect broken ref symlink");
        assert!(result.broken_links > 0, "should report broken links");
        assert!(
            result
                .errors
                .iter()
                .any(|e| e.to_string().contains("broken-symlink")
                    && e.to_string().contains("refs")),
            "errors should mention broken ref symlink: {:?}",
            result.errors
        );
        Ok(())
    }

    #[tokio::test]
    async fn test_fsck_refs_dir_unexpected_file_type() -> Result<()> {
        // Exercises the "unexpected file type" branch in fsck_refs_dir
        // (line ~1817). Regular files in refs/ are neither symlinks nor
        // directories — they should be flagged.
        let tmp = tempdir();
        let repo = create_test_repo(&tmp.path().join("repo"))?;
        repo.sync()?;

        let dir = open_test_repo_dir(&tmp);
        dir.create_dir_all("streams/refs")?;

        // Put a regular file directly in refs/
        dir.write("streams/refs/stray-file", b"should not be here")?;

        let result = repo.fsck().await?;

        assert!(!result.is_ok(), "fsck should detect unexpected file type");
        assert!(
            result
                .errors
                .iter()
                .any(|e| e.to_string().contains("unexpected-file-type")),
            "errors should mention unexpected file type: {:?}",
            result.errors
        );
        Ok(())
    }

    #[tokio::test]
    async fn test_fsck_refs_dir_recursive() -> Result<()> {
        // Exercises the recursive walk in fsck_refs_dir: creates a nested
        // subdirectory under refs/ with a broken symlink inside to verify
        // the recursion actually descends into subdirs.
        let tmp = tempdir();
        let repo = create_test_repo(&tmp.path().join("repo"))?;
        repo.sync()?;

        let dir = open_test_repo_dir(&tmp);
        dir.create_dir_all("streams/refs/nested/deep")?;

        // Broken symlink in the nested directory
        dir.symlink(
            "../../../nonexistent-stream",
            "streams/refs/nested/deep/broken-nested-ref",
        )?;

        let result = repo.fsck().await?;

        assert!(
            !result.is_ok(),
            "fsck should detect broken symlink in nested refs"
        );
        assert!(result.broken_links > 0);
        assert!(
            result
                .errors
                .iter()
                .any(|e| e.to_string().contains("nested/deep")
                    && e.to_string().contains("broken-symlink")),
            "error should reference the nested path: {:?}",
            result.errors
        );
        Ok(())
    }

    #[tokio::test]
    async fn test_fsck_detects_invalid_object_filename() -> Result<()> {
        // Exercises fsck_object_dir invalid filename detection (line ~1581).
        // Creates a file with a name that can't be parsed as a hex hash
        // remainder in objects/XX/.
        let tmp = tempdir();
        let repo = create_test_repo(&tmp.path().join("repo"))?;
        repo.sync()?;

        let dir = open_test_repo_dir(&tmp);
        dir.create_dir_all("objects/ab")?;
        dir.write("objects/ab/not-a-valid-hex-hash", b"junk")?;

        let result = repo.fsck().await?;

        assert!(
            !result.is_ok(),
            "fsck should detect invalid object filename"
        );
        assert!(result.objects_corrupted > 0);
        assert!(
            result
                .errors
                .iter()
                .any(|e| e.to_string().contains("object-invalid-name")),
            "errors should mention invalid filename: {:?}",
            result.errors
        );
        Ok(())
    }

    #[tokio::test]
    async fn test_fsck_detects_broken_image_symlink() -> Result<()> {
        // Exercises the broken symlink path in fsck_category for images
        // (line ~1711). The stream broken-symlink test covers streams;
        // this covers the same logic for images/.
        let tmp = tempdir();
        let repo = create_test_repo(&tmp.path().join("repo"))?;

        let obj_size: u64 = 32 * 1024;
        let obj = generate_test_data(obj_size, 0xBB);
        let obj_id = repo.ensure_object(&obj)?;

        let fs = make_test_fs(&obj_id, obj_size);
        let image_id = fs.commit_image(&repo, None)?;
        repo.sync()?;

        // Delete the backing object that the image symlink points to
        let dir = open_test_repo_dir(&tmp);
        let image_rel = format!("images/{}", image_id.to_hex());
        let link_target = dir.read_link(&image_rel)?;
        let backing_rel = PathBuf::from("images").join(&link_target);
        dir.remove_file(&backing_rel)?;

        let result = repo.fsck().await?;

        assert!(
            !result.is_ok(),
            "fsck should detect broken image symlink: {result}"
        );
        assert!(result.broken_links > 0);
        assert!(result.images_corrupted > 0);
        Ok(())
    }

    #[tokio::test]
    async fn test_fsck_detects_missing_named_ref_object() -> Result<()> {
        // Exercises fsck_splitstream named ref checking (line ~1869).
        // Creates a stream with a named ref pointing to a non-existent
        // object, which should be detected as a missing object.
        let tmp = tempdir();
        let repo = create_test_repo(&tmp.path().join("repo"))?;

        let obj = generate_test_data(64 * 1024, 0xEA);

        // Create stream1 that references obj
        let mut writer1 = repo.create_stream(0)?;
        writer1.write_external(&obj)?;
        let stream1_id = repo.write_stream(writer1, "test-stream1", None)?;

        // Create stream2 with a named ref to stream1
        let mut writer2 = repo.create_stream(0)?;
        writer2.add_named_stream_ref("test-stream1", &stream1_id);
        let _stream2_id = repo.write_stream(writer2, "test-stream2", None)?;
        repo.sync()?;

        // Delete the object that the named ref points to (the stream1 splitstream object)
        let hex = stream1_id.to_hex();
        let (prefix, rest) = hex.split_at(2);
        let repo_dir = open_test_repo_dir(&tmp);
        repo_dir.remove_file(format!("objects/{prefix}/{rest}"))?;

        let result = repo.fsck().await?;

        assert!(
            !result.is_ok(),
            "fsck should detect missing named ref object"
        );
        assert!(
            result.missing_objects > 0,
            "should report missing objects: {result}"
        );
        assert!(
            result
                .errors
                .iter()
                .any(|e| e.to_string().contains("missing-named-ref")),
            "errors should mention missing named ref object: {:?}",
            result.errors
        );
        Ok(())
    }

    #[tokio::test]
    async fn test_fsck_healthy_repo_with_refs() -> Result<()> {
        // Verifies fsck_refs_dir passes on valid refs. Prior tests only
        // checked that fsck detects broken refs; this confirms a repo
        // with valid refs reports ok.
        let tmp = tempdir();
        let repo = create_test_repo(&tmp.path().join("repo"))?;

        let obj = generate_test_data(64 * 1024, 0xEA);

        let mut writer = repo.create_stream(0)?;
        writer.write_external(&obj)?;
        // write_stream with reference creates a ref symlink
        let _stream_id = repo.write_stream(writer, "test-stream", Some("my-ref"))?;
        repo.sync()?;

        let result = repo.fsck().await?;

        assert!(result.is_ok(), "fsck should pass with valid refs: {result}");
        assert!(result.errors.is_empty());
        Ok(())
    }

    #[tokio::test]
    async fn test_fsck_detects_corrupted_splitstream_object() -> Result<()> {
        // Exercises fsck_splitstream failure-to-open path (line ~1829).
        // Corrupts the splitstream object so that open_stream fails to
        // parse it, which is different from a missing external object ref.
        let tmp = tempdir();
        let repo = create_test_repo(&tmp.path().join("repo"))?;

        let obj = generate_test_data(64 * 1024, 0xEA);

        let mut writer = repo.create_stream(0)?;
        writer.write_external(&obj)?;
        let _stream_id = repo.write_stream(writer, "test-stream", None)?;
        repo.sync()?;

        // Find the splitstream object path via the stream symlink
        let dir = open_test_repo_dir(&tmp);
        let link_target = dir.read_link("streams/test-stream")?;
        let backing_rel = PathBuf::from("streams").join(&link_target);

        // Corrupt the splitstream object (not the data object it references)
        dir.remove_file(&backing_rel)?;
        dir.write(&backing_rel, b"corrupted splitstream header")?;

        let result = repo.fsck().await?;

        assert!(
            !result.is_ok(),
            "fsck should detect corrupted splitstream: {result}"
        );
        // The object digest mismatch is detected by object checking,
        // and the stream is also flagged because open_stream will fail
        // or the object refs check will fail.
        assert!(
            result.objects_corrupted > 0 || result.streams_corrupted > 0,
            "should report corruption: {result}"
        );
        Ok(())
    }

    #[tokio::test]
    async fn test_fsck_validates_erofs_image_objects() -> Result<()> {
        // Exercises fsck_image: creates a valid erofs image, then deletes
        // one of its referenced objects. Fsck should detect the missing
        // object via erofs parsing.
        let tmp = tempdir();
        let repo = create_test_repo(&tmp.path().join("repo"))?;

        let obj_size: u64 = 32 * 1024;
        let obj = generate_test_data(obj_size, 0xCC);
        let obj_id = repo.ensure_object(&obj)?;

        let fs = make_test_fs(&obj_id, obj_size);
        let image_id = fs.commit_image(&repo, None)?;
        repo.sync()?;

        // Sanity: fsck passes on a healthy image
        let result = repo.fsck().await?;
        assert!(result.is_ok(), "healthy image should pass fsck: {result}");
        assert!(result.images_checked > 0, "should have checked the image");

        // Delete the object referenced by the erofs image
        let hex = obj_id.to_hex();
        let (prefix, rest) = hex.split_at(2);
        let dir = open_test_repo_dir(&tmp);
        dir.remove_file(format!("objects/{prefix}/{rest}"))?;

        let result = repo.fsck().await?;
        assert!(
            !result.is_ok(),
            "fsck should detect missing object referenced by erofs image: {result}"
        );
        assert!(
            result.missing_objects > 0,
            "should report missing objects: {result}"
        );
        assert!(
            result
                .errors
                .iter()
                .any(|e| e.to_string().contains(&image_id.to_hex())
                    && e.to_string().contains("image-missing-object")),
            "error should reference the image: {:?}",
            result.errors
        );
        Ok(())
    }

    #[tokio::test]
    async fn test_fsck_detects_corrupt_erofs_image() -> Result<()> {
        // Exercises fsck_image: corrupts the erofs image data so that
        // parsing fails. The catch_unwind should catch the panic from
        // the current erofs reader.
        let tmp = tempdir();
        let repo = create_test_repo(&tmp.path().join("repo"))?;

        let obj_size: u64 = 32 * 1024;
        let obj = generate_test_data(obj_size, 0xDD);
        let obj_id = repo.ensure_object(&obj)?;

        let fs = make_test_fs(&obj_id, obj_size);
        let image_id = fs.commit_image(&repo, None)?;
        repo.sync()?;

        // Corrupt the erofs image data (replace the backing object)
        let hex = image_id.to_hex();
        let (prefix, rest) = hex.split_at(2);
        let dir = open_test_repo_dir(&tmp);
        let obj_path = format!("objects/{prefix}/{rest}");
        dir.remove_file(&obj_path)?;
        dir.write(&obj_path, b"this is not a valid erofs image")?;

        let result = repo.fsck().await?;
        assert!(
            !result.is_ok(),
            "fsck should detect corrupt erofs image: {result}"
        );
        assert!(
            result
                .errors
                .iter()
                .any(|e| e.to_string().contains("image-invalid")
                    || e.to_string().contains("digest mismatch")),
            "error should mention erofs corruption or digest mismatch: {:?}",
            result.errors
        );
        Ok(())
    }

    // ---- Fsck metadata validation tests ----

    #[tokio::test]
    async fn test_fsck_valid_metadata() -> Result<()> {
        let tmp = tempdir();
        let repo = create_test_repo(&tmp.path().join("repo"))?;

        let result = repo.fsck().await?;
        assert!(result.is_ok());
        assert!(result.has_metadata());
        assert!(result.errors().is_empty());
        assert!(
            result.to_string().contains("meta.json: ok"),
            "display should show ok: {result}"
        );
        Ok(())
    }

    #[tokio::test]
    async fn test_fsck_corrupt_metadata() -> Result<()> {
        // Write garbage to meta.json after opening — fsck re-reads from disk.
        let tmp = tempdir();
        let repo = create_test_repo(&tmp.path().join("repo"))?;

        let dir = open_test_repo_dir(&tmp);
        // Remove the valid meta.json and replace with garbage
        dir.remove_file(REPO_METADATA_FILENAME)?;
        dir.write(REPO_METADATA_FILENAME, b"not valid json {{")?;

        let result = repo.fsck().await?;
        assert!(!result.is_ok());
        assert!(
            result
                .errors()
                .iter()
                .any(|e| matches!(e, FsckError::MetadataParseFailed { .. }))
        );
        assert!(
            result.to_string().contains("meta.json: error"),
            "display should show error: {result}"
        );
        Ok(())
    }

    #[test]
    fn test_open_path_requires_metadata() {
        // Opening a directory without meta.json should fail with MetadataMissing.
        let tmp = tempdir();
        let path = tmp.path().join("bare-repo");
        mkdirat(CWD, &path, Mode::from_raw_mode(0o755)).unwrap();
        assert!(matches!(
            Repository::<Sha512HashValue>::open_path(CWD, &path),
            Err(RepositoryOpenError::MetadataMissing)
        ));
    }

    #[test]
    fn test_open_path_detects_old_format() {
        // A directory with objects/ but no meta.json → OldFormatRepository.
        let tmp = tempdir();
        let path = tmp.path().join("old-repo");
        mkdirat(CWD, &path, Mode::from_raw_mode(0o755)).unwrap();
        mkdirat(CWD, &path.join("objects"), Mode::from_raw_mode(0o755)).unwrap();
        assert!(matches!(
            Repository::<Sha512HashValue>::open_path(CWD, &path),
            Err(RepositoryOpenError::OldFormatRepository)
        ));
    }

    #[test]
    fn test_open_path_algorithm_mismatch() {
        // Open a sha512 repo as sha256 → AlgorithmMismatch.
        let tmp = tempdir();
        let path = tmp.path().join("sha512-repo");
        Repository::<Sha512HashValue>::init_path(CWD, &path, Algorithm::SHA512, false).unwrap();
        assert!(matches!(
            Repository::<Sha256HashValue>::open_path(CWD, &path),
            Err(RepositoryOpenError::AlgorithmMismatch { .. })
        ));
    }

    // ---- RepoMetadata / FeatureFlags tests ----
    //
    // Basic metadata construction, JSON roundtrip, algorithm compatibility,
    // and read/write are covered by the fsck tests above and the CLI
    // integration tests (init, hash-mismatch, backcompat).  The tests
    // below focus on the three-tier feature-flag compatibility model and
    // JSON serialization of populated feature vectors, which aren't
    // exercised elsewhere.

    #[test]
    fn test_metadata_json_with_features() {
        let mut meta = RepoMetadata::for_hash::<Sha512HashValue>();
        meta.features.compatible.push("some-compat".to_string());
        meta.features
            .read_only_compatible
            .push("some-rocompat".to_string());

        let json = meta.to_json().unwrap();
        let parsed: serde_json::Value = serde_json::from_slice(&json).unwrap();

        assert_eq!(parsed["features"]["compatible"][0], "some-compat");
        assert_eq!(
            parsed["features"]["read-only-compatible"][0],
            "some-rocompat"
        );

        // Roundtrip
        let meta2 = RepoMetadata::from_json(&json).unwrap();
        assert_eq!(meta, meta2);
    }

    #[test]
    fn test_feature_flags_unknown_incompat() {
        let mut meta = RepoMetadata::for_hash::<Sha512HashValue>();
        meta.features
            .incompatible
            .push("fancy-new-thing".to_string());
        let err = meta.check_compatible::<Sha512HashValue>().unwrap_err();
        assert!(
            format!("{err}").contains("fancy-new-thing"),
            "error should name the unknown feature: {err}"
        );
    }

    #[test]
    fn test_feature_flags_unknown_ro_compat() {
        let mut meta = RepoMetadata::for_hash::<Sha512HashValue>();
        meta.features
            .read_only_compatible
            .push("new-index".to_string());
        let check = meta.check_compatible::<Sha512HashValue>().unwrap();
        assert_eq!(check, FeatureCheck::ReadOnly(vec!["new-index".to_string()]));
    }

    #[test]
    fn test_feature_flags_unknown_compat_ignored() {
        let mut meta = RepoMetadata::for_hash::<Sha512HashValue>();
        meta.features.compatible.push("optional-hint".to_string());
        assert_eq!(
            meta.check_compatible::<Sha512HashValue>().unwrap(),
            FeatureCheck::ReadWrite
        );
    }

    #[test]
    fn test_object_store_method_variants() {
        // Verify all variants exist and are distinct
        let methods = [
            ObjectStoreMethod::Reflinked,
            ObjectStoreMethod::Hardlinked,
            ObjectStoreMethod::Copied,
            ObjectStoreMethod::AlreadyPresent,
        ];

        for (i, a) in methods.iter().enumerate() {
            for (j, b) in methods.iter().enumerate() {
                if i == j {
                    assert_eq!(a, b);
                } else {
                    assert_ne!(a, b);
                }
            }
        }

        // Verify Debug impl works
        assert_eq!(format!("{:?}", ObjectStoreMethod::Hardlinked), "Hardlinked");
    }

    // ---- open_upgrade tests ----

    #[test]
    fn test_open_upgrade_sha256() {
        let tmp = tempdir();
        let repo_path = tmp.path().join("repo");

        // Create a repo, store an object, then remove meta.json to
        // simulate an old-format repository.
        let (repo, _) =
            Repository::<Sha256HashValue>::init_path(CWD, &repo_path, Algorithm::SHA256, false)
                .unwrap();
        let data = b"hello world";
        let obj_id = repo.ensure_object(data).unwrap();
        drop(repo);

        std::fs::remove_file(repo_path.join(REPO_METADATA_FILENAME)).unwrap();

        // open_path should fail with OldFormatRepository
        assert!(matches!(
            Repository::<Sha256HashValue>::open_path(CWD, &repo_path),
            Err(RepositoryOpenError::OldFormatRepository)
        ));

        // open_upgrade should infer metadata and succeed
        let (repo, upgraded) =
            Repository::<Sha256HashValue>::open_upgrade(CWD, &repo_path).unwrap();
        assert!(upgraded);
        assert!(repo_path.join(REPO_METADATA_FILENAME).exists());

        // Verify the algorithm was inferred correctly
        let meta = read_repo_metadata(
            &openat(
                CWD,
                &repo_path,
                OFlags::RDONLY | OFlags::CLOEXEC,
                Mode::empty(),
            )
            .unwrap(),
        )
        .unwrap()
        .unwrap();
        assert!(meta.algorithm.is_compatible::<Sha256HashValue>());

        // The repo should work — read back the object
        let read_data = repo.read_object(&obj_id).unwrap();
        assert_eq!(&read_data[..], data);

        // Second call should not upgrade
        drop(repo);
        let (_repo, upgraded) =
            Repository::<Sha256HashValue>::open_upgrade(CWD, &repo_path).unwrap();
        assert!(!upgraded);
    }

    #[test]
    fn test_open_upgrade_sha512() {
        let tmp = tempdir();
        let repo_path = tmp.path().join("repo");

        let (repo, _) =
            Repository::<Sha512HashValue>::init_path(CWD, &repo_path, Algorithm::SHA512, false)
                .unwrap();
        let data = b"sha512 test data";
        let obj_id = repo.ensure_object(data).unwrap();
        drop(repo);

        std::fs::remove_file(repo_path.join(REPO_METADATA_FILENAME)).unwrap();

        let (repo, upgraded) =
            Repository::<Sha512HashValue>::open_upgrade(CWD, &repo_path).unwrap();
        assert!(upgraded);

        let meta = read_repo_metadata(
            &openat(
                CWD,
                &repo_path,
                OFlags::RDONLY | OFlags::CLOEXEC,
                Mode::empty(),
            )
            .unwrap(),
        )
        .unwrap()
        .unwrap();
        assert!(meta.algorithm.is_compatible::<Sha512HashValue>());

        let read_data = repo.read_object(&obj_id).unwrap();
        assert_eq!(&read_data[..], data);
    }

    #[test]
    fn test_open_upgrade_algorithm_mismatch() {
        // Create a sha512 repo, remove meta.json, then try to
        // open_upgrade as sha256 — should fail with algorithm mismatch.
        let tmp = tempdir();
        let repo_path = tmp.path().join("repo");

        let (repo, _) =
            Repository::<Sha512HashValue>::init_path(CWD, &repo_path, Algorithm::SHA512, false)
                .unwrap();
        repo.ensure_object(b"some data").unwrap();
        drop(repo);

        std::fs::remove_file(repo_path.join(REPO_METADATA_FILENAME)).unwrap();

        let err = Repository::<Sha256HashValue>::open_upgrade(CWD, &repo_path).unwrap_err();
        let msg = format!("{err:#}");
        assert!(
            msg.contains("not compatible"),
            "expected algorithm mismatch error, got: {msg}"
        );
    }

    #[test]
    fn test_open_upgrade_empty_objects() {
        // An old-format repo with an empty objects/ directory should
        // fail because we can't infer the algorithm.
        let tmp = tempdir();
        let repo_path = tmp.path().join("repo");
        mkdirat(CWD, &repo_path, Mode::from_raw_mode(0o755)).unwrap();
        mkdirat(CWD, &repo_path.join("objects"), Mode::from_raw_mode(0o755)).unwrap();

        let err = Repository::<Sha256HashValue>::open_upgrade(CWD, &repo_path).unwrap_err();
        let msg = format!("{err:#}");
        assert!(
            msg.contains("no objects found"),
            "expected 'no objects found' error, got: {msg}"
        );
    }

    #[test]
    fn test_open_upgrade_already_initialized() {
        // open_upgrade on a repo that already has meta.json should
        // return upgraded=false.
        let tmp = tempdir();
        let repo_path = tmp.path().join("repo");

        Repository::<Sha256HashValue>::init_path(CWD, &repo_path, Algorithm::SHA256, false)
            .unwrap();

        let (_repo, upgraded) =
            Repository::<Sha256HashValue>::open_upgrade(CWD, &repo_path).unwrap();
        assert!(!upgraded);
    }
}