Bruno Haible wrote:
Isn't the problem which you described in the "not fixed
by gnulib" section the same as the bug tested for by the RENAME_HARD_LINK_BUG
test in m4/rename.m4 ? Both are about hard links to the same file, no?

Yes and no. Sorry, I had forgotten about that test. As its comment says, it attempts to detect a bug with rename("a","b") setting st_nlink incorrectly when "a" and "b" are hard links to the same file. This bug is somewhat independent of the issue we're talking about.

That test is too strict, as it rejects the NetBSD 7 behavior. I installed the attached, to try to fix this.
From 6980ecee9d86e1f99f7acd403d696da96bbed0a6 Mon Sep 17 00:00:00 2001
From: Paul Eggert <[email protected]>
Date: Wed, 16 Aug 2017 08:34:41 -0700
Subject: [PATCH] rename: port better to NetBSD
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

* doc/posix-functions/rename.texi (rename): NetBSD 7
does not have the link-count bug.
* m4/rename.m4 (gl_FUNC_RENAME): Don’t consider NetBSD to be
broken merely because rename ("a", "b") removes "a" when the two
names are hard links to the same file.
---
 ChangeLog                       |  9 +++++++++
 doc/posix-functions/rename.texi |  2 +-
 m4/rename.m4                    | 19 ++++++++++++++-----
 3 files changed, 24 insertions(+), 6 deletions(-)

diff --git a/ChangeLog b/ChangeLog
index 259b999..422d0a3 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,12 @@
+2017-08-16  Paul Eggert  <[email protected]>
+
+       rename: port better to NetBSD
+       * doc/posix-functions/rename.texi (rename): NetBSD 7
+       does not have the link-count bug.
+       * m4/rename.m4 (gl_FUNC_RENAME): Don’t consider NetBSD to be
+       broken merely because rename ("a", "b") removes "a" when the two
+       names are hard links to the same file.
+
 2017-08-16  Bruno Haible  <[email protected]>
 
        iconv_open, uni*: Add support for VPATH builds with OpenBSD 'make'.
diff --git a/doc/posix-functions/rename.texi b/doc/posix-functions/rename.texi
index 12c3be4..97c44a5 100644
--- a/doc/posix-functions/rename.texi
+++ b/doc/posix-functions/rename.texi
@@ -28,7 +28,7 @@ Solaris 9.
 @item
 This function incorrectly reduces the link count when comparing two
 spellings of a hard link on some platforms:
-NetBSD 1.6, NetBSD 7.0 without @code{-D_XOPEN_SOURCE=500}, Cygwin 1.5.x.
+NetBSD 1.6, Cygwin 1.5.x.
 @item
 This function will not always replace an existing destination on some
 platforms:
diff --git a/m4/rename.m4 b/m4/rename.m4
index 9a37307..a702b3e 100644
--- a/m4/rename.m4
+++ b/m4/rename.m4
@@ -1,4 +1,4 @@
-# serial 27
+# serial 28
 
 # Copyright (C) 2001, 2003, 2005-2006, 2009-2017 Free Software Foundation, Inc.
 # This file is free software; the Free Software Foundation
@@ -136,11 +136,12 @@ AC_DEFUN([gl_FUNC_RENAME],
   AC_CACHE_CHECK([whether rename manages hard links correctly],
     [gl_cv_func_rename_link_works],
     [if test $ac_cv_func_link = yes; then
-       rm -rf conftest.f conftest.f1
-       if touch conftest.f && ln conftest.f conftest.f1 &&
+       rm -rf conftest.f conftest.f1 conftest.f2
+       if touch conftest.f conftest.f2 && ln conftest.f conftest.f1 &&
            set x `ls -i conftest.f conftest.f1` && test "$2" = "$4"; then
          AC_RUN_IFELSE(
            [AC_LANG_PROGRAM([[
+#             include <errno.h>
 #             include <stdio.h>
 #             include <stdlib.h>
 #             include <unistd.h>
@@ -150,10 +151,18 @@ AC_DEFUN([gl_FUNC_RENAME],
                   result |= 1;
                 if (unlink ("conftest.f1"))
                   result |= 2;
-                if (rename ("conftest.f", "conftest.f"))
+
+                /* Allow either the POSIX-required behavior, where the
+                   previous rename kept conftest.f, or the (better) NetBSD
+                   behavior, where it removed conftest.f.  */
+                if (rename ("conftest.f", "conftest.f") != 0
+                    && errno != ENOENT)
                   result |= 4;
+
                 if (rename ("conftest.f1", "conftest.f1") == 0)
                   result |= 8;
+                if (rename ("conftest.f2", "conftest.f2") != 0)
+                  result |= 16;
                 return result;
               ]])],
            [gl_cv_func_rename_link_works=yes],
@@ -171,7 +180,7 @@ AC_DEFUN([gl_FUNC_RENAME],
        else
          gl_cv_func_rename_link_works="guessing no"
        fi
-       rm -rf conftest.f conftest.f1
+       rm -rf conftest.f conftest.f1 conftest.f2
      else
        gl_cv_func_rename_link_works=yes
      fi
-- 
2.7.4

Reply via email to