use anyhow::{Context, Result};
use serde::{Deserialize, Serialize};
use std::collections::{BTreeMap, BTreeSet};
use std::fs;
use std::path::{Component, Path, PathBuf};
pub const RECORD_FILE: &str = ".rsconstruct/outputs.json";
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
struct Stamp {
size: u64,
mtime_ns: u64,
}
fn stamp(path: &Path) -> Option<Stamp> {
let meta = fs::symlink_metadata(path).ok()?;
if !meta.is_file() {
return None;
}
let mtime = meta
.modified()
.ok()?
.duration_since(std::time::UNIX_EPOCH)
.ok()?;
Some(Stamp {
size: meta.len(),
mtime_ns: u64::try_from(mtime.as_nanos()).ok()?,
})
}
fn inside_project(path: &Path) -> bool {
path.components()
.all(|c| matches!(c, Component::Normal(_) | Component::CurDir))
}
pub fn tracked<'a>(outputs: impl IntoIterator<Item = &'a PathBuf>) -> BTreeSet<PathBuf> {
outputs
.into_iter()
.filter(|p| inside_project(p))
.cloned()
.collect()
}
fn load(record: &Path) -> Result<Option<BTreeMap<PathBuf, Stamp>>> {
let text = match fs::read_to_string(record) {
Ok(text) => text,
Err(e) if e.kind() == std::io::ErrorKind::NotFound => return Ok(None),
Err(e) => {
return Err(e).with_context(|| format!("Failed to read {}", record.display()));
}
};
serde_json::from_str(&text).map(Some).with_context(|| {
format!(
"Failed to parse {}; delete it and the next build starts a new record",
record.display()
)
})
}
#[derive(Debug, Default)]
pub struct Removal {
pub removed: Vec<PathBuf>,
pub changed: Vec<PathBuf>,
pub tracked: Vec<PathBuf>,
}
pub fn remove_orphans(
root: &Path,
current: &BTreeSet<PathBuf>,
git_tracked: impl FnOnce() -> Result<BTreeSet<PathBuf>>,
) -> Result<Removal> {
let mut removal = Removal::default();
let Some(previous) = load(&root.join(RECORD_FILE))? else {
return Ok(removal);
};
let mut candidates: Vec<PathBuf> = Vec::new();
for (path, recorded) in previous {
if current.contains(&path) || !inside_project(&path) {
continue;
}
let full = root.join(&path);
match stamp(&full) {
None if fs::symlink_metadata(&full).is_err() => {}
Some(now) if now == recorded => candidates.push(path),
_ => removal.changed.push(path),
}
}
if candidates.is_empty() {
return Ok(removal);
}
let tracked = git_tracked()?;
for path in candidates {
if tracked.contains(&path) {
removal.tracked.push(path);
continue;
}
let full = root.join(&path);
fs::remove_file(&full).with_context(|| {
format!(
"Failed to remove output no longer produced: {}",
full.display()
)
})?;
remove_empty_parents(root, &path)?;
removal.removed.push(path);
}
Ok(removal)
}
pub fn git_tracked(ctx: &crate::build_context::BuildContext) -> Result<BTreeSet<PathBuf>> {
let mut probe = std::process::Command::new("git");
probe.args(["rev-parse", "--is-inside-work-tree"]);
let inside = crate::processor::run_command_capture(ctx, &probe)
.context("Failed to run git rev-parse")?;
if !inside.status.success() {
return Ok(BTreeSet::new());
}
let mut cmd = std::process::Command::new("git");
cmd.args(["ls-files", "-z", "--cached"]);
let output =
crate::processor::run_command_capture(ctx, &cmd).context("Failed to run git ls-files")?;
if !output.status.success() {
anyhow::bail!(
"git ls-files failed:\n{}",
String::from_utf8_lossy(&output.stderr)
);
}
Ok(String::from_utf8_lossy(&output.stdout)
.split('\0')
.filter(|s| !s.is_empty())
.map(PathBuf::from)
.collect())
}
fn remove_empty_parents(root: &Path, path: &Path) -> Result<()> {
let mut dir = path.parent();
while let Some(d) = dir.filter(|d| !d.as_os_str().is_empty()) {
let full = root.join(d);
match fs::remove_dir(&full) {
Ok(()) => dir = d.parent(),
Err(e)
if matches!(
e.kind(),
std::io::ErrorKind::DirectoryNotEmpty | std::io::ErrorKind::NotFound
) =>
{
break;
}
Err(e) => {
return Err(e).with_context(|| {
format!("Failed to remove empty directory {}", full.display())
});
}
}
}
Ok(())
}
pub fn write_record(root: &Path, current: &BTreeSet<PathBuf>) -> Result<()> {
let stamps: BTreeMap<&PathBuf, Stamp> = current
.iter()
.filter_map(|path| stamp(&root.join(path)).map(|s| (path, s)))
.collect();
let text = serde_json::to_string(&stamps).expect(crate::errors::JSON_SERIALIZE);
let record = root.join(RECORD_FILE);
if let Some(dir) = record.parent() {
fs::create_dir_all(dir)
.with_context(|| format!("Failed to create directory {}", dir.display()))?;
}
fs::write(&record, text).with_context(|| format!("Failed to write {}", record.display()))
}
#[cfg(test)]
mod tests {
use super::*;
fn set(paths: &[&str]) -> BTreeSet<PathBuf> {
paths.iter().map(PathBuf::from).collect()
}
#[test]
fn removes_outputs_no_longer_produced() {
let dir = tempfile::tempdir().unwrap();
let root = dir.path();
fs::create_dir_all(root.join("out/a/b")).unwrap();
fs::write(root.join("out/a/b/gone.html"), "x").unwrap();
fs::write(root.join("out/kept.html"), "y").unwrap();
write_record(root, &set(&["out/a/b/gone.html", "out/kept.html"])).unwrap();
let removal =
remove_orphans(root, &set(&["out/kept.html"]), || Ok(BTreeSet::new())).unwrap();
assert_eq!(removal.removed, vec![PathBuf::from("out/a/b/gone.html")]);
assert!(removal.changed.is_empty() && removal.tracked.is_empty());
assert!(
!root.join("out/a").exists(),
"empty directories are removed"
);
assert!(root.join("out/kept.html").exists());
}
#[test]
fn keeps_orphans_changed_since() {
let dir = tempfile::tempdir().unwrap();
let root = dir.path();
fs::create_dir_all(root.join("out")).unwrap();
fs::write(root.join("out/edited.html"), "x").unwrap();
write_record(root, &set(&["out/edited.html"])).unwrap();
fs::write(root.join("out/edited.html"), "edited by hand").unwrap();
let removal = remove_orphans(root, &set(&[]), || Ok(BTreeSet::new())).unwrap();
assert_eq!(removal.removed, Vec::<PathBuf>::new());
assert_eq!(removal.changed, vec![PathBuf::from("out/edited.html")]);
assert!(root.join("out/edited.html").exists());
}
#[test]
fn keeps_orphans_git_tracks() {
let dir = tempfile::tempdir().unwrap();
let root = dir.path();
fs::write(root.join("README.md"), "x").unwrap();
write_record(root, &set(&["README.md"])).unwrap();
let removal = remove_orphans(root, &set(&[]), || Ok(set(&["README.md"]))).unwrap();
assert_eq!(removal.removed, Vec::<PathBuf>::new());
assert_eq!(removal.tracked, vec![PathBuf::from("README.md")]);
assert!(root.join("README.md").exists());
}
#[test]
fn git_is_asked_only_when_something_would_be_removed() {
let dir = tempfile::tempdir().unwrap();
let root = dir.path();
fs::write(root.join("a.txt"), "x").unwrap();
write_record(root, &set(&["a.txt"])).unwrap();
let removal = remove_orphans(root, &set(&["a.txt"]), || {
panic!("git must not be asked when nothing is orphaned")
})
.unwrap();
assert_eq!(removal.removed, Vec::<PathBuf>::new());
}
#[test]
fn no_record_and_missing_orphans_remove_nothing() {
let dir = tempfile::tempdir().unwrap();
let root = dir.path();
let removal = remove_orphans(root, &set(&[]), || Ok(BTreeSet::new())).unwrap();
assert!(removal.removed.is_empty() && removal.changed.is_empty());
fs::write(root.join("a.txt"), "x").unwrap();
write_record(root, &set(&["a.txt"])).unwrap();
fs::remove_file(root.join("a.txt")).unwrap();
let removal = remove_orphans(root, &set(&[]), || Ok(BTreeSet::new())).unwrap();
assert!(removal.removed.is_empty() && removal.changed.is_empty());
}
#[test]
fn only_paths_inside_the_project_are_tracked() {
let outputs = [
PathBuf::from("out/a"),
PathBuf::from("/etc/passwd"),
PathBuf::from("../other/x"),
];
assert_eq!(tracked(&outputs), set(&["out/a"]));
}
}