shared,all: return boolean success from nm_utils_file_get_contents()
... and nm_utils_fd_get_contents() and nm_utils_file_set_contents(). Don't mix negative errno return value with a GError output. Instead, return a boolean result indicating success or failure. Also, optionally - output GError - set out_errsv to the positive errno (or 0 on success) Obviously, the return value and the output arguments (contents, length, out_errsv, error) must all agree in their success/failure result. That means, you may check any of the return value, out_errsv, error, and contents to reliably detect failure or success. Also note that out_errsv gives the positive(!) errno. But you probably shouldn't care about the distinction and use nm_errno_native() either way to normalize the value.
This commit is contained in:
@@ -390,13 +390,14 @@ nm_vpn_wireguard_import (const char *filename,
|
||||
return FALSE;
|
||||
}
|
||||
|
||||
if (nm_utils_file_get_contents (-1,
|
||||
filename,
|
||||
10*1024*1024,
|
||||
NM_UTILS_FILE_GET_CONTENTS_FLAG_SECRET,
|
||||
&file_content.str,
|
||||
&file_content.len,
|
||||
error) < 0)
|
||||
if (!nm_utils_file_get_contents (-1,
|
||||
filename,
|
||||
10*1024*1024,
|
||||
NM_UTILS_FILE_GET_CONTENTS_FLAG_SECRET,
|
||||
&file_content.str,
|
||||
&file_content.len,
|
||||
NULL,
|
||||
error))
|
||||
return NULL;
|
||||
|
||||
/* We interpret the file like `wg-quick up` and `wg setconf` do.
|
||||
|
@@ -444,7 +444,8 @@ file_read_contents (const char *filename,
|
||||
NM_UTILS_FILE_GET_CONTENTS_FLAG_SECRET,
|
||||
&out_contents->str,
|
||||
&out_contents->len,
|
||||
error) >= 0;
|
||||
NULL,
|
||||
error);
|
||||
}
|
||||
|
||||
GBytes *
|
||||
|
@@ -32,9 +32,9 @@
|
||||
|
||||
/*****************************************************************************/
|
||||
|
||||
_nm_printf (3, 4)
|
||||
_nm_printf (4, 5)
|
||||
static int
|
||||
_get_contents_error (GError **error, int errsv, const char *format, ...)
|
||||
_get_contents_error (GError **error, int errsv, int *out_errsv, const char *format, ...)
|
||||
{
|
||||
nm_assert (NM_ERRNO_NATIVE (errsv));
|
||||
|
||||
@@ -53,13 +53,17 @@ _get_contents_error (GError **error, int errsv, const char *format, ...)
|
||||
msg,
|
||||
nm_strerror_native_r (errsv, bstrerr, sizeof (bstrerr)));
|
||||
}
|
||||
return -errsv;
|
||||
|
||||
nm_assert (errsv > 0);
|
||||
NM_SET_OUT (out_errsv, errsv);
|
||||
|
||||
return FALSE;
|
||||
}
|
||||
#define _get_contents_error_errno(error, ...) \
|
||||
#define _get_contents_error_errno(error, out_errsv, ...) \
|
||||
({ \
|
||||
int _errsv = (errno); \
|
||||
\
|
||||
_get_contents_error (error, _errsv, __VA_ARGS__); \
|
||||
_get_contents_error (error, _errsv, out_errsv, __VA_ARGS__); \
|
||||
})
|
||||
|
||||
static char *
|
||||
@@ -110,21 +114,25 @@ _mem_realloc (char *old, gboolean do_bzero_mem, gsize cur_len, gsize new_len)
|
||||
* the NUL byte. That is, it reads only files up to a length of
|
||||
* @max_length - 1 bytes.
|
||||
* @length: optional output argument of the read file size.
|
||||
* @out_errsv: (allow-none) (out): on error, a positive errno. or zero.
|
||||
* @error:
|
||||
*
|
||||
*
|
||||
* A reimplementation of g_file_get_contents() with a few differences:
|
||||
* - accepts an open fd, instead of a path name. This allows you to
|
||||
* use openat().
|
||||
* - limits the maximum filesize to max_length.
|
||||
*
|
||||
* Returns: a negative error code on failure.
|
||||
* Returns: TRUE on success.
|
||||
*/
|
||||
int
|
||||
gboolean
|
||||
nm_utils_fd_get_contents (int fd,
|
||||
gboolean close_fd,
|
||||
gsize max_length,
|
||||
NMUtilsFileGetContentsFlags flags,
|
||||
char **contents,
|
||||
gsize *length,
|
||||
int *out_errsv,
|
||||
GError **error)
|
||||
{
|
||||
nm_auto_close int fd_keeper = close_fd ? fd : -1;
|
||||
@@ -133,12 +141,14 @@ nm_utils_fd_get_contents (int fd,
|
||||
const bool do_bzero_mem = NM_FLAGS_HAS (flags, NM_UTILS_FILE_GET_CONTENTS_FLAG_SECRET);
|
||||
int errsv;
|
||||
|
||||
g_return_val_if_fail (fd >= 0, -EINVAL);
|
||||
g_return_val_if_fail (contents, -EINVAL);
|
||||
g_return_val_if_fail (!error || !*error, -EINVAL);
|
||||
g_return_val_if_fail (fd >= 0, FALSE);
|
||||
g_return_val_if_fail (contents && !*contents, FALSE);
|
||||
g_return_val_if_fail (!error || !*error, FALSE);
|
||||
|
||||
NM_SET_OUT (length, 0);
|
||||
|
||||
if (fstat (fd, &stat_buf) < 0)
|
||||
return _get_contents_error_errno (error, "failure during fstat");
|
||||
return _get_contents_error_errno (error, out_errsv, "failure during fstat");
|
||||
|
||||
if (!max_length) {
|
||||
/* default to a very large size, but not extreme */
|
||||
@@ -151,23 +161,23 @@ nm_utils_fd_get_contents (int fd,
|
||||
ssize_t n_read;
|
||||
|
||||
if (n_stat > max_length - 1)
|
||||
return _get_contents_error (error, EMSGSIZE, "file too large (%zu+1 bytes with maximum %zu bytes)", n_stat, max_length);
|
||||
return _get_contents_error (error, EMSGSIZE, out_errsv, "file too large (%zu+1 bytes with maximum %zu bytes)", n_stat, max_length);
|
||||
|
||||
str = g_try_malloc (n_stat + 1);
|
||||
if (!str)
|
||||
return _get_contents_error (error, ENOMEM, "failure to allocate buffer of %zu+1 bytes", n_stat);
|
||||
return _get_contents_error (error, ENOMEM, out_errsv, "failure to allocate buffer of %zu+1 bytes", n_stat);
|
||||
|
||||
n_read = nm_utils_fd_read_loop (fd, str, n_stat, TRUE);
|
||||
if (n_read < 0) {
|
||||
if (do_bzero_mem)
|
||||
nm_explicit_bzero (str, n_stat);
|
||||
return _get_contents_error (error, -n_read, "error reading %zu bytes from file descriptor", n_stat);
|
||||
return _get_contents_error (error, -n_read, out_errsv, "error reading %zu bytes from file descriptor", n_stat);
|
||||
}
|
||||
str[n_read] = '\0';
|
||||
|
||||
if (n_read < n_stat) {
|
||||
if (!(str = _mem_realloc (str, do_bzero_mem, n_stat + 1, n_read + 1)))
|
||||
return _get_contents_error (error, ENOMEM, "failure to reallocate buffer with %zu bytes", n_read + 1);
|
||||
return _get_contents_error (error, ENOMEM, out_errsv, "failure to reallocate buffer with %zu bytes", n_read + 1);
|
||||
}
|
||||
NM_SET_OUT (length, n_read);
|
||||
} else {
|
||||
@@ -181,13 +191,13 @@ nm_utils_fd_get_contents (int fd,
|
||||
else {
|
||||
fd2 = fcntl (fd, F_DUPFD_CLOEXEC, 0);
|
||||
if (fd2 < 0)
|
||||
return _get_contents_error_errno (error, "error during dup");
|
||||
return _get_contents_error_errno (error, out_errsv, "error during dup");
|
||||
}
|
||||
|
||||
if (!(f = fdopen (fd2, "r"))) {
|
||||
errsv = errno;
|
||||
nm_close (fd2);
|
||||
return _get_contents_error (error, errsv, "failure during fdopen");
|
||||
return _get_contents_error (error, errsv, out_errsv, "failure during fdopen");
|
||||
}
|
||||
|
||||
n_have = 0;
|
||||
@@ -201,14 +211,14 @@ nm_utils_fd_get_contents (int fd,
|
||||
if (ferror (f)) {
|
||||
if (do_bzero_mem)
|
||||
nm_explicit_bzero (buf, sizeof (buf));
|
||||
return _get_contents_error (error, errsv, "error during fread");
|
||||
return _get_contents_error (error, errsv, out_errsv, "error during fread");
|
||||
}
|
||||
|
||||
if ( n_have > G_MAXSIZE - 1 - n_read
|
||||
|| n_have + n_read + 1 > max_length) {
|
||||
if (do_bzero_mem)
|
||||
nm_explicit_bzero (buf, sizeof (buf));
|
||||
return _get_contents_error (error, EMSGSIZE, "file stream too large (%zu+1 bytes with maximum %zu bytes)",
|
||||
return _get_contents_error (error, EMSGSIZE, out_errsv, "file stream too large (%zu+1 bytes with maximum %zu bytes)",
|
||||
(n_have > G_MAXSIZE - 1 - n_read) ? G_MAXSIZE : n_have + n_read,
|
||||
max_length);
|
||||
}
|
||||
@@ -230,7 +240,7 @@ nm_utils_fd_get_contents (int fd,
|
||||
if (!(str = _mem_realloc (str, do_bzero_mem, old_n_alloc, n_alloc))) {
|
||||
if (do_bzero_mem)
|
||||
nm_explicit_bzero (buf, sizeof (buf));
|
||||
return _get_contents_error (error, ENOMEM, "failure to allocate buffer of %zu bytes", n_alloc);
|
||||
return _get_contents_error (error, ENOMEM, out_errsv, "failure to allocate buffer of %zu bytes", n_alloc);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -247,7 +257,7 @@ nm_utils_fd_get_contents (int fd,
|
||||
str[n_have] = '\0';
|
||||
if (n_have + 1 < n_alloc) {
|
||||
if (!(str = _mem_realloc (str, do_bzero_mem, n_alloc, n_have + 1)))
|
||||
return _get_contents_error (error, ENOMEM, "failure to truncate buffer to %zu bytes", n_have + 1);
|
||||
return _get_contents_error (error, ENOMEM, out_errsv, "failure to truncate buffer to %zu bytes", n_have + 1);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -255,7 +265,8 @@ nm_utils_fd_get_contents (int fd,
|
||||
}
|
||||
|
||||
*contents = g_steal_pointer (&str);
|
||||
return 0;
|
||||
NM_SET_OUT (out_errsv, 0);
|
||||
return TRUE;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -270,54 +281,49 @@ nm_utils_fd_get_contents (int fd,
|
||||
* the NUL byte. That is, it reads only files up to a length of
|
||||
* @max_length - 1 bytes.
|
||||
* @length: optional output argument of the read file size.
|
||||
* @out_errsv: (allow-none) (out): on error, a positive errno. or zero.
|
||||
* @error:
|
||||
*
|
||||
* A reimplementation of g_file_get_contents() with a few differences:
|
||||
* - accepts an @dirfd to open @filename relative to that path via openat().
|
||||
* - limits the maximum filesize to max_length.
|
||||
* - uses O_CLOEXEC on internal file descriptor
|
||||
* - optionally returns the native errno on failure.
|
||||
*
|
||||
* Returns: a negative error code on failure.
|
||||
* Returns: TRUE on success.
|
||||
*/
|
||||
int
|
||||
gboolean
|
||||
nm_utils_file_get_contents (int dirfd,
|
||||
const char *filename,
|
||||
gsize max_length,
|
||||
NMUtilsFileGetContentsFlags flags,
|
||||
char **contents,
|
||||
gsize *length,
|
||||
int *out_errsv,
|
||||
GError **error)
|
||||
{
|
||||
int fd;
|
||||
int errsv;
|
||||
char bstrerr[NM_STRERROR_BUFSIZE];
|
||||
|
||||
g_return_val_if_fail (filename && filename[0], -EINVAL);
|
||||
g_return_val_if_fail (filename && filename[0], FALSE);
|
||||
g_return_val_if_fail (contents && !*contents, FALSE);
|
||||
|
||||
NM_SET_OUT (length, 0);
|
||||
|
||||
if (dirfd >= 0) {
|
||||
fd = openat (dirfd, filename, O_RDONLY | O_CLOEXEC);
|
||||
if (fd < 0) {
|
||||
errsv = errno;
|
||||
|
||||
g_set_error (error,
|
||||
G_FILE_ERROR,
|
||||
g_file_error_from_errno (errsv),
|
||||
"Failed to open file \"%s\" with openat: %s",
|
||||
filename,
|
||||
nm_strerror_native_r (errsv, bstrerr, sizeof (bstrerr)));
|
||||
return -NM_ERRNO_NATIVE (errsv);
|
||||
return _get_contents_error_errno (error,
|
||||
out_errsv,
|
||||
"Failed to open file \"%s\" with openat",
|
||||
filename);
|
||||
}
|
||||
} else {
|
||||
fd = open (filename, O_RDONLY | O_CLOEXEC);
|
||||
if (fd < 0) {
|
||||
errsv = errno;
|
||||
|
||||
g_set_error (error,
|
||||
G_FILE_ERROR,
|
||||
g_file_error_from_errno (errsv),
|
||||
"Failed to open file \"%s\": %s",
|
||||
filename,
|
||||
nm_strerror_native_r (errsv, bstrerr, sizeof (bstrerr)));
|
||||
return -NM_ERRNO_NATIVE (errsv);
|
||||
return _get_contents_error_errno (error,
|
||||
out_errsv,
|
||||
"Failed to open file \"%s\"",
|
||||
filename);
|
||||
}
|
||||
}
|
||||
return nm_utils_fd_get_contents (fd,
|
||||
@@ -326,6 +332,7 @@ nm_utils_file_get_contents (int dirfd,
|
||||
flags,
|
||||
contents,
|
||||
length,
|
||||
out_errsv,
|
||||
error);
|
||||
}
|
||||
|
||||
@@ -335,11 +342,12 @@ 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.
|
||||
*/
|
||||
int
|
||||
gboolean
|
||||
nm_utils_file_set_contents (const char *filename,
|
||||
const char *contents,
|
||||
gssize length,
|
||||
mode_t mode,
|
||||
int *out_errsv,
|
||||
GError **error)
|
||||
{
|
||||
gs_free char *tmp_name = NULL;
|
||||
@@ -347,12 +355,11 @@ nm_utils_file_set_contents (const char *filename,
|
||||
int errsv;
|
||||
gssize s;
|
||||
int fd;
|
||||
char bstrerr[NM_STRERROR_BUFSIZE];
|
||||
|
||||
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);
|
||||
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);
|
||||
|
||||
if (length == -1)
|
||||
length = strlen (contents);
|
||||
@@ -360,14 +367,10 @@ 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 = 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 -errsv;
|
||||
return _get_contents_error_errno (error,
|
||||
out_errsv,
|
||||
"failed to create file %s",
|
||||
tmp_name);
|
||||
}
|
||||
|
||||
while (length > 0) {
|
||||
@@ -379,13 +382,11 @@ nm_utils_file_set_contents (const char *filename,
|
||||
|
||||
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 -errsv;
|
||||
return _get_contents_error (error,
|
||||
errsv,
|
||||
out_errsv,
|
||||
"failed to write to file %s",
|
||||
tmp_name);
|
||||
}
|
||||
|
||||
g_assert (s <= length);
|
||||
@@ -406,13 +407,11 @@ nm_utils_file_set_contents (const char *filename,
|
||||
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 -errsv;
|
||||
return _get_contents_error (error,
|
||||
errsv,
|
||||
out_errsv,
|
||||
"failed to fsync %s",
|
||||
tmp_name);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -421,17 +420,15 @@ nm_utils_file_set_contents (const char *filename,
|
||||
if (rename (tmp_name, filename)) {
|
||||
errsv = NM_ERRNO_NATIVE (errno);
|
||||
unlink (tmp_name);
|
||||
g_set_error (error,
|
||||
G_FILE_ERROR,
|
||||
g_file_error_from_errno (errsv),
|
||||
"failed to rename %s to %s: %s",
|
||||
tmp_name,
|
||||
filename,
|
||||
nm_strerror_native_r (errsv, bstrerr, sizeof (bstrerr)));
|
||||
return -errsv;
|
||||
return _get_contents_error (error,
|
||||
errsv,
|
||||
out_errsv,
|
||||
"failed rename %s to %s",
|
||||
tmp_name,
|
||||
filename);
|
||||
}
|
||||
|
||||
return 0;
|
||||
return TRUE;
|
||||
}
|
||||
|
||||
/**
|
||||
|
@@ -37,27 +37,30 @@ typedef enum {
|
||||
NM_UTILS_FILE_GET_CONTENTS_FLAG_SECRET = (1 << 0),
|
||||
} NMUtilsFileGetContentsFlags;
|
||||
|
||||
int nm_utils_fd_get_contents (int fd,
|
||||
gboolean close_fd,
|
||||
gsize max_length,
|
||||
NMUtilsFileGetContentsFlags flags,
|
||||
char **contents,
|
||||
gsize *length,
|
||||
GError **error);
|
||||
gboolean nm_utils_fd_get_contents (int fd,
|
||||
gboolean close_fd,
|
||||
gsize max_length,
|
||||
NMUtilsFileGetContentsFlags flags,
|
||||
char **contents,
|
||||
gsize *length,
|
||||
int *out_errsv,
|
||||
GError **error);
|
||||
|
||||
int nm_utils_file_get_contents (int dirfd,
|
||||
const char *filename,
|
||||
gsize max_length,
|
||||
NMUtilsFileGetContentsFlags flags,
|
||||
char **contents,
|
||||
gsize *length,
|
||||
GError **error);
|
||||
gboolean nm_utils_file_get_contents (int dirfd,
|
||||
const char *filename,
|
||||
gsize max_length,
|
||||
NMUtilsFileGetContentsFlags flags,
|
||||
char **contents,
|
||||
gsize *length,
|
||||
int *out_errsv,
|
||||
GError **error);
|
||||
|
||||
int nm_utils_file_set_contents (const char *filename,
|
||||
const char *contents,
|
||||
gssize length,
|
||||
mode_t mode,
|
||||
GError **error);
|
||||
gboolean nm_utils_file_set_contents (const char *filename,
|
||||
const char *contents,
|
||||
gssize length,
|
||||
mode_t mode,
|
||||
int *out_errsv,
|
||||
GError **error);
|
||||
|
||||
struct stat;
|
||||
|
||||
|
@@ -186,7 +186,6 @@ nm_key_file_db_destroy (NMKeyFileDB *self)
|
||||
void
|
||||
nm_key_file_db_start (NMKeyFileDB *self)
|
||||
{
|
||||
int r;
|
||||
gs_free char *contents = NULL;
|
||||
gsize contents_len;
|
||||
gs_free_error GError *error = NULL;
|
||||
@@ -196,14 +195,14 @@ nm_key_file_db_start (NMKeyFileDB *self)
|
||||
|
||||
self->is_started = TRUE;
|
||||
|
||||
r = nm_utils_file_get_contents (-1,
|
||||
self->filename,
|
||||
20*1024*1024,
|
||||
NM_UTILS_FILE_GET_CONTENTS_FLAG_NONE,
|
||||
&contents,
|
||||
&contents_len,
|
||||
&error);
|
||||
if (r < 0) {
|
||||
if (!nm_utils_file_get_contents (-1,
|
||||
self->filename,
|
||||
20*1024*1024,
|
||||
NM_UTILS_FILE_GET_CONTENTS_FLAG_NONE,
|
||||
&contents,
|
||||
&contents_len,
|
||||
NULL,
|
||||
&error)) {
|
||||
_LOGD ("failed to read \"%s\": %s", self->filename, error->message);
|
||||
return;
|
||||
}
|
||||
|
@@ -63,7 +63,12 @@ output_conn (gpointer key, gpointer value, gpointer user_data)
|
||||
filename = nm_keyfile_utils_create_filename (basename, TRUE);
|
||||
full_filename = g_build_filename (connections_dir, filename, NULL);
|
||||
|
||||
if (nm_utils_file_set_contents (full_filename, data, len, 0600, &error) < 0)
|
||||
if (!nm_utils_file_set_contents (full_filename,
|
||||
data,
|
||||
len,
|
||||
0600,
|
||||
NULL,
|
||||
&error))
|
||||
goto err_out;
|
||||
} else
|
||||
g_print ("\n*** Connection '%s' ***\n\n%s", basename, data);
|
||||
|
@@ -2431,8 +2431,8 @@ again:
|
||||
* where our configured SYSCONFDIR is. Alternatively, it might be in
|
||||
* LOCALSTATEDIR /lib/dbus/machine-id.
|
||||
*/
|
||||
if ( nm_utils_file_get_contents (-1, "/etc/machine-id", 100*1024, 0, &content, NULL, NULL) >= 0
|
||||
|| nm_utils_file_get_contents (-1, LOCALSTATEDIR"/lib/dbus/machine-id", 100*1024, 0, &content, NULL, NULL) >= 0) {
|
||||
if ( nm_utils_file_get_contents (-1, "/etc/machine-id", 100*1024, 0, &content, NULL, NULL, NULL)
|
||||
|| nm_utils_file_get_contents (-1, LOCALSTATEDIR"/lib/dbus/machine-id", 100*1024, 0, &content, NULL, NULL, NULL)) {
|
||||
g_strstrip (content);
|
||||
if (nm_utils_hexstr2bin_full (content,
|
||||
FALSE,
|
||||
@@ -2615,13 +2615,14 @@ _host_id_read (guint8 **out_host_id,
|
||||
GError *error = NULL;
|
||||
gboolean success;
|
||||
|
||||
if (nm_utils_file_get_contents (-1,
|
||||
SECRET_KEY_FILE,
|
||||
10*1024,
|
||||
NM_UTILS_FILE_GET_CONTENTS_FLAG_SECRET,
|
||||
(char **) &file_content.str,
|
||||
&file_content.len,
|
||||
&error) < 0) {
|
||||
if (!nm_utils_file_get_contents (-1,
|
||||
SECRET_KEY_FILE,
|
||||
10*1024,
|
||||
NM_UTILS_FILE_GET_CONTENTS_FLAG_SECRET,
|
||||
&file_content.str,
|
||||
&file_content.len,
|
||||
NULL,
|
||||
&error)) {
|
||||
if (!nm_utils_error_is_notfound (error)) {
|
||||
nm_log_warn (LOGD_CORE, "secret-key: failure reading secret key in \"%s\": %s (generate new key)",
|
||||
SECRET_KEY_FILE, error->message);
|
||||
@@ -2695,11 +2696,12 @@ _host_id_read (guint8 **out_host_id,
|
||||
nm_log_warn (LOGD_CORE, "secret-key: failure to generate good random data for secret-key (use non-persistent key)");
|
||||
else if (nm_utils_get_testing ()) {
|
||||
/* for test code, we don't write the generated secret-key to disk. */
|
||||
} else if (nm_utils_file_set_contents (SECRET_KEY_FILE,
|
||||
(const char *) new_content,
|
||||
len,
|
||||
0600,
|
||||
&error) < 0) {
|
||||
} else if (!nm_utils_file_set_contents (SECRET_KEY_FILE,
|
||||
(const char *) new_content,
|
||||
len,
|
||||
0600,
|
||||
NULL,
|
||||
&error)) {
|
||||
nm_log_warn (LOGD_CORE, "secret-key: failure to persist secret key in \"%s\" (%s) (use non-persistent key)",
|
||||
SECRET_KEY_FILE, error->message);
|
||||
g_clear_error (&error);
|
||||
@@ -2809,9 +2811,14 @@ again:
|
||||
NMUuid uuid;
|
||||
gboolean is_fake = FALSE;
|
||||
|
||||
nm_utils_file_get_contents (-1, "/proc/sys/kernel/random/boot_id", 0,
|
||||
nm_utils_file_get_contents (-1,
|
||||
"/proc/sys/kernel/random/boot_id",
|
||||
0,
|
||||
NM_UTILS_FILE_GET_CONTENTS_FLAG_NONE,
|
||||
&contents, NULL, NULL);
|
||||
&contents,
|
||||
NULL,
|
||||
NULL,
|
||||
NULL);
|
||||
if ( !contents
|
||||
|| !_nm_utils_uuid_parse (nm_strstrip (contents), &uuid)) {
|
||||
/* generate a random UUID instead. */
|
||||
|
@@ -868,14 +868,19 @@ _lookup_cached_link (const NMPCache *cache,
|
||||
static char *
|
||||
_linktype_read_devtype (int dirfd)
|
||||
{
|
||||
char *contents = NULL;
|
||||
gs_free char *contents = NULL;
|
||||
char *cont, *end;
|
||||
|
||||
nm_assert (dirfd >= 0);
|
||||
|
||||
if (nm_utils_file_get_contents (dirfd, "uevent", 1*1024*1024,
|
||||
NM_UTILS_FILE_GET_CONTENTS_FLAG_NONE,
|
||||
&contents, NULL, NULL) < 0)
|
||||
if (!nm_utils_file_get_contents (dirfd,
|
||||
"uevent",
|
||||
1*1024*1024,
|
||||
NM_UTILS_FILE_GET_CONTENTS_FLAG_NONE,
|
||||
&contents,
|
||||
NULL,
|
||||
NULL,
|
||||
NULL))
|
||||
return NULL;
|
||||
for (cont = contents; cont; cont = end) {
|
||||
end = strpbrk (cont, "\r\n");
|
||||
@@ -884,10 +889,9 @@ _linktype_read_devtype (int dirfd)
|
||||
if (strncmp (cont, DEVTYPE_PREFIX, NM_STRLEN (DEVTYPE_PREFIX)) == 0) {
|
||||
cont += NM_STRLEN (DEVTYPE_PREFIX);
|
||||
memmove (contents, cont, strlen (cont) + 1);
|
||||
return contents;
|
||||
return g_steal_pointer (&contents);
|
||||
}
|
||||
}
|
||||
g_free (contents);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
@@ -4406,12 +4410,17 @@ static void
|
||||
_log_dbg_sysctl_set_impl (NMPlatform *platform, const char *pathid, int dirfd, const char *path, const char *value)
|
||||
{
|
||||
GError *error = NULL;
|
||||
char *contents;
|
||||
gs_free char *contents = NULL;
|
||||
gs_free char *value_escaped = g_strescape (value, NULL);
|
||||
|
||||
if (nm_utils_file_get_contents (dirfd, path, 1*1024*1024,
|
||||
NM_UTILS_FILE_GET_CONTENTS_FLAG_NONE,
|
||||
&contents, NULL, &error) < 0) {
|
||||
if (!nm_utils_file_get_contents (dirfd,
|
||||
path,
|
||||
1*1024*1024,
|
||||
NM_UTILS_FILE_GET_CONTENTS_FLAG_NONE,
|
||||
&contents,
|
||||
NULL,
|
||||
NULL,
|
||||
&error)) {
|
||||
_LOGD ("sysctl: setting '%s' to '%s' (current value cannot be read: %s)", pathid ?: path, value_escaped, error->message);
|
||||
g_clear_error (&error);
|
||||
return;
|
||||
@@ -4425,7 +4434,6 @@ _log_dbg_sysctl_set_impl (NMPlatform *platform, const char *pathid, int dirfd, c
|
||||
|
||||
_LOGD ("sysctl: setting '%s' to '%s' (current value is '%s')", pathid ?: path, value_escaped, contents_escaped);
|
||||
}
|
||||
g_free (contents);
|
||||
}
|
||||
|
||||
#define _log_dbg_sysctl_set(platform, pathid, dirfd, path, value) \
|
||||
@@ -4841,7 +4849,7 @@ sysctl_get (NMPlatform *platform, const char *pathid, int dirfd, const char *pat
|
||||
{
|
||||
nm_auto_pop_netns NMPNetns *netns = NULL;
|
||||
GError *error = NULL;
|
||||
char *contents;
|
||||
gs_free char *contents = NULL;
|
||||
|
||||
ASSERT_SYSCTL_ARGS (pathid, dirfd, path);
|
||||
|
||||
@@ -4853,9 +4861,14 @@ sysctl_get (NMPlatform *platform, const char *pathid, int dirfd, const char *pat
|
||||
pathid = path;
|
||||
}
|
||||
|
||||
if (nm_utils_file_get_contents (dirfd, path, 1*1024*1024,
|
||||
NM_UTILS_FILE_GET_CONTENTS_FLAG_NONE,
|
||||
&contents, NULL, &error) < 0) {
|
||||
if (!nm_utils_file_get_contents (dirfd,
|
||||
path,
|
||||
1*1024*1024,
|
||||
NM_UTILS_FILE_GET_CONTENTS_FLAG_NONE,
|
||||
&contents,
|
||||
NULL,
|
||||
NULL,
|
||||
&error)) {
|
||||
NMLogLevel log_level = LOGL_ERR;
|
||||
int errsv = EBUSY;
|
||||
|
||||
@@ -4879,7 +4892,7 @@ sysctl_get (NMPlatform *platform, const char *pathid, int dirfd, const char *pat
|
||||
_log_dbg_sysctl_get (platform, pathid, contents);
|
||||
|
||||
/* errno is left undefined (as we don't return NULL). */
|
||||
return contents;
|
||||
return g_steal_pointer (&contents);
|
||||
}
|
||||
|
||||
/*****************************************************************************/
|
||||
|
@@ -2886,9 +2886,14 @@ test_sysctl_rename (void)
|
||||
case 0: {
|
||||
gs_free char *c = NULL;
|
||||
|
||||
if (nm_utils_file_get_contents (dirfd, "ifindex", 1*1024*1024,
|
||||
NM_UTILS_FILE_GET_CONTENTS_FLAG_NONE,
|
||||
&c, NULL, NULL) < 0)
|
||||
if (!nm_utils_file_get_contents (dirfd,
|
||||
"ifindex",
|
||||
1*1024*1024,
|
||||
NM_UTILS_FILE_GET_CONTENTS_FLAG_NONE,
|
||||
&c,
|
||||
NULL,
|
||||
NULL,
|
||||
NULL))
|
||||
g_assert_not_reached();
|
||||
g_assert_cmpint (ifindex[0], ==, (int) _nm_utils_ascii_str_to_int64 (c, 10, 0, G_MAXINT, -1));
|
||||
break;
|
||||
@@ -2952,9 +2957,14 @@ test_sysctl_netns_switch (void)
|
||||
{
|
||||
gs_free char *c = NULL;
|
||||
|
||||
if (nm_utils_file_get_contents (dirfd, "ifindex", 0,
|
||||
NM_UTILS_FILE_GET_CONTENTS_FLAG_NONE,
|
||||
&c, NULL, NULL) < 0)
|
||||
if (!nm_utils_file_get_contents (dirfd,
|
||||
"ifindex",
|
||||
0,
|
||||
NM_UTILS_FILE_GET_CONTENTS_FLAG_NONE,
|
||||
&c,
|
||||
NULL,
|
||||
NULL,
|
||||
NULL))
|
||||
g_assert_not_reached();
|
||||
g_assert_cmpint (ifindex, ==, (int) _nm_utils_ascii_str_to_int64 (c, 10, 0, G_MAXINT, -1));
|
||||
}
|
||||
@@ -2997,11 +3007,14 @@ test_sysctl_netns_switch (void)
|
||||
{
|
||||
gs_free char *c = NULL;
|
||||
|
||||
if (nm_utils_file_get_contents (-1,
|
||||
nm_sprintf_bufa (100, "/sys/class/net/%s/ifindex", IFNAME),
|
||||
0,
|
||||
NM_UTILS_FILE_GET_CONTENTS_FLAG_NONE,
|
||||
&c, NULL, NULL) < 0)
|
||||
if (!nm_utils_file_get_contents (-1,
|
||||
nm_sprintf_bufa (100, "/sys/class/net/%s/ifindex", IFNAME),
|
||||
0,
|
||||
NM_UTILS_FILE_GET_CONTENTS_FLAG_NONE,
|
||||
&c,
|
||||
NULL,
|
||||
NULL,
|
||||
NULL))
|
||||
ifindex_tmp = -1;
|
||||
else
|
||||
ifindex_tmp = _nm_utils_ascii_str_to_int64 (c, 10, 0, G_MAXINT, -2);
|
||||
|
@@ -301,11 +301,12 @@ write_blobs (GHashTable *blobs, GError **error)
|
||||
* can use paths from now on instead of pushing around the certificate
|
||||
* data itself.
|
||||
*/
|
||||
if (nm_utils_file_set_contents (filename,
|
||||
(const char *) g_bytes_get_data (blob, NULL),
|
||||
g_bytes_get_size (blob),
|
||||
0600,
|
||||
&write_error) < 0) {
|
||||
if (!nm_utils_file_set_contents (filename,
|
||||
(const char *) g_bytes_get_data (blob, NULL),
|
||||
g_bytes_get_size (blob),
|
||||
0600,
|
||||
NULL,
|
||||
&write_error)) {
|
||||
g_set_error (error, NM_SETTINGS_ERROR, NM_SETTINGS_ERROR_FAILED,
|
||||
"Could not write certificate to file \"%s\": %s",
|
||||
filename,
|
||||
|
@@ -790,7 +790,7 @@ svOpenFileInternal (const char *name, gboolean create, GError **error)
|
||||
shvarFile *s;
|
||||
gboolean closefd = FALSE;
|
||||
int errsv = 0;
|
||||
char *arena;
|
||||
gs_free char *arena = NULL;
|
||||
const char *p, *q;
|
||||
gs_free_error GError *local = NULL;
|
||||
nm_auto_close int fd = -1;
|
||||
@@ -816,13 +816,14 @@ svOpenFileInternal (const char *name, gboolean create, GError **error)
|
||||
return NULL;
|
||||
}
|
||||
|
||||
if (nm_utils_fd_get_contents (closefd ? nm_steal_fd (&fd) : fd,
|
||||
closefd,
|
||||
10 * 1024 * 1024,
|
||||
NM_UTILS_FILE_GET_CONTENTS_FLAG_NONE,
|
||||
&arena,
|
||||
NULL,
|
||||
&local) < 0) {
|
||||
if (!nm_utils_fd_get_contents (closefd ? nm_steal_fd (&fd) : fd,
|
||||
closefd,
|
||||
10 * 1024 * 1024,
|
||||
NM_UTILS_FILE_GET_CONTENTS_FLAG_NONE,
|
||||
&arena,
|
||||
NULL,
|
||||
NULL,
|
||||
&local)) {
|
||||
if (create)
|
||||
return svFile_new (name);
|
||||
|
||||
@@ -839,7 +840,6 @@ svOpenFileInternal (const char *name, gboolean create, GError **error)
|
||||
c_list_link_tail (&s->lst_head, &line_new_parse (p, q - p)->lst);
|
||||
if (p[0])
|
||||
c_list_link_tail (&s->lst_head, &line_new_parse (p, strlen (p))->lst);
|
||||
g_free (arena);
|
||||
|
||||
/* closefd is set if we opened the file read-only, so go ahead and
|
||||
* close it, because we can't write to it anyway */
|
||||
|
@@ -266,7 +266,12 @@ nms_keyfile_nmmeta_write (const char *dirname,
|
||||
|
||||
contents = g_key_file_to_data (kf, &length, NULL);
|
||||
|
||||
if (nm_utils_file_set_contents (full_filename, contents, length, 0600, NULL) < 0) {
|
||||
if (!nm_utils_file_set_contents (full_filename,
|
||||
contents,
|
||||
length,
|
||||
0600,
|
||||
NULL,
|
||||
NULL)) {
|
||||
NM_SET_OUT (out_full_filename, g_steal_pointer (&full_filename_tmp));
|
||||
return FALSE;
|
||||
}
|
||||
|
@@ -126,8 +126,12 @@ cert_writer (NMConnection *connection,
|
||||
new_path = g_strdup_printf ("%s/%s-%s.%s", info->keyfile_dir, nm_connection_get_uuid (connection),
|
||||
cert_data->vtable->file_suffix, ext);
|
||||
|
||||
success = (nm_utils_file_set_contents (new_path, (const char *) blob_data,
|
||||
blob_len, 0600, &local) >= 0);
|
||||
success = nm_utils_file_set_contents (new_path,
|
||||
(const char *) blob_data,
|
||||
blob_len,
|
||||
0600,
|
||||
NULL,
|
||||
&local);
|
||||
if (success) {
|
||||
/* Write the path value to the keyfile.
|
||||
* We know, that basename(new_path) starts with a UUID, hence no conflict with "data:;base64," */
|
||||
@@ -309,7 +313,12 @@ _internal_write_connection (NMConnection *connection,
|
||||
return FALSE;
|
||||
}
|
||||
|
||||
nm_utils_file_set_contents (path, kf_content_buf, kf_content_len, 0600, &local_err);
|
||||
nm_utils_file_set_contents (path,
|
||||
kf_content_buf,
|
||||
kf_content_len,
|
||||
0600,
|
||||
NULL,
|
||||
&local_err);
|
||||
if (local_err) {
|
||||
g_set_error (error, NM_SETTINGS_ERROR, NM_SETTINGS_ERROR_FAILED,
|
||||
"error writing to file '%s': %s",
|
||||
|
Reference in New Issue
Block a user