immutable: Add tests.
[gnulib.git] / m4 / utimensat.m4
blobb5bff1651f3c9ed7b3cdbc34538a9e34bdb5cd1c
1 # serial 9
2 # See if we need to provide utimensat replacement.
4 dnl Copyright (C) 2009-2021 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.
9 # Written by Eric Blake.
11 AC_DEFUN([gl_FUNC_UTIMENSAT],
13   AC_REQUIRE([gl_SYS_STAT_H_DEFAULTS])
14   AC_REQUIRE([gl_USE_SYSTEM_EXTENSIONS])
15   AC_REQUIRE([AC_CANONICAL_HOST]) dnl for cross-compiles
16   AC_CHECK_FUNCS_ONCE([utimensat])
17   if test $ac_cv_func_utimensat = no; then
18     HAVE_UTIMENSAT=0
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 ]GL_MDA_DEFINES],
28             [[int result = 0;
29               const char *f = "conftest.file";
30               if (close (creat (f, 0600)))
31                 return 1;
32               /* Test whether a trailing slash is handled correctly.
33                  This fails on AIX 7.2.  */
34               {
35                 struct timespec ts[2];
36                 ts[0].tv_sec = 345183300; ts[0].tv_nsec = 0;
37                 ts[1] = ts[0];
38                 if (utimensat (AT_FDCWD, "conftest.file/", ts, 0) == 0)
39                   result |= 2;
40               }
41               /* Test whether the AT_SYMLINK_NOFOLLOW flag is supported.  */
42               {
43                 if (utimensat (AT_FDCWD, f, NULL, AT_SYMLINK_NOFOLLOW))
44                   result |= 4;
45               }
46               /* Test whether UTIME_NOW and UTIME_OMIT work.  */
47               {
48                 struct timespec ts[2];
49                 ts[0].tv_sec = 1;
50                 ts[0].tv_nsec = UTIME_OMIT;
51                 ts[1].tv_sec = 1;
52                 ts[1].tv_nsec = UTIME_NOW;
53                 if (utimensat (AT_FDCWD, f, ts, 0))
54                   result |= 8;
55               }
56               sleep (1);
57               {
58                 struct stat st;
59                 struct timespec ts[2];
60                 ts[0].tv_sec = 1;
61                 ts[0].tv_nsec = UTIME_NOW;
62                 ts[1].tv_sec = 1;
63                 ts[1].tv_nsec = UTIME_OMIT;
64                 if (utimensat (AT_FDCWD, f, ts, 0))
65                   result |= 16;
66                 if (stat (f, &st))
67                   result |= 32;
68                 else if (st.st_ctime < st.st_atime)
69                   result |= 64;
70               }
71               return result;
72             ]])],
73          [gl_cv_func_utimensat_works=yes],
74          [case $? in
75             2) gl_cv_func_utimensat_works='nearly' ;;
76             *) gl_cv_func_utimensat_works=no ;;
77           esac
78          ],
79          [case "$host_os" in
80             # Guess yes on Linux or glibc systems.
81             linux-* | linux | *-gnu* | gnu*)
82               gl_cv_func_utimensat_works="guessing yes" ;;
83             # Guess 'nearly' on AIX.
84             aix*)
85               gl_cv_func_utimensat_works="guessing nearly" ;;
86             # If we don't know, obey --enable-cross-guesses.
87             *)
88               gl_cv_func_utimensat_works="$gl_cross_guess_normal" ;;
89           esac
90          ])
91       ])
92     case "$gl_cv_func_utimensat_works" in
93       *yes)
94         ;;
95       *nearly)
96         AC_DEFINE([HAVE_NEARLY_WORKING_UTIMENSAT], [1],
97           [Define to 1 if utimensat works, except for the trailing slash handling.])
98         REPLACE_UTIMENSAT=1
99         ;;
100       *)
101         REPLACE_UTIMENSAT=1
102         ;;
103     esac
104   fi