[PATCH] Operations on refs
[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 */
0fcfd160 9#include "cache.h"
91d7b8af 10#include "delta.h"
0fcfd160 11
144bde78
LT
12#ifndef O_NOATIME
13#if defined(__linux__) && (defined(__i386__) || defined(__PPC__))
14#define O_NOATIME 01000000
15#else
16#define O_NOATIME 0
17#endif
18#endif
19
20static unsigned int sha1_file_open_flag = O_NOATIME;
21
0fcfd160
LT
22static unsigned hexval(char c)
23{
24 if (c >= '0' && c <= '9')
25 return c - '0';
26 if (c >= 'a' && c <= 'f')
27 return c - 'a' + 10;
28 if (c >= 'A' && c <= 'F')
29 return c - 'A' + 10;
30 return ~0;
31}
32
33int get_sha1_hex(const char *hex, unsigned char *sha1)
34{
35 int i;
36 for (i = 0; i < 20; i++) {
37 unsigned int val = (hexval(hex[0]) << 4) | hexval(hex[1]);
38 if (val & ~0xff)
39 return -1;
40 *sha1++ = val;
41 hex += 2;
42 }
43 return 0;
44}
45
e99d59ff 46static int get_sha1_file(const char *path, unsigned char *result)
3c249c95
LT
47{
48 char buffer[60];
49 int fd = open(path, O_RDONLY);
50 int len;
51
52 if (fd < 0)
53 return -1;
54 len = read(fd, buffer, sizeof(buffer));
55 close(fd);
56 if (len < 40)
57 return -1;
58 return get_sha1_hex(buffer, result);
59}
60
95fc7512 61static char *git_dir, *git_object_dir, *git_index_file, *git_refs_dir;
8ac069ac
JH
62static void setup_git_env(void)
63{
64 git_dir = gitenv(GIT_DIR_ENVIRONMENT);
65 if (!git_dir)
66 git_dir = DEFAULT_GIT_DIR_ENVIRONMENT;
67 git_object_dir = gitenv(DB_ENVIRONMENT);
68 if (!git_object_dir) {
69 git_object_dir = xmalloc(strlen(git_dir) + 9);
70 sprintf(git_object_dir, "%s/objects", git_dir);
71 }
95fc7512
DB
72 git_refs_dir = xmalloc(strlen(git_dir) + 6);
73 sprintf(git_refs_dir, "%s/refs", git_dir);
8ac069ac
JH
74 git_index_file = gitenv(INDEX_ENVIRONMENT);
75 if (!git_index_file) {
76 git_index_file = xmalloc(strlen(git_dir) + 7);
77 sprintf(git_index_file, "%s/index", git_dir);
78 }
79}
80
81char *get_object_directory(void)
82{
83 if (!git_object_dir)
84 setup_git_env();
85 return git_object_dir;
86}
87
95fc7512
DB
88char *get_refs_directory(void)
89{
90 if (!git_refs_dir)
91 setup_git_env();
92 return git_refs_dir;
93}
94
8ac069ac
JH
95char *get_index_file(void)
96{
97 if (!git_index_file)
98 setup_git_env();
99 return git_index_file;
100}
101
3c249c95
LT
102int get_sha1(const char *str, unsigned char *sha1)
103{
104 static char pathname[PATH_MAX];
35ad3382
LT
105 static const char *prefix[] = {
106 "",
107 "refs",
108 "refs/tags",
109 "refs/heads",
110 "refs/snap",
111 NULL
112 };
35ad3382 113 const char **p;
3c249c95
LT
114
115 if (!get_sha1_hex(str, sha1))
116 return 0;
35ad3382 117
8ac069ac
JH
118 if (!git_dir)
119 setup_git_env();
35ad3382 120 for (p = prefix; *p; p++) {
8ac069ac
JH
121 snprintf(pathname, sizeof(pathname), "%s/%s/%s",
122 git_dir, *p, str);
35ad3382
LT
123 if (!get_sha1_file(pathname, sha1))
124 return 0;
125 }
126
3c249c95
LT
127 return -1;
128}
129
0fcfd160
LT
130char * sha1_to_hex(const unsigned char *sha1)
131{
132 static char buffer[50];
133 static const char hex[] = "0123456789abcdef";
134 char *buf = buffer;
135 int i;
136
137 for (i = 0; i < 20; i++) {
138 unsigned int val = *sha1++;
139 *buf++ = hex[val >> 4];
140 *buf++ = hex[val & 0xf];
141 }
142 return buffer;
143}
144
ace1534d
JH
145static void fill_sha1_path(char *pathbuf, const unsigned char *sha1)
146{
147 int i;
148 for (i = 0; i < 20; i++) {
149 static char hex[] = "0123456789abcdef";
150 unsigned int val = sha1[i];
151 char *pos = pathbuf + i*2 + (i > 0);
152 *pos++ = hex[val >> 4];
153 *pos = hex[val & 0xf];
154 }
155}
156
0fcfd160
LT
157/*
158 * NOTE! This returns a statically allocated buffer, so you have to be
159 * careful about using it. Do a "strdup()" if you need to save the
160 * filename.
ace1534d
JH
161 *
162 * Also note that this returns the location for creating. Reading
163 * SHA1 file can happen from any alternate directory listed in the
d19938ab 164 * DB_ENVIRONMENT environment variable if it is not found in
ace1534d 165 * the primary object database.
0fcfd160
LT
166 */
167char *sha1_file_name(const unsigned char *sha1)
168{
0fcfd160
LT
169 static char *name, *base;
170
171 if (!base) {
d19938ab 172 const char *sha1_file_directory = get_object_directory();
0fcfd160 173 int len = strlen(sha1_file_directory);
812666c8 174 base = xmalloc(len + 60);
0fcfd160
LT
175 memcpy(base, sha1_file_directory, len);
176 memset(base+len, 0, 60);
177 base[len] = '/';
178 base[len+3] = '/';
179 name = base + len + 1;
180 }
ace1534d 181 fill_sha1_path(name, sha1);
0fcfd160
LT
182 return base;
183}
184
ddd5d056 185static struct alternate_object_database {
ace1534d
JH
186 char *base;
187 char *name;
188} *alt_odb;
189
ddd5d056
JH
190/*
191 * Prepare alternate object database registry.
192 * alt_odb points at an array of struct alternate_object_database.
193 * This array is terminated with an element that has both its base
194 * and name set to NULL. alt_odb[n] comes from n'th non-empty
d19938ab 195 * element from colon separated ALTERNATE_DB_ENVIRONMENT environment
ddd5d056
JH
196 * variable, and its base points at a statically allocated buffer
197 * that contains "/the/directory/corresponding/to/.git/objects/...",
198 * while its name points just after the slash at the end of
199 * ".git/objects/" in the example above, and has enough space to hold
200 * 40-byte hex SHA1, an extra slash for the first level indirection,
201 * and the terminating NUL.
202 * This function allocates the alt_odb array and all the strings
203 * pointed by base fields of the array elements with one xmalloc();
204 * the string pool immediately follows the array.
205 */
ace1534d
JH
206static void prepare_alt_odb(void)
207{
208 int pass, totlen, i;
ace1534d 209 const char *cp, *last;
e99d59ff 210 char *op = NULL;
d19938ab 211 const char *alt = gitenv(ALTERNATE_DB_ENVIRONMENT) ? : "";
ace1534d 212
ddd5d056
JH
213 /* The first pass counts how large an area to allocate to
214 * hold the entire alt_odb structure, including array of
215 * structs and path buffers for them. The second pass fills
216 * the structure and prepares the path buffers for use by
217 * fill_sha1_path().
218 */
ace1534d
JH
219 for (totlen = pass = 0; pass < 2; pass++) {
220 last = alt;
221 i = 0;
222 do {
223 cp = strchr(last, ':') ? : last + strlen(last);
224 if (last != cp) {
225 /* 43 = 40-byte + 2 '/' + terminating NUL */
226 int pfxlen = cp - last;
227 int entlen = pfxlen + 43;
228 if (pass == 0)
229 totlen += entlen;
230 else {
231 alt_odb[i].base = op;
232 alt_odb[i].name = op + pfxlen + 1;
233 memcpy(op, last, pfxlen);
234 op[pfxlen] = op[pfxlen + 3] = '/';
235 op[entlen-1] = 0;
236 op += entlen;
237 }
238 i++;
239 }
240 while (*cp && *cp == ':')
241 cp++;
242 last = cp;
243 } while (*cp);
244 if (pass)
245 break;
ddd5d056 246 alt_odb = xmalloc(sizeof(*alt_odb) * (i + 1) + totlen);
e99d59ff 247 alt_odb[i].base = alt_odb[i].name = NULL;
ace1534d
JH
248 op = (char*)(&alt_odb[i+1]);
249 }
250}
251
252static char *find_sha1_file(const unsigned char *sha1, struct stat *st)
253{
254 int i;
255 char *name = sha1_file_name(sha1);
256
257 if (!stat(name, st))
258 return name;
259 if (!alt_odb)
260 prepare_alt_odb();
261 for (i = 0; (name = alt_odb[i].name) != NULL; i++) {
262 fill_sha1_path(name, sha1);
263 if (!stat(alt_odb[i].base, st))
264 return alt_odb[i].base;
265 }
266 return NULL;
267}
268
d98b46f8 269int check_sha1_signature(unsigned char *sha1, void *map, unsigned long size, const char *type)
0fcfd160 270{
d98b46f8 271 char header[100];
0fcfd160
LT
272 unsigned char real_sha1[20];
273 SHA_CTX c;
274
275 SHA1_Init(&c);
d98b46f8 276 SHA1_Update(&c, header, 1+sprintf(header, "%s %lu", type, size));
0fcfd160
LT
277 SHA1_Update(&c, map, size);
278 SHA1_Final(real_sha1, &c);
279 return memcmp(sha1, real_sha1, 20) ? -1 : 0;
280}
281
282void *map_sha1_file(const unsigned char *sha1, unsigned long *size)
283{
0fcfd160
LT
284 struct stat st;
285 void *map;
144bde78 286 int fd;
ace1534d
JH
287 char *filename = find_sha1_file(sha1, &st);
288
289 if (!filename) {
290 error("cannot map sha1 file %s", sha1_to_hex(sha1));
291 return NULL;
292 }
0fcfd160 293
144bde78 294 fd = open(filename, O_RDONLY | sha1_file_open_flag);
0fcfd160 295 if (fd < 0) {
144bde78
LT
296 /* See if it works without O_NOATIME */
297 switch (sha1_file_open_flag) {
298 default:
299 fd = open(filename, O_RDONLY);
300 if (fd >= 0)
301 break;
302 /* Fallthrough */
303 case 0:
304 perror(filename);
305 return NULL;
306 }
307
308 /* If it failed once, it will probably fail again. Stop using O_NOATIME */
309 sha1_file_open_flag = 0;
0fcfd160 310 }
0fcfd160
LT
311 map = mmap(NULL, st.st_size, PROT_READ, MAP_PRIVATE, fd, 0);
312 close(fd);
313 if (-1 == (int)(long)map)
314 return NULL;
315 *size = st.st_size;
316 return map;
317}
318
c4483576
LT
319int unpack_sha1_header(z_stream *stream, void *map, unsigned long mapsize, void *buffer, unsigned long size)
320{
321 /* Get the data stream */
322 memset(stream, 0, sizeof(*stream));
323 stream->next_in = map;
324 stream->avail_in = mapsize;
325 stream->next_out = buffer;
326 stream->avail_out = size;
327
328 inflateInit(stream);
329 return inflate(stream, 0);
330}
331
5180cacc
LT
332void *unpack_sha1_rest(z_stream *stream, void *buffer, unsigned long size)
333{
334 int bytes = strlen(buffer) + 1;
335 char *buf = xmalloc(1+size);
336
337 memcpy(buf, buffer + bytes, stream->total_out - bytes);
338 bytes = stream->total_out - bytes;
339 if (bytes < size) {
340 stream->next_out = buf + bytes;
341 stream->avail_out = size - bytes;
342 while (inflate(stream, Z_FINISH) == Z_OK)
343 /* nothing */;
344 }
345 buf[size] = 0;
346 inflateEnd(stream);
347 return buf;
348}
349
350/*
351 * We used to just use "sscanf()", but that's actually way
352 * too permissive for what we want to check. So do an anal
353 * object header parse by hand.
354 */
355int parse_sha1_header(char *hdr, char *type, unsigned long *sizep)
356{
357 int i;
358 unsigned long size;
359
360 /*
361 * The type can be at most ten bytes (including the
362 * terminating '\0' that we add), and is followed by
363 * a space.
364 */
365 i = 10;
366 for (;;) {
367 char c = *hdr++;
368 if (c == ' ')
369 break;
370 if (!--i)
371 return -1;
372 *type++ = c;
373 }
374 *type = 0;
375
376 /*
377 * The length must follow immediately, and be in canonical
378 * decimal format (ie "010" is not valid).
379 */
380 size = *hdr++ - '0';
381 if (size > 9)
382 return -1;
383 if (size) {
384 for (;;) {
385 unsigned long c = *hdr - '0';
386 if (c > 9)
387 break;
388 hdr++;
389 size = size * 10 + c;
390 }
391 }
392 *sizep = size;
393
394 /*
395 * The length must be followed by a zero byte
396 */
397 return *hdr ? -1 : 0;
398}
399
0fcfd160
LT
400void * unpack_sha1_file(void *map, unsigned long mapsize, char *type, unsigned long *size)
401{
5180cacc 402 int ret;
0fcfd160 403 z_stream stream;
5180cacc 404 char hdr[8192];
0fcfd160 405
5180cacc
LT
406 ret = unpack_sha1_header(&stream, map, mapsize, hdr, sizeof(hdr));
407 if (ret < Z_OK || parse_sha1_header(hdr, type, size) < 0)
0fcfd160
LT
408 return NULL;
409
5180cacc 410 return unpack_sha1_rest(&stream, hdr, *size);
0fcfd160
LT
411}
412
4a62b619
JH
413int sha1_delta_base(const unsigned char *sha1, unsigned char *base_sha1)
414{
415 int ret;
416 unsigned long mapsize, size;
417 void *map;
418 z_stream stream;
419 char hdr[64], type[20];
420 void *delta_data_head;
421
422 map = map_sha1_file(sha1, &mapsize);
423 if (!map)
424 return -1;
425 ret = unpack_sha1_header(&stream, map, mapsize, hdr, sizeof(hdr));
426 if (ret < Z_OK || parse_sha1_header(hdr, type, &size) < 0) {
427 ret = -1;
428 goto out;
429 }
430 if (strcmp(type, "delta")) {
431 ret = 0;
432 goto out;
433 }
434
435 delta_data_head = hdr + strlen(hdr) + 1;
436 ret = 1;
437 memcpy(base_sha1, delta_data_head, 20);
438 out:
439 inflateEnd(&stream);
440 munmap(map, mapsize);
441 return ret;
442}
443
65c2e0c3
JH
444int sha1_file_size(const unsigned char *sha1, unsigned long *sizep)
445{
446 int ret, status;
447 unsigned long mapsize, size;
448 void *map;
449 z_stream stream;
450 char hdr[64], type[20];
451 const unsigned char *data;
452 unsigned char cmd;
453 int i;
454
455 map = map_sha1_file(sha1, &mapsize);
456 if (!map)
457 return -1;
458 ret = unpack_sha1_header(&stream, map, mapsize, hdr, sizeof(hdr));
459 status = -1;
460 if (ret < Z_OK || parse_sha1_header(hdr, type, &size) < 0)
461 goto out;
462 if (strcmp(type, "delta")) {
463 *sizep = size;
464 status = 0;
465 goto out;
466 }
467
468 /* We are dealing with a delta object. Inflated, the first
469 * 20 bytes hold the base object SHA1, and delta data follows
470 * immediately after it.
471 *
472 * The initial part of the delta starts at delta_data_head +
473 * 20. Borrow code from patch-delta to read the result size.
474 */
475 data = hdr + strlen(hdr) + 1 + 20;
476
477 /* Skip over the source size; we are not interested in
478 * it and we cannot verify it because we do not want
479 * to read the base object.
480 */
481 cmd = *data++;
482 while (cmd) {
483 if (cmd & 1)
484 data++;
485 cmd >>= 1;
486 }
487 /* Read the result size */
488 size = i = 0;
489 cmd = *data++;
490 while (cmd) {
491 if (cmd & 1)
492 size |= *data++ << i;
493 i += 8;
494 cmd >>= 1;
495 }
496 *sizep = size;
497 status = 0;
498 out:
499 inflateEnd(&stream);
500 munmap(map, mapsize);
501 return status;
502}
503
0fcfd160
LT
504void * read_sha1_file(const unsigned char *sha1, char *type, unsigned long *size)
505{
506 unsigned long mapsize;
507 void *map, *buf;
508
509 map = map_sha1_file(sha1, &mapsize);
510 if (map) {
511 buf = unpack_sha1_file(map, mapsize, type, size);
512 munmap(map, mapsize);
91d7b8af
NP
513 if (buf && !strcmp(type, "delta")) {
514 void *ref = NULL, *delta = buf;
515 unsigned long ref_size, delta_size = *size;
516 buf = NULL;
517 if (delta_size > 20)
518 ref = read_sha1_file(delta, type, &ref_size);
519 if (ref)
520 buf = patch_delta(ref, ref_size,
521 delta+20, delta_size-20,
522 size);
523 free(delta);
524 free(ref);
525 }
0fcfd160
LT
526 return buf;
527 }
528 return NULL;
529}
530
40469ee9 531void *read_object_with_reference(const unsigned char *sha1,
bf0f910d 532 const char *required_type,
40469ee9
JH
533 unsigned long *size,
534 unsigned char *actual_sha1_return)
f4913f91
JH
535{
536 char type[20];
537 void *buffer;
538 unsigned long isize;
40469ee9 539 unsigned char actual_sha1[20];
f4913f91 540
40469ee9
JH
541 memcpy(actual_sha1, sha1, 20);
542 while (1) {
543 int ref_length = -1;
544 const char *ref_type = NULL;
f4913f91 545
40469ee9
JH
546 buffer = read_sha1_file(actual_sha1, type, &isize);
547 if (!buffer)
548 return NULL;
549 if (!strcmp(type, required_type)) {
550 *size = isize;
551 if (actual_sha1_return)
552 memcpy(actual_sha1_return, actual_sha1, 20);
553 return buffer;
554 }
555 /* Handle references */
556 else if (!strcmp(type, "commit"))
557 ref_type = "tree ";
558 else if (!strcmp(type, "tag"))
559 ref_type = "object ";
560 else {
561 free(buffer);
562 return NULL;
563 }
564 ref_length = strlen(ref_type);
f4913f91 565
40469ee9
JH
566 if (memcmp(buffer, ref_type, ref_length) ||
567 get_sha1_hex(buffer + ref_length, actual_sha1)) {
568 free(buffer);
569 return NULL;
570 }
571 /* Now we have the ID of the referred-to object in
572 * actual_sha1. Check again. */
f4913f91 573 }
f4913f91
JH
574}
575
bf0f910d 576int write_sha1_file(void *buf, unsigned long len, const char *type, unsigned char *returnsha1)
0fcfd160
LT
577{
578 int size;
bf0f910d 579 unsigned char *compressed;
0fcfd160
LT
580 z_stream stream;
581 unsigned char sha1[20];
582 SHA_CTX c;
706bc531 583 char *filename;
aac17941 584 static char tmpfile[PATH_MAX];
bf0f910d 585 unsigned char hdr[50];
aac17941 586 int fd, hdrlen, ret;
a44c9a5e
LT
587
588 /* Generate the header */
bf0f910d 589 hdrlen = sprintf((char *)hdr, "%s %lu", type, len)+1;
0fcfd160 590
d98b46f8
LT
591 /* Sha1.. */
592 SHA1_Init(&c);
a44c9a5e 593 SHA1_Update(&c, hdr, hdrlen);
d98b46f8
LT
594 SHA1_Update(&c, buf, len);
595 SHA1_Final(sha1, &c);
596
706bc531
LT
597 if (returnsha1)
598 memcpy(returnsha1, sha1, 20);
599
600 filename = sha1_file_name(sha1);
aac17941
LT
601 fd = open(filename, O_RDONLY);
602 if (fd >= 0) {
706bc531 603 /*
aac17941
LT
604 * FIXME!!! We might do collision checking here, but we'd
605 * need to uncompress the old file and check it. Later.
706bc531 606 */
aac17941 607 close(fd);
706bc531
LT
608 return 0;
609 }
610
aac17941
LT
611 if (errno != ENOENT) {
612 fprintf(stderr, "sha1 file %s: %s", filename, strerror(errno));
613 return -1;
614 }
615
616 snprintf(tmpfile, sizeof(tmpfile), "%s/obj_XXXXXX", get_object_directory());
ace1534d 617
aac17941
LT
618 fd = mkstemp(tmpfile);
619 if (fd < 0) {
620 fprintf(stderr, "unable to create temporary sha1 filename %s: %s", tmpfile, strerror(errno));
621 return -1;
622 }
623
0fcfd160
LT
624 /* Set it up */
625 memset(&stream, 0, sizeof(stream));
626 deflateInit(&stream, Z_BEST_COMPRESSION);
a44c9a5e 627 size = deflateBound(&stream, len+hdrlen);
812666c8 628 compressed = xmalloc(size);
0fcfd160
LT
629
630 /* Compress it */
0fcfd160
LT
631 stream.next_out = compressed;
632 stream.avail_out = size;
a44c9a5e
LT
633
634 /* First header.. */
635 stream.next_in = hdr;
636 stream.avail_in = hdrlen;
637 while (deflate(&stream, 0) == Z_OK)
6ffcee88 638 /* nothing */;
a44c9a5e
LT
639
640 /* Then the data itself.. */
641 stream.next_in = buf;
642 stream.avail_in = len;
0fcfd160
LT
643 while (deflate(&stream, Z_FINISH) == Z_OK)
644 /* nothing */;
645 deflateEnd(&stream);
646 size = stream.total_out;
647
706bc531
LT
648 if (write(fd, compressed, size) != size)
649 die("unable to write file");
aac17941 650 fchmod(fd, 0444);
706bc531 651 close(fd);
383f85b7 652 free(compressed);
0fcfd160 653
aac17941 654 ret = link(tmpfile, filename);
a31c6d02 655 if (ret < 0) {
aac17941 656 ret = errno;
a31c6d02
LT
657
658 /*
659 * Coda hack - coda doesn't like cross-directory links,
660 * so we fall back to a rename, which will mean that it
661 * won't be able to check collisions, but that's not a
662 * big deal.
663 *
664 * When this succeeds, we just return 0. We have nothing
665 * left to unlink.
666 */
667 if (ret == EXDEV && !rename(tmpfile, filename))
668 return 0;
669 }
aac17941
LT
670 unlink(tmpfile);
671 if (ret) {
672 if (ret != EEXIST) {
673 fprintf(stderr, "unable to write sha1 filename %s: %s", filename, strerror(ret));
0fcfd160 674 return -1;
aac17941
LT
675 }
676 /* FIXME!!! Collision check here ? */
0fcfd160 677 }
aac17941 678
0fcfd160
LT
679 return 0;
680}
8237b185
DB
681
682int write_sha1_from_fd(const unsigned char *sha1, int fd)
683{
684 char *filename = sha1_file_name(sha1);
685
686 int local;
687 z_stream stream;
688 unsigned char real_sha1[20];
bf0f910d
BG
689 unsigned char buf[4096];
690 unsigned char discard[4096];
8237b185
DB
691 int ret;
692 SHA_CTX c;
693
694 local = open(filename, O_WRONLY | O_CREAT | O_EXCL, 0666);
695
696 if (local < 0)
697 return error("Couldn't open %s\n", filename);
698
699 memset(&stream, 0, sizeof(stream));
700
701 inflateInit(&stream);
702
703 SHA1_Init(&c);
704
705 do {
706 ssize_t size;
707 size = read(fd, buf, 4096);
708 if (size <= 0) {
709 close(local);
710 unlink(filename);
711 if (!size)
712 return error("Connection closed?");
713 perror("Reading from connection");
714 return -1;
715 }
716 write(local, buf, size);
717 stream.avail_in = size;
718 stream.next_in = buf;
719 do {
720 stream.next_out = discard;
721 stream.avail_out = sizeof(discard);
722 ret = inflate(&stream, Z_SYNC_FLUSH);
723 SHA1_Update(&c, discard, sizeof(discard) -
724 stream.avail_out);
725 } while (stream.avail_in && ret == Z_OK);
726
727 } while (ret == Z_OK);
728 inflateEnd(&stream);
729
730 close(local);
731 SHA1_Final(real_sha1, &c);
732 if (ret != Z_STREAM_END) {
733 unlink(filename);
734 return error("File %s corrupted", sha1_to_hex(sha1));
735 }
736 if (memcmp(sha1, real_sha1, 20)) {
737 unlink(filename);
738 return error("File %s has bad hash\n", sha1_to_hex(sha1));
739 }
740
741 return 0;
742}
743
744int has_sha1_file(const unsigned char *sha1)
745{
8237b185 746 struct stat st;
ace1534d 747 return !!find_sha1_file(sha1, &st);
8237b185 748}
74400e71
JH
749
750int index_fd(unsigned char *sha1, int fd, struct stat *st)
751{
74400e71 752 unsigned long size = st->st_size;
aac17941
LT
753 void *buf;
754 int ret;
74400e71 755
aac17941 756 buf = "";
74400e71 757 if (size)
aac17941 758 buf = mmap(NULL, size, PROT_READ, MAP_PRIVATE, fd, 0);
74400e71 759 close(fd);
aac17941 760 if ((int)(long)buf == -1)
74400e71
JH
761 return -1;
762
aac17941
LT
763 ret = write_sha1_file(buf, size, "blob", sha1);
764 if (size)
765 munmap(buf, size);
766 return ret;
74400e71 767}