diff options
author | Thomas Haller <thaller@redhat.com> | 2019-08-01 10:59:36 +0200 |
---|---|---|
committer | Thomas Haller <thaller@redhat.com> | 2019-08-08 10:53:03 +0200 |
commit | 1bad35061fb3d0807048601b404619ad816be28b (patch) | |
tree | 112b58d4a4199f328dab4ae02d7309913a3f059c /shared | |
parent | 041a95229722c57a21492f69d3fbb9805bfe05e6 (diff) | |
download | NetworkManager-1bad35061fb3d0807048601b404619ad816be28b.tar.gz |
shared: let nm_utils_file_set_contents() return a errno error code
nm_utils_file_set_contents() is a re-implementation of g_file_set_contents(),
as such it returned merely a boolean success value.
It's sometimes interesting to get the native error code. Let the function
deviate from glib's original g_file_set_contents() and return the error code
(as negative value) instead.
This requires all callers to change. Also, it's potentially a dangerous
change, as this is easy to miss.
Note that nm_utils_file_get_contents() also returns an errno, and
already deviates from g_file_get_contents() in the same way. This patch
resolves at least the inconsistency with nm_utils_file_get_contents().
Diffstat (limited to 'shared')
-rw-r--r-- | shared/nm-glib-aux/nm-io-utils.c | 31 | ||||
-rw-r--r-- | shared/nm-glib-aux/nm-io-utils.h | 10 |
2 files changed, 19 insertions, 22 deletions
diff --git a/shared/nm-glib-aux/nm-io-utils.c b/shared/nm-glib-aux/nm-io-utils.c index 23133ec568..c62fcffadb 100644 --- a/shared/nm-glib-aux/nm-io-utils.c +++ b/shared/nm-glib-aux/nm-io-utils.c @@ -335,7 +335,7 @@ nm_utils_file_get_contents (int dirfd, * Copied from GLib's g_file_set_contents() et al., but allows * specifying a mode for the new file. */ -gboolean +int nm_utils_file_set_contents (const char *filename, const char *contents, gssize length, @@ -349,10 +349,10 @@ nm_utils_file_set_contents (const char *filename, int fd; char bstrerr[NM_STRERROR_BUFSIZE]; - g_return_val_if_fail (filename, FALSE); - g_return_val_if_fail (contents || !length, FALSE); - g_return_val_if_fail (!error || !*error, FALSE); - g_return_val_if_fail (length >= -1, FALSE); + g_return_val_if_fail (filename, -EINVAL); + g_return_val_if_fail (contents || !length, -EINVAL); + g_return_val_if_fail (!error || !*error, -EINVAL); + g_return_val_if_fail (length >= -1, -EINVAL); if (length == -1) length = strlen (contents); @@ -360,33 +360,32 @@ nm_utils_file_set_contents (const char *filename, tmp_name = g_strdup_printf ("%s.XXXXXX", filename); fd = g_mkstemp_full (tmp_name, O_RDWR | O_CLOEXEC, mode); if (fd < 0) { - errsv = errno; + errsv = NM_ERRNO_NATIVE (errno); g_set_error (error, G_FILE_ERROR, g_file_error_from_errno (errsv), "failed to create file %s: %s", tmp_name, nm_strerror_native_r (errsv, bstrerr, sizeof (bstrerr))); - return FALSE; + return -errsv; } while (length > 0) { s = write (fd, contents, length); if (s < 0) { - errsv = errno; + errsv = NM_ERRNO_NATIVE (errno); if (errsv == EINTR) continue; nm_close (fd); unlink (tmp_name); - g_set_error (error, G_FILE_ERROR, g_file_error_from_errno (errsv), "failed to write to file %s: %s", tmp_name, nm_strerror_native_r (errsv, bstrerr, sizeof (bstrerr))); - return FALSE; + return -errsv; } g_assert (s <= length); @@ -404,25 +403,23 @@ nm_utils_file_set_contents (const char *filename, if ( lstat (filename, &statbuf) == 0 && statbuf.st_size > 0) { if (fsync (fd) != 0) { - errsv = errno; - + errsv = NM_ERRNO_NATIVE (errno); nm_close (fd); unlink (tmp_name); - g_set_error (error, G_FILE_ERROR, g_file_error_from_errno (errsv), "failed to fsync %s: %s", tmp_name, nm_strerror_native_r (errsv, bstrerr, sizeof (bstrerr))); - return FALSE; + return -errsv; } } nm_close (fd); if (rename (tmp_name, filename)) { - errsv = errno; + errsv = NM_ERRNO_NATIVE (errno); unlink (tmp_name); g_set_error (error, G_FILE_ERROR, @@ -431,10 +428,10 @@ nm_utils_file_set_contents (const char *filename, tmp_name, filename, nm_strerror_native_r (errsv, bstrerr, sizeof (bstrerr))); - return FALSE; + return -errsv; } - return TRUE; + return 0; } /** diff --git a/shared/nm-glib-aux/nm-io-utils.h b/shared/nm-glib-aux/nm-io-utils.h index 121fc481d9..e883d77ca1 100644 --- a/shared/nm-glib-aux/nm-io-utils.h +++ b/shared/nm-glib-aux/nm-io-utils.h @@ -53,11 +53,11 @@ int nm_utils_file_get_contents (int dirfd, gsize *length, GError **error); -gboolean nm_utils_file_set_contents (const char *filename, - const char *contents, - gssize length, - mode_t mode, - GError **error); +int nm_utils_file_set_contents (const char *filename, + const char *contents, + gssize length, + mode_t mode, + GError **error); struct stat; |