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
use crate::{
disk::{DiskMmapFile, DiskMmapFileMut},
empty::EmptyMmapFile,
error::{Error, ErrorKind, Result},
memory::{MemoryMmapFile, MemoryMmapFileMut},
metadata::MetaData,
options::Options,
MmapFileReader, MmapFileWriter,
};
use std::{
borrow::Cow,
io::Cursor,
mem,
path::{Path, PathBuf},
};
/// fsync the directory `path` lives in so a freshly-`create_new`-opened
/// file becomes crash-durable by name (not just by content). Basename
/// paths get `.` so they're covered too.
fn sync_new_file_parent(path: &Path) -> Result<()> {
let parent = match path.parent() {
Some(p) if !p.as_os_str().is_empty() => p.to_path_buf(),
_ => std::path::PathBuf::from("."),
};
crate::utils::sync_dir(&parent)
}
/// Initial-call durable unlink with optional inode pin.
///
/// Sequence: open parent dir handle → probe identity at the path
/// (`fstatat` on POSIX, bound to the same parent fd) → unlink at the
/// path (`unlinkat` on POSIX, bound to the same parent fd) → fsync the
/// parent handle. All four ops are bound to the same parent inode on
/// POSIX, so a parent rename mid-operation can't direct durability to
/// the wrong directory.
///
/// `inode_pin` is the original `File` handle, held alive across probe +
/// unlink so the kernel cannot recycle the inode number while the
/// identity check runs. Required on POSIX where `(dev, ino)` is the
/// identity — without the pin, a concurrent replacement created right
/// after the caller closed the file could land on the same inode and
/// pass `is_known_equal`. On Windows, holding the handle without
/// `FILE_SHARE_DELETE` would *prevent* unlink, so the caller must drop
/// it before calling and pass `None`. The pin is dropped on function
/// exit, after the unlink.
///
/// Identity check distinguishes three cases: `NotFound` (original inode
/// presumed already gone, surface as `NeedsParentSync`); identity
/// mismatch (path was reused, refuse unlink, keep `NeedsUnlink`);
/// identity match (proceed with `unlinkat`).
fn initial_remove_durably(
path: &Path,
identity: crate::utils::FileIdentity,
#[cfg(unix)] inode_pin: std::fs::File,
) -> std::result::Result<(), (crate::mmap_file::PendingDelete, Error)> {
// pin is a required parameter on POSIX (not Option). Callers must
// dup before calling and hard-fail on dup failure, so this function
// never sees a missing pin. The pin is moved into
// `PendingDelete::NeedsUnlink` on retryable failures so subsequent
// retries inherit it; otherwise dropped on function exit.
// Pin a handle to the *original* parent inode BEFORE the unlink. If
// the path's parent gets renamed/replaced between unlink and fsync, a
// path-based fsync would commit metadata on the wrong inode; the
// pre-opened handle commits to the inode that actually contained our
// directory entry. The handle is also stashed in any `NeedsParentSync`
// pending state so retry fsyncs the same inode.
let parent_handle = match crate::utils::open_parent_for_sync(path) {
Ok(h) => h,
Err(e) => {
return Err((
crate::mmap_file::PendingDelete::NeedsUnlink {
path: path.to_path_buf(),
identity,
#[cfg(unix)]
pin: inode_pin,
},
e,
));
}
};
// Probe identity *relative to* parent_handle on POSIX (fstatat) so
// the probe is bound to the same inode the unlink will go through —
// a path-based probe could race a parent rename between
// open_parent_for_sync and probe.
match crate::utils::identity_at_or_path(&parent_handle, path) {
Err(e) if e.kind() == std::io::ErrorKind::NotFound => {
// NotFound never proves crash-durable deletion. An nlink-based
// "durably gone" inference would be wrong because an external
// rename + unlink-elsewhere also produces `nlink == 0` while
// leaving the actual unlink in a parent we don't fsync —
// fsyncing OUR parent then doesn't make their unlink
// crash-durable. Always route NotFound to NeedsUnlink so retry
// surfaces NotFound rather than false-success-via-fsync.
// Drop's NeedsUnlink path still best-effort fsyncs the parent,
// which IS correct in the common "external rm in our parent"
// case — we just don't promise the caller that deletion
// succeeded.
return Err((
crate::mmap_file::PendingDelete::NeedsUnlink {
path: path.to_path_buf(),
identity,
#[cfg(unix)]
pin: inode_pin,
},
e,
));
}
Err(e) => {
return Err((
crate::mmap_file::PendingDelete::NeedsUnlink {
path: path.to_path_buf(),
identity,
#[cfg(unix)]
pin: inode_pin,
},
e,
));
}
Ok(probe_id) => {
if !identity.is_known_equal(&probe_id) {
let err = Error::other(format!(
"cannot unlink '{}': path no longer names the original file (path-reuse detected between handle drop and unlink)",
path.display(),
));
return Err((
crate::mmap_file::PendingDelete::NeedsUnlink {
path: path.to_path_buf(),
identity,
#[cfg(unix)]
pin: inode_pin,
},
err,
));
}
}
}
// Bind the unlink to parent_handle (POSIX `unlinkat`) so the
// subsequent `sync_parent_handle` is durable for the directory the
// entry was actually removed from. There's still an irreducible
// narrow TOCTOU between the identity probe and the unlinkat call,
// but the *broad* race against parent renames is now closed.
match crate::utils::unlink_at_or_path(&parent_handle, path, identity) {
Ok(()) => match crate::utils::sync_parent_handle(&parent_handle) {
Ok(()) => Ok(()),
Err(e) => Err((
crate::mmap_file::PendingDelete::NeedsParentSync {
path: path.to_path_buf(),
parent_handle,
},
e,
)),
},
Err(e) if e.kind() == std::io::ErrorKind::NotFound => {
// Same as pre-probe NotFound — never claim durable deletion.
// Post-probe NotFound from `unlinkat` could be a
// rename-then-unlink-elsewhere; our parent fsync wouldn't
// commit the right journal. Stay NeedsUnlink.
Err((
crate::mmap_file::PendingDelete::NeedsUnlink {
path: path.to_path_buf(),
identity,
#[cfg(unix)]
pin: inode_pin,
},
e,
))
}
Err(e) => Err((
crate::mmap_file::PendingDelete::NeedsUnlink {
path: path.to_path_buf(),
identity,
#[cfg(unix)]
pin: inode_pin,
},
e,
)),
}
}
/// Retry a pending delete in a path-reuse-safe way.
///
/// `NeedsParentSync` only fsyncs the parent — never re-calls `remove_file`.
///
/// `NeedsUnlink { path, identity }` re-opens the path metadata, compares
/// against the captured identity, and only unlinks when they match. On
/// mismatch the path was reused, so we keep the pending state and return
/// a tagged error rather than deleting an unrelated file.
fn retry_pending_delete(
pending: crate::mmap_file::PendingDelete,
) -> std::result::Result<(), (crate::mmap_file::PendingDelete, Error)> {
match pending {
crate::mmap_file::PendingDelete::NeedsParentSync {
path,
parent_handle,
} => match crate::utils::sync_parent_handle(&parent_handle) {
Ok(()) => Ok(()),
Err(e) => Err((
crate::mmap_file::PendingDelete::NeedsParentSync {
path,
parent_handle,
},
e,
)),
},
crate::mmap_file::PendingDelete::NeedsUnlink {
path,
identity,
#[cfg(unix)]
pin,
} => {
// Just delegate to `initial_remove_durably`. It will re-stitch
// the pin into `NeedsUnlink` on Err so the next retry inherits
// a still-active inode pin.
initial_remove_durably(
&path,
identity,
#[cfg(unix)]
pin,
)
}
}
}
/// Utility methods to [`MmapFile`]
///
/// [`MmapFile`]: structs.MmapFile.html
#[enum_dispatch]
pub trait MmapFileExt {
/// Returns the current mmap length
fn len(&self) -> usize;
/// Returns the mmap is empty of not.
fn is_empty(&self) -> bool {
self.len() == 0
}
/// Returns the underlying slice of the mmap
fn as_slice(&self) -> &[u8];
/// slice returns data starting from offset off of size sz.
///
/// # Panics
/// If there's not enough data, it would
/// panic.
fn slice(&self, offset: usize, sz: usize) -> &[u8] {
&self.as_slice()[offset..offset + sz]
}
/// bytes returns data starting from offset off of size sz.
///
/// # Errors
/// If there's not enough data, it would return
/// `Err(Error::from(ErrorKind::UnexpectedEof))`.
fn bytes(&self, offset: usize, sz: usize) -> Result<&[u8]> {
let buf = self.as_slice();
super::checked_range(offset, sz, buf.len()).map(|range| &buf[range])
}
/// Returns the path of the inner file.
fn path(&self) -> &Path;
/// Returns the path buf of the inner file.
fn path_buf(&self) -> PathBuf {
self.path().to_path_buf()
}
/// Returns the path lossy string of the inner file.
fn path_lossy(&self) -> Cow<'_, str> {
self.path().to_string_lossy()
}
/// Returns the path string of the inner file.
fn path_string(&self) -> String {
self.path_lossy().to_string()
}
/// Returns the metadata of file metadata
///
/// Metadata information about a file.
/// This structure is returned from the metadata or
/// symlink_metadata function or method and represents
/// known metadata about a file such as its permissions, size, modification times, etc
fn metadata(&self) -> Result<MetaData>;
/// Whether the mmap is executable.
fn is_exec(&self) -> bool;
/// Copy the content of the mmap file to Vec
#[inline]
fn copy_all_to_vec(&self) -> Vec<u8> {
self.as_slice().to_vec()
}
/// Copy a range of content of the mmap file to Vec
#[inline]
fn copy_range_to_vec(&self, offset: usize, len: usize) -> Vec<u8> {
self.slice(offset, len).to_vec()
}
/// Write the content of the mmap file to a new file.
///
/// The destination is opened with plain `OpenOptions::create_new(true)`
/// — we deliberately do NOT mmap it. Mmapping a destination would push
/// the crate's "no concurrent mutators / truncators" precondition onto
/// every safe caller of this helper, which is a footgun: a caller in
/// shared storage where another actor truncates the destination during
/// the write would hit UB / SIGBUS.
///
/// On success the new file is durably created: bytes synced via
/// `sync_all`, and the parent directory fsynced so the new directory
/// entry is committed too. Without the parent fsync, a crash could
/// leave the data on disk but the filename absent.
#[inline]
fn write_all_to_new_file<P: AsRef<Path>>(&self, new_file_path: P) -> Result<()> {
use std::io::Write as _;
let path = new_file_path.as_ref();
let buf = self.as_slice();
let mut file = std::fs::OpenOptions::new()
.create_new(true)
.read(true)
.write(true)
.open(path)?;
file.write_all(buf)?;
file.sync_all()?;
sync_new_file_parent(path)
}
/// Write a range of content of the mmap file to new file.
#[inline]
fn write_range_to_new_file<P: AsRef<Path>>(
&self,
new_file_path: P,
offset: usize,
len: usize,
) -> Result<()> {
use std::io::Write as _;
let path = new_file_path.as_ref();
let buf = self.as_slice();
let range = super::checked_range(offset, len, buf.len())?;
// See `write_all_to_new_file` for the no-mmap rationale.
let mut file = std::fs::OpenOptions::new()
.create_new(true)
.read(true)
.write(true)
.open(path)?;
file.write_all(&buf[range])?;
file.sync_all()?;
sync_new_file_parent(path)
}
/// Returns a [`MmapFileReader`] which helps read data from mmap like a normal File.
///
/// # Errors
/// If there's not enough data, it would return
/// `Err(Error::from(ErrorKind::UnexpectedEof))`.
///
/// [`MmapFileReader`]: structs.MmapFileReader.html
fn reader(&self, offset: usize) -> Result<MmapFileReader<'_>> {
let buf = self.as_slice();
if buf.len() < offset {
Err(Error::from(ErrorKind::UnexpectedEof))
} else {
Ok(MmapFileReader::new(
Cursor::new(&buf[offset..]),
offset,
buf.len() - offset,
))
}
}
/// Returns a [`MmapFileReader`] base on the given `offset` and `len`, which helps read data from mmap like a normal File.
///
/// # Errors
/// If there's not enough data, it would return
/// `Err(Error::from(ErrorKind::UnexpectedEof))`.
///
/// [`MmapFileReader`]: structs.MmapFileReader.html
fn range_reader(&self, offset: usize, len: usize) -> Result<MmapFileReader<'_>> {
let buf = self.as_slice();
let range = super::checked_range(offset, len, buf.len())?;
Ok(MmapFileReader::new(Cursor::new(&buf[range]), offset, len))
}
/// Locks the file for exclusively usage, blocking if the file is currently locked.
///
/// # Notes
/// This function will do nothing if the underlying is not a real file, e.g. in-memory.
fn lock(&mut self) -> Result<()>;
/// Locks the file for shared usage, blocking if the file is currently locked exclusively.
///
/// # Safety
/// On a `MmapFileMut` the constructor auto-acquired an exclusive lock to
/// guarantee that no other writable or read-only mapping of the same file
/// can be opened. Calling `lock_shared` on `flock`-style platforms downgrades
/// that exclusive lock to a shared lock, which then allows another process
/// (or another `fmmap` handle in the same process) to open a read-only
/// mapping of the same file. The resulting concurrent `&mut [u8]` from this
/// writer and `&[u8]` from the reader alias the same bytes — which is
/// undefined behavior.
///
/// Callers must ensure no conflicting mapping of the same file can be
/// created for as long as this mapping (and any borrowed slices it has
/// yielded) lives.
///
/// On a read-only `MmapFile` this call is a no-op (the auto lock is already
/// shared) and is sound, but is still marked `unsafe` because the trait is
/// shared between read-only and writable types.
///
/// # Notes
/// This function will do nothing if the underlying is not a real file, e.g. in-memory.
unsafe fn lock_shared(&mut self) -> Result<()>;
/// Locks the file for exclusively usage, or returns a an error if the file is currently locked (see lock_contended_error).
///
/// # Notes
/// This function will do nothing if the underlying is not a real file, e.g. in-memory.
fn try_lock(&mut self) -> Result<()>;
/// Locks the file for shared usage, or returns a an error if the file is currently locked exclusively (see lock_contended_error).
///
/// # Safety
/// Same hazard as [`lock_shared`]: on a writable mapping this can downgrade
/// the auto-acquired exclusive lock to a shared lock and allow another
/// concurrent mapping of the same file, producing aliasing UB.
///
/// # Notes
/// This function will do nothing if the underlying is not a real file, e.g. in-memory.
unsafe fn try_lock_shared(&mut self) -> Result<()>;
/// Unlocks the file.
///
/// # Safety
/// `MmapFile`/`MmapFileMut` constructors automatically acquire a file lock
/// (shared or exclusive) to prevent the underlying file from being mapped
/// concurrently with conflicting access. Calling `unlock` releases that
/// guard; if any other process or `fmmap` instance subsequently opens the
/// same file with a writable mapping while this mapping is alive, the two
/// mappings will alias each other, which is undefined behavior.
///
/// Callers must therefore ensure no conflicting mapping of the same file
/// can be created for as long as this mapping (and any borrowed slices it
/// has yielded) lives.
///
/// # Notes
/// This function will do nothing if the underlying is not a real file, e.g. in-memory.
unsafe fn unlock(&mut self) -> Result<()>;
/// Read bytes to the dst buf from the offset, returns how many bytes read.
fn read(&self, dst: &mut [u8], offset: usize) -> usize {
let buf = self.as_slice();
if buf.len() < offset {
0
} else {
let remaining = buf.len() - offset;
let dst_len = dst.len();
if remaining > dst_len {
dst.copy_from_slice(&buf[offset..offset + dst_len]);
dst_len
} else {
dst[..remaining].copy_from_slice(&buf[offset..offset + remaining]);
remaining
}
}
}
/// Read the exact number of bytes required to fill buf.
fn read_exact(&self, dst: &mut [u8], offset: usize) -> Result<()> {
let buf = self.as_slice();
let remaining = buf.len().checked_sub(offset);
match remaining {
None => Err(Error::from(ErrorKind::UnexpectedEof)),
Some(remaining) => {
let dst_len = dst.len();
if remaining < dst_len {
Err(Error::from(ErrorKind::UnexpectedEof))
} else {
dst.copy_from_slice(&buf[offset..offset + dst_len]);
Ok(())
}
}
}
}
/// Read a signed 8 bit integer from offset.
fn read_i8(&self, offset: usize) -> Result<i8> {
let buf = self.as_slice();
let remaining = buf.len().checked_sub(offset);
match remaining {
None => Err(Error::from(ErrorKind::UnexpectedEof)),
Some(remaining) => {
if remaining < 1 {
Err(Error::from(ErrorKind::UnexpectedEof))
} else {
Ok(buf[offset] as i8)
}
}
}
}
/// Read a signed 16 bit integer from offset in big-endian byte order.
fn read_i16(&self, offset: usize) -> Result<i16> {
read_impl!(self, offset, i16::from_be_bytes)
}
/// Read a signed 16 bit integer from offset in little-endian byte order.
fn read_i16_le(&self, offset: usize) -> Result<i16> {
read_impl!(self, offset, i16::from_le_bytes)
}
/// Read a signed integer from offset in big-endian byte order.
fn read_isize(&self, offset: usize) -> Result<isize> {
read_impl!(self, offset, isize::from_be_bytes)
}
/// Read a signed integer from offset in little-endian byte order.
fn read_isize_le(&self, offset: usize) -> Result<isize> {
read_impl!(self, offset, isize::from_le_bytes)
}
/// Read a signed 32 bit integer from offset in big-endian byte order.
fn read_i32(&self, offset: usize) -> Result<i32> {
read_impl!(self, offset, i32::from_be_bytes)
}
/// Read a signed 32 bit integer from offset in little-endian byte order.
fn read_i32_le(&self, offset: usize) -> Result<i32> {
read_impl!(self, offset, i32::from_le_bytes)
}
/// Read a signed 64 bit integer from offset in big-endian byte order.
fn read_i64(&self, offset: usize) -> Result<i64> {
read_impl!(self, offset, i64::from_be_bytes)
}
/// Read a signed 64 bit integer from offset in little-endian byte order.
fn read_i64_le(&self, offset: usize) -> Result<i64> {
read_impl!(self, offset, i64::from_le_bytes)
}
/// Read a signed 128 bit integer from offset in big-endian byte order.
fn read_i128(&self, offset: usize) -> Result<i128> {
read_impl!(self, offset, i128::from_be_bytes)
}
/// Read a signed 128 bit integer from offset in little-endian byte order.
fn read_i128_le(&self, offset: usize) -> Result<i128> {
read_impl!(self, offset, i128::from_le_bytes)
}
/// Read an unsigned 8 bit integer from offset.
fn read_u8(&self, offset: usize) -> Result<u8> {
let buf = self.as_slice();
let remaining = buf.len().checked_sub(offset);
match remaining {
None => Err(Error::from(ErrorKind::UnexpectedEof)),
Some(remaining) => {
if remaining < 1 {
Err(Error::from(ErrorKind::UnexpectedEof))
} else {
Ok(buf[offset])
}
}
}
}
/// Read an unsigned 16 bit integer from offset in big-endian.
fn read_u16(&self, offset: usize) -> Result<u16> {
read_impl!(self, offset, u16::from_be_bytes)
}
/// Read an unsigned 16 bit integer from offset in little-endian.
fn read_u16_le(&self, offset: usize) -> Result<u16> {
read_impl!(self, offset, u16::from_le_bytes)
}
/// Read an unsigned integer from offset in big-endian byte order.
fn read_usize(&self, offset: usize) -> Result<usize> {
read_impl!(self, offset, usize::from_be_bytes)
}
/// Read an unsigned integer from offset in little-endian byte order.
fn read_usize_le(&self, offset: usize) -> Result<usize> {
read_impl!(self, offset, usize::from_le_bytes)
}
/// Read an unsigned 32 bit integer from offset in big-endian.
fn read_u32(&self, offset: usize) -> Result<u32> {
read_impl!(self, offset, u32::from_be_bytes)
}
/// Read an unsigned 32 bit integer from offset in little-endian.
fn read_u32_le(&self, offset: usize) -> Result<u32> {
read_impl!(self, offset, u32::from_le_bytes)
}
/// Read an unsigned 64 bit integer from offset in big-endian.
fn read_u64(&self, offset: usize) -> Result<u64> {
read_impl!(self, offset, u64::from_be_bytes)
}
/// Read an unsigned 64 bit integer from offset in little-endian.
fn read_u64_le(&self, offset: usize) -> Result<u64> {
read_impl!(self, offset, u64::from_le_bytes)
}
/// Read an unsigned 128 bit integer from offset in big-endian.
fn read_u128(&self, offset: usize) -> Result<u128> {
read_impl!(self, offset, u128::from_be_bytes)
}
/// Read an unsigned 128 bit integer from offset in little-endian.
fn read_u128_le(&self, offset: usize) -> Result<u128> {
read_impl!(self, offset, u128::from_le_bytes)
}
/// Read an IEEE754 single-precision (4 bytes) floating point number from
/// offset in big-endian byte order.
fn read_f32(&self, offset: usize) -> Result<f32> {
read_impl!(self, offset, f32::from_be_bytes)
}
/// Read an IEEE754 single-precision (4 bytes) floating point number from
/// offset in little-endian byte order.
fn read_f32_le(&self, offset: usize) -> Result<f32> {
read_impl!(self, offset, f32::from_le_bytes)
}
/// Read an IEEE754 single-precision (8 bytes) floating point number from
/// offset in big-endian byte order.
fn read_f64(&self, offset: usize) -> Result<f64> {
read_impl!(self, offset, f64::from_be_bytes)
}
/// Read an IEEE754 single-precision (8 bytes) floating point number from
/// offset in little-endian byte order.
fn read_f64_le(&self, offset: usize) -> Result<f64> {
read_impl!(self, offset, f64::from_le_bytes)
}
}
/// Utility methods to [`MmapFileMut`]
///
/// [`MmapFileMut`]: structs.MmapFileMut.html
#[enum_dispatch]
pub trait MmapFileMutExt {
/// Returns the mutable underlying slice of the mmap
fn as_mut_slice(&mut self) -> &mut [u8];
/// slice_mut returns mutable data starting from offset off of size sz.
///
/// # Panics
/// If there's not enough data, it would
/// panic.
fn slice_mut(&mut self, offset: usize, sz: usize) -> &mut [u8] {
&mut self.as_mut_slice()[offset..offset + sz]
}
/// Whether mmap is copy on write
fn is_cow(&self) -> bool;
/// bytes_mut returns mutable data starting from offset off of size sz.
///
/// # Errors
/// If there's not enough data, it would return
/// `Err(Error::from(ErrorKind::UnexpectedEof))`.
fn bytes_mut(&mut self, offset: usize, sz: usize) -> Result<&mut [u8]> {
let buf = self.as_mut_slice();
super::checked_range(offset, sz, buf.len()).map(|range| &mut buf[range])
}
/// Fill 0 to the specific range
fn zero_range(&mut self, start: usize, end: usize) {
let buf = self.as_mut_slice();
let end = end.min(buf.len());
buf[start..end].fill(0);
}
/// Flushes outstanding memory map modifications to disk (if the inner is a real file).
///
/// When this method returns with a non-error result,
/// all outstanding changes to a file-backed memory map are guaranteed to be durably stored.
/// The file’s metadata (including last modification timestamp) may not be updated.
fn flush(&self) -> Result<()>;
/// Asynchronously flushes outstanding memory map modifications to disk(if the inner is a real file).
///
/// This method initiates flushing modified pages to durable storage,
/// but it will not wait for the operation to complete before returning.
/// The file’s metadata (including last modification timestamp) may not be updated.
fn flush_async(&self) -> Result<()>;
/// Flushes outstanding memory map modifications in the range to disk(if the inner is a real file).
///
/// The offset and length must be in the bounds of the memory map.
///
/// When this method returns with a non-error result,
/// all outstanding changes to a file-backed memory
/// in the range are guaranteed to be durable stored.
/// The file’s metadata (including last modification timestamp) may not be updated.
/// It is not guaranteed the only the changes in the specified range are flushed;
/// other outstanding changes to the memory map may be flushed as well.
fn flush_range(&self, offset: usize, len: usize) -> Result<()>;
/// Asynchronously flushes outstanding memory map modifications in the range to disk(if the inner is a real file).
///
/// The offset and length must be in the bounds of the memory map.
///
/// This method initiates flushing modified pages to durable storage,
/// but it will not wait for the operation to complete before returning.
/// The file’s metadata (including last modification timestamp) may not be updated.
/// It is not guaranteed that the only changes flushed are those in the specified range;
/// other outstanding changes to the memory map may be flushed as well.
fn flush_async_range(&self, offset: usize, len: usize) -> Result<()>;
/// Truncates the file to the `max_size`, which will lead to
/// do re-mmap and sync_dir if the inner is a real file.
fn truncate(&mut self, max_sz: u64) -> Result<()>;
/// Remove the underlying file
fn drop_remove(self) -> Result<()>;
/// Close and truncate the underlying file
fn close_with_truncate(self, max_sz: i64) -> Result<()>;
/// Returns a [`MmapFileWriter`] base on the given `offset`, which helps read or write data from mmap like a normal File.
///
/// # Notes
/// If you use a writer to write data to mmap, there is no guarantee all
/// data will be durably stored. So you need to call [`flush`]/[`flush_range`]/[`flush_async`]/[`flush_async_range`] in [`MmapFileMutExt`]
/// to guarantee all data will be durably stored.
///
/// # Errors
/// If there's not enough data, it would return
/// `Err(Error::from(ErrorKind::UnexpectedEof))`.
///
/// [`flush`]: traits.MmapFileMutExt.html#methods.flush
/// [`flush_range`]: traits.MmapFileMutExt.html#methods.flush_range
/// [`flush_async`]: traits.MmapFileMutExt.html#methods.flush_async
/// [`flush_async_range`]: traits.MmapFileMutExt.html#methods.flush_async_range
/// [`MmapFileWriter`]: structs.MmapFileWriter.html
fn writer(&mut self, offset: usize) -> Result<MmapFileWriter<'_>> {
let buf = self.as_mut_slice();
let buf_len = buf.len();
if buf_len < offset {
Err(Error::from(ErrorKind::UnexpectedEof))
} else {
Ok(MmapFileWriter::new(
Cursor::new(&mut buf[offset..]),
offset,
buf_len - offset,
))
}
}
/// Returns a [`MmapFileWriter`] base on the given `offset` and `len`, which helps read or write data from mmap like a normal File.
///
/// # Notes
/// If you use a writer to write data to mmap, there is no guarantee all
/// data will be durably stored. So you need to call [`flush`]/[`flush_range`]/[`flush_async`]/[`flush_async_range`] in [`MmapFileMutExt`]
/// to guarantee all data will be durably stored.
///
/// # Errors
/// If there's not enough data, it would return
/// `Err(Error::from(ErrorKind::UnexpectedEof))`.
///
/// [`flush`]: traits.MmapFileMutExt.html#methods.flush
/// [`flush_range`]: traits.MmapFileMutExt.html#methods.flush_range
/// [`flush_async`]: traits.MmapFileMutExt.html#methods.flush_async
/// [`flush_async_range`]: traits.MmapFileMutExt.html#methods.flush_async_range
/// [`MmapFileWriter`]: structs.MmapFileWriter.html
fn range_writer(&mut self, offset: usize, len: usize) -> Result<MmapFileWriter<'_>> {
let buf = self.as_mut_slice();
let range = super::checked_range(offset, len, buf.len())?;
Ok(MmapFileWriter::new(
Cursor::new(&mut buf[range]),
offset,
len,
))
}
/// Write bytes to the mmap from the offset.
fn write(&mut self, src: &[u8], offset: usize) -> usize {
let buf = self.as_mut_slice();
if buf.len() <= offset {
0
} else {
let remaining = buf.len() - offset;
let src_len = src.len();
if remaining > src_len {
buf[offset..offset + src_len].copy_from_slice(src);
src_len
} else {
buf[offset..offset + remaining].copy_from_slice(&src[..remaining]);
remaining
}
}
}
/// Write the all of bytes in `src` to the mmap from the offset.
fn write_all(&mut self, src: &[u8], offset: usize) -> Result<()> {
let buf = self.as_mut_slice();
let remaining = buf.len().checked_sub(offset);
match remaining {
None => Err(Error::from(ErrorKind::UnexpectedEof)),
Some(remaining) => {
let src_len = src.len();
if remaining < src_len {
Err(Error::from(ErrorKind::UnexpectedEof))
} else {
buf[offset..offset + src_len].copy_from_slice(src);
Ok(())
}
}
}
}
/// Writes a signed 8 bit integer to mmap from the offset.
fn write_i8(&mut self, val: i8, offset: usize) -> Result<()> {
self.write_all(&[val as u8], offset)
}
/// Writes a signed 16 bit integer to mmap from the offset in the big-endian byte order.
fn write_i16(&mut self, val: i16, offset: usize) -> Result<()> {
self.write_all(&val.to_be_bytes(), offset)
}
/// Writes a signed 16 bit integer to mmap from the offset in the little-endian byte order.
fn write_i16_le(&mut self, val: i16, offset: usize) -> Result<()> {
self.write_all(&val.to_le_bytes(), offset)
}
/// Writes a signed integer to mmap from the offset in the big-endian byte order.
fn write_isize(&mut self, val: isize, offset: usize) -> Result<()> {
self.write_all(&val.to_be_bytes(), offset)
}
/// Writes a signed integer to mmap from the offset in the little-endian byte order.
fn write_isize_le(&mut self, val: isize, offset: usize) -> Result<()> {
self.write_all(&val.to_le_bytes(), offset)
}
/// Writes a signed 32 bit integer to mmap from the offset in the big-endian byte order.
fn write_i32(&mut self, val: i32, offset: usize) -> Result<()> {
self.write_all(&val.to_be_bytes(), offset)
}
/// Writes a signed 32 bit integer to mmap from the offset in the little-endian byte order.
fn write_i32_le(&mut self, val: i32, offset: usize) -> Result<()> {
self.write_all(&val.to_le_bytes(), offset)
}
/// Writes a signed 64 bit integer to mmap from the offset in the big-endian byte order.
fn write_i64(&mut self, val: i64, offset: usize) -> Result<()> {
self.write_all(&val.to_be_bytes(), offset)
}
/// Writes a signed 64 bit integer to mmap from the offset in the little-endian byte order.
fn write_i64_le(&mut self, val: i64, offset: usize) -> Result<()> {
self.write_all(&val.to_le_bytes(), offset)
}
/// Writes a signed 128 bit integer to mmap from the offset in the big-endian byte order.
fn write_i128(&mut self, val: i128, offset: usize) -> Result<()> {
self.write_all(&val.to_be_bytes(), offset)
}
/// Writes a signed 128 bit integer to mmap from the offset in the little-endian byte order.
fn write_i128_le(&mut self, val: i128, offset: usize) -> Result<()> {
self.write_all(&val.to_le_bytes(), offset)
}
/// Writes an unsigned 8 bit integer to mmap from the offset.
fn write_u8(&mut self, val: u8, offset: usize) -> Result<()> {
self.write_all(&[val], offset)
}
/// Writes an unsigned 16 bit integer to mmap from the offset in the big-endian byte order.
fn write_u16(&mut self, val: u16, offset: usize) -> Result<()> {
self.write_all(&val.to_be_bytes(), offset)
}
/// Writes an unsigned 16 bit integer to mmap from the offset in the little-endian byte order.
fn write_u16_le(&mut self, val: u16, offset: usize) -> Result<()> {
self.write_all(&val.to_le_bytes(), offset)
}
/// Writes an unsigned integer to mmap from the offset in the big-endian byte order.
fn write_usize(&mut self, val: usize, offset: usize) -> Result<()> {
self.write_all(&val.to_be_bytes(), offset)
}
/// Writes an unsigned integer to mmap from the offset in the little-endian byte order.
fn write_usize_le(&mut self, val: usize, offset: usize) -> Result<()> {
self.write_all(&val.to_le_bytes(), offset)
}
/// Writes an unsigned 32 bit integer to mmap from the offset in the big-endian byte order.
fn write_u32(&mut self, val: u32, offset: usize) -> Result<()> {
self.write_all(&val.to_be_bytes(), offset)
}
/// Writes an unsigned 32 bit integer to mmap from the offset in the little-endian byte order.
fn write_u32_le(&mut self, val: u32, offset: usize) -> Result<()> {
self.write_all(&val.to_le_bytes(), offset)
}
/// Writes an unsigned 64 bit integer to mmap from the offset in the big-endian byte order.
fn write_u64(&mut self, val: u64, offset: usize) -> Result<()> {
self.write_all(&val.to_be_bytes(), offset)
}
/// Writes an unsigned 64 bit integer to mmap from the offset in the little-endian byte order.
fn write_u64_le(&mut self, val: u64, offset: usize) -> Result<()> {
self.write_all(&val.to_le_bytes(), offset)
}
/// Writes an unsigned 128 bit integer to mmap from the offset in the big-endian byte order.
fn write_u128(&mut self, val: u128, offset: usize) -> Result<()> {
self.write_all(&val.to_be_bytes(), offset)
}
/// Writes an unsigned 128 bit integer to mmap from the offset in the little-endian byte order.
fn write_u128_le(&mut self, val: u128, offset: usize) -> Result<()> {
self.write_all(&val.to_le_bytes(), offset)
}
/// Writes an IEEE754 single-precision (4 bytes) floating point number to mmap from the offset in big-endian byte order.
fn write_f32(&mut self, val: f32, offset: usize) -> Result<()> {
self.write_all(&val.to_be_bytes(), offset)
}
/// Writes an IEEE754 single-precision (4 bytes) floating point number to mmap from the offset in little-endian byte order.
fn write_f32_le(&mut self, val: f32, offset: usize) -> Result<()> {
self.write_all(&val.to_le_bytes(), offset)
}
/// Writes an IEEE754 single-precision (8 bytes) floating point number to mmap from the offset in big-endian byte order.
fn write_f64(&mut self, val: f64, offset: usize) -> Result<()> {
self.write_all(&val.to_be_bytes(), offset)
}
/// Writes an IEEE754 single-precision (8 bytes) floating point number to mmap from the offset in little-endian byte order.
fn write_f64_le(&mut self, val: f64, offset: usize) -> Result<()> {
self.write_all(&val.to_le_bytes(), offset)
}
}
#[enum_dispatch(MmapFileExt)]
enum MmapFileInner {
Empty(EmptyMmapFile),
Memory(MemoryMmapFile),
Disk(DiskMmapFile),
}
/// A read-only memory map file.
///
/// There is 3 status of this struct:
/// - __Disk__: mmap to a real file
/// - __Memory__: use [`Bytes`] to mock a mmap, which is useful for test and in-memory storage engine
/// - __Empty__: a state represents null mmap, which is helpful for drop, close the `MmapFile`. This state cannot be constructed directly.
///
/// [`Bytes`]: https://docs.rs/bytes/1.1.0/bytes/struct.Bytes.html
#[repr(transparent)]
pub struct MmapFile {
inner: MmapFileInner,
}
impl_mmap_file_ext!(MmapFile);
impl_from!(
MmapFile,
MmapFileInner,
[EmptyMmapFile, MemoryMmapFile, DiskMmapFile]
);
impl MmapFile {
/// Open a readable memory map backed by a file
///
/// # Examples
///
/// ```no_compile
/// use fmmap::{MmapFile, MmapFileExt};
/// use std::fs::{remove_file, File};
/// use std::io::Write;
/// # use scopeguard::defer;
///
/// # let mut file = File::create("open_test.txt").unwrap();
/// # defer!(remove_file("open_test.txt").unwrap());
/// # file.write_all("some data...".as_bytes()).unwrap();
/// # drop(file);
///
/// // open and mmap the file
/// let mut file = MmapFile::open("open_test.txt").unwrap();
/// let mut buf = vec![0; "some data...".len()];
/// file.read_exact(buf.as_mut_slice(), 0);
/// assert_eq!(buf.as_slice(), "some data...".as_bytes());
/// ```
/// # Safety
///
/// See the [crate-level safety section](crate) for the full contract.
///
pub unsafe fn open<P: AsRef<Path>>(path: P) -> Result<Self> {
Ok(Self::from(DiskMmapFile::open(path)?))
}
/// Open a readable memory map backed by a file with [`Options`]
///
/// # Examples
///
/// ```no_compile
/// use fmmap::{Options, MmapFile, MmapFileExt};
/// # use scopeguard::defer;
///
/// # let mut file = std::fs::File::create("open_test_with_options.txt").unwrap();
/// # defer!(std::fs::remove_file("open_test_with_options.txt").unwrap());
/// # std::io::Write::write_all(&mut file, "sanity text".as_bytes()).unwrap();
/// # std::io::Write::write_all(&mut file, "some data...".as_bytes()).unwrap();
/// # drop(file);
///
/// // mmap the file with options
/// let opts = Options::new()
/// // allow read
/// .read(true)
/// // allow write
/// .write(true)
/// // allow append
/// .append(true)
/// // truncate to 100
/// .max_size(100)
/// // mmap content after the sanity text
/// .offset("sanity text".as_bytes().len() as u64);
/// // open and mmap the file
/// let mut file = MmapFile::open_with_options("open_test_with_options.txt", opts).unwrap();
/// let mut buf = vec![0; "some data...".len()];
/// file.read_exact(buf.as_mut_slice(), 0);
/// assert_eq!(buf.as_slice(), "some data...".as_bytes());
/// ```
///
/// [`Options`]: struct.Options.html
/// # Safety
///
/// See the [crate-level safety section](crate) for the full contract.
///
pub unsafe fn open_with_options<P: AsRef<Path>>(path: P, opts: Options) -> Result<Self> {
Ok(Self::from(DiskMmapFile::open_with_options(path, opts)?))
}
/// Open a readable memory map backed by a file
///
/// # Examples
///
/// ```no_compile
/// use fmmap::{MmapFile, MmapFileExt};
/// use std::fs::{remove_file, File};
/// use std::io::Write;
/// # use scopeguard::defer;
///
/// # let mut file = File::create("open_exec_test.txt").unwrap();
/// # defer!(remove_file("open_exec_test.txt").unwrap());
/// # file.write_all("some data...".as_bytes()).unwrap();
/// # drop(file);
///
/// // open and mmap the file
/// let mut file = MmapFile::open_exec("open_exec_test.txt").unwrap();
/// let mut buf = vec![0; "some data...".len()];
/// file.read_exact(buf.as_mut_slice(), 0);
/// assert_eq!(buf.as_slice(), "some data...".as_bytes());
/// ```
/// # Safety
///
/// See the [crate-level safety section](crate) for the full contract.
///
pub unsafe fn open_exec<P: AsRef<Path>>(path: P) -> Result<Self> {
Ok(Self::from(DiskMmapFile::open_exec(path)?))
}
/// Open a readable and executable memory map backed by a file with [`Options`].
///
/// # Examples
///
/// ```no_compile
/// use fmmap::{Options, MmapFile, MmapFileExt};
/// # use scopeguard::defer;
///
/// # let mut file = std::fs::File::create("open_exec_test_with_options.txt").unwrap();
/// # defer!(std::fs::remove_file("open_exec_test_with_options.txt").unwrap());
/// # std::io::Write::write_all(&mut file, "sanity text".as_bytes()).unwrap();
/// # std::io::Write::write_all(&mut file, "some data...".as_bytes()).unwrap();
/// # drop(file);
///
/// // mmap the file with options
/// let opts = Options::new()
/// // allow read
/// .read(true)
/// // mmap content after the sanity text
/// .offset("sanity text".as_bytes().len() as u64);
/// // open and mmap the file
/// let mut file = MmapFile::open_exec_with_options("open_exec_test_with_options.txt", opts).unwrap();
/// let mut buf = vec![0; "some data...".len()];
/// file.read_exact(buf.as_mut_slice(), 0);
/// assert_eq!(buf.as_slice(), "some data...".as_bytes());
/// ```
///
/// [`Options`]: struct.Options.html
/// # Safety
///
/// See the [crate-level safety section](crate) for the full contract.
///
pub unsafe fn open_exec_with_options<P: AsRef<Path>>(path: P, opts: Options) -> Result<Self> {
Ok(Self::from(DiskMmapFile::open_exec_with_options(
path, opts,
)?))
}
}
impl_constructor_for_memory_mmap_file!(MemoryMmapFile, MmapFile, "MmapFile", "sync");
#[enum_dispatch(MmapFileExt, MmapFileMutExt)]
enum MmapFileMutInner {
Empty(EmptyMmapFile),
Memory(MemoryMmapFileMut),
Disk(DiskMmapFileMut),
}
/// A writable memory map file.
///
/// There is 3 status of this struct:
/// - __Disk__: mmap to a real file
/// - __Memory__: use [`BytesMut`] to mock a mmap, which is useful for test and in-memory storage engine
/// - __Empty__: a state represents null mmap, which is helpful for drop, remove, close the `MmapFileMut`. This state cannot be constructed directly.
///
/// [`BytesMut`]: https://docs.rs/bytes/1.1.0/bytes/struct.BytesMut.html
pub struct MmapFileMut {
inner: MmapFileMutInner,
remove_on_drop: bool,
deleted: bool,
/// User-requested deletion that failed and must be retried on `Drop`,
/// regardless of `remove_on_drop`. The `PendingDelete` variant tracks
/// whether the inode was already unlinked (so retry must NOT call
/// `remove_file` again — path-reuse safety) or whether unlink itself
/// still needs to happen.
pending_drop_remove: Option<crate::mmap_file::PendingDelete>,
/// Path retained so `Drop`'s opt-in `remove_on_drop` cleanup has a target
/// after the inner mapping was already dropped — e.g. consuming
/// `close_with_truncate(self)` failed mid-way and the inner is now
/// `Empty`.
pending_remove_path: Option<PathBuf>,
}
impl_from_mut!(
MmapFileMut,
MmapFileMutInner,
[EmptyMmapFile, MemoryMmapFileMut, DiskMmapFileMut]
);
impl_mmap_file_ext!(MmapFileMut);
impl MmapFileMutExt for MmapFileMut {
fn as_mut_slice(&mut self) -> &mut [u8] {
self.inner.as_mut_slice()
}
fn is_cow(&self) -> bool {
self.inner.is_cow()
}
impl_flush!();
fn truncate(&mut self, max_sz: u64) -> Result<()> {
// Just dispatch — the disk backend's `truncate` already keeps the
// poisoned `DiskMmapFileMut` installed with its `path`/`file` intact,
// and the disk-side `len` / `as_slice` / `as_mut_slice` accessors all
// short-circuit to empty when `poisoned == true`. Swapping the inner
// to `Empty` here would silently lose the path so `Drop`'s
// `remove_on_drop` cleanup (and any subsequent `remove()` /
// `drop_remove()` retry) couldn't find the (possibly-resized) file.
self.inner.truncate(max_sz)
}
/// Remove the underlying file
///
/// # Examples
///
/// ```no_compile
/// use fmmap::{MmapFileMut, MmapFileMutExt};
/// # use scopeguard::defer;
///
/// let mut file = MmapFileMut::create("remove_test.txt").unwrap();
///
/// file.truncate(12);
/// file.write_all("some data...".as_bytes(), 0).unwrap();
/// file.flush().unwrap();
///
/// file.drop_remove().unwrap();
///
/// let err = std::fs::File::open("remove_test.txt");
/// assert_eq!(err.unwrap_err().kind(), std::io::ErrorKind::NotFound);
/// ```
fn drop_remove(mut self) -> Result<()> {
let empty = MmapFileMutInner::Empty(EmptyMmapFile::default());
let inner = mem::replace(&mut self.inner, empty);
match inner {
MmapFileMutInner::Disk(disk) => {
// Run the durable unlink at the wrapper layer so we can
// classify failures correctly (`NeedsUnlink` vs
// `NeedsParentSync`). If we delegated to the disk inner's
// `drop_remove` instead, a parent-sync failure would be
// indistinguishable from a real unlink failure, and Drop's
// retry could call `remove_file` on a path that's already been
// unlinked and possibly reused.
let path = disk.path.clone();
let identity = disk.file_identity;
drop(disk.mmap);
// Keep `disk.file` alive across the probe+unlink on POSIX so
// the inode it refers to cannot be recycled (which would let
// a fresh file at the same path pass identity). Windows:
// holding the file without FILE_SHARE_DELETE would prevent
// the unlink itself, so we drop it first and rely on
// `(volume_serial, file_index)` not being recycled.
#[cfg(unix)]
let pin: std::fs::File = disk.file;
#[cfg(not(unix))]
drop(disk.file);
match initial_remove_durably(
&path,
identity,
#[cfg(unix)]
pin,
) {
Ok(()) => {
self.deleted = true;
Ok(())
}
Err((pending, e)) => {
self.pending_drop_remove = Some(pending);
Err(e)
}
}
}
_ => {
// Memory/Empty drop_remove is a no-op.
self.deleted = true;
Ok(())
}
}
}
/// Close and truncate the underlying file
///
/// # Examples
///
/// ```no_compile
/// use fmmap::{MetaDataExt, MmapFileMut, MmapFileExt, MmapFileMutExt};
/// # use scopeguard::defer;
///
/// let mut file = MmapFileMut::create("close_with_truncate_test.txt").unwrap();
/// # defer!(std::fs::remove_file("close_with_truncate_test.txt").unwrap());
/// file.truncate(12);
/// file.write_all("some data...".as_bytes(), 0).unwrap();
/// file.flush().unwrap();
///
/// file.close_with_truncate(50).unwrap();
///
/// let file = MmapFileMut::open("close_with_truncate_test.txt").unwrap();
/// let meta = file.metadata().unwrap();
/// assert_eq!(meta.len(), 50);
/// ```
fn close_with_truncate(mut self, max_sz: i64) -> Result<()> {
// COW mappings are private — by contract they must not mutate the
// backing file. Refuse close-time truncation on COW; do it BEFORE
// touching the inner so the original mapping stays usable on error.
if max_sz >= 0 && self.is_cow() {
return Err(Error::new(
ErrorKind::Unsupported,
"cannot truncate a copy-on-write mmap file",
));
}
// Capture the path now in case any in-place fallible step fails and
// we need to surface a `pending_remove_path` for `remove_on_drop`.
let path = self.inner.path_buf();
if max_sz >= 0 {
// Run the destructive work in-place so a transient flush/set_len/
// sync failure leaves the disk inner *poisoned but intact* — its
// file handle is preserved, matching the inherent `close()`'s
// recovery model. Without this, a partial failure used to swap
// inner with `Empty` and lose the path/file.
if let MmapFileMutInner::Disk(disk) = &mut self.inner {
if let Err(e) = disk.close_with_truncate_in_place(max_sz as u64) {
if !path.as_os_str().is_empty() {
self.pending_remove_path = Some(path);
}
return Err(e);
}
}
} else if let Err(e) = self.flush() {
if !path.as_os_str().is_empty() {
self.pending_remove_path = Some(path);
}
return Err(e);
}
// All fallible work succeeded. Now drop the disk inner.
let empty = MmapFileMutInner::Empty(EmptyMmapFile::default());
drop(mem::replace(&mut self.inner, empty));
Ok(())
}
}
impl MmapFileMut {
/// Create a new file and mmap this file
///
/// # Notes
/// The new file is zero size, so before do write, you should truncate first.
/// Or you can use [`Options::create_mmap_file_mut`] and set `max_size` field for [`Options`] to enable directly write
/// without truncating.
///
/// # Examples
///
/// ```no_compile
/// use fmmap::{Options, MmapFileMut, MmapFileMutExt, MmapFileExt};
/// # use scopeguard::defer;
///
/// let mut file = MmapFileMut::create("create_test.txt").unwrap();
/// # defer!(std::fs::remove_file("create_test.txt").unwrap());
/// assert!(file.is_empty());
/// assert_eq!(file.path_string(), String::from("create_test.txt"));
///
/// file.truncate(12);
/// file.write_all("some data...".as_bytes(), 0).unwrap();
/// file.flush().unwrap();
/// ```
///
/// [`Options::create_mmap_file_mut`]: struct.Options.html#method.create_mmap_file_mut
/// [`Options`]: struct.Options.html
/// # Safety
///
/// See the [crate-level safety section](crate) for the full contract.
///
pub unsafe fn create<P: AsRef<Path>>(path: P) -> Result<Self> {
Ok(Self::from(DiskMmapFileMut::create(path)?))
}
/// Create a new file and mmap this file with [`Options`]
///
/// # Examples
///
/// ```no_compile
/// use fmmap::{Options, MmapFileMut, MmapFileMutExt, MmapFileExt};
/// # use scopeguard::defer;
///
/// let opts = Options::new()
/// // truncate to 100
/// .max_size(100);
/// let mut file = MmapFileMut::create_with_options("create_with_options_test.txt", opts).unwrap();
/// # defer!(std::fs::remove_file("create_with_options_test.txt").unwrap());
/// assert!(!file.is_empty());
///
/// file.write_all("some data...".as_bytes(), 0).unwrap();
/// file.flush().unwrap();
/// ```
///
/// [`Options`]: struct.Options.html
/// # Safety
///
/// See the [crate-level safety section](crate) for the full contract.
///
pub unsafe fn create_with_options<P: AsRef<Path>>(path: P, opts: Options) -> Result<Self> {
Ok(Self::from(DiskMmapFileMut::create_with_options(
path, opts,
)?))
}
/// Open or Create(if not exists) a file and mmap this file.
///
/// # Notes
/// If the file does not exist, then the new file will be open in zero size, so before do write, you should truncate first.
/// Or you can use [`open_with_options`] and set `max_size` field for [`Options`] to enable directly write
/// without truncating.
///
/// # Examples
///
/// File already exists
///
/// ```no_compile
/// use fmmap::{MmapFileMut, MmapFileExt, MmapFileMutExt};
/// use std::fs::File;
/// use std::io::{Read, Write};
/// # use scopeguard::defer;
///
/// # let mut file = File::create("open_test.txt").unwrap();
/// # defer!(std::fs::remove_file("open_test.txt").unwrap());
/// # file.write_all("some data...".as_bytes()).unwrap();
/// # drop(file);
///
/// // open and mmap the file
/// let mut file = MmapFileMut::open("open_test.txt").unwrap();
/// let mut buf = vec![0; "some data...".len()];
/// file.read_exact(buf.as_mut_slice(), 0);
/// assert_eq!(buf.as_slice(), "some data...".as_bytes());
///
/// // modify the file data
/// file.truncate("some modified data...".len() as u64).unwrap();
/// file.write_all("some modified data...".as_bytes(), 0).unwrap();
/// file.flush().unwrap();
/// drop(file);
///
/// // reopen to check content
/// let mut buf = vec![0; "some modified data...".len()];
/// let mut file = File::open("open_test.txt").unwrap();
/// file.read_exact(buf.as_mut_slice()).unwrap();
/// assert_eq!(buf.as_slice(), "some modified data...".as_bytes());
/// ```
///
/// File does not exists
///
/// ```no_run
/// use fmmap::{MmapFileMut, MmapFileExt, MmapFileMutExt};
/// use std::fs::{remove_file, File};
/// use std::io::{Read, Write};
/// # use scopeguard::defer;
///
/// // create and mmap the file
/// let mut file = unsafe { MmapFileMut::open("open_test.txt") }.unwrap();
/// # defer!(remove_file("open_test.txt").unwrap());
/// file.truncate(100).unwrap();
/// file.write_all("some data...".as_bytes(), 0).unwrap();
///
/// let mut buf = vec![0; "some data...".len()];
/// file.read_exact(buf.as_mut_slice(), 0);
/// assert_eq!(buf.as_slice(), "some data...".as_bytes());
///
/// // modify the file data
/// file.truncate("some modified data...".len() as u64).unwrap();
/// file.write_all("some modified data...".as_bytes(), 0).unwrap();
/// file.flush().unwrap();
/// drop(file);
///
/// // reopen to check content
/// let mut buf = vec![0; "some modified data...".len()];
/// let mut file = File::open("open_test.txt").unwrap();
/// file.read_exact(buf.as_mut_slice()).unwrap();
/// assert_eq!(buf.as_slice(), "some modified data...".as_bytes());
/// ```
///
/// [`open_with_options`]: struct.MmapFileMut.html#method.open_with_options
/// [`Options`]: struct.Options.html
/// # Safety
///
/// See the [crate-level safety section](crate) for the full contract.
///
pub unsafe fn open<P: AsRef<Path>>(path: P) -> Result<Self> {
Ok(Self::from(DiskMmapFileMut::open(path)?))
}
/// Open or Create(if not exists) a file and mmap this file with [`Options`].
///
/// # Examples
///
/// File already exists
///
/// ```no_compile
/// use fmmap::{MmapFileMut, MmapFileExt, MmapFileMutExt, Options};
/// use std::fs::{remove_file, File};
/// use std::io::{Read, Seek, SeekFrom, Write};
/// # use scopeguard::defer;
///
/// # let mut file = File::create("open_test_with_options.txt").unwrap();
/// # defer!(remove_file("open_test_with_options.txt").unwrap());
/// # file.write_all("sanity text".as_bytes()).unwrap();
/// # file.write_all("some data...".as_bytes()).unwrap();
/// # drop(file);
///
/// // mmap the file with options
/// let opts = Options::new()
/// // allow read
/// .read(true)
/// // allow write
/// .write(true)
/// // allow append
/// .append(true)
/// // truncate to 100
/// .max_size(100)
/// // mmap content after the sanity text
/// .offset("sanity text".as_bytes().len() as u64);
/// let mut file = unsafe { MmapFileMut::open_with_options("open_test_with_options.txt", opts) }.unwrap();
/// let mut buf = vec![0; "some data...".len()];
/// file.read_exact(buf.as_mut_slice(), 0);
/// assert_eq!(buf.as_slice(), "some data...".as_bytes());
///
/// // modify the file data
/// file.truncate(("some modified data...".len() + "sanity text".len()) as u64).unwrap();
/// file.write_all("some modified data...".as_bytes(), 0).unwrap();
/// file.flush().unwrap();
/// drop(file);
///
/// // reopen to check content
/// let mut buf = vec![0; "some modified data...".len()];
/// let mut file = File::open("open_test_with_options.txt").unwrap();
/// file.seek(SeekFrom::Start("sanity text".as_bytes().len() as u64)).unwrap();
/// file.read_exact(buf.as_mut_slice()).unwrap();
/// assert_eq!(buf.as_slice(), "some modified data...".as_bytes());
/// ```
///
/// File does not exists
///
/// ```no_run
/// use fmmap::{MmapFileMut, MmapFileExt, MmapFileMutExt, Options};
/// use std::fs::File;
/// use std::io::{Read, Write};
/// # use scopeguard::defer;
///
/// // mmap the file with options
/// let opts = Options::new()
/// // allow read
/// .read(true)
/// // allow write
/// .write(true)
/// // allow append
/// .append(true)
/// // truncate to 100
/// .max_size(100);
///
/// let mut file = unsafe { MmapFileMut::open_with_options("open_test_with_options.txt", opts) }.unwrap();
/// # defer!(std::fs::remove_file("open_test_with_options.txt").unwrap());
/// file.write_all("some data...".as_bytes(), 0).unwrap();
///
/// let mut buf = vec![0; "some data...".len()];
/// file.read_exact(buf.as_mut_slice(), 0);
/// assert_eq!(buf.as_slice(), "some data...".as_bytes());
///
/// // modify the file data
/// file.truncate("some modified data...".len() as u64).unwrap();
/// file.write_all("some modified data...".as_bytes(), 0).unwrap();
/// file.flush().unwrap();
/// drop(file);
///
/// // reopen to check content
/// let mut buf = vec![0; "some modified data...".len()];
/// let mut file = File::open("open_test_with_options.txt").unwrap();
/// file.read_exact(buf.as_mut_slice()).unwrap();
/// assert_eq!(buf.as_slice(), "some modified data...".as_bytes());
/// ```
///
/// [`Options`]: struct.Options.html
/// # Safety
///
/// See the [crate-level safety section](crate) for the full contract.
///
pub unsafe fn open_with_options<P: AsRef<Path>>(path: P, opts: Options) -> Result<Self> {
Ok(Self::from(DiskMmapFileMut::open_with_options(path, opts)?))
}
/// Open an existing file and mmap this file
///
/// # Examples
/// ```no_compile
/// use fmmap::{MmapFileMut, MmapFileExt, MmapFileMutExt};
/// use std::fs::File;
/// use std::io::{Read, Write};
/// # use scopeguard::defer;
///
/// // create a temp file
/// let mut file = File::create("open_existing_test.txt").unwrap();
/// # defer!(std::fs::remove_file("open_existing_test.txt").unwrap());
/// file.write_all("some data...".as_bytes()).unwrap();
/// drop(file);
///
/// // mmap the file
/// let mut file = MmapFileMut::open_exist("open_existing_test.txt").unwrap();
/// let mut buf = vec![0; "some data...".len()];
/// file.read_exact(buf.as_mut_slice(), 0);
/// assert_eq!(buf.as_slice(), "some data...".as_bytes());
///
/// // modify the file data
/// file.truncate("some modified data...".len() as u64).unwrap();
/// file.write_all("some modified data...".as_bytes(), 0).unwrap();
/// file.flush().unwrap();
/// drop(file);
///
/// // reopen to check content
/// let mut buf = vec![0; "some modified data...".len()];
/// let mut file = File::open("open_existing_test.txt").unwrap();
/// file.read_exact(buf.as_mut_slice()).unwrap();
/// assert_eq!(buf.as_slice(), "some modified data...".as_bytes());
/// ```
/// # Safety
///
/// See the [crate-level safety section](crate) for the full contract.
///
pub unsafe fn open_exist<P: AsRef<Path>>(path: P) -> Result<Self> {
Ok(Self::from(DiskMmapFileMut::open_exist(path)?))
}
/// Open an existing file and mmap this file with [`Options`]
///
/// # Examples
/// ```no_compile
/// use fmmap::{MmapFileMut, MmapFileExt, MmapFileMutExt, Options};
/// use std::fs::File;
/// use std::io::{Read, Seek, SeekFrom, Write};
/// # use scopeguard::defer;
///
/// // create a temp file
/// let mut file = File::create("open_existing_test_with_options.txt").unwrap();
/// # defer!(std::fs::remove_file("open_existing_test_with_options.txt").unwrap());
/// file.write_all("sanity text".as_bytes()).unwrap();
/// file.write_all("some data...".as_bytes()).unwrap();
/// drop(file);
///
/// // mmap the file with options
/// let opts = Options::new()
/// // truncate to 100
/// .max_size(100)
/// // mmap content after the sanity text
/// .offset("sanity text".as_bytes().len() as u64);
/// let mut file = MmapFileMut::open_exist_with_options("open_existing_test_with_options.txt", opts).unwrap();
/// let mut buf = vec![0; "some data...".len()];
/// file.read_exact(buf.as_mut_slice(), 0);
/// assert_eq!(buf.as_slice(), "some data...".as_bytes());
///
/// // modify the file data
/// file.truncate(("some modified data...".len() + "sanity text".len()) as u64).unwrap();
/// file.write_all("some modified data...".as_bytes(), 0).unwrap();
/// file.flush().unwrap();
/// drop(file);
///
/// // reopen to check content
/// let mut buf = vec![0; "some modified data...".len()];
/// let mut file = File::open("open_existing_test_with_options.txt").unwrap();
/// file.seek(SeekFrom::Start("sanity text".as_bytes().len() as u64)).unwrap();
/// file.read_exact(buf.as_mut_slice()).unwrap();
/// assert_eq!(buf.as_slice(), "some modified data...".as_bytes());
/// ```
///
/// [`Options`]: struct.Options.html
/// # Safety
///
/// See the [crate-level safety section](crate) for the full contract.
///
pub unsafe fn open_exist_with_options<P: AsRef<Path>>(path: P, opts: Options) -> Result<Self> {
Ok(Self::from(DiskMmapFileMut::open_exist_with_options(
path, opts,
)?))
}
/// Open and mmap an existing file in copy-on-write mode(copy-on-write memory map backed by a file).
/// Data written to the memory map will not be visible by other processes, and will not be carried through to the underlying file.
///
/// # Examples
///
/// ```no_compile
/// use fmmap::{MmapFileMut, MmapFileExt, MmapFileMutExt};
/// use std::fs::File;
/// use std::io::{Read, Write};
/// # use scopeguard::defer;
///
/// // create a temp file
/// let mut file = File::create("open_cow_test.txt").unwrap();
/// # defer!(std::fs::remove_file("open_cow_test.txt").unwrap());
/// file.write_all("some data...".as_bytes()).unwrap();
/// drop(file);
///
/// // mmap the file
/// let mut file = MmapFileMut::open_cow("open_cow_test.txt").unwrap();
/// assert!(file.is_cow());
///
/// let mut buf = vec![0; "some data...".len()];
/// file.read_exact(buf.as_mut_slice(), 0).unwrap();
/// assert_eq!(buf.as_slice(), "some data...".as_bytes());
///
/// // modify the file data
/// file.write_all("some data!!!".as_bytes(), 0).unwrap();
/// file.flush().unwrap();
///
/// // cow, change will only be seen in current caller
/// assert_eq!(file.as_slice(), "some data!!!".as_bytes());
/// drop(file);
///
/// // reopen to check content, cow will not change the content.
/// let mut file = File::open("open_cow_test.txt").unwrap();
/// let mut buf = vec![0; "some data...".len()];
/// file.read_exact(buf.as_mut_slice()).unwrap();
/// assert_eq!(buf.as_slice(), "some data...".as_bytes());
/// ```
///
/// [`Options`]: struct.Options.html
/// # Safety
///
/// See the [crate-level safety section](crate) for the full contract.
///
pub unsafe fn open_cow<P: AsRef<Path>>(path: P) -> Result<Self> {
Ok(Self::from(DiskMmapFileMut::open_cow(path)?))
}
/// Open and mmap an existing file in copy-on-write mode(copy-on-write memory map backed by a file) with [`Options`].
/// Data written to the memory map will not be visible by other processes, and will not be carried through to the underlying file.
///
/// # Examples
///
/// ```no_compile
/// use fmmap::{MmapFileMut, MmapFileExt, MmapFileMutExt, Options};
/// use std::fs::File;
/// use std::io::{Read, Seek, Write, SeekFrom};
/// # use scopeguard::defer;
///
/// // create a temp file
/// let mut file = File::create("open_cow_with_options_test.txt").unwrap();
/// # defer!(std::fs::remove_file("open_cow_with_options_test.txt").unwrap());
/// file.write_all("sanity text".as_bytes()).unwrap();
/// file.write_all("some data...".as_bytes()).unwrap();
/// drop(file);
///
/// // mmap the file with options
/// let opts = Options::new()
/// // mmap content after the sanity text
/// .offset("sanity text".as_bytes().len() as u64);
/// let mut file = MmapFileMut::open_cow_with_options("open_cow_with_options_test.txt", opts).unwrap();
/// assert!(file.is_cow());
///
/// let mut buf = vec![0; "some data...".len()];
/// file.read_exact(buf.as_mut_slice(), 0).unwrap();
/// assert_eq!(buf.as_slice(), "some data...".as_bytes());
///
/// // modify the file data
/// file.write_all("some data!!!".as_bytes(), 0).unwrap();
/// file.flush().unwrap();
///
/// // cow, change will only be seen in current caller
/// assert_eq!(file.as_slice(), "some data!!!".as_bytes());
/// drop(file);
///
/// // reopen to check content, cow will not change the content.
/// let mut file = File::open("open_cow_with_options_test.txt").unwrap();
/// let mut buf = vec![0; "some data...".len()];
/// // skip the sanity text
/// file.seek(SeekFrom::Start("sanity text".as_bytes().len() as u64)).unwrap();
/// file.read_exact(buf.as_mut_slice()).unwrap();
/// assert_eq!(buf.as_slice(), "some data...".as_bytes());
/// ```
///
/// [`Options`]: struct.Options.html
/// # Safety
///
/// See the [crate-level safety section](crate) for the full contract.
///
pub unsafe fn open_cow_with_options<P: AsRef<Path>>(path: P, opts: Options) -> Result<Self> {
Ok(Self::from(DiskMmapFileMut::open_cow_with_options(
path, opts,
)?))
}
/// Make the mmap file read-only.
///
/// # Notes
/// If `remove_on_drop` is set to `true`, then the underlying file will not be removed on drop if this function is invoked. [Read more]
///
/// # Examples
/// ```no_compile
/// use fmmap::{MmapFileMut, MmapFileMutExt};
/// # use scopeguard::defer;
///
/// let mut file = MmapFileMut::create("mmap_file_freeze_test.txt").unwrap();
/// # defer!(std::fs::remove_file("mmap_file_freeze_test.txt").unwrap());
/// file.truncate(12);
/// file.write_all("some data...".as_bytes(), 0).unwrap();
/// file.flush().unwrap();
///
/// file.freeze().unwrap();
/// ```
///
/// [Read more]: structs.MmapFileMut.html#methods.set_remove_on_drop
pub fn freeze(mut self) -> Result<MmapFile> {
let empty = MmapFileMutInner::Empty(EmptyMmapFile::default());
let inner = mem::replace(&mut self.inner, empty);
let path = inner.path_buf();
match inner {
MmapFileMutInner::Empty(empty) => Ok(MmapFile::from(empty)), // unreachable, keep this for good measure
MmapFileMutInner::Memory(memory) => Ok(MmapFile::from(memory.freeze())),
MmapFileMutInner::Disk(disk) => match disk.freeze() {
Ok(frozen) => Ok(MmapFile::from(frozen)),
Err(e) => {
// The disk is poisoned (or make_read_only failed). The wrapper
// is being consumed; preserve the path so `Drop`'s opt-in
// `remove_on_drop` cleanup can find the (possibly-mutated) file.
// Not an explicit-delete request, so use `pending_remove_path`.
if !path.as_os_str().is_empty() {
self.pending_remove_path = Some(path);
}
Err(e)
}
},
}
}
/// Transition the memory map to be readable and executable.
/// If the memory map is file-backed, the file must have been opened with execute permissions.
///
/// # Errors
/// This method returns an error when the underlying system call fails,
/// which can happen for a variety of reasons,
/// such as when the file has not been opened with execute permissions
///
/// # Examples
/// ```no_compile
/// use fmmap::{MmapFileExt, MmapFileMut, MmapFileMutExt};
/// # use scopeguard::defer;
///
/// let mut file = MmapFileMut::create("mmap_file_freeze_exec_test.txt").unwrap();
/// # defer!(std::fs::remove_file("mmap_file_freeze_exec_test.txt").unwrap());
/// file.truncate(12);
/// file.write_all("some data...".as_bytes(), 0).unwrap();
/// file.flush().unwrap();
///
/// let file = file.freeze_exec().unwrap();
/// assert!(file.is_exec());
/// ```
pub fn freeze_exec(mut self) -> Result<MmapFile> {
let empty = MmapFileMutInner::Empty(EmptyMmapFile::default());
let inner = mem::replace(&mut self.inner, empty);
let path = inner.path_buf();
match inner {
MmapFileMutInner::Empty(empty) => Ok(MmapFile::from(empty)), // unreachable, keep this for good measure
MmapFileMutInner::Memory(memory) => Ok(MmapFile::from(memory.freeze())),
MmapFileMutInner::Disk(disk) => match disk.freeze_exec() {
Ok(frozen) => Ok(MmapFile::from(frozen)),
Err(e) => {
if !path.as_os_str().is_empty() {
self.pending_remove_path = Some(path);
}
Err(e)
}
},
}
}
/// Returns whether remove the underlying file on drop.
#[inline]
pub fn get_remove_on_drop(&self) -> bool {
self.remove_on_drop
}
/// Whether to remove the underlying file on drop. Default is false.
///
/// # Notes
/// If invoke [`MmapFileMut::freeze`], then the file will
/// not be removed even though the field `remove_on_drop` is true.
///
/// # Path-reuse safety
///
/// `Drop` runs the same identity-checked, parent-bound unlink
/// sequence as the explicit `remove()` / `drop_remove()` paths
/// (POSIX: `fstatat` + `unlinkat` against a pre-opened parent dir
/// fd, with the original file's fd duped and held alive across the
/// probe + unlink so the `(dev, ino)` we compare to can't be
/// recycled). If the path no longer names the original inode — or
/// if it became a symlink — `Drop` leaves it alone. The residual
/// risks are the irreducible probe→unlink TOCTOU window (one
/// syscall) and Windows' lack of true openat-equivalents; see
/// `FileIdentity` for the full residual-race breakdown.
///
/// If you require synchronous error reporting, call
/// [`MmapFileMut::remove`] or [`MmapFileMut::drop_remove`]
/// explicitly before the wrapper is dropped — `Drop` swallows
/// errors because it cannot return a `Result`.
///
/// [`MmapFileMut::freeze`]: structs.MmapFileMut.html#methods.freeze
/// [`MmapFileMut::remove`]: structs.MmapFileMut.html#methods.remove
/// [`MmapFileMut::drop_remove`]: structs.MmapFileMut.html#methods.drop_remove
#[inline]
pub fn set_remove_on_drop(&mut self, val: bool) {
self.remove_on_drop = val;
}
/// Close the file. It would also truncate the file if max_sz >= 0.
///
/// On error the wrapper keeps its original `Disk` inner (now poisoned), so
/// the caller still has access to the path and can retry via `drop_remove`
/// / `remove` / `Drop`. `Empty` is only installed after every fallible step
/// succeeded.
#[inline]
pub fn close(&mut self, max_sz: i64) -> Result<()> {
// COW mappings are private — by contract they must not mutate the
// backing file. Refuse close-time truncation on COW; do it BEFORE
// touching the inner so the original mapping stays usable on error.
if max_sz >= 0 && self.is_cow() {
return Err(Error::new(
ErrorKind::Unsupported,
"cannot truncate a copy-on-write mmap file",
));
}
if max_sz >= 0 {
// Run the destructive work in-place on the disk inner so a transient
// flush/set_len/sync failure does NOT strand the wrapper with `Empty`
// and lose the path. On Err the disk inner is poisoned but still owns
// its `path` / `file`, so the caller can call `remove` / `drop_remove`.
if let MmapFileMutInner::Disk(disk) = &mut self.inner {
disk.close_with_truncate_in_place(max_sz as u64)?;
}
// Memory / Empty: nothing to do.
} else {
// No truncate requested — flush via the trait dispatcher; on Err the
// inner is unchanged.
self.flush()?;
}
// All fallible work succeeded. Now safe to drop the disk inner.
let empty = MmapFileMutInner::Empty(EmptyMmapFile::default());
drop(mem::replace(&mut self.inner, empty));
Ok(())
}
/// Remove the underlying file without dropping, leaving an `EmptyMmapFile`.
#[inline]
pub fn remove(&mut self) -> Result<()> {
// If a previous `remove()` call already dropped the inner mapping but
// the unlink itself failed, retry that pending unlink first. Otherwise
// a retry would short-circuit on the `_ => Ok(())` arm below (because
// the inner is already Empty) and report success while the file still
// exists.
if let Some(pending) = self.pending_drop_remove.take() {
return match retry_pending_delete(pending) {
Ok(()) => {
self.deleted = true;
Ok(())
}
Err((pending, e)) => {
self.pending_drop_remove = Some(pending);
Err(e)
}
};
}
let empty = MmapFileMutInner::Empty(EmptyMmapFile::default());
let inner = mem::replace(&mut self.inner, empty);
match inner {
MmapFileMutInner::Disk(disk) => {
let path = disk.path;
let identity = disk.file_identity;
drop(disk.mmap);
// Keep the inode pinned through probe+unlink on POSIX.
// Windows must drop first (see drop_remove sibling).
#[cfg(unix)]
let pin: std::fs::File = disk.file;
#[cfg(not(unix))]
drop(disk.file);
// Initial call: a missing file is the user's error. On other
// failures we record `PendingDelete::NeedsUnlink`; if `remove_file`
// itself succeeded but parent fsync didn't, we record
// `NeedsParentSync` so retry doesn't re-call `remove_file` on a
// possibly-reused path.
match initial_remove_durably(
&path,
identity,
#[cfg(unix)]
pin,
) {
Ok(()) => {
self.deleted = true;
Ok(())
}
Err((pending, e)) => {
// Deletion was the user's explicit intent — record it so a
// subsequent `remove()` retry AND `Drop` (regardless of
// `remove_on_drop`) can re-attempt the unlink instead of
// leaking the file.
self.pending_drop_remove = Some(pending);
Err(e)
}
}
}
_ => {
self.deleted = true;
Ok(())
}
}
}
}
impl_constructor_for_memory_mmap_file_mut!(MemoryMmapFileMut, MmapFileMut, "MmapFileMut", "sync");
impl_drop!(MmapFileMut, MmapFileMutInner, EmptyMmapFile);
impl_sync_tests!("", MmapFile, MmapFileMut);
/// Extract the inode pin from a sync `std::fs::File` owned at Drop
/// time. Sync owns the file directly — just move it in. No fd
/// allocation, no EMFILE failure mode. Called by `impl_drop!`'s
/// `remove_on_drop` path; the macro's name resolution finds this
/// per-impl-file (sync here, async in `mmap_file/{tokio,smol}_impl.rs`).
#[cfg(unix)]
fn sync_drop_pin(file: std::fs::File) -> Option<std::fs::File> {
Some(file)
}
#[cfg(not(unix))]
fn sync_drop_pin(file: std::fs::File) -> Option<std::fs::File> {
drop(file);
None
}
#[cfg(test)]
mod regression {
use super::*;
use crate::Options;
use scopeguard::defer;
use std::io::Write;
/// Test helper: dup a `File`'s descriptor via `F_DUPFD_CLOEXEC` and
/// wrap the new descriptor as an owned `File`. Mirrors the production
/// dup that wrappers do before calling `initial_remove_durably`. Used
/// to populate `PendingDelete::NeedsUnlink::pin` in tests that
/// construct the pending state directly.
#[cfg(unix)]
fn dup_for_pin(file: &std::fs::File) -> std::fs::File {
use std::os::fd::{AsRawFd, BorrowedFd};
let raw = file.as_raw_fd();
// SAFETY: `file` is a live, owned File; the borrow lives for the
// call and `fcntl_dupfd_cloexec` returns a fresh OwnedFd.
let borrowed = unsafe { BorrowedFd::borrow_raw(raw) };
let owned = rustix::io::fcntl_dupfd_cloexec(borrowed, 0).expect("fcntl dup");
std::fs::File::from(owned)
}
/// Finding #1: even though the public `lock_shared` is `unsafe`, calling it
/// on a writable mapping does in fact downgrade the auto-acquired exclusive
/// lock and thus allow another reader. This is documented as UB-on-the-caller;
/// the test only confirms the unsafe contract is correctly described.
#[test]
fn auto_lock_blocks_aliased_writer_until_drop() {
let path = "_regression_auto_lock_blocks.txt";
defer!(let _ = std::fs::remove_file(path););
let _ = std::fs::remove_file(path);
let writer = unsafe { MmapFileMut::create(path) }.unwrap();
// Second writer attempt on the same path must fail (exclusive lock held).
assert!(unsafe { MmapFileMut::open(path) }.is_err());
// Reader attempt must also fail (would conflict with exclusive).
assert!(unsafe { MmapFile::open(path) }.is_err());
drop(writer);
// After the writer drops, both reader and writer can be opened (separately).
let r = unsafe { MmapFile::open(path) }.unwrap();
drop(r);
let _ = unsafe { MmapFileMut::open(path) }.unwrap();
}
/// Finding #2: opening with `Options::truncate(true)` on a path whose lock
/// is already held by another mapping must NOT destroy the existing file
/// content. Truncation is now applied only after the auto-lock is acquired.
#[test]
fn lock_contended_open_with_truncate_preserves_content() {
let path = crate::tests::get_random_filename();
defer!(let _ = std::fs::remove_file(&path););
// Pre-populate the file with content.
{
let mut f = std::fs::File::create(&path).unwrap();
f.write_all(b"keep me").unwrap();
f.sync_all().unwrap();
}
// First handle holds the exclusive lock.
let holder = unsafe { MmapFileMut::open(&path) }.unwrap();
// Second open with truncate(true) must fail at the lock step, NOT have
// already truncated the file.
let opts = Options::new().read(true).write(true).truncate(true);
let err = match unsafe { MmapFileMut::open_with_options(&path, opts) } {
Err(e) => e,
Ok(_) => panic!("expected lock contention to fail the open"),
};
assert_eq!(err.kind(), std::io::ErrorKind::WouldBlock);
// Drop the holder before reading: on Windows, an exclusive `LockFileEx`
// hold blocks all access — even a plain `std::fs::read` from the same
// process — so we must release first to verify content.
drop(holder);
let bytes = std::fs::read(&path).unwrap();
assert_eq!(bytes, b"keep me");
}
/// A `truncate` failure that happens BEFORE the disk backend swaps
/// in the anonymous placeholder (e.g. the cow-unsupported check)
/// must NOT detach the live mapping. The original mapping should
/// stay usable so the caller can flush/read it.
#[test]
fn pre_swap_truncate_failure_preserves_mapping() {
let path = "_regression_pre_swap_truncate_preserves.txt";
defer!(let _ = std::fs::remove_file(path););
let _ = std::fs::remove_file(path);
// Pre-populate with content.
{
let mut f = std::fs::File::create(path).unwrap();
f.write_all(b"original").unwrap();
f.sync_all().unwrap();
}
// Open in COW mode; truncate is unsupported on COW and returns Err
// before touching the mapping.
let mut cow = unsafe { MmapFileMut::open_cow(path) }.unwrap();
let err = match cow.truncate(0) {
Err(e) => e,
Ok(()) => panic!("expected COW truncate to fail"),
};
assert_eq!(err.kind(), std::io::ErrorKind::Unsupported);
// The COW mapping must still be live: original bytes still readable.
assert_eq!(cow.as_slice(), b"original");
// Writes through the COW mapping still succeed (visible to this
// handle only).
cow.write_all(b"modified", 0).unwrap();
assert_eq!(&cow.as_slice()[..b"modified".len()], b"modified");
}
/// Truncate clamp: opening with a large `len` and then truncating
/// to a smaller size must NOT leave a mapping that extends past
/// EOF. The crate clamps the stored `len` to `(new_size - offset)`
/// on remap.
#[test]
fn truncate_clamps_oversized_len() {
let path = "_regression_truncate_clamps_len.txt";
defer!(let _ = std::fs::remove_file(path););
let _ = std::fs::remove_file(path);
// Pre-populate 8KiB.
{
let f = std::fs::File::create(path).unwrap();
f.set_len(8192).unwrap();
f.sync_all().unwrap();
}
// Open with explicit len = 8192.
let opts = Options::new().read(true).write(true).len(8192);
let mut file = unsafe { MmapFileMut::open_with_options(path, opts) }.unwrap();
assert_eq!(file.len(), 8192);
// Truncate to 1KiB. The new mapping must be 1024, not 8192.
file.truncate(1024).unwrap();
assert_eq!(file.len(), 1024);
// Writes within the new bounds succeed.
file.write_all(&[0xab; 1024], 0).unwrap();
file.flush().unwrap();
}
/// Offset past EOF: truncating to below the mapping's offset must
/// fail with InvalidInput rather than producing a broken mapping.
/// The object remains usable (not poisoned) since we check before
/// touching the mapping.
#[test]
fn truncate_below_offset_errors() {
let path = "_regression_truncate_below_offset.txt";
defer!(let _ = std::fs::remove_file(path););
let _ = std::fs::remove_file(path);
{
let f = std::fs::File::create(path).unwrap();
f.set_len(2048).unwrap();
f.sync_all().unwrap();
}
let opts = Options::new().read(true).write(true).offset(1024);
let mut file = unsafe { MmapFileMut::open_with_options(path, opts) }.unwrap();
// truncate(500) would leave offset (1024) past the new EOF.
let err = match file.truncate(500) {
Err(e) => e,
Ok(()) => panic!("expected InvalidInput"),
};
assert_eq!(err.kind(), std::io::ErrorKind::InvalidInput);
// The object is still usable: the mapping was never replaced because the
// check fired before the placeholder swap.
file.flush().unwrap();
}
/// `create_with_options` used to drop the user-set
/// `Options::mode` / `custom_flags` because `create_in` opened with
/// the hard-coded `create_file()` helper instead of routing through
/// `opts.file_opts`. Verify on Unix that a custom mode is honored.
#[cfg(unix)]
#[test]
fn create_with_options_honors_unix_mode() {
use crate::Options;
use std::os::unix::fs::PermissionsExt;
let path = crate::tests::get_random_filename();
defer!(let _ = std::fs::remove_file(&path););
let opts = Options::new().mode(0o600).max_size(8);
let f = unsafe { MmapFileMut::create_with_options(&path, opts) }.unwrap();
drop(f);
let mode = std::fs::metadata(&path).unwrap().permissions().mode() & 0o777;
assert_eq!(mode, 0o600);
}
/// `remove()` swaps the inner to Empty before calling `remove_file`.
/// If the unlink fails, the original `MmapFileMut` is left with an
/// Empty inner whose path is `""`, so a subsequent `Drop` can no
/// longer attempt the unlink and the file leaks. Verify the
/// wrapper retains the original path on failure (in
/// `pending_drop_remove` because deletion was explicitly requested)
/// so Drop has a chance to retry regardless of `remove_on_drop`.
///
/// Unix-only: setup uses `std::fs::remove_file` which would fail
/// against a still-open handle on Windows.
#[cfg(unix)]
#[test]
fn remove_failure_retains_path_for_drop_retry() {
let path = crate::tests::get_random_filename();
let mut f = unsafe { MmapFileMut::create(&path) }.unwrap();
f.truncate(8).unwrap();
// Force the unlink to fail by pre-removing the file: the second
// remove will fail with NotFound, so pending_drop_remove should be
// populated.
drop(std::fs::remove_file(&path));
let err = f.remove().unwrap_err();
assert_eq!(err.kind(), std::io::ErrorKind::NotFound);
let pending = f.pending_drop_remove.as_ref().expect("pending state set");
assert_eq!(pending.path(), path.as_path());
// NotFound always routes to NeedsUnlink. An nlink-based
// NeedsParentSync routing would assume the entry was unlinked
// from OUR parent, but a rename + unlink-elsewhere also produces
// nlink==0 — fsyncing our parent then doesn't make the other
// directory's unlink crash-durable. Surfacing NeedsUnlink is
// conservative: caller knows we did not confirm crash-durable
// deletion.
assert!(matches!(
pending,
crate::mmap_file::PendingDelete::NeedsUnlink { .. }
));
}
/// A subsequent `remove()` after a failed one used to
/// short-circuit on the `_ => Ok(())` arm because the inner was
/// Empty, reporting a successful cleanup while the original file
/// still existed. Verify the retry actually attempts cleanup
/// against the saved `pending_drop_remove`.
///
/// NotFound at probe time stays NeedsUnlink. The retry re-probes;
/// against a path-reused (recreated) file the identity check fails
/// and we surface a path-reuse error. The recreated file is
/// preserved.
///
/// Unix-only: see sibling test for rationale.
#[cfg(unix)]
#[test]
fn remove_retry_attempts_pending_path() {
let path = crate::tests::get_random_filename();
defer!(let _ = std::fs::remove_file(&path););
let mut f = unsafe { MmapFileMut::create(&path) }.unwrap();
f.truncate(8).unwrap();
// First call: pre-delete the file so remove() fails with NotFound.
// Pending state is NeedsUnlink — we never confirmed the unlink
// happened in OUR parent, so we can't claim crash-durable
// deletion via parent fsync alone.
drop(std::fs::remove_file(&path));
assert!(f.remove().is_err());
assert!(matches!(
f.pending_drop_remove,
Some(crate::mmap_file::PendingDelete::NeedsUnlink { .. })
));
// Re-create a different file at the same path. The retry's
// identity probe sees a different inode and surfaces a
// path-reuse error; pending state stays NeedsUnlink. The
// recreated file is preserved (we don't touch it).
{
let mut g = std::fs::File::create(&path).unwrap();
use std::io::Write as _;
g.write_all(b"different file").unwrap();
}
let err = f
.remove()
.expect_err("retry against path-reused file must NOT succeed");
assert!(
err.to_string().contains("path-reuse detected") || err.kind() == std::io::ErrorKind::NotFound,
"expected path-reuse or NotFound, got: {err}"
);
assert!(path.exists(), "recreated file must remain");
assert_eq!(std::fs::read(&path).unwrap(), b"different file");
assert!(!f.deleted, "deleted must NOT be set on path-reuse retry");
}
/// When an explicit deletion (via `remove(&mut self)` or consuming
/// `drop_remove(self)`) fails, an older implementation stored the
/// retained path in `pending_remove_path`, which `Drop` only honors
/// under `remove_on_drop=true` — and a caller asking for delete
/// usually does NOT set that flag. Result: transient unlink
/// failures silently leaked the file. Verify the
/// `pending_drop_remove` channel triggers Drop's retry regardless
/// of `remove_on_drop`.
#[test]
fn explicit_remove_failure_drop_retries_unconditionally() {
let path = crate::tests::get_random_filename();
{
let mut f = unsafe { MmapFileMut::create(&path) }.unwrap();
// Deliberately do NOT set remove_on_drop.
f.truncate(8).unwrap();
// Force remove() to fail with NotFound by pre-deleting the path.
drop(std::fs::remove_file(&path));
let err = f.remove().unwrap_err();
assert_eq!(err.kind(), std::io::ErrorKind::NotFound);
// Recreate the file. The pending state is `NeedsParentSync` (NotFound
// on initial → presumed already-unlinked), so Drop's retry only
// fsyncs the parent and MUST NOT delete the recreated file (which
// is unrelated to the original mapping — path-reuse safety).
std::fs::File::create(&path).unwrap();
}
assert!(
path.exists(),
"Drop's path-reuse-safe retry must NOT delete a path-reused file",
);
let _ = std::fs::remove_file(&path);
}
/// The public `lock()` and `lock_shared()` methods used to call
/// `fs4::FileExt::lock` / `lock_shared` blindly. POSIX `flock` is
/// idempotent on the same handle, but Windows `LockFileEx` waits
/// indefinitely for the same handle to release — deadlock. Verify
/// that calling `lock()` / `lock_shared()` / `try_lock()` /
/// `try_lock_shared()` on an auto-locked wrapper is reentrant-safe
/// (no-op when state matches, `WouldBlock` when state mismatches).
#[test]
fn reentrant_lock_methods_do_not_deadlock_on_auto_lock() {
let path = crate::tests::get_random_filename();
defer!(let _ = std::fs::remove_file(&path););
// Mut wrapper: auto-acquired exclusive lock.
let mut f = unsafe { MmapFileMut::create(&path) }.unwrap();
f.lock()
.expect("lock() on already-exclusive must be Ok no-op");
f.try_lock()
.expect("try_lock() on already-exclusive must be Ok no-op");
let err = unsafe { f.lock_shared() }.unwrap_err();
assert_eq!(err.kind(), std::io::ErrorKind::WouldBlock);
let err = unsafe { f.try_lock_shared() }.unwrap_err();
assert_eq!(err.kind(), std::io::ErrorKind::WouldBlock);
drop(f);
// Read-only wrapper: auto-acquired shared lock.
let mut f = unsafe { MmapFile::open(&path) }.unwrap();
unsafe { f.lock_shared() }.expect("lock_shared() on already-shared must be Ok no-op");
unsafe { f.try_lock_shared() }.expect("try_lock_shared() on already-shared must be Ok no-op");
let err = f.lock().unwrap_err();
assert_eq!(err.kind(), std::io::ErrorKind::WouldBlock);
let err = f.try_lock().unwrap_err();
assert_eq!(err.kind(), std::io::ErrorKind::WouldBlock);
// After explicit unlock, lock() succeeds.
unsafe { f.unlock() }.unwrap();
f.lock().unwrap();
unsafe { f.unlock() }.unwrap();
}
/// Durable unlink retry must be idempotent — after the first
/// attempt unlinks the inode but fails `sync_dir`, the retry's
/// parent fsync should still complete so the unlink isn't lost on
/// crash. Verify `NeedsParentSync` retries fsync-only and reports
/// success.
///
/// We directly construct `NeedsParentSync` to exercise the
/// post-unlink path. (A test that pre-deletes the path and relies
/// on initial-call NotFound to produce `NeedsParentSync` does not
/// work, because pre-unlink NotFound is correctly routed to
/// `NeedsUnlink` — a renamed-away file isn't deleted, so that
/// setup wouldn't reach the NeedsParentSync code path.)
#[test]
fn pending_drop_remove_retry_tolerates_already_unlinked() {
let path = crate::tests::get_random_filename();
defer!(let _ = std::fs::remove_file(&path););
let mut f = unsafe { MmapFileMut::create(&path) }.unwrap();
f.truncate(8).unwrap();
drop(std::fs::remove_file(&path));
// Pre-open a parent handle to construct NeedsParentSync directly,
// mirroring the post-unlink-pre-sync state initial_remove_durably
// would have reached if `unlinkat` succeeded but
// `sync_parent_handle` failed. The path is gone (we pre-deleted it
// above) — the retry must still complete via parent fsync alone
// and clear the pending state.
let parent_handle = crate::utils::open_parent_for_sync(&path)
.expect("open parent for NeedsParentSync test fixture");
f.pending_drop_remove = Some(crate::mmap_file::PendingDelete::NeedsParentSync {
path: path.clone(),
parent_handle,
});
f.remove().unwrap();
assert!(f.pending_drop_remove.is_none());
assert!(f.deleted);
}
/// Pre-unlink NotFound after a *rename* (not an unlink) must keep
/// `NeedsUnlink` and never mark `deleted = true`. The pin's inode
/// is alive at the rename destination (`nlink > 0`), so the nlink
/// classifier correctly stays in NeedsUnlink. A `NeedsParentSync`
/// route would let the next retry trivially fsync and report
/// success while the original file is still alive at a new name.
#[cfg(unix)]
#[test]
fn pre_unlink_notfound_after_rename_keeps_needs_unlink() {
let path = crate::tests::get_random_filename();
let mut renamed = crate::tests::get_random_filename();
renamed.set_extension("renamed");
defer!(let _ = std::fs::remove_file(&path););
defer!(let _ = std::fs::remove_file(&renamed););
let mut f = unsafe { MmapFileMut::create(&path) }.unwrap();
f.truncate(8).unwrap();
// Rename the file away — directory entry at `path` is gone, but
// the inode lives on at `renamed` (nlink stays at 1).
std::fs::rename(&path, &renamed).unwrap();
assert!(!path.exists());
assert!(renamed.exists());
// First remove() — pre-unlink probe at `path` is NotFound.
// fstat on the pin sees nlink == 1 (alive at `renamed`), so we
// route to NeedsUnlink (the rename-then-NotFound case).
let err = f.remove().expect_err("path missing → NotFound");
assert_eq!(err.kind(), std::io::ErrorKind::NotFound);
assert!(matches!(
f.pending_drop_remove,
Some(crate::mmap_file::PendingDelete::NeedsUnlink { .. })
));
assert!(!f.deleted, "deleted must NOT be set on initial NotFound");
// Retry — still missing at `path`, still alive at `renamed`,
// still NeedsUnlink. No false-success.
let err = f
.remove()
.expect_err("retry against renamed-away file stays NotFound");
assert_eq!(err.kind(), std::io::ErrorKind::NotFound);
assert!(matches!(
f.pending_drop_remove,
Some(crate::mmap_file::PendingDelete::NeedsUnlink { .. })
));
assert!(
!f.deleted,
"false-success on retry: the renamed-away file is still alive, deletion did not happen"
);
// The renamed-away file is still alive — we never touched it.
assert!(renamed.exists());
}
/// Cross-directory rename + external unlink: the documented residual
/// race that the README's "External rename + unlink elsewhere"
/// section covers. A concurrent actor renames our file into a
/// different directory, then unlinks it there. The pin's `nlink`
/// drops to 0 — but the actual unlink happened in a parent we don't
/// hold a handle to, so fsyncing OUR parent doesn't make their
/// unlink crash-durable. fmmap must surface this as `NotFound`
/// (i.e., `NeedsUnlink`) rather than claiming durable success — even
/// though the file is genuinely gone from the namespace, we can't
/// prove crash-durability from our side.
#[cfg(unix)]
#[test]
fn external_rename_to_other_dir_then_unlink_surfaces_notfound() {
let dir_a = std::env::temp_dir().join(format!(
"fmmap-test-dir-a-{}-{}",
std::process::id(),
std::time::SystemTime::now()
.duration_since(std::time::UNIX_EPOCH)
.map(|d| d.as_nanos())
.unwrap_or(0)
));
let dir_b = std::env::temp_dir().join(format!(
"fmmap-test-dir-b-{}-{}",
std::process::id(),
std::time::SystemTime::now()
.duration_since(std::time::UNIX_EPOCH)
.map(|d| d.as_nanos())
.unwrap_or(0)
));
std::fs::create_dir_all(&dir_a).unwrap();
std::fs::create_dir_all(&dir_b).unwrap();
defer!(let _ = std::fs::remove_dir_all(&dir_a););
defer!(let _ = std::fs::remove_dir_all(&dir_b););
let path_a = dir_a.join("original.txt");
let path_b = dir_b.join("moved.txt");
let mut f = unsafe { MmapFileMut::create(&path_a) }.unwrap();
f.truncate(8).unwrap();
// Simulate external actor: rename to a different directory, then
// unlink there. fmmap's pin keeps the inode alive in memory but
// nlink drops to 0; our parent_handle still points at dir_a, where
// the entry hasn't existed since the rename.
std::fs::rename(&path_a, &path_b).unwrap();
std::fs::remove_file(&path_b).unwrap();
assert!(!path_a.exists());
assert!(!path_b.exists());
// First remove() — probe at path_a is NotFound. fmmap stays in
// NeedsUnlink and surfaces NotFound; we MUST NOT claim durable
// deletion (round-30 attempted that via nlink==0; round-36
// reverted because the actual unlink was in dir_b's journal,
// not ours).
let err = f.remove().expect_err("path missing → NotFound");
assert_eq!(err.kind(), std::io::ErrorKind::NotFound);
assert!(matches!(
f.pending_drop_remove,
Some(crate::mmap_file::PendingDelete::NeedsUnlink { .. })
));
assert!(
!f.deleted,
"deleted must NOT be set: we cannot crash-durably commit dir_b's unlink"
);
// Retry — same surface area, same conservative answer.
let err = f
.remove()
.expect_err("retry against externally-deleted-elsewhere file stays NotFound");
assert_eq!(err.kind(), std::io::ErrorKind::NotFound);
assert!(matches!(
f.pending_drop_remove,
Some(crate::mmap_file::PendingDelete::NeedsUnlink { .. })
));
assert!(!f.deleted);
}
/// `Drop` used to call `remove_file` on `inner.path_buf()` whenever
/// `remove_on_drop=true`, regardless of inner variant. Memory
/// variants store the user-supplied string as a label, so
/// `MmapFileMut::memory_from_vec("real_file", ...)` followed by
/// `set_remove_on_drop(true)` would delete `real_file` on Drop
/// even though no on-disk mapping owns it. Verify Drop now no-ops
/// on Memory variants (matching the explicit `remove()` method's
/// behavior).
#[test]
fn drop_on_memory_variant_does_not_unlink_label_path() {
let real_file_path = crate::tests::get_random_filename();
{
let mut g = std::fs::File::create(&real_file_path).unwrap();
use std::io::Write as _;
g.write_all(b"do not delete me").unwrap();
g.sync_all().unwrap();
}
{
let mut f = MmapFileMut::memory_from_vec(&real_file_path, vec![1, 2, 3]);
f.set_remove_on_drop(true);
// f drops here.
}
assert!(
real_file_path.exists(),
"Drop on a memory variant must not unlink a real file matching its label"
);
assert_eq!(std::fs::read(&real_file_path).unwrap(), b"do not delete me");
let _ = std::fs::remove_file(&real_file_path);
}
/// `freeze`/`freeze_exec` on `DiskMmapFileMut` must check the
/// `poisoned` flag — otherwise a failed truncate could be turned
/// into a successful read-only `MmapFile` whose `as_slice()`
/// returns the anonymous placeholder bytes while `path()`/
/// `metadata()` refer to the real file — silently corrupt views.
/// Verify `freeze` and `freeze_exec` reject a poisoned mapping.
#[test]
fn freeze_rejects_poisoned_mapping() {
use crate::raw::DiskMmapFileMut;
let path = crate::tests::get_random_filename();
defer!(let _ = std::fs::remove_file(&path););
let opts = Options::new().read(true).write(true).max_size(64);
let mut raw = unsafe { DiskMmapFileMut::create_with_options(&path, opts) }.unwrap();
raw.force_poison_for_test();
assert!(raw.is_poisoned());
let err = raw.freeze().err().expect("freeze on poisoned should fail");
assert!(
err.to_string().contains("poisoned"),
"expected poison error, got: {err}"
);
}
#[test]
fn freeze_exec_rejects_poisoned_mapping() {
use crate::raw::DiskMmapFileMut;
let path = crate::tests::get_random_filename();
defer!(let _ = std::fs::remove_file(&path););
let opts = Options::new().read(true).write(true).max_size(64);
let mut raw = unsafe { DiskMmapFileMut::create_with_options(&path, opts) }.unwrap();
raw.force_poison_for_test();
let err = raw
.freeze_exec()
.err()
.expect("freeze_exec on poisoned should fail");
assert!(
err.to_string().contains("poisoned"),
"expected poison error, got: {err}"
);
}
/// Consuming `drop_remove(self)` used to swap inner to Empty BEFORE
/// running fallible disk I/O. On Err the wrapper was consumed and
/// `Drop`'s `remove_on_drop` cleanup silently no-op'd against the
/// Empty inner's `""` path. Verify `drop_remove` propagates the
/// Err (the `pending_remove_path` retention is verified by the
/// inherent-`remove` regression above; the consuming variant uses
/// the same retain-on-Err shape).
#[test]
fn drop_remove_consuming_propagates_failure() {
let path = crate::tests::get_random_filename();
let mut f = unsafe { MmapFileMut::create(&path) }.unwrap();
f.truncate(8).unwrap();
drop(std::fs::remove_file(&path));
let err = f.drop_remove().unwrap_err();
assert_eq!(err.kind(), std::io::ErrorKind::NotFound);
}
/// inner to `Empty` before doing the fallible `flush`/`set_len`/`sync`
/// work, so a transient I/O failure stranded the wrapper without a path
/// or handle to retry/inspect. The fix runs the destructive work
/// in-place on the disk inner (poisoning it on Err) and only swaps to
/// Empty after success. Verify a successful close zeroes the file and
/// installs Empty, and a no-truncate close (max_sz<0) is the same.
#[test]
fn close_with_truncate_in_place_succeeds() {
let path = crate::tests::get_random_filename();
defer!(let _ = std::fs::remove_file(&path););
let mut f = unsafe { MmapFileMut::create(&path) }.unwrap();
f.truncate(8).unwrap();
f.write_all(b"abcdefgh", 0).unwrap();
f.flush().unwrap();
f.close(4).unwrap();
// After close: inner is Empty; methods route through the Empty arm.
assert_eq!(MmapFileExt::len(&f), 0);
assert_eq!(MmapFileExt::as_slice(&f), &[] as &[u8]);
// Backing file is truncated to 4 bytes.
let bytes = std::fs::read(&path).unwrap();
assert_eq!(bytes, b"abcd");
}
/// A copy-on-write mapping must not mutate the backing file. Both
/// `close(max_sz)` (inherent) and `close_with_truncate` (trait,
/// dispatching to disk) used to call `set_len` on the backing file
/// regardless of mapping type. Verify both paths refuse with
/// Unsupported when `max_sz >= 0` AND the underlying file is
/// preserved.
#[test]
fn cow_close_does_not_truncate_backing_file() {
let path = crate::tests::get_random_filename();
defer!(let _ = std::fs::remove_file(&path););
{
let mut f = std::fs::File::create(&path).unwrap();
f.write_all(b"keep me intact").unwrap();
f.sync_all().unwrap();
}
// inherent `MmapFileMut::close(max_sz >= 0)` on a COW mapping → Unsupported
{
let mut cow = unsafe { MmapFileMut::open_cow(&path) }.unwrap();
let err = cow.close(0).unwrap_err();
assert_eq!(err.kind(), std::io::ErrorKind::Unsupported);
}
assert_eq!(std::fs::read(&path).unwrap(), b"keep me intact");
// close(-1) is fine (no truncation) on COW
{
let mut cow = unsafe { MmapFileMut::open_cow(&path) }.unwrap();
cow.close(-1).unwrap();
}
assert_eq!(std::fs::read(&path).unwrap(), b"keep me intact");
// trait `close_with_truncate(max_sz >= 0)` on a COW mapping → Unsupported
{
let cow = unsafe { MmapFileMut::open_cow(&path) }.unwrap();
let err = cow.close_with_truncate(0).unwrap_err();
assert_eq!(err.kind(), std::io::ErrorKind::Unsupported);
}
assert_eq!(std::fs::read(&path).unwrap(), b"keep me intact");
}
/// `Options::len` was previously passed straight to memmapix
/// without bounds-checking against the backing file. memmapix
/// accepts the configured length unconditionally, so a 4096-byte
/// mapping over a 1-byte file produces a mapping whose pages past
/// EOF SIGBUS on access — turning a safe-API entry point into an
/// unannounced UB trap. Verify each constructor rejects an
/// `offset+len` window that exceeds the file before invoking
/// memmapix.
#[test]
fn map_range_validation_rejects_oversized_window() {
use crate::Options;
let path = crate::tests::get_random_filename();
defer!(let _ = std::fs::remove_file(&path););
{
let mut f = std::fs::File::create(&path).unwrap();
f.write_all(b"abcd").unwrap(); // 4 bytes
}
let assert_invalid_input = |result: Result<()>| {
let err = result.expect_err("expected InvalidInput rejection");
assert_eq!(err.kind(), std::io::ErrorKind::InvalidInput);
};
// Mut create_with_options: file is brand new (0 bytes), no max_size,
// but len=128. Constructor must reject before mmap.
let create_path = crate::tests::get_random_filename();
defer!(let _ = std::fs::remove_file(&create_path););
let opts = Options::new().len(128);
assert_invalid_input(
unsafe { MmapFileMut::create_with_options(&create_path, opts) }.map(|_| ()),
);
let _ = std::fs::remove_file(&create_path);
// Mut open_with_options: existing 4-byte file, len=128.
let opts = Options::new().len(128);
assert_invalid_input(unsafe { MmapFileMut::open_with_options(&path, opts) }.map(|_| ()));
// Mut open_exist_with_options: same.
let opts = Options::new().len(128);
assert_invalid_input(unsafe { MmapFileMut::open_exist_with_options(&path, opts) }.map(|_| ()));
// COW open_cow_with_options: same.
let opts = Options::new().len(128);
assert_invalid_input(unsafe { MmapFileMut::open_cow_with_options(&path, opts) }.map(|_| ()));
// Read-only open_with_options: same.
let opts = Options::new().len(128);
assert_invalid_input(unsafe { MmapFile::open_with_options(&path, opts) }.map(|_| ()));
// offset past EOF (no len) is also rejected.
let opts = Options::new().offset(64);
assert_invalid_input(unsafe { MmapFile::open_with_options(&path, opts) }.map(|_| ()));
// In-bounds window is accepted: len=2 at offset=1 fits in 4 bytes.
let opts = Options::new().offset(1).len(2);
let f = unsafe { MmapFile::open_with_options(&path, opts) }.unwrap();
assert_eq!(f.as_slice(), b"bc");
}
/// The raw `DiskMmapFileMut::drop_remove` is consuming and used to
/// return parent-fsync errors in a generic shape, so a caller
/// couldn't tell unlink-failed from
/// unlink-succeeded-but-parent-sync-failed and would be tempted to
/// retry `remove_file` on a path that may have been reused. Verify
/// the post-unlink failure is reported with a message that names
/// the parent dir and warns against re-calling `remove_file`.
///
/// Triggering an actual `sync_dir` failure is intrusive (mocking fsync),
/// so this test only covers the success path of the message-tagging code:
/// a successful drop_remove must not synthesize the warning. The error-
/// path message construction is covered by code review and the matching
/// async test below.
#[test]
fn raw_drop_remove_success_path_no_spurious_warning() {
use crate::raw::DiskMmapFileMut;
let path = crate::tests::get_random_filename();
defer!(let _ = std::fs::remove_file(&path););
let mut f = unsafe { DiskMmapFileMut::create(&path) }.unwrap();
f.truncate(8).unwrap();
f.flush().unwrap();
f.drop_remove()
.expect("drop_remove on a normal path must succeed");
assert!(!path.exists(), "file should be unlinked");
}
/// `open_with_options` used to apply `truncate(true)` and
/// `max_size` extension *before* validating the mapping range. An
/// invalid `offset/len` combined with `truncate(true)` would zero
/// an existing file and only then return Err — silent data loss.
/// Verify the file content is preserved when validation rejects.
#[test]
fn invalid_options_with_truncate_preserve_existing_file() {
use crate::Options;
let path = crate::tests::get_random_filename();
defer!(let _ = std::fs::remove_file(&path););
{
let mut f = std::fs::File::create(&path).unwrap();
f.write_all(b"PRESERVE_ME").unwrap();
f.sync_all().unwrap();
}
let original = std::fs::read(&path).unwrap();
assert_eq!(original, b"PRESERVE_ME");
// truncate(true) + offset past EOF (after planned truncate to 0).
// Validation must reject before set_len(0) destroys the bytes.
let opts = Options::new().truncate(true).offset(1).len(2);
let result =
unsafe { MmapFileMut::open_with_options(&path, opts) }.map(|_| "should have rejected");
let err = result.expect_err("invalid offset/len must reject");
assert_eq!(err.kind(), std::io::ErrorKind::InvalidInput);
assert_eq!(
std::fs::read(&path).unwrap(),
b"PRESERVE_ME",
"file content must be intact after validation rejection"
);
// truncate(true) + max_size with len > max_size also rejects pre-truncate.
let opts = Options::new().truncate(true).max_size(4).len(64);
let result =
unsafe { MmapFileMut::open_with_options(&path, opts) }.map(|_| "should have rejected");
let err = result.expect_err("invalid offset/len must reject");
assert_eq!(err.kind(), std::io::ErrorKind::InvalidInput);
assert_eq!(
std::fs::read(&path).unwrap(),
b"PRESERVE_ME",
"file content must be intact after validation rejection"
);
}
/// `pending_remove_path` (set when `close_with_truncate` consumed
/// the inner) does NOT carry identity — by that point the `File`
/// was already gone — so its Drop path is the path-reuse-safe
/// "fsync parent only" behavior. Verify a wrapper whose only Drop
/// signal is `pending_remove_path` does not unlink.
#[test]
fn pending_remove_path_drop_does_not_unlink() {
let path = crate::tests::get_random_filename();
defer!(let _ = std::fs::remove_file(&path););
std::fs::write(&path, b"keep").unwrap();
{
let mut f = MmapFileMut::memory_from_vec("dummy.mem", vec![1u8]);
f.pending_remove_path = Some(path.clone());
f.remove_on_drop = true;
}
assert!(
path.exists(),
"pending_remove_path Drop path is identity-less and must not unlink",
);
assert_eq!(std::fs::read(&path).unwrap(), b"keep");
}
/// `drop_complete_pending_delete` used to retry `remove_file` from
/// `Drop` when state was `NeedsUnlink`, which races path reuse —
/// between the initial failure and Drop, another actor could swap
/// the path and our retry would delete an unrelated file. Verify
/// Drop no longer unlinks by path alone.
///
/// Construction: induce a `NeedsUnlink` pending state by
/// pre-deleting the file to make the explicit `drop_remove()` fail
/// with a typed non-NotFound error. (We can't easily provoke a
/// non-NotFound failure in tests without privilege escalation;
/// instead we directly install the pending state and observe Drop
/// behavior.)
///
/// Explicit `remove()` retry of a `NeedsUnlink` pending state must
/// verify file identity before unlinking. If the path was reused
/// between failure and retry, retry must NOT delete the unrelated
/// occupant. POSIX uses dev+ino; Windows uses
/// `GetFileInformationByHandle`'s volume serial + file index.
#[test]
fn explicit_retry_with_identity_check_refuses_path_reused_file() {
let probe_path = crate::tests::get_random_filename();
defer!(let _ = std::fs::remove_file(&probe_path););
// Capture identity from the *original* file, then simulate path
// reuse. We must keep `original` open while we recreate the file:
// on tmpfs (Linux CI) inode numbers are recycled aggressively, so
// a `remove_file` + `write` sequence often hands back the same
// (dev, ino), defeating the path-reuse simulation. Holding the
// original handle pins its inode until end-of-test.
let original = std::fs::File::create(&probe_path).unwrap();
use std::io::Write;
let mut original = original;
original.write_all(b"original").unwrap();
original.sync_all().unwrap();
let original_identity = crate::utils::FileIdentity::from_file(&original).unwrap();
// Unlink the directory entry but keep the inode pinned via `original`.
std::fs::remove_file(&probe_path).unwrap();
// Plant a *different* file at the same path. With `original` still
// open, this gets a fresh inode.
std::fs::write(&probe_path, b"unrelated content").unwrap();
let mut f = MmapFileMut::memory_from_vec("dummy.mem", vec![1u8]);
f.pending_drop_remove = Some(crate::mmap_file::PendingDelete::NeedsUnlink {
path: probe_path.clone(),
identity: original_identity,
#[cfg(unix)]
pin: dup_for_pin(&original),
});
let err = f.remove().unwrap_err();
assert!(
err.to_string().contains("path-reuse detected"),
"expected path-reuse error, got: {err}",
);
assert!(
probe_path.exists(),
"retry must NOT have unlinked the path-reused file",
);
assert_eq!(std::fs::read(&probe_path).unwrap(), b"unrelated content");
assert!(matches!(
f.pending_drop_remove,
Some(crate::mmap_file::PendingDelete::NeedsUnlink { .. })
));
drop(original);
}
/// With a matching identity, retry succeeds and unlinks.
#[test]
fn explicit_retry_with_matching_identity_unlinks() {
let probe_path = crate::tests::get_random_filename();
defer!(let _ = std::fs::remove_file(&probe_path););
// Create the file and capture its identity. Don't delete it; the
// path still names the same inode, so retry must succeed.
let mut original = std::fs::File::create(&probe_path).unwrap();
original.write_all(b"to-be-deleted").unwrap();
original.sync_all().unwrap();
let identity = crate::utils::FileIdentity::from_file(&original).unwrap();
#[cfg(unix)]
let pin = dup_for_pin(&original);
drop(original);
assert!(probe_path.exists());
let mut f = MmapFileMut::memory_from_vec("dummy.mem", vec![1u8]);
f.pending_drop_remove = Some(crate::mmap_file::PendingDelete::NeedsUnlink {
path: probe_path.clone(),
identity,
#[cfg(unix)]
pin,
});
f.remove().expect("identity matches → retry must unlink");
assert!(!probe_path.exists(), "unlink must have succeeded");
assert!(f.pending_drop_remove.is_none());
}
/// POSIX unlink permission is controlled by the parent dir, not by
/// the file's mode bits — a `chmod 000` file in a writable parent
/// must still be removable through fmmap's identity-checked path.
/// A previous implementation opened the file to probe identity and
/// so failed with EACCES on unreadable files; we now use
/// `metadata()` (stat), which only needs execute on the parent dir.
#[cfg(unix)]
#[test]
fn unix_identity_check_works_on_chmod_000_file() {
use std::os::unix::fs::PermissionsExt;
let probe_path = crate::tests::get_random_filename();
defer!(let _ = {
// Restore perms in case the test panics mid-way; otherwise the
// tempfile cleanup helper can't remove the file.
let _ = std::fs::set_permissions(&probe_path, std::fs::Permissions::from_mode(0o600));
std::fs::remove_file(&probe_path)
};);
// Create the file, dup for the inode pin, capture identity.
let original = std::fs::File::create(&probe_path).unwrap();
let identity = crate::utils::FileIdentity::from_file(&original).unwrap();
let pin = dup_for_pin(&original);
drop(original);
// Strip all permissions on the file. The parent dir (typically
// /tmp with 0o1777 or similar) still permits unlink.
std::fs::set_permissions(&probe_path, std::fs::Permissions::from_mode(0o000)).unwrap();
// Identity probe via from_path uses metadata() now — works without
// read permission.
let probe = crate::utils::FileIdentity::from_path(&probe_path)
.expect("metadata-based identity probe must succeed for chmod 000 file");
assert!(identity.is_known_equal(&probe));
assert!(identity.matches_path(&probe_path));
// Wrap in PendingDelete and verify retry's identity check + unlink
// succeeds (the parent dir is writable, the file is unreadable).
let mut f = MmapFileMut::memory_from_vec("dummy.mem", vec![1u8]);
f.pending_drop_remove = Some(crate::mmap_file::PendingDelete::NeedsUnlink {
path: probe_path.clone(),
identity,
pin,
});
f.remove()
.expect("chmod 000 file must still be removable via identity-checked retry");
assert!(!probe_path.exists());
}
/// When the user passes a symlink path, `remove_file(path)`
/// removes only the symlink, not the target — so even with
/// matching identity (the probe and the open file both follow the
/// symlink to the same inode), the wrapper would otherwise
/// succeed-and-leave-the-real-file-behind. Verify that
/// identity-checked cleanup refuses symlink paths instead.
#[cfg(unix)]
#[test]
fn identity_check_refuses_symlink_path() {
use std::os::unix::fs::symlink;
let target_path = crate::tests::get_random_filename();
let mut symlink_path = crate::tests::get_random_filename();
symlink_path.set_extension("symlink");
defer!(let _ = std::fs::remove_file(&target_path););
defer!(let _ = std::fs::remove_file(&symlink_path););
// Create the real file and a symlink pointing at it.
{
let f = std::fs::File::create(&target_path).unwrap();
drop(f);
}
symlink(&target_path, &symlink_path).unwrap();
// Capture identity through the symlink (follows). Both target and
// symlink resolve to the same inode, so the matching-identity
// happy path would otherwise green-light deletion.
let original = std::fs::File::open(&symlink_path).unwrap();
let identity = crate::utils::FileIdentity::from_file(&original).unwrap();
let pin = dup_for_pin(&original);
drop(original);
// matches_path: symlink_metadata sees the link itself → refuse.
assert!(
!identity.matches_path(&symlink_path),
"matches_path must refuse when path is a symlink",
);
// The from_path probe directly reports the symlink-refusal error.
let err = crate::utils::FileIdentity::from_path(&symlink_path).unwrap_err();
assert!(
err.to_string().contains("refuses to follow symlink"),
"expected symlink-refusal error, got: {err}",
);
// Plumbed through the wrapper: a NeedsUnlink retry against a
// symlink path must NOT remove the symlink (or the target).
let mut f = MmapFileMut::memory_from_vec("dummy.mem", vec![1u8]);
f.pending_drop_remove = Some(crate::mmap_file::PendingDelete::NeedsUnlink {
path: symlink_path.clone(),
identity,
pin,
});
let err = f.remove().unwrap_err();
assert!(
err.to_string().contains("refuses to follow symlink")
|| err.to_string().contains("path-reuse detected"),
"expected symlink/path-reuse refusal, got: {err}",
);
assert!(symlink_path.exists(), "symlink must remain");
assert!(target_path.exists(), "target must remain");
}
/// Drop's pending-delete path must verify identity before
/// unlinking. With identity captured from the *original* file but
/// the path now naming a different inode, Drop must leave it alone.
#[test]
fn drop_does_not_unlink_path_reused_file_for_needs_unlink() {
let probe_path = crate::tests::get_random_filename();
defer!(let _ = std::fs::remove_file(&probe_path););
// Hold the original file open across the path swap so its inode
// is pinned (otherwise tmpfs may hand the same inode to the
// replacement file and defeat the simulation; see sibling test).
let original = std::fs::File::create(&probe_path).unwrap();
use std::io::Write;
let mut original = original;
original.write_all(b"original").unwrap();
original.sync_all().unwrap();
let original_identity = crate::utils::FileIdentity::from_file(&original).unwrap();
std::fs::remove_file(&probe_path).unwrap();
std::fs::write(&probe_path, b"unrelated content").unwrap();
{
let mut f = MmapFileMut::memory_from_vec("dummy.mem", vec![1u8]);
f.pending_drop_remove = Some(crate::mmap_file::PendingDelete::NeedsUnlink {
path: probe_path.clone(),
identity: original_identity,
#[cfg(unix)]
pin: dup_for_pin(&original),
});
drop(f);
}
assert!(
probe_path.exists(),
"Drop must NOT unlink a path-reused file (identity mismatch)",
);
assert_eq!(std::fs::read(&probe_path).unwrap(), b"unrelated content");
drop(original);
}
/// Complement to the above — when identity matches, Drop's
/// pending-delete path *does* unlink, now safely guarded by
/// identity verification.
#[test]
fn drop_unlinks_when_identity_matches() {
let probe_path = crate::tests::get_random_filename();
defer!(let _ = std::fs::remove_file(&probe_path););
let mut original = std::fs::File::create(&probe_path).unwrap();
original.write_all(b"to-be-deleted").unwrap();
original.sync_all().unwrap();
let identity = crate::utils::FileIdentity::from_file(&original).unwrap();
#[cfg(unix)]
let pin = dup_for_pin(&original);
drop(original);
assert!(probe_path.exists());
{
let mut f = MmapFileMut::memory_from_vec("dummy.mem", vec![1u8]);
f.pending_drop_remove = Some(crate::mmap_file::PendingDelete::NeedsUnlink {
path: probe_path.clone(),
identity,
#[cfg(unix)]
pin,
});
drop(f);
}
assert!(
!probe_path.exists(),
"Drop must unlink when identity matches the path",
);
}
/// The *initial* `remove()` / `drop_remove()` call must
/// identity-check before `remove_file`. Between the wrapper
/// dropping its file handle and the unlink, an outside actor could
/// rename + recreate the path with a different file. Without the
/// check, the initial unlink would delete that unrelated file.
/// Simulate the race by:
/// 1. opening the wrapper (captures identity from the original inode),
/// 2. externally swapping the path with a different file,
/// 3. calling `remove()`, which must refuse to unlink.
#[test]
fn initial_remove_refuses_path_reused_file() {
let path = crate::tests::get_random_filename();
defer!(let _ = std::fs::remove_file(&path););
let mut f = unsafe { MmapFileMut::create(&path) }.unwrap();
f.truncate(8).unwrap();
// Simulate the race: replace the file at the path with a different
// file (different inode). `MmapFileMut`'s captured identity now
// points at an inode that's been unlinked; the path names a new file.
std::fs::remove_file(&path).unwrap();
std::fs::write(&path, b"unrelated content").unwrap();
let err = f.remove().unwrap_err();
assert!(
err.to_string().contains("path-reuse detected"),
"expected path-reuse error, got: {err}",
);
assert!(
path.exists(),
"initial unlink must NOT have deleted the path-reused file",
);
assert_eq!(std::fs::read(&path).unwrap(), b"unrelated content");
// Pending state is `NeedsUnlink` so the user sees the unfinished
// cleanup; retry would also refuse.
assert!(matches!(
f.pending_drop_remove,
Some(crate::mmap_file::PendingDelete::NeedsUnlink { .. })
));
}
/// Same race as the wrapper test, but for raw
/// `DiskMmapFileMut::drop_remove`. The raw API must not unlink
/// either.
#[test]
fn raw_drop_remove_refuses_path_reused_file() {
use crate::raw::DiskMmapFileMut;
let path = crate::tests::get_random_filename();
defer!(let _ = std::fs::remove_file(&path););
let mut f = unsafe { DiskMmapFileMut::create(&path) }.unwrap();
f.truncate(8).unwrap();
f.flush().unwrap();
// Race window: swap path content with a different file.
std::fs::remove_file(&path).unwrap();
std::fs::write(&path, b"unrelated content").unwrap();
let err = f.drop_remove().unwrap_err();
assert!(
err.to_string().contains("path-reuse detected"),
"expected path-reuse error, got: {err}",
);
assert!(
path.exists(),
"raw drop_remove must not delete a path-reused file"
);
assert_eq!(std::fs::read(&path).unwrap(), b"unrelated content");
}
/// `remove_on_drop` direct path verifies identity from the inner
/// before unlinking. Verify the file IS unlinked when the path
/// still names the same inode at Drop time.
#[test]
fn remove_on_drop_unlinks_when_identity_matches() {
let path = crate::tests::get_random_filename();
defer!(let _ = std::fs::remove_file(&path););
{
let mut f = unsafe { MmapFileMut::create(&path) }.unwrap();
f.set_remove_on_drop(true);
}
assert!(
!path.exists(),
"remove_on_drop must unlink when path identity matches",
);
}
}