From: Daniel Watzinger <daniel.watzinger@gmail.com>
To: Daniel Gustafsson <daniel@yesql.se>
Cc: pgsql-hackers@postgresql.org
Subject: pg_dump/pg_restore: Fix stdin/stdout handling of custom format on Win32
Date: Sat, 17 Dec 2022 00:55:24 +0100
Message-ID: <20221216235524.8483-1-daniel.watzinger@gmail.com> (raw)
In-Reply-To: <0F43E234-48F2-4C99-BCBE-6C41F053C666@yesql.se>
References: <0F43E234-48F2-4C99-BCBE-6C41F053C666@yesql.se>
Well, this is embarassing. Sorry for the inconvenience. Some part
of my company's network infrastruture must have mangled the attachment.
Both mails were sent using a combination of git format-patch
and git send-email. However, as this is my first foray into this
email-based workflow, I won't rule out a failure on my part. Bear
with me and let's try again.
Attachments:
[text/x-patch] v1-0001-pg_dump-pg_restore-Fix-stdin-stdout-handling-of-c.patch (3.6K, ../20221216235524.8483-1-daniel.watzinger@gmail.com/2-v1-0001-pg_dump-pg_restore-Fix-stdin-stdout-handling-of-c.patch)
download | inline diff:diff --git a/src/bin/pg_dump/pg_backup_archiver.c b/src/bin/pg_dump/pg_backup_archiver.cindex 7f7a0f1ce7..684db4a17a 100644--- a/src/bin/pg_dump/pg_backup_archiver.c+++ b/src/bin/pg_dump/pg_backup_archiver.c@@ -3838,6 +3838,18 @@ bool
checkSeek(FILE *fp)
{
pgoff_t tpos;
+ struct stat st;++ /* Check if this is a terminal descriptor */+ if (isatty(fileno(fp))) {+ return false;+ }++ /* Check if this is an unseekable character special device or pipe */+ if ((fstat(fileno(fp), &st) == 0) && (S_ISCHR(st.st_mode)+ || S_ISFIFO(st.st_mode))) {+ return false;+ }
/* Check that ftello works on this file */
tpos = ftello(fp);
diff --git a/src/include/port/win32_port.h b/src/include/port/win32_port.hindex 296905bc6c..892d0bb401 100644--- a/src/include/port/win32_port.h+++ b/src/include/port/win32_port.h@@ -329,6 +329,12 @@ extern int _pglstat64(const char *name, struct stat *buf);
#ifndef S_ISREG
#define S_ISREG(m) (((m) & S_IFMT) == S_IFREG)
#endif
+#ifndef S_ISFIFO+#define S_ISFIFO(m) (((m) & S_IFMT) == _S_IFIFO)+#endif+#ifndef S_ISCHR+#define S_ISCHR(m) (((m) & S_IFMT) == S_IFCHR)+#endif
/*
* In order for lstat() to be able to report junction points as symlinks, we
diff --git a/src/port/win32stat.c b/src/port/win32stat.cindex e6553e4030..7ab0983a74 100644--- a/src/port/win32stat.c+++ b/src/port/win32stat.c@@ -256,35 +256,65 @@ _pgstat64(const char *name, struct stat *buf)
int
_pgfstat64(int fileno, struct stat *buf)
{
- HANDLE hFile = (HANDLE) _get_osfhandle(fileno);- BY_HANDLE_FILE_INFORMATION fiData;+ HANDLE hFile = (HANDLE) _get_osfhandle(fileno);+ DWORD fileType = FILE_TYPE_UNKNOWN;+ DWORD lastError;+ unsigned short st_mode;- if (hFile == INVALID_HANDLE_VALUE || buf == NULL)+ if (hFile == INVALID_HANDLE_VALUE || hFile == (HANDLE)-2 || buf == NULL)
{
errno = EINVAL;
return -1;
}
- /*- * Check if the fileno is a data stream. If so, unless it has been- * redirected to a file, getting information through its HANDLE will fail,- * so emulate its stat information in the most appropriate way and return- * it instead.+ fileType = GetFileType(hFile);+ lastError = GetLastError();++ /* + * Invoke GetLastError in order to distinguish between a "valid"+ * return of FILE_TYPE_UNKNOWN and its return due to a calling error.+ * In case of success, GetLastError returns NO_ERROR.
*/
- if ((fileno == _fileno(stdin) ||- fileno == _fileno(stdout) ||- fileno == _fileno(stderr)) &&- !GetFileInformationByHandle(hFile, &fiData))+ if (fileType == FILE_TYPE_UNKNOWN && lastError != NO_ERROR) {+ _dosmaperr(lastError);+ return -1;+ }++ switch (fileType)
{
- memset(buf, 0, sizeof(*buf));- buf->st_mode = _S_IFCHR;- buf->st_dev = fileno;- buf->st_rdev = fileno;- buf->st_nlink = 1;- return 0;+ /* The specified file is a disk file */+ case FILE_TYPE_DISK:+ return fileinfo_to_stat(hFile, buf);+ /*+ * The specified file is a socket,+ * a named pipe, or an anonymous pipe.+ */+ case FILE_TYPE_PIPE:+ st_mode = _S_IFIFO;+ break;+ /* + * The specified file is a character file,+ * typically an LPT device or a console+ */+ case FILE_TYPE_CHAR:+ /* + * Unused flag and unknown file type+ */+ case FILE_TYPE_REMOTE:+ case FILE_TYPE_UNKNOWN:+ st_mode = _S_IFCHR;+ break;+ default:+ errno = EINVAL;+ return -1;
}
- return fileinfo_to_stat(hFile, buf);+ memset(buf, 0, sizeof(*buf));+ buf->st_mode = st_mode;+ buf->st_dev = fileno;+ buf->st_rdev = fileno;+ buf->st_nlink = 1;+ return 0;
}
#endif /* WIN32 */
base-commit: e52f8b301ed54aac5162b185b43f5f1e44b6b17e
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Reply to all the recipients using the --to and --cc options:
reply via email
To: pgsql-hackers@postgresql.org
Cc: daniel.watzinger@gmail.com, daniel@yesql.se
Subject: Re: pg_dump/pg_restore: Fix stdin/stdout handling of custom format on Win32
In-Reply-To: <20221216235524.8483-1-daniel.watzinger@gmail.com>
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox