2 * GIT - The information manager from hell
4 * Copyright (C) Linus Torvalds, 2005
6 * This handles basic git sha1 object files - packing, unpacking,
14 #if defined(__linux__) && (defined(__i386__) || defined(__PPC__))
15 #define O_NOATIME 01000000
21 const unsigned char null_sha1
[20] = { 0, };
23 static unsigned int sha1_file_open_flag
= O_NOATIME
;
25 static unsigned hexval(char c
)
27 if (c
>= '0' && c
<= '9')
29 if (c
>= 'a' && c
<= 'f')
31 if (c
>= 'A' && c
<= 'F')
36 int get_sha1_hex(const char *hex
, unsigned char *sha1
)
39 for (i
= 0; i
< 20; i
++) {
40 unsigned int val
= (hexval(hex
[0]) << 4) | hexval(hex
[1]);
49 int adjust_shared_perm(const char *path
)
54 if (!shared_repository
)
56 if (lstat(path
, &st
) < 0)
67 if (chmod(path
, mode
) < 0)
72 int safe_create_leading_directories(char *path
)
81 pos
= strchr(pos
, '/');
85 if (!stat(path
, &st
)) {
87 if (!S_ISDIR(st
.st_mode
)) {
92 else if (mkdir(path
, 0777)) {
96 else if (adjust_shared_perm(path
)) {
105 char * sha1_to_hex(const unsigned char *sha1
)
107 static char buffer
[50];
108 static const char hex
[] = "0123456789abcdef";
112 for (i
= 0; i
< 20; i
++) {
113 unsigned int val
= *sha1
++;
114 *buf
++ = hex
[val
>> 4];
115 *buf
++ = hex
[val
& 0xf];
122 static void fill_sha1_path(char *pathbuf
, const unsigned char *sha1
)
125 for (i
= 0; i
< 20; i
++) {
126 static char hex
[] = "0123456789abcdef";
127 unsigned int val
= sha1
[i
];
128 char *pos
= pathbuf
+ i
*2 + (i
> 0);
129 *pos
++ = hex
[val
>> 4];
130 *pos
= hex
[val
& 0xf];
135 * NOTE! This returns a statically allocated buffer, so you have to be
136 * careful about using it. Do a "strdup()" if you need to save the
139 * Also note that this returns the location for creating. Reading
140 * SHA1 file can happen from any alternate directory listed in the
141 * DB_ENVIRONMENT environment variable if it is not found in
142 * the primary object database.
144 char *sha1_file_name(const unsigned char *sha1
)
146 static char *name
, *base
;
149 const char *sha1_file_directory
= get_object_directory();
150 int len
= strlen(sha1_file_directory
);
151 base
= xmalloc(len
+ 60);
152 memcpy(base
, sha1_file_directory
, len
);
153 memset(base
+len
, 0, 60);
156 name
= base
+ len
+ 1;
158 fill_sha1_path(name
, sha1
);
162 char *sha1_pack_name(const unsigned char *sha1
)
164 static const char hex
[] = "0123456789abcdef";
165 static char *name
, *base
, *buf
;
169 const char *sha1_file_directory
= get_object_directory();
170 int len
= strlen(sha1_file_directory
);
171 base
= xmalloc(len
+ 60);
172 sprintf(base
, "%s/pack/pack-1234567890123456789012345678901234567890.pack", sha1_file_directory
);
173 name
= base
+ len
+ 11;
178 for (i
= 0; i
< 20; i
++) {
179 unsigned int val
= *sha1
++;
180 *buf
++ = hex
[val
>> 4];
181 *buf
++ = hex
[val
& 0xf];
187 char *sha1_pack_index_name(const unsigned char *sha1
)
189 static const char hex
[] = "0123456789abcdef";
190 static char *name
, *base
, *buf
;
194 const char *sha1_file_directory
= get_object_directory();
195 int len
= strlen(sha1_file_directory
);
196 base
= xmalloc(len
+ 60);
197 sprintf(base
, "%s/pack/pack-1234567890123456789012345678901234567890.idx", sha1_file_directory
);
198 name
= base
+ len
+ 11;
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
;
216 * Prepare alternate object database registry.
218 * The variable alt_odb_list points at the list of struct
219 * alternate_object_database. The elements on this list come from
220 * non-empty elements from colon separated ALTERNATE_DB_ENVIRONMENT
221 * environment variable, and $GIT_OBJECT_DIRECTORY/info/alternates,
222 * whose contents is similar to that environment variable but can be
223 * LF separated. Its base points at a statically allocated buffer that
224 * contains "/the/directory/corresponding/to/.git/objects/...", while
225 * its name points just after the slash at the end of ".git/objects/"
226 * in the example above, and has enough space to hold 40-byte hex
227 * SHA1, an extra slash for the first level indirection, and the
230 static void link_alt_odb_entries(const char *alt
, const char *ep
, int sep
,
231 const char *relative_base
)
233 const char *cp
, *last
;
234 struct alternate_object_database
*ent
;
235 const char *objdir
= get_object_directory();
241 if (cp
< ep
&& *cp
== '#') {
242 while (cp
< ep
&& *cp
!= sep
)
247 for ( ; cp
< ep
&& *cp
!= sep
; cp
++)
251 struct alternate_object_database
*alt
;
252 /* 43 = 40-byte + 2 '/' + terminating NUL */
253 int pfxlen
= cp
- last
;
254 int entlen
= pfxlen
+ 43;
256 if (*last
!= '/' && relative_base
) {
257 /* Relative alt-odb */
259 base_len
= strlen(relative_base
) + 1;
263 ent
= xmalloc(sizeof(*ent
) + entlen
);
265 if (*last
!= '/' && relative_base
) {
266 memcpy(ent
->base
, relative_base
, base_len
- 1);
267 ent
->base
[base_len
- 1] = '/';
268 memcpy(ent
->base
+ base_len
,
272 memcpy(ent
->base
, last
, pfxlen
);
274 ent
->name
= ent
->base
+ pfxlen
+ 1;
275 ent
->base
[pfxlen
+ 3] = '/';
276 ent
->base
[pfxlen
] = ent
->base
[entlen
-1] = 0;
278 /* Detect cases where alternate disappeared */
279 if (stat(ent
->base
, &st
) || !S_ISDIR(st
.st_mode
)) {
280 error("object directory %s does not exist; "
281 "check .git/objects/info/alternates.",
285 ent
->base
[pfxlen
] = '/';
287 /* Prevent the common mistake of listing the same
288 * thing twice, or object directory itself.
290 for (alt
= alt_odb_list
; alt
; alt
= alt
->next
)
291 if (!memcmp(ent
->base
, alt
->base
, pfxlen
))
293 if (!memcmp(ent
->base
, objdir
, pfxlen
)) {
299 alt_odb_tail
= &(ent
->next
);
303 while (cp
< ep
&& *cp
== sep
)
309 void prepare_alt_odb(void)
317 alt
= getenv(ALTERNATE_DB_ENVIRONMENT
);
322 alt_odb_tail
= &alt_odb_list
;
323 link_alt_odb_entries(alt
, alt
+ strlen(alt
), ':', NULL
);
325 sprintf(path
, "%s/info/alternates", get_object_directory());
326 fd
= open(path
, O_RDONLY
);
329 if (fstat(fd
, &st
) || (st
.st_size
== 0)) {
333 map
= mmap(NULL
, st
.st_size
, PROT_READ
, MAP_PRIVATE
, fd
, 0);
335 if (map
== MAP_FAILED
)
338 link_alt_odb_entries(map
, map
+ st
.st_size
, '\n',
339 get_object_directory());
340 munmap(map
, st
.st_size
);
343 static char *find_sha1_file(const unsigned char *sha1
, struct stat
*st
)
345 char *name
= sha1_file_name(sha1
);
346 struct alternate_object_database
*alt
;
351 for (alt
= alt_odb_list
; alt
; alt
= alt
->next
) {
353 fill_sha1_path(name
, sha1
);
354 if (!stat(alt
->base
, st
))
360 #define PACK_MAX_SZ (1<<26)
361 static int pack_used_ctr
;
362 static unsigned long pack_mapped
;
363 struct packed_git
*packed_git
;
365 static int check_packed_git_idx(const char *path
, unsigned long *idx_size_
,
370 unsigned long idx_size
;
372 int fd
= open(path
, O_RDONLY
);
376 if (fstat(fd
, &st
)) {
380 idx_size
= st
.st_size
;
381 idx_map
= mmap(NULL
, idx_size
, PROT_READ
, MAP_PRIVATE
, fd
, 0);
383 if (idx_map
== MAP_FAILED
)
388 *idx_size_
= idx_size
;
390 /* check index map */
391 if (idx_size
< 4*256 + 20 + 20)
392 return error("index file too small");
394 for (i
= 0; i
< 256; i
++) {
395 unsigned int n
= ntohl(index
[i
]);
397 return error("non-monotonic index");
403 * - 256 index entries 4 bytes each
404 * - 24-byte entries * nr (20-byte sha1 + 4-byte offset)
405 * - 20-byte SHA1 of the packfile
406 * - 20-byte SHA1 file checksum
408 if (idx_size
!= 4*256 + nr
* 24 + 20 + 20)
409 return error("wrong index file size");
414 static int unuse_one_packed_git(void)
416 struct packed_git
*p
, *lru
= NULL
;
418 for (p
= packed_git
; p
; p
= p
->next
) {
419 if (p
->pack_use_cnt
|| !p
->pack_base
)
421 if (!lru
|| p
->pack_last_used
< lru
->pack_last_used
)
426 munmap(lru
->pack_base
, lru
->pack_size
);
427 lru
->pack_base
= NULL
;
431 void unuse_packed_git(struct packed_git
*p
)
436 int use_packed_git(struct packed_git
*p
)
440 // We created the struct before we had the pack
441 stat(p
->pack_name
, &st
);
442 if (!S_ISREG(st
.st_mode
))
443 die("packfile %s not a regular file", p
->pack_name
);
444 p
->pack_size
= st
.st_size
;
451 pack_mapped
+= p
->pack_size
;
452 while (PACK_MAX_SZ
< pack_mapped
&& unuse_one_packed_git())
454 fd
= open(p
->pack_name
, O_RDONLY
);
456 die("packfile %s cannot be opened", p
->pack_name
);
457 if (fstat(fd
, &st
)) {
459 die("packfile %s cannot be opened", p
->pack_name
);
461 if (st
.st_size
!= p
->pack_size
)
462 die("packfile %s size mismatch.", p
->pack_name
);
463 map
= mmap(NULL
, p
->pack_size
, PROT_READ
, MAP_PRIVATE
, fd
, 0);
465 if (map
== MAP_FAILED
)
466 die("packfile %s cannot be mapped.", p
->pack_name
);
469 /* Check if the pack file matches with the index file.
472 if (memcmp((char*)(p
->index_base
) + p
->index_size
- 40,
473 p
->pack_base
+ p
->pack_size
- 20, 20)) {
475 die("packfile %s does not match index.", p
->pack_name
);
478 p
->pack_last_used
= pack_used_ctr
++;
483 struct packed_git
*add_packed_git(char *path
, int path_len
, int local
)
486 struct packed_git
*p
;
487 unsigned long idx_size
;
489 unsigned char sha1
[20];
491 if (check_packed_git_idx(path
, &idx_size
, &idx_map
))
494 /* do we have a corresponding .pack file? */
495 strcpy(path
+ path_len
- 4, ".pack");
496 if (stat(path
, &st
) || !S_ISREG(st
.st_mode
)) {
497 munmap(idx_map
, idx_size
);
500 /* ok, it looks sane as far as we can check without
501 * actually mapping the pack file.
503 p
= xmalloc(sizeof(*p
) + path_len
+ 2);
504 strcpy(p
->pack_name
, path
);
505 p
->index_size
= idx_size
;
506 p
->pack_size
= st
.st_size
;
507 p
->index_base
= idx_map
;
510 p
->pack_last_used
= 0;
512 p
->pack_local
= local
;
513 if ((path_len
> 44) && !get_sha1_hex(path
+ path_len
- 44, sha1
))
514 memcpy(p
->sha1
, sha1
, 20);
518 struct packed_git
*parse_pack_index(unsigned char *sha1
)
520 char *path
= sha1_pack_index_name(sha1
);
521 return parse_pack_index_file(sha1
, path
);
524 struct packed_git
*parse_pack_index_file(const unsigned char *sha1
, char *idx_path
)
526 struct packed_git
*p
;
527 unsigned long idx_size
;
531 if (check_packed_git_idx(idx_path
, &idx_size
, &idx_map
))
534 path
= sha1_pack_name(sha1
);
536 p
= xmalloc(sizeof(*p
) + strlen(path
) + 2);
537 strcpy(p
->pack_name
, path
);
538 p
->index_size
= idx_size
;
540 p
->index_base
= idx_map
;
543 p
->pack_last_used
= 0;
545 memcpy(p
->sha1
, sha1
, 20);
549 void install_packed_git(struct packed_git
*pack
)
551 pack
->next
= packed_git
;
555 static void prepare_packed_git_one(char *objdir
, int local
)
562 sprintf(path
, "%s/pack", objdir
);
567 error("unable to open object pack directory: %s: %s",
568 path
, strerror(errno
));
572 while ((de
= readdir(dir
)) != NULL
) {
573 int namelen
= strlen(de
->d_name
);
574 struct packed_git
*p
;
576 if (strcmp(de
->d_name
+ namelen
- 4, ".idx"))
579 /* we have .idx. Is it a file we can map? */
580 strcpy(path
+ len
, de
->d_name
);
581 p
= add_packed_git(path
, len
+ namelen
, local
);
584 p
->next
= packed_git
;
590 void prepare_packed_git(void)
592 static int run_once
= 0;
593 struct alternate_object_database
*alt
;
597 prepare_packed_git_one(get_object_directory(), 1);
599 for (alt
= alt_odb_list
; alt
; alt
= alt
->next
) {
601 prepare_packed_git_one(alt
->base
, 0);
607 int check_sha1_signature(const unsigned char *sha1
, void *map
, unsigned long size
, const char *type
)
610 unsigned char real_sha1
[20];
614 SHA1_Update(&c
, header
, 1+sprintf(header
, "%s %lu", type
, size
));
615 SHA1_Update(&c
, map
, size
);
616 SHA1_Final(real_sha1
, &c
);
617 return memcmp(sha1
, real_sha1
, 20) ? -1 : 0;
620 static void *map_sha1_file_internal(const unsigned char *sha1
,
626 char *filename
= find_sha1_file(sha1
, &st
);
632 fd
= open(filename
, O_RDONLY
| sha1_file_open_flag
);
634 /* See if it works without O_NOATIME */
635 switch (sha1_file_open_flag
) {
637 fd
= open(filename
, O_RDONLY
);
645 /* If it failed once, it will probably fail again.
646 * Stop using O_NOATIME
648 sha1_file_open_flag
= 0;
650 map
= mmap(NULL
, st
.st_size
, PROT_READ
, MAP_PRIVATE
, fd
, 0);
652 if (map
== MAP_FAILED
)
658 int unpack_sha1_header(z_stream
*stream
, void *map
, unsigned long mapsize
, void *buffer
, unsigned long size
)
660 /* Get the data stream */
661 memset(stream
, 0, sizeof(*stream
));
662 stream
->next_in
= map
;
663 stream
->avail_in
= mapsize
;
664 stream
->next_out
= buffer
;
665 stream
->avail_out
= size
;
668 return inflate(stream
, 0);
671 static void *unpack_sha1_rest(z_stream
*stream
, void *buffer
, unsigned long size
)
673 int bytes
= strlen(buffer
) + 1;
674 unsigned char *buf
= xmalloc(1+size
);
676 memcpy(buf
, buffer
+ bytes
, stream
->total_out
- bytes
);
677 bytes
= stream
->total_out
- bytes
;
679 stream
->next_out
= buf
+ bytes
;
680 stream
->avail_out
= size
- bytes
;
681 while (inflate(stream
, Z_FINISH
) == Z_OK
)
690 * We used to just use "sscanf()", but that's actually way
691 * too permissive for what we want to check. So do an anal
692 * object header parse by hand.
694 int parse_sha1_header(char *hdr
, char *type
, unsigned long *sizep
)
700 * The type can be at most ten bytes (including the
701 * terminating '\0' that we add), and is followed by
716 * The length must follow immediately, and be in canonical
717 * decimal format (ie "010" is not valid).
724 unsigned long c
= *hdr
- '0';
728 size
= size
* 10 + c
;
734 * The length must be followed by a zero byte
736 return *hdr
? -1 : 0;
739 void * unpack_sha1_file(void *map
, unsigned long mapsize
, char *type
, unsigned long *size
)
745 ret
= unpack_sha1_header(&stream
, map
, mapsize
, hdr
, sizeof(hdr
));
746 if (ret
< Z_OK
|| parse_sha1_header(hdr
, type
, size
) < 0)
749 return unpack_sha1_rest(&stream
, hdr
, *size
);
752 /* forward declaration for a mutually recursive function */
753 static int packed_object_info(struct pack_entry
*entry
,
754 char *type
, unsigned long *sizep
);
756 static int packed_delta_info(unsigned char *base_sha1
,
757 unsigned long delta_size
,
760 unsigned long *sizep
,
761 struct packed_git
*p
)
763 struct pack_entry base_ent
;
766 die("truncated pack file");
768 /* The base entry _must_ be in the same pack */
769 if (!find_pack_entry_one(base_sha1
, &base_ent
, p
))
770 die("failed to find delta-pack base object %s",
771 sha1_to_hex(base_sha1
));
773 /* We choose to only get the type of the base object and
774 * ignore potentially corrupt pack file that expects the delta
775 * based on a base with a wrong size. This saves tons of
779 if (packed_object_info(&base_ent
, type
, NULL
))
780 die("cannot get info for delta-pack base");
783 const unsigned char *data
;
784 unsigned char delta_head
[64];
785 unsigned long result_size
;
789 memset(&stream
, 0, sizeof(stream
));
791 data
= stream
.next_in
= base_sha1
+ 20;
792 stream
.avail_in
= left
- 20;
793 stream
.next_out
= delta_head
;
794 stream
.avail_out
= sizeof(delta_head
);
796 inflateInit(&stream
);
797 st
= inflate(&stream
, Z_FINISH
);
799 if ((st
!= Z_STREAM_END
) &&
800 stream
.total_out
!= sizeof(delta_head
))
801 die("delta data unpack-initial failed");
803 /* Examine the initial part of the delta to figure out
807 get_delta_hdr_size(&data
); /* ignore base size */
809 /* Read the result size */
810 result_size
= get_delta_hdr_size(&data
);
811 *sizep
= result_size
;
816 static unsigned long unpack_object_header(struct packed_git
*p
, unsigned long offset
,
817 enum object_type
*type
, unsigned long *sizep
)
820 unsigned char *pack
, c
;
823 if (offset
>= p
->pack_size
)
824 die("object offset outside of pack file");
826 pack
= p
->pack_base
+ offset
;
829 *type
= (c
>> 4) & 7;
833 if (offset
>= p
->pack_size
)
834 die("object offset outside of pack file");
837 size
+= (c
& 0x7f) << shift
;
844 int check_reuse_pack_delta(struct packed_git
*p
, unsigned long offset
,
845 unsigned char *base
, unsigned long *sizep
,
846 enum object_type
*kindp
)
853 ptr
= unpack_object_header(p
, ptr
, kindp
, sizep
);
854 if (*kindp
!= OBJ_DELTA
)
856 memcpy(base
, p
->pack_base
+ ptr
, 20);
863 void packed_object_info_detail(struct pack_entry
*e
,
866 unsigned long *store_size
,
867 unsigned int *delta_chain_length
,
868 unsigned char *base_sha1
)
870 struct packed_git
*p
= e
->p
;
871 unsigned long offset
, left
;
873 enum object_type kind
;
875 offset
= unpack_object_header(p
, e
->offset
, &kind
, size
);
876 pack
= p
->pack_base
+ offset
;
877 left
= p
->pack_size
- offset
;
878 if (kind
!= OBJ_DELTA
)
879 *delta_chain_length
= 0;
881 unsigned int chain_length
= 0;
882 memcpy(base_sha1
, pack
, 20);
884 struct pack_entry base_ent
;
887 find_pack_entry_one(pack
, &base_ent
, p
);
888 offset
= unpack_object_header(p
, base_ent
.offset
,
890 pack
= p
->pack_base
+ offset
;
892 } while (kind
== OBJ_DELTA
);
893 *delta_chain_length
= chain_length
;
897 strcpy(type
, "commit");
900 strcpy(type
, "tree");
903 strcpy(type
, "blob");
909 die("corrupted pack file %s containing object of kind %d",
912 *store_size
= 0; /* notyet */
915 static int packed_object_info(struct pack_entry
*entry
,
916 char *type
, unsigned long *sizep
)
918 struct packed_git
*p
= entry
->p
;
919 unsigned long offset
, size
, left
;
921 enum object_type kind
;
924 if (use_packed_git(p
))
925 die("cannot map packed file");
927 offset
= unpack_object_header(p
, entry
->offset
, &kind
, &size
);
928 pack
= p
->pack_base
+ offset
;
929 left
= p
->pack_size
- offset
;
933 retval
= packed_delta_info(pack
, size
, left
, type
, sizep
, p
);
937 strcpy(type
, "commit");
940 strcpy(type
, "tree");
943 strcpy(type
, "blob");
949 die("corrupted pack file %s containing object of kind %d",
958 /* forward declaration for a mutually recursive function */
959 static void *unpack_entry(struct pack_entry
*, char *, unsigned long *);
961 static void *unpack_delta_entry(unsigned char *base_sha1
,
962 unsigned long delta_size
,
965 unsigned long *sizep
,
966 struct packed_git
*p
)
968 struct pack_entry base_ent
;
969 void *data
, *delta_data
, *result
, *base
;
970 unsigned long data_size
, result_size
, base_size
;
975 die("truncated pack file");
977 /* The base entry _must_ be in the same pack */
978 if (!find_pack_entry_one(base_sha1
, &base_ent
, p
))
979 die("failed to find delta-pack base object %s",
980 sha1_to_hex(base_sha1
));
981 base
= unpack_entry_gently(&base_ent
, type
, &base_size
);
983 die("failed to read delta-pack base object %s",
984 sha1_to_hex(base_sha1
));
986 data
= base_sha1
+ 20;
987 data_size
= left
- 20;
988 delta_data
= xmalloc(delta_size
);
990 memset(&stream
, 0, sizeof(stream
));
992 stream
.next_in
= data
;
993 stream
.avail_in
= data_size
;
994 stream
.next_out
= delta_data
;
995 stream
.avail_out
= delta_size
;
997 inflateInit(&stream
);
998 st
= inflate(&stream
, Z_FINISH
);
1000 if ((st
!= Z_STREAM_END
) || stream
.total_out
!= delta_size
)
1001 die("delta data unpack failed");
1003 result
= patch_delta(base
, base_size
,
1004 delta_data
, delta_size
,
1007 die("failed to apply delta");
1010 *sizep
= result_size
;
1014 static void *unpack_non_delta_entry(unsigned char *data
,
1020 unsigned char *buffer
;
1022 buffer
= xmalloc(size
+ 1);
1024 memset(&stream
, 0, sizeof(stream
));
1025 stream
.next_in
= data
;
1026 stream
.avail_in
= left
;
1027 stream
.next_out
= buffer
;
1028 stream
.avail_out
= size
;
1030 inflateInit(&stream
);
1031 st
= inflate(&stream
, Z_FINISH
);
1032 inflateEnd(&stream
);
1033 if ((st
!= Z_STREAM_END
) || stream
.total_out
!= size
) {
1041 static void *unpack_entry(struct pack_entry
*entry
,
1042 char *type
, unsigned long *sizep
)
1044 struct packed_git
*p
= entry
->p
;
1047 if (use_packed_git(p
))
1048 die("cannot map packed file");
1049 retval
= unpack_entry_gently(entry
, type
, sizep
);
1050 unuse_packed_git(p
);
1052 die("corrupted pack file %s", p
->pack_name
);
1056 /* The caller is responsible for use_packed_git()/unuse_packed_git() pair */
1057 void *unpack_entry_gently(struct pack_entry
*entry
,
1058 char *type
, unsigned long *sizep
)
1060 struct packed_git
*p
= entry
->p
;
1061 unsigned long offset
, size
, left
;
1062 unsigned char *pack
;
1063 enum object_type kind
;
1066 offset
= unpack_object_header(p
, entry
->offset
, &kind
, &size
);
1067 pack
= p
->pack_base
+ offset
;
1068 left
= p
->pack_size
- offset
;
1071 retval
= unpack_delta_entry(pack
, size
, left
, type
, sizep
, p
);
1074 strcpy(type
, "commit");
1077 strcpy(type
, "tree");
1080 strcpy(type
, "blob");
1083 strcpy(type
, "tag");
1089 retval
= unpack_non_delta_entry(pack
, size
, left
);
1093 int num_packed_objects(const struct packed_git
*p
)
1095 /* See check_packed_git_idx() */
1096 return (p
->index_size
- 20 - 20 - 4*256) / 24;
1099 int nth_packed_object_sha1(const struct packed_git
*p
, int n
,
1100 unsigned char* sha1
)
1102 void *index
= p
->index_base
+ 256;
1103 if (n
< 0 || num_packed_objects(p
) <= n
)
1105 memcpy(sha1
, (index
+ 24 * n
+ 4), 20);
1109 int find_pack_entry_one(const unsigned char *sha1
,
1110 struct pack_entry
*e
, struct packed_git
*p
)
1112 unsigned int *level1_ofs
= p
->index_base
;
1113 int hi
= ntohl(level1_ofs
[*sha1
]);
1114 int lo
= ((*sha1
== 0x0) ? 0 : ntohl(level1_ofs
[*sha1
- 1]));
1115 void *index
= p
->index_base
+ 256;
1118 int mi
= (lo
+ hi
) / 2;
1119 int cmp
= memcmp(index
+ 24 * mi
+ 4, sha1
, 20);
1121 e
->offset
= ntohl(*((int*)(index
+ 24 * mi
)));
1122 memcpy(e
->sha1
, sha1
, 20);
1134 static int find_pack_entry(const unsigned char *sha1
, struct pack_entry
*e
)
1136 struct packed_git
*p
;
1137 prepare_packed_git();
1139 for (p
= packed_git
; p
; p
= p
->next
) {
1140 if (find_pack_entry_one(sha1
, e
, p
))
1146 struct packed_git
*find_sha1_pack(const unsigned char *sha1
,
1147 struct packed_git
*packs
)
1149 struct packed_git
*p
;
1150 struct pack_entry e
;
1152 for (p
= packs
; p
; p
= p
->next
) {
1153 if (find_pack_entry_one(sha1
, &e
, p
))
1160 int sha1_object_info(const unsigned char *sha1
, char *type
, unsigned long *sizep
)
1163 unsigned long mapsize
, size
;
1168 map
= map_sha1_file_internal(sha1
, &mapsize
);
1170 struct pack_entry e
;
1172 if (!find_pack_entry(sha1
, &e
))
1173 return error("unable to find %s", sha1_to_hex(sha1
));
1174 return packed_object_info(&e
, type
, sizep
);
1176 if (unpack_sha1_header(&stream
, map
, mapsize
, hdr
, sizeof(hdr
)) < 0)
1177 status
= error("unable to unpack %s header",
1179 if (parse_sha1_header(hdr
, type
, &size
) < 0)
1180 status
= error("unable to parse %s header", sha1_to_hex(sha1
));
1186 inflateEnd(&stream
);
1187 munmap(map
, mapsize
);
1191 static void *read_packed_sha1(const unsigned char *sha1
, char *type
, unsigned long *size
)
1193 struct pack_entry e
;
1195 if (!find_pack_entry(sha1
, &e
)) {
1196 error("cannot read sha1_file for %s", sha1_to_hex(sha1
));
1199 return unpack_entry(&e
, type
, size
);
1202 void * read_sha1_file(const unsigned char *sha1
, char *type
, unsigned long *size
)
1204 unsigned long mapsize
;
1206 struct pack_entry e
;
1208 if (find_pack_entry(sha1
, &e
))
1209 return read_packed_sha1(sha1
, type
, size
);
1210 map
= map_sha1_file_internal(sha1
, &mapsize
);
1212 buf
= unpack_sha1_file(map
, mapsize
, type
, size
);
1213 munmap(map
, mapsize
);
1219 void *read_object_with_reference(const unsigned char *sha1
,
1220 const char *required_type
,
1221 unsigned long *size
,
1222 unsigned char *actual_sha1_return
)
1226 unsigned long isize
;
1227 unsigned char actual_sha1
[20];
1229 memcpy(actual_sha1
, sha1
, 20);
1231 int ref_length
= -1;
1232 const char *ref_type
= NULL
;
1234 buffer
= read_sha1_file(actual_sha1
, type
, &isize
);
1237 if (!strcmp(type
, required_type
)) {
1239 if (actual_sha1_return
)
1240 memcpy(actual_sha1_return
, actual_sha1
, 20);
1243 /* Handle references */
1244 else if (!strcmp(type
, "commit"))
1246 else if (!strcmp(type
, "tag"))
1247 ref_type
= "object ";
1252 ref_length
= strlen(ref_type
);
1254 if (memcmp(buffer
, ref_type
, ref_length
) ||
1255 get_sha1_hex(buffer
+ ref_length
, actual_sha1
)) {
1260 /* Now we have the ID of the referred-to object in
1261 * actual_sha1. Check again. */
1265 char *write_sha1_file_prepare(void *buf
,
1268 unsigned char *sha1
,
1274 /* Generate the header */
1275 *hdrlen
= sprintf((char *)hdr
, "%s %lu", type
, len
)+1;
1279 SHA1_Update(&c
, hdr
, *hdrlen
);
1280 SHA1_Update(&c
, buf
, len
);
1281 SHA1_Final(sha1
, &c
);
1283 return sha1_file_name(sha1
);
1287 * Link the tempfile to the final place, possibly creating the
1288 * last directory level as you do so.
1290 * Returns the errno on failure, 0 on success.
1292 static int link_temp_to_file(const char *tmpfile
, char *filename
)
1296 if (!link(tmpfile
, filename
))
1300 * Try to mkdir the last path component if that failed
1303 * Re-try the "link()" regardless of whether the mkdir
1304 * succeeds, since a race might mean that somebody
1308 if (ret
== ENOENT
) {
1309 char *dir
= strrchr(filename
, '/');
1312 mkdir(filename
, 0777);
1313 if (adjust_shared_perm(filename
))
1316 if (!link(tmpfile
, filename
))
1325 * Move the just written object into its final resting place
1327 int move_temp_to_file(const char *tmpfile
, char *filename
)
1329 int ret
= link_temp_to_file(tmpfile
, filename
);
1332 * Coda hack - coda doesn't like cross-directory links,
1333 * so we fall back to a rename, which will mean that it
1334 * won't be able to check collisions, but that's not a
1337 * The same holds for FAT formatted media.
1339 * When this succeeds, we just return 0. We have nothing
1342 if (ret
&& ret
!= EEXIST
) {
1343 if (!rename(tmpfile
, filename
))
1349 if (ret
!= EEXIST
) {
1350 fprintf(stderr
, "unable to write sha1 filename %s: %s\n", filename
, strerror(ret
));
1353 /* FIXME!!! Collision check here ? */
1359 int write_sha1_file(void *buf
, unsigned long len
, const char *type
, unsigned char *returnsha1
)
1362 unsigned char *compressed
;
1364 unsigned char sha1
[20];
1366 static char tmpfile
[PATH_MAX
];
1367 unsigned char hdr
[50];
1370 /* Normally if we have it in the pack then we do not bother writing
1371 * it out into .git/objects/??/?{38} file.
1373 filename
= write_sha1_file_prepare(buf
, len
, type
, sha1
, hdr
, &hdrlen
);
1375 memcpy(returnsha1
, sha1
, 20);
1376 if (has_sha1_file(sha1
))
1378 fd
= open(filename
, O_RDONLY
);
1381 * FIXME!!! We might do collision checking here, but we'd
1382 * need to uncompress the old file and check it. Later.
1388 if (errno
!= ENOENT
) {
1389 fprintf(stderr
, "sha1 file %s: %s\n", filename
, strerror(errno
));
1393 snprintf(tmpfile
, sizeof(tmpfile
), "%s/obj_XXXXXX", get_object_directory());
1395 fd
= mkstemp(tmpfile
);
1397 fprintf(stderr
, "unable to create temporary sha1 filename %s: %s\n", tmpfile
, strerror(errno
));
1402 memset(&stream
, 0, sizeof(stream
));
1403 deflateInit(&stream
, Z_BEST_COMPRESSION
);
1404 size
= deflateBound(&stream
, len
+hdrlen
);
1405 compressed
= xmalloc(size
);
1408 stream
.next_out
= compressed
;
1409 stream
.avail_out
= size
;
1411 /* First header.. */
1412 stream
.next_in
= hdr
;
1413 stream
.avail_in
= hdrlen
;
1414 while (deflate(&stream
, 0) == Z_OK
)
1417 /* Then the data itself.. */
1418 stream
.next_in
= buf
;
1419 stream
.avail_in
= len
;
1420 while (deflate(&stream
, Z_FINISH
) == Z_OK
)
1422 deflateEnd(&stream
);
1423 size
= stream
.total_out
;
1425 if (write(fd
, compressed
, size
) != size
)
1426 die("unable to write file");
1431 return move_temp_to_file(tmpfile
, filename
);
1434 int write_sha1_to_fd(int fd
, const unsigned char *sha1
)
1437 unsigned long objsize
;
1439 void *map
= map_sha1_file_internal(sha1
, &objsize
);
1441 void *temp_obj
= NULL
;
1445 unsigned char *unpacked
;
1450 // need to unpack and recompress it by itself
1451 unpacked
= read_packed_sha1(sha1
, type
, &len
);
1453 hdrlen
= sprintf(hdr
, "%s %lu", type
, len
) + 1;
1456 memset(&stream
, 0, sizeof(stream
));
1457 deflateInit(&stream
, Z_BEST_COMPRESSION
);
1458 size
= deflateBound(&stream
, len
+ hdrlen
);
1459 temp_obj
= buf
= xmalloc(size
);
1462 stream
.next_out
= buf
;
1463 stream
.avail_out
= size
;
1465 /* First header.. */
1466 stream
.next_in
= (void *)hdr
;
1467 stream
.avail_in
= hdrlen
;
1468 while (deflate(&stream
, 0) == Z_OK
)
1471 /* Then the data itself.. */
1472 stream
.next_in
= unpacked
;
1473 stream
.avail_in
= len
;
1474 while (deflate(&stream
, Z_FINISH
) == Z_OK
)
1476 deflateEnd(&stream
);
1479 objsize
= stream
.total_out
;
1483 size
= write(fd
, buf
+ posn
, objsize
- posn
);
1486 fprintf(stderr
, "write closed\n");
1493 } while (posn
< objsize
);
1496 munmap(map
, objsize
);
1503 int write_sha1_from_fd(const unsigned char *sha1
, int fd
, char *buffer
,
1504 size_t bufsize
, size_t *bufposn
)
1506 char tmpfile
[PATH_MAX
];
1509 unsigned char real_sha1
[20];
1510 unsigned char discard
[4096];
1514 snprintf(tmpfile
, sizeof(tmpfile
), "%s/obj_XXXXXX", get_object_directory());
1516 local
= mkstemp(tmpfile
);
1518 return error("Couldn't open %s for %s",
1519 tmpfile
, sha1_to_hex(sha1
));
1521 memset(&stream
, 0, sizeof(stream
));
1523 inflateInit(&stream
);
1530 stream
.avail_in
= *bufposn
;
1531 stream
.next_in
= (unsigned char *) buffer
;
1533 stream
.next_out
= discard
;
1534 stream
.avail_out
= sizeof(discard
);
1535 ret
= inflate(&stream
, Z_SYNC_FLUSH
);
1536 SHA1_Update(&c
, discard
, sizeof(discard
) -
1538 } while (stream
.avail_in
&& ret
== Z_OK
);
1539 write(local
, buffer
, *bufposn
- stream
.avail_in
);
1540 memmove(buffer
, buffer
+ *bufposn
- stream
.avail_in
,
1542 *bufposn
= stream
.avail_in
;
1546 size
= read(fd
, buffer
+ *bufposn
, bufsize
- *bufposn
);
1551 return error("Connection closed?");
1552 perror("Reading from connection");
1557 inflateEnd(&stream
);
1560 SHA1_Final(real_sha1
, &c
);
1561 if (ret
!= Z_STREAM_END
) {
1563 return error("File %s corrupted", sha1_to_hex(sha1
));
1565 if (memcmp(sha1
, real_sha1
, 20)) {
1567 return error("File %s has bad hash", sha1_to_hex(sha1
));
1570 return move_temp_to_file(tmpfile
, sha1_file_name(sha1
));
1573 int has_pack_index(const unsigned char *sha1
)
1576 if (stat(sha1_pack_index_name(sha1
), &st
))
1581 int has_pack_file(const unsigned char *sha1
)
1584 if (stat(sha1_pack_name(sha1
), &st
))
1589 int has_sha1_pack(const unsigned char *sha1
)
1591 struct pack_entry e
;
1592 return find_pack_entry(sha1
, &e
);
1595 int has_sha1_file(const unsigned char *sha1
)
1598 struct pack_entry e
;
1600 if (find_pack_entry(sha1
, &e
))
1602 return find_sha1_file(sha1
, &st
) ? 1 : 0;
1605 int index_pipe(unsigned char *sha1
, int fd
, const char *type
, int write_object
)
1607 unsigned long size
= 4096;
1608 char *buf
= malloc(size
);
1610 unsigned long off
= 0;
1611 unsigned char hdr
[50];
1614 iret
= read(fd
, buf
+ off
, size
- off
);
1619 buf
= realloc(buf
, size
);
1630 ret
= write_sha1_file(buf
, off
, type
, sha1
);
1632 write_sha1_file_prepare(buf
, off
, type
, sha1
, hdr
, &hdrlen
);
1639 int index_fd(unsigned char *sha1
, int fd
, struct stat
*st
, int write_object
, const char *type
)
1641 unsigned long size
= st
->st_size
;
1644 unsigned char hdr
[50];
1649 buf
= mmap(NULL
, size
, PROT_READ
, MAP_PRIVATE
, fd
, 0);
1651 if (buf
== MAP_FAILED
)
1657 ret
= write_sha1_file(buf
, size
, type
, sha1
);
1659 write_sha1_file_prepare(buf
, size
, type
, sha1
, hdr
, &hdrlen
);
1667 int index_path(unsigned char *sha1
, const char *path
, struct stat
*st
, int write_object
)
1672 switch (st
->st_mode
& S_IFMT
) {
1674 fd
= open(path
, O_RDONLY
);
1676 return error("open(\"%s\"): %s", path
,
1678 if (index_fd(sha1
, fd
, st
, write_object
, NULL
) < 0)
1679 return error("%s: failed to insert into database",
1683 target
= xmalloc(st
->st_size
+1);
1684 if (readlink(path
, target
, st
->st_size
+1) != st
->st_size
) {
1685 char *errstr
= strerror(errno
);
1687 return error("readlink(\"%s\"): %s", path
,
1690 if (!write_object
) {
1691 unsigned char hdr
[50];
1693 write_sha1_file_prepare(target
, st
->st_size
, "blob",
1694 sha1
, hdr
, &hdrlen
);
1695 } else if (write_sha1_file(target
, st
->st_size
, "blob", sha1
))
1696 return error("%s: failed to insert into database",
1701 return error("%s: unsupported file type", path
);