Scripts: a recovery copy keeps a script's change through a crash

A recovery copy's details now say that a script's command changed the
document before the new document entry is written, and a restore writes
a copy's details before its document entry in the new area. A crash
between the two writes no longer restores a script-changed document
without that record, so a later run cannot read the files its links name.

The docs say that a linked file in one of the script's own places inside
the settings folder is still read.
This commit is contained in:
dotCooCoo
2026-11-21 04:08:23 -08:00
parent a45e2dc710
commit 2fcee16756
4 changed files with 53 additions and 6 deletions
+9 -4
View File
@@ -1038,10 +1038,13 @@ impl RecoveryJob {
let meta_name = format!("{}{META_EXT}", self.file);
// The details name the images, so a crash between writing the document and its details
// would pair the new document with the old copy's images: when the new document has
// images the old details don't name, they name both until the new document is in.
let old = read_meta(store, &self.file).images;
if meta.images.iter().any(|(k, r)| old.get(k).is_none_or(|o| o.blob != r.blob)) {
// images the old details don't name, they name both until the new document is in. Likewise
// when a script's command changed the document since the old details: they say so first.
let old_meta = read_meta(store, &self.file);
let old = old_meta.images;
if (meta.by_script && !old_meta.by_script) || meta.images.iter().any(|(k, r)| old.get(k).is_none_or(|o| o.blob != r.blob)) {
let mut both = meta.clone();
both.by_script |= old_meta.by_script;
for (k, r) in old {
both.images.entry(k).or_insert(r);
}
@@ -1503,8 +1506,10 @@ fn restore_one(s: &mut Session, store: &Arc<dyn RecoveryStore>, file: &str) -> R
for (blob, data) in &blobs {
store.write(&format!("{area}/{blob}"), data).map_err(EngineError::Other)?;
}
store.write(&format!("{mine}{DOC_EXT}"), &bytes).map_err(EngineError::Other)?;
// The details go first: a crash in between leaves no copy without them (a copy is its
// document entry), and the copy in the old area stays whole.
store.write(&format!("{mine}{META_EXT}"), &meta_bytes).map_err(EngineError::Other)?;
store.write(&format!("{mine}{DOC_EXT}"), &bytes).map_err(EngineError::Other)?;
}
// The copy's images in its old area go with the area, once no copy is left there.
store.remove(&format!("{file}{DOC_EXT}")).map_err(EngineError::Other)?;
+42
View File
@@ -1714,3 +1714,45 @@ fn a_crash_between_a_copys_document_and_its_details_keeps_every_image() {
assert_eq!(images_of(&s), images, "first copy: {first}");
}
}
/// A crash between writing a copy's document and its details, after a script's command changed
/// the document since the last copy, restores the document as one a script opened.
#[test]
fn a_crash_between_a_copys_document_and_its_details_keeps_a_scripts_change() {
let store = Arc::new(MemoryStore::default());
let mut s = session_in(&store);
s.execute("file.recovery.save", &json!({})).unwrap();
let source = "vc.run('shape.ellipse', {x: 80, y: 20, width: 20, height: 20});";
s.execute("script.run", &json!({"source": source, "name": "t.js"})).unwrap();
assert!(s.doc().unwrap().by_script.is_some(), "the script's command changed the document");
let doc = vectorcraft_doc::Document::clone(&s.doc().unwrap().doc);
s.recovery.set_store(Arc::new(DiesAfterDocument { inner: store.clone(), dead: Default::default() }));
assert!(s.execute("file.recovery.save", &json!({})).is_err(), "the details weren't written");
drop(s);
let mut s = relaunch(&store);
assert_eq!(restored(s.execute("file.recovery.restore", &json!({})).unwrap()), 1);
let count = |d: &vectorcraft_doc::Document| d.layers.iter().map(|l| l.count()).sum::<usize>();
assert_eq!(count(&s.doc().unwrap().doc), count(&doc), "the new document is in");
assert!(s.doc().unwrap().by_script.is_some(), "restored as a document a script opened");
}
/// A crash while a restore moves a copy into the app's own area leaves no copy that a later
/// restore opens without its details: a script's change is kept.
#[test]
fn a_crash_while_a_copy_is_restored_keeps_a_scripts_change() {
let store = Arc::new(MemoryStore::default());
let mut s = session_in(&store);
let source = "vc.run('shape.ellipse', {x: 80, y: 20, width: 20, height: 20});";
s.execute("script.run", &json!({"source": source, "name": "t.js"})).unwrap();
s.execute("file.recovery.save", &json!({})).unwrap();
drop(s);
let mut s = Session::new();
s.recovery.set_store(Arc::new(DiesAfterDocument { inner: store.clone(), dead: Default::default() }));
let _ = s.execute("file.recovery.restore", &json!({}));
drop(s);
let mut s = relaunch(&store);
assert!(restored(s.execute("file.recovery.restore", &json!({})).unwrap()) >= 1);
for st in s.documents().iter().filter(|st| st.recovered) {
assert!(st.by_script.is_some(), "every restored copy keeps the script's change: {}", st.title());
}
}