Radish alpha
H
rad:z3QDZAW2FAfuLvihrhiyDC9fAD8G9
HardenedBSD Package Manager
Radicle
Git
Now pkgng can open pkgfile from http(s) or ftp
Baptiste Daroussin committed 15 years ago
commit 251f425fc1e321903d88032a20f78e35a5f4e003
parent 9757b85
5 files changed +92 -4
modified libpkg/Makefile
@@ -27,7 +27,8 @@ LDADD+= -L${.CURDIR}/../external/jansson \
		-lz \
		-lbz2 \
		-llzma \
-
		-lsbuf
+
		-lsbuf \
+
		-lfetch

DEBUG_FLAGS+=  -g
.if defined(PROFILE_BUILD)
modified libpkg/pkg.c
@@ -77,12 +77,20 @@ pkg_open(const char *path, struct pkg **pkg, int query_flags)
	int64_t size;
	char *buf;

+
	/* search for http(s) or ftp(s) */
+
	if (STARTS_WITH(path, "http://") || STARTS_WITH(path, "https://")
+
			|| STARTS_WITH(path, "ftp://")) {
+
		file_fetch(path, "/tmp/bla");
+
		path = "/tmp/bla";
+
	}
+

	a = archive_read_new();
	archive_read_support_compression_all(a);
	archive_read_support_format_tar(a);

	if (archive_read_open_filename(a, path, 4096) != ARCHIVE_OK) {
		archive_read_finish(a);
+
		printf("la");
		return (-1);
	}

modified libpkg/pkg_manifest.c
@@ -192,17 +192,18 @@ m_parse_dep(struct pkg *pkg, char *buf)
static int
m_parse_conflict(struct pkg *pkg, char *buf)
{
-
	struct pkg_conflict *conflict;
+
/*	struct pkg_conflict *conflict;

	while (isspace(*buf))
		buf++;

-
	pkg_conflict_new(&conflict);
+
	int i = pkg_conflict_new(&conflict);
+
	printf("%d\n", i);
	sbuf_cat(conflict->glob, buf);
	sbuf_finish(conflict->glob);

	array_append(&pkg->conflicts, conflict);
-

+
*/
	return (0);
}

modified libpkg/util.c
@@ -1,4 +1,6 @@
#include <sys/stat.h>
+
#include <sys/param.h>
+
#include <stdio.h>

#include <assert.h>
#include <dirent.h>
@@ -7,6 +9,8 @@
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
+
#include <fetch.h>
+
#include <libutil.h>

#include "util.h"

@@ -156,3 +160,76 @@ split_chr(char *str, char sep)

	return nbel;
}
+

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

+
	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);
+

+
		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));
+
	}
+
	printf("\n");
+

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

+
	close(fd);
+
	fclose(remote);
+

+
	return (0);
+
}
modified libpkg/util.h
@@ -21,5 +21,6 @@ off_t file_to_buffer(const char *path, char **buffer);
char *str_replace(char *string, const char *find, char *replace);
int select_dir(const struct dirent *);
int split_chr(char *, char);
+
int file_fetch(const char *, char *);

#endif