ostrya 0.8.0

Pure-Rust async reimplementation of the ostree repository library
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
//! Static-delta generation: the writer of the superblock and the part files,
//! and the `delta-indexes/` cache.
//!
//! The rules that a caller sees are on `Repo::generate_static_delta`, under
//! `# Object routes`, `# Memory`, and `# Blocking pool`, and on the fields of
//! `DeltaOptions`. The read side is in [`crate::delta`]. The tests check each
//! side against the other and against the `ostree` command.
//!
//! The private items that carry the rules:
//!
//! - [`BSDIFF_CONTENT_LIMIT`] and [`patch_beats_splicing`] decide if a bspatch
//!   stream replaces a splice.
//! - [`Spill`] holds the data source of a part, on the heap up to
//!   [`MMAP_THRESHOLD`] and in a temp file after it.
//! - [`compress_part`] and [`bsdiff_stream`] run the CPU-bound stages on the
//!   blocking pool. [`PART_XZ_LEVEL`] sets the memory of the xz encoder.
//! - [`Repo::walk_delta_indexes`] reads the `delta-indexes/` cache back from a
//!   set of commits, through the source commit of each delta that it finds.
//!   [`advertised_delta_map`] encodes the deltas that it finds as the
//!   `ostree.static-deltas` value of a summary.

use std::collections::{BTreeMap, BTreeSet, HashMap, HashSet, VecDeque};
use std::io::SeekFrom;
use std::os::fd::{AsFd, BorrowedFd, OwnedFd};
use std::path::{Path, PathBuf};
use std::pin::Pin;
use std::sync::Arc;
use std::task::{Context, Poll};

use async_compression::Level;
use async_compression::futures::write::XzEncoder;
use futures_lite::AsyncWriteExt;
use ostrya_core::{
    Checksum, ObjectType, Type, Value, Xattrs, choose_offset_size, from_bytes, to_bytes, varint,
    write_offset,
};
use ostrya_rt::{File as RtFile, FileReader};
use sha2::{Digest, Sha256};

use crate::commit::append_dict_entry;
use crate::delta::{
    Blob, COMMIT_SIG, COMPRESSION_XZ, DeltaEndianness, ENDIANNESS_BIG, ENDIANNESS_KEY,
    ENDIANNESS_LITTLE, IO_CHUNK, MAX_PART_IMAGE, MAX_SUPERBLOCK, MAX_TABLE_BYTES, MMAP_THRESHOLD,
    OP_BSPATCH, OP_CLOSE, OP_OPEN, OP_OPEN_SPLICE_CLOSE, OP_SET_READ_SOURCE, OP_UNSET_READ_SOURCE,
    OP_WRITE, SIGNED_MAGIC, SIGNED_SIG, SUPERBLOCK_SIG, dir_child_names, image_key, open_rw_temp,
    read_capped, spill_to_blob,
};
use crate::error::{Error, Result};
use crate::file::{FileKind, FileObject};
use crate::hashing::HashingWriter;
use crate::object::MAX_METADATA_SIZE;
use crate::repo::Repo;
use crate::rollsum::{self, Run};
use crate::sign::{Signer, append_signature};
use crate::summary::{AdvertisedDelta, check_delta_map_type, parse_delta_map};

/// The format version of a delta part that the meta-entry records.
const PART_VERSION: u32 = 0;

/// The xz preset of each part.
///
/// The value is fixed, so the part bytes do not depend on the default of the
/// compression crate. Preset 8 is not extreme, and it uses the CRC64 check that
/// xz uses by default. Its LZMA2 dictionary is 32 MiB. `xz -8 -T1 -vv` reports
/// 370 MiB of encoder memory for each concurrent part and 33 MiB to decode. The
/// parts of the `ostree` command carry the same dictionary size.
const PART_XZ_LEVEL: i32 = 8;

/// The largest content size for which a bspatch stream is tried. The smaller
/// of this value and [`DeltaOptions::max_bsdiff_size`] applies.
///
/// bsdiff runs only after chunking found no shared chunk. For an object of many
/// chunks, this result means that no chunk-sized window of the target occurs
/// in the source. That is evidence that the two objects are unrelated. A patch
/// against unrelated content is about as long as the target, so it loses to a
/// splice after the cost of a suffix sort.
///
/// An object smaller than [`rollsum::MAX_CHUNK`] is one chunk or a small
/// number of chunks. One edit anywhere in it defeats chunking, so a failure of chunking
/// is no such evidence. The `ostree` command also emits bspatch at this size.
/// It was observed to emit bspatch for a 1,024-byte object with a small edit.
///
/// The limit applies to the source and to the target, because the suffix sort
/// is over the source. Pairing is by path, with no rule on the size ratio, so a
/// small object can pair with a large object that it replaced.
const BSDIFF_CONTENT_LIMIT: u64 = rollsum::MAX_CHUNK as u64;

/// The name of the superblock file in a delta directory.
pub(crate) const SUPERBLOCK_FILE: &str = "superblock";
/// The tree of delta directories, relative to the repository root.
const DELTAS_DIR: &str = "deltas";
/// The tree of delta index files, relative to the repository root.
const DELTA_INDEXES_DIR: &str = "delta-indexes";
/// The suffix of an index file under `delta-indexes/`.
const INDEX_SUFFIX: &str = ".index";
/// The `a{sv}` key under which an index file and the summary store the delta
/// map.
pub(crate) const STATIC_DELTAS_KEY: &str = "ostree.static-deltas";

/// The mode of the delta files and the index files. The `ostree` command gives
/// them this mode whatever the umask is.
const DELTA_FILE_MODE: u32 = 0o644;
/// The mode of a new delta directory. The umask reduces it, as it does for the
/// `ostree` command.
const DELTA_DIR_MODE: u32 = 0o755;

/// The options of [`Repo::generate_static_delta`].
///
/// The three size thresholds are the thresholds of the `ostree static-delta
/// generate` command, in bytes. The `ostree` command takes decimal megabytes:
/// pass `4 * 1_000_000` where the command takes `--min-fallback-size=4`.
#[derive(Clone)]
pub struct DeltaOptions {
    /// The stream size at which an object travels as a loose fallback.
    ///
    /// The stream is the file header and the content, uncompressed. If the
    /// stream of an object is this size or larger, the object is not packed
    /// into a part and is never diffed. The size compared is the serialized
    /// file header, plus 7 bytes, plus the content, as the `ostree` command
    /// compares it.
    ///
    /// Default 4,000,000. Zero turns fallbacks off for content objects, as
    /// `--min-fallback-size=0` of the `ostree` command does. Each content
    /// object is then packed whatever its size, and the memory of a diff has
    /// no bound from this value.
    ///
    /// The composefs image of the target commit is a fallback if its size is
    /// this value or larger, with a value other than zero. It is also a
    /// fallback if it is larger than
    /// [`max_chunk_size`](DeltaOptions::max_chunk_size) or larger than
    /// 128 MiB, also with zero, and in each delta with
    /// [`inline`](DeltaOptions::inline) set.
    pub min_fallback_size: u64,
    /// The largest content size for which a bspatch stream is tried.
    ///
    /// The suffix sort of bsdiff costs several times the source size in
    /// memory, on top of the two objects, so this value bounds the peak.
    /// Default 64,000,000. A second bound of 64 KiB comes from the largest
    /// chunk of the chunker, and the smaller bound applies. At the default,
    /// the 64 KiB bound decides. Both bounds apply to the source object and to
    /// the target object.
    pub max_bsdiff_size: u64,
    /// The payload size at which a part closes and the next part starts.
    ///
    /// A composefs image larger than this size is a fallback. Default
    /// 32,000,000. Zero is refused with [`Error::InvalidFormat`].
    pub max_chunk_size: u64,
    /// The switch that allows bspatch streams.
    ///
    /// If `false`, an object that chunking cannot express is spliced whole.
    /// Default `true`.
    pub bsdiff: bool,
    /// The superblock timestamp, in seconds since the Unix epoch.
    ///
    /// `None` uses the current time, as the `ostree` command does. A fixed
    /// value makes the output reproducible for one liblzma version.
    ///
    /// The generation packs the metadata objects first, by object type and
    /// then by checksum, and then the content objects by checksum. So one
    /// input gives the same parts. Each part is the output of liblzma at xz
    /// preset 8, and another liblzma version can write other bytes.
    pub timestamp: Option<u64>,
    /// A directory that receives the superblock and the part files.
    ///
    /// If set, no file goes into the `deltas/` tree. The generation creates
    /// the directory if it is absent.
    ///
    /// The generation replaces the files that this delta writes and removes
    /// no other file. The extra part files of a longer previous delta stay. A
    /// reader takes only the parts that the superblock lists, so these files
    /// cost disk space and cause no error.
    pub output_dir: Option<PathBuf>,
    /// A file path for the superblock, with the part files in its directory.
    ///
    /// If set, no file goes into the `deltas/` tree. A path with no `/` puts
    /// the superblock and the parts in the working directory. The directory
    /// must exist, and the generation does not create it. `None` by default.
    ///
    /// The generation refuses these paths before it writes a file:
    ///
    /// - a path that names a directory
    /// - a path whose last component is empty, `.`, or `..`
    /// - a path whose last component is a part file name, such as `0` or `1`
    ///
    /// Only the rename that puts the superblock in place replaces a file at
    /// the path. If the generation fails, that file stays as it was. If
    /// [`output_dir`](DeltaOptions::output_dir) is also set, the generation is
    /// refused.
    pub superblock_file: Option<PathBuf>,
    /// The engines that sign the superblock before it is written.
    ///
    /// Each engine signs once, in order. Empty by default, which writes an
    /// unsigned superblock. If a signer fails, the generation fails and writes
    /// no superblock.
    pub signers: Vec<Arc<dyn Signer>>,
    /// The switch that puts the parts into the superblock.
    ///
    /// If `true`, the metadata dict of the superblock holds each part under
    /// the key `deltas/<fanout>/<rest>/<i>`, as a `(yay)` variant, and no part
    /// file is written. The value holds the bytes that a part file holds. The
    /// key is the repository-relative name, also with
    /// [`output_dir`](DeltaOptions::output_dir) or
    /// [`superblock_file`](DeltaOptions::superblock_file).
    ///
    /// The parts count toward the superblock limit of 128 MiB. If the
    /// superblock passes the limit, the generation fails before it writes
    /// the superblock. A superblock that an earlier delta left at the same
    /// location then stays in place. Default `false`.
    ///
    /// An inline delta carries no composefs image in a part. The image of the
    /// target commit is then always a fallback.
    pub inline: bool,
    /// The byte order of the size fields of the superblock.
    ///
    /// The superblock records the order in `ostree.endianness`. The order
    /// applies to the `size` and the `usize` of a meta-entry and to the two
    /// sizes of a fallback. The timestamp, the parts, and the embedded commit
    /// do not change with it. Default [`DeltaEndianness::Little`], on each
    /// host.
    pub endianness: DeltaEndianness,
}

impl std::fmt::Debug for DeltaOptions {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        let signers: Vec<&str> = self.signers.iter().map(|signer| signer.name()).collect();
        f.debug_struct("DeltaOptions")
            .field("min_fallback_size", &self.min_fallback_size)
            .field("max_bsdiff_size", &self.max_bsdiff_size)
            .field("max_chunk_size", &self.max_chunk_size)
            .field("bsdiff", &self.bsdiff)
            .field("timestamp", &self.timestamp)
            .field("output_dir", &self.output_dir)
            .field("superblock_file", &self.superblock_file)
            .field("signers", &signers)
            .field("inline", &self.inline)
            .field("endianness", &self.endianness)
            .finish()
    }
}

impl Default for DeltaOptions {
    fn default() -> Self {
        DeltaOptions {
            min_fallback_size: 4_000_000,
            max_bsdiff_size: 64_000_000,
            max_chunk_size: 32_000_000,
            bsdiff: true,
            timestamp: None,
            output_dir: None,
            superblock_file: None,
            signers: Vec::new(),
            inline: false,
            endianness: DeltaEndianness::Little,
        }
    }
}

/// Methods that generate, sign, and index static deltas.
impl Repo {
    /// Generates the static delta from `from` to `to` and returns its directory.
    ///
    /// If `from` is `None`, the delta is a delta from scratch. Both commits and
    /// each object that the delta packs must be present. Set
    /// [`DeltaOptions::signers`] to sign the superblock.
    /// [`sign_static_delta`](Repo::sign_static_delta) adds a signature to a
    /// delta that exists. Call [`reindex_static_deltas`](Repo::reindex_static_deltas)
    /// to publish the new delta in the index cache.
    ///
    /// # Location
    ///
    /// - By default, the files go under `deltas/`, in the base64-fanout
    ///   directory of [`static_delta_relative_dir`]. The `ostree` command uses
    ///   the same directory.
    /// - If [`DeltaOptions::output_dir`] is set, the files go in that
    ///   directory.
    /// - If [`DeltaOptions::superblock_file`] is set, the superblock goes at
    ///   that path, and the parts go in the directory that holds it.
    /// - If [`DeltaOptions::inline`] is set, the parts go into the superblock,
    ///   and no part file is written.
    ///
    /// For the default location, the returned path is relative to the
    /// repository root. The caller supplies the root, because a handle holds
    /// descriptors and no path. `root.join(returned)` is the directory that
    /// [`apply_static_delta_offline`](Repo::apply_static_delta_offline) and
    /// [`sign_static_delta`](Repo::sign_static_delta) take. If
    /// [`DeltaOptions::output_dir`] or [`DeltaOptions::superblock_file`] is
    /// set, the returned path is that option as given. A relative path
    /// resolves against the working directory of the process.
    ///
    /// # Write order
    ///
    /// The part files are written before the superblock, so an interrupted
    /// generation leaves no superblock for a reader to trust. The superblock
    /// is signed before it is written, so a failure of a signer writes no
    /// superblock.
    ///
    /// Each file is written under a temp name. Only the rename that puts the
    /// file in place keeps the temp file. If a generation fails or is
    /// cancelled, the temp file is unlinked, so no partial file stays in the
    /// directory.
    ///
    /// The new parts overwrite the parts of a delta at the same location. For
    /// this reason, the generation unlinks the old superblock before it writes
    /// the first part. The old superblock then cannot describe files that this
    /// run replaced. A file at a [`DeltaOptions::superblock_file`] path
    /// belongs to the caller, and the generation does not unlink it.
    ///
    /// An inline generation replaces no part file. The previous superblock
    /// stays in place until the rename of the new superblock replaces it. If an
    /// inline generation fails, the previous delta stays as it was.
    ///
    /// # Cleanup
    ///
    /// After the new superblock is in place, the generation removes these
    /// files from the delta directory:
    ///
    /// - the part files of a longer previous delta, or each numbered part file
    ///   if the new delta carries its parts inline
    /// - the temp files of a generation that was killed mid-write, when they
    ///   are one hour old or older
    ///
    /// This pass covers the `deltas/` tree of the repository only. A directory
    /// named through [`DeltaOptions::output_dir`] or
    /// [`DeltaOptions::superblock_file`] belongs to the caller. The generation
    /// removes no other file in it.
    ///
    /// # Concurrency
    ///
    /// Two generations of the same delta at the same time, into one directory,
    /// are not supported. Both runs write the same file names, so each run
    /// overwrites the parts and the superblock of the other. Generations of
    /// different deltas can run concurrently, because each delta has its own
    /// directory.
    ///
    /// # Object routes
    ///
    /// The delta carries the objects that `to` reaches and `from` does not
    /// reach. The superblock holds the commit object of `to`. Each other
    /// object reaches the receiver by one of four routes, chosen for each
    /// object:
    ///
    /// - Loose fallback: if the stream of the object is
    ///   [`DeltaOptions::min_fallback_size`] or larger, the delta names the
    ///   object, and the receiver fetches it whole.
    /// - Rollsum delta: the source object is the object at the same path in
    ///   `from`. If content-defined chunking finds chunks that the two objects
    ///   share, the operation stream copies the unchanged runs from the source
    ///   object. The part holds only the changed runs.
    /// - bspatch stream: if chunking finds no shared chunk, the generation
    ///   tries a bspatch stream against the same source object. Both objects
    ///   must be at most 64 KiB and at most [`DeltaOptions::max_bsdiff_size`].
    ///   The patch stays only if its count of nonzero bytes is less than half
    ///   the content size.
    /// - Splice: in all other cases, the part payload holds the object bytes as
    ///   they are. Metadata objects and symlinks always take this route.
    ///
    /// An object smaller than 64 KiB is one chunk or a small number of chunks,
    /// so one edit defeats chunking. In a patch against unrelated high-entropy
    /// content, about 255 bytes in 256 are nonzero. A patch for a small edit
    /// has a few dozen nonzero bytes. If the source object is not present, the
    /// object is spliced.
    ///
    /// # Composefs image
    ///
    /// If `to` carries a valid `ostree.composefs.digest.v0` and the
    /// repository stores the composefs image of `to`, the delta carries the
    /// image. The image is a fallback if its size is
    /// [`DeltaOptions::min_fallback_size`] or larger, with a value other than
    /// zero, or if it is larger than [`DeltaOptions::max_chunk_size`] or
    /// 128 MiB. It is also a fallback if [`DeltaOptions::inline`] is set. Else
    /// it is spliced whole as the last object of the parts, with no diff
    /// against the image of `from`. The generation does not read the
    /// image to check its digest. A commit with no key or with no stored
    /// image gives a delta with no image entry.
    ///
    /// # Memory
    ///
    /// The generation holds a bounded amount of memory:
    ///
    /// - The data source of a part collects in a buffer on the heap. Past
    ///   128 KiB, the buffer moves to an anonymous temp file in `tmp/`.
    /// - Spliced content streams into this buffer in 128 KiB pieces, and the
    ///   generation never holds it whole.
    /// - The part payload streams straight into the xz encoder. Its GVariant
    ///   framing goes around the two large byte arrays, which stream from the
    ///   buffer.
    /// - The xz encoder uses most of the memory: about 370 MiB for each part
    ///   that it compresses.
    ///
    /// A diff needs random access to both objects, so a diff does not stream.
    /// The source object and the target object load on the heap, as in the
    /// read path. If they are large, they load in a read-only mapping of a
    /// temp file. Chunking scans both objects from end to end, so each page of
    /// both is resident while the generation plans the pair. The peak resident
    /// set size follows the sum of the two object sizes, in mapped pages of
    /// temp files.
    ///
    /// [`DeltaOptions::min_fallback_size`] bounds the target object of a diff.
    /// The source object has no bound of its own.
    /// [`DeltaOptions::max_bsdiff_size`] bounds the patch attempt alone.
    ///
    /// # Blocking pool
    ///
    /// The CPU-bound stages run on the blocking pool, off the executor
    /// threads:
    ///
    /// - the chunking and the hashing of both objects of a diff candidate
    /// - the suffix sort of bsdiff
    /// - the compression of each part
    ///
    /// # Errors
    ///
    /// - [`Error::InvalidFormat`] if [`DeltaOptions::max_chunk_size`] is `0`.
    /// - [`Error::InvalidFormat`] if [`DeltaOptions::output_dir`] and
    ///   [`DeltaOptions::superblock_file`] are both set.
    /// - [`Error::ObjectNotFound`] if the commit `to` or the commit `from` is
    ///   not present, or if an object that the delta carries is not present.
    /// - [`Error::Io`] with `EISDIR` if the [`DeltaOptions::superblock_file`]
    ///   path names a directory, or if its last component is empty, `.`, or
    ///   `..`.
    /// - [`Error::InvalidFormat`] if the last component of the
    ///   [`DeltaOptions::superblock_file`] path is not UTF-8 or is a part file
    ///   name.
    /// - [`Error::InvalidFormat`] if the superblock is larger than 128 MiB
    ///   ([`MAX_METADATA_SIZE`]), or if inline parts
    ///   pass this limit.
    /// - [`Error::InvalidFormat`] if the mode table and the xattr table of a
    ///   part are larger than 128 MiB together.
    /// - [`Error::InvalidFormat`] if the temp file of the data source of a part
    ///   holds a byte count other than the count that the generation wrote.
    /// - [`Error::InvalidFormat`] if the size of the composefs image of `to`
    ///   changes during the generation.
    /// - [`Error::InvalidFormat`] if [`DeltaOptions::timestamp`] is `None` and
    ///   the system clock is before the Unix epoch.
    /// - [`Error::Core`] if the commit or its detached metadata does not parse,
    ///   or if `[core] fsync` in the config is not a boolean.
    /// - [`Error::Signature`] if a signer fails. A signer can also return
    ///   [`Error::InvalidFormat`] or [`Error::Core`].
    /// - [`Error::Io`] if a metadata object is larger than 128 MiB, or if a
    ///   read, a write, or an unlink on the file system fails.
    pub async fn generate_static_delta(
        &self,
        from: Option<&Checksum>,
        to: &Checksum,
        opts: &DeltaOptions,
    ) -> Result<PathBuf> {
        self.generate_static_delta_under(from, to, opts, MAX_SUPERBLOCK)
            .await
    }

