Elijah Newren | b6fdc44 | 2023-04-11 00:41:54 -0700 | [diff] [blame] | 1 | #include "git-compat-util.h" |
Elijah Newren | f394e09 | 2023-03-21 06:25:54 +0000 | [diff] [blame] | 2 | #include "gettext.h" |
Nicolas Pitre | 3449f8c | 2008-02-28 00:25:17 -0500 | [diff] [blame] | 3 | #include "pack-revindex.h" |
Elijah Newren | 87bed17 | 2023-04-11 00:41:53 -0700 | [diff] [blame] | 4 | #include "object-file.h" |
Stefan Beller | a80d72d | 2018-03-23 18:20:59 +0100 | [diff] [blame] | 5 | #include "object-store.h" |
Jeff King | 4828ce9 | 2019-04-05 14:04:24 -0400 | [diff] [blame] | 6 | #include "packfile.h" |
Elijah Newren | 74ea5c9 | 2023-04-11 03:00:38 +0000 | [diff] [blame] | 7 | #include "trace2.h" |
Taylor Blau | ec8e776 | 2021-01-25 18:37:46 -0500 | [diff] [blame] | 8 | #include "config.h" |
Taylor Blau | f894081 | 2021-03-30 11:04:26 -0400 | [diff] [blame] | 9 | #include "midx.h" |
Nicolas Pitre | 3449f8c | 2008-02-28 00:25:17 -0500 | [diff] [blame] | 10 | |
Taylor Blau | d5bc7c6 | 2021-01-13 17:25:06 -0500 | [diff] [blame] | 11 | struct revindex_entry { |
| 12 | off_t offset; |
| 13 | unsigned int nr; |
| 14 | }; |
| 15 | |
Nicolas Pitre | 3449f8c | 2008-02-28 00:25:17 -0500 | [diff] [blame] | 16 | /* |
| 17 | * Pack index for existing packs give us easy access to the offsets into |
| 18 | * corresponding pack file where each object's data starts, but the entries |
| 19 | * do not store the size of the compressed representation (uncompressed |
| 20 | * size is easily available by examining the pack entry header). It is |
| 21 | * also rather expensive to find the sha1 for an object given its offset. |
| 22 | * |
Jeff King | f401533 | 2015-12-21 01:19:49 -0500 | [diff] [blame] | 23 | * The pack index file is sorted by object name mapping to offset; |
| 24 | * this revindex array is a list of offset/index_nr pairs |
Nicolas Pitre | 3449f8c | 2008-02-28 00:25:17 -0500 | [diff] [blame] | 25 | * ordered by offset, so if you know the offset of an object, next offset |
| 26 | * is where its packed representation ends and the index_nr can be used to |
| 27 | * get the object sha1 from the main index. |
| 28 | */ |
| 29 | |
Jeff King | 8b8dfd5 | 2013-07-11 08:16:00 -0400 | [diff] [blame] | 30 | /* |
| 31 | * This is a least-significant-digit radix sort. |
| 32 | * |
| 33 | * It sorts each of the "n" items in "entries" by its offset field. The "max" |
| 34 | * parameter must be at least as large as the largest offset in the array, |
| 35 | * and lets us quit the sort early. |
| 36 | */ |
| 37 | static void sort_revindex(struct revindex_entry *entries, unsigned n, off_t max) |
Nicolas Pitre | 3449f8c | 2008-02-28 00:25:17 -0500 | [diff] [blame] | 38 | { |
Jeff King | 8b8dfd5 | 2013-07-11 08:16:00 -0400 | [diff] [blame] | 39 | /* |
| 40 | * We use a "digit" size of 16 bits. That keeps our memory |
| 41 | * usage reasonable, and we can generally (for a 4G or smaller |
| 42 | * packfile) quit after two rounds of radix-sorting. |
| 43 | */ |
| 44 | #define DIGIT_SIZE (16) |
| 45 | #define BUCKETS (1 << DIGIT_SIZE) |
| 46 | /* |
| 47 | * We want to know the bucket that a[i] will go into when we are using |
| 48 | * the digit that is N bits from the (least significant) end. |
| 49 | */ |
| 50 | #define BUCKET_FOR(a, i, bits) (((a)[(i)].offset >> (bits)) & (BUCKETS-1)) |
| 51 | |
| 52 | /* |
| 53 | * We need O(n) temporary storage. Rather than do an extra copy of the |
| 54 | * partial results into "entries", we sort back and forth between the |
| 55 | * real array and temporary storage. In each iteration of the loop, we |
| 56 | * keep track of them with alias pointers, always sorting from "from" |
| 57 | * to "to". |
| 58 | */ |
Jeff King | b32fa95 | 2016-02-22 17:44:25 -0500 | [diff] [blame] | 59 | struct revindex_entry *tmp, *from, *to; |
Jeff King | 8b8dfd5 | 2013-07-11 08:16:00 -0400 | [diff] [blame] | 60 | int bits; |
Jeff King | b32fa95 | 2016-02-22 17:44:25 -0500 | [diff] [blame] | 61 | unsigned *pos; |
| 62 | |
| 63 | ALLOC_ARRAY(pos, BUCKETS); |
| 64 | ALLOC_ARRAY(tmp, n); |
| 65 | from = entries; |
| 66 | to = tmp; |
Jeff King | 8b8dfd5 | 2013-07-11 08:16:00 -0400 | [diff] [blame] | 67 | |
| 68 | /* |
| 69 | * If (max >> bits) is zero, then we know that the radix digit we are |
| 70 | * on (and any higher) will be zero for all entries, and our loop will |
| 71 | * be a no-op, as everybody lands in the same zero-th bucket. |
| 72 | */ |
| 73 | for (bits = 0; max >> bits; bits += DIGIT_SIZE) { |
Jeff King | 8b8dfd5 | 2013-07-11 08:16:00 -0400 | [diff] [blame] | 74 | unsigned i; |
| 75 | |
| 76 | memset(pos, 0, BUCKETS * sizeof(*pos)); |
| 77 | |
| 78 | /* |
| 79 | * We want pos[i] to store the index of the last element that |
| 80 | * will go in bucket "i" (actually one past the last element). |
| 81 | * To do this, we first count the items that will go in each |
| 82 | * bucket, which gives us a relative offset from the last |
| 83 | * bucket. We can then cumulatively add the index from the |
| 84 | * previous bucket to get the true index. |
| 85 | */ |
| 86 | for (i = 0; i < n; i++) |
| 87 | pos[BUCKET_FOR(from, i, bits)]++; |
| 88 | for (i = 1; i < BUCKETS; i++) |
| 89 | pos[i] += pos[i-1]; |
| 90 | |
| 91 | /* |
| 92 | * Now we can drop the elements into their correct buckets (in |
| 93 | * our temporary array). We iterate the pos counter backwards |
| 94 | * to avoid using an extra index to count up. And since we are |
| 95 | * going backwards there, we must also go backwards through the |
| 96 | * array itself, to keep the sort stable. |
| 97 | * |
| 98 | * Note that we use an unsigned iterator to make sure we can |
| 99 | * handle 2^32-1 objects, even on a 32-bit system. But this |
| 100 | * means we cannot use the more obvious "i >= 0" loop condition |
| 101 | * for counting backwards, and must instead check for |
| 102 | * wrap-around with UINT_MAX. |
| 103 | */ |
| 104 | for (i = n - 1; i != UINT_MAX; i--) |
| 105 | to[--pos[BUCKET_FOR(from, i, bits)]] = from[i]; |
| 106 | |
| 107 | /* |
| 108 | * Now "to" contains the most sorted list, so we swap "from" and |
| 109 | * "to" for the next iteration. |
| 110 | */ |
René Scharfe | 35d803b | 2017-01-28 22:40:58 +0100 | [diff] [blame] | 111 | SWAP(from, to); |
Jeff King | 8b8dfd5 | 2013-07-11 08:16:00 -0400 | [diff] [blame] | 112 | } |
| 113 | |
| 114 | /* |
| 115 | * If we ended with our data in the original array, great. If not, |
| 116 | * we have to move it back from the temporary storage. |
| 117 | */ |
| 118 | if (from != entries) |
René Scharfe | 45ccef8 | 2016-09-25 09:24:03 +0200 | [diff] [blame] | 119 | COPY_ARRAY(entries, tmp, n); |
Jeff King | 8b8dfd5 | 2013-07-11 08:16:00 -0400 | [diff] [blame] | 120 | free(tmp); |
| 121 | free(pos); |
| 122 | |
| 123 | #undef BUCKET_FOR |
| 124 | #undef BUCKETS |
| 125 | #undef DIGIT_SIZE |
Nicolas Pitre | 3449f8c | 2008-02-28 00:25:17 -0500 | [diff] [blame] | 126 | } |
| 127 | |
| 128 | /* |
| 129 | * Ordered list of offsets of objects in the pack. |
| 130 | */ |
Jeff King | 9d98bbf | 2015-12-21 01:20:33 -0500 | [diff] [blame] | 131 | static void create_pack_revindex(struct packed_git *p) |
Nicolas Pitre | 3449f8c | 2008-02-28 00:25:17 -0500 | [diff] [blame] | 132 | { |
Shahzad Lone | 33de80b | 2019-02-02 23:16:45 +0000 | [diff] [blame] | 133 | const unsigned num_ent = p->num_objects; |
Jeff King | 012b32b | 2013-07-10 07:50:26 -0400 | [diff] [blame] | 134 | unsigned i; |
Nicolas Pitre | 3449f8c | 2008-02-28 00:25:17 -0500 | [diff] [blame] | 135 | const char *index = p->index_data; |
brian m. carlson | fa13080 | 2018-10-15 00:01:53 +0000 | [diff] [blame] | 136 | const unsigned hashsz = the_hash_algo->rawsz; |
Nicolas Pitre | 3449f8c | 2008-02-28 00:25:17 -0500 | [diff] [blame] | 137 | |
Junio C Hamano | 11529ec | 2016-02-26 13:37:16 -0800 | [diff] [blame] | 138 | ALLOC_ARRAY(p->revindex, num_ent + 1); |
Nicolas Pitre | 3449f8c | 2008-02-28 00:25:17 -0500 | [diff] [blame] | 139 | index += 4 * 256; |
| 140 | |
| 141 | if (p->index_version > 1) { |
| 142 | const uint32_t *off_32 = |
Jeff King | f86f769 | 2020-11-13 00:06:48 -0500 | [diff] [blame] | 143 | (uint32_t *)(index + 8 + (size_t)p->num_objects * (hashsz + 4)); |
Nicolas Pitre | 3449f8c | 2008-02-28 00:25:17 -0500 | [diff] [blame] | 144 | const uint32_t *off_64 = off_32 + p->num_objects; |
| 145 | for (i = 0; i < num_ent; i++) { |
Shahzad Lone | 33de80b | 2019-02-02 23:16:45 +0000 | [diff] [blame] | 146 | const uint32_t off = ntohl(*off_32++); |
Nicolas Pitre | 3449f8c | 2008-02-28 00:25:17 -0500 | [diff] [blame] | 147 | if (!(off & 0x80000000)) { |
Jeff King | 9d98bbf | 2015-12-21 01:20:33 -0500 | [diff] [blame] | 148 | p->revindex[i].offset = off; |
Nicolas Pitre | 3449f8c | 2008-02-28 00:25:17 -0500 | [diff] [blame] | 149 | } else { |
Derrick Stolee | ad622a2 | 2018-01-17 14:08:23 -0500 | [diff] [blame] | 150 | p->revindex[i].offset = get_be64(off_64); |
| 151 | off_64 += 2; |
Nicolas Pitre | 3449f8c | 2008-02-28 00:25:17 -0500 | [diff] [blame] | 152 | } |
Jeff King | 9d98bbf | 2015-12-21 01:20:33 -0500 | [diff] [blame] | 153 | p->revindex[i].nr = i; |
Nicolas Pitre | 3449f8c | 2008-02-28 00:25:17 -0500 | [diff] [blame] | 154 | } |
| 155 | } else { |
| 156 | for (i = 0; i < num_ent; i++) { |
Shahzad Lone | 33de80b | 2019-02-02 23:16:45 +0000 | [diff] [blame] | 157 | const uint32_t hl = *((uint32_t *)(index + (hashsz + 4) * i)); |
Jeff King | 9d98bbf | 2015-12-21 01:20:33 -0500 | [diff] [blame] | 158 | p->revindex[i].offset = ntohl(hl); |
| 159 | p->revindex[i].nr = i; |
Nicolas Pitre | 3449f8c | 2008-02-28 00:25:17 -0500 | [diff] [blame] | 160 | } |
| 161 | } |
| 162 | |
brian m. carlson | fa13080 | 2018-10-15 00:01:53 +0000 | [diff] [blame] | 163 | /* |
| 164 | * This knows the pack format -- the hash trailer |
Nicolas Pitre | 3449f8c | 2008-02-28 00:25:17 -0500 | [diff] [blame] | 165 | * follows immediately after the last object data. |
| 166 | */ |
brian m. carlson | fa13080 | 2018-10-15 00:01:53 +0000 | [diff] [blame] | 167 | p->revindex[num_ent].offset = p->pack_size - hashsz; |
Jeff King | 9d98bbf | 2015-12-21 01:20:33 -0500 | [diff] [blame] | 168 | p->revindex[num_ent].nr = -1; |
| 169 | sort_revindex(p->revindex, num_ent, p->pack_size); |
Nicolas Pitre | 3449f8c | 2008-02-28 00:25:17 -0500 | [diff] [blame] | 170 | } |
| 171 | |
Taylor Blau | 2f4ba2a | 2021-01-25 18:37:14 -0500 | [diff] [blame] | 172 | static int create_pack_revindex_in_memory(struct packed_git *p) |
| 173 | { |
Taylor Blau | ec8e776 | 2021-01-25 18:37:46 -0500 | [diff] [blame] | 174 | if (git_env_bool(GIT_TEST_REV_INDEX_DIE_IN_MEMORY, 0)) |
| 175 | die("dying as requested by '%s'", |
| 176 | GIT_TEST_REV_INDEX_DIE_IN_MEMORY); |
Taylor Blau | 2f4ba2a | 2021-01-25 18:37:14 -0500 | [diff] [blame] | 177 | if (open_pack_index(p)) |
| 178 | return -1; |
| 179 | create_pack_revindex(p); |
| 180 | return 0; |
| 181 | } |
| 182 | |
| 183 | static char *pack_revindex_filename(struct packed_git *p) |
| 184 | { |
| 185 | size_t len; |
| 186 | if (!strip_suffix(p->pack_name, ".pack", &len)) |
| 187 | BUG("pack_name does not end in .pack"); |
| 188 | return xstrfmt("%.*s.rev", (int)len, p->pack_name); |
| 189 | } |
| 190 | |
| 191 | #define RIDX_HEADER_SIZE (12) |
| 192 | #define RIDX_MIN_SIZE (RIDX_HEADER_SIZE + (2 * the_hash_algo->rawsz)) |
| 193 | |
| 194 | struct revindex_header { |
| 195 | uint32_t signature; |
| 196 | uint32_t version; |
| 197 | uint32_t hash_id; |
| 198 | }; |
| 199 | |
| 200 | static int load_revindex_from_disk(char *revindex_name, |
| 201 | uint32_t num_objects, |
| 202 | const uint32_t **data_p, size_t *len_p) |
| 203 | { |
| 204 | int fd, ret = 0; |
| 205 | struct stat st; |
| 206 | void *data = NULL; |
| 207 | size_t revindex_size; |
| 208 | struct revindex_header *hdr; |
| 209 | |
| 210 | fd = git_open(revindex_name); |
| 211 | |
| 212 | if (fd < 0) { |
| 213 | ret = -1; |
| 214 | goto cleanup; |
| 215 | } |
| 216 | if (fstat(fd, &st)) { |
| 217 | ret = error_errno(_("failed to read %s"), revindex_name); |
| 218 | goto cleanup; |
| 219 | } |
| 220 | |
| 221 | revindex_size = xsize_t(st.st_size); |
| 222 | |
| 223 | if (revindex_size < RIDX_MIN_SIZE) { |
| 224 | ret = error(_("reverse-index file %s is too small"), revindex_name); |
| 225 | goto cleanup; |
| 226 | } |
| 227 | |
| 228 | if (revindex_size - RIDX_MIN_SIZE != st_mult(sizeof(uint32_t), num_objects)) { |
| 229 | ret = error(_("reverse-index file %s is corrupt"), revindex_name); |
| 230 | goto cleanup; |
| 231 | } |
| 232 | |
| 233 | data = xmmap(NULL, revindex_size, PROT_READ, MAP_PRIVATE, fd, 0); |
| 234 | hdr = data; |
| 235 | |
| 236 | if (ntohl(hdr->signature) != RIDX_SIGNATURE) { |
| 237 | ret = error(_("reverse-index file %s has unknown signature"), revindex_name); |
| 238 | goto cleanup; |
| 239 | } |
| 240 | if (ntohl(hdr->version) != 1) { |
| 241 | ret = error(_("reverse-index file %s has unsupported version %"PRIu32), |
| 242 | revindex_name, ntohl(hdr->version)); |
| 243 | goto cleanup; |
| 244 | } |
| 245 | if (!(ntohl(hdr->hash_id) == 1 || ntohl(hdr->hash_id) == 2)) { |
| 246 | ret = error(_("reverse-index file %s has unsupported hash id %"PRIu32), |
| 247 | revindex_name, ntohl(hdr->hash_id)); |
| 248 | goto cleanup; |
| 249 | } |
| 250 | |
| 251 | cleanup: |
| 252 | if (ret) { |
| 253 | if (data) |
| 254 | munmap(data, revindex_size); |
| 255 | } else { |
| 256 | *len_p = revindex_size; |
| 257 | *data_p = (const uint32_t *)data; |
| 258 | } |
| 259 | |
Taylor Blau | 66f52fa | 2021-02-26 11:31:02 -0500 | [diff] [blame] | 260 | if (fd >= 0) |
| 261 | close(fd); |
Taylor Blau | 2f4ba2a | 2021-01-25 18:37:14 -0500 | [diff] [blame] | 262 | return ret; |
| 263 | } |
| 264 | |
| 265 | static int load_pack_revindex_from_disk(struct packed_git *p) |
| 266 | { |
| 267 | char *revindex_name; |
| 268 | int ret; |
| 269 | if (open_pack_index(p)) |
| 270 | return -1; |
| 271 | |
| 272 | revindex_name = pack_revindex_filename(p); |
| 273 | |
| 274 | ret = load_revindex_from_disk(revindex_name, |
| 275 | p->num_objects, |
| 276 | &p->revindex_map, |
| 277 | &p->revindex_size); |
| 278 | if (ret) |
| 279 | goto cleanup; |
| 280 | |
| 281 | p->revindex_data = (const uint32_t *)((const char *)p->revindex_map + RIDX_HEADER_SIZE); |
| 282 | |
| 283 | cleanup: |
| 284 | free(revindex_name); |
| 285 | return ret; |
| 286 | } |
| 287 | |
Jeff King | 4828ce9 | 2019-04-05 14:04:24 -0400 | [diff] [blame] | 288 | int load_pack_revindex(struct packed_git *p) |
Nicolas Pitre | 3449f8c | 2008-02-28 00:25:17 -0500 | [diff] [blame] | 289 | { |
Taylor Blau | 2f4ba2a | 2021-01-25 18:37:14 -0500 | [diff] [blame] | 290 | if (p->revindex || p->revindex_data) |
| 291 | return 0; |
| 292 | |
| 293 | if (!load_pack_revindex_from_disk(p)) |
| 294 | return 0; |
| 295 | else if (!create_pack_revindex_in_memory(p)) |
| 296 | return 0; |
| 297 | return -1; |
Vicent Marti | 92e5c77 | 2013-10-24 14:00:36 -0400 | [diff] [blame] | 298 | } |
| 299 | |
Taylor Blau | f894081 | 2021-03-30 11:04:26 -0400 | [diff] [blame] | 300 | int load_midx_revindex(struct multi_pack_index *m) |
| 301 | { |
Taylor Blau | 60980ae | 2021-10-26 17:01:21 -0400 | [diff] [blame] | 302 | struct strbuf revindex_name = STRBUF_INIT; |
Taylor Blau | f894081 | 2021-03-30 11:04:26 -0400 | [diff] [blame] | 303 | int ret; |
Taylor Blau | 7f514b7 | 2022-01-25 17:41:17 -0500 | [diff] [blame] | 304 | |
Taylor Blau | f894081 | 2021-03-30 11:04:26 -0400 | [diff] [blame] | 305 | if (m->revindex_data) |
| 306 | return 0; |
| 307 | |
Taylor Blau | 7f514b7 | 2022-01-25 17:41:17 -0500 | [diff] [blame] | 308 | if (m->chunk_revindex) { |
| 309 | /* |
| 310 | * If the MIDX `m` has a `RIDX` chunk, then use its contents for |
| 311 | * the reverse index instead of trying to load a separate `.rev` |
| 312 | * file. |
| 313 | * |
| 314 | * Note that we do *not* set `m->revindex_map` here, since we do |
| 315 | * not want to accidentally call munmap() in the middle of the |
| 316 | * MIDX. |
| 317 | */ |
| 318 | trace2_data_string("load_midx_revindex", the_repository, |
| 319 | "source", "midx"); |
| 320 | m->revindex_data = (const uint32_t *)m->chunk_revindex; |
| 321 | return 0; |
| 322 | } |
| 323 | |
Taylor Blau | 09a7799 | 2022-01-25 17:41:05 -0500 | [diff] [blame] | 324 | trace2_data_string("load_midx_revindex", the_repository, |
| 325 | "source", "rev"); |
| 326 | |
Taylor Blau | 60980ae | 2021-10-26 17:01:21 -0400 | [diff] [blame] | 327 | get_midx_rev_filename(&revindex_name, m); |
Taylor Blau | f894081 | 2021-03-30 11:04:26 -0400 | [diff] [blame] | 328 | |
Taylor Blau | 60980ae | 2021-10-26 17:01:21 -0400 | [diff] [blame] | 329 | ret = load_revindex_from_disk(revindex_name.buf, |
Taylor Blau | f894081 | 2021-03-30 11:04:26 -0400 | [diff] [blame] | 330 | m->num_objects, |
| 331 | &m->revindex_map, |
| 332 | &m->revindex_len); |
| 333 | if (ret) |
| 334 | goto cleanup; |
| 335 | |
| 336 | m->revindex_data = (const uint32_t *)((const char *)m->revindex_map + RIDX_HEADER_SIZE); |
| 337 | |
| 338 | cleanup: |
Taylor Blau | 60980ae | 2021-10-26 17:01:21 -0400 | [diff] [blame] | 339 | strbuf_release(&revindex_name); |
Taylor Blau | f894081 | 2021-03-30 11:04:26 -0400 | [diff] [blame] | 340 | return ret; |
| 341 | } |
| 342 | |
| 343 | int close_midx_revindex(struct multi_pack_index *m) |
| 344 | { |
| 345 | if (!m || !m->revindex_map) |
| 346 | return 0; |
| 347 | |
| 348 | munmap((void*)m->revindex_map, m->revindex_len); |
| 349 | |
| 350 | m->revindex_map = NULL; |
| 351 | m->revindex_data = NULL; |
| 352 | m->revindex_len = 0; |
| 353 | |
| 354 | return 0; |
| 355 | } |
| 356 | |
Taylor Blau | 8389855 | 2021-01-13 17:25:02 -0500 | [diff] [blame] | 357 | int offset_to_pack_pos(struct packed_git *p, off_t ofs, uint32_t *pos) |
Vicent Marti | 92e5c77 | 2013-10-24 14:00:36 -0400 | [diff] [blame] | 358 | { |
Taylor Blau | 8389855 | 2021-01-13 17:25:02 -0500 | [diff] [blame] | 359 | unsigned lo, hi; |
Taylor Blau | 8389855 | 2021-01-13 17:25:02 -0500 | [diff] [blame] | 360 | |
| 361 | if (load_pack_revindex(p) < 0) |
| 362 | return -1; |
| 363 | |
| 364 | lo = 0; |
| 365 | hi = p->num_objects + 1; |
Vicent Marti | 92e5c77 | 2013-10-24 14:00:36 -0400 | [diff] [blame] | 366 | |
Nicolas Pitre | 3449f8c | 2008-02-28 00:25:17 -0500 | [diff] [blame] | 367 | do { |
Shahzad Lone | 33de80b | 2019-02-02 23:16:45 +0000 | [diff] [blame] | 368 | const unsigned mi = lo + (hi - lo) / 2; |
Taylor Blau | e5dcd78 | 2021-01-13 17:25:10 -0500 | [diff] [blame] | 369 | off_t got = pack_pos_to_offset(p, mi); |
| 370 | |
| 371 | if (got == ofs) { |
Taylor Blau | 8389855 | 2021-01-13 17:25:02 -0500 | [diff] [blame] | 372 | *pos = mi; |
| 373 | return 0; |
Taylor Blau | e5dcd78 | 2021-01-13 17:25:10 -0500 | [diff] [blame] | 374 | } else if (ofs < got) |
Nicolas Pitre | 3449f8c | 2008-02-28 00:25:17 -0500 | [diff] [blame] | 375 | hi = mi; |
| 376 | else |
| 377 | lo = mi + 1; |
| 378 | } while (lo < hi); |
Vicent Marti | 92e5c77 | 2013-10-24 14:00:36 -0400 | [diff] [blame] | 379 | |
Nicolas Pitre | 08698b1 | 2008-10-29 19:02:49 -0400 | [diff] [blame] | 380 | error("bad offset for revindex"); |
Vicent Marti | 92e5c77 | 2013-10-24 14:00:36 -0400 | [diff] [blame] | 381 | return -1; |
| 382 | } |
| 383 | |
Taylor Blau | f33fb6e | 2021-01-13 17:23:31 -0500 | [diff] [blame] | 384 | uint32_t pack_pos_to_index(struct packed_git *p, uint32_t pos) |
| 385 | { |
Taylor Blau | 2f4ba2a | 2021-01-25 18:37:14 -0500 | [diff] [blame] | 386 | if (!(p->revindex || p->revindex_data)) |
Taylor Blau | f33fb6e | 2021-01-13 17:23:31 -0500 | [diff] [blame] | 387 | BUG("pack_pos_to_index: reverse index not yet loaded"); |
| 388 | if (p->num_objects <= pos) |
| 389 | BUG("pack_pos_to_index: out-of-bounds object at %"PRIu32, pos); |
Taylor Blau | 2f4ba2a | 2021-01-25 18:37:14 -0500 | [diff] [blame] | 390 | |
| 391 | if (p->revindex) |
| 392 | return p->revindex[pos].nr; |
| 393 | else |
| 394 | return get_be32(p->revindex_data + pos); |
Taylor Blau | f33fb6e | 2021-01-13 17:23:31 -0500 | [diff] [blame] | 395 | } |
| 396 | |
| 397 | off_t pack_pos_to_offset(struct packed_git *p, uint32_t pos) |
| 398 | { |
Taylor Blau | 2f4ba2a | 2021-01-25 18:37:14 -0500 | [diff] [blame] | 399 | if (!(p->revindex || p->revindex_data)) |
Taylor Blau | f33fb6e | 2021-01-13 17:23:31 -0500 | [diff] [blame] | 400 | BUG("pack_pos_to_index: reverse index not yet loaded"); |
| 401 | if (p->num_objects < pos) |
| 402 | BUG("pack_pos_to_offset: out-of-bounds object at %"PRIu32, pos); |
Taylor Blau | 2f4ba2a | 2021-01-25 18:37:14 -0500 | [diff] [blame] | 403 | |
| 404 | if (p->revindex) |
| 405 | return p->revindex[pos].offset; |
| 406 | else if (pos == p->num_objects) |
| 407 | return p->pack_size - the_hash_algo->rawsz; |
| 408 | else |
| 409 | return nth_packed_object_offset(p, pack_pos_to_index(p, pos)); |
Taylor Blau | f33fb6e | 2021-01-13 17:23:31 -0500 | [diff] [blame] | 410 | } |
Taylor Blau | f894081 | 2021-03-30 11:04:26 -0400 | [diff] [blame] | 411 | |
| 412 | uint32_t pack_pos_to_midx(struct multi_pack_index *m, uint32_t pos) |
| 413 | { |
| 414 | if (!m->revindex_data) |
| 415 | BUG("pack_pos_to_midx: reverse index not yet loaded"); |
| 416 | if (m->num_objects <= pos) |
| 417 | BUG("pack_pos_to_midx: out-of-bounds object at %"PRIu32, pos); |
| 418 | return get_be32(m->revindex_data + pos); |
| 419 | } |
| 420 | |
| 421 | struct midx_pack_key { |
| 422 | uint32_t pack; |
| 423 | off_t offset; |
| 424 | |
| 425 | uint32_t preferred_pack; |
| 426 | struct multi_pack_index *midx; |
| 427 | }; |
| 428 | |
| 429 | static int midx_pack_order_cmp(const void *va, const void *vb) |
| 430 | { |
| 431 | const struct midx_pack_key *key = va; |
| 432 | struct multi_pack_index *midx = key->midx; |
| 433 | |
| 434 | uint32_t versus = pack_pos_to_midx(midx, (uint32_t*)vb - (const uint32_t *)midx->revindex_data); |
| 435 | uint32_t versus_pack = nth_midxed_pack_int_id(midx, versus); |
| 436 | off_t versus_offset; |
| 437 | |
| 438 | uint32_t key_preferred = key->pack == key->preferred_pack; |
| 439 | uint32_t versus_preferred = versus_pack == key->preferred_pack; |
| 440 | |
| 441 | /* |
| 442 | * First, compare the preferred-ness, noting that the preferred pack |
| 443 | * comes first. |
| 444 | */ |
| 445 | if (key_preferred && !versus_preferred) |
| 446 | return -1; |
| 447 | else if (!key_preferred && versus_preferred) |
| 448 | return 1; |
| 449 | |
| 450 | /* Then, break ties first by comparing the pack IDs. */ |
| 451 | if (key->pack < versus_pack) |
| 452 | return -1; |
| 453 | else if (key->pack > versus_pack) |
| 454 | return 1; |
| 455 | |
| 456 | /* Finally, break ties by comparing offsets within a pack. */ |
| 457 | versus_offset = nth_midxed_offset(midx, versus); |
| 458 | if (key->offset < versus_offset) |
| 459 | return -1; |
| 460 | else if (key->offset > versus_offset) |
| 461 | return 1; |
| 462 | |
| 463 | return 0; |
| 464 | } |
| 465 | |
| 466 | int midx_to_pack_pos(struct multi_pack_index *m, uint32_t at, uint32_t *pos) |
| 467 | { |
| 468 | struct midx_pack_key key; |
| 469 | uint32_t *found; |
| 470 | |
| 471 | if (!m->revindex_data) |
| 472 | BUG("midx_to_pack_pos: reverse index not yet loaded"); |
| 473 | if (m->num_objects <= at) |
| 474 | BUG("midx_to_pack_pos: out-of-bounds object at %"PRIu32, at); |
| 475 | |
| 476 | key.pack = nth_midxed_pack_int_id(m, at); |
| 477 | key.offset = nth_midxed_offset(m, at); |
| 478 | key.midx = m; |
| 479 | /* |
| 480 | * The preferred pack sorts first, so determine its identifier by |
| 481 | * looking at the first object in pseudo-pack order. |
| 482 | * |
| 483 | * Note that if no --preferred-pack is explicitly given when writing a |
| 484 | * multi-pack index, then whichever pack has the lowest identifier |
| 485 | * implicitly is preferred (and includes all its objects, since ties are |
| 486 | * broken first by pack identifier). |
| 487 | */ |
| 488 | key.preferred_pack = nth_midxed_pack_int_id(m, pack_pos_to_midx(m, 0)); |
| 489 | |
| 490 | found = bsearch(&key, m->revindex_data, m->num_objects, |
| 491 | sizeof(*m->revindex_data), midx_pack_order_cmp); |
| 492 | |
| 493 | if (!found) |
| 494 | return error("bad offset for revindex"); |
| 495 | |
| 496 | *pos = found - m->revindex_data; |
| 497 | return 0; |
| 498 | } |