Radish alpha
H
rad:z3QDZAW2FAfuLvihrhiyDC9fAD8G9
HardenedBSD Package Manager
Radicle
Git
Issue #327 -- rework patch to use a flags bitmap in struct pkg_jobs
Matthew Seaman committed 13 years ago
commit 104fb1ec122d9c40c56c9f6d816ef8918c06e18f
parent ac8d8a0
9 files changed +62 -36
modified libpkg/pkg.h
@@ -236,7 +236,7 @@ typedef enum {
typedef enum _pkg_jobs_t {
	PKG_JOBS_INSTALL,
	PKG_JOBS_DEINSTALL,
-
	PKG_JOBS_FETCH
+
	PKG_JOBS_FETCH,
} pkg_jobs_t;

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

/**
 * Free a pkg_jobs
@@ -828,7 +829,7 @@ int pkg_jobs(struct pkg_jobs *jobs, struct pkg **pkg);
 * Apply the jobs in the queue (fetch and install).
 * @return An error code.
 */
-
int pkg_jobs_apply(struct pkg_jobs *jobs, int force);
+
int pkg_jobs_apply(struct pkg_jobs *jobs);

/**
 * Archive formats options.
modified libpkg/pkg_jobs.c
@@ -44,12 +44,13 @@
static int pkg_jobs_fetch(struct pkg_jobs *j);

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

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

	if ((*j = calloc(1, sizeof(struct pkg_jobs))) == NULL) {
@@ -60,6 +61,10 @@ pkg_jobs_new(struct pkg_jobs **j, pkg_jobs_t t, struct pkgdb *db)
	STAILQ_INIT(&(*j)->jobs);
	(*j)->db = db;
	(*j)->type = t;
+
	if (dry_run)
+
		(*j)->flags |= PKG_JOB_FLAGS_DRY_RUN;
+
	if (force)
+
		(*j)->flags |= PKG_JOB_FLAGS_FORCE;

	return (EPKG_OK);
}
@@ -72,7 +77,8 @@ pkg_jobs_free(struct pkg_jobs *j)
	if (j == NULL)
		return;

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

	while (!STAILQ_EMPTY(&j->jobs)) {
		p = STAILQ_FIRST(&j->jobs);
@@ -152,7 +158,7 @@ pkg_jobs_keep_files_to_del(struct pkg *p1, struct pkg *p2)
}

static int
-
pkg_jobs_install(struct pkg_jobs *j, bool force)
+
pkg_jobs_install(struct pkg_jobs *j)
{
	struct pkg *p = NULL;
	struct pkg *pkg = NULL;
@@ -257,7 +263,7 @@ pkg_jobs_install(struct pkg_jobs *j, bool force)
			}
		}

-
		if (force)
+
		if ((j->flags & PKG_JOB_FLAGS_FORCE) != 0)
			flags |= PKG_ADD_FORCE;
		flags |= PKG_ADD_UPGRADE;
		if (automatic)
@@ -289,16 +295,21 @@ pkg_jobs_install(struct pkg_jobs *j, bool force)
}

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

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

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

	while (pkg_jobs(j, &p) == EPKG_OK) {
-
		if (force)
-
			retcode = pkg_delete(p, j->db, PKG_DELETE_FORCE);
-
		else
-
			retcode = pkg_delete(p, j->db, 0);
+
		retcode = pkg_delete(p, j->db, flags);
+

		if (retcode != EPKG_OK)
			return (retcode);
	}
@@ -307,16 +318,16 @@ 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 *j)
{
	int rc;

	switch (j->type) {
	case PKG_JOBS_INSTALL:
-
		rc = pkg_jobs_install(j, force);
+
		rc = pkg_jobs_install(j);
		break;
	case PKG_JOBS_DEINSTALL:
-
		rc = pkg_jobs_deinstall(j, force);
+
		rc = pkg_jobs_deinstall(j);
		break;
	case PKG_JOBS_FETCH:
		rc = pkg_jobs_fetch(j);
@@ -377,7 +388,10 @@ pkg_jobs_fetch(struct pkg_jobs *j)
		    cachedir, dlsz, fsz);
		return (EPKG_FATAL);
	}
-
		
+

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

	/* Fetch */
	p = NULL;
	while (pkg_jobs(j, &p) == EPKG_OK) {
modified libpkg/private/pkg.h
@@ -127,8 +127,14 @@ struct pkg_jobs {
	STAILQ_HEAD(jobs, pkg) jobs;
	struct pkgdb *db;
	pkg_jobs_t type;
+
	unsigned flags;
};

+
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
@@ -86,7 +86,9 @@ exec_autoremove(int argc, char **argv)
		return (EX_IOERR);
	}

-
	if (pkg_jobs_new(&jobs, PKG_JOBS_DEINSTALL, db) != EPKG_OK) {
+
	/* Always force packages to be removed */
+
	if (pkg_jobs_new(&jobs, PKG_JOBS_DEINSTALL, db, true, false)
+
	    != EPKG_OK) {
		pkgdb_close(db);
		return (EX_IOERR);
	}
@@ -138,7 +140,7 @@ exec_autoremove(int argc, char **argv)
	}

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

modified pkg/check.c
@@ -145,7 +145,7 @@ fix_deps(struct pkgdb *db, struct deps_head *dh, int nbpkgs, bool yes)
	if (pkgdb_open(&db, PKGDB_REMOTE) != EPKG_OK)
		return (EPKG_ENODB);

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

	if ((it = pkgdb_query_installs(db, MATCH_EXACT, nbpkgs, pkgs, NULL, false, false)) == NULL) {
@@ -174,7 +174,7 @@ fix_deps(struct pkgdb *db, struct deps_head *dh, int nbpkgs, bool yes)
		yes = query_yesno("\n>>> Try to fix the missing dependencies [y/N]: ");

	if (yes == true)
-
		pkg_jobs_apply(jobs, 0);
+
		pkg_jobs_apply(jobs);

	free(pkgs);
	pkg_free(pkg);
modified pkg/delete.c
@@ -55,7 +55,7 @@ exec_delete(int argc, char **argv)
	match_t match = MATCH_EXACT;
	int ch;
	int flags = PKG_LOAD_BASIC;
-
	int force = 0;
+
	bool force = false;
	bool yes = false;
	bool dry_run = false;
	int retcode = EX_SOFTWARE;
@@ -69,7 +69,7 @@ exec_delete(int argc, char **argv)
			match = MATCH_ALL;
			break;
		case 'f':
-
			force = 1;
+
			force = true;
			break;
		case 'g':
			match = MATCH_GLOB;
@@ -115,7 +115,8 @@ exec_delete(int argc, char **argv)
		return (EPKG_FATAL);
	}

-
	if (pkg_jobs_new(&jobs, PKG_JOBS_DEINSTALL, db) != EPKG_OK) {
+
	if (pkg_jobs_new(&jobs, PKG_JOBS_DEINSTALL, db, force, dry_run)
+
	    != EPKG_OK) {
		pkgdb_close(db);
		return (EX_IOERR);
	}
@@ -165,7 +166,7 @@ exec_delete(int argc, char **argv)
			yes = false;
	}
	if (yes) {
-
		if ((retcode = pkg_jobs_apply(jobs, force)) != EPKG_OK)
+
		if ((retcode = pkg_jobs_apply(jobs)) != EPKG_OK)
			goto cleanup;
	} else
		goto cleanup;
modified pkg/fetch.c
@@ -117,7 +117,7 @@ exec_fetch(int argc, char **argv)
		return (EX_IOERR);
	}

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

