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 1mnheY-0003XQ-Md for pgsql-bugs@arkaria.postgresql.org; Thu, 18 Nov 2021 13:40:39 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.92) (envelope-from ) id 1mnheW-0008Br-OM for pgsql-bugs@arkaria.postgresql.org; Thu, 18 Nov 2021 13:40:36 +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 1mnhOQ-0007Tl-6m for pgsql-bugs@lists.postgresql.org; Thu, 18 Nov 2021 13:23:58 +0000 Received: from mail.postgrespro.ru ([93.174.131.139]) by magus.postgresql.org with esmtp (Exim 4.92) (envelope-from ) id 1mnhOO-0003Xj-14 for pgsql-bugs@lists.postgresql.org; Thu, 18 Nov 2021 13:23:57 +0000 Received: from [192.168.28.32] (cyclops.postgrespro.ru [93.174.131.138]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (Client did not present a certificate) by mail.postgrespro.ru (Postfix) with ESMTPSA id 6EDD121C068E; Thu, 18 Nov 2021 16:23:54 +0300 (MSK) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=postgrespro.ru; s=mail; t=1637241834; bh=YLu6VfJrZ7YncpLYDQ/6r+kZmt3TEQT1rJ5BA7VOFac=; h=Date:Subject:To:Cc:References:From:In-Reply-To; b=PbTO/E/fPnh2XIomUNsOUPe7dlsxiCsreH5723DYjCML7Kt9qTrkFXpgb/jElr42C 3dwuFNkx3clBBNjysFBqFQEVer4yGsDldFDUmzIpZLvsCZe/oNcbfDhmaJSOeYLEqd plshbKhdchsBIMO1LsREWYcKYKXe5d/BFnwENptY= Content-Type: multipart/mixed; boundary="------------zFVin29y6uzZ213OcgD0pJJI" Message-ID: <9e4efe29-0ff9-0808-fe32-d1d50e0be4fa@postgrespro.ru> Date: Thu, 18 Nov 2021 16:23:54 +0300 MIME-Version: 1.0 User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:91.0) Gecko/20100101 Thunderbird/91.3.0 Subject: Re: BUG #17288: PSQL bug with COPY command (Windows) Content-Language: ru To: Michael Paquier Cc: pgsql-bugs@lists.postgresql.org References: <17288-6b58a91025a8a8a3@postgresql.org> From: Dmitry Koval In-Reply-To: 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. --------------zFVin29y6uzZ213OcgD0pJJI Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 7bit >_fstat64() and _stat64() have proved to be tricky to work with and >rather unworkable across all the build systems we support I agree, it is better not use them. >I think that we had better never call GetFileInformationByHandle() if >we use a fileno that maps to stdin, stdout or stderr. Probably we should call GetFileInformationByHandle() for case standard streams stdin/stdout/stderr are redirected to files. See file src\backend\utils\error\elog.c, for example. It contains lines: if (!freopen(OutputFileName, "a", stderr)) if (!freopen(OutputFileName, "a", stdout)) And this command with "stderr" works in PSQL without crash (in contrast to "stdout"): \copy (SELECT 1) TO stderr (it put resullt into file with name "stderr"). We can emulate stats for stdin/stdout/stderr after call GetFileInformationByHandle(). >Just to be sure, this is the code path in psql's copy.c where we check >that a specified copystream is not a directory, right? Yes, fstat() called from file src/bin/psql/copy.c: /* make sure the specified file is not a directory */ if ((result = fstat(fileno(copystream), &st)) < 0) I attached new patch version. With best regards, Dmitry Koval. --------------zFVin29y6uzZ213OcgD0pJJI Content-Type: text/plain; charset=UTF-8; name="v3_0001-Fixed-Windows-stat-emulation-working-with-streams.patch" Content-Disposition: attachment; filename*0="v3_0001-Fixed-Windows-stat-emulation-working-with-streams.pa"; filename*1="tch" Content-Transfer-Encoding: base64 RnJvbSA0YWQ4MmE4ZjdiMTE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