Commit 5e60237a authored by Michael Paquier's avatar Michael Paquier

Revert "Fix issues with Windows' stat() for files pending on deletion"

This reverts commit 54fb8c7, as per the issues reported by fairywren
when it comes to MinGW because of the lack of microsoft_native_stat()
there.  Using just stat() for MSVC is not sufficient to take care of the
concurrency problems with files pending on deletion.  It may be possible
to paint some __MINGW64__ in the code to switch to a different
implementation of stat() in this build context, but I am not sure either
if relying on the implementation of stat() in MinGW to take care of the
problems we are trying to fix is enough or not.  So this needs more
study.

Discussion: https://postgr.es/m/YOvOlfRrIO0yGtgw@paquier.xyz
Backpatch-through: 14
parent de1510e2
...@@ -157,9 +157,9 @@ pgwin32_open(const char *fileName, int fileFlags,...) ...@@ -157,9 +157,9 @@ pgwin32_open(const char *fileName, int fileFlags,...)
{ {
if (loops < 10) if (loops < 10)
{ {
struct microsoft_native_stat st; struct stat st;
if (microsoft_native_stat(fileName, &st) != 0) if (stat(fileName, &st) != 0)
{ {
pg_usleep(100000); pg_usleep(100000);
loops++; loops++;
......
...@@ -18,6 +18,53 @@ ...@@ -18,6 +18,53 @@
#include "c.h" #include "c.h"
#include <windows.h> #include <windows.h>
/*
* In order to support MinGW and MSVC2013 we use NtQueryInformationFile as an
* alternative for GetFileInformationByHandleEx. It is loaded from the ntdll
* library.
*/
#if _WIN32_WINNT < 0x0600
#include <winternl.h>
#if !defined(__MINGW32__) && !defined(__MINGW64__)
/* MinGW includes this in <winternl.h>, but it is missing in MSVC */
typedef struct _FILE_STANDARD_INFORMATION
{
LARGE_INTEGER AllocationSize;
LARGE_INTEGER EndOfFile;
ULONG NumberOfLinks;
BOOLEAN DeletePending;
BOOLEAN Directory;
} FILE_STANDARD_INFORMATION;
#define FileStandardInformation 5
#endif /* !defined(__MINGW32__) &&
* !defined(__MINGW64__) */
typedef NTSTATUS (NTAPI * PFN_NTQUERYINFORMATIONFILE)
(IN HANDLE FileHandle,
OUT PIO_STATUS_BLOCK IoStatusBlock,
OUT PVOID FileInformation,
IN ULONG Length,
IN FILE_INFORMATION_CLASS FileInformationClass);
static PFN_NTQUERYINFORMATIONFILE _NtQueryInformationFile = NULL;
static HMODULE ntdll = NULL;
/*
* Load DLL file just once regardless of how many functions we load/call in it.
*/
static void
LoadNtdll(void)
{
if (ntdll != NULL)
return;
ntdll = LoadLibraryEx("ntdll.dll", NULL, 0);
}
#endif /* _WIN32_WINNT < 0x0600 */
/* /*
* Convert a FILETIME struct into a 64 bit time_t. * Convert a FILETIME struct into a 64 bit time_t.
*/ */
...@@ -116,81 +163,117 @@ _pgstat64(const char *name, struct stat *buf) ...@@ -116,81 +163,117 @@ _pgstat64(const char *name, struct stat *buf)
{ {
/* /*
* We must use a handle so lstat() returns the information of the target * We must use a handle so lstat() returns the information of the target
* file. To have a reliable test for ERROR_DELETE_PENDING, this uses a * file. To have a reliable test for ERROR_DELETE_PENDING, we use
* method similar to open() with a loop using stat() and some waits when * NtQueryInformationFile from Windows 2000 or
* facing ERROR_ACCESS_DENIED. * GetFileInformationByHandleEx from Server 2008 / Vista.
*/ */
SECURITY_ATTRIBUTES sa; SECURITY_ATTRIBUTES sa;
HANDLE hFile; HANDLE hFile;
int ret; int ret;
int loops = 0; #if _WIN32_WINNT < 0x0600
IO_STATUS_BLOCK ioStatus;
FILE_STANDARD_INFORMATION standardInfo;
#else
FILE_STANDARD_INFO standardInfo;
#endif
if (name == NULL || buf == NULL) if (name == NULL || buf == NULL)
{ {
errno = EINVAL; errno = EINVAL;
return -1; return -1;
} }
/* fast not-exists check */ /* fast not-exists check */
if (GetFileAttributes(name) == INVALID_FILE_ATTRIBUTES) if (GetFileAttributes(name) == INVALID_FILE_ATTRIBUTES)
{ {
DWORD err = GetLastError(); _dosmaperr(GetLastError());
return -1;
if (err != ERROR_ACCESS_DENIED)
{
_dosmaperr(err);
return -1;
}
} }
/* get a file handle as lightweight as we can */ /* get a file handle as lightweight as we can */
sa.nLength = sizeof(SECURITY_ATTRIBUTES); sa.nLength = sizeof(SECURITY_ATTRIBUTES);
sa.bInheritHandle = TRUE; sa.bInheritHandle = TRUE;
sa.lpSecurityDescriptor = NULL; sa.lpSecurityDescriptor = NULL;
while ((hFile = CreateFile(name, hFile = CreateFile(name,
GENERIC_READ, GENERIC_READ,
(FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE), (FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE),
&sa, &sa,
OPEN_EXISTING, OPEN_EXISTING,
(FILE_FLAG_NO_BUFFERING | FILE_FLAG_BACKUP_SEMANTICS | (FILE_FLAG_NO_BUFFERING | FILE_FLAG_BACKUP_SEMANTICS |
FILE_FLAG_OVERLAPPED), FILE_FLAG_OVERLAPPED),
NULL)) == INVALID_HANDLE_VALUE) NULL);
if (hFile == INVALID_HANDLE_VALUE)
{ {
DWORD err = GetLastError(); DWORD err = GetLastError();
/* CloseHandle(hFile);
* ERROR_ACCESS_DENIED is returned if the file is deleted but not yet _dosmaperr(err);
* gone (Windows NT status code is STATUS_DELETE_PENDING). In that return -1;
* case we want to wait a bit and try again, giving up after 1 second }
* (since this condition should never persist very long). However,
* there are other commonly-hit cases that return ERROR_ACCESS_DENIED, memset(&standardInfo, 0, sizeof(standardInfo));
* so care is needed. In particular that happens if we try to open a
* directory, or of course if there's an actual file-permissions #if _WIN32_WINNT < 0x0600
* problem. To distinguish these cases, try a stat(). In the if (_NtQueryInformationFile == NULL)
* delete-pending case, it will either also get STATUS_DELETE_PENDING, {
* or it will see the file as gone and fail with ENOENT. In other /* First time through: load ntdll.dll and find NtQueryInformationFile */
* cases it will usually succeed. The only somewhat-likely case where LoadNtdll();
* this coding will uselessly wait is if there's a permissions problem if (ntdll == NULL)
* with a containing directory, which we hope will never happen in any {
* performance-critical code paths. DWORD err = GetLastError();
*/
if (err == ERROR_ACCESS_DENIED) CloseHandle(hFile);
_dosmaperr(err);
return -1;
}
_NtQueryInformationFile = (PFN_NTQUERYINFORMATIONFILE) (pg_funcptr_t)
GetProcAddress(ntdll, "NtQueryInformationFile");
if (_NtQueryInformationFile == NULL)
{ {
if (loops < 10) DWORD err = GetLastError();
{
struct microsoft_native_stat st; CloseHandle(hFile);
_dosmaperr(err);
if (microsoft_native_stat(name, &st) != 0) return -1;
{
pg_usleep(100000);
loops++;
continue;
}
}
} }
}
if (!NT_SUCCESS(_NtQueryInformationFile(hFile, &ioStatus, &standardInfo,
sizeof(standardInfo),
FileStandardInformation)))
{
DWORD err = GetLastError();
CloseHandle(hFile);
_dosmaperr(err);
return -1;
}
#else
if (!GetFileInformationByHandleEx(hFile, FileStandardInfo, &standardInfo,
sizeof(standardInfo)))
{
DWORD err = GetLastError();
CloseHandle(hFile);
_dosmaperr(err); _dosmaperr(err);
return -1; return -1;
} }
#endif /* _WIN32_WINNT < 0x0600 */
if (standardInfo.DeletePending)
{
/*
* File has been deleted, but is not gone from the filesystem yet.
* This can happen when some process with FILE_SHARE_DELETE has it
* open, and it will be fully removed once that handle is closed.
* Meanwhile, we can't open it, so indicate that the file just doesn't
* exist.
*/
CloseHandle(hFile);
errno = ENOENT;
return -1;
}
/* At last we can invoke fileinfo_to_stat */ /* At last we can invoke fileinfo_to_stat */
ret = fileinfo_to_stat(hFile, buf); ret = fileinfo_to_stat(hFile, buf);
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment