diff --git a/crates/engine/src/preset_store.rs b/crates/engine/src/preset_store.rs index c665fdae81e..946a298a1e2 100644 --- a/crates/engine/src/preset_store.rs +++ b/crates/engine/src/preset_store.rs @@ -921,8 +921,17 @@ impl PresetStore { None => order.push((p.group.clone(), vec![i])), } } - // Names of the presets in groups that could not be written: the old files that hold them stay. - let mut unsaved: HashSet = HashSet::new(); + // Groups that take presets they don't hold yet are written first, so a preset moved out of + // one group is never dropped from its old file before its new group is safely written. + order.sort_by_key(|(group, idx)| { + let held = self.groups.get(&group_file_name(group)).map(|s| &s.presets); + let gains = idx.iter().filter_map(|&i| presets.get(i)).any(|p| !held.is_some_and(|h| h.iter().any(|q| q.name == p.name))); + !gains + }); + // After a write that fails (a name the file system refuses, a full disk) nothing more is + // changed on disk: the old files may be the only copy of the presets that failed, and the + // next sync retries the rest. + let mut failed = false; let mut files = Vec::with_capacity(order.len()); for (group, idx) in &order { let file = group_file_name(group); @@ -931,10 +940,11 @@ impl PresetStore { .groups .get(&file) .is_some_and(|s| !s.stale && s.presets.len() == idx.len() && s.presets.iter().zip(idx).all(|(a, &i)| presets.get(i) == Some(a))); - if unchanged { + if unchanged || failed { continue; } if self.skipped.contains(&file) { + failed = true; self.warnings.push(format!( "Couldn't save brush presets `{}`: {file} could not be read, so it was left unchanged", if group.is_empty() { "(ungrouped)" } else { group } @@ -942,17 +952,13 @@ impl PresetStore { continue; } let mut items: Vec<&mut BrushPreset> = presets.iter_mut().filter(|p| !p.builtin && p.group == *group).collect(); - let names: Vec = items.iter().map(|p| p.name.clone()).collect(); if let Err(e) = self.write_group(&file, group, &mut items) { - unsaved.extend(names); + failed = true; self.warnings.push(format!("Couldn't save brush presets `{}`: {e}", if group.is_empty() { "(ungrouped)" } else { group })); } } - // Groups that are gone. When a group could not be written (a rename to a name that failed, - // say), the old files holding its presets stay: they may be the only copy of those presets and - // their tips. Other removed groups are still deleted. - let gone: Vec = - self.groups.iter().filter(|(f, g)| !files.contains(f) && !g.presets.iter().any(|p| unsaved.contains(&p.name))).map(|(f, _)| f.clone()).collect(); + // Groups that are gone, unless a write failed above. + let gone: Vec = self.groups.keys().filter(|f| !failed && !files.contains(f)).cloned().collect(); for f in gone { match self.backend.remove(&f) { Ok(()) => drop(self.groups.remove(&f)), diff --git a/crates/engine/src/preset_store/tests.rs b/crates/engine/src/preset_store/tests.rs index 316ccc5432a..a3b761c14db 100644 --- a/crates/engine/src/preset_store/tests.rs +++ b/crates/engine/src/preset_store/tests.rs @@ -1191,16 +1191,16 @@ fn a_group_that_cannot_be_saved_keeps_the_old_group_file_and_tips() { } #[test] -fn a_failed_group_write_does_not_keep_unrelated_deleted_groups() { - let dir = TempDir::new("keep-unrelated"); +fn a_failed_group_write_changes_nothing_else_until_the_next_sync() { + let dir = TempDir::new("fail-fast"); let (mut s, _) = session(&dir); s.tools.presets.push(sampled("Keep", "Old", tip8(8, 8, 1))); + s.tools.presets.push(sampled("Stay", "Old", tip8(8, 8, 3))); s.tools.presets.push(sampled("Drop", "Gone", tip8(8, 8, 2))); s.brush_presets_changed(); s.sync_preset_store(); - let before = dir.files(); - assert_eq!(before.iter().filter(|f| f.ends_with(".pcbrushes")).count(), 2, "{before:?}"); - // "Keep" moves to a group name that cannot be written; "Drop" is deleted. + // "Keep" moves to a group that cannot be written, "Drop" is deleted. The group that gains + // "Keep" fails first; the source group is not rewritten without it and the other deletion waits. s.tools.presets.retain(|p| p.name != "Drop"); for p in s.tools.presets.iter_mut().filter(|p| p.name == "Keep") { p.group = "g".repeat(MAX_GROUP_NAME_BYTES + 1); @@ -1208,6 +1208,18 @@ fn a_failed_group_write_does_not_keep_unrelated_deleted_groups() { s.brush_presets_changed(); s.sync_preset_store(); let (t, _) = session(&dir); - assert!(photocraft_paint::presets::find(&t.tools.presets, "Keep").is_some(), "the old copy of the unsaved preset stays"); - assert!(photocraft_paint::presets::find(&t.tools.presets, "Drop").is_none(), "the unrelated deleted group is gone after a restart"); + for name in ["Keep", "Stay", "Drop"] { + assert!(photocraft_paint::presets::find(&t.tools.presets, name).is_some(), "{name}: the old files are all still there"); + } + // Once the move can be written, the next sync finishes everything. + for p in s.tools.presets.iter_mut().filter(|p| p.name == "Keep") { + p.group = "New".into(); + } + s.brush_presets_changed(); + s.sync_preset_store(); + let (t, w) = session(&dir); + assert!(w.is_empty(), "{w:?}"); + assert_eq!(photocraft_paint::presets::find(&t.tools.presets, "Keep").map(|p| p.group.clone()), Some("New".into())); + assert!(photocraft_paint::presets::find(&t.tools.presets, "Stay").is_some()); + assert!(photocraft_paint::presets::find(&t.tools.presets, "Drop").is_none(), "the deferred deletion happened"); }