summaryrefslogtreecommitdiff
path: root/src/basic/fileio.c
diff options
context:
space:
mode:
authorZbigniew Jędrzejewski-Szmek <zbyszek@in.waw.pl>2017-10-03 10:41:51 +0200
committerSven Eden <yamakuzure@gmx.net>2017-11-23 08:11:42 +0100
commit3344b839df8fe2dd2b4a4f470225e4c407cf2faa (patch)
treea64c86cfe6adc5718143f5604fef893cfd94a865 /src/basic/fileio.c
parent7d729de3a70bc7a7fef69f5cea03fcbf835e118a (diff)
build-sys: use #if Y instead of #ifdef Y everywhere
The advantage is that is the name is mispellt, cpp will warn us. $ git grep -Ee "conf.set\('(HAVE|ENABLE)_" -l|xargs sed -r -i "s/conf.set\('(HAVE|ENABLE)_/conf.set10('\1_/" $ git grep -Ee '#ifn?def (HAVE|ENABLE)' -l|xargs sed -r -i 's/#ifdef (HAVE|ENABLE)/#if \1/; s/#ifndef (HAVE|ENABLE)/#if ! \1/;' $ git grep -Ee 'if.*defined\(HAVE' -l|xargs sed -i -r 's/defined\((HAVE_[A-Z0-9_]*)\)/\1/g' $ git grep -Ee 'if.*defined\(ENABLE' -l|xargs sed -i -r 's/defined\((ENABLE_[A-Z0-9_]*)\)/\1/g' + manual changes to meson.build squash! build-sys: use #if Y instead of #ifdef Y everywhere v2: - fix incorrect setting of HAVE_LIBIDN2
Diffstat (limited to 'src/basic/fileio.c')
-rw-r--r--src/basic/fileio.c66
1 files changed, 39 insertions, 27 deletions
diff --git a/src/basic/fileio.c b/src/basic/fileio.c
index 3990d4dce..46b8b4a69 100644
--- a/src/basic/fileio.c
+++ b/src/basic/fileio.c
@@ -53,13 +53,17 @@
#define READ_FULL_BYTES_MAX (4U*1024U*1024U)
-int write_string_stream_ts(FILE *f, const char *line, bool enforce_newline, struct timespec *ts) {
+int write_string_stream_ts(
+ FILE *f,
+ const char *line,
+ WriteStringFileFlags flags,
+ struct timespec *ts) {
assert(f);
assert(line);
fputs(line, f);
- if (enforce_newline && !endswith(line, "\n"))
+ if (!(flags & WRITE_STRING_FILE_AVOID_NEWLINE) && !endswith(line, "\n"))
fputc('\n', f);
if (ts) {
@@ -69,10 +73,18 @@ int write_string_stream_ts(FILE *f, const char *line, bool enforce_newline, stru
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(const char *fn, const char *line, bool enforce_newline, bool do_fsync) {
+static int write_string_file_atomic(
+ const char *fn,
+ const char *line,
+ WriteStringFileFlags flags,
+ struct timespec *ts) {
+
_cleanup_fclose_ FILE *f = NULL;
_cleanup_free_ char *p = NULL;
int r;
@@ -86,22 +98,28 @@ static int write_string_file_atomic(const char *fn, const char *line, bool enfor
(void) fchmod_umask(fileno(f), 0644);
- r = write_string_stream(f, line, enforce_newline);
- if (r >= 0 && do_fsync)
- r = fflush_sync_and_check(f);
+ r = write_string_stream_ts(f, line, flags, ts);
+ if (r < 0)
+ goto fail;
- if (r >= 0) {
- if (rename(p, fn) < 0)
- r = -errno;
+ if (rename(p, fn) < 0) {
+ r = -errno;
+ goto fail;
}
- if (r < 0)
- (void) unlink(p);
+ return 0;
+fail:
+ (void) unlink(p);
return r;
}
-int write_string_file_ts(const char *fn, const char *line, WriteStringFileFlags flags, struct timespec *ts) {
+int write_string_file_ts(
+ const char *fn,
+ const char *line,
+ WriteStringFileFlags flags,
+ struct timespec *ts) {
+
_cleanup_fclose_ FILE *f = NULL;
int q, r;
@@ -114,8 +132,7 @@ int write_string_file_ts(const char *fn, const char *line, WriteStringFileFlags
if (flags & WRITE_STRING_FILE_ATOMIC) {
assert(flags & WRITE_STRING_FILE_CREATE);
- r = write_string_file_atomic(fn, line, !(flags & WRITE_STRING_FILE_AVOID_NEWLINE),
- flags & WRITE_STRING_FILE_SYNC);
+ r = write_string_file_atomic(fn, line, flags, ts);
if (r < 0)
goto fail;
@@ -148,16 +165,10 @@ int write_string_file_ts(const char *fn, const char *line, WriteStringFileFlags
}
}
- r = write_string_stream_ts(f, line, !(flags & WRITE_STRING_FILE_AVOID_NEWLINE), ts);
+ r = write_string_stream_ts(f, line, flags, 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:
@@ -247,11 +258,11 @@ int read_full_stream(FILE *f, char **contents, size_t *size) {
if (st.st_size > READ_FULL_BYTES_MAX)
return -E2BIG;
- /* Start with the right file size, but be prepared for
- * files from /proc which generally report a file size
- * of 0 */
+ /* Start with the right file size, but be prepared for files from /proc which generally report a file
+ * size of 0. Note that we increase the size to read here by one, so that the first read attempt
+ * already makes us notice the EOF. */
if (st.st_size > 0)
- n = st.st_size;
+ n = st.st_size + 1;
}
l = 0;
@@ -264,12 +275,13 @@ int read_full_stream(FILE *f, char **contents, size_t *size) {
return -ENOMEM;
buf = t;
+ errno = 0;
k = fread(buf + l, 1, n - l, f);
if (k > 0)
l += k;
if (ferror(f))
- return -errno;
+ return errno > 0 ? -errno : -EIO;
if (feof(f))
break;