Radish alpha
H
HardenedBSD Package Manager
Radicle
Git (anonymous pull)
Log in to clone via SSH
rename util{c,h} to pkg_util*
Baptiste Daroussin committed 15 years ago
commit 37dede7e7eda3723c2ecad723423332656939e4a
parent d3d61a05d1d8cd3a8cccff2b5e557e78706c6158
9 files changed +338 -338
modified libpkg/Makefile
@@ -19,7 +19,7 @@ SRCS= pkg.c \
		pkg_ports.c \
		pkg_repo.c \
		pkg_version.c \
-
		util.c
+
		pkg_util.c

CFLAGS+=	-std=c99
CFLAGS+=	-I${.CURDIR} \
modified libpkg/pkg.c
@@ -7,7 +7,7 @@

#include "pkg.h"
#include "pkg_private.h"
-
#include "util.h"
+
#include "pkg_util.h"

static void pkg_free_void(void*);

modified libpkg/pkg_private.h
@@ -5,7 +5,7 @@
#include <sys/types.h>
#include <sys/sbuf.h>

-
#include "util.h"
+
#include "pkg_util.h"

struct pkg {
	struct sbuf *origin;
modified libpkg/pkg_repo.c
@@ -9,7 +9,7 @@

#include "pkg.h"
#include "pkg_private.h"
-
#include "util.h"
+
#include "pkg_util.h"


int
added libpkg/pkg_util.c
@@ -0,0 +1,302 @@
+
#include <sys/stat.h>
+
#include <sys/param.h>
+
#include <stdio.h>
+

+
#include <assert.h>
+
#include <dirent.h>
+
#include <err.h>
+
#include <fcntl.h>
+
#include <stdlib.h>
+
#include <unistd.h>
+
#include <string.h>
+
#include <fetch.h>
+
#include <libutil.h>
+

+
#include "pkg_util.h"
+

+
void
+
array_init(struct array *a, size_t c)
+
{
+
	assert(c > 0);
+

+
	/* if the array is already initialized, do nothing */
+
	if (a->cap > 0 && a->data != NULL)
+
		return;
+

+
	a->cap = c;
+
	a->len = 0;
+
	a->data = malloc(sizeof(void*) * a->cap);
+
	a->data[0] = NULL;
+
}
+

+
void
+
array_append(struct array *a, void *d)
+
{
+
	assert(a->cap > 0);
+
	assert(a->data != NULL);
+

+
	if (a->cap <= a->len + 1) {
+
		a->cap *= 2;
+
		a->data = realloc(a->data, sizeof(void*) * a->cap);
+
	}
+
	a->data[a->len] = d;
+
	a->data[a->len+1] = NULL;
+
	a->len++;
+
}
+

+
void
+
array_reset(struct array *a, void (*free_elm)(void*))
+
{
+
	if (a->data == NULL)
+
		return;
+

+
	if (free_elm != NULL)
+
		for (size_t i = 0; i < a->len; i++)
+
			free_elm(a->data[i]);
+
	a->len = 0;
+
	a->data[0] = NULL;
+
}
+

+
void
+
array_free(struct array *a, void (*free_elm)(void*))
+
{
+
	if (a->data == NULL)
+
		return;
+

+
	if (free_elm != NULL)
+
		for (size_t i = 0; i < a->len; i++)
+
			free_elm(a->data[i]);
+
	free(a->data);
+
	a->data = NULL;
+
	a->len = 0;
+
	a->cap = 0;
+
}
+

+
int
+
sbuf_set(struct sbuf **buf, const char *str)
+
{
+
	if (*buf == NULL)
+
		*buf = sbuf_new_auto();
+

+
	if (str == NULL)
+
		return (-1);
+

+
	sbuf_cpy(*buf, str);
+
	sbuf_finish(*buf);
+
	return (0);
+
}
+

+
const char *
+
sbuf_get(struct sbuf *buf)
+
{
+
	if (buf == NULL)
+
		return (NULL);
+

+
	return sbuf_data(buf);
+
}
+

+
void
+
sbuf_reset(struct sbuf *buf)
+
{
+
	if (buf != NULL) {
+
		sbuf_clear(buf);
+
		sbuf_finish(buf);
+
	}
+
}
+

+
void
+
sbuf_free(struct sbuf *buf)
+
{
+
	if (buf != NULL)
+
		sbuf_delete(buf);
+
}
+

+
off_t
+
file_to_buffer(const char *path, char **buffer)
+
{
+
	int fd;
+
	struct stat st;
+

+
	assert(path != NULL);
+
	assert(buffer != NULL);
+

+
	if ((fd = open(path, O_RDONLY)) == -1) {
+
		warn("open(%s)", path);
+
		return (-1);
+
	}
+

+
	if (fstat(fd, &st) == -1) {
+
		warn("fstat(%d)", fd);
+
		close(fd);
+
		return (-1);
+
	}
+

+
	if ((*buffer = malloc(st.st_size + 1)) == NULL) {
+
		warn("malloc(%llu)", (unsigned long long)st.st_size + 1);
+
		close(fd);
+
		return (-1);
+
	}
+

+
	if (read(fd, *buffer, st.st_size) == -1) {
+
		warn("read()");
+
		close(fd);
+
		return (-1);
+
	}
+

+
	close(fd);
+

+
	/* NULL terminate the buffer so it can be used by stdio.h functions */
+
	(*buffer)[st.st_size] = '\0';
+

+
	return (st.st_size);
+
}
+

+
int
+
format_exec_cmd(char **dest, const char *in, const char *prefix, const char *plist_file)
+
{
+
	struct sbuf *buf = sbuf_new_auto();
+
	char path[MAXPATHLEN];
+
	char *cp;
+
	int len = 0;
+

+
	while (in[0] != '\0') {
+
		if (in[0] == '%') {
+
			in++;
+
			switch(in[0]) {
+
				case 'D':
+
					sbuf_cat(buf, prefix);
+
					break;
+
				case 'F':
+
					sbuf_cat(buf, plist_file);
+
					break;
+
				case 'f':
+
					if (prefix[strlen(prefix) - 1] == '/')
+
						snprintf(path, MAXPATHLEN, "%s%s", prefix, plist_file);
+
					else
+
						snprintf(path, MAXPATHLEN, "%s/%s", prefix, plist_file);
+
					cp = strrchr(path, '/');
+
					cp ++;
+
					sbuf_cat(buf, cp);
+
					break;
+
				case 'B':
+
					if (prefix[strlen(prefix) - 1] == '/')
+
						snprintf(path, MAXPATHLEN, "%s%s", prefix, plist_file);
+
					else
+
						snprintf(path, MAXPATHLEN, "%s/%s", prefix, plist_file);
+
					cp = strrchr(path, '/');
+
					cp[0] = '\0';
+
					sbuf_cat(buf, path);
+
					break;
+
			}
+

+
		} else {
+
			sbuf_putc(buf, in[0]);
+
		}
+

+
		in++;
+
	}
+

+
	sbuf_finish(buf);
+
	*dest = strdup(sbuf_data(buf));
+
	len = sbuf_len(buf);
+
	sbuf_free(buf);
+
	
+
	return (0);
+
}
+

+
int
+
split_chr(char *str, char sep)
+
{
+
	char *next;
+
	char *buf = str;
+
	int nbel = 0;
+

+
	while ((next = strchr(buf, sep)) != NULL) {
+
		nbel++;
+
		buf = next;
+
		buf[0] = '\0';
+
		buf++;
+
	}
+

+
	return nbel;
+
}
+

+
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], 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);
+
}
+

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

