From: Michael Tremer Date: Sat, 21 Jun 2025 16:35:12 +0000 (+0000) Subject: json: Align creating new JSON objects with other interfaces X-Git-Url: http://git.ipfire.org/?a=commitdiff_plain;h=d2833fbd148949b22cb99c593a453e2f71cf3c4e;p=pakfire.git json: Align creating new JSON objects with other interfaces Signed-off-by: Michael Tremer --- diff --git a/src/pakfire/json.c b/src/pakfire/json.c index 116e7910..17c381d8 100644 --- a/src/pakfire/json.c +++ b/src/pakfire/json.c @@ -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, diff --git a/src/pakfire/json.h b/src/pakfire/json.h index b6a2d3ef..a7d192eb 100644 --- a/src/pakfire/json.h +++ b/src/pakfire/json.h @@ -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); diff --git a/src/pakfire/oci.c b/src/pakfire/oci.c index d9a086d4..2e4c2d50 100644 --- a/src/pakfire/oci.c +++ b/src/pakfire/oci.c @@ -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(); diff --git a/src/pakfire/repo.c b/src/pakfire/repo.c index ac627e06..abf6b94c 100644 --- a/src/pakfire/repo.c +++ b/src/pakfire/repo.c @@ -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; }