add ext4,vfat and tar.bz2
[u-tools.git] / u-tools / apps / tar / gnu / unlinkat.c
blob148f8af34e3d6e86f0d0f2c85b80371ce89a9ced
1 /* -*- buffer-read-only: t -*- vi: set ro: */
2 /* DO NOT EDIT! GENERATED AUTOMATICALLY! */
3 /* Work around unlinkat bugs on Solaris 9.
5 Copyright (C) 2009-2011 Free Software Foundation, Inc.
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.
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.
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/>. */
20 /* Written by Eric Blake. */
22 #include <config.h>
24 #include <unistd.h>
26 #include <errno.h>
27 #include <fcntl.h>
28 #include <string.h>
29 #include <sys/stat.h>
31 #include "dosname.h"
32 #include "openat.h"
34 #if HAVE_UNLINKAT
36 # undef unlinkat
38 /* unlinkat without AT_REMOVEDIR does not honor trailing / on Solaris
39 9. Solve it in a similar manner to unlink. */
41 int
42 rpl_unlinkat (int fd, char const *name, int flag)
44 size_t len;
45 int result = 0;
46 /* rmdir behavior has no problems with trailing slash. */
47 if (flag & AT_REMOVEDIR)
48 return unlinkat (fd, name, flag);
50 len = strlen (name);
51 if (len && ISSLASH (name[len - 1]))
53 /* See the lengthy comment in unlink.c why we disobey the POSIX
54 rule of letting unlink("link-to-dir/") attempt to unlink a
55 directory. */
56 struct stat st;
57 result = lstatat (fd, name, &st);
58 if (result == 0)
60 /* Trailing NUL will overwrite the trailing slash. */
61 char *short_name = malloc (len);
62 if (!short_name)
64 errno = EPERM;
65 return -1;
67 memcpy (short_name, name, len);
68 while (len && ISSLASH (short_name[len - 1]))
69 short_name[--len] = '\0';
70 if (len && (lstatat (fd, short_name, &st) || S_ISLNK (st.st_mode)))
72 free (short_name);
73 errno = EPERM;
74 return -1;
76 free (short_name);
79 if (!result)
80 result = unlinkat (fd, name, flag);
81 return result;
84 #else /* !HAVE_UNLINKAT */
86 /* Replacement for Solaris' function by the same name.
87 <http://www.google.com/search?q=unlinkat+site:docs.sun.com>
88 First, try to simulate it via (unlink|rmdir) ("/proc/self/fd/FD/FILE").
89 Failing that, simulate it via save_cwd/fchdir/(unlink|rmdir)/restore_cwd.
90 If either the save_cwd or the restore_cwd fails (relatively unlikely),
91 then give a diagnostic and exit nonzero.
92 Otherwise, this function works just like Solaris' unlinkat. */
94 # define AT_FUNC_NAME unlinkat
95 # define AT_FUNC_F1 rmdir
96 # define AT_FUNC_F2 unlink
97 # define AT_FUNC_USE_F1_COND AT_REMOVEDIR
98 # define AT_FUNC_POST_FILE_PARAM_DECLS , int flag
99 # define AT_FUNC_POST_FILE_ARGS /* empty */
100 # include "at-func.c"
101 # undef AT_FUNC_NAME
102 # undef AT_FUNC_F1
103 # undef AT_FUNC_F2
104 # undef AT_FUNC_USE_F1_COND
105 # undef AT_FUNC_POST_FILE_PARAM_DECLS
106 # undef AT_FUNC_POST_FILE_ARGS
108 #endif /* !HAVE_UNLINKAT */