Skip to content

Commit

Permalink
Merge pull request #1169 from Ptipiak/mention-executable-detection
Browse files Browse the repository at this point in the history
Using faccess lib to detect executable files
  • Loading branch information
sharkdp authored Nov 15, 2022
2 parents d89b575 + 5c87ff5 commit 38cdeb0
Show file tree
Hide file tree
Showing 5 changed files with 22 additions and 22 deletions.
16 changes: 16 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,21 @@
# Upcoming release

## Features


## Changes

- Breaking: On Unix-like systems, `--type executable` now additionally checks if
the file is executable by the current user, see #1106 and #1169 (@ptipiak)


## Bugfixes


## Other



# v8.5.3

## Bugfixes
Expand Down
4 changes: 1 addition & 3 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ chrono = "0.4"
once_cell = "1.15.0"
crossbeam-channel = "0.5.6"
clap_complete = {version = "4.0.5", optional = true}
faccess = "0.2.4"

[dependencies.clap]
version = "4.0.22"
Expand All @@ -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.
Expand Down
1 change: 1 addition & 0 deletions src/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -643,6 +643,7 @@ pub enum FileType {
Directory,
#[value(alias = "l")]
Symlink,
/// A file which is executable by the current effective user
#[value(alias = "x")]
Executable,
#[value(alias = "e")]
Expand Down
15 changes: 1 addition & 14 deletions src/filesystem.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +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;

use crate::dir_entry;
Expand Down Expand Up @@ -44,16 +41,6 @@ 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 {
path.executable()
}

pub fn is_empty(entry: &dir_entry::DirEntry) -> bool {
if let Some(file_type) = entry.file_type() {
if file_type.is_dir() {
Expand Down
8 changes: 3 additions & 5 deletions src/filetypes.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
use crate::dir_entry;
use crate::filesystem;

use faccess::PathExt;

/// Whether or not to show
#[derive(Default)]
pub struct FileTypes {
Expand All @@ -21,11 +23,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 && !entry.path().executable())
|| (self.empty_only && !filesystem::is_empty(entry))
|| !(entry_type.is_file()
|| entry_type.is_dir()
Expand Down

0 comments on commit 38cdeb0

Please sign in to comment.