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__)
191 test_ctime (const struct stat *statinfo)
193 /* mtime(stamp2) < ctime(renamed) */
194 ASSERT (statinfo[2].st_mtime < statinfo[1].st_ctime
195 || (statinfo[2].st_mtime == statinfo[1].st_ctime
196 && (get_stat_mtime_ns (&statinfo[2])
197 < get_stat_ctime_ns (&statinfo[1]))));
202 test_birthtime (const struct stat *statinfo,
203 const struct timespec *modtimes,
204 struct timespec *birthtimes)
208 /* Collect the birth times.. */
209 for (i = 0; i < NFILES; ++i)
211 birthtimes[i] = get_stat_birthtime (&statinfo[i]);
212 if (birthtimes[i].tv_nsec < 0)
216 /* mtime(stamp1) < birthtime(renamed) */
217 ASSERT (modtimes[0].tv_sec < birthtimes[1].tv_sec
218 || (modtimes[0].tv_sec == birthtimes[1].tv_sec
219 && modtimes[0].tv_nsec < birthtimes[1].tv_nsec));
220 /* birthtime(renamed) < mtime(stamp2) */
221 ASSERT (birthtimes[1].tv_sec < modtimes[2].tv_sec
222 || (birthtimes[1].tv_sec == modtimes[2].tv_sec
223 && birthtimes[1].tv_nsec < modtimes[2].tv_nsec));
229 struct stat statinfo[NFILES];
230 struct timespec modtimes[NFILES];
231 struct timespec birthtimes[NFILES];
234 signal (SIGHUP, cleanup);
237 signal (SIGINT, cleanup);
240 signal (SIGQUIT, cleanup);
243 signal (SIGTERM, cleanup);
247 prepare_test (statinfo, modtimes);
248 test_mtime (statinfo, modtimes);
249 /* Skip the ctime tests on native Windows platforms, because there st_ctime
250 is either the same as st_mtime (plus or minus an offset) or set to the
251 file _creation_ time, and is not influenced by rename or chmod. */
252 #if !((defined _WIN32 || defined __WIN32__) && !defined __CYGWIN__)
253 test_ctime (statinfo);
255 test_birthtime (statinfo, modtimes, birthtimes);