summaryrefslogtreecommitdiff
path: root/c_src/fsynchelper.c
blob: c4035d89e2189ba99d5ab57ab6441394af7e85c6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
/*
 * Copyright (c) 2014-2015, NORDUnet A/S.
 * See LICENSE for licensing information.
 */

#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[1000];
    ssize_t len;

    /* XXX: exits when command size is 0 */

    while ((len = read_command((unsigned char *)buf, sizeof(buf)-1, 2)) > 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;
}