Radish alpha
H
rad:z3QDZAW2FAfuLvihrhiyDC9fAD8G9
HardenedBSD Package Manager
Radicle
Git
Prepare pkg_jobs interface for multi repos
Marin Atanasov Nikolov committed 14 years ago
commit 4ef9d0d6b87529df9204d400f423a22c85af4432
parent 806f467
6 files changed +183 -92
modified libpkg/pkg.h
@@ -18,6 +18,7 @@ struct pkgdb;
struct pkgdb_it;

struct pkg_jobs;
+
struct pkg_jobs_entry;

struct pkg_repos;
struct pkg_repos_entry;
@@ -535,35 +536,49 @@ int pkgdb_compact(struct pkgdb *db);
int pkg_add(struct pkgdb *db, const char *path);

/**
-
 * Allocate a new pkg_jobs.
+
 * Create a new jobs object
+
 * @return EPKG_OK on success, EPKG_FATAL on error
+
 */
+
int pkg_jobs_new(struct pkg_jobs **jobs);
+

+
/**
+
 * Allocate a new pkg_jobs_entry object.
 * @param db A pkgdb open with PKGDB_REMOTE.
 * @return An error code.
 */
-
int pkg_jobs_new(struct pkg_jobs **jobs, pkg_jobs_t type, struct pkgdb *db);
+
int pkg_jobs_new_entry(struct pkg_jobs *jobs, struct pkg_jobs_entry **je, pkg_jobs_t type, struct pkgdb *db);

/**
-
 * Free a pkg_jobs
+
 * Free a pkg_jobs object
 */
void pkg_jobs_free(struct pkg_jobs *jobs);

/**
-
 * Add a pkg to the jobs queue.
+
 * Add a pkg to the jobs entry queue.
 * @return An error code.
 */
-
int pkg_jobs_add(struct pkg_jobs *jobs, struct pkg *pkg);
+
int pkg_jobs_add(struct pkg_jobs_entry *je, struct pkg *pkg);
+

+
/**
+
 * Iterates over the jobs entry objects
+
 * @param je Returns the next entry in a jobs object.
+
 * Must be set to NULL for the first call.
+
 * @return EPKG_OK on success, EPKG_END if end of tail is reached.
+
 */
+
int pkg_jobs(struct pkg_jobs *jobs, struct pkg_jobs_entry **je);

/**
 * Iterates over the packages in the jobs queue.
 * @param pkg Must be set to NULL for the first call.
 * @return An error code.
 */
-
int pkg_jobs(struct pkg_jobs *jobs, struct pkg **pkg);
+
int pkg_jobs_entry(struct pkg_jobs_entry *je, struct pkg **pkg);

/**
-
 * Apply the jobs in the queue (fetch and install).
+
 * Apply the jobs in the queue (fetch/install/deinstall).
 * @return An error code.
 */
-
int pkg_jobs_apply(struct pkg_jobs *jobs, int force);
+
int pkg_jobs_apply(struct pkg_jobs_entry *je, int force);

