use std::fs; use std::path::Path; use std::process::ExitCode; use std::time::{Duration, SystemTime}; const ONE_WEEK: Duration = Duration::from_secs(7 * 24 * 60 * 60); pub fn run(dir: &str) -> ExitCode { let entries = match fs::read_dir(Path::new(dir)) { Ok(e) => e, Err(e) => { eprintln!("estampa: cannot read directory '{dir}': {e}"); return ExitCode::from(1); } }; let now = SystemTime::now(); let mut had_error = false; for entry in entries { let entry = match entry { Ok(e) => e, Err(e) => { eprintln!("estampa: cannot read directory entry: {e}"); had_error = true; continue; } }; let path = entry.path(); // symlink_metadata so we never follow links out of the dir let metadata = match entry.path().symlink_metadata() { Ok(m) => m, Err(e) => { eprintln!("estampa: cannot stat '{}': {e}", path.display()); had_error = true; continue; } }; if !metadata.is_file() { continue; } let modified = match metadata.modified() { Ok(m) => m, Err(e) => { eprintln!("estampa: cannot read mtime for '{}': {e}", path.display()); had_error = true; continue; } }; let Ok(age) = now.duration_since(modified) else { continue; }; if age > ONE_WEEK && let Err(e) = fs::remove_file(&path) { eprintln!("estampa: cannot remove '{}': {e}", path.display()); had_error = true; } } if had_error { ExitCode::from(1) } else { ExitCode::SUCCESS } }