Skip to content

Commit

Permalink
Switch to rustix from libc
Browse files Browse the repository at this point in the history
This commit replaces the code using libc with equivalent code using
rustix. In addition to safety, this move also removes the dependency on
C code and replaces it with a syscall, which is much faster.

This PR also adds `forbid(unsafe_code)`, so that this crate shows up as
unsafe-free when processed in `cargo-geiger`.

Signed-off-by: John Nunley <[email protected]>
  • Loading branch information
notgull committed Aug 30, 2023
1 parent e5ec711 commit 851f302
Show file tree
Hide file tree
Showing 3 changed files with 6 additions and 10 deletions.
3 changes: 2 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,14 @@ keywords = ["which", "which-rs", "unix", "command"]

[dependencies]
either = "1.6.1"
libc = "0.2.121"
regex = { version = "1.5.5", optional = true }
rustix = { version = "0.38.10", default-features = false, features = ["fs", "std"] }

[target.'cfg(windows)'.dependencies]
once_cell = "1"

[dev-dependencies]
libc = "0.2.147"
tempfile = "3.3.0"

[package.metadata.docs.rs]
Expand Down
11 changes: 2 additions & 9 deletions src/checker.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,5 @@
use crate::finder::Checker;
#[cfg(any(unix, target_os = "wasi"))]
use std::ffi::CString;
use std::fs;
#[cfg(unix)]
use std::os::unix::ffi::OsStrExt;
#[cfg(target_os = "wasi")]
use std::os::wasi::ffi::OsStrExt;
use std::path::Path;

pub struct ExecutableChecker;
Expand All @@ -19,9 +13,8 @@ impl ExecutableChecker {
impl Checker for ExecutableChecker {
#[cfg(any(unix, target_os = "wasi"))]
fn is_valid(&self, path: &Path) -> bool {
CString::new(path.as_os_str().as_bytes())
.map(|c| unsafe { libc::access(c.as_ptr(), libc::X_OK) == 0 })
.unwrap_or(false)
use rustix::fs as rfs;
rfs::access(path, rfs::Access::EXEC_OK).is_ok()
}

#[cfg(windows)]
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 851f302

Please sign in to comment.