+
	return (stat(path, &st) == 0 && S_ISDIR(st.st_mode));
+
}
added libpkg/pkg_util.h
@@ -0,0 +1,31 @@
+
#ifndef _PKG_UTIL_H
+
#define _PKG_UTIL_H
+

+
#include <sys/types.h>
+
#include <sys/sbuf.h>
+

+
struct array {
+
	size_t cap;
+
	size_t len;
+
	void **data;
+
};
+

+
#define STARTS_WITH(string, needle) (strncasecmp(string, needle, strlen(needle)) == 0)
+

+
void array_init(struct array *, size_t);
+
void array_append(struct array *, void *);
+
void array_reset(struct array *, void (*free_elm)(void*));
+
void array_free(struct array *, void (*free_elm)(void*));
+

+
int sbuf_set(struct sbuf **, const char *);
+
const char * sbuf_get(struct sbuf *);
+
void sbuf_reset(struct sbuf *);
+
void sbuf_free(struct sbuf *);
+

+
off_t file_to_buffer(const char *path, char **buffer);
+
int format_exec_cmd(char **, const char *, const char *, const char *);
+
int split_chr(char *, char);
+
int file_fetch(const char *, const char *);
+
int is_dir(const char *path);
+

+
#endif
modified libpkg/pkgdb.c
@@ -16,7 +16,7 @@
#include "pkg.h"
#include "pkg_private.h"
#include "pkgdb.h"
-
#include "util.h"
+
#include "pkg_util.h"

