Radish alpha
H
rad:z3QDZAW2FAfuLvihrhiyDC9fAD8G9
HardenedBSD Package Manager
Radicle
Git
Fetch files.
jlaffaye committed 15 years ago
commit 634e04267cec546110b8fb42736467df3ba2c948
parent 6edf08f
5 files changed +85 -95
modified docs/TODO
@@ -2,10 +2,6 @@ ALPHA
- pkg_add (libpkg): refactor to return the struct pkg. use this pkg to display
  pkg-message in the client, and to update the status of the dependencies in
  libpkg without hitting the database.
-
- pkg add (client) need to fetch files if arg is an URL
-
- add facility in libpkg to fetch files so we can use it for pkg add (client)
-
  and later to dowload packages from repo, and even to download the repo.db.
-
that means a function with a callback to display progress.

BETA1
- repo
modified libpkg/Makefile
@@ -21,7 +21,8 @@ SRCS= pkg.c \
		pkg_repo.c \
		pkg_util.c \
		pkg_version.c \
-
		pkgdb.c
+
		pkgdb.c \
+
		fetch.c

CFLAGS+=	-std=c99
CFLAGS+=	-I${.CURDIR} \
modified libpkg/pkg.h
@@ -243,12 +243,6 @@ int pkg_resolvdeps(struct pkg *, struct pkgdb *db);
int pkg_analyse_files(struct pkgdb *, struct pkg *);

/**
-
 * Add a new package.
-
 * @param path The path to the package archive file on the local disk
-
 */
-
int pkg_add(struct pkgdb *, const char *path);
-

-
/**
 * Generic setter for simple attributes.
 */
int pkg_set(struct pkg *, pkg_attr, const char *);
@@ -345,16 +339,40 @@ int pkgdb_loadmtree(struct pkgdb *db, struct pkg *pkg);
int pkgdb_compact(struct pkgdb *db);
const char *pkgdb_get_dir(void);

-
/* create */
+
/**
+
 * Add a new package.
+
 * @param path The path to the package archive file on the local disk
+
 */
+
int pkg_add(struct pkgdb *, const char *path);
+

+
/**
+
 * Archive formats options
+
 */
typedef enum pkg_formats { TAR, TGZ, TBZ, TXZ } pkg_formats;
+

+
/**
+
 * @todo Document
+
 */
int pkg_create(const char *, pkg_formats, const char *, const char *, struct pkg *);

-
/* delete */
+
/**
+
 * @todo Document
+
 */
int pkg_delete(struct pkg *, struct pkgdb *, int);

-
/* version */
+
/**
+
 * @todo Document
+
 */
int pkg_version_cmp(const char *, const char *);

+
typedef void (*fetch_cb)(void *data, const char *url, off_t total, off_t done,
+
						 time_t elapsed);
+

+
/**
+
 * Fetch a file
+
 */
+
int pkg_fetch_file(const char *url, const char *dest, void *data, fetch_cb cb);
+

/* glue to deal with ports */
int ports_parse_plist(struct pkg *, char *);
int ports_parse_depends(struct pkg *, char *);
@@ -362,8 +380,19 @@ int ports_parse_conflicts(struct pkg *, char *);
int ports_parse_scripts(struct pkg *, char *);
int ports_parse_options(struct pkg *, char *);

+
/**
+
 * Return the last error number
+
 */
pkg_error_t pkg_error_number(void);
+

+
/**
+
 * Return the last error string
+
 */
const char * pkg_error_string(void);
+

+
/**
+
 * Behave like warn(3), but with the pkg error instead of errno
+
 */
void pkg_error_warn(const char *fmt, ...);

#endif
modified libpkg/pkg_util.c
@@ -10,8 +10,6 @@
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
-
#include <fetch.h>
-
#include <libutil.h>

#include "pkg.h"
#include "pkg_error.h"
@@ -223,81 +221,6 @@ split_chr(char *str, char sep)
}

