symlink: new module, for Solaris 9 bug
authorEric Blake <ebb9@byu.net>
Sun, 20 Sep 2009 03:20:42 +0000 (21:20 -0600)
committerEric Blake <ebb9@byu.net>
Wed, 23 Sep 2009 10:57:08 +0000 (04:57 -0600)
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 <ebb9@byu.net>
ChangeLog
MODULES.html.sh
doc/posix-functions/symlink.texi
lib/symlink.c [new file with mode: 0644]
lib/unistd.in.h
m4/symlink.m4 [new file with mode: 0644]
m4/unistd_h.m4
modules/symlink [new file with mode: 0644]
modules/symlink-tests [new file with mode: 0644]
modules/unistd
tests/test-symlink.c [new file with mode: 0644]

index 2d9b9ab..386e3b5 100644 (file)
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,17 @@
+2009-09-23  Eric Blake  <ebb9@byu.net>
+
+       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  <bruno@clisp.org>
 
        * gnulib-tool (func_import): Add 'link-warning' to testsrelated_modules
index 9104d54..ad00ac8 100755 (executable)
@@ -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
index e947d2f..de7c0aa 100644 (file)
@@ -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 (file)
index 0000000..89ddddb
--- /dev/null
@@ -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 <http://www.gnu.org/licenses/>.  */
+
+#include <config.h>
+
+/* Specification.  */
+#include <unistd.h>
+
+#include <errno.h>
+#include <string.h>
+#include <sys/stat.h>
+
+
+#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 */
index 12b791b..76d987e 100644 (file)
@@ -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 (file)
index 0000000..abea045
--- /dev/null
@@ -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 <unistd.h>
+]], [[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
+])
index 648fd86..0a263d2 100644 (file)
@@ -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 (file)
index 0000000..4276036
--- /dev/null
@@ -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:
+<unistd.h>
+
+License:
+LGPL
+
+Maintainer:
+Eric Blake
diff --git a/modules/symlink-tests b/modules/symlink-tests
new file mode 100644 (file)
index 0000000..aa67405
--- /dev/null
@@ -0,0 +1,10 @@
+Files:
+tests/test-symlink.c
+
+Depends-on:
+
+configure.ac:
+
+Makefile.am:
+TESTS += test-symlink
+check_PROGRAMS += test-symlink
index 6cc6cda..d2568b2 100644 (file)
@@ -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 (file)
index 0000000..9f9dda1
--- /dev/null
@@ -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 <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>
+
+#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;
+}