#include <pakfire/path.h>
#include <pakfire/string.h>
#include <pakfire/util.h>
+#include <pakfire/xfopen.h>
struct pakfire_oci_writer {
// Context
// 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;
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();
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)
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)
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);
pakfire_archive_writer_unref(writer.image);
if (writer.ctx)
pakfire_ctx_unref(writer.ctx);
+ if (writer.diffid)
+ free(writer.diffid);
return r;
}