]> git.ipfire.org Git - pakfire.git/commitdiff
json: Move functions into a separate file
authorMichael Tremer <michael.tremer@ipfire.org>
Sun, 2 Feb 2025 13:42:15 +0000 (13:42 +0000)
committerMichael Tremer <michael.tremer@ipfire.org>
Sun, 2 Feb 2025 13:42:15 +0000 (13:42 +0000)
Signed-off-by: Michael Tremer <michael.tremer@ipfire.org>
Makefile.am
src/pakfire/daemon.c
src/pakfire/job.c
src/pakfire/mirrorlist.c
src/pakfire/package.c
src/pakfire/repo.c
src/pakfire/util.c
src/pakfire/util.h
src/pakfire/xfer.c

index 4f242028753f8863ff1bbb462b9a9f9db9758372..b730e18e780174c5f6fde33cf8159bd18d89805b 100644 (file)
@@ -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 \
index 94a3307e9e279a91f246ad7ff9b93a51e6dfeaaa..b5f6eab43d651bb3b11d76ab22fb0ff3c8be7e0b 100644 (file)
@@ -35,6 +35,7 @@
 #include <pakfire/daemon.h>
 #include <pakfire/httpclient.h>
 #include <pakfire/job.h>
+#include <pakfire/json.h>
 #include <pakfire/string.h>
 #include <pakfire/util.h>
 
index 642c8422580f577a4471c8888b53cd041bb44411..3335426a4d39e4b05c6894f62d5d6b269d5f3864 100644 (file)
@@ -38,6 +38,7 @@
 #include <pakfire/ctx.h>
 #include <pakfire/daemon.h>
 #include <pakfire/job.h>
+#include <pakfire/json.h>
 #include <pakfire/log_file.h>
 #include <pakfire/log_buffer.h>
 #include <pakfire/log_stream.h>
index b1da8bbaef0d2be136d66bee74a814e30d8649f1..801061c5a8c39260eb6509dcc6a7532e505d75db 100644 (file)
@@ -22,6 +22,7 @@
 #include <stdlib.h>
 
 #include <pakfire/ctx.h>
+#include <pakfire/json.h>
 #include <pakfire/logging.h>
 #include <pakfire/mirror.h>
 #include <pakfire/mirrorlist.h>
index 1426bae09599061b23087e424a9692ed959f7d12..1dafb763615123eb3d81ac55ef95be8eff9b9e94 100644 (file)
@@ -45,6 +45,7 @@
 #include <pakfire/filelist.h>
 #include <pakfire/hex.h>
 #include <pakfire/i18n.h>
+#include <pakfire/json.h>
 #include <pakfire/logging.h>
 #include <pakfire/package.h>
 #include <pakfire/pakfire.h>
index 2aa6cdb222ea08ae6b18a22afa2f458cea5471ba..a5f98764290aa04b2d73cea6d482650d7d7186b7 100644 (file)
@@ -39,6 +39,7 @@
 #include <pakfire/constants.h>
 #include <pakfire/ctx.h>
 #include <pakfire/i18n.h>
+#include <pakfire/json.h>
 #include <pakfire/logging.h>
 #include <pakfire/package.h>
 #include <pakfire/pakfire.h>
index 931a8974db14350977a1798243d3a370fd48e296..3debaf2c83e00c42814da001bed4a164883ba551 100644 (file)
@@ -26,7 +26,6 @@
 #include <sys/mman.h>
 #include <sys/resource.h>
 
-#include <json.h>
 #include <uuid/uuid.h>
 
 #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) {
index 8de8827d033afbc1b867db5658e8905a9225e744..62693bddeddfcccf1114b859edf0e6020f739f4a 100644 (file)
@@ -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)
index b22a151a27f7de32ed12c73d31ea674be925ec76..4bbb2c8b5a4effff0734996e5db6b3da65f3b8c9 100644 (file)
@@ -34,6 +34,7 @@
 
 #include <pakfire/ctx.h>
 #include <pakfire/hex.h>
+#include <pakfire/json.h>
 #include <pakfire/mirrorlist.h>
 #include <pakfire/path.h>
 #include <pakfire/progress.h>