PhotoCraft: An open-source, photo editor written by scratch in pure Rust
Save Actions ATN files atomically so a failed write keeps the old file (#7403)
NetterSilver713dbbed2f77
diff --git a/crates/engine/src/atn_cmds.rs b/crates/engine/src/atn_cmds.rsindex d027dd15c..e0767330c 100644--- a/crates/engine/src/atn_cmds.rs+++ b/crates/engine/src/atn_cmds.rs@@ -3264,7 +3264,8 @@ fn save(s: &mut Session, p: &Value) -> Result<Value> {#[cfg(not(target_arch = "wasm32"))]fn write_file(path: &str, bytes: &[u8]) -> std::io::Result<()> {- std::fs::write(path, bytes)+ // Atomic replacement (temporary file + rename): a failed write keeps the previous ATN (#7290).+ photocraft_format::atomic_write(std::path::Path::new(path), bytes)}#[cfg(target_arch = "wasm32")]diff --git a/crates/engine/src/atn_cmds/tests.rs b/crates/engine/src/atn_cmds/tests.rsindex fd8038775..b280a6d67 100644--- a/crates/engine/src/atn_cmds/tests.rs+++ b/crates/engine/src/atn_cmds/tests.rs@@ -585,6 +585,33 @@ fn save_writes_the_file_and_the_set_of_the_selected_action() {std::fs::remove_dir_all(&dir).ok();}+/// #7290: saving over an existing ATN must replace it atomically (a temporary file renamed over+/// it), never truncate and rewrite it in place, so a write that fails partway (a full disk)+/// leaves the previously valid file readable. A handle opened on the old file still sees the old+/// bytes after an atomic replacement; an in-place rewrite changes them under it.+#[cfg(unix)]+#[test]+fn save_over_an_existing_file_never_rewrites_it_in_place() {+ use std::io::Read;+ let mut s = Session::new();+ s.actions.list.push(Action::new("Owned", vec![("select.all".into(), json!({}))]));+ let dir = tempfile::tempdir().unwrap();+ let path = dir.path().join("Owned.atn");+ let p = path.to_string_lossy().to_string();+ s.execute("actions.save", json!({"path": p})).unwrap();+ let before = std::fs::read(&path).unwrap();+ let mut old = std::fs::File::open(&path).unwrap();+ s.actions.list.push(Action::new("Second", vec![("select.all".into(), json!({}))]));+ s.execute("actions.save", json!({"path": p})).unwrap();+ let mut seen = Vec::new();+ old.read_to_end(&mut seen).unwrap();+ assert_eq!(seen, before, "the existing ATN was rewritten in place");+ let after = atn::parse(&std::fs::read(&path).unwrap()).unwrap();+ assert_eq!(after.actions.len(), 2);+ let names: Vec<_> = std::fs::read_dir(dir.path()).unwrap().map(|e| e.unwrap().file_name()).collect();+ assert_eq!(names, vec![std::ffi::OsString::from("Owned.atn")], "no temporary file is left behind");+}+#[test]fn layer_param_accepts_a_name() {let mut s = Session::new();