[PATCH] Kill a bunch of pointer sign warnings for gcc4
[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>
a1df57ab 10#include <limits.h>
0fcfd160
LT
11#include "cache.h"
12
144bde78
LT
13#ifndef O_NOATIME
14#if defined(__linux__) && (defined(__i386__) || defined(__PPC__))
15#define O_NOATIME 01000000
16#else
17#define O_NOATIME 0
18#endif
19#endif
20
21static unsigned int sha1_file_open_flag = O_NOATIME;
22
0fcfd160
LT
23static unsigned hexval(char c)
24{
25 if (c >= '0' && c <= '9')
26 return c - '0';
27 if (c >= 'a' && c <= 'f')
28 return c - 'a' + 10;
29 if (c >= 'A' && c <= 'F')
30 return c - 'A' + 10;
31 return ~0;
32}
33
34int get_sha1_hex(const char *hex, unsigned char *sha1)
35{
36 int i;
37 for (i = 0; i < 20; i++) {
38 unsigned int val = (hexval(hex[0]) << 4) | hexval(hex[1]);
39 if (val & ~0xff)
40 return -1;
41 *sha1++ = val;
42 hex += 2;
43 }
44 return 0;
45}
46
3c249c95
LT
47int get_sha1_file(const char *path, unsigned char *result)
48{
49 char buffer[60];
50 int fd = open(path, O_RDONLY);
51 int len;
52
53 if (fd < 0)
54 return -1;
55 len = read(fd, buffer, sizeof(buffer));
56 close(fd);
57 if (len < 40)
58 return -1;
59 return get_sha1_hex(buffer, result);
60}
61
8ac069ac
JH
62static char *git_dir, *git_object_dir, *git_index_file;
63static void setup_git_env(void)
64{
65 git_dir = gitenv(GIT_DIR_ENVIRONMENT);
66 if (!git_dir)
67 git_dir = DEFAULT_GIT_DIR_ENVIRONMENT;
68 git_object_dir = gitenv(DB_ENVIRONMENT);
69 if (!git_object_dir) {
70 git_object_dir = xmalloc(strlen(git_dir) + 9);
71 sprintf(git_object_dir, "%s/objects", git_dir);
72 }
73 git_index_file = gitenv(INDEX_ENVIRONMENT);
74 if (!git_index_file) {
75 git_index_file = xmalloc(strlen(git_dir) + 7);
76 sprintf(git_index_file, "%s/index", git_dir);
77 }
78}
79
80char *get_object_directory(void)
81{
82 if (!git_object_dir)
83 setup_git_env();
84 return git_object_dir;
85}
86
87char *get_index_file(void)
88{
89 if (!git_index_file)
90 setup_git_env();
91 return git_index_file;
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 };
35ad3382 105 const char **p;
3c249c95
LT
106
107 if (!get_sha1_hex(str, sha1))
108 return 0;
35ad3382 109
8ac069ac
JH
110 if (!git_dir)
111 setup_git_env();
35ad3382 112 for (p = prefix; *p; p++) {
8ac069ac
JH
113 snprintf(pathname, sizeof(pathname), "%s/%s/%s",
114 git_dir, *p, str);
35ad3382
LT
115 if (!get_sha1_file(pathname, sha1))
116 return 0;
117 }
118
3c249c95
LT
119 return -1;
120}
121
0fcfd160
LT
122char * sha1_to_hex(const unsigned char *sha1)
123{
124 static char buffer[50];
125 static const char hex[] = "0123456789abcdef";
126 char *buf = buffer;
127 int i;
128
129 for (i = 0; i < 20; i++) {
130 unsigned int val = *sha1++;
131 *buf++ = hex[val >> 4];
132 *buf++ = hex[val & 0xf];
133 }
134 return buffer;
135}
136
ace1534d
JH
137static void fill_sha1_path(char *pathbuf, const unsigned char *sha1)
138{
139 int i;
140 for (i = 0; i < 20; i++) {
141 static char hex[] = "0123456789abcdef";
142 unsigned int val = sha1[i];
143 char *pos = pathbuf + i*2 + (i > 0);
144 *pos++ = hex[val >> 4];
145 *pos = hex[val & 0xf];
146 }
147}
148
0fcfd160
LT
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.
ace1534d
JH
153 *
154 * Also note that this returns the location for creating. Reading
155 * SHA1 file can happen from any alternate directory listed in the
d19938ab 156 * DB_ENVIRONMENT environment variable if it is not found in
ace1534d 157 * the primary object database.
0fcfd160
LT
158 */
159char *sha1_file_name(const unsigned char *sha1)
160{
0fcfd160
LT
161 static char *name, *base;
162
163 if (!base) {
d19938ab 164 const char *sha1_file_directory = get_object_directory();
0fcfd160 165 int len = strlen(sha1_file_directory);
812666c8 166 base = xmalloc(len + 60);
0fcfd160
LT
167 memcpy(base, sha1_file_directory, len);
168 memset(base+len, 0, 60);
169 base[len] = '/';
170 base[len+3] = '/';
171 name = base + len + 1;
172 }
ace1534d 173 fill_sha1_path(name, sha1);
0fcfd160
LT
174 return base;
175}
176
ddd5d056 177static struct alternate_object_database {
ace1534d
JH
178 char *base;
179 char *name;
180} *alt_odb;
181
ddd5d056
JH
182/*
183 * Prepare alternate object database registry.
184 * alt_odb points at an array of struct alternate_object_database.
185 * This array is terminated with an element that has both its base
186 * and name set to NULL. alt_odb[n] comes from n'th non-empty
d19938ab 187 * element from colon separated ALTERNATE_DB_ENVIRONMENT environment
ddd5d056
JH
188 * variable, and its base points at a statically allocated buffer
189 * that contains "/the/directory/corresponding/to/.git/objects/...",
190 * while its name points just after the slash at the end of
191 * ".git/objects/" in the example above, and has enough space to hold
192 * 40-byte hex SHA1, an extra slash for the first level indirection,
193 * and the terminating NUL.
194 * This function allocates the alt_odb array and all the strings
195 * pointed by base fields of the array elements with one xmalloc();
196 * the string pool immediately follows the array.
197 */
ace1534d
JH
198static void prepare_alt_odb(void)
199{
200 int pass, totlen, i;
ace1534d
JH
201 const char *cp, *last;
202 char *op = 0;
d19938ab 203 const char *alt = gitenv(ALTERNATE_DB_ENVIRONMENT) ? : "";
ace1534d 204
ddd5d056
JH
205 /* The first pass counts how large an area to allocate to
206 * hold the entire alt_odb structure, including array of
207 * structs and path buffers for them. The second pass fills
208 * the structure and prepares the path buffers for use by
209 * fill_sha1_path().
210 */
ace1534d
JH
211 for (totlen = pass = 0; pass < 2; pass++) {
212 last = alt;
213 i = 0;
214 do {
215 cp = strchr(last, ':') ? : last + strlen(last);
216 if (last != cp) {
217 /* 43 = 40-byte + 2 '/' + terminating NUL */
218 int pfxlen = cp - last;
219 int entlen = pfxlen + 43;
220 if (pass == 0)
221 totlen += entlen;
222 else {
223 alt_odb[i].base = op;
224 alt_odb[i].name = op + pfxlen + 1;
225 memcpy(op, last, pfxlen);
226 op[pfxlen] = op[pfxlen + 3] = '/';
227 op[entlen-1] = 0;
228 op += entlen;
229 }
230 i++;
231 }
232 while (*cp && *cp == ':')
233 cp++;
234 last = cp;
235 } while (*cp);
236 if (pass)
237 break;
ddd5d056 238 alt_odb = xmalloc(sizeof(*alt_odb) * (i + 1) + totlen);
ace1534d
JH
239 alt_odb[i].base = alt_odb[i].name = 0;
240 op = (char*)(&alt_odb[i+1]);
241 }
242}
243
244static char *find_sha1_file(const unsigned char *sha1, struct stat *st)
245{
246 int i;
247 char *name = sha1_file_name(sha1);
248
249 if (!stat(name, st))
250 return name;
251 if (!alt_odb)
252 prepare_alt_odb();
253 for (i = 0; (name = alt_odb[i].name) != NULL; i++) {
254 fill_sha1_path(name, sha1);
255 if (!stat(alt_odb[i].base, st))
256 return alt_odb[i].base;
257 }
258 return NULL;
259}
260
d98b46f8 261int check_sha1_signature(unsigned char *sha1, void *map, unsigned long size, const char *type)
0fcfd160 262{
d98b46f8 263 char header[100];
0fcfd160
LT
264 unsigned char real_sha1[20];
265 SHA_CTX c;
266
267 SHA1_Init(&c);
d98b46f8 268 SHA1_Update(&c, header, 1+sprintf(header, "%s %lu", type, size));
0fcfd160
LT
269 SHA1_Update(&c, map, size);
270 SHA1_Final(real_sha1, &c);
271 return memcmp(sha1, real_sha1, 20) ? -1 : 0;
272}
273
274void *map_sha1_file(const unsigned char *sha1, unsigned long *size)
275{
0fcfd160
LT
276 struct stat st;
277 void *map;
144bde78 278 int fd;
ace1534d
JH
279 char *filename = find_sha1_file(sha1, &st);
280
281 if (!filename) {
282 error("cannot map sha1 file %s", sha1_to_hex(sha1));
283 return NULL;
284 }
0fcfd160 285
144bde78 286 fd = open(filename, O_RDONLY | sha1_file_open_flag);
0fcfd160 287 if (fd < 0) {
144bde78
LT
288 /* See if it works without O_NOATIME */
289 switch (sha1_file_open_flag) {
290 default:
291 fd = open(filename, O_RDONLY);
292 if (fd >= 0)
293 break;
294 /* Fallthrough */
295 case 0:
296 perror(filename);
297 return NULL;
298 }
299
300 /* If it failed once, it will probably fail again. Stop using O_NOATIME */
301 sha1_file_open_flag = 0;
0fcfd160 302 }
0fcfd160
LT
303 map = mmap(NULL, st.st_size, PROT_READ, MAP_PRIVATE, fd, 0);
304 close(fd);
305 if (-1 == (int)(long)map)
306 return NULL;
307 *size = st.st_size;
308 return map;
309}
310
311void * unpack_sha1_file(void *map, unsigned long mapsize, char *type, unsigned long *size)
312{
313 int ret, bytes;
314 z_stream stream;
315 char buffer[8192];
bf0f910d 316 unsigned char *buf;
0fcfd160
LT
317
318 /* Get the data stream */
319 memset(&stream, 0, sizeof(stream));
320 stream.next_in = map;
321 stream.avail_in = mapsize;
bf0f910d 322 stream.next_out = (unsigned char *)buffer;
0fcfd160
LT
323 stream.avail_out = sizeof(buffer);
324
325 inflateInit(&stream);
326 ret = inflate(&stream, 0);
a44c9a5e
LT
327 if (ret < Z_OK)
328 return NULL;
0fcfd160
LT
329 if (sscanf(buffer, "%10s %lu", type, size) != 2)
330 return NULL;
331
332 bytes = strlen(buffer) + 1;
812666c8 333 buf = xmalloc(*size);
0fcfd160
LT
334
335 memcpy(buf, buffer + bytes, stream.total_out - bytes);
336 bytes = stream.total_out - bytes;
337 if (bytes < *size && ret == Z_OK) {
338 stream.next_out = buf + bytes;
339 stream.avail_out = *size - bytes;
340 while (inflate(&stream, Z_FINISH) == Z_OK)
341 /* nothing */;
342 }
343 inflateEnd(&stream);
344 return buf;
345}
346
347void * read_sha1_file(const unsigned char *sha1, char *type, unsigned long *size)
348{
349 unsigned long mapsize;
350 void *map, *buf;
351
352 map = map_sha1_file(sha1, &mapsize);
353 if (map) {
354 buf = unpack_sha1_file(map, mapsize, type, size);
355 munmap(map, mapsize);
356 return buf;
357 }
358 return NULL;
359}
360
40469ee9 361void *read_object_with_reference(const unsigned char *sha1,
bf0f910d 362 const char *required_type,
40469ee9
JH
363 unsigned long *size,
364 unsigned char *actual_sha1_return)
f4913f91
JH
365{
366 char type[20];
367 void *buffer;
368 unsigned long isize;
40469ee9 369 unsigned char actual_sha1[20];
f4913f91 370
40469ee9
JH
371 memcpy(actual_sha1, sha1, 20);
372 while (1) {
373 int ref_length = -1;
374 const char *ref_type = NULL;
f4913f91 375
40469ee9
JH
376 buffer = read_sha1_file(actual_sha1, type, &isize);
377 if (!buffer)
378 return NULL;
379 if (!strcmp(type, required_type)) {
380 *size = isize;
381 if (actual_sha1_return)
382 memcpy(actual_sha1_return, actual_sha1, 20);
383 return buffer;
384 }
385 /* Handle references */
386 else if (!strcmp(type, "commit"))
387 ref_type = "tree ";
388 else if (!strcmp(type, "tag"))
389 ref_type = "object ";
390 else {
391 free(buffer);
392 return NULL;
393 }
394 ref_length = strlen(ref_type);
f4913f91 395
40469ee9
JH
396 if (memcmp(buffer, ref_type, ref_length) ||
397 get_sha1_hex(buffer + ref_length, actual_sha1)) {
398 free(buffer);
399 return NULL;
400 }
401 /* Now we have the ID of the referred-to object in
402 * actual_sha1. Check again. */
f4913f91 403 }
f4913f91
JH
404}
405
bf0f910d 406int write_sha1_file(void *buf, unsigned long len, const char *type, unsigned char *returnsha1)
0fcfd160
LT
407{
408 int size;
bf0f910d 409 unsigned char *compressed;
0fcfd160
LT
410 z_stream stream;
411 unsigned char sha1[20];
412 SHA_CTX c;
706bc531 413 char *filename;
aac17941 414 static char tmpfile[PATH_MAX];
bf0f910d 415 unsigned char hdr[50];
aac17941 416 int fd, hdrlen, ret;
a44c9a5e
LT
417
418 /* Generate the header */
bf0f910d 419 hdrlen = sprintf((char *)hdr, "%s %lu", type, len)+1;
0fcfd160 420
d98b46f8
LT
421 /* Sha1.. */
422 SHA1_Init(&c);
a44c9a5e 423 SHA1_Update(&c, hdr, hdrlen);
d98b46f8
LT
424 SHA1_Update(&c, buf, len);
425 SHA1_Final(sha1, &c);
426
706bc531
LT
427 if (returnsha1)
428 memcpy(returnsha1, sha1, 20);
429
430 filename = sha1_file_name(sha1);
aac17941
LT
431 fd = open(filename, O_RDONLY);
432 if (fd >= 0) {
706bc531 433 /*
aac17941
LT
434 * FIXME!!! We might do collision checking here, but we'd
435 * need to uncompress the old file and check it. Later.
706bc531 436 */
aac17941 437 close(fd);
706bc531
LT
438 return 0;
439 }
440
aac17941
LT
441 if (errno != ENOENT) {
442 fprintf(stderr, "sha1 file %s: %s", filename, strerror(errno));
443 return -1;
444 }
445
446 snprintf(tmpfile, sizeof(tmpfile), "%s/obj_XXXXXX", get_object_directory());
ace1534d 447
aac17941
LT
448 fd = mkstemp(tmpfile);
449 if (fd < 0) {
450 fprintf(stderr, "unable to create temporary sha1 filename %s: %s", tmpfile, strerror(errno));
451 return -1;
452 }
453
0fcfd160
LT
454 /* Set it up */
455 memset(&stream, 0, sizeof(stream));
456 deflateInit(&stream, Z_BEST_COMPRESSION);
a44c9a5e 457 size = deflateBound(&stream, len+hdrlen);
812666c8 458 compressed = xmalloc(size);
0fcfd160
LT
459
460 /* Compress it */
0fcfd160
LT
461 stream.next_out = compressed;
462 stream.avail_out = size;
a44c9a5e
LT
463
464 /* First header.. */
465 stream.next_in = hdr;
466 stream.avail_in = hdrlen;
467 while (deflate(&stream, 0) == Z_OK)
6ffcee88 468 /* nothing */;
a44c9a5e
LT
469
470 /* Then the data itself.. */
471 stream.next_in = buf;
472 stream.avail_in = len;
0fcfd160
LT
473 while (deflate(&stream, Z_FINISH) == Z_OK)
474 /* nothing */;
475 deflateEnd(&stream);
476 size = stream.total_out;
477
706bc531
LT
478 if (write(fd, compressed, size) != size)
479 die("unable to write file");
aac17941 480 fchmod(fd, 0444);
706bc531 481 close(fd);
383f85b7 482 free(compressed);
0fcfd160 483
aac17941 484 ret = link(tmpfile, filename);
a31c6d02 485 if (ret < 0) {
aac17941 486 ret = errno;
a31c6d02
LT
487
488 /*
489 * Coda hack - coda doesn't like cross-directory links,
490 * so we fall back to a rename, which will mean that it
491 * won't be able to check collisions, but that's not a
492 * big deal.
493 *
494 * When this succeeds, we just return 0. We have nothing
495 * left to unlink.
496 */
497 if (ret == EXDEV && !rename(tmpfile, filename))
498 return 0;
499 }
aac17941
LT
500 unlink(tmpfile);
501 if (ret) {
502 if (ret != EEXIST) {
503 fprintf(stderr, "unable to write sha1 filename %s: %s", filename, strerror(ret));
0fcfd160 504 return -1;
aac17941
LT
505 }
506 /* FIXME!!! Collision check here ? */
0fcfd160 507 }
aac17941 508
0fcfd160
LT
509 return 0;
510}
8237b185
DB
511
512int write_sha1_from_fd(const unsigned char *sha1, int fd)
513{
514 char *filename = sha1_file_name(sha1);
515
516 int local;
517 z_stream stream;
518 unsigned char real_sha1[20];
bf0f910d
BG
519 unsigned char buf[4096];
520 unsigned char discard[4096];
8237b185
DB
521 int ret;
522 SHA_CTX c;
523
524 local = open(filename, O_WRONLY | O_CREAT | O_EXCL, 0666);
525
526 if (local < 0)
527 return error("Couldn't open %s\n", filename);
528
529 memset(&stream, 0, sizeof(stream));
530
531 inflateInit(&stream);
532
533 SHA1_Init(&c);
534
535 do {
536 ssize_t size;
537 size = read(fd, buf, 4096);
538 if (size <= 0) {
539 close(local);
540 unlink(filename);
541 if (!size)
542 return error("Connection closed?");
543 perror("Reading from connection");
544 return -1;
545 }
546 write(local, buf, size);
547 stream.avail_in = size;
548 stream.next_in = buf;
549 do {
550 stream.next_out = discard;
551 stream.avail_out = sizeof(discard);
552 ret = inflate(&stream, Z_SYNC_FLUSH);
553 SHA1_Update(&c, discard, sizeof(discard) -
554 stream.avail_out);
555 } while (stream.avail_in && ret == Z_OK);
556
557 } while (ret == Z_OK);
558 inflateEnd(&stream);
559
560 close(local);
561 SHA1_Final(real_sha1, &c);
562 if (ret != Z_STREAM_END) {
563 unlink(filename);
564 return error("File %s corrupted", sha1_to_hex(sha1));
565 }
566 if (memcmp(sha1, real_sha1, 20)) {
567 unlink(filename);
568 return error("File %s has bad hash\n", sha1_to_hex(sha1));
569 }
570
571 return 0;
572}
573
574int has_sha1_file(const unsigned char *sha1)
575{
8237b185 576 struct stat st;
ace1534d 577 return !!find_sha1_file(sha1, &st);
8237b185 578}
74400e71
JH
579
580int index_fd(unsigned char *sha1, int fd, struct stat *st)
581{
74400e71 582 unsigned long size = st->st_size;
aac17941
LT
583 void *buf;
584 int ret;
74400e71 585
aac17941 586 buf = "";
74400e71 587 if (size)
aac17941 588 buf = mmap(NULL, size, PROT_READ, MAP_PRIVATE, fd, 0);
74400e71 589 close(fd);
aac17941 590 if ((int)(long)buf == -1)
74400e71
JH
591 return -1;
592
aac17941
LT
593 ret = write_sha1_file(buf, size, "blob", sha1);
594 if (size)
595 munmap(buf, size);
596 return ret;
74400e71 597}