Radish alpha
H
rad:z3QDZAW2FAfuLvihrhiyDC9fAD8G9
HardenedBSD Package Manager
Radicle
Git
Simplify job api using flags
Baptiste Daroussin committed 13 years ago
commit 172a7f4bdd244c7414edae1130508f3ded9be29b
parent 02c7935
9 files changed +92 -75
modified libpkg/pkg.h
@@ -247,6 +247,15 @@ typedef enum _pkg_jobs_t {
	PKG_JOBS_UPGRADE,
} pkg_jobs_t;

+
typedef enum _pkg_flags {
+
	PKG_FLAG_NONE = 0,
+
	PKG_FLAG_DRY_RUN = (1U << 0),
+
	PKG_FLAG_FORCE = (1U << 1),
+
	PKG_FLAG_RECURSIVE = (1U << 2),
+
	PKG_FLAG_AUTOMATIC = (1U << 3),
+
	PKG_FLAG_WITH_DEPS = (1U << 4),
+
} pkg_flags;
+

typedef enum _pkg_config_key {
	PKG_CONFIG_REPO = 0,
	PKG_CONFIG_DBDIR,
@@ -854,8 +863,7 @@ int pkg_add(struct pkgdb *db, const char *path, unsigned flags);
 * @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,
-
		 bool force, bool dry_run);
+
int pkg_jobs_new(struct pkg_jobs **jobs, pkg_jobs_t type, struct pkgdb *db);

/**
 * Free a pkg_jobs
@@ -866,10 +874,11 @@ void pkg_jobs_free(struct pkg_jobs *jobs);
 * Add a pkg to the jobs queue.
 * @return An error code.
 */
-
int pkg_jobs_add(struct pkg_jobs *j, match_t match, char **argv, int argc, bool recursive);
+
int pkg_jobs_add(struct pkg_jobs *j, match_t match, char **argv, int argc);
int pkg_jobs_solve(struct pkg_jobs *j);
int pkg_jobs_find(struct pkg_jobs *j, const char *origin, struct pkg **pkg);
int pkg_jobs_set_repository(struct pkg_jobs *j, const char *name);
+
void pkg_jobs_set_flags(struct pkg_jobs *j, pkg_flags f);
pkg_jobs_t pkg_jobs_type(struct pkg_jobs *j);

