lib/package_alternatives.c: always use relative symlinks in alternatives

This commit adds relative link support to lib/package_alternatives.c.
Instead of creating absolute links, xbps will create relative links
to the target. This allows to follow links on systems even if the
aren't mounted on /.
This commit is contained in:
Enno Boland
2015-11-06 23:44:10 +01:00
parent 9b0cde3267
commit 635db51c27
2 changed files with 70 additions and 24 deletions

View File

@ -59,6 +59,50 @@ right(const char *str)
return strchr(str, ':') + 1;
}
static const char *
normpath(char *path) {
char *seg, *p;
reinit:
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);
goto reinit;
}
else if (strncmp(p, "/./", 3) == 0 || strncmp(p, "/.", 3) == 0) {
memmove(p, p+2, strlen(p+2) + 1);
}
else if (*p == '/')
seg = p;
}
return path;
}
static char *
relpath(char *from, char *to) {
int up;
char *p = to, *rel;
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++);
rel = calloc(3 * up + strlen(p), sizeof(char));
while (up--)
strcat(rel, "../");
if (*p)
strcat(rel, p+1);
return rel;
}
static int
remove_symlinks(struct xbps_handle *xhp, xbps_array_t a, const char *grname)
{
@ -135,6 +179,8 @@ create_symlinks(struct xbps_handle *xhp, xbps_array_t a, const char *grname)
xbps_set_cb_state(xhp, XBPS_STATE_ALTGROUP_LINK_ADDED, 0, NULL,
"Creating '%s' alternatives group symlink: %s -> %s", grname, l, tgt);
unlink(lnk);
if (tgt[0] == '/')
tgt = relpath(lnk + strlen(xhp->rootdir), tgt);
if ((rv = symlink(tgt, lnk)) != 0) {
xbps_dbg_printf(xhp, "failed to create alt symlink '%s'"
"for group '%s': %s\n", lnk, grname,