diff options
Diffstat (limited to 'src/json.c')
-rw-r--r-- | src/json.c | 67 |
1 files changed, 67 insertions, 0 deletions
@@ -236,3 +236,70 @@ int og_json_parse_procedure(json_t *element, struct og_procedure *proc) return err; } + +void og_cache_image_free(struct list_head *cache_list) +{ + struct og_cache_image *cache_item, *tmp; + + list_for_each_entry_safe(cache_item, tmp, cache_list, list) { + list_del(&cache_item->list); + free(cache_item); + } +} + +int og_json_parse_cache(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; + json_t *json_elem, *value; + const char *key; + unsigned long i; + uint64_t flags; + int err = 0; + + if (json_typeof(element) != JSON_ARRAY) + return -1; + + for (i = 0; i < json_array_size(element); i++) { + cache_item = calloc(1, sizeof(struct og_cache_image)); + flags = 0UL; + + if (!cache_item) + return -1; + + json_elem = json_array_get(element, i); + + if (json_typeof(json_elem) != JSON_OBJECT) { + free(cache_item); + return -1; + } + + json_object_foreach(json_elem, key, value) { + if (!strcmp(key, "name")) { + err = og_json_parse_string(value, &cache_item->name); + flags |= OG_PARAM_IMG_NAME; + } else if (!strcmp(key, "size")) { + err = og_json_parse_uint64(value, &cache_item->size); + flags |= OG_PARAM_IMG_SIZE; + } else if (!strcmp(key, "checksum")) { + err = og_json_parse_string(value, &cache_item->checksum); + flags |= OG_PARAM_IMG_CHECKSUM; + } else + err = -1; + + if (err < 0) { + free(cache_item); + return err; + } + } + + if ((flags & required_flags) != required_flags) { + free(cache_item); + return -1; + } + + list_add_tail(&cache_item->list, cache_list); + } + + return err; +} |