Import upstream version 1.28
[debian/tar] / gnu / unlink.c
1 /* -*- buffer-read-only: t -*- vi: set ro: */
2 /* DO NOT EDIT! GENERATED AUTOMATICALLY! */
3 /* Work around unlink bugs.
4
5    Copyright (C) 2009-2014 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 #include <config.h>
21
22 #include <unistd.h>
23
24 #include <errno.h>
25 #include <stdlib.h>
26 #include <string.h>
27 #include <sys/stat.h>
28
29 #include "dosname.h"
30
31 #undef unlink
32
33 /* Remove file NAME.
34    Return 0 if successful, -1 if not.  */
35
36 int
37 rpl_unlink (char const *name)
38 {
39   /* Work around Solaris 9 bug where unlink("file/") succeeds.  */
40   size_t len = strlen (name);
41   int result = 0;
42   if (len && ISSLASH (name[len - 1]))
43     {
44       /* We can't unlink(2) something if it doesn't exist.  If it does
45          exist, then it resolved to a directory, due to the trailing
46          slash, and POSIX requires that the unlink attempt to remove
47          that directory (which would leave the symlink dangling).
48          Unfortunately, Solaris 9 is one of the platforms where the
49          root user can unlink directories, and we don't want to
50          cripple this behavior on real directories, even if it is
51          seldom needed (at any rate, it's nicer to let coreutils'
52          unlink(1) give the correct errno for non-root users).  But we
53          don't know whether name was an actual directory, or a symlink
54          to a directory; and due to the bug of ignoring trailing
55          slash, Solaris 9 would end up successfully unlinking the
56          symlink instead of the directory.  Technically, we could use
57          realpath to find the canonical directory name to attempt
58          deletion on.  But that is a lot of work for a corner case; so
59          we instead just use an lstat on the shortened name, and
60          reject symlinks with trailing slashes.  The root user of
61          unlink(1) will just have to live with the rule that they
62          can't delete a directory via a symlink.  */
63       struct stat st;
64       result = lstat (name, &st);
65       if (result == 0)
66         {
67           /* Trailing NUL will overwrite the trailing slash.  */
68           char *short_name = malloc (len);
69           if (!short_name)
70             {
71               errno = EPERM;
72               return -1;
73             }
74           memcpy (short_name, name, len);
75           while (len && ISSLASH (short_name[len - 1]))
76             short_name[--len] = '\0';
77           if (len && (lstat (short_name, &st) || S_ISLNK (st.st_mode)))
78             {
79               free (short_name);
80               errno = EPERM;
81               return -1;
82             }
83           free (short_name);
84         }
85     }
86   if (!result)
87     {
88 #if UNLINK_PARENT_BUG
89       if (len >= 2 && name[len - 1] == '.' && name[len - 2] == '.'
90           && (len == 2 || ISSLASH (name[len - 3])))
91         {
92           errno = EISDIR; /* could also use EPERM */
93           return -1;
94         }
95 #endif
96       result = unlink (name);
97     }
98   return result;
99 }