pretty.c: capture invalid trailer argument
[git/debian.git] / pack-revindex.c
blob5e69bc7372f5476429fb11c53d5b4bd24390798c
1 #include "cache.h"
2 #include "pack-revindex.h"
3 #include "object-store.h"
4 #include "packfile.h"
6 struct revindex_entry {
7 off_t offset;
8 unsigned int nr;
9 };
12 * Pack index for existing packs give us easy access to the offsets into
13 * corresponding pack file where each object's data starts, but the entries
14 * do not store the size of the compressed representation (uncompressed
15 * size is easily available by examining the pack entry header). It is
16 * also rather expensive to find the sha1 for an object given its offset.
18 * The pack index file is sorted by object name mapping to offset;
19 * this revindex array is a list of offset/index_nr pairs
20 * ordered by offset, so if you know the offset of an object, next offset
21 * is where its packed representation ends and the index_nr can be used to
22 * get the object sha1 from the main index.
26 * This is a least-significant-digit radix sort.
28 * It sorts each of the "n" items in "entries" by its offset field. The "max"
29 * parameter must be at least as large as the largest offset in the array,
30 * and lets us quit the sort early.
32 static void sort_revindex(struct revindex_entry *entries, unsigned n, off_t max)
35 * We use a "digit" size of 16 bits. That keeps our memory
36 * usage reasonable, and we can generally (for a 4G or smaller
37 * packfile) quit after two rounds of radix-sorting.
39 #define DIGIT_SIZE (16)
40 #define BUCKETS (1 << DIGIT_SIZE)
42 * We want to know the bucket that a[i] will go into when we are using
43 * the digit that is N bits from the (least significant) end.
45 #define BUCKET_FOR(a, i, bits) (((a)[(i)].offset >> (bits)) & (BUCKETS-1))
48 * We need O(n) temporary storage. Rather than do an extra copy of the
49 * partial results into "entries", we sort back and forth between the
50 * real array and temporary storage. In each iteration of the loop, we
51 * keep track of them with alias pointers, always sorting from "from"
52 * to "to".
54 struct revindex_entry *tmp, *from, *to;
55 int bits;
56 unsigned *pos;
58 ALLOC_ARRAY(pos, BUCKETS);
59 ALLOC_ARRAY(tmp, n);
60 from = entries;
61 to = tmp;
64 * If (max >> bits) is zero, then we know that the radix digit we are
65 * on (and any higher) will be zero for all entries, and our loop will
66 * be a no-op, as everybody lands in the same zero-th bucket.
68 for (bits = 0; max >> bits; bits += DIGIT_SIZE) {
69 unsigned i;
71 memset(pos, 0, BUCKETS * sizeof(*pos));
74 * We want pos[i] to store the index of the last element that
75 * will go in bucket "i" (actually one past the last element).
76 * To do this, we first count the items that will go in each
77 * bucket, which gives us a relative offset from the last
78 * bucket. We can then cumulatively add the index from the
79 * previous bucket to get the true index.
81 for (i = 0; i < n; i++)
82 pos[BUCKET_FOR(from, i, bits)]++;
83 for (i = 1; i < BUCKETS; i++)
84 pos[i] += pos[i-1];
87 * Now we can drop the elements into their correct buckets (in
88 * our temporary array). We iterate the pos counter backwards
89 * to avoid using an extra index to count up. And since we are
90 * going backwards there, we must also go backwards through the
91 * array itself, to keep the sort stable.
93 * Note that we use an unsigned iterator to make sure we can
94 * handle 2^32-1 objects, even on a 32-bit system. But this
95 * means we cannot use the more obvious "i >= 0" loop condition
96 * for counting backwards, and must instead check for
97 * wrap-around with UINT_MAX.
99 for (i = n - 1; i != UINT_MAX; i--)
100 to[--pos[BUCKET_FOR(from, i, bits)]] = from[i];
103 * Now "to" contains the most sorted list, so we swap "from" and
104 * "to" for the next iteration.
106 SWAP(from, to);
110 * If we ended with our data in the original array, great. If not,
111 * we have to move it back from the temporary storage.
113 if (from != entries)
114 COPY_ARRAY(entries, tmp, n);
115 free(tmp);
116 free(pos);
118 #undef BUCKET_FOR
119 #undef BUCKETS
120 #undef DIGIT_SIZE
124 * Ordered list of offsets of objects in the pack.
126 static void create_pack_revindex(struct packed_git *p)
128 const unsigned num_ent = p->num_objects;
129 unsigned i;
130 const char *index = p->index_data;
131 const unsigned hashsz = the_hash_algo->rawsz;
133 ALLOC_ARRAY(p->revindex, num_ent + 1);
134 index += 4 * 256;
136 if (p->index_version > 1) {
137 const uint32_t *off_32 =
138 (uint32_t *)(index + 8 + (size_t)p->num_objects * (hashsz + 4));
139 const uint32_t *off_64 = off_32 + p->num_objects;
140 for (i = 0; i < num_ent; i++) {
141 const uint32_t off = ntohl(*off_32++);
142 if (!(off & 0x80000000)) {
143 p->revindex[i].offset = off;
144 } else {
145 p->revindex[i].offset = get_be64(off_64);
146 off_64 += 2;
148 p->revindex[i].nr = i;
150 } else {
151 for (i = 0; i < num_ent; i++) {
152 const uint32_t hl = *((uint32_t *)(index + (hashsz + 4) * i));
153 p->revindex[i].offset = ntohl(hl);
154 p->revindex[i].nr = i;
159 * This knows the pack format -- the hash trailer
160 * follows immediately after the last object data.
162 p->revindex[num_ent].offset = p->pack_size - hashsz;
163 p->revindex[num_ent].nr = -1;
164 sort_revindex(p->revindex, num_ent, p->pack_size);
167 int load_pack_revindex(struct packed_git *p)
169 if (!p->revindex) {
170 if (open_pack_index(p))
171 return -1;
172 create_pack_revindex(p);
174 return 0;
177 int offset_to_pack_pos(struct packed_git *p, off_t ofs, uint32_t *pos)
179 unsigned lo, hi;
181 if (load_pack_revindex(p) < 0)
182 return -1;
184 lo = 0;
185 hi = p->num_objects + 1;
187 do {
188 const unsigned mi = lo + (hi - lo) / 2;
189 off_t got = pack_pos_to_offset(p, mi);
191 if (got == ofs) {
192 *pos = mi;
193 return 0;
194 } else if (ofs < got)
195 hi = mi;
196 else
197 lo = mi + 1;
198 } while (lo < hi);
200 error("bad offset for revindex");
201 return -1;
204 uint32_t pack_pos_to_index(struct packed_git *p, uint32_t pos)
206 if (!p->revindex)
207 BUG("pack_pos_to_index: reverse index not yet loaded");
208 if (p->num_objects <= pos)
209 BUG("pack_pos_to_index: out-of-bounds object at %"PRIu32, pos);
210 return p->revindex[pos].nr;
213 off_t pack_pos_to_offset(struct packed_git *p, uint32_t pos)
215 if (!p->revindex)
216 BUG("pack_pos_to_index: reverse index not yet loaded");
217 if (p->num_objects < pos)
218 BUG("pack_pos_to_offset: out-of-bounds object at %"PRIu32, pos);
219 return p->revindex[pos].offset;