summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorLennart Poettering <lennart@poettering.net>2014-07-07 12:05:41 +0200
committerLennart Poettering <lennart@poettering.net>2014-07-07 15:25:55 +0200
commit736937e5aab60888f0bd2ca75674105f7b7fd0f0 (patch)
tree01b4c7789b1d8c7b8dad17f9635de0c1798a158b
parent6294aa76d818e831de4592b41a37e225fd0871f9 (diff)
fileio: simplify write_env_file()
-rw-r--r--src/shared/fileio.c24
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;
}