tighten export and capture handling

This commit is contained in:
2026-06-04 23:09:13 +03:00
parent a48d82ddc9
commit 4ca802ef71
14 changed files with 736 additions and 456 deletions
+23 -2
View File
@@ -148,8 +148,29 @@ impl Config {
match toml::from_str::<Self>(&raw) {
Ok(cfg) => Ok(cfg),
Err(e) => {
let backup = path.with_extension("toml.bak");
tracing::warn!("config.toml is corrupt ({}), backing up to {:?} and using defaults", e, backup);
// Use a timestamped backup so successive corruptions don't
// clobber each other. Falls back to .toml.bak if the
// timestamped name collides for some reason.
let ts = std::time::SystemTime::now()
.duration_since(std::time::UNIX_EPOCH)
.map(|d| d.as_secs())
.unwrap_or(0);
let stem = path
.file_stem()
.and_then(|s| s.to_str())
.unwrap_or("config");
let ext = path.extension().and_then(|s| s.to_str()).unwrap_or("toml");
let backup = path.with_file_name(format!("{stem}-{ts}.{ext}.bak"));
let backup = if backup.exists() {
path.with_extension("toml.bak")
} else {
backup
};
tracing::warn!(
"config.toml is corrupt ({}), backing up to {:?} and using defaults",
e,
backup
);
let _ = std::fs::copy(path, &backup);
let cfg = Self::default();
cfg.save(path)?;