Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commitf1cf641

Browse files
michaelpqadunstan
authored andcommitted
Fix issues with Windows' stat() for files pending on deletion
The code introduced bybed9075 to enhance the stat() implementation onWindows for file sizes larger than 4GB fails to properly detect filespending for deletion with its method based on NtQueryInformationFile()or GetFileInformationByHandleEx(), as proved by Alexander Lakhin in acustom TAP test of his own.The method used in the implementation of open() to sleep and loop whenwhen failing on ERROR_ACCESS_DENIED (EACCES) is showing much morestability, so switch to this method. This could still lead to issues ifthe permission problem stays around for much longer than the timeout of1 second used, but that should (hopefully) never happen inperformance-critical paths. Still, there could be a point in increasingthe timeouts for the sake of machines that handle heavy loads.Note that WIN32's open() now uses microsoft_native_stat() as it shouldbe similar to stat() when working around issues with concurrent filedeletions.I have spent some time testing this patch with pgbench in combinationof the SQL functions from genfile.c, as well as running the TAP testprovided on the thread with MSVC builds, and this looks much morestable than the previous method.Author: Alexander LakhinReviewed-by: Tom Lane, Michael Paquier,Justin PryzbyDiscussion:https://postgr.es/m/c3427edf-d7c0-ff57-90f6-b5de3bb62709@gmail.comBackpatch-through: 14(cherry picked from commit54fb8c7)Author: Alexandra Wang <alexandra.wang.oss@gmail.com>
1 parenta9beed6 commitf1cf641

File tree

2 files changed

+51
-126
lines changed

2 files changed

+51
-126
lines changed

