Radish alpha
H
rad:z3QDZAW2FAfuLvihrhiyDC9fAD8G9
HardenedBSD Package Manager
Radicle
Git
Add a pkg_config() function.
jlaffaye committed 15 years ago
commit 525496b415bce920814e4c984d70268714697a88
parent 046ef40
5 files changed +84 -45
modified libpkg/Makefile
@@ -8,6 +8,7 @@ SHLIB_MAJOR= 0

SRCS=		pkg.c \
		pkg_add.c \
+
		pkg_config.c \
		pkg_conflict.c \
		pkg_create.c \
		pkg_delete.c \
modified libpkg/pkg.h
@@ -505,6 +505,11 @@ int pkg_create(const char *, pkg_formats, const char *, const char *, struct pkg
int pkg_delete(struct pkg *pkg, struct pkgdb *db, int force);

/**
+
 * Get the value of a configuration key
+
 */
+
const char * pkg_config(const char *key);
+

+
/**
 * @todo Document
 */
int pkg_version_cmp(const char * const , const char * const);
added libpkg/pkg_config.c
@@ -0,0 +1,74 @@
+
#include <sys/types.h>
+

+
#include <err.h>
+
#include <fcntl.h>
+
#include <libutil.h>
+
#include <pthread.h>
+
#include <stdlib.h>
+
#include <string.h>
+
#include <unistd.h>
+

+
#include "pkg.h"
+
#include "pkg_error.h"
+

+
struct _config {
+
	const char *key;
+
	const char *def;
+
	const char *val;
+
} c[] = {
+
	{ "PACKAGESITE", NULL, NULL},
+
	{ NULL, NULL, NULL}
+
};
+

+
static pthread_mutex_t m = PTHREAD_MUTEX_INITIALIZER;
+
static int done = 0;
+

+
static void
+
load_config(void)
+
{
+
	properties p = NULL;
+
	int fd;
+
	int i;
+

+
	if ((fd = open("/etc/pkg.conf", O_RDONLY)) > 0) {
+
		p = properties_read(fd);
+
		close(fd);
+
	}
+

+
	for (i = 0; c[i].key != NULL; i++) {
+
		if ((c[i].val = getenv(c[i].key)) == NULL && p != NULL)
+
			c[i].val = property_find(p, c[i].key);
+
	}
+

+
	done = 1;
+

+
	if (p != NULL)
+
		properties_free(p);
+
}
+

+
const char *
+
pkg_config(const char *key)
+
{
+
	int i;
+

+
	if (done == 0) {
+
		if (pthread_mutex_lock(&m) != 0)
+
			err(1, "pthread_mutex_lock()");
+
		if (done == 0)
+
			load_config();
+
		if (pthread_mutex_unlock(&m) != 0)
+
			err(1, "pthread_mutex_unlock()");
+
	}
+

+
	for (i = 0; c[i].key != NULL; i++) {
+
		if (strcmp(c[i].key, key) == 0) {
+
			if (c[i].val != NULL)
+
				return (c[i].val);
+
			else
+
				return (c[i].def);
+
		}
+
	}
+

+
	pkg_error_set(EPKG_FATAL, "unknown configuration key `%s'", key);
+
	return (NULL);
+
}
modified pkg/update.c
@@ -50,14 +50,12 @@ int
exec_update(int argc, char **argv)
{
	char url[MAXPATHLEN];
-
	char *packagesite = NULL;
+
	const char *packagesite = NULL;
	int retcode = 0;
	int size = 0;
	char *repo;
	struct archive *a;
	struct archive_entry *ae;
-
	int fd;
-
	properties conf = NULL;

	(void)argv;
	if (argc != 1) {
@@ -70,20 +68,9 @@ exec_update(int argc, char **argv)
		return (EX_NOPERM);
	}

-
	if ((fd = open("/etc/pkg.conf", O_RDONLY)) > 0) {
-
		conf = properties_read(fd);
-
		close(fd);
-
	}
-

-
	packagesite = getenv("PACKAGESITE");
-

-
	if (packagesite == NULL) {
-
		packagesite = property_find(conf, "packagesite");
-
		if (packagesite == NULL) {
-
			pkg_error_warn("unable to determine PACKAGESITE");
-
			retcode = 1;
-
			goto cleanup;
-
		}
+
	if ((packagesite = pkg_config("PACKAGESITE")) == NULL) {
+
		warnx("unable to determine PACKAGESITE");
+
		return (1);
	}

	if (packagesite[strlen(packagesite) - 1] == '/')
@@ -112,10 +99,5 @@ exec_update(int argc, char **argv)

	archive_read_finish(a);

-
	cleanup:
-
	
-
	if (conf != NULL)
-
		properties_free(conf);
-

	return (retcode);
}
modified pkg/upgrade.c
@@ -24,16 +24,13 @@ usage_upgrade(void)
int
exec_upgrade(int argc, char **argv)
{
-
	int fd;
	struct pkgdb *db = NULL;
	struct pkgdb_it *it;
	struct pkg *pkg = NULL;
-
	char *packagesite = NULL;
	int retcode = 0;
	int64_t oldsize = 0, newsize = 0;
	int64_t dlsize = 0;
	char size[7];
-
	properties conf = NULL;

	(void) argv;
	if (argc != 1) {
@@ -46,23 +43,6 @@ exec_upgrade(int argc, char **argv)
		return (EX_NOPERM);
	}

-
	if ((fd = open("/etc/pkg.conf", O_RDONLY)) > 0) {
-
		conf = properties_read(fd);
-
		close(fd);
-
	}
-

-
	packagesite = getenv("PACKAGESITE");
-

-
	if (packagesite == NULL) {
-
		packagesite = property_find(conf, "packagesite");
-
		if (packagesite == NULL) {
-
			pkg_error_warn("unable to determine PACKAGESITE");
-
			retcode = 1;
-
			goto cleanup;
-
		}
-
	}
-

-

	if (pkgdb_open(&db, PKGDB_REMOTE) != EPKG_OK) {
		pkg_error_warn("can not open database");
		return (1);
@@ -117,8 +97,5 @@ exec_upgrade(int argc, char **argv)
	if (db != NULL)
		pkgdb_close(db);

-
	if (conf != NULL)
-
		properties_free(conf);
-

	return (retcode);
}