[glib] gio-tool: Various fixes related to error messages



commit 0beeeb2ec9f2a934fee8c7aa40c4d4c415d0d187
Author: Ondrej Holy <oholy redhat com>
Date:   Fri Dec 16 14:32:29 2016 +0100

    gio-tool: Various fixes related to error messages
    
    This patch contains the following changes:
    - Print all errors with "gio: " prefix
    - Print file uri in error for each tool allowing multiple locations
    - Mark all error messages translatable
    - Do not leak strings used in error messages
    - Always start error messages with capital letter
    - Unify some error messages across various tools
    - Fix addional/missing new line characters
    
    https://bugzilla.gnome.org/show_bug.cgi?id=776169

 gio/gio-tool-cat.c     |    4 ++--
 gio/gio-tool-info.c    |    6 +++---
 gio/gio-tool-mime.c    |    6 +++---
 gio/gio-tool-monitor.c |   20 +++++++-------------
 gio/gio-tool-mount.c   |   48 ++++++++++++++++++++++++++++--------------------
 gio/gio-tool-open.c    |    2 +-
 gio/gio-tool-remove.c  |    2 +-
 gio/gio-tool-rename.c  |    2 +-
 gio/gio-tool-save.c    |    6 +++---
 gio/gio-tool-set.c     |    4 ++--
 gio/gio-tool.c         |   30 ++++++++++++++++++++++++++++--
 gio/gio-tool.h         |    5 ++++-
 12 files changed, 83 insertions(+), 52 deletions(-)
---
diff --git a/gio/gio-tool-cat.c b/gio/gio-tool-cat.c
index c057264..3dc02d1 100644
--- a/gio/gio-tool-cat.c
+++ b/gio/gio-tool-cat.c
@@ -77,7 +77,7 @@ cat (GFile *file)
 
               if (written == -1 && errno != EINTR)
                 {
-                  print_file_error (file, "error writing to stdout");
+                  print_file_error (file, _("Error writing to stdout"));
                   success = FALSE;
                   goto out;
                 }
@@ -148,7 +148,7 @@ handle_cat (int argc, char *argv[], gboolean do_help)
 
   if (argc < 2)
     {
-      show_help (context, _("No files given"));
+      show_help (context, _("No locations given"));
       return 1;
     }
 
diff --git a/gio/gio-tool-info.c b/gio/gio-tool-info.c
index 8b86293..e21ef1b 100644
--- a/gio/gio-tool-info.c
+++ b/gio/gio-tool-info.c
@@ -187,7 +187,7 @@ query_info (GFile *file)
 
   if (info == NULL)
     {
-      g_printerr ("Error getting info: %s\n", error->message);
+      print_file_error (file, error->message);
       g_error_free (error);
       return FALSE;
     }
@@ -218,7 +218,7 @@ get_writable_info (GFile *file)
   list = g_file_query_settable_attributes (file, NULL, &error);
   if (list == NULL)
     {
-      g_printerr (_("Error getting writable attributes: %s\n"), error->message);
+      print_file_error (file, error->message);
       g_error_free (error);
       return FALSE;
     }
@@ -239,7 +239,7 @@ get_writable_info (GFile *file)
   list = g_file_query_writable_namespaces (file, NULL, &error);
   if (list == NULL)
     {
-      g_printerr ("Error getting writable namespaces: %s\n", error->message);
+      print_file_error (file, error->message);
       g_error_free (error);
       return FALSE;
     }
