summaryrefslogtreecommitdiffstats
path: root/tests/bugs/readdir-ahead/bug-1390050.c
blob: 5593a1d4c0cc1a652fe48b71f1d53d90d8e077b1 (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
65
66
67
68
69
70
#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include <fcntl.h>
#include <dirent.h>
#include <string.h>
#include <errno.h>

int
main (int argc, char *argv[])
{
        const char  *glfs_dir          = NULL, *filepath = NULL;
        DIR         *dirfd             = NULL;
        int          filefd            = 0, ret = 0;
        struct stat  stbuf             = {0, };
        size_t       size_before_write = 0;

        glfs_dir = argv[1];
        filepath = argv[2];
        dirfd = opendir (glfs_dir);
        if (dirfd == NULL) {
                fprintf (stderr, "opening directory failed (%s)\n",
                         strerror (errno));
                goto err;
        }

        filefd = open (filepath, O_RDWR);
        if (filefd < 0) {
                fprintf (stderr, "open failed on path %s (%s)\n", filepath,
                         strerror (errno));
                goto err;
        }

        ret = stat (filepath, &stbuf);
        if (ret < 0) {
                fprintf (stderr, "stat failed on path %s (%s)\n", filepath,
                         strerror (errno));
                goto err;
        }

        size_before_write = stbuf.st_size;

        ret = write (filefd, "testdata", strlen ("testdata123") + 1);
        if (ret <= 0) {
                fprintf (stderr, "write failed (%s)\n", strerror (errno));
                goto err;
        }

        while (readdir (dirfd)) {
                /* do nothing */
        }

        ret = stat (filepath, &stbuf);
        if (ret < 0) {
                fprintf (stderr, "stat failed on path %s (%s)\n",
                         strerror (errno));
                goto err;
        }

        if (stbuf.st_size == size_before_write) {
                fprintf (stderr, "file size (%lu) has not changed even after "
                         "its written to\n", stbuf.st_size);
                goto err;
        }

        return 0;
err:
        return -1;
}