Radish alpha
H
rad:z3QDZAW2FAfuLvihrhiyDC9fAD8G9
HardenedBSD Package Manager
Radicle
Git
libpkg: Convert licenses and categories to stringlist
Baptiste Daroussin committed 4 years ago
commit b5eefda4b0777e307414cacb129d99cdd57aacd8
parent 7bb8d7e
9 files changed +63 -67
modified libpkg/pkg.c
@@ -100,8 +100,10 @@ pkg_free(struct pkg *pkg)
	pkg_list_free(pkg, PKG_SHLIBS_PROVIDED);
	pkg_list_free(pkg, PKG_PROVIDES);
	pkg_list_free(pkg, PKG_REQUIRES);
-
	pkg_list_free(pkg, PKG_CATEGORIES);
-
	pkg_list_free(pkg, PKG_LICENSES);
+
	tll_free_and_free(pkg->categories, free);
+
	pkg->flags &= ~PKG_LOAD_CATEGORIES;
+
	tll_free_and_free(pkg->licenses, free);
+
	pkg->flags &= ~PKG_LOAD_LICENSES;

	DL_FREE(pkg->message, pkg_message_free);
	DL_FREE(pkg->annotations, pkg_kv_free);
@@ -520,8 +522,6 @@ pkg_##name(const struct pkg *pkg, char **c) { \
	return (EPKG_OK);                            \
}

-
pkg_each_string_hash(categories);
-
pkg_each_string_hash(licenses);
pkg_each_string_hash(requires);
pkg_each_string_hash(provides);
pkg_each_string_hash(shlibs_required);
@@ -726,24 +726,25 @@ pkg_addconfig_file(struct pkg *pkg, const char *path, const char *content)
}

int
-
pkg_addstring(pkghash **list, const char *val, const char *title)
+
pkg_addstring(stringlist_t *list, const char *val, const char *title)
{
	assert(val != NULL);
	assert(title != NULL);

-
	if (pkghash_get(*list, val) != NULL) {
+
	tll_foreach(*list, v) {
+
		if (strcmp(v->item, val) != 0)
+
			continue;
		if (ctx.developer_mode) {
			pkg_emit_error("duplicate %s listing: %s, fatal"
			    " (developer mode)", title, val);
			return (EPKG_FATAL);
-
		} else {
-
			pkg_emit_error("duplicate %s listing: %s, "
-
			    "ignoring", title, val);
-
			return (EPKG_OK);
		}
+
		pkg_emit_error("duplicate %s listing: %s, "
+
		    "ignoring", title, val);
+
		return (EPKG_OK);
	}

-
	pkghash_safe_add(*list, val, NULL, NULL);
+
	tll_push_back(*list, xstrdup(val));

	return (EPKG_OK);
}
@@ -1191,10 +1192,6 @@ pkg_list_count(const struct pkg *pkg, pkg_list list)
		return (pkghash_count(pkg->requires));
	case PKG_CONFIG_FILES:
		return (pkghash_count(pkg->config_files_hash));
-
	case PKG_CATEGORIES:
-
		return (pkghash_count(pkg->categories));
-
	case PKG_LICENSES:
-
		return (pkghash_count(pkg->licenses));
	}

	return (0);
@@ -1280,16 +1277,6 @@ pkg_list_free(struct pkg *pkg, pkg_list list) {
		pkg->requires = NULL;
		pkg->flags &= ~PKG_LOAD_REQUIRES;
		break;
-
	case PKG_CATEGORIES:
-
		pkghash_destroy(pkg->categories);
-
		pkg->categories = NULL;
-
		pkg->flags &= ~PKG_LOAD_CATEGORIES;
-
		break;
-
	case PKG_LICENSES:
-
		pkghash_destroy(pkg->licenses);
-
		pkg->licenses = NULL;
-
		pkg->flags &= ~PKG_LOAD_LICENSES;
-
		break;
	}
}

modified libpkg/pkg.h.in
@@ -274,6 +274,8 @@ typedef enum {
	PKG_OLD_DIGEST,
	PKG_DEP_FORMULA,
	PKG_VITAL,
+
	PKG_CATEGORIES,
+
	PKG_LICENSES,
	PKG_NUM_FIELDS,		/* end of fields */
} pkg_attr;

@@ -313,8 +315,6 @@ typedef enum {
	PKG_PROVIDES,
	PKG_CONFIG_FILES,
	PKG_REQUIRES,
-
	PKG_CATEGORIES,
-
	PKG_LICENSES
} pkg_list;

