From: Alberto Leiva Popper Date: Mon, 14 Oct 2024 23:07:49 +0000 (-0600) Subject: Move stuff between cache_setup() and cache_prepare() X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=385f716a8d40214985edecab49a54519bc6a3d3b;p=thirdparty%2FFORT-validator.git Move stuff between cache_setup() and cache_prepare() The former is intended for stuff that needs to be done once, the latter sets up a single validation cycle. --- diff --git a/src/cache.c b/src/cache.c index fcdde562..53a48e65 100644 --- a/src/cache.c +++ b/src/cache.c @@ -92,6 +92,21 @@ delete_node(struct cache_table *tbl, struct cache_node *node) free(node); } +static void +cache_foreach(void (*cb)(struct cache_table *, struct cache_node *)) +{ + struct cache_node *node, *tmp; + + HASH_ITER(hh, cache.rsync.nodes, node, tmp) + cb(&cache.rsync, node); + HASH_ITER(hh, cache.https.nodes, node, tmp) + cb(&cache.https, node); + HASH_ITER(hh, cache.rrdp.nodes, node, tmp) + cb(&cache.rrdp, node); + HASH_ITER(hh, cache.fallback.nodes, node, tmp) + cb(&cache.fallback, node); +} + char * get_rsync_module(char const *url) { @@ -188,7 +203,7 @@ end: closedir(dir); return error; } -static void +static int init_cache_metafile(void) { json_t *root; @@ -224,13 +239,11 @@ init_cache_metafile(void) goto invalid_cache; json_decref(root); - return; + return 0; invalid_cache: - pr_op_info("The cache seems to have been built by a different version of Fort. " - "I'm going to clear it, just to be safe."); - reset_cache_dir(); json_decref(root); + return EINVAL; } static void @@ -245,25 +258,6 @@ init_cachedir_tag(void) "# https://bford.info/cachedir/\n"); } -static int -init_cache_dirs(void) -{ - int error; - error = file_mkdir("rsync", true); - if (error) - return error; - error = file_mkdir("https", true); - if (error) - return error; - error = file_mkdir("rrdp", true); - if (error) - return error; - error = file_mkdir("fallback", true); - if (error) - return error; - return file_mkdir(CACHE_TMPDIR, true); -} - int cache_setup(void) { @@ -282,11 +276,7 @@ cache_setup(void) return error; } - // XXX Lock the cache directory init_tables(); - init_cache_metafile(); - init_cache_dirs(); - init_cachedir_tag(); return 0; } @@ -354,7 +344,7 @@ json2tbl(json_t *root, struct cache_table *tbl) } } -static void +static int load_tal_json(void) { json_t *root; @@ -388,13 +378,51 @@ load_tal_json(void) json2tbl(root, &cache.rrdp); json2tbl(root, &cache.fallback); + json_decref(root); + return 0; + end: json_decref(root); + return EINVAL; } -void +int cache_prepare(void) { - load_tal_json(); + int error; + + if (init_cache_metafile() != 0) { + error = reset_cache_dir(); + if (error) + return error; + } + if (load_tal_json() != 0) { + error = reset_cache_dir(); + if (error) + return error; + } + + // XXX Lock the cache directory + error = file_mkdir("rsync", true); + if (error) + goto fail; + error = file_mkdir("https", true); + if (error) + goto fail; + error = file_mkdir("rrdp", true); + if (error) + goto fail; + error = file_mkdir("fallback", true); + if (error) + goto fail; + error = file_mkdir(CACHE_TMPDIR, true); + if (error) + goto fail; + init_cachedir_tag(); + + return 0; + +fail: cache_foreach(delete_node); + return error; } static json_t * @@ -1077,21 +1105,6 @@ remove_orphaned(struct cache_table *table, struct cache_node *node) } } -static void -cache_foreach(void (*cb)(struct cache_table *, struct cache_node *)) -{ - struct cache_node *node, *tmp; - - HASH_ITER(hh, cache.rsync.nodes, node, tmp) - cb(&cache.rsync, node); - HASH_ITER(hh, cache.https.nodes, node, tmp) - cb(&cache.https, node); - HASH_ITER(hh, cache.rrdp.nodes, node, tmp) - cb(&cache.rrdp, node); - HASH_ITER(hh, cache.fallback.nodes, node, tmp) - cb(&cache.fallback, node); -} - /* * Deletes unknown and old untraversed cached files, writes metadata into XML. */ diff --git a/src/cache.h b/src/cache.h index 743901a4..0fab4cc9 100644 --- a/src/cache.h +++ b/src/cache.h @@ -8,7 +8,7 @@ int cache_setup(void); /* Init this module */ void cache_teardown(void); /* Destroy this module */ -void cache_prepare(void); /* Prepare cache for new validation cycle */ +int cache_prepare(void); /* Prepare cache for new validation cycle */ void cache_commit(void); /* Finish validation cycle */ /* XXX might wanna rename */ diff --git a/src/object/tal.c b/src/object/tal.c index cd2e3b0e..ffa68f8c 100644 --- a/src/object/tal.c +++ b/src/object/tal.c @@ -264,10 +264,12 @@ perform_standalone_validation(void) struct threads_list threads = SLIST_HEAD_INITIALIZER(threads); struct validation_thread *thread; struct db_table *db = NULL; - int error = 0; + int error; int tmperr; - cache_prepare(); + error = cache_prepare(); + if (error) + return NULL; /* TODO (fine) Maybe don't spawn threads if there's only one TAL */ if (foreach_file(config_get_tal(), ".tal", true, spawn_tal_thread, diff --git a/test/object/tal_test.c b/test/object/tal_test.c index 824a6efc..1f0c4feb 100644 --- a/test/object/tal_test.c +++ b/test/object/tal_test.c @@ -14,7 +14,7 @@ /* Mocks */ -MOCK_ABORT_VOID(cache_prepare, void) +MOCK_ABORT_INT(cache_prepare, void) MOCK_ABORT_VOID(cache_commit, void) MOCK_ABORT_PTR(db_table_create, db_table, void) MOCK_VOID(db_table_destroy, struct db_table *table)