2 * fspause.c: pause until a file timestamp updates
4 * Copyright (C) 2014 Colin Watson.
6 * This file is part of man-db.
8 * man-db is free software; you can redistribute it and/or modify it
9 * under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
13 * man-db is distributed in the hope that it will be useful, but
14 * WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License
19 * along with man-db; if not, write to the Free Software Foundation,
20 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
25 #endif /* HAVE_CONFIG_H */
30 #include <sys/types.h>
36 #include "stat-time.h"
40 #include "manconfig.h"
42 static char *filename;
45 #define MUST(name, cond) \
48 fprintf (stderr, "fspause: " name " failed\n"); \
53 static void unlink_tempfile (void)
56 MUST ("close", close (fd) >= 0);
57 MUST ("unlink", unlink (filename) >= 0);
61 static void delay (int delay_ns)
63 struct timespec delay_ts;
65 delay_ts.tv_sec = delay_ns / 1000000000;
66 delay_ts.tv_nsec = delay_ns % 1000000000;
69 if (nanosleep (&delay_ts, NULL) == 0)
71 MUST ("nanosleep", errno == 0 || errno == EINTR);
75 static int try_delay (struct stat *st, int delay_ns)
77 struct timespec start_ts, end_ts;
79 start_ts = get_stat_mtime (st);
81 MUST ("write", write (fd, "\n", 1) == 1);
82 MUST ("fstat", fstat (fd, st) >= 0);
83 end_ts = get_stat_mtime (st);
84 return timespec_cmp (start_ts, end_ts) != 0;
87 int main (int argc ATTRIBUTE_UNUSED, char **argv)
92 set_program_name (argv[0]);
94 filename = xstrdup ("fspause.tmp.XXXXXX");
95 MUST ("mkstemp", (fd = mkstemp (filename)) >= 0);
96 atexit (unlink_tempfile);
97 MUST ("fstat", fstat (fd, &st) >= 0);
99 /* 0x40000000 nanoseconds is just over a second. The effective
100 * maximum delay we will allow is thus about two seconds. This
101 * saves us having to keep track of anything more complicated than a
102 * single signed 32-bit int.
104 for (delay_ns = 1; delay_ns < 0x40000000; delay_ns *= 2) {
105 if (try_delay (&st, delay_ns))
110 "fspause: temporary file timestamp refuses to change!\n");