From b55abdce7aebb142ce79da3aa3645afe7693a3c4 Mon Sep 17 00:00:00 2001 From: Dan McGee Date: Sun, 4 Nov 2007 18:02:25 -0600 Subject: libalpm: use an lstat wrapper so we never dereference dir symlinks Linux lstat follows POSIX standards and dereferences a symlink pointing to a directory if there is a trailing slash. For purposes of libalpm, we don't want this so make a lstat wrapper that suppresses this behavior. Signed-off-by: Dan McGee --- lib/libalpm/util.c | 26 +++++++++++++++++++++++++- 1 file changed, 25 insertions(+), 1 deletion(-) (limited to 'lib/libalpm/util.c') diff --git a/lib/libalpm/util.c b/lib/libalpm/util.c index 016c0f40..5df3a025 100644 --- a/lib/libalpm/util.c +++ b/lib/libalpm/util.c @@ -432,7 +432,7 @@ int _alpm_rmrf(const char *path) char name[PATH_MAX]; struct stat st; - if(lstat(path, &st) == 0) { + if(_alpm_lstat(path, &st) == 0) { if(!S_ISDIR(st.st_mode)) { if(!unlink(path)) { return(0); @@ -597,6 +597,30 @@ const char *_alpm_filecache_setup(void) return(alpm_list_getdata(tmp)); } +/** lstat wrapper that treats /path/dirsymlink/ the same as /path/dirsymlink. + * Linux lstat follows POSIX semantics and still performs a dereference on + * the first, and for uses of lstat in libalpm this is not what we want. + * @param path path to file to lstat + * @param buf structure to fill with stat information + * @return the return code from lstat + */ +int _alpm_lstat(const char *path, struct stat *buf) +{ + int ret; + char *newpath = strdup(path); + int len = strlen(newpath); + + /* strip the trailing slash if one exists */ + if(len != 0 && newpath[len - 1] == '/') { + newpath[len - 1] = '\0'; + } + + ret = lstat(path, buf); + + FREE(newpath); + return(ret); +} + /** Get the md5 sum of file. * @param filename name of the file * @return the checksum on success, NULL on error -- cgit v1.2.3