10 #include "sha1-lookup.h"
14 #include "tree-walk.h"
16 #include "object-store.h"
18 char *odb_pack_name(struct strbuf
*buf
,
19 const unsigned char *sha1
,
23 strbuf_addf(buf
, "%s/pack/pack-%s.%s", get_object_directory(),
24 sha1_to_hex(sha1
), ext
);
28 char *sha1_pack_name(const unsigned char *sha1
)
30 static struct strbuf buf
= STRBUF_INIT
;
31 return odb_pack_name(&buf
, sha1
, "pack");
34 char *sha1_pack_index_name(const unsigned char *sha1
)
36 static struct strbuf buf
= STRBUF_INIT
;
37 return odb_pack_name(&buf
, sha1
, "idx");
40 static unsigned int pack_used_ctr
;
41 static unsigned int pack_mmap_calls
;
42 static unsigned int peak_pack_open_windows
;
43 static unsigned int pack_open_windows
;
44 static unsigned int pack_open_fds
;
45 static unsigned int pack_max_fds
;
46 static size_t peak_pack_mapped
;
47 static size_t pack_mapped
;
48 struct packed_git
*packed_git
;
49 LIST_HEAD(packed_git_mru
);
51 #define SZ_FMT PRIuMAX
52 static inline uintmax_t sz_fmt(size_t s
) { return s
; }
54 void pack_report(void)
57 "pack_report: getpagesize() = %10" SZ_FMT
"\n"
58 "pack_report: core.packedGitWindowSize = %10" SZ_FMT
"\n"
59 "pack_report: core.packedGitLimit = %10" SZ_FMT
"\n",
60 sz_fmt(getpagesize()),
61 sz_fmt(packed_git_window_size
),
62 sz_fmt(packed_git_limit
));
64 "pack_report: pack_used_ctr = %10u\n"
65 "pack_report: pack_mmap_calls = %10u\n"
66 "pack_report: pack_open_windows = %10u / %10u\n"
67 "pack_report: pack_mapped = "
68 "%10" SZ_FMT
" / %10" SZ_FMT
"\n",
71 pack_open_windows
, peak_pack_open_windows
,
72 sz_fmt(pack_mapped
), sz_fmt(peak_pack_mapped
));
76 * Open and mmap the index file at path, perform a couple of
77 * consistency checks, then record its information to p. Return 0 on
80 static int check_packed_git_idx(const char *path
, struct packed_git
*p
)
83 struct pack_idx_header
*hdr
;
85 uint32_t version
, nr
, i
, *index
;
86 int fd
= git_open(path
);
95 idx_size
= xsize_t(st
.st_size
);
96 if (idx_size
< 4 * 256 + 20 + 20) {
98 return error("index file %s is too small", path
);
100 idx_map
= xmmap(NULL
, idx_size
, PROT_READ
, MAP_PRIVATE
, fd
, 0);
104 if (hdr
->idx_signature
== htonl(PACK_IDX_SIGNATURE
)) {
105 version
= ntohl(hdr
->idx_version
);
106 if (version
< 2 || version
> 2) {
107 munmap(idx_map
, idx_size
);
108 return error("index file %s is version %"PRIu32
109 " and is not supported by this binary"
110 " (try upgrading GIT to a newer version)",
119 index
+= 2; /* skip index header */
120 for (i
= 0; i
< 256; i
++) {
121 uint32_t n
= ntohl(index
[i
]);
123 munmap(idx_map
, idx_size
);
124 return error("non-monotonic index %s", path
);
132 * - 256 index entries 4 bytes each
133 * - 24-byte entries * nr (20-byte sha1 + 4-byte offset)
134 * - 20-byte SHA1 of the packfile
135 * - 20-byte SHA1 file checksum
137 if (idx_size
!= 4*256 + nr
* 24 + 20 + 20) {
138 munmap(idx_map
, idx_size
);
139 return error("wrong index v1 file size in %s", path
);
141 } else if (version
== 2) {
144 * - 8 bytes of header
145 * - 256 index entries 4 bytes each
146 * - 20-byte sha1 entry * nr
147 * - 4-byte crc entry * nr
148 * - 4-byte offset entry * nr
149 * - 20-byte SHA1 of the packfile
150 * - 20-byte SHA1 file checksum
151 * And after the 4-byte offset table might be a
152 * variable sized table containing 8-byte entries
153 * for offsets larger than 2^31.
155 unsigned long min_size
= 8 + 4*256 + nr
*(20 + 4 + 4) + 20 + 20;
156 unsigned long max_size
= min_size
;
158 max_size
+= (nr
- 1)*8;
159 if (idx_size
< min_size
|| idx_size
> max_size
) {
160 munmap(idx_map
, idx_size
);
161 return error("wrong index v2 file size in %s", path
);
163 if (idx_size
!= min_size
&&
165 * make sure we can deal with large pack offsets.
166 * 31-bit signed offset won't be enough, neither
167 * 32-bit unsigned one will be.
169 (sizeof(off_t
) <= 4)) {
170 munmap(idx_map
, idx_size
);
171 return error("pack too large for current definition of off_t in %s", path
);
175 p
->index_version
= version
;
176 p
->index_data
= idx_map
;
177 p
->index_size
= idx_size
;
182 int open_pack_index(struct packed_git
*p
)
191 if (!strip_suffix(p
->pack_name
, ".pack", &len
))
192 die("BUG: pack_name does not end in .pack");
193 idx_name
= xstrfmt("%.*s.idx", (int)len
, p
->pack_name
);
194 ret
= check_packed_git_idx(idx_name
, p
);
199 static struct packed_git
*alloc_packed_git(int extra
)
201 struct packed_git
*p
= xmalloc(st_add(sizeof(*p
), extra
));
202 memset(p
, 0, sizeof(*p
));
207 struct packed_git
*parse_pack_index(unsigned char *sha1
, const char *idx_path
)
209 const char *path
= sha1_pack_name(sha1
);
210 size_t alloc
= st_add(strlen(path
), 1);
211 struct packed_git
*p
= alloc_packed_git(alloc
);
213 memcpy(p
->pack_name
, path
, alloc
); /* includes NUL */
214 hashcpy(p
->sha1
, sha1
);
215 if (check_packed_git_idx(idx_path
, p
)) {
223 static void scan_windows(struct packed_git
*p
,
224 struct packed_git
**lru_p
,
225 struct pack_window
**lru_w
,
226 struct pack_window
**lru_l
)
228 struct pack_window
*w
, *w_l
;
230 for (w_l
= NULL
, w
= p
->windows
; w
; w
= w
->next
) {
232 if (!*lru_w
|| w
->last_used
< (*lru_w
)->last_used
) {
242 static int unuse_one_window(struct packed_git
*current
)
244 struct packed_git
*p
, *lru_p
= NULL
;
245 struct pack_window
*lru_w
= NULL
, *lru_l
= NULL
;
248 scan_windows(current
, &lru_p
, &lru_w
, &lru_l
);
249 for (p
= packed_git
; p
; p
= p
->next
)
250 scan_windows(p
, &lru_p
, &lru_w
, &lru_l
);
252 munmap(lru_w
->base
, lru_w
->len
);
253 pack_mapped
-= lru_w
->len
;
255 lru_l
->next
= lru_w
->next
;
257 lru_p
->windows
= lru_w
->next
;
265 void release_pack_memory(size_t need
)
267 size_t cur
= pack_mapped
;
268 while (need
>= (cur
- pack_mapped
) && unuse_one_window(NULL
))
272 void close_pack_windows(struct packed_git
*p
)
275 struct pack_window
*w
= p
->windows
;
278 die("pack '%s' still has open windows to it",
280 munmap(w
->base
, w
->len
);
281 pack_mapped
-= w
->len
;
283 p
->windows
= w
->next
;
288 static int close_pack_fd(struct packed_git
*p
)
300 void close_pack_index(struct packed_git
*p
)
303 munmap((void *)p
->index_data
, p
->index_size
);
304 p
->index_data
= NULL
;
308 static void close_pack(struct packed_git
*p
)
310 close_pack_windows(p
);
315 void close_all_packs(void)
317 struct packed_git
*p
;
319 for (p
= packed_git
; p
; p
= p
->next
)
321 die("BUG: want to close pack marked 'do-not-close'");
327 * The LRU pack is the one with the oldest MRU window, preferring packs
328 * with no used windows, or the oldest mtime if it has no windows allocated.
330 static void find_lru_pack(struct packed_git
*p
, struct packed_git
**lru_p
, struct pack_window
**mru_w
, int *accept_windows_inuse
)
332 struct pack_window
*w
, *this_mru_w
;
333 int has_windows_inuse
= 0;
336 * Reject this pack if it has windows and the previously selected
337 * one does not. If this pack does not have windows, reject
338 * it if the pack file is newer than the previously selected one.
340 if (*lru_p
&& !*mru_w
&& (p
->windows
|| p
->mtime
> (*lru_p
)->mtime
))
343 for (w
= this_mru_w
= p
->windows
; w
; w
= w
->next
) {
345 * Reject this pack if any of its windows are in use,
346 * but the previously selected pack did not have any
347 * inuse windows. Otherwise, record that this pack
348 * has windows in use.
351 if (*accept_windows_inuse
)
352 has_windows_inuse
= 1;
357 if (w
->last_used
> this_mru_w
->last_used
)
361 * Reject this pack if it has windows that have been
362 * used more recently than the previously selected pack.
363 * If the previously selected pack had windows inuse and
364 * we have not encountered a window in this pack that is
365 * inuse, skip this check since we prefer a pack with no
366 * inuse windows to one that has inuse windows.
368 if (*mru_w
&& *accept_windows_inuse
== has_windows_inuse
&&
369 this_mru_w
->last_used
> (*mru_w
)->last_used
)
378 *accept_windows_inuse
= has_windows_inuse
;
381 static int close_one_pack(void)
383 struct packed_git
*p
, *lru_p
= NULL
;
384 struct pack_window
*mru_w
= NULL
;
385 int accept_windows_inuse
= 1;
387 for (p
= packed_git
; p
; p
= p
->next
) {
388 if (p
->pack_fd
== -1)
390 find_lru_pack(p
, &lru_p
, &mru_w
, &accept_windows_inuse
);
394 return close_pack_fd(lru_p
);
399 static unsigned int get_max_fd_limit(void)
405 if (!getrlimit(RLIMIT_NOFILE
, &lim
))
412 long open_max
= sysconf(_SC_OPEN_MAX
);
416 * Otherwise, we got -1 for one of the two
419 * (1) sysconf() did not understand _SC_OPEN_MAX
420 * and signaled an error with -1; or
421 * (2) sysconf() said there is no limit.
423 * We _could_ clear errno before calling sysconf() to
424 * tell these two cases apart and return a huge number
425 * in the latter case to let the caller cap it to a
426 * value that is not so selfish, but letting the
427 * fallback OPEN_MAX codepath take care of these cases
436 return 1; /* see the caller ;-) */
441 * Do not call this directly as this leaks p->pack_fd on error return;
442 * call open_packed_git() instead.
444 static int open_packed_git_1(struct packed_git
*p
)
447 struct pack_header hdr
;
448 unsigned char sha1
[20];
449 unsigned char *idx_sha1
;
453 if (!p
->index_data
&& open_pack_index(p
))
454 return error("packfile %s index unavailable", p
->pack_name
);
457 unsigned int max_fds
= get_max_fd_limit();
459 /* Save 3 for stdin/stdout/stderr, 22 for work */
461 pack_max_fds
= max_fds
- 25;
466 while (pack_max_fds
<= pack_open_fds
&& close_one_pack())
469 p
->pack_fd
= git_open(p
->pack_name
);
470 if (p
->pack_fd
< 0 || fstat(p
->pack_fd
, &st
))
474 /* If we created the struct before we had the pack we lack size. */
476 if (!S_ISREG(st
.st_mode
))
477 return error("packfile %s not a regular file", p
->pack_name
);
478 p
->pack_size
= st
.st_size
;
479 } else if (p
->pack_size
!= st
.st_size
)
480 return error("packfile %s size changed", p
->pack_name
);
482 /* We leave these file descriptors open with sliding mmap;
483 * there is no point keeping them open across exec(), though.
485 fd_flag
= fcntl(p
->pack_fd
, F_GETFD
, 0);
487 return error("cannot determine file descriptor flags");
488 fd_flag
|= FD_CLOEXEC
;
489 if (fcntl(p
->pack_fd
, F_SETFD
, fd_flag
) == -1)
490 return error("cannot set FD_CLOEXEC");
492 /* Verify we recognize this pack file format. */
493 read_result
= read_in_full(p
->pack_fd
, &hdr
, sizeof(hdr
));
495 return error_errno("error reading from %s", p
->pack_name
);
496 if (read_result
!= sizeof(hdr
))
497 return error("file %s is far too short to be a packfile", p
->pack_name
);
498 if (hdr
.hdr_signature
!= htonl(PACK_SIGNATURE
))
499 return error("file %s is not a GIT packfile", p
->pack_name
);
500 if (!pack_version_ok(hdr
.hdr_version
))
501 return error("packfile %s is version %"PRIu32
" and not"
502 " supported (try upgrading GIT to a newer version)",
503 p
->pack_name
, ntohl(hdr
.hdr_version
));
505 /* Verify the pack matches its index. */
506 if (p
->num_objects
!= ntohl(hdr
.hdr_entries
))
507 return error("packfile %s claims to have %"PRIu32
" objects"
508 " while index indicates %"PRIu32
" objects",
509 p
->pack_name
, ntohl(hdr
.hdr_entries
),
511 if (lseek(p
->pack_fd
, p
->pack_size
- sizeof(sha1
), SEEK_SET
) == -1)
512 return error("end of packfile %s is unavailable", p
->pack_name
);
513 read_result
= read_in_full(p
->pack_fd
, sha1
, sizeof(sha1
));
515 return error_errno("error reading from %s", p
->pack_name
);
516 if (read_result
!= sizeof(sha1
))
517 return error("packfile %s signature is unavailable", p
->pack_name
);
518 idx_sha1
= ((unsigned char *)p
->index_data
) + p
->index_size
- 40;
519 if (hashcmp(sha1
, idx_sha1
))
520 return error("packfile %s does not match index", p
->pack_name
);
524 static int open_packed_git(struct packed_git
*p
)
526 if (!open_packed_git_1(p
))
532 static int in_window(struct pack_window
*win
, off_t offset
)
534 /* We must promise at least 20 bytes (one hash) after the
535 * offset is available from this window, otherwise the offset
536 * is not actually in this window and a different window (which
537 * has that one hash excess) must be used. This is to support
538 * the object header and delta base parsing routines below.
540 off_t win_off
= win
->offset
;
541 return win_off
<= offset
542 && (offset
+ 20) <= (win_off
+ win
->len
);
545 unsigned char *use_pack(struct packed_git
*p
,
546 struct pack_window
**w_cursor
,
550 struct pack_window
*win
= *w_cursor
;
552 /* Since packfiles end in a hash of their content and it's
553 * pointless to ask for an offset into the middle of that
554 * hash, and the in_window function above wouldn't match
555 * don't allow an offset too close to the end of the file.
557 if (!p
->pack_size
&& p
->pack_fd
== -1 && open_packed_git(p
))
558 die("packfile %s cannot be accessed", p
->pack_name
);
559 if (offset
> (p
->pack_size
- 20))
560 die("offset beyond end of packfile (truncated pack?)");
562 die(_("offset before end of packfile (broken .idx?)"));
564 if (!win
|| !in_window(win
, offset
)) {
567 for (win
= p
->windows
; win
; win
= win
->next
) {
568 if (in_window(win
, offset
))
572 size_t window_align
= packed_git_window_size
/ 2;
575 if (p
->pack_fd
== -1 && open_packed_git(p
))
576 die("packfile %s cannot be accessed", p
->pack_name
);
578 win
= xcalloc(1, sizeof(*win
));
579 win
->offset
= (offset
/ window_align
) * window_align
;
580 len
= p
->pack_size
- win
->offset
;
581 if (len
> packed_git_window_size
)
582 len
= packed_git_window_size
;
583 win
->len
= (size_t)len
;
584 pack_mapped
+= win
->len
;
585 while (packed_git_limit
< pack_mapped
586 && unuse_one_window(p
))
588 win
->base
= xmmap(NULL
, win
->len
,
589 PROT_READ
, MAP_PRIVATE
,
590 p
->pack_fd
, win
->offset
);
591 if (win
->base
== MAP_FAILED
)
592 die_errno("packfile %s cannot be mapped",
594 if (!win
->offset
&& win
->len
== p
->pack_size
599 if (pack_mapped
> peak_pack_mapped
)
600 peak_pack_mapped
= pack_mapped
;
601 if (pack_open_windows
> peak_pack_open_windows
)
602 peak_pack_open_windows
= pack_open_windows
;
603 win
->next
= p
->windows
;
607 if (win
!= *w_cursor
) {
608 win
->last_used
= pack_used_ctr
++;
612 offset
-= win
->offset
;
614 *left
= win
->len
- xsize_t(offset
);
615 return win
->base
+ offset
;
618 void unuse_pack(struct pack_window
**w_cursor
)
620 struct pack_window
*w
= *w_cursor
;
627 static void try_to_free_pack_memory(size_t size
)
629 release_pack_memory(size
);
632 struct packed_git
*add_packed_git(const char *path
, size_t path_len
, int local
)
634 static int have_set_try_to_free_routine
;
637 struct packed_git
*p
;
639 if (!have_set_try_to_free_routine
) {
640 have_set_try_to_free_routine
= 1;
641 set_try_to_free_routine(try_to_free_pack_memory
);
645 * Make sure a corresponding .pack file exists and that
646 * the index looks sane.
648 if (!strip_suffix_mem(path
, &path_len
, ".idx"))
652 * ".promisor" is long enough to hold any suffix we're adding (and
653 * the use xsnprintf double-checks that)
655 alloc
= st_add3(path_len
, strlen(".promisor"), 1);
656 p
= alloc_packed_git(alloc
);
657 memcpy(p
->pack_name
, path
, path_len
);
659 xsnprintf(p
->pack_name
+ path_len
, alloc
- path_len
, ".keep");
660 if (!access(p
->pack_name
, F_OK
))
663 xsnprintf(p
->pack_name
+ path_len
, alloc
- path_len
, ".promisor");
664 if (!access(p
->pack_name
, F_OK
))
665 p
->pack_promisor
= 1;
667 xsnprintf(p
->pack_name
+ path_len
, alloc
- path_len
, ".pack");
668 if (stat(p
->pack_name
, &st
) || !S_ISREG(st
.st_mode
)) {
673 /* ok, it looks sane as far as we can check without
674 * actually mapping the pack file.
676 p
->pack_size
= st
.st_size
;
677 p
->pack_local
= local
;
678 p
->mtime
= st
.st_mtime
;
679 if (path_len
< 40 || get_sha1_hex(path
+ path_len
- 40, p
->sha1
))
684 void install_packed_git(struct packed_git
*pack
)
686 if (pack
->pack_fd
!= -1)
689 pack
->next
= packed_git
;
693 void (*report_garbage
)(unsigned seen_bits
, const char *path
);
695 static void report_helper(const struct string_list
*list
,
696 int seen_bits
, int first
, int last
)
698 if (seen_bits
== (PACKDIR_FILE_PACK
|PACKDIR_FILE_IDX
))
701 for (; first
< last
; first
++)
702 report_garbage(seen_bits
, list
->items
[first
].string
);
705 static void report_pack_garbage(struct string_list
*list
)
707 int i
, baselen
= -1, first
= 0, seen_bits
= 0;
712 string_list_sort(list
);
714 for (i
= 0; i
< list
->nr
; i
++) {
715 const char *path
= list
->items
[i
].string
;
717 strncmp(path
, list
->items
[first
].string
, baselen
)) {
718 report_helper(list
, seen_bits
, first
, i
);
723 const char *dot
= strrchr(path
, '.');
725 report_garbage(PACKDIR_FILE_GARBAGE
, path
);
728 baselen
= dot
- path
+ 1;
731 if (!strcmp(path
+ baselen
, "pack"))
733 else if (!strcmp(path
+ baselen
, "idx"))
736 report_helper(list
, seen_bits
, first
, list
->nr
);
739 static void prepare_packed_git_one(char *objdir
, int local
)
741 struct strbuf path
= STRBUF_INIT
;
745 struct string_list garbage
= STRING_LIST_INIT_DUP
;
747 strbuf_addstr(&path
, objdir
);
748 strbuf_addstr(&path
, "/pack");
749 dir
= opendir(path
.buf
);
752 error_errno("unable to open object pack directory: %s",
754 strbuf_release(&path
);
757 strbuf_addch(&path
, '/');
758 dirnamelen
= path
.len
;
759 while ((de
= readdir(dir
)) != NULL
) {
760 struct packed_git
*p
;
763 if (is_dot_or_dotdot(de
->d_name
))
766 strbuf_setlen(&path
, dirnamelen
);
767 strbuf_addstr(&path
, de
->d_name
);
770 if (strip_suffix_mem(path
.buf
, &base_len
, ".idx")) {
771 /* Don't reopen a pack we already have. */
772 for (p
= packed_git
; p
; p
= p
->next
) {
774 if (strip_suffix(p
->pack_name
, ".pack", &len
) &&
776 !memcmp(p
->pack_name
, path
.buf
, len
))
781 * See if it really is a valid .idx file with
782 * corresponding .pack file that we can map.
784 (p
= add_packed_git(path
.buf
, path
.len
, local
)) != NULL
)
785 install_packed_git(p
);
791 if (ends_with(de
->d_name
, ".idx") ||
792 ends_with(de
->d_name
, ".pack") ||
793 ends_with(de
->d_name
, ".bitmap") ||
794 ends_with(de
->d_name
, ".keep") ||
795 ends_with(de
->d_name
, ".promisor"))
796 string_list_append(&garbage
, path
.buf
);
798 report_garbage(PACKDIR_FILE_GARBAGE
, path
.buf
);
801 report_pack_garbage(&garbage
);
802 string_list_clear(&garbage
, 0);
803 strbuf_release(&path
);
806 static int approximate_object_count_valid
;
809 * Give a fast, rough count of the number of objects in the repository. This
810 * ignores loose objects completely. If you have a lot of them, then either
811 * you should repack because your performance will be awful, or they are
812 * all unreachable objects about to be pruned, in which case they're not really
813 * interesting as a measure of repo size in the first place.
815 unsigned long approximate_object_count(void)
817 static unsigned long count
;
818 if (!approximate_object_count_valid
) {
819 struct packed_git
*p
;
821 prepare_packed_git();
823 for (p
= packed_git
; p
; p
= p
->next
) {
824 if (open_pack_index(p
))
826 count
+= p
->num_objects
;
832 static void *get_next_packed_git(const void *p
)
834 return ((const struct packed_git
*)p
)->next
;
837 static void set_next_packed_git(void *p
, void *next
)
839 ((struct packed_git
*)p
)->next
= next
;
842 static int sort_pack(const void *a_
, const void *b_
)
844 const struct packed_git
*a
= a_
;
845 const struct packed_git
*b
= b_
;
849 * Local packs tend to contain objects specific to our
850 * variant of the project than remote ones. In addition,
851 * remote ones could be on a network mounted filesystem.
852 * Favor local ones for these reasons.
854 st
= a
->pack_local
- b
->pack_local
;
859 * Younger packs tend to contain more recent objects,
860 * and more recent objects tend to get accessed more
863 if (a
->mtime
< b
->mtime
)
865 else if (a
->mtime
== b
->mtime
)
870 static void rearrange_packed_git(void)
872 packed_git
= llist_mergesort(packed_git
, get_next_packed_git
,
873 set_next_packed_git
, sort_pack
);
876 static void prepare_packed_git_mru(void)
878 struct packed_git
*p
;
880 INIT_LIST_HEAD(&packed_git_mru
);
882 for (p
= packed_git
; p
; p
= p
->next
)
883 list_add_tail(&p
->mru
, &packed_git_mru
);
886 static int prepare_packed_git_run_once
= 0;
887 void prepare_packed_git(void)
889 struct alternate_object_database
*alt
;
891 if (prepare_packed_git_run_once
)
893 prepare_packed_git_one(get_object_directory(), 1);
895 for (alt
= alt_odb_list
; alt
; alt
= alt
->next
)
896 prepare_packed_git_one(alt
->path
, 0);
897 rearrange_packed_git();
898 prepare_packed_git_mru();
899 prepare_packed_git_run_once
= 1;
902 void reprepare_packed_git(void)
904 approximate_object_count_valid
= 0;
905 prepare_packed_git_run_once
= 0;
906 prepare_packed_git();
909 unsigned long unpack_object_header_buffer(const unsigned char *buf
,
910 unsigned long len
, enum object_type
*type
, unsigned long *sizep
)
913 unsigned long size
, c
;
914 unsigned long used
= 0;
917 *type
= (c
>> 4) & 7;
921 if (len
<= used
|| bitsizeof(long) <= shift
) {
922 error("bad object header");
927 size
+= (c
& 0x7f) << shift
;
934 unsigned long get_size_from_delta(struct packed_git
*p
,
935 struct pack_window
**w_curs
,
938 const unsigned char *data
;
939 unsigned char delta_head
[20], *in
;
943 memset(&stream
, 0, sizeof(stream
));
944 stream
.next_out
= delta_head
;
945 stream
.avail_out
= sizeof(delta_head
);
947 git_inflate_init(&stream
);
949 in
= use_pack(p
, w_curs
, curpos
, &stream
.avail_in
);
951 st
= git_inflate(&stream
, Z_FINISH
);
952 curpos
+= stream
.next_in
- in
;
953 } while ((st
== Z_OK
|| st
== Z_BUF_ERROR
) &&
954 stream
.total_out
< sizeof(delta_head
));
955 git_inflate_end(&stream
);
956 if ((st
!= Z_STREAM_END
) && stream
.total_out
!= sizeof(delta_head
)) {
957 error("delta data unpack-initial failed");
961 /* Examine the initial part of the delta to figure out
966 /* ignore base size */
967 get_delta_hdr_size(&data
, delta_head
+sizeof(delta_head
));
969 /* Read the result size */
970 return get_delta_hdr_size(&data
, delta_head
+sizeof(delta_head
));
973 int unpack_object_header(struct packed_git
*p
,
974 struct pack_window
**w_curs
,
976 unsigned long *sizep
)
981 enum object_type type
;
983 /* use_pack() assures us we have [base, base + 20) available
984 * as a range that we can look at. (Its actually the hash
985 * size that is assured.) With our object header encoding
986 * the maximum deflated object size is 2^137, which is just
987 * insane, so we know won't exceed what we have been given.
989 base
= use_pack(p
, w_curs
, *curpos
, &left
);
990 used
= unpack_object_header_buffer(base
, left
, &type
, sizep
);
999 void mark_bad_packed_object(struct packed_git
*p
, const unsigned char *sha1
)
1002 for (i
= 0; i
< p
->num_bad_objects
; i
++)
1003 if (!hashcmp(sha1
, p
->bad_object_sha1
+ GIT_SHA1_RAWSZ
* i
))
1005 p
->bad_object_sha1
= xrealloc(p
->bad_object_sha1
,
1006 st_mult(GIT_MAX_RAWSZ
,
1007 st_add(p
->num_bad_objects
, 1)));
1008 hashcpy(p
->bad_object_sha1
+ GIT_SHA1_RAWSZ
* p
->num_bad_objects
, sha1
);
1009 p
->num_bad_objects
++;
1012 const struct packed_git
*has_packed_and_bad(const unsigned char *sha1
)
1014 struct packed_git
*p
;
1017 for (p
= packed_git
; p
; p
= p
->next
)
1018 for (i
= 0; i
< p
->num_bad_objects
; i
++)
1019 if (!hashcmp(sha1
, p
->bad_object_sha1
+ 20 * i
))
1024 static off_t
get_delta_base(struct packed_git
*p
,
1025 struct pack_window
**w_curs
,
1027 enum object_type type
,
1028 off_t delta_obj_offset
)
1030 unsigned char *base_info
= use_pack(p
, w_curs
, *curpos
, NULL
);
1033 /* use_pack() assured us we have [base_info, base_info + 20)
1034 * as a range that we can look at without walking off the
1035 * end of the mapped window. Its actually the hash size
1036 * that is assured. An OFS_DELTA longer than the hash size
1037 * is stupid, as then a REF_DELTA would be smaller to store.
1039 if (type
== OBJ_OFS_DELTA
) {
1041 unsigned char c
= base_info
[used
++];
1042 base_offset
= c
& 127;
1045 if (!base_offset
|| MSB(base_offset
, 7))
1046 return 0; /* overflow */
1047 c
= base_info
[used
++];
1048 base_offset
= (base_offset
<< 7) + (c
& 127);
1050 base_offset
= delta_obj_offset
- base_offset
;
1051 if (base_offset
<= 0 || base_offset
>= delta_obj_offset
)
1052 return 0; /* out of bound */
1054 } else if (type
== OBJ_REF_DELTA
) {
1055 /* The base entry _must_ be in the same pack */
1056 base_offset
= find_pack_entry_one(base_info
, p
);
1059 die("I am totally screwed");
1064 * Like get_delta_base above, but we return the sha1 instead of the pack
1065 * offset. This means it is cheaper for REF deltas (we do not have to do
1066 * the final object lookup), but more expensive for OFS deltas (we
1067 * have to load the revidx to convert the offset back into a sha1).
1069 static const unsigned char *get_delta_base_sha1(struct packed_git
*p
,
1070 struct pack_window
**w_curs
,
1072 enum object_type type
,
1073 off_t delta_obj_offset
)
1075 if (type
== OBJ_REF_DELTA
) {
1076 unsigned char *base
= use_pack(p
, w_curs
, curpos
, NULL
);
1078 } else if (type
== OBJ_OFS_DELTA
) {
1079 struct revindex_entry
*revidx
;
1080 off_t base_offset
= get_delta_base(p
, w_curs
, &curpos
,
1081 type
, delta_obj_offset
);
1086 revidx
= find_pack_revindex(p
, base_offset
);
1090 return nth_packed_object_sha1(p
, revidx
->nr
);
1095 static int retry_bad_packed_offset(struct packed_git
*p
, off_t obj_offset
)
1098 struct revindex_entry
*revidx
;
1099 const unsigned char *sha1
;
1100 revidx
= find_pack_revindex(p
, obj_offset
);
1103 sha1
= nth_packed_object_sha1(p
, revidx
->nr
);
1104 mark_bad_packed_object(p
, sha1
);
1105 type
= sha1_object_info(sha1
, NULL
);
1106 if (type
<= OBJ_NONE
)
1111 #define POI_STACK_PREALLOC 64
1113 static enum object_type
packed_to_object_type(struct packed_git
*p
,
1115 enum object_type type
,
1116 struct pack_window
**w_curs
,
1119 off_t small_poi_stack
[POI_STACK_PREALLOC
];
1120 off_t
*poi_stack
= small_poi_stack
;
1121 int poi_stack_nr
= 0, poi_stack_alloc
= POI_STACK_PREALLOC
;
1123 while (type
== OBJ_OFS_DELTA
|| type
== OBJ_REF_DELTA
) {
1126 /* Push the object we're going to leave behind */
1127 if (poi_stack_nr
>= poi_stack_alloc
&& poi_stack
== small_poi_stack
) {
1128 poi_stack_alloc
= alloc_nr(poi_stack_nr
);
1129 ALLOC_ARRAY(poi_stack
, poi_stack_alloc
);
1130 memcpy(poi_stack
, small_poi_stack
, sizeof(off_t
)*poi_stack_nr
);
1132 ALLOC_GROW(poi_stack
, poi_stack_nr
+1, poi_stack_alloc
);
1134 poi_stack
[poi_stack_nr
++] = obj_offset
;
1135 /* If parsing the base offset fails, just unwind */
1136 base_offset
= get_delta_base(p
, w_curs
, &curpos
, type
, obj_offset
);
1139 curpos
= obj_offset
= base_offset
;
1140 type
= unpack_object_header(p
, w_curs
, &curpos
, &size
);
1141 if (type
<= OBJ_NONE
) {
1142 /* If getting the base itself fails, we first
1143 * retry the base, otherwise unwind */
1144 type
= retry_bad_packed_offset(p
, base_offset
);
1145 if (type
> OBJ_NONE
)
1159 error("unknown object type %i at offset %"PRIuMAX
" in %s",
1160 type
, (uintmax_t)obj_offset
, p
->pack_name
);
1165 if (poi_stack
!= small_poi_stack
)
1170 while (poi_stack_nr
) {
1171 obj_offset
= poi_stack
[--poi_stack_nr
];
1172 type
= retry_bad_packed_offset(p
, obj_offset
);
1173 if (type
> OBJ_NONE
)
1180 static struct hashmap delta_base_cache
;
1181 static size_t delta_base_cached
;
1183 static LIST_HEAD(delta_base_cache_lru
);
1185 struct delta_base_cache_key
{
1186 struct packed_git
*p
;
1190 struct delta_base_cache_entry
{
1191 struct hashmap hash
;
1192 struct delta_base_cache_key key
;
1193 struct list_head lru
;
1196 enum object_type type
;
1199 static unsigned int pack_entry_hash(struct packed_git
*p
, off_t base_offset
)
1203 hash
= (unsigned int)(intptr_t)p
+ (unsigned int)base_offset
;
1204 hash
+= (hash
>> 8) + (hash
>> 16);
1208 static struct delta_base_cache_entry
*
1209 get_delta_base_cache_entry(struct packed_git
*p
, off_t base_offset
)
1211 struct hashmap_entry entry
;
1212 struct delta_base_cache_key key
;
1214 if (!delta_base_cache
.cmpfn
)
1217 hashmap_entry_init(&entry
, pack_entry_hash(p
, base_offset
));
1219 key
.base_offset
= base_offset
;
1220 return hashmap_get(&delta_base_cache
, &entry
, &key
);
1223 static int delta_base_cache_key_eq(const struct delta_base_cache_key
*a
,
1224 const struct delta_base_cache_key
*b
)
1226 return a
->p
== b
->p
&& a
->base_offset
== b
->base_offset
;
1229 static int delta_base_cache_hash_cmp(const void *unused_cmp_data
,
1230 const void *va
, const void *vb
,
1233 const struct delta_base_cache_entry
*a
= va
, *b
= vb
;
1234 const struct delta_base_cache_key
*key
= vkey
;
1236 return !delta_base_cache_key_eq(&a
->key
, key
);
1238 return !delta_base_cache_key_eq(&a
->key
, &b
->key
);
1241 static int in_delta_base_cache(struct packed_git
*p
, off_t base_offset
)
1243 return !!get_delta_base_cache_entry(p
, base_offset
);
1247 * Remove the entry from the cache, but do _not_ free the associated
1248 * entry data. The caller takes ownership of the "data" buffer, and
1249 * should copy out any fields it wants before detaching.
1251 static void detach_delta_base_cache_entry(struct delta_base_cache_entry
*ent
)
1253 hashmap_remove(&delta_base_cache
, ent
, &ent
->key
);
1254 list_del(&ent
->lru
);
1255 delta_base_cached
-= ent
->size
;
1259 static void *cache_or_unpack_entry(struct packed_git
*p
, off_t base_offset
,
1260 unsigned long *base_size
, enum object_type
*type
)
1262 struct delta_base_cache_entry
*ent
;
1264 ent
= get_delta_base_cache_entry(p
, base_offset
);
1266 return unpack_entry(p
, base_offset
, type
, base_size
);
1271 *base_size
= ent
->size
;
1272 return xmemdupz(ent
->data
, ent
->size
);
1275 static inline void release_delta_base_cache(struct delta_base_cache_entry
*ent
)
1278 detach_delta_base_cache_entry(ent
);
1281 void clear_delta_base_cache(void)
1283 struct list_head
*lru
, *tmp
;
1284 list_for_each_safe(lru
, tmp
, &delta_base_cache_lru
) {
1285 struct delta_base_cache_entry
*entry
=
1286 list_entry(lru
, struct delta_base_cache_entry
, lru
);
1287 release_delta_base_cache(entry
);
1291 static void add_delta_base_cache(struct packed_git
*p
, off_t base_offset
,
1292 void *base
, unsigned long base_size
, enum object_type type
)
1294 struct delta_base_cache_entry
*ent
= xmalloc(sizeof(*ent
));
1295 struct list_head
*lru
, *tmp
;
1297 delta_base_cached
+= base_size
;
1299 list_for_each_safe(lru
, tmp
, &delta_base_cache_lru
) {
1300 struct delta_base_cache_entry
*f
=
1301 list_entry(lru
, struct delta_base_cache_entry
, lru
);
1302 if (delta_base_cached
<= delta_base_cache_limit
)
1304 release_delta_base_cache(f
);
1308 ent
->key
.base_offset
= base_offset
;
1311 ent
->size
= base_size
;
1312 list_add_tail(&ent
->lru
, &delta_base_cache_lru
);
1314 if (!delta_base_cache
.cmpfn
)
1315 hashmap_init(&delta_base_cache
, delta_base_cache_hash_cmp
, NULL
, 0);
1316 hashmap_entry_init(ent
, pack_entry_hash(p
, base_offset
));
1317 hashmap_add(&delta_base_cache
, ent
);
1320 int packed_object_info(struct packed_git
*p
, off_t obj_offset
,
1321 struct object_info
*oi
)
1323 struct pack_window
*w_curs
= NULL
;
1325 off_t curpos
= obj_offset
;
1326 enum object_type type
;
1329 * We always get the representation type, but only convert it to
1330 * a "real" type later if the caller is interested.
1333 *oi
->contentp
= cache_or_unpack_entry(p
, obj_offset
, oi
->sizep
,
1338 type
= unpack_object_header(p
, &w_curs
, &curpos
, &size
);
1341 if (!oi
->contentp
&& oi
->sizep
) {
1342 if (type
== OBJ_OFS_DELTA
|| type
== OBJ_REF_DELTA
) {
1343 off_t tmp_pos
= curpos
;
1344 off_t base_offset
= get_delta_base(p
, &w_curs
, &tmp_pos
,
1350 *oi
->sizep
= get_size_from_delta(p
, &w_curs
, tmp_pos
);
1351 if (*oi
->sizep
== 0) {
1360 if (oi
->disk_sizep
) {
1361 struct revindex_entry
*revidx
= find_pack_revindex(p
, obj_offset
);
1362 *oi
->disk_sizep
= revidx
[1].offset
- obj_offset
;
1365 if (oi
->typep
|| oi
->typename
) {
1366 enum object_type ptot
;
1367 ptot
= packed_to_object_type(p
, obj_offset
, type
, &w_curs
,
1372 const char *tn
= typename(ptot
);
1374 strbuf_addstr(oi
->typename
, tn
);
1382 if (oi
->delta_base_sha1
) {
1383 if (type
== OBJ_OFS_DELTA
|| type
== OBJ_REF_DELTA
) {
1384 const unsigned char *base
;
1386 base
= get_delta_base_sha1(p
, &w_curs
, curpos
,
1393 hashcpy(oi
->delta_base_sha1
, base
);
1395 hashclr(oi
->delta_base_sha1
);
1398 oi
->whence
= in_delta_base_cache(p
, obj_offset
) ? OI_DBCACHED
:
1402 unuse_pack(&w_curs
);
1406 static void *unpack_compressed_entry(struct packed_git
*p
,
1407 struct pack_window
**w_curs
,
1413 unsigned char *buffer
, *in
;
1415 buffer
= xmallocz_gently(size
);
1418 memset(&stream
, 0, sizeof(stream
));
1419 stream
.next_out
= buffer
;
1420 stream
.avail_out
= size
+ 1;
1422 git_inflate_init(&stream
);
1424 in
= use_pack(p
, w_curs
, curpos
, &stream
.avail_in
);
1425 stream
.next_in
= in
;
1426 st
= git_inflate(&stream
, Z_FINISH
);
1427 if (!stream
.avail_out
)
1428 break; /* the payload is larger than it should be */
1429 curpos
+= stream
.next_in
- in
;
1430 } while (st
== Z_OK
|| st
== Z_BUF_ERROR
);
1431 git_inflate_end(&stream
);
1432 if ((st
!= Z_STREAM_END
) || stream
.total_out
!= size
) {
1440 static void write_pack_access_log(struct packed_git
*p
, off_t obj_offset
)
1442 static struct trace_key pack_access
= TRACE_KEY_INIT(PACK_ACCESS
);
1443 trace_printf_key(&pack_access
, "%s %"PRIuMAX
"\n",
1444 p
->pack_name
, (uintmax_t)obj_offset
);
1447 int do_check_packed_object_crc
;
1449 #define UNPACK_ENTRY_STACK_PREALLOC 64
1450 struct unpack_entry_stack_ent
{
1456 static void *read_object(const unsigned char *sha1
, enum object_type
*type
,
1457 unsigned long *size
)
1459 struct object_info oi
= OBJECT_INFO_INIT
;
1463 oi
.contentp
= &content
;
1465 if (sha1_object_info_extended(sha1
, &oi
, 0) < 0)
1470 void *unpack_entry(struct packed_git
*p
, off_t obj_offset
,
1471 enum object_type
*final_type
, unsigned long *final_size
)
1473 struct pack_window
*w_curs
= NULL
;
1474 off_t curpos
= obj_offset
;
1477 enum object_type type
;
1478 struct unpack_entry_stack_ent small_delta_stack
[UNPACK_ENTRY_STACK_PREALLOC
];
1479 struct unpack_entry_stack_ent
*delta_stack
= small_delta_stack
;
1480 int delta_stack_nr
= 0, delta_stack_alloc
= UNPACK_ENTRY_STACK_PREALLOC
;
1481 int base_from_cache
= 0;
1483 write_pack_access_log(p
, obj_offset
);
1485 /* PHASE 1: drill down to the innermost base object */
1489 struct delta_base_cache_entry
*ent
;
1491 ent
= get_delta_base_cache_entry(p
, curpos
);
1496 detach_delta_base_cache_entry(ent
);
1497 base_from_cache
= 1;
1501 if (do_check_packed_object_crc
&& p
->index_version
> 1) {
1502 struct revindex_entry
*revidx
= find_pack_revindex(p
, obj_offset
);
1503 off_t len
= revidx
[1].offset
- obj_offset
;
1504 if (check_pack_crc(p
, &w_curs
, obj_offset
, len
, revidx
->nr
)) {
1505 const unsigned char *sha1
=
1506 nth_packed_object_sha1(p
, revidx
->nr
);
1507 error("bad packed object CRC for %s",
1509 mark_bad_packed_object(p
, sha1
);
1515 type
= unpack_object_header(p
, &w_curs
, &curpos
, &size
);
1516 if (type
!= OBJ_OFS_DELTA
&& type
!= OBJ_REF_DELTA
)
1519 base_offset
= get_delta_base(p
, &w_curs
, &curpos
, type
, obj_offset
);
1521 error("failed to validate delta base reference "
1522 "at offset %"PRIuMAX
" from %s",
1523 (uintmax_t)curpos
, p
->pack_name
);
1524 /* bail to phase 2, in hopes of recovery */
1529 /* push object, proceed to base */
1530 if (delta_stack_nr
>= delta_stack_alloc
1531 && delta_stack
== small_delta_stack
) {
1532 delta_stack_alloc
= alloc_nr(delta_stack_nr
);
1533 ALLOC_ARRAY(delta_stack
, delta_stack_alloc
);
1534 memcpy(delta_stack
, small_delta_stack
,
1535 sizeof(*delta_stack
)*delta_stack_nr
);
1537 ALLOC_GROW(delta_stack
, delta_stack_nr
+1, delta_stack_alloc
);
1539 i
= delta_stack_nr
++;
1540 delta_stack
[i
].obj_offset
= obj_offset
;
1541 delta_stack
[i
].curpos
= curpos
;
1542 delta_stack
[i
].size
= size
;
1544 curpos
= obj_offset
= base_offset
;
1547 /* PHASE 2: handle the base */
1552 die("BUG: unpack_entry: left loop at a valid delta");
1558 if (!base_from_cache
)
1559 data
= unpack_compressed_entry(p
, &w_curs
, curpos
, size
);
1563 error("unknown object type %i at offset %"PRIuMAX
" in %s",
1564 type
, (uintmax_t)obj_offset
, p
->pack_name
);
1567 /* PHASE 3: apply deltas in order */
1570 * 'data' holds the base data, or NULL if there was corruption
1572 while (delta_stack_nr
) {
1575 void *external_base
= NULL
;
1576 unsigned long delta_size
, base_size
= size
;
1582 add_delta_base_cache(p
, obj_offset
, base
, base_size
, type
);
1586 * We're probably in deep shit, but let's try to fetch
1587 * the required base anyway from another pack or loose.
1588 * This is costly but should happen only in the presence
1589 * of a corrupted pack, and is better than failing outright.
1591 struct revindex_entry
*revidx
;
1592 const unsigned char *base_sha1
;
1593 revidx
= find_pack_revindex(p
, obj_offset
);
1595 base_sha1
= nth_packed_object_sha1(p
, revidx
->nr
);
1596 error("failed to read delta base object %s"
1597 " at offset %"PRIuMAX
" from %s",
1598 sha1_to_hex(base_sha1
), (uintmax_t)obj_offset
,
1600 mark_bad_packed_object(p
, base_sha1
);
1601 base
= read_object(base_sha1
, &type
, &base_size
);
1602 external_base
= base
;
1606 i
= --delta_stack_nr
;
1607 obj_offset
= delta_stack
[i
].obj_offset
;
1608 curpos
= delta_stack
[i
].curpos
;
1609 delta_size
= delta_stack
[i
].size
;
1614 delta_data
= unpack_compressed_entry(p
, &w_curs
, curpos
, delta_size
);
1617 error("failed to unpack compressed delta "
1618 "at offset %"PRIuMAX
" from %s",
1619 (uintmax_t)curpos
, p
->pack_name
);
1621 free(external_base
);
1625 data
= patch_delta(base
, base_size
,
1626 delta_data
, delta_size
,
1630 * We could not apply the delta; warn the user, but keep going.
1631 * Our failure will be noticed either in the next iteration of
1632 * the loop, or if this is the final delta, in the caller when
1633 * we return NULL. Those code paths will take care of making
1634 * a more explicit warning and retrying with another copy of
1638 error("failed to apply delta");
1641 free(external_base
);
1650 unuse_pack(&w_curs
);
1652 if (delta_stack
!= small_delta_stack
)
1658 const unsigned char *nth_packed_object_sha1(struct packed_git
*p
,
1661 const unsigned char *index
= p
->index_data
;
1663 if (open_pack_index(p
))
1665 index
= p
->index_data
;
1667 if (n
>= p
->num_objects
)
1670 if (p
->index_version
== 1) {
1671 return index
+ 24 * n
+ 4;
1674 return index
+ 20 * n
;
1678 const struct object_id
*nth_packed_object_oid(struct object_id
*oid
,
1679 struct packed_git
*p
,
1682 const unsigned char *hash
= nth_packed_object_sha1(p
, n
);
1685 hashcpy(oid
->hash
, hash
);
1689 void check_pack_index_ptr(const struct packed_git
*p
, const void *vptr
)
1691 const unsigned char *ptr
= vptr
;
1692 const unsigned char *start
= p
->index_data
;
1693 const unsigned char *end
= start
+ p
->index_size
;
1695 die(_("offset before start of pack index for %s (corrupt index?)"),
1697 /* No need to check for underflow; .idx files must be at least 8 bytes */
1699 die(_("offset beyond end of pack index for %s (truncated index?)"),
1703 off_t
nth_packed_object_offset(const struct packed_git
*p
, uint32_t n
)
1705 const unsigned char *index
= p
->index_data
;
1707 if (p
->index_version
== 1) {
1708 return ntohl(*((uint32_t *)(index
+ 24 * n
)));
1711 index
+= 8 + p
->num_objects
* (20 + 4);
1712 off
= ntohl(*((uint32_t *)(index
+ 4 * n
)));
1713 if (!(off
& 0x80000000))
1715 index
+= p
->num_objects
* 4 + (off
& 0x7fffffff) * 8;
1716 check_pack_index_ptr(p
, index
);
1717 return get_be64(index
);
1721 off_t
find_pack_entry_one(const unsigned char *sha1
,
1722 struct packed_git
*p
)
1724 const uint32_t *level1_ofs
= p
->index_data
;
1725 const unsigned char *index
= p
->index_data
;
1726 unsigned hi
, lo
, stride
;
1727 static int debug_lookup
= -1;
1729 if (debug_lookup
< 0)
1730 debug_lookup
= !!getenv("GIT_DEBUG_LOOKUP");
1733 if (open_pack_index(p
))
1735 level1_ofs
= p
->index_data
;
1736 index
= p
->index_data
;
1738 if (p
->index_version
> 1) {
1743 hi
= ntohl(level1_ofs
[*sha1
]);
1744 lo
= ((*sha1
== 0x0) ? 0 : ntohl(level1_ofs
[*sha1
- 1]));
1745 if (p
->index_version
> 1) {
1753 printf("%02x%02x%02x... lo %u hi %u nr %"PRIu32
"\n",
1754 sha1
[0], sha1
[1], sha1
[2], lo
, hi
, p
->num_objects
);
1757 unsigned mi
= lo
+ (hi
- lo
) / 2;
1758 int cmp
= hashcmp(index
+ mi
* stride
, sha1
);
1761 printf("lo %u hi %u rg %u mi %u\n",
1762 lo
, hi
, hi
- lo
, mi
);
1764 return nth_packed_object_offset(p
, mi
);
1773 int is_pack_valid(struct packed_git
*p
)
1775 /* An already open pack is known to be valid. */
1776 if (p
->pack_fd
!= -1)
1779 /* If the pack has one window completely covering the
1780 * file size, the pack is known to be valid even if
1781 * the descriptor is not currently open.
1784 struct pack_window
*w
= p
->windows
;
1786 if (!w
->offset
&& w
->len
== p
->pack_size
)
1790 /* Force the pack to open to prove its valid. */
1791 return !open_packed_git(p
);
1794 struct packed_git
*find_sha1_pack(const unsigned char *sha1
,
1795 struct packed_git
*packs
)
1797 struct packed_git
*p
;
1799 for (p
= packs
; p
; p
= p
->next
) {
1800 if (find_pack_entry_one(sha1
, p
))
1807 static int fill_pack_entry(const unsigned char *sha1
,
1808 struct pack_entry
*e
,
1809 struct packed_git
*p
)
1813 if (p
->num_bad_objects
) {
1815 for (i
= 0; i
< p
->num_bad_objects
; i
++)
1816 if (!hashcmp(sha1
, p
->bad_object_sha1
+ 20 * i
))
1820 offset
= find_pack_entry_one(sha1
, p
);
1825 * We are about to tell the caller where they can locate the
1826 * requested object. We better make sure the packfile is
1827 * still here and can be accessed before supplying that
1828 * answer, as it may have been deleted since the index was
1831 if (!is_pack_valid(p
))
1835 hashcpy(e
->sha1
, sha1
);
1840 * Iff a pack file contains the object named by sha1, return true and
1841 * store its location to e.
1843 int find_pack_entry(const unsigned char *sha1
, struct pack_entry
*e
)
1845 struct list_head
*pos
;
1847 prepare_packed_git();
1851 list_for_each(pos
, &packed_git_mru
) {
1852 struct packed_git
*p
= list_entry(pos
, struct packed_git
, mru
);
1853 if (fill_pack_entry(sha1
, e
, p
)) {
1854 list_move(&p
->mru
, &packed_git_mru
);
1861 int has_sha1_pack(const unsigned char *sha1
)
1863 struct pack_entry e
;
1864 return find_pack_entry(sha1
, &e
);
1867 int has_pack_index(const unsigned char *sha1
)
1870 if (stat(sha1_pack_index_name(sha1
), &st
))
1875 static int for_each_object_in_pack(struct packed_git
*p
, each_packed_object_fn cb
, void *data
)
1880 for (i
= 0; i
< p
->num_objects
; i
++) {
1881 struct object_id oid
;
1883 if (!nth_packed_object_oid(&oid
, p
, i
))
1884 return error("unable to get sha1 of object %u in %s",
1887 r
= cb(&oid
, p
, i
, data
);
1894 int for_each_packed_object(each_packed_object_fn cb
, void *data
, unsigned flags
)
1896 struct packed_git
*p
;
1898 int pack_errors
= 0;
1900 prepare_packed_git();
1901 for (p
= packed_git
; p
; p
= p
->next
) {
1902 if ((flags
& FOR_EACH_OBJECT_LOCAL_ONLY
) && !p
->pack_local
)
1904 if ((flags
& FOR_EACH_OBJECT_PROMISOR_ONLY
) &&
1907 if (open_pack_index(p
)) {
1911 r
= for_each_object_in_pack(p
, cb
, data
);
1915 return r
? r
: pack_errors
;
1918 static int add_promisor_object(const struct object_id
*oid
,
1919 struct packed_git
*pack
,
1923 struct oidset
*set
= set_
;
1924 struct object
*obj
= parse_object(oid
);
1928 oidset_insert(set
, oid
);
1931 * If this is a tree, commit, or tag, the objects it refers
1932 * to are also promisor objects. (Blobs refer to no objects.)
1934 if (obj
->type
== OBJ_TREE
) {
1935 struct tree
*tree
= (struct tree
*)obj
;
1936 struct tree_desc desc
;
1937 struct name_entry entry
;
1938 if (init_tree_desc_gently(&desc
, tree
->buffer
, tree
->size
))
1940 * Error messages are given when packs are
1941 * verified, so do not print any here.
1944 while (tree_entry_gently(&desc
, &entry
))
1945 oidset_insert(set
, entry
.oid
);
1946 } else if (obj
->type
== OBJ_COMMIT
) {
1947 struct commit
*commit
= (struct commit
*) obj
;
1948 struct commit_list
*parents
= commit
->parents
;
1950 oidset_insert(set
, &commit
->tree
->object
.oid
);
1951 for (; parents
; parents
= parents
->next
)
1952 oidset_insert(set
, &parents
->item
->object
.oid
);
1953 } else if (obj
->type
== OBJ_TAG
) {
1954 struct tag
*tag
= (struct tag
*) obj
;
1955 oidset_insert(set
, &tag
->tagged
->oid
);
1960 int is_promisor_object(const struct object_id
*oid
)
1962 static struct oidset promisor_objects
;
1963 static int promisor_objects_prepared
;
1965 if (!promisor_objects_prepared
) {
1966 if (repository_format_partial_clone
) {
1967 for_each_packed_object(add_promisor_object
,
1969 FOR_EACH_OBJECT_PROMISOR_ONLY
);
1971 promisor_objects_prepared
= 1;
1973 return oidset_contains(&promisor_objects
, oid
);