From: Michael Tremer Date: Fri, 25 Oct 2024 17:20:45 +0000 (+0000) Subject: linter: Create a separate place to scan the payload X-Git-Tag: 0.9.30~867 X-Git-Url: http://git.ipfire.org/?a=commitdiff_plain;h=0c0b7b1baeae0f289916f4088283ecbfff5d796c;p=pakfire.git linter: Create a separate place to scan the payload Otherwise this is becoming yet another callback hell... Signed-off-by: Michael Tremer --- diff --git a/Makefile.am b/Makefile.am index c31ad4651..3d08d0b8c 100644 --- a/Makefile.am +++ b/Makefile.am @@ -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 index 000000000..4a57fec99 --- /dev/null +++ b/src/libpakfire/include/pakfire/linter-file.h @@ -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 . # +# # +#############################################################################*/ + +#ifndef PAKFIRE_LINTER_FILE_H +#define PAKFIRE_LINTER_FILE_H + +#ifdef PAKFIRE_PRIVATE + +struct pakfire_linter_file; + +#include +#include +#include + +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 index 000000000..4d1d2c73a --- /dev/null +++ b/src/libpakfire/linter-file.c @@ -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 . # +# # +#############################################################################*/ + +#include +#include + +#include +#include +#include +#include + +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; +} diff --git a/src/libpakfire/linter.c b/src/libpakfire/linter.c index 97a2b88a8..69df57a7e 100644 --- a/src/libpakfire/linter.c +++ b/src/libpakfire/linter.c @@ -35,6 +35,7 @@ #include #include #include +#include #include #include #include @@ -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; }