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 char * sha1_to_hex(const unsigned char *sha1
)
50 static char buffer
[50];
51 static const char hex
[] = "0123456789abcdef";
55 for (i
= 0; i
< 20; i
++) {
56 unsigned int val
= *sha1
++;
57 *buf
++ = hex
[val
>> 4];
58 *buf
++ = hex
[val
& 0xf];
64 * NOTE! This returns a statically allocated buffer, so you have to be
65 * careful about using it. Do a "strdup()" if you need to save the
68 char *sha1_file_name(const unsigned char *sha1
)
71 static char *name
, *base
;
74 char *sha1_file_directory
= getenv(DB_ENVIRONMENT
) ? : DEFAULT_DB_ENVIRONMENT
;
75 int len
= strlen(sha1_file_directory
);
76 base
= malloc(len
+ 60);
77 memcpy(base
, sha1_file_directory
, len
);
78 memset(base
+len
, 0, 60);
81 name
= base
+ len
+ 1;
83 for (i
= 0; i
< 20; i
++) {
84 static char hex
[] = "0123456789abcdef";
85 unsigned int val
= sha1
[i
];
86 char *pos
= name
+ i
*2 + (i
> 0);
87 *pos
++ = hex
[val
>> 4];
88 *pos
= hex
[val
& 0xf];
93 int check_sha1_signature(unsigned char *sha1
, void *map
, unsigned long size
, const char *type
)
96 unsigned char real_sha1
[20];
100 SHA1_Update(&c
, header
, 1+sprintf(header
, "%s %lu", type
, size
));
101 SHA1_Update(&c
, map
, size
);
102 SHA1_Final(real_sha1
, &c
);
103 return memcmp(sha1
, real_sha1
, 20) ? -1 : 0;
106 void *map_sha1_file(const unsigned char *sha1
, unsigned long *size
)
108 char *filename
= sha1_file_name(sha1
);
113 fd
= open(filename
, O_RDONLY
| sha1_file_open_flag
);
115 /* See if it works without O_NOATIME */
116 switch (sha1_file_open_flag
) {
118 fd
= open(filename
, O_RDONLY
);
127 /* If it failed once, it will probably fail again. Stop using O_NOATIME */
128 sha1_file_open_flag
= 0;
130 if (fstat(fd
, &st
) < 0) {
134 map
= mmap(NULL
, st
.st_size
, PROT_READ
, MAP_PRIVATE
, fd
, 0);
136 if (-1 == (int)(long)map
)
142 void * unpack_sha1_file(void *map
, unsigned long mapsize
, char *type
, unsigned long *size
)
149 /* Get the data stream */
150 memset(&stream
, 0, sizeof(stream
));
151 stream
.next_in
= map
;
152 stream
.avail_in
= mapsize
;
153 stream
.next_out
= buffer
;
154 stream
.avail_out
= sizeof(buffer
);
156 inflateInit(&stream
);
157 ret
= inflate(&stream
, 0);
158 if (sscanf(buffer
, "%10s %lu", type
, size
) != 2)
161 bytes
= strlen(buffer
) + 1;
166 memcpy(buf
, buffer
+ bytes
, stream
.total_out
- bytes
);
167 bytes
= stream
.total_out
- bytes
;
168 if (bytes
< *size
&& ret
== Z_OK
) {
169 stream
.next_out
= buf
+ bytes
;
170 stream
.avail_out
= *size
- bytes
;
171 while (inflate(&stream
, Z_FINISH
) == Z_OK
)
178 void * read_sha1_file(const unsigned char *sha1
, char *type
, unsigned long *size
)
180 unsigned long mapsize
;
183 map
= map_sha1_file(sha1
, &mapsize
);
185 buf
= unpack_sha1_file(map
, mapsize
, type
, size
);
186 munmap(map
, mapsize
);
192 void *read_tree_with_tree_or_commit_sha1(const unsigned char *sha1
,
194 unsigned char *tree_sha1_return
)
200 unsigned char tree_sha1
[20];
202 buffer
= read_sha1_file(sha1
, type
, &isize
);
205 * We might have read a commit instead of a tree, in which case
206 * we parse out the tree_sha1 and attempt to read from there.
207 * (buffer + 5) is because the tree sha1 is always at offset 5
208 * in a commit record ("tree ").
211 !strcmp(type
, "commit") &&
212 !get_sha1_hex(buffer
+ 5, tree_sha1
)) {
214 buffer
= read_sha1_file(tree_sha1
, type
, &isize
);
219 * Now do we have something and if so is it a tree?
221 if (!buffer
|| strcmp(type
, "tree")) {
227 if (tree_sha1_return
)
228 memcpy(tree_sha1_return
, was_commit
? tree_sha1
: sha1
, 20);
232 int write_sha1_file(char *buf
, unsigned len
, unsigned char *returnsha1
)
237 unsigned char sha1
[20];
244 SHA1_Update(&c
, buf
, len
);
245 SHA1_Final(sha1
, &c
);
248 memcpy(returnsha1
, sha1
, 20);
250 filename
= sha1_file_name(sha1
);
251 fd
= open(filename
, O_WRONLY
| O_CREAT
| O_EXCL
, 0666);
257 * We might do collision checking here, but we'd need to
258 * uncompress the old file and check it. Later.
264 memset(&stream
, 0, sizeof(stream
));
265 deflateInit(&stream
, Z_BEST_COMPRESSION
);
266 size
= deflateBound(&stream
, len
);
267 compressed
= malloc(size
);
270 stream
.next_in
= buf
;
271 stream
.avail_in
= len
;
272 stream
.next_out
= compressed
;
273 stream
.avail_out
= size
;
274 while (deflate(&stream
, Z_FINISH
) == Z_OK
)
277 size
= stream
.total_out
;
279 if (write(fd
, compressed
, size
) != size
)
280 die("unable to write file");
286 static inline int collision_check(char *filename
, void *buf
, unsigned int size
)
288 #ifdef COLLISION_CHECK
290 int fd
= open(filename
, O_RDONLY
);
294 /* Unreadable object, or object went away? Strange. */
298 if (fstat(fd
, &st
) < 0 || size
!= st
.st_size
)
301 map
= mmap(NULL
, size
, PROT_READ
, MAP_PRIVATE
, fd
, 0);
303 if (map
== MAP_FAILED
)
305 cmp
= memcmp(buf
, map
, size
);
313 int write_sha1_buffer(const unsigned char *sha1
, void *buf
, unsigned int size
)
315 char *filename
= sha1_file_name(sha1
);
318 fd
= open(filename
, O_WRONLY
| O_CREAT
| O_EXCL
, 0666);
322 if (collision_check(filename
, buf
, size
))
323 return error("SHA1 collision detected!"
324 " This is bad, bad, BAD!\a\n");
327 write(fd
, buf
, size
);
332 int write_sha1_from_fd(const unsigned char *sha1
, int fd
)
334 char *filename
= sha1_file_name(sha1
);
338 unsigned char real_sha1
[20];
344 local
= open(filename
, O_WRONLY
| O_CREAT
| O_EXCL
, 0666);
347 return error("Couldn't open %s\n", filename
);
349 memset(&stream
, 0, sizeof(stream
));
351 inflateInit(&stream
);
357 size
= read(fd
, buf
, 4096);
362 return error("Connection closed?");
363 perror("Reading from connection");
366 write(local
, buf
, size
);
367 stream
.avail_in
= size
;
368 stream
.next_in
= buf
;
370 stream
.next_out
= discard
;
371 stream
.avail_out
= sizeof(discard
);
372 ret
= inflate(&stream
, Z_SYNC_FLUSH
);
373 SHA1_Update(&c
, discard
, sizeof(discard
) -
375 } while (stream
.avail_in
&& ret
== Z_OK
);
377 } while (ret
== Z_OK
);
381 SHA1_Final(real_sha1
, &c
);
382 if (ret
!= Z_STREAM_END
) {
384 return error("File %s corrupted", sha1_to_hex(sha1
));
386 if (memcmp(sha1
, real_sha1
, 20)) {
388 return error("File %s has bad hash\n", sha1_to_hex(sha1
));
394 int has_sha1_file(const unsigned char *sha1
)
396 char *filename
= sha1_file_name(sha1
);
399 if (!stat(filename
, &st
))