Git development
 help / color / mirror / Atom feed
From: Junio C Hamano <gitster@pobox.com>
To: "René Scharfe" <l.s.r@web.de>
Cc: David Turner <dturner@twosigma.com>,
	git@vger.kernel.org, peff@peff.net, jrnieder@gmail.com,
	Duy Nguyen <pclouds@gmail.com>
Subject: Re: [PATCH v2] xgethostname: handle long hostnames
Date: Tue, 18 Apr 2017 18:49:09 -0700	[thread overview]
Message-ID: <xmqqpog9cgkq.fsf@gitster.mtv.corp.google.com> (raw)
In-Reply-To: <281d0843-d48a-b7ab-737b-b9528689d44e@web.de> ("René Scharfe"'s message of "Tue, 18 Apr 2017 18:07:43 +0200")

René Scharfe <l.s.r@web.de> writes:

> How important is it to scan the whole file in one call?  We could split
> it up like this and use a strbuf to handle host names of any length.  We
> need to be permissive here to allow machines with different values for
> HOST_NAME_MAX to work with the same file on a network file system, so
> this would have to be the first patch, right?

Absolutely.  FWIW, I agree with Peff that we do not need to use
fscanf here; just reading a line into strbuf and picking pieces
would be sufficient.

> NB: That && cascade has enough meat for a whole function.

True, too.

>
> René
>
> ---
>  builtin/gc.c | 10 +++++-----
>  1 file changed, 5 insertions(+), 5 deletions(-)
>
> diff --git a/builtin/gc.c b/builtin/gc.c
> index 1fca84c19d..d5e880028e 100644
> --- a/builtin/gc.c
> +++ b/builtin/gc.c
> @@ -251,10 +251,9 @@ static const char *lock_repo_for_gc(int force, pid_t* ret_pid)
>  	fd = hold_lock_file_for_update(&lock, pidfile_path,
>  				       LOCK_DIE_ON_ERROR);
>  	if (!force) {
> -		static char locking_host[128];
> +		static struct strbuf locking_host = STRBUF_INIT;
>  		int should_exit;
>  		fp = fopen(pidfile_path, "r");
> -		memset(locking_host, 0, sizeof(locking_host));
>  		should_exit =
>  			fp != NULL &&
>  			!fstat(fileno(fp), &st) &&
> @@ -268,9 +267,10 @@ static const char *lock_repo_for_gc(int force, pid_t* ret_pid)
>  			 * running.
>  			 */
>  			time(NULL) - st.st_mtime <= 12 * 3600 &&
> -			fscanf(fp, "%"SCNuMAX" %127c", &pid, locking_host) == 2 &&
> +			fscanf(fp, "%"SCNuMAX" ", &pid) == 1 &&
> +			!strbuf_getwholeline(&locking_host, fp, '\0') &&
>  			/* be gentle to concurrent "gc" on remote hosts */
> -			(strcmp(locking_host, my_host) || !kill(pid, 0) || errno == EPERM);
> +			(strcmp(locking_host.buf, my_host) || !kill(pid, 0) || errno == EPERM);
>  		if (fp != NULL)
>  			fclose(fp);
>  		if (should_exit) {
> @@ -278,7 +278,7 @@ static const char *lock_repo_for_gc(int force, pid_t* ret_pid)
>  				rollback_lock_file(&lock);
>  			*ret_pid = pid;
>  			free(pidfile_path);
> -			return locking_host;
> +			return locking_host.buf;
>  		}
>  	}

      parent reply	other threads:[~2017-04-19  1:49 UTC|newest]

Thread overview: 9+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2017-04-17 16:17 [PATCH v2] xgethostname: handle long hostnames David Turner
2017-04-18  1:19 ` Junio C Hamano
2017-04-18  1:41   ` Junio C Hamano
2017-04-18 16:07     ` René Scharfe
2017-04-18 16:17       ` Jeff King
2017-04-19  1:47         ` Junio C Hamano
2017-04-18 17:52       ` David Turner
2017-04-19  3:49         ` Junio C Hamano
2017-04-19  1:49       ` Junio C Hamano [this message]

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=xmqqpog9cgkq.fsf@gitster.mtv.corp.google.com \
    --to=gitster@pobox.com \
    --cc=dturner@twosigma.com \
    --cc=git@vger.kernel.org \
    --cc=jrnieder@gmail.com \
    --cc=l.s.r@web.de \
    --cc=pclouds@gmail.com \
    --cc=peff@peff.net \
    /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