Mercurial > hg > octave-lyh > gnulib-hg
changeset 11993:767d867147de
rmdir: work around cygwin 1.5.x and mingw bugs
* m4/rmdir.m4 (gl_FUNC_RMDIR): Detect the bugs.
* lib/rmdir.c (rmdir): Work around it.
* modules/rmdir (Status, Notice): No longer obsolete.
(Files): Add dos.m4.
(Depends-on): Add unistd.
(configure.ac): Set witnesses.
(License): Relax to LGPLv2+.
* m4/unistd_h.m4 (gl_UNISTD_H_DEFAULTS): Set defaults.
* modules/unistd (Makefile.am): Substitute witnesses.
* lib/unistd.in.h (rmdir): Declare replacement.
* doc/posix-functions/rmdir.texi (rmdir): Document this.
* modules/rmdir-tests: New tests.
* tests/test-rmdir.c: Likewise.
Signed-off-by: Eric Blake <ebb9@byu.net>
author | Eric Blake <ebb9@byu.net> |
---|---|
date | Wed, 16 Sep 2009 10:08:55 -0600 |
parents | a1e0b2efc812 |
children | 5fc17e46270b |
files | ChangeLog doc/posix-functions/rmdir.texi lib/rmdir.c lib/unistd.in.h m4/rmdir.m4 m4/unistd_h.m4 modules/rmdir modules/rmdir-tests modules/unistd tests/test-rmdir.c |
diffstat | 10 files changed, 256 insertions(+), 16 deletions(-) [+] |
line wrap: on
line diff
--- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,20 @@ +2009-09-16 Eric Blake <ebb9@byu.net> + + rmdir: work around cygwin 1.5.x and mingw bugs + * m4/rmdir.m4 (gl_FUNC_RMDIR): Detect the bugs. + * lib/rmdir.c (rmdir): Work around it. + * modules/rmdir (Status, Notice): No longer obsolete. + (Files): Add dos.m4. + (Depends-on): Add unistd. + (configure.ac): Set witnesses. + (License): Relax to LGPLv2+. + * m4/unistd_h.m4 (gl_UNISTD_H_DEFAULTS): Set defaults. + * modules/unistd (Makefile.am): Substitute witnesses. + * lib/unistd.in.h (rmdir): Declare replacement. + * doc/posix-functions/rmdir.texi (rmdir): Document this. + * modules/rmdir-tests: New tests. + * tests/test-rmdir.c: Likewise. + 2009-09-15 Eric Blake <ebb9@byu.net> fchdir: improve use of replacement functions
--- a/doc/posix-functions/rmdir.texi +++ b/doc/posix-functions/rmdir.texi @@ -9,6 +9,14 @@ Portability problems fixed by Gnulib: @itemize @item +This function mistakenly removes a directory with +@code{rmdir("dir/./")} on some platforms: +Cygwin 1.5.x. +@item +This function fails with @code{EINVAL} instead of the expected +@code{ENOTDIR} for @code{rmdir("file/")} on some platforms: +mingw. +@item This function is missing on some old platforms. @end itemize @@ -17,4 +25,9 @@ @item When @code{rmdir} fails because the specified directory is not empty, the @code{errno} value is system dependent. +@item +POSIX requires that @code{rmdir("link-to-empty/")} remove @file{empty} +and leave @file{link-to-empty} as a dangling symlink. This is +counter-intuitive, so some systems fail with @code{ENOTDIR} instead: +glibc @end itemize
--- a/lib/rmdir.c +++ b/lib/rmdir.c @@ -1,6 +1,6 @@ -/* BSD compatible remove directory function for System V +/* Work around rmdir bugs. - Copyright (C) 1988, 1990, 1999, 2003, 2004, 2005, 2006 Free + Copyright (C) 1988, 1990, 1999, 2003, 2004, 2005, 2006, 2009 Free Software Foundation, Inc. This program is free software: you can redistribute it and/or modify @@ -18,18 +18,43 @@ #include <config.h> -#include <sys/types.h> -#include <sys/stat.h> +#include <unistd.h> + #include <errno.h> +#include <string.h> +#include <sys/stat.h> +#include <sys/types.h> -/* rmdir adapted from GNU tar. */ +#undef rmdir /* Remove directory DIR. Return 0 if successful, -1 if not. */ int -rmdir (char const *dir) +rpl_rmdir (char const *dir) { +#if HAVE_RMDIR + /* Work around cygwin 1.5.x bug where rmdir("dir/./") succeeds. */ + size_t len = strlen (dir); + int result; + while (len && ISSLASH (dir[len - 1])) + len--; + if (len && dir[len - 1] == '.' && (1 == len || ISSLASH (dir[len - 2]))) + { + errno = EINVAL; + return -1; + } + result = rmdir (dir); + /* Work around mingw bug, where rmdir("file/") fails with EINVAL + instead of ENOTDIR. We've already filtered out trailing ., the + only reason allowed by POSIX for EINVAL. */ + if (result == -1 && errno == EINVAL) + errno = ENOTDIR; + return result; + +#else /* !HAVE_RMDIR */ + /* rmdir adapted from GNU tar. FIXME: Delete this implementation in + 2010 if no one reports a system with missing rmdir. */ pid_t cpid; int status; struct stat statbuf; @@ -70,4 +95,5 @@ } return 0; } +#endif /* !HAVE_RMDIR */ }
--- a/lib/unistd.in.h +++ b/lib/unistd.in.h @@ -672,6 +672,21 @@ #endif +#if @GNULIB_RMDIR@ +# if @REPLACE_RMDIR@ +# define rmdir rpl_rmdir +/* Remove the directory DIR. */ +extern int rmdir (char const *name); +# endif +#elif defined GNULIB_POSIXCHECK +# undef rmdir +# define rmdir(n) \ + (GL_LINK_WARNING ("rmdir is unportable - " \ + "use gnulib module rmdir for portability"), \ + rmdir (n)) +#endif + + #if @GNULIB_SLEEP@ /* Pause the execution of the current thread for N seconds. Returns the number of seconds left to sleep.
--- a/m4/rmdir.m4 +++ b/m4/rmdir.m4 @@ -1,4 +1,4 @@ -# rmdir.m4 serial 4 +# rmdir.m4 serial 5 dnl Copyright (C) 2002, 2005, 2009 Free Software Foundation, Inc. dnl This file is free software; the Free Software Foundation dnl gives unlimited permission to copy and/or distribute it, @@ -6,5 +6,37 @@ AC_DEFUN([gl_FUNC_RMDIR], [ + AC_REQUIRE([gl_AC_DOS]) + dnl FIXME: simplify this module in 2010 if no one reports a missing rmdir AC_REPLACE_FUNCS([rmdir]) + if test $ac_cv_func_rmdir = no; then + REPLACE_RMDIR=1 + # If someone lacks rmdir, make configure fail, and request + # a bug report to inform us about it. + if test x"$SKIP_RMDIR_CHECK" != xyes; then + AC_MSG_FAILURE([Your system lacks the rmdir function. + Please report this, along with the output of "uname -a", to the + bug-coreutils@gnu.org mailing list. To continue past this point, + rerun configure with SKIP_RMDIR_CHECK=yes. + E.g., ./configure SKIP_RMDIR_CHECK=yes]) + fi + else + dnl Detect cygwin 1.5.x bug. + AC_CACHE_CHECK([whether rmdir works], [gl_cv_func_rmdir_works], + [mkdir conftest.dir + touch conftest.file + AC_RUN_IFELSE( + [AC_LANG_PROGRAM( + [[#include <stdio.h> + #include <errno.h> +]], [[return !rmdir ("conftest.file/") || errno != ENOTDIR + || !rmdir ("conftest.dir/./");]])], + [gl_cv_func_rmdir_works=yes], [gl_cv_func_rmdir_works=no], + [gl_cv_func_rmdir_works="guessing no"]) + rm -rf conftest.dir conftest.file]) + if test x"$gl_cv_func_rmdir_works" != xyes; then + REPLACE_RMDIR=1 + AC_LIBOBJ([rmdir]) + fi + fi ])
--- a/m4/unistd_h.m4 +++ b/m4/unistd_h.m4 @@ -1,4 +1,4 @@ -# unistd_h.m4 serial 26 +# unistd_h.m4 serial 27 dnl Copyright (C) 2006-2009 Free Software Foundation, Inc. dnl This file is free software; the Free Software Foundation dnl gives unlimited permission to copy and/or distribute it, @@ -56,6 +56,7 @@ GNULIB_PIPE2=0; AC_SUBST([GNULIB_PIPE2]) GNULIB_READLINK=0; AC_SUBST([GNULIB_READLINK]) GNULIB_READLINKAT=0; AC_SUBST([GNULIB_READLINKAT]) + GNULIB_RMDIR=0; AC_SUBST([GNULIB_RMDIR]) GNULIB_SLEEP=0; AC_SUBST([GNULIB_SLEEP]) GNULIB_SYMLINKAT=0; AC_SUBST([GNULIB_SYMLINKAT]) GNULIB_UNISTD_H_GETOPT=0; AC_SUBST([GNULIB_UNISTD_H_GETOPT]) @@ -97,6 +98,7 @@ REPLACE_LCHOWN=0; AC_SUBST([REPLACE_LCHOWN]) REPLACE_LINK=0; AC_SUBST([REPLACE_LINK]) REPLACE_LSEEK=0; AC_SUBST([REPLACE_LSEEK]) + REPLACE_RMDIR=0; AC_SUBST([REPLACE_RMDIR]) REPLACE_WRITE=0; AC_SUBST([REPLACE_WRITE]) UNISTD_H_HAVE_WINSOCK2_H=0; AC_SUBST([UNISTD_H_HAVE_WINSOCK2_H]) UNISTD_H_HAVE_WINSOCK2_H_AND_USE_SOCKETS=0;
--- a/modules/rmdir +++ b/modules/rmdir @@ -1,21 +1,18 @@ Description: rmdir() function: delete a directory. -Status: -obsolete - -Notice: -This module is obsolete. - Files: lib/rmdir.c +m4/dos.m4 m4/rmdir.m4 Depends-on: sys_stat +unistd configure.ac: gl_FUNC_RMDIR +gl_UNISTD_MODULE_INDICATOR([rmdir]) Makefile.am: @@ -23,7 +20,7 @@ <unistd.h> License: -GPL +LGPLv2+ Maintainer: -Jim Meyering +Jim Meyering, Eric Blake
new file mode 100644 --- /dev/null +++ b/modules/rmdir-tests @@ -0,0 +1,11 @@ +Files: +tests/test-rmdir.c + +Depends-on: + +configure.ac: +AC_CHECK_FUNCS_ONCE([symlink]) + +Makefile.am: +TESTS += test-rmdir +check_PROGRAMS += test-rmdir
--- a/modules/unistd +++ b/modules/unistd @@ -49,6 +49,7 @@ -e 's|@''GNULIB_PIPE2''@|$(GNULIB_PIPE2)|g' \ -e 's|@''GNULIB_READLINK''@|$(GNULIB_READLINK)|g' \ -e 's|@''GNULIB_READLINKAT''@|$(GNULIB_READLINKAT)|g' \ + -e 's|@''GNULIB_RMDIR''@|$(GNULIB_RMDIR)|g' \ -e 's|@''GNULIB_SLEEP''@|$(GNULIB_SLEEP)|g' \ -e 's|@''GNULIB_SYMLINKAT''@|$(GNULIB_SYMLINKAT)|g' \ -e 's|@''GNULIB_UNISTD_H_GETOPT''@|$(GNULIB_UNISTD_H_GETOPT)|g' \ @@ -89,6 +90,7 @@ -e 's|@''REPLACE_LCHOWN''@|$(REPLACE_LCHOWN)|g' \ -e 's|@''REPLACE_LINK''@|$(REPLACE_LINK)|g' \ -e 's|@''REPLACE_LSEEK''@|$(REPLACE_LSEEK)|g' \ + -e 's|@''REPLACE_RMDIR''@|$(REPLACE_RMDIR)|g' \ -e 's|@''REPLACE_WRITE''@|$(REPLACE_WRITE)|g' \ -e 's|@''UNISTD_H_HAVE_WINSOCK2_H''@|$(UNISTD_H_HAVE_WINSOCK2_H)|g' \ -e 's|@''UNISTD_H_HAVE_WINSOCK2_H_AND_USE_SOCKETS''@|$(UNISTD_H_HAVE_WINSOCK2_H_AND_USE_SOCKETS)|g' \
new file mode 100644 --- /dev/null +++ b/tests/test-rmdir.c @@ -0,0 +1,125 @@ +/* Tests of rmdir. + Copyright (C) 2009 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 <http://www.gnu.org/licenses/>. */ + +/* Written by Eric Blake <ebb9@byu.net>, 2009. */ + +#include <config.h> + +#include <unistd.h> + +#include <fcntl.h> +#include <errno.h> +#include <stdio.h> +#include <stdlib.h> +#include <sys/stat.h> + +#if !HAVE_SYMLINK +# define symlink(a,b) (-1) +#endif + +#define ASSERT(expr) \ + do \ + { \ + if (!(expr)) \ + { \ + fprintf (stderr, "%s:%d: assertion failed\n", __FILE__, __LINE__); \ + fflush (stderr); \ + abort (); \ + } \ + } \ + while (0) + +#define BASE "test-rmdir.t" + +int +main () +{ + /* Remove any leftovers from a previous partial run. */ + ASSERT (system ("rm -rf " BASE "*") == 0); + + /* Setup. */ + ASSERT (mkdir (BASE "dir", 0700) == 0); + ASSERT (close (creat (BASE "dir/file", 0600)) == 0); + + /* Basic error conditions. */ + errno = 0; + ASSERT (rmdir ("") == -1); + ASSERT (errno == ENOENT); + errno = 0; + ASSERT (rmdir (BASE "nosuch") == -1); + ASSERT (errno == ENOENT); + errno = 0; + ASSERT (rmdir (BASE "nosuch/") == -1); + ASSERT (errno == ENOENT); + errno = 0; + ASSERT (rmdir (".") == -1); + ASSERT (errno == EINVAL || errno == EBUSY); + /* Resulting errno after ".." or "/" is too varied to test; it is + reasonable to see any of EINVAL, EBUSY, EEXIST, ENOTEMPTY, + EACCES, EPERM. */ + ASSERT (rmdir ("..") == -1); + ASSERT (rmdir ("/") == -1); + ASSERT (rmdir ("///") == -1); + errno = 0; + ASSERT (rmdir (BASE "dir/file/") == -1); + ASSERT (errno == ENOTDIR); + + /* Non-empty directory. */ + errno = 0; + ASSERT (rmdir (BASE "dir") == -1); + ASSERT (errno == EEXIST || errno == ENOTEMPTY); + + /* Non-directory. */ + errno = 0; + ASSERT (rmdir (BASE "dir/file") == -1); + ASSERT (errno == ENOTDIR); + + /* Empty directory. */ + ASSERT (unlink (BASE "dir/file") == 0); + errno = 0; + ASSERT (rmdir (BASE "dir/./") == -1); + ASSERT (errno == EINVAL || errno == EBUSY); + ASSERT (rmdir (BASE "dir") == 0); + + /* Test symlink behavior. Specifying trailing slash should remove + referent directory (POSIX), or cause ENOTDIR failure (Linux), but + not touch symlink. We prefer the Linux behavior for its + intuitiveness (especially compared to rmdir("symlink-to-file/")), + but not enough to penalize POSIX systems with an rpl_rmdir. */ + if (symlink (BASE "dir", BASE "link") != 0) + { + fputs ("skipping test: symlinks not supported on this filesystem\n", + stderr); + return 77; + } + ASSERT (mkdir (BASE "dir", 0700) == 0); + errno = 0; + if (rmdir (BASE "link/") == 0) + { + struct stat st; + errno = 0; + ASSERT (stat (BASE "link", &st) == -1); + ASSERT (errno == ENOENT); + } + else + { + ASSERT (errno == ENOTDIR); + ASSERT (rmdir (BASE "dir") == 0); + } + ASSERT (unlink (BASE "link") == 0); + + return 0; +}