Radish alpha
H
rad:z3QDZAW2FAfuLvihrhiyDC9fAD8G9
HardenedBSD Package Manager
Radicle
Git
Use the new events API to show fetching status.
jlaffaye committed 14 years ago
commit 929b50a6a47445e8d4a3b23beeaa36e5ebe83462
parent b8ca00c
8 files changed +41 -58
modified libpkg/fetch.c
@@ -15,7 +15,7 @@
#include "pkg_error.h"

int
-
pkg_fetch_file(const char *url, const char *dest, void *data, fetch_cb cb)
+
pkg_fetch_file(const char *url, const char *dest)
{
	int fd = -1;
	FILE *remote = NULL;
@@ -62,8 +62,8 @@ pkg_fetch_file(const char *url, const char *dest, void *data, fetch_cb cb)
		done += r;
		now = time(NULL);
		/* Only call the callback every second */
-
		if (cb != NULL && (now > last || done == st.size)) {
-
			cb(data, url, st.size, done, (now - begin_dl));
+
		if (now > last || done == st.size) {
+
			EMIT_FETCHING(url, st.size, done, (now - begin_dl));
			last = now;
		}
	}
modified libpkg/pkg.h
@@ -143,13 +143,6 @@ typedef enum {
	EPKG_DEPENDENCY,
} pkg_error_t;

-
/**
-
 * A function used as a callback by functions which fetch files from the
-
 * network.
-
 */
-
typedef void (*fetch_cb)(void *data, const char *url, off_t total, off_t done,
-
						 time_t elapsed);
-

typedef void (*status_cb)(void *data, struct pkg *pkg);

/**
@@ -539,8 +532,7 @@ int pkg_jobs(struct pkg_jobs *jobs, struct pkg **pkg);
 * Apply the jobs in the queue (fetch and install).
 * @return An error code.
 */
-
int pkg_jobs_apply(struct pkg_jobs *jobs, void *data, fetch_cb fcb,
-
				   status_cb scb);
+
int pkg_jobs_apply(struct pkg_jobs *jobs, status_cb scb);

/**
 * Archive formats options.
@@ -567,7 +559,7 @@ int pkg_create_fakeroot(const char *, pkg_formats, const char *, const char *);
 */
int pkg_delete(struct pkg *pkg, struct pkgdb *db, int force);

-
int pkg_repo_fetch(struct pkg *pkg, void *data, fetch_cb cb);
+
int pkg_repo_fetch(struct pkg *pkg);

/**
 * Get the value of a configuration key
@@ -583,7 +575,7 @@ int pkg_version_cmp(const char * const , const char * const);
 * Fetch a file.
 * @return An error code.
 */
-
int pkg_fetch_file(const char *url, const char *dest, void *data, fetch_cb cb);
+
int pkg_fetch_file(const char *url, const char *dest);

/* glue to deal with ports */
int ports_parse_plist(struct pkg *, char *);
@@ -625,9 +617,10 @@ int pkg_script_run(struct pkg *, pkg_script_t type);
typedef enum {
	/* informational */
	PKG_EVENT_INSTALL_BEGIN = 0,
+
	PKG_EVENT_FETCHING,
+
	/* errors */
	PKG_EVENT_ERROR,
	PKG_EVENT_ERRNO,
-
	/* errors */
	PKG_EVENT_ARCHIVE_COMP_UNSUP = 65536,
	PKG_EVENT_ALREADY_INSTALLED,
	PKG_EVENT_FAILED_CKSUM,
@@ -649,6 +642,12 @@ struct pkg_event {
			char *msg;
		} e_pkg_error;
		struct {
+
			const char *url;
+
			off_t total;
+
			off_t done;
+
			time_t elapsed;
+
		} e_fetching;
+
		struct {
			struct pkg *pkg;
		} e_already_installed;
		struct {
modified libpkg/pkg_event.h
@@ -34,6 +34,16 @@
	_EV_EMIT; \
	_EV_END

+
#define EMIT_FETCHING(u, t, d, e) \
+
	_EV_START; \
+
	ev.type = PKG_EVENT_FETCHING; \
+
	ev.e_fetching.url = u; \
+
	ev.e_fetching.total = t; \
+
	ev.e_fetching.done = d; \
+
	ev.e_fetching.elapsed = e; \
+
	_EV_EMIT; \
+
	_EV_END
+

#define EMIT_INSTALL_BEGIN(p) \
	_EV_START; \
	ev.type = PKG_EVENT_INSTALL_BEGIN; \
modified libpkg/pkg_jobs.c
@@ -73,7 +73,7 @@ pkg_jobs(struct pkg_jobs *j, struct pkg **pkg)
}

int
-
pkg_jobs_apply(struct pkg_jobs *j, void *data, fetch_cb fcb, status_cb scb)
+
pkg_jobs_apply(struct pkg_jobs *j, status_cb scb)
{
	struct pkg *p = NULL;
	struct pkg *pfile = NULL;
@@ -82,7 +82,7 @@ pkg_jobs_apply(struct pkg_jobs *j, void *data, fetch_cb fcb, status_cb scb)

	/* Fetch */
	while (pkg_jobs(j, &p) == EPKG_OK) {
-
		if (pkg_repo_fetch(p, data, fcb) != EPKG_OK)
+
		if (pkg_repo_fetch(p) != EPKG_OK)
			return (EPKG_FATAL);
	}

@@ -94,13 +94,13 @@ pkg_jobs_apply(struct pkg_jobs *j, void *data, fetch_cb fcb, status_cb scb)
				 pkg_get(p, PKG_REPOPATH));

		if (scb != NULL)
-
			scb(data, p);
+
			scb(NULL, p);
		if (pkg_add(j->db, path, &pfile) != EPKG_OK) {
			pkg_free(pfile);
			return (EPKG_FATAL);
		}
		if (scb != NULL)
-
			scb(data, pfile);
+
			scb(NULL, pfile);
	}

	pkg_free(pfile);
modified libpkg/pkg_repo.c
@@ -9,7 +9,7 @@
#include "pkg_private.h"

int
-
pkg_repo_fetch(struct pkg *pkg, void *data, fetch_cb cb)
+
pkg_repo_fetch(struct pkg *pkg)
{
	char dest[MAXPATHLEN];
	char cksum[65];
@@ -39,7 +39,7 @@ pkg_repo_fetch(struct pkg *pkg, void *data, fetch_cb cb)
	asprintf(&url, "%s/%s", pkg_config("PACKAGESITE"),
			 pkg_get(pkg, PKG_REPOPATH));

-
	retcode = pkg_fetch_file(url, dest, data, cb);
+
	retcode = pkg_fetch_file(url, dest);
	free(url);
	if (retcode != EPKG_OK)
		goto cleanup;
modified pkg/add.c
@@ -12,21 +12,6 @@

#include "add.h"

-
static void
-
fetch_status(__unused void *data, const char *url, off_t total, off_t done,
-
			 __unused time_t elapsed)
-
{
-
	unsigned int percent;
-

-
	percent = ((float)done / (float)total) * 100;
-
	printf("\rFetching %s... %d%%", url, percent);
-

-
	if (done == total)
-
		printf("\n");
-

-
	fflush(stdout);
-
}
-

static int
is_url(const char *pattern)
{
@@ -91,7 +76,7 @@ add_from_repo(const char *name)
		printf("%s-%s\n", pkg_get(pkg, PKG_NAME), pkg_get(pkg, PKG_VERSION));
	}

-
	if (pkg_jobs_apply(jobs, NULL, fetch_status, install_status) != EPKG_OK)
+
	if (pkg_jobs_apply(jobs, install_status) != EPKG_OK)
		pkg_error_warn("can not install");

	cleanup:
@@ -157,7 +142,7 @@ exec_add(int argc, char **argv)

	if (is_url(argv[1]) == EPKG_OK) {
		asprintf(&name, "./%s", basename(argv[1]));
-
		if (pkg_fetch_file(argv[1], name, NULL, &fetch_status) != EPKG_OK) {
+
		if (pkg_fetch_file(argv[1], name) != EPKG_OK) {
			pkg_error_warn("can not fetch %s", argv[1]);
			return (1);
		}
modified pkg/event.c
@@ -6,6 +6,7 @@
int
event_callback(void *data __unused, struct pkg_event *ev)
{
+
	unsigned int percent;

	switch(ev->type) {
	case PKG_EVENT_ERRNO:
@@ -14,6 +15,13 @@ event_callback(void *data __unused, struct pkg_event *ev)
	case PKG_EVENT_ERROR:
		warnx("%s", ev->e_pkg_error.msg);
		break;
+
	case PKG_EVENT_FETCHING:
+
		percent = ((float)ev->e_fetching.done / (float)ev->e_fetching.total) * 100;
+
		printf("\rFetching %s... %d%%", ev->e_fetching.url, percent);
+
		if (ev->e_fetching.done == ev->e_fetching.total)
+
			printf("\n");
+
		fflush(stdout);
+
		break;
	case PKG_EVENT_INSTALL_BEGIN:
		break;
	default:
modified pkg/update.c
@@ -21,24 +21,6 @@
		ARCHIVE_EXTRACT_TIME  |ARCHIVE_EXTRACT_ACL | \
		ARCHIVE_EXTRACT_FFLAGS|ARCHIVE_EXTRACT_XATTR)

-
static void
-
fetch_status(void *data, const char *url, off_t total, off_t done, time_t elapsed)
-
{
-
	unsigned int percent;
-

-
	elapsed = 0;
-

-
	int *size = (int *)data;
-
	*size = total;
-
	percent = ((float)done / (float)total) * 100;
-
	printf("\rFetching %s... %d%%", url, percent);
-

-
	if (done == total)
-
		printf("\n");
-

-
	fflush(stdout);
-
}
-

void
usage_update(void)
{
@@ -77,10 +59,9 @@ exec_update(int argc, char **argv)
	else
		snprintf(url, MAXPATHLEN, "%s/repo.txz", packagesite);

-
	
	tmp = mktemp(strdup("/tmp/repo.txz.XXXXXX"));

-
	if (pkg_fetch_file(url, tmp, NULL, &fetch_status) != EPKG_OK) {
+
	if (pkg_fetch_file(url, tmp) != EPKG_OK) {
		pkg_error_warn("can not fetch %s", url);
		retcode = 1;
	}