]> git.ipfire.org Git - thirdparty/FORT-validator.git/commitdiff
RRDP: Clear RPPs before exploding snapshots into them
authorAlberto Leiva Popper <ydahhrk@gmail.com>
Wed, 2 Feb 2022 23:56:40 +0000 (17:56 -0600)
committerAlberto Leiva Popper <ydahhrk@gmail.com>
Wed, 2 Feb 2022 23:56:40 +0000 (17:56 -0600)
src/data_structure/path_builder.c
src/rrdp/snapshot.c

index 51c6d342a48cd18c700a38c1ea216f000303cf19..32e1ca35a1da2037d84d6195b8adea81581c6f68 100644 (file)
@@ -71,6 +71,9 @@ path_append_url(struct path_builder *pb, char const *guri)
 {
        char *colon;
 
+       if (guri == NULL)
+               return;
+
        /* Is there really a point to removing the colon? */
        colon = strchr(guri, ':');
        if (colon != NULL) {
index 21313f8553f34ac08d430af7e74c3f2d7746480d..5ee1290cd45061120065601c4af09eeb5d4d9cc7 100644 (file)
@@ -1,9 +1,45 @@
 #include "rrdp/snapshot.h"
 
+#define _XOPEN_SOURCE 500
+#include <ftw.h>
+
 #include "thread_var.h"
 #include "http/http.h"
 #include "xml/relax_ng.h"
 
+static int
+rm_file(const char *fpath, const struct stat *sb, int typeflag,
+    struct FTW *ftwbuf)
+{
+       int error;
+
+       error = remove(fpath);
+       if (error) {
+               pr_val_errno(errno, "Could not clean file or directory '%s'",
+                   fpath);
+       }
+
+       return error;
+}
+
+/* Deletes (if any) all the old files from the RPP owned by @notif. */
+static int
+clear_caged_directory(struct rrdp_notification *notif)
+{
+       struct rpki_uri *cage;
+       int error;
+
+       error = uri_create_caged(NULL, notif, &cage);
+       if (error)
+               return error;
+
+       pr_val_debug("Making sure '%s' is empty.", uri_get_local(cage));
+       error = nftw(uri_get_local(cage), rm_file, 32, FTW_DEPTH | FTW_PHYS);
+
+       uri_refput(cage);
+       return error;
+}
+
 static int
 xml_read_snapshot(xmlTextReaderPtr reader, void *arg)
 {
@@ -12,8 +48,6 @@ xml_read_snapshot(xmlTextReaderPtr reader, void *arg)
        struct rrdp_notification *notif;
        xmlChar const *name;
 
-       /* TODO (aaaa) probably make sure the directory is empty before exploding */
-
        if (xmlTextReaderNodeType(reader) != XML_READER_TYPE_ELEMENT)
                return 0;
 
@@ -47,6 +81,10 @@ rrdp_parse_snapshot(struct rrdp_notification *notification)
        if (error)
                goto pop;
 
+       error = clear_caged_directory(notification);
+       if (error)
+               goto pop;
+
        error = relax_ng_parse(uri_get_local(uri), xml_read_snapshot,
            notification);