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 int get_sha1_file(const char *path
, unsigned char *result
)
52 int fd
= open(path
, O_RDONLY
);
57 len
= read(fd
, buffer
, sizeof(buffer
));
61 return get_sha1_hex(buffer
, result
);
64 static char *git_dir
, *git_object_dir
, *git_index_file
, *git_refs_dir
;
65 static void setup_git_env(void)
67 git_dir
= gitenv(GIT_DIR_ENVIRONMENT
);
69 git_dir
= DEFAULT_GIT_DIR_ENVIRONMENT
;
70 git_object_dir
= gitenv(DB_ENVIRONMENT
);
71 if (!git_object_dir
) {
72 git_object_dir
= xmalloc(strlen(git_dir
) + 9);
73 sprintf(git_object_dir
, "%s/objects", git_dir
);
75 git_refs_dir
= xmalloc(strlen(git_dir
) + 6);
76 sprintf(git_refs_dir
, "%s/refs", git_dir
);
77 git_index_file
= gitenv(INDEX_ENVIRONMENT
);
78 if (!git_index_file
) {
79 git_index_file
= xmalloc(strlen(git_dir
) + 7);
80 sprintf(git_index_file
, "%s/index", git_dir
);
84 char *get_object_directory(void)
88 return git_object_dir
;
91 char *get_refs_directory(void)
98 char *get_index_file(void)
102 return git_index_file
;
105 int get_sha1(const char *str
, unsigned char *sha1
)
107 static char pathname
[PATH_MAX
];
108 static const char *prefix
[] = {
118 if (!get_sha1_hex(str
, sha1
))
123 for (p
= prefix
; *p
; p
++) {
124 snprintf(pathname
, sizeof(pathname
), "%s/%s/%s",
126 if (!get_sha1_file(pathname
, sha1
))
133 char * sha1_to_hex(const unsigned char *sha1
)
135 static char buffer
[50];
136 static const char hex
[] = "0123456789abcdef";
140 for (i
= 0; i
< 20; i
++) {
141 unsigned int val
= *sha1
++;
142 *buf
++ = hex
[val
>> 4];
143 *buf
++ = hex
[val
& 0xf];
148 static void fill_sha1_path(char *pathbuf
, const unsigned char *sha1
)
151 for (i
= 0; i
< 20; i
++) {
152 static char hex
[] = "0123456789abcdef";
153 unsigned int val
= sha1
[i
];
154 char *pos
= pathbuf
+ i
*2 + (i
> 0);
155 *pos
++ = hex
[val
>> 4];
156 *pos
= hex
[val
& 0xf];
161 * NOTE! This returns a statically allocated buffer, so you have to be
162 * careful about using it. Do a "strdup()" if you need to save the
165 * Also note that this returns the location for creating. Reading
166 * SHA1 file can happen from any alternate directory listed in the
167 * DB_ENVIRONMENT environment variable if it is not found in
168 * the primary object database.
170 char *sha1_file_name(const unsigned char *sha1
)
172 static char *name
, *base
;
175 const char *sha1_file_directory
= get_object_directory();
176 int len
= strlen(sha1_file_directory
);
177 base
= xmalloc(len
+ 60);
178 memcpy(base
, sha1_file_directory
, len
);
179 memset(base
+len
, 0, 60);
182 name
= base
+ len
+ 1;
184 fill_sha1_path(name
, sha1
);
188 struct alternate_object_database
*alt_odb
;
191 * Prepare alternate object database registry.
192 * alt_odb points at an array of struct alternate_object_database.
193 * This array is terminated with an element that has both its base
194 * and name set to NULL. alt_odb[n] comes from n'th non-empty
195 * element from colon separated ALTERNATE_DB_ENVIRONMENT environment
196 * variable, and its base points at a statically allocated buffer
197 * that contains "/the/directory/corresponding/to/.git/objects/...",
198 * while its name points just after the slash at the end of
199 * ".git/objects/" in the example above, and has enough space to hold
200 * 40-byte hex SHA1, an extra slash for the first level indirection,
201 * and the terminating NUL.
202 * This function allocates the alt_odb array and all the strings
203 * pointed by base fields of the array elements with one xmalloc();
204 * the string pool immediately follows the array.
206 void prepare_alt_odb(void)
209 const char *cp
, *last
;
211 const char *alt
= gitenv(ALTERNATE_DB_ENVIRONMENT
) ? : "";
215 /* The first pass counts how large an area to allocate to
216 * hold the entire alt_odb structure, including array of
217 * structs and path buffers for them. The second pass fills
218 * the structure and prepares the path buffers for use by
221 for (totlen
= pass
= 0; pass
< 2; pass
++) {
225 cp
= strchr(last
, ':') ? : last
+ strlen(last
);
227 /* 43 = 40-byte + 2 '/' + terminating NUL */
228 int pfxlen
= cp
- last
;
229 int entlen
= pfxlen
+ 43;
233 alt_odb
[i
].base
= op
;
234 alt_odb
[i
].name
= op
+ pfxlen
+ 1;
235 memcpy(op
, last
, pfxlen
);
236 op
[pfxlen
] = op
[pfxlen
+ 3] = '/';
242 while (*cp
&& *cp
== ':')
248 alt_odb
= xmalloc(sizeof(*alt_odb
) * (i
+ 1) + totlen
);
249 alt_odb
[i
].base
= alt_odb
[i
].name
= NULL
;
250 op
= (char*)(&alt_odb
[i
+1]);
254 static char *find_sha1_file(const unsigned char *sha1
, struct stat
*st
)
257 char *name
= sha1_file_name(sha1
);
262 for (i
= 0; (name
= alt_odb
[i
].name
) != NULL
; i
++) {
263 fill_sha1_path(name
, sha1
);
264 if (!stat(alt_odb
[i
].base
, st
))
265 return alt_odb
[i
].base
;
270 #define PACK_MAX_SZ (1<<26)
271 static int pack_used_ctr
;
272 static unsigned long pack_mapped
;
273 struct packed_git
*packed_git
;
275 static int check_packed_git_idx(const char *path
, unsigned long *idx_size_
,
280 unsigned long idx_size
;
282 int fd
= open(path
, O_RDONLY
);
286 if (fstat(fd
, &st
)) {
290 idx_size
= st
.st_size
;
291 idx_map
= mmap(NULL
, idx_size
, PROT_READ
, MAP_PRIVATE
, fd
, 0);
293 if (idx_map
== MAP_FAILED
)
298 *idx_size_
= idx_size
;
300 /* check index map */
301 if (idx_size
< 4*256 + 20 + 20)
302 return error("index file too small");
304 for (i
= 0; i
< 256; i
++) {
305 unsigned int n
= ntohl(index
[i
]);
307 return error("non-monotonic index");
313 * - 256 index entries 4 bytes each
314 * - 24-byte entries * nr (20-byte sha1 + 4-byte offset)
315 * - 20-byte SHA1 of the packfile
316 * - 20-byte SHA1 file checksum
318 if (idx_size
!= 4*256 + nr
* 24 + 20 + 20)
319 return error("wrong index file size");
324 static int unuse_one_packed_git(void)
326 struct packed_git
*p
, *lru
= NULL
;
328 for (p
= packed_git
; p
; p
= p
->next
) {
329 if (p
->pack_use_cnt
|| !p
->pack_base
)
331 if (!lru
|| p
->pack_last_used
< lru
->pack_last_used
)
336 munmap(lru
->pack_base
, lru
->pack_size
);
337 lru
->pack_base
= NULL
;
341 void unuse_packed_git(struct packed_git
*p
)
346 int use_packed_git(struct packed_git
*p
)
353 pack_mapped
+= p
->pack_size
;
354 while (PACK_MAX_SZ
< pack_mapped
&& unuse_one_packed_git())
356 fd
= open(p
->pack_name
, O_RDONLY
);
358 die("packfile %s cannot be opened", p
->pack_name
);
359 if (fstat(fd
, &st
)) {
361 die("packfile %s cannot be opened", p
->pack_name
);
363 if (st
.st_size
!= p
->pack_size
)
364 die("packfile %s size mismatch.", p
->pack_name
);
365 map
= mmap(NULL
, p
->pack_size
, PROT_READ
, MAP_PRIVATE
, fd
, 0);
367 if (map
== MAP_FAILED
)
368 die("packfile %s cannot be mapped.", p
->pack_name
);
371 /* Check if the pack file matches with the index file.
374 if (memcmp((char*)(p
->index_base
) + p
->index_size
- 40,
375 p
->pack_base
+ p
->pack_size
- 20, 20))
376 die("packfile %s does not match index.", p
->pack_name
);
378 p
->pack_last_used
= pack_used_ctr
++;
383 struct packed_git
*add_packed_git(char *path
, int path_len
)
386 struct packed_git
*p
;
387 unsigned long idx_size
;
390 if (check_packed_git_idx(path
, &idx_size
, &idx_map
))
393 /* do we have a corresponding .pack file? */
394 strcpy(path
+ path_len
- 4, ".pack");
395 if (stat(path
, &st
) || !S_ISREG(st
.st_mode
)) {
396 munmap(idx_map
, idx_size
);
399 /* ok, it looks sane as far as we can check without
400 * actually mapping the pack file.
402 p
= xmalloc(sizeof(*p
) + path_len
+ 2);
403 strcpy(p
->pack_name
, path
);
404 p
->index_size
= idx_size
;
405 p
->pack_size
= st
.st_size
;
406 p
->index_base
= idx_map
;
409 p
->pack_last_used
= 0;
414 static void prepare_packed_git_one(char *objdir
)
421 sprintf(path
, "%s/pack", objdir
);
427 while ((de
= readdir(dir
)) != NULL
) {
428 int namelen
= strlen(de
->d_name
);
429 struct packed_git
*p
;
431 if (strcmp(de
->d_name
+ namelen
- 4, ".idx"))
434 /* we have .idx. Is it a file we can map? */
435 strcpy(path
+ len
, de
->d_name
);
436 p
= add_packed_git(path
, len
+ namelen
);
439 p
->next
= packed_git
;
444 void prepare_packed_git(void)
447 static int run_once
= 0;
452 prepare_packed_git_one(get_object_directory());
454 for (i
= 0; alt_odb
[i
].base
!= NULL
; i
++) {
455 alt_odb
[i
].name
[0] = 0;
456 prepare_packed_git_one(alt_odb
[i
].base
);
460 int check_sha1_signature(const unsigned char *sha1
, void *map
, unsigned long size
, const char *type
)
463 unsigned char real_sha1
[20];
467 SHA1_Update(&c
, header
, 1+sprintf(header
, "%s %lu", type
, size
));
468 SHA1_Update(&c
, map
, size
);
469 SHA1_Final(real_sha1
, &c
);
470 return memcmp(sha1
, real_sha1
, 20) ? -1 : 0;
473 static void *map_sha1_file_internal(const unsigned char *sha1
,
480 char *filename
= find_sha1_file(sha1
, &st
);
484 error("cannot map sha1 file %s", sha1_to_hex(sha1
));
488 fd
= open(filename
, O_RDONLY
| sha1_file_open_flag
);
490 /* See if it works without O_NOATIME */
491 switch (sha1_file_open_flag
) {
493 fd
= open(filename
, O_RDONLY
);
503 /* If it failed once, it will probably fail again.
504 * Stop using O_NOATIME
506 sha1_file_open_flag
= 0;
508 map
= mmap(NULL
, st
.st_size
, PROT_READ
, MAP_PRIVATE
, fd
, 0);
510 if (-1 == (int)(long)map
)
516 void *map_sha1_file(const unsigned char *sha1
, unsigned long *size
)
518 return map_sha1_file_internal(sha1
, size
, 1);
521 int unpack_sha1_header(z_stream
*stream
, void *map
, unsigned long mapsize
, void *buffer
, unsigned long size
)
523 /* Get the data stream */
524 memset(stream
, 0, sizeof(*stream
));
525 stream
->next_in
= map
;
526 stream
->avail_in
= mapsize
;
527 stream
->next_out
= buffer
;
528 stream
->avail_out
= size
;
531 return inflate(stream
, 0);
534 void *unpack_sha1_rest(z_stream
*stream
, void *buffer
, unsigned long size
)
536 int bytes
= strlen(buffer
) + 1;
537 unsigned char *buf
= xmalloc(1+size
);
539 memcpy(buf
, buffer
+ bytes
, stream
->total_out
- bytes
);
540 bytes
= stream
->total_out
- bytes
;
542 stream
->next_out
= buf
+ bytes
;
543 stream
->avail_out
= size
- bytes
;
544 while (inflate(stream
, Z_FINISH
) == Z_OK
)
553 * We used to just use "sscanf()", but that's actually way
554 * too permissive for what we want to check. So do an anal
555 * object header parse by hand.
557 int parse_sha1_header(char *hdr
, char *type
, unsigned long *sizep
)
563 * The type can be at most ten bytes (including the
564 * terminating '\0' that we add), and is followed by
579 * The length must follow immediately, and be in canonical
580 * decimal format (ie "010" is not valid).
587 unsigned long c
= *hdr
- '0';
591 size
= size
* 10 + c
;
597 * The length must be followed by a zero byte
599 return *hdr
? -1 : 0;
602 void * unpack_sha1_file(void *map
, unsigned long mapsize
, char *type
, unsigned long *size
)
608 ret
= unpack_sha1_header(&stream
, map
, mapsize
, hdr
, sizeof(hdr
));
609 if (ret
< Z_OK
|| parse_sha1_header(hdr
, type
, size
) < 0)
612 return unpack_sha1_rest(&stream
, hdr
, *size
);
615 /* forward declaration for a mutually recursive function */
616 static int packed_object_info(struct pack_entry
*entry
,
617 char *type
, unsigned long *sizep
);
619 static int packed_delta_info(unsigned char *base_sha1
,
620 unsigned long delta_size
,
623 unsigned long *sizep
,
624 struct packed_git
*p
)
626 struct pack_entry base_ent
;
629 die("truncated pack file");
631 /* The base entry _must_ be in the same pack */
632 if (!find_pack_entry_one(base_sha1
, &base_ent
, p
))
633 die("failed to find delta-pack base object %s",
634 sha1_to_hex(base_sha1
));
636 /* We choose to only get the type of the base object and
637 * ignore potentially corrupt pack file that expects the delta
638 * based on a base with a wrong size. This saves tons of
642 if (packed_object_info(&base_ent
, type
, NULL
))
643 die("cannot get info for delta-pack base");
646 const unsigned char *data
;
647 unsigned char delta_head
[64];
648 unsigned long result_size
;
652 memset(&stream
, 0, sizeof(stream
));
654 data
= stream
.next_in
= base_sha1
+ 20;
655 stream
.avail_in
= left
- 20;
656 stream
.next_out
= delta_head
;
657 stream
.avail_out
= sizeof(delta_head
);
659 inflateInit(&stream
);
660 st
= inflate(&stream
, Z_FINISH
);
662 if ((st
!= Z_STREAM_END
) &&
663 stream
.total_out
!= sizeof(delta_head
))
664 die("delta data unpack-initial failed");
666 /* Examine the initial part of the delta to figure out
670 get_delta_hdr_size(&data
); /* ignore base size */
672 /* Read the result size */
673 result_size
= get_delta_hdr_size(&data
);
674 *sizep
= result_size
;
679 static unsigned long unpack_object_header(struct packed_git
*p
, unsigned long offset
,
680 enum object_type
*type
, unsigned long *sizep
)
683 unsigned char *pack
, c
;
686 if (offset
>= p
->pack_size
)
687 die("object offset outside of pack file");
689 pack
= p
->pack_base
+ offset
;
692 *type
= (c
>> 4) & 7;
696 if (offset
>= p
->pack_size
)
697 die("object offset outside of pack file");
700 size
+= (c
& 0x7f) << shift
;
707 void packed_object_info_detail(struct pack_entry
*e
,
710 unsigned long *store_size
,
711 int *delta_chain_length
,
712 unsigned char *base_sha1
)
714 struct packed_git
*p
= e
->p
;
715 unsigned long offset
, left
;
717 enum object_type kind
;
719 offset
= unpack_object_header(p
, e
->offset
, &kind
, size
);
720 pack
= p
->pack_base
+ offset
;
721 left
= p
->pack_size
- offset
;
722 if (kind
!= OBJ_DELTA
)
723 *delta_chain_length
= 0;
725 int chain_length
= 0;
726 memcpy(base_sha1
, pack
, 20);
728 struct pack_entry base_ent
;
731 find_pack_entry_one(pack
, &base_ent
, p
);
732 offset
= unpack_object_header(p
, base_ent
.offset
,
734 pack
= p
->pack_base
+ offset
;
736 } while (kind
== OBJ_DELTA
);
737 *delta_chain_length
= chain_length
;
741 strcpy(type
, "commit");
744 strcpy(type
, "tree");
747 strcpy(type
, "blob");
753 die("corrupted pack file");
755 *store_size
= 0; /* notyet */
758 static int packed_object_info(struct pack_entry
*entry
,
759 char *type
, unsigned long *sizep
)
761 struct packed_git
*p
= entry
->p
;
762 unsigned long offset
, size
, left
;
764 enum object_type kind
;
767 if (use_packed_git(p
))
768 die("cannot map packed file");
770 offset
= unpack_object_header(p
, entry
->offset
, &kind
, &size
);
771 pack
= p
->pack_base
+ offset
;
772 left
= p
->pack_size
- offset
;
776 retval
= packed_delta_info(pack
, size
, left
, type
, sizep
, p
);
780 strcpy(type
, "commit");
783 strcpy(type
, "tree");
786 strcpy(type
, "blob");
792 die("corrupted pack file");
800 /* forward declaration for a mutually recursive function */
801 static void *unpack_entry(struct pack_entry
*, char *, unsigned long *);
803 static void *unpack_delta_entry(unsigned char *base_sha1
,
804 unsigned long delta_size
,
807 unsigned long *sizep
,
808 struct packed_git
*p
)
810 struct pack_entry base_ent
;
811 void *data
, *delta_data
, *result
, *base
;
812 unsigned long data_size
, result_size
, base_size
;
817 die("truncated pack file");
818 data
= base_sha1
+ 20;
819 data_size
= left
- 20;
820 delta_data
= xmalloc(delta_size
);
822 memset(&stream
, 0, sizeof(stream
));
824 stream
.next_in
= data
;
825 stream
.avail_in
= data_size
;
826 stream
.next_out
= delta_data
;
827 stream
.avail_out
= delta_size
;
829 inflateInit(&stream
);
830 st
= inflate(&stream
, Z_FINISH
);
832 if ((st
!= Z_STREAM_END
) || stream
.total_out
!= delta_size
)
833 die("delta data unpack failed");
835 /* The base entry _must_ be in the same pack */
836 if (!find_pack_entry_one(base_sha1
, &base_ent
, p
))
837 die("failed to find delta-pack base object %s",
838 sha1_to_hex(base_sha1
));
839 base
= unpack_entry_gently(&base_ent
, type
, &base_size
);
841 die("failed to read delta-pack base object %s",
842 sha1_to_hex(base_sha1
));
843 result
= patch_delta(base
, base_size
,
844 delta_data
, delta_size
,
847 die("failed to apply delta");
850 *sizep
= result_size
;
854 static void *unpack_non_delta_entry(unsigned char *data
,
860 unsigned char *buffer
;
862 buffer
= xmalloc(size
+ 1);
864 memset(&stream
, 0, sizeof(stream
));
865 stream
.next_in
= data
;
866 stream
.avail_in
= left
;
867 stream
.next_out
= buffer
;
868 stream
.avail_out
= size
;
870 inflateInit(&stream
);
871 st
= inflate(&stream
, Z_FINISH
);
873 if ((st
!= Z_STREAM_END
) || stream
.total_out
!= size
) {
881 static void *unpack_entry(struct pack_entry
*entry
,
882 char *type
, unsigned long *sizep
)
884 struct packed_git
*p
= entry
->p
;
887 if (use_packed_git(p
))
888 die("cannot map packed file");
889 retval
= unpack_entry_gently(entry
, type
, sizep
);
892 die("corrupted pack file");
896 /* The caller is responsible for use_packed_git()/unuse_packed_git() pair */
897 void *unpack_entry_gently(struct pack_entry
*entry
,
898 char *type
, unsigned long *sizep
)
900 struct packed_git
*p
= entry
->p
;
901 unsigned long offset
, size
, left
;
903 enum object_type kind
;
906 offset
= unpack_object_header(p
, entry
->offset
, &kind
, &size
);
907 pack
= p
->pack_base
+ offset
;
908 left
= p
->pack_size
- offset
;
911 retval
= unpack_delta_entry(pack
, size
, left
, type
, sizep
, p
);
914 strcpy(type
, "commit");
917 strcpy(type
, "tree");
920 strcpy(type
, "blob");
929 retval
= unpack_non_delta_entry(pack
, size
, left
);
933 int num_packed_objects(const struct packed_git
*p
)
935 /* See check_packed_git_idx() */
936 return (p
->index_size
- 20 - 20 - 4*256) / 24;
939 int nth_packed_object_sha1(const struct packed_git
*p
, int n
,
942 void *index
= p
->index_base
+ 256;
943 if (n
< 0 || num_packed_objects(p
) <= n
)
945 memcpy(sha1
, (index
+ 24 * n
+ 4), 20);
949 int find_pack_entry_one(const unsigned char *sha1
,
950 struct pack_entry
*e
, struct packed_git
*p
)
952 unsigned int *level1_ofs
= p
->index_base
;
953 int hi
= ntohl(level1_ofs
[*sha1
]);
954 int lo
= ((*sha1
== 0x0) ? 0 : ntohl(level1_ofs
[*sha1
- 1]));
955 void *index
= p
->index_base
+ 256;
958 int mi
= (lo
+ hi
) / 2;
959 int cmp
= memcmp(index
+ 24 * mi
+ 4, sha1
, 20);
961 e
->offset
= ntohl(*((int*)(index
+ 24 * mi
)));
962 memcpy(e
->sha1
, sha1
, 20);
974 static int find_pack_entry(const unsigned char *sha1
, struct pack_entry
*e
)
976 struct packed_git
*p
;
977 prepare_packed_git();
979 for (p
= packed_git
; p
; p
= p
->next
) {
980 if (find_pack_entry_one(sha1
, e
, p
))
986 int sha1_object_info(const unsigned char *sha1
, char *type
, unsigned long *sizep
)
989 unsigned long mapsize
, size
;
994 map
= map_sha1_file_internal(sha1
, &mapsize
, 0);
998 if (!find_pack_entry(sha1
, &e
))
999 return error("unable to find %s", sha1_to_hex(sha1
));
1000 return packed_object_info(&e
, type
, sizep
);
1002 if (unpack_sha1_header(&stream
, map
, mapsize
, hdr
, sizeof(hdr
)) < 0)
1003 status
= error("unable to unpack %s header",
1005 if (parse_sha1_header(hdr
, type
, &size
) < 0)
1006 status
= error("unable to parse %s header", sha1_to_hex(sha1
));
1012 inflateEnd(&stream
);
1013 munmap(map
, mapsize
);
1017 static void *read_packed_sha1(const unsigned char *sha1
, char *type
, unsigned long *size
)
1019 struct pack_entry e
;
1021 if (!find_pack_entry(sha1
, &e
)) {
1022 error("cannot read sha1_file for %s", sha1_to_hex(sha1
));
1025 return unpack_entry(&e
, type
, size
);
1028 void * read_sha1_file(const unsigned char *sha1
, char *type
, unsigned long *size
)
1030 unsigned long mapsize
;
1033 map
= map_sha1_file_internal(sha1
, &mapsize
, 0);
1035 buf
= unpack_sha1_file(map
, mapsize
, type
, size
);
1036 munmap(map
, mapsize
);
1039 return read_packed_sha1(sha1
, type
, size
);
1042 void *read_object_with_reference(const unsigned char *sha1
,
1043 const char *required_type
,
1044 unsigned long *size
,
1045 unsigned char *actual_sha1_return
)
1049 unsigned long isize
;
1050 unsigned char actual_sha1
[20];
1052 memcpy(actual_sha1
, sha1
, 20);
1054 int ref_length
= -1;
1055 const char *ref_type
= NULL
;
1057 buffer
= read_sha1_file(actual_sha1
, type
, &isize
);
1060 if (!strcmp(type
, required_type
)) {
1062 if (actual_sha1_return
)
1063 memcpy(actual_sha1_return
, actual_sha1
, 20);
1066 /* Handle references */
1067 else if (!strcmp(type
, "commit"))
1069 else if (!strcmp(type
, "tag"))
1070 ref_type
= "object ";
1075 ref_length
= strlen(ref_type
);
1077 if (memcmp(buffer
, ref_type
, ref_length
) ||
1078 get_sha1_hex(buffer
+ ref_length
, actual_sha1
)) {
1082 /* Now we have the ID of the referred-to object in
1083 * actual_sha1. Check again. */
1087 static char *write_sha1_file_prepare(void *buf
,
1090 unsigned char *sha1
,
1096 /* Generate the header */
1097 *hdrlen
= sprintf((char *)hdr
, "%s %lu", type
, len
)+1;
1101 SHA1_Update(&c
, hdr
, *hdrlen
);
1102 SHA1_Update(&c
, buf
, len
);
1103 SHA1_Final(sha1
, &c
);
1105 return sha1_file_name(sha1
);
1108 int write_sha1_file(void *buf
, unsigned long len
, const char *type
, unsigned char *returnsha1
)
1111 unsigned char *compressed
;
1113 unsigned char sha1
[20];
1115 static char tmpfile
[PATH_MAX
];
1116 unsigned char hdr
[50];
1117 int fd
, hdrlen
, ret
;
1119 /* Normally if we have it in the pack then we do not bother writing
1120 * it out into .git/objects/??/?{38} file.
1122 filename
= write_sha1_file_prepare(buf
, len
, type
, sha1
, hdr
, &hdrlen
);
1124 memcpy(returnsha1
, sha1
, 20);
1125 if (has_sha1_file(sha1
))
1127 fd
= open(filename
, O_RDONLY
);
1130 * FIXME!!! We might do collision checking here, but we'd
1131 * need to uncompress the old file and check it. Later.
1137 if (errno
!= ENOENT
) {
1138 fprintf(stderr
, "sha1 file %s: %s", filename
, strerror(errno
));
1142 snprintf(tmpfile
, sizeof(tmpfile
), "%s/obj_XXXXXX", get_object_directory());
1144 fd
= mkstemp(tmpfile
);
1146 fprintf(stderr
, "unable to create temporary sha1 filename %s: %s", tmpfile
, strerror(errno
));
1151 memset(&stream
, 0, sizeof(stream
));
1152 deflateInit(&stream
, Z_BEST_COMPRESSION
);
1153 size
= deflateBound(&stream
, len
+hdrlen
);
1154 compressed
= xmalloc(size
);
1157 stream
.next_out
= compressed
;
1158 stream
.avail_out
= size
;
1160 /* First header.. */
1161 stream
.next_in
= hdr
;
1162 stream
.avail_in
= hdrlen
;
1163 while (deflate(&stream
, 0) == Z_OK
)
1166 /* Then the data itself.. */
1167 stream
.next_in
= buf
;
1168 stream
.avail_in
= len
;
1169 while (deflate(&stream
, Z_FINISH
) == Z_OK
)
1171 deflateEnd(&stream
);
1172 size
= stream
.total_out
;
1174 if (write(fd
, compressed
, size
) != size
)
1175 die("unable to write file");
1180 ret
= link(tmpfile
, filename
);
1185 * Coda hack - coda doesn't like cross-directory links,
1186 * so we fall back to a rename, which will mean that it
1187 * won't be able to check collisions, but that's not a
1190 * When this succeeds, we just return 0. We have nothing
1193 if (ret
== EXDEV
&& !rename(tmpfile
, filename
))
1198 if (ret
!= EEXIST
) {
1199 fprintf(stderr
, "unable to write sha1 filename %s: %s", filename
, strerror(ret
));
1202 /* FIXME!!! Collision check here ? */
1208 int write_sha1_from_fd(const unsigned char *sha1
, int fd
)
1210 char *filename
= sha1_file_name(sha1
);
1214 unsigned char real_sha1
[20];
1215 unsigned char buf
[4096];
1216 unsigned char discard
[4096];
1220 local
= open(filename
, O_WRONLY
| O_CREAT
| O_EXCL
, 0666);
1223 return error("Couldn't open %s\n", filename
);
1225 memset(&stream
, 0, sizeof(stream
));
1227 inflateInit(&stream
);
1233 size
= read(fd
, buf
, 4096);
1238 return error("Connection closed?");
1239 perror("Reading from connection");
1242 write(local
, buf
, size
);
1243 stream
.avail_in
= size
;
1244 stream
.next_in
= buf
;
1246 stream
.next_out
= discard
;
1247 stream
.avail_out
= sizeof(discard
);
1248 ret
= inflate(&stream
, Z_SYNC_FLUSH
);
1249 SHA1_Update(&c
, discard
, sizeof(discard
) -
1251 } while (stream
.avail_in
&& ret
== Z_OK
);
1253 } while (ret
== Z_OK
);
1254 inflateEnd(&stream
);
1257 SHA1_Final(real_sha1
, &c
);
1258 if (ret
!= Z_STREAM_END
) {
1260 return error("File %s corrupted", sha1_to_hex(sha1
));
1262 if (memcmp(sha1
, real_sha1
, 20)) {
1264 return error("File %s has bad hash\n", sha1_to_hex(sha1
));
1270 int has_sha1_file(const unsigned char *sha1
)
1273 struct pack_entry e
;
1275 if (find_sha1_file(sha1
, &st
))
1277 return find_pack_entry(sha1
, &e
);
1280 int index_fd(unsigned char *sha1
, int fd
, struct stat
*st
)
1282 unsigned long size
= st
->st_size
;
1288 buf
= mmap(NULL
, size
, PROT_READ
, MAP_PRIVATE
, fd
, 0);
1290 if ((int)(long)buf
== -1)
1293 ret
= write_sha1_file(buf
, size
, "blob", sha1
);