use std::{collections::HashSet, path::Path};
pub(crate) async fn size(path: &Path) -> u64 {
let path = path.to_path_buf();
tokio::task::spawn_blocking(move || walk(&path, &mut HashSet::new()))
.await
.unwrap_or_default()
}
type FileId = (u64, u64);
fn walk(dir: &Path, seen: &mut HashSet<FileId>) -> u64 {
let Ok(entries) = std::fs::read_dir(dir) else {
return 0;
};
let mut bytes = 0;
for entry in entries.flatten() {
match entry.file_type() {
Ok(kind) if kind.is_dir() => bytes += walk(&entry.path(), seen),
Ok(_) => {
let Ok(meta) = entry.metadata() else {
continue;
};
if linked(&meta).is_none_or(|id| seen.insert(id)) {
bytes += meta.len();
}
}
Err(_) => {}
}
}
bytes
}
#[cfg(unix)]
fn linked(meta: &std::fs::Metadata) -> Option<FileId> {
use std::os::unix::fs::MetadataExt;
(meta.nlink() > 1).then(|| (meta.dev(), meta.ino()))
}
#[cfg(not(unix))]
fn linked(_meta: &std::fs::Metadata) -> Option<FileId> {
None
}
#[cfg(test)]
#[path = "size_tests.rs"]
mod tests;