summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorLennart Poettering <lennart@poettering.net>2017-09-22 20:55:34 +0200
committerSven Eden <yamakuzure@gmx.net>2017-09-22 20:55:34 +0200
commit42d58088504bf7fef99f427bf863263c562ec4d3 (patch)
tree86c3ad7fce5503f42caf3ae5c60a0d0d5b0c3593 /src
parent0d1efe6ec9b637291d4356925b582f743fba4c78 (diff)
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.
Diffstat (limited to 'src')
-rw-r--r--src/basic/fileio.c29
-rw-r--r--src/basic/fileio.h6
-rw-r--r--src/sleep/sleep.c2
3 files changed, 21 insertions, 16 deletions
diff --git a/src/basic/fileio.c b/src/basic/fileio.c
index e3f3b7fc3..404d79d18 100644
--- a/src/basic/fileio.c
+++ b/src/basic/fileio.c
@@ -51,13 +51,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) {
@@ -73,8 +77,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;
@@ -90,8 +93,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)
@@ -104,7 +107,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;
@@ -117,10 +125,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;
@@ -153,7 +158,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;
diff --git a/src/basic/fileio.h b/src/basic/fileio.h
index 2ff3b434d..5b3d045e0 100644
--- a/src/basic/fileio.h
+++ b/src/basic/fileio.h
@@ -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) {
diff --git a/src/sleep/sleep.c b/src/sleep/sleep.c
index 01fa22349..c6dd13197 100644
--- a/src/sleep/sleep.c
+++ b/src/sleep/sleep.c
@@ -69,7 +69,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",