typedef enum {
@@ -581,7 +581,7 @@ typedef enum {
#define pkg_get_stringlist(p, t, v) do { \
	struct pkg_el *e = pkg_get_element(p, t); \
	if (e->type != PKG_STRINGLIST) abort(); \
-
	v = e->kvlist; \
+
	v = e->stringlist; \
} while (0);

struct pkg_el {
modified libpkg/pkg_attributes.c
@@ -279,6 +279,12 @@ pkg_get_element(struct pkg *p, pkg_attr a)
		e->integer = p->pkgsize;
		e->type = PKG_INTEGER;
		break;
+
	case PKG_CATEGORIES:
+
		e->stringlist = xcalloc(1, sizeof(struct pkg_stringlist *));
+
		e->stringlist->list = &p->categories;
+
		e->type = PKG_STRINGLIST;
+
		break;
	}
+

	return (e);
}
modified libpkg/pkg_manifest.c
@@ -1027,11 +1027,10 @@ pkg_emit_object(struct pkg *pkg, short flags)

	pkg_debug(4, "Emitting licenses");
	seq = NULL;
-
	it = pkghash_iterator(pkg->licenses);
-
	while (pkghash_next(&it)) {
+
	tll_foreach(pkg->licenses, l) {
		if (seq == NULL)
			seq = ucl_object_typed_new(UCL_ARRAY);
-
		ucl_array_append(seq, ucl_object_fromstring(it.key));
+
		ucl_array_append(seq, ucl_object_fromstring(l->item));
	}
	if (seq)
		ucl_object_insert_key(top, seq, "licenses", 8, false);
@@ -1064,11 +1063,10 @@ pkg_emit_object(struct pkg *pkg, short flags)

	pkg_debug(4, "Emitting categories");
	seq = NULL;
-
	it = pkghash_iterator(pkg->categories);
-
	while (pkghash_next(&it)) {
+
	tll_foreach(pkg->categories, c) {
		if (seq == NULL)
			seq = ucl_object_typed_new(UCL_ARRAY);
-
		ucl_array_append(seq, ucl_object_fromstring(it.key));
+
		ucl_array_append(seq, ucl_object_fromstring(c->item));
	}
	if (seq)
		ucl_object_insert_key(top, seq, "categories", 10, false);
modified libpkg/pkg_printf.c
@@ -964,20 +964,19 @@ format_categories(xstring *buf, const void *data, struct percent_esc *p)
	int			 count = 0;

	if (p->flags & (PP_ALTERNATE_FORM1|PP_ALTERNATE_FORM2)) {
-
		return (list_count(buf, pkg_list_count(pkg, PKG_CATEGORIES), p));
+
		return (list_count(buf, tll_length(pkg->categories), p));
	} else {
		set_list_defaults(p, "%Cn", ", ");

		count = 1;
		fflush(p->sep_fmt->fp);
		fflush(p->item_fmt->fp);
-
		pkghash_it it = pkghash_iterator(pkg->categories);
-
		while (pkghash_next(&it)) {
+
		tll_foreach(pkg->categories, c) {
			if (count > 1)
				iterate_item(buf, pkg, p->sep_fmt->buf,
-
				    it.key, count, PP_C);
+
				    c->item, count, PP_C);

-
			iterate_item(buf, pkg, p->item_fmt->buf, it.key,
+
			iterate_item(buf, pkg, p->item_fmt->buf, c->item,
			    count, PP_C);
			count++;
		}
@@ -1237,20 +1236,19 @@ format_licenses(xstring *buf, const void *data, struct percent_esc *p)
	int			 count = 0;

	if (p->flags & (PP_ALTERNATE_FORM1|PP_ALTERNATE_FORM2)) {
-
		return (list_count(buf, pkg_list_count(pkg, PKG_LICENSES), p));
+
		return (list_count(buf, tll_length(pkg->licenses), p));
	} else {
		set_list_defaults(p, "%Ln", " %l ");

		count = 1;
		fflush(p->sep_fmt->fp);
		fflush(p->item_fmt->fp);
-
		pkghash_it it = pkghash_iterator(pkg->licenses);
-
		while (pkghash_next(&it)) {
+
		tll_foreach(pkg->licenses, l) {
			if (count > 1)
				iterate_item(buf, pkg, p->sep_fmt->buf,
-
				    it.key, count, PP_L);
+
				    l->item, count, PP_L);

-
			iterate_item(buf, pkg, p->item_fmt->buf, it.key,
+
			iterate_item(buf, pkg, p->item_fmt->buf, l->item,
			    count, PP_L);
			count++;
		}
modified libpkg/pkgdb.c
@@ -1927,11 +1927,10 @@ pkgdb_register_pkg(struct pkgdb *db, struct pkg *pkg, int forced,
	 * Insert categories
	 */

-
	hit = pkghash_iterator(pkg->categories);
-
	while (pkghash_next(&hit)) {
-
		ret = run_prstmt(CATEGORY1, hit.key);
+
	tll_foreach(pkg->categories, c) {
+
		ret = run_prstmt(CATEGORY1, c->item);
		if (ret == SQLITE_DONE)
-
			ret = run_prstmt(CATEGORY2, package_id, hit.key);
+
			ret = run_prstmt(CATEGORY2, package_id, c->item);
		if (ret != SQLITE_DONE) {
			ERROR_STMT_SQLITE(s, STMT(CATEGORY2));
			goto cleanup;
@@ -1942,12 +1941,11 @@ pkgdb_register_pkg(struct pkgdb *db, struct pkg *pkg, int forced,
	 * Insert licenses
	 */

-
	hit = pkghash_iterator(pkg->licenses);
-
	while (pkghash_next(&hit)) {
-
		if (run_prstmt(LICENSES1, hit.key)
+
	tll_foreach(pkg->licenses, l) {
+
		if (run_prstmt(LICENSES1, l->item)
		    != SQLITE_DONE
		    ||
-
		    run_prstmt(LICENSES2, package_id, hit.key)
+
		    run_prstmt(LICENSES2, package_id, l->item)
		    != SQLITE_DONE) {
			ERROR_STMT_SQLITE(s, STMT(LICENSES2));
			goto cleanup;
modified libpkg/private/pkg.h
@@ -216,8 +216,8 @@ struct pkg {
	struct pkg_dep		*depends;
	pkghash			*rdepshash;
	struct pkg_dep		*rdepends;
-
	pkghash			*categories;
-
	pkghash			*licenses;
+
	stringlist_t		 categories;
+
	stringlist_t		 licenses;
	pkghash			*filehash;
	struct pkg_file		*files;
	pkghash			*dirhash;
@@ -780,7 +780,7 @@ int pkg_adddir(struct pkg *pkg, const char *path, bool check_duplicates);
int pkg_adddir_attr(struct pkg *pkg, const char *path, const char *uname,
    const char *gname, mode_t perm, u_long fflags, bool check_duplicates);

-
int pkg_addstring(pkghash **s, const char *value, const char *title);
+
int pkg_addstring(stringlist_t *s, const char *value, const char *title);
int pkg_kv_add(struct pkg_kv **kv, const char *key, const char *value, const char *title);
const char *pkg_kv_get(struct pkg_kv *const*kv, const char *key);
int pkg_adduser(struct pkg *pkg, const char *name);
modified libpkg/repo/binary/update.c
@@ -178,24 +178,22 @@ try_again:
		}
	}

-
	it = pkghash_iterator(pkg->categories);
-
	while (pkghash_next(&it)) {
-
	ret = pkg_repo_binary_run_prstatement(CAT1, it.key);
+
	tll_foreach(pkg->categories, c) {
+
		ret = pkg_repo_binary_run_prstatement(CAT1, c->item);
		if (ret == SQLITE_DONE)
			ret = pkg_repo_binary_run_prstatement(CAT2, package_id,
-
			    it.key);
+
			    c->item);
		if (ret != SQLITE_DONE) {
			ERROR_SQLITE(sqlite, pkg_repo_binary_sql_prstatement(CAT2));
			return (EPKG_FATAL);
		}
	}

-
	it = pkghash_iterator(pkg->licenses);
-
	while (pkghash_next(&it)) {
-
		ret = pkg_repo_binary_run_prstatement(LIC1, it.key);
+
	tll_foreach(pkg->licenses, l) {
+
		ret = pkg_repo_binary_run_prstatement(LIC1, l->item);
		if (ret == SQLITE_DONE)
			ret = pkg_repo_binary_run_prstatement(LIC2, package_id,
-
			    it.key);
+
			    l->item);
		if (ret != SQLITE_DONE) {
			ERROR_SQLITE(sqlite, pkg_repo_binary_sql_prstatement(LIC2));
			return (EPKG_FATAL);
modified src/query.c
@@ -347,7 +347,10 @@ print_query(struct pkg *pkg, char *qstr, char multiline)
	struct pkg_file		*file   = NULL;
	struct pkg_dir		*dir    = NULL;
	char			*buf;
+
	const char		*str;
	struct pkg_kv		*kv;
+
	struct pkg_stringlist	*sl;
+
	struct pkg_stringlist_iterator	*slit;

	output = xstring_new();

@@ -366,10 +369,14 @@ print_query(struct pkg *pkg, char *qstr, char multiline)
		break;
	case 'C':
		buf = NULL;
-
		while (pkg_categories(pkg, &buf) == EPKG_OK) {
-
			format_str(pkg, output, qstr, buf);
+
		pkg_get_stringlist(pkg, PKG_CATEGORIES, sl);
+
		slit = pkg_stringlist_iterator(sl);
+
		while ((str = pkg_stringlist_next(slit))) {
+
			format_str(pkg, output, qstr, str);
			printf("%s\n", output->buf);
		}
+
		free(slit);
+
		free(sl);
		break;
	case 'O':
		while (pkg_options(pkg, &option) == EPKG_OK) {
@@ -391,10 +398,14 @@ print_query(struct pkg *pkg, char *qstr, char multiline)
		break;
	case 'L':
		buf = NULL;
-
		while (pkg_licenses(pkg, &buf) == EPKG_OK) {
-
			format_str(pkg, output, qstr, buf);
+
		pkg_get_stringlist(pkg, PKG_LICENSES, sl);
+
		slit = pkg_stringlist_iterator(sl);
+
		while ((str = pkg_stringlist_next(slit))) {
+
			format_str(pkg, output, qstr, str);
			printf("%s\n", output->buf);
		}
+
		free(slit);
+
		free(sl);
		break;
	case 'U':
		buf = NULL;