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>
25 #include <sys/mman.h>
27 #include <errno.h>
28 #include <fcntl.h>
29 #include <stdio.h>
30 #include <stdlib.h>
31 #include <string.h>
32 #include <stdint.h>
33 #include <sha1.h>
34 #include <unistd.h>
35 #include <zlib.h>
36 #include <ctype.h>
37 #include <libgen.h>
38 #include <limits.h>
39 #include <imsg.h>
40 #include <time.h>
42 #include "got_error.h"
43 #include "got_object.h"
44 #include "got_repository.h"
45 #include "got_opentemp.h"
46 #include "got_path.h"
48 #include "got_lib_sha1.h"
49 #include "got_lib_delta.h"
50 #include "got_lib_inflate.h"
51 #include "got_lib_object.h"
52 #include "got_lib_privsep.h"
53 #include "got_lib_object_idcache.h"
54 #include "got_lib_object_cache.h"
55 #include "got_lib_object_parse.h"
56 #include "got_lib_pack.h"
57 #include "got_lib_repository.h"
59 #ifndef MIN
60 #define MIN(_a,_b) ((_a) < (_b) ? (_a) : (_b))
61 #endif
63 struct got_object_id *
64 got_object_get_id(struct got_object *obj)
65 {
66 return &obj->id;
67 }
69 const struct got_error *
70 got_object_get_id_str(char **outbuf, struct got_object *obj)
71 {
72 return got_object_id_str(outbuf, &obj->id);
73 }
75 const struct got_error *
76 got_object_get_type(int *type, struct got_repository *repo,
77 struct got_object_id *id)
78 {
79 const struct got_error *err = NULL;
80 struct got_object *obj;
82 err = got_object_open(&obj, repo, id);
83 if (err)
84 return err;
86 switch (obj->type) {
87 case GOT_OBJ_TYPE_COMMIT:
88 case GOT_OBJ_TYPE_TREE:
89 case GOT_OBJ_TYPE_BLOB:
90 case GOT_OBJ_TYPE_TAG:
91 *type = obj->type;
92 break;
93 default:
94 err = got_error(GOT_ERR_OBJ_TYPE);
95 break;
96 }
98 got_object_close(obj);
99 return err;
102 const struct got_error *
103 got_object_get_path(char **path, struct got_object_id *id,
104 struct got_repository *repo)
106 const struct got_error *err = NULL;
107 char *hex = NULL;
108 char *path_objects;
110 *path = NULL;
112 path_objects = got_repo_get_path_objects(repo);
113 if (path_objects == NULL)
114 return got_error_from_errno("got_repo_get_path_objects");
116 err = got_object_id_str(&hex, id);
117 if (err)
118 goto done;
120 if (asprintf(path, "%s/%.2x/%s", path_objects,
121 id->sha1[0], hex + 2) == -1)
122 err = got_error_from_errno("asprintf");
124 done:
125 free(hex);
126 free(path_objects);
127 return err;
130 const struct got_error *
131 got_object_open_loose_fd(int *fd, struct got_object_id *id,
132 struct got_repository *repo)
134 const struct got_error *err = NULL;
135 char *path;
137 err = got_object_get_path(&path, id, repo);
138 if (err)
139 return err;
140 *fd = open(path, O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
141 if (*fd == -1) {
142 err = got_error_from_errno2("open", path);
143 goto done;
145 done:
146 free(path);
147 return err;
150 static const struct got_error *
151 request_packed_object(struct got_object **obj, struct got_pack *pack, int idx,
152 struct got_object_id *id)
154 const struct got_error *err = NULL;
155 struct imsgbuf *ibuf = pack->privsep_child->ibuf;
157 err = got_privsep_send_packed_obj_req(ibuf, idx, id);
158 if (err)
159 return err;
161 err = got_privsep_recv_obj(obj, ibuf);
162 if (err)
163 return err;
165 memcpy(&(*obj)->id, id, sizeof((*obj)->id));
167 return NULL;
170 /* Create temporary files used during delta application. */
171 static const struct got_error *
172 pack_child_send_tempfiles(struct imsgbuf *ibuf, struct got_pack *pack)
174 const struct got_error *err;
175 int basefd, accumfd;
177 /*
178 * For performance reasons, the child will keep reusing the
179 * same temporary files during every object request.
180 * Opening and closing new files for every object request is
181 * too expensive during operations such as 'gotadmin pack'.
182 */
183 if (pack->child_has_tempfiles)
184 return NULL;
186 basefd = got_opentempfd();
187 if (basefd == -1)
188 return got_error_from_errno("got_opentempfd");
190 err = got_privsep_send_tmpfd(ibuf, basefd);
191 if (err)
192 return err;
194 accumfd = got_opentempfd();
195 if (accumfd == -1)
196 return got_error_from_errno("got_opentempfd");
198 err = got_privsep_send_tmpfd(ibuf, accumfd);
199 if (err)
200 return err;
202 pack->child_has_tempfiles = 1;
203 return NULL;
206 static const struct got_error *
207 request_packed_object_raw(uint8_t **outbuf, off_t *size, size_t *hdrlen,
208 int outfd, struct got_pack *pack, int idx, struct got_object_id *id)
210 const struct got_error *err = NULL;
211 struct imsgbuf *ibuf = pack->privsep_child->ibuf;
212 int outfd_child;
214 err = pack_child_send_tempfiles(ibuf, pack);
215 if (err)
216 return err;
218 outfd_child = dup(outfd);
219 if (outfd_child == -1)
220 return got_error_from_errno("dup");
222 err = got_privsep_send_packed_raw_obj_req(ibuf, idx, id);
223 if (err) {
224 close(outfd_child);
225 return err;
228 err = got_privsep_send_raw_obj_outfd(ibuf, outfd_child);
229 if (err)
230 return err;
232 err = got_privsep_recv_raw_obj(outbuf, size, hdrlen, ibuf);
233 if (err)
234 return err;
236 return NULL;
239 static void
240 set_max_datasize(void)
242 struct rlimit rl;
244 if (getrlimit(RLIMIT_DATA, &rl) != 0)
245 return;
247 rl.rlim_cur = rl.rlim_max;
248 setrlimit(RLIMIT_DATA, &rl);
251 static const struct got_error *
252 start_pack_privsep_child(struct got_pack *pack, struct got_packidx *packidx)
254 const struct got_error *err = NULL;
255 int imsg_fds[2];
256 pid_t pid;
257 struct imsgbuf *ibuf;
259 ibuf = calloc(1, sizeof(*ibuf));
260 if (ibuf == NULL)
261 return got_error_from_errno("calloc");
263 pack->privsep_child = calloc(1, sizeof(*pack->privsep_child));
264 if (pack->privsep_child == NULL) {
265 err = got_error_from_errno("calloc");
266 free(ibuf);
267 return err;
269 pack->child_has_tempfiles = 0;
271 if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_fds) == -1) {
272 err = got_error_from_errno("socketpair");
273 goto done;
276 pid = fork();
277 if (pid == -1) {
278 err = got_error_from_errno("fork");
279 goto done;
280 } else if (pid == 0) {
281 set_max_datasize();
282 got_privsep_exec_child(imsg_fds, GOT_PATH_PROG_READ_PACK,
283 pack->path_packfile);
284 /* not reached */
287 if (close(imsg_fds[1]) == -1)
288 return got_error_from_errno("close");
289 pack->privsep_child->imsg_fd = imsg_fds[0];
290 pack->privsep_child->pid = pid;
291 imsg_init(ibuf, imsg_fds[0]);
292 pack->privsep_child->ibuf = ibuf;
294 err = got_privsep_init_pack_child(ibuf, pack, packidx);
295 if (err) {
296 const struct got_error *child_err;
297 err = got_privsep_send_stop(pack->privsep_child->imsg_fd);
298 child_err = got_privsep_wait_for_child(
299 pack->privsep_child->pid);
300 if (child_err && err == NULL)
301 err = child_err;
303 done:
304 if (err) {
305 free(ibuf);
306 free(pack->privsep_child);
307 pack->privsep_child = NULL;
309 return err;
312 static const struct got_error *
313 read_packed_object_privsep(struct got_object **obj,
314 struct got_repository *repo, struct got_pack *pack,
315 struct got_packidx *packidx, int idx, struct got_object_id *id)
317 const struct got_error *err = NULL;
319 if (pack->privsep_child == NULL) {
320 err = start_pack_privsep_child(pack, packidx);
321 if (err)
322 return err;
325 return request_packed_object(obj, pack, idx, id);
328 static const struct got_error *
329 read_packed_object_raw_privsep(uint8_t **outbuf, off_t *size, size_t *hdrlen,
330 int outfd, struct got_pack *pack, struct got_packidx *packidx, int idx,
331 struct got_object_id *id)
333 const struct got_error *err = NULL;
335 if (pack->privsep_child == NULL) {
336 err = start_pack_privsep_child(pack, packidx);
337 if (err)
338 return err;
341 return request_packed_object_raw(outbuf, size, hdrlen, outfd, pack,
342 idx, id);
345 const struct got_error *
346 got_object_open_packed(struct got_object **obj, struct got_object_id *id,
347 struct got_repository *repo)
349 const struct got_error *err = NULL;
350 struct got_pack *pack = NULL;
351 struct got_packidx *packidx = NULL;
352 int idx;
353 char *path_packfile;
355 err = got_repo_search_packidx(&packidx, &idx, repo, id);
356 if (err)
357 return err;
359 err = got_packidx_get_packfile_path(&path_packfile,
360 packidx->path_packidx);
361 if (err)
362 return err;
364 pack = got_repo_get_cached_pack(repo, path_packfile);
365 if (pack == NULL) {
366 err = got_repo_cache_pack(&pack, repo, path_packfile, packidx);
367 if (err)
368 goto done;
371 err = read_packed_object_privsep(obj, repo, pack, packidx, idx, id);
372 if (err)
373 goto done;
374 done:
375 free(path_packfile);
376 return err;
379 static const struct got_error *
380 request_object(struct got_object **obj, struct got_object_id *id,
381 struct got_repository *repo, int fd)
383 const struct got_error *err = NULL;
384 struct imsgbuf *ibuf;
386 ibuf = repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_OBJECT].ibuf;
388 err = got_privsep_send_obj_req(ibuf, fd, id);
389 if (err)
390 return err;
392 return got_privsep_recv_obj(obj, ibuf);
395 static const struct got_error *
396 request_raw_object(uint8_t **outbuf, off_t *size, size_t *hdrlen, int outfd,
397 struct got_object_id *id, struct got_repository *repo, int infd)
399 const struct got_error *err = NULL;
400 struct imsgbuf *ibuf;
401 int outfd_child;
403 ibuf = repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_OBJECT].ibuf;
405 outfd_child = dup(outfd);
406 if (outfd_child == -1)
407 return got_error_from_errno("dup");
409 err = got_privsep_send_raw_obj_req(ibuf, infd, id);
410 if (err)
411 return err;
413 err = got_privsep_send_raw_obj_outfd(ibuf, outfd_child);
414 if (err)
415 return err;
417 return got_privsep_recv_raw_obj(outbuf, size, hdrlen, ibuf);
420 static const struct got_error *
421 start_read_object_child(struct got_repository *repo)
423 const struct got_error *err = NULL;
424 int imsg_fds[2];
425 pid_t pid;
426 struct imsgbuf *ibuf;
428 ibuf = calloc(1, sizeof(*ibuf));
429 if (ibuf == NULL)
430 return got_error_from_errno("calloc");
432 if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_fds) == -1) {
433 err = got_error_from_errno("socketpair");
434 free(ibuf);
435 return err;
438 pid = fork();
439 if (pid == -1) {
440 err = got_error_from_errno("fork");
441 free(ibuf);
442 return err;
444 else if (pid == 0) {
445 got_privsep_exec_child(imsg_fds, GOT_PATH_PROG_READ_OBJECT,
446 repo->path);
447 /* not reached */
450 if (close(imsg_fds[1]) == -1) {
451 err = got_error_from_errno("close");
452 free(ibuf);
453 return err;
456 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_OBJECT].imsg_fd =
457 imsg_fds[0];
458 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_OBJECT].pid = pid;
459 imsg_init(ibuf, imsg_fds[0]);
460 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_OBJECT].ibuf = ibuf;
462 return NULL;
465 const struct got_error *
466 got_object_read_header_privsep(struct got_object **obj,
467 struct got_object_id *id, struct got_repository *repo, int obj_fd)
469 const struct got_error *err;
471 if (repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_OBJECT].imsg_fd != -1)
472 return request_object(obj, id, repo, obj_fd);
474 err = start_read_object_child(repo);
475 if (err) {
476 close(obj_fd);
477 return err;
480 return request_object(obj, id, repo, obj_fd);
483 static const struct got_error *
484 read_object_raw_privsep(uint8_t **outbuf, off_t *size, size_t *hdrlen,
485 int outfd, struct got_object_id *id, struct got_repository *repo,
486 int obj_fd)
488 const struct got_error *err;
490 if (repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_OBJECT].imsg_fd != -1)
491 return request_raw_object(outbuf, size, hdrlen, outfd, id,
492 repo, obj_fd);
494 err = start_read_object_child(repo);
495 if (err)
496 return err;
498 return request_raw_object(outbuf, size, hdrlen, outfd, id, repo,
499 obj_fd);
502 const struct got_error *
503 got_object_open(struct got_object **obj, struct got_repository *repo,
504 struct got_object_id *id)
506 const struct got_error *err = NULL;
507 int fd;
509 *obj = got_repo_get_cached_object(repo, id);
510 if (*obj != NULL) {
511 (*obj)->refcnt++;
512 return NULL;
515 err = got_object_open_packed(obj, id, repo);
516 if (err && err->code != GOT_ERR_NO_OBJ)
517 return err;
518 if (*obj) {
519 (*obj)->refcnt++;
520 return got_repo_cache_object(repo, id, *obj);
523 err = got_object_open_loose_fd(&fd, id, repo);
524 if (err) {
525 if (err->code == GOT_ERR_ERRNO && errno == ENOENT)
526 err = got_error_no_obj(id);
527 return err;
530 err = got_object_read_header_privsep(obj, id, repo, fd);
531 if (err)
532 return err;
534 memcpy((*obj)->id.sha1, id->sha1, SHA1_DIGEST_LENGTH);
536 (*obj)->refcnt++;
537 return got_repo_cache_object(repo, id, *obj);
540 /* *outfd must be initialized to -1 by caller */
541 const struct got_error *
542 got_object_raw_open(struct got_raw_object **obj, int *outfd,
543 struct got_repository *repo, struct got_object_id *id)
545 const struct got_error *err = NULL;
546 struct got_packidx *packidx = NULL;
547 int idx;
548 uint8_t *outbuf = NULL;
549 off_t size = 0;
550 size_t hdrlen = 0;
551 char *path_packfile = NULL;
553 *obj = got_repo_get_cached_raw_object(repo, id);
554 if (*obj != NULL) {
555 (*obj)->refcnt++;
556 return NULL;
559 if (*outfd == -1) {
560 *outfd = got_opentempfd();
561 if (*outfd == -1)
562 return got_error_from_errno("got_opentempfd");
565 err = got_repo_search_packidx(&packidx, &idx, repo, id);
566 if (err == NULL) {
567 struct got_pack *pack = NULL;
569 err = got_packidx_get_packfile_path(&path_packfile,
570 packidx->path_packidx);
571 if (err)
572 goto done;
574 pack = got_repo_get_cached_pack(repo, path_packfile);
575 if (pack == NULL) {
576 err = got_repo_cache_pack(&pack, repo, path_packfile,
577 packidx);
578 if (err)
579 goto done;
581 err = read_packed_object_raw_privsep(&outbuf, &size, &hdrlen,
582 *outfd, pack, packidx, idx, id);
583 if (err)
584 goto done;
585 } else if (err->code == GOT_ERR_NO_OBJ) {
586 int fd;
588 err = got_object_open_loose_fd(&fd, id, repo);
589 if (err)
590 goto done;
591 err = read_object_raw_privsep(&outbuf, &size, &hdrlen, *outfd,
592 id, repo, fd);
593 if (err)
594 goto done;
597 *obj = calloc(1, sizeof(**obj));
598 if (*obj == NULL) {
599 err = got_error_from_errno("calloc");
600 goto done;
602 (*obj)->fd = -1;
604 if (outbuf) {
605 (*obj)->data = outbuf;
606 } else {
607 struct stat sb;
608 if (fstat(*outfd, &sb) == -1) {
609 err = got_error_from_errno("fstat");
610 goto done;
613 if (sb.st_size != hdrlen + size) {
614 err = got_error(GOT_ERR_PRIVSEP_LEN);
615 goto done;
617 #ifndef GOT_PACK_NO_MMAP
618 if (hdrlen + size > 0) {
619 (*obj)->data = mmap(NULL, hdrlen + size, PROT_READ,
620 MAP_PRIVATE, *outfd, 0);
621 if ((*obj)->data == MAP_FAILED) {
622 if (errno != ENOMEM) {
623 err = got_error_from_errno("mmap");
624 goto done;
626 (*obj)->data = NULL;
627 } else {
628 (*obj)->fd = *outfd;
629 *outfd = -1;
632 #endif
633 if (*outfd != -1) {
634 (*obj)->f = fdopen(*outfd, "r");
635 if ((*obj)->f == NULL) {
636 err = got_error_from_errno("fdopen");
637 goto done;
639 *outfd = -1;
642 (*obj)->hdrlen = hdrlen;
643 (*obj)->size = size;
644 err = got_repo_cache_raw_object(repo, id, *obj);
645 done:
646 free(path_packfile);
647 if (err) {
648 if (*obj) {
649 got_object_raw_close(*obj);
650 *obj = NULL;
652 free(outbuf);
653 } else
654 (*obj)->refcnt++;
655 return err;
658 const struct got_error *
659 got_object_open_by_id_str(struct got_object **obj, struct got_repository *repo,
660 const char *id_str)
662 struct got_object_id id;
664 if (!got_parse_sha1_digest(id.sha1, id_str))
665 return got_error_path(id_str, GOT_ERR_BAD_OBJ_ID_STR);
667 return got_object_open(obj, repo, &id);
670 const struct got_error *
671 got_object_resolve_id_str(struct got_object_id **id,
672 struct got_repository *repo, const char *id_str)
674 const struct got_error *err = NULL;
675 struct got_object *obj;
677 err = got_object_open_by_id_str(&obj, repo, id_str);
678 if (err)
679 return err;
681 *id = got_object_id_dup(got_object_get_id(obj));
682 got_object_close(obj);
683 if (*id == NULL)
684 return got_error_from_errno("got_object_id_dup");
686 return NULL;
689 static const struct got_error *
690 request_packed_commit(struct got_commit_object **commit, struct got_pack *pack,
691 int pack_idx, struct got_object_id *id)
693 const struct got_error *err = NULL;
695 err = got_privsep_send_commit_req(pack->privsep_child->ibuf, -1, id,
696 pack_idx);
697 if (err)
698 return err;
700 err = got_privsep_recv_commit(commit, pack->privsep_child->ibuf);
701 if (err)
702 return err;
704 (*commit)->flags |= GOT_COMMIT_FLAG_PACKED;
705 return NULL;
708 static const struct got_error *
709 read_packed_commit_privsep(struct got_commit_object **commit,
710 struct got_pack *pack, struct got_packidx *packidx, int idx,
711 struct got_object_id *id)
713 const struct got_error *err = NULL;
715 if (pack->privsep_child)
716 return request_packed_commit(commit, pack, idx, id);
718 err = start_pack_privsep_child(pack, packidx);
719 if (err)
720 return err;
722 return request_packed_commit(commit, pack, idx, id);
725 static const struct got_error *
726 request_commit(struct got_commit_object **commit, struct got_repository *repo,
727 int fd, struct got_object_id *id)
729 const struct got_error *err = NULL;
730 struct imsgbuf *ibuf;
732 ibuf = repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_COMMIT].ibuf;
734 err = got_privsep_send_commit_req(ibuf, fd, id, -1);
735 if (err)
736 return err;
738 return got_privsep_recv_commit(commit, ibuf);
741 static const struct got_error *
742 read_commit_privsep(struct got_commit_object **commit, int obj_fd,
743 struct got_object_id *id, struct got_repository *repo)
745 const struct got_error *err;
746 int imsg_fds[2];
747 pid_t pid;
748 struct imsgbuf *ibuf;
750 if (repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_COMMIT].imsg_fd != -1)
751 return request_commit(commit, repo, obj_fd, id);
753 ibuf = calloc(1, sizeof(*ibuf));
754 if (ibuf == NULL)
755 return got_error_from_errno("calloc");
757 if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_fds) == -1) {
758 err = got_error_from_errno("socketpair");
759 free(ibuf);
760 return err;
763 pid = fork();
764 if (pid == -1) {
765 err = got_error_from_errno("fork");
766 free(ibuf);
767 return err;
769 else if (pid == 0) {
770 got_privsep_exec_child(imsg_fds, GOT_PATH_PROG_READ_COMMIT,
771 repo->path);
772 /* not reached */
775 if (close(imsg_fds[1]) == -1) {
776 err = got_error_from_errno("close");
777 free(ibuf);
778 return err;
780 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_COMMIT].imsg_fd =
781 imsg_fds[0];
782 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_COMMIT].pid = pid;
783 imsg_init(ibuf, imsg_fds[0]);
784 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_COMMIT].ibuf = ibuf;
786 return request_commit(commit, repo, obj_fd, id);
790 static const struct got_error *
791 open_commit(struct got_commit_object **commit,
792 struct got_repository *repo, struct got_object_id *id, int check_cache)
794 const struct got_error *err = NULL;
795 struct got_packidx *packidx = NULL;
796 int idx;
797 char *path_packfile = NULL;
799 if (check_cache) {
800 *commit = got_repo_get_cached_commit(repo, id);
801 if (*commit != NULL) {
802 (*commit)->refcnt++;
803 return NULL;
805 } else
806 *commit = NULL;
808 err = got_repo_search_packidx(&packidx, &idx, repo, id);
809 if (err == NULL) {
810 struct got_pack *pack = NULL;
812 err = got_packidx_get_packfile_path(&path_packfile,
813 packidx->path_packidx);
814 if (err)
815 return err;
817 pack = got_repo_get_cached_pack(repo, path_packfile);
818 if (pack == NULL) {
819 err = got_repo_cache_pack(&pack, repo, path_packfile,
820 packidx);
821 if (err)
822 goto done;
824 err = read_packed_commit_privsep(commit, pack,
825 packidx, idx, id);
826 } else if (err->code == GOT_ERR_NO_OBJ) {
827 int fd;
829 err = got_object_open_loose_fd(&fd, id, repo);
830 if (err)
831 return err;
832 err = read_commit_privsep(commit, fd, id, repo);
835 if (err == NULL) {
836 (*commit)->refcnt++;
837 err = got_repo_cache_commit(repo, id, *commit);
839 done:
840 free(path_packfile);
841 return err;
844 const struct got_error *
845 got_object_open_as_commit(struct got_commit_object **commit,
846 struct got_repository *repo, struct got_object_id *id)
848 *commit = got_repo_get_cached_commit(repo, id);
849 if (*commit != NULL) {
850 (*commit)->refcnt++;
851 return NULL;
854 return open_commit(commit, repo, id, 0);
857 const struct got_error *
858 got_object_commit_open(struct got_commit_object **commit,
859 struct got_repository *repo, struct got_object *obj)
861 return open_commit(commit, repo, got_object_get_id(obj), 1);
864 const struct got_error *
865 got_object_qid_alloc(struct got_object_qid **qid, struct got_object_id *id)
867 const struct got_error *err = NULL;
869 *qid = calloc(1, sizeof(**qid));
870 if (*qid == NULL)
871 return got_error_from_errno("calloc");
873 (*qid)->id = got_object_id_dup(id);
874 if ((*qid)->id == NULL) {
875 err = got_error_from_errno("got_object_id_dup");
876 got_object_qid_free(*qid);
877 *qid = NULL;
878 return err;
881 return NULL;
884 const struct got_error *
885 got_object_id_queue_copy(const struct got_object_id_queue *src,
886 struct got_object_id_queue *dest)
888 const struct got_error *err;
889 struct got_object_qid *qid;
891 STAILQ_FOREACH(qid, src, entry) {
892 struct got_object_qid *new;
893 /*
894 * Deep-copy the object ID only. Let the caller deal
895 * with setting up the new->data pointer if needed.
896 */
897 err = got_object_qid_alloc(&new, qid->id);
898 if (err) {
899 got_object_id_queue_free(dest);
900 return err;
902 STAILQ_INSERT_TAIL(dest, new, entry);
905 return NULL;
908 static const struct got_error *
909 request_packed_tree(struct got_tree_object **tree, struct got_pack *pack,
910 int pack_idx, struct got_object_id *id)
912 const struct got_error *err = NULL;
914 err = got_privsep_send_tree_req(pack->privsep_child->ibuf, -1, id,
915 pack_idx);
916 if (err)
917 return err;
919 return got_privsep_recv_tree(tree, pack->privsep_child->ibuf);
922 static const struct got_error *
923 read_packed_tree_privsep(struct got_tree_object **tree,
924 struct got_pack *pack, struct got_packidx *packidx, int idx,
925 struct got_object_id *id)
927 const struct got_error *err = NULL;
929 if (pack->privsep_child)
930 return request_packed_tree(tree, pack, idx, id);
932 err = start_pack_privsep_child(pack, packidx);
933 if (err)
934 return err;
936 return request_packed_tree(tree, pack, idx, id);
939 static const struct got_error *
940 request_tree(struct got_tree_object **tree, struct got_repository *repo,
941 int fd, struct got_object_id *id)
943 const struct got_error *err = NULL;
944 struct imsgbuf *ibuf;
946 ibuf = repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TREE].ibuf;
948 err = got_privsep_send_tree_req(ibuf, fd, id, -1);
949 if (err)
950 return err;
952 return got_privsep_recv_tree(tree, ibuf);
955 const struct got_error *
956 read_tree_privsep(struct got_tree_object **tree, int obj_fd,
957 struct got_object_id *id, struct got_repository *repo)
959 const struct got_error *err;
960 int imsg_fds[2];
961 pid_t pid;
962 struct imsgbuf *ibuf;
964 if (repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TREE].imsg_fd != -1)
965 return request_tree(tree, repo, obj_fd, id);
967 ibuf = calloc(1, sizeof(*ibuf));
968 if (ibuf == NULL)
969 return got_error_from_errno("calloc");
971 if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_fds) == -1) {
972 err = got_error_from_errno("socketpair");
973 free(ibuf);
974 return err;
977 pid = fork();
978 if (pid == -1) {
979 err = got_error_from_errno("fork");
980 free(ibuf);
981 return err;
983 else if (pid == 0) {
984 got_privsep_exec_child(imsg_fds, GOT_PATH_PROG_READ_TREE,
985 repo->path);
986 /* not reached */
989 if (close(imsg_fds[1]) == -1) {
990 err = got_error_from_errno("close");
991 free(ibuf);
992 return err;
994 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TREE].imsg_fd =
995 imsg_fds[0];
996 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TREE].pid = pid;
997 imsg_init(ibuf, imsg_fds[0]);
998 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TREE].ibuf = ibuf;
1001 return request_tree(tree, repo, obj_fd, id);
1004 static const struct got_error *
1005 open_tree(struct got_tree_object **tree, struct got_repository *repo,
1006 struct got_object_id *id, int check_cache)
1008 const struct got_error *err = NULL;
1009 struct got_packidx *packidx = NULL;
1010 int idx;
1011 char *path_packfile = NULL;
1013 if (check_cache) {
1014 *tree = got_repo_get_cached_tree(repo, id);
1015 if (*tree != NULL) {
1016 (*tree)->refcnt++;
1017 return NULL;
1019 } else
1020 *tree = NULL;
1022 err = got_repo_search_packidx(&packidx, &idx, repo, id);
1023 if (err == NULL) {
1024 struct got_pack *pack = NULL;
1026 err = got_packidx_get_packfile_path(&path_packfile,
1027 packidx->path_packidx);
1028 if (err)
1029 return err;
1031 pack = got_repo_get_cached_pack(repo, path_packfile);
1032 if (pack == NULL) {
1033 err = got_repo_cache_pack(&pack, repo, path_packfile,
1034 packidx);
1035 if (err)
1036 goto done;
1038 err = read_packed_tree_privsep(tree, pack,
1039 packidx, idx, id);
1040 } else if (err->code == GOT_ERR_NO_OBJ) {
1041 int fd;
1043 err = got_object_open_loose_fd(&fd, id, repo);
1044 if (err)
1045 return err;
1046 err = read_tree_privsep(tree, fd, id, repo);
1049 if (err == NULL) {
1050 (*tree)->refcnt++;
1051 err = got_repo_cache_tree(repo, id, *tree);
1053 done:
1054 free(path_packfile);
1055 return err;
1058 const struct got_error *
1059 got_object_open_as_tree(struct got_tree_object **tree,
1060 struct got_repository *repo, struct got_object_id *id)
1062 *tree = got_repo_get_cached_tree(repo, id);
1063 if (*tree != NULL) {
1064 (*tree)->refcnt++;
1065 return NULL;
1068 return open_tree(tree, repo, id, 0);
1071 const struct got_error *
1072 got_object_tree_open(struct got_tree_object **tree,
1073 struct got_repository *repo, struct got_object *obj)
1075 return open_tree(tree, repo, got_object_get_id(obj), 1);
1078 int
1079 got_object_tree_get_nentries(struct got_tree_object *tree)
1081 return tree->nentries;
1084 struct got_tree_entry *
1085 got_object_tree_get_first_entry(struct got_tree_object *tree)
1087 return got_object_tree_get_entry(tree, 0);
1090 struct got_tree_entry *
1091 got_object_tree_get_last_entry(struct got_tree_object *tree)
1093 return got_object_tree_get_entry(tree, tree->nentries - 1);
1096 struct got_tree_entry *
1097 got_object_tree_get_entry(struct got_tree_object *tree, int i)
1099 if (i < 0 || i >= tree->nentries)
1100 return NULL;
1101 return &tree->entries[i];
1104 mode_t
1105 got_tree_entry_get_mode(struct got_tree_entry *te)
1107 return te->mode;
1110 const char *
1111 got_tree_entry_get_name(struct got_tree_entry *te)
1113 return &te->name[0];
1116 struct got_object_id *
1117 got_tree_entry_get_id(struct got_tree_entry *te)
1119 return &te->id;
1122 const struct got_error *
1123 got_object_blob_read_to_str(char **s, struct got_blob_object *blob)
1125 const struct got_error *err = NULL;
1126 size_t len, totlen, hdrlen, offset;
1128 *s = NULL;
1130 hdrlen = got_object_blob_get_hdrlen(blob);
1131 totlen = 0;
1132 offset = 0;
1133 do {
1134 char *p;
1136 err = got_object_blob_read_block(&len, blob);
1137 if (err)
1138 return err;
1140 if (len == 0)
1141 break;
1143 totlen += len - hdrlen;
1144 p = realloc(*s, totlen + 1);
1145 if (p == NULL) {
1146 err = got_error_from_errno("realloc");
1147 free(*s);
1148 *s = NULL;
1149 return err;
1151 *s = p;
1152 /* Skip blob object header first time around. */
1153 memcpy(*s + offset,
1154 got_object_blob_get_read_buf(blob) + hdrlen, len - hdrlen);
1155 hdrlen = 0;
1156 offset = totlen;
1157 } while (len > 0);
1159 (*s)[totlen] = '\0';
1160 return NULL;
1163 const struct got_error *
1164 got_tree_entry_get_symlink_target(char **link_target, struct got_tree_entry *te,
1165 struct got_repository *repo)
1167 const struct got_error *err = NULL;
1168 struct got_blob_object *blob = NULL;
1170 *link_target = NULL;
1172 if (!got_object_tree_entry_is_symlink(te))
1173 return got_error(GOT_ERR_TREE_ENTRY_TYPE);
1175 err = got_object_open_as_blob(&blob, repo,
1176 got_tree_entry_get_id(te), PATH_MAX);
1177 if (err)
1178 return err;
1180 err = got_object_blob_read_to_str(link_target, blob);
1181 got_object_blob_close(blob);
1182 if (err) {
1183 free(*link_target);
1184 *link_target = NULL;
1186 return err;
1189 int
1190 got_tree_entry_get_index(struct got_tree_entry *te)
1192 return te->idx;
1195 struct got_tree_entry *
1196 got_tree_entry_get_next(struct got_tree_object *tree,
1197 struct got_tree_entry *te)
1199 return got_object_tree_get_entry(tree, te->idx + 1);
1202 struct got_tree_entry *
1203 got_tree_entry_get_prev(struct got_tree_object *tree,
1204 struct got_tree_entry *te)
1206 return got_object_tree_get_entry(tree, te->idx - 1);
1209 static const struct got_error *
1210 request_packed_blob(uint8_t **outbuf, size_t *size, size_t *hdrlen, int outfd,
1211 struct got_pack *pack, struct got_packidx *packidx, int idx,
1212 struct got_object_id *id)
1214 const struct got_error *err = NULL;
1215 struct imsgbuf *ibuf = pack->privsep_child->ibuf;
1216 int outfd_child;
1218 err = pack_child_send_tempfiles(ibuf, pack);
1219 if (err)
1220 return err;
1222 outfd_child = dup(outfd);
1223 if (outfd_child == -1)
1224 return got_error_from_errno("dup");
1226 err = got_privsep_send_blob_req(pack->privsep_child->ibuf, -1, id, idx);
1227 if (err)
1228 return err;
1230 err = got_privsep_send_blob_outfd(pack->privsep_child->ibuf,
1231 outfd_child);
1232 if (err) {
1233 return err;
1236 err = got_privsep_recv_blob(outbuf, size, hdrlen,
1237 pack->privsep_child->ibuf);
1238 if (err)
1239 return err;
1241 if (lseek(outfd, SEEK_SET, 0) == -1)
1242 err = got_error_from_errno("lseek");
1244 return err;
1247 static const struct got_error *
1248 read_packed_blob_privsep(uint8_t **outbuf, size_t *size, size_t *hdrlen,
1249 int outfd, struct got_pack *pack, struct got_packidx *packidx, int idx,
1250 struct got_object_id *id)
1252 const struct got_error *err = NULL;
1254 if (pack->privsep_child == NULL) {
1255 err = start_pack_privsep_child(pack, packidx);
1256 if (err)
1257 return err;
1260 return request_packed_blob(outbuf, size, hdrlen, outfd, pack, packidx,
1261 idx, id);
1264 static const struct got_error *
1265 request_blob(uint8_t **outbuf, size_t *size, size_t *hdrlen, int outfd,
1266 int infd, struct got_object_id *id, struct imsgbuf *ibuf)
1268 const struct got_error *err = NULL;
1269 int outfd_child;
1271 outfd_child = dup(outfd);
1272 if (outfd_child == -1)
1273 return got_error_from_errno("dup");
1275 err = got_privsep_send_blob_req(ibuf, infd, id, -1);
1276 if (err)
1277 return err;
1279 err = got_privsep_send_blob_outfd(ibuf, outfd_child);
1280 if (err)
1281 return err;
1283 err = got_privsep_recv_blob(outbuf, size, hdrlen, ibuf);
1284 if (err)
1285 return err;
1287 if (lseek(outfd, SEEK_SET, 0) == -1)
1288 return got_error_from_errno("lseek");
1290 return err;
1293 static const struct got_error *
1294 read_blob_privsep(uint8_t **outbuf, size_t *size, size_t *hdrlen,
1295 int outfd, int infd, struct got_object_id *id, struct got_repository *repo)
1297 const struct got_error *err;
1298 int imsg_fds[2];
1299 pid_t pid;
1300 struct imsgbuf *ibuf;
1302 if (repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_BLOB].imsg_fd != -1) {
1303 ibuf = repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_BLOB].ibuf;
1304 return request_blob(outbuf, size, hdrlen, outfd, infd, id,
1305 ibuf);
1308 ibuf = calloc(1, sizeof(*ibuf));
1309 if (ibuf == NULL)
1310 return got_error_from_errno("calloc");
1312 if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_fds) == -1) {
1313 err = got_error_from_errno("socketpair");
1314 free(ibuf);
1315 return err;
1318 pid = fork();
1319 if (pid == -1) {
1320 err = got_error_from_errno("fork");
1321 free(ibuf);
1322 return err;
1324 else if (pid == 0) {
1325 got_privsep_exec_child(imsg_fds, GOT_PATH_PROG_READ_BLOB,
1326 repo->path);
1327 /* not reached */
1330 if (close(imsg_fds[1]) == -1) {
1331 err = got_error_from_errno("close");
1332 free(ibuf);
1333 return err;
1335 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_BLOB].imsg_fd =
1336 imsg_fds[0];
1337 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_BLOB].pid = pid;
1338 imsg_init(ibuf, imsg_fds[0]);
1339 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_BLOB].ibuf = ibuf;
1341 return request_blob(outbuf, size, hdrlen, outfd, infd, id, ibuf);
1344 static const struct got_error *
1345 open_blob(struct got_blob_object **blob, struct got_repository *repo,
1346 struct got_object_id *id, size_t blocksize)
1348 const struct got_error *err = NULL;
1349 struct got_packidx *packidx = NULL;
1350 int idx;
1351 char *path_packfile = NULL;
1352 uint8_t *outbuf;
1353 int outfd;
1354 size_t size, hdrlen;
1355 struct stat sb;
1357 *blob = calloc(1, sizeof(**blob));
1358 if (*blob == NULL)
1359 return got_error_from_errno("calloc");
1361 outfd = got_opentempfd();
1362 if (outfd == -1)
1363 return got_error_from_errno("got_opentempfd");
1365 (*blob)->read_buf = malloc(blocksize);
1366 if ((*blob)->read_buf == NULL) {
1367 err = got_error_from_errno("malloc");
1368 goto done;
1371 err = got_repo_search_packidx(&packidx, &idx, repo, id);
1372 if (err == NULL) {
1373 struct got_pack *pack = NULL;
1375 err = got_packidx_get_packfile_path(&path_packfile,
1376 packidx->path_packidx);
1377 if (err)
1378 goto done;
1380 pack = got_repo_get_cached_pack(repo, path_packfile);
1381 if (pack == NULL) {
1382 err = got_repo_cache_pack(&pack, repo, path_packfile,
1383 packidx);
1384 if (err)
1385 goto done;
1387 err = read_packed_blob_privsep(&outbuf, &size, &hdrlen, outfd,
1388 pack, packidx, idx, id);
1389 } else if (err->code == GOT_ERR_NO_OBJ) {
1390 int infd;
1392 err = got_object_open_loose_fd(&infd, id, repo);
1393 if (err)
1394 goto done;
1395 err = read_blob_privsep(&outbuf, &size, &hdrlen, outfd, infd,
1396 id, repo);
1398 if (err)
1399 goto done;
1401 if (hdrlen > size) {
1402 err = got_error(GOT_ERR_BAD_OBJ_HDR);
1403 goto done;
1406 if (outbuf) {
1407 if (close(outfd) == -1 && err == NULL)
1408 err = got_error_from_errno("close");
1409 outfd = -1;
1410 (*blob)->f = fmemopen(outbuf, size, "rb");
1411 if ((*blob)->f == NULL) {
1412 err = got_error_from_errno("fmemopen");
1413 free(outbuf);
1414 goto done;
1416 (*blob)->data = outbuf;
1417 } else {
1418 if (fstat(outfd, &sb) == -1) {
1419 err = got_error_from_errno("fstat");
1420 goto done;
1423 if (sb.st_size != size) {
1424 err = got_error(GOT_ERR_PRIVSEP_LEN);
1425 goto done;
1428 (*blob)->f = fdopen(outfd, "rb");
1429 if ((*blob)->f == NULL) {
1430 err = got_error_from_errno("fdopen");
1431 close(outfd);
1432 outfd = -1;
1433 goto done;
1437 (*blob)->hdrlen = hdrlen;
1438 (*blob)->blocksize = blocksize;
1439 memcpy(&(*blob)->id.sha1, id->sha1, SHA1_DIGEST_LENGTH);
1441 done:
1442 free(path_packfile);
1443 if (err) {
1444 if (*blob) {
1445 got_object_blob_close(*blob);
1446 *blob = NULL;
1447 } else if (outfd != -1)
1448 close(outfd);
1450 return err;
1453 const struct got_error *
1454 got_object_open_as_blob(struct got_blob_object **blob,
1455 struct got_repository *repo, struct got_object_id *id,
1456 size_t blocksize)
1458 return open_blob(blob, repo, id, blocksize);
1461 const struct got_error *
1462 got_object_blob_open(struct got_blob_object **blob,
1463 struct got_repository *repo, struct got_object *obj, size_t blocksize)
1465 return open_blob(blob, repo, got_object_get_id(obj), blocksize);
1468 const struct got_error *
1469 got_object_blob_close(struct got_blob_object *blob)
1471 const struct got_error *err = NULL;
1472 free(blob->read_buf);
1473 if (blob->f && fclose(blob->f) == EOF)
1474 err = got_error_from_errno("fclose");
1475 free(blob->data);
1476 free(blob);
1477 return err;
1480 void
1481 got_object_blob_rewind(struct got_blob_object *blob)
1483 if (blob->f)
1484 rewind(blob->f);
1487 char *
1488 got_object_blob_id_str(struct got_blob_object *blob, char *buf, size_t size)
1490 return got_sha1_digest_to_str(blob->id.sha1, buf, size);
1493 size_t
1494 got_object_blob_get_hdrlen(struct got_blob_object *blob)
1496 return blob->hdrlen;
1499 const uint8_t *
1500 got_object_blob_get_read_buf(struct got_blob_object *blob)
1502 return blob->read_buf;
1505 const struct got_error *
1506 got_object_blob_read_block(size_t *outlenp, struct got_blob_object *blob)
1508 size_t n;
1510 n = fread(blob->read_buf, 1, blob->blocksize, blob->f);
1511 if (n == 0 && ferror(blob->f))
1512 return got_ferror(blob->f, GOT_ERR_IO);
1513 *outlenp = n;
1514 return NULL;
1517 const struct got_error *
1518 got_object_blob_dump_to_file(off_t *filesize, int *nlines,
1519 off_t **line_offsets, FILE *outfile, struct got_blob_object *blob)
1521 const struct got_error *err = NULL;
1522 size_t n, len, hdrlen;
1523 const uint8_t *buf;
1524 int i;
1525 const int alloc_chunksz = 512;
1526 size_t nalloc = 0;
1527 off_t off = 0, total_len = 0;
1529 if (line_offsets)
1530 *line_offsets = NULL;
1531 if (filesize)
1532 *filesize = 0;
1533 if (nlines)
1534 *nlines = 0;
1536 hdrlen = got_object_blob_get_hdrlen(blob);
1537 do {
1538 err = got_object_blob_read_block(&len, blob);
1539 if (err)
1540 return err;
1541 if (len == 0)
1542 break;
1543 buf = got_object_blob_get_read_buf(blob);
1544 i = hdrlen;
1545 if (nlines) {
1546 if (line_offsets && *line_offsets == NULL) {
1547 /* Have some data but perhaps no '\n'. */
1548 *nlines = 1;
1549 nalloc = alloc_chunksz;
1550 *line_offsets = calloc(nalloc,
1551 sizeof(**line_offsets));
1552 if (*line_offsets == NULL)
1553 return got_error_from_errno("calloc");
1555 /* Skip forward over end of first line. */
1556 while (i < len) {
1557 if (buf[i] == '\n')
1558 break;
1559 i++;
1562 /* Scan '\n' offsets in remaining chunk of data. */
1563 while (i < len) {
1564 if (buf[i] != '\n') {
1565 i++;
1566 continue;
1568 (*nlines)++;
1569 if (line_offsets && nalloc < *nlines) {
1570 size_t n = *nlines + alloc_chunksz;
1571 off_t *o = recallocarray(*line_offsets,
1572 nalloc, n, sizeof(**line_offsets));
1573 if (o == NULL) {
1574 free(*line_offsets);
1575 *line_offsets = NULL;
1576 return got_error_from_errno(
1577 "recallocarray");
1579 *line_offsets = o;
1580 nalloc = n;
1582 if (line_offsets) {
1583 off = total_len + i - hdrlen + 1;
1584 (*line_offsets)[*nlines - 1] = off;
1586 i++;
1589 /* Skip blob object header first time around. */
1590 n = fwrite(buf + hdrlen, 1, len - hdrlen, outfile);
1591 if (n != len - hdrlen)
1592 return got_ferror(outfile, GOT_ERR_IO);
1593 total_len += len - hdrlen;
1594 hdrlen = 0;
1595 } while (len != 0);
1597 if (fflush(outfile) != 0)
1598 return got_error_from_errno("fflush");
1599 rewind(outfile);
1601 if (filesize)
1602 *filesize = total_len;
1604 return NULL;
1607 static const struct got_error *
1608 request_packed_tag(struct got_tag_object **tag, struct got_pack *pack,
1609 int pack_idx, struct got_object_id *id)
1611 const struct got_error *err = NULL;
1613 err = got_privsep_send_tag_req(pack->privsep_child->ibuf, -1, id,
1614 pack_idx);
1615 if (err)
1616 return err;
1618 return got_privsep_recv_tag(tag, pack->privsep_child->ibuf);
1621 static const struct got_error *
1622 read_packed_tag_privsep(struct got_tag_object **tag,
1623 struct got_pack *pack, struct got_packidx *packidx, int idx,
1624 struct got_object_id *id)
1626 const struct got_error *err = NULL;
1628 if (pack->privsep_child)
1629 return request_packed_tag(tag, pack, idx, id);
1631 err = start_pack_privsep_child(pack, packidx);
1632 if (err)
1633 return err;
1635 return request_packed_tag(tag, pack, idx, id);
1638 static const struct got_error *
1639 request_tag(struct got_tag_object **tag, struct got_repository *repo,
1640 int fd, struct got_object_id *id)
1642 const struct got_error *err = NULL;
1643 struct imsgbuf *ibuf;
1645 ibuf = repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TAG].ibuf;
1647 err = got_privsep_send_tag_req(ibuf, fd, id, -1);
1648 if (err)
1649 return err;
1651 return got_privsep_recv_tag(tag, ibuf);
1654 static const struct got_error *
1655 read_tag_privsep(struct got_tag_object **tag, int obj_fd,
1656 struct got_object_id *id, struct got_repository *repo)
1658 const struct got_error *err;
1659 int imsg_fds[2];
1660 pid_t pid;
1661 struct imsgbuf *ibuf;
1663 if (repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TAG].imsg_fd != -1)
1664 return request_tag(tag, repo, obj_fd, id);
1666 ibuf = calloc(1, sizeof(*ibuf));
1667 if (ibuf == NULL)
1668 return got_error_from_errno("calloc");
1670 if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_fds) == -1) {
1671 err = got_error_from_errno("socketpair");
1672 free(ibuf);
1673 return err;
1676 pid = fork();
1677 if (pid == -1) {
1678 err = got_error_from_errno("fork");
1679 free(ibuf);
1680 return err;
1682 else if (pid == 0) {
1683 got_privsep_exec_child(imsg_fds, GOT_PATH_PROG_READ_TAG,
1684 repo->path);
1685 /* not reached */
1688 if (close(imsg_fds[1]) == -1) {
1689 err = got_error_from_errno("close");
1690 free(ibuf);
1691 return err;
1693 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TAG].imsg_fd =
1694 imsg_fds[0];
1695 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TAG].pid = pid;
1696 imsg_init(ibuf, imsg_fds[0]);
1697 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TAG].ibuf = ibuf;
1699 return request_tag(tag, repo, obj_fd, id);
1702 static const struct got_error *
1703 open_tag(struct got_tag_object **tag, struct got_repository *repo,
1704 struct got_object_id *id, int check_cache)
1706 const struct got_error *err = NULL;
1707 struct got_packidx *packidx = NULL;
1708 int idx;
1709 char *path_packfile = NULL;
1710 struct got_object *obj = NULL;
1711 int obj_type = GOT_OBJ_TYPE_ANY;
1713 if (check_cache) {
1714 *tag = got_repo_get_cached_tag(repo, id);
1715 if (*tag != NULL) {
1716 (*tag)->refcnt++;
1717 return NULL;
1719 } else
1720 *tag = NULL;
1722 err = got_repo_search_packidx(&packidx, &idx, repo, id);
1723 if (err == NULL) {
1724 struct got_pack *pack = NULL;
1726 err = got_packidx_get_packfile_path(&path_packfile,
1727 packidx->path_packidx);
1728 if (err)
1729 return err;
1731 pack = got_repo_get_cached_pack(repo, path_packfile);
1732 if (pack == NULL) {
1733 err = got_repo_cache_pack(&pack, repo, path_packfile,
1734 packidx);
1735 if (err)
1736 goto done;
1739 /* Beware of "lightweight" tags: Check object type first. */
1740 err = read_packed_object_privsep(&obj, repo, pack, packidx,
1741 idx, id);
1742 if (err)
1743 goto done;
1744 obj_type = obj->type;
1745 got_object_close(obj);
1746 if (obj_type != GOT_OBJ_TYPE_TAG) {
1747 err = got_error(GOT_ERR_OBJ_TYPE);
1748 goto done;
1750 err = read_packed_tag_privsep(tag, pack, packidx, idx, id);
1751 } else if (err->code == GOT_ERR_NO_OBJ) {
1752 int fd;
1754 err = got_object_open_loose_fd(&fd, id, repo);
1755 if (err)
1756 return err;
1757 err = got_object_read_header_privsep(&obj, id, repo, fd);
1758 if (err)
1759 return err;
1760 obj_type = obj->type;
1761 got_object_close(obj);
1762 if (obj_type != GOT_OBJ_TYPE_TAG)
1763 return got_error(GOT_ERR_OBJ_TYPE);
1765 err = got_object_open_loose_fd(&fd, id, repo);
1766 if (err)
1767 return err;
1768 err = read_tag_privsep(tag, fd, id, repo);
1771 if (err == NULL) {
1772 (*tag)->refcnt++;
1773 err = got_repo_cache_tag(repo, id, *tag);
1775 done:
1776 free(path_packfile);
1777 return err;
1780 const struct got_error *
1781 got_object_open_as_tag(struct got_tag_object **tag,
1782 struct got_repository *repo, struct got_object_id *id)
1784 *tag = got_repo_get_cached_tag(repo, id);
1785 if (*tag != NULL) {
1786 (*tag)->refcnt++;
1787 return NULL;
1790 return open_tag(tag, repo, id, 0);
1793 const struct got_error *
1794 got_object_tag_open(struct got_tag_object **tag,
1795 struct got_repository *repo, struct got_object *obj)
1797 return open_tag(tag, repo, got_object_get_id(obj), 1);
1800 const char *
1801 got_object_tag_get_name(struct got_tag_object *tag)
1803 return tag->tag;
1806 int
1807 got_object_tag_get_object_type(struct got_tag_object *tag)
1809 return tag->obj_type;
1812 struct got_object_id *
1813 got_object_tag_get_object_id(struct got_tag_object *tag)
1815 return &tag->id;
1818 time_t
1819 got_object_tag_get_tagger_time(struct got_tag_object *tag)
1821 return tag->tagger_time;
1824 time_t
1825 got_object_tag_get_tagger_gmtoff(struct got_tag_object *tag)
1827 return tag->tagger_gmtoff;
1830 const char *
1831 got_object_tag_get_tagger(struct got_tag_object *tag)
1833 return tag->tagger;
1836 const char *
1837 got_object_tag_get_message(struct got_tag_object *tag)
1839 return tag->tagmsg;
1842 static struct got_tree_entry *
1843 find_entry_by_name(struct got_tree_object *tree, const char *name, size_t len)
1845 int i;
1847 /* Note that tree entries are sorted in strncmp() order. */
1848 for (i = 0; i < tree->nentries; i++) {
1849 struct got_tree_entry *te = &tree->entries[i];
1850 int cmp = strncmp(te->name, name, len);
1851 if (cmp < 0)
1852 continue;
1853 if (cmp > 0)
1854 break;
1855 if (te->name[len] == '\0')
1856 return te;
1858 return NULL;
1861 struct got_tree_entry *
1862 got_object_tree_find_entry(struct got_tree_object *tree, const char *name)
1864 return find_entry_by_name(tree, name, strlen(name));
1867 const struct got_error *
1868 got_object_tree_find_path(struct got_object_id **id, mode_t *mode,
1869 struct got_repository *repo, struct got_tree_object *tree,
1870 const char *path)
1872 const struct got_error *err = NULL;
1873 struct got_tree_object *subtree = NULL;
1874 struct got_tree_entry *te = NULL;
1875 const char *seg, *s;
1876 size_t seglen;
1878 *id = NULL;
1880 s = path;
1881 while (s[0] == '/')
1882 s++;
1883 seg = s;
1884 seglen = 0;
1885 subtree = tree;
1886 while (*s) {
1887 struct got_tree_object *next_tree;
1889 if (*s != '/') {
1890 s++;
1891 seglen++;
1892 if (*s)
1893 continue;
1896 te = find_entry_by_name(subtree, seg, seglen);
1897 if (te == NULL) {
1898 err = got_error_path(path, GOT_ERR_NO_TREE_ENTRY);
1899 goto done;
1902 if (*s == '\0')
1903 break;
1905 seg = s + 1;
1906 seglen = 0;
1907 s++;
1908 if (*s) {
1909 err = got_object_open_as_tree(&next_tree, repo,
1910 &te->id);
1911 te = NULL;
1912 if (err)
1913 goto done;
1914 if (subtree != tree)
1915 got_object_tree_close(subtree);
1916 subtree = next_tree;
1920 if (te) {
1921 *id = got_object_id_dup(&te->id);
1922 if (*id == NULL)
1923 return got_error_from_errno("got_object_id_dup");
1924 if (mode)
1925 *mode = te->mode;
1926 } else
1927 err = got_error_path(path, GOT_ERR_NO_TREE_ENTRY);
1928 done:
1929 if (subtree && subtree != tree)
1930 got_object_tree_close(subtree);
1931 return err;
1933 const struct got_error *
1934 got_object_id_by_path(struct got_object_id **id, struct got_repository *repo,
1935 struct got_object_id *commit_id, const char *path)
1937 const struct got_error *err = NULL;
1938 struct got_commit_object *commit = NULL;
1939 struct got_tree_object *tree = NULL;
1941 *id = NULL;
1943 err = got_object_open_as_commit(&commit, repo, commit_id);
1944 if (err)
1945 goto done;
1947 /* Handle opening of root of commit's tree. */
1948 if (got_path_is_root_dir(path)) {
1949 *id = got_object_id_dup(commit->tree_id);
1950 if (*id == NULL)
1951 err = got_error_from_errno("got_object_id_dup");
1952 } else {
1953 err = got_object_open_as_tree(&tree, repo, commit->tree_id);
1954 if (err)
1955 goto done;
1956 err = got_object_tree_find_path(id, NULL, repo, tree, path);
1958 done:
1959 if (commit)
1960 got_object_commit_close(commit);
1961 if (tree)
1962 got_object_tree_close(tree);
1963 return err;
1967 * Normalize file mode bits to avoid false positive tree entry differences
1968 * in case tree entries have unexpected mode bits set.
1970 static mode_t
1971 normalize_mode_for_comparison(mode_t mode)
1974 * For directories, the only relevant bit is the IFDIR bit.
1975 * This allows us to detect paths changing from a directory
1976 * to a file and vice versa.
1978 if (S_ISDIR(mode))
1979 return mode & S_IFDIR;
1982 * For symlinks, the only relevant bit is the IFLNK bit.
1983 * This allows us to detect paths changing from a symlinks
1984 * to a file or directory and vice versa.
1986 if (S_ISLNK(mode))
1987 return mode & S_IFLNK;
1989 /* For files, the only change we care about is the executable bit. */
1990 return mode & S_IXUSR;
1993 const struct got_error *
1994 got_object_tree_path_changed(int *changed,
1995 struct got_tree_object *tree01, struct got_tree_object *tree02,
1996 const char *path, struct got_repository *repo)
1998 const struct got_error *err = NULL;
1999 struct got_tree_object *tree1 = NULL, *tree2 = NULL;
2000 struct got_tree_entry *te1 = NULL, *te2 = NULL;
2001 const char *seg, *s;
2002 size_t seglen;
2004 *changed = 0;
2006 /* We not do support comparing the root path. */
2007 if (got_path_is_root_dir(path))
2008 return got_error_path(path, GOT_ERR_BAD_PATH);
2010 tree1 = tree01;
2011 tree2 = tree02;
2012 s = path;
2013 while (*s == '/')
2014 s++;
2015 seg = s;
2016 seglen = 0;
2017 while (*s) {
2018 struct got_tree_object *next_tree1, *next_tree2;
2019 mode_t mode1, mode2;
2021 if (*s != '/') {
2022 s++;
2023 seglen++;
2024 if (*s)
2025 continue;
2028 te1 = find_entry_by_name(tree1, seg, seglen);
2029 if (te1 == NULL) {
2030 err = got_error(GOT_ERR_NO_OBJ);
2031 goto done;
2034 if (tree2)
2035 te2 = find_entry_by_name(tree2, seg, seglen);
2037 if (te2) {
2038 mode1 = normalize_mode_for_comparison(te1->mode);
2039 mode2 = normalize_mode_for_comparison(te2->mode);
2040 if (mode1 != mode2) {
2041 *changed = 1;
2042 goto done;
2045 if (got_object_id_cmp(&te1->id, &te2->id) == 0) {
2046 *changed = 0;
2047 goto done;
2051 if (*s == '\0') { /* final path element */
2052 *changed = 1;
2053 goto done;
2056 seg = s + 1;
2057 s++;
2058 seglen = 0;
2059 if (*s) {
2060 err = got_object_open_as_tree(&next_tree1, repo,
2061 &te1->id);
2062 te1 = NULL;
2063 if (err)
2064 goto done;
2065 if (tree1 != tree01)
2066 got_object_tree_close(tree1);
2067 tree1 = next_tree1;
2069 if (te2) {
2070 err = got_object_open_as_tree(&next_tree2, repo,
2071 &te2->id);
2072 te2 = NULL;
2073 if (err)
2074 goto done;
2075 if (tree2 != tree02)
2076 got_object_tree_close(tree2);
2077 tree2 = next_tree2;
2078 } else if (tree2) {
2079 if (tree2 != tree02)
2080 got_object_tree_close(tree2);
2081 tree2 = NULL;
2085 done:
2086 if (tree1 && tree1 != tree01)
2087 got_object_tree_close(tree1);
2088 if (tree2 && tree2 != tree02)
2089 got_object_tree_close(tree2);
2090 return err;
2093 const struct got_error *
2094 got_object_tree_entry_dup(struct got_tree_entry **new_te,
2095 struct got_tree_entry *te)
2097 const struct got_error *err = NULL;
2099 *new_te = calloc(1, sizeof(**new_te));
2100 if (*new_te == NULL)
2101 return got_error_from_errno("calloc");
2103 (*new_te)->mode = te->mode;
2104 memcpy((*new_te)->name, te->name, sizeof((*new_te)->name));
2105 memcpy(&(*new_te)->id, &te->id, sizeof((*new_te)->id));
2106 return err;
2109 int
2110 got_object_tree_entry_is_submodule(struct got_tree_entry *te)
2112 return (te->mode & S_IFMT) == (S_IFDIR | S_IFLNK);
2115 int
2116 got_object_tree_entry_is_symlink(struct got_tree_entry *te)
2118 /* S_IFDIR check avoids confusing symlinks with submodules. */
2119 return ((te->mode & (S_IFDIR | S_IFLNK)) == S_IFLNK);
2122 static const struct got_error *
2123 resolve_symlink(char **link_target, const char *path,
2124 struct got_object_id *commit_id, struct got_repository *repo)
2126 const struct got_error *err = NULL;
2127 char buf[PATH_MAX];
2128 char *name, *parent_path = NULL;
2129 struct got_object_id *tree_obj_id = NULL;
2130 struct got_tree_object *tree = NULL;
2131 struct got_tree_entry *te = NULL;
2133 *link_target = NULL;
2135 if (strlcpy(buf, path, sizeof(buf)) >= sizeof(buf))
2136 return got_error(GOT_ERR_NO_SPACE);
2138 name = basename(buf);
2139 if (name == NULL)
2140 return got_error_from_errno2("basename", path);
2142 err = got_path_dirname(&parent_path, path);
2143 if (err)
2144 return err;
2146 err = got_object_id_by_path(&tree_obj_id, repo, commit_id,
2147 parent_path);
2148 if (err) {
2149 if (err->code == GOT_ERR_NO_TREE_ENTRY) {
2150 /* Display the complete path in error message. */
2151 err = got_error_path(path, err->code);
2153 goto done;
2156 err = got_object_open_as_tree(&tree, repo, tree_obj_id);
2157 if (err)
2158 goto done;
2160 te = got_object_tree_find_entry(tree, name);
2161 if (te == NULL) {
2162 err = got_error_path(path, GOT_ERR_NO_TREE_ENTRY);
2163 goto done;
2166 if (got_object_tree_entry_is_symlink(te)) {
2167 err = got_tree_entry_get_symlink_target(link_target, te, repo);
2168 if (err)
2169 goto done;
2170 if (!got_path_is_absolute(*link_target)) {
2171 char *abspath;
2172 if (asprintf(&abspath, "%s/%s", parent_path,
2173 *link_target) == -1) {
2174 err = got_error_from_errno("asprintf");
2175 goto done;
2177 free(*link_target);
2178 *link_target = malloc(PATH_MAX);
2179 if (*link_target == NULL) {
2180 err = got_error_from_errno("malloc");
2181 goto done;
2183 err = got_canonpath(abspath, *link_target, PATH_MAX);
2184 free(abspath);
2185 if (err)
2186 goto done;
2189 done:
2190 free(tree_obj_id);
2191 if (tree)
2192 got_object_tree_close(tree);
2193 if (err) {
2194 free(*link_target);
2195 *link_target = NULL;
2197 return err;
2200 const struct got_error *
2201 got_object_resolve_symlinks(char **link_target, const char *path,
2202 struct got_object_id *commit_id, struct got_repository *repo)
2204 const struct got_error *err = NULL;
2205 char *next_target = NULL;
2206 int max_recursion = 40; /* matches Git */
2208 *link_target = NULL;
2210 do {
2211 err = resolve_symlink(&next_target,
2212 *link_target ? *link_target : path, commit_id, repo);
2213 if (err)
2214 break;
2215 if (next_target) {
2216 free(*link_target);
2217 if (--max_recursion == 0) {
2218 err = got_error_path(path, GOT_ERR_RECURSION);
2219 *link_target = NULL;
2220 break;
2222 *link_target = next_target;
2224 } while (next_target);
2226 return err;
2229 const struct got_error *
2230 got_traverse_packed_commits(struct got_object_id_queue *traversed_commits,
2231 struct got_object_id *commit_id, const char *path,
2232 struct got_repository *repo)
2234 const struct got_error *err = NULL;
2235 struct got_pack *pack = NULL;
2236 struct got_packidx *packidx = NULL;
2237 char *path_packfile = NULL;
2238 struct got_commit_object *changed_commit = NULL;
2239 struct got_object_id *changed_commit_id = NULL;
2240 int idx;
2242 err = got_repo_search_packidx(&packidx, &idx, repo, commit_id);
2243 if (err) {
2244 if (err->code != GOT_ERR_NO_OBJ)
2245 return err;
2246 return NULL;
2249 err = got_packidx_get_packfile_path(&path_packfile,
2250 packidx->path_packidx);
2251 if (err)
2252 return err;
2254 pack = got_repo_get_cached_pack(repo, path_packfile);
2255 if (pack == NULL) {
2256 err = got_repo_cache_pack(&pack, repo, path_packfile, packidx);
2257 if (err)
2258 goto done;
2261 if (pack->privsep_child == NULL) {
2262 err = start_pack_privsep_child(pack, packidx);
2263 if (err)
2264 goto done;
2267 err = got_privsep_send_commit_traversal_request(
2268 pack->privsep_child->ibuf, commit_id, idx, path);
2269 if (err)
2270 goto done;
2272 err = got_privsep_recv_traversed_commits(&changed_commit,
2273 &changed_commit_id, traversed_commits, pack->privsep_child->ibuf);
2274 if (err)
2275 goto done;
2277 if (changed_commit) {
2279 * Cache the commit in which the path was changed.
2280 * This commit might be opened again soon.
2282 changed_commit->refcnt++;
2283 err = got_repo_cache_commit(repo, changed_commit_id,
2284 changed_commit);
2285 got_object_commit_close(changed_commit);
2287 done:
2288 free(path_packfile);
2289 free(changed_commit_id);
2290 return err;