diff --git a/gio/gio-tool-mime.c b/gio/gio-tool-mime.c
index 92a96c0..ead2f12 100644
--- a/gio/gio-tool-mime.c
+++ b/gio/gio-tool-mime.c
@@ -156,14 +156,14 @@ handle_mime (int argc, char *argv[], gboolean do_help)
       info = get_app_info_for_id (handler);
       if (info == NULL)
         {
-          g_printerr (_("Failed to load info for handler “%s”\n"), handler);
+          print_error (_("Failed to load info for handler “%s”"), handler);
           return 1;
         }
 
       if (g_app_info_set_as_default_for_type (info, mimetype, &error) == FALSE)
         {
-          g_printerr (_("Failed to set “%s” as the default handler for “%s”: %s\n"),
-                      handler, mimetype, error->message);
+          print_error (_("Failed to set “%s” as the default handler for “%s”: %s\n"),
+                       handler, mimetype, error->message);
           g_error_free (error);
           g_object_unref (info);
           return 1;
diff --git a/gio/gio-tool-monitor.c b/gio/gio-tool-monitor.c
index 9e43d99..c8ab04f 100644
--- a/gio/gio-tool-monitor.c
+++ b/gio/gio-tool-monitor.c
@@ -180,7 +180,7 @@ add_watch (const gchar       *cmdline,
   return TRUE;
 
 err:
-  g_printerr ("error: %s: %s", cmdline, error->message);
+  print_file_error (file, error->message);
   g_error_free (error);
 
   return FALSE;
@@ -193,7 +193,6 @@ handle_monitor (int argc, gchar *argv[], gboolean do_help)
   gchar *param;
   GError *error = NULL;
   GFileMonitorFlags flags;
-  guint total = 0;
   guint i;
 
   g_set_prgname ("gio monitor");
@@ -220,6 +219,12 @@ handle_monitor (int argc, gchar *argv[], gboolean do_help)
       return 1;
     }
 
+  if (!watch_dirs || !watch_files || !watch_direct || !watch_silent || !watch_default)
+    {
+      show_help (context, _("No locations given"));
+      return 1;
+    }
+
   g_option_context_free (context);
 
   flags = (no_moves ? 0 : G_FILE_MONITOR_WATCH_MOVES) |
@@ -230,7 +235,6 @@ handle_monitor (int argc, gchar *argv[], gboolean do_help)
       for (i = 0; watch_dirs[i]; i++)
         if (!add_watch (watch_dirs[i], WATCH_DIR, flags, TRUE))
           return 1;
-      total++;
     }
 
   if (watch_files)
@@ -238,7 +242,6 @@ handle_monitor (int argc, gchar *argv[], gboolean do_help)
       for (i = 0; watch_files[i]; i++)
         if (!add_watch (watch_files[i], WATCH_FILE, flags, TRUE))
           return 1;
-      total++;
     }
 
   if (watch_direct)
@@ -246,7 +249,6 @@ handle_monitor (int argc, gchar *argv[], gboolean do_help)
       for (i = 0; watch_direct[i]; i++)
         if (!add_watch (watch_direct[i], WATCH_FILE, flags | G_FILE_MONITOR_WATCH_HARD_LINKS, TRUE))
           return 1;
-      total++;
     }
 
   if (watch_silent)
@@ -254,7 +256,6 @@ handle_monitor (int argc, gchar *argv[], gboolean do_help)
       for (i = 0; watch_silent[i]; i++)
         if (!add_watch (watch_silent[i], WATCH_FILE, flags | G_FILE_MONITOR_WATCH_HARD_LINKS, FALSE))
           return 1;
-      total++;
     }
 
   if (watch_default)
@@ -262,13 +263,6 @@ handle_monitor (int argc, gchar *argv[], gboolean do_help)
       for (i = 0; watch_default[i]; i++)
         if (!add_watch (watch_default[i], WATCH_AUTO, flags, TRUE))
           return 1;
-      total++;
-    }
-
-  if (!total)
-    {
-      g_printerr ("gio: Must give at least one file to monitor\n");
-      return 1;
     }
 
   while (TRUE)
