tinysandbox 0.4.2

Ultra-minimal Linux-like sandbox for AI agents: shell, coreutils, VFS, and a Wasmtime-hosted JS runtime in one crate
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
//! In-memory VFS with quota enforcement and copy-on-write snapshots.

use std::collections::BTreeMap;
use std::sync::{Arc, Mutex, MutexGuard, PoisonError};

use super::path::normalize_path;
use super::{
    DirEntry, Errno, FileHandle, FileType, Metadata, OpenMode, Vfs, VfsError, VfsResult,
    VfsSnapshot,
};

/// Storage limits enforced by [`InMemoryVfs`].
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct VfsQuota {
    /// Maximum total bytes stored in visible files and open unlinked files.
    pub max_bytes: u64,
    /// Maximum non-root directory entries, including files and directories.
    pub max_files: u64,
    /// Maximum size of any single file.
    pub max_file_size: u64,
}

impl VfsQuota {
    /// Returns a quota with all limits set to `u64::MAX`.
    pub const fn unlimited() -> Self {
        Self {
            max_bytes: u64::MAX,
            max_files: u64::MAX,
            max_file_size: u64::MAX,
        }
    }
}

impl Default for VfsQuota {
    fn default() -> Self {
        Self::unlimited()
    }
}

/// Current quota usage for a VFS.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct VfsStats {
    /// Total bytes stored in files.
    pub used_bytes: u64,
    /// Count of non-root files and directories.
    pub file_count: u64,
}

/// Quota-enforced in-memory implementation of [`Vfs`].
#[derive(Debug)]
pub struct InMemoryVfs {
    quota: VfsQuota,
    state: Mutex<State>,
}

/// Copy-on-write snapshot captured from an [`InMemoryVfs`].
#[derive(Debug, Clone)]
pub struct InMemoryVfsSnapshot {
    state: SnapshotState,
}

impl InMemoryVfs {
    /// Creates an empty in-memory VFS with `quota`.
    pub fn new(quota: VfsQuota) -> Self {
        Self {
            quota,
            state: Mutex::new(State::default()),
        }
    }

    /// Returns current quota usage.
    pub fn stats(&self) -> VfsResult<VfsStats> {
        let state = self.state();
        Ok(VfsStats {
            used_bytes: state.used_bytes,
            file_count: state.file_count,
        })
    }

    fn from_snapshot(quota: VfsQuota, snapshot: &InMemoryVfsSnapshot) -> VfsResult<Self> {
        ensure_snapshot_fits_quota(&snapshot.state, quota)?;
        Ok(Self {
            quota,
            state: Mutex::new(State::from_snapshot(&snapshot.state, 1)),
        })
    }

    fn state(&self) -> MutexGuard<'_, State> {
        self.state.lock().unwrap_or_else(PoisonError::into_inner)
    }

    fn ensure_entry_slot(&self, state: &State) -> VfsResult<()> {
        if state.file_count >= self.quota.max_files {
            return Err(VfsError::new(Errno::ENOSPC));
        }

        Ok(())
    }

    fn resize_file(&self, state: &mut State, inode: Inode, new_len: usize) -> VfsResult<()> {
        let old_len = state
            .files
            .get(&inode)
            .ok_or(VfsError::new(Errno::ENOENT))?
            .data
            .len();

        let new_len_u64 = u64::try_from(new_len).map_err(|_| VfsError::new(Errno::ENOSPC))?;
        if new_len_u64 > self.quota.max_file_size {
            return Err(VfsError::new(Errno::ENOSPC));
        }

        let used_bytes = adjusted_used_bytes(state.used_bytes, old_len, new_len)?;
        if used_bytes > self.quota.max_bytes {
            return Err(VfsError::new(Errno::ENOSPC));
        }

        let file = state
            .files
            .get_mut(&inode)
            .ok_or(VfsError::new(Errno::ENOENT))?;
        if new_len < old_len && Arc::strong_count(&file.data) > 1 {
            file.data = Arc::new(file.data[..new_len].to_vec());
        } else {
            Arc::make_mut(&mut file.data).resize(new_len, 0);
        }
        state.used_bytes = used_bytes;
        Ok(())
    }
}