    /// Runs [`generate_static_delta`](Repo::generate_static_delta) with
    /// `ceiling` as the superblock size for the budget of inline parts.
    ///
    /// The public call gives [`MAX_SUPERBLOCK`]. A test gives a smaller
    /// ceiling to reach the inline refusal without a 128 MiB part.
    async fn generate_static_delta_under(
        &self,
        from: Option<&Checksum>,
        to: &Checksum,
        opts: &DeltaOptions,
        ceiling: u64,
    ) -> Result<PathBuf> {
        if opts.max_chunk_size == 0 {
            return Err(Error::InvalidFormat(
                "static delta max chunk size must be positive".to_owned(),
            ));
        }
        if opts.output_dir.is_some() && opts.superblock_file.is_some() {
            return Err(Error::InvalidFormat(
                "static delta output_dir and superblock_file cannot both be set".to_owned(),
            ));
        }
        // The superblock embeds the whole target commit, so the commit loads,
        // and must be present, before any write. Its detached metadata goes
        // into the superblock beside it, because a verifying pull of the
        // `ostree` command checks the delivered commit against that copy. The
        // two reads run in one call on the blocking pool, the commit first.
        let (commit_bytes, detached) = self.load_commit_and_detached(to).await?;
        // A valid key `ostree.composefs.digest.v0` is the condition for the
        // composefs image in the delta. The parse runs on the blocking pool.
        let (commit_bytes, keyed) = ostrya_rt::unblock(move || {
            let keyed = image_key(&commit_bytes).is_some();
            (commit_bytes, keyed)
        })
        .await;
        let commit = Target {
            bytes: &commit_bytes,
            detached: detached.as_ref(),
        };
        if let Some(from) = from
            && !self.has_object(ObjectType::Commit, from).await?
        {
            return Err(Error::ObjectNotFound {
                checksum: *from,
                ty: ObjectType::Commit,
            });
        }

        // A superblock-file target creates nothing when it opens. It opens
        // before the selection walk, so a refused target costs no walk.
        let superblock_target = match &opts.superblock_file {
            Some(file) => {
                let target = file.clone();
                let (fd, name) =
                    ostrya_rt::unblock(move || open_superblock_parent_blocking(&target)).await?;
                Some((file.clone(), fd, name))
            }
            None => None,
        };

        let mut selection = self.select_objects(from, to, keyed, opts).await?;
        let (dir_path, dir_fd, superblock_name) = match superblock_target {
            Some(target) => target,
            None => {
                let (path, fd) = self.open_delta_dir(from, to, opts).await?;
                (path, fd, SUPERBLOCK_FILE.to_owned())
            }
        };
        let tmp_fd = self.open_tmp_dir().await?;
        let fsync = self.config().fsync()?;

        // The new parts overwrite the old parts in place, so the superblock of
        // the previous delta at this location goes before the first part. A
        // file at a superblock-file path belongs to the caller. Only the final
        // rename replaces it, so a failed generation leaves it as it was. An
        // inline generation overwrites no part file. The previous superblock
        // stays until the rename replaces it, and a failed generation leaves
        // the previous delta whole.
        if opts.superblock_file.is_none() && !opts.inline {
            remove_superblock(&dir_fd).await?;
        }

        // The generation holds inline parts until it serializes the
        // superblock, so the parts share the superblock ceiling. Each part
        // compresses into a buffer capped at the space that the rest of the
        // superblock and the earlier parts leave. The count of the rest is its
        // minimum: the embedded commit, 33 bytes for each object in the
        // meta-entries, and 49 bytes for each fallback. A delta whose parts
        // cannot fit is then refused at the part that passes the ceiling. The
        // count leaves out the framing, the copy of the detached metadata, and
        // the signatures. The size check on the serialized superblock refuses
        // a superblock that they push past the ceiling.
        let mut target = if opts.inline {
            let fixed = commit_bytes
                .len()
                .saturating_add(selection.packed.len().saturating_mul(1 + 32))
                .saturating_add(selection.fallbacks.len().saturating_mul(1 + 32 + 8 + 8));
            PartTarget::Inline {
                budget: usize::try_from(ceiling)
                    .unwrap_or(usize::MAX)
                    .saturating_sub(fixed),
            }
        } else {
            PartTarget::File(&dir_fd)
        };
        let mut entries: Vec<PartEntry> = Vec::new();
        let mut part = Part::default();
        for item in &mut selection.packed {
            // A part closes if the next object can push its payload past the
            // chunk ceiling. The content size of the object is the estimate. It
            // is exact for a splice and an upper bound for a diffed object. The
            // decision comes before the object goes into the part, so the
            // payload of a part with more than one object never passes the
            // ceiling. An object that rollsums down to a small payload still
            // closes the part that it fits in. The cost is one more xz stream
            // and one more pair of mode and xattr tables, and the ceiling holds.
            if !part.is_empty() && part.payload_len() + item.content_size > opts.max_chunk_size {
                entries.push(write_part(&mut target, entries.len(), part, fsync).await?);
                part = Part::default();
            }
            self.add_object(&mut part, item, opts, tmp_fd.as_fd())
                .await?;
        }
        if !part.is_empty() {
            entries.push(write_part(&mut target, entries.len(), part, fsync).await?);
        }
        let part_files = if opts.inline { 0 } else { entries.len() };

        let mut superblock =
            self.build_superblock(from, to, &commit, &mut entries, &selection, opts)?;
        drop(entries);
        if !opts.signers.is_empty() {
            let signers: Vec<&dyn Signer> = opts.signers.iter().map(|s| s.as_ref()).collect();
            superblock = sign_superblock(superblock, Value::Array(Vec::new()), &signers).await?;
        }
        write_delta_file(&dir_fd, &superblock_name, &superblock, fsync).await?;
        // The sweep finds what it removes by name. This is safe only where
        // this code wrote each entry. An output directory of the caller can
        // hold files with the names of delta files.
        if opts.output_dir.is_none() && opts.superblock_file.is_none() {
            clean_delta_dir(&dir_fd, part_files).await?;
        }
        Ok(dir_path)
    }

    /// Signs the superblock of the delta in `dir` with `signer`.
    ///
    /// The signed payload is the raw superblock bytes, and the new superblock
    /// is the signed envelope. If the delta is already signed, the signature
    /// goes at the end of the signature array of the engine. The arrays of the
    /// other engines stay, so one call for each engine collects the signatures.
    ///
    /// The call replaces the superblock atomically. Call
    /// [`reindex_static_deltas`](Repo::reindex_static_deltas) after it,
    /// because the index records the digest of the superblock. The envelope
    /// adds to the superblock size, so the result is held to the 128 MiB
    /// limit of [`generate_static_delta`](Repo::generate_static_delta). If
    /// the signed superblock passes the limit, the call fails. It never
    /// writes a superblock that the read path refuses.
    ///
    /// # Errors
    ///
    /// - [`Error::Io`] if the `superblock` file in `dir` cannot be read, or if
    ///   it is larger than 128 MiB.
    /// - [`Error::Core`] if the superblock starts with the magic of the signed
    ///   envelope and does not parse as one.
    /// - [`Error::Signature`] if the signer fails. A signer can also return
    ///   [`Error::InvalidFormat`] or [`Error::Core`].
    /// - [`Error::InvalidFormat`] if the signed superblock is larger than
    ///   128 MiB.
    /// - [`Error::Core`] if `[core] fsync` in the config is not a boolean.
    /// - [`Error::Io`] if `dir` cannot be opened, or if the write of the
    ///   superblock fails.
    pub async fn sign_static_delta(&self, dir: &Path, signer: &dyn Signer) -> Result<()> {
        let bytes = read_capped(dir.join(SUPERBLOCK_FILE)).await?;
        let (payload, signatures) = split_envelope(bytes)?;
        let encoded = sign_superblock(payload, signatures, &[signer]).await?;

        let dir_fd = open_dir_path(dir).await?;
        let fsync = self.config().fsync()?;
        write_delta_file(&dir_fd, SUPERBLOCK_FILE, &encoded, fsync).await
    }

    /// Rebuilds the `delta-indexes/` cache from the deltas under `deltas/`.
    ///
    /// Each target commit gets one index file. The file lists each delta that
    /// produces the target, keyed by the delta name, with the SHA-256 of its
    /// superblock. The summary carries the same map. The entries are in
    /// delta-name order, and the `ostree` command writes them in hash-table
    /// order.
    ///
    /// The pass removes the index file of a target that has no delta left, so
    /// a stale entry cannot advertise a delta that is gone. The fanout
    /// directory that this removal empties stays, as it does for the `ostree`
    /// command. The pass skips a delta with no superblock, so a half-written
    /// delta does not fail the pass.
    ///
    /// # Errors
    ///
    /// - [`Error::Core`] if a delta directory that holds a superblock has a
    ///   name that does not decode to a checksum.
    /// - [`Error::Core`] if `[core] fsync` in the config is not a boolean.
    /// - [`Error::Io`] if a superblock is larger than 128 MiB.
    /// - [`Error::Io`] if an entry of `delta-indexes/` is not a directory.
    /// - [`Error::Io`] if a scan, a read, a write, or an unlink on the file
    ///   system fails.
    pub async fn reindex_static_deltas(&self) -> Result<()> {
        let mut by_target: BTreeMap<Checksum, BTreeMap<String, Checksum>> = BTreeMap::new();
        for entry in self.static_delta_digests().await? {
            by_target
                .entry(entry.to)
                .or_default()
                .insert(entry.name, entry.digest);
        }

        let fsync = self.config().fsync()?;
        let mut written: BTreeSet<String> = BTreeSet::new();
        for (to, deltas) in &by_target {
            self.write_delta_index(to, deltas, fsync).await?;
            let (fanout, name) = delta_index_parts(to);
            written.insert(format!("{fanout}/{name}"));
        }
        self.prune_delta_indexes(written).await
    }

    /// Rewrites the index file of the target commit `to` alone.
    ///
    /// The file is `delta-indexes/<fanout>/<rest>.index`. It lists the deltas
    /// under `deltas/` whose target is `to`. The scan rule of
    /// [`reindex_static_deltas`](Repo::reindex_static_deltas) finds the
    /// deltas, and the call reads only the superblocks of `to`.
    ///
    /// If no delta for `to` exists, the call removes the index file. An absent
    /// file, fanout directory, or `delta-indexes/` is no error, and the call
    /// creates no directory for it. The fanout directory that the removal
    /// empties stays.
    ///
    /// The index files of other targets stay as they are, stale files
    /// included. The call does not check that `to` names a commit object, so a
    /// target that the repository does not hold is valid.
    ///
    /// # Errors
    ///
    /// - [`Error::Io`] if the fanout or `delta-indexes` is not a directory, a
    ///   regular file for example.
    /// - [`Error::Io`] if a directory is at the path of the index file.
    /// - [`Error::Io`] if a superblock of `to` is larger than 128 MiB.
    /// - [`Error::Core`] if a delta directory that holds a superblock has a
    ///   name that does not decode to a checksum.
    /// - [`Error::Core`] if `[core] fsync` in the config is not a boolean.
    /// - [`Error::Io`] if a scan, a read, a write, or an unlink on the file
    ///   system fails.
    pub async fn reindex_static_deltas_to(&self, to: &Checksum) -> Result<()> {
        let deltas: BTreeMap<String, Checksum> = self
            .static_delta_digests_of(Some(to))
            .await?
            .into_iter()
            .map(|entry| (entry.name, entry.digest))
            .collect();
        if deltas.is_empty() {
            let repo = self.clone();
            let relative = delta_index_relative_path(to);
            return ostrya_rt::unblock(move || {
                remove_delta_index_blocking(repo.repo_fd(), &relative)
            })
            .await;
        }
        let fsync = self.config().fsync()?;
        self.write_delta_index(to, &deltas, fsync).await
    }

    /// Writes the index file of the target commit `to` from its delta map, and
    /// creates the fanout directory and its parents.
    async fn write_delta_index(
        &self,
        to: &Checksum,
        deltas: &BTreeMap<String, Checksum>,
        fsync: bool,
    ) -> Result<()> {
        let (fanout, name) = delta_index_parts(to);
        let dir_fd = self
            .open_repo_subdir(&format!("{DELTA_INDEXES_DIR}/{fanout}"))
            .await?;
        let index = index_value(deltas)?;
        write_delta_file(&dir_fd, &name, &index, fsync).await
    }

    /// Returns each delta under `deltas/`, sorted by delta name, with the
    /// SHA-256 of its superblock.
    ///
    /// The `delta-indexes/` cache and the `ostree.static-deltas` map of the
    /// summary both advertise this list, so both are built from it. The scan
    /// lists only a directory that holds a superblock, so a half-written delta
    /// stays unadvertised. If a superblock goes away between the scan and the
    /// read, the call skips it and does not fail.
    pub(crate) async fn static_delta_digests(&self) -> Result<Vec<DeltaDigest>> {
        self.static_delta_digests_of(None).await
    }

    /// Returns the deltas of [`static_delta_digests`](Repo::static_delta_digests),
    /// limited to the target commit `to` if it is given.
    ///
    /// The limit applies before a superblock is read, so a pass over one
    /// target reads only the superblocks of that target.
    async fn static_delta_digests_of(&self, to: Option<&Checksum>) -> Result<Vec<DeltaDigest>> {
        let mut out = Vec::new();
        for (from, target) in self.list_static_delta_targets().await? {
            if to.is_some_and(|to| *to != target) {
                continue;
            }
            let relative = format!(
                "{}/{SUPERBLOCK_FILE}",
                delta_relative_dir(from.as_ref(), &target)
            );
            let Some(bytes) = self.read_repo_file(&relative).await? else {
                continue;
            };
            out.push(DeltaDigest {
                name: delta_name(from.as_ref(), &target),
                to: target,
                digest: Checksum::sha256(&bytes),
            });
        }
        // The `ostree` command writes the index files and the summary map in
        // hash-table order, and ostrya does not reproduce that order. A sort by
        // name gives both one order, whatever order the file system returns
        // for the `deltas/` tree.
        out.sort_by(|a, b| a.name.cmp(&b.name));
        Ok(out)
    }

    /// Returns the target commits of the index files in `delta-indexes/`, sorted.
    ///
    /// An index file is a regular file `delta-indexes/<fanout>/<rest>.index`.
    /// Its fanout is a directory with a two-character name, and
    /// `<fanout><rest>` is the 43-character modified-base64 form of the target
    /// checksum. The call skips each other entry, symlinks included.
    ///
    /// The call reads no delta directory, so it lists a target also if no
    /// delta for it exists. A repository with no `delta-indexes/` lists
    /// nothing.
    ///
    /// # Errors
    ///
    /// - [`Error::Io`] if `delta-indexes/` or a fanout directory cannot be
    ///   opened or read.
    pub async fn list_static_delta_indexes(&self) -> Result<Vec<Checksum>> {
        let repo = self.clone();
        ostrya_rt::unblock(move || list_delta_indexes_blocking(repo.repo_fd())).await
    }

