37#include "xbps_api_impl.h"
56 return strndup(str, e - str);
62 const char *p = strchr(str,
':');
73 for (p = path, seg = NULL; *p; p++) {
74 if (strncmp(p,
"/../", 4) == 0 || strncmp(p,
"/..", 4) == 0) {
75 memmove(seg ? seg : p, p+3, strlen(p+3) + 1);
76 return normpath(path);
77 }
else if (strncmp(p,
"/./", 3) == 0 || strncmp(p,
"/.", 3) == 0) {
78 memmove(p, p+2, strlen(p+2) + 1);
79 }
else if (strncmp(p,
"//", 2) == 0 || strncmp(p,
"/", 2) == 0) {
80 memmove(p, p+1, strlen(p+1) + 1);
89relpath(
char *from,
char *to)
94 assert(from[0] ==
'/');
99 for (; *from == *to && *to; from++, to++) {
104 for (up = -1, from--; from && *from; from = strchr(from + 1,
'/'), up++);
106 rel = calloc(1, 3 * up + strlen(p) + 1);
118remove_symlinks(
struct xbps_handle *xhp, xbps_array_t a,
const char *grname)
123 cnt = xbps_array_count(a);
124 for (i = 0; i < cnt; i++) {
128 str = xbps_array_get(a, i);
129 l = left(xbps_string_cstring_nocopy(str));
133 char *tgt_dup, *tgt_dir;
134 tgt = right(xbps_string_cstring_nocopy(str));
136 tgt_dup = strdup(tgt);
138 tgt_dir = dirname(tgt_dup);
144 if (lstat(lnk, &st) == -1 || !S_ISLNK(st.st_mode)) {
149 xbps_set_cb_state(xhp, XBPS_STATE_ALTGROUP_LINK_REMOVED, 0, NULL,
150 "Removing '%s' alternatives group symlink: %s", grname, l);
160create_symlinks(
struct xbps_handle *xhp, xbps_array_t a,
const char *grname)
164 char *alternative, *tok1, *tok2, *linkpath, *target, *dir, *p;
166 n = xbps_array_count(a);
168 for (i = 0; i < n; i++) {
169 alternative = xbps_string_cstring(xbps_array_get(a, i));
171 if (!(tok1 = strtok(alternative,
":")) ||
172 !(tok2 = strtok(NULL,
":"))) {
177 target = strdup(tok2);
188 if (strcmp(dir,
".") &&
xbps_mkpath(dir, 0755) && errno != EEXIST) {
191 "failed to create target dir '%s' for group '%s': %s\n",
192 dir, grname, strerror(errno));
199 p = strdup(linkpath);
201 if (strcmp(dir,
".") &&
xbps_mkpath(dir, 0755) && errno != EEXIST) {
204 "failed to create symlink dir '%s' for group '%s': %s\n",
205 dir, grname, strerror(errno));
211 xbps_set_cb_state(xhp, XBPS_STATE_ALTGROUP_LINK_ADDED, 0, NULL,
212 "Creating '%s' alternatives group symlink: %s -> %s",
213 grname, tok1, target);
215 if (target[0] ==
'/') {
216 p = relpath(linkpath + strlen(xhp->
rootdir), target);
222 if ((rv = symlink(target, linkpath)) != 0) {
224 "failed to create alt symlink '%s' for group '%s': %s\n",
225 linkpath, grname, strerror(errno));
247 xbps_array_t allkeys;
248 xbps_dictionary_t alternatives, pkg_alternatives, pkgd, prevpkgd, prevpkg_alts;
249 const char *pkgver = NULL, *prevpkgname = NULL;
255 alternatives = xbps_dictionary_get(xhp->
pkgdb,
"_XBPS_ALTERNATIVES_");
256 if (alternatives == NULL)
263 pkg_alternatives = xbps_dictionary_get(pkgd,
"alternatives");
264 if (!xbps_dictionary_count(pkg_alternatives))
267 if (group && !xbps_dictionary_get(pkg_alternatives, group))
270 xbps_dictionary_get_cstring_nocopy(pkgd,
"pkgver", &pkgver);
272 allkeys = xbps_dictionary_all_keys(pkg_alternatives);
273 for (
unsigned int i = 0; i < xbps_array_count(allkeys); i++) {
275 xbps_object_t keysym;
279 keysym = xbps_array_get(allkeys, i);
280 keyname = xbps_dictionary_keysym_cstring_nocopy(keysym);
282 if (group && strcmp(keyname, group))
285 array = xbps_dictionary_get(alternatives, keyname);
290 xbps_array_get_cstring_nocopy(array, 0, &prevpkgname);
291 if (prevpkgname && strcmp(pkgname, prevpkgname) != 0) {
293 (prevpkg_alts = xbps_dictionary_get(prevpkgd,
"alternatives")) &&
294 xbps_dictionary_count(prevpkg_alts)) {
295 rv = remove_symlinks(xhp,
296 xbps_dictionary_get(prevpkg_alts, keyname),
305 kstr = xbps_string_create_cstring(pkgname);
306 xbps_array_add_first(array, kstr);
307 xbps_object_release(kstr);
310 xbps_set_cb_state(xhp, XBPS_STATE_ALTGROUP_ADDED, 0, NULL,
311 "%s: applying '%s' alternatives group", pkgver, keyname);
312 rv = create_symlinks(xhp, xbps_dictionary_get(pkg_alternatives, keyname), keyname);
313 if (rv != 0 || group)
316 xbps_object_release(allkeys);
321switch_alt_group(
struct xbps_handle *xhp,
const char *grpn,
const char *pkgn,
322 xbps_dictionary_t *pkg_alternatives)
324 xbps_dictionary_t curpkgd, pkgalts;
329 xbps_set_cb_state(xhp, XBPS_STATE_ALTGROUP_SWITCHED, 0, NULL,
330 "Switched '%s' alternatives group to '%s'", grpn, pkgn);
331 pkgalts = xbps_dictionary_get(curpkgd,
"alternatives");
332 if (pkg_alternatives) *pkg_alternatives = pkgalts;
333 return create_symlinks(xhp, xbps_dictionary_get(pkgalts, grpn), grpn);
339 xbps_array_t allkeys;
340 xbps_dictionary_t alternatives, pkg_alternatives;
341 const char *pkgver, *pkgname;
347 alternatives = xbps_dictionary_get(xhp->
pkgdb,
"_XBPS_ALTERNATIVES_");
348 if (alternatives == NULL)
351 pkg_alternatives = xbps_dictionary_get(pkgd,
"alternatives");
352 if (!xbps_dictionary_count(pkg_alternatives))
355 xbps_dictionary_get_cstring_nocopy(pkgd,
"pkgver", &pkgver);
356 xbps_dictionary_get_cstring_nocopy(pkgd,
"pkgname", &pkgname);
358 xbps_dictionary_get_bool(pkgd,
"alternatives-update", &update);
360 allkeys = xbps_dictionary_all_keys(pkg_alternatives);
361 for (
unsigned int i = 0; i < xbps_array_count(allkeys); i++) {
363 xbps_object_t keysym;
364 bool current =
false;
365 const char *first = NULL, *keyname;
367 keysym = xbps_array_get(allkeys, i);
368 keyname = xbps_dictionary_keysym_cstring_nocopy(keysym);
370 array = xbps_dictionary_get(alternatives, keyname);
374 xbps_array_get_cstring_nocopy(array, 0, &first);
375 if (strcmp(pkgname, first) == 0) {
378 rv = remove_symlinks(xhp,
379 xbps_dictionary_get(pkg_alternatives, keyname),
386 xbps_set_cb_state(xhp, XBPS_STATE_ALTGROUP_REMOVED, 0, NULL,
387 "%s: unregistered '%s' alternatives group", pkgver, keyname);
389 xbps_array_get_cstring_nocopy(array, 0, &first);
392 if (xbps_array_count(array) == 0) {
393 xbps_dictionary_remove(alternatives, keyname);
397 if (update || !current)
401 if (switch_alt_group(xhp, keyname, first, &pkg_alternatives) != 0)
404 xbps_object_release(allkeys);
417prune_altgroup(
struct xbps_handle *xhp, xbps_dictionary_t repod,
418 const char *pkgname,
const char *pkgver,
const char *keyname)
420 const char *newpkg = NULL, *curpkg = NULL;
422 xbps_dictionary_t alternatives;
424 unsigned int grp_count;
425 bool current =
false;
427 xbps_set_cb_state(xhp, XBPS_STATE_ALTGROUP_REMOVED, 0, NULL,
428 "%s: unregistered '%s' alternatives group", pkgver, keyname);
430 alternatives = xbps_dictionary_get(xhp->
pkgdb,
"_XBPS_ALTERNATIVES_");
431 assert(alternatives);
432 array = xbps_dictionary_get(alternatives, keyname);
435 xbps_array_get_cstring_nocopy(array, 0, &curpkg);
436 current = (strcmp(pkgname, curpkg) == 0);
440 grp_count = xbps_array_count(array);
441 if (grp_count == 0) {
443 xbps_dictionary_remove(alternatives, keyname);
451 if (xbps_array_count(xbps_dictionary_get(repod,
"run_depends")) == 0 &&
452 xbps_array_count(xbps_dictionary_get(repod,
"shlib-requires")) == 0) {
457 xbps_array_get_cstring_nocopy(array, 0, &newpkg);
458 switch_alt_group(xhp, keyname, newpkg, NULL);
467 xbps_array_get_cstring_nocopy(array, grp_count - 1, &newpkg);
470 kstr = xbps_string_create_cstring(newpkg);
472 xbps_array_add_first(array, kstr);
473 xbps_array_get_cstring_nocopy(array, 0, &newpkg);
474 xbps_object_release(kstr);
476 switch_alt_group(xhp, keyname, newpkg, NULL);
481remove_obsoletes(
struct xbps_handle *xhp,
const char *pkgname,
const char *pkgver,
482 xbps_dictionary_t pkgdb_alts, xbps_dictionary_t repod)
484 xbps_array_t allkeys;
485 xbps_dictionary_t pkgd, pkgd_alts, repod_alts;
488 if (xbps_object_type(pkgd) != XBPS_TYPE_DICTIONARY) {
492 pkgd_alts = xbps_dictionary_get(pkgd,
"alternatives");
493 repod_alts = xbps_dictionary_get(repod,
"alternatives");
495 if (xbps_object_type(pkgd_alts) != XBPS_TYPE_DICTIONARY) {
499 allkeys = xbps_dictionary_all_keys(pkgd_alts);
500 for (
unsigned int i = 0; i < xbps_array_count(allkeys); i++) {
501 xbps_array_t array, array2, array_repo;
502 xbps_object_t keysym;
503 const char *keyname, *first = NULL;
505 keysym = xbps_array_get(allkeys, i);
506 array = xbps_dictionary_get_keysym(pkgd_alts, keysym);
507 keyname = xbps_dictionary_keysym_cstring_nocopy(keysym);
509 array_repo = xbps_dictionary_get(repod_alts, keyname);
510 if (!xbps_array_equals(array, array_repo)) {
514 array2 = xbps_dictionary_get(pkgdb_alts, keyname);
516 xbps_array_get_cstring_nocopy(array2, 0, &first);
517 if (strcmp(pkgname, first) == 0) {
518 remove_symlinks(xhp, array_repo, keyname);
528 if (xbps_array_count(array_repo) == 0) {
529 prune_altgroup(xhp, repod, pkgname, pkgver, keyname);
532 xbps_object_release(allkeys);
538 xbps_array_t allkeys;
539 xbps_dictionary_t alternatives, pkg_alternatives;
540 const char *pkgver, *pkgname;
545 if (xhp->
pkgdb == NULL)
548 alternatives = xbps_dictionary_get(xhp->
pkgdb,
"_XBPS_ALTERNATIVES_");
549 if (alternatives == NULL) {
550 alternatives = xbps_dictionary_create();
551 xbps_dictionary_set(xhp->
pkgdb,
"_XBPS_ALTERNATIVES_", alternatives);
552 xbps_object_release(alternatives);
554 alternatives = xbps_dictionary_get(xhp->
pkgdb,
"_XBPS_ALTERNATIVES_");
555 assert(alternatives);
557 xbps_dictionary_get_cstring_nocopy(pkg_repod,
"pkgver", &pkgver);
558 xbps_dictionary_get_cstring_nocopy(pkg_repod,
"pkgname", &pkgname);
564 remove_obsoletes(xhp, pkgname, pkgver, alternatives, pkg_repod);
566 pkg_alternatives = xbps_dictionary_get(pkg_repod,
"alternatives");
567 if (!xbps_dictionary_count(pkg_alternatives))
570 allkeys = xbps_dictionary_all_keys(pkg_alternatives);
571 for (
unsigned int i = 0; i < xbps_array_count(allkeys); i++) {
573 xbps_object_t keysym;
574 const char *keyname, *first = NULL;
576 keysym = xbps_array_get(allkeys, i);
577 keyname = xbps_dictionary_keysym_cstring_nocopy(keysym);
579 array = xbps_dictionary_get(alternatives, keyname);
581 array = xbps_array_create();
584 xbps_array_get_cstring_nocopy(array, 0, &first);
585 if (strcmp(pkgname, first)) {
590 rv = create_symlinks(xhp,
591 xbps_dictionary_get(pkg_alternatives, keyname),
597 xbps_array_add_cstring(array, pkgname);
598 xbps_set_cb_state(xhp, XBPS_STATE_ALTGROUP_ADDED, 0, NULL,
599 "%s: registered '%s' alternatives group", pkgver, keyname);
604 xbps_array_add_cstring(array, pkgname);
605 xbps_dictionary_set(alternatives, keyname, array);
606 xbps_set_cb_state(xhp, XBPS_STATE_ALTGROUP_ADDED, 0, NULL,
607 "%s: registered '%s' alternatives group", pkgver, keyname);
609 rv = create_symlinks(xhp,
610 xbps_dictionary_get(pkg_alternatives, keyname),
612 xbps_object_release(array);
616 xbps_object_release(allkeys);
int xbps_alternatives_register(struct xbps_handle *xhp, xbps_dictionary_t pkg_repod)
int xbps_alternatives_set(struct xbps_handle *xhp, const char *pkgname, const char *group)
int xbps_alternatives_unregister(struct xbps_handle *xhp, xbps_dictionary_t pkgd)
char rootdir[XBPS_MAXPATH]
Generic XBPS structure handler for initialization.
void xbps_dbg_printf(const char *fmt,...)
Prints debug messages to stderr.
xbps_dictionary_t xbps_pkgdb_get_pkg(struct xbps_handle *xhp, const char *pkg)
bool xbps_match_string_in_array(xbps_array_t array, const char *val)
char * xbps_xasprintf(const char *fmt,...) __attribute__((format(printf
int xbps_mkpath(const char *path, mode_t mode)
bool xbps_remove_string_from_array(xbps_array_t array, const char *str)