Radish alpha
H
rad:z3QDZAW2FAfuLvihrhiyDC9fAD8G9
HardenedBSD Package Manager
Radicle
Git
First step at hidding the pkgdb_query_* inside the library
Baptiste Daroussin committed 13 years ago
commit 7502f271ed8759a32bde1c18492c1bcbae804990
parent 0813460
4 files changed +49 -17
modified libpkg/pkg.h
@@ -792,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_downgrades(struct pkgdb *db, const char *reponame);
-
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_fetch(struct pkgdb *db, match_t type, int nbpkgs, char **pkgs, const char *reponame, unsigned flags);

@@ -872,6 +871,9 @@ 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_find(struct pkg_jobs *j, const char *origin, struct pkg **pkg);
+

/**
 * Returns the number of elements in the job queue
 */
modified libpkg/pkg_jobs.c
@@ -87,6 +87,47 @@ pkg_jobs_free(struct pkg_jobs *j)
}

int
+
pkg_jobs_append(struct pkg_jobs *j, match_t match, char **argv, int argc, bool recursive)
+
{
+
	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)
+
			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);
+
		break;
+
	default:
+
		return (EPKG_FATAL);
+
	}
+
	return (EPKG_OK);
+
}
+

+
int
+
pkg_jobs_find(struct pkg_jobs *j, const char *origin, struct pkg **p)
+
{
+
	struct pkg *pkg;
+

+
	HASH_FIND_STR(j->jobs, __DECONST(char *, origin), pkg);
+
	if (pkg == NULL)
+
		return (EPKG_FATAL);
+

+
	if (p != NULL)
+
		*p = pkg;
+

+
	return (EPKG_OK);
+
}
+

+
/* deprecated should die in the end */
+
int
pkg_jobs_add(struct pkg_jobs *j, struct pkg *pkg)
{
	char *origin;
modified libpkg/private/pkgdb.h
@@ -56,6 +56,8 @@ int pkgdb_transaction_begin(sqlite3 *sqlite, const char *savepoint);
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);
+

int pkgdb_lock(struct pkgdb *db);
int pkgdb_unlock(struct pkgdb *db);

modified pkg/delete.c
@@ -51,17 +51,13 @@ exec_delete(int argc, char **argv)
	struct pkg_jobs *jobs = NULL;
	struct pkg *pkg = NULL;
	struct pkgdb *db = NULL;
-
	struct pkgdb_it *it = NULL;
	match_t match = MATCH_EXACT;
	int ch;
-
	int flags = PKG_LOAD_BASIC;
	bool force = false;
	bool yes;
	bool dry_run = false;
	int retcode = EX_SOFTWARE;
	int recursive = 0;
-
	bool haspkg = false;
-
	const char *origin;
	nbactions = nbdone = 0;

	pkg_config_bool(PKG_CONFIG_ASSUME_ALWAYS_YES, &yes);
@@ -121,19 +117,11 @@ exec_delete(int argc, char **argv)
		return (EX_IOERR);
	}

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

-
	while (pkgdb_it_next(it, &pkg, flags) == EPKG_OK) {
-
		pkg_get(pkg, PKG_ORIGIN, &origin);
-
		if (!force && !haspkg) {
-
			if (strcmp(origin, "ports-mgmt/pkg") == 0)
-
				haspkg = true;
-
		}
-
		pkg_jobs_add(jobs, pkg);
-
		pkg = NULL;
-
	}
-
	if (haspkg && !force) {
+
	if ((pkg_jobs_find(jobs, "ports-mgmt/pkg", NULL) == EPKG_OK)
+
	     && !force) {
		warnx("You are about to delete 'ports-mgmt/pkg' which is really "
		    "dangerous, you can't do that without specifying -f");
		goto cleanup;
@@ -174,7 +162,6 @@ exec_delete(int argc, char **argv)

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

	return (retcode);