From: Alberto Leiva Popper Date: Tue, 14 Nov 2023 22:39:18 +0000 (-0600) Subject: stat() to find out if a file is cached in offline mode X-Git-Tag: 1.6.0~14 X-Git-Url: http://git.ipfire.org/gitweb/gitweb.cgi?a=commitdiff_plain;h=65f09421efe1e3bd5f64f1547aa2c6bf651c164d;p=thirdparty%2FFORT-validator.git stat() to find out if a file is cached in offline mode The offline code used to assume files were always cached when --http.enabled/--rsync.enabled were false. Now it returns a more informed answer, depending on whether the file is actually cached. With this, offline mode more closely resembles online mode. --- diff --git a/src/cache/local_cache.c b/src/cache/local_cache.c index fe454d06..373aaaed 100644 --- a/src/cache/local_cache.c +++ b/src/cache/local_cache.c @@ -405,6 +405,23 @@ uri2luri(struct rpki_uri *uri) return pstrdup(luri); } +/* Returns 0 if the file exists, nonzero otherwise. */ +static int +cache_check(struct rpki_uri *uri) +{ + struct stat meta; + int error; + + if (stat(uri_get_local(uri), &meta) != 0) { + error = errno; + pr_val_debug("Offline mode, file is not cached."); + return error; + } + + pr_val_debug("Offline mode, file is cached."); + return 0; +} + /** * @changed only on HTTP. */ @@ -431,12 +448,20 @@ cache_download(struct rpki_cache *cache, struct rpki_uri *uri, bool *changed) case UT_RSYNC: if (strcmp(token, "rsync") != 0) return pr_val_err("Path is not rsync: %s", uri_get_local(uri)); + if (!config_get_rsync_enabled()) { + error = cache_check(uri); + goto end; + } node = cache->rsync = init_root(cache->rsync, "rsync"); recursive = true; break; case UT_HTTPS: if (strcmp(token, "https") != 0) return pr_val_err("Path is not HTTPS: %s", uri_get_local(uri)); + if (!config_get_http_enabled()) { + error = cache_check(uri); + goto end; + } node = cache->https = init_root(cache->https, "https"); recursive = false; break; @@ -517,6 +542,8 @@ download(struct rpki_cache *cache, struct rpki_uri *uri, bool use_rrdp, { int error; + pr_val_debug("Trying URL %s...", uri_get_global(uri)); + error = (use_rrdp && (uri_get_type(uri) == UT_HTTPS)) ? rrdp_update(uri) : cache_download(cache, uri, NULL); diff --git a/src/http/http.c b/src/http/http.c index 6aecba91..724185a4 100644 --- a/src/http/http.c +++ b/src/http/http.c @@ -416,9 +416,6 @@ http_download(struct rpki_uri *uri, bool *changed) changed = &__changed; *changed = false; - if (!config_get_http_enabled()) - return 0; /* Skip; caller will work with existing cache. */ - error = cache_tmpfile(&tmp_file_name); if (error) return error; diff --git a/src/rsync/rsync.c b/src/rsync/rsync.c index 9c1a874e..dbedd118 100644 --- a/src/rsync/rsync.c +++ b/src/rsync/rsync.c @@ -266,9 +266,6 @@ rsync_download(struct rpki_uri *uri) int child_status; int error; - if (!config_get_rsync_enabled()) - return 0; /* Skip; caller will work with existing cache. */ - /* Prepare everything for the child exec */ args = NULL; args_len = 0; diff --git a/test/mock.c b/test/mock.c index 328d580a..aafba65f 100644 --- a/test/mock.c +++ b/test/mock.c @@ -109,6 +109,7 @@ MOCK(config_get_local_repository, char const *, "tmp", void) MOCK(config_get_mode, enum mode, STANDALONE, void) MOCK_TRUE(config_get_rsync_enabled, void) MOCK_UINT(config_get_rsync_priority, 50, void) +MOCK_TRUE(config_get_http_enabled, void) MOCK_UINT(config_get_http_priority, 60, void) MOCK_NULL(config_get_output_roa, char const *, void) MOCK_NULL(config_get_output_bgpsec, char const *, void)