Expand description
File system utilities for cross-platform file operations
This module provides safe, atomic file operations designed to work consistently across Windows, macOS, and Linux. All functions handle platform-specific differences such as path lengths, permissions, and separators.
§Key Features
- Atomic operations: Files are written atomically to prevent corruption
- Cross-platform: Handles Windows long paths, Unix permissions, and path separators
- Parallel operations: Async versions for processing multiple files concurrently
- Safety: Path traversal prevention and safe path handling
- Checksum validation: SHA-256 checksums for data integrity
§Examples
use agpm_cli::utils::fs::{ensure_dir, safe_write, calculate_checksum};
use std::path::Path;
// Create directory structure
ensure_dir(Path::new("output/agents"))?;
// Write file atomically
safe_write(Path::new("output/config.toml"), "[sources]")?;
// Verify file integrity
let checksum = calculate_checksum(Path::new("output/config.toml"))?;
println!("File checksum: {}", checksum);§Platform Considerations
§Windows
- Supports long paths (>260 characters) using UNC prefixes
- Handles case-insensitive file systems
- Manages file permissions and attributes correctly
§Unix/Linux
- Preserves file permissions during copy operations
- Handles case-sensitive file systems
- Supports symbolic links appropriately
§macOS
- Handles HFS+ case-insensitive by default
- Supports extended attributes
- Works with case-sensitive APFS volumes
Structs§
- TempDir
- A temporary directory that automatically cleans up when dropped.
Functions§
- atomic_
write - Atomically writes bytes to a file using a write-then-rename strategy.
- atomic_
write_ multiple - Writes multiple files atomically in parallel.
- calculate_
checksum - Calculates the SHA-256 checksum of a file.
- calculate_
checksums_ parallel - Calculates SHA-256 checksums for multiple files concurrently.
- compare_
file_ times - Compares the modification times of two files.
- copy_
dir - Recursively copies a directory and all its contents to a new location.
- copy_
dir_ all - Copy a directory recursively (alias for consistency)
- copy_
dirs_ parallel - Copies multiple directories concurrently.
- copy_
files_ parallel - Copies multiple files concurrently using parallel processing.
- create_
temp_ file - Creates a temporary file with content for testing.
- dir_
size - Calculates the total size of a directory and all its contents recursively.
- ensure_
dir - Ensures a directory exists, creating it and all parent directories if necessary.
- ensure_
dir_ exists - Alias for
ensure_dirfor consistency - ensure_
parent_ dir - Ensures that the parent directory of a file path exists.
- file_
exists_ and_ readable - Checks if a file exists and is readable.
- find_
files - Recursively finds files matching a pattern in a directory tree.
- find_
project_ root - Finds the AGPM project root by searching for
agpm.tomlin the directory hierarchy. - get_
directory_ size - Asynchronously calculates the total size of a directory and all its contents.
- get_
global_ config_ path - Returns the path to the global AGPM configuration file.
- get_
modified_ time - Gets the modification time of a file.
- is_
safe_ path - Checks if a path is safe and doesn’t escape the base directory.
- normalize_
path - Normalizes a path by resolving
.and..components. - read_
files_ parallel - Reads multiple files concurrently and returns their contents.
- read_
json_ file - Reads and parses a JSON file.
- read_
text_ file - Reads a text file with proper error handling and context.
- read_
toml_ file - Reads and parses a TOML file.
- read_
yaml_ file - Reads and parses a YAML file.
- remove_
dir_ all - Recursively removes a directory and all its contents.
- safe_
write - Safely writes a string to a file using atomic operations.
- write_
json_ file - Writes data as JSON to a file atomically.
- write_
text_ file - Writes a text file atomically with proper error handling.
- write_
toml_ file - Writes data as TOML to a file atomically.
- write_
yaml_ file - Writes data as YAML to a file atomically.