From 3c395ecea2bb538cf5182a64df503329a0a1cbcc Mon Sep 17 00:00:00 2001 From: OpenGnSys Support Team Date: Wed, 25 Sep 2024 14:47:06 +0200 Subject: client: move image list to cache data to consolidate it Move list of images in the cache to cache_data. Add new functions to initialize cache data (image list) and release it. --- src/client.c | 36 +++++++++++++++++++----------------- src/json.c | 13 +++++++++---- src/json.h | 7 ++++--- 3 files changed, 32 insertions(+), 24 deletions(-) (limited to 'src') diff --git a/src/client.c b/src/client.c index bbe4c15..6e568b5 100644 --- a/src/client.c +++ b/src/client.c @@ -407,7 +407,7 @@ static int og_json_parse_partition_array(json_t *value, return 0; } -static int og_update_cache_info(struct og_dbi *dbi, struct og_cache_data *cache_data, struct list_head *cache_list, int clientid) +static int og_update_cache_info(struct og_dbi *dbi, struct og_cache_data *cache_data, int clientid) { struct og_cache_image *cache_image; const char *msglog; @@ -426,7 +426,7 @@ static int og_update_cache_info(struct og_dbi *dbi, struct og_cache_data *cache_ dbi_result_free(result); /* Add new cache image info */ - list_for_each_entry(cache_image, cache_list, list) { + list_for_each_entry(cache_image, &cache_data->image_list, list) { result = dbi_conn_queryf(dbi->conn, "INSERT INTO cache (clientid, imagename, size, checksum)" "VALUES (%d, '%s', %lu, '%s')", @@ -508,36 +508,37 @@ static int og_resp_update_cache(json_t *data, struct og_client *cli) struct og_cache_data cache_data = {}; struct og_computer computer = {}; json_t *value, *cache = NULL; - LIST_HEAD(cache_list); struct og_dbi *dbi; const char *key; int err = 0; + og_cache_data_init(&cache_data); + if (json_typeof(data) != JSON_OBJECT) { - og_cache_image_free(&cache_list); + og_cache_data_free(&cache_data); return -1; } json_object_foreach(data, key, value) { if (!strcmp(key, "cache")) { - err = og_json_parse_cache(value, &cache_data, &cache_list); + err = og_json_parse_cache(value, &cache_data); cache = value; } if (err < 0) { - og_cache_image_free(&cache_list); + og_cache_data_free(&cache_data); return err; } } if (!cache) { - og_cache_image_free(&cache_list); + og_cache_data_free(&cache_data); return 0; } dbi = og_dbi_open(&ogconfig.db); if (!dbi) { - og_cache_image_free(&cache_list); + og_cache_data_free(&cache_data); syslog(LOG_ERR, "cannot open connection database (%s:%d)\n", __func__, __LINE__); return -1; @@ -545,19 +546,19 @@ static int og_resp_update_cache(json_t *data, struct og_client *cli) err = og_dbi_get_computer_info(dbi, &computer, cli->addr.sin_addr); if (err < 0) { - og_cache_image_free(&cache_list); + og_cache_data_free(&cache_data); og_dbi_close(dbi); return -1; } - err = og_update_cache_info(dbi, &cache_data, &cache_list, computer.id); + err = og_update_cache_info(dbi, &cache_data, computer.id); if (err < 0) { - og_cache_image_free(&cache_list); + og_cache_data_free(&cache_data); og_dbi_close(dbi); return -1; } - og_cache_image_free(&cache_list); + og_cache_data_free(&cache_data); og_dbi_close(dbi); return 0; } @@ -823,7 +824,6 @@ static int og_resp_refresh(json_t *data, struct og_client *cli) struct og_computer computer = {}; const char *status = NULL; LIST_HEAD(boot_entry_list); - LIST_HEAD(cache_list); json_t *value = NULL; struct og_dbi *dbi; uint32_t link = 0; @@ -831,6 +831,8 @@ static int og_resp_refresh(json_t *data, struct og_client *cli) int err = 0; bool res; + og_cache_data_init(&cache_data); + if (json_typeof(data) != JSON_OBJECT) goto err_out; @@ -850,7 +852,7 @@ static int og_resp_refresh(json_t *data, struct og_client *cli) } else if (!strcmp(key, "efi")) { err = og_json_parse_efi(value, &boot_entry_list); } else if (!strcmp(key, "cache")) { - err = og_json_parse_cache(value, &cache_data, &cache_list); + err = og_json_parse_cache(value, &cache_data); } if (err < 0) @@ -886,7 +888,7 @@ static int og_resp_refresh(json_t *data, struct og_client *cli) goto err_out; } - if (og_update_cache_info(dbi, &cache_data, &cache_list, computer.id) < 0) { + if (og_update_cache_info(dbi, &cache_data, computer.id) < 0) { og_dbi_close(dbi); goto err_out; } @@ -907,13 +909,13 @@ static int og_resp_refresh(json_t *data, struct og_client *cli) goto err_out; } - og_cache_image_free(&cache_list); + og_cache_data_free(&cache_data); og_boot_entry_free(&boot_entry_list); return 0; err_out: syslog(LOG_ERR, "Failed to refresh info from client %s\n", inet_ntoa(cli->addr.sin_addr)); - og_cache_image_free(&cache_list); + og_cache_data_free(&cache_data); og_boot_entry_free(&boot_entry_list); return -1; } diff --git a/src/json.c b/src/json.c index 05b725c..501bcca 100644 --- a/src/json.c +++ b/src/json.c @@ -241,11 +241,16 @@ int og_json_parse_procedure(json_t *element, struct og_procedure *proc) return err; } -void og_cache_image_free(struct list_head *cache_list) +void og_cache_data_init(struct og_cache_data *cache_data) +{ + INIT_LIST_HEAD(&cache_data->image_list); +} + +void og_cache_data_free(struct og_cache_data *cache_data) { struct og_cache_image *cache_item, *tmp; - list_for_each_entry_safe(cache_item, tmp, cache_list, list) { + list_for_each_entry_safe(cache_item, tmp, &cache_data->image_list, list) { list_del(&cache_item->list); free(cache_item); } @@ -308,7 +313,7 @@ static int og_json_parse_cache_images(json_t *element, struct list_head *cache_l return err; } -int og_json_parse_cache(json_t *element, struct og_cache_data *cache_data, struct list_head *cache_list) +int og_json_parse_cache(json_t *element, struct og_cache_data *cache_data) { uint64_t required_flags = OG_PARAM_CACHE_FREE_SIZE | OG_PARAM_CACHE_USED_SIZE | OG_PARAM_CACHE_IMAGES; uint64_t flags = 0UL; @@ -330,7 +335,7 @@ int og_json_parse_cache(json_t *element, struct og_cache_data *cache_data, struc err = og_json_parse_uint64(value, &cache_data->free_size); flags |= OG_PARAM_CACHE_FREE_SIZE; } else if (!strcmp(key, "images")) { - err = og_json_parse_cache_images(value, cache_list); + err = og_json_parse_cache_images(value, &cache_data->image_list); flags |= OG_PARAM_CACHE_IMAGES; } else err = -1; diff --git a/src/json.h b/src/json.h index cc08d9d..1d711dd 100644 --- a/src/json.h +++ b/src/json.h @@ -138,11 +138,12 @@ struct og_cache_image { struct og_cache_data { uint64_t used_size; uint64_t free_size; + struct list_head image_list; }; -void og_cache_image_free(struct list_head *cache_list); -int og_json_parse_cache(json_t *element, struct og_cache_data *cache_data, struct list_head *cache_list); - +void og_cache_data_init(struct og_cache_data *cache_data); +void og_cache_data_free(struct og_cache_data *cache_data); +int og_json_parse_cache(json_t *element, struct og_cache_data *cache_data); #define OG_PARAM_EFI_ENTRIES (1UL << 0) #define OG_PARAM_EFI_ORDER (1UL << 1) -- cgit v1.2.3-18-g5258