From 93a783d9f46953e55a733f08db7f8c5b1028e5b9 Mon Sep 17 00:00:00 2001
From: Simon McVittie <smcv@collabora.com>
Date: Mon, 4 Nov 2019 19:23:14 +0000
Subject: [PATCH] _srt_get_helper: Add support for searching the PATH

We have two broad classes of helper:

- Helpers from this source package, normally found in
  /usr/libexec/steam-runtime-tools-0
- Tools we have repurposed from other source packages (wflinfo and
  vulkaninfo) found via the $PATH

For the former class, our behaviour is unchanged.

To support the latter class, a caller can now set the SEARCH_PATH flag.

Signed-off-by: Simon McVittie <smcv@collabora.com>
---
 steam-runtime-tools/utils-internal.h |  1 +
 steam-runtime-tools/utils.c          | 16 ++++++++++++++++
 2 files changed, 17 insertions(+)

diff --git a/steam-runtime-tools/utils-internal.h b/steam-runtime-tools/utils-internal.h
index ca463b8f5..19c3b0f49 100644
--- a/steam-runtime-tools/utils-internal.h
+++ b/steam-runtime-tools/utils-internal.h
@@ -30,6 +30,7 @@
 
 typedef enum
 {
+  SRT_HELPER_FLAGS_SEARCH_PATH = (1 << 0),
   SRT_HELPER_FLAGS_TIME_OUT = (1 << 1),
   SRT_HELPER_FLAGS_TIME_OUT_SOONER = (1 << 2),
   SRT_HELPER_FLAGS_NONE = 0
diff --git a/steam-runtime-tools/utils.c b/steam-runtime-tools/utils.c
index 3d8e1651e..322619cf1 100644
--- a/steam-runtime-tools/utils.c
+++ b/steam-runtime-tools/utils.c
@@ -237,6 +237,7 @@ _srt_get_helper (const char *helpers_path,
 {
   GPtrArray *argv = NULL;
   gchar *path;
+  gchar *prefixed;
 
   g_return_val_if_fail (base != NULL, NULL);
   g_return_val_if_fail (error == NULL || *error == NULL, NULL);
@@ -264,6 +265,21 @@ _srt_get_helper (const char *helpers_path,
         }
     }
 
+  if ((flags & SRT_HELPER_FLAGS_SEARCH_PATH) != 0
+      && helpers_path == NULL)
+    {
+      /* For helpers that are not part of steam-runtime-tools, such as
+       * *-wflinfo and *-vulkaninfo, we currently only search PATH,
+       * unless helpers_path has been overridden */
+      if (multiarch == NULL)
+        prefixed = g_strdup (base);
+      else
+        prefixed = g_strdup_printf ("%s-%s", multiarch, base);
+
+      g_ptr_array_add (argv, g_steal_pointer (&prefixed));
+      return argv;
+    }
+
   if (helpers_path == NULL)
     {
       helpers_path = _srt_get_helpers_path (error);
-- 
GitLab