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

Commit5c32549

Browse files
committed
Fix detection of unseekable files for fseek() and ftello() with MSVC
Calling fseek() or ftello() on a handle to a non-seeking device such asa pipe or a communications device is not supported. Unfortunately,MSVC's flavor of these routines, _fseeki64() and _ftelli64(), do notreturn an error when given a pipe as handle. Some of the logic ofpg_dump and restore relies on these routines to check if a handle isseekable, causing failures when passing the contents of pg_dump topg_restore through a pipe, for example.This commit introduces wrappers for fseeko() and ftello() on MSVC so asany callers are able to properly detect the cases of non-seekablehandles. This relies mainly on GetFileType(), sharing a bit of codewith the MSVC port for fstat(). The code in charge of getting a filetype is refactored into a new file called win32common.c, shared bywin32stat.c and the new win32fseek.c. It includes the MSVC ports forfseeko() and ftello().Like765f5df, this is backpatched down to 14, where the fstat()implementation for MSVC is able to understand about files larger than4GB in size. Using a TAP test for that is proving to be tricky asIPC::Run handles the pipes by itself, still I have been able to checkthe fix manually.Reported-by: Daniel WatzingerAuthor: Juan José Santamaría Flecha, Michael PaquierDiscussion:https://postgr.es/m/CAC+AXB26a4EmxM2suXxPpJaGrqAdxracd7hskLg-zxtPB50h7A@mail.gmail.comBackpatch-through: 14
1 parent8b07ee0 commit5c32549

File tree

7 files changed

+163
-23
lines changed

7 files changed

+163
-23
lines changed

‎configure

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17287,6 +17287,12 @@ esac
1728717287
;;
1728817288
esac
1728917289

17290+
case " $LIBOBJS " in
17291+
*" win32common.$ac_objext "* ) ;;
17292+
*) LIBOBJS="$LIBOBJS win32common.$ac_objext"
17293+
;;
17294+
esac
17295+
1729017296
case " $LIBOBJS " in
1729117297
*" win32env.$ac_objext "* ) ;;
1729217298
*) LIBOBJS="$LIBOBJS win32env.$ac_objext"

‎configure.ac

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2004,6 +2004,7 @@ if test "$PORTNAME" = "win32"; then
20042004
AC_LIBOBJ(kill)
20052005
AC_LIBOBJ(open)
20062006
AC_LIBOBJ(system)
2007+
AC_LIBOBJ(win32common)
20072008
AC_LIBOBJ(win32env)
20082009
AC_LIBOBJ(win32error)
20092010
AC_LIBOBJ(win32ntdll)

‎src/include/port/win32_port.h

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -201,15 +201,21 @@ struct itimerval
201201

202202
intsetitimer(intwhich,conststructitimerval*value,structitimerval*ovalue);
203203

204+
/* Convenience wrapper for GetFileType() */
205+
externDWORDpgwin32_get_file_type(HANDLEhFile);
206+
204207
/*
205208
* WIN32 does not provide 64-bit off_t, but does provide the functions operating
206-
* with 64-bit offsets.
209+
* with 64-bit offsets. Also, fseek() might not give an error for unseekable
210+
* streams, so harden that function with our version.
207211
*/
208212
#definepgoff_t __int64
209213

210214
#ifdef_MSC_VER
211-
#definefseeko(stream,offset,origin) _fseeki64(stream, offset, origin)
212-
#defineftello(stream) _ftelli64(stream)
215+
externint_pgfseeko64(FILE*stream,pgoff_toffset,intorigin);
216+
externpgoff_t_pgftello64(FILE*stream);
217+
#definefseeko(stream,offset,origin) _pgfseeko64(stream, offset, origin)
218+
#defineftello(stream) _pgftello64(stream)
213219
#else
214220
#ifndeffseeko
215221
#definefseeko(stream,offset,origin) fseeko64(stream, offset, origin)

‎src/port/win32common.c

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
/*-------------------------------------------------------------------------
2+
*
3+
* win32common.c
4+
* Common routines shared among the win32*.c ports.
5+
*
6+
* Portions Copyright (c) 1996-2023, PostgreSQL Global Development Group
7+
* Portions Copyright (c) 1994, Regents of the University of California
8+
*
9+
*
10+
* IDENTIFICATION
11+
* src/port/win32common.c
12+
*
13+
*-------------------------------------------------------------------------
14+
*/
15+
16+
#ifdefFRONTEND
17+
#include"postgres_fe.h"
18+
#else
19+
#include"postgres.h"
20+
#endif
21+
22+
#ifdefWIN32
23+
24+
/*
25+
* pgwin32_get_file_type
26+
*
27+
* Convenience wrapper for GetFileType() with specific error handling for all the
28+
* port implementations. Returns the file type associated with a HANDLE.
29+
*
30+
* On error, sets errno with FILE_TYPE_UNKNOWN as file type.
31+
*/
32+
DWORD
33+
pgwin32_get_file_type(HANDLEhFile)
34+
{
35+
DWORDfileType=FILE_TYPE_UNKNOWN;
36+
DWORDlastError;
37+
38+
errno=0;
39+
40+
/*
41+
* When stdin, stdout, and stderr aren't associated with a stream the
42+
* special value -2 is returned:
43+
* https://learn.microsoft.com/en-us/cpp/c-runtime-library/reference/get-osfhandle
44+
*/
45+
if (hFile==INVALID_HANDLE_VALUE||hFile== (HANDLE)-2)
46+
{
47+
errno=EINVAL;
48+
returnFILE_TYPE_UNKNOWN;
49+
}
50+
51+
fileType=GetFileType(hFile);
52+
lastError=GetLastError();
53+
54+
/*
55+
* Invoke GetLastError in order to distinguish between a "valid" return of
56+
* FILE_TYPE_UNKNOWN and its return due to a calling error. In case of
57+
* success, GetLastError() returns NO_ERROR.
58+
*/
59+
if (fileType==FILE_TYPE_UNKNOWN&&lastError!=NO_ERROR)
60+
{
61+
_dosmaperr(lastError);
62+
returnFILE_TYPE_UNKNOWN;
63+
}
64+
65+
returnfileType;
66+
}
67+
68+
#endif/* WIN32 */

