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
use std::fs;
use std::path::Path;
use chrono::{DateTime, UTC, Duration, TimeZone};
use filetime::FileTime;
use walkdir::WalkDir;
use super::LalResult;
fn clean_in_dir(cutoff: DateTime<UTC>, dirs: WalkDir) -> LalResult<()> {
let drs = dirs.into_iter().filter_map(|e| e.ok()).filter(|e| e.path().is_dir());
for d in drs {
let pth = d.path();
trace!("Checking {}", pth.to_str().unwrap());
let mtime = FileTime::from_last_modification_time(&d.metadata().unwrap());
let mtimedate = UTC.ymd(1970, 1, 1).and_hms(0, 0, 0) +
Duration::seconds(mtime.seconds_relative_to_1970() as i64);
trace!("Found {} with mtime {}", pth.to_str().unwrap(), mtimedate);
if mtimedate < cutoff {
debug!("Cleaning {}", pth.to_str().unwrap());
fs::remove_dir_all(pth)?;
}
}
Ok(())
}
pub fn clean(cachedir: &str, days: i64) -> LalResult<()> {
let cutoff = UTC::now() - Duration::days(days);
debug!("Cleaning all artifacts from before {}", cutoff);
let edir = Path::new(&cachedir).join("environments");
let edirs = WalkDir::new(&edir).min_depth(3).max_depth(3);
clean_in_dir(cutoff, edirs)?;
let dirs = WalkDir::new(&cachedir).min_depth(3).max_depth(3);
clean_in_dir(cutoff, dirs)?;
Ok(())
}