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