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.
This commit is contained in:
Andrew Zaborowski 2021-04-14 15:09:39 +02:00 committed by Thomas Haller
parent caa1b5c60d
commit 38ac64ba62
No known key found for this signature in database
GPG key ID: 29C2366E4DFC5728
7 changed files with 40 additions and 17 deletions

View file

@ -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,

View file

@ -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,

View file

@ -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);
}

View file

@ -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,

View file

@ -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)) {

View file

@ -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;

View file

@ -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);