From: Michael Tremer Date: Wed, 25 Dec 2024 13:18:14 +0000 (+0000) Subject: Revert "file: Remove ELF detection with libelf" X-Git-Tag: 0.9.30~682 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=5eeea5804e9f770b302f81d03c8a28bc06c56a86;p=pakfire.git Revert "file: Remove ELF detection with libelf" This reverts commit 65fcf2eb3c4fceb81887bd969e68ceae8613e26e. Signed-off-by: Michael Tremer --- diff --git a/src/libpakfire/file.c b/src/libpakfire/file.c index 8e003c6cb..cda479388 100644 --- a/src/libpakfire/file.c +++ b/src/libpakfire/file.c @@ -31,9 +31,10 @@ #include #include -// libarchive #include +#include + #include #include #include @@ -1307,6 +1308,17 @@ PAKFIRE_EXPORT int pakfire_file_set_mimetype( Classification */ +static int setup_libelf(struct pakfire_ctx* ctx) { + // Initialize libelf + if (elf_version(EV_CURRENT) == EV_NONE) { + ERROR(ctx, "Could not initialize libelf: %s\n", elf_errmsg(-1)); + + return 1; + } + + return 0; +} + static int pakfire_file_classify_mode(struct pakfire_file* file) { const mode_t mode = pakfire_file_get_mode(file); @@ -1396,6 +1408,54 @@ static int pakfire_file_classify_magic(struct pakfire_file* file) { return 0; } +static int pakfire_file_classify_elf(struct pakfire_file* file) { + FILE* f = NULL; + Elf* elf = NULL; + int r; + + // Don't run this if we already know that file is an ELF file + if (file->class & PAKFIRE_FILE_ELF) + return 0; + + // Setup libelf + r = setup_libelf(file->ctx); + if (r) + return r; + + // Open the file + f = pakfire_file_open(file); + if (!f) { + ERROR(file->ctx, "Could not open %s: %m\n", pakfire_file_get_path(file)); + return 1; + } + + // Try to open the ELF file + elf = elf_begin(fileno(f), ELF_C_READ, NULL); + if (!elf) { + // We fail silently here, because this file might be in a different format + goto ERROR; + } + + switch (elf_kind(elf)) { + // Mark this file as an ELF file + case ELF_K_ELF: + file->class |= PAKFIRE_FILE_ELF; + break; + + // Ignore everything else + default: + break; + } + +ERROR: + if (elf) + elf_end(elf); + if (f) + fclose(f); + + return 0; +} + int pakfire_file_classify(struct pakfire_file* file) { int r; @@ -1416,6 +1476,11 @@ int pakfire_file_classify(struct pakfire_file* file) { r = pakfire_file_classify_magic(file); if (r) goto ERROR; + + // Check if the file is an ELF file + r = pakfire_file_classify_elf(file); + if (r) + goto ERROR; } } diff --git a/src/libpakfire/include/pakfire/file.h b/src/libpakfire/include/pakfire/file.h index 9be4649ce..52b1cf02c 100644 --- a/src/libpakfire/include/pakfire/file.h +++ b/src/libpakfire/include/pakfire/file.h @@ -123,6 +123,7 @@ enum pakfire_file_classes { PAKFIRE_FILE_EXECUTABLE = (1 << 7), // The rest + PAKFIRE_FILE_ELF = (1 << 8), PAKFIRE_FILE_PERL = (1 << 10), };