Skip to content

Commit

Permalink
xdp-utils: Make xdp_spawn() fit better our use cases
Browse files Browse the repository at this point in the history
There aren't many places where we need to spawn a subprocess but in all
cases we have a list of commend args and need the output as a response.
Make `xdp_spawn()` and `xdp_spawnv()` nicer to use and only take arguments
that are actually used.
  • Loading branch information
jsparber committed Aug 27, 2024
1 parent aec44ca commit fea6b28
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 83 deletions.
27 changes: 4 additions & 23 deletions document-portal/document-portal.c
Original file line number Diff line number Diff line change
Expand Up @@ -522,27 +522,6 @@ portal_add (GDBusMethodInvocation *invocation,
g_dbus_method_invocation_return_value (invocation, g_variant_new ("(s)", ids[0]));
}

static char *
get_output (GError **error,
const char *argv0,
...)
{
gboolean res;
g_autofree char *output = NULL;
va_list ap;

va_start (ap, argv0);
res = xdp_spawn (NULL, &output, 0, error, argv0, ap);
va_end (ap);

if (res)
{
g_strchomp (output);
return g_steal_pointer (&output);
}
return NULL;
}

/* out =>
0 == hidden
1 == read-only
Expand Down Expand Up @@ -659,16 +638,18 @@ app_has_file_access (const char *target_app_id,

if (g_str_has_prefix (target_app_id, "snap."))
{
res = get_output (&error, "snap", "routine", "file-access",
res = xdp_spawn (&error, "snap", "routine", "file-access",
target_app_id + strlen ("snap."), path, NULL);
}
else
{
/* First we try flatpak info --file-access=PATH APPID, which is supported on new versions */
arg = g_strdup_printf ("--file-access=%s", path);
res = get_output (&error, "flatpak", "info", arg, target_app_id, NULL);
res = xdp_spawn (&error, "flatpak", "info", arg, target_app_id, NULL);
}

g_strchomp (res);

