| 1 |
/* Assist in file system timestamp tests. |
| 2 |
Copyright (C) 2009-2025 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 <https://www.gnu.org/licenses/>. */ |
| 16 |
|
| 17 |
/* Written by Eric Blake <[email protected]>, 2009. */ |
| 18 |
|
| 19 |
#ifndef GLTEST_NAP_H |
| 20 |
# define GLTEST_NAP_H |
| 21 |
|
| 22 |
# include <limits.h> |
| 23 |
# include <stdckdint.h> |
| 24 |
|
| 25 |
# include "concat-filename.h" |
| 26 |
# include "xgetcwd.h" |
| 27 |
|
| 28 |
/* Avoid a conflict with a function called nap() on UnixWare. */ |
| 29 |
# if defined _SCO_DS || (defined __SCO_VERSION__ || defined __sysv5__) /* OpenServer, UnixWare */ |
| 30 |
# include <unistd.h> |
| 31 |
# undef nap |
| 32 |
# define nap gl_nap |
| 33 |
# endif |
| 34 |
|
| 35 |
/* Name of the witness file. */ |
| 36 |
#define TEMPFILE BASE "nap.tmp" |
| 37 |
|
| 38 |
/* Absolute name of the witness file. */ |
| 39 |
static char *nap_file /* = NULL */; |
| 40 |
|
| 41 |
/* File descriptor used for the witness file. */ |
| 42 |
static int nap_fd = -1; |
| 43 |
|
| 44 |
/* Return A - B, in ns. |
| 45 |
Return 0 if the true result would be negative. |
| 46 |
Return INT_MAX if the true result would be greater than INT_MAX. */ |
| 47 |
static int |
| 48 |
diff_timespec (struct timespec a, struct timespec b) |
| 49 |
{ |
| 50 |
time_t as = a.tv_sec; |
| 51 |
time_t bs = b.tv_sec; |
| 52 |
int ans = a.tv_nsec; |
| 53 |
int bns = b.tv_nsec; |
| 54 |
int sdiff; |
| 55 |
|
| 56 |
ASSERT (0 <= ans && ans < 2000000000); |
| 57 |
ASSERT (0 <= bns && bns < 2000000000); |
| 58 |
|
| 59 |
if (! (bs < as || (bs == as && bns < ans))) |
| 60 |
return 0; |
| 61 |
|
| 62 |
if (ckd_sub (&sdiff, as, bs) |
| 63 |
|| ckd_mul (&sdiff, sdiff, 1000000000) |
| 64 |
|| ckd_add (&sdiff, sdiff, ans - bns)) |
| 65 |
return INT_MAX; |
| 66 |
|
| 67 |
return sdiff; |
| 68 |
} |
| 69 |
|
| 70 |
/* If DO_WRITE, bump the modification time of the file designated by NAP_FD. |
| 71 |
Then fetch the new STAT information of NAP_FD. */ |
| 72 |
static void |
| 73 |
nap_get_stat (struct stat *st, int do_write) |
| 74 |
{ |
| 75 |
if (do_write) |
| 76 |
{ |
| 77 |
ASSERT (write (nap_fd, "\n", 1) == 1); |
| 78 |
#if defined _WIN32 || defined __CYGWIN__ |
| 79 |
/* On Windows, the modification times are not changed until NAP_FD |
| 80 |
is closed. See |
| 81 |
<https://docs.microsoft.com/en-us/windows/desktop/api/fileapi/nf-fileapi-writefile> */ |
| 82 |
close (nap_fd); |
| 83 |
nap_fd = open (nap_file, O_RDWR, 0600); |
| 84 |
ASSERT (nap_fd != -1); |
| 85 |
lseek (nap_fd, 0, SEEK_END); |
| 86 |
#endif |
| 87 |
} |
| 88 |
ASSERT (fstat (nap_fd, st) == 0); |
| 89 |
} |
| 90 |
|
| 91 |
/* Given a file whose descriptor is FD, see whether delaying by DELAY |
| 92 |
nanoseconds causes a change in a file's mtime. |
| 93 |
OLD_ST is the file's status, recently gotten. */ |
| 94 |
static bool |
| 95 |
nap_works (int delay, struct stat old_st) |
| 96 |
{ |
| 97 |
struct stat st; |
| 98 |
struct timespec delay_spec; |
| 99 |
delay_spec.tv_sec = delay / 1000000000; |
| 100 |
delay_spec.tv_nsec = delay % 1000000000; |
| 101 |
ASSERT (nanosleep (&delay_spec, NULL) == 0); |
| 102 |
nap_get_stat (&st, 1); |
| 103 |
|
| 104 |
if (diff_timespec (get_stat_mtime (&st), get_stat_mtime (&old_st))) |
| 105 |
return true; |
| 106 |
|
| 107 |
return false; |
| 108 |
} |
| 109 |
|
| 110 |
static void |
| 111 |
clear_temp_file (void) |
| 112 |
{ |
| 113 |
if (0 <= nap_fd) |
| 114 |
{ |
| 115 |
ASSERT (close (nap_fd) != -1); |
| 116 |
ASSERT (unlink (nap_file) != -1); |
| 117 |
} |
| 118 |
} |
| 119 |
|
| 120 |
/* Sleep long enough to notice a timestamp difference on the file |
| 121 |
system in the current directory. Use an adaptive approach, trying |
| 122 |
to find the smallest delay which works on the current file system |
| 123 |
to make the timestamp difference appear. Assert a maximum delay of |
| 124 |
~2 seconds, more precisely sum(2^n) from 0 to 30 = 2^31 - 1 = 2.1s. |
| 125 |
Assumes that BASE is defined, and requires that the test module |
| 126 |
depends on nanosleep. */ |
| 127 |
static void |
| 128 |
nap (void) |
| 129 |
{ |
| 130 |
struct stat old_st; |
| 131 |
static int delay = 1; |
| 132 |
|
| 133 |
if (-1 == nap_fd) |
| 134 |
{ |
| 135 |
nap_file = xconcatenated_filename (xgetcwd (), TEMPFILE, NULL); |
| 136 |
atexit (clear_temp_file); |
| 137 |
ASSERT ((nap_fd = creat (nap_file, 0600)) != -1); |
| 138 |
nap_get_stat (&old_st, 0); |
| 139 |
} |
| 140 |
else |
| 141 |
{ |
| 142 |
ASSERT (0 <= nap_fd); |
| 143 |
nap_get_stat (&old_st, 1); |
| 144 |
} |
| 145 |
|
| 146 |
if (1 < delay) |
| 147 |
delay = delay / 2; /* Try half of the previous delay. */ |
| 148 |
ASSERT (0 < delay); |
| 149 |
|
| 150 |
for (;;) |
| 151 |
{ |
| 152 |
if (nap_works (delay, old_st)) |
| 153 |
return; |
| 154 |
if (delay <= (2147483647 - 1) / 2) |
| 155 |
{ |
| 156 |
delay = delay * 2 + 1; |
| 157 |
continue; |
| 158 |
} |
| 159 |
else |
| 160 |
break; |
| 161 |
} |
| 162 |
|
| 163 |
/* Bummer: even the highest nap delay didn't work. */ |
| 164 |
ASSERT (0); |
| 165 |
} |
| 166 |
|
| 167 |
#endif /* GLTEST_NAP_H */ |