diff options
author | Lubomir Rintel <lkundrak@v3.sk> | 2017-06-02 13:38:31 +0200 |
---|---|---|
committer | Lubomir Rintel <lkundrak@v3.sk> | 2017-06-02 20:53:24 +0200 |
commit | 10c6a82c1c91531da367cf7f8ef84b0f320a7f94 (patch) | |
tree | f9a6ad95d129bcec4838dfed59139fbf40a9836d | |
parent | 1d35fd01031aeeaf54edb55771e53d8c5365ccbd (diff) | |
download | NetworkManager-10c6a82c1c91531da367cf7f8ef84b0f320a7f94.tar.gz |
utils: split out nm_utils_fd_set_contents()
-rw-r--r-- | src/nm-core-utils.c | 66 | ||||
-rw-r--r-- | src/nm-core-utils.h | 5 |
2 files changed, 47 insertions, 24 deletions
diff --git a/src/nm-core-utils.c b/src/nm-core-utils.c index 4da1fb2c68..cfd623aadf 100644 --- a/src/nm-core-utils.c +++ b/src/nm-core-utils.c @@ -4193,6 +4193,43 @@ nm_utils_get_reverse_dns_domains_ip6 (const struct in6_addr *ip, guint8 plen, GP #undef N_SHIFT } +gboolean +nm_utils_fd_set_contents (int fd, + const gchar *contents, + gssize length, + GError **error) +{ + int errsv; + gssize s; + + g_return_val_if_fail (fd, 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); + + while (length > 0) { + s = write (fd, contents, length); + if (s < 0) { + errsv = errno; + if (errsv == EINTR) + continue; + + g_set_error_literal (error, + G_FILE_ERROR, + g_file_error_from_errno (errsv), + g_strerror (errsv)); + return FALSE; + } + + g_assert (s <= length); + + contents += s; + length -= s; + } + + return TRUE; +} + /** * Copied from GLib's g_file_set_contents() et al., but allows * specifying a mode for the new file. @@ -4207,7 +4244,6 @@ nm_utils_file_set_contents (const gchar *filename, gs_free char *tmp_name = NULL; struct stat statbuf; int errsv; - gssize s; int fd; g_return_val_if_fail (filename, FALSE); @@ -4231,29 +4267,11 @@ nm_utils_file_set_contents (const gchar *filename, return FALSE; } - while (length > 0) { - s = write (fd, contents, length); - if (s < 0) { - errsv = errno; - if (errsv == EINTR) - continue; - - 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, - g_strerror (errsv)); - return FALSE; - } - - g_assert (s <= length); - - contents += s; - length -= s; + if (!nm_utils_fd_set_contents (fd, contents, length, error)) { + g_prefix_error (error, "failed to write to file %s: ", tmp_name); + close (fd); + unlink (tmp_name); + return FALSE; } /* If the final destination exists and is > 0 bytes, we want to sync the diff --git a/src/nm-core-utils.h b/src/nm-core-utils.h index 51a0c9cff7..a2ff5e7d32 100644 --- a/src/nm-core-utils.h +++ b/src/nm-core-utils.h @@ -289,6 +289,11 @@ int nm_utils_file_get_contents (int dirfd, gsize *length, GError **error); +gboolean nm_utils_fd_set_contents (int fd, + const gchar *contents, + gssize length, + GError **error); + gboolean nm_utils_file_set_contents (const gchar *filename, const gchar *contents, gssize length, |