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 | O_CLOEXEC);
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 /* Create temporary files used during delta application. */
168 static const struct got_error *
169 pack_child_send_tempfiles(struct imsgbuf *ibuf, struct got_pack *pack)
171 const struct got_error *err;
172 int basefd, accumfd;
174 /*
175 * For performance reasons, the child will keep reusing the
176 * same temporary files during every object request.
177 * Opening and closing new files for every object request is
178 * too expensive during operations such as 'gotadmin pack'.
179 */
180 if (pack->child_has_tempfiles)
181 return NULL;
183 basefd = got_opentempfd();
184 if (basefd == -1)
185 return got_error_from_errno("got_opentempfd");
187 err = got_privsep_send_tmpfd(ibuf, basefd);
188 if (err)
189 return err;
191 accumfd = got_opentempfd();
192 if (accumfd == -1)
193 return got_error_from_errno("got_opentempfd");
195 err = got_privsep_send_tmpfd(ibuf, accumfd);
196 if (err)
197 return err;
199 pack->child_has_tempfiles = 1;
200 return NULL;
203 static const struct got_error *
204 request_packed_object_raw(uint8_t **outbuf, off_t *size, size_t *hdrlen,
205 int outfd, struct got_pack *pack, int idx, struct got_object_id *id)
207 const struct got_error *err = NULL;
208 struct imsgbuf *ibuf = pack->privsep_child->ibuf;
209 int outfd_child;
211 err = pack_child_send_tempfiles(ibuf, pack);
212 if (err)
213 return err;
215 outfd_child = dup(outfd);
216 if (outfd_child == -1)
217 return got_error_from_errno("dup");
219 err = got_privsep_send_packed_raw_obj_req(ibuf, idx, id);
220 if (err) {
221 close(outfd_child);
222 return err;
225 err = got_privsep_send_raw_obj_outfd(ibuf, outfd_child);
226 if (err)
227 return err;
229 err = got_privsep_recv_raw_obj(outbuf, size, hdrlen, ibuf);
230 if (err)
231 return err;
233 return NULL;
236 static void
237 set_max_datasize(void)
239 struct rlimit rl;
241 if (getrlimit(RLIMIT_DATA, &rl) != 0)
242 return;
244 rl.rlim_cur = rl.rlim_max;
245 setrlimit(RLIMIT_DATA, &rl);
248 static const struct got_error *
249 start_pack_privsep_child(struct got_pack *pack, struct got_packidx *packidx)
251 const struct got_error *err = NULL;
252 int imsg_fds[2];
253 pid_t pid;
254 struct imsgbuf *ibuf;
256 ibuf = calloc(1, sizeof(*ibuf));
257 if (ibuf == NULL)
258 return got_error_from_errno("calloc");
260 pack->privsep_child = calloc(1, sizeof(*pack->privsep_child));
261 if (pack->privsep_child == NULL) {
262 err = got_error_from_errno("calloc");
263 free(ibuf);
264 return err;
266 pack->child_has_tempfiles = 0;
268 if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_fds) == -1) {
269 err = got_error_from_errno("socketpair");
270 goto done;
273 pid = fork();
274 if (pid == -1) {
275 err = got_error_from_errno("fork");
276 goto done;
277 } else if (pid == 0) {
278 set_max_datasize();
279 got_privsep_exec_child(imsg_fds, GOT_PATH_PROG_READ_PACK,
280 pack->path_packfile);
281 /* not reached */
284 if (close(imsg_fds[1]) == -1)
285 return got_error_from_errno("close");
286 pack->privsep_child->imsg_fd = imsg_fds[0];
287 pack->privsep_child->pid = pid;
288 imsg_init(ibuf, imsg_fds[0]);
289 pack->privsep_child->ibuf = ibuf;
291 err = got_privsep_init_pack_child(ibuf, pack, packidx);
292 if (err) {
293 const struct got_error *child_err;
294 err = got_privsep_send_stop(pack->privsep_child->imsg_fd);
295 child_err = got_privsep_wait_for_child(
296 pack->privsep_child->pid);
297 if (child_err && err == NULL)
298 err = child_err;
300 done:
301 if (err) {
302 free(ibuf);
303 free(pack->privsep_child);
304 pack->privsep_child = NULL;
306 return err;
309 static const struct got_error *
310 read_packed_object_privsep(struct got_object **obj,
311 struct got_repository *repo, struct got_pack *pack,
312 struct got_packidx *packidx, int idx, struct got_object_id *id)
314 const struct got_error *err = NULL;
316 if (pack->privsep_child == NULL) {
317 err = start_pack_privsep_child(pack, packidx);
318 if (err)
319 return err;
322 return request_packed_object(obj, pack, idx, id);
325 static const struct got_error *
326 read_packed_object_raw_privsep(uint8_t **outbuf, off_t *size, size_t *hdrlen,
327 int outfd, struct got_pack *pack, struct got_packidx *packidx, int idx,
328 struct got_object_id *id)
330 const struct got_error *err = NULL;
332 if (pack->privsep_child == NULL) {
333 err = start_pack_privsep_child(pack, packidx);
334 if (err)
335 return err;
338 return request_packed_object_raw(outbuf, size, hdrlen, outfd, pack,
339 idx, id);
342 const struct got_error *
343 got_object_open_packed(struct got_object **obj, struct got_object_id *id,
344 struct got_repository *repo)
346 const struct got_error *err = NULL;
347 struct got_pack *pack = NULL;
348 struct got_packidx *packidx = NULL;
349 int idx;
350 char *path_packfile;
352 err = got_repo_search_packidx(&packidx, &idx, repo, id);
353 if (err)
354 return err;
356 err = got_packidx_get_packfile_path(&path_packfile,
357 packidx->path_packidx);
358 if (err)
359 return err;
361 pack = got_repo_get_cached_pack(repo, path_packfile);
362 if (pack == NULL) {
363 err = got_repo_cache_pack(&pack, repo, path_packfile, packidx);
364 if (err)
365 goto done;
368 err = read_packed_object_privsep(obj, repo, pack, packidx, idx, id);
369 if (err)
370 goto done;
371 done:
372 free(path_packfile);
373 return err;
376 static const struct got_error *
377 request_object(struct got_object **obj, struct got_object_id *id,
378 struct got_repository *repo, int fd)
380 const struct got_error *err = NULL;
381 struct imsgbuf *ibuf;
383 ibuf = repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_OBJECT].ibuf;
385 err = got_privsep_send_obj_req(ibuf, fd, id);
386 if (err)
387 return err;
389 return got_privsep_recv_obj(obj, ibuf);
392 static const struct got_error *
393 request_raw_object(uint8_t **outbuf, off_t *size, size_t *hdrlen, int outfd,
394 struct got_object_id *id, struct got_repository *repo, int infd)
396 const struct got_error *err = NULL;
397 struct imsgbuf *ibuf;
398 int outfd_child;
400 ibuf = repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_OBJECT].ibuf;
402 outfd_child = dup(outfd);
403 if (outfd_child == -1)
404 return got_error_from_errno("dup");
406 err = got_privsep_send_raw_obj_req(ibuf, infd, id);
407 if (err)
408 return err;
410 err = got_privsep_send_raw_obj_outfd(ibuf, outfd_child);
411 if (err)
412 return err;
414 return got_privsep_recv_raw_obj(outbuf, size, hdrlen, ibuf);
417 static const struct got_error *
418 start_read_object_child(struct got_repository *repo)
420 const struct got_error *err = NULL;
421 int imsg_fds[2];
422 pid_t pid;
423 struct imsgbuf *ibuf;
425 ibuf = calloc(1, sizeof(*ibuf));
426 if (ibuf == NULL)
427 return got_error_from_errno("calloc");
429 if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_fds) == -1) {
430 err = got_error_from_errno("socketpair");
431 free(ibuf);
432 return err;
435 pid = fork();
436 if (pid == -1) {
437 err = got_error_from_errno("fork");
438 free(ibuf);
439 return err;
441 else if (pid == 0) {
442 got_privsep_exec_child(imsg_fds, GOT_PATH_PROG_READ_OBJECT,
443 repo->path);
444 /* not reached */
447 if (close(imsg_fds[1]) == -1) {
448 err = got_error_from_errno("close");
449 free(ibuf);
450 return err;
453 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_OBJECT].imsg_fd =
454 imsg_fds[0];
455 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_OBJECT].pid = pid;
456 imsg_init(ibuf, imsg_fds[0]);
457 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_OBJECT].ibuf = ibuf;
459 return NULL;
462 const struct got_error *
463 got_object_read_header_privsep(struct got_object **obj,
464 struct got_object_id *id, struct got_repository *repo, int obj_fd)
466 const struct got_error *err;
468 if (repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_OBJECT].imsg_fd != -1)
469 return request_object(obj, id, repo, obj_fd);
471 err = start_read_object_child(repo);
472 if (err) {
473 close(obj_fd);
474 return err;
477 return request_object(obj, id, repo, obj_fd);
480 static const struct got_error *
481 read_object_raw_privsep(uint8_t **outbuf, off_t *size, size_t *hdrlen,
482 int outfd, struct got_object_id *id, struct got_repository *repo,
483 int obj_fd)
485 const struct got_error *err;
487 if (repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_OBJECT].imsg_fd != -1)
488 return request_raw_object(outbuf, size, hdrlen, outfd, id,
489 repo, obj_fd);
491 err = start_read_object_child(repo);
492 if (err)
493 return err;
495 return request_raw_object(outbuf, size, hdrlen, outfd, id, repo,
496 obj_fd);
499 const struct got_error *
500 got_object_open(struct got_object **obj, struct got_repository *repo,
501 struct got_object_id *id)
503 const struct got_error *err = NULL;
504 int fd;
506 *obj = got_repo_get_cached_object(repo, id);
507 if (*obj != NULL) {
508 (*obj)->refcnt++;
509 return NULL;
512 err = got_object_open_packed(obj, id, repo);
513 if (err && err->code != GOT_ERR_NO_OBJ)
514 return err;
515 if (*obj) {
516 (*obj)->refcnt++;
517 return got_repo_cache_object(repo, id, *obj);
520 err = got_object_open_loose_fd(&fd, id, repo);
521 if (err) {
522 if (err->code == GOT_ERR_ERRNO && errno == ENOENT)
523 err = got_error_no_obj(id);
524 return err;
527 err = got_object_read_header_privsep(obj, id, repo, fd);
528 if (err)
529 return err;
531 memcpy((*obj)->id.sha1, id->sha1, SHA1_DIGEST_LENGTH);
533 (*obj)->refcnt++;
534 return got_repo_cache_object(repo, id, *obj);
537 /* *outfd must be initialized to -1 by caller */
538 const struct got_error *
539 got_object_raw_open(struct got_raw_object **obj, int *outfd,
540 struct got_repository *repo, struct got_object_id *id)
542 const struct got_error *err = NULL;
543 struct got_packidx *packidx = NULL;
544 int idx;
545 uint8_t *outbuf = NULL;
546 off_t size = 0;
547 size_t hdrlen = 0;
548 char *path_packfile = NULL;
550 *obj = got_repo_get_cached_raw_object(repo, id);
551 if (*obj != NULL) {
552 (*obj)->refcnt++;
553 return NULL;
556 if (*outfd == -1) {
557 *outfd = got_opentempfd();
558 if (*outfd == -1)
559 return got_error_from_errno("got_opentempfd");
562 err = got_repo_search_packidx(&packidx, &idx, repo, id);
563 if (err == NULL) {
564 struct got_pack *pack = NULL;
566 err = got_packidx_get_packfile_path(&path_packfile,
567 packidx->path_packidx);
568 if (err)
569 goto done;
571 pack = got_repo_get_cached_pack(repo, path_packfile);
572 if (pack == NULL) {
573 err = got_repo_cache_pack(&pack, repo, path_packfile,
574 packidx);
575 if (err)
576 goto done;
578 err = read_packed_object_raw_privsep(&outbuf, &size, &hdrlen,
579 *outfd, pack, packidx, idx, id);
580 if (err)
581 goto done;
582 } else if (err->code == GOT_ERR_NO_OBJ) {
583 int fd;
585 err = got_object_open_loose_fd(&fd, id, repo);
586 if (err)
587 goto done;
588 err = read_object_raw_privsep(&outbuf, &size, &hdrlen, *outfd,
589 id, repo, fd);
590 if (err)
591 goto done;
594 *obj = calloc(1, sizeof(**obj));
595 if (*obj == NULL) {
596 err = got_error_from_errno("calloc");
597 goto done;
600 if (outbuf) {
601 (*obj)->f = fmemopen(outbuf, hdrlen + size, "r");
602 if ((*obj)->f == NULL) {
603 err = got_error_from_errno("fdopen");
604 goto done;
606 (*obj)->data = outbuf;
607 } else {
608 struct stat sb;
609 if (fstat(*outfd, &sb) == -1) {
610 err = got_error_from_errno("fstat");
611 goto done;
614 if (sb.st_size != hdrlen + size) {
615 err = got_error(GOT_ERR_PRIVSEP_LEN);
616 goto done;
619 (*obj)->f = fdopen(*outfd, "r");
620 if ((*obj)->f == NULL) {
621 err = got_error_from_errno("fdopen");
622 goto done;
624 (*obj)->data = NULL;
625 *outfd = -1;
627 (*obj)->hdrlen = hdrlen;
628 (*obj)->size = size;
629 err = got_repo_cache_raw_object(repo, id, *obj);
630 done:
631 free(path_packfile);
632 if (err) {
633 if (*obj) {
634 got_object_raw_close(*obj);
635 *obj = NULL;
637 free(outbuf);
638 } else
639 (*obj)->refcnt++;
640 return err;
643 const struct got_error *
644 got_object_open_by_id_str(struct got_object **obj, struct got_repository *repo,
645 const char *id_str)
647 struct got_object_id id;
649 if (!got_parse_sha1_digest(id.sha1, id_str))
650 return got_error_path(id_str, GOT_ERR_BAD_OBJ_ID_STR);
652 return got_object_open(obj, repo, &id);
655 const struct got_error *
656 got_object_resolve_id_str(struct got_object_id **id,
657 struct got_repository *repo, const char *id_str)
659 const struct got_error *err = NULL;
660 struct got_object *obj;
662 err = got_object_open_by_id_str(&obj, repo, id_str);
663 if (err)
664 return err;
666 *id = got_object_id_dup(got_object_get_id(obj));
667 got_object_close(obj);
668 if (*id == NULL)
669 return got_error_from_errno("got_object_id_dup");
671 return NULL;
674 static const struct got_error *
675 request_packed_commit(struct got_commit_object **commit, struct got_pack *pack,
676 int pack_idx, struct got_object_id *id)
678 const struct got_error *err = NULL;
680 err = got_privsep_send_commit_req(pack->privsep_child->ibuf, -1, id,
681 pack_idx);
682 if (err)
683 return err;
685 err = got_privsep_recv_commit(commit, pack->privsep_child->ibuf);
686 if (err)
687 return err;
689 (*commit)->flags |= GOT_COMMIT_FLAG_PACKED;
690 return NULL;
693 static const struct got_error *
694 read_packed_commit_privsep(struct got_commit_object **commit,
695 struct got_pack *pack, struct got_packidx *packidx, int idx,
696 struct got_object_id *id)
698 const struct got_error *err = NULL;
700 if (pack->privsep_child)
701 return request_packed_commit(commit, pack, idx, id);
703 err = start_pack_privsep_child(pack, packidx);
704 if (err)
705 return err;
707 return request_packed_commit(commit, pack, idx, id);
710 static const struct got_error *
711 request_commit(struct got_commit_object **commit, struct got_repository *repo,
712 int fd, struct got_object_id *id)
714 const struct got_error *err = NULL;
715 struct imsgbuf *ibuf;
717 ibuf = repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_COMMIT].ibuf;
719 err = got_privsep_send_commit_req(ibuf, fd, id, -1);
720 if (err)
721 return err;
723 return got_privsep_recv_commit(commit, ibuf);
726 static const struct got_error *
727 read_commit_privsep(struct got_commit_object **commit, int obj_fd,
728 struct got_object_id *id, struct got_repository *repo)
730 const struct got_error *err;
731 int imsg_fds[2];
732 pid_t pid;
733 struct imsgbuf *ibuf;
735 if (repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_COMMIT].imsg_fd != -1)
736 return request_commit(commit, repo, obj_fd, id);
738 ibuf = calloc(1, sizeof(*ibuf));
739 if (ibuf == NULL)
740 return got_error_from_errno("calloc");
742 if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_fds) == -1) {
743 err = got_error_from_errno("socketpair");
744 free(ibuf);
745 return err;
748 pid = fork();
749 if (pid == -1) {
750 err = got_error_from_errno("fork");
751 free(ibuf);
752 return err;
754 else if (pid == 0) {
755 got_privsep_exec_child(imsg_fds, GOT_PATH_PROG_READ_COMMIT,
756 repo->path);
757 /* not reached */
760 if (close(imsg_fds[1]) == -1) {
761 err = got_error_from_errno("close");
762 free(ibuf);
763 return err;
765 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_COMMIT].imsg_fd =
766 imsg_fds[0];
767 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_COMMIT].pid = pid;
768 imsg_init(ibuf, imsg_fds[0]);
769 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_COMMIT].ibuf = ibuf;
771 return request_commit(commit, repo, obj_fd, id);
775 static const struct got_error *
776 open_commit(struct got_commit_object **commit,
777 struct got_repository *repo, struct got_object_id *id, int check_cache)
779 const struct got_error *err = NULL;
780 struct got_packidx *packidx = NULL;
781 int idx;
782 char *path_packfile = NULL;
784 if (check_cache) {
785 *commit = got_repo_get_cached_commit(repo, id);
786 if (*commit != NULL) {
787 (*commit)->refcnt++;
788 return NULL;
790 } else
791 *commit = NULL;
793 err = got_repo_search_packidx(&packidx, &idx, repo, id);
794 if (err == NULL) {
795 struct got_pack *pack = NULL;
797 err = got_packidx_get_packfile_path(&path_packfile,
798 packidx->path_packidx);
799 if (err)
800 return err;
802 pack = got_repo_get_cached_pack(repo, path_packfile);
803 if (pack == NULL) {
804 err = got_repo_cache_pack(&pack, repo, path_packfile,
805 packidx);
806 if (err)
807 goto done;
809 err = read_packed_commit_privsep(commit, pack,
810 packidx, idx, id);
811 } else if (err->code == GOT_ERR_NO_OBJ) {
812 int fd;
814 err = got_object_open_loose_fd(&fd, id, repo);
815 if (err)
816 return err;
817 err = read_commit_privsep(commit, fd, id, repo);
820 if (err == NULL) {
821 (*commit)->refcnt++;
822 err = got_repo_cache_commit(repo, id, *commit);
824 done:
825 free(path_packfile);
826 return err;
829 const struct got_error *
830 got_object_open_as_commit(struct got_commit_object **commit,
831 struct got_repository *repo, struct got_object_id *id)
833 *commit = got_repo_get_cached_commit(repo, id);
834 if (*commit != NULL) {
835 (*commit)->refcnt++;
836 return NULL;
839 return open_commit(commit, repo, id, 0);
842 const struct got_error *
843 got_object_commit_open(struct got_commit_object **commit,
844 struct got_repository *repo, struct got_object *obj)
846 return open_commit(commit, repo, got_object_get_id(obj), 1);
849 const struct got_error *
850 got_object_qid_alloc(struct got_object_qid **qid, struct got_object_id *id)
852 const struct got_error *err = NULL;
854 *qid = calloc(1, sizeof(**qid));
855 if (*qid == NULL)
856 return got_error_from_errno("calloc");
858 (*qid)->id = got_object_id_dup(id);
859 if ((*qid)->id == NULL) {
860 err = got_error_from_errno("got_object_id_dup");
861 got_object_qid_free(*qid);
862 *qid = NULL;
863 return err;
866 return NULL;
869 const struct got_error *
870 got_object_id_queue_copy(const struct got_object_id_queue *src,
871 struct got_object_id_queue *dest)
873 const struct got_error *err;
874 struct got_object_qid *qid;
876 STAILQ_FOREACH(qid, src, entry) {
877 struct got_object_qid *new;
878 /*
879 * Deep-copy the object ID only. Let the caller deal
880 * with setting up the new->data pointer if needed.
881 */
882 err = got_object_qid_alloc(&new, qid->id);
883 if (err) {
884 got_object_id_queue_free(dest);
885 return err;
887 STAILQ_INSERT_TAIL(dest, new, entry);
890 return NULL;
893 static const struct got_error *
894 request_packed_tree(struct got_tree_object **tree, struct got_pack *pack,
895 int pack_idx, struct got_object_id *id)
897 const struct got_error *err = NULL;
899 err = got_privsep_send_tree_req(pack->privsep_child->ibuf, -1, id,
900 pack_idx);
901 if (err)
902 return err;
904 return got_privsep_recv_tree(tree, pack->privsep_child->ibuf);
907 static const struct got_error *
908 read_packed_tree_privsep(struct got_tree_object **tree,
909 struct got_pack *pack, struct got_packidx *packidx, int idx,
910 struct got_object_id *id)
912 const struct got_error *err = NULL;
914 if (pack->privsep_child)
915 return request_packed_tree(tree, pack, idx, id);
917 err = start_pack_privsep_child(pack, packidx);
918 if (err)
919 return err;
921 return request_packed_tree(tree, pack, idx, id);
924 static const struct got_error *
925 request_tree(struct got_tree_object **tree, struct got_repository *repo,
926 int fd, struct got_object_id *id)
928 const struct got_error *err = NULL;
929 struct imsgbuf *ibuf;
931 ibuf = repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TREE].ibuf;
933 err = got_privsep_send_tree_req(ibuf, fd, id, -1);
934 if (err)
935 return err;
937 return got_privsep_recv_tree(tree, ibuf);
940 const struct got_error *
941 read_tree_privsep(struct got_tree_object **tree, int obj_fd,
942 struct got_object_id *id, struct got_repository *repo)
944 const struct got_error *err;
945 int imsg_fds[2];
946 pid_t pid;
947 struct imsgbuf *ibuf;
949 if (repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TREE].imsg_fd != -1)
950 return request_tree(tree, repo, obj_fd, id);
952 ibuf = calloc(1, sizeof(*ibuf));
953 if (ibuf == NULL)
954 return got_error_from_errno("calloc");
956 if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_fds) == -1) {
957 err = got_error_from_errno("socketpair");
958 free(ibuf);
959 return err;
962 pid = fork();
963 if (pid == -1) {
964 err = got_error_from_errno("fork");
965 free(ibuf);
966 return err;
968 else if (pid == 0) {
969 got_privsep_exec_child(imsg_fds, GOT_PATH_PROG_READ_TREE,
970 repo->path);
971 /* not reached */
974 if (close(imsg_fds[1]) == -1) {
975 err = got_error_from_errno("close");
976 free(ibuf);
977 return err;
979 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TREE].imsg_fd =
980 imsg_fds[0];
981 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TREE].pid = pid;
982 imsg_init(ibuf, imsg_fds[0]);
983 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TREE].ibuf = ibuf;
986 return request_tree(tree, repo, obj_fd, id);
989 static const struct got_error *
990 open_tree(struct got_tree_object **tree, struct got_repository *repo,
991 struct got_object_id *id, int check_cache)
993 const struct got_error *err = NULL;
994 struct got_packidx *packidx = NULL;
995 int idx;
996 char *path_packfile = NULL;
998 if (check_cache) {
999 *tree = got_repo_get_cached_tree(repo, id);
1000 if (*tree != NULL) {
1001 (*tree)->refcnt++;
1002 return NULL;
1004 } else
1005 *tree = NULL;
1007 err = got_repo_search_packidx(&packidx, &idx, repo, id);
1008 if (err == NULL) {
1009 struct got_pack *pack = NULL;
1011 err = got_packidx_get_packfile_path(&path_packfile,
1012 packidx->path_packidx);
1013 if (err)
1014 return err;
1016 pack = got_repo_get_cached_pack(repo, path_packfile);
1017 if (pack == NULL) {
1018 err = got_repo_cache_pack(&pack, repo, path_packfile,
1019 packidx);
1020 if (err)
1021 goto done;
1023 err = read_packed_tree_privsep(tree, pack,
1024 packidx, idx, id);
1025 } else if (err->code == GOT_ERR_NO_OBJ) {
1026 int fd;
1028 err = got_object_open_loose_fd(&fd, id, repo);
1029 if (err)
1030 return err;
1031 err = read_tree_privsep(tree, fd, id, repo);
1034 if (err == NULL) {
1035 (*tree)->refcnt++;
1036 err = got_repo_cache_tree(repo, id, *tree);
1038 done:
1039 free(path_packfile);
1040 return err;
1043 const struct got_error *
1044 got_object_open_as_tree(struct got_tree_object **tree,
1045 struct got_repository *repo, struct got_object_id *id)
1047 *tree = got_repo_get_cached_tree(repo, id);
1048 if (*tree != NULL) {
1049 (*tree)->refcnt++;
1050 return NULL;
1053 return open_tree(tree, repo, id, 0);
1056 const struct got_error *
1057 got_object_tree_open(struct got_tree_object **tree,
1058 struct got_repository *repo, struct got_object *obj)
1060 return open_tree(tree, repo, got_object_get_id(obj), 1);
1063 int
1064 got_object_tree_get_nentries(struct got_tree_object *tree)
1066 return tree->nentries;
1069 struct got_tree_entry *
1070 got_object_tree_get_first_entry(struct got_tree_object *tree)
1072 return got_object_tree_get_entry(tree, 0);
1075 struct got_tree_entry *
1076 got_object_tree_get_last_entry(struct got_tree_object *tree)
1078 return got_object_tree_get_entry(tree, tree->nentries - 1);
1081 struct got_tree_entry *
1082 got_object_tree_get_entry(struct got_tree_object *tree, int i)
1084 if (i < 0 || i >= tree->nentries)
1085 return NULL;
1086 return &tree->entries[i];
1089 mode_t
1090 got_tree_entry_get_mode(struct got_tree_entry *te)
1092 return te->mode;
1095 const char *
1096 got_tree_entry_get_name(struct got_tree_entry *te)
1098 return &te->name[0];
1101 struct got_object_id *
1102 got_tree_entry_get_id(struct got_tree_entry *te)
1104 return &te->id;
1107 const struct got_error *
1108 got_object_blob_read_to_str(char **s, struct got_blob_object *blob)
1110 const struct got_error *err = NULL;
1111 size_t len, totlen, hdrlen, offset;
1113 *s = NULL;
1115 hdrlen = got_object_blob_get_hdrlen(blob);
1116 totlen = 0;
1117 offset = 0;
1118 do {
1119 char *p;
1121 err = got_object_blob_read_block(&len, blob);
1122 if (err)
1123 return err;
1125 if (len == 0)
1126 break;
1128 totlen += len - hdrlen;
1129 p = realloc(*s, totlen + 1);
1130 if (p == NULL) {
1131 err = got_error_from_errno("realloc");
1132 free(*s);
1133 *s = NULL;
1134 return err;
1136 *s = p;
1137 /* Skip blob object header first time around. */
1138 memcpy(*s + offset,
1139 got_object_blob_get_read_buf(blob) + hdrlen, len - hdrlen);
1140 hdrlen = 0;
1141 offset = totlen;
1142 } while (len > 0);
1144 (*s)[totlen] = '\0';
1145 return NULL;
1148 const struct got_error *
1149 got_tree_entry_get_symlink_target(char **link_target, struct got_tree_entry *te,
1150 struct got_repository *repo)
1152 const struct got_error *err = NULL;
1153 struct got_blob_object *blob = NULL;
1155 *link_target = NULL;
1157 if (!got_object_tree_entry_is_symlink(te))
1158 return got_error(GOT_ERR_TREE_ENTRY_TYPE);
1160 err = got_object_open_as_blob(&blob, repo,
1161 got_tree_entry_get_id(te), PATH_MAX);
1162 if (err)
1163 return err;
1165 err = got_object_blob_read_to_str(link_target, blob);
1166 got_object_blob_close(blob);
1167 if (err) {
1168 free(*link_target);
1169 *link_target = NULL;
1171 return err;
1174 int
1175 got_tree_entry_get_index(struct got_tree_entry *te)
1177 return te->idx;
1180 struct got_tree_entry *
1181 got_tree_entry_get_next(struct got_tree_object *tree,
1182 struct got_tree_entry *te)
1184 return got_object_tree_get_entry(tree, te->idx + 1);
1187 struct got_tree_entry *
1188 got_tree_entry_get_prev(struct got_tree_object *tree,
1189 struct got_tree_entry *te)
1191 return got_object_tree_get_entry(tree, te->idx - 1);
1194 static const struct got_error *
1195 request_packed_blob(uint8_t **outbuf, size_t *size, size_t *hdrlen, int outfd,
1196 struct got_pack *pack, struct got_packidx *packidx, int idx,
1197 struct got_object_id *id)
1199 const struct got_error *err = NULL;
1200 struct imsgbuf *ibuf = pack->privsep_child->ibuf;
1201 int outfd_child;
1203 err = pack_child_send_tempfiles(ibuf, pack);
1204 if (err)
1205 return err;
1207 outfd_child = dup(outfd);
1208 if (outfd_child == -1)
1209 return got_error_from_errno("dup");
1211 err = got_privsep_send_blob_req(pack->privsep_child->ibuf, -1, id, idx);
1212 if (err)
1213 return err;
1215 err = got_privsep_send_blob_outfd(pack->privsep_child->ibuf,
1216 outfd_child);
1217 if (err) {
1218 return err;
1221 err = got_privsep_recv_blob(outbuf, size, hdrlen,
1222 pack->privsep_child->ibuf);
1223 if (err)
1224 return err;
1226 if (lseek(outfd, SEEK_SET, 0) == -1)
1227 err = got_error_from_errno("lseek");
1229 return err;
1232 static const struct got_error *
1233 read_packed_blob_privsep(uint8_t **outbuf, size_t *size, size_t *hdrlen,
1234 int outfd, struct got_pack *pack, struct got_packidx *packidx, int idx,
1235 struct got_object_id *id)
1237 const struct got_error *err = NULL;
1239 if (pack->privsep_child == NULL) {
1240 err = start_pack_privsep_child(pack, packidx);
1241 if (err)
1242 return err;
1245 return request_packed_blob(outbuf, size, hdrlen, outfd, pack, packidx,
1246 idx, id);
1249 static const struct got_error *
1250 request_blob(uint8_t **outbuf, size_t *size, size_t *hdrlen, int outfd,
1251 int infd, struct got_object_id *id, struct imsgbuf *ibuf)
1253 const struct got_error *err = NULL;
1254 int outfd_child;
1256 outfd_child = dup(outfd);
1257 if (outfd_child == -1)
1258 return got_error_from_errno("dup");
1260 err = got_privsep_send_blob_req(ibuf, infd, id, -1);
1261 if (err)
1262 return err;
1264 err = got_privsep_send_blob_outfd(ibuf, outfd_child);
1265 if (err)
1266 return err;
1268 err = got_privsep_recv_blob(outbuf, size, hdrlen, ibuf);
1269 if (err)
1270 return err;
1272 if (lseek(outfd, SEEK_SET, 0) == -1)
1273 return got_error_from_errno("lseek");
1275 return err;
1278 static const struct got_error *
1279 read_blob_privsep(uint8_t **outbuf, size_t *size, size_t *hdrlen,
1280 int outfd, int infd, struct got_object_id *id, struct got_repository *repo)
1282 const struct got_error *err;
1283 int imsg_fds[2];
1284 pid_t pid;
1285 struct imsgbuf *ibuf;
1287 if (repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_BLOB].imsg_fd != -1) {
1288 ibuf = repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_BLOB].ibuf;
1289 return request_blob(outbuf, size, hdrlen, outfd, infd, id,
1290 ibuf);
1293 ibuf = calloc(1, sizeof(*ibuf));
1294 if (ibuf == NULL)
1295 return got_error_from_errno("calloc");
1297 if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_fds) == -1) {
1298 err = got_error_from_errno("socketpair");
1299 free(ibuf);
1300 return err;
1303 pid = fork();
1304 if (pid == -1) {
1305 err = got_error_from_errno("fork");
1306 free(ibuf);
1307 return err;
1309 else if (pid == 0) {
1310 got_privsep_exec_child(imsg_fds, GOT_PATH_PROG_READ_BLOB,
1311 repo->path);
1312 /* not reached */
1315 if (close(imsg_fds[1]) == -1) {
1316 err = got_error_from_errno("close");
1317 free(ibuf);
1318 return err;
1320 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_BLOB].imsg_fd =
1321 imsg_fds[0];
1322 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_BLOB].pid = pid;
1323 imsg_init(ibuf, imsg_fds[0]);
1324 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_BLOB].ibuf = ibuf;
1326 return request_blob(outbuf, size, hdrlen, outfd, infd, id, ibuf);
1329 static const struct got_error *
1330 open_blob(struct got_blob_object **blob, struct got_repository *repo,
1331 struct got_object_id *id, size_t blocksize)
1333 const struct got_error *err = NULL;
1334 struct got_packidx *packidx = NULL;
1335 int idx;
1336 char *path_packfile = NULL;
1337 uint8_t *outbuf;
1338 int outfd;
1339 size_t size, hdrlen;
1340 struct stat sb;
1342 *blob = calloc(1, sizeof(**blob));
1343 if (*blob == NULL)
1344 return got_error_from_errno("calloc");
1346 outfd = got_opentempfd();
1347 if (outfd == -1)
1348 return got_error_from_errno("got_opentempfd");
1350 (*blob)->read_buf = malloc(blocksize);
1351 if ((*blob)->read_buf == NULL) {
1352 err = got_error_from_errno("malloc");
1353 goto done;
1356 err = got_repo_search_packidx(&packidx, &idx, repo, id);
1357 if (err == NULL) {
1358 struct got_pack *pack = NULL;
1360 err = got_packidx_get_packfile_path(&path_packfile,
1361 packidx->path_packidx);
1362 if (err)
1363 goto done;
1365 pack = got_repo_get_cached_pack(repo, path_packfile);
1366 if (pack == NULL) {
1367 err = got_repo_cache_pack(&pack, repo, path_packfile,
1368 packidx);
1369 if (err)
1370 goto done;
1372 err = read_packed_blob_privsep(&outbuf, &size, &hdrlen, outfd,
1373 pack, packidx, idx, id);
1374 } else if (err->code == GOT_ERR_NO_OBJ) {
1375 int infd;
1377 err = got_object_open_loose_fd(&infd, id, repo);
1378 if (err)
1379 goto done;
1380 err = read_blob_privsep(&outbuf, &size, &hdrlen, outfd, infd,
1381 id, repo);
1383 if (err)
1384 goto done;
1386 if (hdrlen > size) {
1387 err = got_error(GOT_ERR_BAD_OBJ_HDR);
1388 goto done;
1391 if (outbuf) {
1392 if (close(outfd) == -1 && err == NULL)
1393 err = got_error_from_errno("close");
1394 outfd = -1;
1395 (*blob)->f = fmemopen(outbuf, size, "rb");
1396 if ((*blob)->f == NULL) {
1397 err = got_error_from_errno("fmemopen");
1398 free(outbuf);
1399 goto done;
1401 (*blob)->data = outbuf;
1402 } else {
1403 if (fstat(outfd, &sb) == -1) {
1404 err = got_error_from_errno("fstat");
1405 goto done;
1408 if (sb.st_size != size) {
1409 err = got_error(GOT_ERR_PRIVSEP_LEN);
1410 goto done;
1413 (*blob)->f = fdopen(outfd, "rb");
1414 if ((*blob)->f == NULL) {
1415 err = got_error_from_errno("fdopen");
1416 close(outfd);
1417 outfd = -1;
1418 goto done;
1422 (*blob)->hdrlen = hdrlen;
1423 (*blob)->blocksize = blocksize;
1424 memcpy(&(*blob)->id.sha1, id->sha1, SHA1_DIGEST_LENGTH);
1426 done:
1427 free(path_packfile);
1428 if (err) {
1429 if (*blob) {
1430 got_object_blob_close(*blob);
1431 *blob = NULL;
1432 } else if (outfd != -1)
1433 close(outfd);
1435 return err;
1438 const struct got_error *
1439 got_object_open_as_blob(struct got_blob_object **blob,
1440 struct got_repository *repo, struct got_object_id *id,
1441 size_t blocksize)
1443 return open_blob(blob, repo, id, blocksize);
1446 const struct got_error *
1447 got_object_blob_open(struct got_blob_object **blob,
1448 struct got_repository *repo, struct got_object *obj, size_t blocksize)
1450 return open_blob(blob, repo, got_object_get_id(obj), blocksize);
1453 const struct got_error *
1454 got_object_blob_close(struct got_blob_object *blob)
1456 const struct got_error *err = NULL;
1457 free(blob->read_buf);
1458 if (blob->f && fclose(blob->f) == EOF)
1459 err = got_error_from_errno("fclose");
1460 free(blob->data);
1461 free(blob);
1462 return err;
1465 void
1466 got_object_blob_rewind(struct got_blob_object *blob)
1468 if (blob->f)
1469 rewind(blob->f);
1472 char *
1473 got_object_blob_id_str(struct got_blob_object *blob, char *buf, size_t size)
1475 return got_sha1_digest_to_str(blob->id.sha1, buf, size);
1478 size_t
1479 got_object_blob_get_hdrlen(struct got_blob_object *blob)
1481 return blob->hdrlen;
1484 const uint8_t *
1485 got_object_blob_get_read_buf(struct got_blob_object *blob)
1487 return blob->read_buf;
1490 const struct got_error *
1491 got_object_blob_read_block(size_t *outlenp, struct got_blob_object *blob)
1493 size_t n;
1495 n = fread(blob->read_buf, 1, blob->blocksize, blob->f);
1496 if (n == 0 && ferror(blob->f))
1497 return got_ferror(blob->f, GOT_ERR_IO);
1498 *outlenp = n;
1499 return NULL;
1502 const struct got_error *
1503 got_object_blob_dump_to_file(off_t *filesize, int *nlines,
1504 off_t **line_offsets, FILE *outfile, struct got_blob_object *blob)
1506 const struct got_error *err = NULL;
1507 size_t n, len, hdrlen;
1508 const uint8_t *buf;
1509 int i;
1510 const int alloc_chunksz = 512;
1511 size_t nalloc = 0;
1512 off_t off = 0, total_len = 0;
1514 if (line_offsets)
1515 *line_offsets = NULL;
1516 if (filesize)
1517 *filesize = 0;
1518 if (nlines)
1519 *nlines = 0;
1521 hdrlen = got_object_blob_get_hdrlen(blob);
1522 do {
1523 err = got_object_blob_read_block(&len, blob);
1524 if (err)
1525 return err;
1526 if (len == 0)
1527 break;
1528 buf = got_object_blob_get_read_buf(blob);
1529 i = hdrlen;
1530 if (nlines) {
1531 if (line_offsets && *line_offsets == NULL) {
1532 /* Have some data but perhaps no '\n'. */
1533 *nlines = 1;
1534 nalloc = alloc_chunksz;
1535 *line_offsets = calloc(nalloc,
1536 sizeof(**line_offsets));
1537 if (*line_offsets == NULL)
1538 return got_error_from_errno("calloc");
1540 /* Skip forward over end of first line. */
1541 while (i < len) {
1542 if (buf[i] == '\n')
1543 break;
1544 i++;
1547 /* Scan '\n' offsets in remaining chunk of data. */
1548 while (i < len) {
1549 if (buf[i] != '\n') {
1550 i++;
1551 continue;
1553 (*nlines)++;
1554 if (line_offsets && nalloc < *nlines) {
1555 size_t n = *nlines + alloc_chunksz;
1556 off_t *o = recallocarray(*line_offsets,
1557 nalloc, n, sizeof(**line_offsets));
1558 if (o == NULL) {
1559 free(*line_offsets);
1560 *line_offsets = NULL;
1561 return got_error_from_errno(
1562 "recallocarray");
1564 *line_offsets = o;
1565 nalloc = n;
1567 if (line_offsets) {
1568 off = total_len + i - hdrlen + 1;
1569 (*line_offsets)[*nlines - 1] = off;
1571 i++;
1574 /* Skip blob object header first time around. */
1575 n = fwrite(buf + hdrlen, 1, len - hdrlen, outfile);
1576 if (n != len - hdrlen)
1577 return got_ferror(outfile, GOT_ERR_IO);
1578 total_len += len - hdrlen;
1579 hdrlen = 0;
1580 } while (len != 0);
1582 if (fflush(outfile) != 0)
1583 return got_error_from_errno("fflush");
1584 rewind(outfile);
1586 if (filesize)
1587 *filesize = total_len;
1589 return NULL;
1592 static const struct got_error *
1593 request_packed_tag(struct got_tag_object **tag, struct got_pack *pack,
1594 int pack_idx, struct got_object_id *id)
1596 const struct got_error *err = NULL;
1598 err = got_privsep_send_tag_req(pack->privsep_child->ibuf, -1, id,
1599 pack_idx);
1600 if (err)
1601 return err;
1603 return got_privsep_recv_tag(tag, pack->privsep_child->ibuf);
1606 static const struct got_error *
1607 read_packed_tag_privsep(struct got_tag_object **tag,
1608 struct got_pack *pack, struct got_packidx *packidx, int idx,
1609 struct got_object_id *id)
1611 const struct got_error *err = NULL;
1613 if (pack->privsep_child)
1614 return request_packed_tag(tag, pack, idx, id);
1616 err = start_pack_privsep_child(pack, packidx);
1617 if (err)
1618 return err;
1620 return request_packed_tag(tag, pack, idx, id);
1623 static const struct got_error *
1624 request_tag(struct got_tag_object **tag, struct got_repository *repo,
1625 int fd, struct got_object_id *id)
1627 const struct got_error *err = NULL;
1628 struct imsgbuf *ibuf;
1630 ibuf = repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TAG].ibuf;
1632 err = got_privsep_send_tag_req(ibuf, fd, id, -1);
1633 if (err)
1634 return err;
1636 return got_privsep_recv_tag(tag, ibuf);
1639 static const struct got_error *
1640 read_tag_privsep(struct got_tag_object **tag, int obj_fd,
1641 struct got_object_id *id, struct got_repository *repo)
1643 const struct got_error *err;
1644 int imsg_fds[2];
1645 pid_t pid;
1646 struct imsgbuf *ibuf;
1648 if (repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TAG].imsg_fd != -1)
1649 return request_tag(tag, repo, obj_fd, id);
1651 ibuf = calloc(1, sizeof(*ibuf));
1652 if (ibuf == NULL)
1653 return got_error_from_errno("calloc");
1655 if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_fds) == -1) {
1656 err = got_error_from_errno("socketpair");
1657 free(ibuf);
1658 return err;
1661 pid = fork();
1662 if (pid == -1) {
1663 err = got_error_from_errno("fork");
1664 free(ibuf);
1665 return err;
1667 else if (pid == 0) {
1668 got_privsep_exec_child(imsg_fds, GOT_PATH_PROG_READ_TAG,
1669 repo->path);
1670 /* not reached */
1673 if (close(imsg_fds[1]) == -1) {
1674 err = got_error_from_errno("close");
1675 free(ibuf);
1676 return err;
1678 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TAG].imsg_fd =
1679 imsg_fds[0];
1680 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TAG].pid = pid;
1681 imsg_init(ibuf, imsg_fds[0]);
1682 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TAG].ibuf = ibuf;
1684 return request_tag(tag, repo, obj_fd, id);
1687 static const struct got_error *
1688 open_tag(struct got_tag_object **tag, struct got_repository *repo,
1689 struct got_object_id *id, int check_cache)
1691 const struct got_error *err = NULL;
1692 struct got_packidx *packidx = NULL;
1693 int idx;
1694 char *path_packfile = NULL;
1695 struct got_object *obj = NULL;
1696 int obj_type = GOT_OBJ_TYPE_ANY;
1698 if (check_cache) {
1699 *tag = got_repo_get_cached_tag(repo, id);
1700 if (*tag != NULL) {
1701 (*tag)->refcnt++;
1702 return NULL;
1704 } else
1705 *tag = NULL;
1707 err = got_repo_search_packidx(&packidx, &idx, repo, id);
1708 if (err == NULL) {
1709 struct got_pack *pack = NULL;
1711 err = got_packidx_get_packfile_path(&path_packfile,
1712 packidx->path_packidx);
1713 if (err)
1714 return err;
1716 pack = got_repo_get_cached_pack(repo, path_packfile);
1717 if (pack == NULL) {
1718 err = got_repo_cache_pack(&pack, repo, path_packfile,
1719 packidx);
1720 if (err)
1721 goto done;
1724 /* Beware of "lightweight" tags: Check object type first. */
1725 err = read_packed_object_privsep(&obj, repo, pack, packidx,
1726 idx, id);
1727 if (err)
1728 goto done;
1729 obj_type = obj->type;
1730 got_object_close(obj);
1731 if (obj_type != GOT_OBJ_TYPE_TAG) {
1732 err = got_error(GOT_ERR_OBJ_TYPE);
1733 goto done;
1735 err = read_packed_tag_privsep(tag, pack, packidx, idx, id);
1736 } else if (err->code == GOT_ERR_NO_OBJ) {
1737 int fd;
1739 err = got_object_open_loose_fd(&fd, id, repo);
1740 if (err)
1741 return err;
1742 err = got_object_read_header_privsep(&obj, id, repo, fd);
1743 if (err)
1744 return err;
1745 obj_type = obj->type;
1746 got_object_close(obj);
1747 if (obj_type != GOT_OBJ_TYPE_TAG)
1748 return got_error(GOT_ERR_OBJ_TYPE);
1750 err = got_object_open_loose_fd(&fd, id, repo);
1751 if (err)
1752 return err;
1753 err = read_tag_privsep(tag, fd, id, repo);
1756 if (err == NULL) {
1757 (*tag)->refcnt++;
1758 err = got_repo_cache_tag(repo, id, *tag);
1760 done:
1761 free(path_packfile);
1762 return err;
1765 const struct got_error *
1766 got_object_open_as_tag(struct got_tag_object **tag,
1767 struct got_repository *repo, struct got_object_id *id)
1769 *tag = got_repo_get_cached_tag(repo, id);
1770 if (*tag != NULL) {
1771 (*tag)->refcnt++;
1772 return NULL;
1775 return open_tag(tag, repo, id, 0);
1778 const struct got_error *
1779 got_object_tag_open(struct got_tag_object **tag,
1780 struct got_repository *repo, struct got_object *obj)
1782 return open_tag(tag, repo, got_object_get_id(obj), 1);
1785 const char *
1786 got_object_tag_get_name(struct got_tag_object *tag)
1788 return tag->tag;
1791 int
1792 got_object_tag_get_object_type(struct got_tag_object *tag)
1794 return tag->obj_type;
1797 struct got_object_id *
1798 got_object_tag_get_object_id(struct got_tag_object *tag)
1800 return &tag->id;
1803 time_t
1804 got_object_tag_get_tagger_time(struct got_tag_object *tag)
1806 return tag->tagger_time;
1809 time_t
1810 got_object_tag_get_tagger_gmtoff(struct got_tag_object *tag)
1812 return tag->tagger_gmtoff;
1815 const char *
1816 got_object_tag_get_tagger(struct got_tag_object *tag)
1818 return tag->tagger;
1821 const char *
1822 got_object_tag_get_message(struct got_tag_object *tag)
1824 return tag->tagmsg;
1827 static struct got_tree_entry *
1828 find_entry_by_name(struct got_tree_object *tree, const char *name, size_t len)
1830 int i;
1832 /* Note that tree entries are sorted in strncmp() order. */
1833 for (i = 0; i < tree->nentries; i++) {
1834 struct got_tree_entry *te = &tree->entries[i];
1835 int cmp = strncmp(te->name, name, len);
1836 if (cmp < 0)
1837 continue;
1838 if (cmp > 0)
1839 break;
1840 if (te->name[len] == '\0')
1841 return te;
1843 return NULL;
1846 struct got_tree_entry *
1847 got_object_tree_find_entry(struct got_tree_object *tree, const char *name)
1849 return find_entry_by_name(tree, name, strlen(name));
1852 const struct got_error *
1853 got_object_tree_find_path(struct got_object_id **id, mode_t *mode,
1854 struct got_repository *repo, struct got_tree_object *tree,
1855 const char *path)
1857 const struct got_error *err = NULL;
1858 struct got_tree_object *subtree = NULL;
1859 struct got_tree_entry *te = NULL;
1860 const char *seg, *s;
1861 size_t seglen;
1863 *id = NULL;
1865 s = path;
1866 while (s[0] == '/')
1867 s++;
1868 seg = s;
1869 seglen = 0;
1870 subtree = tree;
1871 while (*s) {
1872 struct got_tree_object *next_tree;
1874 if (*s != '/') {
1875 s++;
1876 seglen++;
1877 if (*s)
1878 continue;
1881 te = find_entry_by_name(subtree, seg, seglen);
1882 if (te == NULL) {
1883 err = got_error_path(path, GOT_ERR_NO_TREE_ENTRY);
1884 goto done;
1887 if (*s == '\0')
1888 break;
1890 seg = s + 1;
1891 seglen = 0;
1892 s++;
1893 if (*s) {
1894 err = got_object_open_as_tree(&next_tree, repo,
1895 &te->id);
1896 te = NULL;
1897 if (err)
1898 goto done;
1899 if (subtree != tree)
1900 got_object_tree_close(subtree);
1901 subtree = next_tree;
1905 if (te) {
1906 *id = got_object_id_dup(&te->id);
1907 if (*id == NULL)
1908 return got_error_from_errno("got_object_id_dup");
1909 if (mode)
1910 *mode = te->mode;
1911 } else
1912 err = got_error_path(path, GOT_ERR_NO_TREE_ENTRY);
1913 done:
1914 if (subtree && subtree != tree)
1915 got_object_tree_close(subtree);
1916 return err;
1918 const struct got_error *
1919 got_object_id_by_path(struct got_object_id **id, struct got_repository *repo,
1920 struct got_object_id *commit_id, const char *path)
1922 const struct got_error *err = NULL;
1923 struct got_commit_object *commit = NULL;
1924 struct got_tree_object *tree = NULL;
1926 *id = NULL;
1928 err = got_object_open_as_commit(&commit, repo, commit_id);
1929 if (err)
1930 goto done;
1932 /* Handle opening of root of commit's tree. */
1933 if (got_path_is_root_dir(path)) {
1934 *id = got_object_id_dup(commit->tree_id);
1935 if (*id == NULL)
1936 err = got_error_from_errno("got_object_id_dup");
1937 } else {
1938 err = got_object_open_as_tree(&tree, repo, commit->tree_id);
1939 if (err)
1940 goto done;
1941 err = got_object_tree_find_path(id, NULL, repo, tree, path);
1943 done:
1944 if (commit)
1945 got_object_commit_close(commit);
1946 if (tree)
1947 got_object_tree_close(tree);
1948 return err;
1952 * Normalize file mode bits to avoid false positive tree entry differences
1953 * in case tree entries have unexpected mode bits set.
1955 static mode_t
1956 normalize_mode_for_comparison(mode_t mode)
1959 * For directories, the only relevant bit is the IFDIR bit.
1960 * This allows us to detect paths changing from a directory
1961 * to a file and vice versa.
1963 if (S_ISDIR(mode))
1964 return mode & S_IFDIR;
1967 * For symlinks, the only relevant bit is the IFLNK bit.
1968 * This allows us to detect paths changing from a symlinks
1969 * to a file or directory and vice versa.
1971 if (S_ISLNK(mode))
1972 return mode & S_IFLNK;
1974 /* For files, the only change we care about is the executable bit. */
1975 return mode & S_IXUSR;
1978 const struct got_error *
1979 got_object_tree_path_changed(int *changed,
1980 struct got_tree_object *tree01, struct got_tree_object *tree02,
1981 const char *path, struct got_repository *repo)
1983 const struct got_error *err = NULL;
1984 struct got_tree_object *tree1 = NULL, *tree2 = NULL;
1985 struct got_tree_entry *te1 = NULL, *te2 = NULL;
1986 const char *seg, *s;
1987 size_t seglen;
1989 *changed = 0;
1991 /* We not do support comparing the root path. */
1992 if (got_path_is_root_dir(path))
1993 return got_error_path(path, GOT_ERR_BAD_PATH);
1995 tree1 = tree01;
1996 tree2 = tree02;
1997 s = path;
1998 while (*s == '/')
1999 s++;
2000 seg = s;
2001 seglen = 0;
2002 while (*s) {
2003 struct got_tree_object *next_tree1, *next_tree2;
2004 mode_t mode1, mode2;
2006 if (*s != '/') {
2007 s++;
2008 seglen++;
2009 if (*s)
2010 continue;
2013 te1 = find_entry_by_name(tree1, seg, seglen);
2014 if (te1 == NULL) {
2015 err = got_error(GOT_ERR_NO_OBJ);
2016 goto done;
2019 if (tree2)
2020 te2 = find_entry_by_name(tree2, seg, seglen);
2022 if (te2) {
2023 mode1 = normalize_mode_for_comparison(te1->mode);
2024 mode2 = normalize_mode_for_comparison(te2->mode);
2025 if (mode1 != mode2) {
2026 *changed = 1;
2027 goto done;
2030 if (got_object_id_cmp(&te1->id, &te2->id) == 0) {
2031 *changed = 0;
2032 goto done;
2036 if (*s == '\0') { /* final path element */
2037 *changed = 1;
2038 goto done;
2041 seg = s + 1;
2042 s++;
2043 seglen = 0;
2044 if (*s) {
2045 err = got_object_open_as_tree(&next_tree1, repo,
2046 &te1->id);
2047 te1 = NULL;
2048 if (err)
2049 goto done;
2050 if (tree1 != tree01)
2051 got_object_tree_close(tree1);
2052 tree1 = next_tree1;
2054 if (te2) {
2055 err = got_object_open_as_tree(&next_tree2, repo,
2056 &te2->id);
2057 te2 = NULL;
2058 if (err)
2059 goto done;
2060 if (tree2 != tree02)
2061 got_object_tree_close(tree2);
2062 tree2 = next_tree2;
2063 } else if (tree2) {
2064 if (tree2 != tree02)
2065 got_object_tree_close(tree2);
2066 tree2 = NULL;
2070 done:
2071 if (tree1 && tree1 != tree01)
2072 got_object_tree_close(tree1);
2073 if (tree2 && tree2 != tree02)
2074 got_object_tree_close(tree2);
2075 return err;
2078 const struct got_error *
2079 got_object_tree_entry_dup(struct got_tree_entry **new_te,
2080 struct got_tree_entry *te)
2082 const struct got_error *err = NULL;
2084 *new_te = calloc(1, sizeof(**new_te));
2085 if (*new_te == NULL)
2086 return got_error_from_errno("calloc");
2088 (*new_te)->mode = te->mode;
2089 memcpy((*new_te)->name, te->name, sizeof((*new_te)->name));
2090 memcpy(&(*new_te)->id, &te->id, sizeof((*new_te)->id));
2091 return err;
2094 int
2095 got_object_tree_entry_is_submodule(struct got_tree_entry *te)
2097 return (te->mode & S_IFMT) == (S_IFDIR | S_IFLNK);
2100 int
2101 got_object_tree_entry_is_symlink(struct got_tree_entry *te)
2103 /* S_IFDIR check avoids confusing symlinks with submodules. */
2104 return ((te->mode & (S_IFDIR | S_IFLNK)) == S_IFLNK);
2107 static const struct got_error *
2108 resolve_symlink(char **link_target, const char *path,
2109 struct got_object_id *commit_id, struct got_repository *repo)
2111 const struct got_error *err = NULL;
2112 char buf[PATH_MAX];
2113 char *name, *parent_path = NULL;
2114 struct got_object_id *tree_obj_id = NULL;
2115 struct got_tree_object *tree = NULL;
2116 struct got_tree_entry *te = NULL;
2118 *link_target = NULL;
2120 if (strlcpy(buf, path, sizeof(buf)) >= sizeof(buf))
2121 return got_error(GOT_ERR_NO_SPACE);
2123 name = basename(buf);
2124 if (name == NULL)
2125 return got_error_from_errno2("basename", path);
2127 err = got_path_dirname(&parent_path, path);
2128 if (err)
2129 return err;
2131 err = got_object_id_by_path(&tree_obj_id, repo, commit_id,
2132 parent_path);
2133 if (err) {
2134 if (err->code == GOT_ERR_NO_TREE_ENTRY) {
2135 /* Display the complete path in error message. */
2136 err = got_error_path(path, err->code);
2138 goto done;
2141 err = got_object_open_as_tree(&tree, repo, tree_obj_id);
2142 if (err)
2143 goto done;
2145 te = got_object_tree_find_entry(tree, name);
2146 if (te == NULL) {
2147 err = got_error_path(path, GOT_ERR_NO_TREE_ENTRY);
2148 goto done;
2151 if (got_object_tree_entry_is_symlink(te)) {
2152 err = got_tree_entry_get_symlink_target(link_target, te, repo);
2153 if (err)
2154 goto done;
2155 if (!got_path_is_absolute(*link_target)) {
2156 char *abspath;
2157 if (asprintf(&abspath, "%s/%s", parent_path,
2158 *link_target) == -1) {
2159 err = got_error_from_errno("asprintf");
2160 goto done;
2162 free(*link_target);
2163 *link_target = malloc(PATH_MAX);
2164 if (*link_target == NULL) {
2165 err = got_error_from_errno("malloc");
2166 goto done;
2168 err = got_canonpath(abspath, *link_target, PATH_MAX);
2169 free(abspath);
2170 if (err)
2171 goto done;
2174 done:
2175 free(tree_obj_id);
2176 if (tree)
2177 got_object_tree_close(tree);
2178 if (err) {
2179 free(*link_target);
2180 *link_target = NULL;
2182 return err;
2185 const struct got_error *
2186 got_object_resolve_symlinks(char **link_target, const char *path,
2187 struct got_object_id *commit_id, struct got_repository *repo)
2189 const struct got_error *err = NULL;
2190 char *next_target = NULL;
2191 int max_recursion = 40; /* matches Git */
2193 *link_target = NULL;
2195 do {
2196 err = resolve_symlink(&next_target,
2197 *link_target ? *link_target : path, commit_id, repo);
2198 if (err)
2199 break;
2200 if (next_target) {
2201 free(*link_target);
2202 if (--max_recursion == 0) {
2203 err = got_error_path(path, GOT_ERR_RECURSION);
2204 *link_target = NULL;
2205 break;
2207 *link_target = next_target;
2209 } while (next_target);
2211 return err;
2214 const struct got_error *
2215 got_traverse_packed_commits(struct got_object_id_queue *traversed_commits,
2216 struct got_object_id *commit_id, const char *path,
2217 struct got_repository *repo)
2219 const struct got_error *err = NULL;
2220 struct got_pack *pack = NULL;
2221 struct got_packidx *packidx = NULL;
2222 char *path_packfile = NULL;
2223 struct got_commit_object *changed_commit = NULL;
2224 struct got_object_id *changed_commit_id = NULL;
2225 int idx;
2227 err = got_repo_search_packidx(&packidx, &idx, repo, commit_id);
2228 if (err) {
2229 if (err->code != GOT_ERR_NO_OBJ)
2230 return err;
2231 return NULL;
2234 err = got_packidx_get_packfile_path(&path_packfile,
2235 packidx->path_packidx);
2236 if (err)
2237 return err;
2239 pack = got_repo_get_cached_pack(repo, path_packfile);
2240 if (pack == NULL) {
2241 err = got_repo_cache_pack(&pack, repo, path_packfile, packidx);
2242 if (err)
2243 goto done;
2246 if (pack->privsep_child == NULL) {
2247 err = start_pack_privsep_child(pack, packidx);
2248 if (err)
2249 goto done;
2252 err = got_privsep_send_commit_traversal_request(
2253 pack->privsep_child->ibuf, commit_id, idx, path);
2254 if (err)
2255 goto done;
2257 err = got_privsep_recv_traversed_commits(&changed_commit,
2258 &changed_commit_id, traversed_commits, pack->privsep_child->ibuf);
2259 if (err)
2260 goto done;
2262 if (changed_commit) {
2264 * Cache the commit in which the path was changed.
2265 * This commit might be opened again soon.
2267 changed_commit->refcnt++;
2268 err = got_repo_cache_commit(repo, changed_commit_id,
2269 changed_commit);
2270 got_object_commit_close(changed_commit);
2272 done:
2273 free(path_packfile);
2274 free(changed_commit_id);
2275 return err;