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
= gitenv(GIT_DIR_ENVIRONMENT
);
55 git_dir
= DEFAULT_GIT_DIR_ENVIRONMENT
;
56 git_object_dir
= gitenv(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
= gitenv(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
= gitenv(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
)
245 const char *cp
, *last
;
246 struct alternate_object_database
*ent
;
251 if (cp
< ep
&& *cp
== '#') {
252 while (cp
< ep
&& *cp
!= sep
)
257 for ( ; cp
< ep
&& *cp
!= sep
; cp
++)
260 /* 43 = 40-byte + 2 '/' + terminating NUL */
261 int pfxlen
= cp
- last
;
262 int entlen
= pfxlen
+ 43;
264 ent
= xmalloc(sizeof(*ent
) + entlen
);
266 alt_odb_tail
= &(ent
->next
);
269 memcpy(ent
->base
, last
, pfxlen
);
270 ent
->name
= ent
->base
+ pfxlen
+ 1;
271 ent
->base
[pfxlen
] = ent
->base
[pfxlen
+ 3] = '/';
272 ent
->base
[entlen
-1] = 0;
274 while (cp
< ep
&& *cp
== sep
)
280 void prepare_alt_odb(void)
288 alt
= gitenv(ALTERNATE_DB_ENVIRONMENT
);
291 sprintf(path
, "%s/info/alternates", get_object_directory());
294 alt_odb_tail
= &alt_odb_list
;
295 link_alt_odb_entries(alt
, alt
+ strlen(alt
), ':');
297 fd
= open(path
, O_RDONLY
);
300 if (fstat(fd
, &st
) || (st
.st_size
== 0)) {
304 map
= mmap(NULL
, st
.st_size
, PROT_READ
, MAP_PRIVATE
, fd
, 0);
306 if (map
== MAP_FAILED
)
309 link_alt_odb_entries(map
, map
+ st
.st_size
, '\n');
310 munmap(map
, st
.st_size
);
313 static char *find_sha1_file(const unsigned char *sha1
, struct stat
*st
)
315 char *name
= sha1_file_name(sha1
);
316 struct alternate_object_database
*alt
;
321 for (alt
= alt_odb_list
; alt
; alt
= alt
->next
) {
323 fill_sha1_path(name
, sha1
);
324 if (!stat(alt
->base
, st
))
330 #define PACK_MAX_SZ (1<<26)
331 static int pack_used_ctr
;
332 static unsigned long pack_mapped
;
333 struct packed_git
*packed_git
;
335 static int check_packed_git_idx(const char *path
, unsigned long *idx_size_
,
340 unsigned long idx_size
;
342 int fd
= open(path
, O_RDONLY
);
346 if (fstat(fd
, &st
)) {
350 idx_size
= st
.st_size
;
351 idx_map
= mmap(NULL
, idx_size
, PROT_READ
, MAP_PRIVATE
, fd
, 0);
353 if (idx_map
== MAP_FAILED
)
358 *idx_size_
= idx_size
;
360 /* check index map */
361 if (idx_size
< 4*256 + 20 + 20)
362 return error("index file too small");
364 for (i
= 0; i
< 256; i
++) {
365 unsigned int n
= ntohl(index
[i
]);
367 return error("non-monotonic index");
373 * - 256 index entries 4 bytes each
374 * - 24-byte entries * nr (20-byte sha1 + 4-byte offset)
375 * - 20-byte SHA1 of the packfile
376 * - 20-byte SHA1 file checksum
378 if (idx_size
!= 4*256 + nr
* 24 + 20 + 20)
379 return error("wrong index file size");
384 static int unuse_one_packed_git(void)
386 struct packed_git
*p
, *lru
= NULL
;
388 for (p
= packed_git
; p
; p
= p
->next
) {
389 if (p
->pack_use_cnt
|| !p
->pack_base
)
391 if (!lru
|| p
->pack_last_used
< lru
->pack_last_used
)
396 munmap(lru
->pack_base
, lru
->pack_size
);
397 lru
->pack_base
= NULL
;
401 void unuse_packed_git(struct packed_git
*p
)
406 int use_packed_git(struct packed_git
*p
)
410 // We created the struct before we had the pack
411 stat(p
->pack_name
, &st
);
412 if (!S_ISREG(st
.st_mode
))
413 die("packfile %s not a regular file", p
->pack_name
);
414 p
->pack_size
= st
.st_size
;
421 pack_mapped
+= p
->pack_size
;
422 while (PACK_MAX_SZ
< pack_mapped
&& unuse_one_packed_git())
424 fd
= open(p
->pack_name
, O_RDONLY
);
426 die("packfile %s cannot be opened", p
->pack_name
);
427 if (fstat(fd
, &st
)) {
429 die("packfile %s cannot be opened", p
->pack_name
);
431 if (st
.st_size
!= p
->pack_size
)
432 die("packfile %s size mismatch.", p
->pack_name
);
433 map
= mmap(NULL
, p
->pack_size
, PROT_READ
, MAP_PRIVATE
, fd
, 0);
435 if (map
== MAP_FAILED
)
436 die("packfile %s cannot be mapped.", p
->pack_name
);
439 /* Check if the pack file matches with the index file.
442 if (memcmp((char*)(p
->index_base
) + p
->index_size
- 40,
443 p
->pack_base
+ p
->pack_size
- 20, 20)) {
445 die("packfile %s does not match index.", p
->pack_name
);
448 p
->pack_last_used
= pack_used_ctr
++;
453 struct packed_git
*add_packed_git(char *path
, int path_len
)
456 struct packed_git
*p
;
457 unsigned long idx_size
;
460 if (check_packed_git_idx(path
, &idx_size
, &idx_map
))
463 /* do we have a corresponding .pack file? */
464 strcpy(path
+ path_len
- 4, ".pack");
465 if (stat(path
, &st
) || !S_ISREG(st
.st_mode
)) {
466 munmap(idx_map
, idx_size
);
469 /* ok, it looks sane as far as we can check without
470 * actually mapping the pack file.
472 p
= xmalloc(sizeof(*p
) + path_len
+ 2);
473 strcpy(p
->pack_name
, path
);
474 p
->index_size
= idx_size
;
475 p
->pack_size
= st
.st_size
;
476 p
->index_base
= idx_map
;
479 p
->pack_last_used
= 0;
484 struct packed_git
*parse_pack_index(unsigned char *sha1
)
486 char *path
= sha1_pack_index_name(sha1
);
487 return parse_pack_index_file(sha1
, path
);
490 struct packed_git
*parse_pack_index_file(unsigned char *sha1
, char *idx_path
)
492 struct packed_git
*p
;
493 unsigned long idx_size
;
497 if (check_packed_git_idx(idx_path
, &idx_size
, &idx_map
))
500 path
= sha1_pack_name(sha1
);
502 p
= xmalloc(sizeof(*p
) + strlen(path
) + 2);
503 strcpy(p
->pack_name
, path
);
504 p
->index_size
= idx_size
;
506 p
->index_base
= idx_map
;
509 p
->pack_last_used
= 0;
511 memcpy(p
->sha1
, sha1
, 20);
515 void install_packed_git(struct packed_git
*pack
)
517 pack
->next
= packed_git
;
521 static void prepare_packed_git_one(char *objdir
)
528 sprintf(path
, "%s/pack", objdir
);
534 while ((de
= readdir(dir
)) != NULL
) {
535 int namelen
= strlen(de
->d_name
);
536 struct packed_git
*p
;
538 if (strcmp(de
->d_name
+ namelen
- 4, ".idx"))
541 /* we have .idx. Is it a file we can map? */
542 strcpy(path
+ len
, de
->d_name
);
543 p
= add_packed_git(path
, len
+ namelen
);
546 p
->next
= packed_git
;
552 void prepare_packed_git(void)
554 static int run_once
= 0;
555 struct alternate_object_database
*alt
;
559 prepare_packed_git_one(get_object_directory());
561 for (alt
= alt_odb_list
; alt
; alt
= alt
->next
) {
563 prepare_packed_git_one(alt
->base
);
568 int check_sha1_signature(const unsigned char *sha1
, void *map
, unsigned long size
, const char *type
)
571 unsigned char real_sha1
[20];
575 SHA1_Update(&c
, header
, 1+sprintf(header
, "%s %lu", type
, size
));
576 SHA1_Update(&c
, map
, size
);
577 SHA1_Final(real_sha1
, &c
);
578 return memcmp(sha1
, real_sha1
, 20) ? -1 : 0;
581 static void *map_sha1_file_internal(const unsigned char *sha1
,
587 char *filename
= find_sha1_file(sha1
, &st
);
593 fd
= open(filename
, O_RDONLY
| sha1_file_open_flag
);
595 /* See if it works without O_NOATIME */
596 switch (sha1_file_open_flag
) {
598 fd
= open(filename
, O_RDONLY
);
606 /* If it failed once, it will probably fail again.
607 * Stop using O_NOATIME
609 sha1_file_open_flag
= 0;
611 map
= mmap(NULL
, st
.st_size
, PROT_READ
, MAP_PRIVATE
, fd
, 0);
613 if (map
== MAP_FAILED
)
619 int unpack_sha1_header(z_stream
*stream
, void *map
, unsigned long mapsize
, void *buffer
, unsigned long size
)
621 /* Get the data stream */
622 memset(stream
, 0, sizeof(*stream
));
623 stream
->next_in
= map
;
624 stream
->avail_in
= mapsize
;
625 stream
->next_out
= buffer
;
626 stream
->avail_out
= size
;
629 return inflate(stream
, 0);
632 static void *unpack_sha1_rest(z_stream
*stream
, void *buffer
, unsigned long size
)
634 int bytes
= strlen(buffer
) + 1;
635 unsigned char *buf
= xmalloc(1+size
);
637 memcpy(buf
, buffer
+ bytes
, stream
->total_out
- bytes
);
638 bytes
= stream
->total_out
- bytes
;
640 stream
->next_out
= buf
+ bytes
;
641 stream
->avail_out
= size
- bytes
;
642 while (inflate(stream
, Z_FINISH
) == Z_OK
)
651 * We used to just use "sscanf()", but that's actually way
652 * too permissive for what we want to check. So do an anal
653 * object header parse by hand.
655 int parse_sha1_header(char *hdr
, char *type
, unsigned long *sizep
)
661 * The type can be at most ten bytes (including the
662 * terminating '\0' that we add), and is followed by
677 * The length must follow immediately, and be in canonical
678 * decimal format (ie "010" is not valid).
685 unsigned long c
= *hdr
- '0';
689 size
= size
* 10 + c
;
695 * The length must be followed by a zero byte
697 return *hdr
? -1 : 0;
700 void * unpack_sha1_file(void *map
, unsigned long mapsize
, char *type
, unsigned long *size
)
706 ret
= unpack_sha1_header(&stream
, map
, mapsize
, hdr
, sizeof(hdr
));
707 if (ret
< Z_OK
|| parse_sha1_header(hdr
, type
, size
) < 0)
710 return unpack_sha1_rest(&stream
, hdr
, *size
);
713 /* forward declaration for a mutually recursive function */
714 static int packed_object_info(struct pack_entry
*entry
,
715 char *type
, unsigned long *sizep
);
717 static int packed_delta_info(unsigned char *base_sha1
,
718 unsigned long delta_size
,
721 unsigned long *sizep
,
722 struct packed_git
*p
)
724 struct pack_entry base_ent
;
727 die("truncated pack file");
729 /* The base entry _must_ be in the same pack */
730 if (!find_pack_entry_one(base_sha1
, &base_ent
, p
))
731 die("failed to find delta-pack base object %s",
732 sha1_to_hex(base_sha1
));
734 /* We choose to only get the type of the base object and
735 * ignore potentially corrupt pack file that expects the delta
736 * based on a base with a wrong size. This saves tons of
740 if (packed_object_info(&base_ent
, type
, NULL
))
741 die("cannot get info for delta-pack base");
744 const unsigned char *data
;
745 unsigned char delta_head
[64];
746 unsigned long result_size
;
750 memset(&stream
, 0, sizeof(stream
));
752 data
= stream
.next_in
= base_sha1
+ 20;
753 stream
.avail_in
= left
- 20;
754 stream
.next_out
= delta_head
;
755 stream
.avail_out
= sizeof(delta_head
);
757 inflateInit(&stream
);
758 st
= inflate(&stream
, Z_FINISH
);
760 if ((st
!= Z_STREAM_END
) &&
761 stream
.total_out
!= sizeof(delta_head
))
762 die("delta data unpack-initial failed");
764 /* Examine the initial part of the delta to figure out
768 get_delta_hdr_size(&data
); /* ignore base size */
770 /* Read the result size */
771 result_size
= get_delta_hdr_size(&data
);
772 *sizep
= result_size
;
777 static unsigned long unpack_object_header(struct packed_git
*p
, unsigned long offset
,
778 enum object_type
*type
, unsigned long *sizep
)
781 unsigned char *pack
, c
;
784 if (offset
>= p
->pack_size
)
785 die("object offset outside of pack file");
787 pack
= p
->pack_base
+ offset
;
790 *type
= (c
>> 4) & 7;
794 if (offset
>= p
->pack_size
)
795 die("object offset outside of pack file");
798 size
+= (c
& 0x7f) << shift
;
805 void packed_object_info_detail(struct pack_entry
*e
,
808 unsigned long *store_size
,
809 int *delta_chain_length
,
810 unsigned char *base_sha1
)
812 struct packed_git
*p
= e
->p
;
813 unsigned long offset
, left
;
815 enum object_type kind
;
817 offset
= unpack_object_header(p
, e
->offset
, &kind
, size
);
818 pack
= p
->pack_base
+ offset
;
819 left
= p
->pack_size
- offset
;
820 if (kind
!= OBJ_DELTA
)
821 *delta_chain_length
= 0;
823 int chain_length
= 0;
824 memcpy(base_sha1
, pack
, 20);
826 struct pack_entry base_ent
;
829 find_pack_entry_one(pack
, &base_ent
, p
);
830 offset
= unpack_object_header(p
, base_ent
.offset
,
832 pack
= p
->pack_base
+ offset
;
834 } while (kind
== OBJ_DELTA
);
835 *delta_chain_length
= chain_length
;
839 strcpy(type
, "commit");
842 strcpy(type
, "tree");
845 strcpy(type
, "blob");
851 die("corrupted pack file");
853 *store_size
= 0; /* notyet */
856 static int packed_object_info(struct pack_entry
*entry
,
857 char *type
, unsigned long *sizep
)
859 struct packed_git
*p
= entry
->p
;
860 unsigned long offset
, size
, left
;
862 enum object_type kind
;
865 if (use_packed_git(p
))
866 die("cannot map packed file");
868 offset
= unpack_object_header(p
, entry
->offset
, &kind
, &size
);
869 pack
= p
->pack_base
+ offset
;
870 left
= p
->pack_size
- offset
;
874 retval
= packed_delta_info(pack
, size
, left
, type
, sizep
, p
);
878 strcpy(type
, "commit");
881 strcpy(type
, "tree");
884 strcpy(type
, "blob");
890 die("corrupted pack file");
898 /* forward declaration for a mutually recursive function */
899 static void *unpack_entry(struct pack_entry
*, char *, unsigned long *);
901 static void *unpack_delta_entry(unsigned char *base_sha1
,
902 unsigned long delta_size
,
905 unsigned long *sizep
,
906 struct packed_git
*p
)
908 struct pack_entry base_ent
;
909 void *data
, *delta_data
, *result
, *base
;
910 unsigned long data_size
, result_size
, base_size
;
915 die("truncated pack file");
916 data
= base_sha1
+ 20;
917 data_size
= left
- 20;
918 delta_data
= xmalloc(delta_size
);
920 memset(&stream
, 0, sizeof(stream
));
922 stream
.next_in
= data
;
923 stream
.avail_in
= data_size
;
924 stream
.next_out
= delta_data
;
925 stream
.avail_out
= delta_size
;
927 inflateInit(&stream
);
928 st
= inflate(&stream
, Z_FINISH
);
930 if ((st
!= Z_STREAM_END
) || stream
.total_out
!= delta_size
)
931 die("delta data unpack failed");
933 /* The base entry _must_ be in the same pack */
934 if (!find_pack_entry_one(base_sha1
, &base_ent
, p
))
935 die("failed to find delta-pack base object %s",
936 sha1_to_hex(base_sha1
));
937 base
= unpack_entry_gently(&base_ent
, type
, &base_size
);
939 die("failed to read delta-pack base object %s",
940 sha1_to_hex(base_sha1
));
941 result
= patch_delta(base
, base_size
,
942 delta_data
, delta_size
,
945 die("failed to apply delta");
948 *sizep
= result_size
;
952 static void *unpack_non_delta_entry(unsigned char *data
,
958 unsigned char *buffer
;
960 buffer
= xmalloc(size
+ 1);
962 memset(&stream
, 0, sizeof(stream
));
963 stream
.next_in
= data
;
964 stream
.avail_in
= left
;
965 stream
.next_out
= buffer
;
966 stream
.avail_out
= size
;
968 inflateInit(&stream
);
969 st
= inflate(&stream
, Z_FINISH
);
971 if ((st
!= Z_STREAM_END
) || stream
.total_out
!= size
) {
979 static void *unpack_entry(struct pack_entry
*entry
,
980 char *type
, unsigned long *sizep
)
982 struct packed_git
*p
= entry
->p
;
985 if (use_packed_git(p
))
986 die("cannot map packed file");
987 retval
= unpack_entry_gently(entry
, type
, sizep
);
990 die("corrupted pack file");
994 /* The caller is responsible for use_packed_git()/unuse_packed_git() pair */
995 void *unpack_entry_gently(struct pack_entry
*entry
,
996 char *type
, unsigned long *sizep
)
998 struct packed_git
*p
= entry
->p
;
999 unsigned long offset
, size
, left
;
1000 unsigned char *pack
;
1001 enum object_type kind
;
1004 offset
= unpack_object_header(p
, entry
->offset
, &kind
, &size
);
1005 pack
= p
->pack_base
+ offset
;
1006 left
= p
->pack_size
- offset
;
1009 retval
= unpack_delta_entry(pack
, size
, left
, type
, sizep
, p
);
1012 strcpy(type
, "commit");
1015 strcpy(type
, "tree");
1018 strcpy(type
, "blob");
1021 strcpy(type
, "tag");
1027 retval
= unpack_non_delta_entry(pack
, size
, left
);
1031 int num_packed_objects(const struct packed_git
*p
)
1033 /* See check_packed_git_idx() */
1034 return (p
->index_size
- 20 - 20 - 4*256) / 24;
1037 int nth_packed_object_sha1(const struct packed_git
*p
, int n
,
1038 unsigned char* sha1
)
1040 void *index
= p
->index_base
+ 256;
1041 if (n
< 0 || num_packed_objects(p
) <= n
)
1043 memcpy(sha1
, (index
+ 24 * n
+ 4), 20);
1047 int find_pack_entry_one(const unsigned char *sha1
,
1048 struct pack_entry
*e
, struct packed_git
*p
)
1050 unsigned int *level1_ofs
= p
->index_base
;
1051 int hi
= ntohl(level1_ofs
[*sha1
]);
1052 int lo
= ((*sha1
== 0x0) ? 0 : ntohl(level1_ofs
[*sha1
- 1]));
1053 void *index
= p
->index_base
+ 256;
1056 int mi
= (lo
+ hi
) / 2;
1057 int cmp
= memcmp(index
+ 24 * mi
+ 4, sha1
, 20);
1059 e
->offset
= ntohl(*((int*)(index
+ 24 * mi
)));
1060 memcpy(e
->sha1
, sha1
, 20);
1072 static int find_pack_entry(const unsigned char *sha1
, struct pack_entry
*e
)
1074 struct packed_git
*p
;
1075 prepare_packed_git();
1077 for (p
= packed_git
; p
; p
= p
->next
) {
1078 if (find_pack_entry_one(sha1
, e
, p
))
1084 struct packed_git
*find_sha1_pack(const unsigned char *sha1
,
1085 struct packed_git
*packs
)
1087 struct packed_git
*p
;
1088 struct pack_entry e
;
1090 for (p
= packs
; p
; p
= p
->next
) {
1091 if (find_pack_entry_one(sha1
, &e
, p
))
1098 int sha1_object_info(const unsigned char *sha1
, char *type
, unsigned long *sizep
)
1101 unsigned long mapsize
, size
;
1106 map
= map_sha1_file_internal(sha1
, &mapsize
);
1108 struct pack_entry e
;
1110 if (!find_pack_entry(sha1
, &e
))
1111 return error("unable to find %s", sha1_to_hex(sha1
));
1112 return packed_object_info(&e
, type
, sizep
);
1114 if (unpack_sha1_header(&stream
, map
, mapsize
, hdr
, sizeof(hdr
)) < 0)
1115 status
= error("unable to unpack %s header",
1117 if (parse_sha1_header(hdr
, type
, &size
) < 0)
1118 status
= error("unable to parse %s header", sha1_to_hex(sha1
));
1124 inflateEnd(&stream
);
1125 munmap(map
, mapsize
);
1129 static void *read_packed_sha1(const unsigned char *sha1
, char *type
, unsigned long *size
)
1131 struct pack_entry e
;
1133 if (!find_pack_entry(sha1
, &e
)) {
1134 error("cannot read sha1_file for %s", sha1_to_hex(sha1
));
1137 return unpack_entry(&e
, type
, size
);
1140 void * read_sha1_file(const unsigned char *sha1
, char *type
, unsigned long *size
)
1142 unsigned long mapsize
;
1144 struct pack_entry e
;
1146 if (find_pack_entry(sha1
, &e
))
1147 return read_packed_sha1(sha1
, type
, size
);
1148 map
= map_sha1_file_internal(sha1
, &mapsize
);
1150 buf
= unpack_sha1_file(map
, mapsize
, type
, size
);
1151 munmap(map
, mapsize
);
1157 void *read_object_with_reference(const unsigned char *sha1
,
1158 const char *required_type
,
1159 unsigned long *size
,
1160 unsigned char *actual_sha1_return
)
1164 unsigned long isize
;
1165 unsigned char actual_sha1
[20];
1167 memcpy(actual_sha1
, sha1
, 20);
1169 int ref_length
= -1;
1170 const char *ref_type
= NULL
;
1172 buffer
= read_sha1_file(actual_sha1
, type
, &isize
);
1175 if (!strcmp(type
, required_type
)) {
1177 if (actual_sha1_return
)
1178 memcpy(actual_sha1_return
, actual_sha1
, 20);
1181 /* Handle references */
1182 else if (!strcmp(type
, "commit"))
1184 else if (!strcmp(type
, "tag"))
1185 ref_type
= "object ";
1190 ref_length
= strlen(ref_type
);
1192 if (memcmp(buffer
, ref_type
, ref_length
) ||
1193 get_sha1_hex(buffer
+ ref_length
, actual_sha1
)) {
1198 /* Now we have the ID of the referred-to object in
1199 * actual_sha1. Check again. */
1203 char *write_sha1_file_prepare(void *buf
,
1206 unsigned char *sha1
,
1212 /* Generate the header */
1213 *hdrlen
= sprintf((char *)hdr
, "%s %lu", type
, len
)+1;
1217 SHA1_Update(&c
, hdr
, *hdrlen
);
1218 SHA1_Update(&c
, buf
, len
);
1219 SHA1_Final(sha1
, &c
);
1221 return sha1_file_name(sha1
);
1224 int write_sha1_file(void *buf
, unsigned long len
, const char *type
, unsigned char *returnsha1
)
1227 unsigned char *compressed
;
1229 unsigned char sha1
[20];
1231 static char tmpfile
[PATH_MAX
];
1232 unsigned char hdr
[50];
1233 int fd
, hdrlen
, ret
;
1235 /* Normally if we have it in the pack then we do not bother writing
1236 * it out into .git/objects/??/?{38} file.
1238 filename
= write_sha1_file_prepare(buf
, len
, type
, sha1
, hdr
, &hdrlen
);
1240 memcpy(returnsha1
, sha1
, 20);
1241 if (has_sha1_file(sha1
))
1243 fd
= open(filename
, O_RDONLY
);
1246 * FIXME!!! We might do collision checking here, but we'd
1247 * need to uncompress the old file and check it. Later.
1253 if (errno
!= ENOENT
) {
1254 fprintf(stderr
, "sha1 file %s: %s", filename
, strerror(errno
));
1258 snprintf(tmpfile
, sizeof(tmpfile
), "%s/obj_XXXXXX", get_object_directory());
1260 fd
= mkstemp(tmpfile
);
1262 fprintf(stderr
, "unable to create temporary sha1 filename %s: %s", tmpfile
, strerror(errno
));
1267 memset(&stream
, 0, sizeof(stream
));
1268 deflateInit(&stream
, Z_BEST_COMPRESSION
);
1269 size
= deflateBound(&stream
, len
+hdrlen
);
1270 compressed
= xmalloc(size
);
1273 stream
.next_out
= compressed
;
1274 stream
.avail_out
= size
;
1276 /* First header.. */
1277 stream
.next_in
= hdr
;
1278 stream
.avail_in
= hdrlen
;
1279 while (deflate(&stream
, 0) == Z_OK
)
1282 /* Then the data itself.. */
1283 stream
.next_in
= buf
;
1284 stream
.avail_in
= len
;
1285 while (deflate(&stream
, Z_FINISH
) == Z_OK
)
1287 deflateEnd(&stream
);
1288 size
= stream
.total_out
;
1290 if (write(fd
, compressed
, size
) != size
)
1291 die("unable to write file");
1296 ret
= link(tmpfile
, filename
);
1301 * Coda hack - coda doesn't like cross-directory links,
1302 * so we fall back to a rename, which will mean that it
1303 * won't be able to check collisions, but that's not a
1306 * When this succeeds, we just return 0. We have nothing
1309 if (ret
== EXDEV
&& !rename(tmpfile
, filename
))
1314 if (ret
!= EEXIST
) {
1315 fprintf(stderr
, "unable to write sha1 filename %s: %s", filename
, strerror(ret
));
1318 /* FIXME!!! Collision check here ? */
1324 int write_sha1_to_fd(int fd
, const unsigned char *sha1
)
1327 unsigned long objsize
;
1329 void *map
= map_sha1_file_internal(sha1
, &objsize
);
1331 void *temp_obj
= NULL
;
1335 unsigned char *unpacked
;
1340 // need to unpack and recompress it by itself
1341 unpacked
= read_packed_sha1(sha1
, type
, &len
);
1343 hdrlen
= sprintf(hdr
, "%s %lu", type
, len
) + 1;
1346 memset(&stream
, 0, sizeof(stream
));
1347 deflateInit(&stream
, Z_BEST_COMPRESSION
);
1348 size
= deflateBound(&stream
, len
+ hdrlen
);
1349 temp_obj
= buf
= xmalloc(size
);
1352 stream
.next_out
= buf
;
1353 stream
.avail_out
= size
;
1355 /* First header.. */
1356 stream
.next_in
= (void *)hdr
;
1357 stream
.avail_in
= hdrlen
;
1358 while (deflate(&stream
, 0) == Z_OK
)
1361 /* Then the data itself.. */
1362 stream
.next_in
= unpacked
;
1363 stream
.avail_in
= len
;
1364 while (deflate(&stream
, Z_FINISH
) == Z_OK
)
1366 deflateEnd(&stream
);
1369 objsize
= stream
.total_out
;
1373 size
= write(fd
, buf
+ posn
, objsize
- posn
);
1376 fprintf(stderr
, "write closed");
1383 } while (posn
< objsize
);
1386 munmap(map
, objsize
);
1393 int write_sha1_from_fd(const unsigned char *sha1
, int fd
, char *buffer
,
1394 size_t bufsize
, size_t *bufposn
)
1396 char *filename
= sha1_file_name(sha1
);
1400 unsigned char real_sha1
[20];
1401 unsigned char discard
[4096];
1405 local
= open(filename
, O_WRONLY
| O_CREAT
| O_EXCL
, 0666);
1408 return error("Couldn't open %s\n", filename
);
1410 memset(&stream
, 0, sizeof(stream
));
1412 inflateInit(&stream
);
1419 stream
.avail_in
= *bufposn
;
1420 stream
.next_in
= (unsigned char *) buffer
;
1422 stream
.next_out
= discard
;
1423 stream
.avail_out
= sizeof(discard
);
1424 ret
= inflate(&stream
, Z_SYNC_FLUSH
);
1425 SHA1_Update(&c
, discard
, sizeof(discard
) -
1427 } while (stream
.avail_in
&& ret
== Z_OK
);
1428 write(local
, buffer
, *bufposn
- stream
.avail_in
);
1429 memmove(buffer
, buffer
+ *bufposn
- stream
.avail_in
,
1431 *bufposn
= stream
.avail_in
;
1435 size
= read(fd
, buffer
+ *bufposn
, bufsize
- *bufposn
);
1440 return error("Connection closed?");
1441 perror("Reading from connection");
1446 inflateEnd(&stream
);
1449 SHA1_Final(real_sha1
, &c
);
1450 if (ret
!= Z_STREAM_END
) {
1452 return error("File %s corrupted", sha1_to_hex(sha1
));
1454 if (memcmp(sha1
, real_sha1
, 20)) {
1456 return error("File %s has bad hash\n", sha1_to_hex(sha1
));
1462 int has_pack_index(const unsigned char *sha1
)
1465 if (stat(sha1_pack_index_name(sha1
), &st
))
1470 int has_pack_file(const unsigned char *sha1
)
1473 if (stat(sha1_pack_name(sha1
), &st
))
1478 int has_sha1_pack(const unsigned char *sha1
)
1480 struct pack_entry e
;
1481 return find_pack_entry(sha1
, &e
);
1484 int has_sha1_file(const unsigned char *sha1
)
1487 struct pack_entry e
;
1489 if (find_pack_entry(sha1
, &e
))
1491 return find_sha1_file(sha1
, &st
) ? 1 : 0;
1494 int index_fd(unsigned char *sha1
, int fd
, struct stat
*st
, int write_object
, const char *type
)
1496 unsigned long size
= st
->st_size
;
1499 unsigned char hdr
[50];
1504 buf
= mmap(NULL
, size
, PROT_READ
, MAP_PRIVATE
, fd
, 0);
1506 if (buf
== MAP_FAILED
)
1512 ret
= write_sha1_file(buf
, size
, type
, sha1
);
1514 write_sha1_file_prepare(buf
, size
, type
, sha1
, hdr
, &hdrlen
);