Radish alpha
H
rad:z3QDZAW2FAfuLvihrhiyDC9fAD8G9
HardenedBSD Package Manager
Radicle
Git
Step 3 at hiding the pkgdb_query_* behind pkg_jobs
Baptiste Daroussin committed 13 years ago
commit f080efac8d7481cfa843759359904f30c73a577c
parent 7d49201
6 files changed +54 -19
modified libpkg/pkg.h
@@ -244,6 +244,7 @@ typedef enum _pkg_jobs_t {
	PKG_JOBS_DEINSTALL,
	PKG_JOBS_FETCH,
	PKG_JOBS_AUTOREMOVE,
+
	PKG_JOBS_UPGRADE,
} pkg_jobs_t;

typedef enum _pkg_config_key {
@@ -791,7 +792,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_fetch(struct pkgdb *db, match_t type, int nbpkgs, char **pkgs, const char *reponame, unsigned flags);

/**
@@ -873,6 +873,7 @@ 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);
+
int pkg_jobs_set_repository(struct pkg_jobs *j, const char *name);
pkg_jobs_t pkg_jobs_type(struct pkg_jobs *j);

/**
modified libpkg/pkg_jobs.c
@@ -74,6 +74,15 @@ pkg_jobs_new(struct pkg_jobs **j, pkg_jobs_t t, struct pkgdb *db, bool force,
	return (EPKG_OK);
}

+
int
+
pkg_jobs_set_repository(struct pkg_jobs *j, const char *name)
+
{
+
	/* TODO should validate if the repository exists */
+
	j->reponame = name;
+

+
	return (EPKG_OK);
+
}
+

void
pkg_jobs_free(struct pkg_jobs *j)
{
@@ -162,6 +171,32 @@ jobs_solve_autoremove(struct pkg_jobs *j)

	return (EPKG_OK);
}
+

+
static int
+
jobs_solve_upgrade(struct pkg_jobs *j)
+
{
+
	struct pkg *pkg = NULL;
+
	struct pkgdb_it *it;
+
	char *origin;
+
	bool all = false;
+

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

+
	if ((it = pkgdb_query_upgrades(j->db, j->reponame, all)) == 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)
{
@@ -170,6 +205,8 @@ pkg_jobs_solve(struct pkg_jobs *j)
		return (jobs_solve_autoremove(j));
	case PKG_JOBS_DEINSTALL:
		return (jobs_solve_deinstall(j));
+
	case PKG_JOBS_UPGRADE:
+
		return (jobs_solve_upgrade(j));
	default:
		return (EPKG_FATAL);
	}
modified libpkg/private/pkg.h
@@ -156,6 +156,7 @@ struct pkg_jobs {
	pkg_jobs_t	 type;
	unsigned	 flags;
	bool		 solved;
+
	const char *	 reponame;
	STAILQ_HEAD(,job_pattern) patterns;
};

modified libpkg/private/pkgdb.h
@@ -58,6 +58,7 @@ 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);

int pkgdb_lock(struct pkgdb *db);
int pkgdb_unlock(struct pkgdb *db);
modified pkg/upgrade.c
@@ -45,8 +45,6 @@ int
exec_upgrade(int argc, char **argv)
{
	struct pkgdb *db = NULL;
-
	struct pkgdb_it *it = NULL;
-
	struct pkg *pkg = NULL;
	struct pkg_jobs *jobs = NULL;
	const char *reponame = NULL;
	int retcode = 1;
@@ -104,25 +102,15 @@ exec_upgrade(int argc, char **argv)
	    (updcode = pkgcli_update(false)) != EPKG_OK)
		return (updcode);

-
	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_INSTALL, db, false, dry_run)
-
	    != EPKG_OK) {
+
	if (pkg_jobs_new(&jobs, PKG_JOBS_UPGRADE, db, all, dry_run)
+
	    != EPKG_OK)
		goto cleanup;
-
	}

-
	if ((it = pkgdb_query_upgrades(db, reponame, all)) == NULL) {
+
	if (pkg_jobs_solve(jobs) != EPKG_OK)
		goto cleanup;
-
	}
-

-
	while (pkgdb_it_next(it, &pkg, PKG_LOAD_BASIC|PKG_LOAD_DEPS) ==
-
	       EPKG_OK) {
-
		pkg_jobs_add(jobs, pkg);
-
		pkg = NULL;
-
	}
-
	pkgdb_it_free(it);

	if ((nbactions = pkg_jobs_count(jobs)) == 0) {
		if (!quiet)
@@ -131,7 +119,6 @@ exec_upgrade(int argc, char **argv)
		goto cleanup;
	}

-
	pkg = NULL;
	if (!quiet || dry_run) {
		print_jobs_summary(jobs, PKG_JOBS_INSTALL,
		    "Uprgades have been requested for the following %d "
modified pkg/utils.c
@@ -560,6 +560,7 @@ print_jobs_summary(struct pkg_jobs *jobs, const char *msg, ...)
			       name, version);
			switch (type) {
			case PKG_JOBS_INSTALL:
+
			case PKG_JOBS_UPGRADE:
				/* If it's a new install, then it
				 * cannot have been locked yet. */
				if (newversion != NULL) {
@@ -591,6 +592,7 @@ print_jobs_summary(struct pkg_jobs *jobs, const char *msg, ...)

		switch (type) {
		case PKG_JOBS_INSTALL:
+
		case PKG_JOBS_UPGRADE:
			snprintf(path, MAXPATHLEN, "%s/%s", cachedir, pkgrepopath);
			if (stat(path, &st) == -1 || pkgsize != st.st_size)
				/* file looks corrupted (wrong size), assume a checksum mismatch will
@@ -641,6 +643,9 @@ print_jobs_summary(struct pkg_jobs *jobs, const char *msg, ...)
		case PKG_JOBS_INSTALL:
			printf("\nThe installation will free %s\n", size);
			break;
+
		case PKG_JOBS_UPGRADE:
+
			printf("\nThe upgrade will free %s\n", size);
+
			break;
		case PKG_JOBS_DEINSTALL:
		case PKG_JOBS_AUTOREMOVE:
			printf("\nThe deinstallation will free %s\n", size);
@@ -656,6 +661,9 @@ print_jobs_summary(struct pkg_jobs *jobs, const char *msg, ...)
		case PKG_JOBS_INSTALL:
			printf("\nThe installation will require %s more space\n", size);
			break;
+
		case PKG_JOBS_UPGRADE:
+
			printf("\nThe upgrade 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);
@@ -666,7 +674,7 @@ print_jobs_summary(struct pkg_jobs *jobs, const char *msg, ...)
		}
	}

-
	if ((type == PKG_JOBS_INSTALL) || (type == PKG_JOBS_FETCH)) {
+
	if ((type == PKG_JOBS_INSTALL) || (type == PKG_JOBS_FETCH) || (type == PKG_JOBS_UPGRADE)) {
		humanize_number(size, sizeof(size), dlsize, "B", HN_AUTOSCALE, 0);
		printf("\n%s to be downloaded\n", size);
	}