finish_tmp_packfile(): a helper function

Factor out a small logic out of the private write_pack_file() function in builtin/pack-objects.c. This changes the order of finishing multi-pack generation slightly. The code used to - adjust shared perm of temporary packfile - rename temporary packfile to the final name - update mtime of the packfile under the final name - adjust shared perm of temporary idxfile - rename temporary idxfile to the final name but because the helper does not want to do the mtime thing, the updated code does that step first and then all the rest. Signed-off-by: Junio C Hamano <gitster@pobox.com>

Junio C Hamano committed Oct 28, 2011 at 12:34 UTC 0e990530ae6d6c6805d31c666953541f762dd402
3 files changed +42 -23
builtin/pack-objects.c
+10 -23
@@ -617,20 +617,8 @@ static void write_pack_file(void)
617
618 if (!pack_to_stdout) {
619 struct stat st;
620 - const char *idx_tmp_name;
620 char tmpname[PATH_MAX];
621
623 - idx_tmp_name = write_idx_file(NULL, written_list, nr_written,
624 - &pack_idx_opts, sha1);
625 -
626 - snprintf(tmpname, sizeof(tmpname), "%s-%s.pack",
627 - base_name, sha1_to_hex(sha1));
628 - free_pack_by_name(tmpname);
629 - if (adjust_shared_perm(pack_tmp_name))
630 - die_errno("unable to make temporary pack file readable");
631 - if (rename(pack_tmp_name, tmpname))
632 - die_errno("unable to rename temporary pack file");
633 -
622 /*
623 * Packs are runtime accessed in their mtime
624 * order since newer packs are more likely to contain
@@ -638,28 +626,27 @@ static void write_pack_file(void)
626 * packs then we should modify the mtime of later ones
627 * to preserve this property.
628 */
641 - if (stat(tmpname, &st) < 0) {
629 + if (stat(pack_tmp_name, &st) < 0) {
630 warning("failed to stat %s: %s",
643 - tmpname, strerror(errno));
631 + pack_tmp_name, strerror(errno));
632 } else if (!last_mtime) {
633 last_mtime = st.st_mtime;
634 } else {
635 struct utimbuf utb;
636 utb.actime = st.st_atime;
637 utb.modtime = --last_mtime;
650 - if (utime(tmpname, &utb) < 0)
638 + if (utime(pack_tmp_name, &utb) < 0)
639 warning("failed utime() on %s: %s",
640 tmpname, strerror(errno));
641 }
642
655 - snprintf(tmpname, sizeof(tmpname), "%s-%s.idx",
656 - base_name, sha1_to_hex(sha1));
657 - if (adjust_shared_perm(idx_tmp_name))
658 - die_errno("unable to make temporary index file readable");
659 - if (rename(idx_tmp_name, tmpname))
660 - die_errno("unable to rename temporary index file");
661 -
662 - free((void *) idx_tmp_name);
643 + /* Enough space for "-<sha-1>.pack"? */
644 + if (sizeof(tmpname) <= strlen(base_name) + 50)
645 + die("pack base name '%s' too long", base_name);
646 + snprintf(tmpname, sizeof(tmpname), "%s-", base_name);
647 + finish_tmp_packfile(tmpname, pack_tmp_name,
648 + written_list, nr_written,
649 + &pack_idx_opts, sha1);
650 free(pack_tmp_name);
651 puts(sha1_to_hex(sha1));
652 }
pack-write.c
+31
@@ -338,3 +338,34 @@ struct sha1file *create_tmp_packfile(char **pack_tmp_name)
338 *pack_tmp_name = xstrdup(tmpname);
339 return sha1fd(fd, *pack_tmp_name);
340 }
341 +
342 +void finish_tmp_packfile(char *name_buffer,
343 + const char *pack_tmp_name,
344 + struct pack_idx_entry **written_list,
345 + uint32_t nr_written,
346 + struct pack_idx_option *pack_idx_opts,
347 + unsigned char sha1[])
348 +{
349 + const char *idx_tmp_name;
350 + char *end_of_name_prefix = strrchr(name_buffer, 0);
351 +
352 + if (adjust_shared_perm(pack_tmp_name))
353 + die_errno("unable to make temporary pack file readable");
354 +
355 + idx_tmp_name = write_idx_file(NULL, written_list, nr_written,
356 + pack_idx_opts, sha1);
357 + if (adjust_shared_perm(idx_tmp_name))
358 + die_errno("unable to make temporary index file readable");
359 +
360 + sprintf(end_of_name_prefix, "%s.pack", sha1_to_hex(sha1));
361 + free_pack_by_name(name_buffer);
362 +
363 + if (rename(pack_tmp_name, name_buffer))
364 + die_errno("unable to rename temporary pack file");
365 +
366 + sprintf(end_of_name_prefix, "%s.idx", sha1_to_hex(sha1));
367 + if (rename(idx_tmp_name, name_buffer))
368 + die_errno("unable to rename temporary index file");
369 +
370 + free((void *)idx_tmp_name);
371 +}
pack.h
+1
@@ -86,5 +86,6 @@ extern int encode_in_pack_object_header(enum object_type, uintmax_t, unsigned ch
86 extern int read_pack_header(int fd, struct pack_header *);
87
88 extern struct sha1file *create_tmp_packfile(char **pack_tmp_name);
89 +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[]);
90
91 #endif