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