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
;
14 static unsigned hexval(char c
)
16 if (c
>= '0' && c
<= '9')
18 if (c
>= 'a' && c
<= 'f')
20 if (c
>= 'A' && c
<= 'F')
25 int get_sha1_hex(const char *hex
, unsigned char *sha1
)
28 for (i
= 0; i
< 20; i
++) {
29 unsigned int val
= (hexval(hex
[0]) << 4) | hexval(hex
[1]);
38 char * sha1_to_hex(const unsigned char *sha1
)
40 static char buffer
[50];
41 static const char hex
[] = "0123456789abcdef";
45 for (i
= 0; i
< 20; i
++) {
46 unsigned int val
= *sha1
++;
47 *buf
++ = hex
[val
>> 4];
48 *buf
++ = hex
[val
& 0xf];
54 * NOTE! This returns a statically allocated buffer, so you have to be
55 * careful about using it. Do a "strdup()" if you need to save the
58 char *sha1_file_name(const unsigned char *sha1
)
61 static char *name
, *base
;
64 char *sha1_file_directory
= getenv(DB_ENVIRONMENT
) ? : DEFAULT_DB_ENVIRONMENT
;
65 int len
= strlen(sha1_file_directory
);
66 base
= malloc(len
+ 60);
67 memcpy(base
, sha1_file_directory
, len
);
68 memset(base
+len
, 0, 60);
71 name
= base
+ len
+ 1;
73 for (i
= 0; i
< 20; i
++) {
74 static char hex
[] = "0123456789abcdef";
75 unsigned int val
= sha1
[i
];
76 char *pos
= name
+ i
*2 + (i
> 0);
77 *pos
++ = hex
[val
>> 4];
78 *pos
= hex
[val
& 0xf];
83 int check_sha1_signature(unsigned char *sha1
, void *map
, unsigned long size
, const char *type
)
86 unsigned char real_sha1
[20];
90 SHA1_Update(&c
, header
, 1+sprintf(header
, "%s %lu", type
, size
));
91 SHA1_Update(&c
, map
, size
);
92 SHA1_Final(real_sha1
, &c
);
93 return memcmp(sha1
, real_sha1
, 20) ? -1 : 0;
96 void *map_sha1_file(const unsigned char *sha1
, unsigned long *size
)
98 char *filename
= sha1_file_name(sha1
);
99 int fd
= open(filename
, O_RDONLY
);
107 if (fstat(fd
, &st
) < 0) {
111 map
= mmap(NULL
, st
.st_size
, PROT_READ
, MAP_PRIVATE
, fd
, 0);
113 if (-1 == (int)(long)map
)
119 void * unpack_sha1_file(void *map
, unsigned long mapsize
, char *type
, unsigned long *size
)
126 /* Get the data stream */
127 memset(&stream
, 0, sizeof(stream
));
128 stream
.next_in
= map
;
129 stream
.avail_in
= mapsize
;
130 stream
.next_out
= buffer
;
131 stream
.avail_out
= sizeof(buffer
);
133 inflateInit(&stream
);
134 ret
= inflate(&stream
, 0);
135 if (sscanf(buffer
, "%10s %lu", type
, size
) != 2)
138 bytes
= strlen(buffer
) + 1;
143 memcpy(buf
, buffer
+ bytes
, stream
.total_out
- bytes
);
144 bytes
= stream
.total_out
- bytes
;
145 if (bytes
< *size
&& ret
== Z_OK
) {
146 stream
.next_out
= buf
+ bytes
;
147 stream
.avail_out
= *size
- bytes
;
148 while (inflate(&stream
, Z_FINISH
) == Z_OK
)
155 void * read_sha1_file(const unsigned char *sha1
, char *type
, unsigned long *size
)
157 unsigned long mapsize
;
160 map
= map_sha1_file(sha1
, &mapsize
);
162 buf
= unpack_sha1_file(map
, mapsize
, type
, size
);
163 munmap(map
, mapsize
);
169 void *read_tree_with_tree_or_commit_sha1(const unsigned char *sha1
,
171 unsigned char *tree_sha1_return
)
177 unsigned char tree_sha1
[20];
179 buffer
= read_sha1_file(sha1
, type
, &isize
);
182 * We might have read a commit instead of a tree, in which case
183 * we parse out the tree_sha1 and attempt to read from there.
184 * (buffer + 5) is because the tree sha1 is always at offset 5
185 * in a commit record ("tree ").
188 !strcmp(type
, "commit") &&
189 !get_sha1_hex(buffer
+ 5, tree_sha1
)) {
191 buffer
= read_sha1_file(tree_sha1
, type
, &isize
);
196 * Now do we have something and if so is it a tree?
198 if (!buffer
|| strcmp(type
, "tree")) {
204 if (tree_sha1_return
)
205 memcpy(tree_sha1_return
, was_commit
? tree_sha1
: sha1
, 20);
209 int write_sha1_file(char *buf
, unsigned len
, unsigned char *returnsha1
)
214 unsigned char sha1
[20];
221 SHA1_Update(&c
, buf
, len
);
222 SHA1_Final(sha1
, &c
);
225 memcpy(returnsha1
, sha1
, 20);
227 filename
= sha1_file_name(sha1
);
228 fd
= open(filename
, O_WRONLY
| O_CREAT
| O_EXCL
, 0666);
234 * We might do collision checking here, but we'd need to
235 * uncompress the old file and check it. Later.
241 memset(&stream
, 0, sizeof(stream
));
242 deflateInit(&stream
, Z_BEST_COMPRESSION
);
243 size
= deflateBound(&stream
, len
);
244 compressed
= malloc(size
);
247 stream
.next_in
= buf
;
248 stream
.avail_in
= len
;
249 stream
.next_out
= compressed
;
250 stream
.avail_out
= size
;
251 while (deflate(&stream
, Z_FINISH
) == Z_OK
)
254 size
= stream
.total_out
;
256 if (write(fd
, compressed
, size
) != size
)
257 die("unable to write file");
263 static inline int collision_check(char *filename
, void *buf
, unsigned int size
)
265 #ifdef COLLISION_CHECK
267 int fd
= open(filename
, O_RDONLY
);
271 /* Unreadable object, or object went away? Strange. */
275 if (fstat(fd
, &st
) < 0 || size
!= st
.st_size
)
278 map
= mmap(NULL
, size
, PROT_READ
, MAP_PRIVATE
, fd
, 0);
280 if (map
== MAP_FAILED
)
282 cmp
= memcmp(buf
, map
, size
);
290 int write_sha1_buffer(const unsigned char *sha1
, void *buf
, unsigned int size
)
292 char *filename
= sha1_file_name(sha1
);
295 fd
= open(filename
, O_WRONLY
| O_CREAT
| O_EXCL
, 0666);
299 if (collision_check(filename
, buf
, size
))
300 return error("SHA1 collision detected!"
301 " This is bad, bad, BAD!\a\n");
304 write(fd
, buf
, size
);