]> git.ipfire.org Git - thirdparty/FORT-validator.git/commitdiff
Init basic cache structure on validation start
authorAlberto Leiva Popper <ydahhrk@gmail.com>
Tue, 3 Oct 2023 18:52:51 +0000 (12:52 -0600)
committerAlberto Leiva Popper <ydahhrk@gmail.com>
Tue, 3 Oct 2023 19:59:06 +0000 (13:59 -0600)
Perform a token attempt to create the cache directory, as well as a more
reasonable one to create its tmp/ subdirectory, whenever the validation
cycle begins.

src/cache/local_cache.c
src/cache/local_cache.h
src/common.c
src/data_structure/path_builder.c
src/http/http.c
src/rtr/db/vrps.c
test/cache/local_cache_test.c

index 7a9493e7853975fcf7c52b43ff95bf25ca34a952..41099cfa1ef6bec4b1a4c3f6293c633792d7473c 100644 (file)
@@ -6,6 +6,7 @@
 #include <time.h>
 
 #include "alloc.h"
+#include "common.h"
 #include "config.h"
 #include "file.h"
 #include "log.h"
@@ -82,6 +83,26 @@ static struct cache_node *https;
 
 static time_t startup_time; /* When we started the last validation */
 
+static int
+init_cache_pb(struct path_builder *pb, char const *subdir)
+{
+       int error;
+
+       pb_init(pb);
+       error = pb_append(pb, config_get_local_repository());
+       if (error)
+               goto cancel;
+       error = pb_append(pb, subdir);
+       if (error)
+               goto cancel;
+
+       return 0;
+
+cancel:
+       pb_cleanup(pb);
+       return error;
+}
+
 /* Minimizes multiple evaluation */
 static struct cache_node *
 add_child(struct cache_node *parent, char const *basename)
@@ -156,20 +177,14 @@ get_metadata_json_filename(char **filename)
        struct path_builder pb;
        int error;
 
-       pb_init(&pb);
-       error = pb_append(&pb, config_get_local_repository());
-       if (error)
-               goto cancel;
-       error = pb_append(&pb, "metadata.json");
-       if (error)
-               goto cancel;
+       error = init_cache_pb(&pb, "metadata.json");
+       if (error) {
+               pb_cleanup(&pb);
+               return error;
+       }
 
        *filename = pb.string;
        return 0;
-
-cancel:
-       pb_cleanup(&pb);
-       return error;
 }
 
 static int
@@ -318,15 +333,25 @@ end:
        json_decref(root);
 }
 
-void
+int
 cache_prepare(void)
 {
+       struct path_builder pb;
+       int error;
+
        startup_time = time(NULL);
        if (startup_time == ((time_t) -1))
                pr_crit("time(NULL) returned -1");
 
        if (rsync == NULL)
                load_metadata_json();
+
+       error = init_cache_pb(&pb, "tmp/a");
+       if (error)
+               return error;
+       error = create_dir_recursive(pb.string);
+       pb_cleanup(&pb);
+       return error;
 }
 
 static int
@@ -678,8 +703,7 @@ static void cleanup_tree(struct cache_node **root, char const *treename)
        struct cache_node *node, *child, *tmp;
        int error;
 
-       pb_init(&pb);
-       if (pb_append(&pb, config_get_local_repository()) != 0)
+       if (init_cache_pb(&pb, NULL) != 0)
                goto end;
 
        ctt_init(&ctt, root, &pb);
index 6aa4416e9047641e723dfbff5bf65568ed290fa4..938e9cbace3ec8dd28d8d503d7a5687ffc802268 100644 (file)
@@ -4,7 +4,7 @@
 #include "types/uri.h"
 
 /* Warms up cache for new validation run */
-void cache_prepare(void);
+int cache_prepare(void);
 
 /* Downloads @uri into the cache */
 int cache_download(struct rpki_uri *uri, bool *);
index 0037875ce1449d322bb7fdf5d4daeb743009782c..5498b7b67eca38447767831d3000f275fbdca0c4 100644 (file)
@@ -262,7 +262,7 @@ dir_exists(char *path, bool *result)
 }
 
 static int
