From f2997962ff8aeea577bed878d3bc4e4f64784e45 Mon Sep 17 00:00:00 2001 From: Lennart Poettering Date: Thu, 6 Nov 2014 21:21:39 +0100 Subject: [PATCH] fileio: simplify write_string_file_atomic() by reusing write_string_stream() --- src/shared/fileio.c | 16 +++------------- 1 file changed, 3 insertions(+), 13 deletions(-) diff --git a/src/shared/fileio.c b/src/shared/fileio.c index f4efc4c9f..ff6b1a7ed 100644 --- a/src/shared/fileio.c +++ b/src/shared/fileio.c @@ -66,7 +66,7 @@ int write_string_file_no_create(const char *fn, const char *line) { assert(line); /* We manually build our own version of fopen(..., "we") that - * without O_CREAT */ + * works without O_CREAT */ fd = open(fn, O_WRONLY|O_CLOEXEC|O_NOCTTY); if (fd < 0) return -errno; @@ -94,20 +94,10 @@ int write_string_file_atomic(const char *fn, const char *line) { fchmod_umask(fileno(f), 0644); - errno = 0; - fputs(line, f); - if (!endswith(line, "\n")) - fputc('\n', f); - - fflush(f); - - if (ferror(f)) - r = errno ? -errno : -EIO; - else { + r = write_string_stream(f, line); + if (r >= 0) { if (rename(p, fn) < 0) r = -errno; - else - r = 0; } if (r < 0) -- 2.30.2