git.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Johannes Schindelin <Johannes.Schindelin@gmx.de>
To: Johannes Sixt <j6t@kdbg.org>
Cc: git@vger.kernel.org, Jeff Hostetler <jeffhost@microsoft.com>,
	Junio C Hamano <gitster@pobox.com>,
	Pranit Bauva <pranit.bauva@gmail.com>,
	Beat Bolli <dev+git@drbeat.li>
Subject: Re: [PATCH v2 3/3] mingw: replace isatty() hack
Date: Thu, 22 Dec 2016 22:37:20 +0100 (CET)	[thread overview]
Message-ID: <alpine.DEB.2.20.1612222235200.155951@virtualbox> (raw)
In-Reply-To: <e9f8a015-4106-fa88-082a-9e8c06ff61f3@kdbg.org>

Hi Hannes,

On Thu, 22 Dec 2016, Johannes Sixt wrote:

> Am 22.12.2016 um 18:09 schrieb Johannes Schindelin:
> > +static HANDLE swap_osfhnd(int fd, HANDLE new_handle)
> > +{
> > +	/*
> > +	 * Create a copy of the original handle associated with fd
> > +	 * because the original will get closed when we dup2().
> > +	 */
> > +	HANDLE handle = (HANDLE)_get_osfhandle(fd);
> > +	HANDLE duplicate = duplicate_handle(handle);
> >
> > +	/* Create a temp fd associated with the already open "new_handle". */
> > +	int new_fd = _open_osfhandle((intptr_t)new_handle, O_BINARY);
> >
> > +	assert((fd == 1) || (fd == 2));
> >
> > +	/*
> > +	 * Use stock dup2() to re-bind fd to the new handle.  Note that
> > +	 * this will implicitly close(1) and close both fd=1 and the
> > +	 * originally associated handle.  It will open a new fd=1 and
> > +	 * call DuplicateHandle() on the handle associated with new_fd.
> > +	 * It is because of this implicit close() that we created the
> > +	 * copy of the original.
> > +	 *
> > +	 * Note that the OS can recycle HANDLE (numbers) just like it
> > +	 * recycles fd (numbers), so we must update the cached value
> > +	 * of "console".  You can use GetFileType() to see that
> > +	 * handle and _get_osfhandle(fd) may have the same number
> > +	 * value, but they refer to different actual files now.
> 
> Certainly, the OS does not recycle handle values that are in use (open). Then
> I do not quite get the point of this paragraph. See...
> 
> > +	 *
> > +	 * Note that dup2() when given target := {0,1,2} will also
> > +	 * call SetStdHandle(), so we don't need to worry about that.
> > +	 */
> > +	dup2(new_fd, fd);
> > +	if (console == handle)
> > +		console = duplicate;
> 
> ... This is where "the cached value of console is updated", right? If console
> == handle, then this is not because a handle value was recycled, but because
> fd *was* console. Since the old value of console has been closed by the
> dup2(), we must refer to the back-up value in the future. Am I missing
> something?

You are correct, we must update `console` because `handle` is no longer
the handle we want.

The comment above only meant to reinforce that we have to forget about the
previous handle, too, as we might access something completely different
than a console otherwise.

Would you have a suggestion how to rephrase the comment to make it less
confusing?

Ciao,
Dscho

  reply	other threads:[~2016-12-22 21:37 UTC|newest]

Thread overview: 29+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2016-12-21 17:53 [PATCH 0/2] Really fix the isatty() problem on Windows Johannes Schindelin
2016-12-21 17:53 ` [PATCH 1/2] mingw: adjust is_console() to work with stdin Johannes Schindelin
2016-12-21 17:53 ` [PATCH 2/2] mingw: replace isatty() hack Johannes Schindelin
2016-12-21 18:45   ` Junio C Hamano
2016-12-21 21:15 ` [PATCH 0/2] Really fix the isatty() problem on Windows Johannes Sixt
2016-12-21 21:21   ` Junio C Hamano
2016-12-22 18:48   ` Johannes Sixt
2016-12-22 21:33     ` Johannes Schindelin
2016-12-22 17:08 ` [PATCH v2 0/3] " Johannes Schindelin
2016-12-22 17:08   ` [PATCH v2 1/3] mingw: adjust is_console() to work with stdin Johannes Schindelin
2016-12-22 23:04     ` Beat Bolli
2016-12-22 23:18       ` Junio C Hamano
2016-12-23  9:30       ` Johannes Schindelin
2016-12-23 12:51         ` Beat Bolli
2016-12-22 17:09   ` [PATCH v2 2/3] mingw: fix colourization on Cygwin pseudo terminals Johannes Schindelin
2016-12-22 17:09   ` [PATCH v2 3/3] mingw: replace isatty() hack Johannes Schindelin
2016-12-22 20:26     ` Johannes Sixt
2016-12-22 21:37       ` Johannes Schindelin [this message]
2016-12-22 22:28         ` Johannes Sixt
2016-12-22 23:18           ` Johannes Schindelin
2016-12-22 17:49   ` [PATCH v2 0/3] Really fix the isatty() problem on Windows Junio C Hamano
2016-12-22 17:59     ` Johannes Schindelin
2016-12-22 18:32       ` Junio C Hamano
2016-12-22 18:19     ` Junio C Hamano
2016-12-22 23:16   ` [PATCH v3 " Johannes Schindelin
2016-12-22 23:16     ` [PATCH v3 1/3] mingw: adjust is_console() to work with stdin Johannes Schindelin
2016-12-22 23:16     ` [PATCH v3 2/3] mingw: fix colourization on Cygwin pseudo terminals Johannes Schindelin
2016-12-22 23:16     ` [PATCH v3 3/3] mingw: replace isatty() hack Johannes Schindelin
2017-01-18 12:13       ` Johannes Schindelin

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=alpine.DEB.2.20.1612222235200.155951@virtualbox \
    --to=johannes.schindelin@gmx.de \
    --cc=dev+git@drbeat.li \
    --cc=git@vger.kernel.org \
    --cc=gitster@pobox.com \
    --cc=j6t@kdbg.org \
    --cc=jeffhost@microsoft.com \
    --cc=pranit.bauva@gmail.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).