2011-01-24 21:25:58 +05:30
|
|
|
/*-
|
2013-03-05 08:38:42 +05:30
|
|
|
* Copyright (c) 2009-2013 Juan Romero Pardines.
|
2011-01-24 21:25:58 +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 "xbps_api_impl.h"
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @file lib/transaction_dictionary.c
|
|
|
|
* @brief Transaction handling routines
|
2012-01-20 16:47:27 +05:30
|
|
|
* @defgroup transaction Transaction handling functions
|
2011-01-24 21:25:58 +05:30
|
|
|
*
|
|
|
|
* 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.
|
|
|
|
*/
|
|
|
|
|
|
|
|
static int
|
2012-06-14 11:52:11 +05:30
|
|
|
compute_transaction_stats(struct xbps_handle *xhp)
|
2011-01-24 21:25:58 +05:30
|
|
|
{
|
2011-11-27 13:35:18 +05:30
|
|
|
prop_dictionary_t pkg_metad;
|
2011-01-24 21:25:58 +05:30
|
|
|
prop_object_iterator_t iter;
|
|
|
|
prop_object_t obj;
|
2011-11-27 13:35:18 +05:30
|
|
|
uint64_t tsize, dlsize, instsize, rmsize;
|
2011-07-24 21:13:17 +05:30
|
|
|
uint32_t inst_pkgcnt, up_pkgcnt, cf_pkgcnt, rm_pkgcnt;
|
2011-01-24 21:25:58 +05:30
|
|
|
int rv = 0;
|
2013-03-05 08:38:42 +05:30
|
|
|
const char *tract, *pkgver, *repo;
|
2011-01-24 21:25:58 +05:30
|
|
|
|
2011-07-24 21:13:17 +05:30
|
|
|
inst_pkgcnt = up_pkgcnt = cf_pkgcnt = rm_pkgcnt = 0;
|
2011-11-27 13:35:18 +05:30
|
|
|
tsize = dlsize = instsize = rmsize = 0;
|
2011-07-24 21:13:17 +05:30
|
|
|
|
2012-06-14 11:52:11 +05:30
|
|
|
iter = xbps_array_iter_from_dict(xhp->transd, "packages");
|
2011-01-24 21:25:58 +05:30
|
|
|
if (iter == NULL)
|
|
|
|
return EINVAL;
|
|
|
|
|
2011-07-24 21:13:17 +05:30
|
|
|
while ((obj = prop_object_iterator_next(iter)) != NULL) {
|
|
|
|
/*
|
|
|
|
* Count number of pkgs to be removed, configured,
|
|
|
|
* installed and updated.
|
|
|
|
*/
|
2013-03-05 08:38:42 +05:30
|
|
|
prop_dictionary_get_cstring_nocopy(obj, "pkgver", &pkgver);
|
2011-07-24 21:13:17 +05:30
|
|
|
prop_dictionary_get_cstring_nocopy(obj, "transaction", &tract);
|
2012-06-04 13:39:46 +05:30
|
|
|
prop_dictionary_get_cstring_nocopy(obj, "repository", &repo);
|
2012-01-25 09:26:54 +05:30
|
|
|
|
2012-06-05 02:52:33 +05:30
|
|
|
if (strcmp(tract, "configure") == 0) {
|
|
|
|
cf_pkgcnt++;
|
|
|
|
continue;
|
|
|
|
} else if (strcmp(tract, "install") == 0) {
|
2011-07-24 21:13:17 +05:30
|
|
|
inst_pkgcnt++;
|
2012-06-05 02:52:33 +05:30
|
|
|
} else if (strcmp(tract, "update") == 0) {
|
2011-07-24 21:13:17 +05:30
|
|
|
up_pkgcnt++;
|
2012-06-05 02:52:33 +05:30
|
|
|
} else if (strcmp(tract, "remove") == 0) {
|
2011-07-24 21:13:17 +05:30
|
|
|
rm_pkgcnt++;
|
2012-06-05 02:52:33 +05:30
|
|
|
}
|
2011-01-24 21:25:58 +05:30
|
|
|
|
|
|
|
tsize = 0;
|
2011-11-27 13:35:18 +05:30
|
|
|
/*
|
2012-01-25 09:40:24 +05:30
|
|
|
* If removing or updating a package, get installed_size
|
|
|
|
* from pkg's metadata dictionary.
|
2011-11-27 13:35:18 +05:30
|
|
|
*/
|
2012-01-25 09:40:24 +05:30
|
|
|
if ((strcmp(tract, "remove") == 0) ||
|
|
|
|
(strcmp(tract, "update") == 0)) {
|
2013-04-04 14:10:32 +05:30
|
|
|
char *pkgname;
|
|
|
|
|
|
|
|
pkgname = xbps_pkg_name(pkgver);
|
|
|
|
assert(pkgname);
|
|
|
|
pkg_metad = xbps_pkgdb_get_pkg_metadata(xhp, pkgname);
|
|
|
|
free(pkgname);
|
2011-12-24 05:35:26 +05:30
|
|
|
if (pkg_metad == NULL)
|
|
|
|
continue;
|
2011-11-27 13:35:18 +05:30
|
|
|
prop_dictionary_get_uint64(pkg_metad,
|
|
|
|
"installed_size", &tsize);
|
|
|
|
rmsize += tsize;
|
2012-01-25 09:40:24 +05:30
|
|
|
}
|
|
|
|
if ((strcmp(tract, "install") == 0) ||
|
|
|
|
(strcmp(tract, "update") == 0)) {
|
2011-11-27 13:35:18 +05:30
|
|
|
prop_dictionary_get_uint64(obj,
|
|
|
|
"installed_size", &tsize);
|
|
|
|
instsize += tsize;
|
2012-11-30 11:41:51 +05:30
|
|
|
if (xbps_repository_is_remote(repo)) {
|
2012-06-04 13:39:46 +05:30
|
|
|
prop_dictionary_get_uint64(obj,
|
|
|
|
"filename-size", &tsize);
|
|
|
|
dlsize += tsize;
|
|
|
|
}
|
2011-11-27 13:35:18 +05:30
|
|
|
}
|
2011-01-24 21:25:58 +05:30
|
|
|
}
|
2012-01-25 09:26:54 +05:30
|
|
|
|
|
|
|
if (inst_pkgcnt &&
|
2012-06-14 11:52:11 +05:30
|
|
|
!prop_dictionary_set_uint32(xhp->transd, "total-install-pkgs",
|
2012-01-25 09:26:54 +05:30
|
|
|
inst_pkgcnt)) {
|
|
|
|
rv = EINVAL;
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
if (up_pkgcnt &&
|
2012-06-14 11:52:11 +05:30
|
|
|
!prop_dictionary_set_uint32(xhp->transd, "total-update-pkgs",
|
2012-01-25 09:26:54 +05:30
|
|
|
up_pkgcnt)) {
|
|
|
|
rv = EINVAL;
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
if (cf_pkgcnt &&
|
2012-06-14 11:52:11 +05:30
|
|
|
!prop_dictionary_set_uint32(xhp->transd, "total-configure-pkgs",
|
2012-01-25 09:26:54 +05:30
|
|
|
cf_pkgcnt)) {
|
|
|
|
rv = EINVAL;
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
if (rm_pkgcnt &&
|
2012-06-14 11:52:11 +05:30
|
|
|
!prop_dictionary_set_uint32(xhp->transd, "total-remove-pkgs",
|
2012-01-25 09:26:54 +05:30
|
|
|
rm_pkgcnt)) {
|
|
|
|
rv = EINVAL;
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (instsize > rmsize) {
|
2011-11-27 13:35:18 +05:30
|
|
|
instsize -= rmsize;
|
2012-01-25 09:26:54 +05:30
|
|
|
rmsize = 0;
|
|
|
|
} else if (rmsize > instsize) {
|
|
|
|
rmsize -= instsize;
|
|
|
|
instsize = 0;
|
2012-01-25 09:40:24 +05:30
|
|
|
} else
|
|
|
|
instsize = rmsize = 0;
|
|
|
|
|
2011-01-24 21:25:58 +05:30
|
|
|
/*
|
|
|
|
* Add object in transaction dictionary with total installed
|
|
|
|
* size that it will take.
|
|
|
|
*/
|
2012-06-14 11:52:11 +05:30
|
|
|
if (!prop_dictionary_set_uint64(xhp->transd,
|
2011-01-24 21:25:58 +05:30
|
|
|
"total-installed-size", instsize)) {
|
|
|
|
rv = EINVAL;
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
/*
|
|
|
|
* Add object in transaction dictionary with total download
|
|
|
|
* size that needs to be sucked in.
|
|
|
|
*/
|
2012-06-14 11:52:11 +05:30
|
|
|
if (!prop_dictionary_set_uint64(xhp->transd,
|
2011-01-24 21:25:58 +05:30
|
|
|
"total-download-size", dlsize)) {
|
|
|
|
rv = EINVAL;
|
|
|
|
goto out;
|
|
|
|
}
|
2011-11-27 13:35:18 +05:30
|
|
|
/*
|
|
|
|
* Add object in transaction dictionary with total size to be
|
|
|
|
* freed from packages to be removed.
|
|
|
|
*/
|
2012-06-14 11:52:11 +05:30
|
|
|
if (!prop_dictionary_set_uint64(xhp->transd,
|
2011-11-27 13:35:18 +05:30
|
|
|
"total-removed-size", rmsize)) {
|
|
|
|
rv = EINVAL;
|
|
|
|
goto out;
|
|
|
|
}
|
2011-01-24 21:25:58 +05:30
|
|
|
out:
|
|
|
|
prop_object_iterator_release(iter);
|
|
|
|
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
2012-01-22 14:30:46 +05:30
|
|
|
int HIDDEN
|
|
|
|
xbps_transaction_init(struct xbps_handle *xhp)
|
2011-01-24 21:25:58 +05:30
|
|
|
{
|
2012-06-11 19:44:03 +05:30
|
|
|
prop_array_t unsorted, mdeps, conflicts;
|
2011-01-24 21:25:58 +05:30
|
|
|
|
2012-01-22 14:30:46 +05:30
|
|
|
if (xhp->transd != NULL)
|
|
|
|
return 0;
|
2011-01-24 21:25:58 +05:30
|
|
|
|
2012-06-05 02:52:33 +05:30
|
|
|
if ((xhp->transd = prop_dictionary_create()) == NULL)
|
2012-01-22 14:30:46 +05:30
|
|
|
return ENOMEM;
|
2011-01-24 21:25:58 +05:30
|
|
|
|
2012-06-05 02:52:33 +05:30
|
|
|
if ((unsorted = prop_array_create()) == NULL) {
|
2012-01-22 14:30:46 +05:30
|
|
|
prop_object_release(xhp->transd);
|
|
|
|
xhp->transd = NULL;
|
|
|
|
return ENOMEM;
|
|
|
|
}
|
|
|
|
if (!xbps_add_obj_to_dict(xhp->transd, unsorted, "unsorted_deps")) {
|
|
|
|
prop_object_release(xhp->transd);
|
|
|
|
xhp->transd = NULL;
|
|
|
|
return EINVAL;
|
|
|
|
}
|
2012-06-05 02:52:33 +05:30
|
|
|
if ((mdeps = prop_array_create()) == NULL) {
|
2012-01-22 14:30:46 +05:30
|
|
|
prop_object_release(xhp->transd);
|
|
|
|
xhp->transd = NULL;
|
|
|
|
return ENOMEM;
|
|
|
|
}
|
|
|
|
if (!xbps_add_obj_to_dict(xhp->transd, mdeps, "missing_deps")) {
|
|
|
|
prop_object_release(xhp->transd);
|
|
|
|
xhp->transd = NULL;
|
|
|
|
return EINVAL;
|
|
|
|
}
|
2012-06-11 19:44:03 +05:30
|
|
|
if ((conflicts = prop_array_create()) == NULL) {
|
|
|
|
prop_object_release(xhp->transd);
|
|
|
|
xhp->transd = NULL;
|
|
|
|
return ENOMEM;
|
|
|
|
}
|
|
|
|
if (!xbps_add_obj_to_dict(xhp->transd, conflicts, "conflicts")) {
|
|
|
|
prop_object_release(xhp->transd);
|
|
|
|
xhp->transd = NULL;
|
|
|
|
return EINVAL;
|
|
|
|
}
|
2012-01-22 14:30:46 +05:30
|
|
|
|
|
|
|
return 0;
|
2011-01-24 21:25:58 +05:30
|
|
|
}
|
|
|
|
|
2012-01-22 14:30:46 +05:30
|
|
|
int
|
2012-06-14 11:52:11 +05:30
|
|
|
xbps_transaction_prepare(struct xbps_handle *xhp)
|
2011-01-24 21:25:58 +05:30
|
|
|
{
|
2012-06-11 19:44:03 +05:30
|
|
|
prop_array_t mdeps, conflicts;
|
2011-01-24 21:25:58 +05:30
|
|
|
int rv = 0;
|
|
|
|
|
2012-01-22 14:30:46 +05:30
|
|
|
if (xhp->transd == NULL)
|
|
|
|
return ENXIO;
|
|
|
|
|
2011-01-24 21:25:58 +05:30
|
|
|
/*
|
|
|
|
* If there are missing deps bail out.
|
|
|
|
*/
|
2012-01-22 14:30:46 +05:30
|
|
|
mdeps = prop_dictionary_get(xhp->transd, "missing_deps");
|
|
|
|
if (prop_array_count(mdeps) > 0)
|
|
|
|
return ENODEV;
|
|
|
|
|
2012-06-11 19:44:03 +05:30
|
|
|
/*
|
|
|
|
* If there are package conflicts bail out.
|
|
|
|
*/
|
|
|
|
conflicts = prop_dictionary_get(xhp->transd, "conflicts");
|
|
|
|
if (prop_array_count(conflicts) > 0)
|
|
|
|
return EAGAIN;
|
|
|
|
|
2011-10-20 18:09:58 +05:30
|
|
|
/*
|
|
|
|
* Check for packages to be replaced.
|
|
|
|
*/
|
2012-06-14 11:52:11 +05:30
|
|
|
if ((rv = xbps_transaction_package_replace(xhp)) != 0) {
|
2012-01-22 14:30:46 +05:30
|
|
|
prop_object_release(xhp->transd);
|
|
|
|
xhp->transd = NULL;
|
|
|
|
return rv;
|
2011-10-20 18:09:58 +05:30
|
|
|
}
|
2011-01-24 21:25:58 +05:30
|
|
|
/*
|
|
|
|
* Sort package dependencies if necessary.
|
|
|
|
*/
|
2012-11-30 11:41:51 +05:30
|
|
|
if ((rv = xbps_transaction_sort(xhp)) != 0) {
|
2012-01-22 14:30:46 +05:30
|
|
|
prop_object_release(xhp->transd);
|
|
|
|
xhp->transd = NULL;
|
|
|
|
return rv;
|
2011-01-24 21:25:58 +05:30
|
|
|
}
|
|
|
|
/*
|
2011-07-24 21:13:17 +05:30
|
|
|
* Add transaction stats for total download/installed size,
|
|
|
|
* number of packages to be installed, updated, configured
|
|
|
|
* and removed to the transaction dictionary.
|
2011-01-24 21:25:58 +05:30
|
|
|
*/
|
2012-06-14 11:52:11 +05:30
|
|
|
if ((rv = compute_transaction_stats(xhp)) != 0) {
|
2012-01-22 14:30:46 +05:30
|
|
|
prop_object_release(xhp->transd);
|
|
|
|
xhp->transd = NULL;
|
|
|
|
return rv;
|
2011-01-24 21:25:58 +05:30
|
|
|
}
|
|
|
|
/*
|
2012-06-11 19:44:03 +05:30
|
|
|
* The missing deps and conflicts arrays are not necessary anymore.
|
2011-01-24 21:25:58 +05:30
|
|
|
*/
|
2012-01-22 14:30:46 +05:30
|
|
|
prop_dictionary_remove(xhp->transd, "missing_deps");
|
2012-06-11 19:44:03 +05:30
|
|
|
prop_dictionary_remove(xhp->transd, "conflicts");
|
2012-01-22 14:30:46 +05:30
|
|
|
prop_dictionary_make_immutable(xhp->transd);
|
2011-01-24 21:25:58 +05:30
|
|
|
|
2012-01-22 14:30:46 +05:30
|
|
|
return 0;
|
2011-01-24 21:25:58 +05:30
|
|
|
}
|