Radish alpha
H
rad:z3QDZAW2FAfuLvihrhiyDC9fAD8G9
HardenedBSD Package Manager
Radicle
Git
Last step in hiding pkhdb_query_* function behind pkg_jobs
Baptiste Daroussin committed 13 years ago
commit f79cb6941e709c87ef87ec1d8323ee8d9075235e
parent 1a32d79
6 files changed +95 -52
modified libpkg/pkg.h
@@ -789,12 +789,6 @@ struct pkgdb_it * pkgdb_search(struct pkgdb *db, const char *pattern,
    match_t type, pkgdb_field field, pkgdb_field sort, const char *reponame);

/**
-
 *
-
 */
-
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_fetch(struct pkgdb *db, match_t type, int nbpkgs, char **pkgs, const char *reponame, unsigned flags);
-

-
/**
 * @todo Return directly the struct pkg?
 */
struct pkgdb_it * pkgdb_query_which(struct pkgdb *db, const char *path);
modified libpkg/pkg_jobs.c
@@ -197,6 +197,64 @@ jobs_solve_upgrade(struct pkg_jobs *j)
	return (EPKG_OK);
}

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

+
	if ((j->flags & PKG_JOB_FLAGS_FORCE) != 0)
+
		force = 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)
+
			return (EPKG_FATAL);
+

+
		while (pkgdb_it_next(it, &pkg, PKG_LOAD_BASIC|PKG_LOAD_DEPS) == 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);
+
}
+

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

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

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

+
		while (pkgdb_it_next(it, &pkg, flag) == 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)
{
@@ -207,6 +265,10 @@ pkg_jobs_solve(struct pkg_jobs *j)
		return (jobs_solve_deinstall(j));
	case PKG_JOBS_UPGRADE:
		return (jobs_solve_upgrade(j));
+
	case PKG_JOBS_INSTALL:
+
		return (jobs_solve_install(j));
+
	case PKG_JOBS_FETCH:
+
		return (jobs_solve_fetch(j));
	default:
		return (EPKG_FATAL);
	}
modified libpkg/private/pkgdb.h
@@ -59,6 +59,8 @@ 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);
struct pkgdb_it *pkgdb_query_upgrades(struct pkgdb *db, const char *reponame, bool all);
+
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_fetch(struct pkgdb *db, match_t type, int nbpkgs, char **pkgs, const char *reponame, unsigned flags);

int pkgdb_lock(struct pkgdb *db);
int pkgdb_unlock(struct pkgdb *db);
modified pkg/check.c
@@ -127,7 +127,6 @@ static int
fix_deps(struct pkgdb *db, struct deps_head *dh, int nbpkgs, bool yes)
{
	struct pkg *pkg = NULL;
-
	struct pkgdb_it *it = NULL;
	struct pkg_jobs *jobs = NULL;
	struct deps_entry *e = NULL;
	char **pkgs = NULL;
@@ -152,29 +151,30 @@ fix_deps(struct pkgdb *db, struct deps_head *dh, int nbpkgs, bool yes)
		return (EPKG_FATAL);
	}

