Radish alpha
H
rad:z3QDZAW2FAfuLvihrhiyDC9fAD8G9
HardenedBSD Package Manager
Radicle
Git
Step 2 at hiding the pkgdb_query_* behind pkg_jobs
Baptiste Daroussin committed 13 years ago
commit 7e985a6189a2e9da65d1c7a0d4c536ffbf7a5926
parent df57985
7 files changed +107 -58
modified libpkg/pkg.h
@@ -243,6 +243,7 @@ typedef enum _pkg_jobs_t {
	PKG_JOBS_INSTALL,
	PKG_JOBS_DEINSTALL,
	PKG_JOBS_FETCH,
+
	PKG_JOBS_AUTOREMOVE,
} pkg_jobs_t;

typedef enum _pkg_config_key {
@@ -792,7 +793,6 @@ struct pkgdb_it * pkgdb_search(struct pkgdb *db, const char *pattern,
struct pkgdb_it *pkgdb_query_installs(struct pkgdb *db, match_t type, int nbpkgs, char **pkgs, const char *reponame, bool force, bool recursive);
struct pkgdb_it *pkgdb_query_upgrades(struct pkgdb *db, const char *reponame, bool all);
struct pkgdb_it *pkgdb_query_downgrades(struct pkgdb *db, const char *reponame);
-
struct pkgdb_it *pkgdb_query_autoremove(struct pkgdb *db);
struct pkgdb_it *pkgdb_query_fetch(struct pkgdb *db, match_t type, int nbpkgs, char **pkgs, const char *reponame, unsigned flags);

/**
@@ -872,6 +872,7 @@ void pkg_jobs_free(struct pkg_jobs *jobs);
int pkg_jobs_add(struct pkg_jobs *jobs, struct pkg *pkg);

int pkg_jobs_append(struct pkg_jobs *j, match_t match, char **argv, int argc, bool recursive);
+
int pkg_jobs_solve(struct pkg_jobs *j);
int pkg_jobs_find(struct pkg_jobs *j, const char *origin, struct pkg **pkg);

/**
modified libpkg/pkg_jobs.c
@@ -64,6 +64,8 @@ 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;
+
	STAILQ_INIT(&(*j)->patterns);
	if (dry_run)
		(*j)->flags |= PKG_JOB_FLAGS_DRY_RUN;
	if (force)
@@ -75,6 +77,8 @@ pkg_jobs_new(struct pkg_jobs **j, pkg_jobs_t t, struct pkgdb *db, bool force,
void
pkg_jobs_free(struct pkg_jobs *j)
{
+
	struct job_pattern *jp;
+

	if (j == NULL)
		return;

@@ -82,20 +86,48 @@ pkg_jobs_free(struct pkg_jobs *j)
		pkgdb_unlock(j->db);

	HASH_FREE(j->jobs, pkg, pkg_free);
+
	while (!STAILQ_EMPTY(&j->patterns)) {
+
		jp = STAILQ_FIRST(&j->patterns);
+
		STAILQ_REMOVE_HEAD(&j->patterns, next);
+
		free(jp);
+
	}

	free(j);
}

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

+
	if (!j->solved) {
+
		pkg_emit_error("The job has already been solved."
+
		    "Impossible to append now elements");
+
		return (EPKG_FATAL);
+
	}
+

+
	jp = malloc(sizeof(struct job_pattern));
+
	jp->pattern = argv;
+
	jp->nb = argc;
+
	jp->match = match;
+
	jp->recursive = recursive;
+
	STAILQ_INSERT_TAIL(&j->patterns, jp, next);
+

+
	return (EPKG_OK);
+
}
+

+
static int
+
jobs_solve_deinstall(struct pkg_jobs *j)
{
+
	struct job_pattern *jp = NULL;
	struct pkg *pkg = NULL;
	struct pkgdb_it *it;
	char *origin;

-
	switch (j->type) {
-
	case PKG_JOBS_DEINSTALL:
-
		if ((it = pkgdb_query_delete(j->db, match, argc, argv, recursive)) == NULL)
+
	STAILQ_FOREACH(jp, &j->patterns, next) {
+
		if ((it = pkgdb_query_delete(j->db, jp->match, jp->nb,
+
		    jp->pattern, jp->recursive)) == NULL)
			return (EPKG_FATAL);

		while (pkgdb_it_next(it, &pkg, PKG_LOAD_BASIC) == EPKG_OK) {
@@ -104,12 +136,44 @@ pkg_jobs_append(struct pkg_jobs *j, match_t match, char **argv, int argc, bool r
			pkg = NULL;
		}
		pkgdb_it_free(it);
-
		break;
-
	default:
+
	}
+
	j->solved = true;
+

+
	return( EPKG_OK);
+
}
+

+
static int
+
jobs_solve_autoremove(struct pkg_jobs *j)
+
{
+
	struct pkg *pkg = NULL;
+
	struct pkgdb_it *it;
+
	char *origin;
+

+
	if ((it = pkgdb_query_autoremove(j->db)) == NULL)
		return (EPKG_FATAL);
+

+
	while (pkgdb_it_next(it, &pkg, PKG_LOAD_BASIC) == EPKG_OK) {
+
		pkg_get(pkg, PKG_ORIGIN, &origin);
+
		HASH_ADD_KEYPTR(hh, j->jobs, origin, strlen(origin), pkg);
+
		pkg = NULL;
	}
+
	pkgdb_it_free(it);
+
	j->solved = true;
+

	return (EPKG_OK);
}
+
int
+
pkg_jobs_solve(struct pkg_jobs *j)
+
{
+
	switch (j->type) {
+
	case PKG_JOBS_AUTOREMOVE:
+
		return (jobs_solve_autoremove(j));
+
	case PKG_JOBS_DEINSTALL:
+
		return (jobs_solve_deinstall(j));
+
	default:
+
		return (EPKG_FATAL);
+
	}
+
}

int
pkg_jobs_find(struct pkg_jobs *j, const char *origin, struct pkg **p)
modified libpkg/private/pkg.h
@@ -155,6 +155,16 @@ struct pkg_jobs {
	struct pkgdb	*db;
	pkg_jobs_t	 type;
	unsigned	 flags;
+
	bool		 solved;
+
	STAILQ_HEAD(,job_pattern) patterns;
+
};
+

+
struct job_pattern {
+
	char		**pattern;
+
	int		nb;
+
	match_t		match;
+
	bool		recursive;
+
	STAILQ_ENTRY(job_pattern) next;
};

typedef enum _pkg_job_flags {
modified libpkg/private/pkgdb.h
@@ -57,6 +57,7 @@ int pkgdb_transaction_commit(sqlite3 *sqlite, const char *savepoint);
int pkgdb_transaction_rollback(sqlite3 *sqlite, const char *savepoint);

struct pkgdb_it *pkgdb_query_delete(struct pkgdb *db, match_t type, int nbpkgs, char **pkgs, int recursive);
+
struct pkgdb_it *pkgdb_query_autoremove(struct pkgdb *db);

int pkgdb_lock(struct pkgdb *db);
int pkgdb_unlock(struct pkgdb *db);
modified pkg/autoremove.c
@@ -46,18 +46,15 @@ int
exec_autoremove(int argc, char **argv)
{
	struct pkgdb *db = NULL;
-
	struct pkgdb_it *it;
-
	struct pkg *pkg = NULL;
	struct pkg_jobs *jobs = NULL;
-
	int retcode = EPKG_OK;
-
	int64_t oldsize = 0, newsize = 0;
-
	int64_t flatsize, newflatsize;
-
	char size[7];
+
	int retcode = EX_SOFTWARE;
	int ch;
	bool yes = false;
	bool dry_run = false;
	nbactions = nbdone = 0;

+
	pkg_config_bool(PKG_CONFIG_ASSUME_ALWAYS_YES, &yes);
+

	while ((ch = getopt(argc, argv, "ynq")) != -1) {
		switch (ch) {
		case 'q':
@@ -92,31 +89,14 @@ exec_autoremove(int argc, char **argv)
	}

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

-
	if ((it = pkgdb_query_autoremove(db)) == NULL) {
-
		retcode = EPKG_FATAL;
+
	if ((retcode = pkg_jobs_solve(jobs)) != EPKG_OK)
		goto cleanup;
-
	}
-

-
	while ((retcode = pkgdb_it_next(it, &pkg, PKG_LOAD_BASIC)) == EPKG_OK) {
-
		pkg_get(pkg, PKG_FLATSIZE, &flatsize, PKG_NEW_FLATSIZE, &newflatsize);
-
		oldsize += flatsize;
-
		newsize += newflatsize;
-
		pkg_jobs_add(jobs, pkg);
-
		pkg = NULL;
-
	}
-

-
	if (oldsize > newsize) {
-
		newsize *= -1;
-
		humanize_number(size, sizeof(size), oldsize - newsize, "B", HN_AUTOSCALE, 0);
-
	} else {
-
		humanize_number(size, sizeof(size), newsize - oldsize, "B", HN_AUTOSCALE, 0);
-
	}

	if ((nbactions = pkg_jobs_count(jobs)) == 0) {
		printf("Nothing to do.\n");
@@ -124,42 +104,28 @@ exec_autoremove(int argc, char **argv)
		goto cleanup;
	}

-
	pkg = NULL;
	if (!quiet || dry_run) {
-
		printf("%d packages to be autoremoved: \n", nbactions);
-
		while (pkg_jobs(jobs, &pkg) == EPKG_OK) {
-
			const char *name, *version;
-
			pkg_get(pkg, PKG_NAME, &name, PKG_VERSION, &version);
-
			printf("\t%s-%s\n", name, version);
-
		}
-

-
		if (oldsize > newsize)
-
			printf("\nThe autoremoval will free %s\n", size);
-
		else
-
			printf("\nThe autoremoval will require %s more space\n", size);
-

-
		if (!yes)
-
			pkg_config_bool(PKG_CONFIG_ASSUME_ALWAYS_YES, &yes);
+
		print_jobs_summary(jobs, PKG_JOBS_AUTOREMOVE,
+
		    "Deinstallation has been requested for the following %d packages:\n\n", nbactions);
		if (!yes && !dry_run)
-
			yes = query_yesno("\nProceed with autoremoval of packages [y/N]: ");
+
			yes = query_yesno(
+
		            "\nProceed with deinstalling packages [y/N]: ");
		if (dry_run)
			yes = false;
	}
-

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

-
	if (pkgdb_compact(db) != EPKG_OK) {
-
		retcode = EPKG_FATAL;
-
	}
+
	pkgdb_compact(db);
+

+
	retcode = EX_OK;

-
	cleanup:
-
	pkg_free(pkg);
+
cleanup:
	pkg_jobs_free(jobs);
-
	pkgdb_it_free(it);
	pkgdb_close(db);

-
	return ((retcode == EPKG_OK) ? EX_OK : EX_SOFTWARE);
+
	return (retcode);
}
modified pkg/delete.c
@@ -120,6 +120,9 @@ exec_delete(int argc, char **argv)
	if (pkg_jobs_append(jobs, match, argv, argc, recursive) == EPKG_FATAL)
		goto cleanup;

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

	if ((pkg_jobs_find(jobs, "ports-mgmt/pkg", NULL) == EPKG_OK)
	     && !force) {
		warnx("You are about to delete 'ports-mgmt/pkg' which is really "
modified pkg/utils.c
@@ -575,6 +575,7 @@ print_jobs_summary(struct pkg_jobs *jobs, pkg_jobs_t type, const char *msg, ...)
				} 
				break;
			case PKG_JOBS_DEINSTALL:
+
			case PKG_JOBS_AUTOREMOVE:
				printf("and may not be deinstalled\n");
				continue;
				break;
@@ -613,6 +614,7 @@ print_jobs_summary(struct pkg_jobs *jobs, pkg_jobs_t type, const char *msg, ...)
			}
			break;
		case PKG_JOBS_DEINSTALL:
+
		case PKG_JOBS_AUTOREMOVE:
			oldsize += flatsize;
			newsize += newflatsize;
			
@@ -637,6 +639,7 @@ print_jobs_summary(struct pkg_jobs *jobs, pkg_jobs_t type, const char *msg, ...)
			printf("\nThe installation will free %s\n", size);
			break;
		case PKG_JOBS_DEINSTALL:
+
		case PKG_JOBS_AUTOREMOVE:
			printf("\nThe deinstallation will free %s\n", size);
			break;
		case PKG_JOBS_FETCH:
@@ -651,6 +654,7 @@ print_jobs_summary(struct pkg_jobs *jobs, pkg_jobs_t type, const char *msg, ...)
			printf("\nThe installation will require %s more space\n", size);
			break;
		case PKG_JOBS_DEINSTALL:
+
		case PKG_JOBS_AUTOREMOVE:
			printf("\nThe deinstallation will require %s more space\n", size);
			break;
		case PKG_JOBS_FETCH: