pub trait FsPath: FsRead + FsLink {
// Provided methods
fn canonicalize(&self, path: &Path) -> Result<PathBuf, FsError> { ... }
fn soft_canonicalize(&self, path: &Path) -> Result<PathBuf, FsError> { ... }
}Expand description
Path canonicalization with a default implementation.
This trait provides methods for resolving paths to their canonical form,
handling symlinks and normalizing . and .. components.
§Blanket Implementation
This trait has a blanket implementation for any type implementing
FsRead + FsLink, so all backends with symlink support
automatically get these methods.
§Backend Optimization
Backends can override the default implementation for optimization.
For example, SqliteBackend could use a single recursive CTE query
instead of the iterative component-by-component approach.
§Example
use anyfs_backend::{FsPath, FsRead, FsLink};
use std::path::Path;
// Generic function that works with any FsPath implementation
fn resolve<B: FsPath>(backend: &B) -> Result<(), anyfs_backend::FsError> {
// Resolve symlinks and normalize path
let path = backend.canonicalize(Path::new("/some/path/../file.txt"))?;
// Resolve parent, allow non-existent final component
let new_path = backend.soft_canonicalize(Path::new("/dir/new_file.txt"))?;
Ok(())
}Provided Methods§
Sourcefn canonicalize(&self, path: &Path) -> Result<PathBuf, FsError>
fn canonicalize(&self, path: &Path) -> Result<PathBuf, FsError>
Resolve all symlinks and normalize path (., ..).
All path components must exist. Returns error if any component is missing or a symlink loop is detected.
§Arguments
path- The path to canonicalize
§Returns
The fully resolved canonical path.
§Errors
FsError::NotFound- A component doesn’t existFsError::InvalidData- Symlink loop detected (exceeded max depth)
§Example
use anyfs_backend::FsPath;
use std::path::{Path, PathBuf};
// Generic function that demonstrates canonicalize
fn resolve_link<B: FsPath>(backend: &B) -> Result<PathBuf, anyfs_backend::FsError> {
// Given: /link -> /target, /target/file.txt exists
let path = backend.canonicalize(Path::new("/link/file.txt"))?;
// Result: PathBuf::from("/target/file.txt")
Ok(path)
}Sourcefn soft_canonicalize(&self, path: &Path) -> Result<PathBuf, FsError>
fn soft_canonicalize(&self, path: &Path) -> Result<PathBuf, FsError>
Like canonicalize, but allows non-existent final component.
Resolves parent path fully, appends final component lexically.
This is useful for write() operations where the target file
doesn’t exist yet.
§Arguments
path- The path to soft-canonicalize
§Returns
The resolved path with the final component appended lexically.
§Errors
FsError::NotFound- A parent component doesn’t existFsError::InvalidData- Symlink loop detected
§Example
use anyfs_backend::FsPath;
use std::path::{Path, PathBuf};
// Generic function that demonstrates soft_canonicalize
fn resolve_new_file<B: FsPath>(backend: &B) -> Result<PathBuf, anyfs_backend::FsError> {
// Given: /dir exists, /dir/new_file.txt does NOT exist
let path = backend.soft_canonicalize(Path::new("/dir/new_file.txt"))?;
// Result: PathBuf::from("/dir/new_file.txt")
Ok(path)
}