Whamcloud - gitweb
merge b_devel into HEAD (20030703)
[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         before_mknod = time(0);
34         rc = mknod(filename, 0700, S_IFREG);
35         after_mknod = time(0);
36         if (rc && errno != EEXIST) {
37                 fprintf(stderr, "%s: mknod(%s) failed: rc %d: %s\n",
38                         prog, filename, errno, strerror(errno));
39                 return 2;
40         } else if (!rc) {
41                 rc = stat(filename, &st);
42                 if (rc) {
43                         fprintf(stderr, "%s: stat(%s) failed: rc %d: %s\n",
44                                 prog, filename, errno, strerror(errno));
45                         return 3;
46                 }
47
48                 if (st.st_mtime < before_mknod || st.st_mtime > after_mknod) {
49                         fprintf(stderr,
50                                 "%s: bad mknod times %lu <= %lu <= %lu false\n",
51                                 prog, before_mknod, st.st_mtime, after_mknod);
52                         return 4;
53                 }
54
55                 printf("%s: good mknod times %lu <= %lu <= %lu\n",
56                        prog, before_mknod, st.st_mtime, after_mknod);
57
58                 sleep(5);
59         }
60
61         before_utime = time(0);
62         rc = utime(filename, NULL);
63         after_utime = time(0);
64         if (rc) {
65                 fprintf(stderr, "%s: utime(%s) failed: rc %d: %s\n",
66                         prog, filename, errno, strerror(errno));
67                 return 5;
68         }
69
70         rc = stat(filename, &st);
71         if (rc) {
72                 fprintf(stderr, "%s: second stat(%s) failed: rc %d: %s\n",
73                         prog, filename, errno, strerror(errno));
74                 return 6;
75         }
76
77         if (st.st_mtime < before_utime || st.st_mtime > after_utime) {
78                 fprintf(stderr, "%s: bad utime times %lu <= %lu <= %lu false\n",
79                         prog, before_utime, st.st_mtime, after_utime);
80                 return 7;
81         }
82
83         printf("%s: good utime times %lu <= %lu <= %lu\n",
84                prog, before_utime, st.st_mtime, after_utime);
85
86         return 0;
87 }