aboutsummaryrefslogtreecommitdiff
path: root/crates/fparkan-vfs/src/lib.rs
blob: 9ca57daa0fb9b42b43abfe644eedd59868d568fd (plain) (blame)
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
#![forbid(unsafe_code)]
//! Virtual filesystem ports for resource loading.

use fparkan_binary::{sha256, Sha256Digest};
use fparkan_path::{ascii_lookup_key, join_under, NormalizedPath};
use std::collections::BTreeMap;
use std::fs;
use std::path::{Path, PathBuf};
use std::sync::{Arc, Mutex};
use std::time::SystemTime;
#[cfg(unix)]
use std::os::unix::fs::MetadataExt;
#[cfg(windows)]
use std::os::windows::fs::MetadataExt;

/// VFS metadata.
#[derive(Clone, Debug, Eq, PartialEq)]
pub struct VfsMetadata {
    /// Byte length.
    pub len: u64,
    /// SHA-256 content fingerprint for cache invalidation.
    pub fingerprint: Sha256Digest,
}

/// VFS entry.
#[derive(Clone, Debug, Eq, PartialEq)]
pub struct VfsEntry {
    /// Path.
    pub path: NormalizedPath,
    /// Metadata.
    pub metadata: VfsMetadata,
}

/// VFS error.
#[derive(Debug)]
pub enum VfsError {
    /// Missing entry.
    NotFound(String),
    /// Ambiguous host path.
    Ambiguous(String),
    /// I/O error.
    Io(std::io::Error),
    /// Invalid path.
    Path,
}

impl std::fmt::Display for VfsError {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match self {
            Self::NotFound(path) => write!(f, "not found: {path}"),
            Self::Ambiguous(path) => write!(f, "ambiguous host path: {path}"),
            Self::Io(err) => write!(f, "{err}"),
            Self::Path => write!(f, "invalid path"),
        }
    }
}

impl std::error::Error for VfsError {}

/// Resource VFS.
pub trait Vfs: Send + Sync {
    /// Reads metadata.
    ///
    /// # Errors
    ///
    /// Returns [`VfsError`] when the path is invalid, missing, or cannot be
    /// inspected by the backing store.
    fn metadata(&self, path: &NormalizedPath) -> Result<VfsMetadata, VfsError>;
    /// Reads bytes.
    ///
    /// # Errors
    ///
    /// Returns [`VfsError`] when the path is invalid, missing, or cannot be
    /// read by the backing store.
    fn read(&self, path: &NormalizedPath) -> Result<Arc<[u8]>, VfsError>;
    /// Lists entries below prefix.
    ///
    /// # Errors
    ///
    /// Returns [`VfsError`] when the prefix is invalid, missing, or cannot be
    /// traversed by the backing store.
    fn list(&self, prefix: &NormalizedPath) -> Result<Vec<VfsEntry>, VfsError>;
}

/// Host directory VFS.
#[derive(Clone, Debug)]
pub struct DirectoryVfs {
    root: PathBuf,
    fingerprint_cache: Arc<Mutex<BTreeMap<PathBuf, CachedHostFingerprint>>>,
}

impl DirectoryVfs {
    /// Creates a directory VFS.
    #[must_use]
    pub fn new(root: impl AsRef<Path>) -> Self {
        Self {
            root: root.as_ref().to_path_buf(),
            fingerprint_cache: Arc::default(),
        }
    }

    fn host_path(&self, path: &NormalizedPath) -> Result<PathBuf, VfsError> {
        join_under(&self.root, path).map_err(|_| VfsError::Path)?;
        resolve_casefolded(&self.root, path.as_str())
    }

    fn metadata_from_host_file(&self, path: &Path) -> Result<VfsMetadata, VfsError> {
        let metadata = fs::symlink_metadata(path).map_err(VfsError::Io)?;
        metadata_from_host_file_with_cache(path, &metadata, &self.fingerprint_cache)
    }
}

#[derive(Clone, Debug, Eq, PartialEq)]
struct CachedHostFingerprint {
    len: u64,
    modified: Option<SystemTime>,
    identity: Option<u64>,
    fingerprint: Sha256Digest,
}

impl Vfs for DirectoryVfs {
    fn metadata(&self, path: &NormalizedPath) -> Result<VfsMetadata, VfsError> {
        self.metadata_from_host_file(&self.host_path(path)?)
    }

    fn read(&self, path: &NormalizedPath) -> Result<Arc<[u8]>, VfsError> {
        let host = self.host_path(path)?;
        let pre_metadata = fs::symlink_metadata(&host).map_err(VfsError::Io)?;
        if pre_metadata.file_type().is_symlink() || !pre_metadata.is_file() {
            return Err(VfsError::Path);
        }
        let pre_identity = file_identity(&pre_metadata);
        let pre_len = pre_metadata.len();
        let pre_modified = pre_metadata.modified().ok();
        let bytes = fs::read(&host).map_err(VfsError::Io)?;
        let post_metadata = fs::symlink_metadata(&host).map_err(VfsError::Io)?;
        if post_metadata.file_type().is_symlink()
            || !post_metadata.is_file()
            || post_metadata.len() != pre_len
            || post_metadata.modified().ok() != pre_modified
            || file_identity(&post_metadata) != pre_identity
        {
            return Err(VfsError::Path);
        }
        Ok(Arc::from(bytes.into_boxed_slice()))
    }

    fn list(&self, prefix: &NormalizedPath) -> Result<Vec<VfsEntry>, VfsError> {
        let base = self.host_path(prefix)?;
        let mut entries = Vec::new();
        if base.is_file() {
            let metadata = fs::symlink_metadata(&base).map_err(VfsError::Io)?;
            entries.push(VfsEntry {
                path: prefix.clone(),
                metadata: metadata_from_host_file_with_cache(
                    &base,
                    &metadata,
                    &self.fingerprint_cache,
                )?,
            });
            return Ok(entries);
        }
        list_recursive(&self.root, &base, &self.fingerprint_cache, &mut entries)?;
        entries.sort_by(|a, b| a.path.as_str().cmp(b.path.as_str()));
        Ok(entries)
    }
}

fn resolve_casefolded(root: &Path, normalized: &str) -> Result<PathBuf, VfsError> {
    let mut current = root.to_path_buf();
    for segment in normalized.split('/') {
        let read_dir = fs::read_dir(&current).map_err(VfsError::Io)?;
        let mut matches = Vec::new();
        for entry in read_dir {
            let entry = entry.map_err(VfsError::Io)?;
            let name = entry.file_name();
            let Some(name) = name.to_str() else {
                continue;
            };
            if name.eq_ignore_ascii_case(segment) {
                if entry.file_type().map_err(VfsError::Io)?.is_symlink() {
                    return Err(VfsError::Path);
                }
                matches.push(entry.path());
            }
        }
        current = select_casefolded_match(normalized, &current, segment, matches)?;
    }
    Ok(current)
}

fn select_casefolded_match(
    normalized: &str,
    current: &Path,
    segment: &str,
    mut matches: Vec<PathBuf>,
) -> Result<PathBuf, VfsError> {
    matches.sort();
    match matches.len() {
        0 => Err(VfsError::NotFound(normalized.to_string())),
        1 => Ok(matches.remove(0)),
        _ => Err(VfsError::Ambiguous(format!(
            "{}/{}",
            current.display(),
            segment
        ))),
    }
}

fn list_recursive(
    root: &Path,
    dir: &Path,
    fingerprint_cache: &Mutex<BTreeMap<PathBuf, CachedHostFingerprint>>,
    out: &mut Vec<VfsEntry>,
) -> Result<(), VfsError> {
    let read_dir = fs::read_dir(dir).map_err(VfsError::Io)?;
    let mut children = Vec::new();
    for entry in read_dir {
        let entry = entry.map_err(VfsError::Io)?;
        children.push(entry.path());
    }
    children.sort();
    for child in children {
        let metadata = fs::symlink_metadata(&child).map_err(VfsError::Io)?;
        if metadata.file_type().is_symlink() {
            return Err(VfsError::Path);
        }
        if metadata.is_dir() {
            list_recursive(root, &child, fingerprint_cache, out)?;
            continue;
        }
        if !metadata.is_file() {
            continue;
        }
        let rel = child.strip_prefix(root).map_err(|_| VfsError::Path)?;
        let rel_text = rel.to_str().ok_or(VfsError::Path)?;
        let path = fparkan_path::normalize_relative(
            rel_text.as_bytes(),
            fparkan_path::PathPolicy::HostCompatible,
        )
        .map_err(|_| VfsError::Path)?;
        out.push(VfsEntry {
            path,
            metadata: metadata_from_host_file_with_cache(&child, &metadata, fingerprint_cache)?,
        });
    }
    Ok(())
}

fn metadata_from_host_file_with_cache(
    path: &Path,
    metadata: &fs::Metadata,
    fingerprint_cache: &Mutex<BTreeMap<PathBuf, CachedHostFingerprint>>,
) -> Result<VfsMetadata, VfsError> {
    if !metadata.is_file() {
        return Err(VfsError::Path);
    }
    let len = metadata.len();
    let modified = metadata.modified().ok();
    if let Some(cached) = fingerprint_cache
        .lock()
        .map_err(|_| VfsError::Path)?
        .get(path)
        .cloned()
        .filter(|cached| {
            cached.len == len
                && cached.modified == modified
                && cached.identity == file_identity(metadata)
        })
    {
        return Ok(VfsMetadata {
            len,
            fingerprint: cached.fingerprint,
        });
    }

    let bytes = fs::read(path).map_err(VfsError::Io)?;
    let fingerprint = sha256(&bytes);
    fingerprint_cache
        .lock()
        .map_err(|_| VfsError::Path)?
        .insert(
            path.to_path_buf(),
            CachedHostFingerprint {
                len,
                modified,
                identity: file_identity(metadata),
                fingerprint,
            },
        );
    Ok(VfsMetadata { len, fingerprint })
}

/// In-memory VFS.
#[derive(Clone, Debug, Default)]
pub struct MemoryVfs {
    files: BTreeMap<Vec<u8>, Arc<[u8]>>,
    lookup: BTreeMap<Vec<u8>, Vec<Vec<u8>>>,
}

impl MemoryVfs {
    /// Inserts a file.
    #[allow(clippy::needless_pass_by_value)]
    pub fn insert(&mut self, path: NormalizedPath, bytes: Arc<[u8]>) {
        let path = path.as_bytes().to_vec();
        self.files.insert(path, bytes);
        self.rebuild_lookup();
    }

    fn rebuild_lookup(&mut self) {
        self.lookup.clear();
        for path in self.files.keys() {
            self.lookup
                .entry(ascii_lookup_key(path).0)
                .or_default()
                .push(path.clone());
        }
        for paths in self.lookup.values_mut() {
            paths.sort();
        }
    }

    fn resolve_path(&self, path: &NormalizedPath) -> Result<&[u8], VfsError> {
        let key = ascii_lookup_key(path.as_bytes()).0;
        let matches = self
            .lookup
            .get(&key)
            .ok_or_else(|| VfsError::NotFound(path.as_str().to_string()))?;
        match matches.as_slice() {
            [single] => Ok(single.as_slice()),
            [] => Err(VfsError::NotFound(path.as_str().to_string())),
            _ => Err(VfsError::Ambiguous(path.as_str().to_string())),
        }
    }
}

#[cfg(unix)]
fn file_identity(metadata: &fs::Metadata) -> Option<u64> {
    Some((metadata.dev() as u64).rotate_left(32) ^ metadata.ino())
}

#[cfg(windows)]
fn file_identity(metadata: &fs::Metadata) -> Option<u64> {
    Some(
        (metadata.volume_serial_number() as u64).rotate_left(40)
            ^ ((metadata.file_index_high() as u64) << 32)
            ^ metadata.file_index_low() as u64,
    )
}

#[cfg(not(any(unix, windows)))]
fn file_identity(_metadata: &fs::Metadata) -> Option<u64> {
    None
}

impl Vfs for MemoryVfs {
    fn metadata(&self, path: &NormalizedPath) -> Result<VfsMetadata, VfsError> {
        let resolved = self.resolve_path(path)?;
        let bytes = self
            .files
            .get(resolved)
            .ok_or_else(|| VfsError::NotFound(path.as_str().to_string()))?;
        Ok(VfsMetadata {
            len: bytes.len() as u64,
            fingerprint: sha256(bytes),
        })
    }

    fn read(&self, path: &NormalizedPath) -> Result<Arc<[u8]>, VfsError> {
        let resolved = self.resolve_path(path)?;
        self.files
            .get(resolved)
            .cloned()
            .ok_or_else(|| VfsError::NotFound(path.as_str().to_string()))
    }

