#include <sys/mman.h>
#include <sys/resource.h>
-#include <json.h>
#include <uuid/uuid.h>
#define PCRE2_CODE_UNIT_WIDTH 8
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) {
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)