From ee09056908bc925d018a5466629872434ac3edf6 Mon Sep 17 00:00:00 2001 From: OpenGnSys Support Team Date: Tue, 12 Dec 2023 16:49:25 +0100 Subject: client: parse permissions and lastupdate in /image/create response from client Use permissions and last update from client. update src/schema.c to add a new database version. --- src/client.c | 16 +++++++++++----- src/dbi.h | 4 +++- src/rest.c | 30 ++++++++---------------------- src/schema.c | 32 ++++++++++++++++++++++++++++++++ 4 files changed, 54 insertions(+), 28 deletions(-) diff --git a/src/client.c b/src/client.c index f2eb865..93bd8f8 100644 --- a/src/client.c +++ b/src/client.c @@ -500,7 +500,7 @@ static int og_resp_refresh(json_t *data, struct og_client *cli) static int update_image_info(struct og_dbi *dbi, const char *image_id, const char *clonator, const char *compressor, const char *filesystem, const uint64_t datasize, - uint64_t size) + uint64_t size, uint64_t lastupdate, uint32_t perms) { const char *msglog; dbi_result result; @@ -508,9 +508,10 @@ static int update_image_info(struct og_dbi *dbi, const char *image_id, result = dbi_conn_queryf(dbi->conn, "UPDATE imagenes" " SET clonator='%s', compressor='%s'," - " filesystem='%s', datasize=%lld" + " filesystem='%s', datasize=%lld, " + " size=%lld, lastupdate=%lld, permissions=%u " " WHERE idimagen=%s", clonator, compressor, filesystem, - datasize, image_id); + datasize, size, lastupdate, perms, image_id); if (!result) { dbi_conn_error(dbi->conn, &msglog); @@ -525,10 +526,10 @@ static int update_image_info(struct og_dbi *dbi, const char *image_id, static int og_resp_image_create(json_t *data, struct og_client *cli) { + uint64_t datasize = 0, size = 0, lastupdate = 0; struct og_software_legacy soft_legacy; struct og_image_legacy img_legacy; struct og_computer computer = {}; - uint64_t datasize = 0, size = 0; const char *compressor = NULL; const char *filesystem = NULL; const char *partition = NULL; @@ -539,6 +540,7 @@ static int og_resp_image_create(json_t *data, struct og_client *cli) const char *code = NULL; const char *name = NULL; const char *repo = NULL; + uint32_t perms = 0; struct og_dbi *dbi; const char *key; json_t *value; @@ -573,6 +575,10 @@ static int og_resp_image_create(json_t *data, struct og_client *cli) err = og_json_parse_uint64(value, &datasize); else if (!strcmp(key, "size")) err = og_json_parse_uint64(value, &size); + else if (!strcmp(key, "lastupdate")) + err = og_json_parse_uint64(value, &lastupdate); + else if (!strcmp(key, "perms")) + err = og_json_parse_uint(value, &perms); if (err < 0) return err; @@ -636,7 +642,7 @@ static int og_resp_image_create(json_t *data, struct og_client *cli) } res = update_image_info(dbi, image_id, clonator, compressor, - filesystem, datasize, size); + filesystem, datasize, size, lastupdate, perms); og_dbi_close(dbi); if (res) { diff --git a/src/dbi.h b/src/dbi.h index 7e0757c..feac3fb 100644 --- a/src/dbi.h +++ b/src/dbi.h @@ -57,11 +57,13 @@ struct og_image { uint64_t software_id; uint64_t center_id; uint64_t datasize; + uint64_t size; + long int lastupdate; uint64_t group_id; uint64_t repo_id; uint64_t type; uint64_t id; - struct stat image_stats; + uint32_t perms; }; struct og_legacy_partition { diff --git a/src/rest.c b/src/rest.c index 84018fb..0b252e2 100644 --- a/src/rest.c +++ b/src/rest.c @@ -2424,20 +2424,6 @@ static const int og_cmd_get_repositories(char *buffer_reply) #define OG_IMAGE_TYPE_MAXLEN 4 -static int og_get_image_stats(const char *name, - struct stat *image_stats) -{ - const char *dir = ogconfig.repo.dir; - char filename[PATH_MAX + 1]; - - snprintf(filename, sizeof(filename), "%s/%s.img", dir, name); - if (stat(filename, image_stats) < 0) { - syslog(LOG_ERR, "%s image does not exists", name); - return -1; - } - return 0; -} - static json_t *og_json_disk_alloc() { const char *dir = ogconfig.repo.dir; @@ -2469,16 +2455,14 @@ static json_t *og_json_image_alloc(struct og_image *image) char perms_string[OG_PERMS_MAXLEN]; json_t *image_json; char *modified; - uint16_t perms; image_json = json_object(); if (!image_json) return NULL; - perms = image->image_stats.st_mode & OG_PERMS_IRWX; - snprintf(perms_string, sizeof(perms_string), "%o", perms); + snprintf(perms_string, sizeof(perms_string), "%o", image->perms); - modified = ctime(&image->image_stats.st_mtime); + modified = ctime(&image->lastupdate); modified[strlen(modified) - 1] = '\0'; json_object_set_new(image_json, "name", @@ -2486,7 +2470,7 @@ static json_t *og_json_image_alloc(struct og_image *image) json_object_set_new(image_json, "datasize", json_integer(image->datasize)); json_object_set_new(image_json, "size", - json_integer(image->image_stats.st_size)); + json_integer(image->size)); json_object_set_new(image_json, "modified", json_string(modified)); json_object_set_new(image_json, "permissions", @@ -2538,7 +2522,8 @@ static int og_cmd_images(char *buffer_reply) result = dbi_conn_queryf(dbi->conn, "SELECT i.nombreca, o.nombreordenador, " " i.clonator, i.compressor, " - " i.filesystem, i.datasize, " + " i.filesystem, i.datasize, i.size, " + " i.lastupdate, i.permissions, " " i.idperfilsoft, i.tipo, " " i.idimagen, i.idrepositorio, " " i.descripcion " @@ -2549,6 +2534,9 @@ static int og_cmd_images(char *buffer_reply) while (dbi_result_next_row(result)) { image = (struct og_image){0}; image.datasize = dbi_result_get_ulonglong(result, "datasize"); + image.size = dbi_result_get_ulonglong(result, "size"); + image.lastupdate = dbi_result_get_ulonglong(result, "lastupdate"); + image.perms = dbi_result_get_uint(result, "permissions"); image.software_id = dbi_result_get_ulonglong(result, "idperfilsoft"); image.type = dbi_result_get_ulonglong(result, "tipo"); image.id = dbi_result_get_ulonglong(result, "idimagen"); @@ -2558,8 +2546,6 @@ static int og_cmd_images(char *buffer_reply) snprintf(image.description, sizeof(image.description), "%s", dbi_result_get_string(result, "descripcion")); - og_get_image_stats(image.name, &image.image_stats); - image_json = og_json_image_alloc(&image); if (!image_json) { dbi_result_free(result); diff --git a/src/schema.c b/src/schema.c index d31046e..f1fab5b 100644 --- a/src/schema.c +++ b/src/schema.c @@ -240,6 +240,37 @@ static int og_dbi_schema_v4(struct og_dbi *dbi) return 0; } +static int og_dbi_schema_v5(struct og_dbi *dbi) +{ + const char *msglog; + dbi_result result; + + syslog(LOG_DEBUG, "Adding size, lastupdate and permissions to imagenes\n"); + result = dbi_conn_query(dbi->conn, + "ALTER TABLE `imagenes` " + "ADD `size` bigint DEFAULT '0' AFTER `datasize`, " + "ADD `lastupdate` bigint DEFAULT '0' AFTER `size`, " + "ADD `permissions` int DEFAULT '0' AFTER `lastupdate`;"); + if (!result) { + dbi_conn_error(dbi->conn, &msglog); + syslog(LOG_INFO, "Error when adding identorno (%s:%d) %s\n", + __func__, __LINE__, msglog); + return -1; + } + dbi_result_free(result); + + result = dbi_conn_query(dbi->conn, "UPDATE version SET version = 5"); + if (!result) { + dbi_conn_error(dbi->conn, &msglog); + syslog(LOG_INFO, "Could not update version row (%s:%d) %s\n", + __func__, __LINE__, msglog); + return -1; + } + dbi_result_free(result); + + return 0; +} + static struct og_schema_version { int version; int (*update)(struct og_dbi *dbi); @@ -248,6 +279,7 @@ static struct og_schema_version { { .version = 2, .update = og_dbi_schema_v2 }, { .version = 3, .update = og_dbi_schema_v3 }, { .version = 4, .update = og_dbi_schema_v4 }, + { .version = 5, .update = og_dbi_schema_v5 }, { 0, NULL }, }; -- cgit v1.2.3-18-g5258