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
This commit is contained in:
Ondrej Holy 2016-12-16 14:32:29 +01:00
parent a83ccc535f
commit 0beeeb2ec9
12 changed files with 83 additions and 52 deletions

View File

@ -77,7 +77,7 @@ cat (GFile *file)
if (written == -1 && errno != EINTR) if (written == -1 && errno != EINTR)
{ {
print_file_error (file, "error writing to stdout"); print_file_error (file, _("Error writing to stdout"));
success = FALSE; success = FALSE;
goto out; goto out;
} }
@ -148,7 +148,7 @@ handle_cat (int argc, char *argv[], gboolean do_help)
if (argc < 2) if (argc < 2)
{ {
show_help (context, _("No files given")); show_help (context, _("No locations given"));
return 1; return 1;
} }

View File

@ -187,7 +187,7 @@ query_info (GFile *file)
if (info == NULL) if (info == NULL)
{ {
g_printerr ("Error getting info: %s\n", error->message); print_file_error (file, error->message);
g_error_free (error); g_error_free (error);
return FALSE; return FALSE;
} }
@ -218,7 +218,7 @@ get_writable_info (GFile *file)
list = g_file_query_settable_attributes (file, NULL, &error); list = g_file_query_settable_attributes (file, NULL, &error);
if (list == NULL) if (list == NULL)
{ {
g_printerr (_("Error getting writable attributes: %s\n"), error->message); print_file_error (file, error->message);
g_error_free (error); g_error_free (error);
return FALSE; return FALSE;
} }
@ -239,7 +239,7 @@ get_writable_info (GFile *file)
list = g_file_query_writable_namespaces (file, NULL, &error); list = g_file_query_writable_namespaces (file, NULL, &error);
if (list == NULL) if (list == NULL)
{ {
g_printerr ("Error getting writable namespaces: %s\n", error->message); print_file_error (file, error->message);
g_error_free (error); g_error_free (error);
return FALSE; return FALSE;
} }

View File

@ -156,14 +156,14 @@ handle_mime (int argc, char *argv[], gboolean do_help)
info = get_app_info_for_id (handler); info = get_app_info_for_id (handler);
if (info == NULL) 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; return 1;
} }
if (g_app_info_set_as_default_for_type (info, mimetype, &error) == FALSE) 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"), print_error (_("Failed to set “%s” as the default handler for “%s”: %s\n"),
handler, mimetype, error->message); handler, mimetype, error->message);
g_error_free (error); g_error_free (error);
g_object_unref (info); g_object_unref (info);
return 1; return 1;

View File

