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