From: Michael Tremer Date: Fri, 25 Oct 2024 16:56:07 +0000 (+0000) Subject: linter: Don't move the FD into the file module X-Git-Tag: 0.9.30~869 X-Git-Url: http://git.ipfire.org/?a=commitdiff_plain;h=837842a1d2b0b2dec415d278c2d0b6544e226a04;p=pakfire.git linter: Don't move the FD into the file module Signed-off-by: Michael Tremer --- diff --git a/src/libpakfire/file.c b/src/libpakfire/file.c index 458e49a89..5693c988f 100644 --- a/src/libpakfire/file.c +++ b/src/libpakfire/file.c @@ -68,9 +68,6 @@ struct pakfire_file { // Use the libarchive entry to store common attributes struct archive_entry* entry; - // File Descriptor - int fd; - // Capabilities cap_t caps; @@ -390,9 +387,6 @@ PAKFIRE_EXPORT int pakfire_file_create(struct pakfire_file** file, // Initialize reference counter f->nrefs = 1; - // Initialize file descriptor - f->fd = -EBADF; - // Create a new archive entry f->entry = archive_entry_new(); if (!f->entry) { @@ -530,9 +524,6 @@ ERROR: } static void pakfire_file_free(struct pakfire_file* file) { - if (file->fd >= 0) - close(file->fd); - // Free capabilities if (file->caps) cap_free(file->caps); @@ -573,27 +564,6 @@ int pakfire_file_set_flags(struct pakfire_file* file, int flag) { return 0; } -// File Descriptor - -int pakfire_file_get_fd(struct pakfire_file* file) { - return file->fd; -} - -int pakfire_file_set_fd(struct pakfire_file* file, int fd) { - // Close any previously assigned file descriptors - if (file->fd >= 0) - close(file->fd); - - // Duplicate the file descriptor - file->fd = dup(fd); - if (file->fd < 0) { - ERROR(file->ctx, "Could not duplicate file descriptor: %m\n"); - return -errno; - } - - return 0; -} - #define pakfire_file_strmode(file, buffer) \ __pakfire_file_strmode(file, buffer, sizeof(buffer)) diff --git a/src/libpakfire/include/pakfire/file.h b/src/libpakfire/include/pakfire/file.h index c21672198..f03744e9a 100644 --- a/src/libpakfire/include/pakfire/file.h +++ b/src/libpakfire/include/pakfire/file.h @@ -132,9 +132,6 @@ enum pakfire_file_classes { PAKFIRE_FILE_RUNTIME_LINKER = (1 << 14), }; -int pakfire_file_get_fd(struct pakfire_file* file); -int pakfire_file_set_fd(struct pakfire_file* file, int fd); - int pakfire_file_has_payload(struct pakfire_file* file); int pakfire_file_write_fcaps(struct pakfire_file* file, struct vfs_cap_data* cap_data); diff --git a/src/libpakfire/linter.c b/src/libpakfire/linter.c index a70baa334..97a2b88a8 100644 --- a/src/libpakfire/linter.c +++ b/src/libpakfire/linter.c @@ -238,13 +238,10 @@ static int pakfire_linter_init_libelf(struct pakfire_linter* linter) { } static int pakfire_linter_file_is_elf( - struct pakfire_linter* linter, struct pakfire_file* file) { + struct pakfire_linter* linter, struct pakfire_file* file, int fd) { Elf* elf = NULL; int r; - // Fetch the file descriptor - int fd = pakfire_file_get_fd(file); - // Initialize libelf r = pakfire_linter_init_libelf(linter); if (r < 0) @@ -279,11 +276,10 @@ ERROR: /* A helper function that opens an ELF file and calls a callback. */ -static int pakfire_linter_elf(struct pakfire_linter* linter, struct pakfire_file* file, +static int pakfire_linter_elf(struct pakfire_linter* linter, struct pakfire_file* file, int fd, int (*callback)(struct pakfire_linter* linter, struct pakfire_file* file, Elf* elf, void* data), void* data) { Elf* elf = NULL; - int fd = -EBADF; int r; // Initialize libelf @@ -291,11 +287,6 @@ static int pakfire_linter_elf(struct pakfire_linter* linter, struct pakfire_file if (r < 0) return r; - // Fetch file descriptor - fd = pakfire_file_get_fd(file); - if (fd < 0) - return fd; - // Parse the ELF header elf = elf_begin(fd, ELF_C_READ, NULL); if (!elf) { @@ -344,12 +335,12 @@ static int __pakfire_linter_get_elf_type(struct pakfire_linter* linter, } static int pakfire_linter_get_elf_type( - struct pakfire_linter* linter, struct pakfire_file* file) { + struct pakfire_linter* linter, struct pakfire_file* file, int fd) { int type = ET_NONE; int r; // Fetch the type - r = pakfire_linter_elf(linter, file, __pakfire_linter_get_elf_type, &type); + r = pakfire_linter_elf(linter, file, fd, __pakfire_linter_get_elf_type, &type); if (r < 0) return r; @@ -357,8 +348,8 @@ static int pakfire_linter_get_elf_type( } static int pakfire_linter_check_pie( - struct pakfire_linter* linter, struct pakfire_file* file) { - switch (pakfire_linter_get_elf_type(linter, file)) { + struct pakfire_linter* linter, struct pakfire_file* file, int fd) { + switch (pakfire_linter_get_elf_type(linter, file, fd)) { // Shared Object files are good case ET_DYN: return 0; @@ -446,10 +437,7 @@ static int pakfire_linter_read_file( goto ERROR; } - // Store the file descriptor - r = pakfire_file_set_fd(file, fd); - if (r < 0) - goto ERROR; + return fd; ERROR: if (fd >= 0) @@ -460,6 +448,7 @@ ERROR: static int pakfire_linter_payload( struct pakfire_linter* linter, struct pakfire_file* file, struct archive* a) { + int fd = -EBADF; int r; // Fetch path @@ -468,14 +457,14 @@ static int pakfire_linter_payload( DEBUG(linter->ctx, "Checking payload of %s\n", path); // Read the file - r = pakfire_linter_read_file(linter, file, a); + fd = r = pakfire_linter_read_file(linter, file, a); if (r < 0) goto ERROR; // Check ELF files - if (pakfire_linter_file_is_elf(linter, file)) { + if (pakfire_linter_file_is_elf(linter, file, fd)) { // Check PIE - r = pakfire_linter_check_pie(linter, file); + r = pakfire_linter_check_pie(linter, file, fd); if (r < 0) goto ERROR;