mirror of
https://github.com/storytold/photocraft.git
synced 2026-12-03 06:31:42 +01:00
address review: after a failed group write nothing else is rewritten or deleted until the next sync; groups that gain presets are written first
This commit is contained in:
1 parent
0502a47687
commit
e782f63888
2 files changed
+35
-17
No files matched your search
@@ -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<String> = 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<String> = 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<String> =
|
||||
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<String> = 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)),
|
||||
|
||||
@@ -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");
|
||||
}
|
||||
Reference in new issue
Block a user