Handle symlinks graciously
[git/jrn.git] / sha1_file.c
blob6b7577dbc4ed845da1a70b6d727370423b086735
1 /*
2 * GIT - The information manager from hell
4 * Copyright (C) Linus Torvalds, 2005
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"
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
23 const unsigned char null_sha1[20] = { 0, };
25 static unsigned int sha1_file_open_flag = O_NOATIME;
27 static unsigned hexval(char c)
29 if (c >= '0' && c <= '9')
30 return c - '0';
31 if (c >= 'a' && c <= 'f')
32 return c - 'a' + 10;
33 if (c >= 'A' && c <= 'F')
34 return c - 'A' + 10;
35 return ~0;
38 int get_sha1_hex(const char *hex, unsigned char *sha1)
40 int i;
41 for (i = 0; i < 20; i++) {
42 unsigned int val = (hexval(hex[0]) << 4) | hexval(hex[1]);
43 if (val & ~0xff)
44 return -1;
45 *sha1++ = val;
46 hex += 2;
48 return 0;
51 int safe_create_leading_directories(char *path)
53 char *pos = path;
54 if (*pos == '/')
55 pos++;
57 while (pos) {
58 pos = strchr(pos, '/');
59 if (!pos)
60 break;
61 *pos = 0;
62 if (mkdir(path, 0777) < 0)
63 if (errno != EEXIST) {
64 *pos = '/';
65 return -1;
67 *pos++ = '/';
69 return 0;
72 char * sha1_to_hex(const unsigned char *sha1)
74 static char buffer[50];
75 static const char hex[] = "0123456789abcdef";
76 char *buf = buffer;
77 int i;
79 for (i = 0; i < 20; i++) {
80 unsigned int val = *sha1++;
81 *buf++ = hex[val >> 4];
82 *buf++ = hex[val & 0xf];
84 *buf = '\0';
86 return buffer;
89 static void fill_sha1_path(char *pathbuf, const unsigned char *sha1)
91 int i;
92 for (i = 0; i < 20; i++) {
93 static char hex[] = "0123456789abcdef";
94 unsigned int val = sha1[i];
95 char *pos = pathbuf + i*2 + (i > 0);
96 *pos++ = hex[val >> 4];
97 *pos = hex[val & 0xf];
102 * NOTE! This returns a statically allocated buffer, so you have to be
103 * careful about using it. Do a "strdup()" if you need to save the
104 * filename.
106 * Also note that this returns the location for creating. Reading
107 * SHA1 file can happen from any alternate directory listed in the
108 * DB_ENVIRONMENT environment variable if it is not found in
109 * the primary object database.
111 char *sha1_file_name(const unsigned char *sha1)
113 static char *name, *base;
115 if (!base) {
116 const char *sha1_file_directory = get_object_directory();
117 int len = strlen(sha1_file_directory);
118 base = xmalloc(len + 60);
119 memcpy(base, sha1_file_directory, len);
120 memset(base+len, 0, 60);
121 base[len] = '/';
122 base[len+3] = '/';
123 name = base + len + 1;
125 fill_sha1_path(name, sha1);
126 return base;
129 char *sha1_pack_name(const unsigned char *sha1)
131 static const char hex[] = "0123456789abcdef";
132 static char *name, *base, *buf;
133 int i;
135 if (!base) {
136 const char *sha1_file_directory = get_object_directory();
137 int len = strlen(sha1_file_directory);
138 base = xmalloc(len + 60);
139 sprintf(base, "%s/pack/pack-1234567890123456789012345678901234567890.pack", sha1_file_directory);
140 name = base + len + 11;
143 buf = name;
145 for (i = 0; i < 20; i++) {
146 unsigned int val = *sha1++;
147 *buf++ = hex[val >> 4];
148 *buf++ = hex[val & 0xf];
151 return base;
154 char *sha1_pack_index_name(const unsigned char *sha1)
156 static const char hex[] = "0123456789abcdef";
157 static char *name, *base, *buf;
158 int i;
160 if (!base) {
161 const char *sha1_file_directory = get_object_directory();
162 int len = strlen(sha1_file_directory);
163 base = xmalloc(len + 60);
164 sprintf(base, "%s/pack/pack-1234567890123456789012345678901234567890.idx", sha1_file_directory);
165 name = base + len + 11;
168 buf = name;
170 for (i = 0; i < 20; i++) {
171 unsigned int val = *sha1++;
172 *buf++ = hex[val >> 4];
173 *buf++ = hex[val & 0xf];
176 return base;
179 struct alternate_object_database *alt_odb_list;
180 static struct alternate_object_database **alt_odb_tail;
183 * Prepare alternate object database registry.
185 * The variable alt_odb_list points at the list of struct
186 * alternate_object_database. The elements on this list come from
187 * non-empty elements from colon separated ALTERNATE_DB_ENVIRONMENT
188 * environment variable, and $GIT_OBJECT_DIRECTORY/info/alternates,
189 * whose contents is similar to that environment variable but can be
190 * LF separated. Its base points at a statically allocated buffer that
191 * contains "/the/directory/corresponding/to/.git/objects/...", while
192 * its name points just after the slash at the end of ".git/objects/"
193 * in the example above, and has enough space to hold 40-byte hex
194 * SHA1, an extra slash for the first level indirection, and the
195 * terminating NUL.
197 static void link_alt_odb_entries(const char *alt, const char *ep, int sep,
198 const char *relative_base)
200 const char *cp, *last;
201 struct alternate_object_database *ent;
202 const char *objdir = get_object_directory();
203 int base_len = -1;
205 last = alt;
206 while (last < ep) {
207 cp = last;
208 if (cp < ep && *cp == '#') {
209 while (cp < ep && *cp != sep)
210 cp++;
211 last = cp + 1;
212 continue;
214 for ( ; cp < ep && *cp != sep; cp++)
216 if (last != cp) {
217 struct alternate_object_database *alt;
218 /* 43 = 40-byte + 2 '/' + terminating NUL */
219 int pfxlen = cp - last;
220 int entlen = pfxlen + 43;
222 if (*last != '/' && relative_base) {
223 /* Relative alt-odb */
224 if (base_len < 0)
225 base_len = strlen(relative_base) + 1;
226 entlen += base_len;
227 pfxlen += base_len;
229 ent = xmalloc(sizeof(*ent) + entlen);
231 if (*last != '/' && relative_base) {
232 memcpy(ent->base, relative_base, base_len - 1);
233 ent->base[base_len - 1] = '/';
234 memcpy(ent->base + base_len,
235 last, cp - last);
237 else
238 memcpy(ent->base, last, pfxlen);
239 ent->name = ent->base + pfxlen + 1;
240 ent->base[pfxlen] = ent->base[pfxlen + 3] = '/';
241 ent->base[entlen-1] = 0;
243 /* Prevent the common mistake of listing the same
244 * thing twice, or object directory itself.
246 for (alt = alt_odb_list; alt; alt = alt->next)
247 if (!memcmp(ent->base, alt->base, pfxlen))
248 goto bad;
249 if (!memcmp(ent->base, objdir, pfxlen)) {
250 bad:
251 free(ent);
253 else {
254 *alt_odb_tail = ent;
255 alt_odb_tail = &(ent->next);
256 ent->next = NULL;
259 while (cp < ep && *cp == sep)
260 cp++;
261 last = cp;
265 void prepare_alt_odb(void)
267 char path[PATH_MAX];
268 char *map;
269 int fd;
270 struct stat st;
271 char *alt;
273 alt = getenv(ALTERNATE_DB_ENVIRONMENT);
274 if (!alt) alt = "";
276 if (alt_odb_tail)
277 return;
278 alt_odb_tail = &alt_odb_list;
279 link_alt_odb_entries(alt, alt + strlen(alt), ':', NULL);
281 sprintf(path, "%s/info/alternates", get_object_directory());
282 fd = open(path, O_RDONLY);
283 if (fd < 0)
284 return;
285 if (fstat(fd, &st) || (st.st_size == 0)) {
286 close(fd);
287 return;
289 map = mmap(NULL, st.st_size, PROT_READ, MAP_PRIVATE, fd, 0);
290 close(fd);
291 if (map == MAP_FAILED)
292 return;
294 link_alt_odb_entries(map, map + st.st_size, '\n',
295 get_object_directory());
296 munmap(map, st.st_size);
299 static char *find_sha1_file(const unsigned char *sha1, struct stat *st)
301 char *name = sha1_file_name(sha1);
302 struct alternate_object_database *alt;
304 if (!stat(name, st))
305 return name;
306 prepare_alt_odb();
307 for (alt = alt_odb_list; alt; alt = alt->next) {
308 name = alt->name;
309 fill_sha1_path(name, sha1);
310 if (!stat(alt->base, st))
311 return alt->base;
313 return NULL;
316 #define PACK_MAX_SZ (1<<26)
317 static int pack_used_ctr;
318 static unsigned long pack_mapped;
319 struct packed_git *packed_git;
321 static int check_packed_git_idx(const char *path, unsigned long *idx_size_,
322 void **idx_map_)
324 SHA_CTX ctx;
325 unsigned char sha1[20];
326 void *idx_map;
327 unsigned int *index;
328 unsigned long idx_size;
329 int nr, i;
330 int fd;
331 struct stat st;
333 fd = open(path, O_RDONLY);
334 if (fd < 0)
335 return -1;
336 if (fstat(fd, &st)) {
337 close(fd);
338 return -1;
340 idx_size = st.st_size;
341 idx_map = mmap(NULL, idx_size, PROT_READ, MAP_PRIVATE, fd, 0);
342 close(fd);
343 if (idx_map == MAP_FAILED)
344 return -1;
346 index = idx_map;
347 *idx_map_ = idx_map;
348 *idx_size_ = idx_size;
350 /* check index map */
351 if (idx_size < 4*256 + 20 + 20)
352 return error("index file too small");
353 nr = 0;
354 for (i = 0; i < 256; i++) {
355 unsigned int n = ntohl(index[i]);
356 if (n < nr)
357 return error("non-monotonic index");
358 nr = n;
362 * Total size:
363 * - 256 index entries 4 bytes each
364 * - 24-byte entries * nr (20-byte sha1 + 4-byte offset)
365 * - 20-byte SHA1 of the packfile
366 * - 20-byte SHA1 file checksum
368 if (idx_size != 4*256 + nr * 24 + 20 + 20)
369 return error("wrong index file size");
372 * File checksum.
374 SHA1_Init(&ctx);
375 SHA1_Update(&ctx, idx_map, idx_size-20);
376 SHA1_Final(sha1, &ctx);
378 if (memcmp(sha1, idx_map + idx_size - 20, 20))
379 return error("index checksum mismatch");
381 return 0;
384 static int unuse_one_packed_git(void)
386 struct packed_git *p, *lru = NULL;
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;
394 if (!lru)
395 return 0;
396 munmap(lru->pack_base, lru->pack_size);
397 lru->pack_base = NULL;
398 return 1;
401 void unuse_packed_git(struct packed_git *p)
403 p->pack_use_cnt--;
406 int use_packed_git(struct packed_git *p)
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;
416 if (!p->pack_base) {
417 int fd;
418 struct stat st;
419 void *map;
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);
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;
439 /* Check if the pack file matches with the index file.
440 * this is cheap.
442 if (memcmp((char*)(p->index_base) + p->index_size - 40,
443 p->pack_base + p->pack_size - 20, 20)) {
445 die("packfile %s does not match index.", p->pack_name);
448 p->pack_last_used = pack_used_ctr++;
449 p->pack_use_cnt++;
450 return 0;
453 struct packed_git *add_packed_git(char *path, int path_len, int local)
455 struct stat st;
456 struct packed_git *p;
457 unsigned long idx_size;
458 void *idx_map;
459 unsigned char sha1[20];
461 if (check_packed_git_idx(path, &idx_size, &idx_map))
462 return NULL;
464 /* do we have a corresponding .pack file? */
465 strcpy(path + path_len - 4, ".pack");
466 if (stat(path, &st) || !S_ISREG(st.st_mode)) {
467 munmap(idx_map, idx_size);
468 return NULL;
470 /* ok, it looks sane as far as we can check without
471 * actually mapping the pack file.
473 p = xmalloc(sizeof(*p) + path_len + 2);
474 strcpy(p->pack_name, path);
475 p->index_size = idx_size;
476 p->pack_size = st.st_size;
477 p->index_base = idx_map;
478 p->next = NULL;
479 p->pack_base = NULL;
480 p->pack_last_used = 0;
481 p->pack_use_cnt = 0;
482 p->pack_local = local;
483 if ((path_len > 44) && !get_sha1_hex(path + path_len - 44, sha1))
484 memcpy(p->sha1, sha1, 20);
485 return p;
488 struct packed_git *parse_pack_index(unsigned char *sha1)
490 char *path = sha1_pack_index_name(sha1);
491 return parse_pack_index_file(sha1, path);
494 struct packed_git *parse_pack_index_file(const unsigned char *sha1, char *idx_path)
496 struct packed_git *p;
497 unsigned long idx_size;
498 void *idx_map;
499 char *path;
501 if (check_packed_git_idx(idx_path, &idx_size, &idx_map))
502 return NULL;
504 path = sha1_pack_name(sha1);
506 p = xmalloc(sizeof(*p) + strlen(path) + 2);
507 strcpy(p->pack_name, path);
508 p->index_size = idx_size;
509 p->pack_size = 0;
510 p->index_base = idx_map;
511 p->next = NULL;
512 p->pack_base = NULL;
513 p->pack_last_used = 0;
514 p->pack_use_cnt = 0;
515 memcpy(p->sha1, sha1, 20);
516 return p;
519 void install_packed_git(struct packed_git *pack)
521 pack->next = packed_git;
522 packed_git = pack;
525 static void prepare_packed_git_one(char *objdir, int local)
527 char path[PATH_MAX];
528 int len;
529 DIR *dir;
530 struct dirent *de;
532 sprintf(path, "%s/pack", objdir);
533 len = strlen(path);
534 dir = opendir(path);
535 if (!dir)
536 return;
537 path[len++] = '/';
538 while ((de = readdir(dir)) != NULL) {
539 int namelen = strlen(de->d_name);
540 struct packed_git *p;
542 if (strcmp(de->d_name + namelen - 4, ".idx"))
543 continue;
545 /* we have .idx. Is it a file we can map? */
546 strcpy(path + len, de->d_name);
547 p = add_packed_git(path, len + namelen, local);
548 if (!p)
549 continue;
550 p->next = packed_git;
551 packed_git = p;
553 closedir(dir);
556 void prepare_packed_git(void)
558 static int run_once = 0;
559 struct alternate_object_database *alt;
561 if (run_once)
562 return;
563 prepare_packed_git_one(get_object_directory(), 1);
564 prepare_alt_odb();
565 for (alt = alt_odb_list; alt; alt = alt->next) {
566 alt->name[-1] = 0;
567 prepare_packed_git_one(alt->base, 0);
568 alt->name[-1] = '/';
570 run_once = 1;
573 int check_sha1_signature(const unsigned char *sha1, void *map, unsigned long size, const char *type)
575 char header[100];
576 unsigned char real_sha1[20];
577 SHA_CTX c;
579 SHA1_Init(&c);
580 SHA1_Update(&c, header, 1+sprintf(header, "%s %lu", type, size));
581 SHA1_Update(&c, map, size);
582 SHA1_Final(real_sha1, &c);
583 return memcmp(sha1, real_sha1, 20) ? -1 : 0;
586 static void *map_sha1_file_internal(const unsigned char *sha1,
587 unsigned long *size)
589 struct stat st;
590 void *map;
591 int fd;
592 char *filename = find_sha1_file(sha1, &st);
594 if (!filename) {
595 return NULL;
598 fd = open(filename, O_RDONLY | sha1_file_open_flag);
599 if (fd < 0) {
600 /* See if it works without O_NOATIME */
601 switch (sha1_file_open_flag) {
602 default:
603 fd = open(filename, O_RDONLY);
604 if (fd >= 0)
605 break;
606 /* Fallthrough */
607 case 0:
608 return NULL;
611 /* If it failed once, it will probably fail again.
612 * Stop using O_NOATIME
614 sha1_file_open_flag = 0;
616 map = mmap(NULL, st.st_size, PROT_READ, MAP_PRIVATE, fd, 0);
617 close(fd);
618 if (map == MAP_FAILED)
619 return NULL;
620 *size = st.st_size;
621 return map;
624 int unpack_sha1_header(z_stream *stream, void *map, unsigned long mapsize, void *buffer, unsigned long size)
626 /* Get the data stream */
627 memset(stream, 0, sizeof(*stream));
628 stream->next_in = map;
629 stream->avail_in = mapsize;
630 stream->next_out = buffer;
631 stream->avail_out = size;
633 inflateInit(stream);
634 return inflate(stream, 0);
637 static void *unpack_sha1_rest(z_stream *stream, void *buffer, unsigned long size)
639 int bytes = strlen(buffer) + 1;
640 unsigned char *buf = xmalloc(1+size);
642 memcpy(buf, buffer + bytes, stream->total_out - bytes);
643 bytes = stream->total_out - bytes;
644 if (bytes < size) {
645 stream->next_out = buf + bytes;
646 stream->avail_out = size - bytes;
647 while (inflate(stream, Z_FINISH) == Z_OK)
648 /* nothing */;
650 buf[size] = 0;
651 inflateEnd(stream);
652 return buf;
656 * We used to just use "sscanf()", but that's actually way
657 * too permissive for what we want to check. So do an anal
658 * object header parse by hand.
660 int parse_sha1_header(char *hdr, char *type, unsigned long *sizep)
662 int i;
663 unsigned long size;
666 * The type can be at most ten bytes (including the
667 * terminating '\0' that we add), and is followed by
668 * a space.
670 i = 10;
671 for (;;) {
672 char c = *hdr++;
673 if (c == ' ')
674 break;
675 if (!--i)
676 return -1;
677 *type++ = c;
679 *type = 0;
682 * The length must follow immediately, and be in canonical
683 * decimal format (ie "010" is not valid).
685 size = *hdr++ - '0';
686 if (size > 9)
687 return -1;
688 if (size) {
689 for (;;) {
690 unsigned long c = *hdr - '0';
691 if (c > 9)
692 break;
693 hdr++;
694 size = size * 10 + c;
697 *sizep = size;
700 * The length must be followed by a zero byte
702 return *hdr ? -1 : 0;
705 void * unpack_sha1_file(void *map, unsigned long mapsize, char *type, unsigned long *size)
707 int ret;
708 z_stream stream;
709 char hdr[8192];
711 ret = unpack_sha1_header(&stream, map, mapsize, hdr, sizeof(hdr));
712 if (ret < Z_OK || parse_sha1_header(hdr, type, size) < 0)
713 return NULL;
715 return unpack_sha1_rest(&stream, hdr, *size);
718 /* forward declaration for a mutually recursive function */
719 static int packed_object_info(struct pack_entry *entry,
720 char *type, unsigned long *sizep);
722 static int packed_delta_info(unsigned char *base_sha1,
723 unsigned long delta_size,
724 unsigned long left,
725 char *type,
726 unsigned long *sizep,
727 struct packed_git *p)
729 struct pack_entry base_ent;
731 if (left < 20)
732 die("truncated pack file");
734 /* The base entry _must_ be in the same pack */
735 if (!find_pack_entry_one(base_sha1, &base_ent, p))
736 die("failed to find delta-pack base object %s",
737 sha1_to_hex(base_sha1));
739 /* We choose to only get the type of the base object and
740 * ignore potentially corrupt pack file that expects the delta
741 * based on a base with a wrong size. This saves tons of
742 * inflate() calls.
745 if (packed_object_info(&base_ent, type, NULL))
746 die("cannot get info for delta-pack base");
748 if (sizep) {
749 const unsigned char *data;
750 unsigned char delta_head[64];
751 unsigned long result_size;
752 z_stream stream;
753 int st;
755 memset(&stream, 0, sizeof(stream));
757 data = stream.next_in = base_sha1 + 20;
758 stream.avail_in = left - 20;
759 stream.next_out = delta_head;
760 stream.avail_out = sizeof(delta_head);
762 inflateInit(&stream);
763 st = inflate(&stream, Z_FINISH);
764 inflateEnd(&stream);
765 if ((st != Z_STREAM_END) &&
766 stream.total_out != sizeof(delta_head))
767 die("delta data unpack-initial failed");
769 /* Examine the initial part of the delta to figure out
770 * the result size.
772 data = delta_head;
773 get_delta_hdr_size(&data); /* ignore base size */
775 /* Read the result size */
776 result_size = get_delta_hdr_size(&data);
777 *sizep = result_size;
779 return 0;
782 static unsigned long unpack_object_header(struct packed_git *p, unsigned long offset,
783 enum object_type *type, unsigned long *sizep)
785 unsigned shift;
786 unsigned char *pack, c;
787 unsigned long size;
789 if (offset >= p->pack_size)
790 die("object offset outside of pack file");
792 pack = p->pack_base + offset;
793 c = *pack++;
794 offset++;
795 *type = (c >> 4) & 7;
796 size = c & 15;
797 shift = 4;
798 while (c & 0x80) {
799 if (offset >= p->pack_size)
800 die("object offset outside of pack file");
801 c = *pack++;
802 offset++;
803 size += (c & 0x7f) << shift;
804 shift += 7;
806 *sizep = size;
807 return offset;
810 void packed_object_info_detail(struct pack_entry *e,
811 char *type,
812 unsigned long *size,
813 unsigned long *store_size,
814 int *delta_chain_length,
815 unsigned char *base_sha1)
817 struct packed_git *p = e->p;
818 unsigned long offset, left;
819 unsigned char *pack;
820 enum object_type kind;
822 offset = unpack_object_header(p, e->offset, &kind, size);
823 pack = p->pack_base + offset;
824 left = p->pack_size - offset;
825 if (kind != OBJ_DELTA)
826 *delta_chain_length = 0;
827 else {
828 int chain_length = 0;
829 memcpy(base_sha1, pack, 20);
830 do {
831 struct pack_entry base_ent;
832 unsigned long junk;
834 find_pack_entry_one(pack, &base_ent, p);
835 offset = unpack_object_header(p, base_ent.offset,
836 &kind, &junk);
837 pack = p->pack_base + offset;
838 chain_length++;
839 } while (kind == OBJ_DELTA);
840 *delta_chain_length = chain_length;
842 switch (kind) {
843 case OBJ_COMMIT:
844 strcpy(type, "commit");
845 break;
846 case OBJ_TREE:
847 strcpy(type, "tree");
848 break;
849 case OBJ_BLOB:
850 strcpy(type, "blob");
851 break;
852 case OBJ_TAG:
853 strcpy(type, "tag");
854 break;
855 default:
856 die("corrupted pack file %s containing object of kind %d",
857 p->pack_name, kind);
859 *store_size = 0; /* notyet */
862 static int packed_object_info(struct pack_entry *entry,
863 char *type, unsigned long *sizep)
865 struct packed_git *p = entry->p;
866 unsigned long offset, size, left;
867 unsigned char *pack;
868 enum object_type kind;
869 int retval;
871 if (use_packed_git(p))
872 die("cannot map packed file");
874 offset = unpack_object_header(p, entry->offset, &kind, &size);
875 pack = p->pack_base + offset;
876 left = p->pack_size - offset;
878 switch (kind) {
879 case OBJ_DELTA:
880 retval = packed_delta_info(pack, size, left, type, sizep, p);
881 unuse_packed_git(p);
882 return retval;
883 case OBJ_COMMIT:
884 strcpy(type, "commit");
885 break;
886 case OBJ_TREE:
887 strcpy(type, "tree");
888 break;
889 case OBJ_BLOB:
890 strcpy(type, "blob");
891 break;
892 case OBJ_TAG:
893 strcpy(type, "tag");
894 break;
895 default:
896 die("corrupted pack file %s containing object of kind %d",
897 p->pack_name, kind);
899 if (sizep)
900 *sizep = size;
901 unuse_packed_git(p);
902 return 0;
905 /* forward declaration for a mutually recursive function */
906 static void *unpack_entry(struct pack_entry *, char *, unsigned long *);
908 static void *unpack_delta_entry(unsigned char *base_sha1,
909 unsigned long delta_size,
910 unsigned long left,
911 char *type,
912 unsigned long *sizep,
913 struct packed_git *p)
915 struct pack_entry base_ent;
916 void *data, *delta_data, *result, *base;
917 unsigned long data_size, result_size, base_size;
918 z_stream stream;
919 int st;
921 if (left < 20)
922 die("truncated pack file");
923 data = base_sha1 + 20;
924 data_size = left - 20;
925 delta_data = xmalloc(delta_size);
927 memset(&stream, 0, sizeof(stream));
929 stream.next_in = data;
930 stream.avail_in = data_size;
931 stream.next_out = delta_data;
932 stream.avail_out = delta_size;
934 inflateInit(&stream);
935 st = inflate(&stream, Z_FINISH);
936 inflateEnd(&stream);
937 if ((st != Z_STREAM_END) || stream.total_out != delta_size)
938 die("delta data unpack failed");
940 /* The base entry _must_ be in the same pack */
941 if (!find_pack_entry_one(base_sha1, &base_ent, p))
942 die("failed to find delta-pack base object %s",
943 sha1_to_hex(base_sha1));
944 base = unpack_entry_gently(&base_ent, type, &base_size);
945 if (!base)
946 die("failed to read delta-pack base object %s",
947 sha1_to_hex(base_sha1));
948 result = patch_delta(base, base_size,
949 delta_data, delta_size,
950 &result_size);
951 if (!result)
952 die("failed to apply delta");
953 free(delta_data);
954 free(base);
955 *sizep = result_size;
956 return result;
959 static void *unpack_non_delta_entry(unsigned char *data,
960 unsigned long size,
961 unsigned long left)
963 int st;
964 z_stream stream;
965 unsigned char *buffer;
967 buffer = xmalloc(size + 1);
968 buffer[size] = 0;
969 memset(&stream, 0, sizeof(stream));
970 stream.next_in = data;
971 stream.avail_in = left;
972 stream.next_out = buffer;
973 stream.avail_out = size;
975 inflateInit(&stream);
976 st = inflate(&stream, Z_FINISH);
977 inflateEnd(&stream);
978 if ((st != Z_STREAM_END) || stream.total_out != size) {
979 free(buffer);
980 return NULL;
983 return buffer;
986 static void *unpack_entry(struct pack_entry *entry,
987 char *type, unsigned long *sizep)
989 struct packed_git *p = entry->p;
990 void *retval;
992 if (use_packed_git(p))
993 die("cannot map packed file");
994 retval = unpack_entry_gently(entry, type, sizep);
995 unuse_packed_git(p);
996 if (!retval)
997 die("corrupted pack file %s", p->pack_name);
998 return retval;
1001 /* The caller is responsible for use_packed_git()/unuse_packed_git() pair */
1002 void *unpack_entry_gently(struct pack_entry *entry,
1003 char *type, unsigned long *sizep)
1005 struct packed_git *p = entry->p;
1006 unsigned long offset, size, left;
1007 unsigned char *pack;
1008 enum object_type kind;
1009 void *retval;
1011 offset = unpack_object_header(p, entry->offset, &kind, &size);
1012 pack = p->pack_base + offset;
1013 left = p->pack_size - offset;
1014 switch (kind) {
1015 case OBJ_DELTA:
1016 retval = unpack_delta_entry(pack, size, left, type, sizep, p);
1017 return retval;
1018 case OBJ_COMMIT:
1019 strcpy(type, "commit");
1020 break;
1021 case OBJ_TREE:
1022 strcpy(type, "tree");
1023 break;
1024 case OBJ_BLOB:
1025 strcpy(type, "blob");
1026 break;
1027 case OBJ_TAG:
1028 strcpy(type, "tag");
1029 break;
1030 default:
1031 return NULL;
1033 *sizep = size;
1034 retval = unpack_non_delta_entry(pack, size, left);
1035 return retval;
1038 int num_packed_objects(const struct packed_git *p)
1040 /* See check_packed_git_idx() */
1041 return (p->index_size - 20 - 20 - 4*256) / 24;
1044 int nth_packed_object_sha1(const struct packed_git *p, int n,
1045 unsigned char* sha1)
1047 void *index = p->index_base + 256;
1048 if (n < 0 || num_packed_objects(p) <= n)
1049 return -1;
1050 memcpy(sha1, (index + 24 * n + 4), 20);
1051 return 0;
1054 int find_pack_entry_one(const unsigned char *sha1,
1055 struct pack_entry *e, struct packed_git *p)
1057 unsigned int *level1_ofs = p->index_base;
1058 int hi = ntohl(level1_ofs[*sha1]);
1059 int lo = ((*sha1 == 0x0) ? 0 : ntohl(level1_ofs[*sha1 - 1]));
1060 void *index = p->index_base + 256;
1062 do {
1063 int mi = (lo + hi) / 2;
1064 int cmp = memcmp(index + 24 * mi + 4, sha1, 20);
1065 if (!cmp) {
1066 e->offset = ntohl(*((int*)(index + 24 * mi)));
1067 memcpy(e->sha1, sha1, 20);
1068 e->p = p;
1069 return 1;
1071 if (cmp > 0)
1072 hi = mi;
1073 else
1074 lo = mi+1;
1075 } while (lo < hi);
1076 return 0;
1079 static int find_pack_entry(const unsigned char *sha1, struct pack_entry *e)
1081 struct packed_git *p;
1082 prepare_packed_git();
1084 for (p = packed_git; p; p = p->next) {
1085 if (find_pack_entry_one(sha1, e, p))
1086 return 1;
1088 return 0;
1091 struct packed_git *find_sha1_pack(const unsigned char *sha1,
1092 struct packed_git *packs)
1094 struct packed_git *p;
1095 struct pack_entry e;
1097 for (p = packs; p; p = p->next) {
1098 if (find_pack_entry_one(sha1, &e, p))
1099 return p;
1101 return NULL;
1105 int sha1_object_info(const unsigned char *sha1, char *type, unsigned long *sizep)
1107 int status;
1108 unsigned long mapsize, size;
1109 void *map;
1110 z_stream stream;
1111 char hdr[128];
1113 map = map_sha1_file_internal(sha1, &mapsize);
1114 if (!map) {
1115 struct pack_entry e;
1117 if (!find_pack_entry(sha1, &e))
1118 return error("unable to find %s", sha1_to_hex(sha1));
1119 return packed_object_info(&e, type, sizep);
1121 if (unpack_sha1_header(&stream, map, mapsize, hdr, sizeof(hdr)) < 0)
1122 status = error("unable to unpack %s header",
1123 sha1_to_hex(sha1));
1124 if (parse_sha1_header(hdr, type, &size) < 0)
1125 status = error("unable to parse %s header", sha1_to_hex(sha1));
1126 else {
1127 status = 0;
1128 if (sizep)
1129 *sizep = size;
1131 inflateEnd(&stream);
1132 munmap(map, mapsize);
1133 return status;
1136 static void *read_packed_sha1(const unsigned char *sha1, char *type, unsigned long *size)
1138 struct pack_entry e;
1140 if (!find_pack_entry(sha1, &e)) {
1141 error("cannot read sha1_file for %s", sha1_to_hex(sha1));
1142 return NULL;
1144 return unpack_entry(&e, type, size);
1147 void * read_sha1_file(const unsigned char *sha1, char *type, unsigned long *size)
1149 unsigned long mapsize;
1150 void *map, *buf;
1151 struct pack_entry e;
1153 if (find_pack_entry(sha1, &e))
1154 return read_packed_sha1(sha1, type, size);
1155 map = map_sha1_file_internal(sha1, &mapsize);
1156 if (map) {
1157 buf = unpack_sha1_file(map, mapsize, type, size);
1158 munmap(map, mapsize);
1159 return buf;
1161 return NULL;
1164 void *read_object_with_reference(const unsigned char *sha1,
1165 const char *required_type,
1166 unsigned long *size,
1167 unsigned char *actual_sha1_return)
1169 char type[20];
1170 void *buffer;
1171 unsigned long isize;
1172 unsigned char actual_sha1[20];
1174 memcpy(actual_sha1, sha1, 20);
1175 while (1) {
1176 int ref_length = -1;
1177 const char *ref_type = NULL;
1179 buffer = read_sha1_file(actual_sha1, type, &isize);
1180 if (!buffer)
1181 return NULL;
1182 if (!strcmp(type, required_type)) {
1183 *size = isize;
1184 if (actual_sha1_return)
1185 memcpy(actual_sha1_return, actual_sha1, 20);
1186 return buffer;
1188 /* Handle references */
1189 else if (!strcmp(type, "commit"))
1190 ref_type = "tree ";
1191 else if (!strcmp(type, "tag"))
1192 ref_type = "object ";
1193 else {
1194 free(buffer);
1195 return NULL;
1197 ref_length = strlen(ref_type);
1199 if (memcmp(buffer, ref_type, ref_length) ||
1200 get_sha1_hex(buffer + ref_length, actual_sha1)) {
1201 free(buffer);
1202 return NULL;
1204 free(buffer);
1205 /* Now we have the ID of the referred-to object in
1206 * actual_sha1. Check again. */
1210 char *write_sha1_file_prepare(void *buf,
1211 unsigned long len,
1212 const char *type,
1213 unsigned char *sha1,
1214 unsigned char *hdr,
1215 int *hdrlen)
1217 SHA_CTX c;
1219 /* Generate the header */
1220 *hdrlen = sprintf((char *)hdr, "%s %lu", type, len)+1;
1222 /* Sha1.. */
1223 SHA1_Init(&c);
1224 SHA1_Update(&c, hdr, *hdrlen);
1225 SHA1_Update(&c, buf, len);
1226 SHA1_Final(sha1, &c);
1228 return sha1_file_name(sha1);
1232 * Link the tempfile to the final place, possibly creating the
1233 * last directory level as you do so.
1235 * Returns the errno on failure, 0 on success.
1237 static int link_temp_to_file(const char *tmpfile, char *filename)
1239 int ret;
1241 if (!link(tmpfile, filename))
1242 return 0;
1245 * Try to mkdir the last path component if that failed
1246 * with an ENOENT.
1248 * Re-try the "link()" regardless of whether the mkdir
1249 * succeeds, since a race might mean that somebody
1250 * else succeeded.
1252 ret = errno;
1253 if (ret == ENOENT) {
1254 char *dir = strrchr(filename, '/');
1255 if (dir) {
1256 *dir = 0;
1257 mkdir(filename, 0777);
1258 *dir = '/';
1259 if (!link(tmpfile, filename))
1260 return 0;
1261 ret = errno;
1264 return ret;
1268 * Move the just written object into its final resting place
1270 int move_temp_to_file(const char *tmpfile, char *filename)
1272 int ret = link_temp_to_file(tmpfile, filename);
1275 * Coda hack - coda doesn't like cross-directory links,
1276 * so we fall back to a rename, which will mean that it
1277 * won't be able to check collisions, but that's not a
1278 * big deal.
1280 * The same holds for FAT formatted media.
1282 * When this succeeds, we just return 0. We have nothing
1283 * left to unlink.
1285 if (ret && ret != EEXIST) {
1286 if (!rename(tmpfile, filename))
1287 return 0;
1288 ret = errno;
1290 unlink(tmpfile);
1291 if (ret) {
1292 if (ret != EEXIST) {
1293 fprintf(stderr, "unable to write sha1 filename %s: %s\n", filename, strerror(ret));
1294 return -1;
1296 /* FIXME!!! Collision check here ? */
1299 return 0;
1302 int write_sha1_file(void *buf, unsigned long len, const char *type, unsigned char *returnsha1)
1304 int size;
1305 unsigned char *compressed;
1306 z_stream stream;
1307 unsigned char sha1[20];
1308 char *filename;
1309 static char tmpfile[PATH_MAX];
1310 unsigned char hdr[50];
1311 int fd, hdrlen;
1313 /* Normally if we have it in the pack then we do not bother writing
1314 * it out into .git/objects/??/?{38} file.
1316 filename = write_sha1_file_prepare(buf, len, type, sha1, hdr, &hdrlen);
1317 if (returnsha1)
1318 memcpy(returnsha1, sha1, 20);
1319 if (has_sha1_file(sha1))
1320 return 0;
1321 fd = open(filename, O_RDONLY);
1322 if (fd >= 0) {
1324 * FIXME!!! We might do collision checking here, but we'd
1325 * need to uncompress the old file and check it. Later.
1327 close(fd);
1328 return 0;
1331 if (errno != ENOENT) {
1332 fprintf(stderr, "sha1 file %s: %s\n", filename, strerror(errno));
1333 return -1;
1336 snprintf(tmpfile, sizeof(tmpfile), "%s/obj_XXXXXX", get_object_directory());
1338 fd = mkstemp(tmpfile);
1339 if (fd < 0) {
1340 fprintf(stderr, "unable to create temporary sha1 filename %s: %s\n", tmpfile, strerror(errno));
1341 return -1;
1344 /* Set it up */
1345 memset(&stream, 0, sizeof(stream));
1346 deflateInit(&stream, Z_BEST_COMPRESSION);
1347 size = deflateBound(&stream, len+hdrlen);
1348 compressed = xmalloc(size);
1350 /* Compress it */
1351 stream.next_out = compressed;
1352 stream.avail_out = size;
1354 /* First header.. */
1355 stream.next_in = hdr;
1356 stream.avail_in = hdrlen;
1357 while (deflate(&stream, 0) == Z_OK)
1358 /* nothing */;
1360 /* Then the data itself.. */
1361 stream.next_in = buf;
1362 stream.avail_in = len;
1363 while (deflate(&stream, Z_FINISH) == Z_OK)
1364 /* nothing */;
1365 deflateEnd(&stream);
1366 size = stream.total_out;
1368 if (write(fd, compressed, size) != size)
1369 die("unable to write file");
1370 fchmod(fd, 0444);
1371 close(fd);
1372 free(compressed);
1374 return move_temp_to_file(tmpfile, filename);
1377 int write_sha1_to_fd(int fd, const unsigned char *sha1)
1379 ssize_t size;
1380 unsigned long objsize;
1381 int posn = 0;
1382 void *map = map_sha1_file_internal(sha1, &objsize);
1383 void *buf = map;
1384 void *temp_obj = NULL;
1385 z_stream stream;
1387 if (!buf) {
1388 unsigned char *unpacked;
1389 unsigned long len;
1390 char type[20];
1391 char hdr[50];
1392 int hdrlen;
1393 // need to unpack and recompress it by itself
1394 unpacked = read_packed_sha1(sha1, type, &len);
1396 hdrlen = sprintf(hdr, "%s %lu", type, len) + 1;
1398 /* Set it up */
1399 memset(&stream, 0, sizeof(stream));
1400 deflateInit(&stream, Z_BEST_COMPRESSION);
1401 size = deflateBound(&stream, len + hdrlen);
1402 temp_obj = buf = xmalloc(size);
1404 /* Compress it */
1405 stream.next_out = buf;
1406 stream.avail_out = size;
1408 /* First header.. */
1409 stream.next_in = (void *)hdr;
1410 stream.avail_in = hdrlen;
1411 while (deflate(&stream, 0) == Z_OK)
1412 /* nothing */;
1414 /* Then the data itself.. */
1415 stream.next_in = unpacked;
1416 stream.avail_in = len;
1417 while (deflate(&stream, Z_FINISH) == Z_OK)
1418 /* nothing */;
1419 deflateEnd(&stream);
1420 free(unpacked);
1422 objsize = stream.total_out;
1425 do {
1426 size = write(fd, buf + posn, objsize - posn);
1427 if (size <= 0) {
1428 if (!size) {
1429 fprintf(stderr, "write closed\n");
1430 } else {
1431 perror("write ");
1433 return -1;
1435 posn += size;
1436 } while (posn < objsize);
1438 if (map)
1439 munmap(map, objsize);
1440 if (temp_obj)
1441 free(temp_obj);
1443 return 0;
1446 int write_sha1_from_fd(const unsigned char *sha1, int fd, char *buffer,
1447 size_t bufsize, size_t *bufposn)
1449 char tmpfile[PATH_MAX];
1450 int local;
1451 z_stream stream;
1452 unsigned char real_sha1[20];
1453 unsigned char discard[4096];
1454 int ret;
1455 SHA_CTX c;
1457 snprintf(tmpfile, sizeof(tmpfile), "%s/obj_XXXXXX", get_object_directory());
1459 local = mkstemp(tmpfile);
1460 if (local < 0)
1461 return error("Couldn't open %s for %s\n", tmpfile, sha1_to_hex(sha1));
1463 memset(&stream, 0, sizeof(stream));
1465 inflateInit(&stream);
1467 SHA1_Init(&c);
1469 do {
1470 ssize_t size;
1471 if (*bufposn) {
1472 stream.avail_in = *bufposn;
1473 stream.next_in = (unsigned char *) buffer;
1474 do {
1475 stream.next_out = discard;
1476 stream.avail_out = sizeof(discard);
1477 ret = inflate(&stream, Z_SYNC_FLUSH);
1478 SHA1_Update(&c, discard, sizeof(discard) -
1479 stream.avail_out);
1480 } while (stream.avail_in && ret == Z_OK);
1481 write(local, buffer, *bufposn - stream.avail_in);
1482 memmove(buffer, buffer + *bufposn - stream.avail_in,
1483 stream.avail_in);
1484 *bufposn = stream.avail_in;
1485 if (ret != Z_OK)
1486 break;
1488 size = read(fd, buffer + *bufposn, bufsize - *bufposn);
1489 if (size <= 0) {
1490 close(local);
1491 unlink(tmpfile);
1492 if (!size)
1493 return error("Connection closed?");
1494 perror("Reading from connection");
1495 return -1;
1497 *bufposn += size;
1498 } while (1);
1499 inflateEnd(&stream);
1501 close(local);
1502 SHA1_Final(real_sha1, &c);
1503 if (ret != Z_STREAM_END) {
1504 unlink(tmpfile);
1505 return error("File %s corrupted", sha1_to_hex(sha1));
1507 if (memcmp(sha1, real_sha1, 20)) {
1508 unlink(tmpfile);
1509 return error("File %s has bad hash\n", sha1_to_hex(sha1));
1512 return move_temp_to_file(tmpfile, sha1_file_name(sha1));
1515 int has_pack_index(const unsigned char *sha1)
1517 struct stat st;
1518 if (stat(sha1_pack_index_name(sha1), &st))
1519 return 0;
1520 return 1;
1523 int has_pack_file(const unsigned char *sha1)
1525 struct stat st;
1526 if (stat(sha1_pack_name(sha1), &st))
1527 return 0;
1528 return 1;
1531 int has_sha1_pack(const unsigned char *sha1)
1533 struct pack_entry e;
1534 return find_pack_entry(sha1, &e);
1537 int has_sha1_file(const unsigned char *sha1)
1539 struct stat st;
1540 struct pack_entry e;
1542 if (find_pack_entry(sha1, &e))
1543 return 1;
1544 return find_sha1_file(sha1, &st) ? 1 : 0;
1547 int index_pipe(unsigned char *sha1, int fd, const char *type, int write_object)
1549 unsigned long size = 4096;
1550 char *buf = malloc(size);
1551 int iret, ret;
1552 unsigned long off = 0;
1553 unsigned char hdr[50];
1554 int hdrlen;
1555 do {
1556 iret = read(fd, buf + off, size - off);
1557 if (iret > 0) {
1558 off += iret;
1559 if (off == size) {
1560 size *= 2;
1561 buf = realloc(buf, size);
1564 } while (iret > 0);
1565 if (iret < 0) {
1566 free(buf);
1567 return -1;
1569 if (!type)
1570 type = "blob";
1571 if (write_object)
1572 ret = write_sha1_file(buf, off, type, sha1);
1573 else {
1574 write_sha1_file_prepare(buf, off, type, sha1, hdr, &hdrlen);
1575 ret = 0;
1577 free(buf);
1578 return ret;
1581 int index_fd(unsigned char *sha1, int fd, struct stat *st, int write_object, const char *type)
1583 unsigned long size = st->st_size;
1584 void *buf;
1585 int ret;
1586 unsigned char hdr[50];
1587 int hdrlen;
1589 buf = "";
1590 if (size)
1591 buf = mmap(NULL, size, PROT_READ, MAP_PRIVATE, fd, 0);
1592 close(fd);
1593 if (buf == MAP_FAILED)
1594 return -1;
1596 if (!type)
1597 type = "blob";
1598 if (write_object)
1599 ret = write_sha1_file(buf, size, type, sha1);
1600 else {
1601 write_sha1_file_prepare(buf, size, type, sha1, hdr, &hdrlen);
1602 ret = 0;
1604 if (size)
1605 munmap(buf, size);
1606 return ret;
1609 int index_path(unsigned char *sha1, const char *path, struct stat *st, int write_object)
1611 int fd;
1612 char *target;
1614 switch (st->st_mode & S_IFMT) {
1615 case S_IFREG:
1616 fd = open(path, O_RDONLY);
1617 if (fd < 0)
1618 return error("open(\"%s\"): %s", path,
1619 strerror(errno));
1620 if (index_fd(sha1, fd, st, write_object, NULL) < 0)
1621 return error("%s: failed to insert into database",
1622 path);
1623 break;
1624 case S_IFLNK:
1625 target = xmalloc(st->st_size+1);
1626 if (readlink(path, target, st->st_size+1) != st->st_size) {
1627 char *errstr = strerror(errno);
1628 free(target);
1629 return error("readlink(\"%s\"): %s", path,
1630 errstr);
1632 if (!write_object) {
1633 unsigned char hdr[50];
1634 int hdrlen;
1635 write_sha1_file_prepare(target, st->st_size, "blob",
1636 sha1, hdr, &hdrlen);
1637 } else if (write_sha1_file(target, st->st_size, "blob", sha1))
1638 return error("%s: failed to insert into database",
1639 path);
1640 free(target);
1641 break;
1642 default:
1643 return error("%s: unsupported file type", path);
1645 return 0;