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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
use crate::SuffixInfo;
#[cfg(feature = "chrono04")]
use chrono::{offset::Local, Duration, NaiveDateTime};
use std::{
cmp::Ordering,
collections::BTreeSet,
io,
path::{Path, PathBuf},
};
pub trait Representation: Ord + ToString + Eq + Clone + std::fmt::Debug {
fn to_path(&self, basepath: &Path) -> PathBuf {
PathBuf::from(format!("{}.{}", basepath.display(), self.to_string()))
}
}
pub trait SuffixScheme {
type Repr: Representation;
fn rotate_file(
&mut self,
basepath: &Path,
newest_suffix: Option<&Self::Repr>,
suffix: &Option<Self::Repr>,
) -> io::Result<Self::Repr>;
fn parse(&self, suffix: &str) -> Option<Self::Repr>;
fn too_old(&self, suffix: &Self::Repr, file_number: usize) -> bool;
fn scan_suffixes(&self, basepath: &Path) -> BTreeSet<SuffixInfo<Self::Repr>> {
let mut suffixes = BTreeSet::new();
let filename_prefix = basepath
.file_name()
.expect("basepath.file_name()")
.to_string_lossy();
let basepath = if basepath.is_relative() {
let mut path = std::env::current_dir().unwrap();
path.push(basepath);
path
} else {
basepath.to_path_buf()
};
let parent = basepath.parent().unwrap();
let filenames = std::fs::read_dir(parent)
.unwrap()
.filter_map(|entry| entry.ok())
.filter(|entry| entry.path().is_file())
.map(|entry| entry.file_name());
for filename in filenames {
let filename = filename.to_string_lossy();
if !filename.starts_with(&*filename_prefix) {
continue;
}
let (filename, compressed) = prepare_filename(&*filename);
let suffix_str = filename.strip_prefix(&format!("{}.", filename_prefix));
if let Some(suffix) = suffix_str.and_then(|s| self.parse(s)) {
suffixes.insert(SuffixInfo { suffix, compressed });
}
}
suffixes
}
}
fn prepare_filename(path: &str) -> (&str, bool) {
path.strip_suffix(".gz")
.map(|x| (x, true))
.unwrap_or((path, false))
}
pub struct AppendCount {
max_files: usize,
}
impl AppendCount {
pub fn new(max_files: usize) -> Self {
Self { max_files }
}
}
impl Representation for usize {}
impl SuffixScheme for AppendCount {
type Repr = usize;
fn rotate_file(
&mut self,
_basepath: &Path,
_: Option<&usize>,
suffix: &Option<usize>,
) -> io::Result<usize> {
Ok(match suffix {
Some(suffix) => suffix + 1,
None => 1,
})
}
fn parse(&self, suffix: &str) -> Option<usize> {
suffix.parse::<usize>().ok()
}
fn too_old(&self, _suffix: &usize, file_number: usize) -> bool {
file_number >= self.max_files
}
}
#[cfg(feature = "chrono04")]
pub struct AppendTimestamp {
pub format: &'static str,
pub file_limit: FileLimit,
}
#[cfg(feature = "chrono04")]
impl AppendTimestamp {
pub fn default(file_limit: FileLimit) -> Self {
Self {
format: "%Y%m%dT%H%M%S",
file_limit,
}
}
pub fn with_format(format: &'static str, file_limit: FileLimit) -> Self {
Self { format, file_limit }
}
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct TimestampSuffix {
pub timestamp: String,
pub number: Option<usize>,
}
impl Representation for TimestampSuffix {}
impl Ord for TimestampSuffix {
fn cmp(&self, other: &Self) -> Ordering {
match other.timestamp.cmp(&self.timestamp) {
Ordering::Equal => other.number.cmp(&self.number),
unequal => unequal,
}
}
}
impl PartialOrd for TimestampSuffix {
fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
Some(self.cmp(other))
}
}
impl std::fmt::Display for TimestampSuffix {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> Result<(), std::fmt::Error> {
match self.number {
Some(n) => write!(f, "{}.{}", self.timestamp, n),
None => write!(f, "{}", self.timestamp),
}
}
}
#[cfg(feature = "chrono04")]
impl SuffixScheme for AppendTimestamp {
type Repr = TimestampSuffix;
fn rotate_file(
&mut self,
_basepath: &Path,
newest_suffix: Option<&TimestampSuffix>,
suffix: &Option<TimestampSuffix>,
) -> io::Result<TimestampSuffix> {
assert!(suffix.is_none());
if suffix.is_none() {
let now = Local::now().format(self.format).to_string();
let number = if let Some(newest_suffix) = newest_suffix {
if newest_suffix.timestamp == now {
Some(newest_suffix.number.unwrap_or(0) + 1)
} else {
None
}
} else {
None
};
Ok(TimestampSuffix {
timestamp: now,
number,
})
} else {
Err(io::Error::new(
io::ErrorKind::InvalidData,
"Critical error in file-rotate algorithm",
))
}
}
fn parse(&self, suffix: &str) -> Option<Self::Repr> {
let (timestamp_str, n) = if let Some(dot) = suffix.find('.') {
if let Ok(n) = suffix[(dot + 1)..].parse::<usize>() {
(&suffix[..dot], Some(n))
} else {
return None;
}
} else {
(suffix, None)
};
NaiveDateTime::parse_from_str(timestamp_str, self.format)
.map(|_| TimestampSuffix {
timestamp: timestamp_str.to_string(),
number: n,
})
.ok()
}
fn too_old(&self, suffix: &TimestampSuffix, file_number: usize) -> bool {
match self.file_limit {
FileLimit::MaxFiles(max_files) => file_number >= max_files,
FileLimit::Age(age) => {
let old_timestamp = (Local::now() - age).format(self.format).to_string();
suffix.timestamp < old_timestamp
}
}
}
}
#[cfg(feature = "chrono04")]
pub enum FileLimit {
MaxFiles(usize),
Age(Duration),
}
#[cfg(test)]
mod test {
use super::*;
use std::fs::File;
#[test]
fn timestamp_ordering() {
assert!(
TimestampSuffix {
timestamp: "2021".to_string(),
number: None
} < TimestampSuffix {
timestamp: "2020".to_string(),
number: None
}
);
assert!(
TimestampSuffix {
timestamp: "2021".to_string(),
number: Some(1)
} < TimestampSuffix {
timestamp: "2021".to_string(),
number: None
}
);
}
#[test]
fn scan_suffixes() {
let working_dir = tempdir::TempDir::new("file-rotate").unwrap();
let working_dir = working_dir.path().join("dir");
let suffix_scheme = AppendTimestamp::default(FileLimit::Age(Duration::weeks(1)));
for relative_path in ["logs/log", "./log", "log", "../log", "../logs/log"] {
std::fs::create_dir_all(&working_dir).unwrap();
println!("Testing relative path: {}", relative_path);
let relative_path = Path::new(relative_path);
let log_file = working_dir.join(relative_path);
let log_dir = log_file.parent().unwrap();
std::fs::create_dir_all(log_dir).unwrap();
std::env::set_current_dir(&working_dir).unwrap();
File::create(working_dir.join(&relative_path)).unwrap();
let canonicalized = relative_path.canonicalize().unwrap();
let relative_dir = canonicalized.parent().unwrap();
File::create(relative_dir.join("log.20210911T121830")).unwrap();
File::create(relative_dir.join("log.20210911T121831.gz")).unwrap();
let paths = suffix_scheme.scan_suffixes(relative_path);
assert_eq!(paths.len(), 2);
std::fs::remove_dir_all(&working_dir).unwrap();
}
}
}