1 /* -*- mode: c; c-basic-offset: 8; indent-tabs-mode: nil; -*-
2 * vim:expandtab:shiftwidth=8:tabstop=8:
6 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
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.
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).
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
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
29 * Copyright 2008 Sun Microsystems, Inc. All rights reserved
30 * Use is subject to license terms.
33 * This file is part of Lustre, http://www.lustre.org/
34 * Lustre is a trademark of Sun Microsystems, Inc.
36 * lustre/tests/utime.c
38 * Simple test for validating mtime on a file create and set via utime.
43 #include <sys/types.h>
52 void usage(char *prog)
54 fprintf(stderr, "usage: %s <filename> [-s <filename>]\n", prog);
58 int main(int argc, char *argv[])
60 long before_mknod, after_mknod;
61 const char *prog = argv[0];
62 const char *filename = argv[1];
72 while ((c = getopt(argc, argv, "s:")) != -1) {
81 if (optind + 1 > argc)
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.
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));
99 rc = stat(filename, &st);
101 fprintf(stderr, "%s: stat(%s) failed: rc %d: %s\n",
102 prog, filename, errno, strerror(errno));
106 if (st.st_mtime < before_mknod || st.st_mtime > after_mknod) {
108 "%s: bad mknod times %lu <= %lu <= %lu false\n",
109 prog, before_mknod, st.st_mtime, after_mknod);
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);
119 rc = stat(secname, &st2);
121 fprintf(stderr, "%s: stat(%s) failed: rc %d: "
122 "%s\n", prog, secname, errno,
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);
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);
143 rc = utime(filename, &utb);
145 fprintf(stderr, "%s: utime(%s) failed: rc %d: %s\n",
146 prog, filename, errno, strerror(errno));
150 rc = stat(filename, &st);
152 fprintf(stderr, "%s: second stat(%s) failed: rc %d: %s\n",
153 prog, filename, errno, strerror(errno));
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);
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);
169 printf("%s: good utime mtimes %lu, atime %lu\n",
170 prog, utb.modtime, utb.actime);
175 /* Checking that times in past get updated on another client. */
176 rc = stat(secname, &st2);
178 fprintf(stderr, "%s: second stat(%s) failed: rc %d: %s\n",
179 prog, secname, errno, strerror(errno));
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);
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);
195 printf("%s: updated times for %s\n", prog, secname);