99d47b66c840cca5ad975b59fb5254a3e242f115
[gnulib.git] / tests / nap.h
1 /* Assist in file system timestamp tests.
2    Copyright (C) 2009-2012 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   int result = 0;
52   old_st = *st;
53   usleep (delay);
54   get_mtime (fd, st, 1);
55   if (! lt_mtime (&old_st, st))
56     return 0;
57   old_st = *st;
58   usleep (delay);
59   get_mtime (fd, st, 1);
60   return lt_mtime (&old_st, st);
61 }
62
63 static int
64 guess_delay (void)
65 {
66   /* Try a 1-microsecond sleep first, for speed.  If that doesn't
67      work, try a 1 ms sleep; that should work with ext.  If it doesn't
68      work, try a 20 ms sleep.  xfs has a quantization of about 10
69      milliseconds, even though it has a granularity of 1 nanosecond,
70      and NTFS has a default quantization of 15.25 milliseconds, even
71      though it has a granularity of 100 nanoseconds, so 20 ms is a
72      good quantization to try.  If that doesn't work, try 1 second.
73      The worst case is 2 seconds, needed for FAT.  */
74   static int const delaytab[] = {1, 1000, 20000, 1000000 };
75   int fd = creat (BASE "tmp", 0600);
76   int i;
77   int delay = 2000000;
78   struct stat st;
79   ASSERT (0 <= fd);
80   get_mtime (fd, &st, 0);
81   for (i = 0; i < sizeof delaytab / sizeof delaytab[0]; i++)
82     if (nap_works (fd, delaytab[i], &st))
83       {
84         delay = delaytab[i];
85         break;
86       }
87   ASSERT (close (fd) == 0);
88   ASSERT (unlink (BASE "tmp") == 0);
89   return delay;
90 }
91
92 /* Sleep long enough to notice a timestamp difference on the file
93    system in the current directory.  Assumes that BASE is defined,
94    and requires that the test module depends on usleep.  */
95 static void
96 nap (void)
97 {
98   static int delay;
99   if (!delay)
100     delay = guess_delay ();
101   usleep (delay);
102 }
103
104 #endif /* GLTEST_NAP_H */