]> git.proxmox.com Git - libgit2.git/blame - src/pack.c
Merge pull request #795 from nulltoken/topic/revparse-notfound
[libgit2.git] / src / pack.c
CommitLineData
7d0cdf82 1/*
5e0de328 2 * Copyright (C) 2009-2012 the libgit2 contributors
7d0cdf82 3 *
bb742ede
VM
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.
7d0cdf82
CMN
6 */
7
a15c550d 8#include "common.h"
7d0cdf82
CMN
9#include "odb.h"
10#include "pack.h"
11#include "delta-apply.h"
a070f152 12#include "sha1_lookup.h"
a15c550d
VM
13#include "mwindow.h"
14#include "fileops.h"
7d0cdf82
CMN
15
16#include "git2/oid.h"
0c3bae62 17#include <zlib.h>
7d0cdf82 18
a070f152 19static int packfile_open(struct git_pack_file *p);
e1de726c 20static git_off_t nth_packed_object_offset(const struct git_pack_file *p, uint32_t n);
a070f152
CMN
21int packfile_unpack_compressed(
22 git_rawobj *obj,
23 struct git_pack_file *p,
24 git_mwindow **w_curs,
e1de726c 25 git_off_t *curpos,
a070f152
CMN
26 size_t size,
27 git_otype type);
28
29/* Can find the offset of an object given
30 * a prefix of an identifier.
904b67e6 31 * Throws GIT_EAMBIGUOUSOIDPREFIX if short oid
a070f152
CMN
32 * is ambiguous within the pack.
33 * This method assumes that len is between
34 * GIT_OID_MINPREFIXLEN and GIT_OID_HEXSZ.
35 */
36static int pack_entry_find_offset(
e1de726c 37 git_off_t *offset_out,
a070f152
CMN
38 git_oid *found_oid,
39 struct git_pack_file *p,
40 const git_oid *short_oid,
41 unsigned int len);
42
e1de726c
RB
43static int packfile_error(const char *message)
44{
45 giterr_set(GITERR_ODB, "Invalid pack file - %s", message);
46 return -1;
47}
48
a070f152
CMN
49/***********************************************************
50 *
51 * PACK INDEX METHODS
52 *
53 ***********************************************************/
54
55static void pack_index_free(struct git_pack_file *p)
56{
57 if (p->index_map.data) {
58 git_futils_mmap_free(&p->index_map);
59 p->index_map.data = NULL;
60 }
61}
62
87d9869f 63static int pack_index_check(const char *path, struct git_pack_file *p)
a070f152
CMN
64{
65 struct git_pack_idx_header *hdr;
66 uint32_t version, nr, i, *index;
a070f152
CMN
67 void *idx_map;
68 size_t idx_size;
a070f152 69 struct stat st;
a070f152 70 int error;
e1de726c
RB
71 /* TODO: properly open the file without access time using O_NOATIME */
72 git_file fd = git_futils_open_ro(path);
a070f152 73 if (fd < 0)
e1de726c 74 return fd;
a070f152 75
e1de726c
RB
76 if (p_fstat(fd, &st) < 0 ||
77 !S_ISREG(st.st_mode) ||
78 !git__is_sizet(st.st_size) ||
79 (idx_size = (size_t)st.st_size) < 4 * 256 + 20 + 20)
80 {
a070f152 81 p_close(fd);
e1de726c
RB
82 giterr_set(GITERR_OS, "Failed to check pack index.");
83 return -1;
a070f152
CMN
84 }
85
86 error = git_futils_mmap_ro(&p->index_map, fd, 0, idx_size);
e1de726c 87
a070f152
CMN
88 p_close(fd);
89
e1de726c
RB
90 if (error < 0)
91 return error;
a070f152
CMN
92
93 hdr = idx_map = p->index_map.data;
94
95 if (hdr->idx_signature == htonl(PACK_IDX_SIGNATURE)) {
96 version = ntohl(hdr->idx_version);
97
98 if (version < 2 || version > 2) {
99 git_futils_mmap_free(&p->index_map);
e1de726c 100 return packfile_error("unsupported index version");
a070f152
CMN
101 }
102
103 } else
104 version = 1;
105
106 nr = 0;
107 index = idx_map;
108
109 if (version > 1)
87d9869f 110 index += 2; /* skip index header */
a070f152
CMN
111
112 for (i = 0; i < 256; i++) {
113 uint32_t n = ntohl(index[i]);
114 if (n < nr) {
115 git_futils_mmap_free(&p->index_map);
e1de726c 116 return packfile_error("index is non-monotonic");
a070f152
CMN
117 }
118 nr = n;
119 }
120
121 if (version == 1) {
122 /*
123 * Total size:
87d9869f
VM
124 * - 256 index entries 4 bytes each
125 * - 24-byte entries * nr (20-byte sha1 + 4-byte offset)
126 * - 20-byte SHA1 of the packfile
127 * - 20-byte SHA1 file checksum
a070f152
CMN
128 */
129 if (idx_size != 4*256 + nr * 24 + 20 + 20) {
130 git_futils_mmap_free(&p->index_map);
e1de726c 131 return packfile_error("index is corrupted");
a070f152
CMN
132 }
133 } else if (version == 2) {
134 /*
135 * Minimum size:
87d9869f
VM
136 * - 8 bytes of header
137 * - 256 index entries 4 bytes each
138 * - 20-byte sha1 entry * nr
139 * - 4-byte crc entry * nr
140 * - 4-byte offset entry * nr
141 * - 20-byte SHA1 of the packfile
142 * - 20-byte SHA1 file checksum
a070f152
CMN
143 * And after the 4-byte offset table might be a
144 * variable sized table containing 8-byte entries
145 * for offsets larger than 2^31.
146 */
147 unsigned long min_size = 8 + 4*256 + nr*(20 + 4 + 4) + 20 + 20;
148 unsigned long max_size = min_size;
149
150 if (nr)
151 max_size += (nr - 1)*8;
152
153 if (idx_size < min_size || idx_size > max_size) {
154 git_futils_mmap_free(&p->index_map);
e1de726c 155 return packfile_error("wrong index size");
a070f152 156 }
a070f152
CMN
157 }
158
159 p->index_version = version;
160 p->num_objects = nr;
e1de726c 161 return 0;
a070f152
CMN
162}
163
164static int pack_index_open(struct git_pack_file *p)
165{
166 char *idx_name;
167 int error;
44ef8b1b 168 size_t name_len, offset;
a070f152
CMN
169
170 if (p->index_map.data)
e1de726c 171 return 0;
a070f152
CMN
172
173 idx_name = git__strdup(p->pack_name);
e1de726c
RB
174 GITERR_CHECK_ALLOC(idx_name);
175
44ef8b1b
RB
176 name_len = strlen(idx_name);
177 offset = name_len - strlen(".pack");
178 assert(offset < name_len); /* make sure no underflow */
179
180 strncpy(idx_name + offset, ".idx", name_len - offset);
a070f152
CMN
181
182 error = pack_index_check(idx_name, p);
3286c408 183 git__free(idx_name);
a070f152 184
e1de726c 185 return error;
a070f152
CMN
186}
187
188static unsigned char *pack_window_open(
189 struct git_pack_file *p,
7d0cdf82 190 git_mwindow **w_cursor,
e1de726c 191 git_off_t offset,
7d0cdf82
CMN
192 unsigned int *left)
193{
e1de726c 194 if (p->mwf.fd == -1 && packfile_open(p) < 0)
7d0cdf82
CMN
195 return NULL;
196
197 /* Since packfiles end in a hash of their content and it's
198 * pointless to ask for an offset into the middle of that
199 * hash, and the pack_window_contains function above wouldn't match
200 * don't allow an offset too close to the end of the file.
201 */
202 if (offset > (p->mwf.size - 20))
203 return NULL;
204
205 return git_mwindow_open(&p->mwf, w_cursor, offset, 20, left);
206 }
207
45d773ef
CMN
208static int packfile_unpack_header1(
209 unsigned long *usedp,
7d0cdf82
CMN
210 size_t *sizep,
211 git_otype *type,
212 const unsigned char *buf,
213 unsigned long len)
214{
215 unsigned shift;
216 unsigned long size, c;
217 unsigned long used = 0;
2aeadb9c 218
7d0cdf82
CMN
219 c = buf[used++];
220 *type = (c >> 4) & 7;
221 size = c & 15;
222 shift = 4;
223 while (c & 0x80) {
45d773ef 224 if (len <= used)
904b67e6 225 return GIT_EBUFS;
45d773ef
CMN
226
227 if (bitsizeof(long) <= shift) {
228 *usedp = 0;
229 return -1;
230 }
7d0cdf82
CMN
231
232 c = buf[used++];
233 size += (c & 0x7f) << shift;
234 shift += 7;
235 }
236
237 *sizep = (size_t)size;
45d773ef
CMN
238 *usedp = used;
239 return 0;
7d0cdf82
CMN
240}
241
242int git_packfile_unpack_header(
243 size_t *size_p,
244 git_otype *type_p,
245 git_mwindow_file *mwf,
246 git_mwindow **w_curs,
e1de726c 247 git_off_t *curpos)
7d0cdf82
CMN
248{
249 unsigned char *base;
250 unsigned int left;
251 unsigned long used;
45d773ef 252 int ret;
7d0cdf82
CMN
253
254 /* pack_window_open() assures us we have [base, base + 20) available
87d9869f
VM
255 * as a range that we can look at at. (Its actually the hash
256 * size that is assured.) With our object header encoding
7d0cdf82
CMN
257 * the maximum deflated object size is 2^137, which is just
258 * insane, so we know won't exceed what we have been given.
259 */
260// base = pack_window_open(p, w_curs, *curpos, &left);
261 base = git_mwindow_open(mwf, w_curs, *curpos, 20, &left);
262 if (base == NULL)
904b67e6 263 return GIT_EBUFS;
2aeadb9c
CY
264
265 ret = packfile_unpack_header1(&used, size_p, type_p, base, left);
45d773ef 266 git_mwindow_close(w_curs);
904b67e6 267 if (ret == GIT_EBUFS)
45d773ef
CMN
268 return ret;
269 else if (ret < 0)
e1de726c 270 return packfile_error("header length is zero");
7d0cdf82
CMN
271
272 *curpos += used;
e1de726c 273 return 0;
7d0cdf82
CMN
274}
275
a070f152 276static int packfile_unpack_delta(
7d0cdf82 277 git_rawobj *obj,
a070f152 278 struct git_pack_file *p,
7d0cdf82 279 git_mwindow **w_curs,
e1de726c 280 git_off_t *curpos,
7d0cdf82
CMN
281 size_t delta_size,
282 git_otype delta_type,
e1de726c 283 git_off_t obj_offset)
7d0cdf82 284{
e1de726c 285 git_off_t base_offset;
7d0cdf82
CMN
286 git_rawobj base, delta;
287 int error;
288
b5b474dd 289 base_offset = get_delta_base(p, w_curs, curpos, delta_type, obj_offset);
45d773ef 290 git_mwindow_close(w_curs);
7d0cdf82 291 if (base_offset == 0)
e1de726c
RB
292 return packfile_error("delta offset is zero");
293 if (base_offset < 0) /* must actually be an error code */
294 return (int)base_offset;
7d0cdf82 295
b5b474dd 296 error = git_packfile_unpack(&base, p, &base_offset);
7d0cdf82
CMN
297
298 /*
299 * TODO: git.git tries to load the base from other packfiles
300 * or loose objects.
301 *
302 * We'll need to do this in order to support thin packs.
303 */
e1de726c
RB
304 if (error < 0)
305 return error;
7d0cdf82
CMN
306
307 error = packfile_unpack_compressed(&delta, p, w_curs, curpos, delta_size, delta_type);
45d773ef 308 git_mwindow_close(w_curs);
e1de726c 309 if (error < 0) {
3286c408 310 git__free(base.data);
e1de726c 311 return error;
7d0cdf82
CMN
312 }
313
314 obj->type = base.type;
e1de726c 315 error = git__delta_apply(obj, base.data, base.len, delta.data, delta.len);
7d0cdf82 316
3286c408
VM
317 git__free(base.data);
318 git__free(delta.data);
7d0cdf82
CMN
319
320 /* TODO: we might want to cache this shit. eventually */
321 //add_delta_base_cache(p, base_offset, base, base_size, *type);
e1de726c 322
7d0cdf82
CMN
323 return error; /* error set by git__delta_apply */
324}
325
a070f152 326int git_packfile_unpack(
e1de726c
RB
327 git_rawobj *obj,
328 struct git_pack_file *p,
329 git_off_t *obj_offset)
7d0cdf82
CMN
330{
331 git_mwindow *w_curs = NULL;
e1de726c 332 git_off_t curpos = *obj_offset;
7d0cdf82
CMN
333 int error;
334
335 size_t size = 0;
336 git_otype type;
337
338 /*
339 * TODO: optionally check the CRC on the packfile
340 */
341
342 obj->data = NULL;
343 obj->len = 0;
344 obj->type = GIT_OBJ_BAD;
345
346 error = git_packfile_unpack_header(&size, &type, &p->mwf, &w_curs, &curpos);
45d773ef
CMN
347 git_mwindow_close(&w_curs);
348
e1de726c
RB
349 if (error < 0)
350 return error;
7d0cdf82
CMN
351
352 switch (type) {
353 case GIT_OBJ_OFS_DELTA:
354 case GIT_OBJ_REF_DELTA:
355 error = packfile_unpack_delta(
b5b474dd
CMN
356 obj, p, &w_curs, &curpos,
357 size, type, *obj_offset);
7d0cdf82
CMN
358 break;
359
360 case GIT_OBJ_COMMIT:
361 case GIT_OBJ_TREE:
362 case GIT_OBJ_BLOB:
363 case GIT_OBJ_TAG:
364 error = packfile_unpack_compressed(
b5b474dd 365 obj, p, &w_curs, &curpos,
7d0cdf82
CMN
366 size, type);
367 break;
368
369 default:
e1de726c 370 error = packfile_error("invalid packfile type in header");;
7d0cdf82
CMN
371 break;
372 }
373
b5b474dd 374 *obj_offset = curpos;
e1de726c 375 return error;
7d0cdf82
CMN
376}
377
282283ac
RB
378static void *use_git_alloc(void *opaq, unsigned int count, unsigned int size)
379{
380 GIT_UNUSED(opaq);
381 return git__calloc(count, size);
382}
383
384static void use_git_free(void *opaq, void *ptr)
385{
386 GIT_UNUSED(opaq);
387 git__free(ptr);
388}
389
7d0cdf82 390int packfile_unpack_compressed(
e1de726c
RB
391 git_rawobj *obj,
392 struct git_pack_file *p,
393 git_mwindow **w_curs,
394 git_off_t *curpos,
395 size_t size,
396 git_otype type)
7d0cdf82
CMN
397{
398 int st;
399 z_stream stream;
400 unsigned char *buffer, *in;
401
e1de726c
RB
402 buffer = git__calloc(1, size + 1);
403 GITERR_CHECK_ALLOC(buffer);
7d0cdf82
CMN
404
405 memset(&stream, 0, sizeof(stream));
406 stream.next_out = buffer;
1c3fac4d 407 stream.avail_out = (uInt)size + 1;
282283ac
RB
408 stream.zalloc = use_git_alloc;
409 stream.zfree = use_git_free;
7d0cdf82
CMN
410
411 st = inflateInit(&stream);
412 if (st != Z_OK) {
3286c408 413 git__free(buffer);
e1de726c 414 giterr_set(GITERR_ZLIB, "Failed to inflate packfile");
45d773ef 415
e1de726c 416 return -1;
7d0cdf82
CMN
417 }
418
419 do {
b5b474dd 420 in = pack_window_open(p, w_curs, *curpos, &stream.avail_in);
7d0cdf82
CMN
421 stream.next_in = in;
422 st = inflate(&stream, Z_FINISH);
45d773ef 423 git_mwindow_close(w_curs);
7d0cdf82
CMN
424
425 if (!stream.avail_out)
426 break; /* the payload is larger than it should be */
427
45d773ef
CMN
428 if (st == Z_BUF_ERROR && in == NULL) {
429 inflateEnd(&stream);
430 git__free(buffer);
904b67e6 431 return GIT_EBUFS;
45d773ef
CMN
432 }
433
b5b474dd 434 *curpos += stream.next_in - in;
7d0cdf82
CMN
435 } while (st == Z_OK || st == Z_BUF_ERROR);
436
437 inflateEnd(&stream);
438
439 if ((st != Z_STREAM_END) || stream.total_out != size) {
3286c408 440 git__free(buffer);
e1de726c
RB
441 giterr_set(GITERR_ZLIB, "Failed to inflate packfile");
442 return -1;
7d0cdf82
CMN
443 }
444
445 obj->type = type;
446 obj->len = size;
447 obj->data = buffer;
e1de726c 448 return 0;
7d0cdf82
CMN
449}
450
b5b474dd
CMN
451/*
452 * curpos is where the data starts, delta_obj_offset is the where the
453 * header starts
454 */
e1de726c
RB
455git_off_t get_delta_base(
456 struct git_pack_file *p,
457 git_mwindow **w_curs,
458 git_off_t *curpos,
459 git_otype type,
460 git_off_t delta_obj_offset)
7d0cdf82 461{
45d773ef
CMN
462 unsigned int left = 0;
463 unsigned char *base_info;
e1de726c 464 git_off_t base_offset;
7d0cdf82
CMN
465 git_oid unused;
466
45d773ef
CMN
467 base_info = pack_window_open(p, w_curs, *curpos, &left);
468 /* Assumption: the only reason this would fail is because the file is too small */
469 if (base_info == NULL)
904b67e6 470 return GIT_EBUFS;
7d0cdf82
CMN
471 /* pack_window_open() assured us we have [base_info, base_info + 20)
472 * as a range that we can look at without walking off the
87d9869f
VM
473 * end of the mapped window. Its actually the hash size
474 * that is assured. An OFS_DELTA longer than the hash size
7d0cdf82
CMN
475 * is stupid, as then a REF_DELTA would be smaller to store.
476 */
477 if (type == GIT_OBJ_OFS_DELTA) {
478 unsigned used = 0;
479 unsigned char c = base_info[used++];
480 base_offset = c & 127;
481 while (c & 128) {
45d773ef 482 if (left <= used)
904b67e6 483 return GIT_EBUFS;
7d0cdf82
CMN
484 base_offset += 1;
485 if (!base_offset || MSB(base_offset, 7))
87d9869f 486 return 0; /* overflow */
7d0cdf82
CMN
487 c = base_info[used++];
488 base_offset = (base_offset << 7) + (c & 127);
489 }
490 base_offset = delta_obj_offset - base_offset;
491 if (base_offset <= 0 || base_offset >= delta_obj_offset)
87d9869f 492 return 0; /* out of bound */
7d0cdf82
CMN
493 *curpos += used;
494 } else if (type == GIT_OBJ_REF_DELTA) {
c1af5a39
CMN
495 /* If we have the cooperative cache, search in it first */
496 if (p->has_cache) {
497 int pos;
498 struct git_pack_entry key;
499
500 git_oid_fromraw(&key.sha1, base_info);
501 pos = git_vector_bsearch(&p->cache, &key);
502 if (pos >= 0) {
503 *curpos += 20;
504 return ((struct git_pack_entry *)git_vector_get(&p->cache, pos))->offset;
505 }
506 }
7d0cdf82 507 /* The base entry _must_ be in the same pack */
e1de726c
RB
508 if (pack_entry_find_offset(&base_offset, &unused, p, (git_oid *)base_info, GIT_OID_HEXSZ) < 0)
509 return packfile_error("base entry delta is not in the same pack");
7d0cdf82
CMN
510 *curpos += 20;
511 } else
512 return 0;
513
514 return base_offset;
515}
a070f152
CMN
516
517/***********************************************************
518 *
519 * PACKFILE METHODS
520 *
521 ***********************************************************/
522
44ef8b1b 523static struct git_pack_file *packfile_alloc(size_t extra)
a070f152 524{
e1de726c
RB
525 struct git_pack_file *p = git__calloc(1, sizeof(*p) + extra);
526 if (p != NULL)
527 p->mwf.fd = -1;
a070f152
CMN
528 return p;
529}
530
531
532void packfile_free(struct git_pack_file *p)
533{
534 assert(p);
535
536 /* clear_delta_base_cache(); */
537 git_mwindow_free_all(&p->mwf);
1d8943c6 538 git_mwindow_file_deregister(&p->mwf);
a070f152
CMN
539
540 if (p->mwf.fd != -1)
541 p_close(p->mwf.fd);
542
543 pack_index_free(p);
544
3286c408
VM
545 git__free(p->bad_object_sha1);
546 git__free(p);
a070f152
CMN
547}
548
549static int packfile_open(struct git_pack_file *p)
550{
551 struct stat st;
552 struct git_pack_header hdr;
553 git_oid sha1;
554 unsigned char *idx_sha1;
555
e1de726c
RB
556 assert(p->index_map.data);
557
0d0fa7c3 558 if (!p->index_map.data && pack_index_open(p) < 0)
282283ac 559 return git_odb__error_notfound("failed to open packfile", NULL);
a070f152
CMN
560
561 /* TODO: open with noatime */
e1de726c
RB
562 p->mwf.fd = git_futils_open_ro(p->pack_name);
563 if (p->mwf.fd < 0)
564 return p->mwf.fd;
a070f152 565
e1de726c
RB
566 if (p_fstat(p->mwf.fd, &st) < 0 ||
567 git_mwindow_file_register(&p->mwf) < 0)
568 goto cleanup;
a070f152
CMN
569
570 /* If we created the struct before we had the pack we lack size. */
571 if (!p->mwf.size) {
572 if (!S_ISREG(st.st_mode))
573 goto cleanup;
e1de726c 574 p->mwf.size = (git_off_t)st.st_size;
a070f152
CMN
575 } else if (p->mwf.size != st.st_size)
576 goto cleanup;
577
578#if 0
579 /* We leave these file descriptors open with sliding mmap;
580 * there is no point keeping them open across exec(), though.
581 */
582 fd_flag = fcntl(p->mwf.fd, F_GETFD, 0);
583 if (fd_flag < 0)
e1de726c 584 goto cleanup;
a070f152
CMN
585
586 fd_flag |= FD_CLOEXEC;
587 if (fcntl(p->pack_fd, F_SETFD, fd_flag) == -1)
e1de726c 588 goto cleanup;
a070f152
CMN
589#endif
590
591 /* Verify we recognize this pack file format. */
e1de726c
RB
592 if (p_read(p->mwf.fd, &hdr, sizeof(hdr)) < 0 ||
593 hdr.hdr_signature != htonl(PACK_SIGNATURE) ||
594 !pack_version_ok(hdr.hdr_version))
a070f152
CMN
595 goto cleanup;
596
597 /* Verify the pack matches its index. */
e1de726c
RB
598 if (p->num_objects != ntohl(hdr.hdr_entries) ||
599 p_lseek(p->mwf.fd, p->mwf.size - GIT_OID_RAWSZ, SEEK_SET) == -1 ||
600 p_read(p->mwf.fd, sha1.id, GIT_OID_RAWSZ) < 0)
a070f152
CMN
601 goto cleanup;
602
603 idx_sha1 = ((unsigned char *)p->index_map.data) + p->index_map.len - 40;
604
e1de726c
RB
605 if (git_oid_cmp(&sha1, (git_oid *)idx_sha1) == 0)
606 return 0;
a070f152
CMN
607
608cleanup:
e1de726c 609 giterr_set(GITERR_OS, "Invalid packfile '%s'", p->pack_name);
a070f152
CMN
610 p_close(p->mwf.fd);
611 p->mwf.fd = -1;
e1de726c 612 return -1;
a070f152
CMN
613}
614
615int git_packfile_check(struct git_pack_file **pack_out, const char *path)
616{
617 struct stat st;
618 struct git_pack_file *p;
619 size_t path_len;
620
621 *pack_out = NULL;
622 path_len = strlen(path);
623 p = packfile_alloc(path_len + 2);
e1de726c 624 GITERR_CHECK_ALLOC(p);
a070f152
CMN
625
626 /*
627 * Make sure a corresponding .pack file exists and that
628 * the index looks sane.
629 */
932669b8 630 path_len -= strlen(".idx");
a070f152 631 if (path_len < 1) {
3286c408 632 git__free(p);
282283ac 633 return git_odb__error_notfound("invalid packfile path", NULL);
a070f152
CMN
634 }
635
636 memcpy(p->pack_name, path, path_len);
637
638 strcpy(p->pack_name + path_len, ".keep");
1a481123 639 if (git_path_exists(p->pack_name) == true)
a070f152
CMN
640 p->pack_keep = 1;
641
642 strcpy(p->pack_name + path_len, ".pack");
e1de726c 643 if (p_stat(p->pack_name, &st) < 0 || !S_ISREG(st.st_mode)) {
3286c408 644 git__free(p);
282283ac 645 return git_odb__error_notfound("packfile not found", NULL);
a070f152
CMN
646 }
647
648 /* ok, it looks sane as far as we can check without
649 * actually mapping the pack file.
650 */
1af56d7d 651 p->mwf.size = st.st_size;
a070f152
CMN
652 p->pack_local = 1;
653 p->mtime = (git_time_t)st.st_mtime;
654
655 /* see if we can parse the sha1 oid in the packfile name */
656 if (path_len < 40 ||
e1de726c 657 git_oid_fromstr(&p->sha1, path + path_len - GIT_OID_HEXSZ) < 0)
a070f152
CMN
658 memset(&p->sha1, 0x0, GIT_OID_RAWSZ);
659
660 *pack_out = p;
e1de726c
RB
661
662 return 0;
a070f152
CMN
663}
664
665/***********************************************************
666 *
667 * PACKFILE ENTRY SEARCH INTERNALS
668 *
669 ***********************************************************/
670
e1de726c 671static git_off_t nth_packed_object_offset(const struct git_pack_file *p, uint32_t n)
a070f152
CMN
672{
673 const unsigned char *index = p->index_map.data;
674 index += 4 * 256;
675 if (p->index_version == 1) {
676 return ntohl(*((uint32_t *)(index + 24 * n)));
677 } else {
678 uint32_t off;
679 index += 8 + p->num_objects * (20 + 4);
680 off = ntohl(*((uint32_t *)(index + 4 * n)));
681 if (!(off & 0x80000000))
682 return off;
683 index += p->num_objects * 4 + (off & 0x7fffffff) * 8;
684 return (((uint64_t)ntohl(*((uint32_t *)(index + 0)))) << 32) |
87d9869f 685 ntohl(*((uint32_t *)(index + 4)));
a070f152
CMN
686 }
687}
688
689static int pack_entry_find_offset(
e1de726c
RB
690 git_off_t *offset_out,
691 git_oid *found_oid,
692 struct git_pack_file *p,
693 const git_oid *short_oid,
694 unsigned int len)
a070f152
CMN
695{
696 const uint32_t *level1_ofs = p->index_map.data;
697 const unsigned char *index = p->index_map.data;
698 unsigned hi, lo, stride;
699 int pos, found = 0;
700 const unsigned char *current = 0;
701
702 *offset_out = 0;
703
704 if (index == NULL) {
705 int error;
706
e1de726c
RB
707 if ((error = pack_index_open(p)) < 0)
708 return error;
a070f152
CMN
709
710 assert(p->index_map.data);
711
712 index = p->index_map.data;
713 level1_ofs = p->index_map.data;
714 }
715
716 if (p->index_version > 1) {
717 level1_ofs += 2;
718 index += 8;
719 }
720
721 index += 4 * 256;
722 hi = ntohl(level1_ofs[(int)short_oid->id[0]]);
723 lo = ((short_oid->id[0] == 0x0) ? 0 : ntohl(level1_ofs[(int)short_oid->id[0] - 1]));
724
725 if (p->index_version > 1) {
726 stride = 20;
727 } else {
728 stride = 24;
729 index += 4;
730 }
731
732#ifdef INDEX_DEBUG_LOOKUP
733 printf("%02x%02x%02x... lo %u hi %u nr %d\n",
734 short_oid->id[0], short_oid->id[1], short_oid->id[2], lo, hi, p->num_objects);
735#endif
736
737 /* Use git.git lookup code */
87d9869f 738 pos = sha1_entry_pos(index, stride, 0, lo, hi, p->num_objects, short_oid->id);
a070f152
CMN
739
740 if (pos >= 0) {
741 /* An object matching exactly the oid was found */
742 found = 1;
743 current = index + pos * stride;
744 } else {
745 /* No object was found */
746 /* pos refers to the object with the "closest" oid to short_oid */
747 pos = - 1 - pos;
748 if (pos < (int)p->num_objects) {
749 current = index + pos * stride;
750
282283ac 751 if (!git_oid_ncmp(short_oid, (const git_oid *)current, len))
a070f152 752 found = 1;
a070f152
CMN
753 }
754 }
755
b2a2702d 756 if (found && len != GIT_OID_HEXSZ && pos + 1 < (int)p->num_objects) {
a070f152
CMN
757 /* Check for ambiguousity */
758 const unsigned char *next = current + stride;
759
760 if (!git_oid_ncmp(short_oid, (const git_oid *)next, len)) {
761 found = 2;
762 }
763 }
764
e1de726c 765 if (!found)
282283ac 766 return git_odb__error_notfound("failed to find offset for pack entry", short_oid);
e1de726c
RB
767 if (found > 1)
768 return git_odb__error_ambiguous("found multiple offsets for pack entry");
769 *offset_out = nth_packed_object_offset(p, pos);
770 git_oid_fromraw(found_oid, current);
a070f152
CMN
771
772#ifdef INDEX_DEBUG_LOOKUP
e1de726c 773 {
a070f152
CMN
774 unsigned char hex_sha1[GIT_OID_HEXSZ + 1];
775 git_oid_fmt(hex_sha1, found_oid);
776 hex_sha1[GIT_OID_HEXSZ] = '\0';
777 printf("found lo=%d %s\n", lo, hex_sha1);
a070f152 778 }
e1de726c
RB
779#endif
780 return 0;
a070f152
CMN
781}
782
783int git_pack_entry_find(
784 struct git_pack_entry *e,
785 struct git_pack_file *p,
786 const git_oid *short_oid,
787 unsigned int len)
788{
e1de726c 789 git_off_t offset;
a070f152
CMN
790 git_oid found_oid;
791 int error;
792
793 assert(p);
794
795 if (len == GIT_OID_HEXSZ && p->num_bad_objects) {
796 unsigned i;
797 for (i = 0; i < p->num_bad_objects; i++)
798 if (git_oid_cmp(short_oid, &p->bad_object_sha1[i]) == 0)
e1de726c 799 return packfile_error("bad object found in packfile");
a070f152
CMN
800 }
801
802 error = pack_entry_find_offset(&offset, &found_oid, p, short_oid, len);
e1de726c
RB
803 if (error < 0)
804 return error;
a070f152
CMN
805
806 /* we found a unique entry in the index;
807 * make sure the packfile backing the index
808 * still exists on disk */
e1de726c
RB
809 if (p->mwf.fd == -1 && (error = packfile_open(p)) < 0)
810 return error;
a070f152
CMN
811
812 e->offset = offset;
813 e->p = p;
814
815 git_oid_cpy(&e->sha1, &found_oid);
e1de726c 816 return 0;
a070f152 817}