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.
master
OpenGnSys Support Team 2024-09-25 14:47:06 +02:00
parent 65aee8f3f3
commit 3c395ecea2
3 changed files with 32 additions and 24 deletions

View File

@ -407,7 +407,7 @@ static int og_json_parse_partition_array(json_t *value,
return 0; 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; struct og_cache_image *cache_image;
const char *msglog; 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); dbi_result_free(result);
/* Add new cache image info */ /* 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, result = dbi_conn_queryf(dbi->conn,
"INSERT INTO cache (clientid, imagename, size, checksum)" "INSERT INTO cache (clientid, imagename, size, checksum)"
"VALUES (%d, '%s', %lu, '%s')", "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_cache_data cache_data = {};
struct og_computer computer = {}; struct og_computer computer = {};
json_t *value, *cache = NULL; json_t *value, *cache = NULL;
LIST_HEAD(cache_list);
struct og_dbi *dbi; struct og_dbi *dbi;
const char *key; const char *key;
int err = 0; int err = 0;
og_cache_data_init(&cache_data);
if (json_typeof(data) != JSON_OBJECT) { if (json_typeof(data) != JSON_OBJECT) {
og_cache_image_free(&cache_list); og_cache_data_free(&cache_data);
return -1; return -1;
} }
json_object_foreach(data, key, value) { json_object_foreach(data, key, value) {
if (!strcmp(key, "cache")) { if (!strcmp(key, "cache")) {
err = og_json_parse_cache(value, &cache_data, &cache_list); err = og_json_parse_cache(value, &cache_data);
cache = value; cache = value;
} }
if (err < 0) { if (err < 0) {
og_cache_image_free(&cache_list); og_cache_data_free(&cache_data);
return err; return err;
} }
} }
if (!cache) { if (!cache) {
og_cache_image_free(&cache_list); og_cache_data_free(&cache_data);
return 0; return 0;
} }
dbi = og_dbi_open(&ogconfig.db); dbi = og_dbi_open(&ogconfig.db);
if (!dbi) { if (!dbi) {
og_cache_image_free(&cache_list); og_cache_data_free(&cache_data);
syslog(LOG_ERR, "cannot open connection database (%s:%d)\n", syslog(LOG_ERR, "cannot open connection database (%s:%d)\n",
__func__, __LINE__); __func__, __LINE__);
return -1; 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); err = og_dbi_get_computer_info(dbi, &computer, cli->addr.sin_addr);
if (err < 0) { if (err < 0) {
og_cache_image_free(&cache_list); og_cache_data_free(&cache_data);
og_dbi_close(dbi); og_dbi_close(dbi);
return -1; 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) { if (err < 0) {
og_cache_image_free(&cache_list); og_cache_data_free(&cache_data);
og_dbi_close(dbi); og_dbi_close(dbi);
return -1; return -1;
} }
og_cache_image_free(&cache_list); og_cache_data_free(&cache_data);
og_dbi_close(dbi); og_dbi_close(dbi);
return 0; return 0;
} }
@ -823,7 +824,6 @@ static int og_resp_refresh(json_t *data, struct og_client *cli)
struct og_computer computer = {}; struct og_computer computer = {};
const char *status = NULL; const char *status = NULL;
LIST_HEAD(boot_entry_list); LIST_HEAD(boot_entry_list);
LIST_HEAD(cache_list);
json_t *value = NULL; json_t *value = NULL;
struct og_dbi *dbi; struct og_dbi *dbi;
uint32_t link = 0; uint32_t link = 0;
@ -831,6 +831,8 @@ static int og_resp_refresh(json_t *data, struct og_client *cli)
int err = 0; int err = 0;
bool res; bool res;
og_cache_data_init(&cache_data);
if (json_typeof(data) != JSON_OBJECT) if (json_typeof(data) != JSON_OBJECT)
goto err_out; goto err_out;
@ -850,7 +852,7 @@ static int og_resp_refresh(json_t *data, struct og_client *cli)
} else if (!strcmp(key, "efi")) { } else if (!strcmp(key, "efi")) {
err = og_json_parse_efi(value, &boot_entry_list); err = og_json_parse_efi(value, &boot_entry_list);
} else if (!strcmp(key, "cache")) { } 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) if (err < 0)
@ -886,7 +888,7 @@ static int og_resp_refresh(json_t *data, struct og_client *cli)
goto err_out; 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); og_dbi_close(dbi);
goto err_out; goto err_out;
} }
@ -907,13 +909,13 @@ static int og_resp_refresh(json_t *data, struct og_client *cli)
goto err_out; goto err_out;
} }
og_cache_image_free(&cache_list); og_cache_data_free(&cache_data);
og_boot_entry_free(&boot_entry_list); og_boot_entry_free(&boot_entry_list);
return 0; return 0;
err_out: err_out:
syslog(LOG_ERR, "Failed to refresh info from client %s\n", syslog(LOG_ERR, "Failed to refresh info from client %s\n",
inet_ntoa(cli->addr.sin_addr)); 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); og_boot_entry_free(&boot_entry_list);
return -1; return -1;
} }

View File

@ -241,11 +241,16 @@ int og_json_parse_procedure(json_t *element, struct og_procedure *proc)
return err; 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; 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); list_del(&cache_item->list);
free(cache_item); free(cache_item);
} }
@ -308,7 +313,7 @@ static int og_json_parse_cache_images(json_t *element, struct list_head *cache_l
return err; 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 required_flags = OG_PARAM_CACHE_FREE_SIZE | OG_PARAM_CACHE_USED_SIZE | OG_PARAM_CACHE_IMAGES;
uint64_t flags = 0UL; 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); err = og_json_parse_uint64(value, &cache_data->free_size);
flags |= OG_PARAM_CACHE_FREE_SIZE; flags |= OG_PARAM_CACHE_FREE_SIZE;
} else if (!strcmp(key, "images")) { } 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; flags |= OG_PARAM_CACHE_IMAGES;
} else } else
err = -1; err = -1;

View File

@ -138,11 +138,12 @@ struct og_cache_image {
struct og_cache_data { struct og_cache_data {
uint64_t used_size; uint64_t used_size;
uint64_t free_size; uint64_t free_size;
struct list_head image_list;
}; };
void og_cache_image_free(struct list_head *cache_list); void og_cache_data_init(struct og_cache_data *cache_data);
int og_json_parse_cache(json_t *element, struct og_cache_data *cache_data, struct list_head *cache_list); 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_ENTRIES (1UL << 0)
#define OG_PARAM_EFI_ORDER (1UL << 1) #define OG_PARAM_EFI_ORDER (1UL << 1)