mirror of
https://github.com/systemd/systemd.git
synced 2024-12-23 21:35:11 +03:00
basic/fileio: extend atomic file writing with timestamp setting
There should be no functional change.
This commit is contained in:
parent
488ab41cb8
commit
39c38d773f
@ -24,14 +24,14 @@
|
||||
#include "fileio.h"
|
||||
#include "selinux-util.h"
|
||||
|
||||
int write_string_file_atomic_label(const char *fn, const char *line) {
|
||||
int write_string_file_atomic_label_ts(const char *fn, const char *line, struct timespec *ts) {
|
||||
int r;
|
||||
|
||||
r = mac_selinux_create_file_prepare(fn, S_IFREG);
|
||||
if (r < 0)
|
||||
return r;
|
||||
|
||||
r = write_string_file(fn, line, WRITE_STRING_FILE_CREATE|WRITE_STRING_FILE_ATOMIC);
|
||||
r = write_string_file_ts(fn, line, WRITE_STRING_FILE_CREATE|WRITE_STRING_FILE_ATOMIC, ts);
|
||||
|
||||
mac_selinux_create_file_clear();
|
||||
|
||||
|
@ -24,7 +24,10 @@
|
||||
|
||||
#include "fileio.h"
|
||||
|
||||
int write_string_file_atomic_label(const char *fn, const char *line);
|
||||
int write_string_file_atomic_label_ts(const char *fn, const char *line, struct timespec *ts);
|
||||
static inline int write_string_file_atomic_label(const char *fn, const char *line) {
|
||||
return write_string_file_atomic_label_ts(fn, line, NULL);
|
||||
}
|
||||
int write_env_file_label(const char *fname, char **l);
|
||||
int fopen_temporary_label(const char *target,
|
||||
const char *path, FILE **f, char **temp_path);
|
||||
|
@ -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;
|
||||
|
||||
|
@ -35,8 +35,14 @@ typedef enum {
|
||||
WRITE_STRING_FILE_VERIFY_ON_FAILURE = 8,
|
||||
} WriteStringFileFlags;
|
||||
|
||||
int write_string_stream(FILE *f, const char *line, bool enforce_newline);
|
||||
int write_string_file(const char *fn, const char *line, WriteStringFileFlags flags);
|
||||
int write_string_stream_ts(FILE *f, const char *line, bool enforce_newline, struct timespec *ts);
|
||||
static inline int write_string_stream(FILE *f, const char *line, bool enforce_newline) {
|
||||
return write_string_stream_ts(f, line, enforce_newline, NULL);
|
||||
}
|
||||
int write_string_file_ts(const char *fn, const char *line, WriteStringFileFlags flags, struct timespec *ts);
|
||||
static inline int write_string_file(const char *fn, const char *line, WriteStringFileFlags flags) {
|
||||
return write_string_file_ts(fn, line, flags, NULL);
|
||||
}
|
||||
|
||||
int read_one_line_file(const char *fn, char **line);
|
||||
int read_full_file(const char *fn, char **contents, size_t *size);
|
||||
|
Loading…
Reference in New Issue
Block a user