Merge branch 'upstream' into stable
[gnulib.git] / tests / test-utimens-common.h
1 /* Test of file timestamp modification functions.
2    Copyright (C) 2009 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 2 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 /* This file defines some prerequisites useful to utime-related tests.  */
18
19 #ifndef GL_TEST_UTIMENS_COMMON
20 # define GL_TEST_UTIMENS_COMMON
21
22 #include <fcntl.h>
23 #include <errno.h>
24 #include <string.h>
25 #include <unistd.h>
26
27 #include "stat-time.h"
28 #include "timespec.h"
29 #include "utimecmp.h"
30
31 enum {
32   BILLION = 1000 * 1000 * 1000,
33
34   Y2K = 946684800, /* Jan 1, 2000, in seconds since epoch.  */
35
36   /* Bogus positive and negative tv_nsec values closest to valid
37      range, but without colliding with UTIME_NOW or UTIME_OMIT.  */
38   UTIME_BOGUS_POS = BILLION + ((UTIME_NOW == BILLION || UTIME_OMIT == BILLION)
39                                ? (1 + (UTIME_NOW == BILLION + 1)
40                                   + (UTIME_OMIT == BILLION + 1))
41                                : 0),
42   UTIME_BOGUS_NEG = -1 - ((UTIME_NOW == -1 || UTIME_OMIT == -1)
43                           ? (1 + (UTIME_NOW == -2) + (UTIME_OMIT == -2))
44                           : 0)
45 };
46
47 /* Sleep long enough to cross a timestamp quantization boundary on
48    most known systems with subsecond timestamp resolution.  For
49    example, ext4 has a quantization of 10 milliseconds, but a
50    resolution of 1 nanosecond.  Likewise, NTFS has a quantization as
51    slow as 15.25 milliseconds, but a resolution of 100 nanoseconds.
52    This is necessary on systems where creat or utimens with NULL
53    rounds down to the quantization boundary, but where gettime and
54    hence utimensat can inject timestamps between quantization
55    boundaries.  By ensuring we cross a boundary, we are less likely to
56    confuse utimecmp for two times that would round to the same
57    quantization boundary but are distinct based on resolution.  */
58 static void
59 nap (void)
60 {
61   /* Systems that lack usleep also lack subsecond timestamps, and have
62      a quantization boundary equal to the resolution.  Our usage of
63      utimecmp allows equality, so no need to waste 980 milliseconds
64      if the replacement usleep rounds to 1 second.  */
65 #if HAVE_USLEEP
66   usleep (20 * 1000); /* 20 milliseconds.  */
67 #endif
68 }
69
70 #endif /* GL_TEST_UTIMENS_COMMON */