Radish alpha
H
rad:z3QDZAW2FAfuLvihrhiyDC9fAD8G9
HardenedBSD Package Manager
Radicle
Git
pkg_t checking on fields.
jlaffaye committed 14 years ago
commit 4ca34a2c46cd344bce0b95a8f3986bd1b0fda0eb
parent a7ff034
3 files changed +46 -14
modified libpkg/pkg.c
@@ -1,9 +1,10 @@
-
#include <err.h>
-
#include <string.h>
#include <archive.h>
#include <archive_entry.h>
-
#include <stdlib.h>
+
#include <err.h>
#include <errno.h>
+
#include <string.h>
+
#include <stdlib.h>
+
#include <sysexits.h>

#include "pkg.h"
#include "pkg_error.h"
@@ -34,6 +35,9 @@ pkg_get(struct pkg const * const pkg, const pkg_attr attr)
		return (NULL);
	}

+
	if ((pkg->fields[attr].type & pkg->type) == 0)
+
		errx(EX_SOFTWARE, "wrong usage of `attr` for this type of `pkg`");
+

	return (sbuf_get(pkg->fields[attr].value));
}

@@ -385,9 +389,31 @@ pkg_new(struct pkg **pkg)
	if ((*pkg = calloc(1, sizeof(struct pkg))) == NULL)
		return(pkg_error_set(EPKG_FATAL, "%s", strerror(errno)));

-
	(*pkg)->fields[PKG_MESSAGE].optional = 1;
-
	(*pkg)->fields[PKG_WWW].optional = 1;
-
	(*pkg)->fields[PKG_MTREE].optional = 1;
+
	struct _fields {
+
		int id;
+
		int type;
+
		int optional;
+
	} fields[] = {
+
		{PKG_ORIGIN, PKG_FILE|PKG_REMOTE|PKG_INSTALLED|PKG_UPGRADE|PKG_NOTFOUND, 0},
+
		{PKG_NAME, PKG_FILE|PKG_REMOTE|PKG_INSTALLED|PKG_UPGRADE|PKG_NOTFOUND, 0},
+
		{PKG_VERSION, PKG_FILE|PKG_REMOTE|PKG_INSTALLED|PKG_UPGRADE|PKG_NOTFOUND, 0},
+
		{PKG_COMMENT, PKG_FILE|PKG_REMOTE|PKG_INSTALLED|PKG_UPGRADE, 0},
+
		{PKG_DESC, PKG_FILE|PKG_REMOTE|PKG_INSTALLED|PKG_UPGRADE, 0},
+
		{PKG_MTREE, PKG_FILE|PKG_INSTALLED|PKG_UPGRADE, 1},
+
		{PKG_MESSAGE, PKG_FILE|PKG_INSTALLED|PKG_UPGRADE, 1},
+
		{PKG_ARCH, PKG_FILE|PKG_REMOTE|PKG_INSTALLED|PKG_UPGRADE, 0},
+
		{PKG_OSVERSION, PKG_FILE|PKG_REMOTE|PKG_INSTALLED|PKG_UPGRADE, 0},
+
		{PKG_MAINTAINER, PKG_FILE|PKG_REMOTE|PKG_INSTALLED|PKG_UPGRADE, 0},
+
		{PKG_WWW, PKG_FILE|PKG_REMOTE|PKG_INSTALLED|PKG_UPGRADE, 1},
+
		{PKG_PREFIX, PKG_FILE|PKG_REMOTE|PKG_INSTALLED|PKG_UPGRADE, 0},
+
		{PKG_NEWVERSION, PKG_UPGRADE, 0},
+
		{PKG_NEWPATH, PKG_UPGRADE, 0}
+
	};
+

+
	for (int i = 0; i < PKG_NUM_FIELDS; i++) {
+
		(*pkg)->fields[fields[i].id].type = fields[i].type;
+
		(*pkg)->fields[fields[i].id].optional = fields[i].optional;
+
	}

	return (EPKG_OK);
}
modified libpkg/pkg.h
@@ -49,18 +49,27 @@ typedef enum {
 */
typedef enum {
	/**
+
	 * The pkg type can not be determined.
+
	 */
+
	PKG_NONE = 0,
+

+
	/**
	 * The pkg refers to a local file archive.
	 */
-
	PKG_FILE,
+
	PKG_FILE = 1 << 0,
	/**
	 * The pkg refers to a package available on the remote repository.
	 * @todo Document which attributes are available.
	 */
-
	PKG_REMOTE,
+
	PKG_REMOTE = 1 << 1,
	/**
	 * The pkg refers to a localy installed package.
	 */
-
	PKG_INSTALLED,
+
	PKG_INSTALLED = 1 << 2,
+
	/**
+
	 * A package to be upgraded.
+
	 */
+
	PKG_UPGRADE = 1 << 3,
	/**
	 * The pkg refers to a non installed package.
	 * @warning That means that the pkg contains only few attributes:
@@ -68,11 +77,7 @@ typedef enum {
	 *   - name
	 *   - version
	 */
-
	PKG_NOTFOUND,
-
	/**
-
	 * The pkg type can not be determined.
-
	 */
-
	PKG_NONE
+
	PKG_NOTFOUND = 1 << 4,
} pkg_t;

/**
modified libpkg/pkg_private.h
@@ -18,6 +18,7 @@
struct pkg {
	struct {
		struct sbuf *value;
+
		int type; /* for which pkg_t this field is defined */
		unsigned int optional :1;
	} fields[PKG_NUM_FIELDS];
	int64_t flatsize;