Radish alpha
H
rad:z3QDZAW2FAfuLvihrhiyDC9fAD8G9
HardenedBSD Package Manager
Radicle
Git
libpkg: convert annotation to kvlist
Baptiste Daroussin committed 4 years ago
commit a37bc66b4df970f8a41ba5ca645bcf1b75e5e38e
parent 2772d8a
10 files changed +47 -38
modified libpkg/pkg.c
@@ -106,7 +106,7 @@ pkg_free(struct pkg *pkg)
	pkg->flags &= ~PKG_LOAD_LICENSES;

	DL_FREE(pkg->message, pkg_message_free);
-
	DL_FREE(pkg->annotations, pkg_kv_free);
+
	tll_free_and_free(pkg->annotations, pkg_kv_free);

	if (pkg->rootfd != -1)
		close(pkg->rootfd);
@@ -990,44 +990,41 @@ pkg_addprovide(struct pkg *pkg, const char *name)
}

const char *
-
pkg_kv_get(struct pkg_kv *const *kv, const char *tag)
+
pkg_kv_get(const kvlist_t *kv, const char *tag)
{
-
	struct pkg_kv *k;
-

	assert(tag != NULL);

-
	LL_FOREACH(*kv, k) {
-
		if (strcmp(k->key, tag) == 0)
-
			return (k->value);
+
	tll_foreach(*kv, k) {
+
		if (strcmp(k->item->key, tag) == 0)
+
			return (k->item->value);
	}

	return (NULL);
}

int
-
pkg_kv_add(struct pkg_kv **list, const char *key, const char *val, const char *title)
+
pkg_kv_add(kvlist_t *list, const char *key, const char *val, const char *title)
{
	struct pkg_kv *kv;

	assert(val != NULL);
	assert(title != NULL);

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

	kv = pkg_kv_new(key, val);
-
	DL_APPEND(*list, kv);
+
	tll_push_back(*list, kv);

	return (EPKG_OK);
}
modified libpkg/pkg.h.in
@@ -119,7 +119,6 @@ typedef void * pkg_iter;
struct pkg_kv {
	char *key;
	char *value;
-
	struct pkg_kv *next, *prev;
};

/**
modified libpkg/pkg_attributes.c
@@ -284,6 +284,11 @@ pkg_get_element(struct pkg *p, pkg_attr a)
		e->stringlist->list = &p->categories;
		e->type = PKG_STRINGLIST;
		break;
+
	case PKG_ANNOTATIONS:
+
		e->kvlist = xcalloc(1, sizeof(struct pkg_kvlist *));
+
		e->kvlist->list = &p->annotations;
+
		e->type = PKG_KVLIST;
+
		break;
	}

	return (e);
modified libpkg/pkg_manifest.c
@@ -1161,8 +1161,8 @@ pkg_emit_object(struct pkg *pkg, short flags)
		ucl_object_insert_key(top, map, "options", 7, false);

	map = NULL;
-
	kv = NULL;
-
	LL_FOREACH(pkg->annotations, kv) {
+
	tll_foreach(pkg->annotations, k) {
+
		kv = k->item;
		if (map == NULL)
			map = ucl_object_typed_new(UCL_OBJECT);
		/* Add annotations except for internal ones. */
modified libpkg/pkg_printf.c
@@ -857,25 +857,23 @@ xstring *
format_annotations(xstring *buf, const void *data, struct percent_esc *p)
{
	const struct pkg	*pkg = data;
-
	struct pkg_kv		*kv;
	int			count;

	if (p->flags & (PP_ALTERNATE_FORM1|PP_ALTERNATE_FORM2)) {
-
		LL_COUNT(pkg->annotations, kv, count);
-
		return (list_count(buf, count, p));
+
		return (list_count(buf, tll_length(pkg->annotations), p));
	} else {
		set_list_defaults(p, "%An: %Av\n", "");

		count = 1;
		fflush(p->sep_fmt->fp);
		fflush(p->item_fmt->fp);
-
		LL_FOREACH(pkg->annotations, kv) {
+
		tll_foreach(pkg->annotations, k) {
			if (count > 1)
				iterate_item(buf, pkg, p->sep_fmt->buf,
-
					     kv, count, PP_A);
+
					     k->item, count, PP_A);

			iterate_item(buf, pkg, p->item_fmt->buf,
-
				     kv, count, PP_A);
+
				     k->item, count, PP_A);
			count++;
		}
	}
modified libpkg/pkgdb.c
@@ -2194,7 +2194,8 @@ pkgdb_insert_annotations(struct pkg *pkg, int64_t package_id, sqlite3 *s)
{
	struct pkg_kv	*kv;

-
	LL_FOREACH(pkg->annotations, kv) {
+
	tll_foreach(pkg->annotations, k) {
+
		kv = k->item;
		if (run_prstmt(ANNOTATE1, kv->key)
		    != SQLITE_DONE
		    ||
modified libpkg/private/pkg.h
@@ -234,7 +234,7 @@ struct pkg {
	pkghash			*requires;
	pkghash			*config_files_hash;
	struct pkg_config_file	*config_files;
-
	struct pkg_kv		*annotations;
+
	kvlist_t		 annotations;
	unsigned			flags;
	int		rootfd;
	char		rootpath[MAXPATHLEN];
@@ -781,8 +781,8 @@ 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(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_kv_add(kvlist_t *kv, const char *key, const char *value, const char *title);
+
const char *pkg_kv_get(const kvlist_t *kv, const char *key);
int pkg_adduser(struct pkg *pkg, const char *name);
int pkg_addgroup(struct pkg *pkg, const char *group);
int pkg_addshlib_required(struct pkg *pkg, const char *name);
modified libpkg/repo/binary/update.c
@@ -260,7 +260,8 @@ try_again:
		}
	}

-
	LL_FOREACH(pkg->annotations, kv) {
+
	tll_foreach(pkg->annotations, k) {
+
		kv = k->item;
		ret = pkg_repo_binary_run_prstatement(ANNOTATE1, kv->key);
		if (ret == SQLITE_DONE)
			ret = pkg_repo_binary_run_prstatement(ANNOTATE1, kv->value);
modified src/annotate.c
@@ -141,11 +141,14 @@ do_delete(struct pkgdb *db, struct pkg *pkg, const char *tag)
static int
do_show(struct pkg *pkg, const char *tag)
{
+
	struct pkg_kvlist_iterator *kit;
+
	struct pkg_kvlist *kl;
	struct pkg_kv *note;
	int ret = EPKG_OK;

-
	ret = pkg_get(pkg, PKG_ANNOTATIONS, &note);
-
	while (note != NULL) {
+
	pkg_get_kv(pkg, PKG_ANNOTATIONS, kl);
+
	kit = pkg_kvlist_iterator(kl);
+
	while ((note = pkg_kvlist_next(kit))) {
		if (strcmp(tag, note->key) == 0) {
			if (quiet)
				printf("%s\n", note->value);
@@ -154,8 +157,9 @@ do_show(struct pkg *pkg, const char *tag)
				    pkg, pkg, note->key, note->value);
			return (EPKG_OK);
		}
-
		note = note->next;
	}
+
	free(kit);
+
	free(kl);

	return (ret);
}
modified src/query.c
@@ -351,6 +351,8 @@ print_query(struct pkg *pkg, char *qstr, char multiline)
	struct pkg_kv		*kv;
	struct pkg_stringlist	*sl;
	struct pkg_stringlist_iterator	*slit;
+
	struct pkg_kvlist	*kl;
+
	struct pkg_kvlist_iterator	*kit;

	output = xstring_new();

@@ -436,12 +438,14 @@ print_query(struct pkg *pkg, char *qstr, char multiline)
		}
		break;
	case 'A':
-
		pkg_get(pkg, PKG_ANNOTATIONS, &kv);
-
		while (kv != NULL) {
+
		pkg_get_kv(pkg, PKG_ANNOTATIONS, kl);
+
		kit = pkg_kvlist_iterator(kl);
+
		while ((kv = pkg_kvlist_next(kit))) {
			format_str(pkg, output, qstr, kv);
			printf("%s\n", output->buf);
-
			kv = kv->next;
		}
+
		free(kit);
+
		free(kl);
		break;
	default:
		format_str(pkg, output, qstr, dep);