@matheusbsbs / matheus-bagliano / commits / d1a46a9eab
Fork 0

index-pack: learn about pack index version 2

Like previous patch but for index-pack. [ There is quite some code duplication between pack-objects and index-pack for generating a pack index (and fast-import as well I suppose). This should be reworked into a common function eventually. But not now. ] Signed-off-by: Nicolas Pitre <nico@cam.org> Signed-off-by: Junio C Hamano <junkio@cox.net>

Nicolas Pitre committed Apr 9, 2007 at 01:06 UTC d1a46a9eab2ce136240fe2e8db34b36338052e97
1 file changed +57 -9
index-pack.c
+57 -9
@@ -686,9 +686,10 @@ static const char *write_index_file(const char *index_name, unsigned char *sha1)
686 {
687 struct sha1file *f;
688 struct object_entry **sorted_by_sha, **list, **last;
689 - unsigned int array[256];
689 + uint32_t array[256];
690 int i, fd;
691 SHA_CTX ctx;
692 + uint32_t index_version;
693
694 if (nr_objects) {
695 sorted_by_sha =
@@ -699,7 +700,6 @@ static const char *write_index_file(const char *index_name, unsigned char *sha1)
700 sorted_by_sha[i] = &objects[i];
701 qsort(sorted_by_sha, nr_objects, sizeof(sorted_by_sha[0]),
702 sha1_compare);
702 -
703 }
704 else
705 sorted_by_sha = list = last = NULL;
@@ -718,6 +718,17 @@ static const char *write_index_file(const char *index_name, unsigned char *sha1)
718 die("unable to create %s: %s", index_name, strerror(errno));
719 f = sha1fd(fd, index_name);
720
721 + /* if last object's offset is >= 2^31 we should use index V2 */
722 + index_version = (objects[nr_objects-1].offset >> 31) ? 2 : 1;
723 +
724 + /* index versions 2 and above need a header */
725 + if (index_version >= 2) {
726 + struct pack_idx_header hdr;
727 + hdr.idx_signature = htonl(PACK_IDX_SIGNATURE);
728 + hdr.idx_version = htonl(index_version);
729 + sha1write(f, &hdr, sizeof(hdr));
730 + }
731 +
732 /*
733 * Write the first-level table (the list is sorted,
734 * but we use a 256-entry lookup to be able to avoid
@@ -734,24 +745,61 @@ static const char *write_index_file(const char *index_name, unsigned char *sha1)
745 array[i] = htonl(next - sorted_by_sha);
746 list = next;
747 }
737 - sha1write(f, array, 256 * sizeof(int));
748 + sha1write(f, array, 256 * 4);
749
739 - /* recompute the SHA1 hash of sorted object names.
740 - * currently pack-objects does not do this, but that
741 - * can be fixed.
742 - */
750 + /* compute the SHA1 hash of sorted object names. */
751 SHA1_Init(&ctx);
752 +
753 /*
754 * Write the actual SHA1 entries..
755 */
756 list = sorted_by_sha;
757 for (i = 0; i < nr_objects; i++) {
758 struct object_entry *obj = *list++;
750 - unsigned int offset = htonl(obj->offset);
751 - sha1write(f, &offset, 4);
759 + if (index_version < 2) {
760 + uint32_t offset = htonl(obj->offset);
761 + sha1write(f, &offset, 4);
762 + }
763 sha1write(f, obj->sha1, 20);
764 SHA1_Update(&ctx, obj->sha1, 20);
765 }
766 +
767 + if (index_version >= 2) {
768 + unsigned int nr_large_offset = 0;
769 +
770 + /* write the crc32 table */
771 + list = sorted_by_sha;
772 + for (i = 0; i < nr_objects; i++) {
773 + struct object_entry *obj = *list++;
774 + uint32_t crc32_val = htonl(obj->crc32);
775 + sha1write(f, &crc32_val, 4);
776 + }
777 +
778 + /* write the 32-bit offset table */
779 + list = sorted_by_sha;
780 + for (i = 0; i < nr_objects; i++) {
781 + struct object_entry *obj = *list++;
782 + uint32_t offset = (obj->offset <= 0x7fffffff) ?
783 + obj->offset : (0x80000000 | nr_large_offset++);
784 + offset = htonl(offset);
785 + sha1write(f, &offset, 4);
786 + }
787 +
788 + /* write the large offset table */
789 + list = sorted_by_sha;
790 + while (nr_large_offset) {
791 + struct object_entry *obj = *list++;
792 + uint64_t offset = obj->offset;
793 + if (offset > 0x7fffffff) {
794 + uint32_t split[2];
795 + split[0] = htonl(offset >> 32);
796 + split[1] = htonl(offset & 0xffffffff);
797 + sha1write(f, split, 8);
798 + nr_large_offset--;
799 + }
800 + }
801 + }
802 +
803 sha1write(f, sha1, 20);
804 sha1close(f, NULL, 1);
805 free(sorted_by_sha);