util: Introduce virFileFindInPathFull()

Signed-off-by: Andrea Bolognani <abologna@redhat.com>
Reviewed-by: Martin Kletzander <mkletzan@redhat.com>
This commit is contained in:
Andrea Bolognani 2023-04-25 16:18:35 +02:00
parent d92054c867
commit ef91f9e52a
3 changed files with 37 additions and 0 deletions

View File

@ -2358,6 +2358,7 @@ virFileWrapperFdFree;
virFileWrapperFdNew;
virFileWriteStr;
virFindFileInPath;
virFindFileInPathFull;
# util/virfilecache.h

View 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;
}

View 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,