2 * GIT - The information manager from hell
4 * Copyright (C) Linus Torvalds, 2005
6 * This handles basic git sha1 object files - packing, unpacking,
16 #if defined(__linux__) && (defined(__i386__) || defined(__PPC__))
17 #define O_NOATIME 01000000
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 static char *git_dir
, *git_object_dir
, *git_index_file
, *git_refs_dir
,
51 static void setup_git_env(void)
53 git_dir
= getenv(GIT_DIR_ENVIRONMENT
);
55 git_dir
= DEFAULT_GIT_DIR_ENVIRONMENT
;
56 git_object_dir
= getenv(DB_ENVIRONMENT
);
57 if (!git_object_dir
) {
58 git_object_dir
= xmalloc(strlen(git_dir
) + 9);
59 sprintf(git_object_dir
, "%s/objects", git_dir
);
61 git_refs_dir
= xmalloc(strlen(git_dir
) + 6);
62 sprintf(git_refs_dir
, "%s/refs", git_dir
);
63 git_index_file
= getenv(INDEX_ENVIRONMENT
);
64 if (!git_index_file
) {
65 git_index_file
= xmalloc(strlen(git_dir
) + 7);
66 sprintf(git_index_file
, "%s/index", git_dir
);
68 git_graft_file
= getenv(GRAFT_ENVIRONMENT
);
70 git_graft_file
= strdup(git_path("info/grafts"));
73 char *get_object_directory(void)
77 return git_object_dir
;
80 char *get_refs_directory(void)
87 char *get_index_file(void)
91 return git_index_file
;
94 char *get_graft_file(void)
98 return git_graft_file
;
101 int safe_create_leading_directories(char *path
)
106 pos
= strchr(pos
, '/');
110 if (mkdir(path
, 0777) < 0)
111 if (errno
!= EEXIST
) {
120 char * sha1_to_hex(const unsigned char *sha1
)
122 static char buffer
[50];
123 static const char hex
[] = "0123456789abcdef";
127 for (i
= 0; i
< 20; i
++) {
128 unsigned int val
= *sha1
++;
129 *buf
++ = hex
[val
>> 4];
130 *buf
++ = hex
[val
& 0xf];
135 static void fill_sha1_path(char *pathbuf
, const unsigned char *sha1
)
138 for (i
= 0; i
< 20; i
++) {
139 static char hex
[] = "0123456789abcdef";
140 unsigned int val
= sha1
[i
];
141 char *pos
= pathbuf
+ i
*2 + (i
> 0);
142 *pos
++ = hex
[val
>> 4];
143 *pos
= hex
[val
& 0xf];
148 * NOTE! This returns a statically allocated buffer, so you have to be
149 * careful about using it. Do a "strdup()" if you need to save the
152 * Also note that this returns the location for creating. Reading
153 * SHA1 file can happen from any alternate directory listed in the
154 * DB_ENVIRONMENT environment variable if it is not found in
155 * the primary object database.
157 char *sha1_file_name(const unsigned char *sha1
)
159 static char *name
, *base
;
162 const char *sha1_file_directory
= get_object_directory();
163 int len
= strlen(sha1_file_directory
);
164 base
= xmalloc(len
+ 60);
165 memcpy(base
, sha1_file_directory
, len
);
166 memset(base
+len
, 0, 60);
169 name
= base
+ len
+ 1;
171 fill_sha1_path(name
, sha1
);
175 char *sha1_pack_name(const unsigned char *sha1
)
177 static const char hex
[] = "0123456789abcdef";
178 static char *name
, *base
, *buf
;
182 const char *sha1_file_directory
= get_object_directory();
183 int len
= strlen(sha1_file_directory
);
184 base
= xmalloc(len
+ 60);
185 sprintf(base
, "%s/pack/pack-1234567890123456789012345678901234567890.pack", sha1_file_directory
);
186 name
= base
+ len
+ 11;
191 for (i
= 0; i
< 20; i
++) {
192 unsigned int val
= *sha1
++;
193 *buf
++ = hex
[val
>> 4];
194 *buf
++ = hex
[val
& 0xf];
200 char *sha1_pack_index_name(const unsigned char *sha1
)
202 static const char hex
[] = "0123456789abcdef";
203 static char *name
, *base
, *buf
;
207 const char *sha1_file_directory
= get_object_directory();
208 int len
= strlen(sha1_file_directory
);
209 base
= xmalloc(len
+ 60);
210 sprintf(base
, "%s/pack/pack-1234567890123456789012345678901234567890.idx", sha1_file_directory
);
211 name
= base
+ len
+ 11;
216 for (i
= 0; i
< 20; i
++) {
217 unsigned int val
= *sha1
++;
218 *buf
++ = hex
[val
>> 4];
219 *buf
++ = hex
[val
& 0xf];
225 struct alternate_object_database
*alt_odb_list
;
226 static struct alternate_object_database
**alt_odb_tail
;
229 * Prepare alternate object database registry.
231 * The variable alt_odb_list points at the list of struct
232 * alternate_object_database. The elements on this list come from
233 * non-empty elements from colon separated ALTERNATE_DB_ENVIRONMENT
234 * environment variable, and $GIT_OBJECT_DIRECTORY/info/alternates,
235 * whose contents is exactly in the same format as that environment
236 * variable. Its base points at a statically allocated buffer that
237 * contains "/the/directory/corresponding/to/.git/objects/...", while
238 * its name points just after the slash at the end of ".git/objects/"
239 * in the example above, and has enough space to hold 40-byte hex
240 * SHA1, an extra slash for the first level indirection, and the
243 static void link_alt_odb_entries(const char *alt
, const char *ep
, int sep
,
244 const char *relative_base
)
246 const char *cp
, *last
;
247 struct alternate_object_database
*ent
;
253 if (cp
< ep
&& *cp
== '#') {
254 while (cp
< ep
&& *cp
!= sep
)
259 for ( ; cp
< ep
&& *cp
!= sep
; cp
++)
262 /* 43 = 40-byte + 2 '/' + terminating NUL */
263 int pfxlen
= cp
- last
;
264 int entlen
= pfxlen
+ 43;
266 if (*last
!= '/' && relative_base
) {
267 /* Relative alt-odb */
269 base_len
= strlen(relative_base
) + 1;
273 ent
= xmalloc(sizeof(*ent
) + entlen
);
275 alt_odb_tail
= &(ent
->next
);
277 if (*last
!= '/' && relative_base
) {
278 memcpy(ent
->base
, relative_base
, base_len
- 1);
279 ent
->base
[base_len
- 1] = '/';
280 memcpy(ent
->base
+ base_len
,
284 memcpy(ent
->base
, last
, pfxlen
);
285 ent
->name
= ent
->base
+ pfxlen
+ 1;
286 ent
->base
[pfxlen
] = ent
->base
[pfxlen
+ 3] = '/';
287 ent
->base
[entlen
-1] = 0;
289 while (cp
< ep
&& *cp
== sep
)
295 void prepare_alt_odb(void)
303 alt
= getenv(ALTERNATE_DB_ENVIRONMENT
);
308 alt_odb_tail
= &alt_odb_list
;
309 link_alt_odb_entries(alt
, alt
+ strlen(alt
), ':', NULL
);
311 sprintf(path
, "%s/info/alternates", get_object_directory());
312 fd
= open(path
, O_RDONLY
);
315 if (fstat(fd
, &st
) || (st
.st_size
== 0)) {
319 map
= mmap(NULL
, st
.st_size
, PROT_READ
, MAP_PRIVATE
, fd
, 0);
321 if (map
== MAP_FAILED
)
324 link_alt_odb_entries(map
, map
+ st
.st_size
, '\n',
325 get_object_directory());
326 munmap(map
, st
.st_size
);
329 static char *find_sha1_file(const unsigned char *sha1
, struct stat
*st
)
331 char *name
= sha1_file_name(sha1
);
332 struct alternate_object_database
*alt
;
337 for (alt
= alt_odb_list
; alt
; alt
= alt
->next
) {
339 fill_sha1_path(name
, sha1
);
340 if (!stat(alt
->base
, st
))
346 #define PACK_MAX_SZ (1<<26)
347 static int pack_used_ctr
;
348 static unsigned long pack_mapped
;
349 struct packed_git
*packed_git
;
351 static int check_packed_git_idx(const char *path
, unsigned long *idx_size_
,
356 unsigned long idx_size
;
358 int fd
= open(path
, O_RDONLY
);
362 if (fstat(fd
, &st
)) {
366 idx_size
= st
.st_size
;
367 idx_map
= mmap(NULL
, idx_size
, PROT_READ
, MAP_PRIVATE
, fd
, 0);
369 if (idx_map
== MAP_FAILED
)
374 *idx_size_
= idx_size
;
376 /* check index map */
377 if (idx_size
< 4*256 + 20 + 20)
378 return error("index file too small");
380 for (i
= 0; i
< 256; i
++) {
381 unsigned int n
= ntohl(index
[i
]);
383 return error("non-monotonic index");
389 * - 256 index entries 4 bytes each
390 * - 24-byte entries * nr (20-byte sha1 + 4-byte offset)
391 * - 20-byte SHA1 of the packfile
392 * - 20-byte SHA1 file checksum
394 if (idx_size
!= 4*256 + nr
* 24 + 20 + 20)
395 return error("wrong index file size");
400 static int unuse_one_packed_git(void)
402 struct packed_git
*p
, *lru
= NULL
;
404 for (p
= packed_git
; p
; p
= p
->next
) {
405 if (p
->pack_use_cnt
|| !p
->pack_base
)
407 if (!lru
|| p
->pack_last_used
< lru
->pack_last_used
)
412 munmap(lru
->pack_base
, lru
->pack_size
);
413 lru
->pack_base
= NULL
;
417 void unuse_packed_git(struct packed_git
*p
)
422 int use_packed_git(struct packed_git
*p
)
426 // We created the struct before we had the pack
427 stat(p
->pack_name
, &st
);
428 if (!S_ISREG(st
.st_mode
))
429 die("packfile %s not a regular file", p
->pack_name
);
430 p
->pack_size
= st
.st_size
;
437 pack_mapped
+= p
->pack_size
;
438 while (PACK_MAX_SZ
< pack_mapped
&& unuse_one_packed_git())
440 fd
= open(p
->pack_name
, O_RDONLY
);
442 die("packfile %s cannot be opened", p
->pack_name
);
443 if (fstat(fd
, &st
)) {
445 die("packfile %s cannot be opened", p
->pack_name
);
447 if (st
.st_size
!= p
->pack_size
)
448 die("packfile %s size mismatch.", p
->pack_name
);
449 map
= mmap(NULL
, p
->pack_size
, PROT_READ
, MAP_PRIVATE
, fd
, 0);
451 if (map
== MAP_FAILED
)
452 die("packfile %s cannot be mapped.", p
->pack_name
);
455 /* Check if the pack file matches with the index file.
458 if (memcmp((char*)(p
->index_base
) + p
->index_size
- 40,
459 p
->pack_base
+ p
->pack_size
- 20, 20)) {
461 die("packfile %s does not match index.", p
->pack_name
);
464 p
->pack_last_used
= pack_used_ctr
++;
469 struct packed_git
*add_packed_git(char *path
, int path_len
)
472 struct packed_git
*p
;
473 unsigned long idx_size
;
476 if (check_packed_git_idx(path
, &idx_size
, &idx_map
))
479 /* do we have a corresponding .pack file? */
480 strcpy(path
+ path_len
- 4, ".pack");
481 if (stat(path
, &st
) || !S_ISREG(st
.st_mode
)) {
482 munmap(idx_map
, idx_size
);
485 /* ok, it looks sane as far as we can check without
486 * actually mapping the pack file.
488 p
= xmalloc(sizeof(*p
) + path_len
+ 2);
489 strcpy(p
->pack_name
, path
);
490 p
->index_size
= idx_size
;
491 p
->pack_size
= st
.st_size
;
492 p
->index_base
= idx_map
;
495 p
->pack_last_used
= 0;
500 struct packed_git
*parse_pack_index(unsigned char *sha1
)
502 char *path
= sha1_pack_index_name(sha1
);
503 return parse_pack_index_file(sha1
, path
);
506 struct packed_git
*parse_pack_index_file(const unsigned char *sha1
, char *idx_path
)
508 struct packed_git
*p
;
509 unsigned long idx_size
;
513 if (check_packed_git_idx(idx_path
, &idx_size
, &idx_map
))
516 path
= sha1_pack_name(sha1
);
518 p
= xmalloc(sizeof(*p
) + strlen(path
) + 2);
519 strcpy(p
->pack_name
, path
);
520 p
->index_size
= idx_size
;
522 p
->index_base
= idx_map
;
525 p
->pack_last_used
= 0;
527 memcpy(p
->sha1
, sha1
, 20);
531 void install_packed_git(struct packed_git
*pack
)
533 pack
->next
= packed_git
;
537 static void prepare_packed_git_one(char *objdir
)
544 sprintf(path
, "%s/pack", objdir
);
550 while ((de
= readdir(dir
)) != NULL
) {
551 int namelen
= strlen(de
->d_name
);
552 struct packed_git
*p
;
554 if (strcmp(de
->d_name
+ namelen
- 4, ".idx"))
557 /* we have .idx. Is it a file we can map? */
558 strcpy(path
+ len
, de
->d_name
);
559 p
= add_packed_git(path
, len
+ namelen
);
562 p
->next
= packed_git
;
568 void prepare_packed_git(void)
570 static int run_once
= 0;
571 struct alternate_object_database
*alt
;
575 prepare_packed_git_one(get_object_directory());
577 for (alt
= alt_odb_list
; alt
; alt
= alt
->next
) {
579 prepare_packed_git_one(alt
->base
);
584 int check_sha1_signature(const unsigned char *sha1
, void *map
, unsigned long size
, const char *type
)
587 unsigned char real_sha1
[20];
591 SHA1_Update(&c
, header
, 1+sprintf(header
, "%s %lu", type
, size
));
592 SHA1_Update(&c
, map
, size
);
593 SHA1_Final(real_sha1
, &c
);
594 return memcmp(sha1
, real_sha1
, 20) ? -1 : 0;
597 static void *map_sha1_file_internal(const unsigned char *sha1
,
603 char *filename
= find_sha1_file(sha1
, &st
);
609 fd
= open(filename
, O_RDONLY
| sha1_file_open_flag
);
611 /* See if it works without O_NOATIME */
612 switch (sha1_file_open_flag
) {
614 fd
= open(filename
, O_RDONLY
);
622 /* If it failed once, it will probably fail again.
623 * Stop using O_NOATIME
625 sha1_file_open_flag
= 0;
627 map
= mmap(NULL
, st
.st_size
, PROT_READ
, MAP_PRIVATE
, fd
, 0);
629 if (map
== MAP_FAILED
)
635 int unpack_sha1_header(z_stream
*stream
, void *map
, unsigned long mapsize
, void *buffer
, unsigned long size
)
637 /* Get the data stream */
638 memset(stream
, 0, sizeof(*stream
));
639 stream
->next_in
= map
;
640 stream
->avail_in
= mapsize
;
641 stream
->next_out
= buffer
;
642 stream
->avail_out
= size
;
645 return inflate(stream
, 0);
648 static void *unpack_sha1_rest(z_stream
*stream
, void *buffer
, unsigned long size
)
650 int bytes
= strlen(buffer
) + 1;
651 unsigned char *buf
= xmalloc(1+size
);
653 memcpy(buf
, buffer
+ bytes
, stream
->total_out
- bytes
);
654 bytes
= stream
->total_out
- bytes
;
656 stream
->next_out
= buf
+ bytes
;
657 stream
->avail_out
= size
- bytes
;
658 while (inflate(stream
, Z_FINISH
) == Z_OK
)
667 * We used to just use "sscanf()", but that's actually way
668 * too permissive for what we want to check. So do an anal
669 * object header parse by hand.
671 int parse_sha1_header(char *hdr
, char *type
, unsigned long *sizep
)
677 * The type can be at most ten bytes (including the
678 * terminating '\0' that we add), and is followed by
693 * The length must follow immediately, and be in canonical
694 * decimal format (ie "010" is not valid).
701 unsigned long c
= *hdr
- '0';
705 size
= size
* 10 + c
;
711 * The length must be followed by a zero byte
713 return *hdr
? -1 : 0;
716 void * unpack_sha1_file(void *map
, unsigned long mapsize
, char *type
, unsigned long *size
)
722 ret
= unpack_sha1_header(&stream
, map
, mapsize
, hdr
, sizeof(hdr
));
723 if (ret
< Z_OK
|| parse_sha1_header(hdr
, type
, size
) < 0)
726 return unpack_sha1_rest(&stream
, hdr
, *size
);
729 /* forward declaration for a mutually recursive function */
730 static int packed_object_info(struct pack_entry
*entry
,
731 char *type
, unsigned long *sizep
);
733 static int packed_delta_info(unsigned char *base_sha1
,
734 unsigned long delta_size
,
737 unsigned long *sizep
,
738 struct packed_git
*p
)
740 struct pack_entry base_ent
;
743 die("truncated pack file");
745 /* The base entry _must_ be in the same pack */
746 if (!find_pack_entry_one(base_sha1
, &base_ent
, p
))
747 die("failed to find delta-pack base object %s",
748 sha1_to_hex(base_sha1
));
750 /* We choose to only get the type of the base object and
751 * ignore potentially corrupt pack file that expects the delta
752 * based on a base with a wrong size. This saves tons of
756 if (packed_object_info(&base_ent
, type
, NULL
))
757 die("cannot get info for delta-pack base");
760 const unsigned char *data
;
761 unsigned char delta_head
[64];
762 unsigned long result_size
;
766 memset(&stream
, 0, sizeof(stream
));
768 data
= stream
.next_in
= base_sha1
+ 20;
769 stream
.avail_in
= left
- 20;
770 stream
.next_out
= delta_head
;
771 stream
.avail_out
= sizeof(delta_head
);
773 inflateInit(&stream
);
774 st
= inflate(&stream
, Z_FINISH
);
776 if ((st
!= Z_STREAM_END
) &&
777 stream
.total_out
!= sizeof(delta_head
))
778 die("delta data unpack-initial failed");
780 /* Examine the initial part of the delta to figure out
784 get_delta_hdr_size(&data
); /* ignore base size */
786 /* Read the result size */
787 result_size
= get_delta_hdr_size(&data
);
788 *sizep
= result_size
;
793 static unsigned long unpack_object_header(struct packed_git
*p
, unsigned long offset
,
794 enum object_type
*type
, unsigned long *sizep
)
797 unsigned char *pack
, c
;
800 if (offset
>= p
->pack_size
)
801 die("object offset outside of pack file");
803 pack
= p
->pack_base
+ offset
;
806 *type
= (c
>> 4) & 7;
810 if (offset
>= p
->pack_size
)
811 die("object offset outside of pack file");
814 size
+= (c
& 0x7f) << shift
;
821 void packed_object_info_detail(struct pack_entry
*e
,
824 unsigned long *store_size
,
825 int *delta_chain_length
,
826 unsigned char *base_sha1
)
828 struct packed_git
*p
= e
->p
;
829 unsigned long offset
, left
;
831 enum object_type kind
;
833 offset
= unpack_object_header(p
, e
->offset
, &kind
, size
);
834 pack
= p
->pack_base
+ offset
;
835 left
= p
->pack_size
- offset
;
836 if (kind
!= OBJ_DELTA
)
837 *delta_chain_length
= 0;
839 int chain_length
= 0;
840 memcpy(base_sha1
, pack
, 20);
842 struct pack_entry base_ent
;
845 find_pack_entry_one(pack
, &base_ent
, p
);
846 offset
= unpack_object_header(p
, base_ent
.offset
,
848 pack
= p
->pack_base
+ offset
;
850 } while (kind
== OBJ_DELTA
);
851 *delta_chain_length
= chain_length
;
855 strcpy(type
, "commit");
858 strcpy(type
, "tree");
861 strcpy(type
, "blob");
867 die("corrupted pack file");
869 *store_size
= 0; /* notyet */
872 static int packed_object_info(struct pack_entry
*entry
,
873 char *type
, unsigned long *sizep
)
875 struct packed_git
*p
= entry
->p
;
876 unsigned long offset
, size
, left
;
878 enum object_type kind
;
881 if (use_packed_git(p
))
882 die("cannot map packed file");
884 offset
= unpack_object_header(p
, entry
->offset
, &kind
, &size
);
885 pack
= p
->pack_base
+ offset
;
886 left
= p
->pack_size
- offset
;
890 retval
= packed_delta_info(pack
, size
, left
, type
, sizep
, p
);
894 strcpy(type
, "commit");
897 strcpy(type
, "tree");
900 strcpy(type
, "blob");
906 die("corrupted pack file");
914 /* forward declaration for a mutually recursive function */
915 static void *unpack_entry(struct pack_entry
*, char *, unsigned long *);
917 static void *unpack_delta_entry(unsigned char *base_sha1
,
918 unsigned long delta_size
,
921 unsigned long *sizep
,
922 struct packed_git
*p
)
924 struct pack_entry base_ent
;
925 void *data
, *delta_data
, *result
, *base
;
926 unsigned long data_size
, result_size
, base_size
;
931 die("truncated pack file");
932 data
= base_sha1
+ 20;
933 data_size
= left
- 20;
934 delta_data
= xmalloc(delta_size
);
936 memset(&stream
, 0, sizeof(stream
));
938 stream
.next_in
= data
;
939 stream
.avail_in
= data_size
;
940 stream
.next_out
= delta_data
;
941 stream
.avail_out
= delta_size
;
943 inflateInit(&stream
);
944 st
= inflate(&stream
, Z_FINISH
);
946 if ((st
!= Z_STREAM_END
) || stream
.total_out
!= delta_size
)
947 die("delta data unpack failed");
949 /* The base entry _must_ be in the same pack */
950 if (!find_pack_entry_one(base_sha1
, &base_ent
, p
))
951 die("failed to find delta-pack base object %s",
952 sha1_to_hex(base_sha1
));
953 base
= unpack_entry_gently(&base_ent
, type
, &base_size
);
955 die("failed to read delta-pack base object %s",
956 sha1_to_hex(base_sha1
));
957 result
= patch_delta(base
, base_size
,
958 delta_data
, delta_size
,
961 die("failed to apply delta");
964 *sizep
= result_size
;
968 static void *unpack_non_delta_entry(unsigned char *data
,
974 unsigned char *buffer
;
976 buffer
= xmalloc(size
+ 1);
978 memset(&stream
, 0, sizeof(stream
));
979 stream
.next_in
= data
;
980 stream
.avail_in
= left
;
981 stream
.next_out
= buffer
;
982 stream
.avail_out
= size
;
984 inflateInit(&stream
);
985 st
= inflate(&stream
, Z_FINISH
);
987 if ((st
!= Z_STREAM_END
) || stream
.total_out
!= size
) {
995 static void *unpack_entry(struct pack_entry
*entry
,
996 char *type
, unsigned long *sizep
)
998 struct packed_git
*p
= entry
->p
;
1001 if (use_packed_git(p
))
1002 die("cannot map packed file");
1003 retval
= unpack_entry_gently(entry
, type
, sizep
);
1004 unuse_packed_git(p
);
1006 die("corrupted pack file");
1010 /* The caller is responsible for use_packed_git()/unuse_packed_git() pair */
1011 void *unpack_entry_gently(struct pack_entry
*entry
,
1012 char *type
, unsigned long *sizep
)
1014 struct packed_git
*p
= entry
->p
;
1015 unsigned long offset
, size
, left
;
1016 unsigned char *pack
;
1017 enum object_type kind
;
1020 offset
= unpack_object_header(p
, entry
->offset
, &kind
, &size
);
1021 pack
= p
->pack_base
+ offset
;
1022 left
= p
->pack_size
- offset
;
1025 retval
= unpack_delta_entry(pack
, size
, left
, type
, sizep
, p
);
1028 strcpy(type
, "commit");
1031 strcpy(type
, "tree");
1034 strcpy(type
, "blob");
1037 strcpy(type
, "tag");
1043 retval
= unpack_non_delta_entry(pack
, size
, left
);
1047 int num_packed_objects(const struct packed_git
*p
)
1049 /* See check_packed_git_idx() */
1050 return (p
->index_size
- 20 - 20 - 4*256) / 24;
1053 int nth_packed_object_sha1(const struct packed_git
*p
, int n
,
1054 unsigned char* sha1
)
1056 void *index
= p
->index_base
+ 256;
1057 if (n
< 0 || num_packed_objects(p
) <= n
)
1059 memcpy(sha1
, (index
+ 24 * n
+ 4), 20);
1063 int find_pack_entry_one(const unsigned char *sha1
,
1064 struct pack_entry
*e
, struct packed_git
*p
)
1066 unsigned int *level1_ofs
= p
->index_base
;
1067 int hi
= ntohl(level1_ofs
[*sha1
]);
1068 int lo
= ((*sha1
== 0x0) ? 0 : ntohl(level1_ofs
[*sha1
- 1]));
1069 void *index
= p
->index_base
+ 256;
1072 int mi
= (lo
+ hi
) / 2;
1073 int cmp
= memcmp(index
+ 24 * mi
+ 4, sha1
, 20);
1075 e
->offset
= ntohl(*((int*)(index
+ 24 * mi
)));
1076 memcpy(e
->sha1
, sha1
, 20);
1088 static int find_pack_entry(const unsigned char *sha1
, struct pack_entry
*e
)
1090 struct packed_git
*p
;
1091 prepare_packed_git();
1093 for (p
= packed_git
; p
; p
= p
->next
) {
1094 if (find_pack_entry_one(sha1
, e
, p
))
1100 struct packed_git
*find_sha1_pack(const unsigned char *sha1
,
1101 struct packed_git
*packs
)
1103 struct packed_git
*p
;
1104 struct pack_entry e
;
1106 for (p
= packs
; p
; p
= p
->next
) {
1107 if (find_pack_entry_one(sha1
, &e
, p
))
1114 int sha1_object_info(const unsigned char *sha1
, char *type
, unsigned long *sizep
)
1117 unsigned long mapsize
, size
;
1122 map
= map_sha1_file_internal(sha1
, &mapsize
);
1124 struct pack_entry e
;
1126 if (!find_pack_entry(sha1
, &e
))
1127 return error("unable to find %s", sha1_to_hex(sha1
));
1128 return packed_object_info(&e
, type
, sizep
);
1130 if (unpack_sha1_header(&stream
, map
, mapsize
, hdr
, sizeof(hdr
)) < 0)
1131 status
= error("unable to unpack %s header",
1133 if (parse_sha1_header(hdr
, type
, &size
) < 0)
1134 status
= error("unable to parse %s header", sha1_to_hex(sha1
));
1140 inflateEnd(&stream
);
1141 munmap(map
, mapsize
);
1145 static void *read_packed_sha1(const unsigned char *sha1
, char *type
, unsigned long *size
)
1147 struct pack_entry e
;
1149 if (!find_pack_entry(sha1
, &e
)) {
1150 error("cannot read sha1_file for %s", sha1_to_hex(sha1
));
1153 return unpack_entry(&e
, type
, size
);
1156 void * read_sha1_file(const unsigned char *sha1
, char *type
, unsigned long *size
)
1158 unsigned long mapsize
;
1160 struct pack_entry e
;
1162 if (find_pack_entry(sha1
, &e
))
1163 return read_packed_sha1(sha1
, type
, size
);
1164 map
= map_sha1_file_internal(sha1
, &mapsize
);
1166 buf
= unpack_sha1_file(map
, mapsize
, type
, size
);
1167 munmap(map
, mapsize
);
1173 void *read_object_with_reference(const unsigned char *sha1
,
1174 const char *required_type
,
1175 unsigned long *size
,
1176 unsigned char *actual_sha1_return
)
1180 unsigned long isize
;
1181 unsigned char actual_sha1
[20];
1183 memcpy(actual_sha1
, sha1
, 20);
1185 int ref_length
= -1;
1186 const char *ref_type
= NULL
;
1188 buffer
= read_sha1_file(actual_sha1
, type
, &isize
);
1191 if (!strcmp(type
, required_type
)) {
1193 if (actual_sha1_return
)
1194 memcpy(actual_sha1_return
, actual_sha1
, 20);
1197 /* Handle references */
1198 else if (!strcmp(type
, "commit"))
1200 else if (!strcmp(type
, "tag"))
1201 ref_type
= "object ";
1206 ref_length
= strlen(ref_type
);
1208 if (memcmp(buffer
, ref_type
, ref_length
) ||
1209 get_sha1_hex(buffer
+ ref_length
, actual_sha1
)) {
1214 /* Now we have the ID of the referred-to object in
1215 * actual_sha1. Check again. */
1219 char *write_sha1_file_prepare(void *buf
,
1222 unsigned char *sha1
,
1228 /* Generate the header */
1229 *hdrlen
= sprintf((char *)hdr
, "%s %lu", type
, len
)+1;
1233 SHA1_Update(&c
, hdr
, *hdrlen
);
1234 SHA1_Update(&c
, buf
, len
);
1235 SHA1_Final(sha1
, &c
);
1237 return sha1_file_name(sha1
);
1240 int write_sha1_file(void *buf
, unsigned long len
, const char *type
, unsigned char *returnsha1
)
1243 unsigned char *compressed
;
1245 unsigned char sha1
[20];
1247 static char tmpfile
[PATH_MAX
];
1248 unsigned char hdr
[50];
1249 int fd
, hdrlen
, ret
;
1251 /* Normally if we have it in the pack then we do not bother writing
1252 * it out into .git/objects/??/?{38} file.
1254 filename
= write_sha1_file_prepare(buf
, len
, type
, sha1
, hdr
, &hdrlen
);
1256 memcpy(returnsha1
, sha1
, 20);
1257 if (has_sha1_file(sha1
))
1259 fd
= open(filename
, O_RDONLY
);
1262 * FIXME!!! We might do collision checking here, but we'd
1263 * need to uncompress the old file and check it. Later.
1269 if (errno
!= ENOENT
) {
1270 fprintf(stderr
, "sha1 file %s: %s", filename
, strerror(errno
));
1274 snprintf(tmpfile
, sizeof(tmpfile
), "%s/obj_XXXXXX", get_object_directory());
1276 fd
= mkstemp(tmpfile
);
1278 fprintf(stderr
, "unable to create temporary sha1 filename %s: %s", tmpfile
, strerror(errno
));
1283 memset(&stream
, 0, sizeof(stream
));
1284 deflateInit(&stream
, Z_BEST_COMPRESSION
);
1285 size
= deflateBound(&stream
, len
+hdrlen
);
1286 compressed
= xmalloc(size
);
1289 stream
.next_out
= compressed
;
1290 stream
.avail_out
= size
;
1292 /* First header.. */
1293 stream
.next_in
= hdr
;
1294 stream
.avail_in
= hdrlen
;
1295 while (deflate(&stream
, 0) == Z_OK
)
1298 /* Then the data itself.. */
1299 stream
.next_in
= buf
;
1300 stream
.avail_in
= len
;
1301 while (deflate(&stream
, Z_FINISH
) == Z_OK
)
1303 deflateEnd(&stream
);
1304 size
= stream
.total_out
;
1306 if (write(fd
, compressed
, size
) != size
)
1307 die("unable to write file");
1312 ret
= link(tmpfile
, filename
);
1317 * Coda hack - coda doesn't like cross-directory links,
1318 * so we fall back to a rename, which will mean that it
1319 * won't be able to check collisions, but that's not a
1322 * When this succeeds, we just return 0. We have nothing
1325 if (ret
== EXDEV
&& !rename(tmpfile
, filename
))
1330 if (ret
!= EEXIST
) {
1331 fprintf(stderr
, "unable to write sha1 filename %s: %s", filename
, strerror(ret
));
1334 /* FIXME!!! Collision check here ? */
1340 int write_sha1_to_fd(int fd
, const unsigned char *sha1
)
1343 unsigned long objsize
;
1345 void *map
= map_sha1_file_internal(sha1
, &objsize
);
1347 void *temp_obj
= NULL
;
1351 unsigned char *unpacked
;
1356 // need to unpack and recompress it by itself
1357 unpacked
= read_packed_sha1(sha1
, type
, &len
);
1359 hdrlen
= sprintf(hdr
, "%s %lu", type
, len
) + 1;
1362 memset(&stream
, 0, sizeof(stream
));
1363 deflateInit(&stream
, Z_BEST_COMPRESSION
);
1364 size
= deflateBound(&stream
, len
+ hdrlen
);
1365 temp_obj
= buf
= xmalloc(size
);
1368 stream
.next_out
= buf
;
1369 stream
.avail_out
= size
;
1371 /* First header.. */
1372 stream
.next_in
= (void *)hdr
;
1373 stream
.avail_in
= hdrlen
;
1374 while (deflate(&stream
, 0) == Z_OK
)
1377 /* Then the data itself.. */
1378 stream
.next_in
= unpacked
;
1379 stream
.avail_in
= len
;
1380 while (deflate(&stream
, Z_FINISH
) == Z_OK
)
1382 deflateEnd(&stream
);
1385 objsize
= stream
.total_out
;
1389 size
= write(fd
, buf
+ posn
, objsize
- posn
);
1392 fprintf(stderr
, "write closed");
1399 } while (posn
< objsize
);
1402 munmap(map
, objsize
);
1409 int write_sha1_from_fd(const unsigned char *sha1
, int fd
, char *buffer
,
1410 size_t bufsize
, size_t *bufposn
)
1412 char *filename
= sha1_file_name(sha1
);
1416 unsigned char real_sha1
[20];
1417 unsigned char discard
[4096];
1421 local
= open(filename
, O_WRONLY
| O_CREAT
| O_EXCL
, 0666);
1424 return error("Couldn't open %s\n", filename
);
1426 memset(&stream
, 0, sizeof(stream
));
1428 inflateInit(&stream
);
1435 stream
.avail_in
= *bufposn
;
1436 stream
.next_in
= (unsigned char *) buffer
;
1438 stream
.next_out
= discard
;
1439 stream
.avail_out
= sizeof(discard
);
1440 ret
= inflate(&stream
, Z_SYNC_FLUSH
);
1441 SHA1_Update(&c
, discard
, sizeof(discard
) -
1443 } while (stream
.avail_in
&& ret
== Z_OK
);
1444 write(local
, buffer
, *bufposn
- stream
.avail_in
);
1445 memmove(buffer
, buffer
+ *bufposn
- stream
.avail_in
,
1447 *bufposn
= stream
.avail_in
;
1451 size
= read(fd
, buffer
+ *bufposn
, bufsize
- *bufposn
);
1456 return error("Connection closed?");
1457 perror("Reading from connection");
1462 inflateEnd(&stream
);
1465 SHA1_Final(real_sha1
, &c
);
1466 if (ret
!= Z_STREAM_END
) {
1468 return error("File %s corrupted", sha1_to_hex(sha1
));
1470 if (memcmp(sha1
, real_sha1
, 20)) {
1472 return error("File %s has bad hash\n", sha1_to_hex(sha1
));
1478 int has_pack_index(const unsigned char *sha1
)
1481 if (stat(sha1_pack_index_name(sha1
), &st
))
1486 int has_pack_file(const unsigned char *sha1
)
1489 if (stat(sha1_pack_name(sha1
), &st
))
1494 int has_sha1_pack(const unsigned char *sha1
)
1496 struct pack_entry e
;
1497 return find_pack_entry(sha1
, &e
);
1500 int has_sha1_file(const unsigned char *sha1
)
1503 struct pack_entry e
;
1505 if (find_pack_entry(sha1
, &e
))
1507 return find_sha1_file(sha1
, &st
) ? 1 : 0;
1510 int index_fd(unsigned char *sha1
, int fd
, struct stat
*st
, int write_object
, const char *type
)
1512 unsigned long size
= st
->st_size
;
1515 unsigned char hdr
[50];
1520 buf
= mmap(NULL
, size
, PROT_READ
, MAP_PRIVATE
, fd
, 0);
1522 if (buf
== MAP_FAILED
)
1528 ret
= write_sha1_file(buf
, size
, type
, sha1
);
1530 write_sha1_file_prepare(buf
, size
, type
, sha1
, hdr
, &hdrlen
);