Whamcloud - gitweb
b=16098
[fs/lustre-release.git] / lustre / tests / utime.c
1 /* -*- mode: c; c-basic-offset: 8; indent-tabs-mode: nil; -*-
2  * vim:expandtab:shiftwidth=8:tabstop=8:
3  *
4  * GPL HEADER START
5  *
6  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
7  *
8  * This program is free software; you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License version 2 only,
10  * as published by the Free Software Foundation.
11  *
12  * This program is distributed in the hope that it will be useful, but
13  * WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15  * General Public License version 2 for more details (a copy is included
16  * in the LICENSE file that accompanied this code).
17  *
18  * You should have received a copy of the GNU General Public License
19  * version 2 along with this program; If not, see
20  * http://www.sun.com/software/products/lustre/docs/GPLv2.pdf
21  *
22  * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
23  * CA 95054 USA or visit www.sun.com if you need additional information or
24  * have any questions.
25  *
26  * GPL HEADER END
27  */
28 /*
29  * Copyright  2008 Sun Microsystems, Inc. All rights reserved
30  * Use is subject to license terms.
31  */
32 /*
33  * This file is part of Lustre, http://www.lustre.org/
34  * Lustre is a trademark of Sun Microsystems, Inc.
35  *
36  * lustre/tests/utime.c
37  *
38  * Simple test for validating mtime on a file create and set via utime.
39  */
40
41 #include <stdio.h>
42 #include <stdlib.h>
43 #include <sys/types.h>
44 #include <sys/stat.h>
45 #include <fcntl.h>
46 #include <unistd.h>
47 #include <time.h>
48 #include <string.h>
49 #include <utime.h>
50 #include <errno.h>
51
52 void usage(char *prog)
53 {
54         fprintf(stderr, "usage: %s <filename> [-s <filename>]\n", prog);
55         exit(1);
56 }
57
58 int main(int argc, char *argv[])
59 {
60         long before_mknod, after_mknod;
61         const char *prog = argv[0];
62         const char *filename = argv[1];
63         char *secname = NULL;
64         struct utimbuf utb;
65         struct stat st, st2;
66         int rc;
67         int c;
68
69         utb.actime = 200000;
70         utb.modtime = 100000;
71
72         while ((c = getopt(argc, argv, "s:")) != -1) {
73                 switch(c) {
74                 case 's':
75                         secname = optarg;
76                         break;
77                 default:
78                         usage(argv[0]);
79                 }
80         }
81         if (optind + 1 > argc)
82                 usage(argv[0]);
83
84         /* Adjust the before time back one second, because the kernel's
85          * CURRENT_TIME (lockless clock reading, used to set inode times)
86          * may drift against the do_gettimeofday() time (TSC-corrected and
87          * locked clock reading, used to return timestamps to user space).
88          * This means that the mknod time could be a second older than the
89          * before time, even for a local filesystem such as ext3.
90          */
91         before_mknod = time(0) - 1;
92         rc = mknod(filename, 0700, S_IFREG);
93         after_mknod = time(0);
94         if (rc && errno != EEXIST) {
95                 fprintf(stderr, "%s: mknod(%s) failed: rc %d: %s\n",
96                         prog, filename, errno, strerror(errno));
97                 return 2;
98         } else if (!rc) {
99                 rc = stat(filename, &st);
100                 if (rc) {
101                         fprintf(stderr, "%s: stat(%s) failed: rc %d: %s\n",
102                                 prog, filename, errno, strerror(errno));
103                         return 3;
104                 }
105
106                 if (st.st_mtime < before_mknod || st.st_mtime > after_mknod) {
107                         fprintf(stderr,
108                                 "%s: bad mknod times %lu <= %lu <= %lu false\n",
109                                 prog, before_mknod, st.st_mtime, after_mknod);
110                         return 4;
111                 }
112
113                 printf("%s: good mknod times %lu%s <= %lu <= %lu for %s\n",
114                        prog, before_mknod, before_mknod == st.st_mtime ? "*":"",
115                        st.st_mtime, after_mknod, filename);
116
117                 if (secname) {
118                         sleep(1);
119                         rc = stat(secname, &st2);
120                         if (rc) {
121                                 fprintf(stderr, "%s: stat(%s) failed: rc %d: "
122                                         "%s\n", prog, secname, errno,
123                                         strerror(errno));
124                                 return 5;
125                         }
126
127                         if (st2.st_mtime < before_mknod || 
128                             st2.st_mtime > after_mknod) {
129                                 fprintf(stderr, "%s: bad mknod times %lu <= %lu"
130                                         " <= %lu false\n", prog, before_mknod,
131                                         st2.st_mtime, after_mknod);
132                                 return 6;
133                         }
134
135                         printf("%s: good mknod times %lu%s <= %lu <= %lu "
136                                "for %s\n", prog, before_mknod, 
137                                before_mknod == st.st_mtime ? "*":"", 
138                                st2.st_mtime, after_mknod, secname);
139                 }
140         }
141
142         /* See above */
143         rc = utime(filename, &utb);
144         if (rc) {
145                 fprintf(stderr, "%s: utime(%s) failed: rc %d: %s\n",
146                         prog, filename, errno, strerror(errno));
147                 return 7;
148         }
149         
150         rc = stat(filename, &st);
151         if (rc) {
152                 fprintf(stderr, "%s: second stat(%s) failed: rc %d: %s\n",
153                         prog, filename, errno, strerror(errno));
154                 return 8;
155         }
156
157         if (st.st_mtime != utb.modtime ) {
158                 fprintf(stderr, "%s: bad utime mtime %lu should be  %lu\n",
159                         prog, st.st_mtime, utb.modtime);
160                 return 9;
161         }
162
163         if (st.st_atime != utb.actime ) {
164                 fprintf(stderr, "%s: bad utime atime %lu should be  %lu\n",
165                         prog, st.st_atime, utb.actime);
166                 return 10;
167         }
168
169         printf("%s: good utime mtimes %lu, atime %lu\n",
170                prog, utb.modtime, utb.actime);
171
172         if (secname == NULL)
173                 return 0;
174         
175         /* Checking that times in past get updated on another client. */
176         rc = stat(secname, &st2);
177         if (rc) {
178                 fprintf(stderr, "%s: second stat(%s) failed: rc %d: %s\n",
179                         prog, secname, errno, strerror(errno));
180                 return 12;
181         }
182
183         if (st2.st_mtime != st.st_mtime) {
184                 fprintf(stderr, "%s: not synced mtime between clients: %lu "
185                         "should be  %lu\n", prog, st2.st_mtime, st.st_mtime);
186                 return 13;
187         }
188
189         if (st2.st_ctime != st.st_ctime) {
190                 fprintf(stderr, "%s: not synced ctime between clients: %lu "
191                         " should be  %lu\n", prog, st2.st_ctime, st.st_ctime);
192                 return 14;
193         }
194         
195         printf("%s: updated times for %s\n", prog, secname);
196         
197         return 0;
198 }