package tezos-store

  1. Overview
  2. Docs
Legend:
Page
Library
Module
Module type
Parameter
Class
Class type
Source

Source file snapshots.ml

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583
2584
2585
2586
2587
2588
2589
2590
2591
2592
2593
2594
2595
2596
2597
2598
2599
2600
2601
2602
2603
2604
2605
2606
2607
2608
2609
2610
2611
2612
2613
2614
2615
2616
2617
2618
2619
2620
2621
2622
2623
2624
2625
2626
2627
2628
2629
2630
2631
2632
2633
2634
2635
2636
2637
2638
2639
2640
2641
2642
2643
2644
2645
2646
2647
2648
2649
2650
2651
2652
2653
2654
2655
2656
2657
2658
2659
2660
2661
2662
2663
2664
2665
2666
2667
2668
2669
2670
2671
2672
2673
2674
2675
2676
2677
2678
2679
2680
2681
2682
2683
2684
2685
2686
2687
2688
2689
2690
2691
2692
2693
2694
2695
2696
2697
2698
2699
2700
2701
2702
2703
2704
2705
2706
2707
2708
2709
2710
2711
2712
2713
2714
2715
2716
2717
2718
2719
2720
2721
2722
2723
2724
2725
2726
2727
2728
2729
2730
2731
2732
2733
2734
2735
2736
2737
2738
2739
2740
2741
2742
2743
2744
2745
2746
2747
2748
2749
2750
2751
2752
2753
2754
2755
2756
2757
2758
2759
2760
2761
2762
2763
2764
2765
2766
2767
2768
2769
2770
2771
2772
2773
2774
2775
2776
2777
2778
2779
2780
2781
2782
2783
2784
2785
2786
2787
2788
2789
2790
2791
2792
2793
2794
2795
2796
2797
2798
2799
2800
2801
2802
2803
2804
2805
2806
2807
2808
2809
2810
2811
2812
2813
2814
2815
2816
2817
2818
2819
2820
2821
2822
2823
2824
2825
2826
2827
2828
2829
2830
2831
2832
2833
2834
2835
2836
2837
2838
2839
2840
2841
2842
2843
2844
2845
2846
2847
2848
2849
2850
2851
2852
2853
2854
2855
2856
2857
2858
2859
2860
2861
2862
2863
2864
2865
2866
2867
2868
2869
2870
2871
2872
2873
2874
2875
2876
2877
2878
2879
2880
2881
2882
2883
2884
2885
2886
2887
2888
2889
2890
2891
2892
2893
2894
2895
2896
2897
2898
2899
2900
2901
2902
2903
2904
2905
2906
2907
2908
2909
2910
2911
2912
2913
2914
2915
2916
2917
2918
2919
2920
2921
2922
2923
2924
2925
2926
2927
2928
2929
2930
2931
2932
2933
2934
2935
2936
2937
2938
2939
2940
2941
2942
2943
2944
2945
2946
2947
2948
2949
2950
2951
2952
2953
2954
2955
2956
2957
2958
2959
2960
2961
2962
2963
2964
2965
2966
2967
2968
2969
2970
2971
2972
2973
2974
2975
2976
2977
2978
2979
2980
2981
2982
2983
2984
2985
2986
2987
2988
2989
2990
2991
2992
2993
2994
2995
2996
2997
2998
2999
3000
3001
3002
3003
3004
3005
3006
3007
3008
3009
3010
3011
3012
3013
3014
3015
3016
3017
3018
3019
3020
3021
3022
3023
3024
3025
3026
3027
3028
3029
3030
3031
3032
3033
3034
3035
3036
3037
3038
3039
3040
3041
3042
3043
3044
3045
3046
3047
3048
3049
3050
3051
3052
3053
3054
3055
3056
3057
3058
3059
3060
3061
3062
3063
3064
3065
3066
3067
3068
3069
3070
3071
3072
3073
3074
3075
3076
3077
3078
3079
3080
3081
3082
3083
3084
3085
3086
3087
3088
3089
3090
3091
3092
3093
3094
3095
3096
3097
3098
3099
3100
3101
3102
3103
3104
3105
3106
3107
3108
3109
3110
3111
3112
3113
3114
3115
3116
3117
3118
3119
3120
3121
3122
3123
3124
3125
3126
3127
3128
3129
3130
3131
3132
3133
3134
3135
3136
3137
3138
3139
3140
3141
3142
3143
3144
3145
3146
3147
3148
3149
3150
3151
3152
3153
3154
3155
3156
3157
3158
3159
3160
3161
3162
3163
3164
3165
3166
3167
3168
3169
3170
3171
3172
3173
3174
3175
3176
3177
3178
3179
3180
3181
3182
3183
3184
3185
3186
3187
3188
3189
3190
3191
3192
3193
3194
3195
3196
3197
3198
3199
3200
3201
3202
3203
3204
3205
3206
3207
3208
3209
3210
3211
3212
3213
3214
3215
3216
3217
3218
3219
3220
3221
3222
3223
3224
3225
3226
3227
3228
3229
3230
3231
3232
3233
3234
3235
3236
3237
3238
3239
3240
3241
3242
3243
3244
3245
3246
3247
3248
3249
3250
3251
3252
3253
3254
3255
3256
3257
3258
3259
3260
3261
3262
3263
3264
3265
3266
3267
3268
3269
3270
3271
3272
3273
3274
3275
3276
3277
3278
3279
3280
3281
3282
3283
3284
3285
3286
3287
3288
3289
3290
3291
3292
3293
3294
3295
3296
3297
3298
3299
3300
3301
3302
3303
3304
3305
3306
3307
3308
3309
3310
3311
3312
3313
3314
3315
3316
3317
3318
3319
3320
3321
3322
3323
3324
3325
3326
3327
3328
3329
3330
3331
3332
3333
3334
3335
3336
3337
3338
3339
3340
3341
3342
3343
3344
3345
3346
3347
3348
3349
3350
3351
3352
3353
3354
3355
3356
3357
3358
3359
3360
3361
3362
3363
3364
3365
3366
3367
3368
3369
3370
3371
3372
3373
3374
3375
3376
3377
3378
3379
3380
3381
3382
3383
3384
3385
3386
3387
3388
3389
3390
3391
3392
3393
3394
3395
3396
3397
3398
3399
3400
3401
3402
3403
3404
3405
3406
3407
3408
3409
3410
3411
3412
3413
3414
3415
3416
3417
3418
3419
3420
3421
3422
3423
3424
3425
3426
3427
3428
3429
3430
3431
3432
3433
3434
3435
3436
3437
3438
3439
3440
3441
3442
3443
3444
3445
3446
3447
3448
3449
3450
3451
3452
3453
3454
3455
3456
3457
3458
3459
3460
3461
3462
3463
3464
3465
3466
3467
3468
3469
3470
3471
3472
3473
3474
3475
3476
3477
3478
3479
3480
3481
3482
3483
3484
3485
3486
3487
3488
3489
3490
3491
3492
3493
3494
3495
3496
3497
3498
3499
3500
3501
3502
3503
3504
3505
3506
3507
3508
3509
3510
3511
3512
3513
3514
3515
3516
3517
3518
3519
3520
3521
3522
3523
3524
3525
3526
3527
3528
3529
3530
3531
3532
3533
3534
3535
3536
3537
3538
3539
3540
3541
3542
3543
3544
3545
3546
3547
3548
3549
3550
3551
3552
3553
3554
3555
3556
3557
3558
3559
3560
3561
3562
3563
3564
3565
3566
3567
3568
3569
3570
3571
3572
3573
3574
3575
3576
3577
3578
3579
3580
3581
3582
3583
3584
3585
3586
3587
3588
3589
3590
3591
3592
3593
3594
3595
3596
3597
3598
3599
3600
3601
3602
3603
3604
3605
3606
3607
3608
3609
3610
3611
3612
3613
3614
3615
3616
3617
3618
3619
3620
3621
3622
3623
3624
3625
3626
3627
3628
3629
3630
3631
3632
3633
3634
3635
3636
3637
3638
3639
3640
3641
3642
3643
3644
3645
3646
3647
3648
3649
3650
3651
3652
3653
3654
3655
3656
3657
3658
3659
3660
3661
3662
3663
3664
3665
3666
3667
3668
3669
3670
3671
3672
3673
3674
3675
3676
3677
3678
3679
3680
3681
3682
3683
3684
3685
3686
3687
3688
3689
3690
3691
3692
3693
3694
3695
3696
3697
3698
3699
3700
3701
3702
3703
3704
3705
3706
3707
3708
3709
3710
3711
3712
3713
3714
3715
3716
3717
3718
3719
3720
3721
3722
3723
3724
3725
3726
3727
3728
3729
3730
3731
3732
3733
3734
3735
3736
3737
3738
3739
3740
3741
3742
3743
3744
3745
3746
3747
3748
3749
3750
3751
3752
3753
3754
3755
3756
3757
3758
3759
3760
3761
3762
3763
3764
3765
3766
3767
3768
3769
3770
3771
3772
3773
3774
3775
3776
3777
3778
3779
3780
3781
3782
3783
3784
3785
3786
3787
3788
3789
3790
3791
3792
3793
3794
3795
3796
3797
3798
3799
3800
3801
3802
3803
3804
3805
3806
3807
3808
3809
3810
3811
3812
3813
3814
3815
3816
3817
3818
3819
3820
3821
3822
3823
3824
3825
3826
3827
3828
3829
3830
3831
3832
3833
3834
3835
3836
3837
3838
3839
3840
3841
3842
3843
3844
3845
3846
3847
3848
3849
3850
3851
3852
3853
3854
3855
3856
3857
3858
3859
3860
3861
3862
3863
3864
3865
3866
3867
3868
3869
3870
3871
3872
3873
3874
3875
3876
3877
3878
3879
3880
3881
3882
3883
3884
3885
3886
3887
3888
3889
3890
3891
3892
3893
3894
3895
3896
3897
3898
3899
3900
3901
3902
3903
3904
3905
3906
3907
3908
3909
3910
3911
3912
3913
3914
3915
3916
3917
3918
3919
3920
3921
3922
3923
3924
3925
3926
3927
3928
3929
3930
3931
3932
3933
3934
3935
3936
3937
3938
3939
3940
3941
3942
3943
3944
3945
3946
3947
3948
3949
3950
3951
3952
3953
3954
3955
3956
3957
3958
3959
3960
3961
3962
3963
3964
3965
3966
3967
3968
3969
3970
3971
3972
3973
3974
3975
3976
3977
3978
3979
3980
3981
3982
3983
3984
3985
3986
3987
3988
3989
3990
3991
3992
3993
3994
3995
3996
3997
3998
3999
4000
4001
4002
4003
4004
4005
4006
4007
4008
4009
4010
4011
4012
4013
4014
4015
4016
4017
4018
4019
4020
4021
4022
4023
4024
4025
4026
4027
4028
4029
4030
4031
4032
4033
4034
4035
4036
4037
4038
4039
4040
4041
4042
4043
4044
4045
4046
4047
4048
4049
4050
4051
4052
4053
4054
4055
4056
4057
4058
4059
4060
4061
4062
4063
4064
4065
4066
4067
4068
4069
4070
4071
4072
4073
4074
4075
4076
4077
4078
4079
4080
4081
4082
4083
4084
4085
4086
4087
4088
4089
4090
4091
4092
4093
4094
4095
4096
4097
4098
4099
4100
4101
4102
4103
4104
4105
4106
4107
4108
4109
4110
4111
4112
4113
4114
4115
4116
4117
4118
4119
4120
4121
4122
4123
4124
4125
4126
4127
4128
4129
4130
4131
4132
4133
4134
4135
4136
4137
4138
4139
4140
4141
4142
4143
4144
4145
4146
4147
4148
4149
4150
4151
4152
4153
4154
4155
4156
4157
4158
4159
4160
4161
4162
4163
4164
4165
4166
4167
4168
4169
4170
4171
4172
4173
4174
4175
4176
4177
4178
4179
4180
4181
4182
4183
4184
4185
4186
4187
4188
4189
4190
4191
4192
4193
4194
4195
4196
4197
4198
4199
4200
4201
4202
4203
4204
4205
4206
4207
4208
4209
4210
4211
4212
4213
4214
4215
4216
4217
4218
4219
4220
4221
4222
4223
4224
4225
4226
4227
4228
4229
4230
4231
4232
4233
4234
4235
4236
4237
4238
4239
4240
4241
4242
4243
4244
4245
4246
4247
4248
4249
4250
4251
4252
4253
4254
4255
4256
4257
4258
4259
4260
4261
4262
4263
4264
4265
4266
4267
4268
4269
4270
4271
4272
4273
4274
4275
4276
4277
4278
4279
4280
4281
4282
4283
4284
4285
4286
4287
4288
4289
4290
4291
4292
4293
4294
4295
4296
4297
4298
4299
4300
4301
4302
4303
4304
4305
4306
4307
4308
4309
4310
4311
4312
4313
4314
4315
4316
4317
4318
4319
4320
4321
4322
4323
4324
4325
4326
4327
4328
4329
4330
4331
4332
4333
4334
4335
4336
4337
4338
4339
4340
4341
4342
4343
4344
4345
4346
4347
4348
4349
4350
4351
4352
4353
4354
4355
4356
4357
4358
4359
4360
4361
4362
4363
4364
4365
4366
4367
4368
4369
4370
4371
4372
4373
4374
4375
4376
4377
4378
4379
4380
4381
4382
4383
4384
4385
4386
4387
4388
4389
4390
4391
4392
4393
4394
4395
4396
4397
4398
4399
4400
4401
4402
4403
4404
4405
4406
4407
4408
4409
4410
4411
4412
4413
4414
4415
4416
4417
4418
4419
4420
4421
4422
4423
4424
4425
4426
4427
4428
4429
4430
4431
4432
4433
4434
4435
4436
4437
4438
4439
4440
4441
4442
4443
4444
4445
4446
4447
4448
4449
4450
4451
4452
4453
4454
4455
4456
4457
4458
4459
4460
4461
4462
4463
4464
4465
4466
4467
4468
4469
4470
4471
4472
4473
4474
4475
4476
4477
4478
4479
4480
4481
4482
4483
4484
4485
4486
4487
4488
4489
4490
4491
4492
4493
4494
4495
4496
4497
4498
4499
(*****************************************************************************)
(*                                                                           *)
(* Open Source License                                                       *)
(* Copyright (c) 2020-2021 Nomadic Labs, <contact@nomadic-labs.com>          *)
(*                                                                           *)
(* Permission is hereby granted, free of charge, to any person obtaining a   *)
(* copy of this software and associated documentation files (the "Software"),*)
(* to deal in the Software without restriction, including without limitation *)
(* the rights to use, copy, modify, merge, publish, distribute, sublicense,  *)
(* and/or sell copies of the Software, and to permit persons to whom the     *)
(* Software is furnished to do so, subject to the following conditions:      *)
(*                                                                           *)
(* The above copyright notice and this permission notice shall be included   *)
(* in all copies or substantial portions of the Software.                    *)
(*                                                                           *)
(* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR*)
(* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,  *)
(* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL   *)
(* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER*)
(* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING   *)
(* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER       *)
(* DEALINGS IN THE SOFTWARE.                                                 *)
(*                                                                           *)
(*****************************************************************************)

open Snapshots_events
open Store_types

type error +=
  | Incompatible_history_mode of {
      requested : History_mode.t;
      stored : History_mode.t;
    }
  | Invalid_export_block of {
      block : Block_hash.t option;
      reason :
        [ `Pruned
        | `Pruned_pred
        | `Unknown
        | `Unknown_ancestor
        | `Caboose
        | `Genesis
        | `Not_enough_pred ];
    }
  | Invalid_export_path of string
  | Snapshot_file_not_found of string
  | Inconsistent_protocol_hash of {
      expected : Protocol_hash.t;
      got : Protocol_hash.t;
    }
  | Inconsistent_context_hash of {
      expected : Context_hash.t;
      got : Context_hash.t;
    }
  | Inconsistent_context of Context_hash.t
  | Cannot_decode_protocol of Protocol_hash.t
  | Cannot_write_metadata of string
  | Cannot_read of {
      kind :
        [ `Version
        | `Metadata
        | `Block_data
        | `Context
        | `Protocol_table
        | `Protocol
        | `Cemented_cycle ];
      path : string;
    }
  | Inconsistent_floating_store of block_descriptor * block_descriptor
  | Missing_target_block of block_descriptor
  | Cannot_read_floating_store of string
  | Cannot_retrieve_block_interval
  | Invalid_cemented_file of string
  | Missing_cemented_file of string
  | Corrupted_floating_store
  | Invalid_protocol_file of string
  | Target_block_validation_failed of Block_hash.t * string
  | Directory_already_exists of string
  | Empty_floating_store
  | Cannot_remove_tmp_export_directory of string
  | Inconsistent_version_import of {expected : int list; got : int}
  | Inconsistent_chain_import of {
      expected : Distributed_db_version.Name.t;
      got : Distributed_db_version.Name.t;
    }
  | Inconsistent_history_mode_import of {
      requested : History_mode.t;
      stored : History_mode.t;
    }
  | Inconsistent_imported_block of Block_hash.t * Block_hash.t
  | Wrong_snapshot_file of {filename : string}
  | Invalid_chain_store_export of Chain_id.t * string
  | Cannot_export_snapshot_format