    /// Returns the deltas that the index files advertise on the paths back
    /// from `commits`, sorted by delta name.
    ///
    /// The walk reads the index file of each commit in `commits`. For each
    /// delta from a source commit, it then reads the index file of that source
    /// commit, and so on. The walk reads the index file of a commit one time
    /// at most, so a cycle of deltas ends the walk. A commit with no index
    /// file adds no delta. An index file is a file that
    /// [`list_static_delta_indexes`](Repo::list_static_delta_indexes) lists.
    ///
    /// An index file lists only the deltas to its own commit, so a delta name
    /// can occur in one index file alone. If an index file lists a delta name
    /// more than one time, the first entry applies, as in a pull.
    ///
    /// The walk reads `delta-indexes/` alone. It does not open `deltas/`, and
    /// it does not check that a commit or a delta exists. It takes no lock.
    ///
    /// # Errors
    ///
    /// - [`Error::InvalidFormat`] if an index file is larger than
    ///   [`MAX_METADATA_SIZE`], if it does not decode as an `a{sv}`, if it
    ///   holds no `ostree.static-deltas` map, if the map fails the rules of
    ///   [`Summary::static_deltas`], or if an entry names a delta to another
    ///   commit. The message names the index file.
    /// - [`Error::Io`] if `delta-indexes/` is not a directory, or if an open
    ///   or a read fails.
    ///
    /// [`Summary::static_deltas`]: crate::Summary::static_deltas
    pub(crate) async fn walk_delta_indexes(
        &self,
        commits: impl IntoIterator<Item = Checksum>,
    ) -> Result<Vec<AdvertisedDelta>> {
        let repo = self.clone();
        let commits: Vec<Checksum> = commits.into_iter().collect();
        ostrya_rt::unblock(move || walk_delta_indexes_blocking(repo.repo_fd(), commits)).await
    }

    /// Removes the index files under `delta-indexes/` that this pass did not
    /// write.
    ///
    /// `written` holds the `<fanout>/<rest>.index` names that the pass wrote.
    /// The call leaves each name that does not end in [`INDEX_SUFFIX`], so a
    /// temp file of a concurrent write stays.
    async fn prune_delta_indexes(&self, written: BTreeSet<String>) -> Result<()> {
        let repo = self.clone();
        ostrya_rt::unblock(move || prune_delta_indexes_blocking(repo.repo_fd(), &written)).await
    }

    /// Splits the objects that the delta delivers into packed objects and
    /// loose fallbacks.
    ///
    /// Each packed content object pairs with the object at the same path in
    /// the source commit. Objects that `from` reaches are not delivered, and
    /// the superblock carries the target commit object. Metadata objects come
    /// before content objects, and each group is in checksum order, so the
    /// same inputs give the same delta.
    ///
    /// `keyed` is `true` if `to` carries a valid key
    /// `ostree.composefs.digest.v0`. If it is `true` and the repository
    /// stores the composefs image of `to`, the image comes last, as a packed
    /// object or as a fallback, by the rule of [`image_is_fallback`]. A
    /// packed image keeps the file that the selection opens. The generation
    /// does not read the image to check its digest.
    async fn select_objects(
        &self,
        from: Option<&Checksum>,
        to: &Checksum,
        keyed: bool,
        opts: &DeltaOptions,
    ) -> Result<Selection> {
        let mut needed = self.traverse_commit(to, 0).await?;
        if let Some(from) = from {
            for name in self.traverse_commit(from, 0).await? {
                needed.remove(&name);
            }
        }

        let mut metadata: Vec<(ObjectType, Checksum)> = Vec::new();
        let mut content: Vec<Checksum> = Vec::new();
        for name in needed {
            match name.ty {
                // The superblock embeds the target commit, and no other commit
                // object is reachable at depth 0.
                ObjectType::Commit => {}
                ty if ty.is_meta() => metadata.push((ty, name.checksum)),
                _ => content.push(name.checksum),
            }
        }
        metadata.sort_by_key(|(ty, checksum)| (ty.as_u32(), *checksum));
        content.sort();

        let sources = match from {
            Some(from) => self.pair_by_path(from, to).await?,
            None => HashMap::new(),
        };

        let mut selection = Selection::default();
        for (ty, checksum) in metadata {
            // Each repository mode stores metadata objects uncompressed, so
            // the size on disk is the size that the part carries.
            selection.packed.push(PackItem {
                objtype: ty,
                checksum,
                content_size: self.loose_object_size(ty, &checksum).await?,
                source: None,
                file: None,
                image: None,
            });
        }
        for checksum in content {
            let file = self.load_file(&checksum).await?;
            let content_size = content_size(&file);
            if opts.min_fallback_size != 0 && stream_size(&file)? >= opts.min_fallback_size {
                selection.fallbacks.push(FallbackItem {
                    objtype: ObjectType::File,
                    checksum,
                    compressed_size: self.loose_object_size(ObjectType::File, &checksum).await?,
                    content_size,
                });
                continue;
            }
            // A diff needs the source object. If the source object is no
            // longer present, the object is delivered whole.
            let source = match sources.get(&checksum) {
                Some(source) if self.has_object(ObjectType::File, source).await? => Some(*source),
                _ => None,
            };
            selection.packed.push(PackItem {
                objtype: ObjectType::File,
                checksum,
                content_size,
                source,
                file: Some(file),
                image: None,
            });
        }

        // The image goes into a part whole, with no diff.
        if keyed && let Some((file, size)) = self.open_composefs_image(to).await? {
            if image_is_fallback(size, opts) {
                selection.fallbacks.push(FallbackItem {
                    objtype: ObjectType::ComposefsImage,
                    checksum: *to,
                    compressed_size: size,
                    content_size: size,
                });
            } else {
                selection.packed.push(PackItem {
                    objtype: ObjectType::ComposefsImage,
                    checksum: *to,
                    content_size: size,
                    source: None,
                    file: None,
                    image: Some(file),
                });
            }
        }
        Ok(selection)
    }

    /// Maps each content object of `to` to the object at the same path in
    /// `from`.
    ///
    /// Pairing is by path, so a file that moved does not pair with its old
    /// version. An object at more than one path takes the first pairing in
    /// path order.
    async fn pair_by_path(
        &self,
        from: &Checksum,
        to: &Checksum,
    ) -> Result<HashMap<Checksum, Checksum>> {
        let old = self.file_paths(from).await?;
        let mut sources = HashMap::new();
        for (path, new_checksum) in self.file_paths(to).await? {
            if let Some(&old_checksum) = old.get(&path)
                && old_checksum != new_checksum
            {
                sources.entry(new_checksum).or_insert(old_checksum);
            }
        }
        Ok(sources)
    }

    /// Returns each file path in the tree of a commit, with the content object
    /// at that path.
    async fn file_paths(&self, commit: &Checksum) -> Result<BTreeMap<String, Checksum>> {
        let (commit, _) = self.load_commit(commit).await?;
        let mut paths = BTreeMap::new();
        let mut stack = vec![(String::new(), commit.root_dirtree)];
        while let Some((prefix, dirtree)) = stack.pop() {
            let dirtree = self.load_dirtree(&dirtree).await?;
            for (name, checksum) in dirtree.files {
                paths.insert(format!("{prefix}/{name}"), checksum);
            }
            for (name, subtree, _) in dirtree.dirs {
                stack.push((format!("{prefix}/{name}"), subtree));
            }
        }
        Ok(paths)
    }

    /// Adds one object to the part under construction: its bytes go into the
    /// data source, and its operations go onto the stream.
    async fn add_object(
        &self,
        part: &mut Part,
        item: &mut PackItem,
        opts: &DeltaOptions,
        tmp: BorrowedFd<'_>,
    ) -> Result<()> {
        // The composefs image streams into the data source from the file
        // that the selection opened, and the generation never holds it whole.
        // The read stops one byte past the selected size, so an image that
        // grows costs no more than that byte. Its `S` has the two operands of
        // a metadata splice.
        if item.objtype == ObjectType::ComposefsImage {
            use futures_lite::AsyncReadExt;

            let file = item
                .image
                .take()
                .expect("the selection opens each composefs image that it packs");
            let size = item.content_size;
            let reader = FileReader::with_len_hint(file, size).take(size + 1);
            let (offset, len) = part.blob.append_reader(reader, tmp).await?;
            if len != size {
                return Err(Error::InvalidFormat(format!(
                    "the size of the composefs image of {} changed after the generation \
                     selected it at {size} byte(s)",
                    item.checksum
                )));
            }
            part.push_op(OP_OPEN_SPLICE_CLOSE, &[len, offset]);
            part.finish_object(item.objtype, item.checksum, len);
            return Ok(());
        }

        // Only a content object carries a loaded file object. A metadata
        // object is spliced from its serialized bytes.
        let Some(file) = &item.file else {
            let bytes = self.load_object_bytes(item.objtype, &item.checksum).await?;
            let offset = part.blob.append(&bytes, tmp).await?;
            part.push_op(OP_OPEN_SPLICE_CLOSE, &[bytes.len() as u64, offset]);
            part.finish_object(item.objtype, item.checksum, bytes.len() as u64);
            return Ok(());
        };

        let mode_index = part.mode_index(file.uid, file.gid, file.mode);
        let xattr_index = part.xattr_index(&file.xattrs);

        if let FileKind::Symlink { target } = &file.kind {
            let offset = part.blob.append(target.as_bytes(), tmp).await?;
            part.push_op(
                OP_OPEN_SPLICE_CLOSE,
                &[mode_index, xattr_index, target.len() as u64, offset],
            );
            part.finish_object(item.objtype, item.checksum, target.len() as u64);
            return Ok(());
        }

        // A diff candidate needs random access to both objects, so both load
        // as heap-or-mmap blobs. Without a source, the content streams straight
        // into the data source, and the generation never holds it whole.
        let Some(source) = item.source else {
            let reader = file.reader().await?;
            let (offset, len) = part.blob.append_reader(reader, tmp).await?;
            part.push_op(
                OP_OPEN_SPLICE_CLOSE,
                &[mode_index, xattr_index, len, offset],
            );
            part.finish_object(item.objtype, item.checksum, len);
            return Ok(());
        };

        let target_blob = self.load_content_blob(file, tmp).await?;
        let source_blob = self
            .load_content_blob(&self.load_file(&source).await?, tmp)
            .await?;
        // The chunking and the hashing of both objects from end to end are
        // CPU-bound, so they run on the blocking pool, as the patch attempt
        // does. The blobs are owned values. They move in with the work and
        // come back with the plan.
        let (plan, source_blob, target_blob) = ostrya_rt::unblock(move || {
            let plan = rollsum::plan(source_blob.as_slice(), target_blob.as_slice());
            (plan, source_blob, target_blob)
        })
        .await;
        // The receiver rebuilds the object from these bytes, so its declared
        // output size comes from them. The size in the header is not used.
        let output_size = target_blob.as_slice().len() as u64;

        if plan.copied > 0 {
            part.push_op(OP_OPEN, &[mode_index, xattr_index, output_size]);
            let source_offset = part.blob.append(source.as_bytes(), tmp).await?;
            for run in &plan.runs {
                match *run {
                    Run::Copy {
                        source_offset: from,
                        length,
                    } => {
                        part.push_op(OP_SET_READ_SOURCE, &[source_offset]);
                        part.push_op(OP_WRITE, &[length, from]);
                        part.push_op(OP_UNSET_READ_SOURCE, &[]);
                    }
                    Run::Payload {
                        target_offset,
                        length,
                    } => {
                        let start = target_offset as usize;
                        let end = start + length as usize;
                        let offset = part
                            .blob
                            .append(&target_blob.as_slice()[start..end], tmp)
                            .await?;
                        part.push_op(OP_WRITE, &[length, offset]);
                    }
                }
            }
            part.push_op(OP_CLOSE, &[]);
            part.finish_object(item.objtype, item.checksum, output_size);
            return Ok(());
        }

        // Chunking found nothing shared to copy. If the object is small enough
        // that the failure of chunking says nothing about the relation of the
        // two objects, the code tries a patch. It keeps the patch only if the
        // patch beats a splice of the content. The blobs move into the patch
        // attempt, which gives the target back for the splice.
        let mut target_blob = target_blob;
        // The suffix sort is over the source, so the limit applies to both
        // objects. The limit is the smaller of the chunker bound and the
        // option of the caller.
        let limit = BSDIFF_CONTENT_LIMIT.min(opts.max_bsdiff_size);
        let source_size = source_blob.as_slice().len() as u64;
        if opts.bsdiff && output_size <= limit && source_size <= limit {
            let (stream, returned) = bsdiff_stream(source_blob, target_blob).await?;
            target_blob = returned;
            if patch_beats_splicing(&stream, output_size) {
                part.push_op(OP_OPEN, &[mode_index, xattr_index, output_size]);
                let source_offset = part.blob.append(source.as_bytes(), tmp).await?;
                let stream_offset = part.blob.append(&stream, tmp).await?;
                part.push_op(OP_SET_READ_SOURCE, &[source_offset]);
                part.push_op(OP_BSPATCH, &[stream_offset, stream.len() as u64]);
                part.push_op(OP_UNSET_READ_SOURCE, &[]);
                part.push_op(OP_CLOSE, &[]);
                part.finish_object(item.objtype, item.checksum, output_size);
                return Ok(());
            }
        }

        let offset = part.blob.append(target_blob.as_slice(), tmp).await?;
        part.push_op(
            OP_OPEN_SPLICE_CLOSE,
            &[mode_index, xattr_index, output_size, offset],
        );
        part.finish_object(item.objtype, item.checksum, output_size);
        Ok(())
    }

    /// Loads the payload of a content object for random access.
    ///
    /// A small payload loads on the heap. A large payload loads in a read-only
    /// mapping of a temp file.
    async fn load_content_blob(&self, file: &FileObject, tmp: BorrowedFd<'_>) -> Result<Blob> {
        let reader = file.reader().await?;
        let owned = tmp.try_clone_to_owned()?;
        spill_to_blob(reader, &owned, None).await
    }

    /// Builds the superblock GVariant.
    ///
    /// The metadata dict holds a copy of the detached metadata of the target
    /// commit, under the delta directory with `/commitmeta` added. A pull of
    /// the `ostree` command verifies a commit from a delta against that copy.
    /// A signed commit then reaches a verifying destination only if the copy
    /// is here. A commit with no detached metadata gets no entry.
    ///
    /// The dict entries are in the order of the `ostree` command:
    /// `ostree.endianness`, then each inline part in part order under
    /// `<dir>/<i>`, then `<dir>/commitmeta`. The inline bytes move out of
    /// `entries` into the dict. They are dropped with the dict after the
    /// superblock is serialized.
    fn build_superblock(
        &self,
        from: Option<&Checksum>,
        to: &Checksum,
        target: &Target<'_>,
        entries: &mut [PartEntry],
        selection: &Selection,
        opts: &DeltaOptions,
    ) -> Result<Vec<u8>> {
        let (byte, big_endian) = match opts.endianness {
            DeltaEndianness::Little => (ENDIANNESS_LITTLE, false),
            DeltaEndianness::Big => (ENDIANNESS_BIG, true),
        };
        // The sizes are in host order, which the `ostree.endianness` byte
        // declares. The serializer writes little-endian on each host, so the
        // code swaps a big-endian field here.
        let host = |value: u64| {
            if big_endian {
                value.swap_bytes()
            } else {
                value
            }
        };
        let mut metadata = Value::Array(Vec::new());
        crate::commit::append_dict_entry(
            &mut metadata,
            ENDIANNESS_KEY,
            Value::variant(
                Type::parse("y").map_err(ostrya_core::Error::from)?,
                Value::Byte(byte),
            ),
        )?;
        let inline_ty = Type::parse("(yay)").map_err(ostrya_core::Error::from)?;
        for (index, entry) in entries.iter_mut().enumerate() {
            let Some((compression, body)) = entry.inline.take() else {
                continue;
            };
            crate::commit::append_dict_entry(
                &mut metadata,
                &format!("{}/{index}", delta_relative_dir(from, to)),
                Value::variant(
                    inline_ty.clone(),
                    Value::Tuple(vec![Value::Byte(compression), Value::Bytes(body)]),
                ),
            )?;
        }
        if let Some(detached) = target.detached {
            crate::commit::append_dict_entry(
                &mut metadata,
                &format!("{}/commitmeta", delta_relative_dir(from, to)),
                Value::variant(
                    Type::parse("a{sv}").map_err(ostrya_core::Error::from)?,
                    detached.clone(),
                ),
            )?;
        }

        let commit_ty = Type::parse(COMMIT_SIG).map_err(ostrya_core::Error::from)?;
        let commit = from_bytes(&commit_ty, target.bytes).map_err(ostrya_core::Error::from)?;

        let meta_entries = entries
            .iter()
            .map(|entry| {
                Value::Tuple(vec![
                    Value::U32(PART_VERSION),
                    Value::Bytes(entry.checksum.as_bytes().to_vec()),
                    Value::U64(host(entry.size)),
                    Value::U64(host(entry.uncompressed_size)),
                    Value::Bytes(object_array(&entry.objects)),
                ])
            })
            .collect();

        let fallbacks = selection
            .fallbacks
            .iter()
            .map(|fallback| {
                Value::Tuple(vec![
                    Value::Byte(fallback.objtype.as_u32() as u8),
                    Value::Bytes(fallback.checksum.as_bytes().to_vec()),
                    Value::U64(host(fallback.compressed_size)),
                    Value::U64(host(fallback.content_size)),
                ])
            })
            .collect();

        let superblock = Value::Tuple(vec![
            metadata,
            // The timestamp is big-endian, whatever the endianness byte is.
            Value::U64(resolve_timestamp(opts.timestamp)?.swap_bytes()),
            Value::Bytes(from.map_or_else(Vec::new, |from| from.as_bytes().to_vec())),
            Value::Bytes(to.as_bytes().to_vec()),
            commit,
            // The recursion array is always empty.
            Value::Bytes(Vec::new()),
            Value::Array(meta_entries),
            Value::Array(fallbacks),
        ]);
        let ty = Type::parse(SUPERBLOCK_SIG).map_err(ostrya_core::Error::from)?;
        let bytes = to_bytes(&ty, &superblock).map_err(ostrya_core::Error::from)?;
        check_superblock_size(bytes.len())?;
        Ok(bytes)
    }

    /// Returns the `(from, to)` pair of each delta under `deltas/`.
    async fn list_static_delta_targets(&self) -> Result<Vec<(Option<Checksum>, Checksum)>> {
        let repo = self.clone();
        ostrya_rt::unblock(move || crate::delta::list_delta_targets(repo.repo_fd())).await
    }

