use std::{
ffi::OsStr,
io,
path::{Component, Path, PathBuf},
};
pub(crate) fn reject_link_ancestors(path: &Path) -> io::Result<()> {
for ancestor in path
.ancestors()
.filter(|ancestor| ancestor.file_name().is_some())
{
match super::path_kind(ancestor) {
Ok(super::FileKind::Symlink) => {
return Err(io::Error::other("Artifact symlinks are not supported."));
}
Ok(_) => (),
Err(error) if error.kind() == io::ErrorKind::NotFound => (),
Err(error) => return Err(error),
}
}
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;