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>
24 #include <errno.h>
25 #include <fcntl.h>
26 #include <stdio.h>
27 #include <stdlib.h>
28 #include <string.h>
29 #include <stdint.h>
30 #include <unistd.h>
31 #include <zlib.h>
32 #include <ctype.h>
33 #include <libgen.h>
34 #include <limits.h>
35 #include <time.h>
37 #include "got_compat.h"
39 #include "got_error.h"
40 #include "got_object.h"
41 #include "got_repository.h"
42 #include "got_opentemp.h"
43 #include "got_path.h"
45 #include "got_lib_sha1.h"
46 #include "got_lib_delta.h"
47 #include "got_lib_inflate.h"
48 #include "got_lib_object.h"
49 #include "got_lib_privsep.h"
50 #include "got_lib_object_idcache.h"
51 #include "got_lib_object_cache.h"
52 #include "got_lib_object_parse.h"
53 #include "got_lib_pack.h"
54 #include "got_lib_repository.h"
56 #ifndef MIN
57 #define MIN(_a,_b) ((_a) < (_b) ? (_a) : (_b))
58 #endif
60 struct got_object_id *
61 got_object_get_id(struct got_object *obj)
62 {
63 return &obj->id;
64 }
66 const struct got_error *
67 got_object_get_id_str(char **outbuf, struct got_object *obj)
68 {
69 return got_object_id_str(outbuf, &obj->id);
70 }
72 const struct got_error *
73 got_object_get_type(int *type, struct got_repository *repo,
74 struct got_object_id *id)
75 {
76 const struct got_error *err = NULL;
77 struct got_object *obj;
79 err = got_object_open(&obj, repo, id);
80 if (err)
81 return err;
83 switch (obj->type) {
84 case GOT_OBJ_TYPE_COMMIT:
85 case GOT_OBJ_TYPE_TREE:
86 case GOT_OBJ_TYPE_BLOB:
87 case GOT_OBJ_TYPE_TAG:
88 *type = obj->type;
89 break;
90 default:
91 err = got_error(GOT_ERR_OBJ_TYPE);
92 break;
93 }
95 got_object_close(obj);
96 return err;
97 }
99 const struct got_error *
100 got_object_get_path(char **path, struct got_object_id *id,
101 struct got_repository *repo)
103 const struct got_error *err = NULL;
104 char *hex = NULL;
105 char *path_objects;
107 *path = NULL;
109 path_objects = got_repo_get_path_objects(repo);
110 if (path_objects == NULL)
111 return got_error_from_errno("got_repo_get_path_objects");
113 err = got_object_id_str(&hex, id);
114 if (err)
115 goto done;
117 if (asprintf(path, "%s/%.2x/%s", path_objects,
118 id->sha1[0], hex + 2) == -1)
119 err = got_error_from_errno("asprintf");
121 done:
122 free(hex);
123 free(path_objects);
124 return err;
127 const struct got_error *
128 got_object_open_loose_fd(int *fd, struct got_object_id *id,
129 struct got_repository *repo)
131 const struct got_error *err = NULL;
132 char *path;
134 err = got_object_get_path(&path, id, repo);
135 if (err)
136 return err;
137 *fd = open(path, O_RDONLY | O_NOFOLLOW);
138 if (*fd == -1) {
139 err = got_error_from_errno2("open", path);
140 goto done;
142 done:
143 free(path);
144 return err;
147 static const struct got_error *
148 request_packed_object(struct got_object **obj, struct got_pack *pack, int idx,
149 struct got_object_id *id)
151 const struct got_error *err = NULL;
152 struct imsgbuf *ibuf = pack->privsep_child->ibuf;
154 err = got_privsep_send_packed_obj_req(ibuf, idx, id);
155 if (err)
156 return err;
158 err = got_privsep_recv_obj(obj, ibuf);
159 if (err)
160 return err;
162 memcpy(&(*obj)->id, id, sizeof((*obj)->id));
164 return NULL;
167 static const struct got_error *
168 request_packed_object_raw(uint8_t **outbuf, off_t *size, size_t *hdrlen,
169 int outfd, struct got_pack *pack, int idx, struct got_object_id *id)
171 const struct got_error *err = NULL;
172 struct imsgbuf *ibuf = pack->privsep_child->ibuf;
173 int outfd_child;
174 int basefd, accumfd; /* temporary files for delta application */
176 basefd = got_opentempfd();
177 if (basefd == -1)
178 return got_error_from_errno("got_opentempfd");
180 accumfd = got_opentempfd();
181 if (accumfd == -1) {
182 close(basefd);
183 return got_error_from_errno("got_opentempfd");
186 outfd_child = dup(outfd);
187 if (outfd_child == -1) {
188 err = got_error_from_errno("dup");
189 close(basefd);
190 close(accumfd);
191 return err;
194 err = got_privsep_send_packed_raw_obj_req(ibuf, idx, id);
195 if (err) {
196 close(basefd);
197 close(accumfd);
198 close(outfd_child);
199 return err;
202 err = got_privsep_send_raw_obj_outfd(ibuf, outfd_child);
203 if (err) {
204 close(basefd);
205 close(accumfd);
206 return err;
210 err = got_privsep_send_tmpfd(pack->privsep_child->ibuf,
211 basefd);
212 if (err) {
213 close(accumfd);
214 return err;
217 err = got_privsep_send_tmpfd(pack->privsep_child->ibuf,
218 accumfd);
219 if (err)
220 return err;
222 err = got_privsep_recv_raw_obj(outbuf, size, hdrlen, ibuf);
223 if (err)
224 return err;
226 return NULL;
229 static void
230 set_max_datasize(void)
232 struct rlimit rl;
234 if (getrlimit(RLIMIT_DATA, &rl) != 0)
235 return;
237 rl.rlim_cur = rl.rlim_max;
238 setrlimit(RLIMIT_DATA, &rl);
241 static const struct got_error *
242 start_pack_privsep_child(struct got_pack *pack, struct got_packidx *packidx)
244 const struct got_error *err = NULL;
245 int imsg_fds[2];
246 pid_t pid;
247 struct imsgbuf *ibuf;
249 ibuf = calloc(1, sizeof(*ibuf));
250 if (ibuf == NULL)
251 return got_error_from_errno("calloc");
253 pack->privsep_child = calloc(1, sizeof(*pack->privsep_child));
254 if (pack->privsep_child == NULL) {
255 err = got_error_from_errno("calloc");
256 free(ibuf);
257 return err;
260 if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_fds) == -1) {
261 err = got_error_from_errno("socketpair");
262 goto done;
265 pid = fork();
266 if (pid == -1) {
267 err = got_error_from_errno("fork");
268 goto done;
269 } else if (pid == 0) {
270 set_max_datasize();
271 got_privsep_exec_child(imsg_fds, GOT_PATH_PROG_READ_PACK,
272 pack->path_packfile);
273 /* not reached */
276 if (close(imsg_fds[1]) == -1)
277 return got_error_from_errno("close");
278 pack->privsep_child->imsg_fd = imsg_fds[0];
279 pack->privsep_child->pid = pid;
280 imsg_init(ibuf, imsg_fds[0]);
281 pack->privsep_child->ibuf = ibuf;
283 err = got_privsep_init_pack_child(ibuf, pack, packidx);
284 if (err) {
285 const struct got_error *child_err;
286 err = got_privsep_send_stop(pack->privsep_child->imsg_fd);
287 child_err = got_privsep_wait_for_child(
288 pack->privsep_child->pid);
289 if (child_err && err == NULL)
290 err = child_err;
292 done:
293 if (err) {
294 free(ibuf);
295 free(pack->privsep_child);
296 pack->privsep_child = NULL;
298 return err;
301 static const struct got_error *
302 read_packed_object_privsep(struct got_object **obj,
303 struct got_repository *repo, struct got_pack *pack,
304 struct got_packidx *packidx, int idx, struct got_object_id *id)
306 const struct got_error *err = NULL;
308 if (pack->privsep_child == NULL) {
309 err = start_pack_privsep_child(pack, packidx);
310 if (err)
311 return err;
314 return request_packed_object(obj, pack, idx, id);
317 static const struct got_error *
318 read_packed_object_raw_privsep(uint8_t **outbuf, off_t *size, size_t *hdrlen,
319 int outfd, struct got_pack *pack, struct got_packidx *packidx, int idx,
320 struct got_object_id *id)
322 const struct got_error *err = NULL;
324 if (pack->privsep_child == NULL) {
325 err = start_pack_privsep_child(pack, packidx);
326 if (err)
327 return err;
330 return request_packed_object_raw(outbuf, size, hdrlen, outfd, pack,
331 idx, id);
334 const struct got_error *
335 got_object_open_packed(struct got_object **obj, struct got_object_id *id,
336 struct got_repository *repo)
338 const struct got_error *err = NULL;
339 struct got_pack *pack = NULL;
340 struct got_packidx *packidx = NULL;
341 int idx;
342 char *path_packfile;
344 err = got_repo_search_packidx(&packidx, &idx, repo, id);
345 if (err)
346 return err;
348 err = got_packidx_get_packfile_path(&path_packfile,
349 packidx->path_packidx);
350 if (err)
351 return err;
353 pack = got_repo_get_cached_pack(repo, path_packfile);
354 if (pack == NULL) {
355 err = got_repo_cache_pack(&pack, repo, path_packfile, packidx);
356 if (err)
357 goto done;
360 err = read_packed_object_privsep(obj, repo, pack, packidx, idx, id);
361 if (err)
362 goto done;
363 done:
364 free(path_packfile);
365 return err;
368 static const struct got_error *
369 request_object(struct got_object **obj, struct got_object_id *id,
370 struct got_repository *repo, int fd)
372 const struct got_error *err = NULL;
373 struct imsgbuf *ibuf;
375 ibuf = repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_OBJECT].ibuf;
377 err = got_privsep_send_obj_req(ibuf, fd, id);
378 if (err)
379 return err;
381 return got_privsep_recv_obj(obj, ibuf);
384 static const struct got_error *
385 request_raw_object(uint8_t **outbuf, off_t *size, size_t *hdrlen, int outfd,
386 struct got_object_id *id, struct got_repository *repo, int infd)
388 const struct got_error *err = NULL;
389 struct imsgbuf *ibuf;
390 int outfd_child;
392 ibuf = repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_OBJECT].ibuf;
394 outfd_child = dup(outfd);
395 if (outfd_child == -1)
396 return got_error_from_errno("dup");
398 err = got_privsep_send_raw_obj_req(ibuf, infd, id);
399 if (err)
400 return err;
402 err = got_privsep_send_raw_obj_outfd(ibuf, outfd_child);
403 if (err)
404 return err;
406 return got_privsep_recv_raw_obj(outbuf, size, hdrlen, ibuf);
409 static const struct got_error *
410 start_read_object_child(struct got_repository *repo)
412 const struct got_error *err = NULL;
413 int imsg_fds[2];
414 pid_t pid;
415 struct imsgbuf *ibuf;
417 ibuf = calloc(1, sizeof(*ibuf));
418 if (ibuf == NULL)
419 return got_error_from_errno("calloc");
421 if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_fds) == -1) {
422 err = got_error_from_errno("socketpair");
423 free(ibuf);
424 return err;
427 pid = fork();
428 if (pid == -1) {
429 err = got_error_from_errno("fork");
430 free(ibuf);
431 return err;
433 else if (pid == 0) {
434 got_privsep_exec_child(imsg_fds, GOT_PATH_PROG_READ_OBJECT,
435 repo->path);
436 /* not reached */
439 if (close(imsg_fds[1]) == -1) {
440 err = got_error_from_errno("close");
441 free(ibuf);
442 return err;
445 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_OBJECT].imsg_fd =
446 imsg_fds[0];
447 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_OBJECT].pid = pid;
448 imsg_init(ibuf, imsg_fds[0]);
449 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_OBJECT].ibuf = ibuf;
451 return NULL;
454 const struct got_error *
455 got_object_read_header_privsep(struct got_object **obj,
456 struct got_object_id *id, struct got_repository *repo, int obj_fd)
458 const struct got_error *err;
460 if (repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_OBJECT].imsg_fd != -1)
461 return request_object(obj, id, repo, obj_fd);
463 err = start_read_object_child(repo);
464 if (err) {
465 close(obj_fd);
466 return err;
469 return request_object(obj, id, repo, obj_fd);
472 static const struct got_error *
473 read_object_raw_privsep(uint8_t **outbuf, off_t *size, size_t *hdrlen,
474 int outfd, struct got_object_id *id, struct got_repository *repo,
475 int obj_fd)
477 const struct got_error *err;
479 if (repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_OBJECT].imsg_fd != -1)
480 return request_raw_object(outbuf, size, hdrlen, outfd, id,
481 repo, obj_fd);
483 err = start_read_object_child(repo);
484 if (err)
485 return err;
487 return request_raw_object(outbuf, size, hdrlen, outfd, id, repo,
488 obj_fd);
491 const struct got_error *
492 got_object_open(struct got_object **obj, struct got_repository *repo,
493 struct got_object_id *id)
495 const struct got_error *err = NULL;
496 int fd;
498 *obj = got_repo_get_cached_object(repo, id);
499 if (*obj != NULL) {
500 (*obj)->refcnt++;
501 return NULL;
504 err = got_object_open_packed(obj, id, repo);
505 if (err && err->code != GOT_ERR_NO_OBJ)
506 return err;
507 if (*obj) {
508 (*obj)->refcnt++;
509 return got_repo_cache_object(repo, id, *obj);
512 err = got_object_open_loose_fd(&fd, id, repo);
513 if (err) {
514 if (err->code == GOT_ERR_ERRNO && errno == ENOENT)
515 err = got_error_no_obj(id);
516 return err;
519 err = got_object_read_header_privsep(obj, id, repo, fd);
520 if (err)
521 return err;
523 memcpy((*obj)->id.sha1, id->sha1, SHA1_DIGEST_LENGTH);
525 (*obj)->refcnt++;
526 return got_repo_cache_object(repo, id, *obj);
529 /* *outfd must be initialized to -1 by caller */
530 const struct got_error *
531 got_object_raw_open(struct got_raw_object **obj, int *outfd,
532 struct got_repository *repo, struct got_object_id *id)
534 const struct got_error *err = NULL;
535 struct got_packidx *packidx = NULL;
536 int idx;
537 uint8_t *outbuf = NULL;
538 off_t size = 0;
539 size_t hdrlen = 0;
540 char *path_packfile = NULL;
542 *obj = got_repo_get_cached_raw_object(repo, id);
543 if (*obj != NULL) {
544 (*obj)->refcnt++;
545 return NULL;
548 if (*outfd == -1) {
549 *outfd = got_opentempfd();
550 if (*outfd == -1)
551 return got_error_from_errno("got_opentempfd");
554 err = got_repo_search_packidx(&packidx, &idx, repo, id);
555 if (err == NULL) {
556 struct got_pack *pack = NULL;
558 err = got_packidx_get_packfile_path(&path_packfile,
559 packidx->path_packidx);
560 if (err)
561 goto done;
563 pack = got_repo_get_cached_pack(repo, path_packfile);
564 if (pack == NULL) {
565 err = got_repo_cache_pack(&pack, repo, path_packfile,
566 packidx);
567 if (err)
568 goto done;
570 err = read_packed_object_raw_privsep(&outbuf, &size, &hdrlen,
571 *outfd, pack, packidx, idx, id);
572 if (err)
573 goto done;
574 } else if (err->code == GOT_ERR_NO_OBJ) {
575 int fd;
577 err = got_object_open_loose_fd(&fd, id, repo);
578 if (err)
579 goto done;
580 err = read_object_raw_privsep(&outbuf, &size, &hdrlen, *outfd,
581 id, repo, fd);
582 if (err)
583 goto done;
586 *obj = calloc(1, sizeof(**obj));
587 if (*obj == NULL) {
588 err = got_error_from_errno("calloc");
589 goto done;
592 if (outbuf) {
593 (*obj)->f = fmemopen(outbuf, hdrlen + size, "r");
594 if ((*obj)->f == NULL) {
595 err = got_error_from_errno("fdopen");
596 goto done;
598 (*obj)->data = outbuf;
599 } else {
600 struct stat sb;
601 if (fstat(*outfd, &sb) == -1) {
602 err = got_error_from_errno("fstat");
603 goto done;
606 if (sb.st_size != hdrlen + size) {
607 err = got_error(GOT_ERR_PRIVSEP_LEN);
608 goto done;
611 (*obj)->f = fdopen(*outfd, "r");
612 if ((*obj)->f == NULL) {
613 err = got_error_from_errno("fdopen");
614 goto done;
616 (*obj)->data = NULL;
617 *outfd = -1;
619 (*obj)->hdrlen = hdrlen;
620 (*obj)->size = size;
621 err = got_repo_cache_raw_object(repo, id, *obj);
622 done:
623 free(path_packfile);
624 if (err) {
625 if (*obj) {
626 got_object_raw_close(*obj);
627 *obj = NULL;
629 free(outbuf);
630 } else
631 (*obj)->refcnt++;
632 return err;
635 const struct got_error *
636 got_object_open_by_id_str(struct got_object **obj, struct got_repository *repo,
637 const char *id_str)
639 struct got_object_id id;
641 if (!got_parse_sha1_digest(id.sha1, id_str))
642 return got_error_path(id_str, GOT_ERR_BAD_OBJ_ID_STR);
644 return got_object_open(obj, repo, &id);
647 const struct got_error *
648 got_object_resolve_id_str(struct got_object_id **id,
649 struct got_repository *repo, const char *id_str)
651 const struct got_error *err = NULL;
652 struct got_object *obj;
654 err = got_object_open_by_id_str(&obj, repo, id_str);
655 if (err)
656 return err;
658 *id = got_object_id_dup(got_object_get_id(obj));
659 got_object_close(obj);
660 if (*id == NULL)
661 return got_error_from_errno("got_object_id_dup");
663 return NULL;
666 static const struct got_error *
667 request_packed_commit(struct got_commit_object **commit, struct got_pack *pack,
668 int pack_idx, struct got_object_id *id)
670 const struct got_error *err = NULL;
672 err = got_privsep_send_commit_req(pack->privsep_child->ibuf, -1, id,
673 pack_idx);
674 if (err)
675 return err;
677 err = got_privsep_recv_commit(commit, pack->privsep_child->ibuf);
678 if (err)
679 return err;
681 (*commit)->flags |= GOT_COMMIT_FLAG_PACKED;
682 return NULL;
685 static const struct got_error *
686 read_packed_commit_privsep(struct got_commit_object **commit,
687 struct got_pack *pack, struct got_packidx *packidx, int idx,
688 struct got_object_id *id)
690 const struct got_error *err = NULL;
692 if (pack->privsep_child)
693 return request_packed_commit(commit, pack, idx, id);
695 err = start_pack_privsep_child(pack, packidx);
696 if (err)
697 return err;
699 return request_packed_commit(commit, pack, idx, id);
702 static const struct got_error *
703 request_commit(struct got_commit_object **commit, struct got_repository *repo,
704 int fd, struct got_object_id *id)
706 const struct got_error *err = NULL;
707 struct imsgbuf *ibuf;
709 ibuf = repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_COMMIT].ibuf;
711 err = got_privsep_send_commit_req(ibuf, fd, id, -1);
712 if (err)
713 return err;
715 return got_privsep_recv_commit(commit, ibuf);
718 static const struct got_error *
719 read_commit_privsep(struct got_commit_object **commit, int obj_fd,
720 struct got_object_id *id, struct got_repository *repo)
722 const struct got_error *err;
723 int imsg_fds[2];
724 pid_t pid;
725 struct imsgbuf *ibuf;
727 if (repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_COMMIT].imsg_fd != -1)
728 return request_commit(commit, repo, obj_fd, id);
730 ibuf = calloc(1, sizeof(*ibuf));
731 if (ibuf == NULL)
732 return got_error_from_errno("calloc");
734 if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_fds) == -1) {
735 err = got_error_from_errno("socketpair");
736 free(ibuf);
737 return err;
740 pid = fork();
741 if (pid == -1) {
742 err = got_error_from_errno("fork");
743 free(ibuf);
744 return err;
746 else if (pid == 0) {
747 got_privsep_exec_child(imsg_fds, GOT_PATH_PROG_READ_COMMIT,
748 repo->path);
749 /* not reached */
752 if (close(imsg_fds[1]) == -1) {
753 err = got_error_from_errno("close");
754 free(ibuf);
755 return err;
757 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_COMMIT].imsg_fd =
758 imsg_fds[0];
759 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_COMMIT].pid = pid;
760 imsg_init(ibuf, imsg_fds[0]);
761 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_COMMIT].ibuf = ibuf;
763 return request_commit(commit, repo, obj_fd, id);
767 static const struct got_error *
768 open_commit(struct got_commit_object **commit,
769 struct got_repository *repo, struct got_object_id *id, int check_cache)
771 const struct got_error *err = NULL;
772 struct got_packidx *packidx = NULL;
773 int idx;
774 char *path_packfile = NULL;
776 if (check_cache) {
777 *commit = got_repo_get_cached_commit(repo, id);
778 if (*commit != NULL) {
779 (*commit)->refcnt++;
780 return NULL;
782 } else
783 *commit = NULL;
785 err = got_repo_search_packidx(&packidx, &idx, repo, id);
786 if (err == NULL) {
787 struct got_pack *pack = NULL;
789 err = got_packidx_get_packfile_path(&path_packfile,
790 packidx->path_packidx);
791 if (err)
792 return err;
794 pack = got_repo_get_cached_pack(repo, path_packfile);
795 if (pack == NULL) {
796 err = got_repo_cache_pack(&pack, repo, path_packfile,
797 packidx);
798 if (err)
799 goto done;
801 err = read_packed_commit_privsep(commit, pack,
802 packidx, idx, id);
803 } else if (err->code == GOT_ERR_NO_OBJ) {
804 int fd;
806 err = got_object_open_loose_fd(&fd, id, repo);
807 if (err)
808 return err;
809 err = read_commit_privsep(commit, fd, id, repo);
812 if (err == NULL) {
813 (*commit)->refcnt++;
814 err = got_repo_cache_commit(repo, id, *commit);
816 done:
817 free(path_packfile);
818 return err;
821 const struct got_error *
822 got_object_open_as_commit(struct got_commit_object **commit,
823 struct got_repository *repo, struct got_object_id *id)
825 *commit = got_repo_get_cached_commit(repo, id);
826 if (*commit != NULL) {
827 (*commit)->refcnt++;
828 return NULL;
831 return open_commit(commit, repo, id, 0);
834 const struct got_error *
835 got_object_commit_open(struct got_commit_object **commit,
836 struct got_repository *repo, struct got_object *obj)
838 return open_commit(commit, repo, got_object_get_id(obj), 1);
841 const struct got_error *
842 got_object_qid_alloc(struct got_object_qid **qid, struct got_object_id *id)
844 const struct got_error *err = NULL;
846 *qid = calloc(1, sizeof(**qid));
847 if (*qid == NULL)
848 return got_error_from_errno("calloc");
850 (*qid)->id = got_object_id_dup(id);
851 if ((*qid)->id == NULL) {
852 err = got_error_from_errno("got_object_id_dup");
853 got_object_qid_free(*qid);
854 *qid = NULL;
855 return err;
858 return NULL;
861 const struct got_error *
862 got_object_id_queue_copy(const struct got_object_id_queue *src,
863 struct got_object_id_queue *dest)
865 const struct got_error *err;
866 struct got_object_qid *qid;
868 STAILQ_FOREACH(qid, src, entry) {
869 struct got_object_qid *new;
870 /*
871 * Deep-copy the object ID only. Let the caller deal
872 * with setting up the new->data pointer if needed.
873 */
874 err = got_object_qid_alloc(&new, qid->id);
875 if (err) {
876 got_object_id_queue_free(dest);
877 return err;
879 STAILQ_INSERT_TAIL(dest, new, entry);
882 return NULL;
885 static const struct got_error *
886 request_packed_tree(struct got_tree_object **tree, struct got_pack *pack,
887 int pack_idx, struct got_object_id *id)
889 const struct got_error *err = NULL;
891 err = got_privsep_send_tree_req(pack->privsep_child->ibuf, -1, id,
892 pack_idx);
893 if (err)
894 return err;
896 return got_privsep_recv_tree(tree, pack->privsep_child->ibuf);
899 static const struct got_error *
900 read_packed_tree_privsep(struct got_tree_object **tree,
901 struct got_pack *pack, struct got_packidx *packidx, int idx,
902 struct got_object_id *id)
904 const struct got_error *err = NULL;
906 if (pack->privsep_child)
907 return request_packed_tree(tree, pack, idx, id);
909 err = start_pack_privsep_child(pack, packidx);
910 if (err)
911 return err;
913 return request_packed_tree(tree, pack, idx, id);
916 static const struct got_error *
917 request_tree(struct got_tree_object **tree, struct got_repository *repo,
918 int fd, struct got_object_id *id)
920 const struct got_error *err = NULL;
921 struct imsgbuf *ibuf;
923 ibuf = repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TREE].ibuf;
925 err = got_privsep_send_tree_req(ibuf, fd, id, -1);
926 if (err)
927 return err;
929 return got_privsep_recv_tree(tree, ibuf);
932 const struct got_error *
933 read_tree_privsep(struct got_tree_object **tree, int obj_fd,
934 struct got_object_id *id, struct got_repository *repo)
936 const struct got_error *err;
937 int imsg_fds[2];
938 pid_t pid;
939 struct imsgbuf *ibuf;
941 if (repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TREE].imsg_fd != -1)
942 return request_tree(tree, repo, obj_fd, id);
944 ibuf = calloc(1, sizeof(*ibuf));
945 if (ibuf == NULL)
946 return got_error_from_errno("calloc");
948 if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_fds) == -1) {
949 err = got_error_from_errno("socketpair");
950 free(ibuf);
951 return err;
954 pid = fork();
955 if (pid == -1) {
956 err = got_error_from_errno("fork");
957 free(ibuf);
958 return err;
960 else if (pid == 0) {
961 got_privsep_exec_child(imsg_fds, GOT_PATH_PROG_READ_TREE,
962 repo->path);
963 /* not reached */
966 if (close(imsg_fds[1]) == -1) {
967 err = got_error_from_errno("close");
968 free(ibuf);
969 return err;
971 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TREE].imsg_fd =
972 imsg_fds[0];
973 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TREE].pid = pid;
974 imsg_init(ibuf, imsg_fds[0]);
975 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TREE].ibuf = ibuf;
978 return request_tree(tree, repo, obj_fd, id);
981 static const struct got_error *
982 open_tree(struct got_tree_object **tree, struct got_repository *repo,
983 struct got_object_id *id, int check_cache)
985 const struct got_error *err = NULL;
986 struct got_packidx *packidx = NULL;
987 int idx;
988 char *path_packfile = NULL;
990 if (check_cache) {
991 *tree = got_repo_get_cached_tree(repo, id);
992 if (*tree != NULL) {
993 (*tree)->refcnt++;
994 return NULL;
996 } else
997 *tree = NULL;
999 err = got_repo_search_packidx(&packidx, &idx, repo, id);
1000 if (err == NULL) {
1001 struct got_pack *pack = NULL;
1003 err = got_packidx_get_packfile_path(&path_packfile,
1004 packidx->path_packidx);
1005 if (err)
1006 return err;
1008 pack = got_repo_get_cached_pack(repo, path_packfile);
1009 if (pack == NULL) {
1010 err = got_repo_cache_pack(&pack, repo, path_packfile,
1011 packidx);
1012 if (err)
1013 goto done;
1015 err = read_packed_tree_privsep(tree, pack,
1016 packidx, idx, id);
1017 } else if (err->code == GOT_ERR_NO_OBJ) {
1018 int fd;
1020 err = got_object_open_loose_fd(&fd, id, repo);
1021 if (err)
1022 return err;
1023 err = read_tree_privsep(tree, fd, id, repo);
1026 if (err == NULL) {
1027 (*tree)->refcnt++;
1028 err = got_repo_cache_tree(repo, id, *tree);
1030 done:
1031 free(path_packfile);
1032 return err;
1035 const struct got_error *
1036 got_object_open_as_tree(struct got_tree_object **tree,
1037 struct got_repository *repo, struct got_object_id *id)
1039 *tree = got_repo_get_cached_tree(repo, id);
1040 if (*tree != NULL) {
1041 (*tree)->refcnt++;
1042 return NULL;
1045 return open_tree(tree, repo, id, 0);
1048 const struct got_error *
1049 got_object_tree_open(struct got_tree_object **tree,
1050 struct got_repository *repo, struct got_object *obj)
1052 return open_tree(tree, repo, got_object_get_id(obj), 1);
1055 int
1056 got_object_tree_get_nentries(struct got_tree_object *tree)
1058 return tree->nentries;
1061 struct got_tree_entry *
1062 got_object_tree_get_first_entry(struct got_tree_object *tree)
1064 return got_object_tree_get_entry(tree, 0);
1067 struct got_tree_entry *
1068 got_object_tree_get_last_entry(struct got_tree_object *tree)
1070 return got_object_tree_get_entry(tree, tree->nentries - 1);
1073 struct got_tree_entry *
1074 got_object_tree_get_entry(struct got_tree_object *tree, int i)
1076 if (i < 0 || i >= tree->nentries)
1077 return NULL;
1078 return &tree->entries[i];
1081 mode_t
1082 got_tree_entry_get_mode(struct got_tree_entry *te)
1084 return te->mode;
1087 const char *
1088 got_tree_entry_get_name(struct got_tree_entry *te)
1090 return &te->name[0];
1093 struct got_object_id *
1094 got_tree_entry_get_id(struct got_tree_entry *te)
1096 return &te->id;
1099 const struct got_error *
1100 got_object_blob_read_to_str(char **s, struct got_blob_object *blob)
1102 const struct got_error *err = NULL;
1103 size_t len, totlen, hdrlen, offset;
1105 *s = NULL;
1107 hdrlen = got_object_blob_get_hdrlen(blob);
1108 totlen = 0;
1109 offset = 0;
1110 do {
1111 char *p;
1113 err = got_object_blob_read_block(&len, blob);
1114 if (err)
1115 return err;
1117 if (len == 0)
1118 break;
1120 totlen += len - hdrlen;
1121 p = realloc(*s, totlen + 1);
1122 if (p == NULL) {
1123 err = got_error_from_errno("realloc");
1124 free(*s);
1125 *s = NULL;
1126 return err;
1128 *s = p;
1129 /* Skip blob object header first time around. */
1130 memcpy(*s + offset,
1131 got_object_blob_get_read_buf(blob) + hdrlen, len - hdrlen);
1132 hdrlen = 0;
1133 offset = totlen;
1134 } while (len > 0);
1136 (*s)[totlen] = '\0';
1137 return NULL;
1140 const struct got_error *
1141 got_tree_entry_get_symlink_target(char **link_target, struct got_tree_entry *te,
1142 struct got_repository *repo)
1144 const struct got_error *err = NULL;
1145 struct got_blob_object *blob = NULL;
1147 *link_target = NULL;
1149 if (!got_object_tree_entry_is_symlink(te))
1150 return got_error(GOT_ERR_TREE_ENTRY_TYPE);
1152 err = got_object_open_as_blob(&blob, repo,
1153 got_tree_entry_get_id(te), PATH_MAX);
1154 if (err)
1155 return err;
1157 err = got_object_blob_read_to_str(link_target, blob);
1158 got_object_blob_close(blob);
1159 if (err) {
1160 free(*link_target);
1161 *link_target = NULL;
1163 return err;
1166 int
1167 got_tree_entry_get_index(struct got_tree_entry *te)
1169 return te->idx;
1172 struct got_tree_entry *
1173 got_tree_entry_get_next(struct got_tree_object *tree,
1174 struct got_tree_entry *te)
1176 return got_object_tree_get_entry(tree, te->idx + 1);
1179 struct got_tree_entry *
1180 got_tree_entry_get_prev(struct got_tree_object *tree,
1181 struct got_tree_entry *te)
1183 return got_object_tree_get_entry(tree, te->idx - 1);
1186 static const struct got_error *
1187 request_packed_blob(uint8_t **outbuf, size_t *size, size_t *hdrlen, int outfd,
1188 struct got_pack *pack, struct got_packidx *packidx, int idx,
1189 struct got_object_id *id)
1191 const struct got_error *err = NULL;
1192 int outfd_child;
1193 int basefd, accumfd; /* temporary files for delta application */
1195 basefd = got_opentempfd();
1196 if (basefd == -1)
1197 return got_error_from_errno("got_opentempfd");
1198 accumfd = got_opentempfd();
1199 if (accumfd == -1)
1200 return got_error_from_errno("got_opentempfd");
1202 outfd_child = dup(outfd);
1203 if (outfd_child == -1)
1204 return got_error_from_errno("dup");
1206 err = got_privsep_send_blob_req(pack->privsep_child->ibuf, -1, id, idx);
1207 if (err)
1208 return err;
1210 err = got_privsep_send_blob_outfd(pack->privsep_child->ibuf,
1211 outfd_child);
1212 if (err) {
1213 close(basefd);
1214 close(accumfd);
1215 return err;
1218 err = got_privsep_send_tmpfd(pack->privsep_child->ibuf,
1219 basefd);
1220 if (err) {
1221 close(accumfd);
1222 return err;
1225 err = got_privsep_send_tmpfd(pack->privsep_child->ibuf,
1226 accumfd);
1227 if (err)
1228 return err;
1230 err = got_privsep_recv_blob(outbuf, size, hdrlen,
1231 pack->privsep_child->ibuf);
1232 if (err)
1233 return err;
1235 if (lseek(outfd, SEEK_SET, 0) == -1)
1236 err = got_error_from_errno("lseek");
1238 return err;
1241 static const struct got_error *
1242 read_packed_blob_privsep(uint8_t **outbuf, size_t *size, size_t *hdrlen,
1243 int outfd, struct got_pack *pack, struct got_packidx *packidx, int idx,
1244 struct got_object_id *id)
1246 const struct got_error *err = NULL;
1248 if (pack->privsep_child == NULL) {
1249 err = start_pack_privsep_child(pack, packidx);
1250 if (err)
1251 return err;
1254 return request_packed_blob(outbuf, size, hdrlen, outfd, pack, packidx,
1255 idx, id);
1258 static const struct got_error *
1259 request_blob(uint8_t **outbuf, size_t *size, size_t *hdrlen, int outfd,
1260 int infd, struct got_object_id *id, struct imsgbuf *ibuf)
1262 const struct got_error *err = NULL;
1263 int outfd_child;
1265 outfd_child = dup(outfd);
1266 if (outfd_child == -1)
1267 return got_error_from_errno("dup");
1269 err = got_privsep_send_blob_req(ibuf, infd, id, -1);
1270 if (err)
1271 return err;
1273 err = got_privsep_send_blob_outfd(ibuf, outfd_child);
1274 if (err)
1275 return err;
1277 err = got_privsep_recv_blob(outbuf, size, hdrlen, ibuf);
1278 if (err)
1279 return err;
1281 if (lseek(outfd, SEEK_SET, 0) == -1)
1282 return got_error_from_errno("lseek");
1284 return err;
1287 static const struct got_error *
1288 read_blob_privsep(uint8_t **outbuf, size_t *size, size_t *hdrlen,
1289 int outfd, int infd, struct got_object_id *id, struct got_repository *repo)
1291 const struct got_error *err;
1292 int imsg_fds[2];
1293 pid_t pid;
1294 struct imsgbuf *ibuf;
1296 if (repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_BLOB].imsg_fd != -1) {
1297 ibuf = repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_BLOB].ibuf;
1298 return request_blob(outbuf, size, hdrlen, outfd, infd, id,
1299 ibuf);
1302 ibuf = calloc(1, sizeof(*ibuf));
1303 if (ibuf == NULL)
1304 return got_error_from_errno("calloc");
1306 if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_fds) == -1) {
1307 err = got_error_from_errno("socketpair");
1308 free(ibuf);
1309 return err;
1312 pid = fork();
1313 if (pid == -1) {
1314 err = got_error_from_errno("fork");
1315 free(ibuf);
1316 return err;
1318 else if (pid == 0) {
1319 got_privsep_exec_child(imsg_fds, GOT_PATH_PROG_READ_BLOB,
1320 repo->path);
1321 /* not reached */
1324 if (close(imsg_fds[1]) == -1) {
1325 err = got_error_from_errno("close");
1326 free(ibuf);
1327 return err;
1329 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_BLOB].imsg_fd =
1330 imsg_fds[0];
1331 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_BLOB].pid = pid;
1332 imsg_init(ibuf, imsg_fds[0]);
1333 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_BLOB].ibuf = ibuf;
1335 return request_blob(outbuf, size, hdrlen, outfd, infd, id, ibuf);
1338 static const struct got_error *
1339 open_blob(struct got_blob_object **blob, struct got_repository *repo,
1340 struct got_object_id *id, size_t blocksize)
1342 const struct got_error *err = NULL;
1343 struct got_packidx *packidx = NULL;
1344 int idx;
1345 char *path_packfile = NULL;
1346 uint8_t *outbuf;
1347 int outfd;
1348 size_t size, hdrlen;
1349 struct stat sb;
1351 *blob = calloc(1, sizeof(**blob));
1352 if (*blob == NULL)
1353 return got_error_from_errno("calloc");
1355 outfd = got_opentempfd();
1356 if (outfd == -1)
1357 return got_error_from_errno("got_opentempfd");
1359 (*blob)->read_buf = malloc(blocksize);
1360 if ((*blob)->read_buf == NULL) {
1361 err = got_error_from_errno("malloc");
1362 goto done;
1365 err = got_repo_search_packidx(&packidx, &idx, repo, id);
1366 if (err == NULL) {
1367 struct got_pack *pack = NULL;
1369 err = got_packidx_get_packfile_path(&path_packfile,
1370 packidx->path_packidx);
1371 if (err)
1372 goto done;
1374 pack = got_repo_get_cached_pack(repo, path_packfile);
1375 if (pack == NULL) {
1376 err = got_repo_cache_pack(&pack, repo, path_packfile,
1377 packidx);
1378 if (err)
1379 goto done;
1381 err = read_packed_blob_privsep(&outbuf, &size, &hdrlen, outfd,
1382 pack, packidx, idx, id);
1383 } else if (err->code == GOT_ERR_NO_OBJ) {
1384 int infd;
1386 err = got_object_open_loose_fd(&infd, id, repo);
1387 if (err)
1388 goto done;
1389 err = read_blob_privsep(&outbuf, &size, &hdrlen, outfd, infd,
1390 id, repo);
1392 if (err)
1393 goto done;
1395 if (hdrlen > size) {
1396 err = got_error(GOT_ERR_BAD_OBJ_HDR);
1397 goto done;
1400 if (outbuf) {
1401 if (close(outfd) == -1 && err == NULL)
1402 err = got_error_from_errno("close");
1403 outfd = -1;
1404 (*blob)->f = fmemopen(outbuf, size, "rb");
1405 if ((*blob)->f == NULL) {
1406 err = got_error_from_errno("fmemopen");
1407 free(outbuf);
1408 goto done;
1410 (*blob)->data = outbuf;
1411 } else {
1412 if (fstat(outfd, &sb) == -1) {
1413 err = got_error_from_errno("fstat");
1414 goto done;
1417 if (sb.st_size != size) {
1418 err = got_error(GOT_ERR_PRIVSEP_LEN);
1419 goto done;
1422 (*blob)->f = fdopen(outfd, "rb");
1423 if ((*blob)->f == NULL) {
1424 err = got_error_from_errno("fdopen");
1425 close(outfd);
1426 outfd = -1;
1427 goto done;
1431 (*blob)->hdrlen = hdrlen;
1432 (*blob)->blocksize = blocksize;
1433 memcpy(&(*blob)->id.sha1, id->sha1, SHA1_DIGEST_LENGTH);
1435 done:
1436 free(path_packfile);
1437 if (err) {
1438 if (*blob) {
1439 got_object_blob_close(*blob);
1440 *blob = NULL;
1441 } else if (outfd != -1)
1442 close(outfd);
1444 return err;
1447 const struct got_error *
1448 got_object_open_as_blob(struct got_blob_object **blob,
1449 struct got_repository *repo, struct got_object_id *id,
1450 size_t blocksize)
1452 return open_blob(blob, repo, id, blocksize);
1455 const struct got_error *
1456 got_object_blob_open(struct got_blob_object **blob,
1457 struct got_repository *repo, struct got_object *obj, size_t blocksize)
1459 return open_blob(blob, repo, got_object_get_id(obj), blocksize);
1462 const struct got_error *
1463 got_object_blob_close(struct got_blob_object *blob)
1465 const struct got_error *err = NULL;
1466 free(blob->read_buf);
1467 if (blob->f && fclose(blob->f) == EOF)
1468 err = got_error_from_errno("fclose");
1469 free(blob->data);
1470 free(blob);
1471 return err;
1474 void
1475 got_object_blob_rewind(struct got_blob_object *blob)
1477 if (blob->f)
1478 rewind(blob->f);
1481 char *
1482 got_object_blob_id_str(struct got_blob_object *blob, char *buf, size_t size)
1484 return got_sha1_digest_to_str(blob->id.sha1, buf, size);
1487 size_t
1488 got_object_blob_get_hdrlen(struct got_blob_object *blob)
1490 return blob->hdrlen;
1493 const uint8_t *
1494 got_object_blob_get_read_buf(struct got_blob_object *blob)
1496 return blob->read_buf;
1499 const struct got_error *
1500 got_object_blob_read_block(size_t *outlenp, struct got_blob_object *blob)
1502 size_t n;
1504 n = fread(blob->read_buf, 1, blob->blocksize, blob->f);
1505 if (n == 0 && ferror(blob->f))
1506 return got_ferror(blob->f, GOT_ERR_IO);
1507 *outlenp = n;
1508 return NULL;
1511 const struct got_error *
1512 got_object_blob_dump_to_file(off_t *filesize, int *nlines,
1513 off_t **line_offsets, FILE *outfile, struct got_blob_object *blob)
1515 const struct got_error *err = NULL;
1516 size_t n, len, hdrlen;
1517 const uint8_t *buf;
1518 int i;
1519 const int alloc_chunksz = 512;
1520 size_t nalloc = 0;
1521 off_t off = 0, total_len = 0;
1523 if (line_offsets)
1524 *line_offsets = NULL;
1525 if (filesize)
1526 *filesize = 0;
1527 if (nlines)
1528 *nlines = 0;
1530 hdrlen = got_object_blob_get_hdrlen(blob);
1531 do {
1532 err = got_object_blob_read_block(&len, blob);
1533 if (err)
1534 return err;
1535 if (len == 0)
1536 break;
1537 buf = got_object_blob_get_read_buf(blob);
1538 i = hdrlen;
1539 if (nlines) {
1540 if (line_offsets && *line_offsets == NULL) {
1541 /* Have some data but perhaps no '\n'. */
1542 *nlines = 1;
1543 nalloc = alloc_chunksz;
1544 *line_offsets = calloc(nalloc,
1545 sizeof(**line_offsets));
1546 if (*line_offsets == NULL)
1547 return got_error_from_errno("calloc");
1549 /* Skip forward over end of first line. */
1550 while (i < len) {
1551 if (buf[i] == '\n')
1552 break;
1553 i++;
1556 /* Scan '\n' offsets in remaining chunk of data. */
1557 while (i < len) {
1558 if (buf[i] != '\n') {
1559 i++;
1560 continue;
1562 (*nlines)++;
1563 if (line_offsets && nalloc < *nlines) {
1564 size_t n = *nlines + alloc_chunksz;
1565 off_t *o = recallocarray(*line_offsets,
1566 nalloc, n, sizeof(**line_offsets));
1567 if (o == NULL) {
1568 free(*line_offsets);
1569 *line_offsets = NULL;
1570 return got_error_from_errno(
1571 "recallocarray");
1573 *line_offsets = o;
1574 nalloc = n;
1576 if (line_offsets) {
1577 off = total_len + i - hdrlen + 1;
1578 (*line_offsets)[*nlines - 1] = off;
1580 i++;
1583 /* Skip blob object header first time around. */
1584 n = fwrite(buf + hdrlen, 1, len - hdrlen, outfile);
1585 if (n != len - hdrlen)
1586 return got_ferror(outfile, GOT_ERR_IO);
1587 total_len += len - hdrlen;
1588 hdrlen = 0;
1589 } while (len != 0);
1591 if (fflush(outfile) != 0)
1592 return got_error_from_errno("fflush");
1593 rewind(outfile);
1595 if (filesize)
1596 *filesize = total_len;
1598 return NULL;
1601 static const struct got_error *
1602 request_packed_tag(struct got_tag_object **tag, struct got_pack *pack,
1603 int pack_idx, struct got_object_id *id)
1605 const struct got_error *err = NULL;
1607 err = got_privsep_send_tag_req(pack->privsep_child->ibuf, -1, id,
1608 pack_idx);
1609 if (err)
1610 return err;
1612 return got_privsep_recv_tag(tag, pack->privsep_child->ibuf);
1615 static const struct got_error *
1616 read_packed_tag_privsep(struct got_tag_object **tag,
1617 struct got_pack *pack, struct got_packidx *packidx, int idx,
1618 struct got_object_id *id)
1620 const struct got_error *err = NULL;
1622 if (pack->privsep_child)
1623 return request_packed_tag(tag, pack, idx, id);
1625 err = start_pack_privsep_child(pack, packidx);
1626 if (err)
1627 return err;
1629 return request_packed_tag(tag, pack, idx, id);
1632 static const struct got_error *
1633 request_tag(struct got_tag_object **tag, struct got_repository *repo,
1634 int fd, struct got_object_id *id)
1636 const struct got_error *err = NULL;
1637 struct imsgbuf *ibuf;
1639 ibuf = repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TAG].ibuf;
1641 err = got_privsep_send_tag_req(ibuf, fd, id, -1);
1642 if (err)
1643 return err;
1645 return got_privsep_recv_tag(tag, ibuf);
1648 static const struct got_error *
1649 read_tag_privsep(struct got_tag_object **tag, int obj_fd,
1650 struct got_object_id *id, struct got_repository *repo)
1652 const struct got_error *err;
1653 int imsg_fds[2];
1654 pid_t pid;
1655 struct imsgbuf *ibuf;
1657 if (repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TAG].imsg_fd != -1)
1658 return request_tag(tag, repo, obj_fd, id);
1660 ibuf = calloc(1, sizeof(*ibuf));
1661 if (ibuf == NULL)
1662 return got_error_from_errno("calloc");
1664 if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_fds) == -1) {
1665 err = got_error_from_errno("socketpair");
1666 free(ibuf);
1667 return err;
1670 pid = fork();
1671 if (pid == -1) {
1672 err = got_error_from_errno("fork");
1673 free(ibuf);
1674 return err;
1676 else if (pid == 0) {
1677 got_privsep_exec_child(imsg_fds, GOT_PATH_PROG_READ_TAG,
1678 repo->path);
1679 /* not reached */
1682 if (close(imsg_fds[1]) == -1) {
1683 err = got_error_from_errno("close");
1684 free(ibuf);
1685 return err;
1687 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TAG].imsg_fd =
1688 imsg_fds[0];
1689 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TAG].pid = pid;
1690 imsg_init(ibuf, imsg_fds[0]);
1691 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TAG].ibuf = ibuf;
1693 return request_tag(tag, repo, obj_fd, id);
1696 static const struct got_error *
1697 open_tag(struct got_tag_object **tag, struct got_repository *repo,
1698 struct got_object_id *id, int check_cache)
1700 const struct got_error *err = NULL;
1701 struct got_packidx *packidx = NULL;
1702 int idx;
1703 char *path_packfile = NULL;
1704 struct got_object *obj = NULL;
1705 int obj_type = GOT_OBJ_TYPE_ANY;
1707 if (check_cache) {
1708 *tag = got_repo_get_cached_tag(repo, id);
1709 if (*tag != NULL) {
1710 (*tag)->refcnt++;
1711 return NULL;
1713 } else
1714 *tag = NULL;
1716 err = got_repo_search_packidx(&packidx, &idx, repo, id);
1717 if (err == NULL) {
1718 struct got_pack *pack = NULL;
1720 err = got_packidx_get_packfile_path(&path_packfile,
1721 packidx->path_packidx);
1722 if (err)
1723 return err;
1725 pack = got_repo_get_cached_pack(repo, path_packfile);
1726 if (pack == NULL) {
1727 err = got_repo_cache_pack(&pack, repo, path_packfile,
1728 packidx);
1729 if (err)
1730 goto done;
1733 /* Beware of "lightweight" tags: Check object type first. */
1734 err = read_packed_object_privsep(&obj, repo, pack, packidx,
1735 idx, id);
1736 if (err)
1737 goto done;
1738 obj_type = obj->type;
1739 got_object_close(obj);
1740 if (obj_type != GOT_OBJ_TYPE_TAG) {
1741 err = got_error(GOT_ERR_OBJ_TYPE);
1742 goto done;
1744 err = read_packed_tag_privsep(tag, pack, packidx, idx, id);
1745 } else if (err->code == GOT_ERR_NO_OBJ) {
1746 int fd;
1748 err = got_object_open_loose_fd(&fd, id, repo);
1749 if (err)
1750 return err;
1751 err = got_object_read_header_privsep(&obj, id, repo, fd);
1752 if (err)
1753 return err;
1754 obj_type = obj->type;
1755 got_object_close(obj);
1756 if (obj_type != GOT_OBJ_TYPE_TAG)
1757 return got_error(GOT_ERR_OBJ_TYPE);
1759 err = got_object_open_loose_fd(&fd, id, repo);
1760 if (err)
1761 return err;
1762 err = read_tag_privsep(tag, fd, id, repo);
1765 if (err == NULL) {
1766 (*tag)->refcnt++;
1767 err = got_repo_cache_tag(repo, id, *tag);
1769 done:
1770 free(path_packfile);
1771 return err;
1774 const struct got_error *
1775 got_object_open_as_tag(struct got_tag_object **tag,
1776 struct got_repository *repo, struct got_object_id *id)
1778 *tag = got_repo_get_cached_tag(repo, id);
1779 if (*tag != NULL) {
1780 (*tag)->refcnt++;
1781 return NULL;
1784 return open_tag(tag, repo, id, 0);
1787 const struct got_error *
1788 got_object_tag_open(struct got_tag_object **tag,
1789 struct got_repository *repo, struct got_object *obj)
1791 return open_tag(tag, repo, got_object_get_id(obj), 1);
1794 const char *
1795 got_object_tag_get_name(struct got_tag_object *tag)
1797 return tag->tag;
1800 int
1801 got_object_tag_get_object_type(struct got_tag_object *tag)
1803 return tag->obj_type;
1806 struct got_object_id *
1807 got_object_tag_get_object_id(struct got_tag_object *tag)
1809 return &tag->id;
1812 time_t
1813 got_object_tag_get_tagger_time(struct got_tag_object *tag)
1815 return tag->tagger_time;
1818 time_t
1819 got_object_tag_get_tagger_gmtoff(struct got_tag_object *tag)
1821 return tag->tagger_gmtoff;
1824 const char *
1825 got_object_tag_get_tagger(struct got_tag_object *tag)
1827 return tag->tagger;
1830 const char *
1831 got_object_tag_get_message(struct got_tag_object *tag)
1833 return tag->tagmsg;
1836 static struct got_tree_entry *
1837 find_entry_by_name(struct got_tree_object *tree, const char *name, size_t len)
1839 int i;
1841 /* Note that tree entries are sorted in strncmp() order. */
1842 for (i = 0; i < tree->nentries; i++) {
1843 struct got_tree_entry *te = &tree->entries[i];
1844 int cmp = strncmp(te->name, name, len);
1845 if (cmp < 0)
1846 continue;
1847 if (cmp > 0)
1848 break;
1849 if (te->name[len] == '\0')
1850 return te;
1852 return NULL;
1855 struct got_tree_entry *
1856 got_object_tree_find_entry(struct got_tree_object *tree, const char *name)
1858 return find_entry_by_name(tree, name, strlen(name));
1861 const struct got_error *
1862 got_object_tree_find_path(struct got_object_id **id, mode_t *mode,
1863 struct got_repository *repo, struct got_tree_object *tree,
1864 const char *path)
1866 const struct got_error *err = NULL;
1867 struct got_tree_object *subtree = NULL;
1868 struct got_tree_entry *te = NULL;
1869 const char *seg, *s;
1870 size_t seglen;
1872 *id = NULL;
1874 s = path;
1875 while (s[0] == '/')
1876 s++;
1877 seg = s;
1878 seglen = 0;
1879 subtree = tree;
1880 while (*s) {
1881 struct got_tree_object *next_tree;
1883 if (*s != '/') {
1884 s++;
1885 seglen++;
1886 if (*s)
1887 continue;
1890 te = find_entry_by_name(subtree, seg, seglen);
1891 if (te == NULL) {
1892 err = got_error_path(path, GOT_ERR_NO_TREE_ENTRY);
1893 goto done;
1896 if (*s == '\0')
1897 break;
1899 seg = s + 1;
1900 seglen = 0;
1901 s++;
1902 if (*s) {
1903 err = got_object_open_as_tree(&next_tree, repo,
1904 &te->id);
1905 te = NULL;
1906 if (err)
1907 goto done;
1908 if (subtree != tree)
1909 got_object_tree_close(subtree);
1910 subtree = next_tree;
1914 if (te) {
1915 *id = got_object_id_dup(&te->id);
1916 if (*id == NULL)
1917 return got_error_from_errno("got_object_id_dup");
1918 if (mode)
1919 *mode = te->mode;
1920 } else
1921 err = got_error_path(path, GOT_ERR_NO_TREE_ENTRY);
1922 done:
1923 if (subtree && subtree != tree)
1924 got_object_tree_close(subtree);
1925 return err;
1927 const struct got_error *
1928 got_object_id_by_path(struct got_object_id **id, struct got_repository *repo,
1929 struct got_object_id *commit_id, const char *path)
1931 const struct got_error *err = NULL;
1932 struct got_commit_object *commit = NULL;
1933 struct got_tree_object *tree = NULL;
1935 *id = NULL;
1937 err = got_object_open_as_commit(&commit, repo, commit_id);
1938 if (err)
1939 goto done;
1941 /* Handle opening of root of commit's tree. */
1942 if (got_path_is_root_dir(path)) {
1943 *id = got_object_id_dup(commit->tree_id);
1944 if (*id == NULL)
1945 err = got_error_from_errno("got_object_id_dup");
1946 } else {
1947 err = got_object_open_as_tree(&tree, repo, commit->tree_id);
1948 if (err)
1949 goto done;
1950 err = got_object_tree_find_path(id, NULL, repo, tree, path);
1952 done:
1953 if (commit)
1954 got_object_commit_close(commit);
1955 if (tree)
1956 got_object_tree_close(tree);
1957 return err;
1961 * Normalize file mode bits to avoid false positive tree entry differences
1962 * in case tree entries have unexpected mode bits set.
1964 static mode_t
1965 normalize_mode_for_comparison(mode_t mode)
1968 * For directories, the only relevant bit is the IFDIR bit.
1969 * This allows us to detect paths changing from a directory
1970 * to a file and vice versa.
1972 if (S_ISDIR(mode))
1973 return mode & S_IFDIR;
1976 * For symlinks, the only relevant bit is the IFLNK bit.
1977 * This allows us to detect paths changing from a symlinks
1978 * to a file or directory and vice versa.
1980 if (S_ISLNK(mode))
1981 return mode & S_IFLNK;
1983 /* For files, the only change we care about is the executable bit. */
1984 return mode & S_IXUSR;
1987 const struct got_error *
1988 got_object_tree_path_changed(int *changed,
1989 struct got_tree_object *tree01, struct got_tree_object *tree02,
1990 const char *path, struct got_repository *repo)
1992 const struct got_error *err = NULL;
1993 struct got_tree_object *tree1 = NULL, *tree2 = NULL;
1994 struct got_tree_entry *te1 = NULL, *te2 = NULL;
1995 const char *seg, *s;
1996 size_t seglen;
1998 *changed = 0;
2000 /* We not do support comparing the root path. */
2001 if (got_path_is_root_dir(path))
2002 return got_error_path(path, GOT_ERR_BAD_PATH);
2004 tree1 = tree01;
2005 tree2 = tree02;
2006 s = path;
2007 while (*s == '/')
2008 s++;
2009 seg = s;
2010 seglen = 0;
2011 while (*s) {
2012 struct got_tree_object *next_tree1, *next_tree2;
2013 mode_t mode1, mode2;
2015 if (*s != '/') {
2016 s++;
2017 seglen++;
2018 if (*s)
2019 continue;
2022 te1 = find_entry_by_name(tree1, seg, seglen);
2023 if (te1 == NULL) {
2024 err = got_error(GOT_ERR_NO_OBJ);
2025 goto done;
2028 if (tree2)
2029 te2 = find_entry_by_name(tree2, seg, seglen);
2031 if (te2) {
2032 mode1 = normalize_mode_for_comparison(te1->mode);
2033 mode2 = normalize_mode_for_comparison(te2->mode);
2034 if (mode1 != mode2) {
2035 *changed = 1;
2036 goto done;
2039 if (got_object_id_cmp(&te1->id, &te2->id) == 0) {
2040 *changed = 0;
2041 goto done;
2045 if (*s == '\0') { /* final path element */
2046 *changed = 1;
2047 goto done;
2050 seg = s + 1;
2051 s++;
2052 seglen = 0;
2053 if (*s) {
2054 err = got_object_open_as_tree(&next_tree1, repo,
2055 &te1->id);
2056 te1 = NULL;
2057 if (err)
2058 goto done;
2059 if (tree1 != tree01)
2060 got_object_tree_close(tree1);
2061 tree1 = next_tree1;
2063 if (te2) {
2064 err = got_object_open_as_tree(&next_tree2, repo,
2065 &te2->id);
2066 te2 = NULL;
2067 if (err)
2068 goto done;
2069 if (tree2 != tree02)
2070 got_object_tree_close(tree2);
2071 tree2 = next_tree2;
2072 } else if (tree2) {
2073 if (tree2 != tree02)
2074 got_object_tree_close(tree2);
2075 tree2 = NULL;
2079 done:
2080 if (tree1 && tree1 != tree01)
2081 got_object_tree_close(tree1);
2082 if (tree2 && tree2 != tree02)
2083 got_object_tree_close(tree2);
2084 return err;
2087 const struct got_error *
2088 got_object_tree_entry_dup(struct got_tree_entry **new_te,
2089 struct got_tree_entry *te)
2091 const struct got_error *err = NULL;
2093 *new_te = calloc(1, sizeof(**new_te));
2094 if (*new_te == NULL)
2095 return got_error_from_errno("calloc");
2097 (*new_te)->mode = te->mode;
2098 memcpy((*new_te)->name, te->name, sizeof((*new_te)->name));
2099 memcpy(&(*new_te)->id, &te->id, sizeof((*new_te)->id));
2100 return err;
2103 int
2104 got_object_tree_entry_is_submodule(struct got_tree_entry *te)
2106 return (te->mode & S_IFMT) == (S_IFDIR | S_IFLNK);
2109 int
2110 got_object_tree_entry_is_symlink(struct got_tree_entry *te)
2112 /* S_IFDIR check avoids confusing symlinks with submodules. */
2113 return ((te->mode & (S_IFDIR | S_IFLNK)) == S_IFLNK);
2116 static const struct got_error *
2117 resolve_symlink(char **link_target, const char *path,
2118 struct got_object_id *commit_id, struct got_repository *repo)
2120 const struct got_error *err = NULL;
2121 char buf[PATH_MAX];
2122 char *name, *parent_path = NULL;
2123 struct got_object_id *tree_obj_id = NULL;
2124 struct got_tree_object *tree = NULL;
2125 struct got_tree_entry *te = NULL;
2127 *link_target = NULL;
2129 if (strlcpy(buf, path, sizeof(buf)) >= sizeof(buf))
2130 return got_error(GOT_ERR_NO_SPACE);
2132 name = basename(buf);
2133 if (name == NULL)
2134 return got_error_from_errno2("basename", path);
2136 err = got_path_dirname(&parent_path, path);
2137 if (err)
2138 return err;
2140 err = got_object_id_by_path(&tree_obj_id, repo, commit_id,
2141 parent_path);
2142 if (err) {
2143 if (err->code == GOT_ERR_NO_TREE_ENTRY) {
2144 /* Display the complete path in error message. */
2145 err = got_error_path(path, err->code);
2147 goto done;
2150 err = got_object_open_as_tree(&tree, repo, tree_obj_id);
2151 if (err)
2152 goto done;
2154 te = got_object_tree_find_entry(tree, name);
2155 if (te == NULL) {
2156 err = got_error_path(path, GOT_ERR_NO_TREE_ENTRY);
2157 goto done;
2160 if (got_object_tree_entry_is_symlink(te)) {
2161 err = got_tree_entry_get_symlink_target(link_target, te, repo);
2162 if (err)
2163 goto done;
2164 if (!got_path_is_absolute(*link_target)) {
2165 char *abspath;
2166 if (asprintf(&abspath, "%s/%s", parent_path,
2167 *link_target) == -1) {
2168 err = got_error_from_errno("asprintf");
2169 goto done;
2171 free(*link_target);
2172 *link_target = malloc(PATH_MAX);
2173 if (*link_target == NULL) {
2174 err = got_error_from_errno("malloc");
2175 goto done;
2177 err = got_canonpath(abspath, *link_target, PATH_MAX);
2178 free(abspath);
2179 if (err)
2180 goto done;
2183 done:
2184 free(tree_obj_id);
2185 if (tree)
2186 got_object_tree_close(tree);
2187 if (err) {
2188 free(*link_target);
2189 *link_target = NULL;
2191 return err;
2194 const struct got_error *
2195 got_object_resolve_symlinks(char **link_target, const char *path,
2196 struct got_object_id *commit_id, struct got_repository *repo)
2198 const struct got_error *err = NULL;
2199 char *next_target = NULL;
2200 int max_recursion = 40; /* matches Git */
2202 *link_target = NULL;
2204 do {
2205 err = resolve_symlink(&next_target,
2206 *link_target ? *link_target : path, commit_id, repo);
2207 if (err)
2208 break;
2209 if (next_target) {
2210 free(*link_target);
2211 if (--max_recursion == 0) {
2212 err = got_error_path(path, GOT_ERR_RECURSION);
2213 *link_target = NULL;
2214 break;
2216 *link_target = next_target;
2218 } while (next_target);
2220 return err;
2223 const struct got_error *
2224 got_traverse_packed_commits(struct got_object_id_queue *traversed_commits,
2225 struct got_object_id *commit_id, const char *path,
2226 struct got_repository *repo)
2228 const struct got_error *err = NULL;
2229 struct got_pack *pack = NULL;
2230 struct got_packidx *packidx = NULL;
2231 char *path_packfile = NULL;
2232 struct got_commit_object *changed_commit = NULL;
2233 struct got_object_id *changed_commit_id = NULL;
2234 int idx;
2236 err = got_repo_search_packidx(&packidx, &idx, repo, commit_id);
2237 if (err) {
2238 if (err->code != GOT_ERR_NO_OBJ)
2239 return err;
2240 return NULL;
2243 err = got_packidx_get_packfile_path(&path_packfile,
2244 packidx->path_packidx);
2245 if (err)
2246 return err;
2248 pack = got_repo_get_cached_pack(repo, path_packfile);
2249 if (pack == NULL) {
2250 err = got_repo_cache_pack(&pack, repo, path_packfile, packidx);
2251 if (err)
2252 goto done;
2255 if (pack->privsep_child == NULL) {
2256 err = start_pack_privsep_child(pack, packidx);
2257 if (err)
2258 goto done;
2261 err = got_privsep_send_commit_traversal_request(
2262 pack->privsep_child->ibuf, commit_id, idx, path);
2263 if (err)
2264 goto done;
2266 err = got_privsep_recv_traversed_commits(&changed_commit,
2267 &changed_commit_id, traversed_commits, pack->privsep_child->ibuf);
2268 if (err)
2269 goto done;
2271 if (changed_commit) {
2273 * Cache the commit in which the path was changed.
2274 * This commit might be opened again soon.
2276 changed_commit->refcnt++;
2277 err = got_repo_cache_commit(repo, changed_commit_id,
2278 changed_commit);
2279 got_object_commit_close(changed_commit);
2281 done:
2282 free(path_packfile);
2283 free(changed_commit_id);
2284 return err;