    /// Reads a file under the repository root, or returns `None` if it is
    /// absent.
    ///
    /// The metadata ceiling bounds the read, so it serves superblocks and index
    /// files, and no object payloads. A file past the ceiling is an error, as
    /// for [`read_capped`]. A prefix of a superblock gives an index digest that
    /// covers only part of the superblock.
    async fn read_repo_file(&self, relative: &str) -> Result<Option<Vec<u8>>> {
        use std::io::Read;

        let repo = self.clone();
        let relative = relative.to_owned();
        ostrya_rt::unblock(move || {
            let fd = match rustix::fs::openat(
                repo.repo_fd(),
                relative.as_str(),
                rustix::fs::OFlags::RDONLY | rustix::fs::OFlags::CLOEXEC,
                rustix::fs::Mode::empty(),
            ) {
                Ok(fd) => fd,
                Err(rustix::io::Errno::NOENT) => return Ok(None),
                Err(e) => return Err(Error::Io(e.into())),
            };
            let mut file = std::fs::File::from(fd);
            if file.metadata().map_err(Error::Io)?.len() > MAX_SUPERBLOCK {
                return Err(Error::Io(std::io::Error::other(format!(
                    "static delta file {relative} exceeds the size ceiling"
                ))));
            }
            let mut bytes = Vec::new();
            file.read_to_end(&mut bytes).map_err(Error::Io)?;
            Ok(Some(bytes))
        })
        .await
    }

    /// Opens a directory under the repository root, and creates it and its
    /// parents if they are absent.
    async fn open_repo_subdir(&self, relative: &str) -> Result<OwnedFd> {
        let repo = self.clone();
        let relative = relative.to_owned();
        ostrya_rt::unblock(move || open_subdir_blocking(repo.repo_fd(), &relative)).await
    }

    /// Opens the `tmp/` directory of the repository, which holds the spill
    /// files. [`crate::staging::open_tmp_dir`] creates it if it is absent.
    pub(crate) async fn open_tmp_dir(&self) -> Result<OwnedFd> {
        let repo = self.clone();
        ostrya_rt::unblock(move || {
            crate::staging::open_tmp_dir(repo.repo_fd(), repo.mode()).map_err(Error::Io)
        })
        .await
    }

    /// Creates and opens the directory for the files of the delta, and returns
    /// its path with the descriptor.
    async fn open_delta_dir(
        &self,
        from: Option<&Checksum>,
        to: &Checksum,
        opts: &DeltaOptions,
    ) -> Result<(PathBuf, OwnedFd)> {
        match &opts.output_dir {
            Some(dir) => {
                let path = dir.clone();
                let target = path.clone();
                let fd = ostrya_rt::unblock(move || create_dir_path_blocking(&target)).await?;
                Ok((path, fd))
            }
            None => {
                let relative = delta_relative_dir(from, to);
                let fd = self.open_repo_subdir(&relative).await?;
                Ok((PathBuf::from(relative), fd))
            }
        }
    }
}

/// The target commit as the superblock carries it: the serialized commit
/// object, and its detached metadata if it has any.
struct Target<'a> {
    bytes: &'a [u8],
    detached: Option<&'a Value>,
}

/// The objects that a delta delivers, split by their route.
#[derive(Default)]
struct Selection {
    packed: Vec<PackItem>,
    fallbacks: Vec<FallbackItem>,
}

/// One object that a part carries.
struct PackItem {
    objtype: ObjectType,
    checksum: Checksum,
    /// The payload size of the object: the serialized size of a metadata
    /// object, the content size of a file, the target length of a symlink,
    /// or the size of the composefs image.
    content_size: u64,
    /// The object at the same path in the source commit, if one exists for a
    /// diff.
    source: Option<Checksum>,
    /// The content object, loaded once at selection and used again when the
    /// object is packed. `None` for a metadata object, which is packed from
    /// its serialized bytes, and for the composefs image, which streams from
    /// its file.
    file: Option<FileObject>,
    /// The file of the composefs image, opened at selection and taken when
    /// the image is packed. `None` for each other object.
    image: Option<std::fs::File>,
}

/// One object that the delta names and does not carry: a content object or
/// the composefs image.
struct FallbackItem {
    objtype: ObjectType,
    checksum: Checksum,
    compressed_size: u64,
    content_size: u64,
}

/// Returns `true` if the composefs image of `size` bytes is a fallback
/// entry, and `false` if a part carries it.
///
/// The image is a fallback in an inline delta, because the generation and
/// the applier hold an inline part in memory. The applier holds the payload
/// of a part file in a mapped temp file, so the image in a part has a size
/// limit also when fallbacks are off.
fn image_is_fallback(size: u64, opts: &DeltaOptions) -> bool {
    opts.inline
        || (opts.min_fallback_size != 0 && size >= opts.min_fallback_size)
        || size > opts.max_chunk_size.min(MAX_PART_IMAGE)
}

/// A written part, as its meta-entry records it.
struct PartEntry {
    checksum: Checksum,
    /// The size of the part file on disk.
    size: u64,
    /// The sum of the uncompressed payloads of the objects of the part.
    uncompressed_size: u64,
    objects: Vec<(ObjectType, Checksum)>,
    /// The bytes of a part file, if the superblock carries the part inline:
    /// the compression byte, and the body after it. The superblock build takes
    /// them.
    inline: Option<(u8, Vec<u8>)>,
}

/// The destination of the bytes of a part in [`write_part`].
enum PartTarget<'a> {
    /// The numbered part file in this delta directory.
    File(&'a OwnedFd),
    /// A buffer that the superblock carries inline. `budget` is the space that
    /// the superblock ceiling leaves for the parts not yet written.
    Inline { budget: usize },
}

/// A part under construction: the mode and xattr tables, the data source, the
/// operation stream, and the objects that the stream produces, in order.
///
/// Each table is a vector in wire order, with a map from entry to index beside
/// it. The lookup of an object then costs one hash, with no scan of the
/// entries.
#[derive(Default)]
struct Part {
    modes: Vec<(u32, u32, u32)>,
    mode_slots: HashMap<(u32, u32, u32), u64>,
    xattrs: Vec<Xattrs>,
    xattr_slots: HashMap<Xattrs, u64>,
    blob: Spill,
    ops: Vec<u8>,
    objects: Vec<(ObjectType, Checksum)>,
    uncompressed_size: u64,
}

impl Part {
    fn is_empty(&self) -> bool {
        self.objects.is_empty()
    }

    /// Returns the size of the payload so far, to which the chunk ceiling
    /// applies.
    fn payload_len(&self) -> u64 {
        self.blob.len() + self.ops.len() as u64
    }

    /// Appends one operation: its opcode, then its LEB128 operands.
    fn push_op(&mut self, opcode: u8, operands: &[u64]) {
        self.ops.push(opcode);
        for &operand in operands {
            varint::encode(operand, &mut self.ops);
        }
    }

    /// Records that the operations just emitted complete one object.
    fn finish_object(&mut self, objtype: ObjectType, checksum: Checksum, payload: u64) {
        self.objects.push((objtype, checksum));
        self.uncompressed_size += payload;
    }

    /// Returns the index of `(uid, gid, mode)` in the mode table, and appends
    /// the entry if it is new.
    fn mode_index(&mut self, uid: u32, gid: u32, mode: u32) -> u64 {
        let triple = (uid, gid, mode);
        if let Some(&index) = self.mode_slots.get(&triple) {
            return index;
        }
        let index = self.modes.len() as u64;
        self.modes.push(triple);
        self.mode_slots.insert(triple, index);
        index
    }

    /// Returns the index of an xattr set in the xattr table, and appends the
    /// set if it is new.
    fn xattr_index(&mut self, xattrs: &Xattrs) -> u64 {
        if let Some(&index) = self.xattr_slots.get(xattrs) {
            return index;
        }
        let index = self.xattrs.len() as u64;
        self.xattrs.push(xattrs.clone());
        self.xattr_slots.insert(xattrs.clone(), index);
        index
    }
}

/// An append-only buffer for the data source of a part: on the heap up to
/// [`MMAP_THRESHOLD`], then in an anonymous temp file.
///
/// After the move, the payload of a part costs disk space, and only the
/// streaming window is resident.
enum Spill {
    Ram(Vec<u8>),
    File { file: RtFile, len: u64 },
}

impl Default for Spill {
    fn default() -> Self {
        Spill::Ram(Vec::new())
    }
}

impl Spill {
    fn len(&self) -> u64 {
        match self {
            Spill::Ram(buf) => buf.len() as u64,
            Spill::File { len, .. } => *len,
        }
    }

    /// Appends `bytes` and returns the offset at which they were written.
    async fn append(&mut self, bytes: &[u8], tmp: BorrowedFd<'_>) -> Result<u64> {
        let offset = self.len();
        if let Spill::Ram(buf) = self
            && buf.len() + bytes.len() > MMAP_THRESHOLD
        {
            self.spill(tmp).await?;
        }
        match self {
            Spill::Ram(buf) => buf.extend_from_slice(bytes),
            Spill::File { file, len } => {
                file.write_all(bytes).await.map_err(Error::Io)?;
                *len += bytes.len() as u64;
            }
        }
        Ok(offset)
    }

    /// Appends all bytes that `reader` yields, and returns the offset and the
    /// length written.
    ///
    /// The reader drains in [`IO_CHUNK`] pieces, so an object of any size goes
    /// through a bounded buffer.
    async fn append_reader<R: futures_io::AsyncRead + Unpin>(
        &mut self,
        mut reader: R,
        tmp: BorrowedFd<'_>,
    ) -> Result<(u64, u64)> {
        use futures_lite::AsyncReadExt;

        let offset = self.len();
        let mut chunk = vec![0u8; IO_CHUNK];
        let mut total = 0u64;
        loop {
            let n = reader.read(&mut chunk).await.map_err(Error::Io)?;
            if n == 0 {
                break;
            }
            self.append(&chunk[..n], tmp).await?;
            total += n as u64;
        }
        Ok((offset, total))
    }

    /// Moves an in-memory buffer into a temp file.
    async fn spill(&mut self, tmp: BorrowedFd<'_>) -> Result<()> {
        let Spill::Ram(buf) = self else {
            return Ok(());
        };
        let owned = tmp.try_clone_to_owned()?;
        let fd = ostrya_rt::unblock(move || open_rw_temp(owned.as_fd())).await?;
        let mut file = RtFile::from(fd);
        file.write_all(buf).await.map_err(Error::Io)?;
        let len = buf.len() as u64;
        *self = Spill::File { file, len };
        Ok(())
    }

    /// Returns the buffer as a blocking handle at its start, so a blocking-pool
    /// thread can stream the payload of a part.
    async fn into_blocking(self) -> Result<BlockingSpill> {
        match self {
            Spill::Ram(buf) => Ok(BlockingSpill::Ram(buf)),
            Spill::File { mut file, len } => {
                // The async file writes on a background task. The next `flush`
                // reports a failed write, and `write_all` does not. `into_std`
                // completes pending writes and reports no error. This flush
                // turns an `ENOSPC` or `EIO` on the spill file into a failed
                // generation. Without it, the part compresses from a truncated
                // data source, and its framing offsets still count the bytes
                // that the spill accepted.
                file.flush().await.map_err(Error::Io)?;
                // The recovered file shares the open file description. The read
                // starts from an explicit rewind, because the appends moved the
                // offset.
                let mut file = file.into_std().await;
                std::io::Seek::seek(&mut file, SeekFrom::Start(0)).map_err(Error::Io)?;
                Ok(BlockingSpill::File { file, len })
            }
        }
    }
}

/// The data source of a part, ready to stream from a blocking-pool thread, in
/// the same heap or temp-file form that [`Spill`] used.
enum BlockingSpill {
    Ram(Vec<u8>),
    File { file: std::fs::File, len: u64 },
}

impl BlockingSpill {
    /// Streams the contents of the buffer into `out` in [`IO_CHUNK`] pieces, so
    /// a payload of any size goes through a bounded buffer.
    ///
    /// The temp-file form counts the bytes that it streams. It refuses a count
    /// other than the length that the spill recorded, which is the source of
    /// the framing offsets of the part. If the data source lost bytes, the
    /// generation fails, and no part decodes short when it is applied.
    async fn write_into<W: futures_io::AsyncWrite + Unpin>(self, out: &mut W) -> Result<()> {
        match self {
            BlockingSpill::Ram(buf) => out.write_all(&buf).await.map_err(Error::Io),
            BlockingSpill::File { mut file, len } => {
                let mut chunk = vec![0u8; IO_CHUNK];
                let mut streamed = 0u64;
                loop {
                    let n = match std::io::Read::read(&mut file, &mut chunk) {
                        // An interrupted read is retried, as `read_to_end` does.
                        Err(e) if e.kind() == std::io::ErrorKind::Interrupted => continue,
                        result => result.map_err(Error::Io)?,
                    };
                    if n == 0 {
                        break;
                    }
                    out.write_all(&chunk[..n]).await.map_err(Error::Io)?;
                    streamed += n as u64;
                }
                if streamed != len {
                    return Err(Error::InvalidFormat(format!(
                        "static delta part data source holds {streamed} bytes, not the \
                         {len} its framing counts"
                    )));
                }
                Ok(())
            }
        }
    }
}

/// Writes one part, to its numbered part file or to an inline buffer, and
/// returns its meta-entry.
///
/// The payload GVariant `(a(uuu)aa(ayay)ayay)` goes straight into the xz
/// encoder, in this order:
///
/// 1. The two tables, which are bounded metadata built in memory.
/// 2. The data source, streamed from its spill buffer.
/// 3. The operation stream.
/// 4. The framing offsets of the tuple. Their width follows from the payload
///    length, which is known before the first byte is written.
///
/// The SHA-256 covers the whole file on disk, with the compression byte. The
/// meta-entry records this digest. This function builds the framing and the
/// two tables. [`compress_part`] does the compression and the file write, on
/// the blocking pool.
async fn write_part(
    target: &mut PartTarget<'_>,
    index: usize,
    part: Part,
    fsync: bool,
) -> Result<PartEntry> {
    let modes = mode_table(&part.modes);
    let xattrs = xattr_table(&part.xattrs)?;
    check_table_size(modes.len() + xattrs.len())?;
    let blob_len = part.blob.len();
    let ops_len = part.ops.len() as u64;

    // Field alignments: `a(uuu)` needs 4 and is at offset 0. The xattr table,
    // the data source, and the operation stream align to 1, so no padding
    // falls between the members.
    let body = modes.len() as u64 + xattrs.len() as u64 + blob_len + ops_len;
    let body =
        usize::try_from(body).map_err(|_| Error::InvalidFormat("part payload too large".into()))?;
    let width = choose_offset_size(body, 3);
    let mut offsets = Vec::with_capacity(3 * width);
    // Tuple offsets are the end of each variable-size member except the last,
    // written in reverse member order.
    write_offset(
        &mut offsets,
        modes.len() + xattrs.len() + blob_len as usize,
        width,
    );
    write_offset(&mut offsets, modes.len() + xattrs.len(), width);
    write_offset(&mut offsets, modes.len(), width);

    let dir_fd = match target {
        PartTarget::File(dir_fd) => *dir_fd,
        PartTarget::Inline { budget } => {
            let limit = *budget;
            let blob = part.blob.into_blocking().await?;
            let ops = part.ops;
            let (checksum, size, inline) = ostrya_rt::unblock(move || {
                let mut capped = CappedBuf::new(limit);
                match compress_part(&mut capped, &modes, &xattrs, blob, &ops, &offsets) {
                    Ok((checksum, size)) => Ok((checksum, size, capped.into_parts())),
                    Err(_) if capped.len() == limit => Err(inline_ceiling_error()),
                    Err(err) => Err(err),
                }
            })
            .await?;
            *budget -= 1 + inline.1.len();
            return Ok(PartEntry {
                checksum,
                size,
                uncompressed_size: part.uncompressed_size,
                objects: part.objects,
                inline: Some(inline),
            });
        }
    };

    let name = index.to_string();
    let temp = TempFile::create(dir_fd, &name);
    let fd = {
        let owned = dir_fd.try_clone()?;
        let temp = temp.name().to_owned();
        ostrya_rt::unblock(move || create_file_blocking(owned.as_fd(), &temp)).await?
    };

    // The spill buffer gives a blocking handle, so the payload, its
    // compression, and the file write all run on one blocking-pool thread.
    let blob = part.blob.into_blocking().await?;
    let ops = part.ops;
    let (checksum, size) = ostrya_rt::unblock(move || {
        let file = std::fs::File::from(fd);
        compress_part(file, &modes, &xattrs, blob, &ops, &offsets)
    })
    .await?;

    finish_file(dir_fd, temp.name(), &name, fsync).await?;
    temp.keep();
    Ok(PartEntry {
        checksum,
        size,
        uncompressed_size: part.uncompressed_size,
        objects: part.objects,
        inline: None,
    })
}

/// A buffer that takes at most `limit` bytes.
///
/// A write past the limit fills the buffer to the limit and then fails, so a
/// caller finds the cause from the length. The buffer holds the first byte, the
/// compression byte of a part, apart from the body after it. The superblock
/// carries the part in this form.
struct CappedBuf {
    compression: Option<u8>,
    body: Vec<u8>,
    limit: usize,
}

impl CappedBuf {
    fn new(limit: usize) -> CappedBuf {
        CappedBuf {
            compression: None,
            body: Vec::new(),
            limit,
        }
    }

    /// Returns the count of bytes taken, with the compression byte.
    fn len(&self) -> usize {
        usize::from(self.compression.is_some()) + self.body.len()
    }

    /// Returns the compression byte and the body. A part always starts with
    /// its compression byte, so a buffer that took no byte reads as
    /// uncompressed.
    fn into_parts(self) -> (u8, Vec<u8>) {
        (self.compression.unwrap_or(0), self.body)
    }
}

impl std::io::Write for CappedBuf {
    fn write(&mut self, bytes: &[u8]) -> std::io::Result<usize> {
        let room = self.limit - self.len();
        if room == 0 && !bytes.is_empty() {
            return Err(std::io::Error::other(
                "static delta inline part passes the superblock ceiling",
            ));
        }
        let n = bytes.len().min(room);
        let mut taken = &bytes[..n];
        if self.compression.is_none()
            && let Some((&first, rest)) = taken.split_first()
        {
            self.compression = Some(first);
            taken = rest;
        }
        self.body.extend_from_slice(taken);
        Ok(n)
    }

    fn flush(&mut self) -> std::io::Result<()> {
        Ok(())
    }
}

/// Returns the refusal of inline parts that do not fit in a superblock that the
/// read path accepts.
fn inline_ceiling_error() -> Error {
    Error::InvalidFormat(format!(
        "static delta inline parts pass the {MAX_SUPERBLOCK}-byte superblock ceiling"
    ))
}

