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:
parent
2eabcc772b
commit
b183713383
|
@ -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;
|
||||
|
||||
|
|
|
@ -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) {
|
||||
|
|
|
@ -66,7 +66,7 @@ static int write_state(FILE **f, char **states) {
|
|||
STRV_FOREACH(state, states) {
|
||||
int k;
|
||||
|
||||
k = write_string_stream(*f, *state, true);
|
||||
k = write_string_stream(*f, *state, 0);
|
||||
if (k == 0)
|
||||
return 0;
|
||||
log_debug_errno(k, "Failed to write '%s' to /sys/power/state: %m",
|
||||
|
|
|
@ -66,7 +66,7 @@ static void test_clock_is_localtime(void) {
|
|||
log_info("%s", scenarios[i].contents);
|
||||
rewind(f);
|
||||
ftruncate(fd, 0);
|
||||
assert_se(write_string_stream(f, scenarios[i].contents, false) == 0);
|
||||
assert_se(write_string_stream(f, scenarios[i].contents, WRITE_STRING_FILE_AVOID_NEWLINE) == 0);
|
||||
assert_se(clock_is_localtime(adjtime) == scenarios[i].expected_result);
|
||||
}
|
||||
|
||||
|
|
|
@ -233,7 +233,7 @@ static void test_merge_env_file(void) {
|
|||
"zzz=${one:+replacement}\n"
|
||||
"zzzz=${foobar:-${nothing}}\n"
|
||||
"zzzzz=${nothing:+${nothing}}\n"
|
||||
, false);
|
||||
, WRITE_STRING_FILE_AVOID_NEWLINE);
|
||||
assert(r >= 0);
|
||||
|
||||
r = merge_env_file(&a, NULL, t);
|
||||
|
@ -302,7 +302,7 @@ static void test_merge_env_file_invalid(void) {
|
|||
";comment2=comment2\n"
|
||||
"#\n"
|
||||
"\n\n" /* empty line */
|
||||
, false);
|
||||
, WRITE_STRING_FILE_AVOID_NEWLINE);
|
||||
assert(r >= 0);
|
||||
|
||||
r = merge_env_file(&a, NULL, t);
|
||||
|
@ -414,12 +414,12 @@ static void test_write_string_stream(void) {
|
|||
|
||||
f = fdopen(fd, "r");
|
||||
assert_se(f);
|
||||
assert_se(write_string_stream(f, "boohoo", true) < 0);
|
||||
assert_se(write_string_stream(f, "boohoo", 0) < 0);
|
||||
|
||||
f = freopen(fn, "r+", f);
|
||||
assert_se(f);
|
||||
|
||||
assert_se(write_string_stream(f, "boohoo", true) == 0);
|
||||
assert_se(write_string_stream(f, "boohoo", 0) == 0);
|
||||
rewind(f);
|
||||
|
||||
assert_se(fgets(buf, sizeof(buf), f));
|
||||
|
@ -428,7 +428,7 @@ static void test_write_string_stream(void) {
|
|||
f = freopen(fn, "w+", f);
|
||||
assert_se(f);
|
||||
|
||||
assert_se(write_string_stream(f, "boohoo", false) == 0);
|
||||
assert_se(write_string_stream(f, "boohoo", WRITE_STRING_FILE_AVOID_NEWLINE) == 0);
|
||||
rewind(f);
|
||||
|
||||
assert_se(fgets(buf, sizeof(buf), f));
|
||||
|
|
Loading…
Reference in a new issue