impl Default for InMemoryVfs {
    fn default() -> Self {
        Self::new(VfsQuota::default())
    }
}

impl Vfs for InMemoryVfs {
    fn is_fast(&self) -> bool {
        true
    }

    fn stats(&self) -> Option<VfsResult<VfsStats>> {
        Some(InMemoryVfs::stats(self))
    }

    fn stat(&self, path: &str) -> VfsResult<Metadata> {
        let path = normalize_path(path)?;
        let state = self.state();
        metadata_for_node(&state, get_node(&state.root, &path)?)
    }

    fn readdir(&self, path: &str) -> VfsResult<Vec<DirEntry>> {
        let path = normalize_path(path)?;
        let state = self.state();

        match get_node(&state.root, &path)? {
            Node::File(_) => Err(VfsError::new(Errno::ENOTDIR)),
            Node::Directory(entries) => entries
                .iter()
                .map(|(name, node)| {
                    Ok(DirEntry {
                        name: name.clone(),
                        metadata: metadata_for_node(&state, node)?,
                    })
                })
                .collect(),
        }
    }

    fn mkdir(&self, path: &str) -> VfsResult<()> {
        let path = normalize_path(path)?;
        if path.is_empty() {
            return Err(VfsError::new(Errno::EEXIST));
        }

        let mut state = self.state();
        match get_node(&state.root, &path) {
            Ok(_) => return Err(VfsError::new(Errno::EEXIST)),
            Err(err) if err.errno() == Errno::ENOENT => {}
            Err(err) => return Err(err),
        }
        ensure_parent_is_dir(&state.root, &path)?;
        self.ensure_entry_slot(&state)?;

        let (parent, name) = parent_dir_mut(&mut state.root, &path)?;
        parent.insert(name.to_owned(), Node::Directory(BTreeMap::new()));
        state.file_count += 1;
        Ok(())
    }

    fn rename(&self, from: &str, to: &str) -> VfsResult<()> {
        let from_path = normalize_path(from)?;
        let to_path = normalize_path(to)?;

        if from_path.is_empty() || to_path.is_empty() {
            return Err(VfsError::new(Errno::EINVAL));
        }

        let mut state = self.state();
        let source_kind = node_kind(get_node(&state.root, &from_path)?);
        if from_path == to_path {
            return Ok(());
        }

        if source_kind == NodeKind::Directory && has_prefix(&to_path, &from_path) {
            return Err(VfsError::new(Errno::EINVAL));
        }

        ensure_parent_is_dir(&state.root, &to_path)?;
        match get_node(&state.root, &to_path) {
            Ok(target) => match (source_kind, target) {
                (NodeKind::File, Node::File(_)) => {}
                (NodeKind::File, Node::Directory(_)) => {
                    return Err(VfsError::new(Errno::EISDIR));
                }
                (NodeKind::Directory, Node::File(_)) => {
                    return Err(VfsError::new(Errno::ENOTDIR));
                }
                (NodeKind::Directory, Node::Directory(entries)) if !entries.is_empty() => {
                    return Err(VfsError::new(Errno::ENOTEMPTY));
                }
                (NodeKind::Directory, Node::Directory(_)) => {}
            },
            Err(err) if err.errno() == Errno::ENOENT => {}
            Err(err) => return Err(err),
        }

        let removed = {
            let (parent, name) = parent_dir_mut(&mut state.root, &from_path)?;
            parent.remove(name).ok_or(VfsError::new(Errno::ENOENT))?
        };

        let replaced = {
            let (parent, name) = parent_dir_mut(&mut state.root, &to_path)?;
            parent.insert(name.to_owned(), removed)
        };

        if let Some(replaced) = replaced {
            remove_tree(&mut state, replaced)?;
        }

        Ok(())
    }

