Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1p6rCo-0003mV-MD for pgsql-hackers@arkaria.postgresql.org; Sun, 18 Dec 2022 10:47:42 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.92) (envelope-from ) id 1p6rCn-0004J0-Jt for pgsql-hackers@arkaria.postgresql.org; Sun, 18 Dec 2022 10:47:41 +0000 Received: from magus.postgresql.org ([2a02:c0:301:0:ffff::29]) by malur.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1p6Ka4-0001vZ-6H for pgsql-hackers@lists.postgresql.org; Fri, 16 Dec 2022 23:57:32 +0000 Received: from mail-ej1-x634.google.com ([2a00:1450:4864:20::634]) by magus.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.92) (envelope-from ) id 1p6KZx-0007Tr-Sg for pgsql-hackers@postgresql.org; Fri, 16 Dec 2022 23:57:31 +0000 Received: by mail-ej1-x634.google.com with SMTP id x22so9638834ejs.11 for ; Fri, 16 Dec 2022 15:57:25 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=HIlSUR4n2ve0RYFTKERmIuxgPqoaAqdhoWP0+15d9Oc=; b=my4Ph8YomsTP4BFrqVj02xbE43iy5AfAaDxANH84F4XAH4MqZj8f/qAHfJGeNpjika RMIqFtgPtMw/K6BoVsqi3Dh/VUQXmUeKmVVL3rDbc90+qwXKoDslF7QErOU6F8+sXu8f e0OC/eQicJq79kMGo/97358DhNKaf+9RDw24604ghmKFLvkfARdFEnssExUOdm7UadLf Eo5eYj//7R6JvR6dLmuaXxbwj/Vr+FLra5C04abah6PFziRDri3Pc//Cq0/+XAZ6W5wb tugB+ctiSqh0T4EjYxjHVd0Pi12e6UmZ9G1KxTRi0X7+Y8KDvIJ3WScrb5hVVRelI+kG +Xaw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=HIlSUR4n2ve0RYFTKERmIuxgPqoaAqdhoWP0+15d9Oc=; b=rdmSYFGG9Jg3EN+UoMbXmbh9z1jhYqvrNTANbZEYNO1mbIVfYZBnnwaUrCYt+AYvSd Q9vPfV+kAcJ5dVu+c5nbVa43Rbfctb9SAT/xQLEOwP+qO9u017/cNtCfkCltyMmWjEW9 drzBe1CeoFYzrQdX7cvbFcVnmXR4bJZet+AO/vmIJLFMa1/cJrfZ9qbY0lpvZny524Wf GVS+pn/kpVnEl3dwRiyeJ76ELAqVbIsKQ+keOnzUvCVYVpNZKVb9qVwxVPFqFLW0QiAu uZsPSAlb3WrMPa57mxZjmhIiwCb5fdR42B/maxDuMSiMtYaAqkycTWDi+Flv89tcjtTH xm/w== X-Gm-Message-State: ANoB5pkO+hH//xLcDP4Ums0gR/M7psLbv7x+fzL7q1e48wcC8sebGb3y R5IqF7pu0lpBeX3Ce8FmcnxPTdaQabxltg== X-Google-Smtp-Source: AA0mqf40dbGnZBfx5/CDdADHQYpXaKSKpfycvwYks1JX6x/kaefjm5GCYiGJGoWDsnkWJTeYjtBmdA== X-Received: by 2002:a17:906:3917:b0:7bf:1081:9472 with SMTP id f23-20020a170906391700b007bf10819472mr26816300eje.69.1671235043766; Fri, 16 Dec 2022 15:57:23 -0800 (PST) Received: from DeskMini.fritz.box (p200300d52f00a200e8c7fe65383f739a.dip0.t-ipconnect.de. [2003:d5:2f00:a200:e8c7:fe65:383f:739a]) by smtp.gmail.com with ESMTPSA id mh11-20020a170906eb8b00b007ad69e9d34dsm1368122ejb.54.2022.12.16.15.57.22 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 16 Dec 2022 15:57:22 -0800 (PST) From: Daniel Watzinger To: Daniel Gustafsson 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> X-Mailer: git-send-email 2.35.1 In-Reply-To: <0F43E234-48F2-4C99-BCBE-6C41F053C666@yesql.se> References: <0F43E234-48F2-4C99-BCBE-6C41F053C666@yesql.se> MIME-Version: 1.0 Content-Type: multipart/mixed; boundary="------------2.39.0.windows.1" Content-Transfer-Encoding: 8bit List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk This is a multi-part message in MIME format. --------------2.39.0.windows.1 Content-Type: text/plain; charset=UTF-8; format=fixed Content-Transfer-Encoding: 8bit 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. --------------2.39.0.windows.1 Content-Type: text/x-patch; name="v1-0001-pg_dump-pg_restore-Fix-stdin-stdout-handling-of-c.patch" Content-Transfer-Encoding: 8bit Content-Disposition: attachment; filename="v1-0001-pg_dump-pg_restore-Fix-stdin-stdout-handling-of-c.patch" diff --git a/src/bin/pg_dump/pg_backup_archiver.c b/src/bin/pg_dump/pg_backup_archiver.c index 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.h index 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.c index 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 --------------2.39.0.windows.1--