@ -180,7 +180,7 @@ add_watch (const gchar *cmdline,
return TRUE; return TRUE;
err: err:
g_printerr ("error: %s: %s", cmdline, error->message); print_file_error (file, error->message);
g_error_free (error); g_error_free (error);
return FALSE; return FALSE;
@ -193,7 +193,6 @@ handle_monitor (int argc, gchar *argv[], gboolean do_help)
gchar *param; gchar *param;
GError *error = NULL; GError *error = NULL;
GFileMonitorFlags flags; GFileMonitorFlags flags;
guint total = 0;
guint i; guint i;
g_set_prgname ("gio monitor"); g_set_prgname ("gio monitor");
@ -220,6 +219,12 @@ handle_monitor (int argc, gchar *argv[], gboolean do_help)
return 1; 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); g_option_context_free (context);
flags = (no_moves ? 0 : G_FILE_MONITOR_WATCH_MOVES) | 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++) for (i = 0; watch_dirs[i]; i++)
if (!add_watch (watch_dirs[i], WATCH_DIR, flags, TRUE)) if (!add_watch (watch_dirs[i], WATCH_DIR, flags, TRUE))
return 1; return 1;
total++;
} }
if (watch_files) if (watch_files)
@ -238,7 +242,6 @@ handle_monitor (int argc, gchar *argv[], gboolean do_help)
for (i = 0; watch_files[i]; i++) for (i = 0; watch_files[i]; i++)
if (!add_watch (watch_files[i], WATCH_FILE, flags, TRUE)) if (!add_watch (watch_files[i], WATCH_FILE, flags, TRUE))
return 1; return 1;
total++;
} }
if (watch_direct) if (watch_direct)
@ -246,7 +249,6 @@ handle_monitor (int argc, gchar *argv[], gboolean do_help)
for (i = 0; watch_direct[i]; i++) for (i = 0; watch_direct[i]; i++)
if (!add_watch (watch_direct[i], WATCH_FILE, flags | G_FILE_MONITOR_WATCH_HARD_LINKS, TRUE)) if (!add_watch (watch_direct[i], WATCH_FILE, flags | G_FILE_MONITOR_WATCH_HARD_LINKS, TRUE))
return 1; return 1;
total++;
} }
if (watch_silent) if (watch_silent)
@ -254,7 +256,6 @@ handle_monitor (int argc, gchar *argv[], gboolean do_help)
for (i = 0; watch_silent[i]; i++) for (i = 0; watch_silent[i]; i++)
if (!add_watch (watch_silent[i], WATCH_FILE, flags | G_FILE_MONITOR_WATCH_HARD_LINKS, FALSE)) if (!add_watch (watch_silent[i], WATCH_FILE, flags | G_FILE_MONITOR_WATCH_HARD_LINKS, FALSE))
return 1; return 1;
total++;
} }
if (watch_default) if (watch_default)
@ -262,13 +263,6 @@ handle_monitor (int argc, gchar *argv[], gboolean do_help)
for (i = 0; watch_default[i]; i++) for (i = 0; watch_default[i]; i++)
if (!add_watch (watch_default[i], WATCH_AUTO, flags, TRUE)) if (!add_watch (watch_default[i], WATCH_AUTO, flags, TRUE))
return 1; return 1;
total++;
}
if (!total)
{
g_printerr ("gio: Must give at least one file to monitor\n");
return 1;
} }
while (TRUE) while (TRUE)

View File

