Whamcloud - gitweb
Revert my use of the configure_flags macro in the lustre.spec file and just
[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         const char *prog = argv[0];
25         const char *filename = argv[1];
26         struct utimbuf utb;
27         struct stat st;
28         int rc;
29
30         utb.actime = 0x47114711;
31         utb.modtime = 0x11471147;
32                 
33
34         if (argc != 2)
35                 usage(argv[0]);
36
37         /* Adjust the before time back one second, because the kernel's
38          * CURRENT_TIME (lockless clock reading, used to set inode times)
39          * may drift against the do_gettimeofday() time (TSC-corrected and
40          * locked clock reading, used to return timestamps to user space).
41          * This means that the mknod time could be a second older than the
42          * before time, even for a local filesystem such as ext3.
43          */
44         before_mknod = time(0) - 1;
45         rc = mknod(filename, 0700, S_IFREG);
46         after_mknod = time(0);
47         if (rc && errno != EEXIST) {
48                 fprintf(stderr, "%s: mknod(%s) failed: rc %d: %s\n",
49                         prog, filename, errno, strerror(errno));
50                 return 2;
51         } else if (!rc) {
52                 rc = stat(filename, &st);
53                 if (rc) {
54                         fprintf(stderr, "%s: stat(%s) failed: rc %d: %s\n",
55                                 prog, filename, errno, strerror(errno));
56                         return 3;
57                 }
58
59                 if (st.st_mtime < before_mknod || st.st_mtime > after_mknod) {
60                         fprintf(stderr,
61                                 "%s: bad mknod times %lu <= %lu <= %lu false\n",
62                                 prog, before_mknod, st.st_mtime, after_mknod);
63                         return 4;
64                 }
65
66                 printf("%s: good mknod times %lu%s <= %lu <= %lu\n",
67                        prog, before_mknod, before_mknod == st.st_mtime ? "*":"",
68                        st.st_mtime, after_mknod);
69
70         }
71
72         /* See above */
73         rc = utime(filename, &utb);
74         if (rc) {
75                 fprintf(stderr, "%s: utime(%s) failed: rc %d: %s\n",
76                         prog, filename, errno, strerror(errno));
77                 return 5;
78         }
79
80         rc = stat(filename, &st);
81         if (rc) {
82                 fprintf(stderr, "%s: second stat(%s) failed: rc %d: %s\n",
83                         prog, filename, errno, strerror(errno));
84                 return 6;
85         }
86
87         if (st.st_mtime != utb.modtime ) {
88                 fprintf(stderr, "%s: bad utime mtime %lu should be  %lu\n",
89                         prog, st.st_mtime, utb.modtime);
90                 return 7;
91         }
92
93         if (st.st_atime != utb.actime ) {
94                 fprintf(stderr, "%s: bad utime mtime %lu should be  %lu\n",
95                         prog, st.st_atime, utb.actime);
96                 return 7;
97         }
98
99         printf("%s: good utime mtimes %lu, atime %lu\n",
100                prog, utb.modtime, utb.actime);
101
102         return 0;
103 }