Whamcloud - gitweb
merge b_devel into HEAD, which will become 0.7.3
[fs/lustre-release.git] / lustre / tests / utime.c
1 /*
2  * Simple test for validating mtime on a file create and set via utime.
3  */
4 #include <stdio.h>
5 #include <stdlib.h>
6 #include <sys/types.h>
7 #include <sys/stat.h>
8 #include <fcntl.h>
9 #include <unistd.h>
10 #include <time.h>
11 #include <string.h>
12 #include <utime.h>
13 #include <errno.h>
14
15 void usage(char *prog)
16 {
17         fprintf(stderr, "usage: %s <filename>\n", prog);
18         exit(1);
19 }
20
21 int main(int argc, char *argv[])
22 {
23         long before_mknod, after_mknod;
24         long before_utime, after_utime;
25         const char *prog = argv[0];
26         const char *filename = argv[1];
27         struct stat st;
28         int rc;
29
30         if (argc != 2)
31                 usage(argv[0]);
32
33         /* Adjust the before time back one second, because the kernel's
34          * CURRENT_TIME (lockless clock reading, used to set inode times)
35          * may drift against the do_gettimeofday() time (TSC-corrected and
36          * locked clock reading, used to return timestamps to user space).
37          * This means that the mknod time could be a second older than the
38          * before time, even for a local filesystem such as ext3.
39          */
40         before_mknod = time(0) - 1;
41         rc = mknod(filename, 0700, S_IFREG);
42         after_mknod = time(0);
43         if (rc && errno != EEXIST) {
44                 fprintf(stderr, "%s: mknod(%s) failed: rc %d: %s\n",
45                         prog, filename, errno, strerror(errno));
46                 return 2;
47         } else if (!rc) {
48                 rc = stat(filename, &st);
49                 if (rc) {
50                         fprintf(stderr, "%s: stat(%s) failed: rc %d: %s\n",
51                                 prog, filename, errno, strerror(errno));
52                         return 3;
53                 }
54
55                 if (st.st_mtime < before_mknod || st.st_mtime > after_mknod) {
56                         fprintf(stderr,
57                                 "%s: bad mknod times %lu <= %lu <= %lu false\n",
58                                 prog, before_mknod, st.st_mtime, after_mknod);
59                         return 4;
60                 }
61
62                 printf("%s: good mknod times %lu%s <= %lu <= %lu\n",
63                        prog, before_mknod, before_mknod == st.st_mtime ? "*":"",
64                        st.st_mtime, after_mknod);
65
66                 sleep(5);
67         }
68
69         /* See above */
70         before_utime = time(0) - 1;
71         rc = utime(filename, NULL);
72         after_utime = time(0);
73         if (rc) {
74                 fprintf(stderr, "%s: utime(%s) failed: rc %d: %s\n",
75                         prog, filename, errno, strerror(errno));
76                 return 5;
77         }
78
79         rc = stat(filename, &st);
80         if (rc) {
81                 fprintf(stderr, "%s: second stat(%s) failed: rc %d: %s\n",
82                         prog, filename, errno, strerror(errno));
83                 return 6;
84         }
85
86         if (st.st_mtime < before_utime || st.st_mtime > after_utime) {
87                 fprintf(stderr, "%s: bad utime times %lu <= %lu <= %lu false\n",
88                         prog, before_utime, st.st_mtime, after_utime);
89                 return 7;
90         }
91
92         printf("%s: good utime times %lu%s <= %lu <= %lu\n",
93                prog, before_utime, before_utime == st.st_mtime ? "*" : "",
94                st.st_mtime, after_utime);
95
96         return 0;
97 }