Commit c152ceba authored by Allison Karlitskaya's avatar Allison Karlitskaya
Browse files

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
parent d3be43fc
......@@ -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,30 +1032,20 @@ 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;
tmp_name = g_strdup_printf ("%s.XXXXXX", dest_file);
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,17 +1053,10 @@ 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);
goto out;
}
......@@ -1066,44 +1070,27 @@ write_to_temp_file (const gchar *contents,
*/
(void) posix_fallocate (fd, 0, length);
#endif
errno = 0;
n_written = fwrite (contents, 1, length, file);
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));
fclose (file);
g_unlink (tmp_name);
goto out;
}
{
format_error_message (err, tmp_name, _("Failed to write file '%s': fwrite() failed: %s"));
fclose (file);
g_unlink (tmp_name);
goto out;
}
}
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);
goto out;
}
......@@ -1120,7 +1107,7 @@ write_to_temp_file (const gchar *contents,
goto no_fsync;
}
#endif
#ifdef HAVE_FSYNC
{
struct stat statbuf;
......@@ -1132,23 +1119,13 @@ 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);
g_unlink (tmp_name);
goto out;
goto out;
}
}
#endif
......@@ -1156,30 +1133,21 @@ write_to_temp_file (const gchar *contents,
#ifdef BTRFS_SUPER_MAGIC
no_fsync:
#endif
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;
}
retval = g_strdup (tmp_name);
out:
g_free (tmp_name);
g_free (display_name);
return retval;
}
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment