6340d777466790929bd0fc2278b0896072947276
[debian/tar] / gnu / unlinkat.c
1 /* -*- buffer-read-only: t -*- vi: set ro: */
2 /* DO NOT EDIT! GENERATED AUTOMATICALLY! */
3 /* Work around unlinkat bugs on Solaris 9 and Hurd.
4
5    Copyright (C) 2009-2013 Free Software Foundation, Inc.
6
7    This program is free software: you can redistribute it and/or modify
8    it under the terms of the GNU General Public License as published by
9    the Free Software Foundation; either version 3 of the License, or
10    (at your option) any later version.
11
12    This program is distributed in the hope that it will be useful,
13    but WITHOUT ANY WARRANTY; without even the implied warranty of
14    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15    GNU General Public License for more details.
16
17    You should have received a copy of the GNU General Public License
18    along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
19
20 /* Written by Eric Blake.  */
21
22 #include <config.h>
23
24 #include <unistd.h>
25
26 #include <errno.h>
27 #include <fcntl.h>
28 #include <string.h>
29 #include <sys/stat.h>
30
31 #include <stdlib.h>
32
33 #include "dosname.h"
34 #include "openat.h"
35
36 #if HAVE_UNLINKAT
37
38 # undef unlinkat
39
40 /* unlinkat without AT_REMOVEDIR does not honor trailing / on Solaris
41    9.  Solve it in a similar manner to unlink.  Hurd has the same
42    issue. */
43
44 int
45 rpl_unlinkat (int fd, char const *name, int flag)
46 {
47   size_t len;
48   int result = 0;
49   /* rmdir behavior has no problems with trailing slash.  */
50   if (flag & AT_REMOVEDIR)
51     return unlinkat (fd, name, flag);
52
53   len = strlen (name);
54   if (len && ISSLASH (name[len - 1]))
55     {
56       /* See the lengthy comment in unlink.c why we disobey the POSIX
57          rule of letting unlink("link-to-dir/") attempt to unlink a
58          directory.  */
59       struct stat st;
60       result = lstatat (fd, name, &st);
61       if (result == 0)
62         {
63           /* Trailing NUL will overwrite the trailing slash.  */
64           char *short_name = malloc (len);
65           if (!short_name)
66             {
67               errno = EPERM;
68               return -1;
69             }
70           memcpy (short_name, name, len);
71           while (len && ISSLASH (short_name[len - 1]))
72             short_name[--len] = '\0';
73           if (len && (lstatat (fd, short_name, &st) || S_ISLNK (st.st_mode)))
74             {
75               free (short_name);
76               errno = EPERM;
77               return -1;
78             }
79           free (short_name);
80         }
81     }
82   if (!result)
83     result = unlinkat (fd, name, flag);
84   return result;
85 }
86
87 #else /* !HAVE_UNLINKAT */
88
89 /* Replacement for Solaris' function by the same name.
90    <http://www.google.com/search?q=unlinkat+site:docs.sun.com>
91    First, try to simulate it via (unlink|rmdir) ("/proc/self/fd/FD/FILE").
92    Failing that, simulate it via save_cwd/fchdir/(unlink|rmdir)/restore_cwd.
93    If either the save_cwd or the restore_cwd fails (relatively unlikely),
94    then give a diagnostic and exit nonzero.
95    Otherwise, this function works just like Solaris' unlinkat.  */
96
97 # define AT_FUNC_NAME unlinkat
98 # define AT_FUNC_F1 rmdir
99 # define AT_FUNC_F2 unlink
100 # define AT_FUNC_USE_F1_COND AT_REMOVEDIR
101 # define AT_FUNC_POST_FILE_PARAM_DECLS , int flag
102 # define AT_FUNC_POST_FILE_ARGS        /* empty */
103 # include "at-func.c"
104 # undef AT_FUNC_NAME
105 # undef AT_FUNC_F1
106 # undef AT_FUNC_F2
107 # undef AT_FUNC_USE_F1_COND
108 # undef AT_FUNC_POST_FILE_PARAM_DECLS
109 # undef AT_FUNC_POST_FILE_ARGS
110
111 #endif /* !HAVE_UNLINKAT */