Radish alpha
H
rad:z3QDZAW2FAfuLvihrhiyDC9fAD8G9
HardenedBSD Package Manager
Radicle
Git
Improvements for pkgdb_open. - Check to make sure the db dir has the appropriate permissions for the caller. Some just need to read and others also need to write. This also provides a more usable error message in case the user hasn't created the db directory yet. - Split out a few compound statements.
Will Andrews committed 14 years ago
commit ed3421c215be0eaa5750978aad9f65c3be792412
parent 65cf3f1
9 files changed +23 -17
modified libpkg/pkg.h
@@ -396,7 +396,7 @@ int pkg_finish_repo(char *patj, pem_password_cb *cb, char *rsa_key_path);
 * The db must be free'ed with pkgdb_close().
 * @return An error code.
 */
-
int pkgdb_open(struct pkgdb **db, pkgdb_t remote);
+
int pkgdb_open(struct pkgdb **db, pkgdb_t remote, int mode);

/**
 * Close and free the struct pkgdb.
modified libpkg/pkgdb.c
@@ -213,7 +213,7 @@ pkgdb_init(sqlite3 *sdb)
}

int
-
pkgdb_open(struct pkgdb **db, pkgdb_t remote)
+
pkgdb_open(struct pkgdb **db, pkgdb_t remote, int mode)
{
	int retcode;
	struct stat st;
@@ -221,23 +221,30 @@ pkgdb_open(struct pkgdb **db, pkgdb_t remote)
	char localpath[MAXPATHLEN];
	char remotepath[MAXPATHLEN];
	char sql[BUFSIZ];
+
	const char *dbdir;

-
	snprintf(localpath, sizeof(localpath), "%s/local.sqlite",
-
			 pkg_config("PKG_DBDIR"));
+
	/* First check to make sure PKG_DBDIR exists before trying to use it */
+
	dbdir = pkg_config("PKG_DBDIR");
+
	if (eaccess(dbdir, mode) == -1)
+
		return (pkg_error_set(EPKG_FATAL, "Package database directory "
+
		    "%s error: %s", dbdir, strerror(errno)));
+

+
	snprintf(localpath, sizeof(localpath), "%s/local.sqlite", dbdir);

	if ((*db = calloc(1, sizeof(struct pkgdb))) == NULL)
		return (pkg_error_set(EPKG_FATAL, "calloc(): %s", strerror(errno)));

	(*db)->remote = remote;

-
	if ((retcode = stat(localpath, &st)) == -1 && errno != ENOENT)
+
	retcode = stat(localpath, &st);
+
	if (retcode == -1 && errno != ENOENT)
		return (pkg_error_set(EPKG_FATAL, "can not stat %s: %s", localpath,
							  strerror(errno)));

	if (remote == PKGDB_REMOTE) {
-
		snprintf(remotepath, sizeof(localpath), "%s/repo.sqlite",
-
				 pkg_config("PKG_DBDIR"));
-
		if ((retcode = stat(remotepath, &st)) == -1 && errno != ENOENT)
+
		snprintf(remotepath, sizeof(localpath), "%s/repo.sqlite", dbdir);
+
		retcode = stat(remotepath, &st);
+
		if (retcode == -1 && errno != ENOENT)
			return (pkg_error_set(EPKG_FATAL, "can not stat %s: %s", remotepath,
						strerror(errno)));
	}
@@ -257,7 +264,7 @@ pkgdb_open(struct pkgdb **db, pkgdb_t remote)
	/* If the database is missing we have to initialize it */
	if (retcode == -1)
		if ((retcode = pkgdb_init((*db)->sqlite)) != EPKG_OK)
-
			return (retcode);
+
			return (ERROR_SQLITE((*db)->sqlite));

	sqlite3_create_function((*db)->sqlite, "regexp", 2, SQLITE_ANY, NULL,
							pkgdb_regex_basic, NULL, NULL);
modified pkg/add.c
@@ -67,7 +67,7 @@ exec_add(int argc, char **argv)
		return (EX_NOPERM);
	}

-
	if (pkgdb_open(&db, PKGDB_DEFAULT) != EPKG_OK) {
+
	if (pkgdb_open(&db, PKGDB_DEFAULT, R_OK|W_OK) != EPKG_OK) {
		pkg_error_warn("can not open database");
		return (1);
	}
modified pkg/create.c
@@ -29,7 +29,7 @@ pkg_create_matches(int argc, char **argv, match_t match, pkg_formats fmt, const
					  PKG_LOAD_EXECS | PKG_LOAD_SCRIPTS | PKG_LOAD_OPTIONS |
					  PKG_LOAD_MTREE;

-
	if (pkgdb_open(&db, PKGDB_DEFAULT) != EPKG_OK) {
+
	if (pkgdb_open(&db, PKGDB_DEFAULT, R_OK) != EPKG_OK) {
		pkg_error_warn("can not open database");
		pkgdb_close(db);
		return (-1);
modified pkg/delete.c
@@ -55,7 +55,7 @@ exec_delete(int argc, char **argv)
		return (EX_NOPERM);
	}
	
-
	if (pkgdb_open(&db, PKGDB_DEFAULT) != EPKG_OK) {
+
	if (pkgdb_open(&db, PKGDB_DEFAULT, R_OK|W_OK) != EPKG_OK) {
		pkg_error_warn("can not open database");
		pkgdb_close(db);
		return (-1);
modified pkg/info.c
@@ -164,9 +164,8 @@ exec_info(int argc, char **argv)

	pkg_new(&pkg);

-
	if (pkgdb_open(&db, PKGDB_DEFAULT) != EPKG_OK) {
+
	if (pkgdb_open(&db, PKGDB_DEFAULT, R_OK) != EPKG_OK) {
		pkg_error_warn("can not open database");
-
		pkgdb_close(db);
		return (-1);
	}

modified pkg/register.c
@@ -203,7 +203,7 @@ exec_register(int argc, char **argv)
	if (plist != NULL)
		free(plist);

-
	if (pkgdb_open(&db, PKGDB_DEFAULT) != EPKG_OK) {
+
	if (pkgdb_open(&db, PKGDB_DEFAULT, R_OK|W_OK) != EPKG_OK) {
		pkg_error_warn("can not open database");
		return (-1);
	}
modified pkg/upgrade.c
@@ -43,7 +43,7 @@ exec_upgrade(int argc, char **argv)
		return (EX_NOPERM);
	}

-
	if (pkgdb_open(&db, PKGDB_REMOTE) != EPKG_OK) {
+
	if (pkgdb_open(&db, PKGDB_REMOTE, R_OK|W_OK) != EPKG_OK) {
		pkg_error_warn("can not open database");
		return (1);
	}
modified pkg/which.c
@@ -34,7 +34,7 @@ exec_which(int argc, char **argv)
		return (EX_USAGE);
	}

-
	if (pkgdb_open(&db, PKGDB_DEFAULT) != EPKG_OK) {
+
	if (pkgdb_open(&db, PKGDB_DEFAULT, R_OK) != EPKG_OK) {
		pkg_error_warn("can not open database");
		pkgdb_close(db);
		return (-1);