scorpiofs 0.2.2

FUSE-based virtual filesystem with Antares overlay for monorepo builds
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
use std::{path::PathBuf, sync::Arc};

use libfuse_fs::{
    passthrough::{new_passthroughfs_layer, PassthroughArgs},
    unionfs::{config::Config, layer::Layer, OverlayFs},
};
use tokio::task::JoinHandle;

use crate::server::mount_filesystem_with_antares_cache;

/// Antares union-fs wrapper: dicfuse lower + passthrough upper/CL.
pub struct AntaresFuse {
    pub mountpoint: PathBuf,
    pub upper_dir: PathBuf,
    pub dic: Arc<crate::dicfuse::Dicfuse>,
    pub cl_dir: Option<PathBuf>,
    /// Background task running the FUSE session.
    fuse_task: Option<JoinHandle<()>>,
}
use rfuse3::raw::logfs::LoggingFileSystem;
impl AntaresFuse {
    /// Build directories for upper / optional CL layers.
    pub async fn new(
        mountpoint: PathBuf,
        dic: Arc<crate::dicfuse::Dicfuse>,
        upper_dir: PathBuf,
        cl_dir: Option<PathBuf>,
    ) -> std::io::Result<Self> {
        if let Some(cl) = &cl_dir {
            std::fs::create_dir_all(cl)?;
        }
        std::fs::create_dir_all(&upper_dir)?;
        std::fs::create_dir_all(&mountpoint)?;

        Ok(Self {
            mountpoint,
            upper_dir,
            dic,
            cl_dir,
            fuse_task: None,
        })
    }

    /// Compose the union filesystem instance.
    pub async fn build_overlay(&self) -> std::io::Result<OverlayFs> {
        // Build lower layers:
        // - Optional CL dir sits above Dicfuse to override base files for the CL view.
        // - Dicfuse remains the base read-only monorepo layer.
        let mut lower_layers: Vec<Arc<dyn Layer>> = Vec::new();

        if let Some(cl_dir) = &self.cl_dir {
            let cl_layer = new_passthroughfs_layer(PassthroughArgs {
                root_dir: cl_dir,
                mapping: None::<String>,
            })
            .await?;
            lower_layers.push(Arc::new(cl_layer) as Arc<dyn Layer>);
        }

        lower_layers.push(self.dic.clone() as Arc<dyn Layer>);

        // Upper layer mirrors upper_dir to keep writes separated from lower layers.
        let upper_layer: Arc<dyn Layer> = Arc::new(
            new_passthroughfs_layer(PassthroughArgs {
                root_dir: &self.upper_dir,
                mapping: None::<String>,
            })
            .await?,
        );

        // passthrough Upper  - readwrite file system over upper dir
        // passthrough CL  - readwrite file system over upper dir
        // dicfuse  - readonly file and dictionary from mega

        let cfg = Config {
            mountpoint: self.mountpoint.clone(),
            do_import: true,
            ..Default::default()
        };

        OverlayFs::new(Some(upper_layer), lower_layers, cfg, 1)
    }

    /// Mount the composed unionfs into the provided mountpoint, spawning a background task to run the FUSE session.
    pub async fn mount(&mut self) -> std::io::Result<()> {
        if self.fuse_task.is_some() {
            return Ok(());
        }

        // Ensure mountpoint exists *before* mounting. This is a plain filesystem check.
        // Do not probe it *after* mounting, because that may trigger FUSE getattr and can fail
        // transiently while Dicfuse is still loading.
        std::fs::metadata(&self.mountpoint)?;

        let overlay = self.build_overlay().await?;
        let logfs = LoggingFileSystem::new(overlay);
        let handle =
            mount_filesystem_with_antares_cache(logfs, self.mountpoint.as_os_str(), true).await;

        // Spawn background task to run the FUSE session
        let fuse_task = tokio::spawn(async move {
            // This will block until unmount is called
            let _ = handle.await;
        });

        self.fuse_task = Some(fuse_task);

        // Readiness probe: wait until the FUSE mount is actually servicing requests.
        // Without this, callers (e.g., Buck2) that immediately stat() the mountpoint
        // may race against the kernel FUSE_INIT handshake and get ENOTCONN (errno 107).
        let mp = self.mountpoint.clone();
        let probe_timeout = std::time::Duration::from_secs(10);
        let probe_interval = std::time::Duration::from_millis(50);
        let probe_start = std::time::Instant::now();
        loop {
            match tokio::fs::metadata(&mp).await {
                Ok(_) => {
                    tracing::info!(
                        "FUSE mount ready at {} (probe took {:.2}s)",
                        mp.display(),
                        probe_start.elapsed().as_secs_f64()
                    );
                    break;
                }
                Err(e) => {
                    if probe_start.elapsed() >= probe_timeout {
                        tracing::warn!(
                            "FUSE mount probe timed out for {} after {:.1}s: {}",
                            mp.display(),
                            probe_timeout.as_secs_f64(),
                            e
                        );
                        break;
                    }
                    tokio::time::sleep(probe_interval).await;
                }
            }
        }

        Ok(())
    }

    /// Unmount the FUSE session if mounted.
    ///
    /// Uses lazy unmount (`fusermount -uz`) to detach the filesystem even if
    /// it's busy, preventing the unmount operation from blocking indefinitely.
    /// A timeout is applied when waiting for the FUSE task to complete.
    ///
    /// # Errors
    ///
    /// This method will log warnings but not fail if:
    /// - The FUSE task doesn't complete within the timeout
    /// - The task panics
    ///
    /// Only critical errors (e.g., fusermount command execution failure)
    /// will cause this method to return an error.
    pub async fn unmount(&mut self) -> std::io::Result<()> {
        if let Some(task) = self.fuse_task.take() {
            let mount_path = self.mountpoint.to_string_lossy().to_string();
            // Prefer graceful unmount first to reduce stale-handle windows for active clients.
            // If it cannot finish quickly (busy mount), fall back to lazy detach.
            let graceful = tokio::time::timeout(
                tokio::time::Duration::from_millis(1200),
                tokio::process::Command::new("fusermount")
                    .arg("-u")
                    .arg(&mount_path)
                    .output(),
            )
            .await;

            match graceful {
                Ok(Ok(output)) if output.status.success() => {}
                Ok(Ok(output)) => {
                    tracing::warn!(
                        "fusermount -u failed for {}: {}; falling back to -uz",
                        mount_path,
                        String::from_utf8_lossy(&output.stderr)
                    );
                    let lazy = tokio::process::Command::new("fusermount")
                        .arg("-uz")
                        .arg(&mount_path)
                        .output()
                        .await?;
                    if !lazy.status.success() {
                        tracing::warn!(
                            "fusermount -uz failed for {}: {}",
                            mount_path,
                            String::from_utf8_lossy(&lazy.stderr)
                        );
                    }
                }
                Ok(Err(e)) => {
                    tracing::warn!(
                        "failed to execute fusermount -u for {}: {}; falling back to -uz",
                        mount_path,
                        e
                    );
                    let lazy = tokio::process::Command::new("fusermount")
                        .arg("-uz")
                        .arg(&mount_path)
                        .output()
                        .await?;
                    if !lazy.status.success() {
                        tracing::warn!(
                            "fusermount -uz failed for {}: {}",
                            mount_path,
                            String::from_utf8_lossy(&lazy.stderr)
                        );
                    }
                }
                Err(_) => {
                    tracing::warn!(
                        "fusermount -u timed out for {}; falling back to -uz",
                        mount_path
                    );
                    let lazy = tokio::process::Command::new("fusermount")
                        .arg("-uz")
                        .arg(&mount_path)
                        .output()
                        .await?;
                    if !lazy.status.success() {
                        tracing::warn!(
                            "fusermount -uz failed for {}: {}",
                            mount_path,
                            String::from_utf8_lossy(&lazy.stderr)
                        );
                    }
                }
            }

            // Wait for the FUSE task to complete with timeout to avoid hanging
            let timeout_duration = tokio::time::Duration::from_secs(5);
            match tokio::time::timeout(timeout_duration, task).await {
                Ok(Ok(_)) => {
                    // Task completed successfully
                }
                Ok(Err(e)) => {
                    tracing::warn!(
                        "fuse task panicked during unmount of {}: {:?}",
                        mount_path,
                        e
                    );
                }
                Err(_) => {
                    tracing::warn!(
                        "fuse task did not complete within {}s for {}, continuing anyway",
                        timeout_duration.as_secs(),
                        mount_path
                    );
                }
            }
        }
        Ok(())
    }
}

#[cfg(test)]
mod tests {
    use std::{
        collections::HashMap,
        ffi::{OsStr, OsString},
        num::NonZeroU32,
        path::PathBuf,
        sync::atomic::{AtomicU64, Ordering},
    };

    use async_trait::async_trait;
    use bytes::Bytes;
    use libfuse_fs::{
        context::OperationContext,
        unionfs::{config::Config as UnionConfig, layer::Layer, OverlayFs},
    };
    use rfuse3::{
        raw::{
            reply::{
                DirectoryEntry, FileAttr, ReplyAttr, ReplyCreated, ReplyData, ReplyDirectory,
                ReplyEntry, ReplyInit, ReplyOpen, ReplyWrite, ReplyXAttr,
            },
            Filesystem, Request,
        },
        FileType, Inode, Result as FuseResult, Timestamp,
    };
    use serial_test::serial;
    use tokio::time::{sleep, Duration};
    use uuid::Uuid;

    use super::AntaresFuse;
    use crate::{dicfuse::Dicfuse, util::config};

    #[derive(Debug, Clone)]
    struct MemNode {
        inode: u64,
        parent: u64,
        kind: FileType,
        perm: u16,
        uid: u32,
        gid: u32,
        data: Vec<u8>,
    }

    #[derive(Debug, Default)]
    struct MemState {
        nodes: HashMap<u64, MemNode>,
        children: HashMap<u64, HashMap<OsString, u64>>,
    }

    /// A tiny in-memory upper layer used to test OverlayFs copy-up semantics without requiring
    /// passthroughfs (open_by_handle_at) or an actual kernel mount.
    #[derive(Debug)]
    struct MemUpperLayer {
        next_inode: AtomicU64,
        state: tokio::sync::RwLock<MemState>,
    }

    impl MemUpperLayer {
        fn new() -> Self {
            let uid = unsafe { libc::getuid() } as u32;
            let gid = unsafe { libc::getgid() } as u32;
            let mut st = MemState::default();
            st.nodes.insert(
                1,
                MemNode {
                    inode: 1,
                    parent: 0,
                    kind: FileType::Directory,
                    perm: 0o755,
                    uid,
                    gid,
                    data: Vec::new(),
                },
            );
            Self {
                next_inode: AtomicU64::new(1),
                state: tokio::sync::RwLock::new(st),
            }
        }

        fn now_ts() -> Timestamp {
            Timestamp::from(std::time::SystemTime::now())
        }

        fn file_attr(node: &MemNode) -> FileAttr {
            let ts = Self::now_ts();
            FileAttr {
                ino: node.inode,
                size: node.data.len() as u64,
                blocks: 0,
                atime: ts,
                mtime: ts,
                ctime: ts,
                kind: node.kind,
                perm: node.perm,
                nlink: if node.kind == FileType::Directory {
                    2
                } else {
                    1
                },
                uid: node.uid,
                gid: node.gid,
                rdev: 0,
                blksize: 4096,
            }
        }

        async fn create_child(
            &self,
            parent: u64,
            name: &OsStr,
            kind: FileType,
            perm: u16,
        ) -> std::io::Result<u64> {
            let mut st = self.state.write().await;
            let parent_node = st
                .nodes
                .get(&parent)
                .ok_or_else(|| std::io::Error::from_raw_os_error(libc::ENOENT))?;
            if parent_node.kind != FileType::Directory {
                return Err(std::io::Error::from_raw_os_error(libc::ENOTDIR));
            }

            let existing = st.children.get(&parent).and_then(|m| m.get(name).copied());
            if let Some(inode) = existing {
                return Ok(inode);
            }

            let inode = self.next_inode.fetch_add(1, Ordering::Relaxed) + 1;
            let uid = unsafe { libc::getuid() } as u32;
            let gid = unsafe { libc::getgid() } as u32;
            st.nodes.insert(
                inode,
                MemNode {
                    inode,
                    parent,
                    kind,
                    perm,
                    uid,
                    gid,
                    data: Vec::new(),
                },
            );
            st.children
                .entry(parent)
                .or_default()
                .insert(name.to_os_string(), inode);
            Ok(inode)
        }

        async fn get_child_inode(&self, parent: u64, name: &OsStr) -> Option<u64> {
            let st = self.state.read().await;
            st.children.get(&parent).and_then(|m| m.get(name).copied())
        }

        async fn read_file_by_name(&self, name: &str) -> Option<Vec<u8>> {
            let st = self.state.read().await;
            let ino = st
                .children
                .get(&1)
                .and_then(|m| m.get(OsStr::new(name)))
                .copied()?;
            st.nodes.get(&ino).map(|n| n.data.clone())
        }
    }

    impl Filesystem for MemUpperLayer {
        async fn init(&self, _req: Request) -> FuseResult<ReplyInit> {
            Ok(ReplyInit {
                max_write: NonZeroU32::new(128 * 1024).unwrap(),
            })
        }

        async fn destroy(&self, _req: Request) {}

        async fn lookup(
            &self,
            _req: Request,
            parent: Inode,
            name: &OsStr,
        ) -> FuseResult<ReplyEntry> {
            let inode = self
                .get_child_inode(parent, name)
                .await
                .ok_or_else(|| std::io::Error::from_raw_os_error(libc::ENOENT))?;
            let st = self.state.read().await;
            let node = st
                .nodes
                .get(&inode)
                .ok_or_else(|| std::io::Error::from_raw_os_error(libc::ENOENT))?;
            Ok(ReplyEntry {
                ttl: Duration::from_secs(1),
                attr: Self::file_attr(node),
                generation: 0,
            })
        }

        async fn getattr(
            &self,
            _req: Request,
            inode: Inode,
            _fh: Option<u64>,
            _flags: u32,
        ) -> FuseResult<ReplyAttr> {
            let st = self.state.read().await;
            let node = st
                .nodes
                .get(&inode)
                .ok_or_else(|| std::io::Error::from_raw_os_error(libc::ENOENT))?;
            Ok(ReplyAttr {
                ttl: Duration::from_secs(1),
                attr: Self::file_attr(node),
            })
        }

        async fn setattr(
            &self,
            _req: Request,
            inode: Inode,
            _fh: Option<u64>,
            _set_attr: rfuse3::SetAttr,
        ) -> FuseResult<ReplyAttr> {
            // Minimal: accept setattr and return current attrs. Overlay copy-up uses this to
            // preserve ownership/mode; our in-memory layer does not model these changes yet.
            self.getattr(_req, inode, _fh, 0).await
        }

        async fn open(&self, _req: Request, inode: Inode, _flags: u32) -> FuseResult<ReplyOpen> {
            // Use inode as the handle.
            Ok(ReplyOpen {
                fh: inode,
                flags: 0,
            })
        }

        async fn read(
            &self,
            _req: Request,
            inode: Inode,
            _fh: u64,
            offset: u64,
            size: u32,
        ) -> FuseResult<ReplyData> {
            let st = self.state.read().await;
            let node = st
                .nodes
                .get(&inode)
                .ok_or_else(|| std::io::Error::from_raw_os_error(libc::ENOENT))?;
            let off = offset as usize;
            let end = (off + size as usize).min(node.data.len());
            let slice = if off >= node.data.len() {
                &[]
            } else {
                &node.data[off..end]
            };
            Ok(ReplyData {
                data: Bytes::copy_from_slice(slice),
            })
        }

        #[allow(clippy::too_many_arguments)]
        async fn write(
            &self,
            _req: Request,
            inode: Inode,
            _fh: u64,
            offset: u64,
            data: &[u8],
            _write_flags: u32,
            _flags: u32,
        ) -> FuseResult<ReplyWrite> {
            let mut st = self.state.write().await;
            let node = st
                .nodes
                .get_mut(&inode)
                .ok_or_else(|| std::io::Error::from_raw_os_error(libc::ENOENT))?;
            if node.kind == FileType::Directory {
                return Err(std::io::Error::from_raw_os_error(libc::EISDIR).into());
            }
            let off = offset as usize;
            let needed = off + data.len();
            if node.data.len() < needed {
                node.data.resize(needed, 0);
            }
            node.data[off..off + data.len()].copy_from_slice(data);
            Ok(ReplyWrite {
                written: data.len() as u32,
            })
        }

        async fn setxattr(
            &self,
            _req: Request,
            _inode: Inode,
            _name: &OsStr,
            _value: &[u8],
            _flags: u32,
            _position: u32,
        ) -> FuseResult<()> {
            // No-op xattr support for overlay bookkeeping.
            Ok(())
        }

        async fn getxattr(
            &self,
            _req: Request,
            _inode: Inode,
            _name: &OsStr,
            _size: u32,
        ) -> FuseResult<ReplyXAttr> {
            Err(std::io::Error::from_raw_os_error(libc::ENODATA).into())
        }

        async fn listxattr(
            &self,
            _req: Request,
            _inode: Inode,
            size: u32,
        ) -> FuseResult<ReplyXAttr> {
            if size == 0 {
                Ok(ReplyXAttr::Size(0))
            } else {
                Ok(ReplyXAttr::Data(Bytes::new()))
            }
        }

        async fn removexattr(&self, _req: Request, _inode: Inode, _name: &OsStr) -> FuseResult<()> {
            Ok(())
        }

        async fn readdir<'a>(
            &'a self,
            _req: Request,
            parent: Inode,
            _fh: u64,
            offset: i64,
        ) -> FuseResult<
            ReplyDirectory<impl futures::Stream<Item = FuseResult<DirectoryEntry>> + Send + 'a>,
        > {
            use futures::stream::iter;

            let st = self.state.read().await;
            let parent_node = st
                .nodes
                .get(&parent)
                .ok_or_else(|| std::io::Error::from_raw_os_error(libc::ENOENT))?;
            if parent_node.kind != FileType::Directory {
                return Err(std::io::Error::from_raw_os_error(libc::ENOTDIR).into());
            }
            let parent_parent_inode = if parent == 1 { 1 } else { parent_node.parent };

            let mut out: Vec<std::result::Result<DirectoryEntry, rfuse3::Errno>> = Vec::new();

            // offset 0: ".", offset 1: "..", offset 2+: children
            if offset < 1 {
                out.push(Ok(DirectoryEntry {
                    inode: parent,
                    kind: FileType::Directory,
                    name: ".".into(),
                    offset: 1,
                }));
            }
            if offset < 2 {
                out.push(Ok(DirectoryEntry {
                    inode: parent_parent_inode,
                    kind: FileType::Directory,
                    name: "..".into(),
                    offset: 2,
                }));
            }

            if let Some(children) = st.children.get(&parent) {
                for (idx, (name, inode)) in children.iter().enumerate() {
                    let entry_offset = (idx + 2) as i64;
                    if entry_offset > offset {
                        let kind = st
                            .nodes
                            .get(inode)
                            .map(|n| n.kind)
                            .unwrap_or(FileType::RegularFile);
                        out.push(Ok(DirectoryEntry {
                            inode: *inode,
                            kind,
                            name: name.clone(),
                            offset: entry_offset + 1,
                        }));
                    }
                }
            }

            Ok(ReplyDirectory { entries: iter(out) })
        }

        async fn releasedir(
            &self,
            _req: Request,
            _inode: Inode,
            _fh: u64,
            _flags: u32,
        ) -> FuseResult<()> {
            Ok(())
        }

        async fn getlk(
            &self,
            _req: Request,
            _inode: Inode,
            _fh: u64,
            _lock_owner: u64,
            start: u64,
            end: u64,
            _type: u32,
            _pid: u32,
        ) -> FuseResult<rfuse3::raw::reply::ReplyLock> {
            // In-memory test layer: no locks held, report F_UNLCK.
            Ok(rfuse3::raw::reply::ReplyLock {
                start,
                end,
                r#type: libc::F_UNLCK as u32,
                pid: 0,
            })
        }

        async fn setlk(
            &self,
            _req: Request,
            _inode: Inode,
            _fh: u64,
            _lock_owner: u64,
            _start: u64,
            _end: u64,
            _type: u32,
            _pid: u32,
            _block: bool,
        ) -> FuseResult<()> {
            // In-memory test layer: accept lock requests (no real enforcement).
            Ok(())
        }
    }

    #[async_trait]
    impl Layer for MemUpperLayer {
        fn root_inode(&self) -> Inode {
            1
        }

        async fn create_with_context(
            &self,
            _ctx: OperationContext,
            parent: Inode,
            name: &OsStr,
            _mode: u32,
            _flags: u32,
        ) -> FuseResult<ReplyCreated> {
            let inode = self
                .create_child(parent, name, FileType::RegularFile, 0o644)
                .await
                .map_err(rfuse3::Errno::from)?;
            let st = self.state.read().await;
            let node = st
                .nodes
                .get(&inode)
                .ok_or_else(|| std::io::Error::from_raw_os_error(libc::ENOENT))?;
            Ok(ReplyCreated {
                ttl: Duration::from_secs(1),
                attr: Self::file_attr(node),
                generation: 0,
                fh: inode,
                flags: 0,
            })
        }

        async fn mkdir_with_context(
            &self,
            _ctx: OperationContext,
            parent: Inode,
            name: &OsStr,
            _mode: u32,
            _umask: u32,
        ) -> FuseResult<ReplyEntry> {
            let inode = self
                .create_child(parent, name, FileType::Directory, 0o755)
                .await
                .map_err(rfuse3::Errno::from)?;
            let st = self.state.read().await;
            let node = st
                .nodes
                .get(&inode)
                .ok_or_else(|| std::io::Error::from_raw_os_error(libc::ENOENT))?;
            Ok(ReplyEntry {
                ttl: Duration::from_secs(1),
                attr: Self::file_attr(node),
                generation: 0,
            })
        }

        async fn symlink_with_context(
            &self,
            _ctx: OperationContext,
            _parent: Inode,
            _name: &OsStr,
            _link: &OsStr,
        ) -> FuseResult<ReplyEntry> {
            Err(std::io::Error::from_raw_os_error(libc::ENOSYS).into())
        }
    }

    /// No actual mount required: validate copy-up behavior via `OverlayFs`'s Filesystem interface.
    #[tokio::test]
    async fn test_overlay_copyup_without_mount_does_not_mutate_dicfuse_lower() {
        let test_id = Uuid::new_v4();
        let store_path = format!("/tmp/scorpio_dicfuse_unit_store_{test_id}");
        let _ = std::fs::remove_dir_all(&store_path);
        std::fs::create_dir_all(&store_path).unwrap();

        // Lower: dicfuse (read-only)
        let dic = std::sync::Arc::new(Dicfuse::new_with_store_path(&store_path).await);
        dic.store.insert_mock_item(1, 0, "", true).await; // root
        dic.store.insert_mock_item(2, 1, "hello.txt", false).await;
        dic.store.save_file(2, b"lower".to_vec());
        dic.store.load_db().await.unwrap();

        // Upper: in-memory writable layer.
        let upper = std::sync::Arc::new(MemUpperLayer::new());

        let cfg = UnionConfig {
            mountpoint: PathBuf::from(format!("/tmp/scorpio_overlay_unit_{test_id}")),
            do_import: true,
            ..Default::default()
        };

        let overlay = OverlayFs::new(
            Some(upper.clone() as std::sync::Arc<dyn Layer>),
            vec![dic.clone() as std::sync::Arc<dyn Layer>],
            cfg,
            1,
        )
        .unwrap();

        overlay.init(Request::default()).await.unwrap();

        let entry = overlay
            .lookup(Request::default(), 1, OsStr::new("hello.txt"))
            .await
            .unwrap();
        let overlay_ino = entry.attr.ino;

        // Read initial content from lower.
        let ro = overlay
            .open(Request::default(), overlay_ino, libc::O_RDONLY as u32)
            .await
            .unwrap();
        let data = overlay
            .read(Request::default(), overlay_ino, ro.fh, 0, 32)
            .await
            .unwrap();
        assert_eq!(data.data.as_ref(), b"lower");

        // Write triggers copy-up.
        let wo = overlay
            .open(Request::default(), overlay_ino, libc::O_WRONLY as u32)
            .await
            .unwrap();
        overlay
            .write(Request::default(), overlay_ino, wo.fh, 0, b"upper", 0, 0)
            .await
            .unwrap();

        // Now read should see upper content.
        let ro2 = overlay
            .open(Request::default(), overlay_ino, libc::O_RDONLY as u32)
            .await
            .unwrap();
        let data2 = overlay
            .read(Request::default(), overlay_ino, ro2.fh, 0, 32)
            .await
            .unwrap();
        assert_eq!(data2.data.as_ref(), b"upper");

        // Verify upper contains the file and lower store did not change.
        assert_eq!(
            upper.read_file_by_name("hello.txt").await.unwrap(),
            b"upper"
        );
        assert_eq!(dic.store.get_file_content(2).unwrap().to_vec(), b"lower");

        let _ = std::fs::remove_dir_all(&store_path);
    }

    /// No actual mount required: validate multiple overlays share the same Dicfuse lower while uppers stay isolated.
    #[tokio::test]
    async fn test_two_overlays_share_dicfuse_lower_isolate_upper_without_mount() {
        // Ensure config is loaded so dicfuse import logic (if triggered by OverlayFs) can resolve store_path.
        if let Err(e) = config::init_config("./scorpio.toml") {
            if !e.contains("already initialized") {
                panic!("Failed to load config: {e}");
            }
        }

        let test_id = Uuid::new_v4();
        let store_path = format!("/tmp/scorpio_dicfuse_unit_store2_{test_id}");
        let _ = std::fs::remove_dir_all(&store_path);
        std::fs::create_dir_all(&store_path).unwrap();

        let dic = std::sync::Arc::new(Dicfuse::new_with_store_path(&store_path).await);
        dic.store.insert_mock_item(1, 0, "", true).await;
        dic.store.insert_mock_item(2, 1, "hello.txt", false).await;
        dic.store.save_file(2, b"lower".to_vec());
        dic.store.load_db().await.unwrap();

        // OverlayFs with do_import=true may trigger Dicfuse background import logic.
        // Ensure it treats the pre-seeded sled DB as reusable (skip network fetch) by writing the marker
        // into the SAME store directory we seeded.
        let marker_path = std::path::PathBuf::from(&store_path).join(".dicfuse_import_done");
        if let Some(parent) = marker_path.parent() {
            let _ = std::fs::create_dir_all(parent);
        }
        let _ = std::fs::write(&marker_path, b"ok\n");

        let upper1 = std::sync::Arc::new(MemUpperLayer::new());
        let upper2 = std::sync::Arc::new(MemUpperLayer::new());

        let cfg1 = UnionConfig {
            mountpoint: PathBuf::from(format!("/tmp/scorpio_overlay_unit2_a_{test_id}")),
            // Keep import enabled so OverlayFs builds the view, but Dicfuse import should be skipped by marker above.
            do_import: true,
            ..Default::default()
        };
        let cfg2 = UnionConfig {
            mountpoint: PathBuf::from(format!("/tmp/scorpio_overlay_unit2_b_{test_id}")),
            do_import: true,
            ..Default::default()
        };

        let overlay1 = std::sync::Arc::new(
            OverlayFs::new(
                Some(upper1.clone() as std::sync::Arc<dyn Layer>),
                vec![dic.clone() as std::sync::Arc<dyn Layer>],
                cfg1,
                1,
            )
            .unwrap(),
        );
        let overlay2 = std::sync::Arc::new(
            OverlayFs::new(
                Some(upper2.clone() as std::sync::Arc<dyn Layer>),
                vec![dic.clone() as std::sync::Arc<dyn Layer>],
                cfg2,
                1,
            )
            .unwrap(),
        );

        overlay1.init(Request::default()).await.unwrap();
        overlay2.init(Request::default()).await.unwrap();

        let e1 = overlay1
            .lookup(Request::default(), 1, OsStr::new("hello.txt"))
            .await
            .unwrap();
        let e2 = overlay2
            .lookup(Request::default(), 1, OsStr::new("hello.txt"))
            .await
            .unwrap();
        let ino1 = e1.attr.ino;
        let ino2 = e2.attr.ino;

        // Concurrent reads from both overlays should be consistent.
        let o1 = overlay1.clone();
        let o2 = overlay2.clone();
        let t1 = tokio::spawn(async move {
            for _ in 0..50 {
                let ro = o1
                    .open(Request::default(), ino1, libc::O_RDONLY as u32)
                    .await
                    .unwrap();
                let data = o1
                    .read(Request::default(), ino1, ro.fh, 0, 32)
                    .await
                    .unwrap();
                assert_eq!(data.data.as_ref(), b"lower");
            }
        });
        let t2 = tokio::spawn(async move {
            for _ in 0..50 {
                let ro = o2
                    .open(Request::default(), ino2, libc::O_RDONLY as u32)
                    .await
                    .unwrap();
                let data = o2
                    .read(Request::default(), ino2, ro.fh, 0, 32)
                    .await
                    .unwrap();
                assert_eq!(data.data.as_ref(), b"lower");
            }
        });
        let _ = tokio::join!(t1, t2);

        // Write in overlay1 must not affect overlay2.
        let wo = overlay1
            .open(Request::default(), ino1, libc::O_WRONLY as u32)
            .await
            .unwrap();
        overlay1
            .write(Request::default(), ino1, wo.fh, 0, b"upper1", 0, 0)
            .await
            .unwrap();

        let ro1 = overlay1
            .open(Request::default(), ino1, libc::O_RDONLY as u32)
            .await
            .unwrap();
        let d1 = overlay1
            .read(Request::default(), ino1, ro1.fh, 0, 32)
            .await
            .unwrap();
        assert_eq!(d1.data.as_ref(), b"upper1");

        let ro2 = overlay2
            .open(Request::default(), ino2, libc::O_RDONLY as u32)
            .await
            .unwrap();
        let d2 = overlay2
            .read(Request::default(), ino2, ro2.fh, 0, 32)
            .await
            .unwrap();
        assert_eq!(d2.data.as_ref(), b"lower");

        assert_eq!(
            upper1.read_file_by_name("hello.txt").await.unwrap(),
            b"upper1"
        );
        assert!(upper2.read_file_by_name("hello.txt").await.is_none());

        // Lower store content must remain unchanged.
        assert_eq!(dic.store.get_file_content(2).unwrap().to_vec(), b"lower");

        let _ = std::fs::remove_file(&marker_path);
        let _ = std::fs::remove_dir_all(&store_path);
    }

    fn fuse_test_prereqs_or_skip() -> bool {
        let uid = unsafe { libc::geteuid() };
        if uid != 0 {
            println!("Skipping: requires root privileges");
            return false;
        }

        if !std::path::Path::new("/dev/fuse").exists() {
            println!("Skipping: /dev/fuse not available");
            return false;
        }

        // AntaresFuse::unmount uses `fusermount -uz`.
        if std::process::Command::new("fusermount")
            .arg("--version")
            .output()
            .is_err()
        {
            println!("Skipping: fusermount not found");
            return false;
        }

        true
    }

    async fn retry_read(path: &std::path::Path) -> std::io::Result<Vec<u8>> {
        // FUSE mounts can become ready slightly after mount() returns; retry briefly.
        const RETRIES: usize = 20;
        const SLEEP_MS: u64 = 50;
        for attempt in 0..RETRIES {
            match std::fs::read(path) {
                Ok(v) => return Ok(v),
                Err(e) if attempt + 1 < RETRIES => {
                    tracing::debug!(
                        "retry_read({}): attempt {} failed: {}",
                        path.display(),
                        attempt + 1,
                        e
                    );
                    sleep(Duration::from_millis(SLEEP_MS)).await;
                }
                Err(e) => return Err(e),
            }
        }
        unreachable!()
    }

    /// Validate: when Dicfuse is used as the lower layer, writes copy-up into upper and do not mutate Dicfuse's read-only data.
    #[tokio::test]
    #[serial] // Avoid overlapping FUSE mounts in tests.
    async fn test_dicfuse_lower_copyup_does_not_mutate_lower() {
        if !fuse_test_prereqs_or_skip() {
            return;
        }

        if let Err(e) = config::init_config("./scorpio.toml") {
            if !e.contains("already initialized") {
                panic!("Failed to load config: {e}");
            }
        }

        let test_id = Uuid::new_v4();
        let base = PathBuf::from(format!("/tmp/antares_e2e_ro_{test_id}"));
        let _ = std::fs::remove_dir_all(&base);

        let mount = base.join("mnt");
        let upper = base.join("upper");
        let store_path = base.join("store");
        std::fs::create_dir_all(&store_path).unwrap();

        // Prepare a minimal Dicfuse store without any network calls.
        let dic =
            std::sync::Arc::new(Dicfuse::new_with_store_path(store_path.to_str().unwrap()).await);
        dic.store.insert_mock_item(1, 0, "", true).await; // root
        dic.store.insert_mock_item(2, 1, "hello.txt", false).await;
        dic.store.save_file(2, b"lower".to_vec());
        dic.store.load_db().await.unwrap();

        let mut fuse = AntaresFuse::new(mount.clone(), dic.clone(), upper.clone(), None)
            .await
            .unwrap();
        fuse.mount().await.unwrap();

        let mounted_file = mount.join("hello.txt");

        // Read from mount should return lower content initially.
        let before = retry_read(&mounted_file).await.unwrap();
        assert_eq!(before, b"lower");

        // Write should copy-up into upper and not mutate dicfuse store.
        std::fs::write(&mounted_file, b"upper").unwrap();

        let after = retry_read(&mounted_file).await.unwrap();
        assert_eq!(after, b"upper");

        let upper_file = upper.join("hello.txt");
        assert!(upper_file.exists(), "copy-up should create file in upper");
        assert_eq!(std::fs::read(&upper_file).unwrap(), b"upper");

        // Lower store content must remain unchanged.
        let lower_content = dic.store.get_file_content(2).unwrap().to_vec();
        assert_eq!(lower_content, b"lower");

        fuse.unmount().await.unwrap();
        let _ = std::fs::remove_dir_all(&base);
    }

    /// Validate: when multiple Antares instances share the same Dicfuse lower, reads are consistent and each mount's upper is isolated.
    #[tokio::test]
    #[serial] // Avoid overlapping FUSE mounts in tests.
    async fn test_concurrent_mounts_share_dicfuse_but_isolate_upper() {
        if !fuse_test_prereqs_or_skip() {
            return;
        }

        if let Err(e) = config::init_config("./scorpio.toml") {
            if !e.contains("already initialized") {
                panic!("Failed to load config: {e}");
            }
        }

        let test_id = Uuid::new_v4();
        let base = PathBuf::from(format!("/tmp/antares_e2e_concurrent_{test_id}"));
        let _ = std::fs::remove_dir_all(&base);

        let mount1 = base.join("mnt1");
        let mount2 = base.join("mnt2");
        let upper1 = base.join("upper1");
        let upper2 = base.join("upper2");
        let store_path = base.join("store");
        std::fs::create_dir_all(&store_path).unwrap();

        let dic =
            std::sync::Arc::new(Dicfuse::new_with_store_path(store_path.to_str().unwrap()).await);
        dic.store.insert_mock_item(1, 0, "", true).await; // root
        dic.store.insert_mock_item(2, 1, "hello.txt", false).await;
        dic.store.save_file(2, b"lower".to_vec());
        dic.store.load_db().await.unwrap();

        let mut fuse1 = AntaresFuse::new(mount1.clone(), dic.clone(), upper1.clone(), None)
            .await
            .unwrap();
        let mut fuse2 = AntaresFuse::new(mount2.clone(), dic.clone(), upper2.clone(), None)
            .await
            .unwrap();

        fuse1.mount().await.unwrap();
        fuse2.mount().await.unwrap();

        let file1 = mount1.join("hello.txt");
        let file2 = mount2.join("hello.txt");

        // Initial reads should come from lower in both mounts.
        assert_eq!(retry_read(&file1).await.unwrap(), b"lower");
        assert_eq!(retry_read(&file2).await.unwrap(), b"lower");

        // Write in mount1 should only affect upper1, not mount2.
        std::fs::write(&file1, b"upper1").unwrap();

        assert_eq!(retry_read(&file1).await.unwrap(), b"upper1");
        assert_eq!(retry_read(&file2).await.unwrap(), b"lower");

        assert_eq!(std::fs::read(upper1.join("hello.txt")).unwrap(), b"upper1");
        assert!(
            !upper2.join("hello.txt").exists(),
            "upper2 should remain untouched"
        );

        fuse1.unmount().await.unwrap();
        fuse2.unmount().await.unwrap();
        let _ = std::fs::remove_dir_all(&base);
    }

    #[tokio::test]
    #[ignore]
    // Requires FUSE/root. Direct run example:
    //   sudo -E cargo test --lib antares::fuse::tests::test_simple_passthrough_mount -- --exact --ignored --nocapture
    // For LLDB debug workflow, see `doc/test.md`.
    #[serial] // Serialize to avoid config initialization conflicts
    async fn test_simple_passthrough_mount() {
        // Simplified test using only passthrough layers (no Dicfuse)
        use std::sync::Arc;

        use libfuse_fs::{
            passthrough::{new_passthroughfs_layer, PassthroughArgs},
            unionfs::{config::Config, OverlayFs},
        };

        let uid = unsafe { libc::geteuid() };
        if uid != 0 {
            println!("Warning: This test requires root privileges");
            println!("Run with: sudo -E cargo test --lib antares::fuse::tests::test_simple_passthrough_mount -- --exact --ignored --nocapture");
            return;
        }

        let base = PathBuf::from("/tmp/antares_simple_test");
        let _ = std::fs::remove_dir_all(&base);

        let mount = base.join("mnt");
        let upper = base.join("upper");
        let lower1 = base.join("lower1");
        let lower2 = base.join("lower2");

        // Create directories and test files
        std::fs::create_dir_all(&mount).unwrap();
        std::fs::create_dir_all(&upper).unwrap();
        std::fs::create_dir_all(&lower1).unwrap();
        std::fs::create_dir_all(&lower2).unwrap();

        std::fs::write(lower1.join("file1.txt"), b"from lower1").unwrap();
        std::fs::write(lower2.join("file2.txt"), b"from lower2").unwrap();

        // Build overlay
        let lower1_layer = new_passthroughfs_layer(PassthroughArgs {
            root_dir: &lower1,
            mapping: None::<String>,
        })
        .await
        .unwrap();

        let lower2_layer = new_passthroughfs_layer(PassthroughArgs {
            root_dir: &lower2,
            mapping: None::<String>,
        })
        .await
        .unwrap();

        let upper_layer = new_passthroughfs_layer(PassthroughArgs {
            root_dir: &upper,
            mapping: None::<String>,
        })
        .await
        .unwrap();

        let cfg = Config {
            mountpoint: mount.clone(),
            do_import: true,
            ..Default::default()
        };

        let overlay = OverlayFs::new(
            Some(Arc::new(upper_layer)),
            vec![Arc::new(lower2_layer), Arc::new(lower1_layer)],
            cfg,
            1,
        )
        .unwrap();

        println!(
            "Mounting simple passthrough overlay at: {}",
            mount.display()
        );
        let handle = crate::server::mount_filesystem(overlay, mount.as_os_str()).await;

        // Spawn background task
        let fuse_task = tokio::spawn(async move {
            let _ = handle.await;
        });

        // Give it time to initialize
        sleep(Duration::from_millis(200)).await;

        println!("Mount successful!");
        println!("Mountpoint: {}", mount.display());
        println!("Try in another terminal: ls -la {}", mount.display());
        println!("You should see file1.txt and file2.txt");

        // Keep mounted for inspection
        sleep(Duration::from_secs(5)).await;

        // Unmount using lazy unmount to avoid blocking
        println!("Unmounting...");
        let output = tokio::process::Command::new("fusermount")
            .arg("-uz") // Use lazy unmount
            .arg(&mount)
            .output()
            .await
            .unwrap();

        if !output.status.success() {
            let error_msg = String::from_utf8_lossy(&output.stderr);
            // Check if the error is because the filesystem is not mounted
            if !error_msg.contains("not mounted") && !error_msg.contains("Invalid argument") {
                eprintln!("fusermount failed: {}", error_msg);
            }
        }

        // Wait for FUSE task to complete with timeout (don't wait indefinitely)
        let timeout_duration = tokio::time::Duration::from_secs(5);
        match tokio::time::timeout(timeout_duration, fuse_task).await {
            Ok(Ok(_)) => println!("FUSE task completed successfully"),
            Ok(Err(e)) => tracing::warn!("FUSE task panicked: {:?}", e),
            Err(_) => tracing::warn!(
                "FUSE task did not complete within {}s, continuing anyway",
                timeout_duration.as_secs()
            ),
        }
        println!("Unmount successful!");

        // cleanup
        let _ = std::fs::remove_dir_all(&base);
    }

    #[tokio::test]
    #[ignore] // Run with: sudo -E $(which cargo) test --lib antares::fuse::tests::test_run_mount -- --exact --ignored --nocapture
    async fn test_run_mount() {
        // Helper function to check if a file should be skipped in directory iteration
        let _should_skip_test_file = |name: &str| -> bool {
            name == "test_file.txt" || name == "created_file.txt" || name == "test_dir"
        };
        // Only LoggingFileSystem DEBUG
        use tracing_subscriber::EnvFilter;
        let _ = tracing_subscriber::fmt()
            .with_env_filter(
                EnvFilter::from_default_env()
                    .add_directive("libfuse_fs::passthrough::newlogfs=debug".parse().unwrap()),
            )
            .try_init();
        // Ignore "already initialized" error when running multiple tests
        if let Err(e) = config::init_config("./scorpio.toml") {
            if !e.contains("already initialized") {
                panic!("Failed to load config: {e}");
            }
        }
        // Check if we have necessary privileges
        let uid = unsafe { libc::geteuid() };
        if uid != 0 {
            println!("Warning: This test requires root privileges for open_by_handle_at");
            println!("Run with: sudo -E cargo test --lib antares::fuse::tests::test_run_mount -- --exact --ignored --nocapture");
            println!("Skipping test...");
            return;
        }

        let test_id = Uuid::new_v4();
        let base = PathBuf::from(format!("/tmp/antares_test_mount_{test_id}"));
        let _ = std::fs::remove_dir_all(&base);
        let mount = base.join("mnt");
        let upper = base.join("upper");
        let cl = base.join("cl");
        let store_path = base.join("store");
        std::fs::create_dir_all(&store_path).unwrap();

        let dic = Dicfuse::new_with_store_path(store_path.to_str().unwrap()).await;
        // Load directory tree synchronously - simpler and more efficient for tests
        // since we need the tree fully loaded before mount verification anyway
        println!("Loading directory tree...");
        crate::dicfuse::store::import_arc(dic.store.clone()).await;
        println!("Directory tree loaded, proceeding to mount");

        let mut fuse = AntaresFuse::new(
            mount.clone(),
            std::sync::Arc::new(dic),
            upper.clone(),
            Some(cl.clone()),
        )
        .await
        .unwrap();

        // Actually mount the filesystem
        println!("Mounting Antares overlay at: {}", mount.display());
        fuse.mount().await.unwrap();
        println!("Mount completed successfully");
        // mount() already verified accessibility via read_dir, so we can skip redundant checks
        // Keep mounted for inspection
        sleep(Duration::from_secs(30)).await;
        // Listen for Ctrl+C and unmount on signal
        println!("Press Ctrl+C to unmount and exit...");
        tokio::signal::ctrl_c()
            .await
            .expect("failed to listen for ctrl_c");
        println!("Ctrl+C received, unmounting...");
        fuse.unmount().await.unwrap();
        println!("Unmount successful!");
        //let _ = std::fs::remove_dir_all(&base);
    }

    #[tokio::test]
    #[ignore]
    // Requires FUSE/root. Direct run example:
    //   sudo -E cargo test --lib antares::fuse::tests::test_antares_mount -- --exact --ignored --nocapture
    // For no-run + LLDB debugging steps, see `doc/test.md`.
    #[serial] // Serialize to avoid config initialization conflicts
    async fn test_antares_mount() {
        // Set overall test timeout to 60 seconds
        let test_future = async {
            // Helper function to check if a file should be skipped in directory iteration
            let should_skip_test_file = |name: &str| -> bool {
                name == "test_file.txt" || name == "created_file.txt" || name == "test_dir"
            };
            // Only  LoggingFileSystem DEBUG
            use tracing_subscriber::EnvFilter;
            let _ = tracing_subscriber::fmt()
                .with_env_filter(
                    EnvFilter::from_default_env()
                        .add_directive("libfuse_fs::passthrough::newlogfs=debug".parse().unwrap()),
                )
                .try_init();
            // Ignore "already initialized" error when running multiple tests
            if let Err(e) = config::init_config("./scorpio.toml") {
                if !e.contains("already initialized") {
                    panic!("Failed to load config: {e}");
                }
            }
            // Check if we have necessary privileges
            let uid = unsafe { libc::geteuid() };
            if uid != 0 {
                println!("Warning: This test requires root privileges for open_by_handle_at");
                println!("Run with: sudo -E cargo test --lib antares::fuse::tests::test_antares_mount -- --exact --ignored --nocapture");
                println!("Skipping test...");
                return;
            }

            let test_id = Uuid::new_v4();
            let base = PathBuf::from(format!("/tmp/antares_test_mount_{test_id}"));
            let _ = std::fs::remove_dir_all(&base);
            let mount = base.join("mnt");
            let upper = base.join("upper");
            let cl = base.join("cl");
            let store_path = base.join("store");
            std::fs::create_dir_all(&store_path).unwrap();

            // Use isolated Dicfuse instance for testing to avoid database lock conflicts
            // In production, use DicfuseManager::global() to share the instance
            let dic =
                crate::dicfuse::Dicfuse::new_with_store_path(store_path.to_str().unwrap()).await;
            // Start background import_arc task to load directory tree asynchronously
            // This prevents blocking during FUSE operations (see blog post for details)
            tokio::spawn(crate::dicfuse::store::import_arc(dic.store.clone()));
            // Wait for Dicfuse to initialize and fetch directory tree from network
            // Increased wait time to allow for network requests to complete
            tokio::time::sleep(tokio::time::Duration::from_secs(5)).await;

            let mut fuse = AntaresFuse::new(
                mount.clone(),
                std::sync::Arc::new(dic),
                upper.clone(),
                Some(cl.clone()),
            )
            .await
            .unwrap();

            // Actually mount the filesystem
            println!("Mounting Antares overlay at: {}", mount.display());
            fuse.mount().await.unwrap();
            println!("Mount completed successfully");
            // mount() already verified accessibility via read_dir, so we can skip redundant checks

            // Let it run for a bit to ensure stability
            println!("Sleeping for 1 second...");
            sleep(Duration::from_secs(1)).await;
            println!("Sleep completed");

            // Test basic read operations
            println!("Testing basic read operations...");
            let read_dir_result = tokio::fs::read_dir(&mount).await;
            assert!(read_dir_result.is_ok(), "should be able to read directory");
            println!("✓ Directory read successful");

            // Test reading from read-only layer (Dicfuse)
            // Try to read files from Dicfuse lower layer if they exist
            println!("Testing read from read-only layer (Dicfuse)...");
            let mut dir_entries = read_dir_result.unwrap();
            let mut found_readonly_file = false;
            while let Some(entry) = dir_entries.next_entry().await.unwrap_or(None) {
                let path = entry.path();
                let file_name = path.file_name().unwrap().to_string_lossy();

                // Skip . and .. entries
                if file_name == "." || file_name == ".." {
                    continue;
                }

                // Try to read a file from the read-only layer
                if entry.file_type().await.unwrap().is_file() {
                    match tokio::fs::read(&path).await {
                        Ok(content) => {
                            println!(
                                "✓ Read file from read-only layer: {} ({} bytes)",
                                file_name,
                                content.len()
                            );
                            found_readonly_file = true;
                            break;
                        }
                        Err(e) => {
                            // File might not be loaded yet, skip
                            println!("⚠ Could not read {} from read-only layer: {}", file_name, e);
                        }
                    }
                }
            }
            if !found_readonly_file {
                println!("⚠ No files found in read-only layer (may still be loading)");
            }

            // Test basic write operations (create a file in upper layer)
            println!("Testing basic write operations...");
            let test_file = mount.join("test_file.txt");
            let test_content = b"Hello, FUSE!";

            // Write file
            tokio::fs::write(&test_file, test_content).await.unwrap();
            println!("✓ File write successful");

            // Read file back (this verifies file exists and content is correct)
            let read_content = tokio::fs::read(&test_file).await.unwrap();
            assert_eq!(read_content, test_content, "file content should match");
            println!("✓ File read successful, content matches");

            // Test directory creation
            println!("Testing directory creation...");
            let test_dir = mount.join("test_dir");
            tokio::fs::create_dir(&test_dir).await.unwrap();
            println!("✓ Directory creation successful");

            // Test file creation (create empty file first, then write to it)
            println!("Testing file creation...");
            let created_file = mount.join("created_file.txt");
            let created_content = b"Content written to created file";

            // Use tokio::fs::write which handles file creation, writing, and closing atomically
            tokio::fs::write(&created_file, created_content)
                .await
                .unwrap();
            println!("✓ File created and written successfully");

            // Verify the created file
            let read_created = tokio::fs::read(&created_file).await.unwrap();
            assert_eq!(
                read_created, created_content,
                "created file content should match"
            );
            println!("✓ File creation verification successful");

            // Test file in subdirectory
            let subdir_file = test_dir.join("subdir_file.txt");
            let subdir_content = b"File in subdirectory";
            tokio::fs::write(&subdir_file, subdir_content)
                .await
                .unwrap();
            let read_subdir_content = tokio::fs::read(&subdir_file).await.unwrap();
            assert_eq!(
                read_subdir_content, subdir_content,
                "subdirectory file content should match"
            );
            println!("✓ Subdirectory file operations successful");

            // Test Copy-Up mechanism: modify a file from read-only layer
            println!("Testing Copy-Up mechanism (modify read-only file)...");
            // Try to find a file from read-only layer and modify it
            let mut dir_entries = tokio::fs::read_dir(&mount).await.unwrap();
            let mut tested_copyup = false;
            while let Some(entry) = dir_entries.next_entry().await.unwrap_or(None) {
                let path = entry.path();
                let file_name = path.file_name().unwrap().to_string_lossy();

                // Skip . and .. entries, and files we created during this test
                if file_name == "." || file_name == ".." || should_skip_test_file(&file_name) {
                    continue;
                }

                // Try to modify a file from read-only layer (triggers Copy-Up)
                if entry.file_type().await.unwrap().is_file() {
                    match tokio::fs::read(&path).await {
                        Ok(_original_content) => {
                            // Modify the file (this should trigger Copy-Up)
                            let modified_content = b"Modified content from test";
                            tokio::fs::write(&path, modified_content).await.unwrap();

                            // Verify the modification
                            let read_modified = tokio::fs::read(&path).await.unwrap();
                            assert_eq!(
                                read_modified, modified_content,
                                "modified file content should match"
                            );

                            // Verify Copy-Up: file should now be in upper layer
                            let upper_file = upper.join(file_name.as_ref());
                            let upper_check = tokio::time::timeout(
                                Duration::from_secs(2),
                                tokio::fs::read(&upper_file),
                            )
                            .await;
                            match upper_check {
                                Ok(Ok(upper_content)) => {
                                    assert_eq!(
                                        upper_content, modified_content,
                                        "upper layer file should have modified content"
                                    );
                                    println!("✓ Copy-Up mechanism verified: {} copied to upper layer and modified", file_name);
                                    tested_copyup = true;
                                }
                                _ => {
                                    println!("⚠ Copy-Up verification skipped for {} (file may still be syncing)", file_name);
                                }
                            }
                            break;
                        }
                        Err(_) => {
                            // File might not be loaded yet, skip
                            continue;
                        }
                    }
                }
            }
            if !tested_copyup {
                println!("⚠ Copy-Up test skipped (no files from read-only layer available yet)");
            }

            // Verify files are in upper layer (use async check with timeout)
            println!("Verifying copy-up to upper layer for new files...");
            let upper_test_file = upper.join("test_file.txt");
            let upper_check = tokio::time::timeout(
                Duration::from_secs(2),
                tokio::fs::metadata(&upper_test_file),
            )
            .await;
            if upper_check.is_ok() && upper_check.unwrap().is_ok() {
                println!("✓ Copy-up to upper layer confirmed");
            } else {
                println!("⚠ Copy-up verification skipped (file may still be syncing)");
            }

            // Unmount
            println!("Unmounting...");
            fuse.unmount().await.unwrap();
            println!("Unmount successful!");

            // Cleanup
            let _ = std::fs::remove_dir_all(&base);
        };

        // Run test with timeout to prevent hanging
        // Increased timeout to 120s to account for Dicfuse network initialization
        match tokio::time::timeout(Duration::from_secs(120), test_future).await {
            Ok(_) => println!("✓ Test completed successfully"),
            Err(_) => panic!("Test timed out after 120 seconds - this may indicate a blocking operation or network issue"),
        }
    }

    #[tokio::test]
    #[ignore] // Requires root privileges for FUSE mount
    #[serial] // Serialize to avoid config initialization conflicts
    async fn creates_dirs_and_placeholder_overlay() {
        // Set overall test timeout to prevent hanging
        let test_future = async {
            // Ignore "already initialized" error when running multiple tests
            if let Err(e) = config::init_config("./scorpio.toml") {
                if !e.contains("already initialized") {
                    panic!("Failed to load config: {e}");
                }
            }

            // Check if we have necessary privileges
            let uid = unsafe { libc::geteuid() };
            if uid != 0 {
                println!("Warning: This test requires root privileges");
                println!("Run with: sudo -E cargo test --lib antares::fuse::tests::creates_dirs_and_placeholder_overlay -- --exact --ignored --nocapture");
                println!("Skipping test...");
                return;
            }

            let test_id = uuid::Uuid::new_v4();
            let base = PathBuf::from(format!("/tmp/antares_test_job1_{test_id}"));
            let _ = std::fs::remove_dir_all(&base);
            let mount = base.join("mnt");
            let upper = base.join("upper");
            let cl = base.join("cl");
            let store_path = base.join("store");
            std::fs::create_dir_all(&store_path).unwrap();

            // Use isolated Dicfuse instance for testing to avoid database lock conflicts
            // In production, use DicfuseManager::global() to share the instance
            let dic =
                crate::dicfuse::Dicfuse::new_with_store_path(store_path.to_str().unwrap()).await;
            // Start background import_arc task to load directory tree asynchronously
            // This prevents blocking during FUSE operations (see blog post for details)
            println!("Starting Dicfuse background import_arc task...");
            tokio::spawn(crate::dicfuse::store::import_arc(dic.store.clone()));

            // Wait for Dicfuse to initialize and fetch directory tree from network
            // Use wait_for_ready() with timeout instead of fixed sleep to handle variable load times
            println!("Waiting for Dicfuse to initialize (this may take time if loading large directory trees)...");
            let init_start = std::time::Instant::now();
            match tokio::time::timeout(
                tokio::time::Duration::from_secs(120), // 120 second timeout for large directory trees
                dic.store.wait_for_ready(),
            )
            .await
            {
                Ok(_) => {
                    let elapsed = init_start.elapsed();
                    println!(
                        "✓ Dicfuse initialized successfully after {:.2}s",
                        elapsed.as_secs_f64()
                    );
                }
                Err(_) => {
                    panic!(
                        "Dicfuse initialization timed out after 120 seconds. \
                        This may indicate:\n\
                        - Network issues preventing directory tree fetch\n\
                        - Very large directory tree (load_dir_depth={}) taking longer than expected\n\
                        - Background task may have failed\n\
                        Check logs for 'load_dir_depth' and 'Worker processing path' messages",
                        dic.store.max_depth()
                    );
                }
            }

            let mut fuse = AntaresFuse::new(
                mount.clone(),
                std::sync::Arc::new(dic),
                upper.clone(),
                Some(cl.clone()),
            )
            .await
            .unwrap();

            // Mount the overlay filesystem
            // mount() already verifies accessibility via read_dir, so we can skip redundant checks
            println!("Mounting Antares overlay at: {}", mount.display());
            fuse.mount().await.unwrap();
            println!("✓ Mount completed successfully");

            // Verify directories were created
            println!("Verifying directories exist...");

            // Use async metadata with timeout to avoid blocking on FUSE operations
            // PathBuf::exists() on FUSE mountpoint may trigger getattr/lookup which could block
            const CHECK_TIMEOUT_MS: u64 = 5000; // 5 second timeout per check

            // Check mount directory with timeout
            println!("  Checking mount directory: {}", mount.display());
            let mount_check_start = std::time::Instant::now();
            let mount_exists = match tokio::time::timeout(
                Duration::from_millis(CHECK_TIMEOUT_MS),
                tokio::fs::metadata(&mount),
            )
            .await
            {
                Ok(Ok(_)) => true,
                Ok(Err(_)) => false,
                Err(_) => {
                    let elapsed = mount_check_start.elapsed();
                    panic!("Mount directory check timed out after {:.2}s - FUSE operation may be blocked", elapsed.as_secs_f64());
                }
            };
            let mount_check_elapsed = mount_check_start.elapsed();
            println!(
                "  Mount directory check took {:.2}ms, exists: {}",
                mount_check_elapsed.as_secs_f64() * 1000.0,
                mount_exists
            );
            assert!(mount_exists, "mount directory should exist");
            println!("✓ Mount directory exists");

            // Check upper directory (regular filesystem, should be fast)
            println!("  Checking upper directory: {}", upper.display());
            let upper_check_start = std::time::Instant::now();
            let upper_exists = match tokio::time::timeout(
                Duration::from_millis(CHECK_TIMEOUT_MS),
                tokio::fs::metadata(&upper),
            )
            .await
            {
                Ok(Ok(_)) => true,
                Ok(Err(_)) => false,
                Err(_) => {
                    let elapsed = upper_check_start.elapsed();
                    panic!(
                        "Upper directory check timed out after {:.2}s",
                        elapsed.as_secs_f64()
                    );
                }
            };
            let upper_check_elapsed = upper_check_start.elapsed();
            println!(
                "  Upper directory check took {:.2}ms, exists: {}",
                upper_check_elapsed.as_secs_f64() * 1000.0,
                upper_exists
            );
            assert!(upper_exists, "upper directory should exist");
            println!("✓ Upper directory exists");

            // Check CL directory (regular filesystem, should be fast)
            println!("  Checking CL directory: {}", cl.display());
            let cl_check_start = std::time::Instant::now();
            let cl_exists = match tokio::time::timeout(
                Duration::from_millis(CHECK_TIMEOUT_MS),
                tokio::fs::metadata(&cl),
            )
            .await
            {
                Ok(Ok(_)) => true,
                Ok(Err(_)) => false,
                Err(_) => {
                    let elapsed = cl_check_start.elapsed();
                    panic!(
                        "CL directory check timed out after {:.2}s",
                        elapsed.as_secs_f64()
                    );
                }
            };
            let cl_check_elapsed = cl_check_start.elapsed();
            println!(
                "  CL directory check took {:.2}ms, exists: {}",
                cl_check_elapsed.as_secs_f64() * 1000.0,
                cl_exists
            );
            assert!(cl_exists, "cl directory should exist");
            println!("✓ CL directory exists");
            // Note: We don't call read_dir here because:
            // 1. mount() already verified accessibility via read_dir internally
            // 2. read_dir on FUSE mountpoint may trigger readdirplus which could block
            //    if Dicfuse is still loading data in the background
            // 3. This test focuses on verifying directory creation, not readdir functionality

            // Unmount
            println!("Unmounting...");
            let unmount_start = std::time::Instant::now();
            fuse.unmount().await.unwrap();
            let unmount_elapsed = unmount_start.elapsed();
            println!(
                "✓ Unmount successful (took {:.2}s)",
                unmount_elapsed.as_secs_f64()
            );

            // Cleanup
            let _ = std::fs::remove_dir_all(&base);
        };

        // Run test with timeout to prevent hanging
        // Increased timeout to 180s to account for Dicfuse network initialization and large directory trees
        match tokio::time::timeout(Duration::from_secs(180), test_future).await {
            Ok(_) => println!("✓ Test completed successfully"),
            Err(_) => panic!("Test timed out after 180 seconds - this may indicate:\n- Dicfuse background loading taking too long\n- Network issues\n- Very large directory tree (check load_dir_depth config)\nCheck logs for '[load_dir_depth]' messages to see loading progress"),
        }
    }

    /// Verify that creating a file in a deep directory path is reflected in the upper layer.
    ///
    /// Topology:
    /// - lower: one passthrough layer with a 3-level deep directory tree `a/b/c`.
    /// - upper: empty directory used as the writable layer.
    ///
    /// We create a file at `/mnt/a/b/c/created.txt` and then check that the file
    /// appears under `upper/a/b/c/created.txt` and does NOT exist in the lower tree.
    #[tokio::test]
    #[ignore]
    // Requires FUSE/root. Direct run example:
    //   sudo -E cargo test --lib antares::fuse::tests::deep_write_goes_to_upper -- --exact --ignored --nocapture
    // For LLDB-based debugging, follow the steps in `doc/test.md`.
    #[serial] // Serialize to avoid config initialization conflicts
    async fn deep_write_goes_to_upper() {
        use std::sync::Arc;

        use libfuse_fs::{
            passthrough::{new_passthroughfs_layer, PassthroughArgs},
            unionfs::{config::Config, OverlayFs},
        };
        use rfuse3::raw::logfs::LoggingFileSystem;
        // Only  LoggingFileSystem DEBUG
        use tracing_subscriber::EnvFilter;
        let _ = tracing_subscriber::fmt()
            .with_env_filter(
                EnvFilter::from_default_env()
                    .add_directive("libfuse_fs::passthrough::newlogfs=debug".parse().unwrap()),
            )
            .try_init();
        let uid = unsafe { libc::geteuid() };
        if uid != 0 {
            println!("Warning: This test requires root privileges for FUSE/open_by_handle_at");
            println!(
                "Run with: sudo -E cargo test --lib antares::fuse::tests::deep_write_goes_to_upper -- --exact --ignored --nocapture"
            );
            return;
        }

        let base = PathBuf::from("/tmp/antares_deep_overlay_test3");
        // Clean up any existing mount point first
        let mount = base.join("mnt");
        let _ = tokio::process::Command::new("fusermount")
            .arg("-uz")
            .arg(&mount)
            .output()
            .await;
        let _ = std::fs::remove_dir_all(&base);
        let mount = base.join("mnt");
        let upper = base.join("upper");
        let lower = base.join("lower");

        // Prepare directory layout: lower contains `a/b/c`, upper is empty.
        std::fs::create_dir_all(&mount).unwrap();
        std::fs::create_dir_all(&upper).unwrap();
        std::fs::create_dir_all(lower.join("a/b/c")).unwrap();

        // Build overlay: empty upper, single lower.
        let lower_layer = new_passthroughfs_layer(PassthroughArgs {
            root_dir: &lower,
            mapping: None::<String>,
        })
        .await
        .unwrap();

        let upper_layer = new_passthroughfs_layer(PassthroughArgs {
            root_dir: &upper,
            mapping: None::<String>,
        })
        .await
        .unwrap();

        let cfg = Config {
            mountpoint: mount.clone(),
            do_import: true,
            ..Default::default()
        };

        let overlay = OverlayFs::new(
            Some(Arc::new(upper_layer)),
            vec![Arc::new(lower_layer)],
            cfg,
            1,
        )
        .unwrap();

        println!("Mounting deep overlay at: {}", mount.display());
        let logfs = LoggingFileSystem::new(overlay);
        let handle = crate::server::mount_filesystem(logfs, mount.as_os_str()).await;

        // Run FUSE session in the background.
        let _fuse_task = tokio::spawn(async move {
            let _ = handle.await;
        });

        // Give the mount a moment to initialize.
        tokio::time::sleep(tokio::time::Duration::from_secs(2)).await;

        // Verify mountpoint is accessible
        let metadata = tokio::fs::metadata(&mount).await;
        assert!(metadata.is_ok(), "Mountpoint should be accessible");

        // Test: Create a file in a deep directory path
        let test_file = mount.join("a/b/c/created.txt");
        tokio::fs::write(&test_file, b"test content").await.unwrap();

        // Verify file exists in mountpoint
        let content = tokio::fs::read(&test_file).await.unwrap();
        assert_eq!(content, b"test content");

        // Verify file exists in upper layer (copy-up happened)
        let upper_file = upper.join("a/b/c/created.txt");
        let upper_content = tokio::fs::read(&upper_file).await.unwrap();
        assert_eq!(
            upper_content, b"test content",
            "File should be copied up to upper layer"
        );

        // Verify file does NOT exist in lower layer
        let lower_file = lower.join("a/b/c/created.txt");
        assert!(!lower_file.exists(), "File should NOT exist in lower layer");

        // Unmount
        let _ = tokio::process::Command::new("fusermount")
            .arg("-uz")
            .arg(&mount)
            .output()
            .await;

        // Cleanup
        let _ = std::fs::remove_dir_all(&base);
    }

    /// Test that copy-up works correctly when modifying files from the lower layer.
    /// This test specifically verifies that `do_getattr_helper` is properly implemented,
    /// as copy-up requires getting file attributes from the lower layer.
    #[tokio::test]
    #[ignore] // Requires root privileges and network access
    #[serial]
    async fn test_copyup_modifies_lower_file() {
        use tracing_subscriber::EnvFilter;
        let _ = tracing_subscriber::fmt()
            .with_env_filter(EnvFilter::from_default_env())
            .try_init();

        if let Err(e) = config::init_config("./scorpio.toml") {
            if !e.contains("already initialized") {
                panic!("Failed to load config: {e}");
            }
        }

        let uid = unsafe { libc::geteuid() };
        if uid != 0 {
            println!("Warning: This test requires root privileges");
            println!("Run with: sudo -E cargo test -p scorpio --lib antares::fuse::tests::test_copyup_modifies_lower_file -- --exact --ignored --nocapture");
            println!("Skipping test...");
            return;
        }

        let test_id = Uuid::new_v4();
        let base = PathBuf::from(format!("/tmp/antares_copyup_test_{test_id}"));
        let _ = std::fs::remove_dir_all(&base);
        let mount = base.join("mnt");
        let upper = base.join("upper");
        let cl = base.join("cl");
        let store_path = base.join("store");
        std::fs::create_dir_all(&store_path).unwrap();

        // Create Dicfuse and wait for directory tree to load
        let dic = crate::dicfuse::Dicfuse::new_with_store_path(store_path.to_str().unwrap()).await;

        println!("Loading directory tree synchronously...");
        crate::dicfuse::store::import_arc(dic.store.clone()).await;
        println!("Directory tree loaded");

        let mut fuse = AntaresFuse::new(
            mount.clone(),
            std::sync::Arc::new(dic),
            upper.clone(),
            Some(cl.clone()),
        )
        .await
        .unwrap();

        println!("Mounting Antares overlay at: {}", mount.display());
        fuse.mount().await.unwrap();
        println!("Mount completed");

        // Give mount a moment to stabilize
        sleep(Duration::from_millis(500)).await;

        // Recursively find a file from the lower layer (Dicfuse)
        // We need to search in subdirectories since root may only have directories
        async fn find_file_recursive(
            dir: &std::path::Path,
            upper: &std::path::Path,
            mount: &std::path::Path,
            depth: usize,
        ) -> Option<std::path::PathBuf> {
            if depth > 3 {
                return None; // Don't go too deep
            }

            let mut entries = match tokio::fs::read_dir(dir).await {
                Ok(e) => e,
                Err(_) => return None,
            };

            while let Ok(Some(entry)) = entries.next_entry().await {
                let path = entry.path();
                let file_type = match entry.file_type().await {
                    Ok(ft) => ft,
                    Err(_) => continue,
                };

                if file_type.is_file() {
                    // Get relative path from mount point
                    let rel_path = path.strip_prefix(mount).unwrap_or(&path);
                    let upper_path = upper.join(rel_path);
                    if !upper_path.exists() {
                        return Some(path);
                    }
                } else if file_type.is_dir() {
                    // Recurse into subdirectory
                    if let Some(found) =
                        Box::pin(find_file_recursive(&path, upper, mount, depth + 1)).await
                    {
                        return Some(found);
                    }
                }
            }
            None
        }

        println!("Searching for a file in lower layer (Dicfuse)...");
        let found_lower_file = find_file_recursive(&mount, &upper, &mount, 0).await;

        if let Some(lower_file) = found_lower_file {
            // Get relative path from mount point for correct upper layer path
            let rel_path = lower_file.strip_prefix(&mount).unwrap();
            println!("Found lower layer file: {}", rel_path.display());

            // Read original content
            let original_content = tokio::fs::read(&lower_file).await.unwrap();
            println!("Original content length: {} bytes", original_content.len());

            // Modify the file - THIS TRIGGERS COPY-UP
            // Copy-up calls do_getattr_helper to get file attributes
            let modified_content = b"MODIFIED BY TEST - copy-up successful!";
            println!("Attempting to modify file (this triggers copy-up)...");

            match tokio::fs::write(&lower_file, modified_content).await {
                Ok(_) => {
                    println!("✓ File modification successful");

                    // Verify modification persisted
                    let read_back = tokio::fs::read(&lower_file).await.unwrap();
                    assert_eq!(
                        read_back, modified_content,
                        "Modified content should be readable"
                    );
                    println!("✓ Modified content verified");

                    // Verify copy-up: file should now be in upper layer (use relative path)
                    let upper_file = upper.join(rel_path);
                    assert!(
                        upper_file.exists(),
                        "File should be copied to upper layer after modification: {}",
                        upper_file.display()
                    );

                    let upper_content = tokio::fs::read(&upper_file).await.unwrap();
                    assert_eq!(
                        upper_content, modified_content,
                        "Upper layer should have modified content"
                    );
                    println!(
                        "✓ Copy-up verified: {} copied to upper layer with modified content",
                        rel_path.display()
                    );
                }
                Err(e) => {
                    panic!("Failed to modify lower layer file - copy-up failed: {}", e);
                }
            }
        } else {
            println!("⚠ No files found in lower layer - test inconclusive");
            println!("  This may happen if Dicfuse couldn't load files from remote server");
        }

        // Cleanup
        println!("Unmounting...");
        fuse.unmount().await.unwrap();
        println!("✓ Test completed");

        let _ = std::fs::remove_dir_all(&base);
    }

    /// Test that mkdir works in a lower layer directory (requires directory copy-up).
    /// This simulates Buck2's behavior: creating buck-out/v2 inside a directory from Dicfuse.
    ///
    /// The test verifies:
    /// 1. We can find a directory from lower layer (Dicfuse)
    /// 2. We can create a new subdirectory inside it (triggers directory copy-up)
    /// 3. The new directory appears in the upper layer
    #[tokio::test]
    #[ignore] // Requires root privileges and network access
    #[serial]
    async fn test_mkdir_in_lower_layer_directory() {
        use tracing_subscriber::EnvFilter;
        let _ = tracing_subscriber::fmt()
            .with_env_filter(EnvFilter::from_default_env())
            .try_init();

        if let Err(e) = config::init_config("./scorpio.toml") {
            if !e.contains("already initialized") {
                panic!("Failed to load config: {e}");
            }
        }

        let uid = unsafe { libc::geteuid() };
        if uid != 0 {
            println!("Warning: This test requires root privileges");
            println!("Run with: sudo -E cargo test -p scorpio --lib antares::fuse::tests::test_mkdir_in_lower_layer_directory -- --exact --ignored --nocapture");
            println!("Skipping test...");
            return;
        }

        let test_id = Uuid::new_v4();
        let base = PathBuf::from(format!("/tmp/antares_mkdir_test_{test_id}"));
        let _ = std::fs::remove_dir_all(&base);
        let mount = base.join("mnt");
        let upper = base.join("upper");
        let cl = base.join("cl");
        let store_path = base.join("store");
        std::fs::create_dir_all(&store_path).unwrap();

        // Create Dicfuse and wait for directory tree to load
        let dic = crate::dicfuse::Dicfuse::new_with_store_path(store_path.to_str().unwrap()).await;

        println!("Loading directory tree synchronously...");
        crate::dicfuse::store::import_arc(dic.store.clone()).await;
        println!("Directory tree loaded");

        let mut fuse = AntaresFuse::new(
            mount.clone(),
            std::sync::Arc::new(dic),
            upper.clone(),
            Some(cl.clone()),
        )
        .await
        .unwrap();

        println!("Mounting Antares overlay at: {}", mount.display());
        fuse.mount().await.unwrap();
        println!("Mount completed");

        // Give mount a moment to stabilize
        sleep(Duration::from_millis(500)).await;

        // Find a directory from the lower layer (Dicfuse)
        async fn find_dir_recursive(
            dir: &std::path::Path,
            upper: &std::path::Path,
            mount: &std::path::Path,
            depth: usize,
        ) -> Option<std::path::PathBuf> {
            if depth > 2 {
                return None; // Don't go too deep
            }

            let mut entries = match tokio::fs::read_dir(dir).await {
                Ok(e) => e,
                Err(_) => return None,
            };

            while let Ok(Some(entry)) = entries.next_entry().await {
                let path = entry.path();
                let file_type = match entry.file_type().await {
                    Ok(ft) => ft,
                    Err(_) => continue,
                };

                if file_type.is_dir() {
                    // Get relative path from mount point
                    let rel_path = path.strip_prefix(mount).unwrap_or(&path);
                    let upper_path = upper.join(rel_path);

                    // We want a directory that exists in lower but NOT in upper
                    if !upper_path.exists() {
                        return Some(path);
                    }

                    // Recurse into subdirectory
                    if let Some(found) =
                        Box::pin(find_dir_recursive(&path, upper, mount, depth + 1)).await
                    {
                        return Some(found);
                    }
                }
            }
            None
        }

        println!("Searching for a directory in lower layer (Dicfuse)...");
        let found_lower_dir = find_dir_recursive(&mount, &upper, &mount, 0).await;

        if let Some(lower_dir) = found_lower_dir {
            let rel_path = lower_dir.strip_prefix(&mount).unwrap();
            println!("Found lower layer directory: {}", rel_path.display());

            // Try to create a new subdirectory inside it
            // This simulates Buck2 creating buck-out/v2
            let new_subdir = lower_dir.join("test-subdir-created-by-test");
            println!(
                "Attempting to create subdirectory: {}",
                new_subdir.strip_prefix(&mount).unwrap().display()
            );
            println!("This will trigger directory copy-up...");

            match tokio::fs::create_dir(&new_subdir).await {
                Ok(_) => {
                    println!("✓ Subdirectory creation successful!");

                    // Verify the directory exists (use async with timeout to avoid blocking)
                    match tokio::time::timeout(
                        Duration::from_secs(2),
                        tokio::fs::metadata(&new_subdir),
                    )
                    .await
                    {
                        Ok(Ok(meta)) if meta.is_dir() => {
                            println!("✓ Subdirectory exists in mountpoint");
                        }
                        _ => {
                            println!(
                                "⚠ Could not verify subdirectory in mountpoint (timeout or error)"
                            );
                        }
                    }

                    // Verify it's in upper layer (copy-up happened for parent directory)
                    // Use std::fs for upper layer since it's not through FUSE
                    let upper_new_subdir = upper.join(rel_path).join("test-subdir-created-by-test");

                    // Give filesystem a moment to sync
                    sleep(Duration::from_millis(100)).await;

                    if upper_new_subdir.exists() {
                        println!(
                            "✓ Directory copy-up verified: new subdirectory exists in upper layer"
                        );
                        println!("  Upper path: {}", upper_new_subdir.display());
                    } else {
                        println!(
                            "⚠ New subdirectory not found in upper layer (may be a timing issue)"
                        );
                        println!("  Expected: {}", upper_new_subdir.display());
                    }

                    // Test creating a file inside the new directory (with timeout)
                    let test_file = new_subdir.join("test.txt");
                    match tokio::time::timeout(
                        Duration::from_secs(2),
                        tokio::fs::write(&test_file, b"test content"),
                    )
                    .await
                    {
                        Ok(Ok(_)) => {
                            println!("✓ Created file inside new subdirectory");

                            // Verify file content (with timeout)
                            match tokio::time::timeout(
                                Duration::from_secs(2),
                                tokio::fs::read(&test_file),
                            )
                            .await
                            {
                                Ok(Ok(content)) => {
                                    assert_eq!(content, b"test content");
                                    println!("✓ File content verified");
                                }
                                _ => {
                                    println!("⚠ Could not verify file content (timeout)");
                                }
                            }
                        }
                        _ => {
                            println!("⚠ Could not create file inside new subdirectory (timeout)");
                        }
                    }
                }
                Err(e) => {
                    println!("✗ Failed to create subdirectory in lower layer directory!");
                    println!(
                        "  Error: {} (os error {})",
                        e,
                        e.raw_os_error().unwrap_or(-1)
                    );
                    println!("  This indicates directory copy-up is not working correctly.");
                    println!(
                        "  The OverlayFS should copy the parent directory to upper layer first,"
                    );
                    println!("  then create the new subdirectory there.");
                    panic!("mkdir in lower layer directory failed: {}", e);
                }
            }
        } else {
            println!("⚠ No directories found in lower layer - test inconclusive");
            println!("  This may happen if Dicfuse couldn't load directories from remote server");
        }

        // Cleanup
        println!("Unmounting...");
        fuse.unmount().await.unwrap();
        println!("✓ Test completed");

        let _ = std::fs::remove_dir_all(&base);
    }
}