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