From: Michael Tremer Date: Thu, 2 Jan 2025 15:53:02 +0000 (+0000) Subject: file: Remove libelf stuff and use our own abstraction X-Git-Tag: 0.9.30~591 X-Git-Url: http://git.ipfire.org/?a=commitdiff_plain;h=91b882a9f3b8982859ced403492f59780ecd45fb;p=pakfire.git file: Remove libelf stuff and use our own abstraction Signed-off-by: Michael Tremer --- diff --git a/src/libpakfire/file.c b/src/libpakfire/file.c index a988d811c..525b1ada6 100644 --- a/src/libpakfire/file.c +++ b/src/libpakfire/file.c @@ -35,11 +35,10 @@ #include -#include - #include #include #include +#include #include #include #include @@ -1346,17 +1345,6 @@ 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); @@ -1447,51 +1435,34 @@ static int pakfire_file_classify_magic(struct pakfire_file* file) { } static int pakfire_file_classify_elf(struct pakfire_file* file) { - FILE* f = NULL; - Elf* elf = NULL; + struct pakfire_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_fopen(file, "r"); - if (!f) { - ERROR(file->ctx, "Could not open %s: %m\n", pakfire_file_get_path(file)); - return 1; - } + // Try to open the file as ELF + r = pakfire_elf_open_file(&elf, file->ctx, file); + if (r < 0) { + switch (-r) { + // This is not an ELF file + case ENOTSUP: + return 0; - // 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; + default: + 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; - } + // Mark this file as an ELF file + file->class |= PAKFIRE_FILE_ELF; ERROR: if (elf) - elf_end(elf); - if (f) - fclose(f); + pakfire_elf_unref(elf); - return 0; + return r; } int pakfire_file_classify(struct pakfire_file* file) {