diff options
| author | Michael Paquier | 2023-04-12 00:09:38 +0000 |
|---|---|---|
| committer | Michael Paquier | 2023-04-12 00:09:38 +0000 |
| commit | a923e21631a29dc8b8781d7d02b5003d0df64ca3 (patch) | |
| tree | 96f01ccbd81dede97fb157a839a250a6564f79ef /src/include | |
| parent | c03c2eae0acbb82db8e626f71f367ef4b043d27d (diff) | |
Fix detection of unseekable files for fseek() and ftello() with MSVC
Calling fseek() or ftello() on a handle to a non-seeking device such as
a pipe or a communications device is not supported. Unfortunately,
MSVC's flavor of these routines, _fseeki64() and _ftelli64(), do not
return an error when given a pipe as handle. Some of the logic of
pg_dump and restore relies on these routines to check if a handle is
seekable, causing failures when passing the contents of pg_dump to
pg_restore through a pipe, for example.
This commit introduces wrappers for fseeko() and ftello() on MSVC so as
any callers are able to properly detect the cases of non-seekable
handles. This relies mainly on GetFileType(), sharing a bit of code
with the MSVC port for fstat(). The code in charge of getting a file
type is refactored into a new file called win32common.c, shared by
win32stat.c and the new win32fseek.c. It includes the MSVC ports for
fseeko() and ftello().
Like 765f5df, this is backpatched down to 14, where the fstat()
implementation for MSVC is able to understand about files larger than
4GB in size. Using a TAP test for that is proving to be tricky as
IPC::Run handles the pipes by itself, still I have been able to check
the fix manually.
Reported-by: Daniel Watzinger
Author: Juan José SantamarÃa Flecha, Michael Paquier
Discussion: https://postgr.es/m/CAC+AXB26a4EmxM2suXxPpJaGrqAdxracd7hskLg-zxtPB50h7A@mail.gmail.com
Backpatch-through: 14
Diffstat (limited to 'src/include')
| -rw-r--r-- | src/include/port/win32_port.h | 12 |
1 files changed, 9 insertions, 3 deletions
diff --git a/src/include/port/win32_port.h b/src/include/port/win32_port.h index 5116c2fc06c..58965e0dfd7 100644 --- a/src/include/port/win32_port.h +++ b/src/include/port/win32_port.h @@ -204,15 +204,21 @@ struct itimerval int setitimer(int which, const struct itimerval *value, struct itimerval *ovalue); +/* Convenience wrapper for GetFileType() */ +extern DWORD pgwin32_get_file_type(HANDLE hFile); + /* * WIN32 does not provide 64-bit off_t, but does provide the functions operating - * with 64-bit offsets. + * with 64-bit offsets. Also, fseek() might not give an error for unseekable + * streams, so harden that function with our version. */ #define pgoff_t __int64 #ifdef _MSC_VER -#define fseeko(stream, offset, origin) _fseeki64(stream, offset, origin) -#define ftello(stream) _ftelli64(stream) +extern int _pgfseeko64(FILE *stream, pgoff_t offset, int origin); +extern pgoff_t _pgftello64(FILE *stream); +#define fseeko(stream, offset, origin) _pgfseeko64(stream, offset, origin) +#define ftello(stream) _pgftello64(stream) #else #ifndef fseeko #define fseeko(stream, offset, origin) fseeko64(stream, offset, origin) |