if (res)
{
if (strcmp (res, "read-write") == 0)
Expand Down
5 changes: 2 additions & 3 deletions src/xdp-app-info.c
Original file line number Diff line number Diff line change
Expand Up @@ -1765,7 +1765,6 @@ xdp_app_info_from_snap (int pid,
{
g_autoptr(GError) local_error = NULL;
g_autofree char *pid_str = NULL;
const char *argv[] = { "snap", "routine", "portal-info", NULL, NULL };
g_autofree char *output = NULL;
g_autoptr(GKeyFile) metadata = NULL;
g_autoptr(XdpAppInfo) app_info = NULL;
Expand All @@ -1779,8 +1778,8 @@ xdp_app_info_from_snap (int pid,
}

pid_str = g_strdup_printf ("%u", (guint) pid);
argv[3] = pid_str;
if (!xdp_spawnv (NULL, &output, 0, error, argv))
output = xdp_spawn (error, "snap", "routine", "portal-info", pid_str, NULL);
if (output == NULL)
{
return FALSE;
}
Expand Down
76 changes: 29 additions & 47 deletions src/xdp-utils.c
Original file line number Diff line number Diff line change
Expand Up @@ -414,37 +414,34 @@ spawn_exit_cb (GObject *obj,
spawn_data_exit (data);
}

gboolean
xdp_spawn (GFile *dir,
char **output,
GSubprocessFlags flags,
GError **error,
gchar *
xdp_spawn (GError **error,
const gchar *argv0,
va_list ap)
...)
{
GPtrArray *args;
const gchar *arg;
gboolean res;
va_list ap;
char *output;

va_start (ap, argv0);
args = g_ptr_array_new ();
g_ptr_array_add (args, (gchar *) argv0);
while ((arg = va_arg (ap, const gchar *)))
g_ptr_array_add (args, (gchar *) arg);
g_ptr_array_add (args, NULL);
va_end (ap);

res = xdp_spawnv (dir, output, flags, error, (const gchar * const *) args->pdata);
output = xdp_spawnv ((const gchar * const *) args->pdata, error);

g_ptr_array_free (args, TRUE);

return res;
return output;
}

gboolean
xdp_spawnv (GFile *dir,
char **output,
GSubprocessFlags flags,
GError **error,
const gchar * const *argv)
gchar *
xdp_spawnv (const gchar * const *argv,
GError **error)
{
g_autoptr(GSubprocessLauncher) launcher = NULL;
g_autoptr(GSubprocess) subp = NULL;
Expand All @@ -454,45 +451,30 @@ xdp_spawnv (GFile *dir,
SpawnData data = {0};
g_autofree gchar *commandline = NULL;

launcher = g_subprocess_launcher_new (0);

if (output)
flags |= G_SUBPROCESS_FLAGS_STDOUT_PIPE;

g_subprocess_launcher_set_flags (launcher, flags);

if (dir)
{
g_autofree char *path = g_file_get_path (dir);
g_subprocess_launcher_set_cwd (launcher, path);
}
launcher = g_subprocess_launcher_new (G_SUBPROCESS_FLAGS_STDOUT_PIPE);

commandline = xdp_quote_argv ((const char **)argv);
g_debug ("Running: %s", commandline);

subp = g_subprocess_launcher_spawnv (launcher, argv, error);

if (subp == NULL)
return FALSE;
return NULL;

loop = g_main_loop_new (NULL, FALSE);

data.loop = loop;
data.refs = 1;

if (output)
{
data.refs++;
in = g_subprocess_get_stdout_pipe (subp);
out = g_memory_output_stream_new_resizable ();
g_output_stream_splice_async (out,
in,
G_OUTPUT_STREAM_SPLICE_NONE,
0,
NULL,
spawn_output_spliced_cb,
&data);
}
data.refs = 2;

in = g_subprocess_get_stdout_pipe (subp);
out = g_memory_output_stream_new_resizable ();
g_output_stream_splice_async (out,
in,
G_OUTPUT_STREAM_SPLICE_NONE,
0,
NULL,
spawn_output_spliced_cb,
&data);

g_subprocess_wait_async (subp, NULL, spawn_exit_cb, &data);

Expand All @@ -502,24 +484,24 @@ xdp_spawnv (GFile *dir,
{
g_propagate_error (error, data.error);
g_clear_error (&data.splice_error);
return FALSE;
return NULL;
}

if (out)
{
if (data.splice_error)
{
g_propagate_error (error, data.splice_error);
return FALSE;
return NULL;
}

/* Null terminate */
g_output_stream_write (out, "\0", 1, NULL, NULL);
g_output_stream_close (out, NULL, NULL);
*output = g_memory_output_stream_steal_data (G_MEMORY_OUTPUT_STREAM (out));
return g_memory_output_stream_steal_data (G_MEMORY_OUTPUT_STREAM (out));
}

return TRUE;
return NULL;
}

char *
Expand Down
14 changes: 4 additions & 10 deletions src/xdp-utils.h
Original file line number Diff line number Diff line change
Expand Up @@ -122,17 +122,11 @@ xdp_close_fd (int *fdp)


char * xdp_quote_argv (const char *argv[]);
gboolean xdp_spawn (GFile *dir,
char **output,
GSubprocessFlags flags,
GError **error,
char * xdp_spawn (GError **error,
const gchar *argv0,
va_list ap);
gboolean xdp_spawnv (GFile *dir,
char **output,
GSubprocessFlags flags,
GError **error,
const gchar * const *argv);
...) G_GNUC_NULL_TERMINATED;
char * xdp_spawnv (const gchar * const *argv,
GError **error);

char * xdp_canonicalize_filename (const char *path);
gboolean xdp_has_path_prefix (const char *str,
Expand Down

0 comments on commit fea6b28

Please sign in to comment.