1 #define DISABLE_SIGN_COMPARE_WARNINGS
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.h"
24 #include "commit-graph.h"
25 #include "pack-revindex.h"
26 #include "promisor-remote.h"
27 #include "pack-mtimes.h"
29 char *odb_pack_name(struct repository
*r
, struct strbuf
*buf
,
30 const unsigned char *hash
, const char *ext
)
33 strbuf_addf(buf
, "%s/pack/pack-%s.%s", repo_get_object_directory(r
),
34 hash_to_hex_algop(hash
, r
->hash_algo
), 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(struct repository
*repo
)
53 "pack_report: getpagesize() = %10" SZ_FMT
"\n"
54 "pack_report: core.packedGitWindowSize = %10" SZ_FMT
"\n"
55 "pack_report: core.packedGitLimit = %10" SZ_FMT
"\n",
56 sz_fmt(getpagesize()),
57 sz_fmt(repo
->settings
.packed_git_window_size
),
58 sz_fmt(repo
->settings
.packed_git_limit
));
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
= p
->repo
->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(struct repository
*r
, int extra
)
222 struct packed_git
*p
= xmalloc(st_add(sizeof(*p
), extra
));
223 memset(p
, 0, sizeof(*p
));
229 static char *pack_path_from_idx(const char *idx_path
)
232 if (!strip_suffix(idx_path
, ".idx", &len
))
233 BUG("idx path does not end in .idx: %s", idx_path
);
234 return xstrfmt("%.*s.pack", (int)len
, idx_path
);
237 struct packed_git
*parse_pack_index(struct repository
*r
, unsigned char *sha1
,
238 const char *idx_path
)
240 char *path
= pack_path_from_idx(idx_path
);
241 size_t alloc
= st_add(strlen(path
), 1);
242 struct packed_git
*p
= alloc_packed_git(r
, alloc
);
244 memcpy(p
->pack_name
, path
, alloc
); /* includes NUL */
246 hashcpy(p
->hash
, sha1
, p
->repo
->hash_algo
);
247 if (check_packed_git_idx(idx_path
, p
)) {
255 static void scan_windows(struct packed_git
*p
,
256 struct packed_git
**lru_p
,
257 struct pack_window
**lru_w
,
258 struct pack_window
**lru_l
)
260 struct pack_window
*w
, *w_l
;
262 for (w_l
= NULL
, w
= p
->windows
; w
; w
= w
->next
) {
264 if (!*lru_w
|| w
->last_used
< (*lru_w
)->last_used
) {
274 static int unuse_one_window(struct packed_git
*current
)
276 struct packed_git
*p
, *lru_p
= NULL
;
277 struct pack_window
*lru_w
= NULL
, *lru_l
= NULL
;
280 scan_windows(current
, &lru_p
, &lru_w
, &lru_l
);
281 for (p
= current
->repo
->objects
->packed_git
; p
; p
= p
->next
)
282 scan_windows(p
, &lru_p
, &lru_w
, &lru_l
);
284 munmap(lru_w
->base
, lru_w
->len
);
285 pack_mapped
-= lru_w
->len
;
287 lru_l
->next
= lru_w
->next
;
289 lru_p
->windows
= lru_w
->next
;
297 void close_pack_windows(struct packed_git
*p
)
300 struct pack_window
*w
= p
->windows
;
303 die("pack '%s' still has open windows to it",
305 munmap(w
->base
, w
->len
);
306 pack_mapped
-= w
->len
;
308 p
->windows
= w
->next
;
313 int close_pack_fd(struct packed_git
*p
)
325 void close_pack_index(struct packed_git
*p
)
328 munmap((void *)p
->index_data
, p
->index_size
);
329 p
->index_data
= NULL
;
333 static void close_pack_revindex(struct packed_git
*p
)
335 if (!p
->revindex_map
)
338 munmap((void *)p
->revindex_map
, p
->revindex_size
);
339 p
->revindex_map
= NULL
;
340 p
->revindex_data
= NULL
;
343 static void close_pack_mtimes(struct packed_git
*p
)
348 munmap((void *)p
->mtimes_map
, p
->mtimes_size
);
349 p
->mtimes_map
= NULL
;
352 void close_pack(struct packed_git
*p
)
354 close_pack_windows(p
);
357 close_pack_revindex(p
);
358 close_pack_mtimes(p
);
359 oidset_clear(&p
->bad_objects
);
362 void close_object_store(struct raw_object_store
*o
)
364 struct packed_git
*p
;
366 for (p
= o
->packed_git
; p
; p
= p
->next
)
368 BUG("want to close pack marked 'do-not-close'");
372 if (o
->multi_pack_index
) {
373 close_midx(o
->multi_pack_index
);
374 o
->multi_pack_index
= NULL
;
377 close_commit_graph(o
);
380 void unlink_pack_path(const char *pack_name
, int force_delete
)
382 static const char *exts
[] = {".idx", ".pack", ".rev", ".keep", ".bitmap", ".promisor", ".mtimes"};
384 struct strbuf buf
= STRBUF_INIT
;
387 strbuf_addstr(&buf
, pack_name
);
388 strip_suffix_mem(buf
.buf
, &buf
.len
, ".pack");
392 strbuf_addstr(&buf
, ".keep");
393 if (!access(buf
.buf
, F_OK
)) {
394 strbuf_release(&buf
);
399 for (i
= 0; i
< ARRAY_SIZE(exts
); i
++) {
400 strbuf_setlen(&buf
, plen
);
401 strbuf_addstr(&buf
, exts
[i
]);
405 strbuf_release(&buf
);
409 * The LRU pack is the one with the oldest MRU window, preferring packs
410 * with no used windows, or the oldest mtime if it has no windows allocated.
412 static void find_lru_pack(struct packed_git
*p
, struct packed_git
**lru_p
, struct pack_window
**mru_w
, int *accept_windows_inuse
)
414 struct pack_window
*w
, *this_mru_w
;
415 int has_windows_inuse
= 0;
418 * Reject this pack if it has windows and the previously selected
419 * one does not. If this pack does not have windows, reject
420 * it if the pack file is newer than the previously selected one.
422 if (*lru_p
&& !*mru_w
&& (p
->windows
|| p
->mtime
> (*lru_p
)->mtime
))
425 for (w
= this_mru_w
= p
->windows
; w
; w
= w
->next
) {
427 * Reject this pack if any of its windows are in use,
428 * but the previously selected pack did not have any
429 * inuse windows. Otherwise, record that this pack
430 * has windows in use.
433 if (*accept_windows_inuse
)
434 has_windows_inuse
= 1;
439 if (w
->last_used
> this_mru_w
->last_used
)
443 * Reject this pack if it has windows that have been
444 * used more recently than the previously selected pack.
445 * If the previously selected pack had windows inuse and
446 * we have not encountered a window in this pack that is
447 * inuse, skip this check since we prefer a pack with no
448 * inuse windows to one that has inuse windows.
450 if (*mru_w
&& *accept_windows_inuse
== has_windows_inuse
&&
451 this_mru_w
->last_used
> (*mru_w
)->last_used
)
460 *accept_windows_inuse
= has_windows_inuse
;
463 static int close_one_pack(struct repository
*r
)
465 struct packed_git
*p
, *lru_p
= NULL
;
466 struct pack_window
*mru_w
= NULL
;
467 int accept_windows_inuse
= 1;
469 for (p
= r
->objects
->packed_git
; p
; p
= p
->next
) {
470 if (p
->pack_fd
== -1)
472 find_lru_pack(p
, &lru_p
, &mru_w
, &accept_windows_inuse
);
476 return close_pack_fd(lru_p
);
481 static unsigned int get_max_fd_limit(void)
487 if (!getrlimit(RLIMIT_NOFILE
, &lim
))
494 long open_max
= sysconf(_SC_OPEN_MAX
);
498 * Otherwise, we got -1 for one of the two
501 * (1) sysconf() did not understand _SC_OPEN_MAX
502 * and signaled an error with -1; or
503 * (2) sysconf() said there is no limit.
505 * We _could_ clear errno before calling sysconf() to
506 * tell these two cases apart and return a huge number
507 * in the latter case to let the caller cap it to a
508 * value that is not so selfish, but letting the
509 * fallback OPEN_MAX codepath take care of these cases
518 return 1; /* see the caller ;-) */
522 const char *pack_basename(struct packed_git
*p
)
524 const char *ret
= strrchr(p
->pack_name
, '/');
526 ret
= ret
+ 1; /* skip past slash */
528 ret
= p
->pack_name
; /* we only have a base */
533 * Do not call this directly as this leaks p->pack_fd on error return;
534 * call open_packed_git() instead.
536 static int open_packed_git_1(struct packed_git
*p
)
539 struct pack_header hdr
;
540 unsigned char hash
[GIT_MAX_RAWSZ
];
541 unsigned char *idx_hash
;
543 const unsigned hashsz
= p
->repo
->hash_algo
->rawsz
;
545 if (open_pack_index(p
))
546 return error("packfile %s index unavailable", p
->pack_name
);
549 unsigned int max_fds
= get_max_fd_limit();
551 /* Save 3 for stdin/stdout/stderr, 22 for work */
553 pack_max_fds
= max_fds
- 25;
558 while (pack_max_fds
<= pack_open_fds
&& close_one_pack(p
->repo
))
561 p
->pack_fd
= git_open(p
->pack_name
);
562 if (p
->pack_fd
< 0 || fstat(p
->pack_fd
, &st
))
566 /* If we created the struct before we had the pack we lack size. */
568 if (!S_ISREG(st
.st_mode
))
569 return error("packfile %s not a regular file", p
->pack_name
);
570 p
->pack_size
= st
.st_size
;
571 } else if (p
->pack_size
!= st
.st_size
)
572 return error("packfile %s size changed", p
->pack_name
);
574 /* Verify we recognize this pack file format. */
575 read_result
= read_in_full(p
->pack_fd
, &hdr
, sizeof(hdr
));
577 return error_errno("error reading from %s", p
->pack_name
);
578 if (read_result
!= sizeof(hdr
))
579 return error("file %s is far too short to be a packfile", p
->pack_name
);
580 if (hdr
.hdr_signature
!= htonl(PACK_SIGNATURE
))
581 return error("file %s is not a GIT packfile", p
->pack_name
);
582 if (!pack_version_ok(hdr
.hdr_version
))
583 return error("packfile %s is version %"PRIu32
" and not"
584 " supported (try upgrading GIT to a newer version)",
585 p
->pack_name
, ntohl(hdr
.hdr_version
));
587 /* Verify the pack matches its index. */
588 if (p
->num_objects
!= ntohl(hdr
.hdr_entries
))
589 return error("packfile %s claims to have %"PRIu32
" objects"
590 " while index indicates %"PRIu32
" objects",
591 p
->pack_name
, ntohl(hdr
.hdr_entries
),
593 read_result
= pread_in_full(p
->pack_fd
, hash
, hashsz
,
594 p
->pack_size
- hashsz
);
596 return error_errno("error reading from %s", p
->pack_name
);
597 if (read_result
!= hashsz
)
598 return error("packfile %s signature is unavailable", p
->pack_name
);
599 idx_hash
= ((unsigned char *)p
->index_data
) + p
->index_size
- hashsz
* 2;
600 if (!hasheq(hash
, idx_hash
, p
->repo
->hash_algo
))
601 return error("packfile %s does not match index", p
->pack_name
);
605 static int open_packed_git(struct packed_git
*p
)
607 if (!open_packed_git_1(p
))
613 static int in_window(struct repository
*r
, struct pack_window
*win
,
616 /* We must promise at least one full hash after the
617 * offset is available from this window, otherwise the offset
618 * is not actually in this window and a different window (which
619 * has that one hash excess) must be used. This is to support
620 * the object header and delta base parsing routines below.
622 off_t win_off
= win
->offset
;
623 return win_off
<= offset
624 && (offset
+ r
->hash_algo
->rawsz
) <= (win_off
+ win
->len
);
627 unsigned char *use_pack(struct packed_git
*p
,
628 struct pack_window
**w_cursor
,
632 struct pack_window
*win
= *w_cursor
;
634 /* Since packfiles end in a hash of their content and it's
635 * pointless to ask for an offset into the middle of that
636 * hash, and the in_window function above wouldn't match
637 * don't allow an offset too close to the end of the file.
639 if (!p
->pack_size
&& p
->pack_fd
== -1 && open_packed_git(p
))
640 die("packfile %s cannot be accessed", p
->pack_name
);
641 if (offset
> (p
->pack_size
- p
->repo
->hash_algo
->rawsz
))
642 die("offset beyond end of packfile (truncated pack?)");
644 die(_("offset before end of packfile (broken .idx?)"));
646 if (!win
|| !in_window(p
->repo
, win
, offset
)) {
649 for (win
= p
->windows
; win
; win
= win
->next
) {
650 if (in_window(p
->repo
, win
, offset
))
656 struct repo_settings
*settings
;
658 /* lazy load the settings in case it hasn't been setup */
659 prepare_repo_settings(p
->repo
);
660 settings
= &p
->repo
->settings
;
662 window_align
= settings
->packed_git_window_size
/ 2;
664 if (p
->pack_fd
== -1 && open_packed_git(p
))
665 die("packfile %s cannot be accessed", p
->pack_name
);
667 CALLOC_ARRAY(win
, 1);
668 win
->offset
= (offset
/ window_align
) * window_align
;
669 len
= p
->pack_size
- win
->offset
;
670 if (len
> settings
->packed_git_window_size
)
671 len
= settings
->packed_git_window_size
;
672 win
->len
= (size_t)len
;
673 pack_mapped
+= win
->len
;
675 while (settings
->packed_git_limit
< pack_mapped
676 && unuse_one_window(p
))
678 win
->base
= xmmap_gently(NULL
, win
->len
,
679 PROT_READ
, MAP_PRIVATE
,
680 p
->pack_fd
, win
->offset
);
681 if (win
->base
== MAP_FAILED
)
682 die_errno(_("packfile %s cannot be mapped%s"),
683 p
->pack_name
, mmap_os_err());
684 if (!win
->offset
&& win
->len
== p
->pack_size
689 if (pack_mapped
> peak_pack_mapped
)
690 peak_pack_mapped
= pack_mapped
;
691 if (pack_open_windows
> peak_pack_open_windows
)
692 peak_pack_open_windows
= pack_open_windows
;
693 win
->next
= p
->windows
;
697 if (win
!= *w_cursor
) {
698 win
->last_used
= pack_used_ctr
++;
702 offset
-= win
->offset
;
704 *left
= win
->len
- xsize_t(offset
);
705 return win
->base
+ offset
;
708 void unuse_pack(struct pack_window
**w_cursor
)
710 struct pack_window
*w
= *w_cursor
;
717 struct packed_git
*add_packed_git(struct repository
*r
, const char *path
,
718 size_t path_len
, int local
)
722 struct packed_git
*p
;
723 struct object_id oid
;
726 * Make sure a corresponding .pack file exists and that
727 * the index looks sane.
729 if (!strip_suffix_mem(path
, &path_len
, ".idx"))
733 * ".promisor" is long enough to hold any suffix we're adding (and
734 * the use xsnprintf double-checks that)
736 alloc
= st_add3(path_len
, strlen(".promisor"), 1);
737 p
= alloc_packed_git(r
, alloc
);
738 memcpy(p
->pack_name
, path
, path_len
);
741 * Note that we have to check auxiliary data structures before we check
742 * for the ".pack" file to exist to avoid races with a packfile that is
743 * in the process of being deleted. The ".pack" file is unlinked before
744 * its auxiliary data structures, so we know that we either get a
745 * consistent snapshot of all data structures or that we'll fail to
746 * stat(3p) the packfile itself and thus return `NULL`.
748 * As such, we cannot bail out before the access(3p) calls in case the
749 * packfile doesn't exist without doing two stat(3p) calls for it.
751 xsnprintf(p
->pack_name
+ path_len
, alloc
- path_len
, ".keep");
752 if (!access(p
->pack_name
, F_OK
))
755 xsnprintf(p
->pack_name
+ path_len
, alloc
- path_len
, ".promisor");
756 if (!access(p
->pack_name
, F_OK
))
757 p
->pack_promisor
= 1;
759 xsnprintf(p
->pack_name
+ path_len
, alloc
- path_len
, ".mtimes");
760 if (!access(p
->pack_name
, F_OK
))
763 xsnprintf(p
->pack_name
+ path_len
, alloc
- path_len
, ".pack");
764 if (stat(p
->pack_name
, &st
) || !S_ISREG(st
.st_mode
)) {
769 /* ok, it looks sane as far as we can check without
770 * actually mapping the pack file.
772 p
->pack_size
= st
.st_size
;
773 p
->pack_local
= local
;
774 p
->mtime
= st
.st_mtime
;
775 if (path_len
< r
->hash_algo
->hexsz
||
776 get_oid_hex_algop(path
+ path_len
- r
->hash_algo
->hexsz
, &oid
,
778 hashclr(p
->hash
, r
->hash_algo
);
780 hashcpy(p
->hash
, oid
.hash
, r
->hash_algo
);
785 void install_packed_git(struct repository
*r
, struct packed_git
*pack
)
787 if (pack
->pack_fd
!= -1)
790 pack
->next
= r
->objects
->packed_git
;
791 r
->objects
->packed_git
= pack
;
793 hashmap_entry_init(&pack
->packmap_ent
, strhash(pack
->pack_name
));
794 hashmap_add(&r
->objects
->pack_map
, &pack
->packmap_ent
);
797 void (*report_garbage
)(unsigned seen_bits
, const char *path
);
799 static void report_helper(const struct string_list
*list
,
800 int seen_bits
, int first
, int last
)
802 if (seen_bits
== (PACKDIR_FILE_PACK
|PACKDIR_FILE_IDX
))
805 for (; first
< last
; first
++)
806 report_garbage(seen_bits
, list
->items
[first
].string
);
809 static void report_pack_garbage(struct string_list
*list
)
811 int i
, baselen
= -1, first
= 0, seen_bits
= 0;
816 string_list_sort(list
);
818 for (i
= 0; i
< list
->nr
; i
++) {
819 const char *path
= list
->items
[i
].string
;
821 strncmp(path
, list
->items
[first
].string
, baselen
)) {
822 report_helper(list
, seen_bits
, first
, i
);
827 const char *dot
= strrchr(path
, '.');
829 report_garbage(PACKDIR_FILE_GARBAGE
, path
);
832 baselen
= dot
- path
+ 1;
835 if (!strcmp(path
+ baselen
, "pack"))
837 else if (!strcmp(path
+ baselen
, "idx"))
840 report_helper(list
, seen_bits
, first
, list
->nr
);
843 void for_each_file_in_pack_subdir(const char *objdir
,
845 each_file_in_pack_dir_fn fn
,
848 struct strbuf path
= STRBUF_INIT
;
853 strbuf_addstr(&path
, objdir
);
854 strbuf_addstr(&path
, "/pack");
856 strbuf_addf(&path
, "/%s", subdir
);
857 dir
= opendir(path
.buf
);
860 error_errno("unable to open object pack directory: %s",
862 strbuf_release(&path
);
865 strbuf_addch(&path
, '/');
866 dirnamelen
= path
.len
;
867 while ((de
= readdir_skip_dot_and_dotdot(dir
)) != NULL
) {
868 strbuf_setlen(&path
, dirnamelen
);
869 strbuf_addstr(&path
, de
->d_name
);
871 fn(path
.buf
, path
.len
, de
->d_name
, data
);
875 strbuf_release(&path
);
878 void for_each_file_in_pack_dir(const char *objdir
,
879 each_file_in_pack_dir_fn fn
,
882 for_each_file_in_pack_subdir(objdir
, NULL
, fn
, data
);
885 struct prepare_pack_data
{
886 struct repository
*r
;
887 struct string_list
*garbage
;
889 struct multi_pack_index
*m
;
892 static void prepare_pack(const char *full_name
, size_t full_name_len
,
893 const char *file_name
, void *_data
)
895 struct prepare_pack_data
*data
= (struct prepare_pack_data
*)_data
;
896 struct packed_git
*p
;
897 size_t base_len
= full_name_len
;
899 if (strip_suffix_mem(full_name
, &base_len
, ".idx") &&
900 !(data
->m
&& midx_contains_pack(data
->m
, file_name
))) {
901 struct hashmap_entry hent
;
902 char *pack_name
= xstrfmt("%.*s.pack", (int)base_len
, full_name
);
903 unsigned int hash
= strhash(pack_name
);
904 hashmap_entry_init(&hent
, hash
);
906 /* Don't reopen a pack we already have. */
907 if (!hashmap_get(&data
->r
->objects
->pack_map
, &hent
, pack_name
)) {
908 p
= add_packed_git(data
->r
, full_name
, full_name_len
, data
->local
);
910 install_packed_git(data
->r
, p
);
918 if (!strcmp(file_name
, "multi-pack-index") ||
919 !strcmp(file_name
, "multi-pack-index.d"))
921 if (starts_with(file_name
, "multi-pack-index") &&
922 (ends_with(file_name
, ".bitmap") || ends_with(file_name
, ".rev")))
924 if (ends_with(file_name
, ".idx") ||
925 ends_with(file_name
, ".rev") ||
926 ends_with(file_name
, ".pack") ||
927 ends_with(file_name
, ".bitmap") ||
928 ends_with(file_name
, ".keep") ||
929 ends_with(file_name
, ".promisor") ||
930 ends_with(file_name
, ".mtimes"))
931 string_list_append(data
->garbage
, full_name
);
933 report_garbage(PACKDIR_FILE_GARBAGE
, full_name
);
936 static void prepare_packed_git_one(struct repository
*r
, char *objdir
, int local
)
938 struct prepare_pack_data data
;
939 struct string_list garbage
= STRING_LIST_INIT_DUP
;
941 data
.m
= r
->objects
->multi_pack_index
;
943 /* look for the multi-pack-index for this object directory */
944 while (data
.m
&& strcmp(data
.m
->object_dir
, objdir
))
945 data
.m
= data
.m
->next
;
948 data
.garbage
= &garbage
;
951 for_each_file_in_pack_dir(objdir
, prepare_pack
, &data
);
953 report_pack_garbage(data
.garbage
);
954 string_list_clear(data
.garbage
, 0);
957 static void prepare_packed_git(struct repository
*r
);
959 * Give a fast, rough count of the number of objects in the repository. This
960 * ignores loose objects completely. If you have a lot of them, then either
961 * you should repack because your performance will be awful, or they are
962 * all unreachable objects about to be pruned, in which case they're not really
963 * interesting as a measure of repo size in the first place.
965 unsigned long repo_approximate_object_count(struct repository
*r
)
967 if (!r
->objects
->approximate_object_count_valid
) {
969 struct multi_pack_index
*m
;
970 struct packed_git
*p
;
972 prepare_packed_git(r
);
974 for (m
= get_multi_pack_index(r
); m
; m
= m
->next
)
975 count
+= m
->num_objects
;
976 for (p
= r
->objects
->packed_git
; p
; p
= p
->next
) {
977 if (open_pack_index(p
))
979 count
+= p
->num_objects
;
981 r
->objects
->approximate_object_count
= count
;
982 r
->objects
->approximate_object_count_valid
= 1;
984 return r
->objects
->approximate_object_count
;
987 DEFINE_LIST_SORT(static, sort_packs
, struct packed_git
, next
);
989 static int sort_pack(const struct packed_git
*a
, const struct packed_git
*b
)
994 * Local packs tend to contain objects specific to our
995 * variant of the project than remote ones. In addition,
996 * remote ones could be on a network mounted filesystem.
997 * Favor local ones for these reasons.
999 st
= a
->pack_local
- b
->pack_local
;
1004 * Younger packs tend to contain more recent objects,
1005 * and more recent objects tend to get accessed more
1008 if (a
->mtime
< b
->mtime
)
1010 else if (a
->mtime
== b
->mtime
)
1015 static void rearrange_packed_git(struct repository
*r
)
1017 sort_packs(&r
->objects
->packed_git
, sort_pack
);
1020 static void prepare_packed_git_mru(struct repository
*r
)
1022 struct packed_git
*p
;
1024 INIT_LIST_HEAD(&r
->objects
->packed_git_mru
);
1026 for (p
= r
->objects
->packed_git
; p
; p
= p
->next
)
1027 list_add_tail(&p
->mru
, &r
->objects
->packed_git_mru
);
1030 static void prepare_packed_git(struct repository
*r
)
1032 struct object_directory
*odb
;
1034 if (r
->objects
->packed_git_initialized
)
1038 for (odb
= r
->objects
->odb
; odb
; odb
= odb
->next
) {
1039 int local
= (odb
== r
->objects
->odb
);
1040 prepare_multi_pack_index_one(r
, odb
->path
, local
);
1041 prepare_packed_git_one(r
, odb
->path
, local
);
1043 rearrange_packed_git(r
);
1045 prepare_packed_git_mru(r
);
1046 r
->objects
->packed_git_initialized
= 1;
1049 void reprepare_packed_git(struct repository
*r
)
1051 struct object_directory
*odb
;
1056 * Reprepare alt odbs, in case the alternates file was modified
1057 * during the course of this process. This only _adds_ odbs to
1058 * the linked list, so existing odbs will continue to exist for
1059 * the lifetime of the process.
1061 r
->objects
->loaded_alternates
= 0;
1064 for (odb
= r
->objects
->odb
; odb
; odb
= odb
->next
)
1065 odb_clear_loose_cache(odb
);
1067 r
->objects
->approximate_object_count_valid
= 0;
1068 r
->objects
->packed_git_initialized
= 0;
1069 prepare_packed_git(r
);
1073 struct packed_git
*get_packed_git(struct repository
*r
)
1075 prepare_packed_git(r
);
1076 return r
->objects
->packed_git
;
1079 struct multi_pack_index
*get_multi_pack_index(struct repository
*r
)
1081 prepare_packed_git(r
);
1082 return r
->objects
->multi_pack_index
;
1085 struct multi_pack_index
*get_local_multi_pack_index(struct repository
*r
)
1087 struct multi_pack_index
*m
= get_multi_pack_index(r
);
1089 /* no need to iterate; we always put the local one first (if any) */
1096 struct packed_git
*get_all_packs(struct repository
*r
)
1098 struct multi_pack_index
*m
;
1100 prepare_packed_git(r
);
1101 for (m
= r
->objects
->multi_pack_index
; m
; m
= m
->next
) {
1103 for (i
= 0; i
< m
->num_packs
+ m
->num_packs_in_base
; i
++)
1104 prepare_midx_pack(r
, m
, i
);
1107 return r
->objects
->packed_git
;
1110 struct list_head
*get_packed_git_mru(struct repository
*r
)
1112 prepare_packed_git(r
);
1113 return &r
->objects
->packed_git_mru
;
1116 unsigned long unpack_object_header_buffer(const unsigned char *buf
,
1117 unsigned long len
, enum object_type
*type
, unsigned long *sizep
)
1121 unsigned long used
= 0;
1124 *type
= (c
>> 4) & 7;
1128 if (len
<= used
|| (bitsizeof(long) - 7) < shift
) {
1129 error("bad object header");
1134 size
= st_add(size
, st_left_shift(c
& 0x7f, shift
));
1137 *sizep
= cast_size_t_to_ulong(size
);
1141 unsigned long get_size_from_delta(struct packed_git
*p
,
1142 struct pack_window
**w_curs
,
1145 const unsigned char *data
;
1146 unsigned char delta_head
[20], *in
;
1150 memset(&stream
, 0, sizeof(stream
));
1151 stream
.next_out
= delta_head
;
1152 stream
.avail_out
= sizeof(delta_head
);
1154 git_inflate_init(&stream
);
1156 in
= use_pack(p
, w_curs
, curpos
, &stream
.avail_in
);
1157 stream
.next_in
= in
;
1159 * Note: the window section returned by use_pack() must be
1160 * available throughout git_inflate()'s unlocked execution. To
1161 * ensure no other thread will modify the window in the
1162 * meantime, we rely on the packed_window.inuse_cnt. This
1163 * counter is incremented before window reading and checked
1164 * before window disposal.
1166 * Other worrying sections could be the call to close_pack_fd(),
1167 * which can close packs even with in-use windows, and to
1168 * reprepare_packed_git(). Regarding the former, mmap doc says:
1169 * "closing the file descriptor does not unmap the region". And
1170 * for the latter, it won't re-open already available packs.
1173 st
= git_inflate(&stream
, Z_FINISH
);
1175 curpos
+= stream
.next_in
- in
;
1176 } while ((st
== Z_OK
|| st
== Z_BUF_ERROR
) &&
1177 stream
.total_out
< sizeof(delta_head
));
1178 git_inflate_end(&stream
);
1179 if ((st
!= Z_STREAM_END
) && stream
.total_out
!= sizeof(delta_head
)) {
1180 error("delta data unpack-initial failed");
1184 /* Examine the initial part of the delta to figure out
1189 /* ignore base size */
1190 get_delta_hdr_size(&data
, delta_head
+sizeof(delta_head
));
1192 /* Read the result size */
1193 return get_delta_hdr_size(&data
, delta_head
+sizeof(delta_head
));
1196 int unpack_object_header(struct packed_git
*p
,
1197 struct pack_window
**w_curs
,
1199 unsigned long *sizep
)
1201 unsigned char *base
;
1204 enum object_type type
;
1206 /* use_pack() assures us we have [base, base + 20) available
1207 * as a range that we can look at. (Its actually the hash
1208 * size that is assured.) With our object header encoding
1209 * the maximum deflated object size is 2^137, which is just
1210 * insane, so we know won't exceed what we have been given.
1212 base
= use_pack(p
, w_curs
, *curpos
, &left
);
1213 used
= unpack_object_header_buffer(base
, left
, &type
, sizep
);
1222 void mark_bad_packed_object(struct packed_git
*p
, const struct object_id
*oid
)
1224 oidset_insert(&p
->bad_objects
, oid
);
1227 const struct packed_git
*has_packed_and_bad(struct repository
*r
,
1228 const struct object_id
*oid
)
1230 struct packed_git
*p
;
1232 for (p
= r
->objects
->packed_git
; p
; p
= p
->next
)
1233 if (oidset_contains(&p
->bad_objects
, oid
))
1238 off_t
get_delta_base(struct packed_git
*p
,
1239 struct pack_window
**w_curs
,
1241 enum object_type type
,
1242 off_t delta_obj_offset
)
1244 unsigned char *base_info
= use_pack(p
, w_curs
, *curpos
, NULL
);
1247 /* use_pack() assured us we have [base_info, base_info + 20)
1248 * as a range that we can look at without walking off the
1249 * end of the mapped window. Its actually the hash size
1250 * that is assured. An OFS_DELTA longer than the hash size
1251 * is stupid, as then a REF_DELTA would be smaller to store.
1253 if (type
== OBJ_OFS_DELTA
) {
1255 unsigned char c
= base_info
[used
++];
1256 base_offset
= c
& 127;
1259 if (!base_offset
|| MSB(base_offset
, 7))
1260 return 0; /* overflow */
1261 c
= base_info
[used
++];
1262 base_offset
= (base_offset
<< 7) + (c
& 127);
1264 base_offset
= delta_obj_offset
- base_offset
;
1265 if (base_offset
<= 0 || base_offset
>= delta_obj_offset
)
1266 return 0; /* out of bound */
1268 } else if (type
== OBJ_REF_DELTA
) {
1269 /* The base entry _must_ be in the same pack */
1270 struct object_id oid
;
1271 oidread(&oid
, base_info
, p
->repo
->hash_algo
);
1272 base_offset
= find_pack_entry_one(&oid
, p
);
1273 *curpos
+= p
->repo
->hash_algo
->rawsz
;
1275 die("I am totally screwed");
1280 * Like get_delta_base above, but we return the sha1 instead of the pack
1281 * offset. This means it is cheaper for REF deltas (we do not have to do
1282 * the final object lookup), but more expensive for OFS deltas (we
1283 * have to load the revidx to convert the offset back into a sha1).
1285 static int get_delta_base_oid(struct packed_git
*p
,
1286 struct pack_window
**w_curs
,
1288 struct object_id
*oid
,
1289 enum object_type type
,
1290 off_t delta_obj_offset
)
1292 if (type
== OBJ_REF_DELTA
) {
1293 unsigned char *base
= use_pack(p
, w_curs
, curpos
, NULL
);
1294 oidread(oid
, base
, p
->repo
->hash_algo
);
1296 } else if (type
== OBJ_OFS_DELTA
) {
1298 off_t base_offset
= get_delta_base(p
, w_curs
, &curpos
,
1299 type
, delta_obj_offset
);
1304 if (offset_to_pack_pos(p
, base_offset
, &base_pos
) < 0)
1307 return nth_packed_object_id(oid
, p
,
1308 pack_pos_to_index(p
, base_pos
));
1313 static int retry_bad_packed_offset(struct repository
*r
,
1314 struct packed_git
*p
,
1319 struct object_id oid
;
1320 if (offset_to_pack_pos(p
, obj_offset
, &pos
) < 0)
1322 nth_packed_object_id(&oid
, p
, pack_pos_to_index(p
, pos
));
1323 mark_bad_packed_object(p
, &oid
);
1324 type
= oid_object_info(r
, &oid
, NULL
);
1325 if (type
<= OBJ_NONE
)
1330 #define POI_STACK_PREALLOC 64
1332 static enum object_type
packed_to_object_type(struct repository
*r
,
1333 struct packed_git
*p
,
1335 enum object_type type
,
1336 struct pack_window
**w_curs
,
1339 off_t small_poi_stack
[POI_STACK_PREALLOC
];
1340 off_t
*poi_stack
= small_poi_stack
;
1341 int poi_stack_nr
= 0, poi_stack_alloc
= POI_STACK_PREALLOC
;
1343 while (type
== OBJ_OFS_DELTA
|| type
== OBJ_REF_DELTA
) {
1346 /* Push the object we're going to leave behind */
1347 if (poi_stack_nr
>= poi_stack_alloc
&& poi_stack
== small_poi_stack
) {
1348 poi_stack_alloc
= alloc_nr(poi_stack_nr
);
1349 ALLOC_ARRAY(poi_stack
, poi_stack_alloc
);
1350 COPY_ARRAY(poi_stack
, small_poi_stack
, poi_stack_nr
);
1352 ALLOC_GROW(poi_stack
, poi_stack_nr
+1, poi_stack_alloc
);
1354 poi_stack
[poi_stack_nr
++] = obj_offset
;
1355 /* If parsing the base offset fails, just unwind */
1356 base_offset
= get_delta_base(p
, w_curs
, &curpos
, type
, obj_offset
);
1359 curpos
= obj_offset
= base_offset
;
1360 type
= unpack_object_header(p
, w_curs
, &curpos
, &size
);
1361 if (type
<= OBJ_NONE
) {
1362 /* If getting the base itself fails, we first
1363 * retry the base, otherwise unwind */
1364 type
= retry_bad_packed_offset(r
, p
, base_offset
);
1365 if (type
> OBJ_NONE
)
1379 error("unknown object type %i at offset %"PRIuMAX
" in %s",
1380 type
, (uintmax_t)obj_offset
, p
->pack_name
);
1385 if (poi_stack
!= small_poi_stack
)
1390 while (poi_stack_nr
) {
1391 obj_offset
= poi_stack
[--poi_stack_nr
];
1392 type
= retry_bad_packed_offset(r
, p
, obj_offset
);
1393 if (type
> OBJ_NONE
)
1400 static struct hashmap delta_base_cache
;
1401 static size_t delta_base_cached
;
1403 static LIST_HEAD(delta_base_cache_lru
);
1405 struct delta_base_cache_key
{
1406 struct packed_git
*p
;
1410 struct delta_base_cache_entry
{
1411 struct hashmap_entry ent
;
1412 struct delta_base_cache_key key
;
1413 struct list_head lru
;
1416 enum object_type type
;
1419 static unsigned int pack_entry_hash(struct packed_git
*p
, off_t base_offset
)
1423 hash
= (unsigned int)(intptr_t)p
+ (unsigned int)base_offset
;
1424 hash
+= (hash
>> 8) + (hash
>> 16);
1428 static struct delta_base_cache_entry
*
1429 get_delta_base_cache_entry(struct packed_git
*p
, off_t base_offset
)
1431 struct hashmap_entry entry
, *e
;
1432 struct delta_base_cache_key key
;
1434 if (!delta_base_cache
.cmpfn
)
1437 hashmap_entry_init(&entry
, pack_entry_hash(p
, base_offset
));
1439 key
.base_offset
= base_offset
;
1440 e
= hashmap_get(&delta_base_cache
, &entry
, &key
);
1441 return e
? container_of(e
, struct delta_base_cache_entry
, ent
) : NULL
;
1444 static int delta_base_cache_key_eq(const struct delta_base_cache_key
*a
,
1445 const struct delta_base_cache_key
*b
)
1447 return a
->p
== b
->p
&& a
->base_offset
== b
->base_offset
;
1450 static int delta_base_cache_hash_cmp(const void *cmp_data UNUSED
,
1451 const struct hashmap_entry
*va
,
1452 const struct hashmap_entry
*vb
,
1455 const struct delta_base_cache_entry
*a
, *b
;
1456 const struct delta_base_cache_key
*key
= vkey
;
1458 a
= container_of(va
, const struct delta_base_cache_entry
, ent
);
1459 b
= container_of(vb
, const struct delta_base_cache_entry
, ent
);
1462 return !delta_base_cache_key_eq(&a
->key
, key
);
1464 return !delta_base_cache_key_eq(&a
->key
, &b
->key
);
1467 static int in_delta_base_cache(struct packed_git
*p
, off_t base_offset
)
1469 return !!get_delta_base_cache_entry(p
, base_offset
);
1473 * Remove the entry from the cache, but do _not_ free the associated
1474 * entry data. The caller takes ownership of the "data" buffer, and
1475 * should copy out any fields it wants before detaching.
1477 static void detach_delta_base_cache_entry(struct delta_base_cache_entry
*ent
)
1479 hashmap_remove(&delta_base_cache
, &ent
->ent
, &ent
->key
);
1480 list_del(&ent
->lru
);
1481 delta_base_cached
-= ent
->size
;
1485 static void *cache_or_unpack_entry(struct repository
*r
, struct packed_git
*p
,
1486 off_t base_offset
, unsigned long *base_size
,
1487 enum object_type
*type
)
1489 struct delta_base_cache_entry
*ent
;
1491 ent
= get_delta_base_cache_entry(p
, base_offset
);
1493 return unpack_entry(r
, p
, base_offset
, type
, base_size
);
1498 *base_size
= ent
->size
;
1499 return xmemdupz(ent
->data
, ent
->size
);
1502 static inline void release_delta_base_cache(struct delta_base_cache_entry
*ent
)
1505 detach_delta_base_cache_entry(ent
);
1508 void clear_delta_base_cache(void)
1510 struct list_head
*lru
, *tmp
;
1511 list_for_each_safe(lru
, tmp
, &delta_base_cache_lru
) {
1512 struct delta_base_cache_entry
*entry
=
1513 list_entry(lru
, struct delta_base_cache_entry
, lru
);
1514 release_delta_base_cache(entry
);
1518 static void add_delta_base_cache(struct packed_git
*p
, off_t base_offset
,
1519 void *base
, unsigned long base_size
,
1520 unsigned long delta_base_cache_limit
,
1521 enum object_type type
)
1523 struct delta_base_cache_entry
*ent
;
1524 struct list_head
*lru
, *tmp
;
1527 * Check required to avoid redundant entries when more than one thread
1528 * is unpacking the same object, in unpack_entry() (since its phases I
1529 * and III might run concurrently across multiple threads).
1531 if (in_delta_base_cache(p
, base_offset
)) {
1536 delta_base_cached
+= base_size
;
1538 list_for_each_safe(lru
, tmp
, &delta_base_cache_lru
) {
1539 struct delta_base_cache_entry
*f
=
1540 list_entry(lru
, struct delta_base_cache_entry
, lru
);
1541 if (delta_base_cached
<= delta_base_cache_limit
)
1543 release_delta_base_cache(f
);
1546 ent
= xmalloc(sizeof(*ent
));
1548 ent
->key
.base_offset
= base_offset
;
1551 ent
->size
= base_size
;
1552 list_add_tail(&ent
->lru
, &delta_base_cache_lru
);
1554 if (!delta_base_cache
.cmpfn
)
1555 hashmap_init(&delta_base_cache
, delta_base_cache_hash_cmp
, NULL
, 0);
1556 hashmap_entry_init(&ent
->ent
, pack_entry_hash(p
, base_offset
));
1557 hashmap_add(&delta_base_cache
, &ent
->ent
);
1560 int packed_object_info(struct repository
*r
, struct packed_git
*p
,
1561 off_t obj_offset
, struct object_info
*oi
)
1563 struct pack_window
*w_curs
= NULL
;
1565 off_t curpos
= obj_offset
;
1566 enum object_type type
;
1569 * We always get the representation type, but only convert it to
1570 * a "real" type later if the caller is interested.
1573 *oi
->contentp
= cache_or_unpack_entry(r
, p
, obj_offset
, oi
->sizep
,
1578 type
= unpack_object_header(p
, &w_curs
, &curpos
, &size
);
1581 if (!oi
->contentp
&& oi
->sizep
) {
1582 if (type
== OBJ_OFS_DELTA
|| type
== OBJ_REF_DELTA
) {
1583 off_t tmp_pos
= curpos
;
1584 off_t base_offset
= get_delta_base(p
, &w_curs
, &tmp_pos
,
1590 *oi
->sizep
= get_size_from_delta(p
, &w_curs
, tmp_pos
);
1591 if (*oi
->sizep
== 0) {
1600 if (oi
->disk_sizep
) {
1602 if (offset_to_pack_pos(p
, obj_offset
, &pos
) < 0) {
1603 error("could not find object at offset %"PRIuMAX
" "
1604 "in pack %s", (uintmax_t)obj_offset
, p
->pack_name
);
1609 *oi
->disk_sizep
= pack_pos_to_offset(p
, pos
+ 1) - obj_offset
;
1613 enum object_type ptot
;
1614 ptot
= packed_to_object_type(r
, p
, obj_offset
,
1615 type
, &w_curs
, curpos
);
1624 if (oi
->delta_base_oid
) {
1625 if (type
== OBJ_OFS_DELTA
|| type
== OBJ_REF_DELTA
) {
1626 if (get_delta_base_oid(p
, &w_curs
, curpos
,
1628 type
, obj_offset
) < 0) {
1633 oidclr(oi
->delta_base_oid
, p
->repo
->hash_algo
);
1636 oi
->whence
= in_delta_base_cache(p
, obj_offset
) ? OI_DBCACHED
:
1640 unuse_pack(&w_curs
);
1644 static void *unpack_compressed_entry(struct packed_git
*p
,
1645 struct pack_window
**w_curs
,
1651 unsigned char *buffer
, *in
;
1653 buffer
= xmallocz_gently(size
);
1656 memset(&stream
, 0, sizeof(stream
));
1657 stream
.next_out
= buffer
;
1658 stream
.avail_out
= size
+ 1;
1660 git_inflate_init(&stream
);
1662 in
= use_pack(p
, w_curs
, curpos
, &stream
.avail_in
);
1663 stream
.next_in
= in
;
1665 * Note: we must ensure the window section returned by
1666 * use_pack() will be available throughout git_inflate()'s
1667 * unlocked execution. Please refer to the comment at
1668 * get_size_from_delta() to see how this is done.
1671 st
= git_inflate(&stream
, Z_FINISH
);
1673 if (!stream
.avail_out
)
1674 break; /* the payload is larger than it should be */
1675 curpos
+= stream
.next_in
- in
;
1676 } while (st
== Z_OK
|| st
== Z_BUF_ERROR
);
1677 git_inflate_end(&stream
);
1678 if ((st
!= Z_STREAM_END
) || stream
.total_out
!= size
) {
1683 /* versions of zlib can clobber unconsumed portion of outbuf */
1684 buffer
[size
] = '\0';
1689 static void write_pack_access_log(struct packed_git
*p
, off_t obj_offset
)
1691 static struct trace_key pack_access
= TRACE_KEY_INIT(PACK_ACCESS
);
1692 trace_printf_key(&pack_access
, "%s %"PRIuMAX
"\n",
1693 p
->pack_name
, (uintmax_t)obj_offset
);
1696 int do_check_packed_object_crc
;
1698 #define UNPACK_ENTRY_STACK_PREALLOC 64
1699 struct unpack_entry_stack_ent
{
1705 void *unpack_entry(struct repository
*r
, struct packed_git
*p
, off_t obj_offset
,
1706 enum object_type
*final_type
, unsigned long *final_size
)
1708 struct pack_window
*w_curs
= NULL
;
1709 off_t curpos
= obj_offset
;
1712 enum object_type type
;
1713 struct unpack_entry_stack_ent small_delta_stack
[UNPACK_ENTRY_STACK_PREALLOC
];
1714 struct unpack_entry_stack_ent
*delta_stack
= small_delta_stack
;
1715 int delta_stack_nr
= 0, delta_stack_alloc
= UNPACK_ENTRY_STACK_PREALLOC
;
1716 int base_from_cache
= 0;
1718 prepare_repo_settings(p
->repo
);
1720 write_pack_access_log(p
, obj_offset
);
1722 /* PHASE 1: drill down to the innermost base object */
1726 struct delta_base_cache_entry
*ent
;
1728 ent
= get_delta_base_cache_entry(p
, curpos
);
1733 detach_delta_base_cache_entry(ent
);
1734 base_from_cache
= 1;
1738 if (do_check_packed_object_crc
&& p
->index_version
> 1) {
1739 uint32_t pack_pos
, index_pos
;
1742 if (offset_to_pack_pos(p
, obj_offset
, &pack_pos
) < 0) {
1743 error("could not find object at offset %"PRIuMAX
" in pack %s",
1744 (uintmax_t)obj_offset
, p
->pack_name
);
1749 len
= pack_pos_to_offset(p
, pack_pos
+ 1) - obj_offset
;
1750 index_pos
= pack_pos_to_index(p
, pack_pos
);
1751 if (check_pack_crc(p
, &w_curs
, obj_offset
, len
, index_pos
)) {
1752 struct object_id oid
;
1753 nth_packed_object_id(&oid
, p
, index_pos
);
1754 error("bad packed object CRC for %s",
1756 mark_bad_packed_object(p
, &oid
);
1762 type
= unpack_object_header(p
, &w_curs
, &curpos
, &size
);
1763 if (type
!= OBJ_OFS_DELTA
&& type
!= OBJ_REF_DELTA
)
1766 base_offset
= get_delta_base(p
, &w_curs
, &curpos
, type
, obj_offset
);
1768 error("failed to validate delta base reference "
1769 "at offset %"PRIuMAX
" from %s",
1770 (uintmax_t)curpos
, p
->pack_name
);
1771 /* bail to phase 2, in hopes of recovery */
1776 /* push object, proceed to base */
1777 if (delta_stack_nr
>= delta_stack_alloc
1778 && delta_stack
== small_delta_stack
) {
1779 delta_stack_alloc
= alloc_nr(delta_stack_nr
);
1780 ALLOC_ARRAY(delta_stack
, delta_stack_alloc
);
1781 COPY_ARRAY(delta_stack
, small_delta_stack
,
1784 ALLOC_GROW(delta_stack
, delta_stack_nr
+1, delta_stack_alloc
);
1786 i
= delta_stack_nr
++;
1787 delta_stack
[i
].obj_offset
= obj_offset
;
1788 delta_stack
[i
].curpos
= curpos
;
1789 delta_stack
[i
].size
= size
;
1791 curpos
= obj_offset
= base_offset
;
1794 /* PHASE 2: handle the base */
1799 BUG("unpack_entry: left loop at a valid delta");
1805 if (!base_from_cache
)
1806 data
= unpack_compressed_entry(p
, &w_curs
, curpos
, size
);
1810 error("unknown object type %i at offset %"PRIuMAX
" in %s",
1811 type
, (uintmax_t)obj_offset
, p
->pack_name
);
1814 /* PHASE 3: apply deltas in order */
1817 * 'data' holds the base data, or NULL if there was corruption
1819 while (delta_stack_nr
) {
1822 void *external_base
= NULL
;
1823 unsigned long delta_size
, base_size
= size
;
1825 off_t base_obj_offset
= obj_offset
;
1831 * We're probably in deep shit, but let's try to fetch
1832 * the required base anyway from another pack or loose.
1833 * This is costly but should happen only in the presence
1834 * of a corrupted pack, and is better than failing outright.
1837 struct object_id base_oid
;
1838 if (!(offset_to_pack_pos(p
, obj_offset
, &pos
))) {
1839 struct object_info oi
= OBJECT_INFO_INIT
;
1841 nth_packed_object_id(&base_oid
, p
,
1842 pack_pos_to_index(p
, pos
));
1843 error("failed to read delta base object %s"
1844 " at offset %"PRIuMAX
" from %s",
1845 oid_to_hex(&base_oid
), (uintmax_t)obj_offset
,
1847 mark_bad_packed_object(p
, &base_oid
);
1850 oi
.sizep
= &base_size
;
1851 oi
.contentp
= &base
;
1852 if (oid_object_info_extended(r
, &base_oid
, &oi
, 0) < 0)
1855 external_base
= base
;
1859 i
= --delta_stack_nr
;
1860 obj_offset
= delta_stack
[i
].obj_offset
;
1861 curpos
= delta_stack
[i
].curpos
;
1862 delta_size
= delta_stack
[i
].size
;
1867 delta_data
= unpack_compressed_entry(p
, &w_curs
, curpos
, delta_size
);
1870 error("failed to unpack compressed delta "
1871 "at offset %"PRIuMAX
" from %s",
1872 (uintmax_t)curpos
, p
->pack_name
);
1875 data
= patch_delta(base
, base_size
, delta_data
,
1879 * We could not apply the delta; warn the user, but
1880 * keep going. Our failure will be noticed either in
1881 * the next iteration of the loop, or if this is the
1882 * final delta, in the caller when we return NULL.
1883 * Those code paths will take care of making a more
1884 * explicit warning and retrying with another copy of
1888 error("failed to apply delta");
1892 * We delay adding `base` to the cache until the end of the loop
1893 * because unpack_compressed_entry() momentarily releases the
1894 * obj_read_mutex, giving another thread the chance to access
1895 * the cache. Therefore, if `base` was already there, this other
1896 * thread could free() it (e.g. to make space for another entry)
1897 * before we are done using it.
1900 add_delta_base_cache(p
, base_obj_offset
, base
, base_size
,
1901 p
->repo
->settings
.delta_base_cache_limit
,
1905 free(external_base
);
1914 unuse_pack(&w_curs
);
1916 if (delta_stack
!= small_delta_stack
)
1922 int bsearch_pack(const struct object_id
*oid
, const struct packed_git
*p
, uint32_t *result
)
1924 const unsigned char *index_fanout
= p
->index_data
;
1925 const unsigned char *index_lookup
;
1926 const unsigned int hashsz
= p
->repo
->hash_algo
->rawsz
;
1927 int index_lookup_width
;
1930 BUG("bsearch_pack called without a valid pack-index");
1932 index_lookup
= index_fanout
+ 4 * 256;
1933 if (p
->index_version
== 1) {
1934 index_lookup_width
= hashsz
+ 4;
1937 index_lookup_width
= hashsz
;
1942 return bsearch_hash(oid
->hash
, (const uint32_t*)index_fanout
,
1943 index_lookup
, index_lookup_width
, result
);
1946 int nth_packed_object_id(struct object_id
*oid
,
1947 struct packed_git
*p
,
1950 const unsigned char *index
= p
->index_data
;
1951 const unsigned int hashsz
= p
->repo
->hash_algo
->rawsz
;
1953 if (open_pack_index(p
))
1955 index
= p
->index_data
;
1957 if (n
>= p
->num_objects
)
1960 if (p
->index_version
== 1) {
1961 oidread(oid
, index
+ st_add(st_mult(hashsz
+ 4, n
), 4),
1962 p
->repo
->hash_algo
);
1965 oidread(oid
, index
+ st_mult(hashsz
, n
), p
->repo
->hash_algo
);
1970 void check_pack_index_ptr(const struct packed_git
*p
, const void *vptr
)
1972 const unsigned char *ptr
= vptr
;
1973 const unsigned char *start
= p
->index_data
;
1974 const unsigned char *end
= start
+ p
->index_size
;
1976 die(_("offset before start of pack index for %s (corrupt index?)"),
1978 /* No need to check for underflow; .idx files must be at least 8 bytes */
1980 die(_("offset beyond end of pack index for %s (truncated index?)"),
1984 off_t
nth_packed_object_offset(const struct packed_git
*p
, uint32_t n
)
1986 const unsigned char *index
= p
->index_data
;
1987 const unsigned int hashsz
= p
->repo
->hash_algo
->rawsz
;
1989 if (p
->index_version
== 1) {
1990 return ntohl(*((uint32_t *)(index
+ st_mult(hashsz
+ 4, n
))));
1993 index
+= st_add(8, st_mult(p
->num_objects
, hashsz
+ 4));
1994 off
= ntohl(*((uint32_t *)(index
+ st_mult(4, n
))));
1995 if (!(off
& 0x80000000))
1997 index
+= st_add(st_mult(p
->num_objects
, 4),
1998 st_mult(off
& 0x7fffffff, 8));
1999 check_pack_index_ptr(p
, index
);
2000 return get_be64(index
);
2004 off_t
find_pack_entry_one(const struct object_id
*oid
,
2005 struct packed_git
*p
)
2007 const unsigned char *index
= p
->index_data
;
2011 if (open_pack_index(p
))
2015 if (bsearch_pack(oid
, p
, &result
))
2016 return nth_packed_object_offset(p
, result
);
2020 int is_pack_valid(struct packed_git
*p
)
2022 /* An already open pack is known to be valid. */
2023 if (p
->pack_fd
!= -1)
2026 /* If the pack has one window completely covering the
2027 * file size, the pack is known to be valid even if
2028 * the descriptor is not currently open.
2031 struct pack_window
*w
= p
->windows
;
2033 if (!w
->offset
&& w
->len
== p
->pack_size
)
2037 /* Force the pack to open to prove its valid. */
2038 return !open_packed_git(p
);
2041 struct packed_git
*find_oid_pack(const struct object_id
*oid
,
2042 struct packed_git
*packs
)
2044 struct packed_git
*p
;
2046 for (p
= packs
; p
; p
= p
->next
) {
2047 if (find_pack_entry_one(oid
, p
))
2054 static int fill_pack_entry(const struct object_id
*oid
,
2055 struct pack_entry
*e
,
2056 struct packed_git
*p
)
2060 if (oidset_size(&p
->bad_objects
) &&
2061 oidset_contains(&p
->bad_objects
, oid
))
2064 offset
= find_pack_entry_one(oid
, p
);
2069 * We are about to tell the caller where they can locate the
2070 * requested object. We better make sure the packfile is
2071 * still here and can be accessed before supplying that
2072 * answer, as it may have been deleted since the index was
2075 if (!is_pack_valid(p
))
2082 int find_pack_entry(struct repository
*r
, const struct object_id
*oid
, struct pack_entry
*e
)
2084 struct list_head
*pos
;
2085 struct multi_pack_index
*m
;
2087 prepare_packed_git(r
);
2088 if (!r
->objects
->packed_git
&& !r
->objects
->multi_pack_index
)
2091 for (m
= r
->objects
->multi_pack_index
; m
; m
= m
->next
) {
2092 if (fill_midx_entry(r
, oid
, e
, m
))
2096 list_for_each(pos
, &r
->objects
->packed_git_mru
) {
2097 struct packed_git
*p
= list_entry(pos
, struct packed_git
, mru
);
2098 if (!p
->multi_pack_index
&& fill_pack_entry(oid
, e
, p
)) {
2099 list_move(&p
->mru
, &r
->objects
->packed_git_mru
);
2106 static void maybe_invalidate_kept_pack_cache(struct repository
*r
,
2109 if (!r
->objects
->kept_pack_cache
.packs
)
2111 if (r
->objects
->kept_pack_cache
.flags
== flags
)
2113 FREE_AND_NULL(r
->objects
->kept_pack_cache
.packs
);
2114 r
->objects
->kept_pack_cache
.flags
= 0;
2117 struct packed_git
**kept_pack_cache(struct repository
*r
, unsigned flags
)
2119 maybe_invalidate_kept_pack_cache(r
, flags
);
2121 if (!r
->objects
->kept_pack_cache
.packs
) {
2122 struct packed_git
**packs
= NULL
;
2123 size_t nr
= 0, alloc
= 0;
2124 struct packed_git
*p
;
2127 * We want "all" packs here, because we need to cover ones that
2128 * are used by a midx, as well. We need to look in every one of
2129 * them (instead of the midx itself) to cover duplicates. It's
2130 * possible that an object is found in two packs that the midx
2131 * covers, one kept and one not kept, but the midx returns only
2132 * the non-kept version.
2134 for (p
= get_all_packs(r
); p
; p
= p
->next
) {
2135 if ((p
->pack_keep
&& (flags
& ON_DISK_KEEP_PACKS
)) ||
2136 (p
->pack_keep_in_core
&& (flags
& IN_CORE_KEEP_PACKS
))) {
2137 ALLOC_GROW(packs
, nr
+ 1, alloc
);
2141 ALLOC_GROW(packs
, nr
+ 1, alloc
);
2144 r
->objects
->kept_pack_cache
.packs
= packs
;
2145 r
->objects
->kept_pack_cache
.flags
= flags
;
2148 return r
->objects
->kept_pack_cache
.packs
;
2151 int find_kept_pack_entry(struct repository
*r
,
2152 const struct object_id
*oid
,
2154 struct pack_entry
*e
)
2156 struct packed_git
**cache
;
2158 for (cache
= kept_pack_cache(r
, flags
); *cache
; cache
++) {
2159 struct packed_git
*p
= *cache
;
2160 if (fill_pack_entry(oid
, e
, p
))
2167 int has_object_pack(struct repository
*r
, const struct object_id
*oid
)
2169 struct pack_entry e
;
2170 return find_pack_entry(r
, oid
, &e
);
2173 int has_object_kept_pack(struct repository
*r
, const struct object_id
*oid
,
2176 struct pack_entry e
;
2177 return find_kept_pack_entry(r
, oid
, flags
, &e
);
2180 int for_each_object_in_pack(struct packed_git
*p
,
2181 each_packed_object_fn cb
, void *data
,
2182 enum for_each_object_flags flags
)
2187 if (flags
& FOR_EACH_OBJECT_PACK_ORDER
) {
2188 if (load_pack_revindex(p
->repo
, p
))
2192 for (i
= 0; i
< p
->num_objects
; i
++) {
2194 struct object_id oid
;
2197 * We are iterating "i" from 0 up to num_objects, but its
2198 * meaning may be different, depending on the requested output
2201 * - in object-name order, it is the same as the index order
2202 * used by nth_packed_object_id(), so we can pass it
2205 * - in pack-order, it is pack position, which we must
2206 * convert to an index position in order to get the oid.
2208 if (flags
& FOR_EACH_OBJECT_PACK_ORDER
)
2209 index_pos
= pack_pos_to_index(p
, i
);
2213 if (nth_packed_object_id(&oid
, p
, index_pos
) < 0)
2214 return error("unable to get sha1 of object %u in %s",
2215 index_pos
, p
->pack_name
);
2217 r
= cb(&oid
, p
, index_pos
, data
);
2224 int for_each_packed_object(struct repository
*repo
, each_packed_object_fn cb
,
2225 void *data
, enum for_each_object_flags flags
)
2227 struct packed_git
*p
;
2229 int pack_errors
= 0;
2231 for (p
= get_all_packs(repo
); p
; p
= p
->next
) {
2232 if ((flags
& FOR_EACH_OBJECT_LOCAL_ONLY
) && !p
->pack_local
)
2234 if ((flags
& FOR_EACH_OBJECT_PROMISOR_ONLY
) &&
2237 if ((flags
& FOR_EACH_OBJECT_SKIP_IN_CORE_KEPT_PACKS
) &&
2238 p
->pack_keep_in_core
)
2240 if ((flags
& FOR_EACH_OBJECT_SKIP_ON_DISK_KEPT_PACKS
) &&
2243 if (open_pack_index(p
)) {
2247 r
= for_each_object_in_pack(p
, cb
, data
, flags
);
2251 return r
? r
: pack_errors
;
2254 static int add_promisor_object(const struct object_id
*oid
,
2255 struct packed_git
*pack
,
2256 uint32_t pos UNUSED
,
2259 struct oidset
*set
= set_
;
2261 int we_parsed_object
;
2263 obj
= lookup_object(pack
->repo
, oid
);
2264 if (obj
&& obj
->parsed
) {
2265 we_parsed_object
= 0;
2267 we_parsed_object
= 1;
2268 obj
= parse_object(pack
->repo
, oid
);
2274 oidset_insert(set
, oid
);
2277 * If this is a tree, commit, or tag, the objects it refers
2278 * to are also promisor objects. (Blobs refer to no objects->)
2280 if (obj
->type
== OBJ_TREE
) {
2281 struct tree
*tree
= (struct tree
*)obj
;
2282 struct tree_desc desc
;
2283 struct name_entry entry
;
2284 if (init_tree_desc_gently(&desc
, &tree
->object
.oid
,
2285 tree
->buffer
, tree
->size
, 0))
2287 * Error messages are given when packs are
2288 * verified, so do not print any here.
2291 while (tree_entry_gently(&desc
, &entry
))
2292 oidset_insert(set
, &entry
.oid
);
2293 if (we_parsed_object
)
2294 free_tree_buffer(tree
);
2295 } else if (obj
->type
== OBJ_COMMIT
) {
2296 struct commit
*commit
= (struct commit
*) obj
;
2297 struct commit_list
*parents
= commit
->parents
;
2299 oidset_insert(set
, get_commit_tree_oid(commit
));
2300 for (; parents
; parents
= parents
->next
)
2301 oidset_insert(set
, &parents
->item
->object
.oid
);
2302 } else if (obj
->type
== OBJ_TAG
) {
2303 struct tag
*tag
= (struct tag
*) obj
;
2304 oidset_insert(set
, get_tagged_oid(tag
));
2309 int is_promisor_object(struct repository
*r
, const struct object_id
*oid
)
2311 static struct oidset promisor_objects
;
2312 static int promisor_objects_prepared
;
2314 if (!promisor_objects_prepared
) {
2315 if (repo_has_promisor_remote(r
)) {
2316 for_each_packed_object(r
, add_promisor_object
,
2318 FOR_EACH_OBJECT_PROMISOR_ONLY
|
2319 FOR_EACH_OBJECT_PACK_ORDER
);
2321 promisor_objects_prepared
= 1;
2323 return oidset_contains(&promisor_objects
, oid
);
2326 int parse_pack_header_option(const char *in
, unsigned char *out
, unsigned int *len
)
2332 put_be32(hdr
, PACK_SIGNATURE
);
2334 put_be32(hdr
, strtoul(in
, &c
, 10));
2338 put_be32(hdr
, strtoul(c
+ 1, &c
, 10));