    fn list(&self, prefix: &NormalizedPath) -> Result<Vec<VfsEntry>, VfsError> {
        let mut out = Vec::new();
        for (path, bytes) in &self.files {
            if has_segment_boundary_prefix_bytes(path, prefix.as_bytes()) {
                let normalized = fparkan_path::normalize_relative(
                    path,
                    fparkan_path::PathPolicy::StrictLegacy,
                )
                .map_err(|_| VfsError::Path)?;
                out.push(VfsEntry {
                    path: normalized,
                    metadata: VfsMetadata {
                        len: bytes.len() as u64,
                        fingerprint: sha256(bytes),
                    },
                });
            }
        }
        Ok(out)
    }
}

fn has_segment_boundary_prefix_bytes(haystack: &[u8], needle: &[u8]) -> bool {
    if haystack.len() < needle.len() {
        return false;
    }
    if haystack.len() == needle.len() {
        return haystack
            .iter()
            .zip(needle.iter())
            .all(|(left, right)| left.eq_ignore_ascii_case(right));
    }
    if haystack[needle.len()] != b'/' {
        return false;
    }
    haystack[..needle.len()]
        .iter()
        .zip(needle.iter())
        .all(|(left, right)| left.eq_ignore_ascii_case(right))
}

/// Layered VFS with deterministic first-layer precedence.
#[derive(Clone, Default)]
pub struct OverlayVfs {
    layers: Vec<Arc<dyn Vfs>>,
}

impl std::fmt::Debug for OverlayVfs {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        f.debug_struct("OverlayVfs")
            .field("layers", &self.layers.len())
            .finish()
    }
}

impl OverlayVfs {
    /// Creates an empty overlay.
    #[must_use]
    pub fn new() -> Self {
        Self::default()
    }

    /// Creates an overlay from ordered layers.
    #[must_use]
    pub fn from_layers(layers: Vec<Arc<dyn Vfs>>) -> Self {
        Self { layers }
    }

    /// Appends a lower-priority layer.
    pub fn push_layer(&mut self, layer: Arc<dyn Vfs>) {
        self.layers.push(layer);
    }
}

impl Vfs for OverlayVfs {
    fn metadata(&self, path: &NormalizedPath) -> Result<VfsMetadata, VfsError> {
        for layer in &self.layers {
            match layer.metadata(path) {
                Ok(metadata) => return Ok(metadata),
                Err(VfsError::NotFound(_)) => {}
                Err(err) => return Err(err),
            }
        }
        Err(VfsError::NotFound(path.as_str().to_string()))
    }

    fn read(&self, path: &NormalizedPath) -> Result<Arc<[u8]>, VfsError> {
        for layer in &self.layers {
            match layer.read(path) {
                Ok(bytes) => return Ok(bytes),
                Err(VfsError::NotFound(_)) => {}
                Err(err) => return Err(err),
            }
        }
        Err(VfsError::NotFound(path.as_str().to_string()))
    }

