From 52263437bf68f8615012b9a8de62ae779619a440 Mon Sep 17 00:00:00 2001 From: Benedikt Meurer <benny@xfce.org> Date: Tue, 14 Mar 2006 13:51:43 +0000 Subject: [PATCH] 2006-03-14 Benedikt Meurer <benny@xfce.org> * thunar-vfs/thunar-vfs-exec.{c,h}: Add thunar_vfs_exec_sync() helper function. * thunar-vfs/thunar-vfs-volume-freebsd.c: Use thunar_vfs_exec_sync(). (Old svn revision: 20415) --- ChangeLog | 6 + thunar-vfs/thunar-vfs-exec.c | 69 +++++++++++ thunar-vfs/thunar-vfs-exec.h | 4 + thunar-vfs/thunar-vfs-volume-freebsd.c | 155 ++----------------------- 4 files changed, 86 insertions(+), 148 deletions(-) diff --git a/ChangeLog b/ChangeLog index ce78b35b2..2bcb3c775 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,9 @@ +2006-03-14 Benedikt Meurer <benny@xfce.org> + + * thunar-vfs/thunar-vfs-exec.{c,h}: Add thunar_vfs_exec_sync() helper + function. + * thunar-vfs/thunar-vfs-volume-freebsd.c: Use thunar_vfs_exec_sync(). + 2006-03-14 Benedikt Meurer <benny@xfce.org> * thunar/thunar-details-view.c(thunar_details_view_zoom_level_changed): diff --git a/thunar-vfs/thunar-vfs-exec.c b/thunar-vfs/thunar-vfs-exec.c index 49143c861..5341e87e4 100644 --- a/thunar-vfs/thunar-vfs-exec.c +++ b/thunar-vfs/thunar-vfs-exec.c @@ -32,6 +32,9 @@ #ifdef HAVE_MEMORY_H #include <memory.h> #endif +#ifdef HAVE_STDARG_H +#include <stdarg.h> +#endif #ifdef HAVE_STRING_H #include <string.h> #endif @@ -454,3 +457,69 @@ thunar_vfs_exec_on_screen (GdkScreen *screen, } + +/** + * thunar_vfs_exec_sync: + * @command_fmt : the command to execute (can be a printf + * format string). + * @error : return location for errors or %NULL. + * @... : additional parameters to fill into + * @command_fmt. + * + * Executes the given @command_fmt and returns %TRUE if the + * command terminated successfully. Else, the @error is set + * to the standard error output. + * + * Return value: %TRUE if the @command_line was executed + * successfully, %FALSE if @error is set. + **/ +gboolean +thunar_vfs_exec_sync (const gchar *command_fmt, + GError **error, + ...) +{ + gboolean result; + va_list args; + gchar *standard_error; + gchar *command_line; + gint exit_status; + + g_return_val_if_fail (error == NULL || *error == NULL, FALSE); + g_return_val_if_fail (command_fmt != NULL, FALSE); + + /* determine the command line */ + va_start (args, error); + command_line = g_strdup_vprintf (command_fmt, args); + va_end (args); + + /* try to execute the command line */ + result = g_spawn_command_line_sync (command_line, NULL, &standard_error, &exit_status, error); + if (G_UNLIKELY (result)) + { + /* check if the command failed */ + if (G_UNLIKELY (exit_status != 0)) + { + /* drop additional whitespace from the stderr output */ + g_strstrip (standard_error); + + /* generate an error from the stderr output */ + if (G_LIKELY (*standard_error != '\0')) + g_set_error (error, G_FILE_ERROR, G_FILE_ERROR_FAILED, "%s", standard_error); + else + g_set_error (error, G_FILE_ERROR, G_FILE_ERROR_FAILED, _("Unknown error")); + + /* and yes, we failed */ + result = FALSE; + } + + /* release the stderr output */ + g_free (standard_error); + } + + /* cleanup */ + g_free (command_line); + + return result; +} + + diff --git a/thunar-vfs/thunar-vfs-exec.h b/thunar-vfs/thunar-vfs-exec.h index 32f88106f..751d1435d 100644 --- a/thunar-vfs/thunar-vfs-exec.h +++ b/thunar-vfs/thunar-vfs-exec.h @@ -43,6 +43,10 @@ gboolean thunar_vfs_exec_on_screen (GdkScreen *screen, gboolean startup_notify, GError **error) G_GNUC_INTERNAL; +gboolean thunar_vfs_exec_sync (const gchar *command_line, + GError **error, + ...) G_GNUC_INTERNAL; + G_END_DECLS; #endif /* !__THUNAR_VFS_EXEC_H__ */ diff --git a/thunar-vfs/thunar-vfs-volume-freebsd.c b/thunar-vfs/thunar-vfs-volume-freebsd.c index 1b01c97d4..851c47ed2 100644 --- a/thunar-vfs/thunar-vfs-volume-freebsd.c +++ b/thunar-vfs/thunar-vfs-volume-freebsd.c @@ -47,6 +47,7 @@ #include <exo/exo.h> +#include <thunar-vfs/thunar-vfs-exec.h> #include <thunar-vfs/thunar-vfs-volume-freebsd.h> #include <thunar-vfs/thunar-vfs-alias.h> @@ -203,49 +204,13 @@ thunar_vfs_volume_freebsd_eject (ThunarVfsVolume *volume, { ThunarVfsVolumeFreeBSD *volume_freebsd = THUNAR_VFS_VOLUME_FREEBSD (volume); gboolean result; - gchar *standard_error; - gchar *command_line; gchar *quoted; - gint exit_status; - /* generate the command line for the eject command */ + /* execute the eject command */ quoted = g_shell_quote (volume_freebsd->device_path); - command_line = g_strconcat ("eject ", quoted, NULL); + result = thunar_vfs_exec_sync ("eject %s", error, quoted); g_free (quoted); - /* execute the eject command */ - result = g_spawn_command_line_sync (command_line, NULL, &standard_error, &exit_status, error); - if (G_LIKELY (result)) - { - /* check if the command failed */ - if (G_UNLIKELY (exit_status != 0)) - { - /* drop additional whitespace from the stderr output */ - g_strstrip (standard_error); - - /* generate an error from the standard_error content */ - if (G_LIKELY (g_str_has_prefix (standard_error, "eject: "))) - { - /* strip off the "eject: " prefix */ - g_set_error (error, G_FILE_ERROR, G_FILE_ERROR_FAILED, "%s", standard_error + 7); - } - else - { - /* use the error message as-is, not nice, but hey, better than nothing */ - g_set_error (error, G_FILE_ERROR, G_FILE_ERROR_FAILED, "%s", standard_error); - } - - /* and yes, we failed */ - result = FALSE; - } - - /* release the stderr output */ - g_free (standard_error); - } - - /* cleanup */ - g_free (command_line); - /* update volume state if successfull */ if (G_LIKELY (result)) thunar_vfs_volume_freebsd_update (volume_freebsd); @@ -262,87 +227,18 @@ thunar_vfs_volume_freebsd_mount (ThunarVfsVolume *volume, { ThunarVfsVolumeFreeBSD *volume_freebsd = THUNAR_VFS_VOLUME_FREEBSD (volume); gboolean result; - GString *message; gchar mount_point[THUNAR_VFS_PATH_MAXSTRLEN]; - gchar **standard_error_parts; - gchar *standard_error; - gchar *command_line; gchar *quoted; - gint exit_status; - gint n_parts; - gint i; /* determine the absolute path to the mount point */ if (thunar_vfs_path_to_string (volume_freebsd->mount_point, mount_point, sizeof (mount_point), error) < 0) return FALSE; - /* generate the command line for the mount command */ + /* execute the mount command */ quoted = g_shell_quote (mount_point); - command_line = g_strconcat ("mount ", quoted, NULL); + result = thunar_vfs_exec_sync ("mount %s", error, quoted); g_free (quoted); - /* execute the mount command */ - result = g_spawn_command_line_sync (command_line, NULL, &standard_error, &exit_status, error); - if (G_LIKELY (result)) - { - /* check if the command failed */ - if (G_UNLIKELY (exit_status != 0)) - { - /* drop additional whitespace from the stderr output */ - g_strstrip (standard_error); - - /* the error message will usually look like - * "cd9660: /dev/cd1: Operation not permitted", - * so let's apply some voodoo magic here to - * make it look better. - */ - standard_error_parts = g_strsplit (standard_error, ":", -1); - if (G_UNLIKELY (standard_error_parts[0] == NULL)) - { - /* no useful information, *narf* */ - g_set_error (error, G_FILE_ERROR, G_FILE_ERROR_FAILED, _("Unknown error")); - } - else - { - /* determine the last part of the error */ - for (n_parts = 0; standard_error_parts[n_parts + 1] != NULL; ++n_parts) - ; - - /* construct a new error message */ - message = g_string_new (g_strstrip (standard_error_parts[n_parts])); - - /* append any additional information in () */ - if (G_LIKELY (n_parts > 0)) - { - g_string_append (message, " ("); - for (i = 0; i < n_parts; ++i) - { - if (G_UNLIKELY (i > 0)) - g_string_append (message, ", "); - g_string_append (message, g_strstrip (standard_error_parts[i])); - } - g_string_append (message, ")"); - } - - /* use the generated message for the error */ - g_set_error (error, G_FILE_ERROR, G_FILE_ERROR_FAILED, "%s", message->str); - g_string_free (message, TRUE); - } - - /* release the standard error parts */ - g_strfreev (standard_error_parts); - - /* and yes, we failed */ - result = FALSE; - } - - /* release the stderr output */ - g_free (standard_error); - } - - /* cleanup */ - g_free (command_line); - /* update volume state if successfull */ if (G_LIKELY (result)) thunar_vfs_volume_freebsd_update (volume_freebsd); @@ -360,54 +256,17 @@ thunar_vfs_volume_freebsd_unmount (ThunarVfsVolume *volume, ThunarVfsVolumeFreeBSD *volume_freebsd = THUNAR_VFS_VOLUME_FREEBSD (volume); gboolean result; gchar mount_point[THUNAR_VFS_PATH_MAXSTRLEN]; - gchar *standard_error; - gchar *command_line; gchar *quoted; - gint exit_status; /* determine the absolute path to the mount point */ if (thunar_vfs_path_to_string (volume_freebsd->mount_point, mount_point, sizeof (mount_point), error) < 0) return FALSE; - /* generate the command line for the umount command */ + /* execute the umount command */ quoted = g_shell_quote (mount_point); - command_line = g_strconcat ("umount ", quoted, NULL); + result = thunar_vfs_exec_sync ("umount %s", error, quoted); g_free (quoted); - /* execute the umount command */ - result = g_spawn_command_line_sync (command_line, NULL, &standard_error, &exit_status, error); - if (G_LIKELY (result)) - { - /* check if the command failed */ - if (G_UNLIKELY (exit_status != 0)) - { - /* drop additional whitespace from the stderr output */ - g_strstrip (standard_error); - - /* generate an error from the standard_error content */ - if (G_LIKELY (g_str_has_prefix (standard_error, "umount: "))) - { - /* strip off the "umount: " prefix */ - g_set_error (error, G_FILE_ERROR, G_FILE_ERROR_FAILED, "%s", standard_error + 8); - } - else - { - /* use the error message as-is, not nice, but hey, better than nothing */ - g_set_error (error, G_FILE_ERROR, G_FILE_ERROR_FAILED, "%s", standard_error); - } - - - /* and yes, we failed */ - result = FALSE; - } - - /* release the stderr output */ - g_free (standard_error); - } - - /* cleanup */ - g_free (command_line); - /* update volume state if successfull */ if (G_LIKELY (result)) thunar_vfs_volume_freebsd_update (volume_freebsd); -- GitLab