g_file_set_contents(): don't allocate display name

g_file_set_contents() sets a GError in the event of various failures
that count occur.  It uses g_filename_display_name() in order to get the
filename to include in the messages.

Factor out the error handling to make it easier to allocate the display
name only when we need it (instead of allocating it every time).

https://bugzilla.gnome.org/show_bug.cgi?id=701560
This commit is contained in:
Ryan Lortie 2013-06-03 16:16:25 -04:00
parent d3be43fcc5
commit c152ceba09

View File

@ -1004,6 +1004,27 @@ rename_file (const char *old_name,
return TRUE;
}
/* format string must have two '%s':
*
* - the place for the filename
* - the place for the strerror
*/
static void
format_error_message (GError **error,
const gchar *filename,
const gchar *format_string)
{
gint saved_errno = errno;
gchar *display_name;
display_name = g_filename_display_name (filename);
g_set_error (error, G_FILE_ERROR, g_file_error_from_errno (saved_errno),
format_string, display_name, g_strerror (saved_errno));
g_free (display_name);
}
static gchar *
write_to_temp_file (const gchar *contents,
gssize length,
@ -1011,11 +1032,9 @@ write_to_temp_file (const gchar *contents,
GError **err)
{
gchar *tmp_name;
gchar *display_name;
gchar *retval;
FILE *file;
gint fd;
int save_errno;
retval = NULL;
@ -1023,18 +1042,10 @@ write_to_temp_file (const gchar *contents,
errno = 0;
fd = g_mkstemp_full (tmp_name, O_RDWR | O_BINARY, 0666);
save_errno = errno;
display_name = g_filename_display_name (tmp_name);
if (fd == -1)
{
g_set_error (err,
G_FILE_ERROR,
g_file_error_from_errno (save_errno),
_("Failed to create file '%s': %s"),
display_name, g_strerror (save_errno));
format_error_message (err, tmp_name, _("Failed to create file '%s': %s"));
goto out;
}
@ -1042,14 +1053,7 @@ write_to_temp_file (const gchar *contents,
file = fdopen (fd, "wb");
if (!file)
{
save_errno = errno;
g_set_error (err,
G_FILE_ERROR,
g_file_error_from_errno (save_errno),
_("Failed to open file '%s' for writing: fdopen() failed: %s"),
display_name,
g_strerror (save_errno));
format_error_message (err, tmp_name, _("Failed to open file '%s' for writing: fdopen() failed: %s"));
close (fd);
g_unlink (tmp_name);
@ -1073,18 +1077,9 @@ write_to_temp_file (const gchar *contents,
if (n_written < length)
{
save_errno = errno;
g_set_error (err,
G_FILE_ERROR,
g_file_error_from_errno (save_errno),
_("Failed to write file '%s': fwrite() failed: %s"),
display_name,
g_strerror (save_errno));
format_error_message (err, tmp_name, _("Failed to write file '%s': fwrite() failed: %s"));
fclose (file);
g_unlink (tmp_name);
goto out;
}
}
@ -1092,15 +1087,7 @@ write_to_temp_file (const gchar *contents,
errno = 0;
if (fflush (file) != 0)
{
save_errno = errno;
g_set_error (err,
G_FILE_ERROR,
g_file_error_from_errno (save_errno),
_("Failed to write file '%s': fflush() failed: %s"),
display_name,
g_strerror (save_errno));
format_error_message (err, tmp_name, _("Failed to write file '%s': fflush() failed: %s"));
fclose (file);
g_unlink (tmp_name);
@ -1132,19 +1119,9 @@ write_to_temp_file (const gchar *contents,
* the new and the old file on some filesystems. (I.E. those that don't
* guarantee the data is written to the disk before the metadata.)
*/
if (g_lstat (dest_file, &statbuf) == 0 &&
statbuf.st_size > 0 &&
fsync (fileno (file)) != 0)
if (g_lstat (dest_file, &statbuf) == 0 && statbuf.st_size > 0 && fsync (fileno (file)) != 0)
{
save_errno = errno;
g_set_error (err,
G_FILE_ERROR,
g_file_error_from_errno (save_errno),
_("Failed to write file '%s': fsync() failed: %s"),
display_name,
g_strerror (save_errno));
format_error_message (err, tmp_name, _("Failed to write file '%s': fsync() failed: %s"));
fclose (file);
g_unlink (tmp_name);
@ -1160,15 +1137,7 @@ write_to_temp_file (const gchar *contents,
errno = 0;
if (fclose (file) == EOF)
{
save_errno = errno;
g_set_error (err,
G_FILE_ERROR,
g_file_error_from_errno (save_errno),
_("Failed to close file '%s': fclose() failed: %s"),
display_name,
g_strerror (save_errno));
format_error_message (err, tmp_name, _("Failed to close file '%s': fclose() failed: %s"));
g_unlink (tmp_name);
goto out;
@ -1178,7 +1147,6 @@ write_to_temp_file (const gchar *contents,
out:
g_free (tmp_name);
g_free (display_name);
return retval;
}