    fn unlink(&self, path: &str) -> VfsResult<()> {
        let path = normalize_path(path)?;
        if path.is_empty() {
            return Err(VfsError::new(Errno::EISDIR));
        }

        let mut state = self.state();
        let removed = {
            let (parent, name) = parent_dir_mut(&mut state.root, &path)?;
            match parent.get(name) {
                Some(Node::File(_)) => {}
                Some(Node::Directory(_)) => return Err(VfsError::new(Errno::EISDIR)),
                None => return Err(VfsError::new(Errno::ENOENT)),
            }
            parent.remove(name).ok_or(VfsError::new(Errno::ENOENT))?
        };

        remove_tree(&mut state, removed)
    }

    fn rmdir(&self, path: &str) -> VfsResult<()> {
        let path = normalize_path(path)?;
        if path.is_empty() {
            return Err(VfsError::new(Errno::EBUSY));
        }

        let mut state = self.state();
        let removed = {
            let (parent, name) = parent_dir_mut(&mut state.root, &path)?;
            match parent.get(name) {
                Some(Node::File(_)) => return Err(VfsError::new(Errno::ENOTDIR)),
                Some(Node::Directory(entries)) if !entries.is_empty() => {
                    return Err(VfsError::new(Errno::ENOTEMPTY));
                }
                Some(Node::Directory(_)) => {}
                None => return Err(VfsError::new(Errno::ENOENT)),
            }
            parent.remove(name).ok_or(VfsError::new(Errno::ENOENT))?
        };

        remove_tree(&mut state, removed)
    }

    fn open(&self, path: &str, mode: OpenMode) -> VfsResult<FileHandle> {
        mode.validate()?;
        let path = normalize_path(path)?;

        let mut state = self.state();
        let inode = match get_node(&state.root, &path) {
            Ok(Node::Directory(_)) => return Err(VfsError::new(Errno::EISDIR)),
            Ok(Node::File(_)) if mode.create_new => {
                return Err(VfsError::new(Errno::EEXIST));
            }
            Ok(Node::File(inode)) => {
                let inode = *inode;
                if mode.truncate {
                    self.resize_file(&mut state, inode, 0)?;
                }
                inode
            }
            Err(err) if err.errno() == Errno::ENOENT && (mode.create || mode.create_new) => {
                ensure_parent_is_dir(&state.root, &path)?;
                self.ensure_entry_slot(&state)?;

                let inode = state.next_inode;
                state.next_inode += 1;
                state.files.insert(
                    inode,
                    FileNode {
                        links: 1,
                        ..FileNode::default()
                    },
                );
                let (parent, name) = parent_dir_mut(&mut state.root, &path)?;
                parent.insert(name.to_owned(), Node::File(inode));
                state.file_count += 1;
                inode
            }
            Err(err) => return Err(err),
        };

        let handle = FileHandle(state.next_handle);
        state.next_handle += 1;
        state.handles.insert(
            handle,
            Handle {
                inode,
                readable: mode.read,
                writable: mode.write,
                append: mode.append,
            },
        );
        state
            .files
            .get_mut(&inode)
            .ok_or(VfsError::new(Errno::ENOENT))?
            .open_handles += 1;
        Ok(handle)
    }

    fn read_at(&self, handle: FileHandle, offset: u64, buf: &mut [u8]) -> VfsResult<usize> {
        let offset = usize::try_from(offset).map_err(|_| VfsError::new(Errno::EINVAL))?;
        let state = self.state();
        let handle = state
            .handles
            .get(&handle)
            .copied()
            .ok_or(VfsError::new(Errno::EBADF))?;

        if !handle.readable {
            return Err(VfsError::new(Errno::EBADF));
        }

        let data = &state
            .files
            .get(&handle.inode)
            .ok_or(VfsError::new(Errno::ENOENT))?
            .data;
        if offset >= data.len() {
            return Ok(0);
        }

        let available = data.len() - offset;
        let len = available.min(buf.len());
        buf[..len].copy_from_slice(&data[offset..offset + len]);
        Ok(len)
    }

