fsqlite-vfs 0.1.16

Virtual filesystem abstraction layer
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583
2584
2585
2586
2587
2588
2589
2590
2591
2592
2593
2594
2595
2596
2597
2598
2599
2600
2601
2602
2603
2604
2605
2606
2607
2608
2609
2610
2611
2612
2613
2614
2615
2616
2617
2618
2619
2620
2621
2622
2623
2624
2625
2626
2627
2628
2629
2630
2631
2632
2633
2634
2635
2636
2637
2638
2639
2640
2641
2642
2643
2644
2645
2646
2647
2648
2649
2650
2651
2652
2653
2654
2655
2656
2657
2658
2659
2660
2661
2662
2663
2664
2665
2666
2667
2668
2669
2670
2671
2672
2673
2674
2675
2676
2677
2678
2679
2680
2681
2682
2683
2684
2685
2686
2687
2688
2689
2690
2691
2692
2693
2694
2695
2696
2697
2698
2699
2700
2701
2702
2703
2704
2705
2706
2707
2708
2709
2710
2711
2712
2713
2714
2715
2716
2717
2718
2719
2720
2721
2722
2723
2724
2725
2726
2727
2728
2729
2730
2731
2732
2733
2734
2735
2736
2737
2738
2739
2740
2741
2742
2743
2744
2745
2746
2747
2748
2749
2750
2751
2752
2753
2754
2755
2756
2757
2758
2759
2760
2761
2762
2763
2764
2765
2766
2767
2768
2769
2770
2771
2772
2773
2774
2775
2776
2777
2778
2779
2780
2781
2782
2783
2784
2785
2786
2787
2788
2789
2790
2791
2792
2793
2794
2795
2796
2797
2798
2799
2800
2801
2802
2803
2804
2805
2806
2807
2808
2809
2810
2811
2812
2813
2814
2815
2816
2817
2818
2819
2820
2821
2822
2823
2824
2825
2826
2827
2828
2829
2830
2831
2832
2833
2834
2835
2836
2837
2838
2839
2840
2841
2842
2843
2844
2845
2846
2847
2848
2849
2850
2851
2852
2853
2854
2855
2856
2857
2858
2859
2860
2861
2862
2863
2864
2865
2866
2867
2868
2869
2870
2871
2872
2873
2874
2875
2876
2877
2878
2879
2880
2881
2882
2883
2884
2885
2886
2887
2888
2889
2890
2891
2892
2893
2894
2895
2896
2897
2898
2899
2900
2901
2902
2903
2904
2905
2906
2907
2908
2909
2910
2911
2912
2913
2914
2915
2916
2917
2918
2919
2920
2921
2922
2923
2924
2925
2926
2927
2928
2929
2930
2931
2932
2933
2934
2935
2936
2937
2938
2939
2940
2941
2942
2943
2944
2945
2946
2947
2948
2949
2950
2951
2952
2953
2954
2955
2956
2957
2958
2959
2960
2961
2962
2963
2964
2965
2966
2967
2968
2969
2970
2971
2972
2973
2974
2975
2976
2977
2978
2979
2980
2981
2982
2983
2984
2985
2986
2987
2988
2989
2990
2991
2992
2993
2994
2995
2996
2997
2998
2999
3000
3001
3002
3003
3004
3005
3006
3007
3008
3009
3010
3011
3012
3013
3014
3015
3016
3017
3018
3019
3020
3021
3022
3023
3024
3025
3026
3027
3028
3029
3030
3031
3032
3033
3034
3035
3036
3037
3038
3039
3040
3041
3042
3043
3044
3045
3046
3047
3048
3049
3050
3051
3052
3053
3054
3055
3056
3057
3058
3059
3060
3061
3062
3063
3064
3065
3066
3067
3068
3069
3070
3071
3072
3073
3074
3075
3076
3077
3078
3079
3080
3081
3082
3083
3084
3085
3086
3087
3088
3089
3090
3091
3092
3093
3094
3095
3096
3097
3098
3099
3100
3101
3102
3103
3104
3105
3106
3107
3108
3109
3110
3111
3112
3113
3114
3115
3116
3117
3118
3119
3120
3121
3122
3123
3124
3125
3126
3127
3128
3129
3130
3131
3132
3133
3134
3135
3136
3137
3138
3139
3140
3141
3142
3143
3144
3145
3146
3147
3148
3149
3150
3151
3152
3153
3154
3155
3156
3157
3158
3159
3160
3161
3162
3163
3164
3165
3166
3167
3168
3169
3170
3171
3172
3173
3174
3175
3176
3177
3178
3179
3180
3181
3182
3183
3184
3185
3186
3187
3188
3189
3190
3191
3192
3193
3194
3195
3196
3197
3198
3199
3200
3201
3202
3203
3204
3205
3206
3207
3208
3209
3210
3211
3212
3213
3214
3215
3216
3217
3218
3219
3220
3221
3222
3223
3224
3225
3226
3227
3228
3229
3230
3231
3232
3233
3234
3235
3236
3237
3238
3239
3240
3241
3242
3243
3244
3245
3246
3247
3248
3249
3250
3251
3252
3253
3254
3255
3256
3257
3258
3259
3260
3261
3262
3263
3264
3265
3266
3267
3268
3269
3270
3271
3272
3273
3274
3275
3276
3277
3278
3279
3280
3281
3282
3283
3284
3285
3286
3287
3288
3289
3290
3291
3292
3293
3294
3295
3296
3297
3298
3299
3300
3301
3302
3303
3304
3305
3306
3307
3308
3309
3310
3311
3312
3313
3314
3315
3316
3317
3318
3319
3320
3321
3322
3323
3324
3325
3326
3327
3328
3329
3330
3331
3332
3333
3334
3335
3336
3337
3338
3339
3340
3341
3342
3343
3344
3345
3346
3347
3348
3349
3350
3351
3352
3353
3354
3355
3356
3357
3358
3359
3360
3361
3362
3363
3364
3365
3366
3367
3368
3369
3370
3371
3372
3373
3374
3375
3376
3377
3378
3379
3380
3381
3382
3383
3384
3385
3386
3387
3388
3389
3390
3391
3392
3393
3394
3395
3396
3397
3398
3399
3400
3401
3402
3403
3404
3405
3406
3407
3408
3409
3410
3411
3412
3413
3414
3415
3416
3417
3418
3419
3420
3421
3422
3423
3424
3425
3426
3427
3428
3429
3430
3431
3432
3433
3434
3435
3436
3437
3438
3439
3440
3441
3442
3443
3444
3445
3446
3447
3448
3449
3450
3451
3452
3453
3454
3455
3456
3457
3458
3459
3460
3461
3462
3463
3464
3465
3466
3467
3468
3469
3470
3471
3472
3473
3474
3475
3476
3477
3478
3479
3480
3481
3482
3483
3484
3485
3486
3487
3488
3489
3490
3491
3492
3493
3494
3495
3496
3497
3498
3499
3500
3501
3502
3503
3504
3505
3506
3507
3508
3509
3510
3511
3512
3513
3514
3515
3516
3517
3518
3519
3520
3521
3522
3523
3524
3525
3526
3527
3528
3529
3530
3531
3532
3533
3534
3535
3536
3537
3538
3539
3540
3541
3542
3543
3544
3545
3546
3547
3548
3549
3550
3551
3552
3553
3554
3555
3556
3557
3558
3559
3560
3561
3562
3563
3564
3565
3566
3567
3568
3569
3570
3571
3572
3573
3574
3575
3576
3577
3578
3579
3580
3581
3582
3583
3584
3585
3586
3587
3588
3589
3590
3591
3592
3593
3594
3595
3596
3597
3598
3599
3600
3601
3602
3603
3604
3605
3606
3607
3608
3609
3610
3611
3612
//! Windows VFS implementation.
//!
//! This backend provides the same `Vfs` / `VfsFile` surface as `UnixVfs`,
//! using Windows-friendly file APIs and lock sidecars backed by OS advisory
//! locks (`LockFileEx` via `advisory-lock`) that mirror SQLite lock-level
//! transitions (`NONE` → `SHARED` → `RESERVED` → `PENDING` → `EXCLUSIVE`).

use std::collections::HashMap;
use std::env;
use std::ffi::{OsString, c_void};
use std::fs::{self, File, OpenOptions};
use std::os::windows::fs::{FileExt, OpenOptionsExt};
use std::os::windows::io::AsRawHandle as _;
use std::path::{Path, PathBuf};
use std::sync::atomic::{AtomicU64, Ordering, fence};
use std::sync::{Arc, Mutex, OnceLock};

use advisory_lock::{AdvisoryFileLock, FileLockError, FileLockMode};
use fsqlite_error::{FrankenError, Result};
use fsqlite_types::LockLevel;
use fsqlite_types::cx::Cx;
use fsqlite_types::flags::{AccessFlags, SyncFlags, VfsOpenFlags};
use tracing::{debug, info};

use crate::shm::{
    SQLITE_SHM_EXCLUSIVE, SQLITE_SHM_LOCK, SQLITE_SHM_SHARED, SQLITE_SHM_UNLOCK, ShmRegion,
    WAL_CKPT_LOCK, WAL_TOTAL_LOCKS, WAL_WRITE_LOCK,
};
use crate::traits::{FileIdentity, Vfs, VfsFile};

/// SQLite I/O capability bit indicating files cannot be deleted while open.
const SQLITE_IOCAP_UNDELETABLE_WHEN_OPEN: u32 = 0x0000_0800;
const WINDOWS_FILE_SHARE_READ: u32 = 0x0000_0001;
const WINDOWS_FILE_SHARE_WRITE: u32 = 0x0000_0002;
const WINDOWS_SHARE_READ_WRITE: u32 = WINDOWS_FILE_SHARE_READ | WINDOWS_FILE_SHARE_WRITE;

// Stock SQLite's Windows VFS coordinates main-database access through these
// byte ranges on the *database file itself*. They intentionally match the
// constants in SQLite's os_win.c and the Unix VFS in this crate.
const STOCK_SQLITE_PENDING_BYTE: u64 = 0x4000_0000;
const STOCK_SQLITE_RESERVED_BYTE: u64 = STOCK_SQLITE_PENDING_BYTE + 1;
const STOCK_SQLITE_SHARED_FIRST: u64 = STOCK_SQLITE_PENDING_BYTE + 2;
const STOCK_SQLITE_SHARED_SIZE: u64 = 510;

// SQLite's Windows WAL VFS places the eight WAL lock bytes at offsets 120..128
// of the real `-shm` file. WAL_WRITE_LOCK and WAL_CKPT_LOCK are slots 0 and 1.
const STOCK_SQLITE_SHM_LOCK_BASE: u64 = 120;
const STOCK_SQLITE_WAL_WRITE_BYTE: u64 = STOCK_SQLITE_SHM_LOCK_BASE;
const STOCK_SQLITE_WAL_CKPT_BYTE: u64 = STOCK_SQLITE_SHM_LOCK_BASE + 1;

const LOCKFILE_FAIL_IMMEDIATELY: u32 = 0x0000_0001;
const LOCKFILE_EXCLUSIVE_LOCK: u32 = 0x0000_0002;
const ERROR_LOCK_VIOLATION: i32 = 33;
const ERROR_NOT_LOCKED: i32 = 158;

/// Layout-compatible subset of Win32 `OVERLAPPED` used for byte-range locks.
///
/// `windows-sys` gates `LockFileEx` behind an additional feature that this
/// crate does not otherwise need, so the two small FFI calls live here at the
/// platform boundary. The anonymous union in the SDK is represented by its
/// two 32-bit offset fields; that has the same layout on 32-bit and 64-bit
/// Windows.
#[repr(C)]
struct WindowsOverlapped {
    internal: usize,
    internal_high: usize,
    offset: u32,
    offset_high: u32,
    event: *mut c_void,
}

impl WindowsOverlapped {
    fn at(offset: u64) -> Self {
        Self {
            internal: 0,
            internal_high: 0,
            offset: offset as u32,
            offset_high: (offset >> 32) as u32,
            event: std::ptr::null_mut(),
        }
    }
}

#[link(name = "kernel32")]
unsafe extern "system" {
    #[link_name = "LockFileEx"]
    fn windows_lock_file_ex(
        file: *mut c_void,
        flags: u32,
        reserved: u32,
        bytes_low: u32,
        bytes_high: u32,
        overlapped: *mut WindowsOverlapped,
    ) -> i32;

    #[link_name = "UnlockFileEx"]
    fn windows_unlock_file_ex(
        file: *mut c_void,
        reserved: u32,
        bytes_low: u32,
        bytes_high: u32,
        overlapped: *mut WindowsOverlapped,
    ) -> i32;
}

fn split_u64(value: u64) -> (u32, u32) {
    (value as u32, (value >> 32) as u32)
}

#[derive(Clone, Copy)]
enum WindowsRangeLockMode {
    Shared,
    Exclusive,
}

fn try_lock_stock_sqlite_range_with_mode(
    file: &File,
    offset: u64,
    len: u64,
    mode: WindowsRangeLockMode,
) -> Result<()> {
    if len == 0 {
        return Err(FrankenError::internal(
            "cannot acquire a zero-length Windows byte-range lock",
        ));
    }
    let (bytes_low, bytes_high) = split_u64(len);
    let mut overlapped = WindowsOverlapped::at(offset);
    // SAFETY: `file` owns a live Windows handle, `overlapped` is layout-
    // compatible with Win32 OVERLAPPED and remains live for the synchronous
    // nonblocking call, and the requested range is non-empty.
    let flags = LOCKFILE_FAIL_IMMEDIATELY
        | match mode {
            WindowsRangeLockMode::Shared => 0,
            WindowsRangeLockMode::Exclusive => LOCKFILE_EXCLUSIVE_LOCK,
        };
    let locked = unsafe {
        windows_lock_file_ex(
            file.as_raw_handle().cast(),
            flags,
            0,
            bytes_low,
            bytes_high,
            &raw mut overlapped,
        )
    };
    if locked != 0 {
        return Ok(());
    }
    let error = std::io::Error::last_os_error();
    if error.raw_os_error() == Some(ERROR_LOCK_VIOLATION) {
        Err(FrankenError::Busy)
    } else {
        Err(FrankenError::Io(error))
    }
}

fn try_lock_stock_sqlite_range(file: &File, offset: u64, len: u64) -> Result<()> {
    try_lock_stock_sqlite_range_with_mode(file, offset, len, WindowsRangeLockMode::Exclusive)
}

fn try_lock_stock_sqlite_shared_range(file: &File, offset: u64, len: u64) -> Result<()> {
    try_lock_stock_sqlite_range_with_mode(file, offset, len, WindowsRangeLockMode::Shared)
}

fn unlock_stock_sqlite_range(file: &File, offset: u64, len: u64) -> Result<()> {
    let (bytes_low, bytes_high) = split_u64(len);
    let mut overlapped = WindowsOverlapped::at(offset);
    // SAFETY: the handle and OVERLAPPED invariants are the same as in
    // `try_lock_stock_sqlite_range`; this exact range was previously locked
    // through the same handle.
    let unlocked = unsafe {
        windows_unlock_file_ex(
            file.as_raw_handle().cast(),
            0,
            bytes_low,
            bytes_high,
            &raw mut overlapped,
        )
    };
    if unlocked != 0 {
        return Ok(());
    }
    let error = std::io::Error::last_os_error();
    if error.raw_os_error() == Some(ERROR_NOT_LOCKED) {
        Ok(())
    } else {
        Err(FrankenError::Io(error))
    }
}

fn checkpoint_or_abort(cx: &Cx) -> Result<()> {
    cx.checkpoint().map_err(|_| FrankenError::Abort)
}

fn lock_poisoned(name: &str) -> FrankenError {
    FrankenError::internal(format!("{name} lock poisoned"))
}

fn windows_open_options() -> OpenOptions {
    let mut options = OpenOptions::new();
    options.share_mode(WINDOWS_SHARE_READ_WRITE);
    options
}

fn resolve_path(path: &Path) -> Result<PathBuf> {
    if path.is_absolute() {
        Ok(path.to_path_buf())
    } else {
        Ok(env::current_dir()?.join(path))
    }
}

fn stable_full_path(path: &Path) -> Result<PathBuf> {
    let absolute = resolve_path(path)?;

    match absolute.canonicalize() {
        Ok(canonical) => Ok(canonical),
        Err(error) if error.kind() == std::io::ErrorKind::NotFound => {
            let parent = absolute.parent().ok_or_else(|| FrankenError::CannotOpen {
                path: absolute.clone(),
            })?;
            let file_name = absolute
                .file_name()
                .ok_or_else(|| FrankenError::CannotOpen {
                    path: absolute.clone(),
                })?;
            let canonical_parent = parent.canonicalize()?;
            Ok(canonical_parent.join(file_name))
        }
        Err(error) => Err(FrankenError::Io(error)),
    }
}

