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