]> git.ipfire.org Git - thirdparty/FORT-validator.git/commitdiff
Calculate the position of new (and foreach) elements at delta_head funcs
authorpcarana <pc.moreno2099@gmail.com>
Fri, 6 Dec 2019 17:07:33 +0000 (11:07 -0600)
committerpcarana <pc.moreno2099@gmail.com>
Fri, 6 Dec 2019 17:07:33 +0000 (11:07 -0600)
src/rrdp/rrdp_objects.c
src/rrdp/rrdp_objects.h
src/rrdp/rrdp_parser.c

index 31160fac9cb683e94d1f9f7f6846f7d4326435ef..031a203ee4428088038eda9635ba5fa372a3cb94 100644 (file)
@@ -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)
index b062b49ebdc7537df38739803a94b60f7d908fc1..1ecc4a72842cd0351bccbe7d5203ce72b77907c7 100644 (file)
@@ -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 **);
index 6444267f16d97c723350660ecf3b393e5c09c91d..88fee1133423107a9c6b975af12f792915790367 100644 (file)
@@ -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);
 }