pg_dump/pg_restore: Fix stdin/stdout handling of custom format on Win32

From: Daniel Watzinger <daniel(dot)watzinger(at)gmail(dot)com>
To: pgsql-hackers(at)postgresql(dot)org
Subject: pg_dump/pg_restore: Fix stdin/stdout handling of custom format on Win32
Date: 2022-12-16 15:09:21
Message-ID: b1448cd7-871e-20e3-8398-895e2d1d3bf9@gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-hackers

Hi there,

first-time contributor here. I certainly hope I got the patch
creation and email workflow right. Let me know if anything can be
improved as I`m eager to learn. Regression tests (check) were
successful on native Win32 MSVC as well as Debian. Here comes the
patch and corresponding commit text.

During archive initialization pg_backup_custom.c determines if the file
pointer it should read from or write to is seekable. pg_dump
uses this information to rewrite the custom output format's TOC
enriched with known offsets into the archive on close. pg_restore uses
seeking to speed up file operations when searching for specific
blocks within the archive.

The seekable property of a file pointer is currently checked by
invoking ftello and subsequently fseeko. Both calls succeed
on Windows platforms if the underlying file descriptor represents a
terminal handle or an anonymous or named pipe. Obviously, these type
of devices do not support seeking. In the case of pg_dump, this
leads to the TOC being appended to the end of the output when attempting
to rewrite known offsets. Furthermore, pg_restore may try to seek to
known file offsets if the custom format archive's TOC supports it
and subsequently fails to locate blocks.

This commit improves the detection of the seekable property by checking
a descriptor's file type (st_mode) and filtering character special
devices and pipes. The current customized implementation of fstat on
Windows platforms (_pgfstat64) erroneously marks terminal and pipe
handles as regular files (_S_IFREG). This was improved on by
utilizing WinAPI functionality (GetFileType) to correctly distinguish
and flag descriptors based on their native OS handle's file type.

Daniel

---
src/bin/pg_dump/pg_backup_archiver.c | 12 +++++
src/include/port/win32_port.h | 6 +++
src/port/win32stat.c | 68 ++++++++++++++++++++--------
3 files changed, 67 insertions(+), 19 deletions(-)

Attachment Content-Type Size
v1-0001-pg_dump-pg_restore-Fix-stdin-stdout-handling-of-c.patch application/x-patch 1.8 KB

Responses

Browse pgsql-hackers by date

  From Date Subject
Next Message Daniel Gustafsson 2022-12-16 16:06:48 Re: pg_dump/pg_restore: Fix stdin/stdout handling of custom format on Win32
Previous Message Drouvot, Bertrand 2022-12-16 15:08:13 Re: Minimal logical decoding on standbys