diff --git a/apps/rustgis/src/main.rs b/apps/rustgis/src/main.rs index c42ef52ad6..b1c30d9b80 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,92 @@ 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()); + /// 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> { + 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 a failure whose outcome changed since last time. + pub fn saved(result: Result, String>) { + match result { + Ok(set_aside) => { + *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(); + notices.retain(|n| !matches!(n, Notice::Unreadable(_))); + notices.push(Notice::SetAside(path)); + } + } + Err(error) => { + log::warn!("{error}"); + 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)); + } + } + } + } + + /// The problems since the last call, oldest first. + pub fn take_notices() -> Vec { + 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; + 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 could not be saved reliably ({error}).", &[("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 +142,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,15 +352,22 @@ 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); 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) { @@ -620,7 +703,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 +794,58 @@ 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(), []); + // 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"); + // 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())); + 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}"); + // 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] 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..f19e6d8ce3 100644 --- a/crates/engine/src/prefs.rs +++ b/crates/engine/src/prefs.rs @@ -4,7 +4,8 @@ use rustgis_data::secrets::{Reporting, SecretStore}; use serde_json::{Map, Value}; -use std::path::Path; +use std::io::{Read, Write}; +use std::path::{Path, PathBuf}; use std::sync::{Arc, Mutex}; pub const FILE: &str = "prefs.json"; @@ -21,12 +22,17 @@ 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) + 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), @@ -34,33 +40,168 @@ 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() - } - Err(error) => return Err(error), +/// [`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> { + 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, &bytes)?)), + }, + 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")?; 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}"), + })?; + 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) +} + +/// How many malformed preference files are kept: `prefs.json.bad`, then `prefs.json.bad2` to +/// `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 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, 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, 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!( + "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 targets: Vec = (1..=KEPT_BAD).map(name).collect(); + for target in &targets { + if let Ok(Some(copy)) = same_contents(bytes, target) { + return confirm(©, target); + } + } + for target in targets { + 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 => { + // Another process may have completed this copy after the first scan. + 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) = backup.write_all(bytes) { + return Err(retained(error, &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 {}", + path.display() + )) +} + +/// 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(()) +} + +/// 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: &[u8], target: &Path) -> std::io::Result> { + if !std::fs::symlink_metadata(target)?.file_type().is_file() { + return Ok(None); + } + let mut options = std::fs::OpenOptions::new(); + options.read(true); + // Linux can sync a read-only descriptor; retain the other platforms' flush access. + #[cfg(not(target_os = "linux"))] + options.write(true); + let mut copy = options.open(target)?; + if source.len() as u64 != copy.metadata()?.len() { + return Ok(None); + } + let mut right = [0u8; 8192]; + for left in source.chunks(right.len()) { + copy.read_exact(&mut right[..left.len()])?; + if left != &right[..left.len()] { + return Ok(None); + } + } + if copy.read(&mut right[..1])? != 0 { + return Ok(None); + } + Ok(Some(copy)) } /// Opts a native caller into the desktop's saved connections and lazy credential store, which @@ -104,24 +245,169 @@ pub fn use_saved_secrets(s: &mut crate::Session, prefs: &Map, 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() { + 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() { + 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, 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, &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(); + } + + /// 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, 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")) + }) + .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())); + 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 n in 0..KEPT_BAD { + let text = format!("broken preferences {n}"); + std::fs::write(&path, &text).unwrap(); + 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")); + assert_eq!(std::fs::read_to_string(&path).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(); + } + #[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()))); + // 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(); 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); + // Failed replacements leave the malformed original: repeated preservation reuses its copy. + // Protecting the existing copy on Linux must not spend more recovery slots. + #[cfg(target_os = "linux")] + { + use std::os::unix::fs::PermissionsExt; + std::fs::set_permissions(dir.join("prefs.json.bad"), std::fs::Permissions::from_mode(0o444)).unwrap(); + } + std::fs::write(&path, broken).unwrap(); + for _ in 0..=KEPT_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"))); + // 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", "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());