2 * GIT - The information manager from hell
4 * Copyright (C) Linus Torvalds, 2005
6 * This handles basic git sha1 object files - packing, unpacking,
16 #if defined(__linux__) && (defined(__i386__) || defined(__PPC__))
17 #define O_NOATIME 01000000
23 static unsigned int sha1_file_open_flag
= O_NOATIME
;
25 static unsigned hexval(char c
)
27 if (c
>= '0' && c
<= '9')
29 if (c
>= 'a' && c
<= 'f')
31 if (c
>= 'A' && c
<= 'F')
36 int get_sha1_hex(const char *hex
, unsigned char *sha1
)
39 for (i
= 0; i
< 20; i
++) {
40 unsigned int val
= (hexval(hex
[0]) << 4) | hexval(hex
[1]);
49 static int get_sha1_file(const char *path
, unsigned char *result
)
52 int fd
= open(path
, O_RDONLY
);
57 len
= read(fd
, buffer
, sizeof(buffer
));
61 return get_sha1_hex(buffer
, result
);
64 static char *git_dir
, *git_object_dir
, *git_index_file
, *git_refs_dir
;
65 static void setup_git_env(void)
67 git_dir
= gitenv(GIT_DIR_ENVIRONMENT
);
69 git_dir
= DEFAULT_GIT_DIR_ENVIRONMENT
;
70 git_object_dir
= gitenv(DB_ENVIRONMENT
);
71 if (!git_object_dir
) {
72 git_object_dir
= xmalloc(strlen(git_dir
) + 9);
73 sprintf(git_object_dir
, "%s/objects", git_dir
);
75 git_refs_dir
= xmalloc(strlen(git_dir
) + 6);
76 sprintf(git_refs_dir
, "%s/refs", git_dir
);
77 git_index_file
= gitenv(INDEX_ENVIRONMENT
);
78 if (!git_index_file
) {
79 git_index_file
= xmalloc(strlen(git_dir
) + 7);
80 sprintf(git_index_file
, "%s/index", git_dir
);
84 char *get_object_directory(void)
88 return git_object_dir
;
91 char *get_refs_directory(void)
98 char *get_index_file(void)
102 return git_index_file
;
105 char *git_path(const char *fmt
, ...)
107 static char pathname
[PATH_MAX
], *ret
;
113 len
= strlen(git_dir
);
114 if (len
> PATH_MAX
-100)
116 memcpy(pathname
, git_dir
, len
);
117 if (len
&& git_dir
[len
-1] != '/')
118 pathname
[len
++] = '/';
120 vsnprintf(pathname
+ len
, sizeof(pathname
) - len
, fmt
, args
);
125 if (!memcmp(pathname
, "./", 2)) {
133 int safe_create_leading_directories(char *path
)
138 pos
= strchr(pos
, '/');
142 if (mkdir(path
, 0777) < 0)
143 if (errno
!= EEXIST
) {
152 int get_sha1(const char *str
, unsigned char *sha1
)
154 static const char *prefix
[] = {
164 if (!get_sha1_hex(str
, sha1
))
167 for (p
= prefix
; *p
; p
++) {
168 char * pathname
= git_path("%s/%s", *p
, str
);
169 if (!get_sha1_file(pathname
, sha1
))
176 char * sha1_to_hex(const unsigned char *sha1
)
178 static char buffer
[50];
179 static const char hex
[] = "0123456789abcdef";
183 for (i
= 0; i
< 20; i
++) {
184 unsigned int val
= *sha1
++;
185 *buf
++ = hex
[val
>> 4];
186 *buf
++ = hex
[val
& 0xf];
191 static void fill_sha1_path(char *pathbuf
, const unsigned char *sha1
)
194 for (i
= 0; i
< 20; i
++) {
195 static char hex
[] = "0123456789abcdef";
196 unsigned int val
= sha1
[i
];
197 char *pos
= pathbuf
+ i
*2 + (i
> 0);
198 *pos
++ = hex
[val
>> 4];
199 *pos
= hex
[val
& 0xf];
204 * NOTE! This returns a statically allocated buffer, so you have to be
205 * careful about using it. Do a "strdup()" if you need to save the
208 * Also note that this returns the location for creating. Reading
209 * SHA1 file can happen from any alternate directory listed in the
210 * DB_ENVIRONMENT environment variable if it is not found in
211 * the primary object database.
213 char *sha1_file_name(const unsigned char *sha1
)
215 static char *name
, *base
;
218 const char *sha1_file_directory
= get_object_directory();
219 int len
= strlen(sha1_file_directory
);
220 base
= xmalloc(len
+ 60);
221 memcpy(base
, sha1_file_directory
, len
);
222 memset(base
+len
, 0, 60);
225 name
= base
+ len
+ 1;
227 fill_sha1_path(name
, sha1
);
231 struct alternate_object_database
*alt_odb
;
234 * Prepare alternate object database registry.
235 * alt_odb points at an array of struct alternate_object_database.
236 * This array is terminated with an element that has both its base
237 * and name set to NULL. alt_odb[n] comes from n'th non-empty
238 * element from colon separated ALTERNATE_DB_ENVIRONMENT environment
239 * variable, and its base points at a statically allocated buffer
240 * that contains "/the/directory/corresponding/to/.git/objects/...",
241 * while its name points just after the slash at the end of
242 * ".git/objects/" in the example above, and has enough space to hold
243 * 40-byte hex SHA1, an extra slash for the first level indirection,
244 * and the terminating NUL.
245 * This function allocates the alt_odb array and all the strings
246 * pointed by base fields of the array elements with one xmalloc();
247 * the string pool immediately follows the array.
249 void prepare_alt_odb(void)
252 const char *cp
, *last
;
254 const char *alt
= gitenv(ALTERNATE_DB_ENVIRONMENT
) ? : "";
258 /* The first pass counts how large an area to allocate to
259 * hold the entire alt_odb structure, including array of
260 * structs and path buffers for them. The second pass fills
261 * the structure and prepares the path buffers for use by
264 for (totlen
= pass
= 0; pass
< 2; pass
++) {
268 cp
= strchr(last
, ':') ? : last
+ strlen(last
);
270 /* 43 = 40-byte + 2 '/' + terminating NUL */
271 int pfxlen
= cp
- last
;
272 int entlen
= pfxlen
+ 43;
276 alt_odb
[i
].base
= op
;
277 alt_odb
[i
].name
= op
+ pfxlen
+ 1;
278 memcpy(op
, last
, pfxlen
);
279 op
[pfxlen
] = op
[pfxlen
+ 3] = '/';
285 while (*cp
&& *cp
== ':')
291 alt_odb
= xmalloc(sizeof(*alt_odb
) * (i
+ 1) + totlen
);
292 alt_odb
[i
].base
= alt_odb
[i
].name
= NULL
;
293 op
= (char*)(&alt_odb
[i
+1]);
297 static char *find_sha1_file(const unsigned char *sha1
, struct stat
*st
)
300 char *name
= sha1_file_name(sha1
);
305 for (i
= 0; (name
= alt_odb
[i
].name
) != NULL
; i
++) {
306 fill_sha1_path(name
, sha1
);
307 if (!stat(alt_odb
[i
].base
, st
))
308 return alt_odb
[i
].base
;
313 #define PACK_MAX_SZ (1<<26)
314 static int pack_used_ctr
;
315 static unsigned long pack_mapped
;
316 struct packed_git
*packed_git
;
318 static int check_packed_git_idx(const char *path
, unsigned long *idx_size_
,
323 unsigned long idx_size
;
325 int fd
= open(path
, O_RDONLY
);
329 if (fstat(fd
, &st
)) {
333 idx_size
= st
.st_size
;
334 idx_map
= mmap(NULL
, idx_size
, PROT_READ
, MAP_PRIVATE
, fd
, 0);
336 if (idx_map
== MAP_FAILED
)
341 *idx_size_
= idx_size
;
343 /* check index map */
344 if (idx_size
< 4*256 + 20 + 20)
345 return error("index file too small");
347 for (i
= 0; i
< 256; i
++) {
348 unsigned int n
= ntohl(index
[i
]);
350 return error("non-monotonic index");
356 * - 256 index entries 4 bytes each
357 * - 24-byte entries * nr (20-byte sha1 + 4-byte offset)
358 * - 20-byte SHA1 of the packfile
359 * - 20-byte SHA1 file checksum
361 if (idx_size
!= 4*256 + nr
* 24 + 20 + 20)
362 return error("wrong index file size");
367 static int unuse_one_packed_git(void)
369 struct packed_git
*p
, *lru
= NULL
;
371 for (p
= packed_git
; p
; p
= p
->next
) {
372 if (p
->pack_use_cnt
|| !p
->pack_base
)
374 if (!lru
|| p
->pack_last_used
< lru
->pack_last_used
)
379 munmap(lru
->pack_base
, lru
->pack_size
);
380 lru
->pack_base
= NULL
;
384 void unuse_packed_git(struct packed_git
*p
)
389 int use_packed_git(struct packed_git
*p
)
396 pack_mapped
+= p
->pack_size
;
397 while (PACK_MAX_SZ
< pack_mapped
&& unuse_one_packed_git())
399 fd
= open(p
->pack_name
, O_RDONLY
);
401 die("packfile %s cannot be opened", p
->pack_name
);
402 if (fstat(fd
, &st
)) {
404 die("packfile %s cannot be opened", p
->pack_name
);
406 if (st
.st_size
!= p
->pack_size
)
407 die("packfile %s size mismatch.", p
->pack_name
);
408 map
= mmap(NULL
, p
->pack_size
, PROT_READ
, MAP_PRIVATE
, fd
, 0);
410 if (map
== MAP_FAILED
)
411 die("packfile %s cannot be mapped.", p
->pack_name
);
414 /* Check if the pack file matches with the index file.
417 if (memcmp((char*)(p
->index_base
) + p
->index_size
- 40,
418 p
->pack_base
+ p
->pack_size
- 20, 20))
419 die("packfile %s does not match index.", p
->pack_name
);
421 p
->pack_last_used
= pack_used_ctr
++;
426 struct packed_git
*add_packed_git(char *path
, int path_len
)
429 struct packed_git
*p
;
430 unsigned long idx_size
;
433 if (check_packed_git_idx(path
, &idx_size
, &idx_map
))
436 /* do we have a corresponding .pack file? */
437 strcpy(path
+ path_len
- 4, ".pack");
438 if (stat(path
, &st
) || !S_ISREG(st
.st_mode
)) {
439 munmap(idx_map
, idx_size
);
442 /* ok, it looks sane as far as we can check without
443 * actually mapping the pack file.
445 p
= xmalloc(sizeof(*p
) + path_len
+ 2);
446 strcpy(p
->pack_name
, path
);
447 p
->index_size
= idx_size
;
448 p
->pack_size
= st
.st_size
;
449 p
->index_base
= idx_map
;
452 p
->pack_last_used
= 0;
457 static void prepare_packed_git_one(char *objdir
)
464 sprintf(path
, "%s/pack", objdir
);
470 while ((de
= readdir(dir
)) != NULL
) {
471 int namelen
= strlen(de
->d_name
);
472 struct packed_git
*p
;
474 if (strcmp(de
->d_name
+ namelen
- 4, ".idx"))
477 /* we have .idx. Is it a file we can map? */
478 strcpy(path
+ len
, de
->d_name
);
479 p
= add_packed_git(path
, len
+ namelen
);
482 p
->next
= packed_git
;
488 void prepare_packed_git(void)
491 static int run_once
= 0;
496 prepare_packed_git_one(get_object_directory());
498 for (i
= 0; alt_odb
[i
].base
!= NULL
; i
++) {
499 alt_odb
[i
].name
[0] = 0;
500 prepare_packed_git_one(alt_odb
[i
].base
);
504 int check_sha1_signature(const unsigned char *sha1
, void *map
, unsigned long size
, const char *type
)
507 unsigned char real_sha1
[20];
511 SHA1_Update(&c
, header
, 1+sprintf(header
, "%s %lu", type
, size
));
512 SHA1_Update(&c
, map
, size
);
513 SHA1_Final(real_sha1
, &c
);
514 return memcmp(sha1
, real_sha1
, 20) ? -1 : 0;
517 static void *map_sha1_file_internal(const unsigned char *sha1
,
524 char *filename
= find_sha1_file(sha1
, &st
);
528 error("cannot map sha1 file %s", sha1_to_hex(sha1
));
532 fd
= open(filename
, O_RDONLY
| sha1_file_open_flag
);
534 /* See if it works without O_NOATIME */
535 switch (sha1_file_open_flag
) {
537 fd
= open(filename
, O_RDONLY
);
547 /* If it failed once, it will probably fail again.
548 * Stop using O_NOATIME
550 sha1_file_open_flag
= 0;
552 map
= mmap(NULL
, st
.st_size
, PROT_READ
, MAP_PRIVATE
, fd
, 0);
554 if (-1 == (int)(long)map
)
560 void *map_sha1_file(const unsigned char *sha1
, unsigned long *size
)
562 return map_sha1_file_internal(sha1
, size
, 1);
565 int unpack_sha1_header(z_stream
*stream
, void *map
, unsigned long mapsize
, void *buffer
, unsigned long size
)
567 /* Get the data stream */
568 memset(stream
, 0, sizeof(*stream
));
569 stream
->next_in
= map
;
570 stream
->avail_in
= mapsize
;
571 stream
->next_out
= buffer
;
572 stream
->avail_out
= size
;
575 return inflate(stream
, 0);
578 static void *unpack_sha1_rest(z_stream
*stream
, void *buffer
, unsigned long size
)
580 int bytes
= strlen(buffer
) + 1;
581 unsigned char *buf
= xmalloc(1+size
);
583 memcpy(buf
, buffer
+ bytes
, stream
->total_out
- bytes
);
584 bytes
= stream
->total_out
- bytes
;
586 stream
->next_out
= buf
+ bytes
;
587 stream
->avail_out
= size
- bytes
;
588 while (inflate(stream
, Z_FINISH
) == Z_OK
)
597 * We used to just use "sscanf()", but that's actually way
598 * too permissive for what we want to check. So do an anal
599 * object header parse by hand.
601 int parse_sha1_header(char *hdr
, char *type
, unsigned long *sizep
)
607 * The type can be at most ten bytes (including the
608 * terminating '\0' that we add), and is followed by
623 * The length must follow immediately, and be in canonical
624 * decimal format (ie "010" is not valid).
631 unsigned long c
= *hdr
- '0';
635 size
= size
* 10 + c
;
641 * The length must be followed by a zero byte
643 return *hdr
? -1 : 0;
646 void * unpack_sha1_file(void *map
, unsigned long mapsize
, char *type
, unsigned long *size
)
652 ret
= unpack_sha1_header(&stream
, map
, mapsize
, hdr
, sizeof(hdr
));
653 if (ret
< Z_OK
|| parse_sha1_header(hdr
, type
, size
) < 0)
656 return unpack_sha1_rest(&stream
, hdr
, *size
);
659 /* forward declaration for a mutually recursive function */
660 static int packed_object_info(struct pack_entry
*entry
,
661 char *type
, unsigned long *sizep
);
663 static int packed_delta_info(unsigned char *base_sha1
,
664 unsigned long delta_size
,
667 unsigned long *sizep
,
668 struct packed_git
*p
)
670 struct pack_entry base_ent
;
673 die("truncated pack file");
675 /* The base entry _must_ be in the same pack */
676 if (!find_pack_entry_one(base_sha1
, &base_ent
, p
))
677 die("failed to find delta-pack base object %s",
678 sha1_to_hex(base_sha1
));
680 /* We choose to only get the type of the base object and
681 * ignore potentially corrupt pack file that expects the delta
682 * based on a base with a wrong size. This saves tons of
686 if (packed_object_info(&base_ent
, type
, NULL
))
687 die("cannot get info for delta-pack base");
690 const unsigned char *data
;
691 unsigned char delta_head
[64];
692 unsigned long result_size
;
696 memset(&stream
, 0, sizeof(stream
));
698 data
= stream
.next_in
= base_sha1
+ 20;
699 stream
.avail_in
= left
- 20;
700 stream
.next_out
= delta_head
;
701 stream
.avail_out
= sizeof(delta_head
);
703 inflateInit(&stream
);
704 st
= inflate(&stream
, Z_FINISH
);
706 if ((st
!= Z_STREAM_END
) &&
707 stream
.total_out
!= sizeof(delta_head
))
708 die("delta data unpack-initial failed");
710 /* Examine the initial part of the delta to figure out
714 get_delta_hdr_size(&data
); /* ignore base size */
716 /* Read the result size */
717 result_size
= get_delta_hdr_size(&data
);
718 *sizep
= result_size
;
723 static unsigned long unpack_object_header(struct packed_git
*p
, unsigned long offset
,
724 enum object_type
*type
, unsigned long *sizep
)
727 unsigned char *pack
, c
;
730 if (offset
>= p
->pack_size
)
731 die("object offset outside of pack file");
733 pack
= p
->pack_base
+ offset
;
736 *type
= (c
>> 4) & 7;
740 if (offset
>= p
->pack_size
)
741 die("object offset outside of pack file");
744 size
+= (c
& 0x7f) << shift
;
751 void packed_object_info_detail(struct pack_entry
*e
,
754 unsigned long *store_size
,
755 int *delta_chain_length
,
756 unsigned char *base_sha1
)
758 struct packed_git
*p
= e
->p
;
759 unsigned long offset
, left
;
761 enum object_type kind
;
763 offset
= unpack_object_header(p
, e
->offset
, &kind
, size
);
764 pack
= p
->pack_base
+ offset
;
765 left
= p
->pack_size
- offset
;
766 if (kind
!= OBJ_DELTA
)
767 *delta_chain_length
= 0;
769 int chain_length
= 0;
770 memcpy(base_sha1
, pack
, 20);
772 struct pack_entry base_ent
;
775 find_pack_entry_one(pack
, &base_ent
, p
);
776 offset
= unpack_object_header(p
, base_ent
.offset
,
778 pack
= p
->pack_base
+ offset
;
780 } while (kind
== OBJ_DELTA
);
781 *delta_chain_length
= chain_length
;
785 strcpy(type
, "commit");
788 strcpy(type
, "tree");
791 strcpy(type
, "blob");
797 die("corrupted pack file");
799 *store_size
= 0; /* notyet */
802 static int packed_object_info(struct pack_entry
*entry
,
803 char *type
, unsigned long *sizep
)
805 struct packed_git
*p
= entry
->p
;
806 unsigned long offset
, size
, left
;
808 enum object_type kind
;
811 if (use_packed_git(p
))
812 die("cannot map packed file");
814 offset
= unpack_object_header(p
, entry
->offset
, &kind
, &size
);
815 pack
= p
->pack_base
+ offset
;
816 left
= p
->pack_size
- offset
;
820 retval
= packed_delta_info(pack
, size
, left
, type
, sizep
, p
);
824 strcpy(type
, "commit");
827 strcpy(type
, "tree");
830 strcpy(type
, "blob");
836 die("corrupted pack file");
844 /* forward declaration for a mutually recursive function */
845 static void *unpack_entry(struct pack_entry
*, char *, unsigned long *);
847 static void *unpack_delta_entry(unsigned char *base_sha1
,
848 unsigned long delta_size
,
851 unsigned long *sizep
,
852 struct packed_git
*p
)
854 struct pack_entry base_ent
;
855 void *data
, *delta_data
, *result
, *base
;
856 unsigned long data_size
, result_size
, base_size
;
861 die("truncated pack file");
862 data
= base_sha1
+ 20;
863 data_size
= left
- 20;
864 delta_data
= xmalloc(delta_size
);
866 memset(&stream
, 0, sizeof(stream
));
868 stream
.next_in
= data
;
869 stream
.avail_in
= data_size
;
870 stream
.next_out
= delta_data
;
871 stream
.avail_out
= delta_size
;
873 inflateInit(&stream
);
874 st
= inflate(&stream
, Z_FINISH
);
876 if ((st
!= Z_STREAM_END
) || stream
.total_out
!= delta_size
)
877 die("delta data unpack failed");
879 /* The base entry _must_ be in the same pack */
880 if (!find_pack_entry_one(base_sha1
, &base_ent
, p
))
881 die("failed to find delta-pack base object %s",
882 sha1_to_hex(base_sha1
));
883 base
= unpack_entry_gently(&base_ent
, type
, &base_size
);
885 die("failed to read delta-pack base object %s",
886 sha1_to_hex(base_sha1
));
887 result
= patch_delta(base
, base_size
,
888 delta_data
, delta_size
,
891 die("failed to apply delta");
894 *sizep
= result_size
;
898 static void *unpack_non_delta_entry(unsigned char *data
,
904 unsigned char *buffer
;
906 buffer
= xmalloc(size
+ 1);
908 memset(&stream
, 0, sizeof(stream
));
909 stream
.next_in
= data
;
910 stream
.avail_in
= left
;
911 stream
.next_out
= buffer
;
912 stream
.avail_out
= size
;
914 inflateInit(&stream
);
915 st
= inflate(&stream
, Z_FINISH
);
917 if ((st
!= Z_STREAM_END
) || stream
.total_out
!= size
) {
925 static void *unpack_entry(struct pack_entry
*entry
,
926 char *type
, unsigned long *sizep
)
928 struct packed_git
*p
= entry
->p
;
931 if (use_packed_git(p
))
932 die("cannot map packed file");
933 retval
= unpack_entry_gently(entry
, type
, sizep
);
936 die("corrupted pack file");
940 /* The caller is responsible for use_packed_git()/unuse_packed_git() pair */
941 void *unpack_entry_gently(struct pack_entry
*entry
,
942 char *type
, unsigned long *sizep
)
944 struct packed_git
*p
= entry
->p
;
945 unsigned long offset
, size
, left
;
947 enum object_type kind
;
950 offset
= unpack_object_header(p
, entry
->offset
, &kind
, &size
);
951 pack
= p
->pack_base
+ offset
;
952 left
= p
->pack_size
- offset
;
955 retval
= unpack_delta_entry(pack
, size
, left
, type
, sizep
, p
);
958 strcpy(type
, "commit");
961 strcpy(type
, "tree");
964 strcpy(type
, "blob");
973 retval
= unpack_non_delta_entry(pack
, size
, left
);
977 int num_packed_objects(const struct packed_git
*p
)
979 /* See check_packed_git_idx() */
980 return (p
->index_size
- 20 - 20 - 4*256) / 24;
983 int nth_packed_object_sha1(const struct packed_git
*p
, int n
,
986 void *index
= p
->index_base
+ 256;
987 if (n
< 0 || num_packed_objects(p
) <= n
)
989 memcpy(sha1
, (index
+ 24 * n
+ 4), 20);
993 int find_pack_entry_one(const unsigned char *sha1
,
994 struct pack_entry
*e
, struct packed_git
*p
)
996 unsigned int *level1_ofs
= p
->index_base
;
997 int hi
= ntohl(level1_ofs
[*sha1
]);
998 int lo
= ((*sha1
== 0x0) ? 0 : ntohl(level1_ofs
[*sha1
- 1]));
999 void *index
= p
->index_base
+ 256;
1002 int mi
= (lo
+ hi
) / 2;
1003 int cmp
= memcmp(index
+ 24 * mi
+ 4, sha1
, 20);
1005 e
->offset
= ntohl(*((int*)(index
+ 24 * mi
)));
1006 memcpy(e
->sha1
, sha1
, 20);
1018 static int find_pack_entry(const unsigned char *sha1
, struct pack_entry
*e
)
1020 struct packed_git
*p
;
1021 prepare_packed_git();
1023 for (p
= packed_git
; p
; p
= p
->next
) {
1024 if (find_pack_entry_one(sha1
, e
, p
))
1030 int sha1_object_info(const unsigned char *sha1
, char *type
, unsigned long *sizep
)
1033 unsigned long mapsize
, size
;
1038 map
= map_sha1_file_internal(sha1
, &mapsize
, 0);
1040 struct pack_entry e
;
1042 if (!find_pack_entry(sha1
, &e
))
1043 return error("unable to find %s", sha1_to_hex(sha1
));
1044 return packed_object_info(&e
, type
, sizep
);
1046 if (unpack_sha1_header(&stream
, map
, mapsize
, hdr
, sizeof(hdr
)) < 0)
1047 status
= error("unable to unpack %s header",
1049 if (parse_sha1_header(hdr
, type
, &size
) < 0)
1050 status
= error("unable to parse %s header", sha1_to_hex(sha1
));
1056 inflateEnd(&stream
);
1057 munmap(map
, mapsize
);
1061 static void *read_packed_sha1(const unsigned char *sha1
, char *type
, unsigned long *size
)
1063 struct pack_entry e
;
1065 if (!find_pack_entry(sha1
, &e
)) {
1066 error("cannot read sha1_file for %s", sha1_to_hex(sha1
));
1069 return unpack_entry(&e
, type
, size
);
1072 void * read_sha1_file(const unsigned char *sha1
, char *type
, unsigned long *size
)
1074 unsigned long mapsize
;
1077 map
= map_sha1_file_internal(sha1
, &mapsize
, 0);
1079 buf
= unpack_sha1_file(map
, mapsize
, type
, size
);
1080 munmap(map
, mapsize
);
1083 return read_packed_sha1(sha1
, type
, size
);
1086 void *read_object_with_reference(const unsigned char *sha1
,
1087 const char *required_type
,
1088 unsigned long *size
,
1089 unsigned char *actual_sha1_return
)
1093 unsigned long isize
;
1094 unsigned char actual_sha1
[20];
1096 memcpy(actual_sha1
, sha1
, 20);
1098 int ref_length
= -1;
1099 const char *ref_type
= NULL
;
1101 buffer
= read_sha1_file(actual_sha1
, type
, &isize
);
1104 if (!strcmp(type
, required_type
)) {
1106 if (actual_sha1_return
)
1107 memcpy(actual_sha1_return
, actual_sha1
, 20);
1110 /* Handle references */
1111 else if (!strcmp(type
, "commit"))
1113 else if (!strcmp(type
, "tag"))
1114 ref_type
= "object ";
1119 ref_length
= strlen(ref_type
);
1121 if (memcmp(buffer
, ref_type
, ref_length
) ||
1122 get_sha1_hex(buffer
+ ref_length
, actual_sha1
)) {
1126 /* Now we have the ID of the referred-to object in
1127 * actual_sha1. Check again. */
1131 static char *write_sha1_file_prepare(void *buf
,
1134 unsigned char *sha1
,
1140 /* Generate the header */
1141 *hdrlen
= sprintf((char *)hdr
, "%s %lu", type
, len
)+1;
1145 SHA1_Update(&c
, hdr
, *hdrlen
);
1146 SHA1_Update(&c
, buf
, len
);
1147 SHA1_Final(sha1
, &c
);
1149 return sha1_file_name(sha1
);
1152 int write_sha1_file(void *buf
, unsigned long len
, const char *type
, unsigned char *returnsha1
)
1155 unsigned char *compressed
;
1157 unsigned char sha1
[20];
1159 static char tmpfile
[PATH_MAX
];
1160 unsigned char hdr
[50];
1161 int fd
, hdrlen
, ret
;
1163 /* Normally if we have it in the pack then we do not bother writing
1164 * it out into .git/objects/??/?{38} file.
1166 filename
= write_sha1_file_prepare(buf
, len
, type
, sha1
, hdr
, &hdrlen
);
1168 memcpy(returnsha1
, sha1
, 20);
1169 if (has_sha1_file(sha1
))
1171 fd
= open(filename
, O_RDONLY
);
1174 * FIXME!!! We might do collision checking here, but we'd
1175 * need to uncompress the old file and check it. Later.
1181 if (errno
!= ENOENT
) {
1182 fprintf(stderr
, "sha1 file %s: %s", filename
, strerror(errno
));
1186 snprintf(tmpfile
, sizeof(tmpfile
), "%s/obj_XXXXXX", get_object_directory());
1188 fd
= mkstemp(tmpfile
);
1190 fprintf(stderr
, "unable to create temporary sha1 filename %s: %s", tmpfile
, strerror(errno
));
1195 memset(&stream
, 0, sizeof(stream
));
1196 deflateInit(&stream
, Z_BEST_COMPRESSION
);
1197 size
= deflateBound(&stream
, len
+hdrlen
);
1198 compressed
= xmalloc(size
);
1201 stream
.next_out
= compressed
;
1202 stream
.avail_out
= size
;
1204 /* First header.. */
1205 stream
.next_in
= hdr
;
1206 stream
.avail_in
= hdrlen
;
1207 while (deflate(&stream
, 0) == Z_OK
)
1210 /* Then the data itself.. */
1211 stream
.next_in
= buf
;
1212 stream
.avail_in
= len
;
1213 while (deflate(&stream
, Z_FINISH
) == Z_OK
)
1215 deflateEnd(&stream
);
1216 size
= stream
.total_out
;
1218 if (write(fd
, compressed
, size
) != size
)
1219 die("unable to write file");
1224 ret
= link(tmpfile
, filename
);
1229 * Coda hack - coda doesn't like cross-directory links,
1230 * so we fall back to a rename, which will mean that it
1231 * won't be able to check collisions, but that's not a
1234 * When this succeeds, we just return 0. We have nothing
1237 if (ret
== EXDEV
&& !rename(tmpfile
, filename
))
1242 if (ret
!= EEXIST
) {
1243 fprintf(stderr
, "unable to write sha1 filename %s: %s", filename
, strerror(ret
));
1246 /* FIXME!!! Collision check here ? */
1252 int write_sha1_from_fd(const unsigned char *sha1
, int fd
)
1254 char *filename
= sha1_file_name(sha1
);
1258 unsigned char real_sha1
[20];
1259 unsigned char buf
[4096];
1260 unsigned char discard
[4096];
1264 local
= open(filename
, O_WRONLY
| O_CREAT
| O_EXCL
, 0666);
1267 return error("Couldn't open %s\n", filename
);
1269 memset(&stream
, 0, sizeof(stream
));
1271 inflateInit(&stream
);
1277 size
= read(fd
, buf
, 4096);
1282 return error("Connection closed?");
1283 perror("Reading from connection");
1286 write(local
, buf
, size
);
1287 stream
.avail_in
= size
;
1288 stream
.next_in
= buf
;
1290 stream
.next_out
= discard
;
1291 stream
.avail_out
= sizeof(discard
);
1292 ret
= inflate(&stream
, Z_SYNC_FLUSH
);
1293 SHA1_Update(&c
, discard
, sizeof(discard
) -
1295 } while (stream
.avail_in
&& ret
== Z_OK
);
1297 } while (ret
== Z_OK
);
1298 inflateEnd(&stream
);
1301 SHA1_Final(real_sha1
, &c
);
1302 if (ret
!= Z_STREAM_END
) {
1304 return error("File %s corrupted", sha1_to_hex(sha1
));
1306 if (memcmp(sha1
, real_sha1
, 20)) {
1308 return error("File %s has bad hash\n", sha1_to_hex(sha1
));
1314 int has_sha1_pack(const unsigned char *sha1
)
1316 struct pack_entry e
;
1317 return find_pack_entry(sha1
, &e
);
1320 int has_sha1_file(const unsigned char *sha1
)
1323 struct pack_entry e
;
1325 if (find_sha1_file(sha1
, &st
))
1327 return find_pack_entry(sha1
, &e
);
1330 int index_fd(unsigned char *sha1
, int fd
, struct stat
*st
)
1332 unsigned long size
= st
->st_size
;
1338 buf
= mmap(NULL
, size
, PROT_READ
, MAP_PRIVATE
, fd
, 0);
1340 if ((int)(long)buf
== -1)
1343 ret
= write_sha1_file(buf
, size
, "blob", sha1
);