summaryrefslogtreecommitdiff
path: root/src/basic/fileio.c
diff options
context:
space:
mode:
authorZbigniew Jędrzejewski-Szmek <zbyszek@in.waw.pl>2017-05-11 10:23:36 -0400
committerZbigniew Jędrzejewski-Szmek <zbyszek@in.waw.pl>2017-05-11 10:23:36 -0400
commit39c38d773fbe2b4f76ff29ecd3344640efb9a86c (patch)
treefcdb651664382d4696f35ece2ab836084529ac30 /src/basic/fileio.c
parent488ab41cb89828e68162f34fb68241bbda700c05 (diff)
downloadsystemd-39c38d773fbe2b4f76ff29ecd3344640efb9a86c.tar.gz
basic/fileio: extend atomic file writing with timestamp setting
There should be no functional change.
Diffstat (limited to 'src/basic/fileio.c')
-rw-r--r--src/basic/fileio.c16
1 files changed, 12 insertions, 4 deletions
diff --git a/src/basic/fileio.c b/src/basic/fileio.c
index 7c2c2b38f5..9a185e3e60 100644
--- a/src/basic/fileio.c
+++ b/src/basic/fileio.c
@@ -51,7 +51,7 @@
#define READ_FULL_BYTES_MAX (4U*1024U*1024U)
-int write_string_stream(FILE *f, const char *line, bool enforce_newline) {
+int write_string_stream_ts(FILE *f, const char *line, bool enforce_newline, struct timespec *ts) {
assert(f);
assert(line);
@@ -60,6 +60,13 @@ int write_string_stream(FILE *f, const char *line, bool enforce_newline) {
if (enforce_newline && !endswith(line, "\n"))
fputc('\n', f);
+ if (ts) {
+ struct timespec twice[2] = {*ts, *ts};
+
+ if (futimens(fileno(f), twice) < 0)
+ return -errno;
+ }
+
return fflush_and_check(f);
}
@@ -89,7 +96,7 @@ static int write_string_file_atomic(const char *fn, const char *line, bool enfor
return r;
}
-int write_string_file(const char *fn, const char *line, WriteStringFileFlags flags) {
+int write_string_file_ts(const char *fn, const char *line, WriteStringFileFlags flags, struct timespec *ts) {
_cleanup_fclose_ FILE *f = NULL;
int q, r;
@@ -104,7 +111,8 @@ int write_string_file(const char *fn, const char *line, WriteStringFileFlags fla
goto fail;
return r;
- }
+ } else
+ assert(ts == NULL);
if (flags & WRITE_STRING_FILE_CREATE) {
f = fopen(fn, "we");
@@ -131,7 +139,7 @@ int write_string_file(const char *fn, const char *line, WriteStringFileFlags fla
}
}
- r = write_string_stream(f, line, !(flags & WRITE_STRING_FILE_AVOID_NEWLINE));
+ r = write_string_stream_ts(f, line, !(flags & WRITE_STRING_FILE_AVOID_NEWLINE), ts);
if (r < 0)
goto fail;