2020-10-09 22:20:12 +02:00
|
|
|
/*-------------------------------------------------------------------------
|
|
|
|
*
|
|
|
|
* win32stat.c
|
|
|
|
* Replacements for <sys/stat.h> functions using GetFileInformationByHandle
|
|
|
|
*
|
2022-01-08 01:04:57 +01:00
|
|
|
* Portions Copyright (c) 1996-2022, PostgreSQL Global Development Group
|
2020-10-09 22:20:12 +02:00
|
|
|
* Portions Copyright (c) 1994, Regents of the University of California
|
|
|
|
*
|
|
|
|
*
|
|
|
|
* IDENTIFICATION
|
|
|
|
* src/port/win32stat.c
|
|
|
|
*
|
|
|
|
*-------------------------------------------------------------------------
|
|
|
|
*/
|
|
|
|
|
|
|
|
#ifdef WIN32
|
|
|
|
|
|
|
|
#include "c.h"
|
|
|
|
#include <windows.h>
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Convert a FILETIME struct into a 64 bit time_t.
|
|
|
|
*/
|
|
|
|
static __time64_t
|
|
|
|
filetime_to_time(const FILETIME *ft)
|
|
|
|
{
|
|
|
|
ULARGE_INTEGER unified_ft = {0};
|
|
|
|
static const uint64 EpochShift = UINT64CONST(116444736000000000);
|
|
|
|
|
|
|
|
unified_ft.LowPart = ft->dwLowDateTime;
|
|
|
|
unified_ft.HighPart = ft->dwHighDateTime;
|
|
|
|
|
|
|
|
if (unified_ft.QuadPart < EpochShift)
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
unified_ft.QuadPart -= EpochShift;
|
|
|
|
unified_ft.QuadPart /= 10 * 1000 * 1000;
|
|
|
|
|
|
|
|
return unified_ft.QuadPart;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Convert WIN32 file attributes to a Unix-style mode.
|
|
|
|
*
|
|
|
|
* Only owner permissions are set.
|
|
|
|
*/
|
|
|
|
static unsigned short
|
|
|
|
fileattr_to_unixmode(int attr)
|
|
|
|
{
|
|
|
|
unsigned short uxmode = 0;
|
|
|
|
|
|
|
|
uxmode |= (unsigned short) ((attr & FILE_ATTRIBUTE_DIRECTORY) ?
|
|
|
|
(_S_IFDIR) : (_S_IFREG));
|
|
|
|
|
2020-10-10 19:39:21 +02:00
|
|
|
uxmode |= (unsigned short) ((attr & FILE_ATTRIBUTE_READONLY) ?
|
|
|
|
(_S_IREAD) : (_S_IREAD | _S_IWRITE));
|
2020-10-09 22:20:12 +02:00
|
|
|
|
|
|
|
/* there is no need to simulate _S_IEXEC using CMD's PATHEXT extensions */
|
|
|
|
uxmode |= _S_IEXEC;
|
|
|
|
|
|
|
|
return uxmode;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Convert WIN32 file information (from a HANDLE) to a struct stat.
|
|
|
|
*/
|
|
|
|
static int
|
|
|
|
fileinfo_to_stat(HANDLE hFile, struct stat *buf)
|
|
|
|
{
|
|
|
|
BY_HANDLE_FILE_INFORMATION fiData;
|
|
|
|
|
|
|
|
memset(buf, 0, sizeof(*buf));
|
|
|
|
|
|
|
|
/*
|
|
|
|
* GetFileInformationByHandle minimum supported version: Windows XP and
|
|
|
|
* Windows Server 2003, so it exists everywhere we care about.
|
|
|
|
*/
|
|
|
|
if (!GetFileInformationByHandle(hFile, &fiData))
|
|
|
|
{
|
|
|
|
_dosmaperr(GetLastError());
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (fiData.ftLastWriteTime.dwLowDateTime ||
|
|
|
|
fiData.ftLastWriteTime.dwHighDateTime)
|
|
|
|
buf->st_mtime = filetime_to_time(&fiData.ftLastWriteTime);
|
|
|
|
|
|
|
|
if (fiData.ftLastAccessTime.dwLowDateTime ||
|
|
|
|
fiData.ftLastAccessTime.dwHighDateTime)
|
|
|
|
buf->st_atime = filetime_to_time(&fiData.ftLastAccessTime);
|
|
|
|
else
|
|
|
|
buf->st_atime = buf->st_mtime;
|
|
|
|
|
|
|
|
if (fiData.ftCreationTime.dwLowDateTime ||
|
|
|
|
fiData.ftCreationTime.dwHighDateTime)
|
|
|
|
buf->st_ctime = filetime_to_time(&fiData.ftCreationTime);
|
|
|
|
else
|
|
|
|
buf->st_ctime = buf->st_mtime;
|
|
|
|
|
|
|
|
buf->st_mode = fileattr_to_unixmode(fiData.dwFileAttributes);
|
|
|
|
buf->st_nlink = fiData.nNumberOfLinks;
|
|
|
|
|
2020-10-10 19:39:21 +02:00
|
|
|
buf->st_size = ((((uint64) fiData.nFileSizeHigh) << 32) |
|
2020-10-10 20:53:23 +02:00
|
|
|
fiData.nFileSizeLow);
|
2020-10-09 22:20:12 +02:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Windows implementation of stat().
|
|
|
|
*
|
|
|
|
* This currently also implements lstat(), though perhaps that should change.
|
|
|
|
*/
|
|
|
|
int
|
|
|
|
_pgstat64(const char *name, struct stat *buf)
|
|
|
|
{
|
|
|
|
/*
|
2021-12-10 04:13:14 +01:00
|
|
|
* Our open wrapper will report STATUS_DELETE_PENDING as ENOENT. We
|
|
|
|
* request FILE_FLAG_BACKUP_SEMANTICS so that we can open directories too,
|
|
|
|
* for limited purposes. We use the private handle-based version, so we
|
|
|
|
* don't risk running out of fds.
|
2020-10-09 22:20:12 +02:00
|
|
|
*/
|
|
|
|
HANDLE hFile;
|
|
|
|
int ret;
|
2021-07-12 07:46:08 +02:00
|
|
|
|
2021-12-10 04:13:14 +01:00
|
|
|
hFile = pgwin32_open_handle(name, O_RDONLY, true);
|
2021-07-12 07:46:08 +02:00
|
|
|
if (hFile == INVALID_HANDLE_VALUE)
|
|
|
|
return -1;
|
|
|
|
|
2020-10-09 22:20:12 +02:00
|
|
|
ret = fileinfo_to_stat(hFile, buf);
|
|
|
|
|
|
|
|
CloseHandle(hFile);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Windows implementation of fstat().
|
|
|
|
*/
|
|
|
|
int
|
|
|
|
_pgfstat64(int fileno, struct stat *buf)
|
|
|
|
{
|
|
|
|
HANDLE hFile = (HANDLE) _get_osfhandle(fileno);
|
2021-11-30 01:55:50 +01:00
|
|
|
BY_HANDLE_FILE_INFORMATION fiData;
|
2020-10-09 22:20:12 +02:00
|
|
|
|
|
|
|
if (hFile == INVALID_HANDLE_VALUE || buf == NULL)
|
|
|
|
{
|
|
|
|
errno = EINVAL;
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
Fix fstat() emulation on Windows with standard streams
The emulation of fstat() in win32stat.c caused two issues with the
existing in-core callers, failing on EINVAL when using a stream as
argument:
- psql's \copy would crash when using a stream.
- pg_recvlogical would fail with -f -.
The tests in copyselect.sql from the main test suite covers the first
case, and there is a TAP test for the second case. However, in both
cases, as the standard streams are always redirected, automated tests
did not notice those issues, requiring a terminal on Windows to be
reproducible.
This issue has been introduced in bed9075, and the origin of the problem
is that GetFileInformationByHandle() does not work directly on streams,
so this commit adds an extra code path to emulate and return a set of
stats that match best with the reality. Note that redirected streams
rely on handles that can be queried with GetFileInformationByHandle(),
but we can rely on GetFinalPathNameByHandleA() to detect this case.
Author: Dmitry Koval, Juan José Santamaría Flecha
Discussion: https://postgr.es/m/17288-6b58a91025a8a8a3@postgresql.org
Backpatch-through: 14
2021-11-25 04:16:21 +01:00
|
|
|
/*
|
|
|
|
* 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.
|
|
|
|
*/
|
|
|
|
if ((fileno == _fileno(stdin) ||
|
|
|
|
fileno == _fileno(stdout) ||
|
|
|
|
fileno == _fileno(stderr)) &&
|
2021-11-30 01:55:50 +01:00
|
|
|
!GetFileInformationByHandle(hFile, &fiData))
|
Fix fstat() emulation on Windows with standard streams
The emulation of fstat() in win32stat.c caused two issues with the
existing in-core callers, failing on EINVAL when using a stream as
argument:
- psql's \copy would crash when using a stream.
- pg_recvlogical would fail with -f -.
The tests in copyselect.sql from the main test suite covers the first
case, and there is a TAP test for the second case. However, in both
cases, as the standard streams are always redirected, automated tests
did not notice those issues, requiring a terminal on Windows to be
reproducible.
This issue has been introduced in bed9075, and the origin of the problem
is that GetFileInformationByHandle() does not work directly on streams,
so this commit adds an extra code path to emulate and return a set of
stats that match best with the reality. Note that redirected streams
rely on handles that can be queried with GetFileInformationByHandle(),
but we can rely on GetFinalPathNameByHandleA() to detect this case.
Author: Dmitry Koval, Juan José Santamaría Flecha
Discussion: https://postgr.es/m/17288-6b58a91025a8a8a3@postgresql.org
Backpatch-through: 14
2021-11-25 04:16:21 +01:00
|
|
|
{
|
|
|
|
memset(buf, 0, sizeof(*buf));
|
|
|
|
buf->st_mode = _S_IFCHR;
|
|
|
|
buf->st_dev = fileno;
|
|
|
|
buf->st_rdev = fileno;
|
|
|
|
buf->st_nlink = 1;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2020-10-09 22:20:12 +02:00
|
|
|
return fileinfo_to_stat(hFile, buf);
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif /* WIN32 */
|