2 * GIT - The information manager from hell
4 * Copyright (C) Linus Torvalds, 2005
6 * This handles basic git sha1 object files - packing, unpacking,
13 #if defined(__linux__) && (defined(__i386__) || defined(__PPC__))
14 #define O_NOATIME 01000000
20 static unsigned int sha1_file_open_flag
= O_NOATIME
;
22 static unsigned hexval(char c
)
24 if (c
>= '0' && c
<= '9')
26 if (c
>= 'a' && c
<= 'f')
28 if (c
>= 'A' && c
<= 'F')
33 int get_sha1_hex(const char *hex
, unsigned char *sha1
)
36 for (i
= 0; i
< 20; i
++) {
37 unsigned int val
= (hexval(hex
[0]) << 4) | hexval(hex
[1]);
46 static int get_sha1_file(const char *path
, unsigned char *result
)
49 int fd
= open(path
, O_RDONLY
);
54 len
= read(fd
, buffer
, sizeof(buffer
));
58 return get_sha1_hex(buffer
, result
);
61 static char *git_dir
, *git_object_dir
, *git_index_file
;
62 static void setup_git_env(void)
64 git_dir
= gitenv(GIT_DIR_ENVIRONMENT
);
66 git_dir
= DEFAULT_GIT_DIR_ENVIRONMENT
;
67 git_object_dir
= gitenv(DB_ENVIRONMENT
);
68 if (!git_object_dir
) {
69 git_object_dir
= xmalloc(strlen(git_dir
) + 9);
70 sprintf(git_object_dir
, "%s/objects", git_dir
);
72 git_index_file
= gitenv(INDEX_ENVIRONMENT
);
73 if (!git_index_file
) {
74 git_index_file
= xmalloc(strlen(git_dir
) + 7);
75 sprintf(git_index_file
, "%s/index", git_dir
);
79 char *get_object_directory(void)
83 return git_object_dir
;
86 char *get_index_file(void)
90 return git_index_file
;
93 int get_sha1(const char *str
, unsigned char *sha1
)
95 static char pathname
[PATH_MAX
];
96 static const char *prefix
[] = {
106 if (!get_sha1_hex(str
, sha1
))
111 for (p
= prefix
; *p
; p
++) {
112 snprintf(pathname
, sizeof(pathname
), "%s/%s/%s",
114 if (!get_sha1_file(pathname
, sha1
))
121 char * sha1_to_hex(const unsigned char *sha1
)
123 static char buffer
[50];
124 static const char hex
[] = "0123456789abcdef";
128 for (i
= 0; i
< 20; i
++) {
129 unsigned int val
= *sha1
++;
130 *buf
++ = hex
[val
>> 4];
131 *buf
++ = hex
[val
& 0xf];
136 static void fill_sha1_path(char *pathbuf
, const unsigned char *sha1
)
139 for (i
= 0; i
< 20; i
++) {
140 static char hex
[] = "0123456789abcdef";
141 unsigned int val
= sha1
[i
];
142 char *pos
= pathbuf
+ i
*2 + (i
> 0);
143 *pos
++ = hex
[val
>> 4];
144 *pos
= hex
[val
& 0xf];
149 * NOTE! This returns a statically allocated buffer, so you have to be
150 * careful about using it. Do a "strdup()" if you need to save the
153 * Also note that this returns the location for creating. Reading
154 * SHA1 file can happen from any alternate directory listed in the
155 * DB_ENVIRONMENT environment variable if it is not found in
156 * the primary object database.
158 char *sha1_file_name(const unsigned char *sha1
)
160 static char *name
, *base
;
163 const char *sha1_file_directory
= get_object_directory();
164 int len
= strlen(sha1_file_directory
);
165 base
= xmalloc(len
+ 60);
166 memcpy(base
, sha1_file_directory
, len
);
167 memset(base
+len
, 0, 60);
170 name
= base
+ len
+ 1;
172 fill_sha1_path(name
, sha1
);
176 static struct alternate_object_database
{
182 * Prepare alternate object database registry.
183 * alt_odb points at an array of struct alternate_object_database.
184 * This array is terminated with an element that has both its base
185 * and name set to NULL. alt_odb[n] comes from n'th non-empty
186 * element from colon separated ALTERNATE_DB_ENVIRONMENT environment
187 * variable, and its base points at a statically allocated buffer
188 * that contains "/the/directory/corresponding/to/.git/objects/...",
189 * while its name points just after the slash at the end of
190 * ".git/objects/" in the example above, and has enough space to hold
191 * 40-byte hex SHA1, an extra slash for the first level indirection,
192 * and the terminating NUL.
193 * This function allocates the alt_odb array and all the strings
194 * pointed by base fields of the array elements with one xmalloc();
195 * the string pool immediately follows the array.
197 static void prepare_alt_odb(void)
200 const char *cp
, *last
;
202 const char *alt
= gitenv(ALTERNATE_DB_ENVIRONMENT
) ? : "";
204 /* The first pass counts how large an area to allocate to
205 * hold the entire alt_odb structure, including array of
206 * structs and path buffers for them. The second pass fills
207 * the structure and prepares the path buffers for use by
210 for (totlen
= pass
= 0; pass
< 2; pass
++) {
214 cp
= strchr(last
, ':') ? : last
+ strlen(last
);
216 /* 43 = 40-byte + 2 '/' + terminating NUL */
217 int pfxlen
= cp
- last
;
218 int entlen
= pfxlen
+ 43;
222 alt_odb
[i
].base
= op
;
223 alt_odb
[i
].name
= op
+ pfxlen
+ 1;
224 memcpy(op
, last
, pfxlen
);
225 op
[pfxlen
] = op
[pfxlen
+ 3] = '/';
231 while (*cp
&& *cp
== ':')
237 alt_odb
= xmalloc(sizeof(*alt_odb
) * (i
+ 1) + totlen
);
238 alt_odb
[i
].base
= alt_odb
[i
].name
= NULL
;
239 op
= (char*)(&alt_odb
[i
+1]);
243 static char *find_sha1_file(const unsigned char *sha1
, struct stat
*st
)
246 char *name
= sha1_file_name(sha1
);
252 for (i
= 0; (name
= alt_odb
[i
].name
) != NULL
; i
++) {
253 fill_sha1_path(name
, sha1
);
254 if (!stat(alt_odb
[i
].base
, st
))
255 return alt_odb
[i
].base
;
260 int check_sha1_signature(unsigned char *sha1
, void *map
, unsigned long size
, const char *type
)
263 unsigned char real_sha1
[20];
267 SHA1_Update(&c
, header
, 1+sprintf(header
, "%s %lu", type
, size
));
268 SHA1_Update(&c
, map
, size
);
269 SHA1_Final(real_sha1
, &c
);
270 return memcmp(sha1
, real_sha1
, 20) ? -1 : 0;
273 void *map_sha1_file(const unsigned char *sha1
, unsigned long *size
)
278 char *filename
= find_sha1_file(sha1
, &st
);
281 error("cannot map sha1 file %s", sha1_to_hex(sha1
));
285 fd
= open(filename
, O_RDONLY
| sha1_file_open_flag
);
287 /* See if it works without O_NOATIME */
288 switch (sha1_file_open_flag
) {
290 fd
= open(filename
, O_RDONLY
);
299 /* If it failed once, it will probably fail again. Stop using O_NOATIME */
300 sha1_file_open_flag
= 0;
302 map
= mmap(NULL
, st
.st_size
, PROT_READ
, MAP_PRIVATE
, fd
, 0);
304 if (-1 == (int)(long)map
)
310 int unpack_sha1_header(z_stream
*stream
, void *map
, unsigned long mapsize
, void *buffer
, unsigned long size
)
312 /* Get the data stream */
313 memset(stream
, 0, sizeof(*stream
));
314 stream
->next_in
= map
;
315 stream
->avail_in
= mapsize
;
316 stream
->next_out
= buffer
;
317 stream
->avail_out
= size
;
320 return inflate(stream
, 0);
323 void *unpack_sha1_rest(z_stream
*stream
, void *buffer
, unsigned long size
)
325 int bytes
= strlen(buffer
) + 1;
326 char *buf
= xmalloc(1+size
);
328 memcpy(buf
, buffer
+ bytes
, stream
->total_out
- bytes
);
329 bytes
= stream
->total_out
- bytes
;
331 stream
->next_out
= buf
+ bytes
;
332 stream
->avail_out
= size
- bytes
;
333 while (inflate(stream
, Z_FINISH
) == Z_OK
)
342 * We used to just use "sscanf()", but that's actually way
343 * too permissive for what we want to check. So do an anal
344 * object header parse by hand.
346 int parse_sha1_header(char *hdr
, char *type
, unsigned long *sizep
)
352 * The type can be at most ten bytes (including the
353 * terminating '\0' that we add), and is followed by
368 * The length must follow immediately, and be in canonical
369 * decimal format (ie "010" is not valid).
376 unsigned long c
= *hdr
- '0';
380 size
= size
* 10 + c
;
386 * The length must be followed by a zero byte
388 return *hdr
? -1 : 0;
391 void * unpack_sha1_file(void *map
, unsigned long mapsize
, char *type
, unsigned long *size
)
397 ret
= unpack_sha1_header(&stream
, map
, mapsize
, hdr
, sizeof(hdr
));
398 if (ret
< Z_OK
|| parse_sha1_header(hdr
, type
, size
) < 0)
401 return unpack_sha1_rest(&stream
, hdr
, *size
);
404 int sha1_delta_base(const unsigned char *sha1
, unsigned char *base_sha1
)
407 unsigned long mapsize
, size
;
410 char hdr
[64], type
[20];
411 void *delta_data_head
;
413 map
= map_sha1_file(sha1
, &mapsize
);
416 ret
= unpack_sha1_header(&stream
, map
, mapsize
, hdr
, sizeof(hdr
));
417 if (ret
< Z_OK
|| parse_sha1_header(hdr
, type
, &size
) < 0) {
421 if (strcmp(type
, "delta")) {
426 delta_data_head
= hdr
+ strlen(hdr
) + 1;
428 memcpy(base_sha1
, delta_data_head
, 20);
431 munmap(map
, mapsize
);
435 int sha1_file_size(const unsigned char *sha1
, unsigned long *sizep
)
438 unsigned long mapsize
, size
;
441 char hdr
[64], type
[20];
442 const unsigned char *data
;
446 map
= map_sha1_file(sha1
, &mapsize
);
449 ret
= unpack_sha1_header(&stream
, map
, mapsize
, hdr
, sizeof(hdr
));
451 if (ret
< Z_OK
|| parse_sha1_header(hdr
, type
, &size
) < 0)
453 if (strcmp(type
, "delta")) {
459 /* We are dealing with a delta object. Inflated, the first
460 * 20 bytes hold the base object SHA1, and delta data follows
461 * immediately after it.
463 * The initial part of the delta starts at delta_data_head +
464 * 20. Borrow code from patch-delta to read the result size.
466 data
= hdr
+ strlen(hdr
) + 1 + 20;
468 /* Skip over the source size; we are not interested in
469 * it and we cannot verify it because we do not want
470 * to read the base object.
478 /* Read the result size */
483 size
|= *data
++ << i
;
491 munmap(map
, mapsize
);
495 void * read_sha1_file(const unsigned char *sha1
, char *type
, unsigned long *size
)
497 unsigned long mapsize
;
500 map
= map_sha1_file(sha1
, &mapsize
);
502 buf
= unpack_sha1_file(map
, mapsize
, type
, size
);
503 munmap(map
, mapsize
);
504 if (buf
&& !strcmp(type
, "delta")) {
505 void *ref
= NULL
, *delta
= buf
;
506 unsigned long ref_size
, delta_size
= *size
;
509 ref
= read_sha1_file(delta
, type
, &ref_size
);
511 buf
= patch_delta(ref
, ref_size
,
512 delta
+20, delta_size
-20,
522 void *read_object_with_reference(const unsigned char *sha1
,
523 const char *required_type
,
525 unsigned char *actual_sha1_return
)
530 unsigned char actual_sha1
[20];
532 memcpy(actual_sha1
, sha1
, 20);
535 const char *ref_type
= NULL
;
537 buffer
= read_sha1_file(actual_sha1
, type
, &isize
);
540 if (!strcmp(type
, required_type
)) {
542 if (actual_sha1_return
)
543 memcpy(actual_sha1_return
, actual_sha1
, 20);
546 /* Handle references */
547 else if (!strcmp(type
, "commit"))
549 else if (!strcmp(type
, "tag"))
550 ref_type
= "object ";
555 ref_length
= strlen(ref_type
);
557 if (memcmp(buffer
, ref_type
, ref_length
) ||
558 get_sha1_hex(buffer
+ ref_length
, actual_sha1
)) {
562 /* Now we have the ID of the referred-to object in
563 * actual_sha1. Check again. */
567 int write_sha1_file(void *buf
, unsigned long len
, const char *type
, unsigned char *returnsha1
)
570 unsigned char *compressed
;
572 unsigned char sha1
[20];
575 static char tmpfile
[PATH_MAX
];
576 unsigned char hdr
[50];
579 /* Generate the header */
580 hdrlen
= sprintf((char *)hdr
, "%s %lu", type
, len
)+1;
584 SHA1_Update(&c
, hdr
, hdrlen
);
585 SHA1_Update(&c
, buf
, len
);
586 SHA1_Final(sha1
, &c
);
589 memcpy(returnsha1
, sha1
, 20);
591 filename
= sha1_file_name(sha1
);
592 fd
= open(filename
, O_RDONLY
);
595 * FIXME!!! We might do collision checking here, but we'd
596 * need to uncompress the old file and check it. Later.
602 if (errno
!= ENOENT
) {
603 fprintf(stderr
, "sha1 file %s: %s", filename
, strerror(errno
));
607 snprintf(tmpfile
, sizeof(tmpfile
), "%s/obj_XXXXXX", get_object_directory());
609 fd
= mkstemp(tmpfile
);
611 fprintf(stderr
, "unable to create temporary sha1 filename %s: %s", tmpfile
, strerror(errno
));
616 memset(&stream
, 0, sizeof(stream
));
617 deflateInit(&stream
, Z_BEST_COMPRESSION
);
618 size
= deflateBound(&stream
, len
+hdrlen
);
619 compressed
= xmalloc(size
);
622 stream
.next_out
= compressed
;
623 stream
.avail_out
= size
;
626 stream
.next_in
= hdr
;
627 stream
.avail_in
= hdrlen
;
628 while (deflate(&stream
, 0) == Z_OK
)
631 /* Then the data itself.. */
632 stream
.next_in
= buf
;
633 stream
.avail_in
= len
;
634 while (deflate(&stream
, Z_FINISH
) == Z_OK
)
637 size
= stream
.total_out
;
639 if (write(fd
, compressed
, size
) != size
)
640 die("unable to write file");
645 ret
= link(tmpfile
, filename
);
650 * Coda hack - coda doesn't like cross-directory links,
651 * so we fall back to a rename, which will mean that it
652 * won't be able to check collisions, but that's not a
655 * When this succeeds, we just return 0. We have nothing
658 if (ret
== EXDEV
&& !rename(tmpfile
, filename
))
664 fprintf(stderr
, "unable to write sha1 filename %s: %s", filename
, strerror(ret
));
667 /* FIXME!!! Collision check here ? */
673 int write_sha1_from_fd(const unsigned char *sha1
, int fd
)
675 char *filename
= sha1_file_name(sha1
);
679 unsigned char real_sha1
[20];
680 unsigned char buf
[4096];
681 unsigned char discard
[4096];
685 local
= open(filename
, O_WRONLY
| O_CREAT
| O_EXCL
, 0666);
688 return error("Couldn't open %s\n", filename
);
690 memset(&stream
, 0, sizeof(stream
));
692 inflateInit(&stream
);
698 size
= read(fd
, buf
, 4096);
703 return error("Connection closed?");
704 perror("Reading from connection");
707 write(local
, buf
, size
);
708 stream
.avail_in
= size
;
709 stream
.next_in
= buf
;
711 stream
.next_out
= discard
;
712 stream
.avail_out
= sizeof(discard
);
713 ret
= inflate(&stream
, Z_SYNC_FLUSH
);
714 SHA1_Update(&c
, discard
, sizeof(discard
) -
716 } while (stream
.avail_in
&& ret
== Z_OK
);
718 } while (ret
== Z_OK
);
722 SHA1_Final(real_sha1
, &c
);
723 if (ret
!= Z_STREAM_END
) {
725 return error("File %s corrupted", sha1_to_hex(sha1
));
727 if (memcmp(sha1
, real_sha1
, 20)) {
729 return error("File %s has bad hash\n", sha1_to_hex(sha1
));
735 int has_sha1_file(const unsigned char *sha1
)
738 return !!find_sha1_file(sha1
, &st
);
741 int index_fd(unsigned char *sha1
, int fd
, struct stat
*st
)
743 unsigned long size
= st
->st_size
;
749 buf
= mmap(NULL
, size
, PROT_READ
, MAP_PRIVATE
, fd
, 0);
751 if ((int)(long)buf
== -1)
754 ret
= write_sha1_file(buf
, size
, "blob", sha1
);