/// Compresses the payload of a part into `out`, a part file or an inline
/// buffer, and returns the SHA-256 and the size of the output.
///
/// This is the expensive half of a part write. xz at [`PART_XZ_LEVEL`] costs
/// seconds of CPU for each tens of megabytes and holds about 370 MiB of encoder
/// state. `XzEncoder` compresses inside `poll_write` and never yields. The
/// callers run this function on the blocking pool, off the executor threads, as
/// [`bsdiff_stream`] does for the other CPU-bound stage.
///
/// The encoder streams in both cases, so the payload is never buffered whole.
/// [`SyncWriter`] and [`BlockingSpill`] complete each I/O call in place. The
/// future never returns `Pending`, so `block_on` drives it to completion on
/// this thread with no executor.
fn compress_part<W: std::io::Write + Unpin>(
    out: W,
    modes: &[u8],
    xattrs: &[u8],
    blob: BlockingSpill,
    ops: &[u8],
    offsets: &[u8],
) -> Result<(Checksum, u64)> {
    let mut hashing = HashingWriter::new(Sha256::new(), SyncWriter(out));
    futures_lite::future::block_on(async {
        hashing
            .write_all(&[COMPRESSION_XZ])
            .await
            .map_err(Error::Io)?;
        let mut encoder = XzEncoder::with_quality(&mut hashing, Level::Precise(PART_XZ_LEVEL));
        encoder.write_all(modes).await.map_err(Error::Io)?;
        encoder.write_all(xattrs).await.map_err(Error::Io)?;
        blob.write_into(&mut encoder).await?;
        encoder.write_all(ops).await.map_err(Error::Io)?;
        encoder.write_all(offsets).await.map_err(Error::Io)?;
        encoder.close().await.map_err(Error::Io)?;
        drop(encoder);
        hashing.flush().await.map_err(Error::Io)
    })?;
    Ok(hashing.finalize())
}

/// A `futures-io` writer over a blocking writer: a file or a buffer.
///
/// Each method does its syscall and returns `Ready`, so a future over it never
/// parks. [`compress_part`] can then drive the async xz encoder to completion
/// on a blocking-pool thread.
struct SyncWriter<W>(W);

impl<W: std::io::Write + Unpin> futures_io::AsyncWrite for SyncWriter<W> {
    fn poll_write(
        self: Pin<&mut Self>,
        _: &mut Context<'_>,
        buf: &[u8],
    ) -> Poll<std::io::Result<usize>> {
        // An interrupted write is retried here. The `write_all` that drives
        // this writer is the futures-io one, which returns `Interrupted` as an
        // error. `std::io::Write::write_all` retries it.
        let file = &mut self.get_mut().0;
        loop {
            match std::io::Write::write(file, buf) {
                Err(e) if e.kind() == std::io::ErrorKind::Interrupted => continue,
                result => return Poll::Ready(result),
            }
        }
    }

    fn poll_flush(self: Pin<&mut Self>, _: &mut Context<'_>) -> Poll<std::io::Result<()>> {
        Poll::Ready(std::io::Write::flush(&mut self.get_mut().0))
    }

    fn poll_close(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<std::io::Result<()>> {
        self.poll_flush(cx)
    }
}

/// Returns the mode table `a(uuu)`: fixed-size 12-byte triples, big-endian on
/// the wire whatever the endianness byte of the superblock is.
fn mode_table(modes: &[(u32, u32, u32)]) -> Vec<u8> {
    let mut out = Vec::with_capacity(modes.len() * 12);
    for &(uid, gid, mode) in modes {
        out.extend_from_slice(&uid.to_be_bytes());
        out.extend_from_slice(&gid.to_be_bytes());
        out.extend_from_slice(&mode.to_be_bytes());
    }
    out
}

/// Returns the xattr table `aa(ayay)`: one entry for each distinct xattr set,
/// in the order in which the objects use them.
fn xattr_table(xattrs: &[Xattrs]) -> Result<Vec<u8>> {
    let entries = xattrs
        .iter()
        .map(|set| {
            Value::Array(
                set.iter()
                    .map(|(name, value)| {
                        Value::Tuple(vec![
                            Value::Bytes(name.to_vec()),
                            Value::Bytes(value.to_vec()),
                        ])
                    })
                    .collect(),
            )
        })
        .collect();
    let ty = Type::parse("aa(ayay)").map_err(ostrya_core::Error::from)?;
    Ok(to_bytes(&ty, &Value::Array(entries)).map_err(ostrya_core::Error::from)?)
}

/// Returns the array of 33-byte entries, an object type and a checksum, that a
/// meta-entry carries.
fn object_array(objects: &[(ObjectType, Checksum)]) -> Vec<u8> {
    let mut out = Vec::with_capacity(objects.len() * 33);
    for (objtype, checksum) in objects {
        out.push(objtype.as_u32() as u8);
        out.extend_from_slice(checksum.as_bytes());
    }
    out
}

/// Generates the bspatch stream that turns `source` into `target`, and gives
/// the target blob back.
///
/// A caller that rejects the patch can then splice from the blob. The patch
/// runs on the blocking pool, because bsdiff sorts the suffixes of the source.
/// The sort is CPU-bound and costs several times the source size in memory.
async fn bsdiff_stream(source: Blob, target: Blob) -> Result<(Vec<u8>, Blob)> {
    ostrya_rt::unblock(move || {
        let mut stream = Vec::new();
        bsdiff::diff(source.as_slice(), target.as_slice(), &mut stream).map_err(Error::Io)?;
        Ok((stream, target))
    })
    .await
}

/// Returns the count of novel bytes in a bspatch stream.
///
/// Most of a patch is its diff stream, which holds the byte-wise difference
/// between target and source. It is zero where the two agree. xz compresses
/// the part as a whole, which reduces those zero runs to almost nothing.
///
/// The size that matters is the count of nonzero bytes. A patch against a
/// near-identical source counts a few dozen bytes, whatever the object size.
fn novel_bytes(stream: &[u8]) -> u64 {
    stream.iter().filter(|&&byte| byte != 0).count() as u64
}

/// Returns `true` if a bspatch stream is worth more than a splice of the
/// content.
///
/// The bound is half the content size. A bound of the full content size does
/// not separate the two cases. A patch against unrelated content has diff and
/// extra blocks about as long as the target. Its bytes are nonzero except where
/// target and source agree, about 1 byte in 256 of high-entropy content.
///
/// For such a patch, [`novel_bytes`] is near 0.996 of the output size, which
/// passes a bound of 1.0. The delta from such a patch is larger than the splice
/// and costs several times the CPU. A real small edit counts a few dozen bytes,
/// so it stays far inside a bound of one half at any object size.
fn patch_beats_splicing(stream: &[u8], output_size: u64) -> bool {
    novel_bytes(stream) * 2 < output_size
}

/// The bytes that the `ostree` command adds to a content size when it compares
/// an object against [`DeltaOptions::min_fallback_size`].
///
/// The size compared is the file header variant, plus this constant, plus the
/// content. The content-stream framing on disk is eight bytes: a big-endian
/// `u32` length and four NUL bytes. The count that the `ostree` command
/// compares is one less.
///
/// The observed switch points at a 1,000,000-byte threshold, for three header
/// shapes:
///
/// - A plain file with no xattr and `uid=gid=0` has an 18-byte header. It packs
///   at a content size of 999,974 and falls back at 999,975.
/// - A file with an 8-byte xattr has a 39-byte header. It switches at 999,954.
/// - A file with a 300-byte xattr has a 334-byte header, past the GVariant
///   offset-width boundary. It switches at 999,659.
///
/// The overhead is 25, 46, and 341 bytes, seven more than the header in each
/// case. The offset table of the header counts, and the constant is flat.
const FALLBACK_FRAMING: u64 = 7;

/// Returns the stream size of a content object, as the fallback threshold
/// compares it: the file header variant, [`FALLBACK_FRAMING`], and the payload.
///
/// A large object travels as a loose object, and the part stays small.
fn stream_size(file: &FileObject) -> Result<u64> {
    let header = file.header();
    Ok(FALLBACK_FRAMING + header.serialize()?.len() as u64 + content_size(file))
}

/// Returns the payload size of a content object: the content length of a
/// regular file, or the target length of a symlink.
fn content_size(file: &FileObject) -> u64 {
    match &file.kind {
        FileKind::Regular { size } => *size,
        FileKind::Symlink { target } => target.len() as u64,
    }
}

/// Splits a superblock file into the payload that signatures cover and the
/// signature dict that it carries (an empty dict if the delta is unsigned).
fn split_envelope(bytes: Vec<u8>) -> Result<(Vec<u8>, Value)> {
    if !bytes.starts_with(SIGNED_MAGIC) {
        return Ok((bytes, Value::Array(Vec::new())));
    }
    let ty = Type::parse(SIGNED_SIG).map_err(ostrya_core::Error::from)?;
    let value = from_bytes(&ty, &bytes).map_err(ostrya_core::Error::from)?;
    let fields = crate::delta::tuple(&value)?;
    let payload = crate::delta::bytes_field(&fields[1], "signed superblock payload")?.to_vec();
    Ok((payload, fields[2].clone()))
}

/// Signs `payload` with each signer in order, adds the signatures to
/// `signatures`, and returns the signed envelope.
///
/// The envelope is held to the superblock ceiling.
async fn sign_superblock(
    payload: Vec<u8>,
    mut signatures: Value,
    signers: &[&dyn Signer],
) -> Result<Vec<u8>> {
    for signer in signers {
        let signature = signer.sign(&payload).await?;
        append_signature(&mut signatures, signer.metadata_key(), signature)?;
    }
    let envelope = Value::Tuple(vec![
        Value::U64(u64::from_le_bytes(*SIGNED_MAGIC)),
        Value::Bytes(payload),
        signatures,
    ]);
    let ty = Type::parse(SIGNED_SIG).map_err(ostrya_core::Error::from)?;
    let encoded = to_bytes(&ty, &envelope).map_err(ostrya_core::Error::from)?;
    check_superblock_size(encoded.len())?;
    Ok(encoded)
}

/// One delta under `deltas/`: its name, its target commit, and the SHA-256 of
/// its superblock, which both advertisements carry.
pub(crate) struct DeltaDigest {
    /// The name of the delta in hex, as the `ostree` command names it: `<to>`
    /// for a delta from scratch, and `<from>-<to>` for other deltas.
    pub(crate) name: String,
    /// The target commit, which the index files are keyed by.
    pub(crate) to: Checksum,
    /// The SHA-256 of the `superblock` file of the delta.
    pub(crate) digest: Checksum,
}

/// Returns the `a{sv}` map that both advertisements carry: the name of each
/// delta to the 32-byte digest of its superblock.
///
/// The entries keep the order of the input.
fn delta_map_value<'a>(deltas: impl Iterator<Item = (&'a str, &'a Checksum)>) -> Result<Value> {
    let ay = Type::parse("ay").map_err(ostrya_core::Error::from)?;
    let mut map = Value::Array(Vec::new());
    for (name, digest) in deltas {
        append_dict_entry(
            &mut map,
            name,
            Value::variant(ay.clone(), Value::Bytes(digest.as_bytes().to_vec())),
        )?;
    }
    Ok(map)
}

/// Returns the `ostree.static-deltas` value of a summary: the map of
/// [`delta_map_value`] in a variant of type `a{sv}`.
pub(crate) fn summary_delta_map<'a>(
    deltas: impl Iterator<Item = (&'a str, &'a Checksum)>,
) -> Result<Value> {
    let map_ty = Type::parse("a{sv}").map_err(ostrya_core::Error::from)?;
    Ok(Value::variant(map_ty, delta_map_value(deltas)?))
}

/// Returns the `ostree.static-deltas` value of a summary that advertises
/// `deltas`, in the order of the slice.
///
/// The value has the bytes of the value that [`summary_delta_map`] gives
/// for the same deltas in the same order.
pub(crate) fn advertised_delta_map(deltas: &[AdvertisedDelta]) -> Result<Value> {
    let names: Vec<String> = deltas
        .iter()
        .map(|delta| delta_name(delta.from.as_ref(), &delta.to))
        .collect();
    summary_delta_map(
        names
            .iter()
            .map(String::as_str)
            .zip(deltas.iter().map(|delta| &delta.superblock)),
    )
}

/// Builds the `a{sv}` of an index file: the delta map under the shared
/// `ostree.static-deltas` key, with the superblock digest of each delta.
fn index_value(deltas: &BTreeMap<String, Checksum>) -> Result<Vec<u8>> {
    let map = delta_map_value(deltas.iter().map(|(name, digest)| (name.as_str(), digest)))?;
    let mut dict = Value::Array(Vec::new());
    let map_ty = Type::parse("a{sv}").map_err(ostrya_core::Error::from)?;
    append_dict_entry(
        &mut dict,
        STATIC_DELTAS_KEY,
        Value::variant(map_ty.clone(), map),
    )?;
    Ok(to_bytes(&map_ty, &dict).map_err(ostrya_core::Error::from)?)
}

/// Returns the delta map of an index file, with no variant wrapper, or `None`
/// if the file has no [`STATIC_DELTAS_KEY`] entry that holds a variant.
///
/// The file is the `a{sv}` that [`index_value`] writes. If the key occurs
/// more than once, the first entry applies, as for [`Value::dict_get`].
///
/// # Errors
///
/// - [`Error::Core`] if the bytes do not decode as an `a{sv}`.
/// - [`Error::InvalidFormat`] if the variant of the entry holds a type other
///   than `a{sv}`.
pub(crate) fn index_delta_map(bytes: &[u8]) -> Result<Option<Value>> {
    let ty = Type::parse("a{sv}").map_err(ostrya_core::Error::from)?;
    let Value::Array(entries) = from_bytes(&ty, bytes).map_err(ostrya_core::Error::from)? else {
        return Ok(None);
    };
    // The dict is owned, so the map moves out of it with no clone.
    for entry in entries {
        let Value::Tuple(fields) = entry else {
            continue;
        };
        let Ok([key, value]) = <[Value; 2]>::try_from(fields) else {
            continue;
        };
        if key.as_str() != Some(STATIC_DELTAS_KEY) {
            continue;
        }
        return Ok(match value {
            Value::Variant(inner) => {
                let (map_ty, map) = *inner;
                check_delta_map_type(&map_ty)?;
                Some(map)
            }
            _ => None,
        });
    }
    Ok(None)
}

/// Returns the name of a delta, as an advertisement keys it and a message
/// names it.
///
/// The name is the hex of the target commit for a delta from scratch, and
/// `<from>-<to>` for other deltas.
pub(crate) fn delta_name(from: Option<&Checksum>, to: &Checksum) -> String {
    match from {
        Some(from) => format!("{}-{}", from.to_hex(), to.to_hex()),
        None => to.to_hex(),
    }
}

/// Returns the source commit and the target commit of a delta name, the
/// inverse of [`delta_name`].
///
/// The name is `<to>` or `<from>-<to>`, and each checksum is 64 lowercase hex
/// characters. Each other name gives `None`.
pub(crate) fn parse_delta_name(name: &str) -> Option<(Option<Checksum>, Checksum)> {
    let hex = |s: &str| Checksum::from_hex_lower(s).ok();
    match name.split_once('-') {
        Some((from, to)) => Some((Some(hex(from)?), hex(to)?)),
        None => Some((None, hex(name)?)),
    }
}

/// Returns the fanout directory under [`DELTA_INDEXES_DIR`] and the file name
/// of the delta index of one target commit.
fn delta_index_parts(to: &Checksum) -> (String, String) {
    let b64 = to.to_base64_modified();
    let (fanout, rest) = b64.split_at(2);
    (fanout.to_owned(), format!("{rest}{INDEX_SUFFIX}"))
}

/// Returns the path of the delta index of one target commit, relative to the
/// repository root: `delta-indexes/<fanout>/<rest>.index`.
pub(crate) fn delta_index_relative_path(to: &Checksum) -> String {
    let (fanout, name) = delta_index_parts(to);
    format!("{DELTA_INDEXES_DIR}/{fanout}/{name}")
}

/// Returns the directory of a delta, relative to the repository root.
///
/// The path is `deltas/<fanout>/<rest>`. For a delta from scratch,
/// `<fanout><rest>` is the modified-base64 form of `to`. For a delta from
/// `from`, it is the modified-base64 form of `from`, then `-`, then the form
/// of `to`. The fanout is the first two characters. Modified base64 replaces
/// `/` with `_` and keeps `+`.
pub fn static_delta_relative_dir(from: Option<&Checksum>, to: &Checksum) -> String {
    delta_relative_dir(from, to)
}

/// Returns the directory of a delta, relative to the repository root, by the
/// rule of [`static_delta_relative_dir`].
///
/// A pull requests this path with the names as written. Modified base64
/// replaces `/` with `_` and keeps `+`, which is a path character, so no escape
/// is added here. The `ostree` command was observed to request these paths with
/// `+` unencoded.
pub(crate) fn delta_relative_dir(from: Option<&Checksum>, to: &Checksum) -> String {
    let to_b64 = to.to_base64_modified();
    match from {
        Some(from) => {
            let from_b64 = from.to_base64_modified();
            let (fanout, rest) = from_b64.split_at(2);
            format!("{DELTAS_DIR}/{fanout}/{rest}-{to_b64}")
        }
        None => {
            let (fanout, rest) = to_b64.split_at(2);
            format!("{DELTAS_DIR}/{fanout}/{rest}")
        }
    }
}

/// Returns the superblock timestamp: the explicit value, else the current time.
fn resolve_timestamp(explicit: Option<u64>) -> Result<u64> {
    match explicit {
        Some(timestamp) => Ok(timestamp),
        None => unix_seconds(),
    }
}

/// Writes a whole file into a delta directory atomically.
async fn write_delta_file(dir_fd: &OwnedFd, name: &str, bytes: &[u8], fsync: bool) -> Result<()> {
    let temp = TempFile::create(dir_fd, name);
    let fd = {
        let owned = dir_fd.try_clone()?;
        let temp = temp.name().to_owned();
        ostrya_rt::unblock(move || create_file_blocking(owned.as_fd(), &temp)).await?
    };
    let mut file = RtFile::from(fd);
    file.write_all(bytes).await.map_err(Error::Io)?;
    file.flush().await.map_err(Error::Io)?;
    drop(file);
    finish_file(dir_fd, temp.name(), name, fsync).await?;
    temp.keep();
    Ok(())
}

/// A guard on the temp name of a delta file.
///
/// A drop unlinks the file until the rename that puts the file in place
/// disarms the guard. Each code path from the creation of the temp file to its
/// rename holds a guard. A failed or cancelled write then leaves only the
/// finished files in the directory.
struct TempFile<'a> {
    dir: &'a OwnedFd,
    name: String,
    armed: bool,
}

