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 char *git_dir
, *git_object_dir
, *git_index_file
, *git_refs_dir
,
51 static void setup_git_env(void)
53 git_dir
= gitenv(GIT_DIR_ENVIRONMENT
);
55 git_dir
= DEFAULT_GIT_DIR_ENVIRONMENT
;
56 git_object_dir
= gitenv(DB_ENVIRONMENT
);
57 if (!git_object_dir
) {
58 git_object_dir
= xmalloc(strlen(git_dir
) + 9);
59 sprintf(git_object_dir
, "%s/objects", git_dir
);
61 git_refs_dir
= xmalloc(strlen(git_dir
) + 6);
62 sprintf(git_refs_dir
, "%s/refs", git_dir
);
63 git_index_file
= gitenv(INDEX_ENVIRONMENT
);
64 if (!git_index_file
) {
65 git_index_file
= xmalloc(strlen(git_dir
) + 7);
66 sprintf(git_index_file
, "%s/index", git_dir
);
68 git_graft_file
= gitenv(GRAFT_ENVIRONMENT
);
70 git_graft_file
= strdup(git_path("info/grafts"));
73 char *get_object_directory(void)
77 return git_object_dir
;
80 char *get_refs_directory(void)
87 char *get_index_file(void)
91 return git_index_file
;
94 char *get_graft_file(void)
98 return git_graft_file
;
101 int safe_create_leading_directories(char *path
)
106 pos
= strchr(pos
, '/');
110 if (mkdir(path
, 0777) < 0)
111 if (errno
!= EEXIST
) {
120 char * sha1_to_hex(const unsigned char *sha1
)
122 static char buffer
[50];
123 static const char hex
[] = "0123456789abcdef";
127 for (i
= 0; i
< 20; i
++) {
128 unsigned int val
= *sha1
++;
129 *buf
++ = hex
[val
>> 4];
130 *buf
++ = hex
[val
& 0xf];
135 static void fill_sha1_path(char *pathbuf
, const unsigned char *sha1
)
138 for (i
= 0; i
< 20; i
++) {
139 static char hex
[] = "0123456789abcdef";
140 unsigned int val
= sha1
[i
];
141 char *pos
= pathbuf
+ i
*2 + (i
> 0);
142 *pos
++ = hex
[val
>> 4];
143 *pos
= hex
[val
& 0xf];
148 * NOTE! This returns a statically allocated buffer, so you have to be
149 * careful about using it. Do a "strdup()" if you need to save the
152 * Also note that this returns the location for creating. Reading
153 * SHA1 file can happen from any alternate directory listed in the
154 * DB_ENVIRONMENT environment variable if it is not found in
155 * the primary object database.
157 char *sha1_file_name(const unsigned char *sha1
)
159 static char *name
, *base
;
162 const char *sha1_file_directory
= get_object_directory();
163 int len
= strlen(sha1_file_directory
);
164 base
= xmalloc(len
+ 60);
165 memcpy(base
, sha1_file_directory
, len
);
166 memset(base
+len
, 0, 60);
169 name
= base
+ len
+ 1;
171 fill_sha1_path(name
, sha1
);
175 char *sha1_pack_name(const unsigned char *sha1
)
177 static const char hex
[] = "0123456789abcdef";
178 static char *name
, *base
, *buf
;
182 const char *sha1_file_directory
= get_object_directory();
183 int len
= strlen(sha1_file_directory
);
184 base
= xmalloc(len
+ 60);
185 sprintf(base
, "%s/pack/pack-1234567890123456789012345678901234567890.pack", sha1_file_directory
);
186 name
= base
+ len
+ 11;
191 for (i
= 0; i
< 20; i
++) {
192 unsigned int val
= *sha1
++;
193 *buf
++ = hex
[val
>> 4];
194 *buf
++ = hex
[val
& 0xf];
200 char *sha1_pack_index_name(const unsigned char *sha1
)
202 static const char hex
[] = "0123456789abcdef";
203 static char *name
, *base
, *buf
;
207 const char *sha1_file_directory
= get_object_directory();
208 int len
= strlen(sha1_file_directory
);
209 base
= xmalloc(len
+ 60);
210 sprintf(base
, "%s/pack/pack-1234567890123456789012345678901234567890.idx", sha1_file_directory
);
211 name
= base
+ len
+ 11;
216 for (i
= 0; i
< 20; i
++) {
217 unsigned int val
= *sha1
++;
218 *buf
++ = hex
[val
>> 4];
219 *buf
++ = hex
[val
& 0xf];
225 struct alternate_object_database
*alt_odb
;
228 * Prepare alternate object database registry.
229 * alt_odb points at an array of struct alternate_object_database.
230 * This array is terminated with an element that has both its base
231 * and name set to NULL. alt_odb[n] comes from n'th non-empty
232 * element from colon separated ALTERNATE_DB_ENVIRONMENT environment
233 * variable, and its base points at a statically allocated buffer
234 * that contains "/the/directory/corresponding/to/.git/objects/...",
235 * while its name points just after the slash at the end of
236 * ".git/objects/" in the example above, and has enough space to hold
237 * 40-byte hex SHA1, an extra slash for the first level indirection,
238 * and the terminating NUL.
239 * This function allocates the alt_odb array and all the strings
240 * pointed by base fields of the array elements with one xmalloc();
241 * the string pool immediately follows the array.
243 void prepare_alt_odb(void)
246 const char *cp
, *last
;
248 const char *alt
= gitenv(ALTERNATE_DB_ENVIRONMENT
) ? : "";
252 /* The first pass counts how large an area to allocate to
253 * hold the entire alt_odb structure, including array of
254 * structs and path buffers for them. The second pass fills
255 * the structure and prepares the path buffers for use by
258 for (totlen
= pass
= 0; pass
< 2; pass
++) {
262 cp
= strchr(last
, ':') ? : last
+ strlen(last
);
264 /* 43 = 40-byte + 2 '/' + terminating NUL */
265 int pfxlen
= cp
- last
;
266 int entlen
= pfxlen
+ 43;
270 alt_odb
[i
].base
= op
;
271 alt_odb
[i
].name
= op
+ pfxlen
+ 1;
272 memcpy(op
, last
, pfxlen
);
273 op
[pfxlen
] = op
[pfxlen
+ 3] = '/';
279 while (*cp
&& *cp
== ':')
285 alt_odb
= xmalloc(sizeof(*alt_odb
) * (i
+ 1) + totlen
);
286 alt_odb
[i
].base
= alt_odb
[i
].name
= NULL
;
287 op
= (char*)(&alt_odb
[i
+1]);
291 static char *find_sha1_file(const unsigned char *sha1
, struct stat
*st
)
294 char *name
= sha1_file_name(sha1
);
299 for (i
= 0; (name
= alt_odb
[i
].name
) != NULL
; i
++) {
300 fill_sha1_path(name
, sha1
);
301 if (!stat(alt_odb
[i
].base
, st
))
302 return alt_odb
[i
].base
;
307 #define PACK_MAX_SZ (1<<26)
308 static int pack_used_ctr
;
309 static unsigned long pack_mapped
;
310 struct packed_git
*packed_git
;
312 static int check_packed_git_idx(const char *path
, unsigned long *idx_size_
,
317 unsigned long idx_size
;
319 int fd
= open(path
, O_RDONLY
);
323 if (fstat(fd
, &st
)) {
327 idx_size
= st
.st_size
;
328 idx_map
= mmap(NULL
, idx_size
, PROT_READ
, MAP_PRIVATE
, fd
, 0);
330 if (idx_map
== MAP_FAILED
)
335 *idx_size_
= idx_size
;
337 /* check index map */
338 if (idx_size
< 4*256 + 20 + 20)
339 return error("index file too small");
341 for (i
= 0; i
< 256; i
++) {
342 unsigned int n
= ntohl(index
[i
]);
344 return error("non-monotonic index");
350 * - 256 index entries 4 bytes each
351 * - 24-byte entries * nr (20-byte sha1 + 4-byte offset)
352 * - 20-byte SHA1 of the packfile
353 * - 20-byte SHA1 file checksum
355 if (idx_size
!= 4*256 + nr
* 24 + 20 + 20)
356 return error("wrong index file size");
361 static int unuse_one_packed_git(void)
363 struct packed_git
*p
, *lru
= NULL
;
365 for (p
= packed_git
; p
; p
= p
->next
) {
366 if (p
->pack_use_cnt
|| !p
->pack_base
)
368 if (!lru
|| p
->pack_last_used
< lru
->pack_last_used
)
373 munmap(lru
->pack_base
, lru
->pack_size
);
374 lru
->pack_base
= NULL
;
378 void unuse_packed_git(struct packed_git
*p
)
383 int use_packed_git(struct packed_git
*p
)
387 // We created the struct before we had the pack
388 stat(p
->pack_name
, &st
);
389 if (!S_ISREG(st
.st_mode
))
390 die("packfile %s not a regular file", p
->pack_name
);
391 p
->pack_size
= st
.st_size
;
398 pack_mapped
+= p
->pack_size
;
399 while (PACK_MAX_SZ
< pack_mapped
&& unuse_one_packed_git())
401 fd
= open(p
->pack_name
, O_RDONLY
);
403 die("packfile %s cannot be opened", p
->pack_name
);
404 if (fstat(fd
, &st
)) {
406 die("packfile %s cannot be opened", p
->pack_name
);
408 if (st
.st_size
!= p
->pack_size
)
409 die("packfile %s size mismatch.", p
->pack_name
);
410 map
= mmap(NULL
, p
->pack_size
, PROT_READ
, MAP_PRIVATE
, fd
, 0);
412 if (map
== MAP_FAILED
)
413 die("packfile %s cannot be mapped.", p
->pack_name
);
416 /* Check if the pack file matches with the index file.
419 if (memcmp((char*)(p
->index_base
) + p
->index_size
- 40,
420 p
->pack_base
+ p
->pack_size
- 20, 20)) {
422 die("packfile %s does not match index.", p
->pack_name
);
425 p
->pack_last_used
= pack_used_ctr
++;
430 struct packed_git
*add_packed_git(char *path
, int path_len
)
433 struct packed_git
*p
;
434 unsigned long idx_size
;
437 if (check_packed_git_idx(path
, &idx_size
, &idx_map
))
440 /* do we have a corresponding .pack file? */
441 strcpy(path
+ path_len
- 4, ".pack");
442 if (stat(path
, &st
) || !S_ISREG(st
.st_mode
)) {
443 munmap(idx_map
, idx_size
);
446 /* ok, it looks sane as far as we can check without
447 * actually mapping the pack file.
449 p
= xmalloc(sizeof(*p
) + path_len
+ 2);
450 strcpy(p
->pack_name
, path
);
451 p
->index_size
= idx_size
;
452 p
->pack_size
= st
.st_size
;
453 p
->index_base
= idx_map
;
456 p
->pack_last_used
= 0;
461 struct packed_git
*parse_pack_index(unsigned char *sha1
)
463 struct packed_git
*p
;
464 unsigned long idx_size
;
466 char *path
= sha1_pack_index_name(sha1
);
468 if (check_packed_git_idx(path
, &idx_size
, &idx_map
))
471 path
= sha1_pack_name(sha1
);
473 p
= xmalloc(sizeof(*p
) + strlen(path
) + 2);
474 strcpy(p
->pack_name
, path
);
475 p
->index_size
= idx_size
;
477 p
->index_base
= idx_map
;
480 p
->pack_last_used
= 0;
482 memcpy(p
->sha1
, sha1
, 20);
486 void install_packed_git(struct packed_git
*pack
)
488 pack
->next
= packed_git
;
492 static void prepare_packed_git_one(char *objdir
)
499 sprintf(path
, "%s/pack", objdir
);
505 while ((de
= readdir(dir
)) != NULL
) {
506 int namelen
= strlen(de
->d_name
);
507 struct packed_git
*p
;
509 if (strcmp(de
->d_name
+ namelen
- 4, ".idx"))
512 /* we have .idx. Is it a file we can map? */
513 strcpy(path
+ len
, de
->d_name
);
514 p
= add_packed_git(path
, len
+ namelen
);
517 p
->next
= packed_git
;
523 void prepare_packed_git(void)
526 static int run_once
= 0;
531 prepare_packed_git_one(get_object_directory());
533 for (i
= 0; alt_odb
[i
].base
!= NULL
; i
++) {
534 alt_odb
[i
].name
[0] = 0;
535 prepare_packed_git_one(alt_odb
[i
].base
);
539 int check_sha1_signature(const unsigned char *sha1
, void *map
, unsigned long size
, const char *type
)
542 unsigned char real_sha1
[20];
546 SHA1_Update(&c
, header
, 1+sprintf(header
, "%s %lu", type
, size
));
547 SHA1_Update(&c
, map
, size
);
548 SHA1_Final(real_sha1
, &c
);
549 return memcmp(sha1
, real_sha1
, 20) ? -1 : 0;
552 static void *map_sha1_file_internal(const unsigned char *sha1
,
558 char *filename
= find_sha1_file(sha1
, &st
);
564 fd
= open(filename
, O_RDONLY
| sha1_file_open_flag
);
566 /* See if it works without O_NOATIME */
567 switch (sha1_file_open_flag
) {
569 fd
= open(filename
, O_RDONLY
);
577 /* If it failed once, it will probably fail again.
578 * Stop using O_NOATIME
580 sha1_file_open_flag
= 0;
582 map
= mmap(NULL
, st
.st_size
, PROT_READ
, MAP_PRIVATE
, fd
, 0);
584 if (map
== MAP_FAILED
)
590 int unpack_sha1_header(z_stream
*stream
, void *map
, unsigned long mapsize
, void *buffer
, unsigned long size
)
592 /* Get the data stream */
593 memset(stream
, 0, sizeof(*stream
));
594 stream
->next_in
= map
;
595 stream
->avail_in
= mapsize
;
596 stream
->next_out
= buffer
;
597 stream
->avail_out
= size
;
600 return inflate(stream
, 0);
603 static void *unpack_sha1_rest(z_stream
*stream
, void *buffer
, unsigned long size
)
605 int bytes
= strlen(buffer
) + 1;
606 unsigned char *buf
= xmalloc(1+size
);
608 memcpy(buf
, buffer
+ bytes
, stream
->total_out
- bytes
);
609 bytes
= stream
->total_out
- bytes
;
611 stream
->next_out
= buf
+ bytes
;
612 stream
->avail_out
= size
- bytes
;
613 while (inflate(stream
, Z_FINISH
) == Z_OK
)
622 * We used to just use "sscanf()", but that's actually way
623 * too permissive for what we want to check. So do an anal
624 * object header parse by hand.
626 int parse_sha1_header(char *hdr
, char *type
, unsigned long *sizep
)
632 * The type can be at most ten bytes (including the
633 * terminating '\0' that we add), and is followed by
648 * The length must follow immediately, and be in canonical
649 * decimal format (ie "010" is not valid).
656 unsigned long c
= *hdr
- '0';
660 size
= size
* 10 + c
;
666 * The length must be followed by a zero byte
668 return *hdr
? -1 : 0;
671 void * unpack_sha1_file(void *map
, unsigned long mapsize
, char *type
, unsigned long *size
)
677 ret
= unpack_sha1_header(&stream
, map
, mapsize
, hdr
, sizeof(hdr
));
678 if (ret
< Z_OK
|| parse_sha1_header(hdr
, type
, size
) < 0)
681 return unpack_sha1_rest(&stream
, hdr
, *size
);
684 /* forward declaration for a mutually recursive function */
685 static int packed_object_info(struct pack_entry
*entry
,
686 char *type
, unsigned long *sizep
);
688 static int packed_delta_info(unsigned char *base_sha1
,
689 unsigned long delta_size
,
692 unsigned long *sizep
,
693 struct packed_git
*p
)
695 struct pack_entry base_ent
;
698 die("truncated pack file");
700 /* The base entry _must_ be in the same pack */
701 if (!find_pack_entry_one(base_sha1
, &base_ent
, p
))
702 die("failed to find delta-pack base object %s",
703 sha1_to_hex(base_sha1
));
705 /* We choose to only get the type of the base object and
706 * ignore potentially corrupt pack file that expects the delta
707 * based on a base with a wrong size. This saves tons of
711 if (packed_object_info(&base_ent
, type
, NULL
))
712 die("cannot get info for delta-pack base");
715 const unsigned char *data
;
716 unsigned char delta_head
[64];
717 unsigned long result_size
;
721 memset(&stream
, 0, sizeof(stream
));
723 data
= stream
.next_in
= base_sha1
+ 20;
724 stream
.avail_in
= left
- 20;
725 stream
.next_out
= delta_head
;
726 stream
.avail_out
= sizeof(delta_head
);
728 inflateInit(&stream
);
729 st
= inflate(&stream
, Z_FINISH
);
731 if ((st
!= Z_STREAM_END
) &&
732 stream
.total_out
!= sizeof(delta_head
))
733 die("delta data unpack-initial failed");
735 /* Examine the initial part of the delta to figure out
739 get_delta_hdr_size(&data
); /* ignore base size */
741 /* Read the result size */
742 result_size
= get_delta_hdr_size(&data
);
743 *sizep
= result_size
;
748 static unsigned long unpack_object_header(struct packed_git
*p
, unsigned long offset
,
749 enum object_type
*type
, unsigned long *sizep
)
752 unsigned char *pack
, c
;
755 if (offset
>= p
->pack_size
)
756 die("object offset outside of pack file");
758 pack
= p
->pack_base
+ offset
;
761 *type
= (c
>> 4) & 7;
765 if (offset
>= p
->pack_size
)
766 die("object offset outside of pack file");
769 size
+= (c
& 0x7f) << shift
;
776 void packed_object_info_detail(struct pack_entry
*e
,
779 unsigned long *store_size
,
780 int *delta_chain_length
,
781 unsigned char *base_sha1
)
783 struct packed_git
*p
= e
->p
;
784 unsigned long offset
, left
;
786 enum object_type kind
;
788 offset
= unpack_object_header(p
, e
->offset
, &kind
, size
);
789 pack
= p
->pack_base
+ offset
;
790 left
= p
->pack_size
- offset
;
791 if (kind
!= OBJ_DELTA
)
792 *delta_chain_length
= 0;
794 int chain_length
= 0;
795 memcpy(base_sha1
, pack
, 20);
797 struct pack_entry base_ent
;
800 find_pack_entry_one(pack
, &base_ent
, p
);
801 offset
= unpack_object_header(p
, base_ent
.offset
,
803 pack
= p
->pack_base
+ offset
;
805 } while (kind
== OBJ_DELTA
);
806 *delta_chain_length
= chain_length
;
810 strcpy(type
, "commit");
813 strcpy(type
, "tree");
816 strcpy(type
, "blob");
822 die("corrupted pack file");
824 *store_size
= 0; /* notyet */
827 static int packed_object_info(struct pack_entry
*entry
,
828 char *type
, unsigned long *sizep
)
830 struct packed_git
*p
= entry
->p
;
831 unsigned long offset
, size
, left
;
833 enum object_type kind
;
836 if (use_packed_git(p
))
837 die("cannot map packed file");
839 offset
= unpack_object_header(p
, entry
->offset
, &kind
, &size
);
840 pack
= p
->pack_base
+ offset
;
841 left
= p
->pack_size
- offset
;
845 retval
= packed_delta_info(pack
, size
, left
, type
, sizep
, p
);
849 strcpy(type
, "commit");
852 strcpy(type
, "tree");
855 strcpy(type
, "blob");
861 die("corrupted pack file");
869 /* forward declaration for a mutually recursive function */
870 static void *unpack_entry(struct pack_entry
*, char *, unsigned long *);
872 static void *unpack_delta_entry(unsigned char *base_sha1
,
873 unsigned long delta_size
,
876 unsigned long *sizep
,
877 struct packed_git
*p
)
879 struct pack_entry base_ent
;
880 void *data
, *delta_data
, *result
, *base
;
881 unsigned long data_size
, result_size
, base_size
;
886 die("truncated pack file");
887 data
= base_sha1
+ 20;
888 data_size
= left
- 20;
889 delta_data
= xmalloc(delta_size
);
891 memset(&stream
, 0, sizeof(stream
));
893 stream
.next_in
= data
;
894 stream
.avail_in
= data_size
;
895 stream
.next_out
= delta_data
;
896 stream
.avail_out
= delta_size
;
898 inflateInit(&stream
);
899 st
= inflate(&stream
, Z_FINISH
);
901 if ((st
!= Z_STREAM_END
) || stream
.total_out
!= delta_size
)
902 die("delta data unpack failed");
904 /* The base entry _must_ be in the same pack */
905 if (!find_pack_entry_one(base_sha1
, &base_ent
, p
))
906 die("failed to find delta-pack base object %s",
907 sha1_to_hex(base_sha1
));
908 base
= unpack_entry_gently(&base_ent
, type
, &base_size
);
910 die("failed to read delta-pack base object %s",
911 sha1_to_hex(base_sha1
));
912 result
= patch_delta(base
, base_size
,
913 delta_data
, delta_size
,
916 die("failed to apply delta");
919 *sizep
= result_size
;
923 static void *unpack_non_delta_entry(unsigned char *data
,
929 unsigned char *buffer
;
931 buffer
= xmalloc(size
+ 1);
933 memset(&stream
, 0, sizeof(stream
));
934 stream
.next_in
= data
;
935 stream
.avail_in
= left
;
936 stream
.next_out
= buffer
;
937 stream
.avail_out
= size
;
939 inflateInit(&stream
);
940 st
= inflate(&stream
, Z_FINISH
);
942 if ((st
!= Z_STREAM_END
) || stream
.total_out
!= size
) {
950 static void *unpack_entry(struct pack_entry
*entry
,
951 char *type
, unsigned long *sizep
)
953 struct packed_git
*p
= entry
->p
;
956 if (use_packed_git(p
))
957 die("cannot map packed file");
958 retval
= unpack_entry_gently(entry
, type
, sizep
);
961 die("corrupted pack file");
965 /* The caller is responsible for use_packed_git()/unuse_packed_git() pair */
966 void *unpack_entry_gently(struct pack_entry
*entry
,
967 char *type
, unsigned long *sizep
)
969 struct packed_git
*p
= entry
->p
;
970 unsigned long offset
, size
, left
;
972 enum object_type kind
;
975 offset
= unpack_object_header(p
, entry
->offset
, &kind
, &size
);
976 pack
= p
->pack_base
+ offset
;
977 left
= p
->pack_size
- offset
;
980 retval
= unpack_delta_entry(pack
, size
, left
, type
, sizep
, p
);
983 strcpy(type
, "commit");
986 strcpy(type
, "tree");
989 strcpy(type
, "blob");
998 retval
= unpack_non_delta_entry(pack
, size
, left
);
1002 int num_packed_objects(const struct packed_git
*p
)
1004 /* See check_packed_git_idx() */
1005 return (p
->index_size
- 20 - 20 - 4*256) / 24;
1008 int nth_packed_object_sha1(const struct packed_git
*p
, int n
,
1009 unsigned char* sha1
)
1011 void *index
= p
->index_base
+ 256;
1012 if (n
< 0 || num_packed_objects(p
) <= n
)
1014 memcpy(sha1
, (index
+ 24 * n
+ 4), 20);
1018 int find_pack_entry_one(const unsigned char *sha1
,
1019 struct pack_entry
*e
, struct packed_git
*p
)
1021 unsigned int *level1_ofs
= p
->index_base
;
1022 int hi
= ntohl(level1_ofs
[*sha1
]);
1023 int lo
= ((*sha1
== 0x0) ? 0 : ntohl(level1_ofs
[*sha1
- 1]));
1024 void *index
= p
->index_base
+ 256;
1027 int mi
= (lo
+ hi
) / 2;
1028 int cmp
= memcmp(index
+ 24 * mi
+ 4, sha1
, 20);
1030 e
->offset
= ntohl(*((int*)(index
+ 24 * mi
)));
1031 memcpy(e
->sha1
, sha1
, 20);
1043 static int find_pack_entry(const unsigned char *sha1
, struct pack_entry
*e
)
1045 struct packed_git
*p
;
1046 prepare_packed_git();
1048 for (p
= packed_git
; p
; p
= p
->next
) {
1049 if (find_pack_entry_one(sha1
, e
, p
))
1055 struct packed_git
*find_sha1_pack(const unsigned char *sha1
,
1056 struct packed_git
*packs
)
1058 struct packed_git
*p
;
1059 struct pack_entry e
;
1061 for (p
= packs
; p
; p
= p
->next
) {
1062 if (find_pack_entry_one(sha1
, &e
, p
))
1069 int sha1_object_info(const unsigned char *sha1
, char *type
, unsigned long *sizep
)
1072 unsigned long mapsize
, size
;
1077 map
= map_sha1_file_internal(sha1
, &mapsize
);
1079 struct pack_entry e
;
1081 if (!find_pack_entry(sha1
, &e
))
1082 return error("unable to find %s", sha1_to_hex(sha1
));
1083 return packed_object_info(&e
, type
, sizep
);
1085 if (unpack_sha1_header(&stream
, map
, mapsize
, hdr
, sizeof(hdr
)) < 0)
1086 status
= error("unable to unpack %s header",
1088 if (parse_sha1_header(hdr
, type
, &size
) < 0)
1089 status
= error("unable to parse %s header", sha1_to_hex(sha1
));
1095 inflateEnd(&stream
);
1096 munmap(map
, mapsize
);
1100 static void *read_packed_sha1(const unsigned char *sha1
, char *type
, unsigned long *size
)
1102 struct pack_entry e
;
1104 if (!find_pack_entry(sha1
, &e
)) {
1105 error("cannot read sha1_file for %s", sha1_to_hex(sha1
));
1108 return unpack_entry(&e
, type
, size
);
1111 void * read_sha1_file(const unsigned char *sha1
, char *type
, unsigned long *size
)
1113 unsigned long mapsize
;
1115 struct pack_entry e
;
1117 if (find_pack_entry(sha1
, &e
))
1118 return read_packed_sha1(sha1
, type
, size
);
1119 map
= map_sha1_file_internal(sha1
, &mapsize
);
1121 buf
= unpack_sha1_file(map
, mapsize
, type
, size
);
1122 munmap(map
, mapsize
);
1128 void *read_object_with_reference(const unsigned char *sha1
,
1129 const char *required_type
,
1130 unsigned long *size
,
1131 unsigned char *actual_sha1_return
)
1135 unsigned long isize
;
1136 unsigned char actual_sha1
[20];
1138 memcpy(actual_sha1
, sha1
, 20);
1140 int ref_length
= -1;
1141 const char *ref_type
= NULL
;
1143 buffer
= read_sha1_file(actual_sha1
, type
, &isize
);
1146 if (!strcmp(type
, required_type
)) {
1148 if (actual_sha1_return
)
1149 memcpy(actual_sha1_return
, actual_sha1
, 20);
1152 /* Handle references */
1153 else if (!strcmp(type
, "commit"))
1155 else if (!strcmp(type
, "tag"))
1156 ref_type
= "object ";
1161 ref_length
= strlen(ref_type
);
1163 if (memcmp(buffer
, ref_type
, ref_length
) ||
1164 get_sha1_hex(buffer
+ ref_length
, actual_sha1
)) {
1169 /* Now we have the ID of the referred-to object in
1170 * actual_sha1. Check again. */
1174 char *write_sha1_file_prepare(void *buf
,
1177 unsigned char *sha1
,
1183 /* Generate the header */
1184 *hdrlen
= sprintf((char *)hdr
, "%s %lu", type
, len
)+1;
1188 SHA1_Update(&c
, hdr
, *hdrlen
);
1189 SHA1_Update(&c
, buf
, len
);
1190 SHA1_Final(sha1
, &c
);
1192 return sha1_file_name(sha1
);
1195 int write_sha1_file(void *buf
, unsigned long len
, const char *type
, unsigned char *returnsha1
)
1198 unsigned char *compressed
;
1200 unsigned char sha1
[20];
1202 static char tmpfile
[PATH_MAX
];
1203 unsigned char hdr
[50];
1204 int fd
, hdrlen
, ret
;
1206 /* Normally if we have it in the pack then we do not bother writing
1207 * it out into .git/objects/??/?{38} file.
1209 filename
= write_sha1_file_prepare(buf
, len
, type
, sha1
, hdr
, &hdrlen
);
1211 memcpy(returnsha1
, sha1
, 20);
1212 if (has_sha1_file(sha1
))
1214 fd
= open(filename
, O_RDONLY
);
1217 * FIXME!!! We might do collision checking here, but we'd
1218 * need to uncompress the old file and check it. Later.
1224 if (errno
!= ENOENT
) {
1225 fprintf(stderr
, "sha1 file %s: %s", filename
, strerror(errno
));
1229 snprintf(tmpfile
, sizeof(tmpfile
), "%s/obj_XXXXXX", get_object_directory());
1231 fd
= mkstemp(tmpfile
);
1233 fprintf(stderr
, "unable to create temporary sha1 filename %s: %s", tmpfile
, strerror(errno
));
1238 memset(&stream
, 0, sizeof(stream
));
1239 deflateInit(&stream
, Z_BEST_COMPRESSION
);
1240 size
= deflateBound(&stream
, len
+hdrlen
);
1241 compressed
= xmalloc(size
);
1244 stream
.next_out
= compressed
;
1245 stream
.avail_out
= size
;
1247 /* First header.. */
1248 stream
.next_in
= hdr
;
1249 stream
.avail_in
= hdrlen
;
1250 while (deflate(&stream
, 0) == Z_OK
)
1253 /* Then the data itself.. */
1254 stream
.next_in
= buf
;
1255 stream
.avail_in
= len
;
1256 while (deflate(&stream
, Z_FINISH
) == Z_OK
)
1258 deflateEnd(&stream
);
1259 size
= stream
.total_out
;
1261 if (write(fd
, compressed
, size
) != size
)
1262 die("unable to write file");
1267 ret
= link(tmpfile
, filename
);
1272 * Coda hack - coda doesn't like cross-directory links,
1273 * so we fall back to a rename, which will mean that it
1274 * won't be able to check collisions, but that's not a
1277 * When this succeeds, we just return 0. We have nothing
1280 if (ret
== EXDEV
&& !rename(tmpfile
, filename
))
1285 if (ret
!= EEXIST
) {
1286 fprintf(stderr
, "unable to write sha1 filename %s: %s", filename
, strerror(ret
));
1289 /* FIXME!!! Collision check here ? */
1295 int write_sha1_to_fd(int fd
, const unsigned char *sha1
)
1298 unsigned long objsize
;
1300 void *map
= map_sha1_file_internal(sha1
, &objsize
);
1302 void *temp_obj
= NULL
;
1306 unsigned char *unpacked
;
1311 // need to unpack and recompress it by itself
1312 unpacked
= read_packed_sha1(sha1
, type
, &len
);
1314 hdrlen
= sprintf(hdr
, "%s %lu", type
, len
) + 1;
1317 memset(&stream
, 0, sizeof(stream
));
1318 deflateInit(&stream
, Z_BEST_COMPRESSION
);
1319 size
= deflateBound(&stream
, len
+ hdrlen
);
1320 temp_obj
= buf
= xmalloc(size
);
1323 stream
.next_out
= buf
;
1324 stream
.avail_out
= size
;
1326 /* First header.. */
1327 stream
.next_in
= (void *)hdr
;
1328 stream
.avail_in
= hdrlen
;
1329 while (deflate(&stream
, 0) == Z_OK
)
1332 /* Then the data itself.. */
1333 stream
.next_in
= unpacked
;
1334 stream
.avail_in
= len
;
1335 while (deflate(&stream
, Z_FINISH
) == Z_OK
)
1337 deflateEnd(&stream
);
1340 objsize
= stream
.total_out
;
1344 size
= write(fd
, buf
+ posn
, objsize
- posn
);
1347 fprintf(stderr
, "write closed");
1354 } while (posn
< objsize
);
1357 munmap(map
, objsize
);
1364 int write_sha1_from_fd(const unsigned char *sha1
, int fd
, char *buffer
,
1365 size_t bufsize
, size_t *bufposn
)
1367 char *filename
= sha1_file_name(sha1
);
1371 unsigned char real_sha1
[20];
1372 unsigned char discard
[4096];
1376 local
= open(filename
, O_WRONLY
| O_CREAT
| O_EXCL
, 0666);
1379 return error("Couldn't open %s\n", filename
);
1381 memset(&stream
, 0, sizeof(stream
));
1383 inflateInit(&stream
);
1390 stream
.avail_in
= *bufposn
;
1391 stream
.next_in
= (unsigned char *) buffer
;
1393 stream
.next_out
= discard
;
1394 stream
.avail_out
= sizeof(discard
);
1395 ret
= inflate(&stream
, Z_SYNC_FLUSH
);
1396 SHA1_Update(&c
, discard
, sizeof(discard
) -
1398 } while (stream
.avail_in
&& ret
== Z_OK
);
1399 write(local
, buffer
, *bufposn
- stream
.avail_in
);
1400 memmove(buffer
, buffer
+ *bufposn
- stream
.avail_in
,
1402 *bufposn
= stream
.avail_in
;
1406 size
= read(fd
, buffer
+ *bufposn
, bufsize
- *bufposn
);
1411 return error("Connection closed?");
1412 perror("Reading from connection");
1417 inflateEnd(&stream
);
1420 SHA1_Final(real_sha1
, &c
);
1421 if (ret
!= Z_STREAM_END
) {
1423 return error("File %s corrupted", sha1_to_hex(sha1
));
1425 if (memcmp(sha1
, real_sha1
, 20)) {
1427 return error("File %s has bad hash\n", sha1_to_hex(sha1
));
1433 int has_pack_index(const unsigned char *sha1
)
1436 if (stat(sha1_pack_index_name(sha1
), &st
))
1441 int has_pack_file(const unsigned char *sha1
)
1444 if (stat(sha1_pack_name(sha1
), &st
))
1449 int has_sha1_pack(const unsigned char *sha1
)
1451 struct pack_entry e
;
1452 return find_pack_entry(sha1
, &e
);
1455 int has_sha1_file(const unsigned char *sha1
)
1458 struct pack_entry e
;
1460 if (find_pack_entry(sha1
, &e
))
1462 return find_sha1_file(sha1
, &st
) ? 1 : 0;
1465 int index_fd(unsigned char *sha1
, int fd
, struct stat
*st
, int write_object
, const char *type
)
1467 unsigned long size
= st
->st_size
;
1470 unsigned char hdr
[50];
1475 buf
= mmap(NULL
, size
, PROT_READ
, MAP_PRIVATE
, fd
, 0);
1477 if (buf
== MAP_FAILED
)
1483 ret
= write_sha1_file(buf
, size
, type
, sha1
);
1485 write_sha1_file_prepare(buf
, size
, type
, sha1
, hdr
, &hdrlen
);