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 const unsigned char null_sha1
[20] = { 0, };
25 static unsigned int sha1_file_open_flag
= O_NOATIME
;
27 static unsigned hexval(char c
)
29 if (c
>= '0' && c
<= '9')
31 if (c
>= 'a' && c
<= 'f')
33 if (c
>= 'A' && c
<= 'F')
38 int get_sha1_hex(const char *hex
, unsigned char *sha1
)
41 for (i
= 0; i
< 20; i
++) {
42 unsigned int val
= (hexval(hex
[0]) << 4) | hexval(hex
[1]);
51 int safe_create_leading_directories(char *path
)
58 pos
= strchr(pos
, '/');
62 if (mkdir(path
, 0777) < 0)
63 if (errno
!= EEXIST
) {
72 char * sha1_to_hex(const unsigned char *sha1
)
74 static char buffer
[50];
75 static const char hex
[] = "0123456789abcdef";
79 for (i
= 0; i
< 20; i
++) {
80 unsigned int val
= *sha1
++;
81 *buf
++ = hex
[val
>> 4];
82 *buf
++ = hex
[val
& 0xf];
87 static void fill_sha1_path(char *pathbuf
, const unsigned char *sha1
)
90 for (i
= 0; i
< 20; i
++) {
91 static char hex
[] = "0123456789abcdef";
92 unsigned int val
= sha1
[i
];
93 char *pos
= pathbuf
+ i
*2 + (i
> 0);
94 *pos
++ = hex
[val
>> 4];
95 *pos
= hex
[val
& 0xf];
100 * NOTE! This returns a statically allocated buffer, so you have to be
101 * careful about using it. Do a "strdup()" if you need to save the
104 * Also note that this returns the location for creating. Reading
105 * SHA1 file can happen from any alternate directory listed in the
106 * DB_ENVIRONMENT environment variable if it is not found in
107 * the primary object database.
109 char *sha1_file_name(const unsigned char *sha1
)
111 static char *name
, *base
;
114 const char *sha1_file_directory
= get_object_directory();
115 int len
= strlen(sha1_file_directory
);
116 base
= xmalloc(len
+ 60);
117 memcpy(base
, sha1_file_directory
, len
);
118 memset(base
+len
, 0, 60);
121 name
= base
+ len
+ 1;
123 fill_sha1_path(name
, sha1
);
127 char *sha1_pack_name(const unsigned char *sha1
)
129 static const char hex
[] = "0123456789abcdef";
130 static char *name
, *base
, *buf
;
134 const char *sha1_file_directory
= get_object_directory();
135 int len
= strlen(sha1_file_directory
);
136 base
= xmalloc(len
+ 60);
137 sprintf(base
, "%s/pack/pack-1234567890123456789012345678901234567890.pack", sha1_file_directory
);
138 name
= base
+ len
+ 11;
143 for (i
= 0; i
< 20; i
++) {
144 unsigned int val
= *sha1
++;
145 *buf
++ = hex
[val
>> 4];
146 *buf
++ = hex
[val
& 0xf];
152 char *sha1_pack_index_name(const unsigned char *sha1
)
154 static const char hex
[] = "0123456789abcdef";
155 static char *name
, *base
, *buf
;
159 const char *sha1_file_directory
= get_object_directory();
160 int len
= strlen(sha1_file_directory
);
161 base
= xmalloc(len
+ 60);
162 sprintf(base
, "%s/pack/pack-1234567890123456789012345678901234567890.idx", sha1_file_directory
);
163 name
= base
+ len
+ 11;
168 for (i
= 0; i
< 20; i
++) {
169 unsigned int val
= *sha1
++;
170 *buf
++ = hex
[val
>> 4];
171 *buf
++ = hex
[val
& 0xf];
177 struct alternate_object_database
*alt_odb_list
;
178 static struct alternate_object_database
**alt_odb_tail
;
181 * Prepare alternate object database registry.
183 * The variable alt_odb_list points at the list of struct
184 * alternate_object_database. The elements on this list come from
185 * non-empty elements from colon separated ALTERNATE_DB_ENVIRONMENT
186 * environment variable, and $GIT_OBJECT_DIRECTORY/info/alternates,
187 * whose contents is similar to that environment variable but can be
188 * LF separated. Its base points at a statically allocated buffer that
189 * contains "/the/directory/corresponding/to/.git/objects/...", while
190 * its name points just after the slash at the end of ".git/objects/"
191 * in the example above, and has enough space to hold 40-byte hex
192 * SHA1, an extra slash for the first level indirection, and the
195 static void link_alt_odb_entries(const char *alt
, const char *ep
, int sep
,
196 const char *relative_base
)
198 const char *cp
, *last
;
199 struct alternate_object_database
*ent
;
200 const char *objdir
= get_object_directory();
206 if (cp
< ep
&& *cp
== '#') {
207 while (cp
< ep
&& *cp
!= sep
)
212 for ( ; cp
< ep
&& *cp
!= sep
; cp
++)
215 struct alternate_object_database
*alt
;
216 /* 43 = 40-byte + 2 '/' + terminating NUL */
217 int pfxlen
= cp
- last
;
218 int entlen
= pfxlen
+ 43;
220 if (*last
!= '/' && relative_base
) {
221 /* Relative alt-odb */
223 base_len
= strlen(relative_base
) + 1;
227 ent
= xmalloc(sizeof(*ent
) + entlen
);
229 if (*last
!= '/' && relative_base
) {
230 memcpy(ent
->base
, relative_base
, base_len
- 1);
231 ent
->base
[base_len
- 1] = '/';
232 memcpy(ent
->base
+ base_len
,
236 memcpy(ent
->base
, last
, pfxlen
);
237 ent
->name
= ent
->base
+ pfxlen
+ 1;
238 ent
->base
[pfxlen
] = ent
->base
[pfxlen
+ 3] = '/';
239 ent
->base
[entlen
-1] = 0;
241 /* Prevent the common mistake of listing the same
242 * thing twice, or object directory itself.
244 for (alt
= alt_odb_list
; alt
; alt
= alt
->next
)
245 if (!memcmp(ent
->base
, alt
->base
, pfxlen
))
247 if (!memcmp(ent
->base
, objdir
, pfxlen
)) {
253 alt_odb_tail
= &(ent
->next
);
257 while (cp
< ep
&& *cp
== sep
)
263 void prepare_alt_odb(void)
271 alt
= getenv(ALTERNATE_DB_ENVIRONMENT
);
276 alt_odb_tail
= &alt_odb_list
;
277 link_alt_odb_entries(alt
, alt
+ strlen(alt
), ':', NULL
);
279 sprintf(path
, "%s/info/alternates", get_object_directory());
280 fd
= open(path
, O_RDONLY
);
283 if (fstat(fd
, &st
) || (st
.st_size
== 0)) {
287 map
= mmap(NULL
, st
.st_size
, PROT_READ
, MAP_PRIVATE
, fd
, 0);
289 if (map
== MAP_FAILED
)
292 link_alt_odb_entries(map
, map
+ st
.st_size
, '\n',
293 get_object_directory());
294 munmap(map
, st
.st_size
);
297 static char *find_sha1_file(const unsigned char *sha1
, struct stat
*st
)
299 char *name
= sha1_file_name(sha1
);
300 struct alternate_object_database
*alt
;
305 for (alt
= alt_odb_list
; alt
; alt
= alt
->next
) {
307 fill_sha1_path(name
, sha1
);
308 if (!stat(alt
->base
, st
))
314 #define PACK_MAX_SZ (1<<26)
315 static int pack_used_ctr
;
316 static unsigned long pack_mapped
;
317 struct packed_git
*packed_git
;
319 static int check_packed_git_idx(const char *path
, unsigned long *idx_size_
,
324 unsigned long idx_size
;
326 int fd
= open(path
, O_RDONLY
);
330 if (fstat(fd
, &st
)) {
334 idx_size
= st
.st_size
;
335 idx_map
= mmap(NULL
, idx_size
, PROT_READ
, MAP_PRIVATE
, fd
, 0);
337 if (idx_map
== MAP_FAILED
)
342 *idx_size_
= idx_size
;
344 /* check index map */
345 if (idx_size
< 4*256 + 20 + 20)
346 return error("index file too small");
348 for (i
= 0; i
< 256; i
++) {
349 unsigned int n
= ntohl(index
[i
]);
351 return error("non-monotonic index");
357 * - 256 index entries 4 bytes each
358 * - 24-byte entries * nr (20-byte sha1 + 4-byte offset)
359 * - 20-byte SHA1 of the packfile
360 * - 20-byte SHA1 file checksum
362 if (idx_size
!= 4*256 + nr
* 24 + 20 + 20)
363 return error("wrong index file size");
368 static int unuse_one_packed_git(void)
370 struct packed_git
*p
, *lru
= NULL
;
372 for (p
= packed_git
; p
; p
= p
->next
) {
373 if (p
->pack_use_cnt
|| !p
->pack_base
)
375 if (!lru
|| p
->pack_last_used
< lru
->pack_last_used
)
380 munmap(lru
->pack_base
, lru
->pack_size
);
381 lru
->pack_base
= NULL
;
385 void unuse_packed_git(struct packed_git
*p
)
390 int use_packed_git(struct packed_git
*p
)
394 // We created the struct before we had the pack
395 stat(p
->pack_name
, &st
);
396 if (!S_ISREG(st
.st_mode
))
397 die("packfile %s not a regular file", p
->pack_name
);
398 p
->pack_size
= st
.st_size
;
405 pack_mapped
+= p
->pack_size
;
406 while (PACK_MAX_SZ
< pack_mapped
&& unuse_one_packed_git())
408 fd
= open(p
->pack_name
, O_RDONLY
);
410 die("packfile %s cannot be opened", p
->pack_name
);
411 if (fstat(fd
, &st
)) {
413 die("packfile %s cannot be opened", p
->pack_name
);
415 if (st
.st_size
!= p
->pack_size
)
416 die("packfile %s size mismatch.", p
->pack_name
);
417 map
= mmap(NULL
, p
->pack_size
, PROT_READ
, MAP_PRIVATE
, fd
, 0);
419 if (map
== MAP_FAILED
)
420 die("packfile %s cannot be mapped.", p
->pack_name
);
423 /* Check if the pack file matches with the index file.
426 if (memcmp((char*)(p
->index_base
) + p
->index_size
- 40,
427 p
->pack_base
+ p
->pack_size
- 20, 20)) {
429 die("packfile %s does not match index.", p
->pack_name
);
432 p
->pack_last_used
= pack_used_ctr
++;
437 struct packed_git
*add_packed_git(char *path
, int path_len
, int local
)
440 struct packed_git
*p
;
441 unsigned long idx_size
;
443 unsigned char sha1
[20];
445 if (check_packed_git_idx(path
, &idx_size
, &idx_map
))
448 /* do we have a corresponding .pack file? */
449 strcpy(path
+ path_len
- 4, ".pack");
450 if (stat(path
, &st
) || !S_ISREG(st
.st_mode
)) {
451 munmap(idx_map
, idx_size
);
454 /* ok, it looks sane as far as we can check without
455 * actually mapping the pack file.
457 p
= xmalloc(sizeof(*p
) + path_len
+ 2);
458 strcpy(p
->pack_name
, path
);
459 p
->index_size
= idx_size
;
460 p
->pack_size
= st
.st_size
;
461 p
->index_base
= idx_map
;
464 p
->pack_last_used
= 0;
466 p
->pack_local
= local
;
467 if (!get_sha1_hex(path
+ path_len
- 40 - 4, sha1
))
468 memcpy(p
->sha1
, sha1
, 20);
472 struct packed_git
*parse_pack_index(unsigned char *sha1
)
474 char *path
= sha1_pack_index_name(sha1
);
475 return parse_pack_index_file(sha1
, path
);
478 struct packed_git
*parse_pack_index_file(const unsigned char *sha1
, char *idx_path
)
480 struct packed_git
*p
;
481 unsigned long idx_size
;
485 if (check_packed_git_idx(idx_path
, &idx_size
, &idx_map
))
488 path
= sha1_pack_name(sha1
);
490 p
= xmalloc(sizeof(*p
) + strlen(path
) + 2);
491 strcpy(p
->pack_name
, path
);
492 p
->index_size
= idx_size
;
494 p
->index_base
= idx_map
;
497 p
->pack_last_used
= 0;
499 memcpy(p
->sha1
, sha1
, 20);
503 void install_packed_git(struct packed_git
*pack
)
505 pack
->next
= packed_git
;
509 static void prepare_packed_git_one(char *objdir
, int local
)
516 sprintf(path
, "%s/pack", objdir
);
522 while ((de
= readdir(dir
)) != NULL
) {
523 int namelen
= strlen(de
->d_name
);
524 struct packed_git
*p
;
526 if (strcmp(de
->d_name
+ namelen
- 4, ".idx"))
529 /* we have .idx. Is it a file we can map? */
530 strcpy(path
+ len
, de
->d_name
);
531 p
= add_packed_git(path
, len
+ namelen
, local
);
534 p
->next
= packed_git
;
540 void prepare_packed_git(void)
542 static int run_once
= 0;
543 struct alternate_object_database
*alt
;
547 prepare_packed_git_one(get_object_directory(), 1);
549 for (alt
= alt_odb_list
; alt
; alt
= alt
->next
) {
551 prepare_packed_git_one(alt
->base
, 0);
557 int check_sha1_signature(const unsigned char *sha1
, void *map
, unsigned long size
, const char *type
)
560 unsigned char real_sha1
[20];
564 SHA1_Update(&c
, header
, 1+sprintf(header
, "%s %lu", type
, size
));
565 SHA1_Update(&c
, map
, size
);
566 SHA1_Final(real_sha1
, &c
);
567 return memcmp(sha1
, real_sha1
, 20) ? -1 : 0;
570 static void *map_sha1_file_internal(const unsigned char *sha1
,
576 char *filename
= find_sha1_file(sha1
, &st
);
582 fd
= open(filename
, O_RDONLY
| sha1_file_open_flag
);
584 /* See if it works without O_NOATIME */
585 switch (sha1_file_open_flag
) {
587 fd
= open(filename
, O_RDONLY
);
595 /* If it failed once, it will probably fail again.
596 * Stop using O_NOATIME
598 sha1_file_open_flag
= 0;
600 map
= mmap(NULL
, st
.st_size
, PROT_READ
, MAP_PRIVATE
, fd
, 0);
602 if (map
== MAP_FAILED
)
608 int unpack_sha1_header(z_stream
*stream
, void *map
, unsigned long mapsize
, void *buffer
, unsigned long size
)
610 /* Get the data stream */
611 memset(stream
, 0, sizeof(*stream
));
612 stream
->next_in
= map
;
613 stream
->avail_in
= mapsize
;
614 stream
->next_out
= buffer
;
615 stream
->avail_out
= size
;
618 return inflate(stream
, 0);
621 static void *unpack_sha1_rest(z_stream
*stream
, void *buffer
, unsigned long size
)
623 int bytes
= strlen(buffer
) + 1;
624 unsigned char *buf
= xmalloc(1+size
);
626 memcpy(buf
, buffer
+ bytes
, stream
->total_out
- bytes
);
627 bytes
= stream
->total_out
- bytes
;
629 stream
->next_out
= buf
+ bytes
;
630 stream
->avail_out
= size
- bytes
;
631 while (inflate(stream
, Z_FINISH
) == Z_OK
)
640 * We used to just use "sscanf()", but that's actually way
641 * too permissive for what we want to check. So do an anal
642 * object header parse by hand.
644 int parse_sha1_header(char *hdr
, char *type
, unsigned long *sizep
)
650 * The type can be at most ten bytes (including the
651 * terminating '\0' that we add), and is followed by
666 * The length must follow immediately, and be in canonical
667 * decimal format (ie "010" is not valid).
674 unsigned long c
= *hdr
- '0';
678 size
= size
* 10 + c
;
684 * The length must be followed by a zero byte
686 return *hdr
? -1 : 0;
689 void * unpack_sha1_file(void *map
, unsigned long mapsize
, char *type
, unsigned long *size
)
695 ret
= unpack_sha1_header(&stream
, map
, mapsize
, hdr
, sizeof(hdr
));
696 if (ret
< Z_OK
|| parse_sha1_header(hdr
, type
, size
) < 0)
699 return unpack_sha1_rest(&stream
, hdr
, *size
);
702 /* forward declaration for a mutually recursive function */
703 static int packed_object_info(struct pack_entry
*entry
,
704 char *type
, unsigned long *sizep
);
706 static int packed_delta_info(unsigned char *base_sha1
,
707 unsigned long delta_size
,
710 unsigned long *sizep
,
711 struct packed_git
*p
)
713 struct pack_entry base_ent
;
716 die("truncated pack file");
718 /* The base entry _must_ be in the same pack */
719 if (!find_pack_entry_one(base_sha1
, &base_ent
, p
))
720 die("failed to find delta-pack base object %s",
721 sha1_to_hex(base_sha1
));
723 /* We choose to only get the type of the base object and
724 * ignore potentially corrupt pack file that expects the delta
725 * based on a base with a wrong size. This saves tons of
729 if (packed_object_info(&base_ent
, type
, NULL
))
730 die("cannot get info for delta-pack base");
733 const unsigned char *data
;
734 unsigned char delta_head
[64];
735 unsigned long result_size
;
739 memset(&stream
, 0, sizeof(stream
));
741 data
= stream
.next_in
= base_sha1
+ 20;
742 stream
.avail_in
= left
- 20;
743 stream
.next_out
= delta_head
;
744 stream
.avail_out
= sizeof(delta_head
);
746 inflateInit(&stream
);
747 st
= inflate(&stream
, Z_FINISH
);
749 if ((st
!= Z_STREAM_END
) &&
750 stream
.total_out
!= sizeof(delta_head
))
751 die("delta data unpack-initial failed");
753 /* Examine the initial part of the delta to figure out
757 get_delta_hdr_size(&data
); /* ignore base size */
759 /* Read the result size */
760 result_size
= get_delta_hdr_size(&data
);
761 *sizep
= result_size
;
766 static unsigned long unpack_object_header(struct packed_git
*p
, unsigned long offset
,
767 enum object_type
*type
, unsigned long *sizep
)
770 unsigned char *pack
, c
;
773 if (offset
>= p
->pack_size
)
774 die("object offset outside of pack file");
776 pack
= p
->pack_base
+ offset
;
779 *type
= (c
>> 4) & 7;
783 if (offset
>= p
->pack_size
)
784 die("object offset outside of pack file");
787 size
+= (c
& 0x7f) << shift
;
794 void packed_object_info_detail(struct pack_entry
*e
,
797 unsigned long *store_size
,
798 int *delta_chain_length
,
799 unsigned char *base_sha1
)
801 struct packed_git
*p
= e
->p
;
802 unsigned long offset
, left
;
804 enum object_type kind
;
806 offset
= unpack_object_header(p
, e
->offset
, &kind
, size
);
807 pack
= p
->pack_base
+ offset
;
808 left
= p
->pack_size
- offset
;
809 if (kind
!= OBJ_DELTA
)
810 *delta_chain_length
= 0;
812 int chain_length
= 0;
813 memcpy(base_sha1
, pack
, 20);
815 struct pack_entry base_ent
;
818 find_pack_entry_one(pack
, &base_ent
, p
);
819 offset
= unpack_object_header(p
, base_ent
.offset
,
821 pack
= p
->pack_base
+ offset
;
823 } while (kind
== OBJ_DELTA
);
824 *delta_chain_length
= chain_length
;
828 strcpy(type
, "commit");
831 strcpy(type
, "tree");
834 strcpy(type
, "blob");
840 die("corrupted pack file %s containing object of kind %d",
843 *store_size
= 0; /* notyet */
846 static int packed_object_info(struct pack_entry
*entry
,
847 char *type
, unsigned long *sizep
)
849 struct packed_git
*p
= entry
->p
;
850 unsigned long offset
, size
, left
;
852 enum object_type kind
;
855 if (use_packed_git(p
))
856 die("cannot map packed file");
858 offset
= unpack_object_header(p
, entry
->offset
, &kind
, &size
);
859 pack
= p
->pack_base
+ offset
;
860 left
= p
->pack_size
- offset
;
864 retval
= packed_delta_info(pack
, size
, left
, type
, sizep
, p
);
868 strcpy(type
, "commit");
871 strcpy(type
, "tree");
874 strcpy(type
, "blob");
880 die("corrupted pack file %s containing object of kind %d",
889 /* forward declaration for a mutually recursive function */
890 static void *unpack_entry(struct pack_entry
*, char *, unsigned long *);
892 static void *unpack_delta_entry(unsigned char *base_sha1
,
893 unsigned long delta_size
,
896 unsigned long *sizep
,
897 struct packed_git
*p
)
899 struct pack_entry base_ent
;
900 void *data
, *delta_data
, *result
, *base
;
901 unsigned long data_size
, result_size
, base_size
;
906 die("truncated pack file");
907 data
= base_sha1
+ 20;
908 data_size
= left
- 20;
909 delta_data
= xmalloc(delta_size
);
911 memset(&stream
, 0, sizeof(stream
));
913 stream
.next_in
= data
;
914 stream
.avail_in
= data_size
;
915 stream
.next_out
= delta_data
;
916 stream
.avail_out
= delta_size
;
918 inflateInit(&stream
);
919 st
= inflate(&stream
, Z_FINISH
);
921 if ((st
!= Z_STREAM_END
) || stream
.total_out
!= delta_size
)
922 die("delta data unpack failed");
924 /* The base entry _must_ be in the same pack */
925 if (!find_pack_entry_one(base_sha1
, &base_ent
, p
))
926 die("failed to find delta-pack base object %s",
927 sha1_to_hex(base_sha1
));
928 base
= unpack_entry_gently(&base_ent
, type
, &base_size
);
930 die("failed to read delta-pack base object %s",
931 sha1_to_hex(base_sha1
));
932 result
= patch_delta(base
, base_size
,
933 delta_data
, delta_size
,
936 die("failed to apply delta");
939 *sizep
= result_size
;
943 static void *unpack_non_delta_entry(unsigned char *data
,
949 unsigned char *buffer
;
951 buffer
= xmalloc(size
+ 1);
953 memset(&stream
, 0, sizeof(stream
));
954 stream
.next_in
= data
;
955 stream
.avail_in
= left
;
956 stream
.next_out
= buffer
;
957 stream
.avail_out
= size
;
959 inflateInit(&stream
);
960 st
= inflate(&stream
, Z_FINISH
);
962 if ((st
!= Z_STREAM_END
) || stream
.total_out
!= size
) {
970 static void *unpack_entry(struct pack_entry
*entry
,
971 char *type
, unsigned long *sizep
)
973 struct packed_git
*p
= entry
->p
;
976 if (use_packed_git(p
))
977 die("cannot map packed file");
978 retval
= unpack_entry_gently(entry
, type
, sizep
);
981 die("corrupted pack file %s", p
->pack_name
);
985 /* The caller is responsible for use_packed_git()/unuse_packed_git() pair */
986 void *unpack_entry_gently(struct pack_entry
*entry
,
987 char *type
, unsigned long *sizep
)
989 struct packed_git
*p
= entry
->p
;
990 unsigned long offset
, size
, left
;
992 enum object_type kind
;
995 offset
= unpack_object_header(p
, entry
->offset
, &kind
, &size
);
996 pack
= p
->pack_base
+ offset
;
997 left
= p
->pack_size
- offset
;
1000 retval
= unpack_delta_entry(pack
, size
, left
, type
, sizep
, p
);
1003 strcpy(type
, "commit");
1006 strcpy(type
, "tree");
1009 strcpy(type
, "blob");
1012 strcpy(type
, "tag");
1018 retval
= unpack_non_delta_entry(pack
, size
, left
);
1022 int num_packed_objects(const struct packed_git
*p
)
1024 /* See check_packed_git_idx() */
1025 return (p
->index_size
- 20 - 20 - 4*256) / 24;
1028 int nth_packed_object_sha1(const struct packed_git
*p
, int n
,
1029 unsigned char* sha1
)
1031 void *index
= p
->index_base
+ 256;
1032 if (n
< 0 || num_packed_objects(p
) <= n
)
1034 memcpy(sha1
, (index
+ 24 * n
+ 4), 20);
1038 int find_pack_entry_one(const unsigned char *sha1
,
1039 struct pack_entry
*e
, struct packed_git
*p
)
1041 unsigned int *level1_ofs
= p
->index_base
;
1042 int hi
= ntohl(level1_ofs
[*sha1
]);
1043 int lo
= ((*sha1
== 0x0) ? 0 : ntohl(level1_ofs
[*sha1
- 1]));
1044 void *index
= p
->index_base
+ 256;
1047 int mi
= (lo
+ hi
) / 2;
1048 int cmp
= memcmp(index
+ 24 * mi
+ 4, sha1
, 20);
1050 e
->offset
= ntohl(*((int*)(index
+ 24 * mi
)));
1051 memcpy(e
->sha1
, sha1
, 20);
1063 static int find_pack_entry(const unsigned char *sha1
, struct pack_entry
*e
)
1065 struct packed_git
*p
;
1066 prepare_packed_git();
1068 for (p
= packed_git
; p
; p
= p
->next
) {
1069 if (find_pack_entry_one(sha1
, e
, p
))
1075 struct packed_git
*find_sha1_pack(const unsigned char *sha1
,
1076 struct packed_git
*packs
)
1078 struct packed_git
*p
;
1079 struct pack_entry e
;
1081 for (p
= packs
; p
; p
= p
->next
) {
1082 if (find_pack_entry_one(sha1
, &e
, p
))
1089 int sha1_object_info(const unsigned char *sha1
, char *type
, unsigned long *sizep
)
1092 unsigned long mapsize
, size
;
1097 map
= map_sha1_file_internal(sha1
, &mapsize
);
1099 struct pack_entry e
;
1101 if (!find_pack_entry(sha1
, &e
))
1102 return error("unable to find %s", sha1_to_hex(sha1
));
1103 return packed_object_info(&e
, type
, sizep
);
1105 if (unpack_sha1_header(&stream
, map
, mapsize
, hdr
, sizeof(hdr
)) < 0)
1106 status
= error("unable to unpack %s header",
1108 if (parse_sha1_header(hdr
, type
, &size
) < 0)
1109 status
= error("unable to parse %s header", sha1_to_hex(sha1
));
1115 inflateEnd(&stream
);
1116 munmap(map
, mapsize
);
1120 static void *read_packed_sha1(const unsigned char *sha1
, char *type
, unsigned long *size
)
1122 struct pack_entry e
;
1124 if (!find_pack_entry(sha1
, &e
)) {
1125 error("cannot read sha1_file for %s", sha1_to_hex(sha1
));
1128 return unpack_entry(&e
, type
, size
);
1131 void * read_sha1_file(const unsigned char *sha1
, char *type
, unsigned long *size
)
1133 unsigned long mapsize
;
1135 struct pack_entry e
;
1137 if (find_pack_entry(sha1
, &e
))
1138 return read_packed_sha1(sha1
, type
, size
);
1139 map
= map_sha1_file_internal(sha1
, &mapsize
);
1141 buf
= unpack_sha1_file(map
, mapsize
, type
, size
);
1142 munmap(map
, mapsize
);
1148 void *read_object_with_reference(const unsigned char *sha1
,
1149 const char *required_type
,
1150 unsigned long *size
,
1151 unsigned char *actual_sha1_return
)
1155 unsigned long isize
;
1156 unsigned char actual_sha1
[20];
1158 memcpy(actual_sha1
, sha1
, 20);
1160 int ref_length
= -1;
1161 const char *ref_type
= NULL
;
1163 buffer
= read_sha1_file(actual_sha1
, type
, &isize
);
1166 if (!strcmp(type
, required_type
)) {
1168 if (actual_sha1_return
)
1169 memcpy(actual_sha1_return
, actual_sha1
, 20);
1172 /* Handle references */
1173 else if (!strcmp(type
, "commit"))
1175 else if (!strcmp(type
, "tag"))
1176 ref_type
= "object ";
1181 ref_length
= strlen(ref_type
);
1183 if (memcmp(buffer
, ref_type
, ref_length
) ||
1184 get_sha1_hex(buffer
+ ref_length
, actual_sha1
)) {
1189 /* Now we have the ID of the referred-to object in
1190 * actual_sha1. Check again. */
1194 char *write_sha1_file_prepare(void *buf
,
1197 unsigned char *sha1
,
1203 /* Generate the header */
1204 *hdrlen
= sprintf((char *)hdr
, "%s %lu", type
, len
)+1;
1208 SHA1_Update(&c
, hdr
, *hdrlen
);
1209 SHA1_Update(&c
, buf
, len
);
1210 SHA1_Final(sha1
, &c
);
1212 return sha1_file_name(sha1
);
1216 * Link the tempfile to the final place, possibly creating the
1217 * last directory level as you do so.
1219 * Returns the errno on failure, 0 on success.
1221 static int link_temp_to_file(const char *tmpfile
, char *filename
)
1225 if (!link(tmpfile
, filename
))
1229 * Try to mkdir the last path component if that failed
1232 * Re-try the "link()" regardless of whether the mkdir
1233 * succeeds, since a race might mean that somebody
1237 if (ret
== ENOENT
) {
1238 char *dir
= strrchr(filename
, '/');
1241 mkdir(filename
, 0777);
1243 if (!link(tmpfile
, filename
))
1252 * Move the just written object into its final resting place
1254 int move_temp_to_file(const char *tmpfile
, char *filename
)
1256 int ret
= link_temp_to_file(tmpfile
, filename
);
1259 * Coda hack - coda doesn't like cross-directory links,
1260 * so we fall back to a rename, which will mean that it
1261 * won't be able to check collisions, but that's not a
1264 * The same holds for FAT formatted media.
1266 * When this succeeds, we just return 0. We have nothing
1269 if (ret
&& ret
!= EEXIST
) {
1270 if (!rename(tmpfile
, filename
))
1276 if (ret
!= EEXIST
) {
1277 fprintf(stderr
, "unable to write sha1 filename %s: %s", filename
, strerror(ret
));
1280 /* FIXME!!! Collision check here ? */
1286 int write_sha1_file(void *buf
, unsigned long len
, const char *type
, unsigned char *returnsha1
)
1289 unsigned char *compressed
;
1291 unsigned char sha1
[20];
1293 static char tmpfile
[PATH_MAX
];
1294 unsigned char hdr
[50];
1297 /* Normally if we have it in the pack then we do not bother writing
1298 * it out into .git/objects/??/?{38} file.
1300 filename
= write_sha1_file_prepare(buf
, len
, type
, sha1
, hdr
, &hdrlen
);
1302 memcpy(returnsha1
, sha1
, 20);
1303 if (has_sha1_file(sha1
))
1305 fd
= open(filename
, O_RDONLY
);
1308 * FIXME!!! We might do collision checking here, but we'd
1309 * need to uncompress the old file and check it. Later.
1315 if (errno
!= ENOENT
) {
1316 fprintf(stderr
, "sha1 file %s: %s", filename
, strerror(errno
));
1320 snprintf(tmpfile
, sizeof(tmpfile
), "%s/obj_XXXXXX", get_object_directory());
1322 fd
= mkstemp(tmpfile
);
1324 fprintf(stderr
, "unable to create temporary sha1 filename %s: %s", tmpfile
, strerror(errno
));
1329 memset(&stream
, 0, sizeof(stream
));
1330 deflateInit(&stream
, Z_BEST_COMPRESSION
);
1331 size
= deflateBound(&stream
, len
+hdrlen
);
1332 compressed
= xmalloc(size
);
1335 stream
.next_out
= compressed
;
1336 stream
.avail_out
= size
;
1338 /* First header.. */
1339 stream
.next_in
= hdr
;
1340 stream
.avail_in
= hdrlen
;
1341 while (deflate(&stream
, 0) == Z_OK
)
1344 /* Then the data itself.. */
1345 stream
.next_in
= buf
;
1346 stream
.avail_in
= len
;
1347 while (deflate(&stream
, Z_FINISH
) == Z_OK
)
1349 deflateEnd(&stream
);
1350 size
= stream
.total_out
;
1352 if (write(fd
, compressed
, size
) != size
)
1353 die("unable to write file");
1358 return move_temp_to_file(tmpfile
, filename
);
1361 int write_sha1_to_fd(int fd
, const unsigned char *sha1
)
1364 unsigned long objsize
;
1366 void *map
= map_sha1_file_internal(sha1
, &objsize
);
1368 void *temp_obj
= NULL
;
1372 unsigned char *unpacked
;
1377 // need to unpack and recompress it by itself
1378 unpacked
= read_packed_sha1(sha1
, type
, &len
);
1380 hdrlen
= sprintf(hdr
, "%s %lu", type
, len
) + 1;
1383 memset(&stream
, 0, sizeof(stream
));
1384 deflateInit(&stream
, Z_BEST_COMPRESSION
);
1385 size
= deflateBound(&stream
, len
+ hdrlen
);
1386 temp_obj
= buf
= xmalloc(size
);
1389 stream
.next_out
= buf
;
1390 stream
.avail_out
= size
;
1392 /* First header.. */
1393 stream
.next_in
= (void *)hdr
;
1394 stream
.avail_in
= hdrlen
;
1395 while (deflate(&stream
, 0) == Z_OK
)
1398 /* Then the data itself.. */
1399 stream
.next_in
= unpacked
;
1400 stream
.avail_in
= len
;
1401 while (deflate(&stream
, Z_FINISH
) == Z_OK
)
1403 deflateEnd(&stream
);
1406 objsize
= stream
.total_out
;
1410 size
= write(fd
, buf
+ posn
, objsize
- posn
);
1413 fprintf(stderr
, "write closed");
1420 } while (posn
< objsize
);
1423 munmap(map
, objsize
);
1430 int write_sha1_from_fd(const unsigned char *sha1
, int fd
, char *buffer
,
1431 size_t bufsize
, size_t *bufposn
)
1433 char tmpfile
[PATH_MAX
];
1436 unsigned char real_sha1
[20];
1437 unsigned char discard
[4096];
1441 snprintf(tmpfile
, sizeof(tmpfile
), "%s/obj_XXXXXX", get_object_directory());
1443 local
= mkstemp(tmpfile
);
1445 return error("Couldn't open %s for %s\n", tmpfile
, sha1_to_hex(sha1
));
1447 memset(&stream
, 0, sizeof(stream
));
1449 inflateInit(&stream
);
1456 stream
.avail_in
= *bufposn
;
1457 stream
.next_in
= (unsigned char *) buffer
;
1459 stream
.next_out
= discard
;
1460 stream
.avail_out
= sizeof(discard
);
1461 ret
= inflate(&stream
, Z_SYNC_FLUSH
);
1462 SHA1_Update(&c
, discard
, sizeof(discard
) -
1464 } while (stream
.avail_in
&& ret
== Z_OK
);
1465 write(local
, buffer
, *bufposn
- stream
.avail_in
);
1466 memmove(buffer
, buffer
+ *bufposn
- stream
.avail_in
,
1468 *bufposn
= stream
.avail_in
;
1472 size
= read(fd
, buffer
+ *bufposn
, bufsize
- *bufposn
);
1477 return error("Connection closed?");
1478 perror("Reading from connection");
1483 inflateEnd(&stream
);
1486 SHA1_Final(real_sha1
, &c
);
1487 if (ret
!= Z_STREAM_END
) {
1489 return error("File %s corrupted", sha1_to_hex(sha1
));
1491 if (memcmp(sha1
, real_sha1
, 20)) {
1493 return error("File %s has bad hash\n", sha1_to_hex(sha1
));
1496 return move_temp_to_file(tmpfile
, sha1_file_name(sha1
));
1499 int has_pack_index(const unsigned char *sha1
)
1502 if (stat(sha1_pack_index_name(sha1
), &st
))
1507 int has_pack_file(const unsigned char *sha1
)
1510 if (stat(sha1_pack_name(sha1
), &st
))
1515 int has_sha1_pack(const unsigned char *sha1
)
1517 struct pack_entry e
;
1518 return find_pack_entry(sha1
, &e
);
1521 int has_sha1_file(const unsigned char *sha1
)
1524 struct pack_entry e
;
1526 if (find_pack_entry(sha1
, &e
))
1528 return find_sha1_file(sha1
, &st
) ? 1 : 0;
1531 int index_fd(unsigned char *sha1
, int fd
, struct stat
*st
, int write_object
, const char *type
)
1533 unsigned long size
= st
->st_size
;
1536 unsigned char hdr
[50];
1541 buf
= mmap(NULL
, size
, PROT_READ
, MAP_PRIVATE
, fd
, 0);
1543 if (buf
== MAP_FAILED
)
1549 ret
= write_sha1_file(buf
, size
, type
, sha1
);
1551 write_sha1_file_prepare(buf
, size
, type
, sha1
, hdr
, &hdrlen
);
1559 int index_path(unsigned char *sha1
, const char *path
, struct stat
*st
, int write_object
)
1564 switch (st
->st_mode
& S_IFMT
) {
1566 fd
= open(path
, O_RDONLY
);
1568 return error("open(\"%s\"): %s", path
,
1570 if (index_fd(sha1
, fd
, st
, write_object
, NULL
) < 0)
1571 return error("%s: failed to insert into database",
1575 target
= xmalloc(st
->st_size
+1);
1576 if (readlink(path
, target
, st
->st_size
+1) != st
->st_size
) {
1577 char *errstr
= strerror(errno
);
1579 return error("readlink(\"%s\"): %s", path
,
1582 if (!write_object
) {
1583 unsigned char hdr
[50];
1585 write_sha1_file_prepare(target
, st
->st_size
, "blob",
1586 sha1
, hdr
, &hdrlen
);
1587 } else if (write_sha1_file(target
, st
->st_size
, "blob", sha1
))
1588 return error("%s: failed to insert into database",
1593 return error("%s: unsupported file type", path
);