Skip to content

Commit

Permalink
add optional tracing feature, trace which logic
Browse files Browse the repository at this point in the history
  • Loading branch information
Xaeroxe committed Jul 30, 2024
1 parent 7a71ac6 commit d6a1416
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 7 deletions.
5 changes: 5 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,14 @@ readme = "README.md"
categories = ["os", "filesystem"]
keywords = ["which", "which-rs", "unix", "command"]

[features]
regex = ["dep:regex"]
tracing = ["dep:tracing"]

[dependencies]
either = "1.9.0"
regex = { version = "1.10.2", optional = true }
tracing = { version = "0.1.40", optional = true }

[target.'cfg(any(windows, unix, target_os = "redox"))'.dependencies]
home = "0.5.9"
Expand Down
26 changes: 20 additions & 6 deletions src/checker.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,10 @@ impl Checker for ExecutableChecker {
#[cfg(any(unix, target_os = "wasi", target_os = "redox"))]
fn is_valid(&self, path: &Path) -> bool {
use rustix::fs as rfs;
rfs::access(path, rfs::Access::EXEC_OK).is_ok()
let ret = rfs::access(path, rfs::Access::EXEC_OK).is_ok();
#[cfg(feature = "tracing")]
tracing::trace!("{} EXEC_OK = {ret}", path.display());
ret
}

#[cfg(windows)]
Expand All @@ -34,26 +37,37 @@ impl ExistedChecker {
impl Checker for ExistedChecker {
#[cfg(target_os = "windows")]
fn is_valid(&self, path: &Path) -> bool {
fs::symlink_metadata(path)
let ret = fs::symlink_metadata(path)
.map(|metadata| {
let file_type = metadata.file_type();
#[cfg(feature = "tracing")]
tracing::trace!("{} is_file() = {}, is_symlink() = {}", new_path.display(), file_type.is_file(), file_type.is_symlink());
file_type.is_file() || file_type.is_symlink()
})
.unwrap_or(false)
&& (path.extension().is_some() || matches_arch(path))
&& (path.extension().is_some() || matches_arch(path));
#[cfg(feature = "tracing")]
tracing::trace!("{} has_extension = {}, ExistedChecker::is_valid() = {ret}", path.display(), path.extension().is_some());
ret
}

#[cfg(not(target_os = "windows"))]
fn is_valid(&self, path: &Path) -> bool {
fs::metadata(path)
let ret = fs::metadata(path)
.map(|metadata| metadata.is_file())
.unwrap_or(false)
.unwrap_or(false);
#[cfg(feature = "tracing")]
tracing::trace!("{} is_file() = {ret}", path.display());
ret
}
}

#[cfg(target_os = "windows")]
fn matches_arch(path: &Path) -> bool {
winsafe::GetBinaryType(&path.display().to_string()).is_ok()
let ret = winsafe::GetBinaryType(&path.display().to_string()).is_ok();
#[cfg(feature = "tracing")]
tracing::trace!("{} matches_arch() = {ret}", path.display());
ret
}

pub struct CompositeChecker {
Expand Down
25 changes: 24 additions & 1 deletion src/finder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -78,10 +78,17 @@ impl Finder {

let binary_path_candidates = match cwd {
Some(cwd) if path.has_separator() => {
#[cfg(feature = "tracing")]
tracing::trace!(
"{} has a path seperator, so only CWD will be searched.",
path.display()
);
// Search binary in cwd if the path have a path separator.
Either::Left(Self::cwd_search_candidates(path, cwd).into_iter())
}
_ => {
#[cfg(feature = "tracing")]
tracing::trace!("{} has no path seperators, so only paths in PATH environment variable will be searched.", path.display());
// Search binary in PATHs(defined in environment variable).
let paths =
env::split_paths(&paths.ok_or(Error::CannotGetCurrentDirAndPathListEmpty)?)
Expand Down Expand Up @@ -201,8 +208,18 @@ impl Finder {
});
// Check if path already have executable extension
if has_executable_extension(&p, path_extensions) {
#[cfg(feature = "tracing")]
tracing::trace!(
"{} already has an executable extension, not modifying it further",
p.display()
);
Box::new(iter::once(p))
} else {
#[cfg(feature = "tracing")]
tracing::trace!(
"{} has no extension, using PATHEXT environment variable to infer one",
p.display()
);
// Appended paths with windows executable extensions.
// e.g. path `c:/windows/bin[.ext]` will expand to:
// [c:/windows/bin.ext]
Expand All @@ -215,7 +232,8 @@ impl Finder {
// Append the extension.
let mut p = p.clone().into_os_string();
p.push(e);

#[cfg(feature = "tracing")]
tracing::trace!("possible extension: {}", p.display());
PathBuf::from(p)
})),
)
Expand All @@ -230,6 +248,11 @@ fn tilde_expansion(p: &PathBuf) -> Cow<'_, PathBuf> {
if o == "~" {
let mut new_path = home_dir().unwrap_or_default();
new_path.extend(component_iter);
#[cfg(feature = "tracing")]
tracing::trace!(
"found tilde, substituting in user's home directory to get {}",
new_path.display()
);
Cow::Owned(new_path)
} else {
Cow::Borrowed(p)
Expand Down

0 comments on commit d6a1416

Please sign in to comment.