1/*
2 * GIT - The information manager from hell
3 *
4 * Copyright (C) Linus Torvalds, 2005
5 *
6 * This handles basic git sha1 object files - packing, unpacking,
7 * creation etc.
8 */
9#include "cache.h"
10#include "delta.h"
11#include "pack.h"
12#include "blob.h"
13#include "commit.h"
14#include "tag.h"
15#include "tree.h"
16
17#ifndef O_NOATIME
18#if defined(__linux__) && (defined(__i386__) || defined(__PPC__))
19#define O_NOATIME 01000000
20#else
21#define O_NOATIME 0
22#endif
23#endif
24
25const unsigned char null_sha1[20];
26
27static unsigned int sha1_file_open_flag = O_NOATIME;
28
29static inline unsigned int hexval(unsigned int c)
30{
31 static signed char val[256] = {
32 -1, -1, -1, -1, -1, -1, -1, -1, /* 00-07 */
33 -1, -1, -1, -1, -1, -1, -1, -1, /* 08-0f */
34 -1, -1, -1, -1, -1, -1, -1, -1, /* 10-17 */
35 -1, -1, -1, -1, -1, -1, -1, -1, /* 18-1f */
36 -1, -1, -1, -1, -1, -1, -1, -1, /* 20-27 */
37 -1, -1, -1, -1, -1, -1, -1, -1, /* 28-2f */
38 0, 1, 2, 3, 4, 5, 6, 7, /* 30-37 */
39 8, 9, -1, -1, -1, -1, -1, -1, /* 38-3f */
40 -1, 10, 11, 12, 13, 14, 15, -1, /* 40-47 */
41 -1, -1, -1, -1, -1, -1, -1, -1, /* 48-4f */
42 -1, -1, -1, -1, -1, -1, -1, -1, /* 50-57 */
43 -1, -1, -1, -1, -1, -1, -1, -1, /* 58-5f */
44 -1, 10, 11, 12, 13, 14, 15, -1, /* 60-67 */
45 -1, -1, -1, -1, -1, -1, -1, -1, /* 68-67 */
46 -1, -1, -1, -1, -1, -1, -1, -1, /* 70-77 */
47 -1, -1, -1, -1, -1, -1, -1, -1, /* 78-7f */
48 -1, -1, -1, -1, -1, -1, -1, -1, /* 80-87 */
49 -1, -1, -1, -1, -1, -1, -1, -1, /* 88-8f */
50 -1, -1, -1, -1, -1, -1, -1, -1, /* 90-97 */
51 -1, -1, -1, -1, -1, -1, -1, -1, /* 98-9f */
52 -1, -1, -1, -1, -1, -1, -1, -1, /* a0-a7 */
53 -1, -1, -1, -1, -1, -1, -1, -1, /* a8-af */
54 -1, -1, -1, -1, -1, -1, -1, -1, /* b0-b7 */
55 -1, -1, -1, -1, -1, -1, -1, -1, /* b8-bf */
56 -1, -1, -1, -1, -1, -1, -1, -1, /* c0-c7 */
57 -1, -1, -1, -1, -1, -1, -1, -1, /* c8-cf */
58 -1, -1, -1, -1, -1, -1, -1, -1, /* d0-d7 */
59 -1, -1, -1, -1, -1, -1, -1, -1, /* d8-df */
60 -1, -1, -1, -1, -1, -1, -1, -1, /* e0-e7 */
61 -1, -1, -1, -1, -1, -1, -1, -1, /* e8-ef */
62 -1, -1, -1, -1, -1, -1, -1, -1, /* f0-f7 */
63 -1, -1, -1, -1, -1, -1, -1, -1, /* f8-ff */
64 };
65 return val[c];
66}
67
68int get_sha1_hex(const char *hex, unsigned char *sha1)
69{
70 int i;
71 for (i = 0; i < 20; i++) {
72 unsigned int val = (hexval(hex[0]) << 4) | hexval(hex[1]);
73 if (val & ~0xff)
74 return -1;
75 *sha1++ = val;
76 hex += 2;
77 }
78 return 0;
79}
80
81int safe_create_leading_directories(char *path)
82{
83 char *pos = path;
84 struct stat st;
85
86 if (*pos == '/')
87 pos++;
88
89 while (pos) {
90 pos = strchr(pos, '/');
91 if (!pos)
92 break;
93 *pos = 0;
94 if (!stat(path, &st)) {
95 /* path exists */
96 if (!S_ISDIR(st.st_mode)) {
97 *pos = '/';
98 return -3;
99 }
100 }
101 else if (mkdir(path, 0777)) {
102 *pos = '/';
103 return -1;
104 }
105 else if (adjust_shared_perm(path)) {
106 *pos = '/';
107 return -2;
108 }
109 *pos++ = '/';
110 }
111 return 0;
112}
113
114char * sha1_to_hex(const unsigned char *sha1)
115{
116 static int bufno;
117 static char hexbuffer[4][50];
118 static const char hex[] = "0123456789abcdef";
119 char *buffer = hexbuffer[3 & ++bufno], *buf = buffer;
120 int i;
121
122 for (i = 0; i < 20; i++) {
123 unsigned int val = *sha1++;
124 *buf++ = hex[val >> 4];
125 *buf++ = hex[val & 0xf];
126 }
127 *buf = '\0';
128
129 return buffer;
130}
131
132static void fill_sha1_path(char *pathbuf, const unsigned char *sha1)
133{
134 int i;
135 for (i = 0; i < 20; i++) {
136 static char hex[] = "0123456789abcdef";
137 unsigned int val = sha1[i];
138 char *pos = pathbuf + i*2 + (i > 0);
139 *pos++ = hex[val >> 4];
140 *pos = hex[val & 0xf];
141 }
142}
143
144/*
145 * NOTE! This returns a statically allocated buffer, so you have to be
146 * careful about using it. Do a "xstrdup()" if you need to save the
147 * filename.
148 *
149 * Also note that this returns the location for creating. Reading
150 * SHA1 file can happen from any alternate directory listed in the
151 * DB_ENVIRONMENT environment variable if it is not found in
152 * the primary object database.
153 */
154char *sha1_file_name(const unsigned char *sha1)
155{
156 static char *name, *base;
157
158 if (!base) {
159 const char *sha1_file_directory = get_object_directory();
160 int len = strlen(sha1_file_directory);
161 base = xmalloc(len + 60);
162 memcpy(base, sha1_file_directory, len);
163 memset(base+len, 0, 60);
164 base[len] = '/';
165 base[len+3] = '/';
166 name = base + len + 1;
167 }
168 fill_sha1_path(name, sha1);
169 return base;
170}
171
172char *sha1_pack_name(const unsigned char *sha1)
173{
174 static const char hex[] = "0123456789abcdef";
175 static char *name, *base, *buf;
176 int i;
177
178 if (!base) {
179 const char *sha1_file_directory = get_object_directory();
180 int len = strlen(sha1_file_directory);
181 base = xmalloc(len + 60);
182 sprintf(base, "%s/pack/pack-1234567890123456789012345678901234567890.pack", sha1_file_directory);
183 name = base + len + 11;
184 }
185
186 buf = name;
187
188 for (i = 0; i < 20; i++) {
189 unsigned int val = *sha1++;
190 *buf++ = hex[val >> 4];
191 *buf++ = hex[val & 0xf];
192 }
193
194 return base;
195}
196
197char *sha1_pack_index_name(const unsigned char *sha1)
198{
199 static const char hex[] = "0123456789abcdef";
200 static char *name, *base, *buf;
201 int i;
202
203 if (!base) {
204 const char *sha1_file_directory = get_object_directory();
205 int len = strlen(sha1_file_directory);
206 base = xmalloc(len + 60);
207 sprintf(base, "%s/pack/pack-1234567890123456789012345678901234567890.idx", sha1_file_directory);
208 name = base + len + 11;
209 }
210
211 buf = name;
212
213 for (i = 0; i < 20; i++) {
214 unsigned int val = *sha1++;
215 *buf++ = hex[val >> 4];
216 *buf++ = hex[val & 0xf];
217 }
218
219 return base;
220}
221
222struct alternate_object_database *alt_odb_list;
223static struct alternate_object_database **alt_odb_tail;
224
225static void read_info_alternates(const char * alternates, int depth);
226
227/*
228 * Prepare alternate object database registry.
229 *
230 * The variable alt_odb_list points at the list of struct
231 * alternate_object_database. The elements on this list come from
232 * non-empty elements from colon separated ALTERNATE_DB_ENVIRONMENT
233 * environment variable, and $GIT_OBJECT_DIRECTORY/info/alternates,
234 * whose contents is similar to that environment variable but can be
235 * LF separated. Its base points at a statically allocated buffer that
236 * contains "/the/directory/corresponding/to/.git/objects/...", while
237 * its name points just after the slash at the end of ".git/objects/"
238 * in the example above, and has enough space to hold 40-byte hex
239 * SHA1, an extra slash for the first level indirection, and the
240 * terminating NUL.
241 */
242static int link_alt_odb_entry(const char * entry, int len, const char * relative_base, int depth)
243{
244 struct stat st;
245 const char *objdir = get_object_directory();
246 struct alternate_object_database *ent;
247 struct alternate_object_database *alt;
248 /* 43 = 40-byte + 2 '/' + terminating NUL */
249 int pfxlen = len;
250 int entlen = pfxlen + 43;
251 int base_len = -1;
252
253 if (*entry != '/' && relative_base) {
254 /* Relative alt-odb */
255 if (base_len < 0)
256 base_len = strlen(relative_base) + 1;
257 entlen += base_len;
258 pfxlen += base_len;
259 }
260 ent = xmalloc(sizeof(*ent) + entlen);
261
262 if (*entry != '/' && relative_base) {
263 memcpy(ent->base, relative_base, base_len - 1);
264 ent->base[base_len - 1] = '/';
265 memcpy(ent->base + base_len, entry, len);
266 }
267 else
268 memcpy(ent->base, entry, pfxlen);
269
270 ent->name = ent->base + pfxlen + 1;
271 ent->base[pfxlen + 3] = '/';
272 ent->base[pfxlen] = ent->base[entlen-1] = 0;
273
274 /* Detect cases where alternate disappeared */
275 if (stat(ent->base, &st) || !S_ISDIR(st.st_mode)) {
276 error("object directory %s does not exist; "
277 "check .git/objects/info/alternates.",
278 ent->base);
279 free(ent);
280 return -1;
281 }
282
283 /* Prevent the common mistake of listing the same
284 * thing twice, or object directory itself.
285 */
286 for (alt = alt_odb_list; alt; alt = alt->next) {
287 if (!memcmp(ent->base, alt->base, pfxlen)) {
288 free(ent);
289 return -1;
290 }
291 }
292 if (!memcmp(ent->base, objdir, pfxlen)) {
293 free(ent);
294 return -1;
295 }
296
297 /* add the alternate entry */
298 *alt_odb_tail = ent;
299 alt_odb_tail = &(ent->next);
300 ent->next = NULL;
301
302 /* recursively add alternates */
303 read_info_alternates(ent->base, depth + 1);
304
305 ent->base[pfxlen] = '/';
306
307 return 0;
308}
309
310static void link_alt_odb_entries(const char *alt, const char *ep, int sep,
311 const char *relative_base, int depth)
312{
313 const char *cp, *last;
314
315 if (depth > 5) {
316 error("%s: ignoring alternate object stores, nesting too deep.",
317 relative_base);
318 return;
319 }
320
321 last = alt;
322 while (last < ep) {
323 cp = last;
324 if (cp < ep && *cp == '#') {
325 while (cp < ep && *cp != sep)
326 cp++;
327 last = cp + 1;
328 continue;
329 }
330 while (cp < ep && *cp != sep)
331 cp++;
332 if (last != cp) {
333 if ((*last != '/') && depth) {
334 error("%s: ignoring relative alternate object store %s",
335 relative_base, last);
336 } else {
337 link_alt_odb_entry(last, cp - last,
338 relative_base, depth);
339 }
340 }
341 while (cp < ep && *cp == sep)
342 cp++;
343 last = cp;
344 }
345}
346
347static void read_info_alternates(const char * relative_base, int depth)
348{
349 char *map;
350 struct stat st;
351 char path[PATH_MAX];
352 int fd;
353
354 sprintf(path, "%s/info/alternates", relative_base);
355 fd = open(path, O_RDONLY);
356 if (fd < 0)
357 return;
358 if (fstat(fd, &st) || (st.st_size == 0)) {
359 close(fd);
360 return;
361 }
362 map = mmap(NULL, st.st_size, PROT_READ, MAP_PRIVATE, fd, 0);
363 close(fd);
364 if (map == MAP_FAILED)
365 return;
366
367 link_alt_odb_entries(map, map + st.st_size, '\n', relative_base, depth);
368
369 munmap(map, st.st_size);
370}
371
372void prepare_alt_odb(void)
373{
374 const char *alt;
375
376 alt = getenv(ALTERNATE_DB_ENVIRONMENT);
377 if (!alt) alt = "";
378
379 if (alt_odb_tail)
380 return;
381 alt_odb_tail = &alt_odb_list;
382 link_alt_odb_entries(alt, alt + strlen(alt), ':', NULL, 0);
383
384 read_info_alternates(get_object_directory(), 0);
385}
386
387static char *find_sha1_file(const unsigned char *sha1, struct stat *st)
388{
389 char *name = sha1_file_name(sha1);
390 struct alternate_object_database *alt;
391
392 if (!stat(name, st))
393 return name;
394 prepare_alt_odb();
395 for (alt = alt_odb_list; alt; alt = alt->next) {
396 name = alt->name;
397 fill_sha1_path(name, sha1);
398 if (!stat(alt->base, st))
399 return alt->base;
400 }
401 return NULL;
402}
403
404#define PACK_MAX_SZ (1<<26)
405static int pack_used_ctr;
406static unsigned long pack_mapped;
407struct packed_git *packed_git;
408
409static int check_packed_git_idx(const char *path, unsigned long *idx_size_,
410 void **idx_map_)
411{
412 void *idx_map;
413 unsigned int *index;
414 unsigned long idx_size;
415 int nr, i;
416 int fd = open(path, O_RDONLY);
417 struct stat st;
418 if (fd < 0)
419 return -1;
420 if (fstat(fd, &st)) {
421 close(fd);
422 return -1;
423 }
424 idx_size = st.st_size;
425 idx_map = mmap(NULL, idx_size, PROT_READ, MAP_PRIVATE, fd, 0);
426 close(fd);
427 if (idx_map == MAP_FAILED)
428 return -1;
429
430 index = idx_map;
431 *idx_map_ = idx_map;
432 *idx_size_ = idx_size;
433
434 /* check index map */
435 if (idx_size < 4*256 + 20 + 20)
436 return error("index file too small");
437 nr = 0;
438 for (i = 0; i < 256; i++) {
439 unsigned int n = ntohl(index[i]);
440 if (n < nr)
441 return error("non-monotonic index");
442 nr = n;
443 }
444
445 /*
446 * Total size:
447 * - 256 index entries 4 bytes each
448 * - 24-byte entries * nr (20-byte sha1 + 4-byte offset)
449 * - 20-byte SHA1 of the packfile
450 * - 20-byte SHA1 file checksum
451 */
452 if (idx_size != 4*256 + nr * 24 + 20 + 20)
453 return error("wrong index file size");
454
455 return 0;
456}
457
458static int unuse_one_packed_git(void)
459{
460 struct packed_git *p, *lru = NULL;
461
462 for (p = packed_git; p; p = p->next) {
463 if (p->pack_use_cnt || !p->pack_base)
464 continue;
465 if (!lru || p->pack_last_used < lru->pack_last_used)
466 lru = p;
467 }
468 if (!lru)
469 return 0;
470 munmap(lru->pack_base, lru->pack_size);
471 lru->pack_base = NULL;
472 return 1;
473}
474
475void unuse_packed_git(struct packed_git *p)
476{
477 p->pack_use_cnt--;
478}
479
480int use_packed_git(struct packed_git *p)
481{
482 if (!p->pack_size) {
483 struct stat st;
484 /* We created the struct before we had the pack */
485 stat(p->pack_name, &st);
486 if (!S_ISREG(st.st_mode))
487 die("packfile %s not a regular file", p->pack_name);
488 p->pack_size = st.st_size;
489 }
490 if (!p->pack_base) {
491 int fd;
492 struct stat st;
493 void *map;
494 struct pack_header *hdr;
495
496 pack_mapped += p->pack_size;
497 while (PACK_MAX_SZ < pack_mapped && unuse_one_packed_git())
498 ; /* nothing */
499 fd = open(p->pack_name, O_RDONLY);
500 if (fd < 0)
501 die("packfile %s cannot be opened", p->pack_name);
502 if (fstat(fd, &st)) {
503 close(fd);
504 die("packfile %s cannot be opened", p->pack_name);
505 }
506 if (st.st_size != p->pack_size)
507 die("packfile %s size mismatch.", p->pack_name);
508 map = mmap(NULL, p->pack_size, PROT_READ, MAP_PRIVATE, fd, 0);
509 close(fd);
510 if (map == MAP_FAILED)
511 die("packfile %s cannot be mapped.", p->pack_name);
512 p->pack_base = map;
513
514 /* Check if we understand this pack file. If we don't we're
515 * likely too old to handle it.
516 */
517 hdr = map;
518 if (hdr->hdr_signature != htonl(PACK_SIGNATURE))
519 die("packfile %s isn't actually a pack.", p->pack_name);
520 if (!pack_version_ok(hdr->hdr_version))
521 die("packfile %s is version %i and not supported"
522 " (try upgrading GIT to a newer version)",
523 p->pack_name, ntohl(hdr->hdr_version));
524
525 /* Check if the pack file matches with the index file.
526 * this is cheap.
527 */
528 if (hashcmp((unsigned char *)(p->index_base) +
529 p->index_size - 40,
530 (unsigned char *)p->pack_base +
531 p->pack_size - 20)) {
532 die("packfile %s does not match index.", p->pack_name);
533 }
534 }
535 p->pack_last_used = pack_used_ctr++;
536 p->pack_use_cnt++;
537 return 0;
538}
539
540struct packed_git *add_packed_git(char *path, int path_len, int local)
541{
542 struct stat st;
543 struct packed_git *p;
544 unsigned long idx_size;
545 void *idx_map;
546 unsigned char sha1[20];
547
548 if (check_packed_git_idx(path, &idx_size, &idx_map))
549 return NULL;
550
551 /* do we have a corresponding .pack file? */
552 strcpy(path + path_len - 4, ".pack");
553 if (stat(path, &st) || !S_ISREG(st.st_mode)) {
554 munmap(idx_map, idx_size);
555 return NULL;
556 }
557 /* ok, it looks sane as far as we can check without
558 * actually mapping the pack file.
559 */
560 p = xmalloc(sizeof(*p) + path_len + 2);
561 strcpy(p->pack_name, path);
562 p->index_size = idx_size;
563 p->pack_size = st.st_size;
564 p->index_base = idx_map;
565 p->next = NULL;
566 p->pack_base = NULL;
567 p->pack_last_used = 0;
568 p->pack_use_cnt = 0;
569 p->pack_local = local;
570 if ((path_len > 44) && !get_sha1_hex(path + path_len - 44, sha1))
571 hashcpy(p->sha1, sha1);
572 return p;
573}
574
575struct packed_git *parse_pack_index(unsigned char *sha1)
576{
577 char *path = sha1_pack_index_name(sha1);
578 return parse_pack_index_file(sha1, path);
579}
580
581struct packed_git *parse_pack_index_file(const unsigned char *sha1, char *idx_path)
582{
583 struct packed_git *p;
584 unsigned long idx_size;
585 void *idx_map;
586 char *path;
587
588 if (check_packed_git_idx(idx_path, &idx_size, &idx_map))
589 return NULL;
590
591 path = sha1_pack_name(sha1);
592
593 p = xmalloc(sizeof(*p) + strlen(path) + 2);
594 strcpy(p->pack_name, path);
595 p->index_size = idx_size;
596 p->pack_size = 0;
597 p->index_base = idx_map;
598 p->next = NULL;
599 p->pack_base = NULL;
600 p->pack_last_used = 0;
601 p->pack_use_cnt = 0;
602 hashcpy(p->sha1, sha1);
603 return p;
604}
605
606void install_packed_git(struct packed_git *pack)
607{
608 pack->next = packed_git;
609 packed_git = pack;
610}
611
612static void prepare_packed_git_one(char *objdir, int local)
613{
614 char path[PATH_MAX];
615 int len;
616 DIR *dir;
617 struct dirent *de;
618
619 sprintf(path, "%s/pack", objdir);
620 len = strlen(path);
621 dir = opendir(path);
622 if (!dir) {
623 if (errno != ENOENT)
624 error("unable to open object pack directory: %s: %s",
625 path, strerror(errno));
626 return;
627 }
628 path[len++] = '/';
629 while ((de = readdir(dir)) != NULL) {
630 int namelen = strlen(de->d_name);
631 struct packed_git *p;
632
633 if (!has_extension(de->d_name, ".idx"))
634 continue;
635
636 /* we have .idx. Is it a file we can map? */
637 strcpy(path + len, de->d_name);
638 for (p = packed_git; p; p = p->next) {
639 if (!memcmp(path, p->pack_name, len + namelen - 4))
640 break;
641 }
642 if (p)
643 continue;
644 p = add_packed_git(path, len + namelen, local);
645 if (!p)
646 continue;
647 p->next = packed_git;
648 packed_git = p;
649 }
650 closedir(dir);
651}
652
653static int prepare_packed_git_run_once = 0;
654void prepare_packed_git(void)
655{
656 struct alternate_object_database *alt;
657
658 if (prepare_packed_git_run_once)
659 return;
660 prepare_packed_git_one(get_object_directory(), 1);
661 prepare_alt_odb();
662 for (alt = alt_odb_list; alt; alt = alt->next) {
663 alt->name[-1] = 0;
664 prepare_packed_git_one(alt->base, 0);
665 alt->name[-1] = '/';
666 }
667 prepare_packed_git_run_once = 1;
668}
669
670static void reprepare_packed_git(void)
671{
672 prepare_packed_git_run_once = 0;
673 prepare_packed_git();
674}
675
676int check_sha1_signature(const unsigned char *sha1, void *map, unsigned long size, const char *type)
677{
678 char header[100];
679 unsigned char real_sha1[20];
680 SHA_CTX c;
681
682 SHA1_Init(&c);
683 SHA1_Update(&c, header, 1+sprintf(header, "%s %lu", type, size));
684 SHA1_Update(&c, map, size);
685 SHA1_Final(real_sha1, &c);
686 return hashcmp(sha1, real_sha1) ? -1 : 0;
687}
688
689void *map_sha1_file(const unsigned char *sha1, unsigned long *size)
690{
691 struct stat st;
692 void *map;
693 int fd;
694 char *filename = find_sha1_file(sha1, &st);
695
696 if (!filename) {
697 return NULL;
698 }
699
700 fd = open(filename, O_RDONLY | sha1_file_open_flag);
701 if (fd < 0) {
702 /* See if it works without O_NOATIME */
703 switch (sha1_file_open_flag) {
704 default:
705 fd = open(filename, O_RDONLY);
706 if (fd >= 0)
707 break;
708 /* Fallthrough */
709 case 0:
710 return NULL;
711 }
712
713 /* If it failed once, it will probably fail again.
714 * Stop using O_NOATIME
715 */
716 sha1_file_open_flag = 0;
717 }
718 map = mmap(NULL, st.st_size, PROT_READ, MAP_PRIVATE, fd, 0);
719 close(fd);
720 if (map == MAP_FAILED)
721 return NULL;
722 *size = st.st_size;
723 return map;
724}
725
726int legacy_loose_object(unsigned char *map)
727{
728 unsigned int word;
729
730 /*
731 * Is it a zlib-compressed buffer? If so, the first byte
732 * must be 0x78 (15-bit window size, deflated), and the
733 * first 16-bit word is evenly divisible by 31
734 */
735 word = (map[0] << 8) + map[1];
736 if (map[0] == 0x78 && !(word % 31))
737 return 1;
738 else
739 return 0;
740}
741
742unsigned long unpack_object_header_gently(const unsigned char *buf, unsigned long len, enum object_type *type, unsigned long *sizep)
743{
744 unsigned shift;
745 unsigned char c;
746 unsigned long size;
747 unsigned long used = 0;
748
749 c = buf[used++];
750 *type = (c >> 4) & 7;
751 size = c & 15;
752 shift = 4;
753 while (c & 0x80) {
754 if (len <= used)
755 return 0;
756 if (sizeof(long) * 8 <= shift)
757 return 0;
758 c = buf[used++];
759 size += (c & 0x7f) << shift;
760 shift += 7;
761 }
762 *sizep = size;
763 return used;
764}
765
766static int unpack_sha1_header(z_stream *stream, unsigned char *map, unsigned long mapsize, void *buffer, unsigned long bufsiz)
767{
768 unsigned long size, used;
769 static const char valid_loose_object_type[8] = {
770 0, /* OBJ_EXT */
771 1, 1, 1, 1, /* "commit", "tree", "blob", "tag" */
772 0, /* "delta" and others are invalid in a loose object */
773 };
774 enum object_type type;
775
776 /* Get the data stream */
777 memset(stream, 0, sizeof(*stream));
778 stream->next_in = map;
779 stream->avail_in = mapsize;
780 stream->next_out = buffer;
781 stream->avail_out = bufsiz;
782
783 if (legacy_loose_object(map)) {
784 inflateInit(stream);
785 return inflate(stream, 0);
786 }
787
788 used = unpack_object_header_gently(map, mapsize, &type, &size);
789 if (!used || !valid_loose_object_type[type])
790 return -1;
791 map += used;
792 mapsize -= used;
793
794 /* Set up the stream for the rest.. */
795 stream->next_in = map;
796 stream->avail_in = mapsize;
797 inflateInit(stream);
798
799 /* And generate the fake traditional header */
800 stream->total_out = 1 + snprintf(buffer, bufsiz, "%s %lu",
801 type_names[type], size);
802 return 0;
803}
804
805static void *unpack_sha1_rest(z_stream *stream, void *buffer, unsigned long size)
806{
807 int bytes = strlen(buffer) + 1;
808 unsigned char *buf = xmalloc(1+size);
809 unsigned long n;
810
811 n = stream->total_out - bytes;
812 if (n > size)
813 n = size;
814 memcpy(buf, (char *) buffer + bytes, n);
815 bytes = n;
816 if (bytes < size) {
817 stream->next_out = buf + bytes;
818 stream->avail_out = size - bytes;
819 while (inflate(stream, Z_FINISH) == Z_OK)
820 /* nothing */;
821 }
822 buf[size] = 0;
823 inflateEnd(stream);
824 return buf;
825}
826
827/*
828 * We used to just use "sscanf()", but that's actually way
829 * too permissive for what we want to check. So do an anal
830 * object header parse by hand.
831 */
832static int parse_sha1_header(char *hdr, char *type, unsigned long *sizep)
833{
834 int i;
835 unsigned long size;
836
837 /*
838 * The type can be at most ten bytes (including the
839 * terminating '\0' that we add), and is followed by
840 * a space.
841 */
842 i = 10;
843 for (;;) {
844 char c = *hdr++;
845 if (c == ' ')
846 break;
847 if (!--i)
848 return -1;
849 *type++ = c;
850 }
851 *type = 0;
852
853 /*
854 * The length must follow immediately, and be in canonical
855 * decimal format (ie "010" is not valid).
856 */
857 size = *hdr++ - '0';
858 if (size > 9)
859 return -1;
860 if (size) {
861 for (;;) {
862 unsigned long c = *hdr - '0';
863 if (c > 9)
864 break;
865 hdr++;
866 size = size * 10 + c;
867 }
868 }
869 *sizep = size;
870
871 /*
872 * The length must be followed by a zero byte
873 */
874 return *hdr ? -1 : 0;
875}
876
877void * unpack_sha1_file(void *map, unsigned long mapsize, char *type, unsigned long *size)
878{
879 int ret;
880 z_stream stream;
881 char hdr[8192];
882
883 ret = unpack_sha1_header(&stream, map, mapsize, hdr, sizeof(hdr));
884 if (ret < Z_OK || parse_sha1_header(hdr, type, size) < 0)
885 return NULL;
886
887 return unpack_sha1_rest(&stream, hdr, *size);
888}
889
890/* forward declaration for a mutually recursive function */
891static int packed_object_info(struct pack_entry *entry,
892 char *type, unsigned long *sizep);
893
894static int packed_delta_info(unsigned char *base_sha1,
895 unsigned long delta_size,
896 unsigned long left,
897 char *type,
898 unsigned long *sizep,
899 struct packed_git *p)
900{
901 struct pack_entry base_ent;
902
903 if (left < 20)
904 die("truncated pack file");
905
906 /* The base entry _must_ be in the same pack */
907 if (!find_pack_entry_one(base_sha1, &base_ent, p))
908 die("failed to find delta-pack base object %s",
909 sha1_to_hex(base_sha1));
910
911 /* We choose to only get the type of the base object and
912 * ignore potentially corrupt pack file that expects the delta
913 * based on a base with a wrong size. This saves tons of
914 * inflate() calls.
915 */
916
917 if (packed_object_info(&base_ent, type, NULL))
918 die("cannot get info for delta-pack base");
919
920 if (sizep) {
921 const unsigned char *data;
922 unsigned char delta_head[64];
923 unsigned long result_size;
924 z_stream stream;
925 int st;
926
927 memset(&stream, 0, sizeof(stream));
928
929 data = stream.next_in = base_sha1 + 20;
930 stream.avail_in = left - 20;
931 stream.next_out = delta_head;
932 stream.avail_out = sizeof(delta_head);
933
934 inflateInit(&stream);
935 st = inflate(&stream, Z_FINISH);
936 inflateEnd(&stream);
937 if ((st != Z_STREAM_END) &&
938 stream.total_out != sizeof(delta_head))
939 die("delta data unpack-initial failed");
940
941 /* Examine the initial part of the delta to figure out
942 * the result size.
943 */
944 data = delta_head;
945
946 /* ignore base size */
947 get_delta_hdr_size(&data, delta_head+sizeof(delta_head));
948
949 /* Read the result size */
950 result_size = get_delta_hdr_size(&data, delta_head+sizeof(delta_head));
951 *sizep = result_size;
952 }
953 return 0;
954}
955
956static unsigned long unpack_object_header(struct packed_git *p, unsigned long offset,
957 enum object_type *type, unsigned long *sizep)
958{
959 unsigned long used;
960
961 if (p->pack_size <= offset)
962 die("object offset outside of pack file");
963
964 used = unpack_object_header_gently((unsigned char *)p->pack_base +
965 offset,
966 p->pack_size - offset, type, sizep);
967 if (!used)
968 die("object offset outside of pack file");
969
970 return offset + used;
971}
972
973int check_reuse_pack_delta(struct packed_git *p, unsigned long offset,
974 unsigned char *base, unsigned long *sizep,
975 enum object_type *kindp)
976{
977 unsigned long ptr;
978 int status = -1;
979
980 use_packed_git(p);
981 ptr = offset;
982 ptr = unpack_object_header(p, ptr, kindp, sizep);
983 if (*kindp != OBJ_DELTA)
984 goto done;
985 hashcpy(base, (unsigned char *) p->pack_base + ptr);
986 status = 0;
987 done:
988 unuse_packed_git(p);
989 return status;
990}
991
992void packed_object_info_detail(struct pack_entry *e,
993 char *type,
994 unsigned long *size,
995 unsigned long *store_size,
996 unsigned int *delta_chain_length,
997 unsigned char *base_sha1)
998{
999 struct packed_git *p = e->p;
1000 unsigned long offset;
1001 unsigned char *pack;
1002 enum object_type kind;
1003
1004 offset = unpack_object_header(p, e->offset, &kind, size);
1005 pack = (unsigned char *) p->pack_base + offset;
1006 if (kind != OBJ_DELTA)
1007 *delta_chain_length = 0;
1008 else {
1009 unsigned int chain_length = 0;
1010 if (p->pack_size <= offset + 20)
1011 die("pack file %s records an incomplete delta base",
1012 p->pack_name);
1013 hashcpy(base_sha1, pack);
1014 do {
1015 struct pack_entry base_ent;
1016 unsigned long junk;
1017
1018 find_pack_entry_one(pack, &base_ent, p);
1019 offset = unpack_object_header(p, base_ent.offset,
1020 &kind, &junk);
1021 pack = (unsigned char *) p->pack_base + offset;
1022 chain_length++;
1023 } while (kind == OBJ_DELTA);
1024 *delta_chain_length = chain_length;
1025 }
1026 switch (kind) {
1027 case OBJ_COMMIT:
1028 case OBJ_TREE:
1029 case OBJ_BLOB:
1030 case OBJ_TAG:
1031 strcpy(type, type_names[kind]);
1032 break;
1033 default:
1034 die("corrupted pack file %s containing object of kind %d",
1035 p->pack_name, kind);
1036 }
1037 *store_size = 0; /* notyet */
1038}
1039
1040static int packed_object_info(struct pack_entry *entry,
1041 char *type, unsigned long *sizep)
1042{
1043 struct packed_git *p = entry->p;
1044 unsigned long offset, size, left;
1045 unsigned char *pack;
1046 enum object_type kind;
1047 int retval;
1048
1049 if (use_packed_git(p))
1050 die("cannot map packed file");
1051
1052 offset = unpack_object_header(p, entry->offset, &kind, &size);
1053 pack = (unsigned char *) p->pack_base + offset;
1054 left = p->pack_size - offset;
1055
1056 switch (kind) {
1057 case OBJ_DELTA:
1058 retval = packed_delta_info(pack, size, left, type, sizep, p);
1059 unuse_packed_git(p);
1060 return retval;
1061 case OBJ_COMMIT:
1062 case OBJ_TREE:
1063 case OBJ_BLOB:
1064 case OBJ_TAG:
1065 strcpy(type, type_names[kind]);
1066 break;
1067 default:
1068 die("corrupted pack file %s containing object of kind %d",
1069 p->pack_name, kind);
1070 }
1071 if (sizep)
1072 *sizep = size;
1073 unuse_packed_git(p);
1074 return 0;
1075}
1076
1077static void *unpack_compressed_entry(struct packed_git *p,
1078 unsigned long offset,
1079 unsigned long size)
1080{
1081 int st;
1082 z_stream stream;
1083 unsigned char *buffer;
1084
1085 buffer = xmalloc(size + 1);
1086 buffer[size] = 0;
1087 memset(&stream, 0, sizeof(stream));
1088 stream.next_in = (unsigned char*)p->pack_base + offset;
1089 stream.avail_in = p->pack_size - offset;
1090 stream.next_out = buffer;
1091 stream.avail_out = size;
1092
1093 inflateInit(&stream);
1094 st = inflate(&stream, Z_FINISH);
1095 inflateEnd(&stream);
1096 if ((st != Z_STREAM_END) || stream.total_out != size) {
1097 free(buffer);
1098 return NULL;
1099 }
1100
1101 return buffer;
1102}
1103
1104static void *unpack_delta_entry(struct packed_git *p,
1105 unsigned long offset,
1106 unsigned long delta_size,
1107 char *type,
1108 unsigned long *sizep)
1109{
1110 struct pack_entry base_ent;
1111 void *delta_data, *result, *base;
1112 unsigned long result_size, base_size;
1113 unsigned char* base_sha1;
1114
1115 if ((offset + 20) >= p->pack_size)
1116 die("truncated pack file");
1117
1118 /* The base entry _must_ be in the same pack */
1119 base_sha1 = (unsigned char*)p->pack_base + offset;
1120 if (!find_pack_entry_one(base_sha1, &base_ent, p))
1121 die("failed to find delta-pack base object %s",
1122 sha1_to_hex(base_sha1));
1123 base = unpack_entry_gently(&base_ent, type, &base_size);
1124 if (!base)
1125 die("failed to read delta-pack base object %s",
1126 sha1_to_hex(base_sha1));
1127
1128 delta_data = unpack_compressed_entry(p, offset + 20, delta_size);
1129 result = patch_delta(base, base_size,
1130 delta_data, delta_size,
1131 &result_size);
1132 if (!result)
1133 die("failed to apply delta");
1134 free(delta_data);
1135 free(base);
1136 *sizep = result_size;
1137 return result;
1138}
1139
1140static void *unpack_entry(struct pack_entry *entry,
1141 char *type, unsigned long *sizep)
1142{
1143 struct packed_git *p = entry->p;
1144 void *retval;
1145
1146 if (use_packed_git(p))
1147 die("cannot map packed file");
1148 retval = unpack_entry_gently(entry, type, sizep);
1149 unuse_packed_git(p);
1150 if (!retval)
1151 die("corrupted pack file %s", p->pack_name);
1152 return retval;
1153}
1154
1155/* The caller is responsible for use_packed_git()/unuse_packed_git() pair */
1156void *unpack_entry_gently(struct pack_entry *entry,
1157 char *type, unsigned long *sizep)
1158{
1159 struct packed_git *p = entry->p;
1160 unsigned long offset, size;
1161 enum object_type kind;
1162
1163 offset = unpack_object_header(p, entry->offset, &kind, &size);
1164 switch (kind) {
1165 case OBJ_DELTA:
1166 return unpack_delta_entry(p, offset, size, type, sizep);
1167 case OBJ_COMMIT:
1168 case OBJ_TREE:
1169 case OBJ_BLOB:
1170 case OBJ_TAG:
1171 strcpy(type, type_names[kind]);
1172 *sizep = size;
1173 return unpack_compressed_entry(p, offset, size);
1174 default:
1175 return NULL;
1176 }
1177}
1178
1179int num_packed_objects(const struct packed_git *p)
1180{
1181 /* See check_packed_git_idx() */
1182 return (p->index_size - 20 - 20 - 4*256) / 24;
1183}
1184
1185int nth_packed_object_sha1(const struct packed_git *p, int n,
1186 unsigned char* sha1)
1187{
1188 void *index = p->index_base + 256;
1189 if (n < 0 || num_packed_objects(p) <= n)
1190 return -1;
1191 hashcpy(sha1, (unsigned char *) index + (24 * n) + 4);
1192 return 0;
1193}
1194
1195int find_pack_entry_one(const unsigned char *sha1,
1196 struct pack_entry *e, struct packed_git *p)
1197{
1198 unsigned int *level1_ofs = p->index_base;
1199 int hi = ntohl(level1_ofs[*sha1]);
1200 int lo = ((*sha1 == 0x0) ? 0 : ntohl(level1_ofs[*sha1 - 1]));
1201 void *index = p->index_base + 256;
1202
1203 do {
1204 int mi = (lo + hi) / 2;
1205 int cmp = hashcmp((unsigned char *)index + (24 * mi) + 4, sha1);
1206 if (!cmp) {
1207 e->offset = ntohl(*((unsigned int *) ((char *) index + (24 * mi))));
1208 hashcpy(e->sha1, sha1);
1209 e->p = p;
1210 return 1;
1211 }
1212 if (cmp > 0)
1213 hi = mi;
1214 else
1215 lo = mi+1;
1216 } while (lo < hi);
1217 return 0;
1218}
1219
1220static int find_pack_entry(const unsigned char *sha1, struct pack_entry *e)
1221{
1222 struct packed_git *p;
1223 prepare_packed_git();
1224
1225 for (p = packed_git; p; p = p->next) {
1226 if (find_pack_entry_one(sha1, e, p))
1227 return 1;
1228 }
1229 return 0;
1230}
1231
1232struct packed_git *find_sha1_pack(const unsigned char *sha1,
1233 struct packed_git *packs)
1234{
1235 struct packed_git *p;
1236 struct pack_entry e;
1237
1238 for (p = packs; p; p = p->next) {
1239 if (find_pack_entry_one(sha1, &e, p))
1240 return p;
1241 }
1242 return NULL;
1243
1244}
1245
1246int sha1_object_info(const unsigned char *sha1, char *type, unsigned long *sizep)
1247{
1248 int status;
1249 unsigned long mapsize, size;
1250 void *map;
1251 z_stream stream;
1252 char hdr[128];
1253
1254 map = map_sha1_file(sha1, &mapsize);
1255 if (!map) {
1256 struct pack_entry e;
1257
1258 if (find_pack_entry(sha1, &e))
1259 return packed_object_info(&e, type, sizep);
1260 reprepare_packed_git();
1261 if (find_pack_entry(sha1, &e))
1262 return packed_object_info(&e, type, sizep);
1263 return error("unable to find %s", sha1_to_hex(sha1));
1264 }
1265 if (unpack_sha1_header(&stream, map, mapsize, hdr, sizeof(hdr)) < 0)
1266 status = error("unable to unpack %s header",
1267 sha1_to_hex(sha1));
1268 if (parse_sha1_header(hdr, type, &size) < 0)
1269 status = error("unable to parse %s header", sha1_to_hex(sha1));
1270 else {
1271 status = 0;
1272 if (sizep)
1273 *sizep = size;
1274 }
1275 inflateEnd(&stream);
1276 munmap(map, mapsize);
1277 return status;
1278}
1279
1280static void *read_packed_sha1(const unsigned char *sha1, char *type, unsigned long *size)
1281{
1282 struct pack_entry e;
1283
1284 if (!find_pack_entry(sha1, &e)) {
1285 error("cannot read sha1_file for %s", sha1_to_hex(sha1));
1286 return NULL;
1287 }
1288 return unpack_entry(&e, type, size);
1289}
1290
1291void * read_sha1_file(const unsigned char *sha1, char *type, unsigned long *size)
1292{
1293 unsigned long mapsize;
1294 void *map, *buf;
1295 struct pack_entry e;
1296
1297 if (find_pack_entry(sha1, &e))
1298 return read_packed_sha1(sha1, type, size);
1299 map = map_sha1_file(sha1, &mapsize);
1300 if (map) {
1301 buf = unpack_sha1_file(map, mapsize, type, size);
1302 munmap(map, mapsize);
1303 return buf;
1304 }
1305 reprepare_packed_git();
1306 if (find_pack_entry(sha1, &e))
1307 return read_packed_sha1(sha1, type, size);
1308 return NULL;
1309}
1310
1311void *read_object_with_reference(const unsigned char *sha1,
1312 const char *required_type,
1313 unsigned long *size,
1314 unsigned char *actual_sha1_return)
1315{
1316 char type[20];
1317 void *buffer;
1318 unsigned long isize;
1319 unsigned char actual_sha1[20];
1320
1321 hashcpy(actual_sha1, sha1);
1322 while (1) {
1323 int ref_length = -1;
1324 const char *ref_type = NULL;
1325
1326 buffer = read_sha1_file(actual_sha1, type, &isize);
1327 if (!buffer)
1328 return NULL;
1329 if (!strcmp(type, required_type)) {
1330 *size = isize;
1331 if (actual_sha1_return)
1332 hashcpy(actual_sha1_return, actual_sha1);
1333 return buffer;
1334 }
1335 /* Handle references */
1336 else if (!strcmp(type, commit_type))
1337 ref_type = "tree ";
1338 else if (!strcmp(type, tag_type))
1339 ref_type = "object ";
1340 else {
1341 free(buffer);
1342 return NULL;
1343 }
1344 ref_length = strlen(ref_type);
1345
1346 if (memcmp(buffer, ref_type, ref_length) ||
1347 get_sha1_hex((char *) buffer + ref_length, actual_sha1)) {
1348 free(buffer);
1349 return NULL;
1350 }
1351 free(buffer);
1352 /* Now we have the ID of the referred-to object in
1353 * actual_sha1. Check again. */
1354 }
1355}
1356
1357char *write_sha1_file_prepare(void *buf,
1358 unsigned long len,
1359 const char *type,
1360 unsigned char *sha1,
1361 unsigned char *hdr,
1362 int *hdrlen)
1363{
1364 SHA_CTX c;
1365
1366 /* Generate the header */
1367 *hdrlen = sprintf((char *)hdr, "%s %lu", type, len)+1;
1368
1369 /* Sha1.. */
1370 SHA1_Init(&c);
1371 SHA1_Update(&c, hdr, *hdrlen);
1372 SHA1_Update(&c, buf, len);
1373 SHA1_Final(sha1, &c);
1374
1375 return sha1_file_name(sha1);
1376}
1377
1378/*
1379 * Link the tempfile to the final place, possibly creating the
1380 * last directory level as you do so.
1381 *
1382 * Returns the errno on failure, 0 on success.
1383 */
1384static int link_temp_to_file(const char *tmpfile, const char *filename)
1385{
1386 int ret;
1387 char *dir;
1388
1389 if (!link(tmpfile, filename))
1390 return 0;
1391
1392 /*
1393 * Try to mkdir the last path component if that failed.
1394 *
1395 * Re-try the "link()" regardless of whether the mkdir
1396 * succeeds, since a race might mean that somebody
1397 * else succeeded.
1398 */
1399 ret = errno;
1400 dir = strrchr(filename, '/');
1401 if (dir) {
1402 *dir = 0;
1403 mkdir(filename, 0777);
1404 if (adjust_shared_perm(filename))
1405 return -2;
1406 *dir = '/';
1407 if (!link(tmpfile, filename))
1408 return 0;
1409 ret = errno;
1410 }
1411 return ret;
1412}
1413
1414/*
1415 * Move the just written object into its final resting place
1416 */
1417int move_temp_to_file(const char *tmpfile, const char *filename)
1418{
1419 int ret = link_temp_to_file(tmpfile, filename);
1420
1421 /*
1422 * Coda hack - coda doesn't like cross-directory links,
1423 * so we fall back to a rename, which will mean that it
1424 * won't be able to check collisions, but that's not a
1425 * big deal.
1426 *
1427 * The same holds for FAT formatted media.
1428 *
1429 * When this succeeds, we just return 0. We have nothing
1430 * left to unlink.
1431 */
1432 if (ret && ret != EEXIST) {
1433 if (!rename(tmpfile, filename))
1434 return 0;
1435 ret = errno;
1436 }
1437 unlink(tmpfile);
1438 if (ret) {
1439 if (ret != EEXIST) {
1440 fprintf(stderr, "unable to write sha1 filename %s: %s\n", filename, strerror(ret));
1441 return -1;
1442 }
1443 /* FIXME!!! Collision check here ? */
1444 }
1445
1446 return 0;
1447}
1448
1449static int write_buffer(int fd, const void *buf, size_t len)
1450{
1451 while (len) {
1452 ssize_t size;
1453
1454 size = write(fd, buf, len);
1455 if (!size)
1456 return error("file write: disk full");
1457 if (size < 0) {
1458 if (errno == EINTR || errno == EAGAIN)
1459 continue;
1460 return error("file write error (%s)", strerror(errno));
1461 }
1462 len -= size;
1463 buf = (char *) buf + size;
1464 }
1465 return 0;
1466}
1467
1468static int write_binary_header(unsigned char *hdr, enum object_type type, unsigned long len)
1469{
1470 int hdr_len;
1471 unsigned char c;
1472
1473 c = (type << 4) | (len & 15);
1474 len >>= 4;
1475 hdr_len = 1;
1476 while (len) {
1477 *hdr++ = c | 0x80;
1478 hdr_len++;
1479 c = (len & 0x7f);
1480 len >>= 7;
1481 }
1482 *hdr = c;
1483 return hdr_len;
1484}
1485
1486static void setup_object_header(z_stream *stream, const char *type, unsigned long len)
1487{
1488 int obj_type, hdr;
1489
1490 if (use_legacy_headers) {
1491 while (deflate(stream, 0) == Z_OK)
1492 /* nothing */;
1493 return;
1494 }
1495 if (!strcmp(type, blob_type))
1496 obj_type = OBJ_BLOB;
1497 else if (!strcmp(type, tree_type))
1498 obj_type = OBJ_TREE;
1499 else if (!strcmp(type, commit_type))
1500 obj_type = OBJ_COMMIT;
1501 else if (!strcmp(type, tag_type))
1502 obj_type = OBJ_TAG;
1503 else
1504 die("trying to generate bogus object of type '%s'", type);
1505 hdr = write_binary_header(stream->next_out, obj_type, len);
1506 stream->total_out = hdr;
1507 stream->next_out += hdr;
1508 stream->avail_out -= hdr;
1509}
1510
1511int write_sha1_file(void *buf, unsigned long len, const char *type, unsigned char *returnsha1)
1512{
1513 int size;
1514 unsigned char *compressed;
1515 z_stream stream;
1516 unsigned char sha1[20];
1517 char *filename;
1518 static char tmpfile[PATH_MAX];
1519 unsigned char hdr[50];
1520 int fd, hdrlen;
1521
1522 /* Normally if we have it in the pack then we do not bother writing
1523 * it out into .git/objects/??/?{38} file.
1524 */
1525 filename = write_sha1_file_prepare(buf, len, type, sha1, hdr, &hdrlen);
1526 if (returnsha1)
1527 hashcpy(returnsha1, sha1);
1528 if (has_sha1_file(sha1))
1529 return 0;
1530 fd = open(filename, O_RDONLY);
1531 if (fd >= 0) {
1532 /*
1533 * FIXME!!! We might do collision checking here, but we'd
1534 * need to uncompress the old file and check it. Later.
1535 */
1536 close(fd);
1537 return 0;
1538 }
1539
1540 if (errno != ENOENT) {
1541 fprintf(stderr, "sha1 file %s: %s\n", filename, strerror(errno));
1542 return -1;
1543 }
1544
1545 snprintf(tmpfile, sizeof(tmpfile), "%s/obj_XXXXXX", get_object_directory());
1546
1547 fd = mkstemp(tmpfile);
1548 if (fd < 0) {
1549 fprintf(stderr, "unable to create temporary sha1 filename %s: %s\n", tmpfile, strerror(errno));
1550 return -1;
1551 }
1552
1553 /* Set it up */
1554 memset(&stream, 0, sizeof(stream));
1555 deflateInit(&stream, zlib_compression_level);
1556 size = 8 + deflateBound(&stream, len+hdrlen);
1557 compressed = xmalloc(size);
1558
1559 /* Compress it */
1560 stream.next_out = compressed;
1561 stream.avail_out = size;
1562
1563 /* First header.. */
1564 stream.next_in = hdr;
1565 stream.avail_in = hdrlen;
1566 setup_object_header(&stream, type, len);
1567
1568 /* Then the data itself.. */
1569 stream.next_in = buf;
1570 stream.avail_in = len;
1571 while (deflate(&stream, Z_FINISH) == Z_OK)
1572 /* nothing */;
1573 deflateEnd(&stream);
1574 size = stream.total_out;
1575
1576 if (write_buffer(fd, compressed, size) < 0)
1577 die("unable to write sha1 file");
1578 fchmod(fd, 0444);
1579 close(fd);
1580 free(compressed);
1581
1582 return move_temp_to_file(tmpfile, filename);
1583}
1584
1585/*
1586 * We need to unpack and recompress the object for writing
1587 * it out to a different file.
1588 */
1589static void *repack_object(const unsigned char *sha1, unsigned long *objsize)
1590{
1591 size_t size;
1592 z_stream stream;
1593 unsigned char *unpacked;
1594 unsigned long len;
1595 char type[20];
1596 char hdr[50];
1597 int hdrlen;
1598 void *buf;
1599
1600 /* need to unpack and recompress it by itself */
1601 unpacked = read_packed_sha1(sha1, type, &len);
1602
1603 hdrlen = sprintf(hdr, "%s %lu", type, len) + 1;
1604
1605 /* Set it up */
1606 memset(&stream, 0, sizeof(stream));
1607 deflateInit(&stream, zlib_compression_level);
1608 size = deflateBound(&stream, len + hdrlen);
1609 buf = xmalloc(size);
1610
1611 /* Compress it */
1612 stream.next_out = buf;
1613 stream.avail_out = size;
1614
1615 /* First header.. */
1616 stream.next_in = (void *)hdr;
1617 stream.avail_in = hdrlen;
1618 while (deflate(&stream, 0) == Z_OK)
1619 /* nothing */;
1620
1621 /* Then the data itself.. */
1622 stream.next_in = unpacked;
1623 stream.avail_in = len;
1624 while (deflate(&stream, Z_FINISH) == Z_OK)
1625 /* nothing */;
1626 deflateEnd(&stream);
1627 free(unpacked);
1628
1629 *objsize = stream.total_out;
1630 return buf;
1631}
1632
1633int write_sha1_to_fd(int fd, const unsigned char *sha1)
1634{
1635 int retval;
1636 unsigned long objsize;
1637 void *buf = map_sha1_file(sha1, &objsize);
1638
1639 if (buf) {
1640 retval = write_buffer(fd, buf, objsize);
1641 munmap(buf, objsize);
1642 return retval;
1643 }
1644
1645 buf = repack_object(sha1, &objsize);
1646 retval = write_buffer(fd, buf, objsize);
1647 free(buf);
1648 return retval;
1649}
1650
1651int write_sha1_from_fd(const unsigned char *sha1, int fd, char *buffer,
1652 size_t bufsize, size_t *bufposn)
1653{
1654 char tmpfile[PATH_MAX];
1655 int local;
1656 z_stream stream;
1657 unsigned char real_sha1[20];
1658 unsigned char discard[4096];
1659 int ret;
1660 SHA_CTX c;
1661
1662 snprintf(tmpfile, sizeof(tmpfile), "%s/obj_XXXXXX", get_object_directory());
1663
1664 local = mkstemp(tmpfile);
1665 if (local < 0)
1666 return error("Couldn't open %s for %s",
1667 tmpfile, sha1_to_hex(sha1));
1668
1669 memset(&stream, 0, sizeof(stream));
1670
1671 inflateInit(&stream);
1672
1673 SHA1_Init(&c);
1674
1675 do {
1676 ssize_t size;
1677 if (*bufposn) {
1678 stream.avail_in = *bufposn;
1679 stream.next_in = (unsigned char *) buffer;
1680 do {
1681 stream.next_out = discard;
1682 stream.avail_out = sizeof(discard);
1683 ret = inflate(&stream, Z_SYNC_FLUSH);
1684 SHA1_Update(&c, discard, sizeof(discard) -
1685 stream.avail_out);
1686 } while (stream.avail_in && ret == Z_OK);
1687 if (write_buffer(local, buffer, *bufposn - stream.avail_in) < 0)
1688 die("unable to write sha1 file");
1689 memmove(buffer, buffer + *bufposn - stream.avail_in,
1690 stream.avail_in);
1691 *bufposn = stream.avail_in;
1692 if (ret != Z_OK)
1693 break;
1694 }
1695 size = read(fd, buffer + *bufposn, bufsize - *bufposn);
1696 if (size <= 0) {
1697 close(local);
1698 unlink(tmpfile);
1699 if (!size)
1700 return error("Connection closed?");
1701 perror("Reading from connection");
1702 return -1;
1703 }
1704 *bufposn += size;
1705 } while (1);
1706 inflateEnd(&stream);
1707
1708 close(local);
1709 SHA1_Final(real_sha1, &c);
1710 if (ret != Z_STREAM_END) {
1711 unlink(tmpfile);
1712 return error("File %s corrupted", sha1_to_hex(sha1));
1713 }
1714 if (hashcmp(sha1, real_sha1)) {
1715 unlink(tmpfile);
1716 return error("File %s has bad hash", sha1_to_hex(sha1));
1717 }
1718
1719 return move_temp_to_file(tmpfile, sha1_file_name(sha1));
1720}
1721
1722int has_pack_index(const unsigned char *sha1)
1723{
1724 struct stat st;
1725 if (stat(sha1_pack_index_name(sha1), &st))
1726 return 0;
1727 return 1;
1728}
1729
1730int has_pack_file(const unsigned char *sha1)
1731{
1732 struct stat st;
1733 if (stat(sha1_pack_name(sha1), &st))
1734 return 0;
1735 return 1;
1736}
1737
1738int has_sha1_pack(const unsigned char *sha1)
1739{
1740 struct pack_entry e;
1741 return find_pack_entry(sha1, &e);
1742}
1743
1744int has_sha1_file(const unsigned char *sha1)
1745{
1746 struct stat st;
1747 struct pack_entry e;
1748
1749 if (find_pack_entry(sha1, &e))
1750 return 1;
1751 return find_sha1_file(sha1, &st) ? 1 : 0;
1752}
1753
1754/*
1755 * reads from fd as long as possible into a supplied buffer of size bytes.
1756 * If necessary the buffer's size is increased using realloc()
1757 *
1758 * returns 0 if anything went fine and -1 otherwise
1759 *
1760 * NOTE: both buf and size may change, but even when -1 is returned
1761 * you still have to free() it yourself.
1762 */
1763int read_pipe(int fd, char** return_buf, unsigned long* return_size)
1764{
1765 char* buf = *return_buf;
1766 unsigned long size = *return_size;
1767 int iret;
1768 unsigned long off = 0;
1769
1770 do {
1771 iret = xread(fd, buf + off, size - off);
1772 if (iret > 0) {
1773 off += iret;
1774 if (off == size) {
1775 size *= 2;
1776 buf = xrealloc(buf, size);
1777 }
1778 }
1779 } while (iret > 0);
1780
1781 *return_buf = buf;
1782 *return_size = off;
1783
1784 if (iret < 0)
1785 return -1;
1786 return 0;
1787}
1788
1789int index_pipe(unsigned char *sha1, int fd, const char *type, int write_object)
1790{
1791 unsigned long size = 4096;
1792 char *buf = xmalloc(size);
1793 int ret;
1794 unsigned char hdr[50];
1795 int hdrlen;
1796
1797 if (read_pipe(fd, &buf, &size)) {
1798 free(buf);
1799 return -1;
1800 }
1801
1802 if (!type)
1803 type = blob_type;
1804 if (write_object)
1805 ret = write_sha1_file(buf, size, type, sha1);
1806 else {
1807 write_sha1_file_prepare(buf, size, type, sha1, hdr, &hdrlen);
1808 ret = 0;
1809 }
1810 free(buf);
1811 return ret;
1812}
1813
1814int index_fd(unsigned char *sha1, int fd, struct stat *st, int write_object, const char *type)
1815{
1816 unsigned long size = st->st_size;
1817 void *buf;
1818 int ret;
1819 unsigned char hdr[50];
1820 int hdrlen;
1821
1822 buf = "";
1823 if (size)
1824 buf = mmap(NULL, size, PROT_READ, MAP_PRIVATE, fd, 0);
1825 close(fd);
1826 if (buf == MAP_FAILED)
1827 return -1;
1828
1829 if (!type)
1830 type = blob_type;
1831 if (write_object)
1832 ret = write_sha1_file(buf, size, type, sha1);
1833 else {
1834 write_sha1_file_prepare(buf, size, type, sha1, hdr, &hdrlen);
1835 ret = 0;
1836 }
1837 if (size)
1838 munmap(buf, size);
1839 return ret;
1840}
1841
1842int index_path(unsigned char *sha1, const char *path, struct stat *st, int write_object)
1843{
1844 int fd;
1845 char *target;
1846
1847 switch (st->st_mode & S_IFMT) {
1848 case S_IFREG:
1849 fd = open(path, O_RDONLY);
1850 if (fd < 0)
1851 return error("open(\"%s\"): %s", path,
1852 strerror(errno));
1853 if (index_fd(sha1, fd, st, write_object, NULL) < 0)
1854 return error("%s: failed to insert into database",
1855 path);
1856 break;
1857 case S_IFLNK:
1858 target = xmalloc(st->st_size+1);
1859 if (readlink(path, target, st->st_size+1) != st->st_size) {
1860 char *errstr = strerror(errno);
1861 free(target);
1862 return error("readlink(\"%s\"): %s", path,
1863 errstr);
1864 }
1865 if (!write_object) {
1866 unsigned char hdr[50];
1867 int hdrlen;
1868 write_sha1_file_prepare(target, st->st_size, blob_type,
1869 sha1, hdr, &hdrlen);
1870 } else if (write_sha1_file(target, st->st_size, blob_type, sha1))
1871 return error("%s: failed to insert into database",
1872 path);
1873 free(target);
1874 break;
1875 default:
1876 return error("%s: unsupported file type", path);
1877 }
1878 return 0;
1879}