Skip to contentSkip to conversation

Rewrite bulk-checkin.c:finish_bulk_checkin() to use a strbuf for handling packname

PATCH In review Sun He 2 participants 8 messages Jun 15, 2016 1 message not in the archive

lore
Share & tools
lore

Sun He

author
#0 lore raw

PATCH 1/1 Rewrite bulk-checkin.c:finish_bulk_checkin() to use a strbuf for handling packname

---
 builtin/pack-objects.c | 17 +++++++----------
 bulk-checkin.c         |  8 +++++---
 pack-write.c           | 20 ++++++++++++--------
 pack.h                 |  2 +-
 4 files changed, 25 insertions(+), 22 deletions(-)
builtin/pack-objects.c +7 -10
index c733379..72fb82b 100644
@@ -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));
 			}
 
-			/* 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);
 
@@ -851,10 +847,11 @@ static void write_pack_file(void)
 				bitmap_writer_select_commits(indexed_commits, indexed_commits_nr, -1);
 				bitmap_writer_build(&to_pack);
 				bitmap_writer_finish(written_list, nr_written,
-						     tmpname, write_bitmap_options);
+						     tmpname.buf, write_bitmap_options);
 				write_bitmap_index = 0;
 			}
 
+			strbuf_release(&tmpname);
 			free(pack_tmp_name);
 			puts(sha1_to_hex(sha1));
 		}
bulk-checkin.c +5 -3
index 118c625..98e651c 100644
@@ -4,6 +4,7 @@
 #include "bulk-checkin.h"
 #include "csum-file.h"
 #include "pack.h"
+#include "strbuf.h"
 
 static int pack_compression_level = Z_DEFAULT_COMPRESSION;
 
@@ -23,7 +24,7 @@ static struct bulk_checkin_state {
 static void finish_bulk_checkin(struct bulk_checkin_state *state)
 {
 	unsigned char sha1[20];
-	char packname[PATH_MAX];
+	struct strbuf packname = STRBUF_INIT;
 	int i;
 
 	if (!state->f)
@@ -43,8 +44,8 @@ static void finish_bulk_checkin(struct bulk_checkin_state *state)
 		close(fd);
 	}
 
-	sprintf(packname, "%s/pack/pack-", get_object_directory());
-	finish_tmp_packfile(packname, state->pack_tmp_name,
+	strbuf_addf(&packname, "%s/pack/pack-", get_object_directory());
+	finish_tmp_packfile(&packname, state->pack_tmp_name,
 			    state->written, state->nr_written,
 			    &state->pack_idx_opts, sha1);
 	for (i = 0; i < state->nr_written; i++)
@@ -54,6 +55,7 @@ clear_exit:
 	free(state->written);
 	memset(state, 0, sizeof(*state));
 
+	strbuf_release(&packname);
 	/* Make objects we just wrote available to ourselves */
 	reprepare_packed_git();
 }
pack-write.c +12 -8
index 9b8308b..2204aa9 100644
@@ -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;
 
 	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);
+
+	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);
 
 	free((void *)idx_tmp_name);
 }
pack.h +1 -1
index 12d9516..3223f5a 100644
@@ -91,6 +91,6 @@ extern int encode_in_pack_object_header(enum object_type, uintmax_t, unsigned ch
 extern int read_pack_header(int fd, struct pack_header *);
 
 extern struct sha1file *create_tmp_packfile(char **pack_tmp_name);
-extern void finish_tmp_packfile(char *name_buffer, const char *pack_tmp_name, struct pack_idx_entry **written_list, uint32_t nr_written, struct pack_idx_option *pack_idx_opts, unsigned char sha1[]);
+extern void finish_tmp_packfile(struct strbuf *name_buffer, const char *pack_tmp_name, struct pack_idx_entry **written_list, uint32_t nr_written, struct pack_idx_option *pack_idx_opts, unsigned char sha1[]);
 
 #endif
Signature · 23 lines
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.

Cheers,
He Sun
#1 lore raw
Quoted 2 lines · Sun He

---

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

Quoted 13 lines

index c733379..72fb82b 100644

builtin/pack-objects.c +1 -1
@@ -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

builtin/pack-objects.c static void write_pack_file(void) · line 826

Quoted 5 lines
builtin/pack-objects.c +1 -1
@@ -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).

