]> git.ipfire.org Git - pakfire.git/commitdiff
linter: Create a separate place to scan the payload
authorMichael Tremer <michael.tremer@ipfire.org>
Fri, 25 Oct 2024 17:20:45 +0000 (17:20 +0000)
committerMichael Tremer <michael.tremer@ipfire.org>
Fri, 25 Oct 2024 17:20:45 +0000 (17:20 +0000)
Otherwise this is becoming yet another callback hell...

Signed-off-by: Michael Tremer <michael.tremer@ipfire.org>
Makefile.am
src/libpakfire/include/pakfire/linter-file.h [new file with mode: 0644]
src/libpakfire/linter-file.c [new file with mode: 0644]
src/libpakfire/linter.c

index c31ad46518c5643b1ad6662dea7729aa4fdbdb90..3d08d0b8c35905f6e9c3d620c01fa8cd4cc2524c 100644 (file)
@@ -205,6 +205,7 @@ libpakfire_la_SOURCES = \
        src/libpakfire/job.c \
        src/libpakfire/key.c \
        src/libpakfire/linter.c \
+       src/libpakfire/linter-file.c \
        src/libpakfire/log_buffer.c \
        src/libpakfire/log_stream.c \
        src/libpakfire/logging.c \
@@ -257,6 +258,7 @@ pkginclude_HEADERS += \
        src/libpakfire/include/pakfire/job.h \
        src/libpakfire/include/pakfire/key.h \
        src/libpakfire/include/pakfire/linter.h \
+       src/libpakfire/include/pakfire/linter-file.h \
        src/libpakfire/include/pakfire/log_buffer.h \
        src/libpakfire/include/pakfire/log_stream.h \
        src/libpakfire/include/pakfire/logging.h \
