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

Commitbbf18fe

Browse files
committed
Improve WIN32 port of fstat() to detect more file types
The current implementation of _pgfstat64() is ineffective in detecting aterminal handle or an anonymous named pipe. This commit improves ourport of fstat() to detect more efficiently such cases by relying onGetFileType(), and returning more correct data when the type found iseither 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 theoutput 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 aboutfseek() and ftello() which are not reliable on WIN32 for the same caseswhere fstat() was incorrect. Fixing fstat() is independent of the rest,though, which is why both fixes are handled separately, and this is thefirst part of it.Reported-by: Daniel WatzingerAuthor: Daniel Watzinger, Juan José Santamaría FlechaDiscussion:https://postgr.es/m/b1448cd7-871e-20e3-8398-895e2d1d3bf9@gmail.comBackpatch-through: 14
1 parent2bef57e commitbbf18fe

File tree

1 file changed

+48
-21
lines changed

1 file changed

+48
-21
lines changed

‎src/port/win32stat.c

Lines changed: 48 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -289,39 +289,66 @@ int
289289
_pgfstat64(intfileno,structstat*buf)
290290
{
291291
HANDLEhFile= (HANDLE)_get_osfhandle(fileno);
292-
BY_HANDLE_FILE_INFORMATIONfiData;
292+
DWORDfileType=FILE_TYPE_UNKNOWN;
293+
DWORDlastError;
294+
unsigned shortst_mode;
293295

294-
if (hFile==INVALID_HANDLE_VALUE||buf==NULL)
296+
/*
297+
* When stdin, stdout, and stderr aren't associated with a stream the
298+
* special value -2 is returned:
299+
* https://learn.microsoft.com/en-us/cpp/c-runtime-library/reference/get-osfhandle
300+
*/
301+
if (hFile==INVALID_HANDLE_VALUE||hFile== (HANDLE)-2||buf==NULL)
295302
{
296303
errno=EINVAL;
297304
return-1;
298305
}
299306

307+
fileType=GetFileType(hFile);
308+
lastError=GetLastError();
309+
300310
/*
301-
* Check if the fileno is a data stream. If so, unless it has been
302-
* redirected to a file, getting information through its HANDLE will fail,
303-
* so emulate its stat information in the most appropriate way and return
304-
* it instead.
311+
* Invoke GetLastError in order to distinguish between a "valid" return of
312+
* FILE_TYPE_UNKNOWN and its return due to a calling error. In case of
313+
* success, GetLastError returns NO_ERROR.
305314
*/
306-
if ((fileno==_fileno(stdin)||
307-
fileno==_fileno(stdout)||
308-
fileno==_fileno(stderr))&&
309-
!GetFileInformationByHandle(hFile,&fiData))
315+
if (fileType==FILE_TYPE_UNKNOWN&&lastError!=NO_ERROR)
310316
{
311-
memset(buf,0,sizeof(*buf));
312-
buf->st_mode=_S_IFCHR;
313-
buf->st_dev=fileno;
314-
buf->st_rdev=fileno;
315-
buf->st_nlink=1;
316-
return0;
317+
_dosmaperr(lastError);
318+
return-1;
317319
}
318320

319-
/*
320-
* Since we already have a file handle there is no need to check for
321-
* ERROR_DELETE_PENDING.
322-
*/
321+
switch (fileType)
322+
{
323+
/* The specified file is a disk file */
324+
caseFILE_TYPE_DISK:
325+
returnfileinfo_to_stat(hFile,buf);
326+
327+
/*
328+
* The specified file is a socket, a named pipe, or an anonymous
329+
* pipe.
330+
*/
331+
caseFILE_TYPE_PIPE:
332+
st_mode=_S_IFIFO;
333+
break;
334+
/* The specified file is a character file */
335+
caseFILE_TYPE_CHAR:
336+
st_mode=_S_IFCHR;
337+
break;
338+
/* Unused flag and unknown file type */
339+
caseFILE_TYPE_REMOTE:
340+
caseFILE_TYPE_UNKNOWN:
341+
default:
342+
errno=EINVAL;
343+
return-1;
344+
}
323345

324-
returnfileinfo_to_stat(hFile,buf);
346+
memset(buf,0,sizeof(*buf));
347+
buf->st_mode=st_mode;
348+
buf->st_dev=fileno;
349+
buf->st_rdev=fileno;
350+
buf->st_nlink=1;
351+
return0;
325352
}
326353

327354
#endif/* WIN32 */

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp