diff options
-rw-r--r-- | src/shared/fileio.c | 24 |
1 files 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; } |