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 static struct alternate_object_database
{
194 * Prepare alternate object database registry.
195 * alt_odb points at an array of struct alternate_object_database.
196 * This array is terminated with an element that has both its base
197 * and name set to NULL. alt_odb[n] comes from n'th non-empty
198 * element from colon separated ALTERNATE_DB_ENVIRONMENT environment
199 * variable, and its base points at a statically allocated buffer
200 * that contains "/the/directory/corresponding/to/.git/objects/...",
201 * while its name points just after the slash at the end of
202 * ".git/objects/" in the example above, and has enough space to hold
203 * 40-byte hex SHA1, an extra slash for the first level indirection,
204 * and the terminating NUL.
205 * This function allocates the alt_odb array and all the strings
206 * pointed by base fields of the array elements with one xmalloc();
207 * the string pool immediately follows the array.
209 static void prepare_alt_odb(void)
212 const char *cp
, *last
;
214 const char *alt
= gitenv(ALTERNATE_DB_ENVIRONMENT
) ? : "";
216 /* The first pass counts how large an area to allocate to
217 * hold the entire alt_odb structure, including array of
218 * structs and path buffers for them. The second pass fills
219 * the structure and prepares the path buffers for use by
222 for (totlen
= pass
= 0; pass
< 2; pass
++) {
226 cp
= strchr(last
, ':') ? : last
+ strlen(last
);
228 /* 43 = 40-byte + 2 '/' + terminating NUL */
229 int pfxlen
= cp
- last
;
230 int entlen
= pfxlen
+ 43;
234 alt_odb
[i
].base
= op
;
235 alt_odb
[i
].name
= op
+ pfxlen
+ 1;
236 memcpy(op
, last
, pfxlen
);
237 op
[pfxlen
] = op
[pfxlen
+ 3] = '/';
243 while (*cp
&& *cp
== ':')
249 alt_odb
= xmalloc(sizeof(*alt_odb
) * (i
+ 1) + totlen
);
250 alt_odb
[i
].base
= alt_odb
[i
].name
= NULL
;
251 op
= (char*)(&alt_odb
[i
+1]);
255 static char *find_sha1_file(const unsigned char *sha1
, struct stat
*st
)
258 char *name
= sha1_file_name(sha1
);
264 for (i
= 0; (name
= alt_odb
[i
].name
) != NULL
; i
++) {
265 fill_sha1_path(name
, sha1
);
266 if (!stat(alt_odb
[i
].base
, st
))
267 return alt_odb
[i
].base
;
272 #define PACK_MAX_SZ (1<<26)
273 static int pack_used_ctr
;
274 static unsigned long pack_mapped
;
275 static struct packed_git
{
276 struct packed_git
*next
;
277 unsigned long index_size
;
278 unsigned long pack_size
;
279 unsigned int *index_base
;
281 unsigned int pack_last_used
;
282 char pack_name
[0]; /* something like ".git/objects/pack/xxxxx.pack" */
287 unsigned char sha1
[20];
288 struct packed_git
*p
;
291 static int check_packed_git_idx(const char *path
, unsigned long *idx_size_
,
296 unsigned long idx_size
;
298 int fd
= open(path
, O_RDONLY
);
302 if (fstat(fd
, &st
)) {
306 idx_size
= st
.st_size
;
307 idx_map
= mmap(NULL
, idx_size
, PROT_READ
, MAP_PRIVATE
, fd
, 0);
309 if (idx_map
== MAP_FAILED
)
314 /* check index map */
315 if (idx_size
< 4*256 + 20)
316 return error("index file too small");
318 for (i
= 0; i
< 256; i
++) {
319 unsigned int n
= ntohl(index
[i
]);
321 return error("non-monotonic index");
327 * - 256 index entries 4 bytes each
328 * - 24-byte entries * nr (20-byte sha1 + 4-byte offset)
329 * - 20-byte SHA1 of the packfile
330 * - 20-byte SHA1 file checksum
332 if (idx_size
!= 4*256 + nr
* 24 + 20 + 20)
333 return error("wrong index file size");
336 *idx_size_
= idx_size
;
340 static void unuse_one_packed_git(void)
345 static int use_packed_git(struct packed_git
*p
)
352 pack_mapped
+= p
->pack_size
;
353 while (PACK_MAX_SZ
< pack_mapped
)
354 unuse_one_packed_git();
355 fd
= open(p
->pack_name
, O_RDONLY
);
358 if (fstat(fd
, &st
)) {
362 if (st
.st_size
!= p
->pack_size
)
364 map
= mmap(NULL
, p
->pack_size
, PROT_READ
, MAP_PRIVATE
, fd
, 0);
366 if (map
== MAP_FAILED
)
370 p
->pack_last_used
= pack_used_ctr
++;
374 static struct packed_git
*add_packed_git(char *path
, int path_len
)
377 struct packed_git
*p
;
378 unsigned long idx_size
;
381 if (check_packed_git_idx(path
, &idx_size
, &idx_map
))
384 /* do we have a corresponding .pack file? */
385 strcpy(path
+ path_len
- 4, ".pack");
386 if (stat(path
, &st
) || !S_ISREG(st
.st_mode
)) {
387 munmap(idx_map
, idx_size
);
390 /* ok, it looks sane as far as we can check without
391 * actually mapping the pack file.
393 p
= xmalloc(sizeof(*p
) + path_len
+ 2);
394 strcpy(p
->pack_name
, path
);
395 p
->index_size
= idx_size
;
396 p
->pack_size
= st
.st_size
;
397 p
->index_base
= idx_map
;
399 p
->pack_last_used
= 0;
403 static void prepare_packed_git_one(char *objdir
)
410 sprintf(path
, "%s/pack", objdir
);
416 while ((de
= readdir(dir
)) != NULL
) {
417 int namelen
= strlen(de
->d_name
);
418 struct packed_git
*p
;
420 if (strcmp(de
->d_name
+ namelen
- 4, ".idx"))
423 /* we have .idx. Is it a file we can map? */
424 strcpy(path
+ len
, de
->d_name
);
425 p
= add_packed_git(path
, len
+ namelen
);
428 p
->next
= packed_git
;
433 static void prepare_packed_git(void)
436 static int run_once
= 0;
441 prepare_packed_git_one(get_object_directory());
444 for (i
= 0; alt_odb
[i
].base
!= NULL
; i
++) {
445 alt_odb
[i
].name
[0] = 0;
446 prepare_packed_git_one(alt_odb
[i
].base
);
450 int check_sha1_signature(const unsigned char *sha1
, void *map
, unsigned long size
, const char *type
)
453 unsigned char real_sha1
[20];
457 SHA1_Update(&c
, header
, 1+sprintf(header
, "%s %lu", type
, size
));
458 SHA1_Update(&c
, map
, size
);
459 SHA1_Final(real_sha1
, &c
);
460 return memcmp(sha1
, real_sha1
, 20) ? -1 : 0;
463 static void *map_sha1_file_internal(const unsigned char *sha1
,
470 char *filename
= find_sha1_file(sha1
, &st
);
474 error("cannot map sha1 file %s", sha1_to_hex(sha1
));
478 fd
= open(filename
, O_RDONLY
| sha1_file_open_flag
);
480 /* See if it works without O_NOATIME */
481 switch (sha1_file_open_flag
) {
483 fd
= open(filename
, O_RDONLY
);
493 /* If it failed once, it will probably fail again.
494 * Stop using O_NOATIME
496 sha1_file_open_flag
= 0;
498 map
= mmap(NULL
, st
.st_size
, PROT_READ
, MAP_PRIVATE
, fd
, 0);
500 if (-1 == (int)(long)map
)
506 void *map_sha1_file(const unsigned char *sha1
, unsigned long *size
)
508 return map_sha1_file_internal(sha1
, size
, 1);
511 int unpack_sha1_header(z_stream
*stream
, void *map
, unsigned long mapsize
, void *buffer
, unsigned long size
)
513 /* Get the data stream */
514 memset(stream
, 0, sizeof(*stream
));
515 stream
->next_in
= map
;
516 stream
->avail_in
= mapsize
;
517 stream
->next_out
= buffer
;
518 stream
->avail_out
= size
;
521 return inflate(stream
, 0);
524 void *unpack_sha1_rest(z_stream
*stream
, void *buffer
, unsigned long size
)
526 int bytes
= strlen(buffer
) + 1;
527 unsigned char *buf
= xmalloc(1+size
);
529 memcpy(buf
, buffer
+ bytes
, stream
->total_out
- bytes
);
530 bytes
= stream
->total_out
- bytes
;
532 stream
->next_out
= buf
+ bytes
;
533 stream
->avail_out
= size
- bytes
;
534 while (inflate(stream
, Z_FINISH
) == Z_OK
)
543 * We used to just use "sscanf()", but that's actually way
544 * too permissive for what we want to check. So do an anal
545 * object header parse by hand.
547 int parse_sha1_header(char *hdr
, char *type
, unsigned long *sizep
)
553 * The type can be at most ten bytes (including the
554 * terminating '\0' that we add), and is followed by
569 * The length must follow immediately, and be in canonical
570 * decimal format (ie "010" is not valid).
577 unsigned long c
= *hdr
- '0';
581 size
= size
* 10 + c
;
587 * The length must be followed by a zero byte
589 return *hdr
? -1 : 0;
592 void * unpack_sha1_file(void *map
, unsigned long mapsize
, char *type
, unsigned long *size
)
598 ret
= unpack_sha1_header(&stream
, map
, mapsize
, hdr
, sizeof(hdr
));
599 if (ret
< Z_OK
|| parse_sha1_header(hdr
, type
, size
) < 0)
602 return unpack_sha1_rest(&stream
, hdr
, *size
);
605 static int packed_delta_info(unsigned char *base_sha1
,
606 unsigned long delta_size
,
609 unsigned long *sizep
)
612 unsigned char delta_head
[64];
615 unsigned long data_size
, result_size
, base_size
, verify_base_size
;
620 die("truncated pack file");
621 if (sha1_object_info(base_sha1
, type
, &base_size
))
622 die("cannot get info for delta-pack base");
624 data
= base_sha1
+ 20;
625 data_size
= left
- 20;
627 memset(&stream
, 0, sizeof(stream
));
629 stream
.next_in
= data
;
630 stream
.avail_in
= data_size
;
631 stream
.next_out
= delta_head
;
632 stream
.avail_out
= sizeof(delta_head
);
634 inflateInit(&stream
);
635 st
= inflate(&stream
, Z_FINISH
);
637 if ((st
!= Z_STREAM_END
) && stream
.total_out
!= sizeof(delta_head
))
638 die("delta data unpack-initial failed");
640 /* Examine the initial part of the delta to figure out
641 * the result size. Verify the base size while we are at it.
644 verify_base_size
= i
= 0;
648 verify_base_size
|= *data
++ << i
;
653 /* Read the result size */
658 result_size
|= *data
++ << i
;
662 if (verify_base_size
!= base_size
)
663 die("delta base size mismatch");
665 *sizep
= result_size
;
669 static unsigned long unpack_object_header(struct packed_git
*p
, unsigned long offset
,
670 enum object_type
*type
, unsigned long *sizep
)
672 unsigned char *pack
, c
;
675 if (offset
>= p
->pack_size
)
676 die("object offset outside of pack file");
678 pack
= p
->pack_base
+ offset
;
681 *type
= (c
>> 4) & 7;
684 if (offset
>= p
->pack_size
)
685 die("object offset outside of pack file");
688 size
= (size
<< 7) | (c
& 0x7f);
694 static int packed_object_info(struct pack_entry
*entry
,
695 char *type
, unsigned long *sizep
)
697 struct packed_git
*p
= entry
->p
;
698 unsigned long offset
, size
, left
;
700 enum object_type kind
;
702 if (use_packed_git(p
))
703 die("cannot map packed file");
705 offset
= unpack_object_header(p
, entry
->offset
, &kind
, &size
);
706 pack
= p
->pack_base
+ offset
;
707 left
= p
->pack_size
- offset
;
711 return packed_delta_info(pack
, size
, left
, type
, sizep
);
714 strcpy(type
, "commit");
717 strcpy(type
, "tree");
720 strcpy(type
, "blob");
726 die("corrupted pack file");
732 /* forward declaration for a mutually recursive function */
733 static void *unpack_entry(struct pack_entry
*, char *, unsigned long *);
735 static void *unpack_delta_entry(unsigned char *base_sha1
,
736 unsigned long delta_size
,
739 unsigned long *sizep
)
741 void *data
, *delta_data
, *result
, *base
;
742 unsigned long data_size
, result_size
, base_size
;
747 die("truncated pack file");
748 data
= base_sha1
+ 20;
749 data_size
= left
- 20;
750 delta_data
= xmalloc(delta_size
);
752 memset(&stream
, 0, sizeof(stream
));
754 stream
.next_in
= data
;
755 stream
.avail_in
= data_size
;
756 stream
.next_out
= delta_data
;
757 stream
.avail_out
= delta_size
;
759 inflateInit(&stream
);
760 st
= inflate(&stream
, Z_FINISH
);
762 if ((st
!= Z_STREAM_END
) || stream
.total_out
!= delta_size
)
763 die("delta data unpack failed");
765 /* This may recursively unpack the base, which is what we want */
766 base
= read_sha1_file(base_sha1
, type
, &base_size
);
768 die("failed to read delta-pack base object %s",
769 sha1_to_hex(base_sha1
));
770 result
= patch_delta(base
, base_size
,
771 delta_data
, delta_size
,
774 die("failed to apply delta");
777 *sizep
= result_size
;
781 static void *unpack_non_delta_entry(unsigned char *data
,
789 buffer
= xmalloc(size
+ 1);
791 memset(&stream
, 0, sizeof(stream
));
792 stream
.next_in
= data
;
793 stream
.avail_in
= left
;
794 stream
.next_out
= buffer
;
795 stream
.avail_out
= size
;
797 inflateInit(&stream
);
798 st
= inflate(&stream
, Z_FINISH
);
800 if ((st
!= Z_STREAM_END
) || stream
.total_out
!= size
) {
808 static void *unpack_entry(struct pack_entry
*entry
,
809 char *type
, unsigned long *sizep
)
811 struct packed_git
*p
= entry
->p
;
812 unsigned long offset
, size
, left
;
814 enum object_type kind
;
816 if (use_packed_git(p
))
817 die("cannot map packed file");
819 offset
= unpack_object_header(p
, entry
->offset
, &kind
, &size
);
820 pack
= p
->pack_base
+ offset
;
821 left
= p
->pack_size
- offset
;
824 return unpack_delta_entry(pack
, size
, left
, type
, sizep
);
826 strcpy(type
, "commit");
829 strcpy(type
, "tree");
832 strcpy(type
, "blob");
838 die("corrupted pack file");
841 return unpack_non_delta_entry(pack
, size
, left
);
844 static int find_pack_entry_1(const unsigned char *sha1
,
845 struct pack_entry
*e
, struct packed_git
*p
)
847 int *level1_ofs
= p
->index_base
;
848 int hi
= ntohl(level1_ofs
[*sha1
]);
849 int lo
= ((*sha1
== 0x0) ? 0 : ntohl(level1_ofs
[*sha1
- 1]));
850 void *index
= p
->index_base
+ 256;
853 int mi
= (lo
+ hi
) / 2;
854 int cmp
= memcmp(index
+ 24 * mi
+ 4, sha1
, 20);
856 e
->offset
= ntohl(*((int*)(index
+ 24 * mi
)));
857 memcpy(e
->sha1
, sha1
, 20);
869 static int find_pack_entry(const unsigned char *sha1
, struct pack_entry
*e
)
871 struct packed_git
*p
;
872 prepare_packed_git();
874 for (p
= packed_git
; p
; p
= p
->next
) {
875 if (find_pack_entry_1(sha1
, e
, p
))
881 int sha1_object_info(const unsigned char *sha1
, char *type
, unsigned long *sizep
)
884 unsigned long mapsize
, size
;
889 map
= map_sha1_file_internal(sha1
, &mapsize
, 0);
893 if (!find_pack_entry(sha1
, &e
))
894 return error("unable to find %s", sha1_to_hex(sha1
));
895 if (!packed_object_info(&e
, type
, sizep
))
898 map
= unpack_entry(&e
, type
, sizep
);
900 return (map
== NULL
) ? 0 : -1;
902 if (unpack_sha1_header(&stream
, map
, mapsize
, hdr
, sizeof(hdr
)) < 0)
903 status
= error("unable to unpack %s header",
905 if (parse_sha1_header(hdr
, type
, &size
) < 0)
906 status
= error("unable to parse %s header", sha1_to_hex(sha1
));
912 munmap(map
, mapsize
);
916 static void *read_packed_sha1(const unsigned char *sha1
, char *type
, unsigned long *size
)
920 if (!find_pack_entry(sha1
, &e
)) {
921 error("cannot read sha1_file for %s", sha1_to_hex(sha1
));
924 return unpack_entry(&e
, type
, size
);
927 void * read_sha1_file(const unsigned char *sha1
, char *type
, unsigned long *size
)
929 unsigned long mapsize
;
932 map
= map_sha1_file_internal(sha1
, &mapsize
, 0);
934 buf
= unpack_sha1_file(map
, mapsize
, type
, size
);
935 munmap(map
, mapsize
);
938 return read_packed_sha1(sha1
, type
, size
);
941 void *read_object_with_reference(const unsigned char *sha1
,
942 const char *required_type
,
944 unsigned char *actual_sha1_return
)
949 unsigned char actual_sha1
[20];
951 memcpy(actual_sha1
, sha1
, 20);
954 const char *ref_type
= NULL
;
956 buffer
= read_sha1_file(actual_sha1
, type
, &isize
);
959 if (!strcmp(type
, required_type
)) {
961 if (actual_sha1_return
)
962 memcpy(actual_sha1_return
, actual_sha1
, 20);
965 /* Handle references */
966 else if (!strcmp(type
, "commit"))
968 else if (!strcmp(type
, "tag"))
969 ref_type
= "object ";
974 ref_length
= strlen(ref_type
);
976 if (memcmp(buffer
, ref_type
, ref_length
) ||
977 get_sha1_hex(buffer
+ ref_length
, actual_sha1
)) {
981 /* Now we have the ID of the referred-to object in
982 * actual_sha1. Check again. */
986 static char *write_sha1_file_prepare(void *buf
,
995 /* Generate the header */
996 *hdrlen
= sprintf((char *)hdr
, "%s %lu", type
, len
)+1;
1000 SHA1_Update(&c
, hdr
, *hdrlen
);
1001 SHA1_Update(&c
, buf
, len
);
1002 SHA1_Final(sha1
, &c
);
1004 return sha1_file_name(sha1
);
1007 int write_sha1_file(void *buf
, unsigned long len
, const char *type
, unsigned char *returnsha1
)
1010 unsigned char *compressed
;
1012 unsigned char sha1
[20];
1014 static char tmpfile
[PATH_MAX
];
1015 unsigned char hdr
[50];
1016 int fd
, hdrlen
, ret
;
1018 /* Normally if we have it in the pack then we do not bother writing
1019 * it out into .git/objects/??/?{38} file.
1021 filename
= write_sha1_file_prepare(buf
, len
, type
, sha1
, hdr
, &hdrlen
);
1023 memcpy(returnsha1
, sha1
, 20);
1024 if (has_sha1_file(sha1
))
1026 fd
= open(filename
, O_RDONLY
);
1029 * FIXME!!! We might do collision checking here, but we'd
1030 * need to uncompress the old file and check it. Later.
1036 if (errno
!= ENOENT
) {
1037 fprintf(stderr
, "sha1 file %s: %s", filename
, strerror(errno
));
1041 snprintf(tmpfile
, sizeof(tmpfile
), "%s/obj_XXXXXX", get_object_directory());
1043 fd
= mkstemp(tmpfile
);
1045 fprintf(stderr
, "unable to create temporary sha1 filename %s: %s", tmpfile
, strerror(errno
));
1050 memset(&stream
, 0, sizeof(stream
));
1051 deflateInit(&stream
, Z_BEST_COMPRESSION
);
1052 size
= deflateBound(&stream
, len
+hdrlen
);
1053 compressed
= xmalloc(size
);
1056 stream
.next_out
= compressed
;
1057 stream
.avail_out
= size
;
1059 /* First header.. */
1060 stream
.next_in
= hdr
;
1061 stream
.avail_in
= hdrlen
;
1062 while (deflate(&stream
, 0) == Z_OK
)
1065 /* Then the data itself.. */
1066 stream
.next_in
= buf
;
1067 stream
.avail_in
= len
;
1068 while (deflate(&stream
, Z_FINISH
) == Z_OK
)
1070 deflateEnd(&stream
);
1071 size
= stream
.total_out
;
1073 if (write(fd
, compressed
, size
) != size
)
1074 die("unable to write file");
1079 ret
= link(tmpfile
, filename
);
1084 * Coda hack - coda doesn't like cross-directory links,
1085 * so we fall back to a rename, which will mean that it
1086 * won't be able to check collisions, but that's not a
1089 * When this succeeds, we just return 0. We have nothing
1092 if (ret
== EXDEV
&& !rename(tmpfile
, filename
))
1097 if (ret
!= EEXIST
) {
1098 fprintf(stderr
, "unable to write sha1 filename %s: %s", filename
, strerror(ret
));
1101 /* FIXME!!! Collision check here ? */
1107 int write_sha1_from_fd(const unsigned char *sha1
, int fd
)
1109 char *filename
= sha1_file_name(sha1
);
1113 unsigned char real_sha1
[20];
1114 unsigned char buf
[4096];
1115 unsigned char discard
[4096];
1119 local
= open(filename
, O_WRONLY
| O_CREAT
| O_EXCL
, 0666);
1122 return error("Couldn't open %s\n", filename
);
1124 memset(&stream
, 0, sizeof(stream
));
1126 inflateInit(&stream
);
1132 size
= read(fd
, buf
, 4096);
1137 return error("Connection closed?");
1138 perror("Reading from connection");
1141 write(local
, buf
, size
);
1142 stream
.avail_in
= size
;
1143 stream
.next_in
= buf
;
1145 stream
.next_out
= discard
;
1146 stream
.avail_out
= sizeof(discard
);
1147 ret
= inflate(&stream
, Z_SYNC_FLUSH
);
1148 SHA1_Update(&c
, discard
, sizeof(discard
) -
1150 } while (stream
.avail_in
&& ret
== Z_OK
);
1152 } while (ret
== Z_OK
);
1153 inflateEnd(&stream
);
1156 SHA1_Final(real_sha1
, &c
);
1157 if (ret
!= Z_STREAM_END
) {
1159 return error("File %s corrupted", sha1_to_hex(sha1
));
1161 if (memcmp(sha1
, real_sha1
, 20)) {
1163 return error("File %s has bad hash\n", sha1_to_hex(sha1
));
1169 int has_sha1_file(const unsigned char *sha1
)
1172 struct pack_entry e
;
1174 if (find_sha1_file(sha1
, &st
))
1176 return find_pack_entry(sha1
, &e
);
1179 int index_fd(unsigned char *sha1
, int fd
, struct stat
*st
)
1181 unsigned long size
= st
->st_size
;
1187 buf
= mmap(NULL
, size
, PROT_READ
, MAP_PRIVATE
, fd
, 0);
1189 if ((int)(long)buf
== -1)
1192 ret
= write_sha1_file(buf
, size
, "blob", sha1
);