summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAndrew Zaborowski <andrew.zaborowski@intel.com>2021-04-14 15:09:39 +0200
committerThomas Haller <thaller@redhat.com>2021-04-19 18:11:11 +0200
commit38ac64ba620c63bf1361dcac081c625afa66d308 (patch)
tree73fa625bf3e8ec36a8a8a5a43510d0dae94fc1f0
parentcaa1b5c60d0caafb234a9344d378374752fe3167 (diff)
downloadNetworkManager-38ac64ba620c63bf1361dcac081c625afa66d308.tar.gz
glib-aux: Set file timestamps in nm_utils_file_set_contents
Extend nm_utils_file_set_contents to be able to optionally set the last access + last modification times on the file being created, in addition to the mode.
-rw-r--r--src/core/nm-core-utils.c1
-rw-r--r--src/core/settings/plugins/ifcfg-rh/nms-ifcfg-rh-writer.c1
-rw-r--r--src/core/settings/plugins/keyfile/nms-keyfile-utils.c8
-rw-r--r--src/core/settings/plugins/keyfile/nms-keyfile-writer.c5
-rw-r--r--src/libnm-glib-aux/nm-io-utils.c27
-rw-r--r--src/libnm-glib-aux/nm-io-utils.h13
-rw-r--r--src/nm-initrd-generator/nm-initrd-generator.c2
7 files changed, 40 insertions, 17 deletions
diff --git a/src/core/nm-core-utils.c b/src/core/nm-core-utils.c
index ac146be9d8..8fe6b70982 100644
--- a/src/core/nm-core-utils.c
+++ b/src/core/nm-core-utils.c
@@ -2732,6 +2732,7 @@ _host_id_read(guint8 **out_host_id, gsize *out_host_id_len)
len,
0600,
NULL,
+ NULL,
&error)) {
nm_log_warn(
LOGD_CORE,
diff --git a/src/core/settings/plugins/ifcfg-rh/nms-ifcfg-rh-writer.c b/src/core/settings/plugins/ifcfg-rh/nms-ifcfg-rh-writer.c
index 955a9e89d5..9801d3b3e7 100644
--- a/src/core/settings/plugins/ifcfg-rh/nms-ifcfg-rh-writer.c
+++ b/src/core/settings/plugins/ifcfg-rh/nms-ifcfg-rh-writer.c
@@ -299,6 +299,7 @@ write_blobs(GHashTable *blobs, GError **error)
g_bytes_get_size(blob),
0600,
NULL,
+ NULL,
&write_error)) {
g_set_error(error,
NM_SETTINGS_ERROR,
diff --git a/src/core/settings/plugins/keyfile/nms-keyfile-utils.c b/src/core/settings/plugins/keyfile/nms-keyfile-utils.c
index c26b8676f1..affe1c3d2a 100644
--- a/src/core/settings/plugins/keyfile/nms-keyfile-utils.c
+++ b/src/core/settings/plugins/keyfile/nms-keyfile-utils.c
@@ -269,7 +269,13 @@ 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, &errsv, NULL)) {
+ if (!nm_utils_file_set_contents(full_filename,
+ contents,
+ length,
+ 0600,
+ NULL,
+ &errsv,
+ NULL)) {
NM_SET_OUT(out_full_filename, g_steal_pointer(&full_filename_tmp));
return -NM_ERRNO_NATIVE(errsv);
}
diff --git a/src/core/settings/plugins/keyfile/nms-keyfile-writer.c b/src/core/settings/plugins/keyfile/nms-keyfile-writer.c
index 661a828058..70afdc2362 100644
--- a/src/core/settings/plugins/keyfile/nms-keyfile-writer.c
+++ b/src/core/settings/plugins/keyfile/nms-keyfile-writer.c
@@ -125,13 +125,14 @@ cert_writer(NMConnection * connection,
/* FIXME(keyfile-parse-in-memory): writer must not access/write to the file system before
* being sure that the entire profile can be written and all circumstances are good to
- * proceed. That means, while writing we must only collect the blogs in-memory, and write
+ * proceed. That means, while writing we must only collect the blobs in-memory, and write
* them all in the end together (or not at all). */
success = nm_utils_file_set_contents(new_path,
(const char *) blob_data,
blob_len,
0600,
NULL,
+ NULL,
&local);
if (success) {
/* Write the path value to the keyfile.
@@ -378,7 +379,7 @@ _internal_write_connection(NMConnection * connection,
}
}
- nm_utils_file_set_contents(path, kf_content_buf, kf_content_len, 0600, NULL, &local_err);
+ nm_utils_file_set_contents(path, kf_content_buf, kf_content_len, 0600, NULL, NULL, &local_err);
if (local_err) {
g_set_error(error,
NM_SETTINGS_ERROR,
diff --git a/src/libnm-glib-aux/nm-io-utils.c b/src/libnm-glib-aux/nm-io-utils.c
index e02049af1a..0176abf7f8 100644
--- a/src/libnm-glib-aux/nm-io-utils.c
+++ b/src/libnm-glib-aux/nm-io-utils.c
@@ -332,15 +332,17 @@ 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.
+ * specifying a mode for the new file and optionally the last access
+ * and last modification times.
*/
gboolean
-nm_utils_file_set_contents(const char *filename,
- const char *contents,
- gssize length,
- mode_t mode,
- int * out_errsv,
- GError ** error)
+nm_utils_file_set_contents(const char * filename,
+ const char * contents,
+ gssize length,
+ mode_t mode,
+ const struct timespec *times,
+ int * out_errsv,
+ GError ** error)
{
gs_free char *tmp_name = NULL;
struct stat statbuf;
@@ -399,6 +401,17 @@ nm_utils_file_set_contents(const char *filename,
}
}
+ if (times && futimens(fd, times) != 0) {
+ errsv = NM_ERRNO_NATIVE(errno);
+ nm_close(fd);
+ unlink(tmp_name);
+ return _get_contents_error(error,
+ errsv,
+ out_errsv,
+ "failed to set atime and mtime on %s",
+ tmp_name);
+ }
+
nm_close(fd);
if (rename(tmp_name, filename)) {
diff --git a/src/libnm-glib-aux/nm-io-utils.h b/src/libnm-glib-aux/nm-io-utils.h
index 8182f5cf60..2b132ff07d 100644
--- a/src/libnm-glib-aux/nm-io-utils.h
+++ b/src/libnm-glib-aux/nm-io-utils.h
@@ -40,12 +40,13 @@ gboolean nm_utils_file_get_contents(int dirfd,
int * out_errsv,
GError ** error);
-gboolean nm_utils_file_set_contents(const char *filename,
- const char *contents,
- gssize length,
- mode_t mode,
- int * out_errsv,
- GError ** error);
+gboolean nm_utils_file_set_contents(const char * filename,
+ const char * contents,
+ gssize length,
+ mode_t mode,
+ const struct timespec *times,
+ int * out_errsv,
+ GError ** error);
struct _NMStrBuf;
diff --git a/src/nm-initrd-generator/nm-initrd-generator.c b/src/nm-initrd-generator/nm-initrd-generator.c
index 337e4559d9..490a4547d5 100644
--- a/src/nm-initrd-generator/nm-initrd-generator.c
+++ b/src/nm-initrd-generator/nm-initrd-generator.c
@@ -54,7 +54,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, &error))
+ if (!nm_utils_file_set_contents(full_filename, data, len, 0600, NULL, NULL, &error))
goto err_out;
} else
g_print("\n*** Connection '%s' ***\n\n%s", basename, data);