1 /* Test of <stat-time.h>.
2 Copyright (C) 2007-2011 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"
33 force_unlink (const char *filename)
35 /* This chmod is necessary on mingw, where unlink() of a read-only file
37 chmod (filename, 0600);
38 return unlink (filename);
44 /* Remove temporary files. */
45 force_unlink ("t-stt-stamp1");
46 force_unlink ("t-stt-testfile");
47 force_unlink ("t-stt-stamp2");
48 force_unlink ("t-stt-renamed");
49 force_unlink ("t-stt-stamp3");
56 open_file (const char *filename, int flags)
58 int fd = open (filename, flags | O_WRONLY, 0500);
71 create_file (const char *filename)
73 ASSERT (open_file (filename, O_CREAT | O_EXCL));
77 do_stat (const char *filename, struct stat *p)
79 ASSERT (stat (filename, p) == 0);
82 /* Sleep long enough to notice a timestamp difference on the file
83 system in the current directory. */
90 /* Initialize only once, by sleeping for 20 milliseconds (needed
91 since xfs has a quantization of about 10 milliseconds, even
92 though it has a granularity of 1 nanosecond, and since NTFS
93 has a default quantization of 15.25 milliseconds, even though
94 it has a granularity of 100 nanoseconds). If the seconds
95 differ, repeat the test one more time (in case we crossed a
96 quantization boundary on a file system with 1 second
97 resolution). If we can't observe a difference in only the
98 nanoseconds, then fall back to 1 second if the time is odd,
99 and 2 seconds (needed for FAT) if time is even. */
102 ASSERT (stat ("t-stt-stamp1", &st1) == 0);
103 ASSERT (force_unlink ("t-stt-stamp1") == 0);
106 create_file ("t-stt-stamp1");
107 ASSERT (stat ("t-stt-stamp1", &st2) == 0);
108 if (st1.st_mtime != st2.st_mtime)
110 /* Seconds differ, give it one more shot. */
112 ASSERT (force_unlink ("t-stt-stamp1") == 0);
114 create_file ("t-stt-stamp1");
115 ASSERT (stat ("t-stt-stamp1", &st2) == 0);
117 if (! (st1.st_mtime == st2.st_mtime
118 && get_stat_mtime_ns (&st1) < get_stat_mtime_ns (&st2)))
119 delay = (st1.st_mtime & 1) ? 1000000 : 2000000;
125 prepare_test (struct stat *statinfo, struct timespec *modtimes)
129 create_file ("t-stt-stamp1");
131 create_file ("t-stt-testfile");
133 create_file ("t-stt-stamp2");
135 ASSERT (chmod ("t-stt-testfile", 0400) == 0);
137 create_file ("t-stt-stamp3");
139 do_stat ("t-stt-stamp1", &statinfo[0]);
140 do_stat ("t-stt-testfile", &statinfo[1]);
141 do_stat ("t-stt-stamp2", &statinfo[2]);
142 do_stat ("t-stt-stamp3", &statinfo[3]);
144 /* Now use our access functions. */
145 for (i = 0; i < NFILES; ++i)
147 modtimes[i] = get_stat_mtime (&statinfo[i]);
152 test_mtime (const struct stat *statinfo, struct timespec *modtimes)
156 /* Use the struct stat fields directly. */
157 /* mtime(stamp1) < mtime(stamp2) */
158 ASSERT (statinfo[0].st_mtime < statinfo[2].st_mtime
159 || (statinfo[0].st_mtime == statinfo[2].st_mtime
160 && (get_stat_mtime_ns (&statinfo[0])
161 < get_stat_mtime_ns (&statinfo[2]))));
162 /* mtime(stamp2) < mtime(stamp3) */
163 ASSERT (statinfo[2].st_mtime < statinfo[3].st_mtime
164 || (statinfo[2].st_mtime == statinfo[3].st_mtime
165 && (get_stat_mtime_ns (&statinfo[2])
166 < get_stat_mtime_ns (&statinfo[3]))));
168 /* Now check the result of the access functions. */
169 /* mtime(stamp1) < mtime(stamp2) */
170 ASSERT (modtimes[0].tv_sec < modtimes[2].tv_sec
171 || (modtimes[0].tv_sec == modtimes[2].tv_sec
172 && modtimes[0].tv_nsec < modtimes[2].tv_nsec));
173 /* mtime(stamp2) < mtime(stamp3) */
174 ASSERT (modtimes[2].tv_sec < modtimes[3].tv_sec
175 || (modtimes[2].tv_sec == modtimes[3].tv_sec
176 && modtimes[2].tv_nsec < modtimes[3].tv_nsec));
178 /* verify equivalence */
179 for (i = 0; i < NFILES; ++i)
182 ts = get_stat_mtime (&statinfo[i]);
183 ASSERT (ts.tv_sec == statinfo[i].st_mtime);
187 #if (defined _WIN32 || defined __WIN32__) && !defined __CYGWIN__
188 /* Skip the ctime tests on native Windows platforms, because their
189 st_ctime is either the same as st_mtime (plus or minus an offset)
190 or set to the file _creation_ time, and is not influenced by rename
192 # define test_ctime(ignored) ((void) 0)
195 test_ctime (const struct stat *statinfo)
197 /* On some buggy NFS clients, mtime and ctime are disproportionately
198 skewed from one another. Skip this test in that case. */
199 if (statinfo[0].st_mtime != statinfo[0].st_ctime)
202 /* mtime(stamp2) < ctime(renamed) */
203 ASSERT (statinfo[2].st_mtime < statinfo[1].st_ctime
204 || (statinfo[2].st_mtime == statinfo[1].st_ctime
205 && (get_stat_mtime_ns (&statinfo[2])
206 < get_stat_ctime_ns (&statinfo[1]))));
211 test_birthtime (const struct stat *statinfo,
212 const struct timespec *modtimes,
213 struct timespec *birthtimes)
217 /* Collect the birth times. */
218 for (i = 0; i < NFILES; ++i)
220 birthtimes[i] = get_stat_birthtime (&statinfo[i]);
221 if (birthtimes[i].tv_nsec < 0)
225 /* mtime(stamp1) < birthtime(renamed) */
226 ASSERT (modtimes[0].tv_sec < birthtimes[1].tv_sec
227 || (modtimes[0].tv_sec == birthtimes[1].tv_sec
228 && modtimes[0].tv_nsec < birthtimes[1].tv_nsec));
229 /* birthtime(renamed) < mtime(stamp2) */
230 ASSERT (birthtimes[1].tv_sec < modtimes[2].tv_sec
231 || (birthtimes[1].tv_sec == modtimes[2].tv_sec
232 && birthtimes[1].tv_nsec < modtimes[2].tv_nsec));
238 struct stat statinfo[NFILES];
239 struct timespec modtimes[NFILES];
240 struct timespec birthtimes[NFILES];
243 signal (SIGHUP, cleanup);
246 signal (SIGINT, cleanup);
249 signal (SIGQUIT, cleanup);
252 signal (SIGTERM, cleanup);
256 prepare_test (statinfo, modtimes);
257 test_mtime (statinfo, modtimes);
258 test_ctime (statinfo);
259 test_birthtime (statinfo, modtimes, birthtimes);