impl<'a> TempFile<'a> {
    fn create(dir: &'a OwnedFd, name: &str) -> TempFile<'a> {
        TempFile {
            dir,
            name: temp_name(name),
            armed: true,
        }
    }

    fn name(&self) -> &str {
        &self.name
    }

    /// Disarms the guard, because the file is in place under its final name.
    fn keep(mut self) {
        self.armed = false;
    }
}

impl Drop for TempFile<'_> {
    /// The unlink runs on the thread that drops the guard, which can be an
    /// executor thread. It is one `unlinkat` on an open directory descriptor, a
    /// syscall that the write path also does inline.
    fn drop(&mut self) {
        if self.armed {
            let _ = rustix::fs::unlinkat(
                self.dir.as_fd(),
                self.name.as_str(),
                rustix::fs::AtFlags::empty(),
            );
        }
    }
}

/// Renames a new temp file to its final name, with a sync if `fsync` is set.
async fn finish_file(dir_fd: &OwnedFd, temp: &str, name: &str, fsync: bool) -> Result<()> {
    let owned = dir_fd.try_clone()?;
    let temp_owned = temp.to_owned();
    let name = name.to_owned();
    ostrya_rt::unblock(move || {
        use rustix::fs::{Mode, OFlags, openat, renameat};

        if fsync {
            let fd = openat(
                owned.as_fd(),
                temp_owned.as_str(),
                OFlags::RDONLY,
                Mode::empty(),
            )
            .map_err(|e| Error::Io(e.into()))?;
            rustix::fs::fdatasync(fd.as_fd()).map_err(|e| Error::Io(e.into()))?;
        }
        renameat(
            owned.as_fd(),
            temp_owned.as_str(),
            owned.as_fd(),
            name.as_str(),
        )
        .map_err(|e| Error::Io(e.into()))?;
        if fsync {
            rustix::fs::fsync(owned.as_fd()).map_err(|e| Error::Io(e.into()))?;
        }
        Ok(())
    })
    .await
}

/// Unlinks the superblock that a previous delta left at this location.
///
/// The unlink comes before the first part of the new delta overwrites the old
/// files. A reader trusts the part checksums of a superblock. If the old
/// superblock stayed while the parts change, an interrupted regeneration gives
/// a delta that fails its own checksum test. With the superblock gone first,
/// the directory reads as a delta that was never finished.
async fn remove_superblock(dir_fd: &OwnedFd) -> Result<()> {
    let owned = dir_fd.try_clone()?;
    ostrya_rt::unblock(move || {
        match rustix::fs::unlinkat(owned.as_fd(), SUPERBLOCK_FILE, rustix::fs::AtFlags::empty()) {
            Ok(()) | Err(rustix::io::Errno::NOENT) => Ok(()),
            Err(e) => Err(Error::Io(e.into())),
        }
    })
    .await
}

/// Removes the files that are not part of the finished delta.
///
/// The sweep removes these files:
///
/// - numbered part files at or past `count`, from a previous delta at the same
///   location with more parts
/// - temp files of a generation that was killed mid-write, at the age
///   [`TEMP_STALE_SECS`] or older
///
/// The delta directory then holds only its superblock and its numbered parts,
/// which is the layout of a delta directory. A temp file that is too young for
/// the sweep to call abandoned can also stay.
///
/// The pass matches by name, so it runs only over a delta directory of the
/// repository, where this code wrote each entry. An output directory of the
/// caller can hold any file. A file there named `0` or `.x.tmp-1-2` does not
/// belong to this delta.
async fn clean_delta_dir(dir_fd: &OwnedFd, count: usize) -> Result<()> {
    let owned = dir_fd.try_clone()?;
    let now = unix_seconds()?;
    ostrya_rt::unblock(move || {
        for name in dir_child_names(&owned)? {
            let remove = match name.parse::<usize>() {
                Ok(index) => index >= count,
                Err(_) => is_temp_name(&name) && temp_is_stale(owned.as_fd(), &name, now),
            };
            if !remove {
                continue;
            }
            match rustix::fs::unlinkat(owned.as_fd(), name.as_str(), rustix::fs::AtFlags::empty()) {
                Ok(()) | Err(rustix::io::Errno::NOENT) => {}
                Err(e) => return Err(Error::Io(e.into())),
            }
        }
        Ok(())
    })
    .await
}

/// The age at which the sweep removes a temp file.
///
/// A generation renames each of its own temp files into place before its
/// sweep. Each temp name that the sweep finds belongs to another run. It is an
/// abandoned leftover, or a file that a running generation still writes.
///
/// The process id in a temp name does not separate the two, because two
/// concurrent generations in one process share it. If the sweep unlinks a file
/// in progress, the rename of that run fails with `ENOENT`.
///
/// The age separates the two cases. A temp file lives much less than one hour
/// before its rename: a 200 MB part compresses in about ninety seconds. The
/// sweep then touches no file in progress.
const TEMP_STALE_SECS: u64 = 60 * 60;

/// Returns `true` if a temp file is old enough to be a leftover, and not a
/// write in progress. A file whose metadata cannot be read stays in place.
fn temp_is_stale(dir: BorrowedFd<'_>, name: &str, now: u64) -> bool {
    let Ok(stat) = rustix::fs::statat(dir, name, rustix::fs::AtFlags::SYMLINK_NOFOLLOW) else {
        return false;
    };
    let Ok(mtime) = u64::try_from(stat.st_mtime) else {
        return false;
    };
    now.saturating_sub(mtime) >= TEMP_STALE_SECS
}

/// Returns the current time in seconds since the Unix epoch.
fn unix_seconds() -> Result<u64> {
    let now = std::time::SystemTime::now()
        .duration_since(std::time::UNIX_EPOCH)
        .map_err(|_| Error::InvalidFormat("the system clock is before the Unix epoch".into()))?;
    Ok(now.as_secs())
}

/// Refuses a superblock past the ceiling of the read path.
///
/// The delta then fails at write time, and not later when it is applied or
/// signed.
fn check_superblock_size(len: usize) -> Result<()> {
    if len as u64 > MAX_SUPERBLOCK {
        return Err(Error::InvalidFormat(format!(
            "static delta superblock is {len} bytes, over the {MAX_SUPERBLOCK}-byte ceiling"
        )));
    }
    Ok(())
}

/// Refuses a part whose mode and xattr tables together pass the ceiling of the
/// read path.
///
/// A part that the reader of ostrya refuses then fails at write time.
fn check_table_size(len: usize) -> Result<()> {
    if len > MAX_TABLE_BYTES {
        return Err(Error::InvalidFormat(format!(
            "static delta part mode and xattr tables are {len} bytes, over the \
             {MAX_TABLE_BYTES}-byte ceiling"
        )));
    }
    Ok(())
}

/// Walks `delta-indexes/<fanout>/` and unlinks each index file whose
/// `<fanout>/<name>` is not in `written`.
///
/// A repository with no `delta-indexes/` needs no work.
fn prune_delta_indexes_blocking(repo_fd: BorrowedFd<'_>, written: &BTreeSet<String>) -> Result<()> {
    use rustix::fs::{AtFlags, Mode, OFlags, openat, unlinkat};

    let flags = OFlags::RDONLY | OFlags::DIRECTORY | OFlags::CLOEXEC;
    let indexes = match openat(repo_fd, DELTA_INDEXES_DIR, flags, Mode::empty()) {
        Ok(fd) => fd,
        Err(rustix::io::Errno::NOENT) => return Ok(()),
        Err(e) => return Err(Error::Io(e.into())),
    };

    for fanout in dir_child_names(&indexes)? {
        let fan_fd = openat(&indexes, fanout.as_str(), flags, Mode::empty())
            .map_err(|e| Error::Io(e.into()))?;
        for name in dir_child_names(&fan_fd)? {
            if !name.ends_with(INDEX_SUFFIX) || written.contains(&format!("{fanout}/{name}")) {
                continue;
            }
            unlinkat(&fan_fd, name.as_str(), AtFlags::empty()).map_err(|e| Error::Io(e.into()))?;
        }
    }
    Ok(())
}

/// Unlinks the index file at `relative` under the repository root.
///
/// An absent file, fanout directory, or `delta-indexes/` is no error. A
/// dangling symlink on the path also counts as absent. Each other failure is an
/// error, as it is for the `ostree` command. This includes a directory at the
/// path. It also includes a path component that is not a directory, such as a
/// regular file at the fanout or at `delta-indexes`.
fn remove_delta_index_blocking(repo_fd: BorrowedFd<'_>, relative: &str) -> Result<()> {
    use rustix::fs::{AtFlags, unlinkat};

    match unlinkat(repo_fd, relative, AtFlags::empty()) {
        Ok(()) | Err(rustix::io::Errno::NOENT) => Ok(()),
        Err(e) => Err(Error::Io(e.into())),
    }
}

/// Walks `delta-indexes/<fanout>/` and collects the target of each index file,
/// by the rule of [`Repo::list_static_delta_indexes`].
fn list_delta_indexes_blocking(repo_fd: BorrowedFd<'_>) -> Result<Vec<Checksum>> {
    use rustix::fs::{Dir, FileType, Mode, OFlags, openat};

    let flags = OFlags::RDONLY | OFlags::DIRECTORY | OFlags::CLOEXEC;
    let indexes = match openat(repo_fd, DELTA_INDEXES_DIR, flags, Mode::empty()) {
        Ok(fd) => fd,
        Err(rustix::io::Errno::NOENT) => return Ok(Vec::new()),
        Err(e) => return Err(Error::Io(e.into())),
    };
    let mut indexes = Dir::new(indexes).map_err(|e| Error::Io(e.into()))?;

    let mut out = Vec::new();
    for fanout in entries_of_type(&mut indexes, FileType::Directory)? {
        if fanout.len() != 2 {
            continue;
        }
        let fan_fd = openat(
            indexes.fd().map_err(|e| Error::Io(e.into()))?,
            fanout.as_slice(),
            flags | OFlags::NOFOLLOW,
            Mode::empty(),
        )
        .map_err(|e| Error::Io(e.into()))?;
        let mut fan_dir = Dir::new(fan_fd).map_err(|e| Error::Io(e.into()))?;
        for name in entries_of_type(&mut fan_dir, FileType::RegularFile)? {
            let Some(rest) = name.strip_suffix(INDEX_SUFFIX.as_bytes()) else {
                continue;
            };
            if rest.len() != 41 {
                continue;
            }
            let mut b64 = fanout.clone();
            b64.extend_from_slice(rest);
            let Ok(b64) = std::str::from_utf8(&b64) else {
                continue;
            };
            if let Ok(to) = Checksum::from_base64_modified(b64) {
                out.push(to);
            }
        }
    }
    out.sort();
    Ok(out)
}

/// Walks the index files back from `commits`, by the rule of
/// [`Repo::walk_delta_indexes`].
fn walk_delta_indexes_blocking(
    repo_fd: BorrowedFd<'_>,
    commits: Vec<Checksum>,
) -> Result<Vec<AdvertisedDelta>> {
    use rustix::fs::{Mode, OFlags, openat};

    let flags = OFlags::RDONLY | OFlags::DIRECTORY | OFlags::CLOEXEC;
    let indexes = match openat(repo_fd, DELTA_INDEXES_DIR, flags, Mode::empty()) {
        Ok(fd) => fd,
        Err(rustix::io::Errno::NOENT) => return Ok(Vec::new()),
        Err(e) => return Err(Error::Io(e.into())),
    };
    let mut seen: HashSet<Checksum> = HashSet::new();
    let mut queue: VecDeque<Checksum> = commits
        .into_iter()
        .filter(|commit| seen.insert(*commit))
        .collect();
    let mut found: BTreeMap<String, AdvertisedDelta> = BTreeMap::new();
    while let Some(to) = queue.pop_front() {
        let Some(bytes) = read_delta_index_blocking(indexes.as_fd(), &to)? else {
            continue;
        };
        let path = delta_index_relative_path(&to);
        let at_path = |e: Error| match e {
            Error::InvalidFormat(message) => Error::InvalidFormat(format!("{path}: {message}")),
            Error::Core(e) => Error::InvalidFormat(format!("{path}: {e}")),
            e => e,
        };
        // Each stage drops its input, so one index file is held in one form
        // at a time.
        let map = index_delta_map(&bytes).map_err(at_path)?;
        drop(bytes);
        let map = map.ok_or_else(|| {
            Error::InvalidFormat(format!("{path} holds no {STATIC_DELTAS_KEY} map"))
        })?;
        let deltas = parse_delta_map(&map).map_err(at_path)?;
        drop(map);
        for delta in deltas {
            let name = delta_name(delta.from.as_ref(), &delta.to);
            if delta.to != to {
                return Err(Error::InvalidFormat(format!(
                    "{path} lists static delta {name}, whose target is not {to}"
                )));
            }
            if let Some(from) = delta.from
                && seen.insert(from)
            {
                queue.push_back(from);
            }
            found.entry(name).or_insert(delta);
        }
    }
    Ok(found.into_values().collect())
}

/// Reads the index file of the target commit `to` in the open
/// `delta-indexes/` directory `indexes`, or returns `None` if the repository
/// holds no index file for it.
///
/// The rule is the rule of [`list_delta_indexes_blocking`]. The call follows
/// no symlink. A fanout that is not a directory, and an index path that is
/// not a regular file, count as no index file. The call reads the type of the
/// index path before the open, so it opens no socket, FIFO, or device node.
/// The open does not block. A second type check after the open gives `None`
/// for a file that another process puts at the path after the first check.
fn read_delta_index_blocking(indexes: BorrowedFd<'_>, to: &Checksum) -> Result<Option<Vec<u8>>> {
    use std::io::Read;

    use rustix::fs::{AtFlags, FileType, Mode, OFlags, fstat, openat, statat};
    use rustix::io::Errno;

    let (fanout, name) = delta_index_parts(to);
    let fan_fd = match openat(
        indexes,
        fanout.as_str(),
        OFlags::RDONLY | OFlags::DIRECTORY | OFlags::NOFOLLOW | OFlags::CLOEXEC,
        Mode::empty(),
    ) {
        Ok(fd) => fd,
        Err(Errno::NOENT | Errno::NOTDIR | Errno::LOOP) => return Ok(None),
        Err(e) => return Err(Error::Io(e.into())),
    };
    match statat(&fan_fd, name.as_str(), AtFlags::SYMLINK_NOFOLLOW) {
        Ok(stat) if FileType::from_raw_mode(stat.st_mode) == FileType::RegularFile => {}
        Ok(_) | Err(Errno::NOENT) => return Ok(None),
        Err(e) => return Err(Error::Io(e.into())),
    }
    let fd = match openat(
        &fan_fd,
        name.as_str(),
        OFlags::RDONLY | OFlags::NOFOLLOW | OFlags::NONBLOCK | OFlags::CLOEXEC,
        Mode::empty(),
    ) {
        Ok(fd) => fd,
        Err(Errno::NOENT | Errno::LOOP) => return Ok(None),
        Err(e) => return Err(Error::Io(e.into())),
    };
    let stat = fstat(&fd).map_err(|e| Error::Io(e.into()))?;
    if FileType::from_raw_mode(stat.st_mode) != FileType::RegularFile {
        return Ok(None);
    }
    let too_large = || {
        Error::InvalidFormat(format!(
            "{} is larger than the {MAX_METADATA_SIZE}-byte metadata ceiling",
            delta_index_relative_path(to)
        ))
    };
    let len = stat.st_size.max(0) as u64;
    if len > MAX_METADATA_SIZE {
        return Err(too_large());
    }
    let mut bytes = Vec::with_capacity(len as usize + 1);
    std::fs::File::from(fd)
        .take(MAX_METADATA_SIZE + 1)
        .read_to_end(&mut bytes)
        .map_err(Error::Io)?;
    if bytes.len() as u64 > MAX_METADATA_SIZE {
        return Err(too_large());
    }
    Ok(Some(bytes))
}

/// Returns the names of the entries of type `ty` in an open directory, with no
/// symlink followed.
///
/// If the directory reports no type for an entry, a `statat` reads the type.
fn entries_of_type(dir: &mut rustix::fs::Dir, ty: rustix::fs::FileType) -> Result<Vec<Vec<u8>>> {
    use rustix::fs::{AtFlags, FileType, statat};

    let mut out = Vec::new();
    while let Some(entry) = dir.next() {
        let entry = entry.map_err(|e| Error::Io(e.into()))?;
        let name = entry.file_name();
        if matches!(name.to_bytes(), b"." | b"..") {
            continue;
        }
        let entry_ty = match entry.file_type() {
            FileType::Unknown => match statat(
                dir.fd().map_err(|e| Error::Io(e.into()))?,
                name,
                AtFlags::SYMLINK_NOFOLLOW,
            ) {
                Ok(stat) => FileType::from_raw_mode(stat.st_mode),
                Err(rustix::io::Errno::NOENT) => continue,
                Err(e) => return Err(Error::Io(e.into())),
            },
            known => known,
        };
        if entry_ty == ty {
            out.push(name.to_bytes().to_vec());
        }
    }
    Ok(out)
}

/// Returns the temp name under which a delta file is written before its rename
/// into place.
fn temp_name(name: &str) -> String {
    format!(
        ".{name}.tmp-{}-{}",
        std::process::id(),
        crate::write::unique()
    )
}

/// Returns `true` if `name` is a temp name that [`temp_name`] makes.
fn is_temp_name(name: &str) -> bool {
    name.starts_with('.') && name.contains(".tmp-")
}

/// Creates a file for writing and replaces a leftover with the same name.
///
/// The call sets the mode to [`DELTA_FILE_MODE`] whatever the umask is, as the
/// `ostree` command does.
fn create_file_blocking(dir: BorrowedFd<'_>, name: &str) -> Result<OwnedFd> {
    use rustix::fs::{Mode, OFlags, fchmod, openat};

    let _ = rustix::fs::unlinkat(dir, name, rustix::fs::AtFlags::empty());
    let fd = openat(
        dir,
        name,
        OFlags::WRONLY | OFlags::CREATE | OFlags::EXCL | OFlags::CLOEXEC | OFlags::NOFOLLOW,
        Mode::from_raw_mode(DELTA_FILE_MODE),
    )
    .map_err(|e| Error::Io(e.into()))?;
    fchmod(&fd, Mode::from_raw_mode(DELTA_FILE_MODE)).map_err(|e| Error::Io(e.into()))?;
    Ok(fd)
}