#define PKG_DBDIR "/var/db/pkg"

deleted libpkg/util.c
@@ -1,302 +0,0 @@
-
#include <sys/stat.h>
-
#include <sys/param.h>
-
#include <stdio.h>
-

-
#include <assert.h>
-
#include <dirent.h>
-
#include <err.h>
-
#include <fcntl.h>
-
#include <stdlib.h>
-
#include <unistd.h>
-
#include <string.h>
-
#include <fetch.h>
-
#include <libutil.h>
-

-
#include "util.h"
-

-
void
-
array_init(struct array *a, size_t c)
-
{
-
	assert(c > 0);
-

-
	/* if the array is already initialized, do nothing */
-
	if (a->cap > 0 && a->data != NULL)
-
		return;
-

-
	a->cap = c;
-
	a->len = 0;
-
	a->data = malloc(sizeof(void*) * a->cap);
-
	a->data[0] = NULL;
-
}
-

-
void
-
array_append(struct array *a, void *d)
-
{
-
	assert(a->cap > 0);
-
	assert(a->data != NULL);
-

-
	if (a->cap <= a->len + 1) {
-
		a->cap *= 2;
-
		a->data = realloc(a->data, sizeof(void*) * a->cap);
-
	}
-
	a->data[a->len] = d;
-
	a->data[a->len+1] = NULL;
-
	a->len++;
-
}
-

-
void
-
array_reset(struct array *a, void (*free_elm)(void*))
-
{
-
	if (a->data == NULL)
-
		return;
-

-
	if (free_elm != NULL)
-
		for (size_t i = 0; i < a->len; i++)
-
			free_elm(a->data[i]);
-
	a->len = 0;
-
	a->data[0] = NULL;
-
}
-

-
void
-
array_free(struct array *a, void (*free_elm)(void*))
-
{
-
	if (a->data == NULL)
-
		return;
-

-
	if (free_elm != NULL)
-
		for (size_t i = 0; i < a->len; i++)
-
			free_elm(a->data[i]);
-
	free(a->data);
-
	a->data = NULL;
-
	a->len = 0;
-
	a->cap = 0;
-
}
-

-
int
-
sbuf_set(struct sbuf **buf, const char *str)
-
{
-
	if (*buf == NULL)
-
		*buf = sbuf_new_auto();
-

-
	if (str == NULL)
-
		return (-1);
-

-
	sbuf_cpy(*buf, str);
-
	sbuf_finish(*buf);
-
	return (0);
-
}
-

-
const char *
-
sbuf_get(struct sbuf *buf)
-
{
-
	if (buf == NULL)
-
		return (NULL);
-

-
	return sbuf_data(buf);
-
}
-

-
void
-
sbuf_reset(struct sbuf *buf)
-
{
-
	if (buf != NULL) {
-
		sbuf_clear(buf);
-
		sbuf_finish(buf);
-
	}
-
}
-

-
void
-
sbuf_free(struct sbuf *buf)
-
{
-
	if (buf != NULL)
-
		sbuf_delete(buf);
-
}
-

