Struct MountTree

Source
pub struct MountTree {
    root: Arc<MountNode>,
    path_cache: BTreeMap<String, Arc<MountPoint>>,
}
Expand description

Mount point management using Trie structure

MountTree provides efficient hierarchical mount point management using a Trie data structure. This enables O(log k) path resolution where k is the path depth, making filesystem operations scale well with complex mount hierarchies.

§Features

  • Efficient Path Resolution: Trie-based lookup for fast mount point discovery
  • Bind Mount Support: Advanced bind mounting with cross-VFS capability
  • Security: Enhanced path normalization preventing directory traversal attacks
  • Caching: Optional path caching for frequently accessed mount points
  • Thread Safety: All operations are thread-safe using RwLock protection

§Architecture

The mount tree consists of:

  • Root node representing the filesystem root “/”
  • Internal nodes for directory components
  • Leaf nodes containing actual mount points
  • Path cache for performance optimization

§Usage

let mut mount_tree = MountTree::new();
 
// Mount a filesystem
mount_tree.mount("/mnt/data", mount_point)?;
 
// Resolve a path to its mount point
let (mount_node, relative_path) = mount_tree.resolve("/mnt/data/file.txt")?;

Fields§

§root: Arc<MountNode>§path_cache: BTreeMap<String, Arc<MountPoint>>

Cache for fast lookup

Implementations§

Source§

impl MountTree

Source

pub fn new() -> Self

Create a new empty mount tree

Initializes a new mount tree with an empty root node and no cached paths. This creates the foundation for a new filesystem namespace.

§Returns

A new MountTree instance ready for mount operations

Source

pub fn mount(&mut self, path: &str, mount_point: MountPoint) -> Result<()>

Add mount point (VfsManager interface)

This method provides the primary interface for VfsManager to add new mount points to the tree. It normalizes the path and delegates to the internal insert method.

§Arguments
  • path - Absolute path where the filesystem should be mounted
  • mount_point - MountPoint structure containing mount information
§Returns
  • Ok(()) - Mount operation succeeded
  • Err(FileSystemError) - If path is invalid or mount point already exists
Source

pub fn insert(&mut self, path: &str, mount_point: MountPoint) -> Result<()>

Internal method to add mount point to the tree

This method handles the actual insertion logic, creating intermediate nodes as needed and validating that mount points don’t conflict.

§Arguments
  • path - Normalized absolute path for the mount
  • mount_point - MountPoint structure to insert
§Returns
  • Ok(()) - Mount point successfully added
  • Err(FileSystemError) - If mount point already exists at the path
Source

pub fn resolve(&self, path: &str) -> Result<(Arc<MountNode>, String)>

Find mount point by path (transparent resolution - resolves through bind mounts)

This method resolves the given path to its corresponding mount point. For bind mounts, it recursively resolves through the source mount tree to find the deepest actual mount that handles the requested path.

§Arguments
  • path - The absolute path to resolve.
§Returns
  • Ok((Arc<MountNode>, String)) - A tuple containing the resolved mount node and the relative path.
  • Err(FileSystemError) - If the path is invalid or no mount point is found.
Source

pub fn resolve_non_transparent( &self, path: &str, ) -> Result<(Arc<MountNode>, String)>

Find mount point by path (non-transparent resolution - stops at bind mount nodes)

This method resolves the given path to its corresponding mount point without resolving through bind mounts. This is useful for mount management operations where you need to work with the bind mount node itself.

§Arguments
  • path - The absolute path to resolve.
§Returns
  • Ok((Arc<MountNode>, String)) - A tuple containing the mount node and the relative path.
  • Err(FileSystemError) - If the path is invalid or no mount point is found.
Source

pub fn remove(&mut self, path: &str) -> Result<Arc<MountPoint>>

Remove mount point from the tree

Removes a mount point at the specified path and returns the removed MountPoint for cleanup. This operation also removes the path from the internal cache.

§Arguments
  • path - Absolute path of the mount point to remove
§Returns
  • Ok(Arc<MountPoint>) - The removed mount point
  • Err(FileSystemError) - If no mount point exists at the path
Source

pub fn list_all(&self) -> Vec<String>

List all mount points in the tree

Returns a vector of all mount point paths currently registered in the mount tree. This is useful for debugging and system introspection.

§Returns

Vector of mount point paths in no particular order

Source

pub fn len(&self) -> usize

Get number of mount points

Returns the total number of mount points currently registered in this mount tree.

§Returns

Number of active mount points

Source

pub fn normalize_path(path: &str) -> Result<String>

Secure path normalization with directory traversal protection

This method normalizes filesystem paths by resolving “.” and “..” components while preventing directory traversal attacks that could escape the filesystem root. It ensures all paths are absolute and properly formatted.

§Arguments
  • path - Input path to normalize
§Returns
  • Ok(String) - Normalized absolute path
  • Err(FileSystemError) - If path is not absolute or invalid
§Security

This method prevents:

  • Relative path traversal (../../../etc/passwd)
  • Root directory escape attempts
  • Malformed path components
§Examples
assert_eq!(MountTree::normalize_path("/a/b/../c")?, "/a/c");
assert_eq!(MountTree::normalize_path("/a/./b")?, "/a/b");
assert_eq!(MountTree::normalize_path("/../..")?, "/");
Source

fn split_path(&self, path: &str) -> Vec<String>

Split normalized path into components

Converts a normalized path string into a vector of path components for tree traversal. Root path “/” becomes an empty vector.

§Arguments
  • path - Normalized absolute path
§Returns

Vector of path components, empty for root path

Source

fn collect_mount_paths( &self, node: &MountNode, current_path: String, paths: &mut Vec<String>, )

Recursively collect mount paths from tree nodes

Internal method for traversing the mount tree and collecting all mount point paths for the list_all() operation.

§Arguments
  • node - Current node being examined
  • current_path - Path accumulated up to this node
  • paths - Vector to collect found mount paths

Trait Implementations§

Source§

impl Clone for MountTree

Source§

fn clone(&self) -> MountTree

Returns a copy of the value. Read more
1.0.0§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more

Auto Trait Implementations§

§

impl Freeze for MountTree

§

impl !RefUnwindSafe for MountTree

§

impl Send for MountTree

§

impl Sync for MountTree

§

impl Unpin for MountTree

§

impl !UnwindSafe for MountTree

Blanket Implementations§

§

impl<T> Any for T
where T: 'static + ?Sized,

§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
§

impl<T> Borrow<T> for T
where T: ?Sized,

§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
§

impl<T> BorrowMut<T> for T
where T: ?Sized,

§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
§

impl<T> CloneToUninit for T
where T: Clone,

§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
§

impl<T> From<T> for T

§

fn from(t: T) -> T

Returns the argument unchanged.

§

impl<T, U> Into<U> for T
where U: From<T>,

§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of [From]<T> for U chooses to do.

§

impl<T> ToOwned for T
where T: Clone,

§

type Owned = T

The resulting type after obtaining ownership.
§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

§

type Error = Infallible

The type returned in the event of a conversion error.
§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.