read pack files with mmap
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679
diff --git a/lib/got_lib_pack.h b/lib/got_lib_pack.h
index ed55c45..ac420c8 100644
--- a/lib/got_lib_pack.h
+++ b/lib/got_lib_pack.h
@@ -18,10 +18,11 @@
struct got_pack {
char *path_packfile;
int fd;
+ uint8_t *map;
size_t filesize;
};
-void got_pack_close(struct got_pack *);
+const struct got_error *got_pack_close(struct got_pack *);
/* See Documentation/technical/pack-format.txt in Git. */
diff --git a/lib/got_lib_zbuf.h b/lib/got_lib_zbuf.h
index 55eb6bb..45fce72 100644
--- a/lib/got_lib_zbuf.h
+++ b/lib/got_lib_zbuf.h
@@ -33,9 +33,15 @@ const struct got_error *got_inflate_read(struct got_zstream_buf *, FILE *,
size_t *);
const struct got_error *got_inflate_read_fd(struct got_zstream_buf *, int,
size_t *);
+const struct got_error *got_inflate_read_mmap(struct got_zstream_buf *,
+ uint8_t *, size_t, size_t, size_t *, size_t *);
void got_inflate_end(struct got_zstream_buf *);
const struct got_error *got_inflate_to_mem(uint8_t **, size_t *, FILE *);
const struct got_error *got_inflate_to_mem_fd(uint8_t **, size_t *, int);
+const struct got_error *got_inflate_to_mem_mmap(uint8_t **, size_t *, uint8_t *,
+ size_t, size_t);
const struct got_error *got_inflate_to_file(size_t *, FILE *, FILE *);
const struct got_error *got_inflate_to_file_fd(size_t *, int, FILE *);
const struct got_error *got_inflate_to_fd(size_t *, FILE *, int);
+const struct got_error *got_inflate_to_file_mmap(size_t *, uint8_t *, size_t,
+ size_t, FILE *);
diff --git a/lib/pack.c b/lib/pack.c
index 2636e0c..2c8c7be 100644
--- a/lib/pack.c
+++ b/lib/pack.c
@@ -482,7 +482,7 @@ get_packfile_path(char **path_packfile, struct got_repository *repo,
return NULL;
}
-const struct got_error *
+static const struct got_error *
read_packfile_hdr(int fd, struct got_packidx *packidx)
{
const struct got_error *err = NULL;
@@ -524,14 +524,20 @@ open_packfile(int *fd, const char *path_packfile,
return err;
}
-void
+const struct got_error *
got_pack_close(struct got_pack *pack)
{
+ const struct got_error *err = NULL;
+
+ if (munmap(pack->map, pack->filesize) == -1)
+ err = got_error_from_errno();
close(pack->fd);
pack->fd = -1;
free(pack->path_packfile);
pack->path_packfile = NULL;
pack->filesize = 0;
+
+ return err;
}
static const struct got_error *
@@ -554,7 +560,9 @@ cache_pack(struct got_pack **packp, const char *path_packfile,
}
if (i == nitems(repo->packs) - 1) {
- got_pack_close(&repo->packs[i - 1]);
+ err = got_pack_close(&repo->packs[i - 1]);
+ if (err)
+ return err;
memmove(&repo->packs[1], &repo->packs[0],
sizeof(repo->packs) - sizeof(repo->packs[0]));
i = 0;
@@ -573,6 +581,15 @@ cache_pack(struct got_pack **packp, const char *path_packfile,
goto done;
err = get_packfile_size(&pack->filesize, path_packfile);
+ if (err)
+ goto done;
+
+ pack->map = mmap(NULL, pack->filesize, PROT_READ, MAP_PRIVATE,
+ pack->fd, 0);
+ if (pack->map == MAP_FAILED) {
+ err = got_error_from_errno();
+ pack->map = NULL;
+ }
done:
if (err) {
if (pack) {
@@ -602,24 +619,43 @@ get_cached_pack(const char *path_packfile, struct got_repository *repo)
}
static const struct got_error *
-parse_object_type_and_size(uint8_t *type, uint64_t *size, size_t *len, int fd)
+parse_object_type_and_size(uint8_t *type, uint64_t *size, size_t *len,
+ struct got_pack *pack, off_t offset)
{
uint8_t t = 0;
uint64_t s = 0;
uint8_t sizeN;
- ssize_t n;
+ size_t mapoff = 0;
int i = 0;
+ *len = 0;
+
+ if (offset >= pack->filesize)
+ return got_error(GOT_ERR_PACK_OFFSET);
+
+ if (pack->map) {
+ mapoff = (size_t)offset;
+ } else {
+ if (lseek(pack->fd, offset, SEEK_SET) == -1)
+ return got_error_from_errno();
+ }
+
do {
/* We do not support size values which don't fit in 64 bit. */
if (i > 9)
return got_error(GOT_ERR_NO_SPACE);
- n = read(fd, &sizeN, sizeof(sizeN));
- if (n < 0)
- return got_error_from_errno();
- if (n != sizeof(sizeN))
- return got_error(GOT_ERR_BAD_PACKFILE);
+ if (pack->map) {
+ sizeN = *(pack->map + mapoff);
+ mapoff += sizeof(sizeN);
+ } else {
+ ssize_t n = read(pack->fd, &sizeN, sizeof(sizeN));
+ if (n < 0)
+ return got_error_from_errno();
+ if (n != sizeof(sizeN))
+ return got_error(GOT_ERR_BAD_PACKFILE);
+ }
+ *len += sizeof(sizeN);
if (i == 0) {
t = (sizeN & GOT_PACK_OBJ_SIZE0_TYPE_MASK) >>
@@ -634,7 +670,6 @@ parse_object_type_and_size(uint8_t *type, uint64_t *size, size_t *len, int fd)
*type = t;
*size = s;
- *len = i * sizeof(sizeN);
return NULL;
}
@@ -665,23 +700,35 @@ open_plain_object(struct got_object **obj, const char *path_packfile,
}
static const struct got_error *
-parse_negative_offset(int64_t *offset, size_t *len, int fd)
+parse_negative_offset(int64_t *offset, size_t *len, struct got_pack *pack,
+ off_t delta_offset)
{
int64_t o = 0;
uint8_t offN;
- ssize_t n;
int i = 0;
+ *len = 0;
+
do {
/* We do not support offset values which don't fit in 64 bit. */
if (i > 8)
return got_error(GOT_ERR_NO_SPACE);
- n = read(fd, &offN, sizeof(offN));
- if (n < 0)
- return got_error_from_errno();
- if (n != sizeof(offN))
- return got_error(GOT_ERR_BAD_PACKFILE);
+ if (pack->map) {
+ size_t mapoff;
+ if (delta_offset >= pack->filesize)
+ return got_error(GOT_ERR_PACK_OFFSET);
+ mapoff = (size_t)delta_offset + *len;
+ offN = *(pack->map + mapoff);
+ } else {
+ ssize_t n;
+ n = read(pack->fd, &offN, sizeof(offN));
+ if (n < 0)
+ return got_error_from_errno();
+ if (n != sizeof(offN))
+ return got_error(GOT_ERR_BAD_PACKFILE);
+ }
+ *len += sizeof(offN);
if (i == 0)
o = (offN & GOT_PACK_OBJ_DELTA_OFF_VAL_MASK);
@@ -694,18 +741,21 @@ parse_negative_offset(int64_t *offset, size_t *len, int fd)
} while (offN & GOT_PACK_OBJ_DELTA_OFF_MORE);
*offset = o;
- *len = i * sizeof(offN);
return NULL;
}
static const struct got_error *
-parse_offset_delta(off_t *base_offset, int fd, off_t offset)
+parse_offset_delta(off_t *base_offset, size_t *len, struct got_pack *pack,
+ off_t offset, int tslen)
{
const struct got_error *err;
int64_t negoffset;
size_t negofflen;
- err = parse_negative_offset(&negoffset, &negofflen, fd);
+ *len = 0;
+
+ err = parse_negative_offset(&negoffset, &negofflen, pack,
+ offset + tslen);
if (err)
return err;
@@ -714,6 +764,7 @@ parse_offset_delta(off_t *base_offset, int fd, off_t offset)
if (*base_offset <= 0)
return got_error(GOT_ERR_BAD_PACKFILE);
+ *len = negofflen;
return NULL;
}
@@ -754,19 +805,35 @@ resolve_offset_delta(struct got_delta_chain *deltas,
size_t base_tslen;
off_t delta_data_offset;
uint8_t *delta_buf;
- size_t delta_len;
+ size_t delta_len, consumed;
- err = parse_offset_delta(&base_offset, pack->fd, delta_offset);
+ err = parse_offset_delta(&base_offset, &consumed, pack,
+ delta_offset, tslen);
if (err)
return err;
- delta_data_offset = lseek(pack->fd, 0, SEEK_CUR);
- if (delta_data_offset == -1)
- return got_error_from_errno();
+ delta_data_offset = delta_offset + tslen + consumed;
+ if (delta_data_offset >= pack->filesize)
+ return got_error(GOT_ERR_PACK_OFFSET);
- err = got_inflate_to_mem_fd(&delta_buf, &delta_len, pack->fd);
- if (err)
- return err;
+ if (pack->map == NULL) {
+ delta_data_offset = lseek(pack->fd, 0, SEEK_CUR);
+ if (delta_data_offset == -1)
+ return got_error_from_errno();
+ }
+
+ if (pack->map) {
+ size_t mapoff = (size_t)delta_data_offset;
+ err = got_inflate_to_mem_mmap(&delta_buf, &delta_len, pack->map,
+ mapoff, pack->filesize - mapoff);
+ if (err)
+ return err;
+ } else {
+
+ err = got_inflate_to_mem_fd(&delta_buf, &delta_len, pack->fd);
+ if (err)
+ return err;
+ }
err = add_delta(deltas, pack->path_packfile, delta_offset, tslen,
delta_type, delta_size, delta_data_offset, delta_buf, delta_len);
@@ -776,11 +843,9 @@ resolve_offset_delta(struct got_delta_chain *deltas,
/* An offset delta must be in the same packfile. */
if (base_offset >= pack->filesize)
return got_error(GOT_ERR_PACK_OFFSET);
- if (lseek(pack->fd, base_offset, SEEK_SET) == -1)
- return got_error_from_errno();
err = parse_object_type_and_size(&base_type, &base_size, &base_tslen,
- pack->fd);
+ pack, base_offset);
if (err)
return err;
@@ -801,24 +866,41 @@ resolve_ref_delta(struct got_delta_chain *deltas, struct got_repository *repo,
uint8_t base_type;
uint64_t base_size;
size_t base_tslen;
- ssize_t n;
off_t delta_data_offset;
uint8_t *delta_buf;
size_t delta_len;
- n = read(pack->fd, &id, sizeof(id));
- if (n < 0)
- return got_error_from_errno();
- if (n != sizeof(id))
- return got_error(GOT_ERR_BAD_PACKFILE);
+ if (delta_offset >= pack->filesize)
+ return got_error(GOT_ERR_PACK_OFFSET);
+ delta_data_offset = delta_offset + tslen + sizeof(id);
+ if (delta_data_offset >= pack->filesize)
+ return got_error(GOT_ERR_PACK_OFFSET);
- delta_data_offset = lseek(pack->fd, 0, SEEK_CUR);
- if (delta_data_offset == -1)
- return got_error_from_errno();
+ if (pack->map == NULL) {
+ delta_data_offset = lseek(pack->fd, 0, SEEK_CUR);
+ if (delta_data_offset == -1)
+ return got_error_from_errno();
+ }
- err = got_inflate_to_mem_fd(&delta_buf, &delta_len, pack->fd);
- if (err)
- return err;
+
+ if (pack->map) {
+ size_t mapoff = (size_t)delta_offset;
+ memcpy(&id, pack->map + mapoff, sizeof(id));
+ mapoff += sizeof(id);
+ err = got_inflate_to_mem_mmap(&delta_buf, &delta_len, pack->map,
+ mapoff, pack->filesize - delta_data_offset);
+ if (err)
+ return err;
+ } else {
+ ssize_t n = read(pack->fd, &id, sizeof(id));
+ if (n < 0)
+ return got_error_from_errno();
+ if (n != sizeof(id))
+ return got_error(GOT_ERR_BAD_PACKFILE);
+ err = got_inflate_to_mem_fd(&delta_buf, &delta_len, pack->fd);
+ if (err)
+ return err;
+ }
err = add_delta(deltas, pack->path_packfile, delta_offset, tslen,
delta_type, delta_size, delta_data_offset, delta_buf, delta_len);
@@ -839,13 +921,9 @@ resolve_ref_delta(struct got_delta_chain *deltas, struct got_repository *repo,
err = got_error(GOT_ERR_PACK_OFFSET);
goto done;
}
- if (lseek(pack->fd, base_offset, SEEK_SET) == -1) {
- err = got_error_from_errno();
- goto done;
- }
err = parse_object_type_and_size(&base_type, &base_size, &base_tslen,
- pack->fd);
+ pack, base_offset);
if (err)
goto done;
@@ -965,16 +1043,7 @@ open_packed_object(struct got_object **obj, struct got_repository *repo,
goto done;
}
- if (offset >= pack->filesize) {
- err = got_error(GOT_ERR_PACK_OFFSET);
- goto done;
- }
- if (lseek(pack->fd, offset, SEEK_SET) == -1) {
- err = got_error_from_errno();
- goto done;
- }
-
- err = parse_object_type_and_size(&type, &size, &tslen, pack->fd);
+ err = parse_object_type_and_size(&type, &size, &tslen, pack, offset);
if (err)
goto done;
@@ -1093,7 +1162,7 @@ dump_delta_chain_to_file(size_t *result_size, struct got_delta_chain *deltas,
SIMPLEQ_FOREACH(delta, &deltas->entries, entry) {
if (n == 0) {
struct got_pack *pack;
- size_t base_len;
+ size_t base_len, mapoff;
off_t delta_data_offset;
/* Plain object types are the delta base. */
@@ -1116,17 +1185,31 @@ dump_delta_chain_to_file(size_t *result_size, struct got_delta_chain *deltas,
err = got_error(GOT_ERR_PACK_OFFSET);
goto done;
}
- if (lseek(pack->fd, delta_data_offset, SEEK_SET)
- == -1) {
- err = got_error_from_errno();
- goto done;
+ if (pack->map == NULL) {
+ if (lseek(pack->fd, delta_data_offset, SEEK_SET)
+ == -1) {
+ err = got_error_from_errno();
+ goto done;
+ }
}
- if (base_file)
- err = got_inflate_to_file_fd(&base_len,
- pack->fd, base_file);
- else {
- err = got_inflate_to_mem_fd(&base_buf,
- &base_len, pack->fd);
+ if (base_file) {
+ if (pack->map) {
+ mapoff = (size_t)delta_data_offset;
+ err = got_inflate_to_file_mmap(
+ &base_len, pack->map, mapoff,
+ pack->filesize - mapoff, base_file);
+ } else
+ err = got_inflate_to_file_fd(&base_len,
+ pack->fd, base_file);
+ } else {
+ if (pack->map) {
+ mapoff = (size_t)delta_data_offset;
+ err = got_inflate_to_mem_mmap(&base_buf,
+ &base_len, pack->map, mapoff,
+ pack->filesize - mapoff);
+ } else
+ err = got_inflate_to_mem_fd(&base_buf,
+ &base_len, pack->fd);
if (base_len < max_size) {
uint8_t *p;
p = reallocarray(base_buf, 1, max_size);
@@ -1244,13 +1327,22 @@ dump_delta_chain_to_mem(uint8_t **outbuf, size_t *outlen,
err = got_error(GOT_ERR_PACK_OFFSET);
goto done;
}
- if (lseek(pack->fd, delta_data_offset, SEEK_SET)
- == -1) {
- err = got_error_from_errno();
- goto done;
+ if (pack->map) {
+ size_t mapoff = (size_t)delta_data_offset;
+ err = got_inflate_to_mem_mmap(&base_buf,
+ &base_len, pack->map, mapoff,
+ pack->filesize - mapoff);
+ } else {
+ if (lseek(pack->fd, delta_data_offset, SEEK_SET)
+ == -1) {
+ err = got_error_from_errno();
+ goto done;
+ }
+ err = got_inflate_to_mem_fd(&base_buf,
+ &base_len, pack->fd);
}
- err = got_inflate_to_mem_fd(&base_buf, &base_len,
- pack->fd);
+ if (err)
+ goto done;
if (base_len < max_size) {
uint8_t *p;
p = reallocarray(base_buf, 1, max_size);
@@ -1260,8 +1352,6 @@ dump_delta_chain_to_mem(uint8_t **outbuf, size_t *outlen,
}
base_buf = p;
}
- if (err)
- goto done;
n++;
continue;
}
@@ -1324,12 +1414,18 @@ got_packfile_extract_object(FILE **f, struct got_object *obj,
err = got_error(GOT_ERR_PACK_OFFSET);
goto done;
}
- if (lseek(pack->fd, obj->pack_offset, SEEK_SET) == -1) {
- err = got_error_from_errno();
- goto done;
- }
- err = got_inflate_to_file_fd(&obj->size, pack->fd, *f);
+ if (pack->map) {
+ size_t mapoff = (size_t)obj->pack_offset;
+ err = got_inflate_to_file_mmap(&obj->size, pack->map,
+ mapoff, pack->filesize - mapoff, *f);
+ } else {
+ if (lseek(pack->fd, obj->pack_offset, SEEK_SET) == -1) {
+ err = got_error_from_errno();
+ goto done;
+ }
+ err = got_inflate_to_file_fd(&obj->size, pack->fd, *f);
+ }
} else
err = dump_delta_chain_to_file(&obj->size,
&obj->deltas, *f, repo);
@@ -1364,12 +1460,17 @@ got_packfile_extract_object_to_mem(uint8_t **buf, size_t *len,
err = got_error(GOT_ERR_PACK_OFFSET);
goto done;
}
- if (lseek(pack->fd, obj->pack_offset, SEEK_SET) == -1) {
- err = got_error_from_errno();
- goto done;
+ if (pack->map) {
+ size_t mapoff = (size_t)obj->pack_offset;
+ err = got_inflate_to_mem_mmap(buf, len, pack->map,
+ mapoff, pack->filesize - mapoff);
+ } else {
+ if (lseek(pack->fd, obj->pack_offset, SEEK_SET) == -1) {
+ err = got_error_from_errno();
+ goto done;
+ }
+ err = got_inflate_to_mem_fd(buf, len, pack->fd);
}
-
- err = got_inflate_to_mem_fd(buf, len, pack->fd);
} else
err = dump_delta_chain_to_mem(buf, len, &obj->deltas, repo);
done:
diff --git a/lib/zbuf.c b/lib/zbuf.c
index cd8dc7d..85e1798 100644
--- a/lib/zbuf.c
+++ b/lib/zbuf.c
@@ -29,6 +29,10 @@
#include "got_lib_path.h"
#include "got_lib_zbuf.h"
+#ifndef MIN
+#define MIN(_a,_b) ((_a) < (_b) ? (_a) : (_b))
+#endif
+
const struct got_error *
got_inflate_init(struct got_zstream_buf *zb, uint8_t *outbuf, size_t bufsize)
{
@@ -145,6 +149,48 @@ got_inflate_read_fd(struct got_zstream_buf *zb, int fd, size_t *outlenp)
return NULL;
}
+const struct got_error *
+got_inflate_read_mmap(struct got_zstream_buf *zb, uint8_t *map, size_t offset,
+ size_t len, size_t *outlenp, size_t *consumed)
+{
+ size_t last_total_out = zb->z.total_out;
+ z_stream *z = &zb->z;
+ int ret = Z_ERRNO;
+
+ z->next_out = zb->outbuf;
+ z->avail_out = zb->outlen;
+
+ *outlenp = 0;
+ *consumed = 0;
+
+ do {
+ if (z->avail_in == 0) {
+ if (len == 0) {
+ /* EOF */
+ ret = Z_STREAM_END;
+ break;
+ }
+ z->next_in = map + offset;
+ z->avail_in = MIN(zb->inlen, len);
+ *consumed += z->avail_in;
+ offset += z->avail_in;
+ len -= z->avail_in;
+ }
+ ret = inflate(z, Z_SYNC_FLUSH);
+ } while (ret == Z_OK && z->avail_out > 0);
+
+ if (ret == Z_OK) {
+ zb->flags |= GOT_ZSTREAM_F_HAVE_MORE;
+ } else {
+ if (ret != Z_STREAM_END)
+ return got_error(GOT_ERR_DECOMPRESSION);
+ zb->flags &= ~GOT_ZSTREAM_F_HAVE_MORE;
+ }
+
+ *outlenp = z->total_out - last_total_out;
+ return NULL;
+}
+
void
got_inflate_end(struct got_zstream_buf *zb)
{
@@ -241,6 +287,56 @@ done:
}
const struct got_error *
+got_inflate_to_mem_mmap(uint8_t **outbuf, size_t *outlen, uint8_t *map,
+ size_t offset, size_t len)
+{
+ const struct got_error *err;
+ size_t avail;
+ struct got_zstream_buf zb;
+ void *newbuf;
+ size_t consumed;
+
+ *outbuf = calloc(1, GOT_ZSTREAM_BUFSIZE);
+ if (*outbuf == NULL)
+ return got_error_from_errno();
+ err = got_inflate_init(&zb, *outbuf, GOT_ZSTREAM_BUFSIZE);
+ if (err)
+ return err;
+
+ *outlen = 0;
+
+ do {
+ err = got_inflate_read_mmap(&zb, map, offset, len, &avail,
+ &consumed);
+ if (err)
+ return err;
+ offset += consumed;
+ len -= consumed;
+ *outlen += avail;
+ if (len == 0)
+ break;
+ if (zb.flags & GOT_ZSTREAM_F_HAVE_MORE) {
+ newbuf = reallocarray(*outbuf, 1,
+ *outlen + GOT_ZSTREAM_BUFSIZE);
+ if (newbuf == NULL) {
+ err = got_error_from_errno();
+ free(*outbuf);
+ *outbuf = NULL;
+ *outlen = 0;
+ goto done;
+ }
+ *outbuf = newbuf;
+ zb.outbuf = newbuf + *outlen;
+ zb.outlen = GOT_ZSTREAM_BUFSIZE;
+ }
+ } while (zb.flags & GOT_ZSTREAM_F_HAVE_MORE);
+
+done:
+ got_inflate_end(&zb);
+ return err;
+}
+
+const struct got_error *
got_inflate_to_fd(size_t *outlen, FILE *infile, int outfd)
{
const struct got_error *err = NULL;
@@ -346,3 +442,43 @@ done:
got_inflate_end(&zb);
return err;
}
+
+const struct got_error *
+got_inflate_to_file_mmap(size_t *outlen, uint8_t *map, size_t offset,
+ size_t len, FILE *outfile)
+{
+ const struct got_error *err;
+ size_t avail;
+ struct got_zstream_buf zb;
+ size_t consumed;
+
+ err = got_inflate_init(&zb, NULL, GOT_ZSTREAM_BUFSIZE);
+ if (err)
+ goto done;
+
+ *outlen = 0;
+
+ do {
+ err = got_inflate_read_mmap(&zb, map, offset, len, &avail,
+ &consumed);
+ if (err)
+ return err;
+ offset += consumed;
+ len -= consumed;
+ if (avail > 0) {
+ size_t n;
+ n = fwrite(zb.outbuf, avail, 1, outfile);
+ if (n != 1) {
+ err = got_ferror(outfile, GOT_ERR_IO);
+ goto done;
+ }
+ *outlen += avail;
+ }
+ } while (zb.flags & GOT_ZSTREAM_F_HAVE_MORE);
+
+done:
+ if (err == NULL)
+ rewind(outfile);
+ got_inflate_end(&zb);
+ return err;
+}