]> git.ipfire.org Git - thirdparty/git.git/blame - sha1_file.c
Fix ?: statements.
[thirdparty/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 */
1f688557
JH
9#include <sys/types.h>
10#include <dirent.h>
0fcfd160 11#include "cache.h"
1f688557 12#include "delta.h"
a733cb60 13#include "pack.h"
0fcfd160 14
144bde78
LT
15#ifndef O_NOATIME
16#if defined(__linux__) && (defined(__i386__) || defined(__PPC__))
17#define O_NOATIME 01000000
18#else
19#define O_NOATIME 0
20#endif
21#endif
22
23static unsigned int sha1_file_open_flag = O_NOATIME;
24
0fcfd160
LT
25static unsigned hexval(char c)
26{
27 if (c >= '0' && c <= '9')
28 return c - '0';
29 if (c >= 'a' && c <= 'f')
30 return c - 'a' + 10;
31 if (c >= 'A' && c <= 'F')
32 return c - 'A' + 10;
33 return ~0;
34}
35
36int get_sha1_hex(const char *hex, unsigned char *sha1)
37{
38 int i;
39 for (i = 0; i < 20; i++) {
40 unsigned int val = (hexval(hex[0]) << 4) | hexval(hex[1]);
41 if (val & ~0xff)
42 return -1;
43 *sha1++ = val;
44 hex += 2;
45 }
46 return 0;
47}
48
5da5c8f4
JH
49static char *git_dir, *git_object_dir, *git_index_file, *git_refs_dir,
50 *git_graft_file;
8ac069ac
JH
51static void setup_git_env(void)
52{
53 git_dir = gitenv(GIT_DIR_ENVIRONMENT);
54 if (!git_dir)
55 git_dir = DEFAULT_GIT_DIR_ENVIRONMENT;
56 git_object_dir = gitenv(DB_ENVIRONMENT);
57 if (!git_object_dir) {
58 git_object_dir = xmalloc(strlen(git_dir) + 9);
59 sprintf(git_object_dir, "%s/objects", git_dir);
60 }
95fc7512
DB
61 git_refs_dir = xmalloc(strlen(git_dir) + 6);
62 sprintf(git_refs_dir, "%s/refs", git_dir);
8ac069ac
JH
63 git_index_file = gitenv(INDEX_ENVIRONMENT);
64 if (!git_index_file) {
65 git_index_file = xmalloc(strlen(git_dir) + 7);
66 sprintf(git_index_file, "%s/index", git_dir);
67 }
5da5c8f4
JH
68 git_graft_file = gitenv(GRAFT_ENVIRONMENT);
69 if (!git_graft_file)
70 git_graft_file = strdup(git_path("info/grafts"));
8ac069ac
JH
71}
72
73char *get_object_directory(void)
74{
75 if (!git_object_dir)
76 setup_git_env();
77 return git_object_dir;
78}
79
95fc7512
DB
80char *get_refs_directory(void)
81{
82 if (!git_refs_dir)
83 setup_git_env();
84 return git_refs_dir;
85}
86
8ac069ac
JH
87char *get_index_file(void)
88{
89 if (!git_index_file)
90 setup_git_env();
91 return git_index_file;
92}
93
5da5c8f4
JH
94char *get_graft_file(void)
95{
96 if (!git_graft_file)
97 setup_git_env();
98 return git_graft_file;
99}
100
b2cb9425
JH
101int safe_create_leading_directories(char *path)
102{
103 char *pos = path;
104
105 while (pos) {
106 pos = strchr(pos, '/');
107 if (!pos)
108 break;
109 *pos = 0;
110 if (mkdir(path, 0777) < 0)
111 if (errno != EEXIST) {
112 *pos = '/';
113 return -1;
114 }
115 *pos++ = '/';
116 }
117 return 0;
118}
723c31fe 119
0fcfd160
LT
120char * sha1_to_hex(const unsigned char *sha1)
121{
122 static char buffer[50];
123 static const char hex[] = "0123456789abcdef";
124 char *buf = buffer;
125 int i;
126
127 for (i = 0; i < 20; i++) {
128 unsigned int val = *sha1++;
129 *buf++ = hex[val >> 4];
130 *buf++ = hex[val & 0xf];
131 }
132 return buffer;
133}
134
ace1534d
JH
135static void fill_sha1_path(char *pathbuf, const unsigned char *sha1)
136{
137 int i;
138 for (i = 0; i < 20; i++) {
139 static char hex[] = "0123456789abcdef";
140 unsigned int val = sha1[i];
141 char *pos = pathbuf + i*2 + (i > 0);
142 *pos++ = hex[val >> 4];
143 *pos = hex[val & 0xf];
144 }
145}
146
0fcfd160
LT
147/*
148 * NOTE! This returns a statically allocated buffer, so you have to be
149 * careful about using it. Do a "strdup()" if you need to save the
150 * filename.
ace1534d
JH
151 *
152 * Also note that this returns the location for creating. Reading
153 * SHA1 file can happen from any alternate directory listed in the
d19938ab 154 * DB_ENVIRONMENT environment variable if it is not found in
ace1534d 155 * the primary object database.
0fcfd160
LT
156 */
157char *sha1_file_name(const unsigned char *sha1)
158{
0fcfd160
LT
159 static char *name, *base;
160
161 if (!base) {
d19938ab 162 const char *sha1_file_directory = get_object_directory();
0fcfd160 163 int len = strlen(sha1_file_directory);
812666c8 164 base = xmalloc(len + 60);
0fcfd160
LT
165 memcpy(base, sha1_file_directory, len);
166 memset(base+len, 0, 60);
167 base[len] = '/';
168 base[len+3] = '/';
169 name = base + len + 1;
170 }
ace1534d 171 fill_sha1_path(name, sha1);
0fcfd160
LT
172 return base;
173}
174
bf592c50
DB
175char *sha1_pack_name(const unsigned char *sha1)
176{
177 static const char hex[] = "0123456789abcdef";
178 static char *name, *base, *buf;
179 int i;
180
181 if (!base) {
182 const char *sha1_file_directory = get_object_directory();
183 int len = strlen(sha1_file_directory);
184 base = xmalloc(len + 60);
185 sprintf(base, "%s/pack/pack-1234567890123456789012345678901234567890.pack", sha1_file_directory);
186 name = base + len + 11;
187 }
188
189 buf = name;
190
191 for (i = 0; i < 20; i++) {
192 unsigned int val = *sha1++;
193 *buf++ = hex[val >> 4];
194 *buf++ = hex[val & 0xf];
195 }
196
197 return base;
198}
199
200char *sha1_pack_index_name(const unsigned char *sha1)
201{
202 static const char hex[] = "0123456789abcdef";
203 static char *name, *base, *buf;
204 int i;
205
206 if (!base) {
207 const char *sha1_file_directory = get_object_directory();
208 int len = strlen(sha1_file_directory);
209 base = xmalloc(len + 60);
210 sprintf(base, "%s/pack/pack-1234567890123456789012345678901234567890.idx", sha1_file_directory);
211 name = base + len + 11;
212 }
213
214 buf = name;
215
216 for (i = 0; i < 20; i++) {
217 unsigned int val = *sha1++;
218 *buf++ = hex[val >> 4];
219 *buf++ = hex[val & 0xf];
220 }
221
222 return base;
223}
224
d5a63b99
JH
225struct alternate_object_database *alt_odb_list;
226static struct alternate_object_database **alt_odb_tail;
ace1534d 227
ddd5d056
JH
228/*
229 * Prepare alternate object database registry.
d5a63b99
JH
230 *
231 * The variable alt_odb_list points at the list of struct
232 * alternate_object_database. The elements on this list come from
233 * non-empty elements from colon separated ALTERNATE_DB_ENVIRONMENT
234 * environment variable, and $GIT_OBJECT_DIRECTORY/info/alternates,
235 * whose contents is exactly in the same format as that environment
236 * variable. Its base points at a statically allocated buffer that
237 * contains "/the/directory/corresponding/to/.git/objects/...", while
238 * its name points just after the slash at the end of ".git/objects/"
239 * in the example above, and has enough space to hold 40-byte hex
240 * SHA1, an extra slash for the first level indirection, and the
241 * terminating NUL.
ddd5d056 242 */
9577e7e3 243static void link_alt_odb_entries(const char *alt, const char *ep, int sep)
ace1534d 244{
ace1534d 245 const char *cp, *last;
d5a63b99
JH
246 struct alternate_object_database *ent;
247
248 last = alt;
9577e7e3
JH
249 while (last < ep) {
250 cp = last;
251 if (cp < ep && *cp == '#') {
252 while (cp < ep && *cp != sep)
253 cp++;
254 last = cp + 1;
255 continue;
256 }
257 for ( ; cp < ep && *cp != sep; cp++)
d5a63b99
JH
258 ;
259 if (last != cp) {
260 /* 43 = 40-byte + 2 '/' + terminating NUL */
261 int pfxlen = cp - last;
262 int entlen = pfxlen + 43;
263
264 ent = xmalloc(sizeof(*ent) + entlen);
265 *alt_odb_tail = ent;
266 alt_odb_tail = &(ent->next);
267 ent->next = NULL;
268
269 memcpy(ent->base, last, pfxlen);
270 ent->name = ent->base + pfxlen + 1;
271 ent->base[pfxlen] = ent->base[pfxlen + 3] = '/';
272 ent->base[entlen-1] = 0;
273 }
9577e7e3 274 while (cp < ep && *cp == sep)
d5a63b99
JH
275 cp++;
276 last = cp;
9577e7e3 277 }
d5a63b99
JH
278}
279
280void prepare_alt_odb(void)
281{
282 char path[PATH_MAX];
9577e7e3 283 char *map;
d5a63b99
JH
284 int fd;
285 struct stat st;
c7c81b3a
JR
286 char *alt;
287
288 alt = gitenv(ALTERNATE_DB_ENVIRONMENT);
289 if (!alt) alt = "";
ace1534d 290
d5a63b99
JH
291 sprintf(path, "%s/info/alternates", get_object_directory());
292 if (alt_odb_tail)
293 return;
294 alt_odb_tail = &alt_odb_list;
9577e7e3 295 link_alt_odb_entries(alt, alt + strlen(alt), ':');
d5a63b99
JH
296
297 fd = open(path, O_RDONLY);
298 if (fd < 0)
299 return;
300 if (fstat(fd, &st) || (st.st_size == 0)) {
301 close(fd);
9a217f2a 302 return;
ace1534d 303 }
d5a63b99
JH
304 map = mmap(NULL, st.st_size, PROT_READ, MAP_PRIVATE, fd, 0);
305 close(fd);
306 if (map == MAP_FAILED)
307 return;
308
9577e7e3 309 link_alt_odb_entries(map, map + st.st_size, '\n');
d5a63b99 310 munmap(map, st.st_size);
ace1534d
JH
311}
312
313static char *find_sha1_file(const unsigned char *sha1, struct stat *st)
314{
ace1534d 315 char *name = sha1_file_name(sha1);
d5a63b99 316 struct alternate_object_database *alt;
ace1534d
JH
317
318 if (!stat(name, st))
319 return name;
9a217f2a 320 prepare_alt_odb();
d5a63b99
JH
321 for (alt = alt_odb_list; alt; alt = alt->next) {
322 name = alt->name;
ace1534d 323 fill_sha1_path(name, sha1);
d5a63b99
JH
324 if (!stat(alt->base, st))
325 return alt->base;
ace1534d
JH
326 }
327 return NULL;
328}
329
1f688557
JH
330#define PACK_MAX_SZ (1<<26)
331static int pack_used_ctr;
332static unsigned long pack_mapped;
9a217f2a 333struct packed_git *packed_git;
1f688557 334
1f688557
JH
335static int check_packed_git_idx(const char *path, unsigned long *idx_size_,
336 void **idx_map_)
337{
338 void *idx_map;
339 unsigned int *index;
340 unsigned long idx_size;
341 int nr, i;
342 int fd = open(path, O_RDONLY);
343 struct stat st;
344 if (fd < 0)
345 return -1;
346 if (fstat(fd, &st)) {
347 close(fd);
348 return -1;
349 }
350 idx_size = st.st_size;
351 idx_map = mmap(NULL, idx_size, PROT_READ, MAP_PRIVATE, fd, 0);
352 close(fd);
353 if (idx_map == MAP_FAILED)
354 return -1;
355
356 index = idx_map;
4d235c80
LT
357 *idx_map_ = idx_map;
358 *idx_size_ = idx_size;
1f688557
JH
359
360 /* check index map */
f9253394 361 if (idx_size < 4*256 + 20 + 20)
1f688557
JH
362 return error("index file too small");
363 nr = 0;
364 for (i = 0; i < 256; i++) {
365 unsigned int n = ntohl(index[i]);
366 if (n < nr)
367 return error("non-monotonic index");
368 nr = n;
369 }
370
371 /*
372 * Total size:
373 * - 256 index entries 4 bytes each
374 * - 24-byte entries * nr (20-byte sha1 + 4-byte offset)
375 * - 20-byte SHA1 of the packfile
376 * - 20-byte SHA1 file checksum
377 */
378 if (idx_size != 4*256 + nr * 24 + 20 + 20)
379 return error("wrong index file size");
380
1f688557
JH
381 return 0;
382}
383
f9253394 384static int unuse_one_packed_git(void)
1f688557 385{
f9253394
JH
386 struct packed_git *p, *lru = NULL;
387
388 for (p = packed_git; p; p = p->next) {
389 if (p->pack_use_cnt || !p->pack_base)
390 continue;
391 if (!lru || p->pack_last_used < lru->pack_last_used)
392 lru = p;
393 }
394 if (!lru)
395 return 0;
396 munmap(lru->pack_base, lru->pack_size);
397 lru->pack_base = NULL;
398 return 1;
399}
400
401void unuse_packed_git(struct packed_git *p)
402{
403 p->pack_use_cnt--;
1f688557
JH
404}
405
f9253394 406int use_packed_git(struct packed_git *p)
1f688557 407{
bf592c50
DB
408 if (!p->pack_size) {
409 struct stat st;
410 // We created the struct before we had the pack
411 stat(p->pack_name, &st);
412 if (!S_ISREG(st.st_mode))
413 die("packfile %s not a regular file", p->pack_name);
414 p->pack_size = st.st_size;
415 }
1f688557
JH
416 if (!p->pack_base) {
417 int fd;
418 struct stat st;
419 void *map;
420
421 pack_mapped += p->pack_size;
f9253394
JH
422 while (PACK_MAX_SZ < pack_mapped && unuse_one_packed_git())
423 ; /* nothing */
1f688557
JH
424 fd = open(p->pack_name, O_RDONLY);
425 if (fd < 0)
f9253394 426 die("packfile %s cannot be opened", p->pack_name);
1f688557
JH
427 if (fstat(fd, &st)) {
428 close(fd);
f9253394 429 die("packfile %s cannot be opened", p->pack_name);
1f688557
JH
430 }
431 if (st.st_size != p->pack_size)
f9253394 432 die("packfile %s size mismatch.", p->pack_name);
1f688557
JH
433 map = mmap(NULL, p->pack_size, PROT_READ, MAP_PRIVATE, fd, 0);
434 close(fd);
435 if (map == MAP_FAILED)
f9253394 436 die("packfile %s cannot be mapped.", p->pack_name);
1f688557 437 p->pack_base = map;
f9253394
JH
438
439 /* Check if the pack file matches with the index file.
440 * this is cheap.
441 */
442 if (memcmp((char*)(p->index_base) + p->index_size - 40,
bf592c50
DB
443 p->pack_base + p->pack_size - 20, 20)) {
444
f9253394 445 die("packfile %s does not match index.", p->pack_name);
bf592c50 446 }
1f688557
JH
447 }
448 p->pack_last_used = pack_used_ctr++;
f9253394 449 p->pack_use_cnt++;
1f688557
JH
450 return 0;
451}
452
f9253394 453struct packed_git *add_packed_git(char *path, int path_len)
1f688557
JH
454{
455 struct stat st;
456 struct packed_git *p;
457 unsigned long idx_size;
458 void *idx_map;
459
460 if (check_packed_git_idx(path, &idx_size, &idx_map))
461 return NULL;
462
463 /* do we have a corresponding .pack file? */
464 strcpy(path + path_len - 4, ".pack");
465 if (stat(path, &st) || !S_ISREG(st.st_mode)) {
466 munmap(idx_map, idx_size);
467 return NULL;
468 }
469 /* ok, it looks sane as far as we can check without
470 * actually mapping the pack file.
471 */
472 p = xmalloc(sizeof(*p) + path_len + 2);
473 strcpy(p->pack_name, path);
474 p->index_size = idx_size;
475 p->pack_size = st.st_size;
476 p->index_base = idx_map;
477 p->next = NULL;
d85a4fee 478 p->pack_base = NULL;
1f688557 479 p->pack_last_used = 0;
f9253394 480 p->pack_use_cnt = 0;
1f688557
JH
481 return p;
482}
483
bf592c50 484struct packed_git *parse_pack_index(unsigned char *sha1)
c508df5e
DB
485{
486 char *path = sha1_pack_index_name(sha1);
487 return parse_pack_index_file(sha1, path);
488}
489
490struct packed_git *parse_pack_index_file(unsigned char *sha1, char *idx_path)
bf592c50
DB
491{
492 struct packed_git *p;
493 unsigned long idx_size;
494 void *idx_map;
c508df5e 495 char *path;
bf592c50 496
c508df5e 497 if (check_packed_git_idx(idx_path, &idx_size, &idx_map))
bf592c50
DB
498 return NULL;
499
500 path = sha1_pack_name(sha1);
501
502 p = xmalloc(sizeof(*p) + strlen(path) + 2);
503 strcpy(p->pack_name, path);
504 p->index_size = idx_size;
505 p->pack_size = 0;
506 p->index_base = idx_map;
507 p->next = NULL;
508 p->pack_base = NULL;
509 p->pack_last_used = 0;
510 p->pack_use_cnt = 0;
511 memcpy(p->sha1, sha1, 20);
512 return p;
513}
514
515void install_packed_git(struct packed_git *pack)
516{
517 pack->next = packed_git;
518 packed_git = pack;
519}
520
1f688557
JH
521static void prepare_packed_git_one(char *objdir)
522{
523 char path[PATH_MAX];
524 int len;
525 DIR *dir;
526 struct dirent *de;
527
528 sprintf(path, "%s/pack", objdir);
529 len = strlen(path);
530 dir = opendir(path);
531 if (!dir)
532 return;
533 path[len++] = '/';
534 while ((de = readdir(dir)) != NULL) {
535 int namelen = strlen(de->d_name);
536 struct packed_git *p;
537
538 if (strcmp(de->d_name + namelen - 4, ".idx"))
539 continue;
540
541 /* we have .idx. Is it a file we can map? */
542 strcpy(path + len, de->d_name);
543 p = add_packed_git(path, len + namelen);
544 if (!p)
545 continue;
546 p->next = packed_git;
547 packed_git = p;
548 }
5b35bcd5 549 closedir(dir);
1f688557
JH
550}
551
9a217f2a 552void prepare_packed_git(void)
1f688557 553{
1f688557 554 static int run_once = 0;
d5a63b99 555 struct alternate_object_database *alt;
1f688557 556
d5a63b99 557 if (run_once)
1f688557 558 return;
1f688557 559 prepare_packed_git_one(get_object_directory());
9a217f2a 560 prepare_alt_odb();
d5a63b99
JH
561 for (alt = alt_odb_list; alt; alt = alt->next) {
562 alt->name[0] = 0;
563 prepare_packed_git_one(alt->base);
1f688557 564 }
d5a63b99 565 run_once = 1;
1f688557
JH
566}
567
5d6ccf5c 568int check_sha1_signature(const unsigned char *sha1, void *map, unsigned long size, const char *type)
0fcfd160 569{
d98b46f8 570 char header[100];
0fcfd160
LT
571 unsigned char real_sha1[20];
572 SHA_CTX c;
573
574 SHA1_Init(&c);
d98b46f8 575 SHA1_Update(&c, header, 1+sprintf(header, "%s %lu", type, size));
0fcfd160
LT
576 SHA1_Update(&c, map, size);
577 SHA1_Final(real_sha1, &c);
578 return memcmp(sha1, real_sha1, 20) ? -1 : 0;
579}
580
1f688557 581static void *map_sha1_file_internal(const unsigned char *sha1,
d5f1befc 582 unsigned long *size)
0fcfd160 583{
0fcfd160
LT
584 struct stat st;
585 void *map;
144bde78 586 int fd;
ace1534d
JH
587 char *filename = find_sha1_file(sha1, &st);
588
589 if (!filename) {
ace1534d
JH
590 return NULL;
591 }
0fcfd160 592
144bde78 593 fd = open(filename, O_RDONLY | sha1_file_open_flag);
0fcfd160 594 if (fd < 0) {
144bde78
LT
595 /* See if it works without O_NOATIME */
596 switch (sha1_file_open_flag) {
597 default:
598 fd = open(filename, O_RDONLY);
599 if (fd >= 0)
600 break;
601 /* Fallthrough */
602 case 0:
144bde78
LT
603 return NULL;
604 }
605
1f688557
JH
606 /* If it failed once, it will probably fail again.
607 * Stop using O_NOATIME
608 */
144bde78 609 sha1_file_open_flag = 0;
0fcfd160 610 }
0fcfd160
LT
611 map = mmap(NULL, st.st_size, PROT_READ, MAP_PRIVATE, fd, 0);
612 close(fd);
e35f9824 613 if (map == MAP_FAILED)
0fcfd160
LT
614 return NULL;
615 *size = st.st_size;
616 return map;
617}
618
c4483576
LT
619int unpack_sha1_header(z_stream *stream, void *map, unsigned long mapsize, void *buffer, unsigned long size)
620{
621 /* Get the data stream */
622 memset(stream, 0, sizeof(*stream));
623 stream->next_in = map;
624 stream->avail_in = mapsize;
625 stream->next_out = buffer;
626 stream->avail_out = size;
627
628 inflateInit(stream);
629 return inflate(stream, 0);
630}
631
6da4016a 632static void *unpack_sha1_rest(z_stream *stream, void *buffer, unsigned long size)
5180cacc
LT
633{
634 int bytes = strlen(buffer) + 1;
d565b341 635 unsigned char *buf = xmalloc(1+size);
5180cacc
LT
636
637 memcpy(buf, buffer + bytes, stream->total_out - bytes);
638 bytes = stream->total_out - bytes;
639 if (bytes < size) {
640 stream->next_out = buf + bytes;
641 stream->avail_out = size - bytes;
642 while (inflate(stream, Z_FINISH) == Z_OK)
643 /* nothing */;
644 }
645 buf[size] = 0;
646 inflateEnd(stream);
647 return buf;
648}
649
650/*
651 * We used to just use "sscanf()", but that's actually way
652 * too permissive for what we want to check. So do an anal
653 * object header parse by hand.
654 */
655int parse_sha1_header(char *hdr, char *type, unsigned long *sizep)
656{
657 int i;
658 unsigned long size;
659
660 /*
661 * The type can be at most ten bytes (including the
662 * terminating '\0' that we add), and is followed by
663 * a space.
664 */
665 i = 10;
666 for (;;) {
667 char c = *hdr++;
668 if (c == ' ')
669 break;
670 if (!--i)
671 return -1;
672 *type++ = c;
673 }
674 *type = 0;
675
676 /*
677 * The length must follow immediately, and be in canonical
678 * decimal format (ie "010" is not valid).
679 */
680 size = *hdr++ - '0';
681 if (size > 9)
682 return -1;
683 if (size) {
684 for (;;) {
685 unsigned long c = *hdr - '0';
686 if (c > 9)
687 break;
688 hdr++;
689 size = size * 10 + c;
690 }
691 }
692 *sizep = size;
693
694 /*
695 * The length must be followed by a zero byte
696 */
697 return *hdr ? -1 : 0;
698}
699
0fcfd160
LT
700void * unpack_sha1_file(void *map, unsigned long mapsize, char *type, unsigned long *size)
701{
5180cacc 702 int ret;
0fcfd160 703 z_stream stream;
5180cacc 704 char hdr[8192];
0fcfd160 705
5180cacc
LT
706 ret = unpack_sha1_header(&stream, map, mapsize, hdr, sizeof(hdr));
707 if (ret < Z_OK || parse_sha1_header(hdr, type, size) < 0)
0fcfd160
LT
708 return NULL;
709
5180cacc 710 return unpack_sha1_rest(&stream, hdr, *size);
0fcfd160
LT
711}
712
f3bf9224
JH
713/* forward declaration for a mutually recursive function */
714static int packed_object_info(struct pack_entry *entry,
715 char *type, unsigned long *sizep);
716
5db47c2b
JH
717static int packed_delta_info(unsigned char *base_sha1,
718 unsigned long delta_size,
719 unsigned long left,
720 char *type,
f3bf9224
JH
721 unsigned long *sizep,
722 struct packed_git *p)
5db47c2b 723{
f3bf9224
JH
724 struct pack_entry base_ent;
725
5db47c2b
JH
726 if (left < 20)
727 die("truncated pack file");
5db47c2b 728
f3bf9224
JH
729 /* The base entry _must_ be in the same pack */
730 if (!find_pack_entry_one(base_sha1, &base_ent, p))
731 die("failed to find delta-pack base object %s",
732 sha1_to_hex(base_sha1));
733
c62266f3
JH
734 /* We choose to only get the type of the base object and
735 * ignore potentially corrupt pack file that expects the delta
736 * based on a base with a wrong size. This saves tons of
737 * inflate() calls.
738 */
5db47c2b 739
f3bf9224 740 if (packed_object_info(&base_ent, type, NULL))
c62266f3 741 die("cannot get info for delta-pack base");
5db47c2b 742
c62266f3
JH
743 if (sizep) {
744 const unsigned char *data;
745 unsigned char delta_head[64];
746 unsigned long result_size;
747 z_stream stream;
748 int st;
749
750 memset(&stream, 0, sizeof(stream));
751
752 data = stream.next_in = base_sha1 + 20;
753 stream.avail_in = left - 20;
754 stream.next_out = delta_head;
755 stream.avail_out = sizeof(delta_head);
756
757 inflateInit(&stream);
758 st = inflate(&stream, Z_FINISH);
759 inflateEnd(&stream);
760 if ((st != Z_STREAM_END) &&
761 stream.total_out != sizeof(delta_head))
762 die("delta data unpack-initial failed");
763
764 /* Examine the initial part of the delta to figure out
765 * the result size.
766 */
767 data = delta_head;
768 get_delta_hdr_size(&data); /* ignore base size */
5db47c2b 769
c62266f3
JH
770 /* Read the result size */
771 result_size = get_delta_hdr_size(&data);
772 *sizep = result_size;
773 }
5db47c2b
JH
774 return 0;
775}
776
a733cb60
LT
777static unsigned long unpack_object_header(struct packed_git *p, unsigned long offset,
778 enum object_type *type, unsigned long *sizep)
779{
01247d87 780 unsigned shift;
a733cb60
LT
781 unsigned char *pack, c;
782 unsigned long size;
783
784 if (offset >= p->pack_size)
785 die("object offset outside of pack file");
786
787 pack = p->pack_base + offset;
788 c = *pack++;
789 offset++;
790 *type = (c >> 4) & 7;
791 size = c & 15;
01247d87 792 shift = 4;
a733cb60
LT
793 while (c & 0x80) {
794 if (offset >= p->pack_size)
795 die("object offset outside of pack file");
796 c = *pack++;
797 offset++;
01247d87
LT
798 size += (c & 0x7f) << shift;
799 shift += 7;
a733cb60
LT
800 }
801 *sizep = size;
802 return offset;
803}
804
ad8c80a5
JH
805void packed_object_info_detail(struct pack_entry *e,
806 char *type,
807 unsigned long *size,
808 unsigned long *store_size,
809 int *delta_chain_length,
810 unsigned char *base_sha1)
811{
812 struct packed_git *p = e->p;
813 unsigned long offset, left;
814 unsigned char *pack;
815 enum object_type kind;
816
817 offset = unpack_object_header(p, e->offset, &kind, size);
818 pack = p->pack_base + offset;
819 left = p->pack_size - offset;
820 if (kind != OBJ_DELTA)
821 *delta_chain_length = 0;
822 else {
823 int chain_length = 0;
824 memcpy(base_sha1, pack, 20);
825 do {
826 struct pack_entry base_ent;
827 unsigned long junk;
828
829 find_pack_entry_one(pack, &base_ent, p);
830 offset = unpack_object_header(p, base_ent.offset,
831 &kind, &junk);
832 pack = p->pack_base + offset;
833 chain_length++;
834 } while (kind == OBJ_DELTA);
835 *delta_chain_length = chain_length;
836 }
837 switch (kind) {
838 case OBJ_COMMIT:
839 strcpy(type, "commit");
840 break;
841 case OBJ_TREE:
842 strcpy(type, "tree");
843 break;
844 case OBJ_BLOB:
845 strcpy(type, "blob");
846 break;
847 case OBJ_TAG:
848 strcpy(type, "tag");
849 break;
850 default:
851 die("corrupted pack file");
852 }
853 *store_size = 0; /* notyet */
854}
855
1f688557
JH
856static int packed_object_info(struct pack_entry *entry,
857 char *type, unsigned long *sizep)
858{
859 struct packed_git *p = entry->p;
860 unsigned long offset, size, left;
861 unsigned char *pack;
a733cb60 862 enum object_type kind;
f9253394 863 int retval;
5db47c2b
JH
864
865 if (use_packed_git(p))
866 die("cannot map packed file");
867
a733cb60 868 offset = unpack_object_header(p, entry->offset, &kind, &size);
1f688557 869 pack = p->pack_base + offset;
a733cb60
LT
870 left = p->pack_size - offset;
871
872 switch (kind) {
873 case OBJ_DELTA:
f3bf9224 874 retval = packed_delta_info(pack, size, left, type, sizep, p);
f9253394
JH
875 unuse_packed_git(p);
876 return retval;
a733cb60 877 case OBJ_COMMIT:
1f688557
JH
878 strcpy(type, "commit");
879 break;
a733cb60 880 case OBJ_TREE:
1f688557
JH
881 strcpy(type, "tree");
882 break;
a733cb60 883 case OBJ_BLOB:
1f688557
JH
884 strcpy(type, "blob");
885 break;
a733cb60 886 case OBJ_TAG:
a69d0943
LT
887 strcpy(type, "tag");
888 break;
1f688557
JH
889 default:
890 die("corrupted pack file");
891 }
c62266f3
JH
892 if (sizep)
893 *sizep = size;
f9253394 894 unuse_packed_git(p);
1f688557
JH
895 return 0;
896}
897
898/* forward declaration for a mutually recursive function */
899static void *unpack_entry(struct pack_entry *, char *, unsigned long *);
900
901static void *unpack_delta_entry(unsigned char *base_sha1,
902 unsigned long delta_size,
903 unsigned long left,
904 char *type,
f3bf9224
JH
905 unsigned long *sizep,
906 struct packed_git *p)
1f688557 907{
f3bf9224 908 struct pack_entry base_ent;
1f688557
JH
909 void *data, *delta_data, *result, *base;
910 unsigned long data_size, result_size, base_size;
911 z_stream stream;
912 int st;
913
914 if (left < 20)
915 die("truncated pack file");
916 data = base_sha1 + 20;
917 data_size = left - 20;
918 delta_data = xmalloc(delta_size);
919
920 memset(&stream, 0, sizeof(stream));
921
922 stream.next_in = data;
923 stream.avail_in = data_size;
924 stream.next_out = delta_data;
925 stream.avail_out = delta_size;
926
927 inflateInit(&stream);
928 st = inflate(&stream, Z_FINISH);
929 inflateEnd(&stream);
930 if ((st != Z_STREAM_END) || stream.total_out != delta_size)
931 die("delta data unpack failed");
932
f3bf9224
JH
933 /* The base entry _must_ be in the same pack */
934 if (!find_pack_entry_one(base_sha1, &base_ent, p))
935 die("failed to find delta-pack base object %s",
936 sha1_to_hex(base_sha1));
937 base = unpack_entry_gently(&base_ent, type, &base_size);
1f688557
JH
938 if (!base)
939 die("failed to read delta-pack base object %s",
940 sha1_to_hex(base_sha1));
941 result = patch_delta(base, base_size,
942 delta_data, delta_size,
943 &result_size);
944 if (!result)
945 die("failed to apply delta");
946 free(delta_data);
947 free(base);
948 *sizep = result_size;
949 return result;
950}
951
952static void *unpack_non_delta_entry(unsigned char *data,
953 unsigned long size,
954 unsigned long left)
955{
956 int st;
957 z_stream stream;
4d235c80 958 unsigned char *buffer;
1f688557
JH
959
960 buffer = xmalloc(size + 1);
961 buffer[size] = 0;
962 memset(&stream, 0, sizeof(stream));
963 stream.next_in = data;
964 stream.avail_in = left;
965 stream.next_out = buffer;
966 stream.avail_out = size;
967
968 inflateInit(&stream);
969 st = inflate(&stream, Z_FINISH);
970 inflateEnd(&stream);
971 if ((st != Z_STREAM_END) || stream.total_out != size) {
972 free(buffer);
973 return NULL;
974 }
975
976 return buffer;
977}
978
979static void *unpack_entry(struct pack_entry *entry,
980 char *type, unsigned long *sizep)
981{
982 struct packed_git *p = entry->p;
f9253394 983 void *retval;
1f688557
JH
984
985 if (use_packed_git(p))
986 die("cannot map packed file");
f3bf9224
JH
987 retval = unpack_entry_gently(entry, type, sizep);
988 unuse_packed_git(p);
989 if (!retval)
990 die("corrupted pack file");
991 return retval;
992}
993
994/* The caller is responsible for use_packed_git()/unuse_packed_git() pair */
995void *unpack_entry_gently(struct pack_entry *entry,
996 char *type, unsigned long *sizep)
997{
998 struct packed_git *p = entry->p;
999 unsigned long offset, size, left;
1000 unsigned char *pack;
1001 enum object_type kind;
1002 void *retval;
1f688557 1003
a733cb60 1004 offset = unpack_object_header(p, entry->offset, &kind, &size);
1f688557 1005 pack = p->pack_base + offset;
a733cb60
LT
1006 left = p->pack_size - offset;
1007 switch (kind) {
1008 case OBJ_DELTA:
f3bf9224 1009 retval = unpack_delta_entry(pack, size, left, type, sizep, p);
f9253394 1010 return retval;
a733cb60 1011 case OBJ_COMMIT:
1f688557
JH
1012 strcpy(type, "commit");
1013 break;
a733cb60 1014 case OBJ_TREE:
1f688557
JH
1015 strcpy(type, "tree");
1016 break;
a733cb60 1017 case OBJ_BLOB:
1f688557
JH
1018 strcpy(type, "blob");
1019 break;
a733cb60 1020 case OBJ_TAG:
a69d0943
LT
1021 strcpy(type, "tag");
1022 break;
1f688557 1023 default:
f3bf9224 1024 return NULL;
1f688557
JH
1025 }
1026 *sizep = size;
f9253394 1027 retval = unpack_non_delta_entry(pack, size, left);
f9253394 1028 return retval;
1f688557
JH
1029}
1030
9a217f2a
JH
1031int num_packed_objects(const struct packed_git *p)
1032{
f9253394 1033 /* See check_packed_git_idx() */
9a217f2a
JH
1034 return (p->index_size - 20 - 20 - 4*256) / 24;
1035}
1036
1037int nth_packed_object_sha1(const struct packed_git *p, int n,
1038 unsigned char* sha1)
1039{
1040 void *index = p->index_base + 256;
1041 if (n < 0 || num_packed_objects(p) <= n)
1042 return -1;
1043 memcpy(sha1, (index + 24 * n + 4), 20);
1044 return 0;
1045}
1046
f3bf9224
JH
1047int find_pack_entry_one(const unsigned char *sha1,
1048 struct pack_entry *e, struct packed_git *p)
1f688557 1049{
4d235c80 1050 unsigned int *level1_ofs = p->index_base;
1f688557
JH
1051 int hi = ntohl(level1_ofs[*sha1]);
1052 int lo = ((*sha1 == 0x0) ? 0 : ntohl(level1_ofs[*sha1 - 1]));
1053 void *index = p->index_base + 256;
1054
1055 do {
1056 int mi = (lo + hi) / 2;
1057 int cmp = memcmp(index + 24 * mi + 4, sha1, 20);
1058 if (!cmp) {
1059 e->offset = ntohl(*((int*)(index + 24 * mi)));
1060 memcpy(e->sha1, sha1, 20);
1061 e->p = p;
1062 return 1;
1063 }
1064 if (cmp > 0)
1065 hi = mi;
1066 else
1067 lo = mi+1;
1068 } while (lo < hi);
1069 return 0;
1070}
1071
1072static int find_pack_entry(const unsigned char *sha1, struct pack_entry *e)
1073{
1074 struct packed_git *p;
1075 prepare_packed_git();
1076
1077 for (p = packed_git; p; p = p->next) {
f3bf9224 1078 if (find_pack_entry_one(sha1, e, p))
1f688557
JH
1079 return 1;
1080 }
1081 return 0;
1082}
1083
bf592c50
DB
1084struct packed_git *find_sha1_pack(const unsigned char *sha1,
1085 struct packed_git *packs)
1086{
1087 struct packed_git *p;
1088 struct pack_entry e;
1089
1090 for (p = packs; p; p = p->next) {
1091 if (find_pack_entry_one(sha1, &e, p))
1092 return p;
1093 }
1094 return NULL;
1095
1096}
1097
36e4d74a 1098int sha1_object_info(const unsigned char *sha1, char *type, unsigned long *sizep)
65c2e0c3 1099{
36e4d74a 1100 int status;
65c2e0c3
JH
1101 unsigned long mapsize, size;
1102 void *map;
1103 z_stream stream;
36e4d74a 1104 char hdr[128];
65c2e0c3 1105
d5f1befc 1106 map = map_sha1_file_internal(sha1, &mapsize);
1f688557
JH
1107 if (!map) {
1108 struct pack_entry e;
1109
1110 if (!find_pack_entry(sha1, &e))
1111 return error("unable to find %s", sha1_to_hex(sha1));
c62266f3 1112 return packed_object_info(&e, type, sizep);
1f688557 1113 }
36e4d74a
JH
1114 if (unpack_sha1_header(&stream, map, mapsize, hdr, sizeof(hdr)) < 0)
1115 status = error("unable to unpack %s header",
1116 sha1_to_hex(sha1));
1117 if (parse_sha1_header(hdr, type, &size) < 0)
1118 status = error("unable to parse %s header", sha1_to_hex(sha1));
c4584ae3 1119 else {
65c2e0c3 1120 status = 0;
c62266f3
JH
1121 if (sizep)
1122 *sizep = size;
65c2e0c3 1123 }
65c2e0c3
JH
1124 inflateEnd(&stream);
1125 munmap(map, mapsize);
1126 return status;
1127}
1128
1f688557
JH
1129static void *read_packed_sha1(const unsigned char *sha1, char *type, unsigned long *size)
1130{
1131 struct pack_entry e;
1132
1133 if (!find_pack_entry(sha1, &e)) {
1134 error("cannot read sha1_file for %s", sha1_to_hex(sha1));
1135 return NULL;
1136 }
1137 return unpack_entry(&e, type, size);
1138}
1139
0fcfd160
LT
1140void * read_sha1_file(const unsigned char *sha1, char *type, unsigned long *size)
1141{
1142 unsigned long mapsize;
1143 void *map, *buf;
ab90ea5d 1144 struct pack_entry e;
0fcfd160 1145
ab90ea5d
JH
1146 if (find_pack_entry(sha1, &e))
1147 return read_packed_sha1(sha1, type, size);
d5f1befc 1148 map = map_sha1_file_internal(sha1, &mapsize);
0fcfd160
LT
1149 if (map) {
1150 buf = unpack_sha1_file(map, mapsize, type, size);
1151 munmap(map, mapsize);
1152 return buf;
1153 }
ab90ea5d 1154 return NULL;
0fcfd160
LT
1155}
1156
40469ee9 1157void *read_object_with_reference(const unsigned char *sha1,
bf0f910d 1158 const char *required_type,
40469ee9
JH
1159 unsigned long *size,
1160 unsigned char *actual_sha1_return)
f4913f91
JH
1161{
1162 char type[20];
1163 void *buffer;
1164 unsigned long isize;
40469ee9 1165 unsigned char actual_sha1[20];
f4913f91 1166
40469ee9
JH
1167 memcpy(actual_sha1, sha1, 20);
1168 while (1) {
1169 int ref_length = -1;
1170 const char *ref_type = NULL;
f4913f91 1171
40469ee9
JH
1172 buffer = read_sha1_file(actual_sha1, type, &isize);
1173 if (!buffer)
1174 return NULL;
1175 if (!strcmp(type, required_type)) {
1176 *size = isize;
1177 if (actual_sha1_return)
1178 memcpy(actual_sha1_return, actual_sha1, 20);
1179 return buffer;
1180 }
1181 /* Handle references */
1182 else if (!strcmp(type, "commit"))
1183 ref_type = "tree ";
1184 else if (!strcmp(type, "tag"))
1185 ref_type = "object ";
1186 else {
1187 free(buffer);
1188 return NULL;
1189 }
1190 ref_length = strlen(ref_type);
f4913f91 1191
40469ee9
JH
1192 if (memcmp(buffer, ref_type, ref_length) ||
1193 get_sha1_hex(buffer + ref_length, actual_sha1)) {
1194 free(buffer);
1195 return NULL;
1196 }
1cf58e72 1197 free(buffer);
40469ee9
JH
1198 /* Now we have the ID of the referred-to object in
1199 * actual_sha1. Check again. */
f4913f91 1200 }
f4913f91
JH
1201}
1202
7672db20
BL
1203char *write_sha1_file_prepare(void *buf,
1204 unsigned long len,
1205 const char *type,
1206 unsigned char *sha1,
1207 unsigned char *hdr,
1208 int *hdrlen)
d410c0f5
JH
1209{
1210 SHA_CTX c;
1211
1212 /* Generate the header */
1213 *hdrlen = sprintf((char *)hdr, "%s %lu", type, len)+1;
1214
1215 /* Sha1.. */
1216 SHA1_Init(&c);
1217 SHA1_Update(&c, hdr, *hdrlen);
1218 SHA1_Update(&c, buf, len);
1219 SHA1_Final(sha1, &c);
1220
1221 return sha1_file_name(sha1);
1222}
1223
bf0f910d 1224int write_sha1_file(void *buf, unsigned long len, const char *type, unsigned char *returnsha1)
0fcfd160
LT
1225{
1226 int size;
bf0f910d 1227 unsigned char *compressed;
0fcfd160
LT
1228 z_stream stream;
1229 unsigned char sha1[20];
706bc531 1230 char *filename;
aac17941 1231 static char tmpfile[PATH_MAX];
bf0f910d 1232 unsigned char hdr[50];
aac17941 1233 int fd, hdrlen, ret;
a44c9a5e 1234
d410c0f5
JH
1235 /* Normally if we have it in the pack then we do not bother writing
1236 * it out into .git/objects/??/?{38} file.
1237 */
1238 filename = write_sha1_file_prepare(buf, len, type, sha1, hdr, &hdrlen);
706bc531
LT
1239 if (returnsha1)
1240 memcpy(returnsha1, sha1, 20);
d410c0f5
JH
1241 if (has_sha1_file(sha1))
1242 return 0;
aac17941
LT
1243 fd = open(filename, O_RDONLY);
1244 if (fd >= 0) {
706bc531 1245 /*
aac17941
LT
1246 * FIXME!!! We might do collision checking here, but we'd
1247 * need to uncompress the old file and check it. Later.
706bc531 1248 */
aac17941 1249 close(fd);
706bc531
LT
1250 return 0;
1251 }
1252
aac17941
LT
1253 if (errno != ENOENT) {
1254 fprintf(stderr, "sha1 file %s: %s", filename, strerror(errno));
1255 return -1;
1256 }
1257
1258 snprintf(tmpfile, sizeof(tmpfile), "%s/obj_XXXXXX", get_object_directory());
ace1534d 1259
aac17941
LT
1260 fd = mkstemp(tmpfile);
1261 if (fd < 0) {
1262 fprintf(stderr, "unable to create temporary sha1 filename %s: %s", tmpfile, strerror(errno));
1263 return -1;
1264 }
1265
0fcfd160
LT
1266 /* Set it up */
1267 memset(&stream, 0, sizeof(stream));
1268 deflateInit(&stream, Z_BEST_COMPRESSION);
a44c9a5e 1269 size = deflateBound(&stream, len+hdrlen);
812666c8 1270 compressed = xmalloc(size);
0fcfd160
LT
1271
1272 /* Compress it */
0fcfd160
LT
1273 stream.next_out = compressed;
1274 stream.avail_out = size;
a44c9a5e
LT
1275
1276 /* First header.. */
1277 stream.next_in = hdr;
1278 stream.avail_in = hdrlen;
1279 while (deflate(&stream, 0) == Z_OK)
6ffcee88 1280 /* nothing */;
a44c9a5e
LT
1281
1282 /* Then the data itself.. */
1283 stream.next_in = buf;
1284 stream.avail_in = len;
0fcfd160
LT
1285 while (deflate(&stream, Z_FINISH) == Z_OK)
1286 /* nothing */;
1287 deflateEnd(&stream);
1288 size = stream.total_out;
1289
706bc531
LT
1290 if (write(fd, compressed, size) != size)
1291 die("unable to write file");
aac17941 1292 fchmod(fd, 0444);
706bc531 1293 close(fd);
383f85b7 1294 free(compressed);
0fcfd160 1295
aac17941 1296 ret = link(tmpfile, filename);
a31c6d02 1297 if (ret < 0) {
aac17941 1298 ret = errno;
a31c6d02
LT
1299
1300 /*
1301 * Coda hack - coda doesn't like cross-directory links,
1302 * so we fall back to a rename, which will mean that it
1303 * won't be able to check collisions, but that's not a
1304 * big deal.
1305 *
1306 * When this succeeds, we just return 0. We have nothing
1307 * left to unlink.
1308 */
1309 if (ret == EXDEV && !rename(tmpfile, filename))
1310 return 0;
1311 }
aac17941
LT
1312 unlink(tmpfile);
1313 if (ret) {
1314 if (ret != EEXIST) {
1315 fprintf(stderr, "unable to write sha1 filename %s: %s", filename, strerror(ret));
0fcfd160 1316 return -1;
aac17941
LT
1317 }
1318 /* FIXME!!! Collision check here ? */
0fcfd160 1319 }
aac17941 1320
0fcfd160
LT
1321 return 0;
1322}
8237b185 1323
a5eda52b
DB
1324int write_sha1_to_fd(int fd, const unsigned char *sha1)
1325{
1326 ssize_t size;
1327 unsigned long objsize;
1328 int posn = 0;
bfc66daf
SV
1329 void *map = map_sha1_file_internal(sha1, &objsize);
1330 void *buf = map;
1331 void *temp_obj = NULL;
a5eda52b 1332 z_stream stream;
bfc66daf 1333
a5eda52b
DB
1334 if (!buf) {
1335 unsigned char *unpacked;
1336 unsigned long len;
1337 char type[20];
1338 char hdr[50];
1339 int hdrlen;
1340 // need to unpack and recompress it by itself
1341 unpacked = read_packed_sha1(sha1, type, &len);
1342
1343 hdrlen = sprintf(hdr, "%s %lu", type, len) + 1;
1344
1345 /* Set it up */
1346 memset(&stream, 0, sizeof(stream));
1347 deflateInit(&stream, Z_BEST_COMPRESSION);
1348 size = deflateBound(&stream, len + hdrlen);
bfc66daf 1349 temp_obj = buf = xmalloc(size);
a5eda52b
DB
1350
1351 /* Compress it */
1352 stream.next_out = buf;
1353 stream.avail_out = size;
1354
1355 /* First header.. */
0ee19dce 1356 stream.next_in = (void *)hdr;
a5eda52b
DB
1357 stream.avail_in = hdrlen;
1358 while (deflate(&stream, 0) == Z_OK)
1359 /* nothing */;
1360
1361 /* Then the data itself.. */
1362 stream.next_in = unpacked;
1363 stream.avail_in = len;
1364 while (deflate(&stream, Z_FINISH) == Z_OK)
1365 /* nothing */;
1366 deflateEnd(&stream);
bfc66daf 1367 free(unpacked);
a5eda52b
DB
1368
1369 objsize = stream.total_out;
1370 }
1371
1372 do {
1373 size = write(fd, buf + posn, objsize - posn);
1374 if (size <= 0) {
1375 if (!size) {
1376 fprintf(stderr, "write closed");
1377 } else {
1378 perror("write ");
1379 }
1380 return -1;
1381 }
1382 posn += size;
1383 } while (posn < objsize);
bfc66daf
SV
1384
1385 if (map)
1386 munmap(map, objsize);
1387 if (temp_obj)
1388 free(temp_obj);
1389
a5eda52b
DB
1390 return 0;
1391}
1392
70b9829e
DB
1393int write_sha1_from_fd(const unsigned char *sha1, int fd, char *buffer,
1394 size_t bufsize, size_t *bufposn)
8237b185
DB
1395{
1396 char *filename = sha1_file_name(sha1);
1397
1398 int local;
1399 z_stream stream;
1400 unsigned char real_sha1[20];
bf0f910d 1401 unsigned char discard[4096];
8237b185
DB
1402 int ret;
1403 SHA_CTX c;
1404
1405 local = open(filename, O_WRONLY | O_CREAT | O_EXCL, 0666);
1406
1407 if (local < 0)
1408 return error("Couldn't open %s\n", filename);
1409
1410 memset(&stream, 0, sizeof(stream));
1411
1412 inflateInit(&stream);
1413
1414 SHA1_Init(&c);
1415
1416 do {
1417 ssize_t size;
70b9829e
DB
1418 if (*bufposn) {
1419 stream.avail_in = *bufposn;
96ad15ae 1420 stream.next_in = (unsigned char *) buffer;
70b9829e
DB
1421 do {
1422 stream.next_out = discard;
1423 stream.avail_out = sizeof(discard);
1424 ret = inflate(&stream, Z_SYNC_FLUSH);
1425 SHA1_Update(&c, discard, sizeof(discard) -
1426 stream.avail_out);
1427 } while (stream.avail_in && ret == Z_OK);
1428 write(local, buffer, *bufposn - stream.avail_in);
1429 memmove(buffer, buffer + *bufposn - stream.avail_in,
1430 stream.avail_in);
1431 *bufposn = stream.avail_in;
1432 if (ret != Z_OK)
1433 break;
1434 }
1435 size = read(fd, buffer + *bufposn, bufsize - *bufposn);
8237b185
DB
1436 if (size <= 0) {
1437 close(local);
1438 unlink(filename);
1439 if (!size)
1440 return error("Connection closed?");
1441 perror("Reading from connection");
1442 return -1;
1443 }
70b9829e
DB
1444 *bufposn += size;
1445 } while (1);
8237b185
DB
1446 inflateEnd(&stream);
1447
1448 close(local);
1449 SHA1_Final(real_sha1, &c);
1450 if (ret != Z_STREAM_END) {
1451 unlink(filename);
1452 return error("File %s corrupted", sha1_to_hex(sha1));
1453 }
1454 if (memcmp(sha1, real_sha1, 20)) {
1455 unlink(filename);
1456 return error("File %s has bad hash\n", sha1_to_hex(sha1));
1457 }
1458
1459 return 0;
1460}
1461
bf592c50
DB
1462int has_pack_index(const unsigned char *sha1)
1463{
1464 struct stat st;
1465 if (stat(sha1_pack_index_name(sha1), &st))
1466 return 0;
1467 return 1;
1468}
1469
1470int has_pack_file(const unsigned char *sha1)
1471{
1472 struct stat st;
1473 if (stat(sha1_pack_name(sha1), &st))
1474 return 0;
1475 return 1;
1476}
1477
dade09c2
LT
1478int has_sha1_pack(const unsigned char *sha1)
1479{
1480 struct pack_entry e;
1481 return find_pack_entry(sha1, &e);
1482}
1483
8237b185
DB
1484int has_sha1_file(const unsigned char *sha1)
1485{
8237b185 1486 struct stat st;
1f688557
JH
1487 struct pack_entry e;
1488
ab90ea5d 1489 if (find_pack_entry(sha1, &e))
1f688557 1490 return 1;
ab90ea5d 1491 return find_sha1_file(sha1, &st) ? 1 : 0;
8237b185 1492}
74400e71 1493
7672db20 1494int index_fd(unsigned char *sha1, int fd, struct stat *st, int write_object, const char *type)
74400e71 1495{
74400e71 1496 unsigned long size = st->st_size;
aac17941
LT
1497 void *buf;
1498 int ret;
7672db20
BL
1499 unsigned char hdr[50];
1500 int hdrlen;
74400e71 1501
aac17941 1502 buf = "";
74400e71 1503 if (size)
aac17941 1504 buf = mmap(NULL, size, PROT_READ, MAP_PRIVATE, fd, 0);
74400e71 1505 close(fd);
e35f9824 1506 if (buf == MAP_FAILED)
74400e71
JH
1507 return -1;
1508
7672db20
BL
1509 if (!type)
1510 type = "blob";
1511 if (write_object)
1512 ret = write_sha1_file(buf, size, type, sha1);
1513 else {
1514 write_sha1_file_prepare(buf, size, type, sha1, hdr, &hdrlen);
1515 ret = 0;
1516 }
aac17941
LT
1517 if (size)
1518 munmap(buf, size);
1519 return ret;
74400e71 1520}