git.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Eric Sunshine <sunshine@sunshineco.com>
To: Sun He <sunheehnus@gmail.com>
Cc: "Git List" <git@vger.kernel.org>,
	"Michael Haggerty" <mhagger@alum.mit.edu>,
	"Junio C Hamano" <gitster@pobox.com>, "Jeff King" <peff@peff.net>,
	"Torsten Bögershausen" <tboegi@web.de>
Subject: Re: [PATCH] Rewrite bulk-checkin.c:finish_bulk_checkin() to use a strbuf for handling packname
Date: Fri, 28 Feb 2014 04:46:55 -0500	[thread overview]
Message-ID: <CAPig+cSvkmZH2qEqKd=sjaMO8bfnxiKiuTKtfOuMCDwDfCDciw@mail.gmail.com> (raw)
In-Reply-To: <1393576104-1758-1-git-send-email-sunheehnus@gmail.com>

On Fri, Feb 28, 2014 at 3:28 AM, Sun He <sunheehnus@gmail.com> wrote:
> Signed-off-by: Sun He <sunheehnus@gmail.com>
> ---

Nicely done.

Due to the necessary changes to finish_tmp_packfile(), the focus of
this patch has changed slightly from that suggested by the
microproject. It's really now about finish_tmp_packfile() rather than
finish_bulk_checkin(). As such, it may make sense to adjust the patch
subject to reflect this. For instance:

  Subject: finish_tmp_packfile(): use strbuf for pathname construction

