diff --git a/.github/workflows/rust.yml b/.github/workflows/rust.yml index 60921de..a14d04b 100644 --- a/.github/workflows/rust.yml +++ b/.github/workflows/rust.yml @@ -81,7 +81,7 @@ jobs: fail-fast: false matrix: os: [ubuntu-latest, windows-latest, macos-latest] - rust: ['1.85', stable, nightly] + rust: ['1.70', stable, nightly] steps: - name: Setup | Checkout uses: actions/checkout@v4 diff --git a/CHANGELOG.md b/CHANGELOG.md index 98599f1..24eb522 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,10 +1,10 @@ # CHANGELOG -## 9.0.0 +## 8.0.2 -- MSRV bumped to Rust 1.85.0 - Dependency on `home_env` removed, the implementation found in rust 1.85.0 for a home directory has been fixed. Thanks, [@madsmtm],(https://github.com/madsmtm) for this contribution to which! +- Dependency on `winsafe` removed, code for Windows API is now handwritten. ## 8.0.1 diff --git a/Cargo.lock b/Cargo.lock index 57bc227..2f06bd2 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1,6 +1,6 @@ # This file is automatically @generated by Cargo. # It is not intended for manual editing. -version = 4 +version = 3 [[package]] name = "aho-corasick" @@ -169,13 +169,12 @@ dependencies = [ [[package]] name = "which" -version = "9.0.0" +version = "8.0.2" dependencies = [ "regex", "rustix", "tempfile", "tracing", - "winsafe", ] [[package]] @@ -258,12 +257,6 @@ version = "0.53.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "d6bbff5f0aada427a1e5a6da5f1f98158182f26556f345ac9e04d36d0ebed650" -[[package]] -name = "winsafe" -version = "0.0.19" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "d135d17ab770252ad95e9a872d365cf3090e3be864a34ab46f48555993efc904" - [[package]] name = "wit-bindgen" version = "0.51.0" diff --git a/Cargo.toml b/Cargo.toml index c4c5131..f42c772 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,8 +1,8 @@ [package] name = "which" -version = "9.0.0" +version = "8.0.2" edition = "2021" -rust-version = "1.85" +rust-version = "1.70" authors = ["Harry Fei , Jacob Kiesel "] repository = "https://github.com/harryfei/which-rs.git" documentation = "https://docs.rs/which/" @@ -16,7 +16,7 @@ keywords = ["which", "which-rs", "unix", "command"] default = ["real-sys"] regex = ["dep:regex"] tracing = ["dep:tracing"] -real-sys = ["dep:rustix", "dep:winsafe"] +real-sys = ["dep:rustix"] [dependencies] regex = { version = "1.10.2", optional = true } @@ -25,9 +25,6 @@ tracing = { version = "0.1.40", default-features = false, optional = true } [target.'cfg(any(unix, target_os = "wasi", target_os = "redox"))'.dependencies] rustix = { version = "1.0.5", default-features = false, features = ["fs", "std"], optional = true } -[target.'cfg(windows)'.dependencies] -winsafe = { version = "0.0.19", features = ["kernel"], optional = true } - [dev-dependencies] tempfile = "3.9.0" diff --git a/src/finder.rs b/src/finder.rs index 9b25e54..49bb7ef 100644 --- a/src/finder.rs +++ b/src/finder.rs @@ -82,7 +82,10 @@ impl Finder { } _ => { #[cfg(feature = "tracing")] - tracing::trace!("{} has no path seperators, so only paths in PATH environment variable will be searched.", path.display()); + 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 = paths.ok_or(Error::CannotGetCurrentDirAndPathListEmpty)?; let paths = self.sys.env_split_paths(paths.as_ref()); @@ -346,7 +349,10 @@ impl, F: NonFatalErrorHandler> Iterator } } else { #[cfg(feature = "tracing")] - tracing::debug!("regex unable to evaluate filename as it's not valid unicode. Lossy filename conversion: {}", path.file_name().to_string_lossy()); + tracing::debug!( + "regex unable to evaluate filename as it's not valid unicode. Lossy filename conversion: {}", + path.file_name().to_string_lossy() + ); } } Some(Err(e)) => { diff --git a/src/lib.rs b/src/lib.rs index 3a975da..e3af417 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -21,6 +21,8 @@ mod error; mod finder; mod helper; pub mod sys; +#[cfg(all(windows, feature = "real-sys"))] +mod win_ffi; use std::fmt; use std::path; diff --git a/src/sys.rs b/src/sys.rs index 53795e0..986e9ee 100644 --- a/src/sys.rs +++ b/src/sys.rs @@ -210,9 +210,23 @@ impl Sys for RealSys { #[cfg(windows)] fn is_valid_executable(&self, path: &Path) -> io::Result { - winsafe::GetBinaryType(&path.display().to_string()) - .map(|_| true) - .map_err(|e| io::Error::from_raw_os_error(e.raw() as i32)) + use std::os::windows::ffi::OsStrExt; + + use crate::win_ffi; + let w_str: Vec = path + .as_os_str() + .encode_wide() + .chain(std::iter::once(0)) + .collect(); + let mut binary_type = 0u32; + unsafe { + let success = win_ffi::GetBinaryTypeW(w_str.as_ptr(), &mut binary_type); + if success != 0 { + Ok(true) + } else { + Err(io::Error::from_raw_os_error(win_ffi::GetLastError() as i32)) + } + } } } diff --git a/src/win_ffi.rs b/src/win_ffi.rs new file mode 100644 index 0000000..fc0a65f --- /dev/null +++ b/src/win_ffi.rs @@ -0,0 +1,18 @@ +#[cfg(all(windows, target_arch = "x86"))] +#[link( + name = "kernel32.dll", + kind = "raw-dylib", + modifiers = "+verbatim", + import_name_type = "undecorated" +)] +extern "system" { + pub fn GetBinaryTypeW(app_name: *const u16, bin_type: *mut u32) -> i32; + pub fn GetLastError() -> u32; +} + +#[cfg(all(windows, not(target_arch = "x86")))] +#[link(name = "kernel32.dll", kind = "raw-dylib", modifiers = "+verbatim")] +extern "system" { + pub fn GetBinaryTypeW(app_name: *const u16, bin_type: *mut u32) -> i32; + pub fn GetLastError() -> u32; +} diff --git a/tests/basic.rs b/tests/basic.rs index d14dcac..3e82c73 100644 --- a/tests/basic.rs +++ b/tests/basic.rs @@ -835,7 +835,7 @@ mod in_memory { Ok(Box::new(entries.into_iter())) } // should use ErrorKind::NotADirectory once upgrading rust version - _ => Err(Error::other("Not a directory")), + _ => Err(Error::new(ErrorKind::Other, "Not a directory")), } }