Cleanup read_cache_from error handling.
[git/gitweb-caching.git] / sha1_file.c
blob84037fe98fa6ea0e4f1127c165786df51a34c5dd
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 signed char hexval_table[256] = {
30 -1, -1, -1, -1, -1, -1, -1, -1, /* 00-07 */
31 -1, -1, -1, -1, -1, -1, -1, -1, /* 08-0f */
32 -1, -1, -1, -1, -1, -1, -1, -1, /* 10-17 */
33 -1, -1, -1, -1, -1, -1, -1, -1, /* 18-1f */
34 -1, -1, -1, -1, -1, -1, -1, -1, /* 20-27 */
35 -1, -1, -1, -1, -1, -1, -1, -1, /* 28-2f */
36 0, 1, 2, 3, 4, 5, 6, 7, /* 30-37 */
37 8, 9, -1, -1, -1, -1, -1, -1, /* 38-3f */
38 -1, 10, 11, 12, 13, 14, 15, -1, /* 40-47 */
39 -1, -1, -1, -1, -1, -1, -1, -1, /* 48-4f */
40 -1, -1, -1, -1, -1, -1, -1, -1, /* 50-57 */
41 -1, -1, -1, -1, -1, -1, -1, -1, /* 58-5f */
42 -1, 10, 11, 12, 13, 14, 15, -1, /* 60-67 */
43 -1, -1, -1, -1, -1, -1, -1, -1, /* 68-67 */
44 -1, -1, -1, -1, -1, -1, -1, -1, /* 70-77 */
45 -1, -1, -1, -1, -1, -1, -1, -1, /* 78-7f */
46 -1, -1, -1, -1, -1, -1, -1, -1, /* 80-87 */
47 -1, -1, -1, -1, -1, -1, -1, -1, /* 88-8f */
48 -1, -1, -1, -1, -1, -1, -1, -1, /* 90-97 */
49 -1, -1, -1, -1, -1, -1, -1, -1, /* 98-9f */
50 -1, -1, -1, -1, -1, -1, -1, -1, /* a0-a7 */
51 -1, -1, -1, -1, -1, -1, -1, -1, /* a8-af */
52 -1, -1, -1, -1, -1, -1, -1, -1, /* b0-b7 */
53 -1, -1, -1, -1, -1, -1, -1, -1, /* b8-bf */
54 -1, -1, -1, -1, -1, -1, -1, -1, /* c0-c7 */
55 -1, -1, -1, -1, -1, -1, -1, -1, /* c8-cf */
56 -1, -1, -1, -1, -1, -1, -1, -1, /* d0-d7 */
57 -1, -1, -1, -1, -1, -1, -1, -1, /* d8-df */
58 -1, -1, -1, -1, -1, -1, -1, -1, /* e0-e7 */
59 -1, -1, -1, -1, -1, -1, -1, -1, /* e8-ef */
60 -1, -1, -1, -1, -1, -1, -1, -1, /* f0-f7 */
61 -1, -1, -1, -1, -1, -1, -1, -1, /* f8-ff */
64 int get_sha1_hex(const char *hex, unsigned char *sha1)
66 int i;
67 for (i = 0; i < 20; i++) {
68 unsigned int val = (hexval(hex[0]) << 4) | hexval(hex[1]);
69 if (val & ~0xff)
70 return -1;
71 *sha1++ = val;
72 hex += 2;
74 return 0;
77 int safe_create_leading_directories(char *path)
79 char *pos = path;
80 struct stat st;
82 if (*pos == '/')
83 pos++;
85 while (pos) {
86 pos = strchr(pos, '/');
87 if (!pos)
88 break;
89 *pos = 0;
90 if (!stat(path, &st)) {
91 /* path exists */
92 if (!S_ISDIR(st.st_mode)) {
93 *pos = '/';
94 return -3;
97 else if (mkdir(path, 0777)) {
98 *pos = '/';
99 return -1;
101 else if (adjust_shared_perm(path)) {
102 *pos = '/';
103 return -2;
105 *pos++ = '/';
107 return 0;
110 char * sha1_to_hex(const unsigned char *sha1)
112 static int bufno;
113 static char hexbuffer[4][50];
114 static const char hex[] = "0123456789abcdef";
115 char *buffer = hexbuffer[3 & ++bufno], *buf = buffer;
116 int i;
118 for (i = 0; i < 20; i++) {
119 unsigned int val = *sha1++;
120 *buf++ = hex[val >> 4];
121 *buf++ = hex[val & 0xf];
123 *buf = '\0';
125 return buffer;
128 static void fill_sha1_path(char *pathbuf, const unsigned char *sha1)
130 int i;
131 for (i = 0; i < 20; i++) {
132 static char hex[] = "0123456789abcdef";
133 unsigned int val = sha1[i];
134 char *pos = pathbuf + i*2 + (i > 0);
135 *pos++ = hex[val >> 4];
136 *pos = hex[val & 0xf];
141 * NOTE! This returns a statically allocated buffer, so you have to be
142 * careful about using it. Do a "xstrdup()" if you need to save the
143 * filename.
145 * Also note that this returns the location for creating. Reading
146 * SHA1 file can happen from any alternate directory listed in the
147 * DB_ENVIRONMENT environment variable if it is not found in
148 * the primary object database.
150 char *sha1_file_name(const unsigned char *sha1)
152 static char *name, *base;
154 if (!base) {
155 const char *sha1_file_directory = get_object_directory();
156 int len = strlen(sha1_file_directory);
157 base = xmalloc(len + 60);
158 memcpy(base, sha1_file_directory, len);
159 memset(base+len, 0, 60);
160 base[len] = '/';
161 base[len+3] = '/';
162 name = base + len + 1;
164 fill_sha1_path(name, sha1);
165 return base;
168 char *sha1_pack_name(const unsigned char *sha1)
170 static const char hex[] = "0123456789abcdef";
171 static char *name, *base, *buf;
172 int i;
174 if (!base) {
175 const char *sha1_file_directory = get_object_directory();
176 int len = strlen(sha1_file_directory);
177 base = xmalloc(len + 60);
178 sprintf(base, "%s/pack/pack-1234567890123456789012345678901234567890.pack", sha1_file_directory);
179 name = base + len + 11;
182 buf = name;
184 for (i = 0; i < 20; i++) {
185 unsigned int val = *sha1++;
186 *buf++ = hex[val >> 4];
187 *buf++ = hex[val & 0xf];
190 return base;
193 char *sha1_pack_index_name(const unsigned char *sha1)
195 static const char hex[] = "0123456789abcdef";
196 static char *name, *base, *buf;
197 int i;
199 if (!base) {
200 const char *sha1_file_directory = get_object_directory();
201 int len = strlen(sha1_file_directory);
202 base = xmalloc(len + 60);
203 sprintf(base, "%s/pack/pack-1234567890123456789012345678901234567890.idx", sha1_file_directory);
204 name = base + len + 11;
207 buf = name;
209 for (i = 0; i < 20; i++) {
210 unsigned int val = *sha1++;
211 *buf++ = hex[val >> 4];
212 *buf++ = hex[val & 0xf];
215 return base;
218 struct alternate_object_database *alt_odb_list;
219 static struct alternate_object_database **alt_odb_tail;
221 static void read_info_alternates(const char * alternates, int depth);
224 * Prepare alternate object database registry.
226 * The variable alt_odb_list points at the list of struct
227 * alternate_object_database. The elements on this list come from
228 * non-empty elements from colon separated ALTERNATE_DB_ENVIRONMENT
229 * environment variable, and $GIT_OBJECT_DIRECTORY/info/alternates,
230 * whose contents is similar to that environment variable but can be
231 * LF separated. Its base points at a statically allocated buffer that
232 * contains "/the/directory/corresponding/to/.git/objects/...", while
233 * its name points just after the slash at the end of ".git/objects/"
234 * in the example above, and has enough space to hold 40-byte hex
235 * SHA1, an extra slash for the first level indirection, and the
236 * terminating NUL.
238 static int link_alt_odb_entry(const char * entry, int len, const char * relative_base, int depth)
240 struct stat st;
241 const char *objdir = get_object_directory();
242 struct alternate_object_database *ent;
243 struct alternate_object_database *alt;
244 /* 43 = 40-byte + 2 '/' + terminating NUL */
245 int pfxlen = len;
246 int entlen = pfxlen + 43;
247 int base_len = -1;
249 if (*entry != '/' && relative_base) {
250 /* Relative alt-odb */
251 if (base_len < 0)
252 base_len = strlen(relative_base) + 1;
253 entlen += base_len;
254 pfxlen += base_len;
256 ent = xmalloc(sizeof(*ent) + entlen);
258 if (*entry != '/' && relative_base) {
259 memcpy(ent->base, relative_base, base_len - 1);
260 ent->base[base_len - 1] = '/';
261 memcpy(ent->base + base_len, entry, len);
263 else
264 memcpy(ent->base, entry, pfxlen);
266 ent->name = ent->base + pfxlen + 1;
267 ent->base[pfxlen + 3] = '/';
268 ent->base[pfxlen] = ent->base[entlen-1] = 0;
270 /* Detect cases where alternate disappeared */
271 if (stat(ent->base, &st) || !S_ISDIR(st.st_mode)) {
272 error("object directory %s does not exist; "
273 "check .git/objects/info/alternates.",
274 ent->base);
275 free(ent);
276 return -1;
279 /* Prevent the common mistake of listing the same
280 * thing twice, or object directory itself.
282 for (alt = alt_odb_list; alt; alt = alt->next) {
283 if (!memcmp(ent->base, alt->base, pfxlen)) {
284 free(ent);
285 return -1;
288 if (!memcmp(ent->base, objdir, pfxlen)) {
289 free(ent);
290 return -1;
293 /* add the alternate entry */
294 *alt_odb_tail = ent;
295 alt_odb_tail = &(ent->next);
296 ent->next = NULL;
298 /* recursively add alternates */
299 read_info_alternates(ent->base, depth + 1);
301 ent->base[pfxlen] = '/';
303 return 0;
306 static void link_alt_odb_entries(const char *alt, const char *ep, int sep,
307 const char *relative_base, int depth)
309 const char *cp, *last;
311 if (depth > 5) {
312 error("%s: ignoring alternate object stores, nesting too deep.",
313 relative_base);
314 return;
317 last = alt;
318 while (last < ep) {
319 cp = last;
320 if (cp < ep && *cp == '#') {
321 while (cp < ep && *cp != sep)
322 cp++;
323 last = cp + 1;
324 continue;
326 while (cp < ep && *cp != sep)
327 cp++;
328 if (last != cp) {
329 if ((*last != '/') && depth) {
330 error("%s: ignoring relative alternate object store %s",
331 relative_base, last);
332 } else {
333 link_alt_odb_entry(last, cp - last,
334 relative_base, depth);
337 while (cp < ep && *cp == sep)
338 cp++;
339 last = cp;
343 static void read_info_alternates(const char * relative_base, int depth)
345 char *map;
346 struct stat st;
347 char path[PATH_MAX];
348 int fd;
350 sprintf(path, "%s/info/alternates", relative_base);
351 fd = open(path, O_RDONLY);
352 if (fd < 0)
353 return;
354 if (fstat(fd, &st) || (st.st_size == 0)) {
355 close(fd);
356 return;
358 map = xmmap(NULL, st.st_size, PROT_READ, MAP_PRIVATE, fd, 0);
359 close(fd);
361 link_alt_odb_entries(map, map + st.st_size, '\n', relative_base, depth);
363 munmap(map, st.st_size);
366 void prepare_alt_odb(void)
368 const char *alt;
370 alt = getenv(ALTERNATE_DB_ENVIRONMENT);
371 if (!alt) alt = "";
373 if (alt_odb_tail)
374 return;
375 alt_odb_tail = &alt_odb_list;
376 link_alt_odb_entries(alt, alt + strlen(alt), ':', NULL, 0);
378 read_info_alternates(get_object_directory(), 0);
381 static char *find_sha1_file(const unsigned char *sha1, struct stat *st)
383 char *name = sha1_file_name(sha1);
384 struct alternate_object_database *alt;
386 if (!stat(name, st))
387 return name;
388 prepare_alt_odb();
389 for (alt = alt_odb_list; alt; alt = alt->next) {
390 name = alt->name;
391 fill_sha1_path(name, sha1);
392 if (!stat(alt->base, st))
393 return alt->base;
395 return NULL;
398 static unsigned int pack_used_ctr;
399 static unsigned int pack_mmap_calls;
400 static unsigned int peak_pack_open_windows;
401 static unsigned int pack_open_windows;
402 static size_t peak_pack_mapped;
403 static size_t pack_mapped;
404 static size_t page_size;
405 struct packed_git *packed_git;
407 void pack_report()
409 fprintf(stderr,
410 "pack_report: getpagesize() = %10lu\n"
411 "pack_report: core.packedGitWindowSize = %10lu\n"
412 "pack_report: core.packedGitLimit = %10lu\n",
413 page_size,
414 packed_git_window_size,
415 packed_git_limit);
416 fprintf(stderr,
417 "pack_report: pack_used_ctr = %10u\n"
418 "pack_report: pack_mmap_calls = %10u\n"
419 "pack_report: pack_open_windows = %10u / %10u\n"
420 "pack_report: pack_mapped = %10lu / %10lu\n",
421 pack_used_ctr,
422 pack_mmap_calls,
423 pack_open_windows, peak_pack_open_windows,
424 pack_mapped, peak_pack_mapped);
427 static int check_packed_git_idx(const char *path, unsigned long *idx_size_,
428 void **idx_map_)
430 void *idx_map;
431 unsigned int *index;
432 unsigned long idx_size;
433 int nr, i;
434 int fd = open(path, O_RDONLY);
435 struct stat st;
436 if (fd < 0)
437 return -1;
438 if (fstat(fd, &st)) {
439 close(fd);
440 return -1;
442 idx_size = st.st_size;
443 idx_map = xmmap(NULL, idx_size, PROT_READ, MAP_PRIVATE, fd, 0);
444 close(fd);
446 index = idx_map;
447 *idx_map_ = idx_map;
448 *idx_size_ = idx_size;
450 /* check index map */
451 if (idx_size < 4*256 + 20 + 20)
452 return error("index file too small");
453 nr = 0;
454 for (i = 0; i < 256; i++) {
455 unsigned int n = ntohl(index[i]);
456 if (n < nr)
457 return error("non-monotonic index");
458 nr = n;
462 * Total size:
463 * - 256 index entries 4 bytes each
464 * - 24-byte entries * nr (20-byte sha1 + 4-byte offset)
465 * - 20-byte SHA1 of the packfile
466 * - 20-byte SHA1 file checksum
468 if (idx_size != 4*256 + nr * 24 + 20 + 20)
469 return error("wrong index file size");
471 return 0;
474 static void scan_windows(struct packed_git *p,
475 struct packed_git **lru_p,
476 struct pack_window **lru_w,
477 struct pack_window **lru_l)
479 struct pack_window *w, *w_l;
481 for (w_l = NULL, w = p->windows; w; w = w->next) {
482 if (!w->inuse_cnt) {
483 if (!*lru_w || w->last_used < (*lru_w)->last_used) {
484 *lru_p = p;
485 *lru_w = w;
486 *lru_l = w_l;
489 w_l = w;
493 static int unuse_one_window(struct packed_git *current)
495 struct packed_git *p, *lru_p = NULL;
496 struct pack_window *lru_w = NULL, *lru_l = NULL;
498 if (current)
499 scan_windows(current, &lru_p, &lru_w, &lru_l);
500 for (p = packed_git; p; p = p->next)
501 scan_windows(p, &lru_p, &lru_w, &lru_l);
502 if (lru_p) {
503 munmap(lru_w->base, lru_w->len);
504 pack_mapped -= lru_w->len;
505 if (lru_l)
506 lru_l->next = lru_w->next;
507 else {
508 lru_p->windows = lru_w->next;
509 if (!lru_p->windows && lru_p != current) {
510 close(lru_p->pack_fd);
511 lru_p->pack_fd = -1;
514 free(lru_w);
515 pack_open_windows--;
516 return 1;
518 return 0;
521 void release_pack_memory(size_t need)
523 size_t cur = pack_mapped;
524 while (need >= (cur - pack_mapped) && unuse_one_window(NULL))
525 ; /* nothing */
528 void unuse_pack(struct pack_window **w_cursor)
530 struct pack_window *w = *w_cursor;
531 if (w) {
532 w->inuse_cnt--;
533 *w_cursor = NULL;
537 static void open_packed_git(struct packed_git *p)
539 struct stat st;
540 struct pack_header hdr;
541 unsigned char sha1[20];
542 unsigned char *idx_sha1;
544 p->pack_fd = open(p->pack_name, O_RDONLY);
545 if (p->pack_fd < 0 || fstat(p->pack_fd, &st))
546 die("packfile %s cannot be opened", p->pack_name);
548 /* If we created the struct before we had the pack we lack size. */
549 if (!p->pack_size) {
550 if (!S_ISREG(st.st_mode))
551 die("packfile %s not a regular file", p->pack_name);
552 p->pack_size = st.st_size;
553 } else if (p->pack_size != st.st_size)
554 die("packfile %s size changed", p->pack_name);
556 /* Verify we recognize this pack file format. */
557 read_or_die(p->pack_fd, &hdr, sizeof(hdr));
558 if (hdr.hdr_signature != htonl(PACK_SIGNATURE))
559 die("file %s is not a GIT packfile", p->pack_name);
560 if (!pack_version_ok(hdr.hdr_version))
561 die("packfile %s is version %u and not supported"
562 " (try upgrading GIT to a newer version)",
563 p->pack_name, ntohl(hdr.hdr_version));
565 /* Verify the pack matches its index. */
566 if (num_packed_objects(p) != ntohl(hdr.hdr_entries))
567 die("packfile %s claims to have %u objects"
568 " while index size indicates %u objects",
569 p->pack_name, ntohl(hdr.hdr_entries),
570 num_packed_objects(p));
571 if (lseek(p->pack_fd, p->pack_size - sizeof(sha1), SEEK_SET) == -1)
572 die("end of packfile %s is unavailable", p->pack_name);
573 read_or_die(p->pack_fd, sha1, sizeof(sha1));
574 idx_sha1 = ((unsigned char *)p->index_base) + p->index_size - 40;
575 if (hashcmp(sha1, idx_sha1))
576 die("packfile %s does not match index", p->pack_name);
579 static int in_window(struct pack_window *win, unsigned long offset)
581 /* We must promise at least 20 bytes (one hash) after the
582 * offset is available from this window, otherwise the offset
583 * is not actually in this window and a different window (which
584 * has that one hash excess) must be used. This is to support
585 * the object header and delta base parsing routines below.
587 off_t win_off = win->offset;
588 return win_off <= offset
589 && (offset + 20) <= (win_off + win->len);
592 unsigned char* use_pack(struct packed_git *p,
593 struct pack_window **w_cursor,
594 unsigned long offset,
595 unsigned int *left)
597 struct pack_window *win = *w_cursor;
599 if (p->pack_fd == -1)
600 open_packed_git(p);
602 /* Since packfiles end in a hash of their content and its
603 * pointless to ask for an offset into the middle of that
604 * hash, and the in_window function above wouldn't match
605 * don't allow an offset too close to the end of the file.
607 if (offset > (p->pack_size - 20))
608 die("offset beyond end of packfile (truncated pack?)");
610 if (!win || !in_window(win, offset)) {
611 if (win)
612 win->inuse_cnt--;
613 for (win = p->windows; win; win = win->next) {
614 if (in_window(win, offset))
615 break;
617 if (!win) {
618 if (!page_size)
619 page_size = getpagesize();
620 win = xcalloc(1, sizeof(*win));
621 win->offset = (offset / page_size) * page_size;
622 win->len = p->pack_size - win->offset;
623 if (win->len > packed_git_window_size)
624 win->len = packed_git_window_size;
625 pack_mapped += win->len;
626 while (packed_git_limit < pack_mapped
627 && unuse_one_window(p))
628 ; /* nothing */
629 win->base = xmmap(NULL, win->len,
630 PROT_READ, MAP_PRIVATE,
631 p->pack_fd, win->offset);
632 if (win->base == MAP_FAILED)
633 die("packfile %s cannot be mapped: %s",
634 p->pack_name,
635 strerror(errno));
636 pack_mmap_calls++;
637 pack_open_windows++;
638 if (pack_mapped > peak_pack_mapped)
639 peak_pack_mapped = pack_mapped;
640 if (pack_open_windows > peak_pack_open_windows)
641 peak_pack_open_windows = pack_open_windows;
642 win->next = p->windows;
643 p->windows = win;
646 if (win != *w_cursor) {
647 win->last_used = pack_used_ctr++;
648 win->inuse_cnt++;
649 *w_cursor = win;
651 offset -= win->offset;
652 if (left)
653 *left = win->len - offset;
654 return win->base + offset;
657 struct packed_git *add_packed_git(char *path, int path_len, int local)
659 struct stat st;
660 struct packed_git *p;
661 unsigned long idx_size;
662 void *idx_map;
663 unsigned char sha1[20];
665 if (check_packed_git_idx(path, &idx_size, &idx_map))
666 return NULL;
668 /* do we have a corresponding .pack file? */
669 strcpy(path + path_len - 4, ".pack");
670 if (stat(path, &st) || !S_ISREG(st.st_mode)) {
671 munmap(idx_map, idx_size);
672 return NULL;
674 /* ok, it looks sane as far as we can check without
675 * actually mapping the pack file.
677 p = xmalloc(sizeof(*p) + path_len + 2);
678 strcpy(p->pack_name, path);
679 p->index_size = idx_size;
680 p->pack_size = st.st_size;
681 p->index_base = idx_map;
682 p->next = NULL;
683 p->windows = NULL;
684 p->pack_fd = -1;
685 p->pack_local = local;
686 if ((path_len > 44) && !get_sha1_hex(path + path_len - 44, sha1))
687 hashcpy(p->sha1, sha1);
688 return p;
691 struct packed_git *parse_pack_index(unsigned char *sha1)
693 char *path = sha1_pack_index_name(sha1);
694 return parse_pack_index_file(sha1, path);
697 struct packed_git *parse_pack_index_file(const unsigned char *sha1, char *idx_path)
699 struct packed_git *p;
700 unsigned long idx_size;
701 void *idx_map;
702 char *path;
704 if (check_packed_git_idx(idx_path, &idx_size, &idx_map))
705 return NULL;
707 path = sha1_pack_name(sha1);
709 p = xmalloc(sizeof(*p) + strlen(path) + 2);
710 strcpy(p->pack_name, path);
711 p->index_size = idx_size;
712 p->pack_size = 0;
713 p->index_base = idx_map;
714 p->next = NULL;
715 p->windows = NULL;
716 p->pack_fd = -1;
717 hashcpy(p->sha1, sha1);
718 return p;
721 void install_packed_git(struct packed_git *pack)
723 pack->next = packed_git;
724 packed_git = pack;
727 static void prepare_packed_git_one(char *objdir, int local)
729 char path[PATH_MAX];
730 int len;
731 DIR *dir;
732 struct dirent *de;
734 sprintf(path, "%s/pack", objdir);
735 len = strlen(path);
736 dir = opendir(path);
737 if (!dir) {
738 if (errno != ENOENT)
739 error("unable to open object pack directory: %s: %s",
740 path, strerror(errno));
741 return;
743 path[len++] = '/';
744 while ((de = readdir(dir)) != NULL) {
745 int namelen = strlen(de->d_name);
746 struct packed_git *p;
748 if (!has_extension(de->d_name, ".idx"))
749 continue;
751 /* we have .idx. Is it a file we can map? */
752 strcpy(path + len, de->d_name);
753 for (p = packed_git; p; p = p->next) {
754 if (!memcmp(path, p->pack_name, len + namelen - 4))
755 break;
757 if (p)
758 continue;
759 p = add_packed_git(path, len + namelen, local);
760 if (!p)
761 continue;
762 p->next = packed_git;
763 packed_git = p;
765 closedir(dir);
768 static int prepare_packed_git_run_once = 0;
769 void prepare_packed_git(void)
771 struct alternate_object_database *alt;
773 if (prepare_packed_git_run_once)
774 return;
775 prepare_packed_git_one(get_object_directory(), 1);
776 prepare_alt_odb();
777 for (alt = alt_odb_list; alt; alt = alt->next) {
778 alt->name[-1] = 0;
779 prepare_packed_git_one(alt->base, 0);
780 alt->name[-1] = '/';
782 prepare_packed_git_run_once = 1;
785 void reprepare_packed_git(void)
787 prepare_packed_git_run_once = 0;
788 prepare_packed_git();
791 int check_sha1_signature(const unsigned char *sha1, void *map, unsigned long size, const char *type)
793 unsigned char real_sha1[20];
794 hash_sha1_file(map, size, type, real_sha1);
795 return hashcmp(sha1, real_sha1) ? -1 : 0;
798 void *map_sha1_file(const unsigned char *sha1, unsigned long *size)
800 struct stat st;
801 void *map;
802 int fd;
803 char *filename = find_sha1_file(sha1, &st);
805 if (!filename) {
806 return NULL;
809 fd = open(filename, O_RDONLY | sha1_file_open_flag);
810 if (fd < 0) {
811 /* See if it works without O_NOATIME */
812 switch (sha1_file_open_flag) {
813 default:
814 fd = open(filename, O_RDONLY);
815 if (fd >= 0)
816 break;
817 /* Fallthrough */
818 case 0:
819 return NULL;
822 /* If it failed once, it will probably fail again.
823 * Stop using O_NOATIME
825 sha1_file_open_flag = 0;
827 map = xmmap(NULL, st.st_size, PROT_READ, MAP_PRIVATE, fd, 0);
828 close(fd);
829 *size = st.st_size;
830 return map;
833 int legacy_loose_object(unsigned char *map)
835 unsigned int word;
838 * Is it a zlib-compressed buffer? If so, the first byte
839 * must be 0x78 (15-bit window size, deflated), and the
840 * first 16-bit word is evenly divisible by 31
842 word = (map[0] << 8) + map[1];
843 if (map[0] == 0x78 && !(word % 31))
844 return 1;
845 else
846 return 0;
849 unsigned long unpack_object_header_gently(const unsigned char *buf, unsigned long len, enum object_type *type, unsigned long *sizep)
851 unsigned shift;
852 unsigned char c;
853 unsigned long size;
854 unsigned long used = 0;
856 c = buf[used++];
857 *type = (c >> 4) & 7;
858 size = c & 15;
859 shift = 4;
860 while (c & 0x80) {
861 if (len <= used)
862 return 0;
863 if (sizeof(long) * 8 <= shift)
864 return 0;
865 c = buf[used++];
866 size += (c & 0x7f) << shift;
867 shift += 7;
869 *sizep = size;
870 return used;
873 static int unpack_sha1_header(z_stream *stream, unsigned char *map, unsigned long mapsize, void *buffer, unsigned long bufsiz)
875 unsigned long size, used;
876 static const char valid_loose_object_type[8] = {
877 0, /* OBJ_EXT */
878 1, 1, 1, 1, /* "commit", "tree", "blob", "tag" */
879 0, /* "delta" and others are invalid in a loose object */
881 enum object_type type;
883 /* Get the data stream */
884 memset(stream, 0, sizeof(*stream));
885 stream->next_in = map;
886 stream->avail_in = mapsize;
887 stream->next_out = buffer;
888 stream->avail_out = bufsiz;
890 if (legacy_loose_object(map)) {
891 inflateInit(stream);
892 return inflate(stream, 0);
895 used = unpack_object_header_gently(map, mapsize, &type, &size);
896 if (!used || !valid_loose_object_type[type])
897 return -1;
898 map += used;
899 mapsize -= used;
901 /* Set up the stream for the rest.. */
902 stream->next_in = map;
903 stream->avail_in = mapsize;
904 inflateInit(stream);
906 /* And generate the fake traditional header */
907 stream->total_out = 1 + snprintf(buffer, bufsiz, "%s %lu",
908 type_names[type], size);
909 return 0;
912 static void *unpack_sha1_rest(z_stream *stream, void *buffer, unsigned long size)
914 int bytes = strlen(buffer) + 1;
915 unsigned char *buf = xmalloc(1+size);
916 unsigned long n;
918 n = stream->total_out - bytes;
919 if (n > size)
920 n = size;
921 memcpy(buf, (char *) buffer + bytes, n);
922 bytes = n;
923 if (bytes < size) {
924 stream->next_out = buf + bytes;
925 stream->avail_out = size - bytes;
926 while (inflate(stream, Z_FINISH) == Z_OK)
927 /* nothing */;
929 buf[size] = 0;
930 inflateEnd(stream);
931 return buf;
935 * We used to just use "sscanf()", but that's actually way
936 * too permissive for what we want to check. So do an anal
937 * object header parse by hand.
939 static int parse_sha1_header(char *hdr, char *type, unsigned long *sizep)
941 int i;
942 unsigned long size;
945 * The type can be at most ten bytes (including the
946 * terminating '\0' that we add), and is followed by
947 * a space.
949 i = 10;
950 for (;;) {
951 char c = *hdr++;
952 if (c == ' ')
953 break;
954 if (!--i)
955 return -1;
956 *type++ = c;
958 *type = 0;
961 * The length must follow immediately, and be in canonical
962 * decimal format (ie "010" is not valid).
964 size = *hdr++ - '0';
965 if (size > 9)
966 return -1;
967 if (size) {
968 for (;;) {
969 unsigned long c = *hdr - '0';
970 if (c > 9)
971 break;
972 hdr++;
973 size = size * 10 + c;
976 *sizep = size;
979 * The length must be followed by a zero byte
981 return *hdr ? -1 : 0;
984 void * unpack_sha1_file(void *map, unsigned long mapsize, char *type, unsigned long *size)
986 int ret;
987 z_stream stream;
988 char hdr[8192];
990 ret = unpack_sha1_header(&stream, map, mapsize, hdr, sizeof(hdr));
991 if (ret < Z_OK || parse_sha1_header(hdr, type, size) < 0)
992 return NULL;
994 return unpack_sha1_rest(&stream, hdr, *size);
997 static unsigned long get_delta_base(struct packed_git *p,
998 struct pack_window **w_curs,
999 unsigned long offset,
1000 enum object_type kind,
1001 unsigned long delta_obj_offset,
1002 unsigned long *base_obj_offset)
1004 unsigned char *base_info = use_pack(p, w_curs, offset, NULL);
1005 unsigned long base_offset;
1007 /* use_pack() assured us we have [base_info, base_info + 20)
1008 * as a range that we can look at without walking off the
1009 * end of the mapped window. Its actually the hash size
1010 * that is assured. An OFS_DELTA longer than the hash size
1011 * is stupid, as then a REF_DELTA would be smaller to store.
1013 if (kind == OBJ_OFS_DELTA) {
1014 unsigned used = 0;
1015 unsigned char c = base_info[used++];
1016 base_offset = c & 127;
1017 while (c & 128) {
1018 base_offset += 1;
1019 if (!base_offset || base_offset & ~(~0UL >> 7))
1020 die("offset value overflow for delta base object");
1021 c = base_info[used++];
1022 base_offset = (base_offset << 7) + (c & 127);
1024 base_offset = delta_obj_offset - base_offset;
1025 if (base_offset >= delta_obj_offset)
1026 die("delta base offset out of bound");
1027 offset += used;
1028 } else if (kind == OBJ_REF_DELTA) {
1029 /* The base entry _must_ be in the same pack */
1030 base_offset = find_pack_entry_one(base_info, p);
1031 if (!base_offset)
1032 die("failed to find delta-pack base object %s",
1033 sha1_to_hex(base_info));
1034 offset += 20;
1035 } else
1036 die("I am totally screwed");
1037 *base_obj_offset = base_offset;
1038 return offset;
1041 /* forward declaration for a mutually recursive function */
1042 static int packed_object_info(struct packed_git *p, unsigned long offset,
1043 char *type, unsigned long *sizep);
1045 static int packed_delta_info(struct packed_git *p,
1046 struct pack_window **w_curs,
1047 unsigned long offset,
1048 enum object_type kind,
1049 unsigned long obj_offset,
1050 char *type,
1051 unsigned long *sizep)
1053 unsigned long base_offset;
1055 offset = get_delta_base(p, w_curs, offset, kind,
1056 obj_offset, &base_offset);
1058 /* We choose to only get the type of the base object and
1059 * ignore potentially corrupt pack file that expects the delta
1060 * based on a base with a wrong size. This saves tons of
1061 * inflate() calls.
1063 if (packed_object_info(p, base_offset, type, NULL))
1064 die("cannot get info for delta-pack base");
1066 if (sizep) {
1067 const unsigned char *data;
1068 unsigned char delta_head[20], *in;
1069 unsigned long result_size;
1070 z_stream stream;
1071 int st;
1073 memset(&stream, 0, sizeof(stream));
1074 stream.next_out = delta_head;
1075 stream.avail_out = sizeof(delta_head);
1077 inflateInit(&stream);
1078 do {
1079 in = use_pack(p, w_curs, offset, &stream.avail_in);
1080 stream.next_in = in;
1081 st = inflate(&stream, Z_FINISH);
1082 offset += stream.next_in - in;
1083 } while ((st == Z_OK || st == Z_BUF_ERROR)
1084 && stream.total_out < sizeof(delta_head));
1085 inflateEnd(&stream);
1086 if ((st != Z_STREAM_END) &&
1087 stream.total_out != sizeof(delta_head))
1088 die("delta data unpack-initial failed");
1090 /* Examine the initial part of the delta to figure out
1091 * the result size.
1093 data = delta_head;
1095 /* ignore base size */
1096 get_delta_hdr_size(&data, delta_head+sizeof(delta_head));
1098 /* Read the result size */
1099 result_size = get_delta_hdr_size(&data, delta_head+sizeof(delta_head));
1100 *sizep = result_size;
1102 return 0;
1105 static unsigned long unpack_object_header(struct packed_git *p,
1106 struct pack_window **w_curs,
1107 unsigned long offset,
1108 enum object_type *type,
1109 unsigned long *sizep)
1111 unsigned char *base;
1112 unsigned int left;
1113 unsigned long used;
1115 /* use_pack() assures us we have [base, base + 20) available
1116 * as a range that we can look at at. (Its actually the hash
1117 * size that is assurred.) With our object header encoding
1118 * the maximum deflated object size is 2^137, which is just
1119 * insane, so we know won't exceed what we have been given.
1121 base = use_pack(p, w_curs, offset, &left);
1122 used = unpack_object_header_gently(base, left, type, sizep);
1123 if (!used)
1124 die("object offset outside of pack file");
1126 return offset + used;
1129 void packed_object_info_detail(struct packed_git *p,
1130 unsigned long offset,
1131 char *type,
1132 unsigned long *size,
1133 unsigned long *store_size,
1134 unsigned int *delta_chain_length,
1135 unsigned char *base_sha1)
1137 struct pack_window *w_curs = NULL;
1138 unsigned long obj_offset, val;
1139 unsigned char *next_sha1;
1140 enum object_type kind;
1142 *delta_chain_length = 0;
1143 obj_offset = offset;
1144 offset = unpack_object_header(p, &w_curs, offset, &kind, size);
1146 for (;;) {
1147 switch (kind) {
1148 default:
1149 die("pack %s contains unknown object type %d",
1150 p->pack_name, kind);
1151 case OBJ_COMMIT:
1152 case OBJ_TREE:
1153 case OBJ_BLOB:
1154 case OBJ_TAG:
1155 strcpy(type, type_names[kind]);
1156 *store_size = 0; /* notyet */
1157 unuse_pack(&w_curs);
1158 return;
1159 case OBJ_OFS_DELTA:
1160 get_delta_base(p, &w_curs, offset, kind,
1161 obj_offset, &offset);
1162 if (*delta_chain_length == 0) {
1163 /* TODO: find base_sha1 as pointed by offset */
1165 break;
1166 case OBJ_REF_DELTA:
1167 next_sha1 = use_pack(p, &w_curs, offset, NULL);
1168 if (*delta_chain_length == 0)
1169 hashcpy(base_sha1, next_sha1);
1170 offset = find_pack_entry_one(next_sha1, p);
1171 break;
1173 obj_offset = offset;
1174 offset = unpack_object_header(p, &w_curs, offset, &kind, &val);
1175 (*delta_chain_length)++;
1179 static int packed_object_info(struct packed_git *p, unsigned long offset,
1180 char *type, unsigned long *sizep)
1182 struct pack_window *w_curs = NULL;
1183 unsigned long size, obj_offset = offset;
1184 enum object_type kind;
1185 int r;
1187 offset = unpack_object_header(p, &w_curs, offset, &kind, &size);
1189 switch (kind) {
1190 case OBJ_OFS_DELTA:
1191 case OBJ_REF_DELTA:
1192 r = packed_delta_info(p, &w_curs, offset, kind,
1193 obj_offset, type, sizep);
1194 unuse_pack(&w_curs);
1195 return r;
1196 case OBJ_COMMIT:
1197 case OBJ_TREE:
1198 case OBJ_BLOB:
1199 case OBJ_TAG:
1200 strcpy(type, type_names[kind]);
1201 unuse_pack(&w_curs);
1202 break;
1203 default:
1204 die("pack %s contains unknown object type %d",
1205 p->pack_name, kind);
1207 if (sizep)
1208 *sizep = size;
1209 return 0;
1212 static void *unpack_compressed_entry(struct packed_git *p,
1213 struct pack_window **w_curs,
1214 unsigned long offset,
1215 unsigned long size)
1217 int st;
1218 z_stream stream;
1219 unsigned char *buffer, *in;
1221 buffer = xmalloc(size + 1);
1222 buffer[size] = 0;
1223 memset(&stream, 0, sizeof(stream));
1224 stream.next_out = buffer;
1225 stream.avail_out = size;
1227 inflateInit(&stream);
1228 do {
1229 in = use_pack(p, w_curs, offset, &stream.avail_in);
1230 stream.next_in = in;
1231 st = inflate(&stream, Z_FINISH);
1232 offset += stream.next_in - in;
1233 } while (st == Z_OK || st == Z_BUF_ERROR);
1234 inflateEnd(&stream);
1235 if ((st != Z_STREAM_END) || stream.total_out != size) {
1236 free(buffer);
1237 return NULL;
1240 return buffer;
1243 static void *unpack_delta_entry(struct packed_git *p,
1244 struct pack_window **w_curs,
1245 unsigned long offset,
1246 unsigned long delta_size,
1247 enum object_type kind,
1248 unsigned long obj_offset,
1249 char *type,
1250 unsigned long *sizep)
1252 void *delta_data, *result, *base;
1253 unsigned long result_size, base_size, base_offset;
1255 offset = get_delta_base(p, w_curs, offset, kind,
1256 obj_offset, &base_offset);
1257 base = unpack_entry(p, base_offset, type, &base_size);
1258 if (!base)
1259 die("failed to read delta base object at %lu from %s",
1260 base_offset, p->pack_name);
1262 delta_data = unpack_compressed_entry(p, w_curs, offset, delta_size);
1263 result = patch_delta(base, base_size,
1264 delta_data, delta_size,
1265 &result_size);
1266 if (!result)
1267 die("failed to apply delta");
1268 free(delta_data);
1269 free(base);
1270 *sizep = result_size;
1271 return result;
1274 void *unpack_entry(struct packed_git *p, unsigned long offset,
1275 char *type, unsigned long *sizep)
1277 struct pack_window *w_curs = NULL;
1278 unsigned long size, obj_offset = offset;
1279 enum object_type kind;
1280 void *retval;
1282 offset = unpack_object_header(p, &w_curs, offset, &kind, &size);
1283 switch (kind) {
1284 case OBJ_OFS_DELTA:
1285 case OBJ_REF_DELTA:
1286 retval = unpack_delta_entry(p, &w_curs, offset, size,
1287 kind, obj_offset, type, sizep);
1288 break;
1289 case OBJ_COMMIT:
1290 case OBJ_TREE:
1291 case OBJ_BLOB:
1292 case OBJ_TAG:
1293 strcpy(type, type_names[kind]);
1294 *sizep = size;
1295 retval = unpack_compressed_entry(p, &w_curs, offset, size);
1296 break;
1297 default:
1298 die("unknown object type %i in %s", kind, p->pack_name);
1300 unuse_pack(&w_curs);
1301 return retval;
1304 int num_packed_objects(const struct packed_git *p)
1306 /* See check_packed_git_idx() */
1307 return (p->index_size - 20 - 20 - 4*256) / 24;
1310 int nth_packed_object_sha1(const struct packed_git *p, int n,
1311 unsigned char* sha1)
1313 void *index = p->index_base + 256;
1314 if (n < 0 || num_packed_objects(p) <= n)
1315 return -1;
1316 hashcpy(sha1, (unsigned char *) index + (24 * n) + 4);
1317 return 0;
1320 unsigned long find_pack_entry_one(const unsigned char *sha1,
1321 struct packed_git *p)
1323 unsigned int *level1_ofs = p->index_base;
1324 int hi = ntohl(level1_ofs[*sha1]);
1325 int lo = ((*sha1 == 0x0) ? 0 : ntohl(level1_ofs[*sha1 - 1]));
1326 void *index = p->index_base + 256;
1328 do {
1329 int mi = (lo + hi) / 2;
1330 int cmp = hashcmp((unsigned char *)index + (24 * mi) + 4, sha1);
1331 if (!cmp)
1332 return ntohl(*((unsigned int *) ((char *) index + (24 * mi))));
1333 if (cmp > 0)
1334 hi = mi;
1335 else
1336 lo = mi+1;
1337 } while (lo < hi);
1338 return 0;
1341 static int matches_pack_name(struct packed_git *p, const char *ig)
1343 const char *last_c, *c;
1345 if (!strcmp(p->pack_name, ig))
1346 return 0;
1348 for (c = p->pack_name, last_c = c; *c;)
1349 if (*c == '/')
1350 last_c = ++c;
1351 else
1352 ++c;
1353 if (!strcmp(last_c, ig))
1354 return 0;
1356 return 1;
1359 static int find_pack_entry(const unsigned char *sha1, struct pack_entry *e, const char **ignore_packed)
1361 struct packed_git *p;
1362 unsigned long offset;
1364 prepare_packed_git();
1366 for (p = packed_git; p; p = p->next) {
1367 if (ignore_packed) {
1368 const char **ig;
1369 for (ig = ignore_packed; *ig; ig++)
1370 if (!matches_pack_name(p, *ig))
1371 break;
1372 if (*ig)
1373 continue;
1375 offset = find_pack_entry_one(sha1, p);
1376 if (offset) {
1377 e->offset = offset;
1378 e->p = p;
1379 hashcpy(e->sha1, sha1);
1380 return 1;
1383 return 0;
1386 struct packed_git *find_sha1_pack(const unsigned char *sha1,
1387 struct packed_git *packs)
1389 struct packed_git *p;
1391 for (p = packs; p; p = p->next) {
1392 if (find_pack_entry_one(sha1, p))
1393 return p;
1395 return NULL;
1399 static int sha1_loose_object_info(const unsigned char *sha1, char *type, unsigned long *sizep)
1401 int status;
1402 unsigned long mapsize, size;
1403 void *map;
1404 z_stream stream;
1405 char hdr[128];
1407 map = map_sha1_file(sha1, &mapsize);
1408 if (!map)
1409 return error("unable to find %s", sha1_to_hex(sha1));
1410 if (unpack_sha1_header(&stream, map, mapsize, hdr, sizeof(hdr)) < 0)
1411 status = error("unable to unpack %s header",
1412 sha1_to_hex(sha1));
1413 if (parse_sha1_header(hdr, type, &size) < 0)
1414 status = error("unable to parse %s header", sha1_to_hex(sha1));
1415 else {
1416 status = 0;
1417 if (sizep)
1418 *sizep = size;
1420 inflateEnd(&stream);
1421 munmap(map, mapsize);
1422 return status;
1425 int sha1_object_info(const unsigned char *sha1, char *type, unsigned long *sizep)
1427 struct pack_entry e;
1429 if (!find_pack_entry(sha1, &e, NULL)) {
1430 reprepare_packed_git();
1431 if (!find_pack_entry(sha1, &e, NULL))
1432 return sha1_loose_object_info(sha1, type, sizep);
1434 return packed_object_info(e.p, e.offset, type, sizep);
1437 static void *read_packed_sha1(const unsigned char *sha1, char *type, unsigned long *size)
1439 struct pack_entry e;
1441 if (!find_pack_entry(sha1, &e, NULL)) {
1442 error("cannot read sha1_file for %s", sha1_to_hex(sha1));
1443 return NULL;
1445 return unpack_entry(e.p, e.offset, type, size);
1448 void * read_sha1_file(const unsigned char *sha1, char *type, unsigned long *size)
1450 unsigned long mapsize;
1451 void *map, *buf;
1452 struct pack_entry e;
1454 if (find_pack_entry(sha1, &e, NULL))
1455 return read_packed_sha1(sha1, type, size);
1456 map = map_sha1_file(sha1, &mapsize);
1457 if (map) {
1458 buf = unpack_sha1_file(map, mapsize, type, size);
1459 munmap(map, mapsize);
1460 return buf;
1462 reprepare_packed_git();
1463 if (find_pack_entry(sha1, &e, NULL))
1464 return read_packed_sha1(sha1, type, size);
1465 return NULL;
1468 void *read_object_with_reference(const unsigned char *sha1,
1469 const char *required_type,
1470 unsigned long *size,
1471 unsigned char *actual_sha1_return)
1473 char type[20];
1474 void *buffer;
1475 unsigned long isize;
1476 unsigned char actual_sha1[20];
1478 hashcpy(actual_sha1, sha1);
1479 while (1) {
1480 int ref_length = -1;
1481 const char *ref_type = NULL;
1483 buffer = read_sha1_file(actual_sha1, type, &isize);
1484 if (!buffer)
1485 return NULL;
1486 if (!strcmp(type, required_type)) {
1487 *size = isize;
1488 if (actual_sha1_return)
1489 hashcpy(actual_sha1_return, actual_sha1);
1490 return buffer;
1492 /* Handle references */
1493 else if (!strcmp(type, commit_type))
1494 ref_type = "tree ";
1495 else if (!strcmp(type, tag_type))
1496 ref_type = "object ";
1497 else {
1498 free(buffer);
1499 return NULL;
1501 ref_length = strlen(ref_type);
1503 if (memcmp(buffer, ref_type, ref_length) ||
1504 get_sha1_hex((char *) buffer + ref_length, actual_sha1)) {
1505 free(buffer);
1506 return NULL;
1508 free(buffer);
1509 /* Now we have the ID of the referred-to object in
1510 * actual_sha1. Check again. */
1514 static void write_sha1_file_prepare(void *buf, unsigned long len,
1515 const char *type, unsigned char *sha1,
1516 unsigned char *hdr, int *hdrlen)
1518 SHA_CTX c;
1520 /* Generate the header */
1521 *hdrlen = sprintf((char *)hdr, "%s %lu", type, len)+1;
1523 /* Sha1.. */
1524 SHA1_Init(&c);
1525 SHA1_Update(&c, hdr, *hdrlen);
1526 SHA1_Update(&c, buf, len);
1527 SHA1_Final(sha1, &c);
1531 * Link the tempfile to the final place, possibly creating the
1532 * last directory level as you do so.
1534 * Returns the errno on failure, 0 on success.
1536 static int link_temp_to_file(const char *tmpfile, const char *filename)
1538 int ret;
1539 char *dir;
1541 if (!link(tmpfile, filename))
1542 return 0;
1545 * Try to mkdir the last path component if that failed.
1547 * Re-try the "link()" regardless of whether the mkdir
1548 * succeeds, since a race might mean that somebody
1549 * else succeeded.
1551 ret = errno;
1552 dir = strrchr(filename, '/');
1553 if (dir) {
1554 *dir = 0;
1555 if (!mkdir(filename, 0777) && adjust_shared_perm(filename)) {
1556 *dir = '/';
1557 return -2;
1559 *dir = '/';
1560 if (!link(tmpfile, filename))
1561 return 0;
1562 ret = errno;
1564 return ret;
1568 * Move the just written object into its final resting place
1570 int move_temp_to_file(const char *tmpfile, const char *filename)
1572 int ret = link_temp_to_file(tmpfile, filename);
1575 * Coda hack - coda doesn't like cross-directory links,
1576 * so we fall back to a rename, which will mean that it
1577 * won't be able to check collisions, but that's not a
1578 * big deal.
1580 * The same holds for FAT formatted media.
1582 * When this succeeds, we just return 0. We have nothing
1583 * left to unlink.
1585 if (ret && ret != EEXIST) {
1586 if (!rename(tmpfile, filename))
1587 return 0;
1588 ret = errno;
1590 unlink(tmpfile);
1591 if (ret) {
1592 if (ret != EEXIST) {
1593 return error("unable to write sha1 filename %s: %s\n", filename, strerror(ret));
1595 /* FIXME!!! Collision check here ? */
1598 return 0;
1601 static int write_buffer(int fd, const void *buf, size_t len)
1603 while (len) {
1604 ssize_t size;
1606 size = write(fd, buf, len);
1607 if (!size)
1608 return error("file write: disk full");
1609 if (size < 0) {
1610 if (errno == EINTR || errno == EAGAIN)
1611 continue;
1612 return error("file write error (%s)", strerror(errno));
1614 len -= size;
1615 buf = (char *) buf + size;
1617 return 0;
1620 static int write_binary_header(unsigned char *hdr, enum object_type type, unsigned long len)
1622 int hdr_len;
1623 unsigned char c;
1625 c = (type << 4) | (len & 15);
1626 len >>= 4;
1627 hdr_len = 1;
1628 while (len) {
1629 *hdr++ = c | 0x80;
1630 hdr_len++;
1631 c = (len & 0x7f);
1632 len >>= 7;
1634 *hdr = c;
1635 return hdr_len;
1638 static void setup_object_header(z_stream *stream, const char *type, unsigned long len)
1640 int obj_type, hdr;
1642 if (use_legacy_headers) {
1643 while (deflate(stream, 0) == Z_OK)
1644 /* nothing */;
1645 return;
1647 if (!strcmp(type, blob_type))
1648 obj_type = OBJ_BLOB;
1649 else if (!strcmp(type, tree_type))
1650 obj_type = OBJ_TREE;
1651 else if (!strcmp(type, commit_type))
1652 obj_type = OBJ_COMMIT;
1653 else if (!strcmp(type, tag_type))
1654 obj_type = OBJ_TAG;
1655 else
1656 die("trying to generate bogus object of type '%s'", type);
1657 hdr = write_binary_header(stream->next_out, obj_type, len);
1658 stream->total_out = hdr;
1659 stream->next_out += hdr;
1660 stream->avail_out -= hdr;
1663 int hash_sha1_file(void *buf, unsigned long len, const char *type,
1664 unsigned char *sha1)
1666 unsigned char hdr[50];
1667 int hdrlen;
1668 write_sha1_file_prepare(buf, len, type, sha1, hdr, &hdrlen);
1669 return 0;
1672 int write_sha1_file(void *buf, unsigned long len, const char *type, unsigned char *returnsha1)
1674 int size;
1675 unsigned char *compressed;
1676 z_stream stream;
1677 unsigned char sha1[20];
1678 char *filename;
1679 static char tmpfile[PATH_MAX];
1680 unsigned char hdr[50];
1681 int fd, hdrlen;
1683 /* Normally if we have it in the pack then we do not bother writing
1684 * it out into .git/objects/??/?{38} file.
1686 write_sha1_file_prepare(buf, len, type, sha1, hdr, &hdrlen);
1687 filename = sha1_file_name(sha1);
1688 if (returnsha1)
1689 hashcpy(returnsha1, sha1);
1690 if (has_sha1_file(sha1))
1691 return 0;
1692 fd = open(filename, O_RDONLY);
1693 if (fd >= 0) {
1695 * FIXME!!! We might do collision checking here, but we'd
1696 * need to uncompress the old file and check it. Later.
1698 close(fd);
1699 return 0;
1702 if (errno != ENOENT) {
1703 return error("sha1 file %s: %s\n", filename, strerror(errno));
1706 snprintf(tmpfile, sizeof(tmpfile), "%s/obj_XXXXXX", get_object_directory());
1708 fd = mkstemp(tmpfile);
1709 if (fd < 0) {
1710 if (errno == EPERM)
1711 return error("insufficient permission for adding an object to repository database %s\n", get_object_directory());
1712 else
1713 return error("unable to create temporary sha1 filename %s: %s\n", tmpfile, strerror(errno));
1716 /* Set it up */
1717 memset(&stream, 0, sizeof(stream));
1718 deflateInit(&stream, zlib_compression_level);
1719 size = 8 + deflateBound(&stream, len+hdrlen);
1720 compressed = xmalloc(size);
1722 /* Compress it */
1723 stream.next_out = compressed;
1724 stream.avail_out = size;
1726 /* First header.. */
1727 stream.next_in = hdr;
1728 stream.avail_in = hdrlen;
1729 setup_object_header(&stream, type, len);
1731 /* Then the data itself.. */
1732 stream.next_in = buf;
1733 stream.avail_in = len;
1734 while (deflate(&stream, Z_FINISH) == Z_OK)
1735 /* nothing */;
1736 deflateEnd(&stream);
1737 size = stream.total_out;
1739 if (write_buffer(fd, compressed, size) < 0)
1740 die("unable to write sha1 file");
1741 fchmod(fd, 0444);
1742 close(fd);
1743 free(compressed);
1745 return move_temp_to_file(tmpfile, filename);
1749 * We need to unpack and recompress the object for writing
1750 * it out to a different file.
1752 static void *repack_object(const unsigned char *sha1, unsigned long *objsize)
1754 size_t size;
1755 z_stream stream;
1756 unsigned char *unpacked;
1757 unsigned long len;
1758 char type[20];
1759 char hdr[50];
1760 int hdrlen;
1761 void *buf;
1763 /* need to unpack and recompress it by itself */
1764 unpacked = read_packed_sha1(sha1, type, &len);
1766 hdrlen = sprintf(hdr, "%s %lu", type, len) + 1;
1768 /* Set it up */
1769 memset(&stream, 0, sizeof(stream));
1770 deflateInit(&stream, zlib_compression_level);
1771 size = deflateBound(&stream, len + hdrlen);
1772 buf = xmalloc(size);
1774 /* Compress it */
1775 stream.next_out = buf;
1776 stream.avail_out = size;
1778 /* First header.. */
1779 stream.next_in = (void *)hdr;
1780 stream.avail_in = hdrlen;
1781 while (deflate(&stream, 0) == Z_OK)
1782 /* nothing */;
1784 /* Then the data itself.. */
1785 stream.next_in = unpacked;
1786 stream.avail_in = len;
1787 while (deflate(&stream, Z_FINISH) == Z_OK)
1788 /* nothing */;
1789 deflateEnd(&stream);
1790 free(unpacked);
1792 *objsize = stream.total_out;
1793 return buf;
1796 int write_sha1_to_fd(int fd, const unsigned char *sha1)
1798 int retval;
1799 unsigned long objsize;
1800 void *buf = map_sha1_file(sha1, &objsize);
1802 if (buf) {
1803 retval = write_buffer(fd, buf, objsize);
1804 munmap(buf, objsize);
1805 return retval;
1808 buf = repack_object(sha1, &objsize);
1809 retval = write_buffer(fd, buf, objsize);
1810 free(buf);
1811 return retval;
1814 int write_sha1_from_fd(const unsigned char *sha1, int fd, char *buffer,
1815 size_t bufsize, size_t *bufposn)
1817 char tmpfile[PATH_MAX];
1818 int local;
1819 z_stream stream;
1820 unsigned char real_sha1[20];
1821 unsigned char discard[4096];
1822 int ret;
1823 SHA_CTX c;
1825 snprintf(tmpfile, sizeof(tmpfile), "%s/obj_XXXXXX", get_object_directory());
1827 local = mkstemp(tmpfile);
1828 if (local < 0) {
1829 if (errno == EPERM)
1830 return error("insufficient permission for adding an object to repository database %s\n", get_object_directory());
1831 else
1832 return error("unable to create temporary sha1 filename %s: %s\n", tmpfile, strerror(errno));
1835 memset(&stream, 0, sizeof(stream));
1837 inflateInit(&stream);
1839 SHA1_Init(&c);
1841 do {
1842 ssize_t size;
1843 if (*bufposn) {
1844 stream.avail_in = *bufposn;
1845 stream.next_in = (unsigned char *) buffer;
1846 do {
1847 stream.next_out = discard;
1848 stream.avail_out = sizeof(discard);
1849 ret = inflate(&stream, Z_SYNC_FLUSH);
1850 SHA1_Update(&c, discard, sizeof(discard) -
1851 stream.avail_out);
1852 } while (stream.avail_in && ret == Z_OK);
1853 if (write_buffer(local, buffer, *bufposn - stream.avail_in) < 0)
1854 die("unable to write sha1 file");
1855 memmove(buffer, buffer + *bufposn - stream.avail_in,
1856 stream.avail_in);
1857 *bufposn = stream.avail_in;
1858 if (ret != Z_OK)
1859 break;
1861 size = read(fd, buffer + *bufposn, bufsize - *bufposn);
1862 if (size <= 0) {
1863 close(local);
1864 unlink(tmpfile);
1865 if (!size)
1866 return error("Connection closed?");
1867 perror("Reading from connection");
1868 return -1;
1870 *bufposn += size;
1871 } while (1);
1872 inflateEnd(&stream);
1874 close(local);
1875 SHA1_Final(real_sha1, &c);
1876 if (ret != Z_STREAM_END) {
1877 unlink(tmpfile);
1878 return error("File %s corrupted", sha1_to_hex(sha1));
1880 if (hashcmp(sha1, real_sha1)) {
1881 unlink(tmpfile);
1882 return error("File %s has bad hash", sha1_to_hex(sha1));
1885 return move_temp_to_file(tmpfile, sha1_file_name(sha1));
1888 int has_pack_index(const unsigned char *sha1)
1890 struct stat st;
1891 if (stat(sha1_pack_index_name(sha1), &st))
1892 return 0;
1893 return 1;
1896 int has_pack_file(const unsigned char *sha1)
1898 struct stat st;
1899 if (stat(sha1_pack_name(sha1), &st))
1900 return 0;
1901 return 1;
1904 int has_sha1_pack(const unsigned char *sha1, const char **ignore_packed)
1906 struct pack_entry e;
1907 return find_pack_entry(sha1, &e, ignore_packed);
1910 int has_sha1_file(const unsigned char *sha1)
1912 struct stat st;
1913 struct pack_entry e;
1915 if (find_pack_entry(sha1, &e, NULL))
1916 return 1;
1917 return find_sha1_file(sha1, &st) ? 1 : 0;
1921 * reads from fd as long as possible into a supplied buffer of size bytes.
1922 * If necessary the buffer's size is increased using realloc()
1924 * returns 0 if anything went fine and -1 otherwise
1926 * NOTE: both buf and size may change, but even when -1 is returned
1927 * you still have to free() it yourself.
1929 int read_pipe(int fd, char** return_buf, unsigned long* return_size)
1931 char* buf = *return_buf;
1932 unsigned long size = *return_size;
1933 int iret;
1934 unsigned long off = 0;
1936 do {
1937 iret = xread(fd, buf + off, size - off);
1938 if (iret > 0) {
1939 off += iret;
1940 if (off == size) {
1941 size *= 2;
1942 buf = xrealloc(buf, size);
1945 } while (iret > 0);
1947 *return_buf = buf;
1948 *return_size = off;
1950 if (iret < 0)
1951 return -1;
1952 return 0;
1955 int index_pipe(unsigned char *sha1, int fd, const char *type, int write_object)
1957 unsigned long size = 4096;
1958 char *buf = xmalloc(size);
1959 int ret;
1961 if (read_pipe(fd, &buf, &size)) {
1962 free(buf);
1963 return -1;
1966 if (!type)
1967 type = blob_type;
1968 if (write_object)
1969 ret = write_sha1_file(buf, size, type, sha1);
1970 else
1971 ret = hash_sha1_file(buf, size, type, sha1);
1972 free(buf);
1973 return ret;
1976 int index_fd(unsigned char *sha1, int fd, struct stat *st, int write_object, const char *type)
1978 unsigned long size = st->st_size;
1979 void *buf;
1980 int ret;
1982 buf = "";
1983 if (size)
1984 buf = xmmap(NULL, size, PROT_READ, MAP_PRIVATE, fd, 0);
1985 close(fd);
1987 if (!type)
1988 type = blob_type;
1989 if (write_object)
1990 ret = write_sha1_file(buf, size, type, sha1);
1991 else
1992 ret = hash_sha1_file(buf, size, type, sha1);
1993 if (size)
1994 munmap(buf, size);
1995 return ret;
1998 int index_path(unsigned char *sha1, const char *path, struct stat *st, int write_object)
2000 int fd;
2001 char *target;
2003 switch (st->st_mode & S_IFMT) {
2004 case S_IFREG:
2005 fd = open(path, O_RDONLY);
2006 if (fd < 0)
2007 return error("open(\"%s\"): %s", path,
2008 strerror(errno));
2009 if (index_fd(sha1, fd, st, write_object, NULL) < 0)
2010 return error("%s: failed to insert into database",
2011 path);
2012 break;
2013 case S_IFLNK:
2014 target = xmalloc(st->st_size+1);
2015 if (readlink(path, target, st->st_size+1) != st->st_size) {
2016 char *errstr = strerror(errno);
2017 free(target);
2018 return error("readlink(\"%s\"): %s", path,
2019 errstr);
2021 if (!write_object)
2022 hash_sha1_file(target, st->st_size, blob_type, sha1);
2023 else if (write_sha1_file(target, st->st_size, blob_type, sha1))
2024 return error("%s: failed to insert into database",
2025 path);
2026 free(target);
2027 break;
2028 default:
2029 return error("%s: unsupported file type", path);
2031 return 0;