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
,
66 static void setup_git_env(void)
68 git_dir
= gitenv(GIT_DIR_ENVIRONMENT
);
70 git_dir
= DEFAULT_GIT_DIR_ENVIRONMENT
;
71 git_object_dir
= gitenv(DB_ENVIRONMENT
);
72 if (!git_object_dir
) {
73 git_object_dir
= xmalloc(strlen(git_dir
) + 9);
74 sprintf(git_object_dir
, "%s/objects", git_dir
);
76 git_refs_dir
= xmalloc(strlen(git_dir
) + 6);
77 sprintf(git_refs_dir
, "%s/refs", git_dir
);
78 git_index_file
= gitenv(INDEX_ENVIRONMENT
);
79 if (!git_index_file
) {
80 git_index_file
= xmalloc(strlen(git_dir
) + 7);
81 sprintf(git_index_file
, "%s/index", git_dir
);
83 git_graft_file
= gitenv(GRAFT_ENVIRONMENT
);
85 git_graft_file
= strdup(git_path("info/grafts"));
88 char *get_object_directory(void)
92 return git_object_dir
;
95 char *get_refs_directory(void)
102 char *get_index_file(void)
106 return git_index_file
;
109 char *get_graft_file(void)
113 return git_graft_file
;
116 int safe_create_leading_directories(char *path
)
121 pos
= strchr(pos
, '/');
125 if (mkdir(path
, 0777) < 0)
126 if (errno
!= EEXIST
) {
135 int get_sha1(const char *str
, unsigned char *sha1
)
137 static const char *prefix
[] = {
147 if (!get_sha1_hex(str
, sha1
))
150 for (p
= prefix
; *p
; p
++) {
151 char * pathname
= git_path("%s/%s", *p
, str
);
152 if (!get_sha1_file(pathname
, sha1
))
159 char * sha1_to_hex(const unsigned char *sha1
)
161 static char buffer
[50];
162 static const char hex
[] = "0123456789abcdef";
166 for (i
= 0; i
< 20; i
++) {
167 unsigned int val
= *sha1
++;
168 *buf
++ = hex
[val
>> 4];
169 *buf
++ = hex
[val
& 0xf];
174 static void fill_sha1_path(char *pathbuf
, const unsigned char *sha1
)
177 for (i
= 0; i
< 20; i
++) {
178 static char hex
[] = "0123456789abcdef";
179 unsigned int val
= sha1
[i
];
180 char *pos
= pathbuf
+ i
*2 + (i
> 0);
181 *pos
++ = hex
[val
>> 4];
182 *pos
= hex
[val
& 0xf];
187 * NOTE! This returns a statically allocated buffer, so you have to be
188 * careful about using it. Do a "strdup()" if you need to save the
191 * Also note that this returns the location for creating. Reading
192 * SHA1 file can happen from any alternate directory listed in the
193 * DB_ENVIRONMENT environment variable if it is not found in
194 * the primary object database.
196 char *sha1_file_name(const unsigned char *sha1
)
198 static char *name
, *base
;
201 const char *sha1_file_directory
= get_object_directory();
202 int len
= strlen(sha1_file_directory
);
203 base
= xmalloc(len
+ 60);
204 memcpy(base
, sha1_file_directory
, len
);
205 memset(base
+len
, 0, 60);
208 name
= base
+ len
+ 1;
210 fill_sha1_path(name
, sha1
);
214 struct alternate_object_database
*alt_odb
;
217 * Prepare alternate object database registry.
218 * alt_odb points at an array of struct alternate_object_database.
219 * This array is terminated with an element that has both its base
220 * and name set to NULL. alt_odb[n] comes from n'th non-empty
221 * element from colon separated ALTERNATE_DB_ENVIRONMENT environment
222 * variable, and its base points at a statically allocated buffer
223 * that contains "/the/directory/corresponding/to/.git/objects/...",
224 * while its name points just after the slash at the end of
225 * ".git/objects/" in the example above, and has enough space to hold
226 * 40-byte hex SHA1, an extra slash for the first level indirection,
227 * and the terminating NUL.
228 * This function allocates the alt_odb array and all the strings
229 * pointed by base fields of the array elements with one xmalloc();
230 * the string pool immediately follows the array.
232 void prepare_alt_odb(void)
235 const char *cp
, *last
;
237 const char *alt
= gitenv(ALTERNATE_DB_ENVIRONMENT
) ? : "";
241 /* The first pass counts how large an area to allocate to
242 * hold the entire alt_odb structure, including array of
243 * structs and path buffers for them. The second pass fills
244 * the structure and prepares the path buffers for use by
247 for (totlen
= pass
= 0; pass
< 2; pass
++) {
251 cp
= strchr(last
, ':') ? : last
+ strlen(last
);
253 /* 43 = 40-byte + 2 '/' + terminating NUL */
254 int pfxlen
= cp
- last
;
255 int entlen
= pfxlen
+ 43;
259 alt_odb
[i
].base
= op
;
260 alt_odb
[i
].name
= op
+ pfxlen
+ 1;
261 memcpy(op
, last
, pfxlen
);
262 op
[pfxlen
] = op
[pfxlen
+ 3] = '/';
268 while (*cp
&& *cp
== ':')
274 alt_odb
= xmalloc(sizeof(*alt_odb
) * (i
+ 1) + totlen
);
275 alt_odb
[i
].base
= alt_odb
[i
].name
= NULL
;
276 op
= (char*)(&alt_odb
[i
+1]);
280 static char *find_sha1_file(const unsigned char *sha1
, struct stat
*st
)
283 char *name
= sha1_file_name(sha1
);
288 for (i
= 0; (name
= alt_odb
[i
].name
) != NULL
; i
++) {
289 fill_sha1_path(name
, sha1
);
290 if (!stat(alt_odb
[i
].base
, st
))
291 return alt_odb
[i
].base
;
296 #define PACK_MAX_SZ (1<<26)
297 static int pack_used_ctr
;
298 static unsigned long pack_mapped
;
299 struct packed_git
*packed_git
;
301 static int check_packed_git_idx(const char *path
, unsigned long *idx_size_
,
306 unsigned long idx_size
;
308 int fd
= open(path
, O_RDONLY
);
312 if (fstat(fd
, &st
)) {
316 idx_size
= st
.st_size
;
317 idx_map
= mmap(NULL
, idx_size
, PROT_READ
, MAP_PRIVATE
, fd
, 0);
319 if (idx_map
== MAP_FAILED
)
324 *idx_size_
= idx_size
;
326 /* check index map */
327 if (idx_size
< 4*256 + 20 + 20)
328 return error("index file too small");
330 for (i
= 0; i
< 256; i
++) {
331 unsigned int n
= ntohl(index
[i
]);
333 return error("non-monotonic index");
339 * - 256 index entries 4 bytes each
340 * - 24-byte entries * nr (20-byte sha1 + 4-byte offset)
341 * - 20-byte SHA1 of the packfile
342 * - 20-byte SHA1 file checksum
344 if (idx_size
!= 4*256 + nr
* 24 + 20 + 20)
345 return error("wrong index file size");
350 static int unuse_one_packed_git(void)
352 struct packed_git
*p
, *lru
= NULL
;
354 for (p
= packed_git
; p
; p
= p
->next
) {
355 if (p
->pack_use_cnt
|| !p
->pack_base
)
357 if (!lru
|| p
->pack_last_used
< lru
->pack_last_used
)
362 munmap(lru
->pack_base
, lru
->pack_size
);
363 lru
->pack_base
= NULL
;
367 void unuse_packed_git(struct packed_git
*p
)
372 int use_packed_git(struct packed_git
*p
)
379 pack_mapped
+= p
->pack_size
;
380 while (PACK_MAX_SZ
< pack_mapped
&& unuse_one_packed_git())
382 fd
= open(p
->pack_name
, O_RDONLY
);
384 die("packfile %s cannot be opened", p
->pack_name
);
385 if (fstat(fd
, &st
)) {
387 die("packfile %s cannot be opened", p
->pack_name
);
389 if (st
.st_size
!= p
->pack_size
)
390 die("packfile %s size mismatch.", p
->pack_name
);
391 map
= mmap(NULL
, p
->pack_size
, PROT_READ
, MAP_PRIVATE
, fd
, 0);
393 if (map
== MAP_FAILED
)
394 die("packfile %s cannot be mapped.", p
->pack_name
);
397 /* Check if the pack file matches with the index file.
400 if (memcmp((char*)(p
->index_base
) + p
->index_size
- 40,
401 p
->pack_base
+ p
->pack_size
- 20, 20))
402 die("packfile %s does not match index.", p
->pack_name
);
404 p
->pack_last_used
= pack_used_ctr
++;
409 struct packed_git
*add_packed_git(char *path
, int path_len
)
412 struct packed_git
*p
;
413 unsigned long idx_size
;
416 if (check_packed_git_idx(path
, &idx_size
, &idx_map
))
419 /* do we have a corresponding .pack file? */
420 strcpy(path
+ path_len
- 4, ".pack");
421 if (stat(path
, &st
) || !S_ISREG(st
.st_mode
)) {
422 munmap(idx_map
, idx_size
);
425 /* ok, it looks sane as far as we can check without
426 * actually mapping the pack file.
428 p
= xmalloc(sizeof(*p
) + path_len
+ 2);
429 strcpy(p
->pack_name
, path
);
430 p
->index_size
= idx_size
;
431 p
->pack_size
= st
.st_size
;
432 p
->index_base
= idx_map
;
435 p
->pack_last_used
= 0;
440 static void prepare_packed_git_one(char *objdir
)
447 sprintf(path
, "%s/pack", objdir
);
453 while ((de
= readdir(dir
)) != NULL
) {
454 int namelen
= strlen(de
->d_name
);
455 struct packed_git
*p
;
457 if (strcmp(de
->d_name
+ namelen
- 4, ".idx"))
460 /* we have .idx. Is it a file we can map? */
461 strcpy(path
+ len
, de
->d_name
);
462 p
= add_packed_git(path
, len
+ namelen
);
465 p
->next
= packed_git
;
471 void prepare_packed_git(void)
474 static int run_once
= 0;
479 prepare_packed_git_one(get_object_directory());
481 for (i
= 0; alt_odb
[i
].base
!= NULL
; i
++) {
482 alt_odb
[i
].name
[0] = 0;
483 prepare_packed_git_one(alt_odb
[i
].base
);
487 int check_sha1_signature(const unsigned char *sha1
, void *map
, unsigned long size
, const char *type
)
490 unsigned char real_sha1
[20];
494 SHA1_Update(&c
, header
, 1+sprintf(header
, "%s %lu", type
, size
));
495 SHA1_Update(&c
, map
, size
);
496 SHA1_Final(real_sha1
, &c
);
497 return memcmp(sha1
, real_sha1
, 20) ? -1 : 0;
500 static void *map_sha1_file_internal(const unsigned char *sha1
,
506 char *filename
= find_sha1_file(sha1
, &st
);
512 fd
= open(filename
, O_RDONLY
| sha1_file_open_flag
);
514 /* See if it works without O_NOATIME */
515 switch (sha1_file_open_flag
) {
517 fd
= open(filename
, O_RDONLY
);
525 /* If it failed once, it will probably fail again.
526 * Stop using O_NOATIME
528 sha1_file_open_flag
= 0;
530 map
= mmap(NULL
, st
.st_size
, PROT_READ
, MAP_PRIVATE
, fd
, 0);
532 if (map
== MAP_FAILED
)
538 int unpack_sha1_header(z_stream
*stream
, void *map
, unsigned long mapsize
, void *buffer
, unsigned long size
)
540 /* Get the data stream */
541 memset(stream
, 0, sizeof(*stream
));
542 stream
->next_in
= map
;
543 stream
->avail_in
= mapsize
;
544 stream
->next_out
= buffer
;
545 stream
->avail_out
= size
;
548 return inflate(stream
, 0);
551 static void *unpack_sha1_rest(z_stream
*stream
, void *buffer
, unsigned long size
)
553 int bytes
= strlen(buffer
) + 1;
554 unsigned char *buf
= xmalloc(1+size
);
556 memcpy(buf
, buffer
+ bytes
, stream
->total_out
- bytes
);
557 bytes
= stream
->total_out
- bytes
;
559 stream
->next_out
= buf
+ bytes
;
560 stream
->avail_out
= size
- bytes
;
561 while (inflate(stream
, Z_FINISH
) == Z_OK
)
570 * We used to just use "sscanf()", but that's actually way
571 * too permissive for what we want to check. So do an anal
572 * object header parse by hand.
574 int parse_sha1_header(char *hdr
, char *type
, unsigned long *sizep
)
580 * The type can be at most ten bytes (including the
581 * terminating '\0' that we add), and is followed by
596 * The length must follow immediately, and be in canonical
597 * decimal format (ie "010" is not valid).
604 unsigned long c
= *hdr
- '0';
608 size
= size
* 10 + c
;
614 * The length must be followed by a zero byte
616 return *hdr
? -1 : 0;
619 void * unpack_sha1_file(void *map
, unsigned long mapsize
, char *type
, unsigned long *size
)
625 ret
= unpack_sha1_header(&stream
, map
, mapsize
, hdr
, sizeof(hdr
));
626 if (ret
< Z_OK
|| parse_sha1_header(hdr
, type
, size
) < 0)
629 return unpack_sha1_rest(&stream
, hdr
, *size
);
632 /* forward declaration for a mutually recursive function */
633 static int packed_object_info(struct pack_entry
*entry
,
634 char *type
, unsigned long *sizep
);
636 static int packed_delta_info(unsigned char *base_sha1
,
637 unsigned long delta_size
,
640 unsigned long *sizep
,
641 struct packed_git
*p
)
643 struct pack_entry base_ent
;
646 die("truncated pack file");
648 /* The base entry _must_ be in the same pack */
649 if (!find_pack_entry_one(base_sha1
, &base_ent
, p
))
650 die("failed to find delta-pack base object %s",
651 sha1_to_hex(base_sha1
));
653 /* We choose to only get the type of the base object and
654 * ignore potentially corrupt pack file that expects the delta
655 * based on a base with a wrong size. This saves tons of
659 if (packed_object_info(&base_ent
, type
, NULL
))
660 die("cannot get info for delta-pack base");
663 const unsigned char *data
;
664 unsigned char delta_head
[64];
665 unsigned long result_size
;
669 memset(&stream
, 0, sizeof(stream
));
671 data
= stream
.next_in
= base_sha1
+ 20;
672 stream
.avail_in
= left
- 20;
673 stream
.next_out
= delta_head
;
674 stream
.avail_out
= sizeof(delta_head
);
676 inflateInit(&stream
);
677 st
= inflate(&stream
, Z_FINISH
);
679 if ((st
!= Z_STREAM_END
) &&
680 stream
.total_out
!= sizeof(delta_head
))
681 die("delta data unpack-initial failed");
683 /* Examine the initial part of the delta to figure out
687 get_delta_hdr_size(&data
); /* ignore base size */
689 /* Read the result size */
690 result_size
= get_delta_hdr_size(&data
);
691 *sizep
= result_size
;
696 static unsigned long unpack_object_header(struct packed_git
*p
, unsigned long offset
,
697 enum object_type
*type
, unsigned long *sizep
)
700 unsigned char *pack
, c
;
703 if (offset
>= p
->pack_size
)
704 die("object offset outside of pack file");
706 pack
= p
->pack_base
+ offset
;
709 *type
= (c
>> 4) & 7;
713 if (offset
>= p
->pack_size
)
714 die("object offset outside of pack file");
717 size
+= (c
& 0x7f) << shift
;
724 void packed_object_info_detail(struct pack_entry
*e
,
727 unsigned long *store_size
,
728 int *delta_chain_length
,
729 unsigned char *base_sha1
)
731 struct packed_git
*p
= e
->p
;
732 unsigned long offset
, left
;
734 enum object_type kind
;
736 offset
= unpack_object_header(p
, e
->offset
, &kind
, size
);
737 pack
= p
->pack_base
+ offset
;
738 left
= p
->pack_size
- offset
;
739 if (kind
!= OBJ_DELTA
)
740 *delta_chain_length
= 0;
742 int chain_length
= 0;
743 memcpy(base_sha1
, pack
, 20);
745 struct pack_entry base_ent
;
748 find_pack_entry_one(pack
, &base_ent
, p
);
749 offset
= unpack_object_header(p
, base_ent
.offset
,
751 pack
= p
->pack_base
+ offset
;
753 } while (kind
== OBJ_DELTA
);
754 *delta_chain_length
= chain_length
;
758 strcpy(type
, "commit");
761 strcpy(type
, "tree");
764 strcpy(type
, "blob");
770 die("corrupted pack file");
772 *store_size
= 0; /* notyet */
775 static int packed_object_info(struct pack_entry
*entry
,
776 char *type
, unsigned long *sizep
)
778 struct packed_git
*p
= entry
->p
;
779 unsigned long offset
, size
, left
;
781 enum object_type kind
;
784 if (use_packed_git(p
))
785 die("cannot map packed file");
787 offset
= unpack_object_header(p
, entry
->offset
, &kind
, &size
);
788 pack
= p
->pack_base
+ offset
;
789 left
= p
->pack_size
- offset
;
793 retval
= packed_delta_info(pack
, size
, left
, type
, sizep
, p
);
797 strcpy(type
, "commit");
800 strcpy(type
, "tree");
803 strcpy(type
, "blob");
809 die("corrupted pack file");
817 /* forward declaration for a mutually recursive function */
818 static void *unpack_entry(struct pack_entry
*, char *, unsigned long *);
820 static void *unpack_delta_entry(unsigned char *base_sha1
,
821 unsigned long delta_size
,
824 unsigned long *sizep
,
825 struct packed_git
*p
)
827 struct pack_entry base_ent
;
828 void *data
, *delta_data
, *result
, *base
;
829 unsigned long data_size
, result_size
, base_size
;
834 die("truncated pack file");
835 data
= base_sha1
+ 20;
836 data_size
= left
- 20;
837 delta_data
= xmalloc(delta_size
);
839 memset(&stream
, 0, sizeof(stream
));
841 stream
.next_in
= data
;
842 stream
.avail_in
= data_size
;
843 stream
.next_out
= delta_data
;
844 stream
.avail_out
= delta_size
;
846 inflateInit(&stream
);
847 st
= inflate(&stream
, Z_FINISH
);
849 if ((st
!= Z_STREAM_END
) || stream
.total_out
!= delta_size
)
850 die("delta data unpack failed");
852 /* The base entry _must_ be in the same pack */
853 if (!find_pack_entry_one(base_sha1
, &base_ent
, p
))
854 die("failed to find delta-pack base object %s",
855 sha1_to_hex(base_sha1
));
856 base
= unpack_entry_gently(&base_ent
, type
, &base_size
);
858 die("failed to read delta-pack base object %s",
859 sha1_to_hex(base_sha1
));
860 result
= patch_delta(base
, base_size
,
861 delta_data
, delta_size
,
864 die("failed to apply delta");
867 *sizep
= result_size
;
871 static void *unpack_non_delta_entry(unsigned char *data
,
877 unsigned char *buffer
;
879 buffer
= xmalloc(size
+ 1);
881 memset(&stream
, 0, sizeof(stream
));
882 stream
.next_in
= data
;
883 stream
.avail_in
= left
;
884 stream
.next_out
= buffer
;
885 stream
.avail_out
= size
;
887 inflateInit(&stream
);
888 st
= inflate(&stream
, Z_FINISH
);
890 if ((st
!= Z_STREAM_END
) || stream
.total_out
!= size
) {
898 static void *unpack_entry(struct pack_entry
*entry
,
899 char *type
, unsigned long *sizep
)
901 struct packed_git
*p
= entry
->p
;
904 if (use_packed_git(p
))
905 die("cannot map packed file");
906 retval
= unpack_entry_gently(entry
, type
, sizep
);
909 die("corrupted pack file");
913 /* The caller is responsible for use_packed_git()/unuse_packed_git() pair */
914 void *unpack_entry_gently(struct pack_entry
*entry
,
915 char *type
, unsigned long *sizep
)
917 struct packed_git
*p
= entry
->p
;
918 unsigned long offset
, size
, left
;
920 enum object_type kind
;
923 offset
= unpack_object_header(p
, entry
->offset
, &kind
, &size
);
924 pack
= p
->pack_base
+ offset
;
925 left
= p
->pack_size
- offset
;
928 retval
= unpack_delta_entry(pack
, size
, left
, type
, sizep
, p
);
931 strcpy(type
, "commit");
934 strcpy(type
, "tree");
937 strcpy(type
, "blob");
946 retval
= unpack_non_delta_entry(pack
, size
, left
);
950 int num_packed_objects(const struct packed_git
*p
)
952 /* See check_packed_git_idx() */
953 return (p
->index_size
- 20 - 20 - 4*256) / 24;
956 int nth_packed_object_sha1(const struct packed_git
*p
, int n
,
959 void *index
= p
->index_base
+ 256;
960 if (n
< 0 || num_packed_objects(p
) <= n
)
962 memcpy(sha1
, (index
+ 24 * n
+ 4), 20);
966 int find_pack_entry_one(const unsigned char *sha1
,
967 struct pack_entry
*e
, struct packed_git
*p
)
969 unsigned int *level1_ofs
= p
->index_base
;
970 int hi
= ntohl(level1_ofs
[*sha1
]);
971 int lo
= ((*sha1
== 0x0) ? 0 : ntohl(level1_ofs
[*sha1
- 1]));
972 void *index
= p
->index_base
+ 256;
975 int mi
= (lo
+ hi
) / 2;
976 int cmp
= memcmp(index
+ 24 * mi
+ 4, sha1
, 20);
978 e
->offset
= ntohl(*((int*)(index
+ 24 * mi
)));
979 memcpy(e
->sha1
, sha1
, 20);
991 static int find_pack_entry(const unsigned char *sha1
, struct pack_entry
*e
)
993 struct packed_git
*p
;
994 prepare_packed_git();
996 for (p
= packed_git
; p
; p
= p
->next
) {
997 if (find_pack_entry_one(sha1
, e
, p
))
1003 int sha1_object_info(const unsigned char *sha1
, char *type
, unsigned long *sizep
)
1006 unsigned long mapsize
, size
;
1011 map
= map_sha1_file_internal(sha1
, &mapsize
);
1013 struct pack_entry e
;
1015 if (!find_pack_entry(sha1
, &e
))
1016 return error("unable to find %s", sha1_to_hex(sha1
));
1017 return packed_object_info(&e
, type
, sizep
);
1019 if (unpack_sha1_header(&stream
, map
, mapsize
, hdr
, sizeof(hdr
)) < 0)
1020 status
= error("unable to unpack %s header",
1022 if (parse_sha1_header(hdr
, type
, &size
) < 0)
1023 status
= error("unable to parse %s header", sha1_to_hex(sha1
));
1029 inflateEnd(&stream
);
1030 munmap(map
, mapsize
);
1034 static void *read_packed_sha1(const unsigned char *sha1
, char *type
, unsigned long *size
)
1036 struct pack_entry e
;
1038 if (!find_pack_entry(sha1
, &e
)) {
1039 error("cannot read sha1_file for %s", sha1_to_hex(sha1
));
1042 return unpack_entry(&e
, type
, size
);
1045 void * read_sha1_file(const unsigned char *sha1
, char *type
, unsigned long *size
)
1047 unsigned long mapsize
;
1049 struct pack_entry e
;
1051 if (find_pack_entry(sha1
, &e
))
1052 return read_packed_sha1(sha1
, type
, size
);
1053 map
= map_sha1_file_internal(sha1
, &mapsize
);
1055 buf
= unpack_sha1_file(map
, mapsize
, type
, size
);
1056 munmap(map
, mapsize
);
1062 void *read_object_with_reference(const unsigned char *sha1
,
1063 const char *required_type
,
1064 unsigned long *size
,
1065 unsigned char *actual_sha1_return
)
1069 unsigned long isize
;
1070 unsigned char actual_sha1
[20];
1072 memcpy(actual_sha1
, sha1
, 20);
1074 int ref_length
= -1;
1075 const char *ref_type
= NULL
;
1077 buffer
= read_sha1_file(actual_sha1
, type
, &isize
);
1080 if (!strcmp(type
, required_type
)) {
1082 if (actual_sha1_return
)
1083 memcpy(actual_sha1_return
, actual_sha1
, 20);
1086 /* Handle references */
1087 else if (!strcmp(type
, "commit"))
1089 else if (!strcmp(type
, "tag"))
1090 ref_type
= "object ";
1095 ref_length
= strlen(ref_type
);
1097 if (memcmp(buffer
, ref_type
, ref_length
) ||
1098 get_sha1_hex(buffer
+ ref_length
, actual_sha1
)) {
1102 /* Now we have the ID of the referred-to object in
1103 * actual_sha1. Check again. */
1107 char *write_sha1_file_prepare(void *buf
,
1110 unsigned char *sha1
,
1116 /* Generate the header */
1117 *hdrlen
= sprintf((char *)hdr
, "%s %lu", type
, len
)+1;
1121 SHA1_Update(&c
, hdr
, *hdrlen
);
1122 SHA1_Update(&c
, buf
, len
);
1123 SHA1_Final(sha1
, &c
);
1125 return sha1_file_name(sha1
);
1128 int write_sha1_file(void *buf
, unsigned long len
, const char *type
, unsigned char *returnsha1
)
1131 unsigned char *compressed
;
1133 unsigned char sha1
[20];
1135 static char tmpfile
[PATH_MAX
];
1136 unsigned char hdr
[50];
1137 int fd
, hdrlen
, ret
;
1139 /* Normally if we have it in the pack then we do not bother writing
1140 * it out into .git/objects/??/?{38} file.
1142 filename
= write_sha1_file_prepare(buf
, len
, type
, sha1
, hdr
, &hdrlen
);
1144 memcpy(returnsha1
, sha1
, 20);
1145 if (has_sha1_file(sha1
))
1147 fd
= open(filename
, O_RDONLY
);
1150 * FIXME!!! We might do collision checking here, but we'd
1151 * need to uncompress the old file and check it. Later.
1157 if (errno
!= ENOENT
) {
1158 fprintf(stderr
, "sha1 file %s: %s", filename
, strerror(errno
));
1162 snprintf(tmpfile
, sizeof(tmpfile
), "%s/obj_XXXXXX", get_object_directory());
1164 fd
= mkstemp(tmpfile
);
1166 fprintf(stderr
, "unable to create temporary sha1 filename %s: %s", tmpfile
, strerror(errno
));
1171 memset(&stream
, 0, sizeof(stream
));
1172 deflateInit(&stream
, Z_BEST_COMPRESSION
);
1173 size
= deflateBound(&stream
, len
+hdrlen
);
1174 compressed
= xmalloc(size
);
1177 stream
.next_out
= compressed
;
1178 stream
.avail_out
= size
;
1180 /* First header.. */
1181 stream
.next_in
= hdr
;
1182 stream
.avail_in
= hdrlen
;
1183 while (deflate(&stream
, 0) == Z_OK
)
1186 /* Then the data itself.. */
1187 stream
.next_in
= buf
;
1188 stream
.avail_in
= len
;
1189 while (deflate(&stream
, Z_FINISH
) == Z_OK
)
1191 deflateEnd(&stream
);
1192 size
= stream
.total_out
;
1194 if (write(fd
, compressed
, size
) != size
)
1195 die("unable to write file");
1200 ret
= link(tmpfile
, filename
);
1205 * Coda hack - coda doesn't like cross-directory links,
1206 * so we fall back to a rename, which will mean that it
1207 * won't be able to check collisions, but that's not a
1210 * When this succeeds, we just return 0. We have nothing
1213 if (ret
== EXDEV
&& !rename(tmpfile
, filename
))
1218 if (ret
!= EEXIST
) {
1219 fprintf(stderr
, "unable to write sha1 filename %s: %s", filename
, strerror(ret
));
1222 /* FIXME!!! Collision check here ? */
1228 int write_sha1_to_fd(int fd
, const unsigned char *sha1
)
1231 unsigned long objsize
;
1233 void *buf
= map_sha1_file_internal(sha1
, &objsize
);
1236 unsigned char *unpacked
;
1241 // need to unpack and recompress it by itself
1242 unpacked
= read_packed_sha1(sha1
, type
, &len
);
1244 hdrlen
= sprintf(hdr
, "%s %lu", type
, len
) + 1;
1247 memset(&stream
, 0, sizeof(stream
));
1248 deflateInit(&stream
, Z_BEST_COMPRESSION
);
1249 size
= deflateBound(&stream
, len
+ hdrlen
);
1250 buf
= xmalloc(size
);
1253 stream
.next_out
= buf
;
1254 stream
.avail_out
= size
;
1256 /* First header.. */
1257 stream
.next_in
= (void *)hdr
;
1258 stream
.avail_in
= hdrlen
;
1259 while (deflate(&stream
, 0) == Z_OK
)
1262 /* Then the data itself.. */
1263 stream
.next_in
= unpacked
;
1264 stream
.avail_in
= len
;
1265 while (deflate(&stream
, Z_FINISH
) == Z_OK
)
1267 deflateEnd(&stream
);
1269 objsize
= stream
.total_out
;
1273 size
= write(fd
, buf
+ posn
, objsize
- posn
);
1276 fprintf(stderr
, "write closed");
1283 } while (posn
< objsize
);
1287 int write_sha1_from_fd(const unsigned char *sha1
, int fd
)
1289 char *filename
= sha1_file_name(sha1
);
1293 unsigned char real_sha1
[20];
1294 unsigned char buf
[4096];
1295 unsigned char discard
[4096];
1299 local
= open(filename
, O_WRONLY
| O_CREAT
| O_EXCL
, 0666);
1302 return error("Couldn't open %s\n", filename
);
1304 memset(&stream
, 0, sizeof(stream
));
1306 inflateInit(&stream
);
1312 size
= read(fd
, buf
, 4096);
1317 return error("Connection closed?");
1318 perror("Reading from connection");
1321 write(local
, buf
, size
);
1322 stream
.avail_in
= size
;
1323 stream
.next_in
= buf
;
1325 stream
.next_out
= discard
;
1326 stream
.avail_out
= sizeof(discard
);
1327 ret
= inflate(&stream
, Z_SYNC_FLUSH
);
1328 SHA1_Update(&c
, discard
, sizeof(discard
) -
1330 } while (stream
.avail_in
&& ret
== Z_OK
);
1332 } while (ret
== Z_OK
);
1333 inflateEnd(&stream
);
1336 SHA1_Final(real_sha1
, &c
);
1337 if (ret
!= Z_STREAM_END
) {
1339 return error("File %s corrupted", sha1_to_hex(sha1
));
1341 if (memcmp(sha1
, real_sha1
, 20)) {
1343 return error("File %s has bad hash\n", sha1_to_hex(sha1
));
1349 int has_sha1_pack(const unsigned char *sha1
)
1351 struct pack_entry e
;
1352 return find_pack_entry(sha1
, &e
);
1355 int has_sha1_file(const unsigned char *sha1
)
1358 struct pack_entry e
;
1360 if (find_pack_entry(sha1
, &e
))
1362 return find_sha1_file(sha1
, &st
) ? 1 : 0;
1365 int index_fd(unsigned char *sha1
, int fd
, struct stat
*st
, int write_object
, const char *type
)
1367 unsigned long size
= st
->st_size
;
1370 unsigned char hdr
[50];
1375 buf
= mmap(NULL
, size
, PROT_READ
, MAP_PRIVATE
, fd
, 0);
1377 if (buf
== MAP_FAILED
)
1383 ret
= write_sha1_file(buf
, size
, type
, sha1
);
1385 write_sha1_file_prepare(buf
, size
, type
, sha1
, hdr
, &hdrlen
);