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'/') {
program.to_os_string()
} else {
join(OsStr::new("."), program)
};
Ok(Anchored {
program: PathBuf::from(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 complete_posix(
program: &OsStr,
child_cwd: Option<&Path>,
process_cwd: impl FnOnce() -> std::io::Result<PathBuf>,
) -> Result<Completed, Error> {
refuse_unnameable(program)?;
let as_given = || child_cwd.map(Path::to_path_buf);
if is_absolute(program) {
return Ok(Completed {
program: PathBuf::from(program),
child_cwd: as_given(),
});
}
let base = match child_cwd {
Some(dir) if dir.as_os_str().is_empty() => {
return Err(Error::Io(std::io::Error::new(
std::io::ErrorKind::NotFound,
"current_dir(\"\") names no directory",
)))
}
Some(dir) if is_absolute(dir.as_os_str()) => {
return Ok(Completed {
program: PathBuf::from(join(dir.as_os_str(), program)),
child_cwd: as_given(),
})
}
Some(dir) => join(process_cwd()?.as_os_str(), dir.as_os_str()),
None => process_cwd()?.into_os_string(),
};
Ok(Completed {
program: PathBuf::from(join(&base, program)),
child_cwd: Some(PathBuf::from(base)),
})
}
pub(crate) fn enter_posix(
program: &OsStr,
child_cwd: Option<&Path>,
process_cwd: impl FnOnce() -> std::io::Result<PathBuf>,
) -> Result<Entered, Error> {
let completed = complete_posix(program, child_cwd, process_cwd)?;
if is_absolute(program) {
return Ok(Entered {
program: completed.program,
dir: completed.child_cwd,
});
}
let bytes = program.as_encoded_bytes();
let program = if bytes.starts_with(b"./") {
program.to_os_string()
} else {
join(OsStr::new("."), program)
};
Ok(Entered {
program: PathBuf::from(program),
dir: completed.child_cwd,
})
}
#[derive(Debug, PartialEq, Eq)]
pub(crate) struct Entered {
pub(crate) program: PathBuf,
pub(crate) dir: Option<PathBuf>,
}
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(())
}
#[derive(Debug, PartialEq, Eq)]
pub(crate) struct Completed {
pub(crate) program: PathBuf,
pub(crate) child_cwd: Option<PathBuf>,
}
fn is_absolute(p: &OsStr) -> bool {
p.as_encoded_bytes().first() == Some(&b'/')
}
fn join(base: &OsStr, rel: &OsStr) -> OsString {
let mut out = base.to_os_string();
if !base.as_encoded_bytes().ends_with(b"/") {
out.push("/");
}
out.push(rel);
out
}
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;