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] = { 0, };
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 adjust_shared_perm(const char *path
)
58 if (!shared_repository
)
60 if (lstat(path
, &st
) < 0)
71 if (chmod(path
, mode
) < 0)
76 int safe_create_leading_directories(char *path
)
85 pos
= strchr(pos
, '/');
89 if (!stat(path
, &st
)) {
91 if (!S_ISDIR(st
.st_mode
)) {
96 else if (mkdir(path
, 0777)) {
100 else if (adjust_shared_perm(path
)) {
109 char * sha1_to_hex(const unsigned char *sha1
)
111 static char buffer
[50];
112 static const char hex
[] = "0123456789abcdef";
116 for (i
= 0; i
< 20; i
++) {
117 unsigned int val
= *sha1
++;
118 *buf
++ = hex
[val
>> 4];
119 *buf
++ = hex
[val
& 0xf];
126 static void fill_sha1_path(char *pathbuf
, const unsigned char *sha1
)
129 for (i
= 0; i
< 20; i
++) {
130 static char hex
[] = "0123456789abcdef";
131 unsigned int val
= sha1
[i
];
132 char *pos
= pathbuf
+ i
*2 + (i
> 0);
133 *pos
++ = hex
[val
>> 4];
134 *pos
= hex
[val
& 0xf];
139 * NOTE! This returns a statically allocated buffer, so you have to be
140 * careful about using it. Do a "strdup()" if you need to save the
143 * Also note that this returns the location for creating. Reading
144 * SHA1 file can happen from any alternate directory listed in the
145 * DB_ENVIRONMENT environment variable if it is not found in
146 * the primary object database.
148 char *sha1_file_name(const unsigned char *sha1
)
150 static char *name
, *base
;
153 const char *sha1_file_directory
= get_object_directory();
154 int len
= strlen(sha1_file_directory
);
155 base
= xmalloc(len
+ 60);
156 memcpy(base
, sha1_file_directory
, len
);
157 memset(base
+len
, 0, 60);
160 name
= base
+ len
+ 1;
162 fill_sha1_path(name
, sha1
);
166 char *sha1_pack_name(const unsigned char *sha1
)
168 static const char hex
[] = "0123456789abcdef";
169 static char *name
, *base
, *buf
;
173 const char *sha1_file_directory
= get_object_directory();
174 int len
= strlen(sha1_file_directory
);
175 base
= xmalloc(len
+ 60);
176 sprintf(base
, "%s/pack/pack-1234567890123456789012345678901234567890.pack", sha1_file_directory
);
177 name
= base
+ len
+ 11;
182 for (i
= 0; i
< 20; i
++) {
183 unsigned int val
= *sha1
++;
184 *buf
++ = hex
[val
>> 4];
185 *buf
++ = hex
[val
& 0xf];
191 char *sha1_pack_index_name(const unsigned char *sha1
)
193 static const char hex
[] = "0123456789abcdef";
194 static char *name
, *base
, *buf
;
198 const char *sha1_file_directory
= get_object_directory();
199 int len
= strlen(sha1_file_directory
);
200 base
= xmalloc(len
+ 60);
201 sprintf(base
, "%s/pack/pack-1234567890123456789012345678901234567890.idx", sha1_file_directory
);
202 name
= base
+ len
+ 11;
207 for (i
= 0; i
< 20; i
++) {
208 unsigned int val
= *sha1
++;
209 *buf
++ = hex
[val
>> 4];
210 *buf
++ = hex
[val
& 0xf];
216 struct alternate_object_database
*alt_odb_list
;
217 static struct alternate_object_database
**alt_odb_tail
;
220 * Prepare alternate object database registry.
222 * The variable alt_odb_list points at the list of struct
223 * alternate_object_database. The elements on this list come from
224 * non-empty elements from colon separated ALTERNATE_DB_ENVIRONMENT
225 * environment variable, and $GIT_OBJECT_DIRECTORY/info/alternates,
226 * whose contents is similar to that environment variable but can be
227 * LF separated. Its base points at a statically allocated buffer that
228 * contains "/the/directory/corresponding/to/.git/objects/...", while
229 * its name points just after the slash at the end of ".git/objects/"
230 * in the example above, and has enough space to hold 40-byte hex
231 * SHA1, an extra slash for the first level indirection, and the
234 static void link_alt_odb_entries(const char *alt
, const char *ep
, int sep
,
235 const char *relative_base
)
237 const char *cp
, *last
;
238 struct alternate_object_database
*ent
;
239 const char *objdir
= get_object_directory();
245 if (cp
< ep
&& *cp
== '#') {
246 while (cp
< ep
&& *cp
!= sep
)
251 for ( ; cp
< ep
&& *cp
!= sep
; cp
++)
255 struct alternate_object_database
*alt
;
256 /* 43 = 40-byte + 2 '/' + terminating NUL */
257 int pfxlen
= cp
- last
;
258 int entlen
= pfxlen
+ 43;
260 if (*last
!= '/' && relative_base
) {
261 /* Relative alt-odb */
263 base_len
= strlen(relative_base
) + 1;
267 ent
= xmalloc(sizeof(*ent
) + entlen
);
269 if (*last
!= '/' && relative_base
) {
270 memcpy(ent
->base
, relative_base
, base_len
- 1);
271 ent
->base
[base_len
- 1] = '/';
272 memcpy(ent
->base
+ base_len
,
276 memcpy(ent
->base
, last
, pfxlen
);
278 ent
->name
= ent
->base
+ pfxlen
+ 1;
279 ent
->base
[pfxlen
+ 3] = '/';
280 ent
->base
[pfxlen
] = ent
->base
[entlen
-1] = 0;
282 /* Detect cases where alternate disappeared */
283 if (stat(ent
->base
, &st
) || !S_ISDIR(st
.st_mode
)) {
284 error("object directory %s does not exist; "
285 "check .git/objects/info/alternates.",
289 ent
->base
[pfxlen
] = '/';
291 /* Prevent the common mistake of listing the same
292 * thing twice, or object directory itself.
294 for (alt
= alt_odb_list
; alt
; alt
= alt
->next
)
295 if (!memcmp(ent
->base
, alt
->base
, pfxlen
))
297 if (!memcmp(ent
->base
, objdir
, pfxlen
)) {
303 alt_odb_tail
= &(ent
->next
);
307 while (cp
< ep
&& *cp
== sep
)
313 void prepare_alt_odb(void)
321 alt
= getenv(ALTERNATE_DB_ENVIRONMENT
);
326 alt_odb_tail
= &alt_odb_list
;
327 link_alt_odb_entries(alt
, alt
+ strlen(alt
), ':', NULL
);
329 sprintf(path
, "%s/info/alternates", get_object_directory());
330 fd
= open(path
, O_RDONLY
);
333 if (fstat(fd
, &st
) || (st
.st_size
== 0)) {
337 map
= mmap(NULL
, st
.st_size
, PROT_READ
, MAP_PRIVATE
, fd
, 0);
339 if (map
== MAP_FAILED
)
342 link_alt_odb_entries(map
, map
+ st
.st_size
, '\n',
343 get_object_directory());
344 munmap(map
, st
.st_size
);
347 static char *find_sha1_file(const unsigned char *sha1
, struct stat
*st
)
349 char *name
= sha1_file_name(sha1
);
350 struct alternate_object_database
*alt
;
355 for (alt
= alt_odb_list
; alt
; alt
= alt
->next
) {
357 fill_sha1_path(name
, sha1
);
358 if (!stat(alt
->base
, st
))
364 #define PACK_MAX_SZ (1<<26)
365 static int pack_used_ctr
;
366 static unsigned long pack_mapped
;
367 struct packed_git
*packed_git
;
369 static int check_packed_git_idx(const char *path
, unsigned long *idx_size_
,
374 unsigned long idx_size
;
376 int fd
= open(path
, O_RDONLY
);
380 if (fstat(fd
, &st
)) {
384 idx_size
= st
.st_size
;
385 idx_map
= mmap(NULL
, idx_size
, PROT_READ
, MAP_PRIVATE
, fd
, 0);
387 if (idx_map
== MAP_FAILED
)
392 *idx_size_
= idx_size
;
394 /* check index map */
395 if (idx_size
< 4*256 + 20 + 20)
396 return error("index file too small");
398 for (i
= 0; i
< 256; i
++) {
399 unsigned int n
= ntohl(index
[i
]);
401 return error("non-monotonic index");
407 * - 256 index entries 4 bytes each
408 * - 24-byte entries * nr (20-byte sha1 + 4-byte offset)
409 * - 20-byte SHA1 of the packfile
410 * - 20-byte SHA1 file checksum
412 if (idx_size
!= 4*256 + nr
* 24 + 20 + 20)
413 return error("wrong index file size");
418 static int unuse_one_packed_git(void)
420 struct packed_git
*p
, *lru
= NULL
;
422 for (p
= packed_git
; p
; p
= p
->next
) {
423 if (p
->pack_use_cnt
|| !p
->pack_base
)
425 if (!lru
|| p
->pack_last_used
< lru
->pack_last_used
)
430 munmap(lru
->pack_base
, lru
->pack_size
);
431 lru
->pack_base
= NULL
;
435 void unuse_packed_git(struct packed_git
*p
)
440 int use_packed_git(struct packed_git
*p
)
444 // We created the struct before we had the pack
445 stat(p
->pack_name
, &st
);
446 if (!S_ISREG(st
.st_mode
))
447 die("packfile %s not a regular file", p
->pack_name
);
448 p
->pack_size
= st
.st_size
;
455 pack_mapped
+= p
->pack_size
;
456 while (PACK_MAX_SZ
< pack_mapped
&& unuse_one_packed_git())
458 fd
= open(p
->pack_name
, O_RDONLY
);
460 die("packfile %s cannot be opened", p
->pack_name
);
461 if (fstat(fd
, &st
)) {
463 die("packfile %s cannot be opened", p
->pack_name
);
465 if (st
.st_size
!= p
->pack_size
)
466 die("packfile %s size mismatch.", p
->pack_name
);
467 map
= mmap(NULL
, p
->pack_size
, PROT_READ
, MAP_PRIVATE
, fd
, 0);
469 if (map
== MAP_FAILED
)
470 die("packfile %s cannot be mapped.", p
->pack_name
);
473 /* Check if the pack file matches with the index file.
476 if (memcmp((char*)(p
->index_base
) + p
->index_size
- 40,
477 p
->pack_base
+ p
->pack_size
- 20, 20)) {
479 die("packfile %s does not match index.", p
->pack_name
);
482 p
->pack_last_used
= pack_used_ctr
++;
487 struct packed_git
*add_packed_git(char *path
, int path_len
, int local
)
490 struct packed_git
*p
;
491 unsigned long idx_size
;
493 unsigned char sha1
[20];
495 if (check_packed_git_idx(path
, &idx_size
, &idx_map
))
498 /* do we have a corresponding .pack file? */
499 strcpy(path
+ path_len
- 4, ".pack");
500 if (stat(path
, &st
) || !S_ISREG(st
.st_mode
)) {
501 munmap(idx_map
, idx_size
);
504 /* ok, it looks sane as far as we can check without
505 * actually mapping the pack file.
507 p
= xmalloc(sizeof(*p
) + path_len
+ 2);
508 strcpy(p
->pack_name
, path
);
509 p
->index_size
= idx_size
;
510 p
->pack_size
= st
.st_size
;
511 p
->index_base
= idx_map
;
514 p
->pack_last_used
= 0;
516 p
->pack_local
= local
;
517 if ((path_len
> 44) && !get_sha1_hex(path
+ path_len
- 44, sha1
))
518 memcpy(p
->sha1
, sha1
, 20);
522 struct packed_git
*parse_pack_index(unsigned char *sha1
)
524 char *path
= sha1_pack_index_name(sha1
);
525 return parse_pack_index_file(sha1
, path
);
528 struct packed_git
*parse_pack_index_file(const unsigned char *sha1
, char *idx_path
)
530 struct packed_git
*p
;
531 unsigned long idx_size
;
535 if (check_packed_git_idx(idx_path
, &idx_size
, &idx_map
))
538 path
= sha1_pack_name(sha1
);
540 p
= xmalloc(sizeof(*p
) + strlen(path
) + 2);
541 strcpy(p
->pack_name
, path
);
542 p
->index_size
= idx_size
;
544 p
->index_base
= idx_map
;
547 p
->pack_last_used
= 0;
549 memcpy(p
->sha1
, sha1
, 20);
553 void install_packed_git(struct packed_git
*pack
)
555 pack
->next
= packed_git
;
559 static void prepare_packed_git_one(char *objdir
, int local
)
566 sprintf(path
, "%s/pack", objdir
);
571 error("unable to open object pack directory: %s: %s",
572 path
, strerror(errno
));
576 while ((de
= readdir(dir
)) != NULL
) {
577 int namelen
= strlen(de
->d_name
);
578 struct packed_git
*p
;
580 if (strcmp(de
->d_name
+ namelen
- 4, ".idx"))
583 /* we have .idx. Is it a file we can map? */
584 strcpy(path
+ len
, de
->d_name
);
585 p
= add_packed_git(path
, len
+ namelen
, local
);
588 p
->next
= packed_git
;
594 void prepare_packed_git(void)
596 static int run_once
= 0;
597 struct alternate_object_database
*alt
;
601 prepare_packed_git_one(get_object_directory(), 1);
603 for (alt
= alt_odb_list
; alt
; alt
= alt
->next
) {
605 prepare_packed_git_one(alt
->base
, 0);
611 int check_sha1_signature(const unsigned char *sha1
, void *map
, unsigned long size
, const char *type
)
614 unsigned char real_sha1
[20];
618 SHA1_Update(&c
, header
, 1+sprintf(header
, "%s %lu", type
, size
));
619 SHA1_Update(&c
, map
, size
);
620 SHA1_Final(real_sha1
, &c
);
621 return memcmp(sha1
, real_sha1
, 20) ? -1 : 0;
624 static void *map_sha1_file_internal(const unsigned char *sha1
,
630 char *filename
= find_sha1_file(sha1
, &st
);
636 fd
= open(filename
, O_RDONLY
| sha1_file_open_flag
);
638 /* See if it works without O_NOATIME */
639 switch (sha1_file_open_flag
) {
641 fd
= open(filename
, O_RDONLY
);
649 /* If it failed once, it will probably fail again.
650 * Stop using O_NOATIME
652 sha1_file_open_flag
= 0;
654 map
= mmap(NULL
, st
.st_size
, PROT_READ
, MAP_PRIVATE
, fd
, 0);
656 if (map
== MAP_FAILED
)
662 int unpack_sha1_header(z_stream
*stream
, void *map
, unsigned long mapsize
, void *buffer
, unsigned long size
)
664 /* Get the data stream */
665 memset(stream
, 0, sizeof(*stream
));
666 stream
->next_in
= map
;
667 stream
->avail_in
= mapsize
;
668 stream
->next_out
= buffer
;
669 stream
->avail_out
= size
;
672 return inflate(stream
, 0);
675 static void *unpack_sha1_rest(z_stream
*stream
, void *buffer
, unsigned long size
)
677 int bytes
= strlen(buffer
) + 1;
678 unsigned char *buf
= xmalloc(1+size
);
680 memcpy(buf
, buffer
+ bytes
, stream
->total_out
- bytes
);
681 bytes
= stream
->total_out
- bytes
;
683 stream
->next_out
= buf
+ bytes
;
684 stream
->avail_out
= size
- bytes
;
685 while (inflate(stream
, Z_FINISH
) == Z_OK
)
694 * We used to just use "sscanf()", but that's actually way
695 * too permissive for what we want to check. So do an anal
696 * object header parse by hand.
698 int parse_sha1_header(char *hdr
, char *type
, unsigned long *sizep
)
704 * The type can be at most ten bytes (including the
705 * terminating '\0' that we add), and is followed by
720 * The length must follow immediately, and be in canonical
721 * decimal format (ie "010" is not valid).
728 unsigned long c
= *hdr
- '0';
732 size
= size
* 10 + c
;
738 * The length must be followed by a zero byte
740 return *hdr
? -1 : 0;
743 void * unpack_sha1_file(void *map
, unsigned long mapsize
, char *type
, unsigned long *size
)
749 ret
= unpack_sha1_header(&stream
, map
, mapsize
, hdr
, sizeof(hdr
));
750 if (ret
< Z_OK
|| parse_sha1_header(hdr
, type
, size
) < 0)
753 return unpack_sha1_rest(&stream
, hdr
, *size
);
756 /* forward declaration for a mutually recursive function */
757 static int packed_object_info(struct pack_entry
*entry
,
758 char *type
, unsigned long *sizep
);
760 static int packed_delta_info(unsigned char *base_sha1
,
761 unsigned long delta_size
,
764 unsigned long *sizep
,
765 struct packed_git
*p
)
767 struct pack_entry base_ent
;
770 die("truncated pack file");
772 /* The base entry _must_ be in the same pack */
773 if (!find_pack_entry_one(base_sha1
, &base_ent
, p
))
774 die("failed to find delta-pack base object %s",
775 sha1_to_hex(base_sha1
));
777 /* We choose to only get the type of the base object and
778 * ignore potentially corrupt pack file that expects the delta
779 * based on a base with a wrong size. This saves tons of
783 if (packed_object_info(&base_ent
, type
, NULL
))
784 die("cannot get info for delta-pack base");
787 const unsigned char *data
;
788 unsigned char delta_head
[64];
789 unsigned long result_size
;
793 memset(&stream
, 0, sizeof(stream
));
795 data
= stream
.next_in
= base_sha1
+ 20;
796 stream
.avail_in
= left
- 20;
797 stream
.next_out
= delta_head
;
798 stream
.avail_out
= sizeof(delta_head
);
800 inflateInit(&stream
);
801 st
= inflate(&stream
, Z_FINISH
);
803 if ((st
!= Z_STREAM_END
) &&
804 stream
.total_out
!= sizeof(delta_head
))
805 die("delta data unpack-initial failed");
807 /* Examine the initial part of the delta to figure out
811 get_delta_hdr_size(&data
); /* ignore base size */
813 /* Read the result size */
814 result_size
= get_delta_hdr_size(&data
);
815 *sizep
= result_size
;
820 static unsigned long unpack_object_header(struct packed_git
*p
, unsigned long offset
,
821 enum object_type
*type
, unsigned long *sizep
)
824 unsigned char *pack
, c
;
827 if (offset
>= p
->pack_size
)
828 die("object offset outside of pack file");
830 pack
= p
->pack_base
+ offset
;
833 *type
= (c
>> 4) & 7;
837 if (offset
>= p
->pack_size
)
838 die("object offset outside of pack file");
841 size
+= (c
& 0x7f) << shift
;
848 int check_reuse_pack_delta(struct packed_git
*p
, unsigned long offset
,
849 unsigned char *base
, unsigned long *sizep
,
850 enum object_type
*kindp
)
857 ptr
= unpack_object_header(p
, ptr
, kindp
, sizep
);
858 if (*kindp
!= OBJ_DELTA
)
860 memcpy(base
, p
->pack_base
+ ptr
, 20);
867 void packed_object_info_detail(struct pack_entry
*e
,
870 unsigned long *store_size
,
871 unsigned int *delta_chain_length
,
872 unsigned char *base_sha1
)
874 struct packed_git
*p
= e
->p
;
875 unsigned long offset
, left
;
877 enum object_type kind
;
879 offset
= unpack_object_header(p
, e
->offset
, &kind
, size
);
880 pack
= p
->pack_base
+ offset
;
881 left
= p
->pack_size
- offset
;
882 if (kind
!= OBJ_DELTA
)
883 *delta_chain_length
= 0;
885 unsigned int chain_length
= 0;
886 memcpy(base_sha1
, pack
, 20);
888 struct pack_entry base_ent
;
891 find_pack_entry_one(pack
, &base_ent
, p
);
892 offset
= unpack_object_header(p
, base_ent
.offset
,
894 pack
= p
->pack_base
+ offset
;
896 } while (kind
== OBJ_DELTA
);
897 *delta_chain_length
= chain_length
;
901 strcpy(type
, commit_type
);
904 strcpy(type
, tree_type
);
907 strcpy(type
, blob_type
);
910 strcpy(type
, tag_type
);
913 die("corrupted pack file %s containing object of kind %d",
916 *store_size
= 0; /* notyet */
919 static int packed_object_info(struct pack_entry
*entry
,
920 char *type
, unsigned long *sizep
)
922 struct packed_git
*p
= entry
->p
;
923 unsigned long offset
, size
, left
;
925 enum object_type kind
;
928 if (use_packed_git(p
))
929 die("cannot map packed file");
931 offset
= unpack_object_header(p
, entry
->offset
, &kind
, &size
);
932 pack
= p
->pack_base
+ offset
;
933 left
= p
->pack_size
- offset
;
937 retval
= packed_delta_info(pack
, size
, left
, type
, sizep
, p
);
941 strcpy(type
, commit_type
);
944 strcpy(type
, tree_type
);
947 strcpy(type
, blob_type
);
950 strcpy(type
, tag_type
);
953 die("corrupted pack file %s containing object of kind %d",
962 /* forward declaration for a mutually recursive function */
963 static void *unpack_entry(struct pack_entry
*, char *, unsigned long *);
965 static void *unpack_delta_entry(unsigned char *base_sha1
,
966 unsigned long delta_size
,
969 unsigned long *sizep
,
970 struct packed_git
*p
)
972 struct pack_entry base_ent
;
973 void *data
, *delta_data
, *result
, *base
;
974 unsigned long data_size
, result_size
, base_size
;
979 die("truncated pack file");
981 /* The base entry _must_ be in the same pack */
982 if (!find_pack_entry_one(base_sha1
, &base_ent
, p
))
983 die("failed to find delta-pack base object %s",
984 sha1_to_hex(base_sha1
));
985 base
= unpack_entry_gently(&base_ent
, type
, &base_size
);
987 die("failed to read delta-pack base object %s",
988 sha1_to_hex(base_sha1
));
990 data
= base_sha1
+ 20;
991 data_size
= left
- 20;
992 delta_data
= xmalloc(delta_size
);
994 memset(&stream
, 0, sizeof(stream
));
996 stream
.next_in
= data
;
997 stream
.avail_in
= data_size
;
998 stream
.next_out
= delta_data
;
999 stream
.avail_out
= delta_size
;
1001 inflateInit(&stream
);
1002 st
= inflate(&stream
, Z_FINISH
);
1003 inflateEnd(&stream
);
1004 if ((st
!= Z_STREAM_END
) || stream
.total_out
!= delta_size
)
1005 die("delta data unpack failed");
1007 result
= patch_delta(base
, base_size
,
1008 delta_data
, delta_size
,
1011 die("failed to apply delta");
1014 *sizep
= result_size
;
1018 static void *unpack_non_delta_entry(unsigned char *data
,
1024 unsigned char *buffer
;
1026 buffer
= xmalloc(size
+ 1);
1028 memset(&stream
, 0, sizeof(stream
));
1029 stream
.next_in
= data
;
1030 stream
.avail_in
= left
;
1031 stream
.next_out
= buffer
;
1032 stream
.avail_out
= size
;
1034 inflateInit(&stream
);
1035 st
= inflate(&stream
, Z_FINISH
);
1036 inflateEnd(&stream
);
1037 if ((st
!= Z_STREAM_END
) || stream
.total_out
!= size
) {
1045 static void *unpack_entry(struct pack_entry
*entry
,
1046 char *type
, unsigned long *sizep
)
1048 struct packed_git
*p
= entry
->p
;
1051 if (use_packed_git(p
))
1052 die("cannot map packed file");
1053 retval
= unpack_entry_gently(entry
, type
, sizep
);
1054 unuse_packed_git(p
);
1056 die("corrupted pack file %s", p
->pack_name
);
1060 /* The caller is responsible for use_packed_git()/unuse_packed_git() pair */
1061 void *unpack_entry_gently(struct pack_entry
*entry
,
1062 char *type
, unsigned long *sizep
)
1064 struct packed_git
*p
= entry
->p
;
1065 unsigned long offset
, size
, left
;
1066 unsigned char *pack
;
1067 enum object_type kind
;
1070 offset
= unpack_object_header(p
, entry
->offset
, &kind
, &size
);
1071 pack
= p
->pack_base
+ offset
;
1072 left
= p
->pack_size
- offset
;
1075 retval
= unpack_delta_entry(pack
, size
, left
, type
, sizep
, p
);
1078 strcpy(type
, commit_type
);
1081 strcpy(type
, tree_type
);
1084 strcpy(type
, blob_type
);
1087 strcpy(type
, tag_type
);
1093 retval
= unpack_non_delta_entry(pack
, size
, left
);
1097 int num_packed_objects(const struct packed_git
*p
)
1099 /* See check_packed_git_idx() */
1100 return (p
->index_size
- 20 - 20 - 4*256) / 24;
1103 int nth_packed_object_sha1(const struct packed_git
*p
, int n
,
1104 unsigned char* sha1
)
1106 void *index
= p
->index_base
+ 256;
1107 if (n
< 0 || num_packed_objects(p
) <= n
)
1109 memcpy(sha1
, (index
+ 24 * n
+ 4), 20);
1113 int find_pack_entry_one(const unsigned char *sha1
,
1114 struct pack_entry
*e
, struct packed_git
*p
)
1116 unsigned int *level1_ofs
= p
->index_base
;
1117 int hi
= ntohl(level1_ofs
[*sha1
]);
1118 int lo
= ((*sha1
== 0x0) ? 0 : ntohl(level1_ofs
[*sha1
- 1]));
1119 void *index
= p
->index_base
+ 256;
1122 int mi
= (lo
+ hi
) / 2;
1123 int cmp
= memcmp(index
+ 24 * mi
+ 4, sha1
, 20);
1125 e
->offset
= ntohl(*((int*)(index
+ 24 * mi
)));
1126 memcpy(e
->sha1
, sha1
, 20);
1138 static int find_pack_entry(const unsigned char *sha1
, struct pack_entry
*e
)
1140 struct packed_git
*p
;
1141 prepare_packed_git();
1143 for (p
= packed_git
; p
; p
= p
->next
) {
1144 if (find_pack_entry_one(sha1
, e
, p
))
1150 struct packed_git
*find_sha1_pack(const unsigned char *sha1
,
1151 struct packed_git
*packs
)
1153 struct packed_git
*p
;
1154 struct pack_entry e
;
1156 for (p
= packs
; p
; p
= p
->next
) {
1157 if (find_pack_entry_one(sha1
, &e
, p
))
1164 int sha1_object_info(const unsigned char *sha1
, char *type
, unsigned long *sizep
)
1167 unsigned long mapsize
, size
;
1172 map
= map_sha1_file_internal(sha1
, &mapsize
);
1174 struct pack_entry e
;
1176 if (!find_pack_entry(sha1
, &e
))
1177 return error("unable to find %s", sha1_to_hex(sha1
));
1178 return packed_object_info(&e
, type
, sizep
);
1180 if (unpack_sha1_header(&stream
, map
, mapsize
, hdr
, sizeof(hdr
)) < 0)
1181 status
= error("unable to unpack %s header",
1183 if (parse_sha1_header(hdr
, type
, &size
) < 0)
1184 status
= error("unable to parse %s header", sha1_to_hex(sha1
));
1190 inflateEnd(&stream
);
1191 munmap(map
, mapsize
);
1195 static void *read_packed_sha1(const unsigned char *sha1
, char *type
, unsigned long *size
)
1197 struct pack_entry e
;
1199 if (!find_pack_entry(sha1
, &e
)) {
1200 error("cannot read sha1_file for %s", sha1_to_hex(sha1
));
1203 return unpack_entry(&e
, type
, size
);
1206 void * read_sha1_file(const unsigned char *sha1
, char *type
, unsigned long *size
)
1208 unsigned long mapsize
;
1210 struct pack_entry e
;
1212 if (find_pack_entry(sha1
, &e
))
1213 return read_packed_sha1(sha1
, type
, size
);
1214 map
= map_sha1_file_internal(sha1
, &mapsize
);
1216 buf
= unpack_sha1_file(map
, mapsize
, type
, size
);
1217 munmap(map
, mapsize
);
1223 void *read_object_with_reference(const unsigned char *sha1
,
1224 const char *required_type
,
1225 unsigned long *size
,
1226 unsigned char *actual_sha1_return
)
1230 unsigned long isize
;
1231 unsigned char actual_sha1
[20];
1233 memcpy(actual_sha1
, sha1
, 20);
1235 int ref_length
= -1;
1236 const char *ref_type
= NULL
;
1238 buffer
= read_sha1_file(actual_sha1
, type
, &isize
);
1241 if (!strcmp(type
, required_type
)) {
1243 if (actual_sha1_return
)
1244 memcpy(actual_sha1_return
, actual_sha1
, 20);
1247 /* Handle references */
1248 else if (!strcmp(type
, commit_type
))
1250 else if (!strcmp(type
, tag_type
))
1251 ref_type
= "object ";
1256 ref_length
= strlen(ref_type
);
1258 if (memcmp(buffer
, ref_type
, ref_length
) ||
1259 get_sha1_hex(buffer
+ ref_length
, actual_sha1
)) {
1264 /* Now we have the ID of the referred-to object in
1265 * actual_sha1. Check again. */
1269 char *write_sha1_file_prepare(void *buf
,
1272 unsigned char *sha1
,
1278 /* Generate the header */
1279 *hdrlen
= sprintf((char *)hdr
, "%s %lu", type
, len
)+1;
1283 SHA1_Update(&c
, hdr
, *hdrlen
);
1284 SHA1_Update(&c
, buf
, len
);
1285 SHA1_Final(sha1
, &c
);
1287 return sha1_file_name(sha1
);
1291 * Link the tempfile to the final place, possibly creating the
1292 * last directory level as you do so.
1294 * Returns the errno on failure, 0 on success.
1296 static int link_temp_to_file(const char *tmpfile
, char *filename
)
1300 if (!link(tmpfile
, filename
))
1304 * Try to mkdir the last path component if that failed
1307 * Re-try the "link()" regardless of whether the mkdir
1308 * succeeds, since a race might mean that somebody
1312 if (ret
== ENOENT
) {
1313 char *dir
= strrchr(filename
, '/');
1316 mkdir(filename
, 0777);
1317 if (adjust_shared_perm(filename
))
1320 if (!link(tmpfile
, filename
))
1329 * Move the just written object into its final resting place
1331 int move_temp_to_file(const char *tmpfile
, char *filename
)
1333 int ret
= link_temp_to_file(tmpfile
, filename
);
1336 * Coda hack - coda doesn't like cross-directory links,
1337 * so we fall back to a rename, which will mean that it
1338 * won't be able to check collisions, but that's not a
1341 * The same holds for FAT formatted media.
1343 * When this succeeds, we just return 0. We have nothing
1346 if (ret
&& ret
!= EEXIST
) {
1347 if (!rename(tmpfile
, filename
))
1353 if (ret
!= EEXIST
) {
1354 fprintf(stderr
, "unable to write sha1 filename %s: %s\n", filename
, strerror(ret
));
1357 /* FIXME!!! Collision check here ? */
1363 int write_sha1_file(void *buf
, unsigned long len
, const char *type
, unsigned char *returnsha1
)
1366 unsigned char *compressed
;
1368 unsigned char sha1
[20];
1370 static char tmpfile
[PATH_MAX
];
1371 unsigned char hdr
[50];
1374 /* Normally if we have it in the pack then we do not bother writing
1375 * it out into .git/objects/??/?{38} file.
1377 filename
= write_sha1_file_prepare(buf
, len
, type
, sha1
, hdr
, &hdrlen
);
1379 memcpy(returnsha1
, sha1
, 20);
1380 if (has_sha1_file(sha1
))
1382 fd
= open(filename
, O_RDONLY
);
1385 * FIXME!!! We might do collision checking here, but we'd
1386 * need to uncompress the old file and check it. Later.
1392 if (errno
!= ENOENT
) {
1393 fprintf(stderr
, "sha1 file %s: %s\n", filename
, strerror(errno
));
1397 snprintf(tmpfile
, sizeof(tmpfile
), "%s/obj_XXXXXX", get_object_directory());
1399 fd
= mkstemp(tmpfile
);
1401 fprintf(stderr
, "unable to create temporary sha1 filename %s: %s\n", tmpfile
, strerror(errno
));
1406 memset(&stream
, 0, sizeof(stream
));
1407 deflateInit(&stream
, Z_BEST_COMPRESSION
);
1408 size
= deflateBound(&stream
, len
+hdrlen
);
1409 compressed
= xmalloc(size
);
1412 stream
.next_out
= compressed
;
1413 stream
.avail_out
= size
;
1415 /* First header.. */
1416 stream
.next_in
= hdr
;
1417 stream
.avail_in
= hdrlen
;
1418 while (deflate(&stream
, 0) == Z_OK
)
1421 /* Then the data itself.. */
1422 stream
.next_in
= buf
;
1423 stream
.avail_in
= len
;
1424 while (deflate(&stream
, Z_FINISH
) == Z_OK
)
1426 deflateEnd(&stream
);
1427 size
= stream
.total_out
;
1429 if (write(fd
, compressed
, size
) != size
)
1430 die("unable to write file");
1435 return move_temp_to_file(tmpfile
, filename
);
1438 int write_sha1_to_fd(int fd
, const unsigned char *sha1
)
1441 unsigned long objsize
;
1443 void *map
= map_sha1_file_internal(sha1
, &objsize
);
1445 void *temp_obj
= NULL
;
1449 unsigned char *unpacked
;
1454 // need to unpack and recompress it by itself
1455 unpacked
= read_packed_sha1(sha1
, type
, &len
);
1457 hdrlen
= sprintf(hdr
, "%s %lu", type
, len
) + 1;
1460 memset(&stream
, 0, sizeof(stream
));
1461 deflateInit(&stream
, Z_BEST_COMPRESSION
);
1462 size
= deflateBound(&stream
, len
+ hdrlen
);
1463 temp_obj
= buf
= xmalloc(size
);
1466 stream
.next_out
= buf
;
1467 stream
.avail_out
= size
;
1469 /* First header.. */
1470 stream
.next_in
= (void *)hdr
;
1471 stream
.avail_in
= hdrlen
;
1472 while (deflate(&stream
, 0) == Z_OK
)
1475 /* Then the data itself.. */
1476 stream
.next_in
= unpacked
;
1477 stream
.avail_in
= len
;
1478 while (deflate(&stream
, Z_FINISH
) == Z_OK
)
1480 deflateEnd(&stream
);
1483 objsize
= stream
.total_out
;
1487 size
= write(fd
, buf
+ posn
, objsize
- posn
);
1490 fprintf(stderr
, "write closed\n");
1497 } while (posn
< objsize
);
1500 munmap(map
, objsize
);
1507 int write_sha1_from_fd(const unsigned char *sha1
, int fd
, char *buffer
,
1508 size_t bufsize
, size_t *bufposn
)
1510 char tmpfile
[PATH_MAX
];
1513 unsigned char real_sha1
[20];
1514 unsigned char discard
[4096];
1518 snprintf(tmpfile
, sizeof(tmpfile
), "%s/obj_XXXXXX", get_object_directory());
1520 local
= mkstemp(tmpfile
);
1522 return error("Couldn't open %s for %s",
1523 tmpfile
, sha1_to_hex(sha1
));
1525 memset(&stream
, 0, sizeof(stream
));
1527 inflateInit(&stream
);
1534 stream
.avail_in
= *bufposn
;
1535 stream
.next_in
= (unsigned char *) buffer
;
1537 stream
.next_out
= discard
;
1538 stream
.avail_out
= sizeof(discard
);
1539 ret
= inflate(&stream
, Z_SYNC_FLUSH
);
1540 SHA1_Update(&c
, discard
, sizeof(discard
) -
1542 } while (stream
.avail_in
&& ret
== Z_OK
);
1543 write(local
, buffer
, *bufposn
- stream
.avail_in
);
1544 memmove(buffer
, buffer
+ *bufposn
- stream
.avail_in
,
1546 *bufposn
= stream
.avail_in
;
1550 size
= read(fd
, buffer
+ *bufposn
, bufsize
- *bufposn
);
1555 return error("Connection closed?");
1556 perror("Reading from connection");
1561 inflateEnd(&stream
);
1564 SHA1_Final(real_sha1
, &c
);
1565 if (ret
!= Z_STREAM_END
) {
1567 return error("File %s corrupted", sha1_to_hex(sha1
));
1569 if (memcmp(sha1
, real_sha1
, 20)) {
1571 return error("File %s has bad hash", sha1_to_hex(sha1
));
1574 return move_temp_to_file(tmpfile
, sha1_file_name(sha1
));
1577 int has_pack_index(const unsigned char *sha1
)
1580 if (stat(sha1_pack_index_name(sha1
), &st
))
1585 int has_pack_file(const unsigned char *sha1
)
1588 if (stat(sha1_pack_name(sha1
), &st
))
1593 int has_sha1_pack(const unsigned char *sha1
)
1595 struct pack_entry e
;
1596 return find_pack_entry(sha1
, &e
);
1599 int has_sha1_file(const unsigned char *sha1
)
1602 struct pack_entry e
;
1604 if (find_pack_entry(sha1
, &e
))
1606 return find_sha1_file(sha1
, &st
) ? 1 : 0;
1609 int index_pipe(unsigned char *sha1
, int fd
, const char *type
, int write_object
)
1611 unsigned long size
= 4096;
1612 char *buf
= malloc(size
);
1614 unsigned long off
= 0;
1615 unsigned char hdr
[50];
1618 iret
= read(fd
, buf
+ off
, size
- off
);
1623 buf
= realloc(buf
, size
);
1634 ret
= write_sha1_file(buf
, off
, type
, sha1
);
1636 write_sha1_file_prepare(buf
, off
, type
, sha1
, hdr
, &hdrlen
);
1643 int index_fd(unsigned char *sha1
, int fd
, struct stat
*st
, int write_object
, const char *type
)
1645 unsigned long size
= st
->st_size
;
1648 unsigned char hdr
[50];
1653 buf
= mmap(NULL
, size
, PROT_READ
, MAP_PRIVATE
, fd
, 0);
1655 if (buf
== MAP_FAILED
)
1661 ret
= write_sha1_file(buf
, size
, type
, sha1
);
1663 write_sha1_file_prepare(buf
, size
, type
, sha1
, hdr
, &hdrlen
);
1671 int index_path(unsigned char *sha1
, const char *path
, struct stat
*st
, int write_object
)
1676 switch (st
->st_mode
& S_IFMT
) {
1678 fd
= open(path
, O_RDONLY
);
1680 return error("open(\"%s\"): %s", path
,
1682 if (index_fd(sha1
, fd
, st
, write_object
, NULL
) < 0)
1683 return error("%s: failed to insert into database",
1687 target
= xmalloc(st
->st_size
+1);
1688 if (readlink(path
, target
, st
->st_size
+1) != st
->st_size
) {
1689 char *errstr
= strerror(errno
);
1691 return error("readlink(\"%s\"): %s", path
,
1694 if (!write_object
) {
1695 unsigned char hdr
[50];
1697 write_sha1_file_prepare(target
, st
->st_size
, blob_type
,
1698 sha1
, hdr
, &hdrlen
);
1699 } else if (write_sha1_file(target
, st
->st_size
, blob_type
, sha1
))
1700 return error("%s: failed to insert into database",
1705 return error("%s: unsupported file type", path
);