Radish alpha
H
rad:z3QDZAW2FAfuLvihrhiyDC9fAD8G9
HardenedBSD Package Manager
Radicle
Git
fetch: isolate fetch details in specific functions
Baptiste Daroussin committed 3 years ago
commit f8cdf1d761794dd42662fddb13a177570f88445f
parent fb0fbeb
5 files changed +71 -55
modified libpkg/fetch.c
@@ -54,42 +54,49 @@ static struct fetcher fetchers [] = {
		tcp_open,
		NULL,
		fh_close,
+
		stdio_fetch,
	},
	{
		"ssh",
		ssh_open,
		NULL,
		fh_close,
+
		stdio_fetch,
	},
	{
		"pkg+https",
		fetch_open,
		fh_close,
		NULL,
+
		libfetch_fetch,
	},
	{
		"pkg+http",
		fetch_open,
		fh_close,
		NULL,
+
		libfetch_fetch,
	},
	{
		"https",
		fetch_open,
		fh_close,
		NULL,
+
		libfetch_fetch,
	},
	{
		"http",
		fetch_open,
		fh_close,
		NULL,
+
		libfetch_fetch,
	},
	{
		"file",
		file_open,
		fh_close,
		NULL,
+
		stdio_fetch,
	},
};

@@ -181,13 +188,8 @@ pkg_fetch_file_to_fd(struct pkg_repo *repo, const char *url, int dest,
	kvlist_t	 envtorestore = tll_init();
	stringlist_t	 envtounset = tll_init();
	char		*tmp;
-
	off_t		 done = 0;
-
	off_t		 r;
-
	char		 buf[8192];
	int		 retcode = EPKG_OK;
	off_t		 sz = 0;
-
	size_t		 buflen = 0;
-
	size_t		 left = 0;
	struct pkg_repo	*fakerepo = NULL;

	/* A URL of the form http://host.example.com/ where
@@ -265,60 +267,14 @@ pkg_fetch_file_to_fd(struct pkg_repo *repo, const char *url, int dest,
	}
	if ((retcode = repo->fetcher->open(repo, u, &sz)) != EPKG_OK)
		goto cleanup;
-
	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) {
-
			if (u->ims_time <= *t) {
-
				retcode = EPKG_UPTODATE;
-
				goto cleanup;
-
			} else
-
				*t = u->ims_time;
-
		}
-
	}
+
	pkg_debug(1, "Fetch: fetcher used: %s", repo->fetcher->scheme);

	if (sz <= 0 && size > 0)
		sz = size;

-
	pkg_emit_fetch_begin(url);
-
	pkg_emit_progress_start(NULL);
-
	if (offset > 0)
-
		done += offset;
-
	buflen = sizeof(buf);
-
	left = sizeof(buf);
-
	if (sz > 0)
-
		left = sz - done;
-
	while ((r = fread(buf, 1, left < buflen ? left : buflen, repo->fh)) > 0) {
-
		if (write(dest, buf, r) != r) {
-
			pkg_emit_errno("write", "");
-
			retcode = EPKG_FATAL;
-
			goto cleanup;
-
		}
-
		done += r;
-
		if (sz > 0) {
-
			left -= r;
-
			pkg_debug(4, "Read status: %jd over %jd", (intmax_t)done, (intmax_t)sz);
-
		} else
-
			pkg_debug(4, "Read status: %jd", (intmax_t)done);
-
		if (sz > 0)
-
			pkg_emit_progress_tick(done, sz);
-
	}
-

-
	if (r != 0) {
-
		pkg_emit_error("An error occurred while fetching package");
-
		retcode = EPKG_FATAL;
-
		goto cleanup;
-
	} else {
-
		pkg_emit_progress_tick(done, done);
-
	}
-
	pkg_emit_fetch_finished(url);
-

-
	if (strcmp(u->scheme, "ssh") != 0 && strcmp(u->scheme, "tcp") != 0
-
	    && ferror(repo->fh)) {
-
		pkg_emit_error("%s: %s", url, fetchLastErrString);
-
		retcode = EPKG_FATAL;
-
		goto cleanup;
-
	}
+
	retcode = repo->fetcher->fetch(repo, dest, url, u, sz, t);
+
	if (retcode == EPKG_OK)
+
		pkg_emit_fetch_finished(url);

cleanup:
	tll_foreach(envtorestore, k) {
modified libpkg/fetch_file.c
@@ -67,3 +67,46 @@ fh_close(struct pkg_repo *repo)
		fclose(repo->fh);
	repo->fh = NULL;
}
+

+
int
+
stdio_fetch(struct pkg_repo *repo, int dest, const char *url, struct url *u, off_t sz, time_t *t)
+
{
+
	char buf[8192];
+
	size_t buflen = 0, left = 0;
+
	off_t done = 0, r;
+

+
	if (t != NULL && u->ims_time != 0) {
+
		if (u->ims_time <= *t)
+
			return (EPKG_UPTODATE);
+
		*t = u->ims_time;
+
	}
+

+
	pkg_emit_fetch_begin(url);
+
	pkg_emit_progress_start(NULL);
+
	if (u->offset > 0)
+
		done += u->offset;
+
	buflen = sizeof(buf);
+
	left = sizeof(buf);
+
	if (sz > 0)
+
		left = sz - done;
+

+
	while ((r = fread(buf, 1, left < buflen ? left : buflen, repo->fh)) > 0) {
+
		if (write(dest, buf, r) != r) {
+
			pkg_emit_errno("write", "");
+
			return (EPKG_FATAL);
+
		}
+
		done += r;
+
		if (sz > 0) {
+
			left -= r;
+
			pkg_debug(4, "Read status: %jd over %jd", (intmax_t)done, (intmax_t)sz);
+
		} else
+
			pkg_debug(4, "Read status: %jd", (intmax_t)done);
+
		if (sz > 0)
+
			pkg_emit_progress_tick(done, sz);
+
	}
+
	if (r != 0) {
+
		pkg_emit_error("An error occurred while fetching package");
+
		return(EPKG_FATAL);
+
	}
+
	return (EPKG_OK);
+
}
modified libpkg/fetch_libfetch.c
@@ -234,3 +234,17 @@ fetch_open(struct pkg_repo *repo, struct url *u, off_t *sz)

	return (retcode);
}
+

+
int
+
libfetch_fetch(struct pkg_repo *repo, int dest, const char *url, struct url *u, off_t sz, time_t *t)
+
{
+
	int ret;
+

+
	ret = stdio_fetch(repo, dest, url, u, sz, t);
+

+
	if (ret == EPKG_OK && ferror(repo->fh)) {
+
		pkg_emit_error("%s: %s", url, fetchLastErrString);
+
		return (EPKG_FATAL);
+
	}
+
	return (ret);
+
}
modified libpkg/private/fetch.h
@@ -30,3 +30,5 @@ int ssh_open(struct pkg_repo *, struct url *, off_t *);
int file_open(struct pkg_repo *, struct url *, off_t *);
int fh_close(struct pkg_repo *);
int tcp_open(struct pkg_repo *, struct url *, off_t *);
+
int stdio_fetch(struct pkg_repo *, int dest, const char *url, struct url *u, off_t sz, time_t *t);
+
int libfetch_fetch(struct pkg_repo *, int dest, const char *url, struct url *u, off_t sz, time_t *t);
modified libpkg/private/pkg.h
@@ -183,6 +183,7 @@ struct fetcher {
	int (*open)(struct pkg_repo *, struct url *, off_t *);
	int (*close)(struct pkg_repo *);
	int (*cleanup)(struct pkg_repo *);
+
	int (*fetch)(struct pkg_repo *repo, int dest, const char *url, struct url *u, off_t sz, time_t *t);
};
struct pkg_message;
typedef tll(struct pkg_message *) messages_t;