Merge branch 'maint-1.5.1' into maint
[git/dscho.git] / pack-write.c
blobae2e481e5554cfa66cf137d38c38f961b9aa8ec7
1 #include "cache.h"
2 #include "pack.h"
4 void fixup_pack_header_footer(int pack_fd,
5 unsigned char *pack_file_sha1,
6 const char *pack_name,
7 uint32_t object_count)
9 static const int buf_sz = 128 * 1024;
10 SHA_CTX c;
11 struct pack_header hdr;
12 char *buf;
14 if (lseek(pack_fd, 0, SEEK_SET) != 0)
15 die("Failed seeking to start: %s", strerror(errno));
16 if (read_in_full(pack_fd, &hdr, sizeof(hdr)) != sizeof(hdr))
17 die("Unable to reread header of %s: %s", pack_name, strerror(errno));
18 if (lseek(pack_fd, 0, SEEK_SET) != 0)
19 die("Failed seeking to start: %s", strerror(errno));
20 hdr.hdr_entries = htonl(object_count);
21 write_or_die(pack_fd, &hdr, sizeof(hdr));
23 SHA1_Init(&c);
24 SHA1_Update(&c, &hdr, sizeof(hdr));
26 buf = xmalloc(buf_sz);
27 for (;;) {
28 ssize_t n = xread(pack_fd, buf, buf_sz);
29 if (!n)
30 break;
31 if (n < 0)
32 die("Failed to checksum %s: %s", pack_name, strerror(errno));
33 SHA1_Update(&c, buf, n);
35 free(buf);
37 SHA1_Final(pack_file_sha1, &c);
38 write_or_die(pack_fd, pack_file_sha1, 20);