/**
 * Archive formats options.
modified libpkg/pkg_jobs.c
@@ -8,26 +8,59 @@
#include "pkg_private.h"

int
-
pkg_jobs_new(struct pkg_jobs **j, pkg_jobs_t t, struct pkgdb *db)
+
pkg_jobs_new(struct pkg_jobs **jm)
{
+
	if ((*jm = calloc(1, sizeof(struct pkg_jobs))) == NULL) {
+
		EMIT_ERRNO("calloc", "pkg_jobs_new");
+
		return (EPKG_FATAL);
+
	}
+

+
	STAILQ_INIT(&(*jm)->multi);
+

+
	return (EPKG_OK);
+
}
+

+
int
+
pkg_jobs_new_entry(struct pkg_jobs *jm, struct pkg_jobs_entry **je, pkg_jobs_t t, struct pkgdb *db)
+
{
+
	assert(jm != NULL);
	assert(db != NULL);
	assert(t != PKG_JOBS_INSTALL || db->type == PKGDB_REMOTE);

-
	if((*j = calloc(1, sizeof(struct pkg_jobs))) == NULL) {
-
		EMIT_ERRNO("calloc", "pkg_jobs");
+
	if((*je = calloc(1, sizeof(struct pkg_jobs_entry))) == NULL) {
+
		EMIT_ERRNO("calloc", "pkg_jobs_new_entry");
		return (EPKG_FATAL);
	}

-
	STAILQ_INIT(&(*j)->jobs);
-
	LIST_INIT(&(*j)->nodes);
-
	(*j)->db = db;
-
	(*j)->type = t;
+
	STAILQ_INIT(&(*je)->jobs);
+
	LIST_INIT(&(*je)->nodes);
+
	(*je)->db = db;
+
	(*je)->type = t;
+

+
	STAILQ_INSERT_TAIL(&jm->multi, *je, next);

	return (EPKG_OK);
}

void
-
pkg_jobs_free(struct pkg_jobs *j)
+
pkg_jobs_free(struct pkg_jobs *jm)
+
{
+
	struct pkg_jobs_entry *je;
+

+
	if (jm == NULL)
+
		return;
+

+
	while (!STAILQ_EMPTY(&jm->multi)) {
+
		je = STAILQ_FIRST(&jm->multi);
+
		STAILQ_REMOVE_HEAD(&jm->multi, next);
+
		pkg_jobs_free_entry(je);
+
	}
+

+
	free(jm);
+
}
+

+
static void
+
pkg_jobs_free_entry(struct pkg_jobs_entry *j)
{
	struct pkg *p;

@@ -39,29 +72,46 @@ pkg_jobs_free(struct pkg_jobs *j)
		STAILQ_REMOVE_HEAD(&j->jobs, next);
		pkg_free(p);
	}
+

	free(j);
}

int
-
pkg_jobs_add(struct pkg_jobs *j, struct pkg *pkg)
+
pkg_jobs_add(struct pkg_jobs_entry *je, struct pkg *pkg)
{
-
	assert(j != NULL);
+
	assert(je != NULL);
	assert(pkg != NULL);

-
	STAILQ_INSERT_TAIL(&j->jobs, pkg, next);
+
	STAILQ_INSERT_TAIL(&je->jobs, pkg, next);

	return (EPKG_OK);
}

int
-
pkg_jobs(struct pkg_jobs *j, struct pkg **pkg)
+
pkg_jobs(struct pkg_jobs *jm, struct pkg_jobs_entry **je)
{
-
	assert(j != NULL);
+
	assert(jm != NULL);

-
	pkg_jobs_resolv(j);
+
	if (*je == NULL)
+
		*je = STAILQ_FIRST(&jm->multi);
+
	else
+
		*je = STAILQ_NEXT(*je, next);
+

+
	if (*je == NULL)
+
		return (EPKG_END);
+
	else
+
		return (EPKG_OK);
+
}
+

+
int
+
pkg_jobs_entry(struct pkg_jobs_entry *je, struct pkg **pkg)
+
{
+
	assert(je != NULL);
+

+
	pkg_jobs_resolv(je);

	if (*pkg == NULL)
-
		*pkg = STAILQ_FIRST(&j->jobs);
+
		*pkg = STAILQ_FIRST(&je->jobs);
	else
		*pkg = STAILQ_NEXT(*pkg, next);

@@ -72,14 +122,14 @@ pkg_jobs(struct pkg_jobs *j, struct pkg **pkg)
}

static int
-
pkg_jobs_install(struct pkg_jobs *j)
+
pkg_jobs_install(struct pkg_jobs_entry *je)
{
	struct pkg *p = NULL;
	const char *cachedir;
	char path[MAXPATHLEN];

	/* Fetch */
