2 * GIT - The information manager from hell
4 * Copyright (C) Linus Torvalds, 2005
6 * This handles basic git sha1 object files - packing, unpacking,
15 #if defined(__linux__) && (defined(__i386__) || defined(__PPC__))
16 #define O_NOATIME 01000000
22 static unsigned int sha1_file_open_flag
= O_NOATIME
;
24 static unsigned hexval(char c
)
26 if (c
>= '0' && c
<= '9')
28 if (c
>= 'a' && c
<= 'f')
30 if (c
>= 'A' && c
<= 'F')
35 int get_sha1_hex(const char *hex
, unsigned char *sha1
)
38 for (i
= 0; i
< 20; i
++) {
39 unsigned int val
= (hexval(hex
[0]) << 4) | hexval(hex
[1]);
48 static int get_sha1_file(const char *path
, unsigned char *result
)
51 int fd
= open(path
, O_RDONLY
);
56 len
= read(fd
, buffer
, sizeof(buffer
));
60 return get_sha1_hex(buffer
, result
);
63 static char *git_dir
, *git_object_dir
, *git_index_file
, *git_refs_dir
;
64 static void setup_git_env(void)
66 git_dir
= gitenv(GIT_DIR_ENVIRONMENT
);
68 git_dir
= DEFAULT_GIT_DIR_ENVIRONMENT
;
69 git_object_dir
= gitenv(DB_ENVIRONMENT
);
70 if (!git_object_dir
) {
71 git_object_dir
= xmalloc(strlen(git_dir
) + 9);
72 sprintf(git_object_dir
, "%s/objects", git_dir
);
74 git_refs_dir
= xmalloc(strlen(git_dir
) + 6);
75 sprintf(git_refs_dir
, "%s/refs", git_dir
);
76 git_index_file
= gitenv(INDEX_ENVIRONMENT
);
77 if (!git_index_file
) {
78 git_index_file
= xmalloc(strlen(git_dir
) + 7);
79 sprintf(git_index_file
, "%s/index", git_dir
);
83 char *get_object_directory(void)
87 return git_object_dir
;
90 char *get_refs_directory(void)
97 char *get_index_file(void)
101 return git_index_file
;
104 int get_sha1(const char *str
, unsigned char *sha1
)
106 static char pathname
[PATH_MAX
];
107 static const char *prefix
[] = {
117 if (!get_sha1_hex(str
, sha1
))
122 for (p
= prefix
; *p
; p
++) {
123 snprintf(pathname
, sizeof(pathname
), "%s/%s/%s",
125 if (!get_sha1_file(pathname
, sha1
))
132 char * sha1_to_hex(const unsigned char *sha1
)
134 static char buffer
[50];
135 static const char hex
[] = "0123456789abcdef";
139 for (i
= 0; i
< 20; i
++) {
140 unsigned int val
= *sha1
++;
141 *buf
++ = hex
[val
>> 4];
142 *buf
++ = hex
[val
& 0xf];
147 static void fill_sha1_path(char *pathbuf
, const unsigned char *sha1
)
150 for (i
= 0; i
< 20; i
++) {
151 static char hex
[] = "0123456789abcdef";
152 unsigned int val
= sha1
[i
];
153 char *pos
= pathbuf
+ i
*2 + (i
> 0);
154 *pos
++ = hex
[val
>> 4];
155 *pos
= hex
[val
& 0xf];
160 * NOTE! This returns a statically allocated buffer, so you have to be
161 * careful about using it. Do a "strdup()" if you need to save the
164 * Also note that this returns the location for creating. Reading
165 * SHA1 file can happen from any alternate directory listed in the
166 * DB_ENVIRONMENT environment variable if it is not found in
167 * the primary object database.
169 char *sha1_file_name(const unsigned char *sha1
)
171 static char *name
, *base
;
174 const char *sha1_file_directory
= get_object_directory();
175 int len
= strlen(sha1_file_directory
);
176 base
= xmalloc(len
+ 60);
177 memcpy(base
, sha1_file_directory
, len
);
178 memset(base
+len
, 0, 60);
181 name
= base
+ len
+ 1;
183 fill_sha1_path(name
, sha1
);
187 static struct alternate_object_database
{
193 * Prepare alternate object database registry.
194 * alt_odb points at an array of struct alternate_object_database.
195 * This array is terminated with an element that has both its base
196 * and name set to NULL. alt_odb[n] comes from n'th non-empty
197 * element from colon separated ALTERNATE_DB_ENVIRONMENT environment
198 * variable, and its base points at a statically allocated buffer
199 * that contains "/the/directory/corresponding/to/.git/objects/...",
200 * while its name points just after the slash at the end of
201 * ".git/objects/" in the example above, and has enough space to hold
202 * 40-byte hex SHA1, an extra slash for the first level indirection,
203 * and the terminating NUL.
204 * This function allocates the alt_odb array and all the strings
205 * pointed by base fields of the array elements with one xmalloc();
206 * the string pool immediately follows the array.
208 static void prepare_alt_odb(void)
211 const char *cp
, *last
;
213 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
);
263 for (i
= 0; (name
= alt_odb
[i
].name
) != NULL
; i
++) {
264 fill_sha1_path(name
, sha1
);
265 if (!stat(alt_odb
[i
].base
, st
))
266 return alt_odb
[i
].base
;
271 #define PACK_MAX_SZ (1<<26)
272 static int pack_used_ctr
;
273 static unsigned long pack_mapped
;
274 static struct packed_git
{
275 struct packed_git
*next
;
276 unsigned long index_size
;
277 unsigned long pack_size
;
278 unsigned int *index_base
;
280 unsigned int pack_last_used
;
281 char pack_name
[0]; /* something like ".git/objects/pack/xxxxx.pack" */
286 unsigned char sha1
[20];
287 struct packed_git
*p
;
290 static int check_packed_git_idx(const char *path
, unsigned long *idx_size_
,
295 unsigned long idx_size
;
297 int fd
= open(path
, O_RDONLY
);
301 if (fstat(fd
, &st
)) {
305 idx_size
= st
.st_size
;
306 idx_map
= mmap(NULL
, idx_size
, PROT_READ
, MAP_PRIVATE
, fd
, 0);
308 if (idx_map
== MAP_FAILED
)
313 /* check index map */
314 if (idx_size
< 4*256 + 20)
315 return error("index file too small");
317 for (i
= 0; i
< 256; i
++) {
318 unsigned int n
= ntohl(index
[i
]);
320 return error("non-monotonic index");
326 * - 256 index entries 4 bytes each
327 * - 24-byte entries * nr (20-byte sha1 + 4-byte offset)
328 * - 20-byte SHA1 of the packfile
329 * - 20-byte SHA1 file checksum
331 if (idx_size
!= 4*256 + nr
* 24 + 20 + 20)
332 return error("wrong index file size");
335 *idx_size_
= idx_size
;
339 static void unuse_one_packed_git(void)
344 static int use_packed_git(struct packed_git
*p
)
351 pack_mapped
+= p
->pack_size
;
352 while (PACK_MAX_SZ
< pack_mapped
)
353 unuse_one_packed_git();
354 fd
= open(p
->pack_name
, O_RDONLY
);
357 if (fstat(fd
, &st
)) {
361 if (st
.st_size
!= p
->pack_size
)
363 map
= mmap(NULL
, p
->pack_size
, PROT_READ
, MAP_PRIVATE
, fd
, 0);
365 if (map
== MAP_FAILED
)
369 p
->pack_last_used
= pack_used_ctr
++;
373 static struct packed_git
*add_packed_git(char *path
, int path_len
)
376 struct packed_git
*p
;
377 unsigned long idx_size
;
380 if (check_packed_git_idx(path
, &idx_size
, &idx_map
))
383 /* do we have a corresponding .pack file? */
384 strcpy(path
+ path_len
- 4, ".pack");
385 if (stat(path
, &st
) || !S_ISREG(st
.st_mode
)) {
386 munmap(idx_map
, idx_size
);
389 /* ok, it looks sane as far as we can check without
390 * actually mapping the pack file.
392 p
= xmalloc(sizeof(*p
) + path_len
+ 2);
393 strcpy(p
->pack_name
, path
);
394 p
->index_size
= idx_size
;
395 p
->pack_size
= st
.st_size
;
396 p
->index_base
= idx_map
;
398 p
->pack_last_used
= 0;
402 static void prepare_packed_git_one(char *objdir
)
409 sprintf(path
, "%s/pack", objdir
);
415 while ((de
= readdir(dir
)) != NULL
) {
416 int namelen
= strlen(de
->d_name
);
417 struct packed_git
*p
;
419 if (strcmp(de
->d_name
+ namelen
- 4, ".idx"))
422 /* we have .idx. Is it a file we can map? */
423 strcpy(path
+ len
, de
->d_name
);
424 p
= add_packed_git(path
, len
+ namelen
);
427 p
->next
= packed_git
;
432 static void prepare_packed_git(void)
435 static int run_once
= 0;
440 prepare_packed_git_one(get_object_directory());
443 for (i
= 0; alt_odb
[i
].base
!= NULL
; i
++) {
444 alt_odb
[i
].name
[0] = 0;
445 prepare_packed_git_one(alt_odb
[i
].base
);
449 int check_sha1_signature(const unsigned char *sha1
, void *map
, unsigned long size
, const char *type
)
452 unsigned char real_sha1
[20];
456 SHA1_Update(&c
, header
, 1+sprintf(header
, "%s %lu", type
, size
));
457 SHA1_Update(&c
, map
, size
);
458 SHA1_Final(real_sha1
, &c
);
459 return memcmp(sha1
, real_sha1
, 20) ? -1 : 0;
462 static void *map_sha1_file_internal(const unsigned char *sha1
,
469 char *filename
= find_sha1_file(sha1
, &st
);
473 error("cannot map sha1 file %s", sha1_to_hex(sha1
));
477 fd
= open(filename
, O_RDONLY
| sha1_file_open_flag
);
479 /* See if it works without O_NOATIME */
480 switch (sha1_file_open_flag
) {
482 fd
= open(filename
, O_RDONLY
);
492 /* If it failed once, it will probably fail again.
493 * Stop using O_NOATIME
495 sha1_file_open_flag
= 0;
497 map
= mmap(NULL
, st
.st_size
, PROT_READ
, MAP_PRIVATE
, fd
, 0);
499 if (-1 == (int)(long)map
)
505 void *map_sha1_file(const unsigned char *sha1
, unsigned long *size
)
507 return map_sha1_file_internal(sha1
, size
, 1);
510 int unpack_sha1_header(z_stream
*stream
, void *map
, unsigned long mapsize
, void *buffer
, unsigned long size
)
512 /* Get the data stream */
513 memset(stream
, 0, sizeof(*stream
));
514 stream
->next_in
= map
;
515 stream
->avail_in
= mapsize
;
516 stream
->next_out
= buffer
;
517 stream
->avail_out
= size
;
520 return inflate(stream
, 0);
523 void *unpack_sha1_rest(z_stream
*stream
, void *buffer
, unsigned long size
)
525 int bytes
= strlen(buffer
) + 1;
526 unsigned char *buf
= xmalloc(1+size
);
528 memcpy(buf
, buffer
+ bytes
, stream
->total_out
- bytes
);
529 bytes
= stream
->total_out
- bytes
;
531 stream
->next_out
= buf
+ bytes
;
532 stream
->avail_out
= size
- bytes
;
533 while (inflate(stream
, Z_FINISH
) == Z_OK
)
542 * We used to just use "sscanf()", but that's actually way
543 * too permissive for what we want to check. So do an anal
544 * object header parse by hand.
546 int parse_sha1_header(char *hdr
, char *type
, unsigned long *sizep
)
552 * The type can be at most ten bytes (including the
553 * terminating '\0' that we add), and is followed by
568 * The length must follow immediately, and be in canonical
569 * decimal format (ie "010" is not valid).
576 unsigned long c
= *hdr
- '0';
580 size
= size
* 10 + c
;
586 * The length must be followed by a zero byte
588 return *hdr
? -1 : 0;
591 void * unpack_sha1_file(void *map
, unsigned long mapsize
, char *type
, unsigned long *size
)
597 ret
= unpack_sha1_header(&stream
, map
, mapsize
, hdr
, sizeof(hdr
));
598 if (ret
< Z_OK
|| parse_sha1_header(hdr
, type
, size
) < 0)
601 return unpack_sha1_rest(&stream
, hdr
, *size
);
604 static int packed_delta_info(unsigned char *base_sha1
,
605 unsigned long delta_size
,
608 unsigned long *sizep
)
611 unsigned char delta_head
[64];
614 unsigned long data_size
, result_size
, base_size
, verify_base_size
;
619 die("truncated pack file");
620 if (sha1_object_info(base_sha1
, type
, &base_size
))
621 die("cannot get info for delta-pack base");
623 data
= base_sha1
+ 20;
624 data_size
= left
- 20;
626 memset(&stream
, 0, sizeof(stream
));
628 stream
.next_in
= data
;
629 stream
.avail_in
= data_size
;
630 stream
.next_out
= delta_head
;
631 stream
.avail_out
= sizeof(delta_head
);
633 inflateInit(&stream
);
634 st
= inflate(&stream
, Z_FINISH
);
636 if ((st
!= Z_STREAM_END
) && stream
.total_out
!= sizeof(delta_head
))
637 die("delta data unpack-initial failed");
639 /* Examine the initial part of the delta to figure out
640 * the result size. Verify the base size while we are at it.
643 verify_base_size
= i
= 0;
647 verify_base_size
|= *data
++ << i
;
652 /* Read the result size */
657 result_size
|= *data
++ << i
;
661 if (verify_base_size
!= base_size
)
662 die("delta base size mismatch");
664 *sizep
= result_size
;
668 static int packed_object_info(struct pack_entry
*entry
,
669 char *type
, unsigned long *sizep
)
671 struct packed_git
*p
= entry
->p
;
672 unsigned long offset
, size
, left
;
675 offset
= entry
->offset
;
676 if (p
->pack_size
- 5 < offset
)
677 die("object offset outside of pack file");
679 if (use_packed_git(p
))
680 die("cannot map packed file");
682 pack
= p
->pack_base
+ offset
;
683 size
= (pack
[1] << 24) + (pack
[2] << 16) + (pack
[3] << 8) + pack
[4];
684 left
= p
->pack_size
- offset
- 5;
687 return packed_delta_info(pack
+5, size
, left
, type
, sizep
);
690 strcpy(type
, "commit");
693 strcpy(type
, "tree");
696 strcpy(type
, "blob");
699 die("corrupted pack file");
705 /* forward declaration for a mutually recursive function */
706 static void *unpack_entry(struct pack_entry
*, char *, unsigned long *);
708 static void *unpack_delta_entry(unsigned char *base_sha1
,
709 unsigned long delta_size
,
712 unsigned long *sizep
)
714 void *data
, *delta_data
, *result
, *base
;
715 unsigned long data_size
, result_size
, base_size
;
720 die("truncated pack file");
721 data
= base_sha1
+ 20;
722 data_size
= left
- 20;
723 delta_data
= xmalloc(delta_size
);
725 memset(&stream
, 0, sizeof(stream
));
727 stream
.next_in
= data
;
728 stream
.avail_in
= data_size
;
729 stream
.next_out
= delta_data
;
730 stream
.avail_out
= delta_size
;
732 inflateInit(&stream
);
733 st
= inflate(&stream
, Z_FINISH
);
735 if ((st
!= Z_STREAM_END
) || stream
.total_out
!= delta_size
)
736 die("delta data unpack failed");
738 /* This may recursively unpack the base, which is what we want */
739 base
= read_sha1_file(base_sha1
, type
, &base_size
);
741 die("failed to read delta-pack base object %s",
742 sha1_to_hex(base_sha1
));
743 result
= patch_delta(base
, base_size
,
744 delta_data
, delta_size
,
747 die("failed to apply delta");
750 *sizep
= result_size
;
754 static void *unpack_non_delta_entry(unsigned char *data
,
762 buffer
= xmalloc(size
+ 1);
764 memset(&stream
, 0, sizeof(stream
));
765 stream
.next_in
= data
;
766 stream
.avail_in
= left
;
767 stream
.next_out
= buffer
;
768 stream
.avail_out
= size
;
770 inflateInit(&stream
);
771 st
= inflate(&stream
, Z_FINISH
);
773 if ((st
!= Z_STREAM_END
) || stream
.total_out
!= size
) {
781 static void *unpack_entry(struct pack_entry
*entry
,
782 char *type
, unsigned long *sizep
)
784 struct packed_git
*p
= entry
->p
;
785 unsigned long offset
, size
, left
;
788 offset
= entry
->offset
;
789 if (p
->pack_size
- 5 < offset
)
790 die("object offset outside of pack file");
792 if (use_packed_git(p
))
793 die("cannot map packed file");
795 pack
= p
->pack_base
+ offset
;
796 size
= (pack
[1] << 24) + (pack
[2] << 16) + (pack
[3] << 8) + pack
[4];
797 left
= p
->pack_size
- offset
- 5;
800 return unpack_delta_entry(pack
+5, size
, left
, type
, sizep
);
802 strcpy(type
, "commit");
805 strcpy(type
, "tree");
808 strcpy(type
, "blob");
811 die("corrupted pack file");
814 return unpack_non_delta_entry(pack
+5, size
, left
);
817 static int find_pack_entry_1(const unsigned char *sha1
,
818 struct pack_entry
*e
, struct packed_git
*p
)
820 int *level1_ofs
= p
->index_base
;
821 int hi
= ntohl(level1_ofs
[*sha1
]);
822 int lo
= ((*sha1
== 0x0) ? 0 : ntohl(level1_ofs
[*sha1
- 1]));
823 void *index
= p
->index_base
+ 256;
826 int mi
= (lo
+ hi
) / 2;
827 int cmp
= memcmp(index
+ 24 * mi
+ 4, sha1
, 20);
829 e
->offset
= ntohl(*((int*)(index
+ 24 * mi
)));
830 memcpy(e
->sha1
, sha1
, 20);
842 static int find_pack_entry(const unsigned char *sha1
, struct pack_entry
*e
)
844 struct packed_git
*p
;
845 prepare_packed_git();
847 for (p
= packed_git
; p
; p
= p
->next
) {
848 if (find_pack_entry_1(sha1
, e
, p
))
854 int sha1_object_info(const unsigned char *sha1
, char *type
, unsigned long *sizep
)
857 unsigned long mapsize
, size
;
862 map
= map_sha1_file_internal(sha1
, &mapsize
, 0);
866 if (!find_pack_entry(sha1
, &e
))
867 return error("unable to find %s", sha1_to_hex(sha1
));
868 if (!packed_object_info(&e
, type
, sizep
))
871 map
= unpack_entry(&e
, type
, sizep
);
873 return (map
== NULL
) ? 0 : -1;
875 if (unpack_sha1_header(&stream
, map
, mapsize
, hdr
, sizeof(hdr
)) < 0)
876 status
= error("unable to unpack %s header",
878 if (parse_sha1_header(hdr
, type
, &size
) < 0)
879 status
= error("unable to parse %s header", sha1_to_hex(sha1
));
885 munmap(map
, mapsize
);
889 static void *read_packed_sha1(const unsigned char *sha1
, char *type
, unsigned long *size
)
893 if (!find_pack_entry(sha1
, &e
)) {
894 error("cannot read sha1_file for %s", sha1_to_hex(sha1
));
897 return unpack_entry(&e
, type
, size
);
900 void * read_sha1_file(const unsigned char *sha1
, char *type
, unsigned long *size
)
902 unsigned long mapsize
;
905 map
= map_sha1_file_internal(sha1
, &mapsize
, 0);
907 buf
= unpack_sha1_file(map
, mapsize
, type
, size
);
908 munmap(map
, mapsize
);
911 return read_packed_sha1(sha1
, type
, size
);
914 void *read_object_with_reference(const unsigned char *sha1
,
915 const char *required_type
,
917 unsigned char *actual_sha1_return
)
922 unsigned char actual_sha1
[20];
924 memcpy(actual_sha1
, sha1
, 20);
927 const char *ref_type
= NULL
;
929 buffer
= read_sha1_file(actual_sha1
, type
, &isize
);
932 if (!strcmp(type
, required_type
)) {
934 if (actual_sha1_return
)
935 memcpy(actual_sha1_return
, actual_sha1
, 20);
938 /* Handle references */
939 else if (!strcmp(type
, "commit"))
941 else if (!strcmp(type
, "tag"))
942 ref_type
= "object ";
947 ref_length
= strlen(ref_type
);
949 if (memcmp(buffer
, ref_type
, ref_length
) ||
950 get_sha1_hex(buffer
+ ref_length
, actual_sha1
)) {
954 /* Now we have the ID of the referred-to object in
955 * actual_sha1. Check again. */
959 static char *write_sha1_file_prepare(void *buf
,
968 /* Generate the header */
969 *hdrlen
= sprintf((char *)hdr
, "%s %lu", type
, len
)+1;
973 SHA1_Update(&c
, hdr
, *hdrlen
);
974 SHA1_Update(&c
, buf
, len
);
975 SHA1_Final(sha1
, &c
);
977 return sha1_file_name(sha1
);
980 int write_sha1_file(void *buf
, unsigned long len
, const char *type
, unsigned char *returnsha1
)
983 unsigned char *compressed
;
985 unsigned char sha1
[20];
987 static char tmpfile
[PATH_MAX
];
988 unsigned char hdr
[50];
991 /* Normally if we have it in the pack then we do not bother writing
992 * it out into .git/objects/??/?{38} file.
994 filename
= write_sha1_file_prepare(buf
, len
, type
, sha1
, hdr
, &hdrlen
);
996 memcpy(returnsha1
, sha1
, 20);
997 if (has_sha1_file(sha1
))
999 fd
= open(filename
, O_RDONLY
);
1002 * FIXME!!! We might do collision checking here, but we'd
1003 * need to uncompress the old file and check it. Later.
1009 if (errno
!= ENOENT
) {
1010 fprintf(stderr
, "sha1 file %s: %s", filename
, strerror(errno
));
1014 snprintf(tmpfile
, sizeof(tmpfile
), "%s/obj_XXXXXX", get_object_directory());
1016 fd
= mkstemp(tmpfile
);
1018 fprintf(stderr
, "unable to create temporary sha1 filename %s: %s", tmpfile
, strerror(errno
));
1023 memset(&stream
, 0, sizeof(stream
));
1024 deflateInit(&stream
, Z_BEST_COMPRESSION
);
1025 size
= deflateBound(&stream
, len
+hdrlen
);
1026 compressed
= xmalloc(size
);
1029 stream
.next_out
= compressed
;
1030 stream
.avail_out
= size
;
1032 /* First header.. */
1033 stream
.next_in
= hdr
;
1034 stream
.avail_in
= hdrlen
;
1035 while (deflate(&stream
, 0) == Z_OK
)
1038 /* Then the data itself.. */
1039 stream
.next_in
= buf
;
1040 stream
.avail_in
= len
;
1041 while (deflate(&stream
, Z_FINISH
) == Z_OK
)
1043 deflateEnd(&stream
);
1044 size
= stream
.total_out
;
1046 if (write(fd
, compressed
, size
) != size
)
1047 die("unable to write file");
1052 ret
= link(tmpfile
, filename
);
1057 * Coda hack - coda doesn't like cross-directory links,
1058 * so we fall back to a rename, which will mean that it
1059 * won't be able to check collisions, but that's not a
1062 * When this succeeds, we just return 0. We have nothing
1065 if (ret
== EXDEV
&& !rename(tmpfile
, filename
))
1070 if (ret
!= EEXIST
) {
1071 fprintf(stderr
, "unable to write sha1 filename %s: %s", filename
, strerror(ret
));
1074 /* FIXME!!! Collision check here ? */
1080 int write_sha1_from_fd(const unsigned char *sha1
, int fd
)
1082 char *filename
= sha1_file_name(sha1
);
1086 unsigned char real_sha1
[20];
1087 unsigned char buf
[4096];
1088 unsigned char discard
[4096];
1092 local
= open(filename
, O_WRONLY
| O_CREAT
| O_EXCL
, 0666);
1095 return error("Couldn't open %s\n", filename
);
1097 memset(&stream
, 0, sizeof(stream
));
1099 inflateInit(&stream
);
1105 size
= read(fd
, buf
, 4096);
1110 return error("Connection closed?");
1111 perror("Reading from connection");
1114 write(local
, buf
, size
);
1115 stream
.avail_in
= size
;
1116 stream
.next_in
= buf
;
1118 stream
.next_out
= discard
;
1119 stream
.avail_out
= sizeof(discard
);
1120 ret
= inflate(&stream
, Z_SYNC_FLUSH
);
1121 SHA1_Update(&c
, discard
, sizeof(discard
) -
1123 } while (stream
.avail_in
&& ret
== Z_OK
);
1125 } while (ret
== Z_OK
);
1126 inflateEnd(&stream
);
1129 SHA1_Final(real_sha1
, &c
);
1130 if (ret
!= Z_STREAM_END
) {
1132 return error("File %s corrupted", sha1_to_hex(sha1
));
1134 if (memcmp(sha1
, real_sha1
, 20)) {
1136 return error("File %s has bad hash\n", sha1_to_hex(sha1
));
1142 int has_sha1_file(const unsigned char *sha1
)
1145 struct pack_entry e
;
1147 if (find_sha1_file(sha1
, &st
))
1149 return find_pack_entry(sha1
, &e
);
1152 int index_fd(unsigned char *sha1
, int fd
, struct stat
*st
)
1154 unsigned long size
= st
->st_size
;
1160 buf
= mmap(NULL
, size
, PROT_READ
, MAP_PRIVATE
, fd
, 0);
1162 if ((int)(long)buf
== -1)
1165 ret
= write_sha1_file(buf
, size
, "blob", sha1
);