eliminate got_object_open() round-trip when opening commits
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
diff --git a/lib/got_lib_object.h b/lib/got_lib_object.h
index 14bd628..c3257cf 100644
--- a/lib/got_lib_object.h
+++ b/lib/got_lib_object.h
@@ -37,6 +37,7 @@ struct got_object {
};
struct got_commit_object {
+ struct got_object *obj;
struct got_object_id *tree_id;
unsigned int nparents;
struct got_object_id_queue parent_ids;
diff --git a/lib/got_lib_object_parse.h b/lib/got_lib_object_parse.h
index dcc7620..5c3980f 100644
--- a/lib/got_lib_object_parse.h
+++ b/lib/got_lib_object_parse.h
@@ -22,8 +22,7 @@ const struct got_error *got_object_read_header_privsep(struct got_object**,
const struct got_error *got_object_read_blob_privsep(size_t *, int, int,
struct got_repository *repo);
const struct got_error *got_object_read_commit_privsep(
- struct got_commit_object **, struct got_object *, int,
- struct got_repository *);
+ struct got_commit_object **, int, struct got_repository *);
const struct got_error *got_object_read_tree_privsep(struct got_tree_object **,
struct got_object *, int, struct got_repository *);
const struct got_error *got_object_read_tag_privsep(struct got_tag_object **,
@@ -45,9 +44,12 @@ struct got_packidx;
const struct got_error *got_object_packed_read_privsep(struct got_object **,
struct got_repository *, struct got_pack *, struct got_packidx *, int,
struct got_object_id *);
-const struct got_error *got_object_read_packed_commit_privsep(
- struct got_commit_object **, struct got_object *, struct got_pack *);
+const struct got_error *
+got_object_read_packed_commit_privsep(struct got_commit_object **,
+ struct got_pack *, struct got_packidx *, int, struct got_object_id *);
const struct got_error *got_object_read_packed_tree_privsep(
struct got_tree_object **, struct got_object *, struct got_pack *);
const struct got_error *got_object_read_packed_tag_privsep(
struct got_tag_object **, struct got_object *, struct got_pack *);
+const struct got_error *got_object_parse_header(struct got_object **, char *, size_t);
+const struct got_error *got_object_read_header(struct got_object **, int);
diff --git a/lib/got_lib_privsep.h b/lib/got_lib_privsep.h
index 871454f..6f054bf 100644
--- a/lib/got_lib_privsep.h
+++ b/lib/got_lib_privsep.h
@@ -111,8 +111,7 @@ struct got_imsg_error {
} __attribute__((__packed__));
/*
- * Structure for GOT_IMSG_TREE_REQUEST, GOT_IMSG_COMMIT_REQUEST,
- * and GOT_IMSG_OBJECT data.
+ * Structure for GOT_IMSG_TREE_REQUEST and GOT_IMSG_OBJECT data.
*/
struct got_imsg_object {
uint8_t id[SHA1_DIGEST_LENGTH];
@@ -219,6 +218,8 @@ const struct got_error *got_privsep_recv_imsg(struct imsg *, struct imsgbuf *,
void got_privsep_send_error(struct imsgbuf *, const struct got_error *);
const struct got_error *got_privsep_send_obj_req(struct imsgbuf *, int,
struct got_object *);
+const struct got_error *got_privsep_send_commit_req(struct imsgbuf *, int,
+ struct got_object_id *, int);
const struct got_error *got_privsep_send_blob_req(struct imsgbuf *, int);
const struct got_error *got_privsep_send_blob_outfd(struct imsgbuf *, int);
const struct got_error *got_privsep_send_tmpfd(struct imsgbuf *, int);
diff --git a/lib/object.c b/lib/object.c
index 5ca1099..41e5235 100644
--- a/lib/object.c
+++ b/lib/object.c
@@ -296,12 +296,15 @@ got_object_resolve_id_str(struct got_object_id **id,
static const struct got_error *
open_commit(struct got_commit_object **commit,
- struct got_repository *repo, struct got_object *obj, int check_cache)
+ struct got_repository *repo, struct got_object_id *id, int check_cache)
{
const struct got_error *err = NULL;
+ struct got_packidx *packidx = NULL;
+ int idx;
+ char *path_packfile;
if (check_cache) {
- *commit = got_repo_get_cached_commit(repo, &obj->id);
+ *commit = got_repo_get_cached_commit(repo, id);
if (*commit != NULL) {
(*commit)->refcnt++;
return NULL;
@@ -309,31 +312,36 @@ open_commit(struct got_commit_object **commit,
} else
*commit = NULL;
- if (obj->type != GOT_OBJ_TYPE_COMMIT)
- return got_error(GOT_ERR_OBJ_TYPE);
+ err = got_repo_search_packidx(&packidx, &idx, repo, id);
+ if (err == NULL) {
+ struct got_pack *pack = NULL;
- if (obj->flags & GOT_OBJ_FLAG_PACKED) {
- struct got_pack *pack;
- pack = got_repo_get_cached_pack(repo, obj->path_packfile);
+ err = get_packfile_path(&path_packfile, packidx);
+ if (err)
+ return err;
+
+ pack = got_repo_get_cached_pack(repo, path_packfile);
if (pack == NULL) {
- err = got_repo_cache_pack(&pack, repo,
- obj->path_packfile, NULL);
+ err = got_repo_cache_pack(&pack, repo, path_packfile,
+ packidx);
if (err)
return err;
}
- err = got_object_read_packed_commit_privsep(commit, obj, pack);
- } else {
+ err = got_object_read_packed_commit_privsep(commit, pack,
+ packidx, idx, id);
+ } else if (err->code == GOT_ERR_NO_OBJ) {
int fd;
- err = open_loose_object(&fd, got_object_get_id(obj), repo);
+
+ err = open_loose_object(&fd, id, repo);
if (err)
return err;
- err = got_object_read_commit_privsep(commit, obj, fd, repo);
+ err = got_object_read_commit_privsep(commit, fd, repo);
close(fd);
}
if (err == NULL) {
(*commit)->refcnt++;
- err = got_repo_cache_commit(repo, &obj->id, *commit);
+ err = got_repo_cache_commit(repo, id, *commit);
}
return err;
@@ -343,34 +351,20 @@ const struct got_error *
got_object_open_as_commit(struct got_commit_object **commit,
struct got_repository *repo, struct got_object_id *id)
{
- const struct got_error *err;
- struct got_object *obj;
-
*commit = got_repo_get_cached_commit(repo, id);
if (*commit != NULL) {
(*commit)->refcnt++;
return NULL;
}
- err = got_object_open(&obj, repo, id);
- if (err)
- return err;
- if (obj->type != GOT_OBJ_TYPE_COMMIT) {
- err = got_error(GOT_ERR_OBJ_TYPE);
- goto done;
- }
-
- err = open_commit(commit, repo, obj, 0);
-done:
- got_object_close(obj);
- return err;
+ return open_commit(commit, repo, id, 0);
}
const struct got_error *
got_object_commit_open(struct got_commit_object **commit,
struct got_repository *repo, struct got_object *obj)
{
- return open_commit(commit, repo, obj, 1);
+ return open_commit(commit, repo, got_object_get_id(obj), 1);
}
const struct got_error *
@@ -1177,44 +1171,133 @@ done:
}
static const struct got_error *
+receive_commit(struct got_commit_object **commit, struct imsgbuf *ibuf)
+{
+ const struct got_error *err = NULL;
+ struct got_object *obj;
+
+ err = got_privsep_recv_obj(&obj, ibuf);
+ if (err)
+ return err;
+
+ err = got_privsep_recv_commit(commit, ibuf);
+ if (err)
+ got_object_close(obj);
+ else
+ (*commit)->obj = obj; /* XXX should be embedded */
+
+ return err;
+}
+
+static const struct got_error *
request_commit(struct got_commit_object **commit, struct got_repository *repo,
- struct got_object *obj, int fd)
+ int fd)
{
const struct got_error *err = NULL;
struct imsgbuf *ibuf;
ibuf = repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_COMMIT].ibuf;
- err = got_privsep_send_obj_req(ibuf, fd, obj);
+ err = got_privsep_send_commit_req(ibuf, fd, NULL, -1);
+ if (err)
+ return err;
+
+ return receive_commit(commit, ibuf);
+}
+
+static const struct got_error *
+request_packed_commit(struct got_commit_object **commit, struct got_pack *pack,
+ int pack_idx, struct got_object_id *id)
+{
+ const struct got_error *err = NULL;
+
+ err = got_privsep_send_commit_req(pack->privsep_child->ibuf, -1, id,
+ pack_idx);
if (err)
return err;
- return got_privsep_recv_commit(commit, ibuf);
+ return receive_commit(commit, pack->privsep_child->ibuf);
}
const struct got_error *
got_object_read_packed_commit_privsep(struct got_commit_object **commit,
- struct got_object *obj, struct got_pack *pack)
+ struct got_pack *pack, struct got_packidx *packidx, int idx,
+ struct got_object_id *id)
{
const struct got_error *err = NULL;
+ int imsg_fds[2];
+ pid_t pid;
+ struct imsgbuf *ibuf;
- err = got_privsep_send_obj_req(pack->privsep_child->ibuf, -1, obj);
- if (err)
+ if (pack->privsep_child)
+ return request_packed_commit(commit, pack, idx, id);
+
+ ibuf = calloc(1, sizeof(*ibuf));
+ if (ibuf == NULL)
+ return got_error_from_errno();
+
+ pack->privsep_child = calloc(1, sizeof(*pack->privsep_child));
+ if (pack->privsep_child == NULL) {
+ err = got_error_from_errno();
+ free(ibuf);
+ return err;
+ }
+
+ if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_fds) == -1) {
+ err = got_error_from_errno();
+ goto done;
+ }
+
+ pid = fork();
+ if (pid == -1) {
+ err = got_error_from_errno();
+ goto done;
+ } else if (pid == 0) {
+ exec_privsep_child(imsg_fds, GOT_PATH_PROG_READ_PACK,
+ pack->path_packfile);
+ /* not reached */
+ }
+
+ close(imsg_fds[1]);
+ pack->privsep_child->imsg_fd = imsg_fds[0];
+ pack->privsep_child->pid = pid;
+ imsg_init(ibuf, imsg_fds[0]);
+ pack->privsep_child->ibuf = ibuf;
+
+ err = got_privsep_init_pack_child(ibuf, pack, packidx);
+ if (err) {
+ const struct got_error *child_err;
+ err = got_privsep_send_stop(pack->privsep_child->imsg_fd);
+ child_err = got_privsep_wait_for_child(
+ pack->privsep_child->pid);
+ if (child_err && err == NULL)
+ err = child_err;
+ free(ibuf);
+ free(pack->privsep_child);
+ pack->privsep_child = NULL;
return err;
+ }
- return got_privsep_recv_commit(commit, pack->privsep_child->ibuf);
+done:
+ if (err) {
+ free(ibuf);
+ free(pack->privsep_child);
+ pack->privsep_child = NULL;
+ } else
+ err = request_packed_commit(commit, pack, idx, id);
+ return err;
}
const struct got_error *
got_object_read_commit_privsep(struct got_commit_object **commit,
- struct got_object *obj, int obj_fd, struct got_repository *repo)
+ int obj_fd, struct got_repository *repo)
{
int imsg_fds[2];
pid_t pid;
struct imsgbuf *ibuf;
if (repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_COMMIT].imsg_fd != -1)
- return request_commit(commit, repo, obj, obj_fd);
+ return request_commit(commit, repo, obj_fd);
ibuf = calloc(1, sizeof(*ibuf));
if (ibuf == NULL)
@@ -1239,7 +1322,7 @@ got_object_read_commit_privsep(struct got_commit_object **commit,
imsg_init(ibuf, imsg_fds[0]);
repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_COMMIT].ibuf = ibuf;
- return request_commit(commit, repo, obj, obj_fd);
+ return request_commit(commit, repo, obj_fd);
}
static const struct got_error *
diff --git a/lib/object_parse.c b/lib/object_parse.c
index 21d3471..b98b178 100644
--- a/lib/object_parse.c
+++ b/lib/object_parse.c
@@ -142,6 +142,111 @@ got_object_qid_free(struct got_object_qid *qid)
free(qid);
}
+const struct got_error *
+got_object_parse_header(struct got_object **obj, char *buf, size_t len)
+{
+ const char *obj_tags[] = {
+ GOT_OBJ_TAG_COMMIT,
+ GOT_OBJ_TAG_TREE,
+ GOT_OBJ_TAG_BLOB,
+ GOT_OBJ_TAG_TAG,
+ };
+ const int obj_types[] = {
+ GOT_OBJ_TYPE_COMMIT,
+ GOT_OBJ_TYPE_TREE,
+ GOT_OBJ_TYPE_BLOB,
+ GOT_OBJ_TYPE_TAG,
+ };
+ int type = 0;
+ size_t size = 0, hdrlen = 0;
+ int i;
+ char *p = strchr(buf, '\0');
+
+ *obj = NULL;
+
+ if (p == NULL)
+ return got_error(GOT_ERR_BAD_OBJ_HDR);
+
+ hdrlen = strlen(buf) + 1 /* '\0' */;
+
+ for (i = 0; i < nitems(obj_tags); i++) {
+ const char *tag = obj_tags[i];
+ size_t tlen = strlen(tag);
+ const char *errstr;
+
+ if (strncmp(buf, tag, tlen) != 0)
+ continue;
+
+ type = obj_types[i];
+ if (len <= tlen)
+ return got_error(GOT_ERR_BAD_OBJ_HDR);
+ size = strtonum(buf + tlen, 0, LONG_MAX, &errstr);
+ if (errstr != NULL)
+ return got_error(GOT_ERR_BAD_OBJ_HDR);
+ break;
+ }
+
+ if (type == 0)
+ return got_error(GOT_ERR_BAD_OBJ_HDR);
+
+ *obj = calloc(1, sizeof(**obj));
+ if (*obj == NULL)
+ return got_error_from_errno();
+ (*obj)->type = type;
+ (*obj)->hdrlen = hdrlen;
+ (*obj)->size = size;
+ return NULL;
+}
+
+const struct got_error *
+got_object_read_header(struct got_object **obj, int fd)
+{
+ const struct got_error *err;
+ struct got_zstream_buf zb;
+ char *buf;
+ const size_t zbsize = 64;
+ size_t outlen, totlen;
+ int nbuf = 1;
+
+ *obj = NULL;
+
+ buf = malloc(zbsize);
+ if (buf == NULL)
+ return got_error_from_errno();
+
+ err = got_inflate_init(&zb, buf, zbsize);
+ if (err)
+ return err;
+
+ totlen = 0;
+ do {
+ err = got_inflate_read_fd(&zb, fd, &outlen);
+ if (err)
+ goto done;
+ if (outlen == 0)
+ break;
+ totlen += outlen;
+ if (strchr(zb.outbuf, '\0') == NULL) {
+ char *newbuf;
+ nbuf++;
+ newbuf = recallocarray(buf, nbuf - 1, nbuf, zbsize);
+ if (newbuf == NULL) {
+ err = got_error_from_errno();
+ goto done;
+ }
+ buf = newbuf;
+ zb.outbuf = newbuf + totlen;
+ zb.outlen = (nbuf * zbsize) - totlen;
+ }
+ } while (strchr(zb.outbuf, '\0') == NULL);
+
+ err = got_object_parse_header(obj, buf, totlen);
+done:
+ free(buf);
+ got_inflate_end(&zb);
+ return err;
+}
+
struct got_commit_object *
got_object_commit_alloc_partial(void)
{
@@ -272,6 +377,7 @@ got_object_commit_close(struct got_commit_object *commit)
free(commit->author);
free(commit->committer);
free(commit->logmsg);
+ got_object_close(commit->obj);
free(commit);
}
diff --git a/lib/privsep.c b/lib/privsep.c
index d95f501..2a15c7e 100644
--- a/lib/privsep.c
+++ b/lib/privsep.c
@@ -233,7 +233,7 @@ got_privsep_send_obj_req(struct imsgbuf *ibuf, int fd, struct got_object *obj)
imsg_code = GOT_IMSG_TREE_REQUEST;
break;
case GOT_OBJ_TYPE_COMMIT:
- imsg_code = GOT_IMSG_COMMIT_REQUEST;
+ abort(); /* should not get here */
break;
case GOT_OBJ_TYPE_BLOB:
imsg_code = GOT_IMSG_BLOB_REQUEST;
@@ -266,6 +266,30 @@ got_privsep_send_obj_req(struct imsgbuf *ibuf, int fd, struct got_object *obj)
}
const struct got_error *
+got_privsep_send_commit_req(struct imsgbuf *ibuf, int fd,
+ struct got_object_id *id, int pack_idx)
+{
+ struct got_imsg_packed_object iobj, *iobjp;
+ size_t len;
+
+ if (id) { /* commit is packed */
+ iobj.idx = pack_idx;
+ memcpy(iobj.id, id->sha1, sizeof(iobj.id));
+ iobjp = &iobj;
+ len = sizeof(iobj);
+ } else {
+ iobjp = NULL;
+ len = 0;
+ }
+
+ if (imsg_compose(ibuf, GOT_IMSG_COMMIT_REQUEST, 0, 0, fd, iobjp, len)
+ == -1)
+ return got_error_from_errno();
+
+ return flush_imsg(ibuf);
+}
+
+const struct got_error *
got_privsep_send_blob_req(struct imsgbuf *ibuf, int infd)
{
if (imsg_compose(ibuf, GOT_IMSG_BLOB_REQUEST, 0, 0, infd, NULL, 0)
diff --git a/libexec/got-read-commit/got-read-commit.c b/libexec/got-read-commit/got-read-commit.c
index 9ebca90..eeafefc 100644
--- a/libexec/got-read-commit/got-read-commit.c
+++ b/libexec/got-read-commit/got-read-commit.c
@@ -48,17 +48,18 @@ catch_sigint(int signo)
}
static const struct got_error *
-read_commit_object(struct got_commit_object **commit, struct got_object *obj,
- FILE *f)
+read_commit_object(struct got_commit_object **commit, FILE *f)
{
+ struct got_object *obj;
const struct got_error *err = NULL;
size_t len;
uint8_t *p;
- if (obj->flags & GOT_OBJ_FLAG_PACKED)
- err = got_read_file_to_mem(&p, &len, f);
- else
- err = got_inflate_to_mem(&p, &len, f);
+ err = got_inflate_to_mem(&p, &len, f);
+ if (err)
+ return err;
+
+ err = got_object_parse_header(&obj, p, len);
if (err)
return err;
@@ -67,11 +68,21 @@ read_commit_object(struct got_commit_object **commit, struct got_object *obj,
goto done;
}
+ if (obj->type != GOT_OBJ_TYPE_COMMIT) {
+ err = got_error(GOT_ERR_OBJ_TYPE);
+ goto done;
+ }
+
/* Skip object header. */
len -= obj->hdrlen;
err = got_object_parse_commit(commit, p + obj->hdrlen, len);
- free(p);
done:
+ free(p);
+ if (err)
+ got_object_close(obj);
+ else
+ (*commit)->obj = obj; /* XXX should be embedded in struct */
+
return err;
}
@@ -79,9 +90,7 @@ int
main(int argc, char *argv[])
{
const struct got_error *err = NULL;
- struct got_commit_object *commit = NULL;
struct imsgbuf ibuf;
- size_t datalen;
signal(SIGINT, catch_sigint);
@@ -98,9 +107,8 @@ main(int argc, char *argv[])
while (1) {
struct imsg imsg;
- struct got_imsg_object iobj;
FILE *f = NULL;
- struct got_object *obj = NULL;
+ struct got_commit_object *commit = NULL;
if (sigint_received) {
err = got_error(GOT_ERR_CANCELLED);
@@ -122,32 +130,11 @@ main(int argc, char *argv[])
goto done;
}
- datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
- if (datalen != sizeof(iobj)) {
- err = got_error(GOT_ERR_PRIVSEP_LEN);
- goto done;
- }
-
- memcpy(&iobj, imsg.data, sizeof(iobj));
- if (iobj.type != GOT_OBJ_TYPE_COMMIT) {
- err = got_error(GOT_ERR_OBJ_TYPE);
- goto done;
- }
-
if (imsg.fd == -1) {
err = got_error(GOT_ERR_PRIVSEP_NO_FD);
goto done;
}
- obj = calloc(1, sizeof(*obj));
- if (obj == NULL) {
- err = got_error_from_errno();
- goto done;
- }
- obj->type = iobj.type;
- obj->hdrlen = iobj.hdrlen;
- obj->size = iobj.size;
-
/* Always assume file offset zero. */
f = fdopen(imsg.fd, "rb");
if (f == NULL) {
@@ -155,10 +142,17 @@ main(int argc, char *argv[])
goto done;
}
- err = read_commit_object(&commit, obj, f);
+ err = read_commit_object(&commit, f);
+ if (err)
+ goto done;
+
+ /* XXX This flushes the pipe, should only fill it instead. */
+ err = got_privsep_send_obj(&ibuf, commit->obj);
if (err)
goto done;
+ /* XXX Assumes full imsg buf size, should take obj into
+ * account when above flush is removed. */
err = got_privsep_send_commit(&ibuf, commit);
done:
if (f)
@@ -166,8 +160,6 @@ done:
else if (imsg.fd != -1)
close(imsg.fd);
imsg_free(&imsg);
- if (obj)
- got_object_close(obj);
if (err)
break;
}
diff --git a/libexec/got-read-object/got-read-object.c b/libexec/got-read-object/got-read-object.c
index ae3722c..e633934 100644
--- a/libexec/got-read-object/got-read-object.c
+++ b/libexec/got-read-object/got-read-object.c
@@ -36,6 +36,7 @@
#include "got_lib_delta.h"
#include "got_lib_inflate.h"
#include "got_lib_object.h"
+#include "got_lib_object_parse.h"
#include "got_lib_privsep.h"
#ifndef nitems
@@ -55,112 +56,6 @@ catch_sigint(int signo)
sigint_received = 1;
}
-static const struct got_error *
-parse_object_header(struct got_object **obj, char *buf, size_t len)
-{
- const char *obj_tags[] = {
- GOT_OBJ_TAG_COMMIT,
- GOT_OBJ_TAG_TREE,
- GOT_OBJ_TAG_BLOB,
- GOT_OBJ_TAG_TAG,
- };
- const int obj_types[] = {
- GOT_OBJ_TYPE_COMMIT,
- GOT_OBJ_TYPE_TREE,
- GOT_OBJ_TYPE_BLOB,
- GOT_OBJ_TYPE_TAG,
- };
- int type = 0;
- size_t size = 0, hdrlen = 0;
- int i;
- char *p = strchr(buf, '\0');
-
- *obj = NULL;
-
- if (p == NULL)
- return got_error(GOT_ERR_BAD_OBJ_HDR);
-
- hdrlen = strlen(buf) + 1 /* '\0' */;
-
- for (i = 0; i < nitems(obj_tags); i++) {
- const char *tag = obj_tags[i];
- size_t tlen = strlen(tag);
- const char *errstr;
-
- if (strncmp(buf, tag, tlen) != 0)
- continue;
-
- type = obj_types[i];
- if (len <= tlen)
- return got_error(GOT_ERR_BAD_OBJ_HDR);
- size = strtonum(buf + tlen, 0, LONG_MAX, &errstr);
- if (errstr != NULL)
- return got_error(GOT_ERR_BAD_OBJ_HDR);
- break;
- }
-
- if (type == 0)
- return got_error(GOT_ERR_BAD_OBJ_HDR);
-
- *obj = calloc(1, sizeof(**obj));
- if (*obj == NULL)
- return got_error_from_errno();
- (*obj)->type = type;
- (*obj)->hdrlen = hdrlen;
- (*obj)->size = size;
- return NULL;
-}
-
-static const struct got_error *
-read_object_header(struct got_object **obj, int fd)
-{
- const struct got_error *err;
- struct got_zstream_buf zb;
- char *buf;
- const size_t zbsize = 64;
- size_t outlen, totlen;
- int nbuf = 1;
-
- *obj = NULL;
-
- buf = malloc(zbsize);
- if (buf == NULL)
- return got_error_from_errno();
-
- err = got_inflate_init(&zb, buf, zbsize);
- if (err)
- return err;
-
- totlen = 0;
- do {
- err = got_inflate_read_fd(&zb, fd, &outlen);
- if (err)
- goto done;
- if (outlen == 0)
- break;
- totlen += outlen;
- if (strchr(zb.outbuf, '\0') == NULL) {
- char *newbuf;
- nbuf++;
- newbuf = recallocarray(buf, nbuf - 1, nbuf, zbsize);
- if (newbuf == NULL) {
- err = got_error_from_errno();
- goto done;
- }
- buf = newbuf;
- zb.outbuf = newbuf + totlen;
- zb.outlen = (nbuf * zbsize) - totlen;
- }
- } while (strchr(zb.outbuf, '\0') == NULL);
-
- err = parse_object_header(obj, buf, totlen);
-done:
- free(buf);
- got_inflate_end(&zb);
- return err;
-}
-
-
int
main(int argc, char *argv[])
{
@@ -210,7 +105,7 @@ main(int argc, char *argv[])
goto done;
}
- err = read_object_header(&obj, imsg.fd);
+ err = got_object_read_header(&obj, imsg.fd);
if (err)
goto done;
diff --git a/libexec/got-read-pack/got-read-pack.c b/libexec/got-read-pack/got-read-pack.c
index e5d58ee..9222733 100644
--- a/libexec/got-read-pack/got-read-pack.c
+++ b/libexec/got-read-pack/got-read-pack.c
@@ -121,13 +121,21 @@ commit_request(struct imsg *imsg, struct imsgbuf *ibuf, struct got_pack *pack,
struct got_packidx *packidx, struct got_object_cache *objcache)
{
const struct got_error *err = NULL;
- struct got_object *obj = NULL;
+ struct got_imsg_packed_object iobj;
+ struct got_object *obj;
struct got_commit_object *commit = NULL;
uint8_t *buf;
size_t len;
+ struct got_object_id id;
+ size_t datalen;
- err = get_object(&obj, imsg, ibuf, pack, packidx, objcache,
- GOT_OBJ_TYPE_COMMIT);
+ datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
+ if (datalen != sizeof(iobj))
+ return got_error(GOT_ERR_PRIVSEP_LEN);
+ memcpy(&iobj, imsg->data, sizeof(iobj));
+ memcpy(id.sha1, iobj.id, SHA1_DIGEST_LENGTH);
+
+ err = got_packfile_open_object(&obj, pack, packidx, iobj.idx, &id);
if (err)
return err;
@@ -138,10 +146,22 @@ commit_request(struct imsg *imsg, struct imsgbuf *ibuf, struct got_pack *pack,
obj->size = len;
err = got_object_parse_commit(&commit, buf, len);
free(buf);
+ if (err) {
+ got_object_close(obj);
+ return err;
+ } else
+ commit->obj = obj; /* XXX should be embedded */
+ /* XXX This flushes the pipe, should only fill it instead. */
+ err = got_privsep_send_obj(ibuf, commit->obj);
+ if (err) {
+ got_object_commit_close(commit);
+ return err;
+ }
+
+ /* XXX Assumes full imsg buf size, should take obj into
+ * account when above flush is removed. */
err = got_privsep_send_commit(ibuf, commit);
- if (obj)
- got_object_close(obj);
got_object_commit_close(commit);
if (err) {
if (err->code == GOT_ERR_PRIVSEP_PIPE)