]> git.ipfire.org Git - thirdparty/FORT-validator.git/commitdiff
Include cache dir in local counterpart of URIs
authorAlberto Leiva Popper <ydahhrk@gmail.com>
Wed, 4 Oct 2023 20:37:33 +0000 (14:37 -0600)
committerAlberto Leiva Popper <ydahhrk@gmail.com>
Wed, 4 Oct 2023 20:41:46 +0000 (14:41 -0600)
Before:

- URI: https://a.b.c/d/e.xml
- Local path: https/a.b.c/d/e.xml

Now:

- URI: https://a.b.c/d/e.xml
- Local path: /path/to/cache/https/a.b.c/d/e.xml

Is a bit more memory-gluttonous, but prints a more helpful string,
and simplifies user code by taking care of the task up front.

src/cache/local_cache.c
src/cache/tmp.c
src/data_structure/path_builder.c
src/data_structure/path_builder.h
src/types/uri.c
test/Makefile.am
test/cache/local_cache_test.c
test/types/uri_test.c

index e58a1260429f14ade8726305cd6da27ed467476a..c8d45c8a3a20efe0a2e69c66062c76894cb087df 100644 (file)
@@ -80,26 +80,6 @@ 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)
@@ -174,11 +154,9 @@ get_metadata_json_filename(char **filename)
        struct path_builder pb;
        int error;
 
-       error = init_cache_pb(&pb, "metadata.json");
-       if (error) {
-               pb_cleanup(&pb);
+       error = pb_init_cache(&pb, "metadata.json");
+       if (error)
                return error;
-       }
 
        *filename = pb.string;
        return 0;
@@ -346,7 +324,7 @@ cache_prepare(void)
        if (rsync == NULL)
                load_metadata_json();
 
-       error = init_cache_pb(&pb, "tmp/a");
+       error = pb_init_cache(&pb, "tmp/a");
        if (error)
                return error;
        error = create_dir_recursive(pb.string);
@@ -407,6 +385,18 @@ drop_children(struct cache_node *node)
                delete_node(child);
 }
 
+static char *
+uri2luri(struct rpki_uri *uri)
+{
+       char const *luri;
+
+       luri = uri_get_local(uri) + strlen(config_get_local_repository());
+       while (luri[0] == '/')
+               luri++;
+
+       return pstrdup(luri);
+}
+
 /**
  * @changed only on HTTP.
  */
@@ -422,7 +412,7 @@ cache_download(struct rpki_uri *uri, bool *changed)
 
        if (changed != NULL)
                *changed = false;
-       luri = pstrdup(uri_get_local(uri));
+       luri = uri2luri(uri);
        token = strtok_r(luri, "/", &saveptr);
 
        switch (uri_get_type(uri)) {
@@ -703,8 +693,8 @@ static void cleanup_tree(struct cache_node **root, char const *treename)
        struct cache_node *node, *child, *tmp;
        int error;
 
-       if (init_cache_pb(&pb, NULL) != 0)
-               goto end;
+       if (pb_init_cache(&pb, NULL) != 0)
+               return;
 
        ctt_init(&ctt, root, &pb);
 
@@ -790,7 +780,7 @@ static void cleanup_tree(struct cache_node **root, char const *treename)
 
        if ((*root) == NULL && pb_append(&pb, treename) == 0)
                pb_rm_r(&pb, treename, true);
-end:
+
        pb_cleanup(&pb);
 }
 
index 8507f88cbd8c54c1486813a9065107d6c969d62f..26cfaf3d48954aa0c952ee9ec8bc606955f1332c 100644 (file)
@@ -2,7 +2,6 @@
 
 #include <stdatomic.h>
 
-#include "config.h"
 #include "data_structure/path_builder.h"
 
 static atomic_uint file_counter;
@@ -23,12 +22,7 @@ cache_tmpfile(char **filename)
        struct path_builder pb;
        int error;
 
-       pb_init(&pb);
-
-       error = pb_append(&pb, config_get_local_repository());
-       if (error)
-               return error;
-       error = pb_append(&pb, "tmp");
+       error = pb_init_cache(&pb, "tmp");
        if (error)
                return error;
        error = pb_append_u32(&pb, atomic_fetch_add(&file_counter, 1u));
index 3783992749bb77b66db53e528e83c8969d3102d3..bce0fcd4907b575cb7f806ccdb86f4c8c11b3472 100644 (file)
@@ -3,6 +3,7 @@
 #include <errno.h>
 
 #include "alloc.h"
+#include "config.h"
 #include "log.h"
 #include "crypto/hash.h"
 
@@ -23,6 +24,28 @@ pb_init(struct path_builder *pb)
        pb->capacity = INITIAL_CAPACITY;
 }
 
