From: Michael Tremer Date: Wed, 1 Jan 2025 16:46:10 +0000 (+0000) Subject: ELF: Add a convenience function to open from a file object X-Git-Tag: 0.9.30~611 X-Git-Url: http://git.ipfire.org/?a=commitdiff_plain;h=cdbe09b72c6efc58f01736954c6285392cacb6ab;p=pakfire.git ELF: Add a convenience function to open from a file object Signed-off-by: Michael Tremer --- diff --git a/src/libpakfire/elf.c b/src/libpakfire/elf.c index 5ff46bb2a..7db5e2792 100644 --- a/src/libpakfire/elf.c +++ b/src/libpakfire/elf.c @@ -23,6 +23,7 @@ #include #include +#include #include // libelf @@ -151,6 +152,32 @@ ERROR: return r; } +int pakfire_elf_open_file(struct pakfire_elf** elf, + struct pakfire_ctx* ctx, struct pakfire_file* file) { + const char* path = NULL; + int fd = -EBADF; + int r; + + // Fetch the path + path = pakfire_file_get_path(file); + + // Open the file + fd = pakfire_file_open(file, 0); + if (fd < 0) + return -errno; + + // Open the ELF object + r = pakfire_elf_open(elf, ctx, path, fd); + if (r < 0) + goto ERROR; + +ERROR: + if (fd >= 0) + close(fd); + + return r; +} + struct pakfire_elf* pakfire_elf_ref(struct pakfire_elf* self) { self->nrefs++; diff --git a/src/libpakfire/include/pakfire/elf.h b/src/libpakfire/include/pakfire/elf.h index 465d93620..fbfcf3091 100644 --- a/src/libpakfire/include/pakfire/elf.h +++ b/src/libpakfire/include/pakfire/elf.h @@ -27,9 +27,12 @@ struct pakfire_elf; #include +#include int pakfire_elf_open(struct pakfire_elf** elf, struct pakfire_ctx* ctx, const char* path, int fd); +int pakfire_elf_open_file(struct pakfire_elf** elf, + struct pakfire_ctx* ctx, struct pakfire_file* file); struct pakfire_elf* pakfire_elf_ref(struct pakfire_elf* self); struct pakfire_elf* pakfire_elf_unref(struct pakfire_elf* self);