From 8cc11c52a88ec78d175b3ec8de854916f631caab Mon Sep 17 00:00:00 2001 From: Ruben Beltran del Rio Date: Thu, 30 Apr 2026 15:17:30 +0200 Subject: Initial build --- src/cleanup.rs | 72 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 72 insertions(+) create mode 100644 src/cleanup.rs (limited to 'src/cleanup.rs') diff --git a/src/cleanup.rs b/src/cleanup.rs new file mode 100644 index 0000000..4856400 --- /dev/null +++ b/src/cleanup.rs @@ -0,0 +1,72 @@ +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 + } +} -- cgit