Blob


1 /*
2 * Copyright (c) 2018, 2019, 2022 Stefan Sperling <stsp@openbsd.org>
3 *
4 * Permission to use, copy, modify, and distribute this software for any
5 * purpose with or without fee is hereby granted, provided that the above
6 * copyright notice and this permission notice appear in all copies.
7 *
8 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
15 */
17 #include <sys/mman.h>
18 #include <sys/queue.h>
19 #include <sys/types.h>
20 #include <sys/stat.h>
21 #include <sys/socket.h>
22 #include <sys/uio.h>
24 #include <errno.h>
25 #include <imsg.h>
26 #include <stdio.h>
27 #include <stdint.h>
28 #include <stdlib.h>
29 #include <string.h>
30 #include <limits.h>
31 #include <unistd.h>
33 #include "got_error.h"
34 #include "got_object.h"
35 #include "got_repository.h"
36 #include "got_opentemp.h"
37 #include "got_path.h"
39 #include "got_lib_delta.h"
40 #include "got_lib_object.h"
41 #include "got_lib_privsep.h"
42 #include "got_lib_object_cache.h"
43 #include "got_lib_pack.h"
44 #include "got_lib_repository.h"
46 static const struct got_error *
47 request_packed_object(struct got_object **obj, struct got_pack *pack, int idx,
48 struct got_object_id *id)
49 {
50 const struct got_error *err = NULL;
51 struct imsgbuf *ibuf = pack->privsep_child->ibuf;
53 err = got_privsep_send_packed_obj_req(ibuf, idx, id);
54 if (err)
55 return err;
57 err = got_privsep_recv_obj(obj, ibuf);
58 if (err)
59 return err;
61 memcpy(&(*obj)->id, id, sizeof((*obj)->id));
63 return NULL;
64 }
66 /* Create temporary files used during delta application. */
67 static const struct got_error *
68 pack_child_send_tempfiles(struct imsgbuf *ibuf, struct got_pack *pack)
69 {
70 const struct got_error *err;
71 int basefd = -1, accumfd = -1;
73 /*
74 * For performance reasons, the child will keep reusing the
75 * same temporary files during every object request.
76 * Opening and closing new files for every object request is
77 * too expensive during operations such as 'gotadmin pack'.
78 */
79 if (pack->child_has_tempfiles)
80 return NULL;
82 basefd = dup(pack->basefd);
83 if (basefd == -1)
84 return got_error_from_errno("dup");
86 accumfd = dup(pack->accumfd);
87 if (accumfd == -1) {
88 err = got_error_from_errno("dup");
89 goto done;
90 }
92 err = got_privsep_send_tmpfd(ibuf, basefd);
93 if (err)
94 goto done;
96 err = got_privsep_send_tmpfd(ibuf, accumfd);
97 done:
98 if (err) {
99 if (basefd != -1)
100 close(basefd);
101 if (accumfd != -1)
102 close(accumfd);
103 } else
104 pack->child_has_tempfiles = 1;
105 return err;
108 static const struct got_error *
109 request_packed_object_raw(uint8_t **outbuf, off_t *size, size_t *hdrlen,
110 int outfd, struct got_pack *pack, int idx, struct got_object_id *id)
112 const struct got_error *err = NULL;
113 struct imsgbuf *ibuf = pack->privsep_child->ibuf;
114 int outfd_child;
116 err = pack_child_send_tempfiles(ibuf, pack);
117 if (err)
118 return err;
120 outfd_child = dup(outfd);
121 if (outfd_child == -1)
122 return got_error_from_errno("dup");
124 err = got_privsep_send_packed_raw_obj_req(ibuf, idx, id);
125 if (err) {
126 close(outfd_child);
127 return err;
130 err = got_privsep_send_raw_obj_outfd(ibuf, outfd_child);
131 if (err)
132 return err;
134 err = got_privsep_recv_raw_obj(outbuf, size, hdrlen, ibuf);
135 if (err)
136 return err;
138 return NULL;
141 static const struct got_error *
142 read_packed_object_privsep(struct got_object **obj,
143 struct got_repository *repo, struct got_pack *pack,
144 struct got_packidx *packidx, int idx, struct got_object_id *id)
146 const struct got_error *err = NULL;
148 if (pack->privsep_child == NULL) {
149 err = got_pack_start_privsep_child(pack, packidx);
150 if (err)
151 return err;
154 return request_packed_object(obj, pack, idx, id);
157 static const struct got_error *
158 read_packed_object_raw_privsep(uint8_t **outbuf, off_t *size, size_t *hdrlen,
159 int outfd, struct got_pack *pack, struct got_packidx *packidx, int idx,
160 struct got_object_id *id)
162 const struct got_error *err = NULL;
164 if (pack->privsep_child == NULL) {
165 err = got_pack_start_privsep_child(pack, packidx);
166 if (err)
167 return err;
170 return request_packed_object_raw(outbuf, size, hdrlen, outfd, pack,
171 idx, id);
174 const struct got_error *
175 got_object_open_packed(struct got_object **obj, struct got_object_id *id,
176 struct got_repository *repo)
178 const struct got_error *err = NULL;
179 struct got_pack *pack = NULL;
180 struct got_packidx *packidx = NULL;
181 int idx;
182 char *path_packfile;
184 err = got_repo_search_packidx(&packidx, &idx, repo, id);
185 if (err)
186 return err;
188 err = got_packidx_get_packfile_path(&path_packfile,
189 packidx->path_packidx);
190 if (err)
191 return err;
193 pack = got_repo_get_cached_pack(repo, path_packfile);
194 if (pack == NULL) {
195 err = got_repo_cache_pack(&pack, repo, path_packfile, packidx);
196 if (err)
197 goto done;
200 err = read_packed_object_privsep(obj, repo, pack, packidx, idx, id);
201 if (err)
202 goto done;
203 done:
204 free(path_packfile);
205 return err;
208 const struct got_error *
209 got_object_open_from_packfile(struct got_object **obj, struct got_object_id *id,
210 struct got_pack *pack, struct got_packidx *packidx, int obj_idx,
211 struct got_repository *repo)
213 return read_packed_object_privsep(obj, repo, pack, packidx,
214 obj_idx, id);
217 const struct got_error *
218 got_object_read_raw_delta(uint64_t *base_size, uint64_t *result_size,
219 off_t *delta_size, off_t *delta_compressed_size, off_t *delta_offset,
220 off_t *delta_out_offset, struct got_object_id **base_id, int delta_cache_fd,
221 struct got_packidx *packidx, int obj_idx, struct got_object_id *id,
222 struct got_repository *repo)
224 const struct got_error *err = NULL;
225 struct got_pack *pack = NULL;
226 char *path_packfile;
228 *base_size = 0;
229 *result_size = 0;
230 *delta_size = 0;
231 *delta_compressed_size = 0;
232 *delta_offset = 0;
233 *delta_out_offset = 0;
235 err = got_packidx_get_packfile_path(&path_packfile,
236 packidx->path_packidx);
237 if (err)
238 return err;
240 pack = got_repo_get_cached_pack(repo, path_packfile);
241 if (pack == NULL) {
242 err = got_repo_cache_pack(&pack, repo, path_packfile, packidx);
243 if (err)
244 return err;
247 if (pack->privsep_child == NULL) {
248 err = got_pack_start_privsep_child(pack, packidx);
249 if (err)
250 return err;
253 if (!pack->child_has_delta_outfd) {
254 int outfd_child;
255 outfd_child = dup(delta_cache_fd);
256 if (outfd_child == -1)
257 return got_error_from_errno("dup");
258 err = got_privsep_send_raw_delta_outfd(
259 pack->privsep_child->ibuf, outfd_child);
260 if (err)
261 return err;
262 pack->child_has_delta_outfd = 1;
265 err = got_privsep_send_raw_delta_req(pack->privsep_child->ibuf,
266 obj_idx, id);
267 if (err)
268 return err;
270 return got_privsep_recv_raw_delta(base_size, result_size, delta_size,
271 delta_compressed_size, delta_offset, delta_out_offset, base_id,
272 pack->privsep_child->ibuf);
275 static const struct got_error *
276 request_object(struct got_object **obj, struct got_object_id *id,
277 struct got_repository *repo, int fd)
279 const struct got_error *err = NULL;
280 struct imsgbuf *ibuf;
282 ibuf = repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_OBJECT].ibuf;
284 err = got_privsep_send_obj_req(ibuf, fd, id);
285 if (err)
286 return err;
288 return got_privsep_recv_obj(obj, ibuf);
291 static const struct got_error *
292 request_raw_object(uint8_t **outbuf, off_t *size, size_t *hdrlen, int outfd,
293 struct got_object_id *id, struct got_repository *repo, int infd)
295 const struct got_error *err = NULL;
296 struct imsgbuf *ibuf;
297 int outfd_child;
299 ibuf = repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_OBJECT].ibuf;
301 outfd_child = dup(outfd);
302 if (outfd_child == -1)
303 return got_error_from_errno("dup");
305 err = got_privsep_send_raw_obj_req(ibuf, infd, id);
306 if (err)
307 return err;
309 err = got_privsep_send_raw_obj_outfd(ibuf, outfd_child);
310 if (err)
311 return err;
313 return got_privsep_recv_raw_obj(outbuf, size, hdrlen, ibuf);
316 static const struct got_error *
317 start_child(struct got_repository *repo, int type)
319 const struct got_error *err = NULL;
320 int imsg_fds[2];
321 pid_t pid;
322 struct imsgbuf *ibuf;
323 const char *prog_path;
325 switch (type) {
326 case GOT_REPO_PRIVSEP_CHILD_OBJECT:
327 prog_path = GOT_PATH_PROG_READ_OBJECT;
328 break;
329 case GOT_REPO_PRIVSEP_CHILD_TREE:
330 prog_path = GOT_PATH_PROG_READ_TREE;
331 break;
332 case GOT_REPO_PRIVSEP_CHILD_COMMIT:
333 prog_path = GOT_PATH_PROG_READ_COMMIT;
334 break;
335 case GOT_REPO_PRIVSEP_CHILD_BLOB:
336 prog_path = GOT_PATH_PROG_READ_BLOB;
337 break;
338 case GOT_REPO_PRIVSEP_CHILD_TAG:
339 prog_path = GOT_PATH_PROG_READ_TAG;
340 break;
341 default:
342 return got_error(GOT_ERR_OBJ_TYPE);
345 ibuf = calloc(1, sizeof(*ibuf));
346 if (ibuf == NULL)
347 return got_error_from_errno("calloc");
349 if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_fds) == -1) {
350 err = got_error_from_errno("socketpair");
351 free(ibuf);
352 return err;
355 pid = fork();
356 if (pid == -1) {
357 err = got_error_from_errno("fork");
358 free(ibuf);
359 return err;
361 else if (pid == 0) {
362 got_privsep_exec_child(imsg_fds, prog_path, repo->path);
363 /* not reached */
366 if (close(imsg_fds[1]) == -1) {
367 err = got_error_from_errno("close");
368 free(ibuf);
369 return err;
372 repo->privsep_children[type].imsg_fd = imsg_fds[0];
373 repo->privsep_children[type].pid = pid;
374 imsg_init(ibuf, imsg_fds[0]);
375 repo->privsep_children[type].ibuf = ibuf;
377 return NULL;
380 const struct got_error *
381 got_object_read_header_privsep(struct got_object **obj,
382 struct got_object_id *id, struct got_repository *repo, int obj_fd)
384 const struct got_error *err;
386 if (repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_OBJECT].imsg_fd != -1)
387 return request_object(obj, id, repo, obj_fd);
389 err = start_child(repo, GOT_REPO_PRIVSEP_CHILD_OBJECT);
390 if (err)
391 return err;
393 return request_object(obj, id, repo, obj_fd);
396 static const struct got_error *
397 read_object_raw_privsep(uint8_t **outbuf, off_t *size, size_t *hdrlen,
398 int outfd, struct got_object_id *id, struct got_repository *repo,
399 int obj_fd)
401 const struct got_error *err;
403 if (repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_OBJECT].imsg_fd != -1)
404 return request_raw_object(outbuf, size, hdrlen, outfd, id,
405 repo, obj_fd);
407 err = start_child(repo, GOT_REPO_PRIVSEP_CHILD_OBJECT);
408 if (err)
409 return err;
411 return request_raw_object(outbuf, size, hdrlen, outfd, id, repo,
412 obj_fd);
415 const struct got_error *
416 got_object_open(struct got_object **obj, struct got_repository *repo,
417 struct got_object_id *id)
419 const struct got_error *err = NULL;
420 int fd;
422 *obj = got_repo_get_cached_object(repo, id);
423 if (*obj != NULL) {
424 (*obj)->refcnt++;
425 return NULL;
428 err = got_object_open_packed(obj, id, repo);
429 if (err && err->code != GOT_ERR_NO_OBJ)
430 return err;
431 if (*obj) {
432 (*obj)->refcnt++;
433 return got_repo_cache_object(repo, id, *obj);
436 err = got_object_open_loose_fd(&fd, id, repo);
437 if (err) {
438 if (err->code == GOT_ERR_ERRNO && errno == ENOENT)
439 err = got_error_no_obj(id);
440 return err;
443 err = got_object_read_header_privsep(obj, id, repo, fd);
444 if (err)
445 return err;
447 memcpy(&(*obj)->id, id, sizeof(*id));
449 (*obj)->refcnt++;
450 return got_repo_cache_object(repo, id, *obj);
453 /* *outfd must be initialized to -1 by caller */
454 const struct got_error *
455 got_object_raw_open(struct got_raw_object **obj, int *outfd,
456 struct got_repository *repo, struct got_object_id *id)
458 const struct got_error *err = NULL;
459 struct got_packidx *packidx = NULL;
460 int idx;
461 uint8_t *outbuf = NULL;
462 off_t size = 0;
463 size_t hdrlen = 0;
464 char *path_packfile = NULL;
466 *obj = got_repo_get_cached_raw_object(repo, id);
467 if (*obj != NULL) {
468 (*obj)->refcnt++;
469 return NULL;
472 if (*outfd == -1) {
473 *outfd = got_opentempfd();
474 if (*outfd == -1)
475 return got_error_from_errno("got_opentempfd");
478 err = got_repo_search_packidx(&packidx, &idx, repo, id);
479 if (err == NULL) {
480 struct got_pack *pack = NULL;
482 err = got_packidx_get_packfile_path(&path_packfile,
483 packidx->path_packidx);
484 if (err)
485 goto done;
487 pack = got_repo_get_cached_pack(repo, path_packfile);
488 if (pack == NULL) {
489 err = got_repo_cache_pack(&pack, repo, path_packfile,
490 packidx);
491 if (err)
492 goto done;
494 err = read_packed_object_raw_privsep(&outbuf, &size, &hdrlen,
495 *outfd, pack, packidx, idx, id);
496 if (err)
497 goto done;
498 } else if (err->code == GOT_ERR_NO_OBJ) {
499 int fd;
501 err = got_object_open_loose_fd(&fd, id, repo);
502 if (err)
503 goto done;
504 err = read_object_raw_privsep(&outbuf, &size, &hdrlen, *outfd,
505 id, repo, fd);
506 if (err)
507 goto done;
510 err = got_object_raw_alloc(obj, outbuf, outfd,
511 GOT_DELTA_RESULT_SIZE_CACHED_MAX, hdrlen, size);
512 if (err)
513 goto done;
515 err = got_repo_cache_raw_object(repo, id, *obj);
516 done:
517 free(path_packfile);
518 if (err) {
519 if (*obj) {
520 got_object_raw_close(*obj);
521 *obj = NULL;
523 free(outbuf);
525 return err;
528 static const struct got_error *
529 request_packed_commit(struct got_commit_object **commit, struct got_pack *pack,
530 int pack_idx, struct got_object_id *id)
532 const struct got_error *err = NULL;
534 err = got_privsep_send_commit_req(pack->privsep_child->ibuf, -1, id,
535 pack_idx);
536 if (err)
537 return err;
539 err = got_privsep_recv_commit(commit, pack->privsep_child->ibuf);
540 if (err)
541 return err;
543 (*commit)->flags |= GOT_COMMIT_FLAG_PACKED;
544 return NULL;
547 static const struct got_error *
548 read_packed_commit_privsep(struct got_commit_object **commit,
549 struct got_pack *pack, struct got_packidx *packidx, int idx,
550 struct got_object_id *id)
552 const struct got_error *err = NULL;
554 if (pack->privsep_child)
555 return request_packed_commit(commit, pack, idx, id);
557 err = got_pack_start_privsep_child(pack, packidx);
558 if (err)
559 return err;
561 return request_packed_commit(commit, pack, idx, id);
564 static const struct got_error *
565 request_commit(struct got_commit_object **commit, struct got_repository *repo,
566 int fd, struct got_object_id *id)
568 const struct got_error *err = NULL;
569 struct imsgbuf *ibuf;
571 ibuf = repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_COMMIT].ibuf;
573 err = got_privsep_send_commit_req(ibuf, fd, id, -1);
574 if (err)
575 return err;
577 return got_privsep_recv_commit(commit, ibuf);
580 static const struct got_error *
581 read_commit_privsep(struct got_commit_object **commit, int obj_fd,
582 struct got_object_id *id, struct got_repository *repo)
584 const struct got_error *err;
586 if (repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_COMMIT].imsg_fd != -1)
587 return request_commit(commit, repo, obj_fd, id);
589 err = start_child(repo, GOT_REPO_PRIVSEP_CHILD_COMMIT);
590 if (err)
591 return err;
593 return request_commit(commit, repo, obj_fd, id);
596 static const struct got_error *
597 open_commit(struct got_commit_object **commit,
598 struct got_repository *repo, struct got_object_id *id, int check_cache)
600 const struct got_error *err = NULL;
601 struct got_packidx *packidx = NULL;
602 int idx;
603 char *path_packfile = NULL;
605 if (check_cache) {
606 *commit = got_repo_get_cached_commit(repo, id);
607 if (*commit != NULL) {
608 (*commit)->refcnt++;
609 return NULL;
611 } else
612 *commit = NULL;
614 err = got_repo_search_packidx(&packidx, &idx, repo, id);
615 if (err == NULL) {
616 struct got_pack *pack = NULL;
618 err = got_packidx_get_packfile_path(&path_packfile,
619 packidx->path_packidx);
620 if (err)
621 return err;
623 pack = got_repo_get_cached_pack(repo, path_packfile);
624 if (pack == NULL) {
625 err = got_repo_cache_pack(&pack, repo, path_packfile,
626 packidx);
627 if (err)
628 goto done;
630 err = read_packed_commit_privsep(commit, pack,
631 packidx, idx, id);
632 } else if (err->code == GOT_ERR_NO_OBJ) {
633 int fd;
635 err = got_object_open_loose_fd(&fd, id, repo);
636 if (err)
637 return err;
638 err = read_commit_privsep(commit, fd, id, repo);
641 if (err == NULL) {
642 (*commit)->refcnt++;
643 err = got_repo_cache_commit(repo, id, *commit);
645 done:
646 free(path_packfile);
647 return err;
650 const struct got_error *
651 got_object_open_as_commit(struct got_commit_object **commit,
652 struct got_repository *repo, struct got_object_id *id)
654 *commit = got_repo_get_cached_commit(repo, id);
655 if (*commit != NULL) {
656 (*commit)->refcnt++;
657 return NULL;
660 return open_commit(commit, repo, id, 0);
663 const struct got_error *
664 got_object_commit_open(struct got_commit_object **commit,
665 struct got_repository *repo, struct got_object *obj)
667 return open_commit(commit, repo, got_object_get_id(obj), 1);
670 static const struct got_error *
671 request_packed_tree(struct got_tree_object **tree, struct got_pack *pack,
672 int pack_idx, struct got_object_id *id)
674 const struct got_error *err = NULL;
676 err = got_privsep_send_tree_req(pack->privsep_child->ibuf, -1, id,
677 pack_idx);
678 if (err)
679 return err;
681 return got_privsep_recv_tree(tree, pack->privsep_child->ibuf);
684 static const struct got_error *
685 read_packed_tree_privsep(struct got_tree_object **tree,
686 struct got_pack *pack, struct got_packidx *packidx, int idx,
687 struct got_object_id *id)
689 const struct got_error *err = NULL;
691 if (pack->privsep_child)
692 return request_packed_tree(tree, pack, idx, id);
694 err = got_pack_start_privsep_child(pack, packidx);
695 if (err)
696 return err;
698 return request_packed_tree(tree, pack, idx, id);
701 static const struct got_error *
702 request_tree(struct got_tree_object **tree, struct got_repository *repo,
703 int fd, struct got_object_id *id)
705 const struct got_error *err = NULL;
706 struct imsgbuf *ibuf;
708 ibuf = repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TREE].ibuf;
710 err = got_privsep_send_tree_req(ibuf, fd, id, -1);
711 if (err)
712 return err;
714 return got_privsep_recv_tree(tree, ibuf);
717 static const struct got_error *
718 read_tree_privsep(struct got_tree_object **tree, int obj_fd,
719 struct got_object_id *id, struct got_repository *repo)
721 const struct got_error *err;
723 if (repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TREE].imsg_fd != -1)
724 return request_tree(tree, repo, obj_fd, id);
726 err = start_child(repo, GOT_REPO_PRIVSEP_CHILD_TREE);
727 if (err)
728 return err;
730 return request_tree(tree, repo, obj_fd, id);
733 static const struct got_error *
734 open_tree(struct got_tree_object **tree, struct got_repository *repo,
735 struct got_object_id *id, int check_cache)
737 const struct got_error *err = NULL;
738 struct got_packidx *packidx = NULL;
739 int idx;
740 char *path_packfile = NULL;
742 if (check_cache) {
743 *tree = got_repo_get_cached_tree(repo, id);
744 if (*tree != NULL) {
745 (*tree)->refcnt++;
746 return NULL;
748 } else
749 *tree = NULL;
751 err = got_repo_search_packidx(&packidx, &idx, repo, id);
752 if (err == NULL) {
753 struct got_pack *pack = NULL;
755 err = got_packidx_get_packfile_path(&path_packfile,
756 packidx->path_packidx);
757 if (err)
758 return err;
760 pack = got_repo_get_cached_pack(repo, path_packfile);
761 if (pack == NULL) {
762 err = got_repo_cache_pack(&pack, repo, path_packfile,
763 packidx);
764 if (err)
765 goto done;
767 err = read_packed_tree_privsep(tree, pack,
768 packidx, idx, id);
769 } else if (err->code == GOT_ERR_NO_OBJ) {
770 int fd;
772 err = got_object_open_loose_fd(&fd, id, repo);
773 if (err)
774 return err;
775 err = read_tree_privsep(tree, fd, id, repo);
778 if (err == NULL) {
779 (*tree)->refcnt++;
780 err = got_repo_cache_tree(repo, id, *tree);
782 done:
783 free(path_packfile);
784 return err;
787 const struct got_error *
788 got_object_open_as_tree(struct got_tree_object **tree,
789 struct got_repository *repo, struct got_object_id *id)
791 *tree = got_repo_get_cached_tree(repo, id);
792 if (*tree != NULL) {
793 (*tree)->refcnt++;
794 return NULL;
797 return open_tree(tree, repo, id, 0);
800 const struct got_error *
801 got_object_tree_open(struct got_tree_object **tree,
802 struct got_repository *repo, struct got_object *obj)
804 return open_tree(tree, repo, got_object_get_id(obj), 1);
807 static const struct got_error *
808 request_packed_blob(uint8_t **outbuf, size_t *size, size_t *hdrlen, int outfd,
809 struct got_pack *pack, struct got_packidx *packidx, int idx,
810 struct got_object_id *id)
812 const struct got_error *err = NULL;
813 struct imsgbuf *ibuf = pack->privsep_child->ibuf;
814 int outfd_child;
816 err = pack_child_send_tempfiles(ibuf, pack);
817 if (err)
818 return err;
820 outfd_child = dup(outfd);
821 if (outfd_child == -1)
822 return got_error_from_errno("dup");
824 err = got_privsep_send_blob_req(pack->privsep_child->ibuf, -1, id, idx);
825 if (err)
826 return err;
828 err = got_privsep_send_blob_outfd(pack->privsep_child->ibuf,
829 outfd_child);
830 if (err) {
831 return err;
834 err = got_privsep_recv_blob(outbuf, size, hdrlen,
835 pack->privsep_child->ibuf);
836 if (err)
837 return err;
839 if (lseek(outfd, SEEK_SET, 0) == -1)
840 err = got_error_from_errno("lseek");
842 return err;
845 static const struct got_error *
846 read_packed_blob_privsep(uint8_t **outbuf, size_t *size, size_t *hdrlen,
847 int outfd, struct got_pack *pack, struct got_packidx *packidx, int idx,
848 struct got_object_id *id)
850 const struct got_error *err = NULL;
852 if (pack->privsep_child == NULL) {
853 err = got_pack_start_privsep_child(pack, packidx);
854 if (err)
855 return err;
858 return request_packed_blob(outbuf, size, hdrlen, outfd, pack, packidx,
859 idx, id);
862 static const struct got_error *
863 request_blob(uint8_t **outbuf, size_t *size, size_t *hdrlen, int outfd,
864 int infd, struct got_object_id *id, struct imsgbuf *ibuf)
866 const struct got_error *err = NULL;
867 int outfd_child;
869 outfd_child = dup(outfd);
870 if (outfd_child == -1)
871 return got_error_from_errno("dup");
873 err = got_privsep_send_blob_req(ibuf, infd, id, -1);
874 if (err)
875 return err;
877 err = got_privsep_send_blob_outfd(ibuf, outfd_child);
878 if (err)
879 return err;
881 err = got_privsep_recv_blob(outbuf, size, hdrlen, ibuf);
882 if (err)
883 return err;
885 if (lseek(outfd, SEEK_SET, 0) == -1)
886 return got_error_from_errno("lseek");
888 return err;
891 static const struct got_error *
892 read_blob_privsep(uint8_t **outbuf, size_t *size, size_t *hdrlen,
893 int outfd, int infd, struct got_object_id *id, struct got_repository *repo)
895 const struct got_error *err;
896 struct imsgbuf *ibuf;
898 if (repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_BLOB].imsg_fd != -1) {
899 ibuf = repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_BLOB].ibuf;
900 return request_blob(outbuf, size, hdrlen, outfd, infd, id,
901 ibuf);
904 err = start_child(repo, GOT_REPO_PRIVSEP_CHILD_BLOB);
905 if (err)
906 return err;
908 ibuf = repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_BLOB].ibuf;
909 return request_blob(outbuf, size, hdrlen, outfd, infd, id, ibuf);
912 static const struct got_error *
913 open_blob(struct got_blob_object **blob, struct got_repository *repo,
914 struct got_object_id *id, size_t blocksize, int outfd)
916 const struct got_error *err = NULL;
917 struct got_packidx *packidx = NULL;
918 int idx, dfd = -1;
919 char *path_packfile = NULL;
920 uint8_t *outbuf;
921 size_t size, hdrlen;
922 struct stat sb;
924 *blob = calloc(1, sizeof(**blob));
925 if (*blob == NULL)
926 return got_error_from_errno("calloc");
928 (*blob)->read_buf = malloc(blocksize);
929 if ((*blob)->read_buf == NULL) {
930 err = got_error_from_errno("malloc");
931 goto done;
934 if (ftruncate(outfd, 0L) == -1) {
935 err = got_error_from_errno("ftruncate");
936 goto done;
938 if (lseek(outfd, SEEK_SET, 0) == -1) {
939 err = got_error_from_errno("lseek");
940 goto done;
943 err = got_repo_search_packidx(&packidx, &idx, repo, id);
944 if (err == NULL) {
945 struct got_pack *pack = NULL;
947 err = got_packidx_get_packfile_path(&path_packfile,
948 packidx->path_packidx);
949 if (err)
950 goto done;
952 pack = got_repo_get_cached_pack(repo, path_packfile);
953 if (pack == NULL) {
954 err = got_repo_cache_pack(&pack, repo, path_packfile,
955 packidx);
956 if (err)
957 goto done;
959 err = read_packed_blob_privsep(&outbuf, &size, &hdrlen, outfd,
960 pack, packidx, idx, id);
961 } else if (err->code == GOT_ERR_NO_OBJ) {
962 int infd;
964 err = got_object_open_loose_fd(&infd, id, repo);
965 if (err)
966 goto done;
967 err = read_blob_privsep(&outbuf, &size, &hdrlen, outfd, infd,
968 id, repo);
970 if (err)
971 goto done;
973 if (hdrlen > size) {
974 err = got_error(GOT_ERR_BAD_OBJ_HDR);
975 goto done;
978 if (outbuf) {
979 (*blob)->f = fmemopen(outbuf, size, "rb");
980 if ((*blob)->f == NULL) {
981 err = got_error_from_errno("fmemopen");
982 free(outbuf);
983 goto done;
985 (*blob)->data = outbuf;
986 } else {
987 if (fstat(outfd, &sb) == -1) {
988 err = got_error_from_errno("fstat");
989 goto done;
992 if (sb.st_size != size) {
993 err = got_error(GOT_ERR_PRIVSEP_LEN);
994 goto done;
997 dfd = dup(outfd);
998 if (dfd == -1) {
999 err = got_error_from_errno("dup");
1000 goto done;
1003 (*blob)->f = fdopen(dfd, "rb");
1004 if ((*blob)->f == NULL) {
1005 err = got_error_from_errno("fdopen");
1006 close(dfd);
1007 dfd = -1;
1008 goto done;
1012 (*blob)->hdrlen = hdrlen;
1013 (*blob)->blocksize = blocksize;
1014 memcpy(&(*blob)->id.sha1, id->sha1, SHA1_DIGEST_LENGTH);
1016 done:
1017 free(path_packfile);
1018 if (err) {
1019 if (*blob) {
1020 got_object_blob_close(*blob);
1021 *blob = NULL;
1024 return err;
1027 const struct got_error *
1028 got_object_open_as_blob(struct got_blob_object **blob,
1029 struct got_repository *repo, struct got_object_id *id, size_t blocksize,
1030 int outfd)
1032 return open_blob(blob, repo, id, blocksize, outfd);
1035 const struct got_error *
1036 got_object_blob_open(struct got_blob_object **blob,
1037 struct got_repository *repo, struct got_object *obj, size_t blocksize,
1038 int outfd)
1040 return open_blob(blob, repo, got_object_get_id(obj), blocksize, outfd);
1043 static const struct got_error *
1044 request_packed_tag(struct got_tag_object **tag, struct got_pack *pack,
1045 int pack_idx, struct got_object_id *id)
1047 const struct got_error *err = NULL;
1049 err = got_privsep_send_tag_req(pack->privsep_child->ibuf, -1, id,
1050 pack_idx);
1051 if (err)
1052 return err;
1054 return got_privsep_recv_tag(tag, pack->privsep_child->ibuf);
1057 static const struct got_error *
1058 read_packed_tag_privsep(struct got_tag_object **tag,
1059 struct got_pack *pack, struct got_packidx *packidx, int idx,
1060 struct got_object_id *id)
1062 const struct got_error *err = NULL;
1064 if (pack->privsep_child)
1065 return request_packed_tag(tag, pack, idx, id);
1067 err = got_pack_start_privsep_child(pack, packidx);
1068 if (err)
1069 return err;
1071 return request_packed_tag(tag, pack, idx, id);
1074 static const struct got_error *
1075 request_tag(struct got_tag_object **tag, struct got_repository *repo,
1076 int fd, struct got_object_id *id)
1078 const struct got_error *err = NULL;
1079 struct imsgbuf *ibuf;
1081 ibuf = repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TAG].ibuf;
1083 err = got_privsep_send_tag_req(ibuf, fd, id, -1);
1084 if (err)
1085 return err;
1087 return got_privsep_recv_tag(tag, ibuf);
1090 static const struct got_error *
1091 read_tag_privsep(struct got_tag_object **tag, int obj_fd,
1092 struct got_object_id *id, struct got_repository *repo)
1094 const struct got_error *err;
1096 if (repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TAG].imsg_fd != -1)
1097 return request_tag(tag, repo, obj_fd, id);
1099 err = start_child(repo, GOT_REPO_PRIVSEP_CHILD_TAG);
1100 if (err)
1101 return err;
1103 return request_tag(tag, repo, obj_fd, id);
1106 static const struct got_error *
1107 open_tag(struct got_tag_object **tag, struct got_repository *repo,
1108 struct got_object_id *id, int check_cache)
1110 const struct got_error *err = NULL;
1111 struct got_packidx *packidx = NULL;
1112 int idx;
1113 char *path_packfile = NULL;
1114 struct got_object *obj = NULL;
1115 int obj_type = GOT_OBJ_TYPE_ANY;
1117 if (check_cache) {
1118 *tag = got_repo_get_cached_tag(repo, id);
1119 if (*tag != NULL) {
1120 (*tag)->refcnt++;
1121 return NULL;
1123 } else
1124 *tag = NULL;
1126 err = got_repo_search_packidx(&packidx, &idx, repo, id);
1127 if (err == NULL) {
1128 struct got_pack *pack = NULL;
1130 err = got_packidx_get_packfile_path(&path_packfile,
1131 packidx->path_packidx);
1132 if (err)
1133 return err;
1135 pack = got_repo_get_cached_pack(repo, path_packfile);
1136 if (pack == NULL) {
1137 err = got_repo_cache_pack(&pack, repo, path_packfile,
1138 packidx);
1139 if (err)
1140 goto done;
1143 /* Beware of "lightweight" tags: Check object type first. */
1144 err = read_packed_object_privsep(&obj, repo, pack, packidx,
1145 idx, id);
1146 if (err)
1147 goto done;
1148 obj_type = obj->type;
1149 got_object_close(obj);
1150 if (obj_type != GOT_OBJ_TYPE_TAG) {
1151 err = got_error(GOT_ERR_OBJ_TYPE);
1152 goto done;
1154 err = read_packed_tag_privsep(tag, pack, packidx, idx, id);
1155 } else if (err->code == GOT_ERR_NO_OBJ) {
1156 int fd;
1158 err = got_object_open_loose_fd(&fd, id, repo);
1159 if (err)
1160 return err;
1161 err = got_object_read_header_privsep(&obj, id, repo, fd);
1162 if (err)
1163 return err;
1164 obj_type = obj->type;
1165 got_object_close(obj);
1166 if (obj_type != GOT_OBJ_TYPE_TAG)
1167 return got_error(GOT_ERR_OBJ_TYPE);
1169 err = got_object_open_loose_fd(&fd, id, repo);
1170 if (err)
1171 return err;
1172 err = read_tag_privsep(tag, fd, id, repo);
1175 if (err == NULL) {
1176 (*tag)->refcnt++;
1177 err = got_repo_cache_tag(repo, id, *tag);
1179 done:
1180 free(path_packfile);
1181 return err;
1184 const struct got_error *
1185 got_object_open_as_tag(struct got_tag_object **tag,
1186 struct got_repository *repo, struct got_object_id *id)
1188 *tag = got_repo_get_cached_tag(repo, id);
1189 if (*tag != NULL) {
1190 (*tag)->refcnt++;
1191 return NULL;
1194 return open_tag(tag, repo, id, 0);
1197 const struct got_error *
1198 got_object_tag_open(struct got_tag_object **tag,
1199 struct got_repository *repo, struct got_object *obj)
1201 return open_tag(tag, repo, got_object_get_id(obj), 1);
1204 const struct got_error *
1205 got_traverse_packed_commits(struct got_object_id_queue *traversed_commits,
1206 struct got_object_id *commit_id, const char *path,
1207 struct got_repository *repo)
1209 const struct got_error *err = NULL;
1210 struct got_pack *pack = NULL;
1211 struct got_packidx *packidx = NULL;
1212 char *path_packfile = NULL;
1213 struct got_commit_object *changed_commit = NULL;
1214 struct got_object_id *changed_commit_id = NULL;
1215 int idx;
1217 err = got_repo_search_packidx(&packidx, &idx, repo, commit_id);
1218 if (err) {
1219 if (err->code != GOT_ERR_NO_OBJ)
1220 return err;
1221 return NULL;
1224 err = got_packidx_get_packfile_path(&path_packfile,
1225 packidx->path_packidx);
1226 if (err)
1227 return err;
1229 pack = got_repo_get_cached_pack(repo, path_packfile);
1230 if (pack == NULL) {
1231 err = got_repo_cache_pack(&pack, repo, path_packfile, packidx);
1232 if (err)
1233 goto done;
1236 if (pack->privsep_child == NULL) {
1237 err = got_pack_start_privsep_child(pack, packidx);
1238 if (err)
1239 goto done;
1242 err = got_privsep_send_commit_traversal_request(
1243 pack->privsep_child->ibuf, commit_id, idx, path);
1244 if (err)
1245 goto done;
1247 err = got_privsep_recv_traversed_commits(&changed_commit,
1248 &changed_commit_id, traversed_commits, pack->privsep_child->ibuf);
1249 if (err)
1250 goto done;
1252 if (changed_commit) {
1254 * Cache the commit in which the path was changed.
1255 * This commit might be opened again soon.
1257 changed_commit->refcnt++;
1258 err = got_repo_cache_commit(repo, changed_commit_id,
1259 changed_commit);
1260 got_object_commit_close(changed_commit);
1262 done:
1263 free(path_packfile);
1264 free(changed_commit_id);
1265 return err;
1268 const struct got_error *
1269 got_object_enumerate(int *found_all_objects,
1270 got_object_enumerate_commit_cb cb_commit,
1271 got_object_enumerate_tree_cb cb_tree, void *cb_arg,
1272 struct got_object_id **ours, int nours,
1273 struct got_object_id **theirs, int ntheirs,
1274 struct got_packidx *packidx, struct got_repository *repo)
1276 const struct got_error *err = NULL;
1277 struct got_pack *pack;
1278 char *path_packfile = NULL;
1280 err = got_packidx_get_packfile_path(&path_packfile,
1281 packidx->path_packidx);
1282 if (err)
1283 return err;
1285 pack = got_repo_get_cached_pack(repo, path_packfile);
1286 if (pack == NULL) {
1287 err = got_repo_cache_pack(&pack, repo, path_packfile, packidx);
1288 if (err)
1289 goto done;
1292 if (pack->privsep_child == NULL) {
1293 err = got_pack_start_privsep_child(pack, packidx);
1294 if (err)
1295 goto done;
1298 err = got_privsep_send_object_enumeration_request(
1299 pack->privsep_child->ibuf);
1300 if (err)
1301 goto done;
1303 err = got_privsep_send_object_idlist(pack->privsep_child->ibuf,
1304 ours, nours);
1305 if (err)
1306 goto done;
1307 err = got_privsep_send_object_idlist_done(pack->privsep_child->ibuf);
1308 if (err)
1309 goto done;
1311 err = got_privsep_send_object_idlist(pack->privsep_child->ibuf,
1312 theirs, ntheirs);
1313 if (err)
1314 goto done;
1315 err = got_privsep_send_object_idlist_done(pack->privsep_child->ibuf);
1316 if (err)
1317 goto done;
1319 err = got_privsep_recv_enumerated_objects(found_all_objects,
1320 pack->privsep_child->ibuf, cb_commit, cb_tree, cb_arg, repo);
1321 done:
1322 free(path_packfile);
1323 return err;