Radish alpha
H
rad:z3QDZAW2FAfuLvihrhiyDC9fAD8G9
HardenedBSD Package Manager
Radicle
Git
pkg_is_valid now uses ucl schema to validate the package
Baptiste Daroussin committed 12 years ago
commit df83f862a0a69ce4a6de73fc58d74f78b777c136
parent a98946c
2 files changed +86 -28
modified libpkg/pkg.c
@@ -40,30 +40,7 @@
#include "private/pkg.h"
#include "private/utils.h"

-
/*static struct _fields {
-
	const char *human_desc;
-
	int type;
-
	int optional;
-
} fields[] = {
-
	[PKG_ORIGIN] = {"origin", PKG_FILE|PKG_REMOTE|PKG_INSTALLED, 0},
-
	[PKG_NAME] = {"name", PKG_FILE|PKG_REMOTE|PKG_INSTALLED, 0},
-
	[PKG_VERSION] = {"version", PKG_FILE|PKG_REMOTE|PKG_INSTALLED, 0},
-
	[PKG_COMMENT] = {"comment", PKG_FILE|PKG_REMOTE|PKG_INSTALLED, 0},
-
	[PKG_DESC] = {"description", PKG_FILE|PKG_REMOTE|PKG_INSTALLED, 0},
-
	[PKG_MTREE] = {"mtree", PKG_FILE|PKG_INSTALLED, 1},
-
	[PKG_MESSAGE] = {"message", PKG_FILE|PKG_INSTALLED, 1},
-
	[PKG_ARCH] = {"architecture", PKG_FILE|PKG_REMOTE|PKG_INSTALLED, 0},
-
	[PKG_MAINTAINER] = {"maintainer", PKG_FILE|PKG_REMOTE|PKG_INSTALLED, 0},
-
	[PKG_WWW] = {"www", PKG_FILE|PKG_REMOTE|PKG_INSTALLED, 0},
-
	[PKG_PREFIX] = {"prefix", PKG_FILE|PKG_REMOTE|PKG_INSTALLED, 0},
-
	[PKG_REPOPATH] = {"repopath", PKG_REMOTE, 0},
-
	[PKG_CKSUM] = {"checksum", PKG_REMOTE, 0},
-
	[PKG_OLD_VERSION] = {"oldversion", PKG_REMOTE, 1},
-
	[PKG_REPONAME] = {"reponame", PKG_REMOTE, 1},
-
	[PKG_REPOURL] = {"repourl", PKG_REMOTE, 1},
-
	[PKG_DIGEST] = {"manifestdigest", PKG_REMOTE|PKG_INSTALLED, 1},
-
	[PKG_REASON] = {"reason", PKG_REMOTE, 1}
-
};*/
+
static ucl_object_t *manifest_schema = NULL;

int
pkg_new(struct pkg **pkg, pkg_t type)
@@ -140,11 +117,92 @@ pkg_type(const struct pkg * restrict pkg)
	return (pkg->type);
}

+
static ucl_object_t *
+
manifest_schema_open(pkg_t type __unused)
+
{
+
	struct ucl_parser *parser;
+
	static const char manifest_schema_str[] = ""
+
		"{"
+
		"  type = object;"
+
		"  properties {"
+
		"    origin = { type = string };"
+
		"    name = { type = string };"
+
		"    comment = { type = string };"
+
		"    desc = { type = string };"
+
		"    mtree = { type = string };"
+
		"    message = { type = string };"
+
		"    maintainer = { type = string };"
+
		"    arch = { type = string };"
+
		"    www = { type = string };"
+
		"    prefix = { type = string };"
+
		"    digest = { type = string };"
+
		"    repopath = { type = string };"
+
		"    sum = { type = string };"
+
		"    oldversion = { type = string };"
+
		"    reponame = { type = string };"
+
		"    repourl = { type = string };"
+
		"    reason = { type = string };"
+
		"    flatsize = { type = integer }; "
+
		"    oldflatsize = { type = integer }; "
+
		"    pkgsize = { type = integer }; "
+
		"    locked = { type = boolean }; "
+
		"    rowid = { type = integer }; "
+
		"    time = { type = integer }; "
+
		"    annotations = { type = object }; "
+
		"    licenses = { "
+
		"      type = array; "
+
		"      items = { type = string }; "
+
		"      uniqueItems = true ;"
+
		"    };"
+
		"    categories = { "
+
		"      type = array; "
+
		"      items = { type = string }; "
+
		"      uniqueItems = true ;"
+
		"    };"
+
		"  }\n"
+
		"  required = ["
+
		"    origin,"
+
		"    name,"
+
		"    comment,"
+
		"    desc,"
+
		"    maintainer,"
+
		"    arch,"
+
		"    www,"
+
		"    prefix,"
+
		"  ]"
+
		"}";
+

+
	if (manifest_schema != NULL)
+
		return (manifest_schema);
+

+
	parser = ucl_parser_new(0);
+
	if (!ucl_parser_add_chunk(parser, manifest_schema_str,
+
	    sizeof(manifest_schema_str) -1)) {
+
		pkg_emit_error("Cannot parse manifest schema: %s",
+
		    ucl_parser_get_error(parser));
+
		ucl_parser_free(parser);
+
		return (NULL);
+
	}
+

+
	manifest_schema = ucl_parser_get_object(parser);
+
	ucl_parser_free(parser);
+

+
	return (manifest_schema);
+
}
+

int
pkg_is_valid(const struct pkg * restrict pkg)
{
-
	if (pkg->type == 0) {
-
		pkg_emit_error("package type undefined");
+
	ucl_object_t *schema;
+
	struct ucl_schema_error err;
+

+
	schema = manifest_schema_open(pkg->type);
+

+
	if (schema == NULL)
+
		return (EPKG_FATAL);
+

+
	if (!ucl_object_validate(schema, pkg->fields, &err)) {
+
		pkg_emit_error("Invalid package: %s", err.msg);
		return (EPKG_FATAL);
	}

modified libpkg/private/pkg.h
@@ -363,13 +363,13 @@ static struct pkg_key {
	[PKG_PREFIX] = { "prefix", UCL_STRING },
	[PKG_REPOPATH] = { "repopath", UCL_STRING },
	[PKG_CKSUM] = { "sum", UCL_STRING },
-
	[PKG_OLD_VERSION] = { "old_version", UCL_STRING },
+
	[PKG_OLD_VERSION] = { "oldversion", UCL_STRING },
	[PKG_REPONAME] = { "reponame", UCL_STRING },
	[PKG_REPOURL] = { "repourl", UCL_STRING },
	[PKG_DIGEST] = { "digest", UCL_STRING },
	[PKG_REASON] = { "reason", UCL_STRING },
	[PKG_FLATSIZE] = { "flatsize", UCL_INT },
-
	[PKG_OLD_FLATSIZE] = { "old_flatsize", UCL_INT },
+
	[PKG_OLD_FLATSIZE] = { "oldflatsize", UCL_INT },
	[PKG_PKGSIZE] = { "pkgsize", UCL_INT },
	[PKG_LICENSE_LOGIC] = { "licenselogic", UCL_INT },
	[PKG_AUTOMATIC] = { "automatic", UCL_BOOLEAN },