248
bad_object(obj->offset, "unknown object type %d", obj->type);
246
bad_object(obj->idx.offset, "unknown object type %d", obj->type);
250
obj->hdr_size = consumed_bytes - obj->offset;
248
obj->hdr_size = consumed_bytes - obj->idx.offset;
252
data = unpack_entry_data(obj->offset, obj->size);
253
obj->crc32 = input_crc32;
250
data = unpack_entry_data(obj->idx.offset, obj->size);
251
obj->idx.crc32 = input_crc32;
257
255
static void *get_data_from_pack(struct object_entry *obj)
259
unsigned long from = obj[0].offset + obj[0].hdr_size;
260
unsigned long len = obj[1].offset - from;
257
unsigned long from = obj[0].idx.offset + obj[0].hdr_size;
258
unsigned long len = obj[1].idx.offset - from;
261
259
unsigned long rdy = 0;
262
260
unsigned char *src, *data;
361
359
free(delta_data);
363
bad_object(delta_obj->offset, "failed to apply delta");
364
sha1_object(result, result_size, type, delta_obj->sha1);
361
bad_object(delta_obj->idx.offset, "failed to apply delta");
362
sha1_object(result, result_size, type, delta_obj->idx.sha1);
365
363
nr_resolved_deltas++;
367
hashcpy(delta_base.sha1, delta_obj->sha1);
365
hashcpy(delta_base.sha1, delta_obj->idx.sha1);
368
366
if (!find_delta_children(&delta_base, &first, &last)) {
369
367
for (j = first; j <= last; j++) {
370
368
struct object_entry *child = objects + deltas[j].obj_no;
537
535
write_or_die(output_fd, header, n);
538
obj[0].crc32 = crc32(0, Z_NULL, 0);
539
obj[0].crc32 = crc32(obj[0].crc32, header, n);
540
obj[1].offset = obj[0].offset + n;
541
obj[1].offset += write_compressed(output_fd, buf, size, &obj[0].crc32);
542
hashcpy(obj->sha1, sha1);
536
obj[0].idx.crc32 = crc32(0, Z_NULL, 0);
537
obj[0].idx.crc32 = crc32(obj[0].idx.crc32, header, n);
538
obj[1].idx.offset = obj[0].idx.offset + n;
539
obj[1].idx.offset += write_compressed(output_fd, buf, size, &obj[0].idx.crc32);
540
hashcpy(obj->idx.sha1, sha1);
545
543
static int delta_pos_compare(const void *_a, const void *_b)
602
600
free(sorted_by_pos);
605
static uint32_t index_default_version = 1;
606
static uint32_t index_off32_limit = 0x7fffffff;
608
static int sha1_compare(const void *_a, const void *_b)
610
struct object_entry *a = *(struct object_entry **)_a;
611
struct object_entry *b = *(struct object_entry **)_b;
612
return hashcmp(a->sha1, b->sha1);
616
* On entry *sha1 contains the pack content SHA1 hash, on exit it is
617
* the SHA1 hash of sorted object names.
619
static const char *write_index_file(const char *index_name, unsigned char *sha1)
622
struct object_entry **sorted_by_sha, **list, **last;
626
uint32_t index_version;
630
xcalloc(nr_objects, sizeof(struct object_entry *));
631
list = sorted_by_sha;
632
last = sorted_by_sha + nr_objects;
633
for (i = 0; i < nr_objects; ++i)
634
sorted_by_sha[i] = &objects[i];
635
qsort(sorted_by_sha, nr_objects, sizeof(sorted_by_sha[0]),
639
sorted_by_sha = list = last = NULL;
642
static char tmpfile[PATH_MAX];
643
snprintf(tmpfile, sizeof(tmpfile),
644
"%s/tmp_idx_XXXXXX", get_object_directory());
645
fd = mkstemp(tmpfile);
646
index_name = xstrdup(tmpfile);
649
fd = open(index_name, O_CREAT|O_EXCL|O_WRONLY, 0600);
652
die("unable to create %s: %s", index_name, strerror(errno));
653
f = sha1fd(fd, index_name);
655
/* if last object's offset is >= 2^31 we should use index V2 */
656
index_version = (objects[nr_objects-1].offset >> 31) ? 2 : index_default_version;
658
/* index versions 2 and above need a header */
659
if (index_version >= 2) {
660
struct pack_idx_header hdr;
661
hdr.idx_signature = htonl(PACK_IDX_SIGNATURE);
662
hdr.idx_version = htonl(index_version);
663
sha1write(f, &hdr, sizeof(hdr));
667
* Write the first-level table (the list is sorted,
668
* but we use a 256-entry lookup to be able to avoid
669
* having to do eight extra binary search iterations).
671
for (i = 0; i < 256; i++) {
672
struct object_entry **next = list;
673
while (next < last) {
674
struct object_entry *obj = *next;
675
if (obj->sha1[0] != i)
679
array[i] = htonl(next - sorted_by_sha);
682
sha1write(f, array, 256 * 4);
684
/* compute the SHA1 hash of sorted object names. */
688
* Write the actual SHA1 entries..
690
list = sorted_by_sha;
691
for (i = 0; i < nr_objects; i++) {
692
struct object_entry *obj = *list++;
693
if (index_version < 2) {
694
uint32_t offset = htonl(obj->offset);
695
sha1write(f, &offset, 4);
697
sha1write(f, obj->sha1, 20);
698
SHA1_Update(&ctx, obj->sha1, 20);
701
if (index_version >= 2) {
702
unsigned int nr_large_offset = 0;
704
/* write the crc32 table */
705
list = sorted_by_sha;
706
for (i = 0; i < nr_objects; i++) {
707
struct object_entry *obj = *list++;
708
uint32_t crc32_val = htonl(obj->crc32);
709
sha1write(f, &crc32_val, 4);
712
/* write the 32-bit offset table */
713
list = sorted_by_sha;
714
for (i = 0; i < nr_objects; i++) {
715
struct object_entry *obj = *list++;
716
uint32_t offset = (obj->offset <= index_off32_limit) ?
717
obj->offset : (0x80000000 | nr_large_offset++);
718
offset = htonl(offset);
719
sha1write(f, &offset, 4);
722
/* write the large offset table */
723
list = sorted_by_sha;
724
while (nr_large_offset) {
725
struct object_entry *obj = *list++;
726
uint64_t offset = obj->offset;
727
if (offset > index_off32_limit) {
729
split[0] = htonl(offset >> 32);
730
split[1] = htonl(offset & 0xffffffff);
731
sha1write(f, split, 8);
737
sha1write(f, sha1, 20);
738
sha1close(f, NULL, 1);
740
SHA1_Final(sha1, &ctx);
744
603
static void final(const char *final_pack_name, const char *curr_pack_name,
745
604
const char *final_index_name, const char *curr_index_name,
746
605
const char *keep_name, const char *keep_msg,