]> git.ipfire.org Git - pakfire.git/commitdiff
linter: Don't move the FD into the file module
authorMichael Tremer <michael.tremer@ipfire.org>
Fri, 25 Oct 2024 16:56:07 +0000 (16:56 +0000)
committerMichael Tremer <michael.tremer@ipfire.org>
Fri, 25 Oct 2024 16:56:07 +0000 (16:56 +0000)
Signed-off-by: Michael Tremer <michael.tremer@ipfire.org>
src/libpakfire/file.c
src/libpakfire/include/pakfire/file.h
src/libpakfire/linter.c

index 458e49a89759d84c0edd39fc3ea5dd3da4779333..5693c988fc1174e412d818ae58fcd4e58e347588 100644 (file)
@@ -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))
 
index c21672198765bba90daea31da1ab76575beadc7f..f03744e9a5b97a9cbe79c2433ec24b822fe1f4e4 100644 (file)
@@ -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);
index a70baa3340aaebf0196a1f2343ddfc8010634a73..97a2b88a84a1d7676dcc0084e8e2c51719db7070 100644 (file)
@@ -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;