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 1p6D6D-0003jc-RL for pgsql-hackers@arkaria.postgresql.org; Fri, 16 Dec 2022 15:58:13 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.92) (envelope-from ) id 1p6D6B-0008ES-E2 for pgsql-hackers@arkaria.postgresql.org; Fri, 16 Dec 2022 15:58:11 +0000 Received: from makus.postgresql.org ([2001:4800:3e1:1::229]) by malur.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1p6CL1-0006xQ-Lv for pgsql-hackers@lists.postgresql.org; Fri, 16 Dec 2022 15:09:27 +0000 Received: from mail-ej1-x634.google.com ([2a00:1450:4864:20::634]) by makus.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.92) (envelope-from ) id 1p6CKz-0005RR-Hv for pgsql-hackers@postgresql.org; Fri, 16 Dec 2022 15:09:26 +0000 Received: by mail-ej1-x634.google.com with SMTP id x22so6766314ejs.11 for ; Fri, 16 Dec 2022 07:09:25 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=to:content-language:subject:from:user-agent:mime-version:date :message-id:from:to:cc:subject:date:message-id:reply-to; bh=regFF/V5P71nmaTMttRW4QxI3oq9uasQvCoLupS9rmw=; b=m2gsI+qkjH/OYzaerJ+nGH40E4KIk23Ix93ocUt4S8/n4IQ1vZ3LIHbaEM0YWEQQ7A UmUd5YdqOzvVbBMD8UmAY2cmTszIQRTNLbfVB3UwHFAh9mBWqYZ+tbr+TSaneI4QrmUl 4JcIrtt6e2XTz8KnFghhemOOMDZMrOPqxOPB6LXGbw/8Xboom/SuwidFvnXPSYh4l2iU CoGyoqeOceio19KxJEVaswTfE6f4jwqQhpx7zdCwmTVk0s4PlvNKsQJTsYOs1B5d/Onl bbFGQXLLBhqoBp0W7oLPJUsSX9kacCD8fKye45yn0t/k6qYhBNTYyTZRt9zctRnZKZ+P 857g== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=to:content-language:subject:from:user-agent:mime-version:date :message-id:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=regFF/V5P71nmaTMttRW4QxI3oq9uasQvCoLupS9rmw=; b=5zSVLjK0zkq6LVUhc7tgUzuhi0+NgZV8a/uGCnpcqdXU5EZx5x38+nb7uTl9C4nzU6 rzU9wEqilv91phdOoo/RG+NjdwrKTFMpbZH17Mw6RogFm3jKJO/KoPY2ynu+fQF95FPH 0wECmfJfUoAnTi2/FZrf7PXaIPqGRXm32EbxAE/p7mGdcd5R5A2itXB9GKK8e7QMKYwe 9o70tEmhdPM9ZZSvNMdRccnThBFei7hCiTxppl+Bza9dbYrkmZ6i1AmH3psWlXZVUdlc WUuZk0hO1emkrtSdq40hcoX/p2B0ZfuHeBQq284NrybFKwp6lcxlGja/26NZRnRPOxDj OWjw== X-Gm-Message-State: ANoB5pkQqndBetS8xAlh2MlymOnWmwgPmsnSo3TLlpMtyOAc4ZnyqZiU yZ6e7hg73OVF7qGpvHHX30IwyJRL4IE= X-Google-Smtp-Source: AA0mqf4qXdLzIig0ABzBdAaOiHD19v/xD6iQot9yifwAJuPBFeSipyOMXQvBSLXwM7G6x10AdrrB6g== X-Received: by 2002:a17:906:234b:b0:7c0:b4c5:a11d with SMTP id m11-20020a170906234b00b007c0b4c5a11dmr28366087eja.75.1671203363747; Fri, 16 Dec 2022 07:09:23 -0800 (PST) Received: from ?IPV6:2003:d5:2f00:a200:d0f2:a42e:4142:ba5a? (p200300d52f00a200d0f2a42e4142ba5a.dip0.t-ipconnect.de. [2003:d5:2f00:a200:d0f2:a42e:4142:ba5a]) by smtp.gmail.com with ESMTPSA id la26-20020a170907781a00b0078d9cd0d2d6sm973315ejc.11.2022.12.16.07.09.22 for (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Fri, 16 Dec 2022 07:09:22 -0800 (PST) Content-Type: multipart/mixed; boundary="------------cxOe2nm0v00ovuTs91voMla0" Message-ID: Date: Fri, 16 Dec 2022 16:09:21 +0100 MIME-Version: 1.0 User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:102.0) Gecko/20100101 Thunderbird/102.6.0 From: Daniel Watzinger Subject: pg_dump/pg_restore: Fix stdin/stdout handling of custom format on Win32 Content-Language: en-US To: pgsql-hackers@postgresql.org 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. --------------cxOe2nm0v00ovuTs91voMla0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 7bit 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(-) --------------cxOe2nm0v00ovuTs91voMla0 Content-Type: application/x-patch; name="v1-0001-pg_dump-pg_restore-Fix-stdin-stdout-handling-of-c.patch" Content-Disposition: attachment; filename*0="v1-0001-pg_dump-pg_restore-Fix-stdin-stdout-handling-of-c.pa"; filename*1="tch" Content-Transfer-Encoding: base64 diffgita/src/bin/pgdump/pgbackuparchivercb/src/bin/pgdump/pgbackuparchiv ercindex7f7a0f1ce7684db4a17a100644a/src/bin/pgdump/pgbackuparchiverc+++b /src/bin/pgdump/pgbackuparchiverc38386+383818boolcheckSeekFILEfppgoffttp os+structstatst++/Checkifthisisaterminaldescriptor/+ifisattyfilenofp+ret urnfalse+++/Checkifthisisanunseekablecharacterspecialdeviceorpipe/+iffst atfilenofpstACEgh0bLbLZqHXvkiEhSBTrLbLZqHXvq3rbq532pbHvvwoXnJLYWrX7XpZaM KK5LKJ7YYrH4pXv7aaLAtellofpdiffgita/src/include/port/win32porthb/src/inc lude/port/win32porthindex296905bc6c892d0bb401100644a/src/include/port/wi n32porth+++b/src/include/port/win32porth3296+32912externintpglstat64cons tcharnamestructstatbufifndefSISREGdefineSISREGmmSIFMTCBURBnp3Yn/on53Xn0i EhSBTvnXn4p3kiEhSBTppkiBTEwgUgU756d2J/6J+d159IhIIdH515+Kd5IhIIdGaZIgUxMA FCHR+endif/Inorderforlstattobeabletoreportjunctionpointsassymlinkswediff gita/src/port/win32statcb/src/port/win32statcindexe6553e40307ab0983a7410 0644a/src/port/win32statc+++b/src/port/win32statc25635+25665pgstat64cons tcharnamestructstatbufintpgfstat64intfilenostructstatbufHANDLEhFilccA0Ms SB62ix+Fqd2V5+KV6egFgcA0MsQUgsQg0U5EwBMg41+INq1r4cA0MsSEWKV4ATQyxIHraLH4 Wp3ZXn4pXp6Pg1jkQ34pXk8qXgBSETYPEUNKNOWN+DWORDlastError+unsignedshortstm odeifhFilSDVQCyAxwDQyxFQC1BG7nxQsv6J+EWKVwCDALIDHANDLEVALUEhFileADQyxNm7 nwAst6uuehCDVQC63rbq59fwoXnJIn7YXn4pXp6IrGnWrWrLa3mpiH7KLp5XrLIrYWrG3np6 3nYq3nLXnbaGn4pXoHrbYp4Ip36K5mrYqJ7Ya6LoIYrbBwDQyxMIpZX2opbKHprpWrXorbLL WrYp36K5mrYqJ4p7YXpqLLWqaa6Ka4mrXsGsmp3a3rbq54rYp7LXmnfn4pXk8qXgetFileTy pehFile+lastErrorAG0tqy0Suuiv77/4ie+iR4Z60tqy0SuuiuKeit16u2h2Ky2KeC6KyFt 63B56dq9qWJ36t626ueh8UgsRNg8RQ0o05Y1qd2K2yt626ud2562hpxqWWKeB6uuiv4idxqx 6h+y5xx6ywZ60tqy0Suuiut626uew04RFE5H+J9+KV6efilenostdinfilenoIpXp6LLXaLr X4pXp6CKV6eiy116usZ60WKV4id+iuZq2KicHIdqd2V6EWKV5+INq1r6J9+KV5PKlwBSETYP EUNKNOWNlastErrorAMRFE5H52iyZql6uuVqy0Suuiv6t626ufX776zCK1yF+KV5PKl6Z6ax 61u5/SyLN6h9u59u5+y2ah14BBQh0W7n7LXXrwH4enobufstrdevAfh6ehu5+y2eWKeQDXrb q59Pv04XrKXnIn4nnX4pXorGnYrJH4pXv/nGrHhSCxE2DxAyEivq3rbq534pXop36LaLLWrY RYpXm7n/v/k4XrKXnIn4nnX4pXorGrKHJHrfmp2pnnaYqXqK2p2p6J8pqLrKYqXvv/nGrHhS CxE2DxDyDxPrLZqHXgEiIFO+break+/+Thespecifiedfileisacharacterfile+typical lyanLPTdeviceoraconsole+/+caseFILETYPECHAR+/+Unusedflagandunknownfiletyp e+/+caseFILETYPEREMOTE+caseFILETYPEUNKNOWN+stmodeAQUIdH5ut5qT5159q6W356u uegAQlQC/q3rbq59a3rbq534pXop36LaLLWrYRYpXm7n/pnprHrW7n9LIs3qH27n/m7n7LZq HXgLmode+bufstdevAeV6ej5u5+y2t168IpXp6Pm7n7LZ5Yp5ANfeturn0endif/WIN32/ba secommite52f8b301ed54aac5162b185b43f5f1e44b6b17e --------------cxOe2nm0v00ovuTs91voMla0--