Radish alpha
H
rad:z3QDZAW2FAfuLvihrhiyDC9fAD8G9
HardenedBSD Package Manager
Radicle
Git
pkgdb reopenable + better messages
Baptiste Daroussin committed 14 years ago
commit a52a32120373a09b4a5559861ca0d9c06e82117e
parent 5aebcc7
5 files changed +83 -71
modified libpkg/pkgdb.c
@@ -425,6 +425,7 @@ int
pkgdb_open(struct pkgdb **db_p, pkgdb_t type)
{
	struct pkgdb *db = NULL;
+
	bool reopen = false;
	char localpath[MAXPATHLEN + 1];
	char remotepath[MAXPATHLEN + 1];
	const char *dbdir = NULL;
@@ -433,77 +434,78 @@ pkgdb_open(struct pkgdb **db_p, pkgdb_t type)
	bool create = false;
	struct pkg_config_kv *repokv = NULL;

-
	/*
-
	 * Set the pointer to NULL now. Change it to the real pointer just
-
	 * before returning, when we know that we succeeded.
-
	 */
-
	*db_p = NULL;
+
	if (*db_p != NULL) {
+
		reopen = true;
+
		db = *db_p;
+
		if (db->type == type)
+
			return (EPKG_OK);
+
	}

	if (pkg_config_string(PKG_CONFIG_DBDIR, &dbdir) != EPKG_OK)
		return (EPKG_FATAL);

-
	if ((db = calloc(1, sizeof(struct pkgdb))) == NULL) {
+
	if (!reopen && (db = calloc(1, sizeof(struct pkgdb))) == NULL) {
		pkg_emit_errno("malloc", "pkgdb");
		return EPKG_FATAL;
	}

	db->type = type;

-
	snprintf(localpath, sizeof(localpath), "%s/local.sqlite", dbdir);
+
	if (!reopen) {
+
		snprintf(localpath, sizeof(localpath), "%s/local.sqlite", dbdir);

-
	if (eaccess(localpath, R_OK) != 0) {
-
		if (errno != ENOENT) {
-
			/*pkg_emit_errno("Unable to create local database ", localpath);*/
-
			pkgdb_close(db);
-
			return (EPKG_ENODB);
-
		} else if (eaccess(dbdir, W_OK) != 0) {
-
			/* If we need to create the db but can not write to it, fail early */
-
			/*pkg_emit_errno("Unable to create local database ", dbdir);*/
-
			pkgdb_close(db);
-
			return (EPKG_ENODB);
-
		} else {
-
			create = true;
+
		if (eaccess(localpath, R_OK) != 0) {
+
			if (errno != ENOENT) {
+
				pkgdb_close(db);
+
				return (EPKG_ENODB);
+
			} else if (eaccess(dbdir, W_OK) != 0) {
+
				/* If we need to create the db but can not write to it, fail early */
+
				pkgdb_close(db);
+
				return (EPKG_ENODB);
+
			} else {
+
				create = true;
+
			}
		}
-
	}
-
	
-
	sqlite3_initialize();
-
	if (sqlite3_open(localpath, &db->sqlite) != SQLITE_OK) {
-
		ERROR_SQLITE(db->sqlite);
-
		pkgdb_close(db);
-
		return (EPKG_FATAL);
-
	}

-
	/* If the database is missing we have to initialize it */
-
	if (create == true)
-
		if (pkgdb_init(db->sqlite) != EPKG_OK) {
+
		sqlite3_initialize();
+
		if (sqlite3_open(localpath, &db->sqlite) != SQLITE_OK) {
+
			ERROR_SQLITE(db->sqlite);
			pkgdb_close(db);
			return (EPKG_FATAL);
		}

-
	if (eaccess(localpath, W_OK) == 0)
-
		db->writable = 1;
+
		/* If the database is missing we have to initialize it */
+
		if (create == true)
+
			if (pkgdb_init(db->sqlite) != EPKG_OK) {
+
				pkgdb_close(db);
+
				return (EPKG_FATAL);
+
			}
+

+
		if (eaccess(localpath, W_OK) == 0)
+
			db->writable = 1;

-
	if (pkgdb_upgrade(db) != EPKG_OK) {
-
		pkgdb_close(db);
-
		return (EPKG_FATAL);
-
	}
+
		if (pkgdb_upgrade(db) != EPKG_OK) {
+
			pkgdb_close(db);
+
			return (EPKG_FATAL);
+
		}

-
	sqlite3_create_function(db->sqlite, "regexp", 2, SQLITE_ANY, NULL,
-
							pkgdb_regex_basic, NULL, NULL);
-
	sqlite3_create_function(db->sqlite, "eregexp", 2, SQLITE_ANY, NULL,
-
							pkgdb_regex_extended, NULL, NULL);
-
	sqlite3_create_function(db->sqlite, "pkglt", 2, SQLITE_ANY, NULL,
-
			pkgdb_pkglt, NULL, NULL);
-
	sqlite3_create_function(db->sqlite, "pkggt", 2, SQLITE_ANY, NULL,
-
			pkgdb_pkggt, NULL, NULL);
+
		sqlite3_create_function(db->sqlite, "regexp", 2, SQLITE_ANY, NULL,
+
				pkgdb_regex_basic, NULL, NULL);
+
		sqlite3_create_function(db->sqlite, "eregexp", 2, SQLITE_ANY, NULL,
+
				pkgdb_regex_extended, NULL, NULL);
+
		sqlite3_create_function(db->sqlite, "pkglt", 2, SQLITE_ANY, NULL,
+
				pkgdb_pkglt, NULL, NULL);
+
		sqlite3_create_function(db->sqlite, "pkggt", 2, SQLITE_ANY, NULL,
+
				pkgdb_pkggt, NULL, NULL);

-
	/*
-
	 * allow foreign key option which will allow to have clean support for
-
	 * reinstalling
-
	 */
-
	if (sql_exec(db->sqlite, "PRAGMA foreign_keys = ON;") != EPKG_OK) {
-
		pkgdb_close(db);
-
		return (EPKG_FATAL);
+
		/*
+
		 * allow foreign key option which will allow to have clean support for
+
		 * reinstalling
+
		 */
+
		if (sql_exec(db->sqlite, "PRAGMA foreign_keys = ON;") != EPKG_OK) {
+
			pkgdb_close(db);
+
			return (EPKG_FATAL);
+
		}
	}

	if (type == PKGDB_REMOTE) {
@@ -535,9 +537,9 @@ pkgdb_open(struct pkgdb **db_p, pkgdb_t type)
						dbdir, repo_name);

				if (access(remotepath, R_OK) != 0) {
-
					pkg_emit_errno("access", remotepath);
+
					pkg_emit_error("Unable to remote database %s, try running `%s update` first ", remotepath, getprogname());
					pkgdb_close(db);
-
					return (EPKG_FATAL);
+
					return (EPKG_ENODB);
				}
				
				if (sql_exec(db->sqlite, "ATTACH '%q' AS '%q';", remotepath, repo_name) != EPKG_OK) {
@@ -560,9 +562,9 @@ pkgdb_open(struct pkgdb **db_p, pkgdb_t type)
			snprintf(remotepath, sizeof(remotepath), "%s/repo.sqlite", dbdir);

			if (access(remotepath, R_OK) != 0) {
-
				pkg_emit_errno("access", remotepath);
+
				pkg_emit_error("Unable to remote database %s, try running `%s update` first ", remotepath, getprogname());
				pkgdb_close(db);
-
				return (EPKG_FATAL);
+
				return (EPKG_ENODB);
			}
			
			if (sql_exec(db->sqlite, "ATTACH '%q' AS 'remote';", remotepath) != EPKG_OK) {
modified pkg/backup.c
@@ -13,7 +13,7 @@ usage_backup(void)
int
exec_backup(int argc, char **argv)
{
-
	struct pkgdb  *db;
+
	struct pkgdb  *db = NULL;
	char *dest = NULL;

	if (argc < 2 || argc > 3 || argv[1][0] != '-') {
modified pkg/check.c
@@ -26,7 +26,7 @@ STAILQ_HEAD(deps_head, deps_entry);
static int check_deps(struct pkgdb *db, struct pkg *pkg, struct deps_head *dh);
static void add_missing_dep(struct pkg_dep *d, struct deps_head *dh);
static void deps_free(struct deps_head *dh);
-
static void fix_deps(struct pkgdb *db, struct deps_head *dh, int nbpkgs, bool yes);
+
static int fix_deps(struct pkgdb *db, struct deps_head *dh, int nbpkgs, bool yes);
static void check_summary(struct pkgdb *db, struct deps_head *dh);

static int
@@ -104,7 +104,7 @@ deps_free(struct deps_head *dh)
	}
}

-
static void
+
static int
fix_deps(struct pkgdb *db, struct deps_head *dh, int nbpkgs, bool yes)
{
	struct pkg *pkg = NULL;
@@ -126,9 +126,13 @@ fix_deps(struct pkgdb *db, struct deps_head *dh, int nbpkgs, bool yes)
	STAILQ_FOREACH(e, dh, next)
		pkgs[i++] = e->origin;

+
	if (pkgdb_open(&db, PKGDB_REMOTE) != EPKG_OK)
+
		return (EPKG_FATAL);;
+

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

+

        if ((it = pkgdb_query_installs(db, MATCH_EXACT, nbpkgs, pkgs, NULL)) == NULL) {
		free(pkgs);
		pkg_jobs_free(jobs);
@@ -141,7 +145,7 @@ fix_deps(struct pkgdb *db, struct deps_head *dh, int nbpkgs, bool yes)

        if (pkg_jobs_is_empty(jobs)) {
                printf("\n>>> Not able to find packages for installation.\n\n");
-
		return;
+
		return (EPKG_FATAL);
        }

        /* print a summary before applying the jobs */
@@ -186,6 +190,8 @@ fix_deps(struct pkgdb *db, struct deps_head *dh, int nbpkgs, bool yes)
	pkg_free(pkg);
	pkg_jobs_free(jobs);
	pkgdb_it_free(it);
+

+
	return (EPKG_OK);
}

static void
@@ -238,6 +244,7 @@ exec_check(int argc, char **argv)
	struct pkgdb_it *it = NULL;
	struct pkgdb *db = NULL;
	int retcode = EX_OK;
+
	int ret;
	int ch;
	bool yes = false;
	int nbpkgs = 0;
@@ -262,14 +269,16 @@ exec_check(int argc, char **argv)
		return (EX_USAGE);
	}

-
	if (geteuid() != 0) {
-
		warnx("fixing the package database can only be done as root");
-
		return (EX_NOPERM);
+
	ret = pkgdb_open(&db, PKGDB_DEFAULT);
+
	if (ret == EPKG_ENODB) {
+
		if (geteuid() == 0)
+
			err(EX_IOERR, "Unable to create local database");
+

+
		return (retcode);
	}

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

	if ((it = pkgdb_query(db, NULL, MATCH_ALL)) == NULL) {
		pkgdb_close(db);
@@ -280,13 +289,14 @@ exec_check(int argc, char **argv)
	while (pkgdb_it_next(it, &pkg, PKG_LOAD_BASIC|PKG_LOAD_DEPS) == EPKG_OK) 
		nbpkgs += check_deps(db, pkg, &dh);

-
	if (nbpkgs > 0) {
+
	if (geteuid() == 0 && nbpkgs > 0) {
		if (yes == false) 
			pkg_config_bool(PKG_CONFIG_ASSUME_ALWAYS_YES, &yes);

		printf("\n>>> Missing package dependencies were detected.\n\n");
-
		fix_deps(db, &dh, nbpkgs, yes);
-
		check_summary(db, &dh);
+
		ret = fix_deps(db, &dh, nbpkgs, yes);
+
		if (ret == EPKG_OK)
+
			check_summary(db, &dh);
	}

	deps_free(&dh);
modified pkg/info.c
@@ -37,8 +37,8 @@ usage_info(void)
int
exec_info(int argc, char **argv)
{
-
	struct pkgdb *db;
-
	struct pkgdb_it *it;
+
	struct pkgdb *db = NULL;
+
	struct pkgdb_it *it = NULL;
	int query_flags = PKG_LOAD_BASIC;
	struct pkg *pkg = NULL;
	unsigned int opt = 0;
modified pkg/which.c
@@ -21,8 +21,8 @@ usage_which(void)
int
exec_which(int argc, char **argv)
{
-
	struct pkgdb *db;
-
	struct pkgdb_it *it;
+
	struct pkgdb *db = NULL;
+
	struct pkgdb_it *it = NULL;
	struct pkg *pkg = NULL;
	char pathabs[MAXPATHLEN + 1];
	int ret = EPKG_OK, retcode = EPKG_OK;