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 const struct got_error *
530 got_object_raw_open(struct got_raw_object **obj, struct got_repository *repo,
531 struct got_object_id *id, size_t blocksize)
533 const struct got_error *err = NULL;
534 struct got_packidx *packidx = NULL;
535 int idx;
536 uint8_t *outbuf = NULL;
537 int outfd = -1;
538 off_t size = 0;
539 size_t hdrlen = 0;
540 char *path_packfile = NULL;
542 *obj = NULL;
544 outfd = got_opentempfd();
545 if (outfd == -1)
546 return got_error_from_errno("got_opentempfd");
548 err = got_repo_search_packidx(&packidx, &idx, repo, id);
549 if (err == NULL) {
550 struct got_pack *pack = NULL;
552 err = got_packidx_get_packfile_path(&path_packfile,
553 packidx->path_packidx);
554 if (err)
555 goto done;
557 pack = got_repo_get_cached_pack(repo, path_packfile);
558 if (pack == NULL) {
559 err = got_repo_cache_pack(&pack, repo, path_packfile,
560 packidx);
561 if (err)
562 goto done;
564 err = read_packed_object_raw_privsep(&outbuf, &size, &hdrlen,
565 outfd, pack, packidx, idx, id);
566 } else if (err->code == GOT_ERR_NO_OBJ) {
567 int fd;
569 err = got_object_open_loose_fd(&fd, id, repo);
570 if (err)
571 goto done;
572 err = read_object_raw_privsep(&outbuf, &size, &hdrlen, outfd,
573 id, repo, fd);
576 *obj = calloc(1, sizeof(**obj));
577 if (*obj == NULL) {
578 err = got_error_from_errno("calloc");
579 goto done;
582 (*obj)->read_buf = malloc(blocksize);
583 if ((*obj)->read_buf == NULL) {
584 err = got_error_from_errno("malloc");
585 goto done;
588 if (outbuf) {
589 if (close(outfd) == -1) {
590 err = got_error_from_errno("close");
591 goto done;
593 outfd = -1;
594 (*obj)->f = fmemopen(outbuf, hdrlen + size, "r");
595 if ((*obj)->f == NULL) {
596 err = got_error_from_errno("fdopen");
597 goto done;
599 (*obj)->data = outbuf;
600 } else {
601 struct stat sb;
602 if (fstat(outfd, &sb) == -1) {
603 err = got_error_from_errno("fstat");
604 goto done;
607 if (sb.st_size != hdrlen + size) {
608 err = got_error(GOT_ERR_PRIVSEP_LEN);
609 goto done;
612 (*obj)->f = fdopen(outfd, "r");
613 if ((*obj)->f == NULL) {
614 err = got_error_from_errno("fdopen");
615 goto done;
617 outfd = -1;
618 (*obj)->data = NULL;
620 (*obj)->hdrlen = hdrlen;
621 (*obj)->size = size;
622 (*obj)->blocksize = blocksize;
623 done:
624 free(path_packfile);
625 if (err) {
626 if (*obj) {
627 got_object_raw_close(*obj);
628 *obj = NULL;
630 if (outfd != -1)
631 close(outfd);
632 free(outbuf);
634 return err;
637 void
638 got_object_raw_rewind(struct got_raw_object *obj)
640 if (obj->f)
641 rewind(obj->f);
644 size_t
645 got_object_raw_get_hdrlen(struct got_raw_object *obj)
647 return obj->hdrlen;
650 const uint8_t *
651 got_object_raw_get_read_buf(struct got_raw_object *obj)
653 return obj->read_buf;
656 const struct got_error *
657 got_object_raw_read_block(size_t *outlenp, struct got_raw_object *obj)
659 size_t n;
661 n = fread(obj->read_buf, 1, obj->blocksize, obj->f);
662 if (n == 0 && ferror(obj->f))
663 return got_ferror(obj->f, GOT_ERR_IO);
664 *outlenp = n;
665 return NULL;
668 const struct got_error *
669 got_object_raw_close(struct got_raw_object *obj)
671 const struct got_error *err = NULL;
673 free(obj->read_buf);
674 if (obj->f != NULL && fclose(obj->f) == EOF && err == NULL)
675 err = got_error_from_errno("fclose");
676 free(obj->data);
677 free(obj);
678 return err;
681 const struct got_error *
682 got_object_open_by_id_str(struct got_object **obj, struct got_repository *repo,
683 const char *id_str)
685 struct got_object_id id;
687 if (!got_parse_sha1_digest(id.sha1, id_str))
688 return got_error_path(id_str, GOT_ERR_BAD_OBJ_ID_STR);
690 return got_object_open(obj, repo, &id);
693 const struct got_error *
694 got_object_resolve_id_str(struct got_object_id **id,
695 struct got_repository *repo, const char *id_str)
697 const struct got_error *err = NULL;
698 struct got_object *obj;
700 err = got_object_open_by_id_str(&obj, repo, id_str);
701 if (err)
702 return err;
704 *id = got_object_id_dup(got_object_get_id(obj));
705 got_object_close(obj);
706 if (*id == NULL)
707 return got_error_from_errno("got_object_id_dup");
709 return NULL;
712 static const struct got_error *
713 request_packed_commit(struct got_commit_object **commit, struct got_pack *pack,
714 int pack_idx, struct got_object_id *id)
716 const struct got_error *err = NULL;
718 err = got_privsep_send_commit_req(pack->privsep_child->ibuf, -1, id,
719 pack_idx);
720 if (err)
721 return err;
723 err = got_privsep_recv_commit(commit, pack->privsep_child->ibuf);
724 if (err)
725 return err;
727 (*commit)->flags |= GOT_COMMIT_FLAG_PACKED;
728 return NULL;
731 static const struct got_error *
732 read_packed_commit_privsep(struct got_commit_object **commit,
733 struct got_pack *pack, struct got_packidx *packidx, int idx,
734 struct got_object_id *id)
736 const struct got_error *err = NULL;
738 if (pack->privsep_child)
739 return request_packed_commit(commit, pack, idx, id);
741 err = start_pack_privsep_child(pack, packidx);
742 if (err)
743 return err;
745 return request_packed_commit(commit, pack, idx, id);
748 static const struct got_error *
749 request_commit(struct got_commit_object **commit, struct got_repository *repo,
750 int fd, struct got_object_id *id)
752 const struct got_error *err = NULL;
753 struct imsgbuf *ibuf;
755 ibuf = repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_COMMIT].ibuf;
757 err = got_privsep_send_commit_req(ibuf, fd, id, -1);
758 if (err)
759 return err;
761 return got_privsep_recv_commit(commit, ibuf);
764 static const struct got_error *
765 read_commit_privsep(struct got_commit_object **commit, int obj_fd,
766 struct got_object_id *id, struct got_repository *repo)
768 const struct got_error *err;
769 int imsg_fds[2];
770 pid_t pid;
771 struct imsgbuf *ibuf;
773 if (repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_COMMIT].imsg_fd != -1)
774 return request_commit(commit, repo, obj_fd, id);
776 ibuf = calloc(1, sizeof(*ibuf));
777 if (ibuf == NULL)
778 return got_error_from_errno("calloc");
780 if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_fds) == -1) {
781 err = got_error_from_errno("socketpair");
782 free(ibuf);
783 return err;
786 pid = fork();
787 if (pid == -1) {
788 err = got_error_from_errno("fork");
789 free(ibuf);
790 return err;
792 else if (pid == 0) {
793 got_privsep_exec_child(imsg_fds, GOT_PATH_PROG_READ_COMMIT,
794 repo->path);
795 /* not reached */
798 if (close(imsg_fds[1]) == -1) {
799 err = got_error_from_errno("close");
800 free(ibuf);
801 return err;
803 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_COMMIT].imsg_fd =
804 imsg_fds[0];
805 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_COMMIT].pid = pid;
806 imsg_init(ibuf, imsg_fds[0]);
807 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_COMMIT].ibuf = ibuf;
809 return request_commit(commit, repo, obj_fd, id);
813 static const struct got_error *
814 open_commit(struct got_commit_object **commit,
815 struct got_repository *repo, struct got_object_id *id, int check_cache)
817 const struct got_error *err = NULL;
818 struct got_packidx *packidx = NULL;
819 int idx;
820 char *path_packfile = NULL;
822 if (check_cache) {
823 *commit = got_repo_get_cached_commit(repo, id);
824 if (*commit != NULL) {
825 (*commit)->refcnt++;
826 return NULL;
828 } else
829 *commit = NULL;
831 err = got_repo_search_packidx(&packidx, &idx, repo, id);
832 if (err == NULL) {
833 struct got_pack *pack = NULL;
835 err = got_packidx_get_packfile_path(&path_packfile,
836 packidx->path_packidx);
837 if (err)
838 return err;
840 pack = got_repo_get_cached_pack(repo, path_packfile);
841 if (pack == NULL) {
842 err = got_repo_cache_pack(&pack, repo, path_packfile,
843 packidx);
844 if (err)
845 goto done;
847 err = read_packed_commit_privsep(commit, pack,
848 packidx, idx, id);
849 } else if (err->code == GOT_ERR_NO_OBJ) {
850 int fd;
852 err = got_object_open_loose_fd(&fd, id, repo);
853 if (err)
854 return err;
855 err = read_commit_privsep(commit, fd, id, repo);
858 if (err == NULL) {
859 (*commit)->refcnt++;
860 err = got_repo_cache_commit(repo, id, *commit);
862 done:
863 free(path_packfile);
864 return err;
867 const struct got_error *
868 got_object_open_as_commit(struct got_commit_object **commit,
869 struct got_repository *repo, struct got_object_id *id)
871 *commit = got_repo_get_cached_commit(repo, id);
872 if (*commit != NULL) {
873 (*commit)->refcnt++;
874 return NULL;
877 return open_commit(commit, repo, id, 0);
880 const struct got_error *
881 got_object_commit_open(struct got_commit_object **commit,
882 struct got_repository *repo, struct got_object *obj)
884 return open_commit(commit, repo, got_object_get_id(obj), 1);
887 const struct got_error *
888 got_object_qid_alloc(struct got_object_qid **qid, struct got_object_id *id)
890 const struct got_error *err = NULL;
892 *qid = calloc(1, sizeof(**qid));
893 if (*qid == NULL)
894 return got_error_from_errno("calloc");
896 (*qid)->id = got_object_id_dup(id);
897 if ((*qid)->id == NULL) {
898 err = got_error_from_errno("got_object_id_dup");
899 got_object_qid_free(*qid);
900 *qid = NULL;
901 return err;
904 return NULL;
907 const struct got_error *
908 got_object_id_queue_copy(const struct got_object_id_queue *src,
909 struct got_object_id_queue *dest)
911 const struct got_error *err;
912 struct got_object_qid *qid;
914 STAILQ_FOREACH(qid, src, entry) {
915 struct got_object_qid *new;
916 /*
917 * Deep-copy the object ID only. Let the caller deal
918 * with setting up the new->data pointer if needed.
919 */
920 err = got_object_qid_alloc(&new, qid->id);
921 if (err) {
922 got_object_id_queue_free(dest);
923 return err;
925 STAILQ_INSERT_TAIL(dest, new, entry);
928 return NULL;
931 static const struct got_error *
932 request_packed_tree(struct got_tree_object **tree, struct got_pack *pack,
933 int pack_idx, struct got_object_id *id)
935 const struct got_error *err = NULL;
937 err = got_privsep_send_tree_req(pack->privsep_child->ibuf, -1, id,
938 pack_idx);
939 if (err)
940 return err;
942 return got_privsep_recv_tree(tree, pack->privsep_child->ibuf);
945 static const struct got_error *
946 read_packed_tree_privsep(struct got_tree_object **tree,
947 struct got_pack *pack, struct got_packidx *packidx, int idx,
948 struct got_object_id *id)
950 const struct got_error *err = NULL;
952 if (pack->privsep_child)
953 return request_packed_tree(tree, pack, idx, id);
955 err = start_pack_privsep_child(pack, packidx);
956 if (err)
957 return err;
959 return request_packed_tree(tree, pack, idx, id);
962 static const struct got_error *
963 request_tree(struct got_tree_object **tree, struct got_repository *repo,
964 int fd, struct got_object_id *id)
966 const struct got_error *err = NULL;
967 struct imsgbuf *ibuf;
969 ibuf = repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TREE].ibuf;
971 err = got_privsep_send_tree_req(ibuf, fd, id, -1);
972 if (err)
973 return err;
975 return got_privsep_recv_tree(tree, ibuf);
978 const struct got_error *
979 read_tree_privsep(struct got_tree_object **tree, int obj_fd,
980 struct got_object_id *id, struct got_repository *repo)
982 const struct got_error *err;
983 int imsg_fds[2];
984 pid_t pid;
985 struct imsgbuf *ibuf;
987 if (repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TREE].imsg_fd != -1)
988 return request_tree(tree, repo, obj_fd, id);
990 ibuf = calloc(1, sizeof(*ibuf));
991 if (ibuf == NULL)
992 return got_error_from_errno("calloc");
994 if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_fds) == -1) {
995 err = got_error_from_errno("socketpair");
996 free(ibuf);
997 return err;
1000 pid = fork();
1001 if (pid == -1) {
1002 err = got_error_from_errno("fork");
1003 free(ibuf);
1004 return err;
1006 else if (pid == 0) {
1007 got_privsep_exec_child(imsg_fds, GOT_PATH_PROG_READ_TREE,
1008 repo->path);
1009 /* not reached */
1012 if (close(imsg_fds[1]) == -1) {
1013 err = got_error_from_errno("close");
1014 free(ibuf);
1015 return err;
1017 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TREE].imsg_fd =
1018 imsg_fds[0];
1019 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TREE].pid = pid;
1020 imsg_init(ibuf, imsg_fds[0]);
1021 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TREE].ibuf = ibuf;
1024 return request_tree(tree, repo, obj_fd, id);
1027 static const struct got_error *
1028 open_tree(struct got_tree_object **tree, struct got_repository *repo,
1029 struct got_object_id *id, int check_cache)
1031 const struct got_error *err = NULL;
1032 struct got_packidx *packidx = NULL;
1033 int idx;
1034 char *path_packfile = NULL;
1036 if (check_cache) {
1037 *tree = got_repo_get_cached_tree(repo, id);
1038 if (*tree != NULL) {
1039 (*tree)->refcnt++;
1040 return NULL;
1042 } else
1043 *tree = NULL;
1045 err = got_repo_search_packidx(&packidx, &idx, repo, id);
1046 if (err == NULL) {
1047 struct got_pack *pack = NULL;
1049 err = got_packidx_get_packfile_path(&path_packfile,
1050 packidx->path_packidx);
1051 if (err)
1052 return err;
1054 pack = got_repo_get_cached_pack(repo, path_packfile);
1055 if (pack == NULL) {
1056 err = got_repo_cache_pack(&pack, repo, path_packfile,
1057 packidx);
1058 if (err)
1059 goto done;
1061 err = read_packed_tree_privsep(tree, pack,
1062 packidx, idx, id);
1063 } else if (err->code == GOT_ERR_NO_OBJ) {
1064 int fd;
1066 err = got_object_open_loose_fd(&fd, id, repo);
1067 if (err)
1068 return err;
1069 err = read_tree_privsep(tree, fd, id, repo);
1072 if (err == NULL) {
1073 (*tree)->refcnt++;
1074 err = got_repo_cache_tree(repo, id, *tree);
1076 done:
1077 free(path_packfile);
1078 return err;
1081 const struct got_error *
1082 got_object_open_as_tree(struct got_tree_object **tree,
1083 struct got_repository *repo, struct got_object_id *id)
1085 *tree = got_repo_get_cached_tree(repo, id);
1086 if (*tree != NULL) {
1087 (*tree)->refcnt++;
1088 return NULL;
1091 return open_tree(tree, repo, id, 0);
1094 const struct got_error *
1095 got_object_tree_open(struct got_tree_object **tree,
1096 struct got_repository *repo, struct got_object *obj)
1098 return open_tree(tree, repo, got_object_get_id(obj), 1);
1101 int
1102 got_object_tree_get_nentries(struct got_tree_object *tree)
1104 return tree->nentries;
1107 struct got_tree_entry *
1108 got_object_tree_get_first_entry(struct got_tree_object *tree)
1110 return got_object_tree_get_entry(tree, 0);
1113 struct got_tree_entry *
1114 got_object_tree_get_last_entry(struct got_tree_object *tree)
1116 return got_object_tree_get_entry(tree, tree->nentries - 1);
1119 struct got_tree_entry *
1120 got_object_tree_get_entry(struct got_tree_object *tree, int i)
1122 if (i < 0 || i >= tree->nentries)
1123 return NULL;
1124 return &tree->entries[i];
1127 mode_t
1128 got_tree_entry_get_mode(struct got_tree_entry *te)
1130 return te->mode;
1133 const char *
1134 got_tree_entry_get_name(struct got_tree_entry *te)
1136 return &te->name[0];
1139 struct got_object_id *
1140 got_tree_entry_get_id(struct got_tree_entry *te)
1142 return &te->id;
1145 const struct got_error *
1146 got_object_blob_read_to_str(char **s, struct got_blob_object *blob)
1148 const struct got_error *err = NULL;
1149 size_t len, totlen, hdrlen, offset;
1151 *s = NULL;
1153 hdrlen = got_object_blob_get_hdrlen(blob);
1154 totlen = 0;
1155 offset = 0;
1156 do {
1157 char *p;
1159 err = got_object_blob_read_block(&len, blob);
1160 if (err)
1161 return err;
1163 if (len == 0)
1164 break;
1166 totlen += len - hdrlen;
1167 p = realloc(*s, totlen + 1);
1168 if (p == NULL) {
1169 err = got_error_from_errno("realloc");
1170 free(*s);
1171 *s = NULL;
1172 return err;
1174 *s = p;
1175 /* Skip blob object header first time around. */
1176 memcpy(*s + offset,
1177 got_object_blob_get_read_buf(blob) + hdrlen, len - hdrlen);
1178 hdrlen = 0;
1179 offset = totlen;
1180 } while (len > 0);
1182 (*s)[totlen] = '\0';
1183 return NULL;
1186 const struct got_error *
1187 got_tree_entry_get_symlink_target(char **link_target, struct got_tree_entry *te,
1188 struct got_repository *repo)
1190 const struct got_error *err = NULL;
1191 struct got_blob_object *blob = NULL;
1193 *link_target = NULL;
1195 if (!got_object_tree_entry_is_symlink(te))
1196 return got_error(GOT_ERR_TREE_ENTRY_TYPE);
1198 err = got_object_open_as_blob(&blob, repo,
1199 got_tree_entry_get_id(te), PATH_MAX);
1200 if (err)
1201 return err;
1203 err = got_object_blob_read_to_str(link_target, blob);
1204 got_object_blob_close(blob);
1205 if (err) {
1206 free(*link_target);
1207 *link_target = NULL;
1209 return err;
1212 int
1213 got_tree_entry_get_index(struct got_tree_entry *te)
1215 return te->idx;
1218 struct got_tree_entry *
1219 got_tree_entry_get_next(struct got_tree_object *tree,
1220 struct got_tree_entry *te)
1222 return got_object_tree_get_entry(tree, te->idx + 1);
1225 struct got_tree_entry *
1226 got_tree_entry_get_prev(struct got_tree_object *tree,
1227 struct got_tree_entry *te)
1229 return got_object_tree_get_entry(tree, te->idx - 1);
1232 static const struct got_error *
1233 request_packed_blob(uint8_t **outbuf, size_t *size, size_t *hdrlen, int outfd,
1234 struct got_pack *pack, struct got_packidx *packidx, int idx,
1235 struct got_object_id *id)
1237 const struct got_error *err = NULL;
1238 int outfd_child;
1239 int basefd, accumfd; /* temporary files for delta application */
1241 basefd = got_opentempfd();
1242 if (basefd == -1)
1243 return got_error_from_errno("got_opentempfd");
1244 accumfd = got_opentempfd();
1245 if (accumfd == -1)
1246 return got_error_from_errno("got_opentempfd");
1248 outfd_child = dup(outfd);
1249 if (outfd_child == -1)
1250 return got_error_from_errno("dup");
1252 err = got_privsep_send_blob_req(pack->privsep_child->ibuf, -1, id, idx);
1253 if (err)
1254 return err;
1256 err = got_privsep_send_blob_outfd(pack->privsep_child->ibuf,
1257 outfd_child);
1258 if (err) {
1259 close(basefd);
1260 close(accumfd);
1261 return err;
1264 err = got_privsep_send_tmpfd(pack->privsep_child->ibuf,
1265 basefd);
1266 if (err) {
1267 close(accumfd);
1268 return err;
1271 err = got_privsep_send_tmpfd(pack->privsep_child->ibuf,
1272 accumfd);
1273 if (err)
1274 return err;
1276 err = got_privsep_recv_blob(outbuf, size, hdrlen,
1277 pack->privsep_child->ibuf);
1278 if (err)
1279 return err;
1281 if (lseek(outfd, SEEK_SET, 0) == -1)
1282 err = got_error_from_errno("lseek");
1284 return err;
1287 static const struct got_error *
1288 read_packed_blob_privsep(uint8_t **outbuf, size_t *size, size_t *hdrlen,
1289 int outfd, struct got_pack *pack, struct got_packidx *packidx, int idx,
1290 struct got_object_id *id)
1292 const struct got_error *err = NULL;
1294 if (pack->privsep_child == NULL) {
1295 err = start_pack_privsep_child(pack, packidx);
1296 if (err)
1297 return err;
1300 return request_packed_blob(outbuf, size, hdrlen, outfd, pack, packidx,
1301 idx, id);
1304 static const struct got_error *
1305 request_blob(uint8_t **outbuf, size_t *size, size_t *hdrlen, int outfd,
1306 int infd, struct got_object_id *id, struct imsgbuf *ibuf)
1308 const struct got_error *err = NULL;
1309 int outfd_child;
1311 outfd_child = dup(outfd);
1312 if (outfd_child == -1)
1313 return got_error_from_errno("dup");
1315 err = got_privsep_send_blob_req(ibuf, infd, id, -1);
1316 if (err)
1317 return err;
1319 err = got_privsep_send_blob_outfd(ibuf, outfd_child);
1320 if (err)
1321 return err;
1323 err = got_privsep_recv_blob(outbuf, size, hdrlen, ibuf);
1324 if (err)
1325 return err;
1327 if (lseek(outfd, SEEK_SET, 0) == -1)
1328 return got_error_from_errno("lseek");
1330 return err;
1333 static const struct got_error *
1334 read_blob_privsep(uint8_t **outbuf, size_t *size, size_t *hdrlen,
1335 int outfd, int infd, struct got_object_id *id, struct got_repository *repo)
1337 const struct got_error *err;
1338 int imsg_fds[2];
1339 pid_t pid;
1340 struct imsgbuf *ibuf;
1342 if (repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_BLOB].imsg_fd != -1) {
1343 ibuf = repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_BLOB].ibuf;
1344 return request_blob(outbuf, size, hdrlen, outfd, infd, id,
1345 ibuf);
1348 ibuf = calloc(1, sizeof(*ibuf));
1349 if (ibuf == NULL)
1350 return got_error_from_errno("calloc");
1352 if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_fds) == -1) {
1353 err = got_error_from_errno("socketpair");
1354 free(ibuf);
1355 return err;
1358 pid = fork();
1359 if (pid == -1) {
1360 err = got_error_from_errno("fork");
1361 free(ibuf);
1362 return err;
1364 else if (pid == 0) {
1365 got_privsep_exec_child(imsg_fds, GOT_PATH_PROG_READ_BLOB,
1366 repo->path);
1367 /* not reached */
1370 if (close(imsg_fds[1]) == -1) {
1371 err = got_error_from_errno("close");
1372 free(ibuf);
1373 return err;
1375 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_BLOB].imsg_fd =
1376 imsg_fds[0];
1377 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_BLOB].pid = pid;
1378 imsg_init(ibuf, imsg_fds[0]);
1379 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_BLOB].ibuf = ibuf;
1381 return request_blob(outbuf, size, hdrlen, outfd, infd, id, ibuf);
1384 static const struct got_error *
1385 open_blob(struct got_blob_object **blob, struct got_repository *repo,
1386 struct got_object_id *id, size_t blocksize)
1388 const struct got_error *err = NULL;
1389 struct got_packidx *packidx = NULL;
1390 int idx;
1391 char *path_packfile = NULL;
1392 uint8_t *outbuf;
1393 int outfd;
1394 size_t size, hdrlen;
1395 struct stat sb;
1397 *blob = calloc(1, sizeof(**blob));
1398 if (*blob == NULL)
1399 return got_error_from_errno("calloc");
1401 outfd = got_opentempfd();
1402 if (outfd == -1)
1403 return got_error_from_errno("got_opentempfd");
1405 (*blob)->read_buf = malloc(blocksize);
1406 if ((*blob)->read_buf == NULL) {
1407 err = got_error_from_errno("malloc");
1408 goto done;
1411 err = got_repo_search_packidx(&packidx, &idx, repo, id);
1412 if (err == NULL) {
1413 struct got_pack *pack = NULL;
1415 err = got_packidx_get_packfile_path(&path_packfile,
1416 packidx->path_packidx);
1417 if (err)
1418 goto done;
1420 pack = got_repo_get_cached_pack(repo, path_packfile);
1421 if (pack == NULL) {
1422 err = got_repo_cache_pack(&pack, repo, path_packfile,
1423 packidx);
1424 if (err)
1425 goto done;
1427 err = read_packed_blob_privsep(&outbuf, &size, &hdrlen, outfd,
1428 pack, packidx, idx, id);
1429 } else if (err->code == GOT_ERR_NO_OBJ) {
1430 int infd;
1432 err = got_object_open_loose_fd(&infd, id, repo);
1433 if (err)
1434 goto done;
1435 err = read_blob_privsep(&outbuf, &size, &hdrlen, outfd, infd,
1436 id, repo);
1438 if (err)
1439 goto done;
1441 if (hdrlen > size) {
1442 err = got_error(GOT_ERR_BAD_OBJ_HDR);
1443 goto done;
1446 if (outbuf) {
1447 if (close(outfd) == -1 && err == NULL)
1448 err = got_error_from_errno("close");
1449 outfd = -1;
1450 (*blob)->f = fmemopen(outbuf, size, "rb");
1451 if ((*blob)->f == NULL) {
1452 err = got_error_from_errno("fmemopen");
1453 free(outbuf);
1454 goto done;
1456 (*blob)->data = outbuf;
1457 } else {
1458 if (fstat(outfd, &sb) == -1) {
1459 err = got_error_from_errno("fstat");
1460 goto done;
1463 if (sb.st_size != size) {
1464 err = got_error(GOT_ERR_PRIVSEP_LEN);
1465 goto done;
1468 (*blob)->f = fdopen(outfd, "rb");
1469 if ((*blob)->f == NULL) {
1470 err = got_error_from_errno("fdopen");
1471 close(outfd);
1472 outfd = -1;
1473 goto done;
1477 (*blob)->hdrlen = hdrlen;
1478 (*blob)->blocksize = blocksize;
1479 memcpy(&(*blob)->id.sha1, id->sha1, SHA1_DIGEST_LENGTH);
1481 done:
1482 free(path_packfile);
1483 if (err) {
1484 if (*blob) {
1485 got_object_blob_close(*blob);
1486 *blob = NULL;
1487 } else if (outfd != -1)
1488 close(outfd);
1490 return err;
1493 const struct got_error *
1494 got_object_open_as_blob(struct got_blob_object **blob,
1495 struct got_repository *repo, struct got_object_id *id,
1496 size_t blocksize)
1498 return open_blob(blob, repo, id, blocksize);
1501 const struct got_error *
1502 got_object_blob_open(struct got_blob_object **blob,
1503 struct got_repository *repo, struct got_object *obj, size_t blocksize)
1505 return open_blob(blob, repo, got_object_get_id(obj), blocksize);
1508 const struct got_error *
1509 got_object_blob_close(struct got_blob_object *blob)
1511 const struct got_error *err = NULL;
1512 free(blob->read_buf);
1513 if (blob->f && fclose(blob->f) == EOF)
1514 err = got_error_from_errno("fclose");
1515 free(blob->data);
1516 free(blob);
1517 return err;
1520 void
1521 got_object_blob_rewind(struct got_blob_object *blob)
1523 if (blob->f)
1524 rewind(blob->f);
1527 char *
1528 got_object_blob_id_str(struct got_blob_object *blob, char *buf, size_t size)
1530 return got_sha1_digest_to_str(blob->id.sha1, buf, size);
1533 size_t
1534 got_object_blob_get_hdrlen(struct got_blob_object *blob)
1536 return blob->hdrlen;
1539 const uint8_t *
1540 got_object_blob_get_read_buf(struct got_blob_object *blob)
1542 return blob->read_buf;
1545 const struct got_error *
1546 got_object_blob_read_block(size_t *outlenp, struct got_blob_object *blob)
1548 size_t n;
1550 n = fread(blob->read_buf, 1, blob->blocksize, blob->f);
1551 if (n == 0 && ferror(blob->f))
1552 return got_ferror(blob->f, GOT_ERR_IO);
1553 *outlenp = n;
1554 return NULL;
1557 const struct got_error *
1558 got_object_blob_dump_to_file(off_t *filesize, int *nlines,
1559 off_t **line_offsets, FILE *outfile, struct got_blob_object *blob)
1561 const struct got_error *err = NULL;
1562 size_t n, len, hdrlen;
1563 const uint8_t *buf;
1564 int i;
1565 const int alloc_chunksz = 512;
1566 size_t nalloc = 0;
1567 off_t off = 0, total_len = 0;
1569 if (line_offsets)
1570 *line_offsets = NULL;
1571 if (filesize)
1572 *filesize = 0;
1573 if (nlines)
1574 *nlines = 0;
1576 hdrlen = got_object_blob_get_hdrlen(blob);
1577 do {
1578 err = got_object_blob_read_block(&len, blob);
1579 if (err)
1580 return err;
1581 if (len == 0)
1582 break;
1583 buf = got_object_blob_get_read_buf(blob);
1584 i = hdrlen;
1585 if (nlines) {
1586 if (line_offsets && *line_offsets == NULL) {
1587 /* Have some data but perhaps no '\n'. */
1588 *nlines = 1;
1589 nalloc = alloc_chunksz;
1590 *line_offsets = calloc(nalloc,
1591 sizeof(**line_offsets));
1592 if (*line_offsets == NULL)
1593 return got_error_from_errno("calloc");
1595 /* Skip forward over end of first line. */
1596 while (i < len) {
1597 if (buf[i] == '\n')
1598 break;
1599 i++;
1602 /* Scan '\n' offsets in remaining chunk of data. */
1603 while (i < len) {
1604 if (buf[i] != '\n') {
1605 i++;
1606 continue;
1608 (*nlines)++;
1609 if (line_offsets && nalloc < *nlines) {
1610 size_t n = *nlines + alloc_chunksz;
1611 off_t *o = recallocarray(*line_offsets,
1612 nalloc, n, sizeof(**line_offsets));
1613 if (o == NULL) {
1614 free(*line_offsets);
1615 *line_offsets = NULL;
1616 return got_error_from_errno(
1617 "recallocarray");
1619 *line_offsets = o;
1620 nalloc = n;
1622 if (line_offsets) {
1623 off = total_len + i - hdrlen + 1;
1624 (*line_offsets)[*nlines - 1] = off;
1626 i++;
1629 /* Skip blob object header first time around. */
1630 n = fwrite(buf + hdrlen, 1, len - hdrlen, outfile);
1631 if (n != len - hdrlen)
1632 return got_ferror(outfile, GOT_ERR_IO);
1633 total_len += len - hdrlen;
1634 hdrlen = 0;
1635 } while (len != 0);
1637 if (fflush(outfile) != 0)
1638 return got_error_from_errno("fflush");
1639 rewind(outfile);
1641 if (filesize)
1642 *filesize = total_len;
1644 return NULL;
1647 static const struct got_error *
1648 request_packed_tag(struct got_tag_object **tag, struct got_pack *pack,
1649 int pack_idx, struct got_object_id *id)
1651 const struct got_error *err = NULL;
1653 err = got_privsep_send_tag_req(pack->privsep_child->ibuf, -1, id,
1654 pack_idx);
1655 if (err)
1656 return err;
1658 return got_privsep_recv_tag(tag, pack->privsep_child->ibuf);
1661 static const struct got_error *
1662 read_packed_tag_privsep(struct got_tag_object **tag,
1663 struct got_pack *pack, struct got_packidx *packidx, int idx,
1664 struct got_object_id *id)
1666 const struct got_error *err = NULL;
1668 if (pack->privsep_child)
1669 return request_packed_tag(tag, pack, idx, id);
1671 err = start_pack_privsep_child(pack, packidx);
1672 if (err)
1673 return err;
1675 return request_packed_tag(tag, pack, idx, id);
1678 static const struct got_error *
1679 request_tag(struct got_tag_object **tag, struct got_repository *repo,
1680 int fd, struct got_object_id *id)
1682 const struct got_error *err = NULL;
1683 struct imsgbuf *ibuf;
1685 ibuf = repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TAG].ibuf;
1687 err = got_privsep_send_tag_req(ibuf, fd, id, -1);
1688 if (err)
1689 return err;
1691 return got_privsep_recv_tag(tag, ibuf);
1694 static const struct got_error *
1695 read_tag_privsep(struct got_tag_object **tag, int obj_fd,
1696 struct got_object_id *id, struct got_repository *repo)
1698 const struct got_error *err;
1699 int imsg_fds[2];
1700 pid_t pid;
1701 struct imsgbuf *ibuf;
1703 if (repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TAG].imsg_fd != -1)
1704 return request_tag(tag, repo, obj_fd, id);
1706 ibuf = calloc(1, sizeof(*ibuf));
1707 if (ibuf == NULL)
1708 return got_error_from_errno("calloc");
1710 if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_fds) == -1) {
1711 err = got_error_from_errno("socketpair");
1712 free(ibuf);
1713 return err;
1716 pid = fork();
1717 if (pid == -1) {
1718 err = got_error_from_errno("fork");
1719 free(ibuf);
1720 return err;
1722 else if (pid == 0) {
1723 got_privsep_exec_child(imsg_fds, GOT_PATH_PROG_READ_TAG,
1724 repo->path);
1725 /* not reached */
1728 if (close(imsg_fds[1]) == -1) {
1729 err = got_error_from_errno("close");
1730 free(ibuf);
1731 return err;
1733 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TAG].imsg_fd =
1734 imsg_fds[0];
1735 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TAG].pid = pid;
1736 imsg_init(ibuf, imsg_fds[0]);
1737 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TAG].ibuf = ibuf;
1739 return request_tag(tag, repo, obj_fd, id);
1742 static const struct got_error *
1743 open_tag(struct got_tag_object **tag, struct got_repository *repo,
1744 struct got_object_id *id, int check_cache)
1746 const struct got_error *err = NULL;
1747 struct got_packidx *packidx = NULL;
1748 int idx;
1749 char *path_packfile = NULL;
1750 struct got_object *obj = NULL;
1751 int obj_type = GOT_OBJ_TYPE_ANY;
1753 if (check_cache) {
1754 *tag = got_repo_get_cached_tag(repo, id);
1755 if (*tag != NULL) {
1756 (*tag)->refcnt++;
1757 return NULL;
1759 } else
1760 *tag = NULL;
1762 err = got_repo_search_packidx(&packidx, &idx, repo, id);
1763 if (err == NULL) {
1764 struct got_pack *pack = NULL;
1766 err = got_packidx_get_packfile_path(&path_packfile,
1767 packidx->path_packidx);
1768 if (err)
1769 return err;
1771 pack = got_repo_get_cached_pack(repo, path_packfile);
1772 if (pack == NULL) {
1773 err = got_repo_cache_pack(&pack, repo, path_packfile,
1774 packidx);
1775 if (err)
1776 goto done;
1779 /* Beware of "lightweight" tags: Check object type first. */
1780 err = read_packed_object_privsep(&obj, repo, pack, packidx,
1781 idx, id);
1782 if (err)
1783 goto done;
1784 obj_type = obj->type;
1785 got_object_close(obj);
1786 if (obj_type != GOT_OBJ_TYPE_TAG) {
1787 err = got_error(GOT_ERR_OBJ_TYPE);
1788 goto done;
1790 err = read_packed_tag_privsep(tag, pack, packidx, idx, id);
1791 } else if (err->code == GOT_ERR_NO_OBJ) {
1792 int fd;
1794 err = got_object_open_loose_fd(&fd, id, repo);
1795 if (err)
1796 return err;
1797 err = got_object_read_header_privsep(&obj, id, repo, fd);
1798 if (err)
1799 return err;
1800 obj_type = obj->type;
1801 got_object_close(obj);
1802 if (obj_type != GOT_OBJ_TYPE_TAG)
1803 return got_error(GOT_ERR_OBJ_TYPE);
1805 err = got_object_open_loose_fd(&fd, id, repo);
1806 if (err)
1807 return err;
1808 err = read_tag_privsep(tag, fd, id, repo);
1811 if (err == NULL) {
1812 (*tag)->refcnt++;
1813 err = got_repo_cache_tag(repo, id, *tag);
1815 done:
1816 free(path_packfile);
1817 return err;
1820 const struct got_error *
1821 got_object_open_as_tag(struct got_tag_object **tag,
1822 struct got_repository *repo, struct got_object_id *id)
1824 *tag = got_repo_get_cached_tag(repo, id);
1825 if (*tag != NULL) {
1826 (*tag)->refcnt++;
1827 return NULL;
1830 return open_tag(tag, repo, id, 0);
1833 const struct got_error *
1834 got_object_tag_open(struct got_tag_object **tag,
1835 struct got_repository *repo, struct got_object *obj)
1837 return open_tag(tag, repo, got_object_get_id(obj), 1);
1840 const char *
1841 got_object_tag_get_name(struct got_tag_object *tag)
1843 return tag->tag;
1846 int
1847 got_object_tag_get_object_type(struct got_tag_object *tag)
1849 return tag->obj_type;
1852 struct got_object_id *
1853 got_object_tag_get_object_id(struct got_tag_object *tag)
1855 return &tag->id;
1858 time_t
1859 got_object_tag_get_tagger_time(struct got_tag_object *tag)
1861 return tag->tagger_time;
1864 time_t
1865 got_object_tag_get_tagger_gmtoff(struct got_tag_object *tag)
1867 return tag->tagger_gmtoff;
1870 const char *
1871 got_object_tag_get_tagger(struct got_tag_object *tag)
1873 return tag->tagger;
1876 const char *
1877 got_object_tag_get_message(struct got_tag_object *tag)
1879 return tag->tagmsg;
1882 static struct got_tree_entry *
1883 find_entry_by_name(struct got_tree_object *tree, const char *name, size_t len)
1885 int i;
1887 /* Note that tree entries are sorted in strncmp() order. */
1888 for (i = 0; i < tree->nentries; i++) {
1889 struct got_tree_entry *te = &tree->entries[i];
1890 int cmp = strncmp(te->name, name, len);
1891 if (cmp < 0)
1892 continue;
1893 if (cmp > 0)
1894 break;
1895 if (te->name[len] == '\0')
1896 return te;
1898 return NULL;
1901 struct got_tree_entry *
1902 got_object_tree_find_entry(struct got_tree_object *tree, const char *name)
1904 return find_entry_by_name(tree, name, strlen(name));
1907 const struct got_error *
1908 got_object_id_by_path(struct got_object_id **id, struct got_repository *repo,
1909 struct got_object_id *commit_id, const char *path)
1911 const struct got_error *err = NULL;
1912 struct got_commit_object *commit = NULL;
1913 struct got_tree_object *tree = NULL;
1914 struct got_tree_entry *te = NULL;
1915 const char *seg, *s;
1916 size_t seglen;
1918 *id = NULL;
1920 err = got_object_open_as_commit(&commit, repo, commit_id);
1921 if (err)
1922 goto done;
1924 /* Handle opening of root of commit's tree. */
1925 if (got_path_is_root_dir(path)) {
1926 *id = got_object_id_dup(commit->tree_id);
1927 if (*id == NULL)
1928 err = got_error_from_errno("got_object_id_dup");
1929 goto done;
1932 err = got_object_open_as_tree(&tree, repo, commit->tree_id);
1933 if (err)
1934 goto done;
1936 s = path;
1937 while (s[0] == '/')
1938 s++;
1939 seg = s;
1940 seglen = 0;
1941 while (*s) {
1942 struct got_tree_object *next_tree;
1944 if (*s != '/') {
1945 s++;
1946 seglen++;
1947 if (*s)
1948 continue;
1951 te = find_entry_by_name(tree, seg, seglen);
1952 if (te == NULL) {
1953 err = got_error_path(path, GOT_ERR_NO_TREE_ENTRY);
1954 goto done;
1957 if (*s == '\0')
1958 break;
1960 seg = s + 1;
1961 seglen = 0;
1962 s++;
1963 if (*s) {
1964 err = got_object_open_as_tree(&next_tree, repo,
1965 &te->id);
1966 te = NULL;
1967 if (err)
1968 goto done;
1969 got_object_tree_close(tree);
1970 tree = next_tree;
1974 if (te) {
1975 *id = got_object_id_dup(&te->id);
1976 if (*id == NULL)
1977 return got_error_from_errno("got_object_id_dup");
1978 } else
1979 err = got_error_path(path, GOT_ERR_NO_TREE_ENTRY);
1980 done:
1981 if (commit)
1982 got_object_commit_close(commit);
1983 if (tree)
1984 got_object_tree_close(tree);
1985 return err;
1989 * Normalize file mode bits to avoid false positive tree entry differences
1990 * in case tree entries have unexpected mode bits set.
1992 static mode_t
1993 normalize_mode_for_comparison(mode_t mode)
1996 * For directories, the only relevant bit is the IFDIR bit.
1997 * This allows us to detect paths changing from a directory
1998 * to a file and vice versa.
2000 if (S_ISDIR(mode))
2001 return mode & S_IFDIR;
2004 * For symlinks, the only relevant bit is the IFLNK bit.
2005 * This allows us to detect paths changing from a symlinks
2006 * to a file or directory and vice versa.
2008 if (S_ISLNK(mode))
2009 return mode & S_IFLNK;
2011 /* For files, the only change we care about is the executable bit. */
2012 return mode & S_IXUSR;
2015 const struct got_error *
2016 got_object_tree_path_changed(int *changed,
2017 struct got_tree_object *tree01, struct got_tree_object *tree02,
2018 const char *path, struct got_repository *repo)
2020 const struct got_error *err = NULL;
2021 struct got_tree_object *tree1 = NULL, *tree2 = NULL;
2022 struct got_tree_entry *te1 = NULL, *te2 = NULL;
2023 const char *seg, *s;
2024 size_t seglen;
2026 *changed = 0;
2028 /* We not do support comparing the root path. */
2029 if (got_path_is_root_dir(path))
2030 return got_error_path(path, GOT_ERR_BAD_PATH);
2032 tree1 = tree01;
2033 tree2 = tree02;
2034 s = path;
2035 while (*s == '/')
2036 s++;
2037 seg = s;
2038 seglen = 0;
2039 while (*s) {
2040 struct got_tree_object *next_tree1, *next_tree2;
2041 mode_t mode1, mode2;
2043 if (*s != '/') {
2044 s++;
2045 seglen++;
2046 if (*s)
2047 continue;
2050 te1 = find_entry_by_name(tree1, seg, seglen);
2051 if (te1 == NULL) {
2052 err = got_error(GOT_ERR_NO_OBJ);
2053 goto done;
2056 if (tree2)
2057 te2 = find_entry_by_name(tree2, seg, seglen);
2059 if (te2) {
2060 mode1 = normalize_mode_for_comparison(te1->mode);
2061 mode2 = normalize_mode_for_comparison(te2->mode);
2062 if (mode1 != mode2) {
2063 *changed = 1;
2064 goto done;
2067 if (got_object_id_cmp(&te1->id, &te2->id) == 0) {
2068 *changed = 0;
2069 goto done;
2073 if (*s == '\0') { /* final path element */
2074 *changed = 1;
2075 goto done;
2078 seg = s + 1;
2079 s++;
2080 seglen = 0;
2081 if (*s) {
2082 err = got_object_open_as_tree(&next_tree1, repo,
2083 &te1->id);
2084 te1 = NULL;
2085 if (err)
2086 goto done;
2087 if (tree1 != tree01)
2088 got_object_tree_close(tree1);
2089 tree1 = next_tree1;
2091 if (te2) {
2092 err = got_object_open_as_tree(&next_tree2, repo,
2093 &te2->id);
2094 te2 = NULL;
2095 if (err)
2096 goto done;
2097 if (tree2 != tree02)
2098 got_object_tree_close(tree2);
2099 tree2 = next_tree2;
2100 } else if (tree2) {
2101 if (tree2 != tree02)
2102 got_object_tree_close(tree2);
2103 tree2 = NULL;
2107 done:
2108 if (tree1 && tree1 != tree01)
2109 got_object_tree_close(tree1);
2110 if (tree2 && tree2 != tree02)
2111 got_object_tree_close(tree2);
2112 return err;
2115 const struct got_error *
2116 got_object_tree_entry_dup(struct got_tree_entry **new_te,
2117 struct got_tree_entry *te)
2119 const struct got_error *err = NULL;
2121 *new_te = calloc(1, sizeof(**new_te));
2122 if (*new_te == NULL)
2123 return got_error_from_errno("calloc");
2125 (*new_te)->mode = te->mode;
2126 memcpy((*new_te)->name, te->name, sizeof((*new_te)->name));
2127 memcpy(&(*new_te)->id, &te->id, sizeof((*new_te)->id));
2128 return err;
2131 int
2132 got_object_tree_entry_is_submodule(struct got_tree_entry *te)
2134 return (te->mode & S_IFMT) == (S_IFDIR | S_IFLNK);
2137 int
2138 got_object_tree_entry_is_symlink(struct got_tree_entry *te)
2140 /* S_IFDIR check avoids confusing symlinks with submodules. */
2141 return ((te->mode & (S_IFDIR | S_IFLNK)) == S_IFLNK);
2144 static const struct got_error *
2145 resolve_symlink(char **link_target, const char *path,
2146 struct got_object_id *commit_id, struct got_repository *repo)
2148 const struct got_error *err = NULL;
2149 char buf[PATH_MAX];
2150 char *name, *parent_path = NULL;
2151 struct got_object_id *tree_obj_id = NULL;
2152 struct got_tree_object *tree = NULL;
2153 struct got_tree_entry *te = NULL;
2155 *link_target = NULL;
2157 if (strlcpy(buf, path, sizeof(buf)) >= sizeof(buf))
2158 return got_error(GOT_ERR_NO_SPACE);
2160 name = basename(buf);
2161 if (name == NULL)
2162 return got_error_from_errno2("basename", path);
2164 err = got_path_dirname(&parent_path, path);
2165 if (err)
2166 return err;
2168 err = got_object_id_by_path(&tree_obj_id, repo, commit_id,
2169 parent_path);
2170 if (err) {
2171 if (err->code == GOT_ERR_NO_TREE_ENTRY) {
2172 /* Display the complete path in error message. */
2173 err = got_error_path(path, err->code);
2175 goto done;
2178 err = got_object_open_as_tree(&tree, repo, tree_obj_id);
2179 if (err)
2180 goto done;
2182 te = got_object_tree_find_entry(tree, name);
2183 if (te == NULL) {
2184 err = got_error_path(path, GOT_ERR_NO_TREE_ENTRY);
2185 goto done;
2188 if (got_object_tree_entry_is_symlink(te)) {
2189 err = got_tree_entry_get_symlink_target(link_target, te, repo);
2190 if (err)
2191 goto done;
2192 if (!got_path_is_absolute(*link_target)) {
2193 char *abspath;
2194 if (asprintf(&abspath, "%s/%s", parent_path,
2195 *link_target) == -1) {
2196 err = got_error_from_errno("asprintf");
2197 goto done;
2199 free(*link_target);
2200 *link_target = malloc(PATH_MAX);
2201 if (*link_target == NULL) {
2202 err = got_error_from_errno("malloc");
2203 goto done;
2205 err = got_canonpath(abspath, *link_target, PATH_MAX);
2206 free(abspath);
2207 if (err)
2208 goto done;
2211 done:
2212 free(tree_obj_id);
2213 if (tree)
2214 got_object_tree_close(tree);
2215 if (err) {
2216 free(*link_target);
2217 *link_target = NULL;
2219 return err;
2222 const struct got_error *
2223 got_object_resolve_symlinks(char **link_target, const char *path,
2224 struct got_object_id *commit_id, struct got_repository *repo)
2226 const struct got_error *err = NULL;
2227 char *next_target = NULL;
2228 int max_recursion = 40; /* matches Git */
2230 *link_target = NULL;
2232 do {
2233 err = resolve_symlink(&next_target,
2234 *link_target ? *link_target : path, commit_id, repo);
2235 if (err)
2236 break;
2237 if (next_target) {
2238 free(*link_target);
2239 if (--max_recursion == 0) {
2240 err = got_error_path(path, GOT_ERR_RECURSION);
2241 *link_target = NULL;
2242 break;
2244 *link_target = next_target;
2246 } while (next_target);
2248 return err;
2251 const struct got_error *
2252 got_traverse_packed_commits(struct got_object_id_queue *traversed_commits,
2253 struct got_object_id *commit_id, const char *path,
2254 struct got_repository *repo)
2256 const struct got_error *err = NULL;
2257 struct got_pack *pack = NULL;
2258 struct got_packidx *packidx = NULL;
2259 char *path_packfile = NULL;
2260 struct got_commit_object *changed_commit = NULL;
2261 struct got_object_id *changed_commit_id = NULL;
2262 int idx;
2264 err = got_repo_search_packidx(&packidx, &idx, repo, commit_id);
2265 if (err) {
2266 if (err->code != GOT_ERR_NO_OBJ)
2267 return err;
2268 return NULL;
2271 err = got_packidx_get_packfile_path(&path_packfile,
2272 packidx->path_packidx);
2273 if (err)
2274 return err;
2276 pack = got_repo_get_cached_pack(repo, path_packfile);
2277 if (pack == NULL) {
2278 err = got_repo_cache_pack(&pack, repo, path_packfile, packidx);
2279 if (err)
2280 goto done;
2283 if (pack->privsep_child == NULL) {
2284 err = start_pack_privsep_child(pack, packidx);
2285 if (err)
2286 goto done;
2289 err = got_privsep_send_commit_traversal_request(
2290 pack->privsep_child->ibuf, commit_id, idx, path);
2291 if (err)
2292 goto done;
2294 err = got_privsep_recv_traversed_commits(&changed_commit,
2295 &changed_commit_id, traversed_commits, pack->privsep_child->ibuf);
2296 if (err)
2297 goto done;
2299 if (changed_commit) {
2301 * Cache the commit in which the path was changed.
2302 * This commit might be opened again soon.
2304 changed_commit->refcnt++;
2305 err = got_repo_cache_commit(repo, changed_commit_id,
2306 changed_commit);
2307 got_object_commit_close(changed_commit);
2309 done:
2310 free(path_packfile);
2311 free(changed_commit_id);
2312 return err;