Skip to content

Commit 25518dd

Browse files
committed
fix: avoid mistaken linked libraries as binaries
1 parent c6ed2a9 commit 25518dd

File tree

1 file changed

+36
-0
lines changed

1 file changed

+36
-0
lines changed

src/files/filesys.rs

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ pub fn find_exec_files_in_dir(dir: &Path, deep: bool) -> Vec<PathBuf> {
4242
if file_type.is_dir() {
4343
stack.push(entry.path());
4444
} else if file_type.is_file()
45+
&& is_likely_a_binary(&entry.path())
4546
&& ((deep && is_exec_for_current_arch(&entry.path()).unwrap_or(false))
4647
|| (!deep && is_exec_by_magic_number(&entry.path())))
4748
{
@@ -56,6 +57,41 @@ pub fn find_exec_files_in_dir(dir: &Path, deep: bool) -> Vec<PathBuf> {
5657
result
5758
}
5859

60+
/// Return `true` when `path` is a likely a binary file.
61+
///
62+
/// A binary file is a file with a known extension that is likely to be a binary file.
63+
/// It is not a binary file if it has no extension or if it has an extension that is
64+
/// not a known binary extension.
65+
/// Known library extensions are: .so, .dylib, .a, .lib, .dll.
66+
///
67+
/// # Arguments
68+
///
69+
/// * `path` - The path to the file to check.
70+
///
71+
/// # Returns
72+
///
73+
/// * `true` if the file is a likely a binary file, `false` otherwise.
74+
pub fn is_likely_a_binary(path: &Path) -> bool {
75+
// if no extension, it's likely a binary file.
76+
// fast-track most common case.
77+
if path.extension().is_none() {
78+
return true;
79+
}
80+
// else, we guess!
81+
if path.extension().is_some()
82+
&& (path.extension().unwrap().to_string_lossy().to_lowercase() == "so"
83+
|| path.extension().unwrap().to_string_lossy().to_lowercase() == "dylib"
84+
|| path.extension().unwrap().to_string_lossy().to_lowercase() == "a"
85+
|| path.extension().unwrap().to_string_lossy().to_lowercase() == "o"
86+
|| path.extension().unwrap().to_string_lossy().to_lowercase() == "obj"
87+
|| path.extension().unwrap().to_string_lossy().to_lowercase() == "lib"
88+
|| path.extension().unwrap().to_string_lossy().to_lowercase() == "dll")
89+
{
90+
return false;
91+
}
92+
true
93+
}
94+
5995
/// Return `true` when `path` is a regular file with at least one executable bit set (Unix only).
6096
#[cfg(not(target_os = "windows"))]
6197
pub fn is_executable(path: &PathBuf) -> bool {

0 commit comments

Comments
 (0)