@matheusbsbs / matheus-bagliano / commits / 566f44252b
Fork 0

Always use struct pack_header for pack header in fast-import.

Previously we were using 'unsigned int' to update the hdr_entries field of the pack header after the file had been completed and was being hashed. This may not be 32 bits on all platforms. Instead we want to always uint32_t. I'm actually cheating here by just using the pack_header like the rest of Git and letting the struct definition declare the correct type. Right now that field is still 'unsigned int' (wrong) but a pending change submitted by Simon 'corecode' Schubert changes it to uint32_t. After that change is merged in fast-import will do the right thing all of the time. Signed-off-by: Shawn O. Pearce <spearce@spearce.org>

Shawn O. Pearce committed Jan 18, 2007 at 11:26 UTC 566f44252b00003d1f4e7baaaf709d74bf73770f
1 file changed +14 -12
fast-import.c
+14 -12
@@ -624,29 +624,31 @@ static void start_packfile(void)
624
625 static void fixup_header_footer(void)
626 {
627 + static const int buf_sz = 128 * 1024;
628 int pack_fd = pack_data->pack_fd;
629 SHA_CTX c;
629 - char hdr[8];
630 - unsigned long cnt;
630 + struct pack_header hdr;
631 char *buf;
632
633 if (lseek(pack_fd, 0, SEEK_SET) != 0)
634 die("Failed seeking to start: %s", strerror(errno));
635 -
636 - SHA1_Init(&c);
637 - if (read_in_full(pack_fd, hdr, 8) != 8)
635 + if (read_in_full(pack_fd, &hdr, sizeof(hdr)) != sizeof(hdr))
636 die("Unable to reread header of %s", pack_data->pack_name);
639 - SHA1_Update(&c, hdr, 8);
637 + if (lseek(pack_fd, 0, SEEK_SET) != 0)
638 + die("Failed seeking to start: %s", strerror(errno));
639 + hdr.hdr_entries = htonl(object_count);
640 + write_or_die(pack_fd, &hdr, sizeof(hdr));
641
641 - cnt = htonl(object_count);
642 - SHA1_Update(&c, &cnt, 4);
643 - write_or_die(pack_fd, &cnt, 4);
642 + SHA1_Init(&c);
643 + SHA1_Update(&c, &hdr, sizeof(hdr));
644
645 - buf = xmalloc(128 * 1024);
645 + buf = xmalloc(buf_sz);
646 for (;;) {
647 - size_t n = xread(pack_fd, buf, 128 * 1024);
648 - if (n <= 0)
647 + size_t n = xread(pack_fd, buf, buf_sz);
648 + if (!n)
649 break;
650 + if (n < 0)
651 + die("Failed to checksum %s", pack_data->pack_name);
652 SHA1_Update(&c, buf, n);
653 }
654 free(buf);