mirror of
https://gitlab.freedesktop.org/NetworkManager/NetworkManager.git
synced 2025-12-24 14:50:07 +01:00
libnm-glib-aux: add temp name argument to nm_utils_file_set_contents()
In some cases it's useful to specify the name of the temporary file to be used.
This commit is contained in:
parent
d06fd85e57
commit
c312390932
10 changed files with 30 additions and 9 deletions
|
|
@ -684,7 +684,7 @@ iwd_config_write(GKeyFile *config,
|
||||||
* in the last few filename characters -- it cannot end in .open, .psk
|
* in the last few filename characters -- it cannot end in .open, .psk
|
||||||
* or .8021x.
|
* or .8021x.
|
||||||
*/
|
*/
|
||||||
return nm_utils_file_set_contents(filepath, data, length, 0600, times, NULL, error);
|
return nm_utils_file_set_contents(filepath, data, length, 0600, times, NULL, NULL, error);
|
||||||
}
|
}
|
||||||
|
|
||||||
static const char *
|
static const char *
|
||||||
|
|
|
||||||
|
|
@ -81,7 +81,7 @@ nm_main_utils_write_pidfile(const char *pidfile)
|
||||||
char pid[16];
|
char pid[16];
|
||||||
|
|
||||||
nm_sprintf_buf(pid, "%lld", (long long) getpid());
|
nm_sprintf_buf(pid, "%lld", (long long) getpid());
|
||||||
if (!nm_utils_file_set_contents(pidfile, pid, -1, 00644, NULL, NULL, &error)) {
|
if (!nm_utils_file_set_contents(pidfile, pid, -1, 00644, NULL, NULL, NULL, &error)) {
|
||||||
fprintf(stderr, _("Writing to %s failed: %s\n"), pidfile, error->message);
|
fprintf(stderr, _("Writing to %s failed: %s\n"), pidfile, error->message);
|
||||||
return FALSE;
|
return FALSE;
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -2865,6 +2865,7 @@ _host_id_read(guint8 **out_host_id, gsize *out_host_id_len)
|
||||||
0600,
|
0600,
|
||||||
NULL,
|
NULL,
|
||||||
NULL,
|
NULL,
|
||||||
|
NULL,
|
||||||
&error)) {
|
&error)) {
|
||||||
nm_log_warn(
|
nm_log_warn(
|
||||||
LOGD_CORE,
|
LOGD_CORE,
|
||||||
|
|
|
||||||
|
|
@ -186,6 +186,7 @@ ip_again:
|
||||||
00644,
|
00644,
|
||||||
NULL,
|
NULL,
|
||||||
NULL,
|
NULL,
|
||||||
|
NULL,
|
||||||
NULL);
|
NULL);
|
||||||
|
|
||||||
nm_log_dbg(LOGD_PLATFORM, "dump to file complete");
|
nm_log_dbg(LOGD_PLATFORM, "dump to file complete");
|
||||||
|
|
|
||||||
|
|
@ -320,6 +320,7 @@ write_blobs(GHashTable *blobs, GError **error)
|
||||||
0600,
|
0600,
|
||||||
NULL,
|
NULL,
|
||||||
NULL,
|
NULL,
|
||||||
|
NULL,
|
||||||
&write_error)) {
|
&write_error)) {
|
||||||
g_set_error(error,
|
g_set_error(error,
|
||||||
NM_SETTINGS_ERROR,
|
NM_SETTINGS_ERROR,
|
||||||
|
|
|
||||||
|
|
@ -280,6 +280,7 @@ nms_keyfile_nmmeta_write(const char *dirname,
|
||||||
length,
|
length,
|
||||||
0600,
|
0600,
|
||||||
NULL,
|
NULL,
|
||||||
|
NULL,
|
||||||
&errsv,
|
&errsv,
|
||||||
NULL)) {
|
NULL)) {
|
||||||
NM_SET_OUT(out_full_filename, g_steal_pointer(&full_filename_tmp));
|
NM_SET_OUT(out_full_filename, g_steal_pointer(&full_filename_tmp));
|
||||||
|
|
|
||||||
|
|
@ -133,6 +133,7 @@ cert_writer(NMConnection *connection,
|
||||||
0600,
|
0600,
|
||||||
NULL,
|
NULL,
|
||||||
NULL,
|
NULL,
|
||||||
|
NULL,
|
||||||
&local);
|
&local);
|
||||||
if (success) {
|
if (success) {
|
||||||
/* Write the path value to the keyfile.
|
/* Write the path value to the keyfile.
|
||||||
|
|
@ -384,7 +385,14 @@ _internal_write_connection(NMConnection *connection,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
nm_utils_file_set_contents(path, kf_content_buf, kf_content_len, 0600, NULL, NULL, &local_err);
|
nm_utils_file_set_contents(path,
|
||||||
|
kf_content_buf,
|
||||||
|
kf_content_len,
|
||||||
|
0600,
|
||||||
|
NULL,
|
||||||
|
NULL,
|
||||||
|
NULL,
|
||||||
|
&local_err);
|
||||||
if (local_err) {
|
if (local_err) {
|
||||||
g_set_error(error,
|
g_set_error(error,
|
||||||
NM_SETTINGS_ERROR,
|
NM_SETTINGS_ERROR,
|
||||||
|
|
|
||||||
|
|
@ -415,8 +415,10 @@ nm_utils_file_get_contents(int dirfd,
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* Copied from GLib's g_file_set_contents() et al., but allows
|
* Copied from GLib's g_file_set_contents() et al., but allows
|
||||||
* specifying a mode for the new file and optionally the last access
|
* specifying:
|
||||||
* and last modification times.
|
* - the file mode (@mode)
|
||||||
|
* - optionally, the last access and modification times (@times)
|
||||||
|
* - optionally, a fixed name for the temporary file (@tmp_name)
|
||||||
*/
|
*/
|
||||||
gboolean
|
gboolean
|
||||||
nm_utils_file_set_contents(const char *filename,
|
nm_utils_file_set_contents(const char *filename,
|
||||||
|
|
@ -424,10 +426,11 @@ nm_utils_file_set_contents(const char *filename,
|
||||||
gssize length,
|
gssize length,
|
||||||
mode_t mode,
|
mode_t mode,
|
||||||
const struct timespec *times,
|
const struct timespec *times,
|
||||||
|
const char *tmp_name,
|
||||||
int *out_errsv,
|
int *out_errsv,
|
||||||
GError **error)
|
GError **error)
|
||||||
{
|
{
|
||||||
gs_free char *tmp_name = NULL;
|
gs_free char *tmp_name_free = NULL;
|
||||||
struct stat statbuf;
|
struct stat statbuf;
|
||||||
int errsv;
|
int errsv;
|
||||||
gssize s;
|
gssize s;
|
||||||
|
|
@ -442,8 +445,13 @@ nm_utils_file_set_contents(const char *filename,
|
||||||
if (length == -1)
|
if (length == -1)
|
||||||
length = strlen(contents);
|
length = strlen(contents);
|
||||||
|
|
||||||
tmp_name = g_strdup_printf("%s.XXXXXX", filename);
|
if (tmp_name) {
|
||||||
fd = g_mkstemp_full(tmp_name, O_RDWR | O_CLOEXEC, mode);
|
fd = open(tmp_name, O_CREAT | O_RDWR | O_TRUNC | O_CLOEXEC, mode);
|
||||||
|
} else {
|
||||||
|
tmp_name_free = g_strdup_printf("%s.XXXXXX", filename);
|
||||||
|
tmp_name = tmp_name_free;
|
||||||
|
fd = g_mkstemp_full(tmp_name_free, O_RDWR | O_CLOEXEC, mode);
|
||||||
|
}
|
||||||
if (fd < 0) {
|
if (fd < 0) {
|
||||||
return _get_contents_error_errno(error, out_errsv, "failed to create file %s", tmp_name);
|
return _get_contents_error_errno(error, out_errsv, "failed to create file %s", tmp_name);
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -55,6 +55,7 @@ gboolean nm_utils_file_set_contents(const char *filename,
|
||||||
gssize length,
|
gssize length,
|
||||||
mode_t mode,
|
mode_t mode,
|
||||||
const struct timespec *times,
|
const struct timespec *times,
|
||||||
|
const char *tmp_name,
|
||||||
int *out_errsv,
|
int *out_errsv,
|
||||||
GError **error);
|
GError **error);
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -78,7 +78,7 @@ output_conn(gpointer key, gpointer value, gpointer user_data)
|
||||||
filename = nm_keyfile_utils_create_filename(basename, TRUE);
|
filename = nm_keyfile_utils_create_filename(basename, TRUE);
|
||||||
full_filename = g_build_filename(connections_dir, filename, NULL);
|
full_filename = g_build_filename(connections_dir, filename, NULL);
|
||||||
|
|
||||||
if (!nm_utils_file_set_contents(full_filename, data, len, 0600, NULL, NULL, &error))
|
if (!nm_utils_file_set_contents(full_filename, data, len, 0600, NULL, NULL, NULL, &error))
|
||||||
goto err_out;
|
goto err_out;
|
||||||
} else
|
} else
|
||||||
g_print("\n*** Connection '%s' ***\n\n%s", basename, data);
|
g_print("\n*** Connection '%s' ***\n\n%s", basename, data);
|
||||||
|
|
|
||||||
Loading…
Add table
Reference in a new issue