pg.ddx.io  pgsql-bugs@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Dmitry Koval <d.koval@postgrespro.ru>
To: Michael Paquier <michael@paquier.xyz>
To: Juan José Santamaría Flecha <juanjo.santamaria@gmail.com>
Cc: PostgreSQL mailing lists <pgsql-bugs@lists.postgresql.org>
Subject: Re: BUG #17288: PSQL bug with COPY command (Windows)
Date: Sat, 20 Nov 2021 14:51:21 +0300
Message-ID: <ff540eb5-3493-cf02-6c22-20ef6e15fee9@postgrespro.ru> (raw)
In-Reply-To: <YZiGIxk5F3TT/OJc@paquier.xyz>
References: <b95273fb-a51a-d0a9-e4a8-501e32e204e5@postgrespro.ru>
	<YZYDZwDtw/Ioojb4@paquier.xyz>
	<9e4efe29-0ff9-0808-fe32-d1d50e0be4fa@postgrespro.ru>
	<CAC+AXB3BKByL8O39qARu3XYMWmrNqMWhcHZZ_2ygqvrK-W6O1g@mail.gmail.com>
	<fe1cde9e-1e52-4f24-3ff7-d01f28a63790@postgrespro.ru>
	<YZdK6DjbtCT7rz9S@paquier.xyz>
	<4c22d38f-efa9-4d83-c551-707f1770e585@postgrespro.ru>
	<CAC+AXB1C1C_z3XQR=ZxiG+V0U9RDJFtJJamvYMoq+d4RoVRh4A@mail.gmail.com>
	<9caa0612-19e5-f860-4b97-908a1c721233@postgrespro.ru>
	<CAC+AXB1FTZueNdvT1F4fpr4z9gxpcKkgWa-Awor9XC9PAKLVSA@mail.gmail.com>
	<YZiGIxk5F3TT/OJc@paquier.xyz>

 >... We should really
 >add a comment explaining why this is handled this way though for the
 >case of streams, why the case of the redirection matters, and why we
 >count on a failure of GetFinalPathNameByHandleA().

Added a comment for v4 (as draft).
But I don't know English well enough and probably comment should be
corrected.


With best regards,
Dmitry.

Attachments:

v5_0001-Fixed-Windows-stat-emulation-working-with-streams.patch (text/plain, 1.5K) download diff --git a/src/port/win32stat.c b/src/port/win32stat.c index 2ad8ee1359..8b97959223 100644 --- a/src/port/win32stat.c +++ b/src/port/win32stat.c @@ -289,6 +289,7 @@ int _pgfstat64(int fileno, struct stat *buf) { HANDLE hFile = (HANDLE) _get_osfhandle(fileno); + char path[MAX_PATH]; if (hFile == INVALID_HANDLE_VALUE || buf == NULL) { @@ -301,6 +302,34 @@ _pgfstat64(int fileno, struct stat *buf) * ERROR_DELETE_PENDING. */ + /*-------------------- + * Function GetFileInformationByHandle() can not work with + * stdin/stdout/stderr. Need to emulate call _fstat64() for them. + * Two important points: + * 1) we should compare "fileno" with result of _fileno() function + * (not with 0,1,2). It is need if standard stream was closed. Function + * _fileno() returns -1 in this case; + * 2) we call GetFinalPathNameByHandleA() for case "fileno" was reopened + * and associated to file - after call + * "freopen(OutputFileName, "a", stdout)" for example. + * (After freopen() function GetFinalPathNameByHandleA() returns length + * of "path" > 0 and we should process "stdout" handle as file, without + * emulation). + *-------------------- + */ + if ((fileno == _fileno(stdin) || + fileno == _fileno(stdout) || + fileno == _fileno(stderr)) && + !GetFinalPathNameByHandleA(hFile, path, MAX_PATH, VOLUME_NAME_NT)) + { + memset(buf, 0, sizeof(*buf)); + buf->st_mode = _S_IFCHR; + buf->st_dev = fileno; + buf->st_rdev = fileno; + buf->st_nlink = 1; + return 0; + } + return fileinfo_to_stat(hFile, buf); }

view thread (29+ messages)  latest in thread

Message-ID: <ff540eb5-3493-cf02-6c22-20ef6e15fee9@postgrespro.ru>
Permalink:  ../ff540eb5-3493-cf02-6c22-20ef6e15fee9@postgrespro.ru/
Also on:    postgresql.org/message-id/ff540eb5-3493-cf02-6c22-20ef6e15fee9@postgrespro.ru

 ·  · 

reply

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-bugs@postgresql.org
  Cc: d.koval@postgrespro.ru, michael@paquier.xyz, juanjo.santamaria@gmail.com, pgsql-bugs@lists.postgresql.org
  Subject: Re: BUG #17288: PSQL bug with COPY command (Windows)
  In-Reply-To: <ff540eb5-3493-cf02-6c22-20ef6e15fee9@postgrespro.ru>

* 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