    fn list(&self, prefix: &NormalizedPath) -> Result<Vec<VfsEntry>, VfsError> {
        let mut by_key = BTreeMap::new();
        for layer in &self.layers {
            match layer.list(prefix) {
                Ok(entries) => {
                    for entry in entries {
                        let key = entry.path.as_str().to_ascii_uppercase();
                        by_key.entry(key).or_insert(entry);
                    }
                }
                Err(VfsError::NotFound(_)) => {}
                Err(err) => return Err(err),
            }
        }
        let mut entries: Vec<_> = by_key.into_values().collect();
        entries.sort_by(|a, b| a.path.as_str().cmp(b.path.as_str()));
        Ok(entries)
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use fparkan_path::{normalize_relative, PathPolicy};

    #[test]
    fn directory_vfs_resolves_ascii_casefolded_segments() {
        let root = unique_test_dir("casefold");
        let dir = root.join("data").join("MAPS").join("Tut_1");
        std::fs::create_dir_all(&dir).expect("mkdir");
        std::fs::write(dir.join("Land.msh"), b"mesh").expect("write");

        let vfs = DirectoryVfs::new(&root);
        let path = normalize_relative(b"DATA/maps/tut_1/land.MSH", PathPolicy::StrictLegacy)
            .expect("path");
        assert_eq!(vfs.read(&path).expect("read").as_ref(), b"mesh");

        std::fs::remove_dir_all(root).expect("cleanup");
    }

    #[test]
    fn directory_vfs_reports_casefold_ambiguity_even_for_exact_host_path() {
        let root = unique_test_dir("casefold-ambiguous");
        std::fs::create_dir_all(root.join("Data")).expect("mkdir first");
        std::fs::create_dir_all(root.join("data")).expect("mkdir second");
        std::fs::write(root.join("Data").join("File.bin"), b"first").expect("write first");
        std::fs::write(root.join("data").join("File.bin"), b"second").expect("write second");
        let collision_count = std::fs::read_dir(&root)
            .expect("read root")
            .flatten()
            .filter(|entry| {
                entry
                    .file_name()
                    .to_str()
                    .is_some_and(|name| name.eq_ignore_ascii_case("data"))
            })
            .count();
        if collision_count < 2 {
            std::fs::remove_dir_all(root).expect("cleanup");
            return;
        }

        let vfs = DirectoryVfs::new(&root);
        let path = normalize_relative(b"Data/File.bin", PathPolicy::StrictLegacy).expect("path");

        assert!(matches!(vfs.read(&path), Err(VfsError::Ambiguous(_))));

        std::fs::remove_dir_all(root).expect("cleanup");
    }

    #[test]
    fn directory_vfs_lists_files_below_prefix() {
        let root = unique_test_dir("list");
        std::fs::create_dir_all(root.join("DATA").join("MAPS")).expect("mkdir");
        std::fs::write(root.join("DATA").join("MAPS").join("Land.map"), b"map").expect("write");
        std::fs::write(root.join("BuildDat.lst"), b"build").expect("write");

        let vfs = DirectoryVfs::new(&root);
        let prefix = normalize_relative(b"data", PathPolicy::StrictLegacy).expect("prefix");
        let entries = vfs.list(&prefix).expect("list");
        assert_eq!(entries.len(), 1);
        assert!(entries[0]
            .path
            .as_str()
            .eq_ignore_ascii_case("DATA/MAPS/Land.map"));

        std::fs::remove_dir_all(root).expect("cleanup");
    }

    #[test]
    fn memory_vfs_list_prefix_is_boundary_safe() {
        let mut vfs = MemoryVfs::default();
        let exact = normalize_relative(b"DATA/Land.map", PathPolicy::StrictLegacy).expect("path");
        let sibling = normalize_relative(b"DATA2/Land.map", PathPolicy::StrictLegacy).expect("path");
        vfs.insert(exact.clone(), Arc::from(b"exact".as_slice()));
        vfs.insert(sibling, Arc::from(b"sibling".as_slice()));

        let prefix = normalize_relative(b"DATA", PathPolicy::StrictLegacy).expect("prefix");
        let entries = vfs.list(&prefix).expect("list");

        assert_eq!(entries.len(), 1);
        assert_eq!(entries[0].path.as_str(), exact.as_str());
    }

    #[test]
    fn directory_vfs_fingerprint_changes_for_same_length_content() {
        let root = unique_test_dir("content-fingerprint");
        std::fs::create_dir_all(root.join("DATA")).expect("mkdir");
        std::fs::write(root.join("DATA").join("File.bin"), b"before").expect("write before");

        let vfs = DirectoryVfs::new(&root);
        let path = normalize_relative(b"DATA/File.bin", PathPolicy::StrictLegacy).expect("path");
        let before = vfs.metadata(&path).expect("before metadata");
        std::fs::write(root.join("DATA").join("File.bin"), b"after!").expect("write after");
        let after = vfs.metadata(&path).expect("after metadata");

        assert_eq!(before.len, after.len);
        assert_ne!(before.fingerprint, after.fingerprint);

        std::fs::remove_dir_all(root).expect("cleanup");
    }

    #[cfg(unix)]
    #[test]
    fn directory_vfs_rejects_symlink_escape() {
        let root = unique_test_dir("symlink-escape");
        let outside = unique_test_dir("symlink-outside");
        std::fs::create_dir_all(&root).expect("mkdir root");
        std::fs::create_dir_all(&outside).expect("mkdir outside");
        std::fs::write(outside.join("secret.bin"), b"secret").expect("write outside");
        std::os::unix::fs::symlink(&outside, root.join("DATA")).expect("symlink");

        let vfs = DirectoryVfs::new(&root);
        let path = normalize_relative(b"DATA/secret.bin", PathPolicy::StrictLegacy).expect("path");
        let prefix = normalize_relative(b"DATA", PathPolicy::StrictLegacy).expect("prefix");

        assert!(matches!(vfs.read(&path), Err(VfsError::Path)));
        assert!(matches!(vfs.list(&prefix), Err(VfsError::Path)));

        std::fs::remove_dir_all(root).expect("cleanup root");
        std::fs::remove_dir_all(outside).expect("cleanup outside");
    }

    #[test]
    fn casefold_selector_reports_ambiguous_segments() {
        let err = select_casefolded_match(
            "data/file.bin",
            Path::new("/game"),
            "data",
            vec![PathBuf::from("/game/Data"), PathBuf::from("/game/DATA")],
        )
        .expect_err("ambiguous path");

        assert!(matches!(err, VfsError::Ambiguous(_)));
    }

    #[test]
    fn memory_vfs_uses_ascii_casefold_lookup() {
        let path = normalize_relative(b"Data/File.bin", PathPolicy::StrictLegacy).expect("path");
        let mut vfs = MemoryVfs::default();
        vfs.insert(path.clone(), Arc::from(b"payload".as_slice()));

        assert_eq!(vfs.metadata(&path).expect("metadata").len, 7);
        assert_eq!(vfs.read(&path).expect("read").as_ref(), b"payload");

        let other_case =
            normalize_relative(b"data/file.bin", PathPolicy::StrictLegacy).expect("path");
        assert_eq!(
            vfs.read(&other_case).expect("casefold read").as_ref(),
            b"payload"
        );
    }

    #[test]
    fn memory_vfs_reports_casefold_ambiguity() {
        let first = normalize_relative(b"Data/File.bin", PathPolicy::StrictLegacy).expect("first");
        let second =
            normalize_relative(b"DATA/file.BIN", PathPolicy::StrictLegacy).expect("second");
        let query = normalize_relative(b"data/file.bin", PathPolicy::StrictLegacy).expect("query");
        let mut vfs = MemoryVfs::default();
        vfs.insert(first, Arc::from(b"first".as_slice()));
        vfs.insert(second, Arc::from(b"second".as_slice()));

        assert!(matches!(vfs.read(&query), Err(VfsError::Ambiguous(_))));
    }

    #[test]
    fn memory_vfs_distinguishes_non_utf8_path_bytes() {
        let mut vfs = MemoryVfs::default();
        let ascii = normalize_relative(b"DATA/normal.bin", PathPolicy::HostCompatible)
            .expect("ascii path");
        let binary = normalize_relative(b"DATA/\xFF.bin", PathPolicy::HostCompatible)
            .expect("binary path");
        vfs.insert(ascii.clone(), Arc::from(b"ascii".as_slice()));
        vfs.insert(binary.clone(), Arc::from(b"binary".as_slice()));

        let binary_query = normalize_relative(b"DATA/\xFF.bin", PathPolicy::HostCompatible)
            .expect("binary query");

        assert_eq!(vfs.read(&binary_query).expect("read binary").as_ref(), b"binary");
        assert_eq!(vfs.read(&ascii).expect("read ascii").as_ref(), b"ascii");
    }

    #[test]
    fn overlay_vfs_uses_first_matching_layer() {
        let path = normalize_relative(b"DATA/File.bin", PathPolicy::StrictLegacy).expect("path");
        let prefix = normalize_relative(b"DATA", PathPolicy::StrictLegacy).expect("prefix");
        let mut high = MemoryVfs::default();
        let mut low = MemoryVfs::default();
        high.insert(path.clone(), Arc::from(b"high".as_slice()));
        low.insert(path.clone(), Arc::from(b"low".as_slice()));

        let overlay = OverlayVfs::from_layers(vec![Arc::new(high), Arc::new(low)]);

        assert_eq!(overlay.read(&path).expect("read").as_ref(), b"high");
        let entries = overlay.list(&prefix).expect("list");
        assert_eq!(entries.len(), 1);
        assert_eq!(entries[0].metadata.len, 4);
    }

    fn unique_test_dir(name: &str) -> PathBuf {
        let mut path = std::env::temp_dir();
        path.push(format!("fparkan-vfs-{name}-{}", std::process::id()));
        let _ = std::fs::remove_dir_all(&path);
        path
    }
}