libxbps: pkg remove: never remove base symlinks in the root directory.

The /bin, /sbin, /lib, /lib32, /lib64, /var/run symlinks should never be removed
in Void, so be safe and ignore the removal of them.

Added another test to the testsuite to verify its correctness.
This commit is contained in:
Juan RP
2013-11-28 10:27:36 +01:00
parent 5d63f6f442
commit dce26db1de
5 changed files with 75 additions and 6 deletions

View File

@ -33,6 +33,21 @@
#include "xbps_api_impl.h"
#ifndef __arraycount
# define __arraycount(a) (sizeof(a) / sizeof(*(a)))
#endif
/* These are symlinks in Void and must not be removed */
static const char *basesymlinks[] = {
"/bin",
"/sbin",
"/lib",
"/lib32",
"/lib64",
"/usr/lib64",
"/var/run",
};
int HIDDEN
xbps_remove_pkg_files(struct xbps_handle *xhp,
xbps_dictionary_t dict,
@ -44,9 +59,10 @@ xbps_remove_pkg_files(struct xbps_handle *xhp,
xbps_object_iterator_t iter;
xbps_object_t obj;
const char *file, *sha256, *curobj = NULL;
char *path = NULL, *pkgname = NULL;
char *path = NULL;
char buf[PATH_MAX];
int rv = 0;
bool found;
assert(xbps_object_type(dict) == XBPS_TYPE_DICTIONARY);
assert(key != NULL);
@ -68,9 +84,6 @@ xbps_remove_pkg_files(struct xbps_handle *xhp,
else if (strcmp(key, "dirs") == 0)
curobj = "directory";
pkgname = xbps_pkg_name(pkgver);
assert(pkgname);
while ((obj = xbps_object_iterator_next(iter))) {
xbps_dictionary_get_cstring_nocopy(obj, "file", &file);
path = xbps_xasprintf("%s/%s", xhp->rootdir, file);
@ -141,6 +154,22 @@ xbps_remove_pkg_files(struct xbps_handle *xhp,
continue;
}
}
/*
* Make sure to not remove any symlink of root directory.
*/
found = false;
for (uint8_t i = 0; i < __arraycount(basesymlinks); i++) {
if (strcmp(file, basesymlinks[i]) == 0) {
found = true;
xbps_dbg_printf(xhp, "[remove] %s ignoring "
"%s removal\n", pkgver, file);
break;
}
}
if (found) {
free(path);
continue;
}
/*
* Remove the object if possible.
*/
@ -158,7 +187,6 @@ xbps_remove_pkg_files(struct xbps_handle *xhp,
free(path);
}
xbps_object_iterator_release(iter);
free(pkgname);
return rv;
}