diff --git a/src/libpakfire/include/pakfire/linter-file.h b/src/libpakfire/include/pakfire/linter-file.h
new file mode 100644 (file)
index 0000000..4a57fec
--- /dev/null
@@ -0,0 +1,39 @@
+/*#############################################################################
+#                                                                             #
+# Pakfire - The IPFire package management system                              #
+# Copyright (C) 2023 Pakfire development team                                 #
+#                                                                             #
+# This program is free software: you can redistribute it and/or modify        #
+# it under the terms of the GNU General Public License as published by        #
+# the Free Software Foundation, either version 3 of the License, or           #
+# (at your option) any later version.                                         #
+#                                                                             #
+# This program is distributed in the hope that it will be useful,             #
+# but WITHOUT ANY WARRANTY; without even the implied warranty of              #
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the               #
+# GNU General Public License for more details.                                #
+#                                                                             #
+# You should have received a copy of the GNU General Public License           #
+# along with this program.  If not, see <http://www.gnu.org/licenses/>.       #
+#                                                                             #
+#############################################################################*/
+
+#ifndef PAKFIRE_LINTER_FILE_H
+#define PAKFIRE_LINTER_FILE_H
+
+#ifdef PAKFIRE_PRIVATE
+
+struct pakfire_linter_file;
+
+#include <pakfire/ctx.h>
+#include <pakfire/file.h>
+#include <pakfire/linter.h>
+
+int pakfire_linter_file_create(struct pakfire_linter_file** lfile,
+       struct pakfire_ctx* ctx, struct pakfire_linter* linter, struct pakfire_file* file, int fd);
+
+struct pakfire_linter_file* pakfire_linter_file_ref(struct pakfire_linter_file* lfile);
+struct pakfire_linter_file* pakfire_linter_file_unref(struct pakfire_linter_file* lfile);
+
+#endif /* PAKFIRE_PRIVATE */
+#endif /* PAKFIRE_LINTER_FILE_H */
diff --git a/src/libpakfire/linter-file.c b/src/libpakfire/linter-file.c
new file mode 100644 (file)
index 0000000..4d1d2c7
--- /dev/null
@@ -0,0 +1,110 @@
+/*#############################################################################
+#                                                                             #
+# Pakfire - The IPFire package management system                              #
+# Copyright (C) 2024 Pakfire development team                                 #
+#                                                                             #
+# This program is free software: you can redistribute it and/or modify        #
+# it under the terms of the GNU General Public License as published by        #
+# the Free Software Foundation, either version 3 of the License, or           #
+# (at your option) any later version.                                         #
+#                                                                             #
+# This program is distributed in the hope that it will be useful,             #
+# but WITHOUT ANY WARRANTY; without even the implied warranty of              #
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the               #
+# GNU General Public License for more details.                                #
+#                                                                             #
+# You should have received a copy of the GNU General Public License           #
+# along with this program.  If not, see <http://www.gnu.org/licenses/>.       #
+#                                                                             #
+#############################################################################*/
+
+#include <errno.h>
+#include <stdlib.h>
+
+#include <pakfire/ctx.h>
+#include <pakfire/file.h>
+#include <pakfire/linter.h>
+#include <pakfire/linter-file.h>
+
+struct pakfire_linter_file {
+       struct pakfire_ctx* ctx;
+       int nrefs;
+
+       // Linter
+       struct pakfire_linter* linter;
+
+       // File
+       struct pakfire_file* file;
+
+       // File Descriptor
+       int fd;
+};
+
+int pakfire_linter_file_create(struct pakfire_linter_file** lfile,
+               struct pakfire_ctx* ctx, struct pakfire_linter* linter, struct pakfire_file* file, int fd) {
+       struct pakfire_linter_file* l = NULL;
+       int r = 0;
+
+       // Check input
+       if (fd < 0)
+               return -EBADF;
+
+       // Allocate some memory
+       l = calloc(1, sizeof(*l));
+       if (!l)
+               return -errno;
+
+       // Store a reference to the context
+       l->ctx = pakfire_ctx_ref(ctx);
+
+       // Initialize the reference counter
+       l->nrefs = 1;
+
+       // Store a reference to the linter
+       l->linter = pakfire_linter_ref(linter);
+
+       // Store a reference to the file
+       l->file = pakfire_file_ref(file);
+
+       // Store the file descriptor
+       l->fd = dup(fd);
+       if (l->fd < 0) {
+               ERROR(l->ctx, "Could not duplicate file descriptor: %m\n");
+               r = -errno;
+               goto ERROR;
+       }
+
+       // Return the pointer
+       *lfile = pakfire_linter_file_ref(l);
+
+ERROR:
+       if (l)
+               pakfire_linter_file_unref(l);
+
+       return r;
+}
+
+static void pakfire_linter_file_free(struct pakfire_linter_file* lfile) {
+       if (lfile->linter)
+               pakfire_linter_unref(lfile->linter);
+       if (lfile->file)
+               pakfire_file_unref(lfile->file);
+       if (lfile->ctx)
+               pakfire_ctx_unref(lfile->ctx);
+       if (lfile->fd >= 0)
+               close(lfile->fd);
+}
+
+struct pakfire_linter_file* pakfire_linter_file_ref(struct pakfire_linter_file* lfile) {
+       ++lfile->nrefs;
+
+       return lfile;
+}
+
+struct pakfire_linter_file* pakfire_linter_file_unref(struct pakfire_linter_file* lfile) {
+       if (--lfile->nrefs > 0)
+               return lfile;
+
+       pakfire_linter_file_free(lfile);
+       return NULL;
+}
index 97a2b88a84a1d7676dcc0084e8e2c51719db7070..69df57a7eba88952b63e9d6c07da6ebb0af965b9 100644 (file)
@@ -35,6 +35,7 @@
 #include <pakfire/fhs.h>
 #include <pakfire/file.h>
 #include <pakfire/linter.h>
+#include <pakfire/linter-file.h>
 #include <pakfire/logging.h>
 #include <pakfire/package.h>
 #include <pakfire/pakfire.h>
@@ -448,6 +449,7 @@ ERROR:
 
 static int pakfire_linter_payload(
                struct pakfire_linter* linter, struct pakfire_file* file, struct archive* a) {
+       struct pakfire_linter_file* lfile = NULL;
        int fd = -EBADF;
        int r;
 
@@ -461,6 +463,15 @@ static int pakfire_linter_payload(
        if (r < 0)
                goto ERROR;
 
+       // Create a linter file
+       r = pakfire_linter_file_create(&lfile, linter->ctx, linter, file, fd);
+       if (r < 0) {
+               ERROR(linter->ctx, "Could not create linter file for %s: %s\n", path, strerror(-r));
+               goto ERROR;
+       }
+
+       // XXX TODO
+
        // Check ELF files
        if (pakfire_linter_file_is_elf(linter, file, fd)) {
                // Check PIE
@@ -472,6 +483,11 @@ static int pakfire_linter_payload(
        }
 
 ERROR:
+       if (lfile)
+               pakfire_linter_file_unref(lfile);
+       if (fd >= 0)
+               close(fd);
+
        return r;
 }