Introduce xbps_symlink_target() and use it in two places.

This code is now shared by xbps-pkgdb and libxbps/xbps-remove.
This commit is contained in:
Juan RP
2015-02-19 09:47:09 +01:00
parent 69e3a50e75
commit 05f879ec09
4 changed files with 78 additions and 148 deletions

View File

@ -92,60 +92,6 @@ check_remove_pkg_files(struct xbps_handle *xhp,
return fail;
}
static char *
symlink_target(struct xbps_handle *xhp, const char *path)
{
struct stat sb;
char *p, *p1, *dname, *lnk, *res = NULL;
ssize_t r;
if (lstat(path, &sb) == -1)
return NULL;
lnk = malloc(sb.st_size + 1);
assert(lnk);
r = readlink(path, lnk, sb.st_size + 1);
if (r < 0 || r > sb.st_size) {
free(lnk);
return NULL;
}
lnk[sb.st_size] = '\0';
if (strstr(lnk, "./") || lnk[0] != '/') {
/* relative */
p = strdup(path);
assert(p);
dname = dirname(p);
assert(dname);
p = xbps_xasprintf("%s/%s", dname, lnk);
assert(p);
p1 = xbps_sanitize_path(p);
assert(p1);
free(p);
if ((strstr(p1, "./")) && (p = realpath(p1, NULL))) {
if (strcmp(xhp->rootdir, "/") == 0)
res = strdup(p);
else
res = strdup(p + strlen(xhp->rootdir));
free(p);
}
if (res == NULL) {
if (strcmp(xhp->rootdir, "/") == 0)
res = strdup(p1);
else
res = strdup(p1 + strlen(xhp->rootdir));
}
assert(res);
free(lnk);
free(p1);
} else {
/* absolute */
res = lnk;
}
return res;
}
static int
remove_pkg_files(struct xbps_handle *xhp,
xbps_dictionary_t dict,
@ -265,7 +211,7 @@ remove_pkg_files(struct xbps_handle *xhp,
const char *target = NULL;
char *lnk;
lnk = symlink_target(xhp, path);
lnk = xbps_symlink_target(xhp, path);
if (lnk == NULL) {
xbps_dbg_printf(xhp, "[remove] %s "
"symlink_target: %s\n", path, strerror(errno));