diff options
author | Philip Withnall <withnall@endlessm.com> | 2020-05-27 12:03:59 +0100 |
---|---|---|
committer | Philip Withnall <withnall@endlessm.com> | 2020-07-26 21:37:46 +0100 |
commit | 00c17de72eeeb215f163b2775208ea84dfb307cb (patch) | |
tree | c8c49b78425228ec73608a6dab050fba4858c65f /glib/gfileutils.c | |
parent | 554107c23cd94577365e933c0725567fe2fbdb2b (diff) | |
download | glib-00c17de72eeeb215f163b2775208ea84dfb307cb.tar.gz |
gfileutils: Split out write_to_file() helper function
This introduces no functional changes.
Signed-off-by: Philip Withnall <withnall@endlessm.com>
Helps: #1302
Diffstat (limited to 'glib/gfileutils.c')
-rw-r--r-- | glib/gfileutils.c | 91 |
1 files changed, 53 insertions, 38 deletions
diff --git a/glib/gfileutils.c b/glib/gfileutils.c index 1baf58dbb..f192ef03b 100644 --- a/glib/gfileutils.c +++ b/glib/gfileutils.c @@ -1050,32 +1050,15 @@ rename_file (const char *old_name, return TRUE; } -static gchar * -write_to_temp_file (const gchar *contents, - gssize length, - const gchar *dest_file, - GError **err) +/* closes @fd once it’s finished (on success or error) */ +static gboolean +write_to_file (const gchar *contents, + gssize length, + int fd, + const gchar *dest_file, + const gchar *test_file, + GError **err) { - gchar *tmp_name; - gchar *retval; - gint fd; - - retval = NULL; - - tmp_name = g_strdup_printf ("%s.XXXXXX", dest_file); - - errno = 0; - fd = g_mkstemp_full (tmp_name, O_RDWR | O_BINARY, 0666); - - if (fd == -1) - { - int saved_errno = errno; - set_file_error (err, - tmp_name, _("Failed to create file “%s”: %s"), - saved_errno); - goto out; - } - #ifdef HAVE_FALLOCATE if (length > 0) { @@ -1098,12 +1081,11 @@ write_to_temp_file (const gchar *contents, continue; set_file_error (err, - tmp_name, _("Failed to write file “%s”: write() failed: %s"), + dest_file, _("Failed to write file “%s”: write() failed: %s"), saved_errno); close (fd); - g_unlink (tmp_name); - goto out; + return FALSE; } g_assert (s <= length); @@ -1141,12 +1123,11 @@ write_to_temp_file (const gchar *contents, { int saved_errno = errno; set_file_error (err, - tmp_name, _("Failed to write file “%s”: fsync() failed: %s"), + dest_file, _("Failed to write file “%s”: fsync() failed: %s"), saved_errno); close (fd); - g_unlink (tmp_name); - goto out; + return FALSE; } } #endif @@ -1157,18 +1138,52 @@ write_to_temp_file (const gchar *contents, errno = 0; if (!g_close (fd, err)) - { - g_unlink (tmp_name); + return FALSE; - goto out; + return TRUE; +} + +static inline int +steal_fd (int *fd_ptr) +{ + int fd = *fd_ptr; + *fd_ptr = -1; + return fd; +} + +static gchar * +write_to_temp_file (const gchar *contents, + gssize length, + const gchar *dest_file, + GError **err) +{ + gchar *tmp_name = NULL; + int fd; + + tmp_name = g_strdup_printf ("%s.XXXXXX", dest_file); + + errno = 0; + fd = g_mkstemp_full (tmp_name, O_RDWR | O_BINARY, 0666); + + if (fd == -1) + { + int saved_errno = errno; + set_file_error (err, + tmp_name, _("Failed to create file “%s”: %s"), + saved_errno); + g_free (tmp_name); + return NULL; } - retval = g_strdup (tmp_name); + if (!write_to_file (contents, length, steal_fd (&fd), tmp_name, dest_file, err)) + { + g_unlink (tmp_name); + g_free (tmp_name); - out: - g_free (tmp_name); + return NULL; + } - return retval; + return g_steal_pointer (&tmp_name); } /** |