2011-10-20 14:39:58 +02:00
|
|
|
/*-
|
2020-04-29 14:12:10 +02:00
|
|
|
* Copyright (c) 2011-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-10-20 14:39:58 +02:00
|
|
|
*/
|
|
|
|
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdbool.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <errno.h>
|
|
|
|
#include <unistd.h>
|
2012-05-25 22:44:58 +02:00
|
|
|
#include <libgen.h>
|
2011-10-20 14:39:58 +02:00
|
|
|
|
|
|
|
#include "xbps_api_impl.h"
|
|
|
|
|
2020-02-21 09:08:22 +01:00
|
|
|
/*
|
|
|
|
* Processes the array of pkg dictionaries in "pkgs" to
|
|
|
|
* find matching package replacements via "replaces" pkg obj.
|
|
|
|
*
|
|
|
|
* This array contains the unordered list of packages in
|
|
|
|
* the transaction dictionary.
|
|
|
|
*/
|
|
|
|
bool HIDDEN
|
|
|
|
xbps_transaction_check_replaces(struct xbps_handle *xhp, xbps_array_t pkgs)
|
2011-10-20 14:39:58 +02:00
|
|
|
{
|
2020-02-21 09:08:22 +01:00
|
|
|
assert(xhp);
|
|
|
|
assert(pkgs);
|
|
|
|
|
2014-10-18 12:35:47 +02:00
|
|
|
for (unsigned int i = 0; i < xbps_array_count(pkgs); i++) {
|
2014-09-11 17:55:05 +02:00
|
|
|
xbps_array_t replaces;
|
|
|
|
xbps_object_t obj, obj2;
|
|
|
|
xbps_object_iterator_t iter;
|
2020-02-21 09:08:22 +01:00
|
|
|
xbps_dictionary_t instd, reppkgd;
|
|
|
|
const char *pkgver = NULL;
|
|
|
|
char pkgname[XBPS_NAME_SIZE] = {0};
|
2014-09-11 17:55:05 +02:00
|
|
|
|
2014-10-18 12:35:47 +02:00
|
|
|
obj = xbps_array_get(pkgs, i);
|
2013-06-20 10:26:12 +02:00
|
|
|
replaces = xbps_dictionary_get(obj, "replaces");
|
|
|
|
if (replaces == NULL || xbps_array_count(replaces) == 0)
|
2011-10-20 14:39:58 +02:00
|
|
|
continue;
|
|
|
|
|
2020-02-21 09:08:22 +01:00
|
|
|
if (!xbps_dictionary_get_cstring_nocopy(obj, "pkgver", &pkgver)) {
|
|
|
|
return false;
|
|
|
|
}
|
2020-02-08 19:31:29 +01:00
|
|
|
if (!xbps_pkg_name(pkgname, XBPS_NAME_SIZE, pkgver)) {
|
2020-02-21 09:08:22 +01:00
|
|
|
return false;
|
2020-02-08 19:31:29 +01:00
|
|
|
}
|
2014-09-11 17:55:05 +02:00
|
|
|
|
2020-02-21 09:08:22 +01:00
|
|
|
iter = xbps_array_iterator(replaces);
|
|
|
|
assert(iter);
|
|
|
|
|
2013-06-20 10:26:12 +02:00
|
|
|
while ((obj2 = xbps_object_iterator_next(iter)) != NULL) {
|
2020-02-21 09:08:22 +01:00
|
|
|
const char *curpkgver = NULL, *pattern = NULL;
|
|
|
|
char curpkgname[XBPS_NAME_SIZE] = {0};
|
2020-01-25 11:19:54 +01:00
|
|
|
bool instd_auto = false, hold = false;
|
2020-02-21 09:08:22 +01:00
|
|
|
xbps_trans_type_t ttype;
|
2014-09-11 17:55:05 +02:00
|
|
|
|
2013-06-20 10:26:12 +02:00
|
|
|
pattern = xbps_string_cstring_nocopy(obj2);
|
2011-10-20 14:39:58 +02:00
|
|
|
/*
|
|
|
|
* Find the installed package that matches the pattern
|
|
|
|
* to be replaced.
|
|
|
|
*/
|
2012-11-30 10:04:36 +01:00
|
|
|
if (((instd = xbps_pkgdb_get_pkg(xhp, pattern)) == NULL) &&
|
|
|
|
((instd = xbps_pkgdb_get_virtualpkg(xhp, pattern)) == NULL))
|
|
|
|
continue;
|
|
|
|
|
2020-02-21 09:08:22 +01:00
|
|
|
if (!xbps_dictionary_get_cstring_nocopy(instd, "pkgver", &curpkgver)) {
|
|
|
|
xbps_object_iterator_release(iter);
|
|
|
|
return false;
|
|
|
|
}
|
2020-01-25 11:19:54 +01:00
|
|
|
/* ignore pkgs on hold mode */
|
2020-01-25 11:21:37 +01:00
|
|
|
if (xbps_dictionary_get_bool(instd, "hold", &hold) && hold)
|
2020-01-25 11:19:54 +01:00
|
|
|
continue;
|
|
|
|
|
2020-02-08 19:31:29 +01:00
|
|
|
if (!xbps_pkg_name(curpkgname, XBPS_NAME_SIZE, curpkgver)) {
|
2020-02-21 09:08:22 +01:00
|
|
|
xbps_object_iterator_release(iter);
|
|
|
|
return false;
|
2020-02-08 19:31:29 +01:00
|
|
|
}
|
2012-01-24 18:43:43 +01:00
|
|
|
/*
|
|
|
|
* Check that we are not replacing the same package,
|
|
|
|
* due to virtual packages.
|
|
|
|
*/
|
2013-03-05 04:08:42 +01:00
|
|
|
if (strcmp(pkgname, curpkgname) == 0) {
|
2011-10-20 14:39:58 +02:00
|
|
|
continue;
|
2013-03-05 04:08:42 +01:00
|
|
|
}
|
2013-12-12 18:24:24 +01:00
|
|
|
/*
|
|
|
|
* Make sure to not add duplicates.
|
|
|
|
*/
|
2014-10-25 05:20:13 +02:00
|
|
|
xbps_dictionary_get_bool(instd, "automatic-install", &instd_auto);
|
2020-02-21 09:08:22 +01:00
|
|
|
reppkgd = xbps_find_pkg_in_array(pkgs, curpkgname, 0);
|
2013-12-12 18:24:24 +01:00
|
|
|
if (reppkgd) {
|
2020-02-21 09:08:22 +01:00
|
|
|
ttype = xbps_transaction_pkg_type(reppkgd);
|
|
|
|
if (ttype == XBPS_TRANS_REMOVE || ttype == XBPS_TRANS_HOLD)
|
2013-12-12 18:24:24 +01:00
|
|
|
continue;
|
2020-02-21 09:08:22 +01:00
|
|
|
if (!xbps_dictionary_get_cstring_nocopy(reppkgd,
|
|
|
|
"pkgver", &curpkgver)) {
|
|
|
|
xbps_object_iterator_release(iter);
|
|
|
|
return false;
|
|
|
|
}
|
2015-10-19 18:19:24 +02:00
|
|
|
if (!xbps_match_virtual_pkg_in_dict(reppkgd, pattern) &&
|
2020-02-21 09:08:22 +01:00
|
|
|
!xbps_pkgpattern_match(curpkgver, pattern))
|
2015-10-19 18:01:43 +02:00
|
|
|
continue;
|
2014-10-25 05:20:13 +02:00
|
|
|
/*
|
|
|
|
* Package contains replaces="pkgpattern", but the
|
|
|
|
* package that should be replaced is also in the
|
|
|
|
* transaction and it's going to be updated.
|
|
|
|
*/
|
2021-03-16 01:02:26 +01:00
|
|
|
if (!instd_auto) {
|
2021-03-14 17:22:45 +01:00
|
|
|
xbps_dictionary_remove(obj, "automatic-install");
|
2020-02-21 09:08:22 +01:00
|
|
|
}
|
|
|
|
if (!xbps_dictionary_set_bool(reppkgd, "replaced", true)) {
|
|
|
|
xbps_object_iterator_release(iter);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
if (!xbps_transaction_pkg_type_set(reppkgd, XBPS_TRANS_REMOVE)) {
|
|
|
|
xbps_object_iterator_release(iter);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
if (xbps_array_replace_dict_by_name(pkgs, reppkgd, curpkgname) != 0) {
|
|
|
|
xbps_object_iterator_release(iter);
|
|
|
|
return false;
|
|
|
|
}
|
2015-10-19 18:01:43 +02:00
|
|
|
xbps_dbg_printf(xhp,
|
|
|
|
"Package `%s' in transaction will be "
|
|
|
|
"replaced by `%s', matched with `%s'\n",
|
|
|
|
curpkgver, pkgver, pattern);
|
2014-10-17 09:52:32 +02:00
|
|
|
continue;
|
2011-10-20 14:39:58 +02:00
|
|
|
}
|
|
|
|
/*
|
|
|
|
* If new package is providing a virtual package to the
|
|
|
|
* package that we want to replace we should respect
|
2012-11-30 17:40:52 +01:00
|
|
|
* the automatic-install object.
|
2011-10-20 14:39:58 +02:00
|
|
|
*/
|
2014-04-20 16:54:50 +02:00
|
|
|
if (xbps_match_virtual_pkg_in_dict(obj, pattern)) {
|
2021-03-16 01:02:26 +01:00
|
|
|
if (!instd_auto) {
|
|
|
|
xbps_dictionary_remove(obj, "automatic-install");
|
2020-02-21 09:08:22 +01:00
|
|
|
}
|
2011-10-20 14:39:58 +02:00
|
|
|
}
|
|
|
|
/*
|
|
|
|
* Add package dictionary into the transaction and mark
|
|
|
|
* it as to be "removed".
|
|
|
|
*/
|
2020-02-21 09:08:22 +01:00
|
|
|
if (!xbps_transaction_pkg_type_set(instd, XBPS_TRANS_REMOVE)) {
|
|
|
|
xbps_object_iterator_release(iter);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
if (!xbps_dictionary_set_bool(instd, "replaced", true)) {
|
|
|
|
xbps_object_iterator_release(iter);
|
|
|
|
return false;
|
|
|
|
}
|
2015-07-26 08:15:07 +02:00
|
|
|
if (!xbps_array_add_first(pkgs, instd)) {
|
|
|
|
xbps_object_iterator_release(iter);
|
2020-02-21 09:08:22 +01:00
|
|
|
return false;
|
2015-07-26 08:15:07 +02:00
|
|
|
}
|
2020-02-21 09:08:22 +01:00
|
|
|
xbps_dbg_printf(xhp,
|
|
|
|
"Package `%s' will be replaced by `%s', "
|
|
|
|
"matched with `%s'\n", curpkgver, pkgver, pattern);
|
2011-10-20 14:39:58 +02:00
|
|
|
}
|
2013-06-20 10:26:12 +02:00
|
|
|
xbps_object_iterator_release(iter);
|
2011-10-20 14:39:58 +02:00
|
|
|
}
|
|
|
|
|
2020-02-21 09:08:22 +01:00
|
|
|
return true;
|
2011-10-20 14:39:58 +02:00
|
|
|
}
|