‎src/port/win32fseek.c

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
/*-------------------------------------------------------------------------
2+
*
3+
* win32fseek.c
4+
* Replacements for fseeko() and ftello().
5+
*
6+
* Portions Copyright (c) 1996-2023, PostgreSQL Global Development Group
7+
*
8+
* IDENTIFICATION
9+
* src/port/win32fseek.c
10+
*
11+
*-------------------------------------------------------------------------
12+
*/
13+
14+
#ifdefFRONTEND
15+
#include"postgres_fe.h"
16+
#else
17+
#include"postgres.h"
18+
#endif
19+
20+
#if defined(WIN32)&& defined(_MSC_VER)
21+
22+
/*
23+
* _pgfseeko64
24+
*
25+
* Calling fseek() on a handle to a non-seeking device such as a pipe or
26+
* a communications device is not supported, and fseek() may not return
27+
* an error. This wrapper relies on the file type to check which cases
28+
* are supported.
29+
*/
30+
int
31+
_pgfseeko64(FILE*stream,pgoff_toffset,intorigin)
32+
{
33+
DWORDfileType;
34+
HANDLEhFile= (HANDLE)_get_osfhandle(_fileno(stream));
35+
36+
fileType=pgwin32_get_file_type(hFile);
37+
if (errno!=0)
38+
return-1;
39+
40+
if (fileType==FILE_TYPE_DISK)
41+
return_fseeki64(stream,offset,origin);
42+
elseif (fileType==FILE_TYPE_CHAR||fileType==FILE_TYPE_PIPE)
43+
errno=ESPIPE;
44+
else
45+
errno=EINVAL;
46+
47+
return-1;
48+
}
49+
50+
/*
51+
* _pgftello64
52+
*
53+
* Same as _pgfseeko64().
54+
*/
55+
pgoff_t
56+
_pgftello64(FILE*stream)
57+
{
58+
DWORDfileType;
59+
HANDLEhFile= (HANDLE)_get_osfhandle(_fileno(stream));
60+
61+
fileType=pgwin32_get_file_type(hFile);
62+
if (errno!=0)
63+
return-1;
64+
65+
if (fileType==FILE_TYPE_DISK)
66+
return_ftelli64(stream);
67+
elseif (fileType==FILE_TYPE_CHAR||fileType==FILE_TYPE_PIPE)
68+
errno=ESPIPE;
69+
else
70+
errno=EINVAL;
71+
72+
return-1;
73+
}
74+
75+
#endif/* defined(WIN32) && defined(_MSC_VER) */

‎src/port/win32stat.c

Lines changed: 3 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -141,33 +141,17 @@ _pgfstat64(int fileno, struct stat *buf)
141141
{
142142
HANDLEhFile= (HANDLE)_get_osfhandle(fileno);
143143
DWORDfileType=FILE_TYPE_UNKNOWN;
144-
DWORDlastError;
145144
unsigned shortst_mode;
146145

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)
146+
if (buf==NULL)
153147
{
154148
errno=EINVAL;
155149
return-1;
156150
}
157151

158-
fileType=GetFileType(hFile);
159-
lastError=GetLastError();
160-
161-
/*
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.
165-
*/
166-
if (fileType==FILE_TYPE_UNKNOWN&&lastError!=NO_ERROR)
167-
{
168-
_dosmaperr(lastError);
152+
fileType=pgwin32_get_file_type(hFile);
153+
if (errno!=0)
169154
return-1;
170-
}
171155

172156
switch (fileType)
173157
{

‎src/tools/msvc/Mkvcbuild.pm

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,7 @@ sub mkvcbuild
107107
pg_strong_random.c pgcheckdir.c pgmkdirp.c pgsleep.c pgstrcasecmp.c
108108
pqsignal.c mkdtemp.c qsort.c qsort_arg.c bsearch_arg.c quotes.c system.c
109109
strerror.c tar.c
110-
win32env.c win32error.c win32ntdll.c
110+
win32common.cwin32env.c win32error.c win32fseek.c win32ntdll.c
111111
win32security.c win32setlocale.c win32stat.c);
112112

113113
push(@pgportfiles,'strtof.c')if ($vsVersion <'14.00');

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp