]> Savannah Git Hosting - gnulib.git/commitdiff
link: Improve trailing slash handling on native Windows.
authorBruno Haible <bruno@clisp.org>
Tue, 5 Jan 2021 04:22:54 +0000 (05:22 +0100)
committerBruno Haible <bruno@clisp.org>
Tue, 5 Jan 2021 04:22:54 +0000 (05:22 +0100)
* lib/link.c (link): If stat() fails, provide a better errno.

ChangeLog
lib/link.c

index a579efc9b95a2b52d82a3f10caa5f363ab128556..e88a08a28e7e03ae150fb7f8e617da29547c9e1c 100644 (file)
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,8 @@
+2021-01-04  Bruno Haible  <bruno@clisp.org>
+
+       link: Improve trailing slash handling on native Windows.
+       * lib/link.c (link): If stat() fails, provide a better errno.
+
 2021-01-04  Bruno Haible  <bruno@clisp.org>
 
        mkfifoat: Work around trailing slash bug in mknodat() on AIX 7.2.
index 87dae40796ead79e8469c36898c962e5a6e6149c..21c7f7aed0847998f847aa8ca0b4b6fd5e9faf07 100644 (file)
@@ -85,16 +85,24 @@ link (const char *file1, const char *file2)
       errno = EPERM;
       return -1;
     }
-  /* Reject trailing slashes on non-directories; mingw does not
+  /* Reject trailing slashes on non-directories; native Windows does not
      support hard-linking directories.  */
   if ((len1 && (file1[len1 - 1] == '/' || file1[len1 - 1] == '\\'))
       || (len2 && (file2[len2 - 1] == '/' || file2[len2 - 1] == '\\')))
     {
+      /* If stat() fails, then link() should fail for the same reason.  */
       struct stat st;
-      if (stat (file1, &st) == 0 && S_ISDIR (st.st_mode))
-        errno = EPERM;
-      else
+      if (stat (file1, &st))
+        {
+          if (errno == EOVERFLOW)
+            /* It's surely a file, not a directory (see stat-w32.c).  */
+            errno = ENOTDIR;
+          return -1;
+        }
+      if (!S_ISDIR (st.st_mode))
         errno = ENOTDIR;
+      else
+        errno = EPERM;
       return -1;
     }
   /* CreateHardLink("b/.","a",NULL) creates file "b", so we must check