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