2 * GIT - The information manager from hell
4 * Copyright (C) Linus Torvalds, 2005
6 * This handles basic git sha1 object files - packing, unpacking,
17 #include "pack-revindex.h"
18 #include "sha1-lookup.h"
23 #if defined(__linux__) && (defined(__i386__) || defined(__PPC__))
24 #define O_NOATIME 01000000
32 static unsigned long sz_fmt(size_t s
) { return (unsigned long)s
; }
35 static size_t sz_fmt(size_t s
) { return s
; }
38 const unsigned char null_sha1
[20];
40 const signed char hexval_table
[256] = {
41 -1, -1, -1, -1, -1, -1, -1, -1, /* 00-07 */
42 -1, -1, -1, -1, -1, -1, -1, -1, /* 08-0f */
43 -1, -1, -1, -1, -1, -1, -1, -1, /* 10-17 */
44 -1, -1, -1, -1, -1, -1, -1, -1, /* 18-1f */
45 -1, -1, -1, -1, -1, -1, -1, -1, /* 20-27 */
46 -1, -1, -1, -1, -1, -1, -1, -1, /* 28-2f */
47 0, 1, 2, 3, 4, 5, 6, 7, /* 30-37 */
48 8, 9, -1, -1, -1, -1, -1, -1, /* 38-3f */
49 -1, 10, 11, 12, 13, 14, 15, -1, /* 40-47 */
50 -1, -1, -1, -1, -1, -1, -1, -1, /* 48-4f */
51 -1, -1, -1, -1, -1, -1, -1, -1, /* 50-57 */
52 -1, -1, -1, -1, -1, -1, -1, -1, /* 58-5f */
53 -1, 10, 11, 12, 13, 14, 15, -1, /* 60-67 */
54 -1, -1, -1, -1, -1, -1, -1, -1, /* 68-67 */
55 -1, -1, -1, -1, -1, -1, -1, -1, /* 70-77 */
56 -1, -1, -1, -1, -1, -1, -1, -1, /* 78-7f */
57 -1, -1, -1, -1, -1, -1, -1, -1, /* 80-87 */
58 -1, -1, -1, -1, -1, -1, -1, -1, /* 88-8f */
59 -1, -1, -1, -1, -1, -1, -1, -1, /* 90-97 */
60 -1, -1, -1, -1, -1, -1, -1, -1, /* 98-9f */
61 -1, -1, -1, -1, -1, -1, -1, -1, /* a0-a7 */
62 -1, -1, -1, -1, -1, -1, -1, -1, /* a8-af */
63 -1, -1, -1, -1, -1, -1, -1, -1, /* b0-b7 */
64 -1, -1, -1, -1, -1, -1, -1, -1, /* b8-bf */
65 -1, -1, -1, -1, -1, -1, -1, -1, /* c0-c7 */
66 -1, -1, -1, -1, -1, -1, -1, -1, /* c8-cf */
67 -1, -1, -1, -1, -1, -1, -1, -1, /* d0-d7 */
68 -1, -1, -1, -1, -1, -1, -1, -1, /* d8-df */
69 -1, -1, -1, -1, -1, -1, -1, -1, /* e0-e7 */
70 -1, -1, -1, -1, -1, -1, -1, -1, /* e8-ef */
71 -1, -1, -1, -1, -1, -1, -1, -1, /* f0-f7 */
72 -1, -1, -1, -1, -1, -1, -1, -1, /* f8-ff */
75 int get_sha1_hex(const char *hex
, unsigned char *sha1
)
78 for (i
= 0; i
< 20; i
++) {
79 unsigned int val
= (hexval(hex
[0]) << 4) | hexval(hex
[1]);
88 static inline int offset_1st_component(const char *path
)
90 if (has_dos_drive_prefix(path
))
91 return 2 + (path
[2] == '/');
95 int safe_create_leading_directories(char *path
)
97 char *pos
= path
+ offset_1st_component(path
);
101 pos
= strchr(pos
, '/');
105 if (!stat(path
, &st
)) {
107 if (!S_ISDIR(st
.st_mode
)) {
112 else if (mkdir(path
, 0777)) {
116 else if (adjust_shared_perm(path
)) {
125 int safe_create_leading_directories_const(const char *path
)
127 /* path points to cache entries, so xstrdup before messing with it */
128 char *buf
= xstrdup(path
);
129 int result
= safe_create_leading_directories(buf
);
134 char *sha1_to_hex(const unsigned char *sha1
)
137 static char hexbuffer
[4][50];
138 static const char hex
[] = "0123456789abcdef";
139 char *buffer
= hexbuffer
[3 & ++bufno
], *buf
= buffer
;
142 for (i
= 0; i
< 20; i
++) {
143 unsigned int val
= *sha1
++;
144 *buf
++ = hex
[val
>> 4];
145 *buf
++ = hex
[val
& 0xf];
152 static void fill_sha1_path(char *pathbuf
, const unsigned char *sha1
)
155 for (i
= 0; i
< 20; i
++) {
156 static char hex
[] = "0123456789abcdef";
157 unsigned int val
= sha1
[i
];
158 char *pos
= pathbuf
+ i
*2 + (i
> 0);
159 *pos
++ = hex
[val
>> 4];
160 *pos
= hex
[val
& 0xf];
165 * NOTE! This returns a statically allocated buffer, so you have to be
166 * careful about using it. Do an "xstrdup()" if you need to save the
169 * Also note that this returns the location for creating. Reading
170 * SHA1 file can happen from any alternate directory listed in the
171 * DB_ENVIRONMENT environment variable if it is not found in
172 * the primary object database.
174 char *sha1_file_name(const unsigned char *sha1
)
176 static char *name
, *base
;
179 const char *sha1_file_directory
= get_object_directory();
180 int len
= strlen(sha1_file_directory
);
181 base
= xmalloc(len
+ 60);
182 memcpy(base
, sha1_file_directory
, len
);
183 memset(base
+len
, 0, 60);
186 name
= base
+ len
+ 1;
188 fill_sha1_path(name
, sha1
);
192 static char *sha1_get_pack_name(const unsigned char *sha1
,
193 char **name
, char **base
, const char *which
)
195 static const char hex
[] = "0123456789abcdef";
200 const char *sha1_file_directory
= get_object_directory();
201 int len
= strlen(sha1_file_directory
);
202 *base
= xmalloc(len
+ 60);
203 sprintf(*base
, "%s/pack/pack-1234567890123456789012345678901234567890.%s",
204 sha1_file_directory
, which
);
205 *name
= *base
+ len
+ 11;
210 for (i
= 0; i
< 20; i
++) {
211 unsigned int val
= *sha1
++;
212 *buf
++ = hex
[val
>> 4];
213 *buf
++ = hex
[val
& 0xf];
219 char *sha1_pack_name(const unsigned char *sha1
)
221 static char *name
, *base
;
223 return sha1_get_pack_name(sha1
, &name
, &base
, "pack");
226 char *sha1_pack_index_name(const unsigned char *sha1
)
228 static char *name
, *base
;
230 return sha1_get_pack_name(sha1
, &name
, &base
, "idx");
233 struct alternate_object_database
*alt_odb_list
;
234 static struct alternate_object_database
**alt_odb_tail
;
236 static void read_info_alternates(const char * alternates
, int depth
);
239 * Prepare alternate object database registry.
241 * The variable alt_odb_list points at the list of struct
242 * alternate_object_database. The elements on this list come from
243 * non-empty elements from colon separated ALTERNATE_DB_ENVIRONMENT
244 * environment variable, and $GIT_OBJECT_DIRECTORY/info/alternates,
245 * whose contents is similar to that environment variable but can be
246 * LF separated. Its base points at a statically allocated buffer that
247 * contains "/the/directory/corresponding/to/.git/objects/...", while
248 * its name points just after the slash at the end of ".git/objects/"
249 * in the example above, and has enough space to hold 40-byte hex
250 * SHA1, an extra slash for the first level indirection, and the
253 static int link_alt_odb_entry(const char * entry
, int len
, const char * relative_base
, int depth
)
256 const char *objdir
= get_object_directory();
257 struct alternate_object_database
*ent
;
258 struct alternate_object_database
*alt
;
259 /* 43 = 40-byte + 2 '/' + terminating NUL */
261 int entlen
= pfxlen
+ 43;
264 if (!is_absolute_path(entry
) && relative_base
) {
265 /* Relative alt-odb */
267 base_len
= strlen(relative_base
) + 1;
271 ent
= xmalloc(sizeof(*ent
) + entlen
);
273 if (!is_absolute_path(entry
) && relative_base
) {
274 memcpy(ent
->base
, relative_base
, base_len
- 1);
275 ent
->base
[base_len
- 1] = '/';
276 memcpy(ent
->base
+ base_len
, entry
, len
);
279 memcpy(ent
->base
, entry
, pfxlen
);
281 ent
->name
= ent
->base
+ pfxlen
+ 1;
282 ent
->base
[pfxlen
+ 3] = '/';
283 ent
->base
[pfxlen
] = ent
->base
[entlen
-1] = 0;
285 /* Detect cases where alternate disappeared */
286 if (stat(ent
->base
, &st
) || !S_ISDIR(st
.st_mode
)) {
287 error("object directory %s does not exist; "
288 "check .git/objects/info/alternates.",
294 /* Prevent the common mistake of listing the same
295 * thing twice, or object directory itself.
297 for (alt
= alt_odb_list
; alt
; alt
= alt
->next
) {
298 if (!memcmp(ent
->base
, alt
->base
, pfxlen
)) {
303 if (!memcmp(ent
->base
, objdir
, pfxlen
)) {
308 /* add the alternate entry */
310 alt_odb_tail
= &(ent
->next
);
313 /* recursively add alternates */
314 read_info_alternates(ent
->base
, depth
+ 1);
316 ent
->base
[pfxlen
] = '/';
321 static void link_alt_odb_entries(const char *alt
, const char *ep
, int sep
,
322 const char *relative_base
, int depth
)
324 const char *cp
, *last
;
327 error("%s: ignoring alternate object stores, nesting too deep.",
335 if (cp
< ep
&& *cp
== '#') {
336 while (cp
< ep
&& *cp
!= sep
)
341 while (cp
< ep
&& *cp
!= sep
)
344 if (!is_absolute_path(last
) && depth
) {
345 error("%s: ignoring relative alternate object store %s",
346 relative_base
, last
);
348 link_alt_odb_entry(last
, cp
- last
,
349 relative_base
, depth
);
352 while (cp
< ep
&& *cp
== sep
)
358 static void read_info_alternates(const char * relative_base
, int depth
)
363 const char alt_file_name
[] = "info/alternates";
364 /* Given that relative_base is no longer than PATH_MAX,
365 ensure that "path" has enough space to append "/", the
366 file name, "info/alternates", and a trailing NUL. */
367 char path
[PATH_MAX
+ 1 + sizeof alt_file_name
];
370 sprintf(path
, "%s/%s", relative_base
, alt_file_name
);
371 fd
= open(path
, O_RDONLY
);
374 if (fstat(fd
, &st
) || (st
.st_size
== 0)) {
378 mapsz
= xsize_t(st
.st_size
);
379 map
= xmmap(NULL
, mapsz
, PROT_READ
, MAP_PRIVATE
, fd
, 0);
382 link_alt_odb_entries(map
, map
+ mapsz
, '\n', relative_base
, depth
);
387 void add_to_alternates_file(const char *reference
)
389 struct lock_file
*lock
= xcalloc(1, sizeof(struct lock_file
));
390 int fd
= hold_lock_file_for_append(lock
, git_path("objects/info/alternates"), LOCK_DIE_ON_ERROR
);
391 char *alt
= mkpath("%s/objects\n", reference
);
392 write_or_die(fd
, alt
, strlen(alt
));
393 if (commit_lock_file(lock
))
394 die("could not close alternates file");
396 link_alt_odb_entries(alt
, alt
+ strlen(alt
), '\n', NULL
, 0);
399 void prepare_alt_odb(void)
406 alt
= getenv(ALTERNATE_DB_ENVIRONMENT
);
409 alt_odb_tail
= &alt_odb_list
;
410 link_alt_odb_entries(alt
, alt
+ strlen(alt
), PATH_SEP
, NULL
, 0);
412 read_info_alternates(get_object_directory(), 0);
415 static int has_loose_object_local(const unsigned char *sha1
)
417 char *name
= sha1_file_name(sha1
);
418 return !access(name
, F_OK
);
421 int has_loose_object_nonlocal(const unsigned char *sha1
)
423 struct alternate_object_database
*alt
;
425 for (alt
= alt_odb_list
; alt
; alt
= alt
->next
) {
426 fill_sha1_path(alt
->name
, sha1
);
427 if (!access(alt
->base
, F_OK
))
433 static int has_loose_object(const unsigned char *sha1
)
435 return has_loose_object_local(sha1
) ||
436 has_loose_object_nonlocal(sha1
);
439 static unsigned int pack_used_ctr
;
440 static unsigned int pack_mmap_calls
;
441 static unsigned int peak_pack_open_windows
;
442 static unsigned int pack_open_windows
;
443 static size_t peak_pack_mapped
;
444 static size_t pack_mapped
;
445 struct packed_git
*packed_git
;
447 void pack_report(void)
450 "pack_report: getpagesize() = %10" SZ_FMT
"\n"
451 "pack_report: core.packedGitWindowSize = %10" SZ_FMT
"\n"
452 "pack_report: core.packedGitLimit = %10" SZ_FMT
"\n",
453 sz_fmt(getpagesize()),
454 sz_fmt(packed_git_window_size
),
455 sz_fmt(packed_git_limit
));
457 "pack_report: pack_used_ctr = %10u\n"
458 "pack_report: pack_mmap_calls = %10u\n"
459 "pack_report: pack_open_windows = %10u / %10u\n"
460 "pack_report: pack_mapped = "
461 "%10" SZ_FMT
" / %10" SZ_FMT
"\n",
464 pack_open_windows
, peak_pack_open_windows
,
465 sz_fmt(pack_mapped
), sz_fmt(peak_pack_mapped
));
468 static int check_packed_git_idx(const char *path
, struct packed_git
*p
)
471 struct pack_idx_header
*hdr
;
473 uint32_t version
, nr
, i
, *index
;
474 int fd
= open(path
, O_RDONLY
);
479 if (fstat(fd
, &st
)) {
483 idx_size
= xsize_t(st
.st_size
);
484 if (idx_size
< 4 * 256 + 20 + 20) {
486 return error("index file %s is too small", path
);
488 idx_map
= xmmap(NULL
, idx_size
, PROT_READ
, MAP_PRIVATE
, fd
, 0);
492 if (hdr
->idx_signature
== htonl(PACK_IDX_SIGNATURE
)) {
493 version
= ntohl(hdr
->idx_version
);
494 if (version
< 2 || version
> 2) {
495 munmap(idx_map
, idx_size
);
496 return error("index file %s is version %"PRIu32
497 " and is not supported by this binary"
498 " (try upgrading GIT to a newer version)",
507 index
+= 2; /* skip index header */
508 for (i
= 0; i
< 256; i
++) {
509 uint32_t n
= ntohl(index
[i
]);
511 munmap(idx_map
, idx_size
);
512 return error("non-monotonic index %s", path
);
520 * - 256 index entries 4 bytes each
521 * - 24-byte entries * nr (20-byte sha1 + 4-byte offset)
522 * - 20-byte SHA1 of the packfile
523 * - 20-byte SHA1 file checksum
525 if (idx_size
!= 4*256 + nr
* 24 + 20 + 20) {
526 munmap(idx_map
, idx_size
);
527 return error("wrong index v1 file size in %s", path
);
529 } else if (version
== 2) {
532 * - 8 bytes of header
533 * - 256 index entries 4 bytes each
534 * - 20-byte sha1 entry * nr
535 * - 4-byte crc entry * nr
536 * - 4-byte offset entry * nr
537 * - 20-byte SHA1 of the packfile
538 * - 20-byte SHA1 file checksum
539 * And after the 4-byte offset table might be a
540 * variable sized table containing 8-byte entries
541 * for offsets larger than 2^31.
543 unsigned long min_size
= 8 + 4*256 + nr
*(20 + 4 + 4) + 20 + 20;
544 unsigned long max_size
= min_size
;
546 max_size
+= (nr
- 1)*8;
547 if (idx_size
< min_size
|| idx_size
> max_size
) {
548 munmap(idx_map
, idx_size
);
549 return error("wrong index v2 file size in %s", path
);
551 if (idx_size
!= min_size
&&
553 * make sure we can deal with large pack offsets.
554 * 31-bit signed offset won't be enough, neither
555 * 32-bit unsigned one will be.
557 (sizeof(off_t
) <= 4)) {
558 munmap(idx_map
, idx_size
);
559 return error("pack too large for current definition of off_t in %s", path
);
563 p
->index_version
= version
;
564 p
->index_data
= idx_map
;
565 p
->index_size
= idx_size
;
570 int open_pack_index(struct packed_git
*p
)
578 idx_name
= xstrdup(p
->pack_name
);
579 strcpy(idx_name
+ strlen(idx_name
) - strlen(".pack"), ".idx");
580 ret
= check_packed_git_idx(idx_name
, p
);
585 static void scan_windows(struct packed_git
*p
,
586 struct packed_git
**lru_p
,
587 struct pack_window
**lru_w
,
588 struct pack_window
**lru_l
)
590 struct pack_window
*w
, *w_l
;
592 for (w_l
= NULL
, w
= p
->windows
; w
; w
= w
->next
) {
594 if (!*lru_w
|| w
->last_used
< (*lru_w
)->last_used
) {
604 static int unuse_one_window(struct packed_git
*current
, int keep_fd
)
606 struct packed_git
*p
, *lru_p
= NULL
;
607 struct pack_window
*lru_w
= NULL
, *lru_l
= NULL
;
610 scan_windows(current
, &lru_p
, &lru_w
, &lru_l
);
611 for (p
= packed_git
; p
; p
= p
->next
)
612 scan_windows(p
, &lru_p
, &lru_w
, &lru_l
);
614 munmap(lru_w
->base
, lru_w
->len
);
615 pack_mapped
-= lru_w
->len
;
617 lru_l
->next
= lru_w
->next
;
619 lru_p
->windows
= lru_w
->next
;
620 if (!lru_p
->windows
&& lru_p
->pack_fd
!= keep_fd
) {
621 close(lru_p
->pack_fd
);
632 void release_pack_memory(size_t need
, int fd
)
634 size_t cur
= pack_mapped
;
635 while (need
>= (cur
- pack_mapped
) && unuse_one_window(NULL
, fd
))
639 void close_pack_windows(struct packed_git
*p
)
642 struct pack_window
*w
= p
->windows
;
645 die("pack '%s' still has open windows to it",
647 munmap(w
->base
, w
->len
);
648 pack_mapped
-= w
->len
;
650 p
->windows
= w
->next
;
655 void unuse_pack(struct pack_window
**w_cursor
)
657 struct pack_window
*w
= *w_cursor
;
665 * This is used by git-repack in case a newly created pack happens to
666 * contain the same set of objects as an existing one. In that case
667 * the resulting file might be different even if its name would be the
668 * same. It is best to close any reference to the old pack before it is
669 * replaced on disk. Of course no index pointers nor windows for given pack
670 * must subsist at this point. If ever objects from this pack are requested
671 * again, the new version of the pack will be reinitialized through
672 * reprepare_packed_git().
674 void free_pack_by_name(const char *pack_name
)
676 struct packed_git
*p
, **pp
= &packed_git
;
680 if (strcmp(pack_name
, p
->pack_name
) == 0) {
681 close_pack_windows(p
);
682 if (p
->pack_fd
!= -1)
685 munmap((void *)p
->index_data
, p
->index_size
);
686 free(p
->bad_object_sha1
);
696 * Do not call this directly as this leaks p->pack_fd on error return;
697 * call open_packed_git() instead.
699 static int open_packed_git_1(struct packed_git
*p
)
702 struct pack_header hdr
;
703 unsigned char sha1
[20];
704 unsigned char *idx_sha1
;
707 if (!p
->index_data
&& open_pack_index(p
))
708 return error("packfile %s index unavailable", p
->pack_name
);
710 p
->pack_fd
= open(p
->pack_name
, O_RDONLY
);
711 if (p
->pack_fd
< 0 || fstat(p
->pack_fd
, &st
))
714 /* If we created the struct before we had the pack we lack size. */
716 if (!S_ISREG(st
.st_mode
))
717 return error("packfile %s not a regular file", p
->pack_name
);
718 p
->pack_size
= st
.st_size
;
719 } else if (p
->pack_size
!= st
.st_size
)
720 return error("packfile %s size changed", p
->pack_name
);
722 /* We leave these file descriptors open with sliding mmap;
723 * there is no point keeping them open across exec(), though.
725 fd_flag
= fcntl(p
->pack_fd
, F_GETFD
, 0);
727 return error("cannot determine file descriptor flags");
728 fd_flag
|= FD_CLOEXEC
;
729 if (fcntl(p
->pack_fd
, F_SETFD
, fd_flag
) == -1)
730 return error("cannot set FD_CLOEXEC");
732 /* Verify we recognize this pack file format. */
733 if (read_in_full(p
->pack_fd
, &hdr
, sizeof(hdr
)) != sizeof(hdr
))
734 return error("file %s is far too short to be a packfile", p
->pack_name
);
735 if (hdr
.hdr_signature
!= htonl(PACK_SIGNATURE
))
736 return error("file %s is not a GIT packfile", p
->pack_name
);
737 if (!pack_version_ok(hdr
.hdr_version
))
738 return error("packfile %s is version %"PRIu32
" and not"
739 " supported (try upgrading GIT to a newer version)",
740 p
->pack_name
, ntohl(hdr
.hdr_version
));
742 /* Verify the pack matches its index. */
743 if (p
->num_objects
!= ntohl(hdr
.hdr_entries
))
744 return error("packfile %s claims to have %"PRIu32
" objects"
745 " while index indicates %"PRIu32
" objects",
746 p
->pack_name
, ntohl(hdr
.hdr_entries
),
748 if (lseek(p
->pack_fd
, p
->pack_size
- sizeof(sha1
), SEEK_SET
) == -1)
749 return error("end of packfile %s is unavailable", p
->pack_name
);
750 if (read_in_full(p
->pack_fd
, sha1
, sizeof(sha1
)) != sizeof(sha1
))
751 return error("packfile %s signature is unavailable", p
->pack_name
);
752 idx_sha1
= ((unsigned char *)p
->index_data
) + p
->index_size
- 40;
753 if (hashcmp(sha1
, idx_sha1
))
754 return error("packfile %s does not match index", p
->pack_name
);
758 static int open_packed_git(struct packed_git
*p
)
760 if (!open_packed_git_1(p
))
762 if (p
->pack_fd
!= -1) {
769 static int in_window(struct pack_window
*win
, off_t offset
)
771 /* We must promise at least 20 bytes (one hash) after the
772 * offset is available from this window, otherwise the offset
773 * is not actually in this window and a different window (which
774 * has that one hash excess) must be used. This is to support
775 * the object header and delta base parsing routines below.
777 off_t win_off
= win
->offset
;
778 return win_off
<= offset
779 && (offset
+ 20) <= (win_off
+ win
->len
);
782 unsigned char* use_pack(struct packed_git
*p
,
783 struct pack_window
**w_cursor
,
787 struct pack_window
*win
= *w_cursor
;
789 if (p
->pack_fd
== -1 && open_packed_git(p
))
790 die("packfile %s cannot be accessed", p
->pack_name
);
792 /* Since packfiles end in a hash of their content and its
793 * pointless to ask for an offset into the middle of that
794 * hash, and the in_window function above wouldn't match
795 * don't allow an offset too close to the end of the file.
797 if (offset
> (p
->pack_size
- 20))
798 die("offset beyond end of packfile (truncated pack?)");
800 if (!win
|| !in_window(win
, offset
)) {
803 for (win
= p
->windows
; win
; win
= win
->next
) {
804 if (in_window(win
, offset
))
808 size_t window_align
= packed_git_window_size
/ 2;
810 win
= xcalloc(1, sizeof(*win
));
811 win
->offset
= (offset
/ window_align
) * window_align
;
812 len
= p
->pack_size
- win
->offset
;
813 if (len
> packed_git_window_size
)
814 len
= packed_git_window_size
;
815 win
->len
= (size_t)len
;
816 pack_mapped
+= win
->len
;
817 while (packed_git_limit
< pack_mapped
818 && unuse_one_window(p
, p
->pack_fd
))
820 win
->base
= xmmap(NULL
, win
->len
,
821 PROT_READ
, MAP_PRIVATE
,
822 p
->pack_fd
, win
->offset
);
823 if (win
->base
== MAP_FAILED
)
824 die("packfile %s cannot be mapped: %s",
829 if (pack_mapped
> peak_pack_mapped
)
830 peak_pack_mapped
= pack_mapped
;
831 if (pack_open_windows
> peak_pack_open_windows
)
832 peak_pack_open_windows
= pack_open_windows
;
833 win
->next
= p
->windows
;
837 if (win
!= *w_cursor
) {
838 win
->last_used
= pack_used_ctr
++;
842 offset
-= win
->offset
;
844 *left
= win
->len
- xsize_t(offset
);
845 return win
->base
+ offset
;
848 static struct packed_git
*alloc_packed_git(int extra
)
850 struct packed_git
*p
= xmalloc(sizeof(*p
) + extra
);
851 memset(p
, 0, sizeof(*p
));
856 struct packed_git
*add_packed_git(const char *path
, int path_len
, int local
)
859 struct packed_git
*p
= alloc_packed_git(path_len
+ 2);
862 * Make sure a corresponding .pack file exists and that
863 * the index looks sane.
865 path_len
-= strlen(".idx");
870 memcpy(p
->pack_name
, path
, path_len
);
872 strcpy(p
->pack_name
+ path_len
, ".keep");
873 if (!access(p
->pack_name
, F_OK
))
876 strcpy(p
->pack_name
+ path_len
, ".pack");
877 if (stat(p
->pack_name
, &st
) || !S_ISREG(st
.st_mode
)) {
882 /* ok, it looks sane as far as we can check without
883 * actually mapping the pack file.
885 p
->pack_size
= st
.st_size
;
886 p
->pack_local
= local
;
887 p
->mtime
= st
.st_mtime
;
888 if (path_len
< 40 || get_sha1_hex(path
+ path_len
- 40, p
->sha1
))
893 struct packed_git
*parse_pack_index(unsigned char *sha1
)
895 const char *idx_path
= sha1_pack_index_name(sha1
);
896 const char *path
= sha1_pack_name(sha1
);
897 struct packed_git
*p
= alloc_packed_git(strlen(path
) + 1);
899 strcpy(p
->pack_name
, path
);
900 hashcpy(p
->sha1
, sha1
);
901 if (check_packed_git_idx(idx_path
, p
)) {
909 void install_packed_git(struct packed_git
*pack
)
911 pack
->next
= packed_git
;
915 static void prepare_packed_git_one(char *objdir
, int local
)
917 /* Ensure that this buffer is large enough so that we can
918 append "/pack/" without clobbering the stack even if
919 strlen(objdir) were PATH_MAX. */
920 char path
[PATH_MAX
+ 1 + 4 + 1 + 1];
925 sprintf(path
, "%s/pack", objdir
);
930 error("unable to open object pack directory: %s: %s",
931 path
, strerror(errno
));
935 while ((de
= readdir(dir
)) != NULL
) {
936 int namelen
= strlen(de
->d_name
);
937 struct packed_git
*p
;
939 if (!has_extension(de
->d_name
, ".idx"))
942 if (len
+ namelen
+ 1 > sizeof(path
))
945 /* Don't reopen a pack we already have. */
946 strcpy(path
+ len
, de
->d_name
);
947 for (p
= packed_git
; p
; p
= p
->next
) {
948 if (!memcmp(path
, p
->pack_name
, len
+ namelen
- 4))
953 /* See if it really is a valid .idx file with corresponding
954 * .pack file that we can map.
956 p
= add_packed_git(path
, len
+ namelen
, local
);
959 install_packed_git(p
);
964 static int sort_pack(const void *a_
, const void *b_
)
966 struct packed_git
*a
= *((struct packed_git
**)a_
);
967 struct packed_git
*b
= *((struct packed_git
**)b_
);
971 * Local packs tend to contain objects specific to our
972 * variant of the project than remote ones. In addition,
973 * remote ones could be on a network mounted filesystem.
974 * Favor local ones for these reasons.
976 st
= a
->pack_local
- b
->pack_local
;
981 * Younger packs tend to contain more recent objects,
982 * and more recent objects tend to get accessed more
985 if (a
->mtime
< b
->mtime
)
987 else if (a
->mtime
== b
->mtime
)
992 static void rearrange_packed_git(void)
994 struct packed_git
**ary
, *p
;
997 for (n
= 0, p
= packed_git
; p
; p
= p
->next
)
1002 /* prepare an array of packed_git for easier sorting */
1003 ary
= xcalloc(n
, sizeof(struct packed_git
*));
1004 for (n
= 0, p
= packed_git
; p
; p
= p
->next
)
1007 qsort(ary
, n
, sizeof(struct packed_git
*), sort_pack
);
1009 /* link them back again */
1010 for (i
= 0; i
< n
- 1; i
++)
1011 ary
[i
]->next
= ary
[i
+ 1];
1012 ary
[n
- 1]->next
= NULL
;
1013 packed_git
= ary
[0];
1018 static int prepare_packed_git_run_once
= 0;
1019 void prepare_packed_git(void)
1021 struct alternate_object_database
*alt
;
1023 if (prepare_packed_git_run_once
)
1025 prepare_packed_git_one(get_object_directory(), 1);
1027 for (alt
= alt_odb_list
; alt
; alt
= alt
->next
) {
1029 prepare_packed_git_one(alt
->base
, 0);
1030 alt
->name
[-1] = '/';
1032 rearrange_packed_git();
1033 prepare_packed_git_run_once
= 1;
1036 void reprepare_packed_git(void)
1039 prepare_packed_git_run_once
= 0;
1040 prepare_packed_git();
1043 static void mark_bad_packed_object(struct packed_git
*p
,
1044 const unsigned char *sha1
)
1047 for (i
= 0; i
< p
->num_bad_objects
; i
++)
1048 if (!hashcmp(sha1
, p
->bad_object_sha1
+ 20 * i
))
1050 p
->bad_object_sha1
= xrealloc(p
->bad_object_sha1
, 20 * (p
->num_bad_objects
+ 1));
1051 hashcpy(p
->bad_object_sha1
+ 20 * p
->num_bad_objects
, sha1
);
1052 p
->num_bad_objects
++;
1055 static int has_packed_and_bad(const unsigned char *sha1
)
1057 struct packed_git
*p
;
1060 for (p
= packed_git
; p
; p
= p
->next
)
1061 for (i
= 0; i
< p
->num_bad_objects
; i
++)
1062 if (!hashcmp(sha1
, p
->bad_object_sha1
+ 20 * i
))
1067 int check_sha1_signature(const unsigned char *sha1
, void *map
, unsigned long size
, const char *type
)
1069 unsigned char real_sha1
[20];
1070 hash_sha1_file(map
, size
, type
, real_sha1
);
1071 return hashcmp(sha1
, real_sha1
) ? -1 : 0;
1074 static int git_open_noatime(const char *name
)
1076 static int sha1_file_open_flag
= O_NOATIME
;
1077 int fd
= open(name
, O_RDONLY
| sha1_file_open_flag
);
1079 /* Might the failure be due to O_NOATIME? */
1080 if (fd
< 0 && errno
!= ENOENT
&& sha1_file_open_flag
) {
1081 fd
= open(name
, O_RDONLY
);
1083 sha1_file_open_flag
= 0;
1088 static int open_sha1_file(const unsigned char *sha1
)
1091 char *name
= sha1_file_name(sha1
);
1092 struct alternate_object_database
*alt
;
1094 fd
= git_open_noatime(name
);
1100 for (alt
= alt_odb_list
; alt
; alt
= alt
->next
) {
1102 fill_sha1_path(name
, sha1
);
1103 fd
= git_open_noatime(alt
->base
);
1110 static void *map_sha1_file(const unsigned char *sha1
, unsigned long *size
)
1115 fd
= open_sha1_file(sha1
);
1120 if (!fstat(fd
, &st
)) {
1121 *size
= xsize_t(st
.st_size
);
1122 map
= xmmap(NULL
, *size
, PROT_READ
, MAP_PRIVATE
, fd
, 0);
1129 static int legacy_loose_object(unsigned char *map
)
1134 * Is it a zlib-compressed buffer? If so, the first byte
1135 * must be 0x78 (15-bit window size, deflated), and the
1136 * first 16-bit word is evenly divisible by 31
1138 word
= (map
[0] << 8) + map
[1];
1139 if (map
[0] == 0x78 && !(word
% 31))
1145 unsigned long unpack_object_header_gently(const unsigned char *buf
, unsigned long len
, enum object_type
*type
, unsigned long *sizep
)
1150 unsigned long used
= 0;
1153 *type
= (c
>> 4) & 7;
1159 if (sizeof(long) * 8 <= shift
)
1162 size
+= (c
& 0x7f) << shift
;
1169 static int unpack_sha1_header(z_stream
*stream
, unsigned char *map
, unsigned long mapsize
, void *buffer
, unsigned long bufsiz
)
1171 unsigned long size
, used
;
1172 static const char valid_loose_object_type
[8] = {
1174 1, 1, 1, 1, /* "commit", "tree", "blob", "tag" */
1175 0, /* "delta" and others are invalid in a loose object */
1177 enum object_type type
;
1179 /* Get the data stream */
1180 memset(stream
, 0, sizeof(*stream
));
1181 stream
->next_in
= map
;
1182 stream
->avail_in
= mapsize
;
1183 stream
->next_out
= buffer
;
1184 stream
->avail_out
= bufsiz
;
1186 if (legacy_loose_object(map
)) {
1187 inflateInit(stream
);
1188 return inflate(stream
, 0);
1193 * There used to be a second loose object header format which
1194 * was meant to mimic the in-pack format, allowing for direct
1195 * copy of the object data. This format turned up not to be
1196 * really worth it and we don't write it any longer. But we
1197 * can still read it.
1199 used
= unpack_object_header_gently(map
, mapsize
, &type
, &size
);
1200 if (!used
|| !valid_loose_object_type
[type
])
1205 /* Set up the stream for the rest.. */
1206 stream
->next_in
= map
;
1207 stream
->avail_in
= mapsize
;
1208 inflateInit(stream
);
1210 /* And generate the fake traditional header */
1211 stream
->total_out
= 1 + snprintf(buffer
, bufsiz
, "%s %lu",
1212 typename(type
), size
);
1216 static void *unpack_sha1_rest(z_stream
*stream
, void *buffer
, unsigned long size
, const unsigned char *sha1
)
1218 int bytes
= strlen(buffer
) + 1;
1219 unsigned char *buf
= xmalloc(1+size
);
1223 n
= stream
->total_out
- bytes
;
1226 memcpy(buf
, (char *) buffer
+ bytes
, n
);
1228 if (bytes
<= size
) {
1230 * The above condition must be (bytes <= size), not
1231 * (bytes < size). In other words, even though we
1232 * expect no more output and set avail_out to zer0,
1233 * the input zlib stream may have bytes that express
1234 * "this concludes the stream", and we *do* want to
1237 * Otherwise we would not be able to test that we
1238 * consumed all the input to reach the expected size;
1239 * we also want to check that zlib tells us that all
1240 * went well with status == Z_STREAM_END at the end.
1242 stream
->next_out
= buf
+ bytes
;
1243 stream
->avail_out
= size
- bytes
;
1244 while (status
== Z_OK
)
1245 status
= inflate(stream
, Z_FINISH
);
1248 if (status
== Z_STREAM_END
&& !stream
->avail_in
) {
1254 error("corrupt loose object '%s'", sha1_to_hex(sha1
));
1255 else if (stream
->avail_in
)
1256 error("garbage at end of loose object '%s'",
1263 * We used to just use "sscanf()", but that's actually way
1264 * too permissive for what we want to check. So do an anal
1265 * object header parse by hand.
1267 static int parse_sha1_header(const char *hdr
, unsigned long *sizep
)
1274 * The type can be at most ten bytes (including the
1275 * terminating '\0' that we add), and is followed by
1284 if (i
>= sizeof(type
))
1290 * The length must follow immediately, and be in canonical
1291 * decimal format (ie "010" is not valid).
1293 size
= *hdr
++ - '0';
1298 unsigned long c
= *hdr
- '0';
1302 size
= size
* 10 + c
;
1308 * The length must be followed by a zero byte
1310 return *hdr
? -1 : type_from_string(type
);
1313 static void *unpack_sha1_file(void *map
, unsigned long mapsize
, enum object_type
*type
, unsigned long *size
, const unsigned char *sha1
)
1319 ret
= unpack_sha1_header(&stream
, map
, mapsize
, hdr
, sizeof(hdr
));
1320 if (ret
< Z_OK
|| (*type
= parse_sha1_header(hdr
, size
)) < 0)
1323 return unpack_sha1_rest(&stream
, hdr
, *size
, sha1
);
1326 unsigned long get_size_from_delta(struct packed_git
*p
,
1327 struct pack_window
**w_curs
,
1330 const unsigned char *data
;
1331 unsigned char delta_head
[20], *in
;
1335 memset(&stream
, 0, sizeof(stream
));
1336 stream
.next_out
= delta_head
;
1337 stream
.avail_out
= sizeof(delta_head
);
1339 inflateInit(&stream
);
1341 in
= use_pack(p
, w_curs
, curpos
, &stream
.avail_in
);
1342 stream
.next_in
= in
;
1343 st
= inflate(&stream
, Z_FINISH
);
1344 curpos
+= stream
.next_in
- in
;
1345 } while ((st
== Z_OK
|| st
== Z_BUF_ERROR
) &&
1346 stream
.total_out
< sizeof(delta_head
));
1347 inflateEnd(&stream
);
1348 if ((st
!= Z_STREAM_END
) && stream
.total_out
!= sizeof(delta_head
))
1349 die("delta data unpack-initial failed");
1351 /* Examine the initial part of the delta to figure out
1356 /* ignore base size */
1357 get_delta_hdr_size(&data
, delta_head
+sizeof(delta_head
));
1359 /* Read the result size */
1360 return get_delta_hdr_size(&data
, delta_head
+sizeof(delta_head
));
1363 static off_t
get_delta_base(struct packed_git
*p
,
1364 struct pack_window
**w_curs
,
1366 enum object_type type
,
1367 off_t delta_obj_offset
)
1369 unsigned char *base_info
= use_pack(p
, w_curs
, *curpos
, NULL
);
1372 /* use_pack() assured us we have [base_info, base_info + 20)
1373 * as a range that we can look at without walking off the
1374 * end of the mapped window. Its actually the hash size
1375 * that is assured. An OFS_DELTA longer than the hash size
1376 * is stupid, as then a REF_DELTA would be smaller to store.
1378 if (type
== OBJ_OFS_DELTA
) {
1380 unsigned char c
= base_info
[used
++];
1381 base_offset
= c
& 127;
1384 if (!base_offset
|| MSB(base_offset
, 7))
1385 return 0; /* overflow */
1386 c
= base_info
[used
++];
1387 base_offset
= (base_offset
<< 7) + (c
& 127);
1389 base_offset
= delta_obj_offset
- base_offset
;
1390 if (base_offset
>= delta_obj_offset
)
1391 return 0; /* out of bound */
1393 } else if (type
== OBJ_REF_DELTA
) {
1394 /* The base entry _must_ be in the same pack */
1395 base_offset
= find_pack_entry_one(base_info
, p
);
1398 die("I am totally screwed");
1402 /* forward declaration for a mutually recursive function */
1403 static int packed_object_info(struct packed_git
*p
, off_t offset
,
1404 unsigned long *sizep
);
1406 static int packed_delta_info(struct packed_git
*p
,
1407 struct pack_window
**w_curs
,
1409 enum object_type type
,
1411 unsigned long *sizep
)
1415 base_offset
= get_delta_base(p
, w_curs
, &curpos
, type
, obj_offset
);
1416 type
= packed_object_info(p
, base_offset
, NULL
);
1418 /* We choose to only get the type of the base object and
1419 * ignore potentially corrupt pack file that expects the delta
1420 * based on a base with a wrong size. This saves tons of
1424 *sizep
= get_size_from_delta(p
, w_curs
, curpos
);
1429 static int unpack_object_header(struct packed_git
*p
,
1430 struct pack_window
**w_curs
,
1432 unsigned long *sizep
)
1434 unsigned char *base
;
1437 enum object_type type
;
1439 /* use_pack() assures us we have [base, base + 20) available
1440 * as a range that we can look at at. (Its actually the hash
1441 * size that is assured.) With our object header encoding
1442 * the maximum deflated object size is 2^137, which is just
1443 * insane, so we know won't exceed what we have been given.
1445 base
= use_pack(p
, w_curs
, *curpos
, &left
);
1446 used
= unpack_object_header_gently(base
, left
, &type
, sizep
);
1448 die("object offset outside of pack file");
1454 const char *packed_object_info_detail(struct packed_git
*p
,
1456 unsigned long *size
,
1457 unsigned long *store_size
,
1458 unsigned int *delta_chain_length
,
1459 unsigned char *base_sha1
)
1461 struct pack_window
*w_curs
= NULL
;
1463 unsigned long dummy
;
1464 unsigned char *next_sha1
;
1465 enum object_type type
;
1466 struct revindex_entry
*revidx
;
1468 *delta_chain_length
= 0;
1469 curpos
= obj_offset
;
1470 type
= unpack_object_header(p
, &w_curs
, &curpos
, size
);
1472 revidx
= find_pack_revindex(p
, obj_offset
);
1473 *store_size
= revidx
[1].offset
- obj_offset
;
1478 die("pack %s contains unknown object type %d",
1479 p
->pack_name
, type
);
1484 unuse_pack(&w_curs
);
1485 return typename(type
);
1487 obj_offset
= get_delta_base(p
, &w_curs
, &curpos
, type
, obj_offset
);
1489 die("pack %s contains bad delta base reference of type %s",
1490 p
->pack_name
, typename(type
));
1491 if (*delta_chain_length
== 0) {
1492 revidx
= find_pack_revindex(p
, obj_offset
);
1493 hashcpy(base_sha1
, nth_packed_object_sha1(p
, revidx
->nr
));
1497 next_sha1
= use_pack(p
, &w_curs
, curpos
, NULL
);
1498 if (*delta_chain_length
== 0)
1499 hashcpy(base_sha1
, next_sha1
);
1500 obj_offset
= find_pack_entry_one(next_sha1
, p
);
1503 (*delta_chain_length
)++;
1504 curpos
= obj_offset
;
1505 type
= unpack_object_header(p
, &w_curs
, &curpos
, &dummy
);
1509 static int packed_object_info(struct packed_git
*p
, off_t obj_offset
,
1510 unsigned long *sizep
)
1512 struct pack_window
*w_curs
= NULL
;
1514 off_t curpos
= obj_offset
;
1515 enum object_type type
;
1517 type
= unpack_object_header(p
, &w_curs
, &curpos
, &size
);
1522 type
= packed_delta_info(p
, &w_curs
, curpos
,
1523 type
, obj_offset
, sizep
);
1533 die("pack %s contains unknown object type %d",
1534 p
->pack_name
, type
);
1536 unuse_pack(&w_curs
);
1540 static void *unpack_compressed_entry(struct packed_git
*p
,
1541 struct pack_window
**w_curs
,
1547 unsigned char *buffer
, *in
;
1549 buffer
= xmalloc(size
+ 1);
1551 memset(&stream
, 0, sizeof(stream
));
1552 stream
.next_out
= buffer
;
1553 stream
.avail_out
= size
;
1555 inflateInit(&stream
);
1557 in
= use_pack(p
, w_curs
, curpos
, &stream
.avail_in
);
1558 stream
.next_in
= in
;
1559 st
= inflate(&stream
, Z_FINISH
);
1560 curpos
+= stream
.next_in
- in
;
1561 } while (st
== Z_OK
|| st
== Z_BUF_ERROR
);
1562 inflateEnd(&stream
);
1563 if ((st
!= Z_STREAM_END
) || stream
.total_out
!= size
) {
1571 #define MAX_DELTA_CACHE (256)
1573 static size_t delta_base_cached
;
1575 static struct delta_base_cache_lru_list
{
1576 struct delta_base_cache_lru_list
*prev
;
1577 struct delta_base_cache_lru_list
*next
;
1578 } delta_base_cache_lru
= { &delta_base_cache_lru
, &delta_base_cache_lru
};
1580 static struct delta_base_cache_entry
{
1581 struct delta_base_cache_lru_list lru
;
1583 struct packed_git
*p
;
1586 enum object_type type
;
1587 } delta_base_cache
[MAX_DELTA_CACHE
];
1589 static unsigned long pack_entry_hash(struct packed_git
*p
, off_t base_offset
)
1593 hash
= (unsigned long)p
+ (unsigned long)base_offset
;
1594 hash
+= (hash
>> 8) + (hash
>> 16);
1595 return hash
% MAX_DELTA_CACHE
;
1598 static void *cache_or_unpack_entry(struct packed_git
*p
, off_t base_offset
,
1599 unsigned long *base_size
, enum object_type
*type
, int keep_cache
)
1602 unsigned long hash
= pack_entry_hash(p
, base_offset
);
1603 struct delta_base_cache_entry
*ent
= delta_base_cache
+ hash
;
1606 if (ret
&& ent
->p
== p
&& ent
->base_offset
== base_offset
)
1607 goto found_cache_entry
;
1608 return unpack_entry(p
, base_offset
, type
, base_size
);
1613 ent
->lru
.next
->prev
= ent
->lru
.prev
;
1614 ent
->lru
.prev
->next
= ent
->lru
.next
;
1615 delta_base_cached
-= ent
->size
;
1617 ret
= xmemdupz(ent
->data
, ent
->size
);
1620 *base_size
= ent
->size
;
1624 static inline void release_delta_base_cache(struct delta_base_cache_entry
*ent
)
1629 ent
->lru
.next
->prev
= ent
->lru
.prev
;
1630 ent
->lru
.prev
->next
= ent
->lru
.next
;
1631 delta_base_cached
-= ent
->size
;
1635 static void add_delta_base_cache(struct packed_git
*p
, off_t base_offset
,
1636 void *base
, unsigned long base_size
, enum object_type type
)
1638 unsigned long hash
= pack_entry_hash(p
, base_offset
);
1639 struct delta_base_cache_entry
*ent
= delta_base_cache
+ hash
;
1640 struct delta_base_cache_lru_list
*lru
;
1642 release_delta_base_cache(ent
);
1643 delta_base_cached
+= base_size
;
1645 for (lru
= delta_base_cache_lru
.next
;
1646 delta_base_cached
> delta_base_cache_limit
1647 && lru
!= &delta_base_cache_lru
;
1649 struct delta_base_cache_entry
*f
= (void *)lru
;
1650 if (f
->type
== OBJ_BLOB
)
1651 release_delta_base_cache(f
);
1653 for (lru
= delta_base_cache_lru
.next
;
1654 delta_base_cached
> delta_base_cache_limit
1655 && lru
!= &delta_base_cache_lru
;
1657 struct delta_base_cache_entry
*f
= (void *)lru
;
1658 release_delta_base_cache(f
);
1662 ent
->base_offset
= base_offset
;
1665 ent
->size
= base_size
;
1666 ent
->lru
.next
= &delta_base_cache_lru
;
1667 ent
->lru
.prev
= delta_base_cache_lru
.prev
;
1668 delta_base_cache_lru
.prev
->next
= &ent
->lru
;
1669 delta_base_cache_lru
.prev
= &ent
->lru
;
1672 static void *unpack_delta_entry(struct packed_git
*p
,
1673 struct pack_window
**w_curs
,
1675 unsigned long delta_size
,
1677 enum object_type
*type
,
1678 unsigned long *sizep
)
1680 void *delta_data
, *result
, *base
;
1681 unsigned long base_size
;
1684 base_offset
= get_delta_base(p
, w_curs
, &curpos
, *type
, obj_offset
);
1686 error("failed to validate delta base reference "
1687 "at offset %"PRIuMAX
" from %s",
1688 (uintmax_t)curpos
, p
->pack_name
);
1692 base
= cache_or_unpack_entry(p
, base_offset
, &base_size
, type
, 0);
1695 * We're probably in deep shit, but let's try to fetch
1696 * the required base anyway from another pack or loose.
1697 * This is costly but should happen only in the presence
1698 * of a corrupted pack, and is better than failing outright.
1700 struct revindex_entry
*revidx
= find_pack_revindex(p
, base_offset
);
1701 const unsigned char *base_sha1
=
1702 nth_packed_object_sha1(p
, revidx
->nr
);
1703 error("failed to read delta base object %s"
1704 " at offset %"PRIuMAX
" from %s",
1705 sha1_to_hex(base_sha1
), (uintmax_t)base_offset
,
1707 mark_bad_packed_object(p
, base_sha1
);
1708 base
= read_object(base_sha1
, type
, &base_size
);
1713 delta_data
= unpack_compressed_entry(p
, w_curs
, curpos
, delta_size
);
1715 error("failed to unpack compressed delta "
1716 "at offset %"PRIuMAX
" from %s",
1717 (uintmax_t)curpos
, p
->pack_name
);
1721 result
= patch_delta(base
, base_size
,
1722 delta_data
, delta_size
,
1725 die("failed to apply delta");
1727 add_delta_base_cache(p
, base_offset
, base
, base_size
, *type
);
1731 void *unpack_entry(struct packed_git
*p
, off_t obj_offset
,
1732 enum object_type
*type
, unsigned long *sizep
)
1734 struct pack_window
*w_curs
= NULL
;
1735 off_t curpos
= obj_offset
;
1738 *type
= unpack_object_header(p
, &w_curs
, &curpos
, sizep
);
1742 data
= unpack_delta_entry(p
, &w_curs
, curpos
, *sizep
,
1743 obj_offset
, type
, sizep
);
1749 data
= unpack_compressed_entry(p
, &w_curs
, curpos
, *sizep
);
1753 error("unknown object type %i at offset %"PRIuMAX
" in %s",
1754 *type
, (uintmax_t)obj_offset
, p
->pack_name
);
1756 unuse_pack(&w_curs
);
1760 const unsigned char *nth_packed_object_sha1(struct packed_git
*p
,
1763 const unsigned char *index
= p
->index_data
;
1765 if (open_pack_index(p
))
1767 index
= p
->index_data
;
1769 if (n
>= p
->num_objects
)
1772 if (p
->index_version
== 1) {
1773 return index
+ 24 * n
+ 4;
1776 return index
+ 20 * n
;
1780 off_t
nth_packed_object_offset(const struct packed_git
*p
, uint32_t n
)
1782 const unsigned char *index
= p
->index_data
;
1784 if (p
->index_version
== 1) {
1785 return ntohl(*((uint32_t *)(index
+ 24 * n
)));
1788 index
+= 8 + p
->num_objects
* (20 + 4);
1789 off
= ntohl(*((uint32_t *)(index
+ 4 * n
)));
1790 if (!(off
& 0x80000000))
1792 index
+= p
->num_objects
* 4 + (off
& 0x7fffffff) * 8;
1793 return (((uint64_t)ntohl(*((uint32_t *)(index
+ 0)))) << 32) |
1794 ntohl(*((uint32_t *)(index
+ 4)));
1798 off_t
find_pack_entry_one(const unsigned char *sha1
,
1799 struct packed_git
*p
)
1801 const uint32_t *level1_ofs
= p
->index_data
;
1802 const unsigned char *index
= p
->index_data
;
1803 unsigned hi
, lo
, stride
;
1804 static int use_lookup
= -1;
1805 static int debug_lookup
= -1;
1807 if (debug_lookup
< 0)
1808 debug_lookup
= !!getenv("GIT_DEBUG_LOOKUP");
1811 if (open_pack_index(p
))
1813 level1_ofs
= p
->index_data
;
1814 index
= p
->index_data
;
1816 if (p
->index_version
> 1) {
1821 hi
= ntohl(level1_ofs
[*sha1
]);
1822 lo
= ((*sha1
== 0x0) ? 0 : ntohl(level1_ofs
[*sha1
- 1]));
1823 if (p
->index_version
> 1) {
1831 printf("%02x%02x%02x... lo %u hi %u nr %"PRIu32
"\n",
1832 sha1
[0], sha1
[1], sha1
[2], lo
, hi
, p
->num_objects
);
1835 use_lookup
= !!getenv("GIT_USE_LOOKUP");
1837 int pos
= sha1_entry_pos(index
, stride
, 0,
1838 lo
, hi
, p
->num_objects
, sha1
);
1841 return nth_packed_object_offset(p
, pos
);
1845 unsigned mi
= (lo
+ hi
) / 2;
1846 int cmp
= hashcmp(index
+ mi
* stride
, sha1
);
1849 printf("lo %u hi %u rg %u mi %u\n",
1850 lo
, hi
, hi
- lo
, mi
);
1852 return nth_packed_object_offset(p
, mi
);
1861 int is_kept_pack(const struct packed_git
*p
)
1863 return p
->pack_keep
;
1866 static int find_pack_ent(const unsigned char *sha1
, struct pack_entry
*e
,
1867 const struct rev_info
*revs
)
1869 static struct packed_git
*last_found
= (void *)1;
1870 struct packed_git
*p
;
1873 prepare_packed_git();
1876 p
= (last_found
== (void *)1) ? packed_git
: last_found
;
1879 if (revs
->kept_pack_only
&& !is_kept_pack(p
))
1881 if (p
->num_bad_objects
) {
1883 for (i
= 0; i
< p
->num_bad_objects
; i
++)
1884 if (!hashcmp(sha1
, p
->bad_object_sha1
+ 20 * i
))
1888 offset
= find_pack_entry_one(sha1
, p
);
1891 * We are about to tell the caller where they can
1892 * locate the requested object. We better make
1893 * sure the packfile is still here and can be
1894 * accessed before supplying that answer, as
1895 * it may have been deleted since the index
1898 if (p
->pack_fd
== -1 && open_packed_git(p
)) {
1899 error("packfile %s cannot be accessed", p
->pack_name
);
1904 hashcpy(e
->sha1
, sha1
);
1910 if (p
== last_found
)
1914 if (p
== last_found
)
1920 static int find_pack_entry(const unsigned char *sha1
, struct pack_entry
*e
)
1922 return find_pack_ent(sha1
, e
, NULL
);
1925 static int find_kept_pack_entry(const unsigned char *sha1
, struct pack_entry
*e
,
1926 const struct rev_info
*revs
)
1928 return find_pack_ent(sha1
, e
, revs
);
1931 struct packed_git
*find_sha1_pack(const unsigned char *sha1
,
1932 struct packed_git
*packs
)
1934 struct packed_git
*p
;
1936 for (p
= packs
; p
; p
= p
->next
) {
1937 if (find_pack_entry_one(sha1
, p
))
1944 static int sha1_loose_object_info(const unsigned char *sha1
, unsigned long *sizep
)
1947 unsigned long mapsize
, size
;
1952 map
= map_sha1_file(sha1
, &mapsize
);
1954 return error("unable to find %s", sha1_to_hex(sha1
));
1955 if (unpack_sha1_header(&stream
, map
, mapsize
, hdr
, sizeof(hdr
)) < 0)
1956 status
= error("unable to unpack %s header",
1958 else if ((status
= parse_sha1_header(hdr
, &size
)) < 0)
1959 status
= error("unable to parse %s header", sha1_to_hex(sha1
));
1962 inflateEnd(&stream
);
1963 munmap(map
, mapsize
);
1967 int sha1_object_info(const unsigned char *sha1
, unsigned long *sizep
)
1969 struct pack_entry e
;
1972 if (!find_pack_entry(sha1
, &e
)) {
1973 /* Most likely it's a loose object. */
1974 status
= sha1_loose_object_info(sha1
, sizep
);
1978 /* Not a loose object; someone else may have just packed it. */
1979 reprepare_packed_git();
1980 if (!find_pack_entry(sha1
, &e
))
1983 return packed_object_info(e
.p
, e
.offset
, sizep
);
1986 static void *read_packed_sha1(const unsigned char *sha1
,
1987 enum object_type
*type
, unsigned long *size
)
1989 struct pack_entry e
;
1992 if (!find_pack_entry(sha1
, &e
))
1994 data
= cache_or_unpack_entry(e
.p
, e
.offset
, size
, type
, 1);
1997 * We're probably in deep shit, but let's try to fetch
1998 * the required object anyway from another pack or loose.
1999 * This should happen only in the presence of a corrupted
2000 * pack, and is better than failing outright.
2002 error("failed to read object %s at offset %"PRIuMAX
" from %s",
2003 sha1_to_hex(sha1
), (uintmax_t)e
.offset
, e
.p
->pack_name
);
2004 mark_bad_packed_object(e
.p
, sha1
);
2005 data
= read_object(sha1
, type
, size
);
2011 * This is meant to hold a *small* number of objects that you would
2012 * want read_sha1_file() to be able to return, but yet you do not want
2013 * to write them into the object store (e.g. a browse-only
2016 static struct cached_object
{
2017 unsigned char sha1
[20];
2018 enum object_type type
;
2022 static int cached_object_nr
, cached_object_alloc
;
2024 static struct cached_object empty_tree
= {
2025 /* empty tree sha1: 4b825dc642cb6eb9a060e54bf8d69288fbee4904 */
2026 "\x4b\x82\x5d\xc6\x42\xcb\x6e\xb9\xa0\x60"
2027 "\xe5\x4b\xf8\xd6\x92\x88\xfb\xee\x49\x04",
2033 static struct cached_object
*find_cached_object(const unsigned char *sha1
)
2036 struct cached_object
*co
= cached_objects
;
2038 for (i
= 0; i
< cached_object_nr
; i
++, co
++) {
2039 if (!hashcmp(co
->sha1
, sha1
))
2042 if (!hashcmp(sha1
, empty_tree
.sha1
))
2047 int pretend_sha1_file(void *buf
, unsigned long len
, enum object_type type
,
2048 unsigned char *sha1
)
2050 struct cached_object
*co
;
2052 hash_sha1_file(buf
, len
, typename(type
), sha1
);
2053 if (has_sha1_file(sha1
) || find_cached_object(sha1
))
2055 if (cached_object_alloc
<= cached_object_nr
) {
2056 cached_object_alloc
= alloc_nr(cached_object_alloc
);
2057 cached_objects
= xrealloc(cached_objects
,
2058 sizeof(*cached_objects
) *
2059 cached_object_alloc
);
2061 co
= &cached_objects
[cached_object_nr
++];
2064 co
->buf
= xmalloc(len
);
2065 memcpy(co
->buf
, buf
, len
);
2066 hashcpy(co
->sha1
, sha1
);
2070 void *read_object(const unsigned char *sha1
, enum object_type
*type
,
2071 unsigned long *size
)
2073 unsigned long mapsize
;
2075 struct cached_object
*co
;
2077 co
= find_cached_object(sha1
);
2081 return xmemdupz(co
->buf
, co
->size
);
2084 buf
= read_packed_sha1(sha1
, type
, size
);
2087 map
= map_sha1_file(sha1
, &mapsize
);
2089 buf
= unpack_sha1_file(map
, mapsize
, type
, size
, sha1
);
2090 munmap(map
, mapsize
);
2093 reprepare_packed_git();
2094 return read_packed_sha1(sha1
, type
, size
);
2097 void *read_sha1_file(const unsigned char *sha1
, enum object_type
*type
,
2098 unsigned long *size
)
2100 void *data
= read_object(sha1
, type
, size
);
2101 /* legacy behavior is to die on corrupted objects */
2102 if (!data
&& (has_loose_object(sha1
) || has_packed_and_bad(sha1
)))
2103 die("object %s is corrupted", sha1_to_hex(sha1
));
2107 void *read_object_with_reference(const unsigned char *sha1
,
2108 const char *required_type_name
,
2109 unsigned long *size
,
2110 unsigned char *actual_sha1_return
)
2112 enum object_type type
, required_type
;
2114 unsigned long isize
;
2115 unsigned char actual_sha1
[20];
2117 required_type
= type_from_string(required_type_name
);
2118 hashcpy(actual_sha1
, sha1
);
2120 int ref_length
= -1;
2121 const char *ref_type
= NULL
;
2123 buffer
= read_sha1_file(actual_sha1
, &type
, &isize
);
2126 if (type
== required_type
) {
2128 if (actual_sha1_return
)
2129 hashcpy(actual_sha1_return
, actual_sha1
);
2132 /* Handle references */
2133 else if (type
== OBJ_COMMIT
)
2135 else if (type
== OBJ_TAG
)
2136 ref_type
= "object ";
2141 ref_length
= strlen(ref_type
);
2143 if (ref_length
+ 40 > isize
||
2144 memcmp(buffer
, ref_type
, ref_length
) ||
2145 get_sha1_hex((char *) buffer
+ ref_length
, actual_sha1
)) {
2150 /* Now we have the ID of the referred-to object in
2151 * actual_sha1. Check again. */
2155 static void write_sha1_file_prepare(const void *buf
, unsigned long len
,
2156 const char *type
, unsigned char *sha1
,
2157 char *hdr
, int *hdrlen
)
2161 /* Generate the header */
2162 *hdrlen
= sprintf(hdr
, "%s %lu", type
, len
)+1;
2166 SHA1_Update(&c
, hdr
, *hdrlen
);
2167 SHA1_Update(&c
, buf
, len
);
2168 SHA1_Final(sha1
, &c
);
2172 * Move the just written object into its final resting place
2174 int move_temp_to_file(const char *tmpfile
, const char *filename
)
2177 if (link(tmpfile
, filename
))
2181 * Coda hack - coda doesn't like cross-directory links,
2182 * so we fall back to a rename, which will mean that it
2183 * won't be able to check collisions, but that's not a
2186 * The same holds for FAT formatted media.
2188 * When this succeeds, we just return 0. We have nothing
2191 if (ret
&& ret
!= EEXIST
) {
2192 if (!rename(tmpfile
, filename
))
2198 if (ret
!= EEXIST
) {
2199 return error("unable to write sha1 filename %s: %s\n", filename
, strerror(ret
));
2201 /* FIXME!!! Collision check here ? */
2207 static int write_buffer(int fd
, const void *buf
, size_t len
)
2209 if (write_in_full(fd
, buf
, len
) < 0)
2210 return error("file write error (%s)", strerror(errno
));
2214 int hash_sha1_file(const void *buf
, unsigned long len
, const char *type
,
2215 unsigned char *sha1
)
2219 write_sha1_file_prepare(buf
, len
, type
, sha1
, hdr
, &hdrlen
);
2223 /* Finalize a file on disk, and close it. */
2224 static void close_sha1_file(int fd
)
2226 if (fsync_object_files
)
2227 fsync_or_die(fd
, "sha1 file");
2230 die("unable to write sha1 file");
2233 /* Size of directory component, including the ending '/' */
2234 static inline int directory_size(const char *filename
)
2236 const char *s
= strrchr(filename
, '/');
2239 return s
- filename
+ 1;
2243 * This creates a temporary file in the same directory as the final
2246 * We want to avoid cross-directory filename renames, because those
2247 * can have problems on various filesystems (FAT, NFS, Coda).
2249 static int create_tmpfile(char *buffer
, size_t bufsiz
, const char *filename
)
2251 int fd
, dirlen
= directory_size(filename
);
2253 if (dirlen
+ 20 > bufsiz
) {
2254 errno
= ENAMETOOLONG
;
2257 memcpy(buffer
, filename
, dirlen
);
2258 strcpy(buffer
+ dirlen
, "tmp_obj_XXXXXX");
2259 fd
= mkstemp(buffer
);
2260 if (fd
< 0 && dirlen
&& errno
== ENOENT
) {
2261 /* Make sure the directory exists */
2262 memcpy(buffer
, filename
, dirlen
);
2263 buffer
[dirlen
-1] = 0;
2264 if (mkdir(buffer
, 0777) || adjust_shared_perm(buffer
))
2268 strcpy(buffer
+ dirlen
- 1, "/tmp_obj_XXXXXX");
2269 fd
= mkstemp(buffer
);
2274 static int write_loose_object(const unsigned char *sha1
, char *hdr
, int hdrlen
,
2275 void *buf
, unsigned long len
, time_t mtime
)
2278 unsigned char *compressed
;
2281 static char tmpfile
[PATH_MAX
];
2283 filename
= sha1_file_name(sha1
);
2284 fd
= create_tmpfile(tmpfile
, sizeof(tmpfile
), filename
);
2286 if (errno
== EACCES
)
2287 return error("insufficient permission for adding an object to repository database %s\n", get_object_directory());
2289 return error("unable to create temporary sha1 filename %s: %s\n", tmpfile
, strerror(errno
));
2293 memset(&stream
, 0, sizeof(stream
));
2294 deflateInit(&stream
, zlib_compression_level
);
2295 size
= 8 + deflateBound(&stream
, len
+hdrlen
);
2296 compressed
= xmalloc(size
);
2299 stream
.next_out
= compressed
;
2300 stream
.avail_out
= size
;
2302 /* First header.. */
2303 stream
.next_in
= (unsigned char *)hdr
;
2304 stream
.avail_in
= hdrlen
;
2305 while (deflate(&stream
, 0) == Z_OK
)
2308 /* Then the data itself.. */
2309 stream
.next_in
= buf
;
2310 stream
.avail_in
= len
;
2311 ret
= deflate(&stream
, Z_FINISH
);
2312 if (ret
!= Z_STREAM_END
)
2313 die("unable to deflate new object %s (%d)", sha1_to_hex(sha1
), ret
);
2315 ret
= deflateEnd(&stream
);
2317 die("deflateEnd on object %s failed (%d)", sha1_to_hex(sha1
), ret
);
2319 size
= stream
.total_out
;
2321 if (write_buffer(fd
, compressed
, size
) < 0)
2322 die("unable to write sha1 file");
2323 close_sha1_file(fd
);
2329 utb
.modtime
= mtime
;
2330 if (utime(tmpfile
, &utb
) < 0)
2331 warning("failed utime() on %s: %s",
2332 tmpfile
, strerror(errno
));
2335 return move_temp_to_file(tmpfile
, filename
);
2338 int write_sha1_file(void *buf
, unsigned long len
, const char *type
, unsigned char *returnsha1
)
2340 unsigned char sha1
[20];
2344 /* Normally if we have it in the pack then we do not bother writing
2345 * it out into .git/objects/??/?{38} file.
2347 write_sha1_file_prepare(buf
, len
, type
, sha1
, hdr
, &hdrlen
);
2349 hashcpy(returnsha1
, sha1
);
2350 if (has_sha1_file(sha1
))
2352 return write_loose_object(sha1
, hdr
, hdrlen
, buf
, len
, 0);
2355 int force_object_loose(const unsigned char *sha1
, time_t mtime
)
2359 enum object_type type
;
2364 if (has_loose_object(sha1
))
2366 buf
= read_packed_sha1(sha1
, &type
, &len
);
2368 return error("cannot read sha1_file for %s", sha1_to_hex(sha1
));
2369 hdrlen
= sprintf(hdr
, "%s %lu", typename(type
), len
) + 1;
2370 ret
= write_loose_object(sha1
, hdr
, hdrlen
, buf
, len
, mtime
);
2376 int has_pack_index(const unsigned char *sha1
)
2379 if (stat(sha1_pack_index_name(sha1
), &st
))
2384 int has_pack_file(const unsigned char *sha1
)
2387 if (stat(sha1_pack_name(sha1
), &st
))
2392 int has_sha1_pack(const unsigned char *sha1
)
2394 struct pack_entry e
;
2395 return find_pack_entry(sha1
, &e
);
2398 int has_sha1_kept_pack(const unsigned char *sha1
, const struct rev_info
*revs
)
2400 struct pack_entry e
;
2401 return find_kept_pack_entry(sha1
, &e
, revs
);
2404 int has_sha1_file(const unsigned char *sha1
)
2406 struct pack_entry e
;
2408 if (find_pack_entry(sha1
, &e
))
2410 return has_loose_object(sha1
);
2413 int index_pipe(unsigned char *sha1
, int fd
, const char *type
, int write_object
)
2418 strbuf_init(&buf
, 0);
2419 if (strbuf_read(&buf
, fd
, 4096) < 0) {
2420 strbuf_release(&buf
);
2427 ret
= write_sha1_file(buf
.buf
, buf
.len
, type
, sha1
);
2429 ret
= hash_sha1_file(buf
.buf
, buf
.len
, type
, sha1
);
2430 strbuf_release(&buf
);
2435 int index_fd(unsigned char *sha1
, int fd
, struct stat
*st
, int write_object
,
2436 enum object_type type
, const char *path
)
2438 size_t size
= xsize_t(st
->st_size
);
2440 int ret
, re_allocated
= 0;
2443 buf
= xmmap(NULL
, size
, PROT_READ
, MAP_PRIVATE
, fd
, 0);
2450 * Convert blobs to git internal format
2452 if ((type
== OBJ_BLOB
) && S_ISREG(st
->st_mode
)) {
2454 strbuf_init(&nbuf
, 0);
2455 if (convert_to_git(path
, buf
, size
, &nbuf
,
2456 write_object
? safe_crlf
: 0)) {
2458 buf
= strbuf_detach(&nbuf
, &size
);
2464 ret
= write_sha1_file(buf
, size
, typename(type
), sha1
);
2466 ret
= hash_sha1_file(buf
, size
, typename(type
), sha1
);
2476 int index_path(unsigned char *sha1
, const char *path
, struct stat
*st
, int write_object
)
2482 switch (st
->st_mode
& S_IFMT
) {
2484 fd
= open(path
, O_RDONLY
);
2486 return error("open(\"%s\"): %s", path
,
2488 if (index_fd(sha1
, fd
, st
, write_object
, OBJ_BLOB
, path
) < 0)
2489 return error("%s: failed to insert into database",
2493 len
= xsize_t(st
->st_size
);
2494 target
= xmalloc(len
+ 1);
2495 if (readlink(path
, target
, len
+ 1) != st
->st_size
) {
2496 char *errstr
= strerror(errno
);
2498 return error("readlink(\"%s\"): %s", path
,
2502 hash_sha1_file(target
, len
, blob_type
, sha1
);
2503 else if (write_sha1_file(target
, len
, blob_type
, sha1
))
2504 return error("%s: failed to insert into database",
2509 return resolve_gitlink_ref(path
, "HEAD", sha1
);
2511 return error("%s: unsupported file type", path
);
2516 int read_pack_header(int fd
, struct pack_header
*header
)
2518 if (read_in_full(fd
, header
, sizeof(*header
)) < sizeof(*header
))
2519 /* "eof before pack header was fully read" */
2520 return PH_ERROR_EOF
;
2522 if (header
->hdr_signature
!= htonl(PACK_SIGNATURE
))
2523 /* "protocol error (pack signature mismatch detected)" */
2524 return PH_ERROR_PACK_SIGNATURE
;
2525 if (!pack_version_ok(header
->hdr_version
))
2526 /* "protocol error (pack version unsupported)" */
2527 return PH_ERROR_PROTOCOL
;