10 #include "sha1-lookup.h"
14 #include "tree-walk.h"
17 char *odb_pack_name(struct strbuf
*buf
,
18 const unsigned char *sha1
,
22 strbuf_addf(buf
, "%s/pack/pack-%s.%s", get_object_directory(),
23 sha1_to_hex(sha1
), ext
);
27 char *sha1_pack_name(const unsigned char *sha1
)
29 static struct strbuf buf
= STRBUF_INIT
;
30 return odb_pack_name(&buf
, sha1
, "pack");
33 char *sha1_pack_index_name(const unsigned char *sha1
)
35 static struct strbuf buf
= STRBUF_INIT
;
36 return odb_pack_name(&buf
, sha1
, "idx");
39 static unsigned int pack_used_ctr
;
40 static unsigned int pack_mmap_calls
;
41 static unsigned int peak_pack_open_windows
;
42 static unsigned int pack_open_windows
;
43 static unsigned int pack_open_fds
;
44 static unsigned int pack_max_fds
;
45 static size_t peak_pack_mapped
;
46 static size_t pack_mapped
;
47 struct packed_git
*packed_git
;
48 struct mru packed_git_mru
;
50 #define SZ_FMT PRIuMAX
51 static inline uintmax_t sz_fmt(size_t s
) { return s
; }
53 void pack_report(void)
56 "pack_report: getpagesize() = %10" SZ_FMT
"\n"
57 "pack_report: core.packedGitWindowSize = %10" SZ_FMT
"\n"
58 "pack_report: core.packedGitLimit = %10" SZ_FMT
"\n",
59 sz_fmt(getpagesize()),
60 sz_fmt(packed_git_window_size
),
61 sz_fmt(packed_git_limit
));
63 "pack_report: pack_used_ctr = %10u\n"
64 "pack_report: pack_mmap_calls = %10u\n"
65 "pack_report: pack_open_windows = %10u / %10u\n"
66 "pack_report: pack_mapped = "
67 "%10" SZ_FMT
" / %10" SZ_FMT
"\n",
70 pack_open_windows
, peak_pack_open_windows
,
71 sz_fmt(pack_mapped
), sz_fmt(peak_pack_mapped
));
75 * Open and mmap the index file at path, perform a couple of
76 * consistency checks, then record its information to p. Return 0 on
79 static int check_packed_git_idx(const char *path
, struct packed_git
*p
)
82 struct pack_idx_header
*hdr
;
84 uint32_t version
, nr
, i
, *index
;
85 int fd
= git_open(path
);
94 idx_size
= xsize_t(st
.st_size
);
95 if (idx_size
< 4 * 256 + 20 + 20) {
97 return error("index file %s is too small", path
);
99 idx_map
= xmmap(NULL
, idx_size
, PROT_READ
, MAP_PRIVATE
, fd
, 0);
103 if (hdr
->idx_signature
== htonl(PACK_IDX_SIGNATURE
)) {
104 version
= ntohl(hdr
->idx_version
);
105 if (version
< 2 || version
> 2) {
106 munmap(idx_map
, idx_size
);
107 return error("index file %s is version %"PRIu32
108 " and is not supported by this binary"
109 " (try upgrading GIT to a newer version)",
118 index
+= 2; /* skip index header */
119 for (i
= 0; i
< 256; i
++) {
120 uint32_t n
= ntohl(index
[i
]);
122 munmap(idx_map
, idx_size
);
123 return error("non-monotonic index %s", path
);
131 * - 256 index entries 4 bytes each
132 * - 24-byte entries * nr (20-byte sha1 + 4-byte offset)
133 * - 20-byte SHA1 of the packfile
134 * - 20-byte SHA1 file checksum
136 if (idx_size
!= 4*256 + nr
* 24 + 20 + 20) {
137 munmap(idx_map
, idx_size
);
138 return error("wrong index v1 file size in %s", path
);
140 } else if (version
== 2) {
143 * - 8 bytes of header
144 * - 256 index entries 4 bytes each
145 * - 20-byte sha1 entry * nr
146 * - 4-byte crc entry * nr
147 * - 4-byte offset entry * nr
148 * - 20-byte SHA1 of the packfile
149 * - 20-byte SHA1 file checksum
150 * And after the 4-byte offset table might be a
151 * variable sized table containing 8-byte entries
152 * for offsets larger than 2^31.
154 unsigned long min_size
= 8 + 4*256 + nr
*(20 + 4 + 4) + 20 + 20;
155 unsigned long max_size
= min_size
;
157 max_size
+= (nr
- 1)*8;
158 if (idx_size
< min_size
|| idx_size
> max_size
) {
159 munmap(idx_map
, idx_size
);
160 return error("wrong index v2 file size in %s", path
);
162 if (idx_size
!= min_size
&&
164 * make sure we can deal with large pack offsets.
165 * 31-bit signed offset won't be enough, neither
166 * 32-bit unsigned one will be.
168 (sizeof(off_t
) <= 4)) {
169 munmap(idx_map
, idx_size
);
170 return error("pack too large for current definition of off_t in %s", path
);
174 p
->index_version
= version
;
175 p
->index_data
= idx_map
;
176 p
->index_size
= idx_size
;
181 int open_pack_index(struct packed_git
*p
)
190 if (!strip_suffix(p
->pack_name
, ".pack", &len
))
191 die("BUG: pack_name does not end in .pack");
192 idx_name
= xstrfmt("%.*s.idx", (int)len
, p
->pack_name
);
193 ret
= check_packed_git_idx(idx_name
, p
);
198 static struct packed_git
*alloc_packed_git(int extra
)
200 struct packed_git
*p
= xmalloc(st_add(sizeof(*p
), extra
));
201 memset(p
, 0, sizeof(*p
));
206 struct packed_git
*parse_pack_index(unsigned char *sha1
, const char *idx_path
)
208 const char *path
= sha1_pack_name(sha1
);
209 size_t alloc
= st_add(strlen(path
), 1);
210 struct packed_git
*p
= alloc_packed_git(alloc
);
212 memcpy(p
->pack_name
, path
, alloc
); /* includes NUL */
213 hashcpy(p
->sha1
, sha1
);
214 if (check_packed_git_idx(idx_path
, p
)) {
222 static void scan_windows(struct packed_git
*p
,
223 struct packed_git
**lru_p
,
224 struct pack_window
**lru_w
,
225 struct pack_window
**lru_l
)
227 struct pack_window
*w
, *w_l
;
229 for (w_l
= NULL
, w
= p
->windows
; w
; w
= w
->next
) {
231 if (!*lru_w
|| w
->last_used
< (*lru_w
)->last_used
) {
241 static int unuse_one_window(struct packed_git
*current
)
243 struct packed_git
*p
, *lru_p
= NULL
;
244 struct pack_window
*lru_w
= NULL
, *lru_l
= NULL
;
247 scan_windows(current
, &lru_p
, &lru_w
, &lru_l
);
248 for (p
= packed_git
; p
; p
= p
->next
)
249 scan_windows(p
, &lru_p
, &lru_w
, &lru_l
);
251 munmap(lru_w
->base
, lru_w
->len
);
252 pack_mapped
-= lru_w
->len
;
254 lru_l
->next
= lru_w
->next
;
256 lru_p
->windows
= lru_w
->next
;
264 void release_pack_memory(size_t need
)
266 size_t cur
= pack_mapped
;
267 while (need
>= (cur
- pack_mapped
) && unuse_one_window(NULL
))
271 void close_pack_windows(struct packed_git
*p
)
274 struct pack_window
*w
= p
->windows
;
277 die("pack '%s' still has open windows to it",
279 munmap(w
->base
, w
->len
);
280 pack_mapped
-= w
->len
;
282 p
->windows
= w
->next
;
287 static int close_pack_fd(struct packed_git
*p
)
299 void close_pack_index(struct packed_git
*p
)
302 munmap((void *)p
->index_data
, p
->index_size
);
303 p
->index_data
= NULL
;
307 static void close_pack(struct packed_git
*p
)
309 close_pack_windows(p
);
314 void close_all_packs(void)
316 struct packed_git
*p
;
318 for (p
= packed_git
; p
; p
= p
->next
)
320 die("BUG: want to close pack marked 'do-not-close'");
326 * The LRU pack is the one with the oldest MRU window, preferring packs
327 * with no used windows, or the oldest mtime if it has no windows allocated.
329 static void find_lru_pack(struct packed_git
*p
, struct packed_git
**lru_p
, struct pack_window
**mru_w
, int *accept_windows_inuse
)
331 struct pack_window
*w
, *this_mru_w
;
332 int has_windows_inuse
= 0;
335 * Reject this pack if it has windows and the previously selected
336 * one does not. If this pack does not have windows, reject
337 * it if the pack file is newer than the previously selected one.
339 if (*lru_p
&& !*mru_w
&& (p
->windows
|| p
->mtime
> (*lru_p
)->mtime
))
342 for (w
= this_mru_w
= p
->windows
; w
; w
= w
->next
) {
344 * Reject this pack if any of its windows are in use,
345 * but the previously selected pack did not have any
346 * inuse windows. Otherwise, record that this pack
347 * has windows in use.
350 if (*accept_windows_inuse
)
351 has_windows_inuse
= 1;
356 if (w
->last_used
> this_mru_w
->last_used
)
360 * Reject this pack if it has windows that have been
361 * used more recently than the previously selected pack.
362 * If the previously selected pack had windows inuse and
363 * we have not encountered a window in this pack that is
364 * inuse, skip this check since we prefer a pack with no
365 * inuse windows to one that has inuse windows.
367 if (*mru_w
&& *accept_windows_inuse
== has_windows_inuse
&&
368 this_mru_w
->last_used
> (*mru_w
)->last_used
)
377 *accept_windows_inuse
= has_windows_inuse
;
380 static int close_one_pack(void)
382 struct packed_git
*p
, *lru_p
= NULL
;
383 struct pack_window
*mru_w
= NULL
;
384 int accept_windows_inuse
= 1;
386 for (p
= packed_git
; p
; p
= p
->next
) {
387 if (p
->pack_fd
== -1)
389 find_lru_pack(p
, &lru_p
, &mru_w
, &accept_windows_inuse
);
393 return close_pack_fd(lru_p
);
398 static unsigned int get_max_fd_limit(void)
404 if (!getrlimit(RLIMIT_NOFILE
, &lim
))
411 long open_max
= sysconf(_SC_OPEN_MAX
);
415 * Otherwise, we got -1 for one of the two
418 * (1) sysconf() did not understand _SC_OPEN_MAX
419 * and signaled an error with -1; or
420 * (2) sysconf() said there is no limit.
422 * We _could_ clear errno before calling sysconf() to
423 * tell these two cases apart and return a huge number
424 * in the latter case to let the caller cap it to a
425 * value that is not so selfish, but letting the
426 * fallback OPEN_MAX codepath take care of these cases
435 return 1; /* see the caller ;-) */
440 * Do not call this directly as this leaks p->pack_fd on error return;
441 * call open_packed_git() instead.
443 static int open_packed_git_1(struct packed_git
*p
)
446 struct pack_header hdr
;
447 unsigned char sha1
[20];
448 unsigned char *idx_sha1
;
452 if (!p
->index_data
&& open_pack_index(p
))
453 return error("packfile %s index unavailable", p
->pack_name
);
456 unsigned int max_fds
= get_max_fd_limit();
458 /* Save 3 for stdin/stdout/stderr, 22 for work */
460 pack_max_fds
= max_fds
- 25;
465 while (pack_max_fds
<= pack_open_fds
&& close_one_pack())
468 p
->pack_fd
= git_open(p
->pack_name
);
469 if (p
->pack_fd
< 0 || fstat(p
->pack_fd
, &st
))
473 /* If we created the struct before we had the pack we lack size. */
475 if (!S_ISREG(st
.st_mode
))
476 return error("packfile %s not a regular file", p
->pack_name
);
477 p
->pack_size
= st
.st_size
;
478 } else if (p
->pack_size
!= st
.st_size
)
479 return error("packfile %s size changed", p
->pack_name
);
481 /* We leave these file descriptors open with sliding mmap;
482 * there is no point keeping them open across exec(), though.
484 fd_flag
= fcntl(p
->pack_fd
, F_GETFD
, 0);
486 return error("cannot determine file descriptor flags");
487 fd_flag
|= FD_CLOEXEC
;
488 if (fcntl(p
->pack_fd
, F_SETFD
, fd_flag
) == -1)
489 return error("cannot set FD_CLOEXEC");
491 /* Verify we recognize this pack file format. */
492 read_result
= read_in_full(p
->pack_fd
, &hdr
, sizeof(hdr
));
494 return error_errno("error reading from %s", p
->pack_name
);
495 if (read_result
!= sizeof(hdr
))
496 return error("file %s is far too short to be a packfile", p
->pack_name
);
497 if (hdr
.hdr_signature
!= htonl(PACK_SIGNATURE
))
498 return error("file %s is not a GIT packfile", p
->pack_name
);
499 if (!pack_version_ok(hdr
.hdr_version
))
500 return error("packfile %s is version %"PRIu32
" and not"
501 " supported (try upgrading GIT to a newer version)",
502 p
->pack_name
, ntohl(hdr
.hdr_version
));
504 /* Verify the pack matches its index. */
505 if (p
->num_objects
!= ntohl(hdr
.hdr_entries
))
506 return error("packfile %s claims to have %"PRIu32
" objects"
507 " while index indicates %"PRIu32
" objects",
508 p
->pack_name
, ntohl(hdr
.hdr_entries
),
510 if (lseek(p
->pack_fd
, p
->pack_size
- sizeof(sha1
), SEEK_SET
) == -1)
511 return error("end of packfile %s is unavailable", p
->pack_name
);
512 read_result
= read_in_full(p
->pack_fd
, sha1
, sizeof(sha1
));
514 return error_errno("error reading from %s", p
->pack_name
);
515 if (read_result
!= sizeof(sha1
))
516 return error("packfile %s signature is unavailable", p
->pack_name
);
517 idx_sha1
= ((unsigned char *)p
->index_data
) + p
->index_size
- 40;
518 if (hashcmp(sha1
, idx_sha1
))
519 return error("packfile %s does not match index", p
->pack_name
);
523 static int open_packed_git(struct packed_git
*p
)
525 if (!open_packed_git_1(p
))
531 static int in_window(struct pack_window
*win
, off_t offset
)
533 /* We must promise at least 20 bytes (one hash) after the
534 * offset is available from this window, otherwise the offset
535 * is not actually in this window and a different window (which
536 * has that one hash excess) must be used. This is to support
537 * the object header and delta base parsing routines below.
539 off_t win_off
= win
->offset
;
540 return win_off
<= offset
541 && (offset
+ 20) <= (win_off
+ win
->len
);
544 unsigned char *use_pack(struct packed_git
*p
,
545 struct pack_window
**w_cursor
,
549 struct pack_window
*win
= *w_cursor
;
551 /* Since packfiles end in a hash of their content and it's
552 * pointless to ask for an offset into the middle of that
553 * hash, and the in_window function above wouldn't match
554 * don't allow an offset too close to the end of the file.
556 if (!p
->pack_size
&& p
->pack_fd
== -1 && open_packed_git(p
))
557 die("packfile %s cannot be accessed", p
->pack_name
);
558 if (offset
> (p
->pack_size
- 20))
559 die("offset beyond end of packfile (truncated pack?)");
561 die(_("offset before end of packfile (broken .idx?)"));
563 if (!win
|| !in_window(win
, offset
)) {
566 for (win
= p
->windows
; win
; win
= win
->next
) {
567 if (in_window(win
, offset
))
571 size_t window_align
= packed_git_window_size
/ 2;
574 if (p
->pack_fd
== -1 && open_packed_git(p
))
575 die("packfile %s cannot be accessed", p
->pack_name
);
577 win
= xcalloc(1, sizeof(*win
));
578 win
->offset
= (offset
/ window_align
) * window_align
;
579 len
= p
->pack_size
- win
->offset
;
580 if (len
> packed_git_window_size
)
581 len
= packed_git_window_size
;
582 win
->len
= (size_t)len
;
583 pack_mapped
+= win
->len
;
584 while (packed_git_limit
< pack_mapped
585 && unuse_one_window(p
))
587 win
->base
= xmmap(NULL
, win
->len
,
588 PROT_READ
, MAP_PRIVATE
,
589 p
->pack_fd
, win
->offset
);
590 if (win
->base
== MAP_FAILED
)
591 die_errno("packfile %s cannot be mapped",
593 if (!win
->offset
&& win
->len
== p
->pack_size
598 if (pack_mapped
> peak_pack_mapped
)
599 peak_pack_mapped
= pack_mapped
;
600 if (pack_open_windows
> peak_pack_open_windows
)
601 peak_pack_open_windows
= pack_open_windows
;
602 win
->next
= p
->windows
;
606 if (win
!= *w_cursor
) {
607 win
->last_used
= pack_used_ctr
++;
611 offset
-= win
->offset
;
613 *left
= win
->len
- xsize_t(offset
);
614 return win
->base
+ offset
;
617 void unuse_pack(struct pack_window
**w_cursor
)
619 struct pack_window
*w
= *w_cursor
;
626 static void try_to_free_pack_memory(size_t size
)
628 release_pack_memory(size
);
631 struct packed_git
*add_packed_git(const char *path
, size_t path_len
, int local
)
633 static int have_set_try_to_free_routine
;
636 struct packed_git
*p
;
638 if (!have_set_try_to_free_routine
) {
639 have_set_try_to_free_routine
= 1;
640 set_try_to_free_routine(try_to_free_pack_memory
);
644 * Make sure a corresponding .pack file exists and that
645 * the index looks sane.
647 if (!strip_suffix_mem(path
, &path_len
, ".idx"))
651 * ".promisor" is long enough to hold any suffix we're adding (and
652 * the use xsnprintf double-checks that)
654 alloc
= st_add3(path_len
, strlen(".promisor"), 1);
655 p
= alloc_packed_git(alloc
);
656 memcpy(p
->pack_name
, path
, path_len
);
658 xsnprintf(p
->pack_name
+ path_len
, alloc
- path_len
, ".keep");
659 if (!access(p
->pack_name
, F_OK
))
662 xsnprintf(p
->pack_name
+ path_len
, alloc
- path_len
, ".promisor");
663 if (!access(p
->pack_name
, F_OK
))
664 p
->pack_promisor
= 1;
666 xsnprintf(p
->pack_name
+ path_len
, alloc
- path_len
, ".pack");
667 if (stat(p
->pack_name
, &st
) || !S_ISREG(st
.st_mode
)) {
672 /* ok, it looks sane as far as we can check without
673 * actually mapping the pack file.
675 p
->pack_size
= st
.st_size
;
676 p
->pack_local
= local
;
677 p
->mtime
= st
.st_mtime
;
678 if (path_len
< 40 || get_sha1_hex(path
+ path_len
- 40, p
->sha1
))
683 void install_packed_git(struct packed_git
*pack
)
685 if (pack
->pack_fd
!= -1)
688 pack
->next
= packed_git
;
692 void (*report_garbage
)(unsigned seen_bits
, const char *path
);
694 static void report_helper(const struct string_list
*list
,
695 int seen_bits
, int first
, int last
)
697 if (seen_bits
== (PACKDIR_FILE_PACK
|PACKDIR_FILE_IDX
))
700 for (; first
< last
; first
++)
701 report_garbage(seen_bits
, list
->items
[first
].string
);
704 static void report_pack_garbage(struct string_list
*list
)
706 int i
, baselen
= -1, first
= 0, seen_bits
= 0;
711 string_list_sort(list
);
713 for (i
= 0; i
< list
->nr
; i
++) {
714 const char *path
= list
->items
[i
].string
;
716 strncmp(path
, list
->items
[first
].string
, baselen
)) {
717 report_helper(list
, seen_bits
, first
, i
);
722 const char *dot
= strrchr(path
, '.');
724 report_garbage(PACKDIR_FILE_GARBAGE
, path
);
727 baselen
= dot
- path
+ 1;
730 if (!strcmp(path
+ baselen
, "pack"))
732 else if (!strcmp(path
+ baselen
, "idx"))
735 report_helper(list
, seen_bits
, first
, list
->nr
);
738 static void prepare_packed_git_one(char *objdir
, int local
)
740 struct strbuf path
= STRBUF_INIT
;
744 struct string_list garbage
= STRING_LIST_INIT_DUP
;
746 strbuf_addstr(&path
, objdir
);
747 strbuf_addstr(&path
, "/pack");
748 dir
= opendir(path
.buf
);
751 error_errno("unable to open object pack directory: %s",
753 strbuf_release(&path
);
756 strbuf_addch(&path
, '/');
757 dirnamelen
= path
.len
;
758 while ((de
= readdir(dir
)) != NULL
) {
759 struct packed_git
*p
;
762 if (is_dot_or_dotdot(de
->d_name
))
765 strbuf_setlen(&path
, dirnamelen
);
766 strbuf_addstr(&path
, de
->d_name
);
769 if (strip_suffix_mem(path
.buf
, &base_len
, ".idx")) {
770 /* Don't reopen a pack we already have. */
771 for (p
= packed_git
; p
; p
= p
->next
) {
773 if (strip_suffix(p
->pack_name
, ".pack", &len
) &&
775 !memcmp(p
->pack_name
, path
.buf
, len
))
780 * See if it really is a valid .idx file with
781 * corresponding .pack file that we can map.
783 (p
= add_packed_git(path
.buf
, path
.len
, local
)) != NULL
)
784 install_packed_git(p
);
790 if (ends_with(de
->d_name
, ".idx") ||
791 ends_with(de
->d_name
, ".pack") ||
792 ends_with(de
->d_name
, ".bitmap") ||
793 ends_with(de
->d_name
, ".keep") ||
794 ends_with(de
->d_name
, ".promisor"))
795 string_list_append(&garbage
, path
.buf
);
797 report_garbage(PACKDIR_FILE_GARBAGE
, path
.buf
);
800 report_pack_garbage(&garbage
);
801 string_list_clear(&garbage
, 0);
802 strbuf_release(&path
);
805 static int approximate_object_count_valid
;
808 * Give a fast, rough count of the number of objects in the repository. This
809 * ignores loose objects completely. If you have a lot of them, then either
810 * you should repack because your performance will be awful, or they are
811 * all unreachable objects about to be pruned, in which case they're not really
812 * interesting as a measure of repo size in the first place.
814 unsigned long approximate_object_count(void)
816 static unsigned long count
;
817 if (!approximate_object_count_valid
) {
818 struct packed_git
*p
;
820 prepare_packed_git();
822 for (p
= packed_git
; p
; p
= p
->next
) {
823 if (open_pack_index(p
))
825 count
+= p
->num_objects
;
831 static void *get_next_packed_git(const void *p
)
833 return ((const struct packed_git
*)p
)->next
;
836 static void set_next_packed_git(void *p
, void *next
)
838 ((struct packed_git
*)p
)->next
= next
;
841 static int sort_pack(const void *a_
, const void *b_
)
843 const struct packed_git
*a
= a_
;
844 const struct packed_git
*b
= b_
;
848 * Local packs tend to contain objects specific to our
849 * variant of the project than remote ones. In addition,
850 * remote ones could be on a network mounted filesystem.
851 * Favor local ones for these reasons.
853 st
= a
->pack_local
- b
->pack_local
;
858 * Younger packs tend to contain more recent objects,
859 * and more recent objects tend to get accessed more
862 if (a
->mtime
< b
->mtime
)
864 else if (a
->mtime
== b
->mtime
)
869 static void rearrange_packed_git(void)
871 packed_git
= llist_mergesort(packed_git
, get_next_packed_git
,
872 set_next_packed_git
, sort_pack
);
875 static void prepare_packed_git_mru(void)
877 struct packed_git
*p
;
879 mru_clear(&packed_git_mru
);
880 for (p
= packed_git
; p
; p
= p
->next
)
881 mru_append(&packed_git_mru
, p
);
884 static int prepare_packed_git_run_once
= 0;
885 void prepare_packed_git(void)
887 struct alternate_object_database
*alt
;
889 if (prepare_packed_git_run_once
)
891 prepare_packed_git_one(get_object_directory(), 1);
893 for (alt
= alt_odb_list
; alt
; alt
= alt
->next
)
894 prepare_packed_git_one(alt
->path
, 0);
895 rearrange_packed_git();
896 prepare_packed_git_mru();
897 prepare_packed_git_run_once
= 1;
900 void reprepare_packed_git(void)
902 approximate_object_count_valid
= 0;
903 prepare_packed_git_run_once
= 0;
904 prepare_packed_git();
907 unsigned long unpack_object_header_buffer(const unsigned char *buf
,
908 unsigned long len
, enum object_type
*type
, unsigned long *sizep
)
911 unsigned long size
, c
;
912 unsigned long used
= 0;
915 *type
= (c
>> 4) & 7;
919 if (len
<= used
|| bitsizeof(long) <= shift
) {
920 error("bad object header");
925 size
+= (c
& 0x7f) << shift
;
932 unsigned long get_size_from_delta(struct packed_git
*p
,
933 struct pack_window
**w_curs
,
936 const unsigned char *data
;
937 unsigned char delta_head
[20], *in
;
941 memset(&stream
, 0, sizeof(stream
));
942 stream
.next_out
= delta_head
;
943 stream
.avail_out
= sizeof(delta_head
);
945 git_inflate_init(&stream
);
947 in
= use_pack(p
, w_curs
, curpos
, &stream
.avail_in
);
949 st
= git_inflate(&stream
, Z_FINISH
);
950 curpos
+= stream
.next_in
- in
;
951 } while ((st
== Z_OK
|| st
== Z_BUF_ERROR
) &&
952 stream
.total_out
< sizeof(delta_head
));
953 git_inflate_end(&stream
);
954 if ((st
!= Z_STREAM_END
) && stream
.total_out
!= sizeof(delta_head
)) {
955 error("delta data unpack-initial failed");
959 /* Examine the initial part of the delta to figure out
964 /* ignore base size */
965 get_delta_hdr_size(&data
, delta_head
+sizeof(delta_head
));
967 /* Read the result size */
968 return get_delta_hdr_size(&data
, delta_head
+sizeof(delta_head
));
971 int unpack_object_header(struct packed_git
*p
,
972 struct pack_window
**w_curs
,
974 unsigned long *sizep
)
979 enum object_type type
;
981 /* use_pack() assures us we have [base, base + 20) available
982 * as a range that we can look at. (Its actually the hash
983 * size that is assured.) With our object header encoding
984 * the maximum deflated object size is 2^137, which is just
985 * insane, so we know won't exceed what we have been given.
987 base
= use_pack(p
, w_curs
, *curpos
, &left
);
988 used
= unpack_object_header_buffer(base
, left
, &type
, sizep
);
997 void mark_bad_packed_object(struct packed_git
*p
, const unsigned char *sha1
)
1000 for (i
= 0; i
< p
->num_bad_objects
; i
++)
1001 if (!hashcmp(sha1
, p
->bad_object_sha1
+ GIT_SHA1_RAWSZ
* i
))
1003 p
->bad_object_sha1
= xrealloc(p
->bad_object_sha1
,
1004 st_mult(GIT_MAX_RAWSZ
,
1005 st_add(p
->num_bad_objects
, 1)));
1006 hashcpy(p
->bad_object_sha1
+ GIT_SHA1_RAWSZ
* p
->num_bad_objects
, sha1
);
1007 p
->num_bad_objects
++;
1010 const struct packed_git
*has_packed_and_bad(const unsigned char *sha1
)
1012 struct packed_git
*p
;
1015 for (p
= packed_git
; p
; p
= p
->next
)
1016 for (i
= 0; i
< p
->num_bad_objects
; i
++)
1017 if (!hashcmp(sha1
, p
->bad_object_sha1
+ 20 * i
))
1022 static off_t
get_delta_base(struct packed_git
*p
,
1023 struct pack_window
**w_curs
,
1025 enum object_type type
,
1026 off_t delta_obj_offset
)
1028 unsigned char *base_info
= use_pack(p
, w_curs
, *curpos
, NULL
);
1031 /* use_pack() assured us we have [base_info, base_info + 20)
1032 * as a range that we can look at without walking off the
1033 * end of the mapped window. Its actually the hash size
1034 * that is assured. An OFS_DELTA longer than the hash size
1035 * is stupid, as then a REF_DELTA would be smaller to store.
1037 if (type
== OBJ_OFS_DELTA
) {
1039 unsigned char c
= base_info
[used
++];
1040 base_offset
= c
& 127;
1043 if (!base_offset
|| MSB(base_offset
, 7))
1044 return 0; /* overflow */
1045 c
= base_info
[used
++];
1046 base_offset
= (base_offset
<< 7) + (c
& 127);
1048 base_offset
= delta_obj_offset
- base_offset
;
1049 if (base_offset
<= 0 || base_offset
>= delta_obj_offset
)
1050 return 0; /* out of bound */
1052 } else if (type
== OBJ_REF_DELTA
) {
1053 /* The base entry _must_ be in the same pack */
1054 base_offset
= find_pack_entry_one(base_info
, p
);
1057 die("I am totally screwed");
1062 * Like get_delta_base above, but we return the sha1 instead of the pack
1063 * offset. This means it is cheaper for REF deltas (we do not have to do
1064 * the final object lookup), but more expensive for OFS deltas (we
1065 * have to load the revidx to convert the offset back into a sha1).
1067 static const unsigned char *get_delta_base_sha1(struct packed_git
*p
,
1068 struct pack_window
**w_curs
,
1070 enum object_type type
,
1071 off_t delta_obj_offset
)
1073 if (type
== OBJ_REF_DELTA
) {
1074 unsigned char *base
= use_pack(p
, w_curs
, curpos
, NULL
);
1076 } else if (type
== OBJ_OFS_DELTA
) {
1077 struct revindex_entry
*revidx
;
1078 off_t base_offset
= get_delta_base(p
, w_curs
, &curpos
,
1079 type
, delta_obj_offset
);
1084 revidx
= find_pack_revindex(p
, base_offset
);
1088 return nth_packed_object_sha1(p
, revidx
->nr
);
1093 static int retry_bad_packed_offset(struct packed_git
*p
, off_t obj_offset
)
1096 struct revindex_entry
*revidx
;
1097 const unsigned char *sha1
;
1098 revidx
= find_pack_revindex(p
, obj_offset
);
1101 sha1
= nth_packed_object_sha1(p
, revidx
->nr
);
1102 mark_bad_packed_object(p
, sha1
);
1103 type
= sha1_object_info(sha1
, NULL
);
1104 if (type
<= OBJ_NONE
)
1109 #define POI_STACK_PREALLOC 64
1111 static enum object_type
packed_to_object_type(struct packed_git
*p
,
1113 enum object_type type
,
1114 struct pack_window
**w_curs
,
1117 off_t small_poi_stack
[POI_STACK_PREALLOC
];
1118 off_t
*poi_stack
= small_poi_stack
;
1119 int poi_stack_nr
= 0, poi_stack_alloc
= POI_STACK_PREALLOC
;
1121 while (type
== OBJ_OFS_DELTA
|| type
== OBJ_REF_DELTA
) {
1124 /* Push the object we're going to leave behind */
1125 if (poi_stack_nr
>= poi_stack_alloc
&& poi_stack
== small_poi_stack
) {
1126 poi_stack_alloc
= alloc_nr(poi_stack_nr
);
1127 ALLOC_ARRAY(poi_stack
, poi_stack_alloc
);
1128 memcpy(poi_stack
, small_poi_stack
, sizeof(off_t
)*poi_stack_nr
);
1130 ALLOC_GROW(poi_stack
, poi_stack_nr
+1, poi_stack_alloc
);
1132 poi_stack
[poi_stack_nr
++] = obj_offset
;
1133 /* If parsing the base offset fails, just unwind */
1134 base_offset
= get_delta_base(p
, w_curs
, &curpos
, type
, obj_offset
);
1137 curpos
= obj_offset
= base_offset
;
1138 type
= unpack_object_header(p
, w_curs
, &curpos
, &size
);
1139 if (type
<= OBJ_NONE
) {
1140 /* If getting the base itself fails, we first
1141 * retry the base, otherwise unwind */
1142 type
= retry_bad_packed_offset(p
, base_offset
);
1143 if (type
> OBJ_NONE
)
1157 error("unknown object type %i at offset %"PRIuMAX
" in %s",
1158 type
, (uintmax_t)obj_offset
, p
->pack_name
);
1163 if (poi_stack
!= small_poi_stack
)
1168 while (poi_stack_nr
) {
1169 obj_offset
= poi_stack
[--poi_stack_nr
];
1170 type
= retry_bad_packed_offset(p
, obj_offset
);
1171 if (type
> OBJ_NONE
)
1178 static struct hashmap delta_base_cache
;
1179 static size_t delta_base_cached
;
1181 static LIST_HEAD(delta_base_cache_lru
);
1183 struct delta_base_cache_key
{
1184 struct packed_git
*p
;
1188 struct delta_base_cache_entry
{
1189 struct hashmap hash
;
1190 struct delta_base_cache_key key
;
1191 struct list_head lru
;
1194 enum object_type type
;
1197 static unsigned int pack_entry_hash(struct packed_git
*p
, off_t base_offset
)
1201 hash
= (unsigned int)(intptr_t)p
+ (unsigned int)base_offset
;
1202 hash
+= (hash
>> 8) + (hash
>> 16);
1206 static struct delta_base_cache_entry
*
1207 get_delta_base_cache_entry(struct packed_git
*p
, off_t base_offset
)
1209 struct hashmap_entry entry
;
1210 struct delta_base_cache_key key
;
1212 if (!delta_base_cache
.cmpfn
)
1215 hashmap_entry_init(&entry
, pack_entry_hash(p
, base_offset
));
1217 key
.base_offset
= base_offset
;
1218 return hashmap_get(&delta_base_cache
, &entry
, &key
);
1221 static int delta_base_cache_key_eq(const struct delta_base_cache_key
*a
,
1222 const struct delta_base_cache_key
*b
)
1224 return a
->p
== b
->p
&& a
->base_offset
== b
->base_offset
;
1227 static int delta_base_cache_hash_cmp(const void *unused_cmp_data
,
1228 const void *va
, const void *vb
,
1231 const struct delta_base_cache_entry
*a
= va
, *b
= vb
;
1232 const struct delta_base_cache_key
*key
= vkey
;
1234 return !delta_base_cache_key_eq(&a
->key
, key
);
1236 return !delta_base_cache_key_eq(&a
->key
, &b
->key
);
1239 static int in_delta_base_cache(struct packed_git
*p
, off_t base_offset
)
1241 return !!get_delta_base_cache_entry(p
, base_offset
);
1245 * Remove the entry from the cache, but do _not_ free the associated
1246 * entry data. The caller takes ownership of the "data" buffer, and
1247 * should copy out any fields it wants before detaching.
1249 static void detach_delta_base_cache_entry(struct delta_base_cache_entry
*ent
)
1251 hashmap_remove(&delta_base_cache
, ent
, &ent
->key
);
1252 list_del(&ent
->lru
);
1253 delta_base_cached
-= ent
->size
;
1257 static void *cache_or_unpack_entry(struct packed_git
*p
, off_t base_offset
,
1258 unsigned long *base_size
, enum object_type
*type
)
1260 struct delta_base_cache_entry
*ent
;
1262 ent
= get_delta_base_cache_entry(p
, base_offset
);
1264 return unpack_entry(p
, base_offset
, type
, base_size
);
1269 *base_size
= ent
->size
;
1270 return xmemdupz(ent
->data
, ent
->size
);
1273 static inline void release_delta_base_cache(struct delta_base_cache_entry
*ent
)
1276 detach_delta_base_cache_entry(ent
);
1279 void clear_delta_base_cache(void)
1281 struct list_head
*lru
, *tmp
;
1282 list_for_each_safe(lru
, tmp
, &delta_base_cache_lru
) {
1283 struct delta_base_cache_entry
*entry
=
1284 list_entry(lru
, struct delta_base_cache_entry
, lru
);
1285 release_delta_base_cache(entry
);
1289 static void add_delta_base_cache(struct packed_git
*p
, off_t base_offset
,
1290 void *base
, unsigned long base_size
, enum object_type type
)
1292 struct delta_base_cache_entry
*ent
= xmalloc(sizeof(*ent
));
1293 struct list_head
*lru
, *tmp
;
1295 delta_base_cached
+= base_size
;
1297 list_for_each_safe(lru
, tmp
, &delta_base_cache_lru
) {
1298 struct delta_base_cache_entry
*f
=
1299 list_entry(lru
, struct delta_base_cache_entry
, lru
);
1300 if (delta_base_cached
<= delta_base_cache_limit
)
1302 release_delta_base_cache(f
);
1306 ent
->key
.base_offset
= base_offset
;
1309 ent
->size
= base_size
;
1310 list_add_tail(&ent
->lru
, &delta_base_cache_lru
);
1312 if (!delta_base_cache
.cmpfn
)
1313 hashmap_init(&delta_base_cache
, delta_base_cache_hash_cmp
, NULL
, 0);
1314 hashmap_entry_init(ent
, pack_entry_hash(p
, base_offset
));
1315 hashmap_add(&delta_base_cache
, ent
);
1318 int packed_object_info(struct packed_git
*p
, off_t obj_offset
,
1319 struct object_info
*oi
)
1321 struct pack_window
*w_curs
= NULL
;
1323 off_t curpos
= obj_offset
;
1324 enum object_type type
;
1327 * We always get the representation type, but only convert it to
1328 * a "real" type later if the caller is interested.
1331 *oi
->contentp
= cache_or_unpack_entry(p
, obj_offset
, oi
->sizep
,
1336 type
= unpack_object_header(p
, &w_curs
, &curpos
, &size
);
1339 if (!oi
->contentp
&& oi
->sizep
) {
1340 if (type
== OBJ_OFS_DELTA
|| type
== OBJ_REF_DELTA
) {
1341 off_t tmp_pos
= curpos
;
1342 off_t base_offset
= get_delta_base(p
, &w_curs
, &tmp_pos
,
1348 *oi
->sizep
= get_size_from_delta(p
, &w_curs
, tmp_pos
);
1349 if (*oi
->sizep
== 0) {
1358 if (oi
->disk_sizep
) {
1359 struct revindex_entry
*revidx
= find_pack_revindex(p
, obj_offset
);
1360 *oi
->disk_sizep
= revidx
[1].offset
- obj_offset
;
1363 if (oi
->typep
|| oi
->typename
) {
1364 enum object_type ptot
;
1365 ptot
= packed_to_object_type(p
, obj_offset
, type
, &w_curs
,
1370 const char *tn
= typename(ptot
);
1372 strbuf_addstr(oi
->typename
, tn
);
1380 if (oi
->delta_base_sha1
) {
1381 if (type
== OBJ_OFS_DELTA
|| type
== OBJ_REF_DELTA
) {
1382 const unsigned char *base
;
1384 base
= get_delta_base_sha1(p
, &w_curs
, curpos
,
1391 hashcpy(oi
->delta_base_sha1
, base
);
1393 hashclr(oi
->delta_base_sha1
);
1396 oi
->whence
= in_delta_base_cache(p
, obj_offset
) ? OI_DBCACHED
:
1400 unuse_pack(&w_curs
);
1404 static void *unpack_compressed_entry(struct packed_git
*p
,
1405 struct pack_window
**w_curs
,
1411 unsigned char *buffer
, *in
;
1413 buffer
= xmallocz_gently(size
);
1416 memset(&stream
, 0, sizeof(stream
));
1417 stream
.next_out
= buffer
;
1418 stream
.avail_out
= size
+ 1;
1420 git_inflate_init(&stream
);
1422 in
= use_pack(p
, w_curs
, curpos
, &stream
.avail_in
);
1423 stream
.next_in
= in
;
1424 st
= git_inflate(&stream
, Z_FINISH
);
1425 if (!stream
.avail_out
)
1426 break; /* the payload is larger than it should be */
1427 curpos
+= stream
.next_in
- in
;
1428 } while (st
== Z_OK
|| st
== Z_BUF_ERROR
);
1429 git_inflate_end(&stream
);
1430 if ((st
!= Z_STREAM_END
) || stream
.total_out
!= size
) {
1438 static void write_pack_access_log(struct packed_git
*p
, off_t obj_offset
)
1440 static struct trace_key pack_access
= TRACE_KEY_INIT(PACK_ACCESS
);
1441 trace_printf_key(&pack_access
, "%s %"PRIuMAX
"\n",
1442 p
->pack_name
, (uintmax_t)obj_offset
);
1445 int do_check_packed_object_crc
;
1447 #define UNPACK_ENTRY_STACK_PREALLOC 64
1448 struct unpack_entry_stack_ent
{
1454 static void *read_object(const unsigned char *sha1
, enum object_type
*type
,
1455 unsigned long *size
)
1457 struct object_info oi
= OBJECT_INFO_INIT
;
1461 oi
.contentp
= &content
;
1463 if (sha1_object_info_extended(sha1
, &oi
, 0) < 0)
1468 void *unpack_entry(struct packed_git
*p
, off_t obj_offset
,
1469 enum object_type
*final_type
, unsigned long *final_size
)
1471 struct pack_window
*w_curs
= NULL
;
1472 off_t curpos
= obj_offset
;
1475 enum object_type type
;
1476 struct unpack_entry_stack_ent small_delta_stack
[UNPACK_ENTRY_STACK_PREALLOC
];
1477 struct unpack_entry_stack_ent
*delta_stack
= small_delta_stack
;
1478 int delta_stack_nr
= 0, delta_stack_alloc
= UNPACK_ENTRY_STACK_PREALLOC
;
1479 int base_from_cache
= 0;
1481 write_pack_access_log(p
, obj_offset
);
1483 /* PHASE 1: drill down to the innermost base object */
1487 struct delta_base_cache_entry
*ent
;
1489 ent
= get_delta_base_cache_entry(p
, curpos
);
1494 detach_delta_base_cache_entry(ent
);
1495 base_from_cache
= 1;
1499 if (do_check_packed_object_crc
&& p
->index_version
> 1) {
1500 struct revindex_entry
*revidx
= find_pack_revindex(p
, obj_offset
);
1501 off_t len
= revidx
[1].offset
- obj_offset
;
1502 if (check_pack_crc(p
, &w_curs
, obj_offset
, len
, revidx
->nr
)) {
1503 const unsigned char *sha1
=
1504 nth_packed_object_sha1(p
, revidx
->nr
);
1505 error("bad packed object CRC for %s",
1507 mark_bad_packed_object(p
, sha1
);
1513 type
= unpack_object_header(p
, &w_curs
, &curpos
, &size
);
1514 if (type
!= OBJ_OFS_DELTA
&& type
!= OBJ_REF_DELTA
)
1517 base_offset
= get_delta_base(p
, &w_curs
, &curpos
, type
, obj_offset
);
1519 error("failed to validate delta base reference "
1520 "at offset %"PRIuMAX
" from %s",
1521 (uintmax_t)curpos
, p
->pack_name
);
1522 /* bail to phase 2, in hopes of recovery */
1527 /* push object, proceed to base */
1528 if (delta_stack_nr
>= delta_stack_alloc
1529 && delta_stack
== small_delta_stack
) {
1530 delta_stack_alloc
= alloc_nr(delta_stack_nr
);
1531 ALLOC_ARRAY(delta_stack
, delta_stack_alloc
);
1532 memcpy(delta_stack
, small_delta_stack
,
1533 sizeof(*delta_stack
)*delta_stack_nr
);
1535 ALLOC_GROW(delta_stack
, delta_stack_nr
+1, delta_stack_alloc
);
1537 i
= delta_stack_nr
++;
1538 delta_stack
[i
].obj_offset
= obj_offset
;
1539 delta_stack
[i
].curpos
= curpos
;
1540 delta_stack
[i
].size
= size
;
1542 curpos
= obj_offset
= base_offset
;
1545 /* PHASE 2: handle the base */
1550 die("BUG: unpack_entry: left loop at a valid delta");
1556 if (!base_from_cache
)
1557 data
= unpack_compressed_entry(p
, &w_curs
, curpos
, size
);
1561 error("unknown object type %i at offset %"PRIuMAX
" in %s",
1562 type
, (uintmax_t)obj_offset
, p
->pack_name
);
1565 /* PHASE 3: apply deltas in order */
1568 * 'data' holds the base data, or NULL if there was corruption
1570 while (delta_stack_nr
) {
1573 void *external_base
= NULL
;
1574 unsigned long delta_size
, base_size
= size
;
1580 add_delta_base_cache(p
, obj_offset
, base
, base_size
, type
);
1584 * We're probably in deep shit, but let's try to fetch
1585 * the required base anyway from another pack or loose.
1586 * This is costly but should happen only in the presence
1587 * of a corrupted pack, and is better than failing outright.
1589 struct revindex_entry
*revidx
;
1590 const unsigned char *base_sha1
;
1591 revidx
= find_pack_revindex(p
, obj_offset
);
1593 base_sha1
= nth_packed_object_sha1(p
, revidx
->nr
);
1594 error("failed to read delta base object %s"
1595 " at offset %"PRIuMAX
" from %s",
1596 sha1_to_hex(base_sha1
), (uintmax_t)obj_offset
,
1598 mark_bad_packed_object(p
, base_sha1
);
1599 base
= read_object(base_sha1
, &type
, &base_size
);
1600 external_base
= base
;
1604 i
= --delta_stack_nr
;
1605 obj_offset
= delta_stack
[i
].obj_offset
;
1606 curpos
= delta_stack
[i
].curpos
;
1607 delta_size
= delta_stack
[i
].size
;
1612 delta_data
= unpack_compressed_entry(p
, &w_curs
, curpos
, delta_size
);
1615 error("failed to unpack compressed delta "
1616 "at offset %"PRIuMAX
" from %s",
1617 (uintmax_t)curpos
, p
->pack_name
);
1619 free(external_base
);
1623 data
= patch_delta(base
, base_size
,
1624 delta_data
, delta_size
,
1628 * We could not apply the delta; warn the user, but keep going.
1629 * Our failure will be noticed either in the next iteration of
1630 * the loop, or if this is the final delta, in the caller when
1631 * we return NULL. Those code paths will take care of making
1632 * a more explicit warning and retrying with another copy of
1636 error("failed to apply delta");
1639 free(external_base
);
1648 unuse_pack(&w_curs
);
1650 if (delta_stack
!= small_delta_stack
)
1656 const unsigned char *nth_packed_object_sha1(struct packed_git
*p
,
1659 const unsigned char *index
= p
->index_data
;
1661 if (open_pack_index(p
))
1663 index
= p
->index_data
;
1665 if (n
>= p
->num_objects
)
1668 if (p
->index_version
== 1) {
1669 return index
+ 24 * n
+ 4;
1672 return index
+ 20 * n
;
1676 const struct object_id
*nth_packed_object_oid(struct object_id
*oid
,
1677 struct packed_git
*p
,
1680 const unsigned char *hash
= nth_packed_object_sha1(p
, n
);
1683 hashcpy(oid
->hash
, hash
);
1687 void check_pack_index_ptr(const struct packed_git
*p
, const void *vptr
)
1689 const unsigned char *ptr
= vptr
;
1690 const unsigned char *start
= p
->index_data
;
1691 const unsigned char *end
= start
+ p
->index_size
;
1693 die(_("offset before start of pack index for %s (corrupt index?)"),
1695 /* No need to check for underflow; .idx files must be at least 8 bytes */
1697 die(_("offset beyond end of pack index for %s (truncated index?)"),
1701 off_t
nth_packed_object_offset(const struct packed_git
*p
, uint32_t n
)
1703 const unsigned char *index
= p
->index_data
;
1705 if (p
->index_version
== 1) {
1706 return ntohl(*((uint32_t *)(index
+ 24 * n
)));
1709 index
+= 8 + p
->num_objects
* (20 + 4);
1710 off
= ntohl(*((uint32_t *)(index
+ 4 * n
)));
1711 if (!(off
& 0x80000000))
1713 index
+= p
->num_objects
* 4 + (off
& 0x7fffffff) * 8;
1714 check_pack_index_ptr(p
, index
);
1715 return (((uint64_t)ntohl(*((uint32_t *)(index
+ 0)))) << 32) |
1716 ntohl(*((uint32_t *)(index
+ 4)));
1720 off_t
find_pack_entry_one(const unsigned char *sha1
,
1721 struct packed_git
*p
)
1723 const uint32_t *level1_ofs
= p
->index_data
;
1724 const unsigned char *index
= p
->index_data
;
1725 unsigned hi
, lo
, stride
;
1726 static int debug_lookup
= -1;
1728 if (debug_lookup
< 0)
1729 debug_lookup
= !!getenv("GIT_DEBUG_LOOKUP");
1732 if (open_pack_index(p
))
1734 level1_ofs
= p
->index_data
;
1735 index
= p
->index_data
;
1737 if (p
->index_version
> 1) {
1742 hi
= ntohl(level1_ofs
[*sha1
]);
1743 lo
= ((*sha1
== 0x0) ? 0 : ntohl(level1_ofs
[*sha1
- 1]));
1744 if (p
->index_version
> 1) {
1752 printf("%02x%02x%02x... lo %u hi %u nr %"PRIu32
"\n",
1753 sha1
[0], sha1
[1], sha1
[2], lo
, hi
, p
->num_objects
);
1756 unsigned mi
= lo
+ (hi
- lo
) / 2;
1757 int cmp
= hashcmp(index
+ mi
* stride
, sha1
);
1760 printf("lo %u hi %u rg %u mi %u\n",
1761 lo
, hi
, hi
- lo
, mi
);
1763 return nth_packed_object_offset(p
, mi
);
1772 int is_pack_valid(struct packed_git
*p
)
1774 /* An already open pack is known to be valid. */
1775 if (p
->pack_fd
!= -1)
1778 /* If the pack has one window completely covering the
1779 * file size, the pack is known to be valid even if
1780 * the descriptor is not currently open.
1783 struct pack_window
*w
= p
->windows
;
1785 if (!w
->offset
&& w
->len
== p
->pack_size
)
1789 /* Force the pack to open to prove its valid. */
1790 return !open_packed_git(p
);
1793 struct packed_git
*find_sha1_pack(const unsigned char *sha1
,
1794 struct packed_git
*packs
)
1796 struct packed_git
*p
;
1798 for (p
= packs
; p
; p
= p
->next
) {
1799 if (find_pack_entry_one(sha1
, p
))
1806 static int fill_pack_entry(const unsigned char *sha1
,
1807 struct pack_entry
*e
,
1808 struct packed_git
*p
)
1812 if (p
->num_bad_objects
) {
1814 for (i
= 0; i
< p
->num_bad_objects
; i
++)
1815 if (!hashcmp(sha1
, p
->bad_object_sha1
+ 20 * i
))
1819 offset
= find_pack_entry_one(sha1
, p
);
1824 * We are about to tell the caller where they can locate the
1825 * requested object. We better make sure the packfile is
1826 * still here and can be accessed before supplying that
1827 * answer, as it may have been deleted since the index was
1830 if (!is_pack_valid(p
))
1834 hashcpy(e
->sha1
, sha1
);
1839 * Iff a pack file contains the object named by sha1, return true and
1840 * store its location to e.
1842 int find_pack_entry(const unsigned char *sha1
, struct pack_entry
*e
)
1844 struct mru_entry
*p
;
1846 prepare_packed_git();
1850 for (p
= packed_git_mru
.head
; p
; p
= p
->next
) {
1851 if (fill_pack_entry(sha1
, e
, p
->item
)) {
1852 mru_mark(&packed_git_mru
, p
);
1859 int has_sha1_pack(const unsigned char *sha1
)
1861 struct pack_entry e
;
1862 return find_pack_entry(sha1
, &e
);
1865 int has_pack_index(const unsigned char *sha1
)
1868 if (stat(sha1_pack_index_name(sha1
), &st
))
1873 static int for_each_object_in_pack(struct packed_git
*p
, each_packed_object_fn cb
, void *data
)
1878 for (i
= 0; i
< p
->num_objects
; i
++) {
1879 struct object_id oid
;
1881 if (!nth_packed_object_oid(&oid
, p
, i
))
1882 return error("unable to get sha1 of object %u in %s",
1885 r
= cb(&oid
, p
, i
, data
);
1892 int for_each_packed_object(each_packed_object_fn cb
, void *data
, unsigned flags
)
1894 struct packed_git
*p
;
1896 int pack_errors
= 0;
1898 prepare_packed_git();
1899 for (p
= packed_git
; p
; p
= p
->next
) {
1900 if ((flags
& FOR_EACH_OBJECT_LOCAL_ONLY
) && !p
->pack_local
)
1902 if ((flags
& FOR_EACH_OBJECT_PROMISOR_ONLY
) &&
1905 if (open_pack_index(p
)) {
1909 r
= for_each_object_in_pack(p
, cb
, data
);
1913 return r
? r
: pack_errors
;
1916 static int add_promisor_object(const struct object_id
*oid
,
1917 struct packed_git
*pack
,
1921 struct oidset
*set
= set_
;
1922 struct object
*obj
= parse_object(oid
);
1926 oidset_insert(set
, oid
);
1929 * If this is a tree, commit, or tag, the objects it refers
1930 * to are also promisor objects. (Blobs refer to no objects.)
1932 if (obj
->type
== OBJ_TREE
) {
1933 struct tree
*tree
= (struct tree
*)obj
;
1934 struct tree_desc desc
;
1935 struct name_entry entry
;
1936 if (init_tree_desc_gently(&desc
, tree
->buffer
, tree
->size
))
1938 * Error messages are given when packs are
1939 * verified, so do not print any here.
1942 while (tree_entry_gently(&desc
, &entry
))
1943 oidset_insert(set
, entry
.oid
);
1944 } else if (obj
->type
== OBJ_COMMIT
) {
1945 struct commit
*commit
= (struct commit
*) obj
;
1946 struct commit_list
*parents
= commit
->parents
;
1948 oidset_insert(set
, &commit
->tree
->object
.oid
);
1949 for (; parents
; parents
= parents
->next
)
1950 oidset_insert(set
, &parents
->item
->object
.oid
);
1951 } else if (obj
->type
== OBJ_TAG
) {
1952 struct tag
*tag
= (struct tag
*) obj
;
1953 oidset_insert(set
, &tag
->tagged
->oid
);
1958 int is_promisor_object(const struct object_id
*oid
)
1960 static struct oidset promisor_objects
;
1961 static int promisor_objects_prepared
;
1963 if (!promisor_objects_prepared
) {
1964 if (repository_format_partial_clone
) {
1965 for_each_packed_object(add_promisor_object
,
1967 FOR_EACH_OBJECT_PROMISOR_ONLY
);
1969 promisor_objects_prepared
= 1;
1971 return oidset_contains(&promisor_objects
, oid
);