]> git.ipfire.org Git - pakfire.git/commitdiff
archive writer: Add scaffolding
authorMichael Tremer <michael.tremer@ipfire.org>
Sat, 8 Feb 2025 10:29:41 +0000 (10:29 +0000)
committerMichael Tremer <michael.tremer@ipfire.org>
Sat, 8 Feb 2025 10:29:41 +0000 (10:29 +0000)
Signed-off-by: Michael Tremer <michael.tremer@ipfire.org>
Makefile.am
src/pakfire/archive_writer.c [new file with mode: 0644]
src/pakfire/archive_writer.h [new file with mode: 0644]

index 85ed1a6a63f4520fd8873dd5e2a1aebc26a8014d..c2c6b3d4129761f71ce2e2e5201a7d790a556d16 100644 (file)
@@ -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 (file)
index 0000000..c734ffc
--- /dev/null
@@ -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 <http://www.gnu.org/licenses/>.       #
+#                                                                             #
+#############################################################################*/
+
+#include <errno.h>
+#include <stdio.h>
+#include <stdlib.h>
+
+#include <pakfire/archive_writer.h>
+
+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 (file)
index 0000000..32ac9f3
--- /dev/null
@@ -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 <http://www.gnu.org/licenses/>.       #
+#                                                                             #
+#############################################################################*/
+
+#ifndef PAKFIRE_ARCHIVE_WRITER_H
+#define PAKFIRE_ARCHIVE_WRITER_H
+
+#include <stdio.h>
+
+#include <pakfire/pakfire.h>
+
+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 */