chiark / gitweb /
fileio: tweak write_string_stream_ts() to write out trailing \n in one go even if...
authorLennart Poettering <lennart@poettering.net>
Sat, 30 Dec 2017 14:42:03 +0000 (15:42 +0100)
committerSven Eden <yamakuzure@gmx.net>
Wed, 30 May 2018 05:49:53 +0000 (07:49 +0200)
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.

src/basic/fileio.c

index 86ae9780101a0cd5db3e8cd8924c9668bddbe505..8b00fc817b7a45fd017413b819d72cbfbed241e3 100644 (file)
@@ -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;