Alternatives framework for xbps (2/2).

See xbps-alternatives(1) for more information.

Thanks to all who helped to design this and for fixing grammar in
the manual page.
This commit is contained in:
Juan RP
2015-10-30 12:24:46 +01:00
parent aafc85d494
commit cb857dfc27
18 changed files with 749 additions and 332 deletions

View File

@@ -32,7 +32,7 @@
#include "xbps_api_impl.h"
/*
* Alternatives framework for xbps.
* XXX TODO: relative symlinks.
*/
static char *
left(const char *str)
@@ -53,55 +53,6 @@ right(const char *str)
return strchr(str, ':') + 1;
}
#if 0
static int
make_symlink(const char *target, const char *link)
{
char *t, *l, *tdir, *ldir;
tdir = strdup(target);
assert(tdir);
ldir = strdup(link);
assert(ldir);
}
#endif
static void
xbps_alternatives_init(struct xbps_handle *xhp)
{
char *plist;
if (xbps_object_type(xhp->alternatives) == XBPS_TYPE_DICTIONARY)
return;
plist = xbps_xasprintf("%s/%s", xhp->metadir, XBPS_ALTERNATIVES);
xhp->alternatives = xbps_dictionary_internalize_from_file(plist);
free(plist);
if (xhp->alternatives == NULL)
xhp->alternatives = xbps_dictionary_create();
}
int
xbps_alternatives_flush(struct xbps_handle *xhp)
{
char *plist;
if (xbps_object_type(xhp->alternatives) != XBPS_TYPE_DICTIONARY)
return 0;
/* ... and then write dictionary to disk */
plist = xbps_xasprintf("%s/%s", xhp->metadir, XBPS_ALTERNATIVES);
if (!xbps_dictionary_externalize_to_file(xhp->alternatives, plist)) {
free(plist);
return EINVAL;
}
free(plist);
return 0;
}
static int
remove_symlinks(struct xbps_handle *xhp, xbps_array_t a, const char *grname)
{
@@ -162,26 +113,92 @@ create_symlinks(struct xbps_handle *xhp, xbps_array_t a, const char *grname)
return 0;
}
int
xbps_alternatives_set(struct xbps_handle *xhp, const char *pkgname,
const char *group)
{
xbps_array_t allkeys;
xbps_dictionary_t alternatives, pkg_alternatives, pkgd;
const char *pkgver;
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;
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);
if (group && strcmp(keyname, group)) {
rv = ENOENT;
continue;
}
array = xbps_dictionary_get(alternatives, keyname);
if (array == NULL)
continue;
/* 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);
if (rv != 0)
break;
}
xbps_object_release(allkeys);
return rv;
}
int
xbps_alternatives_unregister(struct xbps_handle *xhp, xbps_dictionary_t pkgd)
{
xbps_array_t allkeys;
xbps_dictionary_t alternatives;
xbps_dictionary_t alternatives, pkg_alternatives;
const char *pkgver;
char *pkgname;
int rv = 0;
alternatives = xbps_dictionary_get(pkgd, "alternatives");
if (!xbps_dictionary_count(alternatives))
assert(xhp);
alternatives = xbps_dictionary_get(xhp->pkgdb, "_XBPS_ALTERNATIVES_");
if (alternatives == NULL)
return 0;
xbps_alternatives_init(xhp);
pkg_alternatives = xbps_dictionary_get(pkgd, "alternatives");
if (!xbps_dictionary_count(pkg_alternatives))
return 0;
xbps_dictionary_get_cstring_nocopy(pkgd, "pkgver", &pkgver);
if ((pkgname = xbps_pkg_name(pkgver)) == NULL)
return EINVAL;
allkeys = xbps_dictionary_all_keys(alternatives);
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;
@@ -190,7 +207,7 @@ xbps_alternatives_unregister(struct xbps_handle *xhp, xbps_dictionary_t pkgd)
keysym = xbps_array_get(allkeys, i);
keyname = xbps_dictionary_keysym_cstring_nocopy(keysym);
array = xbps_dictionary_get(xhp->alternatives, keyname);
array = xbps_dictionary_get(alternatives, keyname);
if (array == NULL)
continue;
@@ -198,7 +215,7 @@ xbps_alternatives_unregister(struct xbps_handle *xhp, xbps_dictionary_t pkgd)
if (strcmp(pkgname, first) == 0) {
/* this pkg is the current alternative for this group */
rv = remove_symlinks(xhp,
xbps_dictionary_get(alternatives, keyname),
xbps_dictionary_get(pkg_alternatives, keyname),
keyname);
if (rv != 0)
break;
@@ -207,7 +224,7 @@ xbps_alternatives_unregister(struct xbps_handle *xhp, xbps_dictionary_t pkgd)
"%s: unregistered '%s' alternatives group", pkgver, keyname);
xbps_remove_string_from_array(array, pkgname);
if (xbps_array_count(array) == 0) {
xbps_dictionary_remove(xhp->alternatives, keyname);
xbps_dictionary_remove(alternatives, keyname);
} else {
xbps_dictionary_t curpkgd;
@@ -217,9 +234,9 @@ xbps_alternatives_unregister(struct xbps_handle *xhp, xbps_dictionary_t pkgd)
assert(curpkgd);
xbps_set_cb_state(xhp, XBPS_STATE_ALTGROUP_SWITCHED, 0, NULL,
"Switched '%s' alternatives group to '%s'", keyname, first);
alternatives = xbps_dictionary_get(curpkgd, "alternatives");
pkg_alternatives = xbps_dictionary_get(curpkgd, "alternatives");
rv = create_symlinks(xhp,
xbps_dictionary_get(alternatives, keyname),
xbps_dictionary_get(pkg_alternatives, keyname),
keyname);
if (rv != 0)
break;
@@ -236,23 +253,35 @@ int
xbps_alternatives_register(struct xbps_handle *xhp, xbps_dictionary_t pkgd)
{
xbps_array_t allkeys;
xbps_dictionary_t alternatives;
xbps_dictionary_t alternatives, pkg_alternatives;
const char *pkgver;
char *pkgname;
int rv = 0;
alternatives = xbps_dictionary_get(pkgd, "alternatives");
if (!xbps_dictionary_count(alternatives))
assert(xhp);
if (xhp->pkgdb == NULL)
return EINVAL;
pkg_alternatives = xbps_dictionary_get(pkgd, "alternatives");
if (!xbps_dictionary_count(pkg_alternatives))
return 0;
xbps_alternatives_init(xhp);
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);
xbps_dictionary_get_cstring_nocopy(pkgd, "pkgver", &pkgver);
pkgname = xbps_pkg_name(pkgver);
if (pkgname == NULL)
return EINVAL;
allkeys = xbps_dictionary_all_keys(alternatives);
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;
@@ -262,19 +291,19 @@ xbps_alternatives_register(struct xbps_handle *xhp, xbps_dictionary_t pkgd)
keysym = xbps_array_get(allkeys, i);
keyname = xbps_dictionary_keysym_cstring_nocopy(keysym);
array = xbps_dictionary_get(xhp->alternatives, keyname);
array = xbps_dictionary_get(alternatives, keyname);
if (array == NULL) {
alloc = true;
array = xbps_array_create();
}
xbps_array_add_cstring(array, pkgname);
xbps_dictionary_set(xhp->alternatives, keyname, array);
xbps_dictionary_set(alternatives, keyname, array);
xbps_set_cb_state(xhp, XBPS_STATE_ALTGROUP_ADDED, 0, NULL,
"%s: registered '%s' alternatives group", pkgver, keyname);
if (alloc) {
/* apply alternatives for this group */
rv = create_symlinks(xhp,
xbps_dictionary_get(alternatives, keyname),
xbps_dictionary_get(pkg_alternatives, keyname),
keyname);
xbps_object_release(array);
if (rv != 0)

View File

@@ -208,6 +208,7 @@ xbps_pkgdb_init(struct xbps_handle *xhp)
xbps_dbg_printf(xhp, "[pkgdb] pkgdb_map_vpkgs %s\n", strerror(rv));
return rv;
}
assert(xhp->pkgdb);
xbps_dbg_printf(xhp, "[pkgdb] initialized ok.\n");
return 0;

View File

@@ -1,5 +1,5 @@
/*-
* Copyright (c) 2008-2013 Juan Romero Pardines.
* Copyright (c) 2008-2015 Juan Romero Pardines.
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
@@ -66,6 +66,9 @@ array_foreach_thread(void *arg)
if (xbps_object_type(thd->dict) == XBPS_TYPE_DICTIONARY) {
pkgd = xbps_dictionary_get_keysym(thd->dict, obj);
key = xbps_dictionary_keysym_cstring_nocopy(obj);
/* ignore internal objs */
if (strncmp(key, "_XBPS_", 6) == 0)
continue;
} else {
pkgd = obj;
key = NULL;
@@ -148,6 +151,9 @@ xbps_array_foreach_cb(struct xbps_handle *xhp,
if (xbps_object_type(dict) == XBPS_TYPE_DICTIONARY) {
pkgd = xbps_dictionary_get_keysym(dict, obj);
key = xbps_dictionary_keysym_cstring_nocopy(obj);
/* ignore internal objs */
if (strncmp(key, "_XBPS_", 6) == 0)
continue;
} else {
pkgd = obj;
key = NULL;

View File

@@ -350,10 +350,6 @@ xbps_transaction_commit(struct xbps_handle *xhp)
goto out;
}
}
/* flush changes to the alternatives framework */
if ((rv = xbps_alternatives_flush(xhp)) != 0)
goto out;
/* if there are no packages to install or update we are done */
if (!xbps_dictionary_get(xhp->transd, "total-update-pkgs") &&
!xbps_dictionary_get(xhp->transd, "total-install-pkgs"))

View File

@@ -260,7 +260,8 @@ xbps_transaction_update_packages(struct xbps_handle *xhp)
while ((obj = xbps_object_iterator_next(iter))) {
hold = false;
pkgd = xbps_dictionary_get_keysym(xhp->pkgdb, obj);
xbps_dictionary_get_cstring_nocopy(pkgd, "pkgver", &pkgver);
if (!xbps_dictionary_get_cstring_nocopy(pkgd, "pkgver", &pkgver))
continue;
xbps_dictionary_get_bool(pkgd, "hold", &hold);
if (hold) {
xbps_dbg_printf(xhp, "[rpool] package `%s' "

View File

@@ -84,7 +84,9 @@ collect_shlibs(struct xbps_handle *xhp, xbps_array_t pkgs, bool req)
while ((obj = xbps_object_iterator_next(iter))) {
char *pkgname;
xbps_dictionary_get_cstring_nocopy(obj, "pkgver", &pkgver);
if (!xbps_dictionary_get_cstring_nocopy(obj, "pkgver", &pkgver))
continue;
pkgname = xbps_pkg_name(pkgver);
assert(pkgname);
xbps_dictionary_set(pd, pkgname, obj);