(do_test): Make sure noatime flag isn't set for filesystem.
[glibc.git] / libio / tst-atime.c
blob488963210e995b06c516b1bab817f14f527dc0d0
1 #include <fcntl.h>
2 #include <stdio.h>
3 #include <stdlib.h>
4 #include <string.h>
5 #include <unistd.h>
6 #include <sys/stat.h>
7 #include <sys/statvfs.h>
10 static int do_test (void);
11 #define TEST_FUNCTION do_test ()
12 #define TIMEOUT 5
13 #include <test-skeleton.c>
16 static int
17 do_test (void)
19 char *buf;
20 int fd;
21 FILE *fp;
22 int ch;
23 struct stat st1;
24 struct stat st2;
25 struct statvfs sv;
26 int e;
28 buf = (char *) malloc (strlen (test_dir) + sizeof "/tst-atime.XXXXXX");
29 if (buf == NULL)
31 printf ("cannot allocate memory: %m\n");
32 return 1;
34 stpcpy (stpcpy (buf, test_dir), "/tst-atime.XXXXXX");
36 fd = mkstemp (buf);
37 if (fd == -1)
39 printf ("cannot open temporary file: %m\n");
40 return 1;
43 /* Make sure the filesystem doesn't have the noatime option set. If
44 statvfs is not available just continue. */
45 e = fstatvfs (fd, &sv);
46 if (e != ENOSYS)
48 if (e != 0)
50 printf ("cannot statvfs '%s': %m\n", buf);
51 return 1;
54 if ((sv.f_flag & ST_NOATIME) != 0)
56 puts ("Bah! The filesystem is mounted with noatime");
57 return 0;
61 /* Make sure it gets removed. */
62 add_temp_file (buf);
64 if (write (fd, "some string\n", 12) != 12)
66 printf ("cannot write temporary file: %m\n");
67 return 1;
70 if (lseek (fd, 0, SEEK_SET) == (off_t) -1)
72 printf ("cannot reposition temporary file: %m\n");
73 return 1;
76 fp = fdopen (fd, "r");
77 if (fp == NULL)
79 printf ("cannot create stream: %m\n");
80 return 1;
83 if (fstat (fd, &st1) == -1)
85 printf ("first stat failed: %m\n");
86 return 1;
89 sleep (2);
91 ch = fgetc (fp);
92 if (ch != 's')
94 printf ("did not read correct character: got '%c', expected 's'\n", ch);
95 return 1;
98 if (fstat (fd, &st2) == -1)
100 printf ("second stat failed: %m\n");
101 return 1;
104 if (st1.st_atime > st2.st_atime)
106 puts ("second atime smaller");
107 return 1;
109 else if (st1.st_atime == st2.st_atime)
111 puts ("atime has not changed");
112 return 1;
115 fclose (fp);
117 return 0;