libxbps: remove dangling symlinks properly.

This fixes removal of packages that contain multiple levels
of dangling symlinks, i.e faenza-icon-theme and probably others.

Close #23
This commit is contained in:
Juan RP 2019-05-17 08:19:21 +02:00 committed by Duncan Overbruck
parent 56aa77d51b
commit 9e2c00ee8b
3 changed files with 31 additions and 22 deletions

View File

@ -190,7 +190,10 @@ remove_pkg_files(struct xbps_handle *xhp,
bool found; bool found;
xbps_dictionary_get_cstring_nocopy(obj, "file", &file); xbps_dictionary_get_cstring_nocopy(obj, "file", &file);
snprintf(path, sizeof(path), "%s/%s", xhp->rootdir, file); if (strcmp(xhp->rootdir, "/") == 0)
snprintf(path, sizeof(path), "%s", file);
else
snprintf(path, sizeof(path), "%s%s", xhp->rootdir, file);
if ((strcmp(key, "files") == 0) || if ((strcmp(key, "files") == 0) ||
(strcmp(key, "conf_files") == 0)) { (strcmp(key, "conf_files") == 0)) {

View File

@ -498,7 +498,7 @@ char *
xbps_symlink_target(struct xbps_handle *xhp, const char *path, const char *tgt) xbps_symlink_target(struct xbps_handle *xhp, const char *path, const char *tgt)
{ {
struct stat sb; struct stat sb;
char *res = NULL, *lnk = NULL; char *res = NULL, *lnk = NULL, *p = NULL, *dname = NULL;
ssize_t r; ssize_t r;
if (lstat(path, &sb) == -1) if (lstat(path, &sb) == -1)
@ -522,38 +522,41 @@ xbps_symlink_target(struct xbps_handle *xhp, const char *path, const char *tgt)
return lnk; return lnk;
} }
if (strstr(lnk, "./") || lnk[0] != '/') { if (strstr(lnk, "./")) {
char *p, *p1, *dname, buf[PATH_MAX]; /* contains references to relative paths */
/* relative */ p = realpath(path, NULL);
if (p == NULL) {
/* dangling symlink, use target */
return strdup(tgt);
}
if (strcmp(xhp->rootdir, "/") == 0) {
res = p;
} else {
res = strdup(p + strlen(xhp->rootdir));
free(p);
}
free(lnk);
} else if (lnk[0] != '/') {
/* relative path */
p = strdup(path); p = strdup(path);
assert(p); assert(p);
dname = dirname(p); dname = dirname(p);
assert(dname); assert(dname);
snprintf(buf, sizeof(buf), "%s/%s", dname, lnk);
free(p);
p = xbps_sanitize_path(buf);
assert(p);
if ((strstr(p, "./")) && (p1 = realpath(p, NULL))) {
if (strcmp(xhp->rootdir, "/") == 0) { if (strcmp(xhp->rootdir, "/") == 0) {
res = strdup(p1); res = xbps_xasprintf("%s/%s", dname, lnk);
} else { } else {
res = strdup(p1 + strlen(xhp->rootdir)); char *p1 = strdup(dname + strlen(xhp->rootdir));
} assert(p1);
res = xbps_xasprintf("%s/%s", p1, lnk);
free(p1); free(p1);
} else {
if (strcmp(xhp->rootdir, "/") == 0) {
res = strdup(p);
} else {
res = strdup(p + strlen(xhp->rootdir));
} }
}
assert(res);
free(lnk); free(lnk);
free(p); free(p);
} else { } else {
/* absolute */ /* absolute */
res = lnk; res = lnk;
} }
assert(res);
return res; return res;
} }

View File

@ -85,6 +85,9 @@ remove_symlinks_dangling_body() {
touch -f pkg_A/usr/lib/libfoo.so.1.2.0 touch -f pkg_A/usr/lib/libfoo.so.1.2.0
ln -sf libfoo.so.2 pkg_A/usr/lib/libfoo.so.1 ln -sf libfoo.so.2 pkg_A/usr/lib/libfoo.so.1
ln -sf libfoo.so.2 pkg_B/usr/lib/libfoo.so ln -sf libfoo.so.2 pkg_B/usr/lib/libfoo.so
ln -s ./libfoo.so pkg_B/usr/lib/libfoo.so.3
ln -s ./libfoo.so.4 pkg_B/usr/lib/libfoo.so.3
ln -s ../../../libfoo.so.4 pkg_B/usr/lib/libfoo.so.3
cd some_repo cd some_repo
xbps-create -A noarch -n A-1.0_1 -s "A pkg" ../pkg_A xbps-create -A noarch -n A-1.0_1 -s "A pkg" ../pkg_A