Radish alpha
H
rad:z3QDZAW2FAfuLvihrhiyDC9fAD8G9
HardenedBSD Package Manager
Radicle
Git
Layered fetch API.
Julien Laffaye committed 13 years ago
commit 9694ec166ff7f058eb318a86fac4a73ea7899ee6
parent cedfbc0
3 files changed +29 -21
modified libpkg/fetch.c
@@ -36,12 +36,34 @@

#include "pkg.h"
#include "private/event.h"
+
#include "private/pkg.h"
#include "private/utils.h"

int
pkg_fetch_file(const char *url, const char *dest, time_t t)
{
	int fd = -1;
+
	int retcode = EPKG_FATAL;
+

+
	if ((fd = open(dest, O_WRONLY|O_CREAT|O_TRUNC|O_EXCL, 0600)) == -1) {
+
		pkg_emit_errno("open", dest);
+
		return(EPKG_FATAL);
+
	}
+

+
	retcode = pkg_fetch_file_to_fd(url, fd, t);
+

+
	close(fd);
+

+
	/* Remove local file if fetch failed */
+
	if (retcode != EPKG_OK)
+
		unlink(dest);
+

+
	return (retcode);
+
}
+

+
int
+
pkg_fetch_file_to_fd(const char *url, int dest, time_t t)
+
{
	FILE *remote = NULL;
	struct url *u;
	struct url_stat st;
@@ -67,11 +89,6 @@ pkg_fetch_file(const char *url, const char *dest, time_t t)

	retry = max_retry;

-
	if ((fd = open(dest, O_WRONLY|O_CREAT|O_TRUNC, 0600)) == -1) {
-
		pkg_emit_errno("open", dest);
-
		return(EPKG_FATAL);
-
	}
-

	u = fetchParseURL(url);
	while (remote == NULL) {
		if (retry == max_retry) {
@@ -119,8 +136,8 @@ pkg_fetch_file(const char *url, const char *dest, time_t t)
		if ((r = fread(buf, 1, sizeof(buf), remote)) < 1)
			break;

-
		if (write(fd, buf, r) != r) {
-
			pkg_emit_errno("write", dest);
+
		if (write(dest, buf, r) != r) {
+
			pkg_emit_errno("write", "");
			retcode = EPKG_FATAL;
			goto cleanup;
		}
@@ -142,17 +159,10 @@ pkg_fetch_file(const char *url, const char *dest, time_t t)

	cleanup:

-
	if (fd > 0)
-
		close(fd);
-

	if (remote != NULL)
		fclose(remote);

	fetchFreeURL(u);

-
	/* Remove local file if fetch failed */
-
	if (retcode != EPKG_OK)
-
		unlink(dest);
-

	return (retcode);
}
modified libpkg/private/pkg.h
@@ -193,6 +193,7 @@ int pkg_delete(struct pkg *pkg, struct pkgdb *db, unsigned flags);
#define PKG_DELETE_FORCE (1<<0)
#define PKG_DELETE_UPGRADE (1<<1)

+
int pkg_fetch_file_to_fd(const char *url, int dest, time_t t);
int pkg_repo_fetch(struct pkg *pkg);

int pkg_start_stop_rc_scripts(struct pkg *, pkg_rc_attr attr);
modified libpkg/update.c
@@ -100,7 +100,6 @@ pkg_update(const char *name, const char *packagesite, bool force)
		    "aborting update.\n", tmp);
		return (EPKG_FATAL);
	}
-
	close(fd);

	if (pkg_config_string(PKG_CONFIG_DBDIR, &dbdir) != EPKG_OK) {
		pkg_emit_error("Cant get dbdir config entry");
@@ -121,12 +120,10 @@ pkg_update(const char *name, const char *packagesite, bool force)
		}
	}

-
	if ((rc = pkg_fetch_file(url, tmp, t)) != EPKG_OK) {
-
		/*
-
		 * No need to unlink(tmp) here as it is already
-
		 * done in pkg_fetch_file() in case fetch failed.
-
		 */
-
		return (rc);
+
	rc = pkg_fetch_file_to_fd(url, fd, t);
+
	close(fd);
+
	if (rc != EPKG_OK) {
+
		goto cleanup;
	}

	a = archive_read_new();