]> git.ipfire.org Git - thirdparty/FORT-validator.git/commitdiff
stat() to find out if a file is cached in offline mode
authorAlberto Leiva Popper <ydahhrk@gmail.com>
Tue, 14 Nov 2023 22:39:18 +0000 (16:39 -0600)
committerAlberto Leiva Popper <ydahhrk@gmail.com>
Wed, 15 Nov 2023 00:14:54 +0000 (18:14 -0600)
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.

src/cache/local_cache.c
src/http/http.c
src/rsync/rsync.c
test/mock.c

index fe454d0674b89bddfdc9aa26c2ee2398dfba01c4..373aaaed0749dc262d912d22711058a2506e6fed 100644 (file)
@@ -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);
index 6aecba91956d77e2757aa43a606e5944112b4c5f..724185a4647bd324ccd06631d251c8c5ac9f9776 100644 (file)
@@ -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;
index 9c1a874e7f9dcdedbc528937b164bd7fa23094de..dbedd1180f10722d20d7976481b4721f952b2faa 100644 (file)
@@ -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;
index 328d580a34e1d86f95eea22efea9ff5eec14644d..aafba65fb6d43ea12ab1aa5d4197a2475e7af8fc 100644 (file)
@@ -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)