use std::path::{Path, PathBuf};
use rayon::prelude::*;
#[cfg(unix)]
pub(crate) fn file_size(metadata: &std::fs::Metadata) -> u64 {
use std::os::unix::fs::MetadataExt;
metadata.blocks() * 512
}
#[cfg(not(unix))]
pub(crate) fn file_size(metadata: &std::fs::Metadata) -> u64 {
metadata.len()
}
#[derive(Debug, Clone, Copy, Default, PartialEq, Eq)]
#[non_exhaustive]
pub struct Measured {
pub bytes: u64,
pub unreadable: usize,
}
impl Measured {
#[must_use]
pub fn is_partial(&self) -> bool {
self.unreadable > 0
}
}
impl std::ops::Add for Measured {
type Output = Self;
fn add(self, other: Self) -> Self {
Self {
bytes: self.bytes + other.bytes,
unreadable: self.unreadable + other.unreadable,
}
}
}
impl std::ops::AddAssign for Measured {
fn add_assign(&mut self, other: Self) {
*self = *self + other;
}
}
#[must_use]
pub fn measure(path: &Path) -> u64 {
measure_fully(path).bytes
}
#[must_use]
pub fn measure_fully(path: &Path) -> Measured {
let Ok(metadata) = std::fs::symlink_metadata(path) else {
return Measured::default();
};
if !metadata.is_dir() {
return Measured {
bytes: file_size(&metadata),
unreadable: 0,
};
}
#[cfg(unix)]
let below = crate::tree::measure_below(path);
#[cfg(not(unix))]
let below = measure_dir(path, FANOUT_DEPTH, &Links::default());
Measured {
bytes: file_size(&metadata) + below.bytes,
unreadable: below.unreadable,
}
}
#[must_use]
pub fn newest_mtime(path: &Path) -> Option<std::time::SystemTime> {
let metadata = std::fs::symlink_metadata(path).ok()?;
let mut newest = metadata.modified().ok();
if !metadata.is_dir() {
return newest;
}
let mut stack = vec![path.to_path_buf()];
while let Some(dir) = stack.pop() {
let Ok(entries) = std::fs::read_dir(&dir) else {
continue;
};
for entry in entries.flatten() {
let Ok(metadata) = entry.metadata() else {
continue;
};
if let Ok(modified) = metadata.modified() {
newest = Some(newest.map_or(modified, |current| current.max(modified)));
}
if metadata.is_dir() {
stack.push(entry.path());
}
}
}
newest
}
#[derive(Debug, Default)]
pub(crate) struct Links {
#[cfg(unix)]
shards: [std::sync::Mutex<std::collections::HashSet<(u64, u64)>>; LINK_SHARDS],
}
#[cfg(unix)]
const LINK_SHARDS: usize = 16;
impl Links {
#[cfg(unix)]
pub(crate) fn counted_inode(&self, dev: u64, ino: u64, nlink: u64) -> bool {
if nlink <= 1 {
return false;
}
let key = (dev, ino);
#[expect(clippy::cast_possible_truncation, reason = "a shard index, not a value")]
let shard = (key.1 as usize) % LINK_SHARDS;
self.shards[shard].lock().is_ok_and(|mut seen| !seen.insert(key))
}
#[cfg(not(unix))]
#[expect(
clippy::unused_self,
reason = "the unix arm needs `self`; this stub keeps the same signature"
)]
fn counted(&self, _metadata: &std::fs::Metadata) -> bool {
false
}
}
#[cfg(not(unix))]
const FANOUT_DEPTH: u32 = 10;
#[cfg(not(unix))]
fn measure_dir(dir: &Path, fanout: u32, seen: &Links) -> Measured {
let (mut total, subdirectories) = list(dir, seen);
if fanout == 0 {
for path in &subdirectories {
total += measure_serially(path, seen);
}
return total;
}
total
+ match subdirectories.as_slice() {
[] => Measured::default(),
[only] => measure_dir(only, fanout - 1, seen),
many => many
.par_iter()
.map(|path| measure_dir(path, fanout - 1, seen))
.reduce(Measured::default, |left, right| left + right),
}
}
#[cfg(not(unix))]
fn measure_serially(root: &Path, seen: &Links) -> Measured {
let mut total = Measured::default();
let mut stack = vec![root.to_path_buf()];
while let Some(dir) = stack.pop() {
let (measured, subdirectories) = list(&dir, seen);
total += measured;
stack.extend(subdirectories);
}
total
}
#[cfg(not(unix))]
fn list(dir: &Path, seen: &Links) -> (Measured, Vec<PathBuf>) {
let Ok(entries) = std::fs::read_dir(dir) else {
return (
Measured {
bytes: 0,
unreadable: 1,
},
Vec::new(),
);
};
let mut total = Measured::default();
let mut subdirectories = Vec::new();
for entry in entries.flatten() {
let Ok(metadata) = entry.metadata_no_follow() else {
continue;
};
if !seen.counted(&metadata) {
total.bytes += file_size(&metadata);
}
if metadata.is_dir() {
subdirectories.push(entry.path());
}
}
(total, subdirectories)
}
#[cfg(not(unix))]
trait NoFollow {
fn metadata_no_follow(&self) -> std::io::Result<std::fs::Metadata>;
}
#[cfg(not(unix))]
impl NoFollow for std::fs::DirEntry {
fn metadata_no_follow(&self) -> std::io::Result<std::fs::Metadata> {
self.metadata()
}
}
#[must_use]
pub fn measure_all(paths: &[PathBuf]) -> Vec<Measured> {
paths.par_iter().map(|path| measure_fully(path)).collect()
}
#[cfg(test)]
mod tests {
use super::*;
use crate::testing::tree;
#[test]
fn should_measure_a_file() {
let fixture = tree(&["file.txt"]);
assert!(measure(&fixture.path().join("file.txt")) > 0);
}
#[test]
fn should_find_the_newest_file_below_a_directory_rather_than_the_directory_itself() {
let fixture = tree(&["artifact/debug/deps/app.o"]);
let root = fixture.path().join("artifact");
let old = std::time::SystemTime::now() - std::time::Duration::from_secs(86_400 * 30);
std::fs::File::open(&root)
.expect("the fixture root opens")
.set_modified(old)
.expect("the fixture root can be dated");
let newest = newest_mtime(&root).expect("the tree has a timestamp");
assert!(
newest > old + std::time::Duration::from_secs(86_400),
"a fresh file below an old root is seen"
);
}
#[test]
fn should_measure_a_chain_deeper_than_any_build_tool_produces() {
let fixture = tempfile::TempDir::new().expect("a temp dir");
let mut deep = fixture.path().to_path_buf();
for _ in 0..400 {
deep.push("d");
}
std::fs::create_dir_all(&deep).expect("a deep chain");
std::fs::write(deep.join("leaf.o"), b"output").expect("a leaf file");
assert!(
measure(fixture.path()) > 0,
"a 400-deep chain is measured rather than overflowing the stack"
);
}
#[test]
fn should_measure_a_directory_recursively() {
let fixture = tree(&["target/a.o", "target/deep/b.o", "target/deep/deeper/c.o"]);
let shallow = tree(&["target/a.o"]);
assert!(
measure(&fixture.path().join("target")) > measure(&shallow.path().join("target")),
"a deeper tree must measure larger"
);
}
#[test]
fn should_return_zero_for_a_path_that_does_not_exist() {
let fixture = tree(&["Cargo.toml"]);
assert_eq!(measure(&fixture.path().join("nothing")), 0);
}
#[test]
#[cfg(unix)]
fn should_not_follow_a_symlink_when_measuring() {
let fixture = tree(&["big/one.bin", "big/two.bin", "big/three.bin"]);
std::os::unix::fs::symlink(fixture.path().join("big"), fixture.path().join("link")).expect("a symlink");
assert!(measure(&fixture.path().join("link")) < measure(&fixture.path().join("big")));
}
#[test]
#[cfg(unix)]
fn should_not_follow_a_symlink_found_inside_a_measured_tree() {
let fixture = tree(&["heavy/one.bin", "heavy/two.bin", "heavy/three.bin", "target/app"]);
let bare = measure(&fixture.path().join("target"));
std::os::unix::fs::symlink(fixture.path().join("heavy"), fixture.path().join("target/link"))
.expect("a symlink");
let with_link = measure(&fixture.path().join("target"));
assert!(
with_link - bare < measure(&fixture.path().join("heavy")),
"a link inside the tree added the weight of what it points at: {bare} -> {with_link}"
);
}
#[test]
#[cfg(unix)]
fn should_count_a_hard_linked_file_once() {
let fixture = tree(&["target/one.bin"]);
let one = fixture.path().join("target/one.bin");
std::fs::write(&one, vec![0xA5; 512 * 1024]).expect("something worth linking");
let alone = measure(&fixture.path().join("target"));
std::fs::hard_link(&one, fixture.path().join("target/two.bin")).expect("a hard link");
std::fs::hard_link(&one, fixture.path().join("target/three.bin")).expect("another");
let linked = measure(&fixture.path().join("target"));
assert_eq!(
linked, alone,
"three names for one extent must measure as one extent, not three"
);
}
#[test]
#[cfg(unix)]
fn should_count_a_shared_extent_in_each_artifact_that_holds_it() {
let fixture = tree(&["one/target/app", "two/target/app"]);
let shared = fixture.path().join("one/target/app");
std::fs::write(&shared, vec![0xA5; 512 * 1024]).expect("something worth linking");
std::fs::remove_file(fixture.path().join("two/target/app")).expect("making room");
std::fs::hard_link(&shared, fixture.path().join("two/target/app")).expect("a hard link");
let first = measure(&fixture.path().join("one/target"));
let second = measure(&fixture.path().join("two/target"));
assert_eq!(first, second, "each artifact measures what it holds");
assert!(first >= 512 * 1024, "and that is the extent, not zero: {first}");
}
#[test]
fn should_measure_many_paths_in_one_pass() {
let fixture = tree(&["a/x.o", "b/y.o"]);
let paths = vec![fixture.path().join("a"), fixture.path().join("b")];
let sizes = measure_all(&paths);
assert_eq!(sizes.len(), 2);
assert!(sizes.iter().all(|measured| measured.bytes > 0));
}
}