diff --git a/gio/gio-tool-mount.c b/gio/gio-tool-mount.c
index 92d16c1..6f5cbb8 100644
--- a/gio/gio-tool-mount.c
+++ b/gio/gio-tool-mount.c
@@ -243,9 +243,9 @@ mount_mountable_done_cb (GObject *object,
     {
       success = FALSE;
       if (GPOINTER_TO_INT (g_object_get_data (G_OBJECT (op), "state")) == MOUNT_OP_ABORTED)
-        g_printerr (_("Error mounting location: Anonymous access denied\n"));
+        print_file_error (G_FILE (object), _("Anonymous access denied"));
       else if (!g_error_matches (error, G_IO_ERROR, G_IO_ERROR_FAILED_HANDLED))
-        g_printerr (_("Error mounting location: %s\n"), error->message);
+        print_file_error (G_FILE (object), error->message);
 
       g_error_free (error);
     }
@@ -273,9 +273,9 @@ mount_done_cb (GObject *object,
     {
       success = FALSE;
       if (GPOINTER_TO_INT (g_object_get_data (G_OBJECT (op), "state")) == MOUNT_OP_ABORTED)
-        g_printerr (_("Error mounting location: Anonymous access denied\n"));
+        print_file_error (G_FILE (object), _("Anonymous access denied"));
       else if (!g_error_matches (error, G_IO_ERROR, G_IO_ERROR_FAILED_HANDLED))
-        g_printerr (_("Error mounting location: %s\n"), error->message);
+        print_file_error (G_FILE (object), error->message);
 
       g_error_free (error);
     }
@@ -331,6 +331,7 @@ unmount_done_cb (GObject *object,
 {
   gboolean succeeded;
   GError *error = NULL;
+  GFile *file = G_FILE (user_data);
 
   succeeded = g_mount_unmount_with_operation_finish (G_MOUNT (object), res, &error);
 
@@ -338,11 +339,13 @@ unmount_done_cb (GObject *object,
 
   if (!succeeded)
     {
-      g_printerr (_("Error unmounting mount: %s\n"), error->message);
+      print_file_error (file, error->message);
       success = FALSE;
       g_error_free (error);
     }
 
+  g_object_unref (file);
+
   outstanding_mounts--;
 
   if (outstanding_mounts == 0)
@@ -363,7 +366,7 @@ unmount (GFile *file)
   mount = g_file_find_enclosing_mount (file, NULL, &error);
   if (mount == NULL)
     {
-      g_printerr (_("Error finding enclosing mount: %s\n"), error->message);
+      print_file_error (file, error->message);
       success = FALSE;
       g_error_free (error);
       return;
@@ -371,7 +374,7 @@ unmount (GFile *file)
 
   mount_op = new_mount_op ();
   flags = force ? G_MOUNT_UNMOUNT_FORCE : G_MOUNT_UNMOUNT_NONE;
-  g_mount_unmount_with_operation (mount, flags, mount_op, NULL, unmount_done_cb, NULL);
+  g_mount_unmount_with_operation (mount, flags, mount_op, NULL, unmount_done_cb, g_object_ref (file));
   g_object_unref (mount_op);
 
   outstanding_mounts++;
@@ -384,6 +387,7 @@ eject_done_cb (GObject *object,
 {
   gboolean succeeded;
   GError *error = NULL;
+  GFile *file = G_FILE (user_data);
 
   succeeded = g_mount_eject_with_operation_finish (G_MOUNT (object), res, &error);
 
@@ -391,11 +395,13 @@ eject_done_cb (GObject *object,
 
   if (!succeeded)
     {
-      g_printerr (_("Error ejecting mount: %s\n"), error->message);
+      print_file_error (file, error->message);
       success = FALSE;
       g_error_free (error);
     }
 
+  g_object_unref (file);
+
   outstanding_mounts--;
 
   if (outstanding_mounts == 0)
@@ -416,7 +422,7 @@ eject (GFile *file)
   mount = g_file_find_enclosing_mount (file, NULL, &error);
   if (mount == NULL)
     {
-      g_printerr (_("Error finding enclosing mount: %s\n"), error->message);
+      print_file_error (file, error->message);
       success = FALSE;
       g_error_free (error);
       return;
@@ -424,7 +430,7 @@ eject (GFile *file)
 
   mount_op = new_mount_op ();
   flags = force ? G_MOUNT_UNMOUNT_FORCE : G_MOUNT_UNMOUNT_NONE;
-  g_mount_eject_with_operation (mount, flags, mount_op, NULL, eject_done_cb, NULL);
+  g_mount_eject_with_operation (mount, flags, mount_op, NULL, eject_done_cb, g_object_ref (file));
   g_object_unref (mount_op);
 
   outstanding_mounts++;
@@ -865,6 +871,7 @@ mount_with_device_file_cb (GObject *object,
   GVolume *volume;
   gboolean succeeded;
   GError *error = NULL;
+  gchar *device_path = (gchar *)user_data;
 
   volume = G_VOLUME (object);
 
@@ -872,9 +879,7 @@ mount_with_device_file_cb (GObject *object,
 
   if (!succeeded)
     {
-      g_printerr (_("Error mounting %s: %s\n"),
-                  g_volume_get_identifier (volume, G_VOLUME_IDENTIFIER_KIND_UNIX_DEVICE),
-                  error->message);
+      print_error ("%s: %s", device_path, error->message);
       g_error_free (error);
       success = FALSE;
     }
@@ -888,15 +893,15 @@ mount_with_device_file_cb (GObject *object,
       root = g_mount_get_root (mount);
       mount_path = g_file_get_path (root);
 
-      g_print (_("Mounted %s at %s\n"),
-               g_volume_get_identifier (volume, G_VOLUME_IDENTIFIER_KIND_UNIX_DEVICE),
-               mount_path);
+      g_print (_("Mounted %s at %s\n"), device_path, mount_path);
 
       g_object_unref (mount);
       g_object_unref (root);
       g_free (mount_path);
     }
 
+  g_free (device_path);
+
   outstanding_mounts--;
 
   if (outstanding_mounts == 0)
@@ -916,9 +921,10 @@ mount_with_device_file (const char *device_file)
   for (l = volumes; l != NULL; l = l->next)
     {
       GVolume *volume = G_VOLUME (l->data);
+      gchar *id;
 
-      if (g_strcmp0 (g_volume_get_identifier (volume,
-                                              G_VOLUME_IDENTIFIER_KIND_UNIX_DEVICE), device_file) == 0)
+      id = g_volume_get_identifier (volume, G_VOLUME_IDENTIFIER_KIND_UNIX_DEVICE);
+      if (g_strcmp0 (id, device_file) == 0)
         {
           GMountOperation *op;
 
@@ -929,16 +935,18 @@ mount_with_device_file (const char *device_file)
                           op,
                           NULL,
                           mount_with_device_file_cb,
-                          op);
+                          id);
 
           outstanding_mounts++;
         }
+      else
+        g_free (id);
     }
   g_list_free_full (volumes, g_object_unref);
 
   if (outstanding_mounts == 0)
     {
-      g_print (_("No volume for device file %s\n"), device_file);
+      g_print ("%s: %s\n", device_file, _("No volume for device file"));
       return;
     }
 
diff --git a/gio/gio-tool-open.c b/gio/gio-tool-open.c
index 125b560..9d0847e 100644
--- a/gio/gio-tool-open.c
+++ b/gio/gio-tool-open.c
@@ -66,7 +66,7 @@ handle_open (int argc, char *argv[], gboolean do_help)
 
   if (argc < 2)
     {
-      show_help (context, _("No files to open"));
+      show_help (context, _("No locations given"));
       return 1;
     }
 
diff --git a/gio/gio-tool-remove.c b/gio/gio-tool-remove.c
index 2921d01..fafe176 100644
--- a/gio/gio-tool-remove.c
+++ b/gio/gio-tool-remove.c
@@ -67,7 +67,7 @@ handle_remove (int argc, char *argv[], gboolean do_help)
 
   if (argc == 1)
     {
-      show_help (context, _("No files to delete"));
+      show_help (context, _("No locations given"));
       return 1;
     }
 
diff --git a/gio/gio-tool-rename.c b/gio/gio-tool-rename.c
index c27d668..1f79745 100644
--- a/gio/gio-tool-rename.c
+++ b/gio/gio-tool-rename.c
@@ -81,7 +81,7 @@ handle_rename (int argc, char *argv[], gboolean do_help)
 
   if (new_file == NULL)
     {
-      g_printerr (_("Error: %s\n"), error->message);
+      print_error (error->message);
       g_error_free (error);
       retval = 1;
     }
diff --git a/gio/gio-tool-save.c b/gio/gio-tool-save.c
index a955a24..9dd6f9d 100644
--- a/gio/gio-tool-save.c
+++ b/gio/gio-tool-save.c
@@ -105,7 +105,7 @@ save (GFile *file)
              if (written == -1)
                {
                  save_res = FALSE;
-                 g_printerr ("gio: Error writing to stream: %s\n", error->message);
+                  print_error (error->message);
                  g_error_free (error);
                  goto out;
                }
@@ -116,7 +116,7 @@ save (GFile *file)
       else if (res < 0)
        {
          save_res = FALSE;
-          g_printerr ("gio: Error reading from standard input\n");
+          print_error (_("Error reading from standard input"));
          break;
        }
       else if (res == 0)
@@ -129,7 +129,7 @@ save (GFile *file)
   if (!close_res)
     {
       save_res = FALSE;
-      g_printerr ("gio: Error closing: %s\n", error->message);
+      print_error (error->message);
       g_error_free (error);
     }
 
diff --git a/gio/gio-tool-set.c b/gio/gio-tool-set.c
index 2b63aca..d80aa23 100644
--- a/gio/gio-tool-set.c
+++ b/gio/gio-tool-set.c
@@ -173,7 +173,7 @@ handle_set (int argc, char *argv[], gboolean do_help)
       break;
     case G_FILE_ATTRIBUTE_TYPE_OBJECT:
     default:
-      g_printerr (_("Invalid attribute type %s\n"), attr_type);
+      print_error (_("Invalid attribute type “%s“"), attr_type);
       return 1;
     }
 
@@ -186,7 +186,7 @@ handle_set (int argc, char *argv[], gboolean do_help)
                                G_FILE_QUERY_INFO_NONE,
                              NULL, &error))
     {
-      g_printerr (_("Error setting attribute: %s\n"), error->message);
+      print_error (error->message);
       g_error_free (error);
       return 1;
     }
diff --git a/gio/gio-tool.c b/gio/gio-tool.c
index 2937d8e..61ca87a 100644
--- a/gio/gio-tool.c
+++ b/gio/gio-tool.c
@@ -31,9 +31,35 @@
 
 
 void
-print_file_error (GFile *file, const char *message)
+print_error (const char *format, ...)
 {
-  g_printerr ("gio: %s: %s\n", g_file_get_uri (file), message);
+  gchar *message;
+  va_list args;
+
+  va_start (args, format);
+  message = g_strdup_vprintf (format, args);
+  va_end (args);
+
+  g_printerr ("gio: %s\n", message);
+  g_free (message);
+}
+
+void
+print_file_error (GFile *file, const gchar *format, ...)
+{
+  gchar *uri;
+  gchar *message;
+  va_list args;
+
+  va_start (args, format);
+  message = g_strdup_vprintf (format, args);
+  va_end (args);
+
+  uri = g_file_get_uri (file);
+  print_error ("%s: %s", uri, message);
+  g_free (uri);
+
+  g_free (message);
 }
 
 void
diff --git a/gio/gio-tool.h b/gio/gio-tool.h
index 966500d..154f302 100644
--- a/gio/gio-tool.h
+++ b/gio/gio-tool.h
@@ -20,8 +20,11 @@
 #ifndef __GIO_TOOL_H__
 #define __GIO_TOOL_H__
 
+void print_error      (const gchar    *format,
+                       ...);
 void print_file_error (GFile          *file,
-                       const char     *message);
+                       const gchar    *format,
+                       ...);
 void show_help        (GOptionContext *context,
                        const char     *message);
 


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]