fn sqlite_shm_path(path: &Path) -> PathBuf {
    let mut shm: OsString = path.as_os_str().to_owned();
    shm.push("-shm");
    PathBuf::from(shm)
}

fn sqlite_companion_path(path: &Path, suffix: &str) -> PathBuf {
    let mut companion: OsString = path.as_os_str().to_owned();
    companion.push(suffix);
    PathBuf::from(companion)
}

fn sqlite_shared_lock_path(path: &Path) -> PathBuf {
    let mut p: OsString = path.as_os_str().to_owned();
    p.push("-lock-shared");
    PathBuf::from(p)
}

fn sqlite_reserved_lock_path(path: &Path) -> PathBuf {
    let mut p: OsString = path.as_os_str().to_owned();
    p.push("-lock-reserved");
    PathBuf::from(p)
}

fn sqlite_pending_lock_path(path: &Path) -> PathBuf {
    let mut p: OsString = path.as_os_str().to_owned();
    p.push("-lock-pending");
    PathBuf::from(p)
}

// The three advisory-lock sidecars `WindowsOsLockFiles::open` writes next to
// every DB it touches. Returned as an array so callers can iterate uniformly.
fn windows_lock_sidecar_paths(path: &Path) -> [PathBuf; 3] {
    [
        sqlite_shared_lock_path(path),
        sqlite_reserved_lock_path(path),
        sqlite_pending_lock_path(path),
    ]
}

fn reserved_database_artifact_paths(path: &Path) -> [PathBuf; 7] {
    let [shared, reserved, pending] = windows_lock_sidecar_paths(path);
    [
        sqlite_companion_path(path, "-journal"),
        sqlite_companion_path(path, "-wal"),
        sqlite_companion_path(path, "-wal-fec"),
        sqlite_shm_path(path),
        shared,
        reserved,
        pending,
    ]
}

fn filesystem_entry_exists(path: &Path) -> Result<bool> {
    match fs::symlink_metadata(path) {
        Ok(_) => Ok(true),
        Err(err) if err.kind() == std::io::ErrorKind::NotFound => Ok(false),
        Err(err) => Err(FrankenError::Io(err)),
    }
}

// Best-effort removal of the three advisory-lock sidecars alongside `path`.
// Errors are intentionally swallowed: sidecars are advisory and may be missing,
// in use by a racing handle, or already cleaned up. Without this, every
// transient DB file (e.g. VACUUM INTO backups) leaks three zero-byte files,
// and a downstream caller that re-enumerates the dir can mistake an orphan
// sidecar for a backup root and chain a fresh set on top.
fn try_remove_windows_lock_sidecars(path: &Path) {
    for sidecar in windows_lock_sidecar_paths(path) {
        let _ = fs::remove_file(sidecar);
    }
}

fn ensure_shm_file_len(path: &Path, min_len: u64) -> Result<()> {
    let mut options = windows_open_options();
    let file = options
        .read(true)
        .write(true)
        .create(true)
        .truncate(false)
        .open(path)?;
    let current = file.metadata()?.len();
    if current < min_len {
        file.set_len(min_len)?;
    }
    Ok(())
}

fn open_windows_lock_sidecar(path: &Path) -> Result<(File, bool)> {
    loop {
        let mut create_options = windows_open_options();
        match create_options
            .read(true)
            .write(true)
            .create_new(true)
            .open(path)
        {
            Ok(file) => return Ok((file, true)),
            Err(err) if err.kind() == std::io::ErrorKind::AlreadyExists => {
                let mut open_options = windows_open_options();
                match open_options.read(true).write(true).open(path) {
                    Ok(file) => return Ok((file, false)),
                    Err(err) if err.kind() == std::io::ErrorKind::NotFound => {}
                    Err(err) => return Err(FrankenError::Io(err)),
                }
            }
            Err(err) => return Err(FrankenError::Io(err)),
        }
    }
}

#[derive(Debug, Default)]
struct WindowsVfsInner {
    next_temp_id: u64,
}

/// Windows filesystem-backed VFS implementation.
#[derive(Debug, Clone, Default)]
pub struct WindowsVfs {
    inner: Arc<Mutex<WindowsVfsInner>>,
}

impl WindowsVfs {
    /// Create a new Windows VFS instance.
    #[must_use]
    pub fn new() -> Self {
        info!(
            target: "fsqlite_vfs::windows",
            sector_size = 4096_u32,
            "windows vfs initialized"
        );
        Self::default()
    }

    fn next_temp_path(&self) -> Result<PathBuf> {
        let mut inner = self
            .inner
            .lock()
            .map_err(|_| lock_poisoned("windows vfs inner"))?;
        let id = inner.next_temp_id.max(next_temp_id());
        inner.next_temp_id = id
            .checked_add(1)
            .ok_or_else(|| FrankenError::internal("temp file id overflow"))?;
        Ok(env::temp_dir().join(format!("fsqlite-windows-{id}.tmp")))
    }
}

#[derive(Debug, Clone, Default)]
struct ShmSlotState {
    shared_holders: HashMap<u64, u32>,
    exclusive_owner: Option<u64>,
}

#[derive(Debug)]
struct WindowsShmState {
    regions: HashMap<u32, ShmRegion>,
    slots: Vec<ShmSlotState>,
    owner_refs: HashMap<u64, u32>,
}

impl Default for WindowsShmState {
    fn default() -> Self {
        let slot_count = usize::try_from(WAL_TOTAL_LOCKS).expect("WAL_TOTAL_LOCKS must fit usize");
        Self {
            regions: HashMap::new(),
            slots: vec![ShmSlotState::default(); slot_count],
            owner_refs: HashMap::new(),
        }
    }
}

#[derive(Debug)]
struct WindowsShmTable {
    map: Mutex<HashMap<PathBuf, Arc<Mutex<WindowsShmState>>>>,
}

impl WindowsShmTable {
    fn new() -> Self {
        Self {
            map: Mutex::new(HashMap::new()),
        }
    }

    #[cfg(test)]
    fn get(&self, path: &Path) -> Result<Option<Arc<Mutex<WindowsShmState>>>> {
        let map = self
            .map
            .lock()
            .map_err(|_| lock_poisoned("windows shm table"))?;
        Ok(map.get(path).map(Arc::clone))
    }

    fn get_existing_and_register_with<T>(
        &self,
        path: &Path,
        owner_id: u64,
        before_register: impl FnOnce(),
        inspect: impl FnOnce(&WindowsShmState) -> Result<T>,
    ) -> Result<Option<(Arc<Mutex<WindowsShmState>>, T)>> {
        let map = self
            .map
            .lock()
            .map_err(|_| lock_poisoned("windows shm table"))?;
        let Some(state) = map.get(path).map(Arc::clone) else {
            return Ok(None);
        };

        // Match get_or_create_and_register's map -> state lock order and keep
        // the owner registration in the same table critical section.  The
        // non-extending xShmMap path used to clone the state, release `map`,
        // and only then increment owner_refs.  A concurrent last close could
        // remove the table entry in that gap and split later handles across
        // two independent lock domains.
        before_register();
        let inspected = {
            let mut guard = state
                .lock()
                .map_err(|_| lock_poisoned("windows shm state"))?;
            let inspected = inspect(&guard)?;
            *guard.owner_refs.entry(owner_id).or_insert(0) += 1;
            inspected
        };
        drop(map);
        Ok(Some((state, inspected)))
    }

    fn get_or_create_and_register(
        &self,
        path: &Path,
        owner_id: u64,
    ) -> Result<Arc<Mutex<WindowsShmState>>> {
        self.get_or_create_and_register_with(path, owner_id, || {})
    }

    fn get_or_create_and_register_with(
        &self,
        path: &Path,
        owner_id: u64,
        before_register: impl FnOnce(),
    ) -> Result<Arc<Mutex<WindowsShmState>>> {
        let mut map = self
            .map
            .lock()
            .map_err(|_| lock_poisoned("windows shm table"))?;
        let state = Arc::clone(
            map.entry(path.to_path_buf())
                .or_insert_with(|| Arc::new(Mutex::new(WindowsShmState::default()))),
        );

        // Keep registration inside the table critical section. Otherwise the
        // last old owner could remove the entry after a new opener cloned it
        // but before that opener made its ownership visible, leaving the new
        // handle on a detached SHM lock domain.
        before_register();
        {
            let mut guard = state
                .lock()
                .map_err(|_| lock_poisoned("windows shm state"))?;
            *guard.owner_refs.entry(owner_id).or_insert(0) += 1;
        }
        drop(map);
        Ok(state)
    }

    fn remove_if_orphaned(
        &self,
        path: &Path,
        expected: &Arc<Mutex<WindowsShmState>>,
    ) -> Result<()> {
        let mut map = self
            .map
            .lock()
            .map_err(|_| lock_poisoned("windows shm table"))?;
        if let Some(state) = map.get(path) {
            if !Arc::ptr_eq(state, expected) {
                return Ok(());
            }
            let orphaned = state
                .lock()
                .map_err(|_| lock_poisoned("windows shm state"))?
                .owner_refs
                .is_empty();
            if orphaned {
                map.remove(path);
            }
        }
        Ok(())
    }
}

fn windows_shm_table() -> &'static WindowsShmTable {
    static TABLE: OnceLock<WindowsShmTable> = OnceLock::new();
    TABLE.get_or_init(WindowsShmTable::new)
}

fn next_owner_id() -> u64 {
    static OWNER_SEQ: AtomicU64 = AtomicU64::new(1);
    OWNER_SEQ.fetch_add(1, Ordering::Relaxed)
}

fn next_temp_id() -> u64 {
    static TEMP_SEQ: AtomicU64 = AtomicU64::new(1);
    TEMP_SEQ.fetch_add(1, Ordering::Relaxed)
}

fn to_slot_index(slot: u32) -> Result<usize> {
    usize::try_from(slot).map_err(|_| FrankenError::OutOfRange {
        what: "shm slot index".to_string(),
        value: slot.to_string(),
    })
}

fn next_lock_level(level: LockLevel) -> Option<LockLevel> {
    match level {
        LockLevel::None => Some(LockLevel::Shared),
        LockLevel::Shared => Some(LockLevel::Reserved),
        LockLevel::Reserved => Some(LockLevel::Pending),
        LockLevel::Pending => Some(LockLevel::Exclusive),
        LockLevel::Exclusive => None,
    }
}

fn lock_level_slot(level: LockLevel) -> Option<usize> {
    match level {
        LockLevel::None => None,
        LockLevel::Shared => Some(0),
        LockLevel::Reserved => Some(1),
        LockLevel::Pending => Some(2),
        LockLevel::Exclusive => Some(3),
    }
}

#[derive(Debug)]
struct WindowsOsLockFiles {
    shared_file: File,
    reserved_file: File,
    pending_file: File,
    held_levels: [bool; 4],
}

impl WindowsOsLockFiles {
    fn open(path: &Path) -> Result<Self> {
        let shared_path = sqlite_shared_lock_path(path);
        let reserved_path = sqlite_reserved_lock_path(path);
        let pending_path = sqlite_pending_lock_path(path);
        let (shared_file, shared_created) = open_windows_lock_sidecar(&shared_path)?;
        let (reserved_file, reserved_created) = match open_windows_lock_sidecar(&reserved_path) {
            Ok(opened) => opened,
            Err(err) => {
                drop(shared_file);
                if shared_created {
                    let _ = fs::remove_file(&shared_path);
                }
                return Err(err);
            }
        };
        let (pending_file, _) = match open_windows_lock_sidecar(&pending_path) {
            Ok(opened) => opened,
            Err(err) => {
                drop(reserved_file);
                drop(shared_file);
                if reserved_created {
                    let _ = fs::remove_file(&reserved_path);
                }
                if shared_created {
                    let _ = fs::remove_file(&shared_path);
                }
                return Err(err);
            }
        };
        Ok(Self {
            shared_file,
            reserved_file,
            pending_file,
            held_levels: [false; 4],
        })
    }

    fn try_lock_shared(file: &File) -> Result<()> {
        match AdvisoryFileLock::try_lock(file, FileLockMode::Shared) {
            Ok(()) => Ok(()),
            Err(FileLockError::AlreadyLocked) => Err(FrankenError::Busy),
            Err(FileLockError::Io(err)) => Err(FrankenError::Io(err)),
        }
    }

    fn try_lock_exclusive(file: &File) -> Result<()> {
        match AdvisoryFileLock::try_lock(file, FileLockMode::Exclusive) {
            Ok(()) => Ok(()),
            Err(FileLockError::AlreadyLocked) => Err(FrankenError::Busy),
            Err(FileLockError::Io(err)) => Err(FrankenError::Io(err)),
        }
    }

    fn unlock_file(file: &File) -> Result<()> {
        match AdvisoryFileLock::unlock(file) {
            Ok(()) => Ok(()),
            Err(FileLockError::AlreadyLocked) => Err(FrankenError::LockFailed {
                detail: "unlock called for contended lock".to_string(),
            }),
            Err(FileLockError::Io(err)) => Err(FrankenError::Io(err)),
        }
    }

    fn lock_file_for_level(&self, level: LockLevel) -> Option<&File> {
        match level {
            LockLevel::None => None,
            LockLevel::Shared | LockLevel::Exclusive => Some(&self.shared_file),
            LockLevel::Reserved => Some(&self.reserved_file),
            LockLevel::Pending => Some(&self.pending_file),
        }
    }

    fn lock_held(&self, level: LockLevel) -> bool {
        lock_level_slot(level).is_some_and(|slot| self.held_levels[slot])
    }

    fn set_lock_held(&mut self, level: LockLevel, held: bool) {
        if let Some(slot) = lock_level_slot(level) {
            self.held_levels[slot] = held;
        }
    }

    fn try_lock_level(&mut self, level: LockLevel) -> Result<()> {
        if level == LockLevel::None {
            return Ok(());
        }

        if self.lock_held(level) {
            return Ok(());
        }

        if level == LockLevel::Shared {
            // Match SQLite's pending-byte protocol: readers briefly take a
            // shared lock on the pending sidecar before acquiring the shared
            // range. A pending writer holds this sidecar exclusively, blocking
            // new readers while existing readers drain.
            Self::try_lock_shared(&self.pending_file)?;
            let shared_result = Self::try_lock_shared(&self.shared_file);
            let pending_unlock = Self::unlock_file(&self.pending_file);
            if let Err(err) = shared_result {
                pending_unlock?;
                return Err(err);
            }
            if let Err(err) = pending_unlock {
                let _ = Self::unlock_file(&self.shared_file);
                return Err(err);
            }
            self.set_lock_held(LockLevel::Shared, true);
            return Ok(());
        }

        if level == LockLevel::Exclusive {
            // EXCLUSIVE conflicts with SHARED by upgrading the same shared
            // sidecar from shared to exclusive. Locking a separate
            // "exclusive" sidecar would only exclude other writers and would
            // allow readers through.
            let had_shared = self.lock_held(LockLevel::Shared);
            if had_shared {
                Self::unlock_file(&self.shared_file)?;
                self.set_lock_held(LockLevel::Shared, false);
            }
            if let Err(err) = Self::try_lock_exclusive(&self.shared_file) {
                if had_shared && Self::try_lock_shared(&self.shared_file).is_ok() {
                    self.set_lock_held(LockLevel::Shared, true);
                }
                return Err(err);
            }
            self.set_lock_held(LockLevel::Exclusive, true);
            return Ok(());
        }

        let file = self
            .lock_file_for_level(level)
            .ok_or_else(|| FrankenError::internal("invalid lock level"))?;
        Self::try_lock_exclusive(file)?;
        self.set_lock_held(level, true);
        Ok(())
    }

    fn unlock_to(&mut self, level: LockLevel) -> Result<()> {
        if self.lock_held(LockLevel::Exclusive) && level < LockLevel::Exclusive {
            Self::unlock_file(&self.shared_file)?;
            self.set_lock_held(LockLevel::Exclusive, false);
            if level >= LockLevel::Shared {
                Self::try_lock_shared(&self.shared_file)?;
                self.set_lock_held(LockLevel::Shared, true);
            }
        }

        for held_level in [LockLevel::Pending, LockLevel::Reserved, LockLevel::Shared] {
            if level < held_level && self.lock_held(held_level) {
                let file = self
                    .lock_file_for_level(held_level)
                    .ok_or_else(|| FrankenError::internal("invalid lock level"))?;
                Self::unlock_file(file)?;
                self.set_lock_held(held_level, false);
            }
        }
        Ok(())
    }

    fn highest_held_level(&self) -> LockLevel {
        [
            LockLevel::Exclusive,
            LockLevel::Pending,
            LockLevel::Reserved,
            LockLevel::Shared,
        ]
        .into_iter()
        .find(|level| self.lock_held(*level))
        .unwrap_or(LockLevel::None)
    }

