Radish alpha
H
rad:z3QDZAW2FAfuLvihrhiyDC9fAD8G9
HardenedBSD Package Manager
Radicle
Git
fetch: isolate the close code in a callback
Baptiste Daroussin committed 3 years ago
commit cd3a5f31c8d8ddd6e65c13fce272f37ff23940fe
parent 5d1e133
5 files changed +43 -36
modified libpkg/fetch.c
@@ -1,5 +1,5 @@
/*-
-
 * Copyright (c) 2012-2020 Baptiste Daroussin <bapt@FreeBSD.org>
+
 * Copyright (c) 2012-2023 Baptiste Daroussin <bapt@FreeBSD.org>
 * Copyright (c) 2011-2012 Julien Laffaye <jlaffaye@FreeBSD.org>
 * Copyright (c) 2014 Vsevolod Stakhov <vsevolod@FreeBSD.org>
 * All rights reserved.
@@ -52,30 +52,37 @@ static struct fetcher fetchers [] = {
	{
		"tcp",
		tcp_open,
+
		NULL,
	},
	{
		"ssh",
		ssh_open,
+
		NULL,
	},
	{
		"pkg+https",
		fetch_open,
+
		fh_close,
	},
	{
		"pkg+http",
		fetch_open,
+
		fh_close,
	},
	{
		"https",
		fetch_open,
+
		fh_close,
	},
	{
		"http",
		fetch_open,
+
		fh_close,
	},
	{
		"file",
		file_open,
+
		fh_close,
	},
};

@@ -176,8 +183,6 @@ pkg_fetch_file_to_fd(struct pkg_repo *repo, const char *url, int dest,
	size_t		 left = 0;
	struct pkg_repo	*fakerepo = NULL;

-
	FILE *remote = NULL;
-

	/* A URL of the form http://host.example.com/ where
	 * host.example.com does not resolve as a simple A record is
	 * not valid according to RFC 2616 Section 3.2.2.  Our usage
@@ -253,7 +258,6 @@ 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;
-
	remote = repo->ssh ? repo->ssh : repo->fh;
	pkg_debug(1, "Fetch: fetcher chosen: %s", repo->fetcher->scheme);

	if (strcmp(u->scheme, "ssh") != 0 && strcmp(u->scheme, "tcp") != 0 ) {
@@ -277,7 +281,7 @@ pkg_fetch_file_to_fd(struct pkg_repo *repo, const char *url, int dest,
	left = sizeof(buf);
	if (sz > 0)
		left = sz - done;
-
	while ((r = fread(buf, 1, left < buflen ? left : buflen, remote)) > 0) {
+
	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;
@@ -302,8 +306,8 @@ pkg_fetch_file_to_fd(struct pkg_repo *repo, const char *url, int dest,
	}
	pkg_emit_fetch_finished(url);

-
	if (strcmp(u->scheme, "ssh") != 0 & strcmp(u->scheme, "tcp") != 0
-
	    && ferror(remote)) {
+
	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;
@@ -321,12 +325,8 @@ cleanup:
	}
	tll_free(envtounset);

-
	if (u != NULL) {
-
		if (remote != NULL &&  repo != NULL && remote != repo->ssh) {
-
			fclose(remote);
-
			repo->fh = NULL;
-
		}
-
	}
+
	if (repo->fetcher != NULL && repo->fetcher->close != NULL)
+
		repo->fetcher->close(repo);
	free(fakerepo);

	if (retcode == EPKG_OK) {
modified libpkg/fetch_file.c
@@ -1,5 +1,5 @@
/*-
-
 * Copyright (c) 2020 Baptiste Daroussin <bapt@FreeBSD.org>
+
 * Copyright (c) 2020-2023 Baptiste Daroussin <bapt@FreeBSD.org>
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
@@ -59,3 +59,11 @@ file_open(struct pkg_repo *repo, struct url *u, off_t *sz)
		return (EPKG_FATAL);
	return (EPKG_OK);
}
+

+
void
+
fh_close(struct pkg_repo *repo)
+
{
+
	if (repo->fh != NULL)
+
		fclose(repo->fh);
+
	repo->fh = NULL;
+
}
modified libpkg/fetch_ssh.c
@@ -99,15 +99,15 @@ tcp_connect(struct pkg_repo *repo, struct url *u)
	}
	repo->sshio.in = dup(sd);
	repo->sshio.out = dup(sd);
-
	repo->ssh = funopen(repo, ssh_read, ssh_write, NULL, tcp_close);
+
	repo->fh = funopen(repo, ssh_read, ssh_write, NULL, tcp_close);

	retcode = EPKG_FATAL;
-
	if (repo->ssh == NULL) {
+
	if (repo->fh == NULL) {
		pkg_emit_errno("Failed to open stream", "tcp_connect");
		goto tcp_cleanup;
	}

-
	if (getline(&line, &linecap, repo->ssh) > 0) {
+
	if (getline(&line, &linecap, repo->fh) > 0) {
		if (strncmp(line, "ok:", 3) != 0) {
			pkg_debug(1, "SSH> server rejected, got: %s", line);
			goto tcp_cleanup;
@@ -119,9 +119,9 @@ tcp_connect(struct pkg_repo *repo, struct url *u)
	}
	retcode = EPKG_OK;
tcp_cleanup:
-
	if (retcode == EPKG_FATAL && repo->ssh != NULL) {
-
		fclose(repo->ssh);
-
		repo->ssh = NULL;
+
	if (retcode == EPKG_FATAL && repo->fh != NULL) {
+
		fclose(repo->fh);
+
		repo->fh = NULL;
	}
	free(line);
	return (retcode);
@@ -202,13 +202,13 @@ ssh_connect(struct pkg_repo *repo, struct url *u)
	repo->sshio.out = sshin[1];
	set_nonblocking(repo->sshio.in);

-
	repo->ssh = funopen(repo, ssh_read, ssh_write, NULL, ssh_close);
-
	if (repo->ssh == NULL) {
+
	repo->fh = funopen(repo, ssh_read, ssh_write, NULL, ssh_close);
+
	if (repo->fh == NULL) {
		pkg_emit_errno("Failed to open stream", "start_ssh");
		goto ssh_cleanup;
	}

-
	if (getline(&line, &linecap, repo->ssh) > 0) {
+
	if (getline(&line, &linecap, repo->fh) > 0) {
		if (strncmp(line, "ok:", 3) != 0) {
			pkg_debug(1, "SSH> server rejected, got: %s", line);
			goto ssh_cleanup;
@@ -221,9 +221,9 @@ ssh_connect(struct pkg_repo *repo, struct url *u)
	retcode = EPKG_OK;

ssh_cleanup:
-
	if (retcode == EPKG_FATAL && repo->ssh != NULL) {
-
		fclose(repo->ssh);
-
		repo->ssh = NULL;
+
	if (retcode == EPKG_FATAL && repo->fh != NULL) {
+
		fclose(repo->fh);
+
		repo->fh = NULL;
	}
	free(line);
	return (retcode);
@@ -240,7 +240,7 @@ pkgprotocol_open(struct pkg_repo *repo, struct url *u, off_t *sz,
	int retcode = EPKG_FATAL;

	pkg_debug(1, "SSH> tcp_open");
-
	if (repo->ssh == NULL)
+
	if (repo->fh == NULL)
		retcode = proto_connect(repo, u);
	else
		retcode = EPKG_OK;
@@ -249,8 +249,8 @@ pkgprotocol_open(struct pkg_repo *repo, struct url *u, off_t *sz,
		return (retcode);

	pkg_debug(1, "SSH> get %s %" PRIdMAX "", u->doc, (intmax_t)u->ims_time);
-
	fprintf(repo->ssh, "get %s %" PRIdMAX "\n", u->doc, (intmax_t)u->ims_time);
-
	if ((linelen = getline(&line, &linecap, repo->ssh)) > 0) {
+
	fprintf(repo->fh, "get %s %" PRIdMAX "\n", u->doc, (intmax_t)u->ims_time);
+
	if ((linelen = getline(&line, &linecap, repo->fh)) > 0) {
		if (line[linelen -1 ] == '\n')
			line[linelen -1 ] = '\0';

@@ -300,7 +300,7 @@ tcp_close(void *data)
	write(repo->sshio.out, "quit\n", 5);
	close(repo->sshio.out);
	close(repo->sshio.in);
-
	repo->ssh = NULL;
+
	repo->fh = NULL;
	return (0);
}

@@ -319,7 +319,7 @@ ssh_close(void *data)
	close(repo->sshio.out);
	close(repo->sshio.in);

-
	repo->ssh = NULL;
+
	repo->fh = NULL;

	return (WEXITSTATUS(pstat));
}
modified libpkg/private/fetch.h
@@ -1,5 +1,5 @@
/*-
-
 * Copyright (c) 2020 Baptiste Daroussin <bapt@FreeBSD.org>
+
 * Copyright (c) 2020-2023 Baptiste Daroussin <bapt@FreeBSD.org>
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
@@ -23,12 +23,10 @@
 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */

-
#ifndef _PKG_FETCH_H
-
#define _PKG_FETCH_H
+
#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 fh_close(struct pkg_repo *);
int tcp_open(struct pkg_repo *, struct url *, off_t *);
-

-
#endif
modified libpkg/private/pkg.h
@@ -181,6 +181,7 @@ struct url;
struct fetcher {
	const char *scheme;
	int (*open)(struct pkg_repo *, struct url *, off_t *);
+
	int (*close)(struct pkg_repo *);
};
struct pkg_message;
typedef tll(struct pkg_message *) messages_t;