2011-10-20 18:09:58 +05:30
|
|
|
/*-
|
2015-02-27 00:21:03 +05:30
|
|
|
* Copyright (c) 2011-2015 Juan Romero Pardines.
|
2011-10-20 18:09: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 <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"
|
|
|
|
|
|
|
|
int HIDDEN
|
2014-10-18 16:05:47 +05:30
|
|
|
xbps_transaction_package_replace(struct xbps_handle *xhp, xbps_array_t pkgs)
|
2011-10-20 18:09:58 +05:30
|
|
|
{
|
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;
|
|
|
|
const char *pkgver;
|
|
|
|
char *pkgname;
|
|
|
|
|
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;
|
|
|
|
|
2013-06-20 13:56:12 +05:30
|
|
|
iter = xbps_array_iterator(replaces);
|
2012-11-30 14:34:36 +05:30
|
|
|
assert(iter);
|
2011-10-20 18:09:58 +05:30
|
|
|
|
2014-09-11 21:25:05 +05:30
|
|
|
xbps_dictionary_get_cstring_nocopy(obj, "pkgver", &pkgver);
|
|
|
|
pkgname = xbps_pkg_name(pkgver);
|
|
|
|
assert(pkgname);
|
|
|
|
|
2013-06-20 13:56:12 +05:30
|
|
|
while ((obj2 = xbps_object_iterator_next(iter)) != NULL) {
|
2014-09-11 21:25:05 +05:30
|
|
|
xbps_dictionary_t instd, reppkgd;
|
|
|
|
const char *tract, *pattern, *curpkgver;
|
|
|
|
char *curpkgname;
|
|
|
|
bool instd_auto = false;
|
|
|
|
|
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;
|
|
|
|
|
2013-06-20 13:56:12 +05:30
|
|
|
xbps_dictionary_get_cstring_nocopy(instd,
|
2012-01-24 23:13:43 +05:30
|
|
|
"pkgver", &curpkgver);
|
2013-03-05 08:38:42 +05:30
|
|
|
curpkgname = xbps_pkg_name(curpkgver);
|
2014-10-05 16:20:50 +05:30
|
|
|
assert(curpkgname);
|
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) {
|
|
|
|
free(curpkgname);
|
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);
|
2014-10-18 16:05:47 +05:30
|
|
|
reppkgd = xbps_find_pkg_in_array(pkgs, curpkgname, NULL);
|
2013-12-12 22:54:24 +05:30
|
|
|
if (reppkgd) {
|
|
|
|
xbps_dictionary_get_cstring_nocopy(reppkgd,
|
|
|
|
"transaction", &tract);
|
|
|
|
if (strcmp(tract, "remove") == 0)
|
|
|
|
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.
|
|
|
|
*/
|
2013-06-20 13:56:12 +05:30
|
|
|
xbps_dictionary_set_bool(reppkgd,
|
2011-10-20 18:09:58 +05:30
|
|
|
"automatic-install", instd_auto);
|
2014-10-18 16:05:47 +05:30
|
|
|
xbps_array_replace_dict_by_name(pkgs,
|
2013-12-12 22:54:24 +05:30
|
|
|
reppkgd, curpkgname);
|
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)) {
|
2013-06-20 13:56:12 +05:30
|
|
|
xbps_dictionary_set_bool(obj,
|
2011-10-20 18:09:58 +05:30
|
|
|
"automatic-install", instd_auto);
|
|
|
|
}
|
2013-12-12 22:54:24 +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
|
|
|
/*
|
|
|
|
* Add package dictionary into the transaction and mark
|
|
|
|
* it as to be "removed".
|
|
|
|
*/
|
2013-06-20 13:56:12 +05:30
|
|
|
xbps_dictionary_set_cstring_nocopy(instd,
|
2011-10-20 18:09:58 +05:30
|
|
|
"transaction", "remove");
|
2015-07-26 11:45:07 +05:30
|
|
|
if (!xbps_array_add_first(pkgs, instd)) {
|
|
|
|
xbps_object_iterator_release(iter);
|
|
|
|
free(pkgname);
|
|
|
|
free(curpkgname);
|
2014-11-13 19:48:21 +05:30
|
|
|
return EINVAL;
|
2015-07-26 11:45:07 +05:30
|
|
|
}
|
2013-03-05 08:38:42 +05:30
|
|
|
free(curpkgname);
|
2011-10-20 18:09:58 +05:30
|
|
|
}
|
2013-06-20 13:56:12 +05:30
|
|
|
xbps_object_iterator_release(iter);
|
2014-09-11 21:25:05 +05:30
|
|
|
free(pkgname);
|
2011-10-20 18:09:58 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|