Add "look up parent" logic for the simple names.
[git/git.git] / sha1_file.c
CommitLineData
0fcfd160
LT
1/*
2 * GIT - The information manager from hell
3 *
4 * Copyright (C) Linus Torvalds, 2005
5 *
6 * This handles basic git sha1 object files - packing, unpacking,
7 * creation etc.
8 */
9#include <stdarg.h>
10#include "cache.h"
11
12const char *sha1_file_directory = NULL;
13
144bde78
LT
14#ifndef O_NOATIME
15#if defined(__linux__) && (defined(__i386__) || defined(__PPC__))
16#define O_NOATIME 01000000
17#else
18#define O_NOATIME 0
19#endif
20#endif
21
22static unsigned int sha1_file_open_flag = O_NOATIME;
23
0fcfd160
LT
24static unsigned hexval(char c)
25{
26 if (c >= '0' && c <= '9')
27 return c - '0';
28 if (c >= 'a' && c <= 'f')
29 return c - 'a' + 10;
30 if (c >= 'A' && c <= 'F')
31 return c - 'A' + 10;
32 return ~0;
33}
34
35int get_sha1_hex(const char *hex, unsigned char *sha1)
36{
37 int i;
38 for (i = 0; i < 20; i++) {
39 unsigned int val = (hexval(hex[0]) << 4) | hexval(hex[1]);
40 if (val & ~0xff)
41 return -1;
42 *sha1++ = val;
43 hex += 2;
44 }
45 return 0;
46}
47
3c249c95
LT
48int get_sha1_file(const char *path, unsigned char *result)
49{
50 char buffer[60];
51 int fd = open(path, O_RDONLY);
52 int len;
53
54 if (fd < 0)
55 return -1;
56 len = read(fd, buffer, sizeof(buffer));
57 close(fd);
58 if (len < 40)
59 return -1;
60 return get_sha1_hex(buffer, result);
61}
62
35ad3382
LT
63static int get_parent(int index, const char *str, unsigned char *result)
64{
65 unsigned char sha1[20];
66 char *buffer;
67 unsigned long size, offset;
68 int ret;
69
70 if (get_sha1(str, sha1) < 0)
71 return -1;
72 buffer = read_object_with_reference(sha1, "commit", &size, NULL);
73 if (!buffer)
74 return -1;
75 ret = -1;
76 offset = 46;
77 for (;;) {
78 if (offset + 48 > size)
79 break;
80 if (memcmp(buffer + offset, "parent ", 7))
81 break;
82 if (index > 0) {
83 offset += 48;
84 index--;
85 continue;
86 }
87 ret = get_sha1_hex(buffer + offset + 7, result);
88 break;
89 }
90 free(buffer);
91 return ret;
92}
93
3c249c95
LT
94int get_sha1(const char *str, unsigned char *sha1)
95{
96 static char pathname[PATH_MAX];
35ad3382
LT
97 static const char *prefix[] = {
98 "",
99 "refs",
100 "refs/tags",
101 "refs/heads",
102 "refs/snap",
103 NULL
104 };
105 const char *gitdir;
106 const char **p;
3c249c95
LT
107
108 if (!get_sha1_hex(str, sha1))
109 return 0;
35ad3382
LT
110
111 switch (*str) {
112 case '/':
113 if (!get_sha1_file(str, sha1))
114 return 0;
115 break;
116 case '-':
117 return get_parent(0, str+1, sha1);
118 case '0' ... '9':
119 if (str[1] == '-')
120 return get_parent(*str - '0', str+2, sha1);
121 break;
122 }
123
124 gitdir = ".git";
125 for (p = prefix; *p; p++) {
126 snprintf(pathname, sizeof(pathname), "%s/%s/%s", gitdir, *p, str);
127 if (!get_sha1_file(pathname, sha1))
128 return 0;
129 }
130
3c249c95
LT
131 return -1;
132}
133
0fcfd160
LT
134char * sha1_to_hex(const unsigned char *sha1)
135{
136 static char buffer[50];
137 static const char hex[] = "0123456789abcdef";
138 char *buf = buffer;
139 int i;
140
141 for (i = 0; i < 20; i++) {
142 unsigned int val = *sha1++;
143 *buf++ = hex[val >> 4];
144 *buf++ = hex[val & 0xf];
145 }
146 return buffer;
147}
148
149/*
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
152 * filename.
153 */
154char *sha1_file_name(const unsigned char *sha1)
155{
156 int i;
157 static char *name, *base;
158
159 if (!base) {
160 char *sha1_file_directory = getenv(DB_ENVIRONMENT) ? : DEFAULT_DB_ENVIRONMENT;
161 int len = strlen(sha1_file_directory);
812666c8 162 base = xmalloc(len + 60);
0fcfd160
LT
163 memcpy(base, sha1_file_directory, len);
164 memset(base+len, 0, 60);
165 base[len] = '/';
166 base[len+3] = '/';
167 name = base + len + 1;
168 }
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];
175 }
176 return base;
177}
178
d98b46f8 179int check_sha1_signature(unsigned char *sha1, void *map, unsigned long size, const char *type)
0fcfd160 180{
d98b46f8 181 char header[100];
0fcfd160
LT
182 unsigned char real_sha1[20];
183 SHA_CTX c;
184
185 SHA1_Init(&c);
d98b46f8 186 SHA1_Update(&c, header, 1+sprintf(header, "%s %lu", type, size));
0fcfd160
LT
187 SHA1_Update(&c, map, size);
188 SHA1_Final(real_sha1, &c);
189 return memcmp(sha1, real_sha1, 20) ? -1 : 0;
190}
191
192void *map_sha1_file(const unsigned char *sha1, unsigned long *size)
193{
194 char *filename = sha1_file_name(sha1);
0fcfd160
LT
195 struct stat st;
196 void *map;
144bde78 197 int fd;
0fcfd160 198
144bde78 199 fd = open(filename, O_RDONLY | sha1_file_open_flag);
0fcfd160 200 if (fd < 0) {
144bde78
LT
201 /* See if it works without O_NOATIME */
202 switch (sha1_file_open_flag) {
203 default:
204 fd = open(filename, O_RDONLY);
205 if (fd >= 0)
206 break;
207 /* Fallthrough */
208 case 0:
209 perror(filename);
210 return NULL;
211 }
212
213 /* If it failed once, it will probably fail again. Stop using O_NOATIME */
214 sha1_file_open_flag = 0;
0fcfd160
LT
215 }
216 if (fstat(fd, &st) < 0) {
217 close(fd);
218 return NULL;
219 }
220 map = mmap(NULL, st.st_size, PROT_READ, MAP_PRIVATE, fd, 0);
221 close(fd);
222 if (-1 == (int)(long)map)
223 return NULL;
224 *size = st.st_size;
225 return map;
226}
227
228void * unpack_sha1_file(void *map, unsigned long mapsize, char *type, unsigned long *size)
229{
230 int ret, bytes;
231 z_stream stream;
232 char buffer[8192];
233 char *buf;
234
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);
241
242 inflateInit(&stream);
243 ret = inflate(&stream, 0);
a44c9a5e
LT
244 if (ret < Z_OK)
245 return NULL;
0fcfd160
LT
246 if (sscanf(buffer, "%10s %lu", type, size) != 2)
247 return NULL;
248
249 bytes = strlen(buffer) + 1;
812666c8 250 buf = xmalloc(*size);
0fcfd160
LT
251
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)
258 /* nothing */;
259 }
260 inflateEnd(&stream);
261 return buf;
262}
263
264void * read_sha1_file(const unsigned char *sha1, char *type, unsigned long *size)
265{
266 unsigned long mapsize;
267 void *map, *buf;
268
269 map = map_sha1_file(sha1, &mapsize);
270 if (map) {
271 buf = unpack_sha1_file(map, mapsize, type, size);
272 munmap(map, mapsize);
273 return buf;
274 }
275 return NULL;
276}
277
40469ee9
JH
278void *read_object_with_reference(const unsigned char *sha1,
279 const unsigned char *required_type,
280 unsigned long *size,
281 unsigned char *actual_sha1_return)
f4913f91
JH
282{
283 char type[20];
284 void *buffer;
285 unsigned long isize;
40469ee9 286 unsigned char actual_sha1[20];
f4913f91 287
40469ee9
JH
288 memcpy(actual_sha1, sha1, 20);
289 while (1) {
290 int ref_length = -1;
291 const char *ref_type = NULL;
f4913f91 292
40469ee9
JH
293 buffer = read_sha1_file(actual_sha1, type, &isize);
294 if (!buffer)
295 return NULL;
296 if (!strcmp(type, required_type)) {
297 *size = isize;
298 if (actual_sha1_return)
299 memcpy(actual_sha1_return, actual_sha1, 20);
300 return buffer;
301 }
302 /* Handle references */
303 else if (!strcmp(type, "commit"))
304 ref_type = "tree ";
305 else if (!strcmp(type, "tag"))
306 ref_type = "object ";
307 else {
308 free(buffer);
309 return NULL;
310 }
311 ref_length = strlen(ref_type);
f4913f91 312
40469ee9
JH
313 if (memcmp(buffer, ref_type, ref_length) ||
314 get_sha1_hex(buffer + ref_length, actual_sha1)) {
315 free(buffer);
316 return NULL;
317 }
318 /* Now we have the ID of the referred-to object in
319 * actual_sha1. Check again. */
f4913f91 320 }
f4913f91
JH
321}
322
f5b913c9 323int write_sha1_file(char *buf, unsigned long len, const char *type, unsigned char *returnsha1)
0fcfd160
LT
324{
325 int size;
326 char *compressed;
327 z_stream stream;
328 unsigned char sha1[20];
329 SHA_CTX c;
706bc531 330 char *filename;
a44c9a5e
LT
331 char hdr[50];
332 int fd, hdrlen;
333
334 /* Generate the header */
f5b913c9 335 hdrlen = sprintf(hdr, "%s %lu", type, len)+1;
0fcfd160 336
d98b46f8
LT
337 /* Sha1.. */
338 SHA1_Init(&c);
a44c9a5e 339 SHA1_Update(&c, hdr, hdrlen);
d98b46f8
LT
340 SHA1_Update(&c, buf, len);
341 SHA1_Final(sha1, &c);
342
706bc531
LT
343 if (returnsha1)
344 memcpy(returnsha1, sha1, 20);
345
346 filename = sha1_file_name(sha1);
347 fd = open(filename, O_WRONLY | O_CREAT | O_EXCL, 0666);
348 if (fd < 0) {
349 if (errno != EEXIST)
350 return -1;
351
352 /*
353 * We might do collision checking here, but we'd need to
354 * uncompress the old file and check it. Later.
355 */
356 return 0;
357 }
358
0fcfd160
LT
359 /* Set it up */
360 memset(&stream, 0, sizeof(stream));
361 deflateInit(&stream, Z_BEST_COMPRESSION);
a44c9a5e 362 size = deflateBound(&stream, len+hdrlen);
812666c8 363 compressed = xmalloc(size);
0fcfd160
LT
364
365 /* Compress it */
0fcfd160
LT
366 stream.next_out = compressed;
367 stream.avail_out = size;
a44c9a5e
LT
368
369 /* First header.. */
370 stream.next_in = hdr;
371 stream.avail_in = hdrlen;
372 while (deflate(&stream, 0) == Z_OK)
373 /* nothing */
374
375 /* Then the data itself.. */
376 stream.next_in = buf;
377 stream.avail_in = len;
0fcfd160
LT
378 while (deflate(&stream, Z_FINISH) == Z_OK)
379 /* nothing */;
380 deflateEnd(&stream);
381 size = stream.total_out;
382
706bc531
LT
383 if (write(fd, compressed, size) != size)
384 die("unable to write file");
385 close(fd);
386
0fcfd160
LT
387 return 0;
388}
389
390static inline int collision_check(char *filename, void *buf, unsigned int size)
391{
392#ifdef COLLISION_CHECK
393 void *map;
394 int fd = open(filename, O_RDONLY);
395 struct stat st;
396 int cmp;
397
398 /* Unreadable object, or object went away? Strange. */
399 if (fd < 0)
400 return -1;
401
402 if (fstat(fd, &st) < 0 || size != st.st_size)
403 return -1;
404
405 map = mmap(NULL, size, PROT_READ, MAP_PRIVATE, fd, 0);
406 close(fd);
407 if (map == MAP_FAILED)
408 return -1;
409 cmp = memcmp(buf, map, size);
410 munmap(map, size);
411 if (cmp)
412 return -1;
413#endif
414 return 0;
415}
416
417int write_sha1_buffer(const unsigned char *sha1, void *buf, unsigned int size)
418{
419 char *filename = sha1_file_name(sha1);
420 int fd;
421
422 fd = open(filename, O_WRONLY | O_CREAT | O_EXCL, 0666);
423 if (fd < 0) {
424 if (errno != EEXIST)
425 return -1;
426 if (collision_check(filename, buf, size))
427 return error("SHA1 collision detected!"
428 " This is bad, bad, BAD!\a\n");
429 return 0;
430 }
431 write(fd, buf, size);
432 close(fd);
433 return 0;
434}
8237b185
DB
435
436int write_sha1_from_fd(const unsigned char *sha1, int fd)
437{
438 char *filename = sha1_file_name(sha1);
439
440 int local;
441 z_stream stream;
442 unsigned char real_sha1[20];
443 char buf[4096];
444 char discard[4096];
445 int ret;
446 SHA_CTX c;
447
448 local = open(filename, O_WRONLY | O_CREAT | O_EXCL, 0666);
449
450 if (local < 0)
451 return error("Couldn't open %s\n", filename);
452
453 memset(&stream, 0, sizeof(stream));
454
455 inflateInit(&stream);
456
457 SHA1_Init(&c);
458
459 do {
460 ssize_t size;
461 size = read(fd, buf, 4096);
462 if (size <= 0) {
463 close(local);
464 unlink(filename);
465 if (!size)
466 return error("Connection closed?");
467 perror("Reading from connection");
468 return -1;
469 }
470 write(local, buf, size);
471 stream.avail_in = size;
472 stream.next_in = buf;
473 do {
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) -
478 stream.avail_out);
479 } while (stream.avail_in && ret == Z_OK);
480
481 } while (ret == Z_OK);
482 inflateEnd(&stream);
483
484 close(local);
485 SHA1_Final(real_sha1, &c);
486 if (ret != Z_STREAM_END) {
487 unlink(filename);
488 return error("File %s corrupted", sha1_to_hex(sha1));
489 }
490 if (memcmp(sha1, real_sha1, 20)) {
491 unlink(filename);
492 return error("File %s has bad hash\n", sha1_to_hex(sha1));
493 }
494
495 return 0;
496}
497
498int has_sha1_file(const unsigned char *sha1)
499{
500 char *filename = sha1_file_name(sha1);
501 struct stat st;
502
503 if (!stat(filename, &st))
504 return 1;
505 return 0;
506}