This commit is contained in:
Mateusz Gruszczyński committed 2026-11-23 23:03:30 +01:00
1 parent 96eec3b1c6
commit b9a5b3c153
30 files changed
+1100 -374

No files matched your search

+28 -8
View File
@@ -24,6 +24,8 @@ fn newest_log_files(
result
}
/// Rotated copies of a global log: `<file>.N`, `<file>.N.gz` (logrotate with or
/// without `delaycompress`) and other `<file>.*.gz`, newest rotation first.
fn rotated_gzip_sources(
active: &std::path::Path,
id_prefix: &str,
@@ -44,7 +46,6 @@ fn rotated_gzip_sources(
if !safe {
return Vec::new();
}
let prefix = format!("{base}.");
let entries = newest_log_files(
std::fs::read_dir(parent)
.ok()
@@ -52,7 +53,8 @@ fn rotated_gzip_sources(
.flat_map(|iter| iter.filter_map(|entry| entry.ok()))
.filter_map(|entry| {
let name = entry.file_name().to_string_lossy().into_owned();
if name.len() > 180 || !name.starts_with(&prefix) || !name.ends_with(".gz") {
let rest = name.strip_prefix(base)?;
if name.len() > 180 || rest.is_empty() || rotation_suffix(rest).is_none() {
return None;
}
let file_type = entry.file_type().ok()?;
@@ -64,20 +66,38 @@ fn rotated_gzip_sources(
}),
64,
);
entries
let mut sources: Vec<FileLogSource> = entries
.into_iter()
.map(|(name, path, _)| {
source_from_path(
format!("{id_prefix}:gz:{name}"),
.filter_map(|(name, path, _)| {
let (rotation, compressed) = rotation_suffix(&name[base.len()..])?;
let mut source = source_from_path(
if compressed {
format!("{id_prefix}:gz:{name}")
} else {
format!("{id_prefix}:file:{name}")
},
format!("{label_prefix} · {name}"),
id_prefix.to_owned(),
kind,
path,
enabled,
format,
)
);
source.rotation = rotation;
Some(source)
})
.collect()
.collect();
sort_by_rotation(&mut sources);
sources
}
/// Newest data first: the file being written, then `.1`, `.2`, … (ties by mtime).
fn sort_by_rotation(sources: &mut [FileLogSource]) {
sources.sort_by(|a, b| {
a.rotation
.cmp(&b.rotation)
.then_with(|| b.modified_ms.cmp(&a.modified_ms))
.then_with(|| a.id.cmp(&b.id))
});
}
fn open_file_log(path: &std::path::Path) -> std::io::Result<std::fs::File> {