diff options
author | Dana L. How <danahow@gmail.com> | 2007-05-02 12:13:14 -0400 |
---|---|---|
committer | Shawn O. Pearce <spearce@spearce.org> | 2007-05-02 13:24:18 -0400 |
commit | 8b0eca7c7b73d0dc5d49f400a878d9b781ec4bec (patch) | |
tree | ceb7cab66263987f79c156c223870d0efadb1f6c /pack-write.c | |
parent | db81e67a7d559544f8464cdfd011208e60b76344 (diff) | |
download | git-8b0eca7c7b73d0dc5d49f400a878d9b781ec4bec.tar.gz |
Create pack-write.c for common pack writing code
Include a generalized fixup_pack_header_footer() in this new file.
Needed by git-repack --max-pack-size feature in a later patchset.
[sp: Moved close(pack_fd) to callers, to support index-pack, and
changed name to better indicate it is for packfiles.]
Signed-off-by: Dana L. How <danahow@gmail.com>
Signed-off-by: Shawn O. Pearce <spearce@spearce.org>
Diffstat (limited to 'pack-write.c')
-rw-r--r-- | pack-write.c | 39 |
1 files changed, 39 insertions, 0 deletions
diff --git a/pack-write.c b/pack-write.c new file mode 100644 index 0000000000..de72f44dc8 --- /dev/null +++ b/pack-write.c @@ -0,0 +1,39 @@ +#include "cache.h" +#include "pack.h" + +void fixup_pack_header_footer(int pack_fd, + unsigned char *pack_file_sha1, + const char *pack_name, + uint32_t object_count) +{ + static const int buf_sz = 128 * 1024; + SHA_CTX c; + struct pack_header hdr; + char *buf; + + if (lseek(pack_fd, 0, SEEK_SET) != 0) + die("Failed seeking to start: %s", strerror(errno)); + if (read_in_full(pack_fd, &hdr, sizeof(hdr)) != sizeof(hdr)) + die("Unable to reread header of %s: %s", pack_name, strerror(errno)); + if (lseek(pack_fd, 0, SEEK_SET) != 0) + die("Failed seeking to start: %s", strerror(errno)); + hdr.hdr_entries = htonl(object_count); + write_or_die(pack_fd, &hdr, sizeof(hdr)); + + SHA1_Init(&c); + SHA1_Update(&c, &hdr, sizeof(hdr)); + + buf = xmalloc(buf_sz); + for (;;) { + size_t n = xread(pack_fd, buf, buf_sz); + if (!n) + break; + if (n < 0) + die("Failed to checksum %s: %s", pack_name, strerror(errno)); + SHA1_Update(&c, buf, n); + } + free(buf); + + SHA1_Final(pack_file_sha1, &c); + write_or_die(pack_fd, pack_file_sha1, 20); +} |