    fn reserved_locked_by_other(&self) -> Result<bool> {
        if self.lock_held(LockLevel::Reserved) {
            return Ok(false);
        }

        match AdvisoryFileLock::try_lock(&self.reserved_file, FileLockMode::Exclusive) {
            Ok(()) => {
                Self::unlock_file(&self.reserved_file)?;
                Ok(false)
            }
            Err(FileLockError::AlreadyLocked) => Ok(true),
            Err(FileLockError::Io(err)) => Err(FrankenError::Io(err)),
        }
    }
}

/// Stock-SQLite-visible main-database lock state for an ordinary connection.
///
/// The dedicated handle makes handle closure a synchronous final unlock if an
/// explicit `UnlockFileEx` reports an error during cleanup.
#[derive(Debug)]
#[allow(clippy::struct_excessive_bools)]
struct WindowsStockMainLocks {
    main_file: File,
    pending: bool,
    reserved: bool,
    shared_range: bool,
    shared_range_exclusive: bool,
    lock_level: LockLevel,
}

impl WindowsStockMainLocks {
    const fn new(main_file: File) -> Self {
        Self {
            main_file,
            pending: false,
            reserved: false,
            shared_range: false,
            shared_range_exclusive: false,
            lock_level: LockLevel::None,
        }
    }

    fn try_lock_level(&mut self, level: LockLevel) -> Result<()> {
        if level <= self.lock_level {
            return Ok(());
        }

        match level {
            LockLevel::None => {}
            LockLevel::Shared => {
                // Match winReadLock() in stock SQLite: briefly participate in
                // PENDING, retain a shared lock over the reader range, then
                // release PENDING so later readers can join the snapshot.
                try_lock_stock_sqlite_shared_range(&self.main_file, STOCK_SQLITE_PENDING_BYTE, 1)?;
                self.pending = true;
                if let Err(lock_error) = try_lock_stock_sqlite_shared_range(
                    &self.main_file,
                    STOCK_SQLITE_SHARED_FIRST,
                    STOCK_SQLITE_SHARED_SIZE,
                ) {
                    let unlock_result =
                        unlock_stock_sqlite_range(&self.main_file, STOCK_SQLITE_PENDING_BYTE, 1);
                    if unlock_result.is_ok() {
                        self.pending = false;
                    }
                    return match unlock_result {
                        Ok(()) => Err(lock_error),
                        Err(unlock_error) => Err(FrankenError::internal(format!(
                            "stock SQLite Windows SHARED acquisition failed and could not release transient PENDING: lock={lock_error}; unlock={unlock_error}"
                        ))),
                    };
                }
                self.shared_range = true;
                if let Err(unlock_error) =
                    unlock_stock_sqlite_range(&self.main_file, STOCK_SQLITE_PENDING_BYTE, 1)
                {
                    let shared_cleanup = unlock_stock_sqlite_range(
                        &self.main_file,
                        STOCK_SQLITE_SHARED_FIRST,
                        STOCK_SQLITE_SHARED_SIZE,
                    );
                    if shared_cleanup.is_ok() {
                        self.shared_range = false;
                    }
                    return match shared_cleanup {
                        Ok(()) => Err(unlock_error),
                        Err(shared_error) => Err(FrankenError::internal(format!(
                            "stock SQLite Windows SHARED acquisition could not release transient PENDING or unwind SHARED: pending={unlock_error}; shared={shared_error}"
                        ))),
                    };
                }
                self.pending = false;
            }
            LockLevel::Reserved => {
                try_lock_stock_sqlite_range(&self.main_file, STOCK_SQLITE_RESERVED_BYTE, 1)?;
                self.reserved = true;
            }
            LockLevel::Pending => {
                try_lock_stock_sqlite_range(&self.main_file, STOCK_SQLITE_PENDING_BYTE, 1)?;
                self.pending = true;
            }
            LockLevel::Exclusive => {
                // LockFileEx cannot atomically promote a shared range. Keep
                // PENDING held while dropping our reader lock so no new stock
                // SQLite reader can enter before the exclusive attempt.
                unlock_stock_sqlite_range(
                    &self.main_file,
                    STOCK_SQLITE_SHARED_FIRST,
                    STOCK_SQLITE_SHARED_SIZE,
                )?;
                self.shared_range = false;
                if let Err(lock_error) = try_lock_stock_sqlite_range(
                    &self.main_file,
                    STOCK_SQLITE_SHARED_FIRST,
                    STOCK_SQLITE_SHARED_SIZE,
                ) {
                    let restore_result = try_lock_stock_sqlite_shared_range(
                        &self.main_file,
                        STOCK_SQLITE_SHARED_FIRST,
                        STOCK_SQLITE_SHARED_SIZE,
                    );
                    if restore_result.is_ok() {
                        self.shared_range = true;
                    }
                    return match restore_result {
                        Ok(()) => Err(lock_error),
                        Err(restore_error) => Err(FrankenError::internal(format!(
                            "stock SQLite Windows EXCLUSIVE acquisition failed and could not restore SHARED: lock={lock_error}; restore={restore_error}"
                        ))),
                    };
                }
                self.shared_range = true;
                self.shared_range_exclusive = true;
            }
        }
        self.lock_level = level;
        Ok(())
    }

    fn highest_held_level(&self) -> LockLevel {
        // Only a complete SQLite lock prefix is a reusable level. A failed
        // promotion/unwind can transiently leave an upper byte held without
        // the SHARED range beneath it; reporting that partial state as
        // RESERVED/PENDING would let a later SHARED request become a false
        // no-op. The caller drops this dedicated handle whenever the requested
        // prefix could not be reconstructed.
        if self.shared_range && self.shared_range_exclusive && self.reserved && self.pending {
            LockLevel::Exclusive
        } else if self.shared_range && self.reserved && self.pending {
            LockLevel::Pending
        } else if self.shared_range && self.reserved {
            LockLevel::Reserved
        } else if self.shared_range {
            LockLevel::Shared
        } else {
            LockLevel::None
        }
    }

    fn recompute_lock_level(&mut self) {
        self.lock_level = self.highest_held_level();
    }

    fn unlock_to(&mut self, level: LockLevel) -> Result<()> {
        let mut failures = Vec::new();

        if self.shared_range_exclusive && level < LockLevel::Exclusive {
            match unlock_stock_sqlite_range(
                &self.main_file,
                STOCK_SQLITE_SHARED_FIRST,
                STOCK_SQLITE_SHARED_SIZE,
            ) {
                Ok(()) => {
                    self.shared_range = false;
                    self.shared_range_exclusive = false;
                    if level >= LockLevel::Shared {
                        match try_lock_stock_sqlite_shared_range(
                            &self.main_file,
                            STOCK_SQLITE_SHARED_FIRST,
                            STOCK_SQLITE_SHARED_SIZE,
                        ) {
                            Ok(()) => self.shared_range = true,
                            Err(error) => {
                                failures.push(format!("restore main shared range: {error}"));
                            }
                        }
                    }
                }
                Err(error) => failures.push(format!("main exclusive range: {error}")),
            }
        }

        if self.pending && level < LockLevel::Pending {
            match unlock_stock_sqlite_range(&self.main_file, STOCK_SQLITE_PENDING_BYTE, 1) {
                Ok(()) => self.pending = false,
                Err(error) => failures.push(format!("main pending byte: {error}")),
            }
        }
        if self.reserved && level < LockLevel::Reserved {
            match unlock_stock_sqlite_range(&self.main_file, STOCK_SQLITE_RESERVED_BYTE, 1) {
                Ok(()) => self.reserved = false,
                Err(error) => failures.push(format!("main reserved byte: {error}")),
            }
        }
        if self.shared_range && level < LockLevel::Shared {
            match unlock_stock_sqlite_range(
                &self.main_file,
                STOCK_SQLITE_SHARED_FIRST,
                STOCK_SQLITE_SHARED_SIZE,
            ) {
                Ok(()) => self.shared_range = false,
                Err(error) => failures.push(format!("main shared range: {error}")),
            }
        }

        self.recompute_lock_level();
        if failures.is_empty() && self.lock_level != level {
            failures.push(format!(
                "requested {level:?} but only a partial/non-prefix stock lock state remained ({:?})",
                self.lock_level
            ));
        }
        if failures.is_empty() {
            Ok(())
        } else {
            Err(FrankenError::internal(format!(
                "could not release stock SQLite Windows ordinary lock ranges: {}",
                failures.join("; ")
            )))
        }
    }
}

/// Stock-SQLite-visible WAL portion of an external maintenance fence.
///
/// Ordinary main-file locking already mirrors every level onto stock SQLite's
/// real database bytes. Whole-image maintenance additionally retains the real
/// WAL write/checkpoint slots tracked here.
#[derive(Debug)]
struct WindowsExternalMaintenanceLocks {
    wal_mode: bool,
    shm_file: Option<File>,
    shm_write: bool,
    shm_checkpoint: bool,
}

impl WindowsExternalMaintenanceLocks {
    const fn new(wal_mode: bool) -> Self {
        Self {
            wal_mode,
            shm_file: None,
            shm_write: false,
            shm_checkpoint: false,
        }
    }

    /// Release every acquired range in strict reverse order. A failed explicit
    /// unlock leaves its bit set until this state is dropped; closing the
    /// dedicated handles is Win32's guaranteed final release path.
    fn release(&mut self) -> Result<()> {
        let mut failures = Vec::new();

        if self.shm_checkpoint {
            match self.shm_file.as_ref() {
                Some(shm_file) => {
                    match unlock_stock_sqlite_range(shm_file, STOCK_SQLITE_WAL_CKPT_BYTE, 1) {
                        Ok(()) => self.shm_checkpoint = false,
                        Err(error) => failures.push(format!("WAL checkpoint byte: {error}")),
                    }
                }
                None => failures.push("WAL checkpoint byte: missing -shm handle".to_owned()),
            }
        }
        if self.shm_write {
            match self.shm_file.as_ref() {
                Some(shm_file) => {
                    match unlock_stock_sqlite_range(shm_file, STOCK_SQLITE_WAL_WRITE_BYTE, 1) {
                        Ok(()) => self.shm_write = false,
                        Err(error) => failures.push(format!("WAL write byte: {error}")),
                    }
                }
                None => failures.push("WAL write byte: missing -shm handle".to_owned()),
            }
        }

        if !self.shm_write && !self.shm_checkpoint {
            self.shm_file.take();
        }

        if failures.is_empty() {
            Ok(())
        } else {
            Err(FrankenError::internal(format!(
                "could not release stock SQLite Windows maintenance ranges: {}",
                failures.join("; ")
            )))
        }
    }
}

impl Vfs for WindowsVfs {
    type File = WindowsFile;

    fn name(&self) -> &'static str {
        "windows"
    }

    #[allow(clippy::significant_drop_tightening)]
    fn open(
        &self,
        cx: &Cx,
        path: Option<&Path>,
        flags: VfsOpenFlags,
    ) -> Result<(Self::File, VfsOpenFlags)> {
        checkpoint_or_abort(cx)?;

        let is_temp = path.is_none();
        let mut resolved = if let Some(path) = path {
            resolve_path(path)?
        } else {
            self.next_temp_path()?
        };

        let is_create = path.is_none() || flags.contains(VfsOpenFlags::CREATE);
        let is_rw = path.is_none() || flags.contains(VfsOpenFlags::READWRITE) || is_create;
        let is_exclusive_create = is_create && flags.contains(VfsOpenFlags::EXCLUSIVE);

        if !is_create && !resolved.exists() {
            return Err(FrankenError::CannotOpen { path: resolved });
        }

        let mut created_db_file = false;
        let file = loop {
            let mut options = windows_open_options();
            options.read(true);
            if is_rw {
                options.write(true);
            }
            if is_create {
                options.create_new(true);
            }

            match options.open(&resolved) {
                Ok(file) => {
                    created_db_file = is_create;
                    break file;
                }
                Err(err) if is_temp && err.kind() == std::io::ErrorKind::AlreadyExists => {
                    resolved = self.next_temp_path()?;
                }
                Err(err)
                    if is_create
                        && !is_temp
                        && !is_exclusive_create
                        && err.kind() == std::io::ErrorKind::AlreadyExists =>
                {
                    let mut open_options = windows_open_options();
                    open_options.read(true);
                    if is_rw {
                        open_options.write(true);
                    }
                    match open_options.open(&resolved) {
                        Ok(file) => break file,
                        Err(err) if err.kind() == std::io::ErrorKind::NotFound => {}
                        Err(err) => return Err(FrankenError::Io(err)),
                    }
                }
                Err(err) => {
                    return Err(if err.kind() == std::io::ErrorKind::NotFound {
                        FrankenError::CannotOpen { path: resolved }
                    } else {
                        FrankenError::Io(err)
                    });
                }
            }
        };

        let owner_id = next_owner_id();
        let shm_path = sqlite_shm_path(&resolved);

        let delete_on_close = flags.contains(VfsOpenFlags::DELETEONCLOSE) || is_temp;
        let out_flags = if is_create {
            flags | VfsOpenFlags::READWRITE
        } else {
            flags
        };
        let os_locks = match WindowsOsLockFiles::open(&resolved) {
            Ok(os_locks) => os_locks,
            Err(err) => {
                drop(file);
                if created_db_file {
                    let _ = fs::remove_file(&resolved);
                }
                return Err(err);
            }
        };

        Ok((
            WindowsFile {
                path: resolved,
                file: Some(file),
                os_locks: Some(os_locks),
                stock_main_locks: None,
                external_shared_snapshot_prior_level: None,
                external_maintenance_locks: None,
                owner_id,
                lock_level: LockLevel::None,
                delete_on_close,
                shm_path,
                shm_state: None,
            },
            out_flags,
        ))
    }

    fn open_with_expected_identity(
        &self,
        cx: &Cx,
        path: &Path,
        flags: VfsOpenFlags,
        expected_identity: FileIdentity,
    ) -> Result<(Self::File, VfsOpenFlags)> {
        checkpoint_or_abort(cx)?;
        let resolved = resolve_path(path)?;

        // Query through a read-only handle before `open` reaches
        // `WindowsOsLockFiles::open`, which creates the advisory sidecars.
        // Our share mode deliberately omits FILE_SHARE_DELETE, so retaining
        // this guard also prevents a pathname replacement between the
        // preflight and the final read-write handle verification.
        let mut options = windows_open_options();
        let identity_guard = options.read(true).open(&resolved).map_err(|err| {
            if err.kind() == std::io::ErrorKind::NotFound {
                FrankenError::CannotOpen {
                    path: resolved.clone(),
                }
            } else {
                FrankenError::Io(err)
            }
        })?;
        if FileIdentity::from_file(&identity_guard)? != Some(expected_identity) {
            return Err(FrankenError::CannotOpen { path: resolved });
        }

        let mut existing_flags = flags;
        existing_flags.remove(VfsOpenFlags::CREATE | VfsOpenFlags::EXCLUSIVE);
        let (file, actual_flags) = self.open(cx, Some(&resolved), existing_flags)?;
        if file.file_identity()? != Some(expected_identity) {
            return Err(FrankenError::CannotOpen { path: resolved });
        }
        drop(identity_guard);
        Ok((file, actual_flags))
    }

    fn open_reserved_with_expected_identity(
        &self,
        cx: &Cx,
        path: &Path,
        flags: VfsOpenFlags,
        expected_identity: FileIdentity,
    ) -> Result<(Self::File, VfsOpenFlags)> {
        checkpoint_or_abort(cx)?;
        let resolved = resolve_path(path)?;
        let mut existing_flags = flags;
        existing_flags.remove(VfsOpenFlags::CREATE | VfsOpenFlags::EXCLUSIVE);

        // Open the final main-database handle directly. Ordinary `open`
        // cannot be used here because it creates the advisory lock sidecars
        // before returning the handle to its caller.
        let mut options = windows_open_options();
        options.read(true);
        if existing_flags.contains(VfsOpenFlags::READWRITE) {
            options.write(true);
        }
        let file = options.open(&resolved).map_err(|err| {
            if err.kind() == std::io::ErrorKind::NotFound {
                FrankenError::CannotOpen {
                    path: resolved.clone(),
                }
            } else {
                FrankenError::Io(err)
            }
        })?;
        if FileIdentity::from_file(&file)? != Some(expected_identity) || file.metadata()?.len() != 0
        {
            return Err(FrankenError::CannotOpen { path: resolved });
        }
        for artifact_path in reserved_database_artifact_paths(&resolved) {
            if filesystem_entry_exists(&artifact_path)? {
                return Err(FrankenError::CannotOpen { path: resolved });
            }
        }

        // Only an accepted reservation may create the Windows advisory-lock
        // sidecars. The live main handle above omits FILE_SHARE_DELETE, so the
        // pathname cannot be replaced between verification and construction.
        let os_locks = WindowsOsLockFiles::open(&resolved)?;
        let owner_id = next_owner_id();
        let shm_path = sqlite_shm_path(&resolved);
        let delete_on_close = existing_flags.contains(VfsOpenFlags::DELETEONCLOSE);
        Ok((
            WindowsFile {
                path: resolved,
                file: Some(file),
                os_locks: Some(os_locks),
                stock_main_locks: None,
                external_shared_snapshot_prior_level: None,
                external_maintenance_locks: None,
                owner_id,
                lock_level: LockLevel::None,
                delete_on_close,
                shm_path,
                shm_state: None,
            },
            existing_flags,
        ))
    }

    fn delete(&self, cx: &Cx, path: &Path, sync_dir: bool) -> Result<()> {
        let resolved = resolve_path(path)?;
        if resolved.exists() {
            fs::remove_file(&resolved)?;
        }
        let shm_path = sqlite_shm_path(&resolved);
        if shm_path.exists() {
            fs::remove_file(shm_path)?;
        }
        try_remove_windows_lock_sidecars(&resolved);
        if sync_dir {
            self.sync_parent_directory(cx, &resolved)?;
        }
        Ok(())
    }

    fn sync_parent_directory(&self, _cx: &Cx, _path: &Path) -> Result<()> {
        // Win32 does not expose a portable fsync-directory operation. SQLite's
        // own Windows VFS establishes journal create/delete durability through
        // FlushFileBuffers on the journal handle, which `WindowsFile::sync_all`
        // performs before this hook is reached. Keep the namespace hook an
        // explicit no-op rather than attempting FlushFileBuffers on a directory
        // handle (unsupported on common filesystems and liable to return
        // ERROR_INVALID_HANDLE).
        Ok(())
    }

    fn access(&self, _cx: &Cx, path: &Path, flags: AccessFlags) -> Result<bool> {
        let resolved = resolve_path(path)?;
        if !resolved.exists() {
            return Ok(false);
        }
        match flags {
            f if f == AccessFlags::EXISTS => Ok(true),
            f if f == AccessFlags::READ => {
                let mut options = windows_open_options();
                Ok(options.read(true).open(resolved).is_ok())
            }
            _ => {
                let mut options = windows_open_options();
                Ok(options.read(true).write(true).open(resolved).is_ok())
            }
        }
    }

    fn path_entry_exists(&self, _cx: &Cx, path: &Path) -> Result<bool> {
        filesystem_entry_exists(&resolve_path(path)?)
    }

    fn full_pathname(&self, _cx: &Cx, path: &Path) -> Result<PathBuf> {
        stable_full_path(path)
    }
}

