]> git.ipfire.org Git - pakfire.git/commitdiff
oci: Add some missing metadata
authorMichael Tremer <michael.tremer@ipfire.org>
Mon, 10 Feb 2025 14:59:41 +0000 (14:59 +0000)
committerMichael Tremer <michael.tremer@ipfire.org>
Mon, 10 Feb 2025 14:59:41 +0000 (14:59 +0000)
Signed-off-by: Michael Tremer <michael.tremer@ipfire.org>
src/pakfire/oci.c

index 3914258d4c2a8afe6b73bc47c9e0a25a1b093a31..cc91143b5a67b01be15de728e5b14103c849ca08 100644 (file)
@@ -37,6 +37,7 @@
 #include <pakfire/path.h>
 #include <pakfire/string.h>
 #include <pakfire/util.h>
+#include <pakfire/xfopen.h>
 
 struct pakfire_oci_writer {
        // Context
@@ -48,6 +49,9 @@ struct pakfire_oci_writer {
        // Image writer
        struct pakfire_archive_writer* image;
 
+       // Diff ID of the base layer
+       char* diffid;
+
        // JSON Objects
        struct json_object* config;
        struct json_object* layers;
@@ -200,6 +204,8 @@ static int pakfire_oci_writer_write_config(struct pakfire_oci_writer* self) {
 
        struct json_object* config = NULL;
        struct json_object* cmd = NULL;
+       struct json_object* rootfs = NULL;
+       struct json_object* diffids = NULL;
 
        // Make a new object
        o = pakfire_json_new_object();
@@ -254,6 +260,16 @@ static int pakfire_oci_writer_write_config(struct pakfire_oci_writer* self) {
        if (r < 0)
                goto ERROR;
 
+       // Add user
+       r = pakfire_json_add_string(config, "User", "root");
+       if (r < 0)
+               goto ERROR;
+
+       // Add working directory
+       r = pakfire_json_add_string(config, "WorkingDir", "/root");
+       if (r < 0)
+               goto ERROR;
+
        // Add command
        r = pakfire_json_add_array(config, "Cmd", &cmd);
        if (r < 0)
@@ -264,6 +280,26 @@ static int pakfire_oci_writer_write_config(struct pakfire_oci_writer* self) {
        if (r < 0)
                goto ERROR;
 
+       // Add rootfs
+       r = pakfire_json_add_object(o, "rootfs", &rootfs);
+       if (r < 0)
+               goto ERROR;
+
+       // Add type
+       r = pakfire_json_add_string(rootfs, "type", "layers");
+       if (r < 0)
+               goto ERROR;
+
+       // Add diff IDs
+       r = pakfire_json_add_array(rootfs, "diff_ids", &diffids);
+       if (r < 0)
+               goto ERROR;
+
+       // Add the diff ID of the base layer
+       r = pakfire_json_array_add_stringf(diffids, "sha256:%s", self->diffid);
+       if (r < 0)
+               goto ERROR;
+
        // Write as blob
        r = pakfire_oci_writer_write_json_blob(self, o, &hexdigest, &size);
        if (r < 0)
@@ -497,6 +533,29 @@ static int pakfire_oci_writer_make_layer(struct pakfire_oci_writer* self) {
                goto ERROR;
        }
 
+       // Rewind
+       r = pakfire_rewind(f);
+       if (r < 0)
+               goto ERROR;
+
+       // Replace the file handle to decompress the payload again
+       f = pakfire_gzfopen(f, "r");
+       if (!f) {
+               ERROR(self->ctx, "Failed to re-open the layer for decompression: %m\n");
+               r = -errno;
+               goto ERROR;
+       }
+
+       // Hash the file
+       r = pakfire_hash_file(self->ctx, f, PAKFIRE_HASH_SHA2_256, &hashes);
+       if (r < 0)
+               goto ERROR;
+
+       // Extract the diff ID
+       r = pakfire_hashes_get_hex(&hashes, PAKFIRE_HASH_SHA2_256, &self->diffid);
+       if (r < 0)
+               goto ERROR;
+
 ERROR:
        if (layer)
                pakfire_archive_writer_unref(layer);
@@ -587,6 +646,8 @@ ERROR:
                pakfire_archive_writer_unref(writer.image);
        if (writer.ctx)
                pakfire_ctx_unref(writer.ctx);
+       if (writer.diffid)
+               free(writer.diffid);
 
        return r;
 }