at commit a0189536

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 size_t *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, 1);
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 size_t unpack_object_header_buffer(const unsigned char *buf, size_t len,
870 enum object_type *type, size_t *sizep)
871 {
872 unsigned shift;
873 size_t size, c, used = 0;
874
875 c = buf[used++];
876 *type = (c >> 4) & 7;
877 size = c & 15;
878 shift = 4;
879 while (c & 0x80) {
880 /*
881 * Each continuation byte adds 7 bits. Ensure shift won't
882 * overflow size_t (use size_t not long for 64-bit on Windows).
883 */
884 if (len <= used || (bitsizeof(size_t) - 7) < shift) {
885 error("bad object header");
886 size = used = 0;
887 break;
888 }
889 c = buf[used++];
890 size = st_add(size, st_left_shift(c & 0x7f, shift));
891 shift += 7;
892 }
893 *sizep = size;
894 return used;
895 }
896
897 /*
898 * Read a delta object's header at curpos in p (already inflated as needed)
899 * and return the size of the result object (the post-application target).
900 */
901 size_t get_size_from_delta(struct packed_git *p,
902 struct pack_window **w_curs,
903 off_t curpos)
904 {
905 const unsigned char *data;
906 unsigned char delta_head[20], *in;
907 git_zstream stream;
908 int st;
909
910 memset(&stream, 0, sizeof(stream));
911 stream.next_out = delta_head;
912 stream.avail_out = sizeof(delta_head);
913
914 git_inflate_init(&stream);
915 do {
916 in = use_pack(p, w_curs, curpos, &stream.avail_in);
917 stream.next_in = in;
918 /*
919 * Note: the window section returned by use_pack() must be
920 * available throughout git_inflate()'s unlocked execution. To
921 * ensure no other thread will modify the window in the
922 * meantime, we rely on the packed_window.inuse_cnt. This
923 * counter is incremented before window reading and checked
924 * before window disposal.
925 *
926 * Other worrying sections could be the call to close_pack_fd(),
927 * which can close packs even with in-use windows, and to
928 * odb_reprepare(). Regarding the former, mmap doc says:
929 * "closing the file descriptor does not unmap the region". And
930 * for the latter, it won't re-open already available packs.
931 */
932 obj_read_unlock();
933 st = git_inflate(&stream, Z_FINISH);
934 obj_read_lock();
935 curpos += stream.next_in - in;
936 } while ((st == Z_OK || st == Z_BUF_ERROR) &&
937 stream.total_out < sizeof(delta_head));
938 git_inflate_end(&stream);
939 if ((st != Z_STREAM_END) && stream.total_out != sizeof(delta_head)) {
940 error("delta data unpack-initial failed");
941 return 0;
942 }
943
944 /* Examine the initial part of the delta to figure out
945 * the result size.
946 */
947 data = delta_head;
948
949 /* ignore base size */
950 get_delta_hdr_size(&data, delta_head+sizeof(delta_head));
951
952 /* Read the result size */
953 return get_delta_hdr_size(&data, delta_head+sizeof(delta_head));
954 }
955
956 int unpack_object_header(struct packed_git *p,
957 struct pack_window **w_curs,
958 off_t *curpos,
959 size_t *sizep)
960 {
961 unsigned char *base;
962 size_t left, used;
963 enum object_type type;
964
965 /* use_pack() assures us we have [base, base + 20) available
966 * as a range that we can look at. (Its actually the hash
967 * size that is assured.) With our object header encoding
968 * the maximum deflated object size is 2^137, which is just
969 * insane, so we know won't exceed what we have been given.
970 */
971 base = use_pack(p, w_curs, *curpos, &left);
972 used = unpack_object_header_buffer(base, left, &type, sizep);
973 if (!used) {
974 type = OBJ_BAD;
975 } else
976 *curpos += used;
977
978 return type;
979 }
980
981 void mark_bad_packed_object(struct packed_git *p, const struct object_id *oid)
982 {
983 oidset_insert(&p->bad_objects, oid);
984 }
985
986 off_t get_delta_base(struct packed_git *p,
987 struct pack_window **w_curs,
988 off_t *curpos,
989 enum object_type type,
990 off_t delta_obj_offset)
991 {
992 unsigned char *base_info = use_pack(p, w_curs, *curpos, NULL);
993 off_t base_offset;
994
995 /* use_pack() assured us we have [base_info, base_info + 20)
996 * as a range that we can look at without walking off the
997 * end of the mapped window. Its actually the hash size
998 * that is assured. An OFS_DELTA longer than the hash size
999 * is stupid, as then a REF_DELTA would be smaller to store.
1000 */
1001 if (type == OBJ_OFS_DELTA) {
1002 unsigned used = 0;
1003 unsigned char c = base_info[used++];
1004 base_offset = c & 127;
1005 while (c & 128) {
1006 base_offset += 1;
1007 if (!base_offset || MSB(base_offset, 7))
1008 return 0; /* overflow */
1009 c = base_info[used++];
1010 base_offset = (base_offset << 7) + (c & 127);
1011 }
1012 base_offset = delta_obj_offset - base_offset;
1013 if (base_offset <= 0 || base_offset >= delta_obj_offset)
1014 return 0; /* out of bound */
1015 *curpos += used;
1016 } else if (type == OBJ_REF_DELTA) {
1017 /* The base entry _must_ be in the same pack */
1018 struct object_id oid;
1019 oidread(&oid, base_info, p->repo->hash_algo);
1020 base_offset = find_pack_entry_one(&oid, p);
1021 *curpos += p->repo->hash_algo->rawsz;
1022 } else
1023 die("I am totally screwed");
1024 return base_offset;
1025 }
1026
1027 /*
1028 * Like get_delta_base above, but we return the sha1 instead of the pack
1029 * offset. This means it is cheaper for REF deltas (we do not have to do
1030 * the final object lookup), but more expensive for OFS deltas (we
1031 * have to load the revidx to convert the offset back into a sha1).
1032 */
1033 static int get_delta_base_oid(struct packed_git *p,
1034 struct pack_window **w_curs,
1035 off_t curpos,
1036 struct object_id *oid,
1037 enum object_type type,
1038 off_t delta_obj_offset)
1039 {
1040 if (type == OBJ_REF_DELTA) {
1041 unsigned char *base = use_pack(p, w_curs, curpos, NULL);
1042 oidread(oid, base, p->repo->hash_algo);
1043 return 0;
1044 } else if (type == OBJ_OFS_DELTA) {
1045 uint32_t base_pos;
1046 off_t base_offset = get_delta_base(p, w_curs, &curpos,
1047 type, delta_obj_offset);
1048
1049 if (!base_offset)
1050 return -1;
1051
1052 if (offset_to_pack_pos(p, base_offset, &base_pos) < 0)
1053 return -1;
1054
1055 return nth_packed_object_id(oid, p,
1056 pack_pos_to_index(p, base_pos));
1057 } else
1058 return -1;
1059 }
1060
1061 static int retry_bad_packed_offset(struct repository *r,
1062 struct packed_git *p,
1063 off_t obj_offset)
1064 {
1065 int type;
1066 uint32_t pos;
1067 struct object_id oid;
1068 if (offset_to_pack_pos(p, obj_offset, &pos) < 0)
1069 return OBJ_BAD;
1070 nth_packed_object_id(&oid, p, pack_pos_to_index(p, pos));
1071 mark_bad_packed_object(p, &oid);
1072 type = odb_read_object_info(r->objects, &oid, NULL);
1073 if (type <= OBJ_NONE)
1074 return OBJ_BAD;
1075 return type;
1076 }
1077
1078 #define POI_STACK_PREALLOC 64
1079
1080 static enum object_type packed_to_object_type(struct repository *r,
1081 struct packed_git *p,
1082 off_t obj_offset,
1083 enum object_type type,
1084 struct pack_window **w_curs,
1085 off_t curpos)
1086 {
1087 off_t small_poi_stack[POI_STACK_PREALLOC];
1088 off_t *poi_stack = small_poi_stack;
1089 int poi_stack_nr = 0, poi_stack_alloc = POI_STACK_PREALLOC;
1090
1091 while (type == OBJ_OFS_DELTA || type == OBJ_REF_DELTA) {
1092 off_t base_offset;
1093 size_t size;
1094 /* Push the object we're going to leave behind */
1095 if (poi_stack_nr >= poi_stack_alloc && poi_stack == small_poi_stack) {
1096 poi_stack_alloc = alloc_nr(poi_stack_nr);
1097 ALLOC_ARRAY(poi_stack, poi_stack_alloc);
1098 COPY_ARRAY(poi_stack, small_poi_stack, poi_stack_nr);
1099 } else {
1100 ALLOC_GROW(poi_stack, poi_stack_nr+1, poi_stack_alloc);
1101 }
1102 poi_stack[poi_stack_nr++] = obj_offset;
1103 /* If parsing the base offset fails, just unwind */
1104 base_offset = get_delta_base(p, w_curs, &curpos, type, obj_offset);
1105 if (!base_offset)
1106 goto unwind;
1107 curpos = obj_offset = base_offset;
1108 type = unpack_object_header(p, w_curs, &curpos, &size);
1109 if (type <= OBJ_NONE) {
1110 /* If getting the base itself fails, we first
1111 * retry the base, otherwise unwind */
1112 type = retry_bad_packed_offset(r, p, base_offset);
1113 if (type > OBJ_NONE)
1114 goto out;
1115 goto unwind;
1116 }
1117 }
1118
1119 switch (type) {
1120 case OBJ_BAD:
1121 case OBJ_COMMIT:
1122 case OBJ_TREE:
1123 case OBJ_BLOB:
1124 case OBJ_TAG:
1125 break;
1126 default:
1127 error("unknown object type %i at offset %"PRIuMAX" in %s",
1128 type, (uintmax_t)obj_offset, p->pack_name);
1129 type = OBJ_BAD;
1130 }
1131
1132 out:
1133 if (poi_stack != small_poi_stack)
1134 free(poi_stack);
1135 return type;
1136
1137 unwind:
1138 while (poi_stack_nr) {
1139 obj_offset = poi_stack[--poi_stack_nr];
1140 type = retry_bad_packed_offset(r, p, obj_offset);
1141 if (type > OBJ_NONE)
1142 goto out;
1143 }
1144 type = OBJ_BAD;
1145 goto out;
1146 }
1147
1148 static struct hashmap delta_base_cache;
1149 static size_t delta_base_cached;
1150
1151 static LIST_HEAD(delta_base_cache_lru);
1152
1153 struct delta_base_cache_key {
1154 struct packed_git *p;
1155 off_t base_offset;
1156 };
1157
1158 struct delta_base_cache_entry {
1159 struct hashmap_entry ent;
1160 struct delta_base_cache_key key;
1161 struct list_head lru;
1162 void *data;
1163 size_t size;
1164 enum object_type type;
1165 };
1166
1167 static unsigned int pack_entry_hash(struct packed_git *p, off_t base_offset)
1168 {
1169 unsigned int hash;
1170
1171 hash = (unsigned int)(intptr_t)p + (unsigned int)base_offset;
1172 hash += (hash >> 8) + (hash >> 16);
1173 return hash;
1174 }
1175
1176 static struct delta_base_cache_entry *
1177 get_delta_base_cache_entry(struct packed_git *p, off_t base_offset)
1178 {
1179 struct hashmap_entry entry, *e;
1180 struct delta_base_cache_key key;
1181
1182 if (!delta_base_cache.cmpfn)
1183 return NULL;
1184
1185 hashmap_entry_init(&entry, pack_entry_hash(p, base_offset));
1186 key.p = p;
1187 key.base_offset = base_offset;
1188 e = hashmap_get(&delta_base_cache, &entry, &key);
1189 return e ? container_of(e, struct delta_base_cache_entry, ent) : NULL;
1190 }
1191
1192 static int delta_base_cache_key_eq(const struct delta_base_cache_key *a,
1193 const struct delta_base_cache_key *b)
1194 {
1195 return a->p == b->p && a->base_offset == b->base_offset;
1196 }
1197
1198 static int delta_base_cache_hash_cmp(const void *cmp_data UNUSED,
1199 const struct hashmap_entry *va,
1200 const struct hashmap_entry *vb,
1201 const void *vkey)
1202 {
1203 const struct delta_base_cache_entry *a, *b;
1204 const struct delta_base_cache_key *key = vkey;
1205
1206 a = container_of(va, const struct delta_base_cache_entry, ent);
1207 b = container_of(vb, const struct delta_base_cache_entry, ent);
1208
1209 if (key)
1210 return !delta_base_cache_key_eq(&a->key, key);
1211 else
1212 return !delta_base_cache_key_eq(&a->key, &b->key);
1213 }
1214
1215 static int in_delta_base_cache(struct packed_git *p, off_t base_offset)
1216 {
1217 return !!get_delta_base_cache_entry(p, base_offset);
1218 }
1219
1220 /*
1221 * Remove the entry from the cache, but do _not_ free the associated
1222 * entry data. The caller takes ownership of the "data" buffer, and
1223 * should copy out any fields it wants before detaching.
1224 */
1225 static void detach_delta_base_cache_entry(struct delta_base_cache_entry *ent)
1226 {
1227 hashmap_remove(&delta_base_cache, &ent->ent, &ent->key);
1228 list_del(&ent->lru);
1229 delta_base_cached -= ent->size;
1230 free(ent);
1231 }
1232
1233 static void *cache_or_unpack_entry(struct repository *r, struct packed_git *p,
1234 off_t base_offset, size_t *base_size,
1235 enum object_type *type)
1236 {
1237 struct delta_base_cache_entry *ent;
1238
1239 ent = get_delta_base_cache_entry(p, base_offset);
1240 if (!ent)
1241 return unpack_entry(r, p, base_offset, type, base_size);
1242
1243 if (type)
1244 *type = ent->type;
1245 if (base_size)
1246 *base_size = ent->size;
1247 return xmemdupz(ent->data, ent->size);
1248 }
1249
1250 static inline void release_delta_base_cache(struct delta_base_cache_entry *ent)
1251 {
1252 free(ent->data);
1253 detach_delta_base_cache_entry(ent);
1254 }
1255
1256 void clear_delta_base_cache(void)
1257 {
1258 struct list_head *lru, *tmp;
1259 list_for_each_safe(lru, tmp, &delta_base_cache_lru) {
1260 struct delta_base_cache_entry *entry =
1261 list_entry(lru, struct delta_base_cache_entry, lru);
1262 release_delta_base_cache(entry);
1263 }
1264 }
1265
1266 static void add_delta_base_cache(struct packed_git *p, off_t base_offset,
1267 void *base, size_t base_size,
1268 size_t delta_base_cache_limit,
1269 enum object_type type)
1270 {
1271 struct delta_base_cache_entry *ent;
1272 struct list_head *lru, *tmp;
1273
1274 /*
1275 * Check required to avoid redundant entries when more than one thread
1276 * is unpacking the same object, in unpack_entry() (since its phases I
1277 * and III might run concurrently across multiple threads).
1278 */
1279 if (in_delta_base_cache(p, base_offset)) {
1280 free(base);
1281 return;
1282 }
1283
1284 delta_base_cached += base_size;
1285
1286 list_for_each_safe(lru, tmp, &delta_base_cache_lru) {
1287 struct delta_base_cache_entry *f =
1288 list_entry(lru, struct delta_base_cache_entry, lru);
1289 if (delta_base_cached <= delta_base_cache_limit)
1290 break;
1291 release_delta_base_cache(f);
1292 }
1293
1294 ent = xmalloc(sizeof(*ent));
1295 ent->key.p = p;
1296 ent->key.base_offset = base_offset;
1297 ent->type = type;
1298 ent->data = base;
1299 ent->size = base_size;
1300 list_add_tail(&ent->lru, &delta_base_cache_lru);
1301
1302 if (!delta_base_cache.cmpfn)
1303 hashmap_init(&delta_base_cache, delta_base_cache_hash_cmp, NULL, 0);
1304 hashmap_entry_init(&ent->ent, pack_entry_hash(p, base_offset));
1305 hashmap_add(&delta_base_cache, &ent->ent);
1306 }
1307
1308 int packed_object_info_with_index_pos(struct odb_source_packed *source,
1309 struct packed_git *p, off_t obj_offset,
1310 uint32_t *maybe_index_pos, struct object_info *oi)
1311 {
1312 struct pack_window *w_curs = NULL;
1313 size_t size;
1314 off_t curpos = obj_offset;
1315 enum object_type type = OBJ_NONE;
1316 uint32_t pack_pos;
1317 int ret;
1318
1319 /*
1320 * We always get the representation type, but only convert it to
1321 * a "real" type later if the caller is interested.
1322 */
1323 if (oi->contentp) {
1324 *oi->contentp = cache_or_unpack_entry(p->repo, p, obj_offset,
1325 oi->sizep, &type);
1326 if (!*oi->contentp)
1327 type = OBJ_BAD;
1328 } else if (oi->sizep || oi->typep || oi->delta_base_oid) {
1329 type = unpack_object_header(p, &w_curs, &curpos, &size);
1330 }
1331
1332 if (!oi->contentp && oi->sizep) {
1333 if (type == OBJ_OFS_DELTA || type == OBJ_REF_DELTA) {
1334 off_t tmp_pos = curpos;
1335 off_t base_offset = get_delta_base(p, &w_curs, &tmp_pos,
1336 type, obj_offset);
1337 if (!base_offset) {
1338 ret = -1;
1339 goto out;
1340 }
1341 size = get_size_from_delta(p, &w_curs, tmp_pos);
1342 if (size == 0) {
1343 ret = -1;
1344 goto out;
1345 }
1346 }
1347 *oi->sizep = size;
1348 }
1349
1350 if (oi->disk_sizep || (oi->mtimep && p->is_cruft)) {
1351 if (offset_to_pack_pos(p, obj_offset, &pack_pos) < 0) {
1352 error("could not find object at offset %"PRIuMAX" "
1353 "in pack %s", (uintmax_t)obj_offset, p->pack_name);
1354 ret = -1;
1355 goto out;
1356 }
1357 }
1358
1359 if (oi->disk_sizep)
1360 *oi->disk_sizep = pack_pos_to_offset(p, pack_pos + 1) - obj_offset;
1361
1362 if (oi->mtimep) {
1363 if (p->is_cruft) {
1364 uint32_t index_pos;
1365
1366 if (load_pack_mtimes(p) < 0)
1367 die(_("could not load .mtimes for cruft pack '%s'"),
1368 pack_basename(p));
1369
1370 if (maybe_index_pos)
1371 index_pos = *maybe_index_pos;
1372 else
1373 index_pos = pack_pos_to_index(p, pack_pos);
1374
1375 *oi->mtimep = nth_packed_mtime(p, index_pos);
1376 } else {
1377 *oi->mtimep = p->mtime;
1378 }
1379 }
1380
1381 if (oi->typep) {
1382 enum object_type ptot;
1383 ptot = packed_to_object_type(p->repo, p, obj_offset,
1384 type, &w_curs, curpos);
1385 if (oi->typep)
1386 *oi->typep = ptot;
1387 if (ptot < 0) {
1388 ret = -1;
1389 goto out;
1390 }
1391 }
1392
1393 if (oi->delta_base_oid) {
1394 if (type == OBJ_OFS_DELTA || type == OBJ_REF_DELTA) {
1395 if (get_delta_base_oid(p, &w_curs, curpos,
1396 oi->delta_base_oid,
1397 type, obj_offset) < 0) {
1398 ret = -1;
1399 goto out;
1400 }
1401 } else
1402 oidclr(oi->delta_base_oid, p->repo->hash_algo);
1403 }
1404
1405 if (oi->source_infop) {
1406 if (!source)
1407 BUG("cannot request source without an owning source");
1408 oi->source_infop->source = &source->base;
1409
1410 oi->source_infop->u.packed.offset = obj_offset;
1411 oi->source_infop->u.packed.pack = p;
1412
1413 switch (type) {
1414 case OBJ_NONE:
1415 oi->source_infop->u.packed.type = PACKED_OBJECT_TYPE_UNKNOWN;
1416 break;
1417 case OBJ_REF_DELTA:
1418 oi->source_infop->u.packed.type = PACKED_OBJECT_TYPE_REF_DELTA;
1419 break;
1420 case OBJ_OFS_DELTA:
1421 oi->source_infop->u.packed.type = PACKED_OBJECT_TYPE_OFS_DELTA;
1422 break;
1423 default:
1424 oi->source_infop->u.packed.type = PACKED_OBJECT_TYPE_FULL;
1425 break;
1426 }
1427 }
1428
1429 ret = 0;
1430
1431 out:
1432 unuse_pack(&w_curs);
1433 return ret;
1434 }
1435
1436 int packed_object_info(struct odb_source_packed *source,
1437 struct packed_git *p, off_t obj_offset,
1438 struct object_info *oi)
1439 {
1440 return packed_object_info_with_index_pos(source, p, obj_offset, NULL, oi);
1441 }
1442
1443 static void *unpack_compressed_entry(struct packed_git *p,
1444 struct pack_window **w_curs,
1445 off_t curpos,
1446 size_t size)
1447 {
1448 int st;
1449 git_zstream stream;
1450 unsigned char *buffer, *in;
1451
1452 buffer = xmallocz_gently(size);
1453 if (!buffer)
1454 return NULL;
1455 memset(&stream, 0, sizeof(stream));
1456 stream.next_out = buffer;
1457 stream.avail_out = size + 1;
1458
1459 git_inflate_init(&stream);
1460 do {
1461 in = use_pack(p, w_curs, curpos, &stream.avail_in);
1462 stream.next_in = in;
1463 /*
1464 * Note: we must ensure the window section returned by
1465 * use_pack() will be available throughout git_inflate()'s
1466 * unlocked execution. Please refer to the comment at
1467 * get_size_from_delta() to see how this is done.
1468 */
1469 obj_read_unlock();
1470 st = git_inflate(&stream, Z_FINISH);
1471 obj_read_lock();
1472 if (!stream.avail_out)
1473 break; /* the payload is larger than it should be */
1474 curpos += stream.next_in - in;
1475 } while (st == Z_OK || st == Z_BUF_ERROR);
1476 git_inflate_end(&stream);
1477 if ((st != Z_STREAM_END) || stream.total_out != size) {
1478 free(buffer);
1479 return NULL;
1480 }
1481
1482 /* versions of zlib can clobber unconsumed portion of outbuf */
1483 buffer[size] = '\0';
1484
1485 return buffer;
1486 }
1487
1488 static void write_pack_access_log(struct packed_git *p, off_t obj_offset)
1489 {
1490 static struct trace_key pack_access = TRACE_KEY_INIT(PACK_ACCESS);
1491 trace_printf_key(&pack_access, "%s %"PRIuMAX"\n",
1492 p->pack_name, (uintmax_t)obj_offset);
1493 }
1494
1495 int do_check_packed_object_crc;
1496
1497 #define UNPACK_ENTRY_STACK_PREALLOC 64
1498 struct unpack_entry_stack_ent {
1499 off_t obj_offset;
1500 off_t curpos;
1501 size_t size;
1502 };
1503
1504 void *unpack_entry(struct repository *r, struct packed_git *p, off_t obj_offset,
1505 enum object_type *final_type, size_t *final_size)
1506 {
1507 struct pack_window *w_curs = NULL;
1508 off_t curpos = obj_offset;
1509 void *data = NULL;
1510 size_t size;
1511 enum object_type type;
1512 struct unpack_entry_stack_ent small_delta_stack[UNPACK_ENTRY_STACK_PREALLOC];
1513 struct unpack_entry_stack_ent *delta_stack = small_delta_stack;
1514 int delta_stack_nr = 0, delta_stack_alloc = UNPACK_ENTRY_STACK_PREALLOC;
1515 int base_from_cache = 0;
1516
1517 prepare_repo_settings(p->repo);
1518
1519 write_pack_access_log(p, obj_offset);
1520
1521 /* PHASE 1: drill down to the innermost base object */
1522 for (;;) {
1523 off_t base_offset;
1524 int i;
1525 struct delta_base_cache_entry *ent;
1526
1527 ent = get_delta_base_cache_entry(p, curpos);
1528 if (ent) {
1529 type = ent->type;
1530 data = ent->data;
1531 size = ent->size;
1532 detach_delta_base_cache_entry(ent);
1533 base_from_cache = 1;
1534 break;
1535 }
1536
1537 if (do_check_packed_object_crc && p->index_version > 1) {
1538 uint32_t pack_pos, index_pos;
1539 off_t len;
1540
1541 if (offset_to_pack_pos(p, obj_offset, &pack_pos) < 0) {
1542 error("could not find object at offset %"PRIuMAX" in pack %s",
1543 (uintmax_t)obj_offset, p->pack_name);
1544 data = NULL;
1545 goto out;
1546 }
1547
1548 len = pack_pos_to_offset(p, pack_pos + 1) - obj_offset;
1549 index_pos = pack_pos_to_index(p, pack_pos);
1550 if (check_pack_crc(p, &w_curs, obj_offset, len, index_pos)) {
1551 struct object_id oid;
1552 nth_packed_object_id(&oid, p, index_pos);
1553 error("bad packed object CRC for %s",
1554 oid_to_hex(&oid));
1555 mark_bad_packed_object(p, &oid);
1556 data = NULL;
1557 goto out;
1558 }
1559 }
1560
1561 type = unpack_object_header(p, &w_curs, &curpos, &size);
1562 if (type != OBJ_OFS_DELTA && type != OBJ_REF_DELTA)
1563 break;
1564
1565 base_offset = get_delta_base(p, &w_curs, &curpos, type, obj_offset);
1566 if (!base_offset) {
1567 error("failed to validate delta base reference "
1568 "at offset %"PRIuMAX" from %s",
1569 (uintmax_t)curpos, p->pack_name);
1570 /* bail to phase 2, in hopes of recovery */
1571 data = NULL;
1572 break;
1573 }
1574
1575 /* push object, proceed to base */
1576 if (delta_stack_nr >= delta_stack_alloc
1577 && delta_stack == small_delta_stack) {
1578 delta_stack_alloc = alloc_nr(delta_stack_nr);
1579 ALLOC_ARRAY(delta_stack, delta_stack_alloc);
1580 COPY_ARRAY(delta_stack, small_delta_stack,
1581 delta_stack_nr);
1582 } else {
1583 ALLOC_GROW(delta_stack, delta_stack_nr+1, delta_stack_alloc);
1584 }
1585 i = delta_stack_nr++;
1586 delta_stack[i].obj_offset = obj_offset;
1587 delta_stack[i].curpos = curpos;
1588 delta_stack[i].size = size;
1589
1590 curpos = obj_offset = base_offset;
1591 }
1592
1593 /* PHASE 2: handle the base */
1594 switch (type) {
1595 case OBJ_OFS_DELTA:
1596 case OBJ_REF_DELTA:
1597 if (data)
1598 BUG("unpack_entry: left loop at a valid delta");
1599 break;
1600 case OBJ_COMMIT:
1601 case OBJ_TREE:
1602 case OBJ_BLOB:
1603 case OBJ_TAG:
1604 if (!base_from_cache)
1605 data = unpack_compressed_entry(p, &w_curs, curpos, size);
1606 break;
1607 default:
1608 data = NULL;
1609 error("unknown object type %i at offset %"PRIuMAX" in %s",
1610 type, (uintmax_t)obj_offset, p->pack_name);
1611 }
1612
1613 /* PHASE 3: apply deltas in order */
1614
1615 /* invariants:
1616 * 'data' holds the base data, or NULL if there was corruption
1617 */
1618 while (delta_stack_nr) {
1619 void *delta_data;
1620 void *base = data;
1621 void *external_base = NULL;
1622 size_t delta_size, base_size = size;
1623 int i;
1624 off_t base_obj_offset = obj_offset;
1625
1626 data = NULL;
1627
1628 if (!base) {
1629 /*
1630 * We're probably in deep shit, but let's try to fetch
1631 * the required base anyway from another pack or loose.
1632 * This is costly but should happen only in the presence
1633 * of a corrupted pack, and is better than failing outright.
1634 */
1635 uint32_t pos;
1636 struct object_id base_oid;
1637 if (!(offset_to_pack_pos(p, obj_offset, &pos))) {
1638 struct object_info oi = OBJECT_INFO_INIT;
1639
1640 nth_packed_object_id(&base_oid, p,
1641 pack_pos_to_index(p, pos));
1642 error("failed to read delta base object %s"
1643 " at offset %"PRIuMAX" from %s",
1644 oid_to_hex(&base_oid), (uintmax_t)obj_offset,
1645 p->pack_name);
1646 mark_bad_packed_object(p, &base_oid);
1647
1648 oi.typep = &type;
1649 oi.sizep = &base_size;
1650 oi.contentp = &base;
1651 if (odb_read_object_info_extended(r->objects, &base_oid,
1652 &oi, 0) < 0)
1653 base = NULL;
1654
1655 external_base = base;
1656 }
1657 }
1658
1659 i = --delta_stack_nr;
1660 obj_offset = delta_stack[i].obj_offset;
1661 curpos = delta_stack[i].curpos;
1662 delta_size = delta_stack[i].size;
1663
1664 if (!base)
1665 continue;
1666
1667 delta_data = unpack_compressed_entry(p, &w_curs, curpos, delta_size);
1668
1669 if (!delta_data) {
1670 error("failed to unpack compressed delta "
1671 "at offset %"PRIuMAX" from %s",
1672 (uintmax_t)curpos, p->pack_name);
1673 data = NULL;
1674 } else {
1675 data = patch_delta(base, base_size, delta_data,
1676 delta_size, &size);
1677
1678 /*
1679 * We could not apply the delta; warn the user, but
1680 * keep going. Our failure will be noticed either in
1681 * the next iteration of the loop, or if this is the
1682 * final delta, in the caller when we return NULL.
1683 * Those code paths will take care of making a more
1684 * explicit warning and retrying with another copy of
1685 * the object.
1686 */
1687 if (!data)
1688 error("failed to apply delta");
1689 }
1690
1691 /*
1692 * We delay adding `base` to the cache until the end of the loop
1693 * because unpack_compressed_entry() momentarily releases the
1694 * obj_read_mutex, giving another thread the chance to access
1695 * the cache. Therefore, if `base` was already there, this other
1696 * thread could free() it (e.g. to make space for another entry)
1697 * before we are done using it.
1698 */
1699 if (!external_base)
1700 add_delta_base_cache(p, base_obj_offset, base, base_size,
1701 p->repo->settings.delta_base_cache_limit,
1702 type);
1703
1704 free(delta_data);
1705 free(external_base);
1706 }
1707
1708 if (final_type)
1709 *final_type = type;
1710 if (final_size)
1711 *final_size = size;
1712
1713 out:
1714 unuse_pack(&w_curs);
1715
1716 if (delta_stack != small_delta_stack)
1717 free(delta_stack);
1718
1719 return data;
1720 }
1721
1722 int bsearch_pack(const struct object_id *oid, const struct packed_git *p, uint32_t *result)
1723 {
1724 const unsigned char *index_fanout = p->index_data;
1725 const unsigned char *index_lookup;
1726 const unsigned int hashsz = p->repo->hash_algo->rawsz;
1727 int index_lookup_width;
1728
1729 if (!index_fanout)
1730 BUG("bsearch_pack called without a valid pack-index");
1731
1732 index_lookup = index_fanout + 4 * 256;
1733 if (p->index_version == 1) {
1734 index_lookup_width = hashsz + 4;
1735 index_lookup += 4;
1736 } else {
1737 index_lookup_width = hashsz;
1738 index_fanout += 8;
1739 index_lookup += 8;
1740 }
1741
1742 return bsearch_hash(oid->hash, (const uint32_t*)index_fanout,
1743 index_lookup, index_lookup_width, result);
1744 }
1745
1746 int nth_packed_object_id(struct object_id *oid,
1747 struct packed_git *p,
1748 uint32_t n)
1749 {
1750 const unsigned char *index = p->index_data;
1751 const unsigned int hashsz = p->repo->hash_algo->rawsz;
1752 if (!index) {
1753 if (open_pack_index(p))
1754 return -1;
1755 index = p->index_data;
1756 }
1757 if (n >= p->num_objects)
1758 return -1;
1759 index += 4 * 256;
1760 if (p->index_version == 1) {
1761 oidread(oid, index + st_add(st_mult(hashsz + 4, n), 4),
1762 p->repo->hash_algo);
1763 } else {
1764 index += 8;
1765 oidread(oid, index + st_mult(hashsz, n), p->repo->hash_algo);
1766 }
1767 return 0;
1768 }
1769
1770 void check_pack_index_ptr(const struct packed_git *p, const void *vptr)
1771 {
1772 const unsigned char *ptr = vptr;
1773 const unsigned char *start = p->index_data;
1774 const unsigned char *end = start + p->index_size;
1775 if (ptr < start)
1776 die(_("offset before start of pack index for %s (corrupt index?)"),
1777 p->pack_name);
1778 /* No need to check for underflow; .idx files must be at least 8 bytes */
1779 if (ptr >= end - 8)
1780 die(_("offset beyond end of pack index for %s (truncated index?)"),
1781 p->pack_name);
1782 }
1783
1784 off_t nth_packed_object_offset(const struct packed_git *p, uint32_t n)
1785 {
1786 const unsigned char *index = p->index_data;
1787 const unsigned int hashsz = p->repo->hash_algo->rawsz;
1788 index += 4 * 256;
1789 if (p->index_version == 1) {
1790 return ntohl(*((uint32_t *)(index + st_mult(hashsz + 4, n))));
1791 } else {
1792 uint32_t off;
1793 index += st_add(8, st_mult(p->num_objects, hashsz + 4));
1794 off = ntohl(*((uint32_t *)(index + st_mult(4, n))));
1795 if (!(off & 0x80000000))
1796 return off;
1797 index += st_add(st_mult(p->num_objects, 4),
1798 st_mult(off & 0x7fffffff, 8));
1799 check_pack_index_ptr(p, index);
1800 return get_be64(index);
1801 }
1802 }
1803
1804 off_t find_pack_entry_one(const struct object_id *oid,
1805 struct packed_git *p)
1806 {
1807 const unsigned char *index = p->index_data;
1808 uint32_t result;
1809
1810 if (!index) {
1811 if (open_pack_index(p))
1812 return 0;
1813 }
1814
1815 if (bsearch_pack(oid, p, &result))
1816 return nth_packed_object_offset(p, result);
1817 return 0;
1818 }
1819
1820 int is_pack_valid(struct packed_git *p)
1821 {
1822 /* An already open pack is known to be valid. */
1823 if (p->pack_fd != -1)
1824 return 1;
1825
1826 /* If the pack has one window completely covering the
1827 * file size, the pack is known to be valid even if
1828 * the descriptor is not currently open.
1829 */
1830 if (p->windows) {
1831 struct pack_window *w = p->windows;
1832
1833 if (!w->offset && w->len == p->pack_size)
1834 return 1;
1835 }
1836
1837 /* Force the pack to open to prove its valid. */
1838 return !open_packed_git(p);
1839 }
1840
1841 int packfile_fill_entry(struct packed_git *p,
1842 const struct object_id *oid,
1843 struct pack_entry *e,
1844 struct packed_git **bad_pack)
1845 {
1846 off_t offset;
1847
1848 if (oidset_size(&p->bad_objects) &&
1849 oidset_contains(&p->bad_objects, oid)) {
1850 if (bad_pack && !*bad_pack)
1851 *bad_pack = p;
1852 return 0;
1853 }
1854
1855 offset = find_pack_entry_one(oid, p);
1856 if (!offset)
1857 return 0;
1858
1859 /*
1860 * We are about to tell the caller where they can locate the
1861 * requested object. We better make sure the packfile is
1862 * still here and can be accessed before supplying that
1863 * answer, as it may have been deleted since the index was
1864 * loaded!
1865 */
1866 if (!is_pack_valid(p))
1867 return 0;
1868 e->offset = offset;
1869 e->p = p;
1870 return 1;
1871 }
1872
1873 static void maybe_invalidate_kept_pack_cache(struct odb_source_packed *store,
1874 unsigned flags)
1875 {
1876 if (!store->kept_cache.packs)
1877 return;
1878 if (store->kept_cache.flags == flags)
1879 return;
1880 FREE_AND_NULL(store->kept_cache.packs);
1881 store->kept_cache.flags = 0;
1882 }
1883
1884 struct packed_git **packfile_store_get_kept_pack_cache(struct odb_source_packed *store,
1885 unsigned flags)
1886 {
1887 maybe_invalidate_kept_pack_cache(store, flags);
1888
1889 if (!store->kept_cache.packs) {
1890 struct packed_git **packs = NULL;
1891 struct packfile_list_entry *e;
1892 size_t nr = 0, alloc = 0;
1893
1894 /*
1895 * We want "all" packs here, because we need to cover ones that
1896 * are used by a midx, as well. We need to look in every one of
1897 * them (instead of the midx itself) to cover duplicates. It's
1898 * possible that an object is found in two packs that the midx
1899 * covers, one kept and one not kept, but the midx returns only
1900 * the non-kept version.
1901 */
1902 for (e = packfile_store_get_packs(store); e; e = e->next) {
1903 struct packed_git *p = e->pack;
1904
1905 if ((p->pack_keep && (flags & KEPT_PACK_ON_DISK)) ||
1906 (p->pack_keep_in_core && (flags & KEPT_PACK_IN_CORE)) ||
1907 (p->pack_keep_in_core_open && (flags & KEPT_PACK_IN_CORE_OPEN))) {
1908 ALLOC_GROW(packs, nr + 1, alloc);
1909 packs[nr++] = p;
1910 }
1911 }
1912 ALLOC_GROW(packs, nr + 1, alloc);
1913 packs[nr] = NULL;
1914
1915 store->kept_cache.packs = packs;
1916 store->kept_cache.flags = flags;
1917 }
1918
1919 return store->kept_cache.packs;
1920 }
1921
1922 int has_object_pack(struct repository *r, const struct object_id *oid)
1923 {
1924 struct odb_source *source;
1925
1926 for (source = r->objects->sources; source; source = source->next) {
1927 struct odb_source_files *files = odb_source_files_downcast(source);
1928 if (!odb_source_read_object_info(&files->packed->base, oid, NULL, 0, NULL))
1929 return 1;
1930 }
1931
1932 return 0;
1933 }
1934
1935 int has_object_kept_pack(struct repository *r, const struct object_id *oid,
1936 unsigned flags)
1937 {
1938 struct odb_source *source;
1939 struct pack_entry e;
1940
1941 for (source = r->objects->sources; source; source = source->next) {
1942 struct odb_source_files *files = odb_source_files_downcast(source);
1943 struct packed_git **cache;
1944
1945 cache = packfile_store_get_kept_pack_cache(files->packed, flags);
1946
1947 for (; *cache; cache++) {
1948 struct packed_git *p = *cache;
1949 if (packfile_fill_entry(p, oid, &e, NULL))
1950 return 1;
1951 }
1952 }
1953
1954 return 0;
1955 }
1956
1957 int for_each_object_in_pack(struct packed_git *p,
1958 each_packed_object_fn cb, void *data,
1959 enum odb_for_each_object_flags flags)
1960 {
1961 uint32_t i;
1962 int r = 0;
1963
1964 if (flags & ODB_FOR_EACH_OBJECT_PACK_ORDER) {
1965 if (load_pack_revindex(p->repo, p))
1966 return -1;
1967 }
1968
1969 for (i = 0; i < p->num_objects; i++) {
1970 uint32_t index_pos;
1971 struct object_id oid;
1972
1973 /*
1974 * We are iterating "i" from 0 up to num_objects, but its
1975 * meaning may be different, depending on the requested output
1976 * order:
1977 *
1978 * - in object-name order, it is the same as the index order
1979 * used by nth_packed_object_id(), so we can pass it
1980 * directly
1981 *
1982 * - in pack-order, it is pack position, which we must
1983 * convert to an index position in order to get the oid.
1984 */
1985 if (flags & ODB_FOR_EACH_OBJECT_PACK_ORDER)
1986 index_pos = pack_pos_to_index(p, i);
1987 else
1988 index_pos = i;
1989
1990 if (nth_packed_object_id(&oid, p, index_pos) < 0)
1991 return error("unable to get sha1 of object %u in %s",
1992 index_pos, p->pack_name);
1993
1994 r = cb(&oid, p, index_pos, data);
1995 if (r)
1996 break;
1997 }
1998 return r;
1999 }
2000
2001 struct add_promisor_object_data {
2002 struct repository *repo;
2003 struct oidset *set;
2004 };
2005
2006 static int add_promisor_object(const struct object_id *oid,
2007 struct object_info *oi UNUSED,
2008 void *cb_data)
2009 {
2010 struct add_promisor_object_data *data = cb_data;
2011 struct object *obj;
2012 int we_parsed_object;
2013
2014 obj = lookup_object(data->repo, oid);
2015 if (obj && obj->parsed) {
2016 we_parsed_object = 0;
2017 } else {
2018 we_parsed_object = 1;
2019 obj = parse_object_with_flags(data->repo, oid,
2020 PARSE_OBJECT_SKIP_HASH_CHECK);
2021 }
2022
2023 if (!obj)
2024 return 1;
2025
2026 oidset_insert(data->set, oid);
2027
2028 /*
2029 * If this is a tree, commit, or tag, the objects it refers
2030 * to are also promisor objects. (Blobs refer to no objects->)
2031 */
2032 if (obj->type == OBJ_TREE) {
2033 struct tree *tree = (struct tree *)obj;
2034 struct tree_desc desc;
2035 struct name_entry entry;
2036 if (init_tree_desc_gently(&desc, &tree->object.oid,
2037 tree->buffer, tree->size, 0))
2038 /*
2039 * Error messages are given when packs are
2040 * verified, so do not print any here.
2041 */
2042 return 0;
2043 while (tree_entry_gently(&desc, &entry))
2044 oidset_insert(data->set, &entry.oid);
2045 if (we_parsed_object)
2046 free_tree_buffer(tree);
2047 } else if (obj->type == OBJ_COMMIT) {
2048 struct commit *commit = (struct commit *) obj;
2049 struct commit_list *parents = commit->parents;
2050
2051 oidset_insert(data->set, get_commit_tree_oid(commit));
2052 for (; parents; parents = parents->next)
2053 oidset_insert(data->set, &parents->item->object.oid);
2054 } else if (obj->type == OBJ_TAG) {
2055 struct tag *tag = (struct tag *) obj;
2056 oidset_insert(data->set, get_tagged_oid(tag));
2057 }
2058 return 0;
2059 }
2060
2061 int is_promisor_object(struct repository *r, const struct object_id *oid)
2062 {
2063 static struct oidset promisor_objects;
2064 static int promisor_objects_prepared;
2065
2066 if (!promisor_objects_prepared) {
2067 if (repo_has_promisor_remote(r)) {
2068 struct add_promisor_object_data data = {
2069 .repo = r,
2070 .set = &promisor_objects,
2071 };
2072
2073 odb_for_each_object(r->objects, NULL, add_promisor_object, &data,
2074 ODB_FOR_EACH_OBJECT_PROMISOR_ONLY | ODB_FOR_EACH_OBJECT_PACK_ORDER);
2075 }
2076 promisor_objects_prepared = 1;
2077 }
2078 return oidset_contains(&promisor_objects, oid);
2079 }
2080
2081 int parse_pack_header_option(const char *in, unsigned char *out, unsigned int *len)
2082 {
2083 unsigned char *hdr;
2084 char *c;
2085
2086 hdr = out;
2087 put_be32(hdr, PACK_SIGNATURE);
2088 hdr += 4;
2089 put_be32(hdr, strtoul(in, &c, 10));
2090 hdr += 4;
2091 if (*c != ',')
2092 return -1;
2093 put_be32(hdr, strtoul(c + 1, &c, 10));
2094 hdr += 4;
2095 if (*c)
2096 return -1;
2097 *len = hdr - out;
2098 return 0;
2099 }
2100
2101 struct odb_packed_read_stream {
2102 struct odb_stream base;
2103 struct packed_git *pack;
2104 git_zstream z;
2105 enum {
2106 ODB_PACKED_READ_STREAM_UNINITIALIZED,
2107 ODB_PACKED_READ_STREAM_INUSE,
2108 ODB_PACKED_READ_STREAM_DONE,
2109 ODB_PACKED_READ_STREAM_ERROR,
2110 } z_state;
2111 off_t pos;
2112 };
2113
2114 static ssize_t read_istream_pack_non_delta(struct odb_stream *_st, char *buf,
2115 size_t sz)
2116 {
2117 struct odb_packed_read_stream *st = (struct odb_packed_read_stream *)_st;
2118 size_t total_read = 0;
2119
2120 switch (st->z_state) {
2121 case ODB_PACKED_READ_STREAM_UNINITIALIZED:
2122 memset(&st->z, 0, sizeof(st->z));
2123 git_inflate_init(&st->z);
2124 st->z_state = ODB_PACKED_READ_STREAM_INUSE;
2125 break;
2126 case ODB_PACKED_READ_STREAM_DONE:
2127 return 0;
2128 case ODB_PACKED_READ_STREAM_ERROR:
2129 return -1;
2130 case ODB_PACKED_READ_STREAM_INUSE:
2131 break;
2132 }
2133
2134 while (total_read < sz) {
2135 int status;
2136 struct pack_window *window = NULL;
2137 unsigned char *mapped;
2138
2139 mapped = use_pack(st->pack, &window,
2140 st->pos, &st->z.avail_in);
2141
2142 st->z.next_out = (unsigned char *)buf + total_read;
2143 st->z.avail_out = sz - total_read;
2144 st->z.next_in = mapped;
2145 status = git_inflate(&st->z, Z_FINISH);
2146
2147 st->pos += st->z.next_in - mapped;
2148 total_read = st->z.next_out - (unsigned char *)buf;
2149 unuse_pack(&window);
2150
2151 if (status == Z_STREAM_END) {
2152 git_inflate_end(&st->z);
2153 st->z_state = ODB_PACKED_READ_STREAM_DONE;
2154 break;
2155 }
2156
2157 /*
2158 * Unlike the loose object case, we do not have to worry here
2159 * about running out of input bytes and spinning infinitely. If
2160 * we get Z_BUF_ERROR due to too few input bytes, then we'll
2161 * replenish them in the next use_pack() call when we loop. If
2162 * we truly hit the end of the pack (i.e., because it's corrupt
2163 * or truncated), then use_pack() catches that and will die().
2164 */
2165 if (status != Z_OK && status != Z_BUF_ERROR) {
2166 git_inflate_end(&st->z);
2167 st->z_state = ODB_PACKED_READ_STREAM_ERROR;
2168 return -1;
2169 }
2170 }
2171 return total_read;
2172 }
2173
2174 static int close_istream_pack_non_delta(struct odb_stream *_st)
2175 {
2176 struct odb_packed_read_stream *st = (struct odb_packed_read_stream *)_st;
2177 if (st->z_state == ODB_PACKED_READ_STREAM_INUSE)
2178 git_inflate_end(&st->z);
2179 return 0;
2180 }
2181
2182 int packfile_read_object_stream(struct odb_stream **out,
2183 const struct object_id *oid,
2184 struct packed_git *pack,
2185 off_t offset)
2186 {
2187 struct odb_packed_read_stream *stream;
2188 struct pack_window *window = NULL;
2189 enum object_type in_pack_type;
2190 size_t size;
2191
2192 in_pack_type = unpack_object_header(pack, &window, &offset, &size);
2193 unuse_pack(&window);
2194
2195 if (repo_settings_get_big_file_threshold(pack->repo) >= size)
2196 return -1;
2197
2198 switch (in_pack_type) {
2199 default:
2200 return -1; /* we do not do deltas for now */
2201 case OBJ_BAD:
2202 mark_bad_packed_object(pack, oid);
2203 return -1;
2204 case OBJ_COMMIT:
2205 case OBJ_TREE:
2206 case OBJ_BLOB:
2207 case OBJ_TAG:
2208 break;
2209 }
2210
2211 CALLOC_ARRAY(stream, 1);
2212 stream->base.close = close_istream_pack_non_delta;
2213 stream->base.read = read_istream_pack_non_delta;
2214 stream->base.type = in_pack_type;
2215 stream->base.size = size;
2216 stream->z_state = ODB_PACKED_READ_STREAM_UNINITIALIZED;
2217 stream->pack = pack;
2218 stream->pos = offset;
2219
2220 *out = &stream->base;
2221
2222 return 0;
2223 }