9 char *odb_pack_name(struct strbuf
*buf
,
10 const unsigned char *sha1
,
14 strbuf_addf(buf
, "%s/pack/pack-%s.%s", get_object_directory(),
15 sha1_to_hex(sha1
), ext
);
19 char *sha1_pack_name(const unsigned char *sha1
)
21 static struct strbuf buf
= STRBUF_INIT
;
22 return odb_pack_name(&buf
, sha1
, "pack");
25 char *sha1_pack_index_name(const unsigned char *sha1
)
27 static struct strbuf buf
= STRBUF_INIT
;
28 return odb_pack_name(&buf
, sha1
, "idx");
31 static unsigned int pack_used_ctr
;
32 static unsigned int pack_mmap_calls
;
33 static unsigned int peak_pack_open_windows
;
34 static unsigned int pack_open_windows
;
35 static unsigned int pack_open_fds
;
36 static unsigned int pack_max_fds
;
37 static size_t peak_pack_mapped
;
38 static size_t pack_mapped
;
39 struct packed_git
*packed_git
;
41 static struct mru packed_git_mru_storage
;
42 struct mru
*packed_git_mru
= &packed_git_mru_storage
;
44 #define SZ_FMT PRIuMAX
45 static inline uintmax_t sz_fmt(size_t s
) { return s
; }
47 void pack_report(void)
50 "pack_report: getpagesize() = %10" SZ_FMT
"\n"
51 "pack_report: core.packedGitWindowSize = %10" SZ_FMT
"\n"
52 "pack_report: core.packedGitLimit = %10" SZ_FMT
"\n",
53 sz_fmt(getpagesize()),
54 sz_fmt(packed_git_window_size
),
55 sz_fmt(packed_git_limit
));
57 "pack_report: pack_used_ctr = %10u\n"
58 "pack_report: pack_mmap_calls = %10u\n"
59 "pack_report: pack_open_windows = %10u / %10u\n"
60 "pack_report: pack_mapped = "
61 "%10" SZ_FMT
" / %10" SZ_FMT
"\n",
64 pack_open_windows
, peak_pack_open_windows
,
65 sz_fmt(pack_mapped
), sz_fmt(peak_pack_mapped
));
69 * Open and mmap the index file at path, perform a couple of
70 * consistency checks, then record its information to p. Return 0 on
73 static int check_packed_git_idx(const char *path
, struct packed_git
*p
)
76 struct pack_idx_header
*hdr
;
78 uint32_t version
, nr
, i
, *index
;
79 int fd
= git_open(path
);
88 idx_size
= xsize_t(st
.st_size
);
89 if (idx_size
< 4 * 256 + 20 + 20) {
91 return error("index file %s is too small", path
);
93 idx_map
= xmmap(NULL
, idx_size
, PROT_READ
, MAP_PRIVATE
, fd
, 0);
97 if (hdr
->idx_signature
== htonl(PACK_IDX_SIGNATURE
)) {
98 version
= ntohl(hdr
->idx_version
);
99 if (version
< 2 || version
> 2) {
100 munmap(idx_map
, idx_size
);
101 return error("index file %s is version %"PRIu32
102 " and is not supported by this binary"
103 " (try upgrading GIT to a newer version)",
112 index
+= 2; /* skip index header */
113 for (i
= 0; i
< 256; i
++) {
114 uint32_t n
= ntohl(index
[i
]);
116 munmap(idx_map
, idx_size
);
117 return error("non-monotonic index %s", path
);
125 * - 256 index entries 4 bytes each
126 * - 24-byte entries * nr (20-byte sha1 + 4-byte offset)
127 * - 20-byte SHA1 of the packfile
128 * - 20-byte SHA1 file checksum
130 if (idx_size
!= 4*256 + nr
* 24 + 20 + 20) {
131 munmap(idx_map
, idx_size
);
132 return error("wrong index v1 file size in %s", path
);
134 } else if (version
== 2) {
137 * - 8 bytes of header
138 * - 256 index entries 4 bytes each
139 * - 20-byte sha1 entry * nr
140 * - 4-byte crc entry * nr
141 * - 4-byte offset entry * nr
142 * - 20-byte SHA1 of the packfile
143 * - 20-byte SHA1 file checksum
144 * And after the 4-byte offset table might be a
145 * variable sized table containing 8-byte entries
146 * for offsets larger than 2^31.
148 unsigned long min_size
= 8 + 4*256 + nr
*(20 + 4 + 4) + 20 + 20;
149 unsigned long max_size
= min_size
;
151 max_size
+= (nr
- 1)*8;
152 if (idx_size
< min_size
|| idx_size
> max_size
) {
153 munmap(idx_map
, idx_size
);
154 return error("wrong index v2 file size in %s", path
);
156 if (idx_size
!= min_size
&&
158 * make sure we can deal with large pack offsets.
159 * 31-bit signed offset won't be enough, neither
160 * 32-bit unsigned one will be.
162 (sizeof(off_t
) <= 4)) {
163 munmap(idx_map
, idx_size
);
164 return error("pack too large for current definition of off_t in %s", path
);
168 p
->index_version
= version
;
169 p
->index_data
= idx_map
;
170 p
->index_size
= idx_size
;
175 int open_pack_index(struct packed_git
*p
)
184 if (!strip_suffix(p
->pack_name
, ".pack", &len
))
185 die("BUG: pack_name does not end in .pack");
186 idx_name
= xstrfmt("%.*s.idx", (int)len
, p
->pack_name
);
187 ret
= check_packed_git_idx(idx_name
, p
);
192 static struct packed_git
*alloc_packed_git(int extra
)
194 struct packed_git
*p
= xmalloc(st_add(sizeof(*p
), extra
));
195 memset(p
, 0, sizeof(*p
));
200 struct packed_git
*parse_pack_index(unsigned char *sha1
, const char *idx_path
)
202 const char *path
= sha1_pack_name(sha1
);
203 size_t alloc
= st_add(strlen(path
), 1);
204 struct packed_git
*p
= alloc_packed_git(alloc
);
206 memcpy(p
->pack_name
, path
, alloc
); /* includes NUL */
207 hashcpy(p
->sha1
, sha1
);
208 if (check_packed_git_idx(idx_path
, p
)) {
216 static void scan_windows(struct packed_git
*p
,
217 struct packed_git
**lru_p
,
218 struct pack_window
**lru_w
,
219 struct pack_window
**lru_l
)
221 struct pack_window
*w
, *w_l
;
223 for (w_l
= NULL
, w
= p
->windows
; w
; w
= w
->next
) {
225 if (!*lru_w
|| w
->last_used
< (*lru_w
)->last_used
) {
235 static int unuse_one_window(struct packed_git
*current
)
237 struct packed_git
*p
, *lru_p
= NULL
;
238 struct pack_window
*lru_w
= NULL
, *lru_l
= NULL
;
241 scan_windows(current
, &lru_p
, &lru_w
, &lru_l
);
242 for (p
= packed_git
; p
; p
= p
->next
)
243 scan_windows(p
, &lru_p
, &lru_w
, &lru_l
);
245 munmap(lru_w
->base
, lru_w
->len
);
246 pack_mapped
-= lru_w
->len
;
248 lru_l
->next
= lru_w
->next
;
250 lru_p
->windows
= lru_w
->next
;
258 void release_pack_memory(size_t need
)
260 size_t cur
= pack_mapped
;
261 while (need
>= (cur
- pack_mapped
) && unuse_one_window(NULL
))
265 void close_pack_windows(struct packed_git
*p
)
268 struct pack_window
*w
= p
->windows
;
271 die("pack '%s' still has open windows to it",
273 munmap(w
->base
, w
->len
);
274 pack_mapped
-= w
->len
;
276 p
->windows
= w
->next
;
281 static int close_pack_fd(struct packed_git
*p
)
293 void close_pack_index(struct packed_git
*p
)
296 munmap((void *)p
->index_data
, p
->index_size
);
297 p
->index_data
= NULL
;
301 static void close_pack(struct packed_git
*p
)
303 close_pack_windows(p
);
308 void close_all_packs(void)
310 struct packed_git
*p
;
312 for (p
= packed_git
; p
; p
= p
->next
)
314 die("BUG: want to close pack marked 'do-not-close'");
320 * The LRU pack is the one with the oldest MRU window, preferring packs
321 * with no used windows, or the oldest mtime if it has no windows allocated.
323 static void find_lru_pack(struct packed_git
*p
, struct packed_git
**lru_p
, struct pack_window
**mru_w
, int *accept_windows_inuse
)
325 struct pack_window
*w
, *this_mru_w
;
326 int has_windows_inuse
= 0;
329 * Reject this pack if it has windows and the previously selected
330 * one does not. If this pack does not have windows, reject
331 * it if the pack file is newer than the previously selected one.
333 if (*lru_p
&& !*mru_w
&& (p
->windows
|| p
->mtime
> (*lru_p
)->mtime
))
336 for (w
= this_mru_w
= p
->windows
; w
; w
= w
->next
) {
338 * Reject this pack if any of its windows are in use,
339 * but the previously selected pack did not have any
340 * inuse windows. Otherwise, record that this pack
341 * has windows in use.
344 if (*accept_windows_inuse
)
345 has_windows_inuse
= 1;
350 if (w
->last_used
> this_mru_w
->last_used
)
354 * Reject this pack if it has windows that have been
355 * used more recently than the previously selected pack.
356 * If the previously selected pack had windows inuse and
357 * we have not encountered a window in this pack that is
358 * inuse, skip this check since we prefer a pack with no
359 * inuse windows to one that has inuse windows.
361 if (*mru_w
&& *accept_windows_inuse
== has_windows_inuse
&&
362 this_mru_w
->last_used
> (*mru_w
)->last_used
)
371 *accept_windows_inuse
= has_windows_inuse
;
374 static int close_one_pack(void)
376 struct packed_git
*p
, *lru_p
= NULL
;
377 struct pack_window
*mru_w
= NULL
;
378 int accept_windows_inuse
= 1;
380 for (p
= packed_git
; p
; p
= p
->next
) {
381 if (p
->pack_fd
== -1)
383 find_lru_pack(p
, &lru_p
, &mru_w
, &accept_windows_inuse
);
387 return close_pack_fd(lru_p
);
392 static unsigned int get_max_fd_limit(void)
398 if (!getrlimit(RLIMIT_NOFILE
, &lim
))
405 long open_max
= sysconf(_SC_OPEN_MAX
);
409 * Otherwise, we got -1 for one of the two
412 * (1) sysconf() did not understand _SC_OPEN_MAX
413 * and signaled an error with -1; or
414 * (2) sysconf() said there is no limit.
416 * We _could_ clear errno before calling sysconf() to
417 * tell these two cases apart and return a huge number
418 * in the latter case to let the caller cap it to a
419 * value that is not so selfish, but letting the
420 * fallback OPEN_MAX codepath take care of these cases
429 return 1; /* see the caller ;-) */
434 * Do not call this directly as this leaks p->pack_fd on error return;
435 * call open_packed_git() instead.
437 static int open_packed_git_1(struct packed_git
*p
)
440 struct pack_header hdr
;
441 unsigned char sha1
[20];
442 unsigned char *idx_sha1
;
445 if (!p
->index_data
&& open_pack_index(p
))
446 return error("packfile %s index unavailable", p
->pack_name
);
449 unsigned int max_fds
= get_max_fd_limit();
451 /* Save 3 for stdin/stdout/stderr, 22 for work */
453 pack_max_fds
= max_fds
- 25;
458 while (pack_max_fds
<= pack_open_fds
&& close_one_pack())
461 p
->pack_fd
= git_open(p
->pack_name
);
462 if (p
->pack_fd
< 0 || fstat(p
->pack_fd
, &st
))
466 /* If we created the struct before we had the pack we lack size. */
468 if (!S_ISREG(st
.st_mode
))
469 return error("packfile %s not a regular file", p
->pack_name
);
470 p
->pack_size
= st
.st_size
;
471 } else if (p
->pack_size
!= st
.st_size
)
472 return error("packfile %s size changed", p
->pack_name
);
474 /* We leave these file descriptors open with sliding mmap;
475 * there is no point keeping them open across exec(), though.
477 fd_flag
= fcntl(p
->pack_fd
, F_GETFD
, 0);
479 return error("cannot determine file descriptor flags");
480 fd_flag
|= FD_CLOEXEC
;
481 if (fcntl(p
->pack_fd
, F_SETFD
, fd_flag
) == -1)
482 return error("cannot set FD_CLOEXEC");
484 /* Verify we recognize this pack file format. */
485 if (read_in_full(p
->pack_fd
, &hdr
, sizeof(hdr
)) != sizeof(hdr
))
486 return error("file %s is far too short to be a packfile", p
->pack_name
);
487 if (hdr
.hdr_signature
!= htonl(PACK_SIGNATURE
))
488 return error("file %s is not a GIT packfile", p
->pack_name
);
489 if (!pack_version_ok(hdr
.hdr_version
))
490 return error("packfile %s is version %"PRIu32
" and not"
491 " supported (try upgrading GIT to a newer version)",
492 p
->pack_name
, ntohl(hdr
.hdr_version
));
494 /* Verify the pack matches its index. */
495 if (p
->num_objects
!= ntohl(hdr
.hdr_entries
))
496 return error("packfile %s claims to have %"PRIu32
" objects"
497 " while index indicates %"PRIu32
" objects",
498 p
->pack_name
, ntohl(hdr
.hdr_entries
),
500 if (lseek(p
->pack_fd
, p
->pack_size
- sizeof(sha1
), SEEK_SET
) == -1)
501 return error("end of packfile %s is unavailable", p
->pack_name
);
502 if (read_in_full(p
->pack_fd
, sha1
, sizeof(sha1
)) != sizeof(sha1
))
503 return error("packfile %s signature is unavailable", p
->pack_name
);
504 idx_sha1
= ((unsigned char *)p
->index_data
) + p
->index_size
- 40;
505 if (hashcmp(sha1
, idx_sha1
))
506 return error("packfile %s does not match index", p
->pack_name
);
510 int open_packed_git(struct packed_git
*p
)
512 if (!open_packed_git_1(p
))
518 static int in_window(struct pack_window
*win
, off_t offset
)
520 /* We must promise at least 20 bytes (one hash) after the
521 * offset is available from this window, otherwise the offset
522 * is not actually in this window and a different window (which
523 * has that one hash excess) must be used. This is to support
524 * the object header and delta base parsing routines below.
526 off_t win_off
= win
->offset
;
527 return win_off
<= offset
528 && (offset
+ 20) <= (win_off
+ win
->len
);
531 unsigned char *use_pack(struct packed_git
*p
,
532 struct pack_window
**w_cursor
,
536 struct pack_window
*win
= *w_cursor
;
538 /* Since packfiles end in a hash of their content and it's
539 * pointless to ask for an offset into the middle of that
540 * hash, and the in_window function above wouldn't match
541 * don't allow an offset too close to the end of the file.
543 if (!p
->pack_size
&& p
->pack_fd
== -1 && open_packed_git(p
))
544 die("packfile %s cannot be accessed", p
->pack_name
);
545 if (offset
> (p
->pack_size
- 20))
546 die("offset beyond end of packfile (truncated pack?)");
548 die(_("offset before end of packfile (broken .idx?)"));
550 if (!win
|| !in_window(win
, offset
)) {
553 for (win
= p
->windows
; win
; win
= win
->next
) {
554 if (in_window(win
, offset
))
558 size_t window_align
= packed_git_window_size
/ 2;
561 if (p
->pack_fd
== -1 && open_packed_git(p
))
562 die("packfile %s cannot be accessed", p
->pack_name
);
564 win
= xcalloc(1, sizeof(*win
));
565 win
->offset
= (offset
/ window_align
) * window_align
;
566 len
= p
->pack_size
- win
->offset
;
567 if (len
> packed_git_window_size
)
568 len
= packed_git_window_size
;
569 win
->len
= (size_t)len
;
570 pack_mapped
+= win
->len
;
571 while (packed_git_limit
< pack_mapped
572 && unuse_one_window(p
))
574 win
->base
= xmmap(NULL
, win
->len
,
575 PROT_READ
, MAP_PRIVATE
,
576 p
->pack_fd
, win
->offset
);
577 if (win
->base
== MAP_FAILED
)
578 die_errno("packfile %s cannot be mapped",
580 if (!win
->offset
&& win
->len
== p
->pack_size
585 if (pack_mapped
> peak_pack_mapped
)
586 peak_pack_mapped
= pack_mapped
;
587 if (pack_open_windows
> peak_pack_open_windows
)
588 peak_pack_open_windows
= pack_open_windows
;
589 win
->next
= p
->windows
;
593 if (win
!= *w_cursor
) {
594 win
->last_used
= pack_used_ctr
++;
598 offset
-= win
->offset
;
600 *left
= win
->len
- xsize_t(offset
);
601 return win
->base
+ offset
;
604 void unuse_pack(struct pack_window
**w_cursor
)
606 struct pack_window
*w
= *w_cursor
;
613 static void try_to_free_pack_memory(size_t size
)
615 release_pack_memory(size
);
618 struct packed_git
*add_packed_git(const char *path
, size_t path_len
, int local
)
620 static int have_set_try_to_free_routine
;
623 struct packed_git
*p
;
625 if (!have_set_try_to_free_routine
) {
626 have_set_try_to_free_routine
= 1;
627 set_try_to_free_routine(try_to_free_pack_memory
);
631 * Make sure a corresponding .pack file exists and that
632 * the index looks sane.
634 if (!strip_suffix_mem(path
, &path_len
, ".idx"))
638 * ".pack" is long enough to hold any suffix we're adding (and
639 * the use xsnprintf double-checks that)
641 alloc
= st_add3(path_len
, strlen(".pack"), 1);
642 p
= alloc_packed_git(alloc
);
643 memcpy(p
->pack_name
, path
, path_len
);
645 xsnprintf(p
->pack_name
+ path_len
, alloc
- path_len
, ".keep");
646 if (!access(p
->pack_name
, F_OK
))
649 xsnprintf(p
->pack_name
+ path_len
, alloc
- path_len
, ".pack");
650 if (stat(p
->pack_name
, &st
) || !S_ISREG(st
.st_mode
)) {
655 /* ok, it looks sane as far as we can check without
656 * actually mapping the pack file.
658 p
->pack_size
= st
.st_size
;
659 p
->pack_local
= local
;
660 p
->mtime
= st
.st_mtime
;
661 if (path_len
< 40 || get_sha1_hex(path
+ path_len
- 40, p
->sha1
))
666 void install_packed_git(struct packed_git
*pack
)
668 if (pack
->pack_fd
!= -1)
671 pack
->next
= packed_git
;
675 void (*report_garbage
)(unsigned seen_bits
, const char *path
);
677 static void report_helper(const struct string_list
*list
,
678 int seen_bits
, int first
, int last
)
680 if (seen_bits
== (PACKDIR_FILE_PACK
|PACKDIR_FILE_IDX
))
683 for (; first
< last
; first
++)
684 report_garbage(seen_bits
, list
->items
[first
].string
);
687 static void report_pack_garbage(struct string_list
*list
)
689 int i
, baselen
= -1, first
= 0, seen_bits
= 0;
694 string_list_sort(list
);
696 for (i
= 0; i
< list
->nr
; i
++) {
697 const char *path
= list
->items
[i
].string
;
699 strncmp(path
, list
->items
[first
].string
, baselen
)) {
700 report_helper(list
, seen_bits
, first
, i
);
705 const char *dot
= strrchr(path
, '.');
707 report_garbage(PACKDIR_FILE_GARBAGE
, path
);
710 baselen
= dot
- path
+ 1;
713 if (!strcmp(path
+ baselen
, "pack"))
715 else if (!strcmp(path
+ baselen
, "idx"))
718 report_helper(list
, seen_bits
, first
, list
->nr
);
721 static void prepare_packed_git_one(char *objdir
, int local
)
723 struct strbuf path
= STRBUF_INIT
;
727 struct string_list garbage
= STRING_LIST_INIT_DUP
;
729 strbuf_addstr(&path
, objdir
);
730 strbuf_addstr(&path
, "/pack");
731 dir
= opendir(path
.buf
);
734 error_errno("unable to open object pack directory: %s",
736 strbuf_release(&path
);
739 strbuf_addch(&path
, '/');
740 dirnamelen
= path
.len
;
741 while ((de
= readdir(dir
)) != NULL
) {
742 struct packed_git
*p
;
745 if (is_dot_or_dotdot(de
->d_name
))
748 strbuf_setlen(&path
, dirnamelen
);
749 strbuf_addstr(&path
, de
->d_name
);
752 if (strip_suffix_mem(path
.buf
, &base_len
, ".idx")) {
753 /* Don't reopen a pack we already have. */
754 for (p
= packed_git
; p
; p
= p
->next
) {
756 if (strip_suffix(p
->pack_name
, ".pack", &len
) &&
758 !memcmp(p
->pack_name
, path
.buf
, len
))
763 * See if it really is a valid .idx file with
764 * corresponding .pack file that we can map.
766 (p
= add_packed_git(path
.buf
, path
.len
, local
)) != NULL
)
767 install_packed_git(p
);
773 if (ends_with(de
->d_name
, ".idx") ||
774 ends_with(de
->d_name
, ".pack") ||
775 ends_with(de
->d_name
, ".bitmap") ||
776 ends_with(de
->d_name
, ".keep"))
777 string_list_append(&garbage
, path
.buf
);
779 report_garbage(PACKDIR_FILE_GARBAGE
, path
.buf
);
782 report_pack_garbage(&garbage
);
783 string_list_clear(&garbage
, 0);
784 strbuf_release(&path
);
787 static int approximate_object_count_valid
;
790 * Give a fast, rough count of the number of objects in the repository. This
791 * ignores loose objects completely. If you have a lot of them, then either
792 * you should repack because your performance will be awful, or they are
793 * all unreachable objects about to be pruned, in which case they're not really
794 * interesting as a measure of repo size in the first place.
796 unsigned long approximate_object_count(void)
798 static unsigned long count
;
799 if (!approximate_object_count_valid
) {
800 struct packed_git
*p
;
802 prepare_packed_git();
804 for (p
= packed_git
; p
; p
= p
->next
) {
805 if (open_pack_index(p
))
807 count
+= p
->num_objects
;
813 static void *get_next_packed_git(const void *p
)
815 return ((const struct packed_git
*)p
)->next
;
818 static void set_next_packed_git(void *p
, void *next
)
820 ((struct packed_git
*)p
)->next
= next
;
823 static int sort_pack(const void *a_
, const void *b_
)
825 const struct packed_git
*a
= a_
;
826 const struct packed_git
*b
= b_
;
830 * Local packs tend to contain objects specific to our
831 * variant of the project than remote ones. In addition,
832 * remote ones could be on a network mounted filesystem.
833 * Favor local ones for these reasons.
835 st
= a
->pack_local
- b
->pack_local
;
840 * Younger packs tend to contain more recent objects,
841 * and more recent objects tend to get accessed more
844 if (a
->mtime
< b
->mtime
)
846 else if (a
->mtime
== b
->mtime
)
851 static void rearrange_packed_git(void)
853 packed_git
= llist_mergesort(packed_git
, get_next_packed_git
,
854 set_next_packed_git
, sort_pack
);
857 static void prepare_packed_git_mru(void)
859 struct packed_git
*p
;
861 mru_clear(packed_git_mru
);
862 for (p
= packed_git
; p
; p
= p
->next
)
863 mru_append(packed_git_mru
, p
);
866 static int prepare_packed_git_run_once
= 0;
867 void prepare_packed_git(void)
869 struct alternate_object_database
*alt
;
871 if (prepare_packed_git_run_once
)
873 prepare_packed_git_one(get_object_directory(), 1);
875 for (alt
= alt_odb_list
; alt
; alt
= alt
->next
)
876 prepare_packed_git_one(alt
->path
, 0);
877 rearrange_packed_git();
878 prepare_packed_git_mru();
879 prepare_packed_git_run_once
= 1;
882 void reprepare_packed_git(void)
884 approximate_object_count_valid
= 0;
885 prepare_packed_git_run_once
= 0;
886 prepare_packed_git();
889 unsigned long unpack_object_header_buffer(const unsigned char *buf
,
890 unsigned long len
, enum object_type
*type
, unsigned long *sizep
)
893 unsigned long size
, c
;
894 unsigned long used
= 0;
897 *type
= (c
>> 4) & 7;
901 if (len
<= used
|| bitsizeof(long) <= shift
) {
902 error("bad object header");
907 size
+= (c
& 0x7f) << shift
;
914 unsigned long get_size_from_delta(struct packed_git
*p
,
915 struct pack_window
**w_curs
,
918 const unsigned char *data
;
919 unsigned char delta_head
[20], *in
;
923 memset(&stream
, 0, sizeof(stream
));
924 stream
.next_out
= delta_head
;
925 stream
.avail_out
= sizeof(delta_head
);
927 git_inflate_init(&stream
);
929 in
= use_pack(p
, w_curs
, curpos
, &stream
.avail_in
);
931 st
= git_inflate(&stream
, Z_FINISH
);
932 curpos
+= stream
.next_in
- in
;
933 } while ((st
== Z_OK
|| st
== Z_BUF_ERROR
) &&
934 stream
.total_out
< sizeof(delta_head
));
935 git_inflate_end(&stream
);
936 if ((st
!= Z_STREAM_END
) && stream
.total_out
!= sizeof(delta_head
)) {
937 error("delta data unpack-initial failed");
941 /* Examine the initial part of the delta to figure out
946 /* ignore base size */
947 get_delta_hdr_size(&data
, delta_head
+sizeof(delta_head
));
949 /* Read the result size */
950 return get_delta_hdr_size(&data
, delta_head
+sizeof(delta_head
));
953 int unpack_object_header(struct packed_git
*p
,
954 struct pack_window
**w_curs
,
956 unsigned long *sizep
)
961 enum object_type type
;
963 /* use_pack() assures us we have [base, base + 20) available
964 * as a range that we can look at. (Its actually the hash
965 * size that is assured.) With our object header encoding
966 * the maximum deflated object size is 2^137, which is just
967 * insane, so we know won't exceed what we have been given.
969 base
= use_pack(p
, w_curs
, *curpos
, &left
);
970 used
= unpack_object_header_buffer(base
, left
, &type
, sizep
);