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

Commit765f5df

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 parent89e46da commit765f5df

File tree

1 file changed

+49
-17
lines changed

1 file changed

+49
-17
lines changed

‎src/port/win32stat.c

Lines changed: 49 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -257,34 +257,66 @@ int
257257
_pgfstat64(intfileno,structstat*buf)
258258
{
259259
HANDLEhFile= (HANDLE)_get_osfhandle(fileno);
260-
BY_HANDLE_FILE_INFORMATIONfiData;
260+
DWORDfileType=FILE_TYPE_UNKNOWN;
261+
DWORDlastError;
262+
unsigned shortst_mode;
261263

262-
if (hFile==INVALID_HANDLE_VALUE||buf==NULL)
264+
/*
265+
* When stdin, stdout, and stderr aren't associated with a stream the
266+
* special value -2 is returned:
267+
* https://learn.microsoft.com/en-us/cpp/c-runtime-library/reference/get-osfhandle
268+
*/
269+
if (hFile==INVALID_HANDLE_VALUE||hFile== (HANDLE)-2||buf==NULL)
263270
{
264271
errno=EINVAL;
265272
return-1;
266273
}
267274

275+
fileType=GetFileType(hFile);
276+
lastError=GetLastError();
277+
268278
/*
269-
* Check if the fileno is a data stream. If so, unless it has been
270-
* redirected to a file, getting information through its HANDLE will fail,
271-
* so emulate its stat information in the most appropriate way and return
272-
* it instead.
279+
* Invoke GetLastError in order to distinguish between a "valid" return of
280+
* FILE_TYPE_UNKNOWN and its return due to a calling error. In case of
281+
* success, GetLastError returns NO_ERROR.
273282
*/
274-
if ((fileno==_fileno(stdin)||
275-
fileno==_fileno(stdout)||
276-
fileno==_fileno(stderr))&&
277-
!GetFileInformationByHandle(hFile,&fiData))
283+
if (fileType==FILE_TYPE_UNKNOWN&&lastError!=NO_ERROR)
284+
{
285+
_dosmaperr(lastError);
286+
return-1;
287+
}
288+
289+
switch (fileType)
278290
{
279-
memset(buf,0,sizeof(*buf));
280-
buf->st_mode=_S_IFCHR;
281-
buf->st_dev=fileno;
282-
buf->st_rdev=fileno;
283-
buf->st_nlink=1;
284-
return0;
291+
/* The specified file is a disk file */
292+
caseFILE_TYPE_DISK:
293+
returnfileinfo_to_stat(hFile,buf);
294+
295+
/*
296+
* The specified file is a socket, a named pipe, or an anonymous
297+
* pipe.
298+
*/
299+
caseFILE_TYPE_PIPE:
300+
st_mode=_S_IFIFO;
301+
break;
302+
/* The specified file is a character file */
303+
caseFILE_TYPE_CHAR:
304+
st_mode=_S_IFCHR;
305+
break;
306+
/* Unused flag and unknown file type */
307+
caseFILE_TYPE_REMOTE:
308+
caseFILE_TYPE_UNKNOWN:
309+
default:
310+
errno=EINVAL;
311+
return-1;
285312
}
286313

287-
returnfileinfo_to_stat(hFile,buf);
314+
memset(buf,0,sizeof(*buf));
315+
buf->st_mode=st_mode;
316+
buf->st_dev=fileno;
317+
buf->st_rdev=fileno;
318+
buf->st_nlink=1;
319+
return0;
288320
}
289321

290322
#endif/* WIN32 */

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp