]> git.ipfire.org Git - pakfire.git/commitdiff
oci: Add some JSON data for the layer
authorMichael Tremer <michael.tremer@ipfire.org>
Sat, 8 Feb 2025 14:45:19 +0000 (14:45 +0000)
committerMichael Tremer <michael.tremer@ipfire.org>
Sat, 8 Feb 2025 14:45:19 +0000 (14:45 +0000)
Signed-off-by: Michael Tremer <michael.tremer@ipfire.org>
src/pakfire/oci.c

index e35c790096c95168877f7f8d4a1ede26f8dc9c56..2a63b7c4cc8f6f111e419d37806bc25f8976a9f4 100644 (file)
 #include <stdio.h>
 #include <stdlib.h>
 
+// json-c
+#include <json.h>
+
 #include <pakfire/archive_writer.h>
 #include <pakfire/ctx.h>
 #include <pakfire/hasher.h>
 #include <pakfire/hashes.h>
 #include <pakfire/i18n.h>
+#include <pakfire/json.h>
 #include <pakfire/logging.h>
 #include <pakfire/oci.h>
 #include <pakfire/pakfire.h>
 #include <pakfire/path.h>
+#include <pakfire/string.h>
 #include <pakfire/util.h>
 
 struct pakfire_oci_writer {
@@ -50,6 +55,118 @@ struct pakfire_oci_writer {
        char* layer_hexdigest;
 };
 
+static const char* pakfire_oci_arch(struct pakfire* pakfire) {
+       const char* arch = pakfire_get_arch(pakfire);
+
+       // aarch64
+       if (pakfire_string_equals(arch, "aarch64"))
+               return "arm64";
+
+       // riscv64
+       if (pakfire_string_equals(arch, "riscv64"))
+               return "riscv64";
+
+       // x86_64
+       if (pakfire_string_equals(arch, "x86_64"))
+               return "amd64";
+
+       // Otherwise fall back to the actual architecture
+       return arch;
+}
+
+static int pakfire_oci_writer_write_layer_version(struct pakfire_oci_writer* self) {
+       char filename[PATH_MAX];
+       int r;
+
+       const char* version = "1.0";
+
+       // Make the filename of the layer version
+       r = pakfire_path_format(filename, "%s/VERSION", self->layer_hexdigest);
+       if (r < 0)
+               return r;
+
+       // Write the version file
+       r = pakfire_archive_writer_create_file(self->image,
+                       filename, 0644, version, strlen(version));
+       if (r < 0)
+               return r;
+
+       return 0;
+}
+
+static int pakfire_oci_writer_write_layer_json(struct pakfire_oci_writer* self) {
+       struct json_object* o = NULL;
+       char filename[PATH_MAX];
+       char created[64];
+       int r;
+
+       // Make a new object
+       o = pakfire_json_new_object();
+       if (!o) {
+               r = -errno;
+               goto ERROR;
+       }
+
+       // Add the ID
+       r = pakfire_json_add_string(o, "id", self->layer_hexdigest);
+       if (r < 0)
+               goto ERROR;
+
+       // Format the current time
+       r = pakfire_strftime_now(created, "%Y-%m-%dT%H:%M:%SZ");
+       if (r < 0)
+               goto ERROR;
+
+       // Add the creation timestamp
+       r = pakfire_json_add_string(o, "created", created);
+       if (r < 0)
+               goto ERROR;
+
+       // Vendor
+       const char* vendor = pakfire_get_distro_vendor(self->pakfire);
+       if (vendor) {
+               r = pakfire_json_add_string(o, "author", vendor);
+               if (r < 0)
+                       goto ERROR;
+       }
+
+       const char* arch = pakfire_oci_arch(self->pakfire);
+
+       // Architecture
+       r = pakfire_json_add_string(o, "architecture", arch);
+       if (r < 0)
+               goto ERROR;
+
+       // OS
+       r = pakfire_json_add_string(o, "os", "linux");
+       if (r < 0)
+               goto ERROR;
+
+       // Version ID
+       const char* version_id = pakfire_get_distro_version_id(self->pakfire);
+       if (version_id) {
+               r = pakfire_json_add_string(o, "os.version", version_id);
+               if (r < 0)
+                       goto ERROR;
+       }
+
+       // Make the path
+       r = pakfire_path_format(filename, "%s/json", self->layer_hexdigest);
+       if (r < 0)
+               goto ERROR;
+
+       // Write the file to the image
+       r = pakfire_archive_writer_create_file_from_json(self->image, filename, 0644, o);
+       if (r < 0)
+               goto ERROR;
+
+ERROR:
+       if (o)
+               json_object_put(o);
+
+       return r;
+}
+
 static int pakfire_oci_writer_make_layer(struct pakfire_oci_writer* self) {
        char path[PATH_MAX] = PAKFIRE_TMP_DIR "/pakfire-oci-layer.XXXXXX";
        struct pakfire_archive_writer* layer = NULL;
@@ -102,13 +219,13 @@ static int pakfire_oci_writer_make_layer(struct pakfire_oci_writer* self) {
        if (r < 0)
                goto ERROR;
 
-       // Make the filename of the layer version
-       r = pakfire_path_format(filename, "%s/VERSION", self->layer_hexdigest);
+       // Write the version file
+       r = pakfire_oci_writer_write_layer_version(self);
        if (r < 0)
                goto ERROR;
 
-       // Write the version file
-       r = pakfire_archive_writer_create_file(self->image, filename, 0644, "1.0", strlen("1.0"));
+       // Write the json file
+       r = pakfire_oci_writer_write_layer_json(self);
        if (r < 0)
                goto ERROR;