summaryrefslogtreecommitdiff
path: root/src/basic/fileio.c
diff options
context:
space:
mode:
authorLennart Poettering <lennart@poettering.net>2017-12-30 15:42:03 +0100
committerSven Eden <yamakuzure@gmx.net>2018-05-30 07:49:53 +0200
commit6eeef058d5cb544c567ebc53b8e02d67d33080c9 (patch)
tree814b67a1b6b40f13b2ba7c49940ac6a4aadae746 /src/basic/fileio.c
parentb3c9c3b4ea5bcd877602aa66884809e0ab1a7d76 (diff)
fileio: tweak write_string_stream_ts() to write out trailing \n in one go even if buffering is off
This tweaks write_string_stream_ts() in one minor way: when stdio buffering has been turned off, let's append the newline we shall append to the buffer we write ourselves so that the kernel only gets one syscall for the result. When buffering is enabled stdio will take care of that anyway. Follow-up for #7750.
Diffstat (limited to 'src/basic/fileio.c')
-rw-r--r--src/basic/fileio.c14
1 files changed, 13 insertions, 1 deletions
diff --git a/src/basic/fileio.c b/src/basic/fileio.c
index 86ae97801..8b00fc817 100644
--- a/src/basic/fileio.c
+++ b/src/basic/fileio.c
@@ -62,16 +62,28 @@ int write_string_stream_ts(
WriteStringFileFlags flags,
struct timespec *ts) {
+ bool needs_nl;
+
assert(f);
assert(line);
if (ferror(f))
return -EIO;
+ needs_nl = !(flags & WRITE_STRING_FILE_AVOID_NEWLINE) && !endswith(line, "\n");
+
+ if (needs_nl && (flags & WRITE_STRING_FILE_DISABLE_BUFFER)) {
+ /* If STDIO buffering was disabled, then let's append the newline character to the string itself, so
+ * that the write goes out in one go, instead of two */
+
+ line = strjoina(line, "\n");
+ needs_nl = false;
+ }
+
if (fputs(line, f) == EOF)
return -errno;
- if (!(flags & WRITE_STRING_FILE_AVOID_NEWLINE) && !endswith(line, "\n"))
+ if (needs_nl)
if (fputc('\n', f) == EOF)
return -errno;