Radish alpha
H
rad:z3QDZAW2FAfuLvihrhiyDC9fAD8G9
HardenedBSD Package Manager
Radicle
Git
frontend: replace some utlist with tllist
Baptiste Daroussin committed 4 years ago
commit 5b6ba7fd97f3b55bf136eb0fe46a03c7ea4f8f7b
parent 128504d
3 files changed +47 -89
modified src/check.c
@@ -38,27 +38,23 @@
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
-
#include <utlist.h>

#include <pkg.h>
+
#include <tllist.h>
+
#include <xmalloc.h>

#include "pkgcli.h"

-
struct deps_entry {
-
	char *name;
-
	struct deps_entry *next;
-
	struct deps_entry *prev;
-
};
+
typedef tll(char *) deps_entries;

-
static int check_deps(struct pkgdb *db, struct pkg *pkg, struct deps_entry **dh,
+
static int check_deps(struct pkgdb *db, struct pkg *pkg, deps_entries *dh,
    bool noinstall, xstring *out);
-
static void add_missing_dep(struct pkg_dep *d, struct deps_entry **dh, int *nbpkgs);
-
static void deps_free(struct deps_entry *dh);
-
static int fix_deps(struct pkgdb *db, struct deps_entry *dh, int nbpkgs);
-
static void check_summary(struct pkgdb *db, struct deps_entry *dh);
+
static void add_missing_dep(struct pkg_dep *d, deps_entries *dh, int *nbpkgs);
+
static int fix_deps(struct pkgdb *db, deps_entries *dh, int nbpkgs);
+
static void check_summary(struct pkgdb *db, deps_entries *dh);

static int
-
check_deps(struct pkgdb *db, struct pkg *p, struct deps_entry **dh, bool noinstall, xstring *out)
+
check_deps(struct pkgdb *db, struct pkg *p, deps_entries *dh, bool noinstall, xstring *out)
{
	struct pkg_dep *dep = NULL;
	struct pkgdb_it *it;
@@ -117,9 +113,8 @@ check_deps(struct pkgdb *db, struct pkg *p, struct deps_entry **dh, bool noinsta
}

static void
-
add_missing_dep(struct pkg_dep *d, struct deps_entry **dh, int *nbpkgs)
+
add_missing_dep(struct pkg_dep *d, deps_entries *dh, int *nbpkgs)
{
-
	struct deps_entry *e = NULL;
	const char *name = NULL;

	assert(d != NULL);
@@ -127,38 +122,19 @@ add_missing_dep(struct pkg_dep *d, struct deps_entry **dh, int *nbpkgs)
	/* do not add duplicate entries in the queue */
	name = pkg_dep_name(d);

-
	DL_FOREACH(*dh, e) {
-
		if (strcmp(e->name, name) == 0)
+
	tll_foreach(*dh, it) {
+
		if (strcmp(it->item, name) == 0)
			return;
	}
-

-
	if ((e = calloc(1, sizeof(struct deps_entry))) == NULL)
-
		err(1, "calloc(deps_entry)");
-

-
	e->name = strdup(name);
-

	(*nbpkgs)++;

-
	DL_APPEND(*dh, e);
-
}
-

-
static void
-
deps_free(struct deps_entry *dh)
-
{
-
	struct deps_entry *e, *etmp;
-

-
	DL_FOREACH_SAFE(dh, e, etmp) {
-
		DL_DELETE(dh, e);
-
		free(e->name);
-
		free(e);
-
	}
+
	tll_push_back(*dh, xstrdup(name));
}

static int
-
fix_deps(struct pkgdb *db, struct deps_entry *dh, int nbpkgs)
+
fix_deps(struct pkgdb *db, deps_entries *dh, int nbpkgs)
{
	struct pkg_jobs *jobs = NULL;
-
	struct deps_entry *e = NULL;
	char **pkgs = NULL;
	int i = 0;
	bool rc;
@@ -170,8 +146,8 @@ fix_deps(struct pkgdb *db, struct deps_entry *dh, int nbpkgs)
	if ((pkgs = calloc(nbpkgs, sizeof (char *))) == NULL)
		err(1, "calloc()");

-
	DL_FOREACH(dh, e)
-
		pkgs[i++] = e->name;
+
	tll_foreach(*dh, it)
+
		pkgs[i++] = it->item;

	if (pkgdb_open(&db, PKGDB_REMOTE) != EPKG_OK) {
		free(pkgs);
@@ -224,9 +200,8 @@ cleanup:
}

static void
-
check_summary(struct pkgdb *db, struct deps_entry *dh)
+
check_summary(struct pkgdb *db, deps_entries *dh)
{
-
	struct deps_entry *e = NULL;
	struct pkg *pkg = NULL;
	struct pkgdb_it *it = NULL;
	bool fixed = true;
@@ -235,15 +210,15 @@ check_summary(struct pkgdb *db, struct deps_entry *dh)

	printf(">>> Summary of actions performed:\n\n");

-
	DL_FOREACH(dh, e) {
-
		if ((it = pkgdb_query(db, e->name, MATCH_EXACT)) == NULL)
+
	tll_foreach(*dh, e) {
+
		if ((it = pkgdb_query(db, e->item, MATCH_EXACT)) == NULL)
			return;

		if (pkgdb_it_next(it, &pkg, PKG_LOAD_BASIC) != EPKG_OK) {
			fixed = false;
-
			printf("%s dependency failed to be fixed\n", e->name);
+
			printf("%s dependency failed to be fixed\n", e->item);
		} else
-
			printf("%s dependency has been fixed\n", e->name);
+
			printf("%s dependency has been fixed\n", e->item);

		pkgdb_it_free(it);
	}
@@ -306,7 +281,7 @@ exec_check(int argc, char **argv)
		{ NULL,			0,		NULL,	0   },
	};

-
	struct deps_entry *dh = NULL;
+
	deps_entries dh = tll_init();

	processed = 0;

@@ -526,9 +501,9 @@ exec_check(int argc, char **argv)
			printf(">>> Found %d issue(s) in the package database.\n\n", nbpkgs);
			if (pkgdb_upgrade_lock(db, PKGDB_LOCK_ADVISORY,
					PKGDB_LOCK_EXCLUSIVE) == EPKG_OK) {
-
				ret = fix_deps(db, dh, nbpkgs);
+
				ret = fix_deps(db, &dh, nbpkgs);
				if (ret == EPKG_OK)
-
					check_summary(db, dh);
+
					check_summary(db, &dh);
				else if (ret == EPKG_ENODB) {
					db = NULL;
					rc = EXIT_FAILURE;
@@ -551,7 +526,7 @@ cleanup:
	if (!verbose)
		progressbar_stop();
	xstring_free(msg);
-
	deps_free(dh);
+
	tll_free_and_free(dh, free);
	pkg_free(pkg);
	pkgdb_release_lock(db, PKGDB_LOCK_ADVISORY);
	pkgdb_close(db);
modified src/create.c
@@ -48,16 +48,11 @@
#include <string.h>
#include <strings.h>
#include <unistd.h>
-
#include <utlist.h>
+
#include <tllist.h>

#include "pkgcli.h"

-
struct pkg_entry {
-
	struct pkg *pkg;
-
	struct pkg_entry *next;
-
	struct pkg_entry *prev;
-
};
-
struct pkg_entry *pkg_head = NULL;
+
tll(struct pkg *) pkg_head = tll_init();

void
usage_create(void)
@@ -86,7 +81,6 @@ pkg_create_matches(int argc, char **argv, match_t match, struct pkg_create *pc)
	    PKG_LOAD_USERS | PKG_LOAD_GROUPS | PKG_LOAD_SHLIBS_REQUIRED |
	    PKG_LOAD_PROVIDES | PKG_LOAD_REQUIRES |
	    PKG_LOAD_SHLIBS_PROVIDED | PKG_LOAD_ANNOTATIONS | PKG_LOAD_LUA_SCRIPTS;
-
	struct pkg_entry *e = NULL, *etmp;
	bool foundone;

	if (pkgdb_open(&db, PKGDB_DEFAULT) != EPKG_OK) {
@@ -112,11 +106,8 @@ pkg_create_matches(int argc, char **argv, match_t match, struct pkg_create *pc)

		foundone = false;
		while ((ret = pkgdb_it_next(it, &pkg, query_flags)) == EPKG_OK) {
-
			if ((e = malloc(sizeof(struct pkg_entry))) == NULL)
-
				err(1, "malloc(pkg_entry)");
-
			e->pkg = pkg;
			pkg = NULL;
-
			DL_APPEND(pkg_head, e);
+
			tll_push_back(pkg_head, pkg);
			foundone = true;
		}
		if (!foundone) {
@@ -130,18 +121,16 @@ pkg_create_matches(int argc, char **argv, match_t match, struct pkg_create *pc)
			retcode = EXIT_FAILURE;
	}

-
	DL_FOREACH_SAFE(pkg_head, e, etmp) {
-
		DL_DELETE(pkg_head, e);
-
		pkg_printf("Creating package for %n-%v\n", e->pkg, e->pkg);
-
		ret = pkg_create_i(pc, e->pkg, false);
+
	tll_foreach(pkg_head, el) {
+
		pkg_printf("Creating package for %n-%v\n", el->item, el->item);
+
		ret = pkg_create_i(pc, el->item, false);
		if (ret == EPKG_EXIST) {
			pkg_printf("%n-%v already packaged, skipping...\n",
-
			  e->pkg, e->pkg);
+
			  el->item, el->item);
		}
		if (ret != EPKG_OK && ret != EPKG_EXIST)
			retcode = EXIT_FAILURE;
-
		pkg_free(e->pkg);
-
		free(e);
+
		tll_remove_and_free(pkg_head, el, pkg_free);
	}

cleanup:
modified src/main.c
@@ -54,7 +54,6 @@
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
-
#include <utlist.h>
#include <unistd.h>
#ifdef HAVE_LIBJAIL
#include <jail.h>
@@ -63,6 +62,8 @@
#include <signal.h>

#include <pkg.h>
+
#include <tllist.h>
+
#include <xmalloc.h>

#include "pkgcli.h"

@@ -127,10 +128,8 @@ struct plugcmd {
	const char *name;
	const char *desc;
	int (*exec)(int argc, char **argv);
-
	struct plugcmd *next;
-
	struct plugcmd *prev;
};
-
static struct plugcmd *plugins = NULL;
+
static tll(struct plugcmd *)plugins = tll_init();

typedef int (register_cmd)(int idx, const char **name, const char **desc, int (**exec)(int argc, char **argv));
typedef int (nb_cmd)(void);
@@ -149,7 +148,6 @@ show_command_names(void)
static void
usage(const char *conffile, const char *reposdir, FILE *out, enum pkg_usage_reason reason, ...)
{
-
	struct plugcmd *c;
	bool plugins_enabled = false;
	unsigned int i;
	const char *arg;
@@ -207,8 +205,9 @@ usage(const char *conffile, const char *reposdir, FILE *out, enum pkg_usage_reas

			fprintf(out, "\nCommands provided by plugins:\n");

-
			DL_FOREACH(plugins, c) {
-
				fprintf(out, "\t%-15s%s\n", c->name, c->desc);
+
			tll_foreach(plugins, it) {
+
				fprintf(out, "\t%-15s%s\n", it->item->name,
+
				    it->item->desc);
			}
		}
		fprintf(out, "\nFor more information on the different commands"
@@ -232,7 +231,6 @@ exec_help(int argc, char **argv)
{
	char *manpage;
	bool plugins_enabled = false;
-
	struct plugcmd *c;
	unsigned int i;
	const pkg_object *all_aliases;
	const pkg_object *alias;
@@ -245,9 +243,7 @@ exec_help(int argc, char **argv)

	for (i = 0; i < cmd_len; i++) {
		if (strcmp(cmd[i].name, argv[1]) == 0) {
-
			if (asprintf(&manpage, "/usr/bin/man pkg-%s", cmd[i].name) == -1)
-
				errx(EXIT_FAILURE, "cannot allocate memory");
-

+
			xasprintf(&manpage, "/usr/bin/man pkg-%s", cmd[i].name);
			system(manpage);
			free(manpage);

@@ -258,11 +254,9 @@ exec_help(int argc, char **argv)
	plugins_enabled = pkg_object_bool(pkg_config_get("PKG_ENABLE_PLUGINS"));

	if (plugins_enabled) {
-
		DL_FOREACH(plugins, c) {
-
			if (strcmp(c->name, argv[1]) == 0) {
-
				if (asprintf(&manpage, "/usr/bin/man pkg-%s", c->name) == -1)
-
					errx(EXIT_FAILURE, "cannot allocate memory");
-

+
		tll_foreach(plugins, it) {
+
			if (strcmp(it->item->name, argv[1]) == 0) {
+
				xasprintf(&manpage, "/usr/bin/man pkg-%s", it->item->name);
				system(manpage);
				free(manpage);

@@ -802,9 +796,9 @@ main(int argc, char **argv)
			if (reg != NULL && ncmd != NULL) {
				n = ncmd();
				for (j = 0; j < n ; j++) {
-
					c = malloc(sizeof(struct plugcmd));
+
					c = xmalloc(sizeof(struct plugcmd));
					reg(j, &c->name, &c->desc, &c->exec);
-
					DL_APPEND(plugins, c);
+
					tll_push_back(plugins, c);
				}
			}
		}
@@ -870,10 +864,10 @@ main(int argc, char **argv)
		/* Check if a plugin provides the requested command */
		ret = EPKG_FATAL;
		if (plugins_enabled) {
-
			DL_FOREACH(plugins, c) {
-
				if (strcmp(c->name, argv[0]) == 0) {
+
			tll_foreach(plugins, it) {
+
				if (strcmp(it->item->name, argv[0]) == 0) {
					plugin_found = true;
-
					ret = c->exec(argc, argv);
+
					ret = it->item->exec(argc, argv);
					break;
				}
			}