-
off_t
-
file_to_buffer(const char *path, char **buffer)
-
{
-
	int fd;
-
	struct stat st;
-

-
	assert(path != NULL);
-
	assert(buffer != NULL);
-

-
	if ((fd = open(path, O_RDONLY)) == -1) {
-
		warn("open(%s)", path);
-
		return (-1);
-
	}
-

-
	if (fstat(fd, &st) == -1) {
-
		warn("fstat(%d)", fd);
-
		close(fd);
-
		return (-1);
-
	}
-

-
	if ((*buffer = malloc(st.st_size + 1)) == NULL) {
-
		warn("malloc(%llu)", (unsigned long long)st.st_size + 1);
-
		close(fd);
-
		return (-1);
-
	}
-

-
	if (read(fd, *buffer, st.st_size) == -1) {
-
		warn("read()");
-
		close(fd);
-
		return (-1);
-
	}
-

-
	close(fd);
-

-
	/* NULL terminate the buffer so it can be used by stdio.h functions */
-
	(*buffer)[st.st_size] = '\0';
-

-
	return (st.st_size);
-
}
-

-
int
-
format_exec_cmd(char **dest, const char *in, const char *prefix, const char *plist_file)
-
{
-
	struct sbuf *buf = sbuf_new_auto();
-
	char path[MAXPATHLEN];
-
	char *cp;
-
	int len = 0;
-

-
	while (in[0] != '\0') {
-
		if (in[0] == '%') {
-
			in++;
-
			switch(in[0]) {
-
				case 'D':
-
					sbuf_cat(buf, prefix);
-
					break;
-
				case 'F':
-
					sbuf_cat(buf, plist_file);
-
					break;
-
				case 'f':
-
					if (prefix[strlen(prefix) - 1] == '/')
-
						snprintf(path, MAXPATHLEN, "%s%s", prefix, plist_file);
-
					else
-
						snprintf(path, MAXPATHLEN, "%s/%s", prefix, plist_file);
-
					cp = strrchr(path, '/');
-
					cp ++;
-
					sbuf_cat(buf, cp);
-
					break;
-
				case 'B':
-
					if (prefix[strlen(prefix) - 1] == '/')
-
						snprintf(path, MAXPATHLEN, "%s%s", prefix, plist_file);
-
					else
-
						snprintf(path, MAXPATHLEN, "%s/%s", prefix, plist_file);
-
					cp = strrchr(path, '/');
-
					cp[0] = '\0';
-
					sbuf_cat(buf, path);
-
					break;
-
			}
-

-
		} else {
-
			sbuf_putc(buf, in[0]);
-
		}
-

-
		in++;
-
	}
-

-
	sbuf_finish(buf);
-
	*dest = strdup(sbuf_data(buf));
-
	len = sbuf_len(buf);
-
	sbuf_free(buf);
-
	
-
	return (0);
-
}
-

-
int
-
split_chr(char *str, char sep)
-
{
-
	char *next;
-
	char *buf = str;
-
	int nbel = 0;
-

-
	while ((next = strchr(buf, sep)) != NULL) {
-
		nbel++;
-
		buf = next;
-
		buf[0] = '\0';
-
		buf++;
-
	}
-

-
	return nbel;
-
}
-

-
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], 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);
-
}
-

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

-
	return (stat(path, &st) == 0 && S_ISDIR(st.st_mode));
-
}
deleted libpkg/util.h
@@ -1,31 +0,0 @@
-
#ifndef _PKG_UTIL_H
-
#define _PKG_UTIL_H
-

-
#include <sys/types.h>
-
#include <sys/sbuf.h>
-

-
struct array {
-
	size_t cap;
-
	size_t len;
-
	void **data;
-
};
-

-
#define STARTS_WITH(string, needle) (strncasecmp(string, needle, strlen(needle)) == 0)
-

-
void array_init(struct array *, size_t);
-
void array_append(struct array *, void *);
-
void array_reset(struct array *, void (*free_elm)(void*));
-
void array_free(struct array *, void (*free_elm)(void*));
-

-
int sbuf_set(struct sbuf **, const char *);
-
const char * sbuf_get(struct sbuf *);
-
void sbuf_reset(struct sbuf *);
-
void sbuf_free(struct sbuf *);
-

-
off_t file_to_buffer(const char *path, char **buffer);
-
int format_exec_cmd(char **, const char *, const char *, const char *);
-
int split_chr(char *, char);
-
int file_fetch(const char *, const char *);
-
int is_dir(const char *path);
-

-
#endif