1 /* Work around unlinkat bugs on Solaris 9.
3 Copyright (C) 2009-2011 Free Software Foundation, Inc.
5 This program is free software: you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation; either version 3 of the License, or
8 (at your option) any later version.
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with this program. If not, see <http://www.gnu.org/licenses/>. */
18 /* Written by Eric Blake. */
35 /* unlinkat without AT_REMOVEDIR does not honor trailing / on Solaris
36 9. Solve it in a similar manner to unlink. */
39 rpl_unlinkat (int fd, char const *name, int flag)
43 /* rmdir behavior has no problems with trailing slash. */
44 if (flag & AT_REMOVEDIR)
45 return unlinkat (fd, name, flag);
48 if (len && ISSLASH (name[len - 1]))
50 /* See the lengthy comment in unlink.c why we disobey the POSIX
51 rule of letting unlink("link-to-dir/") attempt to unlink a
54 result = lstatat (fd, name, &st);
57 /* Trailing NUL will overwrite the trailing slash. */
58 char *short_name = malloc (len);
64 memcpy (short_name, name, len);
65 while (len && ISSLASH (short_name[len - 1]))
66 short_name[--len] = '\0';
67 if (len && (lstatat (fd, short_name, &st) || S_ISLNK (st.st_mode)))
77 result = unlinkat (fd, name, flag);
81 #else /* !HAVE_UNLINKAT */
83 /* Replacement for Solaris' function by the same name.
84 <http://www.google.com/search?q=unlinkat+site:docs.sun.com>
85 First, try to simulate it via (unlink|rmdir) ("/proc/self/fd/FD/FILE").
86 Failing that, simulate it via save_cwd/fchdir/(unlink|rmdir)/restore_cwd.
87 If either the save_cwd or the restore_cwd fails (relatively unlikely),
88 then give a diagnostic and exit nonzero.
89 Otherwise, this function works just like Solaris' unlinkat. */
91 # define AT_FUNC_NAME unlinkat
92 # define AT_FUNC_F1 rmdir
93 # define AT_FUNC_F2 unlink
94 # define AT_FUNC_USE_F1_COND AT_REMOVEDIR
95 # define AT_FUNC_POST_FILE_PARAM_DECLS , int flag
96 # define AT_FUNC_POST_FILE_ARGS /* empty */
101 # undef AT_FUNC_USE_F1_COND
102 # undef AT_FUNC_POST_FILE_PARAM_DECLS
103 # undef AT_FUNC_POST_FILE_ARGS
105 #endif /* !HAVE_UNLINKAT */