/// A file handle opened by [`WindowsVfs`].
#[derive(Debug)]
pub struct WindowsFile {
    path: PathBuf,
    file: Option<File>,
    os_locks: Option<WindowsOsLockFiles>,
    stock_main_locks: Option<WindowsStockMainLocks>,
    external_shared_snapshot_prior_level: Option<LockLevel>,
    external_maintenance_locks: Option<WindowsExternalMaintenanceLocks>,
    owner_id: u64,
    lock_level: LockLevel,
    delete_on_close: bool,
    shm_path: PathBuf,
    shm_state: Option<Arc<Mutex<WindowsShmState>>>,
}

impl WindowsFile {
    fn is_closed(&self) -> bool {
        self.file.is_none() && self.os_locks.is_none()
    }

    fn ensure_open(&self) -> Result<()> {
        if self.is_closed() {
            Err(FrankenError::internal("windows file is closed"))
        } else {
            Ok(())
        }
    }

    fn file_ref(&self) -> Result<&File> {
        self.file
            .as_ref()
            .ok_or_else(|| FrankenError::internal("windows file is closed"))
    }

    fn file_mut(&mut self) -> Result<&mut File> {
        self.file
            .as_mut()
            .ok_or_else(|| FrankenError::internal("windows file is closed"))
    }

    fn os_locks_ref(&self) -> Result<&WindowsOsLockFiles> {
        self.os_locks
            .as_ref()
            .ok_or_else(|| FrankenError::internal("windows lock files are closed"))
    }

    fn os_locks_mut(&mut self) -> Result<&mut WindowsOsLockFiles> {
        self.os_locks
            .as_mut()
            .ok_or_else(|| FrankenError::internal("windows lock files are closed"))
    }

    fn stock_main_locks_mut(&mut self) -> Result<&mut WindowsStockMainLocks> {
        if self.stock_main_locks.is_none() {
            self.stock_main_locks = Some(WindowsStockMainLocks::new(self.file_ref()?.try_clone()?));
        }
        self.stock_main_locks
            .as_mut()
            .ok_or_else(|| FrankenError::internal("windows stock main lock handle is closed"))
    }

    fn ensure_shm_state(&mut self) -> Result<Arc<Mutex<WindowsShmState>>> {
        if let Some(state) = &self.shm_state {
            return Ok(Arc::clone(state));
        }
        let state =
            windows_shm_table().get_or_create_and_register(&self.shm_path, self.owner_id)?;
        self.shm_state = Some(Arc::clone(&state));
        Ok(state)
    }

    fn release_shm_owner_state(&mut self, delete: bool) -> Result<()> {
        let Some(state_arc) = self.shm_state.take() else {
            if delete {
                drop(fs::remove_file(&self.shm_path));
            }
            return Ok(());
        };

        let orphaned = {
            let mut state = state_arc
                .lock()
                .map_err(|_| lock_poisoned("windows shm state"))?;

            for slot in &mut state.slots {
                slot.shared_holders.remove(&self.owner_id);
                if slot.exclusive_owner == Some(self.owner_id) {
                    slot.exclusive_owner = None;
                }
            }

            if let Some(count) = state.owner_refs.get_mut(&self.owner_id) {
                if *count > 1 {
                    *count -= 1;
                } else {
                    state.owner_refs.remove(&self.owner_id);
                }
            }
            state.owner_refs.is_empty()
        };

        if orphaned {
            windows_shm_table().remove_if_orphaned(&self.shm_path, &state_arc)?;
        }

        if delete {
            drop(fs::remove_file(&self.shm_path));
        }

        Ok(())
    }

    fn rollback_ordinary_locks_to(&mut self, level: LockLevel) -> Result<()> {
        let stock_result = self
            .stock_main_locks
            .as_mut()
            .map_or(Ok(()), |locks| locks.unlock_to(level));
        if stock_result.is_err() {
            // A partial UnlockFileEx failure can leave a non-prefix subset of
            // SQLite lock levels (for example RESERVED without SHARED). Drop
            // the dedicated handle before returning the error: CloseHandle is
            // the synchronous final release and the next attempt must rebuild
            // its stock-visible state from NONE rather than trust a stale or
            // structurally incomplete lock ladder.
            drop(self.stock_main_locks.take());
        }
        let cooperative_result = self.os_locks_mut()?.unlock_to(level);
        self.lock_level = std::cmp::min(
            self.stock_main_locks
                .as_ref()
                .map_or(LockLevel::None, |locks| locks.lock_level),
            self.os_locks_ref()?.highest_held_level(),
        );
        match (stock_result, cooperative_result) {
            (Ok(()), Ok(())) => Ok(()),
            (Err(error), Ok(())) | (Ok(()), Err(error)) => Err(error),
            (Err(stock_error), Err(cooperative_error)) => Err(FrankenError::internal(format!(
                "Windows ordinary lock rollback failed on both lock surfaces: stock={stock_error}; cooperative={cooperative_error}"
            ))),
        }
    }

    fn acquire_cooperative_wal_maintenance_locks(&mut self, cx: &Cx, wal_mode: bool) -> Result<()> {
        if wal_mode {
            self.shm_lock(
                cx,
                WAL_WRITE_LOCK,
                WAL_CKPT_LOCK - WAL_WRITE_LOCK + 1,
                SQLITE_SHM_LOCK | SQLITE_SHM_EXCLUSIVE,
            )?;
        }
        Ok(())
    }

    fn release_cooperative_wal_maintenance_locks(&mut self, cx: &Cx, wal_mode: bool) -> Result<()> {
        if wal_mode {
            self.shm_lock(
                cx,
                WAL_WRITE_LOCK,
                WAL_CKPT_LOCK - WAL_WRITE_LOCK + 1,
                SQLITE_SHM_UNLOCK | SQLITE_SHM_EXCLUSIVE,
            )
        } else {
            Ok(())
        }
    }

    fn acquire_stock_sqlite_maintenance_locks(&mut self, wal_mode: bool) -> Result<()> {
        if self.external_maintenance_locks.is_some() {
            return Err(FrankenError::internal(
                "stock SQLite Windows maintenance fence is already held",
            ));
        }
        self.ensure_open()?;
        let mut locks = WindowsExternalMaintenanceLocks::new(wal_mode);

        let acquisition_result = (|| -> Result<()> {
            if wal_mode {
                let (shm_file, _) = open_windows_lock_sidecar(&self.shm_path)?;
                locks.shm_file = Some(shm_file);
                let shm_file = locks.shm_file.as_ref().ok_or_else(|| {
                    FrankenError::internal("Windows maintenance lost its -shm handle")
                })?;
                try_lock_stock_sqlite_range(shm_file, STOCK_SQLITE_WAL_WRITE_BYTE, 1)?;
                locks.shm_write = true;
                try_lock_stock_sqlite_range(shm_file, STOCK_SQLITE_WAL_CKPT_BYTE, 1)?;
                locks.shm_checkpoint = true;
            }

            // The ordinary main-file lock path mirrors every SQLite lock level
            // onto the real database bytes. This dedicated state intentionally
            // acquires only the stock-visible WAL slots; the caller acquires
            // main EXCLUSIVE afterward to preserve WAL-then-main ordering.
            Ok(())
        })();

        if let Err(acquisition_error) = acquisition_result {
            let cleanup_result = locks.release();
            // Even if an explicit UnlockFileEx failed, dropping these
            // dedicated handles releases every range before CloseHandle
            // returns. Never strand a partial acquisition on `self.file`.
            drop(locks);
            return match cleanup_result {
                Ok(()) => Err(acquisition_error),
                Err(cleanup_error) => Err(FrankenError::internal(format!(
                    "stock SQLite Windows maintenance lock failed and partial-acquisition unwind also failed: lock={acquisition_error}; unwind={cleanup_error}"
                ))),
            };
        }

        self.external_maintenance_locks = Some(locks);
        Ok(())
    }

    fn release_stock_sqlite_maintenance_locks(&mut self) -> Result<()> {
        let Some(mut locks) = self.external_maintenance_locks.take() else {
            return Ok(());
        };
        let release_result = locks.release();
        // See the acquisition unwind above: handle closure is the final,
        // synchronous lock-release fallback for any range whose explicit
        // UnlockFileEx call failed.
        drop(locks);
        release_result
    }

    fn validate_shm_request(offset: u32, n: u32) -> Result<u32> {
        if n == 0 {
            return Err(FrankenError::LockFailed {
                detail: "shm_lock called with n=0".to_string(),
            });
        }
        let end = offset
            .checked_add(n)
            .ok_or_else(|| FrankenError::LockFailed {
                detail: "shm_lock range overflow".to_string(),
            })?;
        if end > WAL_TOTAL_LOCKS {
            return Err(FrankenError::LockFailed {
                detail: format!("shm_lock range {offset}..{end} exceeds WAL lock table"),
            });
        }
        Ok(end)
    }

    fn acquire_shared_slot(state: &mut WindowsShmState, slot: u32, owner_id: u64) -> Result<()> {
        let idx = to_slot_index(slot)?;
        let slot_state = state
            .slots
            .get_mut(idx)
            .ok_or_else(|| FrankenError::internal("shm slot index out of bounds"))?;
        if let Some(exclusive_owner) = slot_state.exclusive_owner {
            if exclusive_owner != owner_id {
                return Err(FrankenError::Busy);
            }
        }
        *slot_state.shared_holders.entry(owner_id).or_insert(0) += 1;
        Ok(())
    }

    fn acquire_exclusive_slot(state: &mut WindowsShmState, slot: u32, owner_id: u64) -> Result<()> {
        let idx = to_slot_index(slot)?;
        let slot_state = state
            .slots
            .get_mut(idx)
            .ok_or_else(|| FrankenError::internal("shm slot index out of bounds"))?;

        if slot_state.exclusive_owner == Some(owner_id) {
            return Ok(());
        }

        if slot_state.exclusive_owner.is_some() {
            return Err(FrankenError::Busy);
        }

        if slot_state
            .shared_holders
            .iter()
            .any(|(owner, count)| *owner != owner_id && *count > 0)
        {
            return Err(FrankenError::Busy);
        }

        slot_state.exclusive_owner = Some(owner_id);
        Ok(())
    }

    fn release_shared_slot(state: &mut WindowsShmState, slot: u32, owner_id: u64) -> Result<()> {
        let idx = to_slot_index(slot)?;
        let slot_state = state
            .slots
            .get_mut(idx)
            .ok_or_else(|| FrankenError::internal("shm slot index out of bounds"))?;
        let Some(holder_count) = slot_state.shared_holders.get_mut(&owner_id) else {
            return Err(FrankenError::LockFailed {
                detail: format!("owner {owner_id} does not hold shared SHM slot {slot}"),
            });
        };
        if *holder_count > 1 {
            *holder_count -= 1;
        } else {
            slot_state.shared_holders.remove(&owner_id);
        }
        Ok(())
    }

    fn release_exclusive_slot(state: &mut WindowsShmState, slot: u32, owner_id: u64) -> Result<()> {
        let idx = to_slot_index(slot)?;
        let slot_state = state
            .slots
            .get_mut(idx)
            .ok_or_else(|| FrankenError::internal("shm slot index out of bounds"))?;
        if slot_state.exclusive_owner != Some(owner_id) {
            return Err(FrankenError::LockFailed {
                detail: format!("owner {owner_id} does not hold exclusive SHM slot {slot}"),
            });
        }
        slot_state.exclusive_owner = None;
        Ok(())
    }
}

impl VfsFile for WindowsFile {
    fn close(&mut self, cx: &Cx) -> Result<()> {
        if self.is_closed() && self.shm_state.is_none() {
            return Ok(());
        }

        let mut first_error = if self.external_shared_snapshot_prior_level.is_some() {
            self.unlock_external_shared_snapshot(cx).err()
        } else {
            None
        };
        let external_wal_mode = self
            .external_maintenance_locks
            .as_ref()
            .map(|locks| locks.wal_mode);
        if let Some(wal_mode) = external_wal_mode
            && let Err(error) = self.unlock_external_maintenance(cx, wal_mode)
            && first_error.is_none()
        {
            first_error = Some(error);
        }

        if !self.is_closed() {
            if let Err(err) = self.unlock(cx, LockLevel::None) {
                if first_error.is_none() {
                    first_error = Some(err);
                }
            }
        }

        let release_result = if self.shm_state.is_some() || self.delete_on_close {
            self.release_shm_owner_state(self.delete_on_close)
        } else {
            Ok(())
        };
        if first_error.is_none() {
            first_error = release_result.err();
        }

        // Closing the underlying handles is the final Win32 lock-release
        // fallback even if an explicit UnlockFileEx call above failed.
        drop(self.stock_main_locks.take());
        self.external_shared_snapshot_prior_level = None;
        drop(self.external_maintenance_locks.take());
        drop(self.os_locks.take());
        drop(self.file.take());
        self.lock_level = LockLevel::None;

        if self.delete_on_close {
            drop(fs::remove_file(&self.path));
            try_remove_windows_lock_sidecars(&self.path);
        }

        first_error.map_or(Ok(()), Err)
    }

    fn file_identity(&self) -> Result<Option<FileIdentity>> {
        Ok(FileIdentity::from_file(self.file_ref()?)?)
    }

    fn read(&self, cx: &Cx, buf: &mut [u8], offset: u64) -> Result<usize> {
        checkpoint_or_abort(cx)?;
        let mut total = 0_usize;
        while total < buf.len() {
            let read_offset = offset
                .checked_add(u64::try_from(total).map_err(|_| FrankenError::OutOfRange {
                    what: "read offset".to_string(),
                    value: total.to_string(),
                })?)
                .ok_or_else(|| FrankenError::OutOfRange {
                    what: "read offset".to_string(),
                    value: "overflow".to_string(),
                })?;
            let n = self.file_ref()?.seek_read(&mut buf[total..], read_offset)?;
            if n == 0 {
                break;
            }
            total += n;
        }
        if total < buf.len() {
            buf[total..].fill(0);
        }
        Ok(total)
    }

    fn write(&mut self, cx: &Cx, buf: &[u8], offset: u64) -> Result<()> {
        checkpoint_or_abort(cx)?;
        let mut total = 0_usize;
        while total < buf.len() {
            let write_offset = offset
                .checked_add(u64::try_from(total).map_err(|_| FrankenError::OutOfRange {
                    what: "write offset".to_string(),
                    value: total.to_string(),
                })?)
                .ok_or_else(|| FrankenError::OutOfRange {
                    what: "write offset".to_string(),
                    value: "overflow".to_string(),
                })?;
            let n = self.file_mut()?.seek_write(&buf[total..], write_offset)?;
            if n == 0 {
                return Err(FrankenError::Io(std::io::Error::new(
                    std::io::ErrorKind::WriteZero,
                    "seek_write returned 0",
                )));
            }
            total += n;
        }
        Ok(())
    }

