Radish alpha
H
rad:z3QDZAW2FAfuLvihrhiyDC9fAD8G9
HardenedBSD Package Manager
Radicle
Git
pkgdb: convert repo handling to tllist
Baptiste Daroussin committed 3 years ago
commit b0b323129d96ea0ae2d887d44949ede6e69b6bd6
parent 6d0c152
4 files changed +43 -60
modified libpkg/pkgdb.c
@@ -949,7 +949,6 @@ static int
pkgdb_open_repos(struct pkgdb *db, const char *reponame)
{
	struct pkg_repo *r = NULL;
-
	struct _pkg_repo_list_item *item;

	while (pkg_repos(&r) == EPKG_OK) {
		if (!r->enable) {
@@ -959,10 +958,8 @@ pkgdb_open_repos(struct pkgdb *db, const char *reponame)
		if (reponame == NULL || strcasecmp(r->name, reponame) == 0) {
			/* We need read only access here */
			if (r->ops->open(r, R_OK) == EPKG_OK) {
-
				item = xmalloc(sizeof(*item));
				r->ops->init(r);
-
				item->repo = r;
-
				LL_PREPEND(db->repos, item);
+
				tll_push_front(db->repos, r);
			} else
				pkg_emit_error("Repository %s cannot be opened."
				    " 'pkg update' required", r->name);
@@ -1207,11 +1204,15 @@ retry:
	return (EPKG_OK);
}

+
static void
+
pkgdb_free_repo(struct pkg_repo *repo)
+
{
+
	repo->ops->close(repo, false);
+
}
+

void
pkgdb_close(struct pkgdb *db)
{
-
	struct _pkg_repo_list_item *cur, *tmp;
-

	if (db == NULL)
		return;

@@ -1220,10 +1221,7 @@ pkgdb_close(struct pkgdb *db)

	if (db->sqlite != NULL) {

-
		LL_FOREACH_SAFE(db->repos, cur, tmp) {
-
			cur->repo->ops->close(cur->repo, false);
-
			free(cur);
-
		}
+
		tll_free_and_free(db->repos, pkgdb_free_repo);

		if (!sqlite3_db_readonly(db->sqlite, "main"))
			pkg_plugins_hook_run(PKG_PLUGIN_HOOK_PKGDB_CLOSE_RW, NULL, db);
@@ -3001,7 +2999,6 @@ pkgdb_stats(struct pkgdb *db, pkg_stats_t type)
	sqlite3_stmt	*stmt = NULL;
	int64_t		 stats = 0;
	const char *sql = NULL;
-
	struct _pkg_repo_list_item *rit;

	assert(db != NULL);

@@ -3015,19 +3012,14 @@ pkgdb_stats(struct pkgdb *db, pkg_stats_t type)
	case PKG_STATS_REMOTE_UNIQUE:
	case PKG_STATS_REMOTE_COUNT:
	case PKG_STATS_REMOTE_SIZE:
-
		LL_FOREACH(db->repos, rit) {
-
			struct pkg_repo *repo = rit->repo;
-

-
			if (repo->ops->stat != NULL)
-
				stats += repo->ops->stat(repo, type);
+
		tll_foreach(db->repos, rit) {
+
			if (rit->item->ops->stat != NULL)
+
				stats += rit->item->ops->stat(rit->item, type);
		}
		return (stats);
		break;
	case PKG_STATS_REMOTE_REPOS:
-
		LL_FOREACH(db->repos, rit) {
-
			stats ++;
-
		}
-
		return (stats);
+
		return (tll_length(db->repos));
		break;
	}

modified libpkg/pkgdb_iterator.c
@@ -1303,17 +1303,16 @@ int
pkgdb_ensure_loaded(struct pkgdb *db, struct pkg *pkg, unsigned flags)
{
	int ret;
-
	struct _pkg_repo_list_item *cur;

	if (pkg->type == PKG_INSTALLED) {
		return (pkgdb_ensure_loaded_sqlite(db->sqlite, pkg, flags));
	}
	else {
		/* Call repo functions */
-
		LL_FOREACH(db->repos, cur) {
-
			if (cur->repo == pkg->repo) {
-
				if (cur->repo->ops->ensure_loaded) {
-
					ret = cur->repo->ops->ensure_loaded(cur->repo, pkg, flags);
+
		tll_foreach(db->repos, cur) {
+
			if (cur->item == pkg->repo) {
+
				if (cur->item->ops->ensure_loaded) {
+
					ret = cur->item->ops->ensure_loaded(cur->item, pkg, flags);
					return (ret);
				}
			}
modified libpkg/pkgdb_query.c
@@ -350,15 +350,14 @@ pkgdb_repo_query_cond(struct pkgdb *db, const char *cond, const char *pattern, m
{
	struct pkgdb_it *it;
	struct pkg_repo_it *rit;
-
	struct _pkg_repo_list_item *cur;

	it = pkgdb_it_new_repo(db);
	if (it == NULL)
		return (NULL);

-
	LL_FOREACH(db->repos, cur) {
-
		if (repo == NULL || strcasecmp(cur->repo->name, repo) == 0) {
-
			rit = cur->repo->ops->query(cur->repo, cond, pattern, match);
+
	tll_foreach(db->repos, cur) {
+
		if (repo == NULL || strcasecmp(cur->item->name, repo) == 0) {
+
			rit = cur->item->ops->query(cur->item, cond, pattern, match);
			if (rit != NULL)
				pkgdb_it_repo_attach(it, rit);
		}
@@ -378,16 +377,15 @@ pkgdb_repo_shlib_require(struct pkgdb *db, const char *require, const char *repo
{
	struct pkgdb_it *it;
	struct pkg_repo_it *rit;
-
	struct _pkg_repo_list_item *cur;

	it = pkgdb_it_new_repo(db);
	if (it == NULL)
		return (NULL);

-
	LL_FOREACH(db->repos, cur) {
-
		if (repo == NULL || strcasecmp(cur->repo->name, repo) == 0) {
-
			if (cur->repo->ops->shlib_required != NULL) {
-
				rit = cur->repo->ops->shlib_required(cur->repo, require);
+
	tll_foreach(db->repos, cur) {
+
		if (repo == NULL || strcasecmp(cur->item->name, repo) == 0) {
+
			if (cur->item->ops->shlib_required != NULL) {
+
				rit = cur->item->ops->shlib_required(cur->item, require);
				if (rit != NULL)
					pkgdb_it_repo_attach(it, rit);
			}
@@ -402,16 +400,15 @@ pkgdb_repo_shlib_provide(struct pkgdb *db, const char *require, const char *repo
{
	struct pkgdb_it *it;
	struct pkg_repo_it *rit;
-
	struct _pkg_repo_list_item *cur;

	it = pkgdb_it_new_repo(db);
	if (it == NULL)
		return (NULL);

-
	LL_FOREACH(db->repos, cur) {
-
		if (repo == NULL || strcasecmp(cur->repo->name, repo) == 0) {
-
			if (cur->repo->ops->shlib_required != NULL) {
-
				rit = cur->repo->ops->shlib_provided(cur->repo, require);
+
	tll_foreach(db->repos, cur) {
+
		if (repo == NULL || strcasecmp(cur->item->name, repo) == 0) {
+
			if (cur->item->ops->shlib_required != NULL) {
+
				rit = cur->item->ops->shlib_provided(cur->item, require);
				if (rit != NULL)
					pkgdb_it_repo_attach(it, rit);
			}
@@ -426,16 +423,15 @@ pkgdb_repo_require(struct pkgdb *db, const char *require, const char *repo)
{
	struct pkgdb_it *it;
	struct pkg_repo_it *rit;
-
	struct _pkg_repo_list_item *cur;

	it = pkgdb_it_new_repo(db);
	if (it == NULL)
		return (NULL);

-
	LL_FOREACH(db->repos, cur) {
-
		if (repo == NULL || strcasecmp(cur->repo->name, repo) == 0) {
-
			if (cur->repo->ops->required != NULL) {
-
				rit = cur->repo->ops->required(cur->repo, require);
+
	tll_foreach(db->repos, cur) {
+
		if (repo == NULL || strcasecmp(cur->item->name, repo) == 0) {
+
			if (cur->item->ops->required != NULL) {
+
				rit = cur->item->ops->required(cur->item, require);
				if (rit != NULL)
					pkgdb_it_repo_attach(it, rit);
			}
@@ -450,16 +446,15 @@ pkgdb_repo_provide(struct pkgdb *db, const char *require, const char *repo)
{
	struct pkgdb_it *it;
	struct pkg_repo_it *rit;
-
	struct _pkg_repo_list_item *cur;

	it = pkgdb_it_new_repo(db);
	if (it == NULL)
		return (NULL);

-
	LL_FOREACH(db->repos, cur) {
-
		if (repo == NULL || strcasecmp(cur->repo->name, repo) == 0) {
-
			if (cur->repo->ops->required != NULL) {
-
				rit = cur->repo->ops->provided(cur->repo, require);
+
	tll_foreach(db->repos, cur) {
+
		if (repo == NULL || strcasecmp(cur->item->name, repo) == 0) {
+
			if (cur->item->ops->required != NULL) {
+
				rit = cur->item->ops->provided(cur->item, require);
				if (rit != NULL)
					pkgdb_it_repo_attach(it, rit);
			}
@@ -474,16 +469,15 @@ pkgdb_repo_search(struct pkgdb *db, const char *pattern, match_t match,
{
	struct pkgdb_it *it;
	struct pkg_repo_it *rit;
-
	struct _pkg_repo_list_item *cur;

	it = pkgdb_it_new_repo(db);
	if (it == NULL)
		return (NULL);

-
	LL_FOREACH(db->repos, cur) {
-
		if (repo == NULL || strcasecmp(cur->repo->name, repo) == 0) {
-
			if (cur->repo->ops->search != NULL) {
-
				rit = cur->repo->ops->search(cur->repo, pattern, match,
+
	tll_foreach(db->repos, cur) {
+
		if (repo == NULL || strcasecmp(cur->item->name, repo) == 0) {
+
			if (cur->item->ops->search != NULL) {
+
				rit = cur->item->ops->search(cur->item, pattern, match,
					field, sort);
				if (rit != NULL)
					pkgdb_it_repo_attach(it, rit);
modified libpkg/private/pkgdb.h
@@ -1,5 +1,5 @@
/*-
-
 * Copyright (c) 2011-2012 Baptiste Daroussin <bapt@FreeBSD.org>
+
 * Copyright (c) 2011-2022 Baptiste Daroussin <bapt@FreeBSD.org>
 * Copyright (c) 2011-2012 Julien Laffaye <jlaffaye@FreeBSD.org>
 * All rights reserved.
 *
@@ -30,16 +30,14 @@

#include "pkg.h"

-
#include "sqlite3.h"
+
#include <sqlite3.h>
+
#include <tllist.h>

struct pkgdb {
	sqlite3		*sqlite;
	bool		 prstmt_initialized;

-
	struct _pkg_repo_list_item {
-
		struct pkg_repo *repo;
-
		struct _pkg_repo_list_item *next;
-
	} *repos;
+
	tll(struct pkg_repo *) repos;
};

enum pkgdb_iterator_type {