78472a5fbd7edcbfd569161e9c0b7f8753748d21
[debian/tar] / m4 / utimensat.m4
1 # serial 4
2 # See if we need to provide utimensat replacement.
3
4 dnl Copyright (C) 2009-2011 Free Software Foundation, Inc.
5 dnl This file is free software; the Free Software Foundation
6 dnl gives unlimited permission to copy and/or distribute it,
7 dnl with or without modifications, as long as this notice is preserved.
8
9 # Written by Eric Blake.
10
11 AC_DEFUN([gl_FUNC_UTIMENSAT],
12 [
13   AC_REQUIRE([gl_SYS_STAT_H_DEFAULTS])
14   AC_REQUIRE([gl_USE_SYSTEM_EXTENSIONS])
15   AC_CHECK_FUNCS_ONCE([utimensat])
16   if test $ac_cv_func_utimensat = no; then
17     HAVE_UTIMENSAT=0
18     AC_LIBOBJ([utimensat])
19   else
20     AC_CACHE_CHECK([whether utimensat works],
21       [gl_cv_func_utimensat_works],
22       [AC_RUN_IFELSE(
23          [AC_LANG_PROGRAM([[
24 #include <fcntl.h>
25 #include <sys/stat.h>
26 #include <unistd.h>
27 ]],         [[int result = 0;
28               const char *f = "conftest.file";
29               if (close (creat (f, 0600)))
30                 return 1;
31               /* Test whether the AT_SYMLINK_NOFOLLOW flag is supported.  */
32               {
33                 if (utimensat (AT_FDCWD, f, NULL, AT_SYMLINK_NOFOLLOW))
34                   result |= 2;
35               }
36               /* Test whether UTIME_NOW and UTIME_OMIT work.  */
37               {
38                 struct timespec ts[2] = { { 1, UTIME_OMIT }, { 1, UTIME_NOW } };
39                 if (utimensat (AT_FDCWD, f, ts, 0))
40                   result |= 4;
41               }
42               sleep (1);
43               {
44                 struct timespec ts[2] = { { 1, UTIME_NOW }, { 1, UTIME_OMIT } };
45                 struct stat st;
46                 if (utimensat (AT_FDCWD, f, ts, 0))
47                   result |= 8;
48                 if (stat (f, &st))
49                   result |= 16;
50                 else if (st.st_ctime < st.st_atime)
51                   result |= 32;
52               }
53               return result;
54             ]])],
55 dnl FIXME: simplify this in 2012, when file system bugs are no longer common
56          [AC_COMPILE_IFELSE([AC_LANG_PROGRAM([[
57 #ifdef __linux__
58 /* The Linux kernel added utimensat in 2.6.22, but has bugs with UTIME_OMIT
59    in several file systems as recently as 2.6.32.  Always replace utimensat
60    to support older kernels.  */
61 choke me
62 #endif
63       ]])],
64            [gl_cv_func_utimensat_works=yes],
65            [gl_cv_func_utimensat_works="needs runtime check"])],
66          [gl_cv_func_utimensat_works=no],
67          [gl_cv_func_utimensat_works="guessing no"])])
68     if test "$gl_cv_func_utimensat_works" != yes; then
69       REPLACE_UTIMENSAT=1
70       AC_LIBOBJ([utimensat])
71     fi
72   fi
73 ])