Hash :
908b0194
Author :
Date :
2018-02-12T21:29:08
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
/*
* Copyright (c) 2018 Stefan Sperling <stsp@openbsd.org>
*
* Permission to use, copy, modify, and distribute this software for any
* purpose with or without fee is hereby granted, provided that the above
* copyright notice and this permission notice appear in all copies.
*
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*/
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/queue.h>
#include <dirent.h>
#include <errno.h>
#include <stdio.h>
#include <stdint.h>
#include <stdlib.h>
#include <string.h>
#include <limits.h>
#include <sha1.h>
#include <endian.h>
#include <zlib.h>
#include "got_error.h"
#include "got_object.h"
#include "got_repository.h"
#include "got_sha1.h"
#include "pack.h"
#include "path.h"
#include "delta.h"
#include "zb.h"
#include "object.h"
#define GOT_PACK_PREFIX "pack-"
#define GOT_PACKFILE_SUFFIX ".pack"
#define GOT_PACKIDX_SUFFIX ".idx"
#define GOT_PACKFILE_NAMELEN (strlen(GOT_PACK_PREFIX) + \
SHA1_DIGEST_STRING_LENGTH - 1 + \
strlen(GOT_PACKFILE_SUFFIX))
#define GOT_PACKIDX_NAMELEN (strlen(GOT_PACK_PREFIX) + \
SHA1_DIGEST_STRING_LENGTH - 1 + \
strlen(GOT_PACKIDX_SUFFIX))
#ifndef MIN
#define MIN(_a,_b) ((_a) < (_b) ? (_a) : (_b))
#endif
static const struct got_error *
verify_fanout_table(uint32_t *fanout_table)
{
int i;
for (i = 0; i < 0xff - 1; i++) {
if (be32toh(fanout_table[i]) > be32toh(fanout_table[i + 1]))
return got_error(GOT_ERR_BAD_PACKIDX);
}
return NULL;
}
static const struct got_error *
get_packfile_size(size_t *size, const char *path_idx)
{
struct stat sb;
char *path_pack;
char base_path[PATH_MAX];
char *dot;
if (strlcpy(base_path, path_idx, PATH_MAX) > PATH_MAX)
return got_error(GOT_ERR_NO_SPACE);
dot = strrchr(base_path, '.');
if (dot == NULL)
return got_error(GOT_ERR_BAD_PATH);
*dot = '\0';
if (asprintf(&path_pack, "%s.pack", base_path) == -1)
return got_error(GOT_ERR_NO_MEM);
if (stat(path_pack, &sb) != 0) {
free(path_pack);
return got_error_from_errno();
}
free(path_pack);
*size = sb.st_size;
return 0;
}
const struct got_error *
got_packidx_open(struct got_packidx_v2_hdr **packidx, const char *path)
{
struct got_packidx_v2_hdr *p;
FILE *f;
const struct got_error *err = NULL;
size_t n, nobj, packfile_size;
SHA1_CTX ctx;
uint8_t sha1[SHA1_DIGEST_LENGTH];
SHA1Init(&ctx);
f = fopen(path, "rb");
if (f == NULL)
return got_error(GOT_ERR_BAD_PATH);
err = get_packfile_size(&packfile_size, path);
if (err)
return err;
p = calloc(1, sizeof(*p));
if (p == NULL) {
err = got_error(GOT_ERR_NO_MEM);
goto done;
}
n = fread(&p->magic, sizeof(p->magic), 1, f);
if (n != 1) {
err = got_ferror(f, GOT_ERR_BAD_PACKIDX);
goto done;
}
if (betoh32(p->magic) != GOT_PACKIDX_V2_MAGIC) {
err = got_error(GOT_ERR_BAD_PACKIDX);
goto done;
}
SHA1Update(&ctx, (uint8_t *)&p->magic, sizeof(p->magic));
n = fread(&p->version, sizeof(p->version), 1, f);
if (n != 1) {
err = got_ferror(f, GOT_ERR_BAD_PACKIDX);
goto done;
}
if (betoh32(p->version) != GOT_PACKIDX_VERSION) {
err = got_error(GOT_ERR_BAD_PACKIDX);
goto done;
}
SHA1Update(&ctx, (uint8_t *)&p->version, sizeof(p->version));
n = fread(&p->fanout_table, sizeof(p->fanout_table), 1, f);
if (n != 1) {
err = got_ferror(f, GOT_ERR_BAD_PACKIDX);
goto done;
}
err = verify_fanout_table(p->fanout_table);
if (err)
goto done;
SHA1Update(&ctx, (uint8_t *)p->fanout_table, sizeof(p->fanout_table));
nobj = betoh32(p->fanout_table[0xff]);
p->sorted_ids = calloc(nobj, sizeof(*p->sorted_ids));
if (p->sorted_ids == NULL) {
err = got_error(GOT_ERR_NO_MEM);
goto done;
}
n = fread(p->sorted_ids, sizeof(*p->sorted_ids), nobj, f);
if (n != nobj) {
err = got_ferror(f, GOT_ERR_BAD_PACKIDX);
goto done;
}
SHA1Update(&ctx, (uint8_t *)p->sorted_ids,
nobj * sizeof(*p->sorted_ids));
p->crc32 = calloc(nobj, sizeof(*p->crc32));
if (p->crc32 == NULL) {
err = got_error(GOT_ERR_NO_MEM);
goto done;
}
n = fread(p->crc32, sizeof(*p->crc32), nobj, f);
if (n != nobj) {
err = got_ferror(f, GOT_ERR_BAD_PACKIDX);
goto done;
}
SHA1Update(&ctx, (uint8_t *)p->crc32, nobj * sizeof(*p->crc32));
p->offsets = calloc(nobj, sizeof(*p->offsets));
if (p->offsets == NULL) {
err = got_error(GOT_ERR_NO_MEM);
goto done;
}
n = fread(p->offsets, sizeof(*p->offsets), nobj, f);
if (n != nobj) {
err = got_ferror(f, GOT_ERR_BAD_PACKIDX);
goto done;
}
SHA1Update(&ctx, (uint8_t *)p->offsets, nobj * sizeof(*p->offsets));
/* Large file offsets are contained only in files > 2GB. */
if (packfile_size <= 0x80000000)
goto checksum;
p->large_offsets = calloc(nobj, sizeof(*p->large_offsets));
if (p->large_offsets == NULL) {
err = got_error(GOT_ERR_NO_MEM);
goto done;
}
n = fread(p->large_offsets, sizeof(*p->large_offsets), nobj, f);
if (n != nobj) {
err = got_ferror(f, GOT_ERR_BAD_PACKIDX);
goto done;
}
SHA1Update(&ctx, (uint8_t*)p->large_offsets,
nobj * sizeof(*p->large_offsets));
checksum:
n = fread(&p->trailer, sizeof(p->trailer), 1, f);
if (n != 1) {
err = got_ferror(f, GOT_ERR_BAD_PACKIDX);
goto done;
}
SHA1Update(&ctx, p->trailer.packfile_sha1, SHA1_DIGEST_LENGTH);
SHA1Final(sha1, &ctx);
if (memcmp(p->trailer.packidx_sha1, sha1, SHA1_DIGEST_LENGTH) != 0)
err = got_error(GOT_ERR_PACKIDX_CSUM);
done:
fclose(f);
if (err)
got_packidx_close(p);
else
*packidx = p;
return err;
}
void
got_packidx_close(struct got_packidx_v2_hdr *packidx)
{
free(packidx->sorted_ids);
free(packidx->offsets);
free(packidx->crc32);
free(packidx->large_offsets);
free(packidx);
}
static int
is_packidx_filename(const char *name, size_t len)
{
if (len != GOT_PACKIDX_NAMELEN)
return 0;
if (strncmp(name, GOT_PACK_PREFIX, strlen(GOT_PACK_PREFIX)) != 0)
return 0;
if (strcmp(name + strlen(GOT_PACK_PREFIX) +
SHA1_DIGEST_STRING_LENGTH - 1, GOT_PACKIDX_SUFFIX) != 0)
return 0;
return 1;
}
static off_t
get_object_offset(struct got_packidx_v2_hdr *packidx, int idx)
{
uint32_t totobj = betoh32(packidx->fanout_table[0xff]);
uint32_t offset = betoh32(packidx->offsets[idx]);
if (offset & GOT_PACKIDX_OFFSET_VAL_IS_LARGE_IDX) {
uint64_t loffset;
idx = offset & GOT_PACKIDX_OFFSET_VAL_MASK;
if (idx < 0 || idx > totobj || packidx->large_offsets == NULL)
return -1;
loffset = betoh64(packidx->large_offsets[idx]);
return (loffset > INT64_MAX ? -1 : (off_t)loffset);
}
return (off_t)(offset & GOT_PACKIDX_OFFSET_VAL_MASK);
}
static int
get_object_idx(struct got_packidx_v2_hdr *packidx, struct got_object_id *id)
{
u_int8_t id0 = id->sha1[0];
uint32_t totobj = betoh32(packidx->fanout_table[0xff]);
int i = 0;
if (id0 > 0)
i = betoh32(packidx->fanout_table[id0 - 1]);
while (i < totobj) {
struct got_object_id *oid = &packidx->sorted_ids[i];
uint32_t offset;
int cmp = got_object_id_cmp(id, oid);
if (cmp == 0)
return i;
i++;
}
return -1;
}
static const struct got_error *
search_packidx(struct got_packidx_v2_hdr **packidx, int *idx,
struct got_repository *repo, struct got_object_id *id)
{
const struct got_error *err;
char *path_packdir;
DIR *packdir;
struct dirent *dent;
char *path_packidx;
path_packdir = got_repo_get_path_objects_pack(repo);
if (path_packdir == NULL)
return got_error(GOT_ERR_NO_MEM);
packdir = opendir(path_packdir);
if (packdir == NULL) {
err = got_error_from_errno();
goto done;
}
while ((dent = readdir(packdir)) != NULL) {
if (!is_packidx_filename(dent->d_name, dent->d_namlen))
continue;
if (asprintf(&path_packidx, "%s/%s", path_packdir,
dent->d_name) == -1) {
err = got_error(GOT_ERR_NO_MEM);
goto done;
}
err = got_packidx_open(packidx, path_packidx);
free(path_packidx);
if (err)
goto done;
*idx = get_object_idx(*packidx, id);
if (*idx != -1) {
err = NULL; /* found the object */
goto done;
}
got_packidx_close(*packidx);
*packidx = NULL;
}
err = got_error(GOT_ERR_NO_OBJ);
done:
free(path_packdir);
if (packdir && closedir(packdir) != 0 && err == 0)
err = got_error_from_errno();
return err;
}
static const struct got_error *
get_packfile_path(char **path_packfile, struct got_repository *repo,
struct got_packidx_v2_hdr *packidx)
{
char *path_packdir;
char hex[SHA1_DIGEST_STRING_LENGTH];
char *sha1str;
char *path_packidx;
*path_packfile = NULL;
path_packdir = got_repo_get_path_objects_pack(repo);
if (path_packdir == NULL)
return got_error(GOT_ERR_NO_MEM);
sha1str = got_sha1_digest_to_str(packidx->trailer.packfile_sha1,
hex, sizeof(hex));
if (sha1str == NULL)
return got_error(GOT_ERR_PACKIDX_CSUM);
if (asprintf(path_packfile, "%s/%s%s%s", path_packdir,
GOT_PACK_PREFIX, sha1str, GOT_PACKFILE_SUFFIX) == -1) {
*path_packfile = NULL;
return got_error(GOT_ERR_NO_MEM);
}
return NULL;
}
const struct got_error *
read_packfile_hdr(FILE *f, struct got_packidx_v2_hdr *packidx)
{
const struct got_error *err = NULL;
uint32_t totobj = betoh32(packidx->fanout_table[0xff]);
struct got_packfile_hdr hdr;
size_t n;
n = fread(&hdr, sizeof(hdr), 1, f);
if (n != 1)
return got_ferror(f, GOT_ERR_BAD_PACKIDX);
if (betoh32(hdr.signature) != GOT_PACKFILE_SIGNATURE ||
betoh32(hdr.version) != GOT_PACKFILE_VERSION ||
betoh32(hdr.nobjects) != totobj)
err = got_error(GOT_ERR_BAD_PACKFILE);
return err;
}
static const struct got_error *
open_packfile(FILE **packfile, char **path_packfile,
struct got_repository *repo, struct got_packidx_v2_hdr *packidx)
{
const struct got_error *err;
*packfile = NULL;
err = get_packfile_path(path_packfile, repo, packidx);
if (err)
return err;
*packfile = fopen(*path_packfile, "rb");
if (*packfile == NULL) {
err = got_error_from_errno();
free(*path_packfile);
return err;
}
err = read_packfile_hdr(*packfile, packidx);
if (err) {
fclose(*packfile);
*packfile = NULL;
}
return err;
}
static const struct got_error *
parse_object_type_and_size(uint8_t *type, uint64_t *size, size_t *len,
FILE *packfile)
{
uint8_t t = 0;
uint64_t s = 0;
uint8_t sizeN;
size_t n;
int i = 0;
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 = fread(&sizeN, sizeof(sizeN), 1, packfile);
if (n != 1)
return got_ferror(packfile, GOT_ERR_BAD_PACKIDX);
if (i == 0) {
t = (sizeN & GOT_PACK_OBJ_SIZE0_TYPE_MASK) >>
GOT_PACK_OBJ_SIZE0_TYPE_MASK_SHIFT;
s = (sizeN & GOT_PACK_OBJ_SIZE0_VAL_MASK);
} else {
size_t shift = 4 + 7 * (i - 1);
s |= ((sizeN & GOT_PACK_OBJ_SIZE_VAL_MASK) << shift);
}
i++;
} while (sizeN & GOT_PACK_OBJ_SIZE_MORE);
*type = t;
*size = s;
*len = i * sizeof(sizeN);
return NULL;
}
static const struct got_error *
open_plain_object(struct got_object **obj, const char *path_packfile,
struct got_object_id *id, uint8_t type, off_t offset, size_t size)
{
*obj = calloc(1, sizeof(**obj));
if (*obj == NULL)
return got_error(GOT_ERR_NO_MEM);
(*obj)->path_packfile = strdup(path_packfile);
if ((*obj)->path_packfile == NULL) {
free(*obj);
*obj = NULL;
return got_error(GOT_ERR_NO_MEM);
}
(*obj)->type = type;
(*obj)->flags = GOT_OBJ_FLAG_PACKED;
(*obj)->hdrlen = 0;
(*obj)->size = size;
memcpy(&(*obj)->id, id, sizeof((*obj)->id));
(*obj)->pack_offset = offset;
return NULL;
}
static const struct got_error *
parse_negative_offset(int64_t *offset, size_t *len, FILE *packfile)
{
int64_t o = 0;
uint8_t offN;
size_t n;
int i = 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 = fread(&offN, sizeof(offN), 1, packfile);
if (n != 1)
return got_ferror(packfile, GOT_ERR_BAD_PACKIDX);
if (i == 0)
o = (offN & GOT_PACK_OBJ_DELTA_OFF_VAL_MASK);
else {
o++;
o <<= 7;
o += (offN & GOT_PACK_OBJ_DELTA_OFF_VAL_MASK);
}
i++;
} 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, FILE *packfile, off_t offset)
{
const struct got_error *err;
int64_t negoffset;
size_t negofflen;
err = parse_negative_offset(&negoffset, &negofflen, packfile);
if (err)
return err;
/* Compute the base object's offset (must be in the same pack file). */
*base_offset = (offset - negoffset);
if (*base_offset <= 0)
return got_error(GOT_ERR_BAD_PACKFILE);
return NULL;
}
static const struct got_error *
resolve_delta_chain(struct got_delta_chain *, struct got_repository *,
FILE *, const char *, off_t, size_t, int, size_t);
static const struct got_error *
add_delta(struct got_delta_chain *deltas, const char *path_packfile,
off_t delta_offset, size_t tslen, int delta_type, size_t delta_size,
size_t delta_data_offset)
{
struct got_delta *delta;
delta = got_delta_open(path_packfile, delta_offset, tslen,
delta_type, delta_size, delta_data_offset);
if (delta == NULL)
return got_error(GOT_ERR_NO_MEM);
/* delta is freed in got_object_close() */
deltas->nentries++;
SIMPLEQ_INSERT_HEAD(&deltas->entries, delta, entry);
return NULL;
}
static const struct got_error *
resolve_offset_delta(struct got_delta_chain *deltas,
struct got_repository *repo, FILE *packfile, const char *path_packfile,
off_t delta_offset,size_t tslen, int delta_type, size_t delta_size)
{
const struct got_error *err;
off_t base_offset;
uint8_t base_type;
uint64_t base_size;
size_t base_tslen;
off_t delta_data_offset;
err = parse_offset_delta(&base_offset, packfile, delta_offset);
if (err)
return err;
delta_data_offset = ftello(packfile);
if (delta_data_offset == -1)
return got_error_from_errno();
err = add_delta(deltas, path_packfile, delta_offset, tslen,
delta_type, delta_size, delta_data_offset);
if (err)
return err;
/* An offset delta must be in the same packfile. */
if (fseeko(packfile, base_offset, SEEK_SET) != 0)
return got_error_from_errno();
err = parse_object_type_and_size(&base_type, &base_size, &base_tslen,
packfile);
if (err)
return err;
return resolve_delta_chain(deltas, repo, packfile, path_packfile,
base_offset, base_tslen, base_type, base_size);
}
static const struct got_error *
resolve_ref_delta(struct got_delta_chain *deltas, struct got_repository *repo,
FILE *packfile, const char *path_packfile, off_t delta_offset,
size_t tslen, int delta_type, size_t delta_size)
{
const struct got_error *err;
struct got_object_id id;
struct got_packidx_v2_hdr *packidx;
int idx;
off_t base_offset;
uint8_t base_type;
uint64_t base_size;
size_t base_tslen;
size_t n;
FILE *base_packfile;
char *path_base_packfile;
off_t delta_data_offset;
n = fread(&id, sizeof(id), 1, packfile);
if (n != 1)
return got_ferror(packfile, GOT_ERR_IO);
delta_data_offset = ftello(packfile);
if (delta_data_offset == -1)
return got_error_from_errno();
err = add_delta(deltas, path_packfile, delta_offset, tslen,
delta_type, delta_size, delta_data_offset);
if (err)
return err;
err = search_packidx(&packidx, &idx, repo, &id);
if (err)
return err;
base_offset = get_object_offset(packidx, idx);
if (base_offset == (uint64_t)-1) {
got_packidx_close(packidx);
return got_error(GOT_ERR_BAD_PACKIDX);
}
err = open_packfile(&base_packfile, &path_base_packfile, repo, packidx);
got_packidx_close(packidx);
if (err)
return err;
if (fseeko(base_packfile, base_offset, SEEK_SET) != 0) {
err = got_error_from_errno();
goto done;
}
err = parse_object_type_and_size(&base_type, &base_size, &base_tslen,
base_packfile);
if (err)
goto done;
err = resolve_delta_chain(deltas, repo, base_packfile,
path_base_packfile, base_offset, base_tslen, base_type,
base_size);
done:
free(path_base_packfile);
if (base_packfile && fclose(base_packfile) == -1 && err == 0)
err = got_error_from_errno();
return err;
}
static const struct got_error *
resolve_delta_chain(struct got_delta_chain *deltas, struct got_repository *repo,
FILE *packfile, const char *path_packfile, off_t delta_offset, size_t tslen,
int delta_type, size_t delta_size)
{
const struct got_error *err = NULL;
switch (delta_type) {
case GOT_OBJ_TYPE_COMMIT:
case GOT_OBJ_TYPE_TREE:
case GOT_OBJ_TYPE_BLOB:
case GOT_OBJ_TYPE_TAG:
/* Plain types are the final delta base. Recursion ends. */
err = add_delta(deltas, path_packfile, delta_offset, tslen,
delta_type, delta_size, 0);
break;
case GOT_OBJ_TYPE_OFFSET_DELTA:
err = resolve_offset_delta(deltas, repo, packfile,
path_packfile, delta_offset, tslen, delta_type,
delta_size);
break;
case GOT_OBJ_TYPE_REF_DELTA:
err = resolve_ref_delta(deltas, repo, packfile,
path_packfile, delta_offset, tslen, delta_type,
delta_size);
break;
default:
return got_error(GOT_ERR_NOT_IMPL);
}
return err;
}
static const struct got_error *
open_delta_object(struct got_object **obj, struct got_repository *repo,
struct got_packidx_v2_hdr *packidx, const char *path_packfile,
FILE *packfile, struct got_object_id *id, off_t offset, size_t tslen,
int delta_type, size_t delta_size)
{
const struct got_error *err = NULL;
struct got_object_id base_id;
uint8_t base_type;
int resolved_type;
uint64_t base_size;
size_t base_tslen;
*obj = calloc(1, sizeof(**obj));
if (*obj == NULL)
return got_error(GOT_ERR_NO_MEM);
(*obj)->flags = 0;
(*obj)->hdrlen = 0;
(*obj)->size = 0; /* Not yet known because deltas aren't combined. */
memcpy(&(*obj)->id, id, sizeof((*obj)->id));
(*obj)->pack_offset = offset + tslen;
(*obj)->path_packfile = strdup(path_packfile);
if ((*obj)->path_packfile == NULL) {
err = got_error(GOT_ERR_NO_MEM);
goto done;
}
(*obj)->flags |= GOT_OBJ_FLAG_PACKED;
SIMPLEQ_INIT(&(*obj)->deltas.entries);
(*obj)->flags |= GOT_OBJ_FLAG_DELTIFIED;
err = resolve_delta_chain(&(*obj)->deltas, repo, packfile,
path_packfile, offset, tslen, delta_type, delta_size);
if (err)
goto done;
err = got_delta_chain_get_base_type(&resolved_type, &(*obj)->deltas);
if (err)
goto done;
(*obj)->type = resolved_type;
done:
if (err) {
got_object_close(*obj);
*obj = NULL;
}
return err;
}
static const struct got_error *
open_packed_object(struct got_object **obj, struct got_repository *repo,
struct got_packidx_v2_hdr *packidx, int idx, struct got_object_id *id)
{
const struct got_error *err = NULL;
off_t offset;
char *path_packfile;
FILE *packfile;
uint8_t type;
uint64_t size;
size_t tslen;
*obj = NULL;
offset = get_object_offset(packidx, idx);
if (offset == (uint64_t)-1)
return got_error(GOT_ERR_BAD_PACKIDX);
err = open_packfile(&packfile, &path_packfile, repo, packidx);
if (err)
return err;
if (fseeko(packfile, offset, SEEK_SET) != 0) {
err = got_error_from_errno();
goto done;
}
err = parse_object_type_and_size(&type, &size, &tslen, packfile);
if (err)
goto done;
switch (type) {
case GOT_OBJ_TYPE_COMMIT:
case GOT_OBJ_TYPE_TREE:
case GOT_OBJ_TYPE_BLOB:
case GOT_OBJ_TYPE_TAG:
err = open_plain_object(obj, path_packfile, id, type,
offset + tslen, size);
break;
case GOT_OBJ_TYPE_OFFSET_DELTA:
case GOT_OBJ_TYPE_REF_DELTA:
err = open_delta_object(obj, repo, packidx, path_packfile,
packfile, id, offset, tslen, type, size);
break;
default:
err = got_error(GOT_ERR_NOT_IMPL);
goto done;
}
done:
free(path_packfile);
if (packfile && fclose(packfile) == -1 && err == 0)
err = got_error_from_errno();
return err;
}
const struct got_error *
got_packfile_open_object(struct got_object **obj, struct got_object_id *id,
struct got_repository *repo)
{
const struct got_error *err = NULL;
struct got_packidx_v2_hdr *packidx = NULL;
int idx;
err = search_packidx(&packidx, &idx, repo, id);
if (err)
return err;
err = open_packed_object(obj, repo, packidx, idx, id);
got_packidx_close(packidx);
return err;
}
static const struct got_error *
dump_delta_chain(struct got_delta_chain *deltas, FILE *outfile)
{
const struct got_error *err = NULL;
struct got_delta *delta;
FILE *base_file, *accum_file;
int n = 0;
if (SIMPLEQ_EMPTY(&deltas->entries))
return got_error(GOT_ERR_BAD_DELTA_CHAIN);
base_file = got_opentemp();
if (base_file == NULL)
return got_error_from_errno();
accum_file = got_opentemp();
if (accum_file == NULL) {
err = got_error_from_errno();
fclose(base_file);
return err;
}
/* Deltas are ordered in ascending order. */
SIMPLEQ_FOREACH(delta, &deltas->entries, entry) {
uint8_t *delta_buf = NULL;
size_t delta_len = 0;
FILE *delta_file;
delta_file = fopen(delta->path_packfile, "rb");
if (delta_file == NULL) {
err = got_error_from_errno();
goto done;
}
if (n == 0) {
/* Plain object types are the delta base. */
if (delta->type != GOT_OBJ_TYPE_COMMIT &&
delta->type != GOT_OBJ_TYPE_TREE &&
delta->type != GOT_OBJ_TYPE_BLOB &&
delta->type != GOT_OBJ_TYPE_TAG) {
err = got_error(GOT_ERR_BAD_DELTA_CHAIN);
goto done;
}
if (fseeko(delta_file, delta->offset + delta->tslen,
SEEK_SET) != 0) {
fclose(delta_file);
err = got_error_from_errno();
goto done;
}
err = got_inflate_to_file(&delta_len, delta_file,
base_file);
fclose(delta_file);
if (err)
goto done;
n++;
rewind(base_file);
continue;
}
if (fseeko(delta_file, delta->data_offset, SEEK_CUR) != 0) {
fclose(delta_file);
err = got_error_from_errno();
goto done;
}
/* Delta streams should always fit in memory. */
err = got_inflate_to_mem(&delta_buf, &delta_len, delta_file);
fclose(delta_file);
if (err)
goto done;
err = got_delta_apply(base_file, delta_buf, delta_len,
/* Final delta application writes to the output file. */
++n < deltas->nentries ? accum_file : outfile);
free(delta_buf);
if (err)
goto done;
if (n < deltas->nentries) {
/* Accumulated delta becomes the new base. */
FILE *tmp = accum_file;
accum_file = base_file;
base_file = tmp;
rewind(base_file);
rewind(accum_file);
}
}
done:
fclose(base_file);
fclose(accum_file);
rewind(outfile);
return err;
}
const struct got_error *
got_packfile_extract_object(FILE **f, struct got_object *obj,
struct got_repository *repo)
{
const struct got_error *err = NULL;
FILE *packfile = NULL;
if ((obj->flags & GOT_OBJ_FLAG_PACKED) == 0)
return got_error(GOT_ERR_OBJ_NOT_PACKED);
*f = got_opentemp();
if (*f == NULL) {
err = got_error(GOT_ERR_FILE_OPEN);
goto done;
}
if ((obj->flags & GOT_OBJ_FLAG_DELTIFIED) == 0) {
packfile = fopen(obj->path_packfile, "rb");
if (packfile == NULL) {
err = got_error_from_errno();
goto done;
}
if (fseeko(packfile, obj->pack_offset, SEEK_SET) != 0) {
err = got_error_from_errno();
goto done;
}
err = got_inflate_to_file(&obj->size, packfile, *f);
} else
err = dump_delta_chain(&obj->deltas, *f);
done:
if (packfile)
fclose(packfile);
if (err && *f)
fclose(*f);
return err;
}