From 97e3d220503a676acd81839dea289f4e700d8bd8 Mon Sep 17 00:00:00 2001 From: Daniel Baumann Date: Sat, 1 Jul 2023 00:36:07 +0200 Subject: Adding upstream version 1.5. Signed-off-by: Daniel Baumann --- src/nvme/json.c | 40 +++++++++++++++++++++++++++------------- 1 file changed, 27 insertions(+), 13 deletions(-) (limited to 'src/nvme/json.c') diff --git a/src/nvme/json.c b/src/nvme/json.c index a74b5a4..7a5a69e 100644 --- a/src/nvme/json.c +++ b/src/nvme/json.c @@ -127,7 +127,7 @@ static void json_parse_port(nvme_subsystem_t s, struct json_object *port_obj) static void json_parse_subsys(nvme_host_t h, struct json_object *subsys_obj) { - struct json_object *nqn_obj, *port_array; + struct json_object *nqn_obj, *app_obj, *port_array; nvme_subsystem_t s; const char *nqn; int p; @@ -137,6 +137,12 @@ static void json_parse_subsys(nvme_host_t h, struct json_object *subsys_obj) return; nqn = json_object_get_string(nqn_obj); s = nvme_lookup_subsystem(h, NULL, nqn); + if (!s) + return; + app_obj = json_object_object_get(subsys_obj, "application"); + if (app_obj) + nvme_subsystem_set_application(s, json_object_get_string(app_obj)); + port_array = json_object_object_get(subsys_obj, "ports"); if (!port_array) return; @@ -187,18 +193,22 @@ static struct json_object *parse_json(nvme_root_t r, int fd) { char buf[JSON_FILE_BUF_SIZE]; struct json_object *obj = NULL; - struct printbuf *pb; + char *str = NULL; json_tokener *tok = NULL; int ret; + void *ptr = NULL; + int len = 0; + + while ((ret = read(fd, buf, JSON_FILE_BUF_SIZE)) > 0) { + str = realloc(ptr, len + ret); + if (!str) + goto out; + memcpy(&str[len], buf, ret); + len += ret; + ptr = str; + } - pb = printbuf_new(); - if (!pb) - return NULL; - - while ((ret = read(fd, buf, JSON_FILE_BUF_SIZE)) > 0) - printbuf_memappend(pb, buf, ret); - - if (ret < 0) + if (ret < 0 || !len) goto out; tok = json_tokener_new_ex(JSON_TOKENER_DEFAULT_DEPTH); @@ -208,14 +218,14 @@ static struct json_object *parse_json(nvme_root_t r, int fd) /* Enforce correctly formatted JSON */ tok->flags = JSON_TOKENER_STRICT; - obj = json_tokener_parse_ex(tok, pb->buf, printbuf_length(pb)); + obj = json_tokener_parse_ex(tok, str, len); if (!obj) nvme_msg(r, LOG_DEBUG, "JSON parsing failed: %s\n", json_util_get_last_err()); out: if (tok) json_tokener_free(tok); - printbuf_free(pb); + free(ptr); return obj; } @@ -350,7 +360,7 @@ static void json_update_subsys(struct json_object *subsys_array, nvme_subsystem_t s) { nvme_ctrl_t c; - const char *subsysnqn = nvme_subsystem_get_nqn(s); + const char *subsysnqn = nvme_subsystem_get_nqn(s), *app; struct json_object *subsys_obj = json_object_new_object(); struct json_object *port_array; @@ -360,6 +370,10 @@ static void json_update_subsys(struct json_object *subsys_array, json_object_object_add(subsys_obj, "nqn", json_object_new_string(subsysnqn)); + app = nvme_subsystem_get_application(s); + if (app) + json_object_object_add(subsys_obj, "application", + json_object_new_string(app)); port_array = json_object_new_array(); nvme_subsystem_for_each_ctrl(s, c) { json_update_port(port_array, c); -- cgit v1.2.3