Radish alpha
H
rad:z3QDZAW2FAfuLvihrhiyDC9fAD8G9
HardenedBSD Package Manager
Radicle
Git
Add a helper to get the dbdir and the cachedir path
Baptiste Daroussin committed 6 years ago
commit 8dd464b255b1697122156e4d3ee38ae0cb6c990c
parent 0f9a6f8
9 files changed +35 -45
modified libpkg/pkg_config.c
@@ -68,6 +68,8 @@ struct pkg_ctx ctx = {
	.debug_level = 0,
	.developer_mode = false,
	.pkg_rootdir = NULL,
+
	.dbdir = NULL,
+
	.cachedir = NULL,
	.rootfd = -1,
	.cachedirfd = -1,
	.pkg_dbdirfd = -1,
@@ -1185,6 +1187,8 @@ pkg_ini(const char *path, const char *reposdir, pkg_init_flags flags)

	ctx.debug_level = pkg_object_int(pkg_config_get("DEBUG_LEVEL"));
	ctx.developer_mode = pkg_object_bool(pkg_config_get("DEVELOPER_MODE"));
+
	ctx.dbdir = pkg_object_string(pkg_config_get("PKG_DBDIR"));
+
	ctx.cachedir = pkg_object_string(pkg_config_get("PKG_CACHEDIR"));

	it = NULL;
	object = ucl_object_find_key(config, "PKG_ENV");
@@ -1483,15 +1487,13 @@ pkg_set_rootdir(const char *rootdir) {
int
pkg_get_cachedirfd(void)
{
-
	const char *cachedir;

	if (ctx.cachedirfd == -1) {
-
		cachedir = pkg_object_string(pkg_config_get("PKG_CACHEDIR"));
		/*
		 * do not check the value as if we cannot open it means
		 * it has not been created yet
		 */
-
		ctx.cachedirfd = open(cachedir, O_DIRECTORY|O_CLOEXEC);
+
		ctx.cachedirfd = open(ctx.cachedir, O_DIRECTORY|O_CLOEXEC);
	}

	return (ctx.cachedirfd);
@@ -1500,15 +1502,13 @@ pkg_get_cachedirfd(void)
int
pkg_get_dbdirfd(void)
{
-
	const char *dbdir;

	if (ctx.pkg_dbdirfd == -1) {
-
		dbdir = pkg_object_string(pkg_config_get("PKG_DBDIR"));
		/*
		 * do not check the value as if we cannot open it means
		 * it has not been created yet
		 */
-
		ctx.pkg_dbdirfd = open(dbdir, O_DIRECTORY|O_CLOEXEC);
+
		ctx.pkg_dbdirfd = open(ctx.dbdir, O_DIRECTORY|O_CLOEXEC);
	}

	return (ctx.pkg_dbdirfd);
modified libpkg/pkg_jobs.c
@@ -66,6 +66,8 @@
#include "private/pkg_jobs.h"
#include "kvec.h"

+
extern struct pkg_ctx ctx;
+

static int pkg_jobs_find_upgrade(struct pkg_jobs *j, const char *pattern, match_t m);
static int pkg_jobs_fetch(struct pkg_jobs *j);
static bool new_pkg_version(struct pkg_jobs *j);
@@ -2174,7 +2176,7 @@ pkg_jobs_fetch(struct pkg_jobs *j)


	if (j->destdir == NULL || !mirror)
-
		cachedir = pkg_object_string(pkg_config_get("PKG_CACHEDIR"));
+
		cachedir = ctx.cachedir;
	else
		cachedir = j->destdir;

modified libpkg/pkg_status.c
@@ -36,7 +36,9 @@
#include <bsd_compat.h>

#include "pkg.h"
+
#include "private/pkg.h"

+
extern struct pkg_ctx ctx;

#ifndef _LOCALBASE
#define _LOCALBASE	"/usr/local"
@@ -47,7 +49,6 @@ static bool is_exec_at_localbase(const char *progname);
pkg_status_t
pkg_status(int *count)
{
-
	const pkg_object	*o;
	const char		*progname;
	char			 dbpath[MAXPATHLEN];
	int			 numpkgs = 0;
@@ -73,8 +74,7 @@ pkg_status(int *count)
	/* Does the local.sqlite pkg database exist, and can we open
	   it for reading? */

-
	o = pkg_config_get("PKG_DBDIR");
-
	snprintf(dbpath, sizeof(dbpath), "%s/local.sqlite", pkg_object_string(o));
+
	snprintf(dbpath, sizeof(dbpath), "%s/local.sqlite", ctx.dbdir);

	if (eaccess(dbpath, R_OK) == -1)
		return (PKG_STATUS_NODB);
modified libpkg/pkgdb.c
@@ -72,6 +72,8 @@

#include "private/db_upgrades.h"

+
extern struct pkg_ctx ctx;
+

/* An application using a libpkg() DBVERSION is assumed to be compatible
   with:

@@ -869,8 +871,6 @@ pkgdb_check_access(unsigned mode, const char* dbdir, const char *dbname)
int
pkgdb_access(unsigned mode, unsigned database)
{
-
	const pkg_object	*o;
-
	const char		*dbdir;
	int			 retval = EPKG_OK;

	/*
@@ -893,8 +893,6 @@ pkgdb_access(unsigned mode, unsigned database)
	 * EPKG_OK: We can go ahead
	 */

-
	o = pkg_config_get("PKG_DBDIR");
-
	dbdir = pkg_object_string(o);
	if ((mode & ~(PKGDB_MODE_READ|PKGDB_MODE_WRITE|PKGDB_MODE_CREATE))
	    != 0)
		return (EPKG_FATAL); /* EINVAL */
@@ -908,16 +906,16 @@ pkgdb_access(unsigned mode, unsigned database)

	if ((mode & PKGDB_MODE_CREATE) != 0) {
		retval = pkgdb_check_access(PKGDB_MODE_READ|PKGDB_MODE_WRITE,
-
					 dbdir, NULL);
+
					 ctx.dbdir, NULL);
	} else
-
		retval = pkgdb_check_access(PKGDB_MODE_READ, dbdir, NULL);
+
		retval = pkgdb_check_access(PKGDB_MODE_READ, ctx.dbdir, NULL);
	if (retval != EPKG_OK)
		return (retval);

	/* Test local.sqlite, if required */

	if ((database & PKGDB_DB_LOCAL) != 0) {
-
		retval = pkgdb_check_access(mode, dbdir, "local.sqlite");
+
		retval = pkgdb_check_access(mode, ctx.dbdir, "local.sqlite");
		if (retval != EPKG_OK)
			return (retval);
	}
@@ -1102,7 +1100,6 @@ pkgdb_open_all(struct pkgdb **db_p, pkgdb_t type, const char *reponame)
	struct pkgdb	*db = NULL;
	bool		 reopen = false;
	bool		 profile = false;
-
	const char	*dbdir;
	bool		 create = false;
	int		 ret;
	int		 dbdirfd;
@@ -1121,8 +1118,7 @@ retry:
		dbdirfd = pkg_get_dbdirfd();
		if (dbdirfd == -1) {
			if (errno == ENOENT) {
-
				dbdir = pkg_object_string(pkg_config_get("PKG_DBDIR"));
-
				if (mkdirs(dbdir) != EPKG_OK) {
+
				if (mkdirs(ctx.dbdir) != EPKG_OK) {
					pkgdb_close(db);
					return (EPKG_FATAL);
				}
@@ -2750,13 +2746,10 @@ void
pkgshell_open(const char **reponame)
{
	char		 localpath[MAXPATHLEN];
-
	const char	*dbdir;

	sqlite3_auto_extension((void(*)(void))pkgdb_sqlcmd_init);

-
	dbdir = pkg_object_string(pkg_config_get("PKG_DBDIR"));
-

-
	snprintf(localpath, sizeof(localpath), "%s/local.sqlite", dbdir);
+
	snprintf(localpath, sizeof(localpath), "%s/local.sqlite", ctx.dbdir);
	*reponame = xstrdup(localpath);
}

modified libpkg/private/pkg.h
@@ -241,6 +241,8 @@ struct pkg_ctx {
	int64_t debug_level;
	bool developer_mode;
	const char *pkg_rootdir;
+
	const char *dbdir;
+
	const char *cachedir;
	int rootfd;
	int cachedirfd;
	int dbdirfd;
modified libpkg/repo/binary/fetch.c
@@ -49,17 +49,16 @@
#include "private/pkg.h"
#include "binary.h"

+
extern struct pkg_ctx ctx;
+

int
pkg_repo_binary_get_cached_name(struct pkg_repo *repo, struct pkg *pkg,
	char *dest, size_t destlen)
{
	const char *ext = NULL;
-
	const char *cachedir = NULL;
	const char *packagesite;
	struct stat st;

-
	cachedir = pkg_object_string(pkg_config_get("PKG_CACHEDIR"));
-

	packagesite = pkg_repo_url(repo);

	if (strncmp(packagesite, "file:/", 6) == 0) {
@@ -77,14 +76,14 @@ pkg_repo_binary_get_cached_name(struct pkg_repo *repo, struct pkg *pkg,
		 * <cachedir>/<name>-<version>-<checksum>.txz
		 */
		pkg_snprintf(dest, destlen, "%S/%n-%v-%z%S",
-
				cachedir, pkg, pkg, pkg, ext);
+
				ctx.cachedir, pkg, pkg, pkg, ext);
		if (stat (dest, &st) == -1 || pkg->pkgsize != st.st_size)
			return (EPKG_FATAL);

	}
	else {
		pkg_snprintf(dest, destlen, "%S/%n-%v-%z",
-
				cachedir, pkg, pkg, pkg);
+
				ctx.cachedir, pkg, pkg, pkg);
	}

	return (EPKG_OK);
@@ -146,9 +145,9 @@ pkg_repo_binary_try_fetch(struct pkg_repo *repo, struct pkg *pkg,
		if (destdir != NULL)
			cachedir = destdir;
		else
-
			cachedir = pkg_object_string(pkg_config_get("PKG_CACHEDIR"));
+
			cachedir = ctx.cachedir;

-
		snprintf(dest, sizeof(dest), "%s/%s", cachedir, pkg->repopath);
+
		snprintf(dest, sizeof(dest), "%s/%s", ctx.cachedir, pkg->repopath);
	}
	else
		pkg_repo_binary_get_cached_name(repo, pkg, dest, sizeof(dest));
modified libpkg/repo/binary/init.c
@@ -48,6 +48,8 @@
#include "binary.h"
#include "binary_private.h"

+
extern struct pkg_ctx ctx;
+

static void
sqlite_file_exists(sqlite3_context *ctx, int argc, sqlite3_value **argv)
{
@@ -523,14 +525,9 @@ pkg_repo_binary_close(struct pkg_repo *repo, bool commit)
int
pkg_repo_binary_access(struct pkg_repo *repo, unsigned mode)
{
-
	const pkg_object	*o;
-
	const char		*dbdir;
	int			 ret = EPKG_OK;

-
	o = pkg_config_get("PKG_DBDIR");
-
	dbdir = pkg_object_string(o);
-

-
	ret = pkgdb_check_access(mode, dbdir,
+
	ret = pkgdb_check_access(mode, ctx.dbdir,
		pkg_repo_binary_get_filename(pkg_repo_name(repo)));

	return (ret);
modified libpkg/repo/binary/update.c
@@ -597,7 +597,6 @@ pkg_repo_binary_update(struct pkg_repo *repo, bool force)
		"DROP TABLE repo_update;";
	sqlite3 *sqlite;

-
	const char *dbdir = NULL;
	struct stat st;
	time_t t = 0;
	int res = EPKG_FATAL;
@@ -609,7 +608,6 @@ pkg_repo_binary_update(struct pkg_repo *repo, bool force)
	if (!pkg_repo_enabled(repo))
		return (EPKG_OK);

-
	dbdir = pkg_object_string(pkg_config_get("PKG_DBDIR"));
	pkg_debug(1, "PkgRepo: verifying update for %s", pkg_repo_name(repo));

	/* First of all, try to open and init repo and check whether it is fine */
@@ -617,18 +615,18 @@ pkg_repo_binary_update(struct pkg_repo *repo, bool force)
		pkg_debug(1, "PkgRepo: need forced update of %s", pkg_repo_name(repo));
		t = 0;
		force = true;
-
		snprintf(filepath, sizeof(filepath), "%s/%s", dbdir,
+
		snprintf(filepath, sizeof(filepath), "%s/%s", ctx.dbdir,
		    pkg_repo_binary_get_filename(pkg_repo_name(repo)));
	}
	else {
		repo->ops->close(repo, false);
-
		snprintf(filepath, sizeof(filepath), "%s/%s.meta", dbdir, pkg_repo_name(repo));
+
		snprintf(filepath, sizeof(filepath), "%s/%s.meta", ctx.dbdir, pkg_repo_name(repo));
		if (stat(filepath, &st) != -1) {
			t = force ? 0 : st.st_mtime;
			got_meta = true;
		}

-
		snprintf(filepath, sizeof(filepath), "%s/%s", dbdir,
+
		snprintf(filepath, sizeof(filepath), "%s/%s", ctx.dbdir,
			pkg_repo_binary_get_filename(pkg_repo_name(repo)));
		if (stat(filepath, &st) != -1) {
			if (!got_meta && !force)
@@ -664,7 +662,7 @@ cleanup:

		utimes(filepath, ftimes);
		if (got_meta) {
-
			snprintf(filepath, sizeof(filepath), "%s/%s.meta", dbdir, pkg_repo_name(repo));
+
			snprintf(filepath, sizeof(filepath), "%s/%s.meta", ctx.dbdir, pkg_repo_name(repo));
			utimes(filepath, ftimes);
		}
	}
modified src/clean.c
@@ -337,8 +337,7 @@ exec_clean(int argc, char **argv)
		}
	}

-
	cachedir = pkg_object_string(pkg_config_get("PKG_CACHEDIR"));
-
	cachefd = open(cachedir, O_DIRECTORY|O_CLOEXEC);
+
	cachefd = pkg_get_cachedirfd();
	if (cachefd == -1) {
		warn("Impossible to open %s", cachedir);
		return (errno == ENOENT ? EX_OK : EX_IOERR);