From b7b27b039c0e6a8a72a2f8b5f136dc7526c0df25 Mon Sep 17 00:00:00 2001 From: Bruce Leidl Date: Sat, 5 Jan 2019 20:13:45 -0500 Subject: [PATCH] If command not absolute path, confirm it exists by searching $PATH --- libcitadel/src/util.rs | 26 +++++++++++++++++++++----- 1 file changed, 21 insertions(+), 5 deletions(-) diff --git a/libcitadel/src/util.rs b/libcitadel/src/util.rs index 8623337..c830f0b 100644 --- a/libcitadel/src/util.rs +++ b/libcitadel/src/util.rs @@ -1,19 +1,35 @@ -use std::path::Path; +use std::path::{Path,PathBuf}; use std::process::{Command,ExitStatus,Stdio}; use std::mem; use libc::{self, c_char}; use std::ffi::CStr; use std::str::from_utf8_unchecked; +use std::env; use failure::ResultExt; use Result; -pub fn ensure_command_exists(cmd_path: &str) -> Result<()> { - if !Path::new(cmd_path).exists() { - bail!("Cannot execute '{}': command does not exist", cmd_path); +fn search_path(filename: &str) -> Result { + let path_var = env::var("PATH")?; + for mut path in env::split_paths(&path_var) { + path.push(filename); + if path.exists() { + return Ok(path); + } } - Ok(()) + Err(format_err!("Could not find {} in $PATH", filename)) +} + +pub fn ensure_command_exists(cmd: &str) -> Result<()> { + let path = Path::new(cmd); + if !path.is_absolute() { + search_path(cmd)?; + return Ok(()) + } else if path.exists() { + return Ok(()) + } + Err(format_err!("Cannot execute '{}': command does not exist", cmd)) } pub fn exec_cmdline>(cmd_path: &str, args: S) -> Result<()> {