Radish alpha
H
HardenedBSD Package Manager
Radicle
Git (anonymous pull)
Log in to clone via SSH
API cleanup.
jlaffaye committed 15 years ago
commit 768997460da37bbfa536e60b43fd2aa7e77ae4df
parent 414f58eb2677f5a23b28d8e5f30c550dc12eac98
6 files changed +147 -143
modified libpkg/pkg.h
@@ -1,10 +1,12 @@
#ifndef _PKG_H
#define _PKG_H

-
#include <cdb.h>
#include <stdio.h> /* for size_t */
#include <regex.h> /* regex_t */

+
/* Opaque type */
+
struct cdb;
+

struct pkg {
	const char *namever;
	const char *name;
@@ -15,7 +17,7 @@ struct pkg {
	size_t idx; /* index on pkgdb */
	size_t idep; /* iterator deps */
	size_t irdep; /* iterator rdeps */
-
	struct cdb *db;
+
	struct cdb *cdb;
	void *manifest; /* temp for pkgdb_cache */
};

@@ -27,9 +29,8 @@ typedef enum _match_t {
	MATCH_EREGEX
} match_t;

-

struct pkgdb {
-
	struct cdb db;
+
	struct cdb *cdb;
	int lock_fd;
	size_t i; /* iterator */
	const char *pattern;
@@ -39,4 +40,7 @@ struct pkgdb {

typedef enum pkg_formats { TAR, TGZ, TBZ, TXZ } pkg_formats;
int pkg_create(char *, pkg_formats, const char *, const char *);
+

+
void pkg_reset(struct pkg*);
+

#endif
modified libpkg/pkgdb.c
@@ -1,30 +1,24 @@
-
#include <stdlib.h>
-
#include <stdarg.h>
-
#include <string.h>
-
#include <unistd.h>
-
#include <fcntl.h>
#include <sys/param.h>
#include <sys/stat.h>
#include <sys/file.h>
+

#include <err.h>
+
#include <fcntl.h>
#include <fnmatch.h>
+
#include <stdlib.h>
+
#include <stdarg.h>
+
#include <string.h>
+
#include <unistd.h>
#include <regex.h>

#include "pkgdb.h"
#include "pkgdb_cache.h"

-
/* theses functions request on local database (cdb) */
-
static int pkg_db_open(struct pkgdb *);
-
static void pkg_reset(struct pkg *);
-
static const void *pkg_db_query(struct cdb *, const char *, ...);
-
static const char *pkg_db_getattr(struct pkg *, const char *);
-
static int pkg_db_dep(struct pkg *, struct pkg *);
-

const char *
pkg_namever(struct pkg *pkg)
{
	if (pkg->namever == NULL)
-
		pkg->namever = pkg_db_getattr(pkg, PKGDB_NAMEVER);
+
		pkg->namever = pkgdb_cache_getattr(pkg, PKGDB_NAMEVER);
	return (pkg->namever);
}

@@ -32,7 +26,7 @@ const char *
pkg_name(struct pkg *pkg)
{
	if (pkg->name == NULL)
-
		pkg->name = pkg_db_getattr(pkg, PKGDB_NAME);
+
		pkg->name = pkgdb_cache_getattr(pkg, PKGDB_NAME);
	return (pkg->name);
}

@@ -40,7 +34,7 @@ const char *
pkg_version(struct pkg *pkg)
{
	if (pkg->version == NULL)
-
		pkg->version = pkg_db_getattr(pkg, PKGDB_VERSION);
+
		pkg->version = pkgdb_cache_getattr(pkg, PKGDB_VERSION);
	return (pkg->version);
}

@@ -48,7 +42,7 @@ const char *
pkg_comment(struct pkg *pkg)
{
	if (pkg->comment == NULL)
-
		pkg->comment = pkg_db_getattr(pkg, PKGDB_COMMENT);
+
		pkg->comment = pkgdb_cache_getattr(pkg, PKGDB_COMMENT);
	return (pkg->comment);
}

@@ -56,7 +50,7 @@ const char *
pkg_desc(struct pkg *pkg)
{
	if (pkg->desc == NULL)
-
		pkg->desc = pkg_db_getattr(pkg, PKGDB_DESC);
+
		pkg->desc = pkgdb_cache_getattr(pkg, PKGDB_DESC);
	return (pkg->desc);
}

@@ -64,17 +58,17 @@ const char *
pkg_origin(struct pkg *pkg)
{
	if (pkg->origin == NULL)
-
		pkg->origin = pkg_db_getattr(pkg, PKGDB_ORIGIN);
+
		pkg->origin = pkgdb_cache_getattr(pkg, PKGDB_ORIGIN);
	return (pkg->desc);
}

int
pkg_dep(struct pkg *pkg, struct pkg *dep)
{
-
	return (pkg_db_dep(pkg, dep));
+
	return (pkgdb_cache_dep(pkg, dep));
}

-
static void
+
void
pkg_reset(struct pkg *pkg)
{
	pkg->namever = NULL;
@@ -86,59 +80,7 @@ pkg_reset(struct pkg *pkg)
	pkg->idx = -1;
	pkg->idep = 0;
	pkg->irdep = 0;
-
	pkg->db = NULL;
-
}
-

-
static int
-
pkg_db_dep(struct pkg *pkg, struct pkg *dep)
-
{
-
	const size_t *idx;
-
	int ret = -1;
-

-
	pkg_reset(dep);
-

-
	if ((dep->namever = pkg_db_query(pkg->db, PKGDB_DEPS, pkg->idx, pkg->idep)) != NULL &&
-
			(idx = pkg_db_query(pkg->db, "%s", dep->namever)) != NULL) {
-
		dep->idx = *idx;
-
		dep->db = pkg->db;
-
		pkg->idep++;
-
		ret = 0;
-
	}
-
	return (ret);
-
}
-

-
/* query formated using string key */
-
static const void *
-
pkg_db_query(struct cdb *db, const char *fmt, ...)
-
{
-
	va_list args;
-
	char key[BUFSIZ];
-
	size_t len;
-
	const void *val;
-

-
	va_start(args, fmt);
-
	len = vsnprintf(key, sizeof(key), fmt, args);
-

-
	if (len != strlen(key)) {
-
		warnx("key too long:");
-
		vwarnx(fmt, args);
-
		va_end(args);
-
		return NULL;
-
	}
-

-
	va_end(args);
-

-
	if (cdb_find(db, key, len) <= 0)
-
		return NULL;
-

-
	db_get(val, db);
-
	return (val);
-
}
-

-
static const char *
-
pkg_db_getattr(struct pkg *pkg, const char *attr)
-
{
-
	return (pkg_db_query(pkg->db, attr, pkg->idx));
+
	pkg->cdb = NULL;
}

const char *
@@ -152,23 +94,6 @@ pkgdb_get_dir(void)
	return pkg_dbdir;
}

-
static int
-
pkg_db_open(struct pkgdb *db)
-
{
-
	char path[MAXPATHLEN];
-
	int fd;
-

-
	snprintf(path, sizeof(path), "%s/pkgdb.cache", pkgdb_get_dir());
-

-
	if ((fd = open(path, O_RDONLY)) != -1)
-
		fd = cdb_init(&db->db, fd);
-
	else {
-
		/* TODO custom pkgdb error */
-
	}
-

-
	return (fd);
-
}
-

/*
 * Acquire a lock to access the database.
 * If `writer' is set to 1, an exclusive lock is requested so it wont mess up
@@ -205,7 +130,7 @@ int
pkgdb_init(struct pkgdb *db, const char *pattern, match_t match)
{
	pkgdb_cache_update(db);
-
	if (pkg_db_open(db) == -1)
+
	if (pkgdb_cache_open(db) == -1)
		return (-1); /* TOTO pkgdb error */

