]> git.ipfire.org Git - pakfire.git/commitdiff
ELF: Add a convenience function to open from a file object
authorMichael Tremer <michael.tremer@ipfire.org>
Wed, 1 Jan 2025 16:46:10 +0000 (16:46 +0000)
committerMichael Tremer <michael.tremer@ipfire.org>
Wed, 1 Jan 2025 16:46:10 +0000 (16:46 +0000)
Signed-off-by: Michael Tremer <michael.tremer@ipfire.org>
src/libpakfire/elf.c
src/libpakfire/include/pakfire/elf.h

index 5ff46bb2a9636536edcf5363d5242287f5b6faad..7db5e27927f90b53d728375de0eafd4a8658579a 100644 (file)
@@ -23,6 +23,7 @@
 
 #include <pakfire/ctx.h>
 #include <pakfire/elf.h>
+#include <pakfire/file.h>
 #include <pakfire/string.h>
 
 // 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++;
 
index 465d9362054e7b528b5dd372d2584bea14a8b994..fbfcf3091fd9fda504eae7e3ad802c9cf7606785 100644 (file)
 struct pakfire_elf;
 
 #include <pakfire/ctx.h>
+#include <pakfire/file.h>
 
 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);