changeset 11041:894d336e6216

New module 'link'.
author Martin Lambers <marlam@marlam.de>
date Sun, 18 Jan 2009 19:28:53 +0100
parents cee1ca4b9566
children 3da9b6c857c2
files ChangeLog doc/posix-functions/link.texi lib/link.c lib/unistd.in.h m4/link.m4 m4/unistd_h.m4 modules/link modules/unistd
diffstat 8 files changed, 167 insertions(+), 8 deletions(-) [+]
line wrap: on
line diff
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,15 @@
+2009-01-18  Martin Lambers  <marlam@marlam.de>
+
+	New module 'link'.
+	* lib/unistd.in.h (link): New declaration.
+	* lib/link.c: New file.
+	* m4/link.m4: New file.
+	* m4/unistd_h.m4 (gl_UNISTD_H_DEFAULTS): Initialize GNULIB_LINK,
+	HAVE_LINK.
+	* modules/unistd (Makefile.am): Substitute GNULIB_LINK, HAVE_LINK.
+	* modules/link: New file.
+	* doc/posix-functions/link.texi: Mention the new module.
+
 2009-01-18  Bruno Haible  <bruno@clisp.org>
 
 	* tests/test-avltree_list.c (main): Call set_program_name.
--- a/doc/posix-functions/link.texi
+++ b/doc/posix-functions/link.texi
@@ -4,15 +4,15 @@
 
 POSIX specification: @url{http://www.opengroup.org/onlinepubs/9699919799/functions/link.html}
 
-Gnulib module: ---
+Gnulib module: link
 
 Portability problems fixed by Gnulib:
 @itemize
-@end itemize
-
-Portability problems not fixed by Gnulib:
-@itemize
 @item
 This function is missing on some platforms:
 mingw.
 @end itemize
+
+Portability problems not fixed by Gnulib:
+@itemize
+@end itemize
new file mode 100644
--- /dev/null
+++ b/lib/link.c
@@ -0,0 +1,83 @@
+/* Emulate link on platforms that lack it, namely native Windows platforms.
+
+   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 2, 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, write to the Free Software Foundation,
+   Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.  */
+
+#include <config.h>
+
+#if (defined _WIN32 || defined __WIN32__) && ! defined __CYGWIN__
+
+#define WIN32_LEAN_AND_MEAN
+#define _WIN32_WINNT 0x0500
+#include <unistd.h>
+#include <windows.h>
+
+#include <errno.h>
+
+int
+link (const char *path1, const char *path2)
+{
+  if (CreateHardLink (path2, path1, NULL) == 0)
+    {
+      /* It is not documented which errors CreateHardLink() can produce.
+       * The following conversions are based on tests on a Windows XP SP2
+       * system. */
+      DWORD err = GetLastError ();
+      switch (err)
+	{
+	case ERROR_ACCESS_DENIED:
+	  errno = EACCES;
+	  break;
+
+	case ERROR_INVALID_FUNCTION:	/* fs does not support hard links */
+	  errno = EPERM;
+	  break;
+
+	case ERROR_NOT_SAME_DEVICE:
+	  errno = EXDEV;
+	  break;
+
+	case ERROR_PATH_NOT_FOUND:
+	case ERROR_FILE_NOT_FOUND:
+	  errno = ENOENT;
+	  break;
+
+	case ERROR_INVALID_PARAMETER:
+	  errno = ENAMETOOLONG;
+	  break;
+
+	case ERROR_TOO_MANY_LINKS:
+	  errno = EMLINK;
+	  break;
+
+	case ERROR_ALREADY_EXISTS:
+	  errno = EEXIST;
+	  break;
+
+	default:
+	  errno = EIO;
+	}
+      return -1;
+    }
+
+  return 0;
+}
+
+#else /* !Windows */
+
+#error "This platform lacks a link function, and Gnulib doesn't provide a replacement. This is a bug in Gnulib."
+
+#endif /* !Windows */
--- a/lib/unistd.in.h
+++ b/lib/unistd.in.h
@@ -1,5 +1,5 @@
 /* Substitute for and wrapper around <unistd.h>.
-   Copyright (C) 2003-2008 Free Software Foundation, Inc.
+   Copyright (C) 2003-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
@@ -475,6 +475,23 @@
 #endif
 
 
+#if @GNULIB_LINK@
+/* Create a new hard link for an existing file.
+   Return 0 if successful, otherwise -1 and errno set.
+   See POSIX:2001 specification
+   <http://www.opengroup.org/susv3xsh/link.html>.  */
+# if !@HAVE_LINK@
+extern int link (const char *path1, const char *path2);
+# endif
+#elif defined GNULIB_POSIXCHECK
+# undef link
+# define link(path1,path2) \
+    (GL_LINK_WARNING ("link is unportable - " \
+                      "use gnulib module link for portability"), \
+     link (path1, path2))
+#endif
+
+
 #if @GNULIB_LSEEK@
 # if @REPLACE_LSEEK@
 /* Set the offset of FD relative to SEEK_SET, SEEK_CUR, or SEEK_END.
new file mode 100644
--- /dev/null
+++ b/m4/link.m4
@@ -0,0 +1,19 @@
+# link.m4 serial 1
+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.
+
+AC_DEFUN([gl_FUNC_LINK],
+[
+  AC_REQUIRE([gl_UNISTD_H_DEFAULTS])
+  AC_CHECK_FUNCS_ONCE([link])
+  if test $ac_cv_func_link = no; then
+    HAVE_LINK=0
+    AC_LIBOBJ([link])
+    gl_PREREQ_LINK
+  fi
+])
+
+# Prerequisites of lib/link.c.
+AC_DEFUN([gl_PREREQ_LINK], [:])
--- a/m4/unistd_h.m4
+++ b/m4/unistd_h.m4
@@ -1,5 +1,5 @@
-# unistd_h.m4 serial 16
-dnl Copyright (C) 2006-2008 Free Software Foundation, Inc.
+# unistd_h.m4 serial 17
+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,
 dnl with or without modifications, as long as this notice is preserved.
@@ -48,6 +48,7 @@
   GNULIB_GETPAGESIZE=0;      AC_SUBST([GNULIB_GETPAGESIZE])
   GNULIB_GETUSERSHELL=0;     AC_SUBST([GNULIB_GETUSERSHELL])
   GNULIB_LCHOWN=0;           AC_SUBST([GNULIB_LCHOWN])
+  GNULIB_LINK=0;             AC_SUBST([GNULIB_LINK])
   GNULIB_LSEEK=0;            AC_SUBST([GNULIB_LSEEK])
   GNULIB_READLINK=0;         AC_SUBST([GNULIB_READLINK])
   GNULIB_SLEEP=0;            AC_SUBST([GNULIB_SLEEP])
@@ -63,6 +64,7 @@
   HAVE_GETHOSTNAME=1;     AC_SUBST([HAVE_GETHOSTNAME])
   HAVE_GETPAGESIZE=1;     AC_SUBST([HAVE_GETPAGESIZE])
   HAVE_GETUSERSHELL=1;    AC_SUBST([HAVE_GETUSERSHELL])
+  HAVE_LINK=1;            AC_SUBST([HAVE_LINK])
   HAVE_READLINK=1;        AC_SUBST([HAVE_READLINK])
   HAVE_SLEEP=1;           AC_SUBST([HAVE_SLEEP])
   HAVE_DECL_ENVIRON=1;    AC_SUBST([HAVE_DECL_ENVIRON])
new file mode 100644
--- /dev/null
+++ b/modules/link
@@ -0,0 +1,24 @@
+Description:
+link() function: create a new link for an existing file
+
+Files:
+lib/link.c
+m4/link.m4
+
+Depends-on:
+unistd
+
+configure.ac:
+gl_FUNC_LINK
+gl_UNISTD_MODULE_INDICATOR([link])
+
+Makefile.am:
+
+Include:
+<unistd.h>
+
+License:
+LGPLv2+
+
+Maintainer:
+Martin Lambers
--- a/modules/unistd
+++ b/modules/unistd
@@ -40,6 +40,7 @@
 	      -e 's|@''GNULIB_GETPAGESIZE''@|$(GNULIB_GETPAGESIZE)|g' \
 	      -e 's|@''GNULIB_GETUSERSHELL''@|$(GNULIB_GETUSERSHELL)|g' \
 	      -e 's|@''GNULIB_LCHOWN''@|$(GNULIB_LCHOWN)|g' \
+	      -e 's|@''GNULIB_LINK''@|$(GNULIB_LINK)|g' \
 	      -e 's|@''GNULIB_LSEEK''@|$(GNULIB_LSEEK)|g' \
 	      -e 's|@''GNULIB_READLINK''@|$(GNULIB_READLINK)|g' \
 	      -e 's|@''GNULIB_SLEEP''@|$(GNULIB_SLEEP)|g' \
@@ -54,6 +55,7 @@
 	      -e 's|@''HAVE_GETHOSTNAME''@|$(HAVE_GETHOSTNAME)|g' \
 	      -e 's|@''HAVE_GETPAGESIZE''@|$(HAVE_GETPAGESIZE)|g' \
 	      -e 's|@''HAVE_GETUSERSHELL''@|$(HAVE_GETUSERSHELL)|g' \
+	      -e 's|@''HAVE_LINK''@|$(HAVE_LINK)|g' \
 	      -e 's|@''HAVE_READLINK''@|$(HAVE_READLINK)|g' \
 	      -e 's|@''HAVE_SLEEP''@|$(HAVE_SLEEP)|g' \
 	      -e 's|@''HAVE_DECL_ENVIRON''@|$(HAVE_DECL_ENVIRON)|g' \