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 1 millisecond. If that
108 was enough to observe a difference, then we are set;
109 otherwise fall back to 2 seconds. */
112 ASSERT (stat ("t-stt-stamp1", &st1) == 0);
113 ASSERT (unlink ("t-stt-stamp1") == 0);
114 usleep (delay = 1000);
115 create_file ("t-stt-stamp1");
116 ASSERT (stat ("t-stt-stamp1", &st2) == 0);
117 if (! (st1.st_mtime < st2.st_mtime
118 || (st1.st_mtime == st2.st_mtime
119 && get_stat_mtime_ns (&st1) < get_stat_mtime_ns (&st2))))
123 #endif /* HAVE_USLEEP */
127 prepare_test (struct stat *statinfo, struct timespec *modtimes)
131 create_file ("t-stt-stamp1");
133 create_file ("t-stt-testfile");
135 create_file ("t-stt-stamp2");
137 ASSERT (chmod ("t-stt-testfile", 0400) == 0);
139 create_file ("t-stt-stamp3");
141 do_stat ("t-stt-stamp1", &statinfo[0]);
142 do_stat ("t-stt-testfile", &statinfo[1]);
143 do_stat ("t-stt-stamp2", &statinfo[2]);
144 do_stat ("t-stt-stamp3", &statinfo[3]);
146 /* Now use our access functions. */
147 for (i = 0; i < NFILES; ++i)
149 modtimes[i] = get_stat_mtime (&statinfo[i]);
154 test_mtime (const struct stat *statinfo, struct timespec *modtimes)
158 /* Use the struct stat fields directly. */
159 /* mtime(stamp1) < mtime(stamp2) */
160 ASSERT (statinfo[0].st_mtime < statinfo[2].st_mtime
161 || (statinfo[0].st_mtime == statinfo[2].st_mtime
162 && (get_stat_mtime_ns (&statinfo[0])
163 < get_stat_mtime_ns (&statinfo[2]))));
164 /* mtime(stamp2) < mtime(stamp3) */
165 ASSERT (statinfo[2].st_mtime < statinfo[3].st_mtime
166 || (statinfo[2].st_mtime == statinfo[3].st_mtime
167 && (get_stat_mtime_ns (&statinfo[2])
168 < get_stat_mtime_ns (&statinfo[3]))));
170 /* Now check the result of the access functions. */
171 /* mtime(stamp1) < mtime(stamp2) */
172 ASSERT (modtimes[0].tv_sec < modtimes[2].tv_sec
173 || (modtimes[0].tv_sec == modtimes[2].tv_sec
174 && modtimes[0].tv_nsec < modtimes[2].tv_nsec));
175 /* mtime(stamp2) < mtime(stamp3) */
176 ASSERT (modtimes[2].tv_sec < modtimes[3].tv_sec
177 || (modtimes[2].tv_sec == modtimes[3].tv_sec
178 && modtimes[2].tv_nsec < modtimes[3].tv_nsec));
180 /* verify equivalence */
181 for (i = 0; i < NFILES; ++i)
184 ts = get_stat_mtime (&statinfo[i]);
185 ASSERT (ts.tv_sec == statinfo[i].st_mtime);
189 #if (defined _WIN32 || defined __WIN32__) && !defined __CYGWIN__
190 /* Skip the ctime tests on native Windows platforms, because their
191 st_ctime is either the same as st_mtime (plus or minus an offset)
192 or set to the file _creation_ time, and is not influenced by rename
194 # define test_ctime ((void) 0)
197 test_ctime (const struct stat *statinfo)
199 /* On some buggy NFS clients, mtime and ctime are disproportionately
200 skewed from one another. Skip this test in that case. */
201 if (statinfo[0].st_mtime != statinfo[0].st_ctime)
204 /* mtime(stamp2) < ctime(renamed) */
205 ASSERT (statinfo[2].st_mtime < statinfo[1].st_ctime
206 || (statinfo[2].st_mtime == statinfo[1].st_ctime
207 && (get_stat_mtime_ns (&statinfo[2])
208 < get_stat_ctime_ns (&statinfo[1]))));
213 test_birthtime (const struct stat *statinfo,
214 const struct timespec *modtimes,
215 struct timespec *birthtimes)
219 /* Collect the birth times.. */
220 for (i = 0; i < NFILES; ++i)
222 birthtimes[i] = get_stat_birthtime (&statinfo[i]);
223 if (birthtimes[i].tv_nsec < 0)
227 /* mtime(stamp1) < birthtime(renamed) */
228 ASSERT (modtimes[0].tv_sec < birthtimes[1].tv_sec
229 || (modtimes[0].tv_sec == birthtimes[1].tv_sec
230 && modtimes[0].tv_nsec < birthtimes[1].tv_nsec));
231 /* birthtime(renamed) < mtime(stamp2) */
232 ASSERT (birthtimes[1].tv_sec < modtimes[2].tv_sec
233 || (birthtimes[1].tv_sec == modtimes[2].tv_sec
234 && birthtimes[1].tv_nsec < modtimes[2].tv_nsec));
240 struct stat statinfo[NFILES];
241 struct timespec modtimes[NFILES];
242 struct timespec birthtimes[NFILES];
245 signal (SIGHUP, cleanup);
248 signal (SIGINT, cleanup);
251 signal (SIGQUIT, cleanup);
254 signal (SIGTERM, cleanup);
258 prepare_test (statinfo, modtimes);
259 test_mtime (statinfo, modtimes);
260 test_ctime (statinfo);
261 test_birthtime (statinfo, modtimes, birthtimes);