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