Blob


1 /*
2 * Copyright (c) 2018, 2019 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/types.h>
18 #include <sys/stat.h>
19 #include <sys/uio.h>
20 #include <sys/socket.h>
21 #include <sys/wait.h>
22 #include <sys/mman.h>
24 #include <errno.h>
25 #include <fcntl.h>
26 #include <stdio.h>
27 #include <stdlib.h>
28 #include <string.h>
29 #include <stdint.h>
30 #include <unistd.h>
31 #include <zlib.h>
32 #include <ctype.h>
33 #include <libgen.h>
34 #include <limits.h>
35 #include <time.h>
37 #include "got_compat.h"
39 #include "got_error.h"
40 #include "got_object.h"
41 #include "got_repository.h"
42 #include "got_opentemp.h"
43 #include "got_path.h"
45 #include "got_lib_sha1.h"
46 #include "got_lib_delta.h"
47 #include "got_lib_inflate.h"
48 #include "got_lib_object.h"
49 #include "got_lib_privsep.h"
50 #include "got_lib_object_idcache.h"
51 #include "got_lib_object_cache.h"
52 #include "got_lib_object_parse.h"
53 #include "got_lib_pack.h"
54 #include "got_lib_repository.h"
56 #ifndef MIN
57 #define MIN(_a,_b) ((_a) < (_b) ? (_a) : (_b))
58 #endif
60 #ifndef nitems
61 #define nitems(_a) (sizeof((_a)) / sizeof((_a)[0]))
62 #endif
64 struct got_object_id *
65 got_object_get_id(struct got_object *obj)
66 {
67 return &obj->id;
68 }
70 const struct got_error *
71 got_object_get_id_str(char **outbuf, struct got_object *obj)
72 {
73 return got_object_id_str(outbuf, &obj->id);
74 }
76 const struct got_error *
77 got_object_get_type(int *type, struct got_repository *repo,
78 struct got_object_id *id)
79 {
80 const struct got_error *err = NULL;
81 struct got_object *obj;
83 err = got_object_open(&obj, repo, id);
84 if (err)
85 return err;
87 switch (obj->type) {
88 case GOT_OBJ_TYPE_COMMIT:
89 case GOT_OBJ_TYPE_TREE:
90 case GOT_OBJ_TYPE_BLOB:
91 case GOT_OBJ_TYPE_TAG:
92 *type = obj->type;
93 break;
94 default:
95 err = got_error(GOT_ERR_OBJ_TYPE);
96 break;
97 }
99 got_object_close(obj);
100 return err;
103 const struct got_error *
104 got_object_get_path(char **path, struct got_object_id *id,
105 struct got_repository *repo)
107 const struct got_error *err = NULL;
108 char *hex = NULL;
109 char *path_objects;
111 *path = NULL;
113 path_objects = got_repo_get_path_objects(repo);
114 if (path_objects == NULL)
115 return got_error_from_errno("got_repo_get_path_objects");
117 err = got_object_id_str(&hex, id);
118 if (err)
119 goto done;
121 if (asprintf(path, "%s/%.2x/%s", path_objects,
122 id->sha1[0], hex + 2) == -1)
123 err = got_error_from_errno("asprintf");
125 done:
126 free(hex);
127 free(path_objects);
128 return err;
131 const struct got_error *
132 got_object_open_loose_fd(int *fd, struct got_object_id *id,
133 struct got_repository *repo)
135 const struct got_error *err = NULL;
136 char *path;
138 err = got_object_get_path(&path, id, repo);
139 if (err)
140 return err;
141 *fd = open(path, O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
142 if (*fd == -1) {
143 err = got_error_from_errno2("open", path);
144 goto done;
146 done:
147 free(path);
148 return err;
151 static const struct got_error *
152 request_packed_object(struct got_object **obj, struct got_pack *pack, int idx,
153 struct got_object_id *id)
155 const struct got_error *err = NULL;
156 struct imsgbuf *ibuf = pack->privsep_child->ibuf;
158 err = got_privsep_send_packed_obj_req(ibuf, idx, id);
159 if (err)
160 return err;
162 err = got_privsep_recv_obj(obj, ibuf);
163 if (err)
164 return err;
166 memcpy(&(*obj)->id, id, sizeof((*obj)->id));
168 return NULL;
171 /* Create temporary files used during delta application. */
172 static const struct got_error *
173 pack_child_send_tempfiles(struct imsgbuf *ibuf, struct got_pack *pack)
175 const struct got_error *err;
176 int basefd = -1, accumfd = -1;
178 /*
179 * For performance reasons, the child will keep reusing the
180 * same temporary files during every object request.
181 * Opening and closing new files for every object request is
182 * too expensive during operations such as 'gotadmin pack'.
183 */
184 if (pack->child_has_tempfiles)
185 return NULL;
187 basefd = dup(pack->basefd);
188 if (basefd == -1)
189 return got_error_from_errno("dup");
191 accumfd = dup(pack->accumfd);
192 if (accumfd == -1) {
193 err = got_error_from_errno("dup");
194 goto done;
197 err = got_privsep_send_tmpfd(ibuf, basefd);
198 if (err)
199 goto done;
201 err = got_privsep_send_tmpfd(ibuf, accumfd);
202 done:
203 if (err) {
204 if (basefd != -1)
205 close(basefd);
206 if (accumfd != -1)
207 close(accumfd);
208 } else
209 pack->child_has_tempfiles = 1;
210 return NULL;
213 static const struct got_error *
214 request_packed_object_raw(uint8_t **outbuf, off_t *size, size_t *hdrlen,
215 int outfd, struct got_pack *pack, int idx, struct got_object_id *id)
217 const struct got_error *err = NULL;
218 struct imsgbuf *ibuf = pack->privsep_child->ibuf;
219 int outfd_child;
221 err = pack_child_send_tempfiles(ibuf, pack);
222 if (err)
223 return err;
225 outfd_child = dup(outfd);
226 if (outfd_child == -1)
227 return got_error_from_errno("dup");
229 err = got_privsep_send_packed_raw_obj_req(ibuf, idx, id);
230 if (err) {
231 close(outfd_child);
232 return err;
235 err = got_privsep_send_raw_obj_outfd(ibuf, outfd_child);
236 if (err)
237 return err;
239 err = got_privsep_recv_raw_obj(outbuf, size, hdrlen, ibuf);
240 if (err)
241 return err;
243 return NULL;
246 static const struct got_error *
247 read_packed_object_privsep(struct got_object **obj,
248 struct got_repository *repo, struct got_pack *pack,
249 struct got_packidx *packidx, int idx, struct got_object_id *id)
251 const struct got_error *err = NULL;
253 if (pack->privsep_child == NULL) {
254 err = got_pack_start_privsep_child(pack, packidx);
255 if (err)
256 return err;
259 return request_packed_object(obj, pack, idx, id);
262 static const struct got_error *
263 read_packed_object_raw_privsep(uint8_t **outbuf, off_t *size, size_t *hdrlen,
264 int outfd, struct got_pack *pack, struct got_packidx *packidx, int idx,
265 struct got_object_id *id)
267 const struct got_error *err = NULL;
269 if (pack->privsep_child == NULL) {
270 err = got_pack_start_privsep_child(pack, packidx);
271 if (err)
272 return err;
275 return request_packed_object_raw(outbuf, size, hdrlen, outfd, pack,
276 idx, id);
279 const struct got_error *
280 got_object_open_packed(struct got_object **obj, struct got_object_id *id,
281 struct got_repository *repo)
283 const struct got_error *err = NULL;
284 struct got_pack *pack = NULL;
285 struct got_packidx *packidx = NULL;
286 int idx;
287 char *path_packfile;
289 err = got_repo_search_packidx(&packidx, &idx, repo, id);
290 if (err)
291 return err;
293 err = got_packidx_get_packfile_path(&path_packfile,
294 packidx->path_packidx);
295 if (err)
296 return err;
298 pack = got_repo_get_cached_pack(repo, path_packfile);
299 if (pack == NULL) {
300 err = got_repo_cache_pack(&pack, repo, path_packfile, packidx);
301 if (err)
302 goto done;
305 err = read_packed_object_privsep(obj, repo, pack, packidx, idx, id);
306 if (err)
307 goto done;
308 done:
309 free(path_packfile);
310 return err;
313 const struct got_error *
314 got_object_open_from_packfile(struct got_object **obj, struct got_object_id *id,
315 struct got_pack *pack, struct got_packidx *packidx, int obj_idx,
316 struct got_repository *repo)
318 return read_packed_object_privsep(obj, repo, pack, packidx,
319 obj_idx, id);
322 const struct got_error *
323 got_object_read_raw_delta(uint64_t *base_size, uint64_t *result_size,
324 off_t *delta_size, off_t *delta_compressed_size, off_t *delta_offset,
325 off_t *delta_out_offset, struct got_object_id **base_id, int delta_cache_fd,
326 struct got_packidx *packidx, int obj_idx, struct got_object_id *id,
327 struct got_repository *repo)
329 const struct got_error *err = NULL;
330 struct got_pack *pack = NULL;
331 char *path_packfile;
333 *base_size = 0;
334 *result_size = 0;
335 *delta_size = 0;
336 *delta_compressed_size = 0;
337 *delta_offset = 0;
338 *delta_out_offset = 0;
340 err = got_packidx_get_packfile_path(&path_packfile,
341 packidx->path_packidx);
342 if (err)
343 return err;
345 pack = got_repo_get_cached_pack(repo, path_packfile);
346 if (pack == NULL) {
347 err = got_repo_cache_pack(&pack, repo, path_packfile, packidx);
348 if (err)
349 return err;
352 if (pack->privsep_child == NULL) {
353 err = got_pack_start_privsep_child(pack, packidx);
354 if (err)
355 return err;
358 if (!pack->child_has_delta_outfd) {
359 int outfd_child;
360 outfd_child = dup(delta_cache_fd);
361 if (outfd_child == -1)
362 return got_error_from_errno("dup");
363 err = got_privsep_send_raw_delta_outfd(
364 pack->privsep_child->ibuf, outfd_child);
365 if (err)
366 return err;
367 pack->child_has_delta_outfd = 1;
370 err = got_privsep_send_raw_delta_req(pack->privsep_child->ibuf,
371 obj_idx, id);
372 if (err)
373 return err;
375 return got_privsep_recv_raw_delta(base_size, result_size, delta_size,
376 delta_compressed_size, delta_offset, delta_out_offset, base_id,
377 pack->privsep_child->ibuf);
380 static const struct got_error *
381 request_object(struct got_object **obj, struct got_object_id *id,
382 struct got_repository *repo, int fd)
384 const struct got_error *err = NULL;
385 struct imsgbuf *ibuf;
387 ibuf = repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_OBJECT].ibuf;
389 err = got_privsep_send_obj_req(ibuf, fd, id);
390 if (err)
391 return err;
393 return got_privsep_recv_obj(obj, ibuf);
396 static const struct got_error *
397 request_raw_object(uint8_t **outbuf, off_t *size, size_t *hdrlen, int outfd,
398 struct got_object_id *id, struct got_repository *repo, int infd)
400 const struct got_error *err = NULL;
401 struct imsgbuf *ibuf;
402 int outfd_child;
404 ibuf = repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_OBJECT].ibuf;
406 outfd_child = dup(outfd);
407 if (outfd_child == -1)
408 return got_error_from_errno("dup");
410 err = got_privsep_send_raw_obj_req(ibuf, infd, id);
411 if (err)
412 return err;
414 err = got_privsep_send_raw_obj_outfd(ibuf, outfd_child);
415 if (err)
416 return err;
418 return got_privsep_recv_raw_obj(outbuf, size, hdrlen, ibuf);
421 static const struct got_error *
422 start_read_object_child(struct got_repository *repo)
424 const struct got_error *err = NULL;
425 int imsg_fds[2];
426 pid_t pid;
427 struct imsgbuf *ibuf;
429 ibuf = calloc(1, sizeof(*ibuf));
430 if (ibuf == NULL)
431 return got_error_from_errno("calloc");
433 if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_fds) == -1) {
434 err = got_error_from_errno("socketpair");
435 free(ibuf);
436 return err;
439 pid = fork();
440 if (pid == -1) {
441 err = got_error_from_errno("fork");
442 free(ibuf);
443 return err;
445 else if (pid == 0) {
446 got_privsep_exec_child(imsg_fds, GOT_PATH_PROG_READ_OBJECT,
447 repo->path);
448 /* not reached */
451 if (close(imsg_fds[1]) == -1) {
452 err = got_error_from_errno("close");
453 free(ibuf);
454 return err;
457 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_OBJECT].imsg_fd =
458 imsg_fds[0];
459 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_OBJECT].pid = pid;
460 imsg_init(ibuf, imsg_fds[0]);
461 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_OBJECT].ibuf = ibuf;
463 return NULL;
466 const struct got_error *
467 got_object_read_header_privsep(struct got_object **obj,
468 struct got_object_id *id, struct got_repository *repo, int obj_fd)
470 const struct got_error *err;
472 if (repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_OBJECT].imsg_fd != -1)
473 return request_object(obj, id, repo, obj_fd);
475 err = start_read_object_child(repo);
476 if (err) {
477 close(obj_fd);
478 return err;
481 return request_object(obj, id, repo, obj_fd);
484 static const struct got_error *
485 read_object_raw_privsep(uint8_t **outbuf, off_t *size, size_t *hdrlen,
486 int outfd, struct got_object_id *id, struct got_repository *repo,
487 int obj_fd)
489 const struct got_error *err;
491 if (repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_OBJECT].imsg_fd != -1)
492 return request_raw_object(outbuf, size, hdrlen, outfd, id,
493 repo, obj_fd);
495 err = start_read_object_child(repo);
496 if (err)
497 return err;
499 return request_raw_object(outbuf, size, hdrlen, outfd, id, repo,
500 obj_fd);
503 const struct got_error *
504 got_object_open(struct got_object **obj, struct got_repository *repo,
505 struct got_object_id *id)
507 const struct got_error *err = NULL;
508 int fd;
510 *obj = got_repo_get_cached_object(repo, id);
511 if (*obj != NULL) {
512 (*obj)->refcnt++;
513 return NULL;
516 err = got_object_open_packed(obj, id, repo);
517 if (err && err->code != GOT_ERR_NO_OBJ)
518 return err;
519 if (*obj) {
520 (*obj)->refcnt++;
521 return got_repo_cache_object(repo, id, *obj);
524 err = got_object_open_loose_fd(&fd, id, repo);
525 if (err) {
526 if (err->code == GOT_ERR_ERRNO && errno == ENOENT)
527 err = got_error_no_obj(id);
528 return err;
531 err = got_object_read_header_privsep(obj, id, repo, fd);
532 if (err)
533 return err;
535 memcpy((*obj)->id.sha1, id->sha1, SHA1_DIGEST_LENGTH);
537 (*obj)->refcnt++;
538 return got_repo_cache_object(repo, id, *obj);
541 /* *outfd must be initialized to -1 by caller */
542 const struct got_error *
543 got_object_raw_open(struct got_raw_object **obj, int *outfd,
544 struct got_repository *repo, struct got_object_id *id)
546 const struct got_error *err = NULL;
547 struct got_packidx *packidx = NULL;
548 int idx;
549 uint8_t *outbuf = NULL;
550 off_t size = 0;
551 size_t hdrlen = 0;
552 char *path_packfile = NULL;
554 *obj = got_repo_get_cached_raw_object(repo, id);
555 if (*obj != NULL) {
556 (*obj)->refcnt++;
557 return NULL;
560 if (*outfd == -1) {
561 *outfd = got_opentempfd();
562 if (*outfd == -1)
563 return got_error_from_errno("got_opentempfd");
566 err = got_repo_search_packidx(&packidx, &idx, repo, id);
567 if (err == NULL) {
568 struct got_pack *pack = NULL;
570 err = got_packidx_get_packfile_path(&path_packfile,
571 packidx->path_packidx);
572 if (err)
573 goto done;
575 pack = got_repo_get_cached_pack(repo, path_packfile);
576 if (pack == NULL) {
577 err = got_repo_cache_pack(&pack, repo, path_packfile,
578 packidx);
579 if (err)
580 goto done;
582 err = read_packed_object_raw_privsep(&outbuf, &size, &hdrlen,
583 *outfd, pack, packidx, idx, id);
584 if (err)
585 goto done;
586 } else if (err->code == GOT_ERR_NO_OBJ) {
587 int fd;
589 err = got_object_open_loose_fd(&fd, id, repo);
590 if (err)
591 goto done;
592 err = read_object_raw_privsep(&outbuf, &size, &hdrlen, *outfd,
593 id, repo, fd);
594 if (err)
595 goto done;
598 *obj = calloc(1, sizeof(**obj));
599 if (*obj == NULL) {
600 err = got_error_from_errno("calloc");
601 goto done;
603 (*obj)->fd = -1;
605 if (outbuf) {
606 (*obj)->data = outbuf;
607 } else {
608 struct stat sb;
609 if (fstat(*outfd, &sb) == -1) {
610 err = got_error_from_errno("fstat");
611 goto done;
614 if (sb.st_size != hdrlen + size) {
615 err = got_error(GOT_ERR_PRIVSEP_LEN);
616 goto done;
618 #ifndef GOT_PACK_NO_MMAP
619 if (hdrlen + size > 0) {
620 (*obj)->data = mmap(NULL, hdrlen + size, PROT_READ,
621 MAP_PRIVATE, *outfd, 0);
622 if ((*obj)->data == MAP_FAILED) {
623 if (errno != ENOMEM) {
624 err = got_error_from_errno("mmap");
625 goto done;
627 (*obj)->data = NULL;
628 } else {
629 (*obj)->fd = *outfd;
630 *outfd = -1;
633 #endif
634 if (*outfd != -1) {
635 (*obj)->f = fdopen(*outfd, "r");
636 if ((*obj)->f == NULL) {
637 err = got_error_from_errno("fdopen");
638 goto done;
640 *outfd = -1;
643 (*obj)->hdrlen = hdrlen;
644 (*obj)->size = size;
645 err = got_repo_cache_raw_object(repo, id, *obj);
646 done:
647 free(path_packfile);
648 if (err) {
649 if (*obj) {
650 got_object_raw_close(*obj);
651 *obj = NULL;
653 free(outbuf);
654 } else
655 (*obj)->refcnt++;
656 return err;
659 const struct got_error *
660 got_object_open_by_id_str(struct got_object **obj, struct got_repository *repo,
661 const char *id_str)
663 struct got_object_id id;
665 if (!got_parse_sha1_digest(id.sha1, id_str))
666 return got_error_path(id_str, GOT_ERR_BAD_OBJ_ID_STR);
668 return got_object_open(obj, repo, &id);
671 const struct got_error *
672 got_object_resolve_id_str(struct got_object_id **id,
673 struct got_repository *repo, const char *id_str)
675 const struct got_error *err = NULL;
676 struct got_object *obj;
678 err = got_object_open_by_id_str(&obj, repo, id_str);
679 if (err)
680 return err;
682 *id = got_object_id_dup(got_object_get_id(obj));
683 got_object_close(obj);
684 if (*id == NULL)
685 return got_error_from_errno("got_object_id_dup");
687 return NULL;
690 static const struct got_error *
691 request_packed_commit(struct got_commit_object **commit, struct got_pack *pack,
692 int pack_idx, struct got_object_id *id)
694 const struct got_error *err = NULL;
696 err = got_privsep_send_commit_req(pack->privsep_child->ibuf, -1, id,
697 pack_idx);
698 if (err)
699 return err;
701 err = got_privsep_recv_commit(commit, pack->privsep_child->ibuf);
702 if (err)
703 return err;
705 (*commit)->flags |= GOT_COMMIT_FLAG_PACKED;
706 return NULL;
709 static const struct got_error *
710 read_packed_commit_privsep(struct got_commit_object **commit,
711 struct got_pack *pack, struct got_packidx *packidx, int idx,
712 struct got_object_id *id)
714 const struct got_error *err = NULL;
716 if (pack->privsep_child)
717 return request_packed_commit(commit, pack, idx, id);
719 err = got_pack_start_privsep_child(pack, packidx);
720 if (err)
721 return err;
723 return request_packed_commit(commit, pack, idx, id);
726 static const struct got_error *
727 request_commit(struct got_commit_object **commit, struct got_repository *repo,
728 int fd, struct got_object_id *id)
730 const struct got_error *err = NULL;
731 struct imsgbuf *ibuf;
733 ibuf = repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_COMMIT].ibuf;
735 err = got_privsep_send_commit_req(ibuf, fd, id, -1);
736 if (err)
737 return err;
739 return got_privsep_recv_commit(commit, ibuf);
742 static const struct got_error *
743 read_commit_privsep(struct got_commit_object **commit, int obj_fd,
744 struct got_object_id *id, struct got_repository *repo)
746 const struct got_error *err;
747 int imsg_fds[2];
748 pid_t pid;
749 struct imsgbuf *ibuf;
751 if (repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_COMMIT].imsg_fd != -1)
752 return request_commit(commit, repo, obj_fd, id);
754 ibuf = calloc(1, sizeof(*ibuf));
755 if (ibuf == NULL)
756 return got_error_from_errno("calloc");
758 if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_fds) == -1) {
759 err = got_error_from_errno("socketpair");
760 free(ibuf);
761 return err;
764 pid = fork();
765 if (pid == -1) {
766 err = got_error_from_errno("fork");
767 free(ibuf);
768 return err;
770 else if (pid == 0) {
771 got_privsep_exec_child(imsg_fds, GOT_PATH_PROG_READ_COMMIT,
772 repo->path);
773 /* not reached */
776 if (close(imsg_fds[1]) == -1) {
777 err = got_error_from_errno("close");
778 free(ibuf);
779 return err;
781 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_COMMIT].imsg_fd =
782 imsg_fds[0];
783 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_COMMIT].pid = pid;
784 imsg_init(ibuf, imsg_fds[0]);
785 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_COMMIT].ibuf = ibuf;
787 return request_commit(commit, repo, obj_fd, id);
791 static const struct got_error *
792 open_commit(struct got_commit_object **commit,
793 struct got_repository *repo, struct got_object_id *id, int check_cache)
795 const struct got_error *err = NULL;
796 struct got_packidx *packidx = NULL;
797 int idx;
798 char *path_packfile = NULL;
800 if (check_cache) {
801 *commit = got_repo_get_cached_commit(repo, id);
802 if (*commit != NULL) {
803 (*commit)->refcnt++;
804 return NULL;
806 } else
807 *commit = NULL;
809 err = got_repo_search_packidx(&packidx, &idx, repo, id);
810 if (err == NULL) {
811 struct got_pack *pack = NULL;
813 err = got_packidx_get_packfile_path(&path_packfile,
814 packidx->path_packidx);
815 if (err)
816 return err;
818 pack = got_repo_get_cached_pack(repo, path_packfile);
819 if (pack == NULL) {
820 err = got_repo_cache_pack(&pack, repo, path_packfile,
821 packidx);
822 if (err)
823 goto done;
825 err = read_packed_commit_privsep(commit, pack,
826 packidx, idx, id);
827 } else if (err->code == GOT_ERR_NO_OBJ) {
828 int fd;
830 err = got_object_open_loose_fd(&fd, id, repo);
831 if (err)
832 return err;
833 err = read_commit_privsep(commit, fd, id, repo);
836 if (err == NULL) {
837 (*commit)->refcnt++;
838 err = got_repo_cache_commit(repo, id, *commit);
840 done:
841 free(path_packfile);
842 return err;
845 const struct got_error *
846 got_object_open_as_commit(struct got_commit_object **commit,
847 struct got_repository *repo, struct got_object_id *id)
849 *commit = got_repo_get_cached_commit(repo, id);
850 if (*commit != NULL) {
851 (*commit)->refcnt++;
852 return NULL;
855 return open_commit(commit, repo, id, 0);
858 const struct got_error *
859 got_object_commit_open(struct got_commit_object **commit,
860 struct got_repository *repo, struct got_object *obj)
862 return open_commit(commit, repo, got_object_get_id(obj), 1);
865 const struct got_error *
866 got_object_qid_alloc(struct got_object_qid **qid, struct got_object_id *id)
868 *qid = calloc(1, sizeof(**qid));
869 if (*qid == NULL)
870 return got_error_from_errno("calloc");
872 memcpy(&(*qid)->id, id, sizeof((*qid)->id));
873 return NULL;
876 const struct got_error *
877 got_object_id_queue_copy(const struct got_object_id_queue *src,
878 struct got_object_id_queue *dest)
880 const struct got_error *err;
881 struct got_object_qid *qid;
883 STAILQ_FOREACH(qid, src, entry) {
884 struct got_object_qid *new;
885 /*
886 * Deep-copy the object ID only. Let the caller deal
887 * with setting up the new->data pointer if needed.
888 */
889 err = got_object_qid_alloc(&new, &qid->id);
890 if (err) {
891 got_object_id_queue_free(dest);
892 return err;
894 STAILQ_INSERT_TAIL(dest, new, entry);
897 return NULL;
900 static const struct got_error *
901 request_packed_tree(struct got_tree_object **tree, struct got_pack *pack,
902 int pack_idx, struct got_object_id *id)
904 const struct got_error *err = NULL;
906 err = got_privsep_send_tree_req(pack->privsep_child->ibuf, -1, id,
907 pack_idx);
908 if (err)
909 return err;
911 return got_privsep_recv_tree(tree, pack->privsep_child->ibuf);
914 static const struct got_error *
915 read_packed_tree_privsep(struct got_tree_object **tree,
916 struct got_pack *pack, struct got_packidx *packidx, int idx,
917 struct got_object_id *id)
919 const struct got_error *err = NULL;
921 if (pack->privsep_child)
922 return request_packed_tree(tree, pack, idx, id);
924 err = got_pack_start_privsep_child(pack, packidx);
925 if (err)
926 return err;
928 return request_packed_tree(tree, pack, idx, id);
931 static const struct got_error *
932 request_tree(struct got_tree_object **tree, struct got_repository *repo,
933 int fd, struct got_object_id *id)
935 const struct got_error *err = NULL;
936 struct imsgbuf *ibuf;
938 ibuf = repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TREE].ibuf;
940 err = got_privsep_send_tree_req(ibuf, fd, id, -1);
941 if (err)
942 return err;
944 return got_privsep_recv_tree(tree, ibuf);
947 static const struct got_error *
948 read_tree_privsep(struct got_tree_object **tree, int obj_fd,
949 struct got_object_id *id, struct got_repository *repo)
951 const struct got_error *err;
952 int imsg_fds[2];
953 pid_t pid;
954 struct imsgbuf *ibuf;
956 if (repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TREE].imsg_fd != -1)
957 return request_tree(tree, repo, obj_fd, id);
959 ibuf = calloc(1, sizeof(*ibuf));
960 if (ibuf == NULL)
961 return got_error_from_errno("calloc");
963 if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_fds) == -1) {
964 err = got_error_from_errno("socketpair");
965 free(ibuf);
966 return err;
969 pid = fork();
970 if (pid == -1) {
971 err = got_error_from_errno("fork");
972 free(ibuf);
973 return err;
975 else if (pid == 0) {
976 got_privsep_exec_child(imsg_fds, GOT_PATH_PROG_READ_TREE,
977 repo->path);
978 /* not reached */
981 if (close(imsg_fds[1]) == -1) {
982 err = got_error_from_errno("close");
983 free(ibuf);
984 return err;
986 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TREE].imsg_fd =
987 imsg_fds[0];
988 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TREE].pid = pid;
989 imsg_init(ibuf, imsg_fds[0]);
990 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TREE].ibuf = ibuf;
993 return request_tree(tree, repo, obj_fd, id);
996 static const struct got_error *
997 open_tree(struct got_tree_object **tree, struct got_repository *repo,
998 struct got_object_id *id, int check_cache)
1000 const struct got_error *err = NULL;
1001 struct got_packidx *packidx = NULL;
1002 int idx;
1003 char *path_packfile = NULL;
1005 if (check_cache) {
1006 *tree = got_repo_get_cached_tree(repo, id);
1007 if (*tree != NULL) {
1008 (*tree)->refcnt++;
1009 return NULL;
1011 } else
1012 *tree = NULL;
1014 err = got_repo_search_packidx(&packidx, &idx, repo, id);
1015 if (err == NULL) {
1016 struct got_pack *pack = NULL;
1018 err = got_packidx_get_packfile_path(&path_packfile,
1019 packidx->path_packidx);
1020 if (err)
1021 return err;
1023 pack = got_repo_get_cached_pack(repo, path_packfile);
1024 if (pack == NULL) {
1025 err = got_repo_cache_pack(&pack, repo, path_packfile,
1026 packidx);
1027 if (err)
1028 goto done;
1030 err = read_packed_tree_privsep(tree, pack,
1031 packidx, idx, id);
1032 } else if (err->code == GOT_ERR_NO_OBJ) {
1033 int fd;
1035 err = got_object_open_loose_fd(&fd, id, repo);
1036 if (err)
1037 return err;
1038 err = read_tree_privsep(tree, fd, id, repo);
1041 if (err == NULL) {
1042 (*tree)->refcnt++;
1043 err = got_repo_cache_tree(repo, id, *tree);
1045 done:
1046 free(path_packfile);
1047 return err;
1050 const struct got_error *
1051 got_object_open_as_tree(struct got_tree_object **tree,
1052 struct got_repository *repo, struct got_object_id *id)
1054 *tree = got_repo_get_cached_tree(repo, id);
1055 if (*tree != NULL) {
1056 (*tree)->refcnt++;
1057 return NULL;
1060 return open_tree(tree, repo, id, 0);
1063 const struct got_error *
1064 got_object_tree_open(struct got_tree_object **tree,
1065 struct got_repository *repo, struct got_object *obj)
1067 return open_tree(tree, repo, got_object_get_id(obj), 1);
1070 int
1071 got_object_tree_get_nentries(struct got_tree_object *tree)
1073 return tree->nentries;
1076 struct got_tree_entry *
1077 got_object_tree_get_first_entry(struct got_tree_object *tree)
1079 return got_object_tree_get_entry(tree, 0);
1082 struct got_tree_entry *
1083 got_object_tree_get_last_entry(struct got_tree_object *tree)
1085 return got_object_tree_get_entry(tree, tree->nentries - 1);
1088 struct got_tree_entry *
1089 got_object_tree_get_entry(struct got_tree_object *tree, int i)
1091 if (i < 0 || i >= tree->nentries)
1092 return NULL;
1093 return &tree->entries[i];
1096 mode_t
1097 got_tree_entry_get_mode(struct got_tree_entry *te)
1099 return te->mode;
1102 const char *
1103 got_tree_entry_get_name(struct got_tree_entry *te)
1105 return &te->name[0];
1108 struct got_object_id *
1109 got_tree_entry_get_id(struct got_tree_entry *te)
1111 return &te->id;
1114 const struct got_error *
1115 got_object_blob_read_to_str(char **s, struct got_blob_object *blob)
1117 const struct got_error *err = NULL;
1118 size_t len, totlen, hdrlen, offset;
1120 *s = NULL;
1122 hdrlen = got_object_blob_get_hdrlen(blob);
1123 totlen = 0;
1124 offset = 0;
1125 do {
1126 char *p;
1128 err = got_object_blob_read_block(&len, blob);
1129 if (err)
1130 return err;
1132 if (len == 0)
1133 break;
1135 totlen += len - hdrlen;
1136 p = realloc(*s, totlen + 1);
1137 if (p == NULL) {
1138 err = got_error_from_errno("realloc");
1139 free(*s);
1140 *s = NULL;
1141 return err;
1143 *s = p;
1144 /* Skip blob object header first time around. */
1145 memcpy(*s + offset,
1146 got_object_blob_get_read_buf(blob) + hdrlen, len - hdrlen);
1147 hdrlen = 0;
1148 offset = totlen;
1149 } while (len > 0);
1151 (*s)[totlen] = '\0';
1152 return NULL;
1155 const struct got_error *
1156 got_tree_entry_get_symlink_target(char **link_target, struct got_tree_entry *te,
1157 struct got_repository *repo)
1159 const struct got_error *err = NULL;
1160 struct got_blob_object *blob = NULL;
1161 int fd = -1;
1163 *link_target = NULL;
1165 if (!got_object_tree_entry_is_symlink(te))
1166 return got_error(GOT_ERR_TREE_ENTRY_TYPE);
1168 fd = got_opentempfd();
1169 if (fd == -1) {
1170 err = got_error_from_errno("got_opentempfd");
1171 goto done;
1174 err = got_object_open_as_blob(&blob, repo,
1175 got_tree_entry_get_id(te), PATH_MAX, fd);
1176 if (err)
1177 goto done;
1179 err = got_object_blob_read_to_str(link_target, blob);
1180 done:
1181 if (fd != -1 && close(fd) == -1 && err == NULL)
1182 err = got_error_from_errno("close");
1183 if (blob)
1184 got_object_blob_close(blob);
1185 if (err) {
1186 free(*link_target);
1187 *link_target = NULL;
1189 return err;
1192 int
1193 got_tree_entry_get_index(struct got_tree_entry *te)
1195 return te->idx;
1198 struct got_tree_entry *
1199 got_tree_entry_get_next(struct got_tree_object *tree,
1200 struct got_tree_entry *te)
1202 return got_object_tree_get_entry(tree, te->idx + 1);
1205 struct got_tree_entry *
1206 got_tree_entry_get_prev(struct got_tree_object *tree,
1207 struct got_tree_entry *te)
1209 return got_object_tree_get_entry(tree, te->idx - 1);
1212 static const struct got_error *
1213 request_packed_blob(uint8_t **outbuf, size_t *size, size_t *hdrlen, int outfd,
1214 struct got_pack *pack, struct got_packidx *packidx, int idx,
1215 struct got_object_id *id)
1217 const struct got_error *err = NULL;
1218 struct imsgbuf *ibuf = pack->privsep_child->ibuf;
1219 int outfd_child;
1221 err = pack_child_send_tempfiles(ibuf, pack);
1222 if (err)
1223 return err;
1225 outfd_child = dup(outfd);
1226 if (outfd_child == -1)
1227 return got_error_from_errno("dup");
1229 err = got_privsep_send_blob_req(pack->privsep_child->ibuf, -1, id, idx);
1230 if (err)
1231 return err;
1233 err = got_privsep_send_blob_outfd(pack->privsep_child->ibuf,
1234 outfd_child);
1235 if (err) {
1236 return err;
1239 err = got_privsep_recv_blob(outbuf, size, hdrlen,
1240 pack->privsep_child->ibuf);
1241 if (err)
1242 return err;
1244 if (lseek(outfd, SEEK_SET, 0) == -1)
1245 err = got_error_from_errno("lseek");
1247 return err;
1250 static const struct got_error *
1251 read_packed_blob_privsep(uint8_t **outbuf, size_t *size, size_t *hdrlen,
1252 int outfd, struct got_pack *pack, struct got_packidx *packidx, int idx,
1253 struct got_object_id *id)
1255 const struct got_error *err = NULL;
1257 if (pack->privsep_child == NULL) {
1258 err = got_pack_start_privsep_child(pack, packidx);
1259 if (err)
1260 return err;
1263 return request_packed_blob(outbuf, size, hdrlen, outfd, pack, packidx,
1264 idx, id);
1267 static const struct got_error *
1268 request_blob(uint8_t **outbuf, size_t *size, size_t *hdrlen, int outfd,
1269 int infd, struct got_object_id *id, struct imsgbuf *ibuf)
1271 const struct got_error *err = NULL;
1272 int outfd_child;
1274 outfd_child = dup(outfd);
1275 if (outfd_child == -1)
1276 return got_error_from_errno("dup");
1278 err = got_privsep_send_blob_req(ibuf, infd, id, -1);
1279 if (err)
1280 return err;
1282 err = got_privsep_send_blob_outfd(ibuf, outfd_child);
1283 if (err)
1284 return err;
1286 err = got_privsep_recv_blob(outbuf, size, hdrlen, ibuf);
1287 if (err)
1288 return err;
1290 if (lseek(outfd, SEEK_SET, 0) == -1)
1291 return got_error_from_errno("lseek");
1293 return err;
1296 static const struct got_error *
1297 read_blob_privsep(uint8_t **outbuf, size_t *size, size_t *hdrlen,
1298 int outfd, int infd, struct got_object_id *id, struct got_repository *repo)
1300 const struct got_error *err;
1301 int imsg_fds[2];
1302 pid_t pid;
1303 struct imsgbuf *ibuf;
1305 if (repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_BLOB].imsg_fd != -1) {
1306 ibuf = repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_BLOB].ibuf;
1307 return request_blob(outbuf, size, hdrlen, outfd, infd, id,
1308 ibuf);
1311 ibuf = calloc(1, sizeof(*ibuf));
1312 if (ibuf == NULL)
1313 return got_error_from_errno("calloc");
1315 if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_fds) == -1) {
1316 err = got_error_from_errno("socketpair");
1317 free(ibuf);
1318 return err;
1321 pid = fork();
1322 if (pid == -1) {
1323 err = got_error_from_errno("fork");
1324 free(ibuf);
1325 return err;
1327 else if (pid == 0) {
1328 got_privsep_exec_child(imsg_fds, GOT_PATH_PROG_READ_BLOB,
1329 repo->path);
1330 /* not reached */
1333 if (close(imsg_fds[1]) == -1) {
1334 err = got_error_from_errno("close");
1335 free(ibuf);
1336 return err;
1338 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_BLOB].imsg_fd =
1339 imsg_fds[0];
1340 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_BLOB].pid = pid;
1341 imsg_init(ibuf, imsg_fds[0]);
1342 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_BLOB].ibuf = ibuf;
1344 return request_blob(outbuf, size, hdrlen, outfd, infd, id, ibuf);
1347 static const struct got_error *
1348 open_blob(struct got_blob_object **blob, struct got_repository *repo,
1349 struct got_object_id *id, size_t blocksize, int outfd)
1351 const struct got_error *err = NULL;
1352 struct got_packidx *packidx = NULL;
1353 int idx, dfd = -1;
1354 char *path_packfile = NULL;
1355 uint8_t *outbuf;
1356 size_t size, hdrlen;
1357 struct stat sb;
1359 *blob = calloc(1, sizeof(**blob));
1360 if (*blob == NULL)
1361 return got_error_from_errno("calloc");
1363 (*blob)->read_buf = malloc(blocksize);
1364 if ((*blob)->read_buf == NULL) {
1365 err = got_error_from_errno("malloc");
1366 goto done;
1369 if (ftruncate(outfd, 0L) == -1) {
1370 err = got_error_from_errno("ftruncate");
1371 goto done;
1373 if (lseek(outfd, SEEK_SET, 0) == -1) {
1374 err = got_error_from_errno("lseek");
1375 goto done;
1378 err = got_repo_search_packidx(&packidx, &idx, repo, id);
1379 if (err == NULL) {
1380 struct got_pack *pack = NULL;
1382 err = got_packidx_get_packfile_path(&path_packfile,
1383 packidx->path_packidx);
1384 if (err)
1385 goto done;
1387 pack = got_repo_get_cached_pack(repo, path_packfile);
1388 if (pack == NULL) {
1389 err = got_repo_cache_pack(&pack, repo, path_packfile,
1390 packidx);
1391 if (err)
1392 goto done;
1394 err = read_packed_blob_privsep(&outbuf, &size, &hdrlen, outfd,
1395 pack, packidx, idx, id);
1396 } else if (err->code == GOT_ERR_NO_OBJ) {
1397 int infd;
1399 err = got_object_open_loose_fd(&infd, id, repo);
1400 if (err)
1401 goto done;
1402 err = read_blob_privsep(&outbuf, &size, &hdrlen, outfd, infd,
1403 id, repo);
1405 if (err)
1406 goto done;
1408 if (hdrlen > size) {
1409 err = got_error(GOT_ERR_BAD_OBJ_HDR);
1410 goto done;
1413 if (outbuf) {
1414 (*blob)->f = fmemopen(outbuf, size, "rb");
1415 if ((*blob)->f == NULL) {
1416 err = got_error_from_errno("fmemopen");
1417 free(outbuf);
1418 goto done;
1420 (*blob)->data = outbuf;
1421 } else {
1422 if (fstat(outfd, &sb) == -1) {
1423 err = got_error_from_errno("fstat");
1424 goto done;
1427 if (sb.st_size != size) {
1428 err = got_error(GOT_ERR_PRIVSEP_LEN);
1429 goto done;
1432 dfd = dup(outfd);
1433 if (dfd == -1) {
1434 err = got_error_from_errno("dup");
1435 goto done;
1438 (*blob)->f = fdopen(dfd, "rb");
1439 if ((*blob)->f == NULL) {
1440 err = got_error_from_errno("fdopen");
1441 close(dfd);
1442 dfd = -1;
1443 goto done;
1447 (*blob)->hdrlen = hdrlen;
1448 (*blob)->blocksize = blocksize;
1449 memcpy(&(*blob)->id.sha1, id->sha1, SHA1_DIGEST_LENGTH);
1451 done:
1452 free(path_packfile);
1453 if (err) {
1454 if (*blob) {
1455 got_object_blob_close(*blob);
1456 *blob = NULL;
1459 return err;
1462 const struct got_error *
1463 got_object_open_as_blob(struct got_blob_object **blob,
1464 struct got_repository *repo, struct got_object_id *id, size_t blocksize,
1465 int outfd)
1467 return open_blob(blob, repo, id, blocksize, outfd);
1470 const struct got_error *
1471 got_object_blob_open(struct got_blob_object **blob,
1472 struct got_repository *repo, struct got_object *obj, size_t blocksize,
1473 int outfd)
1475 return open_blob(blob, repo, got_object_get_id(obj), blocksize, outfd);
1478 const struct got_error *
1479 got_object_blob_close(struct got_blob_object *blob)
1481 const struct got_error *err = NULL;
1482 free(blob->read_buf);
1483 if (blob->f && fclose(blob->f) == EOF)
1484 err = got_error_from_errno("fclose");
1485 free(blob->data);
1486 free(blob);
1487 return err;
1490 void
1491 got_object_blob_rewind(struct got_blob_object *blob)
1493 if (blob->f)
1494 rewind(blob->f);
1497 char *
1498 got_object_blob_id_str(struct got_blob_object *blob, char *buf, size_t size)
1500 return got_sha1_digest_to_str(blob->id.sha1, buf, size);
1503 size_t
1504 got_object_blob_get_hdrlen(struct got_blob_object *blob)
1506 return blob->hdrlen;
1509 const uint8_t *
1510 got_object_blob_get_read_buf(struct got_blob_object *blob)
1512 return blob->read_buf;
1515 const struct got_error *
1516 got_object_blob_read_block(size_t *outlenp, struct got_blob_object *blob)
1518 size_t n;
1520 n = fread(blob->read_buf, 1, blob->blocksize, blob->f);
1521 if (n == 0 && ferror(blob->f))
1522 return got_ferror(blob->f, GOT_ERR_IO);
1523 *outlenp = n;
1524 return NULL;
1527 const struct got_error *
1528 got_object_blob_dump_to_file(off_t *filesize, int *nlines,
1529 off_t **line_offsets, FILE *outfile, struct got_blob_object *blob)
1531 const struct got_error *err = NULL;
1532 size_t n, len, hdrlen;
1533 const uint8_t *buf;
1534 int i;
1535 const int alloc_chunksz = 512;
1536 size_t nalloc = 0;
1537 off_t off = 0, total_len = 0;
1539 if (line_offsets)
1540 *line_offsets = NULL;
1541 if (filesize)
1542 *filesize = 0;
1543 if (nlines)
1544 *nlines = 0;
1546 hdrlen = got_object_blob_get_hdrlen(blob);
1547 do {
1548 err = got_object_blob_read_block(&len, blob);
1549 if (err)
1550 return err;
1551 if (len == 0)
1552 break;
1553 buf = got_object_blob_get_read_buf(blob);
1554 i = hdrlen;
1555 if (nlines) {
1556 if (line_offsets && *line_offsets == NULL) {
1557 /* Have some data but perhaps no '\n'. */
1558 *nlines = 1;
1559 nalloc = alloc_chunksz;
1560 *line_offsets = calloc(nalloc,
1561 sizeof(**line_offsets));
1562 if (*line_offsets == NULL)
1563 return got_error_from_errno("calloc");
1565 /* Skip forward over end of first line. */
1566 while (i < len) {
1567 if (buf[i] == '\n')
1568 break;
1569 i++;
1572 /* Scan '\n' offsets in remaining chunk of data. */
1573 while (i < len) {
1574 if (buf[i] != '\n') {
1575 i++;
1576 continue;
1578 (*nlines)++;
1579 if (line_offsets && nalloc < *nlines) {
1580 size_t n = *nlines + alloc_chunksz;
1581 off_t *o = recallocarray(*line_offsets,
1582 nalloc, n, sizeof(**line_offsets));
1583 if (o == NULL) {
1584 free(*line_offsets);
1585 *line_offsets = NULL;
1586 return got_error_from_errno(
1587 "recallocarray");
1589 *line_offsets = o;
1590 nalloc = n;
1592 if (line_offsets) {
1593 off = total_len + i - hdrlen + 1;
1594 (*line_offsets)[*nlines - 1] = off;
1596 i++;
1599 /* Skip blob object header first time around. */
1600 n = fwrite(buf + hdrlen, 1, len - hdrlen, outfile);
1601 if (n != len - hdrlen)
1602 return got_ferror(outfile, GOT_ERR_IO);
1603 total_len += len - hdrlen;
1604 hdrlen = 0;
1605 } while (len != 0);
1607 if (fflush(outfile) != 0)
1608 return got_error_from_errno("fflush");
1609 rewind(outfile);
1611 if (filesize)
1612 *filesize = total_len;
1614 return NULL;
1617 static const struct got_error *
1618 request_packed_tag(struct got_tag_object **tag, struct got_pack *pack,
1619 int pack_idx, struct got_object_id *id)
1621 const struct got_error *err = NULL;
1623 err = got_privsep_send_tag_req(pack->privsep_child->ibuf, -1, id,
1624 pack_idx);
1625 if (err)
1626 return err;
1628 return got_privsep_recv_tag(tag, pack->privsep_child->ibuf);
1631 static const struct got_error *
1632 read_packed_tag_privsep(struct got_tag_object **tag,
1633 struct got_pack *pack, struct got_packidx *packidx, int idx,
1634 struct got_object_id *id)
1636 const struct got_error *err = NULL;
1638 if (pack->privsep_child)
1639 return request_packed_tag(tag, pack, idx, id);
1641 err = got_pack_start_privsep_child(pack, packidx);
1642 if (err)
1643 return err;
1645 return request_packed_tag(tag, pack, idx, id);
1648 static const struct got_error *
1649 request_tag(struct got_tag_object **tag, struct got_repository *repo,
1650 int fd, struct got_object_id *id)
1652 const struct got_error *err = NULL;
1653 struct imsgbuf *ibuf;
1655 ibuf = repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TAG].ibuf;
1657 err = got_privsep_send_tag_req(ibuf, fd, id, -1);
1658 if (err)
1659 return err;
1661 return got_privsep_recv_tag(tag, ibuf);
1664 static const struct got_error *
1665 read_tag_privsep(struct got_tag_object **tag, int obj_fd,
1666 struct got_object_id *id, struct got_repository *repo)
1668 const struct got_error *err;
1669 int imsg_fds[2];
1670 pid_t pid;
1671 struct imsgbuf *ibuf;
1673 if (repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TAG].imsg_fd != -1)
1674 return request_tag(tag, repo, obj_fd, id);
1676 ibuf = calloc(1, sizeof(*ibuf));
1677 if (ibuf == NULL)
1678 return got_error_from_errno("calloc");
1680 if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_fds) == -1) {
1681 err = got_error_from_errno("socketpair");
1682 free(ibuf);
1683 return err;
1686 pid = fork();
1687 if (pid == -1) {
1688 err = got_error_from_errno("fork");
1689 free(ibuf);
1690 return err;
1692 else if (pid == 0) {
1693 got_privsep_exec_child(imsg_fds, GOT_PATH_PROG_READ_TAG,
1694 repo->path);
1695 /* not reached */
1698 if (close(imsg_fds[1]) == -1) {
1699 err = got_error_from_errno("close");
1700 free(ibuf);
1701 return err;
1703 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TAG].imsg_fd =
1704 imsg_fds[0];
1705 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TAG].pid = pid;
1706 imsg_init(ibuf, imsg_fds[0]);
1707 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TAG].ibuf = ibuf;
1709 return request_tag(tag, repo, obj_fd, id);
1712 static const struct got_error *
1713 open_tag(struct got_tag_object **tag, struct got_repository *repo,
1714 struct got_object_id *id, int check_cache)
1716 const struct got_error *err = NULL;
1717 struct got_packidx *packidx = NULL;
1718 int idx;
1719 char *path_packfile = NULL;
1720 struct got_object *obj = NULL;
1721 int obj_type = GOT_OBJ_TYPE_ANY;
1723 if (check_cache) {
1724 *tag = got_repo_get_cached_tag(repo, id);
1725 if (*tag != NULL) {
1726 (*tag)->refcnt++;
1727 return NULL;
1729 } else
1730 *tag = NULL;
1732 err = got_repo_search_packidx(&packidx, &idx, repo, id);
1733 if (err == NULL) {
1734 struct got_pack *pack = NULL;
1736 err = got_packidx_get_packfile_path(&path_packfile,
1737 packidx->path_packidx);
1738 if (err)
1739 return err;
1741 pack = got_repo_get_cached_pack(repo, path_packfile);
1742 if (pack == NULL) {
1743 err = got_repo_cache_pack(&pack, repo, path_packfile,
1744 packidx);
1745 if (err)
1746 goto done;
1749 /* Beware of "lightweight" tags: Check object type first. */
1750 err = read_packed_object_privsep(&obj, repo, pack, packidx,
1751 idx, id);
1752 if (err)
1753 goto done;
1754 obj_type = obj->type;
1755 got_object_close(obj);
1756 if (obj_type != GOT_OBJ_TYPE_TAG) {
1757 err = got_error(GOT_ERR_OBJ_TYPE);
1758 goto done;
1760 err = read_packed_tag_privsep(tag, pack, packidx, idx, id);
1761 } else if (err->code == GOT_ERR_NO_OBJ) {
1762 int fd;
1764 err = got_object_open_loose_fd(&fd, id, repo);
1765 if (err)
1766 return err;
1767 err = got_object_read_header_privsep(&obj, id, repo, fd);
1768 if (err)
1769 return err;
1770 obj_type = obj->type;
1771 got_object_close(obj);
1772 if (obj_type != GOT_OBJ_TYPE_TAG)
1773 return got_error(GOT_ERR_OBJ_TYPE);
1775 err = got_object_open_loose_fd(&fd, id, repo);
1776 if (err)
1777 return err;
1778 err = read_tag_privsep(tag, fd, id, repo);
1781 if (err == NULL) {
1782 (*tag)->refcnt++;
1783 err = got_repo_cache_tag(repo, id, *tag);
1785 done:
1786 free(path_packfile);
1787 return err;
1790 const struct got_error *
1791 got_object_open_as_tag(struct got_tag_object **tag,
1792 struct got_repository *repo, struct got_object_id *id)
1794 *tag = got_repo_get_cached_tag(repo, id);
1795 if (*tag != NULL) {
1796 (*tag)->refcnt++;
1797 return NULL;
1800 return open_tag(tag, repo, id, 0);
1803 const struct got_error *
1804 got_object_tag_open(struct got_tag_object **tag,
1805 struct got_repository *repo, struct got_object *obj)
1807 return open_tag(tag, repo, got_object_get_id(obj), 1);
1810 const char *
1811 got_object_tag_get_name(struct got_tag_object *tag)
1813 return tag->tag;
1816 int
1817 got_object_tag_get_object_type(struct got_tag_object *tag)
1819 return tag->obj_type;
1822 struct got_object_id *
1823 got_object_tag_get_object_id(struct got_tag_object *tag)
1825 return &tag->id;
1828 time_t
1829 got_object_tag_get_tagger_time(struct got_tag_object *tag)
1831 return tag->tagger_time;
1834 time_t
1835 got_object_tag_get_tagger_gmtoff(struct got_tag_object *tag)
1837 return tag->tagger_gmtoff;
1840 const char *
1841 got_object_tag_get_tagger(struct got_tag_object *tag)
1843 return tag->tagger;
1846 const char *
1847 got_object_tag_get_message(struct got_tag_object *tag)
1849 return tag->tagmsg;
1852 static struct got_tree_entry *
1853 find_entry_by_name(struct got_tree_object *tree, const char *name, size_t len)
1855 int i;
1857 /* Note that tree entries are sorted in strncmp() order. */
1858 for (i = 0; i < tree->nentries; i++) {
1859 struct got_tree_entry *te = &tree->entries[i];
1860 int cmp = strncmp(te->name, name, len);
1861 if (cmp < 0)
1862 continue;
1863 if (cmp > 0)
1864 break;
1865 if (te->name[len] == '\0')
1866 return te;
1868 return NULL;
1871 struct got_tree_entry *
1872 got_object_tree_find_entry(struct got_tree_object *tree, const char *name)
1874 return find_entry_by_name(tree, name, strlen(name));
1877 const struct got_error *
1878 got_object_tree_find_path(struct got_object_id **id, mode_t *mode,
1879 struct got_repository *repo, struct got_tree_object *tree,
1880 const char *path)
1882 const struct got_error *err = NULL;
1883 struct got_tree_object *subtree = NULL;
1884 struct got_tree_entry *te = NULL;
1885 const char *seg, *s;
1886 size_t seglen;
1888 *id = NULL;
1890 s = path;
1891 while (s[0] == '/')
1892 s++;
1893 seg = s;
1894 seglen = 0;
1895 subtree = tree;
1896 while (*s) {
1897 struct got_tree_object *next_tree;
1899 if (*s != '/') {
1900 s++;
1901 seglen++;
1902 if (*s)
1903 continue;
1906 te = find_entry_by_name(subtree, seg, seglen);
1907 if (te == NULL) {
1908 err = got_error_path(path, GOT_ERR_NO_TREE_ENTRY);
1909 goto done;
1912 if (*s == '\0')
1913 break;
1915 seg = s + 1;
1916 seglen = 0;
1917 s++;
1918 if (*s) {
1919 err = got_object_open_as_tree(&next_tree, repo,
1920 &te->id);
1921 te = NULL;
1922 if (err)
1923 goto done;
1924 if (subtree != tree)
1925 got_object_tree_close(subtree);
1926 subtree = next_tree;
1930 if (te) {
1931 *id = got_object_id_dup(&te->id);
1932 if (*id == NULL)
1933 return got_error_from_errno("got_object_id_dup");
1934 if (mode)
1935 *mode = te->mode;
1936 } else
1937 err = got_error_path(path, GOT_ERR_NO_TREE_ENTRY);
1938 done:
1939 if (subtree && subtree != tree)
1940 got_object_tree_close(subtree);
1941 return err;
1943 const struct got_error *
1944 got_object_id_by_path(struct got_object_id **id, struct got_repository *repo,
1945 struct got_commit_object *commit, const char *path)
1947 const struct got_error *err = NULL;
1948 struct got_tree_object *tree = NULL;
1950 *id = NULL;
1952 /* Handle opening of root of commit's tree. */
1953 if (got_path_is_root_dir(path)) {
1954 *id = got_object_id_dup(commit->tree_id);
1955 if (*id == NULL)
1956 err = got_error_from_errno("got_object_id_dup");
1957 } else {
1958 err = got_object_open_as_tree(&tree, repo, commit->tree_id);
1959 if (err)
1960 goto done;
1961 err = got_object_tree_find_path(id, NULL, repo, tree, path);
1963 done:
1964 if (tree)
1965 got_object_tree_close(tree);
1966 return err;
1970 * Normalize file mode bits to avoid false positive tree entry differences
1971 * in case tree entries have unexpected mode bits set.
1973 static mode_t
1974 normalize_mode_for_comparison(mode_t mode)
1977 * For directories, the only relevant bit is the IFDIR bit.
1978 * This allows us to detect paths changing from a directory
1979 * to a file and vice versa.
1981 if (S_ISDIR(mode))
1982 return mode & S_IFDIR;
1985 * For symlinks, the only relevant bit is the IFLNK bit.
1986 * This allows us to detect paths changing from a symlinks
1987 * to a file or directory and vice versa.
1989 if (S_ISLNK(mode))
1990 return mode & S_IFLNK;
1992 /* For files, the only change we care about is the executable bit. */
1993 return mode & S_IXUSR;
1996 const struct got_error *
1997 got_object_tree_path_changed(int *changed,
1998 struct got_tree_object *tree01, struct got_tree_object *tree02,
1999 const char *path, struct got_repository *repo)
2001 const struct got_error *err = NULL;
2002 struct got_tree_object *tree1 = NULL, *tree2 = NULL;
2003 struct got_tree_entry *te1 = NULL, *te2 = NULL;
2004 const char *seg, *s;
2005 size_t seglen;
2007 *changed = 0;
2009 /* We not do support comparing the root path. */
2010 if (got_path_is_root_dir(path))
2011 return got_error_path(path, GOT_ERR_BAD_PATH);
2013 tree1 = tree01;
2014 tree2 = tree02;
2015 s = path;
2016 while (*s == '/')
2017 s++;
2018 seg = s;
2019 seglen = 0;
2020 while (*s) {
2021 struct got_tree_object *next_tree1, *next_tree2;
2022 mode_t mode1, mode2;
2024 if (*s != '/') {
2025 s++;
2026 seglen++;
2027 if (*s)
2028 continue;
2031 te1 = find_entry_by_name(tree1, seg, seglen);
2032 if (te1 == NULL) {
2033 err = got_error(GOT_ERR_NO_OBJ);
2034 goto done;
2037 if (tree2)
2038 te2 = find_entry_by_name(tree2, seg, seglen);
2040 if (te2) {
2041 mode1 = normalize_mode_for_comparison(te1->mode);
2042 mode2 = normalize_mode_for_comparison(te2->mode);
2043 if (mode1 != mode2) {
2044 *changed = 1;
2045 goto done;
2048 if (got_object_id_cmp(&te1->id, &te2->id) == 0) {
2049 *changed = 0;
2050 goto done;
2054 if (*s == '\0') { /* final path element */
2055 *changed = 1;
2056 goto done;
2059 seg = s + 1;
2060 s++;
2061 seglen = 0;
2062 if (*s) {
2063 err = got_object_open_as_tree(&next_tree1, repo,
2064 &te1->id);
2065 te1 = NULL;
2066 if (err)
2067 goto done;
2068 if (tree1 != tree01)
2069 got_object_tree_close(tree1);
2070 tree1 = next_tree1;
2072 if (te2) {
2073 err = got_object_open_as_tree(&next_tree2, repo,
2074 &te2->id);
2075 te2 = NULL;
2076 if (err)
2077 goto done;
2078 if (tree2 != tree02)
2079 got_object_tree_close(tree2);
2080 tree2 = next_tree2;
2081 } else if (tree2) {
2082 if (tree2 != tree02)
2083 got_object_tree_close(tree2);
2084 tree2 = NULL;
2088 done:
2089 if (tree1 && tree1 != tree01)
2090 got_object_tree_close(tree1);
2091 if (tree2 && tree2 != tree02)
2092 got_object_tree_close(tree2);
2093 return err;
2096 const struct got_error *
2097 got_object_tree_entry_dup(struct got_tree_entry **new_te,
2098 struct got_tree_entry *te)
2100 const struct got_error *err = NULL;
2102 *new_te = calloc(1, sizeof(**new_te));
2103 if (*new_te == NULL)
2104 return got_error_from_errno("calloc");
2106 (*new_te)->mode = te->mode;
2107 memcpy((*new_te)->name, te->name, sizeof((*new_te)->name));
2108 memcpy(&(*new_te)->id, &te->id, sizeof((*new_te)->id));
2109 return err;
2112 int
2113 got_object_tree_entry_is_submodule(struct got_tree_entry *te)
2115 return (te->mode & S_IFMT) == (S_IFDIR | S_IFLNK);
2118 int
2119 got_object_tree_entry_is_symlink(struct got_tree_entry *te)
2121 /* S_IFDIR check avoids confusing symlinks with submodules. */
2122 return ((te->mode & (S_IFDIR | S_IFLNK)) == S_IFLNK);
2125 static const struct got_error *
2126 resolve_symlink(char **link_target, const char *path,
2127 struct got_commit_object *commit, struct got_repository *repo)
2129 const struct got_error *err = NULL;
2130 char buf[PATH_MAX];
2131 char *name, *parent_path = NULL;
2132 struct got_object_id *tree_obj_id = NULL;
2133 struct got_tree_object *tree = NULL;
2134 struct got_tree_entry *te = NULL;
2136 *link_target = NULL;
2138 if (strlcpy(buf, path, sizeof(buf)) >= sizeof(buf))
2139 return got_error(GOT_ERR_NO_SPACE);
2141 name = basename(buf);
2142 if (name == NULL)
2143 return got_error_from_errno2("basename", path);
2145 err = got_path_dirname(&parent_path, path);
2146 if (err)
2147 return err;
2149 err = got_object_id_by_path(&tree_obj_id, repo, commit,
2150 parent_path);
2151 if (err) {
2152 if (err->code == GOT_ERR_NO_TREE_ENTRY) {
2153 /* Display the complete path in error message. */
2154 err = got_error_path(path, err->code);
2156 goto done;
2159 err = got_object_open_as_tree(&tree, repo, tree_obj_id);
2160 if (err)
2161 goto done;
2163 te = got_object_tree_find_entry(tree, name);
2164 if (te == NULL) {
2165 err = got_error_path(path, GOT_ERR_NO_TREE_ENTRY);
2166 goto done;
2169 if (got_object_tree_entry_is_symlink(te)) {
2170 err = got_tree_entry_get_symlink_target(link_target, te, repo);
2171 if (err)
2172 goto done;
2173 if (!got_path_is_absolute(*link_target)) {
2174 char *abspath;
2175 if (asprintf(&abspath, "%s/%s", parent_path,
2176 *link_target) == -1) {
2177 err = got_error_from_errno("asprintf");
2178 goto done;
2180 free(*link_target);
2181 *link_target = malloc(PATH_MAX);
2182 if (*link_target == NULL) {
2183 err = got_error_from_errno("malloc");
2184 goto done;
2186 err = got_canonpath(abspath, *link_target, PATH_MAX);
2187 free(abspath);
2188 if (err)
2189 goto done;
2192 done:
2193 free(tree_obj_id);
2194 if (tree)
2195 got_object_tree_close(tree);
2196 if (err) {
2197 free(*link_target);
2198 *link_target = NULL;
2200 return err;
2203 const struct got_error *
2204 got_object_resolve_symlinks(char **link_target, const char *path,
2205 struct got_commit_object *commit, struct got_repository *repo)
2207 const struct got_error *err = NULL;
2208 char *next_target = NULL;
2209 int max_recursion = 40; /* matches Git */
2211 *link_target = NULL;
2213 do {
2214 err = resolve_symlink(&next_target,
2215 *link_target ? *link_target : path, commit, repo);
2216 if (err)
2217 break;
2218 if (next_target) {
2219 free(*link_target);
2220 if (--max_recursion == 0) {
2221 err = got_error_path(path, GOT_ERR_RECURSION);
2222 *link_target = NULL;
2223 break;
2225 *link_target = next_target;
2227 } while (next_target);
2229 return err;
2232 const struct got_error *
2233 got_traverse_packed_commits(struct got_object_id_queue *traversed_commits,
2234 struct got_object_id *commit_id, const char *path,
2235 struct got_repository *repo)
2237 const struct got_error *err = NULL;
2238 struct got_pack *pack = NULL;
2239 struct got_packidx *packidx = NULL;
2240 char *path_packfile = NULL;
2241 struct got_commit_object *changed_commit = NULL;
2242 struct got_object_id *changed_commit_id = NULL;
2243 int idx;
2245 err = got_repo_search_packidx(&packidx, &idx, repo, commit_id);
2246 if (err) {
2247 if (err->code != GOT_ERR_NO_OBJ)
2248 return err;
2249 return NULL;
2252 err = got_packidx_get_packfile_path(&path_packfile,
2253 packidx->path_packidx);
2254 if (err)
2255 return err;
2257 pack = got_repo_get_cached_pack(repo, path_packfile);
2258 if (pack == NULL) {
2259 err = got_repo_cache_pack(&pack, repo, path_packfile, packidx);
2260 if (err)
2261 goto done;
2264 if (pack->privsep_child == NULL) {
2265 err = got_pack_start_privsep_child(pack, packidx);
2266 if (err)
2267 goto done;
2270 err = got_privsep_send_commit_traversal_request(
2271 pack->privsep_child->ibuf, commit_id, idx, path);
2272 if (err)
2273 goto done;
2275 err = got_privsep_recv_traversed_commits(&changed_commit,
2276 &changed_commit_id, traversed_commits, pack->privsep_child->ibuf);
2277 if (err)
2278 goto done;
2280 if (changed_commit) {
2282 * Cache the commit in which the path was changed.
2283 * This commit might be opened again soon.
2285 changed_commit->refcnt++;
2286 err = got_repo_cache_commit(repo, changed_commit_id,
2287 changed_commit);
2288 got_object_commit_close(changed_commit);
2290 done:
2291 free(path_packfile);
2292 free(changed_commit_id);
2293 return err;
2296 const struct got_error *
2297 got_object_enumerate(int *found_all_objects,
2298 got_object_enumerate_commit_cb cb_commit,
2299 got_object_enumerate_tree_cb cb_tree, void *cb_arg,
2300 struct got_object_id **ours, int nours,
2301 struct got_object_id **theirs, int ntheirs,
2302 struct got_packidx *packidx, struct got_repository *repo)
2304 const struct got_error *err = NULL;
2305 struct got_pack *pack;
2306 char *path_packfile = NULL;
2308 err = got_packidx_get_packfile_path(&path_packfile,
2309 packidx->path_packidx);
2310 if (err)
2311 return err;
2313 pack = got_repo_get_cached_pack(repo, path_packfile);
2314 if (pack == NULL) {
2315 err = got_repo_cache_pack(&pack, repo, path_packfile, packidx);
2316 if (err)
2317 goto done;
2320 if (pack->privsep_child == NULL) {
2321 err = got_pack_start_privsep_child(pack, packidx);
2322 if (err)
2323 goto done;
2326 err = got_privsep_send_object_enumeration_request(
2327 pack->privsep_child->ibuf);
2328 if (err)
2329 goto done;
2331 err = got_privsep_send_object_idlist(pack->privsep_child->ibuf,
2332 ours, nours);
2333 if (err)
2334 goto done;
2335 err = got_privsep_send_object_idlist_done(pack->privsep_child->ibuf);
2336 if (err)
2337 goto done;
2339 err = got_privsep_send_object_idlist(pack->privsep_child->ibuf,
2340 theirs, ntheirs);
2341 if (err)
2342 goto done;
2343 err = got_privsep_send_object_idlist_done(pack->privsep_child->ibuf);
2344 if (err)
2345 goto done;
2347 err = got_privsep_recv_enumerated_objects(found_all_objects,
2348 pack->privsep_child->ibuf, cb_commit, cb_tree, cb_arg, repo);
2349 done:
2350 free(path_packfile);
2351 return err;