Radish alpha
H
rad:z3QDZAW2FAfuLvihrhiyDC9fAD8G9
HardenedBSD Package Manager
Radicle
Git
fetch: remove libfetch usage
Baptiste Daroussin committed 2 years ago
commit e86eab452a5c055e1c0b749272fd057c0e91559a
parent 5b3d2ff
8 files changed +55 -67
modified libpkg/Makefile.autosetup
@@ -44,7 +44,6 @@ SRCS= backup_lib.c \
	rcscripts.c \
	flags.c \
	fetch_ssh.c \
-
	fetch_libfetch.c \
	fetch_libcurl.c \
	fetch_file.c \
	triggers.c \
modified libpkg/fetch.c
@@ -36,7 +36,6 @@
#include <errno.h>
#include <stdio.h>
#include <string.h>
-
#include <fetch.h>
#include <paths.h>
#include <poll.h>

@@ -183,7 +182,6 @@ int
pkg_fetch_file_to_fd(struct pkg_repo *repo, const char *url, int dest,
    time_t *t, ssize_t offset, int64_t size, bool silent)
{
-
	struct url	*u = NULL;
	struct pkg_kv	*kv;
	kvlist_t	 envtorestore = tll_init();
	stringlist_t	 envtounset = tll_init();
@@ -239,11 +237,6 @@ pkg_fetch_file_to_fd(struct pkg_repo *repo, const char *url, int dest,
		}
		url += strlen(URL_SCHEME_PREFIX);
	}
-
	if (u == NULL)
-
		u = fetchParseURL(url);
-

-
	if (offset > 0)
-
		u->offset = offset;

	repo->silent = silent;
	tll_foreach(repo->env, k) {
@@ -258,23 +251,14 @@ pkg_fetch_file_to_fd(struct pkg_repo *repo, const char *url, int dest,
		setenv(k->item->key, k->item->value, 1);
	}

-
	if (u == NULL) {
-
		pkg_emit_error("%s: parse error", url);
-
		/* Too early for there to be anything to cleanup */
-
		return(EPKG_FATAL);
-
	}
-

-
	if (t != NULL)
-
		u->ims_time = *t;
-

-
	if ((retcode = repo->fetcher->open(repo, u, &sz)) != EPKG_OK)
+
	if ((retcode = repo->fetcher->open(repo, url, &sz, t)) != EPKG_OK)
		goto cleanup;
	pkg_debug(1, "Fetch: fetcher used: %s", repo->fetcher->scheme);

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

-
	retcode = repo->fetcher->fetch(repo, dest, url, u, sz, t);
+
	retcode = repo->fetcher->fetch(repo, dest, url, sz, offset, t);
	if (retcode == EPKG_OK)
		pkg_emit_fetch_finished(url);

@@ -308,8 +292,5 @@ cleanup:
		futimes(dest, ftimes);
	}

-
	/* restore original doc */
-
	fetchFreeURL(u);
-

	return (retcode);
}
modified libpkg/fetch_file.c
@@ -36,25 +36,29 @@
#include "private/utils.h"

