From d9d5054ceb34c7e5b1dc64e5477ac6f89969a423 Mon Sep 17 00:00:00 2001 From: Eric Blake Date: Sat, 19 Sep 2009 21:20:42 -0600 Subject: [PATCH] symlink: new module, for Solaris 9 bug symlink("a","link/") mistakenly succeeds. * modules/symlink: New file. * m4/symlink.m4 (gl_FUNC_SYMLINK): Likewise. * lib/symlink.c: Likewise. * m4/unistd_h.m4 (gl_UNISTD_H_DEFAULTS): Add defaults. * modules/unistd (Makefile.am): Substitute them. * lib/unistd.in.h (symlink): Declare replacement. * MODULES.html.sh (File system functions): Mention it. * doc/posix-functions/symlink.texi (symlink): Likewise. * modules/symlink-tests: New test. * tests/test-symlink.c: Likewise. Signed-off-by: Eric Blake --- ChangeLog | 14 ++++++ MODULES.html.sh | 1 + doc/posix-functions/symlink.texi | 13 ++++-- lib/symlink.c | 57 +++++++++++++++++++++++ lib/unistd.in.h | 17 +++++++ m4/symlink.m4 | 35 +++++++++++++++ m4/unistd_h.m4 | 3 ++ modules/symlink | 25 +++++++++++ modules/symlink-tests | 10 +++++ modules/unistd | 3 ++ tests/test-symlink.c | 97 ++++++++++++++++++++++++++++++++++++++++ 11 files changed, 272 insertions(+), 3 deletions(-) create mode 100644 lib/symlink.c create mode 100644 m4/symlink.m4 create mode 100644 modules/symlink create mode 100644 modules/symlink-tests create mode 100644 tests/test-symlink.c diff --git a/ChangeLog b/ChangeLog index 2d9b9ab8b..386e3b51a 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,17 @@ +2009-09-23 Eric Blake + + symlink: new module, for Solaris 9 bug + * modules/symlink: New file. + * m4/symlink.m4 (gl_FUNC_SYMLINK): Likewise. + * lib/symlink.c: Likewise. + * m4/unistd_h.m4 (gl_UNISTD_H_DEFAULTS): Add defaults. + * modules/unistd (Makefile.am): Substitute them. + * lib/unistd.in.h (symlink): Declare replacement. + * MODULES.html.sh (File system functions): Mention it. + * doc/posix-functions/symlink.texi (symlink): Likewise. + * modules/symlink-tests: New test. + * tests/test-symlink.c: Likewise. + 2009-09-23 Bruno Haible * gnulib-tool (func_import): Add 'link-warning' to testsrelated_modules diff --git a/MODULES.html.sh b/MODULES.html.sh index 9104d5427..ad00ac883 100755 --- a/MODULES.html.sh +++ b/MODULES.html.sh @@ -2482,6 +2482,7 @@ func_all_modules () func_module savewd func_module stat-macros func_module stat-time + func_module symlink func_module symlinkat func_module tmpdir func_module unlinkdir diff --git a/doc/posix-functions/symlink.texi b/doc/posix-functions/symlink.texi index e947d2ff3..de7c0aa44 100644 --- a/doc/posix-functions/symlink.texi +++ b/doc/posix-functions/symlink.texi @@ -4,15 +4,22 @@ POSIX specification: @url{http://www.opengroup.org/onlinepubs/9699919799/functions/symlink.html} -Gnulib module: --- +Gnulib module: symlink Portability problems fixed by Gnulib: @itemize +@item +On some systems, @code{symlink(value,"name/")} mistakenly creates a +symlink: +Solaris 9 +@item +This function is missing on some platforms; however, the replacement +always fails with @code{EPERM}: +mingw. @end itemize Portability problems not fixed by Gnulib: @itemize @item -This function is missing on some platforms: -mingw. +Some file systems do not support symbolic links. @end itemize diff --git a/lib/symlink.c b/lib/symlink.c new file mode 100644 index 000000000..89ddddb6f --- /dev/null +++ b/lib/symlink.c @@ -0,0 +1,57 @@ +/* Stub for symlink(). + 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 . */ + +#include + +/* Specification. */ +#include + +#include +#include +#include + + +#if HAVE_SYMLINK + +# undef symlink + +/* Create a symlink, but reject trailing slash. */ +int +rpl_symlink (char const *contents, char const *name) +{ + size_t len = strlen (name); + if (len && name[len - 1] == '/') + { + struct stat st; + if (lstat (name, &st) == 0) + errno = EEXIST; + return -1; + } + return symlink (contents, name); +} + +#else /* !HAVE_SYMLINK */ + +/* The system does not support symlinks. */ +int +symlink (char const *contents _UNUSED_PARAMETER_, + char const *name _UNUSED_PARAMETER_) +{ + errno = ENOSYS; + return -1; +} + +#endif /* !HAVE_SYMLINK */ diff --git a/lib/unistd.in.h b/lib/unistd.in.h index 12b791b0b..76d987e5f 100644 --- a/lib/unistd.in.h +++ b/lib/unistd.in.h @@ -683,6 +683,23 @@ extern unsigned int sleep (unsigned int n); #endif +#if @GNULIB_SYMLINK@ +# if @REPLACE_SYMLINK@ +# undef symlink +# define symlink rpl_symlink +# endif +# if !@HAVE_SYMLINK@ || @REPLACE_SYMLINK@ +int symlink (char const *contents, char const *file); +# endif +#elif defined GNULIB_POSIXCHECK +# undef symlink +# define symlink(c,n) \ + (GL_LINK_WARNING ("symlink is not portable - " \ + "use gnulib module symlink for portability"), \ + symlink (c, n)) +#endif + + #if @GNULIB_SYMLINKAT@ # if !@HAVE_SYMLINKAT@ int symlinkat (char const *contents, int fd, char const *file); diff --git a/m4/symlink.m4 b/m4/symlink.m4 new file mode 100644 index 000000000..abea0459a --- /dev/null +++ b/m4/symlink.m4 @@ -0,0 +1,35 @@ +# serial 1 +# See if we need to provide symlink replacement. + +dnl Copyright (C) 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, +dnl with or without modifications, as long as this notice is preserved. + +# Written by Eric Blake. + +AC_DEFUN([gl_FUNC_SYMLINK], +[ + AC_REQUIRE([gl_UNISTD_H_DEFAULTS]) + AC_CHECK_FUNCS_ONCE([symlink]) + dnl The best we can do on mingw is provide a dummy that always fails, so + dnl that compilation can proceed with fewer ifdefs. On Solaris 9, we + dnl want to fix a bug with trailing slash handling. + if test $ac_cv_func_symlink = no; then + HAVE_SYMLINK=0 + AC_LIBOBJ([symlink]) + else + AC_CACHE_CHECK([whether symlink handles trailing slash correctly], + [gl_cv_func_symlink_works], + [AC_RUN_IFELSE( + [AC_LANG_PROGRAM( + [[#include +]], [[return !symlink ("a", "conftest.link/");]])], + [gl_cv_func_symlink_works=yes], [gl_cv_func_symlink_works=no], + [gl_cv_func_symlink_works="guessing no"])]) + if test "$gl_cv_func_symlink_works" != yes; then + REPLACE_SYMLINK=1 + AC_LIBOBJ([symlink]) + fi + fi +]) diff --git a/m4/unistd_h.m4 b/m4/unistd_h.m4 index 648fd86a6..0a263d2fd 100644 --- a/m4/unistd_h.m4 +++ b/m4/unistd_h.m4 @@ -58,6 +58,7 @@ AC_DEFUN([gl_UNISTD_H_DEFAULTS], GNULIB_READLINKAT=0; AC_SUBST([GNULIB_READLINKAT]) GNULIB_RMDIR=0; AC_SUBST([GNULIB_RMDIR]) GNULIB_SLEEP=0; AC_SUBST([GNULIB_SLEEP]) + GNULIB_SYMLINK=0; AC_SUBST([GNULIB_SYMLINK]) GNULIB_SYMLINKAT=0; AC_SUBST([GNULIB_SYMLINKAT]) GNULIB_UNISTD_H_GETOPT=0; AC_SUBST([GNULIB_UNISTD_H_GETOPT]) GNULIB_UNISTD_H_SIGPIPE=0; AC_SUBST([GNULIB_UNISTD_H_SIGPIPE]) @@ -82,6 +83,7 @@ AC_DEFUN([gl_UNISTD_H_DEFAULTS], HAVE_READLINK=1; AC_SUBST([HAVE_READLINK]) HAVE_READLINKAT=1; AC_SUBST([HAVE_READLINKAT]) HAVE_SLEEP=1; AC_SUBST([HAVE_SLEEP]) + HAVE_SYMLINK=1; AC_SUBST([HAVE_SYMLINK]) HAVE_SYMLINKAT=1; AC_SUBST([HAVE_SYMLINKAT]) HAVE_DECL_ENVIRON=1; AC_SUBST([HAVE_DECL_ENVIRON]) HAVE_DECL_GETLOGIN_R=1; AC_SUBST([HAVE_DECL_GETLOGIN_R]) @@ -100,6 +102,7 @@ AC_DEFUN([gl_UNISTD_H_DEFAULTS], REPLACE_LINK=0; AC_SUBST([REPLACE_LINK]) REPLACE_LSEEK=0; AC_SUBST([REPLACE_LSEEK]) REPLACE_RMDIR=0; AC_SUBST([REPLACE_RMDIR]) + REPLACE_SYMLINK=0; AC_SUBST([REPLACE_SYMLINK]) REPLACE_UNLINK=0; AC_SUBST([REPLACE_UNLINK]) REPLACE_UNLINKAT=0; AC_SUBST([REPLACE_UNLINKAT]) REPLACE_WRITE=0; AC_SUBST([REPLACE_WRITE]) diff --git a/modules/symlink b/modules/symlink new file mode 100644 index 000000000..4276036fe --- /dev/null +++ b/modules/symlink @@ -0,0 +1,25 @@ +Description: +symlink(): create a symlink, if possible + +Files: +lib/symlink.c +m4/symlink.m4 + +Depends-on: +lstat +unistd + +configure.ac: +gl_FUNC_SYMLINK +gl_UNISTD_MODULE_INDICATOR([symlink]) + +Makefile.am: + +Include: + + +License: +LGPL + +Maintainer: +Eric Blake diff --git a/modules/symlink-tests b/modules/symlink-tests new file mode 100644 index 000000000..aa67405d8 --- /dev/null +++ b/modules/symlink-tests @@ -0,0 +1,10 @@ +Files: +tests/test-symlink.c + +Depends-on: + +configure.ac: + +Makefile.am: +TESTS += test-symlink +check_PROGRAMS += test-symlink diff --git a/modules/unistd b/modules/unistd index 6cc6cda12..d2568b20b 100644 --- a/modules/unistd +++ b/modules/unistd @@ -51,6 +51,7 @@ unistd.h: unistd.in.h -e 's|@''GNULIB_READLINKAT''@|$(GNULIB_READLINKAT)|g' \ -e 's|@''GNULIB_RMDIR''@|$(GNULIB_RMDIR)|g' \ -e 's|@''GNULIB_SLEEP''@|$(GNULIB_SLEEP)|g' \ + -e 's|@''GNULIB_SYMLINK''@|$(GNULIB_SYMLINK)|g' \ -e 's|@''GNULIB_SYMLINKAT''@|$(GNULIB_SYMLINKAT)|g' \ -e 's|@''GNULIB_UNISTD_H_GETOPT''@|$(GNULIB_UNISTD_H_GETOPT)|g' \ -e 's|@''GNULIB_UNISTD_H_SIGPIPE''@|$(GNULIB_UNISTD_H_SIGPIPE)|g' \ @@ -74,6 +75,7 @@ unistd.h: unistd.in.h -e 's|@''HAVE_READLINK''@|$(HAVE_READLINK)|g' \ -e 's|@''HAVE_READLINKAT''@|$(HAVE_READLINKAT)|g' \ -e 's|@''HAVE_SLEEP''@|$(HAVE_SLEEP)|g' \ + -e 's|@''HAVE_SYMLINK''@|$(HAVE_SYMLINK)|g' \ -e 's|@''HAVE_SYMLINKAT''@|$(HAVE_SYMLINKAT)|g' \ -e 's|@''HAVE_UNLINKAT''@|$(HAVE_UNLINKAT)|g' \ -e 's|@''HAVE_DECL_ENVIRON''@|$(HAVE_DECL_ENVIRON)|g' \ @@ -92,6 +94,7 @@ unistd.h: unistd.in.h -e 's|@''REPLACE_LINK''@|$(REPLACE_LINK)|g' \ -e 's|@''REPLACE_LSEEK''@|$(REPLACE_LSEEK)|g' \ -e 's|@''REPLACE_RMDIR''@|$(REPLACE_RMDIR)|g' \ + -e 's|@''REPLACE_SYMLINK''@|$(REPLACE_SYMLINK)|g' \ -e 's|@''REPLACE_UNLINK''@|$(REPLACE_UNLINK)|g' \ -e 's|@''REPLACE_UNLINKAT''@|$(REPLACE_UNLINKAT)|g' \ -e 's|@''REPLACE_WRITE''@|$(REPLACE_WRITE)|g' \ diff --git a/tests/test-symlink.c b/tests/test-symlink.c new file mode 100644 index 000000000..9f9dda14c --- /dev/null +++ b/tests/test-symlink.c @@ -0,0 +1,97 @@ +/* Tests of symlink. + 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 . */ + +/* Written by Eric Blake , 2009. */ + +#include + +#include + +#include +#include +#include +#include +#include + +#define ASSERT(expr) \ + do \ + { \ + if (!(expr)) \ + { \ + fprintf (stderr, "%s:%d: assertion failed\n", __FILE__, __LINE__); \ + fflush (stderr); \ + abort (); \ + } \ + } \ + while (0) + +#define BASE "test-symlink.t" + +int +main () +{ + /* Remove any leftovers from a previous partial run. */ + ASSERT (system ("rm -rf " BASE "*") == 0); + + if (symlink ("nowhere", BASE "link1")) + { + fputs ("skipping test: symlinks not supported on this filesystem\n", + stderr); + return 77; + } + + /* Some systems allow the creation of 0-length symlinks as a synonym + for "."; but most reject it. */ + errno = 0; + if (symlink ("", BASE "link2") == -1) + ASSERT (errno == ENOENT || errno == EINVAL); + else + ASSERT (unlink (BASE "link2") == 0); + + /* Sanity checks of failures. */ + errno = 0; + ASSERT (symlink ("nowhere", "") == -1); + ASSERT (errno == ENOENT); + errno = 0; + ASSERT (symlink ("nowhere", ".") == -1); + ASSERT (errno == EEXIST || errno == EINVAL); + errno = 0; + ASSERT (symlink ("somewhere", BASE "link1") == -1); + ASSERT (errno == EEXIST); + errno = 0; + ASSERT (symlink ("nowhere", BASE "link2/") == -1); + ASSERT (errno == ENOTDIR || errno == ENOENT); + ASSERT (mkdir (BASE "dir", 0700) == 0); + errno = 0; + ASSERT (symlink ("nowhere", BASE "dir") == -1); + ASSERT (errno == EEXIST); + errno = 0; + ASSERT (symlink ("nowhere", BASE "dir/") == -1); + ASSERT (errno == EEXIST); + ASSERT (close (creat (BASE "file", 0600)) == 0); + errno = 0; + ASSERT (symlink ("nowhere", BASE "file") == -1); + ASSERT (errno == EEXIST); + errno = 0; + ASSERT (symlink ("nowhere", BASE "file/") == -1); + ASSERT (errno == EEXIST || errno == ENOTDIR); + + ASSERT (rmdir (BASE "dir") == 0); + ASSERT (unlink (BASE "file") == 0); + ASSERT (unlink (BASE "link1") == 0); + + return 0; +} -- 2.11.0