2015-10-18 14:08:35 +05:30
|
|
|
/*-
|
2019-06-11 20:36:03 +05:30
|
|
|
* Copyright (c) 2015-2019 Juan Romero Pardines.
|
|
|
|
* Copyright (c) 2019 Duncan Overbruck.
|
2015-10-18 14:08:35 +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>
|
2015-10-31 17:24:01 +05:30
|
|
|
#include <libgen.h>
|
2015-10-18 14:08:35 +05:30
|
|
|
|
|
|
|
#include "xbps_api_impl.h"
|
|
|
|
|
2015-10-30 17:48:32 +05:30
|
|
|
/**
|
|
|
|
* @file lib/package_alternatives.c
|
|
|
|
* @brief Alternatives generic routines
|
|
|
|
* @defgroup alternatives Alternatives generic functions
|
|
|
|
*
|
|
|
|
* These functions implement the alternatives framework.
|
|
|
|
*/
|
|
|
|
|
2015-10-18 14:08:35 +05:30
|
|
|
static char *
|
|
|
|
left(const char *str)
|
|
|
|
{
|
|
|
|
char *p;
|
|
|
|
size_t len;
|
|
|
|
|
|
|
|
p = strdup(str);
|
|
|
|
len = strlen(p) - strlen(strchr(p, ':'));
|
|
|
|
p[len] = '\0';
|
|
|
|
|
|
|
|
return p;
|
|
|
|
}
|
|
|
|
|
|
|
|
static const char *
|
|
|
|
right(const char *str)
|
|
|
|
{
|
|
|
|
return strchr(str, ':') + 1;
|
|
|
|
}
|
|
|
|
|
2015-11-07 04:14:10 +05:30
|
|
|
static const char *
|
2015-11-09 13:22:49 +05:30
|
|
|
normpath(char *path)
|
|
|
|
{
|
2015-11-07 04:14:10 +05:30
|
|
|
char *seg, *p;
|
2015-11-09 13:22:49 +05:30
|
|
|
|
2015-11-07 04:14:10 +05:30
|
|
|
for (p = path, seg = NULL; *p; p++) {
|
|
|
|
if (strncmp(p, "/../", 4) == 0 || strncmp(p, "/..", 4) == 0) {
|
|
|
|
memmove(seg ? seg : p, p+3, strlen(p+3) + 1);
|
2015-11-08 15:08:31 +05:30
|
|
|
return normpath(path);
|
2015-11-08 15:17:25 +05:30
|
|
|
} else if (strncmp(p, "/./", 3) == 0 || strncmp(p, "/.", 3) == 0) {
|
2015-11-07 04:14:10 +05:30
|
|
|
memmove(p, p+2, strlen(p+2) + 1);
|
2015-11-08 15:17:25 +05:30
|
|
|
} else if (strncmp(p, "//", 2) == 0 || strncmp(p, "/", 2) == 0) {
|
|
|
|
memmove(p, p+1, strlen(p+1) + 1);
|
2015-11-07 04:14:10 +05:30
|
|
|
}
|
2015-11-08 15:07:26 +05:30
|
|
|
if (*p == '/')
|
2015-11-07 04:14:10 +05:30
|
|
|
seg = p;
|
|
|
|
}
|
|
|
|
return path;
|
|
|
|
}
|
|
|
|
|
|
|
|
static char *
|
2015-11-09 13:22:49 +05:30
|
|
|
relpath(char *from, char *to)
|
|
|
|
{
|
2015-11-07 04:14:10 +05:30
|
|
|
int up;
|
|
|
|
char *p = to, *rel;
|
2015-11-09 13:22:49 +05:30
|
|
|
|
2015-11-07 04:14:10 +05:30
|
|
|
assert(from[0] == '/');
|
|
|
|
assert(to[0] == '/');
|
|
|
|
normpath(from);
|
|
|
|
normpath(to);
|
|
|
|
|
|
|
|
for (; *from == *to && *to; from++, to++) {
|
|
|
|
if (*to == '/')
|
|
|
|
p = to;
|
|
|
|
}
|
|
|
|
|
|
|
|
for (up = -1, from--; from && *from; from = strchr(from + 1, '/'), up++);
|
|
|
|
|
2020-04-19 15:08:09 +05:30
|
|
|
rel = calloc(1, 3 * up + strlen(p));
|
|
|
|
if (!rel)
|
|
|
|
return NULL;
|
2015-11-07 04:14:10 +05:30
|
|
|
|
|
|
|
while (up--)
|
|
|
|
strcat(rel, "../");
|
|
|
|
if (*p)
|
|
|
|
strcat(rel, p+1);
|
|
|
|
return rel;
|
|
|
|
}
|
|
|
|
|
2015-10-18 14:08:35 +05:30
|
|
|
static int
|
|
|
|
remove_symlinks(struct xbps_handle *xhp, xbps_array_t a, const char *grname)
|
|
|
|
{
|
|
|
|
unsigned int i, cnt;
|
2019-10-27 13:14:37 +05:30
|
|
|
struct stat st;
|
2015-10-18 14:08:35 +05:30
|
|
|
|
|
|
|
cnt = xbps_array_count(a);
|
|
|
|
for (i = 0; i < cnt; i++) {
|
|
|
|
xbps_string_t str;
|
|
|
|
char *l, *lnk;
|
|
|
|
|
|
|
|
str = xbps_array_get(a, i);
|
|
|
|
l = left(xbps_string_cstring_nocopy(str));
|
|
|
|
assert(l);
|
2015-10-31 17:24:01 +05:30
|
|
|
if (l[0] != '/') {
|
|
|
|
const char *tgt;
|
|
|
|
char *tgt_dup, *tgt_dir;
|
|
|
|
tgt = right(xbps_string_cstring_nocopy(str));
|
|
|
|
tgt_dup = strdup(tgt);
|
|
|
|
assert(tgt_dup);
|
|
|
|
tgt_dir = dirname(tgt_dup);
|
|
|
|
lnk = xbps_xasprintf("%s%s/%s", xhp->rootdir, tgt_dir, l);
|
|
|
|
free(tgt_dup);
|
|
|
|
} else {
|
|
|
|
lnk = xbps_xasprintf("%s%s", xhp->rootdir, l);
|
|
|
|
}
|
2019-10-27 13:14:37 +05:30
|
|
|
if (lstat(lnk, &st) == -1 || !S_ISLNK(st.st_mode)) {
|
|
|
|
free(lnk);
|
|
|
|
free(l);
|
|
|
|
continue;
|
|
|
|
}
|
2015-10-18 14:08:35 +05:30
|
|
|
xbps_set_cb_state(xhp, XBPS_STATE_ALTGROUP_LINK_REMOVED, 0, NULL,
|
|
|
|
"Removing '%s' alternatives group symlink: %s", grname, l);
|
|
|
|
unlink(lnk);
|
|
|
|
free(lnk);
|
|
|
|
free(l);
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
create_symlinks(struct xbps_handle *xhp, xbps_array_t a, const char *grname)
|
|
|
|
{
|
2016-09-05 04:44:34 +05:30
|
|
|
int rv;
|
|
|
|
unsigned int i, n;
|
|
|
|
char *alternative, *tok1, *tok2, *linkpath, *target, *dir, *p;
|
2015-10-18 14:08:35 +05:30
|
|
|
|
2016-09-05 04:44:34 +05:30
|
|
|
n = xbps_array_count(a);
|
2015-10-18 14:08:35 +05:30
|
|
|
|
2016-09-05 04:44:34 +05:30
|
|
|
for (i = 0; i < n; i++) {
|
|
|
|
alternative = xbps_string_cstring(xbps_array_get(a, i));
|
|
|
|
|
|
|
|
if (!(tok1 = strtok(alternative, ":")) ||
|
|
|
|
!(tok2 = strtok(NULL, ":"))) {
|
|
|
|
free(alternative);
|
|
|
|
return -1;
|
2015-11-14 11:56:27 +05:30
|
|
|
}
|
2015-11-03 01:32:37 +05:30
|
|
|
|
2016-09-05 04:44:34 +05:30
|
|
|
target = strdup(tok2);
|
|
|
|
dir = dirname(tok2);
|
|
|
|
|
|
|
|
/* add target dir to relative links */
|
|
|
|
if (tok1[0] != '/')
|
|
|
|
linkpath = xbps_xasprintf("%s/%s/%s", xhp->rootdir, dir, tok1);
|
|
|
|
else
|
|
|
|
linkpath = xbps_xasprintf("%s/%s", xhp->rootdir, tok1);
|
|
|
|
|
|
|
|
/* create target directory, necessary for dangling symlinks */
|
|
|
|
dir = xbps_xasprintf("%s/%s", xhp->rootdir, dir);
|
|
|
|
if (strcmp(dir, ".") && xbps_mkpath(dir, 0755) && errno != EEXIST) {
|
|
|
|
rv = errno;
|
|
|
|
xbps_dbg_printf(xhp,
|
|
|
|
"failed to create target dir '%s' for group '%s': %s\n",
|
|
|
|
dir, grname, strerror(errno));
|
|
|
|
free(dir);
|
|
|
|
goto err;
|
2015-10-31 17:24:01 +05:30
|
|
|
}
|
2016-09-05 04:44:34 +05:30
|
|
|
free(dir);
|
|
|
|
|
|
|
|
/* create link directory, necessary for dangling symlinks */
|
|
|
|
p = strdup(linkpath);
|
|
|
|
dir = dirname(p);
|
|
|
|
if (strcmp(dir, ".") && xbps_mkpath(dir, 0755) && errno != EEXIST) {
|
|
|
|
rv = errno;
|
|
|
|
xbps_dbg_printf(xhp,
|
|
|
|
"failed to create symlink dir '%s' for group '%s': %s\n",
|
|
|
|
dir, grname, strerror(errno));
|
|
|
|
free(p);
|
|
|
|
goto err;
|
|
|
|
}
|
|
|
|
free(p);
|
|
|
|
|
2015-10-18 14:08:35 +05:30
|
|
|
xbps_set_cb_state(xhp, XBPS_STATE_ALTGROUP_LINK_ADDED, 0, NULL,
|
2016-09-05 04:44:34 +05:30
|
|
|
"Creating '%s' alternatives group symlink: %s -> %s",
|
|
|
|
grname, tok1, target);
|
|
|
|
|
|
|
|
if (target[0] == '/') {
|
|
|
|
p = relpath(linkpath + strlen(xhp->rootdir), target);
|
|
|
|
free(target);
|
|
|
|
target = p;
|
2015-11-10 16:28:07 +05:30
|
|
|
}
|
2016-09-05 04:44:34 +05:30
|
|
|
|
|
|
|
unlink(linkpath);
|
|
|
|
if ((rv = symlink(target, linkpath)) != 0) {
|
|
|
|
xbps_dbg_printf(xhp,
|
|
|
|
"failed to create alt symlink '%s' for group '%s': %s\n",
|
|
|
|
linkpath, grname, strerror(errno));
|
|
|
|
goto err;
|
2015-10-19 20:41:34 +05:30
|
|
|
}
|
2016-09-05 04:44:34 +05:30
|
|
|
|
|
|
|
free(alternative);
|
|
|
|
free(target);
|
|
|
|
free(linkpath);
|
2015-10-18 14:08:35 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
2016-09-05 04:44:34 +05:30
|
|
|
|
|
|
|
err:
|
|
|
|
free(alternative);
|
|
|
|
free(target);
|
|
|
|
free(linkpath);
|
|
|
|
return rv;
|
2015-10-18 14:08:35 +05:30
|
|
|
}
|
|
|
|
|
2015-10-30 16:54:46 +05:30
|
|
|
int
|
|
|
|
xbps_alternatives_set(struct xbps_handle *xhp, const char *pkgname,
|
|
|
|
const char *group)
|
|
|
|
{
|
|
|
|
xbps_array_t allkeys;
|
2016-09-04 20:50:44 +05:30
|
|
|
xbps_dictionary_t alternatives, pkg_alternatives, pkgd, prevpkgd, prevpkg_alts;
|
2019-06-27 20:39:43 +05:30
|
|
|
const char *pkgver = NULL, *prevpkgname = NULL;
|
2015-10-30 16:54:46 +05:30
|
|
|
int rv = 0;
|
|
|
|
|
|
|
|
assert(xhp);
|
|
|
|
assert(pkgname);
|
|
|
|
|
|
|
|
alternatives = xbps_dictionary_get(xhp->pkgdb, "_XBPS_ALTERNATIVES_");
|
|
|
|
if (alternatives == NULL)
|
|
|
|
return ENOENT;
|
|
|
|
|
|
|
|
pkgd = xbps_pkgdb_get_pkg(xhp, pkgname);
|
|
|
|
if (pkgd == NULL)
|
|
|
|
return ENOENT;
|
|
|
|
|
|
|
|
pkg_alternatives = xbps_dictionary_get(pkgd, "alternatives");
|
|
|
|
if (!xbps_dictionary_count(pkg_alternatives))
|
|
|
|
return ENOENT;
|
|
|
|
|
2019-01-16 23:18:00 +05:30
|
|
|
if (group && !xbps_dictionary_get(pkg_alternatives, group))
|
|
|
|
return ENOENT;
|
|
|
|
|
2015-10-30 16:54:46 +05:30
|
|
|
xbps_dictionary_get_cstring_nocopy(pkgd, "pkgver", &pkgver);
|
|
|
|
|
|
|
|
allkeys = xbps_dictionary_all_keys(pkg_alternatives);
|
|
|
|
for (unsigned int i = 0; i < xbps_array_count(allkeys); i++) {
|
|
|
|
xbps_array_t array;
|
|
|
|
xbps_object_t keysym;
|
|
|
|
xbps_string_t kstr;
|
|
|
|
const char *keyname;
|
|
|
|
|
|
|
|
keysym = xbps_array_get(allkeys, i);
|
|
|
|
keyname = xbps_dictionary_keysym_cstring_nocopy(keysym);
|
|
|
|
|
2019-01-16 23:18:00 +05:30
|
|
|
if (group && strcmp(keyname, group))
|
2015-10-30 16:54:46 +05:30
|
|
|
continue;
|
|
|
|
|
|
|
|
array = xbps_dictionary_get(alternatives, keyname);
|
|
|
|
if (array == NULL)
|
|
|
|
continue;
|
|
|
|
|
2016-09-04 20:50:44 +05:30
|
|
|
/* remove symlinks from previous alternative */
|
|
|
|
xbps_array_get_cstring_nocopy(array, 0, &prevpkgname);
|
|
|
|
if (prevpkgname && strcmp(pkgname, prevpkgname) != 0) {
|
|
|
|
if ((prevpkgd = xbps_pkgdb_get_pkg(xhp, prevpkgname)) &&
|
|
|
|
(prevpkg_alts = xbps_dictionary_get(prevpkgd, "alternatives")) &&
|
|
|
|
xbps_dictionary_count(prevpkg_alts)) {
|
|
|
|
rv = remove_symlinks(xhp,
|
|
|
|
xbps_dictionary_get(prevpkg_alts, keyname),
|
|
|
|
keyname);
|
|
|
|
if (rv != 0)
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-10-30 16:54:46 +05:30
|
|
|
/* put this alternative group at the head */
|
|
|
|
xbps_remove_string_from_array(array, pkgname);
|
|
|
|
kstr = xbps_string_create_cstring(pkgname);
|
|
|
|
xbps_array_add_first(array, kstr);
|
|
|
|
xbps_object_release(kstr);
|
|
|
|
|
|
|
|
/* apply the alternatives group */
|
|
|
|
xbps_set_cb_state(xhp, XBPS_STATE_ALTGROUP_ADDED, 0, NULL,
|
|
|
|
"%s: applying '%s' alternatives group", pkgver, keyname);
|
|
|
|
rv = create_symlinks(xhp, xbps_dictionary_get(pkg_alternatives, keyname), keyname);
|
2019-01-16 23:18:00 +05:30
|
|
|
if (rv != 0 || group)
|
2015-10-30 16:54:46 +05:30
|
|
|
break;
|
|
|
|
}
|
|
|
|
xbps_object_release(allkeys);
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
2019-11-14 22:10:54 +05:30
|
|
|
static int
|
|
|
|
switch_alt_group(struct xbps_handle *xhp, const char *grpn, const char *pkgn,
|
|
|
|
xbps_dictionary_t *pkg_alternatives)
|
|
|
|
{
|
|
|
|
xbps_dictionary_t curpkgd, pkgalts;
|
|
|
|
|
|
|
|
curpkgd = xbps_pkgdb_get_pkg(xhp, pkgn);
|
|
|
|
assert(curpkgd);
|
|
|
|
|
|
|
|
xbps_set_cb_state(xhp, XBPS_STATE_ALTGROUP_SWITCHED, 0, NULL,
|
|
|
|
"Switched '%s' alternatives group to '%s'", grpn, pkgn);
|
|
|
|
pkgalts = xbps_dictionary_get(curpkgd, "alternatives");
|
|
|
|
if (pkg_alternatives) *pkg_alternatives = pkgalts;
|
|
|
|
return create_symlinks(xhp, xbps_dictionary_get(pkgalts, grpn), grpn);
|
|
|
|
}
|
|
|
|
|
2015-10-18 14:08:35 +05:30
|
|
|
int
|
|
|
|
xbps_alternatives_unregister(struct xbps_handle *xhp, xbps_dictionary_t pkgd)
|
|
|
|
{
|
|
|
|
xbps_array_t allkeys;
|
2015-10-30 16:54:46 +05:30
|
|
|
xbps_dictionary_t alternatives, pkg_alternatives;
|
2020-02-21 13:38:22 +05:30
|
|
|
const char *pkgver, *pkgname;
|
2015-11-17 21:02:12 +05:30
|
|
|
bool update = false;
|
2015-10-18 14:08:35 +05:30
|
|
|
int rv = 0;
|
|
|
|
|
2015-10-30 16:54:46 +05:30
|
|
|
assert(xhp);
|
|
|
|
|
|
|
|
alternatives = xbps_dictionary_get(xhp->pkgdb, "_XBPS_ALTERNATIVES_");
|
|
|
|
if (alternatives == NULL)
|
2015-10-18 14:08:35 +05:30
|
|
|
return 0;
|
|
|
|
|
2015-10-30 16:54:46 +05:30
|
|
|
pkg_alternatives = xbps_dictionary_get(pkgd, "alternatives");
|
|
|
|
if (!xbps_dictionary_count(pkg_alternatives))
|
|
|
|
return 0;
|
2015-10-18 14:08:35 +05:30
|
|
|
|
|
|
|
xbps_dictionary_get_cstring_nocopy(pkgd, "pkgver", &pkgver);
|
2020-02-21 13:38:22 +05:30
|
|
|
xbps_dictionary_get_cstring_nocopy(pkgd, "pkgname", &pkgname);
|
2015-10-18 14:08:35 +05:30
|
|
|
|
2015-11-17 21:02:12 +05:30
|
|
|
xbps_dictionary_get_bool(pkgd, "alternatives-update", &update);
|
|
|
|
|
2015-10-30 16:54:46 +05:30
|
|
|
allkeys = xbps_dictionary_all_keys(pkg_alternatives);
|
2015-10-18 14:08:35 +05:30
|
|
|
for (unsigned int i = 0; i < xbps_array_count(allkeys); i++) {
|
|
|
|
xbps_array_t array;
|
|
|
|
xbps_object_t keysym;
|
2019-04-22 17:38:24 +05:30
|
|
|
bool current = false;
|
2015-10-18 14:08:35 +05:30
|
|
|
const char *first = NULL, *keyname;
|
|
|
|
|
|
|
|
keysym = xbps_array_get(allkeys, i);
|
|
|
|
keyname = xbps_dictionary_keysym_cstring_nocopy(keysym);
|
|
|
|
|
2015-10-30 16:54:46 +05:30
|
|
|
array = xbps_dictionary_get(alternatives, keyname);
|
2015-10-18 14:08:35 +05:30
|
|
|
if (array == NULL)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
xbps_array_get_cstring_nocopy(array, 0, &first);
|
2019-06-19 00:17:55 +05:30
|
|
|
if (strcmp(pkgname, first) == 0) {
|
2015-10-18 14:08:35 +05:30
|
|
|
/* this pkg is the current alternative for this group */
|
2019-06-19 00:17:55 +05:30
|
|
|
current = true;
|
2015-10-18 14:08:35 +05:30
|
|
|
rv = remove_symlinks(xhp,
|
2015-10-30 16:54:46 +05:30
|
|
|
xbps_dictionary_get(pkg_alternatives, keyname),
|
2015-10-18 14:08:35 +05:30
|
|
|
keyname);
|
|
|
|
if (rv != 0)
|
|
|
|
break;
|
|
|
|
}
|
2015-11-17 21:02:12 +05:30
|
|
|
|
2019-06-11 17:24:18 +05:30
|
|
|
if (!update) {
|
2019-06-19 00:17:55 +05:30
|
|
|
xbps_set_cb_state(xhp, XBPS_STATE_ALTGROUP_REMOVED, 0, NULL,
|
|
|
|
"%s: unregistered '%s' alternatives group", pkgver, keyname);
|
2015-11-17 21:02:12 +05:30
|
|
|
xbps_remove_string_from_array(array, pkgname);
|
2019-06-11 17:24:18 +05:30
|
|
|
xbps_array_get_cstring_nocopy(array, 0, &first);
|
|
|
|
}
|
2015-11-17 21:02:12 +05:30
|
|
|
|
2015-10-18 14:08:35 +05:30
|
|
|
if (xbps_array_count(array) == 0) {
|
2015-10-30 16:54:46 +05:30
|
|
|
xbps_dictionary_remove(alternatives, keyname);
|
2019-04-22 17:38:24 +05:30
|
|
|
continue;
|
|
|
|
}
|
2015-10-18 14:08:35 +05:30
|
|
|
|
2019-06-19 00:17:55 +05:30
|
|
|
if (update || !current)
|
2019-04-22 17:38:24 +05:30
|
|
|
continue;
|
|
|
|
|
2019-06-19 00:17:55 +05:30
|
|
|
/* get the new alternative group package */
|
2019-11-14 22:10:54 +05:30
|
|
|
if (switch_alt_group(xhp, keyname, first, &pkg_alternatives) != 0)
|
2019-04-22 17:38:24 +05:30
|
|
|
break;
|
2015-10-18 14:08:35 +05:30
|
|
|
}
|
|
|
|
xbps_object_release(allkeys);
|
|
|
|
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
2019-11-14 22:10:54 +05:30
|
|
|
/*
|
|
|
|
* Prune the alternatives group from the db. This will first unregister
|
|
|
|
* it for the package and if there's no other package left providing the
|
2020-02-07 13:46:57 +05:30
|
|
|
* same, also ditch the whole group. When this is called, it is guaranteed
|
2019-11-14 22:10:54 +05:30
|
|
|
* that what is happening is an upgrade, because it's only invoked when
|
|
|
|
* the repo and installed alternatives sets differ for a specific package.
|
|
|
|
*/
|
2019-06-11 19:08:16 +05:30
|
|
|
static void
|
2019-11-14 22:10:54 +05:30
|
|
|
prune_altgroup(struct xbps_handle *xhp, xbps_dictionary_t repod,
|
2020-02-09 00:01:29 +05:30
|
|
|
const char *pkgname, const char *pkgver, const char *keyname)
|
2019-12-27 15:23:10 +05:30
|
|
|
{
|
|
|
|
const char *newpkg = NULL, *curpkg = NULL;
|
2019-11-14 22:10:54 +05:30
|
|
|
xbps_array_t array;
|
|
|
|
xbps_dictionary_t alternatives;
|
|
|
|
xbps_string_t kstr;
|
|
|
|
unsigned int grp_count;
|
|
|
|
bool current = false;
|
|
|
|
|
|
|
|
xbps_set_cb_state(xhp, XBPS_STATE_ALTGROUP_REMOVED, 0, NULL,
|
|
|
|
"%s: unregistered '%s' alternatives group", pkgver, keyname);
|
|
|
|
|
|
|
|
alternatives = xbps_dictionary_get(xhp->pkgdb, "_XBPS_ALTERNATIVES_");
|
|
|
|
assert(alternatives);
|
|
|
|
array = xbps_dictionary_get(alternatives, keyname);
|
|
|
|
|
|
|
|
/* if using alt group from another package, we won't switch anything */
|
|
|
|
xbps_array_get_cstring_nocopy(array, 0, &curpkg);
|
|
|
|
current = (strcmp(pkgname, curpkg) == 0);
|
|
|
|
|
|
|
|
/* actually prune the alt group for the current package */
|
|
|
|
xbps_remove_string_from_array(array, pkgname);
|
|
|
|
grp_count = xbps_array_count(array);
|
|
|
|
if (grp_count == 0) {
|
|
|
|
/* it was the last one, ditch the whole thing */
|
|
|
|
xbps_dictionary_remove(alternatives, keyname);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (!current) {
|
|
|
|
/* not the last one, and ours wasn't the one being used */
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (xbps_array_count(xbps_dictionary_get(repod, "run_depends")) == 0 &&
|
|
|
|
xbps_array_count(xbps_dictionary_get(repod, "shlib-requires")) == 0) {
|
|
|
|
/*
|
|
|
|
* Empty dependencies indicate a removed package (pure meta),
|
|
|
|
* use the first available group after ours has been pruned
|
|
|
|
*/
|
|
|
|
xbps_array_get_cstring_nocopy(array, 0, &newpkg);
|
|
|
|
switch_alt_group(xhp, keyname, newpkg, NULL);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Use the last group, as this indicates that a transitional metapackage
|
|
|
|
* is replacing the original and therefore a new package has registered
|
|
|
|
* a replacement group, which should be last in the array (most recent).
|
|
|
|
*/
|
|
|
|
xbps_array_get_cstring_nocopy(array, grp_count - 1, &newpkg);
|
|
|
|
|
|
|
|
/* put the new package as head */
|
|
|
|
kstr = xbps_string_create_cstring(newpkg);
|
|
|
|
xbps_remove_string_from_array(array, newpkg);
|
|
|
|
xbps_array_add_first(array, kstr);
|
|
|
|
xbps_array_get_cstring_nocopy(array, 0, &newpkg);
|
|
|
|
xbps_object_release(kstr);
|
|
|
|
|
|
|
|
switch_alt_group(xhp, keyname, newpkg, NULL);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static void
|
2020-02-21 13:38:22 +05:30
|
|
|
remove_obsoletes(struct xbps_handle *xhp, const char *pkgname, const char *pkgver,
|
2020-02-07 13:46:57 +05:30
|
|
|
xbps_dictionary_t pkgdb_alts, xbps_dictionary_t repod)
|
2019-06-11 19:08:16 +05:30
|
|
|
{
|
|
|
|
xbps_array_t allkeys;
|
2019-11-14 22:10:54 +05:30
|
|
|
xbps_dictionary_t pkgd, pkgd_alts, repod_alts;
|
2019-06-11 19:08:16 +05:30
|
|
|
|
2019-11-14 22:10:54 +05:30
|
|
|
pkgd = xbps_pkgdb_get_pkg(xhp, pkgname);
|
|
|
|
if (xbps_object_type(pkgd) != XBPS_TYPE_DICTIONARY) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
pkgd_alts = xbps_dictionary_get(pkgd, "alternatives");
|
|
|
|
repod_alts = xbps_dictionary_get(repod, "alternatives");
|
|
|
|
|
|
|
|
if (xbps_object_type(pkgd_alts) != XBPS_TYPE_DICTIONARY) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
allkeys = xbps_dictionary_all_keys(pkgd_alts);
|
2019-06-11 19:08:16 +05:30
|
|
|
for (unsigned int i = 0; i < xbps_array_count(allkeys); i++) {
|
2020-02-07 13:46:57 +05:30
|
|
|
xbps_array_t array, array2, array_repo;
|
2019-06-11 19:08:16 +05:30
|
|
|
xbps_object_t keysym;
|
2020-02-07 13:46:57 +05:30
|
|
|
const char *keyname, *first;
|
2019-06-11 19:08:16 +05:30
|
|
|
|
|
|
|
keysym = xbps_array_get(allkeys, i);
|
2019-11-14 22:10:54 +05:30
|
|
|
array = xbps_dictionary_get_keysym(pkgd_alts, keysym);
|
2019-06-11 19:08:16 +05:30
|
|
|
keyname = xbps_dictionary_keysym_cstring_nocopy(keysym);
|
|
|
|
|
2019-11-14 22:10:54 +05:30
|
|
|
array_repo = xbps_dictionary_get(repod_alts, keyname);
|
2019-06-11 19:08:16 +05:30
|
|
|
if (!xbps_array_equals(array, array_repo)) {
|
2020-02-07 13:46:57 +05:30
|
|
|
/*
|
|
|
|
* Check if current provider in pkgdb is this pkg.
|
|
|
|
*/
|
|
|
|
array2 = xbps_dictionary_get(pkgdb_alts, keyname);
|
2020-02-07 14:13:47 +05:30
|
|
|
if (array2) {
|
2020-02-07 13:46:57 +05:30
|
|
|
xbps_array_get_cstring_nocopy(array2, 0, &first);
|
|
|
|
if (strcmp(pkgname, first) == 0) {
|
2020-02-07 14:13:47 +05:30
|
|
|
remove_symlinks(xhp, array_repo, keyname);
|
2020-02-07 13:46:57 +05:30
|
|
|
}
|
|
|
|
}
|
2019-06-11 19:08:16 +05:30
|
|
|
}
|
2019-11-14 22:10:54 +05:30
|
|
|
/*
|
|
|
|
* There is nothing left in the alternatives group, which means
|
|
|
|
* the package is being upgraded and is removing it; if we don't
|
|
|
|
* prune it, the system will keep it set after removal of its
|
|
|
|
* parent package, but it will be empty and invalid...
|
|
|
|
*/
|
|
|
|
if (xbps_array_count(array_repo) == 0) {
|
|
|
|
prune_altgroup(xhp, repod, pkgname, pkgver, keyname);
|
|
|
|
}
|
2019-06-11 19:08:16 +05:30
|
|
|
}
|
2019-06-11 21:59:17 +05:30
|
|
|
xbps_object_release(allkeys);
|
2019-06-11 19:08:16 +05:30
|
|
|
}
|
|
|
|
|
2015-10-18 14:08:35 +05:30
|
|
|
int
|
2019-06-11 19:08:16 +05:30
|
|
|
xbps_alternatives_register(struct xbps_handle *xhp, xbps_dictionary_t pkg_repod)
|
2015-10-18 14:08:35 +05:30
|
|
|
{
|
|
|
|
xbps_array_t allkeys;
|
2019-11-14 22:10:54 +05:30
|
|
|
xbps_dictionary_t alternatives, pkg_alternatives;
|
2020-02-21 13:38:22 +05:30
|
|
|
const char *pkgver, *pkgname;
|
2015-10-18 14:08:35 +05:30
|
|
|
int rv = 0;
|
|
|
|
|
2015-10-30 16:54:46 +05:30
|
|
|
assert(xhp);
|
|
|
|
|
|
|
|
if (xhp->pkgdb == NULL)
|
|
|
|
return EINVAL;
|
|
|
|
|
|
|
|
alternatives = xbps_dictionary_get(xhp->pkgdb, "_XBPS_ALTERNATIVES_");
|
|
|
|
if (alternatives == NULL) {
|
|
|
|
alternatives = xbps_dictionary_create();
|
|
|
|
xbps_dictionary_set(xhp->pkgdb, "_XBPS_ALTERNATIVES_", alternatives);
|
|
|
|
xbps_object_release(alternatives);
|
|
|
|
}
|
|
|
|
alternatives = xbps_dictionary_get(xhp->pkgdb, "_XBPS_ALTERNATIVES_");
|
|
|
|
assert(alternatives);
|
2015-10-18 14:08:35 +05:30
|
|
|
|
2019-06-11 19:08:16 +05:30
|
|
|
xbps_dictionary_get_cstring_nocopy(pkg_repod, "pkgver", &pkgver);
|
2020-02-21 13:38:22 +05:30
|
|
|
xbps_dictionary_get_cstring_nocopy(pkg_repod, "pkgname", &pkgname);
|
2015-10-18 14:08:35 +05:30
|
|
|
|
2019-11-14 22:10:54 +05:30
|
|
|
/*
|
|
|
|
* Compare alternatives from pkgdb and repo and then remove obsolete
|
|
|
|
* symlinks, also remove obsolete (empty) alternatives groups.
|
|
|
|
*/
|
2020-02-07 13:46:57 +05:30
|
|
|
remove_obsoletes(xhp, pkgname, pkgver, alternatives, pkg_repod);
|
2019-11-14 22:10:54 +05:30
|
|
|
|
|
|
|
pkg_alternatives = xbps_dictionary_get(pkg_repod, "alternatives");
|
|
|
|
if (!xbps_dictionary_count(pkg_alternatives))
|
|
|
|
return 0;
|
2019-06-11 19:08:16 +05:30
|
|
|
|
2015-10-30 16:54:46 +05:30
|
|
|
allkeys = xbps_dictionary_all_keys(pkg_alternatives);
|
2015-10-18 14:08:35 +05:30
|
|
|
for (unsigned int i = 0; i < xbps_array_count(allkeys); i++) {
|
|
|
|
xbps_array_t array;
|
|
|
|
xbps_object_t keysym;
|
2019-12-27 15:23:10 +05:30
|
|
|
const char *keyname, *first = NULL;
|
2015-10-18 14:08:35 +05:30
|
|
|
|
|
|
|
keysym = xbps_array_get(allkeys, i);
|
|
|
|
keyname = xbps_dictionary_keysym_cstring_nocopy(keysym);
|
|
|
|
|
2015-10-30 16:54:46 +05:30
|
|
|
array = xbps_dictionary_get(alternatives, keyname);
|
2015-10-18 14:08:35 +05:30
|
|
|
if (array == NULL) {
|
|
|
|
array = xbps_array_create();
|
2015-10-31 16:17:58 +05:30
|
|
|
} else {
|
2019-06-11 20:04:25 +05:30
|
|
|
if (xbps_match_string_in_array(array, pkgname)) {
|
2019-06-19 00:17:55 +05:30
|
|
|
xbps_array_get_cstring_nocopy(array, 0, &first);
|
|
|
|
if (strcmp(pkgname, first)) {
|
|
|
|
/* current alternative does not match */
|
|
|
|
continue;
|
|
|
|
}
|
2019-06-11 21:43:19 +05:30
|
|
|
/* already registered, update symlinks */
|
2019-06-11 20:04:25 +05:30
|
|
|
rv = create_symlinks(xhp,
|
|
|
|
xbps_dictionary_get(pkg_alternatives, keyname),
|
|
|
|
keyname);
|
|
|
|
if (rv != 0)
|
|
|
|
break;
|
2019-06-11 21:43:19 +05:30
|
|
|
} else {
|
|
|
|
/* not registered, add provider */
|
|
|
|
xbps_array_add_cstring(array, pkgname);
|
|
|
|
xbps_set_cb_state(xhp, XBPS_STATE_ALTGROUP_ADDED, 0, NULL,
|
|
|
|
"%s: registered '%s' alternatives group", pkgver, keyname);
|
2019-06-11 20:04:25 +05:30
|
|
|
}
|
|
|
|
continue;
|
2015-10-18 14:08:35 +05:30
|
|
|
}
|
2015-10-31 16:17:58 +05:30
|
|
|
|
2015-10-18 14:08:35 +05:30
|
|
|
xbps_array_add_cstring(array, pkgname);
|
2015-10-30 16:54:46 +05:30
|
|
|
xbps_dictionary_set(alternatives, keyname, array);
|
2015-10-18 14:08:35 +05:30
|
|
|
xbps_set_cb_state(xhp, XBPS_STATE_ALTGROUP_ADDED, 0, NULL,
|
|
|
|
"%s: registered '%s' alternatives group", pkgver, keyname);
|
2019-06-11 20:28:21 +05:30
|
|
|
/* apply alternatives for this group */
|
|
|
|
rv = create_symlinks(xhp,
|
|
|
|
xbps_dictionary_get(pkg_alternatives, keyname),
|
|
|
|
keyname);
|
|
|
|
xbps_object_release(array);
|
|
|
|
if (rv != 0)
|
|
|
|
break;
|
2015-10-18 14:08:35 +05:30
|
|
|
}
|
|
|
|
xbps_object_release(allkeys);
|
|
|
|
|
|
|
|
return rv;
|
|
|
|
}
|