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