	db->pattern = pattern;
@@ -234,8 +159,8 @@ pkgdb_init(struct pkgdb *db, const char *pattern, match_t match)
void
pkgdb_free(struct pkgdb *db)
{
-
	close(cdb_fileno(&db->db));
-
	cdb_free(&db->db);
+
	if (db->cdb != NULL)
+
		pkgdb_cache_close(db->cdb);

	if (db->match == MATCH_REGEX || db->match == MATCH_EREGEX)
		regfree(&db->re);
@@ -271,10 +196,10 @@ pkgdb_query(struct pkgdb *db, struct pkg *pkg)
	pkg_reset(pkg);
	pkgdb_lock(db, 0);

-
	while ((pkg->namever = pkg_db_query(&db->db, PKGDB_NAMEVER, db->i)) != NULL) {
+
	while ((pkg->namever = pkgdb_cache_vget(db->cdb, PKGDB_NAMEVER, db->i)) != NULL) {
		if (pkg->namever != NULL && pkgdb_match(db, pkg->namever) == 0) {
			pkg->idx = db->i++;
-
			pkg->db = &db->db;
+
			pkg->cdb = db->cdb;
			return (0);
		}

modified libpkg/pkgdb.h
@@ -1,22 +1,9 @@
#ifndef _PKGDB_H
#define _PKGDB_H
+

#include <pkg.h>

#define PKG_DBDIR "/var/db/pkg"
-
#define PKGDB_LOCK "lock"
-
#define PKGDB_NAMEVER "%zunv"
-
#define PKGDB_NAME    "%zun"
-
#define PKGDB_VERSION "%zuv"
-
#define PKGDB_COMMENT "%zuc"
-
#define PKGDB_DESC    "%zud"
-
#define PKGDB_ORIGIN  "%zuo"
-
#define PKGDB_DEPS    "%zuD%zu"
-
#define PKGDB_COUNT   "count"
-

-
/* quick cdb_get */
-
#define db_get(val, db) do { \
-
	(val) = cdb_get((db), cdb_datalen((db)), cdb_datapos((db))); \
-
	} while (0)

void pkgdb_lock(struct pkgdb *db, int write);
void pkgdb_unlock(struct pkgdb *db);
@@ -36,5 +23,4 @@ int pkgdb_init(struct pkgdb *, const char *, match_t);
int pkgdb_query(struct pkgdb *, struct pkg *);
void pkgdb_free(struct pkgdb *);

-

#endif
modified libpkg/pkgdb_cache.c
@@ -1,11 +1,13 @@
+
#include <sys/param.h>
+
#include <sys/stat.h>
+

#include <dirent.h>
#include <err.h>
#include <errno.h>
+
#include <fcntl.h>
#include <stdlib.h>
#include <string.h>
#include <stdarg.h>
-
#include <sys/param.h>
-
#include <sys/stat.h>
#include <unistd.h>

#include <cdb.h>
@@ -16,9 +18,37 @@
#include "pkg_manifest.h"
#include "pkgdb_cache.h"

+
int
+
pkgdb_cache_open(struct pkgdb *db)
+
{
+
	char path[MAXPATHLEN];
+
	int fd;
+

+
	snprintf(path, sizeof(path), "%s/pkgdb.cache", pkgdb_get_dir());
+

+
	if ((db->cdb = malloc(sizeof(struct cdb))) == NULL)
+
		err(EXIT_FAILURE, "malloc()");
+

+
	if ((fd = open(path, O_RDONLY)) != -1)
+
		fd = cdb_init(db->cdb, fd);
+
	else {
+
		/* TODO custom pkgdb error */
+
	}
+

+
	return (fd);
+
}
+

+
void
+
pkgdb_cache_close(struct cdb *cdb)
+
{
+
	close(cdb_fileno(cdb));
+
	cdb_free(cdb);
+
	free(cdb);
+
}
+

/* add record formated string */
static int
-
pkgdb_vadd(struct cdb_make *db, const void *val, size_t vallen, const char *fmt, va_list args)
+
pkgdb_cache_vadd(struct cdb_make *db, const void *val, size_t vallen, const char *fmt, va_list args)
{
	char key[BUFSIZ];
	size_t len;
@@ -38,39 +68,75 @@ pkgdb_vadd(struct cdb_make *db, const void *val, size_t vallen, const char *fmt,
	return (cdb_make_add(db, key, len, val, vallen));
}

-
/*
+
/* add record formated string -> string (record the last \0 on value) */
static int
-
pkgdb_add(struct cdb_make *db, const void *val, size_t vallen, const char *fmt, ...)
+
pkgdb_cache_add_string(struct cdb_make *db, const char *val, const char *fmt, ...)
{
	int ret;
	va_list args;

	va_start(args, fmt);
-
	ret = pkgdb_vadd(db, val, vallen, fmt, args);
+
	ret = pkgdb_cache_vadd(db, val, strlen(val)+1, fmt, args);
	va_end(args);

	return (ret);
}
-
*/

-
/* add record formated string -> string (record the last \0 on value) */
static int
-
pkgdb_add_string(struct cdb_make *db, const char *val, const char *fmt, ...)
+
pkgdb_cache_add_int(struct cdb_make *db, const char *key, size_t val)
+
{
+
	return cdb_make_add(db, key, strlen(key), &val, sizeof(size_t));
+
}
+

+
const void *
+
pkgdb_cache_vget(struct cdb *db, const char *fmt, ...)
{
-
	int ret;
	va_list args;
+
	char key[BUFSIZ];
+
	size_t len;
+
	const void *val;

	va_start(args, fmt);
-
	ret = pkgdb_vadd(db, val, strlen(val)+1, fmt, args);
+
	len = vsnprintf(key, sizeof(key), fmt, args);
+

+
	if (len != strlen(key)) {
+
		warnx("key too long:");
+
		vwarnx(fmt, args);
+
		va_end(args);
+
		return NULL;
+
        }
+

	va_end(args);

-
	return (ret);
+
	if (cdb_find(db, key, len) <= 0)
+
		return NULL;
+

+
	db_get(val, db);
+
	return (val);
}

-
static int
-
pkgdb_add_int(struct cdb_make *db, const char *key, size_t val)
+
const char *
+
pkgdb_cache_getattr(struct pkg *pkg, const char *attr)
{
-
	return cdb_make_add(db, key, strlen(key), &val, sizeof(size_t));
+
	return (pkgdb_cache_vget(pkg->cdb, attr, pkg->idx));
+
}
+

+
int
+
pkgdb_cache_dep(struct pkg *pkg, struct pkg *dep)
+
{
+
	const size_t *idx;
+
	int ret = -1;
+

+
	pkg_reset(dep);
+

+
	if ((dep->namever = pkgdb_cache_vget(pkg->cdb, PKGDB_DEPS, pkg->idx, pkg->idep)) != NULL &&
+
		(idx = pkgdb_cache_vget(pkg->cdb, "%s", dep->namever)) != NULL) {
+
		dep->idx = *idx;
+
		dep->cdb = pkg->cdb;
+
		pkg->idep++;
+
		ret = 0;
+
	}
+
	return (ret);
}

static void
@@ -121,22 +187,22 @@ pkgdb_cache_rebuild(const char *pkg_dbdir, const char *cache_path)
		snprintf(namever, sizeof(namever), "%s-%s", pkg_manifest_value(m, "name"),
				 pkg_manifest_value(m, "version"));

-
		pkgdb_add_int(&cdb, namever, idx);
-
		pkgdb_add_int(&cdb, pkg_manifest_value(m, "name"), idx);
+
		pkgdb_cache_add_int(&cdb, namever, idx);
+
		pkgdb_cache_add_int(&cdb, pkg_manifest_value(m, "name"), idx);

-
		pkgdb_add_string(&cdb, namever, PKGDB_NAMEVER, idx);
-
		pkgdb_add_string(&cdb, pkg_manifest_value(m, "name"), PKGDB_NAME, idx);
-
		pkgdb_add_string(&cdb, pkg_manifest_value(m, "version"), PKGDB_VERSION, idx);
-
		pkgdb_add_string(&cdb, pkg_manifest_value(m, "comment"), PKGDB_COMMENT, idx);
-
		pkgdb_add_string(&cdb, pkg_manifest_value(m, "origin"), PKGDB_ORIGIN, idx);
-
		pkgdb_add_string(&cdb, pkg_manifest_value(m, "desc"), PKGDB_DESC, idx);
+
		pkgdb_cache_add_string(&cdb, namever, PKGDB_NAMEVER, idx);
+
		pkgdb_cache_add_string(&cdb, pkg_manifest_value(m, "name"), PKGDB_NAME, idx);
+
		pkgdb_cache_add_string(&cdb, pkg_manifest_value(m, "version"), PKGDB_VERSION, idx);
+
		pkgdb_cache_add_string(&cdb, pkg_manifest_value(m, "comment"), PKGDB_COMMENT, idx);
+
		pkgdb_cache_add_string(&cdb, pkg_manifest_value(m, "origin"), PKGDB_ORIGIN, idx);
+
		pkgdb_cache_add_string(&cdb, pkg_manifest_value(m, "desc"), PKGDB_DESC, idx);

		idep = 0;
		pkg_manifest_dep_init(m);
		while (pkg_manifest_dep_next(m) == 0) {
			snprintf(namever, sizeof(namever), "%s-%s", pkg_manifest_dep_name(m),
					 pkg_manifest_dep_version(m));
-
			pkgdb_add_string(&cdb, namever, PKGDB_DEPS, idx, idep);
+
			pkgdb_cache_add_string(&cdb, namever, PKGDB_DEPS, idx, idep);
			idep++;
		}

modified libpkg/pkgdb_cache.h
@@ -1,6 +1,33 @@
#ifndef _PKGDB_CACHE_H
#define _PKGDB_CACHE_H

+
#include <cdb.h>
+

+
#include "pkgdb.h"
+

+
#define PKGDB_LOCK		"lock"
+
#define PKGDB_NAMEVER	"%zunv"
+
#define PKGDB_NAME		"%zun"
+
#define PKGDB_VERSION	"%zuv"
+
#define PKGDB_COMMENT	"%zuc"
+
#define PKGDB_DESC		"%zud"
+
#define PKGDB_ORIGIN	"%zuo"
+
#define PKGDB_DEPS		"%zuD%zu"
+
#define PKGDB_COUNT		"count"
+

+
/* quick cdb_get */
+
#define db_get(val, db) \
+
	do { \
+
		(val) = cdb_get((db), cdb_datalen((db)), cdb_datapos((db))); \
+
	} while (0)
+

+
int pkgdb_cache_open(struct pkgdb *);
+
void pkgdb_cache_close(struct cdb *);
+

+
const void *pkgdb_cache_vget(struct cdb *, const char *, ...);
+
const char *pkgdb_cache_getattr(struct pkg *, const char *);
+
int pkgdb_cache_dep(struct pkg *, struct pkg *);
+

void pkgdb_cache_update(struct pkgdb *db);

#endif
modified pkg/info.c
@@ -7,17 +7,13 @@
#include "info.h"

/*
-
 *
 * list of options
-
 * -g: glob search: TODO
-
 * -x: regex search: TODO
 * -s: show package size: TODO
 * -S <type> : show scripts, type can be pre-install etc: TODO
-
 * -d: dependency list: TODO
 * -D: show reverse dependency list: TODO
 * -l: list contents of a package
 * -w <filename>: (which) finds which package the filename belongs to:
-
 * -e: return 1 of the package exist otherwise 0
+
 * -e: return 1 if the package exist otherwise 0
 */

int