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
;
400 p
->pack_last_used
= 0;
404 static void prepare_packed_git_one(char *objdir
)
411 sprintf(path
, "%s/pack", objdir
);
417 while ((de
= readdir(dir
)) != NULL
) {
418 int namelen
= strlen(de
->d_name
);
419 struct packed_git
*p
;
421 if (strcmp(de
->d_name
+ namelen
- 4, ".idx"))
424 /* we have .idx. Is it a file we can map? */
425 strcpy(path
+ len
, de
->d_name
);
426 p
= add_packed_git(path
, len
+ namelen
);
429 p
->next
= packed_git
;
434 static void prepare_packed_git(void)
437 static int run_once
= 0;
442 prepare_packed_git_one(get_object_directory());
445 for (i
= 0; alt_odb
[i
].base
!= NULL
; i
++) {
446 alt_odb
[i
].name
[0] = 0;
447 prepare_packed_git_one(alt_odb
[i
].base
);
451 int check_sha1_signature(const unsigned char *sha1
, void *map
, unsigned long size
, const char *type
)
454 unsigned char real_sha1
[20];
458 SHA1_Update(&c
, header
, 1+sprintf(header
, "%s %lu", type
, size
));
459 SHA1_Update(&c
, map
, size
);
460 SHA1_Final(real_sha1
, &c
);
461 return memcmp(sha1
, real_sha1
, 20) ? -1 : 0;
464 static void *map_sha1_file_internal(const unsigned char *sha1
,
471 char *filename
= find_sha1_file(sha1
, &st
);
475 error("cannot map sha1 file %s", sha1_to_hex(sha1
));
479 fd
= open(filename
, O_RDONLY
| sha1_file_open_flag
);
481 /* See if it works without O_NOATIME */
482 switch (sha1_file_open_flag
) {
484 fd
= open(filename
, O_RDONLY
);
494 /* If it failed once, it will probably fail again.
495 * Stop using O_NOATIME
497 sha1_file_open_flag
= 0;
499 map
= mmap(NULL
, st
.st_size
, PROT_READ
, MAP_PRIVATE
, fd
, 0);
501 if (-1 == (int)(long)map
)
507 void *map_sha1_file(const unsigned char *sha1
, unsigned long *size
)
509 return map_sha1_file_internal(sha1
, size
, 1);
512 int unpack_sha1_header(z_stream
*stream
, void *map
, unsigned long mapsize
, void *buffer
, unsigned long size
)
514 /* Get the data stream */
515 memset(stream
, 0, sizeof(*stream
));
516 stream
->next_in
= map
;
517 stream
->avail_in
= mapsize
;
518 stream
->next_out
= buffer
;
519 stream
->avail_out
= size
;
522 return inflate(stream
, 0);
525 void *unpack_sha1_rest(z_stream
*stream
, void *buffer
, unsigned long size
)
527 int bytes
= strlen(buffer
) + 1;
528 unsigned char *buf
= xmalloc(1+size
);
530 memcpy(buf
, buffer
+ bytes
, stream
->total_out
- bytes
);
531 bytes
= stream
->total_out
- bytes
;
533 stream
->next_out
= buf
+ bytes
;
534 stream
->avail_out
= size
- bytes
;
535 while (inflate(stream
, Z_FINISH
) == Z_OK
)
544 * We used to just use "sscanf()", but that's actually way
545 * too permissive for what we want to check. So do an anal
546 * object header parse by hand.
548 int parse_sha1_header(char *hdr
, char *type
, unsigned long *sizep
)
554 * The type can be at most ten bytes (including the
555 * terminating '\0' that we add), and is followed by
570 * The length must follow immediately, and be in canonical
571 * decimal format (ie "010" is not valid).
578 unsigned long c
= *hdr
- '0';
582 size
= size
* 10 + c
;
588 * The length must be followed by a zero byte
590 return *hdr
? -1 : 0;
593 void * unpack_sha1_file(void *map
, unsigned long mapsize
, char *type
, unsigned long *size
)
599 ret
= unpack_sha1_header(&stream
, map
, mapsize
, hdr
, sizeof(hdr
));
600 if (ret
< Z_OK
|| parse_sha1_header(hdr
, type
, size
) < 0)
603 return unpack_sha1_rest(&stream
, hdr
, *size
);
606 static int packed_delta_info(unsigned char *base_sha1
,
607 unsigned long delta_size
,
610 unsigned long *sizep
)
613 unsigned char delta_head
[64];
616 unsigned long data_size
, result_size
, base_size
, verify_base_size
;
621 die("truncated pack file");
622 if (sha1_object_info(base_sha1
, type
, &base_size
))
623 die("cannot get info for delta-pack base");
625 data
= base_sha1
+ 20;
626 data_size
= left
- 20;
628 memset(&stream
, 0, sizeof(stream
));
630 stream
.next_in
= data
;
631 stream
.avail_in
= data_size
;
632 stream
.next_out
= delta_head
;
633 stream
.avail_out
= sizeof(delta_head
);
635 inflateInit(&stream
);
636 st
= inflate(&stream
, Z_FINISH
);
638 if ((st
!= Z_STREAM_END
) && stream
.total_out
!= sizeof(delta_head
))
639 die("delta data unpack-initial failed");
641 /* Examine the initial part of the delta to figure out
642 * the result size. Verify the base size while we are at it.
645 verify_base_size
= i
= 0;
649 verify_base_size
|= *data
++ << i
;
654 /* Read the result size */
659 result_size
|= *data
++ << i
;
663 if (verify_base_size
!= base_size
)
664 die("delta base size mismatch");
666 *sizep
= result_size
;
670 static unsigned long unpack_object_header(struct packed_git
*p
, unsigned long offset
,
671 enum object_type
*type
, unsigned long *sizep
)
673 unsigned char *pack
, c
;
676 if (offset
>= p
->pack_size
)
677 die("object offset outside of pack file");
679 pack
= p
->pack_base
+ offset
;
682 *type
= (c
>> 4) & 7;
685 if (offset
>= p
->pack_size
)
686 die("object offset outside of pack file");
689 size
= (size
<< 7) | (c
& 0x7f);
695 static int packed_object_info(struct pack_entry
*entry
,
696 char *type
, unsigned long *sizep
)
698 struct packed_git
*p
= entry
->p
;
699 unsigned long offset
, size
, left
;
701 enum object_type kind
;
703 if (use_packed_git(p
))
704 die("cannot map packed file");
706 offset
= unpack_object_header(p
, entry
->offset
, &kind
, &size
);
707 pack
= p
->pack_base
+ offset
;
708 left
= p
->pack_size
- offset
;
712 return packed_delta_info(pack
, size
, left
, type
, sizep
);
715 strcpy(type
, "commit");
718 strcpy(type
, "tree");
721 strcpy(type
, "blob");
727 die("corrupted pack file");
733 /* forward declaration for a mutually recursive function */
734 static void *unpack_entry(struct pack_entry
*, char *, unsigned long *);
736 static void *unpack_delta_entry(unsigned char *base_sha1
,
737 unsigned long delta_size
,
740 unsigned long *sizep
)
742 void *data
, *delta_data
, *result
, *base
;
743 unsigned long data_size
, result_size
, base_size
;
748 die("truncated pack file");
749 data
= base_sha1
+ 20;
750 data_size
= left
- 20;
751 delta_data
= xmalloc(delta_size
);
753 memset(&stream
, 0, sizeof(stream
));
755 stream
.next_in
= data
;
756 stream
.avail_in
= data_size
;
757 stream
.next_out
= delta_data
;
758 stream
.avail_out
= delta_size
;
760 inflateInit(&stream
);
761 st
= inflate(&stream
, Z_FINISH
);
763 if ((st
!= Z_STREAM_END
) || stream
.total_out
!= delta_size
)
764 die("delta data unpack failed");
766 /* This may recursively unpack the base, which is what we want */
767 base
= read_sha1_file(base_sha1
, type
, &base_size
);
769 die("failed to read delta-pack base object %s",
770 sha1_to_hex(base_sha1
));
771 result
= patch_delta(base
, base_size
,
772 delta_data
, delta_size
,
775 die("failed to apply delta");
778 *sizep
= result_size
;
782 static void *unpack_non_delta_entry(unsigned char *data
,
790 buffer
= xmalloc(size
+ 1);
792 memset(&stream
, 0, sizeof(stream
));
793 stream
.next_in
= data
;
794 stream
.avail_in
= left
;
795 stream
.next_out
= buffer
;
796 stream
.avail_out
= size
;
798 inflateInit(&stream
);
799 st
= inflate(&stream
, Z_FINISH
);
801 if ((st
!= Z_STREAM_END
) || stream
.total_out
!= size
) {
809 static void *unpack_entry(struct pack_entry
*entry
,
810 char *type
, unsigned long *sizep
)
812 struct packed_git
*p
= entry
->p
;
813 unsigned long offset
, size
, left
;
815 enum object_type kind
;
817 if (use_packed_git(p
))
818 die("cannot map packed file");
820 offset
= unpack_object_header(p
, entry
->offset
, &kind
, &size
);
821 pack
= p
->pack_base
+ offset
;
822 left
= p
->pack_size
- offset
;
825 return unpack_delta_entry(pack
, size
, left
, type
, sizep
);
827 strcpy(type
, "commit");
830 strcpy(type
, "tree");
833 strcpy(type
, "blob");
839 die("corrupted pack file");
842 return unpack_non_delta_entry(pack
, size
, left
);
845 static int find_pack_entry_1(const unsigned char *sha1
,
846 struct pack_entry
*e
, struct packed_git
*p
)
848 int *level1_ofs
= p
->index_base
;
849 int hi
= ntohl(level1_ofs
[*sha1
]);
850 int lo
= ((*sha1
== 0x0) ? 0 : ntohl(level1_ofs
[*sha1
- 1]));
851 void *index
= p
->index_base
+ 256;
854 int mi
= (lo
+ hi
) / 2;
855 int cmp
= memcmp(index
+ 24 * mi
+ 4, sha1
, 20);
857 e
->offset
= ntohl(*((int*)(index
+ 24 * mi
)));
858 memcpy(e
->sha1
, sha1
, 20);
870 static int find_pack_entry(const unsigned char *sha1
, struct pack_entry
*e
)
872 struct packed_git
*p
;
873 prepare_packed_git();
875 for (p
= packed_git
; p
; p
= p
->next
) {
876 if (find_pack_entry_1(sha1
, e
, p
))
882 int sha1_object_info(const unsigned char *sha1
, char *type
, unsigned long *sizep
)
885 unsigned long mapsize
, size
;
890 map
= map_sha1_file_internal(sha1
, &mapsize
, 0);
894 if (!find_pack_entry(sha1
, &e
))
895 return error("unable to find %s", sha1_to_hex(sha1
));
896 if (!packed_object_info(&e
, type
, sizep
))
899 map
= unpack_entry(&e
, type
, sizep
);
901 return (map
== NULL
) ? 0 : -1;
903 if (unpack_sha1_header(&stream
, map
, mapsize
, hdr
, sizeof(hdr
)) < 0)
904 status
= error("unable to unpack %s header",
906 if (parse_sha1_header(hdr
, type
, &size
) < 0)
907 status
= error("unable to parse %s header", sha1_to_hex(sha1
));
913 munmap(map
, mapsize
);
917 static void *read_packed_sha1(const unsigned char *sha1
, char *type
, unsigned long *size
)
921 if (!find_pack_entry(sha1
, &e
)) {
922 error("cannot read sha1_file for %s", sha1_to_hex(sha1
));
925 return unpack_entry(&e
, type
, size
);
928 void * read_sha1_file(const unsigned char *sha1
, char *type
, unsigned long *size
)
930 unsigned long mapsize
;
933 map
= map_sha1_file_internal(sha1
, &mapsize
, 0);
935 buf
= unpack_sha1_file(map
, mapsize
, type
, size
);
936 munmap(map
, mapsize
);
939 return read_packed_sha1(sha1
, type
, size
);
942 void *read_object_with_reference(const unsigned char *sha1
,
943 const char *required_type
,
945 unsigned char *actual_sha1_return
)
950 unsigned char actual_sha1
[20];
952 memcpy(actual_sha1
, sha1
, 20);
955 const char *ref_type
= NULL
;
957 buffer
= read_sha1_file(actual_sha1
, type
, &isize
);
960 if (!strcmp(type
, required_type
)) {
962 if (actual_sha1_return
)
963 memcpy(actual_sha1_return
, actual_sha1
, 20);
966 /* Handle references */
967 else if (!strcmp(type
, "commit"))
969 else if (!strcmp(type
, "tag"))
970 ref_type
= "object ";
975 ref_length
= strlen(ref_type
);
977 if (memcmp(buffer
, ref_type
, ref_length
) ||
978 get_sha1_hex(buffer
+ ref_length
, actual_sha1
)) {
982 /* Now we have the ID of the referred-to object in
983 * actual_sha1. Check again. */
987 static char *write_sha1_file_prepare(void *buf
,
996 /* Generate the header */
997 *hdrlen
= sprintf((char *)hdr
, "%s %lu", type
, len
)+1;
1001 SHA1_Update(&c
, hdr
, *hdrlen
);
1002 SHA1_Update(&c
, buf
, len
);
1003 SHA1_Final(sha1
, &c
);
1005 return sha1_file_name(sha1
);
1008 int write_sha1_file(void *buf
, unsigned long len
, const char *type
, unsigned char *returnsha1
)
1011 unsigned char *compressed
;
1013 unsigned char sha1
[20];
1015 static char tmpfile
[PATH_MAX
];
1016 unsigned char hdr
[50];
1017 int fd
, hdrlen
, ret
;
1019 /* Normally if we have it in the pack then we do not bother writing
1020 * it out into .git/objects/??/?{38} file.
1022 filename
= write_sha1_file_prepare(buf
, len
, type
, sha1
, hdr
, &hdrlen
);
1024 memcpy(returnsha1
, sha1
, 20);
1025 if (has_sha1_file(sha1
))
1027 fd
= open(filename
, O_RDONLY
);
1030 * FIXME!!! We might do collision checking here, but we'd
1031 * need to uncompress the old file and check it. Later.
1037 if (errno
!= ENOENT
) {
1038 fprintf(stderr
, "sha1 file %s: %s", filename
, strerror(errno
));
1042 snprintf(tmpfile
, sizeof(tmpfile
), "%s/obj_XXXXXX", get_object_directory());
1044 fd
= mkstemp(tmpfile
);
1046 fprintf(stderr
, "unable to create temporary sha1 filename %s: %s", tmpfile
, strerror(errno
));
1051 memset(&stream
, 0, sizeof(stream
));
1052 deflateInit(&stream
, Z_BEST_COMPRESSION
);
1053 size
= deflateBound(&stream
, len
+hdrlen
);
1054 compressed
= xmalloc(size
);
1057 stream
.next_out
= compressed
;
1058 stream
.avail_out
= size
;
1060 /* First header.. */
1061 stream
.next_in
= hdr
;
1062 stream
.avail_in
= hdrlen
;
1063 while (deflate(&stream
, 0) == Z_OK
)
1066 /* Then the data itself.. */
1067 stream
.next_in
= buf
;
1068 stream
.avail_in
= len
;
1069 while (deflate(&stream
, Z_FINISH
) == Z_OK
)
1071 deflateEnd(&stream
);
1072 size
= stream
.total_out
;
1074 if (write(fd
, compressed
, size
) != size
)
1075 die("unable to write file");
1080 ret
= link(tmpfile
, filename
);
1085 * Coda hack - coda doesn't like cross-directory links,
1086 * so we fall back to a rename, which will mean that it
1087 * won't be able to check collisions, but that's not a
1090 * When this succeeds, we just return 0. We have nothing
1093 if (ret
== EXDEV
&& !rename(tmpfile
, filename
))
1098 if (ret
!= EEXIST
) {
1099 fprintf(stderr
, "unable to write sha1 filename %s: %s", filename
, strerror(ret
));
1102 /* FIXME!!! Collision check here ? */
1108 int write_sha1_from_fd(const unsigned char *sha1
, int fd
)
1110 char *filename
= sha1_file_name(sha1
);
1114 unsigned char real_sha1
[20];
1115 unsigned char buf
[4096];
1116 unsigned char discard
[4096];
1120 local
= open(filename
, O_WRONLY
| O_CREAT
| O_EXCL
, 0666);
1123 return error("Couldn't open %s\n", filename
);
1125 memset(&stream
, 0, sizeof(stream
));
1127 inflateInit(&stream
);
1133 size
= read(fd
, buf
, 4096);
1138 return error("Connection closed?");
1139 perror("Reading from connection");
1142 write(local
, buf
, size
);
1143 stream
.avail_in
= size
;
1144 stream
.next_in
= buf
;
1146 stream
.next_out
= discard
;
1147 stream
.avail_out
= sizeof(discard
);
1148 ret
= inflate(&stream
, Z_SYNC_FLUSH
);
1149 SHA1_Update(&c
, discard
, sizeof(discard
) -
1151 } while (stream
.avail_in
&& ret
== Z_OK
);
1153 } while (ret
== Z_OK
);
1154 inflateEnd(&stream
);
1157 SHA1_Final(real_sha1
, &c
);
1158 if (ret
!= Z_STREAM_END
) {
1160 return error("File %s corrupted", sha1_to_hex(sha1
));
1162 if (memcmp(sha1
, real_sha1
, 20)) {
1164 return error("File %s has bad hash\n", sha1_to_hex(sha1
));
1170 int has_sha1_file(const unsigned char *sha1
)
1173 struct pack_entry e
;
1175 if (find_sha1_file(sha1
, &st
))
1177 return find_pack_entry(sha1
, &e
);
1180 int index_fd(unsigned char *sha1
, int fd
, struct stat
*st
)
1182 unsigned long size
= st
->st_size
;
1188 buf
= mmap(NULL
, size
, PROT_READ
, MAP_PRIVATE
, fd
, 0);
1190 if ((int)(long)buf
== -1)
1193 ret
= write_sha1_file(buf
, size
, "blob", sha1
);