summaryrefslogtreecommitdiff
path: root/c_src/fsynchelper.c
diff options
context:
space:
mode:
Diffstat (limited to 'c_src/fsynchelper.c')
-rw-r--r--c_src/fsynchelper.c64
1 files changed, 64 insertions, 0 deletions
diff --git a/c_src/fsynchelper.c b/c_src/fsynchelper.c
new file mode 100644
index 0000000..e6a04be
--- /dev/null
+++ b/c_src/fsynchelper.c
@@ -0,0 +1,64 @@
+/*
+ * Copyright (c) 2014 Kungliga Tekniska Högskolan
+ * (KTH Royal Institute of Technology, Stockholm, Sweden).
+ */
+
+#include <stdio.h>
+#include <fcntl.h>
+#include <unistd.h>
+#include <errno.h>
+
+#include <sys/time.h>
+#include <sys/select.h>
+
+#include "erlport.h"
+
+static int
+dosync(int fd)
+{
+#ifdef F_FULLFSYNC
+ int ret = fcntl(fd, F_FULLFSYNC);
+#else
+ int ret = fsync(fd);
+#endif
+ return ret;
+}
+
+int
+main()
+{
+ char buf[100];
+ ssize_t len;
+
+ /* XXX: exits when command size is 0 */
+
+ while ((len = read_command(buf, sizeof(buf)-1)) > 0) {
+ buf[len] = '\0';
+ while (1) {
+ int fd;
+
+ fd = open(buf, O_RDONLY);
+ if (fd == -1) {
+ /* XXX: better errors */
+ write_status("openerror");
+ break;
+ }
+
+ if (dosync(fd) == 0) {
+ write_status("ok");
+ } else if (errno == EBADF) {
+ write_status("ebadf");
+ } else if (errno == EINTR) {
+ close(fd);
+ continue;
+ } else {
+ write_status("fsyncerror");
+ }
+
+ close(fd);
+ break;
+ }
+ }
+
+ return 0;
+}