utopia-ia/elffuss-braid public
Changesets · Branches · Tags · Compare
| Changeset | 003ac7192c00fab5945b32cf311c3eea00d53caefdec5ec973a04a9177e566d2 |
|---|---|
| Author | KikoCis <KikoCis@users.noreply.github.com> · 2026-10-21 16:40 +02:00 |
| Committer | KikoCis <KikoCis@users.noreply.github.com> · 2026-10-21 16:40 +02:00 |
| Parents | b092a334ad0e |
1 file changed
modified crates/cli/src/verify.rs +55 −2
| @@ -683,7 +683,9 @@ | ||
| 683 | 683 | |
| 684 | 684 | /// The braid command to start the watchdog with: on Linux this very binary, |
| 685 | 685 | /// through `/proc/self/exe`, even if its file was replaced or removed since |
| 686 | -/// it started; elsewhere the file it was started from. | |
| 686 | +/// it started; elsewhere the file it was started from; and where neither | |
| 687 | +/// can be found, as on Linux without `/proc`, the name it was started by, | |
| 688 | +/// looked up in `PATH`. The handshake turns away any other program. | |
| 687 | 689 | #[cfg(unix)] |
| 688 | 690 | fn own_exe() -> Result<PathBuf> { |
| 689 | 691 | #[cfg(target_os = "linux")] |
| @@ -693,11 +695,36 @@ | ||
| 693 | 695 | return Ok(this); |
| 694 | 696 | } |
| 695 | 697 | } |
| 696 | - std::env::current_exe().context( | |
| 698 | + if let Ok(path) = std::env::current_exe() { | |
| 699 | + return Ok(path); | |
| 700 | + } | |
| 701 | + let name = std::env::args_os().next().unwrap_or_default(); | |
| 702 | + started_as(&name, std::env::var_os("PATH").as_deref()).context( | |
| 697 | 703 | "cannot find the braid command to start verify's watchdog, so the tests did not run", |
| 698 | 704 | ) |
| 699 | 705 | } |
| 700 | 706 | |
| 707 | +/// The file that a command started as `name` runs: `name` itself when it | |
| 708 | +/// holds a `/`, or else the first executable file of that name in the | |
| 709 | +/// directories of `path`, as a shell finds it. | |
| 710 | +#[cfg(unix)] | |
| 711 | +fn started_as(name: &std::ffi::OsStr, path: Option<&std::ffi::OsStr>) -> Option<PathBuf> { | |
| 712 | + use std::os::unix::ffi::OsStrExt; | |
| 713 | + use std::os::unix::fs::PermissionsExt; | |
| 714 | + if name.is_empty() { | |
| 715 | + return None; | |
| 716 | + } | |
| 717 | + if name.as_bytes().contains(&b'/') { | |
| 718 | + return Some(PathBuf::from(name)); | |
| 719 | + } | |
| 720 | + std::env::split_paths(path?) | |
| 721 | + .map(|dir| dir.join(name)) | |
| 722 | + .find(|candidate| { | |
| 723 | + fs::metadata(candidate) | |
| 724 | + .is_ok_and(|meta| meta.is_file() && meta.permissions().mode() & 0o111 != 0) | |
| 725 | + }) | |
| 726 | +} | |
| 727 | + | |
| 701 | 728 | /// The watchdog of one test run in `dir`, as [`Watchdog`] starts it. |
| 702 | 729 | pub(crate) fn watchdog(dir: &Path) -> Result<ExitCode> { |
| 703 | 730 | { |
| @@ -1411,6 +1438,32 @@ | ||
| 1411 | 1438 | assert_eq!(found, expected); |
| 1412 | 1439 | } |
| 1413 | 1440 | |
| 1441 | + #[cfg(unix)] | |
| 1442 | + #[test] | |
| 1443 | + fn without_its_own_file_braid_is_found_as_a_shell_finds_it() { | |
| 1444 | + use std::os::unix::fs::PermissionsExt; | |
| 1445 | + let temp = tempfile::tempdir().unwrap(); | |
| 1446 | + let bin = temp.path().join("bin"); | |
| 1447 | + fs::create_dir(&bin).unwrap(); | |
| 1448 | + let program = bin.join("braid-like"); | |
| 1449 | + fs::write(&program, "#!/bin/sh\n").unwrap(); | |
| 1450 | + fs::set_permissions(&program, fs::Permissions::from_mode(0o755)).unwrap(); | |
| 1451 | + let plain = bin.join("not-a-program"); | |
| 1452 | + fs::write(&plain, "").unwrap(); | |
| 1453 | + let path = std::env::join_paths([temp.path().join("missing"), bin.clone()]).unwrap(); | |
| 1454 | + let found = |name: &str| started_as(name.as_ref(), Some(path.as_os_str())); | |
| 1455 | + assert_eq!(found("braid-like"), Some(program.clone())); | |
| 1456 | + assert_eq!(found("not-a-program"), None); | |
| 1457 | + assert_eq!(found("absent"), None); | |
| 1458 | + assert_eq!(found(""), None); | |
| 1459 | + // A name with a slash is a path already. | |
| 1460 | + assert_eq!( | |
| 1461 | + found("./target/braid"), | |
| 1462 | + Some(PathBuf::from("./target/braid")) | |
| 1463 | + ); | |
| 1464 | + assert_eq!(started_as("braid-like".as_ref(), None), None); | |
| 1465 | + } | |
| 1466 | + | |
| 1414 | 1467 | /// A stand-in for the watchdog: `script`, run by `sh`. |
| 1415 | 1468 | #[cfg(unix)] |
| 1416 | 1469 | fn fake_watchdog(script: &str) -> Command { |