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
use {
crate::*,
anyhow::*,
std::{
path::{Path, PathBuf},
},
};
fn find_files(
path: PathBuf,
files: &mut Vec<PathBuf>,
check_name: bool,
check_deeper_names: bool,
) -> Result<()> {
if path.is_dir() {
for entry in path.read_dir()? {
find_files(entry?.path(), files, check_deeper_names, check_deeper_names)?;
}
} else if !check_name || is_access_log_path(&path) {
files.push(path);
}
Ok(())
}
pub struct FileFinder {
root: PathBuf,
check_names: bool,
}
impl FileFinder {
pub fn new(
root: PathBuf,
check_names: bool,
) -> Self {
Self {
root,
check_names,
}
}
pub fn dated_files(self) -> Result<Vec<(Date, PathBuf)>> {
let mut files = Vec::new();
find_files(self.root, &mut files, false, self.check_names)?;
let mut dated_files = Vec::new();
for path in files.drain(..) {
if let Some(date) = get_file_first_date(&path)? {
dated_files.push((date, path));
} else {
debug!("no date found in {:?}", path);
}
}
dated_files.sort_unstable_by_key(|t| t.0);
Ok(dated_files)
}
}
pub fn is_access_log_path(path: &Path) -> bool {
path.file_name()
.and_then(|n| n.to_str())
.map_or(false, |name| {
name.contains("access.log")
})
}