Hash :
b4152680
Author :
Date :
2020-03-18T16:13:42
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
/*
* Copyright (c) 2019 Ori Bernstein <ori@openbsd.org>
* Copyright (c) 2020 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/queue.h>
#include <sys/stat.h>
#include <sys/syslimits.h>
#include <sys/time.h>
#include <sys/types.h>
#include <sys/uio.h>
#include <stdint.h>
#include <errno.h>
#include <imsg.h>
#include <limits.h>
#include <signal.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include <sha1.h>
#include <fcntl.h>
#include <zlib.h>
#include <err.h>
#include <assert.h>
#include <dirent.h>
#include "got_error.h"
#include "got_object.h"
#include "got_lib_sha1.h"
#include "got_lib_delta.h"
#include "got_lib_inflate.h"
#include "got_lib_object.h"
#include "got_lib_object_parse.h"
#include "got_lib_object_idset.h"
#include "got_lib_privsep.h"
#include "got_lib_pack.h"
#include "got_lib_delta_cache.h"
struct got_indexed_object {
struct got_object_id id;
/*
* Has this object been fully resolved?
* If so, we know its ID, otherwise we don't and 'id' is invalid.
*/
int valid;
/* Offset of type+size field for this object in pack file. */
off_t off;
/* Type+size values parsed from pack file. */
uint8_t type;
uint64_t size;
/* Length of on-disk type+size data. */
size_t tslen;
/* Length of object data following type+size. */
size_t len;
uint32_t crc;
/* For ref deltas. */
struct got_object_id ref_id;
/* For offset deltas. */
off_t base_offset;
size_t base_offsetlen;
};
#define PUTBE32(b, n)\
do{ \
(b)[0] = (n) >> 24; \
(b)[1] = (n) >> 16; \
(b)[2] = (n) >> 8; \
(b)[3] = (n) >> 0; \
} while(0)
#define PUTBE64(b, n)\
do{ \
(b)[0] = (n) >> 56; \
(b)[1] = (n) >> 48; \
(b)[2] = (n) >> 40; \
(b)[3] = (n) >> 32; \
(b)[4] = (n) >> 24; \
(b)[5] = (n) >> 16; \
(b)[6] = (n) >> 8; \
(b)[7] = (n) >> 0; \
} while(0)
static const struct got_error *
get_obj_type_label(const char **label, int obj_type)
{
const struct got_error *err = NULL;
switch (obj_type) {
case GOT_OBJ_TYPE_BLOB:
*label = GOT_OBJ_LABEL_BLOB;
break;
case GOT_OBJ_TYPE_TREE:
*label = GOT_OBJ_LABEL_TREE;
break;
case GOT_OBJ_TYPE_COMMIT:
*label = GOT_OBJ_LABEL_COMMIT;
break;
case GOT_OBJ_TYPE_TAG:
*label = GOT_OBJ_LABEL_TAG;
break;
default:
*label = NULL;
err = got_error(GOT_ERR_OBJ_TYPE);
break;
}
return err;
}
static const struct got_error *
read_packed_object(struct got_pack *pack, struct got_indexed_object *obj)
{
const struct got_error *err = NULL;
SHA1_CTX ctx;
uint8_t *data;
size_t datalen;
ssize_t n;
char *header;
size_t headerlen;
const char *obj_label;
err = got_pack_parse_object_type_and_size(&obj->type, &obj->size, &obj->tslen,
pack, obj->off);
if (err)
return err;
switch (obj->type) {
case GOT_OBJ_TYPE_BLOB:
case GOT_OBJ_TYPE_COMMIT:
case GOT_OBJ_TYPE_TREE:
case GOT_OBJ_TYPE_TAG:
/* XXX TODO reading large objects into memory is bad! */
err = got_inflate_to_mem_fd(&data, &datalen, &obj->len, pack->fd);
if (err)
break;
SHA1Init(&ctx);
err = get_obj_type_label(&obj_label, obj->type);
if (err)
break;
if (asprintf(&header, "%s %lld", obj_label, obj->size) == -1) {
err = got_error_from_errno("asprintf");
free(data);
break;
}
headerlen = strlen(header) + 1;
SHA1Update(&ctx, header, headerlen);
SHA1Update(&ctx, data, datalen);
SHA1Final(obj->id.sha1, &ctx);
free(header);
free(data);
break;
case GOT_OBJ_TYPE_REF_DELTA:
memset(obj->id.sha1, 0xff, SHA1_DIGEST_LENGTH);
n = read(pack->fd, &obj->ref_id.sha1, SHA1_DIGEST_LENGTH);
if (n == -1) {
err = got_error_from_errno("read");
break;
}
if (n < sizeof(obj->id)) {
err = got_error(GOT_ERR_BAD_PACKFILE);
break;
}
err = got_inflate_to_mem_fd(NULL, &datalen, &obj->len, pack->fd);
if (err)
break;
obj->len += SHA1_DIGEST_LENGTH;
break;
case GOT_OBJ_TYPE_OFFSET_DELTA:
memset(obj->id.sha1, 0xff, SHA1_DIGEST_LENGTH);
err = got_pack_parse_offset_delta(&obj->base_offset,
&obj->base_offsetlen, pack, obj->off, obj->tslen);
if (err)
break;
err = got_inflate_to_mem_fd(NULL, &datalen, &obj->len, pack->fd);
if (err)
break;
obj->len += obj->base_offsetlen;
break;
default:
err = got_error(GOT_ERR_OBJ_TYPE);
break;
}
return err;
}
static const struct got_error *
hwrite(int fd, void *buf, int len, SHA1_CTX *ctx)
{
ssize_t w;
SHA1Update(ctx, buf, len);
w = write(fd, buf, len);
if (w == -1)
return got_error_from_errno("write");
if (w != len)
return got_error(GOT_ERR_IO);
return NULL;
}
static const struct got_error *
object_crc(int packfd, struct got_indexed_object *obj)
{
char buf[8096];
size_t n;
ssize_t r;
if (lseek(packfd, obj->off, SEEK_SET) == -1)
return got_error_from_errno("lseek");
obj->crc = crc32(0L, NULL, 0);
for (n = obj->tslen + obj->len; n > 0; n -= r){
r = read(packfd, buf, n > sizeof(buf) ? sizeof(buf) : n);
if (r == -1)
return got_error_from_errno("read");
if (r == 0)
return NULL;
obj->crc = crc32(obj->crc, buf, r);
}
return 0;
}
static const struct got_error *
resolve_deltified_object(struct got_pack *pack, struct got_packidx *packidx,
struct got_indexed_object *obj)
{
const struct got_error *err = NULL;
struct got_delta_chain deltas;
struct got_delta *delta;
uint8_t *buf = NULL;
size_t len;
SHA1_CTX ctx;
char *header;
size_t headerlen;
int base_obj_type;
const char *obj_label;
deltas.nentries = 0;
SIMPLEQ_INIT(&deltas.entries);
err = got_pack_resolve_delta_chain(&deltas, packidx, pack,
obj->off, obj->tslen, obj->type, obj->size,
GOT_DELTA_CHAIN_RECURSION_MAX);
if (err)
goto done;
/* XXX TODO reading large objects into memory is bad! */
err = got_pack_dump_delta_chain_to_mem(&buf, &len, &deltas, pack);
if (err)
goto done;
SHA1Init(&ctx);
err = got_delta_chain_get_base_type(&base_obj_type, &deltas);
if (err)
goto done;
err = get_obj_type_label(&obj_label, base_obj_type);
if (err)
goto done;
if (asprintf(&header, "%s %zd", obj_label, len) == -1) {
err = got_error_from_errno("asprintf");
goto done;
}
headerlen = strlen(header) + 1;
SHA1Update(&ctx, header, headerlen);
SHA1Update(&ctx, buf, len);
SHA1Final(obj->id.sha1, &ctx);
done:
free(buf);
while (!SIMPLEQ_EMPTY(&deltas.entries)) {
delta = SIMPLEQ_FIRST(&deltas.entries);
SIMPLEQ_REMOVE_HEAD(&deltas.entries, entry);
free(delta);
}
return err;
}
/* Determine the slot in the pack index a given object ID should use. */
static int
find_object_idx(struct got_packidx *packidx, uint8_t *sha1)
{
u_int8_t id0 = sha1[0];
uint32_t nindexed = betoh32(packidx->hdr.fanout_table[0xff]);
int left = 0, right = nindexed - 1;
int cmp = 0, i = 0;
if (id0 > 0)
left = betoh32(packidx->hdr.fanout_table[id0 - 1]);
while (left <= right) {
struct got_packidx_object_id *oid;
i = ((left + right) / 2);
oid = &packidx->hdr.sorted_ids[i];
cmp = memcmp(sha1, oid->sha1, SHA1_DIGEST_LENGTH);
if (cmp == 0)
return -1; /* object already indexed */
else if (cmp > 0)
left = i + 1;
else if (cmp < 0)
right = i - 1;
}
return left;
}
#if 0
static void
print_packidx(struct got_packidx *packidx)
{
uint32_t nindexed = betoh32(packidx->hdr.fanout_table[0xff]);
int i;
fprintf(stderr, "object IDs:\n");
for (i = 0; i < nindexed; i++) {
char hex[SHA1_DIGEST_STRING_LENGTH];
got_sha1_digest_to_str(packidx->hdr.sorted_ids[i].sha1,
hex, sizeof(hex));
fprintf(stderr, "%s\n", hex);
}
fprintf(stderr, "\n");
fprintf(stderr, "object offsets:\n");
for (i = 0; i < nindexed; i++) {
uint32_t offset = be32toh(packidx->hdr.offsets[i]);
if (offset & GOT_PACKIDX_OFFSET_VAL_IS_LARGE_IDX) {
int j = offset & GOT_PACKIDX_OFFSET_VAL_MASK;
fprintf(stderr, "%u -> %llu\n", offset,
be64toh(packidx->hdr.large_offsets[j]));
} else
fprintf(stderr, "%u\n", offset);
}
fprintf(stderr, "\n");
fprintf(stderr, "fanout table:");
for (i = 0; i <= 0xff; i++)
fprintf(stderr, " %u", be32toh(packidx->hdr.fanout_table[i]));
fprintf(stderr, "\n");
}
#endif
static void
update_packidx(int *nlarge, struct got_packidx *packidx, int nobj,
struct got_indexed_object *obj)
{
int i, n, idx;
uint32_t nindexed = betoh32(packidx->hdr.fanout_table[0xff]);
idx = find_object_idx(packidx, obj->id.sha1);
if (idx == -1) {
char hex[SHA1_DIGEST_STRING_LENGTH];
got_sha1_digest_to_str(obj->id.sha1, hex, sizeof(hex));
return; /* object already indexed */
}
memmove(&packidx->hdr.sorted_ids[idx + 1],
&packidx->hdr.sorted_ids[idx],
sizeof(struct got_packidx_object_id) * (nindexed - idx));
memmove(&packidx->hdr.offsets[idx + 1], &packidx->hdr.offsets[idx],
sizeof(uint32_t) * (nindexed - idx));
memcpy(packidx->hdr.sorted_ids[idx].sha1, obj->id.sha1,
SHA1_DIGEST_LENGTH);
if (obj->off < GOT_PACKIDX_OFFSET_VAL_IS_LARGE_IDX)
packidx->hdr.offsets[idx] = htobe32(obj->off);
else {
packidx->hdr.offsets[idx] = htobe32(*nlarge |
GOT_PACKIDX_OFFSET_VAL_IS_LARGE_IDX);
packidx->hdr.large_offsets[*nlarge] = htobe64(obj->off);
(*nlarge)++;
}
for (i = obj->id.sha1[0]; i <= 0xff; i++) {
n = be32toh(packidx->hdr.fanout_table[i]);
packidx->hdr.fanout_table[i] = htobe32(n + 1);
}
}
static int
indexed_obj_cmp(const void *pa, const void *pb)
{
struct got_indexed_object *a, *b;
a = *(struct got_indexed_object **)pa;
b = *(struct got_indexed_object **)pb;
return got_object_id_cmp(&a->id, &b->id);
}
static const struct got_error *
index_pack(struct got_pack *pack, int idxfd, uint8_t *pack_hash,
struct imsgbuf *ibuf)
{
const struct got_error *err;
struct got_packfile_hdr hdr;
struct got_packidx packidx;
char buf[8];
int nobj, nvalid, nloose, nlarge = 0, nresolved = 0, i;
struct got_indexed_object **objects = NULL, *obj;
SHA1_CTX ctx;
uint8_t packidx_hash[SHA1_DIGEST_LENGTH];
ssize_t r, w;
int pass;
/* Check pack file header. */
r = read(pack->fd, &hdr, sizeof(hdr));
if (r == -1)
return got_error_from_errno("read");
if (r < sizeof(hdr))
return got_error_msg(GOT_ERR_BAD_PACKFILE,
"short packfile header");
if (hdr.signature != htobe32(GOT_PACKFILE_SIGNATURE))
return got_error_msg(GOT_ERR_BAD_PACKFILE,
"bad packfile signature");
if (hdr.version != htobe32(GOT_PACKFILE_VERSION))
return got_error_msg(GOT_ERR_BAD_PACKFILE,
"bad packfile version");
nobj = betoh32(hdr.nobjects);
if (nobj == 0)
return got_error_msg(GOT_ERR_BAD_PACKFILE,
"bad packfile with zero objects");
/*
* Create an in-memory pack index which will grow as objects
* IDs in the pack file are discovered. Only fields used to
* read deltified objects will be needed by the pack.c library
* code, so setting up just a pack index header is sufficient.
*/
memset(&packidx, 0, sizeof(packidx));
packidx.hdr.magic = malloc(sizeof(uint32_t));
if (packidx.hdr.magic == NULL)
return got_error_from_errno("calloc");
*packidx.hdr.magic = htobe32(GOT_PACKIDX_V2_MAGIC);
packidx.hdr.version = malloc(sizeof(uint32_t));
if (packidx.hdr.version == NULL) {
err = got_error_from_errno("malloc");
goto done;
}
*packidx.hdr.version = htobe32(GOT_PACKIDX_VERSION);
packidx.hdr.fanout_table = calloc(GOT_PACKIDX_V2_FANOUT_TABLE_ITEMS,
sizeof(uint32_t));
if (packidx.hdr.fanout_table == NULL) {
err = got_error_from_errno("calloc");
goto done;
}
packidx.hdr.sorted_ids = calloc(nobj,
sizeof(struct got_packidx_object_id));
if (packidx.hdr.sorted_ids == NULL) {
err = got_error_from_errno("calloc");
goto done;
}
packidx.hdr.offsets = calloc(nobj, sizeof(uint32_t));
if (packidx.hdr.offsets == NULL) {
err = got_error_from_errno("calloc");
goto done;
}
/* Large offsets table is empty for pack files < 2 GB. */
if (pack->filesize >= GOT_PACKIDX_OFFSET_VAL_IS_LARGE_IDX) {
packidx.hdr.large_offsets = calloc(nobj, sizeof(uint64_t));
if (packidx.hdr.large_offsets == NULL) {
err = got_error_from_errno("calloc");
goto done;
}
}
nvalid = 0;
nloose = 0;
objects = calloc(nobj, sizeof(struct got_indexed_object *));
if (objects == NULL)
return got_error_from_errno("calloc");
/*
* First pass: locate all objects and identify un-deltified objects.
*
* When this pass has completed we will know offset, type, size, and
* CRC information for all objects in this pack file. We won't know
* any of the actual object IDs of deltified objects yet since we
* will not yet attempt to combine deltas.
*/
pass = 1;
for (i = 0; i < nobj; i++) {
err = got_privsep_send_index_pack_progress(ibuf, nobj, i + 1,
nloose, 0);
if (err)
goto done;
obj = calloc(1, sizeof(*obj));
if (obj == NULL) {
err = got_error_from_errno("calloc");
goto done;
}
/* Store offset to type+size information for this object. */
obj->off = lseek(pack->fd, 0, SEEK_CUR);
if (obj->off == -1) {
err = got_error_from_errno("lseek");
goto done;
}
err = read_packed_object(pack, obj);
if (err)
goto done;
objects[i] = obj;
err = object_crc(pack->fd, obj);
if (err)
goto done;
if (obj->type == GOT_OBJ_TYPE_BLOB ||
obj->type == GOT_OBJ_TYPE_TREE ||
obj->type == GOT_OBJ_TYPE_COMMIT ||
obj->type == GOT_OBJ_TYPE_TAG) {
objects[i]->valid = 1;
nloose++;
update_packidx(&nlarge, &packidx, nobj, obj);
}
}
nvalid = nloose;
/*
* Second pass: We can now resolve deltas to compute the IDs of
* objects which appear in deltified form. Because deltas can be
* chained this pass may require a couple of iterations until all
* IDs of deltified objects have been discovered.
*/
pass++;
while (nvalid != nobj) {
int n = 0;
for (i = 0; i < nobj; i++) {
if (objects[i]->type != GOT_OBJ_TYPE_REF_DELTA &&
objects[i]->type != GOT_OBJ_TYPE_OFFSET_DELTA)
continue;
if (objects[i]->valid)
continue;
obj = objects[i];
if (lseek(pack->fd, obj->off + obj->tslen, SEEK_SET) == -1) {
err = got_error_from_errno("lseek");
goto done;
}
err = resolve_deltified_object(pack, &packidx, obj);
if (err) {
if (err->code != GOT_ERR_NO_OBJ)
goto done;
/*
* We cannot resolve this object yet because
* a delta base is unknown. Try again later.
*/
continue;
}
objects[i]->valid = 1;
n++;
update_packidx(&nlarge, &packidx, nobj, obj);
err = got_privsep_send_index_pack_progress(ibuf, nobj, nobj,
nloose, nresolved + n);
if (err)
goto done;
}
if (pass++ > 3 && n == 0) {
static char msg[64];
snprintf(msg, sizeof(msg), "could not resolve "
"any of deltas; packfile could be corrupt");
err = got_error_msg(GOT_ERR_BAD_PACKFILE, msg);
goto done;
}
if (nloose + nresolved == nobj) {
static char msg[64];
snprintf(msg, sizeof(msg),
"fix point reached too early: %d/%d/%d", nvalid, nresolved, nobj);
err = got_error_msg(GOT_ERR_BAD_PACKFILE, msg);
goto done;
}
nresolved += n;
nvalid += nresolved;
}
if (nloose + nresolved != nobj) {
static char msg[64];
snprintf(msg, sizeof(msg),
"discovered only %d of %d objects", nloose + nresolved, nobj);
err = got_error_msg(GOT_ERR_BAD_PACKFILE, msg);
goto done;
}
/* We may have seen duplicates. Update our total object count. */
nobj = betoh32(packidx.hdr.fanout_table[0xff]);
SHA1Init(&ctx);
err = hwrite(idxfd, "\xfftOc\x00\x00\x00\x02", 8, &ctx);
if (err)
goto done;
err = hwrite(idxfd, packidx.hdr.fanout_table,
GOT_PACKIDX_V2_FANOUT_TABLE_ITEMS * sizeof(uint32_t), &ctx);
if (err)
goto done;
err = hwrite(idxfd, packidx.hdr.sorted_ids,
nobj * SHA1_DIGEST_LENGTH, &ctx);
if (err)
goto done;
mergesort(objects, nobj, sizeof(struct got_indexed_object *),
indexed_obj_cmp);
for(i = 0; i < nobj; i++){
PUTBE32(buf, objects[i]->crc);
err = hwrite(idxfd, buf, 4, &ctx);
if (err)
goto done;
}
err = hwrite(idxfd, packidx.hdr.offsets, nobj * sizeof(uint32_t), &ctx);
if (err)
goto done;
if (nlarge > 0) {
err = hwrite(idxfd, packidx.hdr.large_offsets,
nlarge * sizeof(uint64_t), &ctx);
if (err)
goto done;
}
err = hwrite(idxfd, pack_hash, SHA1_DIGEST_LENGTH, &ctx);
if (err)
goto done;
SHA1Final(packidx_hash, &ctx);
w = write(idxfd, packidx_hash, sizeof(packidx_hash));
if (w == -1) {
err = got_error_from_errno("write");
goto done;
}
if (w != sizeof(packidx_hash)) {
err = got_error(GOT_ERR_IO);
goto done;
}
done:
free(packidx.hdr.magic);
free(packidx.hdr.version);
free(packidx.hdr.fanout_table);
free(packidx.hdr.sorted_ids);
free(packidx.hdr.offsets);
free(packidx.hdr.large_offsets);
return err;
}
int
main(int argc, char **argv)
{
const struct got_error *err = NULL, *close_err;
struct imsgbuf ibuf;
struct imsg imsg;
int idxfd = -1;
struct got_pack pack;
uint8_t pack_hash[SHA1_DIGEST_LENGTH];
off_t packfile_size;
#if 0
static int attached;
while (!attached)
sleep(1);
#endif
memset(&pack, 0, sizeof(pack));
pack.fd = -1;
pack.delta_cache = got_delta_cache_alloc(100,
GOT_DELTA_RESULT_SIZE_CACHED_MAX);
if (pack.delta_cache == NULL) {
err = got_error_from_errno("got_delta_cache_alloc");
goto done;
}
imsg_init(&ibuf, GOT_IMSG_FD_CHILD);
err = got_privsep_recv_imsg(&imsg, &ibuf, 0);
if (err)
goto done;
if (imsg.hdr.type == GOT_IMSG_STOP)
goto done;
if (imsg.hdr.type != GOT_IMSG_IDXPACK_REQUEST) {
err = got_error(GOT_ERR_PRIVSEP_MSG);
goto done;
}
if (imsg.hdr.len - IMSG_HEADER_SIZE != sizeof(pack_hash)) {
err = got_error(GOT_ERR_PRIVSEP_LEN);
goto done;
}
memcpy(pack_hash, imsg.data, sizeof(pack_hash));
pack.fd = imsg.fd;
err = got_privsep_recv_imsg(&imsg, &ibuf, 0);
if (err)
goto done;
if (imsg.hdr.type == GOT_IMSG_STOP)
goto done;
if (imsg.hdr.type != GOT_IMSG_TMPFD) {
err = got_error(GOT_ERR_PRIVSEP_MSG);
goto done;
}
if (imsg.hdr.len - IMSG_HEADER_SIZE != 0) {
err = got_error(GOT_ERR_PRIVSEP_LEN);
goto done;
}
idxfd = imsg.fd;
if (lseek(pack.fd, 0, SEEK_END) == -1) {
err = got_error_from_errno("lseek");
goto done;
}
packfile_size = lseek(pack.fd, 0, SEEK_CUR);
if (packfile_size == -1) {
err = got_error_from_errno("lseek");
goto done;
}
pack.filesize = packfile_size; /* XXX off_t vs size_t */
if (lseek(pack.fd, 0, SEEK_SET) == -1) {
err = got_error_from_errno("lseek");
goto done;
}
err = index_pack(&pack, idxfd, pack_hash, &ibuf);
done:
close_err = got_pack_close(&pack);
if (close_err && err == NULL)
err = close_err;
if (idxfd != -1 && close(idxfd) == -1 && err == NULL)
err = got_error_from_errno("close");
if (err == NULL)
err = got_privsep_send_index_pack_done(&ibuf);
if (err) {
got_privsep_send_error(&ibuf, err);
fprintf(stderr, "%s: %s\n", getprogname(), err->msg);
got_privsep_send_error(&ibuf, err);
exit(1);
}
exit(0);
}