mirror of
https://gitlab.gnome.org/GNOME/glib.git
synced 2025-03-31 21:03:10 +02:00
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:
parent
d3be43fcc5
commit
c152ceba09
@ -1004,6 +1004,27 @@ rename_file (const char *old_name,
|
|||||||
return TRUE;
|
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 *
|
static gchar *
|
||||||
write_to_temp_file (const gchar *contents,
|
write_to_temp_file (const gchar *contents,
|
||||||
gssize length,
|
gssize length,
|
||||||
@ -1011,30 +1032,20 @@ write_to_temp_file (const gchar *contents,
|
|||||||
GError **err)
|
GError **err)
|
||||||
{
|
{
|
||||||
gchar *tmp_name;
|
gchar *tmp_name;
|
||||||
gchar *display_name;
|
|
||||||
gchar *retval;
|
gchar *retval;
|
||||||
FILE *file;
|
FILE *file;
|
||||||
gint fd;
|
gint fd;
|
||||||
int save_errno;
|
|
||||||
|
|
||||||
retval = NULL;
|
retval = NULL;
|
||||||
|
|
||||||
tmp_name = g_strdup_printf ("%s.XXXXXX", dest_file);
|
tmp_name = g_strdup_printf ("%s.XXXXXX", dest_file);
|
||||||
|
|
||||||
errno = 0;
|
errno = 0;
|
||||||
fd = g_mkstemp_full (tmp_name, O_RDWR | O_BINARY, 0666);
|
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)
|
if (fd == -1)
|
||||||
{
|
{
|
||||||
g_set_error (err,
|
format_error_message (err, tmp_name, _("Failed to create file '%s': %s"));
|
||||||
G_FILE_ERROR,
|
|
||||||
g_file_error_from_errno (save_errno),
|
|
||||||
_("Failed to create file '%s': %s"),
|
|
||||||
display_name, g_strerror (save_errno));
|
|
||||||
|
|
||||||
goto out;
|
goto out;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -1042,17 +1053,10 @@ write_to_temp_file (const gchar *contents,
|
|||||||
file = fdopen (fd, "wb");
|
file = fdopen (fd, "wb");
|
||||||
if (!file)
|
if (!file)
|
||||||
{
|
{
|
||||||
save_errno = errno;
|
format_error_message (err, tmp_name, _("Failed to open file '%s' for writing: fdopen() failed: %s"));
|
||||||
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));
|
|
||||||
|
|
||||||
close (fd);
|
close (fd);
|
||||||
g_unlink (tmp_name);
|
g_unlink (tmp_name);
|
||||||
|
|
||||||
goto out;
|
goto out;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -1066,44 +1070,27 @@ write_to_temp_file (const gchar *contents,
|
|||||||
*/
|
*/
|
||||||
(void) posix_fallocate (fd, 0, length);
|
(void) posix_fallocate (fd, 0, length);
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
errno = 0;
|
errno = 0;
|
||||||
|
|
||||||
n_written = fwrite (contents, 1, length, file);
|
n_written = fwrite (contents, 1, length, file);
|
||||||
|
|
||||||
if (n_written < length)
|
if (n_written < length)
|
||||||
{
|
{
|
||||||
save_errno = errno;
|
format_error_message (err, tmp_name, _("Failed to write file '%s': fwrite() failed: %s"));
|
||||||
|
fclose (file);
|
||||||
g_set_error (err,
|
g_unlink (tmp_name);
|
||||||
G_FILE_ERROR,
|
goto out;
|
||||||
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;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
errno = 0;
|
errno = 0;
|
||||||
if (fflush (file) != 0)
|
if (fflush (file) != 0)
|
||||||
{
|
{
|
||||||
save_errno = errno;
|
format_error_message (err, tmp_name, _("Failed to write file '%s': fflush() failed: %s"));
|
||||||
|
|
||||||
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));
|
|
||||||
|
|
||||||
fclose (file);
|
fclose (file);
|
||||||
g_unlink (tmp_name);
|
g_unlink (tmp_name);
|
||||||
|
|
||||||
goto out;
|
goto out;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -1120,7 +1107,7 @@ write_to_temp_file (const gchar *contents,
|
|||||||
goto no_fsync;
|
goto no_fsync;
|
||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
#ifdef HAVE_FSYNC
|
#ifdef HAVE_FSYNC
|
||||||
{
|
{
|
||||||
struct stat statbuf;
|
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
|
* 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.)
|
* guarantee the data is written to the disk before the metadata.)
|
||||||
*/
|
*/
|
||||||
if (g_lstat (dest_file, &statbuf) == 0 &&
|
if (g_lstat (dest_file, &statbuf) == 0 && statbuf.st_size > 0 && fsync (fileno (file)) != 0)
|
||||||
statbuf.st_size > 0 &&
|
|
||||||
fsync (fileno (file)) != 0)
|
|
||||||
{
|
{
|
||||||
save_errno = errno;
|
format_error_message (err, tmp_name, _("Failed to write file '%s': fsync() failed: %s"));
|
||||||
|
|
||||||
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));
|
|
||||||
|
|
||||||
fclose (file);
|
fclose (file);
|
||||||
g_unlink (tmp_name);
|
g_unlink (tmp_name);
|
||||||
|
|
||||||
goto out;
|
goto out;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
@ -1156,30 +1133,21 @@ write_to_temp_file (const gchar *contents,
|
|||||||
#ifdef BTRFS_SUPER_MAGIC
|
#ifdef BTRFS_SUPER_MAGIC
|
||||||
no_fsync:
|
no_fsync:
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
errno = 0;
|
errno = 0;
|
||||||
if (fclose (file) == EOF)
|
if (fclose (file) == EOF)
|
||||||
{
|
{
|
||||||
save_errno = errno;
|
format_error_message (err, tmp_name, _("Failed to close file '%s': fclose() failed: %s"));
|
||||||
|
|
||||||
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));
|
|
||||||
|
|
||||||
g_unlink (tmp_name);
|
g_unlink (tmp_name);
|
||||||
|
|
||||||
goto out;
|
goto out;
|
||||||
}
|
}
|
||||||
|
|
||||||
retval = g_strdup (tmp_name);
|
retval = g_strdup (tmp_name);
|
||||||
|
|
||||||
out:
|
out:
|
||||||
g_free (tmp_name);
|
g_free (tmp_name);
|
||||||
g_free (display_name);
|
|
||||||
|
|
||||||
return retval;
|
return retval;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user