0.34.6_repo_cleanup
This commit is contained in:
1 parent
ce1a90397c
commit
133d2f0f91
273 files changed
+5969
-6926
No files matched your search
+60
-28
@@ -1,12 +1,11 @@
|
||||
// Bound candidate retention while scanning potentially enormous rotation folders.
|
||||
// The heap's top is the least desirable result: oldest timestamp, then
|
||||
// lexicographically last name. Final output preserves newest/name order.
|
||||
fn newest_log_files(
|
||||
candidates: impl Iterator<Item = (String, std::path::PathBuf, u64)>,
|
||||
limit: usize,
|
||||
) -> Vec<(String, std::path::PathBuf, u64)> {
|
||||
use std::{cmp::Reverse, collections::BinaryHeap};
|
||||
if limit == 0 { return Vec::new(); }
|
||||
if limit == 0 {
|
||||
return Vec::new();
|
||||
}
|
||||
let mut bounded = BinaryHeap::with_capacity(limit);
|
||||
for (name, path, modified_ms) in candidates {
|
||||
let item = (Reverse(modified_ms), name, path);
|
||||
@@ -17,14 +16,14 @@ fn newest_log_files(
|
||||
bounded.push(item);
|
||||
}
|
||||
}
|
||||
let mut result = bounded.into_iter()
|
||||
let mut result = bounded
|
||||
.into_iter()
|
||||
.map(|(Reverse(modified), name, path)| (name, path, modified))
|
||||
.collect::<Vec<_>>();
|
||||
result.sort_by(|a, b| b.2.cmp(&a.2).then_with(|| a.0.cmp(&b.0)));
|
||||
result
|
||||
}
|
||||
|
||||
// Bounded file-log inventory helpers and gzip tail reader.
|
||||
fn rotated_gzip_sources(
|
||||
active: &std::path::Path,
|
||||
id_prefix: &str,
|
||||
@@ -33,25 +32,52 @@ fn rotated_gzip_sources(
|
||||
enabled: bool,
|
||||
format: &'static str,
|
||||
) -> Vec<FileLogSource> {
|
||||
let Some(parent) = active.parent() else { return Vec::new(); };
|
||||
let Some(base) = active.file_name().and_then(|name| name.to_str()) else { return Vec::new(); };
|
||||
let safe = std::fs::symlink_metadata(parent).ok().is_some_and(|meta| meta.file_type().is_dir() && !meta.file_type().is_symlink());
|
||||
if !safe { return Vec::new(); }
|
||||
let Some(parent) = active.parent() else {
|
||||
return Vec::new();
|
||||
};
|
||||
let Some(base) = active.file_name().and_then(|name| name.to_str()) else {
|
||||
return Vec::new();
|
||||
};
|
||||
let safe = std::fs::symlink_metadata(parent)
|
||||
.ok()
|
||||
.is_some_and(|meta| meta.file_type().is_dir() && !meta.file_type().is_symlink());
|
||||
if !safe {
|
||||
return Vec::new();
|
||||
}
|
||||
let prefix = format!("{base}.");
|
||||
let entries = newest_log_files(std::fs::read_dir(parent).ok().into_iter().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") { return None; }
|
||||
let file_type = entry.file_type().ok()?;
|
||||
if !file_type.is_file() || file_type.is_symlink() { return None; }
|
||||
let meta = entry.metadata().ok()?;
|
||||
Some((name, entry.path(), file_modified_ms(&meta)))
|
||||
}), 64);
|
||||
entries.into_iter().map(|(name,path,_)| source_from_path(
|
||||
format!("{id_prefix}:gz:{name}"),
|
||||
format!("{label_prefix} · {name}"),
|
||||
id_prefix.to_owned(),
|
||||
kind, path, enabled, format
|
||||
)).collect()
|
||||
let entries = newest_log_files(
|
||||
std::fs::read_dir(parent)
|
||||
.ok()
|
||||
.into_iter()
|
||||
.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") {
|
||||
return None;
|
||||
}
|
||||
let file_type = entry.file_type().ok()?;
|
||||
if !file_type.is_file() || file_type.is_symlink() {
|
||||
return None;
|
||||
}
|
||||
let meta = entry.metadata().ok()?;
|
||||
Some((name, entry.path(), file_modified_ms(&meta)))
|
||||
}),
|
||||
64,
|
||||
);
|
||||
entries
|
||||
.into_iter()
|
||||
.map(|(name, path, _)| {
|
||||
source_from_path(
|
||||
format!("{id_prefix}:gz:{name}"),
|
||||
format!("{label_prefix} · {name}"),
|
||||
id_prefix.to_owned(),
|
||||
kind,
|
||||
path,
|
||||
enabled,
|
||||
format,
|
||||
)
|
||||
})
|
||||
.collect()
|
||||
}
|
||||
|
||||
fn open_file_log(path: &std::path::Path) -> std::io::Result<std::fs::File> {
|
||||
@@ -81,8 +107,6 @@ fn read_tail(
|
||||
let mut file = open_file_log(&source.path)?;
|
||||
let length = file.metadata()?.len();
|
||||
if source.compressed {
|
||||
// gzip streams are not seekable. Read a complete bounded rotation, then
|
||||
// keep only the same 2 MiB decompressed tail used for plain files.
|
||||
if length > budget || length > FILE_LOG_GZIP_INPUT_MAX {
|
||||
return Ok((Vec::new(), length.min(budget), true));
|
||||
}
|
||||
@@ -108,7 +132,11 @@ fn read_tail(
|
||||
}
|
||||
let text = String::from_utf8_lossy(&content);
|
||||
let mut iter = text.lines().rev();
|
||||
let lines = iter.by_ref().take(line_budget).map(str::to_owned).collect::<Vec<_>>();
|
||||
let lines = iter
|
||||
.by_ref()
|
||||
.take(line_budget)
|
||||
.map(str::to_owned)
|
||||
.collect::<Vec<_>>();
|
||||
return Ok((lines, length, truncated || iter.next().is_some()));
|
||||
}
|
||||
let bytes = length.min(budget).min(FILE_LOG_SCAN_BYTES_PER_SOURCE);
|
||||
@@ -126,6 +154,10 @@ fn read_tail(
|
||||
}
|
||||
let text = String::from_utf8_lossy(&content);
|
||||
let mut iter = text.lines().rev();
|
||||
let lines = iter.by_ref().take(line_budget).map(str::to_owned).collect::<Vec<_>>();
|
||||
let lines = iter
|
||||
.by_ref()
|
||||
.take(line_budget)
|
||||
.map(str::to_owned)
|
||||
.collect::<Vec<_>>();
|
||||
Ok((lines, bytes, truncated || iter.next().is_some()))
|
||||
}
|
||||
Reference in new issue
Block a user