blob: 48564008c3d11a8b720363dd1b399543e1a57bd1 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
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
}
}
|