monty_fs/mount_mode.rs
1//! Public mount mode definitions.
2//!
3//! The public API only needs to describe the access policy for a mount. The
4//! in-memory overlay storage lives in [`super::overlay_state`] so the public
5//! enum can stay focused on behavior rather than internal data layout.
6
7use super::overlay_state::OverlayState;
8
9/// Access policy for a mount point.
10///
11/// Controls what operations sandbox code can perform on files within the mounted
12/// directory. The overlay modes provide copy-on-write semantics where reads fall
13/// through to the real directory but writes are captured separately.
14///
15/// Regardless of mode, path traversal and symlink escape protection is always enforced.
16#[derive(Debug)]
17pub enum MountMode {
18 /// Full read and write access to the host directory.
19 /// Use with caution — sandbox code can modify real files.
20 ReadWrite,
21
22 /// Read-only access. Write operations raise `PermissionError`.
23 ReadOnly,
24
25 /// Copy-on-write overlay backed by in-memory storage.
26 ///
27 /// Reads fall through to the host directory. Writes are captured in the
28 /// contained [`OverlayState`]. Deletions insert `OverlayEntry::Deleted`
29 /// tombstones that hide real files from subsequent reads. Directory listings
30 /// merge real and overlay entries, with overlay taking precedence.
31 OverlayMemory(OverlayState),
32}
33
34impl MountMode {
35 /// Parses a mode string into a [`MountMode`].
36 ///
37 /// Accepted values: `"read-only"`, `"read-write"`, `"overlay"`.
38 /// Returns a descriptive error string on invalid input.
39 pub fn from_mode_str(mode: &str) -> Result<Self, String> {
40 match mode {
41 "read-only" => Ok(Self::ReadOnly),
42 "read-write" => Ok(Self::ReadWrite),
43 "overlay" => Ok(Self::OverlayMemory(OverlayState::new())),
44 other => Err(format!(
45 "Invalid mode '{other}', expected 'read-only', 'read-write', or 'overlay'"
46 )),
47 }
48 }
49
50 /// Returns a short string label for this mode (`"read-write"`, `"read-only"`,
51 /// or `"overlay"`).
52 #[must_use]
53 pub fn as_str(&self) -> &'static str {
54 match self {
55 Self::ReadWrite => "read-write",
56 Self::ReadOnly => "read-only",
57 Self::OverlayMemory(_) => "overlay",
58 }
59 }
60}