/// Opens a directory relative to `base`, and creates each component that is
/// absent.
fn open_subdir_blocking(base: BorrowedFd<'_>, relative: &str) -> Result<OwnedFd> {
    use rustix::fs::{Mode, OFlags, mkdirat, openat};

    let mut current = base.try_clone_to_owned()?;
    for component in relative.split('/').filter(|part| !part.is_empty()) {
        match mkdirat(
            current.as_fd(),
            component,
            Mode::from_raw_mode(DELTA_DIR_MODE),
        ) {
            Ok(()) | Err(rustix::io::Errno::EXIST) => {}
            Err(e) => return Err(Error::Io(e.into())),
        }
        current = openat(
            current.as_fd(),
            component,
            OFlags::RDONLY | OFlags::DIRECTORY | OFlags::CLOEXEC,
            Mode::empty(),
        )
        .map_err(|e| Error::Io(e.into()))?;
    }
    Ok(current)
}

/// Creates a directory and its parents at an absolute or relative path, and
/// opens it.
///
/// The mode is [`DELTA_DIR_MODE`], the mode that the repository path gives to
/// `mkdirat`. An output directory then does not take its permissions from the
/// umask of the caller alone.
fn create_dir_path_blocking(path: &Path) -> Result<OwnedFd> {
    use std::os::unix::fs::DirBuilderExt;

    std::fs::DirBuilder::new()
        .recursive(true)
        .mode(DELTA_DIR_MODE)
        .create(path)
        .map_err(Error::Io)?;
    open_dir_blocking(path)
}

/// Opens the directory that holds a superblock file, and returns the
/// descriptor and the file name in it.
///
/// The directory is the parent of the path, or the working directory for a
/// path with no `/`. The call does not create it. A path whose last component
/// is empty, `.`, or `..` is refused with `EISDIR`, for example the empty path,
/// `/`, and `x/.`. A path that names a directory is also refused with `EISDIR`.
///
/// A part file name is refused, because the superblock replaces a part of that
/// name. A symlink at the path is not followed: the rename that puts the
/// superblock in place replaces the link.
fn open_superblock_parent_blocking(path: &Path) -> Result<(OwnedFd, String)> {
    use std::os::unix::ffi::OsStrExt;

    let is_dir = || Error::Io(rustix::io::Errno::ISDIR.into());
    // The last component comes from the bytes. `Path::file_name` skips a
    // trailing `.`, so it gives `x` for `x/.`.
    let bytes = path.as_os_str().as_bytes();
    let (parent, name) = match bytes.iter().rposition(|b| *b == b'/') {
        Some(slash) => (&bytes[..slash], &bytes[slash + 1..]),
        None => (&b""[..], bytes),
    };
    if matches!(name, b"" | b"." | b"..") {
        return Err(is_dir());
    }
    let name = std::str::from_utf8(name).map_err(|_| {
        Error::InvalidFormat(format!(
            "static delta superblock file name {} is not UTF-8",
            path.display()
        ))
    })?;
    if name
        .parse::<usize>()
        .is_ok_and(|index| index.to_string() == name)
    {
        return Err(Error::InvalidFormat(format!(
            "static delta superblock file name {name} is a part file name"
        )));
    }
    let parent = match parent {
        b"" if bytes.first() == Some(&b'/') => Path::new("/"),
        b"" => Path::new("."),
        parent => Path::new(std::ffi::OsStr::from_bytes(parent)),
    };
    let fd = open_dir_blocking(parent)?;
    match rustix::fs::statat(&fd, name, rustix::fs::AtFlags::SYMLINK_NOFOLLOW) {
        Ok(stat)
            if rustix::fs::FileType::from_raw_mode(stat.st_mode)
                == rustix::fs::FileType::Directory =>
        {
            return Err(is_dir());
        }
        Ok(_) | Err(rustix::io::Errno::NOENT) => {}
        Err(e) => return Err(Error::Io(e.into())),
    }
    Ok((fd, name.to_owned()))
}

/// Opens an existing directory by path.
fn open_dir_blocking(path: &Path) -> Result<OwnedFd> {
    use rustix::fs::{Mode, OFlags, open};

    open(
        path,
        OFlags::RDONLY | OFlags::DIRECTORY | OFlags::CLOEXEC,
        Mode::empty(),
    )
    .map_err(|e| Error::Io(e.into()))
}

/// Opens an existing directory by path, on the blocking pool.
async fn open_dir_path(path: &Path) -> Result<OwnedFd> {
    let path = path.to_owned();
    ostrya_rt::unblock(move || open_dir_blocking(&path)).await
}

/// Checks at compile time that `DeltaOptions` and `DeltaSuperblock` are `Send`
/// and `Sync`.
const _: fn() = || {
    fn assert_send_sync<T: Send + Sync>() {}
    assert_send_sync::<DeltaOptions>();
    assert_send_sync::<crate::delta::DeltaSuperblock>();
};

#[cfg(test)]
mod tests {
    use super::*;

    fn checksum(byte: u8) -> Checksum {
        Checksum::from_bytes([byte; 32])
    }

    /// Checks the two path shapes against the paths that the `ostree` command
    /// was observed to write and to request. A pull requests the same names on
    /// the wire.
    #[test]
    fn delta_paths_take_the_base64_fanout() {
        let to = checksum(0x11);
        let from = checksum(0x22);
        let to_b64 = to.to_base64_modified();
        let from_b64 = from.to_base64_modified();

        assert_eq!(
            delta_relative_dir(None, &to),
            format!("deltas/{}/{}", &to_b64[..2], &to_b64[2..])
        );
        assert_eq!(
            delta_relative_dir(Some(&from), &to),
            format!("deltas/{}/{}-{to_b64}", &from_b64[..2], &from_b64[2..])
        );
        assert_eq!(
            delta_index_relative_path(&to),
            format!("delta-indexes/{}/{}.index", &to_b64[..2], &to_b64[2..])
        );
    }

    /// A delta is keyed by hex in the advertisement, whichever shape it has.
    #[test]
    fn delta_names_are_hex() {
        let to = checksum(0x11);
        let from = checksum(0x22);
        assert_eq!(delta_name(None, &to), to.to_hex());
        assert_eq!(
            delta_name(Some(&from), &to),
            format!("{}-{}", from.to_hex(), to.to_hex())
        );
    }

    /// The parse of a delta name gives back the commits of each shape, and
    /// gives `None` for each name that [`delta_name`] does not write.
    #[test]
    fn delta_names_parse_back_to_their_commits() {
        let to = checksum(0xab);
        let from = checksum(0xcd);
        assert_eq!(parse_delta_name(&delta_name(None, &to)), Some((None, to)));
        assert_eq!(
            parse_delta_name(&delta_name(Some(&from), &to)),
            Some((Some(from), to))
        );

        let to_hex = to.to_hex();
        let from_hex = from.to_hex();
        for name in [
            String::new(),
            "zz".to_owned(),
            to_hex.to_uppercase(),
            format!("{}-{to_hex}", from_hex.to_uppercase()),
            to_hex[1..].to_owned(),
            format!("{}-{to_hex}", &from_hex[1..]),
            format!("{from_hex}-"),
            format!("-{to_hex}"),
            format!("{from_hex}-{to_hex}-{to_hex}"),
        ] {
            assert_eq!(parse_delta_name(&name), None, "{name}");
        }
    }

    /// The map of an index file reads back with no variant wrapper. A dict
    /// with no delta map gives `None`, and bytes that are not an `a{sv}` are
    /// refused.
    #[test]
    fn the_index_map_reads_back() {
        let deltas = BTreeMap::from([
            (delta_name(None, &checksum(1)), checksum(2)),
            (delta_name(Some(&checksum(3)), &checksum(1)), checksum(4)),
        ]);
        let map = index_delta_map(&index_value(&deltas).unwrap())
            .unwrap()
            .expect("the index holds the map");
        let expected =
            delta_map_value(deltas.iter().map(|(name, digest)| (name.as_str(), digest))).unwrap();
        assert_eq!(map, expected);

        let ty = Type::parse("a{sv}").unwrap();
        let mut dict = Value::Array(Vec::new());
        append_dict_entry(
            &mut dict,
            "other",
            Value::variant(Type::parse("b").unwrap(), Value::Bool(true)),
        )
        .unwrap();
        let bytes = to_bytes(&ty, &dict).unwrap();
        assert_eq!(index_delta_map(&bytes).unwrap(), None);
        assert!(matches!(
            index_delta_map(b"\x01\x02\x03"),
            Err(Error::Core(_))
        ));
    }

    #[test]
    fn the_superblock_ceiling_names_the_size_it_rejects() {
        let over = MAX_SUPERBLOCK as usize + 1;
        check_superblock_size(MAX_SUPERBLOCK as usize).unwrap();
        let err = check_superblock_size(over).unwrap_err();
        let Error::InvalidFormat(message) = err else {
            panic!("an oversized superblock must be an InvalidFormat error: {err}");
        };
        assert!(
            message.contains(&over.to_string()),
            "the error does not name the size: {message}"
        );
    }

    #[test]
    fn the_table_ceiling_names_the_size_it_rejects() {
        check_table_size(MAX_TABLE_BYTES).unwrap();
        let over = MAX_TABLE_BYTES + 1;
        let err = check_table_size(over).unwrap_err();
        let Error::InvalidFormat(message) = err else {
            panic!("oversized part tables must be an InvalidFormat error: {err}");
        };
        assert!(
            message.contains(&over.to_string()),
            "the error does not name the size: {message}"
        );
    }

    #[test]
    fn a_patch_of_unrelated_content_loses_to_splicing() {
        // A diff against unrelated content is nonzero except where the two
        // bytes agree. The bound must reject this shape. It counts just under
        // the output size, so a plain `< output_size` comparison keeps it.
        let output_size = 4_096u64;
        let stream: Vec<u8> = (0..output_size)
            .map(|i| if i % 256 == 0 { 0 } else { 0xa5 })
            .collect();
        assert!(novel_bytes(&stream) < output_size);
        assert!(
            !patch_beats_splicing(&stream, output_size),
            "a patch counting {} novel bytes of {output_size} was kept",
            novel_bytes(&stream)
        );
    }

    #[test]
    fn a_patch_of_a_small_edit_beats_splicing() {
        // A diff against a near-identical source is zero except at the edit,
        // which stays far inside the bound at any object size.
        let mut stream = vec![0u8; 4_096];
        stream[100] = 0x01;
        stream[101] = 0x02;
        assert!(patch_beats_splicing(&stream, 4_096));
    }

    #[test]
    fn an_empty_target_never_keeps_a_patch() {
        assert!(!patch_beats_splicing(&[], 0));
    }

    /// A scratch directory for a test that needs real descriptors.
    fn scratch(tag: &str) -> PathBuf {
        let dir = std::env::temp_dir().join(format!(
            "ostrya-deltagen-{tag}-{}-{}",
            std::process::id(),
            crate::write::unique()
        ));
        std::fs::create_dir_all(&dir).unwrap();
        dir
    }

    /// A write to the data source of a part that the async file defers to its
    /// next flush must fail the handover to the blocking side.
    ///
    /// The framing offsets of the part come from the length that the spill
    /// counted. Without this check, a data source that lost bytes compresses
    /// into a part that verifies its own checksum and fails only when the
    /// delta is applied.
    #[test]
    fn a_deferred_spill_write_error_fails_the_handover() {
        let dir = scratch("spill-error");
        let path = dir.join("target");
        std::fs::write(&path, b"").unwrap();
        // A read-only descriptor, so the write to it fails with EBADF. The
        // async file writes on a background task, so `write_all` accepts the
        // bytes, and the error comes at the flush.
        let file = std::fs::File::open(&path).unwrap();
        let spill_dir = std::fs::File::open(&dir).unwrap();

        let outcome = ostrya_rt::block_on(async {
            let mut spill = Spill::File {
                file: RtFile::from(file),
                len: 0,
            };
            spill
                .append(b"data source bytes", spill_dir.as_fd())
                .await?;
            spill.into_blocking().await.map(|_| ())
        });
        let _ = std::fs::remove_dir_all(&dir);
        outcome.expect_err("a failed spill write must fail the handover");
    }

    /// A data source with fewer bytes than the length that the framing counts
    /// fails the part. No payload is made whose two byte arrays disagree with
    /// their offsets.
    #[test]
    fn a_short_data_source_fails_the_part() {
        let dir = scratch("short-source");
        let path = dir.join("source");
        std::fs::write(&path, b"seven!!").unwrap();
        let file = std::fs::File::open(&path).unwrap();

        let outcome = ostrya_rt::block_on(async {
            let spill = BlockingSpill::File { file, len: 9 };
            let mut sink: Vec<u8> = Vec::new();
            spill.write_into(&mut sink).await
        });
        let _ = std::fs::remove_dir_all(&dir);

        let err = outcome.expect_err("a short data source must fail the part");
        let Error::InvalidFormat(message) = err else {
            panic!("a short data source must be an InvalidFormat error: {err}");
        };
        assert!(
            message.contains('7') && message.contains('9'),
            "the error does not name both lengths: {message}"
        );
    }

    /// A part whose write fails after its temp file exists leaves the delta
    /// directory as it was. An interrupted generation then leaves no partial
    /// part beside the finished files.
    ///
    /// The failure is the count check of [`BlockingSpill::write_into`] in
    /// [`compress_part`]. The test gives the part a data source whose recorded
    /// length is more than the file length.
    #[test]
    fn a_failed_part_write_leaves_no_temp_file_behind() {
        let root = scratch("part-temp-leak");
        let path = root.join("source");
        std::fs::write(&path, b"seven!!").unwrap();
        let dir = root.join("delta");
        std::fs::create_dir(&dir).unwrap();
        let dir_fd = open_dir_blocking(&dir).unwrap();

        let outcome = ostrya_rt::block_on(async {
            let mut part = Part {
                blob: Spill::File {
                    file: RtFile::from(std::fs::File::open(&path).unwrap()),
                    len: 9,
                },
                ..Part::default()
            };
            part.finish_object(ObjectType::File, Checksum::sha256(b"x"), 9);
            write_part(&mut PartTarget::File(&dir_fd), 0, part, false).await
        });
        let leftovers = dir_child_names(&dir_fd).unwrap();
        let _ = std::fs::remove_dir_all(&root);

        assert!(outcome.is_err(), "a short data source must fail the part");
        assert!(
            leftovers.is_empty(),
            "the failed part left files behind: {leftovers:?}"
        );
    }

    /// Returns `(repo, commit)` for a repository at `root/repo` with one
    /// commit of `files` files, each of `size` bytes of noise.
    async fn noise_commit(root: &Path, files: usize, size: usize) -> (Repo, Checksum) {
        use crate::{
            CommitModifier, CommitModifierFlags, CommitOptions, CreateOptions, MutableTree,
        };

        let tree = root.join("tree");
        std::fs::create_dir_all(&tree).unwrap();
        let mut state = 0x2545_f491_4f6c_dd1du64;
        for i in 0..files {
            let bytes: Vec<u8> = (0..size)
                .map(|_| {
                    state ^= state << 13;
                    state ^= state >> 7;
                    state ^= state << 17;
                    state as u8
                })
                .collect();
            std::fs::write(tree.join(format!("f{i}")), bytes).unwrap();
        }
        let repo = Repo::create(
            &root.join("repo"),
            CreateOptions::new(ostrya_core::RepoMode::Archive),
        )
        .await
        .unwrap();
        let txn = repo.transaction().await.unwrap();
        let dfd = std::fs::File::open(&tree).unwrap();
        let mut modifier = Some(CommitModifier::new(
            CommitModifierFlags::CANONICAL_PERMISSIONS | CommitModifierFlags::SKIP_XATTRS,
        ));
        let mut mtree = MutableTree::new();
        txn.write_dfd_to_mtree(dfd.as_fd(), Path::new("."), &mut mtree, modifier.as_mut())
            .await
            .unwrap();
        let root_tree = txn.write_mtree(&mut mtree).await.unwrap();
        let commit = txn
            .write_commit(
                CommitOptions {
                    timestamp: Some(1_700_000_000),
                    ..CommitOptions::default()
                },
                &root_tree,
            )
            .await
            .unwrap();
        txn.commit().await.unwrap();
        (repo, commit)
    }

    /// An inline generation whose parts pass the superblock ceiling is
    /// refused, and it changes nothing. The earlier delta at the repository
    /// location keeps its superblock and its part files and stays listed. A
    /// new output directory receives no file.
    #[test]
    fn an_inline_generation_over_the_ceiling_keeps_the_earlier_delta() {
        let root = scratch("inline-ceiling");
        let out = root.join("out");
        std::fs::create_dir_all(&out).unwrap();
        let outcome = ostrya_rt::block_on(async {
            let (repo, commit) = noise_commit(&root, 3, 200_000).await;
            let files = DeltaOptions {
                timestamp: Some(1_700_000_000),
                min_fallback_size: 0,
                max_chunk_size: 150_000,
                ..DeltaOptions::default()
            };
            let relative = repo
                .generate_static_delta(None, &commit, &files)
                .await
                .unwrap();
            let delta = root.join("repo").join(&relative);
            let before = dir_child_names(&open_dir_blocking(&delta).unwrap()).unwrap();
            let superblock = std::fs::read(delta.join(SUPERBLOCK_FILE)).unwrap();
            let listed = repo.list_static_deltas().await.unwrap();
            assert_eq!(listed.len(), 1);

            let inline = DeltaOptions {
                inline: true,
                ..files
            };
            let err = repo
                .generate_static_delta_under(None, &commit, &inline, 100_000)
                .await
                .unwrap_err();
            let Error::InvalidFormat(message) = err else {
                panic!("the ceiling refusal is an invalid-format error: {err}");
            };
            assert!(message.contains("superblock ceiling"), "{message}");
            let mut after = dir_child_names(&open_dir_blocking(&delta).unwrap()).unwrap();
            let mut before = before;
            before.sort();
            after.sort();
            assert_eq!(after, before);
            assert_eq!(
                std::fs::read(delta.join(SUPERBLOCK_FILE)).unwrap(),
                superblock
            );
            assert_eq!(repo.list_static_deltas().await.unwrap(), listed);

            let err = repo
                .generate_static_delta_under(
                    None,
                    &commit,
                    &DeltaOptions {
                        output_dir: Some(out.clone()),
                        ..inline
                    },
                    100_000,
                )
                .await
                .unwrap_err();
            assert!(matches!(err, Error::InvalidFormat(_)), "{err}");
            dir_child_names(&open_dir_blocking(&out).unwrap()).unwrap()
        });
        let _ = std::fs::remove_dir_all(&root);
        assert!(outcome.is_empty(), "{outcome:?}");
    }

