summaryrefslogtreecommitdiff
path: root/pack-write.c
diff options
context:
space:
mode:
authorÆvar Arnfjörð Bjarmason <avarab@gmail.com>2021-09-09 23:24:37 (GMT)
committerJunio C Hamano <gitster@pobox.com>2021-09-10 01:23:11 (GMT)
commit66833f0e70c473ca6c4e6a79d34e879d8b40ba9d (patch)
treea6708df628cdb5a5a2a8b83867a7b2407501d916 /pack-write.c
parentae44b5a4f3b091bc77adc4a70e7bfefd569b78a8 (diff)
downloadgit-66833f0e70c473ca6c4e6a79d34e879d8b40ba9d.zip
git-66833f0e70c473ca6c4e6a79d34e879d8b40ba9d.tar.gz
git-66833f0e70c473ca6c4e6a79d34e879d8b40ba9d.tar.bz2
pack-write: refactor renaming in finish_tmp_packfile()
Refactor the renaming in finish_tmp_packfile() into a helper function. The callers are now expected to pass a "name_buffer" ending in "pack-OID." instead of the previous "pack-", we then append "pack", "idx" or "rev" to it. By doing the strbuf_setlen() in rename_tmp_packfile() we reuse the buffer and avoid the repeated allocations we'd get if that function had its own temporary "struct strbuf". This approach of reusing the buffer does make the last user in pack-object.c's write_pack_file() slightly awkward, since we needlessly do a strbuf_setlen() before calling strbuf_release() for consistency. In subsequent changes we'll move that bitmap writing code around, so let's not skip the strbuf_setlen() now. The previous strbuf_reset() idiom originated with 5889271114a (finish_tmp_packfile():use strbuf for pathname construction, 2014-03-03), which in turn was a minimal adjustment of pre-strbuf code added in 0e990530ae (finish_tmp_packfile(): a helper function, 2011-10-28). Signed-off-by: Ævar Arnfjörð Bjarmason <avarab@gmail.com> Signed-off-by: Taylor Blau <me@ttaylorr.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>
Diffstat (limited to 'pack-write.c')
-rw-r--r--pack-write.c37
1 files changed, 16 insertions, 21 deletions
diff --git a/pack-write.c b/pack-write.c
index f1fc3ec..ca94e2b 100644
--- a/pack-write.c
+++ b/pack-write.c
@@ -462,6 +462,18 @@ struct hashfile *create_tmp_packfile(char **pack_tmp_name)
return hashfd(fd, *pack_tmp_name);
}
+static void rename_tmp_packfile(struct strbuf *name_prefix, const char *source,
+ const char *ext)
+{
+ size_t name_prefix_len = name_prefix->len;
+
+ strbuf_addstr(name_prefix, ext);
+ if (rename(source, name_prefix->buf))
+ die_errno("unable to rename temporary file to '%s'",
+ name_prefix->buf);
+ strbuf_setlen(name_prefix, name_prefix_len);
+}
+
void finish_tmp_packfile(struct strbuf *name_buffer,
const char *pack_tmp_name,
struct pack_idx_entry **written_list,
@@ -470,7 +482,6 @@ void finish_tmp_packfile(struct strbuf *name_buffer,
unsigned char hash[])
{
const char *idx_tmp_name, *rev_tmp_name = NULL;
- int basename_len = name_buffer->len;
if (adjust_shared_perm(pack_tmp_name))
die_errno("unable to make temporary pack file readable");
@@ -483,26 +494,10 @@ void finish_tmp_packfile(struct strbuf *name_buffer,
rev_tmp_name = write_rev_file(NULL, written_list, nr_written, hash,
pack_idx_opts->flags);
- strbuf_addf(name_buffer, "%s.pack", hash_to_hex(hash));
-
- if (rename(pack_tmp_name, name_buffer->buf))
- die_errno("unable to rename temporary pack file");
-
- strbuf_setlen(name_buffer, basename_len);
-
- strbuf_addf(name_buffer, "%s.idx", hash_to_hex(hash));
- if (rename(idx_tmp_name, name_buffer->buf))
- die_errno("unable to rename temporary index file");
-
- strbuf_setlen(name_buffer, basename_len);
-
- if (rev_tmp_name) {
- strbuf_addf(name_buffer, "%s.rev", hash_to_hex(hash));
- if (rename(rev_tmp_name, name_buffer->buf))
- die_errno("unable to rename temporary reverse-index file");
- }
-
- strbuf_setlen(name_buffer, basename_len);
+ rename_tmp_packfile(name_buffer, pack_tmp_name, "pack");
+ rename_tmp_packfile(name_buffer, idx_tmp_name, "idx");
+ if (rev_tmp_name)
+ rename_tmp_packfile(name_buffer, rev_tmp_name, "rev");
free((void *)idx_tmp_name);
}