    fn write_at(&self, handle: FileHandle, offset: u64, data: &[u8]) -> VfsResult<usize> {
        let mut state = self.state();
        let handle = state
            .handles
            .get(&handle)
            .copied()
            .ok_or(VfsError::new(Errno::EBADF))?;

        if !handle.writable {
            return Err(VfsError::new(Errno::EBADF));
        }

        if data.is_empty() {
            return Ok(0);
        }

        let offset = usize::try_from(offset).map_err(|_| VfsError::new(Errno::EINVAL))?;
        let old_len = state
            .files
            .get(&handle.inode)
            .ok_or(VfsError::new(Errno::ENOENT))?
            .data
            .len();
        let write_offset = if handle.append { old_len } else { offset };
        let write_end = write_offset
            .checked_add(data.len())
            .ok_or(VfsError::new(Errno::EINVAL))?;
        let new_len = old_len.max(write_end);

        self.resize_file(&mut state, handle.inode, new_len)?;
        let file = state
            .files
            .get_mut(&handle.inode)
            .ok_or(VfsError::new(Errno::ENOENT))?;
        Arc::make_mut(&mut file.data)[write_offset..write_end].copy_from_slice(data);
        Ok(data.len())
    }

    fn truncate(&self, handle: FileHandle, len: u64) -> VfsResult<()> {
        let len = usize::try_from(len).map_err(|_| VfsError::new(Errno::ENOSPC))?;
        let mut state = self.state();
        let handle = state
            .handles
            .get(&handle)
            .copied()
            .ok_or(VfsError::new(Errno::EBADF))?;

        if !handle.writable {
            return Err(VfsError::new(Errno::EINVAL));
        }

        self.resize_file(&mut state, handle.inode, len)
    }

    fn close(&self, handle: FileHandle) -> VfsResult<()> {
        let mut state = self.state();
        let handle = state
            .handles
            .remove(&handle)
            .ok_or(VfsError::new(Errno::EBADF))?;

        let file = state
            .files
            .get_mut(&handle.inode)
            .ok_or(VfsError::new(Errno::ENOENT))?;
        file.open_handles -= 1;
        release_file_if_unreferenced(&mut state, handle.inode)
    }
}

impl VfsSnapshot for InMemoryVfs {
    type Snapshot = InMemoryVfsSnapshot;

    fn snapshot(&self) -> VfsResult<Self::Snapshot> {
        let state = self.state();
        Ok(InMemoryVfsSnapshot {
            state: SnapshotState::from_state(&state)?,
        })
    }

    fn restore(&self, snapshot: &Self::Snapshot) -> VfsResult<()> {
        ensure_snapshot_fits_quota(&snapshot.state, self.quota)?;
        let mut state = self.state();
        let next_handle = state.next_handle;
        *state = State::from_snapshot(&snapshot.state, next_handle);
        Ok(())
    }

    fn branch(&self, snapshot: &Self::Snapshot) -> VfsResult<Self> {
        Self::from_snapshot(self.quota, snapshot)
    }
}

type Directory = BTreeMap<String, Node>;
type Inode = u64;

#[derive(Debug)]
struct State {
    root: Node,
    files: BTreeMap<Inode, FileNode>,
    next_inode: Inode,
    next_handle: u64,
    handles: BTreeMap<FileHandle, Handle>,
    used_bytes: u64,
    file_count: u64,
}

impl Default for State {
    fn default() -> Self {
        Self {
            root: Node::Directory(BTreeMap::new()),
            files: BTreeMap::new(),
            next_inode: 1,
            next_handle: 1,
            handles: BTreeMap::new(),
            used_bytes: 0,
            file_count: 0,
        }
    }
}

#[derive(Debug, Clone)]
struct SnapshotState {
    root: Node,
    files: BTreeMap<Inode, FileNode>,
    next_inode: Inode,
    used_bytes: u64,
    file_count: u64,
}

