2011-07-27 20:43:54 +05:30
|
|
|
/*-
|
2020-04-29 17:42:10 +05:30
|
|
|
* Copyright (c) 2009-2020 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.
|
2011-07-27 20:43:54 +05:30
|
|
|
*/
|
|
|
|
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdbool.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <errno.h>
|
|
|
|
#include <ctype.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
#include <limits.h>
|
2015-12-11 14:29:16 +05:30
|
|
|
#include <locale.h>
|
2011-07-27 20:43:54 +05:30
|
|
|
|
|
|
|
#include "xbps_api_impl.h"
|
|
|
|
|
2012-01-20 16:47:27 +05:30
|
|
|
/**
|
|
|
|
* @file lib/transaction_commit.c
|
|
|
|
* @brief Transaction handling routines
|
|
|
|
* @defgroup transaction Transaction handling functions
|
|
|
|
*
|
|
|
|
* The following image shows off the full transaction dictionary returned
|
|
|
|
* by xbps_transaction_prepare().
|
|
|
|
*
|
|
|
|
* @image html images/xbps_transaction_dictionary.png
|
|
|
|
*
|
|
|
|
* Legend:
|
|
|
|
* - <b>Salmon bg box</b>: The transaction dictionary.
|
|
|
|
* - <b>White bg box</b>: mandatory objects.
|
|
|
|
* - <b>Grey bg box</b>: optional objects.
|
|
|
|
* - <b>Green bg box</b>: possible value set in the object, only one of them
|
|
|
|
* will be set.
|
|
|
|
*
|
|
|
|
* Text inside of white boxes are the key associated with the object, its
|
|
|
|
* data type is specified on its edge, i.e string, array, integer, dictionary.
|
|
|
|
*/
|
|
|
|
|
2011-07-27 20:43:54 +05:30
|
|
|
int
|
2012-06-14 11:52:11 +05:30
|
|
|
xbps_transaction_commit(struct xbps_handle *xhp)
|
2011-07-27 20:43:54 +05:30
|
|
|
{
|
2013-06-20 13:56:12 +05:30
|
|
|
xbps_object_t obj;
|
|
|
|
xbps_object_iterator_t iter;
|
2020-02-21 13:38:22 +05:30
|
|
|
xbps_trans_type_t ttype;
|
|
|
|
const char *pkgver = NULL;
|
2011-07-27 20:43:54 +05:30
|
|
|
int rv = 0;
|
2013-12-24 15:13:55 +05:30
|
|
|
bool update;
|
2011-07-27 20:43:54 +05:30
|
|
|
|
2015-12-11 14:29:16 +05:30
|
|
|
setlocale(LC_ALL, "");
|
|
|
|
|
2013-06-20 13:56:12 +05:30
|
|
|
assert(xbps_object_type(xhp->transd) == XBPS_TYPE_DICTIONARY);
|
2013-12-24 15:13:55 +05:30
|
|
|
/*
|
|
|
|
* Create cachedir if necessary.
|
|
|
|
*/
|
2014-10-08 13:43:21 +05:30
|
|
|
if (xbps_mkpath(xhp->cachedir, 0755) == -1) {
|
|
|
|
if (errno != EEXIST) {
|
2013-12-24 15:13:55 +05:30
|
|
|
xbps_set_cb_state(xhp, XBPS_STATE_TRANS_FAIL,
|
|
|
|
errno, NULL,
|
|
|
|
"[trans] cannot create cachedir `%s': %s",
|
|
|
|
xhp->cachedir, strerror(errno));
|
|
|
|
return errno;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (chdir(xhp->cachedir) == -1) {
|
|
|
|
xbps_set_cb_state(xhp, XBPS_STATE_TRANS_FAIL,
|
|
|
|
errno, NULL,
|
|
|
|
"[trans] failed to change dir to cachedir `%s': %s",
|
|
|
|
xhp->cachedir, strerror(errno));
|
|
|
|
return errno;
|
|
|
|
}
|
2012-01-22 14:30:46 +05:30
|
|
|
iter = xbps_array_iter_from_dict(xhp->transd, "packages");
|
2011-07-27 20:43:54 +05:30
|
|
|
if (iter == NULL)
|
|
|
|
return EINVAL;
|
2019-07-07 17:41:29 +05:30
|
|
|
|
2011-07-27 20:43:54 +05:30
|
|
|
/*
|
2019-07-07 17:41:29 +05:30
|
|
|
* Download and verify binary packages.
|
2011-07-27 20:43:54 +05:30
|
|
|
*/
|
2019-07-07 17:41:29 +05:30
|
|
|
if ((rv = xbps_transaction_fetch(xhp, iter)) != 0) {
|
|
|
|
xbps_dbg_printf(xhp, "[trans] failed to fetch and verify binpkgs: "
|
2014-03-14 01:19:54 +05:30
|
|
|
"%s\n", strerror(rv));
|
2011-07-27 20:43:54 +05:30
|
|
|
goto out;
|
2014-03-14 01:19:54 +05:30
|
|
|
}
|
2019-06-21 19:27:25 +05:30
|
|
|
if (xhp->flags & XBPS_FLAG_DOWNLOAD_ONLY) {
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
|
2020-07-12 02:57:50 +05:30
|
|
|
/*
|
|
|
|
* After all downloads are finished, clear the connection cache
|
|
|
|
* to avoid file descriptor leaks (see #303)
|
|
|
|
*/
|
|
|
|
xbps_fetch_unset_cache_connection();
|
|
|
|
|
2021-06-24 20:38:53 +05:30
|
|
|
/*
|
|
|
|
* Internalize metadata of downloaded binary packages.
|
|
|
|
*/
|
|
|
|
if ((rv = xbps_transaction_internalize(xhp, iter)) < 0) {
|
|
|
|
xbps_dbg_printf(xhp, "[trans] failed to internalize transaction binpkgs: "
|
|
|
|
"%s\n", strerror(-rv));
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
|
2019-06-16 21:08:38 +05:30
|
|
|
/*
|
|
|
|
* Collect files in the transaction and find some issues
|
|
|
|
* like multiple packages installing the same file.
|
|
|
|
*/
|
|
|
|
xbps_set_cb_state(xhp, XBPS_STATE_TRANS_FILES, 0, NULL, NULL);
|
|
|
|
if ((rv = xbps_transaction_files(xhp, iter)) != 0) {
|
|
|
|
xbps_dbg_printf(xhp, "[trans] failed to verify transaction files: "
|
|
|
|
"%s\n", strerror(rv));
|
|
|
|
goto out;
|
|
|
|
}
|
2019-06-21 19:27:25 +05:30
|
|
|
|
2011-07-27 20:43:54 +05:30
|
|
|
/*
|
2011-10-19 19:55:49 +05:30
|
|
|
* Install, update, configure or remove packages as specified
|
|
|
|
* in the transaction dictionary.
|
2011-07-27 20:43:54 +05:30
|
|
|
*/
|
2013-03-05 08:38:42 +05:30
|
|
|
xbps_set_cb_state(xhp, XBPS_STATE_TRANS_RUN, 0, NULL, NULL);
|
2011-07-27 20:43:54 +05:30
|
|
|
|
2014-05-27 13:32:01 +05:30
|
|
|
/*
|
|
|
|
* Create rootdir if necessary.
|
|
|
|
*/
|
2014-10-08 13:43:21 +05:30
|
|
|
if (xbps_mkpath(xhp->rootdir, 0750) == -1) {
|
|
|
|
rv = errno;
|
|
|
|
if (rv != EEXIST) {
|
2014-05-27 13:32:01 +05:30
|
|
|
xbps_set_cb_state(xhp, XBPS_STATE_TRANS_FAIL, errno, xhp->rootdir,
|
|
|
|
"[trans] failed to create rootdir `%s': %s",
|
|
|
|
xhp->rootdir, strerror(rv));
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (chdir(xhp->rootdir) == -1) {
|
|
|
|
rv = errno;
|
|
|
|
xbps_set_cb_state(xhp, XBPS_STATE_UNPACK_FAIL, rv, xhp->rootdir,
|
|
|
|
"[trans] failed to chdir to rootdir `%s': %s",
|
|
|
|
xhp->rootdir, strerror(errno));
|
|
|
|
goto out;
|
|
|
|
}
|
2013-06-20 13:56:12 +05:30
|
|
|
while ((obj = xbps_object_iterator_next(iter)) != NULL) {
|
|
|
|
xbps_dictionary_get_cstring_nocopy(obj, "pkgver", &pkgver);
|
2011-07-27 20:43:54 +05:30
|
|
|
|
2020-02-21 13:38:22 +05:30
|
|
|
ttype = xbps_transaction_pkg_type(obj);
|
|
|
|
if (ttype == XBPS_TRANS_REMOVE) {
|
2011-10-19 19:55:49 +05:30
|
|
|
/*
|
2011-12-24 05:35:26 +05:30
|
|
|
* Remove package.
|
2011-10-19 19:55:49 +05:30
|
|
|
*/
|
2014-01-03 16:05:20 +05:30
|
|
|
update = false;
|
2013-12-24 15:13:55 +05:30
|
|
|
xbps_dictionary_get_bool(obj, "remove-and-update", &update);
|
2013-12-25 15:42:52 +05:30
|
|
|
rv = xbps_remove_pkg(xhp, pkgver, update);
|
2012-11-19 19:08:06 +05:30
|
|
|
if (rv != 0) {
|
|
|
|
xbps_dbg_printf(xhp, "[trans] failed to "
|
2014-03-14 01:05:26 +05:30
|
|
|
"remove %s: %s\n", pkgver, strerror(rv));
|
2011-07-27 20:43:54 +05:30
|
|
|
goto out;
|
2012-11-19 19:08:06 +05:30
|
|
|
}
|
2013-12-24 15:13:55 +05:30
|
|
|
continue;
|
|
|
|
|
2020-02-21 13:38:22 +05:30
|
|
|
} else if (ttype == XBPS_TRANS_CONFIGURE) {
|
2011-10-19 19:55:49 +05:30
|
|
|
/*
|
|
|
|
* Reconfigure pending package.
|
|
|
|
*/
|
2014-03-04 19:07:10 +05:30
|
|
|
rv = xbps_configure_pkg(xhp, pkgver, false, false);
|
2014-03-14 01:05:26 +05:30
|
|
|
if (rv != 0) {
|
|
|
|
xbps_dbg_printf(xhp, "[trans] failed to "
|
|
|
|
"configure %s: %s\n", pkgver, strerror(rv));
|
2011-07-27 20:43:54 +05:30
|
|
|
goto out;
|
2014-03-14 01:05:26 +05:30
|
|
|
}
|
2013-12-24 15:13:55 +05:30
|
|
|
continue;
|
|
|
|
|
2020-02-21 13:38:22 +05:30
|
|
|
} else if (ttype == XBPS_TRANS_UPDATE) {
|
2011-10-19 19:55:49 +05:30
|
|
|
/*
|
2013-12-24 15:13:55 +05:30
|
|
|
* Update a package: execute pre-remove action of
|
|
|
|
* existing package before unpacking new version.
|
2011-10-19 19:55:49 +05:30
|
|
|
*/
|
2013-12-25 15:42:52 +05:30
|
|
|
xbps_set_cb_state(xhp, XBPS_STATE_UPDATE, 0, pkgver, NULL);
|
|
|
|
rv = xbps_remove_pkg(xhp, pkgver, true);
|
|
|
|
if (rv != 0) {
|
|
|
|
xbps_set_cb_state(xhp,
|
|
|
|
XBPS_STATE_UPDATE_FAIL,
|
|
|
|
rv, pkgver,
|
|
|
|
"%s: [trans] failed to update "
|
|
|
|
"package `%s'", pkgver,
|
|
|
|
strerror(rv));
|
|
|
|
goto out;
|
2011-10-19 19:55:49 +05:30
|
|
|
}
|
2020-02-21 13:38:22 +05:30
|
|
|
} else if (ttype == XBPS_TRANS_HOLD) {
|
2015-01-28 16:37:47 +05:30
|
|
|
/*
|
|
|
|
* Package is on hold mode, ignore it.
|
|
|
|
*/
|
|
|
|
continue;
|
2013-12-24 15:13:55 +05:30
|
|
|
} else {
|
2020-02-21 13:38:22 +05:30
|
|
|
/* Install or reinstall package */
|
2013-12-24 15:13:55 +05:30
|
|
|
xbps_set_cb_state(xhp, XBPS_STATE_INSTALL, 0,
|
|
|
|
pkgver, NULL);
|
2011-07-27 20:43:54 +05:30
|
|
|
}
|
2013-12-24 15:13:55 +05:30
|
|
|
/*
|
|
|
|
* Unpack binary package.
|
|
|
|
*/
|
2014-03-14 01:19:54 +05:30
|
|
|
if ((rv = xbps_unpack_binary_pkg(xhp, obj)) != 0) {
|
|
|
|
xbps_dbg_printf(xhp, "[trans] failed to unpack "
|
|
|
|
"%s: %s\n", pkgver, strerror(rv));
|
2013-12-24 15:13:55 +05:30
|
|
|
goto out;
|
2014-03-14 01:19:54 +05:30
|
|
|
}
|
2013-12-24 15:13:55 +05:30
|
|
|
/*
|
|
|
|
* Register package.
|
|
|
|
*/
|
2014-03-14 01:19:54 +05:30
|
|
|
if ((rv = xbps_register_pkg(xhp, obj)) != 0) {
|
|
|
|
xbps_dbg_printf(xhp, "[trans] failed to register "
|
|
|
|
"%s: %s\n", pkgver, strerror(rv));
|
2013-12-24 15:13:55 +05:30
|
|
|
goto out;
|
2014-03-14 01:19:54 +05:30
|
|
|
}
|
2011-07-27 20:43:54 +05:30
|
|
|
}
|
2011-11-25 15:50:03 +05:30
|
|
|
/* if there are no packages to install or update we are done */
|
2013-12-24 15:13:55 +05:30
|
|
|
if (!xbps_dictionary_get(xhp->transd, "total-update-pkgs") &&
|
|
|
|
!xbps_dictionary_get(xhp->transd, "total-install-pkgs"))
|
2011-11-25 15:50:03 +05:30
|
|
|
goto out;
|
2012-11-30 11:41:51 +05:30
|
|
|
|
2015-06-03 14:45:11 +05:30
|
|
|
if (xhp->target_arch && strcmp(xhp->native_arch, xhp->target_arch)) {
|
|
|
|
/* if installing packages for target_arch, don't configure anything */
|
2013-03-07 22:38:12 +05:30
|
|
|
goto out;
|
2015-06-03 14:45:11 +05:30
|
|
|
/* do not configure packages if only unpacking is desired */
|
|
|
|
} else if (xhp->flags & XBPS_FLAG_UNPACK_ONLY) {
|
|
|
|
goto out;
|
|
|
|
}
|
2013-03-07 22:38:12 +05:30
|
|
|
|
2013-06-20 13:56:12 +05:30
|
|
|
xbps_object_iterator_reset(iter);
|
2014-03-04 19:07:10 +05:30
|
|
|
/* Force a pkgdb write for all unpacked pkgs in transaction */
|
2020-02-21 13:38:22 +05:30
|
|
|
if ((rv = xbps_pkgdb_update(xhp, true, true)) != 0)
|
|
|
|
goto out;
|
2013-03-07 22:38:12 +05:30
|
|
|
|
2011-07-27 20:43:54 +05:30
|
|
|
/*
|
|
|
|
* Configure all unpacked packages.
|
|
|
|
*/
|
2013-03-05 08:38:42 +05:30
|
|
|
xbps_set_cb_state(xhp, XBPS_STATE_TRANS_CONFIGURE, 0, NULL, NULL);
|
2011-07-27 20:43:54 +05:30
|
|
|
|
2013-06-20 13:56:12 +05:30
|
|
|
while ((obj = xbps_object_iterator_next(iter)) != NULL) {
|
2014-09-13 15:51:49 +05:30
|
|
|
xbps_dictionary_get_cstring_nocopy(obj, "pkgver", &pkgver);
|
2020-02-21 13:38:22 +05:30
|
|
|
ttype = xbps_transaction_pkg_type(obj);
|
|
|
|
if (ttype == XBPS_TRANS_REMOVE || ttype == XBPS_TRANS_HOLD ||
|
|
|
|
ttype == XBPS_TRANS_CONFIGURE) {
|
2014-09-13 15:51:49 +05:30
|
|
|
xbps_dbg_printf(xhp, "%s: skipping configuration for "
|
2020-02-21 13:38:22 +05:30
|
|
|
"%s: %d\n", __func__, pkgver, ttype);
|
2011-07-27 20:43:54 +05:30
|
|
|
continue;
|
2014-09-13 15:51:49 +05:30
|
|
|
}
|
2011-07-27 20:43:54 +05:30
|
|
|
update = false;
|
2020-02-21 13:38:22 +05:30
|
|
|
if (ttype == XBPS_TRANS_UPDATE)
|
2011-07-27 20:43:54 +05:30
|
|
|
update = true;
|
|
|
|
|
2014-03-04 19:07:10 +05:30
|
|
|
rv = xbps_configure_pkg(xhp, pkgver, false, update);
|
2012-11-30 11:41:51 +05:30
|
|
|
if (rv != 0) {
|
|
|
|
xbps_dbg_printf(xhp, "%s: configure failed for "
|
2013-08-29 19:19:23 +05:30
|
|
|
"%s: %s\n", __func__, pkgver, strerror(rv));
|
2011-07-27 20:43:54 +05:30
|
|
|
goto out;
|
2012-11-30 11:41:51 +05:30
|
|
|
}
|
2011-11-25 14:14:49 +05:30
|
|
|
/*
|
|
|
|
* Notify client callback when a package has been
|
|
|
|
* installed or updated.
|
|
|
|
*/
|
|
|
|
if (update) {
|
2012-06-14 11:52:11 +05:30
|
|
|
xbps_set_cb_state(xhp, XBPS_STATE_UPDATE_DONE, 0,
|
2013-03-05 08:38:42 +05:30
|
|
|
pkgver, NULL);
|
2011-11-25 14:14:49 +05:30
|
|
|
} else {
|
2012-06-14 11:52:11 +05:30
|
|
|
xbps_set_cb_state(xhp, XBPS_STATE_INSTALL_DONE, 0,
|
2013-03-05 08:38:42 +05:30
|
|
|
pkgver, NULL);
|
2011-11-25 14:14:49 +05:30
|
|
|
}
|
2011-07-27 20:43:54 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
out:
|
2013-06-20 13:56:12 +05:30
|
|
|
xbps_object_iterator_release(iter);
|
2020-02-21 13:38:22 +05:30
|
|
|
if (rv == 0) {
|
|
|
|
/* Force a pkgdb write for all unpacked pkgs in transaction */
|
|
|
|
rv = xbps_pkgdb_update(xhp, true, true);
|
|
|
|
}
|
2011-07-27 20:43:54 +05:30
|
|
|
return rv;
|
|
|
|
}
|