use std::{
ffi::OsStr,
io,
path::{Component, Path, PathBuf},
};
pub(crate) fn reject_link_ancestors(path: &Path) -> Result<(), String> {
for ancestor in path.ancestors() {
if super::path_kind(ancestor).is_ok_and(|kind| kind == super::FileKind::Symlink) {
return Err("Artifact symlinks are not supported.".into());
}
}
Ok(())
}
pub(crate) fn folder_relative(path: &Path) -> Result<Option<String>, String> {
if path.is_absolute() {
return Ok(None);
}
if path.has_root() || matches!(path.components().next(), Some(Component::Prefix(_))) {
return Err("A program path must be absolute or relative to its Artifact.".into());
}
let command = super::path_text(path);
if !command.contains('/') {
return Ok(None);
}
let relative = command.strip_prefix("./").unwrap_or(&command);
artifactize_tools::scope::logical_path(relative).map_err(|error| error.to_string())?;
Ok(Some(relative.to_owned()))
}
pub(crate) fn resolve_scoped(
relative: &Path,
admit: impl Fn(&Path) -> Result<PathBuf, String>,
) -> Result<PathBuf, String> {
let mut failure = None;
for candidate in artifactize_tools::program::candidates(relative, super::environment::var) {
let candidate = if candidate != relative {
let parent = candidate.parent().unwrap_or(Path::new(""));
let directory = admit(parent)?;
let Some(spelling) = suffix_spelling(&directory, &candidate) else {
continue;
};
spelling
} else {
candidate
};
match admit(&candidate) {
Ok(path) if resolve(&path, Path::new("")).is_ok() => return Ok(path),
Ok(_) => (),
Err(error) => {
failure.get_or_insert(error);
}
}
}
Err(failure.unwrap_or_else(|| "Executable path is unavailable or outside scope.".into()))
}
fn suffix_spelling(directory: &Path, candidate: &Path) -> Option<PathBuf> {
let requested = candidate.file_name()?.to_str()?;
std::fs::read_dir(directory)
.ok()?
.filter_map(Result::ok)
.find_map(|entry| {
let name = entry.file_name();
name.to_str()?
.eq_ignore_ascii_case(requested)
.then(|| candidate.with_file_name(name))
})
}
pub(crate) fn resolve_with(
program: &Path,
cwd: &Path,
variable: impl Fn(&str) -> Option<std::ffi::OsString>,
) -> std::io::Result<PathBuf> {
artifactize_tools::program::resolve_with(program, cwd, variable)
}
pub(crate) fn resolve(program: &Path, cwd: &Path) -> std::io::Result<PathBuf> {
artifactize_tools::program::resolve(program, cwd)
}
pub(crate) fn resolve_near_current(program: &OsStr) -> io::Result<PathBuf> {
let current = std::env::current_exe()?;
if let Some(path) = resolve_sibling(program, ¤t) {
return Ok(path);
}
resolve(Path::new(program), &std::env::current_dir()?)
}
fn resolve_sibling(program: &OsStr, current: &Path) -> Option<PathBuf> {
const SIBLING_DIRECTORIES: usize = 2;
current
.ancestors()
.skip(1)
.take(SIBLING_DIRECTORIES)
.find_map(|directory| resolve(&directory.join(program), directory).ok())
}
pub(crate) async fn open_desktop(target: &std::ffi::OsStr) -> std::io::Result<()> {
artifactize_tools::opener::open(target).await
}
pub(crate) fn tool_output<'a>(
program: &str,
args: impl IntoIterator<Item = &'a std::ffi::OsStr>,
) -> Option<Vec<u8>> {
let cwd = std::env::current_dir().ok()?;
let program = resolve(Path::new(program), &cwd).ok()?;
let mut command = std::process::Command::new(program);
command
.args(args)
.env_clear()
.envs(super::environment::snapshot())
.stdin(std::process::Stdio::null());
super::hide_console_window(&mut command);
let output = command.output().ok()?;
output.status.success().then_some(output.stdout)
}
#[cfg(test)]
mod tests {
use super::*;
use crate::test_os;
#[test]
fn relative_program_paths_are_kept_inside_the_scope() {
assert_eq!(folder_relative(Path::new("tool")).unwrap(), None);
assert_eq!(
folder_relative(Path::new("./bin/tool")).unwrap().as_deref(),
Some("bin/tool")
);
assert!(folder_relative(Path::new("../bin/tool")).is_err());
}
#[cfg(windows)]
#[test]
fn windows_relative_program_paths_cannot_bypass_scope_validation() {
assert_eq!(
folder_relative(Path::new(r".\bin\tool"))
.unwrap()
.as_deref(),
Some("bin/tool")
);
assert!(folder_relative(Path::new(r"..\bin\tool")).is_err());
assert!(folder_relative(Path::new(r"C:bin\tool")).is_err());
assert!(folder_relative(Path::new(r"\bin\tool")).is_err());
}
#[test]
fn a_directory_does_not_hide_a_program_one_directory_up() {
sibling_fallback(|_, immediate| std::fs::create_dir(immediate).unwrap());
}
#[cfg(unix)]
#[test]
fn a_non_executable_file_does_not_hide_a_program_one_directory_up() {
sibling_fallback(|candidate, immediate| {
std::fs::copy(candidate, immediate).unwrap();
test_os::deny_execution(immediate);
});
}
fn sibling_fallback(place_unusable_sibling: impl FnOnce(&Path, &Path)) {
let root = test_os::tempdir();
let deps = root.path().join("deps");
std::fs::create_dir(&deps).unwrap();
let candidate = test_os::working_directory_program(root.path(), "worker");
let name = candidate.file_name().unwrap();
place_unusable_sibling(&candidate, &deps.join(name));
assert_eq!(
resolve_sibling(name, &deps.join("current")),
Some(candidate)
);
}
}