Radish alpha
H
rad:z3QDZAW2FAfuLvihrhiyDC9fAD8G9
HardenedBSD Package Manager
Radicle
Git
Turn struct pkg_categories into a pkg_object
Baptiste Daroussin committed 12 years ago
commit a9a44e78eb9e942f8cd0801a21da15df27f24c61
parent f85c2cc
10 files changed +57 -100
modified libpkg/pkg.c
@@ -425,14 +425,6 @@ pkg_files(const struct pkg *pkg, struct pkg_file **f)
}

int
-
pkg_categories(const struct pkg *pkg, struct pkg_category **c)
-
{
-
	assert(pkg != NULL);
-

-
	HASH_NEXT(pkg->categories, (*c));
-
}
-

-
int
pkg_dirs(const struct pkg *pkg, struct pkg_dir **d)
{
	assert(pkg != NULL);
@@ -708,28 +700,26 @@ pkg_addfile_attr(struct pkg *pkg, const char *path, const char *sha256, const ch
int
pkg_addcategory(struct pkg *pkg, const char *name)
{
-
	struct pkg_category *c = NULL;
+
	pkg_object *o = NULL;
+
	pkg_iter iter = NULL;

	assert(pkg != NULL);
	assert(name != NULL && name[0] != '\0');

-
	HASH_FIND_STR(pkg->categories, name, c);
-
	if (c != NULL) {
-
		if (pkg_object_bool(pkg_config_get("DEVELOPER_MODE"))) {
-
			pkg_emit_error("duplicate category listing: %s, fatal (developer mode)", name);
-
			return (EPKG_FATAL);
-
		} else {
-
			pkg_emit_error("duplicate category listing: %s, ignoring", name);
-
			return (EPKG_OK);
+
	while ((o = (pkg_object_iterate(pkg->categories, &iter)))) {
+
		if (strcmp(pkg_object_string(o), name) == 0) {
+
			if (pkg_object_bool(pkg_config_get("DEVELOPER_MODE"))) {
+
				pkg_emit_error("duplicate category listing: %s, fatal (developer mode)", name);
+
				return (EPKG_FATAL);
+
			} else {
+
				pkg_emit_error("duplicate category listing: %s, ignoring", name);
+
				return (EPKG_OK);
+
			}
		}
	}

-
	pkg_category_new(&c);
-

-
	sbuf_set(&c->name, name);
-

-
	HASH_ADD_KEYPTR(hh, pkg->categories, pkg_category_name(c),
-
	    strlen(pkg_category_name(c)), c);
+
	o = ucl_object_fromstring_common(name, strlen(name), 0);
+
	pkg->categories = ucl_array_append(pkg->categories, o);

	return (EPKG_OK);
}
@@ -1112,6 +1102,14 @@ pkg_addannotation(struct pkg *pkg, const char *tag, const char *value)
}

pkg_object *
+
pkg_categories(const struct pkg *pkg)
+
{
+
	assert (pkg != NULL);
+

+
	return (pkg->categories);
+
}
+

+
pkg_object *
pkg_annotations(const struct pkg *pkg)
{
	assert(pkg != NULL);
@@ -1160,7 +1158,7 @@ pkg_list_count(const struct pkg *pkg, pkg_list list)
	case PKG_OPTIONS:
		return (HASH_COUNT(pkg->options));
	case PKG_CATEGORIES:
-
		return (HASH_COUNT(pkg->categories));
+
		return (UCL_COUNT(pkg->categories));
	case PKG_FILES:
		return (HASH_COUNT(pkg->files));
	case PKG_DIRS:
@@ -1204,7 +1202,10 @@ pkg_list_free(struct pkg *pkg, pkg_list list) {
		pkg->flags &= ~PKG_LOAD_OPTIONS;
		break;
	case PKG_CATEGORIES:
-
		HASH_FREE(pkg->categories, free);
+
		if (pkg->categories != NULL) {
+
			ucl_object_unref(pkg->categories);
+
			pkg->categories = NULL;
+
		}
		pkg->flags &= ~PKG_LOAD_CATEGORIES;
		break;
	case PKG_FILES:
modified libpkg/pkg.h.in
@@ -564,12 +564,7 @@ int pkg_files(const struct pkg *, struct pkg_file **file);
 */
int pkg_dirs(const struct pkg *pkg, struct pkg_dir **dir);

-
/**
-
 * Iterates over the categories of the package.
-
 * @param Must be set to NULL for the first call.
-
 * @return An error code
-
 */
-
int pkg_categories(const struct pkg *pkg, struct pkg_category **category);
+
pkg_object *pkg_categories(const struct pkg *pkg);

/**
 * Iterates over the licenses of the package.
modified libpkg/pkg_attributes.c
@@ -215,38 +215,6 @@ pkg_dir_try(struct pkg_dir const * const d)
}

/*
-
 * Category
-
 */
-

-
int
-
pkg_category_new(struct pkg_category **c)
-
{
-
	if ((*c = calloc(1, sizeof(struct pkg_category))) == NULL)
-
		return (EPKG_FATAL);
-

-
	return (EPKG_OK);
-
}
-

-
const char *
-
pkg_category_name(struct pkg_category const * const c)
-
{
-
	assert(c != NULL);
-

-
	return (sbuf_get(c->name));
-
}
-

-
void
-
pkg_category_free(struct pkg_category *c)
-
{
-

-
	if (c == NULL)
-
		return;
-

-
	sbuf_free(c->name);
-
	free(c);
-
}
-

-
/*
 * License
 */
int
modified libpkg/pkg_manifest.c
@@ -840,7 +840,6 @@ emit_manifest(struct pkg *pkg, struct sbuf **out, short flags)
	struct pkg_option	*option   = NULL;
	struct pkg_file		*file     = NULL;
	struct pkg_dir		*dir      = NULL;
-
	struct pkg_category	*category = NULL;
	struct pkg_license	*license  = NULL;
	struct pkg_user		*user     = NULL;
	struct pkg_group	*group    = NULL;
@@ -916,10 +915,10 @@ emit_manifest(struct pkg *pkg, struct sbuf **out, short flags)
	obj = ucl_object_insert_key(top, map, "deps", 4, false);

	pkg_debug(4, "Emitting categories");
-
	seq = NULL;
-
	while (pkg_categories(pkg, &category) == EPKG_OK)
-
		seq = ucl_array_append(seq, ucl_object_fromstring(pkg_category_name(category)));
-
	obj = ucl_object_insert_key(top, seq, "categories", 10, false);
+
	
+
	if (pkg->categories != NULL)
+
		obj = ucl_object_insert_key(top,
+
		    ucl_object_ref(pkg->categories), "categories", 10, false);

	pkg_debug(4, "Emitting users");
	seq = NULL;
@@ -973,7 +972,7 @@ emit_manifest(struct pkg *pkg, struct sbuf **out, short flags)

	if (pkg->annotations != NULL)
		obj = ucl_object_insert_key(top,
-
		    ucl_object_ref(map), "annotations", 11, false);
+
		    ucl_object_ref(pkg->annotations), "annotations", 11, false);

	if ((flags & PKG_MANIFEST_EMIT_COMPACT) == 0) {
		if ((flags & PKG_MANIFEST_EMIT_NOFILES) == 0) {
modified libpkg/pkg_printf.c
@@ -930,13 +930,14 @@ format_categories(struct sbuf *sbuf, const void *data, struct percent_esc *p)
		return (list_count(sbuf, pkg_list_count(pkg, PKG_CATEGORIES),
				   p));
	else {
-
		struct pkg_category	*cat = NULL;
-
		int			 count;
+
		pkg_object	*cat;
+
		pkg_iter	 it = NULL;
+
		int		 count;

		set_list_defaults(p, "%Cn", ", ");

		count = 1;
-
		while (pkg_categories(pkg, &cat) == EPKG_OK) {
+
		while ((cat = pkg_object_iterate(pkg->categories, &it))) {
			if (count > 1)
				iterate_item(sbuf, pkg, sbuf_data(p->sep_fmt),
					     cat, count, PP_C);
@@ -955,9 +956,9 @@ format_categories(struct sbuf *sbuf, const void *data, struct percent_esc *p)
struct sbuf *
format_category_name(struct sbuf *sbuf, const void *data, struct percent_esc *p)
{
-
	const struct pkg_category	*cat = data;
+
	pkg_object	*o = (pkg_object *)data;

-
	return (string_val(sbuf, pkg_category_name(cat), p));
+
	return (string_val(sbuf, pkg_object_string(o), p));
}

/*
modified libpkg/pkgdb.c
@@ -2569,12 +2569,13 @@ pkgdb_register_pkg(struct pkgdb *db, struct pkg *pkg, int complete, int forced)
	struct pkg_file		*file = NULL;
	struct pkg_dir		*dir = NULL;
	struct pkg_option	*option = NULL;
-
	struct pkg_category	*category = NULL;
	struct pkg_license	*license = NULL;
	struct pkg_user		*user = NULL;
	struct pkg_group	*group = NULL;
	struct pkg_conflict	*conflict = NULL;
	struct pkgdb_it		*it = NULL;
+
	pkg_object		*obj;
+
	pkg_iter		 iter;

	sqlite3			*s;

@@ -2760,11 +2761,11 @@ pkgdb_register_pkg(struct pkgdb *db, struct pkg *pkg, int complete, int forced)
	 * Insert categories
	 */

-
	while (pkg_categories(pkg, &category) == EPKG_OK) {
-
		const char	*pkg_cat = pkg_category_name(category);
-
		ret = run_prstmt(CATEGORY1, pkg_cat);
+
	iter = NULL;
+
	while ((obj = pkg_object_iterate(pkg->categories, &iter))) {
+
		ret = run_prstmt(CATEGORY1, pkg_object_string(obj));
		if (ret == SQLITE_DONE)
-
			ret = run_prstmt(CATEGORY2, package_id, pkg_cat);
+
			ret = run_prstmt(CATEGORY2, package_id, pkg_object_string(obj));
		if (ret != SQLITE_DONE) {
			ERROR_SQLITE(s);
			goto cleanup;
modified libpkg/pkgdb_repo.c
@@ -472,7 +472,6 @@ pkgdb_repo_add_package(struct pkg *pkg, const char *pkg_path,
	lic_t			 licenselogic;
	int			 ret;
	struct pkg_dep		*dep      = NULL;
-
	struct pkg_category	*category = NULL;
	struct pkg_license	*license  = NULL;
	struct pkg_option	*option   = NULL;
	struct pkg_shlib	*shlib    = NULL;
@@ -526,14 +525,12 @@ try_again:
		}
	}

-
	category = NULL;
-
	while (pkg_categories(pkg, &category) == EPKG_OK) {
-
		const char *cat_name = pkg_category_name(category);
-

-
		ret = run_prepared_statement(CAT1, cat_name);
+
	it = NULL;
+
	while ((obj = pkg_object_iterate(pkg->categories, &it))) {
+
		ret = run_prepared_statement(CAT1, pkg_object_string(obj));
		if (ret == SQLITE_DONE)
			ret = run_prepared_statement(CAT2, package_id,
-
					cat_name);
+
			    pkg_object_string(obj));
		if (ret != SQLITE_DONE)
		{
			ERROR_SQLITE(sqlite);
modified libpkg/private/pkg.h
@@ -118,7 +118,7 @@ struct pkg {
	int64_t		 pkgsize;
	struct sbuf	*scripts[PKG_NUM_SCRIPTS];
	struct pkg_license	*licenses;
-
	struct pkg_category	*categories;
+
	ucl_object_t		*categories;
	struct pkg_dep		*deps;
	struct pkg_dep		*rdeps;
	struct pkg_file		*files;
@@ -172,11 +172,6 @@ struct pkg_license {
	UT_hash_handle	hh;
};

-
struct pkg_category {
-
	struct sbuf	*name;
-
	UT_hash_handle	hh;
-
};
-

struct pkg_file {
	char		 path[MAXPATHLEN];
	int64_t		 size;
@@ -371,9 +366,6 @@ void pkg_file_free(struct pkg_file *);
int pkg_dir_new(struct pkg_dir **);
void pkg_dir_free(struct pkg_dir *);

-
int pkg_category_new(struct pkg_category **);
-
void pkg_category_free(struct pkg_category *);
-

int pkg_license_new(struct pkg_license **);
void pkg_license_free(struct pkg_license *);

modified src/query.c
@@ -354,8 +354,10 @@ print_query(struct pkg *pkg, char *qstr, char multiline)
		}
		break;
	case 'C':
-
		while (pkg_categories(pkg, &cat) == EPKG_OK) {
-
			format_str(pkg, output, qstr, cat);
+
		obj = pkg_categories(pkg);
+
		it = NULL;
+
		while ((o = pkg_object_iterate(obj, &it))) {
+
			format_str(pkg, output, qstr, o);
			printf("%s\n", sbuf_data(output));
		}
		break;
modified src/rquery.c
@@ -84,12 +84,13 @@ print_index(struct pkg *pkg)
#ifndef PORTSDIR
#define PORTSDIR "/usr/ports"
#endif
-
	struct pkg_category *cat = NULL;
+
	pkg_object *obj = NULL;
+
	pkg_iter iter = NULL;

	pkg_printf("%n-%v|" PORTSDIR "/%o|%p|%c|" PORTSDIR "/%o/pkg-descr|%m|",
	    pkg, pkg, pkg, pkg, pkg, pkg, pkg);
-
	while (pkg_categories(pkg, &cat) == EPKG_OK)
-
		pkg_printf("%Cn ", cat);
+
	while ((obj = pkg_object_iterate(pkg_categories(pkg), &iter)))
+
		pkg_printf("%Cn ", obj);
	printf("\n");
}