use super::*;
pub(in crate::daemon::server) fn touch_mtime(path: &Path) {
if mtime_floor_disabled() {
return;
}
let _ = floor_artifact_mtime_to_sibling_max(path);
}
pub(in crate::daemon::server) fn floor_materialized_outputs_to_input_max<'a>(
output_paths: impl IntoIterator<Item = &'a Path>,
input_paths: impl IntoIterator<Item = &'a Path>,
minimum_mtime: std::time::SystemTime,
) {
if mtime_floor_disabled() {
return;
}
let outputs: Vec<&Path> = output_paths.into_iter().collect();
if outputs.is_empty() {
return;
}
let mut max_mtime = minimum_mtime;
for path in outputs.iter().copied().chain(input_paths) {
let Ok(mtime) = std::fs::metadata(path).and_then(|metadata| metadata.modified()) else {
continue;
};
if mtime > max_mtime {
max_mtime = mtime;
}
}
let ft = filetime::FileTime::from_system_time(max_mtime);
for path in outputs {
let Ok(current) = std::fs::metadata(path).and_then(|metadata| metadata.modified()) else {
continue;
};
if current < max_mtime {
let _ = filetime::set_file_mtime(path, ft);
}
}
}
fn mtime_floor_disabled() -> bool {
use std::sync::OnceLock;
static CACHED: OnceLock<bool> = OnceLock::new();
*CACHED.get_or_init(|| {
std::env::var("ZCCACHE_DISABLE_MTIME_FLOOR")
.ok()
.is_some_and(|v| !v.is_empty() && v != "0")
})
}
pub(in crate::daemon::server) fn floor_artifact_mtime_to_sibling_max(
path: &Path,
) -> std::io::Result<()> {
let parent = match path.parent() {
Some(p) => p,
None => return Ok(()),
};
let my_mtime = std::fs::metadata(path)?.modified()?;
let mut max_mtime = my_mtime;
for entry in std::fs::read_dir(parent)?.flatten() {
let p = entry.path();
if p == path {
continue;
}
let ext = match p.extension().and_then(|s| s.to_str()) {
Some(e) => e,
None => continue,
};
if !matches!(
ext,
"rlib" | "rmeta" | "so" | "dylib" | "dll" | "exe" | "a" | "lib"
) {
continue;
}
if let Ok(m) = entry.metadata().and_then(|md| md.modified()) {
if m > max_mtime {
max_mtime = m;
}
}
}
if max_mtime > my_mtime {
let ft = filetime::FileTime::from_system_time(max_mtime);
let _ = filetime::set_file_mtime(path, ft);
}
Ok(())
}