1#include"cache.h" 2#include"pack-revindex.h" 3 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. 10 * 11 * The pack index file is sorted by object name mapping to offset; 12 * this revindex array is a list of offset/index_nr pairs 13 * ordered by offset, so if you know the offset of an object, next offset 14 * is where its packed representation ends and the index_nr can be used to 15 * get the object sha1 from the main index. 16 */ 17 18/* 19 * This is a least-significant-digit radix sort. 20 * 21 * It sorts each of the "n" items in "entries" by its offset field. The "max" 22 * parameter must be at least as large as the largest offset in the array, 23 * and lets us quit the sort early. 24 */ 25static voidsort_revindex(struct revindex_entry *entries,unsigned n, off_t max) 26{ 27/* 28 * We use a "digit" size of 16 bits. That keeps our memory 29 * usage reasonable, and we can generally (for a 4G or smaller 30 * packfile) quit after two rounds of radix-sorting. 31 */ 32#define DIGIT_SIZE (16) 33#define BUCKETS (1 << DIGIT_SIZE) 34/* 35 * We want to know the bucket that a[i] will go into when we are using 36 * the digit that is N bits from the (least significant) end. 37 */ 38#define BUCKET_FOR(a, i, bits) (((a)[(i)].offset >> (bits)) & (BUCKETS-1)) 39 40/* 41 * We need O(n) temporary storage. Rather than do an extra copy of the 42 * partial results into "entries", we sort back and forth between the 43 * real array and temporary storage. In each iteration of the loop, we 44 * keep track of them with alias pointers, always sorting from "from" 45 * to "to". 46 */ 47struct revindex_entry *tmp =xmalloc(n *sizeof(*tmp)); 48struct revindex_entry *from = entries, *to = tmp; 49int bits; 50unsigned*pos =xmalloc(BUCKETS *sizeof(*pos)); 51 52/* 53 * If (max >> bits) is zero, then we know that the radix digit we are 54 * on (and any higher) will be zero for all entries, and our loop will 55 * be a no-op, as everybody lands in the same zero-th bucket. 56 */ 57for(bits =0; max >> bits; bits += DIGIT_SIZE) { 58struct revindex_entry *swap; 59unsigned i; 60 61memset(pos,0, BUCKETS *sizeof(*pos)); 62 63/* 64 * We want pos[i] to store the index of the last element that 65 * will go in bucket "i" (actually one past the last element). 66 * To do this, we first count the items that will go in each 67 * bucket, which gives us a relative offset from the last 68 * bucket. We can then cumulatively add the index from the 69 * previous bucket to get the true index. 70 */ 71for(i =0; i < n; i++) 72 pos[BUCKET_FOR(from, i, bits)]++; 73for(i =1; i < BUCKETS; i++) 74 pos[i] += pos[i-1]; 75 76/* 77 * Now we can drop the elements into their correct buckets (in 78 * our temporary array). We iterate the pos counter backwards 79 * to avoid using an extra index to count up. And since we are 80 * going backwards there, we must also go backwards through the 81 * array itself, to keep the sort stable. 82 * 83 * Note that we use an unsigned iterator to make sure we can 84 * handle 2^32-1 objects, even on a 32-bit system. But this 85 * means we cannot use the more obvious "i >= 0" loop condition 86 * for counting backwards, and must instead check for 87 * wrap-around with UINT_MAX. 88 */ 89for(i = n -1; i != UINT_MAX; i--) 90 to[--pos[BUCKET_FOR(from, i, bits)]] = from[i]; 91 92/* 93 * Now "to" contains the most sorted list, so we swap "from" and 94 * "to" for the next iteration. 95 */ 96 swap = from; 97 from = to; 98 to = swap; 99} 100 101/* 102 * If we ended with our data in the original array, great. If not, 103 * we have to move it back from the temporary storage. 104 */ 105if(from != entries) 106memcpy(entries, tmp, n *sizeof(*entries)); 107free(tmp); 108free(pos); 109 110#undef BUCKET_FOR 111#undef BUCKETS 112#undef DIGIT_SIZE 113} 114 115/* 116 * Ordered list of offsets of objects in the pack. 117 */ 118static voidcreate_pack_revindex(struct packed_git *p) 119{ 120unsigned num_ent = p->num_objects; 121unsigned i; 122const char*index = p->index_data; 123 124 p->revindex =xmalloc(sizeof(*p->revindex) * (num_ent +1)); 125 index +=4*256; 126 127if(p->index_version >1) { 128const uint32_t*off_32 = 129(uint32_t*)(index +8+ p->num_objects * (20+4)); 130const uint32_t*off_64 = off_32 + p->num_objects; 131for(i =0; i < num_ent; i++) { 132uint32_t off =ntohl(*off_32++); 133if(!(off &0x80000000)) { 134 p->revindex[i].offset = off; 135}else{ 136 p->revindex[i].offset = 137((uint64_t)ntohl(*off_64++)) <<32; 138 p->revindex[i].offset |= 139ntohl(*off_64++); 140} 141 p->revindex[i].nr = i; 142} 143}else{ 144for(i =0; i < num_ent; i++) { 145uint32_t hl = *((uint32_t*)(index +24* i)); 146 p->revindex[i].offset =ntohl(hl); 147 p->revindex[i].nr = i; 148} 149} 150 151/* This knows the pack format -- the 20-byte trailer 152 * follows immediately after the last object data. 153 */ 154 p->revindex[num_ent].offset = p->pack_size -20; 155 p->revindex[num_ent].nr = -1; 156sort_revindex(p->revindex, num_ent, p->pack_size); 157} 158 159voidload_pack_revindex(struct packed_git *p) 160{ 161if(!p->revindex) 162create_pack_revindex(p); 163} 164 165intfind_revindex_position(struct packed_git *p, off_t ofs) 166{ 167int lo =0; 168int hi = p->num_objects +1; 169struct revindex_entry *revindex = p->revindex; 170 171do{ 172unsigned mi = lo + (hi - lo) /2; 173if(revindex[mi].offset == ofs) { 174return mi; 175}else if(ofs < revindex[mi].offset) 176 hi = mi; 177else 178 lo = mi +1; 179}while(lo < hi); 180 181error("bad offset for revindex"); 182return-1; 183} 184 185struct revindex_entry *find_pack_revindex(struct packed_git *p, off_t ofs) 186{ 187int pos; 188 189load_pack_revindex(p); 190 pos =find_revindex_position(p, ofs); 191 192if(pos <0) 193return NULL; 194 195return p->revindex + pos; 196}