2 * GIT - The information manager from hell
4 * Copyright (C) Linus Torvalds, 2005
6 * This handles basic git sha1 object files - packing, unpacking,
12 #if defined(__linux__) && (defined(__i386__) || defined(__PPC__))
13 #define O_NOATIME 01000000
19 static unsigned int sha1_file_open_flag
= O_NOATIME
;
21 static unsigned hexval(char c
)
23 if (c
>= '0' && c
<= '9')
25 if (c
>= 'a' && c
<= 'f')
27 if (c
>= 'A' && c
<= 'F')
32 int get_sha1_hex(const char *hex
, unsigned char *sha1
)
35 for (i
= 0; i
< 20; i
++) {
36 unsigned int val
= (hexval(hex
[0]) << 4) | hexval(hex
[1]);
45 static int get_sha1_file(const char *path
, unsigned char *result
)
48 int fd
= open(path
, O_RDONLY
);
53 len
= read(fd
, buffer
, sizeof(buffer
));
57 return get_sha1_hex(buffer
, result
);
60 static char *git_dir
, *git_object_dir
, *git_index_file
, *git_refs_dir
;
61 static void setup_git_env(void)
63 git_dir
= gitenv(GIT_DIR_ENVIRONMENT
);
65 git_dir
= DEFAULT_GIT_DIR_ENVIRONMENT
;
66 git_object_dir
= gitenv(DB_ENVIRONMENT
);
67 if (!git_object_dir
) {
68 git_object_dir
= xmalloc(strlen(git_dir
) + 9);
69 sprintf(git_object_dir
, "%s/objects", git_dir
);
71 git_refs_dir
= xmalloc(strlen(git_dir
) + 6);
72 sprintf(git_refs_dir
, "%s/refs", git_dir
);
73 git_index_file
= gitenv(INDEX_ENVIRONMENT
);
74 if (!git_index_file
) {
75 git_index_file
= xmalloc(strlen(git_dir
) + 7);
76 sprintf(git_index_file
, "%s/index", git_dir
);
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 int get_sha1(const char *str
, unsigned char *sha1
)
103 static char pathname
[PATH_MAX
];
104 static const char *prefix
[] = {
114 if (!get_sha1_hex(str
, sha1
))
119 for (p
= prefix
; *p
; p
++) {
120 snprintf(pathname
, sizeof(pathname
), "%s/%s/%s",
122 if (!get_sha1_file(pathname
, sha1
))
129 char * sha1_to_hex(const unsigned char *sha1
)
131 static char buffer
[50];
132 static const char hex
[] = "0123456789abcdef";
136 for (i
= 0; i
< 20; i
++) {
137 unsigned int val
= *sha1
++;
138 *buf
++ = hex
[val
>> 4];
139 *buf
++ = hex
[val
& 0xf];
144 static void fill_sha1_path(char *pathbuf
, const unsigned char *sha1
)
147 for (i
= 0; i
< 20; i
++) {
148 static char hex
[] = "0123456789abcdef";
149 unsigned int val
= sha1
[i
];
150 char *pos
= pathbuf
+ i
*2 + (i
> 0);
151 *pos
++ = hex
[val
>> 4];
152 *pos
= hex
[val
& 0xf];
157 * NOTE! This returns a statically allocated buffer, so you have to be
158 * careful about using it. Do a "strdup()" if you need to save the
161 * Also note that this returns the location for creating. Reading
162 * SHA1 file can happen from any alternate directory listed in the
163 * DB_ENVIRONMENT environment variable if it is not found in
164 * the primary object database.
166 char *sha1_file_name(const unsigned char *sha1
)
168 static char *name
, *base
;
171 const char *sha1_file_directory
= get_object_directory();
172 int len
= strlen(sha1_file_directory
);
173 base
= xmalloc(len
+ 60);
174 memcpy(base
, sha1_file_directory
, len
);
175 memset(base
+len
, 0, 60);
178 name
= base
+ len
+ 1;
180 fill_sha1_path(name
, sha1
);
184 static struct alternate_object_database
{
190 * Prepare alternate object database registry.
191 * alt_odb points at an array of struct alternate_object_database.
192 * This array is terminated with an element that has both its base
193 * and name set to NULL. alt_odb[n] comes from n'th non-empty
194 * element from colon separated ALTERNATE_DB_ENVIRONMENT environment
195 * variable, and its base points at a statically allocated buffer
196 * that contains "/the/directory/corresponding/to/.git/objects/...",
197 * while its name points just after the slash at the end of
198 * ".git/objects/" in the example above, and has enough space to hold
199 * 40-byte hex SHA1, an extra slash for the first level indirection,
200 * and the terminating NUL.
201 * This function allocates the alt_odb array and all the strings
202 * pointed by base fields of the array elements with one xmalloc();
203 * the string pool immediately follows the array.
205 static void prepare_alt_odb(void)
208 const char *cp
, *last
;
210 const char *alt
= gitenv(ALTERNATE_DB_ENVIRONMENT
) ? : "";
212 /* The first pass counts how large an area to allocate to
213 * hold the entire alt_odb structure, including array of
214 * structs and path buffers for them. The second pass fills
215 * the structure and prepares the path buffers for use by
218 for (totlen
= pass
= 0; pass
< 2; pass
++) {
222 cp
= strchr(last
, ':') ? : last
+ strlen(last
);
224 /* 43 = 40-byte + 2 '/' + terminating NUL */
225 int pfxlen
= cp
- last
;
226 int entlen
= pfxlen
+ 43;
230 alt_odb
[i
].base
= op
;
231 alt_odb
[i
].name
= op
+ pfxlen
+ 1;
232 memcpy(op
, last
, pfxlen
);
233 op
[pfxlen
] = op
[pfxlen
+ 3] = '/';
239 while (*cp
&& *cp
== ':')
245 alt_odb
= xmalloc(sizeof(*alt_odb
) * (i
+ 1) + totlen
);
246 alt_odb
[i
].base
= alt_odb
[i
].name
= NULL
;
247 op
= (char*)(&alt_odb
[i
+1]);
251 static char *find_sha1_file(const unsigned char *sha1
, struct stat
*st
)
254 char *name
= sha1_file_name(sha1
);
260 for (i
= 0; (name
= alt_odb
[i
].name
) != NULL
; i
++) {
261 fill_sha1_path(name
, sha1
);
262 if (!stat(alt_odb
[i
].base
, st
))
263 return alt_odb
[i
].base
;
268 int check_sha1_signature(const unsigned char *sha1
, void *map
, unsigned long size
, const char *type
)
271 unsigned char real_sha1
[20];
275 SHA1_Update(&c
, header
, 1+sprintf(header
, "%s %lu", type
, size
));
276 SHA1_Update(&c
, map
, size
);
277 SHA1_Final(real_sha1
, &c
);
278 return memcmp(sha1
, real_sha1
, 20) ? -1 : 0;
281 void *map_sha1_file(const unsigned char *sha1
, unsigned long *size
)
286 char *filename
= find_sha1_file(sha1
, &st
);
289 error("cannot map sha1 file %s", sha1_to_hex(sha1
));
293 fd
= open(filename
, O_RDONLY
| sha1_file_open_flag
);
295 /* See if it works without O_NOATIME */
296 switch (sha1_file_open_flag
) {
298 fd
= open(filename
, O_RDONLY
);
307 /* If it failed once, it will probably fail again. Stop using O_NOATIME */
308 sha1_file_open_flag
= 0;
310 map
= mmap(NULL
, st
.st_size
, PROT_READ
, MAP_PRIVATE
, fd
, 0);
312 if (-1 == (int)(long)map
)
318 int unpack_sha1_header(z_stream
*stream
, void *map
, unsigned long mapsize
, void *buffer
, unsigned long size
)
320 /* Get the data stream */
321 memset(stream
, 0, sizeof(*stream
));
322 stream
->next_in
= map
;
323 stream
->avail_in
= mapsize
;
324 stream
->next_out
= buffer
;
325 stream
->avail_out
= size
;
328 return inflate(stream
, 0);
331 void *unpack_sha1_rest(z_stream
*stream
, void *buffer
, unsigned long size
)
333 int bytes
= strlen(buffer
) + 1;
334 unsigned char *buf
= xmalloc(1+size
);
336 memcpy(buf
, buffer
+ bytes
, stream
->total_out
- bytes
);
337 bytes
= stream
->total_out
- bytes
;
339 stream
->next_out
= buf
+ bytes
;
340 stream
->avail_out
= size
- bytes
;
341 while (inflate(stream
, Z_FINISH
) == Z_OK
)
350 * We used to just use "sscanf()", but that's actually way
351 * too permissive for what we want to check. So do an anal
352 * object header parse by hand.
354 int parse_sha1_header(char *hdr
, char *type
, unsigned long *sizep
)
360 * The type can be at most ten bytes (including the
361 * terminating '\0' that we add), and is followed by
376 * The length must follow immediately, and be in canonical
377 * decimal format (ie "010" is not valid).
384 unsigned long c
= *hdr
- '0';
388 size
= size
* 10 + c
;
394 * The length must be followed by a zero byte
396 return *hdr
? -1 : 0;
399 void * unpack_sha1_file(void *map
, unsigned long mapsize
, char *type
, unsigned long *size
)
405 ret
= unpack_sha1_header(&stream
, map
, mapsize
, hdr
, sizeof(hdr
));
406 if (ret
< Z_OK
|| parse_sha1_header(hdr
, type
, size
) < 0)
409 return unpack_sha1_rest(&stream
, hdr
, *size
);
412 int sha1_file_size(const unsigned char *sha1
, unsigned long *sizep
)
415 unsigned long mapsize
, size
;
418 char hdr
[64], type
[20];
420 map
= map_sha1_file(sha1
, &mapsize
);
423 ret
= unpack_sha1_header(&stream
, map
, mapsize
, hdr
, sizeof(hdr
));
424 if (ret
< Z_OK
|| parse_sha1_header(hdr
, type
, &size
) < 0)
431 munmap(map
, mapsize
);
435 void * read_sha1_file(const unsigned char *sha1
, char *type
, unsigned long *size
)
437 unsigned long mapsize
;
440 map
= map_sha1_file(sha1
, &mapsize
);
442 buf
= unpack_sha1_file(map
, mapsize
, type
, size
);
443 munmap(map
, mapsize
);
449 void *read_object_with_reference(const unsigned char *sha1
,
450 const char *required_type
,
452 unsigned char *actual_sha1_return
)
457 unsigned char actual_sha1
[20];
459 memcpy(actual_sha1
, sha1
, 20);
462 const char *ref_type
= NULL
;
464 buffer
= read_sha1_file(actual_sha1
, type
, &isize
);
467 if (!strcmp(type
, required_type
)) {
469 if (actual_sha1_return
)
470 memcpy(actual_sha1_return
, actual_sha1
, 20);
473 /* Handle references */
474 else if (!strcmp(type
, "commit"))
476 else if (!strcmp(type
, "tag"))
477 ref_type
= "object ";
482 ref_length
= strlen(ref_type
);
484 if (memcmp(buffer
, ref_type
, ref_length
) ||
485 get_sha1_hex(buffer
+ ref_length
, actual_sha1
)) {
489 /* Now we have the ID of the referred-to object in
490 * actual_sha1. Check again. */
494 int write_sha1_file(void *buf
, unsigned long len
, const char *type
, unsigned char *returnsha1
)
497 unsigned char *compressed
;
499 unsigned char sha1
[20];
502 static char tmpfile
[PATH_MAX
];
503 unsigned char hdr
[50];
506 /* Generate the header */
507 hdrlen
= sprintf((char *)hdr
, "%s %lu", type
, len
)+1;
511 SHA1_Update(&c
, hdr
, hdrlen
);
512 SHA1_Update(&c
, buf
, len
);
513 SHA1_Final(sha1
, &c
);
516 memcpy(returnsha1
, sha1
, 20);
518 filename
= sha1_file_name(sha1
);
519 fd
= open(filename
, O_RDONLY
);
522 * FIXME!!! We might do collision checking here, but we'd
523 * need to uncompress the old file and check it. Later.
529 if (errno
!= ENOENT
) {
530 fprintf(stderr
, "sha1 file %s: %s", filename
, strerror(errno
));
534 snprintf(tmpfile
, sizeof(tmpfile
), "%s/obj_XXXXXX", get_object_directory());
536 fd
= mkstemp(tmpfile
);
538 fprintf(stderr
, "unable to create temporary sha1 filename %s: %s", tmpfile
, strerror(errno
));
543 memset(&stream
, 0, sizeof(stream
));
544 deflateInit(&stream
, Z_BEST_COMPRESSION
);
545 size
= deflateBound(&stream
, len
+hdrlen
);
546 compressed
= xmalloc(size
);
549 stream
.next_out
= compressed
;
550 stream
.avail_out
= size
;
553 stream
.next_in
= hdr
;
554 stream
.avail_in
= hdrlen
;
555 while (deflate(&stream
, 0) == Z_OK
)
558 /* Then the data itself.. */
559 stream
.next_in
= buf
;
560 stream
.avail_in
= len
;
561 while (deflate(&stream
, Z_FINISH
) == Z_OK
)
564 size
= stream
.total_out
;
566 if (write(fd
, compressed
, size
) != size
)
567 die("unable to write file");
572 ret
= link(tmpfile
, filename
);
577 * Coda hack - coda doesn't like cross-directory links,
578 * so we fall back to a rename, which will mean that it
579 * won't be able to check collisions, but that's not a
582 * When this succeeds, we just return 0. We have nothing
585 if (ret
== EXDEV
&& !rename(tmpfile
, filename
))
591 fprintf(stderr
, "unable to write sha1 filename %s: %s", filename
, strerror(ret
));
594 /* FIXME!!! Collision check here ? */
600 int write_sha1_from_fd(const unsigned char *sha1
, int fd
)
602 char *filename
= sha1_file_name(sha1
);
606 unsigned char real_sha1
[20];
607 unsigned char buf
[4096];
608 unsigned char discard
[4096];
612 local
= open(filename
, O_WRONLY
| O_CREAT
| O_EXCL
, 0666);
615 return error("Couldn't open %s\n", filename
);
617 memset(&stream
, 0, sizeof(stream
));
619 inflateInit(&stream
);
625 size
= read(fd
, buf
, 4096);
630 return error("Connection closed?");
631 perror("Reading from connection");
634 write(local
, buf
, size
);
635 stream
.avail_in
= size
;
636 stream
.next_in
= buf
;
638 stream
.next_out
= discard
;
639 stream
.avail_out
= sizeof(discard
);
640 ret
= inflate(&stream
, Z_SYNC_FLUSH
);
641 SHA1_Update(&c
, discard
, sizeof(discard
) -
643 } while (stream
.avail_in
&& ret
== Z_OK
);
645 } while (ret
== Z_OK
);
649 SHA1_Final(real_sha1
, &c
);
650 if (ret
!= Z_STREAM_END
) {
652 return error("File %s corrupted", sha1_to_hex(sha1
));
654 if (memcmp(sha1
, real_sha1
, 20)) {
656 return error("File %s has bad hash\n", sha1_to_hex(sha1
));
662 int has_sha1_file(const unsigned char *sha1
)
665 return !!find_sha1_file(sha1
, &st
);
668 int index_fd(unsigned char *sha1
, int fd
, struct stat
*st
)
670 unsigned long size
= st
->st_size
;
676 buf
= mmap(NULL
, size
, PROT_READ
, MAP_PRIVATE
, fd
, 0);
678 if ((int)(long)buf
== -1)
681 ret
= write_sha1_file(buf
, size
, "blob", sha1
);