| 1 | #define DISABLE_SIGN_COMPARE_WARNINGS |
| 2 | |
| 3 | #include "git-compat-util.h" |
| 4 | #include "environment.h" |
| 5 | #include "gettext.h" |
| 6 | #include "hex.h" |
| 7 | #include "list.h" |
| 8 | #include "pack.h" |
| 9 | #include "repository.h" |
| 10 | #include "dir.h" |
| 11 | #include "mergesort.h" |
| 12 | #include "packfile.h" |
| 13 | #include "delta.h" |
| 14 | #include "hash-lookup.h" |
| 15 | #include "commit.h" |
| 16 | #include "object.h" |
| 17 | #include "tag.h" |
| 18 | #include "trace.h" |
| 19 | #include "tree-walk.h" |
| 20 | #include "tree.h" |
| 21 | #include "object-file.h" |
| 22 | #include "odb.h" |
| 23 | #include "odb/streaming.h" |
| 24 | #include "midx.h" |
| 25 | #include "commit-graph.h" |
| 26 | #include "pack-revindex.h" |
| 27 | #include "promisor-remote.h" |
| 28 | #include "pack-mtimes.h" |
| 29 | |
| 30 | char *odb_pack_name(struct repository *r, struct strbuf *buf, |
| 31 | const unsigned char *hash, const char *ext) |
| 32 | { |
| 33 | strbuf_reset(buf); |
| 34 | strbuf_addf(buf, "%s/pack/pack-%s.%s", repo_get_object_directory(r), |
| 35 | hash_to_hex_algop(hash, r->hash_algo), ext); |
| 36 | return buf->buf; |
| 37 | } |
| 38 | |
| 39 | static unsigned int pack_used_ctr; |
| 40 | static unsigned int pack_mmap_calls; |
| 41 | static unsigned int peak_pack_open_windows; |
| 42 | static unsigned int pack_open_windows; |
| 43 | static unsigned int pack_open_fds; |
| 44 | static unsigned int pack_max_fds; |
| 45 | static size_t peak_pack_mapped; |
| 46 | static size_t pack_mapped; |
| 47 | |
| 48 | #define SZ_FMT PRIuMAX |
| 49 | static inline uintmax_t sz_fmt(size_t s) { return s; } |
| 50 | |
| 51 | void packfile_list_clear(struct packfile_list *list) |
| 52 | { |
| 53 | struct packfile_list_entry *e, *next; |
| 54 | |
| 55 | for (e = list->head; e; e = next) { |
| 56 | next = e->next; |
| 57 | free(e); |
| 58 | } |
| 59 | |
| 60 | list->head = list->tail = NULL; |
| 61 | } |
| 62 | |
| 63 | static struct packfile_list_entry *packfile_list_remove_internal(struct packfile_list *list, |
| 64 | struct packed_git *pack) |
| 65 | { |
| 66 | struct packfile_list_entry *e, *prev; |
| 67 | |
| 68 | for (e = list->head, prev = NULL; e; prev = e, e = e->next) { |
| 69 | if (e->pack != pack) |
| 70 | continue; |
| 71 | |
| 72 | if (prev) |
| 73 | prev->next = e->next; |
| 74 | if (list->head == e) |
| 75 | list->head = e->next; |
| 76 | if (list->tail == e) |
| 77 | list->tail = prev; |
| 78 | |
| 79 | return e; |
| 80 | } |
| 81 | |
| 82 | return NULL; |
| 83 | } |
| 84 | |
| 85 | void packfile_list_remove(struct packfile_list *list, struct packed_git *pack) |
| 86 | { |
| 87 | free(packfile_list_remove_internal(list, pack)); |
| 88 | } |
| 89 | |
| 90 | void packfile_list_prepend(struct packfile_list *list, struct packed_git *pack) |
| 91 | { |
| 92 | struct packfile_list_entry *entry; |
| 93 | |
| 94 | entry = packfile_list_remove_internal(list, pack); |
| 95 | if (!entry) { |
| 96 | entry = xmalloc(sizeof(*entry)); |
| 97 | entry->pack = pack; |
| 98 | } |
| 99 | entry->next = list->head; |
| 100 | |
| 101 | list->head = entry; |
| 102 | if (!list->tail) |
| 103 | list->tail = entry; |
| 104 | } |
| 105 | |
| 106 | void packfile_list_append(struct packfile_list *list, struct packed_git *pack) |
| 107 | { |
| 108 | struct packfile_list_entry *entry; |
| 109 | |
| 110 | entry = packfile_list_remove_internal(list, pack); |
| 111 | if (!entry) { |
| 112 | entry = xmalloc(sizeof(*entry)); |
| 113 | entry->pack = pack; |
| 114 | } |
| 115 | entry->next = NULL; |
| 116 | |
| 117 | if (list->tail) { |
| 118 | list->tail->next = entry; |
| 119 | list->tail = entry; |
| 120 | } else { |
| 121 | list->head = list->tail = entry; |
| 122 | } |
| 123 | } |
| 124 | |
| 125 | struct packed_git *packfile_list_find_oid(struct packfile_list_entry *packs, |
| 126 | const struct object_id *oid) |
| 127 | { |
| 128 | for (; packs; packs = packs->next) |
| 129 | if (find_pack_entry_one(oid, packs->pack)) |
| 130 | return packs->pack; |
| 131 | return NULL; |
| 132 | } |
| 133 | |
| 134 | void pack_report(struct repository *repo) |
| 135 | { |
| 136 | fprintf(stderr, |
| 137 | "pack_report: getpagesize() = %10" SZ_FMT "\n" |
| 138 | "pack_report: core.packedGitWindowSize = %10" SZ_FMT "\n" |
| 139 | "pack_report: core.packedGitLimit = %10" SZ_FMT "\n", |
| 140 | sz_fmt(getpagesize()), |
| 141 | sz_fmt(repo->settings.packed_git_window_size), |
| 142 | sz_fmt(repo->settings.packed_git_limit)); |
| 143 | fprintf(stderr, |
| 144 | "pack_report: pack_used_ctr = %10u\n" |
| 145 | "pack_report: pack_mmap_calls = %10u\n" |
| 146 | "pack_report: pack_open_windows = %10u / %10u\n" |
| 147 | "pack_report: pack_mapped = " |
| 148 | "%10" SZ_FMT " / %10" SZ_FMT "\n", |
| 149 | pack_used_ctr, |
| 150 | pack_mmap_calls, |
| 151 | pack_open_windows, peak_pack_open_windows, |
| 152 | sz_fmt(pack_mapped), sz_fmt(peak_pack_mapped)); |
| 153 | } |
| 154 | |
| 155 | /* |
| 156 | * Open and mmap the index file at path, perform a couple of |
| 157 | * consistency checks, then record its information to p. Return 0 on |
| 158 | * success. |
| 159 | */ |
| 160 | static int check_packed_git_idx(const char *path, struct packed_git *p) |
| 161 | { |
| 162 | void *idx_map; |
| 163 | size_t idx_size; |
| 164 | int fd = git_open(path), ret; |
| 165 | struct stat st; |
| 166 | const unsigned int hashsz = p->repo->hash_algo->rawsz; |
| 167 | |
| 168 | if (fd < 0) |
| 169 | return -1; |
| 170 | if (fstat(fd, &st)) { |
| 171 | close(fd); |
| 172 | return -1; |
| 173 | } |
| 174 | idx_size = xsize_t(st.st_size); |
| 175 | if (idx_size < 4 * 256 + hashsz + hashsz) { |
| 176 | close(fd); |
| 177 | return error("index file %s is too small", path); |
| 178 | } |
| 179 | idx_map = xmmap(NULL, idx_size, PROT_READ, MAP_PRIVATE, fd, 0); |
| 180 | close(fd); |
| 181 | |
| 182 | ret = load_idx(path, hashsz, idx_map, idx_size, p); |
| 183 | |
| 184 | if (ret) |
| 185 | munmap(idx_map, idx_size); |
| 186 | |
| 187 | return ret; |
| 188 | } |
| 189 | |
| 190 | int load_idx(const char *path, const unsigned int hashsz, void *idx_map, |
| 191 | size_t idx_size, struct packed_git *p) |
| 192 | { |
| 193 | struct pack_idx_header *hdr = idx_map; |
| 194 | uint32_t version, nr, i, *index; |
| 195 | |
| 196 | if (idx_size < 4 * 256 + hashsz + hashsz) |
| 197 | return error("index file %s is too small", path); |
| 198 | if (!idx_map) |
| 199 | return error("empty data"); |
| 200 | |
| 201 | if (hdr->idx_signature == htonl(PACK_IDX_SIGNATURE)) { |
| 202 | version = ntohl(hdr->idx_version); |
| 203 | if (version < 2 || version > 2) |
| 204 | return error("index file %s is version %"PRIu32 |
| 205 | " and is not supported by this binary" |
| 206 | " (try upgrading GIT to a newer version)", |
| 207 | path, version); |
| 208 | } else |
| 209 | version = 1; |
| 210 | |
| 211 | nr = 0; |
| 212 | index = idx_map; |
| 213 | if (version > 1) |
| 214 | index += 2; /* skip index header */ |
| 215 | for (i = 0; i < 256; i++) { |
| 216 | uint32_t n = ntohl(index[i]); |
| 217 | if (n < nr) |
| 218 | return error("non-monotonic index %s", path); |
| 219 | nr = n; |
| 220 | } |
| 221 | |
| 222 | if (version == 1) { |
| 223 | /* |
| 224 | * Total size: |
| 225 | * - 256 index entries 4 bytes each |
| 226 | * - 24-byte entries * nr (object ID + 4-byte offset) |
| 227 | * - hash of the packfile |
| 228 | * - file checksum |
| 229 | */ |
| 230 | if (idx_size != st_add(4 * 256 + hashsz + hashsz, st_mult(nr, hashsz + 4))) |
| 231 | return error("wrong index v1 file size in %s", path); |
| 232 | } else if (version == 2) { |
| 233 | /* |
| 234 | * Minimum size: |
| 235 | * - 8 bytes of header |
| 236 | * - 256 index entries 4 bytes each |
| 237 | * - object ID entry * nr |
| 238 | * - 4-byte crc entry * nr |
| 239 | * - 4-byte offset entry * nr |
| 240 | * - hash of the packfile |
| 241 | * - file checksum |
| 242 | * And after the 4-byte offset table might be a |
| 243 | * variable sized table containing 8-byte entries |
| 244 | * for offsets larger than 2^31. |
| 245 | */ |
| 246 | size_t min_size = st_add(8 + 4*256 + hashsz + hashsz, st_mult(nr, hashsz + 4 + 4)); |
| 247 | size_t max_size = min_size; |
| 248 | if (nr) |
| 249 | max_size = st_add(max_size, st_mult(nr - 1, 8)); |
| 250 | if (idx_size < min_size || idx_size > max_size) |
| 251 | return error("wrong index v2 file size in %s", path); |
| 252 | if (idx_size != min_size && |
| 253 | /* |
| 254 | * make sure we can deal with large pack offsets. |
| 255 | * 31-bit signed offset won't be enough, neither |
| 256 | * 32-bit unsigned one will be. |
| 257 | */ |
| 258 | (sizeof(off_t) <= 4)) |
| 259 | return error("pack too large for current definition of off_t in %s", path); |
| 260 | p->crc_offset = st_add(8 + 4 * 256, st_mult(nr, hashsz)); |
| 261 | } |
| 262 | |
| 263 | p->index_version = version; |
| 264 | p->index_data = idx_map; |
| 265 | p->index_size = idx_size; |
| 266 | p->num_objects = nr; |
| 267 | return 0; |
| 268 | } |
| 269 | |
| 270 | int open_pack_index(struct packed_git *p) |
| 271 | { |
| 272 | char *idx_name; |
| 273 | size_t len; |
| 274 | int ret; |
| 275 | |
| 276 | if (p->index_data) |
| 277 | return 0; |
| 278 | |
| 279 | if (!strip_suffix(p->pack_name, ".pack", &len)) |
| 280 | BUG("pack_name does not end in .pack"); |
| 281 | idx_name = xstrfmt("%.*s.idx", (int)len, p->pack_name); |
| 282 | ret = check_packed_git_idx(idx_name, p); |
| 283 | free(idx_name); |
| 284 | return ret; |
| 285 | } |
| 286 | |
| 287 | uint32_t get_pack_fanout(struct packed_git *p, uint32_t value) |
| 288 | { |
| 289 | const uint32_t *level1_ofs = p->index_data; |
| 290 | |
| 291 | if (!level1_ofs) { |
| 292 | if (open_pack_index(p)) |
| 293 | return 0; |
| 294 | level1_ofs = p->index_data; |
| 295 | } |
| 296 | |
| 297 | if (p->index_version > 1) { |
| 298 | level1_ofs += 2; |
| 299 | } |
| 300 | |
| 301 | return ntohl(level1_ofs[value]); |
| 302 | } |
| 303 | |
| 304 | static struct packed_git *alloc_packed_git(struct repository *r, int extra) |
| 305 | { |
| 306 | struct packed_git *p = xmalloc(st_add(sizeof(*p), extra)); |
| 307 | memset(p, 0, sizeof(*p)); |
| 308 | p->pack_fd = -1; |
| 309 | p->repo = r; |
| 310 | return p; |
| 311 | } |
| 312 | |
| 313 | static char *pack_path_from_idx(const char *idx_path) |
| 314 | { |
| 315 | size_t len; |
| 316 | if (!strip_suffix(idx_path, ".idx", &len)) |
| 317 | BUG("idx path does not end in .idx: %s", idx_path); |
| 318 | return xstrfmt("%.*s.pack", (int)len, idx_path); |
| 319 | } |
| 320 | |
| 321 | struct packed_git *parse_pack_index(struct repository *r, unsigned char *sha1, |
| 322 | const char *idx_path) |
| 323 | { |
| 324 | char *path = pack_path_from_idx(idx_path); |
| 325 | size_t alloc = st_add(strlen(path), 1); |
| 326 | struct packed_git *p = alloc_packed_git(r, alloc); |
| 327 | |
| 328 | memcpy(p->pack_name, path, alloc); /* includes NUL */ |
| 329 | free(path); |
| 330 | hashcpy(p->hash, sha1, p->repo->hash_algo); |
| 331 | if (check_packed_git_idx(idx_path, p)) { |
| 332 | free(p); |
| 333 | return NULL; |
| 334 | } |
| 335 | |
| 336 | return p; |
| 337 | } |
| 338 | |
| 339 | static void scan_windows(struct packed_git *p, |
| 340 | struct packed_git **lru_p, |
| 341 | struct pack_window **lru_w, |
| 342 | struct pack_window **lru_l) |
| 343 | { |
| 344 | struct pack_window *w, *w_l; |
| 345 | |
| 346 | for (w_l = NULL, w = p->windows; w; w = w->next) { |
| 347 | if (!w->inuse_cnt) { |
| 348 | if (!*lru_w || w->last_used < (*lru_w)->last_used) { |
| 349 | *lru_p = p; |
| 350 | *lru_w = w; |
| 351 | *lru_l = w_l; |
| 352 | } |
| 353 | } |
| 354 | w_l = w; |
| 355 | } |
| 356 | } |
| 357 | |
| 358 | static int unuse_one_window(struct object_database *odb) |
| 359 | { |
| 360 | struct odb_source *source; |
| 361 | struct packfile_list_entry *e; |
| 362 | struct packed_git *lru_p = NULL; |
| 363 | struct pack_window *lru_w = NULL, *lru_l = NULL; |
| 364 | |
| 365 | for (source = odb->sources; source; source = source->next) { |
| 366 | struct odb_source_files *files = odb_source_files_downcast(source); |
| 367 | for (e = files->packed->packs.head; e; e = e->next) |
| 368 | scan_windows(e->pack, &lru_p, &lru_w, &lru_l); |
| 369 | } |
| 370 | |
| 371 | if (lru_p) { |
| 372 | munmap(lru_w->base, lru_w->len); |
| 373 | pack_mapped -= lru_w->len; |
| 374 | if (lru_l) |
| 375 | lru_l->next = lru_w->next; |
| 376 | else |
| 377 | lru_p->windows = lru_w->next; |
| 378 | free(lru_w); |
| 379 | pack_open_windows--; |
| 380 | return 1; |
| 381 | } |
| 382 | return 0; |
| 383 | } |
| 384 | |
| 385 | void close_pack_windows(struct packed_git *p) |
| 386 | { |
| 387 | while (p->windows) { |
| 388 | struct pack_window *w = p->windows; |
| 389 | |
| 390 | if (w->inuse_cnt) |
| 391 | die("pack '%s' still has open windows to it", |
| 392 | p->pack_name); |
| 393 | munmap(w->base, w->len); |
| 394 | pack_mapped -= w->len; |
| 395 | pack_open_windows--; |
| 396 | p->windows = w->next; |
| 397 | free(w); |
| 398 | } |
| 399 | } |
| 400 | |
| 401 | int close_pack_fd(struct packed_git *p) |
| 402 | { |
| 403 | if (p->pack_fd < 0) |
| 404 | return 0; |
| 405 | |
| 406 | close(p->pack_fd); |
| 407 | pack_open_fds--; |
| 408 | p->pack_fd = -1; |
| 409 | |
| 410 | return 1; |
| 411 | } |
| 412 | |
| 413 | void close_pack_index(struct packed_git *p) |
| 414 | { |
| 415 | if (p->index_data) { |
| 416 | munmap((void *)p->index_data, p->index_size); |
| 417 | p->index_data = NULL; |
| 418 | } |
| 419 | } |
| 420 | |
| 421 | static void close_pack_revindex(struct packed_git *p) |
| 422 | { |
| 423 | FREE_AND_NULL(p->revindex); |
| 424 | |
| 425 | if (!p->revindex_map) |
| 426 | return; |
| 427 | |
| 428 | munmap((void *)p->revindex_map, p->revindex_size); |
| 429 | p->revindex_map = NULL; |
| 430 | p->revindex_data = NULL; |
| 431 | } |
| 432 | |
| 433 | static void close_pack_mtimes(struct packed_git *p) |
| 434 | { |
| 435 | if (!p->mtimes_map) |
| 436 | return; |
| 437 | |
| 438 | munmap((void *)p->mtimes_map, p->mtimes_size); |
| 439 | p->mtimes_map = NULL; |
| 440 | } |
| 441 | |
| 442 | void close_pack(struct packed_git *p) |
| 443 | { |
| 444 | close_pack_windows(p); |
| 445 | close_pack_fd(p); |
| 446 | close_pack_index(p); |
| 447 | close_pack_revindex(p); |
| 448 | close_pack_mtimes(p); |
| 449 | oidset_clear(&p->bad_objects); |
| 450 | } |
| 451 | |
| 452 | void unlink_pack_path(const char *pack_name, int force_delete) |
| 453 | { |
| 454 | static const char *exts[] = {".idx", ".pack", ".rev", ".keep", ".bitmap", ".promisor", ".mtimes"}; |
| 455 | int i; |
| 456 | struct strbuf buf = STRBUF_INIT; |
| 457 | size_t plen; |
| 458 | |
| 459 | strbuf_addstr(&buf, pack_name); |
| 460 | strip_suffix_mem(buf.buf, &buf.len, ".pack"); |
| 461 | plen = buf.len; |
| 462 | |
| 463 | if (!force_delete) { |
| 464 | strbuf_addstr(&buf, ".keep"); |
| 465 | if (!access(buf.buf, F_OK)) { |
| 466 | strbuf_release(&buf); |
| 467 | return; |
| 468 | } |
| 469 | } |
| 470 | |
| 471 | for (i = 0; i < ARRAY_SIZE(exts); i++) { |
| 472 | strbuf_setlen(&buf, plen); |
| 473 | strbuf_addstr(&buf, exts[i]); |
| 474 | unlink(buf.buf); |
| 475 | } |
| 476 | |
| 477 | strbuf_release(&buf); |
| 478 | } |
| 479 | |
| 480 | /* |
| 481 | * The LRU pack is the one with the oldest MRU window, preferring packs |
| 482 | * with no used windows, or the oldest mtime if it has no windows allocated. |
| 483 | */ |
| 484 | static void find_lru_pack(struct packed_git *p, struct packed_git **lru_p, struct pack_window **mru_w, int *accept_windows_inuse) |
| 485 | { |
| 486 | struct pack_window *w, *this_mru_w; |
| 487 | int has_windows_inuse = 0; |
| 488 | |
| 489 | /* |
| 490 | * Reject this pack if it has windows and the previously selected |
| 491 | * one does not. If this pack does not have windows, reject |
| 492 | * it if the pack file is newer than the previously selected one. |
| 493 | */ |
| 494 | if (*lru_p && !*mru_w && (p->windows || p->mtime > (*lru_p)->mtime)) |
| 495 | return; |
| 496 | |
| 497 | for (w = this_mru_w = p->windows; w; w = w->next) { |
| 498 | /* |
| 499 | * Reject this pack if any of its windows are in use, |
| 500 | * but the previously selected pack did not have any |
| 501 | * inuse windows. Otherwise, record that this pack |
| 502 | * has windows in use. |
| 503 | */ |
| 504 | if (w->inuse_cnt) { |
| 505 | if (*accept_windows_inuse) |
| 506 | has_windows_inuse = 1; |
| 507 | else |
| 508 | return; |
| 509 | } |
| 510 | |
| 511 | if (w->last_used > this_mru_w->last_used) |
| 512 | this_mru_w = w; |
| 513 | |
| 514 | /* |
| 515 | * Reject this pack if it has windows that have been |
| 516 | * used more recently than the previously selected pack. |
| 517 | * If the previously selected pack had windows inuse and |
| 518 | * we have not encountered a window in this pack that is |
| 519 | * inuse, skip this check since we prefer a pack with no |
| 520 | * inuse windows to one that has inuse windows. |
| 521 | */ |
| 522 | if (*mru_w && *accept_windows_inuse == has_windows_inuse && |
| 523 | this_mru_w->last_used > (*mru_w)->last_used) |
| 524 | return; |
| 525 | } |
| 526 | |
| 527 | /* |
| 528 | * Select this pack. |
| 529 | */ |
| 530 | *mru_w = this_mru_w; |
| 531 | *lru_p = p; |
| 532 | *accept_windows_inuse = has_windows_inuse; |
| 533 | } |
| 534 | |
| 535 | static int close_one_pack(struct repository *r) |
| 536 | { |
| 537 | struct odb_source *source; |
| 538 | struct packfile_list_entry *e; |
| 539 | struct packed_git *lru_p = NULL; |
| 540 | struct pack_window *mru_w = NULL; |
| 541 | int accept_windows_inuse = 1; |
| 542 | |
| 543 | for (source = r->objects->sources; source; source = source->next) { |
| 544 | struct odb_source_files *files = odb_source_files_downcast(source); |
| 545 | for (e = files->packed->packs.head; e; e = e->next) { |
| 546 | if (e->pack->pack_fd == -1) |
| 547 | continue; |
| 548 | find_lru_pack(e->pack, &lru_p, &mru_w, &accept_windows_inuse); |
| 549 | } |
| 550 | } |
| 551 | |
| 552 | if (lru_p) |
| 553 | return close_pack_fd(lru_p); |
| 554 | |
| 555 | return 0; |
| 556 | } |
| 557 | |
| 558 | static unsigned int get_max_fd_limit(void) |
| 559 | { |
| 560 | #ifdef RLIMIT_NOFILE |
| 561 | { |
| 562 | struct rlimit lim; |
| 563 | |
| 564 | if (!getrlimit(RLIMIT_NOFILE, &lim)) |
| 565 | return lim.rlim_cur; |
| 566 | } |
| 567 | #endif |
| 568 | |
| 569 | #ifdef _SC_OPEN_MAX |
| 570 | { |
| 571 | long open_max = sysconf(_SC_OPEN_MAX); |
| 572 | if (0 < open_max) |
| 573 | return open_max; |
| 574 | /* |
| 575 | * Otherwise, we got -1 for one of the two |
| 576 | * reasons: |
| 577 | * |
| 578 | * (1) sysconf() did not understand _SC_OPEN_MAX |
| 579 | * and signaled an error with -1; or |
| 580 | * (2) sysconf() said there is no limit. |
| 581 | * |
| 582 | * We _could_ clear errno before calling sysconf() to |
| 583 | * tell these two cases apart and return a huge number |
| 584 | * in the latter case to let the caller cap it to a |
| 585 | * value that is not so selfish, but letting the |
| 586 | * fallback OPEN_MAX codepath take care of these cases |
| 587 | * is a lot simpler. |
| 588 | */ |
| 589 | } |
| 590 | #endif |
| 591 | |
| 592 | #ifdef OPEN_MAX |
| 593 | return OPEN_MAX; |
| 594 | #else |
| 595 | return 1; /* see the caller ;-) */ |
| 596 | #endif |
| 597 | } |
| 598 | |
| 599 | const char *pack_basename(struct packed_git *p) |
| 600 | { |
| 601 | const char *ret = strrchr(p->pack_name, '/'); |
| 602 | if (ret) |
| 603 | ret = ret + 1; /* skip past slash */ |
| 604 | else |
| 605 | ret = p->pack_name; /* we only have a base */ |
| 606 | return ret; |
| 607 | } |
| 608 | |
| 609 | /* |
| 610 | * Do not call this directly as this leaks p->pack_fd on error return; |
| 611 | * call open_packed_git() instead. |
| 612 | */ |
| 613 | static int open_packed_git_1(struct packed_git *p) |
| 614 | { |
| 615 | struct stat st; |
| 616 | struct pack_header hdr; |
| 617 | unsigned char hash[GIT_MAX_RAWSZ]; |
| 618 | unsigned char *idx_hash; |
| 619 | ssize_t read_result; |
| 620 | const unsigned hashsz = p->repo->hash_algo->rawsz; |
| 621 | |
| 622 | if (open_pack_index(p)) |
| 623 | return error("packfile %s index unavailable", p->pack_name); |
| 624 | |
| 625 | if (!pack_max_fds) { |
| 626 | unsigned int max_fds = get_max_fd_limit(); |
| 627 | |
| 628 | /* Save 3 for stdin/stdout/stderr, 22 for work */ |
| 629 | if (25 < max_fds) |
| 630 | pack_max_fds = max_fds - 25; |
| 631 | else |
| 632 | pack_max_fds = 1; |
| 633 | } |
| 634 | |
| 635 | while (pack_max_fds <= pack_open_fds && close_one_pack(p->repo)) |
| 636 | ; /* nothing */ |
| 637 | |
| 638 | p->pack_fd = git_open(p->pack_name); |
| 639 | if (p->pack_fd < 0 || fstat(p->pack_fd, &st)) |
| 640 | return -1; |
| 641 | pack_open_fds++; |
| 642 | |
| 643 | /* If we created the struct before we had the pack we lack size. */ |
| 644 | if (!p->pack_size) { |
| 645 | if (!S_ISREG(st.st_mode)) |
| 646 | return error("packfile %s not a regular file", p->pack_name); |
| 647 | p->pack_size = st.st_size; |
| 648 | } else if (p->pack_size != st.st_size) |
| 649 | return error("packfile %s size changed", p->pack_name); |
| 650 | |
| 651 | /* Verify we recognize this pack file format. */ |
| 652 | read_result = read_in_full(p->pack_fd, &hdr, sizeof(hdr)); |
| 653 | if (read_result < 0) |
| 654 | return error_errno("error reading from %s", p->pack_name); |
| 655 | if (read_result != sizeof(hdr)) |
| 656 | return error("file %s is far too short to be a packfile", p->pack_name); |
| 657 | if (hdr.hdr_signature != htonl(PACK_SIGNATURE)) |
| 658 | return error("file %s is not a GIT packfile", p->pack_name); |
| 659 | if (!pack_version_ok(hdr.hdr_version)) |
| 660 | return error("packfile %s is version %"PRIu32" and not" |
| 661 | " supported (try upgrading GIT to a newer version)", |
| 662 | p->pack_name, ntohl(hdr.hdr_version)); |
| 663 | |
| 664 | /* Verify the pack matches its index. */ |
| 665 | if (p->num_objects != ntohl(hdr.hdr_entries)) |
| 666 | return error("packfile %s claims to have %"PRIu32" objects" |
| 667 | " while index indicates %"PRIu32" objects", |
| 668 | p->pack_name, ntohl(hdr.hdr_entries), |
| 669 | p->num_objects); |
| 670 | read_result = pread_in_full(p->pack_fd, hash, hashsz, |
| 671 | p->pack_size - hashsz); |
| 672 | if (read_result < 0) |
| 673 | return error_errno("error reading from %s", p->pack_name); |
| 674 | if (read_result != hashsz) |
| 675 | return error("packfile %s signature is unavailable", p->pack_name); |
| 676 | idx_hash = ((unsigned char *)p->index_data) + p->index_size - hashsz * 2; |
| 677 | if (!hasheq(hash, idx_hash, p->repo->hash_algo)) |
| 678 | return error("packfile %s does not match index", p->pack_name); |
| 679 | return 0; |
| 680 | } |
| 681 | |
| 682 | static int open_packed_git(struct packed_git *p) |
| 683 | { |
| 684 | if (!open_packed_git_1(p)) |
| 685 | return 0; |
| 686 | close_pack_fd(p); |
| 687 | return -1; |
| 688 | } |
| 689 | |
| 690 | static int in_window(struct repository *r, struct pack_window *win, |
| 691 | off_t offset) |
| 692 | { |
| 693 | /* We must promise at least one full hash after the |
| 694 | * offset is available from this window, otherwise the offset |
| 695 | * is not actually in this window and a different window (which |
| 696 | * has that one hash excess) must be used. This is to support |
| 697 | * the object header and delta base parsing routines below. |
| 698 | */ |
| 699 | off_t win_off = win->offset; |
| 700 | return win_off <= offset |
| 701 | && (offset + r->hash_algo->rawsz) <= (win_off + win->len); |
| 702 | } |
| 703 | |
| 704 | unsigned char *use_pack(struct packed_git *p, |
| 705 | struct pack_window **w_cursor, |
| 706 | off_t offset, |
| 707 | unsigned long *left) |
| 708 | { |
| 709 | struct pack_window *win = *w_cursor; |
| 710 | |
| 711 | /* Since packfiles end in a hash of their content and it's |
| 712 | * pointless to ask for an offset into the middle of that |
| 713 | * hash, and the in_window function above wouldn't match |
| 714 | * don't allow an offset too close to the end of the file. |
| 715 | */ |
| 716 | if (!p->pack_size && p->pack_fd == -1 && open_packed_git(p)) |
| 717 | die("packfile %s cannot be accessed", p->pack_name); |
| 718 | if (offset > (p->pack_size - p->repo->hash_algo->rawsz)) |
| 719 | die("offset beyond end of packfile (truncated pack?)"); |
| 720 | if (offset < 0) |
| 721 | die(_("offset before end of packfile (broken .idx?)")); |
| 722 | |
| 723 | if (!win || !in_window(p->repo, win, offset)) { |
| 724 | if (win) |
| 725 | win->inuse_cnt--; |
| 726 | for (win = p->windows; win; win = win->next) { |
| 727 | if (in_window(p->repo, win, offset)) |
| 728 | break; |
| 729 | } |
| 730 | if (!win) { |
| 731 | size_t window_align; |
| 732 | off_t len; |
| 733 | struct repo_settings *settings; |
| 734 | |
| 735 | /* lazy load the settings in case it hasn't been setup */ |
| 736 | prepare_repo_settings(p->repo); |
| 737 | settings = &p->repo->settings; |
| 738 | |
| 739 | window_align = settings->packed_git_window_size / 2; |
| 740 | |
| 741 | if (p->pack_fd == -1 && open_packed_git(p)) |
| 742 | die("packfile %s cannot be accessed", p->pack_name); |
| 743 | |
| 744 | CALLOC_ARRAY(win, 1); |
| 745 | win->offset = (offset / window_align) * window_align; |
| 746 | len = p->pack_size - win->offset; |
| 747 | if (len > settings->packed_git_window_size) |
| 748 | len = settings->packed_git_window_size; |
| 749 | win->len = (size_t)len; |
| 750 | pack_mapped += win->len; |
| 751 | |
| 752 | while (settings->packed_git_limit < pack_mapped && |
| 753 | unuse_one_window(p->repo->objects)) |
| 754 | ; /* nothing */ |
| 755 | win->base = xmmap_gently(NULL, win->len, |
| 756 | PROT_READ, MAP_PRIVATE, |
| 757 | p->pack_fd, win->offset); |
| 758 | if (win->base == MAP_FAILED) |
| 759 | die_errno(_("packfile %s cannot be mapped%s"), |
| 760 | p->pack_name, mmap_os_err()); |
| 761 | if (!win->offset && win->len == p->pack_size |
| 762 | && !p->do_not_close) |
| 763 | close_pack_fd(p); |
| 764 | pack_mmap_calls++; |
| 765 | pack_open_windows++; |
| 766 | if (pack_mapped > peak_pack_mapped) |
| 767 | peak_pack_mapped = pack_mapped; |
| 768 | if (pack_open_windows > peak_pack_open_windows) |
| 769 | peak_pack_open_windows = pack_open_windows; |
| 770 | win->next = p->windows; |
| 771 | p->windows = win; |
| 772 | } |
| 773 | } |
| 774 | if (win != *w_cursor) { |
| 775 | win->last_used = pack_used_ctr++; |
| 776 | win->inuse_cnt++; |
| 777 | *w_cursor = win; |
| 778 | } |
| 779 | offset -= win->offset; |
| 780 | if (left) |
| 781 | *left = win->len - xsize_t(offset); |
| 782 | return win->base + offset; |
| 783 | } |
| 784 | |
| 785 | void unuse_pack(struct pack_window **w_cursor) |
| 786 | { |
| 787 | struct pack_window *w = *w_cursor; |
| 788 | if (w) { |
| 789 | w->inuse_cnt--; |
| 790 | *w_cursor = NULL; |
| 791 | } |
| 792 | } |
| 793 | |
| 794 | struct packed_git *add_packed_git(struct repository *r, const char *path, |
| 795 | size_t path_len, int local) |
| 796 | { |
| 797 | struct stat st; |
| 798 | size_t alloc; |
| 799 | struct packed_git *p; |
| 800 | struct object_id oid; |
| 801 | |
| 802 | /* |
| 803 | * Make sure a corresponding .pack file exists and that |
| 804 | * the index looks sane. |
| 805 | */ |
| 806 | if (!strip_suffix_mem(path, &path_len, ".idx")) |
| 807 | return NULL; |
| 808 | |
| 809 | /* |
| 810 | * ".promisor" is long enough to hold any suffix we're adding (and |
| 811 | * the use xsnprintf double-checks that) |
| 812 | */ |
| 813 | alloc = st_add3(path_len, strlen(".promisor"), 1); |
| 814 | p = alloc_packed_git(r, alloc); |
| 815 | memcpy(p->pack_name, path, path_len); |
| 816 | |
| 817 | /* |
| 818 | * Note that we have to check auxiliary data structures before we check |
| 819 | * for the ".pack" file to exist to avoid races with a packfile that is |
| 820 | * in the process of being deleted. The ".pack" file is unlinked before |
| 821 | * its auxiliary data structures, so we know that we either get a |
| 822 | * consistent snapshot of all data structures or that we'll fail to |
| 823 | * stat(3p) the packfile itself and thus return `NULL`. |
| 824 | * |
| 825 | * As such, we cannot bail out before the access(3p) calls in case the |
| 826 | * packfile doesn't exist without doing two stat(3p) calls for it. |
| 827 | */ |
| 828 | xsnprintf(p->pack_name + path_len, alloc - path_len, ".keep"); |
| 829 | if (!access(p->pack_name, F_OK)) |
| 830 | p->pack_keep = 1; |
| 831 | |
| 832 | xsnprintf(p->pack_name + path_len, alloc - path_len, ".promisor"); |
| 833 | if (!access(p->pack_name, F_OK)) |
| 834 | p->pack_promisor = 1; |
| 835 | |
| 836 | xsnprintf(p->pack_name + path_len, alloc - path_len, ".mtimes"); |
| 837 | if (!access(p->pack_name, F_OK)) |
| 838 | p->is_cruft = 1; |
| 839 | |
| 840 | xsnprintf(p->pack_name + path_len, alloc - path_len, ".pack"); |
| 841 | if (stat(p->pack_name, &st) || !S_ISREG(st.st_mode)) { |
| 842 | free(p); |
| 843 | return NULL; |
| 844 | } |
| 845 | |
| 846 | /* ok, it looks sane as far as we can check without |
| 847 | * actually mapping the pack file. |
| 848 | */ |
| 849 | p->pack_size = st.st_size; |
| 850 | p->pack_local = local; |
| 851 | p->mtime = st.st_mtime; |
| 852 | if (path_len < r->hash_algo->hexsz || |
| 853 | get_oid_hex_algop(path + path_len - r->hash_algo->hexsz, &oid, |
| 854 | r->hash_algo)) |
| 855 | hashclr(p->hash, r->hash_algo); |
| 856 | else |
| 857 | hashcpy(p->hash, oid.hash, r->hash_algo); |
| 858 | |
| 859 | return p; |
| 860 | } |
| 861 | |
| 862 | void packfile_store_add_pack(struct packfile_store *store, |
| 863 | struct packed_git *pack) |
| 864 | { |
| 865 | if (pack->pack_fd != -1) |
| 866 | pack_open_fds++; |
| 867 | |
| 868 | packfile_list_append(&store->packs, pack); |
| 869 | strmap_put(&store->packs_by_path, pack->pack_name, pack); |
| 870 | } |
| 871 | |
| 872 | struct packed_git *packfile_store_load_pack(struct packfile_store *store, |
| 873 | const char *idx_path, int local) |
| 874 | { |
| 875 | struct strbuf key = STRBUF_INIT; |
| 876 | struct packed_git *p; |
| 877 | |
| 878 | /* |
| 879 | * We're being called with the path to the index file, but `pack_map` |
| 880 | * holds the path to the packfile itself. |
| 881 | */ |
| 882 | strbuf_addstr(&key, idx_path); |
| 883 | strbuf_strip_suffix(&key, ".idx"); |
| 884 | strbuf_addstr(&key, ".pack"); |
| 885 | |
| 886 | p = strmap_get(&store->packs_by_path, key.buf); |
| 887 | if (!p) { |
| 888 | p = add_packed_git(store->source->odb->repo, idx_path, |
| 889 | strlen(idx_path), local); |
| 890 | if (p) |
| 891 | packfile_store_add_pack(store, p); |
| 892 | } |
| 893 | |
| 894 | strbuf_release(&key); |
| 895 | return p; |
| 896 | } |
| 897 | |
| 898 | void (*report_garbage)(unsigned seen_bits, const char *path); |
| 899 | |
| 900 | static void report_helper(const struct string_list *list, |
| 901 | int seen_bits, int first, int last) |
| 902 | { |
| 903 | if (seen_bits == (PACKDIR_FILE_PACK|PACKDIR_FILE_IDX)) |
| 904 | return; |
| 905 | |
| 906 | for (; first < last; first++) |
| 907 | report_garbage(seen_bits, list->items[first].string); |
| 908 | } |
| 909 | |
| 910 | static void report_pack_garbage(struct string_list *list) |
| 911 | { |
| 912 | int i, baselen = -1, first = 0, seen_bits = 0; |
| 913 | |
| 914 | if (!report_garbage) |
| 915 | return; |
| 916 | |
| 917 | string_list_sort(list); |
| 918 | |
| 919 | for (i = 0; i < list->nr; i++) { |
| 920 | const char *path = list->items[i].string; |
| 921 | if (baselen != -1 && |
| 922 | strncmp(path, list->items[first].string, baselen)) { |
| 923 | report_helper(list, seen_bits, first, i); |
| 924 | baselen = -1; |
| 925 | seen_bits = 0; |
| 926 | } |
| 927 | if (baselen == -1) { |
| 928 | const char *dot = strrchr(path, '.'); |
| 929 | if (!dot) { |
| 930 | report_garbage(PACKDIR_FILE_GARBAGE, path); |
| 931 | continue; |
| 932 | } |
| 933 | baselen = dot - path + 1; |
| 934 | first = i; |
| 935 | } |
| 936 | if (!strcmp(path + baselen, "pack")) |
| 937 | seen_bits |= 1; |
| 938 | else if (!strcmp(path + baselen, "idx")) |
| 939 | seen_bits |= 2; |
| 940 | } |
| 941 | report_helper(list, seen_bits, first, list->nr); |
| 942 | } |
| 943 | |
| 944 | void for_each_file_in_pack_subdir(const char *objdir, |
| 945 | const char *subdir, |
| 946 | each_file_in_pack_dir_fn fn, |
| 947 | void *data) |
| 948 | { |
| 949 | struct strbuf path = STRBUF_INIT; |
| 950 | size_t dirnamelen; |
| 951 | DIR *dir; |
| 952 | struct dirent *de; |
| 953 | |
| 954 | strbuf_addstr(&path, objdir); |
| 955 | strbuf_addstr(&path, "/pack"); |
| 956 | if (subdir) |
| 957 | strbuf_addf(&path, "/%s", subdir); |
| 958 | dir = opendir(path.buf); |
| 959 | if (!dir) { |
| 960 | if (errno != ENOENT) |
| 961 | error_errno("unable to open object pack directory: %s", |
| 962 | path.buf); |
| 963 | strbuf_release(&path); |
| 964 | return; |
| 965 | } |
| 966 | strbuf_addch(&path, '/'); |
| 967 | dirnamelen = path.len; |
| 968 | while ((de = readdir_skip_dot_and_dotdot(dir)) != NULL) { |
| 969 | strbuf_setlen(&path, dirnamelen); |
| 970 | strbuf_addstr(&path, de->d_name); |
| 971 | |
| 972 | fn(path.buf, path.len, de->d_name, data); |
| 973 | } |
| 974 | |
| 975 | closedir(dir); |
| 976 | strbuf_release(&path); |
| 977 | } |
| 978 | |
| 979 | void for_each_file_in_pack_dir(const char *objdir, |
| 980 | each_file_in_pack_dir_fn fn, |
| 981 | void *data) |
| 982 | { |
| 983 | for_each_file_in_pack_subdir(objdir, NULL, fn, data); |
| 984 | } |
| 985 | |
| 986 | struct prepare_pack_data { |
| 987 | struct odb_source *source; |
| 988 | struct string_list *garbage; |
| 989 | }; |
| 990 | |
| 991 | static void prepare_pack(const char *full_name, size_t full_name_len, |
| 992 | const char *file_name, void *_data) |
| 993 | { |
| 994 | struct prepare_pack_data *data = (struct prepare_pack_data *)_data; |
| 995 | struct odb_source_files *files = odb_source_files_downcast(data->source); |
| 996 | size_t base_len = full_name_len; |
| 997 | |
| 998 | if (strip_suffix_mem(full_name, &base_len, ".idx") && |
| 999 | !(files->packed->midx && |
| 1000 | midx_contains_pack(files->packed->midx, file_name))) { |
| 1001 | char *trimmed_path = xstrndup(full_name, full_name_len); |
| 1002 | packfile_store_load_pack(files->packed, |
| 1003 | trimmed_path, data->source->local); |
| 1004 | free(trimmed_path); |
| 1005 | } |
| 1006 | |
| 1007 | if (!report_garbage) |
| 1008 | return; |
| 1009 | |
| 1010 | if (!strcmp(file_name, "multi-pack-index") || |
| 1011 | !strcmp(file_name, "multi-pack-index.d")) |
| 1012 | return; |
| 1013 | if (starts_with(file_name, "multi-pack-index") && |
| 1014 | (ends_with(file_name, ".bitmap") || ends_with(file_name, ".rev"))) |
| 1015 | return; |
| 1016 | if (ends_with(file_name, ".idx") || |
| 1017 | ends_with(file_name, ".rev") || |
| 1018 | ends_with(file_name, ".pack") || |
| 1019 | ends_with(file_name, ".bitmap") || |
| 1020 | ends_with(file_name, ".keep") || |
| 1021 | ends_with(file_name, ".promisor") || |
| 1022 | ends_with(file_name, ".mtimes")) |
| 1023 | string_list_append(data->garbage, full_name); |
| 1024 | else |
| 1025 | report_garbage(PACKDIR_FILE_GARBAGE, full_name); |
| 1026 | } |
| 1027 | |
| 1028 | static void prepare_packed_git_one(struct odb_source *source) |
| 1029 | { |
| 1030 | struct string_list garbage = STRING_LIST_INIT_DUP; |
| 1031 | struct prepare_pack_data data = { |
| 1032 | .source = source, |
| 1033 | .garbage = &garbage, |
| 1034 | }; |
| 1035 | |
| 1036 | for_each_file_in_pack_dir(source->path, prepare_pack, &data); |
| 1037 | |
| 1038 | report_pack_garbage(data.garbage); |
| 1039 | string_list_clear(data.garbage, 0); |
| 1040 | } |
| 1041 | |
| 1042 | DEFINE_LIST_SORT(static, sort_packs, struct packfile_list_entry, next); |
| 1043 | |
| 1044 | static int sort_pack(const struct packfile_list_entry *a, |
| 1045 | const struct packfile_list_entry *b) |
| 1046 | { |
| 1047 | int st; |
| 1048 | |
| 1049 | /* |
| 1050 | * Local packs tend to contain objects specific to our |
| 1051 | * variant of the project than remote ones. In addition, |
| 1052 | * remote ones could be on a network mounted filesystem. |
| 1053 | * Favor local ones for these reasons. |
| 1054 | */ |
| 1055 | st = a->pack->pack_local - b->pack->pack_local; |
| 1056 | if (st) |
| 1057 | return -st; |
| 1058 | |
| 1059 | /* |
| 1060 | * Younger packs tend to contain more recent objects, |
| 1061 | * and more recent objects tend to get accessed more |
| 1062 | * often. |
| 1063 | */ |
| 1064 | if (a->pack->mtime < b->pack->mtime) |
| 1065 | return 1; |
| 1066 | else if (a->pack->mtime == b->pack->mtime) |
| 1067 | return 0; |
| 1068 | return -1; |
| 1069 | } |
| 1070 | |
| 1071 | void packfile_store_prepare(struct packfile_store *store) |
| 1072 | { |
| 1073 | if (store->initialized) |
| 1074 | return; |
| 1075 | |
| 1076 | prepare_multi_pack_index_one(store->source); |
| 1077 | prepare_packed_git_one(store->source); |
| 1078 | |
| 1079 | sort_packs(&store->packs.head, sort_pack); |
| 1080 | for (struct packfile_list_entry *e = store->packs.head; e; e = e->next) |
| 1081 | if (!e->next) |
| 1082 | store->packs.tail = e; |
| 1083 | |
| 1084 | store->initialized = true; |
| 1085 | } |
| 1086 | |
| 1087 | void packfile_store_reprepare(struct packfile_store *store) |
| 1088 | { |
| 1089 | store->initialized = false; |
| 1090 | packfile_store_prepare(store); |
| 1091 | } |
| 1092 | |
| 1093 | struct packfile_list_entry *packfile_store_get_packs(struct packfile_store *store) |
| 1094 | { |
| 1095 | packfile_store_prepare(store); |
| 1096 | |
| 1097 | if (store->midx) { |
| 1098 | struct multi_pack_index *m = store->midx; |
| 1099 | for (uint32_t i = 0; i < m->num_packs + m->num_packs_in_base; i++) |
| 1100 | prepare_midx_pack(m, i); |
| 1101 | } |
| 1102 | |
| 1103 | return store->packs.head; |
| 1104 | } |
| 1105 | |
| 1106 | int packfile_store_count_objects(struct packfile_store *store, |
| 1107 | enum odb_count_objects_flags flags UNUSED, |
| 1108 | unsigned long *out) |
| 1109 | { |
| 1110 | struct packfile_list_entry *e; |
| 1111 | struct multi_pack_index *m; |
| 1112 | unsigned long count = 0; |
| 1113 | int ret; |
| 1114 | |
| 1115 | m = get_multi_pack_index(store->source); |
| 1116 | if (m) |
| 1117 | count += m->num_objects + m->num_objects_in_base; |
| 1118 | |
| 1119 | for (e = packfile_store_get_packs(store); e; e = e->next) { |
| 1120 | if (e->pack->multi_pack_index) |
| 1121 | continue; |
| 1122 | if (open_pack_index(e->pack)) { |
| 1123 | ret = -1; |
| 1124 | goto out; |
| 1125 | } |
| 1126 | |
| 1127 | count += e->pack->num_objects; |
| 1128 | } |
| 1129 | |
| 1130 | *out = count; |
| 1131 | ret = 0; |
| 1132 | |
| 1133 | out: |
| 1134 | return ret; |
| 1135 | } |
| 1136 | |
| 1137 | unsigned long unpack_object_header_buffer(const unsigned char *buf, |
| 1138 | unsigned long len, enum object_type *type, size_t *sizep) |
| 1139 | { |
| 1140 | unsigned shift; |
| 1141 | size_t size, c; |
| 1142 | unsigned long used = 0; |
| 1143 | |
| 1144 | c = buf[used++]; |
| 1145 | *type = (c >> 4) & 7; |
| 1146 | size = c & 15; |
| 1147 | shift = 4; |
| 1148 | while (c & 0x80) { |
| 1149 | /* |
| 1150 | * Each continuation byte adds 7 bits. Ensure shift won't |
| 1151 | * overflow size_t (use size_t not long for 64-bit on Windows). |
| 1152 | */ |
| 1153 | if (len <= used || (bitsizeof(size_t) - 7) < shift) { |
| 1154 | error("bad object header"); |
| 1155 | size = used = 0; |
| 1156 | break; |
| 1157 | } |
| 1158 | c = buf[used++]; |
| 1159 | size = st_add(size, st_left_shift(c & 0x7f, shift)); |
| 1160 | shift += 7; |
| 1161 | } |
| 1162 | *sizep = size; |
| 1163 | return used; |
| 1164 | } |
| 1165 | |
| 1166 | /* |
| 1167 | * Size_t variant for >4GB delta results on Windows. |
| 1168 | */ |
| 1169 | static size_t get_size_from_delta_sz(struct packed_git *p, |
| 1170 | struct pack_window **w_curs, |
| 1171 | off_t curpos) |
| 1172 | { |
| 1173 | const unsigned char *data; |
| 1174 | unsigned char delta_head[20], *in; |
| 1175 | git_zstream stream; |
| 1176 | int st; |
| 1177 | |
| 1178 | memset(&stream, 0, sizeof(stream)); |
| 1179 | stream.next_out = delta_head; |
| 1180 | stream.avail_out = sizeof(delta_head); |
| 1181 | |
| 1182 | git_inflate_init(&stream); |
| 1183 | do { |
| 1184 | in = use_pack(p, w_curs, curpos, &stream.avail_in); |
| 1185 | stream.next_in = in; |
| 1186 | /* |
| 1187 | * Note: the window section returned by use_pack() must be |
| 1188 | * available throughout git_inflate()'s unlocked execution. To |
| 1189 | * ensure no other thread will modify the window in the |
| 1190 | * meantime, we rely on the packed_window.inuse_cnt. This |
| 1191 | * counter is incremented before window reading and checked |
| 1192 | * before window disposal. |
| 1193 | * |
| 1194 | * Other worrying sections could be the call to close_pack_fd(), |
| 1195 | * which can close packs even with in-use windows, and to |
| 1196 | * odb_reprepare(). Regarding the former, mmap doc says: |
| 1197 | * "closing the file descriptor does not unmap the region". And |
| 1198 | * for the latter, it won't re-open already available packs. |
| 1199 | */ |
| 1200 | obj_read_unlock(); |
| 1201 | st = git_inflate(&stream, Z_FINISH); |
| 1202 | obj_read_lock(); |
| 1203 | curpos += stream.next_in - in; |
| 1204 | } while ((st == Z_OK || st == Z_BUF_ERROR) && |
| 1205 | stream.total_out < sizeof(delta_head)); |
| 1206 | git_inflate_end(&stream); |
| 1207 | if ((st != Z_STREAM_END) && stream.total_out != sizeof(delta_head)) { |
| 1208 | error("delta data unpack-initial failed"); |
| 1209 | return 0; |
| 1210 | } |
| 1211 | |
| 1212 | /* Examine the initial part of the delta to figure out |
| 1213 | * the result size. |
| 1214 | */ |
| 1215 | data = delta_head; |
| 1216 | |
| 1217 | /* ignore base size */ |
| 1218 | get_delta_hdr_size_sz(&data, delta_head+sizeof(delta_head)); |
| 1219 | |
| 1220 | /* Read the result size */ |
| 1221 | return get_delta_hdr_size_sz(&data, delta_head+sizeof(delta_head)); |
| 1222 | } |
| 1223 | |
| 1224 | unsigned long get_size_from_delta(struct packed_git *p, |
| 1225 | struct pack_window **w_curs, |
| 1226 | off_t curpos) |
| 1227 | { |
| 1228 | size_t size = get_size_from_delta_sz(p, w_curs, curpos); |
| 1229 | return cast_size_t_to_ulong(size); |
| 1230 | } |
| 1231 | |
| 1232 | int unpack_object_header(struct packed_git *p, |
| 1233 | struct pack_window **w_curs, |
| 1234 | off_t *curpos, |
| 1235 | size_t *sizep) |
| 1236 | { |
| 1237 | unsigned char *base; |
| 1238 | unsigned long left; |
| 1239 | unsigned long used; |
| 1240 | enum object_type type; |
| 1241 | |
| 1242 | /* use_pack() assures us we have [base, base + 20) available |
| 1243 | * as a range that we can look at. (Its actually the hash |
| 1244 | * size that is assured.) With our object header encoding |
| 1245 | * the maximum deflated object size is 2^137, which is just |
| 1246 | * insane, so we know won't exceed what we have been given. |
| 1247 | */ |
| 1248 | base = use_pack(p, w_curs, *curpos, &left); |
| 1249 | used = unpack_object_header_buffer(base, left, &type, sizep); |
| 1250 | if (!used) { |
| 1251 | type = OBJ_BAD; |
| 1252 | } else |
| 1253 | *curpos += used; |
| 1254 | |
| 1255 | return type; |
| 1256 | } |
| 1257 | |
| 1258 | void mark_bad_packed_object(struct packed_git *p, const struct object_id *oid) |
| 1259 | { |
| 1260 | oidset_insert(&p->bad_objects, oid); |
| 1261 | } |
| 1262 | |
| 1263 | const struct packed_git *has_packed_and_bad(struct repository *r, |
| 1264 | const struct object_id *oid) |
| 1265 | { |
| 1266 | struct odb_source *source; |
| 1267 | |
| 1268 | for (source = r->objects->sources; source; source = source->next) { |
| 1269 | struct odb_source_files *files = odb_source_files_downcast(source); |
| 1270 | struct packfile_list_entry *e; |
| 1271 | |
| 1272 | for (e = files->packed->packs.head; e; e = e->next) |
| 1273 | if (oidset_contains(&e->pack->bad_objects, oid)) |
| 1274 | return e->pack; |
| 1275 | } |
| 1276 | |
| 1277 | return NULL; |
| 1278 | } |
| 1279 | |
| 1280 | off_t get_delta_base(struct packed_git *p, |
| 1281 | struct pack_window **w_curs, |
| 1282 | off_t *curpos, |
| 1283 | enum object_type type, |
| 1284 | off_t delta_obj_offset) |
| 1285 | { |
| 1286 | unsigned char *base_info = use_pack(p, w_curs, *curpos, NULL); |
| 1287 | off_t base_offset; |
| 1288 | |
| 1289 | /* use_pack() assured us we have [base_info, base_info + 20) |
| 1290 | * as a range that we can look at without walking off the |
| 1291 | * end of the mapped window. Its actually the hash size |
| 1292 | * that is assured. An OFS_DELTA longer than the hash size |
| 1293 | * is stupid, as then a REF_DELTA would be smaller to store. |
| 1294 | */ |
| 1295 | if (type == OBJ_OFS_DELTA) { |
| 1296 | unsigned used = 0; |
| 1297 | unsigned char c = base_info[used++]; |
| 1298 | base_offset = c & 127; |
| 1299 | while (c & 128) { |
| 1300 | base_offset += 1; |
| 1301 | if (!base_offset || MSB(base_offset, 7)) |
| 1302 | return 0; /* overflow */ |
| 1303 | c = base_info[used++]; |
| 1304 | base_offset = (base_offset << 7) + (c & 127); |
| 1305 | } |
| 1306 | base_offset = delta_obj_offset - base_offset; |
| 1307 | if (base_offset <= 0 || base_offset >= delta_obj_offset) |
| 1308 | return 0; /* out of bound */ |
| 1309 | *curpos += used; |
| 1310 | } else if (type == OBJ_REF_DELTA) { |
| 1311 | /* The base entry _must_ be in the same pack */ |
| 1312 | struct object_id oid; |
| 1313 | oidread(&oid, base_info, p->repo->hash_algo); |
| 1314 | base_offset = find_pack_entry_one(&oid, p); |
| 1315 | *curpos += p->repo->hash_algo->rawsz; |
| 1316 | } else |
| 1317 | die("I am totally screwed"); |
| 1318 | return base_offset; |
| 1319 | } |
| 1320 | |
| 1321 | /* |
| 1322 | * Like get_delta_base above, but we return the sha1 instead of the pack |
| 1323 | * offset. This means it is cheaper for REF deltas (we do not have to do |
| 1324 | * the final object lookup), but more expensive for OFS deltas (we |
| 1325 | * have to load the revidx to convert the offset back into a sha1). |
| 1326 | */ |
| 1327 | static int get_delta_base_oid(struct packed_git *p, |
| 1328 | struct pack_window **w_curs, |
| 1329 | off_t curpos, |
| 1330 | struct object_id *oid, |
| 1331 | enum object_type type, |
| 1332 | off_t delta_obj_offset) |
| 1333 | { |
| 1334 | if (type == OBJ_REF_DELTA) { |
| 1335 | unsigned char *base = use_pack(p, w_curs, curpos, NULL); |
| 1336 | oidread(oid, base, p->repo->hash_algo); |
| 1337 | return 0; |
| 1338 | } else if (type == OBJ_OFS_DELTA) { |
| 1339 | uint32_t base_pos; |
| 1340 | off_t base_offset = get_delta_base(p, w_curs, &curpos, |
| 1341 | type, delta_obj_offset); |
| 1342 | |
| 1343 | if (!base_offset) |
| 1344 | return -1; |
| 1345 | |
| 1346 | if (offset_to_pack_pos(p, base_offset, &base_pos) < 0) |
| 1347 | return -1; |
| 1348 | |
| 1349 | return nth_packed_object_id(oid, p, |
| 1350 | pack_pos_to_index(p, base_pos)); |
| 1351 | } else |
| 1352 | return -1; |
| 1353 | } |
| 1354 | |
| 1355 | static int retry_bad_packed_offset(struct repository *r, |
| 1356 | struct packed_git *p, |
| 1357 | off_t obj_offset) |
| 1358 | { |
| 1359 | int type; |
| 1360 | uint32_t pos; |
| 1361 | struct object_id oid; |
| 1362 | if (offset_to_pack_pos(p, obj_offset, &pos) < 0) |
| 1363 | return OBJ_BAD; |
| 1364 | nth_packed_object_id(&oid, p, pack_pos_to_index(p, pos)); |
| 1365 | mark_bad_packed_object(p, &oid); |
| 1366 | type = odb_read_object_info(r->objects, &oid, NULL); |
| 1367 | if (type <= OBJ_NONE) |
| 1368 | return OBJ_BAD; |
| 1369 | return type; |
| 1370 | } |
| 1371 | |
| 1372 | #define POI_STACK_PREALLOC 64 |
| 1373 | |
| 1374 | static enum object_type packed_to_object_type(struct repository *r, |
| 1375 | struct packed_git *p, |
| 1376 | off_t obj_offset, |
| 1377 | enum object_type type, |
| 1378 | struct pack_window **w_curs, |
| 1379 | off_t curpos) |
| 1380 | { |
| 1381 | off_t small_poi_stack[POI_STACK_PREALLOC]; |
| 1382 | off_t *poi_stack = small_poi_stack; |
| 1383 | int poi_stack_nr = 0, poi_stack_alloc = POI_STACK_PREALLOC; |
| 1384 | |
| 1385 | while (type == OBJ_OFS_DELTA || type == OBJ_REF_DELTA) { |
| 1386 | off_t base_offset; |
| 1387 | size_t size; |
| 1388 | /* Push the object we're going to leave behind */ |
| 1389 | if (poi_stack_nr >= poi_stack_alloc && poi_stack == small_poi_stack) { |
| 1390 | poi_stack_alloc = alloc_nr(poi_stack_nr); |
| 1391 | ALLOC_ARRAY(poi_stack, poi_stack_alloc); |
| 1392 | COPY_ARRAY(poi_stack, small_poi_stack, poi_stack_nr); |
| 1393 | } else { |
| 1394 | ALLOC_GROW(poi_stack, poi_stack_nr+1, poi_stack_alloc); |
| 1395 | } |
| 1396 | poi_stack[poi_stack_nr++] = obj_offset; |
| 1397 | /* If parsing the base offset fails, just unwind */ |
| 1398 | base_offset = get_delta_base(p, w_curs, &curpos, type, obj_offset); |
| 1399 | if (!base_offset) |
| 1400 | goto unwind; |
| 1401 | curpos = obj_offset = base_offset; |
| 1402 | type = unpack_object_header(p, w_curs, &curpos, &size); |
| 1403 | if (type <= OBJ_NONE) { |
| 1404 | /* If getting the base itself fails, we first |
| 1405 | * retry the base, otherwise unwind */ |
| 1406 | type = retry_bad_packed_offset(r, p, base_offset); |
| 1407 | if (type > OBJ_NONE) |
| 1408 | goto out; |
| 1409 | goto unwind; |
| 1410 | } |
| 1411 | } |
| 1412 | |
| 1413 | switch (type) { |
| 1414 | case OBJ_BAD: |
| 1415 | case OBJ_COMMIT: |
| 1416 | case OBJ_TREE: |
| 1417 | case OBJ_BLOB: |
| 1418 | case OBJ_TAG: |
| 1419 | break; |
| 1420 | default: |
| 1421 | error("unknown object type %i at offset %"PRIuMAX" in %s", |
| 1422 | type, (uintmax_t)obj_offset, p->pack_name); |
| 1423 | type = OBJ_BAD; |
| 1424 | } |
| 1425 | |
| 1426 | out: |
| 1427 | if (poi_stack != small_poi_stack) |
| 1428 | free(poi_stack); |
| 1429 | return type; |
| 1430 | |
| 1431 | unwind: |
| 1432 | while (poi_stack_nr) { |
| 1433 | obj_offset = poi_stack[--poi_stack_nr]; |
| 1434 | type = retry_bad_packed_offset(r, p, obj_offset); |
| 1435 | if (type > OBJ_NONE) |
| 1436 | goto out; |
| 1437 | } |
| 1438 | type = OBJ_BAD; |
| 1439 | goto out; |
| 1440 | } |
| 1441 | |
| 1442 | static struct hashmap delta_base_cache; |
| 1443 | static size_t delta_base_cached; |
| 1444 | |
| 1445 | static LIST_HEAD(delta_base_cache_lru); |
| 1446 | |
| 1447 | struct delta_base_cache_key { |
| 1448 | struct packed_git *p; |
| 1449 | off_t base_offset; |
| 1450 | }; |
| 1451 | |
| 1452 | struct delta_base_cache_entry { |
| 1453 | struct hashmap_entry ent; |
| 1454 | struct delta_base_cache_key key; |
| 1455 | struct list_head lru; |
| 1456 | void *data; |
| 1457 | unsigned long size; |
| 1458 | enum object_type type; |
| 1459 | }; |
| 1460 | |
| 1461 | static unsigned int pack_entry_hash(struct packed_git *p, off_t base_offset) |
| 1462 | { |
| 1463 | unsigned int hash; |
| 1464 | |
| 1465 | hash = (unsigned int)(intptr_t)p + (unsigned int)base_offset; |
| 1466 | hash += (hash >> 8) + (hash >> 16); |
| 1467 | return hash; |
| 1468 | } |
| 1469 | |
| 1470 | static struct delta_base_cache_entry * |
| 1471 | get_delta_base_cache_entry(struct packed_git *p, off_t base_offset) |
| 1472 | { |
| 1473 | struct hashmap_entry entry, *e; |
| 1474 | struct delta_base_cache_key key; |
| 1475 | |
| 1476 | if (!delta_base_cache.cmpfn) |
| 1477 | return NULL; |
| 1478 | |
| 1479 | hashmap_entry_init(&entry, pack_entry_hash(p, base_offset)); |
| 1480 | key.p = p; |
| 1481 | key.base_offset = base_offset; |
| 1482 | e = hashmap_get(&delta_base_cache, &entry, &key); |
| 1483 | return e ? container_of(e, struct delta_base_cache_entry, ent) : NULL; |
| 1484 | } |
| 1485 | |
| 1486 | static int delta_base_cache_key_eq(const struct delta_base_cache_key *a, |
| 1487 | const struct delta_base_cache_key *b) |
| 1488 | { |
| 1489 | return a->p == b->p && a->base_offset == b->base_offset; |
| 1490 | } |
| 1491 | |
| 1492 | static int delta_base_cache_hash_cmp(const void *cmp_data UNUSED, |
| 1493 | const struct hashmap_entry *va, |
| 1494 | const struct hashmap_entry *vb, |
| 1495 | const void *vkey) |
| 1496 | { |
| 1497 | const struct delta_base_cache_entry *a, *b; |
| 1498 | const struct delta_base_cache_key *key = vkey; |
| 1499 | |
| 1500 | a = container_of(va, const struct delta_base_cache_entry, ent); |
| 1501 | b = container_of(vb, const struct delta_base_cache_entry, ent); |
| 1502 | |
| 1503 | if (key) |
| 1504 | return !delta_base_cache_key_eq(&a->key, key); |
| 1505 | else |
| 1506 | return !delta_base_cache_key_eq(&a->key, &b->key); |
| 1507 | } |
| 1508 | |
| 1509 | static int in_delta_base_cache(struct packed_git *p, off_t base_offset) |
| 1510 | { |
| 1511 | return !!get_delta_base_cache_entry(p, base_offset); |
| 1512 | } |
| 1513 | |
| 1514 | /* |
| 1515 | * Remove the entry from the cache, but do _not_ free the associated |
| 1516 | * entry data. The caller takes ownership of the "data" buffer, and |
| 1517 | * should copy out any fields it wants before detaching. |
| 1518 | */ |
| 1519 | static void detach_delta_base_cache_entry(struct delta_base_cache_entry *ent) |
| 1520 | { |
| 1521 | hashmap_remove(&delta_base_cache, &ent->ent, &ent->key); |
| 1522 | list_del(&ent->lru); |
| 1523 | delta_base_cached -= ent->size; |
| 1524 | free(ent); |
| 1525 | } |
| 1526 | |
| 1527 | static void *cache_or_unpack_entry(struct repository *r, struct packed_git *p, |
| 1528 | off_t base_offset, unsigned long *base_size, |
| 1529 | enum object_type *type) |
| 1530 | { |
| 1531 | struct delta_base_cache_entry *ent; |
| 1532 | |
| 1533 | ent = get_delta_base_cache_entry(p, base_offset); |
| 1534 | if (!ent) |
| 1535 | return unpack_entry(r, p, base_offset, type, base_size); |
| 1536 | |
| 1537 | if (type) |
| 1538 | *type = ent->type; |
| 1539 | if (base_size) |
| 1540 | *base_size = ent->size; |
| 1541 | return xmemdupz(ent->data, ent->size); |
| 1542 | } |
| 1543 | |
| 1544 | static inline void release_delta_base_cache(struct delta_base_cache_entry *ent) |
| 1545 | { |
| 1546 | free(ent->data); |
| 1547 | detach_delta_base_cache_entry(ent); |
| 1548 | } |
| 1549 | |
| 1550 | void clear_delta_base_cache(void) |
| 1551 | { |
| 1552 | struct list_head *lru, *tmp; |
| 1553 | list_for_each_safe(lru, tmp, &delta_base_cache_lru) { |
| 1554 | struct delta_base_cache_entry *entry = |
| 1555 | list_entry(lru, struct delta_base_cache_entry, lru); |
| 1556 | release_delta_base_cache(entry); |
| 1557 | } |
| 1558 | } |
| 1559 | |
| 1560 | static void add_delta_base_cache(struct packed_git *p, off_t base_offset, |
| 1561 | void *base, unsigned long base_size, |
| 1562 | unsigned long delta_base_cache_limit, |
| 1563 | enum object_type type) |
| 1564 | { |
| 1565 | struct delta_base_cache_entry *ent; |
| 1566 | struct list_head *lru, *tmp; |
| 1567 | |
| 1568 | /* |
| 1569 | * Check required to avoid redundant entries when more than one thread |
| 1570 | * is unpacking the same object, in unpack_entry() (since its phases I |
| 1571 | * and III might run concurrently across multiple threads). |
| 1572 | */ |
| 1573 | if (in_delta_base_cache(p, base_offset)) { |
| 1574 | free(base); |
| 1575 | return; |
| 1576 | } |
| 1577 | |
| 1578 | delta_base_cached += base_size; |
| 1579 | |
| 1580 | list_for_each_safe(lru, tmp, &delta_base_cache_lru) { |
| 1581 | struct delta_base_cache_entry *f = |
| 1582 | list_entry(lru, struct delta_base_cache_entry, lru); |
| 1583 | if (delta_base_cached <= delta_base_cache_limit) |
| 1584 | break; |
| 1585 | release_delta_base_cache(f); |
| 1586 | } |
| 1587 | |
| 1588 | ent = xmalloc(sizeof(*ent)); |
| 1589 | ent->key.p = p; |
| 1590 | ent->key.base_offset = base_offset; |
| 1591 | ent->type = type; |
| 1592 | ent->data = base; |
| 1593 | ent->size = base_size; |
| 1594 | list_add_tail(&ent->lru, &delta_base_cache_lru); |
| 1595 | |
| 1596 | if (!delta_base_cache.cmpfn) |
| 1597 | hashmap_init(&delta_base_cache, delta_base_cache_hash_cmp, NULL, 0); |
| 1598 | hashmap_entry_init(&ent->ent, pack_entry_hash(p, base_offset)); |
| 1599 | hashmap_add(&delta_base_cache, &ent->ent); |
| 1600 | } |
| 1601 | |
| 1602 | static int packed_object_info_with_index_pos(struct packed_git *p, off_t obj_offset, |
| 1603 | uint32_t *maybe_index_pos, struct object_info *oi) |
| 1604 | { |
| 1605 | struct pack_window *w_curs = NULL; |
| 1606 | size_t size; |
| 1607 | off_t curpos = obj_offset; |
| 1608 | enum object_type type = OBJ_NONE; |
| 1609 | uint32_t pack_pos; |
| 1610 | int ret; |
| 1611 | |
| 1612 | /* |
| 1613 | * We always get the representation type, but only convert it to |
| 1614 | * a "real" type later if the caller is interested. |
| 1615 | */ |
| 1616 | if (oi->contentp) { |
| 1617 | *oi->contentp = cache_or_unpack_entry(p->repo, p, obj_offset, oi->sizep, |
| 1618 | &type); |
| 1619 | if (!*oi->contentp) |
| 1620 | type = OBJ_BAD; |
| 1621 | } else if (oi->sizep || oi->typep || oi->delta_base_oid) { |
| 1622 | type = unpack_object_header(p, &w_curs, &curpos, &size); |
| 1623 | } |
| 1624 | |
| 1625 | if (!oi->contentp && oi->sizep) { |
| 1626 | if (type == OBJ_OFS_DELTA || type == OBJ_REF_DELTA) { |
| 1627 | off_t tmp_pos = curpos; |
| 1628 | off_t base_offset = get_delta_base(p, &w_curs, &tmp_pos, |
| 1629 | type, obj_offset); |
| 1630 | if (!base_offset) { |
| 1631 | ret = -1; |
| 1632 | goto out; |
| 1633 | } |
| 1634 | /* |
| 1635 | * Use size_t variant to avoid die() on >4GB deltas. |
| 1636 | * oi->sizep is unsigned long, so truncation may occur, |
| 1637 | * but streaming code uses its own size_t tracking. |
| 1638 | */ |
| 1639 | size = get_size_from_delta_sz(p, &w_curs, tmp_pos); |
| 1640 | if (size == 0) { |
| 1641 | ret = -1; |
| 1642 | goto out; |
| 1643 | } |
| 1644 | } |
| 1645 | *oi->sizep = (unsigned long)size; |
| 1646 | } |
| 1647 | |
| 1648 | if (oi->disk_sizep || (oi->mtimep && p->is_cruft)) { |
| 1649 | if (offset_to_pack_pos(p, obj_offset, &pack_pos) < 0) { |
| 1650 | error("could not find object at offset %"PRIuMAX" " |
| 1651 | "in pack %s", (uintmax_t)obj_offset, p->pack_name); |
| 1652 | ret = -1; |
| 1653 | goto out; |
| 1654 | } |
| 1655 | } |
| 1656 | |
| 1657 | if (oi->disk_sizep) |
| 1658 | *oi->disk_sizep = pack_pos_to_offset(p, pack_pos + 1) - obj_offset; |
| 1659 | |
| 1660 | if (oi->mtimep) { |
| 1661 | if (p->is_cruft) { |
| 1662 | uint32_t index_pos; |
| 1663 | |
| 1664 | if (load_pack_mtimes(p) < 0) |
| 1665 | die(_("could not load .mtimes for cruft pack '%s'"), |
| 1666 | pack_basename(p)); |
| 1667 | |
| 1668 | if (maybe_index_pos) |
| 1669 | index_pos = *maybe_index_pos; |
| 1670 | else |
| 1671 | index_pos = pack_pos_to_index(p, pack_pos); |
| 1672 | |
| 1673 | *oi->mtimep = nth_packed_mtime(p, index_pos); |
| 1674 | } else { |
| 1675 | *oi->mtimep = p->mtime; |
| 1676 | } |
| 1677 | } |
| 1678 | |
| 1679 | if (oi->typep) { |
| 1680 | enum object_type ptot; |
| 1681 | ptot = packed_to_object_type(p->repo, p, obj_offset, |
| 1682 | type, &w_curs, curpos); |
| 1683 | if (oi->typep) |
| 1684 | *oi->typep = ptot; |
| 1685 | if (ptot < 0) { |
| 1686 | ret = -1; |
| 1687 | goto out; |
| 1688 | } |
| 1689 | } |
| 1690 | |
| 1691 | if (oi->delta_base_oid) { |
| 1692 | if (type == OBJ_OFS_DELTA || type == OBJ_REF_DELTA) { |
| 1693 | if (get_delta_base_oid(p, &w_curs, curpos, |
| 1694 | oi->delta_base_oid, |
| 1695 | type, obj_offset) < 0) { |
| 1696 | ret = -1; |
| 1697 | goto out; |
| 1698 | } |
| 1699 | } else |
| 1700 | oidclr(oi->delta_base_oid, p->repo->hash_algo); |
| 1701 | } |
| 1702 | |
| 1703 | oi->whence = OI_PACKED; |
| 1704 | oi->u.packed.offset = obj_offset; |
| 1705 | oi->u.packed.pack = p; |
| 1706 | |
| 1707 | switch (type) { |
| 1708 | case OBJ_NONE: |
| 1709 | oi->u.packed.type = PACKED_OBJECT_TYPE_UNKNOWN; |
| 1710 | break; |
| 1711 | case OBJ_REF_DELTA: |
| 1712 | oi->u.packed.type = PACKED_OBJECT_TYPE_REF_DELTA; |
| 1713 | break; |
| 1714 | case OBJ_OFS_DELTA: |
| 1715 | oi->u.packed.type = PACKED_OBJECT_TYPE_OFS_DELTA; |
| 1716 | break; |
| 1717 | default: |
| 1718 | oi->u.packed.type = PACKED_OBJECT_TYPE_FULL; |
| 1719 | break; |
| 1720 | } |
| 1721 | |
| 1722 | ret = 0; |
| 1723 | |
| 1724 | out: |
| 1725 | unuse_pack(&w_curs); |
| 1726 | return ret; |
| 1727 | } |
| 1728 | |
| 1729 | int packed_object_info(struct packed_git *p, off_t obj_offset, |
| 1730 | struct object_info *oi) |
| 1731 | { |
| 1732 | return packed_object_info_with_index_pos(p, obj_offset, NULL, oi); |
| 1733 | } |
| 1734 | |
| 1735 | static void *unpack_compressed_entry(struct packed_git *p, |
| 1736 | struct pack_window **w_curs, |
| 1737 | off_t curpos, |
| 1738 | unsigned long size) |
| 1739 | { |
| 1740 | int st; |
| 1741 | git_zstream stream; |
| 1742 | unsigned char *buffer, *in; |
| 1743 | |
| 1744 | buffer = xmallocz_gently(size); |
| 1745 | if (!buffer) |
| 1746 | return NULL; |
| 1747 | memset(&stream, 0, sizeof(stream)); |
| 1748 | stream.next_out = buffer; |
| 1749 | stream.avail_out = size + 1; |
| 1750 | |
| 1751 | git_inflate_init(&stream); |
| 1752 | do { |
| 1753 | in = use_pack(p, w_curs, curpos, &stream.avail_in); |
| 1754 | stream.next_in = in; |
| 1755 | /* |
| 1756 | * Note: we must ensure the window section returned by |
| 1757 | * use_pack() will be available throughout git_inflate()'s |
| 1758 | * unlocked execution. Please refer to the comment at |
| 1759 | * get_size_from_delta() to see how this is done. |
| 1760 | */ |
| 1761 | obj_read_unlock(); |
| 1762 | st = git_inflate(&stream, Z_FINISH); |
| 1763 | obj_read_lock(); |
| 1764 | if (!stream.avail_out) |
| 1765 | break; /* the payload is larger than it should be */ |
| 1766 | curpos += stream.next_in - in; |
| 1767 | } while (st == Z_OK || st == Z_BUF_ERROR); |
| 1768 | git_inflate_end(&stream); |
| 1769 | if ((st != Z_STREAM_END) || stream.total_out != size) { |
| 1770 | free(buffer); |
| 1771 | return NULL; |
| 1772 | } |
| 1773 | |
| 1774 | /* versions of zlib can clobber unconsumed portion of outbuf */ |
| 1775 | buffer[size] = '\0'; |
| 1776 | |
| 1777 | return buffer; |
| 1778 | } |
| 1779 | |
| 1780 | static void write_pack_access_log(struct packed_git *p, off_t obj_offset) |
| 1781 | { |
| 1782 | static struct trace_key pack_access = TRACE_KEY_INIT(PACK_ACCESS); |
| 1783 | trace_printf_key(&pack_access, "%s %"PRIuMAX"\n", |
| 1784 | p->pack_name, (uintmax_t)obj_offset); |
| 1785 | } |
| 1786 | |
| 1787 | int do_check_packed_object_crc; |
| 1788 | |
| 1789 | #define UNPACK_ENTRY_STACK_PREALLOC 64 |
| 1790 | struct unpack_entry_stack_ent { |
| 1791 | off_t obj_offset; |
| 1792 | off_t curpos; |
| 1793 | unsigned long size; |
| 1794 | }; |
| 1795 | |
| 1796 | void *unpack_entry(struct repository *r, struct packed_git *p, off_t obj_offset, |
| 1797 | enum object_type *final_type, unsigned long *final_size) |
| 1798 | { |
| 1799 | struct pack_window *w_curs = NULL; |
| 1800 | off_t curpos = obj_offset; |
| 1801 | void *data = NULL; |
| 1802 | size_t size; |
| 1803 | enum object_type type; |
| 1804 | struct unpack_entry_stack_ent small_delta_stack[UNPACK_ENTRY_STACK_PREALLOC]; |
| 1805 | struct unpack_entry_stack_ent *delta_stack = small_delta_stack; |
| 1806 | int delta_stack_nr = 0, delta_stack_alloc = UNPACK_ENTRY_STACK_PREALLOC; |
| 1807 | int base_from_cache = 0; |
| 1808 | |
| 1809 | prepare_repo_settings(p->repo); |
| 1810 | |
| 1811 | write_pack_access_log(p, obj_offset); |
| 1812 | |
| 1813 | /* PHASE 1: drill down to the innermost base object */ |
| 1814 | for (;;) { |
| 1815 | off_t base_offset; |
| 1816 | int i; |
| 1817 | struct delta_base_cache_entry *ent; |
| 1818 | |
| 1819 | ent = get_delta_base_cache_entry(p, curpos); |
| 1820 | if (ent) { |
| 1821 | type = ent->type; |
| 1822 | data = ent->data; |
| 1823 | size = ent->size; |
| 1824 | detach_delta_base_cache_entry(ent); |
| 1825 | base_from_cache = 1; |
| 1826 | break; |
| 1827 | } |
| 1828 | |
| 1829 | if (do_check_packed_object_crc && p->index_version > 1) { |
| 1830 | uint32_t pack_pos, index_pos; |
| 1831 | off_t len; |
| 1832 | |
| 1833 | if (offset_to_pack_pos(p, obj_offset, &pack_pos) < 0) { |
| 1834 | error("could not find object at offset %"PRIuMAX" in pack %s", |
| 1835 | (uintmax_t)obj_offset, p->pack_name); |
| 1836 | data = NULL; |
| 1837 | goto out; |
| 1838 | } |
| 1839 | |
| 1840 | len = pack_pos_to_offset(p, pack_pos + 1) - obj_offset; |
| 1841 | index_pos = pack_pos_to_index(p, pack_pos); |
| 1842 | if (check_pack_crc(p, &w_curs, obj_offset, len, index_pos)) { |
| 1843 | struct object_id oid; |
| 1844 | nth_packed_object_id(&oid, p, index_pos); |
| 1845 | error("bad packed object CRC for %s", |
| 1846 | oid_to_hex(&oid)); |
| 1847 | mark_bad_packed_object(p, &oid); |
| 1848 | data = NULL; |
| 1849 | goto out; |
| 1850 | } |
| 1851 | } |
| 1852 | |
| 1853 | type = unpack_object_header(p, &w_curs, &curpos, &size); |
| 1854 | if (type != OBJ_OFS_DELTA && type != OBJ_REF_DELTA) |
| 1855 | break; |
| 1856 | |
| 1857 | base_offset = get_delta_base(p, &w_curs, &curpos, type, obj_offset); |
| 1858 | if (!base_offset) { |
| 1859 | error("failed to validate delta base reference " |
| 1860 | "at offset %"PRIuMAX" from %s", |
| 1861 | (uintmax_t)curpos, p->pack_name); |
| 1862 | /* bail to phase 2, in hopes of recovery */ |
| 1863 | data = NULL; |
| 1864 | break; |
| 1865 | } |
| 1866 | |
| 1867 | /* push object, proceed to base */ |
| 1868 | if (delta_stack_nr >= delta_stack_alloc |
| 1869 | && delta_stack == small_delta_stack) { |
| 1870 | delta_stack_alloc = alloc_nr(delta_stack_nr); |
| 1871 | ALLOC_ARRAY(delta_stack, delta_stack_alloc); |
| 1872 | COPY_ARRAY(delta_stack, small_delta_stack, |
| 1873 | delta_stack_nr); |
| 1874 | } else { |
| 1875 | ALLOC_GROW(delta_stack, delta_stack_nr+1, delta_stack_alloc); |
| 1876 | } |
| 1877 | i = delta_stack_nr++; |
| 1878 | delta_stack[i].obj_offset = obj_offset; |
| 1879 | delta_stack[i].curpos = curpos; |
| 1880 | delta_stack[i].size = size; |
| 1881 | |
| 1882 | curpos = obj_offset = base_offset; |
| 1883 | } |
| 1884 | |
| 1885 | /* PHASE 2: handle the base */ |
| 1886 | switch (type) { |
| 1887 | case OBJ_OFS_DELTA: |
| 1888 | case OBJ_REF_DELTA: |
| 1889 | if (data) |
| 1890 | BUG("unpack_entry: left loop at a valid delta"); |
| 1891 | break; |
| 1892 | case OBJ_COMMIT: |
| 1893 | case OBJ_TREE: |
| 1894 | case OBJ_BLOB: |
| 1895 | case OBJ_TAG: |
| 1896 | if (!base_from_cache) |
| 1897 | data = unpack_compressed_entry(p, &w_curs, curpos, size); |
| 1898 | break; |
| 1899 | default: |
| 1900 | data = NULL; |
| 1901 | error("unknown object type %i at offset %"PRIuMAX" in %s", |
| 1902 | type, (uintmax_t)obj_offset, p->pack_name); |
| 1903 | } |
| 1904 | |
| 1905 | /* PHASE 3: apply deltas in order */ |
| 1906 | |
| 1907 | /* invariants: |
| 1908 | * 'data' holds the base data, or NULL if there was corruption |
| 1909 | */ |
| 1910 | while (delta_stack_nr) { |
| 1911 | void *delta_data; |
| 1912 | void *base = data; |
| 1913 | void *external_base = NULL; |
| 1914 | unsigned long delta_size, base_size = size; |
| 1915 | int i; |
| 1916 | off_t base_obj_offset = obj_offset; |
| 1917 | |
| 1918 | data = NULL; |
| 1919 | |
| 1920 | if (!base) { |
| 1921 | /* |
| 1922 | * We're probably in deep shit, but let's try to fetch |
| 1923 | * the required base anyway from another pack or loose. |
| 1924 | * This is costly but should happen only in the presence |
| 1925 | * of a corrupted pack, and is better than failing outright. |
| 1926 | */ |
| 1927 | uint32_t pos; |
| 1928 | struct object_id base_oid; |
| 1929 | if (!(offset_to_pack_pos(p, obj_offset, &pos))) { |
| 1930 | struct object_info oi = OBJECT_INFO_INIT; |
| 1931 | |
| 1932 | nth_packed_object_id(&base_oid, p, |
| 1933 | pack_pos_to_index(p, pos)); |
| 1934 | error("failed to read delta base object %s" |
| 1935 | " at offset %"PRIuMAX" from %s", |
| 1936 | oid_to_hex(&base_oid), (uintmax_t)obj_offset, |
| 1937 | p->pack_name); |
| 1938 | mark_bad_packed_object(p, &base_oid); |
| 1939 | |
| 1940 | oi.typep = &type; |
| 1941 | oi.sizep = &base_size; |
| 1942 | oi.contentp = &base; |
| 1943 | if (odb_read_object_info_extended(r->objects, &base_oid, |
| 1944 | &oi, 0) < 0) |
| 1945 | base = NULL; |
| 1946 | |
| 1947 | external_base = base; |
| 1948 | } |
| 1949 | } |
| 1950 | |
| 1951 | i = --delta_stack_nr; |
| 1952 | obj_offset = delta_stack[i].obj_offset; |
| 1953 | curpos = delta_stack[i].curpos; |
| 1954 | delta_size = delta_stack[i].size; |
| 1955 | |
| 1956 | if (!base) |
| 1957 | continue; |
| 1958 | |
| 1959 | delta_data = unpack_compressed_entry(p, &w_curs, curpos, delta_size); |
| 1960 | |
| 1961 | if (!delta_data) { |
| 1962 | error("failed to unpack compressed delta " |
| 1963 | "at offset %"PRIuMAX" from %s", |
| 1964 | (uintmax_t)curpos, p->pack_name); |
| 1965 | data = NULL; |
| 1966 | } else { |
| 1967 | unsigned long sz; |
| 1968 | data = patch_delta(base, base_size, delta_data, |
| 1969 | delta_size, &sz); |
| 1970 | size = sz; |
| 1971 | |
| 1972 | /* |
| 1973 | * We could not apply the delta; warn the user, but |
| 1974 | * keep going. Our failure will be noticed either in |
| 1975 | * the next iteration of the loop, or if this is the |
| 1976 | * final delta, in the caller when we return NULL. |
| 1977 | * Those code paths will take care of making a more |
| 1978 | * explicit warning and retrying with another copy of |
| 1979 | * the object. |
| 1980 | */ |
| 1981 | if (!data) |
| 1982 | error("failed to apply delta"); |
| 1983 | } |
| 1984 | |
| 1985 | /* |
| 1986 | * We delay adding `base` to the cache until the end of the loop |
| 1987 | * because unpack_compressed_entry() momentarily releases the |
| 1988 | * obj_read_mutex, giving another thread the chance to access |
| 1989 | * the cache. Therefore, if `base` was already there, this other |
| 1990 | * thread could free() it (e.g. to make space for another entry) |
| 1991 | * before we are done using it. |
| 1992 | */ |
| 1993 | if (!external_base) |
| 1994 | add_delta_base_cache(p, base_obj_offset, base, base_size, |
| 1995 | p->repo->settings.delta_base_cache_limit, |
| 1996 | type); |
| 1997 | |
| 1998 | free(delta_data); |
| 1999 | free(external_base); |
| 2000 | } |
| 2001 | |
| 2002 | if (final_type) |
| 2003 | *final_type = type; |
| 2004 | if (final_size) |
| 2005 | *final_size = size; |
| 2006 | |
| 2007 | out: |
| 2008 | unuse_pack(&w_curs); |
| 2009 | |
| 2010 | if (delta_stack != small_delta_stack) |
| 2011 | free(delta_stack); |
| 2012 | |
| 2013 | return data; |
| 2014 | } |
| 2015 | |
| 2016 | int bsearch_pack(const struct object_id *oid, const struct packed_git *p, uint32_t *result) |
| 2017 | { |
| 2018 | const unsigned char *index_fanout = p->index_data; |
| 2019 | const unsigned char *index_lookup; |
| 2020 | const unsigned int hashsz = p->repo->hash_algo->rawsz; |
| 2021 | int index_lookup_width; |
| 2022 | |
| 2023 | if (!index_fanout) |
| 2024 | BUG("bsearch_pack called without a valid pack-index"); |
| 2025 | |
| 2026 | index_lookup = index_fanout + 4 * 256; |
| 2027 | if (p->index_version == 1) { |
| 2028 | index_lookup_width = hashsz + 4; |
| 2029 | index_lookup += 4; |
| 2030 | } else { |
| 2031 | index_lookup_width = hashsz; |
| 2032 | index_fanout += 8; |
| 2033 | index_lookup += 8; |
| 2034 | } |
| 2035 | |
| 2036 | return bsearch_hash(oid->hash, (const uint32_t*)index_fanout, |
| 2037 | index_lookup, index_lookup_width, result); |
| 2038 | } |
| 2039 | |
| 2040 | int nth_packed_object_id(struct object_id *oid, |
| 2041 | struct packed_git *p, |
| 2042 | uint32_t n) |
| 2043 | { |
| 2044 | const unsigned char *index = p->index_data; |
| 2045 | const unsigned int hashsz = p->repo->hash_algo->rawsz; |
| 2046 | if (!index) { |
| 2047 | if (open_pack_index(p)) |
| 2048 | return -1; |
| 2049 | index = p->index_data; |
| 2050 | } |
| 2051 | if (n >= p->num_objects) |
| 2052 | return -1; |
| 2053 | index += 4 * 256; |
| 2054 | if (p->index_version == 1) { |
| 2055 | oidread(oid, index + st_add(st_mult(hashsz + 4, n), 4), |
| 2056 | p->repo->hash_algo); |
| 2057 | } else { |
| 2058 | index += 8; |
| 2059 | oidread(oid, index + st_mult(hashsz, n), p->repo->hash_algo); |
| 2060 | } |
| 2061 | return 0; |
| 2062 | } |
| 2063 | |
| 2064 | void check_pack_index_ptr(const struct packed_git *p, const void *vptr) |
| 2065 | { |
| 2066 | const unsigned char *ptr = vptr; |
| 2067 | const unsigned char *start = p->index_data; |
| 2068 | const unsigned char *end = start + p->index_size; |
| 2069 | if (ptr < start) |
| 2070 | die(_("offset before start of pack index for %s (corrupt index?)"), |
| 2071 | p->pack_name); |
| 2072 | /* No need to check for underflow; .idx files must be at least 8 bytes */ |
| 2073 | if (ptr >= end - 8) |
| 2074 | die(_("offset beyond end of pack index for %s (truncated index?)"), |
| 2075 | p->pack_name); |
| 2076 | } |
| 2077 | |
| 2078 | off_t nth_packed_object_offset(const struct packed_git *p, uint32_t n) |
| 2079 | { |
| 2080 | const unsigned char *index = p->index_data; |
| 2081 | const unsigned int hashsz = p->repo->hash_algo->rawsz; |
| 2082 | index += 4 * 256; |
| 2083 | if (p->index_version == 1) { |
| 2084 | return ntohl(*((uint32_t *)(index + st_mult(hashsz + 4, n)))); |
| 2085 | } else { |
| 2086 | uint32_t off; |
| 2087 | index += st_add(8, st_mult(p->num_objects, hashsz + 4)); |
| 2088 | off = ntohl(*((uint32_t *)(index + st_mult(4, n)))); |
| 2089 | if (!(off & 0x80000000)) |
| 2090 | return off; |
| 2091 | index += st_add(st_mult(p->num_objects, 4), |
| 2092 | st_mult(off & 0x7fffffff, 8)); |
| 2093 | check_pack_index_ptr(p, index); |
| 2094 | return get_be64(index); |
| 2095 | } |
| 2096 | } |
| 2097 | |
| 2098 | off_t find_pack_entry_one(const struct object_id *oid, |
| 2099 | struct packed_git *p) |
| 2100 | { |
| 2101 | const unsigned char *index = p->index_data; |
| 2102 | uint32_t result; |
| 2103 | |
| 2104 | if (!index) { |
| 2105 | if (open_pack_index(p)) |
| 2106 | return 0; |
| 2107 | } |
| 2108 | |
| 2109 | if (bsearch_pack(oid, p, &result)) |
| 2110 | return nth_packed_object_offset(p, result); |
| 2111 | return 0; |
| 2112 | } |
| 2113 | |
| 2114 | int is_pack_valid(struct packed_git *p) |
| 2115 | { |
| 2116 | /* An already open pack is known to be valid. */ |
| 2117 | if (p->pack_fd != -1) |
| 2118 | return 1; |
| 2119 | |
| 2120 | /* If the pack has one window completely covering the |
| 2121 | * file size, the pack is known to be valid even if |
| 2122 | * the descriptor is not currently open. |
| 2123 | */ |
| 2124 | if (p->windows) { |
| 2125 | struct pack_window *w = p->windows; |
| 2126 | |
| 2127 | if (!w->offset && w->len == p->pack_size) |
| 2128 | return 1; |
| 2129 | } |
| 2130 | |
| 2131 | /* Force the pack to open to prove its valid. */ |
| 2132 | return !open_packed_git(p); |
| 2133 | } |
| 2134 | |
| 2135 | static int fill_pack_entry(const struct object_id *oid, |
| 2136 | struct pack_entry *e, |
| 2137 | struct packed_git *p) |
| 2138 | { |
| 2139 | off_t offset; |
| 2140 | |
| 2141 | if (oidset_size(&p->bad_objects) && |
| 2142 | oidset_contains(&p->bad_objects, oid)) |
| 2143 | return 0; |
| 2144 | |
| 2145 | offset = find_pack_entry_one(oid, p); |
| 2146 | if (!offset) |
| 2147 | return 0; |
| 2148 | |
| 2149 | /* |
| 2150 | * We are about to tell the caller where they can locate the |
| 2151 | * requested object. We better make sure the packfile is |
| 2152 | * still here and can be accessed before supplying that |
| 2153 | * answer, as it may have been deleted since the index was |
| 2154 | * loaded! |
| 2155 | */ |
| 2156 | if (!is_pack_valid(p)) |
| 2157 | return 0; |
| 2158 | e->offset = offset; |
| 2159 | e->p = p; |
| 2160 | return 1; |
| 2161 | } |
| 2162 | |
| 2163 | static int find_pack_entry(struct packfile_store *store, |
| 2164 | const struct object_id *oid, |
| 2165 | struct pack_entry *e) |
| 2166 | { |
| 2167 | struct packfile_list_entry *l; |
| 2168 | |
| 2169 | packfile_store_prepare(store); |
| 2170 | if (store->midx && fill_midx_entry(store->midx, oid, e)) |
| 2171 | return 1; |
| 2172 | |
| 2173 | for (l = store->packs.head; l; l = l->next) { |
| 2174 | struct packed_git *p = l->pack; |
| 2175 | |
| 2176 | if (!p->multi_pack_index && fill_pack_entry(oid, e, p)) { |
| 2177 | if (!store->skip_mru_updates) |
| 2178 | packfile_list_prepend(&store->packs, p); |
| 2179 | return 1; |
| 2180 | } |
| 2181 | } |
| 2182 | |
| 2183 | return 0; |
| 2184 | } |
| 2185 | |
| 2186 | int packfile_store_freshen_object(struct packfile_store *store, |
| 2187 | const struct object_id *oid) |
| 2188 | { |
| 2189 | struct pack_entry e; |
| 2190 | if (!find_pack_entry(store, oid, &e)) |
| 2191 | return 0; |
| 2192 | if (e.p->is_cruft) |
| 2193 | return 0; |
| 2194 | if (e.p->freshened) |
| 2195 | return 1; |
| 2196 | if (utime(e.p->pack_name, NULL)) |
| 2197 | return 0; |
| 2198 | e.p->freshened = 1; |
| 2199 | return 1; |
| 2200 | } |
| 2201 | |
| 2202 | int packfile_store_read_object_info(struct packfile_store *store, |
| 2203 | const struct object_id *oid, |
| 2204 | struct object_info *oi, |
| 2205 | enum object_info_flags flags) |
| 2206 | { |
| 2207 | struct pack_entry e; |
| 2208 | int ret; |
| 2209 | |
| 2210 | /* |
| 2211 | * In case the first read didn't surface the object, we have to reload |
| 2212 | * packfiles. This may cause us to discover new packfiles that have |
| 2213 | * been added since the last time we have prepared the packfile store. |
| 2214 | */ |
| 2215 | if (flags & OBJECT_INFO_SECOND_READ) |
| 2216 | packfile_store_reprepare(store); |
| 2217 | |
| 2218 | if (!find_pack_entry(store, oid, &e)) |
| 2219 | return 1; |
| 2220 | |
| 2221 | /* |
| 2222 | * We know that the caller doesn't actually need the |
| 2223 | * information below, so return early. |
| 2224 | */ |
| 2225 | if (!oi) |
| 2226 | return 0; |
| 2227 | |
| 2228 | ret = packed_object_info(e.p, e.offset, oi); |
| 2229 | if (ret < 0) { |
| 2230 | mark_bad_packed_object(e.p, oid); |
| 2231 | return -1; |
| 2232 | } |
| 2233 | |
| 2234 | return 0; |
| 2235 | } |
| 2236 | |
| 2237 | static void maybe_invalidate_kept_pack_cache(struct packfile_store *store, |
| 2238 | unsigned flags) |
| 2239 | { |
| 2240 | if (!store->kept_cache.packs) |
| 2241 | return; |
| 2242 | if (store->kept_cache.flags == flags) |
| 2243 | return; |
| 2244 | FREE_AND_NULL(store->kept_cache.packs); |
| 2245 | store->kept_cache.flags = 0; |
| 2246 | } |
| 2247 | |
| 2248 | struct packed_git **packfile_store_get_kept_pack_cache(struct packfile_store *store, |
| 2249 | unsigned flags) |
| 2250 | { |
| 2251 | maybe_invalidate_kept_pack_cache(store, flags); |
| 2252 | |
| 2253 | if (!store->kept_cache.packs) { |
| 2254 | struct packed_git **packs = NULL; |
| 2255 | struct packfile_list_entry *e; |
| 2256 | size_t nr = 0, alloc = 0; |
| 2257 | |
| 2258 | /* |
| 2259 | * We want "all" packs here, because we need to cover ones that |
| 2260 | * are used by a midx, as well. We need to look in every one of |
| 2261 | * them (instead of the midx itself) to cover duplicates. It's |
| 2262 | * possible that an object is found in two packs that the midx |
| 2263 | * covers, one kept and one not kept, but the midx returns only |
| 2264 | * the non-kept version. |
| 2265 | */ |
| 2266 | for (e = packfile_store_get_packs(store); e; e = e->next) { |
| 2267 | struct packed_git *p = e->pack; |
| 2268 | |
| 2269 | if ((p->pack_keep && (flags & KEPT_PACK_ON_DISK)) || |
| 2270 | (p->pack_keep_in_core && (flags & KEPT_PACK_IN_CORE)) || |
| 2271 | (p->pack_keep_in_core_open && (flags & KEPT_PACK_IN_CORE_OPEN))) { |
| 2272 | ALLOC_GROW(packs, nr + 1, alloc); |
| 2273 | packs[nr++] = p; |
| 2274 | } |
| 2275 | } |
| 2276 | ALLOC_GROW(packs, nr + 1, alloc); |
| 2277 | packs[nr] = NULL; |
| 2278 | |
| 2279 | store->kept_cache.packs = packs; |
| 2280 | store->kept_cache.flags = flags; |
| 2281 | } |
| 2282 | |
| 2283 | return store->kept_cache.packs; |
| 2284 | } |
| 2285 | |
| 2286 | int has_object_pack(struct repository *r, const struct object_id *oid) |
| 2287 | { |
| 2288 | struct odb_source *source; |
| 2289 | struct pack_entry e; |
| 2290 | |
| 2291 | odb_prepare_alternates(r->objects); |
| 2292 | for (source = r->objects->sources; source; source = source->next) { |
| 2293 | struct odb_source_files *files = odb_source_files_downcast(source); |
| 2294 | int ret = find_pack_entry(files->packed, oid, &e); |
| 2295 | if (ret) |
| 2296 | return ret; |
| 2297 | } |
| 2298 | |
| 2299 | return 0; |
| 2300 | } |
| 2301 | |
| 2302 | int has_object_kept_pack(struct repository *r, const struct object_id *oid, |
| 2303 | unsigned flags) |
| 2304 | { |
| 2305 | struct odb_source *source; |
| 2306 | struct pack_entry e; |
| 2307 | |
| 2308 | for (source = r->objects->sources; source; source = source->next) { |
| 2309 | struct odb_source_files *files = odb_source_files_downcast(source); |
| 2310 | struct packed_git **cache; |
| 2311 | |
| 2312 | cache = packfile_store_get_kept_pack_cache(files->packed, flags); |
| 2313 | |
| 2314 | for (; *cache; cache++) { |
| 2315 | struct packed_git *p = *cache; |
| 2316 | if (fill_pack_entry(oid, &e, p)) |
| 2317 | return 1; |
| 2318 | } |
| 2319 | } |
| 2320 | |
| 2321 | return 0; |
| 2322 | } |
| 2323 | |
| 2324 | int for_each_object_in_pack(struct packed_git *p, |
| 2325 | each_packed_object_fn cb, void *data, |
| 2326 | enum odb_for_each_object_flags flags) |
| 2327 | { |
| 2328 | uint32_t i; |
| 2329 | int r = 0; |
| 2330 | |
| 2331 | if (flags & ODB_FOR_EACH_OBJECT_PACK_ORDER) { |
| 2332 | if (load_pack_revindex(p->repo, p)) |
| 2333 | return -1; |
| 2334 | } |
| 2335 | |
| 2336 | for (i = 0; i < p->num_objects; i++) { |
| 2337 | uint32_t index_pos; |
| 2338 | struct object_id oid; |
| 2339 | |
| 2340 | /* |
| 2341 | * We are iterating "i" from 0 up to num_objects, but its |
| 2342 | * meaning may be different, depending on the requested output |
| 2343 | * order: |
| 2344 | * |
| 2345 | * - in object-name order, it is the same as the index order |
| 2346 | * used by nth_packed_object_id(), so we can pass it |
| 2347 | * directly |
| 2348 | * |
| 2349 | * - in pack-order, it is pack position, which we must |
| 2350 | * convert to an index position in order to get the oid. |
| 2351 | */ |
| 2352 | if (flags & ODB_FOR_EACH_OBJECT_PACK_ORDER) |
| 2353 | index_pos = pack_pos_to_index(p, i); |
| 2354 | else |
| 2355 | index_pos = i; |
| 2356 | |
| 2357 | if (nth_packed_object_id(&oid, p, index_pos) < 0) |
| 2358 | return error("unable to get sha1 of object %u in %s", |
| 2359 | index_pos, p->pack_name); |
| 2360 | |
| 2361 | r = cb(&oid, p, index_pos, data); |
| 2362 | if (r) |
| 2363 | break; |
| 2364 | } |
| 2365 | return r; |
| 2366 | } |
| 2367 | |
| 2368 | struct packfile_store_for_each_object_wrapper_data { |
| 2369 | struct packfile_store *store; |
| 2370 | const struct object_info *request; |
| 2371 | odb_for_each_object_cb cb; |
| 2372 | void *cb_data; |
| 2373 | }; |
| 2374 | |
| 2375 | static int packfile_store_for_each_object_wrapper(const struct object_id *oid, |
| 2376 | struct packed_git *pack, |
| 2377 | uint32_t index_pos, |
| 2378 | void *cb_data) |
| 2379 | { |
| 2380 | struct packfile_store_for_each_object_wrapper_data *data = cb_data; |
| 2381 | |
| 2382 | if (data->request) { |
| 2383 | off_t offset = nth_packed_object_offset(pack, index_pos); |
| 2384 | struct object_info oi = *data->request; |
| 2385 | |
| 2386 | if (packed_object_info_with_index_pos(pack, offset, |
| 2387 | &index_pos, &oi) < 0) { |
| 2388 | mark_bad_packed_object(pack, oid); |
| 2389 | return -1; |
| 2390 | } |
| 2391 | |
| 2392 | return data->cb(oid, &oi, data->cb_data); |
| 2393 | } else { |
| 2394 | return data->cb(oid, NULL, data->cb_data); |
| 2395 | } |
| 2396 | } |
| 2397 | |
| 2398 | static int match_hash(unsigned len, const unsigned char *a, const unsigned char *b) |
| 2399 | { |
| 2400 | do { |
| 2401 | if (*a != *b) |
| 2402 | return 0; |
| 2403 | a++; |
| 2404 | b++; |
| 2405 | len -= 2; |
| 2406 | } while (len > 1); |
| 2407 | if (len) |
| 2408 | if ((*a ^ *b) & 0xf0) |
| 2409 | return 0; |
| 2410 | return 1; |
| 2411 | } |
| 2412 | |
| 2413 | static int for_each_prefixed_object_in_midx( |
| 2414 | struct packfile_store *store, |
| 2415 | struct multi_pack_index *m, |
| 2416 | const struct odb_for_each_object_options *opts, |
| 2417 | struct packfile_store_for_each_object_wrapper_data *data) |
| 2418 | { |
| 2419 | int ret; |
| 2420 | |
| 2421 | for (; m; m = m->base_midx) { |
| 2422 | uint32_t num, i, first = 0; |
| 2423 | int len = opts->prefix_hex_len > m->source->odb->repo->hash_algo->hexsz ? |
| 2424 | m->source->odb->repo->hash_algo->hexsz : opts->prefix_hex_len; |
| 2425 | |
| 2426 | if (!m->num_objects) |
| 2427 | continue; |
| 2428 | |
| 2429 | num = m->num_objects + m->num_objects_in_base; |
| 2430 | |
| 2431 | bsearch_one_midx(opts->prefix, m, &first); |
| 2432 | |
| 2433 | /* |
| 2434 | * At this point, "first" is the location of the lowest |
| 2435 | * object with an object name that could match "opts->prefix". |
| 2436 | * See if we have 0, 1 or more objects that actually match(es). |
| 2437 | */ |
| 2438 | for (i = first; i < num; i++) { |
| 2439 | const struct object_id *current = NULL; |
| 2440 | struct object_id oid; |
| 2441 | |
| 2442 | current = nth_midxed_object_oid(&oid, m, i); |
| 2443 | |
| 2444 | if (!match_hash(len, opts->prefix->hash, current->hash)) |
| 2445 | break; |
| 2446 | |
| 2447 | if (data->request) { |
| 2448 | struct object_info oi = *data->request; |
| 2449 | |
| 2450 | ret = packfile_store_read_object_info(store, current, |
| 2451 | &oi, 0); |
| 2452 | if (ret) |
| 2453 | goto out; |
| 2454 | |
| 2455 | ret = data->cb(&oid, &oi, data->cb_data); |
| 2456 | if (ret) |
| 2457 | goto out; |
| 2458 | } else { |
| 2459 | ret = data->cb(&oid, NULL, data->cb_data); |
| 2460 | if (ret) |
| 2461 | goto out; |
| 2462 | } |
| 2463 | } |
| 2464 | } |
| 2465 | |
| 2466 | ret = 0; |
| 2467 | |
| 2468 | out: |
| 2469 | return ret; |
| 2470 | } |
| 2471 | |
| 2472 | static int for_each_prefixed_object_in_pack( |
| 2473 | struct packfile_store *store, |
| 2474 | struct packed_git *p, |
| 2475 | const struct odb_for_each_object_options *opts, |
| 2476 | struct packfile_store_for_each_object_wrapper_data *data) |
| 2477 | { |
| 2478 | uint32_t num, i, first = 0; |
| 2479 | int len = opts->prefix_hex_len > p->repo->hash_algo->hexsz ? |
| 2480 | p->repo->hash_algo->hexsz : opts->prefix_hex_len; |
| 2481 | int ret; |
| 2482 | |
| 2483 | num = p->num_objects; |
| 2484 | bsearch_pack(opts->prefix, p, &first); |
| 2485 | |
| 2486 | /* |
| 2487 | * At this point, "first" is the location of the lowest object |
| 2488 | * with an object name that could match "bin_pfx". See if we have |
| 2489 | * 0, 1 or more objects that actually match(es). |
| 2490 | */ |
| 2491 | for (i = first; i < num; i++) { |
| 2492 | struct object_id oid; |
| 2493 | |
| 2494 | nth_packed_object_id(&oid, p, i); |
| 2495 | if (!match_hash(len, opts->prefix->hash, oid.hash)) |
| 2496 | break; |
| 2497 | |
| 2498 | if (data->request) { |
| 2499 | struct object_info oi = *data->request; |
| 2500 | |
| 2501 | ret = packfile_store_read_object_info(store, &oid, &oi, 0); |
| 2502 | if (ret) |
| 2503 | goto out; |
| 2504 | |
| 2505 | ret = data->cb(&oid, &oi, data->cb_data); |
| 2506 | if (ret) |
| 2507 | goto out; |
| 2508 | } else { |
| 2509 | ret = data->cb(&oid, NULL, data->cb_data); |
| 2510 | if (ret) |
| 2511 | goto out; |
| 2512 | } |
| 2513 | } |
| 2514 | |
| 2515 | ret = 0; |
| 2516 | |
| 2517 | out: |
| 2518 | return ret; |
| 2519 | } |
| 2520 | |
| 2521 | static int packfile_store_for_each_prefixed_object( |
| 2522 | struct packfile_store *store, |
| 2523 | const struct odb_for_each_object_options *opts, |
| 2524 | struct packfile_store_for_each_object_wrapper_data *data) |
| 2525 | { |
| 2526 | struct packfile_list_entry *e; |
| 2527 | struct multi_pack_index *m; |
| 2528 | bool pack_errors = false; |
| 2529 | int ret; |
| 2530 | |
| 2531 | if (opts->flags) |
| 2532 | BUG("flags unsupported"); |
| 2533 | |
| 2534 | store->skip_mru_updates = true; |
| 2535 | |
| 2536 | m = get_multi_pack_index(store->source); |
| 2537 | if (m) { |
| 2538 | ret = for_each_prefixed_object_in_midx(store, m, opts, data); |
| 2539 | if (ret) |
| 2540 | goto out; |
| 2541 | } |
| 2542 | |
| 2543 | for (e = packfile_store_get_packs(store); e; e = e->next) { |
| 2544 | if (e->pack->multi_pack_index) |
| 2545 | continue; |
| 2546 | |
| 2547 | if (open_pack_index(e->pack)) { |
| 2548 | pack_errors = true; |
| 2549 | continue; |
| 2550 | } |
| 2551 | |
| 2552 | if (!e->pack->num_objects) |
| 2553 | continue; |
| 2554 | |
| 2555 | ret = for_each_prefixed_object_in_pack(store, e->pack, opts, data); |
| 2556 | if (ret) |
| 2557 | goto out; |
| 2558 | } |
| 2559 | |
| 2560 | ret = 0; |
| 2561 | |
| 2562 | out: |
| 2563 | store->skip_mru_updates = false; |
| 2564 | if (!ret && pack_errors) |
| 2565 | ret = -1; |
| 2566 | return ret; |
| 2567 | } |
| 2568 | |
| 2569 | int packfile_store_for_each_object(struct packfile_store *store, |
| 2570 | const struct object_info *request, |
| 2571 | odb_for_each_object_cb cb, |
| 2572 | void *cb_data, |
| 2573 | const struct odb_for_each_object_options *opts) |
| 2574 | { |
| 2575 | struct packfile_store_for_each_object_wrapper_data data = { |
| 2576 | .store = store, |
| 2577 | .request = request, |
| 2578 | .cb = cb, |
| 2579 | .cb_data = cb_data, |
| 2580 | }; |
| 2581 | struct packfile_list_entry *e; |
| 2582 | int pack_errors = 0, ret; |
| 2583 | |
| 2584 | if (opts->prefix) |
| 2585 | return packfile_store_for_each_prefixed_object(store, opts, &data); |
| 2586 | |
| 2587 | store->skip_mru_updates = true; |
| 2588 | |
| 2589 | for (e = packfile_store_get_packs(store); e; e = e->next) { |
| 2590 | struct packed_git *p = e->pack; |
| 2591 | |
| 2592 | if ((opts->flags & ODB_FOR_EACH_OBJECT_LOCAL_ONLY) && !p->pack_local) |
| 2593 | continue; |
| 2594 | if ((opts->flags & ODB_FOR_EACH_OBJECT_PROMISOR_ONLY) && |
| 2595 | !p->pack_promisor) |
| 2596 | continue; |
| 2597 | if ((opts->flags & ODB_FOR_EACH_OBJECT_SKIP_IN_CORE_KEPT_PACKS) && |
| 2598 | p->pack_keep_in_core) |
| 2599 | continue; |
| 2600 | if ((opts->flags & ODB_FOR_EACH_OBJECT_SKIP_ON_DISK_KEPT_PACKS) && |
| 2601 | p->pack_keep) |
| 2602 | continue; |
| 2603 | if (open_pack_index(p)) { |
| 2604 | pack_errors = 1; |
| 2605 | continue; |
| 2606 | } |
| 2607 | |
| 2608 | ret = for_each_object_in_pack(p, packfile_store_for_each_object_wrapper, |
| 2609 | &data, opts->flags); |
| 2610 | if (ret) |
| 2611 | goto out; |
| 2612 | } |
| 2613 | |
| 2614 | ret = 0; |
| 2615 | |
| 2616 | out: |
| 2617 | store->skip_mru_updates = false; |
| 2618 | |
| 2619 | if (!ret && pack_errors) |
| 2620 | ret = -1; |
| 2621 | return ret; |
| 2622 | } |
| 2623 | |
| 2624 | static int extend_abbrev_len(const struct object_id *a, |
| 2625 | const struct object_id *b, |
| 2626 | unsigned *out) |
| 2627 | { |
| 2628 | unsigned len = oid_common_prefix_hexlen(a, b); |
| 2629 | if (len != hash_algos[a->algo].hexsz && len >= *out) |
| 2630 | *out = len + 1; |
| 2631 | return 0; |
| 2632 | } |
| 2633 | |
| 2634 | static void find_abbrev_len_for_midx(struct multi_pack_index *m, |
| 2635 | const struct object_id *oid, |
| 2636 | unsigned min_len, |
| 2637 | unsigned *out) |
| 2638 | { |
| 2639 | unsigned len = min_len; |
| 2640 | |
| 2641 | for (; m; m = m->base_midx) { |
| 2642 | int match = 0; |
| 2643 | uint32_t num, first = 0; |
| 2644 | struct object_id found_oid; |
| 2645 | |
| 2646 | if (!m->num_objects) |
| 2647 | continue; |
| 2648 | |
| 2649 | num = m->num_objects + m->num_objects_in_base; |
| 2650 | match = bsearch_one_midx(oid, m, &first); |
| 2651 | |
| 2652 | /* |
| 2653 | * first is now the position in the packfile where we |
| 2654 | * would insert the object ID if it does not exist (or the |
| 2655 | * position of the object ID if it does exist). Hence, we |
| 2656 | * consider a maximum of two objects nearby for the |
| 2657 | * abbreviation length. |
| 2658 | */ |
| 2659 | |
| 2660 | if (!match) { |
| 2661 | if (nth_midxed_object_oid(&found_oid, m, first)) |
| 2662 | extend_abbrev_len(&found_oid, oid, &len); |
| 2663 | } else if (first < num - 1) { |
| 2664 | if (nth_midxed_object_oid(&found_oid, m, first + 1)) |
| 2665 | extend_abbrev_len(&found_oid, oid, &len); |
| 2666 | } |
| 2667 | if (first > 0) { |
| 2668 | if (nth_midxed_object_oid(&found_oid, m, first - 1)) |
| 2669 | extend_abbrev_len(&found_oid, oid, &len); |
| 2670 | } |
| 2671 | } |
| 2672 | |
| 2673 | *out = len; |
| 2674 | } |
| 2675 | |
| 2676 | static void find_abbrev_len_for_pack(struct packed_git *p, |
| 2677 | const struct object_id *oid, |
| 2678 | unsigned min_len, |
| 2679 | unsigned *out) |
| 2680 | { |
| 2681 | int match; |
| 2682 | uint32_t num, first = 0; |
| 2683 | struct object_id found_oid; |
| 2684 | unsigned len = min_len; |
| 2685 | |
| 2686 | num = p->num_objects; |
| 2687 | match = bsearch_pack(oid, p, &first); |
| 2688 | |
| 2689 | /* |
| 2690 | * first is now the position in the packfile where we would insert |
| 2691 | * the object ID if it does not exist (or the position of mad->hash if |
| 2692 | * it does exist). Hence, we consider a maximum of two objects |
| 2693 | * nearby for the abbreviation length. |
| 2694 | */ |
| 2695 | if (!match) { |
| 2696 | if (!nth_packed_object_id(&found_oid, p, first)) |
| 2697 | extend_abbrev_len(&found_oid, oid, &len); |
| 2698 | } else if (first < num - 1) { |
| 2699 | if (!nth_packed_object_id(&found_oid, p, first + 1)) |
| 2700 | extend_abbrev_len(&found_oid, oid, &len); |
| 2701 | } |
| 2702 | if (first > 0) { |
| 2703 | if (!nth_packed_object_id(&found_oid, p, first - 1)) |
| 2704 | extend_abbrev_len(&found_oid, oid, &len); |
| 2705 | } |
| 2706 | |
| 2707 | *out = len; |
| 2708 | } |
| 2709 | |
| 2710 | int packfile_store_find_abbrev_len(struct packfile_store *store, |
| 2711 | const struct object_id *oid, |
| 2712 | unsigned min_len, |
| 2713 | unsigned *out) |
| 2714 | { |
| 2715 | struct packfile_list_entry *e; |
| 2716 | struct multi_pack_index *m; |
| 2717 | |
| 2718 | m = get_multi_pack_index(store->source); |
| 2719 | if (m) |
| 2720 | find_abbrev_len_for_midx(m, oid, min_len, &min_len); |
| 2721 | |
| 2722 | for (e = packfile_store_get_packs(store); e; e = e->next) { |
| 2723 | if (e->pack->multi_pack_index) |
| 2724 | continue; |
| 2725 | if (open_pack_index(e->pack) || !e->pack->num_objects) |
| 2726 | continue; |
| 2727 | |
| 2728 | find_abbrev_len_for_pack(e->pack, oid, min_len, &min_len); |
| 2729 | } |
| 2730 | |
| 2731 | *out = min_len; |
| 2732 | return 0; |
| 2733 | } |
| 2734 | |
| 2735 | struct add_promisor_object_data { |
| 2736 | struct repository *repo; |
| 2737 | struct oidset *set; |
| 2738 | }; |
| 2739 | |
| 2740 | static int add_promisor_object(const struct object_id *oid, |
| 2741 | struct object_info *oi UNUSED, |
| 2742 | void *cb_data) |
| 2743 | { |
| 2744 | struct add_promisor_object_data *data = cb_data; |
| 2745 | struct object *obj; |
| 2746 | int we_parsed_object; |
| 2747 | |
| 2748 | obj = lookup_object(data->repo, oid); |
| 2749 | if (obj && obj->parsed) { |
| 2750 | we_parsed_object = 0; |
| 2751 | } else { |
| 2752 | we_parsed_object = 1; |
| 2753 | obj = parse_object_with_flags(data->repo, oid, |
| 2754 | PARSE_OBJECT_SKIP_HASH_CHECK); |
| 2755 | } |
| 2756 | |
| 2757 | if (!obj) |
| 2758 | return 1; |
| 2759 | |
| 2760 | oidset_insert(data->set, oid); |
| 2761 | |
| 2762 | /* |
| 2763 | * If this is a tree, commit, or tag, the objects it refers |
| 2764 | * to are also promisor objects. (Blobs refer to no objects->) |
| 2765 | */ |
| 2766 | if (obj->type == OBJ_TREE) { |
| 2767 | struct tree *tree = (struct tree *)obj; |
| 2768 | struct tree_desc desc; |
| 2769 | struct name_entry entry; |
| 2770 | if (init_tree_desc_gently(&desc, &tree->object.oid, |
| 2771 | tree->buffer, tree->size, 0)) |
| 2772 | /* |
| 2773 | * Error messages are given when packs are |
| 2774 | * verified, so do not print any here. |
| 2775 | */ |
| 2776 | return 0; |
| 2777 | while (tree_entry_gently(&desc, &entry)) |
| 2778 | oidset_insert(data->set, &entry.oid); |
| 2779 | if (we_parsed_object) |
| 2780 | free_tree_buffer(tree); |
| 2781 | } else if (obj->type == OBJ_COMMIT) { |
| 2782 | struct commit *commit = (struct commit *) obj; |
| 2783 | struct commit_list *parents = commit->parents; |
| 2784 | |
| 2785 | oidset_insert(data->set, get_commit_tree_oid(commit)); |
| 2786 | for (; parents; parents = parents->next) |
| 2787 | oidset_insert(data->set, &parents->item->object.oid); |
| 2788 | } else if (obj->type == OBJ_TAG) { |
| 2789 | struct tag *tag = (struct tag *) obj; |
| 2790 | oidset_insert(data->set, get_tagged_oid(tag)); |
| 2791 | } |
| 2792 | return 0; |
| 2793 | } |
| 2794 | |
| 2795 | int is_promisor_object(struct repository *r, const struct object_id *oid) |
| 2796 | { |
| 2797 | static struct oidset promisor_objects; |
| 2798 | static int promisor_objects_prepared; |
| 2799 | |
| 2800 | if (!promisor_objects_prepared) { |
| 2801 | if (repo_has_promisor_remote(r)) { |
| 2802 | struct add_promisor_object_data data = { |
| 2803 | .repo = r, |
| 2804 | .set = &promisor_objects, |
| 2805 | }; |
| 2806 | |
| 2807 | odb_for_each_object(r->objects, NULL, add_promisor_object, &data, |
| 2808 | ODB_FOR_EACH_OBJECT_PROMISOR_ONLY | ODB_FOR_EACH_OBJECT_PACK_ORDER); |
| 2809 | } |
| 2810 | promisor_objects_prepared = 1; |
| 2811 | } |
| 2812 | return oidset_contains(&promisor_objects, oid); |
| 2813 | } |
| 2814 | |
| 2815 | int parse_pack_header_option(const char *in, unsigned char *out, unsigned int *len) |
| 2816 | { |
| 2817 | unsigned char *hdr; |
| 2818 | char *c; |
| 2819 | |
| 2820 | hdr = out; |
| 2821 | put_be32(hdr, PACK_SIGNATURE); |
| 2822 | hdr += 4; |
| 2823 | put_be32(hdr, strtoul(in, &c, 10)); |
| 2824 | hdr += 4; |
| 2825 | if (*c != ',') |
| 2826 | return -1; |
| 2827 | put_be32(hdr, strtoul(c + 1, &c, 10)); |
| 2828 | hdr += 4; |
| 2829 | if (*c) |
| 2830 | return -1; |
| 2831 | *len = hdr - out; |
| 2832 | return 0; |
| 2833 | } |
| 2834 | |
| 2835 | struct packfile_store *packfile_store_new(struct odb_source *source) |
| 2836 | { |
| 2837 | struct packfile_store *store; |
| 2838 | CALLOC_ARRAY(store, 1); |
| 2839 | store->source = source; |
| 2840 | strmap_init(&store->packs_by_path); |
| 2841 | return store; |
| 2842 | } |
| 2843 | |
| 2844 | void packfile_store_free(struct packfile_store *store) |
| 2845 | { |
| 2846 | for (struct packfile_list_entry *e = store->packs.head; e; e = e->next) |
| 2847 | free(e->pack); |
| 2848 | packfile_list_clear(&store->packs); |
| 2849 | |
| 2850 | strmap_clear(&store->packs_by_path, 0); |
| 2851 | free(store); |
| 2852 | } |
| 2853 | |
| 2854 | void packfile_store_close(struct packfile_store *store) |
| 2855 | { |
| 2856 | for (struct packfile_list_entry *e = store->packs.head; e; e = e->next) { |
| 2857 | if (e->pack->do_not_close) |
| 2858 | BUG("want to close pack marked 'do-not-close'"); |
| 2859 | close_pack(e->pack); |
| 2860 | } |
| 2861 | if (store->midx) |
| 2862 | close_midx(store->midx); |
| 2863 | store->midx = NULL; |
| 2864 | } |
| 2865 | |
| 2866 | struct odb_packed_read_stream { |
| 2867 | struct odb_read_stream base; |
| 2868 | struct packed_git *pack; |
| 2869 | git_zstream z; |
| 2870 | enum { |
| 2871 | ODB_PACKED_READ_STREAM_UNINITIALIZED, |
| 2872 | ODB_PACKED_READ_STREAM_INUSE, |
| 2873 | ODB_PACKED_READ_STREAM_DONE, |
| 2874 | ODB_PACKED_READ_STREAM_ERROR, |
| 2875 | } z_state; |
| 2876 | off_t pos; |
| 2877 | }; |
| 2878 | |
| 2879 | static ssize_t read_istream_pack_non_delta(struct odb_read_stream *_st, char *buf, |
| 2880 | size_t sz) |
| 2881 | { |
| 2882 | struct odb_packed_read_stream *st = (struct odb_packed_read_stream *)_st; |
| 2883 | size_t total_read = 0; |
| 2884 | |
| 2885 | switch (st->z_state) { |
| 2886 | case ODB_PACKED_READ_STREAM_UNINITIALIZED: |
| 2887 | memset(&st->z, 0, sizeof(st->z)); |
| 2888 | git_inflate_init(&st->z); |
| 2889 | st->z_state = ODB_PACKED_READ_STREAM_INUSE; |
| 2890 | break; |
| 2891 | case ODB_PACKED_READ_STREAM_DONE: |
| 2892 | return 0; |
| 2893 | case ODB_PACKED_READ_STREAM_ERROR: |
| 2894 | return -1; |
| 2895 | case ODB_PACKED_READ_STREAM_INUSE: |
| 2896 | break; |
| 2897 | } |
| 2898 | |
| 2899 | while (total_read < sz) { |
| 2900 | int status; |
| 2901 | struct pack_window *window = NULL; |
| 2902 | unsigned char *mapped; |
| 2903 | |
| 2904 | mapped = use_pack(st->pack, &window, |
| 2905 | st->pos, &st->z.avail_in); |
| 2906 | |
| 2907 | st->z.next_out = (unsigned char *)buf + total_read; |
| 2908 | st->z.avail_out = sz - total_read; |
| 2909 | st->z.next_in = mapped; |
| 2910 | status = git_inflate(&st->z, Z_FINISH); |
| 2911 | |
| 2912 | st->pos += st->z.next_in - mapped; |
| 2913 | total_read = st->z.next_out - (unsigned char *)buf; |
| 2914 | unuse_pack(&window); |
| 2915 | |
| 2916 | if (status == Z_STREAM_END) { |
| 2917 | git_inflate_end(&st->z); |
| 2918 | st->z_state = ODB_PACKED_READ_STREAM_DONE; |
| 2919 | break; |
| 2920 | } |
| 2921 | |
| 2922 | /* |
| 2923 | * Unlike the loose object case, we do not have to worry here |
| 2924 | * about running out of input bytes and spinning infinitely. If |
| 2925 | * we get Z_BUF_ERROR due to too few input bytes, then we'll |
| 2926 | * replenish them in the next use_pack() call when we loop. If |
| 2927 | * we truly hit the end of the pack (i.e., because it's corrupt |
| 2928 | * or truncated), then use_pack() catches that and will die(). |
| 2929 | */ |
| 2930 | if (status != Z_OK && status != Z_BUF_ERROR) { |
| 2931 | git_inflate_end(&st->z); |
| 2932 | st->z_state = ODB_PACKED_READ_STREAM_ERROR; |
| 2933 | return -1; |
| 2934 | } |
| 2935 | } |
| 2936 | return total_read; |
| 2937 | } |
| 2938 | |
| 2939 | static int close_istream_pack_non_delta(struct odb_read_stream *_st) |
| 2940 | { |
| 2941 | struct odb_packed_read_stream *st = (struct odb_packed_read_stream *)_st; |
| 2942 | if (st->z_state == ODB_PACKED_READ_STREAM_INUSE) |
| 2943 | git_inflate_end(&st->z); |
| 2944 | return 0; |
| 2945 | } |
| 2946 | |
| 2947 | int packfile_read_object_stream(struct odb_read_stream **out, |
| 2948 | const struct object_id *oid, |
| 2949 | struct packed_git *pack, |
| 2950 | off_t offset) |
| 2951 | { |
| 2952 | struct odb_packed_read_stream *stream; |
| 2953 | struct pack_window *window = NULL; |
| 2954 | enum object_type in_pack_type; |
| 2955 | size_t size; |
| 2956 | |
| 2957 | in_pack_type = unpack_object_header(pack, &window, &offset, &size); |
| 2958 | unuse_pack(&window); |
| 2959 | |
| 2960 | if (repo_settings_get_big_file_threshold(pack->repo) >= size) |
| 2961 | return -1; |
| 2962 | |
| 2963 | switch (in_pack_type) { |
| 2964 | default: |
| 2965 | return -1; /* we do not do deltas for now */ |
| 2966 | case OBJ_BAD: |
| 2967 | mark_bad_packed_object(pack, oid); |
| 2968 | return -1; |
| 2969 | case OBJ_COMMIT: |
| 2970 | case OBJ_TREE: |
| 2971 | case OBJ_BLOB: |
| 2972 | case OBJ_TAG: |
| 2973 | break; |
| 2974 | } |
| 2975 | |
| 2976 | CALLOC_ARRAY(stream, 1); |
| 2977 | stream->base.close = close_istream_pack_non_delta; |
| 2978 | stream->base.read = read_istream_pack_non_delta; |
| 2979 | stream->base.type = in_pack_type; |
| 2980 | stream->base.size = size; |
| 2981 | stream->z_state = ODB_PACKED_READ_STREAM_UNINITIALIZED; |
| 2982 | stream->pack = pack; |
| 2983 | stream->pos = offset; |
| 2984 | |
| 2985 | *out = &stream->base; |
| 2986 | |
| 2987 | return 0; |
| 2988 | } |
| 2989 | |
| 2990 | int packfile_store_read_object_stream(struct odb_read_stream **out, |
| 2991 | struct packfile_store *store, |
| 2992 | const struct object_id *oid) |
| 2993 | { |
| 2994 | struct pack_entry e; |
| 2995 | |
| 2996 | if (!find_pack_entry(store, oid, &e)) |
| 2997 | return -1; |
| 2998 | |
| 2999 | return packfile_read_object_stream(out, oid, e.p, e.offset); |
| 3000 | } |