    /// The inline part buffer takes bytes up to its limit and refuses the
    /// first byte past it. The part write reports the superblock ceiling where
    /// the buffer filled, so the test checks the ceiling without a 128 MiB
    /// part.
    #[test]
    fn the_capped_part_buffer_refuses_past_its_limit() {
        use std::io::Write;

        let mut capped = CappedBuf::new(4);
        assert_eq!(capped.write(b"abc").unwrap(), 3);
        assert_eq!(capped.write(b"de").unwrap(), 1);
        assert!(capped.write(b"f").is_err());
        assert_eq!(capped.write(b"").unwrap(), 0);
        assert_eq!(capped.len(), 4);
        assert_eq!(capped.into_parts(), (b'a', b"bcd".to_vec()));

        let root = scratch("capped-part");
        let tmp = open_dir_blocking(&root).unwrap();
        let outcome = ostrya_rt::block_on(async {
            let mut part = Part::default();
            part.blob.append(&[7u8; 4096], tmp.as_fd()).await?;
            part.finish_object(ObjectType::File, Checksum::sha256(b"x"), 4096);
            write_part(&mut PartTarget::Inline { budget: 16 }, 0, part, false).await
        });
        let Err(Error::InvalidFormat(message)) = outcome else {
            panic!("an inline part past the budget must be refused");
        };
        assert!(message.contains("superblock ceiling"), "{message}");

        let mut budget = PartTarget::Inline { budget: 1 << 20 };
        let entry = ostrya_rt::block_on(async {
            let mut part = Part::default();
            part.blob.append(&[7u8; 4096], tmp.as_fd()).await?;
            part.finish_object(ObjectType::File, Checksum::sha256(b"x"), 4096);
            write_part(&mut budget, 0, part, false).await
        })
        .unwrap();
        let (compression, body) = entry.inline.expect("an inline part keeps its bytes");
        let mut bytes = vec![compression];
        bytes.extend_from_slice(&body);
        assert_eq!(bytes.len() as u64, entry.size);
        assert_eq!(compression, COMPRESSION_XZ);
        assert_eq!(Checksum::sha256(&bytes), entry.checksum);
        let PartTarget::Inline { budget } = budget else {
            unreachable!();
        };
        assert_eq!(budget, (1 << 20) - bytes.len());
        let _ = std::fs::remove_dir_all(&root);
    }

    /// Returns an empty `archive` repository at `root/repo`.
    async fn empty_repo(root: &Path) -> Repo {
        Repo::create(
            &root.join("repo"),
            crate::CreateOptions::new(ostrya_core::RepoMode::Archive),
        )
        .await
        .unwrap()
    }

    /// Returns the delta from `from` to `to`, with a superblock digest that
    /// its name gives.
    fn advertised(from: Option<Checksum>, to: Checksum) -> AdvertisedDelta {
        AdvertisedDelta {
            from,
            to,
            superblock: Checksum::sha256(delta_name(from.as_ref(), &to).as_bytes()),
        }
    }

    /// Writes the index file of `to` into the repository at `repo_dir`. The
    /// file lists `deltas`, each with the digest of [`advertised`].
    fn write_index(repo_dir: &Path, to: &Checksum, deltas: &[AdvertisedDelta]) {
        let map = deltas
            .iter()
            .map(|delta| (delta_name(delta.from.as_ref(), &delta.to), delta.superblock))
            .collect();
        let path = repo_dir.join(delta_index_relative_path(to));
        std::fs::create_dir_all(path.parent().unwrap()).unwrap();
        std::fs::write(&path, index_value(&map).unwrap()).unwrap();
    }

    /// Writes the index file of `to` into the repository at `repo_dir`. The
    /// file holds `map` under the delta map key, in a variant of type `ty`.
    fn write_index_map(repo_dir: &Path, to: &Checksum, ty: &str, map: Value) {
        let mut dict = Value::Array(Vec::new());
        append_dict_entry(
            &mut dict,
            STATIC_DELTAS_KEY,
            Value::variant(Type::parse(ty).unwrap(), map),
        )
        .unwrap();
        let path = repo_dir.join(delta_index_relative_path(to));
        std::fs::create_dir_all(path.parent().unwrap()).unwrap();
        let bytes = to_bytes(&Type::parse("a{sv}").unwrap(), &dict).unwrap();
        std::fs::write(&path, bytes).unwrap();
    }

    /// Returns `deltas` sorted by delta name, the order of the walk.
    fn by_name(mut deltas: Vec<AdvertisedDelta>) -> Vec<AdvertisedDelta> {
        deltas.sort_by_key(|delta| delta_name(delta.from.as_ref(), &delta.to));
        deltas
    }

    /// The walk follows the source commit of each delta back to the start of
    /// the chain.
    #[test]
    fn the_walk_follows_a_chain() {
        let root = scratch("walk-chain");
        let [a, b, c, d] = [
            checksum(0xa1),
            checksum(0xb2),
            checksum(0xc3),
            checksum(0xd4),
        ];
        let outcome = ostrya_rt::block_on(async {
            let repo = empty_repo(&root).await;
            let repo_dir = root.join("repo");
            write_index(&repo_dir, &b, &[advertised(Some(a), b)]);
            write_index(&repo_dir, &c, &[advertised(Some(b), c)]);
            write_index(&repo_dir, &d, &[advertised(Some(c), d)]);
            repo.walk_delta_indexes([d]).await
        });
        let _ = std::fs::remove_dir_all(&root);
        assert_eq!(
            outcome.unwrap(),
            by_name(vec![
                advertised(Some(a), b),
                advertised(Some(b), c),
                advertised(Some(c), d),
            ])
        );
    }

    /// A cycle of deltas ends the walk, because the walk reads the index of
    /// each commit one time.
    #[test]
    fn the_walk_ends_on_a_cycle() {
        let root = scratch("walk-cycle");
        let [a, b] = [checksum(0xa1), checksum(0xb2)];
        let outcome = ostrya_rt::block_on(async {
            let repo = empty_repo(&root).await;
            let repo_dir = root.join("repo");
            write_index(
                &repo_dir,
                &a,
                &[advertised(Some(b), a), advertised(Some(a), a)],
            );
            write_index(&repo_dir, &b, &[advertised(Some(a), b)]);
            repo.walk_delta_indexes([a, a]).await
        });
        let _ = std::fs::remove_dir_all(&root);
        assert_eq!(
            outcome.unwrap(),
            by_name(vec![
                advertised(Some(b), a),
                advertised(Some(a), a),
                advertised(Some(a), b),
            ])
        );
    }

    /// If an index file lists a delta name two times, the first entry
    /// applies, as in a pull.
    #[test]
    fn the_walk_takes_the_first_entry_of_a_duplicate_name() {
        let root = scratch("walk-duplicate");
        let [a, t] = [checksum(0xa1), checksum(0xf0)];
        let name = delta_name(Some(&a), &t);
        let outcome = ostrya_rt::block_on(async {
            let repo = empty_repo(&root).await;
            let map = delta_map_value(
                [(name.as_str(), &checksum(1)), (name.as_str(), &checksum(2))].into_iter(),
            )
            .unwrap();
            write_index_map(&root.join("repo"), &t, "a{sv}", map);
            repo.walk_delta_indexes([t]).await
        });
        let _ = std::fs::remove_dir_all(&root);
        assert_eq!(
            outcome.unwrap(),
            [AdvertisedDelta {
                from: Some(a),
                to: t,
                superblock: checksum(1),
            }]
        );
    }

    /// The walk reads `delta-indexes/` alone. A delta under `deltas/` that no
    /// index lists adds nothing, and a `deltas` entry that is not a directory
    /// changes nothing. A `deltas` directory that grants no access changes
    /// nothing too, because an open under it would fail.
    #[test]
    fn the_walk_reads_no_deltas_dir() {
        let root = scratch("walk-no-deltas");
        let target = checksum(0x77);
        ostrya_rt::block_on(async {
            let (repo, commit) = noise_commit(&root, 1, 64).await;
            let repo_dir = root.join("repo");
            let opts = DeltaOptions {
                timestamp: Some(1_700_000_000),
                ..DeltaOptions::default()
            };
            repo.generate_static_delta(None, &commit, &opts)
                .await
                .unwrap();
            assert!(repo_dir.join(DELTAS_DIR).is_dir());
            assert_eq!(repo.walk_delta_indexes([commit]).await.unwrap(), []);

            let listed = advertised(Some(commit), target);
            write_index(&repo_dir, &target, &[listed]);
            assert_eq!(repo.walk_delta_indexes([target]).await.unwrap(), [listed]);

            std::fs::remove_dir_all(repo_dir.join(DELTAS_DIR)).unwrap();
            std::fs::write(repo_dir.join(DELTAS_DIR), b"not a directory").unwrap();
            assert_eq!(repo.walk_delta_indexes([target]).await.unwrap(), [listed]);

            // Root passes the mode check, so this step runs for other users
            // alone.
            std::fs::remove_file(repo_dir.join(DELTAS_DIR)).unwrap();
            if !rustix::process::geteuid().is_root() {
                use std::os::unix::fs::PermissionsExt;

                let deltas_dir = repo_dir.join(DELTAS_DIR);
                std::fs::create_dir(&deltas_dir).unwrap();
                std::fs::set_permissions(&deltas_dir, std::fs::Permissions::from_mode(0o000))
                    .unwrap();
                let outcome = repo.walk_delta_indexes([target]).await;
                std::fs::set_permissions(&deltas_dir, std::fs::Permissions::from_mode(0o755))
                    .unwrap();
                assert_eq!(outcome.unwrap(), [listed]);
            }
        });
        let _ = std::fs::remove_dir_all(&root);
    }

    /// An index entry whose target is not the indexed commit fails the walk,
    /// and the error names the index file and the delta.
    #[test]
    fn the_walk_refuses_an_entry_to_another_commit() {
        let root = scratch("walk-other-target");
        let [a, b, c] = [checksum(0xa1), checksum(0xb2), checksum(0xc3)];
        let outcome = ostrya_rt::block_on(async {
            let repo = empty_repo(&root).await;
            write_index(&root.join("repo"), &c, &[advertised(Some(a), b)]);
            repo.walk_delta_indexes([c]).await
        });
        let _ = std::fs::remove_dir_all(&root);
        let Err(Error::InvalidFormat(message)) = outcome else {
            panic!("an entry to another commit must be refused: {outcome:?}");
        };
        assert!(
            message.contains(&delta_index_relative_path(&c))
                && message.contains(&delta_name(Some(&a), &b)),
            "{message}"
        );
    }

    /// An index file with no delta map, with a key that is not a delta name,
    /// with a map of type `a(sv)`, or with bytes that do not decode fails the
    /// walk. The error of the last two names the index file.
    #[test]
    fn the_walk_refuses_an_index_with_no_valid_map() {
        let root = scratch("walk-no-map");
        let [a, b, c, d] = [
            checksum(0xa1),
            checksum(0xb2),
            checksum(0xc3),
            checksum(0xd4),
        ];
        let outcomes = ostrya_rt::block_on(async {
            let repo = empty_repo(&root).await;
            let repo_dir = root.join("repo");
            let ty = Type::parse("a{sv}").unwrap();
            let mut dict = Value::Array(Vec::new());
            append_dict_entry(
                &mut dict,
                "other",
                Value::variant(Type::parse("b").unwrap(), Value::Bool(true)),
            )
            .unwrap();
            let path = repo_dir.join(delta_index_relative_path(&a));
            std::fs::create_dir_all(path.parent().unwrap()).unwrap();
            std::fs::write(&path, to_bytes(&ty, &dict).unwrap()).unwrap();
            let no_map = repo.walk_delta_indexes([a]).await;

            let map = BTreeMap::from([("zz".to_owned(), checksum(1))]);
            let path = repo_dir.join(delta_index_relative_path(&b));
            std::fs::create_dir_all(path.parent().unwrap()).unwrap();
            std::fs::write(&path, index_value(&map).unwrap()).unwrap();
            let bad_key = repo.walk_delta_indexes([b]).await;

            let map = delta_map_value([(delta_name(None, &c).as_str(), &checksum(1))].into_iter())
                .unwrap();
            write_index_map(&repo_dir, &c, "a(sv)", map);
            let bad_type = repo.walk_delta_indexes([c]).await;

            let path = repo_dir.join(delta_index_relative_path(&d));
            std::fs::create_dir_all(path.parent().unwrap()).unwrap();
            std::fs::write(&path, b"\x01\x02\x03").unwrap();
            let undecoded = repo.walk_delta_indexes([d]).await;
            [no_map, bad_key, bad_type, undecoded]
        });
        let _ = std::fs::remove_dir_all(&root);
        for (index, outcome) in outcomes.into_iter().enumerate() {
            let Err(Error::InvalidFormat(message)) = outcome else {
                panic!("case {index} must be refused: {outcome:?}");
            };
            if index >= 2 {
                let named = delta_index_relative_path(&[a, b, c, d][index]);
                assert!(message.contains(&named), "{message}");
            }
        }
    }

    /// An index file past the metadata ceiling fails the walk before a read.
    /// The file is sparse, so the test writes no data.
    #[test]
    fn the_walk_refuses_an_index_past_the_ceiling() {
        let root = scratch("walk-oversize");
        let a = checksum(0xa1);
        let outcome = ostrya_rt::block_on(async {
            let repo = empty_repo(&root).await;
            let path = root.join("repo").join(delta_index_relative_path(&a));
            std::fs::create_dir_all(path.parent().unwrap()).unwrap();
            std::fs::File::create(&path)
                .unwrap()
                .set_len(MAX_METADATA_SIZE + 1)
                .unwrap();
            repo.walk_delta_indexes([a]).await
        });
        let _ = std::fs::remove_dir_all(&root);
        assert!(
            matches!(outcome, Err(Error::InvalidFormat(_))),
            "{outcome:?}"
        );
    }

    /// A symlink, a FIFO, a socket, or a directory at the index path counts as
    /// no index file, and so does a symlinked fanout. The FIFO does not stop
    /// the walk.
    #[test]
    fn the_walk_skips_an_index_that_is_not_a_regular_file() {
        use rustix::fs::{FileType, Mode};

        let root = scratch("walk-not-regular");
        let a = checksum(0xa1);
        let (fanout, name) = delta_index_parts(&a);
        let outcomes = ostrya_rt::block_on(async {
            let repo = empty_repo(&root).await;
            let repo_dir = root.join("repo");
            let path = repo_dir.join(delta_index_relative_path(&a));
            let fan_dir = path.parent().unwrap().to_owned();
            let elsewhere = root.join("elsewhere.index");
            let map = BTreeMap::from([(delta_name(Some(&a), &a), checksum(1))]);
            std::fs::write(&elsewhere, index_value(&map).unwrap()).unwrap();
            std::fs::create_dir_all(&fan_dir).unwrap();
            let mut outcomes = Vec::new();

            std::os::unix::fs::symlink(&elsewhere, &path).unwrap();
            outcomes.push(("symlink", repo.walk_delta_indexes([a]).await));
            std::fs::remove_file(&path).unwrap();

            rustix::fs::mknodat(
                rustix::fs::CWD,
                &path,
                FileType::Fifo,
                Mode::from_raw_mode(0o600),
                0,
            )
            .unwrap();
            outcomes.push(("fifo", repo.walk_delta_indexes([a]).await));
            std::fs::remove_file(&path).unwrap();

            // The path of a socket has a short length limit, so the bind uses
            // a short path and a rename moves the socket into place.
            let short = root.join("sock");
            let listener = std::os::unix::net::UnixListener::bind(&short).unwrap();
            std::fs::rename(&short, &path).unwrap();
            outcomes.push(("socket", repo.walk_delta_indexes([a]).await));
            drop(listener);
            std::fs::remove_file(&path).unwrap();

            std::fs::create_dir(&path).unwrap();
            outcomes.push(("directory", repo.walk_delta_indexes([a]).await));
            std::fs::remove_dir(&path).unwrap();

            let real = repo_dir.join(DELTA_INDEXES_DIR).join("real");
            std::fs::create_dir(&real).unwrap();
            std::fs::copy(&elsewhere, real.join(&name)).unwrap();
            std::fs::remove_dir(&fan_dir).unwrap();
            std::os::unix::fs::symlink("real", repo_dir.join(DELTA_INDEXES_DIR).join(&fanout))
                .unwrap();
            outcomes.push(("symlinked fanout", repo.walk_delta_indexes([a]).await));
            outcomes
        });
        let _ = std::fs::remove_dir_all(&root);
        for (case, outcome) in outcomes {
            assert_eq!(outcome.unwrap(), [], "{case}");
        }
    }

    #[test]
    fn temp_names_are_recognized_and_part_names_are_not() {
        assert!(is_temp_name(&temp_name("0")));
        assert!(is_temp_name(&temp_name(SUPERBLOCK_FILE)));
        assert!(!is_temp_name("0"));
        assert!(!is_temp_name(SUPERBLOCK_FILE));
    }

    /// The composefs image is a fallback at the fallback size or larger, past
    /// the chunk size, past 128 MiB also when fallbacks are off and the chunk
    /// size is larger, and in each inline delta.
    #[test]
    fn the_image_selection_bounds_the_image_in_a_part() {
        let opts = |min_fallback_size, max_chunk_size, inline| DeltaOptions {
            min_fallback_size,
            max_chunk_size,
            inline,
            ..DeltaOptions::default()
        };
        let off = opts(0, u64::MAX, false);
        assert!(!image_is_fallback(MAX_PART_IMAGE, &off));
        assert!(image_is_fallback(MAX_PART_IMAGE + 1, &off));

        let chunk = opts(0, 1000, false);
        assert!(!image_is_fallback(1000, &chunk));
        assert!(image_is_fallback(1001, &chunk));

        let threshold = opts(1000, u64::MAX, false);
        assert!(!image_is_fallback(999, &threshold));
        assert!(image_is_fallback(1000, &threshold));

        assert!(image_is_fallback(1, &opts(0, u64::MAX, true)));
        assert!(!image_is_fallback(1, &DeltaOptions::default()));
    }
}