mirror of
https://github.com/openharmony/third_party_rust_which-rs.git
synced 2026-07-01 21:14:07 -04:00
fix #46 removed current directory error
This commit is contained in:
+12
-9
@@ -56,7 +56,7 @@ impl Finder {
|
||||
&self,
|
||||
binary_name: T,
|
||||
paths: Option<U>,
|
||||
cwd: V,
|
||||
cwd: Option<V>,
|
||||
binary_checker: CompositeChecker,
|
||||
) -> Result<impl Iterator<Item = PathBuf>>
|
||||
where
|
||||
@@ -66,15 +66,18 @@ impl Finder {
|
||||
{
|
||||
let path = PathBuf::from(&binary_name);
|
||||
|
||||
let binary_path_candidates = if path.has_separator() {
|
||||
// Search binary in cwd if the path have a path separator.
|
||||
Either::Left(Self::cwd_search_candidates(path, cwd).into_iter())
|
||||
} else {
|
||||
// Search binary in PATHs(defined in environment variable).
|
||||
let p = paths.ok_or(Error::CannotFindBinaryPath)?;
|
||||
let paths: Vec<_> = env::split_paths(&p).collect();
|
||||
let binary_path_candidates = match cwd {
|
||||
Some(cwd) if path.has_separator() => {
|
||||
// Search binary in cwd if the path have a path separator.
|
||||
Either::Left(Self::cwd_search_candidates(path, cwd).into_iter())
|
||||
}
|
||||
_ => {
|
||||
// Search binary in PATHs(defined in environment variable).
|
||||
let p = paths.ok_or(Error::CannotFindBinaryPath)?;
|
||||
let paths: Vec<_> = env::split_paths(&p).collect();
|
||||
|
||||
Either::Right(Self::path_search_candidates(path, paths).into_iter())
|
||||
Either::Right(Self::path_search_candidates(path, paths).into_iter())
|
||||
}
|
||||
};
|
||||
|
||||
Ok(binary_path_candidates.filter(move |p| binary_checker.is_valid(p)))
|
||||
|
||||
+15
-9
@@ -65,9 +65,13 @@ pub fn which<T: AsRef<OsStr>>(binary_name: T) -> Result<path::PathBuf> {
|
||||
|
||||
/// Find all binaries with `binary_name` in the path list `paths`, using `cwd` to resolve relative paths.
|
||||
pub fn which_all<T: AsRef<OsStr>>(binary_name: T) -> Result<impl Iterator<Item = path::PathBuf>> {
|
||||
let cwd = env::current_dir().map_err(|_| Error::CannotGetCurrentDir)?;
|
||||
let cwd = env::current_dir().ok();
|
||||
|
||||
which_in_all(binary_name, env::var_os("PATH"), cwd)
|
||||
let binary_checker = build_binary_checker();
|
||||
|
||||
let finder = Finder::new();
|
||||
|
||||
finder.find(binary_name, env::var_os("PATH"), cwd, binary_checker)
|
||||
}
|
||||
|
||||
/// Find all binaries matching a regular expression in a the system PATH.
|
||||
@@ -149,9 +153,7 @@ pub fn which_re_in<T>(
|
||||
where
|
||||
T: AsRef<OsStr>,
|
||||
{
|
||||
let binary_checker = CompositeChecker::new()
|
||||
.add_checker(Box::new(ExistedChecker::new()))
|
||||
.add_checker(Box::new(ExecutableChecker::new()));
|
||||
let binary_checker = build_binary_checker();
|
||||
|
||||
let finder = Finder::new();
|
||||
|
||||
@@ -169,13 +171,17 @@ where
|
||||
U: AsRef<OsStr>,
|
||||
V: AsRef<path::Path>,
|
||||
{
|
||||
let binary_checker = CompositeChecker::new()
|
||||
.add_checker(Box::new(ExistedChecker::new()))
|
||||
.add_checker(Box::new(ExecutableChecker::new()));
|
||||
let binary_checker = build_binary_checker();
|
||||
|
||||
let finder = Finder::new();
|
||||
|
||||
finder.find(binary_name, paths, cwd, binary_checker)
|
||||
finder.find(binary_name, paths, Some(cwd), binary_checker)
|
||||
}
|
||||
|
||||
fn build_binary_checker() -> CompositeChecker {
|
||||
CompositeChecker::new()
|
||||
.add_checker(Box::new(ExistedChecker::new()))
|
||||
.add_checker(Box::new(ExecutableChecker::new()))
|
||||
}
|
||||
|
||||
/// An owned, immutable wrapper around a `PathBuf` containing the path of an executable.
|
||||
|
||||
Reference in New Issue
Block a user