Import upstream version 1.26
[debian/tar] / m4 / unlink.m4
1 # unlink.m4 serial 7
2 dnl Copyright (C) 2009-2011 Free Software Foundation, Inc.
3 dnl This file is free software; the Free Software Foundation
4 dnl gives unlimited permission to copy and/or distribute it,
5 dnl with or without modifications, as long as this notice is preserved.
6
7 AC_DEFUN([gl_FUNC_UNLINK],
8 [
9   AC_REQUIRE([gl_UNISTD_H_DEFAULTS])
10   AC_REQUIRE([AC_CANONICAL_HOST])
11   dnl Detect FreeBSD 7.2, AIX 7.1, Solaris 9 bug.
12   AC_CACHE_CHECK([whether unlink honors trailing slashes],
13     [gl_cv_func_unlink_honors_slashes],
14     [touch conftest.file
15      # Assume that if we have lstat, we can also check symlinks.
16      if test $ac_cv_func_lstat = yes; then
17        ln -s conftest.file conftest.lnk
18      fi
19      AC_RUN_IFELSE(
20        [AC_LANG_PROGRAM(
21          [[#include <unistd.h>
22            #include <errno.h>
23          ]],
24          [[int result = 0;
25            if (!unlink ("conftest.file/"))
26              result |= 1;
27            else if (errno != ENOTDIR)
28              result |= 2;
29 #if HAVE_LSTAT
30            if (!unlink ("conftest.lnk/"))
31              result |= 4;
32            else if (errno != ENOTDIR)
33              result |= 8;
34 #endif
35            return result;
36          ]])],
37       [gl_cv_func_unlink_honors_slashes=yes],
38       [gl_cv_func_unlink_honors_slashes=no],
39       [gl_cv_func_unlink_honors_slashes="guessing no"])
40      rm -f conftest.file conftest.lnk])
41   dnl Detect MacOS X 10.5.6 bug: On read-write HFS mounts, unlink("..") or
42   dnl unlink("../..") succeeds without doing anything.
43   AC_CACHE_CHECK([whether unlink of a parent directory fails as it should],
44     [gl_cv_func_unlink_parent_fails],
45     [case "$host_os" in
46        darwin*)
47          dnl Try to unlink a subdirectory of /tmp, because /tmp is usually on a
48          dnl HFS mount on MacOS X. Use a subdirectory, owned by the current
49          dnl user, because otherwise unlink() may fail due to permissions
50          dnl reasons, and because when running as root we don't want to risk
51          dnl destroying the entire /tmp.
52          if {
53               # Use the mktemp program if available. If not available, hide the error
54               # message.
55               tmp=`(umask 077 && mktemp -d /tmp/gtXXXXXX) 2>/dev/null` &&
56               test -n "$tmp" && test -d "$tmp"
57             } ||
58             {
59               # Use a simple mkdir command. It is guaranteed to fail if the directory
60               # already exists.  $RANDOM is bash specific and expands to empty in shells
61               # other than bash, ksh and zsh.  Its use does not increase security;
62               # rather, it minimizes the probability of failure in a very cluttered /tmp
63               # directory.
64               tmp=/tmp/gt$$-$RANDOM
65               (umask 077 && mkdir "$tmp")
66             }; then
67            mkdir "$tmp/subdir"
68            GL_SUBDIR_FOR_UNLINK="$tmp/subdir"
69            export GL_SUBDIR_FOR_UNLINK
70            AC_RUN_IFELSE(
71              [AC_LANG_SOURCE([[
72                 #include <stdlib.h>
73                 #include <unistd.h>
74                 int main ()
75                 {
76                   int result = 0;
77                   if (chdir (getenv ("GL_SUBDIR_FOR_UNLINK")) != 0)
78                     result |= 1;
79                   else if (unlink ("..") == 0)
80                     result |= 2;
81                   return result;
82                 }
83               ]])],
84              [gl_cv_func_unlink_parent_fails=yes],
85              [gl_cv_func_unlink_parent_fails=no],
86              [gl_cv_func_unlink_parent_fails="guessing no"])
87            unset GL_SUBDIR_FOR_UNLINK
88            rm -rf "$tmp"
89          else
90            gl_cv_func_unlink_parent_fails="guessing no"
91          fi
92          ;;
93        *)
94          gl_cv_func_unlink_parent_fails="guessing yes"
95          ;;
96      esac
97     ])
98   case "$gl_cv_func_unlink_parent_fails" in
99     *no)
100       AC_DEFINE([UNLINK_PARENT_BUG], [1],
101         [Define to 1 if unlink() on a parent directory may succeed])
102       ;;
103   esac
104   if test "$gl_cv_func_unlink_honors_slashes" != yes \
105      || { case "$gl_cv_func_unlink_parent_fails" in
106             *yes) false;;
107             *no) true;;
108           esac
109         }; then
110     REPLACE_UNLINK=1
111     AC_LIBOBJ([unlink])
112   fi
113 ])