impl SnapshotState {
    fn from_state(state: &State) -> VfsResult<Self> {
        let mut files = BTreeMap::new();
        let mut used_bytes = 0;
        let mut file_count = 0;
        collect_snapshot_entries(
            &state.root,
            &state.files,
            &mut files,
            &mut used_bytes,
            &mut file_count,
        )?;
        Ok(Self {
            root: state.root.clone(),
            files,
            next_inode: state.next_inode,
            used_bytes,
            file_count,
        })
    }
}

impl State {
    fn from_snapshot(snapshot: &SnapshotState, next_handle: u64) -> Self {
        Self {
            root: snapshot.root.clone(),
            files: snapshot.files.clone(),
            next_inode: snapshot.next_inode,
            next_handle,
            handles: BTreeMap::new(),
            used_bytes: snapshot.used_bytes,
            file_count: snapshot.file_count,
        }
    }
}

#[derive(Debug, Clone)]
struct FileNode {
    data: Arc<Vec<u8>>,
    links: u64,
    open_handles: u64,
}

impl Default for FileNode {
    fn default() -> Self {
        Self {
            data: Arc::new(Vec::new()),
            links: 0,
            open_handles: 0,
        }
    }
}

#[derive(Debug, Clone)]
enum Node {
    File(Inode),
    Directory(Directory),
}

#[derive(Debug, Clone, Copy, PartialEq, Eq)]
enum NodeKind {
    File,
    Directory,
}

#[derive(Debug, Clone, Copy)]
struct Handle {
    inode: Inode,
    readable: bool,
    writable: bool,
    append: bool,
}

fn metadata_for_node(state: &State, node: &Node) -> VfsResult<Metadata> {
    match node {
        Node::File(inode) => Ok(Metadata {
            file_type: FileType::File,
            len: u64::try_from(
                state
                    .files
                    .get(inode)
                    .ok_or(VfsError::new(Errno::ENOENT))?
                    .data
                    .len(),
            )
            .unwrap_or(u64::MAX),
        }),
        Node::Directory(_) => Ok(Metadata {
            file_type: FileType::Directory,
            len: 0,
        }),
    }
}

fn node_kind(node: &Node) -> NodeKind {
    match node {
        Node::File(_) => NodeKind::File,
        Node::Directory(_) => NodeKind::Directory,
    }
}

fn get_node<'a>(root: &'a Node, path: &[String]) -> VfsResult<&'a Node> {
    let mut current = root;

    for component in path {
        current = match current {
            Node::File(_) => return Err(VfsError::new(Errno::ENOTDIR)),
            Node::Directory(entries) => {
                entries.get(component).ok_or(VfsError::new(Errno::ENOENT))?
            }
        };
    }

    Ok(current)
}

fn get_node_mut<'a>(root: &'a mut Node, path: &[String]) -> VfsResult<&'a mut Node> {
    let mut current = root;

    for component in path {
        current = match current {
            Node::File(_) => return Err(VfsError::new(Errno::ENOTDIR)),
            Node::Directory(entries) => entries
                .get_mut(component)
                .ok_or(VfsError::new(Errno::ENOENT))?,
        };
    }

    Ok(current)
}

fn parent_dir_mut<'a>(
    root: &'a mut Node,
    path: &'a [String],
) -> VfsResult<(&'a mut Directory, &'a str)> {
    let Some((name, parent_path)) = path.split_last() else {
        return Err(VfsError::new(Errno::EINVAL));
    };

    match get_node_mut(root, parent_path)? {
        Node::File(_) => Err(VfsError::new(Errno::ENOTDIR)),
        Node::Directory(entries) => Ok((entries, name)),
    }
}

fn ensure_parent_is_dir(root: &Node, path: &[String]) -> VfsResult<()> {
    let Some((_name, parent_path)) = path.split_last() else {
        return Err(VfsError::new(Errno::EINVAL));
    };

    match get_node(root, parent_path)? {
        Node::File(_) => Err(VfsError::new(Errno::ENOTDIR)),
        Node::Directory(_) => Ok(()),
    }
}