@ -243,9 +243,9 @@ mount_mountable_done_cb (GObject *object,
{ {
success = FALSE; success = FALSE;
if (GPOINTER_TO_INT (g_object_get_data (G_OBJECT (op), "state")) == MOUNT_OP_ABORTED) 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)) 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); g_error_free (error);
} }
@ -273,9 +273,9 @@ mount_done_cb (GObject *object,
{ {
success = FALSE; success = FALSE;
if (GPOINTER_TO_INT (g_object_get_data (G_OBJECT (op), "state")) == MOUNT_OP_ABORTED) 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)) 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); g_error_free (error);
} }
@ -331,6 +331,7 @@ unmount_done_cb (GObject *object,
{ {
gboolean succeeded; gboolean succeeded;
GError *error = NULL; GError *error = NULL;
GFile *file = G_FILE (user_data);
succeeded = g_mount_unmount_with_operation_finish (G_MOUNT (object), res, &error); succeeded = g_mount_unmount_with_operation_finish (G_MOUNT (object), res, &error);
@ -338,11 +339,13 @@ unmount_done_cb (GObject *object,
if (!succeeded) if (!succeeded)
{ {
g_printerr (_("Error unmounting mount: %s\n"), error->message); print_file_error (file, error->message);
success = FALSE; success = FALSE;
g_error_free (error); g_error_free (error);
} }
g_object_unref (file);
outstanding_mounts--; outstanding_mounts--;
if (outstanding_mounts == 0) if (outstanding_mounts == 0)
@ -363,7 +366,7 @@ unmount (GFile *file)
mount = g_file_find_enclosing_mount (file, NULL, &error); mount = g_file_find_enclosing_mount (file, NULL, &error);
if (mount == NULL) if (mount == NULL)
{ {
g_printerr (_("Error finding enclosing mount: %s\n"), error->message); print_file_error (file, error->message);
success = FALSE; success = FALSE;
g_error_free (error); g_error_free (error);
return; return;
@ -371,7 +374,7 @@ unmount (GFile *file)
mount_op = new_mount_op (); mount_op = new_mount_op ();
flags = force ? G_MOUNT_UNMOUNT_FORCE : G_MOUNT_UNMOUNT_NONE; 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); g_object_unref (mount_op);
outstanding_mounts++; outstanding_mounts++;
@ -384,6 +387,7 @@ eject_done_cb (GObject *object,
{ {
gboolean succeeded; gboolean succeeded;
GError *error = NULL; GError *error = NULL;
GFile *file = G_FILE (user_data);
succeeded = g_mount_eject_with_operation_finish (G_MOUNT (object), res, &error); succeeded = g_mount_eject_with_operation_finish (G_MOUNT (object), res, &error);
@ -391,11 +395,13 @@ eject_done_cb (GObject *object,
if (!succeeded) if (!succeeded)
{ {
g_printerr (_("Error ejecting mount: %s\n"), error->message); print_file_error (file, error->message);
success = FALSE; success = FALSE;
g_error_free (error); g_error_free (error);
} }
g_object_unref (file);
outstanding_mounts--; outstanding_mounts--;
if (outstanding_mounts == 0) if (outstanding_mounts == 0)
@ -416,7 +422,7 @@ eject (GFile *file)
mount = g_file_find_enclosing_mount (file, NULL, &error); mount = g_file_find_enclosing_mount (file, NULL, &error);
if (mount == NULL) if (mount == NULL)
{ {
g_printerr (_("Error finding enclosing mount: %s\n"), error->message); print_file_error (file, error->message);
success = FALSE; success = FALSE;
g_error_free (error); g_error_free (error);
return; return;
@ -424,7 +430,7 @@ eject (GFile *file)
mount_op = new_mount_op (); mount_op = new_mount_op ();
flags = force ? G_MOUNT_UNMOUNT_FORCE : G_MOUNT_UNMOUNT_NONE; 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); g_object_unref (mount_op);
outstanding_mounts++; outstanding_mounts++;
@ -865,6 +871,7 @@ mount_with_device_file_cb (GObject *object,
GVolume *volume; GVolume *volume;
gboolean succeeded; gboolean succeeded;
GError *error = NULL; GError *error = NULL;
gchar *device_path = (gchar *)user_data;
volume = G_VOLUME (object); volume = G_VOLUME (object);
@ -872,9 +879,7 @@ mount_with_device_file_cb (GObject *object,
if (!succeeded) if (!succeeded)
{ {
g_printerr (_("Error mounting %s: %s\n"), print_error ("%s: %s", device_path, error->message);
g_volume_get_identifier (volume, G_VOLUME_IDENTIFIER_KIND_UNIX_DEVICE),
error->message);
g_error_free (error); g_error_free (error);
success = FALSE; success = FALSE;
} }
@ -888,15 +893,15 @@ mount_with_device_file_cb (GObject *object,
root = g_mount_get_root (mount); root = g_mount_get_root (mount);
mount_path = g_file_get_path (root); mount_path = g_file_get_path (root);
g_print (_("Mounted %s at %s\n"), g_print (_("Mounted %s at %s\n"), device_path, mount_path);
g_volume_get_identifier (volume, G_VOLUME_IDENTIFIER_KIND_UNIX_DEVICE),
mount_path);
g_object_unref (mount); g_object_unref (mount);
g_object_unref (root); g_object_unref (root);
g_free (mount_path); g_free (mount_path);
} }
g_free (device_path);
outstanding_mounts--; outstanding_mounts--;
if (outstanding_mounts == 0) if (outstanding_mounts == 0)
@ -916,9 +921,10 @@ mount_with_device_file (const char *device_file)
for (l = volumes; l != NULL; l = l->next) for (l = volumes; l != NULL; l = l->next)
{ {
GVolume *volume = G_VOLUME (l->data); GVolume *volume = G_VOLUME (l->data);
gchar *id;
if (g_strcmp0 (g_volume_get_identifier (volume, id = g_volume_get_identifier (volume, G_VOLUME_IDENTIFIER_KIND_UNIX_DEVICE);
G_VOLUME_IDENTIFIER_KIND_UNIX_DEVICE), device_file) == 0) if (g_strcmp0 (id, device_file) == 0)
{ {
GMountOperation *op; GMountOperation *op;
@ -929,16 +935,18 @@ mount_with_device_file (const char *device_file)
op, op,
NULL, NULL,
mount_with_device_file_cb, mount_with_device_file_cb,
op); id);
outstanding_mounts++; outstanding_mounts++;
} }
else
g_free (id);
} }
g_list_free_full (volumes, g_object_unref); g_list_free_full (volumes, g_object_unref);
if (outstanding_mounts == 0) 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; return;
} }

View File

@ -66,7 +66,7 @@ handle_open (int argc, char *argv[], gboolean do_help)
if (argc < 2) if (argc < 2)
{ {
show_help (context, _("No files to open")); show_help (context, _("No locations given"));
return 1; return 1;
} }

View File

@ -67,7 +67,7 @@ handle_remove (int argc, char *argv[], gboolean do_help)
if (argc == 1) if (argc == 1)
{ {
show_help (context, _("No files to delete")); show_help (context, _("No locations given"));
return 1; return 1;
} }

View File

@ -81,7 +81,7 @@ handle_rename (int argc, char *argv[], gboolean do_help)
if (new_file == NULL) if (new_file == NULL)
{ {
g_printerr (_("Error: %s\n"), error->message); print_error (error->message);
g_error_free (error); g_error_free (error);
retval = 1; retval = 1;
} }

View File

@ -105,7 +105,7 @@ save (GFile *file)
if (written == -1) if (written == -1)
{ {
save_res = FALSE; save_res = FALSE;
g_printerr ("gio: Error writing to stream: %s\n", error->message); print_error (error->message);
g_error_free (error); g_error_free (error);
goto out; goto out;
} }
@ -116,7 +116,7 @@ save (GFile *file)
else if (res < 0) else if (res < 0)
{ {
save_res = FALSE; save_res = FALSE;
g_printerr ("gio: Error reading from standard input\n"); print_error (_("Error reading from standard input"));
break; break;
} }
else if (res == 0) else if (res == 0)
@ -129,7 +129,7 @@ save (GFile *file)
if (!close_res) if (!close_res)
{ {
save_res = FALSE; save_res = FALSE;
g_printerr ("gio: Error closing: %s\n", error->message); print_error (error->message);
g_error_free (error); g_error_free (error);
} }

View File

@ -173,7 +173,7 @@ handle_set (int argc, char *argv[], gboolean do_help)
break; break;
case G_FILE_ATTRIBUTE_TYPE_OBJECT: case G_FILE_ATTRIBUTE_TYPE_OBJECT:
default: default:
g_printerr (_("Invalid attribute type %s\n"), attr_type); print_error (_("Invalid attribute type “%s“"), attr_type);
return 1; return 1;
} }
@ -186,7 +186,7 @@ handle_set (int argc, char *argv[], gboolean do_help)
G_FILE_QUERY_INFO_NONE, G_FILE_QUERY_INFO_NONE,
NULL, &error)) NULL, &error))
{ {
g_printerr (_("Error setting attribute: %s\n"), error->message); print_error (error->message);
g_error_free (error); g_error_free (error);
return 1; return 1;
} }

View File

@ -31,9 +31,35 @@
void 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 void

View File

@ -20,8 +20,11 @@
#ifndef __GIO_TOOL_H__ #ifndef __GIO_TOOL_H__
#define __GIO_TOOL_H__ #define __GIO_TOOL_H__
void print_error (const gchar *format,
...);
void print_file_error (GFile *file, void print_file_error (GFile *file,
const char *message); const gchar *format,
...);
void show_help (GOptionContext *context, void show_help (GOptionContext *context,
const char *message); const char *message);