1 /* Assist in file system timestamp tests.
2 Copyright (C) 2009-2013 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 Eric Blake <ebb9@byu.net>, 2009. */
23 lt_mtime (struct stat const *a, struct stat const *b)
25 time_t as = a->st_mtime;
26 time_t bs = b->st_mtime;
27 int ans = get_stat_mtime_ns (a);
28 int bns = get_stat_mtime_ns (b);
30 return as < bs || (as == bs && ans < bns);
34 get_mtime (int fd, struct stat *st, int do_write)
37 ASSERT (write (fd, "\n", 1) == 1);
38 ASSERT (fstat (fd, st) == 0);
41 /* Given a file whose descriptor is FD, see whether delaying by DELAY
42 microseconds causes a change in a file's time stamp. If the time
43 stamps differ, repeat the test one more time, in case we crossed a
44 quantization boundary on a file system with lower resolution. *ST
45 is the file's status, recently gotten. Update *ST to reflect the
46 latest status gotten. */
48 nap_works (int fd, int delay, struct stat *st)
53 get_mtime (fd, st, 1);
54 if (! lt_mtime (&old_st, st))
58 get_mtime (fd, st, 1);
59 return lt_mtime (&old_st, st);
65 /* Try a 1-microsecond sleep first, for speed. If that doesn't
66 work, try a 1 ms sleep; that should work with ext. If it doesn't
67 work, try a 20 ms sleep. xfs has a quantization of about 10
68 milliseconds, even though it has a granularity of 1 nanosecond,
69 and NTFS has a default quantization of 15.25 milliseconds, even
70 though it has a granularity of 100 nanoseconds, so 20 ms is a
71 good quantization to try. If that doesn't work, try 1 second.
72 The worst case is 2 seconds, needed for FAT. */
73 static int const delaytab[] = {1, 1000, 20000, 1000000 };
74 int fd = creat (BASE "tmp", 0600);
79 get_mtime (fd, &st, 0);
80 for (i = 0; i < sizeof delaytab / sizeof delaytab[0]; i++)
81 if (nap_works (fd, delaytab[i], &st))
86 ASSERT (close (fd) == 0);
87 ASSERT (unlink (BASE "tmp") == 0);
91 /* Sleep long enough to notice a timestamp difference on the file
92 system in the current directory. Assumes that BASE is defined,
93 and requires that the test module depends on usleep. */
99 delay = guess_delay ();
103 #endif /* GLTEST_NAP_H */