Skip to content

Commit

Permalink
Use winsafe over windows-sys, and reduce dependency on rustix
Browse files Browse the repository at this point in the history
  • Loading branch information
Xaeroxe committed Jan 26, 2024
1 parent 96a8004 commit 071683c
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 15 deletions.
6 changes: 4 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,15 @@ keywords = ["which", "which-rs", "unix", "command"]
[dependencies]
either = "1.9.0"
regex = { version = "1.10.2", optional = true }
rustix = { version = "0.38.30", default-features = false, features = ["fs", "std"] }

[target.'cfg(any(windows, unix, target_os = "redox"))'.dependencies]
home = "0.5.9"

[target.'cfg(any(unix, target_os = "wasi", target_os = "redox"))'.dependencies]
rustix = { version = "0.38.30", default-features = false, features = ["fs", "std"] }

[target.'cfg(windows)'.dependencies]
windows-sys = { version = "0.52", features = ["Win32_Storage_FileSystem", "Win32_Foundation"] }
winsafe = { version = "0.0.19", features = ["kernel"] }
once_cell = "1"

[dev-dependencies]
Expand Down
15 changes: 2 additions & 13 deletions src/checker.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ impl ExecutableChecker {
}

impl Checker for ExecutableChecker {
#[cfg(any(unix, target_os = "wasi"))]
#[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()
Expand Down Expand Up @@ -53,18 +53,7 @@ impl Checker for ExistedChecker {

#[cfg(target_os = "windows")]
fn matches_arch(path: &Path) -> bool {
use std::os::windows::prelude::OsStrExt;

let os_str = path
.as_os_str()
.encode_wide()
.chain(std::iter::once(0))
.collect::<Vec<u16>>();
let mut out = 0;
let is_executable = unsafe {
windows_sys::Win32::Storage::FileSystem::GetBinaryTypeW(os_str.as_ptr(), &mut out)
};
is_executable != 0
winsafe::GetBinaryType(&path.display().to_string()).is_ok()
}

pub struct CompositeChecker {
Expand Down
2 changes: 2 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@
//!
//! ```

#![forbid(unsafe_code)]

mod checker;
mod error;
mod finder;
Expand Down

0 comments on commit 071683c

Please sign in to comment.