From a67374bd667ee2aef64a89d9548110f3ea648185 Mon Sep 17 00:00:00 2001
From: Juan RP <xtraeme@gmail.com>
Date: Tue, 17 Mar 2009 00:32:26 +0100
Subject: [PATCH] Simplify all code that used rootdir and flags passed via
 xbps-bin.

Add util funcs to get/set global values for them:
 * xbps_[gs]et_rootdir()
 * xbps_[gs]et_flags()

--HG--
extra : convert_revision : 2584b098e68a684e7b0eeac57c2b1df4aba6589d
---
 bin/xbps-bin/main.c  | 18 ++++++-----
 bin/xbps-repo/util.c |  3 +-
 bin/xbps-repo/util.h |  2 +-
 include/install.h    | 10 +++---
 include/remove.h     |  2 +-
 include/util.h       |  3 ++
 include/xbps_api.h   |  4 +--
 lib/depends.c        |  4 +--
 lib/install.c        | 38 +++++-----------------
 lib/remove.c         | 77 ++++++++++++++++++++++----------------------
 lib/unpack.c         | 42 +++++++++++++-----------
 lib/util.c           | 19 +++++++++++
 12 files changed, 114 insertions(+), 108 deletions(-)

diff --git a/bin/xbps-bin/main.c b/bin/xbps-bin/main.c
index e4d8d9ec707..9f2b5755baa 100644
--- a/bin/xbps-bin/main.c
+++ b/bin/xbps-bin/main.c
@@ -149,7 +149,7 @@ main(int argc, char **argv)
 	prop_object_iterator_t iter;
 	static size_t count;
 	const char *pkgname, *version;
-	char *plist, *root = NULL;
+	char *plist;
 	int c, flags = 0, rv = 0;
 	bool chkhash = false, forcerm = false, verbose = false;
 
@@ -163,12 +163,11 @@ main(int argc, char **argv)
 			break;
 		case 'r':
 			/* To specify the root directory */
-			root = optarg;
-			xbps_set_rootdir(root);
+			xbps_set_rootdir(optarg);
 			break;
 		case 'v':
 			verbose = true;
-			flags |= XBPS_UNPACK_VERBOSE;
+			flags |= XBPS_VERBOSE;
 			break;
 		case '?':
 		default:
@@ -182,6 +181,9 @@ main(int argc, char **argv)
 	if (argc < 1)
 		usage();
 
