From c7bfc370ae8974302cf1e316b3e03d0e6e960e0c Mon Sep 17 00:00:00 2001 From: Alejandro Sirgo Rica Date: Tue, 24 Sep 2024 13:38:34 +0200 Subject: src: add used_size and free_size to partition data Extend database table ordenadores_particiones to add new "used_size" and "free_size" fields. FIELD TYPE | tamano | bigint | | uso | tinyint | | used_size | bigint | | free_size | bigint | "tamano" is the field storing the total size of the partition. "uso" is a field storing the integer percentage of use, it is preserved for backwards compatibility with scritps that access the database. "used_size" and "free_size" contain the used and free partition size in bytes. Old response from ogClient for /cache/delete, /cache/fetch and /image/restore: { 'cache': [ {'name': 'windows.img', 'size': 2432370213, checksum: '5d4dcc677bc19f40a647d0002f4ade90'}, {'name': 'linux.img', 'size': 243234534213, checksum: '3eb22f888f88a55ad954f55644e1192e'} ] } New response: { 'cache': { 'used_size': 4520232322423, 'free_size': 48273465287452945, 'images': [ {'name': 'windows.img', 'size': 2432370213, checksum: '5d4dcc677bc19f40a647d0002f4ade90'}, {'name': 'linux.img', 'size': 243234534213, checksum: '3eb22f888f88a55ad954f55644e1192e'} ] } } Parse the new "free_size" and "used_size" fields of each partition data in the response payload of /refresh Parse "free_size" and "used_size" fields of the cache data in the reponse payload of /image/restore, /cache/delete and /cache/fetch Replace "used_size" field of GET /client/setup with the value of the new database field "used_size" --- src/json.c | 45 +++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 43 insertions(+), 2 deletions(-) (limited to 'src/json.c') diff --git a/src/json.c b/src/json.c index d33c647..05b725c 100644 --- a/src/json.c +++ b/src/json.c @@ -128,8 +128,11 @@ int og_json_parse_partition(json_t *element, struct og_partition *part, err = og_json_parse_string(value, &part->os); flags |= OG_PARAM_PART_OS; } else if (!strcmp(key, "used_size")) { - err = og_json_parse_string(value, &part->used_size); + err = og_json_parse_uint64(value, &part->used_size); flags |= OG_PARAM_PART_USED_SIZE; + } else if (!strcmp(key, "free_size")) { + err = og_json_parse_uint64(value, &part->free_size); + flags |= OG_PARAM_PART_FREE_SIZE; } if (err < 0) @@ -248,7 +251,7 @@ void og_cache_image_free(struct list_head *cache_list) } } -int og_json_parse_cache(json_t *element, struct list_head *cache_list) +static int og_json_parse_cache_images(json_t *element, struct list_head *cache_list) { uint64_t required_flags = OG_PARAM_IMG_NAME | OG_PARAM_IMG_SIZE | OG_PARAM_IMG_CHECKSUM; struct og_cache_image *cache_item; @@ -305,6 +308,44 @@ int og_json_parse_cache(json_t *element, struct list_head *cache_list) return err; } +int og_json_parse_cache(json_t *element, struct og_cache_data *cache_data, struct list_head *cache_list) +{ + uint64_t required_flags = OG_PARAM_CACHE_FREE_SIZE | OG_PARAM_CACHE_USED_SIZE | OG_PARAM_CACHE_IMAGES; + uint64_t flags = 0UL; + const char *key; + json_t *value; + int err = 0; + + if (json_typeof(element) != JSON_OBJECT) + return -1; + + if (json_object_size(element) == 0) + return 0; + + json_object_foreach(element, key, value) { + if (!strcmp(key, "used_size")) { + err = og_json_parse_uint64(value, &cache_data->used_size); + flags |= OG_PARAM_CACHE_USED_SIZE; + } else if (!strcmp(key, "free_size")) { + 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); + flags |= OG_PARAM_CACHE_IMAGES; + } else + err = -1; + + if (err < 0) { + return err; + } + } + + if ((flags & required_flags) != required_flags) + return -1; + + return err; +} + void og_boot_entry_free(struct list_head *boot_entry_list) { struct og_boot_entry *boot_entry, *tmp; -- cgit v1.2.3-18-g5258