Fix some bugs when testing opensds ansible
[stor4nfv.git] / src / ceph / qa / workunits / direct_io / test_short_dio_read.c
1 #include <unistd.h>
2 #include <sys/types.h>
3 #include <sys/stat.h>
4 #include <fcntl.h>
5 #include <stdio.h>
6 #include <errno.h>
7 #include <string.h>
8 #include <stdlib.h>
9
10 int main()
11 {
12         char buf[409600];
13         ssize_t r;
14         int err;
15         int fd = open("shortfile", O_WRONLY|O_CREAT, 0644);
16
17         if (fd < 0) {
18                 err = errno;
19                 printf("error: open() failed with: %d (%s)\n", err, strerror(err));
20                 exit(err);
21         }
22
23         printf("writing first 3 bytes of 10k file\n");
24         r = write(fd, "foo", 3);
25         if (r == -1) {
26                 err = errno;
27                 printf("error: write() failed with: %d (%s)\n", err, strerror(err));
28                 close(fd);
29                 exit(err);
30         }
31         r = ftruncate(fd, 10000);
32         if (r == -1) {
33                 err = errno;
34                 printf("error: ftruncate() failed with: %d (%s)\n", err, strerror(err));
35                 close(fd);
36                 exit(err);
37         }
38         
39         fsync(fd);
40         close(fd);
41
42         printf("reading O_DIRECT\n");
43         fd = open("shortfile", O_RDONLY|O_DIRECT);
44         if (fd < 0) {
45                 err = errno;
46                 printf("error: open() failed with: %d (%s)\n", err, strerror(err));
47                 exit(err);
48         }
49
50         r = read(fd, buf, sizeof(buf));
51         close(fd);
52
53         printf("got %d\n", (int)r);
54         if (r != 10000)
55                 return 1;
56         return 0;
57 }