‎src/port/open.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -157,9 +157,9 @@ pgwin32_open(const char *fileName, int fileFlags,...)
157157
{
158158
if (loops<10)
159159
{
160-
structstatst;
160+
structmicrosoft_native_statst;
161161

162-
if (stat(fileName,&st)!=0)
162+
if (microsoft_native_stat(fileName,&st)!=0)
163163
{
164164
pg_usleep(100000);
165165
loops++;

‎src/port/win32stat.c

Lines changed: 49 additions & 124 deletions
Original file line numberDiff line numberDiff line change
@@ -18,55 +18,6 @@
1818
#include"c.h"
1919
#include<windows.h>
2020

21-
/*
22-
* In order to support MinGW and MSVC2013 we use NtQueryInformationFile as an
23-
* alternative for GetFileInformationByHandleEx. It is loaded from the ntdll
24-
* library.
25-
*/
26-
#if_WIN32_WINNT<0x0600
27-
#include<winternl.h>
28-
29-
#if !defined(__MINGW32__)&& !defined(__MINGW64__)
30-
/* MinGW includes this in <winternl.h>, but it is missing in MSVC */
31-
typedefstruct_FILE_STANDARD_INFORMATION
32-
{
33-
LARGE_INTEGERAllocationSize;
34-
LARGE_INTEGEREndOfFile;
35-
ULONGNumberOfLinks;
36-
BOOLEANDeletePending;
37-
BOOLEANDirectory;
38-
}FILE_STANDARD_INFORMATION;
39-
#defineFileStandardInformation 5
40-
#endif/* !defined(__MINGW32__) &&
41-
* !defined(__MINGW64__) */
42-
43-
typedefNTSTATUS(NTAPI*PFN_NTQUERYINFORMATIONFILE)
44-
(
45-
INHANDLEFileHandle,
46-
OUTPIO_STATUS_BLOCKIoStatusBlock,
47-
OUTPVOIDFileInformation,
48-
INULONGLength,
49-
INFILE_INFORMATION_CLASSFileInformationClass
50-
);
51-
52-
staticPFN_NTQUERYINFORMATIONFILE_NtQueryInformationFile=NULL;
53-
54-
staticHMODULEntdll=NULL;
55-
56-
/*
57-
* Load DLL file just once regardless of how many functions we load/call in it.
58-
*/
59-
staticvoid
60-
LoadNtdll(void)
61-
{
62-
if (ntdll!=NULL)
63-
return;
64-
ntdll=LoadLibraryEx("ntdll.dll",NULL,0);
65-
}
66-
67-
#endif/* _WIN32_WINNT < 0x0600 */
68-
69-
7021
/*
7122
* Convert a FILETIME struct into a 64 bit time_t.
7223
*/
@@ -165,105 +116,79 @@ _pgstat64(const char *name, struct stat *buf)
165116
{
166117
/*
167118
* We must use a handle so lstat() returns the information of the target
168-
* file. To have a reliable test for ERROR_DELETE_PENDING,we use
169-
*NtQueryInformationFile from Windows 2000 or
170-
*GetFileInformationByHandleEx from Server 2008 / Vista.
119+
* file. To have a reliable test for ERROR_DELETE_PENDING,this uses a
120+
*method similar to open() with a loop using stat() and some waits when
121+
*facing ERROR_ACCESS_DENIED.
171122
*/
172123
SECURITY_ATTRIBUTESsa;
173124
HANDLEhFile;
174125
intret;
175-
#if_WIN32_WINNT<0x0600
176-
IO_STATUS_BLOCKioStatus;
177-
FILE_STANDARD_INFORMATIONstandardInfo;
178-
#else
179-
FILE_STANDARD_INFOstandardInfo;
180-
#endif
126+
intloops=0;
181127

182128
if (name==NULL||buf==NULL)
183129
{
184130
errno=EINVAL;
185131
return-1;
186132
}
187-
188133
/* fast not-exists check */
189134
if (GetFileAttributes(name)==INVALID_FILE_ATTRIBUTES)
190135
{
191-
_dosmaperr(GetLastError());
192-
return-1;
136+
DWORDerr=GetLastError();
137+
138+
if (err!=ERROR_ACCESS_DENIED)
139+
{
140+
_dosmaperr(err);
141+
return-1;
142+
}
193143
}
194144

195145
/* get a file handle as lightweight as we can */
196146
sa.nLength=sizeof(SECURITY_ATTRIBUTES);
197147
sa.bInheritHandle= TRUE;
198148
sa.lpSecurityDescriptor=NULL;
199-
hFile=CreateFile(name,
200-
GENERIC_READ,
201-
(FILE_SHARE_READ |FILE_SHARE_WRITE |FILE_SHARE_DELETE),
202-
&sa,
203-
OPEN_EXISTING,
204-
(FILE_FLAG_NO_BUFFERING |FILE_FLAG_BACKUP_SEMANTICS |
205-
FILE_FLAG_OVERLAPPED),
206-
NULL);
207-
if (hFile==INVALID_HANDLE_VALUE)
149+
while ((hFile=CreateFile(name,
150+
GENERIC_READ,
151+
(FILE_SHARE_READ |FILE_SHARE_WRITE |FILE_SHARE_DELETE),
152+
&sa,
153+
OPEN_EXISTING,
154+
(FILE_FLAG_NO_BUFFERING |FILE_FLAG_BACKUP_SEMANTICS |
155+
FILE_FLAG_OVERLAPPED),
156+
NULL))==INVALID_HANDLE_VALUE)
208157
{
209-
CloseHandle(hFile);
210-
errno=ENOENT;
211-
return-1;
212-
}
213-
214-
memset(&standardInfo,0,sizeof(standardInfo));
158+
DWORDerr=GetLastError();
215159

216-
#if_WIN32_WINNT<0x0600
217-
if (_NtQueryInformationFile==NULL)
218-
{
219-
/* First time through: load ntdll.dll and find NtQueryInformationFile */
220-
LoadNtdll();
221-
if (ntdll==NULL)
222-
{
223-
_dosmaperr(GetLastError());
224-
CloseHandle(hFile);
225-
return-1;
226-
}
227-
228-
_NtQueryInformationFile= (PFN_NTQUERYINFORMATIONFILE)
229-
GetProcAddress(ntdll,"NtQueryInformationFile");
230-
if (_NtQueryInformationFile==NULL)
160+
/*
161+
* ERROR_ACCESS_DENIED is returned if the file is deleted but not yet
162+
* gone (Windows NT status code is STATUS_DELETE_PENDING). In that
163+
* case we want to wait a bit and try again, giving up after 1 second
164+
* (since this condition should never persist very long). However,
165+
* there are other commonly-hit cases that return ERROR_ACCESS_DENIED,
166+
* so care is needed. In particular that happens if we try to open a
167+
* directory, or of course if there's an actual file-permissions
168+
* problem. To distinguish these cases, try a stat(). In the
169+
* delete-pending case, it will either also get STATUS_DELETE_PENDING,
170+
* or it will see the file as gone and fail with ENOENT. In other
171+
* cases it will usually succeed. The only somewhat-likely case where
172+
* this coding will uselessly wait is if there's a permissions problem
173+
* with a containing directory, which we hope will never happen in any
174+
* performance-critical code paths.
175+
*/
176+
if (err==ERROR_ACCESS_DENIED)
231177
{
232-
_dosmaperr(GetLastError());
233-
CloseHandle(hFile);
234-
return-1;
178+
if (loops<10)
179+
{
180+
structmicrosoft_native_statst;
181+
182+
if (microsoft_native_stat(name,&st)!=0)
183+
{
184+
pg_usleep(100000);
185+
loops++;
186+
continue;
187+
}
188+
}
235189
}
236-
}
237190

238-
if (!NT_SUCCESS(_NtQueryInformationFile(hFile,&ioStatus,&standardInfo,
239-
sizeof(standardInfo),
240-
FileStandardInformation)))
241-
{
242-
_dosmaperr(GetLastError());
243-
CloseHandle(hFile);
244-
return-1;
245-
}
246-
#else
247-
if (!GetFileInformationByHandleEx(hFile,FileStandardInfo,&standardInfo,
248-
sizeof(standardInfo)))
249-
{
250-
_dosmaperr(GetLastError());
251-
CloseHandle(hFile);
252-
return-1;
253-
}
254-
#endif/* _WIN32_WINNT < 0x0600 */
255-
256-
if (standardInfo.DeletePending)
257-
{
258-
/*
259-
* File has been deleted, but is not gone from the filesystem yet.
260-
* This can happen when some process with FILE_SHARE_DELETE has it
261-
* open, and it will be fully removed once that handle is closed.
262-
* Meanwhile, we can't open it, so indicate that the file just doesn't
263-
* exist.
264-
*/
265-
CloseHandle(hFile);
266-
errno=ENOENT;
191+
_dosmaperr(err);
267192
return-1;
268193
}
269194

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp