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
= getenv(GIT_DIR_ENVIRONMENT
);
55 git_dir
= DEFAULT_GIT_DIR_ENVIRONMENT
;
56 git_object_dir
= getenv(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
= getenv(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
= getenv(GRAFT_ENVIRONMENT
);
70 git_graft_file
= strdup(git_path("info/grafts"));
73 char *get_git_dir(void)
80 char *get_object_directory(void)
84 return git_object_dir
;
87 char *get_refs_directory(void)
94 char *get_index_file(void)
98 return git_index_file
;
101 char *get_graft_file(void)
105 return git_graft_file
;
108 int safe_create_leading_directories(char *path
)
113 pos
= strchr(pos
, '/');
117 if (mkdir(path
, 0777) < 0)
118 if (errno
!= EEXIST
) {
127 char * sha1_to_hex(const unsigned char *sha1
)
129 static char buffer
[50];
130 static const char hex
[] = "0123456789abcdef";
134 for (i
= 0; i
< 20; i
++) {
135 unsigned int val
= *sha1
++;
136 *buf
++ = hex
[val
>> 4];
137 *buf
++ = hex
[val
& 0xf];
142 static void fill_sha1_path(char *pathbuf
, const unsigned char *sha1
)
145 for (i
= 0; i
< 20; i
++) {
146 static char hex
[] = "0123456789abcdef";
147 unsigned int val
= sha1
[i
];
148 char *pos
= pathbuf
+ i
*2 + (i
> 0);
149 *pos
++ = hex
[val
>> 4];
150 *pos
= hex
[val
& 0xf];
155 * NOTE! This returns a statically allocated buffer, so you have to be
156 * careful about using it. Do a "strdup()" if you need to save the
159 * Also note that this returns the location for creating. Reading
160 * SHA1 file can happen from any alternate directory listed in the
161 * DB_ENVIRONMENT environment variable if it is not found in
162 * the primary object database.
164 char *sha1_file_name(const unsigned char *sha1
)
166 static char *name
, *base
;
169 const char *sha1_file_directory
= get_object_directory();
170 int len
= strlen(sha1_file_directory
);
171 base
= xmalloc(len
+ 60);
172 memcpy(base
, sha1_file_directory
, len
);
173 memset(base
+len
, 0, 60);
176 name
= base
+ len
+ 1;
178 fill_sha1_path(name
, sha1
);
182 char *sha1_pack_name(const unsigned char *sha1
)
184 static const char hex
[] = "0123456789abcdef";
185 static char *name
, *base
, *buf
;
189 const char *sha1_file_directory
= get_object_directory();
190 int len
= strlen(sha1_file_directory
);
191 base
= xmalloc(len
+ 60);
192 sprintf(base
, "%s/pack/pack-1234567890123456789012345678901234567890.pack", sha1_file_directory
);
193 name
= base
+ len
+ 11;
198 for (i
= 0; i
< 20; i
++) {
199 unsigned int val
= *sha1
++;
200 *buf
++ = hex
[val
>> 4];
201 *buf
++ = hex
[val
& 0xf];
207 char *sha1_pack_index_name(const unsigned char *sha1
)
209 static const char hex
[] = "0123456789abcdef";
210 static char *name
, *base
, *buf
;
214 const char *sha1_file_directory
= get_object_directory();
215 int len
= strlen(sha1_file_directory
);
216 base
= xmalloc(len
+ 60);
217 sprintf(base
, "%s/pack/pack-1234567890123456789012345678901234567890.idx", sha1_file_directory
);
218 name
= base
+ len
+ 11;
223 for (i
= 0; i
< 20; i
++) {
224 unsigned int val
= *sha1
++;
225 *buf
++ = hex
[val
>> 4];
226 *buf
++ = hex
[val
& 0xf];
232 struct alternate_object_database
*alt_odb_list
;
233 static struct alternate_object_database
**alt_odb_tail
;
236 * Prepare alternate object database registry.
238 * The variable alt_odb_list points at the list of struct
239 * alternate_object_database. The elements on this list come from
240 * non-empty elements from colon separated ALTERNATE_DB_ENVIRONMENT
241 * environment variable, and $GIT_OBJECT_DIRECTORY/info/alternates,
242 * whose contents is exactly in the same format as that environment
243 * variable. Its base points at a statically allocated buffer that
244 * contains "/the/directory/corresponding/to/.git/objects/...", while
245 * its name points just after the slash at the end of ".git/objects/"
246 * in the example above, and has enough space to hold 40-byte hex
247 * SHA1, an extra slash for the first level indirection, and the
250 static void link_alt_odb_entries(const char *alt
, const char *ep
, int sep
,
251 const char *relative_base
)
253 const char *cp
, *last
;
254 struct alternate_object_database
*ent
;
260 if (cp
< ep
&& *cp
== '#') {
261 while (cp
< ep
&& *cp
!= sep
)
266 for ( ; cp
< ep
&& *cp
!= sep
; cp
++)
269 /* 43 = 40-byte + 2 '/' + terminating NUL */
270 int pfxlen
= cp
- last
;
271 int entlen
= pfxlen
+ 43;
273 if (*last
!= '/' && relative_base
) {
274 /* Relative alt-odb */
276 base_len
= strlen(relative_base
) + 1;
280 ent
= xmalloc(sizeof(*ent
) + entlen
);
282 alt_odb_tail
= &(ent
->next
);
284 if (*last
!= '/' && relative_base
) {
285 memcpy(ent
->base
, relative_base
, base_len
- 1);
286 ent
->base
[base_len
- 1] = '/';
287 memcpy(ent
->base
+ base_len
,
291 memcpy(ent
->base
, last
, pfxlen
);
292 ent
->name
= ent
->base
+ pfxlen
+ 1;
293 ent
->base
[pfxlen
] = ent
->base
[pfxlen
+ 3] = '/';
294 ent
->base
[entlen
-1] = 0;
296 while (cp
< ep
&& *cp
== sep
)
302 void prepare_alt_odb(void)
310 alt
= getenv(ALTERNATE_DB_ENVIRONMENT
);
315 alt_odb_tail
= &alt_odb_list
;
316 link_alt_odb_entries(alt
, alt
+ strlen(alt
), ':', NULL
);
318 sprintf(path
, "%s/info/alternates", get_object_directory());
319 fd
= open(path
, O_RDONLY
);
322 if (fstat(fd
, &st
) || (st
.st_size
== 0)) {
326 map
= mmap(NULL
, st
.st_size
, PROT_READ
, MAP_PRIVATE
, fd
, 0);
328 if (map
== MAP_FAILED
)
331 link_alt_odb_entries(map
, map
+ st
.st_size
, '\n',
332 get_object_directory());
333 munmap(map
, st
.st_size
);
336 static char *find_sha1_file(const unsigned char *sha1
, struct stat
*st
)
338 char *name
= sha1_file_name(sha1
);
339 struct alternate_object_database
*alt
;
344 for (alt
= alt_odb_list
; alt
; alt
= alt
->next
) {
346 fill_sha1_path(name
, sha1
);
347 if (!stat(alt
->base
, st
))
353 #define PACK_MAX_SZ (1<<26)
354 static int pack_used_ctr
;
355 static unsigned long pack_mapped
;
356 struct packed_git
*packed_git
;
358 static int check_packed_git_idx(const char *path
, unsigned long *idx_size_
,
363 unsigned long idx_size
;
365 int fd
= open(path
, O_RDONLY
);
369 if (fstat(fd
, &st
)) {
373 idx_size
= st
.st_size
;
374 idx_map
= mmap(NULL
, idx_size
, PROT_READ
, MAP_PRIVATE
, fd
, 0);
376 if (idx_map
== MAP_FAILED
)
381 *idx_size_
= idx_size
;
383 /* check index map */
384 if (idx_size
< 4*256 + 20 + 20)
385 return error("index file too small");
387 for (i
= 0; i
< 256; i
++) {
388 unsigned int n
= ntohl(index
[i
]);
390 return error("non-monotonic index");
396 * - 256 index entries 4 bytes each
397 * - 24-byte entries * nr (20-byte sha1 + 4-byte offset)
398 * - 20-byte SHA1 of the packfile
399 * - 20-byte SHA1 file checksum
401 if (idx_size
!= 4*256 + nr
* 24 + 20 + 20)
402 return error("wrong index file size");
407 static int unuse_one_packed_git(void)
409 struct packed_git
*p
, *lru
= NULL
;
411 for (p
= packed_git
; p
; p
= p
->next
) {
412 if (p
->pack_use_cnt
|| !p
->pack_base
)
414 if (!lru
|| p
->pack_last_used
< lru
->pack_last_used
)
419 munmap(lru
->pack_base
, lru
->pack_size
);
420 lru
->pack_base
= NULL
;
424 void unuse_packed_git(struct packed_git
*p
)
429 int use_packed_git(struct packed_git
*p
)
433 // We created the struct before we had the pack
434 stat(p
->pack_name
, &st
);
435 if (!S_ISREG(st
.st_mode
))
436 die("packfile %s not a regular file", p
->pack_name
);
437 p
->pack_size
= st
.st_size
;
444 pack_mapped
+= p
->pack_size
;
445 while (PACK_MAX_SZ
< pack_mapped
&& unuse_one_packed_git())
447 fd
= open(p
->pack_name
, O_RDONLY
);
449 die("packfile %s cannot be opened", p
->pack_name
);
450 if (fstat(fd
, &st
)) {
452 die("packfile %s cannot be opened", p
->pack_name
);
454 if (st
.st_size
!= p
->pack_size
)
455 die("packfile %s size mismatch.", p
->pack_name
);
456 map
= mmap(NULL
, p
->pack_size
, PROT_READ
, MAP_PRIVATE
, fd
, 0);
458 if (map
== MAP_FAILED
)
459 die("packfile %s cannot be mapped.", p
->pack_name
);
462 /* Check if the pack file matches with the index file.
465 if (memcmp((char*)(p
->index_base
) + p
->index_size
- 40,
466 p
->pack_base
+ p
->pack_size
- 20, 20)) {
468 die("packfile %s does not match index.", p
->pack_name
);
471 p
->pack_last_used
= pack_used_ctr
++;
476 struct packed_git
*add_packed_git(char *path
, int path_len
)
479 struct packed_git
*p
;
480 unsigned long idx_size
;
483 if (check_packed_git_idx(path
, &idx_size
, &idx_map
))
486 /* do we have a corresponding .pack file? */
487 strcpy(path
+ path_len
- 4, ".pack");
488 if (stat(path
, &st
) || !S_ISREG(st
.st_mode
)) {
489 munmap(idx_map
, idx_size
);
492 /* ok, it looks sane as far as we can check without
493 * actually mapping the pack file.
495 p
= xmalloc(sizeof(*p
) + path_len
+ 2);
496 strcpy(p
->pack_name
, path
);
497 p
->index_size
= idx_size
;
498 p
->pack_size
= st
.st_size
;
499 p
->index_base
= idx_map
;
502 p
->pack_last_used
= 0;
507 struct packed_git
*parse_pack_index(unsigned char *sha1
)
509 char *path
= sha1_pack_index_name(sha1
);
510 return parse_pack_index_file(sha1
, path
);
513 struct packed_git
*parse_pack_index_file(const unsigned char *sha1
, char *idx_path
)
515 struct packed_git
*p
;
516 unsigned long idx_size
;
520 if (check_packed_git_idx(idx_path
, &idx_size
, &idx_map
))
523 path
= sha1_pack_name(sha1
);
525 p
= xmalloc(sizeof(*p
) + strlen(path
) + 2);
526 strcpy(p
->pack_name
, path
);
527 p
->index_size
= idx_size
;
529 p
->index_base
= idx_map
;
532 p
->pack_last_used
= 0;
534 memcpy(p
->sha1
, sha1
, 20);
538 void install_packed_git(struct packed_git
*pack
)
540 pack
->next
= packed_git
;
544 static void prepare_packed_git_one(char *objdir
)
551 sprintf(path
, "%s/pack", objdir
);
557 while ((de
= readdir(dir
)) != NULL
) {
558 int namelen
= strlen(de
->d_name
);
559 struct packed_git
*p
;
561 if (strcmp(de
->d_name
+ namelen
- 4, ".idx"))
564 /* we have .idx. Is it a file we can map? */
565 strcpy(path
+ len
, de
->d_name
);
566 p
= add_packed_git(path
, len
+ namelen
);
569 p
->next
= packed_git
;
575 void prepare_packed_git(void)
577 static int run_once
= 0;
578 struct alternate_object_database
*alt
;
582 prepare_packed_git_one(get_object_directory());
584 for (alt
= alt_odb_list
; alt
; alt
= alt
->next
) {
586 prepare_packed_git_one(alt
->base
);
591 int check_sha1_signature(const unsigned char *sha1
, void *map
, unsigned long size
, const char *type
)
594 unsigned char real_sha1
[20];
598 SHA1_Update(&c
, header
, 1+sprintf(header
, "%s %lu", type
, size
));
599 SHA1_Update(&c
, map
, size
);
600 SHA1_Final(real_sha1
, &c
);
601 return memcmp(sha1
, real_sha1
, 20) ? -1 : 0;
604 static void *map_sha1_file_internal(const unsigned char *sha1
,
610 char *filename
= find_sha1_file(sha1
, &st
);
616 fd
= open(filename
, O_RDONLY
| sha1_file_open_flag
);
618 /* See if it works without O_NOATIME */
619 switch (sha1_file_open_flag
) {
621 fd
= open(filename
, O_RDONLY
);
629 /* If it failed once, it will probably fail again.
630 * Stop using O_NOATIME
632 sha1_file_open_flag
= 0;
634 map
= mmap(NULL
, st
.st_size
, PROT_READ
, MAP_PRIVATE
, fd
, 0);
636 if (map
== MAP_FAILED
)
642 int unpack_sha1_header(z_stream
*stream
, void *map
, unsigned long mapsize
, void *buffer
, unsigned long size
)
644 /* Get the data stream */
645 memset(stream
, 0, sizeof(*stream
));
646 stream
->next_in
= map
;
647 stream
->avail_in
= mapsize
;
648 stream
->next_out
= buffer
;
649 stream
->avail_out
= size
;
652 return inflate(stream
, 0);
655 static void *unpack_sha1_rest(z_stream
*stream
, void *buffer
, unsigned long size
)
657 int bytes
= strlen(buffer
) + 1;
658 unsigned char *buf
= xmalloc(1+size
);
660 memcpy(buf
, buffer
+ bytes
, stream
->total_out
- bytes
);
661 bytes
= stream
->total_out
- bytes
;
663 stream
->next_out
= buf
+ bytes
;
664 stream
->avail_out
= size
- bytes
;
665 while (inflate(stream
, Z_FINISH
) == Z_OK
)
674 * We used to just use "sscanf()", but that's actually way
675 * too permissive for what we want to check. So do an anal
676 * object header parse by hand.
678 int parse_sha1_header(char *hdr
, char *type
, unsigned long *sizep
)
684 * The type can be at most ten bytes (including the
685 * terminating '\0' that we add), and is followed by
700 * The length must follow immediately, and be in canonical
701 * decimal format (ie "010" is not valid).
708 unsigned long c
= *hdr
- '0';
712 size
= size
* 10 + c
;
718 * The length must be followed by a zero byte
720 return *hdr
? -1 : 0;
723 void * unpack_sha1_file(void *map
, unsigned long mapsize
, char *type
, unsigned long *size
)
729 ret
= unpack_sha1_header(&stream
, map
, mapsize
, hdr
, sizeof(hdr
));
730 if (ret
< Z_OK
|| parse_sha1_header(hdr
, type
, size
) < 0)
733 return unpack_sha1_rest(&stream
, hdr
, *size
);
736 /* forward declaration for a mutually recursive function */
737 static int packed_object_info(struct pack_entry
*entry
,
738 char *type
, unsigned long *sizep
);
740 static int packed_delta_info(unsigned char *base_sha1
,
741 unsigned long delta_size
,
744 unsigned long *sizep
,
745 struct packed_git
*p
)
747 struct pack_entry base_ent
;
750 die("truncated pack file");
752 /* The base entry _must_ be in the same pack */
753 if (!find_pack_entry_one(base_sha1
, &base_ent
, p
))
754 die("failed to find delta-pack base object %s",
755 sha1_to_hex(base_sha1
));
757 /* We choose to only get the type of the base object and
758 * ignore potentially corrupt pack file that expects the delta
759 * based on a base with a wrong size. This saves tons of
763 if (packed_object_info(&base_ent
, type
, NULL
))
764 die("cannot get info for delta-pack base");
767 const unsigned char *data
;
768 unsigned char delta_head
[64];
769 unsigned long result_size
;
773 memset(&stream
, 0, sizeof(stream
));
775 data
= stream
.next_in
= base_sha1
+ 20;
776 stream
.avail_in
= left
- 20;
777 stream
.next_out
= delta_head
;
778 stream
.avail_out
= sizeof(delta_head
);
780 inflateInit(&stream
);
781 st
= inflate(&stream
, Z_FINISH
);
783 if ((st
!= Z_STREAM_END
) &&
784 stream
.total_out
!= sizeof(delta_head
))
785 die("delta data unpack-initial failed");
787 /* Examine the initial part of the delta to figure out
791 get_delta_hdr_size(&data
); /* ignore base size */
793 /* Read the result size */
794 result_size
= get_delta_hdr_size(&data
);
795 *sizep
= result_size
;
800 static unsigned long unpack_object_header(struct packed_git
*p
, unsigned long offset
,
801 enum object_type
*type
, unsigned long *sizep
)
804 unsigned char *pack
, c
;
807 if (offset
>= p
->pack_size
)
808 die("object offset outside of pack file");
810 pack
= p
->pack_base
+ offset
;
813 *type
= (c
>> 4) & 7;
817 if (offset
>= p
->pack_size
)
818 die("object offset outside of pack file");
821 size
+= (c
& 0x7f) << shift
;
828 void packed_object_info_detail(struct pack_entry
*e
,
831 unsigned long *store_size
,
832 int *delta_chain_length
,
833 unsigned char *base_sha1
)
835 struct packed_git
*p
= e
->p
;
836 unsigned long offset
, left
;
838 enum object_type kind
;
840 offset
= unpack_object_header(p
, e
->offset
, &kind
, size
);
841 pack
= p
->pack_base
+ offset
;
842 left
= p
->pack_size
- offset
;
843 if (kind
!= OBJ_DELTA
)
844 *delta_chain_length
= 0;
846 int chain_length
= 0;
847 memcpy(base_sha1
, pack
, 20);
849 struct pack_entry base_ent
;
852 find_pack_entry_one(pack
, &base_ent
, p
);
853 offset
= unpack_object_header(p
, base_ent
.offset
,
855 pack
= p
->pack_base
+ offset
;
857 } while (kind
== OBJ_DELTA
);
858 *delta_chain_length
= chain_length
;
862 strcpy(type
, "commit");
865 strcpy(type
, "tree");
868 strcpy(type
, "blob");
874 die("corrupted pack file %s containing object of kind %d",
877 *store_size
= 0; /* notyet */
880 static int packed_object_info(struct pack_entry
*entry
,
881 char *type
, unsigned long *sizep
)
883 struct packed_git
*p
= entry
->p
;
884 unsigned long offset
, size
, left
;
886 enum object_type kind
;
889 if (use_packed_git(p
))
890 die("cannot map packed file");
892 offset
= unpack_object_header(p
, entry
->offset
, &kind
, &size
);
893 pack
= p
->pack_base
+ offset
;
894 left
= p
->pack_size
- offset
;
898 retval
= packed_delta_info(pack
, size
, left
, type
, sizep
, p
);
902 strcpy(type
, "commit");
905 strcpy(type
, "tree");
908 strcpy(type
, "blob");
914 die("corrupted pack file %s containing object of kind %d",
923 /* forward declaration for a mutually recursive function */
924 static void *unpack_entry(struct pack_entry
*, char *, unsigned long *);
926 static void *unpack_delta_entry(unsigned char *base_sha1
,
927 unsigned long delta_size
,
930 unsigned long *sizep
,
931 struct packed_git
*p
)
933 struct pack_entry base_ent
;
934 void *data
, *delta_data
, *result
, *base
;
935 unsigned long data_size
, result_size
, base_size
;
940 die("truncated pack file");
941 data
= base_sha1
+ 20;
942 data_size
= left
- 20;
943 delta_data
= xmalloc(delta_size
);
945 memset(&stream
, 0, sizeof(stream
));
947 stream
.next_in
= data
;
948 stream
.avail_in
= data_size
;
949 stream
.next_out
= delta_data
;
950 stream
.avail_out
= delta_size
;
952 inflateInit(&stream
);
953 st
= inflate(&stream
, Z_FINISH
);
955 if ((st
!= Z_STREAM_END
) || stream
.total_out
!= delta_size
)
956 die("delta data unpack failed");
958 /* The base entry _must_ be in the same pack */
959 if (!find_pack_entry_one(base_sha1
, &base_ent
, p
))
960 die("failed to find delta-pack base object %s",
961 sha1_to_hex(base_sha1
));
962 base
= unpack_entry_gently(&base_ent
, type
, &base_size
);
964 die("failed to read delta-pack base object %s",
965 sha1_to_hex(base_sha1
));
966 result
= patch_delta(base
, base_size
,
967 delta_data
, delta_size
,
970 die("failed to apply delta");
973 *sizep
= result_size
;
977 static void *unpack_non_delta_entry(unsigned char *data
,
983 unsigned char *buffer
;
985 buffer
= xmalloc(size
+ 1);
987 memset(&stream
, 0, sizeof(stream
));
988 stream
.next_in
= data
;
989 stream
.avail_in
= left
;
990 stream
.next_out
= buffer
;
991 stream
.avail_out
= size
;
993 inflateInit(&stream
);
994 st
= inflate(&stream
, Z_FINISH
);
996 if ((st
!= Z_STREAM_END
) || stream
.total_out
!= size
) {
1004 static void *unpack_entry(struct pack_entry
*entry
,
1005 char *type
, unsigned long *sizep
)
1007 struct packed_git
*p
= entry
->p
;
1010 if (use_packed_git(p
))
1011 die("cannot map packed file");
1012 retval
= unpack_entry_gently(entry
, type
, sizep
);
1013 unuse_packed_git(p
);
1015 die("corrupted pack file %s", p
->pack_name
);
1019 /* The caller is responsible for use_packed_git()/unuse_packed_git() pair */
1020 void *unpack_entry_gently(struct pack_entry
*entry
,
1021 char *type
, unsigned long *sizep
)
1023 struct packed_git
*p
= entry
->p
;
1024 unsigned long offset
, size
, left
;
1025 unsigned char *pack
;
1026 enum object_type kind
;
1029 offset
= unpack_object_header(p
, entry
->offset
, &kind
, &size
);
1030 pack
= p
->pack_base
+ offset
;
1031 left
= p
->pack_size
- offset
;
1034 retval
= unpack_delta_entry(pack
, size
, left
, type
, sizep
, p
);
1037 strcpy(type
, "commit");
1040 strcpy(type
, "tree");
1043 strcpy(type
, "blob");
1046 strcpy(type
, "tag");
1052 retval
= unpack_non_delta_entry(pack
, size
, left
);
1056 int num_packed_objects(const struct packed_git
*p
)
1058 /* See check_packed_git_idx() */
1059 return (p
->index_size
- 20 - 20 - 4*256) / 24;
1062 int nth_packed_object_sha1(const struct packed_git
*p
, int n
,
1063 unsigned char* sha1
)
1065 void *index
= p
->index_base
+ 256;
1066 if (n
< 0 || num_packed_objects(p
) <= n
)
1068 memcpy(sha1
, (index
+ 24 * n
+ 4), 20);
1072 int find_pack_entry_one(const unsigned char *sha1
,
1073 struct pack_entry
*e
, struct packed_git
*p
)
1075 unsigned int *level1_ofs
= p
->index_base
;
1076 int hi
= ntohl(level1_ofs
[*sha1
]);
1077 int lo
= ((*sha1
== 0x0) ? 0 : ntohl(level1_ofs
[*sha1
- 1]));
1078 void *index
= p
->index_base
+ 256;
1081 int mi
= (lo
+ hi
) / 2;
1082 int cmp
= memcmp(index
+ 24 * mi
+ 4, sha1
, 20);
1084 e
->offset
= ntohl(*((int*)(index
+ 24 * mi
)));
1085 memcpy(e
->sha1
, sha1
, 20);
1097 static int find_pack_entry(const unsigned char *sha1
, struct pack_entry
*e
)
1099 struct packed_git
*p
;
1100 prepare_packed_git();
1102 for (p
= packed_git
; p
; p
= p
->next
) {
1103 if (find_pack_entry_one(sha1
, e
, p
))
1109 struct packed_git
*find_sha1_pack(const unsigned char *sha1
,
1110 struct packed_git
*packs
)
1112 struct packed_git
*p
;
1113 struct pack_entry e
;
1115 for (p
= packs
; p
; p
= p
->next
) {
1116 if (find_pack_entry_one(sha1
, &e
, p
))
1123 int sha1_object_info(const unsigned char *sha1
, char *type
, unsigned long *sizep
)
1126 unsigned long mapsize
, size
;
1131 map
= map_sha1_file_internal(sha1
, &mapsize
);
1133 struct pack_entry e
;
1135 if (!find_pack_entry(sha1
, &e
))
1136 return error("unable to find %s", sha1_to_hex(sha1
));
1137 return packed_object_info(&e
, type
, sizep
);
1139 if (unpack_sha1_header(&stream
, map
, mapsize
, hdr
, sizeof(hdr
)) < 0)
1140 status
= error("unable to unpack %s header",
1142 if (parse_sha1_header(hdr
, type
, &size
) < 0)
1143 status
= error("unable to parse %s header", sha1_to_hex(sha1
));
1149 inflateEnd(&stream
);
1150 munmap(map
, mapsize
);
1154 static void *read_packed_sha1(const unsigned char *sha1
, char *type
, unsigned long *size
)
1156 struct pack_entry e
;
1158 if (!find_pack_entry(sha1
, &e
)) {
1159 error("cannot read sha1_file for %s", sha1_to_hex(sha1
));
1162 return unpack_entry(&e
, type
, size
);
1165 void * read_sha1_file(const unsigned char *sha1
, char *type
, unsigned long *size
)
1167 unsigned long mapsize
;
1169 struct pack_entry e
;
1171 if (find_pack_entry(sha1
, &e
))
1172 return read_packed_sha1(sha1
, type
, size
);
1173 map
= map_sha1_file_internal(sha1
, &mapsize
);
1175 buf
= unpack_sha1_file(map
, mapsize
, type
, size
);
1176 munmap(map
, mapsize
);
1182 void *read_object_with_reference(const unsigned char *sha1
,
1183 const char *required_type
,
1184 unsigned long *size
,
1185 unsigned char *actual_sha1_return
)
1189 unsigned long isize
;
1190 unsigned char actual_sha1
[20];
1192 memcpy(actual_sha1
, sha1
, 20);
1194 int ref_length
= -1;
1195 const char *ref_type
= NULL
;
1197 buffer
= read_sha1_file(actual_sha1
, type
, &isize
);
1200 if (!strcmp(type
, required_type
)) {
1202 if (actual_sha1_return
)
1203 memcpy(actual_sha1_return
, actual_sha1
, 20);
1206 /* Handle references */
1207 else if (!strcmp(type
, "commit"))
1209 else if (!strcmp(type
, "tag"))
1210 ref_type
= "object ";
1215 ref_length
= strlen(ref_type
);
1217 if (memcmp(buffer
, ref_type
, ref_length
) ||
1218 get_sha1_hex(buffer
+ ref_length
, actual_sha1
)) {
1223 /* Now we have the ID of the referred-to object in
1224 * actual_sha1. Check again. */
1228 char *write_sha1_file_prepare(void *buf
,
1231 unsigned char *sha1
,
1237 /* Generate the header */
1238 *hdrlen
= sprintf((char *)hdr
, "%s %lu", type
, len
)+1;
1242 SHA1_Update(&c
, hdr
, *hdrlen
);
1243 SHA1_Update(&c
, buf
, len
);
1244 SHA1_Final(sha1
, &c
);
1246 return sha1_file_name(sha1
);
1249 int write_sha1_file(void *buf
, unsigned long len
, const char *type
, unsigned char *returnsha1
)
1252 unsigned char *compressed
;
1254 unsigned char sha1
[20];
1256 static char tmpfile
[PATH_MAX
];
1257 unsigned char hdr
[50];
1258 int fd
, hdrlen
, ret
;
1260 /* Normally if we have it in the pack then we do not bother writing
1261 * it out into .git/objects/??/?{38} file.
1263 filename
= write_sha1_file_prepare(buf
, len
, type
, sha1
, hdr
, &hdrlen
);
1265 memcpy(returnsha1
, sha1
, 20);
1266 if (has_sha1_file(sha1
))
1268 fd
= open(filename
, O_RDONLY
);
1271 * FIXME!!! We might do collision checking here, but we'd
1272 * need to uncompress the old file and check it. Later.
1278 if (errno
!= ENOENT
) {
1279 fprintf(stderr
, "sha1 file %s: %s", filename
, strerror(errno
));
1283 snprintf(tmpfile
, sizeof(tmpfile
), "%s/obj_XXXXXX", get_object_directory());
1285 fd
= mkstemp(tmpfile
);
1287 fprintf(stderr
, "unable to create temporary sha1 filename %s: %s", tmpfile
, strerror(errno
));
1292 memset(&stream
, 0, sizeof(stream
));
1293 deflateInit(&stream
, Z_BEST_COMPRESSION
);
1294 size
= deflateBound(&stream
, len
+hdrlen
);
1295 compressed
= xmalloc(size
);
1298 stream
.next_out
= compressed
;
1299 stream
.avail_out
= size
;
1301 /* First header.. */
1302 stream
.next_in
= hdr
;
1303 stream
.avail_in
= hdrlen
;
1304 while (deflate(&stream
, 0) == Z_OK
)
1307 /* Then the data itself.. */
1308 stream
.next_in
= buf
;
1309 stream
.avail_in
= len
;
1310 while (deflate(&stream
, Z_FINISH
) == Z_OK
)
1312 deflateEnd(&stream
);
1313 size
= stream
.total_out
;
1315 if (write(fd
, compressed
, size
) != size
)
1316 die("unable to write file");
1321 ret
= link(tmpfile
, filename
);
1326 * Coda hack - coda doesn't like cross-directory links,
1327 * so we fall back to a rename, which will mean that it
1328 * won't be able to check collisions, but that's not a
1331 * When this succeeds, we just return 0. We have nothing
1334 if (ret
== EXDEV
&& !rename(tmpfile
, filename
))
1339 if (ret
!= EEXIST
) {
1340 fprintf(stderr
, "unable to write sha1 filename %s: %s", filename
, strerror(ret
));
1343 /* FIXME!!! Collision check here ? */
1349 int write_sha1_to_fd(int fd
, const unsigned char *sha1
)
1352 unsigned long objsize
;
1354 void *map
= map_sha1_file_internal(sha1
, &objsize
);
1356 void *temp_obj
= NULL
;
1360 unsigned char *unpacked
;
1365 // need to unpack and recompress it by itself
1366 unpacked
= read_packed_sha1(sha1
, type
, &len
);
1368 hdrlen
= sprintf(hdr
, "%s %lu", type
, len
) + 1;
1371 memset(&stream
, 0, sizeof(stream
));
1372 deflateInit(&stream
, Z_BEST_COMPRESSION
);
1373 size
= deflateBound(&stream
, len
+ hdrlen
);
1374 temp_obj
= buf
= xmalloc(size
);
1377 stream
.next_out
= buf
;
1378 stream
.avail_out
= size
;
1380 /* First header.. */
1381 stream
.next_in
= (void *)hdr
;
1382 stream
.avail_in
= hdrlen
;
1383 while (deflate(&stream
, 0) == Z_OK
)
1386 /* Then the data itself.. */
1387 stream
.next_in
= unpacked
;
1388 stream
.avail_in
= len
;
1389 while (deflate(&stream
, Z_FINISH
) == Z_OK
)
1391 deflateEnd(&stream
);
1394 objsize
= stream
.total_out
;
1398 size
= write(fd
, buf
+ posn
, objsize
- posn
);
1401 fprintf(stderr
, "write closed");
1408 } while (posn
< objsize
);
1411 munmap(map
, objsize
);
1418 int write_sha1_from_fd(const unsigned char *sha1
, int fd
, char *buffer
,
1419 size_t bufsize
, size_t *bufposn
)
1421 char *filename
= sha1_file_name(sha1
);
1425 unsigned char real_sha1
[20];
1426 unsigned char discard
[4096];
1430 local
= open(filename
, O_WRONLY
| O_CREAT
| O_EXCL
, 0666);
1433 return error("Couldn't open %s\n", filename
);
1435 memset(&stream
, 0, sizeof(stream
));
1437 inflateInit(&stream
);
1444 stream
.avail_in
= *bufposn
;
1445 stream
.next_in
= (unsigned char *) buffer
;
1447 stream
.next_out
= discard
;
1448 stream
.avail_out
= sizeof(discard
);
1449 ret
= inflate(&stream
, Z_SYNC_FLUSH
);
1450 SHA1_Update(&c
, discard
, sizeof(discard
) -
1452 } while (stream
.avail_in
&& ret
== Z_OK
);
1453 write(local
, buffer
, *bufposn
- stream
.avail_in
);
1454 memmove(buffer
, buffer
+ *bufposn
- stream
.avail_in
,
1456 *bufposn
= stream
.avail_in
;
1460 size
= read(fd
, buffer
+ *bufposn
, bufsize
- *bufposn
);
1465 return error("Connection closed?");
1466 perror("Reading from connection");
1471 inflateEnd(&stream
);
1474 SHA1_Final(real_sha1
, &c
);
1475 if (ret
!= Z_STREAM_END
) {
1477 return error("File %s corrupted", sha1_to_hex(sha1
));
1479 if (memcmp(sha1
, real_sha1
, 20)) {
1481 return error("File %s has bad hash\n", sha1_to_hex(sha1
));
1487 int has_pack_index(const unsigned char *sha1
)
1490 if (stat(sha1_pack_index_name(sha1
), &st
))
1495 int has_pack_file(const unsigned char *sha1
)
1498 if (stat(sha1_pack_name(sha1
), &st
))
1503 int has_sha1_pack(const unsigned char *sha1
)
1505 struct pack_entry e
;
1506 return find_pack_entry(sha1
, &e
);
1509 int has_sha1_file(const unsigned char *sha1
)
1512 struct pack_entry e
;
1514 if (find_pack_entry(sha1
, &e
))
1516 return find_sha1_file(sha1
, &st
) ? 1 : 0;
1519 int index_fd(unsigned char *sha1
, int fd
, struct stat
*st
, int write_object
, const char *type
)
1521 unsigned long size
= st
->st_size
;
1524 unsigned char hdr
[50];
1529 buf
= mmap(NULL
, size
, PROT_READ
, MAP_PRIVATE
, fd
, 0);
1531 if (buf
== MAP_FAILED
)
1537 ret
= write_sha1_file(buf
, size
, type
, sha1
);
1539 write_sha1_file_prepare(buf
, size
, type
, sha1
, hdr
, &hdrlen
);