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

Commit69b6032

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 parentd9c9c43 commit69b6032

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
@@ -140,34 +140,66 @@ int
140140
_pgfstat64(intfileno,structstat*buf)
141141
{
142142
HANDLEhFile= (HANDLE)_get_osfhandle(fileno);
143-
BY_HANDLE_FILE_INFORMATIONfiData;
143+
DWORDfileType=FILE_TYPE_UNKNOWN;
144+
DWORDlastError;
145+
unsigned shortst_mode;
144146

145-
if (hFile==INVALID_HANDLE_VALUE||buf==NULL)
147+
/*
148+
* When stdin, stdout, and stderr aren't associated with a stream the
149+
* special value -2 is returned:
150+
* https://learn.microsoft.com/en-us/cpp/c-runtime-library/reference/get-osfhandle
151+
*/
152+
if (hFile==INVALID_HANDLE_VALUE||hFile== (HANDLE)-2||buf==NULL)
146153
{
147154
errno=EINVAL;
148155
return-1;
149156
}
150157

158+
fileType=GetFileType(hFile);
159+
lastError=GetLastError();
160+
151161
/*
152-
* Check if the fileno is a data stream. If so, unless it has been
153-
* redirected to a file, getting information through its HANDLE will fail,
154-
* so emulate its stat information in the most appropriate way and return
155-
* it instead.
162+
* Invoke GetLastError in order to distinguish between a "valid" return of
163+
* FILE_TYPE_UNKNOWN and its return due to a calling error. In case of
164+
* success, GetLastError returns NO_ERROR.
156165
*/
157-
if ((fileno==_fileno(stdin)||
158-
fileno==_fileno(stdout)||
159-
fileno==_fileno(stderr))&&
160-
!GetFileInformationByHandle(hFile,&fiData))
166+
if (fileType==FILE_TYPE_UNKNOWN&&lastError!=NO_ERROR)
167+
{
168+
_dosmaperr(lastError);
169+
return-1;
170+
}
171+
172+
switch (fileType)
161173
{
162-
memset(buf,0,sizeof(*buf));
163-
buf->st_mode=_S_IFCHR;
164-
buf->st_dev=fileno;
165-
buf->st_rdev=fileno;
166-
buf->st_nlink=1;
167-
return0;
174+
/* The specified file is a disk file */
175+
caseFILE_TYPE_DISK:
176+
returnfileinfo_to_stat(hFile,buf);
177+
178+
/*
179+
* The specified file is a socket, a named pipe, or an anonymous
180+
* pipe.
181+
*/
182+
caseFILE_TYPE_PIPE:
183+
st_mode=_S_IFIFO;
184+
break;
185+
/* The specified file is a character file */
186+
caseFILE_TYPE_CHAR:
187+
st_mode=_S_IFCHR;
188+
break;
189+
/* Unused flag and unknown file type */
190+
caseFILE_TYPE_REMOTE:
191+
caseFILE_TYPE_UNKNOWN:
192+
default:
193+
errno=EINVAL;
194+
return-1;
168195
}
169196

170-
returnfileinfo_to_stat(hFile,buf);
197+
memset(buf,0,sizeof(*buf));
198+
buf->st_mode=st_mode;
199+
buf->st_dev=fileno;
200+
buf->st_rdev=fileno;
201+
buf->st_nlink=1;
202+
return0;
171203
}
172204

173205
#endif/* WIN32 */

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp