Forward-port #9931

This commit is contained in:
Henrik Hørlück Berg 2023-09-15 14:58:54 +02:00
parent 6ee81c0f15
commit 0cc1aef725
No known key found for this signature in database

View file

@ -315,17 +315,20 @@ fn file_get_desc(
filename: &wstr,
lstat: Option<fs::Metadata>,
stat: Option<io::Result<fs::Metadata>>,
definitely_executable: bool,
) -> &'static wstr {
let Some(lstat) = lstat else {
return *COMPLETE_FILE_DESC;
};
fn is_executable(buf: &fs::Metadata, filename: &wstr) -> bool {
let is_executable = |buf: &fs::Metadata, filename: &wstr| -> bool {
// Weird group permissions and other such issues make it non-trivial to find out if
// we can actually execute a file using the result from stat. It is much safer to
// use the access function, since it tells us exactly what we want to know.
(buf.mode() as mode_t & (S_IXUSR | S_IXGRP | S_IXOTH) != 0) && waccess(filename, X_OK) == 0
}
definitely_executable
|| (buf.mode() as mode_t & (S_IXUSR | S_IXGRP | S_IXOTH) != 0)
&& waccess(filename, X_OK) == 0
};
// stat was only queried if lstat succeeded
let stat = stat.unwrap();
@ -444,7 +447,7 @@ fn wildcard_test_flags_then_complete(
// Compute the description.
let desc = if expand_flags.contains(ExpandFlags::GEN_DESCRIPTIONS) {
let mut desc = file_get_desc(filename, lstat, stat).to_owned();
let mut desc = file_get_desc(filename, lstat, stat, executables_only).to_owned();
if !is_directory && !is_executable {
if !desc.is_empty() {