store deltas in compressed form while packing, both in memory and cache file This reduces memory and disk space consumption during packing. with tweaks + memleak on error fix from op@ ok op@
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 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999
diff --git a/lib/deflate.c b/lib/deflate.c
index 3c97a77..d4180f6 100644
--- a/lib/deflate.c
+++ b/lib/deflate.c
@@ -136,9 +136,9 @@ got_deflate_read(struct got_deflate_buf *zb, FILE *f, off_t len,
return NULL;
}
-const struct got_error *
-got_deflate_read_mmap(struct got_deflate_buf *zb, uint8_t *map, size_t offset,
- size_t len, size_t *outlenp, size_t *consumed)
+static const struct got_error *
+deflate_read_mmap(struct got_deflate_buf *zb, uint8_t *map, size_t offset,
+ size_t len, size_t *outlenp, size_t *consumed, int flush_on_eof)
{
z_stream *z = &zb->z;
size_t last_total_out = z->total_out;
@@ -159,7 +159,8 @@ got_deflate_read_mmap(struct got_deflate_buf *zb, uint8_t *map, size_t offset,
z->avail_in = len - *consumed;
if (z->avail_in == 0) {
/* EOF */
- ret = deflate(z, Z_FINISH);
+ if (flush_on_eof)
+ ret = deflate(z, Z_FINISH);
break;
}
}
@@ -179,6 +180,51 @@ got_deflate_read_mmap(struct got_deflate_buf *zb, uint8_t *map, size_t offset,
return NULL;
}
+const struct got_error *
+got_deflate_read_mmap(struct got_deflate_buf *zb, uint8_t *map, size_t offset,
+ size_t len, size_t *outlenp, size_t *consumed)
+{
+ return deflate_read_mmap(zb, map, offset, len, outlenp, consumed, 1);
+}
+
+const struct got_error *
+got_deflate_flush(struct got_deflate_buf *zb, FILE *outfile,
+ struct got_deflate_checksum *csum, off_t *outlenp)
+{
+ int ret;
+ size_t n;
+ z_stream *z = &zb->z;
+
+ if (z->avail_in != 0)
+ return got_error_msg(GOT_ERR_COMPRESSION,
+ "cannot flush zb with pending input data");
+
+ do {
+ size_t avail, last_total_out = zb->z.total_out;
+
+ z->next_out = zb->outbuf;
+ z->avail_out = zb->outlen;
+
+ ret = deflate(z, Z_FINISH);
+ if (ret != Z_STREAM_END && ret != Z_OK)
+ return got_error(GOT_ERR_COMPRESSION);
+
+ avail = z->total_out - last_total_out;
+ if (avail > 0) {
+ n = fwrite(zb->outbuf, avail, 1, outfile);
+ if (n != 1)
+ return got_ferror(outfile, GOT_ERR_IO);
+ if (csum)
+ csum_output(csum, zb->outbuf, avail);
+ if (outlenp)
+ *outlenp += avail;
+ }
+ } while (ret != Z_STREAM_END);
+
+ zb->flags &= ~GOT_DEFLATE_F_HAVE_MORE;
+ return NULL;
+}
+
void
got_deflate_end(struct got_deflate_buf *zb)
{
@@ -263,3 +309,97 @@ done:
got_deflate_end(&zb);
return err;
}
+
+const struct got_error *
+got_deflate_append_to_file_mmap(struct got_deflate_buf *zb, off_t *outlen,
+ uint8_t *map, size_t offset, size_t len, FILE *outfile,
+ struct got_deflate_checksum *csum)
+{
+ const struct got_error *err;
+ size_t avail, consumed;
+
+ do {
+ err = deflate_read_mmap(zb, map, offset, len, &avail,
+ &consumed, 0);
+ if (err)
+ break;
+ 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);
+ break;
+ }
+ if (csum)
+ csum_output(csum, zb->outbuf, avail);
+ if (outlen)
+ *outlen += avail;
+ }
+ } while ((zb->flags & GOT_DEFLATE_F_HAVE_MORE) && len > 0);
+
+ return err;
+}
+
+const struct got_error *
+got_deflate_to_mem_mmap(uint8_t **outbuf, size_t *outlen,
+ size_t *consumed_total, struct got_deflate_checksum *csum, uint8_t *map,
+ size_t offset, size_t len)
+{
+ const struct got_error *err;
+ size_t avail, consumed;
+ struct got_deflate_buf zb;
+ void *newbuf;
+ size_t nbuf = 1;
+
+ if (outbuf) {
+ *outbuf = malloc(GOT_DEFLATE_BUFSIZE);
+ if (*outbuf == NULL)
+ return got_error_from_errno("malloc");
+ err = got_deflate_init(&zb, *outbuf, GOT_DEFLATE_BUFSIZE);
+ if (err) {
+ free(*outbuf);
+ *outbuf = NULL;
+ return err;
+ }
+ } else {
+ err = got_deflate_init(&zb, NULL, GOT_DEFLATE_BUFSIZE);
+ if (err)
+ return err;
+ }
+
+ *outlen = 0;
+ if (consumed_total)
+ *consumed_total = 0;
+ do {
+ err = got_deflate_read_mmap(&zb, map, offset, len, &avail,
+ &consumed);
+ if (err)
+ goto done;
+ offset += consumed;
+ if (consumed_total)
+ *consumed_total += consumed;
+ len -= consumed;
+ if (avail > 0 && csum)
+ csum_output(csum, zb.outbuf, avail);
+ *outlen += avail;
+ if ((zb.flags & GOT_DEFLATE_F_HAVE_MORE) && outbuf != NULL) {
+ newbuf = reallocarray(*outbuf, ++nbuf,
+ GOT_DEFLATE_BUFSIZE);
+ if (newbuf == NULL) {
+ err = got_error_from_errno("reallocarray");
+ free(*outbuf);
+ *outbuf = NULL;
+ *outlen = 0;
+ goto done;
+ }
+ *outbuf = newbuf;
+ zb.outbuf = newbuf + *outlen;
+ zb.outlen = (nbuf * GOT_DEFLATE_BUFSIZE) - *outlen;
+ }
+ } while (zb.flags & GOT_DEFLATE_F_HAVE_MORE);
+done:
+ got_deflate_end(&zb);
+ return err;
+}
diff --git a/lib/got_lib_deflate.h b/lib/got_lib_deflate.h
index 1c429af..09a8755 100644
--- a/lib/got_lib_deflate.h
+++ b/lib/got_lib_deflate.h
@@ -39,8 +39,17 @@ const struct got_error *got_deflate_init(struct got_deflate_buf *, uint8_t *,
size_t);
const struct got_error *got_deflate_read(struct got_deflate_buf *, FILE *,
off_t, size_t *, off_t *);
+const struct got_error *got_deflate_read_mmap(struct got_deflate_buf *,
+ uint8_t *, size_t, size_t, size_t *, size_t *);
void got_deflate_end(struct got_deflate_buf *);
const struct got_error *got_deflate_to_file(off_t *, FILE *, off_t, FILE *,
struct got_deflate_checksum *);
const struct got_error *got_deflate_to_file_mmap(off_t *, uint8_t *,
size_t, size_t, FILE *, struct got_deflate_checksum *);
+const struct got_error *got_deflate_flush(struct got_deflate_buf *, FILE *,
+ struct got_deflate_checksum *, off_t *);
+const struct got_error *got_deflate_append_to_file_mmap(
+ struct got_deflate_buf *, off_t *, uint8_t *, size_t, size_t, FILE *,
+ struct got_deflate_checksum *);
+const struct got_error *got_deflate_to_mem_mmap(uint8_t **, size_t *, size_t *,
+ struct got_deflate_checksum *, uint8_t *, size_t, size_t);
diff --git a/lib/got_lib_object.h b/lib/got_lib_object.h
index 6af8d57..4bbe44d 100644
--- a/lib/got_lib_object.h
+++ b/lib/got_lib_object.h
@@ -104,7 +104,7 @@ const struct got_error *got_object_open_from_packfile(struct got_object **,
struct got_object_id *, struct got_pack *, struct got_packidx *, int,
struct got_repository *);
const struct got_error *got_object_read_raw_delta(uint64_t *, uint64_t *,
- off_t *, off_t *, off_t *, struct got_object_id **, int,
+ off_t *, off_t *, off_t *, off_t *, struct got_object_id **, int,
struct got_packidx *, int, struct got_object_id *, struct got_repository *);
const struct got_error *got_object_read_header_privsep(struct got_object **,
struct got_object_id *, struct got_repository *, int);
diff --git a/lib/got_lib_pack.h b/lib/got_lib_pack.h
index e8fb373..6a3d398 100644
--- a/lib/got_lib_pack.h
+++ b/lib/got_lib_pack.h
@@ -212,7 +212,7 @@ const struct got_error *got_packfile_extract_object(struct got_pack *,
const struct got_error *got_packfile_extract_object_to_mem(uint8_t **, size_t *,
struct got_object *, struct got_pack *);
const struct got_error *got_packfile_extract_raw_delta(uint8_t **, size_t *,
- off_t *, off_t *, struct got_object_id *, uint64_t *, uint64_t *,
+ size_t *, off_t *, off_t *, struct got_object_id *, uint64_t *, uint64_t *,
struct got_pack *, struct got_packidx *, int);
struct got_pack *got_repo_get_cached_pack(struct got_repository *,
const char *);
diff --git a/lib/got_lib_privsep.h b/lib/got_lib_privsep.h
index 110fe04..e57f4dd 100644
--- a/lib/got_lib_privsep.h
+++ b/lib/got_lib_privsep.h
@@ -284,6 +284,7 @@ struct got_imsg_raw_delta {
uint64_t base_size;
uint64_t result_size;
off_t delta_size;
+ off_t delta_compressed_size;
off_t delta_offset;
off_t delta_out_offset;
@@ -662,8 +663,9 @@ const struct got_error *got_privsep_send_raw_delta_req(struct imsgbuf *, int,
struct got_object_id *);
const struct got_error *got_privsep_send_raw_delta_outfd(struct imsgbuf *, int);
const struct got_error *got_privsep_send_raw_delta(struct imsgbuf *, uint64_t,
- uint64_t, off_t, off_t, off_t, struct got_object_id *);
+ uint64_t, off_t, off_t, off_t, off_t, struct got_object_id *);
const struct got_error *got_privsep_recv_raw_delta(uint64_t *, uint64_t *,
- off_t *, off_t *, off_t *, struct got_object_id **, struct imsgbuf *);
+ off_t *, off_t *, off_t *, off_t *, struct got_object_id **,
+ struct imsgbuf *);
void got_privsep_exec_child(int[2], const char *, const char *);
diff --git a/lib/object.c b/lib/object.c
index b87e6ee..4e5facc 100644
--- a/lib/object.c
+++ b/lib/object.c
@@ -388,8 +388,8 @@ got_object_open_from_packfile(struct got_object **obj, struct got_object_id *id,
const struct got_error *
got_object_read_raw_delta(uint64_t *base_size, uint64_t *result_size,
- off_t *delta_size, off_t *delta_offset, off_t *delta_out_offset,
- struct got_object_id **base_id, int delta_cache_fd,
+ off_t *delta_size, off_t *delta_compressed_size, off_t *delta_offset,
+ off_t *delta_out_offset, struct got_object_id **base_id, int delta_cache_fd,
struct got_packidx *packidx, int obj_idx, struct got_object_id *id,
struct got_repository *repo)
{
@@ -400,6 +400,7 @@ got_object_read_raw_delta(uint64_t *base_size, uint64_t *result_size,
*base_size = 0;
*result_size = 0;
*delta_size = 0;
+ *delta_compressed_size = 0;
*delta_offset = 0;
*delta_out_offset = 0;
@@ -439,7 +440,8 @@ got_object_read_raw_delta(uint64_t *base_size, uint64_t *result_size,
return err;
return got_privsep_recv_raw_delta(base_size, result_size, delta_size,
- delta_offset, delta_out_offset, base_id, pack->privsep_child->ibuf);
+ delta_compressed_size, delta_offset, delta_out_offset, base_id,
+ pack->privsep_child->ibuf);
}
static const struct got_error *
diff --git a/lib/pack.c b/lib/pack.c
index d875046..4bac59b 100644
--- a/lib/pack.c
+++ b/lib/pack.c
@@ -902,23 +902,33 @@ got_pack_parse_offset_delta(off_t *base_offset, size_t *len,
static const struct got_error *
read_delta_data(uint8_t **delta_buf, size_t *delta_len,
- size_t delta_data_offset, struct got_pack *pack)
+ size_t *delta_compressed_len, size_t delta_data_offset,
+ struct got_pack *pack)
{
const struct got_error *err = NULL;
+ size_t consumed = 0;
if (pack->map) {
if (delta_data_offset >= pack->filesize)
return got_error(GOT_ERR_PACK_OFFSET);
err = got_inflate_to_mem_mmap(delta_buf, delta_len,
- NULL, NULL, pack->map, delta_data_offset,
+ &consumed, NULL, pack->map, delta_data_offset,
pack->filesize - delta_data_offset);
+ if (err)
+ return err;
} else {
if (lseek(pack->fd, delta_data_offset, SEEK_SET) == -1)
return got_error_from_errno("lseek");
- err = got_inflate_to_mem_fd(delta_buf, delta_len, NULL,
- NULL, 0, pack->fd);
+ err = got_inflate_to_mem_fd(delta_buf, delta_len,
+ &consumed, NULL, 0, pack->fd);
+ if (err)
+ return err;
}
- return err;
+
+ if (delta_compressed_len)
+ *delta_compressed_len = consumed;
+
+ return NULL;
}
static const struct got_error *
@@ -1200,7 +1210,7 @@ got_pack_get_delta_chain_max_size(uint64_t *max_size,
if (delta_buf == NULL) {
cached = 0;
err = read_delta_data(&delta_buf, &delta_len,
- delta->data_offset, pack);
+ NULL, delta->data_offset, pack);
if (err)
return err;
err = got_delta_cache_add(pack->delta_cache,
@@ -1336,7 +1346,7 @@ got_pack_dump_delta_chain_to_file(size_t *result_size,
pack->delta_cache, delta->data_offset);
if (delta_buf == NULL) {
cached = 0;
- err = read_delta_data(&delta_buf, &delta_len,
+ err = read_delta_data(&delta_buf, &delta_len, NULL,
delta->data_offset, pack);
if (err)
goto done;
@@ -1482,7 +1492,7 @@ got_pack_dump_delta_chain_to_mem(uint8_t **outbuf, size_t *outlen,
pack->delta_cache, delta->data_offset);
if (delta_buf == NULL) {
cached = 0;
- err = read_delta_data(&delta_buf, &delta_len,
+ err = read_delta_data(&delta_buf, &delta_len, NULL,
delta->data_offset, pack);
if (err)
goto done;
@@ -1601,20 +1611,80 @@ got_packfile_extract_object_to_mem(uint8_t **buf, size_t *len,
return err;
}
+static const struct got_error *
+read_raw_delta_data(uint8_t **delta_buf, size_t *delta_len,
+ size_t *delta_len_compressed, uint64_t *base_size, uint64_t *result_size,
+ off_t delta_data_offset, struct got_pack *pack, struct got_packidx *packidx)
+{
+ const struct got_error *err = NULL;
+
+ /* Validate decompression and obtain the decompressed size. */
+ err = read_delta_data(delta_buf, delta_len, delta_len_compressed,
+ delta_data_offset, pack);
+ if (err)
+ return err;
+
+ /* Read delta base/result sizes from head of delta stream. */
+ err = got_delta_get_sizes(base_size, result_size,
+ *delta_buf, *delta_len);
+ if (err)
+ goto done;
+
+ /* Discard decompressed delta and read it again in compressed form. */
+ free(*delta_buf);
+ *delta_buf = malloc(*delta_len_compressed);
+ if (*delta_buf == NULL) {
+ err = got_error_from_errno("malloc");
+ goto done;
+ }
+ if (pack->map) {
+ if (delta_data_offset >= pack->filesize)
+ err = got_error(GOT_ERR_PACK_OFFSET);
+ memcpy(*delta_buf, pack->map + delta_data_offset,
+ *delta_len_compressed);
+ } else {
+ ssize_t n;
+ if (lseek(pack->fd, delta_data_offset, SEEK_SET) == -1) {
+ err = got_error_from_errno("lseek");
+ goto done;
+ }
+ n = read(pack->fd, *delta_buf, *delta_len_compressed);
+ if (n < 0) {
+ err = got_error_from_errno("read");
+ goto done;
+ } else if (n != *delta_len_compressed) {
+ err = got_error(GOT_ERR_IO);
+ goto done;
+ }
+ }
+done:
+ if (err) {
+ free(*delta_buf);
+ *delta_buf = NULL;
+ *delta_len = 0;
+ *delta_len_compressed = 0;
+ *base_size = 0;
+ *result_size = 0;
+ }
+ return err;
+}
+
const struct got_error *
got_packfile_extract_raw_delta(uint8_t **delta_buf, size_t *delta_size,
- off_t *delta_offset, off_t *base_offset, struct got_object_id *base_id,
- uint64_t *base_size, uint64_t *result_size, struct got_pack *pack,
- struct got_packidx *packidx, int idx)
+ size_t *delta_compressed_size, off_t *delta_offset, off_t *base_offset,
+ struct got_object_id *base_id, uint64_t *base_size, uint64_t *result_size,
+ struct got_pack *pack, struct got_packidx *packidx, int idx)
{
const struct got_error *err = NULL;
off_t offset;
uint8_t type;
uint64_t size;
size_t tslen, delta_hdrlen;
+ off_t delta_data_offset;
*delta_buf = NULL;
*delta_size = 0;
+ *delta_compressed_size = 0;
*delta_offset = 0;
*base_offset = 0;
*base_size = 0;
@@ -1659,8 +1729,9 @@ got_packfile_extract_raw_delta(uint8_t **delta_buf, size_t *delta_size,
offset + delta_hdrlen < delta_hdrlen)
return got_error(GOT_ERR_BAD_DELTA);
- err = read_delta_data(delta_buf, delta_size,
- offset + tslen + delta_hdrlen, pack);
+ delta_data_offset = offset + tslen + delta_hdrlen;
+ err = read_raw_delta_data(delta_buf, delta_size, delta_compressed_size,
+ base_size, result_size, delta_data_offset, pack, packidx);
if (err)
return err;
@@ -1669,15 +1740,17 @@ got_packfile_extract_raw_delta(uint8_t **delta_buf, size_t *delta_size,
goto done;
}
- err = got_delta_get_sizes(base_size, result_size, *delta_buf, size);
- if (err)
- goto done;
-
*delta_offset = offset;
done:
if (err) {
free(*delta_buf);
*delta_buf = NULL;
+ *delta_size = 0;
+ *delta_compressed_size = 0;
+ *delta_offset = 0;
+ *base_offset = 0;
+ *base_size = 0;
+ *result_size = 0;
}
return err;
}
diff --git a/lib/pack_create.c b/lib/pack_create.c
index c4297f6..fb1e354 100644
--- a/lib/pack_create.c
+++ b/lib/pack_create.c
@@ -52,6 +52,7 @@
#include "got_lib_privsep.h"
#include "got_lib_repository.h"
#include "got_lib_ratelimit.h"
+#include "got_lib_inflate.h"
#ifndef MIN
#define MIN(_a,_b) ((_a) < (_b) ? (_a) : (_b))
@@ -75,9 +76,10 @@ struct got_pack_meta {
/* The best delta we picked */
struct got_pack_meta *head;
struct got_pack_meta *prev;
- unsigned char *delta_buf; /* if not encoded in delta cache file */
- off_t delta_offset; /* offset in delta cache file */
+ unsigned char *delta_buf; /* if encoded in memory (compressed) */
+ off_t delta_offset; /* offset in delta cache file (compressed) */
off_t delta_len; /* encoded delta length */
+ off_t delta_compressed_len; /* encoded+compressed delta length */
int nchain;
int have_reused_delta;
@@ -209,13 +211,15 @@ encode_delta_in_mem(struct got_pack_meta *m, struct got_raw_object *o,
const struct got_error *err;
unsigned char buf[16], *bp;
int i, j;
- size_t len = 0;
+ size_t len = 0, compressed_len;
+ off_t bufsize = delta_size;
off_t n;
struct got_delta_instruction *d;
+ uint8_t *delta_buf;
- m->delta_buf = malloc(delta_size);
- if (m->delta_buf == NULL)
- return got_error_from_errno("calloc");
+ delta_buf = malloc(bufsize);
+ if (delta_buf == NULL)
+ return got_error_from_errno("malloc");
/* base object size */
buf[0] = base_size & GOT_DELTA_SIZE_VAL_MASK;
@@ -225,9 +229,9 @@ encode_delta_in_mem(struct got_pack_meta *m, struct got_raw_object *o,
buf[i] = n & GOT_DELTA_SIZE_VAL_MASK;
n >>= GOT_DELTA_SIZE_SHIFT;
}
- err = append(&m->delta_buf, &len, &delta_size, buf, i);
+ err = append(&delta_buf, &len, &bufsize, buf, i);
if (err)
- return err;
+ goto done;
/* target object size */
buf[0] = o->size & GOT_DELTA_SIZE_VAL_MASK;
@@ -237,9 +241,9 @@ encode_delta_in_mem(struct got_pack_meta *m, struct got_raw_object *o,
buf[i] = n & GOT_DELTA_SIZE_VAL_MASK;
n >>= GOT_DELTA_SIZE_SHIFT;
}
- err = append(&m->delta_buf, &len, &delta_size, buf, i);
+ err = append(&delta_buf, &len, &bufsize, buf, i);
if (err)
- return err;
+ goto done;
for (j = 0; j < ndeltas; j++) {
d = &deltas[j];
@@ -265,51 +269,63 @@ encode_delta_in_mem(struct got_pack_meta *m, struct got_raw_object *o,
n >>= 8;
}
}
- err = append(&m->delta_buf, &len, &delta_size,
+ err = append(&delta_buf, &len, &bufsize,
buf, bp - buf);
if (err)
- return err;
+ goto done;
} else if (o->f == NULL) {
n = 0;
while (n != d->len) {
buf[0] = (d->len - n < 127) ? d->len - n : 127;
- err = append(&m->delta_buf, &len, &delta_size,
+ err = append(&delta_buf, &len, &bufsize,
buf, 1);
if (err)
- return err;
- err = append(&m->delta_buf, &len, &delta_size,
+ goto done;
+ err = append(&delta_buf, &len, &bufsize,
o->data + o->hdrlen + d->offset + n,
buf[0]);
if (err)
- return err;
+ goto done;
n += buf[0];
}
} else {
char content[128];
size_t r;
- if (fseeko(o->f, o->hdrlen + d->offset, SEEK_SET) == -1)
- return got_error_from_errno("fseeko");
+ if (fseeko(o->f, o->hdrlen + d->offset, SEEK_SET) == -1) {
+ err = got_error_from_errno("fseeko");
+ goto done;
+ }
n = 0;
while (n != d->len) {
buf[0] = (d->len - n < 127) ? d->len - n : 127;
- err = append(&m->delta_buf, &len, &delta_size,
+ err = append(&delta_buf, &len, &bufsize,
buf, 1);
if (err)
- return err;
+ goto done;
r = fread(content, 1, buf[0], o->f);
- if (r != buf[0])
- return got_ferror(o->f, GOT_ERR_IO);
- err = append(&m->delta_buf, &len, &delta_size,
+ if (r != buf[0]) {
+ err = got_ferror(o->f, GOT_ERR_IO);
+ goto done;
+ }
+ err = append(&delta_buf, &len, &bufsize,
content, buf[0]);
if (err)
- return err;
+ goto done;
n += buf[0];
}
}
}
+ err = got_deflate_to_mem_mmap(&m->delta_buf, &compressed_len,
+ NULL, NULL, delta_buf, 0, len);
+ if (err)
+ goto done;
+
m->delta_len = len;
- return NULL;
+ m->delta_compressed_len = compressed_len;
+done:
+ free(delta_buf);
+ return err;
}
static const struct got_error *
@@ -317,11 +333,17 @@ encode_delta(struct got_pack_meta *m, struct got_raw_object *o,
struct got_delta_instruction *deltas, int ndeltas,
off_t base_size, FILE *f)
{
+ const struct got_error *err;
unsigned char buf[16], *bp;
int i, j;
off_t n;
- size_t w;
+ struct got_deflate_buf zb;
struct got_delta_instruction *d;
+ off_t delta_len = 0, compressed_len = 0;
+
+ err = got_deflate_init(&zb, NULL, GOT_DEFLATE_BUFSIZE);
+ if (err)
+ return err;
/* base object size */
buf[0] = base_size & GOT_DELTA_SIZE_VAL_MASK;
@@ -331,9 +353,12 @@ encode_delta(struct got_pack_meta *m, struct got_raw_object *o,
buf[i] = n & GOT_DELTA_SIZE_VAL_MASK;
n >>= GOT_DELTA_SIZE_SHIFT;
}
- w = fwrite(buf, 1, i, f);
- if (w != i)
- return got_ferror(f, GOT_ERR_IO);
+
+ err = got_deflate_append_to_file_mmap(&zb, &compressed_len,
+ buf, 0, i, f, NULL);
+ if (err)
+ goto done;
+ delta_len += i;
/* target object size */
buf[0] = o->size & GOT_DELTA_SIZE_VAL_MASK;
@@ -343,9 +368,12 @@ encode_delta(struct got_pack_meta *m, struct got_raw_object *o,
buf[i] = n & GOT_DELTA_SIZE_VAL_MASK;
n >>= GOT_DELTA_SIZE_SHIFT;
}
- w = fwrite(buf, 1, i, f);
- if (w != i)
- return got_ferror(f, GOT_ERR_IO);
+
+ err = got_deflate_append_to_file_mmap(&zb, &compressed_len,
+ buf, 0, i, f, NULL);
+ if (err)
+ goto done;
+ delta_len += i;
for (j = 0; j < ndeltas; j++) {
d = &deltas[j];
@@ -361,7 +389,6 @@ encode_delta(struct got_pack_meta *m, struct got_raw_object *o,
if (n == 0)
break;
}
-
n = d->len;
if (n != GOT_DELTA_COPY_DEFAULT_LEN) {
/* DELTA_COPY_LEN1 ... DELTA_COPY_LEN3 */
@@ -371,46 +398,75 @@ encode_delta(struct got_pack_meta *m, struct got_raw_object *o,
n >>= 8;
}
}
- w = fwrite(buf, 1, bp - buf, f);
- if (w != bp - buf)
- return got_ferror(f, GOT_ERR_IO);
+ err = got_deflate_append_to_file_mmap(&zb,
+ &compressed_len, buf, 0, bp - buf, f, NULL);
+ if (err)
+ goto done;
+ delta_len += (bp - buf);
} else if (o->f == NULL) {
n = 0;
while (n != d->len) {
buf[0] = (d->len - n < 127) ? d->len - n : 127;
- w = fwrite(buf, 1, 1, f);
- if (w != 1)
- return got_ferror(f, GOT_ERR_IO);
- w = fwrite(o->data + o->hdrlen + d->offset + n,
- 1, buf[0], f);
- if (w != buf[0])
- return got_ferror(f, GOT_ERR_IO);
+ err = got_deflate_append_to_file_mmap(&zb,
+ &compressed_len, buf, 0, 1, f, NULL);
+ if (err)
+ goto done;
+ delta_len++;
+ err = got_deflate_append_to_file_mmap(&zb,
+ &compressed_len,
+ o->data + o->hdrlen + d->offset + n, 0,
+ buf[0], f, NULL);
+ if (err)
+ goto done;
+ delta_len += buf[0];
n += buf[0];
}
} else {
char content[128];
size_t r;
- if (fseeko(o->f, o->hdrlen + d->offset, SEEK_SET) == -1)
- return got_error_from_errno("fseeko");
+ if (fseeko(o->f, o->hdrlen + d->offset, SEEK_SET) == -1) {
+ err = got_error_from_errno("fseeko");
+ goto done;
+ }
n = 0;
while (n != d->len) {
buf[0] = (d->len - n < 127) ? d->len - n : 127;
- w = fwrite(buf, 1, 1, f);
- if (w != 1)
- return got_ferror(f, GOT_ERR_IO);
+ err = got_deflate_append_to_file_mmap(&zb,
+ &compressed_len, buf, 0, 1, f, NULL);
+ if (err)
+ goto done;
+ delta_len++;
r = fread(content, 1, buf[0], o->f);
- if (r != buf[0])
- return got_ferror(o->f, GOT_ERR_IO);
- w = fwrite(content, 1, buf[0], f);
- if (w != buf[0])
- return got_ferror(f, GOT_ERR_IO);
+ if (r != buf[0]) {
+ err = got_ferror(o->f, GOT_ERR_IO);
+ goto done;
+ }
+ err = got_deflate_append_to_file_mmap(&zb,
+ &compressed_len, content, 0, buf[0], f,
+ NULL);
+ if (err)
+ goto done;
+ delta_len += buf[0];
n += buf[0];
}
}
}
- m->delta_len = ftello(f) - m->delta_offset;
- return NULL;
+ err = got_deflate_flush(&zb, f, NULL, &compressed_len);
+ if (err)
+ goto done;
+
+ /* sanity check */
+ if (compressed_len != ftello(f) - m->delta_offset) {
+ err = got_error(GOT_ERR_COMPRESSION);
+ goto done;
+ }
+
+ m->delta_len = delta_len;
+ m->delta_compressed_len = compressed_len;
+done:
+ got_deflate_end(&zb);
+ return err;
}
static const struct got_error *
@@ -459,15 +515,16 @@ reuse_delta(int idx, struct got_pack_meta *m, struct got_pack_metavec *v,
const struct got_error *err = NULL;
struct got_pack_meta *base = NULL;
struct got_object_id *base_obj_id = NULL;
- off_t delta_len = 0, delta_offset = 0, delta_cache_offset = 0;
+ off_t delta_len = 0, delta_compressed_len = 0;
+ off_t delta_offset = 0, delta_cache_offset = 0;
uint64_t base_size, result_size;
if (m->have_reused_delta)
return NULL;
err = got_object_read_raw_delta(&base_size, &result_size, &delta_len,
- &delta_offset, &delta_cache_offset, &base_obj_id, delta_cache_fd,
- packidx, idx, &m->id, repo);
+ &delta_compressed_len, &delta_offset, &delta_cache_offset,
+ &base_obj_id, delta_cache_fd, packidx, idx, &m->id, repo);
if (err)
return err;
@@ -479,6 +536,7 @@ reuse_delta(int idx, struct got_pack_meta *m, struct got_pack_metavec *v,
goto done;
m->delta_len = delta_len;
+ m->delta_compressed_len = delta_compressed_len;
m->delta_offset = delta_cache_offset;
m->prev = base;
m->size = result_size;
@@ -789,15 +847,6 @@ pick_deltas(struct got_pack_meta **meta, int nmeta, int ncolored,
best_ndeltas, best_size, m->prev->size);
} else {
m->delta_offset = ftello(delta_cache);
- /*
- * TODO:
- * Storing compressed delta data in the delta
- * cache file would probably be more efficient
- * than writing uncompressed delta data here
- * and compressing it while writing the pack
- * file. This would also allow for reusing
- * deltas in their compressed form.
- */
err = encode_delta(m, raw, best_deltas,
best_ndeltas, m->prev->size, delta_cache);
}
@@ -1487,7 +1536,7 @@ done:
}
const struct got_error *
-hwrite(FILE *f, void *buf, int len, SHA1_CTX *ctx)
+hwrite(FILE *f, void *buf, off_t len, SHA1_CTX *ctx)
{
size_t n;
@@ -1498,6 +1547,28 @@ hwrite(FILE *f, void *buf, int len, SHA1_CTX *ctx)
return NULL;
}
+const struct got_error *
+hcopy(FILE *fsrc, FILE *fdst, off_t len, SHA1_CTX *ctx)
+{
+ unsigned char buf[65536];
+ off_t remain = len;
+ size_t n;
+
+ while (remain > 0) {
+ size_t copylen = MIN(sizeof(buf), remain);
+ n = fread(buf, 1, copylen, fsrc);
+ if (n != copylen)
+ return got_ferror(fsrc, GOT_ERR_IO);
+ SHA1Update(ctx, buf, copylen);
+ n = fwrite(buf, 1, copylen, fdst);
+ if (n != copylen)
+ return got_ferror(fdst, GOT_ERR_IO);
+ remain -= copylen;
+ }
+
+ return NULL;
+}
+
static void
putbe32(char *b, uint32_t n)
{
@@ -1677,11 +1748,11 @@ write_packed_object(off_t *packfile_size, FILE *packfile,
err = deltahdr(packfile_size, ctx, packfile, m);
if (err)
goto done;
- err = got_deflate_to_file_mmap(&outlen,
- m->delta_buf, 0, m->delta_len, packfile, &csum);
+ err = hwrite(packfile, m->delta_buf,
+ m->delta_compressed_len, ctx);
if (err)
goto done;
- *packfile_size += outlen;
+ *packfile_size += m->delta_compressed_len;
free(m->delta_buf);
m->delta_buf = NULL;
} else {
@@ -1693,11 +1764,11 @@ write_packed_object(off_t *packfile_size, FILE *packfile,
err = deltahdr(packfile_size, ctx, packfile, m);
if (err)
goto done;
- err = got_deflate_to_file(&outlen, delta_cache,
- m->delta_len, packfile, &csum);
+ err = hcopy(delta_cache, packfile,
+ m->delta_compressed_len, ctx);
if (err)
goto done;
- *packfile_size += outlen;
+ *packfile_size += m->delta_compressed_len;
}
done:
if (raw)
@@ -1921,12 +1992,12 @@ got_pack_create(uint8_t *packsha1, FILE *packfile,
progress_cb, progress_arg, &rl, cancel_cb, cancel_arg);
if (err)
goto done;
- if (fseeko(delta_cache, 0L, SEEK_SET) == -1) {
- err = got_error_from_errno("fseeko");
- goto done;
- }
}
+ if (fflush(delta_cache) == EOF) {
+ err = got_error_from_errno("fflush");
+ goto done;
+ }
err = genpack(packsha1, packfile, delta_cache, deltify.meta,
deltify.nmeta, reuse.meta, reuse.nmeta, ncolored, nfound, ntrees,
nours, repo, progress_cb, progress_arg, &rl,
diff --git a/lib/privsep.c b/lib/privsep.c
index de7f8e9..037c960 100644
--- a/lib/privsep.c
+++ b/lib/privsep.c
@@ -2753,8 +2753,8 @@ got_privsep_send_raw_delta_outfd(struct imsgbuf *ibuf, int fd)
const struct got_error *
got_privsep_send_raw_delta(struct imsgbuf *ibuf, uint64_t base_size,
- uint64_t result_size, off_t delta_size, off_t delta_offset,
- off_t delta_out_offset, struct got_object_id *base_id)
+ uint64_t result_size, off_t delta_size, off_t delta_compressed_size,
+ off_t delta_offset, off_t delta_out_offset, struct got_object_id *base_id)
{
struct got_imsg_raw_delta idelta;
int ret;
@@ -2762,6 +2762,7 @@ got_privsep_send_raw_delta(struct imsgbuf *ibuf, uint64_t base_size,
idelta.base_size = base_size;
idelta.result_size = result_size;
idelta.delta_size = delta_size;
+ idelta.delta_compressed_size = delta_compressed_size;
idelta.delta_offset = delta_offset;
idelta.delta_out_offset = delta_out_offset;
memcpy(idelta.base_id, base_id->sha1, SHA1_DIGEST_LENGTH);
@@ -2776,8 +2777,8 @@ got_privsep_send_raw_delta(struct imsgbuf *ibuf, uint64_t base_size,
const struct got_error *
got_privsep_recv_raw_delta(uint64_t *base_size, uint64_t *result_size,
- off_t *delta_size, off_t *delta_offset, off_t *delta_out_offset,
- struct got_object_id **base_id, struct imsgbuf *ibuf)
+ off_t *delta_size, off_t *delta_compressed_size, off_t *delta_offset,
+ off_t *delta_out_offset, struct got_object_id **base_id, struct imsgbuf *ibuf)
{
const struct got_error *err = NULL;
struct imsg imsg;
@@ -2787,6 +2788,7 @@ got_privsep_recv_raw_delta(uint64_t *base_size, uint64_t *result_size,
*base_size = 0;
*result_size = 0;
*delta_size = 0;
+ *delta_compressed_size = 0;
*delta_offset = 0;
*delta_out_offset = 0;
*base_id = NULL;
@@ -2807,6 +2809,7 @@ got_privsep_recv_raw_delta(uint64_t *base_size, uint64_t *result_size,
*base_size = delta->base_size;
*result_size = delta->result_size;
*delta_size = delta->delta_size;
+ *delta_compressed_size = delta->delta_compressed_size;
*delta_offset = delta->delta_offset;
*delta_out_offset = delta->delta_out_offset;
*base_id = calloc(1, sizeof(**base_id));
diff --git a/libexec/got-read-pack/got-read-pack.c b/libexec/got-read-pack/got-read-pack.c
index b014128..0b2b5ee 100644
--- a/libexec/got-read-pack/got-read-pack.c
+++ b/libexec/got-read-pack/got-read-pack.c
@@ -869,7 +869,7 @@ raw_delta_request(struct imsg *imsg, struct imsgbuf *ibuf,
{
const struct got_error *err = NULL;
struct got_imsg_raw_delta_request req;
- size_t datalen, delta_size;
+ size_t datalen, delta_size, delta_compressed_size;
off_t delta_offset;
uint8_t *delta_buf = NULL;
struct got_object_id id, base_id;
@@ -886,8 +886,8 @@ raw_delta_request(struct imsg *imsg, struct imsgbuf *ibuf,
imsg->fd = -1;
err = got_packfile_extract_raw_delta(&delta_buf, &delta_size,
- &delta_offset, &base_offset, &base_id, &base_size, &result_size,
- pack, packidx, req.idx);
+ &delta_compressed_size, &delta_offset, &base_offset, &base_id,
+ &base_size, &result_size, pack, packidx, req.idx);
if (err)
goto done;
@@ -902,8 +902,8 @@ raw_delta_request(struct imsg *imsg, struct imsgbuf *ibuf,
}
delta_out_offset = ftello(delta_outfile);
- w = fwrite(delta_buf, 1, delta_size, delta_outfile);
- if (w != delta_size) {
+ w = fwrite(delta_buf, 1, delta_compressed_size, delta_outfile);
+ if (w != delta_compressed_size) {
err = got_ferror(delta_outfile, GOT_ERR_IO);
goto done;
}
@@ -913,7 +913,8 @@ raw_delta_request(struct imsg *imsg, struct imsgbuf *ibuf,
}
err = got_privsep_send_raw_delta(ibuf, base_size, result_size,
- delta_size, delta_offset, delta_out_offset, &base_id);
+ delta_size, delta_compressed_size, delta_offset, delta_out_offset,
+ &base_id);
done:
free(delta_buf);
return err;