2020-10-09 22:20:12 +02:00
|
|
|
/*-------------------------------------------------------------------------
|
|
|
|
*
|
|
|
|
* win32stat.c
|
|
|
|
* Replacements for <sys/stat.h> functions using GetFileInformationByHandle
|
|
|
|
*
|
2024-01-04 02:49:05 +01:00
|
|
|
* Portions Copyright (c) 1996-2024, 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
|
|
|
|
*
|
|
|
|
*-------------------------------------------------------------------------
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "c.h"
|
2022-08-06 02:00:57 +02:00
|
|
|
#include "port/win32ntdll.h"
|
|
|
|
|
2020-10-09 22:20:12 +02:00
|
|
|
#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;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
2022-08-06 02:00:57 +02:00
|
|
|
* Windows implementation of lstat().
|
2020-10-09 22:20:12 +02:00
|
|
|
*/
|
|
|
|
int
|
2022-08-06 02:00:57 +02:00
|
|
|
_pglstat64(const char *name, struct stat *buf)
|
2020-10-09 22:20:12 +02:00
|
|
|
{
|
|
|
|
/*
|
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)
|
2022-10-25 04:20:00 +02:00
|
|
|
{
|
|
|
|
if (errno == ENOENT)
|
|
|
|
{
|
|
|
|
/*
|
|
|
|
* If it's a junction point pointing to a non-existent path, we'll
|
|
|
|
* have ENOENT here (because pgwin32_open_handle does not use
|
|
|
|
* FILE_FLAG_OPEN_REPARSE_POINT). In that case, we'll try again
|
|
|
|
* with readlink() below, which will distinguish true ENOENT from
|
|
|
|
* pseudo-symlink.
|
|
|
|
*/
|
|
|
|
memset(buf, 0, sizeof(*buf));
|
|
|
|
ret = 0;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
ret = fileinfo_to_stat(hFile, buf);
|
2020-10-09 22:20:12 +02:00
|
|
|
|
2022-08-06 02:00:57 +02:00
|
|
|
/*
|
|
|
|
* Junction points appear as directories to fileinfo_to_stat(), so we'll
|
|
|
|
* need to do a bit more work to distinguish them.
|
|
|
|
*/
|
2022-10-25 04:20:00 +02:00
|
|
|
if ((ret == 0 && S_ISDIR(buf->st_mode)) || hFile == INVALID_HANDLE_VALUE)
|
2022-08-06 02:00:57 +02:00
|
|
|
{
|
|
|
|
char next[MAXPGPATH];
|
|
|
|
ssize_t size;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* POSIX says we need to put the length of the target path into
|
|
|
|
* st_size. Use readlink() to get it, or learn that this is not a
|
|
|
|
* junction point.
|
|
|
|
*/
|
|
|
|
size = readlink(name, next, sizeof(next));
|
|
|
|
if (size < 0)
|
|
|
|
{
|
|
|
|
if (errno == EACCES &&
|
|
|
|
pg_RtlGetLastNtStatus() == STATUS_DELETE_PENDING)
|
|
|
|
{
|
|
|
|
/* Unlinked underneath us. */
|
|
|
|
errno = ENOENT;
|
|
|
|
ret = -1;
|
|
|
|
}
|
|
|
|
else if (errno == EINVAL)
|
|
|
|
{
|
|
|
|
/* It's not a junction point, nothing to do. */
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
/* Some other failure. */
|
|
|
|
ret = -1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
/* It's a junction point, so report it as a symlink. */
|
|
|
|
buf->st_mode &= ~S_IFDIR;
|
|
|
|
buf->st_mode |= S_IFLNK;
|
|
|
|
buf->st_size = size;
|
2022-10-25 04:20:00 +02:00
|
|
|
ret = 0;
|
2022-08-06 02:00:57 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-10-25 04:20:00 +02:00
|
|
|
if (hFile != INVALID_HANDLE_VALUE)
|
|
|
|
CloseHandle(hFile);
|
2020-10-09 22:20:12 +02:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2022-08-06 02:00:57 +02:00
|
|
|
/*
|
|
|
|
* Windows implementation of stat().
|
|
|
|
*/
|
|
|
|
int
|
|
|
|
_pgstat64(const char *name, struct stat *buf)
|
|
|
|
{
|
2022-10-25 04:24:41 +02:00
|
|
|
int loops = 0;
|
2022-08-06 02:00:57 +02:00
|
|
|
int ret;
|
2022-10-25 04:24:41 +02:00
|
|
|
char curr[MAXPGPATH];
|
2022-08-06 02:00:57 +02:00
|
|
|
|
|
|
|
ret = _pglstat64(name, buf);
|
|
|
|
|
2022-10-25 04:24:41 +02:00
|
|
|
strlcpy(curr, name, MAXPGPATH);
|
|
|
|
|
2022-08-06 02:00:57 +02:00
|
|
|
/* Do we need to follow a symlink (junction point)? */
|
2022-10-25 04:24:41 +02:00
|
|
|
while (ret == 0 && S_ISLNK(buf->st_mode))
|
2022-08-06 02:00:57 +02:00
|
|
|
{
|
|
|
|
char next[MAXPGPATH];
|
|
|
|
ssize_t size;
|
|
|
|
|
2022-10-25 04:24:41 +02:00
|
|
|
if (++loops > 8)
|
|
|
|
{
|
|
|
|
errno = ELOOP;
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2022-08-06 02:00:57 +02:00
|
|
|
/*
|
|
|
|
* _pglstat64() already called readlink() once to be able to fill in
|
|
|
|
* st_size, and now we need to do it again to get the path to follow.
|
|
|
|
* That could be optimized, but stat() on symlinks is probably rare
|
|
|
|
* and this way is simple.
|
|
|
|
*/
|
2022-10-25 04:24:41 +02:00
|
|
|
size = readlink(curr, next, sizeof(next));
|
2022-08-06 02:00:57 +02:00
|
|
|
if (size < 0)
|
|
|
|
{
|
|
|
|
if (errno == EACCES &&
|
|
|
|
pg_RtlGetLastNtStatus() == STATUS_DELETE_PENDING)
|
|
|
|
{
|
|
|
|
/* Unlinked underneath us. */
|
|
|
|
errno = ENOENT;
|
|
|
|
}
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
if (size >= sizeof(next))
|
|
|
|
{
|
|
|
|
errno = ENAMETOOLONG;
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
next[size] = 0;
|
|
|
|
|
|
|
|
ret = _pglstat64(next, buf);
|
2022-10-25 04:24:41 +02:00
|
|
|
strcpy(curr, next);
|
2022-08-06 02:00:57 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2020-10-09 22:20:12 +02:00
|
|
|
/*
|
|
|
|
* Windows implementation of fstat().
|
|
|
|
*/
|
|
|
|
int
|
|
|
|
_pgfstat64(int fileno, struct stat *buf)
|
|
|
|
{
|
|
|
|
HANDLE hFile = (HANDLE) _get_osfhandle(fileno);
|
2023-03-15 04:55:51 +01:00
|
|
|
DWORD fileType = FILE_TYPE_UNKNOWN;
|
|
|
|
unsigned short st_mode;
|
2020-10-09 22:20:12 +02:00
|
|
|
|
Fix detection of unseekable files for fseek() and ftello() with MSVC
Calling fseek() or ftello() on a handle to a non-seeking device such as
a pipe or a communications device is not supported. Unfortunately,
MSVC's flavor of these routines, _fseeki64() and _ftelli64(), do not
return an error when given a pipe as handle. Some of the logic of
pg_dump and restore relies on these routines to check if a handle is
seekable, causing failures when passing the contents of pg_dump to
pg_restore through a pipe, for example.
This commit introduces wrappers for fseeko() and ftello() on MSVC so as
any callers are able to properly detect the cases of non-seekable
handles. This relies mainly on GetFileType(), sharing a bit of code
with the MSVC port for fstat(). The code in charge of getting a file
type is refactored into a new file called win32common.c, shared by
win32stat.c and the new win32fseek.c. It includes the MSVC ports for
fseeko() and ftello().
Like 765f5df, this is backpatched down to 14, where the fstat()
implementation for MSVC is able to understand about files larger than
4GB in size. Using a TAP test for that is proving to be tricky as
IPC::Run handles the pipes by itself, still I have been able to check
the fix manually.
Reported-by: Daniel Watzinger
Author: Juan José Santamaría Flecha, Michael Paquier
Discussion: https://postgr.es/m/CAC+AXB26a4EmxM2suXxPpJaGrqAdxracd7hskLg-zxtPB50h7A@mail.gmail.com
Backpatch-through: 14
2023-04-12 02:09:38 +02:00
|
|
|
if (buf == NULL)
|
2020-10-09 22:20:12 +02:00
|
|
|
{
|
|
|
|
errno = EINVAL;
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
Fix detection of unseekable files for fseek() and ftello() with MSVC
Calling fseek() or ftello() on a handle to a non-seeking device such as
a pipe or a communications device is not supported. Unfortunately,
MSVC's flavor of these routines, _fseeki64() and _ftelli64(), do not
return an error when given a pipe as handle. Some of the logic of
pg_dump and restore relies on these routines to check if a handle is
seekable, causing failures when passing the contents of pg_dump to
pg_restore through a pipe, for example.
This commit introduces wrappers for fseeko() and ftello() on MSVC so as
any callers are able to properly detect the cases of non-seekable
handles. This relies mainly on GetFileType(), sharing a bit of code
with the MSVC port for fstat(). The code in charge of getting a file
type is refactored into a new file called win32common.c, shared by
win32stat.c and the new win32fseek.c. It includes the MSVC ports for
fseeko() and ftello().
Like 765f5df, this is backpatched down to 14, where the fstat()
implementation for MSVC is able to understand about files larger than
4GB in size. Using a TAP test for that is proving to be tricky as
IPC::Run handles the pipes by itself, still I have been able to check
the fix manually.
Reported-by: Daniel Watzinger
Author: Juan José Santamaría Flecha, Michael Paquier
Discussion: https://postgr.es/m/CAC+AXB26a4EmxM2suXxPpJaGrqAdxracd7hskLg-zxtPB50h7A@mail.gmail.com
Backpatch-through: 14
2023-04-12 02:09:38 +02:00
|
|
|
fileType = pgwin32_get_file_type(hFile);
|
|
|
|
if (errno != 0)
|
2023-03-15 04:55:51 +01:00
|
|
|
return -1;
|
|
|
|
|
|
|
|
switch (fileType)
|
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
|
|
|
{
|
2023-03-15 04:55:51 +01:00
|
|
|
/* 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;
|
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
|
|
|
}
|
|
|
|
|
2023-03-15 04:55:51 +01:00
|
|
|
memset(buf, 0, sizeof(*buf));
|
|
|
|
buf->st_mode = st_mode;
|
|
|
|
buf->st_dev = fileno;
|
|
|
|
buf->st_rdev = fileno;
|
|
|
|
buf->st_nlink = 1;
|
|
|
|
return 0;
|
2020-10-09 22:20:12 +02:00
|
|
|
}
|