pub struct VirtualRoot<Marker = ()> { /* private fields */ }Expand description
Provide a user‑facing virtual root that produces VirtualPath values clamped to a boundary.
Implementations§
Source§impl<Marker> VirtualRoot<Marker>
impl<Marker> VirtualRoot<Marker>
Sourcepub fn metadata(&self) -> Result<Metadata>
pub fn metadata(&self) -> Result<Metadata>
Return filesystem metadata for the underlying root directory.
Sourcepub fn into_virtualpath(self) -> Result<VirtualPath<Marker>>
pub fn into_virtualpath(self) -> Result<VirtualPath<Marker>>
Consume this virtual root and return the rooted VirtualPath (“/”).
§Errors
StrictPathError::PathResolutionError: Canonicalization fails (root removed or inaccessible).StrictPathError::PathEscapesBoundary: Root moved outside the boundary between checks.
§Examples
let vroot: VirtualRoot = VirtualRoot::try_new(&root)?;
let root_virtual: VirtualPath = vroot.into_virtualpath()?;
assert_eq!(root_virtual.virtualpath_display().to_string(), "/");Sourcepub fn change_marker<NewMarker>(self) -> VirtualRoot<NewMarker>
pub fn change_marker<NewMarker>(self) -> VirtualRoot<NewMarker>
Consume this virtual root and substitute a new marker type.
Mirrors crate::PathBoundary::change_marker, crate::StrictPath::change_marker, and
crate::VirtualPath::change_marker. Use this when encoding proven authorization
into the type system (e.g., after validating a user’s permissions). The
consumption makes marker changes explicit during code review.
§Examples
struct UserFiles;
struct ReadOnly;
struct ReadWrite;
let read_root: VirtualRoot<(UserFiles, ReadOnly)> = VirtualRoot::try_new(&root_dir)?;
// After authorization check...
let write_root: VirtualRoot<(UserFiles, ReadWrite)> = read_root.change_marker();Sourcepub fn virtual_symlink<P: AsRef<Path>>(&self, link_path: P) -> Result<()>
pub fn virtual_symlink<P: AsRef<Path>>(&self, link_path: P) -> Result<()>
Create a symbolic link at link_path pointing to this root’s underlying directory.
link_path is interpreted in the virtual dimension and resolved via virtual_join()
so that absolute virtual paths (“/links/a”) are clamped within this virtual root and
relative paths are resolved relative to the virtual root.
Sourcepub fn virtual_hard_link<P: AsRef<Path>>(&self, link_path: P) -> Result<()>
pub fn virtual_hard_link<P: AsRef<Path>>(&self, link_path: P) -> Result<()>
Create a hard link at link_path pointing to this root’s underlying directory.
The link location is resolved via virtual_join() to clamp/anchor within this root.
Note: Most platforms forbid directory hard links; expect an error from the OS.
Sourcepub fn read_dir(&self) -> Result<ReadDir>
pub fn read_dir(&self) -> Result<ReadDir>
Read directory entries at the virtual root (discovery). Re‑join names through virtual/strict APIs before I/O.
Sourcepub fn virtual_read_dir(&self) -> Result<VirtualRootReadDir<'_, Marker>>
pub fn virtual_read_dir(&self) -> Result<VirtualRootReadDir<'_, Marker>>
Iterate directory entries at the virtual root, yielding validated VirtualPath values.
Unlike read_dir() which returns raw std::fs::DirEntry values requiring manual
re-validation, this method yields VirtualPath entries directly. Each entry is
automatically validated through virtual_join() so you can use it immediately
for I/O operations without additional validation.
§Examples
use strict_path::VirtualRoot;
let vroot: VirtualRoot = VirtualRoot::try_new(temp.path())?;
// Auto-validated iteration - no manual re-join needed!
for entry in vroot.virtual_read_dir()? {
let child = entry?;
println!("Virtual: {}", child.virtualpath_display());
}Sourcepub fn remove_dir(&self) -> Result<()>
pub fn remove_dir(&self) -> Result<()>
Remove the underlying root directory (non‑recursive); fails if not empty.
Sourcepub fn remove_dir_all(&self) -> Result<()>
pub fn remove_dir_all(&self) -> Result<()>
Recursively remove the underlying root directory and all its contents.
Sourcepub fn try_new_create<P: AsRef<Path>>(root_path: P) -> Result<Self>
pub fn try_new_create<P: AsRef<Path>>(root_path: P) -> Result<Self>
Ensure the directory exists (create if missing), then return a VirtualRoot.
§Examples
Uses AsRef<Path> for maximum ergonomics, including direct TempDir support for clean shadowing patterns:
use strict_path::VirtualRoot;
let vroot = VirtualRoot::<()>::try_new_create("./data")?;Sourcepub fn virtual_join<P: AsRef<Path>>(
&self,
candidate_path: P,
) -> Result<VirtualPath<Marker>>
pub fn virtual_join<P: AsRef<Path>>( &self, candidate_path: P, ) -> Result<VirtualPath<Marker>>
Join a candidate path to this virtual root, producing a clamped VirtualPath.
This is the security gateway for virtual paths. Absolute paths (starting with "/") are
automatically clamped to the virtual root, ensuring paths cannot escape the sandbox.
For example, "/etc/config" becomes vroot/etc/config, and traversal attempts like
"../../../../etc/passwd" are clamped to vroot/etc/passwd. This clamping behavior is
what makes the virtual_ dimension safe for user-facing operations.
§Errors
StrictPathError::PathResolutionError,StrictPathError::PathEscapesBoundary.
§Examples
let vroot: VirtualRoot = VirtualRoot::try_new_create(td.path())?;
// Absolute paths are clamped to virtual root, not system root
let user_input_abs = "/etc/config"; // Untrusted input
let path1 = vroot.virtual_join(user_input_abs)?;
assert_eq!(path1.virtualpath_display().to_string(), "/etc/config");
// Traversal attempts are also clamped
let attack_input = "../../../etc/passwd"; // Untrusted input
let path2 = vroot.virtual_join(attack_input)?;
assert_eq!(path2.virtualpath_display().to_string(), "/etc/passwd");
// Both paths are safely within the virtual root on the actual filesystemSourcepub fn interop_path(&self) -> &OsStr
pub fn interop_path(&self) -> &OsStr
Return the virtual root path as &OsStr for unavoidable third-party AsRef<Path> interop.
Sourcepub fn as_unvirtual(&self) -> &PathBoundary<Marker>
pub fn as_unvirtual(&self) -> &PathBoundary<Marker>
Borrow the underlying PathBoundary.
Sourcepub fn unvirtual(self) -> PathBoundary<Marker>
pub fn unvirtual(self) -> PathBoundary<Marker>
Consume this VirtualRoot and return the underlying PathBoundary (symmetry with virtualize).
Trait Implementations§
Source§impl<Marker: Clone> Clone for VirtualRoot<Marker>
impl<Marker: Clone> Clone for VirtualRoot<Marker>
Source§fn clone(&self) -> VirtualRoot<Marker>
fn clone(&self) -> VirtualRoot<Marker>
1.0.0 · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source. Read moreSource§impl<Marker> Debug for VirtualRoot<Marker>
impl<Marker> Debug for VirtualRoot<Marker>
Source§impl<Marker> Display for VirtualRoot<Marker>
Display shows “/”: The real system path must never appear in user-facing output
(logs, API responses, error messages). Showing “/” reinforces that VirtualRoot
represents a virtual namespace root, not a concrete filesystem location.
impl<Marker> Display for VirtualRoot<Marker>
Display shows “/”: The real system path must never appear in user-facing output (logs, API responses, error messages). Showing “/” reinforces that VirtualRoot represents a virtual namespace root, not a concrete filesystem location.
Source§impl<Marker> FromStr for VirtualRoot<Marker>
impl<Marker> FromStr for VirtualRoot<Marker>
Source§fn from_str(path: &str) -> Result<Self, Self::Err>
fn from_str(path: &str) -> Result<Self, Self::Err>
Forwards to try_new_create: creates the
target directory if missing, then canonicalizes and validates it as a
directory.
Untrusted per-request paths (archive entries, user-supplied virtual
paths) are not FromStr input — validate those via
virtual_join on a pre-constructed root.
let vroot: VirtualRoot<()> = p.parse()?;
assert!(vroot.exists());Source§type Err = StrictPathError
type Err = StrictPathError
Source§impl<Marker> Hash for VirtualRoot<Marker>
impl<Marker> Hash for VirtualRoot<Marker>
Source§impl<Marker> Ord for VirtualRoot<Marker>
impl<Marker> Ord for VirtualRoot<Marker>
Source§impl<Marker> PartialEq<&Path> for VirtualRoot<Marker>
impl<Marker> PartialEq<&Path> for VirtualRoot<Marker>
Source§impl<Marker> PartialEq<Path> for VirtualRoot<Marker>
compare against “/”: VirtualRoot’s public identity is the virtual namespace root.
Comparing against the real system path would leak implementation details and break the
abstraction — callers should never need to know the underlying directory.
impl<Marker> PartialEq<Path> for VirtualRoot<Marker>
compare against “/”: VirtualRoot’s public identity is the virtual namespace root. Comparing against the real system path would leak implementation details and break the abstraction — callers should never need to know the underlying directory.
Source§impl<M1, M2> PartialEq<PathBoundary<M2>> for VirtualRoot<M1>
impl<M1, M2> PartialEq<PathBoundary<M2>> for VirtualRoot<M1>
Source§impl<Marker> PartialEq<PathBuf> for VirtualRoot<Marker>
impl<Marker> PartialEq<PathBuf> for VirtualRoot<Marker>
Source§impl<M1, M2> PartialEq<VirtualRoot<M2>> for PathBoundary<M1>
Available on crate feature virtual-path only.
impl<M1, M2> PartialEq<VirtualRoot<M2>> for PathBoundary<M1>
virtual-path only.