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