8000 Improve WIN32 port of fstat() to detect more file types · postgrespro/postgres@765f5df · GitHub
[go: up one dir, main page]

Skip to content
  • Commit 765f5df

    Browse files
    committed
    Improve WIN32 port of fstat() to detect more file types
    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
    1 parent 89e46da commit 765f5df

    File tree

    1 file changed

    +49
    -17
    lines changed

    1 file changed

    +49
    -17
    lines changed

    src/port/win32stat.c

    Lines changed: 49 additions & 17 deletions
    Original file line numberDiff line numberDiff line change
    @@ -257,34 +257,66 @@ int
    257257
    _pgfstat64(int fileno, struct stat *buf)
    258258
    {
    259259
    HANDLE hFile = (HANDLE) _get_osfhandle(fileno);
    260-
    BY_HANDLE_FILE_INFORMATION fiData;
    260+
    DWORD fileType = FILE_TYPE_UNKNOWN;
    261+
    DWORD lastError;
    262+
    unsigned short st_mode;
    261263

    262-
    if (hFile == INVALID_HANDLE_VALUE || buf == NULL)
    264+
    /*
    265+
    * When stdin, stdout, and stderr aren't associated with a stream the
    266+
    * special value -2 is returned:
    267+
    * https://learn.microsoft.com/en-us/cpp/c-runtime-library/reference/get-osfhandle
    268+
    */
    269+
    if (hFile == INVALID_HANDLE_VALUE || hFile == (HANDLE) -2 || buf == NULL)
    263270
    {
    264271
    errno = EINVAL;
    265272
    return -1;
    266273
    }
    267274

    275+
    fileType = GetFileType(hFile);
    276+
    lastError = GetLastError();
    277+
    268278
    /*
    269-
    * Check if the fileno is a data stream. If so, unless it has been
    270-
    * redirected to a file, getting information through its HANDLE will fail,
    271-
    * so emulate its stat information in the most appropriate way and return
    272-
    * it instead.
    279+
    * Invoke GetLastError in order to distinguish between a "valid" return of
    280+
    * FILE_TYPE_UNKNOWN and its return due to a calling error. In case of
    281+
    * success, GetLastError returns NO_ERROR.
    273282
    */
    274-
    if ((fileno == _fileno(stdin) ||
    275-
    fileno == _fileno(stdout) ||
    276-
    fileno == _fileno(stderr)) &&
    277-
    !GetFileInformationByHandle(hFile, &fiData))
    283+
    if (fileType == FILE_TYPE_UNKNOWN && lastError != NO_ERROR)
    284+
    {
    285+
    _dosmaperr(lastError);
    286+
    return -1;
    287+
    }
    288+
    289+
    switch (fileType)
    278290
    {
    279-
    memset(buf, 0, sizeof(*buf));
    280-
    buf->st_mode = _S_IFCHR;
    281-
    buf->st_dev = fileno;
    282-
    buf->st_rdev = fileno;
    283-
    buf->st_nlink = 1;
    284-
    return 0;
    291+
    /* The specified file is a disk file */
    292+
    case FILE_TYPE_DISK:
    293+
    return fileinfo_to_stat(hFile, buf);
    294+
    295+
    /*
    296+
    * The specified file is a socket, a named pipe, or an anonymous
    297+
    * pipe.
    298+
    */
    299+
    case FILE_TYPE_PIPE:
    300+
    st_mode = _S_IFIFO;
    301+
    break;
    302+
    /* The specified file is a character file */
    303+
    case FILE_TYPE_CHAR:
    304+
    st_mode = _S_IFCHR;
    305+
    break;
    306+
    /* Unused flag and unknown file type */
    307+
    case FILE_TYPE_REMOTE:
    308+
    case FILE_TYPE_UNKNOWN:
    309+
    default:
    310+
    errno = EINVAL;
    311+
    return -1;
    285312
    }
    286313

    287-
    return fileinfo_to_stat(hFile, buf);
    314+
    memset(buf, 0, sizeof(*buf));
    315+
    buf->st_mode = st_mode;
    316+
    buf->st_dev = fileno;
    317+
    buf->st_rdev = fileno;
    318+
    buf->st_nlink = 1;
    319+
    return 0;
    288320
    }
    289321

    290322
    #endif /* WIN32 */

    0 commit comments

    Comments
     (0)
    0