Craft Anything: understand how any software works, and craft your own.
Rust
Say why a move was refused, and never leave the analysis lock behind silently (#1047)
kuseidos7b9c0407a169
diff --git a/crates/engines/src/ghidra/mod.rs b/crates/engines/src/ghidra/mod.rsindex 6dada785..7fc40dfd 100644--- a/crates/engines/src/ghidra/mod.rs+++ b/crates/engines/src/ghidra/mod.rs@@ -521,8 +521,29 @@ fn set_aside(project_dir: &Path) -> std::io::Result<Option<Aside>> {}let token = random_token();let aside = project_dir.with_extension(format!("orphaned-{}", token.get(..8).unwrap_or(&token)));- std::fs::rename(project_dir, &aside)?;- Ok(Some(Aside(aside)))+ match std::fs::rename(project_dir, &aside) {+ Ok(()) => Ok(Some(Aside(aside))),+ // Gone between the check and the move: nothing to move, not a reason to wait.+ Err(e) if e.kind() == std::io::ErrorKind::NotFound => Ok(None),+ Err(e) => Err(e),+ }+}++/// Whether a refused move may pass: Windows reports a folder it has open files in as a permission+/// and an antivirus holding a handle looks the same for a moment.+/// Anything else — a path we cannot even name — is the caller's to hear now, not after a timeout.+fn move_may_pass(e: &std::io::Error) -> bool {+ matches!(e.kind(), std::io::ErrorKind::PermissionDenied | std::io::ErrorKind::WouldBlock)+}++/// 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> {+ match std::fs::remove_file(lock) {+ Ok(()) => Ok(()),+ Err(e) if e.kind() == std::io::ErrorKind::NotFound => Ok(()),+ Err(e) => Err(GhidraError::Launch(format!("releasing the analysis lock {}: {e}", lock.display()))),+ }}/// The start of the folder names Ghidra gives this system's native programs (`mac_arm_64`, ...).@@ -596,6 +617,8 @@ pub fn analyse(let lock = lock_path(&project_dir);// The lock sits beside the project, in the cache folder a fresh store doesn't have yet.make_private_dir(lock.parent().unwrap_or(&root))?;+ // Why a move was refused last, so a timeout says what actually kept this waiting.+ let mut held_by: Option<String> = None;// The loop ends by handing over what it moved aside, if anything.let kept_aside = loop {if is_analysed(&project_dir) {@@ -605,7 +628,11 @@ pub fn analyse(return Err(GhidraError::Cancelled);}if Instant::now() >= deadline {- return Err(GhidraError::Timeout(format!("Ghidra's analysis didn't finish within {} s", cfg.startup_timeout.as_secs())));+ let why = match &held_by {+ Some(e) => format!(": the folder an earlier analysis left could not be moved out of the way ({e})"),+ None => String::new(),+ };+ return Err(GhidraError::Timeout(format!("Ghidra's analysis didn't finish within {} s{why}", cfg.startup_timeout.as_secs())));}if lock_is_fresh(&lock) {progress(0.05, &format!("waiting for Ghidra's analysis of this program, started earlier ({} min so far)", minutes()));@@ -613,7 +640,7 @@ pub fn analyse(continue;}// Stale or none: take it (create_new, so only one of two racing processes wins).- let _ = std::fs::remove_file(&lock);+ release(&lock)?;match std::fs::OpenOptions::new().write(true).create_new(true).open(&lock) {Ok(_) => {}Err(e) if e.kind() != std::io::ErrorKind::AlreadyExists => {@@ -626,8 +653,18 @@ 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) => {- let _ = std::fs::remove_file(&lock);+ release(&lock)?;+ held_by = Some(e.to_string());progress(0.05, &format!("waiting for the analysis of this program already under way ({e})"));std::thread::sleep(Duration::from_secs(2));}diff --git a/crates/engines/src/ghidra/tests.rs b/crates/engines/src/ghidra/tests.rsindex 2960083c..fcca448b 100644--- a/crates/engines/src/ghidra/tests.rs+++ b/crates/engines/src/ghidra/tests.rs@@ -468,6 +468,28 @@ fn a_second_analysis_waits_for_the_running_one_and_a_dead_ones_lock_is_taken_oveassert!(!project.join("half-written.gpr").exists(), "it wrote into the folder the other analysis was filling");}+/// Which refusals to move a folder are worth waiting on: Windows reports the live Ghidra this PR+/// is about as a permission problem, and everything else is the caller's to hear now.+#[test]+fn a_move_that_cannot_pass_is_told_apart_from_one_that_might() {+ use std::io::{Error, ErrorKind};+ assert!(move_may_pass(&Error::from(ErrorKind::PermissionDenied)), "a folder Windows has open files in reads as a permission problem");+ assert!(!move_may_pass(&Error::from(ErrorKind::InvalidInput)), "a path we cannot name will not fix itself");+ assert!(!move_may_pass(&Error::from(ErrorKind::NotFound)), "gone is not a reason to wait");+}++/// A lock that cannot be released reads to every other process as an analysis in progress, so+/// releasing it says why instead of failing silently.+#[test]+fn giving_back_a_lock_that_is_already_gone_is_not_a_failure() {+ let d = tempfile::tempdir().unwrap();+ assert!(release(&d.path().join("never-existed.analysing")).is_ok(), "nothing to release is not an error");+ let lock = d.path().join("held.analysing");+ std::fs::write(&lock, "").unwrap();+ assert!(release(&lock).is_ok());+ assert!(!lock.exists(), "the lock is gone once it is given back");+}+/// #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]