/**
modified libpkg/pkg_jobs.c
@@ -48,15 +48,11 @@
static int pkg_jobs_fetch(struct pkg_jobs *j);

int
-
pkg_jobs_new(struct pkg_jobs **j, pkg_jobs_t t, struct pkgdb *db, bool force,
-
	     bool dry_run)
+
pkg_jobs_new(struct pkg_jobs **j, pkg_jobs_t t, struct pkgdb *db)
{
	assert(db != NULL);
	assert(t != PKG_JOBS_INSTALL || db->type == PKGDB_REMOTE);

-
	if (!dry_run && pkgdb_lock(db) != EPKG_OK)
-
		return (EPKG_FATAL);
-

	if ((*j = calloc(1, sizeof(struct pkg_jobs))) == NULL) {
		pkg_emit_errno("calloc", "pkg_jobs");
		return (EPKG_FATAL);
@@ -65,15 +61,18 @@ pkg_jobs_new(struct pkg_jobs **j, pkg_jobs_t t, struct pkgdb *db, bool force,
	(*j)->db = db;
	(*j)->type = t;
	(*j)->solved = false;
+
	(*j)->flags = PKG_FLAG_NONE;
	STAILQ_INIT(&(*j)->patterns);
-
	if (dry_run)
-
		(*j)->flags |= PKG_JOB_FLAGS_DRY_RUN;
-
	if (force)
-
		(*j)->flags |= PKG_JOB_FLAGS_FORCE;

	return (EPKG_OK);
}

+
void
+
pkg_jobs_set_flags(struct pkg_jobs *j, pkg_flags flags)
+
{
+
	j->flags = flags;
+
}
+

int
pkg_jobs_set_repository(struct pkg_jobs *j, const char *name)
{
@@ -91,7 +90,7 @@ pkg_jobs_free(struct pkg_jobs *j)
	if (j == NULL)
		return;

-
	if ((j->flags & PKG_JOB_FLAGS_DRY_RUN) == 0)
+
	if ((j->flags & PKG_FLAG_DRY_RUN) == 0)
		pkgdb_unlock(j->db);

	HASH_FREE(j->jobs, pkg, pkg_free);
@@ -105,8 +104,7 @@ pkg_jobs_free(struct pkg_jobs *j)
}

int
-
pkg_jobs_add(struct pkg_jobs *j, match_t match, char **argv, int argc,
-
    bool recursive)
+
pkg_jobs_add(struct pkg_jobs *j, match_t match, char **argv, int argc)
{
	struct job_pattern *jp;

@@ -120,7 +118,6 @@ pkg_jobs_add(struct pkg_jobs *j, match_t match, char **argv, int argc,
	jp->pattern = argv;
	jp->nb = argc;
	jp->match = match;
-
	jp->recursive = recursive;
	STAILQ_INSERT_TAIL(&j->patterns, jp, next);

	return (EPKG_OK);
@@ -133,10 +130,14 @@ jobs_solve_deinstall(struct pkg_jobs *j)
	struct pkg *pkg = NULL;
	struct pkgdb_it *it;
	char *origin;
+
	bool recursive = false;
+

+
	if ((j->flags & PKG_FLAG_RECURSIVE) == PKG_FLAG_RECURSIVE)
+
		recursive = true;

	STAILQ_FOREACH(jp, &j->patterns, next) {
		if ((it = pkgdb_query_delete(j->db, jp->match, jp->nb,
-
		    jp->pattern, jp->recursive)) == NULL)
+
		    jp->pattern, recursive)) == NULL)
			return (EPKG_FATAL);

		while (pkgdb_it_next(it, &pkg, PKG_LOAD_BASIC) == EPKG_OK) {
@@ -180,7 +181,7 @@ jobs_solve_upgrade(struct pkg_jobs *j)
	char *origin;
	bool all = false;

-
	if ((j->flags & PKG_JOB_FLAGS_FORCE) != 0)
+
	if ((j->flags & PKG_FLAG_FORCE) != 0)
		all = true;

	if ((it = pkgdb_query_upgrades(j->db, j->reponame, all)) == NULL)
@@ -205,17 +206,24 @@ jobs_solve_install(struct pkg_jobs *j)
	struct pkgdb_it *it;
	char *origin;
	bool force = false;
+
	bool recursive = false;

-
	if ((j->flags & PKG_JOB_FLAGS_FORCE) != 0)
+

+
	if ((j->flags & PKG_FLAG_FORCE) == PKG_FLAG_FORCE)
		force = true;

+
	if ((j->flags & PKG_FLAG_RECURSIVE) == PKG_FLAG_RECURSIVE)
+
		recursive = true;
+

	STAILQ_FOREACH(jp, &j->patterns, next) {
		if ((it = pkgdb_query_installs(j->db, jp->match, jp->nb,
-
		    jp->pattern, j->reponame, force, jp->recursive)) == NULL)
+
		    jp->pattern, j->reponame, force, recursive)) == NULL)
			return (EPKG_FATAL);

		while (pkgdb_it_next(it, &pkg, PKG_LOAD_BASIC|PKG_LOAD_DEPS) == EPKG_OK) {
			pkg_get(pkg, PKG_ORIGIN, &origin);
+
			if ((j->flags & PKG_FLAG_AUTOMATIC) == PKG_FLAG_AUTOMATIC)
+
				pkg_set(pkg, PKG_AUTOMATIC, true);
			HASH_ADD_KEYPTR(hh, j->jobs, origin, strlen(origin), pkg);
			pkg = NULL;
		}
@@ -235,7 +243,7 @@ jobs_solve_fetch(struct pkg_jobs *j)
	char *origin;
	unsigned flag = PKG_LOAD_BASIC;

-
	if ((j->flags & PKG_JOB_FLAGS_FORCE) != 0)
+
	if ((j->flags & PKG_FLAG_WITH_DEPS) != 0)
		flag |= PKG_LOAD_DEPS;

	STAILQ_FOREACH(jp, &j->patterns, next) {
@@ -258,6 +266,15 @@ jobs_solve_fetch(struct pkg_jobs *j)
int
pkg_jobs_solve(struct pkg_jobs *j)
{
+
	bool dry_run = false;
+

+
	if ((j->flags & PKG_FLAG_DRY_RUN) != PKG_FLAG_DRY_RUN)
+
		dry_run = true;
+

+
	if (!dry_run && pkgdb_lock(j->db) != EPKG_OK)
+
		return (EPKG_FATAL);
+

+

	switch (j->type) {
	case PKG_JOBS_AUTOREMOVE:
		return (jobs_solve_autoremove(j));
@@ -460,7 +477,7 @@ pkg_jobs_install(struct pkg_jobs *j)
			}
		}

-
		if ((j->flags & PKG_JOB_FLAGS_FORCE) != 0)
+
		if ((j->flags & PKG_FLAG_FORCE) != 0)
			flags |= PKG_ADD_FORCE;
		flags |= PKG_ADD_UPGRADE;
		if (automatic)
@@ -498,10 +515,10 @@ pkg_jobs_deinstall(struct pkg_jobs *j)
	int retcode;
	int flags = 0;

-
	if ((j->flags & PKG_JOB_FLAGS_DRY_RUN) != 0)
+
	if ((j->flags & PKG_FLAG_DRY_RUN) != 0)
		return (EPKG_OK); /* Do nothing */

