pub async fn get_directory_size(path: &Path) -> Result<u64>Expand description
Asynchronously calculates the total size of a directory and all its contents.
This is the async version of dir_size that runs the calculation on a separate
thread to avoid blocking the async runtime. Use this when calculating directory
sizes as part of async operations.
§Arguments
path- The directory to calculate size for
§Returns
The total size in bytes, or an error if the operation fails
§Examples
use agpm_cli::utils::fs::get_directory_size;
use std::path::Path;
let cache_size = get_directory_size(Path::new("~/.agpm/cache")).await?;
println!("Cache size: {} bytes", cache_size);§Performance
This function uses tokio::task::spawn_blocking to run the directory traversal
on a thread pool, preventing it from blocking other async tasks. This is particularly
useful when:
- Calculating sizes for multiple directories concurrently
- Integrating with async workflows
- Avoiding blocking in async web servers or CLI applications
§See Also
dir_sizefor synchronous version