Improve WIN32 port of fstat() to detect more file types
authorMichael Paquier <michael@paquier.xyz>
Wed, 15 Mar 2023 03:55:51 +0000 (12:55 +0900)
committerMichael Paquier <michael@paquier.xyz>
Wed, 15 Mar 2023 03:55:51 +0000 (12:55 +0900)
The current implementation of _pgfstat64() is ineffective in detecting a
terminal handle or an anonymous named pipe.  This commit improves our
port of fstat() to detect more efficiently such cases by relying on
GetFileType(), and returning more correct data when the type found is
either a FILE_TYPE_PIPE (_S_IFIFO) or a FILE_TYPE_CHAR (_S_IFCHR).

This is part of a more global fix to address failures when feeding the
output generated by pg_dump to pg_restore through a pipe, for example,
but not all of it.   We are also going to need to do something about
fseek() and ftello() which are not reliable on WIN32 for the same cases
where fstat() was incorrect.  Fixing fstat() is independent of the rest,
though, which is why both fixes are handled separately, and this is the
first part of it.

Reported-by: Daniel Watzinger
Author: Daniel Watzinger, Juan José Santamaría Flecha
Discussion: https://postgr.es/m/b1448cd7-871e-20e3-8398-895e2d1d3bf9@gmail.com
Backpatch-through: 14

src/port/win32stat.c

index a3c42f0a96b9848352633c3b092d894ddcf798c8..b79da738b2951747b3fc370a732ea7891974f32f 100644 (file)
@@ -257,34 +257,66 @@ int
 _pgfstat64(int fileno, struct stat *buf)
 {
        HANDLE          hFile = (HANDLE) _get_osfhandle(fileno);
-       BY_HANDLE_FILE_INFORMATION fiData;
+       DWORD           fileType = FILE_TYPE_UNKNOWN;
+       DWORD           lastError;
+       unsigned short st_mode;
 
-       if (hFile == INVALID_HANDLE_VALUE || buf == NULL)
+       /*
+        * When stdin, stdout, and stderr aren't associated with a stream the
+        * special value -2 is returned:
+        * https://learn.microsoft.com/en-us/cpp/c-runtime-library/reference/get-osfhandle
+        */
+       if (hFile == INVALID_HANDLE_VALUE || hFile == (HANDLE) -2 || buf == NULL)
        {
                errno = EINVAL;
                return -1;
        }
 
+       fileType = GetFileType(hFile);
+       lastError = GetLastError();
+
        /*
-        * Check if the fileno is a data stream.  If so, unless it has been
-        * redirected to a file, getting information through its HANDLE will fail,
-        * so emulate its stat information in the most appropriate way and return
-        * it instead.
+        * Invoke GetLastError in order to distinguish between a "valid" return of
+        * FILE_TYPE_UNKNOWN and its return due to a calling error.  In case of
+        * success, GetLastError returns NO_ERROR.
         */
-       if ((fileno == _fileno(stdin) ||
-                fileno == _fileno(stdout) ||
-                fileno == _fileno(stderr)) &&
-               !GetFileInformationByHandle(hFile, &fiData))
+       if (fileType == FILE_TYPE_UNKNOWN && lastError != NO_ERROR)
+       {
+               _dosmaperr(lastError);
+               return -1;
+       }
+
+       switch (fileType)
        {
-               memset(buf, 0, sizeof(*buf));
-               buf->st_mode = _S_IFCHR;
-               buf->st_dev = fileno;
-               buf->st_rdev = fileno;
-               buf->st_nlink = 1;
-               return 0;
+                       /* The specified file is a disk file */
+               case FILE_TYPE_DISK:
+                       return fileinfo_to_stat(hFile, buf);
+
+                       /*
+                        * The specified file is a socket, a named pipe, or an anonymous
+                        * pipe.
+                        */
+               case FILE_TYPE_PIPE:
+                       st_mode = _S_IFIFO;
+                       break;
+                       /* The specified file is a character file */
+               case FILE_TYPE_CHAR:
+                       st_mode = _S_IFCHR;
+                       break;
+                       /* Unused flag and unknown file type */
+               case FILE_TYPE_REMOTE:
+               case FILE_TYPE_UNKNOWN:
+               default:
+                       errno = EINVAL;
+                       return -1;
        }
 
-       return fileinfo_to_stat(hFile, buf);
+       memset(buf, 0, sizeof(*buf));
+       buf->st_mode = st_mode;
+       buf->st_dev = fileno;
+       buf->st_rdev = fileno;
+       buf->st_nlink = 1;
+       return 0;
 }
 
 #endif                                                 /* WIN32 */