#![allow(clippy::result_unit_err)]
use std::{
collections::HashMap,
fs,
path::{Path, PathBuf},
sync::{Arc, Mutex},
time::SystemTime,
};
pub fn path_is_symlink(path: &Path) -> bool {
if fs::symlink_metadata(path).is_ok_and(|m| m.file_type().is_symlink()) {
return true;
}
match trimmed_spelling(path) {
Some(trimmed) if trimmed.as_os_str() != path.as_os_str() => {
fs::symlink_metadata(&trimmed).is_ok_and(|m| m.file_type().is_symlink())
}
_ => false,
}
}
fn trimmed_spelling(path: &Path) -> Option<PathBuf> {
let name = path.file_name()?;
let parent = path.parent()?;
Some(parent.join(name))
}
pub fn lexical_identity(path: &Path) -> PathBuf {
if !path.is_absolute() {
return path.to_path_buf();
}
let mut out = PathBuf::from(std::path::MAIN_SEPARATOR_STR);
for component in path.components() {
match component {
std::path::Component::CurDir => {}
std::path::Component::ParentDir => {
out.pop();
}
other => out.push(other.as_os_str()),
}
}
out
}
pub fn resolve_as_far_as_possible(path: &Path) -> PathBuf {
if let Ok(canonical) = fs::canonicalize(path) {
return canonical;
}
resolve_beneath_existing_ancestor(path)
}
fn resolve_beneath_existing_ancestor(path: &Path) -> PathBuf {
let mut current = path;
let mut tail: Vec<std::ffi::OsString> = Vec::new();
while let Some(name) = current.file_name() {
tail.push(name.to_os_string());
let Some(parent) = current.parent() else {
break;
};
if let Ok(canonical) = fs::canonicalize(parent) {
let mut resolved = canonical;
for part in tail.iter().rev() {
resolved.push(part);
}
return resolved;
}
if parent.as_os_str().is_empty() {
break;
}
current = parent;
}
path.to_path_buf()
}
#[derive(Clone, Debug, Eq, PartialEq)]
pub struct ResolvedRoot {
pub identity: PathBuf,
pub spelling: PathBuf,
pub error: Option<String>,
}
pub fn resolve_root(path: &Path) -> ResolvedRoot {
match fs::canonicalize(path) {
Ok(canonical) => ResolvedRoot {
identity: lexical_identity(&canonical),
spelling: canonical,
error: None,
},
Err(e) => {
let resolved = resolve_beneath_existing_ancestor(path);
ResolvedRoot {
identity: lexical_identity(&resolved),
spelling: path.to_path_buf(),
error: Some(e.to_string()),
}
}
}
}
pub fn collapse_outermost(roots: Vec<PathBuf>) -> Vec<ResolvedRoot> {
collapse_resolved_protecting(resolve_roots(roots), &[])
}
pub fn collapse_outermost_protecting(
roots: Vec<PathBuf>,
protected: &[PathBuf],
) -> Vec<ResolvedRoot> {
collapse_resolved_protecting(resolve_roots(roots), protected)
}
pub fn collapse_resolved(resolved: Vec<ResolvedRoot>) -> Vec<ResolvedRoot> {
collapse_resolved_protecting(resolved, &[])
}
fn resolve_roots(roots: Vec<PathBuf>) -> Vec<ResolvedRoot> {
roots.into_iter().map(|root| resolve_root(&root)).collect()
}
fn collapse_resolved_protecting(
mut resolved: Vec<ResolvedRoot>,
protected: &[PathBuf],
) -> Vec<ResolvedRoot> {
resolved.sort_by(|a, b| a.spelling.cmp(&b.spelling));
resolved.dedup_by(|a, b| a.spelling == b.spelling);
let mut collapsed: Vec<ResolvedRoot> = Vec::new();
for root in resolved {
if protected.contains(&root.identity)
|| !collapsed
.iter()
.any(|parent: &ResolvedRoot| root.identity.starts_with(&parent.identity))
{
collapsed.push(root);
}
}
collapsed
}
fn contains_vcs_marker(path: &Path) -> bool {
fs::read_dir(path).ok().is_some_and(|mut children| {
children.any(|child| {
child
.ok()
.is_some_and(|child| is_vcs_name(&child.file_name().to_string_lossy()))
})
})
}
#[derive(Clone, Default)]
struct NestedRepoMarkers(Arc<Mutex<HashMap<PathBuf, bool>>>);
impl NestedRepoMarkers {
fn probe(&self, path: &Path) -> bool {
if let Ok(known) = self.0.lock()
&& let Some(found) = known.get(path)
{
return *found;
}
let found = contains_vcs_marker(path);
if let Ok(mut known) = self.0.lock() {
known.insert(path.to_path_buf(), found);
}
found
}
}
pub fn worker_threads() -> usize {
std::thread::available_parallelism()
.map_or(1, usize::from)
.clamp(1, 8)
}
fn walk_options() -> dua_core::Options {
dua_core::Options::default()
}
#[derive(Clone, Debug, Default)]
pub struct TargetStats {
pub bytes: u64,
pub entries: u64,
pub newest: Option<SystemTime>,
pub uncertain: bool,
pub package_dir: Option<PathBuf>,
}
fn manifest_package_dir(path: &Path) -> Option<PathBuf> {
let name = path.file_name()?;
if !crate::domain::is_manifest_shaped_name(name) {
return None;
}
path.parent()
.filter(|parent| !parent.as_os_str().is_empty())
.map(Path::to_path_buf)
}
fn newest_time(current: Option<SystemTime>, next: SystemTime) -> Option<SystemTime> {
Some(current.map_or(next, |old| old.max(next)))
}
fn is_vcs_name(name: &str) -> bool {
matches!(name, ".git" | ".hg" | ".svn")
}
pub fn measure_single_target(target: &Path) -> TargetStats {
let mut stats = TargetStats::default();
match fs::symlink_metadata(target) {
Ok(meta) if meta.is_dir() && !meta.file_type().is_symlink() => {}
_ => {
stats.uncertain = true;
return stats;
}
}
let root = target.to_path_buf();
let walk = dua_core::walk(
&root,
worker_threads(),
dua_core::Order::ParentFirst,
walk_options(),
|_| true,
);
for item in walk {
let entry = match item {
Ok(entry) => entry,
Err(_) => {
stats.uncertain = true;
break;
}
};
let path = entry.path();
if path == root {
continue;
}
let meta = match fs::symlink_metadata(&path) {
Ok(meta) => meta,
Err(_) => {
stats.uncertain = true;
break;
}
};
let modified = match meta.modified() {
Ok(time) => time,
Err(_) => {
stats.uncertain = true;
break;
}
};
stats.newest = newest_time(stats.newest, modified);
stats.entries = match stats.entries.checked_add(1) {
Some(n) => n,
None => {
stats.uncertain = true;
break;
}
};
if stats.package_dir.is_none() {
stats.package_dir = manifest_package_dir(&path);
}
if meta.is_file() {
match filesize::file_real_size_fast(&path, &meta) {
Ok(size) => match stats.bytes.checked_add(size) {
Some(n) => stats.bytes = n,
None => {
stats.uncertain = true;
break;
}
},
Err(_) => {
stats.uncertain = true;
break;
}
}
}
}
stats
}
pub fn measure_many_targets(targets: &[(usize, PathBuf)]) -> Vec<(usize, TargetStats)> {
let mut ordered: Vec<(usize, PathBuf)> = targets.iter().map(|(i, p)| (*i, p.clone())).collect();
ordered.sort_by_key(|(i, _)| *i);
let mut stats_by_index: std::collections::HashMap<usize, TargetStats> =
std::collections::HashMap::new();
let mut roots: Vec<(usize, PathBuf)> = Vec::new();
let mut seen_indices: std::collections::HashSet<usize> = std::collections::HashSet::new();
for (index, path) in &ordered {
if !seen_indices.insert(*index) {
continue;
}
let mut stats = TargetStats::default();
match fs::symlink_metadata(path) {
Ok(meta) if meta.is_dir() && !meta.file_type().is_symlink() => {
roots.push((*index, path.clone()));
stats_by_index.insert(*index, stats);
}
_ => {
stats.uncertain = true;
stats_by_index.insert(*index, stats);
}
}
}
if roots.is_empty() {
return targets
.iter()
.map(|(i, _)| (*i, stats_by_index.get(i).cloned().unwrap_or_default()))
.collect();
}
let root_paths: std::collections::HashMap<usize, PathBuf> = roots.iter().cloned().collect();
let walk = dua_core::walk_roots(
roots,
worker_threads(),
dua_core::Order::ParentFirst,
walk_options(),
|_, _| true,
);
for (index, event) in walk {
match event {
dua_core::RootEvent::Entry(Ok(entry)) => {
let Some(stats) = stats_by_index.get_mut(&index) else {
continue;
};
if stats.uncertain {
continue;
}
let path = entry.path();
let Some(root) = root_paths.get(&index) else {
stats.uncertain = true;
continue;
};
if path == *root {
continue;
}
let meta = match fs::symlink_metadata(&path) {
Ok(meta) => meta,
Err(_) => {
stats.uncertain = true;
continue;
}
};
let modified = match meta.modified() {
Ok(time) => time,
Err(_) => {
stats.uncertain = true;
continue;
}
};
stats.newest = newest_time(stats.newest, modified);
match stats.entries.checked_add(1) {
Some(n) => stats.entries = n,
None => {
stats.uncertain = true;
continue;
}
}
if stats.package_dir.is_none() {
stats.package_dir = manifest_package_dir(&path);
}
if meta.is_file() {
match filesize::file_real_size_fast(&path, &meta) {
Ok(size) => match stats.bytes.checked_add(size) {
Some(n) => stats.bytes = n,
None => stats.uncertain = true,
},
Err(_) => stats.uncertain = true,
}
}
}
dua_core::RootEvent::Entry(Err(_)) => {
if let Some(stats) = stats_by_index.get_mut(&index) {
stats.uncertain = true;
}
}
dua_core::RootEvent::Finished => {}
}
}
targets
.iter()
.map(|(i, _)| (*i, stats_by_index.get(i).cloned().unwrap_or_default()))
.collect()
}
#[derive(Clone, Debug)]
pub enum SourceActivity {
Recent(SystemTime),
Quiet(Option<SystemTime>),
}
pub fn source_activity(
project: &Path,
target: &Path,
_start: SystemTime,
cutoff: SystemTime,
) -> Result<SourceActivity, ()> {
let project_root = project.to_path_buf();
let target_root = target.to_path_buf();
let is_recent = move |time: SystemTime| time >= cutoff;
let walk_root = project_root.clone();
let descend_project = project_root.clone();
let descend_target = target_root.clone();
let markers = NestedRepoMarkers::default();
let descend_markers = markers.clone();
let walk = dua_core::walk(
&walk_root,
worker_threads(),
dua_core::Order::ParentFirst,
walk_options(),
move |entry| {
let path = entry.path();
if path != descend_project
&& (path == descend_target || path.starts_with(&descend_target))
{
return false;
}
if path != descend_project
&& path
.file_name()
.is_some_and(|n| is_vcs_name(&n.to_string_lossy()))
{
return false;
}
if path != descend_project && entry.file_type.is_dir() && descend_markers.probe(&path) {
return false;
}
true
},
);
let mut newest: Option<SystemTime> = None;
for item in walk {
let entry = match item {
Ok(entry) => entry,
Err(_) => return Err(()),
};
let path = entry.path();
if is_excluded(
&project_root,
&target_root,
&markers,
&path,
entry.file_type.is_dir(),
) {
continue;
}
let modified = fs::metadata(&path)
.map_err(|_| ())?
.modified()
.map_err(|_| ())?;
newest = newest_time(newest, modified);
if is_recent(modified) {
return Ok(SourceActivity::Recent(modified));
}
}
Ok(SourceActivity::Quiet(newest))
}
fn is_excluded(
project: &Path,
target: &Path,
markers: &NestedRepoMarkers,
path: &Path,
is_dir: bool,
) -> bool {
if path == project {
return false;
}
if path == target || path.starts_with(target) {
return true;
}
if path
.file_name()
.is_some_and(|n| is_vcs_name(&n.to_string_lossy()))
{
return true;
}
is_dir && markers.probe(path)
}
pub fn workspace_member_activity(
member_root: &Path,
output_roots: &[PathBuf],
_start: SystemTime,
cutoff: SystemTime,
) -> Result<SourceActivity, ()> {
let root = member_root.to_path_buf();
let outputs = output_roots.to_vec();
let outputs_for_closure = outputs.clone();
let is_recent = move |time: SystemTime| time >= cutoff;
let descend_root = root.clone();
let markers = NestedRepoMarkers::default();
let descend_markers = markers.clone();
let walk = dua_core::walk(
&root,
worker_threads(),
dua_core::Order::ParentFirst,
walk_options(),
move |entry| {
let path = entry.path();
if path != descend_root
&& outputs_for_closure
.iter()
.any(|o| path == *o || path.starts_with(o))
{
return false;
}
if path != descend_root
&& path
.file_name()
.is_some_and(|n| is_vcs_name(&n.to_string_lossy()))
{
return false;
}
if path != descend_root && entry.file_type.is_dir() && descend_markers.probe(&path) {
return false;
}
true
},
);
let mut newest: Option<SystemTime> = None;
for item in walk {
let entry = match item {
Ok(entry) => entry,
Err(_) => return Err(()),
};
let path = entry.path();
if is_excluded_multi(&root, &outputs, &markers, &path, entry.file_type.is_dir()) {
continue;
}
let modified = fs::metadata(&path)
.map_err(|_| ())?
.modified()
.map_err(|_| ())?;
newest = newest_time(newest, modified);
if is_recent(modified) {
return Ok(SourceActivity::Recent(modified));
}
}
Ok(SourceActivity::Quiet(newest))
}
fn is_excluded_multi(
member_root: &Path,
outputs: &[PathBuf],
markers: &NestedRepoMarkers,
path: &Path,
is_dir: bool,
) -> bool {
if path == member_root {
return false;
}
if outputs.iter().any(|o| path == *o || path.starts_with(o)) {
return true;
}
if path
.file_name()
.is_some_and(|n| is_vcs_name(&n.to_string_lossy()))
{
return true;
}
is_dir && markers.probe(path)
}
#[cfg(test)]
mod tests {
use super::*;
use std::time::Duration;
#[test]
fn worker_pool_is_bounded() {
let threads = worker_threads();
assert!((1..=8).contains(&threads));
}
#[test]
fn single_and_batched_target_measures_agree() {
let dir = tempfile::tempdir().unwrap();
let mut targets = Vec::new();
for name in ["a", "b", "c"] {
let target = dir.path().join(name);
fs::create_dir_all(&target).unwrap();
fs::write(target.join("artifact.bin"), vec![9u8; 4096]).unwrap();
targets.push(target);
}
let singles: Vec<TargetStats> = targets.iter().map(|t| measure_single_target(t)).collect();
assert!(singles.iter().all(|s| !s.uncertain && s.entries == 1));
let indexed: Vec<(usize, PathBuf)> = targets.iter().cloned().enumerate().collect();
let batched = measure_many_targets(&indexed);
assert_eq!(batched.len(), 3);
for ((_, batch), single) in batched.iter().zip(singles.iter()) {
assert!(!batch.uncertain);
assert_eq!(batch.entries, single.entries);
assert_eq!(batch.bytes, single.bytes);
}
}
#[test]
fn duplicate_candidate_indices_do_not_panic_and_are_measured_once() {
let dir = tempfile::tempdir().unwrap();
let first = dir.path().join("first");
let second = dir.path().join("second");
fs::create_dir_all(&first).unwrap();
fs::create_dir_all(&second).unwrap();
fs::write(first.join("artifact.bin"), vec![9u8; 4096]).unwrap();
fs::write(second.join("artifact.bin"), vec![9u8; 8192]).unwrap();
let targets = vec![
(7usize, first.clone()),
(7usize, second.clone()),
(9usize, second.clone()),
];
let measured = measure_many_targets(&targets);
assert_eq!(measured.len(), 3, "one result per input, in input order");
let seven: Vec<&TargetStats> = measured
.iter()
.filter(|(i, _)| *i == 7)
.map(|(_, s)| s)
.collect();
assert_eq!(seven.len(), 2);
assert!(seven.iter().all(|s| !s.uncertain && s.entries == 1));
assert!(
seven.windows(2).all(|w| w[0].bytes == w[1].bytes),
"the duplicated index must not accumulate bytes twice"
);
assert_eq!(seven[0].bytes, measure_single_target(&first).bytes);
let nine = measured.iter().find(|(i, _)| *i == 9).unwrap();
assert_eq!(nine.1.bytes, measure_single_target(&second).bytes);
}
#[test]
fn the_batched_measurement_returns_its_results_in_input_order() {
let dir = tempfile::tempdir().unwrap();
let mut targets = Vec::new();
for name in ["a", "b", "c"] {
let target = dir.path().join(name);
fs::create_dir_all(&target).unwrap();
fs::write(target.join("artifact.bin"), vec![9u8; 4096]).unwrap();
targets.push(target);
}
let indexed = vec![
(2usize, targets[0].clone()),
(0usize, targets[1].clone()),
(1usize, targets[2].clone()),
];
let measured = measure_many_targets(&indexed);
assert_eq!(measured.len(), 3, "one result per input");
assert_eq!(
measured.iter().map(|(i, _)| *i).collect::<Vec<_>>(),
vec![2, 0, 1],
"the results come back in the order the inputs were given"
);
for ((index, batch), (input_index, target)) in measured.iter().zip(indexed.iter()) {
assert_eq!(*index, *input_index);
let single = measure_single_target(target);
assert_eq!(batch.bytes, single.bytes);
assert_eq!(batch.entries, single.entries);
}
}
#[test]
fn a_package_directory_under_the_root_is_reported() {
let dir = tempfile::tempdir().unwrap();
let target = dir.path().join("target");
fs::create_dir_all(target.join("nested/src")).unwrap();
fs::write(target.join("nested/Cargo.toml"), b"[package]\n").unwrap();
fs::write(target.join("artifact.bin"), vec![9u8; 4096]).unwrap();
let stats = measure_single_target(&target);
assert_eq!(
stats.package_dir.as_deref(),
Some(target.join("nested").as_path()),
"a manifest under the root makes its directory a package directory"
);
let with_manifest = dir.path().join("root-with-manifest");
fs::create_dir_all(&with_manifest).unwrap();
fs::write(with_manifest.join("Cargo.toml"), b"[package]\n").unwrap();
let stats = measure_single_target(&with_manifest);
assert_eq!(
stats.package_dir.as_deref(),
Some(with_manifest.as_path()),
"a manifest at the root makes the root the package directory"
);
let batched = measure_many_targets(&[(0usize, target.clone())]);
assert_eq!(
batched[0].1.package_dir.as_deref(),
Some(target.join("nested").as_path())
);
}
#[test]
fn candidate_count_does_not_grow_worker_pool() {
let dir = tempfile::tempdir().unwrap();
let targets: Vec<(usize, PathBuf)> = (0..32)
.map(|i| {
let target = dir.path().join(format!("target-{i}"));
fs::create_dir_all(&target).unwrap();
fs::write(target.join("file.bin"), [i as u8; 64]).unwrap();
(i, target)
})
.collect();
assert!(worker_threads() <= 8);
let results = measure_many_targets(&targets);
assert_eq!(results.len(), 32);
assert!(results.iter().all(|(_, s)| !s.uncertain && s.entries == 1));
}
#[test]
fn synthetic_wide_and_deep_trees_count_entries() {
let dir = tempfile::tempdir().unwrap();
let wide = dir.path().join("wide");
fs::create_dir(&wide).unwrap();
for i in 0..64 {
fs::write(wide.join(format!("file-{i}.bin")), vec![1u8; 128]).unwrap();
}
let stats = measure_single_target(&wide);
assert!(!stats.uncertain);
assert_eq!(stats.entries, 64);
let mut deep = dir.path().join("deep");
for _ in 0..12 {
deep = deep.join("level");
fs::create_dir_all(&deep).unwrap();
}
fs::write(deep.join("leaf.bin"), vec![2u8; 256]).unwrap();
let stats = measure_single_target(&dir.path().join("deep"));
assert!(!stats.uncertain);
assert_eq!(stats.entries, 13);
}
#[test]
fn the_marker_memo_records_both_answers() {
let d = tempfile::tempdir().unwrap();
let plain = d.path().join("plain");
let nested = d.path().join("nested");
fs::create_dir_all(&plain).unwrap();
fs::create_dir_all(&nested).unwrap();
fs::write(nested.join(".git"), b"").unwrap();
let markers = NestedRepoMarkers::default();
assert!(!markers.probe(&plain), "a plain directory holds no marker");
assert!(markers.probe(&nested), "a nested repository holds a marker");
let recorded = markers.0.lock().unwrap();
assert_eq!(
recorded.get(&plain),
Some(&false),
"a negative answer must be recorded: the miss is the common one"
);
assert_eq!(recorded.get(&nested), Some(&true));
}
#[test]
fn source_scan_short_circuits_recent_files() {
let dir = tempfile::tempdir().unwrap();
fs::write(dir.path().join("fresh.rs"), "recent").unwrap();
let now = SystemTime::now() + Duration::from_secs(1);
let start = now;
let cutoff = now - Duration::from_secs(300);
let target = dir.path().join("target");
fs::create_dir(&target).unwrap();
match source_activity(dir.path(), &target, start, cutoff).unwrap() {
SourceActivity::Recent(_) => {}
SourceActivity::Quiet(_) => panic!("fresh source must short-circuit"),
}
}
#[test]
#[cfg(unix)]
fn path_is_symlink_recognises_every_spelling_of_a_link() {
use std::os::unix::fs::symlink;
let d = tempfile::tempdir().unwrap();
let target = d.path().join("target");
fs::create_dir(&target).unwrap();
let link = d.path().join("link");
symlink(&target, &link).unwrap();
assert!(path_is_symlink(&link), "the plain spelling");
assert!(
path_is_symlink(&link.join("")),
"a trailing separator must not hide the link"
);
assert!(
path_is_symlink(Path::new(&format!("{}/.", link.display()))),
"a trailing dot component must not hide the link"
);
let plain = d.path().join("plain");
fs::create_dir(&plain).unwrap();
assert!(!path_is_symlink(&plain), "a plain directory is not a link");
assert!(
!path_is_symlink(&d.path().join("nowhere")),
"a path that does not exist is not a link"
);
}
#[test]
#[cfg(unix)]
fn a_unix_directory_named_with_a_trailing_backslash_is_not_a_symlink() {
let d = tempfile::tempdir().unwrap();
let plain = d.path().join("link\\");
fs::create_dir_all(&plain).unwrap();
assert!(
plain.exists(),
"the fixture is a real directory with a backslash in its name"
);
assert!(
!path_is_symlink(&plain),
"a backslash is an ordinary filename character on Unix"
);
}
#[test]
#[cfg(target_os = "linux")]
fn a_non_utf8_link_spelled_with_a_trailing_separator_is_still_a_symlink() {
use std::os::unix::ffi::OsStringExt;
use std::os::unix::fs::symlink;
let d = tempfile::tempdir().unwrap();
let target = d.path().join("target");
fs::create_dir(&target).unwrap();
let name = std::ffi::OsString::from_vec(b"bad-\xff".to_vec());
let link = d.path().join(name);
symlink(&target, &link).unwrap();
assert!(path_is_symlink(&link), "the plain spelling");
let slashed = link.join("");
assert!(
path_is_symlink(&slashed),
"a trailing separator must not hide a link whose name is not UTF-8"
);
}
}