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)
76 * Adjust the before time back one second, because the kernel's
77 * CURRENT_TIME (lockless clock reading, used to set inode times)
78 * may drift against the do_gettimeofday() time (TSC-corrected and
79 * locked clock reading, used to return timestamps to user space).
80 * This means that the mknod time could be a second older than the
81 * before time, even for a local filesystem such as ext3.
83 before_mknod = time(0) - 1;
84 rc = mknod(filename, 0700, S_IFREG);
85 after_mknod = time(0);
86 if (rc && errno != EEXIST) {
87 fprintf(stderr, "%s: mknod(%s) failed: rc %d: %s\n",
88 prog, filename, errno, strerror(errno));
91 rc = stat(filename, &st);
93 fprintf(stderr, "%s: stat(%s) failed: rc %d: %s\n",
94 prog, filename, errno, strerror(errno));
98 if (st.st_mtime < before_mknod || st.st_mtime > after_mknod) {
100 "%s: bad mknod(%s) times %lu <= %lu <= %lu false\n",
101 prog, filename, before_mknod, st.st_mtime,
106 printf("%s: good mknod times %lu%s <= %lu <= %lu for %s\n",
108 before_mknod == st.st_mtime ? "*" : "",
109 st.st_mtime, after_mknod, filename);
113 rc = stat(secname, &st2);
116 "%s: stat(%s) failed: rc %d: %s\n",
117 prog, secname, errno, strerror(errno));
121 if (st2.st_mtime < before_mknod ||
122 st2.st_mtime > after_mknod) {
124 "%s: bad mknod(%s) times %lu <= %lu <= %lu false\n",
125 prog, filename, before_mknod,
126 st2.st_mtime, after_mknod);
130 printf("%s: good mknod times %lu%s <= %lu <= %lu for %s\n",
132 before_mknod == st.st_mtime ? "*" : "",
133 st2.st_mtime, after_mknod, secname);
138 utb.modtime = 100000;
139 rc = utime(filename, &utb);
141 fprintf(stderr, "%s: utime(%s) failed: rc %d: %s\n",
142 prog, filename, errno, strerror(errno));
146 rc = stat(filename, &st);
148 fprintf(stderr, "%s: second stat(%s) failed: rc %d: %s\n",
149 prog, filename, errno, strerror(errno));
153 if (st.st_mtime != utb.modtime) {
154 fprintf(stderr, "%s: bad utime mtime(%s) %lu should be %lu\n",
155 prog, filename, st.st_mtime, utb.modtime);
159 if (st.st_atime != utb.actime) {
160 fprintf(stderr, "%s: bad utime atime(%s) %lu should be %lu\n",
161 prog, filename, st.st_atime, utb.actime);
165 printf("%s: good utime mtimes %lu, atime %lu\n",
166 prog, utb.modtime, utb.actime);
171 /* Checking that times in past get updated on another client. */
172 rc = stat(secname, &st2);
174 fprintf(stderr, "%s: second stat(%s) failed: rc %d: %s\n",
175 prog, secname, errno, strerror(errno));
179 if (st2.st_mtime != st.st_mtime) {
181 "%s: not synced mtime(%s) between clients: %lu should be %lu\n",
182 prog, secname, st2.st_mtime, st.st_mtime);
186 if (st2.st_ctime != st.st_ctime) {
188 "%s: not synced ctime(%s) between clients: %lu should be %lu\n",
189 prog, secname, st2.st_ctime, st.st_ctime);
193 printf("%s: updated times for %s\n", prog, secname);