From 03ca87586f79329b098cd59e6dd696373babb268 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Timm=20B=C3=A4der?= Date: Thu, 31 Dec 2020 11:28:03 +0100 Subject: [PATCH] fileutils: Avoid calling set_file_error with NULL GError** If no pointer to a GError* has been passed to public API, there's not need to look at translations via gettext or format an error message that g_set_error_literal will entirely ignore in the end. --- glib/gfileutils.c | 101 ++++++++++++++++++++++++++-------------------- 1 file changed, 57 insertions(+), 44 deletions(-) diff --git a/glib/gfileutils.c b/glib/gfileutils.c index 16adc3ff7..a4e90265e 100644 --- a/glib/gfileutils.c +++ b/glib/gfileutils.c @@ -897,10 +897,12 @@ get_contents_posix (const gchar *filename, if (fd < 0) { int saved_errno = errno; - set_file_error (error, - filename, - _("Failed to open file “%s”: %s"), - saved_errno); + + if (error) + set_file_error (error, + filename, + _("Failed to open file “%s”: %s"), + saved_errno); return FALSE; } @@ -909,10 +911,11 @@ get_contents_posix (const gchar *filename, if (fstat (fd, &stat_buf) < 0) { int saved_errno = errno; - set_file_error (error, - filename, - _("Failed to get attributes of file “%s”: fstat() failed: %s"), - saved_errno); + if (error) + set_file_error (error, + filename, + _("Failed to get attributes of file “%s”: fstat() failed: %s"), + saved_errno); close (fd); return FALSE; @@ -939,10 +942,11 @@ get_contents_posix (const gchar *filename, if (f == NULL) { int saved_errno = errno; - set_file_error (error, - filename, - _("Failed to open file “%s”: fdopen() failed: %s"), - saved_errno); + if (error) + set_file_error (error, + filename, + _("Failed to open file “%s”: fdopen() failed: %s"), + saved_errno); return FALSE; } @@ -969,10 +973,11 @@ get_contents_win32 (const gchar *filename, if (f == NULL) { int saved_errno = errno; - set_file_error (error, - filename, - _("Failed to open file “%s”: %s"), - saved_errno); + if (error) + set_file_error (error, + filename, + _("Failed to open file “%s”: %s"), + saved_errno); return FALSE; } @@ -1165,9 +1170,10 @@ write_to_file (const gchar *contents, if (saved_errno == EINTR) continue; - set_file_error (err, - dest_file, _("Failed to write file “%s”: write() failed: %s"), - saved_errno); + if (err) + set_file_error (err, + dest_file, _("Failed to write file “%s”: write() failed: %s"), + saved_errno); close (fd); return FALSE; @@ -1185,9 +1191,10 @@ write_to_file (const gchar *contents, if (do_fsync && g_fsync (fd) != 0) { int saved_errno = errno; - set_file_error (err, - dest_file, _("Failed to write file “%s”: fsync() failed: %s"), - saved_errno); + if (err) + set_file_error (err, + dest_file, _("Failed to write file “%s”: fsync() failed: %s"), + saved_errno); close (fd); return FALSE; @@ -1353,9 +1360,10 @@ g_file_set_contents_full (const gchar *filename, if (fd == -1) { int saved_errno = errno; - set_file_error (error, - tmp_filename, _("Failed to create file “%s”: %s"), - saved_errno); + if (error) + set_file_error (error, + tmp_filename, _("Failed to create file “%s”: %s"), + saved_errno); retval = FALSE; goto consistent_out; } @@ -1396,10 +1404,11 @@ g_file_set_contents_full (const gchar *filename, if (g_unlink (filename) == -1) { int saved_errno = errno; - set_file_error (error, - filename, - _("Existing file “%s” could not be removed: g_unlink() failed: %s"), - saved_errno); + if (error) + set_file_error (error, + filename, + _("Existing file “%s” could not be removed: g_unlink() failed: %s"), + saved_errno); g_unlink (tmp_filename); retval = FALSE; goto consistent_out; @@ -1462,9 +1471,10 @@ consistent_out: mode, error); #endif /* O_NOFOLLOW */ - set_file_error (error, - filename, _("Failed to open file “%s”: %s"), - saved_errno); + if (error) + set_file_error (error, + filename, _("Failed to open file “%s”: %s"), + saved_errno); return FALSE; } @@ -1764,10 +1774,11 @@ g_get_tmp_name (const gchar *tmpl, if (retval == -1) { int saved_errno = errno; - set_file_error (error, - fulltemplate, - _("Failed to create file “%s”: %s"), - saved_errno); + if (error) + set_file_error (error, + fulltemplate, + _("Failed to create file “%s”: %s"), + saved_errno); g_free (fulltemplate); return -1; } @@ -2301,10 +2312,11 @@ g_file_read_link (const gchar *filename, if (read_size < 0) { int saved_errno = errno; - set_file_error (error, - filename, - _("Failed to read the symbolic link “%s”: %s"), - saved_errno); + if (error) + set_file_error (error, + filename, + _("Failed to read the symbolic link “%s”: %s"), + saved_errno); g_free (buffer); return NULL; } @@ -2329,10 +2341,11 @@ g_file_read_link (const gchar *filename, if (read_size < 0) { int saved_errno = errno; - set_file_error (error, - filename, - _("Failed to read the symbolic link “%s”: %s"), - saved_errno); + if (error) + set_file_error (error, + filename, + _("Failed to read the symbolic link “%s”: %s"), + saved_errno); return NULL; } else if (read_size == 0)