use crate::platform;
use std::{
env,
ffi::{OsStr, OsString},
io,
path::{Path, PathBuf},
};
const SEARCH_PATH: &str = "PATH";
const EXTENSIONS: &str = "PATHEXT";
pub fn resolve(program: &Path, cwd: &Path) -> io::Result<PathBuf> {
resolve_with(program, cwd, super::environment::var)
}
pub fn resolve_with(
program: &Path,
cwd: &Path,
variable: impl Fn(&str) -> Option<OsString>,
) -> io::Result<PathBuf> {
resolve_from(
program,
cwd,
variable(SEARCH_PATH).as_deref(),
variable(EXTENSIONS).as_deref(),
)
}
fn resolve_from(
program: &Path,
cwd: &Path,
path: Option<&OsStr>,
pathext: Option<&OsStr>,
) -> io::Result<PathBuf> {
lookup_paths(program, cwd, path, pathext)
.into_iter()
.find(|candidate| platform::is_executable(candidate))
.ok_or_else(|| {
io::Error::new(
io::ErrorKind::NotFound,
format!(
"Program is unavailable: {}",
super::files::path_text(program)
),
)
})
}
fn lookup_paths(
program: &Path,
cwd: &Path,
path: Option<&OsStr>,
pathext: Option<&OsStr>,
) -> Vec<PathBuf> {
let candidates = candidates_from(program, pathext);
if program.is_absolute() || program.components().count() > 1 {
return candidates
.into_iter()
.map(|candidate| cwd.join(candidate))
.collect();
}
env::split_paths(path.unwrap_or_default())
.flat_map(|directory| {
let directory = cwd.join(directory);
candidates
.iter()
.map(move |candidate| directory.join(candidate))
})
.collect()
}
pub fn candidates(program: &Path, variable: impl Fn(&str) -> Option<OsString>) -> Vec<PathBuf> {
candidates_from(program, variable(EXTENSIONS).as_deref())
}
fn candidates_from(program: &Path, pathext: Option<&OsStr>) -> Vec<PathBuf> {
if platform::USES_PATHEXT && program.extension().is_none() {
pathext_candidates(program, pathext)
} else {
vec![program.to_owned()]
}
}
const DEFAULT_PATHEXT: &str = ".COM;.EXE;.BAT;.CMD";
fn pathext_candidates(program: &Path, pathext: Option<&OsStr>) -> Vec<PathBuf> {
let pathext = pathext.unwrap_or_else(|| OsStr::new(DEFAULT_PATHEXT));
split_list(pathext)
.into_iter()
.filter(|extension| {
let bytes = extension.as_encoded_bytes();
bytes.first() == Some(&b'.') && !bytes.iter().any(|byte| matches!(byte, b'/' | b'\\'))
})
.map(|extension| {
let mut candidate = program.as_os_str().to_owned();
candidate.push(extension);
PathBuf::from(candidate)
})
.collect()
}
#[cfg(windows)]
fn split_list(list: &OsStr) -> Vec<OsString> {
use std::os::windows::ffi::{OsStrExt, OsStringExt};
let units: Vec<u16> = list.encode_wide().collect();
units
.split(|unit| *unit == u16::from(b';'))
.map(OsString::from_wide)
.collect()
}
#[cfg(unix)]
fn split_list(list: &OsStr) -> Vec<OsString> {
use std::os::unix::ffi::{OsStrExt, OsStringExt};
list.as_bytes()
.split(|byte| *byte == b';')
.map(|entry| OsString::from_vec(entry.to_vec()))
.collect()
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn lookup_uses_path_order_relative_to_child_cwd_and_never_implicit_local_search() {
let root = crate::test_os::tempdir();
for directory in ["first", "second"] {
std::fs::create_dir(root.path().join(directory)).unwrap();
crate::test_os::lookup_program(&root.path().join(directory), "shim", None);
}
let program = crate::test_os::executable_name("shim");
let path = OsString::from(crate::test_os::path_list(["second", "first"]));
assert_eq!(
resolve_with(Path::new(&program), root.path(), |name| (name
== SEARCH_PATH)
.then(|| path.clone()))
.unwrap(),
root.path().join("second").join(&program)
);
assert!(resolve_with(Path::new(&program), root.path(), |_| None).is_err());
}
#[cfg(windows)]
#[test]
fn pathext_candidates_keep_the_variable_order_and_skip_unsafe_suffixes() {
assert_eq!(
candidates(Path::new("dir/shim"), |_| Some(".CMD;bad;.a/b;.EXE".into())),
[PathBuf::from("dir/shim.CMD"), PathBuf::from("dir/shim.EXE")]
);
assert_eq!(
candidates(Path::new("shim"), |_| None),
[".COM", ".EXE", ".BAT", ".CMD"].map(|suffix| PathBuf::from(format!("shim{suffix}")))
);
}
#[cfg(windows)]
#[test]
fn extensionless_names_follow_pathext_order_not_exe_preference() {
let root = crate::test_os::tempdir();
for extension in ["cmd", "exe", "bat"] {
crate::test_os::lookup_program(root.path(), "shim", Some(extension));
}
let path = OsString::from(crate::test_os::path_list([root.path()]));
assert_eq!(
resolve_with(Path::new("shim"), root.path(), |name| match name {
SEARCH_PATH => Some(path.clone()),
EXTENSIONS => Some(".CMD;.EXE".into()),
_ => None,
})
.unwrap(),
root.path().join("shim.CMD")
);
assert_eq!(
resolve_with(Path::new("shim"), root.path(), |name| match name {
SEARCH_PATH => Some(path.clone()),
EXTENSIONS => Some(".BAT;.CMD".into()),
_ => None,
})
.unwrap(),
root.path().join("shim.BAT")
);
}
}