Hash :
97128b57
Author :
Date :
2018-04-02T10:09:22
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 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368
/*
* 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 <fcntl.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_lib_sha1.h"
#include "got_lib_pack.h"
#include "got_lib_path.h"
#include "got_lib_delta.h"
#include "got_lib_zbuf.h"
#include "got_lib_object.h"
#include "got_lib_repository.h"
#ifndef nitems
#define nitems(_a) (sizeof(_a) / sizeof((_a)[0]))
#endif
#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)
{
struct stat sb;
char *dot;
*size = 0;
dot = strrchr(path, '.');
if (dot == NULL)
return got_error(GOT_ERR_BAD_PATH);
/* Path must point to a pack index or to a pack file. */
if (strcmp(dot, ".idx") == 0) {
const struct got_error *err = NULL;
char *path_pack;
char base_path[PATH_MAX];
/* Convert pack index path to pack file path. */
if (strlcpy(base_path, path, 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_from_errno();
if (stat(path_pack, &sb) != 0)
err = got_error_from_errno();
free(path_pack);
if (err)
return err;
} else if (strcmp(dot, ".pack") == 0) {
if (stat(path, &sb) != 0)
return got_error_from_errno();
} else
return got_error(GOT_ERR_BAD_PATH);
*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_from_errno();
err = get_packfile_size(&packfile_size, path);
if (err)
return err;
p = calloc(1, sizeof(*p));
if (p == NULL) {
err = got_error_from_errno();
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_from_errno();
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_from_errno();
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_from_errno();
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_from_errno();
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 const struct got_error *
get_packfile_path(char **path_packfile, struct got_repository *repo,
struct got_packidx_v2_hdr *packidx);
static int
get_object_idx(struct got_packidx_v2_hdr *packidx, struct got_object_id *id, struct got_repository *repo)
{
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];
int cmp = got_object_id_cmp(id, oid);
if (cmp == 0)
return i;
i++;
}
return -1;
}
static struct got_packidx_v2_hdr *
dup_packidx(struct got_packidx_v2_hdr *packidx)
{
struct got_packidx_v2_hdr *p;
size_t nobj;
p = calloc(1, sizeof(*p));
if (p == NULL)
return NULL;
memcpy(p, packidx, sizeof(*p));
p->sorted_ids = NULL;
p->crc32 = NULL;
p->offsets = NULL;
p->large_offsets = NULL;
nobj = betoh32(p->fanout_table[0xff]);
p->sorted_ids = calloc(nobj, sizeof(*p->sorted_ids));
if (p->sorted_ids == NULL)
goto err;
memcpy(p->sorted_ids, packidx->sorted_ids, nobj * sizeof(*p->sorted_ids));
p->crc32 = calloc(nobj, sizeof(*p->crc32));
if (p->crc32 == NULL)
goto err;
memcpy(p->crc32, packidx->crc32, nobj * sizeof(*p->crc32));
p->offsets = calloc(nobj, sizeof(*p->offsets));
if (p->offsets == NULL)
goto err;
memcpy(p->offsets, packidx->offsets, nobj * sizeof(*p->offsets));
if (p->large_offsets) {
p->large_offsets = calloc(nobj, sizeof(*p->large_offsets));
if (p->large_offsets == NULL)
goto err;
memcpy(p->large_offsets, packidx->large_offsets,
nobj * sizeof(*p->large_offsets));
}
return p;
err:
free(p->large_offsets);
free(p->offsets);
free(p->crc32);
free(p->sorted_ids);
free(p);
return NULL;
}
static void
cache_packidx(struct got_packidx_v2_hdr *packidx, struct got_repository *repo)
{
int i;
for (i = 0; i < nitems(repo->packidx_cache); i++) {
if (repo->packidx_cache[i] == NULL)
break;
}
if (i == nitems(repo->packidx_cache)) {
got_packidx_close(repo->packidx_cache[i - 1]);
memmove(&repo->packidx_cache[1], &repo->packidx_cache[0],
sizeof(repo->packidx_cache) -
sizeof(repo->packidx_cache[0]));
i = 0;
}
repo->packidx_cache[i] = dup_packidx(packidx);
}
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;
int i;
/* Search pack index cache. */
for (i = 0; i < nitems(repo->packidx_cache); i++) {
if (repo->packidx_cache[i] == NULL)
break;
*idx = get_object_idx(repo->packidx_cache[i], id, repo);
if (*idx != -1) {
*packidx = repo->packidx_cache[i];
return NULL;
}
}
/* No luck. Search the filesystem. */
path_packdir = got_repo_get_path_objects_pack(repo);
if (path_packdir == NULL)
return got_error_from_errno();
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_from_errno();
goto done;
}
err = got_packidx_open(packidx, path_packidx);
free(path_packidx);
if (err)
goto done;
*idx = get_object_idx(*packidx, id, repo);
if (*idx != -1) {
err = NULL; /* found the object */
cache_packidx(*packidx, repo);
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;
path_packdir = got_repo_get_path_objects_pack(repo);
if (path_packdir == NULL)
return got_error_from_errno();
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_from_errno();
}
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, const char *path_packfile,
struct got_repository *repo, struct got_packidx_v2_hdr *packidx)
{
const struct got_error *err = NULL;
*packfile = NULL;
*packfile = fopen(path_packfile, "rb");
if (*packfile == NULL) {
err = got_error_from_errno();
return err;
}
if (packidx) {
err = read_packfile_hdr(*packfile, packidx);
if (err) {
fclose(*packfile);
*packfile = NULL;
}
}
return err;
}
void
got_pack_close(struct got_pack *pack)
{
fclose(pack->packfile);
pack->packfile = NULL;
free(pack->path_packfile);
pack->path_packfile = NULL;
pack->filesize = 0;
}
static const struct got_error *
cache_pack(struct got_pack **packp, const char *path_packfile,
struct got_packidx_v2_hdr *packidx, struct got_repository *repo)
{
const struct got_error *err = NULL;
struct got_pack *pack = NULL;
int i;
if (packp)
*packp = NULL;
for (i = 0; i < nitems(repo->packs); i++) {
pack = &repo->packs[i];
if (pack->path_packfile == NULL)
break;
if (strcmp(pack->path_packfile, path_packfile) == 0)
return NULL;
}
if (i == nitems(repo->packs) - 1) {
got_pack_close(&repo->packs[i - 1]);
memmove(&repo->packs[1], &repo->packs[0],
sizeof(repo->packs) - sizeof(repo->packs[0]));
i = 0;
}
pack = &repo->packs[i];
pack->path_packfile = strdup(path_packfile);
if (pack->path_packfile == NULL) {
err = got_error_from_errno();
goto done;
}
err = open_packfile(&pack->packfile, path_packfile, repo, packidx);
if (err)
goto done;
err = get_packfile_size(&pack->filesize, path_packfile);
done:
if (err) {
if (pack)
free(pack->path_packfile);
free(pack);
} else if (packp)
*packp = pack;
return err;
}
struct got_pack *
get_cached_pack(const char *path_packfile, struct got_repository *repo)
{
struct got_pack *pack = NULL;
int i;
for (i = 0; i < nitems(repo->packs); i++) {
pack = &repo->packs[i];
if (pack->path_packfile == NULL)
break;
if (strcmp(pack->path_packfile, path_packfile) == 0)
return pack;
}
return NULL;
}
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_from_errno();
(*obj)->path_packfile = strdup(path_packfile);
if ((*obj)->path_packfile == NULL) {
const struct got_error *err = got_error_from_errno();
free(*obj);
*obj = NULL;
return err;
}
(*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, uint8_t *delta_buf, size_t delta_len)
{
struct got_delta *delta;
delta = got_delta_open(path_packfile, delta_offset, tslen,
delta_type, delta_size, delta_data_offset, delta_buf,
delta_len);
if (delta == NULL)
return got_error_from_errno();
/* 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;
uint8_t *delta_buf;
size_t delta_len;
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 = got_inflate_to_mem(&delta_buf, &delta_len, packfile);
if (err)
return err;
err = add_delta(deltas, path_packfile, delta_offset, tslen,
delta_type, delta_size, delta_data_offset, delta_buf, delta_len);
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;
char *path_base_packfile;
struct got_pack *base_pack;
off_t delta_data_offset;
uint8_t *delta_buf;
size_t delta_len;
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 = got_inflate_to_mem(&delta_buf, &delta_len, packfile);
if (err)
return err;
err = add_delta(deltas, path_packfile, delta_offset, tslen,
delta_type, delta_size, delta_data_offset, delta_buf, delta_len);
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) {
return got_error(GOT_ERR_BAD_PACKIDX);
}
err = get_packfile_path(&path_base_packfile, repo, packidx);
if (err)
return err;
base_pack = get_cached_pack(path_base_packfile, repo);
if (base_pack == NULL) {
err = cache_pack(&base_pack, path_base_packfile, NULL, repo);
if (err)
goto done;
}
if (fseeko(base_pack->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_pack->packfile);
if (err)
goto done;
err = resolve_delta_chain(deltas, repo, base_pack->packfile,
path_base_packfile, base_offset, base_tslen, base_type,
base_size);
done:
free(path_base_packfile);
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, NULL, 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_OBJ_TYPE);
}
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;
int resolved_type;
*obj = calloc(1, sizeof(**obj));
if (*obj == NULL)
return got_error_from_errno();
(*obj)->flags = 0;
(*obj)->hdrlen = 0;
(*obj)->size = 0; /* Not known because deltas aren't applied yet. */
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_from_errno();
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;
struct got_pack *pack;
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 = get_packfile_path(&path_packfile, repo, packidx);
if (err)
return err;
pack = get_cached_pack(path_packfile, repo);
if (pack == NULL) {
err = cache_pack(&pack, path_packfile, packidx, repo);
if (err)
goto done;
}
if (fseeko(pack->packfile, offset, SEEK_SET) != 0) {
err = got_error_from_errno();
goto done;
}
err = parse_object_type_and_size(&type, &size, &tslen, pack->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,
pack->packfile, id, offset, tslen, type, size);
break;
default:
err = got_error(GOT_ERR_OBJ_TYPE);
goto done;
}
done:
free(path_packfile);
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);
if (err)
return err;
err = cache_pack(NULL, (*obj)->path_packfile, packidx, repo);
return err;
}
static const struct got_error *
get_delta_chain_max_size(uint64_t *max_size, struct got_delta_chain *deltas)
{
struct got_delta *delta;
uint64_t base_size = 0, result_size = 0;
*max_size = 0;
SIMPLEQ_FOREACH(delta, &deltas->entries, entry) {
/* 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) {
const struct got_error *err;
err = got_delta_get_sizes(&base_size, &result_size,
delta->delta_buf, delta->delta_len);
if (err)
return err;
} else
base_size = delta->size;
if (base_size > *max_size)
*max_size = base_size;
if (result_size > *max_size)
*max_size = result_size;
}
return NULL;
}
static const struct got_error *
dump_delta_chain_to_file(size_t *result_size, struct got_delta_chain *deltas,
FILE *outfile, struct got_repository *repo)
{
const struct got_error *err = NULL;
struct got_delta *delta;
FILE *base_file = NULL, *accum_file = NULL;
uint8_t *base_buf = NULL, *accum_buf = NULL;
size_t accum_size = 0;
uint64_t max_size;
int n = 0;
*result_size = 0;
if (SIMPLEQ_EMPTY(&deltas->entries))
return got_error(GOT_ERR_BAD_DELTA_CHAIN);
/* We process small enough files entirely in memory for speed. */
err = get_delta_chain_max_size(&max_size, deltas);
if (err)
return err;
if (max_size < GOT_DELTA_RESULT_SIZE_CACHED_MAX) {
accum_buf = malloc(max_size);
if (accum_buf == NULL)
return got_error_from_errno();
} else {
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) {
if (n == 0) {
struct got_pack *pack;
size_t base_len;
/* 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;
}
pack = get_cached_pack(delta->path_packfile, repo);
if (pack == NULL) {
err = got_error(GOT_ERR_BAD_DELTA_CHAIN);
goto done;
}
if (fseeko(pack->packfile, delta->offset + delta->tslen,
SEEK_SET) != 0) {
err = got_error_from_errno();
goto done;
}
if (base_file)
err = got_inflate_to_file(&base_len,
pack->packfile, base_file);
else {
err = got_inflate_to_mem(&base_buf, &base_len,
pack->packfile);
if (base_len < max_size) {
uint8_t *p;
p = reallocarray(base_buf, 1, max_size);
if (p == NULL) {
err = got_error_from_errno();
goto done;
}
base_buf = p;
}
}
if (err)
goto done;
n++;
if (base_file)
rewind(base_file);
continue;
}
if (base_buf) {
err = got_delta_apply_in_mem(base_buf, delta->delta_buf,
delta->delta_len, accum_buf, &accum_size);
n++;
} else {
err = got_delta_apply(base_file, delta->delta_buf,
delta->delta_len,
/* Final delta application writes to output file. */
++n < deltas->nentries ? accum_file : outfile,
&accum_size);
}
if (err)
goto done;
if (n < deltas->nentries) {
/* Accumulated delta becomes the new base. */
if (base_buf) {
uint8_t *tmp = accum_buf;
accum_buf = base_buf;
base_buf = tmp;
} else {
FILE *tmp = accum_file;
accum_file = base_file;
base_file = tmp;
rewind(base_file);
rewind(accum_file);
}
}
}
done:
free(base_buf);
if (accum_buf) {
size_t len = fwrite(accum_buf, 1, accum_size, outfile);
free(accum_buf);
if (len != accum_size)
return got_ferror(outfile, GOT_ERR_IO);
}
if (base_file)
fclose(base_file);
if (accum_file)
fclose(accum_file);
rewind(outfile);
if (err == NULL)
*result_size = accum_size;
return err;
}
static const struct got_error *
dump_delta_chain_to_mem(uint8_t **outbuf, size_t *outlen,
struct got_delta_chain *deltas, struct got_repository *repo)
{
const struct got_error *err = NULL;
struct got_delta *delta;
uint8_t *base_buf = NULL, *accum_buf = NULL;
size_t accum_size;
uint64_t max_size;
int n = 0;
*outbuf = NULL;
*outlen = 0;
if (SIMPLEQ_EMPTY(&deltas->entries))
return got_error(GOT_ERR_BAD_DELTA_CHAIN);
err = get_delta_chain_max_size(&max_size, deltas);
if (err)
return err;
accum_buf = malloc(max_size);
if (accum_buf == NULL)
return got_error_from_errno();
/* Deltas are ordered in ascending order. */
SIMPLEQ_FOREACH(delta, &deltas->entries, entry) {
if (n == 0) {
struct got_pack *pack;
size_t base_len;
/* 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;
}
pack = get_cached_pack(delta->path_packfile, repo);
if (pack == NULL) {
err = got_error(GOT_ERR_BAD_DELTA_CHAIN);
goto done;
}
if (fseeko(pack->packfile, delta->offset + delta->tslen,
SEEK_SET) != 0) {
err = got_error_from_errno();
goto done;
}
err = got_inflate_to_mem(&base_buf, &base_len,
pack->packfile);
if (base_len < max_size) {
uint8_t *p;
p = reallocarray(base_buf, 1, max_size);
if (p == NULL) {
err = got_error_from_errno();
goto done;
}
base_buf = p;
}
if (err)
goto done;
n++;
continue;
}
err = got_delta_apply_in_mem(base_buf, delta->delta_buf,
delta->delta_len, accum_buf, &accum_size);
n++;
if (err)
goto done;
if (n < deltas->nentries) {
/* Accumulated delta becomes the new base. */
uint8_t *tmp = accum_buf;
accum_buf = base_buf;
base_buf = tmp;
}
}
done:
free(base_buf);
if (err) {
free(accum_buf);
*outbuf = NULL;
*outlen = 0;
} else {
*outbuf = accum_buf;
*outlen = accum_size;
}
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;
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) {
struct got_pack *pack;
pack = get_cached_pack(obj->path_packfile, repo);
if (pack == NULL) {
err = cache_pack(&pack, obj->path_packfile, NULL, repo);
if (err)
goto done;
}
if (fseeko(pack->packfile, obj->pack_offset, SEEK_SET) != 0) {
err = got_error_from_errno();
goto done;
}
err = got_inflate_to_file(&obj->size, pack->packfile, *f);
} else
err = dump_delta_chain_to_file(&obj->size, &obj->deltas, *f,
repo);
done:
if (err && *f) {
fclose(*f);
*f = NULL;
}
return err;
}
const struct got_error *
got_packfile_extract_object_to_mem(uint8_t **buf, size_t *len,
struct got_object *obj, struct got_repository *repo)
{
const struct got_error *err = NULL;
if ((obj->flags & GOT_OBJ_FLAG_PACKED) == 0)
return got_error(GOT_ERR_OBJ_NOT_PACKED);
if ((obj->flags & GOT_OBJ_FLAG_DELTIFIED) == 0) {
struct got_pack *pack;
pack = get_cached_pack(obj->path_packfile, repo);
if (pack == NULL) {
err = cache_pack(&pack, obj->path_packfile, NULL, repo);
if (err)
goto done;
}
if (fseeko(pack->packfile, obj->pack_offset, SEEK_SET) != 0) {
err = got_error_from_errno();
goto done;
}
err = got_inflate_to_mem(buf, len, pack->packfile);
} else
err = dump_delta_chain_to_mem(buf, len, &obj->deltas, repo);
done:
return err;
}