fileio: support writing atomic files with timestamp
Let's make sure "ts" is taken into account when writing atomic files, too.
This commit is contained in:
+12
-5
@@ -71,7 +71,13 @@ int write_string_stream_ts(FILE *f, const char *line, bool enforce_newline, stru
|
||||
return fflush_and_check(f);
|
||||
}
|
||||
|
||||
static int write_string_file_atomic(const char *fn, const char *line, bool enforce_newline, bool do_fsync) {
|
||||
static int write_string_file_atomic(
|
||||
const char *fn,
|
||||
const char *line,
|
||||
bool enforce_newline,
|
||||
bool do_fsync,
|
||||
struct timespec *ts) {
|
||||
|
||||
_cleanup_fclose_ FILE *f = NULL;
|
||||
_cleanup_free_ char *p = NULL;
|
||||
int r;
|
||||
@@ -85,10 +91,9 @@ static int write_string_file_atomic(const char *fn, const char *line, bool enfor
|
||||
|
||||
(void) fchmod_umask(fileno(f), 0644);
|
||||
|
||||
r = write_string_stream(f, line, enforce_newline);
|
||||
r = write_string_stream_ts(f, line, enforce_newline, ts);
|
||||
if (r >= 0 && do_fsync)
|
||||
r = fflush_sync_and_check(f);
|
||||
|
||||
if (r >= 0) {
|
||||
if (rename(p, fn) < 0)
|
||||
r = -errno;
|
||||
@@ -113,8 +118,10 @@ int write_string_file_ts(const char *fn, const char *line, WriteStringFileFlags
|
||||
if (flags & WRITE_STRING_FILE_ATOMIC) {
|
||||
assert(flags & WRITE_STRING_FILE_CREATE);
|
||||
|
||||
r = write_string_file_atomic(fn, line, !(flags & WRITE_STRING_FILE_AVOID_NEWLINE),
|
||||
flags & WRITE_STRING_FILE_SYNC);
|
||||
r = write_string_file_atomic(fn,
|
||||
line,
|
||||
!(flags & WRITE_STRING_FILE_AVOID_NEWLINE),
|
||||
flags & WRITE_STRING_FILE_SYNC, ts);
|
||||
if (r < 0)
|
||||
goto fail;
|
||||
|
||||
|
||||
Reference in New Issue
Block a user