#![allow(clippy::result_unit_err)]
use std::{
fs,
path::{Path, PathBuf},
time::SystemTime,
};
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,
}
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 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 ordered
.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 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 => {}
}
}
ordered
.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 || time > start;
let walk_root = project_root.clone();
let descend_project = project_root.clone();
let descend_target = target_root.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()
&& 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()))
})
})
{
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, &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, 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;
}
if is_dir
&& 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()))
})
})
{
return true;
}
false
}
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 || time > start;
let descend_root = root.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()
&& 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()))
})
})
{
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, &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], 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;
}
if is_dir
&& 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()))
})
})
{
return true;
}
false
}
#[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 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 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"),
}
}
}