From 131d85559ef68d5b846d3eef5f59e0f9dd85f055 Mon Sep 17 00:00:00 2001 From: mavrick Date: Mon, 9 Nov 2026 15:57:26 -0600 Subject: [PATCH 1/8] fix(settings): save preferences durably and report failures Use atomic managed-file replacement for preferences. Preserve malformed settings in an exclusively created private recovery copy before replacing them, and refuse when all recovery slots are occupied. Report unreadable settings, recovery paths and failed saves in the app, deduplicating repeated save failures until a save succeeds. Keep concurrent recovery files distinct without changing the settings merge policy. --- apps/rustgis/src/main.rs | 114 ++++++++++++++++++++++++++++++++++--- crates/engine/src/prefs.rs | 106 +++++++++++++++++++++++++++------- 2 files changed, 190 insertions(+), 30 deletions(-) diff --git a/apps/rustgis/src/main.rs b/apps/rustgis/src/main.rs index c42ef52ad6..f6c8ac7a0e 100644 --- a/apps/rustgis/src/main.rs +++ b/apps/rustgis/src/main.rs @@ -34,9 +34,13 @@ struct Menus { #[derive(Default)] struct Menus {} -/// Preferences in `prefs.json` beside the platform's other app settings. +/// Preferences in `prefs.json` beside the platform's other app settings. Settings that can't be +/// read or saved are said in the window, not only logged: a setting that looks saved but isn't +/// would be lost silently when RustGIS closes. mod prefs { use std::path::PathBuf; + use std::sync::Mutex; + use std::sync::atomic::{AtomicBool, Ordering}; use rustgis_ui_egui::Services; use serde_json::{Map, Value}; @@ -45,16 +49,84 @@ mod prefs { rustgis_engine::data::dirs::config_dir() } + /// A problem with the preferences file, waiting for the window to say it. + #[derive(Debug, PartialEq)] + pub enum Notice { + /// The file couldn't be read: the defaults are in use. + Unreadable(String), + /// A malformed file was moved here, and new settings started in its place. + SetAside(PathBuf), + /// A setting couldn't be saved. + NotSaved(String), + } + + static NOTICES: Mutex> = Mutex::new(Vec::new()); + /// The last save failed and said so: later failures stay quiet until a save works again. + static FAILING: AtomicBool = AtomicBool::new(false); + + /// The waiting notices; a panic while one was added doesn't lose the others. + fn notices() -> std::sync::MutexGuard<'static, Vec> { + NOTICES.lock().unwrap_or_else(|poisoned| poisoned.into_inner()) + } + + /// The saved preferences, or none when the file can't be read: said once a run. fn read() -> Map { - static WARNED: std::sync::atomic::AtomicBool = std::sync::atomic::AtomicBool::new(false); + static WARNED: AtomicBool = AtomicBool::new(false); rustgis_engine::prefs::read().unwrap_or_else(|error| { - if !WARNED.swap(true, std::sync::atomic::Ordering::Relaxed) { + if !WARNED.swap(true, Ordering::Relaxed) { log::warn!("{error}"); + notices().push(Notice::Unreadable(error)); } Map::new() }) } + /// Keeps what a save came to for the window: a malformed file set aside (which also explains + /// an earlier read failure still waiting), or the first of a run of failed saves. + pub fn saved(result: Result, String>) { + match result { + Ok(set_aside) => { + FAILING.store(false, Ordering::Relaxed); + if let Some(path) = set_aside { + log::warn!("unreadable preferences set aside as {}", path.display()); + let mut notices = notices(); + notices.retain(|n| !matches!(n, Notice::Unreadable(_))); + notices.push(Notice::SetAside(path)); + } + } + Err(error) => { + log::warn!("{error}"); + if !FAILING.swap(true, Ordering::Relaxed) { + notices().push(Notice::NotSaved(error)); + } + } + } + } + + /// The problems since the last call, oldest first. + pub fn take_notices() -> Vec { + std::mem::take(&mut *notices()) + } + + /// A notice's text in the current language. + pub fn text(notice: &Notice) -> String { + use rustgis_ui_egui::i18n::fmt; + match notice { + Notice::Unreadable(error) => { + fmt("Your settings couldn't be read ({error}), so RustGIS is using its defaults.", &[("error", error)]) + } + Notice::SetAside(path) => fmt( + "Your settings couldn't be read, so RustGIS started new ones. The old settings file was kept as {path}.", + &[("path", &path.display().to_string())], + ), + Notice::NotSaved(error) => { + fmt("Settings can't be saved ({error}): changes last only until RustGIS closes.", &[("error", error)]) + } + } + } + + /// The desktop's keychain and the preference hooks over `prefs.json`; saves report through + /// [`saved`]. pub fn services() -> Services { let secrets: std::sync::Arc = std::sync::Arc::new(rustgis_keychain::Keychain::default()); @@ -62,11 +134,7 @@ mod prefs { rustgis_assistant::chatgpt::set_secret_store(Some(secrets.clone())); Services { load_pref: Some(Box::new(|key: &str| read().get(key).and_then(Value::as_str).map(str::to_string))), - save_pref: Some(Box::new(|key: &str, value: &str| { - if let Err(error) = rustgis_engine::prefs::write(key, value) { - log::warn!("{error}"); - } - })), + save_pref: Some(Box::new(|key: &str, value: &str| saved(rustgis_engine::prefs::write(key, value)))), secrets: Some(secrets), ..Services::default() } @@ -276,6 +344,11 @@ impl eframe::App for App { } self.0.ui.titlebar = titlebar; } + // Settings that couldn't be read or saved, since the last frame. + for notice in prefs::take_notices() { + let text = prefs::text(¬ice); + self.0.session.notify(text, true); + } self.0.logic(ctx); #[cfg(target_os = "macos")] self.menus(ctx); @@ -620,7 +693,7 @@ fn main() -> eframe::Result { #[cfg(test)] #[allow(clippy::unwrap_used, clippy::panic)] mod tests { - use super::{backends, crash, flag, running}; + use super::{backends, crash, flag, prefs, running}; use eframe::wgpu::Backends; #[test] @@ -711,6 +784,29 @@ mod tests { assert!(super::recovery_store(Some(dir.join("fresh"))).is_ok()); } + #[test] + fn settings_that_cannot_be_saved_are_said_once_until_saving_works() { + use prefs::Notice; + // Clean saves say nothing. + prefs::saved(Ok(None)); + assert_eq!(prefs::take_notices(), []); + // A full disk: said once, not for every setting that follows. + prefs::saved(Err("could not save preferences: no space left".into())); + prefs::saved(Err("could not save preferences: no space left".into())); + assert_eq!(prefs::take_notices(), [Notice::NotSaved("could not save preferences: no space left".into())]); + // Once a save works, the next failure is news again. + prefs::saved(Ok(None)); + prefs::saved(Err("could not save preferences: read-only".into())); + assert_eq!(prefs::take_notices(), [Notice::NotSaved("could not save preferences: read-only".into())]); + prefs::saved(Ok(None)); + // A malformed file set aside says where it went. + let bad = std::path::PathBuf::from("prefs.json.bad"); + prefs::saved(Ok(Some(bad.clone()))); + assert_eq!(prefs::take_notices(), [Notice::SetAside(bad.clone())]); + let text = prefs::text(&Notice::SetAside(bad)); + assert!(text.contains("prefs.json.bad"), "{text}"); + } + #[test] fn crash_times_are_utc_dates() { assert_eq!(crash::utc(0), "1970-01-01 00:00:00"); diff --git a/crates/engine/src/prefs.rs b/crates/engine/src/prefs.rs index 4474db802c..10dd00fd41 100644 --- a/crates/engine/src/prefs.rs +++ b/crates/engine/src/prefs.rs @@ -4,7 +4,7 @@ use rustgis_data::secrets::{Reporting, SecretStore}; use serde_json::{Map, Value}; -use std::path::Path; +use std::path::{Path, PathBuf}; use std::sync::{Arc, Mutex}; pub const FILE: &str = "prefs.json"; @@ -34,33 +34,69 @@ fn read_at(path: &Path) -> Result, String> { } } -/// Updates one desktop preference, preserving all others. A malformed file is moved aside to -/// `prefs.json.bad` (kept for the user to repair) and a new one started; an unreadable one is -/// never overwritten. A temporary file and rename avoid truncating the old file on a crash. -pub fn write(key: &str, value: &str) -> Result<(), String> { +/// Updates one desktop preference, preserving all others. A malformed file is set aside (see +/// [`set_aside`]) and a new one started: returns where it was put. An unreadable one is never +/// overwritten. The new file is written under a name of its own beside the old one, synced to +/// disk, then renamed over it, so a crash or power cut leaves the old file or the new one, never +/// a partly written one, and two copies of RustGIS saving at once never share a temporary file. +pub fn write(key: &str, value: &str) -> Result, String> { let dir = rustgis_data::dirs::config_dir().ok_or("the settings folder isn't available")?; let _guard = WRITE_LOCK.lock().unwrap_or_else(|poisoned| poisoned.into_inner()); write_at(&dir.join(FILE), key, value) } -fn write_at(path: &Path, key: &str, value: &str) -> Result<(), String> { - let mut map = match read_at(path) { - Ok(map) => map, - Err(_) if path.is_file() && std::fs::read_to_string(path).is_ok() => { - std::fs::rename(path, path.with_extension("json.bad")) - .map_err(|error| format!("could not set aside the malformed preferences: {error}"))?; - Map::new() - } +/// [`write`] to the file at `path` (a test folder in tests): returns where a malformed file went. +fn write_at(path: &Path, key: &str, value: &str) -> Result, String> { + let (mut map, set_aside) = match read_at(path) { + Ok(map) => (map, None), + Err(_) if path.is_file() && std::fs::read_to_string(path).is_ok() => (Map::new(), Some(set_aside(path)?)), Err(error) => return Err(error), }; map.insert(key.to_string(), Value::String(value.to_string())); let text = serde_json::to_vec(&map).map_err(|_| "could not serialize preferences")?; let dir = path.parent().ok_or("the settings folder isn't available")?; - let temporary = path.with_extension("json.tmp"); - std::fs::create_dir_all(dir) - .and_then(|_| std::fs::write(&temporary, text)) - .and_then(|_| std::fs::rename(&temporary, path)) - .map_err(|error| format!("could not save preferences: {error}")) + std::fs::create_dir_all(dir).and_then(|_| rustgis_data::io::replace_atomic(path, &text)).map_err(|error| match &set_aside { + Some(backup) => format!("could not save preferences: {error}; malformed preferences were kept at {}", backup.display()), + None => format!("could not save preferences: {error}"), + })?; + Ok(set_aside) +} + +/// How many malformed preference files are kept: `prefs.json.bad`, then `prefs.json.bad2` to +/// `prefs.json.bad9`. Once all are taken, saving refuses to replace any recovery copy or the +/// malformed current file; the user can repair or move a copy to make room. +const KEPT_BAD: u32 = 9; + +/// Copies malformed preferences to an exclusively created `.bad` file, synced before saving +/// replaces the original. Concurrent callers cannot overwrite each other's recovery files. +/// A failed copy is removed; a failed later save leaves both original and recovery intact. +fn set_aside(path: &Path) -> Result { + let name = |n: u32| path.with_extension(if n == 1 { "json.bad".to_string() } else { format!("json.bad{n}") }); + let mut source = std::fs::File::open(path).map_err(|error| format!("could not preserve malformed preferences: {error}"))?; + for target in (1..=KEPT_BAD).map(name) { + let mut options = std::fs::OpenOptions::new(); + options.write(true).create_new(true); + #[cfg(unix)] + { + use std::os::unix::fs::OpenOptionsExt; + options.mode(0o600); + } + let mut backup = match options.open(&target) { + Ok(file) => file, + Err(error) if error.kind() == std::io::ErrorKind::AlreadyExists => continue, + Err(error) => return Err(format!("could not preserve malformed preferences: {error}")), + }; + if let Err(error) = std::io::copy(&mut source, &mut backup).and_then(|_| backup.sync_all()) { + drop(backup); + let _ = std::fs::remove_file(&target); + return Err(format!("could not preserve malformed preferences: {error}")); + } + return Ok(target); + } + Err(format!( + "all {KEPT_BAD} preference recovery files are occupied; repair or move one before saving settings; the malformed preferences were left unchanged at {}", + path.display() + )) } /// Opts a native caller into the desktop's saved connections and lazy credential store, which @@ -104,14 +140,32 @@ pub fn use_saved_secrets(s: &mut crate::Session, prefs: &Map, sto mod tests { use super::*; + #[test] + fn full_recovery_slots_leave_the_malformed_file_and_every_backup_unchanged() { + let dir = std::env::temp_dir().join(format!("rustgis-preferences-full-{}", std::process::id())); + std::fs::create_dir_all(&dir).unwrap(); + let path = dir.join(FILE); + std::fs::write(&path, "broken preferences").unwrap(); + let mut kept = Vec::new(); + for _ in 0..KEPT_BAD { + kept.push(set_aside(&path).unwrap()); + } + assert!(write_at(&path, "theme", "dark").unwrap_err().contains("left unchanged")); + assert_eq!(std::fs::read_to_string(&path).unwrap(), "broken preferences"); + for backup in kept { + assert_eq!(std::fs::read_to_string(backup).unwrap(), "broken preferences"); + } + std::fs::remove_dir_all(dir).unwrap(); + } + #[test] fn preference_updates_preserve_other_values_and_set_malformed_files_aside() { let dir = std::env::temp_dir().join(format!("rustgis-preferences-{}", std::process::id())); std::fs::create_dir_all(&dir).unwrap(); let path = dir.join(FILE); assert_eq!(read_at(&path).unwrap(), Map::new()); - write_at(&path, "connections", "saved definitions").unwrap(); - write_at(&path, "theme", "dark").unwrap(); + assert_eq!(write_at(&path, "connections", "saved definitions").unwrap(), None); + assert_eq!(write_at(&path, "theme", "dark").unwrap(), None); let map = read_at(&path).unwrap(); assert_eq!(map.get("connections"), Some(&Value::String("saved definitions".into()))); assert_eq!(map.get("theme"), Some(&Value::String("dark".into()))); @@ -119,9 +173,19 @@ mod tests { std::fs::write(&path, broken).unwrap(); let error = read_at(&path).unwrap_err(); assert!(!error.contains("prefs-sentinel")); - write_at(&path, "theme", "light").unwrap(); + assert_eq!(write_at(&path, "theme", "light").unwrap(), Some(dir.join("prefs.json.bad"))); assert_eq!(std::fs::read_to_string(dir.join("prefs.json.bad")).unwrap(), broken); assert_eq!(read_at(&path).unwrap().get("theme"), Some(&Value::String("light".into()))); + // A file cut short (as a power cut can leave one) is set aside beside the first, not over it. + std::fs::write(&path, "").unwrap(); + assert_eq!(write_at(&path, "theme", "dark").unwrap(), Some(dir.join("prefs.json.bad2"))); + assert_eq!(std::fs::read_to_string(dir.join("prefs.json.bad")).unwrap(), broken); + assert_eq!(write_at(&path, "theme", "light").unwrap(), None); + // No temporary file is left behind. + let mut names: Vec = + std::fs::read_dir(&dir).unwrap().map(|e| e.unwrap().file_name().to_string_lossy().into_owned()).collect(); + names.sort(); + assert_eq!(names, ["prefs.json", "prefs.json.bad", "prefs.json.bad2"]); std::fs::write(&path, r#"["prefs-sentinel"]"#).unwrap(); assert!(!read_at(&path).unwrap_err().contains("prefs-sentinel")); assert!(read_at(&dir).is_err()); From 360a5a0a62203df377b1131cca6a02e7461590e4 Mon Sep 17 00:00:00 2001 From: mavrick Date: Mon, 9 Nov 2026 18:17:10 -0600 Subject: [PATCH 2/8] fix(settings): preserve recovery copies durably across retries Reuse identical recovery copies without spending another recovery slot. Preserve malformed non-UTF-8 settings byte for byte. On Unix, sync recovery names before replacing the original and sync the replaced preference name. Make the existing popup decoder test respect optional BMP/TIFF features, matching the clipboard dependencies enabled on Windows and macOS. --- crates/engine/src/prefs.rs | 116 ++++++++++++++++++++++++++----- crates/ui-egui/src/popupmedia.rs | 16 +++-- 2 files changed, 107 insertions(+), 25 deletions(-) diff --git a/crates/engine/src/prefs.rs b/crates/engine/src/prefs.rs index 10dd00fd41..9323df7e5f 100644 --- a/crates/engine/src/prefs.rs +++ b/crates/engine/src/prefs.rs @@ -4,6 +4,7 @@ use rustgis_data::secrets::{Reporting, SecretStore}; use serde_json::{Map, Value}; +use std::io::{Read, Seek}; use std::path::{Path, PathBuf}; use std::sync::{Arc, Mutex}; @@ -21,12 +22,12 @@ pub fn read() -> Result, String> { } fn read_at(path: &Path) -> Result, String> { - let text = match std::fs::read_to_string(path) { - Ok(text) => text, + let bytes = match std::fs::read(path) { + Ok(bytes) => bytes, Err(error) if error.kind() == std::io::ErrorKind::NotFound => return Ok(Map::new()), Err(error) => return Err(format!("could not read preferences: {error}")), }; - let value: Value = serde_json::from_str(&text) + let value: Value = serde_json::from_slice(&bytes) .map_err(|error| format!("invalid preferences JSON at line {}, column {}", error.line(), error.column()))?; match value { Value::Object(map) => Ok(map), @@ -49,31 +50,40 @@ pub fn write(key: &str, value: &str) -> Result, String> { fn write_at(path: &Path, key: &str, value: &str) -> Result, String> { let (mut map, set_aside) = match read_at(path) { Ok(map) => (map, None), - Err(_) if path.is_file() && std::fs::read_to_string(path).is_ok() => (Map::new(), Some(set_aside(path)?)), + Err(_) if path.is_file() && std::fs::read(path).is_ok() => (Map::new(), Some(set_aside(path)?)), Err(error) => return Err(error), }; map.insert(key.to_string(), Value::String(value.to_string())); let text = serde_json::to_vec(&map).map_err(|_| "could not serialize preferences")?; let dir = path.parent().ok_or("the settings folder isn't available")?; - std::fs::create_dir_all(dir).and_then(|_| rustgis_data::io::replace_atomic(path, &text)).map_err(|error| match &set_aside { - Some(backup) => format!("could not save preferences: {error}; malformed preferences were kept at {}", backup.display()), - None => format!("could not save preferences: {error}"), - })?; + std::fs::create_dir_all(dir).and_then(|_| rustgis_data::io::replace_atomic(path, &text)).and_then(|_| sync_directory(path)).map_err( + |error| match &set_aside { + Some(backup) => format!("could not save preferences: {error}; malformed preferences were kept at {}", backup.display()), + None => format!("could not save preferences: {error}"), + }, + )?; Ok(set_aside) } /// How many malformed preference files are kept: `prefs.json.bad`, then `prefs.json.bad2` to -/// `prefs.json.bad9`. Once all are taken, saving refuses to replace any recovery copy or the -/// malformed current file; the user can repair or move a copy to make room. +/// `prefs.json.bad9`. An identical existing copy is reused; when all contain different bytes, +/// saving refuses to replace any recovery copy or the malformed current file. const KEPT_BAD: u32 = 9; /// Copies malformed preferences to an exclusively created `.bad` file, synced before saving /// replaces the original. Concurrent callers cannot overwrite each other's recovery files. /// A failed copy is removed; a failed later save leaves both original and recovery intact. +/// Retries reuse an identical regular recovery file, without spending another recovery slot. fn set_aside(path: &Path) -> Result { let name = |n: u32| path.with_extension(if n == 1 { "json.bad".to_string() } else { format!("json.bad{n}") }); let mut source = std::fs::File::open(path).map_err(|error| format!("could not preserve malformed preferences: {error}"))?; - for target in (1..=KEPT_BAD).map(name) { + let targets: Vec = (1..=KEPT_BAD).map(name).collect(); + for target in &targets { + if same_contents(&mut source, target).unwrap_or(false) { + return Ok(target.clone()); + } + } + for target in targets { let mut options = std::fs::OpenOptions::new(); options.write(true).create_new(true); #[cfg(unix)] @@ -83,10 +93,21 @@ fn set_aside(path: &Path) -> Result { } let mut backup = match options.open(&target) { Ok(file) => file, - Err(error) if error.kind() == std::io::ErrorKind::AlreadyExists => continue, + Err(error) if error.kind() == std::io::ErrorKind::AlreadyExists => { + // Another process may have completed this copy after the first scan. + if same_contents(&mut source, &target).unwrap_or(false) { + return Ok(target); + } + continue; + } Err(error) => return Err(format!("could not preserve malformed preferences: {error}")), }; - if let Err(error) = std::io::copy(&mut source, &mut backup).and_then(|_| backup.sync_all()) { + if let Err(error) = source + .rewind() + .and_then(|_| std::io::copy(&mut source, &mut backup)) + .and_then(|_| backup.sync_all()) + .and_then(|_| sync_directory(&target)) + { drop(backup); let _ = std::fs::remove_file(&target); return Err(format!("could not preserve malformed preferences: {error}")); @@ -99,6 +120,49 @@ fn set_aside(path: &Path) -> Result { )) } +/// Makes a created recovery name or replaced preference name durable on Unix before success. +/// Other platforms keep the synced-file guarantee; opening directories is not portable there. +fn sync_directory(path: &Path) -> std::io::Result<()> { + #[cfg(unix)] + { + let dir = path.parent().filter(|p| !p.as_os_str().is_empty()).unwrap_or(Path::new(".")); + std::fs::File::open(dir)?.sync_all()?; + } + #[cfg(not(unix))] + let _ = path; + Ok(()) +} + +/// Compares regular recovery files in bounded chunks, syncing an identical copy before reuse. +/// Symlink paths and read or sync errors do not establish a reusable recovery copy. +fn same_contents(source: &mut std::fs::File, target: &Path) -> std::io::Result { + if !std::fs::symlink_metadata(target)?.file_type().is_file() { + return Ok(false); + } + let mut copy = std::fs::OpenOptions::new().read(true).write(true).open(target)?; + if source.metadata()?.len() != copy.metadata()?.len() { + return Ok(false); + } + source.rewind()?; + let mut left = [0u8; 8192]; + let mut right = [0u8; 8192]; + loop { + let n = source.read(&mut left)?; + if n == 0 { + if copy.read(&mut right[..1])? != 0 { + return Ok(false); + } + copy.sync_all()?; + sync_directory(target)?; + return Ok(true); + } + copy.read_exact(&mut right[..n])?; + if left[..n] != right[..n] { + return Ok(false); + } + } +} + /// Opts a native caller into the desktop's saved connections and lazy credential store, which /// also keeps the Earthdata Login token remembered on this computer. /// Constructing the store and loading connection definitions never reads a credential. @@ -147,13 +211,16 @@ mod tests { let path = dir.join(FILE); std::fs::write(&path, "broken preferences").unwrap(); let mut kept = Vec::new(); - for _ in 0..KEPT_BAD { - kept.push(set_aside(&path).unwrap()); + for n in 0..KEPT_BAD { + let text = format!("broken preferences {n}"); + std::fs::write(&path, &text).unwrap(); + kept.push((set_aside(&path).unwrap(), text)); } + std::fs::write(&path, "broken preferences").unwrap(); assert!(write_at(&path, "theme", "dark").unwrap_err().contains("left unchanged")); assert_eq!(std::fs::read_to_string(&path).unwrap(), "broken preferences"); - for backup in kept { - assert_eq!(std::fs::read_to_string(backup).unwrap(), "broken preferences"); + for (backup, text) in kept { + assert_eq!(std::fs::read_to_string(backup).unwrap(), text); } std::fs::remove_dir_all(dir).unwrap(); } @@ -181,11 +248,24 @@ mod tests { assert_eq!(write_at(&path, "theme", "dark").unwrap(), Some(dir.join("prefs.json.bad2"))); assert_eq!(std::fs::read_to_string(dir.join("prefs.json.bad")).unwrap(), broken); assert_eq!(write_at(&path, "theme", "light").unwrap(), None); + // Failed replacements leave the malformed original: repeated preservation reuses its copy. + std::fs::write(&path, broken).unwrap(); + for _ in 0..=KEPT_BAD { + assert_eq!(set_aside(&path).unwrap(), dir.join("prefs.json.bad")); + } + assert_eq!(std::fs::read_to_string(&path).unwrap(), broken); + assert_eq!(write_at(&path, "theme", "light").unwrap(), Some(dir.join("prefs.json.bad"))); + // Invalid UTF-8 is malformed JSON too; recovery preserves the exact original bytes. + let invalid = [0xff, 0xfe]; + std::fs::write(&path, invalid).unwrap(); + assert!(read_at(&path).is_err()); + assert_eq!(write_at(&path, "theme", "light").unwrap(), Some(dir.join("prefs.json.bad3"))); + assert_eq!(std::fs::read(dir.join("prefs.json.bad3")).unwrap(), invalid); // No temporary file is left behind. let mut names: Vec = std::fs::read_dir(&dir).unwrap().map(|e| e.unwrap().file_name().to_string_lossy().into_owned()).collect(); names.sort(); - assert_eq!(names, ["prefs.json", "prefs.json.bad", "prefs.json.bad2"]); + assert_eq!(names, ["prefs.json", "prefs.json.bad", "prefs.json.bad2", "prefs.json.bad3"]); std::fs::write(&path, r#"["prefs-sentinel"]"#).unwrap(); assert!(!read_at(&path).unwrap_err().contains("prefs-sentinel")); assert!(read_at(&dir).is_err()); diff --git a/crates/ui-egui/src/popupmedia.rs b/crates/ui-egui/src/popupmedia.rs index 8257621c82..b2bebf3e6e 100644 --- a/crates/ui-egui/src/popupmedia.rs +++ b/crates/ui-egui/src/popupmedia.rs @@ -548,13 +548,15 @@ mod tests { assert_eq!(Failure::TooLarge(4000, 3000).line(), "Picture unavailable: 4000 × 3000 pixels is larger than 2048 × 2048"); assert_eq!(decode(b"Not found"), Err(Failure::NotPicture)); assert_eq!(decode(&[]), Err(Failure::NotPicture)); - // A picture whose format isn't read here says so, not that it isn't a picture. - let mut bmp = b"BM".to_vec(); - bmp.resize(64, 0); - assert_eq!(decode(&bmp), Err(Failure::Unsupported("BMP".into()))); - let mut tiff = b"II*\0".to_vec(); - tiff.resize(64, 0); - assert_eq!(decode(&tiff), Err(Failure::Unsupported("TIFF".into()))); + // Platform clipboard dependencies can enable BMP or TIFF through feature unification. + // These invalid headers are unsupported without a decoder, and malformed with one. + for (format, signature) in [(image::ImageFormat::Bmp, b"BM".as_slice()), (image::ImageFormat::Tiff, b"II*\0".as_slice())] { + let mut bytes = signature.to_vec(); + bytes.resize(64, 0); + let error = + if format.reading_enabled() { Failure::NotPicture } else { Failure::Unsupported(format!("{format:?}").to_uppercase()) }; + assert_eq!(decode(&bytes), Err(error), "{format:?}"); + } assert_eq!(Failure::Unsupported("BMP".into()).line(), "Picture unavailable: BMP pictures aren't supported"); // A picture cut short doesn't decode. let whole = png(64, 64); From f58cfdb6b50e814032e7d231e884ca72eeb4508a Mon Sep 17 00:00:00 2001 From: mavrick Date: Tue, 10 Nov 2026 01:44:16 -0600 Subject: [PATCH 3/8] fix(settings): distinguish replaced preferences from unconfirmed durability Keep the final directory-sync error visible without claiming a completed replacement lasts only until the app closes. Report the completed replacement and unconfirmed durability separately, with accurate translated notice text. Exercise a real replacement followed by an injected final flush failure. --- apps/rustgis/src/main.rs | 10 +++++++--- crates/engine/src/prefs.rs | 35 +++++++++++++++++++++++++++++------ 2 files changed, 36 insertions(+), 9 deletions(-) diff --git a/apps/rustgis/src/main.rs b/apps/rustgis/src/main.rs index f6c8ac7a0e..7c1192681d 100644 --- a/apps/rustgis/src/main.rs +++ b/apps/rustgis/src/main.rs @@ -119,9 +119,7 @@ mod prefs { "Your settings couldn't be read, so RustGIS started new ones. The old settings file was kept as {path}.", &[("path", &path.display().to_string())], ), - Notice::NotSaved(error) => { - fmt("Settings can't be saved ({error}): changes last only until RustGIS closes.", &[("error", error)]) - } + Notice::NotSaved(error) => fmt("Settings could not be saved reliably ({error}).", &[("error", error)]), } } @@ -805,6 +803,12 @@ mod tests { assert_eq!(prefs::take_notices(), [Notice::SetAside(bad.clone())]); let text = prefs::text(&Notice::SetAside(bad)); assert!(text.contains("prefs.json.bad"), "{text}"); + // Both a failed replacement and an unconfirmed final flush give an accurate notice. + for error in ["could not save preferences: read-only", "preferences were replaced, but their durability could not be confirmed"] { + let text = prefs::text(&Notice::NotSaved(error.into())); + assert!(text.contains("Settings could not be saved reliably") && text.contains(error), "{text}"); + assert!(!text.contains("changes last only until"), "{text}"); + } } #[test] diff --git a/crates/engine/src/prefs.rs b/crates/engine/src/prefs.rs index 82ddc0c9ed..c31115d80b 100644 --- a/crates/engine/src/prefs.rs +++ b/crates/engine/src/prefs.rs @@ -48,6 +48,17 @@ pub fn write(key: &str, value: &str) -> Result, String> { /// [`write`] to the file at `path` (a test folder in tests): returns where a malformed file went. fn write_at(path: &Path, key: &str, value: &str) -> Result, String> { + write_at_with_sync(path, key, value, sync_directory) +} + +/// Separates replacement from its final directory flush; an unconfirmed flush must not claim +/// that the already-visible file was left unchanged. The callback permits a post-rename fault test. +fn write_at_with_sync( + path: &Path, + key: &str, + value: &str, + sync: impl FnOnce(&Path) -> std::io::Result<()>, +) -> Result, String> { let (mut map, set_aside) = match read_at(path) { Ok(map) => (map, None), Err(_) if path.is_file() && std::fs::read(path).is_ok() => (Map::new(), Some(set_aside(path)?)), @@ -56,12 +67,17 @@ fn write_at(path: &Path, key: &str, value: &str) -> Result, Stri map.insert(key.to_string(), Value::String(value.to_string())); let text = serde_json::to_vec(&map).map_err(|_| "could not serialize preferences")?; let dir = path.parent().ok_or("the settings folder isn't available")?; - std::fs::create_dir_all(dir).and_then(|_| rustgis_data::io::replace_atomic(path, &text)).and_then(|_| sync_directory(path)).map_err( - |error| match &set_aside { - Some(backup) => format!("could not save preferences: {error}; malformed preferences were kept at {}", backup.display()), - None => format!("could not save preferences: {error}"), - }, - )?; + std::fs::create_dir_all(dir).and_then(|_| rustgis_data::io::replace_atomic(path, &text)).map_err(|error| match &set_aside { + Some(backup) => format!("could not save preferences: {error}; malformed preferences were kept at {}", backup.display()), + None => format!("could not save preferences: {error}"), + })?; + sync(path).map_err(|error| match &set_aside { + Some(backup) => format!( + "preferences were replaced, but their durability could not be confirmed: {error}; malformed preferences were kept at {}", + backup.display() + ), + None => format!("preferences were replaced, but their durability could not be confirmed: {error}"), + })?; Ok(set_aside) } @@ -241,6 +257,13 @@ mod tests { let map = read_at(&path).unwrap(); assert_eq!(map.get("connections"), Some(&Value::String("saved definitions".into()))); assert_eq!(map.get("theme"), Some(&Value::String("dark".into()))); + // A failed final flush leaves the replacement visible, with an honest durability error. + let error = write_at_with_sync(&path, "theme", "light", |_| { + Err(std::io::Error::new(std::io::ErrorKind::Unsupported, "directory sync unavailable")) + }) + .unwrap_err(); + assert!(error.contains("preferences were replaced") && error.contains("durability could not be confirmed"), "{error}"); + assert_eq!(read_at(&path).unwrap().get("theme"), Some(&Value::String("light".into()))); let broken = r#"{"sensitive-pref":"prefs-sentinel", broken}"#; std::fs::write(&path, broken).unwrap(); let error = read_at(&path).unwrap_err(); From c3e217b4f1d7db03828a0c9199cdba563105923e Mon Sep 17 00:00:00 2001 From: mavrick Date: Tue, 10 Nov 2026 06:23:09 -0600 Subject: [PATCH 4/8] fix(settings): request a frame for pending preference notices Saves during logic or UI must deliver their notice without another input event. --- apps/rustgis/src/main.rs | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) diff --git a/apps/rustgis/src/main.rs b/apps/rustgis/src/main.rs index 7c1192681d..8cd720be17 100644 --- a/apps/rustgis/src/main.rs +++ b/apps/rustgis/src/main.rs @@ -108,6 +108,14 @@ mod prefs { std::mem::take(&mut *notices()) } + /// A save during logic or UI may queue a notice after this frame's drain. Ensure another + /// frame delivers it even when the user stops interacting with the window. + pub fn repaint_if_pending(ctx: &egui::Context) { + if !notices().is_empty() { + ctx.request_repaint(); + } + } + /// A notice's text in the current language. pub fn text(notice: &Notice) -> String { use rustgis_ui_egui::i18n::fmt; @@ -353,9 +361,11 @@ impl eframe::App for App { if self.0.quit_requested { ctx.send_viewport_cmd(egui::ViewportCommand::Close); } + prefs::repaint_if_pending(ctx); } fn ui(&mut self, ui: &mut egui::Ui, _frame: &mut eframe::Frame) { self.0.ui(ui); + prefs::repaint_if_pending(ui.ctx()); } #[cfg(not(target_os = "macos"))] fn raw_input_hook(&mut self, _ctx: &egui::Context, raw_input: &mut egui::RawInput) { @@ -788,10 +798,26 @@ mod tests { // Clean saves say nothing. prefs::saved(Ok(None)); assert_eq!(prefs::take_notices(), []); + // An idle window must get another frame when a save queues a notice late in a frame. + let ctx = egui::Context::default(); + let frame = || { + let mut output = ctx.run_ui(egui::RawInput::default(), |ui| prefs::repaint_if_pending(ui.ctx())); + output.textures_delta.clear(); + }; + for _ in 0..4 { + frame(); + } + assert!(!ctx.has_requested_repaint(), "clean preferences keep the window idle"); // A full disk: said once, not for every setting that follows. prefs::saved(Err("could not save preferences: no space left".into())); prefs::saved(Err("could not save preferences: no space left".into())); + frame(); + assert!(ctx.has_requested_repaint(), "the pending failure requests its delivery frame"); assert_eq!(prefs::take_notices(), [Notice::NotSaved("could not save preferences: no space left".into())]); + for _ in 0..4 { + frame(); + } + assert!(!ctx.has_requested_repaint(), "draining the notice lets the window settle"); // Once a save works, the next failure is news again. prefs::saved(Ok(None)); prefs::saved(Err("could not save preferences: read-only".into())); From 7aa554eea27e36064c580caae85f28e6daf02e65 Mon Sep 17 00:00:00 2001 From: mavrick Date: Tue, 10 Nov 2026 08:39:58 -0600 Subject: [PATCH 5/8] fix(settings): retain reused recovery copies on sync failure Never delete a recovery name another writer may have verified and reused. Report changed recovery outcomes while suppressing equivalent failures. --- apps/rustgis/src/main.rs | 19 +++++++++++----- crates/engine/src/prefs.rs | 44 +++++++++++++++++++++++++++++--------- 2 files changed, 48 insertions(+), 15 deletions(-) diff --git a/apps/rustgis/src/main.rs b/apps/rustgis/src/main.rs index 8cd720be17..b1c30d9b80 100644 --- a/apps/rustgis/src/main.rs +++ b/apps/rustgis/src/main.rs @@ -61,8 +61,8 @@ mod prefs { } static NOTICES: Mutex> = Mutex::new(Vec::new()); - /// The last save failed and said so: later failures stay quiet until a save works again. - static FAILING: AtomicBool = AtomicBool::new(false); + /// Repeated equivalent failures stay quiet; changed recovery details must reach the window. + static LAST_FAILURE: Mutex> = Mutex::new(None); /// The waiting notices; a panic while one was added doesn't lose the others. fn notices() -> std::sync::MutexGuard<'static, Vec> { @@ -82,11 +82,11 @@ mod prefs { } /// Keeps what a save came to for the window: a malformed file set aside (which also explains - /// an earlier read failure still waiting), or the first of a run of failed saves. + /// an earlier read failure still waiting), or a failure whose outcome changed since last time. pub fn saved(result: Result, String>) { match result { Ok(set_aside) => { - FAILING.store(false, Ordering::Relaxed); + *LAST_FAILURE.lock().unwrap_or_else(|poisoned| poisoned.into_inner()) = None; if let Some(path) = set_aside { log::warn!("unreadable preferences set aside as {}", path.display()); let mut notices = notices(); @@ -96,7 +96,9 @@ mod prefs { } Err(error) => { log::warn!("{error}"); - if !FAILING.swap(true, Ordering::Relaxed) { + let mut last = LAST_FAILURE.lock().unwrap_or_else(|poisoned| poisoned.into_inner()); + if last.as_deref() != Some(error.as_str()) { + *last = Some(error.clone()); notices().push(Notice::NotSaved(error)); } } @@ -818,6 +820,13 @@ mod tests { frame(); } assert!(!ctx.has_requested_repaint(), "draining the notice lets the window settle"); + // Recovery can advance during a failure episode: do not retain obsolete instructions. + let changed = + "preferences were replaced, but their durability could not be confirmed; malformed preferences were kept at prefs.json.bad"; + prefs::saved(Err(changed.into())); + assert_eq!(prefs::take_notices(), [Notice::NotSaved(changed.into())]); + prefs::saved(Err(changed.into())); + assert!(prefs::take_notices().is_empty(), "equivalent recovery outcomes stay quiet"); // Once a save works, the next failure is news again. prefs::saved(Ok(None)); prefs::saved(Err("could not save preferences: read-only".into())); diff --git a/crates/engine/src/prefs.rs b/crates/engine/src/prefs.rs index c31115d80b..e7bda2478f 100644 --- a/crates/engine/src/prefs.rs +++ b/crates/engine/src/prefs.rs @@ -88,9 +88,16 @@ const KEPT_BAD: u32 = 9; /// Copies malformed preferences to an exclusively created `.bad` file, synced before saving /// replaces the original. Concurrent callers cannot overwrite each other's recovery files. -/// A failed copy is removed; a failed later save leaves both original and recovery intact. +/// Failed copies remain reserved: another process may already have verified and reused one. +/// A copy or sync failure stops replacement and reports the possibly incomplete recovery path. /// Retries reuse an identical regular recovery file, without spending another recovery slot. fn set_aside(path: &Path) -> Result { + set_aside_with_sync(path, |backup, target| backup.sync_all().and_then(|_| sync_directory(target))) +} + +/// Copies into an exclusive recovery name and confirms it before returning. A failing creator +/// never deletes the name, because another writer can independently sync and reuse its bytes. +fn set_aside_with_sync(path: &Path, sync: impl FnOnce(&std::fs::File, &Path) -> std::io::Result<()>) -> Result { let name = |n: u32| path.with_extension(if n == 1 { "json.bad".to_string() } else { format!("json.bad{n}") }); let mut source = std::fs::File::open(path).map_err(|error| format!("could not preserve malformed preferences: {error}"))?; let targets: Vec = (1..=KEPT_BAD).map(name).collect(); @@ -118,15 +125,11 @@ fn set_aside(path: &Path) -> Result { } Err(error) => return Err(format!("could not preserve malformed preferences: {error}")), }; - if let Err(error) = source - .rewind() - .and_then(|_| std::io::copy(&mut source, &mut backup)) - .and_then(|_| backup.sync_all()) - .and_then(|_| sync_directory(&target)) - { - drop(backup); - let _ = std::fs::remove_file(&target); - return Err(format!("could not preserve malformed preferences: {error}")); + if let Err(error) = source.rewind().and_then(|_| std::io::copy(&mut source, &mut backup)).and_then(|_| sync(&backup, &target)) { + return Err(format!( + "could not confirm recovery of malformed preferences: {error}; recovery copy retained at {} and may be incomplete", + target.display() + )); } return Ok(target); } @@ -225,6 +228,27 @@ pub fn use_saved_secrets(s: &mut crate::Session, prefs: &Map, sto mod tests { use super::*; + /// A second writer can finish a save while the recovery creator is still confirming it. + /// A later creator error must not remove the recovery copy that the successful writer used. + #[test] + fn a_creator_sync_error_keeps_a_recovery_copy_another_writer_reused() { + let dir = std::env::temp_dir().join(format!("rustgis-preferences-reused-{}", std::process::id())); + let _ = std::fs::remove_dir_all(&dir); + std::fs::create_dir_all(&dir).unwrap(); + let path = dir.join(FILE); + std::fs::write(&path, "malformed original").unwrap(); + let error = set_aside_with_sync(&path, |_, target| { + let reused = write_at(&path, "theme", "dark").map_err(std::io::Error::other)?; + assert_eq!(reused.as_deref(), Some(target)); + Err(std::io::Error::other("creator sync failed")) + }) + .unwrap_err(); + assert!(error.contains("recovery copy retained") && error.contains("creator sync failed"), "{error}"); + assert_eq!(read_at(&path).unwrap().get("theme").and_then(Value::as_str), Some("dark")); + assert_eq!(std::fs::read_to_string(path.with_extension("json.bad")).unwrap(), "malformed original"); + std::fs::remove_dir_all(dir).unwrap(); + } + #[test] fn full_recovery_slots_leave_the_malformed_file_and_every_backup_unchanged() { let dir = std::env::temp_dir().join(format!("rustgis-preferences-full-{}", std::process::id())); From ef73e6bacc8a7de77f5b4ef5e992078c9c2e5dce Mon Sep 17 00:00:00 2001 From: mavrick Date: Tue, 10 Nov 2026 09:26:37 -0600 Subject: [PATCH 6/8] fix(settings): stop recovery retries on verified copy sync errors --- crates/engine/src/prefs.rs | 64 +++++++++++++++++++++++++------------- 1 file changed, 43 insertions(+), 21 deletions(-) diff --git a/crates/engine/src/prefs.rs b/crates/engine/src/prefs.rs index e7bda2478f..7d702c074b 100644 --- a/crates/engine/src/prefs.rs +++ b/crates/engine/src/prefs.rs @@ -97,13 +97,21 @@ fn set_aside(path: &Path) -> Result { /// Copies into an exclusive recovery name and confirms it before returning. A failing creator /// never deletes the name, because another writer can independently sync and reuse its bytes. -fn set_aside_with_sync(path: &Path, sync: impl FnOnce(&std::fs::File, &Path) -> std::io::Result<()>) -> Result { +fn set_aside_with_sync(path: &Path, sync: impl Fn(&std::fs::File, &Path) -> std::io::Result<()>) -> Result { let name = |n: u32| path.with_extension(if n == 1 { "json.bad".to_string() } else { format!("json.bad{n}") }); + let retained = |error: std::io::Error, target: &Path| { + format!( + "could not confirm recovery of malformed preferences: {error}; recovery copy retained at {} and may be incomplete", + target.display() + ) + }; + let confirm = + |copy: &std::fs::File, target: &Path| sync(copy, target).map(|()| target.to_path_buf()).map_err(|error| retained(error, target)); let mut source = std::fs::File::open(path).map_err(|error| format!("could not preserve malformed preferences: {error}"))?; let targets: Vec = (1..=KEPT_BAD).map(name).collect(); for target in &targets { - if same_contents(&mut source, target).unwrap_or(false) { - return Ok(target.clone()); + if let Ok(Some(copy)) = same_contents(&mut source, target) { + return confirm(©, target); } } for target in targets { @@ -118,20 +126,17 @@ fn set_aside_with_sync(path: &Path, sync: impl FnOnce(&std::fs::File, &Path) -> Ok(file) => file, Err(error) if error.kind() == std::io::ErrorKind::AlreadyExists => { // Another process may have completed this copy after the first scan. - if same_contents(&mut source, &target).unwrap_or(false) { - return Ok(target); + if let Ok(Some(copy)) = same_contents(&mut source, &target) { + return confirm(©, &target); } continue; } Err(error) => return Err(format!("could not preserve malformed preferences: {error}")), }; - if let Err(error) = source.rewind().and_then(|_| std::io::copy(&mut source, &mut backup)).and_then(|_| sync(&backup, &target)) { - return Err(format!( - "could not confirm recovery of malformed preferences: {error}; recovery copy retained at {} and may be incomplete", - target.display() - )); + if let Err(error) = source.rewind().and_then(|_| std::io::copy(&mut source, &mut backup)) { + return Err(retained(error, &target)); } - return Ok(target); + return confirm(&backup, &target); } Err(format!( "all {KEPT_BAD} preference recovery files are occupied; repair or move one before saving settings; the malformed preferences were left unchanged at {}", @@ -152,11 +157,11 @@ fn sync_directory(path: &Path) -> std::io::Result<()> { Ok(()) } -/// Compares regular recovery files in bounded chunks, syncing an identical copy before reuse. -/// Symlink paths and read or sync errors do not establish a reusable recovery copy. -fn same_contents(source: &mut std::fs::File, target: &Path) -> std::io::Result { +/// Returns the open copy when its bytes match, so the caller can confirm its durability. +/// Symlink paths and read errors do not establish a match; sync failures remain distinct. +fn same_contents(source: &mut std::fs::File, target: &Path) -> std::io::Result> { if !std::fs::symlink_metadata(target)?.file_type().is_file() { - return Ok(false); + return Ok(None); } let mut options = std::fs::OpenOptions::new(); options.read(true); @@ -165,7 +170,7 @@ fn same_contents(source: &mut std::fs::File, target: &Path) -> std::io::Result std::io::Result, sto mod tests { use super::*; + /// A verified copy whose durability cannot be confirmed must not spend another recovery slot. + #[test] + fn repeated_recovery_sync_failures_keep_one_slot() { + let dir = std::env::temp_dir().join(format!("rustgis-preferences-sync-retry-{}", std::process::id())); + let _ = std::fs::remove_dir_all(&dir); + std::fs::create_dir_all(&dir).unwrap(); + let path = dir.join(FILE); + std::fs::write(&path, "malformed original").unwrap(); + for _ in 0..2 { + let error = set_aside_with_sync(&path, |_, _| Err(std::io::Error::other("directory sync failed"))).unwrap_err(); + assert!(error.contains("directory sync failed") && error.contains("recovery copy retained"), "{error}"); + assert!(!path.with_extension("json.bad2").exists()); + assert_eq!(std::fs::read_to_string(&path).unwrap(), "malformed original"); + } + assert_eq!(set_aside(&path).unwrap(), path.with_extension("json.bad")); + assert!(!path.with_extension("json.bad2").exists()); + std::fs::remove_dir_all(dir).unwrap(); + } + /// A second writer can finish a save while the recovery creator is still confirming it. /// A later creator error must not remove the recovery copy that the successful writer used. #[test] From 8362ce7350e6c5f894f401cc81c0e3efab906194 Mon Sep 17 00:00:00 2001 From: mavrick Date: Tue, 10 Nov 2026 10:05:43 -0600 Subject: [PATCH 7/8] fix(settings): preserve valid preferences after read failures --- crates/engine/src/prefs.rs | 58 ++++++++++++++++++++++++++++++++++---- 1 file changed, 53 insertions(+), 5 deletions(-) diff --git a/crates/engine/src/prefs.rs b/crates/engine/src/prefs.rs index 7d702c074b..cb7c758676 100644 --- a/crates/engine/src/prefs.rs +++ b/crates/engine/src/prefs.rs @@ -27,7 +27,12 @@ fn read_at(path: &Path) -> Result, String> { Err(error) if error.kind() == std::io::ErrorKind::NotFound => return Ok(Map::new()), Err(error) => return Err(format!("could not read preferences: {error}")), }; - let value: Value = serde_json::from_slice(&bytes) + parse_preferences(&bytes) +} + +/// Parses only bytes successfully read, keeping malformed content distinct from I/O failures. +fn parse_preferences(bytes: &[u8]) -> Result, String> { + let value: Value = serde_json::from_slice(bytes) .map_err(|error| format!("invalid preferences JSON at line {}, column {}", error.line(), error.column()))?; match value { Value::Object(map) => Ok(map), @@ -59,10 +64,25 @@ fn write_at_with_sync( value: &str, sync: impl FnOnce(&Path) -> std::io::Result<()>, ) -> Result, String> { - let (mut map, set_aside) = match read_at(path) { - Ok(map) => (map, None), - Err(_) if path.is_file() && std::fs::read(path).is_ok() => (Map::new(), Some(set_aside(path)?)), - Err(error) => return Err(error), + write_at_with_bytes(path, key, value, std::fs::read(path), sync) +} + +/// Uses one read result for a save. Only successfully read, malformed bytes permit recovery; +/// a transient read failure cannot replace a valid file with a new one containing a single key. +fn write_at_with_bytes( + path: &Path, + key: &str, + value: &str, + bytes: std::io::Result>, + sync: impl FnOnce(&Path) -> std::io::Result<()>, +) -> Result, String> { + let (mut map, set_aside) = match bytes { + Ok(bytes) => match parse_preferences(&bytes) { + Ok(map) => (map, None), + Err(_) => (Map::new(), Some(set_aside(path)?)), + }, + Err(error) if error.kind() == std::io::ErrorKind::NotFound => (Map::new(), None), + Err(error) => return Err(format!("could not read preferences: {error}")), }; map.insert(key.to_string(), Value::String(value.to_string())); let text = serde_json::to_vec(&map).map_err(|_| "could not serialize preferences")?; @@ -231,6 +251,34 @@ pub fn use_saved_secrets(s: &mut crate::Session, prefs: &Map, sto mod tests { use super::*; + /// A failed read must not be reclassified as malformed when the file is readable again. + #[test] + fn transient_read_failure_preserves_valid_preferences() { + let dir = std::env::temp_dir().join(format!("rustgis-preferences-read-retry-{}", std::process::id())); + let _ = std::fs::remove_dir_all(&dir); + std::fs::create_dir_all(&dir).unwrap(); + let path = dir.join(FILE); + let original = br#"{"theme":"dark","other":"kept"}"#; + std::fs::write(&path, original).unwrap(); + let error = write_at_with_bytes( + &path, + "theme", + "light", + Err(std::io::Error::new(std::io::ErrorKind::PermissionDenied, "transient read failure")), + sync_directory, + ) + .unwrap_err(); + assert!(error.contains("could not read preferences") && error.contains("transient read failure"), "{error}"); + assert_eq!(std::fs::read(&path).unwrap(), original); + assert!(!path.with_extension("json.bad").exists()); + assert_eq!(write_at(&path, "theme", "light").unwrap(), None); + let saved = read_at(&path).unwrap(); + assert_eq!(saved.get("theme").and_then(Value::as_str), Some("light")); + assert_eq!(saved.get("other").and_then(Value::as_str), Some("kept")); + assert!(!path.with_extension("json.bad").exists()); + std::fs::remove_dir_all(dir).unwrap(); + } + /// A verified copy whose durability cannot be confirmed must not spend another recovery slot. #[test] fn repeated_recovery_sync_failures_keep_one_slot() { From e5a25f93003f69fd889b48db5a5d8bdae34ca4f4 Mon Sep 17 00:00:00 2001 From: mavrick Date: Tue, 10 Nov 2026 14:05:20 -0600 Subject: [PATCH 8/8] fix(settings): recover the exact malformed preference snapshot --- crates/engine/src/prefs.rs | 72 +++++++++++++++++++++++--------------- 1 file changed, 43 insertions(+), 29 deletions(-) diff --git a/crates/engine/src/prefs.rs b/crates/engine/src/prefs.rs index cb7c758676..f19e6d8ce3 100644 --- a/crates/engine/src/prefs.rs +++ b/crates/engine/src/prefs.rs @@ -4,7 +4,7 @@ use rustgis_data::secrets::{Reporting, SecretStore}; use serde_json::{Map, Value}; -use std::io::{Read, Seek}; +use std::io::{Read, Write}; use std::path::{Path, PathBuf}; use std::sync::{Arc, Mutex}; @@ -79,7 +79,7 @@ fn write_at_with_bytes( let (mut map, set_aside) = match bytes { Ok(bytes) => match parse_preferences(&bytes) { Ok(map) => (map, None), - Err(_) => (Map::new(), Some(set_aside(path)?)), + Err(_) => (Map::new(), Some(set_aside(path, &bytes)?)), }, Err(error) if error.kind() == std::io::ErrorKind::NotFound => (Map::new(), None), Err(error) => return Err(format!("could not read preferences: {error}")), @@ -106,18 +106,18 @@ fn write_at_with_bytes( /// saving refuses to replace any recovery copy or the malformed current file. const KEPT_BAD: u32 = 9; -/// Copies malformed preferences to an exclusively created `.bad` file, synced before saving +/// Copies the exact bytes that failed parsing to an exclusively created `.bad` file, synced before saving /// replaces the original. Concurrent callers cannot overwrite each other's recovery files. /// Failed copies remain reserved: another process may already have verified and reused one. /// A copy or sync failure stops replacement and reports the possibly incomplete recovery path. /// Retries reuse an identical regular recovery file, without spending another recovery slot. -fn set_aside(path: &Path) -> Result { - set_aside_with_sync(path, |backup, target| backup.sync_all().and_then(|_| sync_directory(target))) +fn set_aside(path: &Path, bytes: &[u8]) -> Result { + set_aside_with_sync(path, bytes, |backup, target| backup.sync_all().and_then(|_| sync_directory(target))) } /// Copies into an exclusive recovery name and confirms it before returning. A failing creator /// never deletes the name, because another writer can independently sync and reuse its bytes. -fn set_aside_with_sync(path: &Path, sync: impl Fn(&std::fs::File, &Path) -> std::io::Result<()>) -> Result { +fn set_aside_with_sync(path: &Path, bytes: &[u8], sync: impl Fn(&std::fs::File, &Path) -> std::io::Result<()>) -> Result { let name = |n: u32| path.with_extension(if n == 1 { "json.bad".to_string() } else { format!("json.bad{n}") }); let retained = |error: std::io::Error, target: &Path| { format!( @@ -127,10 +127,9 @@ fn set_aside_with_sync(path: &Path, sync: impl Fn(&std::fs::File, &Path) -> std: }; let confirm = |copy: &std::fs::File, target: &Path| sync(copy, target).map(|()| target.to_path_buf()).map_err(|error| retained(error, target)); - let mut source = std::fs::File::open(path).map_err(|error| format!("could not preserve malformed preferences: {error}"))?; let targets: Vec = (1..=KEPT_BAD).map(name).collect(); for target in &targets { - if let Ok(Some(copy)) = same_contents(&mut source, target) { + if let Ok(Some(copy)) = same_contents(bytes, target) { return confirm(©, target); } } @@ -146,14 +145,14 @@ fn set_aside_with_sync(path: &Path, sync: impl Fn(&std::fs::File, &Path) -> std: Ok(file) => file, Err(error) if error.kind() == std::io::ErrorKind::AlreadyExists => { // Another process may have completed this copy after the first scan. - if let Ok(Some(copy)) = same_contents(&mut source, &target) { + if let Ok(Some(copy)) = same_contents(bytes, &target) { return confirm(©, &target); } continue; } Err(error) => return Err(format!("could not preserve malformed preferences: {error}")), }; - if let Err(error) = source.rewind().and_then(|_| std::io::copy(&mut source, &mut backup)) { + if let Err(error) = backup.write_all(bytes) { return Err(retained(error, &target)); } return confirm(&backup, &target); @@ -179,7 +178,7 @@ fn sync_directory(path: &Path) -> std::io::Result<()> { /// Returns the open copy when its bytes match, so the caller can confirm its durability. /// Symlink paths and read errors do not establish a match; sync failures remain distinct. -fn same_contents(source: &mut std::fs::File, target: &Path) -> std::io::Result> { +fn same_contents(source: &[u8], target: &Path) -> std::io::Result> { if !std::fs::symlink_metadata(target)?.file_type().is_file() { return Ok(None); } @@ -189,25 +188,20 @@ fn same_contents(source: &mut std::fs::File, target: &Path) -> std::io::Result, sto mod tests { use super::*; + /// Recovery preserves the malformed snapshot, even if the path now contains another save. + #[test] + fn recovery_keeps_the_bytes_that_failed_parsing() { + let dir = std::env::temp_dir().join(format!("rustgis-preferences-snapshot-{}", std::process::id())); + let _ = std::fs::remove_dir_all(&dir); + std::fs::create_dir_all(&dir).unwrap(); + let path = dir.join(FILE); + let malformed = b"malformed original"; + assert!(parse_preferences(malformed).is_err()); + let current = br#"{"theme":"dark","other":"kept"}"#; + std::fs::write(&path, current).unwrap(); + let backup = set_aside(&path, malformed).unwrap(); + assert_eq!(std::fs::read(&backup).unwrap(), malformed); + assert_eq!(std::fs::read(&path).unwrap(), current); + assert_eq!(set_aside(&path, malformed).unwrap(), backup); + assert!(!path.with_extension("json.bad2").exists()); + std::fs::remove_dir_all(dir).unwrap(); + } + /// A failed read must not be reclassified as malformed when the file is readable again. #[test] fn transient_read_failure_preserves_valid_preferences() { @@ -288,12 +301,13 @@ mod tests { let path = dir.join(FILE); std::fs::write(&path, "malformed original").unwrap(); for _ in 0..2 { - let error = set_aside_with_sync(&path, |_, _| Err(std::io::Error::other("directory sync failed"))).unwrap_err(); + let error = + set_aside_with_sync(&path, b"malformed original", |_, _| Err(std::io::Error::other("directory sync failed"))).unwrap_err(); assert!(error.contains("directory sync failed") && error.contains("recovery copy retained"), "{error}"); assert!(!path.with_extension("json.bad2").exists()); assert_eq!(std::fs::read_to_string(&path).unwrap(), "malformed original"); } - assert_eq!(set_aside(&path).unwrap(), path.with_extension("json.bad")); + assert_eq!(set_aside(&path, &std::fs::read(&path).unwrap()).unwrap(), path.with_extension("json.bad")); assert!(!path.with_extension("json.bad2").exists()); std::fs::remove_dir_all(dir).unwrap(); } @@ -307,7 +321,7 @@ mod tests { std::fs::create_dir_all(&dir).unwrap(); let path = dir.join(FILE); std::fs::write(&path, "malformed original").unwrap(); - let error = set_aside_with_sync(&path, |_, target| { + let error = set_aside_with_sync(&path, b"malformed original", |_, target| { let reused = write_at(&path, "theme", "dark").map_err(std::io::Error::other)?; assert_eq!(reused.as_deref(), Some(target)); Err(std::io::Error::other("creator sync failed")) @@ -329,7 +343,7 @@ mod tests { for n in 0..KEPT_BAD { let text = format!("broken preferences {n}"); std::fs::write(&path, &text).unwrap(); - kept.push((set_aside(&path).unwrap(), text)); + kept.push((set_aside(&path, &std::fs::read(&path).unwrap()).unwrap(), text)); } std::fs::write(&path, "broken preferences").unwrap(); assert!(write_at(&path, "theme", "dark").unwrap_err().contains("left unchanged")); @@ -379,7 +393,7 @@ mod tests { } std::fs::write(&path, broken).unwrap(); for _ in 0..=KEPT_BAD { - assert_eq!(set_aside(&path).unwrap(), dir.join("prefs.json.bad")); + assert_eq!(set_aside(&path, &std::fs::read(&path).unwrap()).unwrap(), dir.join("prefs.json.bad")); } assert_eq!(std::fs::read_to_string(&path).unwrap(), broken); assert_eq!(write_at(&path, "theme", "light").unwrap(), Some(dir.join("prefs.json.bad")));