diff options
author | Colin Walters <walters@verbum.org> | 2012-12-21 11:51:15 -0500 |
---|---|---|
committer | Colin Walters <walters@verbum.org> | 2012-12-21 11:51:15 -0500 |
commit | b11ad335f459e3c61b317308da57de441660efd0 (patch) | |
tree | 3405ec605fc25a14b9b77f0668a9d1068ce2f01a | |
parent | 0734be8c1211ba04ecdd14ff30b6c07dbf3b9a84 (diff) | |
download | libgsystem-b11ad335f459e3c61b317308da57de441660efd0.tar.gz |
GSSubprocess: Add new helper API to synchronously spawn
This ends up being used a lot in ostree.
-rw-r--r-- | gsystem-subprocess.c | 45 | ||||
-rw-r--r-- | gsystem-subprocess.h | 7 |
2 files changed, 52 insertions, 0 deletions
diff --git a/gsystem-subprocess.c b/gsystem-subprocess.c index b1980c0..383e921 100644 --- a/gsystem-subprocess.c +++ b/gsystem-subprocess.c @@ -891,3 +891,48 @@ gs_subprocess_new_simple_argv (gchar **argv, return result; } + +/** + * gs_subprocess_simple_run_sync: + * @cwd: Current working directory + * @stdin_disposition: What to do with standard input + * @cancellable: a #GCancellable + * @error: a #GError + * @first_arg: First argument + * @...: Remaining arguments, %NULL terminated + * + * Run a process synchronously, throw an error if it fails. + */ +gboolean +gs_subprocess_simple_run_sync (const char *cwd, + GSSubprocessStreamDisposition stdin_disposition, + GCancellable *cancellable, + GError **error, + const char *first_arg, + ...) +{ + gboolean ret = FALSE; + va_list args; + GSSubprocess *proc = NULL; + GSSubprocessContext *context = NULL; + + va_start (args, first_arg); + context = gs_subprocess_context_newa (first_arg, args); + va_end (args); + gs_subprocess_context_set_stdin_disposition (context, stdin_disposition); + gs_subprocess_context_set_cwd (context, cwd); + proc = gs_subprocess_new (context, cancellable, error); + if (!proc) + goto out; + + if (!gs_subprocess_wait_sync_check (proc, cancellable, error)) + goto out; + + ret = TRUE; + out: + if (context) + g_object_unref (context); + if (proc) + g_object_unref (proc); + return ret; +} diff --git a/gsystem-subprocess.h b/gsystem-subprocess.h index 7ca6417..cc66978 100644 --- a/gsystem-subprocess.h +++ b/gsystem-subprocess.h @@ -84,6 +84,13 @@ GSSubprocess * gs_subprocess_new_simple_argv (char ** GCancellable *cancellable, GError **error); +gboolean gs_subprocess_simple_run_sync (const char *cwd, + GSSubprocessStreamDisposition stdin_disposition, + GCancellable *cancellable, + GError **error, + const char *first_arg, + ...) G_GNUC_NULL_TERMINATED; + G_END_DECLS #endif |