Fix typo in recent Makefile cleanup (again).
[git/gitweb.git] / sha1_file.c
blob8d3a65f5f8ff59041cf1d4e07d0e43c9402e0c38
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 static unsigned int sha1_file_open_flag = O_NOATIME;
25 static unsigned hexval(char c)
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;
36 int get_sha1_hex(const char *hex, unsigned char *sha1)
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;
46 return 0;
49 static int get_sha1_file(const char *path, unsigned char *result)
51 char buffer[60];
52 int fd = open(path, O_RDONLY);
53 int len;
55 if (fd < 0)
56 return -1;
57 len = read(fd, buffer, sizeof(buffer));
58 close(fd);
59 if (len < 40)
60 return -1;
61 return get_sha1_hex(buffer, result);
64 static char *git_dir, *git_object_dir, *git_index_file, *git_refs_dir,
65 *git_graft_file;
66 static void setup_git_env(void)
68 git_dir = gitenv(GIT_DIR_ENVIRONMENT);
69 if (!git_dir)
70 git_dir = DEFAULT_GIT_DIR_ENVIRONMENT;
71 git_object_dir = gitenv(DB_ENVIRONMENT);
72 if (!git_object_dir) {
73 git_object_dir = xmalloc(strlen(git_dir) + 9);
74 sprintf(git_object_dir, "%s/objects", git_dir);
76 git_refs_dir = xmalloc(strlen(git_dir) + 6);
77 sprintf(git_refs_dir, "%s/refs", git_dir);
78 git_index_file = gitenv(INDEX_ENVIRONMENT);
79 if (!git_index_file) {
80 git_index_file = xmalloc(strlen(git_dir) + 7);
81 sprintf(git_index_file, "%s/index", git_dir);
83 git_graft_file = gitenv(GRAFT_ENVIRONMENT);
84 if (!git_graft_file)
85 git_graft_file = strdup(git_path("info/grafts"));
88 char *get_object_directory(void)
90 if (!git_object_dir)
91 setup_git_env();
92 return git_object_dir;
95 char *get_refs_directory(void)
97 if (!git_refs_dir)
98 setup_git_env();
99 return git_refs_dir;
102 char *get_index_file(void)
104 if (!git_index_file)
105 setup_git_env();
106 return git_index_file;
109 char *get_graft_file(void)
111 if (!git_graft_file)
112 setup_git_env();
113 return git_graft_file;
116 int safe_create_leading_directories(char *path)
118 char *pos = path;
120 while (pos) {
121 pos = strchr(pos, '/');
122 if (!pos)
123 break;
124 *pos = 0;
125 if (mkdir(path, 0777) < 0)
126 if (errno != EEXIST) {
127 *pos = '/';
128 return -1;
130 *pos++ = '/';
132 return 0;
135 int get_sha1(const char *str, unsigned char *sha1)
137 static const char *prefix[] = {
139 "refs",
140 "refs/tags",
141 "refs/heads",
142 "refs/snap",
143 NULL
145 const char **p;
147 if (!get_sha1_hex(str, sha1))
148 return 0;
150 for (p = prefix; *p; p++) {
151 char * pathname = git_path("%s/%s", *p, str);
152 if (!get_sha1_file(pathname, sha1))
153 return 0;
156 return -1;
159 char * sha1_to_hex(const unsigned char *sha1)
161 static char buffer[50];
162 static const char hex[] = "0123456789abcdef";
163 char *buf = buffer;
164 int i;
166 for (i = 0; i < 20; i++) {
167 unsigned int val = *sha1++;
168 *buf++ = hex[val >> 4];
169 *buf++ = hex[val & 0xf];
171 return buffer;
174 static void fill_sha1_path(char *pathbuf, const unsigned char *sha1)
176 int i;
177 for (i = 0; i < 20; i++) {
178 static char hex[] = "0123456789abcdef";
179 unsigned int val = sha1[i];
180 char *pos = pathbuf + i*2 + (i > 0);
181 *pos++ = hex[val >> 4];
182 *pos = hex[val & 0xf];
187 * NOTE! This returns a statically allocated buffer, so you have to be
188 * careful about using it. Do a "strdup()" if you need to save the
189 * filename.
191 * Also note that this returns the location for creating. Reading
192 * SHA1 file can happen from any alternate directory listed in the
193 * DB_ENVIRONMENT environment variable if it is not found in
194 * the primary object database.
196 char *sha1_file_name(const unsigned char *sha1)
198 static char *name, *base;
200 if (!base) {
201 const char *sha1_file_directory = get_object_directory();
202 int len = strlen(sha1_file_directory);
203 base = xmalloc(len + 60);
204 memcpy(base, sha1_file_directory, len);
205 memset(base+len, 0, 60);
206 base[len] = '/';
207 base[len+3] = '/';
208 name = base + len + 1;
210 fill_sha1_path(name, sha1);
211 return base;
214 struct alternate_object_database *alt_odb;
217 * Prepare alternate object database registry.
218 * alt_odb points at an array of struct alternate_object_database.
219 * This array is terminated with an element that has both its base
220 * and name set to NULL. alt_odb[n] comes from n'th non-empty
221 * element from colon separated ALTERNATE_DB_ENVIRONMENT environment
222 * variable, and its base points at a statically allocated buffer
223 * that contains "/the/directory/corresponding/to/.git/objects/...",
224 * while its name points just after the slash at the end of
225 * ".git/objects/" in the example above, and has enough space to hold
226 * 40-byte hex SHA1, an extra slash for the first level indirection,
227 * and the terminating NUL.
228 * This function allocates the alt_odb array and all the strings
229 * pointed by base fields of the array elements with one xmalloc();
230 * the string pool immediately follows the array.
232 void prepare_alt_odb(void)
234 int pass, totlen, i;
235 const char *cp, *last;
236 char *op = NULL;
237 const char *alt = gitenv(ALTERNATE_DB_ENVIRONMENT) ? : "";
239 if (alt_odb)
240 return;
241 /* The first pass counts how large an area to allocate to
242 * hold the entire alt_odb structure, including array of
243 * structs and path buffers for them. The second pass fills
244 * the structure and prepares the path buffers for use by
245 * fill_sha1_path().
247 for (totlen = pass = 0; pass < 2; pass++) {
248 last = alt;
249 i = 0;
250 do {
251 cp = strchr(last, ':') ? : last + strlen(last);
252 if (last != cp) {
253 /* 43 = 40-byte + 2 '/' + terminating NUL */
254 int pfxlen = cp - last;
255 int entlen = pfxlen + 43;
256 if (pass == 0)
257 totlen += entlen;
258 else {
259 alt_odb[i].base = op;
260 alt_odb[i].name = op + pfxlen + 1;
261 memcpy(op, last, pfxlen);
262 op[pfxlen] = op[pfxlen + 3] = '/';
263 op[entlen-1] = 0;
264 op += entlen;
266 i++;
268 while (*cp && *cp == ':')
269 cp++;
270 last = cp;
271 } while (*cp);
272 if (pass)
273 break;
274 alt_odb = xmalloc(sizeof(*alt_odb) * (i + 1) + totlen);
275 alt_odb[i].base = alt_odb[i].name = NULL;
276 op = (char*)(&alt_odb[i+1]);
280 static char *find_sha1_file(const unsigned char *sha1, struct stat *st)
282 int i;
283 char *name = sha1_file_name(sha1);
285 if (!stat(name, st))
286 return name;
287 prepare_alt_odb();
288 for (i = 0; (name = alt_odb[i].name) != NULL; i++) {
289 fill_sha1_path(name, sha1);
290 if (!stat(alt_odb[i].base, st))
291 return alt_odb[i].base;
293 return NULL;
296 #define PACK_MAX_SZ (1<<26)
297 static int pack_used_ctr;
298 static unsigned long pack_mapped;
299 struct packed_git *packed_git;
301 static int check_packed_git_idx(const char *path, unsigned long *idx_size_,
302 void **idx_map_)
304 void *idx_map;
305 unsigned int *index;
306 unsigned long idx_size;
307 int nr, i;
308 int fd = open(path, O_RDONLY);
309 struct stat st;
310 if (fd < 0)
311 return -1;
312 if (fstat(fd, &st)) {
313 close(fd);
314 return -1;
316 idx_size = st.st_size;
317 idx_map = mmap(NULL, idx_size, PROT_READ, MAP_PRIVATE, fd, 0);
318 close(fd);
319 if (idx_map == MAP_FAILED)
320 return -1;
322 index = idx_map;
323 *idx_map_ = idx_map;
324 *idx_size_ = idx_size;
326 /* check index map */
327 if (idx_size < 4*256 + 20 + 20)
328 return error("index file too small");
329 nr = 0;
330 for (i = 0; i < 256; i++) {
331 unsigned int n = ntohl(index[i]);
332 if (n < nr)
333 return error("non-monotonic index");
334 nr = n;
338 * Total size:
339 * - 256 index entries 4 bytes each
340 * - 24-byte entries * nr (20-byte sha1 + 4-byte offset)
341 * - 20-byte SHA1 of the packfile
342 * - 20-byte SHA1 file checksum
344 if (idx_size != 4*256 + nr * 24 + 20 + 20)
345 return error("wrong index file size");
347 return 0;
350 static int unuse_one_packed_git(void)
352 struct packed_git *p, *lru = NULL;
354 for (p = packed_git; p; p = p->next) {
355 if (p->pack_use_cnt || !p->pack_base)
356 continue;
357 if (!lru || p->pack_last_used < lru->pack_last_used)
358 lru = p;
360 if (!lru)
361 return 0;
362 munmap(lru->pack_base, lru->pack_size);
363 lru->pack_base = NULL;
364 return 1;
367 void unuse_packed_git(struct packed_git *p)
369 p->pack_use_cnt--;
372 int use_packed_git(struct packed_git *p)
374 if (!p->pack_base) {
375 int fd;
376 struct stat st;
377 void *map;
379 pack_mapped += p->pack_size;
380 while (PACK_MAX_SZ < pack_mapped && unuse_one_packed_git())
381 ; /* nothing */
382 fd = open(p->pack_name, O_RDONLY);
383 if (fd < 0)
384 die("packfile %s cannot be opened", p->pack_name);
385 if (fstat(fd, &st)) {
386 close(fd);
387 die("packfile %s cannot be opened", p->pack_name);
389 if (st.st_size != p->pack_size)
390 die("packfile %s size mismatch.", p->pack_name);
391 map = mmap(NULL, p->pack_size, PROT_READ, MAP_PRIVATE, fd, 0);
392 close(fd);
393 if (map == MAP_FAILED)
394 die("packfile %s cannot be mapped.", p->pack_name);
395 p->pack_base = map;
397 /* Check if the pack file matches with the index file.
398 * this is cheap.
400 if (memcmp((char*)(p->index_base) + p->index_size - 40,
401 p->pack_base + p->pack_size - 20, 20))
402 die("packfile %s does not match index.", p->pack_name);
404 p->pack_last_used = pack_used_ctr++;
405 p->pack_use_cnt++;
406 return 0;
409 struct packed_git *add_packed_git(char *path, int path_len)
411 struct stat st;
412 struct packed_git *p;
413 unsigned long idx_size;
414 void *idx_map;
416 if (check_packed_git_idx(path, &idx_size, &idx_map))
417 return NULL;
419 /* do we have a corresponding .pack file? */
420 strcpy(path + path_len - 4, ".pack");
421 if (stat(path, &st) || !S_ISREG(st.st_mode)) {
422 munmap(idx_map, idx_size);
423 return NULL;
425 /* ok, it looks sane as far as we can check without
426 * actually mapping the pack file.
428 p = xmalloc(sizeof(*p) + path_len + 2);
429 strcpy(p->pack_name, path);
430 p->index_size = idx_size;
431 p->pack_size = st.st_size;
432 p->index_base = idx_map;
433 p->next = NULL;
434 p->pack_base = NULL;
435 p->pack_last_used = 0;
436 p->pack_use_cnt = 0;
437 return p;
440 static void prepare_packed_git_one(char *objdir)
442 char path[PATH_MAX];
443 int len;
444 DIR *dir;
445 struct dirent *de;
447 sprintf(path, "%s/pack", objdir);
448 len = strlen(path);
449 dir = opendir(path);
450 if (!dir)
451 return;
452 path[len++] = '/';
453 while ((de = readdir(dir)) != NULL) {
454 int namelen = strlen(de->d_name);
455 struct packed_git *p;
457 if (strcmp(de->d_name + namelen - 4, ".idx"))
458 continue;
460 /* we have .idx. Is it a file we can map? */
461 strcpy(path + len, de->d_name);
462 p = add_packed_git(path, len + namelen);
463 if (!p)
464 continue;
465 p->next = packed_git;
466 packed_git = p;
468 closedir(dir);
471 void prepare_packed_git(void)
473 int i;
474 static int run_once = 0;
476 if (run_once++)
477 return;
479 prepare_packed_git_one(get_object_directory());
480 prepare_alt_odb();
481 for (i = 0; alt_odb[i].base != NULL; i++) {
482 alt_odb[i].name[0] = 0;
483 prepare_packed_git_one(alt_odb[i].base);
487 int check_sha1_signature(const unsigned char *sha1, void *map, unsigned long size, const char *type)
489 char header[100];
490 unsigned char real_sha1[20];
491 SHA_CTX c;
493 SHA1_Init(&c);
494 SHA1_Update(&c, header, 1+sprintf(header, "%s %lu", type, size));
495 SHA1_Update(&c, map, size);
496 SHA1_Final(real_sha1, &c);
497 return memcmp(sha1, real_sha1, 20) ? -1 : 0;
500 static void *map_sha1_file_internal(const unsigned char *sha1,
501 unsigned long *size)
503 struct stat st;
504 void *map;
505 int fd;
506 char *filename = find_sha1_file(sha1, &st);
508 if (!filename) {
509 return NULL;
512 fd = open(filename, O_RDONLY | sha1_file_open_flag);
513 if (fd < 0) {
514 /* See if it works without O_NOATIME */
515 switch (sha1_file_open_flag) {
516 default:
517 fd = open(filename, O_RDONLY);
518 if (fd >= 0)
519 break;
520 /* Fallthrough */
521 case 0:
522 return NULL;
525 /* If it failed once, it will probably fail again.
526 * Stop using O_NOATIME
528 sha1_file_open_flag = 0;
530 map = mmap(NULL, st.st_size, PROT_READ, MAP_PRIVATE, fd, 0);
531 close(fd);
532 if (map == MAP_FAILED)
533 return NULL;
534 *size = st.st_size;
535 return map;
538 int unpack_sha1_header(z_stream *stream, void *map, unsigned long mapsize, void *buffer, unsigned long size)
540 /* Get the data stream */
541 memset(stream, 0, sizeof(*stream));
542 stream->next_in = map;
543 stream->avail_in = mapsize;
544 stream->next_out = buffer;
545 stream->avail_out = size;
547 inflateInit(stream);
548 return inflate(stream, 0);
551 static void *unpack_sha1_rest(z_stream *stream, void *buffer, unsigned long size)
553 int bytes = strlen(buffer) + 1;
554 unsigned char *buf = xmalloc(1+size);
556 memcpy(buf, buffer + bytes, stream->total_out - bytes);
557 bytes = stream->total_out - bytes;
558 if (bytes < size) {
559 stream->next_out = buf + bytes;
560 stream->avail_out = size - bytes;
561 while (inflate(stream, Z_FINISH) == Z_OK)
562 /* nothing */;
564 buf[size] = 0;
565 inflateEnd(stream);
566 return buf;
570 * We used to just use "sscanf()", but that's actually way
571 * too permissive for what we want to check. So do an anal
572 * object header parse by hand.
574 int parse_sha1_header(char *hdr, char *type, unsigned long *sizep)
576 int i;
577 unsigned long size;
580 * The type can be at most ten bytes (including the
581 * terminating '\0' that we add), and is followed by
582 * a space.
584 i = 10;
585 for (;;) {
586 char c = *hdr++;
587 if (c == ' ')
588 break;
589 if (!--i)
590 return -1;
591 *type++ = c;
593 *type = 0;
596 * The length must follow immediately, and be in canonical
597 * decimal format (ie "010" is not valid).
599 size = *hdr++ - '0';
600 if (size > 9)
601 return -1;
602 if (size) {
603 for (;;) {
604 unsigned long c = *hdr - '0';
605 if (c > 9)
606 break;
607 hdr++;
608 size = size * 10 + c;
611 *sizep = size;
614 * The length must be followed by a zero byte
616 return *hdr ? -1 : 0;
619 void * unpack_sha1_file(void *map, unsigned long mapsize, char *type, unsigned long *size)
621 int ret;
622 z_stream stream;
623 char hdr[8192];
625 ret = unpack_sha1_header(&stream, map, mapsize, hdr, sizeof(hdr));
626 if (ret < Z_OK || parse_sha1_header(hdr, type, size) < 0)
627 return NULL;
629 return unpack_sha1_rest(&stream, hdr, *size);
632 /* forward declaration for a mutually recursive function */
633 static int packed_object_info(struct pack_entry *entry,
634 char *type, unsigned long *sizep);
636 static int packed_delta_info(unsigned char *base_sha1,
637 unsigned long delta_size,
638 unsigned long left,
639 char *type,
640 unsigned long *sizep,
641 struct packed_git *p)
643 struct pack_entry base_ent;
645 if (left < 20)
646 die("truncated pack file");
648 /* The base entry _must_ be in the same pack */
649 if (!find_pack_entry_one(base_sha1, &base_ent, p))
650 die("failed to find delta-pack base object %s",
651 sha1_to_hex(base_sha1));
653 /* We choose to only get the type of the base object and
654 * ignore potentially corrupt pack file that expects the delta
655 * based on a base with a wrong size. This saves tons of
656 * inflate() calls.
659 if (packed_object_info(&base_ent, type, NULL))
660 die("cannot get info for delta-pack base");
662 if (sizep) {
663 const unsigned char *data;
664 unsigned char delta_head[64];
665 unsigned long result_size;
666 z_stream stream;
667 int st;
669 memset(&stream, 0, sizeof(stream));
671 data = stream.next_in = base_sha1 + 20;
672 stream.avail_in = left - 20;
673 stream.next_out = delta_head;
674 stream.avail_out = sizeof(delta_head);
676 inflateInit(&stream);
677 st = inflate(&stream, Z_FINISH);
678 inflateEnd(&stream);
679 if ((st != Z_STREAM_END) &&
680 stream.total_out != sizeof(delta_head))
681 die("delta data unpack-initial failed");
683 /* Examine the initial part of the delta to figure out
684 * the result size.
686 data = delta_head;
687 get_delta_hdr_size(&data); /* ignore base size */
689 /* Read the result size */
690 result_size = get_delta_hdr_size(&data);
691 *sizep = result_size;
693 return 0;
696 static unsigned long unpack_object_header(struct packed_git *p, unsigned long offset,
697 enum object_type *type, unsigned long *sizep)
699 unsigned shift;
700 unsigned char *pack, c;
701 unsigned long size;
703 if (offset >= p->pack_size)
704 die("object offset outside of pack file");
706 pack = p->pack_base + offset;
707 c = *pack++;
708 offset++;
709 *type = (c >> 4) & 7;
710 size = c & 15;
711 shift = 4;
712 while (c & 0x80) {
713 if (offset >= p->pack_size)
714 die("object offset outside of pack file");
715 c = *pack++;
716 offset++;
717 size += (c & 0x7f) << shift;
718 shift += 7;
720 *sizep = size;
721 return offset;
724 void packed_object_info_detail(struct pack_entry *e,
725 char *type,
726 unsigned long *size,
727 unsigned long *store_size,
728 int *delta_chain_length,
729 unsigned char *base_sha1)
731 struct packed_git *p = e->p;
732 unsigned long offset, left;
733 unsigned char *pack;
734 enum object_type kind;
736 offset = unpack_object_header(p, e->offset, &kind, size);
737 pack = p->pack_base + offset;
738 left = p->pack_size - offset;
739 if (kind != OBJ_DELTA)
740 *delta_chain_length = 0;
741 else {
742 int chain_length = 0;
743 memcpy(base_sha1, pack, 20);
744 do {
745 struct pack_entry base_ent;
746 unsigned long junk;
748 find_pack_entry_one(pack, &base_ent, p);
749 offset = unpack_object_header(p, base_ent.offset,
750 &kind, &junk);
751 pack = p->pack_base + offset;
752 chain_length++;
753 } while (kind == OBJ_DELTA);
754 *delta_chain_length = chain_length;
756 switch (kind) {
757 case OBJ_COMMIT:
758 strcpy(type, "commit");
759 break;
760 case OBJ_TREE:
761 strcpy(type, "tree");
762 break;
763 case OBJ_BLOB:
764 strcpy(type, "blob");
765 break;
766 case OBJ_TAG:
767 strcpy(type, "tag");
768 break;
769 default:
770 die("corrupted pack file");
772 *store_size = 0; /* notyet */
775 static int packed_object_info(struct pack_entry *entry,
776 char *type, unsigned long *sizep)
778 struct packed_git *p = entry->p;
779 unsigned long offset, size, left;
780 unsigned char *pack;
781 enum object_type kind;
782 int retval;
784 if (use_packed_git(p))
785 die("cannot map packed file");
787 offset = unpack_object_header(p, entry->offset, &kind, &size);
788 pack = p->pack_base + offset;
789 left = p->pack_size - offset;
791 switch (kind) {
792 case OBJ_DELTA:
793 retval = packed_delta_info(pack, size, left, type, sizep, p);
794 unuse_packed_git(p);
795 return retval;
796 case OBJ_COMMIT:
797 strcpy(type, "commit");
798 break;
799 case OBJ_TREE:
800 strcpy(type, "tree");
801 break;
802 case OBJ_BLOB:
803 strcpy(type, "blob");
804 break;
805 case OBJ_TAG:
806 strcpy(type, "tag");
807 break;
808 default:
809 die("corrupted pack file");
811 if (sizep)
812 *sizep = size;
813 unuse_packed_git(p);
814 return 0;
817 /* forward declaration for a mutually recursive function */
818 static void *unpack_entry(struct pack_entry *, char *, unsigned long *);
820 static void *unpack_delta_entry(unsigned char *base_sha1,
821 unsigned long delta_size,
822 unsigned long left,
823 char *type,
824 unsigned long *sizep,
825 struct packed_git *p)
827 struct pack_entry base_ent;
828 void *data, *delta_data, *result, *base;
829 unsigned long data_size, result_size, base_size;
830 z_stream stream;
831 int st;
833 if (left < 20)
834 die("truncated pack file");
835 data = base_sha1 + 20;
836 data_size = left - 20;
837 delta_data = xmalloc(delta_size);
839 memset(&stream, 0, sizeof(stream));
841 stream.next_in = data;
842 stream.avail_in = data_size;
843 stream.next_out = delta_data;
844 stream.avail_out = delta_size;
846 inflateInit(&stream);
847 st = inflate(&stream, Z_FINISH);
848 inflateEnd(&stream);
849 if ((st != Z_STREAM_END) || stream.total_out != delta_size)
850 die("delta data unpack failed");
852 /* The base entry _must_ be in the same pack */
853 if (!find_pack_entry_one(base_sha1, &base_ent, p))
854 die("failed to find delta-pack base object %s",
855 sha1_to_hex(base_sha1));
856 base = unpack_entry_gently(&base_ent, type, &base_size);
857 if (!base)
858 die("failed to read delta-pack base object %s",
859 sha1_to_hex(base_sha1));
860 result = patch_delta(base, base_size,
861 delta_data, delta_size,
862 &result_size);
863 if (!result)
864 die("failed to apply delta");
865 free(delta_data);
866 free(base);
867 *sizep = result_size;
868 return result;
871 static void *unpack_non_delta_entry(unsigned char *data,
872 unsigned long size,
873 unsigned long left)
875 int st;
876 z_stream stream;
877 unsigned char *buffer;
879 buffer = xmalloc(size + 1);
880 buffer[size] = 0;
881 memset(&stream, 0, sizeof(stream));
882 stream.next_in = data;
883 stream.avail_in = left;
884 stream.next_out = buffer;
885 stream.avail_out = size;
887 inflateInit(&stream);
888 st = inflate(&stream, Z_FINISH);
889 inflateEnd(&stream);
890 if ((st != Z_STREAM_END) || stream.total_out != size) {
891 free(buffer);
892 return NULL;
895 return buffer;
898 static void *unpack_entry(struct pack_entry *entry,
899 char *type, unsigned long *sizep)
901 struct packed_git *p = entry->p;
902 void *retval;
904 if (use_packed_git(p))
905 die("cannot map packed file");
906 retval = unpack_entry_gently(entry, type, sizep);
907 unuse_packed_git(p);
908 if (!retval)
909 die("corrupted pack file");
910 return retval;
913 /* The caller is responsible for use_packed_git()/unuse_packed_git() pair */
914 void *unpack_entry_gently(struct pack_entry *entry,
915 char *type, unsigned long *sizep)
917 struct packed_git *p = entry->p;
918 unsigned long offset, size, left;
919 unsigned char *pack;
920 enum object_type kind;
921 void *retval;
923 offset = unpack_object_header(p, entry->offset, &kind, &size);
924 pack = p->pack_base + offset;
925 left = p->pack_size - offset;
926 switch (kind) {
927 case OBJ_DELTA:
928 retval = unpack_delta_entry(pack, size, left, type, sizep, p);
929 return retval;
930 case OBJ_COMMIT:
931 strcpy(type, "commit");
932 break;
933 case OBJ_TREE:
934 strcpy(type, "tree");
935 break;
936 case OBJ_BLOB:
937 strcpy(type, "blob");
938 break;
939 case OBJ_TAG:
940 strcpy(type, "tag");
941 break;
942 default:
943 return NULL;
945 *sizep = size;
946 retval = unpack_non_delta_entry(pack, size, left);
947 return retval;
950 int num_packed_objects(const struct packed_git *p)
952 /* See check_packed_git_idx() */
953 return (p->index_size - 20 - 20 - 4*256) / 24;
956 int nth_packed_object_sha1(const struct packed_git *p, int n,
957 unsigned char* sha1)
959 void *index = p->index_base + 256;
960 if (n < 0 || num_packed_objects(p) <= n)
961 return -1;
962 memcpy(sha1, (index + 24 * n + 4), 20);
963 return 0;
966 int find_pack_entry_one(const unsigned char *sha1,
967 struct pack_entry *e, struct packed_git *p)
969 unsigned int *level1_ofs = p->index_base;
970 int hi = ntohl(level1_ofs[*sha1]);
971 int lo = ((*sha1 == 0x0) ? 0 : ntohl(level1_ofs[*sha1 - 1]));
972 void *index = p->index_base + 256;
974 do {
975 int mi = (lo + hi) / 2;
976 int cmp = memcmp(index + 24 * mi + 4, sha1, 20);
977 if (!cmp) {
978 e->offset = ntohl(*((int*)(index + 24 * mi)));
979 memcpy(e->sha1, sha1, 20);
980 e->p = p;
981 return 1;
983 if (cmp > 0)
984 hi = mi;
985 else
986 lo = mi+1;
987 } while (lo < hi);
988 return 0;
991 static int find_pack_entry(const unsigned char *sha1, struct pack_entry *e)
993 struct packed_git *p;
994 prepare_packed_git();
996 for (p = packed_git; p; p = p->next) {
997 if (find_pack_entry_one(sha1, e, p))
998 return 1;
1000 return 0;
1003 int sha1_object_info(const unsigned char *sha1, char *type, unsigned long *sizep)
1005 int status;
1006 unsigned long mapsize, size;
1007 void *map;
1008 z_stream stream;
1009 char hdr[128];
1011 map = map_sha1_file_internal(sha1, &mapsize);
1012 if (!map) {
1013 struct pack_entry e;
1015 if (!find_pack_entry(sha1, &e))
1016 return error("unable to find %s", sha1_to_hex(sha1));
1017 return packed_object_info(&e, type, sizep);
1019 if (unpack_sha1_header(&stream, map, mapsize, hdr, sizeof(hdr)) < 0)
1020 status = error("unable to unpack %s header",
1021 sha1_to_hex(sha1));
1022 if (parse_sha1_header(hdr, type, &size) < 0)
1023 status = error("unable to parse %s header", sha1_to_hex(sha1));
1024 else {
1025 status = 0;
1026 if (sizep)
1027 *sizep = size;
1029 inflateEnd(&stream);
1030 munmap(map, mapsize);
1031 return status;
1034 static void *read_packed_sha1(const unsigned char *sha1, char *type, unsigned long *size)
1036 struct pack_entry e;
1038 if (!find_pack_entry(sha1, &e)) {
1039 error("cannot read sha1_file for %s", sha1_to_hex(sha1));
1040 return NULL;
1042 return unpack_entry(&e, type, size);
1045 void * read_sha1_file(const unsigned char *sha1, char *type, unsigned long *size)
1047 unsigned long mapsize;
1048 void *map, *buf;
1049 struct pack_entry e;
1051 if (find_pack_entry(sha1, &e))
1052 return read_packed_sha1(sha1, type, size);
1053 map = map_sha1_file_internal(sha1, &mapsize);
1054 if (map) {
1055 buf = unpack_sha1_file(map, mapsize, type, size);
1056 munmap(map, mapsize);
1057 return buf;
1059 return NULL;
1062 void *read_object_with_reference(const unsigned char *sha1,
1063 const char *required_type,
1064 unsigned long *size,
1065 unsigned char *actual_sha1_return)
1067 char type[20];
1068 void *buffer;
1069 unsigned long isize;
1070 unsigned char actual_sha1[20];
1072 memcpy(actual_sha1, sha1, 20);
1073 while (1) {
1074 int ref_length = -1;
1075 const char *ref_type = NULL;
1077 buffer = read_sha1_file(actual_sha1, type, &isize);
1078 if (!buffer)
1079 return NULL;
1080 if (!strcmp(type, required_type)) {
1081 *size = isize;
1082 if (actual_sha1_return)
1083 memcpy(actual_sha1_return, actual_sha1, 20);
1084 return buffer;
1086 /* Handle references */
1087 else if (!strcmp(type, "commit"))
1088 ref_type = "tree ";
1089 else if (!strcmp(type, "tag"))
1090 ref_type = "object ";
1091 else {
1092 free(buffer);
1093 return NULL;
1095 ref_length = strlen(ref_type);
1097 if (memcmp(buffer, ref_type, ref_length) ||
1098 get_sha1_hex(buffer + ref_length, actual_sha1)) {
1099 free(buffer);
1100 return NULL;
1102 /* Now we have the ID of the referred-to object in
1103 * actual_sha1. Check again. */
1107 char *write_sha1_file_prepare(void *buf,
1108 unsigned long len,
1109 const char *type,
1110 unsigned char *sha1,
1111 unsigned char *hdr,
1112 int *hdrlen)
1114 SHA_CTX c;
1116 /* Generate the header */
1117 *hdrlen = sprintf((char *)hdr, "%s %lu", type, len)+1;
1119 /* Sha1.. */
1120 SHA1_Init(&c);
1121 SHA1_Update(&c, hdr, *hdrlen);
1122 SHA1_Update(&c, buf, len);
1123 SHA1_Final(sha1, &c);
1125 return sha1_file_name(sha1);
1128 int write_sha1_file(void *buf, unsigned long len, const char *type, unsigned char *returnsha1)
1130 int size;
1131 unsigned char *compressed;
1132 z_stream stream;
1133 unsigned char sha1[20];
1134 char *filename;
1135 static char tmpfile[PATH_MAX];
1136 unsigned char hdr[50];
1137 int fd, hdrlen, ret;
1139 /* Normally if we have it in the pack then we do not bother writing
1140 * it out into .git/objects/??/?{38} file.
1142 filename = write_sha1_file_prepare(buf, len, type, sha1, hdr, &hdrlen);
1143 if (returnsha1)
1144 memcpy(returnsha1, sha1, 20);
1145 if (has_sha1_file(sha1))
1146 return 0;
1147 fd = open(filename, O_RDONLY);
1148 if (fd >= 0) {
1150 * FIXME!!! We might do collision checking here, but we'd
1151 * need to uncompress the old file and check it. Later.
1153 close(fd);
1154 return 0;
1157 if (errno != ENOENT) {
1158 fprintf(stderr, "sha1 file %s: %s", filename, strerror(errno));
1159 return -1;
1162 snprintf(tmpfile, sizeof(tmpfile), "%s/obj_XXXXXX", get_object_directory());
1164 fd = mkstemp(tmpfile);
1165 if (fd < 0) {
1166 fprintf(stderr, "unable to create temporary sha1 filename %s: %s", tmpfile, strerror(errno));
1167 return -1;
1170 /* Set it up */
1171 memset(&stream, 0, sizeof(stream));
1172 deflateInit(&stream, Z_BEST_COMPRESSION);
1173 size = deflateBound(&stream, len+hdrlen);
1174 compressed = xmalloc(size);
1176 /* Compress it */
1177 stream.next_out = compressed;
1178 stream.avail_out = size;
1180 /* First header.. */
1181 stream.next_in = hdr;
1182 stream.avail_in = hdrlen;
1183 while (deflate(&stream, 0) == Z_OK)
1184 /* nothing */;
1186 /* Then the data itself.. */
1187 stream.next_in = buf;
1188 stream.avail_in = len;
1189 while (deflate(&stream, Z_FINISH) == Z_OK)
1190 /* nothing */;
1191 deflateEnd(&stream);
1192 size = stream.total_out;
1194 if (write(fd, compressed, size) != size)
1195 die("unable to write file");
1196 fchmod(fd, 0444);
1197 close(fd);
1198 free(compressed);
1200 ret = link(tmpfile, filename);
1201 if (ret < 0) {
1202 ret = errno;
1205 * Coda hack - coda doesn't like cross-directory links,
1206 * so we fall back to a rename, which will mean that it
1207 * won't be able to check collisions, but that's not a
1208 * big deal.
1210 * When this succeeds, we just return 0. We have nothing
1211 * left to unlink.
1213 if (ret == EXDEV && !rename(tmpfile, filename))
1214 return 0;
1216 unlink(tmpfile);
1217 if (ret) {
1218 if (ret != EEXIST) {
1219 fprintf(stderr, "unable to write sha1 filename %s: %s", filename, strerror(ret));
1220 return -1;
1222 /* FIXME!!! Collision check here ? */
1225 return 0;
1228 int write_sha1_to_fd(int fd, const unsigned char *sha1)
1230 ssize_t size;
1231 unsigned long objsize;
1232 int posn = 0;
1233 void *buf = map_sha1_file_internal(sha1, &objsize);
1234 z_stream stream;
1235 if (!buf) {
1236 unsigned char *unpacked;
1237 unsigned long len;
1238 char type[20];
1239 char hdr[50];
1240 int hdrlen;
1241 // need to unpack and recompress it by itself
1242 unpacked = read_packed_sha1(sha1, type, &len);
1244 hdrlen = sprintf(hdr, "%s %lu", type, len) + 1;
1246 /* Set it up */
1247 memset(&stream, 0, sizeof(stream));
1248 deflateInit(&stream, Z_BEST_COMPRESSION);
1249 size = deflateBound(&stream, len + hdrlen);
1250 buf = xmalloc(size);
1252 /* Compress it */
1253 stream.next_out = buf;
1254 stream.avail_out = size;
1256 /* First header.. */
1257 stream.next_in = (void *)hdr;
1258 stream.avail_in = hdrlen;
1259 while (deflate(&stream, 0) == Z_OK)
1260 /* nothing */;
1262 /* Then the data itself.. */
1263 stream.next_in = unpacked;
1264 stream.avail_in = len;
1265 while (deflate(&stream, Z_FINISH) == Z_OK)
1266 /* nothing */;
1267 deflateEnd(&stream);
1269 objsize = stream.total_out;
1272 do {
1273 size = write(fd, buf + posn, objsize - posn);
1274 if (size <= 0) {
1275 if (!size) {
1276 fprintf(stderr, "write closed");
1277 } else {
1278 perror("write ");
1280 return -1;
1282 posn += size;
1283 } while (posn < objsize);
1284 return 0;
1287 int write_sha1_from_fd(const unsigned char *sha1, int fd)
1289 char *filename = sha1_file_name(sha1);
1291 int local;
1292 z_stream stream;
1293 unsigned char real_sha1[20];
1294 unsigned char buf[4096];
1295 unsigned char discard[4096];
1296 int ret;
1297 SHA_CTX c;
1299 local = open(filename, O_WRONLY | O_CREAT | O_EXCL, 0666);
1301 if (local < 0)
1302 return error("Couldn't open %s\n", filename);
1304 memset(&stream, 0, sizeof(stream));
1306 inflateInit(&stream);
1308 SHA1_Init(&c);
1310 do {
1311 ssize_t size;
1312 size = read(fd, buf, 4096);
1313 if (size <= 0) {
1314 close(local);
1315 unlink(filename);
1316 if (!size)
1317 return error("Connection closed?");
1318 perror("Reading from connection");
1319 return -1;
1321 write(local, buf, size);
1322 stream.avail_in = size;
1323 stream.next_in = buf;
1324 do {
1325 stream.next_out = discard;
1326 stream.avail_out = sizeof(discard);
1327 ret = inflate(&stream, Z_SYNC_FLUSH);
1328 SHA1_Update(&c, discard, sizeof(discard) -
1329 stream.avail_out);
1330 } while (stream.avail_in && ret == Z_OK);
1332 } while (ret == Z_OK);
1333 inflateEnd(&stream);
1335 close(local);
1336 SHA1_Final(real_sha1, &c);
1337 if (ret != Z_STREAM_END) {
1338 unlink(filename);
1339 return error("File %s corrupted", sha1_to_hex(sha1));
1341 if (memcmp(sha1, real_sha1, 20)) {
1342 unlink(filename);
1343 return error("File %s has bad hash\n", sha1_to_hex(sha1));
1346 return 0;
1349 int has_sha1_pack(const unsigned char *sha1)
1351 struct pack_entry e;
1352 return find_pack_entry(sha1, &e);
1355 int has_sha1_file(const unsigned char *sha1)
1357 struct stat st;
1358 struct pack_entry e;
1360 if (find_pack_entry(sha1, &e))
1361 return 1;
1362 return find_sha1_file(sha1, &st) ? 1 : 0;
1365 int index_fd(unsigned char *sha1, int fd, struct stat *st, int write_object, const char *type)
1367 unsigned long size = st->st_size;
1368 void *buf;
1369 int ret;
1370 unsigned char hdr[50];
1371 int hdrlen;
1373 buf = "";
1374 if (size)
1375 buf = mmap(NULL, size, PROT_READ, MAP_PRIVATE, fd, 0);
1376 close(fd);
1377 if (buf == MAP_FAILED)
1378 return -1;
1380 if (!type)
1381 type = "blob";
1382 if (write_object)
1383 ret = write_sha1_file(buf, size, type, sha1);
1384 else {
1385 write_sha1_file_prepare(buf, size, type, sha1, hdr, &hdrlen);
1386 ret = 0;
1388 if (size)
1389 munmap(buf, size);
1390 return ret;