]> git.ipfire.org Git - thirdparty/libvirt.git/commitdiff
util: Introduce virFileFindInPathFull()
authorAndrea Bolognani <abologna@redhat.com>
Tue, 25 Apr 2023 14:18:35 +0000 (16:18 +0200)
committerAndrea Bolognani <abologna@redhat.com>
Wed, 10 May 2023 16:54:08 +0000 (18:54 +0200)
Signed-off-by: Andrea Bolognani <abologna@redhat.com>
Reviewed-by: Martin Kletzander <mkletzan@redhat.com>
src/libvirt_private.syms
src/util/virfile.c
src/util/virfile.h

index 62c296bf5fa1245d81f553aafa5cb0d0282579b2..27b8e111aacb96356a71a905b4a88fb1fa90c5e6 100644 (file)
@@ -2358,6 +2358,7 @@ virFileWrapperFdFree;
 virFileWrapperFdNew;
 virFileWriteStr;
 virFindFileInPath;
+virFindFileInPathFull;
 
 
 # util/virfilecache.h
index 8e94d19e45ef9c7d150e7d1bc17e897878997645..6b4d4c3522f9d08b9e4e21f697c9849a03c7752f 100644 (file)
@@ -1737,6 +1737,25 @@ virFileIsLink(const char *linkpath)
  */
 char *
 virFindFileInPath(const char *file)
+{
+    return virFindFileInPathFull(file, NULL);
+}
+
+/* virFindFileInPathFull:
+ * @file: name of the program
+ * @extraDirs: NULL-terminated list of additional directories
+ *
+ * Like virFindFileInPath(), but in addition to searching $PATH also
+ * looks into all directories listed in @extraDirs. This is useful to
+ * locate helpers that are installed outside of $PATH.
+ *
+ * The returned path must be freed by the caller.
+ *
+ * Returns: absolute path of the program or NULL
+ */
+char *
+virFindFileInPathFull(const char *file,
+                      const char *const *extraDirs)
 {
     g_autofree char *path = NULL;
     if (file == NULL)
@@ -1751,6 +1770,20 @@ virFindFileInPath(const char *file)
         return g_canonicalize_filename(path, NULL);
     }
 
+    if (extraDirs) {
+        while (*extraDirs) {
+            g_autofree char *extraPath = NULL;
+
+            extraPath = g_strdup_printf("%s/%s", *extraDirs, file);
+
+            if (virFileIsExecutable(extraPath)) {
+                return g_steal_pointer(&extraPath);
+            }
+
+            extraDirs++;
+        }
+    }
+
     return NULL;
 }
 
index f7a31d9f5718a760f24b105cd84b8a70f37158cc..6a14173625009939dff96d322a37570acd858eea 100644 (file)
@@ -189,6 +189,9 @@ int virFileIsLink(const char *linkpath)
 
 char *virFindFileInPath(const char *file)
     G_NO_INLINE;
+char *virFindFileInPathFull(const char *file,
+                            const char *const *extraDirs)
+    G_NO_INLINE;
 
 char *virFileFindResource(const char *filename,
                           const char *builddir,