-
	if ((j->flags & PKG_JOB_FLAGS_FORCE) != 0)
+
	if ((j->flags & PKG_FLAG_FORCE) != 0)
		flags = PKG_DELETE_FORCE;

	while (pkg_jobs(j, &p) == EPKG_OK) {
@@ -606,7 +623,7 @@ pkg_jobs_fetch(struct pkg_jobs *j)
		return (EPKG_FATAL);
	}

-
	if ((j->flags & PKG_JOB_FLAGS_DRY_RUN) != 0)
+
	if ((j->flags & PKG_FLAG_DRY_RUN) != 0)
		return (EPKG_OK); /* don't download anything */

	/* Fetch */
modified libpkg/private/pkg.h
@@ -154,7 +154,7 @@ struct pkg_jobs {
	struct pkg	*jobs;
	struct pkgdb	*db;
	pkg_jobs_t	 type;
-
	unsigned	 flags;
+
	pkg_flags	 flags;
	bool		 solved;
	const char *	 reponame;
	STAILQ_HEAD(,job_pattern) patterns;
@@ -164,15 +164,9 @@ struct job_pattern {
	char		**pattern;
	int		nb;
	match_t		match;
-
	bool		recursive;
	STAILQ_ENTRY(job_pattern) next;
};

-
typedef enum _pkg_job_flags {
-
	PKG_JOB_FLAGS_FORCE =	(1 << 0 ),
-
	PKG_JOB_FLAGS_DRY_RUN =	(1 << 1),
-
} pkg_job_flags;
-

struct pkg_jobs_node {
	struct pkg	*pkg;
	size_t		 nrefs;
modified pkg/autoremove.c
@@ -52,6 +52,7 @@ exec_autoremove(int argc, char **argv)
	bool yes = false;
	bool dry_run = false;
	nbactions = nbdone = 0;
+
	pkg_flags f = PKG_FLAG_FORCE;

	pkg_config_bool(PKG_CONFIG_ASSUME_ALWAYS_YES, &yes);

@@ -64,6 +65,7 @@ exec_autoremove(int argc, char **argv)
			yes = true;
			break;
		case 'n':
+
			f |= PKG_FLAG_DRY_RUN;
			dry_run = true;
			break;
		default:
@@ -73,7 +75,6 @@ exec_autoremove(int argc, char **argv)
	argc -= optind;
	argv += optind;

-
	(void) argv;
	if (argc != 0) {
		usage_autoremove();
		return (EX_USAGE);
@@ -89,12 +90,13 @@ exec_autoremove(int argc, char **argv)
	}

	/* Always force packages to be removed */
-
	if (pkg_jobs_new(&jobs, PKG_JOBS_AUTOREMOVE, db, true, dry_run)
-
	    != EPKG_OK) {
+
	if (pkg_jobs_new(&jobs, PKG_JOBS_AUTOREMOVE, db) != EPKG_OK) {
		pkgdb_close(db);
		return (EX_IOERR);
	}

+
	pkg_jobs_set_flags(jobs, f);
+

	if ((retcode = pkg_jobs_solve(jobs)) != EPKG_OK)
		goto cleanup;

modified pkg/check.c
@@ -126,11 +126,11 @@ deps_free(struct deps_head *dh)
static int
fix_deps(struct pkgdb *db, struct deps_head *dh, int nbpkgs, bool yes)
{
-
	struct pkg *pkg = NULL;
	struct pkg_jobs *jobs = NULL;
	struct deps_entry *e = NULL;
	char **pkgs = NULL;
	int i = 0;
+
	pkg_flags f = PKG_FLAG_AUTOMATIC;

	assert(db != NULL);
	assert(nbpkgs > 0);
@@ -146,12 +146,14 @@ fix_deps(struct pkgdb *db, struct deps_head *dh, int nbpkgs, bool yes)
		return (EPKG_ENODB);
	}

-
	if (pkg_jobs_new(&jobs, PKG_JOBS_INSTALL, db, false, false) != EPKG_OK) {
+
	if (pkg_jobs_new(&jobs, PKG_JOBS_INSTALL, db) != EPKG_OK) {
		free(pkgs);
		return (EPKG_FATAL);
	}

-
	if (pkg_jobs_add(jobs, MATCH_EXACT, pkgs, nbpkgs, false) == EPKG_FATAL) {
+
	pkg_jobs_set_flags(jobs, f);
+

+
	if (pkg_jobs_add(jobs, MATCH_EXACT, pkgs, nbpkgs) == EPKG_FATAL) {
		pkg_jobs_free(jobs);
		return (EPKG_FATAL);
	}
@@ -167,13 +169,7 @@ fix_deps(struct pkgdb *db, struct deps_head *dh, int nbpkgs, bool yes)
		return (EPKG_FATAL);
	}

-
	while (pkg_jobs(jobs, &pkg) == EPKG_OK)
-
		pkg_set(pkg, PKG_AUTOMATIC, true);
-

-

	/* print a summary before applying the jobs */
-
	pkg = NULL;
-

	print_jobs_summary(jobs, "The following packages will be installed:\n\n");
	
	if (yes == false)
@@ -183,7 +179,6 @@ fix_deps(struct pkgdb *db, struct deps_head *dh, int nbpkgs, bool yes)
		pkg_jobs_apply(jobs);

	free(pkgs);
-
	pkg_free(pkg);
	pkg_jobs_free(jobs);

	return (EPKG_OK);
modified pkg/delete.c
@@ -49,7 +49,6 @@ int
exec_delete(int argc, char **argv)
{
	struct pkg_jobs *jobs = NULL;
-
	struct pkg *pkg = NULL;
	struct pkgdb *db = NULL;
	match_t match = MATCH_EXACT;
	int ch;
@@ -57,8 +56,8 @@ exec_delete(int argc, char **argv)
	bool yes;
	bool dry_run = false;
	int retcode = EX_SOFTWARE;
-
	int recursive = 0;
	nbactions = nbdone = 0;
+
	pkg_flags f = PKG_FLAG_NONE;

	pkg_config_bool(PKG_CONFIG_ASSUME_ALWAYS_YES, &yes);

@@ -68,19 +67,21 @@ exec_delete(int argc, char **argv)
			match = MATCH_ALL;
			break;
		case 'f':
+
			f |= PKG_FLAG_FORCE;
			force = true;
			break;
		case 'g':
			match = MATCH_GLOB;
			break;
		case 'n':
+
			f |= PKG_FLAG_DRY_RUN;
			dry_run = true;
			break;
		case 'q':
			quiet = true;
			break;
		case 'R':
-
			recursive = 1;
+
			f |= PKG_FLAG_RECURSIVE;
			break;
		case 'x':
			match = MATCH_REGEX;
@@ -111,13 +112,14 @@ exec_delete(int argc, char **argv)
		return (EPKG_FATAL);
	}

-
	if (pkg_jobs_new(&jobs, PKG_JOBS_DEINSTALL, db, force, dry_run)
-
	    != EPKG_OK) {
+
	if (pkg_jobs_new(&jobs, PKG_JOBS_DEINSTALL, db) != EPKG_OK) {
		pkgdb_close(db);
		return (EX_IOERR);
	}

-
	if (pkg_jobs_add(jobs, match, argv, argc, recursive) == EPKG_FATAL)
+
	pkg_jobs_set_flags(jobs, f);
+

+
	if (pkg_jobs_add(jobs, match, argv, argc) == EPKG_FATAL)
		goto cleanup;

	if (pkg_jobs_solve(jobs) != EPKG_OK)
@@ -143,7 +145,6 @@ exec_delete(int argc, char **argv)
		goto cleanup;
	}

-
	pkg = NULL;
	if (!quiet || dry_run) {
		print_jobs_summary(jobs,
		    "Deinstallation has been requested for the following %d packages:\n\n", nbactions);
modified pkg/fetch.c
@@ -58,6 +58,7 @@ exec_fetch(int argc, char **argv)
	bool yes = false;
	bool auto_update = true;
	match_t match = MATCH_EXACT;
+
	pkg_flags f = PKG_FLAG_NONE;

	while ((ch = getopt(argc, argv, "ygxr:qaLd")) != -1) {
		switch (ch) {
@@ -83,6 +84,7 @@ exec_fetch(int argc, char **argv)
			auto_update = false;
			break;
		case 'd':
+
			f |= PKG_FLAG_WITH_DEPS;
			force = true;
			break;
		default:
@@ -111,10 +113,12 @@ exec_fetch(int argc, char **argv)
	if (pkgdb_open(&db, PKGDB_REMOTE) != EPKG_OK)
		return (EX_IOERR);

-
	if (pkg_jobs_new(&jobs, PKG_JOBS_FETCH, db, force, false) != EPKG_OK)
+
	if (pkg_jobs_new(&jobs, PKG_JOBS_FETCH, db) != EPKG_OK)
		goto cleanup;

-
	if (pkg_jobs_add(jobs, match, argv, argc, false) != EPKG_OK)
+
	pkg_jobs_set_flags(jobs, f);
+

+
	if (pkg_jobs_add(jobs, match, argv, argc) != EPKG_OK)
		goto cleanup;

	if (pkg_jobs_solve(jobs) != EPKG_OK)
modified pkg/install.c
@@ -52,7 +52,6 @@ usage_install(void)
int
exec_install(int argc, char **argv)
{
-
	struct pkg *pkg = NULL;
	struct pkgdb *db = NULL;
	struct pkg_jobs *jobs = NULL;
	const char *reponame = NULL;
@@ -61,23 +60,20 @@ exec_install(int argc, char **argv)
	int ch;
	bool yes;
	bool auto_update = true;
-
	bool recursive = false;
-
	bool automatic = false;
-

	match_t match = MATCH_EXACT;
-
	bool force = false;
	bool dry_run = false;
	nbactions = nbdone = 0;
+
	pkg_flags f = PKG_FLAG_NONE;

	pkg_config_bool(PKG_CONFIG_ASSUME_ALWAYS_YES, &yes);

	while ((ch = getopt(argc, argv, "AfgLnqRr:xy")) != -1) {
		switch (ch) {
		case 'A':
-
			automatic = true;
+
			f |= PKG_FLAG_AUTOMATIC;
			break;
		case 'f':
-
			force = true;
+
			f |= PKG_FLAG_FORCE;
			break;
		case 'g':
			match = MATCH_GLOB;
@@ -86,13 +82,14 @@ exec_install(int argc, char **argv)
			auto_update = false;
			break;
		case 'n':
+
			f |= PKG_FLAG_DRY_RUN;
			dry_run = true;
			break;
		case 'q':
			quiet = true;
			break;
		case 'R':
-
			recursive = true;
+
			f |= PKG_FLAG_RECURSIVE;
			break;
		case 'r':
			reponame = optarg;
@@ -129,12 +126,13 @@ exec_install(int argc, char **argv)
		return (EX_IOERR);
	}

-
	if (pkg_jobs_new(&jobs, PKG_JOBS_INSTALL, db, force, dry_run)
-
	    != EPKG_OK) {
+
	if (pkg_jobs_new(&jobs, PKG_JOBS_INSTALL, db) != EPKG_OK) {
		goto cleanup;
	}

-
	if (pkg_jobs_add(jobs, match, argv, argc, false) == EPKG_FATAL)
+
	pkg_jobs_set_flags(jobs, f);
+

+
	if (pkg_jobs_add(jobs, match, argv, argc) == EPKG_FATAL)
		goto cleanup;

	if (pkg_jobs_solve(jobs) != EPKG_OK)
@@ -143,11 +141,6 @@ exec_install(int argc, char **argv)
	if ((nbactions = pkg_jobs_count(jobs)) == 0)
		goto cleanup;

-
	if (automatic) {
-
		while (pkg_jobs(jobs, &pkg) == EPKG_OK)
-
			pkg_set(pkg, PKG_AUTOMATIC, true);
-
	}
-

	/* print a summary before applying the jobs */
	if (!quiet || dry_run) {
		print_jobs_summary(jobs,
modified pkg/upgrade.c
@@ -51,22 +51,23 @@ exec_upgrade(int argc, char **argv)
	int updcode;
	int ch;
	bool yes;
-
	bool all = false;
	bool dry_run = false;
	bool auto_update = true;
	nbactions = nbdone = 0;
+
	pkg_flags f = PKG_FLAG_NONE;

	pkg_config_bool(PKG_CONFIG_ASSUME_ALWAYS_YES, &yes);

	while ((ch = getopt(argc, argv, "fLnqr:y")) != -1) {
		switch (ch) {
		case 'f':
-
			all = true;
+
			f |= PKG_FLAG_FORCE;
			break;
		case 'L':
			auto_update = false;
			break;
		case 'n':
+
			f |= PKG_FLAG_DRY_RUN;
			dry_run = true;
			break;
		case 'q':
@@ -105,10 +106,11 @@ exec_upgrade(int argc, char **argv)
	if (pkgdb_open(&db, PKGDB_REMOTE) != EPKG_OK)
		return (EX_IOERR);

-
	if (pkg_jobs_new(&jobs, PKG_JOBS_UPGRADE, db, all, dry_run)
-
	    != EPKG_OK)
+
	if (pkg_jobs_new(&jobs, PKG_JOBS_UPGRADE, db) != EPKG_OK)
		goto cleanup;

+
	pkg_jobs_set_flags(jobs, f);
+

	if (pkg_jobs_solve(jobs) != EPKG_OK)
		goto cleanup;

@@ -120,7 +122,7 @@ exec_upgrade(int argc, char **argv)
	}

	if (!quiet || dry_run) {
-
		print_jobs_summary(jobs, PKG_JOBS_INSTALL,
+
		print_jobs_summary(jobs,
		    "Uprgades have been requested for the following %d "
		    "packages:\n\n", nbactions);