+	if (flags != 0)
+		xbps_set_flags(flags);
+
 	if (strcasecmp(argv[0], "list") == 0) {
 		/* Lists packages currently registered in database. */
 		if (argc != 1)
@@ -213,7 +215,7 @@ main(int argc, char **argv)
 			usage();
 
 		/* Install into root directory by default. */
-		rv = xbps_install_binary_pkg(argv[1], root, flags);
+		rv = xbps_install_binary_pkg(argv[1]);
 		if (rv != 0) {
 			if (rv == EAGAIN) {
 				printf("Unable to locate %s in "
@@ -265,7 +267,7 @@ main(int argc, char **argv)
 
 		(void)fflush(stdout);
 
-		rv = xbps_remove_binary_pkg(argv[1], root, flags);
+		rv = xbps_remove_binary_pkg(argv[1]);
 		if (rv != 0) {
 			if (!verbose)
 				printf("failed! (%s)\n", strerror(rv));
@@ -300,7 +302,7 @@ main(int argc, char **argv)
 		if (argc != 2)
 			usage();
 
-		rv = show_pkg_files_from_metadir(argv[1], root, chkhash);
+		rv = show_pkg_files_from_metadir(argv[1], chkhash);
 		if (rv != 0) {
 			printf("Package %s not installed.\n", argv[1]);
 			exit(EXIT_FAILURE);
@@ -364,7 +366,7 @@ main(int argc, char **argv)
 
 			(void)fflush(stdout);
 
-			rv = xbps_remove_binary_pkg(pkgname, root, flags);
+			rv = xbps_remove_binary_pkg(pkgname);
 			if (rv != 0) {
 				if (!verbose)
 					printf("failed! (%s)\n", strerror(rv));
diff --git a/bin/xbps-repo/util.c b/bin/xbps-repo/util.c
index ec042396156..cf366470510 100644
--- a/bin/xbps-repo/util.c
+++ b/bin/xbps-repo/util.c
@@ -212,11 +212,12 @@ show_pkg_info_from_metadir(const char *pkgname)
 }
 
 int
-show_pkg_files_from_metadir(const char *pkgname, const char *destdir, bool hash)
+show_pkg_files_from_metadir(const char *pkgname, bool hash)
 {
 	prop_dictionary_t pkgd;
 	struct show_files_cb sfc;
 	size_t len = 0;
+	const char *destdir = xbps_get_rootdir();
 	char *plist, *path;
 	int rv = 0;
 
diff --git a/bin/xbps-repo/util.h b/bin/xbps-repo/util.h
index f88252900d4..cac86095c5b 100644
--- a/bin/xbps-repo/util.h
+++ b/bin/xbps-repo/util.h
@@ -28,7 +28,7 @@
 
 int	search_string_in_pkgs(prop_object_t, void *, bool *);
 int	show_pkg_info_from_metadir(const char *);
-int	show_pkg_files_from_metadir(const char *, const char *, bool);
+int	show_pkg_files_from_metadir(const char *, bool);
 int	show_pkg_info_from_repolist(prop_object_t, void *, bool *);
 int	list_strings_in_array(prop_object_t, void *, bool *);
 
diff --git a/include/install.h b/include/install.h
index 776e1168e82..a59d60a72b4 100644
--- a/include/install.h
+++ b/include/install.h
@@ -27,14 +27,12 @@
 #define _XBPS_INSTALL_H_
 
 /* From lib/install.c, lib/depends.c and lib/unpack.c */
-int	xbps_install_pkg_deps(const char *, const char *, int);
-int	xbps_install_binary_pkg(const char *, const char *, int);
-int	xbps_install_binary_pkg_fini(prop_dictionary_t, prop_dictionary_t,
-				     const char *, int);
+int	xbps_install_pkg_deps(const char *);
+int	xbps_install_binary_pkg(const char *);
+int	xbps_install_binary_pkg_fini(prop_dictionary_t, prop_dictionary_t);
 int	xbps_register_pkg(prop_dictionary_t, const char *, const char *,
 			  const char *, bool);
-int	xbps_unpack_binary_pkg(prop_dictionary_t, prop_dictionary_t,
-			       const char *, int);
+int	xbps_unpack_binary_pkg(prop_dictionary_t, prop_dictionary_t);
 int	xbps_requiredby_pkg_add(prop_array_t, prop_dictionary_t);
 int	xbps_requiredby_pkg_remove(const char *);
 int	xbps_find_deps_in_pkg(prop_dictionary_t);
diff --git a/include/remove.h b/include/remove.h
index 358a45fa687..47ea064d2c0 100644
--- a/include/remove.h
+++ b/include/remove.h
@@ -27,7 +27,7 @@
 #define _XBPS_REMOVE_H_
 
 /* From lib/remove.c */
-int	xbps_remove_binary_pkg(const char *, const char *, int);
+int	xbps_remove_binary_pkg(const char *);
 int	xbps_unregister_pkg(const char *);
 
 #endif /* !_XBPS_REMOVE_H_ */
diff --git a/include/util.h b/include/util.h
index 626823d8233..894e454cee3 100644
--- a/include/util.h
+++ b/include/util.h
@@ -37,6 +37,9 @@ char *		xbps_get_pkg_name(const char *);
 const char *	xbps_get_pkg_version(const char *);
 bool		xbps_pkg_has_rundeps(prop_dictionary_t);
 void		xbps_set_rootdir(const char *);
+const char *	xbps_get_rootdir(void);
+void		xbps_set_flags(int);
+int		xbps_get_flags(void);
 
 /* From lib/orphans.c */
 prop_array_t	xbps_find_orphan_packages(void);
diff --git a/include/xbps_api.h b/include/xbps_api.h
index 23b7e430127..c032970aa65 100644
--- a/include/xbps_api.h
+++ b/include/xbps_api.h
@@ -51,8 +51,8 @@
 #define XBPS_PKGPROPS		"props.plist"
 #define XBPS_PKGFILES		"files.plist"
 
-/* Unpack flags */
-#define XBPS_UNPACK_VERBOSE	0x00000001
+/* Verbose messages */
+#define XBPS_VERBOSE		0x00000001
 
 #include "cmpver.h"
 #include "fexec.h"
diff --git a/lib/depends.c b/lib/depends.c
index 75f0f5af480..46b36764a5d 100644
--- a/lib/depends.c
+++ b/lib/depends.c
@@ -594,7 +594,7 @@ find_pkg_deps_from_repo(prop_dictionary_t repo, prop_dictionary_t pkg,
 }
 
 int
-xbps_install_pkg_deps(const char *pkgname, const char *destdir, int flags)
+xbps_install_pkg_deps(const char *pkgname)
 {
 	prop_array_t required, missing;
 	prop_object_t obj;
@@ -644,7 +644,7 @@ xbps_install_pkg_deps(const char *pkgname, const char *destdir, int flags)
 	 * Install all required dependencies, previously sorted.
 	 */
 	while ((obj = prop_object_iterator_next(iter)) != NULL) {
-		rv = xbps_install_binary_pkg_fini(NULL, obj, destdir, flags);
+		rv = xbps_install_binary_pkg_fini(NULL, obj);
 		if (rv != 0)
 			break;
 	}
diff --git a/lib/install.c b/lib/install.c
index 2d6a033289e..71260b26084 100644
--- a/lib/install.c
+++ b/lib/install.c
@@ -34,17 +34,10 @@
 
 #include <xbps_api.h>
 
-struct cbargs {
-	const char *destdir;
-	const char *pkgname;
-	int flags;
-};
-
 static int	install_binpkg_repo_cb(prop_object_t, void *, bool *);
 
 int
-xbps_install_binary_pkg_fini(prop_dictionary_t repo, prop_dictionary_t pkg,
-			     const char *destdir, int flags)
+xbps_install_binary_pkg_fini(prop_dictionary_t repo, prop_dictionary_t pkg)
 {
 	const char *pkgname, *version, *desc;
 	int rv = 0;
@@ -62,7 +55,7 @@ xbps_install_binary_pkg_fini(prop_dictionary_t repo, prop_dictionary_t pkg,
 	    automatic ? "dependency " : "", pkgname, version);
 	(void)fflush(stdout);
 
-	rv = xbps_unpack_binary_pkg(repo, pkg, destdir, flags);
+	rv = xbps_unpack_binary_pkg(repo, pkg);
 	if (rv == 0) {
 		rv = xbps_register_pkg(pkg, pkgname, version, desc, automatic);
 		if (rv != 0) {
@@ -76,30 +69,17 @@ xbps_install_binary_pkg_fini(prop_dictionary_t repo, prop_dictionary_t pkg,
 }
 
 int
-xbps_install_binary_pkg(const char *pkgname, const char *destdir, int flags)
+xbps_install_binary_pkg(const char *pkgname)
 {
-	struct cbargs cb;
 	int rv = 0;
 
 	assert(pkgname != NULL);
-	if (destdir) {
-		if (chdir(destdir) == -1)
-			return errno;
-	} else {
-		if (chdir("/") == -1)
-			return errno;
-		destdir = "NOTSET";
-	}
-
-	cb.pkgname = pkgname;
-	cb.destdir = destdir;
-	cb.flags = flags;
 	/*
 	 * Iterate over the repository pool and find out if we have
 	 * all available binary packages.
 	 */
 	rv = xbps_callback_array_iter_in_repolist(install_binpkg_repo_cb,
-	    (void *)&cb);
+	    (void *)pkgname);
 	if (rv == 0 && errno != 0)
 		return errno;
 
@@ -110,8 +90,7 @@ static int
 install_binpkg_repo_cb(prop_object_t obj, void *arg, bool *cbloop_done)
 {
 	prop_dictionary_t repod, pkgrd;
-	struct cbargs *cb = arg;
-	const char *repoloc;
+	const char *repoloc, *pkgname = arg;
 	char *plist;
 	int rv = 0;
 
@@ -130,7 +109,7 @@ install_binpkg_repo_cb(prop_object_t obj, void *arg, bool *cbloop_done)
 	 * Get the package dictionary from current repository.
 	 * If it's not there, pass to the next repository.
 	 */
-	pkgrd = xbps_find_pkg_in_dict(repod, "packages", cb->pkgname);
+	pkgrd = xbps_find_pkg_in_dict(repod, "packages", pkgname);
 	if (pkgrd == NULL) {
 		prop_object_release(repod);
 		errno = EAGAIN;
@@ -172,13 +151,12 @@ install_binpkg_repo_cb(prop_object_t obj, void *arg, bool *cbloop_done)
 	/*
 	 * Install all required dependencies and the package itself.
 	 */
-	rv = xbps_install_pkg_deps(cb->pkgname, cb->destdir, cb->flags);
+	rv = xbps_install_pkg_deps(pkgname);
 	if (rv != 0)
 		goto out;
 
 install:
-	rv = xbps_install_binary_pkg_fini(repod, pkgrd,
-	    cb->destdir, cb->flags);
+	rv = xbps_install_binary_pkg_fini(repod, pkgrd);
 	if (rv == 0) {
 		*cbloop_done = true;
 		/* Cleanup errno, just in case */
diff --git a/lib/remove.c b/lib/remove.c
index 14d690e8f67..82e7a8da11e 100644
--- a/lib/remove.c
+++ b/lib/remove.c
@@ -36,11 +36,6 @@
 
 #include <xbps_api.h>
 
-struct rm_cbarg {
-	const char *destdir;
-	int flags;
-};
-
 static int	remove_pkg_files(prop_object_t, void *, bool *);
 
 int
@@ -64,20 +59,22 @@ xbps_unregister_pkg(const char *pkgname)
 }
 
 static int
-xbps_remove_binary_pkg_meta(const char *pkgname, const char *destdir, int flags)
+xbps_remove_binary_pkg_meta(const char *pkgname)
 {
 	struct dirent *dp;
 	DIR *dirp;
+	const char *rootdir = xbps_get_rootdir();
 	char metadir[PATH_MAX - 1], path[PATH_MAX - 1];
-	int rv = 0;
+	int flags = 0, rv = 0;
 
 	assert(pkgname != NULL);
-
-	if (destdir == NULL)
-		destdir = "";
+	rootdir = xbps_get_rootdir();
+	if (rootdir == NULL)
+		rootdir = "";
+	flags = xbps_get_flags();
 
 	(void)snprintf(metadir, sizeof(metadir), "%s%s/metadata/%s",
-	    destdir, XBPS_META_PATH, pkgname);
+	    rootdir, XBPS_META_PATH, pkgname);
 
 	dirp = opendir(metadir);
 	if (dirp == NULL)
@@ -89,13 +86,13 @@ xbps_remove_binary_pkg_meta(const char *pkgname, const char *destdir, int flags)
 			continue;
 
 		if (snprintf(path, sizeof(path), "%s%s/metadata/%s/%s",
-		    destdir, XBPS_META_PATH, pkgname, dp->d_name) < 0) {
+		    rootdir, XBPS_META_PATH, pkgname, dp->d_name) < 0) {
 			(void)closedir(dirp);
 			return -1;
 		}
 
 		if ((rv = unlink(path)) == -1) {
-			if (flags & XBPS_UNPACK_VERBOSE)
+			if (flags & XBPS_VERBOSE)
 				printf("WARNING: can't remove %s (%s)\n",
 				    pkgname, strerror(errno));
 		}
@@ -111,17 +108,22 @@ static int
 remove_pkg_files(prop_object_t obj, void *arg, bool *loop_done)
 {
 	prop_bool_t bobj;
-	struct rm_cbarg *rmcb = arg;
-	const char *file = NULL, *sha256, *type;
+	const char *file = NULL, *rootdir, *sha256, *type;
 	char *path = NULL;
-	int rv = 0;
+	int flags = 0, rv = 0;
 
+	(void)arg;
 	(void)loop_done;
 
+	rootdir = xbps_get_rootdir();
+	if (rootdir == NULL)
+		rootdir = "";
+	flags = xbps_get_flags();
+
 	if (!prop_dictionary_get_cstring_nocopy(obj, "file", &file))
 		return EINVAL;
 
-	path = xbps_append_full_path(false, rmcb->destdir, file);
+	path = xbps_append_full_path(false, rootdir, file);
 	if (path == NULL)
 		return EINVAL;
 
@@ -150,19 +152,19 @@ remove_pkg_files(prop_object_t obj, void *arg, bool *loop_done)
 		}
 
 		if (rv == ERANGE) {
-			if (rmcb->flags & XBPS_UNPACK_VERBOSE)
+			if (flags & XBPS_VERBOSE)
 				printf("WARNING: SHA256 doesn't match for "
 				    "file %s, ignoring...\n", file);
 			goto out;
 		}
 
 		if ((rv = remove(path)) == -1) {
-			if (rmcb->flags & XBPS_UNPACK_VERBOSE)
+			if (flags & XBPS_VERBOSE)
 				printf("WARNING: can't remove file %s (%s)\n",
 				    file, strerror(errno));
 			goto out;
 		}
-		if (rmcb->flags & XBPS_UNPACK_VERBOSE)
+		if (flags & XBPS_VERBOSE)
 			printf("Removed file: %s\n", file);
 
 		goto out;
@@ -176,23 +178,23 @@ remove_pkg_files(prop_object_t obj, void *arg, bool *loop_done)
 			if (errno == ENOTEMPTY)
 				goto out;
 
-			if (rmcb->flags & XBPS_UNPACK_VERBOSE) {
+			if (flags & XBPS_VERBOSE) {
 				printf("WARNING: can't remove "
 				    "directory %s (%s)\n", file,
 				    strerror(errno));
 				goto out;
 			}
-			if (rmcb->flags & XBPS_UNPACK_VERBOSE)
+			if (flags & XBPS_VERBOSE)
 				printf("Removed directory: %s\n", file);
 		}
 	} else if (strcmp(type, "link") == 0) {
 		if ((rv = remove(path)) == -1) {
-			if (rmcb->flags & XBPS_UNPACK_VERBOSE)
+			if (flags & XBPS_VERBOSE)
 				printf("WARNING: can't remove link %s (%s)\n",
 				    file, strerror(errno));
 			goto out;
 		}
-		if (rmcb->flags & XBPS_UNPACK_VERBOSE)
+		if (flags & XBPS_VERBOSE)
 			printf("Removed link: %s\n", file);
 	}
 
@@ -203,10 +205,10 @@ out:
 }
 
 int
-xbps_remove_binary_pkg(const char *pkgname, const char *destdir, int flags)
+xbps_remove_binary_pkg(const char *pkgname)
 {
 	prop_dictionary_t dict;
-	struct rm_cbarg rmcbarg;
+	const char *rootdir = xbps_get_rootdir();
 	char path[PATH_MAX - 1], *buf;
 	int fd, rv = 0;
 	size_t len = 0;
@@ -214,13 +216,13 @@ xbps_remove_binary_pkg(const char *pkgname, const char *destdir, int flags)
 
 	assert(pkgname != NULL);
 
-	if (destdir) {
-		if (chdir(destdir) == -1)
+	if (rootdir) {
+		if (chdir(rootdir) == -1)
 			return errno;
 	} else {
 		if (chdir("/") == -1)
 			return errno;
-		destdir = "";
+		rootdir = "";
         }
 
 	/* Check if pkg is installed */
@@ -230,13 +232,13 @@ xbps_remove_binary_pkg(const char *pkgname, const char *destdir, int flags)
 	/*
 	 * This length is '%s%s/metadata/%s/REMOVE' + NULL.
 	 */
-	len = strlen(XBPS_META_PATH) + strlen(destdir) + strlen(pkgname) + 19;
+	len = strlen(XBPS_META_PATH) + strlen(rootdir) + strlen(pkgname) + 19;
 	buf = malloc(len);
 	if (buf == NULL)
 		return errno;
 
 	if (snprintf(buf, len, "%s%s/metadata/%s/REMOVE",
-	    destdir, XBPS_META_PATH, pkgname) < 0) {
+	    rootdir, XBPS_META_PATH, pkgname) < 0) {
 		free(buf);
 		return -1;
 	}
@@ -257,7 +259,7 @@ xbps_remove_binary_pkg(const char *pkgname, const char *destdir, int flags)
 		prepostf = true;
 		(void)printf("\n");
 		(void)fflush(stdout);
-		rv = xbps_file_exec(buf, destdir, "pre", pkgname, NULL);
+		rv = xbps_file_exec(buf, rootdir, "pre", pkgname, NULL);
 		if (rv != 0) {
 			printf("%s: prerm action target error (%s)\n", pkgname,
 			    strerror(errno));
@@ -271,7 +273,7 @@ xbps_remove_binary_pkg(const char *pkgname, const char *destdir, int flags)
 	 * files/dirs associated.
 	 */
 	(void)snprintf(path, sizeof(path), "%s%s/metadata/%s/files.plist",
-	    destdir, XBPS_META_PATH, pkgname);
+	    rootdir, XBPS_META_PATH, pkgname);
 
 	dict = prop_dictionary_internalize_from_file(path);
 	if (dict == NULL) {
@@ -279,11 +281,8 @@ xbps_remove_binary_pkg(const char *pkgname, const char *destdir, int flags)
 		return errno;
 	}
 
-	rmcbarg.destdir = destdir;
-	rmcbarg.flags = flags;
-
 	rv = xbps_callback_array_iter_in_dict(dict, "filelist",
-	    remove_pkg_files, (void *)&rmcbarg);
+	    remove_pkg_files, NULL);
 	if (rv != 0) {
 		free(buf);
 		prop_object_release(dict);
@@ -295,7 +294,7 @@ xbps_remove_binary_pkg(const char *pkgname, const char *destdir, int flags)
 	 * Run the post remove action if REMOVE file is there.
 	 */
 	if (prepostf) {
-		if ((rv = xbps_file_exec(buf, destdir, "post",
+		if ((rv = xbps_file_exec(buf, rootdir, "post",
 		     pkgname, NULL)) != 0) {
 			printf("%s: postrm action target error (%s)\n",
 			    pkgname, strerror(errno));
@@ -322,5 +321,5 @@ xbps_remove_binary_pkg(const char *pkgname, const char *destdir, int flags)
 	/*
 	 * Remove pkg metadata directory.
 	 */
-	return xbps_remove_binary_pkg_meta(pkgname, destdir, flags);
+	return xbps_remove_binary_pkg_meta(pkgname);
 }
diff --git a/lib/unpack.c b/lib/unpack.c
index 8f74449a664..e5a53f03292 100644
--- a/lib/unpack.c
+++ b/lib/unpack.c
@@ -35,14 +35,11 @@
 
 #include <xbps_api.h>
 
-static int unpack_archive_init(prop_dictionary_t, const char *,
-			       const char *, int);
-static int unpack_archive_fini(struct archive *, const char *, int,
-			       prop_dictionary_t);
+static int unpack_archive_init(prop_dictionary_t, const char *);
+static int unpack_archive_fini(struct archive *, prop_dictionary_t);
 
 int
-xbps_unpack_binary_pkg(prop_dictionary_t repo, prop_dictionary_t pkg,
-		       const char *destdir, int flags)
+xbps_unpack_binary_pkg(prop_dictionary_t repo, prop_dictionary_t pkg)
 {
 	prop_string_t filename, repoloc, arch;
 	char *binfile, *path;
@@ -72,15 +69,14 @@ xbps_unpack_binary_pkg(prop_dictionary_t repo, prop_dictionary_t pkg,
 	}
 	free(path);
 
-	rv = unpack_archive_init(pkg, destdir, binfile, flags);
+	rv = unpack_archive_init(pkg, binfile);
 	free(binfile);
 	return rv;
 }
 
 
 static int
-unpack_archive_init(prop_dictionary_t pkg, const char *destdir,
-		    const char *binfile, int flags)
+unpack_archive_init(prop_dictionary_t pkg, const char *binfile)
 {
 	struct archive *ar;
 	int pkg_fd, rv;
@@ -108,7 +104,7 @@ unpack_archive_init(prop_dictionary_t pkg, const char *destdir,
 		return rv;
 	}
 
-	rv = unpack_archive_fini(ar, destdir, flags, pkg);
+	rv = unpack_archive_fini(ar, pkg);
 	/*
 	 * If installation of package was successful, make sure the package
 	 * is really on storage (if possible).
@@ -137,19 +133,29 @@ unpack_archive_init(prop_dictionary_t pkg, const char *destdir,
  * the consumer.
  */
 static int
-unpack_archive_fini(struct archive *ar, const char *destdir, int flags,
-		    prop_dictionary_t pkg)
+unpack_archive_fini(struct archive *ar, prop_dictionary_t pkg)
 {
 	struct archive_entry *entry;
 	size_t len;
 	const char *prepost = "./INSTALL";
-	const char *pkgname, *version;
+	const char *pkgname, *version, *rootdir;
 	char *buf;
-	int rv = 0, lflags = 0;
+	int rv = 0, flags = 0, lflags = 0;
 	bool actgt = false;
 
 	assert(ar != NULL);
 	assert(pkg != NULL);
+	rootdir = xbps_get_rootdir();
+	flags = xbps_get_flags();
+
+	if (rootdir) {
+		if (chdir(rootdir) == -1)
+			return errno;
+	} else {
+		if (chdir("/") == -1)
+			return errno;
+		rootdir = "";
+	}
 
 	prop_dictionary_get_cstring_nocopy(pkg, "pkgname", &pkgname);
 	prop_dictionary_get_cstring_nocopy(pkg, "version", &version);
@@ -189,7 +195,7 @@ unpack_archive_fini(struct archive *ar, const char *destdir, int flags,
 					break;
 			}
 
-			if ((rv = xbps_file_exec(buf, destdir, "pre",
+			if ((rv = xbps_file_exec(buf, rootdir, "pre",
 			     pkgname, version, NULL)) != 0) {
 				printf("%s: preinst action target error %s\n",
 				    pkgname, strerror(errno));
@@ -212,7 +218,7 @@ unpack_archive_fini(struct archive *ar, const char *destdir, int flags,
 				(void)fflush(stdout);
 				break;
 			} else if (rv == EEXIST) {
-				if (flags & XBPS_UNPACK_VERBOSE) {
+				if (flags & XBPS_VERBOSE) {
 					printf("WARNING: ignoring existent "
 					    "path: %s\n",
 					    archive_entry_pathname(entry));
@@ -222,7 +228,7 @@ unpack_archive_fini(struct archive *ar, const char *destdir, int flags,
 				continue;
 			}
 		}
-		if (flags & XBPS_UNPACK_VERBOSE) {
+		if (flags & XBPS_VERBOSE) {
 			printf(" %s\n", archive_entry_pathname(entry));
 			(void)fflush(stdout);
 		}
@@ -233,7 +239,7 @@ unpack_archive_fini(struct archive *ar, const char *destdir, int flags,
 		 * Run the post installaction action target, if package
 		 * contains the script.
 		 */
-		if ((rv = xbps_file_exec(buf, destdir, "post",
+		if ((rv = xbps_file_exec(buf, rootdir, "post",
 		     pkgname, version, NULL)) != 0) {
 			printf("%s: postinst action target error %s\n",
 			    pkgname, strerror(errno));
diff --git a/lib/util.c b/lib/util.c
index d549a6756eb..552432a290d 100644
--- a/lib/util.c
+++ b/lib/util.c
@@ -35,6 +35,7 @@
 #include <xbps_api.h>
 
 static const char *rootdir;
+static int flags;
 
 int
 xbps_check_file_hash(const char *path, const char *sha256)
@@ -238,6 +239,24 @@ xbps_set_rootdir(const char *dir)
 	rootdir = dir;
 }
 
+const char *
+xbps_get_rootdir(void)
+{
+	return rootdir;
+}
+
+void
+xbps_set_flags(int lflags)
+{
+	flags = lflags;
+}
+
+int
+xbps_get_flags(void)
+{
+	return flags;
+}
+
 char *
 xbps_append_full_path(bool use_rootdir, const char *basedir, const char *plist)
 {