1 /* Test of <stat-time.h>.
2 Copyright (C) 2007-2009 Free Software Foundation, Inc.
4 This program is free software: you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation; either version 3 of the License, or
7 (at your option) any later version.
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
14 You should have received a copy of the GNU General Public License
15 along with this program. If not, see <http://www.gnu.org/licenses/>. */
17 /* Written by James Youngman <jay@gnu.org>, 2007. */
21 #include "stat-time.h"
30 #define ASSERT(expr) \
35 fprintf (stderr, "%s:%d: assertion failed\n", __FILE__, __LINE__); \
45 force_unlink (const char *filename)
47 /* This chmod is necessary on mingw, where unlink() of a read-only file
49 chmod (filename, 0600);
56 /* Remove temporary files. */
57 force_unlink ("t-stt-stamp1");
58 force_unlink ("t-stt-testfile");
59 force_unlink ("t-stt-stamp2");
60 force_unlink ("t-stt-renamed");
61 force_unlink ("t-stt-stamp3");
68 open_file (const char *filename, int flags)
70 int fd = open (filename, flags | O_WRONLY, 0500);
83 create_file (const char *filename)
85 ASSERT (open_file (filename, O_CREAT | O_EXCL));
89 do_stat (const char *filename, struct stat *p)
91 ASSERT (stat (filename, p) == 0);
94 /* Sleep long enough to notice a timestamp difference on the file
95 system in the current directory. */
100 /* Assume the worst case file system of FAT, which has a granularity
103 #else /* HAVE_USLEEP */
107 /* Initialize only once, by sleeping for 15 milliseconds (needed
108 since xfs has a quantization of about 10 milliseconds, even
109 though it has a granularity of 1 nanosecond). If the seconds
110 differ, repeat the test one more time (in case we crossed a
111 quantization boundary on a file system with 1 second
112 resolution). If we can't observe a difference in only the
113 nanoseconds, then fall back to 2 seconds. */
116 ASSERT (stat ("t-stt-stamp1", &st1) == 0);
117 ASSERT (unlink ("t-stt-stamp1") == 0);
118 usleep (delay = 15000);
119 create_file ("t-stt-stamp1");
120 ASSERT (stat ("t-stt-stamp1", &st2) == 0);
121 if (st1.st_mtime != st2.st_mtime)
123 /* Seconds differ, give it one more shot. */
125 ASSERT (unlink ("t-stt-stamp1") == 0);
127 create_file ("t-stt-stamp1");
128 ASSERT (stat ("t-stt-stamp1", &st2) == 0);
130 if (! (st1.st_mtime == st2.st_mtime
131 && get_stat_mtime_ns (&st1) < get_stat_mtime_ns (&st2)))
135 #endif /* HAVE_USLEEP */
139 prepare_test (struct stat *statinfo, struct timespec *modtimes)
143 create_file ("t-stt-stamp1");
145 create_file ("t-stt-testfile");
147 create_file ("t-stt-stamp2");
149 ASSERT (chmod ("t-stt-testfile", 0400) == 0);
151 create_file ("t-stt-stamp3");
153 do_stat ("t-stt-stamp1", &statinfo[0]);
154 do_stat ("t-stt-testfile", &statinfo[1]);
155 do_stat ("t-stt-stamp2", &statinfo[2]);
156 do_stat ("t-stt-stamp3", &statinfo[3]);
158 /* Now use our access functions. */
159 for (i = 0; i < NFILES; ++i)
161 modtimes[i] = get_stat_mtime (&statinfo[i]);
166 test_mtime (const struct stat *statinfo, struct timespec *modtimes)
170 /* Use the struct stat fields directly. */
171 /* mtime(stamp1) < mtime(stamp2) */
172 ASSERT (statinfo[0].st_mtime < statinfo[2].st_mtime
173 || (statinfo[0].st_mtime == statinfo[2].st_mtime
174 && (get_stat_mtime_ns (&statinfo[0])
175 < get_stat_mtime_ns (&statinfo[2]))));
176 /* mtime(stamp2) < mtime(stamp3) */
177 ASSERT (statinfo[2].st_mtime < statinfo[3].st_mtime
178 || (statinfo[2].st_mtime == statinfo[3].st_mtime
179 && (get_stat_mtime_ns (&statinfo[2])
180 < get_stat_mtime_ns (&statinfo[3]))));
182 /* Now check the result of the access functions. */
183 /* mtime(stamp1) < mtime(stamp2) */
184 ASSERT (modtimes[0].tv_sec < modtimes[2].tv_sec
185 || (modtimes[0].tv_sec == modtimes[2].tv_sec
186 && modtimes[0].tv_nsec < modtimes[2].tv_nsec));
187 /* mtime(stamp2) < mtime(stamp3) */
188 ASSERT (modtimes[2].tv_sec < modtimes[3].tv_sec
189 || (modtimes[2].tv_sec == modtimes[3].tv_sec
190 && modtimes[2].tv_nsec < modtimes[3].tv_nsec));
192 /* verify equivalence */
193 for (i = 0; i < NFILES; ++i)
196 ts = get_stat_mtime (&statinfo[i]);
197 ASSERT (ts.tv_sec == statinfo[i].st_mtime);
201 #if (defined _WIN32 || defined __WIN32__) && !defined __CYGWIN__
202 /* Skip the ctime tests on native Windows platforms, because their
203 st_ctime is either the same as st_mtime (plus or minus an offset)
204 or set to the file _creation_ time, and is not influenced by rename
206 # define test_ctime ((void) 0)
209 test_ctime (const struct stat *statinfo)
211 /* On some buggy NFS clients, mtime and ctime are disproportionately
212 skewed from one another. Skip this test in that case. */
213 if (statinfo[0].st_mtime != statinfo[0].st_ctime)
216 /* mtime(stamp2) < ctime(renamed) */
217 ASSERT (statinfo[2].st_mtime < statinfo[1].st_ctime
218 || (statinfo[2].st_mtime == statinfo[1].st_ctime
219 && (get_stat_mtime_ns (&statinfo[2])
220 < get_stat_ctime_ns (&statinfo[1]))));
225 test_birthtime (const struct stat *statinfo,
226 const struct timespec *modtimes,
227 struct timespec *birthtimes)
231 /* Collect the birth times.. */
232 for (i = 0; i < NFILES; ++i)
234 birthtimes[i] = get_stat_birthtime (&statinfo[i]);
235 if (birthtimes[i].tv_nsec < 0)
239 /* mtime(stamp1) < birthtime(renamed) */
240 ASSERT (modtimes[0].tv_sec < birthtimes[1].tv_sec
241 || (modtimes[0].tv_sec == birthtimes[1].tv_sec
242 && modtimes[0].tv_nsec < birthtimes[1].tv_nsec));
243 /* birthtime(renamed) < mtime(stamp2) */
244 ASSERT (birthtimes[1].tv_sec < modtimes[2].tv_sec
245 || (birthtimes[1].tv_sec == modtimes[2].tv_sec
246 && birthtimes[1].tv_nsec < modtimes[2].tv_nsec));
252 struct stat statinfo[NFILES];
253 struct timespec modtimes[NFILES];
254 struct timespec birthtimes[NFILES];
257 signal (SIGHUP, cleanup);
260 signal (SIGINT, cleanup);
263 signal (SIGQUIT, cleanup);
266 signal (SIGTERM, cleanup);
270 prepare_test (statinfo, modtimes);
271 test_mtime (statinfo, modtimes);
272 test_ctime (statinfo);
273 test_birthtime (statinfo, modtimes, birthtimes);