From: Michael Tremer Date: Sat, 8 Feb 2025 14:45:19 +0000 (+0000) Subject: oci: Add some JSON data for the layer X-Git-Tag: 0.9.30~66 X-Git-Url: http://git.ipfire.org/?a=commitdiff_plain;h=b1b557a2f5fc999bf6221b5699a07198f0426c43;p=pakfire.git oci: Add some JSON data for the layer Signed-off-by: Michael Tremer --- diff --git a/src/pakfire/oci.c b/src/pakfire/oci.c index e35c7900..2a63b7c4 100644 --- a/src/pakfire/oci.c +++ b/src/pakfire/oci.c @@ -22,15 +22,20 @@ #include #include +// json-c +#include + #include #include #include #include #include +#include #include #include #include #include +#include #include 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;