-
	if ((it = pkgdb_query_installs(db, MATCH_EXACT, nbpkgs, pkgs, NULL, false, false)) == NULL) {
-
		free(pkgs);
+
	if (pkg_jobs_append(jobs, MATCH_EXACT, pkgs, nbpkgs, false) == EPKG_FATAL) {
		pkg_jobs_free(jobs);
		return (EPKG_FATAL);
	}

-
	while (pkgdb_it_next(it, &pkg, PKG_LOAD_BASIC|PKG_LOAD_DEPS) == EPKG_OK) {
-
		pkg_set(pkg, PKG_AUTOMATIC, true);
-
		pkg_jobs_add(jobs, pkg);
-
		pkg = NULL;
+
	if (pkg_jobs_solve(jobs) != EPKG_OK) {
+
		pkg_jobs_free(jobs);
+
		return (EPKG_FATAL);
	}

	if (pkg_jobs_count(jobs) == 0) {
-
		printf("\n>>> Unable to find packages for installation.\n\n");
+
		printf("\nUnable to find packages for installation.\n\n");
		pkg_jobs_free(jobs);
-
		pkgdb_it_free(it);
		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, PKG_JOBS_INSTALL, "The following packages will be installed:\n\n");
+
	print_jobs_summary(jobs, "The following packages will be installed:\n\n");
	
	if (yes == false)
		yes = query_yesno("\n>>> Try to fix the missing dependencies [y/N]: ");
@@ -185,7 +185,6 @@ fix_deps(struct pkgdb *db, struct deps_head *dh, int nbpkgs, bool yes)
	free(pkgs);
	pkg_free(pkg);
	pkg_jobs_free(jobs);
-
	pkgdb_it_free(it);

	return (EPKG_OK);
}
modified pkg/fetch.c
@@ -49,14 +49,12 @@ usage_fetch(void)
int
exec_fetch(int argc, char **argv)
{
-
	struct pkg *pkg = NULL;
-
	struct pkgdb_it *it = NULL;
	struct pkgdb *db = NULL;
	struct pkg_jobs *jobs = NULL;
	const char *reponame = NULL;
	int retcode = EX_SOFTWARE;
	int ch;
-
	int flags = PKG_LOAD_BASIC;
+
	bool force = false;
	bool yes = false;
	bool auto_update = true;
	match_t match = MATCH_EXACT;
@@ -85,7 +83,7 @@ exec_fetch(int argc, char **argv)
			auto_update = false;
			break;
		case 'd':
-
			flags |= PKG_LOAD_DEPS;
+
			force = true;
			break;
		default:
			usage_fetch();
@@ -110,23 +108,17 @@ exec_fetch(int argc, char **argv)
	if (auto_update && (retcode = pkgcli_update(false)) != EPKG_OK)
		return (retcode);

-
	if (pkgdb_open(&db, PKGDB_REMOTE) != EPKG_OK) {
+
	if (pkgdb_open(&db, PKGDB_REMOTE) != EPKG_OK)
		return (EX_IOERR);
-
	}

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

-
	if ((it = pkgdb_query_fetch(db, match, argc, argv, reponame, flags)) == NULL)
+
	if (pkg_jobs_append(jobs, match, argv, argc, false) != EPKG_OK)
		goto cleanup;

-
	while (pkgdb_it_next(it, &pkg, flags) == EPKG_OK) {
-
		pkg_jobs_add(jobs, pkg);
-
		pkg = NULL;
-
	}
-
	
-
	pkgdb_it_free(it);
+
	if (pkg_jobs_solve(jobs) != EPKG_OK)
+
		goto cleanup;

	if (pkg_jobs_count(jobs) == 0)
		goto cleanup;
@@ -140,13 +132,12 @@ exec_fetch(int argc, char **argv)
			yes = query_yesno("\nProceed with fetching packages [y/N]: ");
	}
	
-
	if (yes)
-
		if (pkg_jobs_apply(jobs) != EPKG_OK)
-
			goto cleanup;
+
	if (!yes || pkg_jobs_apply(jobs) != EPKG_OK)
+
		goto cleanup;

	retcode = EX_OK;

-
	cleanup:
+
cleanup:
	pkg_jobs_free(jobs);
	pkgdb_close(db);

modified pkg/install.c
@@ -53,7 +53,6 @@ int
exec_install(int argc, char **argv)
{
	struct pkg *pkg = NULL;
-
	struct pkgdb_it *it = NULL;
	struct pkgdb *db = NULL;
	struct pkg_jobs *jobs = NULL;
	const char *reponame = NULL;
@@ -135,24 +134,21 @@ exec_install(int argc, char **argv)
		goto cleanup;
	}

-
	if ((it = pkgdb_query_installs(db, match, argc, argv, reponame,
-
	    force, recursive)) == NULL)
+
	if (pkg_jobs_append(jobs, match, argv, argc, false) == EPKG_FATAL)
		goto cleanup;

-
	while (pkgdb_it_next(it, &pkg, PKG_LOAD_BASIC|PKG_LOAD_DEPS) ==
-
	    EPKG_OK) {
-
		if (automatic)
-
			pkg_set(pkg, PKG_AUTOMATIC, true);
-
		pkg_jobs_add(jobs, pkg);
-
		pkg = NULL;
-
	}
-
	pkgdb_it_free(it);
+
	if (pkg_jobs_solve(jobs) != EPKG_OK)
+
		goto cleanup;

	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 */
-
	pkg = NULL;
	if (!quiet || dry_run) {
		print_jobs_summary(jobs,
		    "The following %d packages will be installed:\n\n",
@@ -165,9 +161,8 @@ exec_install(int argc, char **argv)
			yes = false;
	}

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

	if (messages != NULL) {
		sbuf_finish(messages);
@@ -176,7 +171,7 @@ exec_install(int argc, char **argv)

	retcode = EX_OK;

-
	cleanup:
+
cleanup:
	pkg_jobs_free(jobs);
	pkgdb_close(db);