2 * GIT - The information manager from hell
4 * Copyright (C) Linus Torvalds, 2005
6 * This handles basic git sha1 object files - packing, unpacking,
18 #if defined(__linux__) && (defined(__i386__) || defined(__PPC__))
19 #define O_NOATIME 01000000
25 const unsigned char null_sha1
[20];
27 static unsigned int sha1_file_open_flag
= O_NOATIME
;
29 static unsigned hexval(char c
)
31 if (c
>= '0' && c
<= '9')
33 if (c
>= 'a' && c
<= 'f')
35 if (c
>= 'A' && c
<= 'F')
40 int get_sha1_hex(const char *hex
, unsigned char *sha1
)
43 for (i
= 0; i
< 20; i
++) {
44 unsigned int val
= (hexval(hex
[0]) << 4) | hexval(hex
[1]);
53 int safe_create_leading_directories(char *path
)
62 pos
= strchr(pos
, '/');
66 if (!stat(path
, &st
)) {
68 if (!S_ISDIR(st
.st_mode
)) {
73 else if (mkdir(path
, 0777)) {
77 else if (adjust_shared_perm(path
)) {
86 char * sha1_to_hex(const unsigned char *sha1
)
89 static char hexbuffer
[4][50];
90 static const char hex
[] = "0123456789abcdef";
91 char *buffer
= hexbuffer
[3 & ++bufno
], *buf
= buffer
;
94 for (i
= 0; i
< 20; i
++) {
95 unsigned int val
= *sha1
++;
96 *buf
++ = hex
[val
>> 4];
97 *buf
++ = hex
[val
& 0xf];
104 static void fill_sha1_path(char *pathbuf
, const unsigned char *sha1
)
107 for (i
= 0; i
< 20; i
++) {
108 static char hex
[] = "0123456789abcdef";
109 unsigned int val
= sha1
[i
];
110 char *pos
= pathbuf
+ i
*2 + (i
> 0);
111 *pos
++ = hex
[val
>> 4];
112 *pos
= hex
[val
& 0xf];
117 * NOTE! This returns a statically allocated buffer, so you have to be
118 * careful about using it. Do a "strdup()" if you need to save the
121 * Also note that this returns the location for creating. Reading
122 * SHA1 file can happen from any alternate directory listed in the
123 * DB_ENVIRONMENT environment variable if it is not found in
124 * the primary object database.
126 char *sha1_file_name(const unsigned char *sha1
)
128 static char *name
, *base
;
129 static const char *last_objdir
;
130 const char *sha1_file_directory
= get_object_directory();
132 if (!last_objdir
|| strcmp(last_objdir
, sha1_file_directory
)) {
133 int len
= strlen(sha1_file_directory
);
136 base
= xmalloc(len
+ 60);
137 memcpy(base
, sha1_file_directory
, len
);
138 memset(base
+len
, 0, 60);
141 name
= base
+ len
+ 1;
143 free((char *) last_objdir
);
144 last_objdir
= strdup(sha1_file_directory
);
146 fill_sha1_path(name
, sha1
);
150 char *sha1_pack_name(const unsigned char *sha1
)
152 static const char hex
[] = "0123456789abcdef";
153 static char *name
, *base
, *buf
;
154 static const char *last_objdir
;
155 const char *sha1_file_directory
= get_object_directory();
158 if (!last_objdir
|| strcmp(last_objdir
, sha1_file_directory
)) {
159 int len
= strlen(sha1_file_directory
);
162 base
= xmalloc(len
+ 60);
163 sprintf(base
, "%s/pack/pack-1234567890123456789012345678901234567890.pack", sha1_file_directory
);
164 name
= base
+ len
+ 11;
166 free((char *) last_objdir
);
167 last_objdir
= strdup(sha1_file_directory
);
172 for (i
= 0; i
< 20; i
++) {
173 unsigned int val
= *sha1
++;
174 *buf
++ = hex
[val
>> 4];
175 *buf
++ = hex
[val
& 0xf];
181 char *sha1_pack_index_name(const unsigned char *sha1
)
183 static const char hex
[] = "0123456789abcdef";
184 static char *name
, *base
, *buf
;
185 static const char *last_objdir
;
186 const char *sha1_file_directory
= get_object_directory();
189 if (!last_objdir
|| strcmp(last_objdir
, sha1_file_directory
)) {
190 int len
= strlen(sha1_file_directory
);
193 base
= xmalloc(len
+ 60);
194 sprintf(base
, "%s/pack/pack-1234567890123456789012345678901234567890.idx", sha1_file_directory
);
195 name
= base
+ len
+ 11;
197 free((char *) last_objdir
);
198 last_objdir
= strdup(sha1_file_directory
);
203 for (i
= 0; i
< 20; i
++) {
204 unsigned int val
= *sha1
++;
205 *buf
++ = hex
[val
>> 4];
206 *buf
++ = hex
[val
& 0xf];
212 struct alternate_object_database
*alt_odb_list
;
213 static struct alternate_object_database
**alt_odb_tail
;
215 static void read_info_alternates(const char * alternates
, int depth
);
218 * Prepare alternate object database registry.
220 * The variable alt_odb_list points at the list of struct
221 * alternate_object_database. The elements on this list come from
222 * non-empty elements from colon separated ALTERNATE_DB_ENVIRONMENT
223 * environment variable, and $GIT_OBJECT_DIRECTORY/info/alternates,
224 * whose contents is similar to that environment variable but can be
225 * LF separated. Its base points at a statically allocated buffer that
226 * contains "/the/directory/corresponding/to/.git/objects/...", while
227 * its name points just after the slash at the end of ".git/objects/"
228 * in the example above, and has enough space to hold 40-byte hex
229 * SHA1, an extra slash for the first level indirection, and the
232 static int link_alt_odb_entry(const char * entry
, int len
, const char * relative_base
, int depth
)
235 const char *objdir
= get_object_directory();
236 struct alternate_object_database
*ent
;
237 struct alternate_object_database
*alt
;
238 /* 43 = 40-byte + 2 '/' + terminating NUL */
240 int entlen
= pfxlen
+ 43;
243 if (*entry
!= '/' && relative_base
) {
244 /* Relative alt-odb */
246 base_len
= strlen(relative_base
) + 1;
250 ent
= xmalloc(sizeof(*ent
) + entlen
);
252 if (*entry
!= '/' && relative_base
) {
253 memcpy(ent
->base
, relative_base
, base_len
- 1);
254 ent
->base
[base_len
- 1] = '/';
255 memcpy(ent
->base
+ base_len
, entry
, len
);
258 memcpy(ent
->base
, entry
, pfxlen
);
260 ent
->name
= ent
->base
+ pfxlen
+ 1;
261 ent
->base
[pfxlen
+ 3] = '/';
262 ent
->base
[pfxlen
] = ent
->base
[entlen
-1] = 0;
264 /* Detect cases where alternate disappeared */
265 if (stat(ent
->base
, &st
) || !S_ISDIR(st
.st_mode
)) {
266 error("object directory %s does not exist; "
267 "check .git/objects/info/alternates.",
273 /* Prevent the common mistake of listing the same
274 * thing twice, or object directory itself.
276 for (alt
= alt_odb_list
; alt
; alt
= alt
->next
) {
277 if (!memcmp(ent
->base
, alt
->base
, pfxlen
)) {
282 if (!memcmp(ent
->base
, objdir
, pfxlen
)) {
287 /* add the alternate entry */
289 alt_odb_tail
= &(ent
->next
);
292 /* recursively add alternates */
293 read_info_alternates(ent
->base
, depth
+ 1);
295 ent
->base
[pfxlen
] = '/';
300 static void link_alt_odb_entries(const char *alt
, const char *ep
, int sep
,
301 const char *relative_base
, int depth
)
303 const char *cp
, *last
;
306 error("%s: ignoring alternate object stores, nesting too deep.",
314 if (cp
< ep
&& *cp
== '#') {
315 while (cp
< ep
&& *cp
!= sep
)
320 while (cp
< ep
&& *cp
!= sep
)
323 if ((*last
!= '/') && depth
) {
324 error("%s: ignoring relative alternate object store %s",
325 relative_base
, last
);
327 link_alt_odb_entry(last
, cp
- last
,
328 relative_base
, depth
);
331 while (cp
< ep
&& *cp
== sep
)
337 static void read_info_alternates(const char * relative_base
, int depth
)
344 sprintf(path
, "%s/info/alternates", relative_base
);
345 fd
= open(path
, O_RDONLY
);
348 if (fstat(fd
, &st
) || (st
.st_size
== 0)) {
352 map
= mmap(NULL
, st
.st_size
, PROT_READ
, MAP_PRIVATE
, fd
, 0);
354 if (map
== MAP_FAILED
)
357 link_alt_odb_entries(map
, map
+ st
.st_size
, '\n', relative_base
, depth
);
359 munmap(map
, st
.st_size
);
362 void prepare_alt_odb(void)
366 alt
= getenv(ALTERNATE_DB_ENVIRONMENT
);
371 alt_odb_tail
= &alt_odb_list
;
372 link_alt_odb_entries(alt
, alt
+ strlen(alt
), ':', NULL
, 0);
374 read_info_alternates(get_object_directory(), 0);
377 static char *find_sha1_file(const unsigned char *sha1
, struct stat
*st
)
379 char *name
= sha1_file_name(sha1
);
380 struct alternate_object_database
*alt
;
385 for (alt
= alt_odb_list
; alt
; alt
= alt
->next
) {
387 fill_sha1_path(name
, sha1
);
388 if (!stat(alt
->base
, st
))
394 #define PACK_MAX_SZ (1<<26)
395 static int pack_used_ctr
;
396 static unsigned long pack_mapped
;
397 struct packed_git
*packed_git
;
399 static int check_packed_git_idx(const char *path
, unsigned long *idx_size_
,
404 unsigned long idx_size
;
406 int fd
= open(path
, O_RDONLY
);
410 if (fstat(fd
, &st
)) {
414 idx_size
= st
.st_size
;
415 idx_map
= mmap(NULL
, idx_size
, PROT_READ
, MAP_PRIVATE
, fd
, 0);
417 if (idx_map
== MAP_FAILED
)
422 *idx_size_
= idx_size
;
424 /* check index map */
425 if (idx_size
< 4*256 + 20 + 20)
426 return error("index file too small");
428 for (i
= 0; i
< 256; i
++) {
429 unsigned int n
= ntohl(index
[i
]);
431 return error("non-monotonic index");
437 * - 256 index entries 4 bytes each
438 * - 24-byte entries * nr (20-byte sha1 + 4-byte offset)
439 * - 20-byte SHA1 of the packfile
440 * - 20-byte SHA1 file checksum
442 if (idx_size
!= 4*256 + nr
* 24 + 20 + 20)
443 return error("wrong index file size");
448 static int unuse_one_packed_git(void)
450 struct packed_git
*p
, *lru
= NULL
;
452 for (p
= packed_git
; p
; p
= p
->next
) {
453 if (p
->pack_use_cnt
|| !p
->pack_base
)
455 if (!lru
|| p
->pack_last_used
< lru
->pack_last_used
)
460 munmap(lru
->pack_base
, lru
->pack_size
);
461 lru
->pack_base
= NULL
;
465 void unuse_packed_git(struct packed_git
*p
)
470 int use_packed_git(struct packed_git
*p
)
474 /* We created the struct before we had the pack */
475 stat(p
->pack_name
, &st
);
476 if (!S_ISREG(st
.st_mode
))
477 die("packfile %s not a regular file", p
->pack_name
);
478 p
->pack_size
= st
.st_size
;
484 struct pack_header
*hdr
;
486 pack_mapped
+= p
->pack_size
;
487 while (PACK_MAX_SZ
< pack_mapped
&& unuse_one_packed_git())
489 fd
= open(p
->pack_name
, O_RDONLY
);
491 die("packfile %s cannot be opened", p
->pack_name
);
492 if (fstat(fd
, &st
)) {
494 die("packfile %s cannot be opened", p
->pack_name
);
496 if (st
.st_size
!= p
->pack_size
)
497 die("packfile %s size mismatch.", p
->pack_name
);
498 map
= mmap(NULL
, p
->pack_size
, PROT_READ
, MAP_PRIVATE
, fd
, 0);
500 if (map
== MAP_FAILED
)
501 die("packfile %s cannot be mapped.", p
->pack_name
);
504 /* Check if we understand this pack file. If we don't we're
505 * likely too old to handle it.
508 if (hdr
->hdr_signature
!= htonl(PACK_SIGNATURE
))
509 die("packfile %s isn't actually a pack.", p
->pack_name
);
510 if (!pack_version_ok(hdr
->hdr_version
))
511 die("packfile %s is version %i and not supported"
512 " (try upgrading GIT to a newer version)",
513 p
->pack_name
, ntohl(hdr
->hdr_version
));
515 /* Check if the pack file matches with the index file.
518 if (hashcmp((unsigned char *)(p
->index_base
) +
520 (unsigned char *)p
->pack_base
+
521 p
->pack_size
- 20)) {
522 die("packfile %s does not match index.", p
->pack_name
);
525 p
->pack_last_used
= pack_used_ctr
++;
530 struct packed_git
*add_packed_git(char *path
, int path_len
, int local
)
533 struct packed_git
*p
;
534 unsigned long idx_size
;
536 unsigned char sha1
[20];
538 if (check_packed_git_idx(path
, &idx_size
, &idx_map
))
541 /* do we have a corresponding .pack file? */
542 strcpy(path
+ path_len
- 4, ".pack");
543 if (stat(path
, &st
) || !S_ISREG(st
.st_mode
)) {
544 munmap(idx_map
, idx_size
);
547 /* ok, it looks sane as far as we can check without
548 * actually mapping the pack file.
550 p
= xmalloc(sizeof(*p
) + path_len
+ 2);
551 strcpy(p
->pack_name
, path
);
552 p
->index_size
= idx_size
;
553 p
->pack_size
= st
.st_size
;
554 p
->index_base
= idx_map
;
557 p
->pack_last_used
= 0;
559 p
->pack_local
= local
;
560 if ((path_len
> 44) && !get_sha1_hex(path
+ path_len
- 44, sha1
))
561 hashcpy(p
->sha1
, sha1
);
565 struct packed_git
*parse_pack_index(unsigned char *sha1
)
567 char *path
= sha1_pack_index_name(sha1
);
568 return parse_pack_index_file(sha1
, path
);
571 struct packed_git
*parse_pack_index_file(const unsigned char *sha1
, char *idx_path
)
573 struct packed_git
*p
;
574 unsigned long idx_size
;
578 if (check_packed_git_idx(idx_path
, &idx_size
, &idx_map
))
581 path
= sha1_pack_name(sha1
);
583 p
= xmalloc(sizeof(*p
) + strlen(path
) + 2);
584 strcpy(p
->pack_name
, path
);
585 p
->index_size
= idx_size
;
587 p
->index_base
= idx_map
;
590 p
->pack_last_used
= 0;
592 hashcpy(p
->sha1
, sha1
);
596 void install_packed_git(struct packed_git
*pack
)
598 pack
->next
= packed_git
;
602 static void prepare_packed_git_one(char *objdir
, int local
)
609 sprintf(path
, "%s/pack", objdir
);
614 error("unable to open object pack directory: %s: %s",
615 path
, strerror(errno
));
619 while ((de
= readdir(dir
)) != NULL
) {
620 int namelen
= strlen(de
->d_name
);
621 struct packed_git
*p
;
623 if (!has_extension(de
->d_name
, ".idx"))
626 /* we have .idx. Is it a file we can map? */
627 strcpy(path
+ len
, de
->d_name
);
628 for (p
= packed_git
; p
; p
= p
->next
) {
629 if (!memcmp(path
, p
->pack_name
, len
+ namelen
- 4))
634 p
= add_packed_git(path
, len
+ namelen
, local
);
637 p
->next
= packed_git
;
643 static int prepare_packed_git_run_once
= 0;
644 void prepare_packed_git(void)
646 struct alternate_object_database
*alt
;
648 if (prepare_packed_git_run_once
)
650 prepare_packed_git_one(get_object_directory(), 1);
652 for (alt
= alt_odb_list
; alt
; alt
= alt
->next
) {
654 prepare_packed_git_one(alt
->base
, 0);
657 prepare_packed_git_run_once
= 1;
660 static void reprepare_packed_git(void)
662 prepare_packed_git_run_once
= 0;
663 prepare_packed_git();
666 int check_sha1_signature(const unsigned char *sha1
, void *map
, unsigned long size
, const char *type
)
669 unsigned char real_sha1
[20];
673 SHA1_Update(&c
, header
, 1+sprintf(header
, "%s %lu", type
, size
));
674 SHA1_Update(&c
, map
, size
);
675 SHA1_Final(real_sha1
, &c
);
676 return hashcmp(sha1
, real_sha1
) ? -1 : 0;
679 void *map_sha1_file(const unsigned char *sha1
, unsigned long *size
)
684 char *filename
= find_sha1_file(sha1
, &st
);
690 fd
= open(filename
, O_RDONLY
| sha1_file_open_flag
);
692 /* See if it works without O_NOATIME */
693 switch (sha1_file_open_flag
) {
695 fd
= open(filename
, O_RDONLY
);
703 /* If it failed once, it will probably fail again.
704 * Stop using O_NOATIME
706 sha1_file_open_flag
= 0;
708 map
= mmap(NULL
, st
.st_size
, PROT_READ
, MAP_PRIVATE
, fd
, 0);
710 if (map
== MAP_FAILED
)
716 int legacy_loose_object(unsigned char *map
)
721 * Is it a zlib-compressed buffer? If so, the first byte
722 * must be 0x78 (15-bit window size, deflated), and the
723 * first 16-bit word is evenly divisible by 31
725 word
= (map
[0] << 8) + map
[1];
726 if (map
[0] == 0x78 && !(word
% 31))
732 static int unpack_sha1_header(z_stream
*stream
, unsigned char *map
, unsigned long mapsize
, void *buffer
, unsigned long bufsiz
)
737 static const char *typename
[8] = {
739 "commit", "tree", "blob", "tag",
744 /* Get the data stream */
745 memset(stream
, 0, sizeof(*stream
));
746 stream
->next_in
= map
;
747 stream
->avail_in
= mapsize
;
748 stream
->next_out
= buffer
;
749 stream
->avail_out
= bufsiz
;
751 if (legacy_loose_object(map
)) {
753 return inflate(stream
, 0);
758 type
= typename
[(c
>> 4) & 7];
765 if (bits
>= 8*sizeof(long))
768 size
+= (c
& 0x7f) << bits
;
773 /* Set up the stream for the rest.. */
774 stream
->next_in
= map
;
775 stream
->avail_in
= mapsize
;
778 /* And generate the fake traditional header */
779 stream
->total_out
= 1 + snprintf(buffer
, bufsiz
, "%s %lu", type
, size
);
783 static void *unpack_sha1_rest(z_stream
*stream
, void *buffer
, unsigned long size
)
785 int bytes
= strlen(buffer
) + 1;
786 unsigned char *buf
= xmalloc(1+size
);
789 n
= stream
->total_out
- bytes
;
792 memcpy(buf
, (char *) buffer
+ bytes
, n
);
795 stream
->next_out
= buf
+ bytes
;
796 stream
->avail_out
= size
- bytes
;
797 while (inflate(stream
, Z_FINISH
) == Z_OK
)
806 * We used to just use "sscanf()", but that's actually way
807 * too permissive for what we want to check. So do an anal
808 * object header parse by hand.
810 static int parse_sha1_header(char *hdr
, char *type
, unsigned long *sizep
)
816 * The type can be at most ten bytes (including the
817 * terminating '\0' that we add), and is followed by
832 * The length must follow immediately, and be in canonical
833 * decimal format (ie "010" is not valid).
840 unsigned long c
= *hdr
- '0';
844 size
= size
* 10 + c
;
850 * The length must be followed by a zero byte
852 return *hdr
? -1 : 0;
855 void * unpack_sha1_file(void *map
, unsigned long mapsize
, char *type
, unsigned long *size
)
861 ret
= unpack_sha1_header(&stream
, map
, mapsize
, hdr
, sizeof(hdr
));
862 if (ret
< Z_OK
|| parse_sha1_header(hdr
, type
, size
) < 0)
865 return unpack_sha1_rest(&stream
, hdr
, *size
);
868 /* forward declaration for a mutually recursive function */
869 static int packed_object_info(struct pack_entry
*entry
,
870 char *type
, unsigned long *sizep
);
872 static int packed_delta_info(unsigned char *base_sha1
,
873 unsigned long delta_size
,
876 unsigned long *sizep
,
877 struct packed_git
*p
)
879 struct pack_entry base_ent
;
882 die("truncated pack file");
884 /* The base entry _must_ be in the same pack */
885 if (!find_pack_entry_one(base_sha1
, &base_ent
, p
))
886 die("failed to find delta-pack base object %s",
887 sha1_to_hex(base_sha1
));
889 /* We choose to only get the type of the base object and
890 * ignore potentially corrupt pack file that expects the delta
891 * based on a base with a wrong size. This saves tons of
895 if (packed_object_info(&base_ent
, type
, NULL
))
896 die("cannot get info for delta-pack base");
899 const unsigned char *data
;
900 unsigned char delta_head
[64];
901 unsigned long result_size
;
905 memset(&stream
, 0, sizeof(stream
));
907 data
= stream
.next_in
= base_sha1
+ 20;
908 stream
.avail_in
= left
- 20;
909 stream
.next_out
= delta_head
;
910 stream
.avail_out
= sizeof(delta_head
);
912 inflateInit(&stream
);
913 st
= inflate(&stream
, Z_FINISH
);
915 if ((st
!= Z_STREAM_END
) &&
916 stream
.total_out
!= sizeof(delta_head
))
917 die("delta data unpack-initial failed");
919 /* Examine the initial part of the delta to figure out
924 /* ignore base size */
925 get_delta_hdr_size(&data
, delta_head
+sizeof(delta_head
));
927 /* Read the result size */
928 result_size
= get_delta_hdr_size(&data
, delta_head
+sizeof(delta_head
));
929 *sizep
= result_size
;
934 static unsigned long unpack_object_header(struct packed_git
*p
, unsigned long offset
,
935 enum object_type
*type
, unsigned long *sizep
)
941 if (offset
>= p
->pack_size
)
942 die("object offset outside of pack file");
943 c
= *((unsigned char *)p
->pack_base
+ offset
++);
944 *type
= (c
>> 4) & 7;
948 if (offset
>= p
->pack_size
)
949 die("object offset outside of pack file");
950 c
= *((unsigned char *)p
->pack_base
+ offset
++);
951 size
+= (c
& 0x7f) << shift
;
958 int check_reuse_pack_delta(struct packed_git
*p
, unsigned long offset
,
959 unsigned char *base
, unsigned long *sizep
,
960 enum object_type
*kindp
)
967 ptr
= unpack_object_header(p
, ptr
, kindp
, sizep
);
968 if (*kindp
!= OBJ_DELTA
)
970 hashcpy(base
, (unsigned char *) p
->pack_base
+ ptr
);
977 void packed_object_info_detail(struct pack_entry
*e
,
980 unsigned long *store_size
,
981 unsigned int *delta_chain_length
,
982 unsigned char *base_sha1
)
984 struct packed_git
*p
= e
->p
;
985 unsigned long offset
;
987 enum object_type kind
;
989 offset
= unpack_object_header(p
, e
->offset
, &kind
, size
);
990 pack
= (unsigned char *) p
->pack_base
+ offset
;
991 if (kind
!= OBJ_DELTA
)
992 *delta_chain_length
= 0;
994 unsigned int chain_length
= 0;
995 if (p
->pack_size
<= offset
+ 20)
996 die("pack file %s records an incomplete delta base",
998 hashcpy(base_sha1
, pack
);
1000 struct pack_entry base_ent
;
1003 find_pack_entry_one(pack
, &base_ent
, p
);
1004 offset
= unpack_object_header(p
, base_ent
.offset
,
1006 pack
= (unsigned char *) p
->pack_base
+ offset
;
1008 } while (kind
== OBJ_DELTA
);
1009 *delta_chain_length
= chain_length
;
1016 strcpy(type
, type_names
[kind
]);
1019 die("corrupted pack file %s containing object of kind %d",
1020 p
->pack_name
, kind
);
1022 *store_size
= 0; /* notyet */
1025 static int packed_object_info(struct pack_entry
*entry
,
1026 char *type
, unsigned long *sizep
)
1028 struct packed_git
*p
= entry
->p
;
1029 unsigned long offset
, size
, left
;
1030 unsigned char *pack
;
1031 enum object_type kind
;
1034 if (use_packed_git(p
))
1035 die("cannot map packed file");
1037 offset
= unpack_object_header(p
, entry
->offset
, &kind
, &size
);
1038 pack
= (unsigned char *) p
->pack_base
+ offset
;
1039 left
= p
->pack_size
- offset
;
1043 retval
= packed_delta_info(pack
, size
, left
, type
, sizep
, p
);
1044 unuse_packed_git(p
);
1050 strcpy(type
, type_names
[kind
]);
1053 die("corrupted pack file %s containing object of kind %d",
1054 p
->pack_name
, kind
);
1058 unuse_packed_git(p
);
1062 static void *unpack_compressed_entry(struct packed_git
*p
,
1063 unsigned long offset
,
1068 unsigned char *buffer
;
1070 buffer
= xmalloc(size
+ 1);
1072 memset(&stream
, 0, sizeof(stream
));
1073 stream
.next_in
= (unsigned char*)p
->pack_base
+ offset
;
1074 stream
.avail_in
= p
->pack_size
- offset
;
1075 stream
.next_out
= buffer
;
1076 stream
.avail_out
= size
;
1078 inflateInit(&stream
);
1079 st
= inflate(&stream
, Z_FINISH
);
1080 inflateEnd(&stream
);
1081 if ((st
!= Z_STREAM_END
) || stream
.total_out
!= size
) {
1089 static void *unpack_delta_entry(struct packed_git
*p
,
1090 unsigned long offset
,
1091 unsigned long delta_size
,
1093 unsigned long *sizep
)
1095 struct pack_entry base_ent
;
1096 void *delta_data
, *result
, *base
;
1097 unsigned long result_size
, base_size
;
1098 unsigned char* base_sha1
;
1100 if ((offset
+ 20) >= p
->pack_size
)
1101 die("truncated pack file");
1103 /* The base entry _must_ be in the same pack */
1104 base_sha1
= (unsigned char*)p
->pack_base
+ offset
;
1105 if (!find_pack_entry_one(base_sha1
, &base_ent
, p
))
1106 die("failed to find delta-pack base object %s",
1107 sha1_to_hex(base_sha1
));
1108 base
= unpack_entry_gently(&base_ent
, type
, &base_size
);
1110 die("failed to read delta-pack base object %s",
1111 sha1_to_hex(base_sha1
));
1113 delta_data
= unpack_compressed_entry(p
, offset
+ 20, delta_size
);
1114 result
= patch_delta(base
, base_size
,
1115 delta_data
, delta_size
,
1118 die("failed to apply delta");
1121 *sizep
= result_size
;
1125 static void *unpack_entry(struct pack_entry
*entry
,
1126 char *type
, unsigned long *sizep
)
1128 struct packed_git
*p
= entry
->p
;
1131 if (use_packed_git(p
))
1132 die("cannot map packed file");
1133 retval
= unpack_entry_gently(entry
, type
, sizep
);
1134 unuse_packed_git(p
);
1136 die("corrupted pack file %s", p
->pack_name
);
1140 /* The caller is responsible for use_packed_git()/unuse_packed_git() pair */
1141 void *unpack_entry_gently(struct pack_entry
*entry
,
1142 char *type
, unsigned long *sizep
)
1144 struct packed_git
*p
= entry
->p
;
1145 unsigned long offset
, size
;
1146 enum object_type kind
;
1148 offset
= unpack_object_header(p
, entry
->offset
, &kind
, &size
);
1151 return unpack_delta_entry(p
, offset
, size
, type
, sizep
);
1156 strcpy(type
, type_names
[kind
]);
1158 return unpack_compressed_entry(p
, offset
, size
);
1164 int num_packed_objects(const struct packed_git
*p
)
1166 /* See check_packed_git_idx() */
1167 return (p
->index_size
- 20 - 20 - 4*256) / 24;
1170 int nth_packed_object_sha1(const struct packed_git
*p
, int n
,
1171 unsigned char* sha1
)
1173 void *index
= p
->index_base
+ 256;
1174 if (n
< 0 || num_packed_objects(p
) <= n
)
1176 hashcpy(sha1
, (unsigned char *) index
+ (24 * n
) + 4);
1180 int find_pack_entry_one(const unsigned char *sha1
,
1181 struct pack_entry
*e
, struct packed_git
*p
)
1183 unsigned int *level1_ofs
= p
->index_base
;
1184 int hi
= ntohl(level1_ofs
[*sha1
]);
1185 int lo
= ((*sha1
== 0x0) ? 0 : ntohl(level1_ofs
[*sha1
- 1]));
1186 void *index
= p
->index_base
+ 256;
1189 int mi
= (lo
+ hi
) / 2;
1190 int cmp
= hashcmp((unsigned char *)index
+ (24 * mi
) + 4, sha1
);
1192 e
->offset
= ntohl(*((unsigned int *) ((char *) index
+ (24 * mi
))));
1193 hashcpy(e
->sha1
, sha1
);
1205 static int find_pack_entry(const unsigned char *sha1
, struct pack_entry
*e
)
1207 struct packed_git
*p
;
1208 prepare_packed_git();
1210 for (p
= packed_git
; p
; p
= p
->next
) {
1211 if (find_pack_entry_one(sha1
, e
, p
))
1217 struct packed_git
*find_sha1_pack(const unsigned char *sha1
,
1218 struct packed_git
*packs
)
1220 struct packed_git
*p
;
1221 struct pack_entry e
;
1223 for (p
= packs
; p
; p
= p
->next
) {
1224 if (find_pack_entry_one(sha1
, &e
, p
))
1231 int sha1_object_info(const unsigned char *sha1
, char *type
, unsigned long *sizep
)
1234 unsigned long mapsize
, size
;
1239 map
= map_sha1_file(sha1
, &mapsize
);
1241 struct pack_entry e
;
1243 if (find_pack_entry(sha1
, &e
))
1244 return packed_object_info(&e
, type
, sizep
);
1245 reprepare_packed_git();
1246 if (find_pack_entry(sha1
, &e
))
1247 return packed_object_info(&e
, type
, sizep
);
1248 return error("unable to find %s", sha1_to_hex(sha1
));
1250 if (unpack_sha1_header(&stream
, map
, mapsize
, hdr
, sizeof(hdr
)) < 0)
1251 status
= error("unable to unpack %s header",
1253 if (parse_sha1_header(hdr
, type
, &size
) < 0)
1254 status
= error("unable to parse %s header", sha1_to_hex(sha1
));
1260 inflateEnd(&stream
);
1261 munmap(map
, mapsize
);
1265 static void *read_packed_sha1(const unsigned char *sha1
, char *type
, unsigned long *size
)
1267 struct pack_entry e
;
1269 if (!find_pack_entry(sha1
, &e
)) {
1270 error("cannot read sha1_file for %s", sha1_to_hex(sha1
));
1273 return unpack_entry(&e
, type
, size
);
1276 void * read_sha1_file(const unsigned char *sha1
, char *type
, unsigned long *size
)
1278 unsigned long mapsize
;
1280 struct pack_entry e
;
1282 if (find_pack_entry(sha1
, &e
))
1283 return read_packed_sha1(sha1
, type
, size
);
1284 map
= map_sha1_file(sha1
, &mapsize
);
1286 buf
= unpack_sha1_file(map
, mapsize
, type
, size
);
1287 munmap(map
, mapsize
);
1290 reprepare_packed_git();
1291 if (find_pack_entry(sha1
, &e
))
1292 return read_packed_sha1(sha1
, type
, size
);
1296 void *read_object_with_reference(const unsigned char *sha1
,
1297 const char *required_type
,
1298 unsigned long *size
,
1299 unsigned char *actual_sha1_return
)
1303 unsigned long isize
;
1304 unsigned char actual_sha1
[20];
1306 hashcpy(actual_sha1
, sha1
);
1308 int ref_length
= -1;
1309 const char *ref_type
= NULL
;
1311 buffer
= read_sha1_file(actual_sha1
, type
, &isize
);
1314 if (!strcmp(type
, required_type
)) {
1316 if (actual_sha1_return
)
1317 hashcpy(actual_sha1_return
, actual_sha1
);
1320 /* Handle references */
1321 else if (!strcmp(type
, commit_type
))
1323 else if (!strcmp(type
, tag_type
))
1324 ref_type
= "object ";
1329 ref_length
= strlen(ref_type
);
1331 if (memcmp(buffer
, ref_type
, ref_length
) ||
1332 get_sha1_hex((char *) buffer
+ ref_length
, actual_sha1
)) {
1337 /* Now we have the ID of the referred-to object in
1338 * actual_sha1. Check again. */
1342 char *write_sha1_file_prepare(void *buf
,
1345 unsigned char *sha1
,
1351 /* Generate the header */
1352 *hdrlen
= sprintf((char *)hdr
, "%s %lu", type
, len
)+1;
1356 SHA1_Update(&c
, hdr
, *hdrlen
);
1357 SHA1_Update(&c
, buf
, len
);
1358 SHA1_Final(sha1
, &c
);
1360 return sha1_file_name(sha1
);
1364 * Link the tempfile to the final place, possibly creating the
1365 * last directory level as you do so.
1367 * Returns the errno on failure, 0 on success.
1369 static int link_temp_to_file(const char *tmpfile
, char *filename
)
1374 if (!link(tmpfile
, filename
))
1378 * Try to mkdir the last path component if that failed.
1380 * Re-try the "link()" regardless of whether the mkdir
1381 * succeeds, since a race might mean that somebody
1385 dir
= strrchr(filename
, '/');
1388 mkdir(filename
, 0777);
1389 if (adjust_shared_perm(filename
))
1392 if (!link(tmpfile
, filename
))
1400 * Move the just written object into its final resting place
1402 int move_temp_to_file(const char *tmpfile
, char *filename
)
1404 int ret
= link_temp_to_file(tmpfile
, filename
);
1407 * Coda hack - coda doesn't like cross-directory links,
1408 * so we fall back to a rename, which will mean that it
1409 * won't be able to check collisions, but that's not a
1412 * The same holds for FAT formatted media.
1414 * When this succeeds, we just return 0. We have nothing
1417 if (ret
&& ret
!= EEXIST
) {
1418 if (!rename(tmpfile
, filename
))
1424 if (ret
!= EEXIST
) {
1425 fprintf(stderr
, "unable to write sha1 filename %s: %s\n", filename
, strerror(ret
));
1428 /* FIXME!!! Collision check here ? */
1434 static int write_buffer(int fd
, const void *buf
, size_t len
)
1439 size
= write(fd
, buf
, len
);
1441 return error("file write: disk full");
1443 if (errno
== EINTR
|| errno
== EAGAIN
)
1445 return error("file write error (%s)", strerror(errno
));
1448 buf
= (char *) buf
+ size
;
1453 static int write_binary_header(unsigned char *hdr
, enum object_type type
, unsigned long len
)
1458 c
= (type
<< 4) | (len
& 15);
1471 static void setup_object_header(z_stream
*stream
, const char *type
, unsigned long len
)
1475 if (use_legacy_headers
) {
1476 while (deflate(stream
, 0) == Z_OK
)
1480 if (!strcmp(type
, blob_type
))
1481 obj_type
= OBJ_BLOB
;
1482 else if (!strcmp(type
, tree_type
))
1483 obj_type
= OBJ_TREE
;
1484 else if (!strcmp(type
, commit_type
))
1485 obj_type
= OBJ_COMMIT
;
1486 else if (!strcmp(type
, tag_type
))
1489 die("trying to generate bogus object of type '%s'", type
);
1490 hdr
= write_binary_header(stream
->next_out
, obj_type
, len
);
1491 stream
->total_out
= hdr
;
1492 stream
->next_out
+= hdr
;
1493 stream
->avail_out
-= hdr
;
1496 int write_sha1_file(void *buf
, unsigned long len
, const char *type
, unsigned char *returnsha1
)
1499 unsigned char *compressed
;
1501 unsigned char sha1
[20];
1503 static char tmpfile
[PATH_MAX
];
1504 unsigned char hdr
[50];
1507 /* Normally if we have it in the pack then we do not bother writing
1508 * it out into .git/objects/??/?{38} file.
1510 filename
= write_sha1_file_prepare(buf
, len
, type
, sha1
, hdr
, &hdrlen
);
1512 hashcpy(returnsha1
, sha1
);
1513 if (has_sha1_file(sha1
))
1515 fd
= open(filename
, O_RDONLY
);
1518 * FIXME!!! We might do collision checking here, but we'd
1519 * need to uncompress the old file and check it. Later.
1525 if (errno
!= ENOENT
) {
1526 fprintf(stderr
, "sha1 file %s: %s\n", filename
, strerror(errno
));
1530 snprintf(tmpfile
, sizeof(tmpfile
), "%s/obj_XXXXXX", get_object_directory());
1532 fd
= mkstemp(tmpfile
);
1534 fprintf(stderr
, "unable to create temporary sha1 filename %s: %s\n", tmpfile
, strerror(errno
));
1539 memset(&stream
, 0, sizeof(stream
));
1540 deflateInit(&stream
, zlib_compression_level
);
1541 size
= 8 + deflateBound(&stream
, len
+hdrlen
);
1542 compressed
= xmalloc(size
);
1545 stream
.next_out
= compressed
;
1546 stream
.avail_out
= size
;
1548 /* First header.. */
1549 stream
.next_in
= hdr
;
1550 stream
.avail_in
= hdrlen
;
1551 setup_object_header(&stream
, type
, len
);
1553 /* Then the data itself.. */
1554 stream
.next_in
= buf
;
1555 stream
.avail_in
= len
;
1556 while (deflate(&stream
, Z_FINISH
) == Z_OK
)
1558 deflateEnd(&stream
);
1559 size
= stream
.total_out
;
1561 if (write_buffer(fd
, compressed
, size
) < 0)
1562 die("unable to write sha1 file");
1567 return move_temp_to_file(tmpfile
, filename
);
1571 * We need to unpack and recompress the object for writing
1572 * it out to a different file.
1574 static void *repack_object(const unsigned char *sha1
, unsigned long *objsize
)
1578 unsigned char *unpacked
;
1585 /* need to unpack and recompress it by itself */
1586 unpacked
= read_packed_sha1(sha1
, type
, &len
);
1588 hdrlen
= sprintf(hdr
, "%s %lu", type
, len
) + 1;
1591 memset(&stream
, 0, sizeof(stream
));
1592 deflateInit(&stream
, zlib_compression_level
);
1593 size
= deflateBound(&stream
, len
+ hdrlen
);
1594 buf
= xmalloc(size
);
1597 stream
.next_out
= buf
;
1598 stream
.avail_out
= size
;
1600 /* First header.. */
1601 stream
.next_in
= (void *)hdr
;
1602 stream
.avail_in
= hdrlen
;
1603 while (deflate(&stream
, 0) == Z_OK
)
1606 /* Then the data itself.. */
1607 stream
.next_in
= unpacked
;
1608 stream
.avail_in
= len
;
1609 while (deflate(&stream
, Z_FINISH
) == Z_OK
)
1611 deflateEnd(&stream
);
1614 *objsize
= stream
.total_out
;
1618 int write_sha1_to_fd(int fd
, const unsigned char *sha1
)
1621 unsigned long objsize
;
1622 void *buf
= map_sha1_file(sha1
, &objsize
);
1625 retval
= write_buffer(fd
, buf
, objsize
);
1626 munmap(buf
, objsize
);
1630 buf
= repack_object(sha1
, &objsize
);
1631 retval
= write_buffer(fd
, buf
, objsize
);
1636 int write_sha1_from_fd(const unsigned char *sha1
, int fd
, char *buffer
,
1637 size_t bufsize
, size_t *bufposn
)
1639 char tmpfile
[PATH_MAX
];
1642 unsigned char real_sha1
[20];
1643 unsigned char discard
[4096];
1647 snprintf(tmpfile
, sizeof(tmpfile
), "%s/obj_XXXXXX", get_object_directory());
1649 local
= mkstemp(tmpfile
);
1651 return error("Couldn't open %s for %s",
1652 tmpfile
, sha1_to_hex(sha1
));
1654 memset(&stream
, 0, sizeof(stream
));
1656 inflateInit(&stream
);
1663 stream
.avail_in
= *bufposn
;
1664 stream
.next_in
= (unsigned char *) buffer
;
1666 stream
.next_out
= discard
;
1667 stream
.avail_out
= sizeof(discard
);
1668 ret
= inflate(&stream
, Z_SYNC_FLUSH
);
1669 SHA1_Update(&c
, discard
, sizeof(discard
) -
1671 } while (stream
.avail_in
&& ret
== Z_OK
);
1672 if (write_buffer(local
, buffer
, *bufposn
- stream
.avail_in
) < 0)
1673 die("unable to write sha1 file");
1674 memmove(buffer
, buffer
+ *bufposn
- stream
.avail_in
,
1676 *bufposn
= stream
.avail_in
;
1680 size
= read(fd
, buffer
+ *bufposn
, bufsize
- *bufposn
);
1685 return error("Connection closed?");
1686 perror("Reading from connection");
1691 inflateEnd(&stream
);
1694 SHA1_Final(real_sha1
, &c
);
1695 if (ret
!= Z_STREAM_END
) {
1697 return error("File %s corrupted", sha1_to_hex(sha1
));
1699 if (hashcmp(sha1
, real_sha1
)) {
1701 return error("File %s has bad hash", sha1_to_hex(sha1
));
1704 return move_temp_to_file(tmpfile
, sha1_file_name(sha1
));
1707 int has_pack_index(const unsigned char *sha1
)
1710 if (stat(sha1_pack_index_name(sha1
), &st
))
1715 int has_pack_file(const unsigned char *sha1
)
1718 if (stat(sha1_pack_name(sha1
), &st
))
1723 int has_sha1_pack(const unsigned char *sha1
)
1725 struct pack_entry e
;
1726 return find_pack_entry(sha1
, &e
);
1729 int has_sha1_file(const unsigned char *sha1
)
1732 struct pack_entry e
;
1734 if (find_pack_entry(sha1
, &e
))
1736 return find_sha1_file(sha1
, &st
) ? 1 : 0;
1740 * reads from fd as long as possible into a supplied buffer of size bytes.
1741 * If necessary the buffer's size is increased using realloc()
1743 * returns 0 if anything went fine and -1 otherwise
1745 * NOTE: both buf and size may change, but even when -1 is returned
1746 * you still have to free() it yourself.
1748 int read_pipe(int fd
, char** return_buf
, unsigned long* return_size
)
1750 char* buf
= *return_buf
;
1751 unsigned long size
= *return_size
;
1753 unsigned long off
= 0;
1756 iret
= xread(fd
, buf
+ off
, size
- off
);
1761 buf
= xrealloc(buf
, size
);
1774 int index_pipe(unsigned char *sha1
, int fd
, const char *type
, int write_object
)
1776 unsigned long size
= 4096;
1777 char *buf
= malloc(size
);
1779 unsigned char hdr
[50];
1782 if (read_pipe(fd
, &buf
, &size
)) {
1790 ret
= write_sha1_file(buf
, size
, type
, sha1
);
1792 write_sha1_file_prepare(buf
, size
, type
, sha1
, hdr
, &hdrlen
);
1799 int index_fd(unsigned char *sha1
, int fd
, struct stat
*st
, int write_object
, const char *type
)
1801 unsigned long size
= st
->st_size
;
1804 unsigned char hdr
[50];
1809 buf
= mmap(NULL
, size
, PROT_READ
, MAP_PRIVATE
, fd
, 0);
1811 if (buf
== MAP_FAILED
)
1817 ret
= write_sha1_file(buf
, size
, type
, sha1
);
1819 write_sha1_file_prepare(buf
, size
, type
, sha1
, hdr
, &hdrlen
);
1827 int index_path(unsigned char *sha1
, const char *path
, struct stat
*st
, int write_object
)
1832 switch (st
->st_mode
& S_IFMT
) {
1834 fd
= open(path
, O_RDONLY
);
1836 return error("open(\"%s\"): %s", path
,
1838 if (index_fd(sha1
, fd
, st
, write_object
, NULL
) < 0)
1839 return error("%s: failed to insert into database",
1843 target
= xmalloc(st
->st_size
+1);
1844 if (readlink(path
, target
, st
->st_size
+1) != st
->st_size
) {
1845 char *errstr
= strerror(errno
);
1847 return error("readlink(\"%s\"): %s", path
,
1850 if (!write_object
) {
1851 unsigned char hdr
[50];
1853 write_sha1_file_prepare(target
, st
->st_size
, blob_type
,
1854 sha1
, hdr
, &hdrlen
);
1855 } else if (write_sha1_file(target
, st
->st_size
, blob_type
, sha1
))
1856 return error("%s: failed to insert into database",
1861 return error("%s: unsupported file type", path
);