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/queue.h>
20 #include <sys/tree.h>
21 #include <sys/uio.h>
22 #include <sys/socket.h>
23 #include <sys/wait.h>
24 #include <sys/resource.h>
26 #include <errno.h>
27 #include <fcntl.h>
28 #include <stdio.h>
29 #include <stdlib.h>
30 #include <string.h>
31 #include <stdint.h>
32 #include <sha1.h>
33 #include <unistd.h>
34 #include <zlib.h>
35 #include <ctype.h>
36 #include <libgen.h>
37 #include <limits.h>
38 #include <imsg.h>
39 #include <time.h>
41 #include "got_error.h"
42 #include "got_object.h"
43 #include "got_repository.h"
44 #include "got_opentemp.h"
45 #include "got_path.h"
47 #include "got_lib_sha1.h"
48 #include "got_lib_delta.h"
49 #include "got_lib_inflate.h"
50 #include "got_lib_object.h"
51 #include "got_lib_privsep.h"
52 #include "got_lib_object_idcache.h"
53 #include "got_lib_object_cache.h"
54 #include "got_lib_object_parse.h"
55 #include "got_lib_pack.h"
56 #include "got_lib_repository.h"
58 #ifndef MIN
59 #define MIN(_a,_b) ((_a) < (_b) ? (_a) : (_b))
60 #endif
62 struct got_object_id *
63 got_object_get_id(struct got_object *obj)
64 {
65 return &obj->id;
66 }
68 const struct got_error *
69 got_object_get_id_str(char **outbuf, struct got_object *obj)
70 {
71 return got_object_id_str(outbuf, &obj->id);
72 }
74 const struct got_error *
75 got_object_get_type(int *type, struct got_repository *repo,
76 struct got_object_id *id)
77 {
78 const struct got_error *err = NULL;
79 struct got_object *obj;
81 err = got_object_open(&obj, repo, id);
82 if (err)
83 return err;
85 switch (obj->type) {
86 case GOT_OBJ_TYPE_COMMIT:
87 case GOT_OBJ_TYPE_TREE:
88 case GOT_OBJ_TYPE_BLOB:
89 case GOT_OBJ_TYPE_TAG:
90 *type = obj->type;
91 break;
92 default:
93 err = got_error(GOT_ERR_OBJ_TYPE);
94 break;
95 }
97 got_object_close(obj);
98 return err;
99 }
101 const struct got_error *
102 got_object_get_path(char **path, struct got_object_id *id,
103 struct got_repository *repo)
105 const struct got_error *err = NULL;
106 char *hex = NULL;
107 char *path_objects;
109 *path = NULL;
111 path_objects = got_repo_get_path_objects(repo);
112 if (path_objects == NULL)
113 return got_error_from_errno("got_repo_get_path_objects");
115 err = got_object_id_str(&hex, id);
116 if (err)
117 goto done;
119 if (asprintf(path, "%s/%.2x/%s", path_objects,
120 id->sha1[0], hex + 2) == -1)
121 err = got_error_from_errno("asprintf");
123 done:
124 free(hex);
125 free(path_objects);
126 return err;
129 const struct got_error *
130 got_object_open_loose_fd(int *fd, struct got_object_id *id,
131 struct got_repository *repo)
133 const struct got_error *err = NULL;
134 char *path;
136 err = got_object_get_path(&path, id, repo);
137 if (err)
138 return err;
139 *fd = open(path, O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
140 if (*fd == -1) {
141 err = got_error_from_errno2("open", path);
142 goto done;
144 done:
145 free(path);
146 return err;
149 static const struct got_error *
150 request_packed_object(struct got_object **obj, struct got_pack *pack, int idx,
151 struct got_object_id *id)
153 const struct got_error *err = NULL;
154 struct imsgbuf *ibuf = pack->privsep_child->ibuf;
156 err = got_privsep_send_packed_obj_req(ibuf, idx, id);
157 if (err)
158 return err;
160 err = got_privsep_recv_obj(obj, ibuf);
161 if (err)
162 return err;
164 memcpy(&(*obj)->id, id, sizeof((*obj)->id));
166 return NULL;
169 /* Create temporary files used during delta application. */
170 static const struct got_error *
171 pack_child_send_tempfiles(struct imsgbuf *ibuf, struct got_pack *pack)
173 const struct got_error *err;
174 int basefd, accumfd;
176 /*
177 * For performance reasons, the child will keep reusing the
178 * same temporary files during every object request.
179 * Opening and closing new files for every object request is
180 * too expensive during operations such as 'gotadmin pack'.
181 */
182 if (pack->child_has_tempfiles)
183 return NULL;
185 basefd = got_opentempfd();
186 if (basefd == -1)
187 return got_error_from_errno("got_opentempfd");
189 err = got_privsep_send_tmpfd(ibuf, basefd);
190 if (err)
191 return err;
193 accumfd = got_opentempfd();
194 if (accumfd == -1)
195 return got_error_from_errno("got_opentempfd");
197 err = got_privsep_send_tmpfd(ibuf, accumfd);
198 if (err)
199 return err;
201 pack->child_has_tempfiles = 1;
202 return NULL;
205 static const struct got_error *
206 request_packed_object_raw(uint8_t **outbuf, off_t *size, size_t *hdrlen,
207 int outfd, struct got_pack *pack, int idx, struct got_object_id *id)
209 const struct got_error *err = NULL;
210 struct imsgbuf *ibuf = pack->privsep_child->ibuf;
211 int outfd_child;
213 err = pack_child_send_tempfiles(ibuf, pack);
214 if (err)
215 return err;
217 outfd_child = dup(outfd);
218 if (outfd_child == -1)
219 return got_error_from_errno("dup");
221 err = got_privsep_send_packed_raw_obj_req(ibuf, idx, id);
222 if (err) {
223 close(outfd_child);
224 return err;
227 err = got_privsep_send_raw_obj_outfd(ibuf, outfd_child);
228 if (err)
229 return err;
231 err = got_privsep_recv_raw_obj(outbuf, size, hdrlen, ibuf);
232 if (err)
233 return err;
235 return NULL;
238 static void
239 set_max_datasize(void)
241 struct rlimit rl;
243 if (getrlimit(RLIMIT_DATA, &rl) != 0)
244 return;
246 rl.rlim_cur = rl.rlim_max;
247 setrlimit(RLIMIT_DATA, &rl);
250 static const struct got_error *
251 start_pack_privsep_child(struct got_pack *pack, struct got_packidx *packidx)
253 const struct got_error *err = NULL;
254 int imsg_fds[2];
255 pid_t pid;
256 struct imsgbuf *ibuf;
258 ibuf = calloc(1, sizeof(*ibuf));
259 if (ibuf == NULL)
260 return got_error_from_errno("calloc");
262 pack->privsep_child = calloc(1, sizeof(*pack->privsep_child));
263 if (pack->privsep_child == NULL) {
264 err = got_error_from_errno("calloc");
265 free(ibuf);
266 return err;
268 pack->child_has_tempfiles = 0;
270 if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_fds) == -1) {
271 err = got_error_from_errno("socketpair");
272 goto done;
275 pid = fork();
276 if (pid == -1) {
277 err = got_error_from_errno("fork");
278 goto done;
279 } else if (pid == 0) {
280 set_max_datasize();
281 got_privsep_exec_child(imsg_fds, GOT_PATH_PROG_READ_PACK,
282 pack->path_packfile);
283 /* not reached */
286 if (close(imsg_fds[1]) == -1)
287 return got_error_from_errno("close");
288 pack->privsep_child->imsg_fd = imsg_fds[0];
289 pack->privsep_child->pid = pid;
290 imsg_init(ibuf, imsg_fds[0]);
291 pack->privsep_child->ibuf = ibuf;
293 err = got_privsep_init_pack_child(ibuf, pack, packidx);
294 if (err) {
295 const struct got_error *child_err;
296 err = got_privsep_send_stop(pack->privsep_child->imsg_fd);
297 child_err = got_privsep_wait_for_child(
298 pack->privsep_child->pid);
299 if (child_err && err == NULL)
300 err = child_err;
302 done:
303 if (err) {
304 free(ibuf);
305 free(pack->privsep_child);
306 pack->privsep_child = NULL;
308 return err;
311 static const struct got_error *
312 read_packed_object_privsep(struct got_object **obj,
313 struct got_repository *repo, struct got_pack *pack,
314 struct got_packidx *packidx, int idx, struct got_object_id *id)
316 const struct got_error *err = NULL;
318 if (pack->privsep_child == NULL) {
319 err = start_pack_privsep_child(pack, packidx);
320 if (err)
321 return err;
324 return request_packed_object(obj, pack, idx, id);
327 static const struct got_error *
328 read_packed_object_raw_privsep(uint8_t **outbuf, off_t *size, size_t *hdrlen,
329 int outfd, struct got_pack *pack, struct got_packidx *packidx, int idx,
330 struct got_object_id *id)
332 const struct got_error *err = NULL;
334 if (pack->privsep_child == NULL) {
335 err = start_pack_privsep_child(pack, packidx);
336 if (err)
337 return err;
340 return request_packed_object_raw(outbuf, size, hdrlen, outfd, pack,
341 idx, id);
344 const struct got_error *
345 got_object_open_packed(struct got_object **obj, struct got_object_id *id,
346 struct got_repository *repo)
348 const struct got_error *err = NULL;
349 struct got_pack *pack = NULL;
350 struct got_packidx *packidx = NULL;
351 int idx;
352 char *path_packfile;
354 err = got_repo_search_packidx(&packidx, &idx, repo, id);
355 if (err)
356 return err;
358 err = got_packidx_get_packfile_path(&path_packfile,
359 packidx->path_packidx);
360 if (err)
361 return err;
363 pack = got_repo_get_cached_pack(repo, path_packfile);
364 if (pack == NULL) {
365 err = got_repo_cache_pack(&pack, repo, path_packfile, packidx);
366 if (err)
367 goto done;
370 err = read_packed_object_privsep(obj, repo, pack, packidx, idx, id);
371 if (err)
372 goto done;
373 done:
374 free(path_packfile);
375 return err;
378 static const struct got_error *
379 request_object(struct got_object **obj, struct got_object_id *id,
380 struct got_repository *repo, int fd)
382 const struct got_error *err = NULL;
383 struct imsgbuf *ibuf;
385 ibuf = repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_OBJECT].ibuf;
387 err = got_privsep_send_obj_req(ibuf, fd, id);
388 if (err)
389 return err;
391 return got_privsep_recv_obj(obj, ibuf);
394 static const struct got_error *
395 request_raw_object(uint8_t **outbuf, off_t *size, size_t *hdrlen, int outfd,
396 struct got_object_id *id, struct got_repository *repo, int infd)
398 const struct got_error *err = NULL;
399 struct imsgbuf *ibuf;
400 int outfd_child;
402 ibuf = repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_OBJECT].ibuf;
404 outfd_child = dup(outfd);
405 if (outfd_child == -1)
406 return got_error_from_errno("dup");
408 err = got_privsep_send_raw_obj_req(ibuf, infd, id);
409 if (err)
410 return err;
412 err = got_privsep_send_raw_obj_outfd(ibuf, outfd_child);
413 if (err)
414 return err;
416 return got_privsep_recv_raw_obj(outbuf, size, hdrlen, ibuf);
419 static const struct got_error *
420 start_read_object_child(struct got_repository *repo)
422 const struct got_error *err = NULL;
423 int imsg_fds[2];
424 pid_t pid;
425 struct imsgbuf *ibuf;
427 ibuf = calloc(1, sizeof(*ibuf));
428 if (ibuf == NULL)
429 return got_error_from_errno("calloc");
431 if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_fds) == -1) {
432 err = got_error_from_errno("socketpair");
433 free(ibuf);
434 return err;
437 pid = fork();
438 if (pid == -1) {
439 err = got_error_from_errno("fork");
440 free(ibuf);
441 return err;
443 else if (pid == 0) {
444 got_privsep_exec_child(imsg_fds, GOT_PATH_PROG_READ_OBJECT,
445 repo->path);
446 /* not reached */
449 if (close(imsg_fds[1]) == -1) {
450 err = got_error_from_errno("close");
451 free(ibuf);
452 return err;
455 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_OBJECT].imsg_fd =
456 imsg_fds[0];
457 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_OBJECT].pid = pid;
458 imsg_init(ibuf, imsg_fds[0]);
459 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_OBJECT].ibuf = ibuf;
461 return NULL;
464 const struct got_error *
465 got_object_read_header_privsep(struct got_object **obj,
466 struct got_object_id *id, struct got_repository *repo, int obj_fd)
468 const struct got_error *err;
470 if (repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_OBJECT].imsg_fd != -1)
471 return request_object(obj, id, repo, obj_fd);
473 err = start_read_object_child(repo);
474 if (err) {
475 close(obj_fd);
476 return err;
479 return request_object(obj, id, repo, obj_fd);
482 static const struct got_error *
483 read_object_raw_privsep(uint8_t **outbuf, off_t *size, size_t *hdrlen,
484 int outfd, struct got_object_id *id, struct got_repository *repo,
485 int obj_fd)
487 const struct got_error *err;
489 if (repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_OBJECT].imsg_fd != -1)
490 return request_raw_object(outbuf, size, hdrlen, outfd, id,
491 repo, obj_fd);
493 err = start_read_object_child(repo);
494 if (err)
495 return err;
497 return request_raw_object(outbuf, size, hdrlen, outfd, id, repo,
498 obj_fd);
501 const struct got_error *
502 got_object_open(struct got_object **obj, struct got_repository *repo,
503 struct got_object_id *id)
505 const struct got_error *err = NULL;
506 int fd;
508 *obj = got_repo_get_cached_object(repo, id);
509 if (*obj != NULL) {
510 (*obj)->refcnt++;
511 return NULL;
514 err = got_object_open_packed(obj, id, repo);
515 if (err && err->code != GOT_ERR_NO_OBJ)
516 return err;
517 if (*obj) {
518 (*obj)->refcnt++;
519 return got_repo_cache_object(repo, id, *obj);
522 err = got_object_open_loose_fd(&fd, id, repo);
523 if (err) {
524 if (err->code == GOT_ERR_ERRNO && errno == ENOENT)
525 err = got_error_no_obj(id);
526 return err;
529 err = got_object_read_header_privsep(obj, id, repo, fd);
530 if (err)
531 return err;
533 memcpy((*obj)->id.sha1, id->sha1, SHA1_DIGEST_LENGTH);
535 (*obj)->refcnt++;
536 return got_repo_cache_object(repo, id, *obj);
539 /* *outfd must be initialized to -1 by caller */
540 const struct got_error *
541 got_object_raw_open(struct got_raw_object **obj, int *outfd,
542 struct got_repository *repo, struct got_object_id *id)
544 const struct got_error *err = NULL;
545 struct got_packidx *packidx = NULL;
546 int idx;
547 uint8_t *outbuf = NULL;
548 off_t size = 0;
549 size_t hdrlen = 0;
550 char *path_packfile = NULL;
552 *obj = got_repo_get_cached_raw_object(repo, id);
553 if (*obj != NULL) {
554 (*obj)->refcnt++;
555 return NULL;
558 if (*outfd == -1) {
559 *outfd = got_opentempfd();
560 if (*outfd == -1)
561 return got_error_from_errno("got_opentempfd");
564 err = got_repo_search_packidx(&packidx, &idx, repo, id);
565 if (err == NULL) {
566 struct got_pack *pack = NULL;
568 err = got_packidx_get_packfile_path(&path_packfile,
569 packidx->path_packidx);
570 if (err)
571 goto done;
573 pack = got_repo_get_cached_pack(repo, path_packfile);
574 if (pack == NULL) {
575 err = got_repo_cache_pack(&pack, repo, path_packfile,
576 packidx);
577 if (err)
578 goto done;
580 err = read_packed_object_raw_privsep(&outbuf, &size, &hdrlen,
581 *outfd, pack, packidx, idx, id);
582 if (err)
583 goto done;
584 } else if (err->code == GOT_ERR_NO_OBJ) {
585 int fd;
587 err = got_object_open_loose_fd(&fd, id, repo);
588 if (err)
589 goto done;
590 err = read_object_raw_privsep(&outbuf, &size, &hdrlen, *outfd,
591 id, repo, fd);
592 if (err)
593 goto done;
596 *obj = calloc(1, sizeof(**obj));
597 if (*obj == NULL) {
598 err = got_error_from_errno("calloc");
599 goto done;
602 if (outbuf) {
603 (*obj)->f = fmemopen(outbuf, hdrlen + size, "r");
604 if ((*obj)->f == NULL) {
605 err = got_error_from_errno("fdopen");
606 goto done;
608 (*obj)->data = outbuf;
609 } else {
610 struct stat sb;
611 if (fstat(*outfd, &sb) == -1) {
612 err = got_error_from_errno("fstat");
613 goto done;
616 if (sb.st_size != hdrlen + size) {
617 err = got_error(GOT_ERR_PRIVSEP_LEN);
618 goto done;
621 (*obj)->f = fdopen(*outfd, "r");
622 if ((*obj)->f == NULL) {
623 err = got_error_from_errno("fdopen");
624 goto done;
626 (*obj)->data = NULL;
627 *outfd = -1;
629 (*obj)->hdrlen = hdrlen;
630 (*obj)->size = size;
631 err = got_repo_cache_raw_object(repo, id, *obj);
632 done:
633 free(path_packfile);
634 if (err) {
635 if (*obj) {
636 got_object_raw_close(*obj);
637 *obj = NULL;
639 free(outbuf);
640 } else
641 (*obj)->refcnt++;
642 return err;
645 const struct got_error *
646 got_object_open_by_id_str(struct got_object **obj, struct got_repository *repo,
647 const char *id_str)
649 struct got_object_id id;
651 if (!got_parse_sha1_digest(id.sha1, id_str))
652 return got_error_path(id_str, GOT_ERR_BAD_OBJ_ID_STR);
654 return got_object_open(obj, repo, &id);
657 const struct got_error *
658 got_object_resolve_id_str(struct got_object_id **id,
659 struct got_repository *repo, const char *id_str)
661 const struct got_error *err = NULL;
662 struct got_object *obj;
664 err = got_object_open_by_id_str(&obj, repo, id_str);
665 if (err)
666 return err;
668 *id = got_object_id_dup(got_object_get_id(obj));
669 got_object_close(obj);
670 if (*id == NULL)
671 return got_error_from_errno("got_object_id_dup");
673 return NULL;
676 static const struct got_error *
677 request_packed_commit(struct got_commit_object **commit, struct got_pack *pack,
678 int pack_idx, struct got_object_id *id)
680 const struct got_error *err = NULL;
682 err = got_privsep_send_commit_req(pack->privsep_child->ibuf, -1, id,
683 pack_idx);
684 if (err)
685 return err;
687 err = got_privsep_recv_commit(commit, pack->privsep_child->ibuf);
688 if (err)
689 return err;
691 (*commit)->flags |= GOT_COMMIT_FLAG_PACKED;
692 return NULL;
695 static const struct got_error *
696 read_packed_commit_privsep(struct got_commit_object **commit,
697 struct got_pack *pack, struct got_packidx *packidx, int idx,
698 struct got_object_id *id)
700 const struct got_error *err = NULL;
702 if (pack->privsep_child)
703 return request_packed_commit(commit, pack, idx, id);
705 err = start_pack_privsep_child(pack, packidx);
706 if (err)
707 return err;
709 return request_packed_commit(commit, pack, idx, id);
712 static const struct got_error *
713 request_commit(struct got_commit_object **commit, struct got_repository *repo,
714 int fd, struct got_object_id *id)
716 const struct got_error *err = NULL;
717 struct imsgbuf *ibuf;
719 ibuf = repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_COMMIT].ibuf;
721 err = got_privsep_send_commit_req(ibuf, fd, id, -1);
722 if (err)
723 return err;
725 return got_privsep_recv_commit(commit, ibuf);
728 static const struct got_error *
729 read_commit_privsep(struct got_commit_object **commit, int obj_fd,
730 struct got_object_id *id, struct got_repository *repo)
732 const struct got_error *err;
733 int imsg_fds[2];
734 pid_t pid;
735 struct imsgbuf *ibuf;
737 if (repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_COMMIT].imsg_fd != -1)
738 return request_commit(commit, repo, obj_fd, id);
740 ibuf = calloc(1, sizeof(*ibuf));
741 if (ibuf == NULL)
742 return got_error_from_errno("calloc");
744 if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_fds) == -1) {
745 err = got_error_from_errno("socketpair");
746 free(ibuf);
747 return err;
750 pid = fork();
751 if (pid == -1) {
752 err = got_error_from_errno("fork");
753 free(ibuf);
754 return err;
756 else if (pid == 0) {
757 got_privsep_exec_child(imsg_fds, GOT_PATH_PROG_READ_COMMIT,
758 repo->path);
759 /* not reached */
762 if (close(imsg_fds[1]) == -1) {
763 err = got_error_from_errno("close");
764 free(ibuf);
765 return err;
767 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_COMMIT].imsg_fd =
768 imsg_fds[0];
769 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_COMMIT].pid = pid;
770 imsg_init(ibuf, imsg_fds[0]);
771 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_COMMIT].ibuf = ibuf;
773 return request_commit(commit, repo, obj_fd, id);
777 static const struct got_error *
778 open_commit(struct got_commit_object **commit,
779 struct got_repository *repo, struct got_object_id *id, int check_cache)
781 const struct got_error *err = NULL;
782 struct got_packidx *packidx = NULL;
783 int idx;
784 char *path_packfile = NULL;
786 if (check_cache) {
787 *commit = got_repo_get_cached_commit(repo, id);
788 if (*commit != NULL) {
789 (*commit)->refcnt++;
790 return NULL;
792 } else
793 *commit = NULL;
795 err = got_repo_search_packidx(&packidx, &idx, repo, id);
796 if (err == NULL) {
797 struct got_pack *pack = NULL;
799 err = got_packidx_get_packfile_path(&path_packfile,
800 packidx->path_packidx);
801 if (err)
802 return err;
804 pack = got_repo_get_cached_pack(repo, path_packfile);
805 if (pack == NULL) {
806 err = got_repo_cache_pack(&pack, repo, path_packfile,
807 packidx);
808 if (err)
809 goto done;
811 err = read_packed_commit_privsep(commit, pack,
812 packidx, idx, id);
813 } else if (err->code == GOT_ERR_NO_OBJ) {
814 int fd;
816 err = got_object_open_loose_fd(&fd, id, repo);
817 if (err)
818 return err;
819 err = read_commit_privsep(commit, fd, id, repo);
822 if (err == NULL) {
823 (*commit)->refcnt++;
824 err = got_repo_cache_commit(repo, id, *commit);
826 done:
827 free(path_packfile);
828 return err;
831 const struct got_error *
832 got_object_open_as_commit(struct got_commit_object **commit,
833 struct got_repository *repo, struct got_object_id *id)
835 *commit = got_repo_get_cached_commit(repo, id);
836 if (*commit != NULL) {
837 (*commit)->refcnt++;
838 return NULL;
841 return open_commit(commit, repo, id, 0);
844 const struct got_error *
845 got_object_commit_open(struct got_commit_object **commit,
846 struct got_repository *repo, struct got_object *obj)
848 return open_commit(commit, repo, got_object_get_id(obj), 1);
851 const struct got_error *
852 got_object_qid_alloc(struct got_object_qid **qid, struct got_object_id *id)
854 const struct got_error *err = NULL;
856 *qid = calloc(1, sizeof(**qid));
857 if (*qid == NULL)
858 return got_error_from_errno("calloc");
860 (*qid)->id = got_object_id_dup(id);
861 if ((*qid)->id == NULL) {
862 err = got_error_from_errno("got_object_id_dup");
863 got_object_qid_free(*qid);
864 *qid = NULL;
865 return err;
868 return NULL;
871 const struct got_error *
872 got_object_id_queue_copy(const struct got_object_id_queue *src,
873 struct got_object_id_queue *dest)
875 const struct got_error *err;
876 struct got_object_qid *qid;
878 STAILQ_FOREACH(qid, src, entry) {
879 struct got_object_qid *new;
880 /*
881 * Deep-copy the object ID only. Let the caller deal
882 * with setting up the new->data pointer if needed.
883 */
884 err = got_object_qid_alloc(&new, qid->id);
885 if (err) {
886 got_object_id_queue_free(dest);
887 return err;
889 STAILQ_INSERT_TAIL(dest, new, entry);
892 return NULL;
895 static const struct got_error *
896 request_packed_tree(struct got_tree_object **tree, struct got_pack *pack,
897 int pack_idx, struct got_object_id *id)
899 const struct got_error *err = NULL;
901 err = got_privsep_send_tree_req(pack->privsep_child->ibuf, -1, id,
902 pack_idx);
903 if (err)
904 return err;
906 return got_privsep_recv_tree(tree, pack->privsep_child->ibuf);
909 static const struct got_error *
910 read_packed_tree_privsep(struct got_tree_object **tree,
911 struct got_pack *pack, struct got_packidx *packidx, int idx,
912 struct got_object_id *id)
914 const struct got_error *err = NULL;
916 if (pack->privsep_child)
917 return request_packed_tree(tree, pack, idx, id);
919 err = start_pack_privsep_child(pack, packidx);
920 if (err)
921 return err;
923 return request_packed_tree(tree, pack, idx, id);
926 static const struct got_error *
927 request_tree(struct got_tree_object **tree, struct got_repository *repo,
928 int fd, struct got_object_id *id)
930 const struct got_error *err = NULL;
931 struct imsgbuf *ibuf;
933 ibuf = repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TREE].ibuf;
935 err = got_privsep_send_tree_req(ibuf, fd, id, -1);
936 if (err)
937 return err;
939 return got_privsep_recv_tree(tree, ibuf);
942 const struct got_error *
943 read_tree_privsep(struct got_tree_object **tree, int obj_fd,
944 struct got_object_id *id, struct got_repository *repo)
946 const struct got_error *err;
947 int imsg_fds[2];
948 pid_t pid;
949 struct imsgbuf *ibuf;
951 if (repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TREE].imsg_fd != -1)
952 return request_tree(tree, repo, obj_fd, id);
954 ibuf = calloc(1, sizeof(*ibuf));
955 if (ibuf == NULL)
956 return got_error_from_errno("calloc");
958 if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_fds) == -1) {
959 err = got_error_from_errno("socketpair");
960 free(ibuf);
961 return err;
964 pid = fork();
965 if (pid == -1) {
966 err = got_error_from_errno("fork");
967 free(ibuf);
968 return err;
970 else if (pid == 0) {
971 got_privsep_exec_child(imsg_fds, GOT_PATH_PROG_READ_TREE,
972 repo->path);
973 /* not reached */
976 if (close(imsg_fds[1]) == -1) {
977 err = got_error_from_errno("close");
978 free(ibuf);
979 return err;
981 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TREE].imsg_fd =
982 imsg_fds[0];
983 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TREE].pid = pid;
984 imsg_init(ibuf, imsg_fds[0]);
985 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TREE].ibuf = ibuf;
988 return request_tree(tree, repo, obj_fd, id);
991 static const struct got_error *
992 open_tree(struct got_tree_object **tree, struct got_repository *repo,
993 struct got_object_id *id, int check_cache)
995 const struct got_error *err = NULL;
996 struct got_packidx *packidx = NULL;
997 int idx;
998 char *path_packfile = NULL;
1000 if (check_cache) {
1001 *tree = got_repo_get_cached_tree(repo, id);
1002 if (*tree != NULL) {
1003 (*tree)->refcnt++;
1004 return NULL;
1006 } else
1007 *tree = NULL;
1009 err = got_repo_search_packidx(&packidx, &idx, repo, id);
1010 if (err == NULL) {
1011 struct got_pack *pack = NULL;
1013 err = got_packidx_get_packfile_path(&path_packfile,
1014 packidx->path_packidx);
1015 if (err)
1016 return err;
1018 pack = got_repo_get_cached_pack(repo, path_packfile);
1019 if (pack == NULL) {
1020 err = got_repo_cache_pack(&pack, repo, path_packfile,
1021 packidx);
1022 if (err)
1023 goto done;
1025 err = read_packed_tree_privsep(tree, pack,
1026 packidx, idx, id);
1027 } else if (err->code == GOT_ERR_NO_OBJ) {
1028 int fd;
1030 err = got_object_open_loose_fd(&fd, id, repo);
1031 if (err)
1032 return err;
1033 err = read_tree_privsep(tree, fd, id, repo);
1036 if (err == NULL) {
1037 (*tree)->refcnt++;
1038 err = got_repo_cache_tree(repo, id, *tree);
1040 done:
1041 free(path_packfile);
1042 return err;
1045 const struct got_error *
1046 got_object_open_as_tree(struct got_tree_object **tree,
1047 struct got_repository *repo, struct got_object_id *id)
1049 *tree = got_repo_get_cached_tree(repo, id);
1050 if (*tree != NULL) {
1051 (*tree)->refcnt++;
1052 return NULL;
1055 return open_tree(tree, repo, id, 0);
1058 const struct got_error *
1059 got_object_tree_open(struct got_tree_object **tree,
1060 struct got_repository *repo, struct got_object *obj)
1062 return open_tree(tree, repo, got_object_get_id(obj), 1);
1065 int
1066 got_object_tree_get_nentries(struct got_tree_object *tree)
1068 return tree->nentries;
1071 struct got_tree_entry *
1072 got_object_tree_get_first_entry(struct got_tree_object *tree)
1074 return got_object_tree_get_entry(tree, 0);
1077 struct got_tree_entry *
1078 got_object_tree_get_last_entry(struct got_tree_object *tree)
1080 return got_object_tree_get_entry(tree, tree->nentries - 1);
1083 struct got_tree_entry *
1084 got_object_tree_get_entry(struct got_tree_object *tree, int i)
1086 if (i < 0 || i >= tree->nentries)
1087 return NULL;
1088 return &tree->entries[i];
1091 mode_t
1092 got_tree_entry_get_mode(struct got_tree_entry *te)
1094 return te->mode;
1097 const char *
1098 got_tree_entry_get_name(struct got_tree_entry *te)
1100 return &te->name[0];
1103 struct got_object_id *
1104 got_tree_entry_get_id(struct got_tree_entry *te)
1106 return &te->id;
1109 const struct got_error *
1110 got_object_blob_read_to_str(char **s, struct got_blob_object *blob)
1112 const struct got_error *err = NULL;
1113 size_t len, totlen, hdrlen, offset;
1115 *s = NULL;
1117 hdrlen = got_object_blob_get_hdrlen(blob);
1118 totlen = 0;
1119 offset = 0;
1120 do {
1121 char *p;
1123 err = got_object_blob_read_block(&len, blob);
1124 if (err)
1125 return err;
1127 if (len == 0)
1128 break;
1130 totlen += len - hdrlen;
1131 p = realloc(*s, totlen + 1);
1132 if (p == NULL) {
1133 err = got_error_from_errno("realloc");
1134 free(*s);
1135 *s = NULL;
1136 return err;
1138 *s = p;
1139 /* Skip blob object header first time around. */
1140 memcpy(*s + offset,
1141 got_object_blob_get_read_buf(blob) + hdrlen, len - hdrlen);
1142 hdrlen = 0;
1143 offset = totlen;
1144 } while (len > 0);
1146 (*s)[totlen] = '\0';
1147 return NULL;
1150 const struct got_error *
1151 got_tree_entry_get_symlink_target(char **link_target, struct got_tree_entry *te,
1152 struct got_repository *repo)
1154 const struct got_error *err = NULL;
1155 struct got_blob_object *blob = NULL;
1157 *link_target = NULL;
1159 if (!got_object_tree_entry_is_symlink(te))
1160 return got_error(GOT_ERR_TREE_ENTRY_TYPE);
1162 err = got_object_open_as_blob(&blob, repo,
1163 got_tree_entry_get_id(te), PATH_MAX);
1164 if (err)
1165 return err;
1167 err = got_object_blob_read_to_str(link_target, blob);
1168 got_object_blob_close(blob);
1169 if (err) {
1170 free(*link_target);
1171 *link_target = NULL;
1173 return err;
1176 int
1177 got_tree_entry_get_index(struct got_tree_entry *te)
1179 return te->idx;
1182 struct got_tree_entry *
1183 got_tree_entry_get_next(struct got_tree_object *tree,
1184 struct got_tree_entry *te)
1186 return got_object_tree_get_entry(tree, te->idx + 1);
1189 struct got_tree_entry *
1190 got_tree_entry_get_prev(struct got_tree_object *tree,
1191 struct got_tree_entry *te)
1193 return got_object_tree_get_entry(tree, te->idx - 1);
1196 static const struct got_error *
1197 request_packed_blob(uint8_t **outbuf, size_t *size, size_t *hdrlen, int outfd,
1198 struct got_pack *pack, struct got_packidx *packidx, int idx,
1199 struct got_object_id *id)
1201 const struct got_error *err = NULL;
1202 struct imsgbuf *ibuf = pack->privsep_child->ibuf;
1203 int outfd_child;
1205 err = pack_child_send_tempfiles(ibuf, pack);
1206 if (err)
1207 return err;
1209 outfd_child = dup(outfd);
1210 if (outfd_child == -1)
1211 return got_error_from_errno("dup");
1213 err = got_privsep_send_blob_req(pack->privsep_child->ibuf, -1, id, idx);
1214 if (err)
1215 return err;
1217 err = got_privsep_send_blob_outfd(pack->privsep_child->ibuf,
1218 outfd_child);
1219 if (err) {
1220 return err;
1223 err = got_privsep_recv_blob(outbuf, size, hdrlen,
1224 pack->privsep_child->ibuf);
1225 if (err)
1226 return err;
1228 if (lseek(outfd, SEEK_SET, 0) == -1)
1229 err = got_error_from_errno("lseek");
1231 return err;
1234 static const struct got_error *
1235 read_packed_blob_privsep(uint8_t **outbuf, size_t *size, size_t *hdrlen,
1236 int outfd, struct got_pack *pack, struct got_packidx *packidx, int idx,
1237 struct got_object_id *id)
1239 const struct got_error *err = NULL;
1241 if (pack->privsep_child == NULL) {
1242 err = start_pack_privsep_child(pack, packidx);
1243 if (err)
1244 return err;
1247 return request_packed_blob(outbuf, size, hdrlen, outfd, pack, packidx,
1248 idx, id);
1251 static const struct got_error *
1252 request_blob(uint8_t **outbuf, size_t *size, size_t *hdrlen, int outfd,
1253 int infd, struct got_object_id *id, struct imsgbuf *ibuf)
1255 const struct got_error *err = NULL;
1256 int outfd_child;
1258 outfd_child = dup(outfd);
1259 if (outfd_child == -1)
1260 return got_error_from_errno("dup");
1262 err = got_privsep_send_blob_req(ibuf, infd, id, -1);
1263 if (err)
1264 return err;
1266 err = got_privsep_send_blob_outfd(ibuf, outfd_child);
1267 if (err)
1268 return err;
1270 err = got_privsep_recv_blob(outbuf, size, hdrlen, ibuf);
1271 if (err)
1272 return err;
1274 if (lseek(outfd, SEEK_SET, 0) == -1)
1275 return got_error_from_errno("lseek");
1277 return err;
1280 static const struct got_error *
1281 read_blob_privsep(uint8_t **outbuf, size_t *size, size_t *hdrlen,
1282 int outfd, int infd, struct got_object_id *id, struct got_repository *repo)
1284 const struct got_error *err;
1285 int imsg_fds[2];
1286 pid_t pid;
1287 struct imsgbuf *ibuf;
1289 if (repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_BLOB].imsg_fd != -1) {
1290 ibuf = repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_BLOB].ibuf;
1291 return request_blob(outbuf, size, hdrlen, outfd, infd, id,
1292 ibuf);
1295 ibuf = calloc(1, sizeof(*ibuf));
1296 if (ibuf == NULL)
1297 return got_error_from_errno("calloc");
1299 if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_fds) == -1) {
1300 err = got_error_from_errno("socketpair");
1301 free(ibuf);
1302 return err;
1305 pid = fork();
1306 if (pid == -1) {
1307 err = got_error_from_errno("fork");
1308 free(ibuf);
1309 return err;
1311 else if (pid == 0) {
1312 got_privsep_exec_child(imsg_fds, GOT_PATH_PROG_READ_BLOB,
1313 repo->path);
1314 /* not reached */
1317 if (close(imsg_fds[1]) == -1) {
1318 err = got_error_from_errno("close");
1319 free(ibuf);
1320 return err;
1322 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_BLOB].imsg_fd =
1323 imsg_fds[0];
1324 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_BLOB].pid = pid;
1325 imsg_init(ibuf, imsg_fds[0]);
1326 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_BLOB].ibuf = ibuf;
1328 return request_blob(outbuf, size, hdrlen, outfd, infd, id, ibuf);
1331 static const struct got_error *
1332 open_blob(struct got_blob_object **blob, struct got_repository *repo,
1333 struct got_object_id *id, size_t blocksize)
1335 const struct got_error *err = NULL;
1336 struct got_packidx *packidx = NULL;
1337 int idx;
1338 char *path_packfile = NULL;
1339 uint8_t *outbuf;
1340 int outfd;
1341 size_t size, hdrlen;
1342 struct stat sb;
1344 *blob = calloc(1, sizeof(**blob));
1345 if (*blob == NULL)
1346 return got_error_from_errno("calloc");
1348 outfd = got_opentempfd();
1349 if (outfd == -1)
1350 return got_error_from_errno("got_opentempfd");
1352 (*blob)->read_buf = malloc(blocksize);
1353 if ((*blob)->read_buf == NULL) {
1354 err = got_error_from_errno("malloc");
1355 goto done;
1358 err = got_repo_search_packidx(&packidx, &idx, repo, id);
1359 if (err == NULL) {
1360 struct got_pack *pack = NULL;
1362 err = got_packidx_get_packfile_path(&path_packfile,
1363 packidx->path_packidx);
1364 if (err)
1365 goto done;
1367 pack = got_repo_get_cached_pack(repo, path_packfile);
1368 if (pack == NULL) {
1369 err = got_repo_cache_pack(&pack, repo, path_packfile,
1370 packidx);
1371 if (err)
1372 goto done;
1374 err = read_packed_blob_privsep(&outbuf, &size, &hdrlen, outfd,
1375 pack, packidx, idx, id);
1376 } else if (err->code == GOT_ERR_NO_OBJ) {
1377 int infd;
1379 err = got_object_open_loose_fd(&infd, id, repo);
1380 if (err)
1381 goto done;
1382 err = read_blob_privsep(&outbuf, &size, &hdrlen, outfd, infd,
1383 id, repo);
1385 if (err)
1386 goto done;
1388 if (hdrlen > size) {
1389 err = got_error(GOT_ERR_BAD_OBJ_HDR);
1390 goto done;
1393 if (outbuf) {
1394 if (close(outfd) == -1 && err == NULL)
1395 err = got_error_from_errno("close");
1396 outfd = -1;
1397 (*blob)->f = fmemopen(outbuf, size, "rb");
1398 if ((*blob)->f == NULL) {
1399 err = got_error_from_errno("fmemopen");
1400 free(outbuf);
1401 goto done;
1403 (*blob)->data = outbuf;
1404 } else {
1405 if (fstat(outfd, &sb) == -1) {
1406 err = got_error_from_errno("fstat");
1407 goto done;
1410 if (sb.st_size != size) {
1411 err = got_error(GOT_ERR_PRIVSEP_LEN);
1412 goto done;
1415 (*blob)->f = fdopen(outfd, "rb");
1416 if ((*blob)->f == NULL) {
1417 err = got_error_from_errno("fdopen");
1418 close(outfd);
1419 outfd = -1;
1420 goto done;
1424 (*blob)->hdrlen = hdrlen;
1425 (*blob)->blocksize = blocksize;
1426 memcpy(&(*blob)->id.sha1, id->sha1, SHA1_DIGEST_LENGTH);
1428 done:
1429 free(path_packfile);
1430 if (err) {
1431 if (*blob) {
1432 got_object_blob_close(*blob);
1433 *blob = NULL;
1434 } else if (outfd != -1)
1435 close(outfd);
1437 return err;
1440 const struct got_error *
1441 got_object_open_as_blob(struct got_blob_object **blob,
1442 struct got_repository *repo, struct got_object_id *id,
1443 size_t blocksize)
1445 return open_blob(blob, repo, id, blocksize);
1448 const struct got_error *
1449 got_object_blob_open(struct got_blob_object **blob,
1450 struct got_repository *repo, struct got_object *obj, size_t blocksize)
1452 return open_blob(blob, repo, got_object_get_id(obj), blocksize);
1455 const struct got_error *
1456 got_object_blob_close(struct got_blob_object *blob)
1458 const struct got_error *err = NULL;
1459 free(blob->read_buf);
1460 if (blob->f && fclose(blob->f) == EOF)
1461 err = got_error_from_errno("fclose");
1462 free(blob->data);
1463 free(blob);
1464 return err;
1467 void
1468 got_object_blob_rewind(struct got_blob_object *blob)
1470 if (blob->f)
1471 rewind(blob->f);
1474 char *
1475 got_object_blob_id_str(struct got_blob_object *blob, char *buf, size_t size)
1477 return got_sha1_digest_to_str(blob->id.sha1, buf, size);
1480 size_t
1481 got_object_blob_get_hdrlen(struct got_blob_object *blob)
1483 return blob->hdrlen;
1486 const uint8_t *
1487 got_object_blob_get_read_buf(struct got_blob_object *blob)
1489 return blob->read_buf;
1492 const struct got_error *
1493 got_object_blob_read_block(size_t *outlenp, struct got_blob_object *blob)
1495 size_t n;
1497 n = fread(blob->read_buf, 1, blob->blocksize, blob->f);
1498 if (n == 0 && ferror(blob->f))
1499 return got_ferror(blob->f, GOT_ERR_IO);
1500 *outlenp = n;
1501 return NULL;
1504 const struct got_error *
1505 got_object_blob_dump_to_file(off_t *filesize, int *nlines,
1506 off_t **line_offsets, FILE *outfile, struct got_blob_object *blob)
1508 const struct got_error *err = NULL;
1509 size_t n, len, hdrlen;
1510 const uint8_t *buf;
1511 int i;
1512 const int alloc_chunksz = 512;
1513 size_t nalloc = 0;
1514 off_t off = 0, total_len = 0;
1516 if (line_offsets)
1517 *line_offsets = NULL;
1518 if (filesize)
1519 *filesize = 0;
1520 if (nlines)
1521 *nlines = 0;
1523 hdrlen = got_object_blob_get_hdrlen(blob);
1524 do {
1525 err = got_object_blob_read_block(&len, blob);
1526 if (err)
1527 return err;
1528 if (len == 0)
1529 break;
1530 buf = got_object_blob_get_read_buf(blob);
1531 i = hdrlen;
1532 if (nlines) {
1533 if (line_offsets && *line_offsets == NULL) {
1534 /* Have some data but perhaps no '\n'. */
1535 *nlines = 1;
1536 nalloc = alloc_chunksz;
1537 *line_offsets = calloc(nalloc,
1538 sizeof(**line_offsets));
1539 if (*line_offsets == NULL)
1540 return got_error_from_errno("calloc");
1542 /* Skip forward over end of first line. */
1543 while (i < len) {
1544 if (buf[i] == '\n')
1545 break;
1546 i++;
1549 /* Scan '\n' offsets in remaining chunk of data. */
1550 while (i < len) {
1551 if (buf[i] != '\n') {
1552 i++;
1553 continue;
1555 (*nlines)++;
1556 if (line_offsets && nalloc < *nlines) {
1557 size_t n = *nlines + alloc_chunksz;
1558 off_t *o = recallocarray(*line_offsets,
1559 nalloc, n, sizeof(**line_offsets));
1560 if (o == NULL) {
1561 free(*line_offsets);
1562 *line_offsets = NULL;
1563 return got_error_from_errno(
1564 "recallocarray");
1566 *line_offsets = o;
1567 nalloc = n;
1569 if (line_offsets) {
1570 off = total_len + i - hdrlen + 1;
1571 (*line_offsets)[*nlines - 1] = off;
1573 i++;
1576 /* Skip blob object header first time around. */
1577 n = fwrite(buf + hdrlen, 1, len - hdrlen, outfile);
1578 if (n != len - hdrlen)
1579 return got_ferror(outfile, GOT_ERR_IO);
1580 total_len += len - hdrlen;
1581 hdrlen = 0;
1582 } while (len != 0);
1584 if (fflush(outfile) != 0)
1585 return got_error_from_errno("fflush");
1586 rewind(outfile);
1588 if (filesize)
1589 *filesize = total_len;
1591 return NULL;
1594 static const struct got_error *
1595 request_packed_tag(struct got_tag_object **tag, struct got_pack *pack,
1596 int pack_idx, struct got_object_id *id)
1598 const struct got_error *err = NULL;
1600 err = got_privsep_send_tag_req(pack->privsep_child->ibuf, -1, id,
1601 pack_idx);
1602 if (err)
1603 return err;
1605 return got_privsep_recv_tag(tag, pack->privsep_child->ibuf);
1608 static const struct got_error *
1609 read_packed_tag_privsep(struct got_tag_object **tag,
1610 struct got_pack *pack, struct got_packidx *packidx, int idx,
1611 struct got_object_id *id)
1613 const struct got_error *err = NULL;
1615 if (pack->privsep_child)
1616 return request_packed_tag(tag, pack, idx, id);
1618 err = start_pack_privsep_child(pack, packidx);
1619 if (err)
1620 return err;
1622 return request_packed_tag(tag, pack, idx, id);
1625 static const struct got_error *
1626 request_tag(struct got_tag_object **tag, struct got_repository *repo,
1627 int fd, struct got_object_id *id)
1629 const struct got_error *err = NULL;
1630 struct imsgbuf *ibuf;
1632 ibuf = repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TAG].ibuf;
1634 err = got_privsep_send_tag_req(ibuf, fd, id, -1);
1635 if (err)
1636 return err;
1638 return got_privsep_recv_tag(tag, ibuf);
1641 static const struct got_error *
1642 read_tag_privsep(struct got_tag_object **tag, int obj_fd,
1643 struct got_object_id *id, struct got_repository *repo)
1645 const struct got_error *err;
1646 int imsg_fds[2];
1647 pid_t pid;
1648 struct imsgbuf *ibuf;
1650 if (repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TAG].imsg_fd != -1)
1651 return request_tag(tag, repo, obj_fd, id);
1653 ibuf = calloc(1, sizeof(*ibuf));
1654 if (ibuf == NULL)
1655 return got_error_from_errno("calloc");
1657 if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_fds) == -1) {
1658 err = got_error_from_errno("socketpair");
1659 free(ibuf);
1660 return err;
1663 pid = fork();
1664 if (pid == -1) {
1665 err = got_error_from_errno("fork");
1666 free(ibuf);
1667 return err;
1669 else if (pid == 0) {
1670 got_privsep_exec_child(imsg_fds, GOT_PATH_PROG_READ_TAG,
1671 repo->path);
1672 /* not reached */
1675 if (close(imsg_fds[1]) == -1) {
1676 err = got_error_from_errno("close");
1677 free(ibuf);
1678 return err;
1680 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TAG].imsg_fd =
1681 imsg_fds[0];
1682 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TAG].pid = pid;
1683 imsg_init(ibuf, imsg_fds[0]);
1684 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TAG].ibuf = ibuf;
1686 return request_tag(tag, repo, obj_fd, id);
1689 static const struct got_error *
1690 open_tag(struct got_tag_object **tag, struct got_repository *repo,
1691 struct got_object_id *id, int check_cache)
1693 const struct got_error *err = NULL;
1694 struct got_packidx *packidx = NULL;
1695 int idx;
1696 char *path_packfile = NULL;
1697 struct got_object *obj = NULL;
1698 int obj_type = GOT_OBJ_TYPE_ANY;
1700 if (check_cache) {
1701 *tag = got_repo_get_cached_tag(repo, id);
1702 if (*tag != NULL) {
1703 (*tag)->refcnt++;
1704 return NULL;
1706 } else
1707 *tag = NULL;
1709 err = got_repo_search_packidx(&packidx, &idx, repo, id);
1710 if (err == NULL) {
1711 struct got_pack *pack = NULL;
1713 err = got_packidx_get_packfile_path(&path_packfile,
1714 packidx->path_packidx);
1715 if (err)
1716 return err;
1718 pack = got_repo_get_cached_pack(repo, path_packfile);
1719 if (pack == NULL) {
1720 err = got_repo_cache_pack(&pack, repo, path_packfile,
1721 packidx);
1722 if (err)
1723 goto done;
1726 /* Beware of "lightweight" tags: Check object type first. */
1727 err = read_packed_object_privsep(&obj, repo, pack, packidx,
1728 idx, id);
1729 if (err)
1730 goto done;
1731 obj_type = obj->type;
1732 got_object_close(obj);
1733 if (obj_type != GOT_OBJ_TYPE_TAG) {
1734 err = got_error(GOT_ERR_OBJ_TYPE);
1735 goto done;
1737 err = read_packed_tag_privsep(tag, pack, packidx, idx, id);
1738 } else if (err->code == GOT_ERR_NO_OBJ) {
1739 int fd;
1741 err = got_object_open_loose_fd(&fd, id, repo);
1742 if (err)
1743 return err;
1744 err = got_object_read_header_privsep(&obj, id, repo, fd);
1745 if (err)
1746 return err;
1747 obj_type = obj->type;
1748 got_object_close(obj);
1749 if (obj_type != GOT_OBJ_TYPE_TAG)
1750 return got_error(GOT_ERR_OBJ_TYPE);
1752 err = got_object_open_loose_fd(&fd, id, repo);
1753 if (err)
1754 return err;
1755 err = read_tag_privsep(tag, fd, id, repo);
1758 if (err == NULL) {
1759 (*tag)->refcnt++;
1760 err = got_repo_cache_tag(repo, id, *tag);
1762 done:
1763 free(path_packfile);
1764 return err;
1767 const struct got_error *
1768 got_object_open_as_tag(struct got_tag_object **tag,
1769 struct got_repository *repo, struct got_object_id *id)
1771 *tag = got_repo_get_cached_tag(repo, id);
1772 if (*tag != NULL) {
1773 (*tag)->refcnt++;
1774 return NULL;
1777 return open_tag(tag, repo, id, 0);
1780 const struct got_error *
1781 got_object_tag_open(struct got_tag_object **tag,
1782 struct got_repository *repo, struct got_object *obj)
1784 return open_tag(tag, repo, got_object_get_id(obj), 1);
1787 const char *
1788 got_object_tag_get_name(struct got_tag_object *tag)
1790 return tag->tag;
1793 int
1794 got_object_tag_get_object_type(struct got_tag_object *tag)
1796 return tag->obj_type;
1799 struct got_object_id *
1800 got_object_tag_get_object_id(struct got_tag_object *tag)
1802 return &tag->id;
1805 time_t
1806 got_object_tag_get_tagger_time(struct got_tag_object *tag)
1808 return tag->tagger_time;
1811 time_t
1812 got_object_tag_get_tagger_gmtoff(struct got_tag_object *tag)
1814 return tag->tagger_gmtoff;
1817 const char *
1818 got_object_tag_get_tagger(struct got_tag_object *tag)
1820 return tag->tagger;
1823 const char *
1824 got_object_tag_get_message(struct got_tag_object *tag)
1826 return tag->tagmsg;
1829 static struct got_tree_entry *
1830 find_entry_by_name(struct got_tree_object *tree, const char *name, size_t len)
1832 int i;
1834 /* Note that tree entries are sorted in strncmp() order. */
1835 for (i = 0; i < tree->nentries; i++) {
1836 struct got_tree_entry *te = &tree->entries[i];
1837 int cmp = strncmp(te->name, name, len);
1838 if (cmp < 0)
1839 continue;
1840 if (cmp > 0)
1841 break;
1842 if (te->name[len] == '\0')
1843 return te;
1845 return NULL;
1848 struct got_tree_entry *
1849 got_object_tree_find_entry(struct got_tree_object *tree, const char *name)
1851 return find_entry_by_name(tree, name, strlen(name));
1854 const struct got_error *
1855 got_object_tree_find_path(struct got_object_id **id, mode_t *mode,
1856 struct got_repository *repo, struct got_tree_object *tree,
1857 const char *path)
1859 const struct got_error *err = NULL;
1860 struct got_tree_object *subtree = NULL;
1861 struct got_tree_entry *te = NULL;
1862 const char *seg, *s;
1863 size_t seglen;
1865 *id = NULL;
1867 s = path;
1868 while (s[0] == '/')
1869 s++;
1870 seg = s;
1871 seglen = 0;
1872 subtree = tree;
1873 while (*s) {
1874 struct got_tree_object *next_tree;
1876 if (*s != '/') {
1877 s++;
1878 seglen++;
1879 if (*s)
1880 continue;
1883 te = find_entry_by_name(subtree, seg, seglen);
1884 if (te == NULL) {
1885 err = got_error_path(path, GOT_ERR_NO_TREE_ENTRY);
1886 goto done;
1889 if (*s == '\0')
1890 break;
1892 seg = s + 1;
1893 seglen = 0;
1894 s++;
1895 if (*s) {
1896 err = got_object_open_as_tree(&next_tree, repo,
1897 &te->id);
1898 te = NULL;
1899 if (err)
1900 goto done;
1901 if (subtree != tree)
1902 got_object_tree_close(subtree);
1903 subtree = next_tree;
1907 if (te) {
1908 *id = got_object_id_dup(&te->id);
1909 if (*id == NULL)
1910 return got_error_from_errno("got_object_id_dup");
1911 if (mode)
1912 *mode = te->mode;
1913 } else
1914 err = got_error_path(path, GOT_ERR_NO_TREE_ENTRY);
1915 done:
1916 if (subtree && subtree != tree)
1917 got_object_tree_close(subtree);
1918 return err;
1920 const struct got_error *
1921 got_object_id_by_path(struct got_object_id **id, struct got_repository *repo,
1922 struct got_object_id *commit_id, const char *path)
1924 const struct got_error *err = NULL;
1925 struct got_commit_object *commit = NULL;
1926 struct got_tree_object *tree = NULL;
1928 *id = NULL;
1930 err = got_object_open_as_commit(&commit, repo, commit_id);
1931 if (err)
1932 goto done;
1934 /* Handle opening of root of commit's tree. */
1935 if (got_path_is_root_dir(path)) {
1936 *id = got_object_id_dup(commit->tree_id);
1937 if (*id == NULL)
1938 err = got_error_from_errno("got_object_id_dup");
1939 } else {
1940 err = got_object_open_as_tree(&tree, repo, commit->tree_id);
1941 if (err)
1942 goto done;
1943 err = got_object_tree_find_path(id, NULL, repo, tree, path);
1945 done:
1946 if (commit)
1947 got_object_commit_close(commit);
1948 if (tree)
1949 got_object_tree_close(tree);
1950 return err;
1954 * Normalize file mode bits to avoid false positive tree entry differences
1955 * in case tree entries have unexpected mode bits set.
1957 static mode_t
1958 normalize_mode_for_comparison(mode_t mode)
1961 * For directories, the only relevant bit is the IFDIR bit.
1962 * This allows us to detect paths changing from a directory
1963 * to a file and vice versa.
1965 if (S_ISDIR(mode))
1966 return mode & S_IFDIR;
1969 * For symlinks, the only relevant bit is the IFLNK bit.
1970 * This allows us to detect paths changing from a symlinks
1971 * to a file or directory and vice versa.
1973 if (S_ISLNK(mode))
1974 return mode & S_IFLNK;
1976 /* For files, the only change we care about is the executable bit. */
1977 return mode & S_IXUSR;
1980 const struct got_error *
1981 got_object_tree_path_changed(int *changed,
1982 struct got_tree_object *tree01, struct got_tree_object *tree02,
1983 const char *path, struct got_repository *repo)
1985 const struct got_error *err = NULL;
1986 struct got_tree_object *tree1 = NULL, *tree2 = NULL;
1987 struct got_tree_entry *te1 = NULL, *te2 = NULL;
1988 const char *seg, *s;
1989 size_t seglen;
1991 *changed = 0;
1993 /* We not do support comparing the root path. */
1994 if (got_path_is_root_dir(path))
1995 return got_error_path(path, GOT_ERR_BAD_PATH);
1997 tree1 = tree01;
1998 tree2 = tree02;
1999 s = path;
2000 while (*s == '/')
2001 s++;
2002 seg = s;
2003 seglen = 0;
2004 while (*s) {
2005 struct got_tree_object *next_tree1, *next_tree2;
2006 mode_t mode1, mode2;
2008 if (*s != '/') {
2009 s++;
2010 seglen++;
2011 if (*s)
2012 continue;
2015 te1 = find_entry_by_name(tree1, seg, seglen);
2016 if (te1 == NULL) {
2017 err = got_error(GOT_ERR_NO_OBJ);
2018 goto done;
2021 if (tree2)
2022 te2 = find_entry_by_name(tree2, seg, seglen);
2024 if (te2) {
2025 mode1 = normalize_mode_for_comparison(te1->mode);
2026 mode2 = normalize_mode_for_comparison(te2->mode);
2027 if (mode1 != mode2) {
2028 *changed = 1;
2029 goto done;
2032 if (got_object_id_cmp(&te1->id, &te2->id) == 0) {
2033 *changed = 0;
2034 goto done;
2038 if (*s == '\0') { /* final path element */
2039 *changed = 1;
2040 goto done;
2043 seg = s + 1;
2044 s++;
2045 seglen = 0;
2046 if (*s) {
2047 err = got_object_open_as_tree(&next_tree1, repo,
2048 &te1->id);
2049 te1 = NULL;
2050 if (err)
2051 goto done;
2052 if (tree1 != tree01)
2053 got_object_tree_close(tree1);
2054 tree1 = next_tree1;
2056 if (te2) {
2057 err = got_object_open_as_tree(&next_tree2, repo,
2058 &te2->id);
2059 te2 = NULL;
2060 if (err)
2061 goto done;
2062 if (tree2 != tree02)
2063 got_object_tree_close(tree2);
2064 tree2 = next_tree2;
2065 } else if (tree2) {
2066 if (tree2 != tree02)
2067 got_object_tree_close(tree2);
2068 tree2 = NULL;
2072 done:
2073 if (tree1 && tree1 != tree01)
2074 got_object_tree_close(tree1);
2075 if (tree2 && tree2 != tree02)
2076 got_object_tree_close(tree2);
2077 return err;
2080 const struct got_error *
2081 got_object_tree_entry_dup(struct got_tree_entry **new_te,
2082 struct got_tree_entry *te)
2084 const struct got_error *err = NULL;
2086 *new_te = calloc(1, sizeof(**new_te));
2087 if (*new_te == NULL)
2088 return got_error_from_errno("calloc");
2090 (*new_te)->mode = te->mode;
2091 memcpy((*new_te)->name, te->name, sizeof((*new_te)->name));
2092 memcpy(&(*new_te)->id, &te->id, sizeof((*new_te)->id));
2093 return err;
2096 int
2097 got_object_tree_entry_is_submodule(struct got_tree_entry *te)
2099 return (te->mode & S_IFMT) == (S_IFDIR | S_IFLNK);
2102 int
2103 got_object_tree_entry_is_symlink(struct got_tree_entry *te)
2105 /* S_IFDIR check avoids confusing symlinks with submodules. */
2106 return ((te->mode & (S_IFDIR | S_IFLNK)) == S_IFLNK);
2109 static const struct got_error *
2110 resolve_symlink(char **link_target, const char *path,
2111 struct got_object_id *commit_id, struct got_repository *repo)
2113 const struct got_error *err = NULL;
2114 char buf[PATH_MAX];
2115 char *name, *parent_path = NULL;
2116 struct got_object_id *tree_obj_id = NULL;
2117 struct got_tree_object *tree = NULL;
2118 struct got_tree_entry *te = NULL;
2120 *link_target = NULL;
2122 if (strlcpy(buf, path, sizeof(buf)) >= sizeof(buf))
2123 return got_error(GOT_ERR_NO_SPACE);
2125 name = basename(buf);
2126 if (name == NULL)
2127 return got_error_from_errno2("basename", path);
2129 err = got_path_dirname(&parent_path, path);
2130 if (err)
2131 return err;
2133 err = got_object_id_by_path(&tree_obj_id, repo, commit_id,
2134 parent_path);
2135 if (err) {
2136 if (err->code == GOT_ERR_NO_TREE_ENTRY) {
2137 /* Display the complete path in error message. */
2138 err = got_error_path(path, err->code);
2140 goto done;
2143 err = got_object_open_as_tree(&tree, repo, tree_obj_id);
2144 if (err)
2145 goto done;
2147 te = got_object_tree_find_entry(tree, name);
2148 if (te == NULL) {
2149 err = got_error_path(path, GOT_ERR_NO_TREE_ENTRY);
2150 goto done;
2153 if (got_object_tree_entry_is_symlink(te)) {
2154 err = got_tree_entry_get_symlink_target(link_target, te, repo);
2155 if (err)
2156 goto done;
2157 if (!got_path_is_absolute(*link_target)) {
2158 char *abspath;
2159 if (asprintf(&abspath, "%s/%s", parent_path,
2160 *link_target) == -1) {
2161 err = got_error_from_errno("asprintf");
2162 goto done;
2164 free(*link_target);
2165 *link_target = malloc(PATH_MAX);
2166 if (*link_target == NULL) {
2167 err = got_error_from_errno("malloc");
2168 goto done;
2170 err = got_canonpath(abspath, *link_target, PATH_MAX);
2171 free(abspath);
2172 if (err)
2173 goto done;
2176 done:
2177 free(tree_obj_id);
2178 if (tree)
2179 got_object_tree_close(tree);
2180 if (err) {
2181 free(*link_target);
2182 *link_target = NULL;
2184 return err;
2187 const struct got_error *
2188 got_object_resolve_symlinks(char **link_target, const char *path,
2189 struct got_object_id *commit_id, struct got_repository *repo)
2191 const struct got_error *err = NULL;
2192 char *next_target = NULL;
2193 int max_recursion = 40; /* matches Git */
2195 *link_target = NULL;
2197 do {
2198 err = resolve_symlink(&next_target,
2199 *link_target ? *link_target : path, commit_id, repo);
2200 if (err)
2201 break;
2202 if (next_target) {
2203 free(*link_target);
2204 if (--max_recursion == 0) {
2205 err = got_error_path(path, GOT_ERR_RECURSION);
2206 *link_target = NULL;
2207 break;
2209 *link_target = next_target;
2211 } while (next_target);
2213 return err;
2216 const struct got_error *
2217 got_traverse_packed_commits(struct got_object_id_queue *traversed_commits,
2218 struct got_object_id *commit_id, const char *path,
2219 struct got_repository *repo)
2221 const struct got_error *err = NULL;
2222 struct got_pack *pack = NULL;
2223 struct got_packidx *packidx = NULL;
2224 char *path_packfile = NULL;
2225 struct got_commit_object *changed_commit = NULL;
2226 struct got_object_id *changed_commit_id = NULL;
2227 int idx;
2229 err = got_repo_search_packidx(&packidx, &idx, repo, commit_id);
2230 if (err) {
2231 if (err->code != GOT_ERR_NO_OBJ)
2232 return err;
2233 return NULL;
2236 err = got_packidx_get_packfile_path(&path_packfile,
2237 packidx->path_packidx);
2238 if (err)
2239 return err;
2241 pack = got_repo_get_cached_pack(repo, path_packfile);
2242 if (pack == NULL) {
2243 err = got_repo_cache_pack(&pack, repo, path_packfile, packidx);
2244 if (err)
2245 goto done;
2248 if (pack->privsep_child == NULL) {
2249 err = start_pack_privsep_child(pack, packidx);
2250 if (err)
2251 goto done;
2254 err = got_privsep_send_commit_traversal_request(
2255 pack->privsep_child->ibuf, commit_id, idx, path);
2256 if (err)
2257 goto done;
2259 err = got_privsep_recv_traversed_commits(&changed_commit,
2260 &changed_commit_id, traversed_commits, pack->privsep_child->ibuf);
2261 if (err)
2262 goto done;
2264 if (changed_commit) {
2266 * Cache the commit in which the path was changed.
2267 * This commit might be opened again soon.
2269 changed_commit->refcnt++;
2270 err = got_repo_cache_commit(repo, changed_commit_id,
2271 changed_commit);
2272 got_object_commit_close(changed_commit);
2274 done:
2275 free(path_packfile);
2276 free(changed_commit_id);
2277 return err;