Improve error reporting

Include the filename for the file in question in many of the
error messages in glocalfile.c. This is useful information when
diagnosing such errors, so make it easily available.

http://bugzilla.gnome.org/show_bug.cgi?id=754012
This commit is contained in:
Matthias Clasen 2015-08-26 11:06:31 -04:00
parent b08a8dc949
commit 21ceeed3b9

View File

@ -956,6 +956,21 @@ get_filesystem_readonly (GFileInfo *info,
#endif /* G_OS_WIN32 */ #endif /* G_OS_WIN32 */
static void
g_set_io_error (GError **error,
const gchar *msg,
GFile *file,
gint errsv)
{
GLocalFile *local = G_LOCAL_FILE (file);
gchar *display_name;
display_name = g_filename_display_name (local->filename);
g_set_error (error, G_IO_ERROR, g_io_error_from_errno (errsv),
msg, display_name, g_strerror (errsv));
g_free (display_name);
}
static GFileInfo * static GFileInfo *
g_local_file_query_filesystem_info (GFile *file, g_local_file_query_filesystem_info (GFile *file,
const char *attributes, const char *attributes,
@ -1010,10 +1025,9 @@ g_local_file_query_filesystem_info (GFile *file,
{ {
int errsv = errno; int errsv = errno;
g_set_error (error, G_IO_ERROR, g_set_io_error (error,
g_io_error_from_errno (errsv), _("Error getting filesystem info for %s: %s"),
_("Error getting filesystem info: %s"), file, errsv);
g_strerror (errsv));
return NULL; return NULL;
} }
@ -1134,36 +1148,26 @@ g_local_file_find_enclosing_mount (GFile *file,
GMount *mount; GMount *mount;
if (g_lstat (local->filename, &buf) != 0) if (g_lstat (local->filename, &buf) != 0)
{ goto error;
g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_NOT_FOUND,
/* Translators: This is an error message when trying to
* find the enclosing (user visible) mount of a file, but
* none exists. */
_("Containing mount does not exist"));
return NULL;
}
mountpoint = find_mountpoint_for (local->filename, buf.st_dev); mountpoint = find_mountpoint_for (local->filename, buf.st_dev);
if (mountpoint == NULL) if (mountpoint == NULL)
{ goto error;
g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_NOT_FOUND,
/* Translators: This is an error message when trying to
* find the enclosing (user visible) mount of a file, but
* none exists. */
_("Containing mount does not exist"));
return NULL;
}
mount = _g_mount_get_for_mount_path (mountpoint, cancellable); mount = _g_mount_get_for_mount_path (mountpoint, cancellable);
g_free (mountpoint); g_free (mountpoint);
if (mount) if (mount)
return mount; return mount;
g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_NOT_FOUND, error:
/* Translators: This is an error message when trying to find /* Translators: This is an error message when trying to find
* the enclosing (user visible) mount of a file, but none * the enclosing (user visible) mount of a file, but none
* exists. */ * exists.
_("Containing mount does not exist")); */
g_set_io_error (error,
_("Containing mount for file %s not found"),
file, 0);
return NULL; return NULL;
} }
@ -1183,8 +1187,7 @@ g_local_file_set_display_name (GFile *file,
parent = g_file_get_parent (file); parent = g_file_get_parent (file);
if (parent == NULL) if (parent == NULL)
{ {
g_set_error_literal (error, G_IO_ERROR, g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_FAILED,
G_IO_ERROR_FAILED,
_("Can't rename root directory")); _("Can't rename root directory"));
return NULL; return NULL;
} }
@ -1203,17 +1206,13 @@ g_local_file_set_display_name (GFile *file,
if (errsv != ENOENT) if (errsv != ENOENT)
{ {
g_set_error (error, G_IO_ERROR, g_set_io_error (error, _("Error renaming file %s: %s"), new_file, errsv);
g_io_error_from_errno (errsv),
_("Error renaming file: %s"),
g_strerror (errsv));
return NULL; return NULL;
} }
} }
else else
{ {
g_set_error_literal (error, G_IO_ERROR, g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_EXISTS,
G_IO_ERROR_EXISTS,
_("Can't rename file, filename already exists")); _("Can't rename file, filename already exists"));
return NULL; return NULL;
} }
@ -1223,16 +1222,15 @@ g_local_file_set_display_name (GFile *file,
errsv = errno; errsv = errno;
if (errsv == EINVAL) if (errsv == EINVAL)
/* We can't get a rename file into itself error herer, /* We can't get a rename file into itself error here,
so this must be an invalid filename, on e.g. FAT */ * so this must be an invalid filename, on e.g. FAT
g_set_error_literal (error, G_IO_ERROR, */
G_IO_ERROR_INVALID_FILENAME, g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_INVALID_FILENAME,
_("Invalid filename")); _("Invalid filename"));
else else
g_set_error (error, G_IO_ERROR, g_set_io_error (error,
g_io_error_from_errno (errsv), _("Error renaming file %s: %s"),
_("Error renaming file: %s"), file, errsv);
g_strerror (errsv));
g_object_unref (new_file); g_object_unref (new_file);
return NULL; return NULL;
} }
@ -1391,19 +1389,12 @@ g_local_file_read (GFile *file,
{ {
ret = _stati64 (local->filename, &buf); ret = _stati64 (local->filename, &buf);
if (ret == 0 && S_ISDIR (buf.st_mode)) if (ret == 0 && S_ISDIR (buf.st_mode))
{ errsv = EISDIR;
g_set_error_literal (error, G_IO_ERROR,
G_IO_ERROR_IS_DIRECTORY,
_("Can't open directory"));
return NULL;
}
} }
#endif #endif
g_set_io_error (error,
g_set_error (error, G_IO_ERROR, _("Error opening file %s: %s"),
g_io_error_from_errno (errsv), file, errsv);
_("Error opening file: %s"),
g_strerror (errsv));
return NULL; return NULL;
} }
@ -1416,9 +1407,9 @@ g_local_file_read (GFile *file,
if (ret == 0 && S_ISDIR (buf.st_mode)) if (ret == 0 && S_ISDIR (buf.st_mode))
{ {
(void) g_close (fd, NULL); (void) g_close (fd, NULL);
g_set_error_literal (error, G_IO_ERROR, g_set_io_error (error,
G_IO_ERROR_IS_DIRECTORY, _("Error opening file %s: %s"),
_("Can't open directory")); file, EISDIR);
return NULL; return NULL;
} }
@ -1541,10 +1532,9 @@ g_local_file_delete (GFile *file,
if (errsv == EEXIST) if (errsv == EEXIST)
errsv = ENOTEMPTY; errsv = ENOTEMPTY;
g_set_error (error, G_IO_ERROR, g_set_io_error (error,
g_io_error_from_errno (errsv), _("Error removing file %s: %s"),
_("Error removing file: %s"), file, errsv);
g_strerror (errsv));
return FALSE; return FALSE;
} }
@ -1925,10 +1915,9 @@ g_local_file_trash (GFile *file,
{ {
int errsv = errno; int errsv = errno;
g_set_error (error, G_IO_ERROR, g_set_io_error (error,
g_io_error_from_errno (errsv), _("Error trashing file %s: %s"),
_("Error trashing file: %s"), file, errsv);
g_strerror (errsv));
return FALSE; return FALSE;
} }
@ -1969,9 +1958,9 @@ g_local_file_trash (GFile *file,
topdir = find_topdir_for (local->filename); topdir = find_topdir_for (local->filename);
if (topdir == NULL) if (topdir == NULL)
{ {
g_set_error_literal (error, G_IO_ERROR, g_set_io_error (error,
G_IO_ERROR_NOT_SUPPORTED, _("Unable to find toplevel directory to trash %s"),
_("Unable to find toplevel directory for trash")); file, G_IO_ERROR_NOT_SUPPORTED);
return FALSE; return FALSE;
} }
@ -2048,9 +2037,9 @@ g_local_file_trash (GFile *file,
if (trashdir == NULL) if (trashdir == NULL)
{ {
g_free (topdir); g_free (topdir);
g_set_error_literal (error, G_IO_ERROR, g_set_io_error (error,
G_IO_ERROR_NOT_SUPPORTED, _("Unable to find or create trash directory for %s"),
_("Unable to find or create trash directory")); file, G_IO_ERROR_NOT_SUPPORTED);
return FALSE; return FALSE;
} }
} }
@ -2068,9 +2057,9 @@ g_local_file_trash (GFile *file,
g_free (topdir); g_free (topdir);
g_free (infodir); g_free (infodir);
g_free (filesdir); g_free (filesdir);
g_set_error_literal (error, G_IO_ERROR, g_set_io_error (error,
G_IO_ERROR_NOT_SUPPORTED, _("Unable to find or create trash directory for %s"),
_("Unable to find or create trash directory")); file, G_IO_ERROR_NOT_SUPPORTED);
return FALSE; return FALSE;
} }
@ -2102,10 +2091,9 @@ g_local_file_trash (GFile *file,
g_free (trashname); g_free (trashname);
g_free (infofile); g_free (infofile);
g_set_error (error, G_IO_ERROR, g_set_io_error (error,
g_io_error_from_errno (errsv), _("Unable to create trashing info file for %s: %s"),
_("Unable to create trashing info file: %s"), file, errsv);
g_strerror (errsv));
return FALSE; return FALSE;
} }
@ -2140,7 +2128,8 @@ g_local_file_trash (GFile *file,
g_file_set_contents (infofile, data, -1, NULL); g_file_set_contents (infofile, data, -1, NULL);
/* TODO: Maybe we should verify that you can delete the file from the trash /* TODO: Maybe we should verify that you can delete the file from the trash
before moving it? OTOH, that is hard, as it needs a recursive scan */ * before moving it? OTOH, that is hard, as it needs a recursive scan
*/
trashfile = g_build_filename (filesdir, trashname, NULL); trashfile = g_build_filename (filesdir, trashname, NULL);
@ -2158,17 +2147,16 @@ g_local_file_trash (GFile *file,
if (errsv == EXDEV) if (errsv == EXDEV)
/* The trash dir was actually on another fs anyway!? /* The trash dir was actually on another fs anyway!?
This can happen when the same device is mounted multiple * This can happen when the same device is mounted multiple
times, or with bind mounts of the same fs. */ * times, or with bind mounts of the same fs.
g_set_error (error, G_IO_ERROR, */
G_IO_ERROR_NOT_SUPPORTED, g_set_io_error (error,
_("Unable to trash file: %s"), _("Unable to trash file %s across filesystem boundaries"),
g_strerror (errsv)); file, ENOTSUP);
else else
g_set_error (error, G_IO_ERROR, g_set_io_error (error,
g_io_error_from_errno (errsv), _("Unable to trash file %s: %s"),
_("Unable to trash file: %s"), file, errsv);
g_strerror (errsv));
return FALSE; return FALSE;
} }
@ -2222,17 +2210,15 @@ g_local_file_trash (GFile *file,
{ {
if (cancellable && !g_cancellable_is_cancelled (cancellable)) if (cancellable && !g_cancellable_is_cancelled (cancellable))
g_cancellable_cancel (cancellable); g_cancellable_cancel (cancellable);
g_set_error (error, G_IO_ERROR, g_set_io_error (error,
G_IO_ERROR_CANCELLED, _("Unable to trash file %s: %s"),
_("Unable to trash file: %s"), file, ECANCELED);
_("Operation was cancelled"));
success = FALSE; success = FALSE;
} }
else if (!success) else if (!success)
g_set_error (error, G_IO_ERROR, g_set_io_error (error,
G_IO_ERROR_FAILED, _("Unable to trash file %s"),
_("Unable to trash file: %s"), file, 0);
_("internal error"));
g_free (wfilename); g_free (wfilename);
return success; return success;
@ -2256,10 +2242,9 @@ g_local_file_make_directory (GFile *file,
G_IO_ERROR_INVALID_FILENAME, G_IO_ERROR_INVALID_FILENAME,
_("Invalid filename")); _("Invalid filename"));
else else
g_set_error (error, G_IO_ERROR, g_set_io_error (error,
g_io_error_from_errno (errsv), _("Error creating directory %s: %s"),
_("Error creating directory: %s"), file, errsv);
g_strerror (errsv));
return FALSE; return FALSE;
} }
@ -2289,15 +2274,14 @@ g_local_file_make_symbolic_link (GFile *file,
G_IO_ERROR_NOT_SUPPORTED, G_IO_ERROR_NOT_SUPPORTED,
_("Filesystem does not support symbolic links")); _("Filesystem does not support symbolic links"));
else else
g_set_error (error, G_IO_ERROR, g_set_io_error (error,
g_io_error_from_errno (errsv), _("Error making symbolic link %s: %s"),
_("Error making symbolic link: %s"), file, errsv);
g_strerror (errsv));
return FALSE; return FALSE;
} }
return TRUE; return TRUE;
#else #else
g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED, "Symlinks not supported"); g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED, _("Symbolic links not supported"));
return FALSE; return FALSE;
#endif #endif
} }
@ -2351,10 +2335,9 @@ g_local_file_move (GFile *source,
{ {
int errsv = errno; int errsv = errno;
g_set_error (error, G_IO_ERROR, g_set_io_error (error,
g_io_error_from_errno (errsv), _("Error moving file %s: %s"),
_("Error moving file: %s"), source, errsv);
g_strerror (errsv));
return FALSE; return FALSE;
} }
@ -2387,10 +2370,9 @@ g_local_file_move (GFile *source,
} }
else else
{ {
g_set_error_literal (error, g_set_io_error (error,
G_IO_ERROR, _("Error moving file %s: %s"),
G_IO_ERROR_EXISTS, source, EEXIST);
_("Target file exists"));
return FALSE; return FALSE;
} }
} }
@ -2445,10 +2427,9 @@ g_local_file_move (GFile *source,
G_IO_ERROR_INVALID_FILENAME, G_IO_ERROR_INVALID_FILENAME,
_("Invalid filename")); _("Invalid filename"));
else else
g_set_error (error, G_IO_ERROR, g_set_io_error (error,
g_io_error_from_errno (errsv), _("Error moving file %s: %s"),
_("Error moving file: %s"), source, errsv);
g_strerror (errsv));
return FALSE; return FALSE;
} }