fn adjusted_used_bytes(used_bytes: u64, old_len: usize, new_len: usize) -> VfsResult<u64> {
    let old_len = u64::try_from(old_len).map_err(|_| VfsError::new(Errno::EINVAL))?;
    let new_len = u64::try_from(new_len).map_err(|_| VfsError::new(Errno::ENOSPC))?;

    if new_len >= old_len {
        used_bytes
            .checked_add(new_len - old_len)
            .ok_or(VfsError::new(Errno::ENOSPC))
    } else {
        Ok(used_bytes - (old_len - new_len))
    }
}

fn collect_snapshot_entries(
    node: &Node,
    state_files: &BTreeMap<Inode, FileNode>,
    snapshot_files: &mut BTreeMap<Inode, FileNode>,
    used_bytes: &mut u64,
    file_count: &mut u64,
) -> VfsResult<()> {
    match node {
        Node::File(inode) => {
            let file = state_files.get(inode).ok_or(VfsError::new(Errno::ENOENT))?;
            let len = u64::try_from(file.data.len()).map_err(|_| VfsError::new(Errno::EINVAL))?;
            *used_bytes = used_bytes
                .checked_add(len)
                .ok_or(VfsError::new(Errno::ENOSPC))?;
            *file_count = file_count
                .checked_add(1)
                .ok_or(VfsError::new(Errno::ENOSPC))?;
            snapshot_files.insert(
                *inode,
                FileNode {
                    data: Arc::clone(&file.data),
                    links: 1,
                    open_handles: 0,
                },
            );
            Ok(())
        }
        Node::Directory(entries) => {
            for child in entries.values() {
                *file_count = file_count
                    .checked_add(matches!(child, Node::Directory(_)) as u64)
                    .ok_or(VfsError::new(Errno::ENOSPC))?;
                collect_snapshot_entries(
                    child,
                    state_files,
                    snapshot_files,
                    used_bytes,
                    file_count,
                )?;
            }
            Ok(())
        }
    }
}

fn ensure_snapshot_fits_quota(snapshot: &SnapshotState, quota: VfsQuota) -> VfsResult<()> {
    if snapshot.used_bytes > quota.max_bytes || snapshot.file_count > quota.max_files {
        return Err(VfsError::new(Errno::ENOSPC));
    }

    for file in snapshot.files.values() {
        let len = u64::try_from(file.data.len()).map_err(|_| VfsError::new(Errno::ENOSPC))?;
        if len > quota.max_file_size {
            return Err(VfsError::new(Errno::ENOSPC));
        }
    }

    Ok(())
}

fn remove_tree(state: &mut State, node: Node) -> VfsResult<()> {
    match node {
        Node::File(inode) => {
            let file = state
                .files
                .get_mut(&inode)
                .ok_or(VfsError::new(Errno::ENOENT))?;
            file.links -= 1;
            release_file_if_unreferenced(state, inode)
        }
        Node::Directory(entries) => {
            for child in entries.into_values() {
                remove_tree(state, child)?;
            }
            state.file_count -= 1;
            Ok(())
        }
    }
}

fn release_file_if_unreferenced(state: &mut State, inode: Inode) -> VfsResult<()> {
    let Some(file) = state.files.get(&inode) else {
        return Ok(());
    };
    if file.links > 0 || file.open_handles > 0 {
        return Ok(());
    }

    let file = state
        .files
        .remove(&inode)
        .ok_or(VfsError::new(Errno::ENOENT))?;
    let len = u64::try_from(file.data.len()).map_err(|_| VfsError::new(Errno::EINVAL))?;
    state.used_bytes -= len;
    state.file_count -= 1;
    Ok(())
}

fn has_prefix(path: &[String], prefix: &[String]) -> bool {
    path.len() >= prefix.len() && path[..prefix.len()] == *prefix
}

#[cfg(test)]
mod tests {
    use std::sync::Arc;

    use super::{InMemoryVfs, VfsQuota};
    use crate::vfs::{Errno, OpenMode, Vfs, VfsSnapshot};

