Radish alpha
H
rad:z3QDZAW2FAfuLvihrhiyDC9fAD8G9
HardenedBSD Package Manager
Radicle
Git
vec: convert kvlist into vectors
Baptiste Daroussin committed 1 year ago
commit 28aa748abd5085a569571c841a6d684c5854e5ce
parent e9ddeea
11 files changed +50 -51
modified libpkg/fetch.c
@@ -228,13 +228,15 @@ pkg_fetch_file_to_fd(struct pkg_repo *repo, int dest, struct fetch_item *fi,
    bool silent)
{
	struct pkg_kv	*kv;
-
	kvlist_t	 envtorestore = tll_init();
+
	kvlist_t	 envtorestore;
	stringlist_t	 envtounset = tll_init();
	char		*tmp;
	int		 retcode = EPKG_OK;
	struct pkg_repo	*fakerepo = NULL;
	size_t           nsz;

+
	vec_init(&envtorestore);
+

	/* A URL of the form http://host.example.com/ where
	 * host.example.com does not resolve as a simple A record is
	 * not valid according to RFC 2616 Section 3.2.2.  Our usage
@@ -278,16 +280,16 @@ pkg_fetch_file_to_fd(struct pkg_repo *repo, int dest, struct fetch_item *fi,
	}

	repo->silent = silent;
-
	tll_foreach(repo->env, k) {
-
		if ((tmp = getenv(k->item->key)) != NULL) {
+
	vec_foreach(repo->env, i) {
+
		if ((tmp = getenv(repo->env.d[i]->key)) != NULL) {
			kv = xcalloc(1, sizeof(*kv));
-
			kv->key = xstrdup(k->item->key);
+
			kv->key = xstrdup(repo->env.d[i]->key);
			kv->value = xstrdup(tmp);
-
			tll_push_back(envtorestore, kv);
+
			vec_push(&envtorestore, kv);
		} else {
-
			tll_push_back(envtounset, k->item->key);
+
			tll_push_back(envtounset, repo->env.d[i]->key);
		}
-
		setenv(k->item->key, k->item->value, 1);
+
		setenv(repo->env.d[i]->key, repo->env.d[i]->value, 1);
	}

	if ((retcode = repo->fetcher->open(repo, fi)) != EPKG_OK)
@@ -299,11 +301,11 @@ pkg_fetch_file_to_fd(struct pkg_repo *repo, int dest, struct fetch_item *fi,
		pkg_emit_fetch_finished(fi->url);

cleanup:
-
	tll_foreach(envtorestore, k) {
-
		setenv(k->item->key, k->item->value, 1);
-
		tll_remove_and_free(envtorestore, k, pkg_kv_free);
+
	vec_foreach(envtorestore, i) {
+
		setenv(envtorestore.d[i]->key, envtorestore.d[i]->value, 1);
+
		vec_remove_and_free(&envtorestore, i, pkg_kv_free);
	}
-
	tll_free(envtorestore);
+
	vec_free(&envtorestore);
	tll_foreach(envtounset, k) {
		unsetenv(k->item);
		tll_remove(envtounset, k);
modified libpkg/pkg.c
@@ -104,7 +104,7 @@ pkg_free(struct pkg *pkg)
	pkg->flags &= ~PKG_LOAD_LICENSES;

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

	vec_free_and_free(&pkg->dir_to_del, free);

@@ -1026,9 +1026,9 @@ pkg_kv_get(const kvlist_t *kv, const char *tag)
{
	assert(tag != NULL);

-
	tll_foreach(*kv, k) {
-
		if (STREQ(k->item->key, tag))
-
			return (k->item->value);
+
	vec_foreach(*kv, i) {
+
		if (STREQ(kv->d[i]->key, tag))
+
			return (kv->d[i]->value);
	}

	return (NULL);
@@ -1042,8 +1042,8 @@ pkg_kv_add(kvlist_t *list, const char *key, const char *val, const char *title)
	assert(val != NULL);
	assert(title != NULL);

-
	tll_foreach(*list, k) {
-
		if (!STREQ(k->item->key, key))
+
	vec_foreach(*list, i) {
+
		if (!STREQ(list->d[i]->key, key))
			continue;
		if (ctx.developer_mode) {
			pkg_emit_error("duplicate %s: %s, fatal"
@@ -1056,7 +1056,7 @@ pkg_kv_add(kvlist_t *list, const char *key, const char *val, const char *title)
	}

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

	return (EPKG_OK);
}
modified libpkg/pkg_attributes.c
@@ -164,13 +164,9 @@ pkg_kvlist_iterator(struct pkg_kvlist *l)
struct pkg_kv *
pkg_kvlist_next(struct pkg_kvlist_iterator *it)
{
-
	if (it->cur == NULL)
-
		it->cur = it->list->head;
-
	else
-
		it->cur = ((__typeof__(it->list->head))it->cur)->next;
-
	if (it->cur == NULL)
+
	if (it->pos >= it->list->len)
		return (NULL);
-
	return (((__typeof__(it->list->head))it->cur)->item);
+
	return (it->list->d[it->pos++]);
}

struct pkg_stringlist_iterator *
modified libpkg/pkg_config.c
@@ -780,7 +780,7 @@ add_repo(const ucl_object_t *obj, struct pkg_repo *r, const char *rname, pkg_ini
		while ((cur = ucl_iterate_object(env, &it, true))) {
			kv = pkg_kv_new(ucl_object_key(cur),
			    ucl_object_tostring_forced(cur));
-
			tll_push_back(r->env, kv);
+
			vec_push(&r->env, kv);
		}
	}
}
@@ -1711,7 +1711,7 @@ pkg_repo_free(struct pkg_repo *r)
	pkg_repo_meta_free(r->meta);
	if (r->fetcher != NULL && r->fetcher->cleanup != NULL)
		r->fetcher->cleanup(r);
-
	tll_free_and_free(r->env, pkg_kv_free);
+
	vec_free_and_free(&r->env, pkg_kv_free);
	free(r->dbpath);
	free(r);
}
modified libpkg/pkg_manifest.c
@@ -1090,8 +1090,8 @@ pkg_emit_object(struct pkg *pkg, short flags)
		ucl_object_insert_key(top, map, "options", 7, false);

	map = NULL;
-
	tll_foreach(pkg->annotations, k) {
-
		kv = k->item;
+
	vec_foreach(pkg->annotations, i) {
+
		kv = pkg->annotations.d[i];
		if (map == NULL)
			map = ucl_object_typed_new(UCL_OBJECT);
		/* Add annotations except for internal ones. */
modified libpkg/pkg_ports.c
@@ -1091,7 +1091,7 @@ expand_plist_variables(const char *in, kvlist_t *vars)
	const char *cp;
	size_t len;

-
	if (tll_length(*vars) == 0)
+
	if (vec_len(vars) == 0)
		return (xstrdup(in));

	buf = xstring_new();
@@ -1129,10 +1129,10 @@ expand_plist_variables(const char *in, kvlist_t *vars)
		len = in - cp -1;
		/* we have a variable */
		bool found = false;
-
		tll_foreach(*vars, i) {
-
			if (strncmp(cp, i->item->key, len) != 0)
+
		vec_foreach(*vars, i) {
+
			if (strncmp(cp, vars->d[i]->key, len) != 0)
				continue;
-
			fputs(i->item->value, buf->fp);
+
			fputs(vars->d[i]->value, buf->fp);
			found = true;
			in++;
			break;
@@ -1209,15 +1209,15 @@ add_variable(struct plist *p, char *line, struct file_attr *a __unused)
	while (*val != '\0' && isspace(*val))
		val++;

-
	tll_foreach(p->variables, v) {
-
		if (STREQ(v->item->key, key)) {
-
			free(v->item->value);
-
			v->item->value = xstrdup(val);
+
	vec_foreach(p->variables, i) {
+
		if (STREQ(p->variables.d[i]->key, key)) {
+
			free(p->variables.d[i]->value);
+
			p->variables.d[i]->value = xstrdup(val);
			return (EPKG_OK);
		}
	}
	struct pkg_kv *kv = pkg_kv_new(key, val);
-
	tll_push_back(p->variables, kv);
+
	vec_push(&p->variables, kv);
	return (EPKG_OK);
}

modified libpkg/pkg_printf.c
@@ -860,20 +860,20 @@ format_annotations(xstring *buf, const void *data, struct percent_esc *p)
	int			count;

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

		count = 1;
		fflush(p->sep_fmt->fp);
		fflush(p->item_fmt->fp);
-
		tll_foreach(pkg->annotations, k) {
+
		vec_foreach(pkg->annotations, i) {
			if (count > 1)
				iterate_item(buf, pkg, p->sep_fmt->buf,
-
					     k->item, count, PP_A);
+
					     pkg->annotations.d[i], count, PP_A);

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

-
	tll_foreach(pkg->annotations, k) {
-
		kv = k->item;
+
	vec_foreach(pkg->annotations, i) {
+
		kv = pkg->annotations.d[i];
		if (run_prstmt(ANNOTATE1, kv->key)
		    != SQLITE_DONE
		    ||
modified libpkg/private/pkg.h
@@ -97,7 +97,7 @@
} while (0)
#define DL_FREE(head, free_func) DL_FREE2(head, free_func, prev, next)

-
typedef tll(struct pkg_kv *) kvlist_t;
+
typedef vec_t(struct pkg_kv *) kvlist_t;

typedef enum {
	IPALL = 0,
@@ -115,7 +115,7 @@ struct pkg_stringlist {

struct pkg_kvlist_iterator {
	kvlist_t *list;
-
	void *cur;
+
	size_t pos;
};

struct pkg_stringlist_iterator {
modified libpkg/repo/binary/update.c
@@ -259,8 +259,8 @@ try_again:
		}
	}

-
	tll_foreach(pkg->annotations, k) {
-
		kv = k->item;
+
	vec_foreach(pkg->annotations, i) {
+
		kv = pkg->annotations.d[i];
		ret = pkg_repo_binary_run_prstatement(ANNOTATE1, kv->key);
		if (ret == SQLITE_DONE)
			ret = pkg_repo_binary_run_prstatement(ANNOTATE1, kv->value);
modified tests/lib/plist.c
@@ -275,14 +275,15 @@ ATF_TC_BODY(parse_plist, tc)
ATF_TC_BODY(expand_plist_variables, tc)
{
	char *plop;
-
	kvlist_t kv = tll_init();
+
	kvlist_t kv;
+
	vec_init(&kv);

	plop = expand_plist_variables("%%this%% is a line", &kv);
	ATF_REQUIRE_STREQ(plop, "%%this%% is a line");
	free(plop);

	struct pkg_kv *keyval = pkg_kv_new("this", "@comment ");
-
	tll_push_back(kv, keyval);
+
	vec_push(&kv, keyval);

	plop = expand_plist_variables("%%this%% is a line", &kv);
	ATF_REQUIRE_STREQ(plop, "@comment  is a line");
@@ -297,7 +298,7 @@ ATF_TC_BODY(expand_plist_variables, tc)
	free(plop);

	struct pkg_kv *kv2 = pkg_kv_new("new", "var");
-
	tll_push_back(kv, kv2);
+
	vec_push(&kv, kv2);

	plop = expand_plist_variables("%%this%% %F is a %%new%% line", &kv);
	ATF_REQUIRE_STREQ(plop, "@comment  %F is a var line");
@@ -319,7 +320,7 @@ ATF_TC_BODY(expand_plist_variables, tc)
	ATF_REQUIRE_STREQ(plop, "@comment  %F is %%kof a var line %");
	free(plop);

-
	tll_free_and_free(kv, pkg_kv_free);
+
	vec_free_and_free(&kv, pkg_kv_free);
}

ATF_TP_ADD_TCS(tp)