From cc7412555ffe401c60c457387972ab46c5ee35e7 Mon Sep 17 00:00:00 2001 From: Michael Tremer Date: Sun, 2 Feb 2025 13:42:15 +0000 Subject: [PATCH] json: Move functions into a separate file Signed-off-by: Michael Tremer --- Makefile.am | 2 + src/pakfire/daemon.c | 1 + src/pakfire/job.c | 1 + src/pakfire/mirrorlist.c | 1 + src/pakfire/package.c | 1 + src/pakfire/repo.c | 1 + src/pakfire/util.c | 154 --------------------------------------- src/pakfire/util.h | 12 --- src/pakfire/xfer.c | 1 + 9 files changed, 8 insertions(+), 166 deletions(-) diff --git a/Makefile.am b/Makefile.am index 4f242028..b730e18e 100644 --- a/Makefile.am +++ b/Makefile.am @@ -229,6 +229,8 @@ libpakfire_la_SOURCES = \ src/pakfire/jail.h \ src/pakfire/job.c \ src/pakfire/job.h \ + src/pakfire/json.c \ + src/pakfire/json.h \ src/pakfire/key.c \ src/pakfire/key.h \ src/pakfire/linter.c \ diff --git a/src/pakfire/daemon.c b/src/pakfire/daemon.c index 94a3307e..b5f6eab4 100644 --- a/src/pakfire/daemon.c +++ b/src/pakfire/daemon.c @@ -35,6 +35,7 @@ #include #include #include +#include #include #include diff --git a/src/pakfire/job.c b/src/pakfire/job.c index 642c8422..3335426a 100644 --- a/src/pakfire/job.c +++ b/src/pakfire/job.c @@ -38,6 +38,7 @@ #include #include #include +#include #include #include #include diff --git a/src/pakfire/mirrorlist.c b/src/pakfire/mirrorlist.c index b1da8bba..801061c5 100644 --- a/src/pakfire/mirrorlist.c +++ b/src/pakfire/mirrorlist.c @@ -22,6 +22,7 @@ #include #include +#include #include #include #include diff --git a/src/pakfire/package.c b/src/pakfire/package.c index 1426bae0..1dafb763 100644 --- a/src/pakfire/package.c +++ b/src/pakfire/package.c @@ -45,6 +45,7 @@ #include #include #include +#include #include #include #include diff --git a/src/pakfire/repo.c b/src/pakfire/repo.c index 2aa6cdb2..a5f98764 100644 --- a/src/pakfire/repo.c +++ b/src/pakfire/repo.c @@ -39,6 +39,7 @@ #include #include #include +#include #include #include #include diff --git a/src/pakfire/util.c b/src/pakfire/util.c index 931a8974..3debaf2c 100644 --- a/src/pakfire/util.c +++ b/src/pakfire/util.c @@ -26,7 +26,6 @@ #include #include -#include #include #define PCRE2_CODE_UNIT_WIDTH 8 @@ -376,159 +375,6 @@ int __pakfire_which(struct pakfire* pakfire, char* path, const size_t length, return 0; } -// JSON Stuff - -struct json_object* pakfire_json_parse(struct pakfire_ctx* ctx, - const char* buffer, const size_t length) { - struct json_tokener* tokener = NULL; - struct json_object* json = NULL; - - // Create tokener - tokener = json_tokener_new(); - if (!tokener) { - ERROR(ctx, "Could not allocate JSON tokener: %m\n"); - goto ERROR; - } - - // Parse JSON from buffer - json = json_tokener_parse_ex(tokener, buffer, length); - if (!json) { - enum json_tokener_error error = json_tokener_get_error(tokener); - - ERROR(ctx, "JSON parsing error: %s\n", json_tokener_error_desc(error)); - goto ERROR; - } - - // Log what we have parsed - DEBUG(ctx, "Parsed JSON:\n%s\n", - json_object_to_json_string_ext(json, - JSON_C_TO_STRING_SPACED | JSON_C_TO_STRING_PRETTY) - ); - -ERROR: - if (tokener) - json_tokener_free(tokener); - - return json; -} - -static struct json_object* pakfire_json_parse_file(struct pakfire_ctx* ctx, FILE* f) { - struct json_object* json = NULL; - char* buffer = NULL; - size_t length = 0; - int r; - - // Map everything into memory - r = pakfire_mmap(fileno(f), &buffer, &length); - if (r) - goto ERROR; - - // Parse - json = pakfire_json_parse(ctx, buffer, length); - -ERROR: - if (buffer) - munmap(buffer, length); - - return json; -} - -struct json_object* pakfire_json_parse_from_file(struct pakfire_ctx* ctx, const char* path) { - FILE* f = fopen(path, "r"); - if (!f) - return NULL; - - struct json_object* json = pakfire_json_parse_file(ctx, f); - fclose(f); - - return json; -} - -int pakfire_json_add_string(struct json_object* json, - const char* name, const char* value) { - if (!value) - return 0; - - return pakfire_json_add_stringn(json, name, value, strlen(value)); -} - -int pakfire_json_add_stringn(struct json_object* json, - const char* name, const char* value, size_t length) { - // No string? Nothing to do - if (!value) - return 0; - - // Convert string to JSON object - struct json_object* object = json_object_new_string_len(value, length); - if (!object) - return 1; - - // Add the object - return json_object_object_add(json, name, object); -} - -int pakfire_json_add_string_array(struct json_object* json, const char* name, char** array) { - int r = 1; - - // Allocate a new array - struct json_object* object = json_object_new_array(); - if (!object) - goto ERROR; - - // Add all items on list to the array - for (char** item = array; *item; item++) { - r = json_object_array_add(object, json_object_new_string(*item)); - if (r) - goto ERROR; - } - - // Add object - r = json_object_object_add(json, name, object); - if (r) - goto ERROR; - -ERROR: - // Free JSON object on error - if (r) - json_object_put(object); - - return r; -} - -int pakfire_json_add_int64(struct json_object* json, const char* name, int64_t value) { - // Convert integer to JSON object - struct json_object* object = json_object_new_int64(value); - if (!object) - return -errno; - - // Add the object - return json_object_object_add(json, name, object); -} - -int pakfire_json_add_uint64(struct json_object* json, const char* name, uint64_t value) { - struct json_object* object = NULL; - - // Convert the value to JSON - object = json_object_new_uint64(value); - if (!object) - return -errno; - - // Add the object - return json_object_object_add(json, name, object); -} - -int pakfire_json_add_double(struct json_object* json, const char* name, double value) { - struct json_object* object = NULL; - - // Convert the value to JSON - object = json_object_new_double(value); - if (!object) - return -errno; - - // Add the object - return json_object_object_add(json, name, object); -} - // Resource Limits int pakfire_rlimit_set(struct pakfire_ctx* ctx, int limit) { diff --git a/src/pakfire/util.h b/src/pakfire/util.h index 8de8827d..62693bdd 100644 --- a/src/pakfire/util.h +++ b/src/pakfire/util.h @@ -77,18 +77,6 @@ int __pakfire_which(struct pakfire* pakfire, char* path, const size_t length, co int pakfire_uuid_is_valid(const char* s); char* pakfire_generate_uuid(void); -// JSON Stuff - -struct json_object* pakfire_json_parse(struct pakfire_ctx* ctx, - const char* buffer, const size_t length); -struct json_object* pakfire_json_parse_from_file(struct pakfire_ctx* ctx, const char* path); -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); -int pakfire_json_add_int64(struct json_object* json, const char* name, int64_t value); -int pakfire_json_add_uint64(struct json_object* json, const char* name, uint64_t value); -int pakfire_json_add_double(struct json_object* json, const char* name, double value); -int pakfire_json_add_string_array(struct json_object* json, const char* name, char** array); - // Resource Limits #define PAKFIRE_RLIMIT_NOFILE_MAX (512 * 1024) diff --git a/src/pakfire/xfer.c b/src/pakfire/xfer.c index b22a151a..4bbb2c8b 100644 --- a/src/pakfire/xfer.c +++ b/src/pakfire/xfer.c @@ -34,6 +34,7 @@ #include #include +#include #include #include #include -- 2.39.5