Radish alpha
H
rad:z3QDZAW2FAfuLvihrhiyDC9fAD8G9
HardenedBSD Package Manager
Radicle
Git
fetch: only select the fetchers once per repo
Baptiste Daroussin committed 3 years ago
commit 24d4b7787cd57d2adced5dc42592c5202c9c8f13
parent 6ac42b8
2 files changed +37 -30
modified libpkg/fetch.c
@@ -48,10 +48,7 @@
#include "private/utils.h"
#include "private/fetch.h"

-
static struct fetcher {
-
	const char *scheme;
-
	int (*open)(struct pkg_repo *, struct url *, off_t *);
-
} fetchers [] = {
+
static struct fetcher fetchers [] = {
	{
		"tcp",
		tcp_open,
@@ -82,7 +79,6 @@ static struct fetcher {
	},
};

-

int
pkg_fetch_file_tmp(struct pkg_repo *repo, const char *url, char *dest,
	time_t t)
@@ -178,7 +174,6 @@ pkg_fetch_file_to_fd(struct pkg_repo *repo, const char *url, int dest,
	off_t		 sz = 0;
	size_t		 buflen = 0;
	size_t		 left = 0;
-
	struct fetcher	*fetcher = NULL;
	struct pkg_repo	*fakerepo = NULL;

	FILE *remote = NULL;
@@ -198,19 +193,19 @@ pkg_fetch_file_to_fd(struct pkg_repo *repo, const char *url, int dest,
	 */

	pkg_debug(1, "Request to fetch %s", url);
-
	if (repo != NULL &&
-
		strncmp(URL_SCHEME_PREFIX, url, strlen(URL_SCHEME_PREFIX)) == 0) {
-
		if (repo->mirror_type != SRV) {
-
			pkg_emit_error("packagesite URL error for %s -- "
-
				       URL_SCHEME_PREFIX
-
				       ":// implies SRV mirror type", url);
-

-
			/* Too early for there to be anything to cleanup */
-
			return(EPKG_FATAL);
+
	if (repo != NULL) {
+
		if (strncmp(URL_SCHEME_PREFIX, url,
+
		    strlen(URL_SCHEME_PREFIX)) == 0) {
+
			if (repo->fetcher == NULL && repo->mirror_type != SRV) {
+
				pkg_emit_error("packagesite URL error for %s -- "
+
						URL_SCHEME_PREFIX
+
						":// implies SRV mirror type", url);
+

+
				/* Too early for there to be anything to cleanup */
+
				return(EPKG_FATAL);
+
			}
+
			url += strlen(URL_SCHEME_PREFIX);
		}
-

-
		url += strlen(URL_SCHEME_PREFIX);
-
		u = fetchParseURL(url);
	}

	if (u == NULL)
@@ -247,20 +242,26 @@ pkg_fetch_file_to_fd(struct pkg_repo *repo, const char *url, int dest,
	if (t != NULL)
		u->ims_time = *t;

-
	for (int i = 0; i < nitems(fetchers); i++) {
-
		if (strcmp(u->scheme, fetchers[i].scheme) == 0) {
-
			fetcher = &fetchers[i];
-
			if ((retcode = fetcher->open(repo, u, &sz)) != EPKG_OK)
-
				goto cleanup;
-
			remote = repo->ssh ? repo->ssh : repo->fh;
-
			break;
+
	if (repo != NULL) {
+
		for (int i = 0; i < nitems(fetchers); i++) {
+
			if (strcmp(u->scheme, fetchers[i].scheme) == 0) {
+
				repo->fetcher = &fetchers[i];
+
				if ((retcode = repo->fetcher->open(repo, u, &sz)) != EPKG_OK)
+
					goto cleanup;
+
				remote = repo->ssh ? repo->ssh : repo->fh;
+
				break;
+
			}
		}
+
		if (repo->fetcher == NULL) {
+
			pkg_emit_error("Unknown scheme: %s", u->scheme);
+
			return (EPKG_FATAL);
+
		}
+
	} else {
+
		if ((retcode = repo->fetcher->open(repo, u, &sz)) != EPKG_OK)
+
			goto cleanup;
+
		remote = repo->ssh ? repo->ssh : repo->fh;
	}
-
	if (fetcher == NULL) {
-
		pkg_emit_error("Unknown scheme: %s", u->scheme);
-
		return (EPKG_FATAL);
-
	}
-
	pkg_debug(1, "Fetch: fetcher chosen: %s", fetcher->scheme);
+
	pkg_debug(1, "Fetch: fetcher chosen: %s", repo->fetcher->scheme);

	if (strcmp(u->scheme, "ssh") != 0 && strcmp(u->scheme, "tcp") != 0 ) {
		if (t != NULL && u->ims_time != 0) {
modified libpkg/private/pkg.h
@@ -177,6 +177,11 @@ extern struct pkg_ctx ctx;

struct pkg_repo_it;
struct pkg_repo;
+
struct url;
+
struct fetcher {
+
	const char *scheme;
+
	int (*open)(struct pkg_repo *, struct url *, off_t *);
+
};
struct pkg_message;
typedef tll(struct pkg_message *) messages_t;

@@ -503,6 +508,7 @@ struct pkg_repo {
	struct pkg_repo_ops *ops;

	char *name;
+
	struct fetcher *fetcher;
	char *url;
	char *pubkey;
	mirror_t mirror_type;