-
	while (pkg_jobs(j, &p) == EPKG_OK) {
+
	while (pkg_jobs_entry(je, &p) == EPKG_OK) {
		if (pkg_repo_fetch(p) != EPKG_OK)
			return (EPKG_FATAL);
	}
@@ -87,11 +137,11 @@ pkg_jobs_install(struct pkg_jobs *j)
	/* Install */
	cachedir = pkg_config("PKG_CACHEDIR");
	p = NULL;
-
	while (pkg_jobs(j, &p) == EPKG_OK) {
+
	while (pkg_jobs_entry(je, &p) == EPKG_OK) {
		snprintf(path, sizeof(path), "%s/%s", cachedir,
				 pkg_get(p, PKG_REPOPATH));

-
		if (pkg_add(j->db, path) != EPKG_OK) {
+
		if (pkg_add(je->db, path) != EPKG_OK) {
			return (EPKG_FATAL);
		}
	}
@@ -100,13 +150,13 @@ pkg_jobs_install(struct pkg_jobs *j)
}

static int
-
pkg_jobs_deinstall(struct pkg_jobs *j, int force)
+
pkg_jobs_deinstall(struct pkg_jobs_entry *je, int force)
{
	struct pkg *p = NULL;
-
	int retcode;
+
	int retcode = EPKG_OK;

-
	while (pkg_jobs(j, &p) == EPKG_OK) {
-
		retcode = pkg_delete(p, j->db, force);
+
	while (pkg_jobs_entry(je, &p) == EPKG_OK) {
+
		retcode = pkg_delete(p, je->db, force);
		if (retcode != EPKG_OK)
			return (retcode);
	}
@@ -115,24 +165,24 @@ pkg_jobs_deinstall(struct pkg_jobs *j, int force)
}

int
-
pkg_jobs_apply(struct pkg_jobs *j, int force)
+
pkg_jobs_apply(struct pkg_jobs_entry *je, int force)
{
-
	if (j->type == PKG_JOBS_INSTALL)
-
		return (pkg_jobs_install(j));
-
	if (j->type == PKG_JOBS_DEINSTALL)
-
		return (pkg_jobs_deinstall(j, force));
+
	if (je->type == PKG_JOBS_INSTALL)
+
		return (pkg_jobs_install(je));
+
	if (je->type == PKG_JOBS_DEINSTALL)
+
		return (pkg_jobs_deinstall(je, force));

	EMIT_PKG_ERROR("%s", "bad jobs argument");
	return (EPKG_FATAL);
}

static struct pkg_jobs_node *
-
get_node(struct pkg_jobs *j, const char *name, int create)
+
get_node(struct pkg_jobs_entry *je, const char *name, int create)
{
-
	struct pkg_jobs_node *n;
+
	struct pkg_jobs_node *n = NULL;

	/* XXX hashmap? */
-
	LIST_FOREACH(n, &j->nodes, entries) {
+
	LIST_FOREACH(n, &je->nodes, entries) {
		if (strcmp(name, pkg_get(n->pkg, PKG_ORIGIN)) == 0) {
			return (n);
		}
@@ -142,7 +192,7 @@ get_node(struct pkg_jobs *j, const char *name, int create)
		return (NULL);

	n = calloc(1, sizeof(struct pkg_jobs_node));
-
	LIST_INSERT_HEAD(&j->nodes, n, entries);
+
	LIST_INSERT_HEAD(&je->nodes, n, entries);
	return (n);
}

@@ -164,48 +214,48 @@ add_parent(struct pkg_jobs_node *n, struct pkg_jobs_node *p)
}

static void
-
add_dep(struct pkg_jobs *j, struct pkg_jobs_node *n)
+
add_dep(struct pkg_jobs_entry *je, struct pkg_jobs_node *n)
{
	struct pkg_dep *dep = NULL;
-
	struct pkg_jobs_node *ndep;
+
	struct pkg_jobs_node *ndep = NULL;

	while (pkg_deps(n->pkg, &dep) != EPKG_END) {
-
		ndep = get_node(j, pkg_dep_origin(dep), 1);
+
		ndep = get_node(je, pkg_dep_origin(dep), 1);
		if (ndep->pkg == NULL) {
-
			ndep->pkg = pkgdb_query_remote(j->db, pkg_dep_origin(dep));
+
			ndep->pkg = pkgdb_query_remote(je->db, pkg_dep_origin(dep));
			if (ndep->pkg == NULL)
				EMIT_MISSING_DEP(n->pkg, dep);
			else
-
				add_dep(j, ndep);
+
				add_dep(je, ndep);
		}
		add_parent(ndep, n);
	}
}

static void
-
add_rdep(struct pkg_jobs *j, struct pkg_jobs_node *n)
+
add_rdep(struct pkg_jobs_entry *je, struct pkg_jobs_node *n)
{
-
	struct pkg_jobs_node *nrdep;
+
	struct pkg_jobs_node *nrdep = NULL;
	struct pkg_dep *rdep = NULL;

-
	pkgdb_loadrdeps(j->db, n->pkg);
+
	pkgdb_loadrdeps(je->db, n->pkg);

	while (pkg_rdeps(n->pkg, &rdep) == EPKG_OK) {
-
		nrdep = get_node(j, pkg_dep_origin(rdep), 0);
+
		nrdep = get_node(je, pkg_dep_origin(rdep), 0);
		if (nrdep != NULL)
			add_parent(nrdep, n);
	}
}

static void
-
remove_node(struct pkg_jobs *j, struct pkg_jobs_node *n)
+
remove_node(struct pkg_jobs_entry *je, struct pkg_jobs_node *n)
{
-
	struct pkg_jobs_node *np;
-
	size_t i;
+
	struct pkg_jobs_node *np = 0;
+
	size_t i = 0;

	assert(n->nrefs == 0);

-
	STAILQ_INSERT_TAIL(&j->jobs, n->pkg, next);
+
	STAILQ_INSERT_TAIL(&je->jobs, n->pkg, next);

	LIST_REMOVE(n, entries);

@@ -218,42 +268,42 @@ remove_node(struct pkg_jobs *j, struct pkg_jobs_node *n)
}

int
-
pkg_jobs_resolv(struct pkg_jobs *j)
+
pkg_jobs_resolv(struct pkg_jobs_entry *je)
{
-
	struct pkg_jobs_node *n, *tmp;
-
	struct pkg *p;
+
	struct pkg_jobs_node *n = NULL, *tmp = NULL;
+
	struct pkg *p = NULL;

-
	assert(j != NULL);
+
	assert(je != NULL);

-
	if (j->resolved == 1)
+
	if (je->resolved == 1)
		return (EPKG_OK);

	/* Create nodes and remove jobs form the queue */
-
	while (!STAILQ_EMPTY(&j->jobs)) {
-
		p = STAILQ_FIRST(&j->jobs);
-
		STAILQ_REMOVE_HEAD(&j->jobs, next);
+
	while (!STAILQ_EMPTY(&je->jobs)) {
+
		p = STAILQ_FIRST(&je->jobs);
+
		STAILQ_REMOVE_HEAD(&je->jobs, next);

-
		n = get_node(j, pkg_get(p, PKG_ORIGIN), 1);
+
		n = get_node(je, pkg_get(p, PKG_ORIGIN), 1);

		n->pkg = p;
	}

	/* Add dependencies into nodes */
-
	LIST_FOREACH(n, &j->nodes, entries) {
-
		if (j->type == PKG_JOBS_INSTALL)
-
			add_dep(j, n);
-
		if (j->type == PKG_JOBS_DEINSTALL)
-
			add_rdep(j, n);
+
	LIST_FOREACH(n, &je->nodes, entries) {
+
		if (je->type == PKG_JOBS_INSTALL)
+
			add_dep(je, n);
+
		if (je->type == PKG_JOBS_DEINSTALL)
+
			add_rdep(je, n);
	}

	/* Resolv !*/
	do {
-
		LIST_FOREACH_SAFE(n, &j->nodes, entries, tmp) {
+
		LIST_FOREACH_SAFE(n, &je->nodes, entries, tmp) {
			if (n->nrefs == 0)
-
				remove_node(j, n);
+
				remove_node(je, n);
		}
-
	} while (!LIST_EMPTY(&j->nodes));
+
	} while (!LIST_EMPTY(&je->nodes));

-
	j->resolved = 1;
+
	je->resolved = 1;
	return (EPKG_OK);
}
modified libpkg/pkg_private.h
@@ -83,11 +83,16 @@ struct pkg_option {
};

struct pkg_jobs {
-
	STAILQ_HEAD(jobs, pkg) jobs;
-
	LIST_HEAD(nodes, pkg_jobs_node) nodes;
-
	struct pkgdb *db;
-
	pkg_jobs_t type;
-
	unsigned int resolved :1;
+
	struct pkg_jobs_entry {
+
		STAILQ_HEAD(jobs, pkg) jobs;
+
		LIST_HEAD(nodes, pkg_jobs_node) nodes;
+
		struct pkgdb *db;
+
		pkg_jobs_t type;
+
		unsigned int resolved :1;
+
		STAILQ_ENTRY(pkg_jobs_entry) next;
+
	};
+

+
	STAILQ_HEAD(jobs_multi, pkg_jobs_entry) multi;
};

struct pkg_jobs_node {
@@ -100,15 +105,16 @@ struct pkg_jobs_node {
};

struct pkg_repos {
+
	struct pkg_repos_entry {
+
		char *name;
+
		char *url;
+
		unsigned int line;
+
		STAILQ_ENTRY(pkg_repos_entry) entries;
+
	};
+

	STAILQ_HEAD(repos, pkg_repos_entry) nodes;
};

-
struct pkg_repos_entry {
-
	char *name;
-
	char *url;
-
	unsigned int line;
-
	STAILQ_ENTRY(pkg_repos_entry) entries;
-
};

int pkg_open2(struct pkg **p, struct archive **a, struct archive_entry **ae, const char *path);
void pkg_freecategories(struct pkg *pkg);
@@ -141,7 +147,7 @@ void pkg_script_free(struct pkg_script *);
int pkg_option_new(struct pkg_option **);
void pkg_option_free(struct pkg_option *);

-
int pkg_jobs_resolv(struct pkg_jobs *jobs);
+
int pkg_jobs_resolv(struct pkg_jobs_entry *je);

struct packing;

modified libpkg/pkg_repo.c
@@ -228,7 +228,8 @@ pkg_repos_free(struct pkg_repos *repos)
{
	struct pkg_repos_entry *re1, *re2;

-
	assert(repos != NULL);
+
	if (repos == NULL)
+
		return;

        re1 = STAILQ_FIRST(&repos->nodes);
        while (re1 != NULL) {
@@ -251,7 +252,8 @@ pkg_repos_free_in_pkg(struct pkg *pkg)
{
	struct pkg_repos_entry *re1, *re2;

-
	assert(pkg != NULL);
+
	if (pkg == NULL)
+
		return;

	re1 = STAILQ_FIRST(&pkg->repos);
	while (re1 != NULL) {
modified pkg/delete.c
@@ -21,6 +21,7 @@ int
exec_delete(int argc, char **argv)
{
	struct pkg_jobs *jobs = NULL;
+
	struct pkg_jobs_entry *je = NULL;
	struct pkg *pkg = NULL;
	struct pkgdb *db = NULL;
	struct pkgdb_it *it = NULL;
@@ -64,7 +65,15 @@ exec_delete(int argc, char **argv)
	if (argc == 1)
		origin = argv[0];

-
	if ((retcode = pkg_jobs_new(&jobs, PKG_JOBS_DEINSTALL, db)) != EPKG_OK) {
+
	/* create a jobs object */
+
	if (pkg_jobs_new(&jobs) != EPKG_OK) {
+
		retcode = EPKG_FATAL;
+
		goto cleanup;
+
	}
+

+
	/* create a jobs entry */
+
	if (pkg_jobs_new_entry(jobs, &je, PKG_JOBS_DEINSTALL, db) != EPKG_OK) {
+
		retcode = EPKG_FATAL;
		goto cleanup;
	}

@@ -74,7 +83,7 @@ exec_delete(int argc, char **argv)
	}

	while ((retcode = pkgdb_it_next(it, &pkg, flags)) == EPKG_OK) {
-
		pkg_jobs_add(jobs, pkg);
+
		pkg_jobs_add(je, pkg);
		pkg = NULL;
	}

@@ -82,7 +91,7 @@ exec_delete(int argc, char **argv)
		goto cleanup;
	}

-
	if ((retcode = pkg_jobs_apply(jobs, force)) != EPKG_OK) {
+
	if ((retcode = pkg_jobs_apply(je, force)) != EPKG_OK) {
		goto cleanup;
	}

modified pkg/install.c
@@ -25,6 +25,7 @@ exec_install(int argc, char **argv)
	struct pkg *pkg = NULL;
	struct pkgdb *db = NULL;
	struct pkg_jobs *jobs = NULL;
+
	struct pkg_jobs_entry *je = NULL;
	int retcode = EPKG_OK;
	int i;

@@ -42,7 +43,15 @@ exec_install(int argc, char **argv)
		return (EX_IOERR);
	}

-
	if (pkg_jobs_new(&jobs, PKG_JOBS_INSTALL, db) != EPKG_OK) {
+

+
	/* create a jobs object */
+
	if (pkg_jobs_new(&jobs) != EPKG_OK) {
+
		retcode = EPKG_FATAL;
+
		goto cleanup;
+
	}
+

+
	/* create a jobs entry */
+
	if (pkg_jobs_new_entry(jobs, &je, PKG_JOBS_INSTALL, db) != EPKG_OK) {
		retcode = EPKG_FATAL;
		goto cleanup;
	}
@@ -53,17 +62,17 @@ exec_install(int argc, char **argv)
			goto cleanup;
		}

-
		pkg_jobs_add(jobs, pkg);
+
		pkg_jobs_add(je, pkg);
	}

	/* print a summary before applying the jobs */
	pkg = NULL;
	printf("The following packages will be installed:\n");
-
	while (pkg_jobs(jobs, &pkg) == EPKG_OK) {
+
	while (pkg_jobs_entry(je, &pkg) == EPKG_OK) {
		printf("%s-%s\n", pkg_get(pkg, PKG_NAME), pkg_get(pkg, PKG_VERSION));
	}

-
	retcode = pkg_jobs_apply(jobs, 0);
+
	retcode = pkg_jobs_apply(je, 0);

	cleanup:
	pkgdb_close(db);