1 /*
2  * Copyright (C) the libgit2 contributors. All rights reserved.
3  *
4  * This file is part of libgit2, distributed under the GNU GPL v2 with
5  * a Linking Exception. For full terms see the included COPYING file.
6  */
7 
8 #include "pack.h"
9 
10 #include "odb.h"
11 #include "delta.h"
12 #include "sha1_lookup.h"
13 #include "mwindow.h"
14 #include "fileops.h"
15 #include "oid.h"
16 
17 #include <zlib.h>
18 
19 static int packfile_open(struct git_pack_file *p);
20 static git_off_t nth_packed_object_offset(const struct git_pack_file *p, uint32_t n);
21 static int packfile_unpack_compressed(
22 		git_rawobj *obj,
23 		struct git_pack_file *p,
24 		git_mwindow **w_curs,
25 		git_off_t *curpos,
26 		size_t size,
27 		git_otype type);
28 
29 /* Can find the offset of an object given
30  * a prefix of an identifier.
31  * Throws GIT_EAMBIGUOUSOIDPREFIX if short oid
32  * is ambiguous within the pack.
33  * This method assumes that len is between
34  * GIT_OID_MINPREFIXLEN and GIT_OID_HEXSZ.
35  */
36 static int pack_entry_find_offset(
37 		git_off_t *offset_out,
38 		git_oid *found_oid,
39 		struct git_pack_file *p,
40 		const git_oid *short_oid,
41 		size_t len);
42 
packfile_error(const char * message)43 static int packfile_error(const char *message)
44 {
45 	giterr_set(GITERR_ODB, "invalid pack file - %s", message);
46 	return -1;
47 }
48 
49 /********************
50  * Delta base cache
51  ********************/
52 
new_cache_object(git_rawobj * source)53 static git_pack_cache_entry *new_cache_object(git_rawobj *source)
54 {
55 	git_pack_cache_entry *e = git__calloc(1, sizeof(git_pack_cache_entry));
56 	if (!e)
57 		return NULL;
58 
59 	git_atomic_inc(&e->refcount);
60 	memcpy(&e->raw, source, sizeof(git_rawobj));
61 
62 	return e;
63 }
64 
free_cache_object(void * o)65 static void free_cache_object(void *o)
66 {
67 	git_pack_cache_entry *e = (git_pack_cache_entry *)o;
68 
69 	if (e != NULL) {
70 		assert(e->refcount.val == 0);
71 		git__free(e->raw.data);
72 		git__free(e);
73 	}
74 }
75 
cache_free(git_pack_cache * cache)76 static void cache_free(git_pack_cache *cache)
77 {
78 	git_pack_cache_entry *entry;
79 
80 	if (cache->entries) {
81 		git_offmap_foreach_value(cache->entries, entry, {
82 			free_cache_object(entry);
83 		});
84 
85 		git_offmap_free(cache->entries);
86 		cache->entries = NULL;
87 	}
88 }
89 
cache_init(git_pack_cache * cache)90 static int cache_init(git_pack_cache *cache)
91 {
92 	cache->entries = git_offmap_alloc();
93 	GITERR_CHECK_ALLOC(cache->entries);
94 
95 	cache->memory_limit = GIT_PACK_CACHE_MEMORY_LIMIT;
96 
97 	if (git_mutex_init(&cache->lock)) {
98 		giterr_set(GITERR_OS, "failed to initialize pack cache mutex");
99 
100 		git__free(cache->entries);
101 		cache->entries = NULL;
102 
103 		return -1;
104 	}
105 
106 	return 0;
107 }
108 
cache_get(git_pack_cache * cache,git_off_t offset)109 static git_pack_cache_entry *cache_get(git_pack_cache *cache, git_off_t offset)
110 {
111 	khiter_t k;
112 	git_pack_cache_entry *entry = NULL;
113 
114 	if (git_mutex_lock(&cache->lock) < 0)
115 		return NULL;
116 
117 	k = git_offmap_lookup_index(cache->entries, offset);
118 	if (git_offmap_valid_index(cache->entries, k)) { /* found it */
119 		entry = git_offmap_value_at(cache->entries, k);
120 		git_atomic_inc(&entry->refcount);
121 		entry->last_usage = cache->use_ctr++;
122 	}
123 	git_mutex_unlock(&cache->lock);
124 
125 	return entry;
126 }
127 
128 /* Run with the cache lock held */
free_lowest_entry(git_pack_cache * cache)129 static void free_lowest_entry(git_pack_cache *cache)
130 {
131 	git_off_t offset;
132 	git_pack_cache_entry *entry;
133 
134 	git_offmap_foreach(cache->entries, offset, entry, {
135 		if (entry && entry->refcount.val == 0) {
136 			cache->memory_used -= entry->raw.len;
137 			git_offmap_delete(cache->entries, offset);
138 			free_cache_object(entry);
139 		}
140 	});
141 }
142 
cache_add(git_pack_cache_entry ** cached_out,git_pack_cache * cache,git_rawobj * base,git_off_t offset)143 static int cache_add(
144 		git_pack_cache_entry **cached_out,
145 		git_pack_cache *cache,
146 		git_rawobj *base,
147 		git_off_t offset)
148 {
149 	git_pack_cache_entry *entry;
150 	int error, exists = 0;
151 	khiter_t k;
152 
153 	if (base->len > GIT_PACK_CACHE_SIZE_LIMIT)
154 		return -1;
155 
156 	entry = new_cache_object(base);
157 	if (entry) {
158 		if (git_mutex_lock(&cache->lock) < 0) {
159 			giterr_set(GITERR_OS, "failed to lock cache");
160 			git__free(entry);
161 			return -1;
162 		}
163 		/* Add it to the cache if nobody else has */
164 		exists = git_offmap_exists(cache->entries, offset);
165 		if (!exists) {
166 			while (cache->memory_used + base->len > cache->memory_limit)
167 				free_lowest_entry(cache);
168 
169 			k = git_offmap_put(cache->entries, offset, &error);
170 			assert(error != 0);
171 			git_offmap_set_value_at(cache->entries, k, entry);
172 			cache->memory_used += entry->raw.len;
173 
174 			*cached_out = entry;
175 		}
176 		git_mutex_unlock(&cache->lock);
177 		/* Somebody beat us to adding it into the cache */
178 		if (exists) {
179 			git__free(entry);
180 			return -1;
181 		}
182 	}
183 
184 	return 0;
185 }
186 
187 /***********************************************************
188  *
189  * PACK INDEX METHODS
190  *
191  ***********************************************************/
192 
pack_index_free(struct git_pack_file * p)193 static void pack_index_free(struct git_pack_file *p)
194 {
195 	if (p->oids) {
196 		git__free(p->oids);
197 		p->oids = NULL;
198 	}
199 	if (p->index_map.data) {
200 		git_futils_mmap_free(&p->index_map);
201 		p->index_map.data = NULL;
202 	}
203 }
204 
pack_index_check(const char * path,struct git_pack_file * p)205 static int pack_index_check(const char *path, struct git_pack_file *p)
206 {
207 	struct git_pack_idx_header *hdr;
208 	uint32_t version, nr, i, *index;
209 	void *idx_map;
210 	size_t idx_size;
211 	struct stat st;
212 	int error;
213 	/* TODO: properly open the file without access time using O_NOATIME */
214 	git_file fd = git_futils_open_ro(path);
215 	if (fd < 0)
216 		return fd;
217 
218 	if (p_fstat(fd, &st) < 0) {
219 		p_close(fd);
220 		giterr_set(GITERR_OS, "unable to stat pack index '%s'", path);
221 		return -1;
222 	}
223 
224 	if (!S_ISREG(st.st_mode) ||
225 		!git__is_sizet(st.st_size) ||
226 		(idx_size = (size_t)st.st_size) < 4 * 256 + 20 + 20)
227 	{
228 		p_close(fd);
229 		giterr_set(GITERR_ODB, "invalid pack index '%s'", path);
230 		return -1;
231 	}
232 
233 	error = git_futils_mmap_ro(&p->index_map, fd, 0, idx_size);
234 
235 	p_close(fd);
236 
237 	if (error < 0)
238 		return error;
239 
240 	hdr = idx_map = p->index_map.data;
241 
242 	if (hdr->idx_signature == htonl(PACK_IDX_SIGNATURE)) {
243 		version = ntohl(hdr->idx_version);
244 
245 		if (version < 2 || version > 2) {
246 			git_futils_mmap_free(&p->index_map);
247 			return packfile_error("unsupported index version");
248 		}
249 
250 	} else
251 		version = 1;
252 
253 	nr = 0;
254 	index = idx_map;
255 
256 	if (version > 1)
257 		index += 2; /* skip index header */
258 
259 	for (i = 0; i < 256; i++) {
260 		uint32_t n = ntohl(index[i]);
261 		if (n < nr) {
262 			git_futils_mmap_free(&p->index_map);
263 			return packfile_error("index is non-monotonic");
264 		}
265 		nr = n;
266 	}
267 
268 	if (version == 1) {
269 		/*
270 		 * Total size:
271 		 * - 256 index entries 4 bytes each
272 		 * - 24-byte entries * nr (20-byte sha1 + 4-byte offset)
273 		 * - 20-byte SHA1 of the packfile
274 		 * - 20-byte SHA1 file checksum
275 		 */
276 		if (idx_size != 4*256 + nr * 24 + 20 + 20) {
277 			git_futils_mmap_free(&p->index_map);
278 			return packfile_error("index is corrupted");
279 		}
280 	} else if (version == 2) {
281 		/*
282 		 * Minimum size:
283 		 * - 8 bytes of header
284 		 * - 256 index entries 4 bytes each
285 		 * - 20-byte sha1 entry * nr
286 		 * - 4-byte crc entry * nr
287 		 * - 4-byte offset entry * nr
288 		 * - 20-byte SHA1 of the packfile
289 		 * - 20-byte SHA1 file checksum
290 		 * And after the 4-byte offset table might be a
291 		 * variable sized table containing 8-byte entries
292 		 * for offsets larger than 2^31.
293 		 */
294 		unsigned long min_size = 8 + 4*256 + nr*(20 + 4 + 4) + 20 + 20;
295 		unsigned long max_size = min_size;
296 
297 		if (nr)
298 			max_size += (nr - 1)*8;
299 
300 		if (idx_size < min_size || idx_size > max_size) {
301 			git_futils_mmap_free(&p->index_map);
302 			return packfile_error("wrong index size");
303 		}
304 	}
305 
306 	p->num_objects = nr;
307 	p->index_version = version;
308 	return 0;
309 }
310 
pack_index_open(struct git_pack_file * p)311 static int pack_index_open(struct git_pack_file *p)
312 {
313 	int error = 0;
314 	size_t name_len;
315 	git_buf idx_name;
316 
317 	if (p->index_version > -1)
318 		return 0;
319 
320 	name_len = strlen(p->pack_name);
321 	assert(name_len > strlen(".pack")); /* checked by git_pack_file alloc */
322 
323 	if (git_buf_init(&idx_name, name_len) < 0)
324 		return -1;
325 
326 	git_buf_put(&idx_name, p->pack_name, name_len - strlen(".pack"));
327 	git_buf_puts(&idx_name, ".idx");
328 	if (git_buf_oom(&idx_name)) {
329 		git_buf_free(&idx_name);
330 		return -1;
331 	}
332 
333 	if ((error = git_mutex_lock(&p->lock)) < 0) {
334 		git_buf_free(&idx_name);
335 		return error;
336 	}
337 
338 	if (p->index_version == -1)
339 		error = pack_index_check(idx_name.ptr, p);
340 
341 	git_buf_free(&idx_name);
342 
343 	git_mutex_unlock(&p->lock);
344 
345 	return error;
346 }
347 
pack_window_open(struct git_pack_file * p,git_mwindow ** w_cursor,git_off_t offset,unsigned int * left)348 static unsigned char *pack_window_open(
349 		struct git_pack_file *p,
350 		git_mwindow **w_cursor,
351 		git_off_t offset,
352 		unsigned int *left)
353 {
354 	if (p->mwf.fd == -1 && packfile_open(p) < 0)
355 		return NULL;
356 
357 	/* Since packfiles end in a hash of their content and it's
358 	 * pointless to ask for an offset into the middle of that
359 	 * hash, and the pack_window_contains function above wouldn't match
360 	 * don't allow an offset too close to the end of the file.
361 	 *
362 	 * Don't allow a negative offset, as that means we've wrapped
363 	 * around.
364 	 */
365 	if (offset > (p->mwf.size - 20))
366 		return NULL;
367 	if (offset < 0)
368 		return NULL;
369 
370 	return git_mwindow_open(&p->mwf, w_cursor, offset, 20, left);
371  }
372 
373 /*
374  * The per-object header is a pretty dense thing, which is
375  *  - first byte: low four bits are "size",
376  *    then three bits of "type",
377  *    with the high bit being "size continues".
378  *  - each byte afterwards: low seven bits are size continuation,
379  *    with the high bit being "size continues"
380  */
git_packfile__object_header(unsigned char * hdr,size_t size,git_otype type)381 size_t git_packfile__object_header(unsigned char *hdr, size_t size, git_otype type)
382 {
383 	unsigned char *hdr_base;
384 	unsigned char c;
385 
386 	assert(type >= GIT_OBJ_COMMIT && type <= GIT_OBJ_REF_DELTA);
387 
388 	/* TODO: add support for chunked objects; see git.git 6c0d19b1 */
389 
390 	c = (unsigned char)((type << 4) | (size & 15));
391 	size >>= 4;
392 	hdr_base = hdr;
393 
394 	while (size) {
395 		*hdr++ = c | 0x80;
396 		c = size & 0x7f;
397 		size >>= 7;
398 	}
399 	*hdr++ = c;
400 
401 	return (hdr - hdr_base);
402 }
403 
404 
packfile_unpack_header1(unsigned long * usedp,size_t * sizep,git_otype * type,const unsigned char * buf,unsigned long len)405 static int packfile_unpack_header1(
406 		unsigned long *usedp,
407 		size_t *sizep,
408 		git_otype *type,
409 		const unsigned char *buf,
410 		unsigned long len)
411 {
412 	unsigned shift;
413 	unsigned long size, c;
414 	unsigned long used = 0;
415 
416 	c = buf[used++];
417 	*type = (c >> 4) & 7;
418 	size = c & 15;
419 	shift = 4;
420 	while (c & 0x80) {
421 		if (len <= used) {
422 			giterr_set(GITERR_ODB, "buffer too small");
423 			return GIT_EBUFS;
424 		}
425 
426 		if (bitsizeof(long) <= shift) {
427 			*usedp = 0;
428 			giterr_set(GITERR_ODB, "packfile corrupted");
429 			return -1;
430 		}
431 
432 		c = buf[used++];
433 		size += (c & 0x7f) << shift;
434 		shift += 7;
435 	}
436 
437 	*sizep = (size_t)size;
438 	*usedp = used;
439 	return 0;
440 }
441 
git_packfile_unpack_header(size_t * size_p,git_otype * type_p,git_mwindow_file * mwf,git_mwindow ** w_curs,git_off_t * curpos)442 int git_packfile_unpack_header(
443 		size_t *size_p,
444 		git_otype *type_p,
445 		git_mwindow_file *mwf,
446 		git_mwindow **w_curs,
447 		git_off_t *curpos)
448 {
449 	unsigned char *base;
450 	unsigned int left;
451 	unsigned long used;
452 	int ret;
453 
454 	/* pack_window_open() assures us we have [base, base + 20) available
455 	 * as a range that we can look at at. (Its actually the hash
456 	 * size that is assured.) With our object header encoding
457 	 * the maximum deflated object size is 2^137, which is just
458 	 * insane, so we know won't exceed what we have been given.
459 	 */
460 /*	base = pack_window_open(p, w_curs, *curpos, &left); */
461 	base = git_mwindow_open(mwf, w_curs, *curpos, 20, &left);
462 	if (base == NULL)
463 		return GIT_EBUFS;
464 
465 	ret = packfile_unpack_header1(&used, size_p, type_p, base, left);
466 	git_mwindow_close(w_curs);
467 	if (ret == GIT_EBUFS)
468 		return ret;
469 	else if (ret < 0)
470 		return packfile_error("header length is zero");
471 
472 	*curpos += used;
473 	return 0;
474 }
475 
git_packfile_resolve_header(size_t * size_p,git_otype * type_p,struct git_pack_file * p,git_off_t offset)476 int git_packfile_resolve_header(
477 		size_t *size_p,
478 		git_otype *type_p,
479 		struct git_pack_file *p,
480 		git_off_t offset)
481 {
482 	git_mwindow *w_curs = NULL;
483 	git_off_t curpos = offset;
484 	size_t size;
485 	git_otype type;
486 	git_off_t base_offset;
487 	int error;
488 
489 	error = git_packfile_unpack_header(&size, &type, &p->mwf, &w_curs, &curpos);
490 	if (error < 0)
491 		return error;
492 
493 	if (type == GIT_OBJ_OFS_DELTA || type == GIT_OBJ_REF_DELTA) {
494 		size_t base_size;
495 		git_packfile_stream stream;
496 
497 		base_offset = get_delta_base(p, &w_curs, &curpos, type, offset);
498 		git_mwindow_close(&w_curs);
499 		if ((error = git_packfile_stream_open(&stream, p, curpos)) < 0)
500 			return error;
501 		error = git_delta_read_header_fromstream(&base_size, size_p, &stream);
502 		git_packfile_stream_free(&stream);
503 		if (error < 0)
504 			return error;
505 	} else {
506 		*size_p = size;
507 		base_offset = 0;
508 	}
509 
510 	while (type == GIT_OBJ_OFS_DELTA || type == GIT_OBJ_REF_DELTA) {
511 		curpos = base_offset;
512 		error = git_packfile_unpack_header(&size, &type, &p->mwf, &w_curs, &curpos);
513 		if (error < 0)
514 			return error;
515 		if (type != GIT_OBJ_OFS_DELTA && type != GIT_OBJ_REF_DELTA)
516 			break;
517 		base_offset = get_delta_base(p, &w_curs, &curpos, type, base_offset);
518 		git_mwindow_close(&w_curs);
519 	}
520 	*type_p = type;
521 
522 	return error;
523 }
524 
525 #define SMALL_STACK_SIZE 64
526 
527 /**
528  * Generate the chain of dependencies which we need to get to the
529  * object at `off`. `chain` is used a stack, popping gives the right
530  * order to apply deltas on. If an object is found in the pack's base
531  * cache, we stop calculating there.
532  */
pack_dependency_chain(git_dependency_chain * chain_out,git_pack_cache_entry ** cached_out,git_off_t * cached_off,struct pack_chain_elem * small_stack,size_t * stack_sz,struct git_pack_file * p,git_off_t obj_offset)533 static int pack_dependency_chain(git_dependency_chain *chain_out,
534 				 git_pack_cache_entry **cached_out, git_off_t *cached_off,
535 				 struct pack_chain_elem *small_stack, size_t *stack_sz,
536 				 struct git_pack_file *p, git_off_t obj_offset)
537 {
538 	git_dependency_chain chain = GIT_ARRAY_INIT;
539 	git_mwindow *w_curs = NULL;
540 	git_off_t curpos = obj_offset, base_offset;
541 	int error = 0, use_heap = 0;
542 	size_t size, elem_pos;
543 	git_otype type;
544 
545 	elem_pos = 0;
546 	while (true) {
547 		struct pack_chain_elem *elem;
548 		git_pack_cache_entry *cached = NULL;
549 
550 		/* if we have a base cached, we can stop here instead */
551 		if ((cached = cache_get(&p->bases, obj_offset)) != NULL) {
552 			*cached_out = cached;
553 			*cached_off = obj_offset;
554 			break;
555 		}
556 
557 		/* if we run out of space on the small stack, use the array */
558 		if (elem_pos == SMALL_STACK_SIZE) {
559 			git_array_init_to_size(chain, elem_pos);
560 			GITERR_CHECK_ARRAY(chain);
561 			memcpy(chain.ptr, small_stack, elem_pos * sizeof(struct pack_chain_elem));
562 			chain.size = elem_pos;
563 			use_heap = 1;
564 		}
565 
566 		curpos = obj_offset;
567 		if (!use_heap) {
568 			elem = &small_stack[elem_pos];
569 		} else {
570 			elem = git_array_alloc(chain);
571 			if (!elem) {
572 				error = -1;
573 				goto on_error;
574 			}
575 		}
576 
577 		elem->base_key = obj_offset;
578 
579 		error = git_packfile_unpack_header(&size, &type, &p->mwf, &w_curs, &curpos);
580 
581 		if (error < 0)
582 			goto on_error;
583 
584 		elem->offset = curpos;
585 		elem->size = size;
586 		elem->type = type;
587 		elem->base_key = obj_offset;
588 
589 		if (type != GIT_OBJ_OFS_DELTA && type != GIT_OBJ_REF_DELTA)
590 			break;
591 
592 		base_offset = get_delta_base(p, &w_curs, &curpos, type, obj_offset);
593 		git_mwindow_close(&w_curs);
594 
595 		if (base_offset == 0) {
596 			error = packfile_error("delta offset is zero");
597 			goto on_error;
598 		}
599 		if (base_offset < 0) { /* must actually be an error code */
600 			error = (int)base_offset;
601 			goto on_error;
602 		}
603 
604 		/* we need to pass the pos *after* the delta-base bit */
605 		elem->offset = curpos;
606 
607 		/* go through the loop again, but with the new object */
608 		obj_offset = base_offset;
609 		elem_pos++;
610 	}
611 
612 
613 	*stack_sz = elem_pos + 1;
614 	*chain_out = chain;
615 	return error;
616 
617 on_error:
618 	git_array_clear(chain);
619 	return error;
620 }
621 
git_packfile_unpack(git_rawobj * obj,struct git_pack_file * p,git_off_t * obj_offset)622 int git_packfile_unpack(
623 	git_rawobj *obj,
624 	struct git_pack_file *p,
625 	git_off_t *obj_offset)
626 {
627 	git_mwindow *w_curs = NULL;
628 	git_off_t curpos = *obj_offset;
629 	int error, free_base = 0;
630 	git_dependency_chain chain = GIT_ARRAY_INIT;
631 	struct pack_chain_elem *elem = NULL, *stack;
632 	git_pack_cache_entry *cached = NULL;
633 	struct pack_chain_elem small_stack[SMALL_STACK_SIZE];
634 	size_t stack_size = 0, elem_pos, alloclen;
635 	git_otype base_type;
636 
637 	/*
638 	 * TODO: optionally check the CRC on the packfile
639 	 */
640 
641 	error = pack_dependency_chain(&chain, &cached, obj_offset, small_stack, &stack_size, p, *obj_offset);
642 	if (error < 0)
643 		return error;
644 
645 	obj->data = NULL;
646 	obj->len = 0;
647 	obj->type = GIT_OBJ_BAD;
648 
649 	/* let's point to the right stack */
650 	stack = chain.ptr ? chain.ptr : small_stack;
651 
652 	elem_pos = stack_size;
653 	if (cached) {
654 		memcpy(obj, &cached->raw, sizeof(git_rawobj));
655 		base_type = obj->type;
656 		elem_pos--;	/* stack_size includes the base, which isn't actually there */
657 	} else {
658 		elem = &stack[--elem_pos];
659 		base_type = elem->type;
660 	}
661 
662 	switch (base_type) {
663 	case GIT_OBJ_COMMIT:
664 	case GIT_OBJ_TREE:
665 	case GIT_OBJ_BLOB:
666 	case GIT_OBJ_TAG:
667 		if (!cached) {
668 			curpos = elem->offset;
669 			error = packfile_unpack_compressed(obj, p, &w_curs, &curpos, elem->size, elem->type);
670 			git_mwindow_close(&w_curs);
671 			base_type = elem->type;
672 		}
673 		if (error < 0)
674 			goto cleanup;
675 		break;
676 	case GIT_OBJ_OFS_DELTA:
677 	case GIT_OBJ_REF_DELTA:
678 		error = packfile_error("dependency chain ends in a delta");
679 		goto cleanup;
680 	default:
681 		error = packfile_error("invalid packfile type in header");
682 		goto cleanup;
683 	}
684 
685 	/*
686 	 * Finding the object we want a cached base element is
687 	 * problematic, as we need to make sure we don't accidentally
688 	 * give the caller the cached object, which it would then feel
689 	 * free to free, so we need to copy the data.
690 	 */
691 	if (cached && stack_size == 1) {
692 		void *data = obj->data;
693 
694 		GITERR_CHECK_ALLOC_ADD(&alloclen, obj->len, 1);
695 		obj->data = git__malloc(alloclen);
696 		GITERR_CHECK_ALLOC(obj->data);
697 
698 		memcpy(obj->data, data, obj->len + 1);
699 		git_atomic_dec(&cached->refcount);
700 		goto cleanup;
701 	}
702 
703 	/* we now apply each consecutive delta until we run out */
704 	while (elem_pos > 0 && !error) {
705 		git_rawobj base, delta;
706 
707 		/*
708 		 * We can now try to add the base to the cache, as
709 		 * long as it's not already the cached one.
710 		 */
711 		if (!cached)
712 			free_base = !!cache_add(&cached, &p->bases, obj, elem->base_key);
713 
714 		elem = &stack[elem_pos - 1];
715 		curpos = elem->offset;
716 		error = packfile_unpack_compressed(&delta, p, &w_curs, &curpos, elem->size, elem->type);
717 		git_mwindow_close(&w_curs);
718 
719 		if (error < 0) {
720 			/* We have transferred ownership of the data to the cache. */
721 			obj->data = NULL;
722 			break;
723 		}
724 
725 		/* the current object becomes the new base, on which we apply the delta */
726 		base = *obj;
727 		obj->data = NULL;
728 		obj->len = 0;
729 		obj->type = GIT_OBJ_BAD;
730 
731 		error = git_delta_apply(&obj->data, &obj->len, base.data, base.len, delta.data, delta.len);
732 		obj->type = base_type;
733 
734 		/*
735 		 * We usually don't want to free the base at this
736 		 * point, as we put it into the cache in the previous
737 		 * iteration. free_base lets us know that we got the
738 		 * base object directly from the packfile, so we can free it.
739 		 */
740 		git__free(delta.data);
741 		if (free_base) {
742 			free_base = 0;
743 			git__free(base.data);
744 		}
745 
746 		if (cached) {
747 			git_atomic_dec(&cached->refcount);
748 			cached = NULL;
749 		}
750 
751 		if (error < 0)
752 			break;
753 
754 		elem_pos--;
755 	}
756 
757 cleanup:
758 	if (error < 0) {
759 		git__free(obj->data);
760 		if (cached)
761 			git_atomic_dec(&cached->refcount);
762 	}
763 
764 	if (elem)
765 		*obj_offset = curpos;
766 
767 	git_array_clear(chain);
768 	return error;
769 }
770 
use_git_alloc(void * opaq,unsigned int count,unsigned int size)771 static void *use_git_alloc(void *opaq, unsigned int count, unsigned int size)
772 {
773 	GIT_UNUSED(opaq);
774 	return git__calloc(count, size);
775 }
776 
use_git_free(void * opaq,void * ptr)777 static void use_git_free(void *opaq, void *ptr)
778 {
779 	GIT_UNUSED(opaq);
780 	git__free(ptr);
781 }
782 
git_packfile_stream_open(git_packfile_stream * obj,struct git_pack_file * p,git_off_t curpos)783 int git_packfile_stream_open(git_packfile_stream *obj, struct git_pack_file *p, git_off_t curpos)
784 {
785 	int st;
786 
787 	memset(obj, 0, sizeof(git_packfile_stream));
788 	obj->curpos = curpos;
789 	obj->p = p;
790 	obj->zstream.zalloc = use_git_alloc;
791 	obj->zstream.zfree = use_git_free;
792 	obj->zstream.next_in = Z_NULL;
793 	obj->zstream.next_out = Z_NULL;
794 	st = inflateInit(&obj->zstream);
795 	if (st != Z_OK) {
796 		giterr_set(GITERR_ZLIB, "failed to init packfile stream");
797 		return -1;
798 	}
799 
800 	return 0;
801 }
802 
git_packfile_stream_read(git_packfile_stream * obj,void * buffer,size_t len)803 ssize_t git_packfile_stream_read(git_packfile_stream *obj, void *buffer, size_t len)
804 {
805 	unsigned char *in;
806 	size_t written;
807 	int st;
808 
809 	if (obj->done)
810 		return 0;
811 
812 	in = pack_window_open(obj->p, &obj->mw, obj->curpos, &obj->zstream.avail_in);
813 	if (in == NULL)
814 		return GIT_EBUFS;
815 
816 	obj->zstream.next_out = buffer;
817 	obj->zstream.avail_out = (unsigned int)len;
818 	obj->zstream.next_in = in;
819 
820 	st = inflate(&obj->zstream, Z_SYNC_FLUSH);
821 	git_mwindow_close(&obj->mw);
822 
823 	obj->curpos += obj->zstream.next_in - in;
824 	written = len - obj->zstream.avail_out;
825 
826 	if (st != Z_OK && st != Z_STREAM_END) {
827 		giterr_set(GITERR_ZLIB, "error reading from the zlib stream");
828 		return -1;
829 	}
830 
831 	if (st == Z_STREAM_END)
832 		obj->done = 1;
833 
834 
835 	/* If we didn't write anything out but we're not done, we need more data */
836 	if (!written && st != Z_STREAM_END)
837 		return GIT_EBUFS;
838 
839 	return written;
840 
841 }
842 
git_packfile_stream_free(git_packfile_stream * obj)843 void git_packfile_stream_free(git_packfile_stream *obj)
844 {
845 	inflateEnd(&obj->zstream);
846 }
847 
packfile_unpack_compressed(git_rawobj * obj,struct git_pack_file * p,git_mwindow ** w_curs,git_off_t * curpos,size_t size,git_otype type)848 static int packfile_unpack_compressed(
849 	git_rawobj *obj,
850 	struct git_pack_file *p,
851 	git_mwindow **w_curs,
852 	git_off_t *curpos,
853 	size_t size,
854 	git_otype type)
855 {
856 	size_t buf_size;
857 	int st;
858 	z_stream stream;
859 	unsigned char *buffer, *in;
860 
861 	GITERR_CHECK_ALLOC_ADD(&buf_size, size, 1);
862 	buffer = git__calloc(1, buf_size);
863 	GITERR_CHECK_ALLOC(buffer);
864 
865 	memset(&stream, 0, sizeof(stream));
866 	stream.next_out = buffer;
867 	stream.avail_out = (uInt)buf_size;
868 	stream.zalloc = use_git_alloc;
869 	stream.zfree = use_git_free;
870 
871 	st = inflateInit(&stream);
872 	if (st != Z_OK) {
873 		git__free(buffer);
874 		giterr_set(GITERR_ZLIB, "failed to init zlib stream on unpack");
875 
876 		return -1;
877 	}
878 
879 	do {
880 		in = pack_window_open(p, w_curs, *curpos, &stream.avail_in);
881 		stream.next_in = in;
882 		st = inflate(&stream, Z_FINISH);
883 		git_mwindow_close(w_curs);
884 
885 		if (!stream.avail_out)
886 			break; /* the payload is larger than it should be */
887 
888 		if (st == Z_BUF_ERROR && in == NULL) {
889 			inflateEnd(&stream);
890 			git__free(buffer);
891 			return GIT_EBUFS;
892 		}
893 
894 		*curpos += stream.next_in - in;
895 	} while (st == Z_OK || st == Z_BUF_ERROR);
896 
897 	inflateEnd(&stream);
898 
899 	if ((st != Z_STREAM_END) || stream.total_out != size) {
900 		git__free(buffer);
901 		giterr_set(GITERR_ZLIB, "error inflating zlib stream");
902 		return -1;
903 	}
904 
905 	obj->type = type;
906 	obj->len = size;
907 	obj->data = buffer;
908 	return 0;
909 }
910 
911 /*
912  * curpos is where the data starts, delta_obj_offset is the where the
913  * header starts
914  */
get_delta_base(struct git_pack_file * p,git_mwindow ** w_curs,git_off_t * curpos,git_otype type,git_off_t delta_obj_offset)915 git_off_t get_delta_base(
916 	struct git_pack_file *p,
917 	git_mwindow **w_curs,
918 	git_off_t *curpos,
919 	git_otype type,
920 	git_off_t delta_obj_offset)
921 {
922 	unsigned int left = 0;
923 	unsigned char *base_info;
924 	git_off_t base_offset;
925 	git_oid unused;
926 
927 	base_info = pack_window_open(p, w_curs, *curpos, &left);
928 	/* Assumption: the only reason this would fail is because the file is too small */
929 	if (base_info == NULL)
930 		return GIT_EBUFS;
931 	/* pack_window_open() assured us we have [base_info, base_info + 20)
932 	 * as a range that we can look at without walking off the
933 	 * end of the mapped window. Its actually the hash size
934 	 * that is assured. An OFS_DELTA longer than the hash size
935 	 * is stupid, as then a REF_DELTA would be smaller to store.
936 	 */
937 	if (type == GIT_OBJ_OFS_DELTA) {
938 		unsigned used = 0;
939 		unsigned char c = base_info[used++];
940 		size_t unsigned_base_offset = c & 127;
941 		while (c & 128) {
942 			if (left <= used)
943 				return GIT_EBUFS;
944 			unsigned_base_offset += 1;
945 			if (!unsigned_base_offset || MSB(unsigned_base_offset, 7))
946 				return 0; /* overflow */
947 			c = base_info[used++];
948 			unsigned_base_offset = (unsigned_base_offset << 7) + (c & 127);
949 		}
950 		if (unsigned_base_offset == 0 || (size_t)delta_obj_offset <= unsigned_base_offset)
951 			return 0; /* out of bound */
952 		base_offset = delta_obj_offset - unsigned_base_offset;
953 		*curpos += used;
954 	} else if (type == GIT_OBJ_REF_DELTA) {
955 		/* If we have the cooperative cache, search in it first */
956 		if (p->has_cache) {
957 			khiter_t k;
958 			git_oid oid;
959 
960 			git_oid_fromraw(&oid, base_info);
961 			k = git_oidmap_lookup_index(p->idx_cache, &oid);
962 			if (git_oidmap_valid_index(p->idx_cache, k)) {
963 				*curpos += 20;
964 				return ((struct git_pack_entry *)git_oidmap_value_at(p->idx_cache, k))->offset;
965 			} else {
966 				/* If we're building an index, don't try to find the pack
967 				 * entry; we just haven't seen it yet.  We'll make
968 				 * progress again in the next loop.
969 				 */
970 				return GIT_PASSTHROUGH;
971 			}
972 		}
973 
974 		/* The base entry _must_ be in the same pack */
975 		if (pack_entry_find_offset(&base_offset, &unused, p, (git_oid *)base_info, GIT_OID_HEXSZ) < 0)
976 			return packfile_error("base entry delta is not in the same pack");
977 		*curpos += 20;
978 	} else
979 		return 0;
980 
981 	return base_offset;
982 }
983 
984 /***********************************************************
985  *
986  * PACKFILE METHODS
987  *
988  ***********************************************************/
989 
git_packfile_close(struct git_pack_file * p,bool unlink_packfile)990 void git_packfile_close(struct git_pack_file *p, bool unlink_packfile)
991 {
992 	if (p->mwf.fd >= 0) {
993 		git_mwindow_free_all_locked(&p->mwf);
994 		p_close(p->mwf.fd);
995 		p->mwf.fd = -1;
996 	}
997 
998 	if (unlink_packfile)
999 		p_unlink(p->pack_name);
1000 }
1001 
git_packfile_free(struct git_pack_file * p)1002 void git_packfile_free(struct git_pack_file *p)
1003 {
1004 	if (!p)
1005 		return;
1006 
1007 	cache_free(&p->bases);
1008 
1009 	git_packfile_close(p, false);
1010 
1011 	pack_index_free(p);
1012 
1013 	git__free(p->bad_object_sha1);
1014 
1015 	git_mutex_free(&p->lock);
1016 	git_mutex_free(&p->bases.lock);
1017 	git__free(p);
1018 }
1019 
packfile_open(struct git_pack_file * p)1020 static int packfile_open(struct git_pack_file *p)
1021 {
1022 	struct stat st;
1023 	struct git_pack_header hdr;
1024 	git_oid sha1;
1025 	unsigned char *idx_sha1;
1026 
1027 	if (p->index_version == -1 && pack_index_open(p) < 0)
1028 		return git_odb__error_notfound("failed to open packfile", NULL, 0);
1029 
1030 	/* if mwf opened by another thread, return now */
1031 	if (git_mutex_lock(&p->lock) < 0)
1032 		return packfile_error("failed to get lock for open");
1033 
1034 	if (p->mwf.fd >= 0) {
1035 		git_mutex_unlock(&p->lock);
1036 		return 0;
1037 	}
1038 
1039 	/* TODO: open with noatime */
1040 	p->mwf.fd = git_futils_open_ro(p->pack_name);
1041 	if (p->mwf.fd < 0)
1042 		goto cleanup;
1043 
1044 	if (p_fstat(p->mwf.fd, &st) < 0 ||
1045 		git_mwindow_file_register(&p->mwf) < 0)
1046 		goto cleanup;
1047 
1048 	/* If we created the struct before we had the pack we lack size. */
1049 	if (!p->mwf.size) {
1050 		if (!S_ISREG(st.st_mode))
1051 			goto cleanup;
1052 		p->mwf.size = (git_off_t)st.st_size;
1053 	} else if (p->mwf.size != st.st_size)
1054 		goto cleanup;
1055 
1056 #if 0
1057 	/* We leave these file descriptors open with sliding mmap;
1058 	 * there is no point keeping them open across exec(), though.
1059 	 */
1060 	fd_flag = fcntl(p->mwf.fd, F_GETFD, 0);
1061 	if (fd_flag < 0)
1062 		goto cleanup;
1063 
1064 	fd_flag |= FD_CLOEXEC;
1065 	if (fcntl(p->pack_fd, F_SETFD, fd_flag) == -1)
1066 		goto cleanup;
1067 #endif
1068 
1069 	/* Verify we recognize this pack file format. */
1070 	if (p_read(p->mwf.fd, &hdr, sizeof(hdr)) < 0 ||
1071 		hdr.hdr_signature != htonl(PACK_SIGNATURE) ||
1072 		!pack_version_ok(hdr.hdr_version))
1073 		goto cleanup;
1074 
1075 	/* Verify the pack matches its index. */
1076 	if (p->num_objects != ntohl(hdr.hdr_entries) ||
1077 		p_lseek(p->mwf.fd, p->mwf.size - GIT_OID_RAWSZ, SEEK_SET) == -1 ||
1078 		p_read(p->mwf.fd, sha1.id, GIT_OID_RAWSZ) < 0)
1079 		goto cleanup;
1080 
1081 	idx_sha1 = ((unsigned char *)p->index_map.data) + p->index_map.len - 40;
1082 
1083 	if (git_oid__cmp(&sha1, (git_oid *)idx_sha1) != 0)
1084 		goto cleanup;
1085 
1086 	git_mutex_unlock(&p->lock);
1087 	return 0;
1088 
1089 cleanup:
1090 	giterr_set(GITERR_OS, "invalid packfile '%s'", p->pack_name);
1091 
1092 	if (p->mwf.fd >= 0)
1093 		p_close(p->mwf.fd);
1094 	p->mwf.fd = -1;
1095 
1096 	git_mutex_unlock(&p->lock);
1097 
1098 	return -1;
1099 }
1100 
git_packfile__name(char ** out,const char * path)1101 int git_packfile__name(char **out, const char *path)
1102 {
1103 	size_t path_len;
1104 	git_buf buf = GIT_BUF_INIT;
1105 
1106 	path_len = strlen(path);
1107 
1108 	if (path_len < strlen(".idx"))
1109 		return git_odb__error_notfound("invalid packfile path", NULL, 0);
1110 
1111 	if (git_buf_printf(&buf, "%.*s.pack", (int)(path_len - strlen(".idx")), path) < 0)
1112 		return -1;
1113 
1114 	*out = git_buf_detach(&buf);
1115 	return 0;
1116 }
1117 
git_packfile_alloc(struct git_pack_file ** pack_out,const char * path)1118 int git_packfile_alloc(struct git_pack_file **pack_out, const char *path)
1119 {
1120 	struct stat st;
1121 	struct git_pack_file *p;
1122 	size_t path_len = path ? strlen(path) : 0, alloc_len;
1123 
1124 	*pack_out = NULL;
1125 
1126 	if (path_len < strlen(".idx"))
1127 		return git_odb__error_notfound("invalid packfile path", NULL, 0);
1128 
1129 	GITERR_CHECK_ALLOC_ADD(&alloc_len, sizeof(*p), path_len);
1130 	GITERR_CHECK_ALLOC_ADD(&alloc_len, alloc_len, 2);
1131 
1132 	p = git__calloc(1, alloc_len);
1133 	GITERR_CHECK_ALLOC(p);
1134 
1135 	memcpy(p->pack_name, path, path_len + 1);
1136 
1137 	/*
1138 	 * Make sure a corresponding .pack file exists and that
1139 	 * the index looks sane.
1140 	 */
1141 	if (git__suffixcmp(path, ".idx") == 0) {
1142 		size_t root_len = path_len - strlen(".idx");
1143 
1144 		memcpy(p->pack_name + root_len, ".keep", sizeof(".keep"));
1145 		if (git_path_exists(p->pack_name) == true)
1146 			p->pack_keep = 1;
1147 
1148 		memcpy(p->pack_name + root_len, ".pack", sizeof(".pack"));
1149 	}
1150 
1151 	if (p_stat(p->pack_name, &st) < 0 || !S_ISREG(st.st_mode)) {
1152 		git__free(p);
1153 		return git_odb__error_notfound("packfile not found", NULL, 0);
1154 	}
1155 
1156 	/* ok, it looks sane as far as we can check without
1157 	 * actually mapping the pack file.
1158 	 */
1159 	p->mwf.fd = -1;
1160 	p->mwf.size = st.st_size;
1161 	p->pack_local = 1;
1162 	p->mtime = (git_time_t)st.st_mtime;
1163 	p->index_version = -1;
1164 
1165 	if (git_mutex_init(&p->lock)) {
1166 		giterr_set(GITERR_OS, "failed to initialize packfile mutex");
1167 		git__free(p);
1168 		return -1;
1169 	}
1170 
1171 	if (cache_init(&p->bases) < 0) {
1172 		git__free(p);
1173 		return -1;
1174 	}
1175 
1176 	*pack_out = p;
1177 
1178 	return 0;
1179 }
1180 
1181 /***********************************************************
1182  *
1183  * PACKFILE ENTRY SEARCH INTERNALS
1184  *
1185  ***********************************************************/
1186 
nth_packed_object_offset(const struct git_pack_file * p,uint32_t n)1187 static git_off_t nth_packed_object_offset(const struct git_pack_file *p, uint32_t n)
1188 {
1189 	const unsigned char *index = p->index_map.data;
1190 	const unsigned char *end = index + p->index_map.len;
1191 	index += 4 * 256;
1192 	if (p->index_version == 1) {
1193 		return ntohl(*((uint32_t *)(index + 24 * n)));
1194 	} else {
1195 		uint32_t off;
1196 		index += 8 + p->num_objects * (20 + 4);
1197 		off = ntohl(*((uint32_t *)(index + 4 * n)));
1198 		if (!(off & 0x80000000))
1199 			return off;
1200 		index += p->num_objects * 4 + (off & 0x7fffffff) * 8;
1201 
1202 		/* Make sure we're not being sent out of bounds */
1203 		if (index >= end - 8)
1204 			return -1;
1205 
1206 		return (((uint64_t)ntohl(*((uint32_t *)(index + 0)))) << 32) |
1207 					ntohl(*((uint32_t *)(index + 4)));
1208 	}
1209 }
1210 
git__memcmp4(const void * a,const void * b)1211 static int git__memcmp4(const void *a, const void *b) {
1212 	return memcmp(a, b, 4);
1213 }
1214 
git_pack_foreach_entry(struct git_pack_file * p,git_odb_foreach_cb cb,void * data)1215 int git_pack_foreach_entry(
1216 	struct git_pack_file *p,
1217 	git_odb_foreach_cb cb,
1218 	void *data)
1219 {
1220 	const unsigned char *index = p->index_map.data, *current;
1221 	uint32_t i;
1222 	int error = 0;
1223 
1224 	if (index == NULL) {
1225 		if ((error = pack_index_open(p)) < 0)
1226 			return error;
1227 
1228 		assert(p->index_map.data);
1229 
1230 		index = p->index_map.data;
1231 	}
1232 
1233 	if (p->index_version > 1) {
1234 		index += 8;
1235 	}
1236 
1237 	index += 4 * 256;
1238 
1239 	if (p->oids == NULL) {
1240 		git_vector offsets, oids;
1241 
1242 		if ((error = git_vector_init(&oids, p->num_objects, NULL)))
1243 			return error;
1244 
1245 		if ((error = git_vector_init(&offsets, p->num_objects, git__memcmp4)))
1246 			return error;
1247 
1248 		if (p->index_version > 1) {
1249 			const unsigned char *off = index + 24 * p->num_objects;
1250 			for (i = 0; i < p->num_objects; i++)
1251 				git_vector_insert(&offsets, (void*)&off[4 * i]);
1252 			git_vector_sort(&offsets);
1253 			git_vector_foreach(&offsets, i, current)
1254 				git_vector_insert(&oids, (void*)&index[5 * (current - off)]);
1255 		} else {
1256 			for (i = 0; i < p->num_objects; i++)
1257 				git_vector_insert(&offsets, (void*)&index[24 * i]);
1258 			git_vector_sort(&offsets);
1259 			git_vector_foreach(&offsets, i, current)
1260 				git_vector_insert(&oids, (void*)&current[4]);
1261 		}
1262 
1263 		git_vector_free(&offsets);
1264 		p->oids = (git_oid **)git_vector_detach(NULL, NULL, &oids);
1265 	}
1266 
1267 	for (i = 0; i < p->num_objects; i++)
1268 		if ((error = cb(p->oids[i], data)) != 0)
1269 			return giterr_set_after_callback(error);
1270 
1271 	return error;
1272 }
1273 
pack_entry_find_offset(git_off_t * offset_out,git_oid * found_oid,struct git_pack_file * p,const git_oid * short_oid,size_t len)1274 static int pack_entry_find_offset(
1275 	git_off_t *offset_out,
1276 	git_oid *found_oid,
1277 	struct git_pack_file *p,
1278 	const git_oid *short_oid,
1279 	size_t len)
1280 {
1281 	const uint32_t *level1_ofs;
1282 	const unsigned char *index;
1283 	unsigned hi, lo, stride;
1284 	int pos, found = 0;
1285 	git_off_t offset;
1286 	const unsigned char *current = 0;
1287 
1288 	*offset_out = 0;
1289 
1290 	if (p->index_version == -1) {
1291 		int error;
1292 
1293 		if ((error = pack_index_open(p)) < 0)
1294 			return error;
1295 		assert(p->index_map.data);
1296 	}
1297 
1298 	index = p->index_map.data;
1299 	level1_ofs = p->index_map.data;
1300 
1301 	if (p->index_version > 1) {
1302 		level1_ofs += 2;
1303 		index += 8;
1304 	}
1305 
1306 	index += 4 * 256;
1307 	hi = ntohl(level1_ofs[(int)short_oid->id[0]]);
1308 	lo = ((short_oid->id[0] == 0x0) ? 0 : ntohl(level1_ofs[(int)short_oid->id[0] - 1]));
1309 
1310 	if (p->index_version > 1) {
1311 		stride = 20;
1312 	} else {
1313 		stride = 24;
1314 		index += 4;
1315 	}
1316 
1317 #ifdef INDEX_DEBUG_LOOKUP
1318 	printf("%02x%02x%02x... lo %u hi %u nr %d\n",
1319 		short_oid->id[0], short_oid->id[1], short_oid->id[2], lo, hi, p->num_objects);
1320 #endif
1321 
1322 	pos = sha1_position(index, stride, lo, hi, short_oid->id);
1323 
1324 	if (pos >= 0) {
1325 		/* An object matching exactly the oid was found */
1326 		found = 1;
1327 		current = index + pos * stride;
1328 	} else {
1329 		/* No object was found */
1330 		/* pos refers to the object with the "closest" oid to short_oid */
1331 		pos = - 1 - pos;
1332 		if (pos < (int)p->num_objects) {
1333 			current = index + pos * stride;
1334 
1335 			if (!git_oid_ncmp(short_oid, (const git_oid *)current, len))
1336 				found = 1;
1337 		}
1338 	}
1339 
1340 	if (found && len != GIT_OID_HEXSZ && pos + 1 < (int)p->num_objects) {
1341 		/* Check for ambiguousity */
1342 		const unsigned char *next = current + stride;
1343 
1344 		if (!git_oid_ncmp(short_oid, (const git_oid *)next, len)) {
1345 			found = 2;
1346 		}
1347 	}
1348 
1349 	if (!found)
1350 		return git_odb__error_notfound("failed to find offset for pack entry", short_oid, len);
1351 	if (found > 1)
1352 		return git_odb__error_ambiguous("found multiple offsets for pack entry");
1353 
1354 	if ((offset = nth_packed_object_offset(p, pos)) < 0) {
1355 		giterr_set(GITERR_ODB, "packfile index is corrupt");
1356 		return -1;
1357 	}
1358 
1359 	*offset_out = offset;
1360 	git_oid_fromraw(found_oid, current);
1361 
1362 #ifdef INDEX_DEBUG_LOOKUP
1363 	{
1364 		unsigned char hex_sha1[GIT_OID_HEXSZ + 1];
1365 		git_oid_fmt(hex_sha1, found_oid);
1366 		hex_sha1[GIT_OID_HEXSZ] = '\0';
1367 		printf("found lo=%d %s\n", lo, hex_sha1);
1368 	}
1369 #endif
1370 
1371 	return 0;
1372 }
1373 
git_pack_entry_find(struct git_pack_entry * e,struct git_pack_file * p,const git_oid * short_oid,size_t len)1374 int git_pack_entry_find(
1375 		struct git_pack_entry *e,
1376 		struct git_pack_file *p,
1377 		const git_oid *short_oid,
1378 		size_t len)
1379 {
1380 	git_off_t offset;
1381 	git_oid found_oid;
1382 	int error;
1383 
1384 	assert(p);
1385 
1386 	if (len == GIT_OID_HEXSZ && p->num_bad_objects) {
1387 		unsigned i;
1388 		for (i = 0; i < p->num_bad_objects; i++)
1389 			if (git_oid__cmp(short_oid, &p->bad_object_sha1[i]) == 0)
1390 				return packfile_error("bad object found in packfile");
1391 	}
1392 
1393 	error = pack_entry_find_offset(&offset, &found_oid, p, short_oid, len);
1394 	if (error < 0)
1395 		return error;
1396 
1397 	/* we found a unique entry in the index;
1398 	 * make sure the packfile backing the index
1399 	 * still exists on disk */
1400 	if (p->mwf.fd == -1 && (error = packfile_open(p)) < 0)
1401 		return error;
1402 
1403 	e->offset = offset;
1404 	e->p = p;
1405 
1406 	git_oid_cpy(&e->sha1, &found_oid);
1407 	return 0;
1408 }
1409