From 730d42a872ca48d64d10f3a9c0b668795300dfa0 Mon Sep 17 00:00:00 2001 From: OpenGnSys Support Team Date: Thu, 13 Feb 2025 12:45:32 +0100 Subject: [PATCH] src: handle refresh response after image restore use context to store disk, partition and image id that is requested to client and handle refresh response from ogclient to update database, otherwise, an explicit refresh from web frontend is required. --- src/client.c | 45 +++++++++++++++------------------------------ src/rest.c | 25 ++++++++++++++++++++++++- src/rest.h | 5 +++++ 3 files changed, 44 insertions(+), 31 deletions(-) diff --git a/src/client.c b/src/client.c index 9b1ec71..9187659 100644 --- a/src/client.c +++ b/src/client.c @@ -1154,37 +1154,17 @@ static int og_resp_image_create(json_t *data, struct og_client *cli) static int og_resp_image_restore(json_t *data, struct og_client *cli) { struct og_software_legacy soft_legacy; + uint32_t disk, partition, image_id; struct og_image_legacy img_legacy; struct og_computer computer = {}; - const char *partition = NULL; - const char *image_id = NULL; - const char *disk = NULL; const char *msglog; struct og_dbi *dbi; dbi_result result; - const char *key; - json_t *value; int err = 0; - if (json_typeof(data) != JSON_OBJECT) - return -1; - - json_object_foreach(data, key, value) { - if (!strcmp(key, "partition")) - err = og_json_parse_string(value, &partition); - else if (!strcmp(key, "disk")) - err = og_json_parse_string(value, &disk); - else if (!strcmp(key, "image_id")) - err = og_json_parse_string(value, &image_id); - - if (err < 0) - return err; - } - - if (!partition || !disk || !image_id) { - syslog(LOG_ERR, "malformed response json\n"); - return -1; - } + err = og_resp_refresh(data, cli); + if (err < 0) + return err; dbi = og_dbi_open(&ogconfig.db); if (!dbi) { @@ -1193,9 +1173,13 @@ static int og_resp_image_restore(json_t *data, struct og_client *cli) return -1; } + image_id = cli->last_cmd.ctx.image_restore.id; + partition = cli->last_cmd.ctx.image_restore.part; + disk = cli->last_cmd.ctx.image_restore.disk; + result = dbi_conn_queryf(dbi->conn, "SELECT idperfilsoft FROM imagenes " - " WHERE idimagen='%s'", image_id); + " WHERE idimagen='%u'", image_id); if (!result) { og_dbi_close(dbi); syslog(LOG_ERR, "failed to query database\n"); @@ -1204,7 +1188,8 @@ static int og_resp_image_restore(json_t *data, struct og_client *cli) if (!dbi_result_next_row(result)) { dbi_result_free(result); og_dbi_close(dbi); - syslog(LOG_ERR, "software profile does not exist in database\n"); + syslog(LOG_ERR, "software profile for image id %u does not exist in database\n", + image_id); return -1; } snprintf(img_legacy.software_id, sizeof(img_legacy.software_id), @@ -1217,11 +1202,11 @@ static int og_resp_image_restore(json_t *data, struct og_client *cli) return -1; } - snprintf(img_legacy.image_id, sizeof(img_legacy.image_id), "%s", + snprintf(img_legacy.image_id, sizeof(img_legacy.image_id), "%u", image_id); - snprintf(img_legacy.part, sizeof(img_legacy.part), "%s", partition); - snprintf(img_legacy.disk, sizeof(img_legacy.disk), "%s", disk); - snprintf(soft_legacy.id, sizeof(soft_legacy.id), "%d", computer.id); + snprintf(img_legacy.part, sizeof(img_legacy.part), "%u", partition); + snprintf(img_legacy.disk, sizeof(img_legacy.disk), "%u", disk); + snprintf(soft_legacy.id, sizeof(soft_legacy.id), "%u", computer.id); result = dbi_conn_queryf(dbi->conn, "UPDATE ordenadores_particiones" diff --git a/src/rest.c b/src/rest.c index 6c43a2f..f036bfc 100644 --- a/src/rest.c +++ b/src/rest.c @@ -3981,6 +3981,8 @@ static int og_cmd_restore_image(struct og_rest_ctx *ctx) { char repository_ip[OG_DB_IP_MAXLEN + 1] = {}; struct og_msg_params *params = &ctx->params; + struct og_cmd_ctx cmd_ctx = {}; + uint64_t image_id, disk, part; struct og_dbi *dbi; json_t *body; int err; @@ -4014,12 +4016,33 @@ static int og_cmd_restore_image(struct og_rest_ctx *ctx) if (err < 0) return err; + if (safe_strtoull(params->id, &image_id, 10, UINT32_MAX) < 0) { + syslog(LOG_ERR, "failed to image id %s (%s:%d)\n", + params->id, __func__, __LINE__); + return -1; + } + cmd_ctx.image_restore.id = image_id; + + if (safe_strtoull(params->disk, &disk, 10, UINT32_MAX) < 0) { + syslog(LOG_ERR, "failed to parse disk %s (%s:%d)\n", + params->disk, __func__, __LINE__); + return -1; + } + cmd_ctx.image_restore.disk = disk; + + if (safe_strtoull(params->partition, &part, 10, UINT32_MAX) < 0) { + syslog(LOG_ERR, "failed to parse partition %s (%s:%d)\n", + params->partition, __func__, __LINE__); + return -1; + } + cmd_ctx.image_restore.part = part; + body = json_copy(ctx->json); json_object_del(body, "clients"); json_object_set_new(body, "repository", json_string(repository_ip)); return og_send_request(OG_METHOD_POST, OG_CMD_IMAGE_RESTORE, params, - body, NULL); + body, &cmd_ctx); } static int og_cmd_delete_image(struct og_rest_ctx *ctx) diff --git a/src/rest.h b/src/rest.h index b41aa2c..ff3454a 100644 --- a/src/rest.h +++ b/src/rest.h @@ -56,6 +56,11 @@ struct og_cmd_ctx { struct { uint32_t id; } image; + struct { + uint32_t id; + uint32_t disk; + uint32_t part; + } image_restore; }; };