From: pcarana Date: Fri, 6 Dec 2019 17:07:33 +0000 (-0600) Subject: Calculate the position of new (and foreach) elements at delta_head funcs X-Git-Tag: v1.2.0~41 X-Git-Url: http://git.ipfire.org/gitweb/gitweb.cgi?a=commitdiff_plain;h=414418b979b6aeed37969453d96ecb94bf173ad7;p=thirdparty%2FFORT-validator.git Calculate the position of new (and foreach) elements at delta_head funcs --- diff --git a/src/rrdp/rrdp_objects.c b/src/rrdp/rrdp_objects.c index 31160fac..031a203e 100644 --- a/src/rrdp/rrdp_objects.c +++ b/src/rrdp/rrdp_objects.c @@ -142,15 +142,10 @@ deltas_head_set_size(struct deltas_head *deltas, size_t capacity) return 0; } -size_t -deltas_head_get_size(struct deltas_head *deltas) -{ - return deltas->capacity; -} - /* - * A new delta_head will be allocated at the @position inside @deltas (also its - * URI and HASH will be allocated). + * A new delta_head will be allocated at its corresponding position inside + * @deltas (also its URI and HASH will be allocated). The position is calculated + * using the difference between @max_serial and @serial. * * The following errors can be returned due to a wrong @position: * -EEXIST: There's already an element at @position. @@ -160,12 +155,14 @@ deltas_head_get_size(struct deltas_head *deltas) * Don't forget to call deltas_head_set_size() before this!! */ int -deltas_head_add(struct deltas_head *deltas, size_t position, +deltas_head_add(struct deltas_head *deltas, unsigned long max_serial, unsigned long serial, char *uri, unsigned char *hash, size_t hash_len) { struct delta_head *elem; + size_t position; int error; + position = deltas->capacity - 1 - (max_serial - serial); if (position < 0 || position > deltas->capacity - 1) return -EINVAL; @@ -207,11 +204,13 @@ deltas_head_values_set(struct deltas_head *deltas) return deltas->len == deltas->capacity; } +/* Do the @cb to the delta head elements from @from_serial to @max_serial */ int -deltas_head_for_each(struct deltas_head *deltas, size_t from, delta_head_cb cb, - void *arg) +deltas_head_for_each(struct deltas_head *deltas, unsigned long max_serial, + unsigned long from_serial, delta_head_cb cb, void *arg) { size_t index; + size_t from; int error; /* No elements, send error so that the snapshot is processed */ @@ -220,6 +219,7 @@ deltas_head_for_each(struct deltas_head *deltas, size_t from, delta_head_cb cb, return -ENOENT; } + from = deltas->capacity - (max_serial - from_serial); for (index = from; index < deltas->capacity; index++) { error = cb(deltas->array[index], arg); if (error) diff --git a/src/rrdp/rrdp_objects.h b/src/rrdp/rrdp_objects.h index b062b49e..1ecc4a72 100644 --- a/src/rrdp/rrdp_objects.h +++ b/src/rrdp/rrdp_objects.h @@ -86,12 +86,12 @@ int update_notification_create(struct update_notification **); void update_notification_destroy(struct update_notification *); typedef int (*delta_head_cb)(struct delta_head *, void *); -int deltas_head_for_each(struct deltas_head *, size_t, delta_head_cb, void *); -int deltas_head_add(struct deltas_head *, size_t, unsigned long, char *, +int deltas_head_for_each(struct deltas_head *, unsigned long, unsigned long, + delta_head_cb, void *); +int deltas_head_add(struct deltas_head *, unsigned long, unsigned long, char *, unsigned char *, size_t); int deltas_head_set_size(struct deltas_head *, size_t); -size_t deltas_head_get_size(struct deltas_head *); bool deltas_head_values_set(struct deltas_head *); int snapshot_create(struct snapshot **); diff --git a/src/rrdp/rrdp_parser.c b/src/rrdp/rrdp_parser.c index 6444267f..88fee113 100644 --- a/src/rrdp/rrdp_parser.c +++ b/src/rrdp/rrdp_parser.c @@ -427,11 +427,10 @@ end: static int parse_notification_deltas(xmlNode *root, unsigned long max_serial, - unsigned long deltas_len, struct deltas_head *deltas) + struct deltas_head *deltas) { struct doc_data doc_data; unsigned long serial; - size_t position; int error; error = parse_long(root, RRDP_ATTR_SERIAL, &serial); @@ -449,8 +448,7 @@ parse_notification_deltas(xmlNode *root, unsigned long max_serial, * The delta will be added to the position it belongs, assuring that * the deltas list will be ordered. */ - position = deltas_len - 1 - (max_serial - serial); - error = deltas_head_add(deltas, position, serial, doc_data.uri, + error = deltas_head_add(deltas, max_serial, serial, doc_data.uri, doc_data.hash, doc_data.hash_len); /* Always release data */ @@ -477,7 +475,6 @@ parse_notification_data(xmlNode *root, struct update_notification *file, xmlNode *cur_node; rrdp_uri_cmp_result_t res; unsigned long from_serial, max_serial; - unsigned long deltas_len; bool create_snapshot; int error; @@ -486,9 +483,8 @@ parse_notification_data(xmlNode *root, struct update_notification *file, max_serial = file->global_data.serial; /* By schema, at least one snapshot element will be present */ - deltas_len = xmlChildElementCount(root) - 1; - - error = deltas_head_set_size(file->deltas_list, deltas_len); + error = deltas_head_set_size(file->deltas_list, + xmlChildElementCount(root) - 1); if (error) return error; @@ -517,7 +513,7 @@ parse_notification_data(xmlNode *root, struct update_notification *file, for (cur_node = root->children; cur_node; cur_node = cur_node->next) { if (xmlStrEqual(cur_node->name, BAD_CAST RRDP_ELEM_DELTA)) { error = parse_notification_deltas(cur_node, max_serial, - deltas_len, file->deltas_list); + file->deltas_list); } else if (xmlStrEqual(cur_node->name, BAD_CAST RRDP_ELEM_SNAPSHOT)) { error = parse_doc_data(cur_node, true, true, @@ -1033,12 +1029,6 @@ int rrdp_process_deltas(struct update_notification *parent, unsigned long cur_serial) { - size_t deltas_len; - size_t from; - - deltas_len = deltas_head_get_size(parent->deltas_list); - from = deltas_len - (parent->global_data.serial - cur_serial); - - return deltas_head_for_each(parent->deltas_list, from, process_delta, - parent); + return deltas_head_for_each(parent->deltas_list, + parent->global_data.serial, cur_serial, process_delta, parent); }