2011-07-27 20:43:54 +05:30
|
|
|
/*-
|
2013-03-05 08:38:42 +05:30
|
|
|
* Copyright (c) 2009-2013 Juan Romero Pardines.
|
2011-07-27 20:43:54 +05:30
|
|
|
* 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 <stdio.h>
|
|
|
|
#include <stdbool.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <errno.h>
|
|
|
|
#include <ctype.h>
|
|
|
|
#include <assert.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
#include <limits.h>
|
|
|
|
|
|
|
|
#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
|
|
|
static int
|
2012-06-14 11:52:11 +05:30
|
|
|
check_binpkgs_hash(struct xbps_handle *xhp, prop_object_iterator_t iter)
|
2011-07-27 20:43:54 +05:30
|
|
|
{
|
|
|
|
prop_object_t obj;
|
2013-03-05 08:38:42 +05:30
|
|
|
const char *pkgver, *arch, *repoloc, *sha256, *trans;
|
|
|
|
char *binfile, *filen;
|
2011-07-27 20:43:54 +05:30
|
|
|
int rv = 0;
|
|
|
|
|
|
|
|
while ((obj = prop_object_iterator_next(iter)) != NULL) {
|
|
|
|
prop_dictionary_get_cstring_nocopy(obj, "transaction", &trans);
|
|
|
|
if ((strcmp(trans, "remove") == 0) ||
|
|
|
|
(strcmp(trans, "configure") == 0))
|
|
|
|
continue;
|
|
|
|
|
2013-03-05 08:38:42 +05:30
|
|
|
prop_dictionary_get_cstring_nocopy(obj, "architecture", &arch);
|
2011-07-27 20:43:54 +05:30
|
|
|
prop_dictionary_get_cstring_nocopy(obj, "repository", &repoloc);
|
|
|
|
prop_dictionary_get_cstring_nocopy(obj, "pkgver", &pkgver);
|
|
|
|
prop_dictionary_get_cstring_nocopy(obj,
|
|
|
|
"filename-sha256", &sha256);
|
|
|
|
|
2012-11-30 11:41:51 +05:30
|
|
|
binfile = xbps_repository_pkg_path(xhp, obj);
|
2011-07-27 20:43:54 +05:30
|
|
|
if (binfile == NULL) {
|
|
|
|
rv = EINVAL;
|
|
|
|
break;
|
|
|
|
}
|
2013-03-05 08:38:42 +05:30
|
|
|
filen = xbps_xasprintf("%s.%s.xbps", pkgver, arch);
|
|
|
|
|
|
|
|
xbps_set_cb_state(xhp, XBPS_STATE_VERIFY, 0, pkgver,
|
2011-11-24 15:53:08 +05:30
|
|
|
"Verifying `%s' package integrity...", filen, repoloc);
|
2011-07-27 20:43:54 +05:30
|
|
|
rv = xbps_file_hash_check(binfile, sha256);
|
|
|
|
if (rv != 0) {
|
|
|
|
free(binfile);
|
2012-06-14 11:52:11 +05:30
|
|
|
xbps_set_cb_state(xhp, XBPS_STATE_VERIFY_FAIL,
|
2013-03-05 08:38:42 +05:30
|
|
|
rv, pkgver,
|
2011-11-24 15:53:08 +05:30
|
|
|
"Failed to verify `%s' package integrity: %s",
|
|
|
|
filen, strerror(rv));
|
2013-03-26 15:05:20 +05:30
|
|
|
free(filen);
|
2011-07-27 20:43:54 +05:30
|
|
|
break;
|
|
|
|
}
|
|
|
|
free(binfile);
|
2013-03-05 08:38:42 +05:30
|
|
|
free(filen);
|
2011-07-27 20:43:54 +05:30
|
|
|
}
|
|
|
|
prop_object_iterator_reset(iter);
|
|
|
|
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
download_binpkgs(struct xbps_handle *xhp, prop_object_iterator_t iter)
|
|
|
|
{
|
|
|
|
prop_object_t obj;
|
2013-03-05 08:38:42 +05:30
|
|
|
const char *pkgver, *arch, *fetchstr, *repoloc, *trans;
|
|
|
|
char *binfile, *filen;
|
2011-07-27 20:43:54 +05:30
|
|
|
int rv = 0;
|
2012-11-16 21:25:35 +05:30
|
|
|
bool state_dload = false;
|
2011-07-27 20:43:54 +05:30
|
|
|
|
|
|
|
while ((obj = prop_object_iterator_next(iter)) != NULL) {
|
|
|
|
prop_dictionary_get_cstring_nocopy(obj, "transaction", &trans);
|
|
|
|
if ((strcmp(trans, "remove") == 0) ||
|
|
|
|
(strcmp(trans, "configure") == 0))
|
|
|
|
continue;
|
|
|
|
|
2013-03-05 08:38:42 +05:30
|
|
|
prop_dictionary_get_cstring_nocopy(obj, "architecture", &arch);
|
2011-07-27 20:43:54 +05:30
|
|
|
prop_dictionary_get_cstring_nocopy(obj, "repository", &repoloc);
|
|
|
|
prop_dictionary_get_cstring_nocopy(obj, "pkgver", &pkgver);
|
|
|
|
|
2012-11-30 11:41:51 +05:30
|
|
|
binfile = xbps_repository_pkg_path(xhp, obj);
|
2011-07-27 20:43:54 +05:30
|
|
|
if (binfile == NULL) {
|
|
|
|
rv = EINVAL;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
/*
|
|
|
|
* If downloaded package is in cachedir continue.
|
|
|
|
*/
|
|
|
|
if (access(binfile, R_OK) == 0) {
|
|
|
|
free(binfile);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
/*
|
|
|
|
* Create cachedir.
|
|
|
|
*/
|
2012-11-11 15:59:49 +05:30
|
|
|
if (access(xhp->cachedir, R_OK|X_OK|W_OK) == -1) {
|
|
|
|
if (xbps_mkpath(xhp->cachedir, 0755) == -1) {
|
|
|
|
xbps_set_cb_state(xhp, XBPS_STATE_DOWNLOAD_FAIL,
|
2013-03-05 08:38:42 +05:30
|
|
|
errno, pkgver,
|
2012-11-11 15:59:49 +05:30
|
|
|
"%s: [trans] cannot create cachedir `%s':"
|
|
|
|
"%s", pkgver, xhp->cachedir,
|
|
|
|
strerror(errno));
|
|
|
|
free(binfile);
|
|
|
|
rv = errno;
|
|
|
|
break;
|
|
|
|
}
|
2011-07-27 20:43:54 +05:30
|
|
|
}
|
2012-11-16 21:25:35 +05:30
|
|
|
if (state_dload == false) {
|
|
|
|
xbps_set_cb_state(xhp, XBPS_STATE_TRANS_DOWNLOAD,
|
2013-03-05 08:38:42 +05:30
|
|
|
0, NULL, NULL);
|
2012-11-16 21:25:35 +05:30
|
|
|
state_dload = true;
|
|
|
|
}
|
2013-03-05 08:38:42 +05:30
|
|
|
filen = xbps_xasprintf("%s.%s.xbps", pkgver, arch);
|
2012-06-14 11:52:11 +05:30
|
|
|
xbps_set_cb_state(xhp, XBPS_STATE_DOWNLOAD,
|
2013-03-05 08:38:42 +05:30
|
|
|
0, pkgver, "Downloading binary package `%s' (from `%s')...",
|
2011-11-24 15:53:08 +05:30
|
|
|
filen, repoloc);
|
2011-07-27 20:43:54 +05:30
|
|
|
/*
|
|
|
|
* Fetch binary package.
|
|
|
|
*/
|
2012-11-11 15:59:49 +05:30
|
|
|
if (chdir(xhp->cachedir) == -1) {
|
|
|
|
xbps_set_cb_state(xhp, XBPS_STATE_DOWNLOAD_FAIL,
|
2013-03-05 08:38:42 +05:30
|
|
|
errno, pkgver,
|
2012-11-11 15:59:49 +05:30
|
|
|
"%s: [trans] failed to change dir to cachedir"
|
|
|
|
"`%s': %s", pkgver, xhp->cachedir,
|
|
|
|
strerror(errno));
|
|
|
|
rv = errno;
|
|
|
|
free(binfile);
|
2013-03-05 08:38:42 +05:30
|
|
|
free(filen);
|
2012-11-11 15:59:49 +05:30
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
rv = xbps_fetch_file(xhp, binfile, NULL);
|
2011-07-27 20:43:54 +05:30
|
|
|
if (rv == -1) {
|
2012-05-06 12:39:09 +05:30
|
|
|
fetchstr = xbps_fetch_error_string();
|
2012-06-14 11:52:11 +05:30
|
|
|
xbps_set_cb_state(xhp, XBPS_STATE_DOWNLOAD_FAIL,
|
2012-05-06 12:39:09 +05:30
|
|
|
fetchLastErrCode != 0 ? fetchLastErrCode : errno,
|
2013-03-05 08:38:42 +05:30
|
|
|
pkgver, "%s: [trans] failed to download binary package "
|
2011-11-24 15:53:08 +05:30
|
|
|
"`%s' from `%s': %s", pkgver, filen, repoloc,
|
2012-05-06 12:39:09 +05:30
|
|
|
fetchstr ? fetchstr : strerror(errno));
|
2011-07-27 20:43:54 +05:30
|
|
|
free(binfile);
|
2013-03-05 08:38:42 +05:30
|
|
|
free(filen);
|
2011-07-27 20:43:54 +05:30
|
|
|
break;
|
|
|
|
}
|
|
|
|
rv = 0;
|
|
|
|
free(binfile);
|
2013-03-05 08:38:42 +05:30
|
|
|
free(filen);
|
2011-07-27 20:43:54 +05:30
|
|
|
}
|
|
|
|
prop_object_iterator_reset(iter);
|
|
|
|
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
2012-06-14 11:52:11 +05:30
|
|
|
xbps_transaction_commit(struct xbps_handle *xhp)
|
2011-07-27 20:43:54 +05:30
|
|
|
{
|
|
|
|
prop_object_t obj;
|
|
|
|
prop_object_iterator_t iter;
|
2013-03-05 08:38:42 +05:30
|
|
|
const char *pkgver, *tract;
|
2011-07-27 20:43:54 +05:30
|
|
|
int rv = 0;
|
2012-05-25 20:54:36 +05:30
|
|
|
bool update, install, sr;
|
2011-07-27 20:43:54 +05:30
|
|
|
|
2012-01-22 14:30:46 +05:30
|
|
|
assert(prop_object_type(xhp->transd) == PROP_TYPE_DICTIONARY);
|
2011-07-27 20:43:54 +05:30
|
|
|
|
2011-12-24 05:35:26 +05:30
|
|
|
update = install = false;
|
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;
|
|
|
|
/*
|
|
|
|
* Download binary packages (if they come from a remote repository).
|
|
|
|
*/
|
|
|
|
if ((rv = download_binpkgs(xhp, iter)) != 0)
|
|
|
|
goto out;
|
|
|
|
/*
|
|
|
|
* Check SHA256 hashes for binary packages in transaction.
|
|
|
|
*/
|
2013-03-05 08:38:42 +05:30
|
|
|
xbps_set_cb_state(xhp, XBPS_STATE_TRANS_VERIFY, 0, NULL, NULL);
|
2012-06-14 11:52:11 +05:30
|
|
|
if ((rv = check_binpkgs_hash(xhp, iter)) != 0)
|
2011-07-27 20:43:54 +05:30
|
|
|
goto out;
|
|
|
|
/*
|
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
|
|
|
|
2011-10-19 19:55:49 +05:30
|
|
|
while ((obj = prop_object_iterator_next(iter)) != NULL) {
|
2011-10-20 18:09:58 +05:30
|
|
|
update = false;
|
2011-10-19 19:55:49 +05:30
|
|
|
prop_dictionary_get_cstring_nocopy(obj, "transaction", &tract);
|
|
|
|
prop_dictionary_get_cstring_nocopy(obj, "pkgver", &pkgver);
|
2011-07-27 20:43:54 +05:30
|
|
|
|
2011-10-19 19:55:49 +05:30
|
|
|
if (strcmp(tract, "remove") == 0) {
|
2011-12-24 05:35:26 +05:30
|
|
|
update = false;
|
2012-05-25 20:54:36 +05:30
|
|
|
sr = false;
|
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
|
|
|
*/
|
2011-07-27 20:43:54 +05:30
|
|
|
prop_dictionary_get_bool(obj, "remove-and-update",
|
|
|
|
&update);
|
2012-05-25 20:54:36 +05:30
|
|
|
prop_dictionary_get_bool(obj, "softreplace", &sr);
|
2012-11-30 11:41:51 +05:30
|
|
|
rv = xbps_remove_pkg(xhp, pkgver, update, sr);
|
2012-11-19 19:08:06 +05:30
|
|
|
if (rv != 0) {
|
|
|
|
xbps_dbg_printf(xhp, "[trans] failed to "
|
2013-03-05 08:38:42 +05:30
|
|
|
"remove %s\n", pkgver);
|
2011-07-27 20:43:54 +05:30
|
|
|
goto out;
|
2012-11-19 19:08:06 +05:30
|
|
|
}
|
2011-10-19 19:55:49 +05:30
|
|
|
} else if (strcmp(tract, "configure") == 0) {
|
|
|
|
/*
|
|
|
|
* Reconfigure pending package.
|
|
|
|
*/
|
2013-03-05 08:38:42 +05:30
|
|
|
rv = xbps_configure_pkg(xhp, pkgver, false, false, false);
|
2011-11-24 15:53:08 +05:30
|
|
|
if (rv != 0)
|
2011-07-27 20:43:54 +05:30
|
|
|
goto out;
|
2011-10-19 19:55:49 +05:30
|
|
|
} else {
|
|
|
|
/*
|
|
|
|
* Install or update a package.
|
|
|
|
*/
|
|
|
|
if (strcmp(tract, "update") == 0)
|
|
|
|
update = true;
|
2011-11-25 15:50:03 +05:30
|
|
|
else
|
|
|
|
install = true;
|
2011-10-19 19:55:49 +05:30
|
|
|
|
2013-03-05 08:38:42 +05:30
|
|
|
if (update && xbps_pkgdb_get_pkg(xhp, pkgver)) {
|
2011-10-19 19:55:49 +05:30
|
|
|
/*
|
|
|
|
* Update a package: execute pre-remove
|
|
|
|
* action if found before unpacking.
|
|
|
|
*/
|
2012-06-14 11:52:11 +05:30
|
|
|
xbps_set_cb_state(xhp, XBPS_STATE_UPDATE, 0,
|
2013-03-05 08:38:42 +05:30
|
|
|
pkgver, NULL);
|
2012-11-30 11:41:51 +05:30
|
|
|
rv = xbps_remove_pkg(xhp, pkgver, true, false);
|
2011-10-19 19:55:49 +05:30
|
|
|
if (rv != 0) {
|
2012-06-14 11:52:11 +05:30
|
|
|
xbps_set_cb_state(xhp,
|
2011-11-24 15:53:08 +05:30
|
|
|
XBPS_STATE_UPDATE_FAIL,
|
2013-03-05 08:38:42 +05:30
|
|
|
rv, pkgver,
|
2011-11-24 15:53:08 +05:30
|
|
|
"%s: [trans] failed to update "
|
2013-03-05 08:38:42 +05:30
|
|
|
"package `%s'", pkgver,
|
|
|
|
strerror(rv));
|
2011-10-19 19:55:49 +05:30
|
|
|
goto out;
|
|
|
|
}
|
2011-11-24 15:53:08 +05:30
|
|
|
} else {
|
|
|
|
/* Install a package */
|
2012-06-14 11:52:11 +05:30
|
|
|
xbps_set_cb_state(xhp, XBPS_STATE_INSTALL,
|
2013-03-05 08:38:42 +05:30
|
|
|
0, pkgver, NULL);
|
2011-10-19 19:55:49 +05:30
|
|
|
}
|
|
|
|
/*
|
|
|
|
* Unpack binary package.
|
|
|
|
*/
|
2012-06-14 11:52:11 +05:30
|
|
|
if ((rv = xbps_unpack_binary_pkg(xhp, obj)) != 0)
|
2011-10-19 19:55:49 +05:30
|
|
|
goto out;
|
|
|
|
/*
|
|
|
|
* Register package.
|
|
|
|
*/
|
2013-03-05 08:38:42 +05:30
|
|
|
if ((rv = xbps_register_pkg(xhp, obj)) != 0)
|
2011-07-27 20:43:54 +05:30
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
}
|
2011-11-25 15:50:03 +05:30
|
|
|
/* if there are no packages to install or update we are done */
|
|
|
|
if (!update && !install)
|
|
|
|
goto out;
|
2012-11-30 11:41:51 +05:30
|
|
|
|
2013-03-07 22:38:12 +05:30
|
|
|
/* if installing packages for target_arch, don't configure anything */
|
|
|
|
if (xhp->target_arch && strcmp(xhp->native_arch, xhp->target_arch))
|
|
|
|
goto out;
|
|
|
|
|
|
|
|
prop_object_iterator_reset(iter);
|
|
|
|
|
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
|
|
|
|
|
|
|
while ((obj = prop_object_iterator_next(iter)) != NULL) {
|
|
|
|
prop_dictionary_get_cstring_nocopy(obj, "transaction", &tract);
|
|
|
|
if ((strcmp(tract, "remove") == 0) ||
|
|
|
|
(strcmp(tract, "configure") == 0))
|
|
|
|
continue;
|
2011-12-24 05:35:26 +05:30
|
|
|
|
2013-03-05 08:38:42 +05:30
|
|
|
prop_dictionary_get_cstring_nocopy(obj, "pkgver", &pkgver);
|
2011-07-27 20:43:54 +05:30
|
|
|
update = false;
|
|
|
|
if (strcmp(tract, "update") == 0)
|
|
|
|
update = true;
|
|
|
|
|
2013-03-05 08:38:42 +05:30
|
|
|
rv = xbps_configure_pkg(xhp, pkgver, false, update, true);
|
2012-11-30 11:41:51 +05:30
|
|
|
if (rv != 0) {
|
|
|
|
xbps_dbg_printf(xhp, "%s: configure failed for "
|
2013-03-05 08:38:42 +05:30
|
|
|
"%s: %s\n", 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:
|
|
|
|
prop_object_iterator_release(iter);
|
|
|
|
|
|
|
|
return rv;
|
|
|
|
}
|