t4200: use cut instead of sed
[git/peff.git] / pack-revindex.c
bloba8aa2cd6caefe7d37febdf5c3426cec043492b19
1 #include "cache.h"
2 #include "pack-revindex.h"
4 /*
5 * Pack index for existing packs give us easy access to the offsets into
6 * corresponding pack file where each object's data starts, but the entries
7 * do not store the size of the compressed representation (uncompressed
8 * size is easily available by examining the pack entry header). It is
9 * also rather expensive to find the sha1 for an object given its offset.
11 * We build a hashtable of existing packs (pack_revindex), and keep reverse
12 * index here -- pack index file is sorted by object name mapping to offset;
13 * this pack_revindex[].revindex array is a list of offset/index_nr pairs
14 * ordered by offset, so if you know the offset of an object, next offset
15 * is where its packed representation ends and the index_nr can be used to
16 * get the object sha1 from the main index.
19 struct pack_revindex {
20 struct packed_git *p;
21 struct revindex_entry *revindex;
24 static struct pack_revindex *pack_revindex;
25 static int pack_revindex_hashsz;
27 static int pack_revindex_ix(struct packed_git *p)
29 unsigned long ui = (unsigned long)p;
30 int i;
32 ui = ui ^ (ui >> 16); /* defeat structure alignment */
33 i = (int)(ui % pack_revindex_hashsz);
34 while (pack_revindex[i].p) {
35 if (pack_revindex[i].p == p)
36 return i;
37 if (++i == pack_revindex_hashsz)
38 i = 0;
40 return -1 - i;
43 void init_pack_revindex(void)
45 int num;
46 struct packed_git *p;
48 for (num = 0, p = packed_git; p; p = p->next)
49 num++;
50 if (!num)
51 return;
52 pack_revindex_hashsz = num * 11;
53 pack_revindex = xcalloc(sizeof(*pack_revindex), pack_revindex_hashsz);
54 for (p = packed_git; p; p = p->next) {
55 num = pack_revindex_ix(p);
56 num = - 1 - num;
57 pack_revindex[num].p = p;
59 /* revindex elements are lazily initialized */
62 static int cmp_offset(const void *a_, const void *b_)
64 const struct revindex_entry *a = a_;
65 const struct revindex_entry *b = b_;
66 return (a->offset < b->offset) ? -1 : (a->offset > b->offset) ? 1 : 0;
70 * Ordered list of offsets of objects in the pack.
72 static void create_pack_revindex(struct pack_revindex *rix)
74 struct packed_git *p = rix->p;
75 int num_ent = p->num_objects;
76 int i;
77 const char *index = p->index_data;
79 rix->revindex = xmalloc(sizeof(*rix->revindex) * (num_ent + 1));
80 index += 4 * 256;
82 if (p->index_version > 1) {
83 const uint32_t *off_32 =
84 (uint32_t *)(index + 8 + p->num_objects * (20 + 4));
85 const uint32_t *off_64 = off_32 + p->num_objects;
86 for (i = 0; i < num_ent; i++) {
87 uint32_t off = ntohl(*off_32++);
88 if (!(off & 0x80000000)) {
89 rix->revindex[i].offset = off;
90 } else {
91 rix->revindex[i].offset =
92 ((uint64_t)ntohl(*off_64++)) << 32;
93 rix->revindex[i].offset |=
94 ntohl(*off_64++);
96 rix->revindex[i].nr = i;
98 } else {
99 for (i = 0; i < num_ent; i++) {
100 uint32_t hl = *((uint32_t *)(index + 24 * i));
101 rix->revindex[i].offset = ntohl(hl);
102 rix->revindex[i].nr = i;
106 /* This knows the pack format -- the 20-byte trailer
107 * follows immediately after the last object data.
109 rix->revindex[num_ent].offset = p->pack_size - 20;
110 rix->revindex[num_ent].nr = -1;
111 qsort(rix->revindex, num_ent, sizeof(*rix->revindex), cmp_offset);
114 struct revindex_entry *find_pack_revindex(struct packed_git *p, off_t ofs)
116 int num;
117 int lo, hi;
118 struct pack_revindex *rix;
119 struct revindex_entry *revindex;
121 num = pack_revindex_ix(p);
122 if (num < 0)
123 die("internal error: pack revindex uninitialized");
125 rix = &pack_revindex[num];
126 if (!rix->revindex)
127 create_pack_revindex(rix);
128 revindex = rix->revindex;
130 lo = 0;
131 hi = p->num_objects + 1;
132 do {
133 int mi = (lo + hi) / 2;
134 if (revindex[mi].offset == ofs) {
135 return revindex + mi;
136 } else if (ofs < revindex[mi].offset)
137 hi = mi;
138 else
139 lo = mi + 1;
140 } while (lo < hi);
141 die("internal error: pack revindex corrupt");