diff options
| author | Ruben Beltran del Rio <jj@r.bdr.sh> | 2026-04-30 15:17:30 +0200 |
|---|---|---|
| committer | Ruben Beltran del Rio <jj@r.bdr.sh> | 2026-04-30 15:37:23 +0200 |
| commit | 8cc11c52a88ec78d175b3ec8de854916f631caab (patch) | |
| tree | 4ad0bc1664a97418d0475b0b730c54d874953348 /src/cleanup.rs | |
Initial build
Diffstat (limited to 'src/cleanup.rs')
| -rw-r--r-- | src/cleanup.rs | 72 |
1 files changed, 72 insertions, 0 deletions
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 + } +} |