More comments below.

]> diff --git a/builtin/pack-objects.c b/builtin/pack-objects.c
> index c733379..72fb82b 100644
> --- a/builtin/pack-objects.c
> +++ b/builtin/pack-objects.c
> @@ -803,7 +803,7 @@ static void write_pack_file(void)
>
>                 if (!pack_to_stdout) {
>                         struct stat st;
> -                       char tmpname[PATH_MAX];
> +                       struct strbuf tmpname = STRBUF_INIT;
>
>                         /*
>                          * Packs are runtime accessed in their mtime
> @@ -823,26 +823,22 @@ static void write_pack_file(void)
>                                 utb.modtime = --last_mtime;
>                                 if (utime(pack_tmp_name, &utb) < 0)
>                                         warning("failed utime() on %s: %s",
> -                                               tmpname, strerror(errno));
> +                                               pack_tmp_name, strerror(errno));

Good catch finding this bug (as your commentary below states).
Ideally, each conceptual change should be presented distinctly from
others, so this bug should have its own patch (even though it's just a
one-liner).

>                         }
>
> -                       /* Enough space for "-<sha-1>.pack"? */
> -                       if (sizeof(tmpname) <= strlen(base_name) + 50)
> -                               die("pack base name '%s' too long", base_name);
> -                       snprintf(tmpname, sizeof(tmpname), "%s-", base_name);
> +                       strbuf_addf(&tmpname, "%s-", base_name);
>
>                         if (write_bitmap_index) {
>                                 bitmap_writer_set_checksum(sha1);
>                                 bitmap_writer_build_type_index(written_list, nr_written);
>                         }
>
> -                       finish_tmp_packfile(tmpname, pack_tmp_name,
> +                       finish_tmp_packfile(&tmpname, pack_tmp_name,
>                                             written_list, nr_written,
>                                             &pack_idx_opts, sha1);
>
>                         if (write_bitmap_index) {
> -                               char *end_of_name_prefix = strrchr(tmpname, 0);
> -                               sprintf(end_of_name_prefix, "%s.bitmap", sha1_to_hex(sha1));
> +                               strbuf_addf(&tmpname, "%s.bitmap" ,sha1_to_hex(sha1));
>
>                                 stop_progress(&progress_state);
>
> diff --git a/pack-write.c b/pack-write.c
> index 9b8308b..2204aa9 100644
> --- a/pack-write.c
> +++ b/pack-write.c
> @@ -336,7 +336,7 @@ struct sha1file *create_tmp_packfile(char **pack_tmp_name)
>         return sha1fd(fd, *pack_tmp_name);
>  }
>
> -void finish_tmp_packfile(char *name_buffer,
> +void finish_tmp_packfile(struct strbuf *name_buffer,
>                          const char *pack_tmp_name,
>                          struct pack_idx_entry **written_list,
>                          uint32_t nr_written,
> @@ -344,7 +344,7 @@ void finish_tmp_packfile(char *name_buffer,
>                          unsigned char sha1[])
>  {
>         const char *idx_tmp_name;
> -       char *end_of_name_prefix = strrchr(name_buffer, 0);
> +       int pre_len = name_buffer->len;

Minor: Perhaps basename_len (or some such) would convey a bit more
meaning than pre_len.

>         if (adjust_shared_perm(pack_tmp_name))
>                 die_errno("unable to make temporary pack file readable");
> @@ -354,17 +354,21 @@ void finish_tmp_packfile(char *name_buffer,
>         if (adjust_shared_perm(idx_tmp_name))
>                 die_errno("unable to make temporary index file readable");
>
> -       sprintf(end_of_name_prefix, "%s.pack", sha1_to_hex(sha1));
> -       free_pack_by_name(name_buffer);
> +       strbuf_addf(name_buffer, "%s.pack", sha1_to_hex(sha1));
> +       free_pack_by_name(name_buffer->buf);
>
> -       if (rename(pack_tmp_name, name_buffer))
> +       if (rename(pack_tmp_name, name_buffer->buf))
>                 die_errno("unable to rename temporary pack file");
>
> -       sprintf(end_of_name_prefix, "%s.idx", sha1_to_hex(sha1));
> -       if (rename(idx_tmp_name, name_buffer))
> +       /* remove added suffix string(sha1.pack) */
> +       strbuf_remove(name_buffer, pre_len, name_buffer->len - pre_len);

Since you are merely truncating the buffer back to pre_len, perhaps

    strbuf_setlen(name_buffer, pre_len)

would be more idiomatic and easier to read (and would allow you to
drop the comment).

> +
> +       strbuf_addf(name_buffer, "%s.idx", sha1_to_hex(sha1));
> +       if (rename(idx_tmp_name, name_buffer->buf))
>                 die_errno("unable to rename temporary index file");
>
> -       *end_of_name_prefix = '\0';
> +       /* remove added suffix string(sha1.idx) */
> +       strbuf_remove(name_buffer, pre_len, name_buffer->len - pre_len);

Ditto.

>         free((void *)idx_tmp_name);
>  }
> --
> 1.9.0.138.g2de3478.dirty
>
> Hello,
> This is my patch for the GSoC microproject #2
>
> I follow the suggestion of Junio to use the strbuf_addf to deal with this thing.
> And the usage of strbuf_addf needs to change the function finish_tmp_packfile, I search all over the source code ($ find .| xargs grep "finish_tmp_packfile")
> The following files contains finish_tmp_packfile:
>         bulk-checkin.c
>         builtin/pack-object.c
>         pack-write.c
>         pack.h
> And I do some change to fix them.
> I changed my vimrc so that tabs will not be changed into spaces automatically.
>
> And I came across a bug when I was doing my microproject.
> position:  builtin/pack-objects.c: write_pack_file: if(!pack_to_stdout): first else in it
>  warning() function used an uninitialized array, and I changed it to pack_tmp_name.
>
> Thank you all for all your suggestions.

This section explaining your changes is important for the ongoing
email discussion, however, it is customary (and "git am"-friendly) to
place these notes just below the "---" line following your signoff
near the top of the email.

  reply	other threads:[~2014-02-28  9:47 UTC|newest]

Thread overview: 10+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2014-02-28  8:28 [PATCH] Rewrite bulk-checkin.c:finish_bulk_checkin() to use a strbuf for handling packname Sun He
2014-02-28  9:46 ` Eric Sunshine [this message]
2014-02-28 13:10   ` Eric Sunshine
2014-02-28 14:17     ` 孙赫
2014-02-28 20:42       ` Eric Sunshine
2014-03-01  0:13         ` He Sun
     [not found]   ` <CAJr59C2Uw+tnRSuHbMnyJjGSE+XNpepPdode5MvcHb4X31e+qQ@mail.gmail.com>
2014-02-28 15:54     ` Fwd: " 孙赫
2014-02-28 20:47       ` Eric Sunshine
  -- strict thread matches above, loose matches on Subject: below --
2014-02-27 14:00 Sun He
2014-02-27 19:39 ` Junio C Hamano

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='CAPig+cSvkmZH2qEqKd=sjaMO8bfnxiKiuTKtfOuMCDwDfCDciw@mail.gmail.com' \
    --to=sunshine@sunshineco.com \
    --cc=git@vger.kernel.org \
    --cc=gitster@pobox.com \
    --cc=mhagger@alum.mit.edu \
    --cc=peff@peff.net \
    --cc=sunheehnus@gmail.com \
    --cc=tboegi@web.de \
    /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).