index-pack: use fixup_pack_header_footer()'s validation mode
When completing a thin pack, a new header has to be written to the pack and a new SHA1 computed. Make sure that the SHA1 of what is being read back matches the SHA1 of what was written for both: the original pack and the appended objects. To do so, a couple write_or_die() calls were converted to sha1write() which has the advantage of doing some buffering as well as handling SHA1 and CRC32 checksum already. Signed-off-by: Nicolas Pitre <nico@cam.org> Signed-off-by: Junio C Hamano <gitster@pobox.com>
Nicolas Pitre committed
Aug 29, 2008 at 16:08 UTC
8522148f79c29ef8f63688e7186e458a22333224
1 file changed
+25
-18
index-pack.c
+25
-18
@@ -654,7 +654,7 @@ static void parse_pack_objects(unsigned char *sha1)
654
}
655
}
656
657
-static int write_compressed(int fd, void *in, unsigned int size, uint32_t *obj_crc)
657
+static int write_compressed(struct sha1file *f, void *in, unsigned int size)
658
{
659
z_stream stream;
660
unsigned long maxsize;
@@ -674,13 +674,12 @@ static int write_compressed(int fd, void *in, unsigned int size, uint32_t *obj_c
674
deflateEnd(&stream);
675
676
size = stream.total_out;
677
- write_or_die(fd, out, size);
678
- *obj_crc = crc32(*obj_crc, out, size);
677
+ sha1write(f, out, size);
678
free(out);
679
return size;
680
}
681
683
-static struct object_entry *append_obj_to_pack(
682
+static struct object_entry *append_obj_to_pack(struct sha1file *f,
683
const unsigned char *sha1, void *buf,
684
unsigned long size, enum object_type type)
685
{
@@ -696,15 +695,15 @@ static struct object_entry *append_obj_to_pack(
695
s >>= 7;
696
}
697
header[n++] = c;
699
- write_or_die(output_fd, header, n);
700
- obj[0].idx.crc32 = crc32(0, Z_NULL, 0);
701
- obj[0].idx.crc32 = crc32(obj[0].idx.crc32, header, n);
698
+ crc32_begin(f);
699
+ sha1write(f, header, n);
700
obj[0].size = size;
701
obj[0].hdr_size = n;
702
obj[0].type = type;
703
obj[0].real_type = type;
704
obj[1].idx.offset = obj[0].idx.offset + n;
707
- obj[1].idx.offset += write_compressed(output_fd, buf, size, &obj[0].idx.crc32);
705
+ obj[1].idx.offset += write_compressed(f, buf, size);
706
+ obj[0].idx.crc32 = crc32_end(f);
707
hashcpy(obj->idx.sha1, sha1);
708
return obj;
709
}
@@ -716,7 +715,7 @@ static int delta_pos_compare(const void *_a, const void *_b)
715
return a->obj_no - b->obj_no;
716
}
717
719
-static void fix_unresolved_deltas(int nr_unresolved)
718
+static void fix_unresolved_deltas(struct sha1file *f, int nr_unresolved)
719
{
720
struct delta_entry **sorted_by_pos;
721
int i, n = 0;
@@ -754,8 +753,8 @@ static void fix_unresolved_deltas(int nr_unresolved)
753
if (check_sha1_signature(d->base.sha1, base_obj.data,
754
base_obj.size, typename(type)))
755
die("local object %s is corrupt", sha1_to_hex(d->base.sha1));
757
- base_obj.obj = append_obj_to_pack(d->base.sha1, base_obj.data,
758
- base_obj.size, type);
756
+ base_obj.obj = append_obj_to_pack(f, d->base.sha1,
757
+ base_obj.data, base_obj.size, type);
758
link_base_data(NULL, &base_obj);
759
760
find_delta_children(&d->base, &first, &last);
@@ -875,7 +874,7 @@ int main(int argc, char **argv)
874
const char *keep_name = NULL, *keep_msg = NULL;
875
char *index_name_buf = NULL, *keep_name_buf = NULL;
876
struct pack_idx_entry **idx_objects;
878
- unsigned char sha1[20];
877
+ unsigned char pack_sha1[20];
878
int nongit = 0;
879
880
setup_git_directory_gently(&nongit);
@@ -962,13 +961,15 @@ int main(int argc, char **argv)
961
parse_pack_header();
962
objects = xmalloc((nr_objects + 1) * sizeof(struct object_entry));
963
deltas = xmalloc(nr_objects * sizeof(struct delta_entry));
965
- parse_pack_objects(sha1);
964
+ parse_pack_objects(pack_sha1);
965
if (nr_deltas == nr_resolved_deltas) {
966
stop_progress(&progress);
967
/* Flush remaining pack final 20-byte SHA1. */
968
flush();
969
} else {
970
if (fix_thin_pack) {
971
+ struct sha1file *f;
972
+ unsigned char read_sha1[20], tail_sha1[20];
973
char msg[48];
974
int nr_unresolved = nr_deltas - nr_resolved_deltas;
975
int nr_objects_initial = nr_objects;
@@ -977,13 +978,19 @@ int main(int argc, char **argv)
978
objects = xrealloc(objects,
979
(nr_objects + nr_unresolved + 1)
980
* sizeof(*objects));
980
- fix_unresolved_deltas(nr_unresolved);
981
+ f = sha1fd(output_fd, curr_pack);
982
+ fix_unresolved_deltas(f, nr_unresolved);
983
sprintf(msg, "completed with %d local objects",
984
nr_objects - nr_objects_initial);
985
stop_progress_msg(&progress, msg);
984
- fixup_pack_header_footer(output_fd, sha1,
986
+ sha1close(f, tail_sha1, 0);
987
+ hashcpy(read_sha1, pack_sha1);
988
+ fixup_pack_header_footer(output_fd, pack_sha1,
989
curr_pack, nr_objects,
986
- NULL, 0);
990
+ read_sha1, consumed_bytes-20);
991
+ if (hashcmp(read_sha1, tail_sha1) != 0)
992
+ die("Unexpected tail checksum for %s "
993
+ "(disk corruption?)", curr_pack);
994
}
995
if (nr_deltas != nr_resolved_deltas)
996
die("pack has %d unresolved deltas",
@@ -996,13 +1003,13 @@ int main(int argc, char **argv)
1003
idx_objects = xmalloc((nr_objects) * sizeof(struct pack_idx_entry *));
1004
for (i = 0; i < nr_objects; i++)
1005
idx_objects[i] = &objects[i].idx;
999
- curr_index = write_idx_file(index_name, idx_objects, nr_objects, sha1);
1006
+ curr_index = write_idx_file(index_name, idx_objects, nr_objects, pack_sha1);
1007
free(idx_objects);
1008
1009
final(pack_name, curr_pack,
1010
index_name, curr_index,
1011
keep_name, keep_msg,
1005
- sha1);
1012
+ pack_sha1);
1013
free(objects);
1014
free(index_name_buf);
1015
free(keep_name_buf);