]> git.proxmox.com Git - libgit2.git/blame - src/pack.c
Further callback error check style fixes
[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
CMN
548 error = git_packfile_unpack(&base, p, &base_offset);
549
550 /*
551 * TODO: git.git tries to load the base from other packfiles
552 * or loose objects.
553 *
554 * We'll need to do this in order to support thin packs.
555 */
556 if (error < 0)
557 return error;
558 }
7d0cdf82
CMN
559
560 error = packfile_unpack_compressed(&delta, p, w_curs, curpos, delta_size, delta_type);
45d773ef 561 git_mwindow_close(w_curs);
c0f4a011 562
e1de726c 563 if (error < 0) {
c0f4a011
CMN
564 if (!found_base)
565 git__free(base.data);
e1de726c 566 return error;
7d0cdf82
CMN
567 }
568
569 obj->type = base.type;
e1de726c 570 error = git__delta_apply(obj, base.data, base.len, delta.data, delta.len);
c0f4a011
CMN
571 if (error < 0)
572 goto on_error;
573
c8f79c2b 574 if (found_base)
525d961c 575 git_atomic_dec(&cached->refcount);
c8f79c2b
CMN
576 else if (cache_add(&p->bases, &base, base_key) < 0)
577 git__free(base.data);
7d0cdf82 578
c0f4a011 579on_error:
3286c408 580 git__free(delta.data);
7d0cdf82 581
7d0cdf82
CMN
582 return error; /* error set by git__delta_apply */
583}
584
a070f152 585int git_packfile_unpack(
e1de726c
RB
586 git_rawobj *obj,
587 struct git_pack_file *p,
588 git_off_t *obj_offset)
7d0cdf82
CMN
589{
590 git_mwindow *w_curs = NULL;
e1de726c 591 git_off_t curpos = *obj_offset;
7d0cdf82
CMN
592 int error;
593
594 size_t size = 0;
595 git_otype type;
596
597 /*
598 * TODO: optionally check the CRC on the packfile
599 */
600
601 obj->data = NULL;
602 obj->len = 0;
603 obj->type = GIT_OBJ_BAD;
604
605 error = git_packfile_unpack_header(&size, &type, &p->mwf, &w_curs, &curpos);
45d773ef
CMN
606 git_mwindow_close(&w_curs);
607
e1de726c
RB
608 if (error < 0)
609 return error;
7d0cdf82
CMN
610
611 switch (type) {
612 case GIT_OBJ_OFS_DELTA:
613 case GIT_OBJ_REF_DELTA:
614 error = packfile_unpack_delta(
b5b474dd
CMN
615 obj, p, &w_curs, &curpos,
616 size, type, *obj_offset);
7d0cdf82
CMN
617 break;
618
619 case GIT_OBJ_COMMIT:
620 case GIT_OBJ_TREE:
621 case GIT_OBJ_BLOB:
622 case GIT_OBJ_TAG:
623 error = packfile_unpack_compressed(
b5b474dd 624 obj, p, &w_curs, &curpos,
7d0cdf82
CMN
625 size, type);
626 break;
627
628 default:
e1de726c 629 error = packfile_error("invalid packfile type in header");;
7d0cdf82
CMN
630 break;
631 }
632
b5b474dd 633 *obj_offset = curpos;
e1de726c 634 return error;
7d0cdf82
CMN
635}
636
282283ac
RB
637static void *use_git_alloc(void *opaq, unsigned int count, unsigned int size)
638{
639 GIT_UNUSED(opaq);
640 return git__calloc(count, size);
641}
642
643static void use_git_free(void *opaq, void *ptr)
644{
645 GIT_UNUSED(opaq);
646 git__free(ptr);
647}
648
46635339
CMN
649int git_packfile_stream_open(git_packfile_stream *obj, struct git_pack_file *p, git_off_t curpos)
650{
651 int st;
652
653 memset(obj, 0, sizeof(git_packfile_stream));
654 obj->curpos = curpos;
655 obj->p = p;
656 obj->zstream.zalloc = use_git_alloc;
657 obj->zstream.zfree = use_git_free;
658 obj->zstream.next_in = Z_NULL;
659 obj->zstream.next_out = Z_NULL;
660 st = inflateInit(&obj->zstream);
661 if (st != Z_OK) {
662 git__free(obj);
663 giterr_set(GITERR_ZLIB, "Failed to inflate packfile");
664 return -1;
665 }
666
667 return 0;
668}
669
670ssize_t git_packfile_stream_read(git_packfile_stream *obj, void *buffer, size_t len)
671{
672 unsigned char *in;
673 size_t written;
674 int st;
675
676 if (obj->done)
677 return 0;
678
679 in = pack_window_open(obj->p, &obj->mw, obj->curpos, &obj->zstream.avail_in);
680 if (in == NULL)
681 return GIT_EBUFS;
682
683 obj->zstream.next_out = buffer;
090d5e1f 684 obj->zstream.avail_out = (unsigned int)len;
46635339
CMN
685 obj->zstream.next_in = in;
686
687 st = inflate(&obj->zstream, Z_SYNC_FLUSH);
688 git_mwindow_close(&obj->mw);
689
690 obj->curpos += obj->zstream.next_in - in;
691 written = len - obj->zstream.avail_out;
692
693 if (st != Z_OK && st != Z_STREAM_END) {
694 giterr_set(GITERR_ZLIB, "Failed to inflate packfile");
695 return -1;
696 }
697
698 if (st == Z_STREAM_END)
699 obj->done = 1;
700
701
702 /* If we didn't write anything out but we're not done, we need more data */
703 if (!written && st != Z_STREAM_END)
704 return GIT_EBUFS;
705
706 return written;
707
708}
709
710void git_packfile_stream_free(git_packfile_stream *obj)
711{
712 inflateEnd(&obj->zstream);
713}
714
7d0cdf82 715int packfile_unpack_compressed(
e1de726c
RB
716 git_rawobj *obj,
717 struct git_pack_file *p,
718 git_mwindow **w_curs,
719 git_off_t *curpos,
720 size_t size,
721 git_otype type)
7d0cdf82
CMN
722{
723 int st;
724 z_stream stream;
725 unsigned char *buffer, *in;
726
e1de726c
RB
727 buffer = git__calloc(1, size + 1);
728 GITERR_CHECK_ALLOC(buffer);
7d0cdf82
CMN
729
730 memset(&stream, 0, sizeof(stream));
731 stream.next_out = buffer;
1c3fac4d 732 stream.avail_out = (uInt)size + 1;
282283ac
RB
733 stream.zalloc = use_git_alloc;
734 stream.zfree = use_git_free;
7d0cdf82
CMN
735
736 st = inflateInit(&stream);
737 if (st != Z_OK) {
3286c408 738 git__free(buffer);
e1de726c 739 giterr_set(GITERR_ZLIB, "Failed to inflate packfile");
45d773ef 740
e1de726c 741 return -1;
7d0cdf82
CMN
742 }
743
744 do {
b5b474dd 745 in = pack_window_open(p, w_curs, *curpos, &stream.avail_in);
7d0cdf82
CMN
746 stream.next_in = in;
747 st = inflate(&stream, Z_FINISH);
45d773ef 748 git_mwindow_close(w_curs);
7d0cdf82
CMN
749
750 if (!stream.avail_out)
751 break; /* the payload is larger than it should be */
752
45d773ef
CMN
753 if (st == Z_BUF_ERROR && in == NULL) {
754 inflateEnd(&stream);
755 git__free(buffer);
904b67e6 756 return GIT_EBUFS;
45d773ef
CMN
757 }
758
b5b474dd 759 *curpos += stream.next_in - in;
7d0cdf82
CMN
760 } while (st == Z_OK || st == Z_BUF_ERROR);
761
762 inflateEnd(&stream);
763
764 if ((st != Z_STREAM_END) || stream.total_out != size) {
3286c408 765 git__free(buffer);
e1de726c
RB
766 giterr_set(GITERR_ZLIB, "Failed to inflate packfile");
767 return -1;
7d0cdf82
CMN
768 }
769
770 obj->type = type;
771 obj->len = size;
772 obj->data = buffer;
e1de726c 773 return 0;
7d0cdf82
CMN
774}
775
b5b474dd
CMN
776/*
777 * curpos is where the data starts, delta_obj_offset is the where the
778 * header starts
779 */
e1de726c
RB
780git_off_t get_delta_base(
781 struct git_pack_file *p,
782 git_mwindow **w_curs,
783 git_off_t *curpos,
784 git_otype type,
785 git_off_t delta_obj_offset)
7d0cdf82 786{
45d773ef
CMN
787 unsigned int left = 0;
788 unsigned char *base_info;
e1de726c 789 git_off_t base_offset;
7d0cdf82
CMN
790 git_oid unused;
791
45d773ef
CMN
792 base_info = pack_window_open(p, w_curs, *curpos, &left);
793 /* Assumption: the only reason this would fail is because the file is too small */
794 if (base_info == NULL)
904b67e6 795 return GIT_EBUFS;
7d0cdf82
CMN
796 /* pack_window_open() assured us we have [base_info, base_info + 20)
797 * as a range that we can look at without walking off the
87d9869f
VM
798 * end of the mapped window. Its actually the hash size
799 * that is assured. An OFS_DELTA longer than the hash size
7d0cdf82
CMN
800 * is stupid, as then a REF_DELTA would be smaller to store.
801 */
802 if (type == GIT_OBJ_OFS_DELTA) {
803 unsigned used = 0;
804 unsigned char c = base_info[used++];
805 base_offset = c & 127;
806 while (c & 128) {
45d773ef 807 if (left <= used)
904b67e6 808 return GIT_EBUFS;
7d0cdf82
CMN
809 base_offset += 1;
810 if (!base_offset || MSB(base_offset, 7))
87d9869f 811 return 0; /* overflow */
7d0cdf82
CMN
812 c = base_info[used++];
813 base_offset = (base_offset << 7) + (c & 127);
814 }
815 base_offset = delta_obj_offset - base_offset;
816 if (base_offset <= 0 || base_offset >= delta_obj_offset)
87d9869f 817 return 0; /* out of bound */
7d0cdf82
CMN
818 *curpos += used;
819 } else if (type == GIT_OBJ_REF_DELTA) {
c1af5a39
CMN
820 /* If we have the cooperative cache, search in it first */
821 if (p->has_cache) {
0e040c03
CMN
822 khiter_t k;
823 git_oid oid;
c1af5a39 824
0e040c03
CMN
825 git_oid_fromraw(&oid, base_info);
826 k = kh_get(oid, p->idx_cache, &oid);
827 if (k != kh_end(p->idx_cache)) {
c1af5a39 828 *curpos += 20;
0e040c03 829 return ((struct git_pack_entry *)kh_value(p->idx_cache, k))->offset;
c1af5a39
CMN
830 }
831 }
7d0cdf82 832 /* The base entry _must_ be in the same pack */
e1de726c
RB
833 if (pack_entry_find_offset(&base_offset, &unused, p, (git_oid *)base_info, GIT_OID_HEXSZ) < 0)
834 return packfile_error("base entry delta is not in the same pack");
7d0cdf82
CMN
835 *curpos += 20;
836 } else
837 return 0;
838
839 return base_offset;
840}
a070f152
CMN
841
842/***********************************************************
843 *
844 * PACKFILE METHODS
845 *
846 ***********************************************************/
847
96c9b9f0 848void git_packfile_free(struct git_pack_file *p)
a070f152 849{
24c70804
RB
850 if (!p)
851 return;
852
c8f79c2b 853 cache_free(&p->bases);
c0f4a011 854
a070f152
CMN
855 git_mwindow_free_all(&p->mwf);
856
3a2d48d5 857 if (p->mwf.fd >= 0)
a070f152
CMN
858 p_close(p->mwf.fd);
859
860 pack_index_free(p);
861
3286c408 862 git__free(p->bad_object_sha1);
24c70804 863
24c70804 864 git_mutex_free(&p->lock);
3286c408 865 git__free(p);
a070f152
CMN
866}
867
868static int packfile_open(struct git_pack_file *p)
869{
870 struct stat st;
871 struct git_pack_header hdr;
872 git_oid sha1;
873 unsigned char *idx_sha1;
874
0ddfcb40 875 if (p->index_version == -1 && pack_index_open(p) < 0)
282283ac 876 return git_odb__error_notfound("failed to open packfile", NULL);
a070f152 877
9d2f841a
RB
878 /* if mwf opened by another thread, return now */
879 if (git_mutex_lock(&p->lock) < 0)
880 return packfile_error("failed to get lock for open");
881
882 if (p->mwf.fd >= 0) {
883 git_mutex_unlock(&p->lock);
884 return 0;
885 }
886
a070f152 887 /* TODO: open with noatime */
e1de726c 888 p->mwf.fd = git_futils_open_ro(p->pack_name);
9d2f841a
RB
889 if (p->mwf.fd < 0)
890 goto cleanup;
a070f152 891
e1de726c
RB
892 if (p_fstat(p->mwf.fd, &st) < 0 ||
893 git_mwindow_file_register(&p->mwf) < 0)
894 goto cleanup;
a070f152
CMN
895
896 /* If we created the struct before we had the pack we lack size. */
897 if (!p->mwf.size) {
898 if (!S_ISREG(st.st_mode))
899 goto cleanup;
e1de726c 900 p->mwf.size = (git_off_t)st.st_size;
a070f152
CMN
901 } else if (p->mwf.size != st.st_size)
902 goto cleanup;
903
904#if 0
905 /* We leave these file descriptors open with sliding mmap;
906 * there is no point keeping them open across exec(), though.
907 */
908 fd_flag = fcntl(p->mwf.fd, F_GETFD, 0);
909 if (fd_flag < 0)
e1de726c 910 goto cleanup;
a070f152
CMN
911
912 fd_flag |= FD_CLOEXEC;
913 if (fcntl(p->pack_fd, F_SETFD, fd_flag) == -1)
e1de726c 914 goto cleanup;
a070f152
CMN
915#endif
916
917 /* Verify we recognize this pack file format. */
e1de726c
RB
918 if (p_read(p->mwf.fd, &hdr, sizeof(hdr)) < 0 ||
919 hdr.hdr_signature != htonl(PACK_SIGNATURE) ||
920 !pack_version_ok(hdr.hdr_version))
a070f152
CMN
921 goto cleanup;
922
923 /* Verify the pack matches its index. */
e1de726c
RB
924 if (p->num_objects != ntohl(hdr.hdr_entries) ||
925 p_lseek(p->mwf.fd, p->mwf.size - GIT_OID_RAWSZ, SEEK_SET) == -1 ||
926 p_read(p->mwf.fd, sha1.id, GIT_OID_RAWSZ) < 0)
a070f152
CMN
927 goto cleanup;
928
929 idx_sha1 = ((unsigned char *)p->index_map.data) + p->index_map.len - 40;
930
9d2f841a
RB
931 if (git_oid__cmp(&sha1, (git_oid *)idx_sha1) != 0)
932 goto cleanup;
933
934 git_mutex_unlock(&p->lock);
935 return 0;
a070f152
CMN
936
937cleanup:
e1de726c 938 giterr_set(GITERR_OS, "Invalid packfile '%s'", p->pack_name);
9d2f841a 939
3a2d48d5
SS
940 if (p->mwf.fd >= 0)
941 p_close(p->mwf.fd);
a070f152 942 p->mwf.fd = -1;
9d2f841a
RB
943
944 git_mutex_unlock(&p->lock);
945
e1de726c 946 return -1;
a070f152
CMN
947}
948
5d2d21e5 949int git_packfile_alloc(struct git_pack_file **pack_out, const char *path)
a070f152
CMN
950{
951 struct stat st;
952 struct git_pack_file *p;
38eef611 953 size_t path_len = path ? strlen(path) : 0;
a070f152
CMN
954
955 *pack_out = NULL;
24c70804 956
38eef611 957 if (path_len < strlen(".idx"))
24c70804
RB
958 return git_odb__error_notfound("invalid packfile path", NULL);
959
38eef611 960 p = git__calloc(1, sizeof(*p) + path_len + 2);
e1de726c 961 GITERR_CHECK_ALLOC(p);
a070f152 962
38eef611
RB
963 memcpy(p->pack_name, path, path_len + 1);
964
a070f152
CMN
965 /*
966 * Make sure a corresponding .pack file exists and that
967 * the index looks sane.
968 */
38eef611
RB
969 if (git__suffixcmp(path, ".idx") == 0) {
970 size_t root_len = path_len - strlen(".idx");
971
972 memcpy(p->pack_name + root_len, ".keep", sizeof(".keep"));
973 if (git_path_exists(p->pack_name) == true)
974 p->pack_keep = 1;
a070f152 975
38eef611
RB
976 memcpy(p->pack_name + root_len, ".pack", sizeof(".pack"));
977 path_len = path_len - strlen(".idx") + strlen(".pack");
978 }
a070f152 979
e1de726c 980 if (p_stat(p->pack_name, &st) < 0 || !S_ISREG(st.st_mode)) {
3286c408 981 git__free(p);
282283ac 982 return git_odb__error_notfound("packfile not found", NULL);
a070f152
CMN
983 }
984
985 /* ok, it looks sane as far as we can check without
986 * actually mapping the pack file.
987 */
38eef611 988 p->mwf.fd = -1;
1af56d7d 989 p->mwf.size = st.st_size;
a070f152
CMN
990 p->pack_local = 1;
991 p->mtime = (git_time_t)st.st_mtime;
0ddfcb40 992 p->index_version = -1;
a070f152 993
1a42dd17
RB
994 if (git_mutex_init(&p->lock)) {
995 giterr_set(GITERR_OS, "Failed to initialize packfile mutex");
996 git__free(p);
997 return -1;
998 }
38eef611 999
a070f152
CMN
1000 /* see if we can parse the sha1 oid in the packfile name */
1001 if (path_len < 40 ||
e1de726c 1002 git_oid_fromstr(&p->sha1, path + path_len - GIT_OID_HEXSZ) < 0)
a070f152
CMN
1003 memset(&p->sha1, 0x0, GIT_OID_RAWSZ);
1004
1005 *pack_out = p;
e1de726c
RB
1006
1007 return 0;
a070f152
CMN
1008}
1009
1010/***********************************************************
1011 *
1012 * PACKFILE ENTRY SEARCH INTERNALS
1013 *
1014 ***********************************************************/
1015
e1de726c 1016static git_off_t nth_packed_object_offset(const struct git_pack_file *p, uint32_t n)
a070f152
CMN
1017{
1018 const unsigned char *index = p->index_map.data;
1019 index += 4 * 256;
1020 if (p->index_version == 1) {
1021 return ntohl(*((uint32_t *)(index + 24 * n)));
1022 } else {
1023 uint32_t off;
1024 index += 8 + p->num_objects * (20 + 4);
1025 off = ntohl(*((uint32_t *)(index + 4 * n)));
1026 if (!(off & 0x80000000))
1027 return off;
1028 index += p->num_objects * 4 + (off & 0x7fffffff) * 8;
1029 return (((uint64_t)ntohl(*((uint32_t *)(index + 0)))) << 32) |
87d9869f 1030 ntohl(*((uint32_t *)(index + 4)));
a070f152
CMN
1031 }
1032}
1033
60ecdf59
DMB
1034static int git__memcmp4(const void *a, const void *b) {
1035 return memcmp(a, b, 4);
1036}
1037
521aedad 1038int git_pack_foreach_entry(
5dca2010 1039 struct git_pack_file *p,
c3fb7d04 1040 git_odb_foreach_cb cb,
5dca2010 1041 void *data)
521aedad
CMN
1042{
1043 const unsigned char *index = p->index_map.data, *current;
521aedad 1044 uint32_t i;
dab89f9b 1045 int error = 0;
521aedad
CMN
1046
1047 if (index == NULL) {
521aedad
CMN
1048 if ((error = pack_index_open(p)) < 0)
1049 return error;
1050
1051 assert(p->index_map.data);
1052
1053 index = p->index_map.data;
1054 }
1055
1056 if (p->index_version > 1) {
1057 index += 8;
1058 }
1059
1060 index += 4 * 256;
1061
60ecdf59
DMB
1062 if (p->oids == NULL) {
1063 git_vector offsets, oids;
521aedad 1064
60ecdf59
DMB
1065 if ((error = git_vector_init(&oids, p->num_objects, NULL)))
1066 return error;
1067
1068 if ((error = git_vector_init(&offsets, p->num_objects, git__memcmp4)))
1069 return error;
5dca2010 1070
60ecdf59
DMB
1071 if (p->index_version > 1) {
1072 const unsigned char *off = index + 24 * p->num_objects;
1073 for (i = 0; i < p->num_objects; i++)
1074 git_vector_insert(&offsets, (void*)&off[4 * i]);
1075 git_vector_sort(&offsets);
1076 git_vector_foreach(&offsets, i, current)
1077 git_vector_insert(&oids, (void*)&index[5 * (current - off)]);
1078 } else {
1079 for (i = 0; i < p->num_objects; i++)
1080 git_vector_insert(&offsets, (void*)&index[24 * i]);
1081 git_vector_sort(&offsets);
1082 git_vector_foreach(&offsets, i, current)
1083 git_vector_insert(&oids, (void*)&current[4]);
1084 }
25e0b157 1085
60ecdf59 1086 git_vector_free(&offsets);
25e0b157 1087 p->oids = (git_oid **)git_vector_detach(NULL, NULL, &oids);
521aedad
CMN
1088 }
1089
60ecdf59 1090 for (i = 0; i < p->num_objects; i++)
c7b3e1b3
RB
1091 if ((error = cb(p->oids[i], data)) != 0) {
1092 GITERR_CALLBACK(error);
25e0b157 1093 break;
c7b3e1b3 1094 }
60ecdf59 1095
25e0b157 1096 return error;
521aedad
CMN
1097}
1098
a070f152 1099static int pack_entry_find_offset(
e1de726c
RB
1100 git_off_t *offset_out,
1101 git_oid *found_oid,
1102 struct git_pack_file *p,
1103 const git_oid *short_oid,
b8457baa 1104 size_t len)
a070f152 1105{
34bd5999
CMN
1106 const uint32_t *level1_ofs = p->index_map.data;
1107 const unsigned char *index = p->index_map.data;
a070f152
CMN
1108 unsigned hi, lo, stride;
1109 int pos, found = 0;
1110 const unsigned char *current = 0;
1111
1112 *offset_out = 0;
1113
0ddfcb40 1114 if (p->index_version == -1) {
34bd5999 1115 int error;
a070f152 1116
34bd5999
CMN
1117 if ((error = pack_index_open(p)) < 0)
1118 return error;
1119 assert(p->index_map.data);
8c535f3f 1120
34bd5999
CMN
1121 index = p->index_map.data;
1122 level1_ofs = p->index_map.data;
1123 }
a070f152
CMN
1124
1125 if (p->index_version > 1) {
1126 level1_ofs += 2;
1127 index += 8;
1128 }
1129
1130 index += 4 * 256;
1131 hi = ntohl(level1_ofs[(int)short_oid->id[0]]);
1132 lo = ((short_oid->id[0] == 0x0) ? 0 : ntohl(level1_ofs[(int)short_oid->id[0] - 1]));
1133
1134 if (p->index_version > 1) {
1135 stride = 20;
1136 } else {
1137 stride = 24;
1138 index += 4;
1139 }
1140
1141#ifdef INDEX_DEBUG_LOOKUP
1142 printf("%02x%02x%02x... lo %u hi %u nr %d\n",
1143 short_oid->id[0], short_oid->id[1], short_oid->id[2], lo, hi, p->num_objects);
1144#endif
1145
67591c8c 1146#ifdef GIT_USE_LOOKUP
87d9869f 1147 pos = sha1_entry_pos(index, stride, 0, lo, hi, p->num_objects, short_oid->id);
67591c8c
VM
1148#else
1149 pos = sha1_position(index, stride, lo, hi, short_oid->id);
1150#endif
a070f152
CMN
1151
1152 if (pos >= 0) {
1153 /* An object matching exactly the oid was found */
1154 found = 1;
1155 current = index + pos * stride;
1156 } else {
1157 /* No object was found */
1158 /* pos refers to the object with the "closest" oid to short_oid */
1159 pos = - 1 - pos;
1160 if (pos < (int)p->num_objects) {
1161 current = index + pos * stride;
1162
282283ac 1163 if (!git_oid_ncmp(short_oid, (const git_oid *)current, len))
a070f152 1164 found = 1;
a070f152
CMN
1165 }
1166 }
1167
b2a2702d 1168 if (found && len != GIT_OID_HEXSZ && pos + 1 < (int)p->num_objects) {
a070f152
CMN
1169 /* Check for ambiguousity */
1170 const unsigned char *next = current + stride;
1171
1172 if (!git_oid_ncmp(short_oid, (const git_oid *)next, len)) {
1173 found = 2;
1174 }
1175 }
1176
e1de726c 1177 if (!found)
282283ac 1178 return git_odb__error_notfound("failed to find offset for pack entry", short_oid);
e1de726c
RB
1179 if (found > 1)
1180 return git_odb__error_ambiguous("found multiple offsets for pack entry");
24c70804 1181
e1de726c
RB
1182 *offset_out = nth_packed_object_offset(p, pos);
1183 git_oid_fromraw(found_oid, current);
a070f152
CMN
1184
1185#ifdef INDEX_DEBUG_LOOKUP
e1de726c 1186 {
a070f152
CMN
1187 unsigned char hex_sha1[GIT_OID_HEXSZ + 1];
1188 git_oid_fmt(hex_sha1, found_oid);
1189 hex_sha1[GIT_OID_HEXSZ] = '\0';
1190 printf("found lo=%d %s\n", lo, hex_sha1);
a070f152 1191 }
e1de726c 1192#endif
24c70804 1193
e1de726c 1194 return 0;
a070f152
CMN
1195}
1196
1197int git_pack_entry_find(
1198 struct git_pack_entry *e,
1199 struct git_pack_file *p,
1200 const git_oid *short_oid,
b8457baa 1201 size_t len)
a070f152 1202{
e1de726c 1203 git_off_t offset;
a070f152
CMN
1204 git_oid found_oid;
1205 int error;
1206
1207 assert(p);
1208
1209 if (len == GIT_OID_HEXSZ && p->num_bad_objects) {
1210 unsigned i;
1211 for (i = 0; i < p->num_bad_objects; i++)
b7f167da 1212 if (git_oid__cmp(short_oid, &p->bad_object_sha1[i]) == 0)
e1de726c 1213 return packfile_error("bad object found in packfile");
a070f152
CMN
1214 }
1215
1216 error = pack_entry_find_offset(&offset, &found_oid, p, short_oid, len);
e1de726c
RB
1217 if (error < 0)
1218 return error;
a070f152
CMN
1219
1220 /* we found a unique entry in the index;
1221 * make sure the packfile backing the index
1222 * still exists on disk */
e1de726c
RB
1223 if (p->mwf.fd == -1 && (error = packfile_open(p)) < 0)
1224 return error;
a070f152
CMN
1225
1226 e->offset = offset;
1227 e->p = p;
1228
1229 git_oid_cpy(&e->sha1, &found_oid);
e1de726c 1230 return 0;
a070f152 1231}