From 64ec757d058dae045e275577da8f14f463cc7c44 Mon Sep 17 00:00:00 2001 From: Ryan Lortie Date: Sun, 2 Mar 2014 17:39:11 -0500 Subject: [PATCH] GUnixMount: port unmount to GSubprocess The existing code is buggy and now that we have GSubprocess, we should just use it instead, allowing for some substantial reduction in complexity. https://bugzilla.gnome.org/show_bug.cgi?id=724916 --- gio/gunixmount.c | 158 +++++++++++-------------------------------------------- 1 file changed, 32 insertions(+), 126 deletions(-) diff --git a/gio/gunixmount.c b/gio/gunixmount.c index 2bbb91a..a42614f 100644 --- a/gio/gunixmount.c +++ b/gio/gunixmount.c @@ -28,6 +28,8 @@ #include #include +#include "gsubprocess.h" +#include "gioenums.h" #include "gunixvolumemonitor.h" #include "gunixmount.h" #include "gunixmounts.h" @@ -240,147 +242,55 @@ g_unix_mount_can_eject (GMount *mount) return unix_mount->can_eject; } - -typedef struct { - GUnixMount *unix_mount; - int error_fd; - GIOChannel *error_channel; - GSource *error_channel_source; - GString *error_string; - gchar **argv; -} UnmountEjectOp; - static void -unmount_eject_op_free (UnmountEjectOp *data) -{ - if (data->error_channel_source) - { - g_source_destroy (data->error_channel_source); - g_source_unref (data->error_channel_source); - } - g_io_channel_unref (data->error_channel); - g_string_free (data->error_string, TRUE); - g_strfreev (data->argv); - close (data->error_fd); - g_free (data); -} - -static void -eject_unmount_cb (GPid pid, gint status, gpointer user_data) +eject_unmount_done (GObject *source, + GAsyncResult *result, + gpointer user_data) { + GSubprocess *subprocess = G_SUBPROCESS (source); GTask *task = user_data; - UnmountEjectOp *data = g_task_get_task_data (task); - - if (g_task_return_error_if_cancelled (task)) - return; - - g_spawn_close_pid (pid); + GError *error = NULL; + gchar *stderr_str; - if (WEXITSTATUS (status) != 0) + if (!g_subprocess_communicate_utf8_finish (subprocess, result, NULL, &stderr_str, &error)) { - g_task_return_new_error (task, G_IO_ERROR, G_IO_ERROR_FAILED, - "%s", data->error_string->str); + g_task_return_error (task, error); + g_error_free (error); } - else - g_task_return_boolean (task, TRUE); - - g_object_unref (task); -} - -static gboolean -eject_unmount_read_error (GIOChannel *channel, - GIOCondition condition, - gpointer user_data) -{ - GTask *task = user_data; - UnmountEjectOp *data = g_task_get_task_data (task); - char buf[BUFSIZ]; - gsize bytes_read; - GError *error; - GIOStatus status; - - if (g_task_return_error_if_cancelled (task)) - return FALSE; - - error = NULL; -read: - status = g_io_channel_read_chars (channel, buf, sizeof (buf), &bytes_read, &error); - if (status == G_IO_STATUS_NORMAL) - { - g_string_append_len (data->error_string, buf, bytes_read); - if (bytes_read == sizeof (buf)) - goto read; - } - else if (status == G_IO_STATUS_EOF) - g_string_append_len (data->error_string, buf, bytes_read); - else if (status == G_IO_STATUS_ERROR) + else /* successful communication */ { - if (data->error_string->len > 0) - g_string_append (data->error_string, "\n"); - - g_string_append (data->error_string, error->message); - g_error_free (error); - - if (data->error_channel_source) - { - g_source_unref (data->error_channel_source); - data->error_channel_source = NULL; - } - return FALSE; + if (!g_subprocess_get_successful (subprocess)) + /* ...but bad exit code */ + g_task_return_new_error (task, G_IO_ERROR, G_IO_ERROR_FAILED, "%s", stderr_str); + else + /* ...and successful exit code */ + g_task_return_boolean (task, TRUE); + + g_free (stderr_str); } - return TRUE; + g_object_unref (task); } static gboolean eject_unmount_do_cb (gpointer user_data) { GTask *task = user_data; - UnmountEjectOp *data = g_task_get_task_data (task); - GPid child_pid; - GSource *child_watch; GError *error = NULL; + GSubprocess *subprocess; + const gchar **argv; + + argv = g_task_get_task_data (task); if (g_task_return_error_if_cancelled (task)) return G_SOURCE_REMOVE; - if (!g_spawn_async_with_pipes (NULL, /* working dir */ - data->argv, - NULL, /* envp */ - G_SPAWN_DO_NOT_REAP_CHILD|G_SPAWN_SEARCH_PATH, - NULL, /* child_setup */ - NULL, /* user_data for child_setup */ - &child_pid, - NULL, /* standard_input */ - NULL, /* standard_output */ - &(data->error_fd), - &error)) { - g_assert (error != NULL); - goto handle_error; - } - - data->error_string = g_string_new (""); - - data->error_channel = g_io_channel_unix_new (data->error_fd); - g_io_channel_set_flags (data->error_channel, G_IO_FLAG_NONBLOCK, &error); - if (error != NULL) - goto handle_error; - - data->error_channel_source = g_io_create_watch (data->error_channel, G_IO_IN); - g_task_attach_source (task, data->error_channel_source, - (GSourceFunc) eject_unmount_read_error); - - child_watch = g_child_watch_source_new (child_pid); - g_task_attach_source (task, data->error_channel_source, - (GSourceFunc) eject_unmount_cb); - g_source_unref (child_watch); - -handle_error: - if (error != NULL) - { - g_task_return_error (task, error); - g_object_unref (task); - } + subprocess = g_subprocess_newv (argv, G_SUBPROCESS_FLAGS_STDOUT_SILENCE | G_SUBPROCESS_FLAGS_STDERR_PIPE, &error); + g_assert_no_error (error); + + g_subprocess_communicate_utf8_async (subprocess, NULL, + g_task_get_cancellable (task), + eject_unmount_done, task); return G_SOURCE_REMOVE; } @@ -393,15 +303,11 @@ eject_unmount_do (GMount *mount, char **argv) { GUnixMount *unix_mount = G_UNIX_MOUNT (mount); - UnmountEjectOp *data; GTask *task; GSource *timeout; - data = g_new0 (UnmountEjectOp, 1); - data->argv = g_strdupv (argv); - task = g_task_new (mount, cancellable, callback, user_data); - g_task_set_task_data (task, data, (GDestroyNotify)unmount_eject_op_free); + g_task_set_task_data (task, g_strdupv (argv), (GDestroyNotify) g_strfreev); if (unix_mount->volume_monitor != NULL) g_signal_emit_by_name (unix_mount->volume_monitor, "mount-pre-unmount", mount); -- 2.7.4