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 NULL;
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.sha1, id->sha1, SHA1_DIGEST_LENGTH);
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, hdrlen, size);
511 if (err)
512 goto done;
514 err = got_repo_cache_raw_object(repo, id, *obj);
515 done:
516 free(path_packfile);
517 if (err) {
518 if (*obj) {
519 got_object_raw_close(*obj);
520 *obj = NULL;
522 free(outbuf);
524 return err;
527 static const struct got_error *
528 request_packed_commit(struct got_commit_object **commit, struct got_pack *pack,
529 int pack_idx, struct got_object_id *id)
531 const struct got_error *err = NULL;
533 err = got_privsep_send_commit_req(pack->privsep_child->ibuf, -1, id,
534 pack_idx);
535 if (err)
536 return err;
538 err = got_privsep_recv_commit(commit, pack->privsep_child->ibuf);
539 if (err)
540 return err;
542 (*commit)->flags |= GOT_COMMIT_FLAG_PACKED;
543 return NULL;
546 static const struct got_error *
547 read_packed_commit_privsep(struct got_commit_object **commit,
548 struct got_pack *pack, struct got_packidx *packidx, int idx,
549 struct got_object_id *id)
551 const struct got_error *err = NULL;
553 if (pack->privsep_child)
554 return request_packed_commit(commit, pack, idx, id);
556 err = got_pack_start_privsep_child(pack, packidx);
557 if (err)
558 return err;
560 return request_packed_commit(commit, pack, idx, id);
563 static const struct got_error *
564 request_commit(struct got_commit_object **commit, struct got_repository *repo,
565 int fd, struct got_object_id *id)
567 const struct got_error *err = NULL;
568 struct imsgbuf *ibuf;
570 ibuf = repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_COMMIT].ibuf;
572 err = got_privsep_send_commit_req(ibuf, fd, id, -1);
573 if (err)
574 return err;
576 return got_privsep_recv_commit(commit, ibuf);
579 static const struct got_error *
580 read_commit_privsep(struct got_commit_object **commit, int obj_fd,
581 struct got_object_id *id, struct got_repository *repo)
583 const struct got_error *err;
585 if (repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_COMMIT].imsg_fd != -1)
586 return request_commit(commit, repo, obj_fd, id);
588 err = start_child(repo, GOT_REPO_PRIVSEP_CHILD_COMMIT);
589 if (err)
590 return err;
592 return request_commit(commit, repo, obj_fd, id);
595 static const struct got_error *
596 open_commit(struct got_commit_object **commit,
597 struct got_repository *repo, struct got_object_id *id, int check_cache)
599 const struct got_error *err = NULL;
600 struct got_packidx *packidx = NULL;
601 int idx;
602 char *path_packfile = NULL;
604 if (check_cache) {
605 *commit = got_repo_get_cached_commit(repo, id);
606 if (*commit != NULL) {
607 (*commit)->refcnt++;
608 return NULL;
610 } else
611 *commit = NULL;
613 err = got_repo_search_packidx(&packidx, &idx, repo, id);
614 if (err == NULL) {
615 struct got_pack *pack = NULL;
617 err = got_packidx_get_packfile_path(&path_packfile,
618 packidx->path_packidx);
619 if (err)
620 return err;
622 pack = got_repo_get_cached_pack(repo, path_packfile);
623 if (pack == NULL) {
624 err = got_repo_cache_pack(&pack, repo, path_packfile,
625 packidx);
626 if (err)
627 goto done;
629 err = read_packed_commit_privsep(commit, pack,
630 packidx, idx, id);
631 } else if (err->code == GOT_ERR_NO_OBJ) {
632 int fd;
634 err = got_object_open_loose_fd(&fd, id, repo);
635 if (err)
636 return err;
637 err = read_commit_privsep(commit, fd, id, repo);
640 if (err == NULL) {
641 (*commit)->refcnt++;
642 err = got_repo_cache_commit(repo, id, *commit);
644 done:
645 free(path_packfile);
646 return err;
649 const struct got_error *
650 got_object_open_as_commit(struct got_commit_object **commit,
651 struct got_repository *repo, struct got_object_id *id)
653 *commit = got_repo_get_cached_commit(repo, id);
654 if (*commit != NULL) {
655 (*commit)->refcnt++;
656 return NULL;
659 return open_commit(commit, repo, id, 0);
662 const struct got_error *
663 got_object_commit_open(struct got_commit_object **commit,
664 struct got_repository *repo, struct got_object *obj)
666 return open_commit(commit, repo, got_object_get_id(obj), 1);
669 static const struct got_error *
670 request_packed_tree(struct got_tree_object **tree, struct got_pack *pack,
671 int pack_idx, struct got_object_id *id)
673 const struct got_error *err = NULL;
675 err = got_privsep_send_tree_req(pack->privsep_child->ibuf, -1, id,
676 pack_idx);
677 if (err)
678 return err;
680 return got_privsep_recv_tree(tree, pack->privsep_child->ibuf);
683 static const struct got_error *
684 read_packed_tree_privsep(struct got_tree_object **tree,
685 struct got_pack *pack, struct got_packidx *packidx, int idx,
686 struct got_object_id *id)
688 const struct got_error *err = NULL;
690 if (pack->privsep_child)
691 return request_packed_tree(tree, pack, idx, id);
693 err = got_pack_start_privsep_child(pack, packidx);
694 if (err)
695 return err;
697 return request_packed_tree(tree, pack, idx, id);
700 static const struct got_error *
701 request_tree(struct got_tree_object **tree, struct got_repository *repo,
702 int fd, struct got_object_id *id)
704 const struct got_error *err = NULL;
705 struct imsgbuf *ibuf;
707 ibuf = repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TREE].ibuf;
709 err = got_privsep_send_tree_req(ibuf, fd, id, -1);
710 if (err)
711 return err;
713 return got_privsep_recv_tree(tree, ibuf);
716 static const struct got_error *
717 read_tree_privsep(struct got_tree_object **tree, int obj_fd,
718 struct got_object_id *id, struct got_repository *repo)
720 const struct got_error *err;
722 if (repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TREE].imsg_fd != -1)
723 return request_tree(tree, repo, obj_fd, id);
725 err = start_child(repo, GOT_REPO_PRIVSEP_CHILD_TREE);
726 if (err)
727 return err;
729 return request_tree(tree, repo, obj_fd, id);
732 static const struct got_error *
733 open_tree(struct got_tree_object **tree, struct got_repository *repo,
734 struct got_object_id *id, int check_cache)
736 const struct got_error *err = NULL;
737 struct got_packidx *packidx = NULL;
738 int idx;
739 char *path_packfile = NULL;
741 if (check_cache) {
742 *tree = got_repo_get_cached_tree(repo, id);
743 if (*tree != NULL) {
744 (*tree)->refcnt++;
745 return NULL;
747 } else
748 *tree = NULL;
750 err = got_repo_search_packidx(&packidx, &idx, repo, id);
751 if (err == NULL) {
752 struct got_pack *pack = NULL;
754 err = got_packidx_get_packfile_path(&path_packfile,
755 packidx->path_packidx);
756 if (err)
757 return err;
759 pack = got_repo_get_cached_pack(repo, path_packfile);
760 if (pack == NULL) {
761 err = got_repo_cache_pack(&pack, repo, path_packfile,
762 packidx);
763 if (err)
764 goto done;
766 err = read_packed_tree_privsep(tree, pack,
767 packidx, idx, id);
768 } else if (err->code == GOT_ERR_NO_OBJ) {
769 int fd;
771 err = got_object_open_loose_fd(&fd, id, repo);
772 if (err)
773 return err;
774 err = read_tree_privsep(tree, fd, id, repo);
777 if (err == NULL) {
778 (*tree)->refcnt++;
779 err = got_repo_cache_tree(repo, id, *tree);
781 done:
782 free(path_packfile);
783 return err;
786 const struct got_error *
787 got_object_open_as_tree(struct got_tree_object **tree,
788 struct got_repository *repo, struct got_object_id *id)
790 *tree = got_repo_get_cached_tree(repo, id);
791 if (*tree != NULL) {
792 (*tree)->refcnt++;
793 return NULL;
796 return open_tree(tree, repo, id, 0);
799 const struct got_error *
800 got_object_tree_open(struct got_tree_object **tree,
801 struct got_repository *repo, struct got_object *obj)
803 return open_tree(tree, repo, got_object_get_id(obj), 1);
806 static const struct got_error *
807 request_packed_blob(uint8_t **outbuf, size_t *size, size_t *hdrlen, int outfd,
808 struct got_pack *pack, struct got_packidx *packidx, int idx,
809 struct got_object_id *id)
811 const struct got_error *err = NULL;
812 struct imsgbuf *ibuf = pack->privsep_child->ibuf;
813 int outfd_child;
815 err = pack_child_send_tempfiles(ibuf, pack);
816 if (err)
817 return err;
819 outfd_child = dup(outfd);
820 if (outfd_child == -1)
821 return got_error_from_errno("dup");
823 err = got_privsep_send_blob_req(pack->privsep_child->ibuf, -1, id, idx);
824 if (err)
825 return err;
827 err = got_privsep_send_blob_outfd(pack->privsep_child->ibuf,
828 outfd_child);
829 if (err) {
830 return err;
833 err = got_privsep_recv_blob(outbuf, size, hdrlen,
834 pack->privsep_child->ibuf);
835 if (err)
836 return err;
838 if (lseek(outfd, SEEK_SET, 0) == -1)
839 err = got_error_from_errno("lseek");
841 return err;
844 static const struct got_error *
845 read_packed_blob_privsep(uint8_t **outbuf, size_t *size, size_t *hdrlen,
846 int outfd, struct got_pack *pack, struct got_packidx *packidx, int idx,
847 struct got_object_id *id)
849 const struct got_error *err = NULL;
851 if (pack->privsep_child == NULL) {
852 err = got_pack_start_privsep_child(pack, packidx);
853 if (err)
854 return err;
857 return request_packed_blob(outbuf, size, hdrlen, outfd, pack, packidx,
858 idx, id);
861 static const struct got_error *
862 request_blob(uint8_t **outbuf, size_t *size, size_t *hdrlen, int outfd,
863 int infd, struct got_object_id *id, struct imsgbuf *ibuf)
865 const struct got_error *err = NULL;
866 int outfd_child;
868 outfd_child = dup(outfd);
869 if (outfd_child == -1)
870 return got_error_from_errno("dup");
872 err = got_privsep_send_blob_req(ibuf, infd, id, -1);
873 if (err)
874 return err;
876 err = got_privsep_send_blob_outfd(ibuf, outfd_child);
877 if (err)
878 return err;
880 err = got_privsep_recv_blob(outbuf, size, hdrlen, ibuf);
881 if (err)
882 return err;
884 if (lseek(outfd, SEEK_SET, 0) == -1)
885 return got_error_from_errno("lseek");
887 return err;
890 static const struct got_error *
891 read_blob_privsep(uint8_t **outbuf, size_t *size, size_t *hdrlen,
892 int outfd, int infd, struct got_object_id *id, struct got_repository *repo)
894 const struct got_error *err;
895 struct imsgbuf *ibuf;
897 if (repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_BLOB].imsg_fd != -1) {
898 ibuf = repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_BLOB].ibuf;
899 return request_blob(outbuf, size, hdrlen, outfd, infd, id,
900 ibuf);
903 err = start_child(repo, GOT_REPO_PRIVSEP_CHILD_BLOB);
904 if (err)
905 return err;
907 ibuf = repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_BLOB].ibuf;
908 return request_blob(outbuf, size, hdrlen, outfd, infd, id, ibuf);
911 static const struct got_error *
912 open_blob(struct got_blob_object **blob, struct got_repository *repo,
913 struct got_object_id *id, size_t blocksize, int outfd)
915 const struct got_error *err = NULL;
916 struct got_packidx *packidx = NULL;
917 int idx, dfd = -1;
918 char *path_packfile = NULL;
919 uint8_t *outbuf;
920 size_t size, hdrlen;
921 struct stat sb;
923 *blob = calloc(1, sizeof(**blob));
924 if (*blob == NULL)
925 return got_error_from_errno("calloc");
927 (*blob)->read_buf = malloc(blocksize);
928 if ((*blob)->read_buf == NULL) {
929 err = got_error_from_errno("malloc");
930 goto done;
933 if (ftruncate(outfd, 0L) == -1) {
934 err = got_error_from_errno("ftruncate");
935 goto done;
937 if (lseek(outfd, SEEK_SET, 0) == -1) {
938 err = got_error_from_errno("lseek");
939 goto done;
942 err = got_repo_search_packidx(&packidx, &idx, repo, id);
943 if (err == NULL) {
944 struct got_pack *pack = NULL;
946 err = got_packidx_get_packfile_path(&path_packfile,
947 packidx->path_packidx);
948 if (err)
949 goto done;
951 pack = got_repo_get_cached_pack(repo, path_packfile);
952 if (pack == NULL) {
953 err = got_repo_cache_pack(&pack, repo, path_packfile,
954 packidx);
955 if (err)
956 goto done;
958 err = read_packed_blob_privsep(&outbuf, &size, &hdrlen, outfd,
959 pack, packidx, idx, id);
960 } else if (err->code == GOT_ERR_NO_OBJ) {
961 int infd;
963 err = got_object_open_loose_fd(&infd, id, repo);
964 if (err)
965 goto done;
966 err = read_blob_privsep(&outbuf, &size, &hdrlen, outfd, infd,
967 id, repo);
969 if (err)
970 goto done;
972 if (hdrlen > size) {
973 err = got_error(GOT_ERR_BAD_OBJ_HDR);
974 goto done;
977 if (outbuf) {
978 (*blob)->f = fmemopen(outbuf, size, "rb");
979 if ((*blob)->f == NULL) {
980 err = got_error_from_errno("fmemopen");
981 free(outbuf);
982 goto done;
984 (*blob)->data = outbuf;
985 } else {
986 if (fstat(outfd, &sb) == -1) {
987 err = got_error_from_errno("fstat");
988 goto done;
991 if (sb.st_size != size) {
992 err = got_error(GOT_ERR_PRIVSEP_LEN);
993 goto done;
996 dfd = dup(outfd);
997 if (dfd == -1) {
998 err = got_error_from_errno("dup");
999 goto done;
1002 (*blob)->f = fdopen(dfd, "rb");
1003 if ((*blob)->f == NULL) {
1004 err = got_error_from_errno("fdopen");
1005 close(dfd);
1006 dfd = -1;
1007 goto done;
1011 (*blob)->hdrlen = hdrlen;
1012 (*blob)->blocksize = blocksize;
1013 memcpy(&(*blob)->id.sha1, id->sha1, SHA1_DIGEST_LENGTH);
1015 done:
1016 free(path_packfile);
1017 if (err) {
1018 if (*blob) {
1019 got_object_blob_close(*blob);
1020 *blob = NULL;
1023 return err;
1026 const struct got_error *
1027 got_object_open_as_blob(struct got_blob_object **blob,
1028 struct got_repository *repo, struct got_object_id *id, size_t blocksize,
1029 int outfd)
1031 return open_blob(blob, repo, id, blocksize, outfd);
1034 const struct got_error *
1035 got_object_blob_open(struct got_blob_object **blob,
1036 struct got_repository *repo, struct got_object *obj, size_t blocksize,
1037 int outfd)
1039 return open_blob(blob, repo, got_object_get_id(obj), blocksize, outfd);
1042 static const struct got_error *
1043 request_packed_tag(struct got_tag_object **tag, struct got_pack *pack,
1044 int pack_idx, struct got_object_id *id)
1046 const struct got_error *err = NULL;
1048 err = got_privsep_send_tag_req(pack->privsep_child->ibuf, -1, id,
1049 pack_idx);
1050 if (err)
1051 return err;
1053 return got_privsep_recv_tag(tag, pack->privsep_child->ibuf);
1056 static const struct got_error *
1057 read_packed_tag_privsep(struct got_tag_object **tag,
1058 struct got_pack *pack, struct got_packidx *packidx, int idx,
1059 struct got_object_id *id)
1061 const struct got_error *err = NULL;
1063 if (pack->privsep_child)
1064 return request_packed_tag(tag, pack, idx, id);
1066 err = got_pack_start_privsep_child(pack, packidx);
1067 if (err)
1068 return err;
1070 return request_packed_tag(tag, pack, idx, id);
1073 static const struct got_error *
1074 request_tag(struct got_tag_object **tag, struct got_repository *repo,
1075 int fd, struct got_object_id *id)
1077 const struct got_error *err = NULL;
1078 struct imsgbuf *ibuf;
1080 ibuf = repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TAG].ibuf;
1082 err = got_privsep_send_tag_req(ibuf, fd, id, -1);
1083 if (err)
1084 return err;
1086 return got_privsep_recv_tag(tag, ibuf);
1089 static const struct got_error *
1090 read_tag_privsep(struct got_tag_object **tag, int obj_fd,
1091 struct got_object_id *id, struct got_repository *repo)
1093 const struct got_error *err;
1095 if (repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TAG].imsg_fd != -1)
1096 return request_tag(tag, repo, obj_fd, id);
1098 err = start_child(repo, GOT_REPO_PRIVSEP_CHILD_TAG);
1099 if (err)
1100 return err;
1102 return request_tag(tag, repo, obj_fd, id);
1105 static const struct got_error *
1106 open_tag(struct got_tag_object **tag, struct got_repository *repo,
1107 struct got_object_id *id, int check_cache)
1109 const struct got_error *err = NULL;
1110 struct got_packidx *packidx = NULL;
1111 int idx;
1112 char *path_packfile = NULL;
1113 struct got_object *obj = NULL;
1114 int obj_type = GOT_OBJ_TYPE_ANY;
1116 if (check_cache) {
1117 *tag = got_repo_get_cached_tag(repo, id);
1118 if (*tag != NULL) {
1119 (*tag)->refcnt++;
1120 return NULL;
1122 } else
1123 *tag = NULL;
1125 err = got_repo_search_packidx(&packidx, &idx, repo, id);
1126 if (err == NULL) {
1127 struct got_pack *pack = NULL;
1129 err = got_packidx_get_packfile_path(&path_packfile,
1130 packidx->path_packidx);
1131 if (err)
1132 return err;
1134 pack = got_repo_get_cached_pack(repo, path_packfile);
1135 if (pack == NULL) {
1136 err = got_repo_cache_pack(&pack, repo, path_packfile,
1137 packidx);
1138 if (err)
1139 goto done;
1142 /* Beware of "lightweight" tags: Check object type first. */
1143 err = read_packed_object_privsep(&obj, repo, pack, packidx,
1144 idx, id);
1145 if (err)
1146 goto done;
1147 obj_type = obj->type;
1148 got_object_close(obj);
1149 if (obj_type != GOT_OBJ_TYPE_TAG) {
1150 err = got_error(GOT_ERR_OBJ_TYPE);
1151 goto done;
1153 err = read_packed_tag_privsep(tag, pack, packidx, idx, id);
1154 } else if (err->code == GOT_ERR_NO_OBJ) {
1155 int fd;
1157 err = got_object_open_loose_fd(&fd, id, repo);
1158 if (err)
1159 return err;
1160 err = got_object_read_header_privsep(&obj, id, repo, fd);
1161 if (err)
1162 return err;
1163 obj_type = obj->type;
1164 got_object_close(obj);
1165 if (obj_type != GOT_OBJ_TYPE_TAG)
1166 return got_error(GOT_ERR_OBJ_TYPE);
1168 err = got_object_open_loose_fd(&fd, id, repo);
1169 if (err)
1170 return err;
1171 err = read_tag_privsep(tag, fd, id, repo);
1174 if (err == NULL) {
1175 (*tag)->refcnt++;
1176 err = got_repo_cache_tag(repo, id, *tag);
1178 done:
1179 free(path_packfile);
1180 return err;
1183 const struct got_error *
1184 got_object_open_as_tag(struct got_tag_object **tag,
1185 struct got_repository *repo, struct got_object_id *id)
1187 *tag = got_repo_get_cached_tag(repo, id);
1188 if (*tag != NULL) {
1189 (*tag)->refcnt++;
1190 return NULL;
1193 return open_tag(tag, repo, id, 0);
1196 const struct got_error *
1197 got_object_tag_open(struct got_tag_object **tag,
1198 struct got_repository *repo, struct got_object *obj)
1200 return open_tag(tag, repo, got_object_get_id(obj), 1);
1203 const struct got_error *
1204 got_traverse_packed_commits(struct got_object_id_queue *traversed_commits,
1205 struct got_object_id *commit_id, const char *path,
1206 struct got_repository *repo)
1208 const struct got_error *err = NULL;
1209 struct got_pack *pack = NULL;
1210 struct got_packidx *packidx = NULL;
1211 char *path_packfile = NULL;
1212 struct got_commit_object *changed_commit = NULL;
1213 struct got_object_id *changed_commit_id = NULL;
1214 int idx;
1216 err = got_repo_search_packidx(&packidx, &idx, repo, commit_id);
1217 if (err) {
1218 if (err->code != GOT_ERR_NO_OBJ)
1219 return err;
1220 return NULL;
1223 err = got_packidx_get_packfile_path(&path_packfile,
1224 packidx->path_packidx);
1225 if (err)
1226 return err;
1228 pack = got_repo_get_cached_pack(repo, path_packfile);
1229 if (pack == NULL) {
1230 err = got_repo_cache_pack(&pack, repo, path_packfile, packidx);
1231 if (err)
1232 goto done;
1235 if (pack->privsep_child == NULL) {
1236 err = got_pack_start_privsep_child(pack, packidx);
1237 if (err)
1238 goto done;
1241 err = got_privsep_send_commit_traversal_request(
1242 pack->privsep_child->ibuf, commit_id, idx, path);
1243 if (err)
1244 goto done;
1246 err = got_privsep_recv_traversed_commits(&changed_commit,
1247 &changed_commit_id, traversed_commits, pack->privsep_child->ibuf);
1248 if (err)
1249 goto done;
1251 if (changed_commit) {
1253 * Cache the commit in which the path was changed.
1254 * This commit might be opened again soon.
1256 changed_commit->refcnt++;
1257 err = got_repo_cache_commit(repo, changed_commit_id,
1258 changed_commit);
1259 got_object_commit_close(changed_commit);
1261 done:
1262 free(path_packfile);
1263 free(changed_commit_id);
1264 return err;
1267 const struct got_error *
1268 got_object_enumerate(int *found_all_objects,
1269 got_object_enumerate_commit_cb cb_commit,
1270 got_object_enumerate_tree_cb cb_tree, void *cb_arg,
1271 struct got_object_id **ours, int nours,
1272 struct got_object_id **theirs, int ntheirs,
1273 struct got_packidx *packidx, struct got_repository *repo)
1275 const struct got_error *err = NULL;
1276 struct got_pack *pack;
1277 char *path_packfile = NULL;
1279 err = got_packidx_get_packfile_path(&path_packfile,
1280 packidx->path_packidx);
1281 if (err)
1282 return err;
1284 pack = got_repo_get_cached_pack(repo, path_packfile);
1285 if (pack == NULL) {
1286 err = got_repo_cache_pack(&pack, repo, path_packfile, packidx);
1287 if (err)
1288 goto done;
1291 if (pack->privsep_child == NULL) {
1292 err = got_pack_start_privsep_child(pack, packidx);
1293 if (err)
1294 goto done;
1297 err = got_privsep_send_object_enumeration_request(
1298 pack->privsep_child->ibuf);
1299 if (err)
1300 goto done;
1302 err = got_privsep_send_object_idlist(pack->privsep_child->ibuf,
1303 ours, nours);
1304 if (err)
1305 goto done;
1306 err = got_privsep_send_object_idlist_done(pack->privsep_child->ibuf);
1307 if (err)
1308 goto done;
1310 err = got_privsep_send_object_idlist(pack->privsep_child->ibuf,
1311 theirs, ntheirs);
1312 if (err)
1313 goto done;
1314 err = got_privsep_send_object_idlist_done(pack->privsep_child->ibuf);
1315 if (err)
1316 goto done;
1318 err = got_privsep_recv_enumerated_objects(found_all_objects,
1319 pack->privsep_child->ibuf, cb_commit, cb_tree, cb_arg, repo);
1320 done:
1321 free(path_packfile);
1322 return err;