From 02367e3c006691c5d63ad93418c647bd56a1c6ca Mon Sep 17 00:00:00 2001 From: Duncan Overbruck Date: Thu, 24 Jun 2021 17:33:26 +0200 Subject: [PATCH] lib: run pre-remove and pre-install scripts before unpacking --- bin/xbps-install/state_cb.c | 1 + lib/package_remove.c | 13 +-------- lib/package_unpack.c | 10 ------- lib/transaction_commit.c | 56 ++++++++++++++++++++++++++++++++++++- 4 files changed, 57 insertions(+), 23 deletions(-) diff --git a/bin/xbps-install/state_cb.c b/bin/xbps-install/state_cb.c index a0a2f625..495ebb2b 100644 --- a/bin/xbps-install/state_cb.c +++ b/bin/xbps-install/state_cb.c @@ -165,6 +165,7 @@ state_cb(const struct xbps_state_cb_data *xscd, void *cbdata UNUSED) printf("%s\n", xscd->desc); break; /* errors */ + case XBPS_STATE_TRANS_FAIL: case XBPS_STATE_UNPACK_FAIL: case XBPS_STATE_UPDATE_FAIL: case XBPS_STATE_CONFIGURE_FAIL: diff --git a/lib/package_remove.c b/lib/package_remove.c index 8b6755a9..32e9e635 100644 --- a/lib/package_remove.c +++ b/lib/package_remove.c @@ -153,18 +153,7 @@ xbps_remove_pkg(struct xbps_handle *xhp, const char *pkgver, bool update) /* If package was "half-removed", remove it fully. */ if (state == XBPS_PKG_STATE_HALF_REMOVED) goto purge; - /* - * Run the pre remove action and show pre-remove message if exists. - */ - rv = xbps_pkg_exec_script(xhp, pkgd, "remove-script", "pre", update); - if (rv != 0) { - xbps_set_cb_state(xhp, XBPS_STATE_REMOVE_FAIL, - errno, pkgver, - "%s: [remove] REMOVE script failed to " - "execute pre ACTION: %s", - pkgver, strerror(rv)); - goto out; - } + /* show remove-msg if exists */ if ((rv = xbps_cb_message(xhp, pkgd, "remove-msg")) != 0) goto out; diff --git a/lib/package_unpack.c b/lib/package_unpack.c index 5453dd23..e030df87 100644 --- a/lib/package_unpack.c +++ b/lib/package_unpack.c @@ -198,16 +198,6 @@ unpack_archive(struct xbps_handle *xhp, */ pkg_filesd = xbps_pkgdb_get_pkg_files(xhp, pkgname); - /* - * Execute INSTALL "pre" ACTION before unpacking files. - */ - rv = xbps_pkg_exec_script(xhp, pkg_repod, "install-script", "pre", update); - if (rv != 0) { - xbps_set_cb_state(xhp, XBPS_STATE_UNPACK_FAIL, rv, pkgver, - "%s: [unpack] INSTALL script failed to execute pre ACTION: %s", - pkgver, strerror(rv)); - goto out; - } /* * Unpack all files on archive now. */ diff --git a/lib/transaction_commit.c b/lib/transaction_commit.c index 41353ae1..a1eb89f1 100644 --- a/lib/transaction_commit.c +++ b/lib/transaction_commit.c @@ -59,10 +59,11 @@ int xbps_transaction_commit(struct xbps_handle *xhp) { + xbps_dictionary_t pkgdb_pkgd; xbps_object_t obj; xbps_object_iterator_t iter; xbps_trans_type_t ttype; - const char *pkgver = NULL; + const char *pkgver = NULL, *pkgname = NULL; int rv = 0; bool update; @@ -155,6 +156,59 @@ xbps_transaction_commit(struct xbps_handle *xhp) xhp->rootdir, strerror(errno)); goto out; } + + /* + * Run all pre-remove scripts. + */ + while ((obj = xbps_object_iterator_next(iter)) != NULL) { + xbps_dictionary_get_cstring_nocopy(obj, "pkgver", &pkgver); + xbps_dictionary_get_cstring_nocopy(obj, "pkgname", &pkgname); + + ttype = xbps_transaction_pkg_type(obj); + if (ttype == XBPS_TRANS_INSTALL || ttype == XBPS_TRANS_HOLD || ttype == XBPS_TRANS_CONFIGURE) { + xbps_dbg_printf(xhp, "%s: skipping pre-remove script for " + "%s: %d\n", __func__, pkgver, ttype); + continue; + } + if ((pkgdb_pkgd = xbps_pkgdb_get_pkg(xhp, pkgname)) == NULL) { + rv = errno; + xbps_dbg_printf(xhp, "[trans] cannot find %s in pkgdb: %s\n", + pkgver, strerror(rv)); + goto out; + } + update = ttype == XBPS_TRANS_UPDATE; + rv = xbps_pkg_exec_script(xhp, pkgdb_pkgd, "remove-script", "pre", update); + if (rv != 0) { + xbps_set_cb_state(xhp, XBPS_STATE_TRANS_FAIL, rv, pkgver, + "%s: [trans] REMOVE script failed to execute pre ACTION: %s", + pkgver, strerror(rv)); + goto out; + } + } + xbps_object_iterator_reset(iter); + + /* + * Run all pre-install scripts. + */ + while ((obj = xbps_object_iterator_next(iter)) != NULL) { + xbps_dictionary_get_cstring_nocopy(obj, "pkgver", &pkgver); + ttype = xbps_transaction_pkg_type(obj); + if (ttype == XBPS_TRANS_REMOVE || ttype == XBPS_TRANS_HOLD) { + xbps_dbg_printf(xhp, "%s: skipping pre-install script for " + "%s: %d\n", __func__, pkgver, ttype); + continue; + } + rv = xbps_pkg_exec_script(xhp, obj, "install-script", "pre", ttype == XBPS_TRANS_UPDATE); + if (rv != 0) { + xbps_set_cb_state(xhp, XBPS_STATE_TRANS_FAIL, rv, pkgver, + "%s: [trans] INSTALL script failed to execute pre ACTION: %s", + pkgver, strerror(rv)); + goto out; + } + } + xbps_object_iterator_reset(iter); + + while ((obj = xbps_object_iterator_next(iter)) != NULL) { xbps_dictionary_get_cstring_nocopy(obj, "pkgver", &pkgver);