Blob


1 /*
2 * Copyright (c) 2018, 2019 Stefan Sperling <stsp@openbsd.org>
3 *
4 * Permission to use, copy, modify, and distribute this software for any
5 * purpose with or without fee is hereby granted, provided that the above
6 * copyright notice and this permission notice appear in all copies.
7 *
8 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
15 */
17 #include <sys/types.h>
18 #include <sys/stat.h>
19 #include <sys/uio.h>
20 #include <sys/socket.h>
21 #include <sys/wait.h>
22 #include <sys/resource.h>
23 #include <sys/mman.h>
25 #include <errno.h>
26 #include <fcntl.h>
27 #include <stdio.h>
28 #include <stdlib.h>
29 #include <string.h>
30 #include <stdint.h>
31 #include <unistd.h>
32 #include <zlib.h>
33 #include <ctype.h>
34 #include <libgen.h>
35 #include <limits.h>
36 #include <time.h>
38 #include "got_compat.h"
40 #include "got_error.h"
41 #include "got_object.h"
42 #include "got_repository.h"
43 #include "got_opentemp.h"
44 #include "got_path.h"
46 #include "got_lib_sha1.h"
47 #include "got_lib_delta.h"
48 #include "got_lib_inflate.h"
49 #include "got_lib_object.h"
50 #include "got_lib_privsep.h"
51 #include "got_lib_object_idcache.h"
52 #include "got_lib_object_cache.h"
53 #include "got_lib_object_parse.h"
54 #include "got_lib_pack.h"
55 #include "got_lib_repository.h"
57 #ifndef MIN
58 #define MIN(_a,_b) ((_a) < (_b) ? (_a) : (_b))
59 #endif
61 struct got_object_id *
62 got_object_get_id(struct got_object *obj)
63 {
64 return &obj->id;
65 }
67 const struct got_error *
68 got_object_get_id_str(char **outbuf, struct got_object *obj)
69 {
70 return got_object_id_str(outbuf, &obj->id);
71 }
73 const struct got_error *
74 got_object_get_type(int *type, struct got_repository *repo,
75 struct got_object_id *id)
76 {
77 const struct got_error *err = NULL;
78 struct got_object *obj;
80 err = got_object_open(&obj, repo, id);
81 if (err)
82 return err;
84 switch (obj->type) {
85 case GOT_OBJ_TYPE_COMMIT:
86 case GOT_OBJ_TYPE_TREE:
87 case GOT_OBJ_TYPE_BLOB:
88 case GOT_OBJ_TYPE_TAG:
89 *type = obj->type;
90 break;
91 default:
92 err = got_error(GOT_ERR_OBJ_TYPE);
93 break;
94 }
96 got_object_close(obj);
97 return err;
98 }
100 const struct got_error *
101 got_object_get_path(char **path, struct got_object_id *id,
102 struct got_repository *repo)
104 const struct got_error *err = NULL;
105 char *hex = NULL;
106 char *path_objects;
108 *path = NULL;
110 path_objects = got_repo_get_path_objects(repo);
111 if (path_objects == NULL)
112 return got_error_from_errno("got_repo_get_path_objects");
114 err = got_object_id_str(&hex, id);
115 if (err)
116 goto done;
118 if (asprintf(path, "%s/%.2x/%s", path_objects,
119 id->sha1[0], hex + 2) == -1)
120 err = got_error_from_errno("asprintf");
122 done:
123 free(hex);
124 free(path_objects);
125 return err;
128 const struct got_error *
129 got_object_open_loose_fd(int *fd, struct got_object_id *id,
130 struct got_repository *repo)
132 const struct got_error *err = NULL;
133 char *path;
135 err = got_object_get_path(&path, id, repo);
136 if (err)
137 return err;
138 *fd = open(path, O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
139 if (*fd == -1) {
140 err = got_error_from_errno2("open", path);
141 goto done;
143 done:
144 free(path);
145 return err;
148 static const struct got_error *
149 request_packed_object(struct got_object **obj, struct got_pack *pack, int idx,
150 struct got_object_id *id)
152 const struct got_error *err = NULL;
153 struct imsgbuf *ibuf = pack->privsep_child->ibuf;
155 err = got_privsep_send_packed_obj_req(ibuf, idx, id);
156 if (err)
157 return err;
159 err = got_privsep_recv_obj(obj, ibuf);
160 if (err)
161 return err;
163 memcpy(&(*obj)->id, id, sizeof((*obj)->id));
165 return NULL;
168 /* Create temporary files used during delta application. */
169 static const struct got_error *
170 pack_child_send_tempfiles(struct imsgbuf *ibuf, struct got_pack *pack)
172 const struct got_error *err;
173 int basefd, accumfd;
175 /*
176 * For performance reasons, the child will keep reusing the
177 * same temporary files during every object request.
178 * Opening and closing new files for every object request is
179 * too expensive during operations such as 'gotadmin pack'.
180 */
181 if (pack->child_has_tempfiles)
182 return NULL;
184 basefd = got_opentempfd();
185 if (basefd == -1)
186 return got_error_from_errno("got_opentempfd");
188 err = got_privsep_send_tmpfd(ibuf, basefd);
189 if (err)
190 return err;
192 accumfd = got_opentempfd();
193 if (accumfd == -1)
194 return got_error_from_errno("got_opentempfd");
196 err = got_privsep_send_tmpfd(ibuf, accumfd);
197 if (err)
198 return err;
200 pack->child_has_tempfiles = 1;
201 return NULL;
204 static const struct got_error *
205 request_packed_object_raw(uint8_t **outbuf, off_t *size, size_t *hdrlen,
206 int outfd, struct got_pack *pack, int idx, struct got_object_id *id)
208 const struct got_error *err = NULL;
209 struct imsgbuf *ibuf = pack->privsep_child->ibuf;
210 int outfd_child;
212 err = pack_child_send_tempfiles(ibuf, pack);
213 if (err)
214 return err;
216 outfd_child = dup(outfd);
217 if (outfd_child == -1)
218 return got_error_from_errno("dup");
220 err = got_privsep_send_packed_raw_obj_req(ibuf, idx, id);
221 if (err) {
222 close(outfd_child);
223 return err;
226 err = got_privsep_send_raw_obj_outfd(ibuf, outfd_child);
227 if (err)
228 return err;
230 err = got_privsep_recv_raw_obj(outbuf, size, hdrlen, ibuf);
231 if (err)
232 return err;
234 return NULL;
237 static void
238 set_max_datasize(void)
240 struct rlimit rl;
242 if (getrlimit(RLIMIT_DATA, &rl) != 0)
243 return;
245 rl.rlim_cur = rl.rlim_max;
246 setrlimit(RLIMIT_DATA, &rl);
249 static const struct got_error *
250 start_pack_privsep_child(struct got_pack *pack, struct got_packidx *packidx)
252 const struct got_error *err = NULL;
253 int imsg_fds[2];
254 pid_t pid;
255 struct imsgbuf *ibuf;
257 ibuf = calloc(1, sizeof(*ibuf));
258 if (ibuf == NULL)
259 return got_error_from_errno("calloc");
261 pack->privsep_child = calloc(1, sizeof(*pack->privsep_child));
262 if (pack->privsep_child == NULL) {
263 err = got_error_from_errno("calloc");
264 free(ibuf);
265 return err;
267 pack->child_has_tempfiles = 0;
269 if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_fds) == -1) {
270 err = got_error_from_errno("socketpair");
271 goto done;
274 pid = fork();
275 if (pid == -1) {
276 err = got_error_from_errno("fork");
277 goto done;
278 } else if (pid == 0) {
279 set_max_datasize();
280 got_privsep_exec_child(imsg_fds, GOT_PATH_PROG_READ_PACK,
281 pack->path_packfile);
282 /* not reached */
285 if (close(imsg_fds[1]) == -1)
286 return got_error_from_errno("close");
287 pack->privsep_child->imsg_fd = imsg_fds[0];
288 pack->privsep_child->pid = pid;
289 imsg_init(ibuf, imsg_fds[0]);
290 pack->privsep_child->ibuf = ibuf;
292 err = got_privsep_init_pack_child(ibuf, pack, packidx);
293 if (err) {
294 const struct got_error *child_err;
295 err = got_privsep_send_stop(pack->privsep_child->imsg_fd);
296 child_err = got_privsep_wait_for_child(
297 pack->privsep_child->pid);
298 if (child_err && err == NULL)
299 err = child_err;
301 done:
302 if (err) {
303 free(ibuf);
304 free(pack->privsep_child);
305 pack->privsep_child = NULL;
307 return err;
310 static const struct got_error *
311 read_packed_object_privsep(struct got_object **obj,
312 struct got_repository *repo, struct got_pack *pack,
313 struct got_packidx *packidx, int idx, struct got_object_id *id)
315 const struct got_error *err = NULL;
317 if (pack->privsep_child == NULL) {
318 err = start_pack_privsep_child(pack, packidx);
319 if (err)
320 return err;
323 return request_packed_object(obj, pack, idx, id);
326 static const struct got_error *
327 read_packed_object_raw_privsep(uint8_t **outbuf, off_t *size, size_t *hdrlen,
328 int outfd, struct got_pack *pack, struct got_packidx *packidx, int idx,
329 struct got_object_id *id)
331 const struct got_error *err = NULL;
333 if (pack->privsep_child == NULL) {
334 err = start_pack_privsep_child(pack, packidx);
335 if (err)
336 return err;
339 return request_packed_object_raw(outbuf, size, hdrlen, outfd, pack,
340 idx, id);
343 const struct got_error *
344 got_object_open_packed(struct got_object **obj, struct got_object_id *id,
345 struct got_repository *repo)
347 const struct got_error *err = NULL;
348 struct got_pack *pack = NULL;
349 struct got_packidx *packidx = NULL;
350 int idx;
351 char *path_packfile;
353 err = got_repo_search_packidx(&packidx, &idx, repo, id);
354 if (err)
355 return err;
357 err = got_packidx_get_packfile_path(&path_packfile,
358 packidx->path_packidx);
359 if (err)
360 return err;
362 pack = got_repo_get_cached_pack(repo, path_packfile);
363 if (pack == NULL) {
364 err = got_repo_cache_pack(&pack, repo, path_packfile, packidx);
365 if (err)
366 goto done;
369 err = read_packed_object_privsep(obj, repo, pack, packidx, idx, id);
370 if (err)
371 goto done;
372 done:
373 free(path_packfile);
374 return err;
377 static const struct got_error *
378 request_object(struct got_object **obj, struct got_object_id *id,
379 struct got_repository *repo, int fd)
381 const struct got_error *err = NULL;
382 struct imsgbuf *ibuf;
384 ibuf = repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_OBJECT].ibuf;
386 err = got_privsep_send_obj_req(ibuf, fd, id);
387 if (err)
388 return err;
390 return got_privsep_recv_obj(obj, ibuf);
393 static const struct got_error *
394 request_raw_object(uint8_t **outbuf, off_t *size, size_t *hdrlen, int outfd,
395 struct got_object_id *id, struct got_repository *repo, int infd)
397 const struct got_error *err = NULL;
398 struct imsgbuf *ibuf;
399 int outfd_child;
401 ibuf = repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_OBJECT].ibuf;
403 outfd_child = dup(outfd);
404 if (outfd_child == -1)
405 return got_error_from_errno("dup");
407 err = got_privsep_send_raw_obj_req(ibuf, infd, id);
408 if (err)
409 return err;
411 err = got_privsep_send_raw_obj_outfd(ibuf, outfd_child);
412 if (err)
413 return err;
415 return got_privsep_recv_raw_obj(outbuf, size, hdrlen, ibuf);
418 static const struct got_error *
419 start_read_object_child(struct got_repository *repo)
421 const struct got_error *err = NULL;
422 int imsg_fds[2];
423 pid_t pid;
424 struct imsgbuf *ibuf;
426 ibuf = calloc(1, sizeof(*ibuf));
427 if (ibuf == NULL)
428 return got_error_from_errno("calloc");
430 if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_fds) == -1) {
431 err = got_error_from_errno("socketpair");
432 free(ibuf);
433 return err;
436 pid = fork();
437 if (pid == -1) {
438 err = got_error_from_errno("fork");
439 free(ibuf);
440 return err;
442 else if (pid == 0) {
443 got_privsep_exec_child(imsg_fds, GOT_PATH_PROG_READ_OBJECT,
444 repo->path);
445 /* not reached */
448 if (close(imsg_fds[1]) == -1) {
449 err = got_error_from_errno("close");
450 free(ibuf);
451 return err;
454 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_OBJECT].imsg_fd =
455 imsg_fds[0];
456 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_OBJECT].pid = pid;
457 imsg_init(ibuf, imsg_fds[0]);
458 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_OBJECT].ibuf = ibuf;
460 return NULL;
463 const struct got_error *
464 got_object_read_header_privsep(struct got_object **obj,
465 struct got_object_id *id, struct got_repository *repo, int obj_fd)
467 const struct got_error *err;
469 if (repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_OBJECT].imsg_fd != -1)
470 return request_object(obj, id, repo, obj_fd);
472 err = start_read_object_child(repo);
473 if (err) {
474 close(obj_fd);
475 return err;
478 return request_object(obj, id, repo, obj_fd);
481 static const struct got_error *
482 read_object_raw_privsep(uint8_t **outbuf, off_t *size, size_t *hdrlen,
483 int outfd, struct got_object_id *id, struct got_repository *repo,
484 int obj_fd)
486 const struct got_error *err;
488 if (repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_OBJECT].imsg_fd != -1)
489 return request_raw_object(outbuf, size, hdrlen, outfd, id,
490 repo, obj_fd);
492 err = start_read_object_child(repo);
493 if (err)
494 return err;
496 return request_raw_object(outbuf, size, hdrlen, outfd, id, repo,
497 obj_fd);
500 const struct got_error *
501 got_object_open(struct got_object **obj, struct got_repository *repo,
502 struct got_object_id *id)
504 const struct got_error *err = NULL;
505 int fd;
507 *obj = got_repo_get_cached_object(repo, id);
508 if (*obj != NULL) {
509 (*obj)->refcnt++;
510 return NULL;
513 err = got_object_open_packed(obj, id, repo);
514 if (err && err->code != GOT_ERR_NO_OBJ)
515 return err;
516 if (*obj) {
517 (*obj)->refcnt++;
518 return got_repo_cache_object(repo, id, *obj);
521 err = got_object_open_loose_fd(&fd, id, repo);
522 if (err) {
523 if (err->code == GOT_ERR_ERRNO && errno == ENOENT)
524 err = got_error_no_obj(id);
525 return err;
528 err = got_object_read_header_privsep(obj, id, repo, fd);
529 if (err)
530 return err;
532 memcpy((*obj)->id.sha1, id->sha1, SHA1_DIGEST_LENGTH);
534 (*obj)->refcnt++;
535 return got_repo_cache_object(repo, id, *obj);
538 /* *outfd must be initialized to -1 by caller */
539 const struct got_error *
540 got_object_raw_open(struct got_raw_object **obj, int *outfd,
541 struct got_repository *repo, struct got_object_id *id)
543 const struct got_error *err = NULL;
544 struct got_packidx *packidx = NULL;
545 int idx;
546 uint8_t *outbuf = NULL;
547 off_t size = 0;
548 size_t hdrlen = 0;
549 char *path_packfile = NULL;
551 *obj = got_repo_get_cached_raw_object(repo, id);
552 if (*obj != NULL) {
553 (*obj)->refcnt++;
554 return NULL;
557 if (*outfd == -1) {
558 *outfd = got_opentempfd();
559 if (*outfd == -1)
560 return got_error_from_errno("got_opentempfd");
563 err = got_repo_search_packidx(&packidx, &idx, repo, id);
564 if (err == NULL) {
565 struct got_pack *pack = NULL;
567 err = got_packidx_get_packfile_path(&path_packfile,
568 packidx->path_packidx);
569 if (err)
570 goto done;
572 pack = got_repo_get_cached_pack(repo, path_packfile);
573 if (pack == NULL) {
574 err = got_repo_cache_pack(&pack, repo, path_packfile,
575 packidx);
576 if (err)
577 goto done;
579 err = read_packed_object_raw_privsep(&outbuf, &size, &hdrlen,
580 *outfd, pack, packidx, idx, id);
581 if (err)
582 goto done;
583 } else if (err->code == GOT_ERR_NO_OBJ) {
584 int fd;
586 err = got_object_open_loose_fd(&fd, id, repo);
587 if (err)
588 goto done;
589 err = read_object_raw_privsep(&outbuf, &size, &hdrlen, *outfd,
590 id, repo, fd);
591 if (err)
592 goto done;
595 *obj = calloc(1, sizeof(**obj));
596 if (*obj == NULL) {
597 err = got_error_from_errno("calloc");
598 goto done;
600 (*obj)->fd = -1;
602 if (outbuf) {
603 (*obj)->data = outbuf;
604 } else {
605 struct stat sb;
606 if (fstat(*outfd, &sb) == -1) {
607 err = got_error_from_errno("fstat");
608 goto done;
611 if (sb.st_size != hdrlen + size) {
612 err = got_error(GOT_ERR_PRIVSEP_LEN);
613 goto done;
615 #ifndef GOT_PACK_NO_MMAP
616 if (hdrlen + size > 0) {
617 (*obj)->data = mmap(NULL, hdrlen + size, PROT_READ,
618 MAP_PRIVATE, *outfd, 0);
619 if ((*obj)->data == MAP_FAILED) {
620 if (errno != ENOMEM) {
621 err = got_error_from_errno("mmap");
622 goto done;
624 (*obj)->data = NULL;
625 } else {
626 (*obj)->fd = *outfd;
627 *outfd = -1;
630 #endif
631 if (*outfd != -1) {
632 (*obj)->f = fdopen(*outfd, "r");
633 if ((*obj)->f == NULL) {
634 err = got_error_from_errno("fdopen");
635 goto done;
637 *outfd = -1;
640 (*obj)->hdrlen = hdrlen;
641 (*obj)->size = size;
642 err = got_repo_cache_raw_object(repo, id, *obj);
643 done:
644 free(path_packfile);
645 if (err) {
646 if (*obj) {
647 got_object_raw_close(*obj);
648 *obj = NULL;
650 free(outbuf);
651 } else
652 (*obj)->refcnt++;
653 return err;
656 const struct got_error *
657 got_object_open_by_id_str(struct got_object **obj, struct got_repository *repo,
658 const char *id_str)
660 struct got_object_id id;
662 if (!got_parse_sha1_digest(id.sha1, id_str))
663 return got_error_path(id_str, GOT_ERR_BAD_OBJ_ID_STR);
665 return got_object_open(obj, repo, &id);
668 const struct got_error *
669 got_object_resolve_id_str(struct got_object_id **id,
670 struct got_repository *repo, const char *id_str)
672 const struct got_error *err = NULL;
673 struct got_object *obj;
675 err = got_object_open_by_id_str(&obj, repo, id_str);
676 if (err)
677 return err;
679 *id = got_object_id_dup(got_object_get_id(obj));
680 got_object_close(obj);
681 if (*id == NULL)
682 return got_error_from_errno("got_object_id_dup");
684 return NULL;
687 static const struct got_error *
688 request_packed_commit(struct got_commit_object **commit, struct got_pack *pack,
689 int pack_idx, struct got_object_id *id)
691 const struct got_error *err = NULL;
693 err = got_privsep_send_commit_req(pack->privsep_child->ibuf, -1, id,
694 pack_idx);
695 if (err)
696 return err;
698 err = got_privsep_recv_commit(commit, pack->privsep_child->ibuf);
699 if (err)
700 return err;
702 (*commit)->flags |= GOT_COMMIT_FLAG_PACKED;
703 return NULL;
706 static const struct got_error *
707 read_packed_commit_privsep(struct got_commit_object **commit,
708 struct got_pack *pack, struct got_packidx *packidx, int idx,
709 struct got_object_id *id)
711 const struct got_error *err = NULL;
713 if (pack->privsep_child)
714 return request_packed_commit(commit, pack, idx, id);
716 err = start_pack_privsep_child(pack, packidx);
717 if (err)
718 return err;
720 return request_packed_commit(commit, pack, idx, id);
723 static const struct got_error *
724 request_commit(struct got_commit_object **commit, struct got_repository *repo,
725 int fd, struct got_object_id *id)
727 const struct got_error *err = NULL;
728 struct imsgbuf *ibuf;
730 ibuf = repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_COMMIT].ibuf;
732 err = got_privsep_send_commit_req(ibuf, fd, id, -1);
733 if (err)
734 return err;
736 return got_privsep_recv_commit(commit, ibuf);
739 static const struct got_error *
740 read_commit_privsep(struct got_commit_object **commit, int obj_fd,
741 struct got_object_id *id, struct got_repository *repo)
743 const struct got_error *err;
744 int imsg_fds[2];
745 pid_t pid;
746 struct imsgbuf *ibuf;
748 if (repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_COMMIT].imsg_fd != -1)
749 return request_commit(commit, repo, obj_fd, id);
751 ibuf = calloc(1, sizeof(*ibuf));
752 if (ibuf == NULL)
753 return got_error_from_errno("calloc");
755 if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_fds) == -1) {
756 err = got_error_from_errno("socketpair");
757 free(ibuf);
758 return err;
761 pid = fork();
762 if (pid == -1) {
763 err = got_error_from_errno("fork");
764 free(ibuf);
765 return err;
767 else if (pid == 0) {
768 got_privsep_exec_child(imsg_fds, GOT_PATH_PROG_READ_COMMIT,
769 repo->path);
770 /* not reached */
773 if (close(imsg_fds[1]) == -1) {
774 err = got_error_from_errno("close");
775 free(ibuf);
776 return err;
778 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_COMMIT].imsg_fd =
779 imsg_fds[0];
780 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_COMMIT].pid = pid;
781 imsg_init(ibuf, imsg_fds[0]);
782 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_COMMIT].ibuf = ibuf;
784 return request_commit(commit, repo, obj_fd, id);
788 static const struct got_error *
789 open_commit(struct got_commit_object **commit,
790 struct got_repository *repo, struct got_object_id *id, int check_cache)
792 const struct got_error *err = NULL;
793 struct got_packidx *packidx = NULL;
794 int idx;
795 char *path_packfile = NULL;
797 if (check_cache) {
798 *commit = got_repo_get_cached_commit(repo, id);
799 if (*commit != NULL) {
800 (*commit)->refcnt++;
801 return NULL;
803 } else
804 *commit = NULL;
806 err = got_repo_search_packidx(&packidx, &idx, repo, id);
807 if (err == NULL) {
808 struct got_pack *pack = NULL;
810 err = got_packidx_get_packfile_path(&path_packfile,
811 packidx->path_packidx);
812 if (err)
813 return err;
815 pack = got_repo_get_cached_pack(repo, path_packfile);
816 if (pack == NULL) {
817 err = got_repo_cache_pack(&pack, repo, path_packfile,
818 packidx);
819 if (err)
820 goto done;
822 err = read_packed_commit_privsep(commit, pack,
823 packidx, idx, id);
824 } else if (err->code == GOT_ERR_NO_OBJ) {
825 int fd;
827 err = got_object_open_loose_fd(&fd, id, repo);
828 if (err)
829 return err;
830 err = read_commit_privsep(commit, fd, id, repo);
833 if (err == NULL) {
834 (*commit)->refcnt++;
835 err = got_repo_cache_commit(repo, id, *commit);
837 done:
838 free(path_packfile);
839 return err;
842 const struct got_error *
843 got_object_open_as_commit(struct got_commit_object **commit,
844 struct got_repository *repo, struct got_object_id *id)
846 *commit = got_repo_get_cached_commit(repo, id);
847 if (*commit != NULL) {
848 (*commit)->refcnt++;
849 return NULL;
852 return open_commit(commit, repo, id, 0);
855 const struct got_error *
856 got_object_commit_open(struct got_commit_object **commit,
857 struct got_repository *repo, struct got_object *obj)
859 return open_commit(commit, repo, got_object_get_id(obj), 1);
862 const struct got_error *
863 got_object_qid_alloc(struct got_object_qid **qid, struct got_object_id *id)
865 const struct got_error *err = NULL;
867 *qid = calloc(1, sizeof(**qid));
868 if (*qid == NULL)
869 return got_error_from_errno("calloc");
871 (*qid)->id = got_object_id_dup(id);
872 if ((*qid)->id == NULL) {
873 err = got_error_from_errno("got_object_id_dup");
874 got_object_qid_free(*qid);
875 *qid = NULL;
876 return err;
879 return NULL;
882 const struct got_error *
883 got_object_id_queue_copy(const struct got_object_id_queue *src,
884 struct got_object_id_queue *dest)
886 const struct got_error *err;
887 struct got_object_qid *qid;
889 STAILQ_FOREACH(qid, src, entry) {
890 struct got_object_qid *new;
891 /*
892 * Deep-copy the object ID only. Let the caller deal
893 * with setting up the new->data pointer if needed.
894 */
895 err = got_object_qid_alloc(&new, qid->id);
896 if (err) {
897 got_object_id_queue_free(dest);
898 return err;
900 STAILQ_INSERT_TAIL(dest, new, entry);
903 return NULL;
906 static const struct got_error *
907 request_packed_tree(struct got_tree_object **tree, struct got_pack *pack,
908 int pack_idx, struct got_object_id *id)
910 const struct got_error *err = NULL;
912 err = got_privsep_send_tree_req(pack->privsep_child->ibuf, -1, id,
913 pack_idx);
914 if (err)
915 return err;
917 return got_privsep_recv_tree(tree, pack->privsep_child->ibuf);
920 static const struct got_error *
921 read_packed_tree_privsep(struct got_tree_object **tree,
922 struct got_pack *pack, struct got_packidx *packidx, int idx,
923 struct got_object_id *id)
925 const struct got_error *err = NULL;
927 if (pack->privsep_child)
928 return request_packed_tree(tree, pack, idx, id);
930 err = start_pack_privsep_child(pack, packidx);
931 if (err)
932 return err;
934 return request_packed_tree(tree, pack, idx, id);
937 static const struct got_error *
938 request_tree(struct got_tree_object **tree, struct got_repository *repo,
939 int fd, struct got_object_id *id)
941 const struct got_error *err = NULL;
942 struct imsgbuf *ibuf;
944 ibuf = repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TREE].ibuf;
946 err = got_privsep_send_tree_req(ibuf, fd, id, -1);
947 if (err)
948 return err;
950 return got_privsep_recv_tree(tree, ibuf);
953 const struct got_error *
954 read_tree_privsep(struct got_tree_object **tree, int obj_fd,
955 struct got_object_id *id, struct got_repository *repo)
957 const struct got_error *err;
958 int imsg_fds[2];
959 pid_t pid;
960 struct imsgbuf *ibuf;
962 if (repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TREE].imsg_fd != -1)
963 return request_tree(tree, repo, obj_fd, id);
965 ibuf = calloc(1, sizeof(*ibuf));
966 if (ibuf == NULL)
967 return got_error_from_errno("calloc");
969 if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_fds) == -1) {
970 err = got_error_from_errno("socketpair");
971 free(ibuf);
972 return err;
975 pid = fork();
976 if (pid == -1) {
977 err = got_error_from_errno("fork");
978 free(ibuf);
979 return err;
981 else if (pid == 0) {
982 got_privsep_exec_child(imsg_fds, GOT_PATH_PROG_READ_TREE,
983 repo->path);
984 /* not reached */
987 if (close(imsg_fds[1]) == -1) {
988 err = got_error_from_errno("close");
989 free(ibuf);
990 return err;
992 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TREE].imsg_fd =
993 imsg_fds[0];
994 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TREE].pid = pid;
995 imsg_init(ibuf, imsg_fds[0]);
996 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TREE].ibuf = ibuf;
999 return request_tree(tree, repo, obj_fd, id);
1002 static const struct got_error *
1003 open_tree(struct got_tree_object **tree, struct got_repository *repo,
1004 struct got_object_id *id, int check_cache)
1006 const struct got_error *err = NULL;
1007 struct got_packidx *packidx = NULL;
1008 int idx;
1009 char *path_packfile = NULL;
1011 if (check_cache) {
1012 *tree = got_repo_get_cached_tree(repo, id);
1013 if (*tree != NULL) {
1014 (*tree)->refcnt++;
1015 return NULL;
1017 } else
1018 *tree = NULL;
1020 err = got_repo_search_packidx(&packidx, &idx, repo, id);
1021 if (err == NULL) {
1022 struct got_pack *pack = NULL;
1024 err = got_packidx_get_packfile_path(&path_packfile,
1025 packidx->path_packidx);
1026 if (err)
1027 return err;
1029 pack = got_repo_get_cached_pack(repo, path_packfile);
1030 if (pack == NULL) {
1031 err = got_repo_cache_pack(&pack, repo, path_packfile,
1032 packidx);
1033 if (err)
1034 goto done;
1036 err = read_packed_tree_privsep(tree, pack,
1037 packidx, idx, id);
1038 } else if (err->code == GOT_ERR_NO_OBJ) {
1039 int fd;
1041 err = got_object_open_loose_fd(&fd, id, repo);
1042 if (err)
1043 return err;
1044 err = read_tree_privsep(tree, fd, id, repo);
1047 if (err == NULL) {
1048 (*tree)->refcnt++;
1049 err = got_repo_cache_tree(repo, id, *tree);
1051 done:
1052 free(path_packfile);
1053 return err;
1056 const struct got_error *
1057 got_object_open_as_tree(struct got_tree_object **tree,
1058 struct got_repository *repo, struct got_object_id *id)
1060 *tree = got_repo_get_cached_tree(repo, id);
1061 if (*tree != NULL) {
1062 (*tree)->refcnt++;
1063 return NULL;
1066 return open_tree(tree, repo, id, 0);
1069 const struct got_error *
1070 got_object_tree_open(struct got_tree_object **tree,
1071 struct got_repository *repo, struct got_object *obj)
1073 return open_tree(tree, repo, got_object_get_id(obj), 1);
1076 int
1077 got_object_tree_get_nentries(struct got_tree_object *tree)
1079 return tree->nentries;
1082 struct got_tree_entry *
1083 got_object_tree_get_first_entry(struct got_tree_object *tree)
1085 return got_object_tree_get_entry(tree, 0);
1088 struct got_tree_entry *
1089 got_object_tree_get_last_entry(struct got_tree_object *tree)
1091 return got_object_tree_get_entry(tree, tree->nentries - 1);
1094 struct got_tree_entry *
1095 got_object_tree_get_entry(struct got_tree_object *tree, int i)
1097 if (i < 0 || i >= tree->nentries)
1098 return NULL;
1099 return &tree->entries[i];
1102 mode_t
1103 got_tree_entry_get_mode(struct got_tree_entry *te)
1105 return te->mode;
1108 const char *
1109 got_tree_entry_get_name(struct got_tree_entry *te)
1111 return &te->name[0];
1114 struct got_object_id *
1115 got_tree_entry_get_id(struct got_tree_entry *te)
1117 return &te->id;
1120 const struct got_error *
1121 got_object_blob_read_to_str(char **s, struct got_blob_object *blob)
1123 const struct got_error *err = NULL;
1124 size_t len, totlen, hdrlen, offset;
1126 *s = NULL;
1128 hdrlen = got_object_blob_get_hdrlen(blob);
1129 totlen = 0;
1130 offset = 0;
1131 do {
1132 char *p;
1134 err = got_object_blob_read_block(&len, blob);
1135 if (err)
1136 return err;
1138 if (len == 0)
1139 break;
1141 totlen += len - hdrlen;
1142 p = realloc(*s, totlen + 1);
1143 if (p == NULL) {
1144 err = got_error_from_errno("realloc");
1145 free(*s);
1146 *s = NULL;
1147 return err;
1149 *s = p;
1150 /* Skip blob object header first time around. */
1151 memcpy(*s + offset,
1152 got_object_blob_get_read_buf(blob) + hdrlen, len - hdrlen);
1153 hdrlen = 0;
1154 offset = totlen;
1155 } while (len > 0);
1157 (*s)[totlen] = '\0';
1158 return NULL;
1161 const struct got_error *
1162 got_tree_entry_get_symlink_target(char **link_target, struct got_tree_entry *te,
1163 struct got_repository *repo)
1165 const struct got_error *err = NULL;
1166 struct got_blob_object *blob = NULL;
1168 *link_target = NULL;
1170 if (!got_object_tree_entry_is_symlink(te))
1171 return got_error(GOT_ERR_TREE_ENTRY_TYPE);
1173 err = got_object_open_as_blob(&blob, repo,
1174 got_tree_entry_get_id(te), PATH_MAX);
1175 if (err)
1176 return err;
1178 err = got_object_blob_read_to_str(link_target, blob);
1179 got_object_blob_close(blob);
1180 if (err) {
1181 free(*link_target);
1182 *link_target = NULL;
1184 return err;
1187 int
1188 got_tree_entry_get_index(struct got_tree_entry *te)
1190 return te->idx;
1193 struct got_tree_entry *
1194 got_tree_entry_get_next(struct got_tree_object *tree,
1195 struct got_tree_entry *te)
1197 return got_object_tree_get_entry(tree, te->idx + 1);
1200 struct got_tree_entry *
1201 got_tree_entry_get_prev(struct got_tree_object *tree,
1202 struct got_tree_entry *te)
1204 return got_object_tree_get_entry(tree, te->idx - 1);
1207 static const struct got_error *
1208 request_packed_blob(uint8_t **outbuf, size_t *size, size_t *hdrlen, int outfd,
1209 struct got_pack *pack, struct got_packidx *packidx, int idx,
1210 struct got_object_id *id)
1212 const struct got_error *err = NULL;
1213 struct imsgbuf *ibuf = pack->privsep_child->ibuf;
1214 int outfd_child;
1216 err = pack_child_send_tempfiles(ibuf, pack);
1217 if (err)
1218 return err;
1220 outfd_child = dup(outfd);
1221 if (outfd_child == -1)
1222 return got_error_from_errno("dup");
1224 err = got_privsep_send_blob_req(pack->privsep_child->ibuf, -1, id, idx);
1225 if (err)
1226 return err;
1228 err = got_privsep_send_blob_outfd(pack->privsep_child->ibuf,
1229 outfd_child);
1230 if (err) {
1231 return err;
1234 err = got_privsep_recv_blob(outbuf, size, hdrlen,
1235 pack->privsep_child->ibuf);
1236 if (err)
1237 return err;
1239 if (lseek(outfd, SEEK_SET, 0) == -1)
1240 err = got_error_from_errno("lseek");
1242 return err;
1245 static const struct got_error *
1246 read_packed_blob_privsep(uint8_t **outbuf, size_t *size, size_t *hdrlen,
1247 int outfd, struct got_pack *pack, struct got_packidx *packidx, int idx,
1248 struct got_object_id *id)
1250 const struct got_error *err = NULL;
1252 if (pack->privsep_child == NULL) {
1253 err = start_pack_privsep_child(pack, packidx);
1254 if (err)
1255 return err;
1258 return request_packed_blob(outbuf, size, hdrlen, outfd, pack, packidx,
1259 idx, id);
1262 static const struct got_error *
1263 request_blob(uint8_t **outbuf, size_t *size, size_t *hdrlen, int outfd,
1264 int infd, struct got_object_id *id, struct imsgbuf *ibuf)
1266 const struct got_error *err = NULL;
1267 int outfd_child;
1269 outfd_child = dup(outfd);
1270 if (outfd_child == -1)
1271 return got_error_from_errno("dup");
1273 err = got_privsep_send_blob_req(ibuf, infd, id, -1);
1274 if (err)
1275 return err;
1277 err = got_privsep_send_blob_outfd(ibuf, outfd_child);
1278 if (err)
1279 return err;
1281 err = got_privsep_recv_blob(outbuf, size, hdrlen, ibuf);
1282 if (err)
1283 return err;
1285 if (lseek(outfd, SEEK_SET, 0) == -1)
1286 return got_error_from_errno("lseek");
1288 return err;
1291 static const struct got_error *
1292 read_blob_privsep(uint8_t **outbuf, size_t *size, size_t *hdrlen,
1293 int outfd, int infd, struct got_object_id *id, struct got_repository *repo)
1295 const struct got_error *err;
1296 int imsg_fds[2];
1297 pid_t pid;
1298 struct imsgbuf *ibuf;
1300 if (repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_BLOB].imsg_fd != -1) {
1301 ibuf = repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_BLOB].ibuf;
1302 return request_blob(outbuf, size, hdrlen, outfd, infd, id,
1303 ibuf);
1306 ibuf = calloc(1, sizeof(*ibuf));
1307 if (ibuf == NULL)
1308 return got_error_from_errno("calloc");
1310 if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_fds) == -1) {
1311 err = got_error_from_errno("socketpair");
1312 free(ibuf);
1313 return err;
1316 pid = fork();
1317 if (pid == -1) {
1318 err = got_error_from_errno("fork");
1319 free(ibuf);
1320 return err;
1322 else if (pid == 0) {
1323 got_privsep_exec_child(imsg_fds, GOT_PATH_PROG_READ_BLOB,
1324 repo->path);
1325 /* not reached */
1328 if (close(imsg_fds[1]) == -1) {
1329 err = got_error_from_errno("close");
1330 free(ibuf);
1331 return err;
1333 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_BLOB].imsg_fd =
1334 imsg_fds[0];
1335 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_BLOB].pid = pid;
1336 imsg_init(ibuf, imsg_fds[0]);
1337 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_BLOB].ibuf = ibuf;
1339 return request_blob(outbuf, size, hdrlen, outfd, infd, id, ibuf);
1342 static const struct got_error *
1343 open_blob(struct got_blob_object **blob, struct got_repository *repo,
1344 struct got_object_id *id, size_t blocksize)
1346 const struct got_error *err = NULL;
1347 struct got_packidx *packidx = NULL;
1348 int idx;
1349 char *path_packfile = NULL;
1350 uint8_t *outbuf;
1351 int outfd;
1352 size_t size, hdrlen;
1353 struct stat sb;
1355 *blob = calloc(1, sizeof(**blob));
1356 if (*blob == NULL)
1357 return got_error_from_errno("calloc");
1359 outfd = got_opentempfd();
1360 if (outfd == -1)
1361 return got_error_from_errno("got_opentempfd");
1363 (*blob)->read_buf = malloc(blocksize);
1364 if ((*blob)->read_buf == NULL) {
1365 err = got_error_from_errno("malloc");
1366 goto done;
1369 err = got_repo_search_packidx(&packidx, &idx, repo, id);
1370 if (err == NULL) {
1371 struct got_pack *pack = NULL;
1373 err = got_packidx_get_packfile_path(&path_packfile,
1374 packidx->path_packidx);
1375 if (err)
1376 goto done;
1378 pack = got_repo_get_cached_pack(repo, path_packfile);
1379 if (pack == NULL) {
1380 err = got_repo_cache_pack(&pack, repo, path_packfile,
1381 packidx);
1382 if (err)
1383 goto done;
1385 err = read_packed_blob_privsep(&outbuf, &size, &hdrlen, outfd,
1386 pack, packidx, idx, id);
1387 } else if (err->code == GOT_ERR_NO_OBJ) {
1388 int infd;
1390 err = got_object_open_loose_fd(&infd, id, repo);
1391 if (err)
1392 goto done;
1393 err = read_blob_privsep(&outbuf, &size, &hdrlen, outfd, infd,
1394 id, repo);
1396 if (err)
1397 goto done;
1399 if (hdrlen > size) {
1400 err = got_error(GOT_ERR_BAD_OBJ_HDR);
1401 goto done;
1404 if (outbuf) {
1405 if (close(outfd) == -1 && err == NULL)
1406 err = got_error_from_errno("close");
1407 outfd = -1;
1408 (*blob)->f = fmemopen(outbuf, size, "rb");
1409 if ((*blob)->f == NULL) {
1410 err = got_error_from_errno("fmemopen");
1411 free(outbuf);
1412 goto done;
1414 (*blob)->data = outbuf;
1415 } else {
1416 if (fstat(outfd, &sb) == -1) {
1417 err = got_error_from_errno("fstat");
1418 goto done;
1421 if (sb.st_size != size) {
1422 err = got_error(GOT_ERR_PRIVSEP_LEN);
1423 goto done;
1426 (*blob)->f = fdopen(outfd, "rb");
1427 if ((*blob)->f == NULL) {
1428 err = got_error_from_errno("fdopen");
1429 close(outfd);
1430 outfd = -1;
1431 goto done;
1435 (*blob)->hdrlen = hdrlen;
1436 (*blob)->blocksize = blocksize;
1437 memcpy(&(*blob)->id.sha1, id->sha1, SHA1_DIGEST_LENGTH);
1439 done:
1440 free(path_packfile);
1441 if (err) {
1442 if (*blob) {
1443 got_object_blob_close(*blob);
1444 *blob = NULL;
1445 } else if (outfd != -1)
1446 close(outfd);
1448 return err;
1451 const struct got_error *
1452 got_object_open_as_blob(struct got_blob_object **blob,
1453 struct got_repository *repo, struct got_object_id *id,
1454 size_t blocksize)
1456 return open_blob(blob, repo, id, blocksize);
1459 const struct got_error *
1460 got_object_blob_open(struct got_blob_object **blob,
1461 struct got_repository *repo, struct got_object *obj, size_t blocksize)
1463 return open_blob(blob, repo, got_object_get_id(obj), blocksize);
1466 const struct got_error *
1467 got_object_blob_close(struct got_blob_object *blob)
1469 const struct got_error *err = NULL;
1470 free(blob->read_buf);
1471 if (blob->f && fclose(blob->f) == EOF)
1472 err = got_error_from_errno("fclose");
1473 free(blob->data);
1474 free(blob);
1475 return err;
1478 void
1479 got_object_blob_rewind(struct got_blob_object *blob)
1481 if (blob->f)
1482 rewind(blob->f);
1485 char *
1486 got_object_blob_id_str(struct got_blob_object *blob, char *buf, size_t size)
1488 return got_sha1_digest_to_str(blob->id.sha1, buf, size);
1491 size_t
1492 got_object_blob_get_hdrlen(struct got_blob_object *blob)
1494 return blob->hdrlen;
1497 const uint8_t *
1498 got_object_blob_get_read_buf(struct got_blob_object *blob)
1500 return blob->read_buf;
1503 const struct got_error *
1504 got_object_blob_read_block(size_t *outlenp, struct got_blob_object *blob)
1506 size_t n;
1508 n = fread(blob->read_buf, 1, blob->blocksize, blob->f);
1509 if (n == 0 && ferror(blob->f))
1510 return got_ferror(blob->f, GOT_ERR_IO);
1511 *outlenp = n;
1512 return NULL;
1515 const struct got_error *
1516 got_object_blob_dump_to_file(off_t *filesize, int *nlines,
1517 off_t **line_offsets, FILE *outfile, struct got_blob_object *blob)
1519 const struct got_error *err = NULL;
1520 size_t n, len, hdrlen;
1521 const uint8_t *buf;
1522 int i;
1523 const int alloc_chunksz = 512;
1524 size_t nalloc = 0;
1525 off_t off = 0, total_len = 0;
1527 if (line_offsets)
1528 *line_offsets = NULL;
1529 if (filesize)
1530 *filesize = 0;
1531 if (nlines)
1532 *nlines = 0;
1534 hdrlen = got_object_blob_get_hdrlen(blob);
1535 do {
1536 err = got_object_blob_read_block(&len, blob);
1537 if (err)
1538 return err;
1539 if (len == 0)
1540 break;
1541 buf = got_object_blob_get_read_buf(blob);
1542 i = hdrlen;
1543 if (nlines) {
1544 if (line_offsets && *line_offsets == NULL) {
1545 /* Have some data but perhaps no '\n'. */
1546 *nlines = 1;
1547 nalloc = alloc_chunksz;
1548 *line_offsets = calloc(nalloc,
1549 sizeof(**line_offsets));
1550 if (*line_offsets == NULL)
1551 return got_error_from_errno("calloc");
1553 /* Skip forward over end of first line. */
1554 while (i < len) {
1555 if (buf[i] == '\n')
1556 break;
1557 i++;
1560 /* Scan '\n' offsets in remaining chunk of data. */
1561 while (i < len) {
1562 if (buf[i] != '\n') {
1563 i++;
1564 continue;
1566 (*nlines)++;
1567 if (line_offsets && nalloc < *nlines) {
1568 size_t n = *nlines + alloc_chunksz;
1569 off_t *o = recallocarray(*line_offsets,
1570 nalloc, n, sizeof(**line_offsets));
1571 if (o == NULL) {
1572 free(*line_offsets);
1573 *line_offsets = NULL;
1574 return got_error_from_errno(
1575 "recallocarray");
1577 *line_offsets = o;
1578 nalloc = n;
1580 if (line_offsets) {
1581 off = total_len + i - hdrlen + 1;
1582 (*line_offsets)[*nlines - 1] = off;
1584 i++;
1587 /* Skip blob object header first time around. */
1588 n = fwrite(buf + hdrlen, 1, len - hdrlen, outfile);
1589 if (n != len - hdrlen)
1590 return got_ferror(outfile, GOT_ERR_IO);
1591 total_len += len - hdrlen;
1592 hdrlen = 0;
1593 } while (len != 0);
1595 if (fflush(outfile) != 0)
1596 return got_error_from_errno("fflush");
1597 rewind(outfile);
1599 if (filesize)
1600 *filesize = total_len;
1602 return NULL;
1605 static const struct got_error *
1606 request_packed_tag(struct got_tag_object **tag, struct got_pack *pack,
1607 int pack_idx, struct got_object_id *id)
1609 const struct got_error *err = NULL;
1611 err = got_privsep_send_tag_req(pack->privsep_child->ibuf, -1, id,
1612 pack_idx);
1613 if (err)
1614 return err;
1616 return got_privsep_recv_tag(tag, pack->privsep_child->ibuf);
1619 static const struct got_error *
1620 read_packed_tag_privsep(struct got_tag_object **tag,
1621 struct got_pack *pack, struct got_packidx *packidx, int idx,
1622 struct got_object_id *id)
1624 const struct got_error *err = NULL;
1626 if (pack->privsep_child)
1627 return request_packed_tag(tag, pack, idx, id);
1629 err = start_pack_privsep_child(pack, packidx);
1630 if (err)
1631 return err;
1633 return request_packed_tag(tag, pack, idx, id);
1636 static const struct got_error *
1637 request_tag(struct got_tag_object **tag, struct got_repository *repo,
1638 int fd, struct got_object_id *id)
1640 const struct got_error *err = NULL;
1641 struct imsgbuf *ibuf;
1643 ibuf = repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TAG].ibuf;
1645 err = got_privsep_send_tag_req(ibuf, fd, id, -1);
1646 if (err)
1647 return err;
1649 return got_privsep_recv_tag(tag, ibuf);
1652 static const struct got_error *
1653 read_tag_privsep(struct got_tag_object **tag, int obj_fd,
1654 struct got_object_id *id, struct got_repository *repo)
1656 const struct got_error *err;
1657 int imsg_fds[2];
1658 pid_t pid;
1659 struct imsgbuf *ibuf;
1661 if (repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TAG].imsg_fd != -1)
1662 return request_tag(tag, repo, obj_fd, id);
1664 ibuf = calloc(1, sizeof(*ibuf));
1665 if (ibuf == NULL)
1666 return got_error_from_errno("calloc");
1668 if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_fds) == -1) {
1669 err = got_error_from_errno("socketpair");
1670 free(ibuf);
1671 return err;
1674 pid = fork();
1675 if (pid == -1) {
1676 err = got_error_from_errno("fork");
1677 free(ibuf);
1678 return err;
1680 else if (pid == 0) {
1681 got_privsep_exec_child(imsg_fds, GOT_PATH_PROG_READ_TAG,
1682 repo->path);
1683 /* not reached */
1686 if (close(imsg_fds[1]) == -1) {
1687 err = got_error_from_errno("close");
1688 free(ibuf);
1689 return err;
1691 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TAG].imsg_fd =
1692 imsg_fds[0];
1693 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TAG].pid = pid;
1694 imsg_init(ibuf, imsg_fds[0]);
1695 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TAG].ibuf = ibuf;
1697 return request_tag(tag, repo, obj_fd, id);
1700 static const struct got_error *
1701 open_tag(struct got_tag_object **tag, struct got_repository *repo,
1702 struct got_object_id *id, int check_cache)
1704 const struct got_error *err = NULL;
1705 struct got_packidx *packidx = NULL;
1706 int idx;
1707 char *path_packfile = NULL;
1708 struct got_object *obj = NULL;
1709 int obj_type = GOT_OBJ_TYPE_ANY;
1711 if (check_cache) {
1712 *tag = got_repo_get_cached_tag(repo, id);
1713 if (*tag != NULL) {
1714 (*tag)->refcnt++;
1715 return NULL;
1717 } else
1718 *tag = NULL;
1720 err = got_repo_search_packidx(&packidx, &idx, repo, id);
1721 if (err == NULL) {
1722 struct got_pack *pack = NULL;
1724 err = got_packidx_get_packfile_path(&path_packfile,
1725 packidx->path_packidx);
1726 if (err)
1727 return err;
1729 pack = got_repo_get_cached_pack(repo, path_packfile);
1730 if (pack == NULL) {
1731 err = got_repo_cache_pack(&pack, repo, path_packfile,
1732 packidx);
1733 if (err)
1734 goto done;
1737 /* Beware of "lightweight" tags: Check object type first. */
1738 err = read_packed_object_privsep(&obj, repo, pack, packidx,
1739 idx, id);
1740 if (err)
1741 goto done;
1742 obj_type = obj->type;
1743 got_object_close(obj);
1744 if (obj_type != GOT_OBJ_TYPE_TAG) {
1745 err = got_error(GOT_ERR_OBJ_TYPE);
1746 goto done;
1748 err = read_packed_tag_privsep(tag, pack, packidx, idx, id);
1749 } else if (err->code == GOT_ERR_NO_OBJ) {
1750 int fd;
1752 err = got_object_open_loose_fd(&fd, id, repo);
1753 if (err)
1754 return err;
1755 err = got_object_read_header_privsep(&obj, id, repo, fd);
1756 if (err)
1757 return err;
1758 obj_type = obj->type;
1759 got_object_close(obj);
1760 if (obj_type != GOT_OBJ_TYPE_TAG)
1761 return got_error(GOT_ERR_OBJ_TYPE);
1763 err = got_object_open_loose_fd(&fd, id, repo);
1764 if (err)
1765 return err;
1766 err = read_tag_privsep(tag, fd, id, repo);
1769 if (err == NULL) {
1770 (*tag)->refcnt++;
1771 err = got_repo_cache_tag(repo, id, *tag);
1773 done:
1774 free(path_packfile);
1775 return err;
1778 const struct got_error *
1779 got_object_open_as_tag(struct got_tag_object **tag,
1780 struct got_repository *repo, struct got_object_id *id)
1782 *tag = got_repo_get_cached_tag(repo, id);
1783 if (*tag != NULL) {
1784 (*tag)->refcnt++;
1785 return NULL;
1788 return open_tag(tag, repo, id, 0);
1791 const struct got_error *
1792 got_object_tag_open(struct got_tag_object **tag,
1793 struct got_repository *repo, struct got_object *obj)
1795 return open_tag(tag, repo, got_object_get_id(obj), 1);
1798 const char *
1799 got_object_tag_get_name(struct got_tag_object *tag)
1801 return tag->tag;
1804 int
1805 got_object_tag_get_object_type(struct got_tag_object *tag)
1807 return tag->obj_type;
1810 struct got_object_id *
1811 got_object_tag_get_object_id(struct got_tag_object *tag)
1813 return &tag->id;
1816 time_t
1817 got_object_tag_get_tagger_time(struct got_tag_object *tag)
1819 return tag->tagger_time;
1822 time_t
1823 got_object_tag_get_tagger_gmtoff(struct got_tag_object *tag)
1825 return tag->tagger_gmtoff;
1828 const char *
1829 got_object_tag_get_tagger(struct got_tag_object *tag)
1831 return tag->tagger;
1834 const char *
1835 got_object_tag_get_message(struct got_tag_object *tag)
1837 return tag->tagmsg;
1840 static struct got_tree_entry *
1841 find_entry_by_name(struct got_tree_object *tree, const char *name, size_t len)
1843 int i;
1845 /* Note that tree entries are sorted in strncmp() order. */
1846 for (i = 0; i < tree->nentries; i++) {
1847 struct got_tree_entry *te = &tree->entries[i];
1848 int cmp = strncmp(te->name, name, len);
1849 if (cmp < 0)
1850 continue;
1851 if (cmp > 0)
1852 break;
1853 if (te->name[len] == '\0')
1854 return te;
1856 return NULL;
1859 struct got_tree_entry *
1860 got_object_tree_find_entry(struct got_tree_object *tree, const char *name)
1862 return find_entry_by_name(tree, name, strlen(name));
1865 const struct got_error *
1866 got_object_tree_find_path(struct got_object_id **id, mode_t *mode,
1867 struct got_repository *repo, struct got_tree_object *tree,
1868 const char *path)
1870 const struct got_error *err = NULL;
1871 struct got_tree_object *subtree = NULL;
1872 struct got_tree_entry *te = NULL;
1873 const char *seg, *s;
1874 size_t seglen;
1876 *id = NULL;
1878 s = path;
1879 while (s[0] == '/')
1880 s++;
1881 seg = s;
1882 seglen = 0;
1883 subtree = tree;
1884 while (*s) {
1885 struct got_tree_object *next_tree;
1887 if (*s != '/') {
1888 s++;
1889 seglen++;
1890 if (*s)
1891 continue;
1894 te = find_entry_by_name(subtree, seg, seglen);
1895 if (te == NULL) {
1896 err = got_error_path(path, GOT_ERR_NO_TREE_ENTRY);
1897 goto done;
1900 if (*s == '\0')
1901 break;
1903 seg = s + 1;
1904 seglen = 0;
1905 s++;
1906 if (*s) {
1907 err = got_object_open_as_tree(&next_tree, repo,
1908 &te->id);
1909 te = NULL;
1910 if (err)
1911 goto done;
1912 if (subtree != tree)
1913 got_object_tree_close(subtree);
1914 subtree = next_tree;
1918 if (te) {
1919 *id = got_object_id_dup(&te->id);
1920 if (*id == NULL)
1921 return got_error_from_errno("got_object_id_dup");
1922 if (mode)
1923 *mode = te->mode;
1924 } else
1925 err = got_error_path(path, GOT_ERR_NO_TREE_ENTRY);
1926 done:
1927 if (subtree && subtree != tree)
1928 got_object_tree_close(subtree);
1929 return err;
1931 const struct got_error *
1932 got_object_id_by_path(struct got_object_id **id, struct got_repository *repo,
1933 struct got_object_id *commit_id, const char *path)
1935 const struct got_error *err = NULL;
1936 struct got_commit_object *commit = NULL;
1937 struct got_tree_object *tree = NULL;
1939 *id = NULL;
1941 err = got_object_open_as_commit(&commit, repo, commit_id);
1942 if (err)
1943 goto done;
1945 /* Handle opening of root of commit's tree. */
1946 if (got_path_is_root_dir(path)) {
1947 *id = got_object_id_dup(commit->tree_id);
1948 if (*id == NULL)
1949 err = got_error_from_errno("got_object_id_dup");
1950 } else {
1951 err = got_object_open_as_tree(&tree, repo, commit->tree_id);
1952 if (err)
1953 goto done;
1954 err = got_object_tree_find_path(id, NULL, repo, tree, path);
1956 done:
1957 if (commit)
1958 got_object_commit_close(commit);
1959 if (tree)
1960 got_object_tree_close(tree);
1961 return err;
1965 * Normalize file mode bits to avoid false positive tree entry differences
1966 * in case tree entries have unexpected mode bits set.
1968 static mode_t
1969 normalize_mode_for_comparison(mode_t mode)
1972 * For directories, the only relevant bit is the IFDIR bit.
1973 * This allows us to detect paths changing from a directory
1974 * to a file and vice versa.
1976 if (S_ISDIR(mode))
1977 return mode & S_IFDIR;
1980 * For symlinks, the only relevant bit is the IFLNK bit.
1981 * This allows us to detect paths changing from a symlinks
1982 * to a file or directory and vice versa.
1984 if (S_ISLNK(mode))
1985 return mode & S_IFLNK;
1987 /* For files, the only change we care about is the executable bit. */
1988 return mode & S_IXUSR;
1991 const struct got_error *
1992 got_object_tree_path_changed(int *changed,
1993 struct got_tree_object *tree01, struct got_tree_object *tree02,
1994 const char *path, struct got_repository *repo)
1996 const struct got_error *err = NULL;
1997 struct got_tree_object *tree1 = NULL, *tree2 = NULL;
1998 struct got_tree_entry *te1 = NULL, *te2 = NULL;
1999 const char *seg, *s;
2000 size_t seglen;
2002 *changed = 0;
2004 /* We not do support comparing the root path. */
2005 if (got_path_is_root_dir(path))
2006 return got_error_path(path, GOT_ERR_BAD_PATH);
2008 tree1 = tree01;
2009 tree2 = tree02;
2010 s = path;
2011 while (*s == '/')
2012 s++;
2013 seg = s;
2014 seglen = 0;
2015 while (*s) {
2016 struct got_tree_object *next_tree1, *next_tree2;
2017 mode_t mode1, mode2;
2019 if (*s != '/') {
2020 s++;
2021 seglen++;
2022 if (*s)
2023 continue;
2026 te1 = find_entry_by_name(tree1, seg, seglen);
2027 if (te1 == NULL) {
2028 err = got_error(GOT_ERR_NO_OBJ);
2029 goto done;
2032 if (tree2)
2033 te2 = find_entry_by_name(tree2, seg, seglen);
2035 if (te2) {
2036 mode1 = normalize_mode_for_comparison(te1->mode);
2037 mode2 = normalize_mode_for_comparison(te2->mode);
2038 if (mode1 != mode2) {
2039 *changed = 1;
2040 goto done;
2043 if (got_object_id_cmp(&te1->id, &te2->id) == 0) {
2044 *changed = 0;
2045 goto done;
2049 if (*s == '\0') { /* final path element */
2050 *changed = 1;
2051 goto done;
2054 seg = s + 1;
2055 s++;
2056 seglen = 0;
2057 if (*s) {
2058 err = got_object_open_as_tree(&next_tree1, repo,
2059 &te1->id);
2060 te1 = NULL;
2061 if (err)
2062 goto done;
2063 if (tree1 != tree01)
2064 got_object_tree_close(tree1);
2065 tree1 = next_tree1;
2067 if (te2) {
2068 err = got_object_open_as_tree(&next_tree2, repo,
2069 &te2->id);
2070 te2 = NULL;
2071 if (err)
2072 goto done;
2073 if (tree2 != tree02)
2074 got_object_tree_close(tree2);
2075 tree2 = next_tree2;
2076 } else if (tree2) {
2077 if (tree2 != tree02)
2078 got_object_tree_close(tree2);
2079 tree2 = NULL;
2083 done:
2084 if (tree1 && tree1 != tree01)
2085 got_object_tree_close(tree1);
2086 if (tree2 && tree2 != tree02)
2087 got_object_tree_close(tree2);
2088 return err;
2091 const struct got_error *
2092 got_object_tree_entry_dup(struct got_tree_entry **new_te,
2093 struct got_tree_entry *te)
2095 const struct got_error *err = NULL;
2097 *new_te = calloc(1, sizeof(**new_te));
2098 if (*new_te == NULL)
2099 return got_error_from_errno("calloc");
2101 (*new_te)->mode = te->mode;
2102 memcpy((*new_te)->name, te->name, sizeof((*new_te)->name));
2103 memcpy(&(*new_te)->id, &te->id, sizeof((*new_te)->id));
2104 return err;
2107 int
2108 got_object_tree_entry_is_submodule(struct got_tree_entry *te)
2110 return (te->mode & S_IFMT) == (S_IFDIR | S_IFLNK);
2113 int
2114 got_object_tree_entry_is_symlink(struct got_tree_entry *te)
2116 /* S_IFDIR check avoids confusing symlinks with submodules. */
2117 return ((te->mode & (S_IFDIR | S_IFLNK)) == S_IFLNK);
2120 static const struct got_error *
2121 resolve_symlink(char **link_target, const char *path,
2122 struct got_object_id *commit_id, struct got_repository *repo)
2124 const struct got_error *err = NULL;
2125 char buf[PATH_MAX];
2126 char *name, *parent_path = NULL;
2127 struct got_object_id *tree_obj_id = NULL;
2128 struct got_tree_object *tree = NULL;
2129 struct got_tree_entry *te = NULL;
2131 *link_target = NULL;
2133 if (strlcpy(buf, path, sizeof(buf)) >= sizeof(buf))
2134 return got_error(GOT_ERR_NO_SPACE);
2136 name = basename(buf);
2137 if (name == NULL)
2138 return got_error_from_errno2("basename", path);
2140 err = got_path_dirname(&parent_path, path);
2141 if (err)
2142 return err;
2144 err = got_object_id_by_path(&tree_obj_id, repo, commit_id,
2145 parent_path);
2146 if (err) {
2147 if (err->code == GOT_ERR_NO_TREE_ENTRY) {
2148 /* Display the complete path in error message. */
2149 err = got_error_path(path, err->code);
2151 goto done;
2154 err = got_object_open_as_tree(&tree, repo, tree_obj_id);
2155 if (err)
2156 goto done;
2158 te = got_object_tree_find_entry(tree, name);
2159 if (te == NULL) {
2160 err = got_error_path(path, GOT_ERR_NO_TREE_ENTRY);
2161 goto done;
2164 if (got_object_tree_entry_is_symlink(te)) {
2165 err = got_tree_entry_get_symlink_target(link_target, te, repo);
2166 if (err)
2167 goto done;
2168 if (!got_path_is_absolute(*link_target)) {
2169 char *abspath;
2170 if (asprintf(&abspath, "%s/%s", parent_path,
2171 *link_target) == -1) {
2172 err = got_error_from_errno("asprintf");
2173 goto done;
2175 free(*link_target);
2176 *link_target = malloc(PATH_MAX);
2177 if (*link_target == NULL) {
2178 err = got_error_from_errno("malloc");
2179 goto done;
2181 err = got_canonpath(abspath, *link_target, PATH_MAX);
2182 free(abspath);
2183 if (err)
2184 goto done;
2187 done:
2188 free(tree_obj_id);
2189 if (tree)
2190 got_object_tree_close(tree);
2191 if (err) {
2192 free(*link_target);
2193 *link_target = NULL;
2195 return err;
2198 const struct got_error *
2199 got_object_resolve_symlinks(char **link_target, const char *path,
2200 struct got_object_id *commit_id, struct got_repository *repo)
2202 const struct got_error *err = NULL;
2203 char *next_target = NULL;
2204 int max_recursion = 40; /* matches Git */
2206 *link_target = NULL;
2208 do {
2209 err = resolve_symlink(&next_target,
2210 *link_target ? *link_target : path, commit_id, repo);
2211 if (err)
2212 break;
2213 if (next_target) {
2214 free(*link_target);
2215 if (--max_recursion == 0) {
2216 err = got_error_path(path, GOT_ERR_RECURSION);
2217 *link_target = NULL;
2218 break;
2220 *link_target = next_target;
2222 } while (next_target);
2224 return err;
2227 const struct got_error *
2228 got_traverse_packed_commits(struct got_object_id_queue *traversed_commits,
2229 struct got_object_id *commit_id, const char *path,
2230 struct got_repository *repo)
2232 const struct got_error *err = NULL;
2233 struct got_pack *pack = NULL;
2234 struct got_packidx *packidx = NULL;
2235 char *path_packfile = NULL;
2236 struct got_commit_object *changed_commit = NULL;
2237 struct got_object_id *changed_commit_id = NULL;
2238 int idx;
2240 err = got_repo_search_packidx(&packidx, &idx, repo, commit_id);
2241 if (err) {
2242 if (err->code != GOT_ERR_NO_OBJ)
2243 return err;
2244 return NULL;
2247 err = got_packidx_get_packfile_path(&path_packfile,
2248 packidx->path_packidx);
2249 if (err)
2250 return err;
2252 pack = got_repo_get_cached_pack(repo, path_packfile);
2253 if (pack == NULL) {
2254 err = got_repo_cache_pack(&pack, repo, path_packfile, packidx);
2255 if (err)
2256 goto done;
2259 if (pack->privsep_child == NULL) {
2260 err = start_pack_privsep_child(pack, packidx);
2261 if (err)
2262 goto done;
2265 err = got_privsep_send_commit_traversal_request(
2266 pack->privsep_child->ibuf, commit_id, idx, path);
2267 if (err)
2268 goto done;
2270 err = got_privsep_recv_traversed_commits(&changed_commit,
2271 &changed_commit_id, traversed_commits, pack->privsep_child->ibuf);
2272 if (err)
2273 goto done;
2275 if (changed_commit) {
2277 * Cache the commit in which the path was changed.
2278 * This commit might be opened again soon.
2280 changed_commit->refcnt++;
2281 err = got_repo_cache_commit(repo, changed_commit_id,
2282 changed_commit);
2283 got_object_commit_close(changed_commit);
2285 done:
2286 free(path_packfile);
2287 free(changed_commit_id);
2288 return err;