diff --git a/include/xbps_api_impl.h b/include/xbps_api_impl.h index 85a16206..9838e3c3 100644 --- a/include/xbps_api_impl.h +++ b/include/xbps_api_impl.h @@ -141,6 +141,12 @@ char HIDDEN *xbps_repository_pkg_path(struct xbps_handle *, xbps_dictionary_t); void HIDDEN xbps_fetch_set_cache_connection(int, int); void HIDDEN xbps_fetch_unset_cache_connection(void); +/** + * @private + * From lib/package_msg.c + */ +int HIDDEN xbps_cb_message(struct xbps_handle *, xbps_dictionary_t, const char *); + /** * @private * From lib/package_config_files.c diff --git a/lib/Makefile b/lib/Makefile index 8fb1be89..3a3dd8d1 100644 --- a/lib/Makefile +++ b/lib/Makefile @@ -36,6 +36,7 @@ EXTOBJS = external/dewey.o external/fexec.o external/mkpath.o OBJS = package_configure.o package_config_files.o package_orphans.o OBJS += package_remove.o package_find_obsoletes.o package_state.o OBJS += package_unpack.o package_register.o package_script.o verifysig.o +OBJS += package_msg.o OBJS += transaction_commit.o transaction_package_replace.o OBJS += transaction_dictionary.o transaction_sortdeps.o transaction_ops.o OBJS += transaction_revdeps.o pubkey2fp.o diff --git a/lib/package_configure.c b/lib/package_configure.c index 82c18be1..ef841c5b 100644 --- a/lib/package_configure.c +++ b/lib/package_configure.c @@ -81,11 +81,7 @@ xbps_configure_pkg(struct xbps_handle *xhp, bool update) { xbps_dictionary_t pkgd, pkgmetad; - xbps_data_t imsg; - FILE *f = NULL; - const void *data = NULL; - size_t len; - char *buf = NULL, *pkgname, *plist; + char *pkgname, *plist; int rv = 0; pkg_state_t state = 0; @@ -169,35 +165,8 @@ xbps_configure_pkg(struct xbps_handle *xhp, xbps_set_cb_state(xhp, XBPS_STATE_CONFIGURE_DONE, 0, pkgver, NULL); /* show install-msg if exists */ - imsg = xbps_dictionary_get(pkgmetad, "install-msg"); - if (xbps_object_type(imsg) != XBPS_TYPE_DATA) - goto out; + rv = xbps_cb_message(xhp, pkgmetad, "install-msg"); - data = xbps_data_data_nocopy(imsg); - len = xbps_data_size(imsg); - if ((f = fmemopen(__UNCONST(data), len, "r")) == NULL) { - rv = errno; - xbps_dbg_printf(xhp, "[configure] %s: fmemopen %s\n", pkgver, strerror(rv)); - goto out; - }; - buf = malloc(len); - assert(buf); - if (fread(buf, len, 1, f) != len) { - if (ferror(f)) { - rv = errno; - xbps_dbg_printf(xhp, "[configure] %s: fread %s\n", pkgver, strerror(rv)); - fclose(f); - free(buf); - goto out; - } - } - /* terminate buffer and notify client to show the post-install message */ - buf[len] = '\0'; - xbps_set_cb_state(xhp, XBPS_STATE_SHOW_INSTALL_MSG, 0, pkgver, "%s", buf); - fclose(f); - free(buf); - -out: if (pkgmetad != NULL) xbps_object_release(pkgmetad); diff --git a/lib/package_msg.c b/lib/package_msg.c new file mode 100644 index 00000000..f71dba06 --- /dev/null +++ b/lib/package_msg.c @@ -0,0 +1,86 @@ +/*- + * Copyright (c) 2014 Juan Romero Pardines. + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR + * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES + * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. + * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, + * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT + * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, + * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY + * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF + * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +#include +#include +#include +#include + +#include "xbps_api_impl.h" + +int HIDDEN +xbps_cb_message(struct xbps_handle *xhp, xbps_dictionary_t pkgd, const char *key) +{ + xbps_data_t msg; + FILE *f = NULL; + const void *data = NULL; + const char *pkgver; + size_t len; + char *buf = NULL; + int rv = 0; + + assert(xhp); + assert(pkgd); + assert(key); + + xbps_dictionary_get_cstring_nocopy(pkgd, "pkgver", &pkgver); + + /* show install-msg if exists */ + msg = xbps_dictionary_get(pkgd, key); + if (xbps_object_type(msg) != XBPS_TYPE_DATA) + goto out; + + data = xbps_data_data_nocopy(msg); + len = xbps_data_size(msg); + if ((f = fmemopen(__UNCONST(data), len, "r")) == NULL) { + rv = errno; + xbps_dbg_printf(xhp, "[%s] %s: fmemopen %s\n", __func__, pkgver, strerror(rv)); + goto out; + }; + buf = malloc(len); + assert(buf); + if (fread(buf, len, 1, f) != len) { + if (ferror(f)) { + rv = errno; + xbps_dbg_printf(xhp, "[%s] %s: fread %s\n", __func__, pkgver, strerror(rv)); + goto out; + } + } + /* terminate buffer and notify client to show the post-install message */ + buf[len] = '\0'; + + if (strcmp(key, "install-msg") == 0) + xbps_set_cb_state(xhp, XBPS_STATE_SHOW_INSTALL_MSG, 0, pkgver, "%s", buf); + else + xbps_set_cb_state(xhp, XBPS_STATE_SHOW_REMOVE_MSG, 0, pkgver, "%s", buf); + +out: + if (f != NULL) + fclose(f); + if (buf != NULL) + free(buf); + + return rv; +} diff --git a/lib/package_remove.c b/lib/package_remove.c index 14531564..b57108fa 100644 --- a/lib/package_remove.c +++ b/lib/package_remove.c @@ -259,8 +259,6 @@ xbps_remove_pkg(struct xbps_handle *xhp, const char *pkgver, bool update) * Run the pre remove action and show pre-remove message if exists. */ if (pkgd) { - xbps_data_t rmsg; - rv = xbps_pkg_exec_script(xhp, pkgd, "remove-script", "pre", update); if (rv != 0) { @@ -272,37 +270,9 @@ xbps_remove_pkg(struct xbps_handle *xhp, const char *pkgver, bool update) goto out; } /* show remove-msg if exists */ - rmsg = xbps_dictionary_get(pkgd, "remove-msg"); - if (xbps_object_type(rmsg) == XBPS_TYPE_DATA) { - FILE *f; - const void *data; - char *buf; - size_t len; - - data = xbps_data_data_nocopy(rmsg); - len = xbps_data_size(rmsg); - if ((f = fmemopen(__UNCONST(data), len, "r")) == NULL) { - rv = errno; - xbps_dbg_printf(xhp, "[remove] %s: fmemopen %s\n", pkgver, strerror(rv)); - goto out; - }; - buf = malloc(len); - assert(buf); - if (fread(buf, len, 1, f) != len) { - if (ferror(f)) { - rv = errno; - xbps_dbg_printf(xhp, "[remove] %s: fread %s\n", pkgver, strerror(rv)); - free(buf); - fclose(f); - goto out; - } - } - /* terminate buffer and notify client to show the pre-remove message */ - buf[len] = '\0'; - xbps_set_cb_state(xhp, XBPS_STATE_SHOW_REMOVE_MSG, 0, pkgver, "%s", buf); - free(buf); - fclose(f); - } + rv = xbps_cb_message(xhp, pkgd, "remove-msg"); + if (rv != 0) + goto out; } /* * If updating a package, we just need to execute the current