fsck: avoid reading every object twice
[git/git.git] / pack-check.c
1 #include "cache.h"
2 #include "pack.h"
3 #include "pack-revindex.h"
4
5 struct idx_entry {
6 off_t offset;
7 const unsigned char *sha1;
8 unsigned int nr;
9 };
10
11 static int compare_entries(const void *e1, const void *e2)
12 {
13 const struct idx_entry *entry1 = e1;
14 const struct idx_entry *entry2 = e2;
15 if (entry1->offset < entry2->offset)
16 return -1;
17 if (entry1->offset > entry2->offset)
18 return 1;
19 return 0;
20 }
21
22 int check_pack_crc(struct packed_git *p, struct pack_window **w_curs,
23 off_t offset, off_t len, unsigned int nr)
24 {
25 const uint32_t *index_crc;
26 uint32_t data_crc = crc32(0, NULL, 0);
27
28 do {
29 unsigned long avail;
30 void *data = use_pack(p, w_curs, offset, &avail);
31 if (avail > len)
32 avail = len;
33 data_crc = crc32(data_crc, data, avail);
34 offset += avail;
35 len -= avail;
36 } while (len);
37
38 index_crc = p->index_data;
39 index_crc += 2 + 256 + p->num_objects * (20/4) + nr;
40
41 return data_crc != ntohl(*index_crc);
42 }
43
44 static int verify_packfile(struct packed_git *p,
45 struct pack_window **w_curs,
46 verify_fn fn)
47 {
48 off_t index_size = p->index_size;
49 const unsigned char *index_base = p->index_data;
50 git_SHA_CTX ctx;
51 unsigned char sha1[20], *pack_sig;
52 off_t offset = 0, pack_sig_ofs = 0;
53 uint32_t nr_objects, i;
54 int err = 0;
55 struct idx_entry *entries;
56
57 /* Note that the pack header checks are actually performed by
58 * use_pack when it first opens the pack file. If anything
59 * goes wrong during those checks then the call will die out
60 * immediately.
61 */
62
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);
83
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.
87 */
88 nr_objects = p->num_objects;
89 entries = xmalloc((nr_objects + 1) * sizeof(*entries));
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;
98 }
99 qsort(entries, nr_objects, sizeof(*entries), compare_entries);
100
101 for (i = 0; i < nr_objects; i++) {
102 void *data;
103 enum object_type type;
104 unsigned long size;
105
106 if (p->index_version > 1) {
107 off_t offset = entries[i].offset;
108 off_t len = entries[i+1].offset - offset;
109 unsigned int nr = entries[i].nr;
110 if (check_pack_crc(p, w_curs, offset, len, nr))
111 err = error("index CRC mismatch for object %s "
112 "from %s at offset %"PRIuMAX"",
113 sha1_to_hex(entries[i].sha1),
114 p->pack_name, (uintmax_t)offset);
115 }
116 data = unpack_entry(p, entries[i].offset, &type, &size);
117 if (!data)
118 err = error("cannot unpack %s from %s at offset %"PRIuMAX"",
119 sha1_to_hex(entries[i].sha1), p->pack_name,
120 (uintmax_t)entries[i].offset);
121 else if (check_sha1_signature(entries[i].sha1, data, size, typename(type)))
122 err = error("packed %s from %s is corrupt",
123 sha1_to_hex(entries[i].sha1), p->pack_name);
124 else if (fn) {
125 int eaten = 0;
126 fn(entries[i].sha1, type, size, data, &eaten);
127 if (eaten)
128 data = NULL;
129 }
130 free(data);
131 }
132 free(entries);
133
134 return err;
135 }
136
137 int verify_pack_index(struct packed_git *p)
138 {
139 off_t index_size;
140 const unsigned char *index_base;
141 git_SHA_CTX ctx;
142 unsigned char sha1[20];
143 int err = 0;
144
145 if (open_pack_index(p))
146 return error("packfile %s index not opened", p->pack_name);
147 index_size = p->index_size;
148 index_base = p->index_data;
149
150 /* Verify SHA1 sum of the index file */
151 git_SHA1_Init(&ctx);
152 git_SHA1_Update(&ctx, index_base, (unsigned int)(index_size - 20));
153 git_SHA1_Final(sha1, &ctx);
154 if (hashcmp(sha1, index_base + index_size - 20))
155 err = error("Packfile index for %s SHA1 mismatch",
156 p->pack_name);
157 return err;
158 }
159
160 int verify_pack(struct packed_git *p, verify_fn fn)
161 {
162 int err = 0;
163 struct pack_window *w_curs = NULL;
164
165 err |= verify_pack_index(p);
166 if (!p->index_data)
167 return -1;
168
169 err |= verify_packfile(p, &w_curs, fn);
170 unuse_pack(&w_curs);
171
172 return err;
173 }