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[] = {
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;
}
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