fileio: make write_string_stream() accept flags parameter
Let's make write_string_stream() and write_string_file() more alike, and pass the same flag set so that we can remove a number of boolean parameters.
This commit is contained in:
+17
-12
@@ -52,13 +52,17 @@
|
||||
|
||||
#define READ_FULL_BYTES_MAX (4U*1024U*1024U)
|
||||
|
||||
int write_string_stream_ts(FILE *f, const char *line, bool enforce_newline, struct timespec *ts) {
|
||||
int write_string_stream_ts(
|
||||
FILE *f,
|
||||
const char *line,
|
||||
WriteStringFileFlags flags,
|
||||
struct timespec *ts) {
|
||||
|
||||
assert(f);
|
||||
assert(line);
|
||||
|
||||
fputs(line, f);
|
||||
if (enforce_newline && !endswith(line, "\n"))
|
||||
if (!(flags & WRITE_STRING_FILE_AVOID_NEWLINE) && !endswith(line, "\n"))
|
||||
fputc('\n', f);
|
||||
|
||||
if (ts) {
|
||||
@@ -74,8 +78,7 @@ int write_string_stream_ts(FILE *f, const char *line, bool enforce_newline, stru
|
||||
static int write_string_file_atomic(
|
||||
const char *fn,
|
||||
const char *line,
|
||||
bool enforce_newline,
|
||||
bool do_fsync,
|
||||
WriteStringFileFlags flags,
|
||||
struct timespec *ts) {
|
||||
|
||||
_cleanup_fclose_ FILE *f = NULL;
|
||||
@@ -91,8 +94,8 @@ static int write_string_file_atomic(
|
||||
|
||||
(void) fchmod_umask(fileno(f), 0644);
|
||||
|
||||
r = write_string_stream_ts(f, line, enforce_newline, ts);
|
||||
if (r >= 0 && do_fsync)
|
||||
r = write_string_stream_ts(f, line, flags, ts);
|
||||
if (r >= 0 && (flags & WRITE_STRING_FILE_SYNC))
|
||||
r = fflush_sync_and_check(f);
|
||||
if (r >= 0) {
|
||||
if (rename(p, fn) < 0)
|
||||
@@ -105,7 +108,12 @@ static int write_string_file_atomic(
|
||||
return r;
|
||||
}
|
||||
|
||||
int write_string_file_ts(const char *fn, const char *line, WriteStringFileFlags flags, struct timespec *ts) {
|
||||
int write_string_file_ts(
|
||||
const char *fn,
|
||||
const char *line,
|
||||
WriteStringFileFlags flags,
|
||||
struct timespec *ts) {
|
||||
|
||||
_cleanup_fclose_ FILE *f = NULL;
|
||||
int q, r;
|
||||
|
||||
@@ -118,10 +126,7 @@ 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, ts);
|
||||
r = write_string_file_atomic(fn, line, flags, ts);
|
||||
if (r < 0)
|
||||
goto fail;
|
||||
|
||||
@@ -154,7 +159,7 @@ int write_string_file_ts(const char *fn, const char *line, WriteStringFileFlags
|
||||
}
|
||||
}
|
||||
|
||||
r = write_string_stream_ts(f, line, !(flags & WRITE_STRING_FILE_AVOID_NEWLINE), ts);
|
||||
r = write_string_stream_ts(f, line, flags, ts);
|
||||
if (r < 0)
|
||||
goto fail;
|
||||
|
||||
|
||||
+3
-3
@@ -36,9 +36,9 @@ typedef enum {
|
||||
WRITE_STRING_FILE_SYNC = 1<<4,
|
||||
} WriteStringFileFlags;
|
||||
|
||||
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_stream_ts(FILE *f, const char *line, WriteStringFileFlags flags, struct timespec *ts);
|
||||
static inline int write_string_stream(FILE *f, const char *line, WriteStringFileFlags flags) {
|
||||
return write_string_stream_ts(f, line, flags, 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) {
|
||||
|
||||
Reference in New Issue
Block a user