2 * GIT - The information manager from hell
4 * Copyright (C) Linus Torvalds, 2005
6 * This handles basic git sha1 object files - packing, unpacking,
12 const char *sha1_file_directory
= NULL
;
15 #if defined(__linux__) && (defined(__i386__) || defined(__PPC__))
16 #define O_NOATIME 01000000
22 static unsigned int sha1_file_open_flag
= O_NOATIME
;
24 static unsigned hexval(char c
)
26 if (c
>= '0' && c
<= '9')
28 if (c
>= 'a' && c
<= 'f')
30 if (c
>= 'A' && c
<= 'F')
35 int get_sha1_hex(const char *hex
, unsigned char *sha1
)
38 for (i
= 0; i
< 20; i
++) {
39 unsigned int val
= (hexval(hex
[0]) << 4) | hexval(hex
[1]);
48 int get_sha1_file(const char *path
, unsigned char *result
)
51 int fd
= open(path
, O_RDONLY
);
56 len
= read(fd
, buffer
, sizeof(buffer
));
60 return get_sha1_hex(buffer
, result
);
63 static int get_parent(int index
, const char *str
, unsigned char *result
)
65 unsigned char sha1
[20];
67 unsigned long size
, offset
;
70 if (get_sha1(str
, sha1
) < 0)
72 buffer
= read_object_with_reference(sha1
, "commit", &size
, NULL
);
78 if (offset
+ 48 > size
)
80 if (memcmp(buffer
+ offset
, "parent ", 7))
87 ret
= get_sha1_hex(buffer
+ offset
+ 7, result
);
94 int get_sha1(const char *str
, unsigned char *sha1
)
96 static char pathname
[PATH_MAX
];
97 static const char *prefix
[] = {
108 if (!get_sha1_hex(str
, sha1
))
113 if (!get_sha1_file(str
, sha1
))
117 return get_parent(0, str
+1, sha1
);
120 return get_parent(*str
- '0', str
+2, sha1
);
125 for (p
= prefix
; *p
; p
++) {
126 snprintf(pathname
, sizeof(pathname
), "%s/%s/%s", gitdir
, *p
, str
);
127 if (!get_sha1_file(pathname
, sha1
))
134 char * sha1_to_hex(const unsigned char *sha1
)
136 static char buffer
[50];
137 static const char hex
[] = "0123456789abcdef";
141 for (i
= 0; i
< 20; i
++) {
142 unsigned int val
= *sha1
++;
143 *buf
++ = hex
[val
>> 4];
144 *buf
++ = hex
[val
& 0xf];
150 * NOTE! This returns a statically allocated buffer, so you have to be
151 * careful about using it. Do a "strdup()" if you need to save the
154 char *sha1_file_name(const unsigned char *sha1
)
157 static char *name
, *base
;
160 char *sha1_file_directory
= getenv(DB_ENVIRONMENT
) ? : DEFAULT_DB_ENVIRONMENT
;
161 int len
= strlen(sha1_file_directory
);
162 base
= xmalloc(len
+ 60);
163 memcpy(base
, sha1_file_directory
, len
);
164 memset(base
+len
, 0, 60);
167 name
= base
+ len
+ 1;
169 for (i
= 0; i
< 20; i
++) {
170 static char hex
[] = "0123456789abcdef";
171 unsigned int val
= sha1
[i
];
172 char *pos
= name
+ i
*2 + (i
> 0);
173 *pos
++ = hex
[val
>> 4];
174 *pos
= hex
[val
& 0xf];
179 int check_sha1_signature(unsigned char *sha1
, void *map
, unsigned long size
, const char *type
)
182 unsigned char real_sha1
[20];
186 SHA1_Update(&c
, header
, 1+sprintf(header
, "%s %lu", type
, size
));
187 SHA1_Update(&c
, map
, size
);
188 SHA1_Final(real_sha1
, &c
);
189 return memcmp(sha1
, real_sha1
, 20) ? -1 : 0;
192 void *map_sha1_file(const unsigned char *sha1
, unsigned long *size
)
194 char *filename
= sha1_file_name(sha1
);
199 fd
= open(filename
, O_RDONLY
| sha1_file_open_flag
);
201 /* See if it works without O_NOATIME */
202 switch (sha1_file_open_flag
) {
204 fd
= open(filename
, O_RDONLY
);
213 /* If it failed once, it will probably fail again. Stop using O_NOATIME */
214 sha1_file_open_flag
= 0;
216 if (fstat(fd
, &st
) < 0) {
220 map
= mmap(NULL
, st
.st_size
, PROT_READ
, MAP_PRIVATE
, fd
, 0);
222 if (-1 == (int)(long)map
)
228 void * unpack_sha1_file(void *map
, unsigned long mapsize
, char *type
, unsigned long *size
)
235 /* Get the data stream */
236 memset(&stream
, 0, sizeof(stream
));
237 stream
.next_in
= map
;
238 stream
.avail_in
= mapsize
;
239 stream
.next_out
= buffer
;
240 stream
.avail_out
= sizeof(buffer
);
242 inflateInit(&stream
);
243 ret
= inflate(&stream
, 0);
246 if (sscanf(buffer
, "%10s %lu", type
, size
) != 2)
249 bytes
= strlen(buffer
) + 1;
250 buf
= xmalloc(*size
);
252 memcpy(buf
, buffer
+ bytes
, stream
.total_out
- bytes
);
253 bytes
= stream
.total_out
- bytes
;
254 if (bytes
< *size
&& ret
== Z_OK
) {
255 stream
.next_out
= buf
+ bytes
;
256 stream
.avail_out
= *size
- bytes
;
257 while (inflate(&stream
, Z_FINISH
) == Z_OK
)
264 void * read_sha1_file(const unsigned char *sha1
, char *type
, unsigned long *size
)
266 unsigned long mapsize
;
269 map
= map_sha1_file(sha1
, &mapsize
);
271 buf
= unpack_sha1_file(map
, mapsize
, type
, size
);
272 munmap(map
, mapsize
);
278 void *read_object_with_reference(const unsigned char *sha1
,
279 const unsigned char *required_type
,
281 unsigned char *actual_sha1_return
)
286 unsigned char actual_sha1
[20];
288 memcpy(actual_sha1
, sha1
, 20);
291 const char *ref_type
= NULL
;
293 buffer
= read_sha1_file(actual_sha1
, type
, &isize
);
296 if (!strcmp(type
, required_type
)) {
298 if (actual_sha1_return
)
299 memcpy(actual_sha1_return
, actual_sha1
, 20);
302 /* Handle references */
303 else if (!strcmp(type
, "commit"))
305 else if (!strcmp(type
, "tag"))
306 ref_type
= "object ";
311 ref_length
= strlen(ref_type
);
313 if (memcmp(buffer
, ref_type
, ref_length
) ||
314 get_sha1_hex(buffer
+ ref_length
, actual_sha1
)) {
318 /* Now we have the ID of the referred-to object in
319 * actual_sha1. Check again. */
323 int write_sha1_file(char *buf
, unsigned long len
, const char *type
, unsigned char *returnsha1
)
328 unsigned char sha1
[20];
334 /* Generate the header */
335 hdrlen
= sprintf(hdr
, "%s %lu", type
, len
)+1;
339 SHA1_Update(&c
, hdr
, hdrlen
);
340 SHA1_Update(&c
, buf
, len
);
341 SHA1_Final(sha1
, &c
);
344 memcpy(returnsha1
, sha1
, 20);
346 filename
= sha1_file_name(sha1
);
347 fd
= open(filename
, O_WRONLY
| O_CREAT
| O_EXCL
, 0666);
353 * We might do collision checking here, but we'd need to
354 * uncompress the old file and check it. Later.
360 memset(&stream
, 0, sizeof(stream
));
361 deflateInit(&stream
, Z_BEST_COMPRESSION
);
362 size
= deflateBound(&stream
, len
+hdrlen
);
363 compressed
= xmalloc(size
);
366 stream
.next_out
= compressed
;
367 stream
.avail_out
= size
;
370 stream
.next_in
= hdr
;
371 stream
.avail_in
= hdrlen
;
372 while (deflate(&stream
, 0) == Z_OK
)
375 /* Then the data itself.. */
376 stream
.next_in
= buf
;
377 stream
.avail_in
= len
;
378 while (deflate(&stream
, Z_FINISH
) == Z_OK
)
381 size
= stream
.total_out
;
383 if (write(fd
, compressed
, size
) != size
)
384 die("unable to write file");
390 static inline int collision_check(char *filename
, void *buf
, unsigned int size
)
392 #ifdef COLLISION_CHECK
394 int fd
= open(filename
, O_RDONLY
);
398 /* Unreadable object, or object went away? Strange. */
402 if (fstat(fd
, &st
) < 0 || size
!= st
.st_size
)
405 map
= mmap(NULL
, size
, PROT_READ
, MAP_PRIVATE
, fd
, 0);
407 if (map
== MAP_FAILED
)
409 cmp
= memcmp(buf
, map
, size
);
417 int write_sha1_buffer(const unsigned char *sha1
, void *buf
, unsigned int size
)
419 char *filename
= sha1_file_name(sha1
);
422 fd
= open(filename
, O_WRONLY
| O_CREAT
| O_EXCL
, 0666);
426 if (collision_check(filename
, buf
, size
))
427 return error("SHA1 collision detected!"
428 " This is bad, bad, BAD!\a\n");
431 write(fd
, buf
, size
);
436 int write_sha1_from_fd(const unsigned char *sha1
, int fd
)
438 char *filename
= sha1_file_name(sha1
);
442 unsigned char real_sha1
[20];
448 local
= open(filename
, O_WRONLY
| O_CREAT
| O_EXCL
, 0666);
451 return error("Couldn't open %s\n", filename
);
453 memset(&stream
, 0, sizeof(stream
));
455 inflateInit(&stream
);
461 size
= read(fd
, buf
, 4096);
466 return error("Connection closed?");
467 perror("Reading from connection");
470 write(local
, buf
, size
);
471 stream
.avail_in
= size
;
472 stream
.next_in
= buf
;
474 stream
.next_out
= discard
;
475 stream
.avail_out
= sizeof(discard
);
476 ret
= inflate(&stream
, Z_SYNC_FLUSH
);
477 SHA1_Update(&c
, discard
, sizeof(discard
) -
479 } while (stream
.avail_in
&& ret
== Z_OK
);
481 } while (ret
== Z_OK
);
485 SHA1_Final(real_sha1
, &c
);
486 if (ret
!= Z_STREAM_END
) {
488 return error("File %s corrupted", sha1_to_hex(sha1
));
490 if (memcmp(sha1
, real_sha1
, 20)) {
492 return error("File %s has bad hash\n", sha1_to_hex(sha1
));
498 int has_sha1_file(const unsigned char *sha1
)
500 char *filename
= sha1_file_name(sha1
);
503 if (!stat(filename
, &st
))