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