    #[test]
    fn write_extending_past_eof_fills_gap_with_zeroes() {
        let vfs = InMemoryVfs::default();
        let handle = vfs
            .open("/file", OpenMode::read_write().create_new())
            .expect("file opens");

        vfs.write_at(handle, 3, b"x").expect("write succeeds");

        let mut buf = [1; 4];
        let read = vfs.read_at(handle, 0, &mut buf).expect("read succeeds");
        assert_eq!(read, 4);
        assert_eq!(buf, [0, 0, 0, b'x']);
    }

    #[test]
    fn max_file_size_is_enforced_before_mutation() {
        let vfs = InMemoryVfs::new(VfsQuota {
            max_bytes: 10,
            max_files: 1,
            max_file_size: 3,
        });
        let handle = vfs
            .open("/file", OpenMode::write_only().create_new())
            .expect("file opens");

        let err = vfs
            .write_at(handle, 0, b"abcd")
            .expect_err("oversized write is rejected");
        assert_eq!(err.errno(), Errno::ENOSPC);
        assert_eq!(vfs.stats().expect("stats").used_bytes, 0);
    }

    #[test]
    fn file_count_quota_rejects_new_file() {
        let vfs = InMemoryVfs::new(VfsQuota {
            max_bytes: 10,
            max_files: 1,
            max_file_size: 10,
        });

        vfs.open("/a", OpenMode::write_only().create_new())
            .expect("first file opens");
        let err = vfs
            .open("/b", OpenMode::write_only().create_new())
            .expect_err("second file exceeds quota");

        assert_eq!(err.errno(), Errno::ENOSPC);
        assert_eq!(vfs.stats().expect("stats").file_count, 1);
    }

    #[test]
    fn snapshot_data_is_shared_until_live_write() {
        // This pins the CoW invariant snapshots rely on: reads keep file data
        // shared, while writes detach the live file from the snapshot.
        let vfs = InMemoryVfs::default();
        let handle = vfs
            .open("/file", OpenMode::read_write().create_new())
            .expect("file opens");
        vfs.write_at(handle, 0, b"abcdef")
            .expect("seed write succeeds");

        let snapshot = vfs.snapshot().expect("snapshot succeeds");
        let live_after_snapshot = only_file_data(&vfs);
        let snapshot_data = snapshot
            .state
            .files
            .values()
            .next()
            .expect("snapshot file exists")
            .data
            .clone();
        assert!(Arc::ptr_eq(&live_after_snapshot, &snapshot_data));

        let mut buf = [0; 3];
        vfs.read_at(handle, 0, &mut buf)
            .expect("read keeps data shared");
        assert!(Arc::ptr_eq(&only_file_data(&vfs), &snapshot_data));

        vfs.write_at(handle, 0, b"Z").expect("write detaches data");
        assert!(!Arc::ptr_eq(&only_file_data(&vfs), &snapshot_data));
        vfs.close(handle).expect("close handle");
    }

    #[test]
    fn truncating_shared_snapshot_data_copies_only_the_prefix() {
        // Shrinking a shared buffer should detach to the requested prefix
        // without cloning the discarded suffix first.
        let vfs = InMemoryVfs::default();
        let handle = vfs
            .open("/file", OpenMode::read_write().create_new())
            .expect("file opens");
        vfs.write_at(handle, 0, b"abcdef")
            .expect("seed write succeeds");
        let snapshot = vfs.snapshot().expect("snapshot succeeds");
        let snapshot_data = snapshot
            .state
            .files
            .values()
            .next()
            .expect("snapshot file exists")
            .data
            .clone();

        vfs.truncate(handle, 3).expect("truncate succeeds");

        let live_data = only_file_data(&vfs);
        assert!(!Arc::ptr_eq(&live_data, &snapshot_data));
        assert_eq!(&live_data[..], b"abc");
        assert_eq!(&snapshot_data[..], b"abcdef");
        vfs.close(handle).expect("close handle");
    }

    fn only_file_data(vfs: &InMemoryVfs) -> Arc<Vec<u8>> {
        vfs.state()
            .files
            .values()
            .next()
            .expect("file exists")
            .data
            .clone()
    }
}