Craft Anything: understand how any software works, and craft your own.
Rust
Test both endings of a refused move, and say the lock reason once (#1057)
kuseidosdbd5d0d963fd
diff --git a/crates/engines/src/ghidra/mod.rs b/crates/engines/src/ghidra/mod.rsindex 7fc40dfd..52d26636 100644--- a/crates/engines/src/ghidra/mod.rs+++ b/crates/engines/src/ghidra/mod.rs@@ -536,6 +536,19 @@ fn move_may_pass(e: &std::io::Error) -> bool {matches!(e.kind(), std::io::ErrorKind::PermissionDenied | std::io::ErrorKind::WouldBlock)}+/// The error for a move that cannot pass: the lock goes back first, because left behind it reads+/// to every other process as an analysis in progress. What the person hears is the move, and a+/// lock that also cannot be released is named beside it rather than hiding it.+fn refuse_move(lock: &Path, project_dir: &Path, refusal: &std::io::Error) -> GhidraError {+ let moving = format!("moving {} out of the way: {refusal}", project_dir.display());+ match release(lock) {+ Ok(()) => GhidraError::Launch(moving),+ // The second reason without the outer "could not start Ghidra", which would read twice.+ Err(GhidraError::Launch(also)) => GhidraError::Launch(format!("{moving}; and {also}")),+ Err(other) => GhidraError::Launch(format!("{moving}; and {other}")),+ }+}+/// Give the analysis lock back. A lock this process cannot release would read to everyone else as/// an analysis in progress, so it ends the attempt with the reason instead of waiting it out.fn release(lock: &Path) -> Result<(), GhidraError> {@@ -653,15 +666,7 @@ pub fn analyse(// its own folder. A refusal to move it means that Ghidra is still working: wait for it.match set_aside(&project_dir) {Ok(aside) => break aside,- Err(e) if !move_may_pass(&e) => {- // Ending here, so the lock goes back first: left behind it reads to everyone else as- // an analysis in progress. The move is still what the person needs to hear about.- let why = match release(&lock) {- Ok(()) => format!("moving {} out of the way: {e}", project_dir.display()),- Err(also) => format!("moving {} out of the way: {e}; and {also}", project_dir.display()),- };- return Err(GhidraError::Launch(why));- }+ Err(e) if !move_may_pass(&e) => return Err(refuse_move(&lock, &project_dir, &e)),Err(e) => {release(&lock)?;held_by = Some(e.to_string());diff --git a/crates/engines/src/ghidra/tests.rs b/crates/engines/src/ghidra/tests.rsindex fcca448b..55494240 100644--- a/crates/engines/src/ghidra/tests.rs+++ b/crates/engines/src/ghidra/tests.rs@@ -490,6 +490,31 @@ fn giving_back_a_lock_that_is_already_gone_is_not_a_failure() {assert!(!lock.exists(), "the lock is gone once it is given back");}+/// A move that cannot pass ends the attempt, so the lock goes back with it. Both outcomes are+/// checked here by making the release itself fail, which is a folder named as the lock file:+/// `remove_file` refuses that on every system, and no rename has to fail for the test to run.+#[test]+fn a_move_that_cannot_pass_gives_the_lock_back_and_says_why() {+ let d = tempfile::tempdir().unwrap();+ let project = d.path().join("target-abcd");+ let lock = d.path().join("held.analysing");+ std::fs::write(&lock, "").unwrap();++ let refusal = std::io::Error::from(std::io::ErrorKind::InvalidInput);+ let err = refuse_move(&lock, &project, &refusal);+ assert!(err.to_string().contains("out of the way"), "{err}");+ assert!(!err.to_string().contains("and releasing"), "a clean release is not worth mentioning: {err}");+ assert!(!lock.exists(), "the lock is gone");++ // Now the lock cannot be released: a folder where the lock file should be.+ let stuck = d.path().join("stuck.analysing");+ std::fs::create_dir(&stuck).unwrap();+ let err = refuse_move(&stuck, &project, &refusal);+ let text = err.to_string();+ assert!(text.contains("out of the way"), "the move is still what is reported: {text}");+ assert!(text.contains("and releasing the analysis lock"), "and the lock that could not be freed: {text}");+}+/// #951: the folder a stale lock was guarding is moved, not removed, so a Ghidra that outlived the/// app that started it keeps writing into its own folder instead of into this analysis's.#[test]