shortlog: group by committer information
[alt-git.git] / pack-check.c
blob27f70d345fbf1b339cb4bad1011253cece1bf619
1 #include "cache.h"
2 #include "pack.h"
3 #include "pack-revindex.h"
4 #include "progress.h"
6 struct idx_entry {
7 off_t offset;
8 const unsigned char *sha1;
9 unsigned int nr;
12 static int compare_entries(const void *e1, const void *e2)
14 const struct idx_entry *entry1 = e1;
15 const struct idx_entry *entry2 = e2;
16 if (entry1->offset < entry2->offset)
17 return -1;
18 if (entry1->offset > entry2->offset)
19 return 1;
20 return 0;
23 int check_pack_crc(struct packed_git *p, struct pack_window **w_curs,
24 off_t offset, off_t len, unsigned int nr)
26 const uint32_t *index_crc;
27 uint32_t data_crc = crc32(0, NULL, 0);
29 do {
30 unsigned long avail;
31 void *data = use_pack(p, w_curs, offset, &avail);
32 if (avail > len)
33 avail = len;
34 data_crc = crc32(data_crc, data, avail);
35 offset += avail;
36 len -= avail;
37 } while (len);
39 index_crc = p->index_data;
40 index_crc += 2 + 256 + p->num_objects * (20/4) + nr;
42 return data_crc != ntohl(*index_crc);
45 static int verify_packfile(struct packed_git *p,
46 struct pack_window **w_curs,
47 verify_fn fn,
48 struct progress *progress, uint32_t base_count)
51 off_t index_size = p->index_size;
52 const unsigned char *index_base = p->index_data;
53 git_SHA_CTX ctx;
54 unsigned char sha1[20], *pack_sig;
55 off_t offset = 0, pack_sig_ofs = 0;
56 uint32_t nr_objects, i;
57 int err = 0;
58 struct idx_entry *entries;
60 if (!is_pack_valid(p))
61 return error("packfile %s cannot be accessed", p->pack_name);
63 git_SHA1_Init(&ctx);
64 do {
65 unsigned long remaining;
66 unsigned char *in = use_pack(p, w_curs, offset, &remaining);
67 offset += remaining;
68 if (!pack_sig_ofs)
69 pack_sig_ofs = p->pack_size - 20;
70 if (offset > pack_sig_ofs)
71 remaining -= (unsigned int)(offset - pack_sig_ofs);
72 git_SHA1_Update(&ctx, in, remaining);
73 } while (offset < pack_sig_ofs);
74 git_SHA1_Final(sha1, &ctx);
75 pack_sig = use_pack(p, w_curs, pack_sig_ofs, NULL);
76 if (hashcmp(sha1, pack_sig))
77 err = error("%s SHA1 checksum mismatch",
78 p->pack_name);
79 if (hashcmp(index_base + index_size - 40, pack_sig))
80 err = error("%s SHA1 does not match its index",
81 p->pack_name);
82 unuse_pack(w_curs);
84 /* Make sure everything reachable from idx is valid. Since we
85 * have verified that nr_objects matches between idx and pack,
86 * we do not do scan-streaming check on the pack file.
88 nr_objects = p->num_objects;
89 ALLOC_ARRAY(entries, nr_objects + 1);
90 entries[nr_objects].offset = pack_sig_ofs;
91 /* first sort entries by pack offset, since unpacking them is more efficient that way */
92 for (i = 0; i < nr_objects; i++) {
93 entries[i].sha1 = nth_packed_object_sha1(p, i);
94 if (!entries[i].sha1)
95 die("internal error pack-check nth-packed-object");
96 entries[i].offset = nth_packed_object_offset(p, i);
97 entries[i].nr = i;
99 QSORT(entries, nr_objects, compare_entries);
101 for (i = 0; i < nr_objects; i++) {
102 void *data;
103 enum object_type type;
104 unsigned long size;
105 off_t curpos;
106 int data_valid;
108 if (p->index_version > 1) {
109 off_t offset = entries[i].offset;
110 off_t len = entries[i+1].offset - offset;
111 unsigned int nr = entries[i].nr;
112 if (check_pack_crc(p, w_curs, offset, len, nr))
113 err = error("index CRC mismatch for object %s "
114 "from %s at offset %"PRIuMAX"",
115 sha1_to_hex(entries[i].sha1),
116 p->pack_name, (uintmax_t)offset);
119 curpos = entries[i].offset;
120 type = unpack_object_header(p, w_curs, &curpos, &size);
121 unuse_pack(w_curs);
123 if (type == OBJ_BLOB && big_file_threshold <= size) {
125 * Let check_sha1_signature() check it with
126 * the streaming interface; no point slurping
127 * the data in-core only to discard.
129 data = NULL;
130 data_valid = 0;
131 } else {
132 data = unpack_entry(p, entries[i].offset, &type, &size);
133 data_valid = 1;
136 if (data_valid && !data)
137 err = error("cannot unpack %s from %s at offset %"PRIuMAX"",
138 sha1_to_hex(entries[i].sha1), p->pack_name,
139 (uintmax_t)entries[i].offset);
140 else if (check_sha1_signature(entries[i].sha1, data, size, typename(type)))
141 err = error("packed %s from %s is corrupt",
142 sha1_to_hex(entries[i].sha1), p->pack_name);
143 else if (fn) {
144 int eaten = 0;
145 err |= fn(entries[i].sha1, type, size, data, &eaten);
146 if (eaten)
147 data = NULL;
149 if (((base_count + i) & 1023) == 0)
150 display_progress(progress, base_count + i);
151 free(data);
154 display_progress(progress, base_count + i);
155 free(entries);
157 return err;
160 int verify_pack_index(struct packed_git *p)
162 off_t index_size;
163 const unsigned char *index_base;
164 git_SHA_CTX ctx;
165 unsigned char sha1[20];
166 int err = 0;
168 if (open_pack_index(p))
169 return error("packfile %s index not opened", p->pack_name);
170 index_size = p->index_size;
171 index_base = p->index_data;
173 /* Verify SHA1 sum of the index file */
174 git_SHA1_Init(&ctx);
175 git_SHA1_Update(&ctx, index_base, (unsigned int)(index_size - 20));
176 git_SHA1_Final(sha1, &ctx);
177 if (hashcmp(sha1, index_base + index_size - 20))
178 err = error("Packfile index for %s SHA1 mismatch",
179 p->pack_name);
180 return err;
183 int verify_pack(struct packed_git *p, verify_fn fn,
184 struct progress *progress, uint32_t base_count)
186 int err = 0;
187 struct pack_window *w_curs = NULL;
189 err |= verify_pack_index(p);
190 if (!p->index_data)
191 return -1;
193 err |= verify_packfile(p, &w_curs, fn, progress, base_count);
194 unuse_pack(&w_curs);
196 return err;