From 635db51c275e7acb49dfe005b988d9ea6b05ded1 Mon Sep 17 00:00:00 2001 From: Enno Boland Date: Fri, 6 Nov 2015 23:44:10 +0100 Subject: [PATCH 1/5] 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 /. --- lib/package_alternatives.c | 46 ++++++++++++++++++++++++++ tests/xbps/xbps-alternatives/main.sh | 48 ++++++++++++++-------------- 2 files changed, 70 insertions(+), 24 deletions(-) diff --git a/lib/package_alternatives.c b/lib/package_alternatives.c index ec991cb9..8d1512a4 100644 --- a/lib/package_alternatives.c +++ b/lib/package_alternatives.c @@ -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, diff --git a/tests/xbps/xbps-alternatives/main.sh b/tests/xbps/xbps-alternatives/main.sh index 2444c6cd..e6368941 100644 --- a/tests/xbps/xbps-alternatives/main.sh +++ b/tests/xbps/xbps-alternatives/main.sh @@ -19,8 +19,8 @@ register_one_body() { atf_check_equal $? 0 rv=1 if [ -e root/usr/bin/fileA ]; then - lnk=$(readlink root/usr/bin/file) - if [ "$lnk" = "/usr/bin/fileA" ]; then + lnk=$(readlink -f root/usr/bin/file) + if [ "$lnk" = "$PWD/root/usr/bin/fileA" ]; then rv=0 fi echo "A lnk: $lnk" @@ -46,8 +46,8 @@ register_one_dangling_body() { atf_check_equal $? 0 rv=1 if [ -h root/usr/bin/file ]; then - lnk=$(readlink root/usr/bin/file) - if [ "$lnk" = "/usr/bin/fileA" ]; then + lnk=$(readlink -f root/usr/bin/file) + if [ "$lnk" = "$PWD/root/usr/bin/fileA" ]; then rv=0 fi echo "A lnk: $lnk" @@ -137,8 +137,8 @@ register_multi_body() { atf_check_equal $? 0 rv=1 if [ -e root/usr/bin/fileA ]; then - lnk=$(readlink root/usr/bin/file) - if [ "$lnk" = "/usr/bin/fileA" ]; then + lnk=$(readlink -f root/usr/bin/file) + if [ "$lnk" = "$PWD/root/usr/bin/fileA" ]; then rv=0 fi echo "A lnk: $lnk" @@ -149,8 +149,8 @@ register_multi_body() { atf_check_equal $? 0 rv=1 if [ -e root/usr/bin/fileA -a -e root/usr/bin/fileB ]; then - lnk=$(readlink root/usr/bin/file) - if [ "$lnk" = "/usr/bin/fileA" ]; then + lnk=$(readlink -f root/usr/bin/file) + if [ "$lnk" = "$PWD/root/usr/bin/fileA" ]; then rv=0 fi echo "B lnk: $lnk" @@ -229,8 +229,8 @@ unregister_multi_body() { atf_check_equal $? 0 if [ -e root/usr/bin/fileA ]; then - lnk=$(readlink root/usr/bin/file) - if [ "$lnk" = "/usr/bin/fileA" ]; then + lnk=$(readlink -f root/usr/bin/file) + if [ "$lnk" = "$PWD/root/usr/bin/fileA" ]; then rv=0 fi echo "A lnk: $lnk" @@ -247,8 +247,8 @@ unregister_multi_body() { atf_check_equal $? 0 if [ -e root/usr/bin/fileB ]; then - lnk=$(readlink root/usr/bin/file) - if [ "$lnk" = "/usr/bin/fileB" ]; then + lnk=$(readlink -f root/usr/bin/file) + if [ "$lnk" = "$PWD/root/usr/bin/fileB" ]; then rv=0 fi echo "A lnk: $lnk" @@ -287,8 +287,8 @@ set_pkg_body() { rv=1 if [ -e root/usr/bin/B1 ]; then - lnk=$(readlink root/usr/bin/1) - if [ "$lnk" = "/usr/bin/B1" ]; then + lnk=$(readlink -f root/usr/bin/1) + if [ "$lnk" = "$PWD/root/usr/bin/B1" ]; then rv=0 fi echo "A lnk: $lnk" @@ -297,8 +297,8 @@ set_pkg_body() { rv=1 if [ -e root/usr/bin/B2 ]; then - lnk=$(readlink root/usr/bin/2) - if [ "$lnk" = "/usr/bin/B2" ]; then + lnk=$(readlink -f root/usr/bin/2) + if [ "$lnk" = "$PWD/root/usr/bin/B2" ]; then rv=0 fi echo "A lnk: $lnk" @@ -310,8 +310,8 @@ set_pkg_body() { rv=1 if [ -e root/usr/bin/A1 ]; then - lnk=$(readlink root/usr/bin/1) - if [ "$lnk" = "/usr/bin/A1" ]; then + lnk=$(readlink -f root/usr/bin/1) + if [ "$lnk" = "$PWD/root/usr/bin/A1" ]; then rv=0 fi echo "A lnk: $lnk" @@ -320,8 +320,8 @@ set_pkg_body() { rv=1 if [ -e root/usr/bin/A2 ]; then - lnk=$(readlink root/usr/bin/2) - if [ "$lnk" = "/usr/bin/A2" ]; then + lnk=$(readlink -f root/usr/bin/2) + if [ "$lnk" = "$PWD/root/usr/bin/A2" ]; then rv=0 fi echo "A lnk: $lnk" @@ -353,8 +353,8 @@ set_pkg_group_body() { rv=1 if [ -e root/usr/bin/B1 ]; then - lnk=$(readlink root/usr/bin/1) - if [ "$lnk" = "/usr/bin/A1" ]; then + lnk=$(readlink -f root/usr/bin/1) + if [ "$lnk" = "$PWD/root/usr/bin/A1" ]; then rv=0 fi echo "A lnk: $lnk" @@ -363,8 +363,8 @@ set_pkg_group_body() { rv=1 if [ -e root/usr/bin/B2 ]; then - lnk=$(readlink root/usr/bin/2) - if [ "$lnk" = "/usr/bin/B2" ]; then + lnk=$(readlink -f root/usr/bin/2) + if [ "$lnk" = "$PWD/root/usr/bin/B2" ]; then rv=0 fi echo "A lnk: $lnk" From 8bd2808108cb13c12fbccb97e4e0bf81e7e585ba Mon Sep 17 00:00:00 2001 From: Enno Boland Date: Sun, 8 Nov 2015 10:37:26 +0100 Subject: [PATCH 2/5] lib/package_alternatives.c: fix wrong /. handling for normalizing paths. --- lib/package_alternatives.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/package_alternatives.c b/lib/package_alternatives.c index 8d1512a4..d69db12b 100644 --- a/lib/package_alternatives.c +++ b/lib/package_alternatives.c @@ -71,7 +71,7 @@ reinit: else if (strncmp(p, "/./", 3) == 0 || strncmp(p, "/.", 3) == 0) { memmove(p, p+2, strlen(p+2) + 1); } - else if (*p == '/') + if (*p == '/') seg = p; } return path; From 19a65158d9c325a3b3ee4deb59d81e2c0b73a74c Mon Sep 17 00:00:00 2001 From: Enno Boland Date: Sun, 8 Nov 2015 10:38:31 +0100 Subject: [PATCH 3/5] lib/package_alternatives.c: get rid of goto in normpath. --- lib/package_alternatives.c | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/lib/package_alternatives.c b/lib/package_alternatives.c index d69db12b..390cf29c 100644 --- a/lib/package_alternatives.c +++ b/lib/package_alternatives.c @@ -62,11 +62,10 @@ right(const char *str) 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; + return normpath(path); } else if (strncmp(p, "/./", 3) == 0 || strncmp(p, "/.", 3) == 0) { memmove(p, p+2, strlen(p+2) + 1); From 78e3bde2692e57c31917df517980e88bc120e958 Mon Sep 17 00:00:00 2001 From: Enno Boland Date: Sun, 8 Nov 2015 10:47:25 +0100 Subject: [PATCH 4/5] lib/package_alternatives.c: Handle // cases. --- lib/package_alternatives.c | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/lib/package_alternatives.c b/lib/package_alternatives.c index 390cf29c..36228e5c 100644 --- a/lib/package_alternatives.c +++ b/lib/package_alternatives.c @@ -66,9 +66,10 @@ normpath(char *path) { if (strncmp(p, "/../", 4) == 0 || strncmp(p, "/..", 4) == 0) { memmove(seg ? seg : p, p+3, strlen(p+3) + 1); return normpath(path); - } - else if (strncmp(p, "/./", 3) == 0 || strncmp(p, "/.", 3) == 0) { + } else if (strncmp(p, "/./", 3) == 0 || strncmp(p, "/.", 3) == 0) { memmove(p, p+2, strlen(p+2) + 1); + } else if (strncmp(p, "//", 2) == 0 || strncmp(p, "/", 2) == 0) { + memmove(p, p+1, strlen(p+1) + 1); } if (*p == '/') seg = p; From 2927e58df21d1a4fd016f41ff548b05cff57aef4 Mon Sep 17 00:00:00 2001 From: Enno Boland Date: Mon, 9 Nov 2015 08:52:49 +0100 Subject: [PATCH 5/5] lib/package_alternatives.c: fix style --- lib/package_alternatives.c | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/lib/package_alternatives.c b/lib/package_alternatives.c index 36228e5c..6a3539f0 100644 --- a/lib/package_alternatives.c +++ b/lib/package_alternatives.c @@ -60,8 +60,10 @@ right(const char *str) } static const char * -normpath(char *path) { +normpath(char *path) +{ char *seg, *p; + 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); @@ -79,9 +81,11 @@ normpath(char *path) { static char * -relpath(char *from, char *to) { +relpath(char *from, char *to) +{ int up; char *p = to, *rel; + assert(from[0] == '/'); assert(to[0] == '/'); normpath(from);