From: Lennart Poettering Date: Fri, 22 Sep 2017 18:59:39 +0000 (+0200) Subject: fileio: move fsync() logic into write_string_stream_ts() X-Git-Url: http://www.chiark.greenend.org.uk/ucgi/~ianmdlvl/git?a=commitdiff_plain;h=f030d15c1e3b2279c7bcd71b817cdbcf56ca74de;p=elogind.git fileio: move fsync() logic into write_string_stream_ts() That way, write_string_stream_ts() becomes more powerful, and we can remove duplicate code from write_string_file_atomic() and write_string_file_ts(). --- diff --git a/src/basic/fileio.c b/src/basic/fileio.c index 404d79d18..c2f0c7b7a 100644 --- a/src/basic/fileio.c +++ b/src/basic/fileio.c @@ -71,7 +71,10 @@ int write_string_stream_ts( return -errno; } - return fflush_and_check(f); + if (flags & WRITE_STRING_FILE_SYNC) + return fflush_sync_and_check(f); + else + return fflush_and_check(f); } static int write_string_file_atomic( @@ -94,16 +97,18 @@ static int write_string_file_atomic( (void) fchmod_umask(fileno(f), 0644); r = write_string_stream_ts(f, line, flags, ts); - if (r >= 0 && (flags & WRITE_STRING_FILE_SYNC)) - r = fflush_sync_and_check(f); - if (r >= 0) { - if (rename(p, fn) < 0) - r = -errno; + if (r < 0) + goto fail; + + if (rename(p, fn) < 0) { + r = -errno; + goto fail; } - if (r < 0) - (void) unlink(p); + return 0; +fail: + (void) unlink(p); return r; } @@ -162,12 +167,6 @@ int write_string_file_ts( if (r < 0) goto fail; - if (flags & WRITE_STRING_FILE_SYNC) { - r = fflush_sync_and_check(f); - if (r < 0) - return r; - } - return 0; fail: