342a70ce584ef8806423565f5c5e06ad3b7976b0
[gnulib.git] / tests / nap.h
1 /* Assist in file system timestamp tests.
2    Copyright (C) 2009-2013 Free Software Foundation, Inc.
3
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.
8
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.
13
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/>.  */
16
17 /* Written by Eric Blake <ebb9@byu.net>, 2009.  */
18
19 #ifndef GLTEST_NAP_H
20 # define GLTEST_NAP_H
21
22 static int
23 lt_mtime (struct stat const *a, struct stat const *b)
24 {
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);
29
30   return as < bs || (as == bs && ans < bns);
31 }
32
33 static void
34 get_mtime (int fd, struct stat *st, int do_write)
35 {
36   if (do_write)
37     ASSERT (write (fd, "\n", 1) == 1);
38   ASSERT (fstat (fd, st) == 0);
39 }
40
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.  */
47 static int
48 nap_works (int fd, int delay, struct stat *st)
49 {
50   struct stat old_st;
51   old_st = *st;
52   usleep (delay);
53   get_mtime (fd, st, 1);
54   if (! lt_mtime (&old_st, st))
55     return 0;
56   old_st = *st;
57   usleep (delay);
58   get_mtime (fd, st, 1);
59   return lt_mtime (&old_st, st);
60 }
61
62 static int
63 guess_delay (void)
64 {
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);
75   int i;
76   int delay = 2000000;
77   struct stat st;
78   ASSERT (0 <= fd);
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))
82       {
83         delay = delaytab[i];
84         break;
85       }
86   ASSERT (close (fd) == 0);
87   ASSERT (unlink (BASE "tmp") == 0);
88   return delay;
89 }
90
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.  */
94 static void
95 nap (void)
96 {
97   static int delay;
98   if (!delay)
99     delay = guess_delay ();
100   usleep (delay);
101 }
102
103 #endif /* GLTEST_NAP_H */