use std::ffi::{OsStr, OsString};
use std::path::{Path, PathBuf};
use crate::error::Error;
#[cfg_attr(
not(unix),
allow(
dead_code,
reason = "off-unix the std spawn never has an Exact program - it routes to the raw backend"
)
)]
pub(crate) fn anchor_posix(program: &OsStr, child_cwd: Option<&Path>) -> Result<Anchored, Error> {
refuse_unnameable(program)?;
let cwd = child_cwd.map(Path::to_path_buf);
if is_absolute(program) {
return Ok(Anchored {
program: PathBuf::from(program),
cwd,
enter: false,
});
}
let program = if program.as_encoded_bytes().contains(&b'/') {
PathBuf::from(program)
} else {
let mut anchored = OsString::from("./");
anchored.push(program);
PathBuf::from(anchored)
};
Ok(Anchored {
program,
cwd,
enter: true,
})
}
#[derive(Debug, PartialEq, Eq)]
#[cfg_attr(
not(unix),
allow(
dead_code,
reason = "off-unix the std spawn never has an Exact program - it routes to the raw backend"
)
)]
pub(crate) struct Anchored {
pub(crate) program: PathBuf,
pub(crate) cwd: Option<PathBuf>,
pub(crate) enter: bool,
}
pub(crate) fn refuse_unnameable(program: &OsStr) -> Result<(), Error> {
if program.as_encoded_bytes().contains(&0) {
return Err(invalid_input(
"raw_executable() was given a path containing an embedded NUL, so it names no file".into(),
));
}
if super::names_no_file(program, false) {
return Err(invalid_input(format!(
"raw_executable() was given a path that names no file: {program:?}"
)));
}
Ok(())
}
fn is_absolute(p: &OsStr) -> bool {
p.as_encoded_bytes().first() == Some(&b'/')
}
fn invalid_input(msg: String) -> Error {
Error::Io(std::io::Error::new(std::io::ErrorKind::InvalidInput, msg))
}
#[cfg(test)]
#[path = "exact_tests.rs"]
mod exact_tests;