]> git.proxmox.com Git - libgit2.git/blame - src/pack.c
pack: do not repeat the same error message four times
[libgit2.git] / src / pack.c
CommitLineData
7d0cdf82 1/*
359fc2d2 2 * Copyright (C) the libgit2 contributors. All rights reserved.
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"
b7f167da 15#include "oid.h"
7d0cdf82 16
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,
b8457baa 41 size_t len);
a070f152 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
c8f79c2b
CMN
49/********************
50 * Delta base cache
51 ********************/
c0f4a011 52
525d961c 53static git_pack_cache_entry *new_cache_object(git_rawobj *source)
c0f4a011 54{
c8f79c2b 55 git_pack_cache_entry *e = git__calloc(1, sizeof(git_pack_cache_entry));
c0f4a011
CMN
56 if (!e)
57 return NULL;
58
c0f4a011
CMN
59 memcpy(&e->raw, source, sizeof(git_rawobj));
60
61 return e;
62}
63
64static void free_cache_object(void *o)
65{
66 git_pack_cache_entry *e = (git_pack_cache_entry *)o;
67
68 if (e != NULL) {
0ed75620 69 assert(e->refcount.val == 0);
c0f4a011
CMN
70 git__free(e->raw.data);
71 git__free(e);
72 }
73}
74
c8f79c2b
CMN
75static void cache_free(git_pack_cache *cache)
76{
77 khiter_t k;
78
79 if (cache->entries) {
80 for (k = kh_begin(cache->entries); k != kh_end(cache->entries); k++) {
81 if (kh_exist(cache->entries, k))
82 free_cache_object(kh_value(cache->entries, k));
83 }
84
85 git_offmap_free(cache->entries);
aa3bf89d 86 git_mutex_free(&cache->lock);
c8f79c2b 87 }
f658dc43
RB
88
89 memset(cache, 0, sizeof(*cache));
c8f79c2b
CMN
90}
91
92static int cache_init(git_pack_cache *cache)
93{
f658dc43
RB
94 memset(cache, 0, sizeof(*cache));
95
c8f79c2b
CMN
96 cache->entries = git_offmap_alloc();
97 GITERR_CHECK_ALLOC(cache->entries);
f658dc43 98
c8f79c2b 99 cache->memory_limit = GIT_PACK_CACHE_MEMORY_LIMIT;
1a42dd17
RB
100
101 if (git_mutex_init(&cache->lock)) {
102 giterr_set(GITERR_OS, "Failed to initialize pack cache mutex");
103
104 git__free(cache->entries);
105 cache->entries = NULL;
106
107 return -1;
108 }
c8f79c2b
CMN
109
110 return 0;
111}
112
09e29e47 113static git_pack_cache_entry *cache_get(git_pack_cache *cache, git_off_t offset)
c8f79c2b
CMN
114{
115 khiter_t k;
116 git_pack_cache_entry *entry = NULL;
117
09e29e47
CMN
118 if (git_mutex_lock(&cache->lock) < 0)
119 return NULL;
120
c8f79c2b
CMN
121 k = kh_get(off, cache->entries, offset);
122 if (k != kh_end(cache->entries)) { /* found it */
123 entry = kh_value(cache->entries, k);
124 git_atomic_inc(&entry->refcount);
0ed75620 125 entry->last_usage = cache->use_ctr++;
c8f79c2b
CMN
126 }
127 git_mutex_unlock(&cache->lock);
128
129 return entry;
130}
131
0ed75620
CMN
132/* Run with the cache lock held */
133static void free_lowest_entry(git_pack_cache *cache)
134{
ed6648ba
CMN
135 git_pack_cache_entry *entry;
136 khiter_t k;
137
0ed75620
CMN
138 for (k = kh_begin(cache->entries); k != kh_end(cache->entries); k++) {
139 if (!kh_exist(cache->entries, k))
140 continue;
141
142 entry = kh_value(cache->entries, k);
ed6648ba 143
9c62aaab
CMN
144 if (entry && entry->refcount.val == 0) {
145 cache->memory_used -= entry->raw.len;
146 kh_del(off, cache->entries, k);
147 free_cache_object(entry);
ed6648ba
CMN
148 }
149 }
0ed75620
CMN
150}
151
c8f79c2b
CMN
152static int cache_add(git_pack_cache *cache, git_rawobj *base, git_off_t offset)
153{
154 git_pack_cache_entry *entry;
155 int error, exists = 0;
156 khiter_t k;
157
0ed75620
CMN
158 if (base->len > GIT_PACK_CACHE_SIZE_LIMIT)
159 return -1;
160
c8f79c2b
CMN
161 entry = new_cache_object(base);
162 if (entry) {
09e29e47
CMN
163 if (git_mutex_lock(&cache->lock) < 0) {
164 giterr_set(GITERR_OS, "failed to lock cache");
165 return -1;
166 }
c8f79c2b
CMN
167 /* Add it to the cache if nobody else has */
168 exists = kh_get(off, cache->entries, offset) != kh_end(cache->entries);
169 if (!exists) {
0ed75620
CMN
170 while (cache->memory_used + base->len > cache->memory_limit)
171 free_lowest_entry(cache);
172
c8f79c2b
CMN
173 k = kh_put(off, cache->entries, offset, &error);
174 assert(error != 0);
175 kh_value(cache->entries, k) = entry;
0ed75620 176 cache->memory_used += entry->raw.len;
c8f79c2b
CMN
177 }
178 git_mutex_unlock(&cache->lock);
179 /* Somebody beat us to adding it into the cache */
180 if (exists) {
181 git__free(entry);
182 return -1;
183 }
184 }
185
186 return 0;
187}
188
a070f152
CMN
189/***********************************************************
190 *
191 * PACK INDEX METHODS
192 *
193 ***********************************************************/
194
195static void pack_index_free(struct git_pack_file *p)
196{
60ecdf59
DMB
197 if (p->oids) {
198 git__free(p->oids);
199 p->oids = NULL;
200 }
a070f152
CMN
201 if (p->index_map.data) {
202 git_futils_mmap_free(&p->index_map);
203 p->index_map.data = NULL;
204 }
205}
206
87d9869f 207static int pack_index_check(const char *path, struct git_pack_file *p)
a070f152
CMN
208{
209 struct git_pack_idx_header *hdr;
210 uint32_t version, nr, i, *index;
a070f152
CMN
211 void *idx_map;
212 size_t idx_size;
a070f152 213 struct stat st;
a070f152 214 int error;
e1de726c
RB
215 /* TODO: properly open the file without access time using O_NOATIME */
216 git_file fd = git_futils_open_ro(path);
a070f152 217 if (fd < 0)
e1de726c 218 return fd;
a070f152 219
9d2f841a
RB
220 if (p_fstat(fd, &st) < 0) {
221 p_close(fd);
222 giterr_set(GITERR_OS, "Unable to stat pack index '%s'", path);
223 return -1;
224 }
225
226 if (!S_ISREG(st.st_mode) ||
e1de726c
RB
227 !git__is_sizet(st.st_size) ||
228 (idx_size = (size_t)st.st_size) < 4 * 256 + 20 + 20)
229 {
a070f152 230 p_close(fd);
9d2f841a 231 giterr_set(GITERR_ODB, "Invalid pack index '%s'", path);
e1de726c 232 return -1;
a070f152
CMN
233 }
234
235 error = git_futils_mmap_ro(&p->index_map, fd, 0, idx_size);
e1de726c 236
a070f152
CMN
237 p_close(fd);
238
e1de726c
RB
239 if (error < 0)
240 return error;
a070f152
CMN
241
242 hdr = idx_map = p->index_map.data;
243
244 if (hdr->idx_signature == htonl(PACK_IDX_SIGNATURE)) {
245 version = ntohl(hdr->idx_version);
246
247 if (version < 2 || version > 2) {
248 git_futils_mmap_free(&p->index_map);
e1de726c 249 return packfile_error("unsupported index version");
a070f152
CMN
250 }
251
252 } else
253 version = 1;
254
255 nr = 0;
256 index = idx_map;
257
258 if (version > 1)
87d9869f 259 index += 2; /* skip index header */
a070f152
CMN
260
261 for (i = 0; i < 256; i++) {
262 uint32_t n = ntohl(index[i]);
263 if (n < nr) {
264 git_futils_mmap_free(&p->index_map);
e1de726c 265 return packfile_error("index is non-monotonic");
a070f152
CMN
266 }
267 nr = n;
268 }
269
270 if (version == 1) {
271 /*
272 * Total size:
87d9869f
VM
273 * - 256 index entries 4 bytes each
274 * - 24-byte entries * nr (20-byte sha1 + 4-byte offset)
275 * - 20-byte SHA1 of the packfile
276 * - 20-byte SHA1 file checksum
a070f152
CMN
277 */
278 if (idx_size != 4*256 + nr * 24 + 20 + 20) {
279 git_futils_mmap_free(&p->index_map);
e1de726c 280 return packfile_error("index is corrupted");
a070f152
CMN
281 }
282 } else if (version == 2) {
283 /*
284 * Minimum size:
87d9869f
VM
285 * - 8 bytes of header
286 * - 256 index entries 4 bytes each
287 * - 20-byte sha1 entry * nr
288 * - 4-byte crc entry * nr
289 * - 4-byte offset entry * nr
290 * - 20-byte SHA1 of the packfile
291 * - 20-byte SHA1 file checksum
a070f152
CMN
292 * And after the 4-byte offset table might be a
293 * variable sized table containing 8-byte entries
294 * for offsets larger than 2^31.
295 */
296 unsigned long min_size = 8 + 4*256 + nr*(20 + 4 + 4) + 20 + 20;
297 unsigned long max_size = min_size;
298
299 if (nr)
300 max_size += (nr - 1)*8;
301
302 if (idx_size < min_size || idx_size > max_size) {
303 git_futils_mmap_free(&p->index_map);
e1de726c 304 return packfile_error("wrong index size");
a070f152 305 }
a070f152
CMN
306 }
307
a070f152 308 p->num_objects = nr;
0ddfcb40 309 p->index_version = version;
e1de726c 310 return 0;
a070f152
CMN
311}
312
313static int pack_index_open(struct git_pack_file *p)
314{
315 char *idx_name;
24c70804
RB
316 int error = 0;
317 size_t name_len, base_len;
318
0ddfcb40 319 if (p->index_version > -1)
53607868 320 return 0;
a070f152 321
24c70804
RB
322 name_len = strlen(p->pack_name);
323 assert(name_len > strlen(".pack")); /* checked by git_pack_file alloc */
e1de726c 324
53607868
RB
325 if ((idx_name = git__malloc(name_len)) == NULL)
326 return -1;
44ef8b1b 327
24c70804
RB
328 base_len = name_len - strlen(".pack");
329 memcpy(idx_name, p->pack_name, base_len);
330 memcpy(idx_name + base_len, ".idx", sizeof(".idx"));
a070f152 331
050af8bb
RD
332 if ((error = git_mutex_lock(&p->lock)) < 0) {
333 git__free(idx_name);
53607868 334 return error;
050af8bb 335 }
53607868 336
0ddfcb40 337 if (p->index_version == -1)
53607868 338 error = pack_index_check(idx_name, p);
24c70804 339
3286c408 340 git__free(idx_name);
a070f152 341
24c70804
RB
342 git_mutex_unlock(&p->lock);
343
e1de726c 344 return error;
a070f152
CMN
345}
346
347static unsigned char *pack_window_open(
348 struct git_pack_file *p,
7d0cdf82 349 git_mwindow **w_cursor,
e1de726c 350 git_off_t offset,
7d0cdf82
CMN
351 unsigned int *left)
352{
e1de726c 353 if (p->mwf.fd == -1 && packfile_open(p) < 0)
7d0cdf82
CMN
354 return NULL;
355
356 /* Since packfiles end in a hash of their content and it's
357 * pointless to ask for an offset into the middle of that
358 * hash, and the pack_window_contains function above wouldn't match
359 * don't allow an offset too close to the end of the file.
360 */
361 if (offset > (p->mwf.size - 20))
362 return NULL;
363
364 return git_mwindow_open(&p->mwf, w_cursor, offset, 20, left);
365 }
366
51e82492
CMN
367/*
368 * The per-object header is a pretty dense thing, which is
369 * - first byte: low four bits are "size",
370 * then three bits of "type",
371 * with the high bit being "size continues".
372 * - each byte afterwards: low seven bits are size continuation,
373 * with the high bit being "size continues"
374 */
51a3dfb5 375size_t git_packfile__object_header(unsigned char *hdr, size_t size, git_otype type)
51e82492
CMN
376{
377 unsigned char *hdr_base;
378 unsigned char c;
379
380 assert(type >= GIT_OBJ_COMMIT && type <= GIT_OBJ_REF_DELTA);
381
382 /* TODO: add support for chunked objects; see git.git 6c0d19b1 */
383
384 c = (unsigned char)((type << 4) | (size & 15));
385 size >>= 4;
386 hdr_base = hdr;
387
388 while (size) {
389 *hdr++ = c | 0x80;
390 c = size & 0x7f;
391 size >>= 7;
392 }
393 *hdr++ = c;
394
51a3dfb5 395 return (hdr - hdr_base);
51e82492
CMN
396}
397
398
45d773ef
CMN
399static int packfile_unpack_header1(
400 unsigned long *usedp,
7d0cdf82
CMN
401 size_t *sizep,
402 git_otype *type,
403 const unsigned char *buf,
404 unsigned long len)
405{
406 unsigned shift;
407 unsigned long size, c;
408 unsigned long used = 0;
2aeadb9c 409
7d0cdf82
CMN
410 c = buf[used++];
411 *type = (c >> 4) & 7;
412 size = c & 15;
413 shift = 4;
414 while (c & 0x80) {
45d773ef 415 if (len <= used)
904b67e6 416 return GIT_EBUFS;
45d773ef
CMN
417
418 if (bitsizeof(long) <= shift) {
419 *usedp = 0;
420 return -1;
421 }
7d0cdf82
CMN
422
423 c = buf[used++];
424 size += (c & 0x7f) << shift;
425 shift += 7;
426 }
427
428 *sizep = (size_t)size;
45d773ef
CMN
429 *usedp = used;
430 return 0;
7d0cdf82
CMN
431}
432
433int git_packfile_unpack_header(
434 size_t *size_p,
435 git_otype *type_p,
436 git_mwindow_file *mwf,
437 git_mwindow **w_curs,
e1de726c 438 git_off_t *curpos)
7d0cdf82
CMN
439{
440 unsigned char *base;
441 unsigned int left;
442 unsigned long used;
45d773ef 443 int ret;
7d0cdf82
CMN
444
445 /* pack_window_open() assures us we have [base, base + 20) available
87d9869f
VM
446 * as a range that we can look at at. (Its actually the hash
447 * size that is assured.) With our object header encoding
7d0cdf82
CMN
448 * the maximum deflated object size is 2^137, which is just
449 * insane, so we know won't exceed what we have been given.
450 */
24c70804 451/* base = pack_window_open(p, w_curs, *curpos, &left); */
7d0cdf82
CMN
452 base = git_mwindow_open(mwf, w_curs, *curpos, 20, &left);
453 if (base == NULL)
904b67e6 454 return GIT_EBUFS;
2aeadb9c 455
9d2f841a 456 ret = packfile_unpack_header1(&used, size_p, type_p, base, left);
45d773ef 457 git_mwindow_close(w_curs);
904b67e6 458 if (ret == GIT_EBUFS)
45d773ef
CMN
459 return ret;
460 else if (ret < 0)
e1de726c 461 return packfile_error("header length is zero");
7d0cdf82
CMN
462
463 *curpos += used;
e1de726c 464 return 0;
7d0cdf82
CMN
465}
466
44f9f547
DMB
467int git_packfile_resolve_header(
468 size_t *size_p,
469 git_otype *type_p,
470 struct git_pack_file *p,
471 git_off_t offset)
472{
473 git_mwindow *w_curs = NULL;
474 git_off_t curpos = offset;
475 size_t size;
476 git_otype type;
477 git_off_t base_offset;
478 int error;
479
480 error = git_packfile_unpack_header(&size, &type, &p->mwf, &w_curs, &curpos);
481 git_mwindow_close(&w_curs);
482 if (error < 0)
483 return error;
484
485 if (type == GIT_OBJ_OFS_DELTA || type == GIT_OBJ_REF_DELTA) {
486 size_t base_size;
487 git_rawobj delta;
488 base_offset = get_delta_base(p, &w_curs, &curpos, type, offset);
489 git_mwindow_close(&w_curs);
490 error = packfile_unpack_compressed(&delta, p, &w_curs, &curpos, size, type);
491 git_mwindow_close(&w_curs);
492 if (error < 0)
493 return error;
494 error = git__delta_read_header(delta.data, delta.len, &base_size, size_p);
495 git__free(delta.data);
496 if (error < 0)
497 return error;
498 } else
499 *size_p = size;
500
501 while (type == GIT_OBJ_OFS_DELTA || type == GIT_OBJ_REF_DELTA) {
502 curpos = base_offset;
503 error = git_packfile_unpack_header(&size, &type, &p->mwf, &w_curs, &curpos);
504 git_mwindow_close(&w_curs);
505 if (error < 0)
506 return error;
507 if (type != GIT_OBJ_OFS_DELTA && type != GIT_OBJ_REF_DELTA)
508 break;
509 base_offset = get_delta_base(p, &w_curs, &curpos, type, base_offset);
510 git_mwindow_close(&w_curs);
511 }
512 *type_p = type;
513
514 return error;
515}
516
a070f152 517static int packfile_unpack_delta(
7d0cdf82 518 git_rawobj *obj,
a070f152 519 struct git_pack_file *p,
7d0cdf82 520 git_mwindow **w_curs,
e1de726c 521 git_off_t *curpos,
7d0cdf82
CMN
522 size_t delta_size,
523 git_otype delta_type,
e1de726c 524 git_off_t obj_offset)
7d0cdf82 525{
c0f4a011 526 git_off_t base_offset, base_key;
7d0cdf82 527 git_rawobj base, delta;
525d961c 528 git_pack_cache_entry *cached = NULL;
c0f4a011 529 int error, found_base = 0;
7d0cdf82 530
b5b474dd 531 base_offset = get_delta_base(p, w_curs, curpos, delta_type, obj_offset);
45d773ef 532 git_mwindow_close(w_curs);
7d0cdf82 533 if (base_offset == 0)
e1de726c
RB
534 return packfile_error("delta offset is zero");
535 if (base_offset < 0) /* must actually be an error code */
536 return (int)base_offset;
7d0cdf82 537
c8f79c2b
CMN
538 if (!p->bases.entries && (cache_init(&p->bases) < 0))
539 return -1;
7d0cdf82 540
c0f4a011 541 base_key = base_offset; /* git_packfile_unpack modifies base_offset */
c8f79c2b 542 if ((cached = cache_get(&p->bases, base_offset)) != NULL) {
c0f4a011 543 memcpy(&base, &cached->raw, sizeof(git_rawobj));
c8f79c2b 544 found_base = 1;
525d961c 545 }
525d961c
CMN
546
547 if (!cached) { /* have to inflate it */
c0f4a011 548 error = git_packfile_unpack(&base, p, &base_offset);
c0f4a011
CMN
549 if (error < 0)
550 return error;
551 }
7d0cdf82
CMN
552
553 error = packfile_unpack_compressed(&delta, p, w_curs, curpos, delta_size, delta_type);
45d773ef 554 git_mwindow_close(w_curs);
c0f4a011 555
e1de726c 556 if (error < 0) {
c0f4a011
CMN
557 if (!found_base)
558 git__free(base.data);
e1de726c 559 return error;
7d0cdf82
CMN
560 }
561
562 obj->type = base.type;
e1de726c 563 error = git__delta_apply(obj, base.data, base.len, delta.data, delta.len);
c0f4a011
CMN
564 if (error < 0)
565 goto on_error;
566
c8f79c2b 567 if (found_base)
525d961c 568 git_atomic_dec(&cached->refcount);
c8f79c2b
CMN
569 else if (cache_add(&p->bases, &base, base_key) < 0)
570 git__free(base.data);
7d0cdf82 571
c0f4a011 572on_error:
3286c408 573 git__free(delta.data);
7d0cdf82 574
7d0cdf82
CMN
575 return error; /* error set by git__delta_apply */
576}
577
a070f152 578int git_packfile_unpack(
e1de726c
RB
579 git_rawobj *obj,
580 struct git_pack_file *p,
581 git_off_t *obj_offset)
7d0cdf82
CMN
582{
583 git_mwindow *w_curs = NULL;
e1de726c 584 git_off_t curpos = *obj_offset;
7d0cdf82
CMN
585 int error;
586
587 size_t size = 0;
588 git_otype type;
589
590 /*
591 * TODO: optionally check the CRC on the packfile
592 */
593
594 obj->data = NULL;
595 obj->len = 0;
596 obj->type = GIT_OBJ_BAD;
597
598 error = git_packfile_unpack_header(&size, &type, &p->mwf, &w_curs, &curpos);
45d773ef
CMN
599 git_mwindow_close(&w_curs);
600
e1de726c
RB
601 if (error < 0)
602 return error;
7d0cdf82
CMN
603
604 switch (type) {
605 case GIT_OBJ_OFS_DELTA:
606 case GIT_OBJ_REF_DELTA:
607 error = packfile_unpack_delta(
b5b474dd
CMN
608 obj, p, &w_curs, &curpos,
609 size, type, *obj_offset);
7d0cdf82
CMN
610 break;
611
612 case GIT_OBJ_COMMIT:
613 case GIT_OBJ_TREE:
614 case GIT_OBJ_BLOB:
615 case GIT_OBJ_TAG:
616 error = packfile_unpack_compressed(
b5b474dd 617 obj, p, &w_curs, &curpos,
7d0cdf82
CMN
618 size, type);
619 break;
620
621 default:
e1de726c 622 error = packfile_error("invalid packfile type in header");;
7d0cdf82
CMN
623 break;
624 }
625
b5b474dd 626 *obj_offset = curpos;
e1de726c 627 return error;
7d0cdf82
CMN
628}
629
282283ac
RB
630static void *use_git_alloc(void *opaq, unsigned int count, unsigned int size)
631{
632 GIT_UNUSED(opaq);
633 return git__calloc(count, size);
634}
635
636static void use_git_free(void *opaq, void *ptr)
637{
638 GIT_UNUSED(opaq);
639 git__free(ptr);
640}
641
46635339
CMN
642int git_packfile_stream_open(git_packfile_stream *obj, struct git_pack_file *p, git_off_t curpos)
643{
644 int st;
645
646 memset(obj, 0, sizeof(git_packfile_stream));
647 obj->curpos = curpos;
648 obj->p = p;
649 obj->zstream.zalloc = use_git_alloc;
650 obj->zstream.zfree = use_git_free;
651 obj->zstream.next_in = Z_NULL;
652 obj->zstream.next_out = Z_NULL;
653 st = inflateInit(&obj->zstream);
654 if (st != Z_OK) {
655 git__free(obj);
ae081739 656 giterr_set(GITERR_ZLIB, "failed to init packfile stream");
46635339
CMN
657 return -1;
658 }
659
660 return 0;
661}
662
663ssize_t git_packfile_stream_read(git_packfile_stream *obj, void *buffer, size_t len)
664{
665 unsigned char *in;
666 size_t written;
667 int st;
668
669 if (obj->done)
670 return 0;
671
672 in = pack_window_open(obj->p, &obj->mw, obj->curpos, &obj->zstream.avail_in);
673 if (in == NULL)
674 return GIT_EBUFS;
675
676 obj->zstream.next_out = buffer;
090d5e1f 677 obj->zstream.avail_out = (unsigned int)len;
46635339
CMN
678 obj->zstream.next_in = in;
679
680 st = inflate(&obj->zstream, Z_SYNC_FLUSH);
681 git_mwindow_close(&obj->mw);
682
683 obj->curpos += obj->zstream.next_in - in;
684 written = len - obj->zstream.avail_out;
685
686 if (st != Z_OK && st != Z_STREAM_END) {
ae081739 687 giterr_set(GITERR_ZLIB, "error reading from the zlib stream");
46635339
CMN
688 return -1;
689 }
690
691 if (st == Z_STREAM_END)
692 obj->done = 1;
693
694
695 /* If we didn't write anything out but we're not done, we need more data */
696 if (!written && st != Z_STREAM_END)
697 return GIT_EBUFS;
698
699 return written;
700
701}
702
703void git_packfile_stream_free(git_packfile_stream *obj)
704{
705 inflateEnd(&obj->zstream);
706}
707
7d0cdf82 708int packfile_unpack_compressed(
e1de726c
RB
709 git_rawobj *obj,
710 struct git_pack_file *p,
711 git_mwindow **w_curs,
712 git_off_t *curpos,
713 size_t size,
714 git_otype type)
7d0cdf82
CMN
715{
716 int st;
717 z_stream stream;
718 unsigned char *buffer, *in;
719
e1de726c
RB
720 buffer = git__calloc(1, size + 1);
721 GITERR_CHECK_ALLOC(buffer);
7d0cdf82
CMN
722
723 memset(&stream, 0, sizeof(stream));
724 stream.next_out = buffer;
1c3fac4d 725 stream.avail_out = (uInt)size + 1;
282283ac
RB
726 stream.zalloc = use_git_alloc;
727 stream.zfree = use_git_free;
7d0cdf82
CMN
728
729 st = inflateInit(&stream);
730 if (st != Z_OK) {
3286c408 731 git__free(buffer);
ae081739 732 giterr_set(GITERR_ZLIB, "failed to init zlib stream on unpack");
45d773ef 733
e1de726c 734 return -1;
7d0cdf82
CMN
735 }
736
737 do {
b5b474dd 738 in = pack_window_open(p, w_curs, *curpos, &stream.avail_in);
7d0cdf82
CMN
739 stream.next_in = in;
740 st = inflate(&stream, Z_FINISH);
45d773ef 741 git_mwindow_close(w_curs);
7d0cdf82
CMN
742
743 if (!stream.avail_out)
744 break; /* the payload is larger than it should be */
745
45d773ef
CMN
746 if (st == Z_BUF_ERROR && in == NULL) {
747 inflateEnd(&stream);
748 git__free(buffer);
904b67e6 749 return GIT_EBUFS;
45d773ef
CMN
750 }
751
b5b474dd 752 *curpos += stream.next_in - in;
7d0cdf82
CMN
753 } while (st == Z_OK || st == Z_BUF_ERROR);
754
755 inflateEnd(&stream);
756
757 if ((st != Z_STREAM_END) || stream.total_out != size) {
3286c408 758 git__free(buffer);
ae081739 759 giterr_set(GITERR_ZLIB, "error inflating zlib stream");
e1de726c 760 return -1;
7d0cdf82
CMN
761 }
762
763 obj->type = type;
764 obj->len = size;
765 obj->data = buffer;
e1de726c 766 return 0;
7d0cdf82
CMN
767}
768
b5b474dd
CMN
769/*
770 * curpos is where the data starts, delta_obj_offset is the where the
771 * header starts
772 */
e1de726c
RB
773git_off_t get_delta_base(
774 struct git_pack_file *p,
775 git_mwindow **w_curs,
776 git_off_t *curpos,
777 git_otype type,
778 git_off_t delta_obj_offset)
7d0cdf82 779{
45d773ef
CMN
780 unsigned int left = 0;
781 unsigned char *base_info;
e1de726c 782 git_off_t base_offset;
7d0cdf82
CMN
783 git_oid unused;
784
45d773ef
CMN
785 base_info = pack_window_open(p, w_curs, *curpos, &left);
786 /* Assumption: the only reason this would fail is because the file is too small */
787 if (base_info == NULL)
904b67e6 788 return GIT_EBUFS;
7d0cdf82
CMN
789 /* pack_window_open() assured us we have [base_info, base_info + 20)
790 * as a range that we can look at without walking off the
87d9869f
VM
791 * end of the mapped window. Its actually the hash size
792 * that is assured. An OFS_DELTA longer than the hash size
7d0cdf82
CMN
793 * is stupid, as then a REF_DELTA would be smaller to store.
794 */
795 if (type == GIT_OBJ_OFS_DELTA) {
796 unsigned used = 0;
797 unsigned char c = base_info[used++];
798 base_offset = c & 127;
799 while (c & 128) {
45d773ef 800 if (left <= used)
904b67e6 801 return GIT_EBUFS;
7d0cdf82
CMN
802 base_offset += 1;
803 if (!base_offset || MSB(base_offset, 7))
87d9869f 804 return 0; /* overflow */
7d0cdf82
CMN
805 c = base_info[used++];
806 base_offset = (base_offset << 7) + (c & 127);
807 }
808 base_offset = delta_obj_offset - base_offset;
809 if (base_offset <= 0 || base_offset >= delta_obj_offset)
87d9869f 810 return 0; /* out of bound */
7d0cdf82
CMN
811 *curpos += used;
812 } else if (type == GIT_OBJ_REF_DELTA) {
c1af5a39
CMN
813 /* If we have the cooperative cache, search in it first */
814 if (p->has_cache) {
0e040c03
CMN
815 khiter_t k;
816 git_oid oid;
c1af5a39 817
0e040c03
CMN
818 git_oid_fromraw(&oid, base_info);
819 k = kh_get(oid, p->idx_cache, &oid);
820 if (k != kh_end(p->idx_cache)) {
c1af5a39 821 *curpos += 20;
0e040c03 822 return ((struct git_pack_entry *)kh_value(p->idx_cache, k))->offset;
c1af5a39
CMN
823 }
824 }
7d0cdf82 825 /* The base entry _must_ be in the same pack */
e1de726c
RB
826 if (pack_entry_find_offset(&base_offset, &unused, p, (git_oid *)base_info, GIT_OID_HEXSZ) < 0)
827 return packfile_error("base entry delta is not in the same pack");
7d0cdf82
CMN
828 *curpos += 20;
829 } else
830 return 0;
831
832 return base_offset;
833}
a070f152
CMN
834
835/***********************************************************
836 *
837 * PACKFILE METHODS
838 *
839 ***********************************************************/
840
96c9b9f0 841void git_packfile_free(struct git_pack_file *p)
a070f152 842{
24c70804
RB
843 if (!p)
844 return;
845
c8f79c2b 846 cache_free(&p->bases);
c0f4a011 847
a070f152
CMN
848 git_mwindow_free_all(&p->mwf);
849
3a2d48d5 850 if (p->mwf.fd >= 0)
a070f152
CMN
851 p_close(p->mwf.fd);
852
853 pack_index_free(p);
854
3286c408 855 git__free(p->bad_object_sha1);
24c70804 856
24c70804 857 git_mutex_free(&p->lock);
3286c408 858 git__free(p);
a070f152
CMN
859}
860
861static int packfile_open(struct git_pack_file *p)
862{
863 struct stat st;
864 struct git_pack_header hdr;
865 git_oid sha1;
866 unsigned char *idx_sha1;
867
0ddfcb40 868 if (p->index_version == -1 && pack_index_open(p) < 0)
282283ac 869 return git_odb__error_notfound("failed to open packfile", NULL);
a070f152 870
9d2f841a
RB
871 /* if mwf opened by another thread, return now */
872 if (git_mutex_lock(&p->lock) < 0)
873 return packfile_error("failed to get lock for open");
874
875 if (p->mwf.fd >= 0) {
876 git_mutex_unlock(&p->lock);
877 return 0;
878 }
879
a070f152 880 /* TODO: open with noatime */
e1de726c 881 p->mwf.fd = git_futils_open_ro(p->pack_name);
9d2f841a
RB
882 if (p->mwf.fd < 0)
883 goto cleanup;
a070f152 884
e1de726c
RB
885 if (p_fstat(p->mwf.fd, &st) < 0 ||
886 git_mwindow_file_register(&p->mwf) < 0)
887 goto cleanup;
a070f152
CMN
888
889 /* If we created the struct before we had the pack we lack size. */
890 if (!p->mwf.size) {
891 if (!S_ISREG(st.st_mode))
892 goto cleanup;
e1de726c 893 p->mwf.size = (git_off_t)st.st_size;
a070f152
CMN
894 } else if (p->mwf.size != st.st_size)
895 goto cleanup;
896
897#if 0
898 /* We leave these file descriptors open with sliding mmap;
899 * there is no point keeping them open across exec(), though.
900 */
901 fd_flag = fcntl(p->mwf.fd, F_GETFD, 0);
902 if (fd_flag < 0)
e1de726c 903 goto cleanup;
a070f152
CMN
904
905 fd_flag |= FD_CLOEXEC;
906 if (fcntl(p->pack_fd, F_SETFD, fd_flag) == -1)
e1de726c 907 goto cleanup;
a070f152
CMN
908#endif
909
910 /* Verify we recognize this pack file format. */
e1de726c
RB
911 if (p_read(p->mwf.fd, &hdr, sizeof(hdr)) < 0 ||
912 hdr.hdr_signature != htonl(PACK_SIGNATURE) ||
913 !pack_version_ok(hdr.hdr_version))
a070f152
CMN
914 goto cleanup;
915
916 /* Verify the pack matches its index. */
e1de726c
RB
917 if (p->num_objects != ntohl(hdr.hdr_entries) ||
918 p_lseek(p->mwf.fd, p->mwf.size - GIT_OID_RAWSZ, SEEK_SET) == -1 ||
919 p_read(p->mwf.fd, sha1.id, GIT_OID_RAWSZ) < 0)
a070f152
CMN
920 goto cleanup;
921
922 idx_sha1 = ((unsigned char *)p->index_map.data) + p->index_map.len - 40;
923
9d2f841a
RB
924 if (git_oid__cmp(&sha1, (git_oid *)idx_sha1) != 0)
925 goto cleanup;
926
927 git_mutex_unlock(&p->lock);
928 return 0;
a070f152
CMN
929
930cleanup:
e1de726c 931 giterr_set(GITERR_OS, "Invalid packfile '%s'", p->pack_name);
9d2f841a 932
3a2d48d5
SS
933 if (p->mwf.fd >= 0)
934 p_close(p->mwf.fd);
a070f152 935 p->mwf.fd = -1;
9d2f841a
RB
936
937 git_mutex_unlock(&p->lock);
938
e1de726c 939 return -1;
a070f152
CMN
940}
941
5d2d21e5 942int git_packfile_alloc(struct git_pack_file **pack_out, const char *path)
a070f152
CMN
943{
944 struct stat st;
945 struct git_pack_file *p;
38eef611 946 size_t path_len = path ? strlen(path) : 0;
a070f152
CMN
947
948 *pack_out = NULL;
24c70804 949
38eef611 950 if (path_len < strlen(".idx"))
24c70804
RB
951 return git_odb__error_notfound("invalid packfile path", NULL);
952
38eef611 953 p = git__calloc(1, sizeof(*p) + path_len + 2);
e1de726c 954 GITERR_CHECK_ALLOC(p);
a070f152 955
38eef611
RB
956 memcpy(p->pack_name, path, path_len + 1);
957
a070f152
CMN
958 /*
959 * Make sure a corresponding .pack file exists and that
960 * the index looks sane.
961 */
38eef611
RB
962 if (git__suffixcmp(path, ".idx") == 0) {
963 size_t root_len = path_len - strlen(".idx");
964
965 memcpy(p->pack_name + root_len, ".keep", sizeof(".keep"));
966 if (git_path_exists(p->pack_name) == true)
967 p->pack_keep = 1;
a070f152 968
38eef611
RB
969 memcpy(p->pack_name + root_len, ".pack", sizeof(".pack"));
970 path_len = path_len - strlen(".idx") + strlen(".pack");
971 }
a070f152 972
e1de726c 973 if (p_stat(p->pack_name, &st) < 0 || !S_ISREG(st.st_mode)) {
3286c408 974 git__free(p);
282283ac 975 return git_odb__error_notfound("packfile not found", NULL);
a070f152
CMN
976 }
977
978 /* ok, it looks sane as far as we can check without
979 * actually mapping the pack file.
980 */
38eef611 981 p->mwf.fd = -1;
1af56d7d 982 p->mwf.size = st.st_size;
a070f152
CMN
983 p->pack_local = 1;
984 p->mtime = (git_time_t)st.st_mtime;
0ddfcb40 985 p->index_version = -1;
a070f152 986
1a42dd17
RB
987 if (git_mutex_init(&p->lock)) {
988 giterr_set(GITERR_OS, "Failed to initialize packfile mutex");
989 git__free(p);
990 return -1;
991 }
38eef611 992
a070f152 993 *pack_out = p;
e1de726c
RB
994
995 return 0;
a070f152
CMN
996}
997
998/***********************************************************
999 *
1000 * PACKFILE ENTRY SEARCH INTERNALS
1001 *
1002 ***********************************************************/
1003
e1de726c 1004static git_off_t nth_packed_object_offset(const struct git_pack_file *p, uint32_t n)
a070f152
CMN
1005{
1006 const unsigned char *index = p->index_map.data;
1007 index += 4 * 256;
1008 if (p->index_version == 1) {
1009 return ntohl(*((uint32_t *)(index + 24 * n)));
1010 } else {
1011 uint32_t off;
1012 index += 8 + p->num_objects * (20 + 4);
1013 off = ntohl(*((uint32_t *)(index + 4 * n)));
1014 if (!(off & 0x80000000))
1015 return off;
1016 index += p->num_objects * 4 + (off & 0x7fffffff) * 8;
1017 return (((uint64_t)ntohl(*((uint32_t *)(index + 0)))) << 32) |
87d9869f 1018 ntohl(*((uint32_t *)(index + 4)));
a070f152
CMN
1019 }
1020}
1021
60ecdf59
DMB
1022static int git__memcmp4(const void *a, const void *b) {
1023 return memcmp(a, b, 4);
1024}
1025
521aedad 1026int git_pack_foreach_entry(
5dca2010 1027 struct git_pack_file *p,
c3fb7d04 1028 git_odb_foreach_cb cb,
5dca2010 1029 void *data)
521aedad
CMN
1030{
1031 const unsigned char *index = p->index_map.data, *current;
521aedad 1032 uint32_t i;
dab89f9b 1033 int error = 0;
521aedad
CMN
1034
1035 if (index == NULL) {
521aedad
CMN
1036 if ((error = pack_index_open(p)) < 0)
1037 return error;
1038
1039 assert(p->index_map.data);
1040
1041 index = p->index_map.data;
1042 }
1043
1044 if (p->index_version > 1) {
1045 index += 8;
1046 }
1047
1048 index += 4 * 256;
1049
60ecdf59
DMB
1050 if (p->oids == NULL) {
1051 git_vector offsets, oids;
521aedad 1052
60ecdf59
DMB
1053 if ((error = git_vector_init(&oids, p->num_objects, NULL)))
1054 return error;
1055
1056 if ((error = git_vector_init(&offsets, p->num_objects, git__memcmp4)))
1057 return error;
5dca2010 1058
60ecdf59
DMB
1059 if (p->index_version > 1) {
1060 const unsigned char *off = index + 24 * p->num_objects;
1061 for (i = 0; i < p->num_objects; i++)
1062 git_vector_insert(&offsets, (void*)&off[4 * i]);
1063 git_vector_sort(&offsets);
1064 git_vector_foreach(&offsets, i, current)
1065 git_vector_insert(&oids, (void*)&index[5 * (current - off)]);
1066 } else {
1067 for (i = 0; i < p->num_objects; i++)
1068 git_vector_insert(&offsets, (void*)&index[24 * i]);
1069 git_vector_sort(&offsets);
1070 git_vector_foreach(&offsets, i, current)
1071 git_vector_insert(&oids, (void*)&current[4]);
1072 }
25e0b157 1073
60ecdf59 1074 git_vector_free(&offsets);
25e0b157 1075 p->oids = (git_oid **)git_vector_detach(NULL, NULL, &oids);
521aedad
CMN
1076 }
1077
60ecdf59 1078 for (i = 0; i < p->num_objects; i++)
26c1cb91
RB
1079 if ((error = cb(p->oids[i], data)) != 0)
1080 return giterr_set_after_callback(error);
60ecdf59 1081
25e0b157 1082 return error;
521aedad
CMN
1083}
1084
a070f152 1085static int pack_entry_find_offset(
e1de726c
RB
1086 git_off_t *offset_out,
1087 git_oid *found_oid,
1088 struct git_pack_file *p,
1089 const git_oid *short_oid,
b8457baa 1090 size_t len)
a070f152 1091{
34bd5999
CMN
1092 const uint32_t *level1_ofs = p->index_map.data;
1093 const unsigned char *index = p->index_map.data;
a070f152
CMN
1094 unsigned hi, lo, stride;
1095 int pos, found = 0;
1096 const unsigned char *current = 0;
1097
1098 *offset_out = 0;
1099
0ddfcb40 1100 if (p->index_version == -1) {
34bd5999 1101 int error;
a070f152 1102
34bd5999
CMN
1103 if ((error = pack_index_open(p)) < 0)
1104 return error;
1105 assert(p->index_map.data);
8c535f3f 1106
34bd5999
CMN
1107 index = p->index_map.data;
1108 level1_ofs = p->index_map.data;
1109 }
a070f152
CMN
1110
1111 if (p->index_version > 1) {
1112 level1_ofs += 2;
1113 index += 8;
1114 }
1115
1116 index += 4 * 256;
1117 hi = ntohl(level1_ofs[(int)short_oid->id[0]]);
1118 lo = ((short_oid->id[0] == 0x0) ? 0 : ntohl(level1_ofs[(int)short_oid->id[0] - 1]));
1119
1120 if (p->index_version > 1) {
1121 stride = 20;
1122 } else {
1123 stride = 24;
1124 index += 4;
1125 }
1126
1127#ifdef INDEX_DEBUG_LOOKUP
1128 printf("%02x%02x%02x... lo %u hi %u nr %d\n",
1129 short_oid->id[0], short_oid->id[1], short_oid->id[2], lo, hi, p->num_objects);
1130#endif
1131
67591c8c 1132#ifdef GIT_USE_LOOKUP
87d9869f 1133 pos = sha1_entry_pos(index, stride, 0, lo, hi, p->num_objects, short_oid->id);
67591c8c
VM
1134#else
1135 pos = sha1_position(index, stride, lo, hi, short_oid->id);
1136#endif
a070f152
CMN
1137
1138 if (pos >= 0) {
1139 /* An object matching exactly the oid was found */
1140 found = 1;
1141 current = index + pos * stride;
1142 } else {
1143 /* No object was found */
1144 /* pos refers to the object with the "closest" oid to short_oid */
1145 pos = - 1 - pos;
1146 if (pos < (int)p->num_objects) {
1147 current = index + pos * stride;
1148
282283ac 1149 if (!git_oid_ncmp(short_oid, (const git_oid *)current, len))
a070f152 1150 found = 1;
a070f152
CMN
1151 }
1152 }
1153
b2a2702d 1154 if (found && len != GIT_OID_HEXSZ && pos + 1 < (int)p->num_objects) {
a070f152
CMN
1155 /* Check for ambiguousity */
1156 const unsigned char *next = current + stride;
1157
1158 if (!git_oid_ncmp(short_oid, (const git_oid *)next, len)) {
1159 found = 2;
1160 }
1161 }
1162
e1de726c 1163 if (!found)
282283ac 1164 return git_odb__error_notfound("failed to find offset for pack entry", short_oid);
e1de726c
RB
1165 if (found > 1)
1166 return git_odb__error_ambiguous("found multiple offsets for pack entry");
24c70804 1167
e1de726c
RB
1168 *offset_out = nth_packed_object_offset(p, pos);
1169 git_oid_fromraw(found_oid, current);
a070f152
CMN
1170
1171#ifdef INDEX_DEBUG_LOOKUP
e1de726c 1172 {
a070f152
CMN
1173 unsigned char hex_sha1[GIT_OID_HEXSZ + 1];
1174 git_oid_fmt(hex_sha1, found_oid);
1175 hex_sha1[GIT_OID_HEXSZ] = '\0';
1176 printf("found lo=%d %s\n", lo, hex_sha1);
a070f152 1177 }
e1de726c 1178#endif
24c70804 1179
e1de726c 1180 return 0;
a070f152
CMN
1181}
1182
1183int git_pack_entry_find(
1184 struct git_pack_entry *e,
1185 struct git_pack_file *p,
1186 const git_oid *short_oid,
b8457baa 1187 size_t len)
a070f152 1188{
e1de726c 1189 git_off_t offset;
a070f152
CMN
1190 git_oid found_oid;
1191 int error;
1192
1193 assert(p);
1194
1195 if (len == GIT_OID_HEXSZ && p->num_bad_objects) {
1196 unsigned i;
1197 for (i = 0; i < p->num_bad_objects; i++)
b7f167da 1198 if (git_oid__cmp(short_oid, &p->bad_object_sha1[i]) == 0)
e1de726c 1199 return packfile_error("bad object found in packfile");
a070f152
CMN
1200 }
1201
1202 error = pack_entry_find_offset(&offset, &found_oid, p, short_oid, len);
e1de726c
RB
1203 if (error < 0)
1204 return error;
a070f152
CMN
1205
1206 /* we found a unique entry in the index;
1207 * make sure the packfile backing the index
1208 * still exists on disk */
e1de726c
RB
1209 if (p->mwf.fd == -1 && (error = packfile_open(p)) < 0)
1210 return error;
a070f152
CMN
1211
1212 e->offset = offset;
1213 e->p = p;
1214
1215 git_oid_cpy(&e->sha1, &found_oid);
e1de726c 1216 return 0;
a070f152 1217}