From ecb8156fbd10ab8f0ce287ab6eb6cbb271204355 Mon Sep 17 00:00:00 2001 From: Alberto Leiva Popper Date: Wed, 4 Oct 2023 14:37:33 -0600 Subject: [PATCH] Include cache dir in local counterpart of URIs 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 | 48 ++++++++++++------------------- src/cache/tmp.c | 8 +----- src/data_structure/path_builder.c | 23 +++++++++++++++ src/data_structure/path_builder.h | 1 + src/types/uri.c | 5 +++- test/Makefile.am | 1 + test/cache/local_cache_test.c | 4 +-- test/types/uri_test.c | 16 +++++------ 8 files changed, 59 insertions(+), 47 deletions(-) diff --git a/src/cache/local_cache.c b/src/cache/local_cache.c index e58a1260..c8d45c8a 100644 --- a/src/cache/local_cache.c +++ b/src/cache/local_cache.c @@ -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); } diff --git a/src/cache/tmp.c b/src/cache/tmp.c index 8507f88c..26cfaf3d 100644 --- a/src/cache/tmp.c +++ b/src/cache/tmp.c @@ -2,7 +2,6 @@ #include -#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)); diff --git a/src/data_structure/path_builder.c b/src/data_structure/path_builder.c index 37839927..bce0fcd4 100644 --- a/src/data_structure/path_builder.c +++ b/src/data_structure/path_builder.c @@ -3,6 +3,7 @@ #include #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) { diff --git a/src/data_structure/path_builder.h b/src/data_structure/path_builder.h index 2971b722..ab6b6814 100644 --- a/src/data_structure/path_builder.h +++ b/src/data_structure/path_builder.h @@ -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. diff --git a/src/types/uri.c b/src/types/uri.c index c11674f3..dd688526 100644 --- a/src/types/uri.c +++ b/src/types/uri.c @@ -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); diff --git a/test/Makefile.am b/test/Makefile.am index 80dbc94e..cffea9d2 100644 --- a/test/Makefile.am +++ b/test/Makefile.am @@ -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 diff --git a/test/cache/local_cache_test.c b/test/cache/local_cache_test.c index 3dd4d13f..761dcacd 100644 --- a/test/cache/local_cache_test.c +++ b/test/cache/local_cache_test.c @@ -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); diff --git a/test/types/uri_test.c b/test/types/uri_test.c index 2c2e3992..052fae1b 100644 --- a/test/types/uri_test.c +++ b/test/types/uri_test.c @@ -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/..")); -- 2.47.2