From: Michael Tremer Date: Sun, 29 Jun 2025 15:01:55 +0000 (+0000) Subject: util: Refactor pakfire_which() X-Git-Url: http://git.ipfire.org/gitweb/gitweb.cgi?a=commitdiff_plain;h=f6b181b87afbc31a671295078c46be4e8a724400;p=people%2Fms%2Fpakfire.git util: Refactor pakfire_which() Signed-off-by: Michael Tremer --- diff --git a/src/pakfire/util.c b/src/pakfire/util.c index 92429d06..61b710ab 100644 --- a/src/pakfire/util.c +++ b/src/pakfire/util.c @@ -337,13 +337,12 @@ int pakfire_rmtree(const char* path, int flags) { return r; } -int __pakfire_which(pakfire_root* root, char* path, const size_t length, - const char* what) { - char buffer[PATH_MAX]; +int __pakfire_which(pakfire_root* root, char* path, const size_t length, const char* command) { + int fd = -EBADF; int r; // Check input - if (!what) + if (!path || !length || !command) return -EINVAL; static const char* paths[] = { @@ -354,24 +353,37 @@ int __pakfire_which(pakfire_root* root, char* path, const size_t length, NULL, }; - // Clear path - *path = '\0'; - + // Walk through all search paths... for (const char** p = paths; *p; p++) { // Compose path - r = pakfire_root_path(root, buffer, "%s/%s", *p, what); - if (r) + r = __pakfire_path_format(path, length, "%s/%s", *p, command); + if (r < 0) return r; - // If the path exists and is executable we are done - if (access(buffer, X_OK) == 0) { - const char* relpath = pakfire_root_relpath(root, buffer); + // Try to open the file + fd = pakfire_root_openat(root, path, O_PATH); + if (fd < 0) { + switch (errno) { + case ENOENT: + continue; - // Store the result in path - return __pakfire_string_set(path, length, relpath); + default: + return -errno; + } } + + // Close the file descriptor straight away + r = close(fd); + if (r < 0) + return -errno; + + // Done! + return 0; } + // Nothing found, clear the path + *path = '\0'; + return 0; } diff --git a/src/pakfire/util.h b/src/pakfire/util.h index c806c802..0e8d33c7 100644 --- a/src/pakfire/util.h +++ b/src/pakfire/util.h @@ -67,9 +67,9 @@ char* pakfire_mkdtemp(char* path); int pakfire_symlink(pakfire_ctx* ctx, const char* target, const char* linkpath); int pakfire_rmtree(const char* path, int flags); -#define pakfire_which(root, path, what) \ - __pakfire_which(root, path, sizeof(path), what) -int __pakfire_which(pakfire_root* root, char* path, const size_t length, const char* what); +#define pakfire_which(root, path, command) \ + __pakfire_which(root, path, sizeof(path), command) +int __pakfire_which(pakfire_root* root, char* path, const size_t length, const char* command); // UUID Stuff