dir_size

Function dir_size 

Source
pub fn dir_size(path: &Path) -> Result<u64>
Expand description

Calculates the total size of a directory and all its contents recursively.

This function traverses the directory tree and sums the sizes of all regular files. It handles nested directories and provides the total disk usage for the directory tree.

§Arguments

  • path - The directory to calculate size for

§Returns

The total size in bytes, or an error if the directory cannot be read

§Examples

use agpm_cli::utils::fs::dir_size;
use std::path::Path;

let cache_size = dir_size(Path::new("~/.agpm/cache"))?;
println!("Cache size: {} bytes ({:.2} MB)", cache_size, cache_size as f64 / 1024.0 / 1024.0);

§Behavior

  • Recursively traverses all subdirectories
  • Includes only regular files in size calculation
  • Does not follow symbolic links
  • Returns 0 for empty directories
  • Accumulates sizes using 64-bit integers (supports very large directories)

§Performance

This is a synchronous operation that may take time for large directory trees. For better performance with large directories, use get_directory_size which runs the calculation on a separate thread.

§See Also

  • get_directory_size for async version
  • Platform-specific tools may be faster for very large directories