X-Git-Url: https://git.cworth.org/git?p=tar;a=blobdiff_plain;f=gnu%2Funlinkat.c;fp=gnu%2Funlinkat.c;h=0d68213ee4cad4b736a79da442a4dba9da95fcc4;hp=0000000000000000000000000000000000000000;hb=22f1eb8bc17e5be72dd23d42d6aaa60196ac22e6;hpb=00fa13ff3f2d5b6e2a94c5e948c38616ff7ad37a diff --git a/gnu/unlinkat.c b/gnu/unlinkat.c new file mode 100644 index 0000000..0d68213 --- /dev/null +++ b/gnu/unlinkat.c @@ -0,0 +1,107 @@ +/* -*- buffer-read-only: t -*- vi: set ro: */ +/* DO NOT EDIT! GENERATED AUTOMATICALLY! */ +/* Work around unlinkat bugs on Solaris 9. + + Copyright (C) 2009-2010 Free Software Foundation, Inc. + + This program is free software: you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation; either version 3 of the License, or + (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program. If not, see . */ + +/* Written by Eric Blake. */ + +#include + +#include + +#include +#include +#include +#include + +#include "openat.h" + +#if HAVE_UNLINKAT + +# undef unlinkat + +/* unlinkat without AT_REMOVEDIR does not honor trailing / on Solaris + 9. Solve it in a similar manner to unlink. */ + +int +rpl_unlinkat (int fd, char const *name, int flag) +{ + size_t len; + int result = 0; + /* rmdir behavior has no problems with trailing slash. */ + if (flag & AT_REMOVEDIR) + return unlinkat (fd, name, flag); + + len = strlen (name); + if (len && ISSLASH (name[len - 1])) + { + /* See the lengthy comment in unlink.c why we disobey the POSIX + rule of letting unlink("link-to-dir/") attempt to unlink a + directory. */ + struct stat st; + result = lstatat (fd, name, &st); + if (result == 0) + { + /* Trailing NUL will overwrite the trailing slash. */ + char *short_name = malloc (len); + if (!short_name) + { + errno = EPERM; + return -1; + } + memcpy (short_name, name, len); + while (len && ISSLASH (short_name[len - 1])) + short_name[--len] = '\0'; + if (len && (lstatat (fd, short_name, &st) || S_ISLNK (st.st_mode))) + { + free (short_name); + errno = EPERM; + return -1; + } + free (short_name); + } + } + if (!result) + result = unlinkat (fd, name, flag); + return result; +} + +#else /* !HAVE_UNLINKAT */ + +/* Replacement for Solaris' function by the same name. + + First, try to simulate it via (unlink|rmdir) ("/proc/self/fd/FD/FILE"). + Failing that, simulate it via save_cwd/fchdir/(unlink|rmdir)/restore_cwd. + If either the save_cwd or the restore_cwd fails (relatively unlikely), + then give a diagnostic and exit nonzero. + Otherwise, this function works just like Solaris' unlinkat. */ + +# define AT_FUNC_NAME unlinkat +# define AT_FUNC_F1 rmdir +# define AT_FUNC_F2 unlink +# define AT_FUNC_USE_F1_COND AT_REMOVEDIR +# define AT_FUNC_POST_FILE_PARAM_DECLS , int flag +# define AT_FUNC_POST_FILE_ARGS /* empty */ +# include "at-func.c" +# undef AT_FUNC_NAME +# undef AT_FUNC_F1 +# undef AT_FUNC_F2 +# undef AT_FUNC_USE_F1_COND +# undef AT_FUNC_POST_FILE_PARAM_DECLS +# undef AT_FUNC_POST_FILE_ARGS + +#endif /* !HAVE_UNLINKAT */