From c0ad465725302b9411e53d248871c36880b6f8fd Mon Sep 17 00:00:00 2001 From: Junio C Hamano Date: Fri, 28 Oct 2011 11:40:48 -0700 Subject: write_pack_header(): a helper function Factor out a small logic out of the private write_pack_file() function in builtin/pack-objects.c Signed-off-by: Junio C Hamano --- pack-write.c | 12 ++++++++++++ 1 file changed, 12 insertions(+) (limited to 'pack-write.c') diff --git a/pack-write.c b/pack-write.c index 9cd3bfbb4..46f3f846b 100644 --- a/pack-write.c +++ b/pack-write.c @@ -178,6 +178,18 @@ const char *write_idx_file(const char *index_name, struct pack_idx_entry **objec return index_name; } +off_t write_pack_header(struct sha1file *f, uint32_t nr_entries) +{ + struct pack_header hdr; + + hdr.hdr_signature = htonl(PACK_SIGNATURE); + hdr.hdr_version = htonl(PACK_VERSION); + hdr.hdr_entries = htonl(nr_entries); + if (sha1write(f, &hdr, sizeof(hdr))) + return 0; + return sizeof(hdr); +} + /* * Update pack header with object_count and compute new SHA1 for pack data * associated to pack_fd, and write that SHA1 at the end. That new SHA1 -- cgit v1.2.1 From cdf9db3c83618453809d6a584d1a0db19b3e189f Mon Sep 17 00:00:00 2001 From: Junio C Hamano Date: Fri, 28 Oct 2011 11:52:14 -0700 Subject: create_tmp_packfile(): a helper function Factor out a small logic out of the private write_pack_file() function in builtin/pack-objects.c Signed-off-by: Junio C Hamano --- pack-write.c | 10 ++++++++++ 1 file changed, 10 insertions(+) (limited to 'pack-write.c') diff --git a/pack-write.c b/pack-write.c index 46f3f846b..863cce8a0 100644 --- a/pack-write.c +++ b/pack-write.c @@ -328,3 +328,13 @@ int encode_in_pack_object_header(enum object_type type, uintmax_t size, unsigned *hdr = c; return n; } + +struct sha1file *create_tmp_packfile(char **pack_tmp_name) +{ + char tmpname[PATH_MAX]; + int fd; + + fd = odb_mkstemp(tmpname, sizeof(tmpname), "pack/tmp_pack_XXXXXX"); + *pack_tmp_name = xstrdup(tmpname); + return sha1fd(fd, *pack_tmp_name); +} -- cgit v1.2.1 From 0e990530ae6d6c6805d31c666953541f762dd402 Mon Sep 17 00:00:00 2001 From: Junio C Hamano Date: Fri, 28 Oct 2011 12:34:09 -0700 Subject: 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 --- pack-write.c | 31 +++++++++++++++++++++++++++++++ 1 file changed, 31 insertions(+) (limited to 'pack-write.c') diff --git a/pack-write.c b/pack-write.c index 863cce8a0..cadc3e1ad 100644 --- a/pack-write.c +++ b/pack-write.c @@ -338,3 +338,34 @@ struct sha1file *create_tmp_packfile(char **pack_tmp_name) *pack_tmp_name = xstrdup(tmpname); return sha1fd(fd, *pack_tmp_name); } + +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[]) +{ + const char *idx_tmp_name; + char *end_of_name_prefix = strrchr(name_buffer, 0); + + if (adjust_shared_perm(pack_tmp_name)) + die_errno("unable to make temporary pack file readable"); + + idx_tmp_name = write_idx_file(NULL, written_list, nr_written, + pack_idx_opts, sha1); + 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); + + if (rename(pack_tmp_name, name_buffer)) + 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)) + die_errno("unable to rename temporary index file"); + + free((void *)idx_tmp_name); +} -- cgit v1.2.1