Subject: bug#36405: 26.2.90; O_PATH problem on some versions of Cygwin

From: Ken Brown <[email-masked]>
Date: Sat, 29 Jun 2019 18:03:02 +0000

On 6/29/2019 3:06 AM, Eli Zaretskii wrote:
>> Cc: [email-masked], [email-masked]
>> From: Paul Eggert <[email-masked]>
>> Date: Fri, 28 Jun 2019 23:26:09 -0700
>>
>> Eli Zaretskii wrote:
>>> What problems did you see/envision on other platforms with the
>>> original patch?
>>
>> Mainly the hassle of maintaining code with a lot of hard-to-read ifdefs in it.
>> It's better if system-specific stuff is kept to a minimum in mainline code.
> 
> I agree, of course.  But in this case we are talking about adding 2
> #ifdef's, so the situation is not much worse than it was before.
> 
>> Also, this O_PATH stuff is not that high of a priority, as the code works quite
>> well in practice without O_PATH.
> 
> It was evidently important enough for us to use it on platforms that
> support it.
> 
>> How about something like the attached patch instead? It's simpler and should be
>> a bit faster on Cygwin than the original patch. I haven't tested it.
> 
> Thanks, this is fine with me if it does the job.  Ken?

Fine with me too, after a small tweak.  Revised patch attached.

Thanks, Paul.

Ken
diff --git a/configure.ac b/configure.ac
index 8ff0e21fbf..774f8e5eb9 100644
--- a/configure.ac
+++ b/configure.ac
@@ -5734,6 +5734,9 @@ AC_DEFUN
     AC_MSG_WARN([[building Emacs on Cygwin 1.5 is not supported.]])
            echo
 	   ;;
+  cygwin,3.0.[[0-7]]'('*)
+    AC_DEFINE([HAVE_CYGWIN_O_PATH_BUG], 1,
+      [Define to 1 if opening a FIFO with O_PATH causes a hang.]);;
 esac
 
 # Remove any trailing slashes in these variables.
diff --git a/src/dired.c b/src/dired.c
index b8197d36a0..b700013f6a 100644
--- a/src/dired.c
+++ b/src/dired.c
@@ -41,10 +41,6 @@
 #include "buffer.h"
 #include "coding.h"
 
-#ifdef __CYGWIN__
-# undef O_PATH /* Buggy in Cygwin 3.0.0 through 3.0.7.  */
-#endif
-
 #ifdef MSDOS
 #include "msdos.h"	/* for fstatat */
 #endif
@@ -941,7 +937,7 @@ file_attributes (int fd, char const *name,
 
   int err = EINVAL;
 
-#ifdef O_PATH
+#if defined O_PATH && !defined HAVE_CYGWIN_O_PATH_BUG
   int namefd = openat (fd, name, O_PATH | O_CLOEXEC | O_NOFOLLOW);
   if (namefd < 0)
     err = errno;