aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorTimo Teräs <timo.teras@iki.fi>2024-04-05 11:20:26 +0300
committerTimo Teräs <timo.teras@iki.fi>2024-04-10 15:35:35 +0300
commit759c31e5f320eab2253ab9780815ce88e40fa7f7 (patch)
treece2af414791dd619735b4cf8e0bfcf12b2c01d97
parent53061074481e42652e76fc274d7eda7c37a045ff (diff)
apk, io_url: abstract libfetch away from apk.c
-rw-r--r--src/apk.c28
-rw-r--r--src/apk_io.h6
-rw-r--r--src/io.c7
-rw-r--r--src/io_url_libfetch.c46
4 files changed, 62 insertions, 25 deletions
diff --git a/src/apk.c b/src/apk.c
index ee6f643..a26f637 100644
--- a/src/apk.c
+++ b/src/apk.c
@@ -19,8 +19,6 @@
#include <unistd.h>
#include <sys/stat.h>
-#include <fetch.h>
-
#include "apk_defines.h"
#include "apk_database.h"
#include "apk_applet.h"
@@ -197,7 +195,7 @@ static int option_parse_global(void *ctx, struct apk_ctx *ac, int opt, const cha
ac->flags |= APK_NO_CACHE;
break;
case OPT_GLOBAL_no_check_certificate:
- fetch_no_check_certificate();
+ apk_io_url_no_check_certificate();
break;
case OPT_GLOBAL_cache_dir:
ac->cache_dir = optarg;
@@ -211,7 +209,7 @@ static int option_parse_global(void *ctx, struct apk_ctx *ac, int opt, const cha
ac->cache_max_age = atoi(optarg) * 60;
break;
case OPT_GLOBAL_timeout:
- fetchTimeout = atoi(optarg);
+ apk_io_url_set_timeout(atoi(optarg));
break;
case OPT_GLOBAL_arch:
ac->arch = optarg;
@@ -474,18 +472,9 @@ static int remove_empty_strings(int count, char **args)
return j;
}
-static void fetch_redirect(int code, const struct url *cur, const struct url *next)
+static void redirect_callback(int code, const char *url)
{
- char *url;
-
- switch (code) {
- case 301: // Moved Permanently
- case 308: // Permanent Redirect
- url = fetchStringifyURL(next);
- apk_warn(&ctx.out, "Permanently redirected to %s", url);
- free(url);
- break;
- }
+ apk_warn(&ctx.out, "Permanently redirected to %s", url);
}
int main(int argc, char **argv)
@@ -520,9 +509,9 @@ int main(int argc, char **argv)
apk_crypto_init();
setup_automatic_flags(&ctx);
- fetchTimeout = 60;
- fetchRedirectMethod = fetch_redirect;
- fetchConnectionCacheInit(32, 4);
+ apk_io_url_init();
+ apk_io_url_set_timeout(60);
+ apk_io_url_set_redirect_callback(redirect_callback);
r = parse_options(argc, argv, applet, applet_ctx, &ctx);
if (r != 0) goto err;
@@ -610,7 +599,7 @@ int main(int argc, char **argv)
apk_string_array_resize(&args, argc);
memcpy(args->item, argv, argc * sizeof(*argv));
- fetchRedirectMethod = NULL;
+ apk_io_url_set_redirect_callback(NULL);
r = applet->main(applet_ctx, &ctx, args);
signal(SIGINT, SIG_IGN);
@@ -625,7 +614,6 @@ err:
if (r == -ESHUTDOWN) r = 0;
if (applet_ctx) free(applet_ctx);
- fetchConnectionCacheClose();
apk_ctx_free(&ctx);
apk_string_array_free(&args);
free(apk_argv);
diff --git a/src/apk_io.h b/src/apk_io.h
index 3ef53a8..0520503 100644
--- a/src/apk_io.h
+++ b/src/apk_io.h
@@ -127,6 +127,12 @@ static inline int apk_istream_close_error(struct apk_istream *is, int r)
return apk_istream_close(is);
}
+void apk_io_url_init(void);
+void apk_io_url_set_timeout(int timeout);
+void apk_io_url_set_redirect_callback(void (*cb)(int, const char *));
+void apk_io_url_no_check_certificate(void);
+struct apk_istream *apk_io_url_istream(const char *url, time_t since);
+
struct apk_segment_istream {
struct apk_istream is;
struct apk_istream *pis;
diff --git a/src/io.c b/src/io.c
index bec0ea4..9afb4d6 100644
--- a/src/io.c
+++ b/src/io.c
@@ -613,6 +613,13 @@ struct apk_istream *apk_istream_from_fd(int fd)
return &fis->is;
}
+struct apk_istream *apk_istream_from_fd_url_if_modified(int atfd, const char *url, time_t since)
+{
+ const char *fn = apk_url_local_file(url);
+ if (fn != NULL) return apk_istream_from_file(atfd, fn);
+ return apk_io_url_istream(url, since);
+}
+
struct apk_istream *__apk_istream_from_file(int atfd, const char *file, int try_mmap)
{
int fd;
diff --git a/src/io_url_libfetch.c b/src/io_url_libfetch.c
index 0016f24..67f9664 100644
--- a/src/io_url_libfetch.c
+++ b/src/io_url_libfetch.c
@@ -88,7 +88,7 @@ static const struct apk_istream_ops fetch_istream_ops = {
.close = fetch_close,
};
-static struct apk_istream *apk_istream_fetch(const char *url, time_t since)
+struct apk_istream *apk_io_url_istream(const char *url, time_t since)
{
struct apk_fetch_istream *fis = NULL;
struct url *u;
@@ -135,9 +135,45 @@ err:
return ERR_PTR(rc);
}
-struct apk_istream *apk_istream_from_fd_url_if_modified(int atfd, const char *url, time_t since)
+static void (*io_url_redirect_callback)(int, const char *);
+
+static void fetch_redirect(int code, const struct url *cur, const struct url *next)
+{
+ char *url;
+
+ switch (code) {
+ case 301: // Moved Permanently
+ case 308: // Permanent Redirect
+ url = fetchStringifyURL(next);
+ io_url_redirect_callback(code, url);
+ free(url);
+ break;
+ }
+}
+
+void apk_io_url_no_check_certificate(void)
+{
+ fetch_no_check_certificate();
+}
+
+void apk_io_url_set_timeout(int timeout)
+{
+ fetchTimeout = timeout;
+}
+
+void apk_io_url_set_redirect_callback(void (*cb)(int, const char *))
+{
+ fetchRedirectMethod = cb ? fetch_redirect : NULL;
+ io_url_redirect_callback = cb;
+}
+
+static void apk_io_url_fini(void)
+{
+ fetchConnectionCacheClose();
+}
+
+void apk_io_url_init(void)
{
- if (apk_url_local_file(url) != NULL)
- return apk_istream_from_file(atfd, apk_url_local_file(url));
- return apk_istream_fetch(url, since);
+ fetchConnectionCacheInit(32, 4);
+ atexit(apk_io_url_fini);
}