Radish alpha
H
rad:z3QDZAW2FAfuLvihrhiyDC9fAD8G9
HardenedBSD Package Manager
Radicle
Git
Move the globals under a ctx object to prepare for newer api
Baptiste Daroussin committed 9 years ago
commit 7ea4379ec340b0760a349cca77d52a6bdfb65931
parent badcc05
10 files changed +87 -82
modified libpkg/fetch.c
@@ -603,7 +603,7 @@ pkg_fetch_file_to_fd(struct pkg_repo *repo, const char *url, int dest,
				utstring_printf(fetchOpts, "6");
		}

-
		if (debug_level >= 4)
+
		if (ctx.debug_level >= 4)
			utstring_printf(fetchOpts, "v");

		pkg_debug(1,"Fetch: fetching from: %s://%s%s%s%s with opts \"%s\"",
modified libpkg/pkg.c
@@ -567,7 +567,7 @@ pkg_adduser(struct pkg *pkg, const char *name)
	assert(name != NULL && name[0] != '\0');

	if (kh_contains(strings, pkg->users, name)) {
-
		if (developer_mode) {
+
		if (ctx.developer_mode) {
			pkg_emit_error("duplicate user listing: %s, fatal (developer mode)", name);
			return (EPKG_FATAL);
		} else {
@@ -591,7 +591,7 @@ pkg_addgroup(struct pkg *pkg, const char *name)
	assert(name != NULL && name[0] != '\0');

	if (kh_contains(strings, pkg->groups, name)) {
-
		if (developer_mode) {
+
		if (ctx.developer_mode) {
			pkg_emit_error("duplicate group listing: %s, fatal (developer mode)", name);
			return (EPKG_FATAL);
		} else {
@@ -703,7 +703,7 @@ pkg_addfile_attr(struct pkg *pkg, const char *path, const char *sum,
	pkg_debug(3, "Pkg: add new file '%s'", path);

	if (check_duplicates && kh_contains(pkg_files, pkg->filehash, path)) {
-
		if (developer_mode) {
+
		if (ctx.developer_mode) {
			pkg_emit_error("duplicate file listing: %s, fatal (developer mode)", path);
			return (EPKG_FATAL);
		} else {
@@ -746,7 +746,7 @@ pkg_addconfig_file(struct pkg *pkg, const char *path, const char *content)
	pkg_debug(3, "Pkg: add new config file '%s'", path);

	if (kh_contains(pkg_config_files, pkg->config_files, path)) {
-
		if (developer_mode) {
+
		if (ctx.developer_mode) {
			pkg_emit_error("duplicate file listing: %s, fatal (developer mode)", path);
			return (EPKG_FATAL);
		} else {
@@ -773,7 +773,7 @@ pkg_addstring(kh_strings_t **list, const char *val, const char *title)
	assert(title != NULL);

	if (kh_contains(strings, *list, val)) {
-
		if (developer_mode) {
+
		if (ctx.developer_mode) {
			pkg_emit_error("duplicate %s listing: %s, fatal"
			    " (developer mode)", title, val);
			return (EPKG_FATAL);
@@ -813,7 +813,7 @@ pkg_adddir_attr(struct pkg *pkg, const char *path, const char *uname,
	path = pkg_absolutepath(path, abspath, sizeof(abspath), false);
	pkg_debug(3, "Pkg: add new directory '%s'", path);
	if (check_duplicates && kh_contains(pkg_dirs, pkg->dirhash, path)) {
-
		if (developer_mode) {
+
		if (ctx.developer_mode) {
			pkg_emit_error("duplicate directory listing: %s, fatal (developer mode)", path);
			return (EPKG_FATAL);
		} else {
@@ -1001,7 +1001,7 @@ pkg_addoption(struct pkg *pkg, const char *key, const char *value)

	pkg_debug(2,"Pkg> adding options: %s = %s", key, value);
	if (kh_contains(pkg_options, pkg->optionshash, key)) {
-
		if (developer_mode) {
+
		if (ctx.developer_mode) {
			pkg_emit_error("duplicate options listing: %s, fatal (developer mode)", key);
			return (EPKG_FATAL);
		} else {
@@ -1035,7 +1035,7 @@ pkg_addoption_default(struct pkg *pkg, const char *key,
	   no actual value. */

	if (kh_contains(pkg_options, pkg->optionshash, key)) {
-
		if (developer_mode) {
+
		if (ctx.developer_mode) {
			pkg_emit_error("duplicate default value for option: %s, fatal (developer mode)", key);
			return (EPKG_FATAL);
		} else {
@@ -1068,7 +1068,7 @@ pkg_addoption_description(struct pkg *pkg, const char *key,
	   value or description for an option but no actual value. */

	if (kh_contains(pkg_options, pkg->optionshash, key)) {
-
		if (developer_mode) {
+
		if (ctx.developer_mode) {
			pkg_emit_error("duplicate description for option: %s, fatal (developer mode)", key);
			return (EPKG_FATAL);
		} else {
@@ -1216,7 +1216,7 @@ pkg_kv_add(struct pkg_kv **list, const char *key, const char *val, const char *t

	LL_FOREACH(*list, kv) {
		if (strcmp(kv->key, key) == 0) {
-
			if (developer_mode) {
+
			if (ctx.developer_mode) {
				pkg_emit_error("duplicate %s: %s, fatal"
				    " (developer mode)", title, key);
				return (EPKG_FATAL);
@@ -1742,9 +1742,9 @@ pkg_open_root_fd(struct pkg *pkg)
	path = pkg_kv_get(&pkg->annotations, "relocated");
	if (path == NULL) {
#ifdef F_DUPFD_CLOEXEC
-
		if ((pkg->rootfd = fcntl(rootfd, F_DUPFD_CLOEXEC, 0)) == -1) {
+
		if ((pkg->rootfd = fcntl(ctx.rootfd, F_DUPFD_CLOEXEC, 0)) == -1) {
#else
-
		if ((pkg->rootfd = dup(rootfd)) == -1 || fcntl(pkg->rootfd, F_SETFD, FD_CLOEXEC) == -1) {
+
		if ((pkg->rootfd = dup(ctx.rootfd)) == -1 || fcntl(pkg->rootfd, F_SETFD, FD_CLOEXEC) == -1) {
#endif
			pkg_emit_errno("dup2", "rootfd");
			return (EPKG_FATAL);
@@ -1754,7 +1754,7 @@ pkg_open_root_fd(struct pkg *pkg)

	pkg_absolutepath(path, pkg->rootpath, sizeof(pkg->rootpath), false);

-
	if ((pkg->rootfd = openat(rootfd, pkg->rootpath + 1, O_DIRECTORY|O_CLOEXEC)) >= 0 )
+
	if ((pkg->rootfd = openat(ctx.rootfd, pkg->rootpath + 1, O_DIRECTORY|O_CLOEXEC)) >= 0 )
		return (EPKG_OK);

	pkg->rootpath[0] = '\0';
modified libpkg/pkg_config.c
@@ -63,13 +63,15 @@
#define INDEXFILE	"INDEX"
#endif

-
int eventpipe = -1;
-
int64_t debug_level = 0;
-
bool developer_mode = false;
-
const char *pkg_rootdir = NULL;
-
int rootfd = -1;
-
int cachedirfd = -1;
-
int pkg_dbdirfd = -1;
+
struct pkg_ctx ctx = {
+
	.eventpipe = -1,
+
	.debug_level = 0,
+
	.developer_mode = false,
+
	.pkg_rootdir = NULL,
+
	.rootfd = -1,
+
	.cachedirfd = -1,
+
	.pkg_dbdirfd = -1,
+
};

struct config_entry {
	uint8_t type;
@@ -447,13 +449,13 @@ connect_evpipe(const char *evpipe) {

	if (S_ISFIFO(st.st_mode)) {
		flag |= O_NONBLOCK;
-
		if ((eventpipe = open(evpipe, flag)) == -1)
+
		if ((ctx.eventpipe = open(evpipe, flag)) == -1)
			pkg_emit_errno("open event pipe", evpipe);
		return;
	}

	if (S_ISSOCK(st.st_mode)) {
-
		if ((eventpipe = socket(AF_UNIX, SOCK_STREAM, 0)) == -1) {
+
		if ((ctx.eventpipe = socket(AF_UNIX, SOCK_STREAM, 0)) == -1) {
			pkg_emit_errno("Open event pipe", evpipe);
			return;
		}
@@ -462,15 +464,15 @@ connect_evpipe(const char *evpipe) {
		if (strlcpy(sock.sun_path, evpipe, sizeof(sock.sun_path)) >=
		    sizeof(sock.sun_path)) {
			pkg_emit_error("Socket path too long: %s", evpipe);
-
			close(eventpipe);
-
			eventpipe = -1;
+
			close(ctx.eventpipe);
+
			ctx.eventpipe = -1;
			return;
		}

-
		if (connect(eventpipe, (struct sockaddr *)&sock, SUN_LEN(&sock)) == -1) {
+
		if (connect(ctx.eventpipe, (struct sockaddr *)&sock, SUN_LEN(&sock)) == -1) {
			pkg_emit_errno("Connect event pipe", evpipe);
-
			close(eventpipe);
-
			eventpipe = -1;
+
			close(ctx.eventpipe);
+
			ctx.eventpipe = -1;
			return;
		}
	}
@@ -878,7 +880,7 @@ pkg_ini(const char *path, const char *reposdir, pkg_init_flags flags)

	k = NULL;
	o = NULL;
-
	if (rootfd == -1 && (rootfd = open("/", O_DIRECTORY|O_RDONLY|O_CLOEXEC)) < 0) {
+
	if (ctx.rootfd == -1 && (ctx.rootfd = open("/", O_DIRECTORY|O_RDONLY|O_CLOEXEC)) < 0) {
		pkg_emit_error("Impossible to open /");
		return (EPKG_FATAL);
	}
@@ -903,8 +905,8 @@ pkg_ini(const char *path, const char *reposdir, pkg_init_flags flags)
		case PKG_STRING:
			tmp = NULL;
			if (c[i].def != NULL && c[i].def[0] == '/' &&
-
			    pkg_rootdir != NULL) {
-
				xasprintf(&tmp, "%s%s", pkg_rootdir, c[i].def);
+
			    ctx.pkg_rootdir != NULL) {
+
				xasprintf(&tmp, "%s%s", ctx.pkg_rootdir, c[i].def);
			}
			obj = ucl_object_fromstring_common(
			    c[i].def != NULL ? tmp != NULL ? tmp : c[i].def : "", 0, UCL_STRING_TRIM);
@@ -976,12 +978,12 @@ pkg_ini(const char *path, const char *reposdir, pkg_init_flags flags)
	}

	if (path == NULL)
-
		conffd = openat(rootfd, PREFIX"/etc/pkg.conf" + 1, 0);
+
		conffd = openat(ctx.rootfd, PREFIX"/etc/pkg.conf" + 1, 0);
	else
		conffd = open(path, O_RDONLY);
	if (conffd == -1 && errno != ENOENT) {
		pkg_errno("Cannot open %s/%s",
-
		    pkg_rootdir != NULL ? pkg_rootdir : "",
+
		    ctx.pkg_rootdir != NULL ? ctx.pkg_rootdir : "",
		    path);
	}

@@ -1163,8 +1165,8 @@ pkg_ini(const char *path, const char *reposdir, pkg_init_flags flags)
	if (evpipe != NULL)
		connect_evpipe(evpipe);

-
	debug_level = pkg_object_int(pkg_config_get("DEBUG_LEVEL"));
-
	developer_mode = pkg_object_bool(pkg_config_get("DEVELOPER_MODE"));
+
	ctx.debug_level = pkg_object_int(pkg_config_get("DEBUG_LEVEL"));
+
	ctx.developer_mode = pkg_object_bool(pkg_config_get("DEVELOPER_MODE"));

	it = NULL;
	object = ucl_object_find_key(config, "PKG_ENV");
@@ -1320,12 +1322,12 @@ pkg_shutdown(void)
	ucl_object_unref(config);
	HASH_FREE(repos, pkg_repo_free);

-
	if (rootfd != -1)
-
		close(rootfd);
-
	if (cachedirfd != -1)
-
		close(rootfd);
-
	if (pkg_dbdirfd != -1)
-
		close(pkg_dbdirfd);
+
	if (ctx.rootfd != -1)
+
		close(ctx.rootfd);
+
	if (ctx.cachedirfd != -1)
+
		close(ctx.rootfd);
+
	if (ctx.pkg_dbdirfd != -1)
+
		close(ctx.pkg_dbdirfd);

	parsed = false;

@@ -1431,9 +1433,9 @@ pkg_repo_find(const char *reponame)

int64_t
pkg_set_debug_level(int64_t new_debug_level) {
-
	int64_t old_debug_level = debug_level;
+
	int64_t old_debug_level = ctx.debug_level;

-
	debug_level = new_debug_level;
+
	ctx.debug_level = new_debug_level;
	return old_debug_level;
}

@@ -1442,14 +1444,14 @@ pkg_set_rootdir(const char *rootdir) {
	if (pkg_initialized())
		return (EPKG_FATAL);

-
	if (rootfd != -1)
-
		close(rootfd);
+
	if (ctx.rootfd != -1)
+
		close(ctx.rootfd);

-
	if ((rootfd = open(rootdir, O_DIRECTORY|O_RDONLY|O_CLOEXEC)) < 0) {
+
	if ((ctx.rootfd = open(rootdir, O_DIRECTORY|O_RDONLY|O_CLOEXEC)) < 0) {
		pkg_emit_error("Impossible to open %s", rootdir);
		return (EPKG_FATAL);
	}
-
	pkg_rootdir = rootdir;
+
	ctx.pkg_rootdir = rootdir;

	return (EPKG_OK);
}
@@ -1459,16 +1461,16 @@ pkg_get_cachedirfd(void)
{
	const char *cachedir;

-
	if (cachedirfd == -1) {
+
	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
		 */
-
		cachedirfd = open(cachedir, O_DIRECTORY|O_CLOEXEC);
+
		ctx.cachedirfd = open(cachedir, O_DIRECTORY|O_CLOEXEC);
	}

-
	return (cachedirfd);
+
	return (ctx.cachedirfd);
}

int
@@ -1476,14 +1478,14 @@ pkg_get_dbdirfd(void)
{
	const char *dbdir;

-
	if (pkg_dbdirfd == -1) {
+
	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
		 */
-
		pkg_dbdirfd = open(dbdir, O_DIRECTORY|O_CLOEXEC);
+
		ctx.pkg_dbdirfd = open(dbdir, O_DIRECTORY|O_CLOEXEC);
	}

-
	return (pkg_dbdirfd);
+
	return (ctx.pkg_dbdirfd);
}
modified libpkg/pkg_create.c
@@ -68,8 +68,8 @@ pkg_create_from_dir(struct pkg *pkg, const char *root,
	relocation = pkg_kv_get(&pkg->annotations, "relocated");
	if (relocation == NULL)
		relocation = "";
-
	if (pkg_rootdir != NULL)
-
		relocation = pkg_rootdir;
+
	if (ctx.pkg_rootdir != NULL)
+
		relocation = ctx.pkg_rootdir;

	/*
	 * Get / compute size / checksum if not provided in the manifest
@@ -139,7 +139,7 @@ pkg_create_from_dir(struct pkg *pkg, const char *root,

		ret = packing_append_file_attr(pkg_archive, fpath, file->path,
		    file->uname, file->gname, file->perm, file->fflags);
-
		if (developer_mode && ret != EPKG_OK)
+
		if (ctx.developer_mode && ret != EPKG_OK)
			return (ret);
		counter_count();
	}
@@ -155,7 +155,7 @@ pkg_create_from_dir(struct pkg *pkg, const char *root,

		ret = packing_append_file_attr(pkg_archive, fpath, dir->path,
		    dir->uname, dir->gname, dir->perm, dir->fflags);
-
		if (developer_mode && ret != EPKG_OK)
+
		if (ctx.developer_mode && ret != EPKG_OK)
			return (ret);
		counter_count();
	}
@@ -382,7 +382,7 @@ pkg_load_metadata(struct pkg *pkg, const char *mfile, const char *md_dir,
	if (!testing)
		pkg_analyse_files(NULL, pkg, rootdir);

-
	if (developer_mode)
+
	if (ctx.developer_mode)
		pkg_suggest_arch(pkg, pkg->abi, defaultarch);

cleanup:
modified libpkg/pkg_elf.c
@@ -286,7 +286,7 @@ analyse_elf(struct pkg *pkg, const char *fpath)
		goto cleanup;
	}

-
	if (developer_mode)
+
	if (ctx.developer_mode)
		pkg->flags |= PKG_CONTAINS_ELF_OBJECTS;

	if (gelf_getehdr(e, &elfhdr) == NULL) {
@@ -477,7 +477,7 @@ pkg_analyse_files(struct pkgdb *db, struct pkg *pkg, const char *stage)
		goto cleanup;

	/* Assume no architecture dependence, for contradiction */
-
	if (developer_mode)
+
	if (ctx.developer_mode)
		pkg->flags &= ~(PKG_CONTAINS_ELF_OBJECTS |
				PKG_CONTAINS_STATIC_LIBS |
				PKG_CONTAINS_H_OR_LA);
@@ -489,7 +489,7 @@ pkg_analyse_files(struct pkgdb *db, struct pkg *pkg, const char *stage)
			strlcpy(fpath, file->path, sizeof(fpath));

		ret = analyse_elf(pkg, fpath);
-
		if (developer_mode) {
+
		if (ctx.developer_mode) {
			if (ret != EPKG_OK && ret != EPKG_END) {
				failures = true;
				continue;
modified libpkg/pkg_event.c
@@ -59,7 +59,7 @@ pipeevent(struct pkg_event *ev)
	struct pkg_dep *dep = NULL;
	UT_string *msg, *buf;
	struct pkg_event_conflict *cur_conflict;
-
	if (eventpipe < 0)
+
	if (ctx.eventpipe < 0)
		return;

	utstring_new(msg);
@@ -385,7 +385,7 @@ pipeevent(struct pkg_event *ev)
	default:
		break;
	}
-
	dprintf(eventpipe, "%s\n", utstring_body(msg));
+
	dprintf(ctx.eventpipe, "%s\n", utstring_body(msg));
	utstring_free(msg);
	utstring_free(buf);
}
@@ -953,7 +953,7 @@ pkg_debug(int level, const char *fmt, ...)
	struct pkg_event ev;
	va_list ap;

-
	if (debug_level < level)
+
	if (ctx.debug_level < level)
		return;

	ev.type = PKG_EVENT_DEBUG;
modified libpkg/pkg_ports.c
@@ -248,7 +248,7 @@ dir(struct plist *p, char *line, struct file_attr *a)
		pkg_emit_errno("lstat", testpath);
		if (p->stage != NULL)
			ret = EPKG_FATAL;
-
		if (developer_mode) {
+
		if (ctx.developer_mode) {
			pkg_emit_developer_mode("Plist error: @dirrm %s", line);
			ret = EPKG_FATAL;
		}
@@ -321,7 +321,7 @@ meta_file(struct plist *p, char *line, struct file_attr *a, bool is_config)
		pkg_errno("Unable to access file %s", testpath);
		if (p->stage != NULL)
			ret = EPKG_FATAL;
-
		if (developer_mode) {
+
		if (ctx.developer_mode) {
			pkg_emit_developer_mode("Plist error, missing file: %s",
			    line);
			ret = EPKG_FATAL;
@@ -492,7 +492,7 @@ static int
ignore_next(struct plist *p, __unused char *line, struct file_attr *a)
{
	p->ignore_next = true;
-
	if (developer_mode)
+
	if (ctx.developer_mode)
		pkg_emit_error("Warning: @ignore is deprecated");

	return (EPKG_OK);
@@ -1290,10 +1290,10 @@ pkg_add_port(struct pkgdb *db, struct pkg *pkg, const char *input_path,
	struct pkg_message *msg;

	location = reloc;
-
	if (pkg_rootdir != NULL)
-
		location = pkg_rootdir;
+
	if (ctx.pkg_rootdir != NULL)
+
		location = ctx.pkg_rootdir;

-
	if (pkg_rootdir == NULL && location != NULL)
+
	if (ctx.pkg_rootdir == NULL && location != NULL)
		pkg_kv_add(&pkg->annotations, "relocated", location, "annotation");

	pkg_emit_install_begin(pkg);
modified libpkg/pkg_solve.c
@@ -267,7 +267,7 @@ pkg_debug_print_rule(struct pkg_solve_rule *rule)
{
	UT_string *sb;

-
	if (debug_level < 3)
+
	if (ctx.debug_level < 3)
		return;

	utstring_new(sb);
modified libpkg/private/pkg.h
@@ -227,15 +227,18 @@
	}						\
} while (0)

+
struct pkg_ctx {
+
	int eventpipe;
+
	int64_t debug_level;
+
	bool developer_mode;
+
	const char *pkg_rootdir;
+
	int rootfd;
+
	int cachedirfd;
+
	int dbdirfd;
+
	int pkg_dbdirfd;
+
};

-

-
extern int eventpipe;
-
extern int64_t debug_level;
-
extern bool developer_mode;
-
extern const char *pkg_rootdir;
-
extern int rootfd;
-
extern int cachedirfd;
-
extern int dbdirfd;
+
extern struct pkg_ctx ctx;

struct pkg_repo_it;
struct pkg_repo;
modified libpkg/scripts.c
@@ -111,9 +111,9 @@ pkg_script_run(struct pkg * const pkg, pkg_script type)
		if (j == map[i].a || j == map[i].b) {
			utstring_clear(script_cmd);
			setenv("PKG_PREFIX", pkg->prefix, 1);
-
			if (pkg_rootdir == NULL)
-
				pkg_rootdir = "/";
-
			setenv("PKG_ROOTDIR", pkg_rootdir, 1);
+
			if (ctx.pkg_rootdir == NULL)
+
				ctx.pkg_rootdir = "/";
+
			setenv("PKG_ROOTDIR", ctx.pkg_rootdir, 1);
			debug = pkg_object_bool(pkg_config_get("DEBUG_SCRIPTS"));
			if (debug)
				utstring_printf(script_cmd, "set -x\n");