diff options
author | Florian Pritz <bluewind@xinu.at> | 2011-07-06 17:22:59 +0200 |
---|---|---|
committer | Dan McGee <dan@archlinux.org> | 2011-07-18 10:42:48 -0500 |
commit | 29a96bcfe179a82adbd575057e2ef9d48a6a72fb (patch) | |
tree | dd7d6a909713fea046395c8cd8d399d6f62d43b6 /lib | |
parent | 48e2a1a119ce84681f25855221b6e2fc2689a760 (diff) |
add _alpm_access() wrapper
This is a wrapper function for access() which logs some debug
information and eases handling in case of split directory and filename.
Signed-off-by: Florian Pritz <bluewind@xinu.at>
Signed-off-by: Dan McGee <dan@archlinux.org>
Diffstat (limited to 'lib')
-rw-r--r-- | lib/libalpm/util.c | 47 | ||||
-rw-r--r-- | lib/libalpm/util.h | 1 |
2 files changed, 48 insertions, 0 deletions
diff --git a/lib/libalpm/util.c b/lib/libalpm/util.c index 62313d81..030cf43b 100644 --- a/lib/libalpm/util.c +++ b/lib/libalpm/util.c @@ -959,6 +959,53 @@ long _alpm_parsedate(const char *line) return atol(line); } +/** + * Wrapper around access() which takes a dir and file argument + * separately and generates an appropriate error message. + * If dir is NULL file will be treated as the whole path. + * @param handle an alpm handle + * @param dir directory path ending with and slash + * @param file filename + * @param amode access mode as described in access() + * @return int value returned by access() + */ + +int _alpm_access(alpm_handle_t *handle, const char *dir, const char *file, int amode) +{ + size_t len = 0; + int ret = 0; + + if (dir) { + char *check_path; + + len = strlen(dir) + strlen(file) + 1; + CALLOC(check_path, len, sizeof(char), RET_ERR(handle, ALPM_ERR_MEMORY, -1)); + snprintf(check_path, len, "%s%s", dir, file); + + ret = access(check_path, amode); + free(check_path); + } else { + dir = ""; + ret = access(file, amode); + } + + if(ret != 0) { + if (amode & R_OK) { + _alpm_log(handle, ALPM_LOG_DEBUG, _("\"%s%s\" is not readable: %s\n"), dir, file, strerror(errno)); + } + if (amode & W_OK) { + _alpm_log(handle, ALPM_LOG_DEBUG, _("\"%s%s\" is not writable: %s\n"), dir, file, strerror(errno)); + } + if (amode & X_OK) { + _alpm_log(handle, ALPM_LOG_DEBUG, _("\"%s%s\" is not executable: %s\n"), dir, file, strerror(errno)); + } + if (amode == F_OK) { + _alpm_log(handle, ALPM_LOG_DEBUG, _("\"%s%s\" does not exist: %s\n"), dir, file, strerror(errno)); + } + } + return ret; +} + #ifndef HAVE_STRNDUP /* A quick and dirty implementation derived from glibc */ static size_t strnlen(const char *s, size_t max) diff --git a/lib/libalpm/util.h b/lib/libalpm/util.h index d66ddee9..a75c5aae 100644 --- a/lib/libalpm/util.h +++ b/lib/libalpm/util.h @@ -116,6 +116,7 @@ unsigned long _alpm_hash_sdbm(const char *str); long _alpm_parsedate(const char *line); int _alpm_raw_cmp(const char *first, const char *second); int _alpm_raw_ncmp(const char *first, const char *second, size_t max); +int _alpm_access(alpm_handle_t *handle, const char *dir, const char *file, int amode); #ifndef HAVE_STRSEP char *strsep(char **, const char *); |