int
-
file_open(struct pkg_repo *repo, struct url *u, off_t *sz)
+
file_open(struct pkg_repo *repo, const char *url, size_t *sz,
+
    time_t *t)
{
	struct stat st;
+
	const char *u = url;

-
	if (stat(u->doc, &st) == -1) {
+
	if (strlen(u) > 5)
+
		u += 5; /* file: */
+
	if (*u != '/') {
+
		pkg_emit_error("invalid url: '%s'\n", url);
+
		return (EPKG_FATAL);
+
	}
+
	if (stat(u, &st) == -1) {
		if (!repo->silent)
-
			pkg_emit_error("%s://%s%s%s%s: %s",
-
			    u->scheme,
-
			    u->user,
-
			    u->user[0] != '\0' ? "@" : "",
-
			    u->host,
-
			    u->doc,
+
			pkg_emit_error("%s: %s", url,
			    strerror(errno));
		return (EPKG_FATAL);
	}
	*sz = st.st_size;
-
	u->ims_time = st.st_mtime;
+
	if (st.st_mtime <= *t)
+
		return (EPKG_UPTODATE);

-
	repo->fh = fopen(u->doc, "re");
+
	repo->fh = fopen(u, "re");
	if (repo->fh == NULL)
		return (EPKG_FATAL);
	return (EPKG_OK);
@@ -69,22 +73,16 @@ fh_close(struct pkg_repo *repo)
}

int
-
stdio_fetch(struct pkg_repo *repo, int dest, const char *url, struct url *u, off_t sz, time_t *t)
+
stdio_fetch(struct pkg_repo *repo, int dest, const char *url, off_t sz, off_t offset, time_t *t __unused)
{
	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;
+
	if (offset > 0)
+
		done += offset;
	buflen = sizeof(buf);
	left = sizeof(buf);
	if (sz > 0)
modified libpkg/fetch_libcurl.c
@@ -85,7 +85,8 @@ curl_progress_cb(void *userdata, curl_off_t dltotal, curl_off_t dlnow, curl_off_


int
-
curl_open(struct pkg_repo *repo, struct url *u __unused, off_t *sz __unused)
+
curl_open(struct pkg_repo *repo, const char *u __unused,
+
    size_t *sz __unused, time_t *t __unused)
{
	CURLM *cm;
	pkg_debug(1, "curl_open");
@@ -105,7 +106,7 @@ curl_open(struct pkg_repo *repo, struct url *u __unused, off_t *sz __unused)
}

int
-
curl_fetch(struct pkg_repo *repo, int dest, const char *url, struct url *u, off_t sz, time_t *t)
+
curl_fetch(struct pkg_repo *repo, int dest, const char *url, off_t sz, off_t offset __unused, time_t *t)
{
	CURL *cl;
	CURLM *cm = NULL;
modified libpkg/fetch_libfetch.c
@@ -221,7 +221,7 @@ cleanup:
}

int
-
fetch_open(struct pkg_repo *repo, struct url *u, off_t *sz)
+
fetch_open(struct pkg_repo *repo, struct url *u, off_t *sz, time_t *t)
{
	int retcode = EPKG_FATAL;

modified libpkg/fetch_ssh.c
@@ -46,6 +46,7 @@
#include "private/pkg.h"
#include "private/fetch.h"
#include "private/utils.h"
+
#include "yuarel.h"

static int ssh_read(void *data, char *buf, int len);
static int ssh_write(void *data, const char *buf, int l);
@@ -53,7 +54,7 @@ static int ssh_close(void *data);
static int tcp_close(void *data);

static int
-
tcp_connect(struct pkg_repo *repo, struct url *u)
+
tcp_connect(struct pkg_repo *repo, struct yuarel *u)
{
	char *line = NULL;
	size_t linecap = 0;
@@ -128,7 +129,7 @@ tcp_cleanup:
}

static int
-
ssh_connect(struct pkg_repo *repo, struct url *u)
+
ssh_connect(struct pkg_repo *repo, struct yuarel *u)
{
	char *line = NULL;
	size_t linecap = 0;
@@ -173,8 +174,8 @@ ssh_connect(struct pkg_repo *repo, struct url *u)
			fputs("-6 ", cmd->fp);
		if (u->port > 0)
			fprintf(cmd->fp, "-p %d ", u->port);
-
		if (u->user[0] != '\0')
-
			fprintf(cmd->fp, "%s@", u->user);
+
		if (u->username != NULL)
+
			fprintf(cmd->fp, "%s@", u->username);
		fprintf(cmd->fp, "%s pkg ssh", u->host);
		cmdline = xstring_get(cmd);
		pkg_debug(1, "Fetch: running '%s'", cmdline);
@@ -230,26 +231,34 @@ ssh_cleanup:
}

static int
-
pkgprotocol_open(struct pkg_repo *repo, struct url *u, off_t *sz,
-
    int (*proto_connect)(struct pkg_repo *, struct url *))
+
pkgprotocol_open(struct pkg_repo *repo, const char *u, off_t *sz,
+
    int (*proto_connect)(struct pkg_repo *, struct yuarel *), time_t *t)
{
	char *line = NULL;
	size_t linecap = 0;
	size_t linelen;
	const char *errstr;
	int retcode = EPKG_FATAL;
+
	struct yuarel url;
+
	char *url_to_free = xstrdup(u);
+

+
	if (yuarel_parse(&url, url_to_free) == -1) {
+
		free(url_to_free);
+
		pkg_emit_error("Invalid url: '%s'", u);
+
		return (EPKG_FATAL);
+
	}

	pkg_debug(1, "SSH> tcp_open");
	if (repo->fh == NULL)
-
		retcode = proto_connect(repo, u);
+
		retcode = proto_connect(repo, &url);
	else
		retcode = EPKG_OK;

	if (retcode != EPKG_OK)
		return (retcode);

-
	pkg_debug(1, "SSH> get %s %" PRIdMAX "", u->doc, (intmax_t)u->ims_time);
-
	fprintf(repo->fh, "get %s %" PRIdMAX "\n", u->doc, (intmax_t)u->ims_time);
+
	pkg_debug(1, "SSH> get %s %" PRIdMAX "", url.path, (intmax_t)*t);
+
	fprintf(repo->fh, "get %s %" PRIdMAX "\n", url.path, (intmax_t)*t);
	if ((linelen = getline(&line, &linecap, repo->fh)) > 0) {
		if (line[linelen -1 ] == '\n')
			line[linelen -1 ] = '\0';
@@ -276,20 +285,21 @@ pkgprotocol_open(struct pkg_repo *repo, struct url *u, off_t *sz,
	}

out:
+
	free(url_to_free);
	free(line);
	return (retcode);
}

int
-
tcp_open(struct pkg_repo *repo, struct url *u, off_t *sz)
+
tcp_open(struct pkg_repo *repo, const char *u, off_t *sz, time_t *t)
{
-
	return (pkgprotocol_open(repo, u, sz, tcp_connect));
+
	return (pkgprotocol_open(repo, u, sz, tcp_connect, t));
}

int
-
ssh_open(struct pkg_repo *repo, struct url *u, off_t *sz)
+
ssh_open(struct pkg_repo *repo, const char *u, off_t *sz, time_t *t)
{
-
	return (pkgprotocol_open(repo, u, sz, ssh_connect));
+
	return (pkgprotocol_open(repo, u, sz, ssh_connect, t));
}

static int
modified libpkg/private/fetch.h
@@ -25,13 +25,12 @@

#pragma once

-
int fetch_open(struct pkg_repo *, struct url *, off_t *);
-
int ssh_open(struct pkg_repo *, struct url *, off_t *);
-
int file_open(struct pkg_repo *, struct url *, off_t *);
+
int fetch_open(struct pkg_repo *, const char *, off_t *, time_t *t);
+
int ssh_open(struct pkg_repo *, const char *, off_t *, time_t *t);
+
int file_open(struct pkg_repo *, const char *, off_t *, time_t *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);
-
int curl_open(struct pkg_repo *, struct url *, off_t *);
-
int curl_fetch(struct pkg_repo *, int dest, const char *url, struct url *u, off_t sz, time_t *t);
+
int tcp_open(struct pkg_repo *, const char *, off_t *, time_t *t);
+
int stdio_fetch(struct pkg_repo *, int dest, const char *url, off_t sz, off_t offset, time_t *t);
+
int curl_open(struct pkg_repo *, const char *, off_t *, time_t *t);
+
int curl_fetch(struct pkg_repo *, int dest, const char *url, off_t sz, off_t offset, time_t *t);
int curl_cleanup(struct pkg_repo *);
modified libpkg/private/pkg.h
@@ -180,10 +180,10 @@ struct pkg_repo;
struct url;
struct fetcher {
	const char *scheme;
-
	int (*open)(struct pkg_repo *, struct url *, off_t *);
+
	int (*open)(struct pkg_repo *, const char *url, off_t *, time_t *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);
+
	int (*fetch)(struct pkg_repo *repo, int dest, const char *url, off_t sz, off_t offset, time_t *t);
};
struct pkg_message;
typedef tll(struct pkg_message *) messages_t;