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:
Beniamino Galvani 2025-10-14 10:41:26 +02:00
parent d06fd85e57
commit c312390932
10 changed files with 30 additions and 9 deletions

View file

@ -684,7 +684,7 @@ iwd_config_write(GKeyFile *config,
* in the last few filename characters -- it cannot end in .open, .psk
* 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 *

View file

@ -81,7 +81,7 @@ nm_main_utils_write_pidfile(const char *pidfile)
char pid[16];
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);
return FALSE;
}

View file

@ -2865,6 +2865,7 @@ _host_id_read(guint8 **out_host_id, gsize *out_host_id_len)
0600,
NULL,
NULL,
NULL,
&error)) {
nm_log_warn(
LOGD_CORE,

View file

@ -186,6 +186,7 @@ ip_again:
00644,
NULL,
NULL,
NULL,
NULL);
nm_log_dbg(LOGD_PLATFORM, "dump to file complete");

View file

@ -320,6 +320,7 @@ write_blobs(GHashTable *blobs, GError **error)
0600,
NULL,
NULL,
NULL,
&write_error)) {
g_set_error(error,
NM_SETTINGS_ERROR,

View file

@ -280,6 +280,7 @@ nms_keyfile_nmmeta_write(const char *dirname,
length,
0600,
NULL,
NULL,
&errsv,
NULL)) {
NM_SET_OUT(out_full_filename, g_steal_pointer(&full_filename_tmp));

View file

@ -133,6 +133,7 @@ cert_writer(NMConnection *connection,
0600,
NULL,
NULL,
NULL,
&local);
if (success) {
/* 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) {
g_set_error(error,
NM_SETTINGS_ERROR,

View file

@ -415,8 +415,10 @@ 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 and optionally the last access
* and last modification times.
* specifying:
* - the file mode (@mode)
* - optionally, the last access and modification times (@times)
* - optionally, a fixed name for the temporary file (@tmp_name)
*/
gboolean
nm_utils_file_set_contents(const char *filename,
@ -424,10 +426,11 @@ nm_utils_file_set_contents(const char *filename,
gssize length,
mode_t mode,
const struct timespec *times,
const char *tmp_name,
int *out_errsv,
GError **error)
{
gs_free char *tmp_name = NULL;
gs_free char *tmp_name_free = NULL;
struct stat statbuf;
int errsv;
gssize s;
@ -442,8 +445,13 @@ nm_utils_file_set_contents(const char *filename,
if (length == -1)
length = strlen(contents);
tmp_name = g_strdup_printf("%s.XXXXXX", filename);
fd = g_mkstemp_full(tmp_name, O_RDWR | O_CLOEXEC, mode);
if (tmp_name) {
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) {
return _get_contents_error_errno(error, out_errsv, "failed to create file %s", tmp_name);
}

View file

@ -55,6 +55,7 @@ gboolean nm_utils_file_set_contents(const char *filename,
gssize length,
mode_t mode,
const struct timespec *times,
const char *tmp_name,
int *out_errsv,
GError **error);

View file

@ -78,7 +78,7 @@ 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, NULL, NULL, &error))
if (!nm_utils_file_set_contents(full_filename, data, len, 0600, NULL, NULL, NULL, &error))
goto err_out;
} else
g_print("\n*** Connection '%s' ***\n\n%s", basename, data);