pub fn safe_canonicalize(path: &Path) -> Result<PathBuf>Expand description
Canonicalizes a path with proper cross-platform handling.
This function resolves a path to its canonical, absolute form while handling
platform-specific issues like Windows long paths. It resolves symbolic links,
removes . and .. components, and ensures the path is absolute.
§Arguments
path- The path to canonicalize
§Returns
The canonical absolute path, or an error if canonicalization fails
§Examples
use agpm_cli::utils::platform::safe_canonicalize;
use std::path::Path;
// Canonicalize a relative path
let canonical = safe_canonicalize(Path::new("../src/main.rs"))?;
println!("Canonical path: {}", canonical.display());
// Works with existing files and directories
let current_dir = safe_canonicalize(Path::new("."))?;
println!("Current directory: {}", current_dir.display());§Features
- Cross-platform: Works on Windows, macOS, and Linux
- Long path support: Handles Windows paths >260 characters
- Symlink resolution: Follows symbolic links to their targets
- Path normalization: Removes
.and..components - Absolute paths: Always returns absolute paths
§Error Cases
- Path does not exist
- Permission denied accessing path components
- Invalid path characters for the platform
- Path too long (even with Windows long path support)
- Circular symbolic links
§Platform Notes
- Windows: Automatically applies long path prefixes when needed
- Unix-like: Resolves symbolic links following POSIX semantics
- All platforms: Provides helpful error messages for common issues
§Security
This function safely resolves paths and prevents directory traversal by returning absolute, normalized paths.
§See Also
normalize_pathfor logical path normalization (no filesystem access)windows_long_pathfor Windows-specific path handling