1 | /* Work around unlinkat bugs on Solaris 9 and Hurd.
|
---|
2 |
|
---|
3 | Copyright (C) 2009-2016 Free Software Foundation, Inc.
|
---|
4 |
|
---|
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.
|
---|
9 |
|
---|
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.
|
---|
14 |
|
---|
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/>. */
|
---|
17 |
|
---|
18 | /* Written by Eric Blake. */
|
---|
19 |
|
---|
20 | #include <config.h>
|
---|
21 |
|
---|
22 | #include <unistd.h>
|
---|
23 |
|
---|
24 | #include <errno.h>
|
---|
25 | #include <fcntl.h>
|
---|
26 | #include <string.h>
|
---|
27 | #include <sys/stat.h>
|
---|
28 |
|
---|
29 | #include <stdlib.h>
|
---|
30 |
|
---|
31 | #include "dosname.h"
|
---|
32 | #include "openat.h"
|
---|
33 |
|
---|
34 | #if HAVE_UNLINKAT
|
---|
35 |
|
---|
36 | # undef unlinkat
|
---|
37 |
|
---|
38 | /* unlinkat without AT_REMOVEDIR does not honor trailing / on Solaris 9.
|
---|
39 | Hurd has the same issue.
|
---|
40 |
|
---|
41 | unlinkat without AT_REMOVEDIR erroneously ignores ".." on Darwin 14.
|
---|
42 |
|
---|
43 | Solve these in a similar manner to unlink. */
|
---|
44 |
|
---|
45 | int
|
---|
46 | rpl_unlinkat (int fd, char const *name, int flag)
|
---|
47 | {
|
---|
48 | size_t len;
|
---|
49 | int result = 0;
|
---|
50 | /* rmdir behavior has no problems with trailing slash. */
|
---|
51 | if (flag & AT_REMOVEDIR)
|
---|
52 | return unlinkat (fd, name, flag);
|
---|
53 |
|
---|
54 | len = strlen (name);
|
---|
55 | if (len && ISSLASH (name[len - 1]))
|
---|
56 | {
|
---|
57 | /* See the lengthy comment in unlink.c why we disobey the POSIX
|
---|
58 | rule of letting unlink("link-to-dir/") attempt to unlink a
|
---|
59 | directory. */
|
---|
60 | struct stat st;
|
---|
61 | result = lstatat (fd, name, &st);
|
---|
62 | if (result == 0)
|
---|
63 | {
|
---|
64 | /* Trailing NUL will overwrite the trailing slash. */
|
---|
65 | char *short_name = malloc (len);
|
---|
66 | if (!short_name)
|
---|
67 | {
|
---|
68 | errno = EPERM;
|
---|
69 | return -1;
|
---|
70 | }
|
---|
71 | memcpy (short_name, name, len);
|
---|
72 | while (len && ISSLASH (short_name[len - 1]))
|
---|
73 | short_name[--len] = '\0';
|
---|
74 | if (len && (lstatat (fd, short_name, &st) || S_ISLNK (st.st_mode)))
|
---|
75 | {
|
---|
76 | free (short_name);
|
---|
77 | errno = EPERM;
|
---|
78 | return -1;
|
---|
79 | }
|
---|
80 | free (short_name);
|
---|
81 | }
|
---|
82 | }
|
---|
83 | if (!result)
|
---|
84 | {
|
---|
85 | # if UNLINK_PARENT_BUG
|
---|
86 | if (len >= 2 && name[len - 1] == '.' && name[len - 2] == '.'
|
---|
87 | && (len == 2 || ISSLASH (name[len - 3])))
|
---|
88 | {
|
---|
89 | errno = EISDIR; /* could also use EPERM */
|
---|
90 | return -1;
|
---|
91 | }
|
---|
92 | # endif
|
---|
93 | result = unlinkat (fd, name, flag);
|
---|
94 | }
|
---|
95 | return result;
|
---|
96 | }
|
---|
97 |
|
---|
98 | #else /* !HAVE_UNLINKAT */
|
---|
99 |
|
---|
100 | /* Replacement for Solaris' function by the same name.
|
---|
101 | <http://www.google.com/search?q=unlinkat+site:docs.sun.com>
|
---|
102 | First, try to simulate it via (unlink|rmdir) ("/proc/self/fd/FD/FILE").
|
---|
103 | Failing that, simulate it via save_cwd/fchdir/(unlink|rmdir)/restore_cwd.
|
---|
104 | If either the save_cwd or the restore_cwd fails (relatively unlikely),
|
---|
105 | then give a diagnostic and exit nonzero.
|
---|
106 | Otherwise, this function works just like Solaris' unlinkat. */
|
---|
107 |
|
---|
108 | # define AT_FUNC_NAME unlinkat
|
---|
109 | # define AT_FUNC_F1 rmdir
|
---|
110 | # define AT_FUNC_F2 unlink
|
---|
111 | # define AT_FUNC_USE_F1_COND AT_REMOVEDIR
|
---|
112 | # define AT_FUNC_POST_FILE_PARAM_DECLS , int flag
|
---|
113 | # define AT_FUNC_POST_FILE_ARGS /* empty */
|
---|
114 | # include "at-func.c"
|
---|
115 | # undef AT_FUNC_NAME
|
---|
116 | # undef AT_FUNC_F1
|
---|
117 | # undef AT_FUNC_F2
|
---|
118 | # undef AT_FUNC_USE_F1_COND
|
---|
119 | # undef AT_FUNC_POST_FILE_PARAM_DECLS
|
---|
120 | # undef AT_FUNC_POST_FILE_ARGS
|
---|
121 |
|
---|
122 | #endif /* !HAVE_UNLINKAT */
|
---|