From 7097f67ea845dcf029d470b95dc1951d44ff21b5 Mon Sep 17 00:00:00 2001 From: Michael Tremer Date: Sat, 8 Feb 2025 10:29:41 +0000 Subject: [PATCH] archive writer: Add scaffolding Signed-off-by: Michael Tremer --- Makefile.am | 2 + src/pakfire/archive_writer.c | 104 +++++++++++++++++++++++++++++++++++ src/pakfire/archive_writer.h | 40 ++++++++++++++ 3 files changed, 146 insertions(+) create mode 100644 src/pakfire/archive_writer.c create mode 100644 src/pakfire/archive_writer.h diff --git a/Makefile.am b/Makefile.am index 85ed1a6a..c2c6b3d4 100644 --- a/Makefile.am +++ b/Makefile.am @@ -186,6 +186,8 @@ libpakfire_la_SOURCES = \ src/pakfire/arch.h \ src/pakfire/archive.c \ src/pakfire/archive.h \ + src/pakfire/archive_writer.c \ + src/pakfire/archive_writer.h \ src/pakfire/base64.c \ src/pakfire/base64.h \ src/pakfire/build.c \ diff --git a/src/pakfire/archive_writer.c b/src/pakfire/archive_writer.c new file mode 100644 index 00000000..c734ffc1 --- /dev/null +++ b/src/pakfire/archive_writer.c @@ -0,0 +1,104 @@ +/*############################################################################# +# # +# Pakfire - The IPFire package management system # +# Copyright (C) 2025 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 + +struct pakfire_archive_writer { + struct pakfire_ctx* ctx; + int nrefs; + + // Pakfire + struct pakfire* pakfire; + + // Format + pakfire_archive_writer_format format; + + // File Handle + FILE* f; +}; + +static void pakfire_archive_writer_free(struct pakfire_archive_writer* self) { + if (self->pakfire) + pakfire_unref(self->pakfire); + if (self->ctx) + pakfire_ctx_unref(self->ctx); + free(self); +} + +int pakfire_archive_writer_create(struct pakfire_archive_writer** writer, + struct pakfire* pakfire, pakfire_archive_writer_format format, FILE* f) { + struct pakfire_archive_writer* self = NULL; + int r; + + // Allocate some memory + self = calloc(1, sizeof(*self)); + if (!self) + return -errno; + + // Store a reference to the context + self->ctx = pakfire_ctx(pakfire); + + // Store a reference to Pakfire + self->pakfire = pakfire_ref(pakfire); + + // Initialize the reference counter + self->nrefs = 1; + + // Store the format + switch (format) { + case PAKFIRE_FORMAT_ARCHIVE: + self->format = format; + break; + + default: + ERROR(self->ctx, "Invalid archive format\n"); + r = -EINVAL; + goto ERROR; + } + + // Store the file handle + self->f = f; + + // Return the pointer + *writer = self; + return 0; + +ERROR: + pakfire_archive_writer_free(self); + return r; +} + +struct pakfire_archive_writer* pakfire_archive_writer_ref(struct pakfire_archive_writer* self) { + ++self->nrefs; + + return self; +} + +struct pakfire_archive_writer* pakfire_archive_writer_unref(struct pakfire_archive_writer* self) { + if (--self->nrefs > 0) + return self; + + pakfire_archive_writer_free(self); + return NULL; +} diff --git a/src/pakfire/archive_writer.h b/src/pakfire/archive_writer.h new file mode 100644 index 00000000..32ac9f38 --- /dev/null +++ b/src/pakfire/archive_writer.h @@ -0,0 +1,40 @@ +/*############################################################################# +# # +# Pakfire - The IPFire package management system # +# Copyright (C) 2025 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_ARCHIVE_WRITER_H +#define PAKFIRE_ARCHIVE_WRITER_H + +#include + +#include + +struct pakfire_archive_writer; + +typedef enum pakfire_archive_writer_formats { + PAKFIRE_FORMAT_ARCHIVE, +} pakfire_archive_writer_format; + +int pakfire_archive_writer_create(struct pakfire_archive_writer** writer, + struct pakfire* pakfire, pakfire_archive_writer_format format, FILE* f); + +struct pakfire_archive_writer* pakfire_archive_writer_ref(struct pakfire_archive_writer* self); +struct pakfire_archive_writer* pakfire_archive_writer_unref(struct pakfire_archive_writer* self); + +#endif /* PAKFIRE_ARCHIVE_WRITER_H */ -- 2.39.5