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
impl MountTree
Sourcepub fn new() -> Self
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
Sourcepub fn mount(&mut self, path: &str, mount_point: MountPoint) -> Result<()>
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 mountedmount_point
- MountPoint structure containing mount information
§Returns
Ok(())
- Mount operation succeededErr(FileSystemError)
- If path is invalid or mount point already exists
Sourcepub fn insert(&mut self, path: &str, mount_point: MountPoint) -> Result<()>
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 mountmount_point
- MountPoint structure to insert
§Returns
Ok(())
- Mount point successfully addedErr(FileSystemError)
- If mount point already exists at the path
Sourcepub fn resolve(&self, path: &str) -> Result<(Arc<MountNode>, String)>
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.
Sourcepub fn resolve_non_transparent(
&self,
path: &str,
) -> Result<(Arc<MountNode>, String)>
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.
Sourcepub fn remove(&mut self, path: &str) -> Result<Arc<MountPoint>>
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 pointErr(FileSystemError)
- If no mount point exists at the path
Sourcepub fn list_all(&self) -> Vec<String>
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
Sourcepub fn len(&self) -> usize
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
Sourcepub fn normalize_path(path: &str) -> Result<String>
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 pathErr(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("/../..")?, "/");
Sourcefn split_path(&self, path: &str) -> Vec<String>
fn split_path(&self, path: &str) -> Vec<String>
Sourcefn collect_mount_paths(
&self,
node: &MountNode,
current_path: String,
paths: &mut Vec<String>,
)
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 examinedcurrent_path
- Path accumulated up to this nodepaths
- Vector to collect found mount paths
Trait Implementations§
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 Twhere
T: 'static + ?Sized,
impl<T> Any for Twhere
T: 'static + ?Sized,
§impl<T> Borrow<T> for Twhere
T: ?Sized,
impl<T> Borrow<T> for Twhere
T: ?Sized,
§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
§unsafe fn clone_to_uninit(&self, dest: *mut u8)
unsafe fn clone_to_uninit(&self, dest: *mut u8)
clone_to_uninit
)