Disk usage: size of file or that of a directory tree using Rust

walkdir path is_file metadata

One of the things I wanted to measure for the Rust Digger project is the size of the Crates and the the size of the repositories that are used for the development of crates. For this I needed some code that can traverse a directory tree and then I needed to get the size of each file.

This is going to be the "net size" of the project. The actual disk-space it needs is probably bigger as files are stored in block on the disk, but for now that was not interesting for me.

The dependencies for the code

examples/disk-usage/Cargo.toml

[package]
name = "disk-usage"
version = "0.1.0"
edition = "2021"

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
walkdir = "2.5.0"

The code

examples/disk-usage/src/main.rs

use walkdir::WalkDir;

fn main() {
    let args = std::env::args().collect::<Vec<String>>();
    if args.len() != 2 {
        eprintln!("Usage: {} PATH", args[0]);
        std::process::exit(1);
    }
    let root = &args[1];

    let size = du(root);
    println!("{root} size: {size}");

}

fn du(root: &str) -> u128 {
    let mut size = 0;
    for entry in WalkDir::new(root) {
        let entry = entry.unwrap();
        if entry.path().is_file() {
            if let Ok(meta) = entry.path().metadata() {
                size += meta.len() as u128;
            }
        }
    }

    size
}

As we saw earlier using the walkdir it is actually quite easy to traverse a directory tree. We can then get the Metadata struct and from that we can get the size of a file. I am not sure if I really need this, but I went for the safe side and set the returned number to be a u128 which can contain a number up to 340,282,366,920,938,463,463,374,607,431,768,211,455 according to the table of minimum and maximum values of the numerical types I put together earlier.

OK, I guess I could have used u64 and probably u32 would be enough as that's 4Gb.

Related Pages

Files - dealing with files in Rust

Author

Gabor Szabo (szabgab)

Gabor Szabo, the author of the Rust Maven web site maintains several Open source projects in Rust and while he still feels he has tons of new things to learn about Rust he already offers training courses in Rust and still teaches Python, Perl, git, GitHub, GitLab, CI, and testing.

Gabor Szabo