4 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License version 2 only,
8 * as published by the Free Software Foundation.
10 * This program is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * General Public License version 2 for more details (a copy is included
14 * in the LICENSE file that accompanied this code).
16 * You should have received a copy of the GNU General Public License
17 * version 2 along with this program; If not, see
18 * http://www.gnu.org/licenses/gpl-2.0.html
23 * Copyright (c) 2003, 2010, Oracle and/or its affiliates. All rights reserved.
24 * Use is subject to license terms.
27 * This file is part of Lustre, http://www.lustre.org/
28 * Lustre is a trademark of Sun Microsystems, Inc.
30 * lustre/tests/utime.c
32 * Simple test for validating mtime on a file create and set via utime.
37 #include <sys/types.h>
46 void usage(char *prog)
48 fprintf(stderr, "usage: %s <filename> [-s <filename>]\n", prog);
52 int main(int argc, char *argv[])
54 long before_mknod, after_mknod;
55 const char *prog = argv[0];
56 const char *filename = argv[1];
63 while ((c = getopt(argc, argv, "s:")) != -1) {
72 if (optind + 1 > argc)
75 /* Adjust the before time back one second, because the kernel's
76 * CURRENT_TIME (lockless clock reading, used to set inode times)
77 * may drift against the do_gettimeofday() time (TSC-corrected and
78 * locked clock reading, used to return timestamps to user space).
79 * This means that the mknod time could be a second older than the
80 * before time, even for a local filesystem such as ext3.
82 before_mknod = time(0) - 1;
83 rc = mknod(filename, 0700, S_IFREG);
84 after_mknod = time(0);
85 if (rc && errno != EEXIST) {
86 fprintf(stderr, "%s: mknod(%s) failed: rc %d: %s\n",
87 prog, filename, errno, strerror(errno));
90 rc = stat(filename, &st);
92 fprintf(stderr, "%s: stat(%s) failed: rc %d: %s\n",
93 prog, filename, errno, strerror(errno));
97 if (st.st_mtime < before_mknod || st.st_mtime > after_mknod) {
98 fprintf(stderr, "%s: bad mknod(%s) times %lu <= %lu <= "
99 "%lu false\n", prog, filename, before_mknod,
100 st.st_mtime, after_mknod);
104 printf("%s: good mknod times %lu%s <= %lu <= %lu for %s\n",
105 prog, before_mknod, before_mknod == st.st_mtime ? "*":"",
106 st.st_mtime, after_mknod, filename);
110 rc = stat(secname, &st2);
112 fprintf(stderr, "%s: stat(%s) failed: rc %d: "
113 "%s\n", prog, secname, errno,
118 if (st2.st_mtime < before_mknod ||
119 st2.st_mtime > after_mknod) {
120 fprintf(stderr, "%s: bad mknod(%s) times %lu "
121 " <= %lu <= %lu false\n", prog,
122 filename, before_mknod, st2.st_mtime,
127 printf("%s: good mknod times %lu%s <= %lu <= %lu "
128 "for %s\n", prog, before_mknod,
129 before_mknod == st.st_mtime ? "*":"",
130 st2.st_mtime, after_mknod, secname);
135 utb.modtime = 100000;
136 rc = utime(filename, &utb);
138 fprintf(stderr, "%s: utime(%s) failed: rc %d: %s\n",
139 prog, filename, errno, strerror(errno));
143 rc = stat(filename, &st);
145 fprintf(stderr, "%s: second stat(%s) failed: rc %d: %s\n",
146 prog, filename, errno, strerror(errno));
150 if (st.st_mtime != utb.modtime ) {
151 fprintf(stderr, "%s: bad utime mtime(%s) %lu should be %lu\n",
152 prog, filename, st.st_mtime, utb.modtime);
156 if (st.st_atime != utb.actime ) {
157 fprintf(stderr, "%s: bad utime atime(%s) %lu should be %lu\n",
158 prog, filename, st.st_atime, utb.actime);
162 printf("%s: good utime mtimes %lu, atime %lu\n",
163 prog, utb.modtime, utb.actime);
168 /* Checking that times in past get updated on another client. */
169 rc = stat(secname, &st2);
171 fprintf(stderr, "%s: second stat(%s) failed: rc %d: %s\n",
172 prog, secname, errno, strerror(errno));
176 if (st2.st_mtime != st.st_mtime) {
177 fprintf(stderr, "%s: not synced mtime(%s) between clients: "
178 "%lu should be %lu\n", prog, secname,
179 st2.st_mtime, st.st_mtime);
183 if (st2.st_ctime != st.st_ctime) {
184 fprintf(stderr, "%s: not synced ctime(%s) between clients: "
185 "%lu should be %lu\n", prog, secname,
186 st2.st_ctime, st.st_ctime);
190 printf("%s: updated times for %s\n", prog, secname);