]> git.ipfire.org Git - thirdparty/glibc.git/blame - dirent/tst-fdopendir.c
hurd: writev: Get rid of alloca
[thirdparty/glibc.git] / dirent / tst-fdopendir.c
CommitLineData
1812d50b
UD
1#include <stdio.h>
2#include <fcntl.h>
3#include <stdlib.h>
4#include <unistd.h>
5#include <dirent.h>
6#include <stdbool.h>
7#include <string.h>
ceaa9889 8#include <sys/stat.h>
1812d50b 9
026a84a5
FB
10#include <support/xunistd.h>
11
56606ab3
RM
12#ifndef O_NOATIME
13# define O_NOATIME 0
14#endif
1812d50b
UD
15
16static int
17do_test (void)
18{
19 char fname[] = "/tmp/jXXXXXX";
20 int fd = mkstemp (fname);
21 if (fd == -1)
22 {
23 puts ("mkstemp failed");
24 return 1;
25 }
26
026a84a5 27 xwrite (fd, "hello", 5);
1812d50b
UD
28 close (fd);
29
30 struct stat64 st;
31 if (stat64 (fname, &st) == -1)
32 {
33 puts ("first stat failed");
34 return 0;
35 }
36
37 /* Make sure there is enough time between the creation and the access. */
38 sleep (2);
39
40 fd = open (fname, O_RDONLY | O_NOATIME);
41 if (fd == -1)
42 {
43 puts ("first open failed");
44 return 1;
45 }
46
47 char buf[5];
48 read(fd, buf, sizeof (buf));
49 close(fd);
50
51 struct stat64 st2;
52 if (stat64 (fname, &st2) == -1)
53 {
54 puts ("second stat failed");
55 return 0;
56 }
57
58 bool no_noatime = false;
59#ifdef _STATBUF_ST_NSEC
60 if (st.st_atim.tv_sec != st2.st_atim.tv_sec
61 || st.st_atim.tv_nsec != st2.st_atim.tv_nsec)
62#else
63 if (st.st_atime != st2.st_atime)
64#endif
65 {
66 puts ("file atime changed");
67 no_noatime = true;
68 }
69
70 unlink(fname);
71
72 strcpy(fname, "/tmp/dXXXXXX");
73 char *d = mkdtemp (fname);
74 if (d == NULL)
75 {
76 puts ("mkdtemp failed");
77 return 1;
78 }
79
80 if (stat64 (d, &st) == -1)
81 {
82 puts ("third stat failed");
83 return 0;
84 }
85 sleep (2);
86
87 fd = open64 (d, O_RDONLY|O_NDELAY|O_DIRECTORY|O_NOATIME);
88 if (fd == -1)
89 {
90 puts ("second open failed");
91 return 1;
92 }
93 DIR *dir = fdopendir (fd);
94 if (dir == NULL)
95 {
96 puts ("fdopendir failed");
97 return 1;
98 }
99
100 struct dirent *de;
101 while ((de = readdir (dir)) != NULL)
102 ;
103
104 closedir (dir);
105
106 if (stat64 (d, &st2) == -1)
107 {
108 puts ("fourth stat failed");
109 return 0;
110 }
111#ifdef _STATBUF_ST_NSEC
112 if (!no_noatime
113 && (st.st_atim.tv_sec != st2.st_atim.tv_sec
114 || st.st_atim.tv_nsec != st2.st_atim.tv_nsec))
115#else
116 if (!no_noatime && st.st_atime != st2.st_atime)
117#endif
118 {
119 puts ("directory atime changed");
120 return 1;
121 }
122
123 rmdir(fname);
124
125 return 0;
126}
127
1812d50b
UD
128#define TEST_FUNCTION do_test ()
129#include "../test-skeleton.c"