let () =
  let open Data_encoding in
  register_error_kind
    `Permanent
    ~id:"snapshots.incompatible_export"
    ~title:"Incompatible snapshot export"
    ~description:
      "The requested history mode for the snapshot is not compatible with the \
       given storage."
    ~pp:(fun ppf (requested, stored) ->
      Format.fprintf
        ppf
        "The requested history mode (%a) for the snapshot export is not \
         compatible with the given storage (running with history mode %a)."
        History_mode.pp_short
        requested
        History_mode.pp_short
        stored)
    (obj2
       (req "stored" History_mode.encoding)
       (req "requested" History_mode.encoding))
    (function
      | Incompatible_history_mode {requested; stored} -> Some (requested, stored)
      | _ -> None)
    (fun (requested, stored) -> Incompatible_history_mode {requested; stored}) ;
  register_error_kind
    `Permanent
    ~id:"snapshots.invalid_export_block"
    ~title:"Invalid export block"
    ~description:"Invalid block provided for snapshot export."
    ~pp:(fun ppf (hash, reason) ->
      Format.fprintf
        ppf
        "The selected block %a is invalid: %s."
        (Format.pp_print_option
           ~none:(fun fmt () -> Format.fprintf fmt "(n/a)")
           Block_hash.pp)
        hash
        (match reason with
        | `Pruned -> "the block is too old and has been pruned"
        | `Pruned_pred -> "its predecessor has been pruned"
        | `Unknown -> "the block is unknown"
        | `Unknown_ancestor -> "the block's ancestor is unknown"
        | `Genesis -> "the genesis block is not a valid export point"
        | `Caboose -> "the caboose block is not a valid export point"
        | `Not_enough_pred -> "not enough of the block's predecessors are known"))
    (obj2
       (opt "block" Block_hash.encoding)
       (req
          "reason"
          (string_enum
             [
               ("pruned", `Pruned);
               ("pruned_pred", `Pruned_pred);
               ("unknown", `Unknown);
               ("unknown_ancestor", `Unknown_ancestor);
               ("genesis", `Genesis);
               ("caboose", `Genesis);
               ("not_enough_pred", `Not_enough_pred);
             ])))
    (function
      | Invalid_export_block {block; reason} -> Some (block, reason) | _ -> None)
    (fun (block, reason) -> Invalid_export_block {block; reason}) ;
  register_error_kind
    `Permanent
    ~id:"snapshots.invalid_export_path"
    ~title:"Invalid export path"
    ~description:"Invalid path to export snapshot"
    ~pp:(fun ppf path ->
      Format.fprintf
        ppf
        "Failed to export snapshot: the file or directory %s already exists."
        path)
    (obj1 (req "path" string))
    (function Invalid_export_path path -> Some path | _ -> None)
    (fun path -> Invalid_export_path path) ;
  register_error_kind
    `Permanent
    ~id:"snapshots.snapshot_file_not_found"
    ~title:"Snapshot file not found"
    ~description:"The snapshot file cannot be found."
    ~pp:(fun ppf given_file ->
      Format.fprintf ppf "The snapshot file %s does not exists." given_file)
    (obj1 (req "given_snapshot_file" string))
    (function Snapshot_file_not_found file -> Some file | _ -> None)
    (fun file -> Snapshot_file_not_found file) ;
  register_error_kind
    `Permanent
    ~id:"snapshots.inconsistent_protocol_hash"
    ~title:"Inconsistent protocol hash"
    ~description:"The announced protocol hash doesn't match the computed hash."
    ~pp:(fun ppf (oph, oph') ->
      Format.fprintf
        ppf
        "Inconsistent protocol_hash. Expected: %a, got %a."
        Protocol_hash.pp
        oph
        Protocol_hash.pp
        oph')
    (obj2
       (req "expected" Protocol_hash.encoding)
       (req "got" Protocol_hash.encoding))
    (function
      | Inconsistent_protocol_hash {expected; got} -> Some (expected, got)
      | _ -> None)
    (fun (expected, got) -> Inconsistent_protocol_hash {expected; got}) ;
  register_error_kind
    `Permanent
    ~id:"snapshots.inconsistent_context_hash"
    ~title:"Inconsistent context hash"
    ~description:"The announced context hash doesn't match the computed hash."
    ~pp:(fun ppf (oph, oph') ->
      Format.fprintf
        ppf
        "Inconsistent context_hash. Expected: %a, got %a."
        Context_hash.pp
        oph
        Context_hash.pp
        oph')
    (obj2
       (req "expected" Context_hash.encoding)
       (req "got" Context_hash.encoding))
    (function
      | Inconsistent_context_hash {expected; got} -> Some (expected, got)
      | _ -> None)
    (fun (expected, got) -> Inconsistent_context_hash {expected; got}) ;
  register_error_kind
    `Permanent
    ~id:"snapshot.inconsistent_context"
    ~title:"Inconsistent context"
    ~description:"Inconsistent context after restore."
    ~pp:(fun ppf h ->
      Format.fprintf
        ppf
        "Failed to checkout context %a after restoring it."
        Context_hash.pp
        h)
    (obj1 (req "context_hash" Context_hash.encoding))
    (function Inconsistent_context h -> Some h | _ -> None)
    (fun h -> Inconsistent_context h) ;
  register_error_kind
    `Permanent
    ~id:"snapshot.cannot_decode_protocol"
    ~title:"Protocol import cannot decode"
    ~description:"Failed to decode file when importing protocol"
    ~pp:(fun ppf hash ->
      Format.fprintf
        ppf
        "Cannot decode the protocol in file: %a"
        Protocol_hash.pp
        hash)
    (obj1 (req "filename" Protocol_hash.encoding))
    (function Cannot_decode_protocol hash -> Some hash | _ -> None)
    (fun hash -> Cannot_decode_protocol hash) ;
  register_error_kind
    `Permanent
    ~id:"snapshot.cannot_write_metadata"
    ~title:"Cannot write metadata"
    ~description:"Cannot write metadata while exporting snapshot."
    ~pp:(fun ppf msg ->
      Format.fprintf
        ppf
        "Cannot write metadata while exporting snapshot: %s."
        msg)
    (obj1 (req "msg" string))
    (function Cannot_write_metadata msg -> Some msg | _ -> None)
    (fun msg -> Cannot_write_metadata msg) ;
  register_error_kind
    `Permanent
    ~id:"snapshot.cannot_read"
    ~title:"Cannot read"
    ~description:"Cannot read some snapshot data."
    ~pp:(fun ppf (kind, path) ->
      let kind =
        match kind with
        | `Version -> "version"
        | `Metadata -> "metadata"
        | `Block_data -> "block data"
        | `Context -> "context"
        | `Protocol_table -> "protocol table"
        | `Protocol -> "protocol"
        | `Cemented_cycle -> "cemented cycle"
      in
      Format.fprintf ppf "Cannot read snapshot's %s from %s." kind path)
    (obj2
       (req
          "kind"
          (string_enum
             [
               ("version", `Version);
               ("metadata", `Metadata);
               ("block_data", `Block_data);
               ("context", `Context);
               ("protocol_table", `Protocol_table);
               ("protocol", `Protocol);
               ("cemented_cycle", `Cemented_cycle);
             ]))
       (req "path" string))
    (function Cannot_read {kind; path} -> Some (kind, path) | _ -> None)
    (fun (kind, path) -> Cannot_read {kind; path}) ;
  register_error_kind
    `Permanent
    ~id:"snapshot.inconsistent_floating_store"
    ~title:"Inconsistent floating store"
    ~description:"The floating block store is inconsistent."
    ~pp:(fun ppf (target_blk, first_blk) ->
      Format.fprintf
        ppf
        "Failed to export floating store, the first block %a is above the \
         target block %a (broken invariant)."
        pp_block_descriptor
        first_blk
        pp_block_descriptor
        target_blk)
    (obj2
       (req "target" block_descriptor_encoding)
       (req "first" block_descriptor_encoding))
    (function
      | Inconsistent_floating_store (target, first) -> Some (target, first)
      | _ -> None)
    (fun (target, first) -> Inconsistent_floating_store (target, first)) ;
  register_error_kind
    `Permanent
    ~id:"snapshot.missing_target_block"
    ~title:"Missing target block in floating stores"
    ~description:"Floating stores does not contain the target block."
    ~pp:(fun ppf target_blk ->
      Format.fprintf
        ppf
        "Failed to export floating blocks as the target block %a cannot be \
         found."
        pp_block_descriptor
        target_blk)
    (obj1 (req "target" block_descriptor_encoding))
    (function Missing_target_block descr -> Some descr | _ -> None)
    (fun descr -> Missing_target_block descr) ;
  register_error_kind
    `Permanent
    ~id:"snapshot.cannot_read_floating_stores"
    ~title:"Cannot read floating stores"
    ~description:"Unable to read floating stores."
    ~pp:(fun ppf msg ->
      Format.fprintf ppf "Cannot read the floating blocks stores: %s" msg)
    (obj1 (req "msg" string))
    (function Cannot_read_floating_store msg -> Some msg | _ -> None)
    (fun msg -> Cannot_read_floating_store msg) ;
  register_error_kind
    `Permanent
    ~id:"snapshot.cannot_retrieve_block_interval"
    ~title:"Cannot retrieve block interval"
    ~description:"Cannot retrieve block interval from store"
    ~pp:(fun ppf () ->
      Format.fprintf
        ppf
        "Cannot retrieve block interval: failed to retrieve blocks.")
    unit
    (function Cannot_retrieve_block_interval -> Some () | _ -> None)
    (fun () -> Cannot_retrieve_block_interval) ;
  register_error_kind
    `Permanent
    ~id:"snapshot.invalid_cemented_file"
    ~title:"Invalid cemented file"
    ~description:
      "Encountered an invalid cemented file while restoring the cemented store"
    ~pp:(fun ppf file ->
      Format.fprintf
        ppf
        "Failed to restore cemented blocks. Encountered an invalid file '%s'."
        file)
    (obj1 (req "file" string))
    (function Invalid_cemented_file s -> Some s | _ -> None)
    (fun s -> Invalid_cemented_file s) ;
  register_error_kind
    `Permanent
    ~id:"snapshot.missing_cemented_file"
    ~title:"Missing cemented file"
    ~description:"Cannot find cemented file while restoring cemented store"
    ~pp:(fun ppf file ->
      Format.fprintf
        ppf
        "Failed to restore cemented blocks. The cycle '%s' is missing."
        file)
    (obj1 (req "cycle" string))
    (function Missing_cemented_file s -> Some s | _ -> None)
    (fun s -> Missing_cemented_file s) ;
  register_error_kind
    `Permanent
    ~id:"snapshot.corrupted_floating_store"
    ~title:"Corrupted floating store"
    ~description:"Failed to read floating store"
    ~pp:(fun ppf () ->
      Format.fprintf
        ppf
        "Failed to restore floating blocks. The floating store is corrupted.")
    unit
    (function Corrupted_floating_store -> Some () | _ -> None)
    (fun () -> Corrupted_floating_store) ;
  register_error_kind
    `Permanent
    ~id:"snapshot.protocol_import_invalid_file"
    ~title:"Protocol import invalid file"
    ~description:"Failed to import protocol as the filename is invalid"
    ~pp:(fun ppf filename ->
      Format.fprintf
        ppf
        "Failed to import protocol. The protocol file '%s' is invalid"
        filename)
    (obj1 (req "filename" string))
    (function Invalid_protocol_file filename -> Some filename | _ -> None)
    (fun filename -> Invalid_protocol_file filename) ;
  register_error_kind
    `Permanent
    ~id:"snapshot.target_block_validation_failed"
    ~title:"target block validation failed"
    ~description:"Failed to validate the target block."
    ~pp:(fun ppf (h, errs) ->
      Format.fprintf ppf "Failed to validate block %a: %s" Block_hash.pp h errs)
    (obj2 (req "block" Block_hash.encoding) (req "errors" string))
    (function
      | Target_block_validation_failed (h, errs) -> Some (h, errs) | _ -> None)
    (fun (h, errs) -> Target_block_validation_failed (h, errs)) ;
  register_error_kind
    `Permanent
    ~id:"snapshot.directory_already_exists"
    ~title:"Directory already exists"
    ~description:"The given data directory already exists."
    ~pp:(fun ppf s ->
      Format.fprintf
        ppf
        "Failed to import snapshot as the given directory %s already exists."
        s)
    (obj1 (req "path" string))
    (function Directory_already_exists s -> Some s | _ -> None)
    (fun s -> Directory_already_exists s) ;
  register_error_kind
    `Permanent
    ~id:"snapshot.empty_floating_store"
    ~title:"Empty floating store"
    ~description:"Floating store is empty."
    ~pp:(fun ppf () ->
      Format.fprintf
        ppf
        "Failed to export floating blocks: the floating store does not contain \
         any blocks (broken invariant).")
    unit
    (function Empty_floating_store -> Some () | _ -> None)
    (fun () -> Empty_floating_store) ;
  register_error_kind
    `Permanent
    ~id:"snapshots.cannot_remove_tmp_export_directory"
    ~title:"Cannot remove temporary export directory"
    ~description:"Cannot create temporary directory for exporting snapshot."
    ~pp:(fun ppf msg ->
      Format.fprintf
        ppf
        "Cannot export snapshot: the temporary snapshot directory already \
         exists and cannot be removed. Please remove %s and restart the \
         snapshot export."
        msg)
    (obj1 (req "message" string))
    (function Cannot_remove_tmp_export_directory str -> Some str | _ -> None)
    (fun str -> Cannot_remove_tmp_export_directory str) ;
  register_error_kind
    `Permanent
    ~id:"snapshots.inconsistent_version_import"
    ~title:"Inconsistent version import"
    ~description:"The imported snapshot's version is not supported."
    ~pp:(fun ppf (expected, got) ->
      Format.fprintf
        ppf
        "The version of the snapshot file %d is not compatible with the node. \
         Only the following versions can be imported: %a."
        got
        Format.(
          pp_print_list
            ~pp_sep:(fun fmt () -> Format.fprintf fmt ", ")
            pp_print_int)
        expected)
    (obj2 (req "expected" (list int31)) (req "got" int31))
    (function
      | Inconsistent_version_import {expected; got} -> Some (expected, got)
      | _ -> None)
    (fun (expected, got) -> Inconsistent_version_import {expected; got}) ;
  register_error_kind
    `Permanent
    ~id:"snapshots.inconsistent_chain_import"
    ~title:"Inconsistent chain import"
    ~description:
      "The imported chain is inconsistent with the target data directory."
    ~pp:(fun ppf (expected, got) ->
      Format.fprintf
        ppf
        "The chain name contained in the snapshot file (%a) is not consistent \
         with the network configured in the targeted data directory (%a). \
         Please check your configuration file."
        Distributed_db_version.Name.pp
        expected
        Distributed_db_version.Name.pp
        got)
    (obj2
       (req "expected" Distributed_db_version.Name.encoding)
       (req "got" Distributed_db_version.Name.encoding))
    (function
      | Inconsistent_chain_import {expected; got} -> Some (expected, got)
      | _ -> None)
    (fun (expected, got) -> Inconsistent_chain_import {expected; got}) ;
  register_error_kind
    `Permanent
    ~id:"snapshots.inconsistent_history_mode_import"
    ~title:"Inconsistent history_mode import"
    ~description:
      "The imported history mode is inconsistent with the target data \
       directory."
    ~pp:(fun ppf (requested, stored) ->
      Format.fprintf
        ppf
        "The history mode contained in the snapshot file (%a) is not \
         consistent with the one configured in the targeted data directory \
         (%a). Please check your configuration file."
        History_mode.pp
        requested
        History_mode.pp
        stored)
    (obj2
       (req "requested" History_mode.encoding)
       (req "stored" History_mode.encoding))
    (function
      | Inconsistent_history_mode_import {requested; stored} ->
          Some (requested, stored)
      | _ -> None)
    (fun (requested, stored) ->
      Inconsistent_history_mode_import {requested; stored}) ;
  register_error_kind
    `Permanent
    ~id:"context_dump.inconsistent_imported_block"
    ~title:"Inconsistent imported block"
    ~description:"The imported block is not the expected one."
    ~pp:(fun ppf (got, exp) ->
      Format.fprintf
        ppf
        "The block contained in the file is %a instead of %a."
        Block_hash.pp
        got
        Block_hash.pp
        exp)
    (obj2
       (req "block_hash" Block_hash.encoding)
       (req "block_hash_expected" Block_hash.encoding))
    (function
      | Inconsistent_imported_block (got, exp) -> Some (got, exp) | _ -> None)
    (fun (got, exp) -> Inconsistent_imported_block (got, exp)) ;
  register_error_kind
    `Permanent
    ~id:"Snapshot.wrong_snapshot_file"
    ~title:"Wrong snapshot file"
    ~description:"Error while opening snapshot file"
    ~pp:(fun ppf filename ->
      Format.fprintf
        ppf
        "Failed to read snapshot file %s. The provided file is inconsistent or \
         is from Octez 12 (or before) and it cannot be imported anymore."
        filename)
    Data_encoding.(obj1 (req "filename" string))
    (function Wrong_snapshot_file {filename} -> Some filename | _ -> None)
    (fun filename -> Wrong_snapshot_file {filename}) ;
  register_error_kind
    `Permanent
    ~id:"Snapshot.invalid_chain_store_export"
    ~title:"Invalid chain store export"
    ~description:"Error while exporting snapshot"
    ~pp:(fun ppf (chain_id, store_dir) ->
      Format.fprintf
        ppf
        "Failed to export snapshot. Cannot find chain %a from store located at \
         directory %s."
        Chain_id.pp_short
        chain_id
        store_dir)
    Data_encoding.(
      obj2 (req "chain_id" Chain_id.encoding) (req "store_dir" string))
    (function
      | Invalid_chain_store_export (chain_id, store_dir) ->
          Some (chain_id, store_dir)
      | _ -> None)
    (fun (chain_id, store_dir) ->
      Invalid_chain_store_export (chain_id, store_dir)) ;
  register_error_kind
    `Permanent
    ~id:"Snapshot.cannot_export_snapshot_format"
    ~title:"Cannot export snapshot format"
    ~description:"Cannot export snapshot format"
    ~pp:(fun ppf () ->
      Format.fprintf
        ppf
        "Cannot export snapshot with a storage that was created with Octez v13 \
         (or earlier). Please refer to the documentation and consider \
         switching to the default minimal indexing strategy to enable snapshot \
         exports. ")
    unit
    (function Cannot_export_snapshot_format -> Some () | _ -> None)
    (fun () -> Cannot_export_snapshot_format)

(* This module handles snapshot's versioning system. *)
module Version = struct
  type t = int

  let (encoding : t Data_encoding.t) =
    let open Data_encoding in
    obj1 (req "version" int31)

  (* Current version of the snapshots, since 0.0.7.
   * Previous versions are:
   * - 1: snapshot exported with storage 0.0.1 to 0.0.4
   * - 2: snapshot exported with storage 0.0.4 to 0.0.6
   * - 3: snapshot exported with storage 0.0.7
   * - 4: snapshot exported with storage 0.0.8 to current
   * - 5: snapshot exported with new protocol tables and "à la GC"
       context (storage 2.0 to current)*)

  let legacy_version = 4

  let current_version = 5

  (* List of versions that are supported *)
  let supported_versions =
    [(legacy_version, `Legacy); (current_version, `Current)]

  let is_supported version =
    match List.assq_opt version supported_versions with
    | Some _ -> true
    | None -> false

  (* Returns true if the given version is considered as legacy. *)
  let is_legacy version =
    let open Lwt_result_syntax in
    match List.assq_opt version supported_versions with
    | None ->
        tzfail
          (Inconsistent_version_import
             {expected = List.map fst supported_versions; got = version})
    | Some `Legacy -> return_true
    | Some _ -> return_false
end

(* The [default_index_log_size] defines the default maximal value for
   the log_size, used by the index for storing in memory values, while
   importing a snapshot. The current limit of 30M aims to target
   around 1GB of memory usage. Increasing this variable may slightly
   reduce the import time. *)
let default_index_log_size = 30_000_000

let snapshot_rw_file_perm = 0o644

let snapshot_ro_file_perm = 0o444

let snapshot_dir_perm = 0o755

module Snapshot_metadata = struct
  type metadata = {
    chain_name : Distributed_db_version.Name.t;
    history_mode : History_mode.t;
    block_hash : Block_hash.t;
    level : Int32.t;
    timestamp : Time.Protocol.t;
  }

  let metadata_encoding =
    let open Data_encoding in
    conv
      (fun {chain_name; history_mode; block_hash; level; timestamp} ->
        (chain_name, history_mode, block_hash, level, timestamp))
      (fun (chain_name, history_mode, block_hash, level, timestamp) ->
        {chain_name; history_mode; block_hash; level; timestamp})
      (obj5
         (req "chain_name" Distributed_db_version.Name.encoding)
         (req "mode" History_mode.encoding)
         (req "block_hash" Block_hash.encoding)
         (req "level" int32)
         (req "timestamp" Time.Protocol.encoding))

  type legacy_metadata = {
    chain_name : Distributed_db_version.Name.t;
    history_mode : History_mode.t;
    block_hash : Block_hash.t;
    level : Int32.t;
    timestamp : Time.Protocol.t;
    context_elements : int;
  }

  let legacy_metadata_encoding =
    let open Data_encoding in
    conv
      (fun {
             chain_name;
             history_mode;
             block_hash;
             level;
             timestamp;
             context_elements;
           } ->
        ( chain_name,
          history_mode,
          block_hash,
          level,
          timestamp,
          context_elements ))
      (fun ( chain_name,
             history_mode,
             block_hash,
             level,
             timestamp,
             context_elements ) ->
        {
          chain_name;
          history_mode;
          block_hash;
          level;
          timestamp;
          context_elements;
        })
      (obj6
         (req "chain_name" Distributed_db_version.Name.encoding)
         (req "mode" History_mode.encoding)
         (req "block_hash" Block_hash.encoding)
         (req "level" int32)
         (req "timestamp" Time.Protocol.encoding)
         (req "context_elements" int31))

  type t = Current of metadata | Legacy of legacy_metadata

  let pp ppf metadata =
    let chain_name, block_hash, level, history_mode, timestamp =
      match metadata with
      | Current {chain_name; block_hash; level; history_mode; timestamp} ->
          (chain_name, block_hash, level, history_mode, timestamp)
      | Legacy {chain_name; block_hash; level; history_mode; timestamp; _} ->
          (chain_name, block_hash, level, history_mode, timestamp)
    in
    Format.fprintf
      ppf
      "chain %a, block hash %a at level %ld, timestamp %a in %a"
      Distributed_db_version.Name.pp
      chain_name
      Block_hash.pp
      block_hash
      level
      Time.Protocol.pp_hum
      timestamp
      History_mode.pp_short
      history_mode

  let get_block_hash = function
    | Current {block_hash; _} -> block_hash
    | Legacy {block_hash; _} -> block_hash

  let get_chain_name = function
    | Current {chain_name; _} -> chain_name
    | Legacy {chain_name; _} -> chain_name

  let get_history_mode = function
    | Current {history_mode; _} -> history_mode
    | Legacy {history_mode; _} -> history_mode

  let read_metadata ~metadata_file =
    let open Lwt_result_syntax in
    let read_json json = Data_encoding.Json.destruct metadata_encoding json in
    let* json = Lwt_utils_unix.Json.read_file metadata_file in
    return (read_json json)

  let read_legacy_metadata ~metadata_file =
    let open Lwt_result_syntax in
    let read_json json =
      Data_encoding.Json.destruct legacy_metadata_encoding json
    in
    let* json = Lwt_utils_unix.Json.read_file metadata_file in
    return (read_json json)
end

(* A snapshot header is made of a version and some metadata. The
   encoding of the version aims to be fixed between snapshots
   version. On the contrary, metadata may evolve with snapshot
   versions. *)
module Snapshot_header = struct
  type snapshot_header = Version.t * Snapshot_metadata.metadata

  let snapshot_header_encoding =
    let open Data_encoding in
    obj1
      (req
         "snapshot_header"
         (merge_objs Version.encoding Snapshot_metadata.metadata_encoding))

  type snapshot_header_legacy = Version.t * Snapshot_metadata.legacy_metadata

  let snapshot_header_legacy_encoding =
    let open Data_encoding in
    obj1
      (req
         "snapshot_header"
         (merge_objs
            Version.encoding
            Snapshot_metadata.legacy_metadata_encoding))

  type t = Current of snapshot_header | Legacy of snapshot_header_legacy

  let pp ppf = function
    | Current (version, metadata) ->
        Format.fprintf
          ppf
          "%a (snapshot version %d)"
          Snapshot_metadata.pp
          (Snapshot_metadata.Current metadata)
          version
    | Legacy (version, metadata) ->
        Format.fprintf
          ppf
          "%a (snapshot version %d)"
          Snapshot_metadata.pp
          (Snapshot_metadata.Legacy metadata)
          version

  let to_json = function
    | Current snapshot_header ->
        Data_encoding.Json.construct snapshot_header_encoding snapshot_header
    | Legacy snapshot_header ->
        Data_encoding.Json.construct
          snapshot_header_legacy_encoding
          snapshot_header

  let get_version = function
    | Current (version, _) -> version
    | Legacy (version, _) -> version

  let get_metadata = function
    | Current (_, metadata) -> Snapshot_metadata.Current metadata
    | Legacy (_, metadata) -> Snapshot_metadata.Legacy metadata
end

type snapshot_format = Tar | Raw

let snapshot_format_encoding =
  Data_encoding.string_enum [("Tar", Tar); ("Raw", Raw)]

let pp_snapshot_format ppf = function
  | Tar -> Format.fprintf ppf "tar (single file)"
  | Raw -> Format.fprintf ppf "directory"

(* To speed up the import of the cemented blocks we increase,
   temporarily the index cache size. *)
let cemented_import_log_size = 100_000

type block_data = {
  block_header : Block_header.t;
  operations : Operation.t list list;
  predecessor_header : Block_header.t;
  predecessor_block_metadata_hash : Block_metadata_hash.t option;
  predecessor_ops_metadata_hash : Operation_metadata_list_list_hash.t option;
  resulting_context_hash : Context_hash.t;
}

let block_data_encoding =
  let open Data_encoding in
  conv
    (fun {
           block_header;
           operations;
           predecessor_header;
           predecessor_block_metadata_hash;
           predecessor_ops_metadata_hash;
           resulting_context_hash;
         } ->
      ( operations,
        block_header,
        predecessor_header,
        predecessor_block_metadata_hash,
        predecessor_ops_metadata_hash,
        resulting_context_hash ))
    (fun ( operations,
           block_header,
           predecessor_header,
           predecessor_block_metadata_hash,
           predecessor_ops_metadata_hash,
           resulting_context_hash ) ->
      {
        block_header;
        operations;
        predecessor_header;
        predecessor_block_metadata_hash;
        predecessor_ops_metadata_hash;
        resulting_context_hash;
      })
    (obj6
       (req "operations" (list (list (dynamic_size Operation.encoding))))
       (req "block_header" (dynamic_size Block_header.encoding))
       (req "predecessor_header" (dynamic_size Block_header.encoding))
       (opt "predecessor_block_metadata_hash" Block_metadata_hash.encoding)
       (opt
          "predecessor_ops_metadata_hash"
          Operation_metadata_list_list_hash.encoding)
       (req " resulting_context_hash" Context_hash.encoding))

type legacy_block_data = {
  block_header : Block_header.t;
  operations : Operation.t list list;
  predecessor_header : Block_header.t;
  predecessor_block_metadata_hash : Block_metadata_hash.t option;
  predecessor_ops_metadata_hash : Operation_metadata_list_list_hash.t option;
}

let legacy_block_data_encoding =
  let open Data_encoding in
  conv
    (fun {
           block_header;
           operations;
           predecessor_header;
           predecessor_block_metadata_hash;
           predecessor_ops_metadata_hash;
         } ->
      ( operations,
        block_header,
        predecessor_header,
        predecessor_block_metadata_hash,
        predecessor_ops_metadata_hash ))
    (fun ( operations,
           block_header,
           predecessor_header,
           predecessor_block_metadata_hash,
           predecessor_ops_metadata_hash ) ->
      {
        block_header;
        operations;
        predecessor_header;
        predecessor_block_metadata_hash;
        predecessor_ops_metadata_hash;
      })
    (obj5
       (req "operations" (list (list (dynamic_size Operation.encoding))))
       (req "block_header" (dynamic_size Block_header.encoding))
       (req "predecessor_header" (dynamic_size Block_header.encoding))
       (opt "predecessor_block_metadata_hash" Block_metadata_hash.encoding)
       (opt
          "predecessor_ops_metadata_hash"
          Operation_metadata_list_list_hash.encoding))

let default_snapshot_filename (metadata : Snapshot_metadata.t) =
  let chain_name, block_hash, level, history_mode =
    match metadata with
    | Current {chain_name; block_hash; level; history_mode; _} ->
        (chain_name, block_hash, level, history_mode)
    | Legacy {chain_name; block_hash; level; history_mode; _} ->
        (chain_name, block_hash, level, history_mode)
  in
  (* The generated filename follows this pattern:
     <NETWORK>-<BLOCK_HASH>-<BLOCK_LEVEL>.<SNAPSHOT_HISTORY_MODE> *)
  let default_name =
    Format.asprintf
      "%a-%a-%ld.%a"
      Distributed_db_version.Name.pp
      chain_name
      Block_hash.pp
      block_hash
      level
      History_mode.pp_short
      history_mode
  in
  let unique_name name =
    let rec aux i =
      let new_name = Format.sprintf "%s-%d" name i in
      if Sys.file_exists new_name then aux (i + 1) else new_name
    in
    aux 1
  in
  if Sys.file_exists default_name then unique_name default_name
  else default_name

let ensure_valid_tmp_snapshot_path snapshot_tmp_dir =
  let open Lwt_result_syntax in
  let path = Naming.dir_path snapshot_tmp_dir in
  let exists = Sys.file_exists path in
  if exists then
    Lwt.catch
      (fun () ->
        let*! () = Event.(emit cleaning_tmp_export_directory) path in
        let*! () = Lwt_utils_unix.remove_dir path in
        return_unit)
      (function
        | _ ->
            fail_when
              exists
              (Cannot_remove_tmp_export_directory
                 (Naming.dir_path snapshot_tmp_dir)))
  else return_unit

let ensure_valid_export_path =
  let open Lwt_result_syntax in
  function
  | Some path -> fail_when (Sys.file_exists path) (Invalid_export_path path)
  | None -> return_unit

let clean_all paths =
  List.iter_s
    (fun path ->
      Unit.catch_s (fun () ->
          if Sys.is_directory path then Lwt_utils_unix.remove_dir path
          else Lwt_unix.unlink path))
    paths

(* This module allows to create a tar archive by adding files to it,
   one by one. It can be seen as a list of contiguous files (made of a
   header followed by a raw data) closed by a specific end of file
   flag. *)
module Onthefly : sig
  (* The type of a file contained in the tar archive. It is basically
     a header and a raw data. *)
  type file

  (* The type of an output tar archive. *)
  type o

  (* The type of an input tar archive. *)
  type i

  (* output utilities *)

  (* [open_out ~file] opens a tar archive as an output archive located at
     [file]. *)
  val open_out : file:string -> o Lwt.t

  (* [close_out tar] closes an output tar archive. *)
  val close_out : o -> unit Lwt.t

  (* [add_raw_and_finalize tar ~f ~filename] exposes a file
     descriptor of the tar archive through [f] to be able to write
     arbitrary data in the [tar]. When [f] terminates, a valid tar
     header referenced by [filename] is written *)
  val add_raw_and_finalize :
    o -> f:(Lwt_unix.file_descr -> 'a Lwt.t) -> filename:string -> 'a Lwt.t

  (* [add_file_and_finalize tar ~file ~filename] copies the [file], and
     reference it through the given [filename], into a [tar]. It handles all
     specific operations an returns a handler ready to be enriched. *)
  val add_file_and_finalize : o -> file:string -> filename:string -> unit Lwt.t

  (* [add_directory_and_finalize ?archive_prefix tar ~dir_path] copies
     the [dir_path] and all its sub directories or files into a
     [tar]. By default, the tar archive file path are similar to the
     [dir_path]. They can be overridden using the [archive_prefix].
     It handles all specific operations an returns a handler ready to
     be enriched.
     For example,
     if the directory `/path/to/data` contains 2 files `a` and `b`:
     With the default behaviour, the tar will contain two files:
         - `/path/to/data/a`
         - `/path/to/data/b`
     If the archive_prefix is given with value `local_path`, the tar
     archive will contain:
        - `local_path/a`
        - `local_path/b`
  *)
  val add_directory_and_finalize :
    ?archive_prefix:string -> o -> dir_path:string -> unit Lwt.t

  (* input utilities *)

  (* [open_out ~file] opens a tar archive as an input archive located at
     [file]. *)
  val open_in : file:string -> i Lwt.t

  (* [close_in tar] closes an input tar archive. *)
  val close_in : i -> unit Lwt.t

  (* [list_files tar] returns the list of files contained in the
     [tar]. *)
  val list_files : i -> file list Lwt.t

  (* [get_file tar ~filename] returns the first occurrence of the
     file name [filename] from [tar]. *)
  val get_file : i -> filename:string -> file option Lwt.t

  (* [get_filename file] returns the file name of a [file] contained
     in a tar. *)
  val get_filename : file -> string

  (* [get_file_size file] returns the file size of a [file] contained
     in a tar. *)
  val get_file_size : file -> int64

  (* [get_raw_input_fd tar] returns the file descriptor to read
     directly in the tar file. It is no recommended to use it. *)
  val get_raw_input_fd : i -> Lwt_unix.file_descr

  (* [get_raw_file_ofs file] returns the position offset, from the
     beginning of the tar archive, of the given [file]. *)
  val get_raw_file_ofs : file -> int64

  (* [find_file tar ~filename] returns the file corresponding to the
     given [filename] within the given [tar]. *)
  val find_file : i -> filename:string -> file option Lwt.t

  (* [find_files_with_common_path tar ~pattern] returns, from the [tar] all
      the files matching the given [pattern]. *)
  val find_files_with_common_path : i -> pattern:string -> file list Lwt.t

  (* [read_raw tar file] returns a file descriptor on the [tar] file
     which is pointing toward the data of the given [file] *)
  val read_raw : i -> file -> Lwt_unix.file_descr Lwt.t

  (* [load_file tar file] loads the [file] from the [tar] and returns
     it as bytes.
     Warning, this function loads the whole data in
     memory. *)
  val load_file : i -> file -> string Lwt.t

  (* [load_from_filename tar ~filename] loads the file with the name
     [filename] from the given [tar] and returns it as
     bytes.
     Warning, this function loads the whole data in memory *)
  val load_from_filename : i -> filename:string -> string option Lwt.t

  (* [copy_to_file tar file ~dst] copies the [file] from the [tar]
     into new file designated by [dst]. *)
  val copy_to_file : i -> file -> dst:string -> unit Lwt.t
end = struct
  include Tar

  module Reader = struct
    type in_channel = Lwt_unix.file_descr

    type 'a t = 'a Lwt.t

    let really_read fd = Lwt_cstruct.(complete (read fd))

    let skip (ifd : Lwt_unix.file_descr) (n : int) =
      let open Lwt_syntax in
      let buffer_size = 32768 in
      let buffer = Cstruct.create buffer_size in
      let rec loop (n : int) =
        if n <= 0 then Lwt.return ()
        else
          let amount = min n buffer_size in
          let block = Cstruct.sub buffer 0 amount in
          let* () = really_read ifd block in
          loop (n - amount)
      in
      loop n
  end

  module Writer = struct
    type out_channel = Lwt_unix.file_descr

    type 'a t = 'a Lwt.t

    let really_write fd = Lwt_cstruct.(complete (write fd))
  end

  module HR = Tar.HeaderReader (Lwt) (Reader)
  module HW = Tar.HeaderWriter (Lwt) (Writer)

  type file = {header : Tar.Header.t; data_ofs : Int64.t}

  type o = {
    mutable current_pos : Int64.t;
    mutable data_pos : Int64.t;
    fd : Lwt_unix.file_descr;
  }

  let open_out ~file =
    let open Lwt_syntax in
    let* fd =
      Lwt_unix.openfile file Unix.[O_WRONLY; O_CREAT] snapshot_rw_file_perm
    in
    let data_pos = Int64.of_int Header.length in
    let* _ = Lwt_unix.LargeFile.lseek fd data_pos SEEK_SET in
    Lwt.return {current_pos = 0L; data_pos; fd}

  (* Writes the double zero blocks to close the archive, as it is
     defined in the RFC.*)
  let close_out t =
    let open Lwt_syntax in
    let* _eof = Lwt_unix.LargeFile.lseek t.fd t.current_pos SEEK_SET in
    let* () = Writer.really_write t.fd Tar.Header.zero_block in
    let* () = Writer.really_write t.fd Tar.Header.zero_block in
    Lwt_unix.close t.fd

  (* Builds a tar header for the given sequence of bytes *)
  let header_of_bytes ?level ~filename ~data_size (file : Lwt_unix.file_descr) :
      Header.t Lwt.t =
    let open Lwt_syntax in
    let level =
      match level with None -> Tar.Header.V7 | Some level -> level
    in
    (* Use Posix by default instead of V7? *)
    let* stat = Lwt_unix.LargeFile.fstat file in
    let* pwent = Lwt_unix.getpwuid stat.Lwt_unix.LargeFile.st_uid in
    let* grent = Lwt_unix.getgrgid stat.Lwt_unix.LargeFile.st_gid in
    let file_mode = stat.Lwt_unix.LargeFile.st_perm in
    let user_id = stat.Lwt_unix.LargeFile.st_uid in
    let group_id = stat.Lwt_unix.LargeFile.st_gid in
    let mod_time = Int64.of_float stat.Lwt_unix.LargeFile.st_mtime in
    let link_indicator = Tar.Header.Link.Normal in
    let link_name = "" in
    let uname = if level = V7 then "" else pwent.Lwt_unix.pw_name in
    let gname = if level = V7 then "" else grent.Lwt_unix.gr_name in
    let devmajor =
      if level = Ustar then stat.Lwt_unix.LargeFile.st_dev else 0
    in
    let devminor =
      if level = Ustar then stat.Lwt_unix.LargeFile.st_rdev else 0
    in
    Lwt.return
      (Tar.Header.make
         ~file_mode
         ~user_id
         ~group_id
         ~mod_time
         ~link_indicator
         ~link_name
         ~uname
         ~gname
         ~devmajor
         ~devminor
         filename
         data_size)

  (* [finalize tar ~bytes_written ~filename] writes the header
     corresponding to the quantity of data given through
     [bytes_written] in the [tar]. Then, it finalizes the file and returns a new
     handle. The file descriptor of that handle is positioned to allow
     writing data. *)
  let finalize t ~bytes_written ~filename =
    let open Lwt_syntax in
    (* Build the header based of the bytes_written *)
    let* header = header_of_bytes ~filename ~data_size:bytes_written t.fd in
    let header_length = Int64.of_int Header.length in
    (* Compute and right the adequate padding for finalizing a block data *)
    let c = Tar.Header.zero_padding header in
    let zero_padding = Cstruct.to_bytes c in
    let zero_padding_length = Bytes.length zero_padding in
    (* Make sure that the fd position is after the written data *)
    let* _ =
      Lwt_unix.LargeFile.lseek
        t.fd
        (Int64.add t.data_pos bytes_written)
        SEEK_SET
    in
    let* _ = Lwt_unix.write t.fd zero_padding 0 zero_padding_length in
    (* Go back to the header position to write it *)
    let* _ = Lwt_unix.LargeFile.lseek t.fd t.current_pos SEEK_SET in
    let* () = HW.write header t.fd in
    let next_block_start =
      Int64.(
        add
          (add t.current_pos header_length)
          (add bytes_written (of_int zero_padding_length)))
    in
    let next_data_pos = Int64.(add next_block_start header_length) in
    (* Set fd position to be ready for next data write *)
    let* _ = Lwt_unix.LargeFile.lseek t.fd next_data_pos SEEK_SET in
    t.current_pos <- next_block_start ;
    t.data_pos <- next_data_pos ;
    Lwt.return_unit

  let add_raw_and_finalize t ~f ~filename =
    let open Lwt_syntax in
    let* res =
      Lwt.catch
        (fun () -> f t.fd)
        (function
          | exn ->
              (* Rewind file descriptor to the start of the current data
                 slot. Then, the next write will overwrite the corrupted
                 data. *)
              let* _ = Lwt_unix.LargeFile.lseek t.fd t.data_pos SEEK_SET in
              Lwt.fail exn)
    in
    let* eor = Lwt_unix.LargeFile.lseek t.fd 0L SEEK_CUR in
    let bytes_written = Int64.sub eor t.data_pos in
    let* () = finalize t ~bytes_written ~filename in
    Lwt.return res

  let copy_n ifd ofd n =
    let open Lwt_syntax in
    let block_size = 32768 in
    let buffer = Cstruct.create block_size in
    let rec loop remaining =
      if remaining = 0L then Lwt.return ()
      else
        let this = Int64.(to_int (min (of_int block_size) remaining)) in
        let block = Cstruct.sub buffer 0 this in
        let* () = Reader.really_read ifd block in
        let* () = Writer.really_write ofd block in
        loop Int64.(sub remaining (of_int this))
    in
    loop n

  let add_file_and_finalize tar ~file ~filename =
    let open Lwt_syntax in
    let* fd = Lwt_unix.openfile file [Unix.O_RDONLY] snapshot_ro_file_perm in
    let* stat = Lwt_unix.LargeFile.fstat fd in
    let file_size = stat.st_size in
    let* () = copy_n fd tar.fd file_size in
    let* () = finalize tar ~bytes_written:file_size ~filename in
    let* () = Lwt_unix.close fd in
    Lwt.return_unit

  let rec readdir dir_handler =
    let open Lwt_syntax in
    Option.catch_os
      ~catch_only:(function End_of_file -> true | _ -> false)
      (fun () ->
        let* d = Lwt_unix.readdir dir_handler in
        match d with
        | filename
          when filename = Filename.current_dir_name
               || filename = Filename.parent_dir_name ->
            readdir dir_handler
        | any -> Lwt.return_some any)

  let enumerate path =
    let open Lwt_syntax in
    let rec aux prefix dir_handler acc =
      let* o = readdir dir_handler in
      match o with
      | Some any ->
          let full_path = Filename.concat prefix any in
          if Sys.is_directory full_path then
            let* new_dir_handler = Lwt_unix.opendir full_path in
            let* sub_folder = aux full_path new_dir_handler [] in
            let* () = Lwt_unix.closedir new_dir_handler in
            aux prefix dir_handler (sub_folder @ acc)
          else aux prefix dir_handler (full_path :: acc)
      | None -> Lwt.return acc
    in
    let* dir_handler = Lwt_unix.opendir path in
    let* res = aux path dir_handler [] in
    let* () = Lwt_unix.closedir dir_handler in
    Lwt.return res

  let add_directory_and_finalize ?archive_prefix tar ~dir_path =
    let open Lwt_syntax in
    let dir_prefix = Filename.dirname dir_path in
    let* file_paths = enumerate dir_path in
    let archive_prefix = Option.value archive_prefix ~default:dir_prefix in
    let files =
      let dir_length = String.length dir_prefix in
      List.map
        (fun file_path ->
          let filename =
            String.sub
              file_path
              (dir_length + 1)
              String.(length file_path - dir_length - 1)
          in
          (file_path, filename))
        file_paths
    in
    List.iter_s
      (fun (file, filename) ->
        add_file_and_finalize
          tar
          ~file
          ~filename:Filename.(concat archive_prefix filename))
      files

  type i = {
    mutable current_pos : Int64.t;
    mutable data_pos : Int64.t;
    fd : Lwt_unix.file_descr;
    mutable files : file list option;
  }

  let open_in ~file =
    let open Lwt_syntax in
    let* fd = Lwt_unix.openfile file Unix.[O_RDONLY] snapshot_ro_file_perm in
    let data_pos = Int64.of_int Header.length in
    let files = None in
    Lwt.return {current_pos = 0L; data_pos; fd; files}

  let close_in t = Lwt_unix.close t.fd

  (*[list_files tar] returns the list of files contained in the
     [tar]. *)
  let list_files t =
    let open Lwt_syntax in
    let* _ = Lwt_unix.LargeFile.lseek t.fd 0L SEEK_SET in
    (* This implementation is way faster than the one implemented in
       Tar_lwt_unix.Archive.list function which reads the whole file
    *)
    let rec loop pos acc =
      let* _ = Lwt_unix.LargeFile.lseek t.fd pos SEEK_SET in
      let* _ = Lwt_unix.lseek t.fd 0 SEEK_CUR in
      let* r = HR.read t.fd in
      match r with
      | Error `Eof -> Lwt.return (List.rev acc)
      | Ok hdr ->
          (* Header length can be 1024 if extended *)
          let* data_pos = Lwt_unix.LargeFile.lseek t.fd 0L SEEK_CUR in
          let header_length = Int64.sub data_pos pos in
          let file_size = hdr.Tar.Header.file_size in
          let padding =
            Int64.of_int (Tar.Header.compute_zero_padding_length hdr)
          in
          let next_header = Int64.(add (add file_size padding) header_length) in
          let* _ = Lwt_unix.LargeFile.lseek t.fd next_header SEEK_SET in
          let h = {header = hdr; data_ofs = data_pos} in
          loop (Int64.add pos next_header) (h :: acc)
    in
    loop 0L []

  let update_files t files = t.files <- Some files

  let may_update_files t files =
    match t.files with Some _ -> () | None -> update_files t files

  let get_files t =
    let open Lwt_syntax in
    match t.files with
    | Some files -> Lwt.return files
    | None ->
        let* files = list_files t in
        update_files t files ;
        Lwt.return files

  let get_file tar ~filename =
    let open Lwt_syntax in
    let* files = get_files tar in
    Lwt.return
      (List.find_opt (fun {header; _} -> header.file_name = filename) files)

  let get_filename {header; _} = header.Tar.Header.file_name

  let get_file_size {header; _} = header.Tar.Header.file_size

  (*[get_raw tar file] loads the [file] from [tar] in memory *)
  let get_raw t {header; data_ofs} =
    let open Lwt_syntax in
    let* _ = Lwt_unix.LargeFile.lseek t.fd data_ofs SEEK_SET in
    let data_size = Int64.to_int header.file_size in
    let buf = Bytes.create data_size in
    let* _ = Lwt_unix.read t.fd buf 0 data_size in
    Lwt.return (Bytes.unsafe_to_string buf)

  let get_raw_input_fd {fd; _} = fd

  let get_raw_file_ofs {data_ofs; _} = data_ofs

  let find_file t ~filename =
    let open Lwt_syntax in
    (* If the files were already listed, there is no need to read the whole tar archive.*)
    match t.files with
    | Some _ -> get_file t ~filename
    | None ->
        let* _ = Lwt_unix.LargeFile.lseek t.fd 0L SEEK_SET in
        let rec loop pos acc =
          let* _ = Lwt_unix.LargeFile.lseek t.fd pos SEEK_SET in
          let* _ = Lwt_unix.lseek t.fd 0 SEEK_CUR in
          let* r = HR.read t.fd in
          match r with
          | Error `Eof ->
              (* If the end of file is reached, all the files were
                 enumerated without finding the expected one. In this case,
                 the files are updated. *)
              may_update_files t acc ;
              Lwt.return_none
          | Ok hdr ->
              (* Header length can be 1024 if extended *)
              let* data_pos = Lwt_unix.LargeFile.lseek t.fd 0L SEEK_CUR in
              if hdr.file_name = filename then
                Lwt.return_some {header = hdr; data_ofs = data_pos}
              else
                let header_length = Int64.sub data_pos pos in
                let file_size = hdr.Tar.Header.file_size in
                let padding =
                  Int64.of_int (Tar.Header.compute_zero_padding_length hdr)
                in
                let next_header =
                  Int64.(add (add file_size padding) header_length)
                in
                let* _ = Lwt_unix.LargeFile.lseek t.fd next_header SEEK_SET in
                let h = {header = hdr; data_ofs = data_pos} in
                loop (Int64.add pos next_header) (h :: acc)
        in
        loop 0L []

  let find_files_with_common_path t ~pattern =
    let open Lwt_syntax in
    let* files = get_files t in
    let pattern = Re.compile (Re.Perl.re pattern) in
    Lwt.return
      (List.filter
         (fun {header; _} -> Re.execp pattern header.Tar.Header.file_name)
         files)

  let read_raw t {data_ofs; _} =
    let open Lwt_syntax in
    let* _ = Lwt_unix.LargeFile.lseek t.fd data_ofs SEEK_SET in
    Lwt.return t.fd

  let load_file t file = get_raw t file

  let load_from_filename t ~filename =
    let open Lwt_syntax in
    let* o = get_file t ~filename in
    match o with
    | Some hd ->
        let* str = get_raw t hd in
        Lwt.return_some str
    | None -> Lwt.return_none

  let copy_to_file tar {header; data_ofs} ~dst =
    let open Lwt_syntax in
    let* _ = Lwt_unix.LargeFile.lseek tar.fd data_ofs SEEK_SET in
    let* fd =
      Lwt_unix.openfile
        dst
        Unix.[O_WRONLY; O_CREAT; O_TRUNC]
        snapshot_rw_file_perm
    in
    Lwt.finalize
      (fun () -> copy_n tar.fd fd header.Tar.Header.file_size)
      (fun () -> Lwt_unix.close fd)
end

module type EXPORTER = sig
  type t

  val init : string option -> t tzresult Lwt.t

  val write_block_data :
    t ->
    predecessor_header:Block_header.t ->
    predecessor_block_metadata_hash:Block_metadata_hash.t option ->
    predecessor_ops_metadata_hash:Operation_metadata_list_list_hash.t option ->
    export_block:Store.Block.t ->
    resulting_context_hash:Context_hash.t ->
    unit Lwt.t

  val export_context :
    t -> Context.index -> Context_hash.t -> unit tzresult Lwt.t

  val copy_cemented_block :
    t -> file:string -> start_level:int32 -> end_level:int32 -> unit Lwt.t

  val create_cemented_block_indexes :
    t ->
    Cemented_block_store.Cemented_block_level_index.t
    * Cemented_block_store.Cemented_block_hash_index.t

  (* Removes potential lockfiles that could be created by the index
     library to prevent writes during an internal index maintenance
     procedure. *)
  val clear_cemented_block_indexes_lockfiles : t -> unit Lwt.t

  val filter_cemented_block_indexes : t -> limit:int32 -> unit

  val write_floating_blocks :
    t -> f:(Lwt_unix.file_descr -> 'a Lwt.t) -> 'a Lwt.t

  val write_protocols_table :
    t -> f:(Lwt_unix.file_descr -> 'a Lwt.t) -> 'a Lwt.t

  val copy_protocol : t -> src:string -> dst_ph:Protocol_hash.t -> unit Lwt.t

  val cleaner : ?to_clean:string list -> t -> unit Lwt.t

  val finalize : t -> Snapshot_metadata.t -> string tzresult Lwt.t
end

module Raw_exporter : EXPORTER = struct
  type t = {
    snapshot_dir : string option;
    snapshot_tmp_dir : [`Snapshot_tmp_dir] Naming.directory;
    snapshot_cemented_dir : [`Cemented_blocks_dir] Naming.directory;
    snapshot_protocol_dir : [`Protocol_dir] Naming.directory;
  }

  let init snapshot_dir =
    let open Lwt_result_syntax in
    (* Creates the requested export folder and its hierarchy *)
    let snapshot_tmp_dir =
      let tmp_dir = Naming.snapshot_dir ?snapshot_path:snapshot_dir () in
      Naming.snapshot_tmp_dir tmp_dir
    in
    let* () = ensure_valid_export_path snapshot_dir in
    let* () = ensure_valid_tmp_snapshot_path snapshot_tmp_dir in
    let*! () =
      Lwt_unix.mkdir (Naming.dir_path snapshot_tmp_dir) snapshot_dir_perm
    in
    let snapshot_cemented_dir = Naming.cemented_blocks_dir snapshot_tmp_dir in
    let*! () =
      Lwt_unix.mkdir (Naming.dir_path snapshot_cemented_dir) snapshot_dir_perm
    in
    let snapshot_protocol_dir = Naming.protocol_store_dir snapshot_tmp_dir in
    let*! () =
      Lwt_unix.mkdir (Naming.dir_path snapshot_protocol_dir) snapshot_dir_perm
    in
    let version_file =
      Naming.snapshot_version_file snapshot_tmp_dir |> Naming.file_path
    in
    let version_json =
      Data_encoding.Json.construct Version.encoding Version.current_version
    in
    let* () = Lwt_utils_unix.Json.write_file version_file version_json in
    return
      {
        snapshot_dir;
        snapshot_tmp_dir;
        snapshot_cemented_dir;
        snapshot_protocol_dir;
      }

  let write_block_data t ~predecessor_header ~predecessor_block_metadata_hash
      ~predecessor_ops_metadata_hash ~export_block ~resulting_context_hash =
    let open Lwt_syntax in
    let block_data =
      {
        block_header = Store.Block.header export_block;
        operations = Store.Block.operations export_block;
        predecessor_header;
        predecessor_block_metadata_hash;
        predecessor_ops_metadata_hash;
        resulting_context_hash;
      }
    in
    let bytes =
      Data_encoding.Binary.to_bytes_exn block_data_encoding block_data
    in
    let file =
      Naming.(snapshot_block_data_file t.snapshot_tmp_dir |> file_path)
    in
    let* fd =
      Lwt_unix.openfile
        file
        Unix.[O_CREAT; O_TRUNC; O_WRONLY]
        snapshot_rw_file_perm
    in
    Lwt.finalize
      (fun () -> Lwt_utils_unix.write_bytes fd bytes)
      (fun () -> Lwt_unix.close fd)

  let export_context t context_index context_hash =
    let open Lwt_result_syntax in
    let tmp_context_path =
      Naming.(snapshot_context_file t.snapshot_tmp_dir |> file_path)
    in
    let*! () =
      Context.export_snapshot context_index context_hash ~path:tmp_context_path
    in
    return_unit

  let copy_cemented_block t ~file ~start_level ~end_level =
    let filename =
      Naming.(
        cemented_blocks_file t.snapshot_cemented_dir ~start_level ~end_level
        |> file_path)
    in
    Lwt_utils_unix.copy_file ~src:file ~dst:filename

  let create_cemented_block_indexes t =
    let open Cemented_block_store in
    let fresh_level_index =
      Cemented_block_level_index.v
        ~fresh:true
        ~readonly:false
        ~log_size:cemented_import_log_size
        Naming.(
          cemented_blocks_level_index_dir t.snapshot_cemented_dir |> dir_path)
    in
    let fresh_hash_index =
      Cemented_block_hash_index.v
        ~fresh:true
        ~readonly:false
        ~log_size:cemented_import_log_size
        Naming.(
          cemented_blocks_hash_index_dir t.snapshot_cemented_dir |> dir_path)
    in
    (fresh_level_index, fresh_hash_index)

  let clear_cemented_block_indexes_lockfiles t =
    let open Lwt_syntax in
    let* () =
      Lwt.catch
        (fun () ->
          Lwt_unix.unlink
            Naming.(
              file_path
                (cemented_blocks_hash_lock_file
                   (cemented_blocks_hash_index_dir
                      (cemented_blocks_dir t.snapshot_tmp_dir)))))
        (function
          | Unix.Unix_error (ENOENT, _, _) -> Lwt.return_unit
          | exn -> Lwt.fail exn)
    in
    Lwt.catch
      (fun () ->
        Lwt_unix.unlink
          Naming.(
            file_path
              (cemented_blocks_level_lock_file
                 (cemented_blocks_level_index_dir
                    (cemented_blocks_dir t.snapshot_tmp_dir)))))
      (function
        | Unix.Unix_error (ENOENT, _, _) -> Lwt.return_unit
        | exn -> Lwt.fail exn)

  let filter_cemented_block_indexes t ~limit =
    let open Cemented_block_store in
    let fresh_level_index =
      Cemented_block_level_index.v
        ~fresh:false
        ~readonly:false
        ~log_size:10_000
        Naming.(
          cemented_blocks_level_index_dir t.snapshot_cemented_dir |> dir_path)
    in
    let fresh_hash_index =
      Cemented_block_hash_index.v
        ~fresh:false
        ~readonly:false
        ~log_size:10_000
        Naming.(
          cemented_blocks_hash_index_dir t.snapshot_cemented_dir |> dir_path)
    in
    Cemented_block_level_index.filter fresh_level_index (fun (_, level) ->
        level <= limit) ;
    Cemented_block_hash_index.filter fresh_hash_index (fun (level, _) ->
        level <= limit) ;
    Cemented_block_level_index.close fresh_level_index ;
    Cemented_block_hash_index.close fresh_hash_index

  let write_floating_blocks t ~f =
    let open Lwt_syntax in
    let floating_file =
      Naming.(snapshot_floating_blocks_file t.snapshot_tmp_dir |> file_path)
    in
    let* fd =
      Lwt_unix.openfile
        floating_file
        Unix.[O_CREAT; O_TRUNC; O_WRONLY]
        snapshot_rw_file_perm
    in
    Lwt.finalize (fun () -> f fd) (fun () -> Lwt_unix.close fd)

  let write_protocols_table t ~f =
    let open Lwt_syntax in
    let* fd =
      Lwt_unix.openfile
        Naming.(
          snapshot_protocol_levels_file t.snapshot_tmp_dir |> encoded_file_path)
        Unix.[O_CREAT; O_TRUNC; O_WRONLY]
        snapshot_rw_file_perm
    in
    Lwt.finalize (fun () -> f fd) (fun () -> Lwt_unix.close fd)

  let copy_protocol t ~src ~dst_ph =
    let dst =
      Naming.(
        protocol_file (protocol_store_dir t.snapshot_tmp_dir) dst_ph
        |> file_path)
    in
    Lwt_utils_unix.copy_file ~src ~dst

  let write_metadata t (metadata : Snapshot_metadata.t) =
    let metadata_file =
      Naming.(snapshot_metadata_file t.snapshot_tmp_dir |> file_path)
    in
    let metadata_json =
      match metadata with
      | Current metadata ->
          Data_encoding.Json.(
            construct Snapshot_metadata.metadata_encoding metadata)
      | Legacy _ ->
          (* The export of legacy snapshots is not allowed. *)
          assert false
    in
    Lwt_utils_unix.Json.write_file metadata_file metadata_json

  let cleaner ?to_clean t =
    let open Lwt_syntax in
    let* () = Event.(emit cleaning_after_failure ()) in
    let paths =
      match to_clean with
      | Some paths -> paths
      | None -> [Naming.dir_path t.snapshot_tmp_dir]
    in
    clean_all paths

  let finalize t metadata =
    let open Lwt_result_syntax in
    let snapshot_filename =
      match t.snapshot_dir with
      | Some path -> path
      | None -> default_snapshot_filename metadata
    in
    let* () = write_metadata t metadata in
    protect
      ~on_error:(fun errors ->
        let*! () = cleaner ~to_clean:[Naming.dir_path t.snapshot_tmp_dir] t in
        Lwt.return (Error errors))
      (fun () ->
        let*! () =
          Lwt_unix.rename (Naming.dir_path t.snapshot_tmp_dir) snapshot_filename
        in
        return snapshot_filename)
end

module Tar_exporter : EXPORTER = struct
  type t = {
    snapshot_file : string option;
    snapshot_tar : [`Tar_archive] Naming.directory;
    snapshot_tar_file : [`Snapshot_tar_file] Naming.file;
    snapshot_tmp_dir : [`Snapshot_tmp_dir] Naming.directory;
    snapshot_tmp_cemented_dir : [`Cemented_blocks_dir] Naming.directory;
    snapshot_cemented_dir : [`Cemented_blocks_dir] Naming.directory;
    snapshot_protocol_dir : [`Protocol_dir] Naming.directory;
    tar : Onthefly.o;
  }

  let init snapshot_file =
    let open Lwt_result_syntax in
    (* Creates the requested export folder and its hierarchy *)
    let snapshot_tmp_dir =
      let tmp_dir = Naming.snapshot_dir ?snapshot_path:snapshot_file () in
      Naming.snapshot_tmp_dir tmp_dir
    in
    let* () = ensure_valid_export_path snapshot_file in
    let* () = ensure_valid_tmp_snapshot_path snapshot_tmp_dir in
    let*! () =
      Lwt_unix.mkdir (Naming.dir_path snapshot_tmp_dir) snapshot_dir_perm
    in
    let snapshot_tmp_cemented_dir =
      Naming.cemented_blocks_dir snapshot_tmp_dir
    in
    let snapshot_tar = Naming.snapshot_tar_root in
    let snapshot_cemented_dir = Naming.cemented_blocks_dir snapshot_tar in
    let snapshot_protocol_dir = Naming.protocol_store_dir snapshot_tar in
    let snapshot_tar_file = Naming.snapshot_tmp_tar_file snapshot_tmp_dir in
    let*! tar =
      Onthefly.open_out ~file:(snapshot_tar_file |> Naming.file_path)
    in
    let version_file =
      Naming.snapshot_version_file snapshot_tmp_dir |> Naming.file_path
    in
    let version_json =
      Data_encoding.Json.construct Version.encoding Version.current_version
    in
    let* () = Lwt_utils_unix.Json.write_file version_file version_json in
    let*! () =
      Onthefly.add_file_and_finalize
        tar
        ~file:version_file
        ~filename:(Filename.basename version_file)
    in
    return
      {
        snapshot_file;
        snapshot_tar;
        snapshot_tar_file;
        snapshot_tmp_dir;
        snapshot_tmp_cemented_dir;
        snapshot_cemented_dir;
        snapshot_protocol_dir;
        tar;
      }

  let write_block_data t ~predecessor_header ~predecessor_block_metadata_hash
      ~predecessor_ops_metadata_hash ~export_block ~resulting_context_hash =
    let block_data =
      {
        block_header = Store.Block.header export_block;
        operations = Store.Block.operations export_block;
        predecessor_header;
        predecessor_block_metadata_hash;
        predecessor_ops_metadata_hash;
        resulting_context_hash;
      }
    in
    let bytes =
      Data_encoding.Binary.to_bytes_exn block_data_encoding block_data
    in
    Onthefly.add_raw_and_finalize
      t.tar
      ~f:(fun fd -> Lwt_utils_unix.write_bytes fd bytes)
      ~filename:Naming.(snapshot_block_data_file t.snapshot_tar |> file_path)

  let export_context t context_index context_hash =
    let open Lwt_result_syntax in
    let tmp_context_path =
      Naming.(snapshot_context_file t.snapshot_tmp_dir |> file_path)
    in
    let*! () =
      Context.export_snapshot context_index context_hash ~path:tmp_context_path
    in
    let*! () =
      Onthefly.add_directory_and_finalize
        ~archive_prefix:"" (* /context/ was already added *)
        t.tar
        ~dir_path:tmp_context_path
    in
    let*! () = Lwt_utils_unix.remove_dir tmp_context_path in
    return_unit

  let copy_cemented_block t ~file ~start_level ~end_level =
    let cemented_filename =
      Naming.(
        cemented_blocks_file t.snapshot_cemented_dir ~start_level ~end_level
        |> file_path)
    in
    Onthefly.add_file_and_finalize t.tar ~file ~filename:cemented_filename

  let create_cemented_block_indexes t =
    let open Cemented_block_store in
    let fresh_level_index =
      Cemented_block_level_index.v
        ~fresh:true
        ~readonly:false
        ~log_size:cemented_import_log_size
        Naming.(
          cemented_blocks_level_index_dir t.snapshot_tmp_cemented_dir
          |> dir_path)
    in
    let fresh_hash_index =
      Cemented_block_hash_index.v
        ~fresh:true
        ~readonly:false
        ~log_size:cemented_import_log_size
        Naming.(
          cemented_blocks_hash_index_dir t.snapshot_tmp_cemented_dir |> dir_path)
    in
    (fresh_level_index, fresh_hash_index)

  let clear_cemented_block_indexes_lockfiles t =
    let open Lwt_syntax in
    let* () =
      Lwt.catch
        (fun () ->
          Lwt_unix.unlink
            Naming.(
              file_path
                (cemented_blocks_hash_lock_file
                   (cemented_blocks_hash_index_dir t.snapshot_tmp_cemented_dir))))
        (function
          | Unix.Unix_error (ENOENT, _, _) -> Lwt.return_unit
          | exn -> Lwt.fail exn)
    in
    let* () =
      Lwt.catch
        (fun () ->
          Lwt_unix.unlink
            Naming.(
              file_path
                (cemented_blocks_level_lock_file
                   (cemented_blocks_level_index_dir t.snapshot_tmp_cemented_dir))))
        (function
          | Unix.Unix_error (ENOENT, _, _) -> Lwt.return_unit
          | exn -> Lwt.fail exn)
    in
    let* () =
      Onthefly.add_directory_and_finalize
        ~archive_prefix:(Naming.dir_path t.snapshot_cemented_dir)
        t.tar
        ~dir_path:
          Naming.(
            cemented_blocks_hash_index_dir t.snapshot_tmp_cemented_dir
            |> dir_path)
    in
    Onthefly.add_directory_and_finalize
      ~archive_prefix:(Naming.dir_path t.snapshot_cemented_dir)
      t.tar
      ~dir_path:
        Naming.(
          cemented_blocks_level_index_dir t.snapshot_tmp_cemented_dir
          |> dir_path)

  let filter_cemented_block_indexes t ~limit =
    let open Cemented_block_store in
    let fresh_level_index =
      Cemented_block_level_index.v
        ~fresh:false
        ~readonly:false
        ~log_size:10_000
        Naming.(
          cemented_blocks_level_index_dir t.snapshot_tmp_cemented_dir
          |> dir_path)
    in
    let fresh_hash_index =
      Cemented_block_hash_index.v
        ~fresh:false
        ~readonly:false
        ~log_size:10_000
        Naming.(
          cemented_blocks_hash_index_dir t.snapshot_tmp_cemented_dir |> dir_path)
    in
    Cemented_block_level_index.filter fresh_level_index (fun (_, level) ->
        level <= limit) ;
    Cemented_block_hash_index.filter fresh_hash_index (fun (level, _) ->
        level <= limit) ;
    Cemented_block_level_index.close fresh_level_index ;
    Cemented_block_hash_index.close fresh_hash_index

  let write_floating_blocks t ~f =
    Onthefly.add_raw_and_finalize
      t.tar
      ~f
      ~filename:
        Naming.(snapshot_floating_blocks_file t.snapshot_tar |> file_path)

  let write_protocols_table t ~f =
    Onthefly.add_raw_and_finalize
      t.tar
      ~f
      ~filename:
        Naming.(
          snapshot_protocol_levels_file t.snapshot_tar |> encoded_file_path)

  let copy_protocol t ~src ~dst_ph =
    let dst =
      Filename.(
        concat
          (Naming.dir_path t.snapshot_protocol_dir)
          (Protocol_hash.to_b58check dst_ph))
    in
    Onthefly.add_file_and_finalize t.tar ~file:src ~filename:dst

  let write_metadata t metadata =
    let open Lwt_result_syntax in
    let metadata_json =
      match metadata with
      | Snapshot_metadata.Current metadata ->
          Data_encoding.Json.(
            construct Snapshot_metadata.metadata_encoding metadata)
      | Legacy metadata ->
          Data_encoding.Json.(
            construct Snapshot_metadata.legacy_metadata_encoding metadata)
    in
    let metadata_file =
      Naming.snapshot_metadata_file t.snapshot_tmp_dir |> Naming.file_path
    in
    let* () = Lwt_utils_unix.Json.write_file metadata_file metadata_json in
    let*! () =
      Onthefly.add_file_and_finalize
        t.tar
        ~file:metadata_file
        ~filename:(Filename.basename metadata_file)
    in
    return_unit

  let cleaner ?to_clean t =
    let open Lwt_syntax in
    let* () = Event.(emit cleaning_after_failure ()) in
    let paths =
      match to_clean with
      | Some paths -> paths
      | None -> [Naming.dir_path t.snapshot_tmp_dir]
    in
    clean_all paths

  let finalize t metadata =
    let open Lwt_result_syntax in
    let snapshot_filename =
      match t.snapshot_file with
      | Some path -> path
      | None -> default_snapshot_filename metadata
    in
    let* () = write_metadata t metadata in
    let*! () = Onthefly.close_out t.tar in
    protect
      ~on_error:(fun errors ->
        let*! () = cleaner ~to_clean:[Naming.dir_path t.snapshot_tmp_dir] t in
        Lwt.return (Error errors))
      (fun () ->
        let*! () =
          Lwt_unix.rename
            Naming.(snapshot_tmp_tar_file t.snapshot_tmp_dir |> file_path)
            snapshot_filename
        in
        let*! () =
          Lwt_utils_unix.remove_dir (Naming.dir_path t.snapshot_tmp_dir)
        in
        return snapshot_filename)
end

module type Snapshot_exporter = sig
  type t

  val export :
    ?snapshot_path:string ->
    ?rolling:bool ->
    block:Block_services.block ->
    store_dir:string ->
    context_dir:string ->
    chain_name:Distributed_db_version.Name.t ->
    progress_display_mode:Animation.progress_display_mode ->
    Genesis.t ->
    unit tzresult Lwt.t
end

module Make_snapshot_exporter (Exporter : EXPORTER) : Snapshot_exporter = struct
  type t = Exporter.t

  let init = Exporter.init

  let copy_cemented_blocks snapshot_exporter ~should_filter_indexes
      ~progress_display_mode
      (files : Cemented_block_store.cemented_blocks_file list) =
    let open Lwt_result_syntax in
    let open Cemented_block_store in
    let nb_cycles = List.length files in
    (* Rebuild fresh indexes: cannot cp because of concurrent accesses *)
    let fresh_level_index, fresh_hash_index =
      Exporter.create_cemented_block_indexes snapshot_exporter
    in
    protect (fun () ->
        let* () =
          Animation.display_progress
            ~pp_print_step:(fun fmt i ->
              Format.fprintf
                fmt
                "Copying cemented blocks and populating indexes: %d/%d cycles"
                i
                nb_cycles)
            ~progress_display_mode
            (fun notify ->
              (* Bound the number of copying threads *)
              List.iter_es
                (fun ({start_level; end_level; file} as cemented_file) ->
                  let* () =
                    Cemented_block_store.iter_cemented_file
                      (fun block ->
                        let hash = Block_repr.hash block in
                        let level = Block_repr.level block in
                        Cemented_block_level_index.replace
                          fresh_level_index
                          hash
                          level ;
                        Cemented_block_hash_index.replace
                          fresh_hash_index
                          level
                          hash ;
                        Lwt.return_unit)
                      cemented_file
                  in
                  let file_path = Naming.file_path file in
                  let*! () =
                    Exporter.copy_cemented_block
                      snapshot_exporter
                      ~file:file_path
                      ~start_level
                      ~end_level
                  in
                  let*! () = notify () in
                  return_unit)
                files)
        in
        Cemented_block_level_index.close fresh_level_index ;
        Cemented_block_hash_index.close fresh_hash_index ;
        let*! () =
          Exporter.clear_cemented_block_indexes_lockfiles snapshot_exporter
        in
        if should_filter_indexes && files <> [] then
          Exporter.filter_cemented_block_indexes
            snapshot_exporter
            ~limit:
              (List.last_opt files |> WithExceptions.Option.get ~loc:__LOC__)
                .end_level ;
        return_unit)

  let write_floating_block fd (block : Block_repr.t) =
    let bytes = Data_encoding.Binary.to_bytes_exn Block_repr.encoding block in
    Lwt_utils_unix.write_bytes ~pos:0 ~len:(Bytes.length bytes) fd bytes

  let export_floating_blocks ~floating_ro_fd ~floating_rw_fd ~export_block =
    let open Lwt_result_syntax in
    let ((limit_hash, limit_level) as export_block_descr) =
      Store.Block.descriptor export_block
    in
    let stream, bpush = Lwt_stream.create_bounded 1000 in
    (* Retrieve first floating block *)
    let* first_block =
      let*! o = Block_repr_unix.read_next_block floating_ro_fd in
      match o with
      | Some (block, _length) -> return block
      | None -> (
          let*! o = Block_repr_unix.read_next_block floating_rw_fd in
          match o with
          | Some (block, _length) -> return block
          | None ->
              (* No block to read *)
              tzfail Empty_floating_store)
    in
    let first_block_level = Block_repr.level first_block in
    if Compare.Int32.(limit_level < first_block_level) then
      tzfail
        (Inconsistent_floating_store
           (export_block_descr, (Block_repr.hash first_block, first_block_level)))
    else
      let exception Done in
      let f block =
        (* FIXME: we also write potential branches, it will eventually
           be GCed *)
        if Compare.Int32.(Block_repr.level block >= limit_level) then
          if Block_hash.equal limit_hash (Block_repr.hash block) then raise Done
          else return_unit
        else
          let block = (* Prune everything  *) {block with metadata = None} in
          let*! () = bpush#push block in
          return_unit
      in
      let reading_thread =
        Lwt.finalize
          (fun () ->
            Lwt.catch
              (fun () ->
                let*! _ = Lwt_unix.lseek floating_ro_fd 0 Unix.SEEK_SET in
                let* () = Floating_block_store.iter_s_raw_fd f floating_ro_fd in
                let*! _ = Lwt_unix.lseek floating_rw_fd 0 Unix.SEEK_SET in
                let* () = Floating_block_store.iter_s_raw_fd f floating_rw_fd in
                tzfail (Missing_target_block export_block_descr))
              (function
                | Done -> return_unit
                | exn ->
                    tzfail (Cannot_read_floating_store (Printexc.to_string exn))))
          (fun () ->
            bpush#close ;
            Lwt.return_unit)
      in
      return (reading_thread, stream)

  (* Export the protocol table (info regarding the protocol transitions)
     as well as all the stored protocols *)
  let export_protocols snapshot_exporter export_block all_protocol_levels
      protocol_store_dir progress_display_mode =
    let open Lwt_syntax in
    let export_proto_level = Store.Block.proto_level export_block in
    (* Export only the protocols with a protocol level below the
       protocol's level of the targeted export block. *)
    let protocol_levels =
      Protocol_levels.filter
        (fun proto_level _ -> proto_level <= export_proto_level)
        all_protocol_levels
    in
    let* () =
      Exporter.write_protocols_table snapshot_exporter ~f:(fun fd ->
          let bytes =
            Data_encoding.Binary.to_bytes_exn
              Protocol_levels.encoding
              protocol_levels
          in
          Lwt_utils_unix.write_bytes ~pos:0 fd bytes)
    in
    let* dir_handle = Lwt_unix.opendir (Naming.dir_path protocol_store_dir) in
    let proto_to_export =
      List.map
        (fun (_, {Protocol_levels.protocol; _}) -> protocol)
        (Protocol_levels.bindings protocol_levels)
    in
    let nb_proto_to_export = List.length proto_to_export in
    Animation.display_progress
      ~pp_print_step:(fun fmt i ->
        Format.fprintf fmt "Copying protocols: %d/%d" i nb_proto_to_export)
      ~progress_display_mode
      (fun notify ->
        let rec copy_protocols () =
          Lwt.catch
            (fun () ->
              let* d = Lwt_unix.readdir dir_handle in
              match d with
              | filename
                when filename = Filename.current_dir_name
                     || filename = Filename.parent_dir_name ->
                  copy_protocols ()
              | filename -> (
                  match Protocol_hash.of_b58check_opt filename with
                  | None -> return_ok_unit
                  | Some ph ->
                      let src_protocol_file =
                        Naming.protocol_file protocol_store_dir ph
                      in
                      let* () =
                        if
                          List.mem ~equal:Protocol_hash.equal ph proto_to_export
                        then
                          let* () =
                            Exporter.copy_protocol
                              snapshot_exporter
                              ~src:(Naming.file_path src_protocol_file)
                              ~dst_ph:ph
                          in
                          notify ()
                        else Lwt.return_unit
                      in
                      copy_protocols ()))
            (function
              | End_of_file -> return_ok_unit | exn -> fail_with_exn exn)
        in
        Lwt.finalize
          (fun () -> copy_protocols ())
          (fun () -> Lwt_unix.closedir dir_handle))

  (* Ensures that the data needed to export the snapshot from the target
     block is available:
     - the target_block is not the genesis
     - the target_block and its predecessor are known
     - the context of the predecessor of the target_block must be known
     - at least max_op_ttl(target_block) headers must be available
  *)
  let check_export_block_validity chain_store block =
    let open Lwt_result_syntax in
    let block_hash, block_level = Store.Block.descriptor block in
    let*! is_known = Store.Block.is_known_valid chain_store block_hash in
    let* () =
      fail_unless
        is_known
        (Invalid_export_block {block = Some block_hash; reason = `Unknown})
    in
    let* () =
      fail_when
        (Store.Block.is_genesis chain_store block_hash)
        (Invalid_export_block {block = Some block_hash; reason = `Genesis})
    in
    let*! _, savepoint_level = Store.Chain.savepoint chain_store in
    let* () =
      fail_when
        Compare.Int32.(savepoint_level > block_level)
        (Invalid_export_block {block = Some block_hash; reason = `Pruned})
    in
    let* block = Store.Block.read_block chain_store block_hash in
    let* pred_block =
      let*! o = Store.Block.read_predecessor_opt chain_store block in
      match o with
      | None ->
          tzfail
            (Invalid_export_block
               {block = Some block_hash; reason = `Not_enough_pred})
      | Some pred_block -> return pred_block
    in
    (* Make sure that the predecessor's context is known *)
    let* pred_context_exists =
      let protocol_level = Store.Block.proto_level block in
      let* expect_predecessor_context_hash =
        Store.Chain.expect_predecessor_context_hash chain_store ~protocol_level
      in
      let*! exists =
        if expect_predecessor_context_hash then
          Store.Block.context_exists chain_store block
        else Store.Block.context_exists chain_store pred_block
      in
      return exists
    in
    (* We also need the predecessor not to be pruned *)
    let* () =
      fail_when
        Compare.Int32.(
          savepoint_level > Int32.pred block_level && not pred_context_exists)
        (Invalid_export_block {block = Some block_hash; reason = `Pruned_pred})
    in
    let* block_metadata =
      let*! o = Store.Block.get_block_metadata_opt chain_store block in
      match o with
      | None ->
          tzfail
            (Invalid_export_block {block = Some block_hash; reason = `Pruned})
      | Some block_metadata -> return block_metadata
    in
    let*! _, caboose_level = Store.Chain.caboose chain_store in
    (* We will need the following blocks
       [ (target_block - max_op_ttl(target_block)) ; ... ; target_block ] *)
    let block_max_op_ttl = Store.Block.max_operations_ttl block_metadata in
    let*! genesis_block = Store.Chain.genesis_block chain_store in
    let genesis_level = Store.Block.level genesis_block in
    let minimum_level_needed =
      Compare.Int32.(
        max genesis_level Int32.(sub block_level (of_int block_max_op_ttl)))
    in
    let* () =
      fail_when
        Compare.Int32.(minimum_level_needed < caboose_level)
        (Invalid_export_block
           {block = Some block_hash; reason = `Not_enough_pred})
    in
    return (pred_block, minimum_level_needed)

  (* Retrieves the block to export based on given block "as hint". As
     the checkpoint is provided as a default value, we must ensure that
     it is valid. It may be not the case when the checkpoint was set in
     the future. In this particular case, the last allowed fork level of
     the current head is chosen. *)
  let retrieve_export_block chain_store block =
    let open Lwt_result_syntax in
    let* export_block =
      (match block with
      | `Genesis ->
          (* Exporting the genesis block does not make sense. *)
          tzfail
            (Invalid_export_block
               {
                 block = Some (Store.Chain.genesis chain_store).Genesis.block;
                 reason = `Genesis;
               })
      | `Alias (`Caboose, distance) when distance >= 0 ->
          (* With the caboose, we do not allow to use the ~/- as it is a
             non sense. Additionally, it is not allowed to export the
             caboose block. *)
          let*! hash, _ = Store.Chain.caboose chain_store in
          tzfail (Invalid_export_block {block = Some hash; reason = `Caboose})
      | _ -> Store.Chain.block_of_identifier chain_store block)
      |> trace (Invalid_export_block {block = None; reason = `Unknown})
    in
    let* pred_block, minimum_level_needed =
      check_export_block_validity chain_store export_block
    in
    return (export_block, pred_block, minimum_level_needed)

  (* Returns the list of cemented files to export and an optional list
     of remaining blocks. If the export block is cemented, we need to cut
     the cycle containing the export block accordingly and retrieve the
     extra blocks. *)
  let compute_cemented_table_and_extra_cycle chain_store ~src_cemented_dir
      ~export_block =
    let open Lwt_result_syntax in
    let* o = Cemented_block_store.load_table src_cemented_dir in
    match o with
    | None -> return ([], None)
    | Some table_arr -> (
        let table_len = Array.length table_arr in
        let table = Array.to_list table_arr in
        (* Check whether the export_block is in the cemented blocks *)
        let export_block_level = Store.Block.level export_block in
        let is_cemented =
          table_len > 0
          && Compare.Int32.(
               export_block_level
               <= table_arr.(table_len - 1).Cemented_block_store.end_level)
        in
        if not is_cemented then
          (* Return either an empty list or the list of all cemented files *)
          return (table, None)
        else
          let is_last_cemented_block =
            Compare.Int32.(
              export_block_level
              = table_arr.(table_len - 1).Cemented_block_store.end_level)
          in
          if is_last_cemented_block then return (table, Some [])
          else
            (* If the export block is cemented, cut the cycle containing the
               export block accordingly and retrieve the extra blocks *)
            let filtered_table, extra_cycles =
              List.partition
                (fun {Cemented_block_store.end_level; _} ->
                  Compare.Int32.(export_block_level > end_level))
                table
            in
            assert (extra_cycles <> []) ;
            let extra_cycle =
              List.hd extra_cycles |> WithExceptions.Option.get ~loc:__LOC__
            in
            (* If the export block is the last block in cycle, append the cycle *)
            if Compare.Int32.(export_block_level = extra_cycle.end_level) then
              return (filtered_table @ [extra_cycle], Some [])
            else
              let* first_block =
                let* first_block_in_cycle =
                  Store.Block.read_block_by_level
                    chain_store
                    extra_cycle.start_level
                in
                (* TODO explain this... *)
                if
                  Compare.Int32.(
                    Store.Block.level first_block_in_cycle > export_block_level)
                then
                  (* When the cycles are short, we may keep more blocks in the
                     floating store than in cemented *)
                  let*! _, caboose_level = Store.Chain.caboose chain_store in
                  Store.Block.read_block_by_level chain_store caboose_level
                else return first_block_in_cycle
              in
              let*! o =
                Store.Chain_traversal.path
                  chain_store
                  ~from_block:first_block
                  ~to_block:export_block
              in
              match o with
              | None -> tzfail Cannot_retrieve_block_interval
              | Some floating_blocks ->
                  (* Don't forget to add the first block as
                     [Chain_traversal.path] does not include the lower-bound
                     block *)
                  let floating_blocks = first_block :: floating_blocks in
                  return (filtered_table, Some floating_blocks))

  (* Ensures that the history mode requested to export is compatible
     with the current storage. *)
  let check_history_mode chain_store ~rolling =
    let open Lwt_result_syntax in
    match (Store.Chain.history_mode chain_store : History_mode.t) with
    | Archive | Full _ -> return_unit
    | Rolling _ when rolling -> return_unit
    | Rolling _ as stored ->
        tzfail (Incompatible_history_mode {stored; requested = Full None})

  let export_floating_block_stream snapshot_exporter floating_block_stream
      progress_display_mode =
    let open Lwt_syntax in
    let f fd =
      let* is_empty = Lwt_stream.is_empty floating_block_stream in
      if is_empty then Lwt.return_unit
      else
        Animation.display_progress
          ~every:10
          ~pp_print_step:(fun fmt i ->
            Format.fprintf fmt "Copying floating blocks: %d blocks copied" i)
          (fun notify ->
            Lwt_stream.iter_s
              (fun b ->
                let* () = write_floating_block fd b in
                notify ())
              floating_block_stream)
          ~progress_display_mode
    in
    let* () = Exporter.write_floating_blocks snapshot_exporter ~f in
    return_ok_unit

  let export_context snapshot_exporter ~context_dir context_hash =
    let open Lwt_result_syntax in
    let*! context_index = Context.init ~readonly:true context_dir in
    let is_gc_allowed = Context.is_gc_allowed context_index in
    if not is_gc_allowed then tzfail Cannot_export_snapshot_format
    else
      Animation.three_dots ~progress_display_mode:Auto ~msg:"Exporting context"
      @@ fun () ->
      Lwt.finalize
        (fun () ->
          Exporter.export_context snapshot_exporter context_index context_hash)
        (fun () -> Context.close context_index)

  let export_rolling snapshot_exporter ~store_dir ~context_dir ~block ~rolling
      genesis =
    let open Lwt_result_syntax in
    let export_rolling_f chain_store =
      let* () = check_history_mode chain_store ~rolling in
      let* export_block, pred_block, lowest_block_level_needed =
        retrieve_export_block chain_store block
      in
      (* The number of additional cycles to export is fixed as the
         snapshot content must not rely on the local configuration. *)
      let export_mode = History_mode.Rolling None in
      let*! () =
        Event.(
          emit
            export_info
            ( Version.current_version,
              export_mode,
              Store.Block.descriptor export_block ))
      in
      (* Blocks *)
      (* Read the store to gather only the necessary blocks *)
      let* minimum_block =
        Store.Block.read_block_by_level chain_store lowest_block_level_needed
      in
      let* floating_blocks =
        let*! o =
          Store.Chain_traversal.path
            chain_store
            ~from_block:minimum_block
            ~to_block:pred_block
        in
        match o with
        | None -> tzfail Cannot_retrieve_block_interval
        | Some blocks ->
            (* Don't forget to add the first block as
               [Chain_traversal.path] does not include the
               lower-bound block *)
            return (minimum_block :: blocks)
      in
      (* Prune all blocks except for the export_block's predecessor *)
      let floating_block_stream =
        Lwt_stream.of_list
          (List.filter_map
             (fun b ->
               Some {(Store.Unsafe.repr_of_block b) with metadata = None})
             floating_blocks)
      in
      (* Protocols *)
      let*! protocol_levels = Store.Chain.all_protocol_levels chain_store in
      (* Filter protocols s.t. forall proto. proto.level >=
         caboose.proto_level. *)
      let protocol_levels =
        Protocol_levels.(
          filter
            (fun level {activation_block; _} ->
              let block = activation_block in
              level >= Store.Block.proto_level minimum_block
              || Store.Block.is_genesis chain_store (fst block))
            protocol_levels)
      in
      let* pred_resulting_context_hash =
        Store.Block.resulting_context_hash chain_store pred_block
      in
      let* resulting_context_hash =
        Store.Block.resulting_context_hash chain_store export_block
      in
      let* () =
        export_context
          snapshot_exporter
          ~context_dir
          pred_resulting_context_hash
      in
      return
        ( export_mode,
          export_block,
          resulting_context_hash,
          pred_block,
          protocol_levels,
          (return_unit, floating_block_stream) )
    in
    let* ( export_mode,
           export_block,
           resulting_context_hash,
           pred_block,
           protocol_levels,
           (return_unit, floating_block_stream) ) =
      Store.Unsafe.open_for_snapshot_export
        ~store_dir
        ~context_dir
        genesis
        ~locked_f:export_rolling_f
    in
    return
      ( export_mode,
        export_block,
        resulting_context_hash,
        pred_block,
        protocol_levels,
        (return_unit, floating_block_stream) )

  let export_full snapshot_exporter ~store_dir ~context_dir ~block ~rolling
      ~progress_display_mode genesis =
    let open Lwt_result_syntax in
    let export_full_f chain_store =
      let* () = check_history_mode chain_store ~rolling in
      let* export_block, pred_block, _lowest_block_level_needed =
        retrieve_export_block chain_store block
      in
      (* The number of additional cycles to export is fixed as the
         snapshot content must not rely on the local configuration. *)
      let export_mode = History_mode.Full None in
      let*! () =
        Event.(
          emit
            export_info
            ( Version.current_version,
              export_mode,
              Store.Block.descriptor export_block ))
      in
      let store_dir = Naming.store_dir ~dir_path:store_dir in
      let chain_id = Store.Chain.chain_id chain_store in
      let chain_dir = Naming.chain_dir store_dir chain_id in
      (* Open the floating FDs (in readonly) while the lock is present *)
      let ro_floating_blocks =
        Naming.floating_blocks_file (Naming.floating_blocks_dir chain_dir RO)
      in
      let rw_floating_blocks =
        Naming.floating_blocks_file (Naming.floating_blocks_dir chain_dir RW)
      in
      let*! ro_fd =
        Lwt_unix.openfile
          (Naming.file_path ro_floating_blocks)
          [Unix.O_RDONLY]
          snapshot_ro_file_perm
      in
      let*! rw_fd =
        Lwt_unix.openfile
          (Naming.file_path rw_floating_blocks)
          [Unix.O_RDONLY]
          snapshot_rw_file_perm
      in
      Lwt.catch
        (fun () ->
          let src_cemented_dir = Naming.cemented_blocks_dir chain_dir in
          (* Compute the necessary cemented table *)
          let* cemented_table, extra_floating_blocks =
            compute_cemented_table_and_extra_cycle
              chain_store
              ~src_cemented_dir
              ~export_block
          in
          let*! protocol_levels = Store.Chain.all_protocol_levels chain_store in
          let block_store = Store.Unsafe.get_block_store chain_store in
          let cemented_store = Block_store.cemented_block_store block_store in
          let should_filter_indexes =
            match
              Cemented_block_store.get_highest_cemented_level cemented_store
            with
            | None -> false
            | Some max_cemented_level ->
                Compare.Int32.(
                  max_cemented_level > Store.Block.level export_block)
          in
          let* pred_resulting_context_hash =
            Store.Block.resulting_context_hash chain_store pred_block
          in
          let* resulting_context_hash =
            Store.Block.resulting_context_hash chain_store export_block
          in
          let* () =
            export_context
              snapshot_exporter
              ~context_dir
              pred_resulting_context_hash
          in
          return
            ( export_mode,
              export_block,
              resulting_context_hash,
              pred_block,
              protocol_levels,
              cemented_table,
              (ro_fd, rw_fd),
              extra_floating_blocks,
              should_filter_indexes ))
        (fun exn ->
          let*! _ = Lwt_utils_unix.safe_close ro_fd in
          let*! _ = Lwt_utils_unix.safe_close rw_fd in
          fail_with_exn exn)
    in
    let* ( export_mode,
           export_block,
           pred_block,
           pred_resulting_context,
           protocol_levels,
           cemented_table,
           (floating_ro_fd, floating_rw_fd),
           extra_floating_blocks,
           should_filter_indexes ) =
      Store.Unsafe.open_for_snapshot_export
        ~store_dir
        ~context_dir
        genesis
        ~locked_f:export_full_f
    in
    let* () =
      copy_cemented_blocks
        snapshot_exporter
        ~should_filter_indexes
        cemented_table
        ~progress_display_mode
    in
    let finalizer () =
      let*! _ = Lwt_utils_unix.safe_close floating_ro_fd in
      let*! _ = Lwt_utils_unix.safe_close floating_rw_fd in
      Lwt.return_unit
    in
    let* reading_thread, floating_block_stream =
      match extra_floating_blocks with
      | Some floating_blocks ->
          let*! () = finalizer () in
          return
            ( return_unit,
              Lwt_stream.of_list
                (List.map Store.Unsafe.repr_of_block floating_blocks) )
      | None ->
          (* The export block is in the floating stores, copy all the
             floating stores until the block is reached *)
          let* reading_thread, floating_block_stream =
            export_floating_blocks ~floating_ro_fd ~floating_rw_fd ~export_block
          in
          let reading_thread =
            Lwt.finalize (fun () -> reading_thread) finalizer
          in
          return (reading_thread, floating_block_stream)
    in
    return
      ( export_mode,
        export_block,
        pred_block,
        pred_resulting_context,
        protocol_levels,
        (reading_thread, floating_block_stream) )

  let ensure_valid_export_chain_dir store_path chain_id =
    let open Lwt_result_syntax in
    let store_dir = Naming.store_dir ~dir_path:store_path in
    let chain_dir = Naming.chain_dir store_dir chain_id in
    let*! b = Lwt_unix.file_exists (Naming.dir_path chain_dir) in
    match b with
    | true -> return_unit
    | false ->
        tzfail
          (Invalid_chain_store_export (chain_id, Naming.dir_path store_dir))

  let export ?snapshot_path ?(rolling = false) ~block ~store_dir ~context_dir
      ~chain_name ~progress_display_mode genesis =
    let open Lwt_result_syntax in
    let chain_id = Chain_id.of_block_hash genesis.Genesis.block in
    let* () = ensure_valid_export_chain_dir store_dir chain_id in
    let* snapshot_exporter = init snapshot_path in
    (* Register a clean up callback to prevent export cancellation not
       to be correctly cleaned. *)
    let cleaner_id =
      Lwt_exit.register_clean_up_callback ~loc:__LOC__ (fun _ ->
          let*! () = Exporter.cleaner snapshot_exporter in
          Lwt.return_unit)
    in
    let* metadata =
      protect (fun () ->
          let* ( export_mode,
                 export_block,
                 resulting_context_hash,
                 pred_block,
                 protocol_levels,
                 (reading_thread, floating_block_stream) ) =
            if rolling then
              export_rolling
                snapshot_exporter
                ~store_dir
                ~context_dir
                ~block
                ~rolling
                genesis
            else
              export_full
                snapshot_exporter
                ~store_dir
                ~context_dir
                ~block
                ~rolling
                ~progress_display_mode
                genesis
          in
          (* Retrieve predecessor block metadata hash and operations
             metadata hash from the context of the exported block *)
          let predecessor_block_metadata_hash =
            Store.Block.block_metadata_hash pred_block
          in
          let predecessor_ops_metadata_hash =
            Store.Block.all_operations_metadata_hash pred_block
          in
          let*! () =
            Exporter.write_block_data
              snapshot_exporter
              ~predecessor_header:(Store.Block.header pred_block)
              ~predecessor_block_metadata_hash
              ~predecessor_ops_metadata_hash
              ~export_block
              ~resulting_context_hash
          in
          let* metadata =
            return
              (Snapshot_metadata.Current
                 {
                   chain_name;
                   history_mode = export_mode;
                   block_hash = Store.Block.hash export_block;
                   level = Store.Block.level export_block;
                   timestamp = Store.Block.timestamp export_block;
                 })
          in
          let* () =
            export_floating_block_stream
              snapshot_exporter
              floating_block_stream
              progress_display_mode
          in
          let* () = reading_thread in
          let* () =
            export_protocols
              snapshot_exporter
              export_block
              protocol_levels
              (Naming.protocol_store_dir (Naming.store_dir ~dir_path:store_dir))
              progress_display_mode
          in
          return metadata)
    in
    let* exported_snapshot_filename =
      Exporter.finalize snapshot_exporter metadata
    in
    let*! () = Event.(emit export_success exported_snapshot_filename) in
    Lwt_exit.unregister_clean_up_callback cleaner_id ;
    return_unit
end

module type LOADER = sig
  type t

  val load : string -> t Lwt.t

  val load_snapshot_header : t -> Snapshot_header.t tzresult Lwt.t

  val close : t -> unit Lwt.t
end

module Raw_loader : LOADER = struct
  type t = {snapshot_dir : [`Snapshot_dir] Naming.directory}

  let load snapshot_path =
    let snapshot_dir = Naming.snapshot_dir ~snapshot_path () in
    Lwt.return {snapshot_dir}

  let load_snapshot_version t =
    let open Lwt_result_syntax in
    let snapshot_file =
      Naming.(snapshot_version_file t.snapshot_dir |> file_path)
    in
    let read_json json = Data_encoding.Json.destruct Version.encoding json in
    let* json = Lwt_utils_unix.Json.read_file snapshot_file in
    return (read_json json)

  let load_snapshot_metadata t =
    let metadata_file =
      Naming.(snapshot_metadata_file t.snapshot_dir |> file_path)
    in
    Snapshot_metadata.read_metadata ~metadata_file

  let load_snapshot_legacy_metadata t =
    let metadata_file =
      Naming.(snapshot_metadata_file t.snapshot_dir |> file_path)
    in
    Snapshot_metadata.read_legacy_metadata ~metadata_file

  let load_snapshot_header t =
    let open Lwt_result_syntax in
    let* version = load_snapshot_version t in
    let* is_legacy = Version.is_legacy version in
    if is_legacy then
      let* legacy_metadata = load_snapshot_legacy_metadata t in
      return (Snapshot_header.Legacy (version, legacy_metadata))
    else
      let* metadata = load_snapshot_metadata t in
      return (Snapshot_header.Current (version, metadata))

  let close _ = Lwt.return_unit
end

module Tar_loader : LOADER = struct
  type t = {
    tar : Onthefly.i;
    snapshot_file : [`Snapshot_file] Naming.file;
    snapshot_tar : [`Tar_archive] Naming.directory;
  }

  let load snapshot_path =
    let open Lwt_syntax in
    let snapshot_dir =
      Naming.snapshot_dir ~snapshot_path:(Filename.dirname snapshot_path) ()
    in
    let snapshot_tar = Naming.snapshot_tar_root in
    let snapshot_file =
      Naming.snapshot_file
        ~snapshot_filename:(Filename.basename snapshot_path)
        snapshot_dir
    in
    let* tar = Onthefly.open_in ~file:(Naming.file_path snapshot_file) in
    Lwt.return {tar; snapshot_file; snapshot_tar}

  let load_snapshot_version t =
    let open Lwt_result_syntax in
    let filename = Naming.(snapshot_version_file t.snapshot_tar |> file_path) in
    let*! o =
      let*! o = Onthefly.find_file t.tar ~filename in
      match o with
      | Some file -> (
          let*! str = Onthefly.load_file t.tar file in
          match Data_encoding.Json.from_string str with
          | Ok json ->
              Lwt.return_some
                (Data_encoding.Json.destruct Version.encoding json)
          | Error _ -> Lwt.return_none)
      | None -> Lwt.return_none
    in
    match o with
    | Some version -> return version
    | None -> tzfail (Cannot_read {kind = `Version; path = filename})

  let load_snapshot_metadata t =
    let open Lwt_result_syntax in
    let filename =
      Naming.(snapshot_metadata_file t.snapshot_tar |> file_path)
    in
    let*! o =
      let*! o = Onthefly.find_file t.tar ~filename in
      match o with
      | Some file -> (
          let*! str = Onthefly.load_file t.tar file in
          match Data_encoding.Json.from_string str with
          | Ok json ->
              Lwt.return_some
                (Data_encoding.Json.destruct
                   Snapshot_metadata.metadata_encoding
                   json)
          | Error _ -> Lwt.return_none)
      | None -> Lwt.return_none
    in
    match o with
    | Some metadata -> return metadata
    | None -> tzfail (Cannot_read {kind = `Metadata; path = filename})

  let load_snapshot_legacy_metadata t =
    let open Lwt_result_syntax in
    let filename =
      Naming.(snapshot_metadata_file t.snapshot_tar |> file_path)
    in
    let*! o =
      let*! o = Onthefly.find_file t.tar ~filename in
      match o with
      | Some file -> (
          let*! str = Onthefly.load_file t.tar file in
          match Data_encoding.Json.from_string str with
          | Ok json ->
              Lwt.return_some
                (Data_encoding.Json.destruct
                   Snapshot_metadata.legacy_metadata_encoding
                   json)
          | Error _ -> Lwt.return_none)
      | None -> Lwt.return_none
    in
    match o with
    | Some metadata -> return metadata
    | None -> tzfail (Cannot_read {kind = `Metadata; path = filename})

  let load_snapshot_header t =
    let open Lwt_result_syntax in
    let* version = load_snapshot_version t in
    let* is_legacy = Version.is_legacy version in
    if is_legacy then
      let* legacy_metadata = load_snapshot_legacy_metadata t in
      return (Snapshot_header.Legacy (version, legacy_metadata))
    else
      let* metadata = load_snapshot_metadata t in
      return (Snapshot_header.Current (version, metadata))

  let close t = Onthefly.close_in t.tar
end

module type Snapshot_loader = sig
  type t

  val load_snapshot_header :
    snapshot_path:string -> Snapshot_header.t tzresult Lwt.t
end

module Make_snapshot_loader (Loader : LOADER) : Snapshot_loader = struct
  type t = Loader.t

  let load = Loader.load

  let close = Loader.close

  let load_snapshot_header ~snapshot_path =
    let open Lwt_syntax in
    let* loader = load snapshot_path in
    trace (Wrong_snapshot_file {filename = snapshot_path})
    @@ protect
         (fun () ->
           Lwt.finalize
             (fun () -> Loader.load_snapshot_header loader)
             (fun () -> close loader))
         ~on_error:(fun err ->
           let* () = close loader in
           Lwt.return_error err)
end

module type IMPORTER = sig
  type t

  val init :
    snapshot_path:string ->
    dst_store_dir:[`Store_dir] Naming.directory ->
    Chain_id.t ->
    t tzresult Lwt.t

  val snapshot_version : t -> Version.t

  val snapshot_metadata : t -> Snapshot_metadata.t

  val load_block_data : t -> block_data tzresult Lwt.t

  val restore_context : t -> dst_context_dir:string -> unit tzresult Lwt.t

  val legacy_restore_context :
    t ->
    Context.index ->
    expected_context_hash:Context_hash.t ->
    nb_context_elements:int ->
    progress_display_mode:Animation.progress_display_mode ->
    unit tzresult Lwt.t

  val load_protocol_table :
    t -> Protocol_levels.protocol_info Protocol_levels.t tzresult Lwt.t

  val load_and_validate_protocol_filenames :
    t -> Protocol_hash.t list tzresult Lwt.t

  val copy_and_validate_protocol :
    t -> protocol_hash:Protocol_hash.t -> (unit, error trace) result Lwt.t

  val restore_cemented_indexes : t -> unit Lwt.t

  val load_cemented_files : t -> string list tzresult Lwt.t

  val restore_cemented_cycle : t -> file:string -> unit tzresult Lwt.t

  val restore_floating_blocks :
    t ->
    Block_hash.t ->
    (unit tzresult Lwt.t * Block_repr.block Lwt_stream.t) tzresult Lwt.t

  val close : t -> unit Lwt.t
end

module Raw_importer : IMPORTER = struct
  type t = {
    version : Version.t;
    metadata : Snapshot_metadata.t;
    snapshot_dir : [`Snapshot_dir] Naming.directory;
    snapshot_cemented_dir : [`Cemented_blocks_dir] Naming.directory;
    snapshot_protocol_dir : [`Protocol_dir] Naming.directory;
    dst_cemented_dir : [`Cemented_blocks_dir] Naming.directory;
    dst_protocol_dir : [`Protocol_dir] Naming.directory;
    dst_store_dir : [`Store_dir] Naming.directory;
    dst_chain_dir : [`Chain_dir] Naming.directory;
  }

  let load_snapshot_header ~snapshot_path =
    let (module Loader) =
      (module Make_snapshot_loader (Raw_loader) : Snapshot_loader)
    in
    Loader.load_snapshot_header ~snapshot_path

  let snapshot_version {version; _} = version

  let snapshot_metadata {metadata; _} = metadata

  let init ~snapshot_path ~dst_store_dir chain_id =
    let open Lwt_result_syntax in
    let snapshot_dir = Naming.snapshot_dir ~snapshot_path () in
    let snapshot_cemented_dir = Naming.cemented_blocks_dir snapshot_dir in
    let snapshot_protocol_dir = Naming.protocol_store_dir snapshot_dir in
    let dst_chain_dir = Naming.chain_dir dst_store_dir chain_id in
    let dst_cemented_dir = Naming.cemented_blocks_dir dst_chain_dir in
    let dst_protocol_dir = Naming.protocol_store_dir dst_store_dir in
    let* snapshot_header =
      load_snapshot_header ~snapshot_path:(snapshot_dir |> Naming.(dir_path))
    in
    return
      {
        version = Snapshot_header.get_version snapshot_header;
        metadata = Snapshot_header.get_metadata snapshot_header;
        snapshot_dir;
        snapshot_cemented_dir;
        snapshot_protocol_dir;
        dst_cemented_dir;
        dst_protocol_dir;
        dst_store_dir;
        dst_chain_dir;
      }

  let load_block_data t =
    let open Lwt_result_syntax in
    let file = Naming.(snapshot_block_data_file t.snapshot_dir |> file_path) in
    let*! block_data = Lwt_utils_unix.read_file file in
    match Data_encoding.Binary.of_string_opt block_data_encoding block_data with
    | Some block_data -> return block_data
    | None -> (
        let* is_legacy = Version.is_legacy t.version in
        let* res =
          if is_legacy then
            Data_encoding.Binary.of_string_opt
              legacy_block_data_encoding
              block_data
            |> Option.map
                 (fun
                   {
                     block_header;
                     operations;
                     predecessor_header;
                     predecessor_block_metadata_hash;
                     predecessor_ops_metadata_hash;
                   }
                 ->
                   {
                     block_header;
                     operations;
                     predecessor_header;
                     predecessor_block_metadata_hash;
                     predecessor_ops_metadata_hash;
                     resulting_context_hash = Context_hash.zero;
                   })
            |> return
          else
            return
            @@ Data_encoding.Binary.of_string_opt block_data_encoding block_data
        in
        match res with
        | Some v -> return v
        | None -> tzfail (Cannot_read {kind = `Block_data; path = file}))

  let restore_context t ~dst_context_dir =
    let open Lwt_result_syntax in
    let context_file_path =
      Naming.(snapshot_context_file t.snapshot_dir |> file_path)
    in
    let*! () = Lwt_utils_unix.copy_dir context_file_path dst_context_dir in
    return_unit

  let legacy_restore_context t context_index ~expected_context_hash
      ~nb_context_elements ~progress_display_mode =
    let open Lwt_result_syntax in
    let context_file_path =
      Naming.(snapshot_context_file t.snapshot_dir |> file_path)
    in
    let* fd =
      Lwt.catch
        (fun () ->
          let*! fd =
            Lwt_unix.openfile
              context_file_path
              Lwt_unix.[O_RDONLY]
              snapshot_ro_file_perm
          in
          return fd)
        (function
          | Unix.Unix_error (e, _, _) ->
              tzfail (Context.Cannot_open_file (Unix.error_message e))
          | exc ->
              let msg =
                Printf.sprintf "unknown error: %s" (Printexc.to_string exc)
              in
              tzfail (Context.Cannot_open_file msg))
    in
    Lwt.finalize
      (fun () ->
        let* () =
          Context.restore_context
            context_index
            ~expected_context_hash
            ~fd
            ~nb_context_elements
            ~in_memory:false
            ~progress_display_mode
        in
        let*! current = Lwt_unix.lseek fd 0 Lwt_unix.SEEK_CUR in
        let*! stats = Lwt_unix.fstat fd in
        let total = stats.Lwt_unix.st_size in
        if current = total then return_unit
        else tzfail (Context.Suspicious_file (total - current)))
      (fun () -> Lwt_unix.close fd)

  let load_protocol_table t =
    let open Lwt_result_syntax in
    let protocol_tbl_filename =
      Naming.(snapshot_protocol_levels_file t.snapshot_dir |> encoded_file_path)
    in
    let*! table_bytes = Lwt_utils_unix.read_file protocol_tbl_filename in
    let* res =
      let* is_legacy = Version.is_legacy t.version in
      if is_legacy then
        (* Use the legacy encoding *)
        match
          Data_encoding.Binary.of_string_opt
            Protocol_levels.Legacy.encoding
            table_bytes
        with
        | Some table ->
            let* res =
              Protocol_levels.Legacy.fold_es
                (fun proto_level activation_block map ->
                  let protocol_info =
                    {
                      Protocol_levels.protocol =
                        activation_block.Protocol_levels.Legacy.protocol;
                      activation_block = activation_block.block;
                      (* Only snapshot with legacy semantics will have
                         legacy encoding. *)
                      expect_predecessor_context = false;
                    }
                  in
                  return (Protocol_levels.add proto_level protocol_info map))
                table
                Protocol_levels.empty
            in
            return_some res
        | None -> return_none
      else
        Data_encoding.Binary.of_string_opt Protocol_levels.encoding table_bytes
        |> return
    in
    match res with
    | Some v -> return v
    | None ->
        tzfail
          (Cannot_read {kind = `Protocol_table; path = protocol_tbl_filename})

  let load_and_validate_protocol_filenames t =
    let open Lwt_result_syntax in
    let protocol_levels_file =
      Naming.snapshot_protocol_levels_file t.snapshot_dir
    in
    let stream =
      Lwt_unix.files_of_directory (Naming.dir_path t.snapshot_protocol_dir)
    in
    let*! files = Lwt_stream.to_list stream in
    let is_not_a_protocol =
      let protocol_levels_path =
        Naming.encoded_file_path protocol_levels_file
      in
      fun file ->
        file = Filename.current_dir_name
        || file = Filename.parent_dir_name
        || file = Filename.basename protocol_levels_path
    in
    let protocol_files =
      List.filter_map
        (function
          | file when is_not_a_protocol file -> None | file -> Some file)
        files
    in
    List.map_es
      (fun file ->
        match Protocol_hash.of_b58check_opt file with
        | Some ph -> return ph
        | None -> tzfail (Invalid_protocol_file file))
      protocol_files

  let copy_and_validate_protocol t ~protocol_hash =
    let open Lwt_result_syntax in
    let src =
      Filename.concat
        (Naming.dir_path t.snapshot_protocol_dir)
        (Protocol_hash.to_b58check protocol_hash)
    in
    let dst =
      Filename.concat
        (Naming.dir_path t.dst_protocol_dir)
        (Protocol_hash.to_b58check protocol_hash)
    in
    let*! () = Lwt_utils_unix.copy_file ~src ~dst in
    let*! protocol_sources = Lwt_utils_unix.read_file dst in
    match Protocol.of_string protocol_sources with
    | None -> tzfail (Cannot_decode_protocol protocol_hash)
    | Some p ->
        let hash = Protocol.hash p in
        fail_unless
          (Protocol_hash.equal protocol_hash hash)
          (Inconsistent_protocol_hash {expected = protocol_hash; got = hash})

  let restore_cemented_indexes t =
    let open Lwt_syntax in
    let src_level_dir =
      Naming.(
        cemented_blocks_level_index_dir t.snapshot_cemented_dir |> dir_path)
    in
    let src_hash_dir =
      Naming.(
        cemented_blocks_hash_index_dir t.snapshot_cemented_dir |> dir_path)
    in
    let* () =
      if Sys.file_exists src_level_dir then
        Lwt_utils_unix.copy_dir
          src_level_dir
          Naming.(
            cemented_blocks_level_index_dir t.dst_cemented_dir |> dir_path)
      else Lwt.return_unit
    in
    if Sys.file_exists src_hash_dir then
      Lwt_utils_unix.copy_dir
        src_hash_dir
        Naming.(cemented_blocks_hash_index_dir t.dst_cemented_dir |> dir_path)
    else Lwt.return_unit

  let load_cemented_files t =
    let open Lwt_result_syntax in
    let stream =
      Lwt_unix.files_of_directory (Naming.dir_path t.snapshot_cemented_dir)
    in
    let*! files = Lwt_stream.to_list stream in
    let is_not_cycle_file file =
      file = Filename.current_dir_name
      || file = Filename.parent_dir_name
      || file
         = Filename.basename
             (Naming.dir_path
                (Naming.cemented_blocks_hash_index_dir t.snapshot_cemented_dir))
      || file
         = Filename.basename
             (Naming.dir_path
                (Naming.cemented_blocks_level_index_dir t.snapshot_cemented_dir))
    in
    List.filter_es
      (function
        | file when is_not_cycle_file file -> return_false
        | file ->
            let is_valid =
              match String.split_on_char '_' file with
              | [s; e] ->
                  Int32.of_string_opt s <> None || Int32.of_string_opt e <> None
              | _ -> false
            in
            if not is_valid then tzfail (Invalid_cemented_file file)
            else return_true)
      files

  let restore_cemented_cycle t ~file =
    let open Lwt_syntax in
    let src = Filename.concat (Naming.dir_path t.snapshot_cemented_dir) file in
    let dst = Filename.concat (Naming.dir_path t.dst_cemented_dir) file in
    let* () = Lwt_utils_unix.copy_file ~src ~dst in
    return_ok_unit

  let restore_floating_blocks t genesis_hash =
    let open Lwt_result_syntax in
    let floating_blocks_file =
      Naming.(snapshot_floating_blocks_file t.snapshot_dir |> file_path)
    in
    if not (Sys.file_exists floating_blocks_file) then
      return (return_unit, Lwt_stream.of_list [])
    else
      let*! fd =
        Lwt_unix.openfile
          floating_blocks_file
          Unix.[O_RDONLY]
          snapshot_ro_file_perm
      in
      let stream, bounded_push = Lwt_stream.create_bounded 1000 in
      let rec loop ?pred_block nb_bytes_left =
        if nb_bytes_left < 0 then tzfail Corrupted_floating_store
        else if nb_bytes_left = 0 then return_unit
        else
          let*! block, len_read = Block_repr_unix.read_next_block_exn fd in
          let* () =
            Block_repr.check_block_consistency ~genesis_hash ?pred_block block
          in
          let*! () = bounded_push#push block in
          loop (nb_bytes_left - len_read)
      in
      let reading_thread =
        Lwt.finalize
          (fun () ->
            let*! eof_offset = Lwt_unix.lseek fd 0 Unix.SEEK_END in
            let*! _ = Lwt_unix.lseek fd 0 Unix.SEEK_SET in
            loop eof_offset)
          (fun () ->
            bounded_push#close ;
            let*! _ = Lwt_utils_unix.safe_close fd in
            Lwt.return_unit)
      in
      return (reading_thread, stream)

  let close _ = Lwt.return_unit
end

module Tar_importer : IMPORTER = struct
  type t = {
    version : Version.t;
    metadata : Snapshot_metadata.t;
    snapshot_file : [`Snapshot_file] Naming.file;
    snapshot_tar : [`Tar_archive] Naming.directory;
    snapshot_cemented_blocks_dir : [`Cemented_blocks_dir] Naming.directory;
    dst_store_dir : [`Store_dir] Naming.directory;
    dst_chain_dir : [`Chain_dir] Naming.directory;
    dst_cemented_dir : [`Cemented_blocks_dir] Naming.directory;
    dst_protocol_dir : [`Protocol_dir] Naming.directory;
    tar : Onthefly.i;
    (* Store the files of the archive to avoid re-reading them *)
    files : Onthefly.file list;
  }

  let load_snapshot_header ~snapshot_path =
    let (module Loader) =
      (module Make_snapshot_loader (Tar_loader) : Snapshot_loader)
    in
    Loader.load_snapshot_header ~snapshot_path

  let snapshot_version {version; _} = version

  let snapshot_metadata {metadata; _} = metadata

  let init ~snapshot_path ~dst_store_dir chain_id =
    let open Lwt_result_syntax in
    let snapshot_dir =
      Naming.snapshot_dir ~snapshot_path:(Filename.dirname snapshot_path) ()
    in
    let snapshot_tar = Naming.snapshot_tar_root in
    let snapshot_file =
      Naming.snapshot_file
        ~snapshot_filename:(Filename.basename snapshot_path)
        snapshot_dir
    in
    let snapshot_cemented_blocks_dir =
      Naming.cemented_blocks_dir snapshot_tar
    in
    let dst_chain_dir = Naming.chain_dir dst_store_dir chain_id in
    let dst_cemented_dir = Naming.cemented_blocks_dir dst_chain_dir in
    let dst_protocol_dir = Naming.protocol_store_dir dst_store_dir in
    let* snapshot_header =
      load_snapshot_header ~snapshot_path:(snapshot_file |> Naming.(file_path))
    in
    let*! tar = Onthefly.open_in ~file:(Naming.file_path snapshot_file) in
    let*! files = Onthefly.list_files tar in
    return
      {
        version = Snapshot_header.get_version snapshot_header;
        metadata = Snapshot_header.get_metadata snapshot_header;
        snapshot_file;
        snapshot_tar;
        snapshot_cemented_blocks_dir;
        dst_store_dir;
        dst_chain_dir;
        dst_cemented_dir;
        dst_protocol_dir;
        tar;
        files;
      }

  let load_block_data t =
    let open Lwt_result_syntax in
    let filename =
      Naming.(snapshot_block_data_file t.snapshot_tar |> file_path)
    in
    let*! o = Onthefly.load_from_filename t.tar ~filename in
    match o with
    | Some str -> (
        let* is_legacy = Version.is_legacy t.version in
        let* res =
          if is_legacy then
            Data_encoding.Binary.of_string_opt legacy_block_data_encoding str
            |> Option.map
                 (fun
                   {
                     block_header;
                     operations;
                     predecessor_header;
                     predecessor_block_metadata_hash;
                     predecessor_ops_metadata_hash;
                   }
                 ->
                   {
                     block_header;
                     operations;
                     predecessor_header;
                     predecessor_block_metadata_hash;
                     predecessor_ops_metadata_hash;
                     resulting_context_hash = Context_hash.zero;
                   })
            |> return
          else
            return @@ Data_encoding.Binary.of_string_opt block_data_encoding str
        in
        match res with
        | Some v -> return v
        | None -> tzfail (Cannot_read {kind = `Block_data; path = filename}))
    | None -> tzfail (Cannot_read {kind = `Block_data; path = filename})

  let restore_context t ~dst_context_dir =
    let open Lwt_result_syntax in
    let*! () = Lwt_unix.mkdir dst_context_dir snapshot_dir_perm in
    let index = Filename.concat dst_context_dir "index" in
    let*! () = Lwt_unix.mkdir index snapshot_dir_perm in
    let*! context_files =
      Onthefly.find_files_with_common_path t.tar ~pattern:"context"
    in
    let dst_dir = Filename.chop_suffix dst_context_dir "context" in
    let*! () =
      List.iter_s
        (fun file ->
          let filename = Onthefly.get_filename file in
          Onthefly.copy_to_file
            t.tar
            file
            ~dst:Filename.(concat dst_dir filename))
        context_files
    in
    return_unit

  let legacy_restore_context t context_index ~expected_context_hash
      ~nb_context_elements ~progress_display_mode =
    let open Lwt_result_syntax in
    let filename = Naming.(snapshot_context_file t.snapshot_tar |> file_path) in
    let* header =
      let*! o = Onthefly.get_file t.tar ~filename in
      match o with
      | Some header -> return header
      | None -> tzfail (Cannot_read {kind = `Context; path = filename})
    in
    let*! fd = Onthefly.read_raw t.tar header in
    Context.restore_context
      context_index
      ~expected_context_hash
      ~nb_context_elements
      ~fd
      ~in_memory:false
      ~progress_display_mode

  let load_protocol_table t =
    let open Lwt_result_syntax in
    let protocol_tbl_filename =
      Naming.(snapshot_protocol_levels_file t.snapshot_tar |> encoded_file_path)
    in
    let*! o =
      Onthefly.load_from_filename t.tar ~filename:protocol_tbl_filename
    in
    match o with
    | Some str -> (
        let* res =
          let* is_legacy = Version.is_legacy t.version in
          if is_legacy then
            (* Use the legacy encoding *)
            match
              Data_encoding.Binary.of_string_opt
                Protocol_levels.Legacy.encoding
                str
            with
            | Some table ->
                let* res =
                  Protocol_levels.Legacy.fold_es
                    (fun proto_level activation_block map ->
                      let protocol_info =
                        {
                          Protocol_levels.protocol =
                            activation_block.Protocol_levels.Legacy.protocol;
                          activation_block = activation_block.block;
                          (* Only snapshot with legacy semantics will have
                             legacy encoding. *)
                          expect_predecessor_context = false;
                        }
                      in
                      return (Protocol_levels.add proto_level protocol_info map))
                    table
                    Protocol_levels.empty
                in
                return_some res
            | None -> return_none
          else
            Data_encoding.Binary.of_string_opt Protocol_levels.encoding str
            |> return
        in
        match res with
        | Some v -> return v
        | None ->
            tzfail
              (Cannot_read
                 {kind = `Protocol_table; path = protocol_tbl_filename}))
    | None ->
        tzfail
          (Cannot_read {kind = `Protocol_table; path = protocol_tbl_filename})

  let load_and_validate_protocol_filenames t =
    let open Lwt_result_syntax in
    let protocol_tbl_filename =
      Naming.(snapshot_protocol_levels_file t.snapshot_tar |> encoded_file_path)
    in
    let*! protocol_dir_files =
      Onthefly.find_files_with_common_path
        t.tar
        ~pattern:Naming.(protocol_store_dir t.snapshot_tar |> dir_path)
    in
    let protocol_files =
      List.fold_left
        (fun acc file ->
          let filename = Filename.basename (Onthefly.get_filename file) in
          if filename <> protocol_tbl_filename then filename :: acc else acc)
        []
        protocol_dir_files
    in
    List.map_es
      (fun file ->
        match Protocol_hash.of_b58check_opt file with
        | Some ph -> return ph
        | None -> tzfail (Invalid_protocol_file file))
      protocol_files

  let copy_and_validate_protocol t ~protocol_hash =
    let open Lwt_result_syntax in
    let src =
      Filename.(
        concat
          Naming.(protocol_store_dir t.snapshot_tar |> dir_path)
          (Protocol_hash.to_b58check protocol_hash))
    in
    let* file =
      let*! o = Onthefly.get_file t.tar ~filename:src in
      match o with
      | Some file -> return file
      | None -> tzfail (Cannot_read {kind = `Protocol; path = src})
    in
    let dst =
      Filename.(
        concat
          (Naming.dir_path t.dst_protocol_dir)
          (Protocol_hash.to_b58check protocol_hash))
    in
    let*! () = Onthefly.copy_to_file t.tar file ~dst in
    let*! protocol_sources = Lwt_utils_unix.read_file dst in
    match Protocol.of_string protocol_sources with
    | None -> tzfail (Cannot_decode_protocol protocol_hash)
    | Some p ->
        let hash = Protocol.hash p in
        fail_unless
          (Protocol_hash.equal protocol_hash hash)
          (Inconsistent_protocol_hash {expected = protocol_hash; got = hash})

  let restore_cemented_indexes t =
    let open Lwt_syntax in
    let* cbl =
      Onthefly.find_files_with_common_path
        t.tar
        ~pattern:
          Naming.(
            cemented_blocks_level_index_dir t.snapshot_cemented_blocks_dir
            |> dir_path)
    in
    let* cbh =
      Onthefly.find_files_with_common_path
        t.tar
        ~pattern:
          Naming.(
            cemented_blocks_hash_index_dir t.snapshot_cemented_blocks_dir
            |> dir_path)
    in
    let cemented_indexes_paths = cbl @ cbh in
    if cemented_indexes_paths <> [] then
      let level_index_dir =
        Naming.(cemented_blocks_level_index_dir t.dst_cemented_dir |> dir_path)
      in
      let hash_index_dir =
        Naming.(cemented_blocks_hash_index_dir t.dst_cemented_dir |> dir_path)
      in
      let* () = Lwt_unix.mkdir level_index_dir snapshot_dir_perm in
      let* () = Lwt_unix.mkdir hash_index_dir snapshot_dir_perm in
      let* () =
        Lwt_unix.mkdir
          Filename.(concat level_index_dir "index")
          snapshot_dir_perm
      in
      let* () =
        Lwt_unix.mkdir
          Filename.(concat hash_index_dir "index")
          snapshot_dir_perm
      in
      List.iter_s
        (fun file ->
          Onthefly.copy_to_file
            t.tar
            file
            ~dst:
              (Filename.concat
                 (Naming.dir_path t.dst_chain_dir)
                 (Onthefly.get_filename file)))
        cemented_indexes_paths
    else Lwt.return_unit

  let load_cemented_files t =
    let open Lwt_syntax in
    let* cemented_files =
      Onthefly.find_files_with_common_path t.tar ~pattern:"\\d+_\\d+"
    in
    return_ok
      (List.map
         (fun file -> Filename.basename (Onthefly.get_filename file))
         cemented_files)

  let restore_cemented_cycle t ~file =
    let open Lwt_result_syntax in
    let filename =
      Filename.(
        concat Naming.(cemented_blocks_dir t.snapshot_tar |> dir_path) file)
    in
    let* tar_file =
      let*! o = Onthefly.get_file t.tar ~filename in
      match o with
      | Some file -> return file
      | None -> tzfail (Cannot_read {kind = `Cemented_cycle; path = filename})
    in
    let*! () =
      Onthefly.copy_to_file
        t.tar
        tar_file
        ~dst:
          (Filename.concat
             (Naming.dir_path t.dst_cemented_dir)
             (Filename.basename file))
    in
    return_unit

  let restore_floating_blocks t genesis_hash =
    let open Lwt_result_syntax in
    let*! o =
      Onthefly.get_file
        t.tar
        ~filename:
          Naming.(snapshot_floating_blocks_file t.snapshot_tar |> file_path)
    in
    match o with
    | Some floating_blocks_file ->
        let file_size = Onthefly.get_file_size floating_blocks_file in
        let floating_blocks_file_fd = Onthefly.get_raw_input_fd t.tar in
        let stream, bounded_push = Lwt_stream.create_bounded 1000 in
        let rec loop ?pred_block nb_bytes_left =
          if nb_bytes_left < 0L then tzfail Corrupted_floating_store
          else if nb_bytes_left = 0L then return_unit
          else
            let*! block, len_read =
              Block_repr_unix.read_next_block_exn floating_blocks_file_fd
            in
            let* () =
              Block_repr.check_block_consistency ~genesis_hash ?pred_block block
            in
            let*! () = bounded_push#push block in
            loop Int64.(sub nb_bytes_left (of_int len_read))
        in
        let reading_thread =
          Lwt.finalize
            (fun () ->
              let raw_data_ofs =
                Onthefly.get_raw_file_ofs floating_blocks_file
              in
              let*! _ =
                Lwt_unix.LargeFile.lseek
                  floating_blocks_file_fd
                  raw_data_ofs
                  Unix.SEEK_SET
              in
              loop file_size)
            (fun () ->
              bounded_push#close ;
              Lwt.return_unit)
        in
        return (reading_thread, stream)
    | None -> return (return_unit, Lwt_stream.of_list [])

  let close t = Onthefly.close_in t.tar
end

module type Snapshot_importer = sig
  type t

  val import :
    snapshot_path:string ->
    ?patch_context:
      (Tezos_protocol_environment.Context.t ->
      Tezos_protocol_environment.Context.t tzresult Lwt.t) ->
    ?block:Block_hash.t ->
    ?check_consistency:bool ->
    dst_store_dir:[`Store_dir] Naming.directory ->
    dst_context_dir:string ->
    chain_name:Distributed_db_version.Name.t ->
    configured_history_mode:History_mode.t option ->
    user_activated_upgrades:User_activated.upgrades ->
    user_activated_protocol_overrides:User_activated.protocol_overrides ->
    operation_metadata_size_limit:Shell_limits.operation_metadata_size_limit ->
    progress_display_mode:Animation.progress_display_mode ->
    Genesis.t ->
    (unit, error trace) result Lwt.t
end

module Make_snapshot_importer (Importer : IMPORTER) : Snapshot_importer = struct
  type t = Importer.t

  let init = Importer.init

  let close = Importer.close

  let restore_cemented_blocks ?(check_consistency = true) ~dst_chain_dir
      ~genesis_hash ~progress_display_mode snapshot_importer =
    let open Lwt_result_syntax in
    let*! () = Importer.restore_cemented_indexes snapshot_importer in
    let* cemented_files = Importer.load_cemented_files snapshot_importer in
    let nb_cemented_files = List.length cemented_files in
    let* () =
      if nb_cemented_files > 0 then
        Animation.display_progress
          ~pp_print_step:(fun fmt i ->
            Format.fprintf
              fmt
              "Copying cycles: %d/%d (%d%%)"
              i
              nb_cemented_files
              (100 * i / nb_cemented_files))
          ~progress_display_mode
          (fun notify ->
            List.iter_es
              (fun file ->
                let* () =
                  Importer.restore_cemented_cycle snapshot_importer ~file
                in
                let*! () = notify () in
                return_unit)
              cemented_files)
      else return_unit
    in
    let* cemented_store =
      Cemented_block_store.init
        ~log_size:cemented_import_log_size
        ~readonly:false
        dst_chain_dir
    in
    let* () =
      if check_consistency && nb_cemented_files > 0 then
        match Cemented_block_store.cemented_blocks_files cemented_store with
        | None -> failwith "unexpected empty set of cemented files"
        | Some stored_cemented_files ->
            let* () =
              List.iter_es
                (fun cemented_file ->
                  if
                    not
                      (Array.exists
                         (fun {Cemented_block_store.file; _} ->
                           Compare.String.equal
                             (Naming.file_path file |> Filename.basename)
                             cemented_file)
                         stored_cemented_files)
                  then tzfail (Missing_cemented_file cemented_file)
                  else return_unit)
                (List.sort compare cemented_files)
            in
            Animation.display_progress
              ~pp_print_step:(fun fmt i ->
                Format.fprintf
                  fmt
                  "Restoring cycles consistency: %d/%d (%d%%)"
                  i
                  nb_cemented_files
                  (100 * i / nb_cemented_files))
              ~progress_display_mode
              (fun notify ->
                Cemented_block_store.check_indexes_consistency
                  ~post_step:notify
                  ~genesis_hash
                  cemented_store)
      else return_unit
    in
    Cemented_block_store.close cemented_store ;
    return_unit

  let read_floating_blocks snapshot_importer ~genesis_hash =
    Importer.restore_floating_blocks snapshot_importer genesis_hash

  let restore_protocols snapshot_importer progress_display_mode =
    let open Lwt_result_syntax in
    (* Import protocol table *)
    let* protocol_levels = Importer.load_protocol_table snapshot_importer in
    (* Retrieve protocol files *)
    let* protocols =
      Importer.load_and_validate_protocol_filenames snapshot_importer
    in
    let* () =
      Animation.display_progress
        ~pp_print_step:(fun fmt i ->
          Format.fprintf
            fmt
            "Copying protocols: %d/%d"
            i
            (List.length protocols))
        ~progress_display_mode
        (fun notify ->
          let validate_and_copy protocol_hash =
            let* () =
              Importer.copy_and_validate_protocol
                snapshot_importer
                ~protocol_hash
            in
            let*! () = notify () in
            return_unit
          in
          List.iter_es validate_and_copy protocols)
    in
    return protocol_levels

  let import_log_notice ~snapshot_version ~snapshot_metadata filename block =
    let open Lwt_syntax in
    let header =
      Format.asprintf
        "%a (snapshot version %d)"
        Snapshot_metadata.pp
        snapshot_metadata
        snapshot_version
    in
    let* () = Event.(emit import_info (filename, header)) in
    let* () =
      match block with
      | None -> Event.(emit import_unspecified_hash ())
      | Some _ -> Lwt.return_unit
    in
    Event.(emit import_loading ())

  let check_context_hash_consistency ~expected_context_hash validation_store =
    fail_unless
      (Context_hash.equal
         validation_store.Block_validation.resulting_context_hash
         expected_context_hash
      || (* This is needed as the former snapshot's version does not
            provide enough data to perform this check with the new
            context hash semantics (a block header contains the
            predecessor's context hash). In that particular case, we
            are not able to perform this validity check. *)
      Context_hash.equal expected_context_hash Context_hash.zero)
      (Inconsistent_context_hash
         {
           expected = expected_context_hash;
           got = validation_store.Block_validation.resulting_context_hash;
         })

  let apply_context context_index ~imported_context_hash chain_id ~block_header
      ~operations ~predecessor_header ~predecessor_block_metadata_hash
      ~predecessor_ops_metadata_hash ~user_activated_upgrades
      ~user_activated_protocol_overrides ~operation_metadata_size_limit =
    let open Lwt_result_syntax in
    let* predecessor_context =
      let*! o = Context.checkout context_index imported_context_hash in
      match o with
      | Some ch -> return ch
      | None -> tzfail (Inconsistent_context imported_context_hash)
    in
    let predecessor_context =
      Tezos_shell_context.Shell_context.wrap_disk_context predecessor_context
    in
    let apply_environment =
      {
        Block_validation.max_operations_ttl =
          Int32.to_int predecessor_header.Block_header.shell.level;
        chain_id;
        predecessor_block_header = predecessor_header;
        predecessor_context;
        predecessor_resulting_context_hash = imported_context_hash;
        predecessor_block_metadata_hash;
        predecessor_ops_metadata_hash;
        user_activated_upgrades;
        user_activated_protocol_overrides;
        operation_metadata_size_limit;
      }
    in
    let* {result = block_validation_result; _} =
      let*! r =
        Block_validation.apply
          apply_environment
          block_header
          operations
          ~cache:`Lazy
      in
      match r with
      | Ok block_validation_result -> return block_validation_result
      | Error errs ->
          Format.kasprintf
            (fun errs ->
              tzfail
                (Target_block_validation_failed
                   (Block_header.hash block_header, errs)))
            "%a"
            pp_print_trace
            errs
    in
    return block_validation_result

  let restore_and_apply_context snapshot_importer protocol_levels
      ?user_expected_block ~dst_context_dir ~user_activated_upgrades
      ~user_activated_protocol_overrides ~operation_metadata_size_limit
      ~progress_display_mode ~legacy ~patch_context ~check_consistency
      snapshot_metadata genesis chain_id =
    let open Lwt_result_syntax in
    let* ({
            block_header;
            resulting_context_hash;
            operations;
            predecessor_header;
            predecessor_block_metadata_hash;
            predecessor_ops_metadata_hash;
          } as block_data) =
      Importer.load_block_data snapshot_importer
    in
    (* Checks that the block hash imported from the snapshot is the one
       expected by the user's --block command line option *)
    let block_header_hash = Block_header.hash block_header in
    let* () =
      match user_expected_block with
      | Some bh ->
          fail_unless
            (Block_hash.equal bh block_header_hash)
            (Inconsistent_imported_block (block_header_hash, bh))
      | None -> return_unit
    in
    (* Checks that the block hash read from the snapshot metadata is the
       expected one *)
    let* () =
      let block_hash = Snapshot_metadata.get_block_hash snapshot_metadata in
      fail_unless
        (Block_hash.equal block_hash block_header_hash)
        (Inconsistent_imported_block (block_header_hash, block_hash))
    in
    let imported_context_hash =
      match
        Protocol_levels.find
          predecessor_header.shell.proto_level
          protocol_levels
      with
      | None -> Stdlib.failwith "unknown protocol"
      | Some {Protocol_levels.expect_predecessor_context; _} ->
          if expect_predecessor_context then
            block_header.Block_header.shell.context
          else predecessor_header.Block_header.shell.context
    in
    let* genesis_ctxt_hash, block_validation_result =
      if legacy then
        let*! context_index =
          Context.init
            ~readonly:false
            ~index_log_size:default_index_log_size
            ?patch_context
            dst_context_dir
        in
        let* genesis_ctxt_hash =
          Context.commit_genesis
            context_index
            ~chain_id
            ~time:genesis.Genesis.time
            ~protocol:genesis.protocol
        in
        let* context_elements =
          match snapshot_metadata with
          | Current _ ->
              tzfail (Cannot_read {kind = `Metadata; path = "snapshot's file"})
          | Legacy metadata -> return metadata.context_elements
        in
        let* () =
          Importer.legacy_restore_context
            snapshot_importer
            context_index
            ~expected_context_hash:imported_context_hash
            ~nb_context_elements:context_elements
            ~progress_display_mode
        in
        let* block_validation_result =
          apply_context
            context_index
            ~imported_context_hash
            chain_id
            ~block_header
            ~operations
            ~predecessor_header
            ~predecessor_block_metadata_hash
            ~predecessor_ops_metadata_hash
            ~user_activated_upgrades
            ~user_activated_protocol_overrides
            ~operation_metadata_size_limit
        in
        let*! () = Context.close context_index in
        return (genesis_ctxt_hash, block_validation_result)
      else
        let* () =
          Animation.three_dots
            ~progress_display_mode:Auto
            ~msg:"Importing context"
          @@ fun () ->
          Importer.restore_context snapshot_importer ~dst_context_dir
        in
        let*! context_index =
          Context.init
            ~readonly:false
            ~index_log_size:default_index_log_size
            ?patch_context
            dst_context_dir
        in
        let* genesis_ctxt_hash =
          Context.commit_genesis
            context_index
            ~chain_id
            ~time:genesis.Genesis.time
            ~protocol:genesis.protocol
        in
        let*! () =
          if check_consistency then
            Animation.three_dots
              ~progress_display_mode:Auto
              ~msg:"Checking context integrity"
            @@ fun () ->
            Context.Checks.Pack.Integrity_check.run
              ~root:dst_context_dir
              ~auto_repair:false
              ~always:false
              ~heads:(Some [Context_hash.to_b58check imported_context_hash])
          else Lwt.return_unit
        in
        let* block_validation_result =
          apply_context
            context_index
            ~imported_context_hash
            chain_id
            ~block_header
            ~operations
            ~predecessor_header
            ~predecessor_block_metadata_hash
            ~predecessor_ops_metadata_hash
            ~user_activated_upgrades
            ~user_activated_protocol_overrides
            ~operation_metadata_size_limit
        in
        let*! () = Context.close context_index in
        return (genesis_ctxt_hash, block_validation_result)
    in
    let* () =
      check_context_hash_consistency
        ~expected_context_hash:resulting_context_hash
        block_validation_result.validation_store
    in
    return (block_data, genesis_ctxt_hash, block_validation_result)

  (* TODO parallelise in another process *)
  (* TODO? remove patch context *)
  let import ~snapshot_path ?patch_context ?block:user_expected_block
      ?(check_consistency = true) ~dst_store_dir ~dst_context_dir ~chain_name
      ~configured_history_mode ~user_activated_upgrades
      ~user_activated_protocol_overrides ~operation_metadata_size_limit
      ~progress_display_mode (genesis : Genesis.t) =
    let open Lwt_result_syntax in
    let chain_id = Chain_id.of_block_hash genesis.Genesis.block in
    let* snapshot_importer = init ~snapshot_path ~dst_store_dir chain_id in
    let dst_store_dir = Naming.dir_path dst_store_dir in
    let* () =
      fail_when
        (Sys.file_exists dst_store_dir)
        (Directory_already_exists dst_store_dir)
    in
    let dst_store_dir = Naming.store_dir ~dir_path:dst_store_dir in
    let dst_protocol_dir = Naming.protocol_store_dir dst_store_dir in
    let chain_id = Chain_id.of_block_hash genesis.block in
    let dst_chain_dir = Naming.chain_dir dst_store_dir chain_id in
    let dst_cemented_dir = Naming.cemented_blocks_dir dst_chain_dir in
    (* Create directories *)
    let*! () =
      List.iter_s
        (Lwt_utils_unix.create_dir ~perm:snapshot_dir_perm)
        [
          Naming.dir_path dst_store_dir;
          Naming.dir_path dst_protocol_dir;
          Naming.dir_path dst_chain_dir;
          Naming.dir_path dst_cemented_dir;
        ]
    in
    let* () =
      fail_unless
        (Sys.file_exists snapshot_path)
        (Snapshot_file_not_found snapshot_path)
    in
    let snapshot_version = Importer.snapshot_version snapshot_importer in
    let snapshot_metadata = Importer.snapshot_metadata snapshot_importer in
    let* () =
      fail_unless
        (Version.is_supported snapshot_version)
        (Inconsistent_version_import
           {
             expected = List.map fst Version.supported_versions;
             got = snapshot_version;
           })
    in
    let* () =
      let metadata_chain_name =
        Snapshot_metadata.get_chain_name snapshot_metadata
      in
      fail_unless
        (Distributed_db_version.Name.equal chain_name metadata_chain_name)
        (Inconsistent_chain_import
           {expected = metadata_chain_name; got = chain_name})
    in
    let* () =
      let history_mode = Snapshot_metadata.get_history_mode snapshot_metadata in
      match configured_history_mode with
      | Some stored ->
          let requested = history_mode in
          fail_unless
            (History_mode.mode_equality requested stored)
            (Inconsistent_history_mode_import {requested; stored})
      | None -> return_unit
    in
    let*! () =
      if not check_consistency then Event.(emit warn_no_check ())
      else Event.(emit suggest_no_check ())
    in
    let*! () =
      import_log_notice
        ~snapshot_version
        ~snapshot_metadata
        snapshot_path
        user_expected_block
    in
    let patch_context =
      Option.map
        (fun f ctxt ->
          let open Tezos_shell_context in
          let ctxt = Shell_context.wrap_disk_context ctxt in
          let+ ctxt = f ctxt in
          Shell_context.unwrap_disk_context ctxt)
        patch_context
    in
    (* Restore protocols *)
    let* protocol_levels =
      restore_protocols snapshot_importer progress_display_mode
    in
    let* legacy = Version.is_legacy snapshot_version in
    (* Restore context *)
    let* block_data, genesis_context_hash, block_validation_result =
      restore_and_apply_context
        snapshot_importer
        protocol_levels
        ?user_expected_block
        ~dst_context_dir
        ~user_activated_upgrades
        ~user_activated_protocol_overrides
        ~operation_metadata_size_limit
        ~progress_display_mode
        ~legacy
        ~patch_context
        ~check_consistency
        snapshot_metadata
        genesis
        chain_id
    in
    (* Restore store *)
    (* Restore cemented dir *)
    let* () =
      restore_cemented_blocks
        snapshot_importer
        ~check_consistency
        ~dst_chain_dir
        ~genesis_hash:genesis.block
        ~progress_display_mode
    in
    let* reading_thread, floating_blocks_stream =
      read_floating_blocks snapshot_importer ~genesis_hash:genesis.block
    in
    let {
      Block_validation.validation_store;
      block_metadata;
      ops_metadata;
      shell_header_hash = _;
    } =
      block_validation_result
    in
    let contents =
      {
        Block_repr.header = block_data.block_header;
        operations = block_data.operations;
        block_metadata_hash = snd block_metadata;
        operations_metadata_hashes =
          (match ops_metadata with
          | Block_validation.No_metadata_hash _ -> None
          | Block_validation.Metadata_hash ops_metadata ->
              Some (List.map (List.map snd) ops_metadata));
      }
    in
    let metadata =
      Some
        ({
           message = validation_store.message;
           max_operations_ttl = validation_store.max_operations_ttl;
           last_allowed_fork_level = validation_store.last_allowed_fork_level;
           block_metadata = fst block_metadata;
           operations_metadata =
             (match ops_metadata with
             | Block_validation.No_metadata_hash x -> x
             | Block_validation.Metadata_hash ops_metadata ->
                 List.map (List.map fst) ops_metadata);
         }
          : Block_repr.metadata)
    in
    let new_head_with_metadata =
      ({hash = Block_header.hash block_data.block_header; contents; metadata}
        : Block_repr.block)
    in
    (* Set the history mode with the default additional cycle
       offset. This is necessary as the snapshot content does not rely
       on a given offset. If the node was configured to run with the
       non default number of additional cycles, it will be
       automatically updated when running the node. *)
    let* history_mode =
      let open History_mode in
      match Snapshot_metadata.get_history_mode snapshot_metadata with
      | Archive -> assert false
      | Rolling _ -> return (Rolling None)
      | Full _ -> return (Full None)
    in
    let* () =
      Animation.display_progress
        ~every:100
        ~pp_print_step:(fun fmt i ->
          Format.fprintf fmt "Storing floating blocks: %d blocks written" i)
        ~progress_display_mode
        (fun notify ->
          Store.Unsafe.restore_from_snapshot
            ~notify
            dst_store_dir
            ~genesis
            ~genesis_context_hash
            ~floating_blocks_stream
            ~new_head_with_metadata
            ~new_head_resulting_context_hash:
              validation_store.resulting_context_hash
            ~predecessor_header:block_data.predecessor_header
            ~protocol_levels
            ~history_mode)
    in
    let* () = reading_thread in
    let*! () = Event.(emit import_success snapshot_path) in
    let*! () = close snapshot_importer in
    return_unit
end

(* [snapshot_file_kind ~snapshot_path] returns the kind of a
   snapshot. We assume that a snapshot is valid if the medata can be
   read. *)
let snapshot_file_kind ~snapshot_path =
  let open Lwt_result_syntax in
  let is_valid_uncompressed_snapshot file =
    let (module Loader) =
      (module Make_snapshot_loader (Tar_loader) : Snapshot_loader)
    in
    Error_monad.catch_es (fun () ->
        let* _header =
          Loader.load_snapshot_header ~snapshot_path:(Naming.file_path file)
        in
        return_unit)
  in
  let is_valid_raw_snapshot snapshot_dir =
    let (module Loader) =
      (module Make_snapshot_loader (Raw_loader) : Snapshot_loader)
    in
    Error_monad.catch_es (fun () ->
        let* _header =
          Loader.load_snapshot_header
            ~snapshot_path:(Naming.dir_path snapshot_dir)
        in
        return_unit)
  in
  protect (fun () ->
      let*! is_dir = Lwt_utils_unix.is_directory snapshot_path in
      if is_dir then
        let snapshot_dir = Naming.snapshot_dir ~snapshot_path () in
        let* () = is_valid_raw_snapshot snapshot_dir in
        return Raw
      else
        let snapshot_file =
          Naming.snapshot_file
            ~snapshot_filename:(Filename.basename snapshot_path)
            Naming.(
              snapshot_dir ~snapshot_path:(Filename.dirname snapshot_path) ())
        in
        let* () = is_valid_uncompressed_snapshot snapshot_file in
        return Tar)

let export ?snapshot_path export_format ?rolling ~block ~store_dir ~context_dir
    ~chain_name ~progress_display_mode genesis =
  let (module Exporter) =
    match export_format with
    | Tar -> (module Make_snapshot_exporter (Tar_exporter) : Snapshot_exporter)
    | Raw -> (module Make_snapshot_exporter (Raw_exporter) : Snapshot_exporter)
  in
  Exporter.export
    ?snapshot_path
    ?rolling
    ~block
    ~store_dir
    ~context_dir
    ~chain_name
    ~progress_display_mode
    genesis

let read_snapshot_header ~snapshot_path =
  let open Lwt_result_syntax in
  let* kind = snapshot_file_kind ~snapshot_path in
  let (module Loader) =
    match kind with
    | Tar -> (module Make_snapshot_loader (Tar_loader) : Snapshot_loader)
    | Raw -> (module Make_snapshot_loader (Raw_loader) : Snapshot_loader)
  in
  Loader.load_snapshot_header ~snapshot_path

let import ~snapshot_path ?patch_context ?block ?check_consistency
    ~dst_store_dir ~dst_context_dir ~chain_name ~configured_history_mode
    ~user_activated_upgrades ~user_activated_protocol_overrides
    ~operation_metadata_size_limit ~progress_display_mode genesis =
  let open Lwt_result_syntax in
  let* kind = snapshot_file_kind ~snapshot_path in
  let (module Importer) =
    match kind with
    | Tar -> (module Make_snapshot_importer (Tar_importer) : Snapshot_importer)
    | Raw -> (module Make_snapshot_importer (Raw_importer) : Snapshot_importer)
  in
  let dst_store_dir = Naming.store_dir ~dir_path:dst_store_dir in
  Importer.import
    ~snapshot_path
    ?patch_context
    ?block
    ?check_consistency
    ~dst_store_dir
    ~dst_context_dir
    ~chain_name
    ~configured_history_mode
    ~user_activated_upgrades
    ~user_activated_protocol_overrides
    ~operation_metadata_size_limit
    ~progress_display_mode
    genesis
OCaml

Innovation. Community. Security.