4 static int verify_packfile(struct packed_git
*p
)
6 unsigned long index_size
= p
->index_size
;
7 void *index_base
= p
->index_base
;
9 unsigned char sha1
[20];
10 unsigned long pack_size
= p
->pack_size
;
12 struct pack_header
*hdr
;
13 int nr_objects
, err
, i
;
15 /* Header consistency check */
16 pack_base
= p
->windows
->base
;
17 hdr
= (struct pack_header
*)pack_base
;
18 if (hdr
->hdr_signature
!= htonl(PACK_SIGNATURE
))
19 return error("Packfile %s signature mismatch", p
->pack_name
);
20 if (!pack_version_ok(hdr
->hdr_version
))
21 return error("Packfile version %d unsupported",
22 ntohl(hdr
->hdr_version
));
23 nr_objects
= ntohl(hdr
->hdr_entries
);
24 if (num_packed_objects(p
) != nr_objects
)
25 return error("Packfile claims to have %d objects, "
26 "while idx size expects %d", nr_objects
,
27 num_packed_objects(p
));
30 SHA1_Update(&ctx
, pack_base
, pack_size
- 20);
31 SHA1_Final(sha1
, &ctx
);
32 if (hashcmp(sha1
, (unsigned char *)pack_base
+ pack_size
- 20))
33 return error("Packfile %s SHA1 mismatch with itself",
35 if (hashcmp(sha1
, (unsigned char *)index_base
+ index_size
- 40))
36 return error("Packfile %s SHA1 mismatch with idx",
39 /* Make sure everything reachable from idx is valid. Since we
40 * have verified that nr_objects matches between idx and pack,
41 * we do not do scan-streaming check on the pack file.
43 for (i
= err
= 0; i
< nr_objects
; i
++) {
44 unsigned char sha1
[20];
47 unsigned long size
, offset
;
49 if (nth_packed_object_sha1(p
, i
, sha1
))
50 die("internal error pack-check nth-packed-object");
51 offset
= find_pack_entry_one(sha1
, p
);
53 die("internal error pack-check find-pack-entry-one");
54 data
= unpack_entry(p
, offset
, type
, &size
);
56 err
= error("cannot unpack %s from %s",
57 sha1_to_hex(sha1
), p
->pack_name
);
60 if (check_sha1_signature(sha1
, data
, size
, type
)) {
61 err
= error("packed %s from %s is corrupt",
62 sha1_to_hex(sha1
), p
->pack_name
);
75 static void show_pack_info(struct packed_git
*p
)
77 struct pack_header
*hdr
;
79 unsigned int chain_histogram
[MAX_CHAIN
];
81 hdr
= (struct pack_header
*)p
->windows
->base
;
82 nr_objects
= ntohl(hdr
->hdr_entries
);
83 memset(chain_histogram
, 0, sizeof(chain_histogram
));
85 for (i
= 0; i
< nr_objects
; i
++) {
86 unsigned char sha1
[20], base_sha1
[20];
89 unsigned long store_size
;
91 unsigned int delta_chain_length
;
93 if (nth_packed_object_sha1(p
, i
, sha1
))
94 die("internal error pack-check nth-packed-object");
95 offset
= find_pack_entry_one(sha1
, p
);
97 die("internal error pack-check find-pack-entry-one");
99 packed_object_info_detail(p
, offset
, type
, &size
, &store_size
,
102 printf("%s ", sha1_to_hex(sha1
));
103 if (!delta_chain_length
)
104 printf("%-6s %lu %lu\n", type
, size
, offset
);
106 printf("%-6s %lu %lu %u %s\n", type
, size
, offset
,
107 delta_chain_length
, sha1_to_hex(base_sha1
));
108 if (delta_chain_length
< MAX_CHAIN
)
109 chain_histogram
[delta_chain_length
]++;
111 chain_histogram
[0]++;
115 for (i
= 0; i
< MAX_CHAIN
; i
++) {
116 if (!chain_histogram
[i
])
118 printf("chain length %s %d: %d object%s\n",
122 1 < chain_histogram
[i
] ? "s" : "");
126 int verify_pack(struct packed_git
*p
, int verbose
)
128 unsigned long index_size
= p
->index_size
;
129 void *index_base
= p
->index_base
;
131 unsigned char sha1
[20];
135 /* Verify SHA1 sum of the index file */
137 SHA1_Update(&ctx
, index_base
, index_size
- 20);
138 SHA1_Final(sha1
, &ctx
);
139 if (hashcmp(sha1
, (unsigned char *)index_base
+ index_size
- 20))
140 ret
= error("Packfile index for %s SHA1 mismatch",
144 /* Verify pack file */
146 ret
= verify_packfile(p
);
152 printf("%s: bad\n", p
->pack_name
);
157 printf("%s: ok\n", p
->pack_name
);