    fn truncate(&mut self, _cx: &Cx, size: u64) -> Result<()> {
        self.file_mut()?.set_len(size)?;
        Ok(())
    }

    fn sync(&mut self, _cx: &Cx, flags: SyncFlags) -> Result<()> {
        if flags.contains(SyncFlags::DATAONLY) {
            self.file_mut()?.sync_data()?;
        } else {
            self.file_mut()?.sync_all()?;
        }
        Ok(())
    }

    fn file_size(&self, _cx: &Cx) -> Result<u64> {
        Ok(self.file_ref()?.metadata()?.len())
    }

    fn lock(&mut self, _cx: &Cx, level: LockLevel) -> Result<()> {
        if level <= self.lock_level {
            return Ok(());
        }

        let prior_level = self.lock_level;
        while self.lock_level < level {
            let next = next_lock_level(self.lock_level)
                .ok_or_else(|| FrankenError::internal("invalid lock escalation"))?;
            if let Err(lock_error) = self.os_locks_mut()?.try_lock_level(next) {
                let rollback_result = self.rollback_ordinary_locks_to(prior_level);
                return match rollback_result {
                    Ok(()) => Err(lock_error),
                    Err(rollback_error) => Err(FrankenError::internal(format!(
                        "Windows cooperative lock acquisition failed and rollback also failed: lock={lock_error}; rollback={rollback_error}"
                    ))),
                };
            }
            if let Err(lock_error) = self.stock_main_locks_mut()?.try_lock_level(next) {
                let rollback_result = self.rollback_ordinary_locks_to(prior_level);
                return match rollback_result {
                    Ok(()) => Err(lock_error),
                    Err(rollback_error) => Err(FrankenError::internal(format!(
                        "Windows stock-visible lock acquisition failed and rollback also failed: lock={lock_error}; rollback={rollback_error}"
                    ))),
                };
            }
            self.lock_level = next;
        }
        Ok(())
    }

    fn unlock(&mut self, _cx: &Cx, level: LockLevel) -> Result<()> {
        if level >= self.lock_level {
            return Ok(());
        }
        self.rollback_ordinary_locks_to(level)
    }

    fn lock_external_shared_snapshot(&mut self, cx: &Cx) -> Result<()> {
        if self.external_shared_snapshot_prior_level.is_some() {
            return Err(FrankenError::internal(
                "Windows external shared-snapshot fence is already held",
            ));
        }
        if self.external_maintenance_locks.is_some() {
            return Err(FrankenError::internal(
                "cannot acquire a Windows shared-snapshot fence during external maintenance",
            ));
        }

        let prior_level = self.lock_level;
        self.lock(cx, LockLevel::Shared)?;
        self.external_shared_snapshot_prior_level = Some(prior_level);
        Ok(())
    }

    fn unlock_external_shared_snapshot(&mut self, cx: &Cx) -> Result<()> {
        let Some(prior_level) = self.external_shared_snapshot_prior_level.take() else {
            return Ok(());
        };
        self.unlock(cx, prior_level)
    }

    fn lock_external_maintenance(&mut self, cx: &Cx, wal_mode: bool) -> Result<()> {
        if self.external_shared_snapshot_prior_level.is_some() {
            return Err(FrankenError::internal(
                "cannot acquire Windows external maintenance while a shared-snapshot fence is held",
            ));
        }
        if self.external_maintenance_locks.is_some() {
            return Err(FrankenError::internal(
                "Windows external maintenance fence is already held",
            ));
        }
        // Every participant uses the same deadlock-free order: WAL
        // writer/checkpointer slots first, then main-file EXCLUSIVE. Acquire
        // the cooperative slots before the matching real `-shm` bytes so a
        // FrankenSQLite peer cannot enter between the two surfaces.
        self.acquire_cooperative_wal_maintenance_locks(cx, wal_mode)?;
        if let Err(stock_error) = self.acquire_stock_sqlite_maintenance_locks(wal_mode) {
            let cooperative_cleanup = self.release_cooperative_wal_maintenance_locks(cx, wal_mode);
            return match cooperative_cleanup {
                Ok(()) => Err(stock_error),
                Err(cleanup_error) => Err(FrankenError::internal(format!(
                    "Windows external maintenance fence failed and cooperative cleanup also failed: stock={stock_error}; cleanup={cleanup_error}"
                ))),
            };
        }
        if let Err(main_error) = self.lock(cx, LockLevel::Exclusive) {
            let stock_cleanup = self.release_stock_sqlite_maintenance_locks();
            let cooperative_cleanup = self.release_cooperative_wal_maintenance_locks(cx, wal_mode);
            return match (stock_cleanup, cooperative_cleanup) {
                (Ok(()), Ok(())) => Err(main_error),
                (stock_result, cooperative_result) => Err(FrankenError::internal(format!(
                    "Windows external maintenance could not acquire main EXCLUSIVE or release its WAL fences: main={main_error}; stock_cleanup={stock_result:?}; cooperative_cleanup={cooperative_result:?}"
                ))),
            };
        }
        Ok(())
    }

    fn unlock_external_maintenance(&mut self, cx: &Cx, wal_mode: bool) -> Result<()> {
        let actual_wal_mode = self
            .external_maintenance_locks
            .as_ref()
            .map_or(wal_mode, |locks| locks.wal_mode);
        let mode_mismatch =
            self.external_maintenance_locks.is_some() && actual_wal_mode != wal_mode;
        // Release in the exact reverse of acquisition: main, real WAL bytes,
        // then the in-process/cooperative WAL slots.
        let main_result = self.unlock(cx, LockLevel::None);
        let stock_result = self.release_stock_sqlite_maintenance_locks();
        let cooperative_result =
            self.release_cooperative_wal_maintenance_locks(cx, actual_wal_mode);

        let release_result = if main_result.is_ok()
            && stock_result.is_ok()
            && cooperative_result.is_ok()
        {
            Ok(())
        } else {
            Err(FrankenError::internal(format!(
                "Windows external maintenance could not release every lock: main={main_result:?}; stock={stock_result:?}; cooperative={cooperative_result:?}"
            )))
        };
        if mode_mismatch {
            return match release_result {
                Ok(()) => Err(FrankenError::internal(format!(
                    "Windows external maintenance unlock mode mismatch: acquired wal_mode={actual_wal_mode}, requested wal_mode={wal_mode}"
                ))),
                Err(release_error) => Err(FrankenError::internal(format!(
                    "Windows external maintenance unlock mode mismatch and cleanup failed: acquired wal_mode={actual_wal_mode}, requested wal_mode={wal_mode}; cleanup={release_error}"
                ))),
            };
        }
        release_result
    }

    fn check_reserved_lock(&self, _cx: &Cx) -> Result<bool> {
        if self.os_locks_ref()?.reserved_locked_by_other()? {
            return Ok(true);
        }
        if self.lock_level >= LockLevel::Reserved {
            return Ok(false);
        }

        let probe = self.file_ref()?.try_clone()?;
        match try_lock_stock_sqlite_range(&probe, STOCK_SQLITE_RESERVED_BYTE, 1) {
            Ok(()) => {
                unlock_stock_sqlite_range(&probe, STOCK_SQLITE_RESERVED_BYTE, 1)?;
                Ok(false)
            }
            Err(FrankenError::Busy) => Ok(true),
            Err(error) => Err(error),
        }
    }

    fn sector_size(&self) -> u32 {
        4096
    }

    fn device_characteristics(&self) -> u32 {
        SQLITE_IOCAP_UNDELETABLE_WHEN_OPEN
    }

    #[allow(clippy::significant_drop_tightening)]
    fn shm_map(&mut self, _cx: &Cx, region: u32, size: u32, extend: bool) -> Result<ShmRegion> {
        self.ensure_open()?;
        if size == 0 {
            return Err(FrankenError::LockFailed {
                detail: "shm_map size must be > 0".to_string(),
            });
        }

        let size_usize = usize::try_from(size).map_err(|_| FrankenError::OutOfRange {
            what: "shm region size".to_string(),
            value: size.to_string(),
        })?;

        let min_len = u64::from(region)
            .checked_add(1)
            .and_then(|value| value.checked_mul(u64::from(size)))
            .ok_or_else(|| FrankenError::OutOfRange {
                what: "shm file length".to_string(),
                value: format!("region={region}, size={size}"),
            })?;

        if !extend {
            let (shm_state, mapped_region) = if let Some(state) = &self.shm_state {
                let shm_state = Arc::clone(state);
                let mapped_region = {
                    let state = shm_state
                        .lock()
                        .map_err(|_| lock_poisoned("windows shm state"))?;
                    let existing = state
                        .regions
                        .get(&region)
                        .map(ShmRegion::share)
                        .ok_or_else(|| FrankenError::CannotOpen {
                            path: self.shm_path.clone(),
                        })?;
                    if existing.len() < size_usize {
                        return Err(FrankenError::LockFailed {
                            detail: format!(
                                "shm region {region} is {} bytes, requested {size_usize} bytes without extend",
                                existing.len()
                            ),
                        });
                    }
                    existing
                };
                (shm_state, mapped_region)
            } else {
                windows_shm_table()
                    .get_existing_and_register_with(
                        &self.shm_path,
                        self.owner_id,
                        || {},
                        |state| {
                            let existing = state
                                .regions
                                .get(&region)
                                .map(ShmRegion::share)
                                .ok_or_else(|| FrankenError::CannotOpen {
                                    path: self.shm_path.clone(),
                                })?;
                            if existing.len() < size_usize {
                                return Err(FrankenError::LockFailed {
                                    detail: format!(
                                        "shm region {region} is {} bytes, requested {size_usize} bytes without extend",
                                        existing.len()
                                    ),
                                });
                            }
                            Ok(existing)
                        },
                    )?
                    .ok_or_else(|| FrankenError::CannotOpen {
                        path: self.shm_path.clone(),
                    })?
            };

            if self.shm_state.is_none() {
                self.shm_state = Some(Arc::clone(&shm_state));
            }

            debug!(
                target: "fsqlite_vfs::windows",
                region,
                size,
                path = %self.shm_path.display(),
                "mapped windows shm region"
            );

            return Ok(mapped_region);
        }

        ensure_shm_file_len(&self.shm_path, min_len)?;
        let shm_state = self.ensure_shm_state()?;
        let mapped_region = {
            let mut state = shm_state
                .lock()
                .map_err(|_| lock_poisoned("windows shm state"))?;

            let entry = state.regions.entry(region);
            let region_ref = match entry {
                std::collections::hash_map::Entry::Occupied(occupied) => {
                    let region_ref = occupied.into_mut();
                    if region_ref.len() < size_usize {
                        region_ref.try_resize_heap(size_usize)?;
                    }
                    region_ref
                }
                std::collections::hash_map::Entry::Vacant(vacant) => {
                    vacant.insert(ShmRegion::new(size_usize))
                }
            };
            region_ref.share()
        };

        debug!(
            target: "fsqlite_vfs::windows",
            region,
            size,
            path = %self.shm_path.display(),
            "mapped windows shm region"
        );

        Ok(mapped_region)
    }

    fn shm_lock(&mut self, _cx: &Cx, offset: u32, n: u32, flags: u32) -> Result<()> {
        self.ensure_open()?;
        let end = Self::validate_shm_request(offset, n)?;
        let lock_requested = flags & SQLITE_SHM_LOCK != 0;
        let unlock_requested = flags & SQLITE_SHM_UNLOCK != 0;
        if lock_requested == unlock_requested {
            return Err(FrankenError::LockFailed {
                detail: "invalid shm_lock flags (must set exactly one of LOCK/UNLOCK)".to_string(),
            });
        }

        let shared_requested = flags & SQLITE_SHM_SHARED != 0;
        let exclusive_requested = flags & SQLITE_SHM_EXCLUSIVE != 0;
        if shared_requested == exclusive_requested {
            return Err(FrankenError::LockFailed {
                detail: "invalid shm_lock flags (must set exactly one of SHARED/EXCLUSIVE)"
                    .to_string(),
            });
        }

        let shm_state = self.ensure_shm_state()?;
        let mut state = shm_state
            .lock()
            .map_err(|_| lock_poisoned("windows shm state"))?;

        if lock_requested {
            let mut acquired: Vec<u32> = Vec::new();
            for slot in offset..end {
                let result = if exclusive_requested {
                    Self::acquire_exclusive_slot(&mut state, slot, self.owner_id)
                } else {
                    Self::acquire_shared_slot(&mut state, slot, self.owner_id)
                };

                if let Err(err) = result {
                    for acquired_slot in acquired.into_iter().rev() {
                        let rollback = if exclusive_requested {
                            Self::release_exclusive_slot(&mut state, acquired_slot, self.owner_id)
                        } else {
                            Self::release_shared_slot(&mut state, acquired_slot, self.owner_id)
                        };
                        if rollback.is_err() {
                            break;
                        }
                    }
                    return Err(err);
                }
                acquired.push(slot);
            }
            return Ok(());
        }

        for slot in offset..end {
            if exclusive_requested {
                Self::release_exclusive_slot(&mut state, slot, self.owner_id)?;
            } else {
                Self::release_shared_slot(&mut state, slot, self.owner_id)?;
            }
        }
        Ok(())
    }

    fn shm_barrier(&self) {
        fence(Ordering::SeqCst);
    }

    fn shm_unmap(&mut self, _cx: &Cx, delete: bool) -> Result<()> {
        self.ensure_open()?;
        self.release_shm_owner_state(delete)
    }
}

impl crate::traits::AsyncVfsDataPath for WindowsFile {}

