From e105731ee37d7d4d9091c3b7196f6f0c92d95db6 Mon Sep 17 00:00:00 2001 From: Michael Tremer Date: Fri, 25 Aug 2023 13:10:47 +0000 Subject: [PATCH] arch: Remove using FTS to search interpreter Signed-off-by: Michael Tremer --- src/libpakfire/arch.c | 51 ++++++++++++++++++++++++------------------- 1 file changed, 29 insertions(+), 22 deletions(-) diff --git a/src/libpakfire/arch.c b/src/libpakfire/arch.c index 2dc62585c..b6e963378 100644 --- a/src/libpakfire/arch.c +++ b/src/libpakfire/arch.c @@ -19,8 +19,9 @@ #############################################################################*/ #include +#include #include -#include +#include #include #include #include @@ -208,8 +209,17 @@ int pakfire_arch_supported_by_host(const char* name) { return pakfire_arch_is_compatible(native_arch, name); } -static char* find_interpreter(const char* path, const char* magic) { - FILE* f = fopen(path, "r"); +static char* find_interpreter(DIR* dir, const char* path, const char* magic) { + FILE* f = NULL; + int fd = -1; + + // Open the file + fd = openat(dirfd(dir), path, O_CLOEXEC); + if (fd < 0) + return NULL; + + // Re-open the file as file handle + f = fdopen(fd, "r"); if (!f) return NULL; @@ -259,6 +269,11 @@ static char* find_interpreter(const char* path, const char* magic) { } char* pakfire_arch_find_interpreter(const char* name) { + char* interpreter = NULL; + DIR* dir = NULL; + struct dirent* entry = NULL; + + // Check inputs if (!name) { errno = EINVAL; return NULL; @@ -267,32 +282,24 @@ char* pakfire_arch_find_interpreter(const char* name) { // If the host supports this architecture natively, // we do not need to search for the interpreter if (pakfire_arch_supported_by_host(name)) - return NULL; + goto ERROR; const struct pakfire_arch* arch = pakfire_arch_find(name); if (!arch) - return NULL; - - char* interpreter = NULL; - - char* paths[] = { - "/proc/sys/fs/binfmt_misc", NULL, - }; + goto ERROR; - FTS* f = fts_open(paths, FTS_NOCHDIR|FTS_NOSTAT, NULL); - if (!f) + // Open /proc/sys/fs/binfmt_misc + dir = opendir("/proc/sys/fs/binfmt_misc"); + if (!dir) goto ERROR; for (;;) { - FTSENT* fent = fts_read(f); - if (!fent) + entry = readdir(dir); + if (!entry) break; - // Only handle files - if (!(fent->fts_info & FTS_F)) - continue; - - interpreter = find_interpreter(fent->fts_path, arch->magic); + // Check if the file matches + interpreter = find_interpreter(dir, entry->d_name, arch->magic); // End search if we have found a match if (interpreter) @@ -300,8 +307,8 @@ char* pakfire_arch_find_interpreter(const char* name) { } ERROR: - if (f) - fts_close(f); + if (dir) + closedir(dir); return interpreter; } -- 2.39.5