Skip to content

Commit 0e99053

Browse files
committed
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 <[email protected]>
1 parent cdf9db3 commit 0e99053

File tree

3 files changed

+42
-23
lines changed

3 files changed

+42
-23
lines changed

builtin/pack-objects.c

Lines changed: 10 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -617,49 +617,36 @@ static void write_pack_file(void)
617617

618618
if (!pack_to_stdout) {
619619
struct stat st;
620-
const char *idx_tmp_name;
621620
char tmpname[PATH_MAX];
622621

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-
634622
/*
635623
* Packs are runtime accessed in their mtime
636624
* order since newer packs are more likely to contain
637625
* younger objects. So if we are creating multiple
638626
* packs then we should modify the mtime of later ones
639627
* to preserve this property.
640628
*/
641-
if (stat(tmpname, &st) < 0) {
629+
if (stat(pack_tmp_name, &st) < 0) {
642630
warning("failed to stat %s: %s",
643-
tmpname, strerror(errno));
631+
pack_tmp_name, strerror(errno));
644632
} else if (!last_mtime) {
645633
last_mtime = st.st_mtime;
646634
} else {
647635
struct utimbuf utb;
648636
utb.actime = st.st_atime;
649637
utb.modtime = --last_mtime;
650-
if (utime(tmpname, &utb) < 0)
638+
if (utime(pack_tmp_name, &utb) < 0)
651639
warning("failed utime() on %s: %s",
652640
tmpname, strerror(errno));
653641
}
654642

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);
663650
free(pack_tmp_name);
664651
puts(sha1_to_hex(sha1));
665652
}

pack-write.c

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -338,3 +338,34 @@ struct sha1file *create_tmp_packfile(char **pack_tmp_name)
338338
*pack_tmp_name = xstrdup(tmpname);
339339
return sha1fd(fd, *pack_tmp_name);
340340
}
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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,5 +86,6 @@ extern int encode_in_pack_object_header(enum object_type, uintmax_t, unsigned ch
8686
extern int read_pack_header(int fd, struct pack_header *);
8787

8888
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[]);
8990

9091
#endif

0 commit comments

Comments
 (0)