summaryrefslogtreecommitdiffstats
path: root/include/closestream.h
diff options
context:
space:
mode:
authorSami Kerola2013-04-13 21:54:44 +0200
committerKarel Zak2013-04-26 13:26:06 +0200
commitf416563b284a9656ed012f6e078d3498b3403781 (patch)
tree585728ba935d3d823f645c39aac505a222919651 /include/closestream.h
parentlibblkid: check writing to a file was successful (diff)
downloadkernel-qcow2-util-linux-f416563b284a9656ed012f6e078d3498b3403781.tar.gz
kernel-qcow2-util-linux-f416563b284a9656ed012f6e078d3498b3403781.tar.xz
kernel-qcow2-util-linux-f416563b284a9656ed012f6e078d3498b3403781.zip
include: add close_fd() for noticing write errors before close()
Essentially this helper function is similar to close_stream(), but for file descriptors. When a file descriptors are close()'d status of write is often overlooked. The close_fd() will try to determine what happen to writes with fsync() before closing the file descriptor. Signed-off-by: Sami Kerola <kerolasa@iki.fi>
Diffstat (limited to 'include/closestream.h')
-rw-r--r--include/closestream.h19
1 files changed, 19 insertions, 0 deletions
diff --git a/include/closestream.h b/include/closestream.h
index d61b83b5e..2535c8b22 100644
--- a/include/closestream.h
+++ b/include/closestream.h
@@ -48,4 +48,23 @@ close_stdout(void)
_exit(EXIT_FAILURE);
}
+#ifndef HAVE_FSYNC
+static inline int
+fsync(int fd __attribute__((__unused__)))
+{
+ return 0;
+}
+#endif
+
+static inline int
+close_fd(int fd)
+{
+ const int fsync_fail = (fsync(fd) != 0);
+ const int close_fail = (close(fd) != 0);
+
+ if (fsync_fail || close_fail)
+ return EOF;
+ return 0;
+}
+
#endif /* UTIL_LINUX_CLOSESTREAM_H */