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 int get_sha1(const char *str
, unsigned char *sha1
)
65 static char pathname
[PATH_MAX
];
66 static const char *prefix
[] = {
77 if (!get_sha1_hex(str
, sha1
))
81 for (p
= prefix
; *p
; p
++) {
82 snprintf(pathname
, sizeof(pathname
), "%s/%s/%s", gitdir
, *p
, str
);
83 if (!get_sha1_file(pathname
, sha1
))
90 char * sha1_to_hex(const unsigned char *sha1
)
92 static char buffer
[50];
93 static const char hex
[] = "0123456789abcdef";
97 for (i
= 0; i
< 20; i
++) {
98 unsigned int val
= *sha1
++;
99 *buf
++ = hex
[val
>> 4];
100 *buf
++ = hex
[val
& 0xf];
106 * NOTE! This returns a statically allocated buffer, so you have to be
107 * careful about using it. Do a "strdup()" if you need to save the
110 char *sha1_file_name(const unsigned char *sha1
)
113 static char *name
, *base
;
116 char *sha1_file_directory
= getenv(DB_ENVIRONMENT
) ? : DEFAULT_DB_ENVIRONMENT
;
117 int len
= strlen(sha1_file_directory
);
118 base
= xmalloc(len
+ 60);
119 memcpy(base
, sha1_file_directory
, len
);
120 memset(base
+len
, 0, 60);
123 name
= base
+ len
+ 1;
125 for (i
= 0; i
< 20; i
++) {
126 static char hex
[] = "0123456789abcdef";
127 unsigned int val
= sha1
[i
];
128 char *pos
= name
+ i
*2 + (i
> 0);
129 *pos
++ = hex
[val
>> 4];
130 *pos
= hex
[val
& 0xf];
135 int check_sha1_signature(unsigned char *sha1
, void *map
, unsigned long size
, const char *type
)
138 unsigned char real_sha1
[20];
142 SHA1_Update(&c
, header
, 1+sprintf(header
, "%s %lu", type
, size
));
143 SHA1_Update(&c
, map
, size
);
144 SHA1_Final(real_sha1
, &c
);
145 return memcmp(sha1
, real_sha1
, 20) ? -1 : 0;
148 void *map_sha1_file(const unsigned char *sha1
, unsigned long *size
)
150 char *filename
= sha1_file_name(sha1
);
155 fd
= open(filename
, O_RDONLY
| sha1_file_open_flag
);
157 /* See if it works without O_NOATIME */
158 switch (sha1_file_open_flag
) {
160 fd
= open(filename
, O_RDONLY
);
169 /* If it failed once, it will probably fail again. Stop using O_NOATIME */
170 sha1_file_open_flag
= 0;
172 if (fstat(fd
, &st
) < 0) {
176 map
= mmap(NULL
, st
.st_size
, PROT_READ
, MAP_PRIVATE
, fd
, 0);
178 if (-1 == (int)(long)map
)
184 void * unpack_sha1_file(void *map
, unsigned long mapsize
, char *type
, unsigned long *size
)
191 /* Get the data stream */
192 memset(&stream
, 0, sizeof(stream
));
193 stream
.next_in
= map
;
194 stream
.avail_in
= mapsize
;
195 stream
.next_out
= buffer
;
196 stream
.avail_out
= sizeof(buffer
);
198 inflateInit(&stream
);
199 ret
= inflate(&stream
, 0);
202 if (sscanf(buffer
, "%10s %lu", type
, size
) != 2)
205 bytes
= strlen(buffer
) + 1;
206 buf
= xmalloc(*size
);
208 memcpy(buf
, buffer
+ bytes
, stream
.total_out
- bytes
);
209 bytes
= stream
.total_out
- bytes
;
210 if (bytes
< *size
&& ret
== Z_OK
) {
211 stream
.next_out
= buf
+ bytes
;
212 stream
.avail_out
= *size
- bytes
;
213 while (inflate(&stream
, Z_FINISH
) == Z_OK
)
220 void * read_sha1_file(const unsigned char *sha1
, char *type
, unsigned long *size
)
222 unsigned long mapsize
;
225 map
= map_sha1_file(sha1
, &mapsize
);
227 buf
= unpack_sha1_file(map
, mapsize
, type
, size
);
228 munmap(map
, mapsize
);
234 void *read_object_with_reference(const unsigned char *sha1
,
235 const unsigned char *required_type
,
237 unsigned char *actual_sha1_return
)
242 unsigned char actual_sha1
[20];
244 memcpy(actual_sha1
, sha1
, 20);
247 const char *ref_type
= NULL
;
249 buffer
= read_sha1_file(actual_sha1
, type
, &isize
);
252 if (!strcmp(type
, required_type
)) {
254 if (actual_sha1_return
)
255 memcpy(actual_sha1_return
, actual_sha1
, 20);
258 /* Handle references */
259 else if (!strcmp(type
, "commit"))
261 else if (!strcmp(type
, "tag"))
262 ref_type
= "object ";
267 ref_length
= strlen(ref_type
);
269 if (memcmp(buffer
, ref_type
, ref_length
) ||
270 get_sha1_hex(buffer
+ ref_length
, actual_sha1
)) {
274 /* Now we have the ID of the referred-to object in
275 * actual_sha1. Check again. */
279 int write_sha1_file(char *buf
, unsigned long len
, const char *type
, unsigned char *returnsha1
)
284 unsigned char sha1
[20];
290 /* Generate the header */
291 hdrlen
= sprintf(hdr
, "%s %lu", type
, len
)+1;
295 SHA1_Update(&c
, hdr
, hdrlen
);
296 SHA1_Update(&c
, buf
, len
);
297 SHA1_Final(sha1
, &c
);
300 memcpy(returnsha1
, sha1
, 20);
302 filename
= sha1_file_name(sha1
);
303 fd
= open(filename
, O_WRONLY
| O_CREAT
| O_EXCL
, 0666);
309 * We might do collision checking here, but we'd need to
310 * uncompress the old file and check it. Later.
316 memset(&stream
, 0, sizeof(stream
));
317 deflateInit(&stream
, Z_BEST_COMPRESSION
);
318 size
= deflateBound(&stream
, len
+hdrlen
);
319 compressed
= xmalloc(size
);
322 stream
.next_out
= compressed
;
323 stream
.avail_out
= size
;
326 stream
.next_in
= hdr
;
327 stream
.avail_in
= hdrlen
;
328 while (deflate(&stream
, 0) == Z_OK
)
331 /* Then the data itself.. */
332 stream
.next_in
= buf
;
333 stream
.avail_in
= len
;
334 while (deflate(&stream
, Z_FINISH
) == Z_OK
)
337 size
= stream
.total_out
;
339 if (write(fd
, compressed
, size
) != size
)
340 die("unable to write file");
346 static inline int collision_check(char *filename
, void *buf
, unsigned int size
)
348 #ifdef COLLISION_CHECK
350 int fd
= open(filename
, O_RDONLY
);
354 /* Unreadable object, or object went away? Strange. */
358 if (fstat(fd
, &st
) < 0 || size
!= st
.st_size
)
361 map
= mmap(NULL
, size
, PROT_READ
, MAP_PRIVATE
, fd
, 0);
363 if (map
== MAP_FAILED
)
365 cmp
= memcmp(buf
, map
, size
);
373 int write_sha1_buffer(const unsigned char *sha1
, void *buf
, unsigned int size
)
375 char *filename
= sha1_file_name(sha1
);
378 fd
= open(filename
, O_WRONLY
| O_CREAT
| O_EXCL
, 0666);
382 if (collision_check(filename
, buf
, size
))
383 return error("SHA1 collision detected!"
384 " This is bad, bad, BAD!\a\n");
387 write(fd
, buf
, size
);
392 int write_sha1_from_fd(const unsigned char *sha1
, int fd
)
394 char *filename
= sha1_file_name(sha1
);
398 unsigned char real_sha1
[20];
404 local
= open(filename
, O_WRONLY
| O_CREAT
| O_EXCL
, 0666);
407 return error("Couldn't open %s\n", filename
);
409 memset(&stream
, 0, sizeof(stream
));
411 inflateInit(&stream
);
417 size
= read(fd
, buf
, 4096);
422 return error("Connection closed?");
423 perror("Reading from connection");
426 write(local
, buf
, size
);
427 stream
.avail_in
= size
;
428 stream
.next_in
= buf
;
430 stream
.next_out
= discard
;
431 stream
.avail_out
= sizeof(discard
);
432 ret
= inflate(&stream
, Z_SYNC_FLUSH
);
433 SHA1_Update(&c
, discard
, sizeof(discard
) -
435 } while (stream
.avail_in
&& ret
== Z_OK
);
437 } while (ret
== Z_OK
);
441 SHA1_Final(real_sha1
, &c
);
442 if (ret
!= Z_STREAM_END
) {
444 return error("File %s corrupted", sha1_to_hex(sha1
));
446 if (memcmp(sha1
, real_sha1
, 20)) {
448 return error("File %s has bad hash\n", sha1_to_hex(sha1
));
454 int has_sha1_file(const unsigned char *sha1
)
456 char *filename
= sha1_file_name(sha1
);
459 if (!stat(filename
, &st
))