Quoted 39 lines
builtin/pack-objects.c +3 -7
                        }
 
-                       /* 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);
 
pack-write.c +1 -1
index 9b8308b..2204aa9 100644
@@ -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,

pack-write.c void finish_tmp_packfile(char *name_buffer, · line 347

Quoted 5 lines
pack-write.c +1 -1
@@ -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.

Quoted 9 lines
pack-write.c +1 -2
        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));

pack-write.c void finish_tmp_packfile(char *name_buffer, · line 364

Quoted 10 lines
pack-write.c +4 -3
+       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).

pack-write.c void finish_tmp_packfile(char *name_buffer, · line 371

Quoted 8 lines
pack-write.c +5 -1
+
+       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.

Quoted 18 lines
pack-write.c +0 -1
        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.

Quoted 5 lines

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.

#2 lore raw
Quoted 6 lines · Eric Sunshine
Quoted 2 lines · Sun He

---

Nicely done.

Quoted 7 lines

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

You may also want your commit message to explain why you chose this approach over other legitimate approaches. For instance, your change places extra burden on callers of finish_tmp_packfile() by leaking a detail of its implementation: namely that it wants to manipulate pathnames easily (via strbuf). An equally valid and more encapsulated approach would be for finish_tmp_packfile() to accept a 'const char *' and construct its own strbuf internally.

If the extra strbuf creation in the alternate approach is measurably slower, then you could use that fact to justify your implementation choice in the commit message. On the other hand, if it's not measurably slower, then perhaps the more encapsulated approach with cleaner API might be preferable.

Quoted 141 lines

More comments below.

]> diff --git a/builtin/pack-objects.c b/builtin/pack-objects.c

Quoted 13 lines

index c733379..72fb82b 100644

builtin/pack-objects.c +1 -1
@@ -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

builtin/pack-objects.c static void write_pack_file(void) · line 826

Quoted 5 lines
builtin/pack-objects.c +1 -1
@@ -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).

Quoted 39 lines
builtin/pack-objects.c +3 -7
                        }
 
-                       /* 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);
 
pack-write.c +1 -1
index 9b8308b..2204aa9 100644
@@ -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,

pack-write.c void finish_tmp_packfile(char *name_buffer, · line 347

Quoted 5 lines
pack-write.c +1 -1
@@ -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.

Quoted 9 lines
pack-write.c +1 -2
        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));

pack-write.c void finish_tmp_packfile(char *name_buffer, · line 364

Quoted 10 lines
pack-write.c +4 -3
+       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).

pack-write.c void finish_tmp_packfile(char *name_buffer, · line 371

Quoted 8 lines
pack-write.c +5 -1
+
+       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.

Quoted 18 lines
pack-write.c +0 -1
        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.

Quoted 5 lines

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.

author
#3 lore raw

2014-02-28 21:12 GMT+08:00 Eric Sunshine [via git] <[email protected]>:

Quoted 24 lines · Eric Sunshine
Quoted 7 lines · Sun He
Quoted 2 lines
Signed-off-by Sun He <[hidden email]>

---

Nicely done.

Quoted 7 lines

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

You may also want your commit message to explain why you chose this approach over other legitimate approaches. For instance, your change places extra burden on callers of finish_tmp_packfile() by leaking a detail of its implementation: namely that it wants to manipulate pathnames easily (via strbuf). An equally valid and more encapsulated approach would be for finish_tmp_packfile() to accept a 'const char *' and construct its own strbuf internally.

Quoted 6 lines

If the extra strbuf creation in the alternate approach is measurably slower, then you could use that fact to justify your implementation choice in the commit message. On the other hand, if it's not measurably slower, then perhaps the more encapsulated approach with cleaner API might be preferable.

Thank you for your explaination. I get the point.
And I think if it is proved that the strbuf way is measurably slower. We should add a check for the length of string before we sprintf().

Quoted 164 lines
Quoted 152 lines

More comments below.

]> diff --git a/builtin/pack-objects.c b/builtin/pack-objects.c

Quoted 13 lines

index c733379..72fb82b 100644

builtin/pack-objects.c +1 -1
@@ -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

builtin/pack-objects.c static void write_pack_file(void) · line 829

Quoted 8 lines
builtin/pack-objects.c +1 -1
@@ -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).

Quoted 46 lines
builtin/pack-objects.c +2 -6
                        }
 
-                       /* 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);
pack-write.c +1 -1
index 9b8308b..2204aa9 100644
@@ -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,

pack-write.c void finish_tmp_packfile(char *name_buffer, · line 347

Quoted 5 lines
pack-write.c +1 -1
@@ -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.

Quoted 10 lines
pack-write.c +1 -2
        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));

pack-write.c void finish_tmp_packfile(char *name_buffer, · line 365

Quoted 10 lines
pack-write.c +4 -3
+       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).

pack-write.c void finish_tmp_packfile(char *name_buffer, · line 371

Quoted 8 lines
pack-write.c +5 -1
+
+       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.

Quoted 28 lines
pack-write.c +0 -1
        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.

Quoted 6 lines
Quoted 1 line

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.

--
To unsubscribe from this list: send the line "unsubscribe git" in the body of a message to [hidden email]
More majordomo info at http://vger.kernel.org/majordomo-info.html

Quoted 8 lines

________________________________
If you reply to this email, your message will be added to the discussion below:
http://git.661346.n2.nabble.com/PATCH-Rewrite-bulk-checkin-c-finish-bulk-checkin-to-use-a-strbuf-for-handling-packname-tp7604449p7604500.html
To start a new topic under git, email [email protected]
To unsubscribe from git, click here.
NAML

Cheers,
He Sun

#4 lore raw
Quoted 33 lines · 孙赫

2014-02-28 21:12 GMT+08:00 Eric Sunshine [via git] <[email protected]>:

Quoted 24 lines · Eric Sunshine
Quoted 7 lines · Sun He
Quoted 2 lines
Signed-off-by Sun He <[hidden email]>

---

Nicely done.

Quoted 7 lines

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

You may also want your commit message to explain why you chose this approach over other legitimate approaches. For instance, your change places extra burden on callers of finish_tmp_packfile() by leaking a detail of its implementation: namely that it wants to manipulate pathnames easily (via strbuf). An equally valid and more encapsulated approach would be for finish_tmp_packfile() to accept a 'const char *' and construct its own strbuf internally.

Quoted 6 lines

If the extra strbuf creation in the alternate approach is measurably slower, then you could use that fact to justify your implementation choice in the commit message. On the other hand, if it's not measurably slower, then perhaps the more encapsulated approach with cleaner API might be preferable.

Quoted 3 lines

Thank you for your explaination. I get the point.
And I think if it is proved that the strbuf way is measurably slower. We should add a check for the length of string before we sprintf().

I'm not sure what you mean by checking the string length before sprintf().

My point was that if there are multiple ways of solving the same problem, it can be helpful for reviewers if your commit message explains why the solution you chose is better than the others.

Slowness and/or cleanliness of API were just examples you might use in your commit message for justifying why you chose one approach over another.

author
#5 lore raw

2014-03-01 4:42 GMT+08:00 Eric Sunshine <[email protected]>:

Quoted 34 lines · 孙赫
Quoted 33 lines

2014-02-28 21:12 GMT+08:00 Eric Sunshine [via git] <[email protected]>:

Quoted 30 lines · Eric Sunshine
Quoted 7 lines · Sun He
Quoted 2 lines
Signed-off-by Sun He <[hidden email]>

---

Nicely done.

Quoted 7 lines

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

You may also want your commit message to explain why you chose this approach over other legitimate approaches. For instance, your change places extra burden on callers of finish_tmp_packfile() by leaking a detail of its implementation: namely that it wants to manipulate pathnames easily (via strbuf). An equally valid and more encapsulated approach would be for finish_tmp_packfile() to accept a 'const char *' and construct its own strbuf internally.

If the extra strbuf creation in the alternate approach is measurably slower, then you could use that fact to justify your implementation choice in the commit message. On the other hand, if it's not measurably slower, then perhaps the more encapsulated approach with cleaner API might be preferable.

Quoted 6 lines
Quoted 3 lines

Thank you for your explaination. I get the point.
And I think if it is proved that the strbuf way is measurably slower. We should add a check for the length of string before we sprintf().

I'm not sure what you mean by checking the string length before sprintf().

That's because one is not certain of the length of get_object_directory()

Micheal Mhaggerty<[email protected]> explained this to me.
Saving stack space is nice, though given that it takes more time to allocate space on the heap, it is nonetheless usually preferred to use the stack for temporary variables of this size.

The problem has more to do with the fact that the old version fixes a maximum length on the buffer, which could be a problem if one is not certain of the length of get_object_directory().

The other point of strbuf is that you don't have to do the error-prone bookkeeping yourself. So it would be preferable to use strbuf_addf().

It is the same as yours about the space and time costs. ^_^

Quoted 7 lines

My point was that if there are multiple ways of solving the same problem, it can be helpful for reviewers if your commit message explains why the solution you chose is better than the others.

Slowness and/or cleanliness of API were just examples you might use in your commit message for justifying why you chose one approach over another.

OK, OK, Got it. Thank you very much.

Cheers,
He Sun

author
#7 lore raw

PATCH 1/1 Rewrite bulk-checkin.c:finish_bulk_checkin() to use a strbuf for handling packname

In reply to a message not in the archive

---------- Forwarded message ----------
From: 孙赫 <[email protected]>
Date: 2014-02-28 21:37 GMT+08:00
Subject: Re: [PATCH] Rewrite bulk-checkin.c:finish_bulk_checkin() to use a strbuf for handling packname
To: "Eric Sunshine [via git]" <[email protected]>

2014-02-28 17:47 GMT+08:00 Eric Sunshine [via git] <[email protected]>:

Quoted 6 lines · Sun He
Quoted 2 lines
Signed-off-by Sun He <[hidden email]>

---

Nicely done.

Quoted 8 lines

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

That's great, I will adjust it as you suggested.

Quoted 14 lines

More comments below.

]> diff --git a/builtin/pack-objects.c b/builtin/pack-objects.c

Quoted 10 lines

index c733379..72fb82b 100644

builtin/pack-objects.c +1 -1
@@ -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;
 
Quoted 16 lines

builtin/pack-objects.c static void write_pack_file(void) · line 828

Quoted 10 lines
builtin/pack-objects.c +1 -1
                        /*
                         * 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).

OK. Should I send this patch?

Quoted 40 lines
Quoted 40 lines
builtin/pack-objects.c +2 -6
                        }
 
-                       /* 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);
pack-write.c +0 -0
index 9b8308b..2204aa9 100644
@@ -336,7 +336,7 @@ struct sha1file *create_tmp_packfile(char
 **pack_tmp_name)
        return sha1fd(fd, *pack_tmp_name);
 }
 
Quoted 15 lines

pack-write.c void finish_tmp_packfile(char *name_buffer, · line 347

Quoted 11 lines
pack-write.c +1 -1
-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,

pack-write.c +1 -1
@@ -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.

It's pretty good to work with you next suggestion. THX

Quoted 22 lines
Quoted 9 lines
pack-write.c +1 -2
        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));

pack-write.c void finish_tmp_packfile(char *name_buffer, · line 364

Quoted 10 lines
pack-write.c +4 -3
+       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

Quoted 5 lines

strbuf_setlen(name_buffer, pre_len)

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

Yeah, yeah. It's more elegent. I will fix it later.

Quoted 39 lines

pack-write.c void finish_tmp_packfile(char *name_buffer, · line 371

Quoted 8 lines
pack-write.c +5 -1
+
+       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.

Quoted 28 lines
pack-write.c +0 -1
        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.

Quoted 7 lines
Quoted 1 line

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.
--

Got it.
Thank you very very much for your code review and great suggestions.

He Sun

#8 lore raw
Quoted 34 lines · 孙赫

2014-02-28 17:47 GMT+08:00 Eric Sunshine [via git] <[email protected]>:

Quoted 15 lines · Sun He
Quoted 14 lines
Signed-off-by Sun He <[hidden email]>

---

builtin/pack-objects.c +0 -0

index c733379..72fb82b 100644

builtin/pack-objects.c +1 -1
@@ -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;
 
Quoted 16 lines

builtin/pack-objects.c static void write_pack_file(void) · line 828

Quoted 10 lines
builtin/pack-objects.c +1 -1
                        /*
                         * 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).

Quoted 1 line

OK. Should I send this patch?

Yes, it is perfectly acceptable (and encouraged) to send this patch as a submission distinct from your microproject.