]> git.ipfire.org Git - pakfire.git/commitdiff
json: Align creating new JSON objects with other interfaces
authorMichael Tremer <michael.tremer@ipfire.org>
Sat, 21 Jun 2025 16:35:12 +0000 (16:35 +0000)
committerMichael Tremer <michael.tremer@ipfire.org>
Sat, 21 Jun 2025 16:35:12 +0000 (16:35 +0000)
Signed-off-by: Michael Tremer <michael.tremer@ipfire.org>
src/pakfire/json.c
src/pakfire/json.h
src/pakfire/oci.c
src/pakfire/repo.c

index 116e79105283758929324e11f6479053d06d4e77..17c381d8303db310a01e6aadb2219aad12f6f0b7 100644 (file)
@@ -115,8 +115,15 @@ ERROR:
        return r;
 }
 
-struct json_object* pakfire_json_new_object(void) {
-       return json_object_new_object();
+int pakfire_json_new_object(struct json_object** json) {
+       // Make a new object
+       *json = json_object_new_object();
+
+       // Return an error if the object could not be created
+       if (!*json)
+               return -ENOMEM;
+
+       return 0;
 }
 
 int pakfire_json_add_string(struct json_object* json,
index b6a2d3ef510b2725c6c6300538475faf0037a32a..a7d192ebd0dd0c89e74e18dc922eeef801b33b72 100644 (file)
@@ -37,7 +37,7 @@ int pakfire_json_parse(struct json_object** json, char** error,
 // Parse from file
 int pakfire_json_parse_from_file(struct json_object** json, char** error, const char* path);
 
-struct json_object* pakfire_json_new_object(void);
+int pakfire_json_new_object(struct json_object** json);
 
 int pakfire_json_add_string(struct json_object* json, const char* name, const char* value);
 int pakfire_json_add_stringn(struct json_object* json, const char* name, const char* value, size_t length);
index d9a086d4f8cae17ae3a5d9657c670eb3466ccf06..2e4c2d50a4cae274183742c8da497c0911547404 100644 (file)
@@ -82,11 +82,9 @@ static int pakfire_oci_writer_write_oci_layout(struct pakfire_oci_writer* self)
        int r;
 
        // Make a new object
-       o = pakfire_json_new_object();
-       if (!o) {
-               r = -errno;
+       r = pakfire_json_new_object(&o);
+       if (r < 0)
                goto ERROR;
-       }
 
        // Set version
        r = pakfire_json_add_string(o, "imageLayoutVersion", "1.0.0");
@@ -162,11 +160,9 @@ static int pakfire_oci_writer_write_index(struct pakfire_oci_writer* self) {
        int r;
 
        // Make a new object
-       o = pakfire_json_new_object();
-       if (!o) {
-               r = -errno;
+       r = pakfire_json_new_object(&o);
+       if (r < 0)
                goto ERROR;
-       }
 
        // Set schema version
        r = pakfire_json_add_int64(o, "schemaVersion", 2);
@@ -208,11 +204,9 @@ static int pakfire_oci_writer_write_config(struct pakfire_oci_writer* self) {
        struct json_object* diffids = NULL;
 
        // Make a new object
-       o = pakfire_json_new_object();
-       if (!o) {
-               r = -errno;
+       r = pakfire_json_new_object(&o);
+       if (r < 0)
                goto ERROR;
-       }
 
        // Format creation timestamp
        r = pakfire_strftime_now(created, "%Y-%m-%dT%H:%M:%SZ");
@@ -338,11 +332,9 @@ static int pakfire_oci_writer_write_manifest(struct pakfire_oci_writer* self) {
        int r;
 
        // Make a new object
-       o = pakfire_json_new_object();
-       if (!o) {
-               r = -errno;
+       r = pakfire_json_new_object(&o);
+       if (r < 0)
                goto ERROR;
-       }
 
        // Set schema version
        r = pakfire_json_add_int64(o, "schemaVersion", 2);
@@ -370,11 +362,9 @@ static int pakfire_oci_writer_write_manifest(struct pakfire_oci_writer* self) {
                goto ERROR;
 
        // Make another new object
-       manifest = pakfire_json_new_object();
-       if (!manifest) {
-               r = -errno;
+       r = pakfire_json_new_object(&manifest);
+       if (r < 0)
                goto ERROR;
-       }
 
        // Add media type
        r = pakfire_json_add_string(manifest, "mediaType", "application/vnd.oci.image.manifest.v1+json");
@@ -492,11 +482,9 @@ static int pakfire_oci_writer_make_layer(struct pakfire_oci_writer* self) {
                goto ERROR;
 
        // Create a new JSON object
-       json = pakfire_json_new_object();
-       if (!json) {
-               r = -errno;
+       r = pakfire_json_new_object(&json);
+       if (r < 0)
                goto ERROR;
-       }
 
        // Add the media type
        r = pakfire_json_add_string(json, "mediaType", "application/vnd.oci.image.layer.v1.tar+gzip");
@@ -575,11 +563,9 @@ int pakfire_oci_mkimage(struct pakfire* pakfire, FILE* f) {
        int r;
 
        // Reference to the config
-       writer.config = json_object_new_object();
-       if (!writer.config) {
-               r = -errno;
+       r = pakfire_json_new_object(&writer.config);
+       if (r < 0)
                goto ERROR;
-       }
 
        // To store all layers
        writer.layers = json_object_new_array();
index ac627e0632f09fed26c4bf561e1ff957baecdad8..abf6b94c28d834b5eb163327b57df645891a9c29 100644 (file)
@@ -2306,10 +2306,9 @@ static int pakfire_repo_metadata_add_file(struct pakfire_repo* self,
        }
 
        // Create a new object
-       file = pakfire_json_new_object();
-       if (!file) {
-               ERROR(self->ctx, "Failed to create a new file object: %m\n");
-               r = -errno;
+       r = pakfire_json_new_object(&file);
+       if (r < 0) {
+               ERROR(self->ctx, "Failed to create a new file object: %s\n", strerror(-r));
                goto ERROR;
        }