From 736937e5aab60888f0bd2ca75674105f7b7fd0f0 Mon Sep 17 00:00:00 2001 From: Lennart Poettering Date: Mon, 7 Jul 2014 12:05:41 +0200 Subject: [PATCH] fileio: simplify write_env_file() --- src/shared/fileio.c | 24 ++++++++++-------------- 1 file changed, 10 insertions(+), 14 deletions(-) diff --git a/src/shared/fileio.c b/src/shared/fileio.c index b1de5908c5..b0ab780d77 100644 --- a/src/shared/fileio.c +++ b/src/shared/fileio.c @@ -756,35 +756,31 @@ static void write_env_var(FILE *f, const char *v) { } int write_env_file(const char *fname, char **l) { - char **i; - _cleanup_free_ char *p = NULL; _cleanup_fclose_ FILE *f = NULL; + _cleanup_free_ char *p = NULL; + char **i; int r; + assert(fname); + r = fopen_temporary(fname, &f, &p); if (r < 0) return r; fchmod_umask(fileno(f), 0644); - errno = 0; STRV_FOREACH(i, l) write_env_var(f, *i); - fflush(f); + r = fflush_and_check(f); + if (r >= 0) { + if (rename(p, fname) >= 0) + return 0; - if (ferror(f)) - r = errno ? -errno : -EIO; - else { - if (rename(p, fname) < 0) - r = -errno; - else - r = 0; + r = -errno; } - if (r < 0) - unlink(p); - + unlink(p); return r; }