From 6dc0a85f053a8f445a903b17779052628c6145b9 Mon Sep 17 00:00:00 2001 From: Ptipiak Date: Fri, 11 Nov 2022 14:36:16 +0100 Subject: [PATCH] Using faccess lib to detect executable files * The detection of executable files was not exactly the same as the original find --- CHANGELOG.md | 8 ++++++++ Cargo.toml | 6 ++---- src/cli.rs | 1 + src/filesystem.rs | 12 +++--------- src/filetypes.rs | 6 +----- 5 files changed, 15 insertions(+), 18 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index cde2c294a..19424253f 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,11 @@ +# unreleased + +## Changes + +- On Unix-like system change the detection of executable, + `--type executable` now checks if file is executable by the current user, + see #1106 (@ptipiak) + # v8.5.2 ## Bugfixes diff --git a/Cargo.toml b/Cargo.toml index 5ae8e9bfe..4b36771fd 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -50,7 +50,8 @@ normpath = "0.3.2" chrono = "0.4" once_cell = "1.15.0" crossbeam-channel = "0.5.6" -clap_complete = {version = "4.0.5", optional = true} +clap_complete = {version = "4.0", optional = true} +faccess = "0.2.4" [dependencies.clap] version = "4.0.22" @@ -63,9 +64,6 @@ nix = { version = "0.24.2", default-features = false, features = ["signal"] } [target.'cfg(all(unix, not(target_os = "redox")))'.dependencies] libc = "0.2" -[target.'cfg(windows)'.dependencies] -faccess = "0.2.4" - # FIXME: Re-enable jemalloc on macOS # jemalloc is currently disabled on macOS due to a bug in jemalloc in combination with macOS # Catalina. See https://github.com/sharkdp/fd/issues/498 for details. diff --git a/src/cli.rs b/src/cli.rs index 09630a6e3..0b9ddf9d7 100644 --- a/src/cli.rs +++ b/src/cli.rs @@ -643,6 +643,7 @@ pub enum FileType { Directory, #[value(alias = "l")] Symlink, + /// Filter file which are executable by the current effective user #[value(alias = "x")] Executable, #[value(alias = "e")] diff --git a/src/filesystem.rs b/src/filesystem.rs index 2e79591c1..5c86213aa 100644 --- a/src/filesystem.rs +++ b/src/filesystem.rs @@ -4,10 +4,9 @@ use std::ffi::OsStr; use std::fs; use std::io; #[cfg(any(unix, target_os = "redox"))] -use std::os::unix::fs::{FileTypeExt, PermissionsExt}; +use std::os::unix::fs::FileTypeExt; use std::path::{Path, PathBuf}; -#[cfg(windows)] use faccess::PathExt as _; use normpath::PathExt; @@ -44,13 +43,8 @@ pub fn is_existing_directory(path: &Path) -> bool { path.is_dir() && (path.file_name().is_some() || path.normalize().is_ok()) } -#[cfg(any(unix, target_os = "redox"))] -pub fn is_executable(_: &Path, md: &fs::Metadata) -> bool { - md.permissions().mode() & 0o111 != 0 -} - -#[cfg(windows)] -pub fn is_executable(path: &Path, _: &fs::Metadata) -> bool { +/// Return true if the path point to a file executable by the current effective user +pub fn is_executable(path: &Path) -> bool { path.executable() } diff --git a/src/filetypes.rs b/src/filetypes.rs index 42e95d2a5..62ee3dc3c 100644 --- a/src/filetypes.rs +++ b/src/filetypes.rs @@ -21,11 +21,7 @@ impl FileTypes { || (!self.symlinks && entry_type.is_symlink()) || (!self.sockets && filesystem::is_socket(*entry_type)) || (!self.pipes && filesystem::is_pipe(*entry_type)) - || (self.executables_only - && !entry - .metadata() - .map(|md| filesystem::is_executable(entry.path(), md)) - .unwrap_or(false)) + || (self.executables_only && !filesystem::is_executable(entry.path())) || (self.empty_only && !filesystem::is_empty(entry)) || !(entry_type.is_file() || entry_type.is_dir()