-create_dir(char *path)
+create_dir(char const *path)
 {
        int error;
 
index dbbbc21b6a2523de06263ee9c75b31aa530961d2..b94045405569a697e3397d19b3ba6e91857ce42b 100644 (file)
@@ -79,7 +79,9 @@ pb_appendn(struct path_builder *pb, char const *addend, size_t addlen)
 int
 pb_append(struct path_builder *pb, char const *addend)
 {
-       return pb_appendn(pb, addend, strlen(addend));
+       return (addend != NULL)
+           ? pb_appendn(pb, addend, strlen(addend))
+           : 0;
 }
 
 int
index 73dc7338f1a483ab03348cda07d7f6f9cf98d09b..24d080a21220b6248825758a4d90f6d8c55856e4 100644 (file)
@@ -280,7 +280,7 @@ http_fetch(char const *src, char const *dst, curl_off_t ims, bool *changed)
        setopt_writedata(handler.curl, &args);
 
        pr_val_info("HTTP GET: %s", src);
-       res = curl_easy_perform(handler.curl);
+       res = curl_easy_perform(handler.curl); /* write_callback() */
        if (args.file != NULL)
                file_close(args.file);
        pr_val_debug("Done. Total bytes transferred: %zu", args.total_bytes);
index 3dd6876dc8861de327389c83902f0ac9f7686b8e..57abc26afc7b8f78b09bbe7051b01d9934d3d4c1 100644 (file)
@@ -219,7 +219,9 @@ __perform_standalone_validation(struct db_table **result)
        struct db_table *db;
        int error;
 
-       cache_prepare();
+       error = cache_prepare();
+       if (error)
+               return error;
 
        db = db_table_create();
 
index d1d926e0b044aa6d8544e877e8719893b5d566e7..fb9ceaa14fcb9bac7c54683c3e73dadd92691f64 100644 (file)
@@ -363,7 +363,7 @@ backtrack_times(struct cache_node *node)
 static void
 __cache_prepare(void)
 {
-       cache_prepare();
+       ck_assert_int_eq(0, cache_prepare());
        /* Ensure the old ts_successes and ts_attempts are outdated */
        backtrack_times(rsync);
        backtrack_times(https);
@@ -376,7 +376,7 @@ START_TEST(test_cache_download_rsync)
        ck_assert_int_eq(0, system("rm -rf tmp/"));
        dl_error = false;
 
-       cache_prepare();
+       ck_assert_int_eq(0, cache_prepare());
 
        download_rsync("rsync://a.b.c/d/e", 0, 1);
        validate_tree(rsync,
@@ -457,7 +457,7 @@ START_TEST(test_cache_download_rsync_error)
 {
        ck_assert_int_eq(0, system("rm -rf tmp/"));
 
-       cache_prepare();
+       ck_assert_int_eq(0, cache_prepare());
 
        dl_error = false;
        download_rsync("rsync://a.b.c/d", 0, 1);
@@ -639,7 +639,7 @@ START_TEST(test_cache_cleanup_rsync_error)
 {
        ck_assert_int_eq(0, system("rm -rf tmp/"));
 
-       cache_prepare();
+       ck_assert_int_eq(0, cache_prepare());
 
        /* Set up */
        dl_error = false;
@@ -692,7 +692,7 @@ START_TEST(test_cache_download_https)
        ck_assert_int_eq(0, system("rm -rf tmp/"));
        dl_error = false;
 
-       cache_prepare();
+       ck_assert_int_eq(0, cache_prepare());
 
        /* Download *file* e. */
        download_https("https://a.b.c/d/e", 0, 1);
@@ -744,7 +744,7 @@ START_TEST(test_cache_download_https_error)
 {
        ck_assert_int_eq(0, system("rm -rf tmp/"));
 
-       cache_prepare();
+       ck_assert_int_eq(0, cache_prepare());
 
        dl_error = false;
        download_https("https://a.b.c/d", 0, 1);
@@ -886,7 +886,7 @@ START_TEST(test_cache_cleanup_https_error)
 {
        ck_assert_int_eq(0, system("rm -rf tmp/"));
 
-       cache_prepare();
+       ck_assert_int_eq(0, cache_prepare());
 
        /* Set up */
        dl_error = false;
@@ -934,7 +934,7 @@ START_TEST(test_dots)
        ck_assert_int_eq(0, system("rm -rf tmp/"));
        dl_error = false;
 
-       cache_prepare();
+       ck_assert_int_eq(0, cache_prepare());
 
        download_https("https://a.b.c/d", 0, 1);
        validate_tree(https,
@@ -1054,7 +1054,7 @@ START_TEST(test_ctt_traversal)
 
        ck_assert_int_eq(0, system("rm -rf tmp/"));
 
-       cache_prepare();
+       ck_assert_int_eq(0, cache_prepare());
        now = time(NULL);
        if (now == ((time_t) -1))
                ck_abort_msg("time(NULL) returned -1");