mirror of
https://github.com/storytold/cadcraft.git
synced 2026-12-02 02:38:30 -06:00
* Drawing locks and WHOHAS: say when a drawing is open elsewhere (part of #540) While a drawing opened from a DWG/DXF/DWT file is open, the app keeps `.<file name>.cadcraft-lock` beside it (user, computer, app instance, when opened), touched every minute; a lock not touched for five minutes is stale. Opening a drawing someone else has open says who and since when, on the command line and in the status bar, and opens it anyway: the lock only informs. A lock is released when its drawing closes or is saved under another name and when the app exits, only ever by the instance that holds it; a stale one is taken over, and an app that finds its lock taken over notes it instead of touching it. WHOHAS reports the holder of the current drawing or of a file. The locks run with the autosave tick; the format is CADCraft's own, not AutoCAD's .dwl. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> * Drawing lock: claim a free lock atomically Two apps opening a drawing with no lock at the same moment could both write it and both think they held it, so neither told its user until a later heartbeat. A missing lock is now created atomically (a hard link of the staged file, or create_new without hard links), and a stale one is replaced only if no other app has taken it meanwhile, then read back. The app that loses is told at once who has the drawing. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> * WHOHAS: a `path` that isn't text is an error A `path` of another type (`{"path": 7}`) was taken as left out, so WHOHAS answered for the current drawing. Left out or null still means the current drawing; anything but a string is now refused. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> * Drawing locks: name the imports of the native module The workspace now denies clippy's wildcard_imports, which `use super::*` in drawlock's native module trips after the merge of main. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> * Autosave: wake up for the drawing-lock heartbeat, not only for autosaves Autosaver::tick kept the drawing locks but returned only the next autosave, None with every drawing saved or SAVETIME 0, and the UI only schedules a repaint on Some. An idle app then never touched its locks: after STALE_SECS another app took the drawing over without a word. Locks::next_beat gives the shortest wait until a held lock (or someone else's) is due again, and tick returns the sooner of that and the next autosave. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> * WHOHAS: an error on the web, not "nobody has it open" The web has no lock files, so the lookup isn't done there; reporting no holder presented that as a negative result. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> * Fix the wasm build after the merge The merge dropped the native-only cfg from main's autosave lock field, and two drawlock items used only by native code warned on wasm. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> --------- Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
108 lines
4.9 KiB
Rust
108 lines
4.9 KiB
Rust
//! Drawing locks (#540): an app with a drawing open keeps a lock beside it; another app opening
|
|
//! the same drawing is told who has it, without taking the lock; the lock goes when the drawing
|
|
//! closes, and a stale one is taken over.
|
|
|
|
use std::time::{Duration, SystemTime};
|
|
|
|
use cadcraft_engine::Session;
|
|
use cadcraft_engine::autosave::{self, Autosaver};
|
|
use cadcraft_engine::cmd::file::{IoHooks, set_io};
|
|
use cadcraft_engine::drawlock::{Locks, holder, lock_path};
|
|
use serde_json::json;
|
|
|
|
fn session_with(path: &str) -> Session {
|
|
set_io(IoHooks {
|
|
read: |b, n| cadcraft_io::read(b, n).map_err(|e| e.to_string()),
|
|
write: |d, n| cadcraft_io::write(d, n).map_err(|e| e.to_string()),
|
|
plot: None,
|
|
});
|
|
let mut s = Session::new();
|
|
s.execute("open", &json!({ "path": path })).unwrap();
|
|
s
|
|
}
|
|
|
|
#[test]
|
|
fn a_second_app_is_told_who_has_the_drawing_and_the_lock_follows_the_first() {
|
|
let dir = std::env::temp_dir().join(format!("cadcraft-lock-{}", std::process::id()));
|
|
let _ = std::fs::remove_dir_all(&dir);
|
|
std::fs::create_dir_all(&dir).unwrap();
|
|
let file = dir.join("Shared Plan.dxf");
|
|
std::fs::write(&file, cadcraft_io::write(&cadcraft_engine::doc::Drawing::new_metric(), "x.dxf").unwrap()).unwrap();
|
|
let path = file.to_string_lossy().to_string();
|
|
let now = Duration::ZERO;
|
|
|
|
let mut a = session_with(&path);
|
|
let mut la = Locks::with_heartbeat(now);
|
|
la.sync(&a, "1-a");
|
|
assert!(lock_path(&file).file_name().unwrap().to_string_lossy().starts_with(".Shared Plan.dxf"));
|
|
assert_eq!(holder(&file).map(|h| h.instance), Some("1-a".to_string()));
|
|
assert!(la.take_notes().is_empty());
|
|
|
|
// A second app opening it is told, and the lock stays the first app's.
|
|
let b = session_with(&path);
|
|
let mut lb = Locks::with_heartbeat(now);
|
|
lb.sync(&b, "2-b");
|
|
let notes = lb.take_notes();
|
|
assert_eq!(notes.len(), 1, "{notes:?}");
|
|
assert!(notes[0].starts_with("Shared Plan.dxf is also open by ") && notes[0].contains("UTC"), "{}", notes[0]);
|
|
lb.sync(&b, "2-b");
|
|
assert!(lb.take_notes().is_empty(), "said once");
|
|
assert_eq!(holder(&file).map(|h| h.instance), Some("1-a".to_string()));
|
|
// WHOHAS names the holder.
|
|
let mut b = b;
|
|
// A `path` that isn't a file path is refused, not taken as the current drawing.
|
|
assert!(b.execute("whohas", &json!({ "path": 7 })).is_err());
|
|
let r = b.execute("whohas", &json!({})).unwrap();
|
|
assert_eq!(r["holder"]["instance"], "1-a");
|
|
|
|
// Releasing what isn't ours leaves it alone.
|
|
lb.release_all("2-b");
|
|
assert_eq!(holder(&file).map(|h| h.instance), Some("1-a".to_string()));
|
|
|
|
// The first app closes the drawing: its lock goes, and the second takes it.
|
|
a.execute("close", &json!({})).unwrap();
|
|
la.sync(&a, "1-a");
|
|
assert!(holder(&file).is_none());
|
|
lb.sync(&b, "2-b");
|
|
assert_eq!(holder(&file).map(|h| h.instance), Some("2-b".to_string()));
|
|
|
|
// A lock left by a crashed app (not touched for a while) is taken over without a note.
|
|
let stale = SystemTime::now() - Duration::from_secs(cadcraft_engine::drawlock::STALE_SECS + 60);
|
|
std::fs::OpenOptions::new().write(true).open(lock_path(&file)).unwrap().set_modified(stale).unwrap();
|
|
assert!(holder(&file).is_none(), "stale");
|
|
let c = session_with(&path);
|
|
let mut lc = Locks::with_heartbeat(now);
|
|
lc.sync(&c, "3-c");
|
|
assert!(lc.take_notes().is_empty());
|
|
assert_eq!(holder(&file).map(|h| h.instance), Some("3-c".to_string()));
|
|
// The app whose lock was taken over notices on its next heartbeat and doesn't touch it.
|
|
lb.sync(&b, "2-b");
|
|
assert_eq!(lb.take_notes().len(), 1, "told who has it now");
|
|
assert_eq!(holder(&file).map(|h| h.instance), Some("3-c".to_string()));
|
|
lc.release_all("3-c");
|
|
assert!(!lock_path(&file).exists());
|
|
let _ = std::fs::remove_dir_all(&dir);
|
|
}
|
|
|
|
/// An idle app with every drawing saved (and SAVETIME 0) still wakes up for the lock heartbeat:
|
|
/// otherwise its lock goes stale and another app takes the drawing over without a word.
|
|
#[test]
|
|
fn an_idle_app_wakes_up_for_the_lock_heartbeat() {
|
|
let dir = std::env::temp_dir().join(format!("cadcraft-lock-idle-{}", std::process::id()));
|
|
let _ = std::fs::remove_dir_all(&dir);
|
|
std::fs::create_dir_all(&dir).unwrap();
|
|
let file = dir.join("Saved Plan.dxf");
|
|
std::fs::write(&file, cadcraft_io::write(&cadcraft_engine::doc::Drawing::new_metric(), "x.dxf").unwrap()).unwrap();
|
|
let s = session_with(&file.to_string_lossy());
|
|
assert!(!s.docs.iter().any(|d| d.is_dirty()), "nothing to autosave");
|
|
autosave::set_savetime(0);
|
|
let beat = Duration::from_secs(2);
|
|
let mut a = Autosaver::new("1-idle").with_locks(Locks::with_heartbeat(beat));
|
|
let wait = a.tick(&s);
|
|
assert_eq!(holder(&file).map(|h| h.instance), Some("1-idle".to_string()));
|
|
assert!(wait.is_some_and(|d| d <= beat), "{wait:?}");
|
|
a.shutdown();
|
|
assert!(!lock_path(&file).exists());
|
|
let _ = std::fs::remove_dir_all(&dir);
|
|
}
|