]> git.ipfire.org Git - pakfire.git/commitdiff
util: Refactor pakfire_which()
authorMichael Tremer <michael.tremer@ipfire.org>
Sun, 29 Jun 2025 15:01:55 +0000 (15:01 +0000)
committerMichael Tremer <michael.tremer@ipfire.org>
Sun, 29 Jun 2025 15:01:55 +0000 (15:01 +0000)
Signed-off-by: Michael Tremer <michael.tremer@ipfire.org>
src/pakfire/util.c
src/pakfire/util.h

index 92429d061e76ef8d551f1b3e8ebe8352c421fa81..61b710ab7f8ea8cf94f1b653e284cd42d9bf3c67 100644 (file)
@@ -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;
 }
 
index c806c802f13127cedb2520ee3619bc75a9a981b9..0e8d33c7a88a17250f00d283f9134bb757432e5a 100644 (file)
@@ -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