1 #define USE_THE_REPOSITORY_VARIABLE
3 #include "git-compat-util.h"
4 #include "environment.h"
9 #include "repository.h"
11 #include "mergesort.h"
14 #include "hash-lookup.h"
19 #include "tree-walk.h"
21 #include "object-file.h"
22 #include "object-store-ll.h"
24 #include "commit-graph.h"
25 #include "pack-revindex.h"
26 #include "promisor-remote.h"
28 char *odb_pack_name(struct strbuf
*buf
,
29 const unsigned char *hash
,
33 strbuf_addf(buf
, "%s/pack/pack-%s.%s", repo_get_object_directory(the_repository
),
34 hash_to_hex(hash
), ext
);
38 static unsigned int pack_used_ctr
;
39 static unsigned int pack_mmap_calls
;
40 static unsigned int peak_pack_open_windows
;
41 static unsigned int pack_open_windows
;
42 static unsigned int pack_open_fds
;
43 static unsigned int pack_max_fds
;
44 static size_t peak_pack_mapped
;
45 static size_t pack_mapped
;
47 #define SZ_FMT PRIuMAX
48 static inline uintmax_t sz_fmt(size_t s
) { return s
; }
50 void pack_report(void)
53 "pack_report: getpagesize() = %10" SZ_FMT
"\n"
54 "pack_report: core.packedGitWindowSize = %10" SZ_FMT
"\n"
55 "pack_report: core.packedGitLimit = %10" SZ_FMT
"\n",
56 sz_fmt(getpagesize()),
57 sz_fmt(packed_git_window_size
),
58 sz_fmt(packed_git_limit
));
60 "pack_report: pack_used_ctr = %10u\n"
61 "pack_report: pack_mmap_calls = %10u\n"
62 "pack_report: pack_open_windows = %10u / %10u\n"
63 "pack_report: pack_mapped = "
64 "%10" SZ_FMT
" / %10" SZ_FMT
"\n",
67 pack_open_windows
, peak_pack_open_windows
,
68 sz_fmt(pack_mapped
), sz_fmt(peak_pack_mapped
));
72 * Open and mmap the index file at path, perform a couple of
73 * consistency checks, then record its information to p. Return 0 on
76 static int check_packed_git_idx(const char *path
, struct packed_git
*p
)
80 int fd
= git_open(path
), ret
;
82 const unsigned int hashsz
= the_hash_algo
->rawsz
;
90 idx_size
= xsize_t(st
.st_size
);
91 if (idx_size
< 4 * 256 + hashsz
+ hashsz
) {
93 return error("index file %s is too small", path
);
95 idx_map
= xmmap(NULL
, idx_size
, PROT_READ
, MAP_PRIVATE
, fd
, 0);
98 ret
= load_idx(path
, hashsz
, idx_map
, idx_size
, p
);
101 munmap(idx_map
, idx_size
);
106 int load_idx(const char *path
, const unsigned int hashsz
, void *idx_map
,
107 size_t idx_size
, struct packed_git
*p
)
109 struct pack_idx_header
*hdr
= idx_map
;
110 uint32_t version
, nr
, i
, *index
;
112 if (idx_size
< 4 * 256 + hashsz
+ hashsz
)
113 return error("index file %s is too small", path
);
115 return error("empty data");
117 if (hdr
->idx_signature
== htonl(PACK_IDX_SIGNATURE
)) {
118 version
= ntohl(hdr
->idx_version
);
119 if (version
< 2 || version
> 2)
120 return error("index file %s is version %"PRIu32
121 " and is not supported by this binary"
122 " (try upgrading GIT to a newer version)",
130 index
+= 2; /* skip index header */
131 for (i
= 0; i
< 256; i
++) {
132 uint32_t n
= ntohl(index
[i
]);
134 return error("non-monotonic index %s", path
);
141 * - 256 index entries 4 bytes each
142 * - 24-byte entries * nr (object ID + 4-byte offset)
143 * - hash of the packfile
146 if (idx_size
!= st_add(4 * 256 + hashsz
+ hashsz
, st_mult(nr
, hashsz
+ 4)))
147 return error("wrong index v1 file size in %s", path
);
148 } else if (version
== 2) {
151 * - 8 bytes of header
152 * - 256 index entries 4 bytes each
153 * - object ID entry * nr
154 * - 4-byte crc entry * nr
155 * - 4-byte offset entry * nr
156 * - hash of the packfile
158 * And after the 4-byte offset table might be a
159 * variable sized table containing 8-byte entries
160 * for offsets larger than 2^31.
162 size_t min_size
= st_add(8 + 4*256 + hashsz
+ hashsz
, st_mult(nr
, hashsz
+ 4 + 4));
163 size_t max_size
= min_size
;
165 max_size
= st_add(max_size
, st_mult(nr
- 1, 8));
166 if (idx_size
< min_size
|| idx_size
> max_size
)
167 return error("wrong index v2 file size in %s", path
);
168 if (idx_size
!= min_size
&&
170 * make sure we can deal with large pack offsets.
171 * 31-bit signed offset won't be enough, neither
172 * 32-bit unsigned one will be.
174 (sizeof(off_t
) <= 4))
175 return error("pack too large for current definition of off_t in %s", path
);
176 p
->crc_offset
= st_add(8 + 4 * 256, st_mult(nr
, hashsz
));
179 p
->index_version
= version
;
180 p
->index_data
= idx_map
;
181 p
->index_size
= idx_size
;
186 int open_pack_index(struct packed_git
*p
)
195 if (!strip_suffix(p
->pack_name
, ".pack", &len
))
196 BUG("pack_name does not end in .pack");
197 idx_name
= xstrfmt("%.*s.idx", (int)len
, p
->pack_name
);
198 ret
= check_packed_git_idx(idx_name
, p
);
203 uint32_t get_pack_fanout(struct packed_git
*p
, uint32_t value
)
205 const uint32_t *level1_ofs
= p
->index_data
;
208 if (open_pack_index(p
))
210 level1_ofs
= p
->index_data
;
213 if (p
->index_version
> 1) {
217 return ntohl(level1_ofs
[value
]);
220 static struct packed_git
*alloc_packed_git(int extra
)
222 struct packed_git
*p
= xmalloc(st_add(sizeof(*p
), extra
));
223 memset(p
, 0, sizeof(*p
));
228 static char *pack_path_from_idx(const char *idx_path
)
231 if (!strip_suffix(idx_path
, ".idx", &len
))
232 BUG("idx path does not end in .idx: %s", idx_path
);
233 return xstrfmt("%.*s.pack", (int)len
, idx_path
);
236 struct packed_git
*parse_pack_index(unsigned char *sha1
, const char *idx_path
)
238 char *path
= pack_path_from_idx(idx_path
);
239 size_t alloc
= st_add(strlen(path
), 1);
240 struct packed_git
*p
= alloc_packed_git(alloc
);
242 memcpy(p
->pack_name
, path
, alloc
); /* includes NUL */
244 hashcpy(p
->hash
, sha1
, the_repository
->hash_algo
);
245 if (check_packed_git_idx(idx_path
, p
)) {
253 static void scan_windows(struct packed_git
*p
,
254 struct packed_git
**lru_p
,
255 struct pack_window
**lru_w
,
256 struct pack_window
**lru_l
)
258 struct pack_window
*w
, *w_l
;
260 for (w_l
= NULL
, w
= p
->windows
; w
; w
= w
->next
) {
262 if (!*lru_w
|| w
->last_used
< (*lru_w
)->last_used
) {
272 static int unuse_one_window(struct packed_git
*current
)
274 struct packed_git
*p
, *lru_p
= NULL
;
275 struct pack_window
*lru_w
= NULL
, *lru_l
= NULL
;
278 scan_windows(current
, &lru_p
, &lru_w
, &lru_l
);
279 for (p
= the_repository
->objects
->packed_git
; p
; p
= p
->next
)
280 scan_windows(p
, &lru_p
, &lru_w
, &lru_l
);
282 munmap(lru_w
->base
, lru_w
->len
);
283 pack_mapped
-= lru_w
->len
;
285 lru_l
->next
= lru_w
->next
;
287 lru_p
->windows
= lru_w
->next
;
295 void close_pack_windows(struct packed_git
*p
)
298 struct pack_window
*w
= p
->windows
;
301 die("pack '%s' still has open windows to it",
303 munmap(w
->base
, w
->len
);
304 pack_mapped
-= w
->len
;
306 p
->windows
= w
->next
;
311 int close_pack_fd(struct packed_git
*p
)
323 void close_pack_index(struct packed_git
*p
)
326 munmap((void *)p
->index_data
, p
->index_size
);
327 p
->index_data
= NULL
;
331 static void close_pack_revindex(struct packed_git
*p
)
333 if (!p
->revindex_map
)
336 munmap((void *)p
->revindex_map
, p
->revindex_size
);
337 p
->revindex_map
= NULL
;
338 p
->revindex_data
= NULL
;
341 static void close_pack_mtimes(struct packed_git
*p
)
346 munmap((void *)p
->mtimes_map
, p
->mtimes_size
);
347 p
->mtimes_map
= NULL
;
350 void close_pack(struct packed_git
*p
)
352 close_pack_windows(p
);
355 close_pack_revindex(p
);
356 close_pack_mtimes(p
);
357 oidset_clear(&p
->bad_objects
);
360 void close_object_store(struct raw_object_store
*o
)
362 struct packed_git
*p
;
364 for (p
= o
->packed_git
; p
; p
= p
->next
)
366 BUG("want to close pack marked 'do-not-close'");
370 if (o
->multi_pack_index
) {
371 close_midx(o
->multi_pack_index
);
372 o
->multi_pack_index
= NULL
;
375 close_commit_graph(o
);
378 void unlink_pack_path(const char *pack_name
, int force_delete
)
380 static const char *exts
[] = {".idx", ".pack", ".rev", ".keep", ".bitmap", ".promisor", ".mtimes"};
382 struct strbuf buf
= STRBUF_INIT
;
385 strbuf_addstr(&buf
, pack_name
);
386 strip_suffix_mem(buf
.buf
, &buf
.len
, ".pack");
390 strbuf_addstr(&buf
, ".keep");
391 if (!access(buf
.buf
, F_OK
)) {
392 strbuf_release(&buf
);
397 for (i
= 0; i
< ARRAY_SIZE(exts
); i
++) {
398 strbuf_setlen(&buf
, plen
);
399 strbuf_addstr(&buf
, exts
[i
]);
403 strbuf_release(&buf
);
407 * The LRU pack is the one with the oldest MRU window, preferring packs
408 * with no used windows, or the oldest mtime if it has no windows allocated.
410 static void find_lru_pack(struct packed_git
*p
, struct packed_git
**lru_p
, struct pack_window
**mru_w
, int *accept_windows_inuse
)
412 struct pack_window
*w
, *this_mru_w
;
413 int has_windows_inuse
= 0;
416 * Reject this pack if it has windows and the previously selected
417 * one does not. If this pack does not have windows, reject
418 * it if the pack file is newer than the previously selected one.
420 if (*lru_p
&& !*mru_w
&& (p
->windows
|| p
->mtime
> (*lru_p
)->mtime
))
423 for (w
= this_mru_w
= p
->windows
; w
; w
= w
->next
) {
425 * Reject this pack if any of its windows are in use,
426 * but the previously selected pack did not have any
427 * inuse windows. Otherwise, record that this pack
428 * has windows in use.
431 if (*accept_windows_inuse
)
432 has_windows_inuse
= 1;
437 if (w
->last_used
> this_mru_w
->last_used
)
441 * Reject this pack if it has windows that have been
442 * used more recently than the previously selected pack.
443 * If the previously selected pack had windows inuse and
444 * we have not encountered a window in this pack that is
445 * inuse, skip this check since we prefer a pack with no
446 * inuse windows to one that has inuse windows.
448 if (*mru_w
&& *accept_windows_inuse
== has_windows_inuse
&&
449 this_mru_w
->last_used
> (*mru_w
)->last_used
)
458 *accept_windows_inuse
= has_windows_inuse
;
461 static int close_one_pack(void)
463 struct packed_git
*p
, *lru_p
= NULL
;
464 struct pack_window
*mru_w
= NULL
;
465 int accept_windows_inuse
= 1;
467 for (p
= the_repository
->objects
->packed_git
; p
; p
= p
->next
) {
468 if (p
->pack_fd
== -1)
470 find_lru_pack(p
, &lru_p
, &mru_w
, &accept_windows_inuse
);
474 return close_pack_fd(lru_p
);
479 static unsigned int get_max_fd_limit(void)
485 if (!getrlimit(RLIMIT_NOFILE
, &lim
))
492 long open_max
= sysconf(_SC_OPEN_MAX
);
496 * Otherwise, we got -1 for one of the two
499 * (1) sysconf() did not understand _SC_OPEN_MAX
500 * and signaled an error with -1; or
501 * (2) sysconf() said there is no limit.
503 * We _could_ clear errno before calling sysconf() to
504 * tell these two cases apart and return a huge number
505 * in the latter case to let the caller cap it to a
506 * value that is not so selfish, but letting the
507 * fallback OPEN_MAX codepath take care of these cases
516 return 1; /* see the caller ;-) */
520 const char *pack_basename(struct packed_git
*p
)
522 const char *ret
= strrchr(p
->pack_name
, '/');
524 ret
= ret
+ 1; /* skip past slash */
526 ret
= p
->pack_name
; /* we only have a base */
531 * Do not call this directly as this leaks p->pack_fd on error return;
532 * call open_packed_git() instead.
534 static int open_packed_git_1(struct packed_git
*p
)
537 struct pack_header hdr
;
538 unsigned char hash
[GIT_MAX_RAWSZ
];
539 unsigned char *idx_hash
;
541 const unsigned hashsz
= the_hash_algo
->rawsz
;
543 if (open_pack_index(p
))
544 return error("packfile %s index unavailable", p
->pack_name
);
547 unsigned int max_fds
= get_max_fd_limit();
549 /* Save 3 for stdin/stdout/stderr, 22 for work */
551 pack_max_fds
= max_fds
- 25;
556 while (pack_max_fds
<= pack_open_fds
&& close_one_pack())
559 p
->pack_fd
= git_open(p
->pack_name
);
560 if (p
->pack_fd
< 0 || fstat(p
->pack_fd
, &st
))
564 /* If we created the struct before we had the pack we lack size. */
566 if (!S_ISREG(st
.st_mode
))
567 return error("packfile %s not a regular file", p
->pack_name
);
568 p
->pack_size
= st
.st_size
;
569 } else if (p
->pack_size
!= st
.st_size
)
570 return error("packfile %s size changed", p
->pack_name
);
572 /* Verify we recognize this pack file format. */
573 read_result
= read_in_full(p
->pack_fd
, &hdr
, sizeof(hdr
));
575 return error_errno("error reading from %s", p
->pack_name
);
576 if (read_result
!= sizeof(hdr
))
577 return error("file %s is far too short to be a packfile", p
->pack_name
);
578 if (hdr
.hdr_signature
!= htonl(PACK_SIGNATURE
))
579 return error("file %s is not a GIT packfile", p
->pack_name
);
580 if (!pack_version_ok(hdr
.hdr_version
))
581 return error("packfile %s is version %"PRIu32
" and not"
582 " supported (try upgrading GIT to a newer version)",
583 p
->pack_name
, ntohl(hdr
.hdr_version
));
585 /* Verify the pack matches its index. */
586 if (p
->num_objects
!= ntohl(hdr
.hdr_entries
))
587 return error("packfile %s claims to have %"PRIu32
" objects"
588 " while index indicates %"PRIu32
" objects",
589 p
->pack_name
, ntohl(hdr
.hdr_entries
),
591 read_result
= pread_in_full(p
->pack_fd
, hash
, hashsz
,
592 p
->pack_size
- hashsz
);
594 return error_errno("error reading from %s", p
->pack_name
);
595 if (read_result
!= hashsz
)
596 return error("packfile %s signature is unavailable", p
->pack_name
);
597 idx_hash
= ((unsigned char *)p
->index_data
) + p
->index_size
- hashsz
* 2;
598 if (!hasheq(hash
, idx_hash
, the_repository
->hash_algo
))
599 return error("packfile %s does not match index", p
->pack_name
);
603 static int open_packed_git(struct packed_git
*p
)
605 if (!open_packed_git_1(p
))
611 static int in_window(struct pack_window
*win
, off_t offset
)
613 /* We must promise at least one full hash after the
614 * offset is available from this window, otherwise the offset
615 * is not actually in this window and a different window (which
616 * has that one hash excess) must be used. This is to support
617 * the object header and delta base parsing routines below.
619 off_t win_off
= win
->offset
;
620 return win_off
<= offset
621 && (offset
+ the_hash_algo
->rawsz
) <= (win_off
+ win
->len
);
624 unsigned char *use_pack(struct packed_git
*p
,
625 struct pack_window
**w_cursor
,
629 struct pack_window
*win
= *w_cursor
;
631 /* Since packfiles end in a hash of their content and it's
632 * pointless to ask for an offset into the middle of that
633 * hash, and the in_window function above wouldn't match
634 * don't allow an offset too close to the end of the file.
636 if (!p
->pack_size
&& p
->pack_fd
== -1 && open_packed_git(p
))
637 die("packfile %s cannot be accessed", p
->pack_name
);
638 if (offset
> (p
->pack_size
- the_hash_algo
->rawsz
))
639 die("offset beyond end of packfile (truncated pack?)");
641 die(_("offset before end of packfile (broken .idx?)"));
643 if (!win
|| !in_window(win
, offset
)) {
646 for (win
= p
->windows
; win
; win
= win
->next
) {
647 if (in_window(win
, offset
))
651 size_t window_align
= packed_git_window_size
/ 2;
654 if (p
->pack_fd
== -1 && open_packed_git(p
))
655 die("packfile %s cannot be accessed", p
->pack_name
);
657 CALLOC_ARRAY(win
, 1);
658 win
->offset
= (offset
/ window_align
) * window_align
;
659 len
= p
->pack_size
- win
->offset
;
660 if (len
> packed_git_window_size
)
661 len
= packed_git_window_size
;
662 win
->len
= (size_t)len
;
663 pack_mapped
+= win
->len
;
664 while (packed_git_limit
< pack_mapped
665 && unuse_one_window(p
))
667 win
->base
= xmmap_gently(NULL
, win
->len
,
668 PROT_READ
, MAP_PRIVATE
,
669 p
->pack_fd
, win
->offset
);
670 if (win
->base
== MAP_FAILED
)
671 die_errno(_("packfile %s cannot be mapped%s"),
672 p
->pack_name
, mmap_os_err());
673 if (!win
->offset
&& win
->len
== p
->pack_size
678 if (pack_mapped
> peak_pack_mapped
)
679 peak_pack_mapped
= pack_mapped
;
680 if (pack_open_windows
> peak_pack_open_windows
)
681 peak_pack_open_windows
= pack_open_windows
;
682 win
->next
= p
->windows
;
686 if (win
!= *w_cursor
) {
687 win
->last_used
= pack_used_ctr
++;
691 offset
-= win
->offset
;
693 *left
= win
->len
- xsize_t(offset
);
694 return win
->base
+ offset
;
697 void unuse_pack(struct pack_window
**w_cursor
)
699 struct pack_window
*w
= *w_cursor
;
706 struct packed_git
*add_packed_git(const char *path
, size_t path_len
, int local
)
710 struct packed_git
*p
;
713 * Make sure a corresponding .pack file exists and that
714 * the index looks sane.
716 if (!strip_suffix_mem(path
, &path_len
, ".idx"))
720 * ".promisor" is long enough to hold any suffix we're adding (and
721 * the use xsnprintf double-checks that)
723 alloc
= st_add3(path_len
, strlen(".promisor"), 1);
724 p
= alloc_packed_git(alloc
);
725 memcpy(p
->pack_name
, path
, path_len
);
727 xsnprintf(p
->pack_name
+ path_len
, alloc
- path_len
, ".keep");
728 if (!access(p
->pack_name
, F_OK
))
731 xsnprintf(p
->pack_name
+ path_len
, alloc
- path_len
, ".promisor");
732 if (!access(p
->pack_name
, F_OK
))
733 p
->pack_promisor
= 1;
735 xsnprintf(p
->pack_name
+ path_len
, alloc
- path_len
, ".mtimes");
736 if (!access(p
->pack_name
, F_OK
))
739 xsnprintf(p
->pack_name
+ path_len
, alloc
- path_len
, ".pack");
740 if (stat(p
->pack_name
, &st
) || !S_ISREG(st
.st_mode
)) {
745 /* ok, it looks sane as far as we can check without
746 * actually mapping the pack file.
748 p
->pack_size
= st
.st_size
;
749 p
->pack_local
= local
;
750 p
->mtime
= st
.st_mtime
;
751 if (path_len
< the_hash_algo
->hexsz
||
752 get_hash_hex(path
+ path_len
- the_hash_algo
->hexsz
, p
->hash
))
753 hashclr(p
->hash
, the_repository
->hash_algo
);
757 void install_packed_git(struct repository
*r
, struct packed_git
*pack
)
759 if (pack
->pack_fd
!= -1)
762 pack
->next
= r
->objects
->packed_git
;
763 r
->objects
->packed_git
= pack
;
765 hashmap_entry_init(&pack
->packmap_ent
, strhash(pack
->pack_name
));
766 hashmap_add(&r
->objects
->pack_map
, &pack
->packmap_ent
);
769 void (*report_garbage
)(unsigned seen_bits
, const char *path
);
771 static void report_helper(const struct string_list
*list
,
772 int seen_bits
, int first
, int last
)
774 if (seen_bits
== (PACKDIR_FILE_PACK
|PACKDIR_FILE_IDX
))
777 for (; first
< last
; first
++)
778 report_garbage(seen_bits
, list
->items
[first
].string
);
781 static void report_pack_garbage(struct string_list
*list
)
783 int i
, baselen
= -1, first
= 0, seen_bits
= 0;
788 string_list_sort(list
);
790 for (i
= 0; i
< list
->nr
; i
++) {
791 const char *path
= list
->items
[i
].string
;
793 strncmp(path
, list
->items
[first
].string
, baselen
)) {
794 report_helper(list
, seen_bits
, first
, i
);
799 const char *dot
= strrchr(path
, '.');
801 report_garbage(PACKDIR_FILE_GARBAGE
, path
);
804 baselen
= dot
- path
+ 1;
807 if (!strcmp(path
+ baselen
, "pack"))
809 else if (!strcmp(path
+ baselen
, "idx"))
812 report_helper(list
, seen_bits
, first
, list
->nr
);
815 void for_each_file_in_pack_subdir(const char *objdir
,
817 each_file_in_pack_dir_fn fn
,
820 struct strbuf path
= STRBUF_INIT
;
825 strbuf_addstr(&path
, objdir
);
826 strbuf_addstr(&path
, "/pack");
828 strbuf_addf(&path
, "/%s", subdir
);
829 dir
= opendir(path
.buf
);
832 error_errno("unable to open object pack directory: %s",
834 strbuf_release(&path
);
837 strbuf_addch(&path
, '/');
838 dirnamelen
= path
.len
;
839 while ((de
= readdir_skip_dot_and_dotdot(dir
)) != NULL
) {
840 strbuf_setlen(&path
, dirnamelen
);
841 strbuf_addstr(&path
, de
->d_name
);
843 fn(path
.buf
, path
.len
, de
->d_name
, data
);
847 strbuf_release(&path
);
850 void for_each_file_in_pack_dir(const char *objdir
,
851 each_file_in_pack_dir_fn fn
,
854 for_each_file_in_pack_subdir(objdir
, NULL
, fn
, data
);
857 struct prepare_pack_data
{
858 struct repository
*r
;
859 struct string_list
*garbage
;
861 struct multi_pack_index
*m
;
864 static void prepare_pack(const char *full_name
, size_t full_name_len
,
865 const char *file_name
, void *_data
)
867 struct prepare_pack_data
*data
= (struct prepare_pack_data
*)_data
;
868 struct packed_git
*p
;
869 size_t base_len
= full_name_len
;
871 if (strip_suffix_mem(full_name
, &base_len
, ".idx") &&
872 !(data
->m
&& midx_contains_pack(data
->m
, file_name
))) {
873 struct hashmap_entry hent
;
874 char *pack_name
= xstrfmt("%.*s.pack", (int)base_len
, full_name
);
875 unsigned int hash
= strhash(pack_name
);
876 hashmap_entry_init(&hent
, hash
);
878 /* Don't reopen a pack we already have. */
879 if (!hashmap_get(&data
->r
->objects
->pack_map
, &hent
, pack_name
)) {
880 p
= add_packed_git(full_name
, full_name_len
, data
->local
);
882 install_packed_git(data
->r
, p
);
890 if (!strcmp(file_name
, "multi-pack-index") ||
891 !strcmp(file_name
, "multi-pack-index.d"))
893 if (starts_with(file_name
, "multi-pack-index") &&
894 (ends_with(file_name
, ".bitmap") || ends_with(file_name
, ".rev")))
896 if (ends_with(file_name
, ".idx") ||
897 ends_with(file_name
, ".rev") ||
898 ends_with(file_name
, ".pack") ||
899 ends_with(file_name
, ".bitmap") ||
900 ends_with(file_name
, ".keep") ||
901 ends_with(file_name
, ".promisor") ||
902 ends_with(file_name
, ".mtimes"))
903 string_list_append(data
->garbage
, full_name
);
905 report_garbage(PACKDIR_FILE_GARBAGE
, full_name
);
908 static void prepare_packed_git_one(struct repository
*r
, char *objdir
, int local
)
910 struct prepare_pack_data data
;
911 struct string_list garbage
= STRING_LIST_INIT_DUP
;
913 data
.m
= r
->objects
->multi_pack_index
;
915 /* look for the multi-pack-index for this object directory */
916 while (data
.m
&& strcmp(data
.m
->object_dir
, objdir
))
917 data
.m
= data
.m
->next
;
920 data
.garbage
= &garbage
;
923 for_each_file_in_pack_dir(objdir
, prepare_pack
, &data
);
925 report_pack_garbage(data
.garbage
);
926 string_list_clear(data
.garbage
, 0);
929 static void prepare_packed_git(struct repository
*r
);
931 * Give a fast, rough count of the number of objects in the repository. This
932 * ignores loose objects completely. If you have a lot of them, then either
933 * you should repack because your performance will be awful, or they are
934 * all unreachable objects about to be pruned, in which case they're not really
935 * interesting as a measure of repo size in the first place.
937 unsigned long repo_approximate_object_count(struct repository
*r
)
939 if (!r
->objects
->approximate_object_count_valid
) {
941 struct multi_pack_index
*m
;
942 struct packed_git
*p
;
944 prepare_packed_git(r
);
946 for (m
= get_multi_pack_index(r
); m
; m
= m
->next
)
947 count
+= m
->num_objects
;
948 for (p
= r
->objects
->packed_git
; p
; p
= p
->next
) {
949 if (open_pack_index(p
))
951 count
+= p
->num_objects
;
953 r
->objects
->approximate_object_count
= count
;
954 r
->objects
->approximate_object_count_valid
= 1;
956 return r
->objects
->approximate_object_count
;
959 DEFINE_LIST_SORT(static, sort_packs
, struct packed_git
, next
);
961 static int sort_pack(const struct packed_git
*a
, const struct packed_git
*b
)
966 * Local packs tend to contain objects specific to our
967 * variant of the project than remote ones. In addition,
968 * remote ones could be on a network mounted filesystem.
969 * Favor local ones for these reasons.
971 st
= a
->pack_local
- b
->pack_local
;
976 * Younger packs tend to contain more recent objects,
977 * and more recent objects tend to get accessed more
980 if (a
->mtime
< b
->mtime
)
982 else if (a
->mtime
== b
->mtime
)
987 static void rearrange_packed_git(struct repository
*r
)
989 sort_packs(&r
->objects
->packed_git
, sort_pack
);
992 static void prepare_packed_git_mru(struct repository
*r
)
994 struct packed_git
*p
;
996 INIT_LIST_HEAD(&r
->objects
->packed_git_mru
);
998 for (p
= r
->objects
->packed_git
; p
; p
= p
->next
)
999 list_add_tail(&p
->mru
, &r
->objects
->packed_git_mru
);
1002 static void prepare_packed_git(struct repository
*r
)
1004 struct object_directory
*odb
;
1006 if (r
->objects
->packed_git_initialized
)
1010 for (odb
= r
->objects
->odb
; odb
; odb
= odb
->next
) {
1011 int local
= (odb
== r
->objects
->odb
);
1012 prepare_multi_pack_index_one(r
, odb
->path
, local
);
1013 prepare_packed_git_one(r
, odb
->path
, local
);
1015 rearrange_packed_git(r
);
1017 prepare_packed_git_mru(r
);
1018 r
->objects
->packed_git_initialized
= 1;
1021 void reprepare_packed_git(struct repository
*r
)
1023 struct object_directory
*odb
;
1028 * Reprepare alt odbs, in case the alternates file was modified
1029 * during the course of this process. This only _adds_ odbs to
1030 * the linked list, so existing odbs will continue to exist for
1031 * the lifetime of the process.
1033 r
->objects
->loaded_alternates
= 0;
1036 for (odb
= r
->objects
->odb
; odb
; odb
= odb
->next
)
1037 odb_clear_loose_cache(odb
);
1039 r
->objects
->approximate_object_count_valid
= 0;
1040 r
->objects
->packed_git_initialized
= 0;
1041 prepare_packed_git(r
);
1045 struct packed_git
*get_packed_git(struct repository
*r
)
1047 prepare_packed_git(r
);
1048 return r
->objects
->packed_git
;
1051 struct multi_pack_index
*get_multi_pack_index(struct repository
*r
)
1053 prepare_packed_git(r
);
1054 return r
->objects
->multi_pack_index
;
1057 struct multi_pack_index
*get_local_multi_pack_index(struct repository
*r
)
1059 struct multi_pack_index
*m
= get_multi_pack_index(r
);
1061 /* no need to iterate; we always put the local one first (if any) */
1068 struct packed_git
*get_all_packs(struct repository
*r
)
1070 struct multi_pack_index
*m
;
1072 prepare_packed_git(r
);
1073 for (m
= r
->objects
->multi_pack_index
; m
; m
= m
->next
) {
1075 for (i
= 0; i
< m
->num_packs
+ m
->num_packs_in_base
; i
++)
1076 prepare_midx_pack(r
, m
, i
);
1079 return r
->objects
->packed_git
;
1082 struct list_head
*get_packed_git_mru(struct repository
*r
)
1084 prepare_packed_git(r
);
1085 return &r
->objects
->packed_git_mru
;
1088 unsigned long unpack_object_header_buffer(const unsigned char *buf
,
1089 unsigned long len
, enum object_type
*type
, unsigned long *sizep
)
1093 unsigned long used
= 0;
1096 *type
= (c
>> 4) & 7;
1100 if (len
<= used
|| (bitsizeof(long) - 7) < shift
) {
1101 error("bad object header");
1106 size
= st_add(size
, st_left_shift(c
& 0x7f, shift
));
1109 *sizep
= cast_size_t_to_ulong(size
);
1113 unsigned long get_size_from_delta(struct packed_git
*p
,
1114 struct pack_window
**w_curs
,
1117 const unsigned char *data
;
1118 unsigned char delta_head
[20], *in
;
1122 memset(&stream
, 0, sizeof(stream
));
1123 stream
.next_out
= delta_head
;
1124 stream
.avail_out
= sizeof(delta_head
);
1126 git_inflate_init(&stream
);
1128 in
= use_pack(p
, w_curs
, curpos
, &stream
.avail_in
);
1129 stream
.next_in
= in
;
1131 * Note: the window section returned by use_pack() must be
1132 * available throughout git_inflate()'s unlocked execution. To
1133 * ensure no other thread will modify the window in the
1134 * meantime, we rely on the packed_window.inuse_cnt. This
1135 * counter is incremented before window reading and checked
1136 * before window disposal.
1138 * Other worrying sections could be the call to close_pack_fd(),
1139 * which can close packs even with in-use windows, and to
1140 * reprepare_packed_git(). Regarding the former, mmap doc says:
1141 * "closing the file descriptor does not unmap the region". And
1142 * for the latter, it won't re-open already available packs.
1145 st
= git_inflate(&stream
, Z_FINISH
);
1147 curpos
+= stream
.next_in
- in
;
1148 } while ((st
== Z_OK
|| st
== Z_BUF_ERROR
) &&
1149 stream
.total_out
< sizeof(delta_head
));
1150 git_inflate_end(&stream
);
1151 if ((st
!= Z_STREAM_END
) && stream
.total_out
!= sizeof(delta_head
)) {
1152 error("delta data unpack-initial failed");
1156 /* Examine the initial part of the delta to figure out
1161 /* ignore base size */
1162 get_delta_hdr_size(&data
, delta_head
+sizeof(delta_head
));
1164 /* Read the result size */
1165 return get_delta_hdr_size(&data
, delta_head
+sizeof(delta_head
));
1168 int unpack_object_header(struct packed_git
*p
,
1169 struct pack_window
**w_curs
,
1171 unsigned long *sizep
)
1173 unsigned char *base
;
1176 enum object_type type
;
1178 /* use_pack() assures us we have [base, base + 20) available
1179 * as a range that we can look at. (Its actually the hash
1180 * size that is assured.) With our object header encoding
1181 * the maximum deflated object size is 2^137, which is just
1182 * insane, so we know won't exceed what we have been given.
1184 base
= use_pack(p
, w_curs
, *curpos
, &left
);
1185 used
= unpack_object_header_buffer(base
, left
, &type
, sizep
);
1194 void mark_bad_packed_object(struct packed_git
*p
, const struct object_id
*oid
)
1196 oidset_insert(&p
->bad_objects
, oid
);
1199 const struct packed_git
*has_packed_and_bad(struct repository
*r
,
1200 const struct object_id
*oid
)
1202 struct packed_git
*p
;
1204 for (p
= r
->objects
->packed_git
; p
; p
= p
->next
)
1205 if (oidset_contains(&p
->bad_objects
, oid
))
1210 off_t
get_delta_base(struct packed_git
*p
,
1211 struct pack_window
**w_curs
,
1213 enum object_type type
,
1214 off_t delta_obj_offset
)
1216 unsigned char *base_info
= use_pack(p
, w_curs
, *curpos
, NULL
);
1219 /* use_pack() assured us we have [base_info, base_info + 20)
1220 * as a range that we can look at without walking off the
1221 * end of the mapped window. Its actually the hash size
1222 * that is assured. An OFS_DELTA longer than the hash size
1223 * is stupid, as then a REF_DELTA would be smaller to store.
1225 if (type
== OBJ_OFS_DELTA
) {
1227 unsigned char c
= base_info
[used
++];
1228 base_offset
= c
& 127;
1231 if (!base_offset
|| MSB(base_offset
, 7))
1232 return 0; /* overflow */
1233 c
= base_info
[used
++];
1234 base_offset
= (base_offset
<< 7) + (c
& 127);
1236 base_offset
= delta_obj_offset
- base_offset
;
1237 if (base_offset
<= 0 || base_offset
>= delta_obj_offset
)
1238 return 0; /* out of bound */
1240 } else if (type
== OBJ_REF_DELTA
) {
1241 /* The base entry _must_ be in the same pack */
1242 struct object_id oid
;
1243 oidread(&oid
, base_info
, the_repository
->hash_algo
);
1244 base_offset
= find_pack_entry_one(&oid
, p
);
1245 *curpos
+= the_hash_algo
->rawsz
;
1247 die("I am totally screwed");
1252 * Like get_delta_base above, but we return the sha1 instead of the pack
1253 * offset. This means it is cheaper for REF deltas (we do not have to do
1254 * the final object lookup), but more expensive for OFS deltas (we
1255 * have to load the revidx to convert the offset back into a sha1).
1257 static int get_delta_base_oid(struct packed_git
*p
,
1258 struct pack_window
**w_curs
,
1260 struct object_id
*oid
,
1261 enum object_type type
,
1262 off_t delta_obj_offset
)
1264 if (type
== OBJ_REF_DELTA
) {
1265 unsigned char *base
= use_pack(p
, w_curs
, curpos
, NULL
);
1266 oidread(oid
, base
, the_repository
->hash_algo
);
1268 } else if (type
== OBJ_OFS_DELTA
) {
1270 off_t base_offset
= get_delta_base(p
, w_curs
, &curpos
,
1271 type
, delta_obj_offset
);
1276 if (offset_to_pack_pos(p
, base_offset
, &base_pos
) < 0)
1279 return nth_packed_object_id(oid
, p
,
1280 pack_pos_to_index(p
, base_pos
));
1285 static int retry_bad_packed_offset(struct repository
*r
,
1286 struct packed_git
*p
,
1291 struct object_id oid
;
1292 if (offset_to_pack_pos(p
, obj_offset
, &pos
) < 0)
1294 nth_packed_object_id(&oid
, p
, pack_pos_to_index(p
, pos
));
1295 mark_bad_packed_object(p
, &oid
);
1296 type
= oid_object_info(r
, &oid
, NULL
);
1297 if (type
<= OBJ_NONE
)
1302 #define POI_STACK_PREALLOC 64
1304 static enum object_type
packed_to_object_type(struct repository
*r
,
1305 struct packed_git
*p
,
1307 enum object_type type
,
1308 struct pack_window
**w_curs
,
1311 off_t small_poi_stack
[POI_STACK_PREALLOC
];
1312 off_t
*poi_stack
= small_poi_stack
;
1313 int poi_stack_nr
= 0, poi_stack_alloc
= POI_STACK_PREALLOC
;
1315 while (type
== OBJ_OFS_DELTA
|| type
== OBJ_REF_DELTA
) {
1318 /* Push the object we're going to leave behind */
1319 if (poi_stack_nr
>= poi_stack_alloc
&& poi_stack
== small_poi_stack
) {
1320 poi_stack_alloc
= alloc_nr(poi_stack_nr
);
1321 ALLOC_ARRAY(poi_stack
, poi_stack_alloc
);
1322 COPY_ARRAY(poi_stack
, small_poi_stack
, poi_stack_nr
);
1324 ALLOC_GROW(poi_stack
, poi_stack_nr
+1, poi_stack_alloc
);
1326 poi_stack
[poi_stack_nr
++] = obj_offset
;
1327 /* If parsing the base offset fails, just unwind */
1328 base_offset
= get_delta_base(p
, w_curs
, &curpos
, type
, obj_offset
);
1331 curpos
= obj_offset
= base_offset
;
1332 type
= unpack_object_header(p
, w_curs
, &curpos
, &size
);
1333 if (type
<= OBJ_NONE
) {
1334 /* If getting the base itself fails, we first
1335 * retry the base, otherwise unwind */
1336 type
= retry_bad_packed_offset(r
, p
, base_offset
);
1337 if (type
> OBJ_NONE
)
1351 error("unknown object type %i at offset %"PRIuMAX
" in %s",
1352 type
, (uintmax_t)obj_offset
, p
->pack_name
);
1357 if (poi_stack
!= small_poi_stack
)
1362 while (poi_stack_nr
) {
1363 obj_offset
= poi_stack
[--poi_stack_nr
];
1364 type
= retry_bad_packed_offset(r
, p
, obj_offset
);
1365 if (type
> OBJ_NONE
)
1372 static struct hashmap delta_base_cache
;
1373 static size_t delta_base_cached
;
1375 static LIST_HEAD(delta_base_cache_lru
);
1377 struct delta_base_cache_key
{
1378 struct packed_git
*p
;
1382 struct delta_base_cache_entry
{
1383 struct hashmap_entry ent
;
1384 struct delta_base_cache_key key
;
1385 struct list_head lru
;
1388 enum object_type type
;
1391 static unsigned int pack_entry_hash(struct packed_git
*p
, off_t base_offset
)
1395 hash
= (unsigned int)(intptr_t)p
+ (unsigned int)base_offset
;
1396 hash
+= (hash
>> 8) + (hash
>> 16);
1400 static struct delta_base_cache_entry
*
1401 get_delta_base_cache_entry(struct packed_git
*p
, off_t base_offset
)
1403 struct hashmap_entry entry
, *e
;
1404 struct delta_base_cache_key key
;
1406 if (!delta_base_cache
.cmpfn
)
1409 hashmap_entry_init(&entry
, pack_entry_hash(p
, base_offset
));
1411 key
.base_offset
= base_offset
;
1412 e
= hashmap_get(&delta_base_cache
, &entry
, &key
);
1413 return e
? container_of(e
, struct delta_base_cache_entry
, ent
) : NULL
;
1416 static int delta_base_cache_key_eq(const struct delta_base_cache_key
*a
,
1417 const struct delta_base_cache_key
*b
)
1419 return a
->p
== b
->p
&& a
->base_offset
== b
->base_offset
;
1422 static int delta_base_cache_hash_cmp(const void *cmp_data UNUSED
,
1423 const struct hashmap_entry
*va
,
1424 const struct hashmap_entry
*vb
,
1427 const struct delta_base_cache_entry
*a
, *b
;
1428 const struct delta_base_cache_key
*key
= vkey
;
1430 a
= container_of(va
, const struct delta_base_cache_entry
, ent
);
1431 b
= container_of(vb
, const struct delta_base_cache_entry
, ent
);
1434 return !delta_base_cache_key_eq(&a
->key
, key
);
1436 return !delta_base_cache_key_eq(&a
->key
, &b
->key
);
1439 static int in_delta_base_cache(struct packed_git
*p
, off_t base_offset
)
1441 return !!get_delta_base_cache_entry(p
, base_offset
);
1445 * Remove the entry from the cache, but do _not_ free the associated
1446 * entry data. The caller takes ownership of the "data" buffer, and
1447 * should copy out any fields it wants before detaching.
1449 static void detach_delta_base_cache_entry(struct delta_base_cache_entry
*ent
)
1451 hashmap_remove(&delta_base_cache
, &ent
->ent
, &ent
->key
);
1452 list_del(&ent
->lru
);
1453 delta_base_cached
-= ent
->size
;
1457 static void *cache_or_unpack_entry(struct repository
*r
, struct packed_git
*p
,
1458 off_t base_offset
, unsigned long *base_size
,
1459 enum object_type
*type
)
1461 struct delta_base_cache_entry
*ent
;
1463 ent
= get_delta_base_cache_entry(p
, base_offset
);
1465 return unpack_entry(r
, p
, base_offset
, type
, base_size
);
1470 *base_size
= ent
->size
;
1471 return xmemdupz(ent
->data
, ent
->size
);
1474 static inline void release_delta_base_cache(struct delta_base_cache_entry
*ent
)
1477 detach_delta_base_cache_entry(ent
);
1480 void clear_delta_base_cache(void)
1482 struct list_head
*lru
, *tmp
;
1483 list_for_each_safe(lru
, tmp
, &delta_base_cache_lru
) {
1484 struct delta_base_cache_entry
*entry
=
1485 list_entry(lru
, struct delta_base_cache_entry
, lru
);
1486 release_delta_base_cache(entry
);
1490 static void add_delta_base_cache(struct packed_git
*p
, off_t base_offset
,
1491 void *base
, unsigned long base_size
, enum object_type type
)
1493 struct delta_base_cache_entry
*ent
;
1494 struct list_head
*lru
, *tmp
;
1497 * Check required to avoid redundant entries when more than one thread
1498 * is unpacking the same object, in unpack_entry() (since its phases I
1499 * and III might run concurrently across multiple threads).
1501 if (in_delta_base_cache(p
, base_offset
)) {
1506 delta_base_cached
+= base_size
;
1508 list_for_each_safe(lru
, tmp
, &delta_base_cache_lru
) {
1509 struct delta_base_cache_entry
*f
=
1510 list_entry(lru
, struct delta_base_cache_entry
, lru
);
1511 if (delta_base_cached
<= delta_base_cache_limit
)
1513 release_delta_base_cache(f
);
1516 ent
= xmalloc(sizeof(*ent
));
1518 ent
->key
.base_offset
= base_offset
;
1521 ent
->size
= base_size
;
1522 list_add_tail(&ent
->lru
, &delta_base_cache_lru
);
1524 if (!delta_base_cache
.cmpfn
)
1525 hashmap_init(&delta_base_cache
, delta_base_cache_hash_cmp
, NULL
, 0);
1526 hashmap_entry_init(&ent
->ent
, pack_entry_hash(p
, base_offset
));
1527 hashmap_add(&delta_base_cache
, &ent
->ent
);
1530 int packed_object_info(struct repository
*r
, struct packed_git
*p
,
1531 off_t obj_offset
, struct object_info
*oi
)
1533 struct pack_window
*w_curs
= NULL
;
1535 off_t curpos
= obj_offset
;
1536 enum object_type type
;
1539 * We always get the representation type, but only convert it to
1540 * a "real" type later if the caller is interested.
1543 *oi
->contentp
= cache_or_unpack_entry(r
, p
, obj_offset
, oi
->sizep
,
1548 type
= unpack_object_header(p
, &w_curs
, &curpos
, &size
);
1551 if (!oi
->contentp
&& oi
->sizep
) {
1552 if (type
== OBJ_OFS_DELTA
|| type
== OBJ_REF_DELTA
) {
1553 off_t tmp_pos
= curpos
;
1554 off_t base_offset
= get_delta_base(p
, &w_curs
, &tmp_pos
,
1560 *oi
->sizep
= get_size_from_delta(p
, &w_curs
, tmp_pos
);
1561 if (*oi
->sizep
== 0) {
1570 if (oi
->disk_sizep
) {
1572 if (offset_to_pack_pos(p
, obj_offset
, &pos
) < 0) {
1573 error("could not find object at offset %"PRIuMAX
" "
1574 "in pack %s", (uintmax_t)obj_offset
, p
->pack_name
);
1579 *oi
->disk_sizep
= pack_pos_to_offset(p
, pos
+ 1) - obj_offset
;
1582 if (oi
->typep
|| oi
->type_name
) {
1583 enum object_type ptot
;
1584 ptot
= packed_to_object_type(r
, p
, obj_offset
,
1585 type
, &w_curs
, curpos
);
1588 if (oi
->type_name
) {
1589 const char *tn
= type_name(ptot
);
1591 strbuf_addstr(oi
->type_name
, tn
);
1599 if (oi
->delta_base_oid
) {
1600 if (type
== OBJ_OFS_DELTA
|| type
== OBJ_REF_DELTA
) {
1601 if (get_delta_base_oid(p
, &w_curs
, curpos
,
1603 type
, obj_offset
) < 0) {
1608 oidclr(oi
->delta_base_oid
, the_repository
->hash_algo
);
1611 oi
->whence
= in_delta_base_cache(p
, obj_offset
) ? OI_DBCACHED
:
1615 unuse_pack(&w_curs
);
1619 static void *unpack_compressed_entry(struct packed_git
*p
,
1620 struct pack_window
**w_curs
,
1626 unsigned char *buffer
, *in
;
1628 buffer
= xmallocz_gently(size
);
1631 memset(&stream
, 0, sizeof(stream
));
1632 stream
.next_out
= buffer
;
1633 stream
.avail_out
= size
+ 1;
1635 git_inflate_init(&stream
);
1637 in
= use_pack(p
, w_curs
, curpos
, &stream
.avail_in
);
1638 stream
.next_in
= in
;
1640 * Note: we must ensure the window section returned by
1641 * use_pack() will be available throughout git_inflate()'s
1642 * unlocked execution. Please refer to the comment at
1643 * get_size_from_delta() to see how this is done.
1646 st
= git_inflate(&stream
, Z_FINISH
);
1648 if (!stream
.avail_out
)
1649 break; /* the payload is larger than it should be */
1650 curpos
+= stream
.next_in
- in
;
1651 } while (st
== Z_OK
|| st
== Z_BUF_ERROR
);
1652 git_inflate_end(&stream
);
1653 if ((st
!= Z_STREAM_END
) || stream
.total_out
!= size
) {
1658 /* versions of zlib can clobber unconsumed portion of outbuf */
1659 buffer
[size
] = '\0';
1664 static void write_pack_access_log(struct packed_git
*p
, off_t obj_offset
)
1666 static struct trace_key pack_access
= TRACE_KEY_INIT(PACK_ACCESS
);
1667 trace_printf_key(&pack_access
, "%s %"PRIuMAX
"\n",
1668 p
->pack_name
, (uintmax_t)obj_offset
);
1671 int do_check_packed_object_crc
;
1673 #define UNPACK_ENTRY_STACK_PREALLOC 64
1674 struct unpack_entry_stack_ent
{
1680 void *unpack_entry(struct repository
*r
, struct packed_git
*p
, off_t obj_offset
,
1681 enum object_type
*final_type
, unsigned long *final_size
)
1683 struct pack_window
*w_curs
= NULL
;
1684 off_t curpos
= obj_offset
;
1687 enum object_type type
;
1688 struct unpack_entry_stack_ent small_delta_stack
[UNPACK_ENTRY_STACK_PREALLOC
];
1689 struct unpack_entry_stack_ent
*delta_stack
= small_delta_stack
;
1690 int delta_stack_nr
= 0, delta_stack_alloc
= UNPACK_ENTRY_STACK_PREALLOC
;
1691 int base_from_cache
= 0;
1693 write_pack_access_log(p
, obj_offset
);
1695 /* PHASE 1: drill down to the innermost base object */
1699 struct delta_base_cache_entry
*ent
;
1701 ent
= get_delta_base_cache_entry(p
, curpos
);
1706 detach_delta_base_cache_entry(ent
);
1707 base_from_cache
= 1;
1711 if (do_check_packed_object_crc
&& p
->index_version
> 1) {
1712 uint32_t pack_pos
, index_pos
;
1715 if (offset_to_pack_pos(p
, obj_offset
, &pack_pos
) < 0) {
1716 error("could not find object at offset %"PRIuMAX
" in pack %s",
1717 (uintmax_t)obj_offset
, p
->pack_name
);
1722 len
= pack_pos_to_offset(p
, pack_pos
+ 1) - obj_offset
;
1723 index_pos
= pack_pos_to_index(p
, pack_pos
);
1724 if (check_pack_crc(p
, &w_curs
, obj_offset
, len
, index_pos
)) {
1725 struct object_id oid
;
1726 nth_packed_object_id(&oid
, p
, index_pos
);
1727 error("bad packed object CRC for %s",
1729 mark_bad_packed_object(p
, &oid
);
1735 type
= unpack_object_header(p
, &w_curs
, &curpos
, &size
);
1736 if (type
!= OBJ_OFS_DELTA
&& type
!= OBJ_REF_DELTA
)
1739 base_offset
= get_delta_base(p
, &w_curs
, &curpos
, type
, obj_offset
);
1741 error("failed to validate delta base reference "
1742 "at offset %"PRIuMAX
" from %s",
1743 (uintmax_t)curpos
, p
->pack_name
);
1744 /* bail to phase 2, in hopes of recovery */
1749 /* push object, proceed to base */
1750 if (delta_stack_nr
>= delta_stack_alloc
1751 && delta_stack
== small_delta_stack
) {
1752 delta_stack_alloc
= alloc_nr(delta_stack_nr
);
1753 ALLOC_ARRAY(delta_stack
, delta_stack_alloc
);
1754 COPY_ARRAY(delta_stack
, small_delta_stack
,
1757 ALLOC_GROW(delta_stack
, delta_stack_nr
+1, delta_stack_alloc
);
1759 i
= delta_stack_nr
++;
1760 delta_stack
[i
].obj_offset
= obj_offset
;
1761 delta_stack
[i
].curpos
= curpos
;
1762 delta_stack
[i
].size
= size
;
1764 curpos
= obj_offset
= base_offset
;
1767 /* PHASE 2: handle the base */
1772 BUG("unpack_entry: left loop at a valid delta");
1778 if (!base_from_cache
)
1779 data
= unpack_compressed_entry(p
, &w_curs
, curpos
, size
);
1783 error("unknown object type %i at offset %"PRIuMAX
" in %s",
1784 type
, (uintmax_t)obj_offset
, p
->pack_name
);
1787 /* PHASE 3: apply deltas in order */
1790 * 'data' holds the base data, or NULL if there was corruption
1792 while (delta_stack_nr
) {
1795 void *external_base
= NULL
;
1796 unsigned long delta_size
, base_size
= size
;
1798 off_t base_obj_offset
= obj_offset
;
1804 * We're probably in deep shit, but let's try to fetch
1805 * the required base anyway from another pack or loose.
1806 * This is costly but should happen only in the presence
1807 * of a corrupted pack, and is better than failing outright.
1810 struct object_id base_oid
;
1811 if (!(offset_to_pack_pos(p
, obj_offset
, &pos
))) {
1812 struct object_info oi
= OBJECT_INFO_INIT
;
1814 nth_packed_object_id(&base_oid
, p
,
1815 pack_pos_to_index(p
, pos
));
1816 error("failed to read delta base object %s"
1817 " at offset %"PRIuMAX
" from %s",
1818 oid_to_hex(&base_oid
), (uintmax_t)obj_offset
,
1820 mark_bad_packed_object(p
, &base_oid
);
1823 oi
.sizep
= &base_size
;
1824 oi
.contentp
= &base
;
1825 if (oid_object_info_extended(r
, &base_oid
, &oi
, 0) < 0)
1828 external_base
= base
;
1832 i
= --delta_stack_nr
;
1833 obj_offset
= delta_stack
[i
].obj_offset
;
1834 curpos
= delta_stack
[i
].curpos
;
1835 delta_size
= delta_stack
[i
].size
;
1840 delta_data
= unpack_compressed_entry(p
, &w_curs
, curpos
, delta_size
);
1843 error("failed to unpack compressed delta "
1844 "at offset %"PRIuMAX
" from %s",
1845 (uintmax_t)curpos
, p
->pack_name
);
1848 data
= patch_delta(base
, base_size
, delta_data
,
1852 * We could not apply the delta; warn the user, but
1853 * keep going. Our failure will be noticed either in
1854 * the next iteration of the loop, or if this is the
1855 * final delta, in the caller when we return NULL.
1856 * Those code paths will take care of making a more
1857 * explicit warning and retrying with another copy of
1861 error("failed to apply delta");
1865 * We delay adding `base` to the cache until the end of the loop
1866 * because unpack_compressed_entry() momentarily releases the
1867 * obj_read_mutex, giving another thread the chance to access
1868 * the cache. Therefore, if `base` was already there, this other
1869 * thread could free() it (e.g. to make space for another entry)
1870 * before we are done using it.
1873 add_delta_base_cache(p
, base_obj_offset
, base
, base_size
, type
);
1876 free(external_base
);
1885 unuse_pack(&w_curs
);
1887 if (delta_stack
!= small_delta_stack
)
1893 int bsearch_pack(const struct object_id
*oid
, const struct packed_git
*p
, uint32_t *result
)
1895 const unsigned char *index_fanout
= p
->index_data
;
1896 const unsigned char *index_lookup
;
1897 const unsigned int hashsz
= the_hash_algo
->rawsz
;
1898 int index_lookup_width
;
1901 BUG("bsearch_pack called without a valid pack-index");
1903 index_lookup
= index_fanout
+ 4 * 256;
1904 if (p
->index_version
== 1) {
1905 index_lookup_width
= hashsz
+ 4;
1908 index_lookup_width
= hashsz
;
1913 return bsearch_hash(oid
->hash
, (const uint32_t*)index_fanout
,
1914 index_lookup
, index_lookup_width
, result
);
1917 int nth_packed_object_id(struct object_id
*oid
,
1918 struct packed_git
*p
,
1921 const unsigned char *index
= p
->index_data
;
1922 const unsigned int hashsz
= the_hash_algo
->rawsz
;
1924 if (open_pack_index(p
))
1926 index
= p
->index_data
;
1928 if (n
>= p
->num_objects
)
1931 if (p
->index_version
== 1) {
1932 oidread(oid
, index
+ st_add(st_mult(hashsz
+ 4, n
), 4),
1933 the_repository
->hash_algo
);
1936 oidread(oid
, index
+ st_mult(hashsz
, n
),
1937 the_repository
->hash_algo
);
1942 void check_pack_index_ptr(const struct packed_git
*p
, const void *vptr
)
1944 const unsigned char *ptr
= vptr
;
1945 const unsigned char *start
= p
->index_data
;
1946 const unsigned char *end
= start
+ p
->index_size
;
1948 die(_("offset before start of pack index for %s (corrupt index?)"),
1950 /* No need to check for underflow; .idx files must be at least 8 bytes */
1952 die(_("offset beyond end of pack index for %s (truncated index?)"),
1956 off_t
nth_packed_object_offset(const struct packed_git
*p
, uint32_t n
)
1958 const unsigned char *index
= p
->index_data
;
1959 const unsigned int hashsz
= the_hash_algo
->rawsz
;
1961 if (p
->index_version
== 1) {
1962 return ntohl(*((uint32_t *)(index
+ st_mult(hashsz
+ 4, n
))));
1965 index
+= st_add(8, st_mult(p
->num_objects
, hashsz
+ 4));
1966 off
= ntohl(*((uint32_t *)(index
+ st_mult(4, n
))));
1967 if (!(off
& 0x80000000))
1969 index
+= st_add(st_mult(p
->num_objects
, 4),
1970 st_mult(off
& 0x7fffffff, 8));
1971 check_pack_index_ptr(p
, index
);
1972 return get_be64(index
);
1976 off_t
find_pack_entry_one(const struct object_id
*oid
,
1977 struct packed_git
*p
)
1979 const unsigned char *index
= p
->index_data
;
1983 if (open_pack_index(p
))
1987 if (bsearch_pack(oid
, p
, &result
))
1988 return nth_packed_object_offset(p
, result
);
1992 int is_pack_valid(struct packed_git
*p
)
1994 /* An already open pack is known to be valid. */
1995 if (p
->pack_fd
!= -1)
1998 /* If the pack has one window completely covering the
1999 * file size, the pack is known to be valid even if
2000 * the descriptor is not currently open.
2003 struct pack_window
*w
= p
->windows
;
2005 if (!w
->offset
&& w
->len
== p
->pack_size
)
2009 /* Force the pack to open to prove its valid. */
2010 return !open_packed_git(p
);
2013 struct packed_git
*find_oid_pack(const struct object_id
*oid
,
2014 struct packed_git
*packs
)
2016 struct packed_git
*p
;
2018 for (p
= packs
; p
; p
= p
->next
) {
2019 if (find_pack_entry_one(oid
, p
))
2026 static int fill_pack_entry(const struct object_id
*oid
,
2027 struct pack_entry
*e
,
2028 struct packed_git
*p
)
2032 if (oidset_size(&p
->bad_objects
) &&
2033 oidset_contains(&p
->bad_objects
, oid
))
2036 offset
= find_pack_entry_one(oid
, p
);
2041 * We are about to tell the caller where they can locate the
2042 * requested object. We better make sure the packfile is
2043 * still here and can be accessed before supplying that
2044 * answer, as it may have been deleted since the index was
2047 if (!is_pack_valid(p
))
2054 int find_pack_entry(struct repository
*r
, const struct object_id
*oid
, struct pack_entry
*e
)
2056 struct list_head
*pos
;
2057 struct multi_pack_index
*m
;
2059 prepare_packed_git(r
);
2060 if (!r
->objects
->packed_git
&& !r
->objects
->multi_pack_index
)
2063 for (m
= r
->objects
->multi_pack_index
; m
; m
= m
->next
) {
2064 if (fill_midx_entry(r
, oid
, e
, m
))
2068 list_for_each(pos
, &r
->objects
->packed_git_mru
) {
2069 struct packed_git
*p
= list_entry(pos
, struct packed_git
, mru
);
2070 if (!p
->multi_pack_index
&& fill_pack_entry(oid
, e
, p
)) {
2071 list_move(&p
->mru
, &r
->objects
->packed_git_mru
);
2078 static void maybe_invalidate_kept_pack_cache(struct repository
*r
,
2081 if (!r
->objects
->kept_pack_cache
.packs
)
2083 if (r
->objects
->kept_pack_cache
.flags
== flags
)
2085 FREE_AND_NULL(r
->objects
->kept_pack_cache
.packs
);
2086 r
->objects
->kept_pack_cache
.flags
= 0;
2089 static struct packed_git
**kept_pack_cache(struct repository
*r
, unsigned flags
)
2091 maybe_invalidate_kept_pack_cache(r
, flags
);
2093 if (!r
->objects
->kept_pack_cache
.packs
) {
2094 struct packed_git
**packs
= NULL
;
2095 size_t nr
= 0, alloc
= 0;
2096 struct packed_git
*p
;
2099 * We want "all" packs here, because we need to cover ones that
2100 * are used by a midx, as well. We need to look in every one of
2101 * them (instead of the midx itself) to cover duplicates. It's
2102 * possible that an object is found in two packs that the midx
2103 * covers, one kept and one not kept, but the midx returns only
2104 * the non-kept version.
2106 for (p
= get_all_packs(r
); p
; p
= p
->next
) {
2107 if ((p
->pack_keep
&& (flags
& ON_DISK_KEEP_PACKS
)) ||
2108 (p
->pack_keep_in_core
&& (flags
& IN_CORE_KEEP_PACKS
))) {
2109 ALLOC_GROW(packs
, nr
+ 1, alloc
);
2113 ALLOC_GROW(packs
, nr
+ 1, alloc
);
2116 r
->objects
->kept_pack_cache
.packs
= packs
;
2117 r
->objects
->kept_pack_cache
.flags
= flags
;
2120 return r
->objects
->kept_pack_cache
.packs
;
2123 int find_kept_pack_entry(struct repository
*r
,
2124 const struct object_id
*oid
,
2126 struct pack_entry
*e
)
2128 struct packed_git
**cache
;
2130 for (cache
= kept_pack_cache(r
, flags
); *cache
; cache
++) {
2131 struct packed_git
*p
= *cache
;
2132 if (fill_pack_entry(oid
, e
, p
))
2139 int has_object_pack(const struct object_id
*oid
)
2141 struct pack_entry e
;
2142 return find_pack_entry(the_repository
, oid
, &e
);
2145 int has_object_kept_pack(const struct object_id
*oid
, unsigned flags
)
2147 struct pack_entry e
;
2148 return find_kept_pack_entry(the_repository
, oid
, flags
, &e
);
2151 int for_each_object_in_pack(struct packed_git
*p
,
2152 each_packed_object_fn cb
, void *data
,
2153 enum for_each_object_flags flags
)
2158 if (flags
& FOR_EACH_OBJECT_PACK_ORDER
) {
2159 if (load_pack_revindex(the_repository
, p
))
2163 for (i
= 0; i
< p
->num_objects
; i
++) {
2165 struct object_id oid
;
2168 * We are iterating "i" from 0 up to num_objects, but its
2169 * meaning may be different, depending on the requested output
2172 * - in object-name order, it is the same as the index order
2173 * used by nth_packed_object_id(), so we can pass it
2176 * - in pack-order, it is pack position, which we must
2177 * convert to an index position in order to get the oid.
2179 if (flags
& FOR_EACH_OBJECT_PACK_ORDER
)
2180 index_pos
= pack_pos_to_index(p
, i
);
2184 if (nth_packed_object_id(&oid
, p
, index_pos
) < 0)
2185 return error("unable to get sha1 of object %u in %s",
2186 index_pos
, p
->pack_name
);
2188 r
= cb(&oid
, p
, index_pos
, data
);
2195 int for_each_packed_object(each_packed_object_fn cb
, void *data
,
2196 enum for_each_object_flags flags
)
2198 struct packed_git
*p
;
2200 int pack_errors
= 0;
2202 prepare_packed_git(the_repository
);
2203 for (p
= get_all_packs(the_repository
); p
; p
= p
->next
) {
2204 if ((flags
& FOR_EACH_OBJECT_LOCAL_ONLY
) && !p
->pack_local
)
2206 if ((flags
& FOR_EACH_OBJECT_PROMISOR_ONLY
) &&
2209 if ((flags
& FOR_EACH_OBJECT_SKIP_IN_CORE_KEPT_PACKS
) &&
2210 p
->pack_keep_in_core
)
2212 if ((flags
& FOR_EACH_OBJECT_SKIP_ON_DISK_KEPT_PACKS
) &&
2215 if (open_pack_index(p
)) {
2219 r
= for_each_object_in_pack(p
, cb
, data
, flags
);
2223 return r
? r
: pack_errors
;
2226 static int add_promisor_object(const struct object_id
*oid
,
2227 struct packed_git
*pack UNUSED
,
2228 uint32_t pos UNUSED
,
2231 struct oidset
*set
= set_
;
2233 int we_parsed_object
;
2235 obj
= lookup_object(the_repository
, oid
);
2236 if (obj
&& obj
->parsed
) {
2237 we_parsed_object
= 0;
2239 we_parsed_object
= 1;
2240 obj
= parse_object(the_repository
, oid
);
2246 oidset_insert(set
, oid
);
2249 * If this is a tree, commit, or tag, the objects it refers
2250 * to are also promisor objects. (Blobs refer to no objects->)
2252 if (obj
->type
== OBJ_TREE
) {
2253 struct tree
*tree
= (struct tree
*)obj
;
2254 struct tree_desc desc
;
2255 struct name_entry entry
;
2256 if (init_tree_desc_gently(&desc
, &tree
->object
.oid
,
2257 tree
->buffer
, tree
->size
, 0))
2259 * Error messages are given when packs are
2260 * verified, so do not print any here.
2263 while (tree_entry_gently(&desc
, &entry
))
2264 oidset_insert(set
, &entry
.oid
);
2265 if (we_parsed_object
)
2266 free_tree_buffer(tree
);
2267 } else if (obj
->type
== OBJ_COMMIT
) {
2268 struct commit
*commit
= (struct commit
*) obj
;
2269 struct commit_list
*parents
= commit
->parents
;
2271 oidset_insert(set
, get_commit_tree_oid(commit
));
2272 for (; parents
; parents
= parents
->next
)
2273 oidset_insert(set
, &parents
->item
->object
.oid
);
2274 } else if (obj
->type
== OBJ_TAG
) {
2275 struct tag
*tag
= (struct tag
*) obj
;
2276 oidset_insert(set
, get_tagged_oid(tag
));
2281 int is_promisor_object(const struct object_id
*oid
)
2283 static struct oidset promisor_objects
;
2284 static int promisor_objects_prepared
;
2286 if (!promisor_objects_prepared
) {
2287 if (repo_has_promisor_remote(the_repository
)) {
2288 for_each_packed_object(add_promisor_object
,
2290 FOR_EACH_OBJECT_PROMISOR_ONLY
|
2291 FOR_EACH_OBJECT_PACK_ORDER
);
2293 promisor_objects_prepared
= 1;
2295 return oidset_contains(&promisor_objects
, oid
);