int
-
file_fetch(const char *url, const char *dest)
-
{
-
	int fd;
-
	FILE *remote = NULL;
-
	struct url_stat st;
-
	off_t tfetched, rfetched, wfetched;
-
	int retry = 3;
-
	time_t begin_dl, now;
-
	char buf[BUFSIZ];
-

-
	if ((fetchStatURL(url, &st, "") < 0) || st.size == -1) {
-
		/* TODO error handling */
-
		return (-1);
-
	}
-

-
	while (remote == NULL) {
-
		remote = fetchXGetURL(url, &st, "");
-
		if (remote == NULL) {
-
			/* TODO err handling */
-
			sleep(1);
-
			--retry;
-
		}
-

-
		if (retry == 0) {
-
			/* TODO err handling */
-
			return (-1);
-
		}
-
	}
-

-
	if (st.size > SSIZE_MAX - 1) {
-
		/* TODO err handling */
-
		return (-1);
-
	}
-

-
	if ((fd = open(dest, O_WRONLY|O_CREAT|O_TRUNC, 0644)) == -1) {
-
		/* TODO err handling */
-
		return (-1);
-
	}
-

-
	tfetched = 0;
-
	begin_dl = time(NULL);
-
	while (tfetched < st.size) {
-
		if ((rfetched = fread(buf, 1, sizeof(buf), remote)) < 1)
-
			break;
-

-
		if ((wfetched = write(fd, buf, rfetched)) != rfetched)
-
			break;
-

-
		tfetched +=  rfetched;
-
		now = time(NULL);
-
/* TODO: callback, this is the job of the UI */
-
#if 0
-
		if ((now - begin_dl) > 0)
-
			humanize_number(sz, 8, (int64_t)(tfetched / (now - begin_dl)),
-
					"Bps", HN_AUTOSCALE, HN_DECIMAL);
-
		else
-
			humanize_number(sz, 8, 0,
-
					"Bps", HN_AUTOSCALE, HN_DECIMAL);
-
		printf("\r%s\t%s %d%%", url, sz, (int)(((float)tfetched / (float)st.size) * 100));
-
#endif
-
	}
-
	printf("\n");
-

-
	if (ferror(remote)) {
-
		/* TODO err handling */
-
		return (-1);
-
	}
-

-
	close(fd);
-
	fclose(remote);
-

-
	return (0);
-
}
-

-
int
is_dir(const char *path) {
	struct stat st;

modified pkg/add.c
@@ -1,10 +1,41 @@
+
#include <err.h>
+
#include <libgen.h>
+
#include <stdbool.h>
#include <stdio.h>
+
#include <string.h>
+

#include <pkg.h>
-
#include <stdbool.h>
-
#include <err.h>

#include "add.h"

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

+
	data = NULL;
+
	elapsed = 0;
+

+
	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)
+
{
+
	if (strnstr(pattern, "http://", 7) == 0 ||
+
		strnstr(pattern, "https://", 8) == 0 ||
+
		strnstr(pattern, "ftp://", 6) == 0)
+
		return (0);
+

+
	return (-1);
+
}
+

void
usage_add(void)
{
@@ -16,6 +47,7 @@ int
exec_add(int argc, char **argv)
{
	struct pkgdb *db;
+
	char *file;

	if (argc != 2) {
		usage_add();
@@ -28,8 +60,17 @@ exec_add(int argc, char **argv)
		return (-1);
	}

-
	if (pkg_add(db, argv[1]) != EPKG_OK)
-
		pkg_error_warn("can not install %s", argv[1]);
+
	if (is_url(argv[1]) == 0) {
+
		asprintf(&file, "./%s", basename(argv[1]));
+
		if (pkg_fetch_file(argv[1], file, NULL, &fetch_status) != EPKG_OK) {
+
			pkg_error_warn("can not fetch %s", argv[1]);
+
			return (1);
+
		}
+
	} else
+
		file = argv[1];
+

+
	if (pkg_add(db, file) != EPKG_OK)
+
		pkg_error_warn("can not install %s", file);

	pkgdb_close(db);