about summary refs log tree commit diff
path: root/src/libnm-glib-aux/nm-io-utils.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/libnm-glib-aux/nm-io-utils.c')
-rw-r--r--src/libnm-glib-aux/nm-io-utils.c18
1 files changed, 13 insertions, 5 deletions
diff --git a/src/libnm-glib-aux/nm-io-utils.c b/src/libnm-glib-aux/nm-io-utils.c
index 9443172b..d26ecee4 100644
--- a/src/libnm-glib-aux/nm-io-utils.c
+++ b/src/libnm-glib-aux/nm-io-utils.c
@@ -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);
     }