+int
+pb_init_cache(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;
+}
+
 static int
 pb_grow(struct path_builder *pb, size_t total_len, char const *addend)
 {
index 2971b7224def450134bf88695491f1d253e13316..ab6b68149c80e676d0bbaea58f87a10c893aa5f4 100644 (file)
@@ -10,6 +10,7 @@ struct path_builder {
 };
 
 void pb_init(struct path_builder *);
+int pb_init_cache(struct path_builder *, char const *);
 
 /*
  * The appends are atomic.
index c11674f34cd0acc1a07d474722fb6bfbc402af35..dd6885260804d08373344149dcaa63125132171c 100644 (file)
@@ -312,7 +312,10 @@ map_simple(struct rpki_uri *uri, char const *gprefix, int err)
        struct path_builder pb;
        int error;
 
-       pb_init(&pb);
+       error = pb_init_cache(&pb, NULL);
+       if (error)
+               return error;
+
        error = append_guri(&pb, uri->global, gprefix, err, false);
        if (error) {
                pb_cleanup(&pb);
index 80dbc94e16b0b40baa87707fdb31b13bae29492e..cffea9d2d9a33274626116032ce89450a0c3f4ce 100644 (file)
@@ -13,6 +13,7 @@ if USE_TESTS
 # Otherwise it must be included manually:
 #      mumble_mumble_CFLAGS = ${AM_CFLAGS} flag1 flag2 flag3 ...
 AM_CFLAGS =  -pedantic -Wall
+#AM_CFLAGS += -Wno-unused
 AM_CFLAGS += -std=c99 -D_POSIX_C_SOURCE=200809 -D_XOPEN_SOURCE=700
 AM_CFLAGS += -I../src -DUNIT_TESTING ${CHECK_CFLAGS} ${XML2_CFLAGS}
 # Reminder: As opposed to AM_CFLAGS, "AM_LDADD" is not idiomatic automake, and
index 3dd4d13f2191c5d67629464b3a7e491e460c6277..761dcacddc05ce91beecafc41b3632627cd178ae 100644 (file)
@@ -30,7 +30,7 @@ rsync_download(struct rpki_uri *uri)
                return -EINVAL;
 
        cmd = pmalloc(128);
-       printed = snprintf(cmd, 128, "mkdir -p tmp/%s", uri_get_local(uri));
+       printed = snprintf(cmd, 128, "mkdir -p %s", uri_get_local(uri));
        ck_assert(printed < 128);
 
        ck_assert_int_eq(0, system(cmd));
@@ -53,7 +53,7 @@ http_download(struct rpki_uri *uri, bool *changed)
        cmd = pmalloc(128);
        printed = snprintf(cmd, 128,
            /* "create file, but only if it's not already a directory" */
-           "test ! -d tmp/%s && install -D /dev/null tmp/%s",
+           "test ! -d %s && install -D /dev/null %s",
            uri_get_local(uri), uri_get_local(uri));
        ck_assert(printed < 128);
 
index 2c2e399230db6f99560e6b60f8c77231c4b7d130..052fae1b71c59df47ac0b91e39f818606ea331c2 100644 (file)
@@ -32,42 +32,42 @@ START_TEST(test_constructor)
 
        ck_assert_int_eq(0, uri_create(&uri, UT_HTTPS, "https://a.b.c"));
        ck_assert_str_eq("https://a.b.c", uri_get_global(uri));
-       ck_assert_str_eq("https/a.b.c", uri_get_local(uri));
+       ck_assert_str_eq("tmp/https/a.b.c", uri_get_local(uri));
        uri_refput(uri);
 
        ck_assert_int_eq(0, uri_create(&uri, UT_HTTPS, "https://a.b.c/"));
        ck_assert_str_eq("https://a.b.c/", uri_get_global(uri));
-       ck_assert_str_eq("https/a.b.c", uri_get_local(uri));
+       ck_assert_str_eq("tmp/https/a.b.c", uri_get_local(uri));
        uri_refput(uri);
 
        ck_assert_int_eq(0, uri_create(&uri, UT_HTTPS, "https://a.b.c/d"));
        ck_assert_str_eq("https://a.b.c/d", uri_get_global(uri));
-       ck_assert_str_eq("https/a.b.c/d", uri_get_local(uri));
+       ck_assert_str_eq("tmp/https/a.b.c/d", uri_get_local(uri));
        uri_refput(uri);
 
        ck_assert_int_eq(0, uri_create(&uri, UT_HTTPS, "https://a.b.c/d/e"));
        ck_assert_str_eq("https://a.b.c/d/e", uri_get_global(uri));
-       ck_assert_str_eq("https/a.b.c/d/e", uri_get_local(uri));
+       ck_assert_str_eq("tmp/https/a.b.c/d/e", uri_get_local(uri));
        uri_refput(uri);
 
        ck_assert_int_eq(0, uri_create(&uri, UT_HTTPS, "https://a.b.c/d/.."));
        ck_assert_str_eq("https://a.b.c/d/..", uri_get_global(uri));
-       ck_assert_str_eq("https/a.b.c", uri_get_local(uri));
+       ck_assert_str_eq("tmp/https/a.b.c", uri_get_local(uri));
        uri_refput(uri);
 
        ck_assert_int_eq(0, uri_create(&uri, UT_HTTPS, "https://a.b.c/."));
        ck_assert_str_eq("https://a.b.c/.", uri_get_global(uri));
-       ck_assert_str_eq("https/a.b.c", uri_get_local(uri));
+       ck_assert_str_eq("tmp/https/a.b.c", uri_get_local(uri));
        uri_refput(uri);
 
        ck_assert_int_eq(0, uri_create(&uri, UT_HTTPS, "https://a.b.c/././d/././e/./."));
        ck_assert_str_eq("https://a.b.c/././d/././e/./.", uri_get_global(uri));
-       ck_assert_str_eq("https/a.b.c/d/e", uri_get_local(uri));
+       ck_assert_str_eq("tmp/https/a.b.c/d/e", uri_get_local(uri));
        uri_refput(uri);
 
        ck_assert_int_eq(0, uri_create(&uri, UT_HTTPS, "https://a.b.c/a/b/.././.."));
        ck_assert_str_eq("https://a.b.c/a/b/.././..", uri_get_global(uri));
-       ck_assert_str_eq("https/a.b.c", uri_get_local(uri));
+       ck_assert_str_eq("tmp/https/a.b.c", uri_get_local(uri));
        uri_refput(uri);
 
        ck_assert_int_eq(-EINVAL, uri_create(&uri, UT_HTTPS, "https://a.b.c/.."));