impl Drop for WindowsFile {
    fn drop(&mut self) {
        if !self.is_closed() || self.shm_state.is_some() {
            let cx = Cx::new();
            let _ = self.close(&cx);
        }
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use std::io::Write as _;
    use std::process::Command;
    use tempfile::tempdir;

    struct TempPathCleanup(PathBuf);

    impl Drop for TempPathCleanup {
        fn drop(&mut self) {
            let _ = fs::remove_file(&self.0);
        }
    }

    fn open_flags_create() -> VfsOpenFlags {
        VfsOpenFlags::MAIN_DB | VfsOpenFlags::CREATE | VfsOpenFlags::READWRITE
    }

    fn path_with_suffix(path: &Path, suffix: &str) -> PathBuf {
        let mut suffixed = path.as_os_str().to_owned();
        suffixed.push(suffix);
        PathBuf::from(suffixed)
    }

    #[test]
    fn shm_table_registration_and_orphan_removal_preserve_one_lock_domain() {
        let path = PathBuf::from("registration-race-shm");
        let table = WindowsShmTable::new();

        let old = table
            .get_or_create_and_register(&path, 1)
            .expect("register initial owner");
        old.lock().expect("old SHM state").owner_refs.remove(&1);

        // Model the exact last-close/new-open interleaving: the old owner has
        // become invisible but has not removed the table entry yet. The new
        // opener must publish its owner while the table mutex is still held,
        // so orphan removal cannot pass between lookup and registration.
        let reopened = table
            .get_or_create_and_register_with(&path, 2, || {
                assert!(matches!(
                    table.map.try_lock(),
                    Err(std::sync::TryLockError::WouldBlock)
                ));
            })
            .expect("register replacement owner");
        assert!(Arc::ptr_eq(&old, &reopened));
        table
            .remove_if_orphaned(&path, &old)
            .expect("retain registered state");

        let mapped = table
            .get(&path)
            .expect("read SHM table")
            .expect("registered state remains mapped");
        assert!(Arc::ptr_eq(&mapped, &reopened));
        assert_eq!(
            mapped.lock().expect("mapped SHM state").owner_refs.get(&2),
            Some(&1)
        );

        // Also prove that a delayed cleanup from an older file generation can
        // never erase a newer generation that reused the same pathname.
        mapped
            .lock()
            .expect("mapped SHM state")
            .owner_refs
            .remove(&2);
        table
            .remove_if_orphaned(&path, &mapped)
            .expect("remove old generation");
        let replacement = table
            .get_or_create_and_register(&path, 3)
            .expect("register new generation");
        assert!(!Arc::ptr_eq(&old, &replacement));
        replacement
            .lock()
            .expect("replacement SHM state")
            .owner_refs
            .remove(&3);
        table
            .remove_if_orphaned(&path, &old)
            .expect("ignore stale cleanup");

        let current = table
            .get(&path)
            .expect("read SHM table")
            .expect("new generation remains mapped");
        assert!(Arc::ptr_eq(&current, &replacement));
    }

    #[test]
    fn shm_table_nonextending_registration_is_atomic_with_last_close() {
        let path = PathBuf::from("nonextending-registration-race-shm");
        let table = WindowsShmTable::new();
        let old = table
            .get_or_create_and_register(&path, 1)
            .expect("register initial owner");
        {
            let mut state = old.lock().expect("old SHM state");
            state.regions.insert(0, ShmRegion::new(64));
            state.owner_refs.remove(&1);
        }

        // Model xShmMap(extend=false) racing the old generation's final
        // cleanup.  Lookup, region validation, and the replacement owner ref
        // must all happen while the table mutex excludes orphan removal.
        let (reopened, mapped) = table
            .get_existing_and_register_with(
                &path,
                2,
                || {
                    assert!(matches!(
                        table.map.try_lock(),
                        Err(std::sync::TryLockError::WouldBlock)
                    ));
                },
                |state| {
                    state
                        .regions
                        .get(&0)
                        .map(ShmRegion::share)
                        .ok_or_else(|| FrankenError::CannotOpen { path: path.clone() })
                },
            )
            .expect("register non-extending owner")
            .expect("existing state");
        assert_eq!(mapped.len(), 64);
        assert!(Arc::ptr_eq(&old, &reopened));

        table
            .remove_if_orphaned(&path, &old)
            .expect("registered replacement prevents orphan removal");
        let current = table
            .get(&path)
            .expect("read SHM table")
            .expect("replacement registration remains mapped");
        assert!(Arc::ptr_eq(&current, &reopened));
        assert_eq!(
            current
                .lock()
                .expect("current SHM state")
                .owner_refs
                .get(&2),
            Some(&1)
        );
    }

    #[test]
    fn test_windowsvfs_create_and_write() {
        let cx = Cx::new();
        let dir = tempdir().expect("temp dir");
        let path = dir.path().join("create_write.db");
        let vfs = WindowsVfs::new();
        let (mut file, _) = vfs
            .open(&cx, Some(&path), open_flags_create())
            .expect("open file");

        file.write(&cx, b"hello windows", 0).expect("write");
        let mut buf = [0_u8; 13];
        let n = file.read(&cx, &mut buf, 0).expect("read");
        assert_eq!(n, 13);
        assert_eq!(&buf, b"hello windows");
    }

    #[test]
    fn test_windowsvfs_full_file_identity_is_handle_bound() {
        let cx = Cx::new();
        let dir = tempdir().expect("temp dir");
        let path = dir.path().join("identity.db");
        let alias_path = dir.path().join("identity-alias.db");
        let other_path = dir.path().join("other.db");
        let vfs = WindowsVfs::new();
        let (file_a, _) = vfs
            .open(&cx, Some(&path), open_flags_create())
            .expect("open first handle");
        fs::hard_link(&path, &alias_path).expect("create hard-link alias");
        let (file_b, _) = vfs
            .open(&cx, Some(&path), open_flags_create())
            .expect("open second handle");
        let (alias_file, _) = vfs
            .open(&cx, Some(&alias_path), open_flags_create())
            .expect("open hard-link alias");
        let (other_file, _) = vfs
            .open(&cx, Some(&other_path), open_flags_create())
            .expect("open distinct file");

        let identity_a = file_a
            .file_identity()
            .expect("read first identity")
            .expect("Windows file identity should be available");
        let identity_b = file_b
            .file_identity()
            .expect("read second identity")
            .expect("Windows file identity should be available");
        let alias_identity = alias_file
            .file_identity()
            .expect("read alias identity")
            .expect("Windows file identity should be available");
        let other_identity = other_file
            .file_identity()
            .expect("read distinct identity")
            .expect("Windows file identity should be available");

        assert_eq!(identity_a, identity_b);
        assert_eq!(identity_a, alias_identity);
        assert_ne!(identity_a, other_identity);
    }

    #[test]
    fn test_windowsvfs_expected_identity_mismatch_precedes_side_effects() {
        let cx = Cx::new();
        let dir = tempdir().expect("temp dir");
        let path = dir.path().join("identity-guard.db");
        let other_path = dir.path().join("other.db");
        let journal_path = path_with_suffix(&path, "-journal");
        let wal_path = path_with_suffix(&path, "-wal");
        let shm_path = sqlite_shm_path(&path);

        fs::write(&path, b"main sentinel").expect("seed main sentinel");
        fs::write(&other_path, b"other sentinel").expect("seed other file");
        fs::write(&journal_path, b"journal sentinel").expect("seed journal sentinel");
        fs::write(&wal_path, b"wal sentinel").expect("seed WAL sentinel");
        fs::write(&shm_path, b"shm sentinel").expect("seed SHM sentinel");

        let expected_identity =
            FileIdentity::from_file(&File::open(&other_path).expect("open other identity handle"))
                .expect("query other identity")
                .expect("Windows file identity must be available");
        let actual_identity =
            FileIdentity::from_file(&File::open(&path).expect("open main identity handle"))
                .expect("query main identity")
                .expect("Windows file identity must be available");
        assert_ne!(expected_identity, actual_identity);

        let main_before = fs::read(&path).expect("snapshot main sentinel");
        let journal_before = fs::read(&journal_path).expect("snapshot journal sentinel");
        let wal_before = fs::read(&wal_path).expect("snapshot WAL sentinel");
        let shm_before = fs::read(&shm_path).expect("snapshot SHM sentinel");
        for sidecar in windows_lock_sidecar_paths(&path) {
            assert!(!sidecar.exists(), "lock sidecar must start absent");
        }

        let error = WindowsVfs::new()
            .open_with_expected_identity(
                &cx,
                &path,
                VfsOpenFlags::MAIN_DB | VfsOpenFlags::READWRITE,
                expected_identity,
            )
            .expect_err("wrong expected identity must refuse the open");

        assert!(matches!(error, FrankenError::CannotOpen { .. }));
        assert_eq!(fs::read(&path).unwrap(), main_before);
        assert_eq!(fs::read(&journal_path).unwrap(), journal_before);
        assert_eq!(fs::read(&wal_path).unwrap(), wal_before);
        assert_eq!(fs::read(&shm_path).unwrap(), shm_before);
        for sidecar in windows_lock_sidecar_paths(&path) {
            assert!(
                !sidecar.exists(),
                "identity refusal must not create {}",
                sidecar.display()
            );
        }
    }

    #[test]
    fn test_windowsvfs_read_exact_at() {
        let cx = Cx::new();
        let dir = tempdir().expect("temp dir");
        let path = dir.path().join("read_at.db");
        let vfs = WindowsVfs::new();
        let (mut file, _) = vfs
            .open(&cx, Some(&path), open_flags_create())
            .expect("open file");
        file.write(&cx, b"0123456789", 0).expect("write");

        let mut buf = [0_u8; 4];
        let n = file.read(&cx, &mut buf, 3).expect("read");
        assert_eq!(n, 4);
        assert_eq!(&buf, b"3456");
    }

    #[test]
    fn test_windowsvfs_write_all_at() {
        let cx = Cx::new();
        let dir = tempdir().expect("temp dir");
        let path = dir.path().join("write_at.db");
        let vfs = WindowsVfs::new();
        let (mut file, _) = vfs
            .open(&cx, Some(&path), open_flags_create())
            .expect("open file");
        file.write(&cx, b"abcdefghij", 0).expect("write base");
        file.write(&cx, b"WXYZ", 2).expect("write overlay");

        let mut buf = [0_u8; 10];
        let n = file.read(&cx, &mut buf, 0).expect("read");
        assert_eq!(n, 10);
        assert_eq!(&buf, b"abWXYZghij");
    }

    #[test]
    fn test_windowsvfs_file_size_and_truncate() {
        let cx = Cx::new();
        let dir = tempdir().expect("temp dir");
        let path = dir.path().join("size.db");
        let vfs = WindowsVfs::new();
        let (mut file, _) = vfs
            .open(&cx, Some(&path), open_flags_create())
            .expect("open file");
        file.write(&cx, &[7_u8; 4096], 0).expect("write");
        assert_eq!(file.file_size(&cx).expect("size"), 4096);

        file.truncate(&cx, 1024).expect("truncate");
        assert_eq!(file.file_size(&cx).expect("size"), 1024);
    }

    #[test]
    fn test_windowsvfs_file_size() {
        test_windowsvfs_file_size_and_truncate();
    }

    #[test]
    fn test_windowsvfs_truncate() {
        test_windowsvfs_file_size_and_truncate();
    }

    #[test]
    fn test_windowsvfs_shared_memory_create_and_cross_handle() {
        let cx = Cx::new();
        let dir = tempdir().expect("temp dir");
        let path = dir.path().join("shm.db");
        let vfs = WindowsVfs::new();
        let (mut file_a, _) = vfs
            .open(&cx, Some(&path), open_flags_create())
            .expect("open A");
        let (mut file_b, _) = vfs
            .open(&cx, Some(&path), open_flags_create())
            .expect("open B");

        let region_a = file_a.shm_map(&cx, 0, 32 * 1024, true).expect("map A");
        {
            let mut guard = region_a.lock();
            guard[0] = 0xAA;
            guard[1] = 0x55;
        }

        let region_b = file_b.shm_map(&cx, 0, 32 * 1024, false).expect("map B");
        let guard = region_b.lock();
        assert_eq!(guard[0], 0xAA);
        assert_eq!(guard[1], 0x55);
        drop(guard);
    }

    #[test]
    fn test_windowsvfs_shared_memory_create() {
        test_windowsvfs_shared_memory_create_and_cross_handle();
    }

    #[test]
    fn test_windowsvfs_shared_memory_cross_handle() {
        test_windowsvfs_shared_memory_create_and_cross_handle();
    }

    #[test]
    fn test_windowsvfs_shm_resize_preserves_existing_mappings() {
        let cx = Cx::new();
        let dir = tempdir().expect("temp dir");
        let path = dir.path().join("shm_resize.db");
        let vfs = WindowsVfs::new();
        let (mut file, _) = vfs
            .open(&cx, Some(&path), open_flags_create())
            .expect("open file");

        let region_small = file.shm_map(&cx, 0, 32, true).expect("initial map");
        region_small.write_u32_le(0, 0x1122_3344).unwrap();

        let region_large = file.shm_map(&cx, 0, 64, true).expect("resized map");
        region_large.write_u32_le(0, 0x5566_7788).unwrap();
        region_large.write_u32_le(32, 0xAABB_CCDD).unwrap();

        assert_eq!(
            region_small.read_u32_le(0).unwrap(),
            0x5566_7788,
            "resizing must preserve shared backing for existing mappings"
        );
        assert_eq!(region_large.read_u32_le(32).unwrap(), 0xAABB_CCDD);
    }

    #[test]
    fn test_windowsvfs_shm_map_extend_false_rejects_missing_without_side_effects() {
        let cx = Cx::new();
        let dir = tempdir().expect("temp dir");
        let path = dir.path().join("shm_missing_no_extend.db");
        let vfs = WindowsVfs::new();
        let (mut file, _) = vfs
            .open(&cx, Some(&path), open_flags_create())
            .expect("open file");
        let shm_path = file.shm_path.clone();

        let err = file.shm_map(&cx, 2, 64, false).unwrap_err();
        assert!(
            matches!(err, FrankenError::CannotOpen { .. }),
            "missing non-extend shm_map should report CannotOpen, got {err:?}"
        );
        assert!(
            file.shm_state.is_none(),
            "failed non-extend shm_map must not register shm owner state"
        );
        assert!(
            windows_shm_table().get(&shm_path).unwrap().is_none(),
            "failed non-extend shm_map must not create a shared state entry"
        );
        assert!(
            !shm_path.exists(),
            "failed non-extend shm_map must not create a -shm file"
        );
    }

    #[test]
    fn test_windowsvfs_reserved_lock_conflicts_across_handles() {
        let cx = Cx::new();
        let dir = tempdir().expect("temp dir");
        let path = dir.path().join("reserved_lock.db");
        let vfs = WindowsVfs::new();
        let (mut file_a, _) = vfs
            .open(&cx, Some(&path), open_flags_create())
            .expect("open A");
        let (mut file_b, _) = vfs
            .open(&cx, Some(&path), open_flags_create())
            .expect("open B");

        file_a.lock(&cx, LockLevel::Shared).expect("A shared");
        file_a.lock(&cx, LockLevel::Reserved).expect("A reserved");
        assert!(
            !file_a.check_reserved_lock(&cx).unwrap(),
            "a handle should not report its own RESERVED lock as external"
        );
        assert!(
            file_b.check_reserved_lock(&cx).unwrap(),
            "other handles must observe a RESERVED-or-higher sidecar lock"
        );
        assert!(
            matches!(
                file_b.lock(&cx, LockLevel::Reserved),
                Err(FrankenError::Busy)
            ),
            "second RESERVED locker must be rejected"
        );

        file_a.unlock(&cx, LockLevel::None).expect("release A");
        file_b.lock(&cx, LockLevel::Reserved).expect("B reserved");
    }

    #[test]
    fn test_windowsvfs_exclusive_lock_conflicts_with_other_shared_handle() {
        let cx = Cx::new();
        let dir = tempdir().expect("temp dir");
        let path = dir.path().join("exclusive_vs_shared.db");
        let vfs = WindowsVfs::new();
        let (mut file_a, _) = vfs
            .open(&cx, Some(&path), open_flags_create())
            .expect("open A");
        let (mut file_b, _) = vfs
            .open(&cx, Some(&path), open_flags_create())
            .expect("open B");

        file_a.lock(&cx, LockLevel::Shared).expect("A shared");
        file_b.lock(&cx, LockLevel::Shared).expect("B shared");
        assert!(
            matches!(
                file_a.lock(&cx, LockLevel::Exclusive),
                Err(FrankenError::Busy)
            ),
            "EXCLUSIVE must upgrade the shared sidecar and conflict with another SHARED holder"
        );
        assert_eq!(
            file_a.lock_level,
            LockLevel::Shared,
            "failed EXCLUSIVE upgrade should roll back to the prior lock level"
        );
        file_a
            .lock(&cx, LockLevel::Reserved)
            .expect("failed exclusive upgrade must not strand RESERVED/PENDING sidecars");
    }

    #[test]
    fn test_stock_main_lock_level_is_recomputed_from_held_ranges() {
        let dir = tempdir().unwrap();
        let path = dir.path().join("stock_level_recompute.db");
        let mut options = windows_open_options();
        let file = options
            .read(true)
            .write(true)
            .create_new(true)
            .open(path)
            .expect("open lock-state test file");
        let mut locks = WindowsStockMainLocks::new(file);

        locks.lock_level = LockLevel::Exclusive;
        locks.recompute_lock_level();
        assert_eq!(locks.lock_level, LockLevel::None);

        locks.shared_range = true;
        locks.recompute_lock_level();
        assert_eq!(locks.lock_level, LockLevel::Shared);

        locks.reserved = true;
        locks.recompute_lock_level();
        assert_eq!(locks.lock_level, LockLevel::Reserved);

        locks.pending = true;
        locks.recompute_lock_level();
        assert_eq!(locks.lock_level, LockLevel::Pending);

        locks.shared_range_exclusive = true;
        locks.recompute_lock_level();
        assert_eq!(locks.lock_level, LockLevel::Exclusive);

        locks.shared_range = false;
        locks.recompute_lock_level();
        assert_eq!(
            locks.lock_level,
            LockLevel::None,
            "upper bytes without the SHARED range are not a reusable SQLite lock prefix"
        );

        locks.shared_range = true;
        locks.reserved = false;
        locks.recompute_lock_level();
        assert_eq!(
            locks.lock_level,
            LockLevel::Shared,
            "PENDING/EXCLUSIVE remnants without RESERVED must not overstate the reusable level"
        );
    }

    #[test]
    fn test_ordinary_writer_levels_contend_on_stock_sqlite_bytes() {
        let cx = Cx::new();
        let dir = tempdir().unwrap();
        let path = dir.path().join("ordinary_stock_writer_levels.db");
        let vfs = WindowsVfs::new();
        let (mut writer, _) = vfs
            .open(&cx, Some(&path), open_flags_create())
            .expect("open FrankenSQLite writer");
        let mut blocker_options = windows_open_options();
        let blocker = blocker_options
            .read(true)
            .write(true)
            .open(&path)
            .expect("open independent stock-lock blocker");

        writer.lock(&cx, LockLevel::Shared).expect("shared");
        try_lock_stock_sqlite_range(&blocker, STOCK_SQLITE_RESERVED_BYTE, 1)
            .expect("hold stock RESERVED byte");
        assert!(matches!(
            writer.lock(&cx, LockLevel::Reserved),
            Err(FrankenError::Busy)
        ));
        assert_eq!(writer.lock_level, LockLevel::Shared);
        assert_eq!(
            writer.stock_main_locks.as_ref().unwrap().lock_level,
            LockLevel::Shared
        );
        unlock_stock_sqlite_range(&blocker, STOCK_SQLITE_RESERVED_BYTE, 1)
            .expect("release stock RESERVED blocker");

        writer.lock(&cx, LockLevel::Reserved).expect("reserved");
        try_lock_stock_sqlite_range(&blocker, STOCK_SQLITE_PENDING_BYTE, 1)
            .expect("hold stock PENDING byte");
        assert!(matches!(
            writer.lock(&cx, LockLevel::Pending),
            Err(FrankenError::Busy)
        ));
        assert_eq!(writer.lock_level, LockLevel::Reserved);
        let stock = writer.stock_main_locks.as_ref().unwrap();
        assert_eq!(stock.lock_level, LockLevel::Reserved);
        assert!(stock.reserved);
        assert!(!stock.pending);
        unlock_stock_sqlite_range(&blocker, STOCK_SQLITE_PENDING_BYTE, 1)
            .expect("release stock PENDING blocker");

        writer.lock(&cx, LockLevel::Pending).expect("pending");
        assert!(matches!(
            try_lock_stock_sqlite_range(&blocker, STOCK_SQLITE_RESERVED_BYTE, 1),
            Err(FrankenError::Busy)
        ));
        assert!(matches!(
            try_lock_stock_sqlite_range(&blocker, STOCK_SQLITE_PENDING_BYTE, 1),
            Err(FrankenError::Busy)
        ));
        writer.unlock(&cx, LockLevel::None).expect("unlock writer");
        try_lock_stock_sqlite_range(&blocker, STOCK_SQLITE_RESERVED_BYTE, 1)
            .expect("ordinary unlock must release stock RESERVED");
        unlock_stock_sqlite_range(&blocker, STOCK_SQLITE_RESERVED_BYTE, 1).unwrap();
        try_lock_stock_sqlite_range(&blocker, STOCK_SQLITE_PENDING_BYTE, 1)
            .expect("ordinary unlock must release stock PENDING");
        unlock_stock_sqlite_range(&blocker, STOCK_SQLITE_PENDING_BYTE, 1).unwrap();
        writer.close(&cx).unwrap();
    }

    #[test]
    fn test_ordinary_exclusive_conflict_restores_stock_reserved_snapshot() {
        let cx = Cx::new();
        let dir = tempdir().unwrap();
        let path = dir.path().join("ordinary_stock_exclusive_unwind.db");
        let vfs = WindowsVfs::new();
        let (mut writer, _) = vfs
            .open(&cx, Some(&path), open_flags_create())
            .expect("open FrankenSQLite writer");
        let mut reader_options = windows_open_options();
        let stock_reader = reader_options
            .read(true)
            .write(true)
            .open(&path)
            .expect("open independent stock reader");

        writer
            .lock(&cx, LockLevel::Reserved)
            .expect("acquire FrankenSQLite RESERVED");
        try_lock_stock_sqlite_shared_range(
            &stock_reader,
            STOCK_SQLITE_SHARED_FIRST,
            STOCK_SQLITE_SHARED_SIZE,
        )
        .expect("hold stock SHARED reader range");

        assert!(matches!(
            writer.lock(&cx, LockLevel::Exclusive),
            Err(FrankenError::Busy)
        ));
        assert_eq!(writer.lock_level, LockLevel::Reserved);
        let stock = writer.stock_main_locks.as_ref().unwrap();
        assert_eq!(stock.lock_level, LockLevel::Reserved);
        assert!(stock.shared_range);
        assert!(!stock.shared_range_exclusive);
        assert!(stock.reserved);
        assert!(!stock.pending);
        try_lock_stock_sqlite_range(&stock_reader, STOCK_SQLITE_PENDING_BYTE, 1)
            .expect("failed EXCLUSIVE must release stock PENDING");
        unlock_stock_sqlite_range(&stock_reader, STOCK_SQLITE_PENDING_BYTE, 1).unwrap();

        unlock_stock_sqlite_range(
            &stock_reader,
            STOCK_SQLITE_SHARED_FIRST,
            STOCK_SQLITE_SHARED_SIZE,
        )
        .expect("release stock reader");
        writer
            .lock(&cx, LockLevel::Exclusive)
            .expect("exclusive retry after reader drains");
        let stock = writer.stock_main_locks.as_ref().unwrap();
        assert_eq!(stock.lock_level, LockLevel::Exclusive);
        assert!(stock.pending);
        assert!(stock.reserved);
        assert!(stock.shared_range_exclusive);
        assert!(matches!(
            try_lock_stock_sqlite_range(
                &stock_reader,
                STOCK_SQLITE_SHARED_FIRST,
                STOCK_SQLITE_SHARED_SIZE,
            ),
            Err(FrankenError::Busy)
        ));

        writer.unlock(&cx, LockLevel::None).expect("unlock writer");
        try_lock_stock_sqlite_range(
            &stock_reader,
            STOCK_SQLITE_SHARED_FIRST,
            STOCK_SQLITE_SHARED_SIZE,
        )
        .expect("ordinary unlock must release stock EXCLUSIVE range");
        unlock_stock_sqlite_range(
            &stock_reader,
            STOCK_SQLITE_SHARED_FIRST,
            STOCK_SQLITE_SHARED_SIZE,
        )
        .unwrap();
        writer.close(&cx).unwrap();
    }

    #[test]
    fn test_external_shared_snapshot_uses_stock_sqlite_main_range() {
        let cx = Cx::new();
        let dir = tempdir().unwrap();
        let path = dir.path().join("stock_shared_snapshot.db");
        let vfs = WindowsVfs::new();
        let (mut snapshot, _) = vfs
            .open(&cx, Some(&path), open_flags_create())
            .expect("open snapshot handle");
        let mut probe_options = windows_open_options();
        let probe = probe_options
            .read(true)
            .write(true)
            .open(&path)
            .expect("open independent stock-lock probe");

        snapshot
            .lock_external_shared_snapshot(&cx)
            .expect("acquire external shared-snapshot fence");
        assert!(matches!(
            try_lock_stock_sqlite_range(
                &probe,
                STOCK_SQLITE_SHARED_FIRST,
                STOCK_SQLITE_SHARED_SIZE,
            ),
            Err(FrankenError::Busy)
        ));

        // Stock SQLite releases the transient PENDING participation after the
        // shared range is held, allowing a writer to enter PENDING while it
        // waits for readers to drain.
        try_lock_stock_sqlite_range(&probe, STOCK_SQLITE_PENDING_BYTE, 1)
            .expect("snapshot must not retain the transient pending byte");
        unlock_stock_sqlite_range(&probe, STOCK_SQLITE_PENDING_BYTE, 1)
            .expect("unlock pending probe");

        snapshot
            .unlock_external_shared_snapshot(&cx)
            .expect("release external shared-snapshot fence");
        try_lock_stock_sqlite_range(&probe, STOCK_SQLITE_SHARED_FIRST, STOCK_SQLITE_SHARED_SIZE)
            .expect("released shared range must be exclusively acquirable");
        unlock_stock_sqlite_range(&probe, STOCK_SQLITE_SHARED_FIRST, STOCK_SQLITE_SHARED_SIZE)
            .expect("unlock shared-range probe");
        snapshot.close(&cx).unwrap();
    }

    #[test]
    fn test_external_shared_snapshot_partial_acquisition_unwinds_all_locks() {
        let cx = Cx::new();
        let dir = tempdir().unwrap();
        let path = dir.path().join("stock_shared_snapshot_unwind.db");
        let vfs = WindowsVfs::new();
        let (mut snapshot, _) = vfs
            .open(&cx, Some(&path), open_flags_create())
            .expect("open snapshot handle");
        let mut blocker_options = windows_open_options();
        let blocker = blocker_options
            .read(true)
            .write(true)
            .open(&path)
            .expect("open independent stock-lock blocker");
        try_lock_stock_sqlite_range(
            &blocker,
            STOCK_SQLITE_SHARED_FIRST,
            STOCK_SQLITE_SHARED_SIZE,
        )
        .expect("hold stock exclusive shared range");

        assert!(matches!(
            snapshot.lock_external_shared_snapshot(&cx),
            Err(FrankenError::Busy)
        ));
        assert_eq!(snapshot.lock_level, LockLevel::None);
        assert!(snapshot.external_shared_snapshot_prior_level.is_none());
        let stock = snapshot
            .stock_main_locks
            .as_ref()
            .expect("failed acquisition keeps a reusable dedicated handle");
        assert_eq!(stock.lock_level, LockLevel::None);
        assert!(!stock.pending);
        assert!(!stock.reserved);
        assert!(!stock.shared_range);
        try_lock_stock_sqlite_range(&blocker, STOCK_SQLITE_PENDING_BYTE, 1)
            .expect("failed snapshot acquisition must release pending");
        unlock_stock_sqlite_range(&blocker, STOCK_SQLITE_PENDING_BYTE, 1)
            .expect("unlock pending probe");

        unlock_stock_sqlite_range(
            &blocker,
            STOCK_SQLITE_SHARED_FIRST,
            STOCK_SQLITE_SHARED_SIZE,
        )
        .expect("release shared-range blocker");
        snapshot
            .lock_external_shared_snapshot(&cx)
            .expect("retry after contention must succeed");
        snapshot
            .unlock_external_shared_snapshot(&cx)
            .expect("retry fence must release cleanly");
        snapshot.close(&cx).unwrap();
    }

    #[test]
    fn test_external_shared_snapshot_conflicts_with_external_maintenance() {
        let cx = Cx::new();
        let dir = tempdir().unwrap();
        let path = dir.path().join("shared_snapshot_vs_maintenance.db");
        let vfs = WindowsVfs::new();
        let (mut snapshot, _) = vfs
            .open(&cx, Some(&path), open_flags_create())
            .expect("open snapshot handle");
        let (mut maintenance, _) = vfs
            .open(&cx, Some(&path), open_flags_create())
            .expect("open maintenance handle");

        snapshot
            .lock_external_shared_snapshot(&cx)
            .expect("acquire shared snapshot");
        assert!(matches!(
            maintenance.lock_external_maintenance(&cx, false),
            Err(FrankenError::Busy)
        ));
        snapshot
            .unlock_external_shared_snapshot(&cx)
            .expect("release shared snapshot");

        maintenance
            .lock_external_maintenance(&cx, false)
            .expect("maintenance must succeed after shared snapshot releases");
        maintenance
            .unlock_external_maintenance(&cx, false)
            .expect("release maintenance");
        snapshot.close(&cx).unwrap();
        maintenance.close(&cx).unwrap();
    }

    #[test]
    fn test_external_maintenance_fence_uses_stock_sqlite_main_ranges() {
        let cx = Cx::new();
        let dir = tempdir().unwrap();
        let path = dir.path().join("stock_maintenance_main.db");
        let vfs = WindowsVfs::new();
        let (mut maintenance, _) = vfs
            .open(&cx, Some(&path), open_flags_create())
            .expect("open maintenance handle");
        let mut probe_options = windows_open_options();
        let probe = probe_options
            .read(true)
            .write(true)
            .open(&path)
            .expect("open independent stock-lock probe");

        maintenance
            .lock_external_maintenance(&cx, false)
            .expect("acquire external maintenance fence");
        for (offset, len) in [
            (STOCK_SQLITE_PENDING_BYTE, 1),
            (STOCK_SQLITE_RESERVED_BYTE, 1),
            (STOCK_SQLITE_SHARED_FIRST, STOCK_SQLITE_SHARED_SIZE),
        ] {
            assert!(
                matches!(
                    try_lock_stock_sqlite_range(&probe, offset, len),
                    Err(FrankenError::Busy)
                ),
                "stock SQLite range {offset}..{} must be fenced",
                offset + len
            );
        }

        maintenance
            .unlock_external_maintenance(&cx, false)
            .expect("release external maintenance fence");
        for (offset, len) in [
            (STOCK_SQLITE_PENDING_BYTE, 1),
            (STOCK_SQLITE_RESERVED_BYTE, 1),
            (STOCK_SQLITE_SHARED_FIRST, STOCK_SQLITE_SHARED_SIZE),
        ] {
            try_lock_stock_sqlite_range(&probe, offset, len)
                .expect("released range must be acquirable");
            unlock_stock_sqlite_range(&probe, offset, len).expect("unlock probe range");
        }
        maintenance.close(&cx).unwrap();
    }

    #[test]
    fn test_external_maintenance_wal_fence_uses_real_shm_bytes() {
        let cx = Cx::new();
        let dir = tempdir().unwrap();
        let path = dir.path().join("stock_maintenance_wal.db");
        let shm_path = sqlite_shm_path(&path);
        let vfs = WindowsVfs::new();
        let (mut maintenance, _) = vfs
            .open(&cx, Some(&path), open_flags_create())
            .expect("open maintenance handle");

        maintenance
            .lock_external_maintenance(&cx, true)
            .expect("acquire WAL external maintenance fence");
        let mut probe_options = windows_open_options();
        let probe = probe_options
            .read(true)
            .write(true)
            .open(&shm_path)
            .expect("open real -shm probe");
        for offset in [STOCK_SQLITE_WAL_WRITE_BYTE, STOCK_SQLITE_WAL_CKPT_BYTE] {
            assert!(
                matches!(
                    try_lock_stock_sqlite_range(&probe, offset, 1),
                    Err(FrankenError::Busy)
                ),
                "stock SQLite WAL byte {offset} must be fenced"
            );
        }

        maintenance
            .unlock_external_maintenance(&cx, true)
            .expect("release WAL external maintenance fence");
        for offset in [STOCK_SQLITE_WAL_WRITE_BYTE, STOCK_SQLITE_WAL_CKPT_BYTE] {
            try_lock_stock_sqlite_range(&probe, offset, 1)
                .expect("released WAL byte must be acquirable");
            unlock_stock_sqlite_range(&probe, offset, 1).expect("unlock WAL probe byte");
        }
        maintenance.close(&cx).unwrap();
    }

    #[test]
    fn test_external_maintenance_partial_wal_acquisition_unwinds_every_lock() {
        let cx = Cx::new();
        let dir = tempdir().unwrap();
        let path = dir.path().join("stock_maintenance_unwind.db");
        let shm_path = sqlite_shm_path(&path);
        let vfs = WindowsVfs::new();
        let (mut maintenance, _) = vfs
            .open(&cx, Some(&path), open_flags_create())
            .expect("open maintenance handle");
        let (blocker, _) = open_windows_lock_sidecar(&shm_path).expect("open -shm blocker");
        try_lock_stock_sqlite_range(&blocker, STOCK_SQLITE_WAL_CKPT_BYTE, 1)
            .expect("hold stock checkpoint byte");

        assert!(matches!(
            maintenance.lock_external_maintenance(&cx, true),
            Err(FrankenError::Busy)
        ));

        // The write byte was acquired before checkpoint contention and must
        // have been unwound. Main ranges were never reached and the local
        // cooperative locks must also have been released so a retry can work.
        assert_eq!(maintenance.lock_level, LockLevel::None);
        assert!(
            maintenance.stock_main_locks.is_none(),
            "real WAL contention must be resolved before opening the dedicated stock-main lock handle"
        );
        try_lock_stock_sqlite_range(&blocker, STOCK_SQLITE_WAL_WRITE_BYTE, 1)
            .expect("partial WAL write lock must be unwound");
        unlock_stock_sqlite_range(&blocker, STOCK_SQLITE_WAL_WRITE_BYTE, 1)
            .expect("unlock WAL write probe");
        unlock_stock_sqlite_range(&blocker, STOCK_SQLITE_WAL_CKPT_BYTE, 1)
            .expect("release checkpoint blocker");

        maintenance
            .lock_external_maintenance(&cx, true)
            .expect("retry after contention must succeed");
        maintenance
            .unlock_external_maintenance(&cx, true)
            .expect("retry fence must release cleanly");
        maintenance.close(&cx).unwrap();
    }

    #[test]
    fn test_external_maintenance_partial_main_acquisition_unwinds_every_lock() {
        let cx = Cx::new();
        let dir = tempdir().unwrap();
        let path = dir.path().join("stock_maintenance_main_unwind.db");
        let shm_path = sqlite_shm_path(&path);
        let vfs = WindowsVfs::new();
        let (mut maintenance, _) = vfs
            .open(&cx, Some(&path), open_flags_create())
            .expect("open maintenance handle");
        let mut blocker_options = windows_open_options();
        let blocker = blocker_options
            .read(true)
            .write(true)
            .open(&path)
            .expect("open main-range blocker");
        try_lock_stock_sqlite_range(
            &blocker,
            STOCK_SQLITE_SHARED_FIRST,
            STOCK_SQLITE_SHARED_SIZE,
        )
        .expect("hold stock shared range");

        assert!(matches!(
            maintenance.lock_external_maintenance(&cx, true),
            Err(FrankenError::Busy)
        ));

        // PENDING and RESERVED were acquired before the SHARED-range
        // contention. Both must have been unwound, as must the FrankenSQLite
        // sidecar locks, so a retry can succeed after the blocker leaves.
        for offset in [STOCK_SQLITE_PENDING_BYTE, STOCK_SQLITE_RESERVED_BYTE] {
            try_lock_stock_sqlite_range(&blocker, offset, 1)
                .expect("partial main lock must be unwound");
            unlock_stock_sqlite_range(&blocker, offset, 1).expect("unlock main-range probe");
        }
        let mut shm_probe_options = windows_open_options();
        let shm_probe = shm_probe_options
            .read(true)
            .write(true)
            .open(&shm_path)
            .expect("open released -shm lock probe");
        for offset in [STOCK_SQLITE_WAL_WRITE_BYTE, STOCK_SQLITE_WAL_CKPT_BYTE] {
            try_lock_stock_sqlite_range(&shm_probe, offset, 1)
                .expect("main-lock failure must unwind the earlier real WAL fence");
            unlock_stock_sqlite_range(&shm_probe, offset, 1).expect("unlock WAL-range probe");
        }
        unlock_stock_sqlite_range(
            &blocker,
            STOCK_SQLITE_SHARED_FIRST,
            STOCK_SQLITE_SHARED_SIZE,
        )
        .expect("release shared-range blocker");

        maintenance
            .lock_external_maintenance(&cx, true)
            .expect("retry after contention must succeed");
        maintenance
            .unlock_external_maintenance(&cx, true)
            .expect("retry fence must release cleanly");
        maintenance.close(&cx).unwrap();
    }

    #[test]
    fn test_windowsvfs_shm_exclusive_unlock_preserves_prior_shared_lock() {
        let cx = Cx::new();
        let dir = tempdir().expect("temp dir");
        let path = dir.path().join("shm_lock_downgrade.db");
        let vfs = WindowsVfs::new();
        let (mut file, _) = vfs
            .open(&cx, Some(&path), open_flags_create())
            .expect("open file");

        file.shm_lock(&cx, 0, 1, SQLITE_SHM_LOCK | SQLITE_SHM_SHARED)
            .expect("acquire shared");
        file.shm_lock(&cx, 0, 1, SQLITE_SHM_LOCK | SQLITE_SHM_EXCLUSIVE)
            .expect("upgrade to exclusive");
        file.shm_lock(&cx, 0, 1, SQLITE_SHM_UNLOCK | SQLITE_SHM_EXCLUSIVE)
            .expect("downgrade from exclusive");
        file.shm_lock(&cx, 0, 1, SQLITE_SHM_UNLOCK | SQLITE_SHM_SHARED)
            .expect("release preserved shared lock");
    }

    #[test]
    fn test_windowsvfs_temp_file_auto_delete() {
        let cx = Cx::new();
        let vfs = WindowsVfs::new();
        let flags = VfsOpenFlags::TEMP_DB
            | VfsOpenFlags::CREATE
            | VfsOpenFlags::READWRITE
            | VfsOpenFlags::DELETEONCLOSE;
        let (mut file, _) = vfs.open(&cx, None, flags).expect("open temp");
        let temp_path = file.path.clone();
        let lock_sidecars = windows_lock_sidecar_paths(&temp_path);
        assert!(temp_path.exists());
        for sidecar in &lock_sidecars {
            assert!(
                sidecar.exists(),
                "temporary Windows VFS handle should create {}",
                sidecar.display()
            );
        }
        file.close(&cx).expect("close");
        assert!(!temp_path.exists());
        for sidecar in &lock_sidecars {
            assert!(
                !sidecar.exists(),
                "temporary close should remove advisory lock sidecar {}",
                sidecar.display()
            );
        }
    }

    #[test]
    fn test_windowsvfs_temp_file_skips_existing_candidate() {
        let cx = Cx::new();
        let seed_base = 1_000_000_000_000_u64 + u64::from(std::process::id()) * 1_024;
        let (seed, blocker, blocker_file) = (0_u64..1_024)
            .find_map(|offset| {
                let seed = seed_base + offset;
                let blocker = env::temp_dir().join(format!("fsqlite-windows-{seed}.tmp"));
                let mut blocker_options = windows_open_options();
                blocker_options
                    .write(true)
                    .create_new(true)
                    .open(&blocker)
                    .ok()
                    .map(|file| (seed, blocker, file))
            })
            .expect("available temp candidate");
        let _blocker_cleanup = TempPathCleanup(blocker.clone());
        let mut blocker_file = blocker_file;
        blocker_file
            .write_all(b"existing temp candidate")
            .expect("write existing temp candidate");
        drop(blocker_file);
        let vfs = WindowsVfs {
            inner: Arc::new(Mutex::new(WindowsVfsInner { next_temp_id: seed })),
        };
        let flags = VfsOpenFlags::TEMP_DB
            | VfsOpenFlags::CREATE
            | VfsOpenFlags::READWRITE
            | VfsOpenFlags::DELETEONCLOSE;

        let (mut file, _) = vfs.open(&cx, None, flags).expect("open temp");
        let opened_path = file.path.clone();
        assert_ne!(
            opened_path, blocker,
            "anonymous temp open must not reuse an existing candidate path"
        );
        assert!(
            blocker.exists(),
            "temp collision handling must preserve the existing candidate file"
        );

        file.close(&cx).expect("close temp");
        assert!(
            !opened_path.exists(),
            "delete-on-close should remove the actual temp file"
        );
    }

    #[test]
    fn test_windowsvfs_open_handles_block_delete_sharing() {
        let dir = tempdir().expect("temp dir");
        let path = dir.path().join("delete_sharing.db");
        let mut options = windows_open_options();
        let _file = options
            .read(true)
            .write(true)
            .create(true)
            .truncate(false)
            .open(&path)
            .expect("open file without delete sharing");

        assert!(
            fs::remove_file(&path).is_err(),
            "Windows VFS files must reject unlink while an open handle exists"
        );
    }

    #[test]
    fn test_windowsvfs_lock_open_failure_cleans_created_shared_sidecar() {
        let dir = tempdir().expect("temp dir");
        let path = dir.path().join("partial_lock_open.db");
        let shared_path = sqlite_shared_lock_path(&path);
        let reserved_path = sqlite_reserved_lock_path(&path);
        fs::create_dir(&reserved_path).expect("reserved sidecar blocker");

        assert!(WindowsOsLockFiles::open(&path).is_err());
        assert!(
            !shared_path.exists(),
            "failed lock setup should remove the shared sidecar it just created"
        );
        assert!(
            reserved_path.is_dir(),
            "cleanup must not disturb the path that caused the open failure"
        );
    }

    #[test]
    fn test_windowsvfs_open_failure_cleans_created_db_file() {
        let cx = Cx::new();
        let dir = tempdir().expect("temp dir");
        let path = dir.path().join("partial_vfs_open.db");
        let shared_path = sqlite_shared_lock_path(&path);
        let reserved_path = sqlite_reserved_lock_path(&path);
        fs::create_dir(&reserved_path).expect("reserved sidecar blocker");
        let vfs = WindowsVfs::new();
        let flags = open_flags_create() | VfsOpenFlags::EXCLUSIVE | VfsOpenFlags::DELETEONCLOSE;

        assert!(vfs.open(&cx, Some(&path), flags).is_err());
        assert!(
            !path.exists(),
            "failed exclusive create should remove the DB file it just created"
        );
        assert!(
            !shared_path.exists(),
            "failed lock setup should remove the shared sidecar it just created"
        );
        assert!(
            reserved_path.is_dir(),
            "cleanup must not disturb the path that caused the open failure"
        );
    }

    #[test]
    fn test_windowsvfs_plain_create_failure_cleans_created_db_file() {
        let cx = Cx::new();
        let dir = tempdir().expect("temp dir");
        let path = dir.path().join("partial_plain_create.db");
        let shared_path = sqlite_shared_lock_path(&path);
        let reserved_path = sqlite_reserved_lock_path(&path);
        fs::create_dir(&reserved_path).expect("reserved sidecar blocker");
        let vfs = WindowsVfs::new();

        assert!(vfs.open(&cx, Some(&path), open_flags_create()).is_err());
        assert!(
            !path.exists(),
            "failed plain create should remove the DB file it just created"
        );
        assert!(
            !shared_path.exists(),
            "failed lock setup should remove the shared sidecar it just created"
        );
        assert!(
            reserved_path.is_dir(),
            "cleanup must not disturb the path that caused the open failure"
        );
    }

    #[test]
    fn test_windowsvfs_plain_create_failure_preserves_existing_db_file() {
        let cx = Cx::new();
        let dir = tempdir().expect("temp dir");
        let path = dir.path().join("existing_plain_create.db");
        let shared_path = sqlite_shared_lock_path(&path);
        let reserved_path = sqlite_reserved_lock_path(&path);
        fs::write(&path, b"existing db").expect("existing db");
        fs::create_dir(&reserved_path).expect("reserved sidecar blocker");
        let vfs = WindowsVfs::new();

        assert!(vfs.open(&cx, Some(&path), open_flags_create()).is_err());
        assert_eq!(
            fs::read(&path).expect("read existing db"),
            b"existing db",
            "failed plain create must preserve an existing DB file"
        );
        assert!(
            !shared_path.exists(),
            "failed lock setup should remove only the shared sidecar it just created"
        );
        assert!(
            reserved_path.is_dir(),
            "cleanup must not disturb the path that caused the open failure"
        );
    }

    #[test]
    fn test_windowsvfs_open_failure_preserves_existing_sidecar() {
        let cx = Cx::new();
        let dir = tempdir().expect("temp dir");
        let path = dir.path().join("partial_vfs_open_existing_sidecar.db");
        let shared_path = sqlite_shared_lock_path(&path);
        let reserved_path = sqlite_reserved_lock_path(&path);
        fs::write(&shared_path, b"existing shared sidecar").expect("existing shared sidecar");
        fs::create_dir(&reserved_path).expect("reserved sidecar blocker");
        let vfs = WindowsVfs::new();
        let flags = open_flags_create() | VfsOpenFlags::EXCLUSIVE | VfsOpenFlags::DELETEONCLOSE;

        assert!(vfs.open(&cx, Some(&path), flags).is_err());
        assert!(
            !path.exists(),
            "failed exclusive create should remove the DB file it just created"
        );
        assert!(
            shared_path.exists(),
            "failed VFS open must preserve a sidecar it did not create"
        );
        assert!(
            reserved_path.is_dir(),
            "cleanup must not disturb the path that caused the open failure"
        );
    }

    #[test]
    fn test_windowsvfs_lock_open_failure_preserves_existing_sidecars() {
        let dir = tempdir().expect("temp dir");
        let path = dir.path().join("existing_partial_lock_open.db");
        let shared_path = sqlite_shared_lock_path(&path);
        let reserved_path = sqlite_reserved_lock_path(&path);
        let pending_path = sqlite_pending_lock_path(&path);
        fs::write(&shared_path, b"existing shared sidecar").expect("existing shared sidecar");
        fs::create_dir(&pending_path).expect("pending sidecar blocker");

        assert!(WindowsOsLockFiles::open(&path).is_err());
        assert!(
            shared_path.exists(),
            "failed lock setup must not remove a sidecar it did not create"
        );
        assert!(
            !reserved_path.exists(),
            "failed lock setup should remove the reserved sidecar it just created"
        );
        assert!(
            pending_path.is_dir(),
            "cleanup must not disturb the path that caused the open failure"
        );
    }

    #[test]
    fn test_windowsvfs_delete_on_close_is_idempotent() {
        let cx = Cx::new();
        let dir = tempdir().expect("temp dir");
        let path = dir.path().join("idempotent_close.db");
        let vfs = WindowsVfs::new();
        let flags = open_flags_create() | VfsOpenFlags::DELETEONCLOSE;
        let (mut file, _) = vfs
            .open(&cx, Some(&path), flags)
            .expect("open delete-on-close file");
        let shm_path = file.shm_path.clone();
        let lock_sidecars = windows_lock_sidecar_paths(&path);

        file.close(&cx).expect("first close");
        assert!(!path.exists(), "first close should delete the DB file");

        fs::write(&path, b"replacement db").expect("replacement db");
        fs::write(&shm_path, b"replacement shm").expect("replacement shm");
        for sidecar in &lock_sidecars {
            fs::write(sidecar, b"replacement lock").expect("replacement sidecar");
        }

        file.close(&cx).expect("second close");
        assert!(path.exists(), "second close must be a no-op");
        assert!(
            shm_path.exists(),
            "second close must not delete replacement SHM"
        );
        for sidecar in &lock_sidecars {
            assert!(
                sidecar.exists(),
                "second close must not delete replacement sidecar {}",
                sidecar.display()
            );
        }
    }

    #[test]
    fn test_windowsvfs_shm_rejects_use_after_close() {
        let cx = Cx::new();
        let dir = tempdir().expect("temp dir");
        let path = dir.path().join("closed_shm.db");
        let vfs = WindowsVfs::new();
        let (mut file, _) = vfs
            .open(&cx, Some(&path), open_flags_create())
            .expect("open file");

        file.close(&cx).expect("close file");
        assert!(
            matches!(
                file.shm_map(&cx, 0, 32 * 1024, true),
                Err(FrankenError::Internal(_))
            ),
            "closed Windows handles must not recreate SHM state"
        );
        assert!(
            matches!(
                file.shm_lock(&cx, 0, 1, SQLITE_SHM_LOCK | SQLITE_SHM_SHARED),
                Err(FrankenError::Internal(_))
            ),
            "closed Windows handles must reject SHM locks"
        );
        assert!(
            matches!(file.shm_unmap(&cx, false), Err(FrankenError::Internal(_))),
            "closed Windows handles must reject SHM unmap"
        );
    }

    #[test]
    fn test_windowsvfs_delete_removes_lock_sidecars() {
        let cx = Cx::new();
        let dir = tempdir().expect("temp dir");
        let path = dir.path().join("delete_sidecars.db");
        let vfs = WindowsVfs::new();
        let (mut file, _) = vfs
            .open(&cx, Some(&path), open_flags_create())
            .expect("open file");
        let lock_sidecars = windows_lock_sidecar_paths(&path);

        for sidecar in &lock_sidecars {
            assert!(
                sidecar.exists(),
                "opening the Windows VFS handle should create {}",
                sidecar.display()
            );
        }

        file.close(&cx).expect("close file");

        vfs.delete(&cx, &path, false).expect("delete file");
        assert!(!path.exists(), "Vfs::delete should remove the main DB");
        for sidecar in &lock_sidecars {
            assert!(
                !sidecar.exists(),
                "Vfs::delete should remove advisory lock sidecar {}",
                sidecar.display()
            );
        }
    }

    #[test]
    fn test_windowsvfs_sector_size_detection() {
        let cx = Cx::new();
        let dir = tempdir().expect("temp dir");
        let path = dir.path().join("sector.db");
        let vfs = WindowsVfs::new();
        let (file, _) = vfs
            .open(&cx, Some(&path), open_flags_create())
            .expect("open file");

        let size = file.sector_size();
        assert!(size.is_power_of_two());
        assert!(size >= 512);
    }

    #[test]
    fn test_windowsvfs_device_characteristics() {
        let cx = Cx::new();
        let dir = tempdir().expect("temp dir");
        let path = dir.path().join("iocap.db");
        let vfs = WindowsVfs::new();
        let (file, _) = vfs
            .open(&cx, Some(&path), open_flags_create())
            .expect("open file");

        assert_eq!(
            file.device_characteristics() & SQLITE_IOCAP_UNDELETABLE_WHEN_OPEN,
            SQLITE_IOCAP_UNDELETABLE_WHEN_OPEN
        );
    }

    #[test]
    fn test_e2e_windowsvfs_c_sqlite_interop() {
        let sqlite_available = Command::new("sqlite3")
            .arg("--version")
            .output()
            .is_ok_and(|output| output.status.success());
        if !sqlite_available {
            return;
        }

        let cx = Cx::new();
        let dir = tempdir().expect("temp dir");
        let path = dir.path().join("interop.db");
        let path_str = path.to_str().expect("path utf8");

        let create_status = Command::new("sqlite3")
            .arg(path_str)
            .arg("CREATE TABLE t(x INTEGER); INSERT INTO t(x) VALUES (1),(2),(3);")
            .status()
            .expect("run sqlite3 create");
        assert!(create_status.success());

        let vfs = WindowsVfs::new();
        let (mut file, _) = vfs
            .open(
                &cx,
                Some(&path),
                VfsOpenFlags::MAIN_DB | VfsOpenFlags::READWRITE,
            )
            .expect("open via windows vfs");
        let mut header = [0_u8; 16];
        let read = file.read(&cx, &mut header, 0).expect("read sqlite header");
        assert_eq!(read, 16);
        assert_eq!(&header, b"SQLite format 3\0");
        file.close(&cx).expect("close vfs file");

        let query_output = Command::new("sqlite3")
            .arg(path_str)
            .arg("SELECT count(*) FROM t;")
            .output()
            .expect("run sqlite3 query");
        assert!(query_output.status.success());
        let stdout = String::from_utf8(query_output.stdout).expect("utf8");
        assert_eq!(stdout.trim(), "3");
    }

    #[test]
    fn test_windowsvfs_cfg_gate() {
        let _ = std::any::type_name::<WindowsVfs>();
        let _ = std::any::type_name::<WindowsFile>();
    }
}