@@ -144,7 +144,7 @@ exec_fetch(int argc, char **argv)
	}
	
	if (yes)
-
		if (pkg_jobs_apply(jobs, 0) != EPKG_OK)
+
		if (pkg_jobs_apply(jobs) != EPKG_OK)
			goto cleanup;

	retcode = EX_OK;
modified pkg/install.c
@@ -129,7 +129,8 @@ exec_install(int argc, char **argv)
		return (EX_IOERR);
	}

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

@@ -165,7 +166,7 @@ exec_install(int argc, char **argv)
	}

	if (yes)
-
		if (pkg_jobs_apply(jobs, force) != EPKG_OK)
+
		if (pkg_jobs_apply(jobs) != EPKG_OK)
			goto cleanup;

	if (messages != NULL) {
modified pkg/upgrade.c
@@ -56,11 +56,6 @@ exec_upgrade(int argc, char **argv)
	bool dry_run = false;
	bool auto_update = true;

-
	if (geteuid() != 0) {
-
		warnx("Upgrading can only be done as root");
-
		return (EX_NOPERM);
-
	}
-

	while ((ch = getopt(argc, argv, "fLnqr:y")) != -1) {
		switch (ch) {
		case 'f':
@@ -90,6 +85,11 @@ exec_upgrade(int argc, char **argv)
	argc -= optind;
	argv += optind;

+
	if ((!dry_run || auto_update) && geteuid() != 0) {
+
		warnx("Upgrading can only be done as root");
+
		return (EX_NOPERM);
+
	}
+

	if (argc != 0) {
		usage_upgrade();
		return (EX_USAGE);
@@ -103,7 +103,8 @@ exec_upgrade(int argc, char **argv)
		return (EX_IOERR);
	}

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

@@ -137,7 +138,7 @@ exec_upgrade(int argc, char **argv)
	}

	if (yes)
-
		if (pkg_jobs_apply(jobs, 0) != EPKG_OK)
+
		if (pkg_jobs_apply(jobs) != EPKG_OK)
			goto cleanup;

	if (messages != NULL) {