Blob


1 /*
2 * Copyright (c) 2018, 2019 Stefan Sperling <stsp@openbsd.org>
3 *
4 * Permission to use, copy, modify, and distribute this software for any
5 * purpose with or without fee is hereby granted, provided that the above
6 * copyright notice and this permission notice appear in all copies.
7 *
8 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
15 */
17 #include <sys/types.h>
18 #include <sys/stat.h>
19 #include <sys/queue.h>
20 #include <sys/uio.h>
21 #include <sys/socket.h>
22 #include <sys/wait.h>
23 #include <sys/resource.h>
25 #include <errno.h>
26 #include <fcntl.h>
27 #include <stdio.h>
28 #include <stdlib.h>
29 #include <string.h>
30 #include <stdint.h>
31 #include <sha1.h>
32 #include <unistd.h>
33 #include <zlib.h>
34 #include <ctype.h>
35 #include <libgen.h>
36 #include <limits.h>
37 #include <imsg.h>
38 #include <time.h>
40 #include "got_error.h"
41 #include "got_object.h"
42 #include "got_repository.h"
43 #include "got_opentemp.h"
44 #include "got_path.h"
46 #include "got_lib_sha1.h"
47 #include "got_lib_delta.h"
48 #include "got_lib_inflate.h"
49 #include "got_lib_object.h"
50 #include "got_lib_privsep.h"
51 #include "got_lib_object_idcache.h"
52 #include "got_lib_object_cache.h"
53 #include "got_lib_object_parse.h"
54 #include "got_lib_pack.h"
55 #include "got_lib_repository.h"
57 #ifndef MIN
58 #define MIN(_a,_b) ((_a) < (_b) ? (_a) : (_b))
59 #endif
61 struct got_object_id *
62 got_object_get_id(struct got_object *obj)
63 {
64 return &obj->id;
65 }
67 const struct got_error *
68 got_object_get_id_str(char **outbuf, struct got_object *obj)
69 {
70 return got_object_id_str(outbuf, &obj->id);
71 }
73 const struct got_error *
74 got_object_get_type(int *type, struct got_repository *repo,
75 struct got_object_id *id)
76 {
77 const struct got_error *err = NULL;
78 struct got_object *obj;
80 err = got_object_open(&obj, repo, id);
81 if (err)
82 return err;
84 switch (obj->type) {
85 case GOT_OBJ_TYPE_COMMIT:
86 case GOT_OBJ_TYPE_TREE:
87 case GOT_OBJ_TYPE_BLOB:
88 case GOT_OBJ_TYPE_TAG:
89 *type = obj->type;
90 break;
91 default:
92 err = got_error(GOT_ERR_OBJ_TYPE);
93 break;
94 }
96 got_object_close(obj);
97 return err;
98 }
100 const struct got_error *
101 got_object_get_path(char **path, struct got_object_id *id,
102 struct got_repository *repo)
104 const struct got_error *err = NULL;
105 char *hex = NULL;
106 char *path_objects;
108 *path = NULL;
110 path_objects = got_repo_get_path_objects(repo);
111 if (path_objects == NULL)
112 return got_error_from_errno("got_repo_get_path_objects");
114 err = got_object_id_str(&hex, id);
115 if (err)
116 goto done;
118 if (asprintf(path, "%s/%.2x/%s", path_objects,
119 id->sha1[0], hex + 2) == -1)
120 err = got_error_from_errno("asprintf");
122 done:
123 free(hex);
124 free(path_objects);
125 return err;
128 const struct got_error *
129 got_object_open_loose_fd(int *fd, struct got_object_id *id,
130 struct got_repository *repo)
132 const struct got_error *err = NULL;
133 char *path;
135 err = got_object_get_path(&path, id, repo);
136 if (err)
137 return err;
138 *fd = open(path, O_RDONLY | O_NOFOLLOW);
139 if (*fd == -1) {
140 err = got_error_from_errno2("open", path);
141 goto done;
143 done:
144 free(path);
145 return err;
148 static const struct got_error *
149 request_packed_object(struct got_object **obj, struct got_pack *pack, int idx,
150 struct got_object_id *id)
152 const struct got_error *err = NULL;
153 struct imsgbuf *ibuf = pack->privsep_child->ibuf;
155 err = got_privsep_send_packed_obj_req(ibuf, idx, id);
156 if (err)
157 return err;
159 err = got_privsep_recv_obj(obj, ibuf);
160 if (err)
161 return err;
163 memcpy(&(*obj)->id, id, sizeof((*obj)->id));
165 return NULL;
168 static const struct got_error *
169 request_packed_object_raw(uint8_t **outbuf, off_t *size, size_t *hdrlen,
170 int outfd, struct got_pack *pack, int idx, struct got_object_id *id)
172 const struct got_error *err = NULL;
173 struct imsgbuf *ibuf = pack->privsep_child->ibuf;
174 int outfd_child;
175 int basefd, accumfd; /* temporary files for delta application */
177 basefd = got_opentempfd();
178 if (basefd == -1)
179 return got_error_from_errno("got_opentempfd");
181 accumfd = got_opentempfd();
182 if (accumfd == -1) {
183 close(basefd);
184 return got_error_from_errno("got_opentempfd");
187 outfd_child = dup(outfd);
188 if (outfd_child == -1) {
189 err = got_error_from_errno("dup");
190 close(basefd);
191 close(accumfd);
192 return err;
195 err = got_privsep_send_packed_raw_obj_req(ibuf, idx, id);
196 if (err) {
197 close(basefd);
198 close(accumfd);
199 close(outfd_child);
200 return err;
203 err = got_privsep_send_raw_obj_outfd(ibuf, outfd_child);
204 if (err) {
205 close(basefd);
206 close(accumfd);
207 return err;
211 err = got_privsep_send_tmpfd(pack->privsep_child->ibuf,
212 basefd);
213 if (err) {
214 close(accumfd);
215 return err;
218 err = got_privsep_send_tmpfd(pack->privsep_child->ibuf,
219 accumfd);
220 if (err)
221 return err;
223 err = got_privsep_recv_raw_obj(outbuf, size, hdrlen, ibuf);
224 if (err)
225 return err;
227 return NULL;
230 static void
231 set_max_datasize(void)
233 struct rlimit rl;
235 if (getrlimit(RLIMIT_DATA, &rl) != 0)
236 return;
238 rl.rlim_cur = rl.rlim_max;
239 setrlimit(RLIMIT_DATA, &rl);
242 static const struct got_error *
243 start_pack_privsep_child(struct got_pack *pack, struct got_packidx *packidx)
245 const struct got_error *err = NULL;
246 int imsg_fds[2];
247 pid_t pid;
248 struct imsgbuf *ibuf;
250 ibuf = calloc(1, sizeof(*ibuf));
251 if (ibuf == NULL)
252 return got_error_from_errno("calloc");
254 pack->privsep_child = calloc(1, sizeof(*pack->privsep_child));
255 if (pack->privsep_child == NULL) {
256 err = got_error_from_errno("calloc");
257 free(ibuf);
258 return err;
261 if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_fds) == -1) {
262 err = got_error_from_errno("socketpair");
263 goto done;
266 pid = fork();
267 if (pid == -1) {
268 err = got_error_from_errno("fork");
269 goto done;
270 } else if (pid == 0) {
271 set_max_datasize();
272 got_privsep_exec_child(imsg_fds, GOT_PATH_PROG_READ_PACK,
273 pack->path_packfile);
274 /* not reached */
277 if (close(imsg_fds[1]) == -1)
278 return got_error_from_errno("close");
279 pack->privsep_child->imsg_fd = imsg_fds[0];
280 pack->privsep_child->pid = pid;
281 imsg_init(ibuf, imsg_fds[0]);
282 pack->privsep_child->ibuf = ibuf;
284 err = got_privsep_init_pack_child(ibuf, pack, packidx);
285 if (err) {
286 const struct got_error *child_err;
287 err = got_privsep_send_stop(pack->privsep_child->imsg_fd);
288 child_err = got_privsep_wait_for_child(
289 pack->privsep_child->pid);
290 if (child_err && err == NULL)
291 err = child_err;
293 done:
294 if (err) {
295 free(ibuf);
296 free(pack->privsep_child);
297 pack->privsep_child = NULL;
299 return err;
302 static const struct got_error *
303 read_packed_object_privsep(struct got_object **obj,
304 struct got_repository *repo, struct got_pack *pack,
305 struct got_packidx *packidx, int idx, struct got_object_id *id)
307 const struct got_error *err = NULL;
309 if (pack->privsep_child == NULL) {
310 err = start_pack_privsep_child(pack, packidx);
311 if (err)
312 return err;
315 return request_packed_object(obj, pack, idx, id);
318 static const struct got_error *
319 read_packed_object_raw_privsep(uint8_t **outbuf, off_t *size, size_t *hdrlen,
320 int outfd, struct got_pack *pack, struct got_packidx *packidx, int idx,
321 struct got_object_id *id)
323 const struct got_error *err = NULL;
325 if (pack->privsep_child == NULL) {
326 err = start_pack_privsep_child(pack, packidx);
327 if (err)
328 return err;
331 return request_packed_object_raw(outbuf, size, hdrlen, outfd, pack,
332 idx, id);
335 const struct got_error *
336 got_object_open_packed(struct got_object **obj, struct got_object_id *id,
337 struct got_repository *repo)
339 const struct got_error *err = NULL;
340 struct got_pack *pack = NULL;
341 struct got_packidx *packidx = NULL;
342 int idx;
343 char *path_packfile;
345 err = got_repo_search_packidx(&packidx, &idx, repo, id);
346 if (err)
347 return err;
349 err = got_packidx_get_packfile_path(&path_packfile,
350 packidx->path_packidx);
351 if (err)
352 return err;
354 pack = got_repo_get_cached_pack(repo, path_packfile);
355 if (pack == NULL) {
356 err = got_repo_cache_pack(&pack, repo, path_packfile, packidx);
357 if (err)
358 goto done;
361 err = read_packed_object_privsep(obj, repo, pack, packidx, idx, id);
362 if (err)
363 goto done;
364 done:
365 free(path_packfile);
366 return err;
369 static const struct got_error *
370 request_object(struct got_object **obj, struct got_object_id *id,
371 struct got_repository *repo, int fd)
373 const struct got_error *err = NULL;
374 struct imsgbuf *ibuf;
376 ibuf = repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_OBJECT].ibuf;
378 err = got_privsep_send_obj_req(ibuf, fd, id);
379 if (err)
380 return err;
382 return got_privsep_recv_obj(obj, ibuf);
385 static const struct got_error *
386 request_raw_object(uint8_t **outbuf, off_t *size, size_t *hdrlen, int outfd,
387 struct got_object_id *id, struct got_repository *repo, int infd)
389 const struct got_error *err = NULL;
390 struct imsgbuf *ibuf;
391 int outfd_child;
393 ibuf = repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_OBJECT].ibuf;
395 outfd_child = dup(outfd);
396 if (outfd_child == -1)
397 return got_error_from_errno("dup");
399 err = got_privsep_send_raw_obj_req(ibuf, infd, id);
400 if (err)
401 return err;
403 err = got_privsep_send_raw_obj_outfd(ibuf, outfd_child);
404 if (err)
405 return err;
407 return got_privsep_recv_raw_obj(outbuf, size, hdrlen, ibuf);
410 static const struct got_error *
411 start_read_object_child(struct got_repository *repo)
413 const struct got_error *err = NULL;
414 int imsg_fds[2];
415 pid_t pid;
416 struct imsgbuf *ibuf;
418 ibuf = calloc(1, sizeof(*ibuf));
419 if (ibuf == NULL)
420 return got_error_from_errno("calloc");
422 if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_fds) == -1) {
423 err = got_error_from_errno("socketpair");
424 free(ibuf);
425 return err;
428 pid = fork();
429 if (pid == -1) {
430 err = got_error_from_errno("fork");
431 free(ibuf);
432 return err;
434 else if (pid == 0) {
435 got_privsep_exec_child(imsg_fds, GOT_PATH_PROG_READ_OBJECT,
436 repo->path);
437 /* not reached */
440 if (close(imsg_fds[1]) == -1) {
441 err = got_error_from_errno("close");
442 free(ibuf);
443 return err;
446 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_OBJECT].imsg_fd =
447 imsg_fds[0];
448 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_OBJECT].pid = pid;
449 imsg_init(ibuf, imsg_fds[0]);
450 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_OBJECT].ibuf = ibuf;
452 return NULL;
455 const struct got_error *
456 got_object_read_header_privsep(struct got_object **obj,
457 struct got_object_id *id, struct got_repository *repo, int obj_fd)
459 const struct got_error *err;
461 if (repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_OBJECT].imsg_fd != -1)
462 return request_object(obj, id, repo, obj_fd);
464 err = start_read_object_child(repo);
465 if (err) {
466 close(obj_fd);
467 return err;
470 return request_object(obj, id, repo, obj_fd);
473 static const struct got_error *
474 read_object_raw_privsep(uint8_t **outbuf, off_t *size, size_t *hdrlen,
475 int outfd, struct got_object_id *id, struct got_repository *repo,
476 int obj_fd)
478 const struct got_error *err;
480 if (repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_OBJECT].imsg_fd != -1)
481 return request_raw_object(outbuf, size, hdrlen, outfd, id,
482 repo, obj_fd);
484 err = start_read_object_child(repo);
485 if (err)
486 return err;
488 return request_raw_object(outbuf, size, hdrlen, outfd, id, repo,
489 obj_fd);
492 const struct got_error *
493 got_object_open(struct got_object **obj, struct got_repository *repo,
494 struct got_object_id *id)
496 const struct got_error *err = NULL;
497 int fd;
499 *obj = got_repo_get_cached_object(repo, id);
500 if (*obj != NULL) {
501 (*obj)->refcnt++;
502 return NULL;
505 err = got_object_open_packed(obj, id, repo);
506 if (err && err->code != GOT_ERR_NO_OBJ)
507 return err;
508 if (*obj) {
509 (*obj)->refcnt++;
510 return got_repo_cache_object(repo, id, *obj);
513 err = got_object_open_loose_fd(&fd, id, repo);
514 if (err) {
515 if (err->code == GOT_ERR_ERRNO && errno == ENOENT)
516 err = got_error_no_obj(id);
517 return err;
520 err = got_object_read_header_privsep(obj, id, repo, fd);
521 if (err)
522 return err;
524 memcpy((*obj)->id.sha1, id->sha1, SHA1_DIGEST_LENGTH);
526 (*obj)->refcnt++;
527 return got_repo_cache_object(repo, id, *obj);
530 const struct got_error *
531 got_object_raw_open(struct got_raw_object **obj, struct got_repository *repo,
532 struct got_object_id *id, size_t blocksize)
534 const struct got_error *err = NULL;
535 struct got_packidx *packidx = NULL;
536 int idx;
537 uint8_t *outbuf = NULL;
538 int outfd = -1;
539 off_t size = 0;
540 size_t hdrlen = 0;
541 char *path_packfile = NULL;
543 *obj = NULL;
545 outfd = got_opentempfd();
546 if (outfd == -1)
547 return got_error_from_errno("got_opentempfd");
549 err = got_repo_search_packidx(&packidx, &idx, repo, id);
550 if (err == NULL) {
551 struct got_pack *pack = NULL;
553 err = got_packidx_get_packfile_path(&path_packfile,
554 packidx->path_packidx);
555 if (err)
556 goto done;
558 pack = got_repo_get_cached_pack(repo, path_packfile);
559 if (pack == NULL) {
560 err = got_repo_cache_pack(&pack, repo, path_packfile,
561 packidx);
562 if (err)
563 goto done;
565 err = read_packed_object_raw_privsep(&outbuf, &size, &hdrlen,
566 outfd, pack, packidx, idx, id);
567 } else if (err->code == GOT_ERR_NO_OBJ) {
568 int fd;
570 err = got_object_open_loose_fd(&fd, id, repo);
571 if (err)
572 goto done;
573 err = read_object_raw_privsep(&outbuf, &size, &hdrlen, outfd,
574 id, repo, fd);
577 *obj = calloc(1, sizeof(**obj));
578 if (*obj == NULL) {
579 err = got_error_from_errno("calloc");
580 goto done;
583 (*obj)->read_buf = malloc(blocksize);
584 if ((*obj)->read_buf == NULL) {
585 err = got_error_from_errno("malloc");
586 goto done;
589 if (outbuf) {
590 if (close(outfd) == -1) {
591 err = got_error_from_errno("close");
592 goto done;
594 outfd = -1;
595 (*obj)->f = fmemopen(outbuf, hdrlen + size, "r");
596 if ((*obj)->f == NULL) {
597 err = got_error_from_errno("fdopen");
598 goto done;
600 (*obj)->data = outbuf;
601 } else {
602 struct stat sb;
603 if (fstat(outfd, &sb) == -1) {
604 err = got_error_from_errno("fstat");
605 goto done;
608 if (sb.st_size != hdrlen + size) {
609 err = got_error(GOT_ERR_PRIVSEP_LEN);
610 goto done;
613 (*obj)->f = fdopen(outfd, "r");
614 if ((*obj)->f == NULL) {
615 err = got_error_from_errno("fdopen");
616 goto done;
618 outfd = -1;
619 (*obj)->data = NULL;
621 (*obj)->hdrlen = hdrlen;
622 (*obj)->size = size;
623 (*obj)->blocksize = blocksize;
624 done:
625 free(path_packfile);
626 if (err) {
627 if (*obj) {
628 got_object_raw_close(*obj);
629 *obj = NULL;
631 if (outfd != -1)
632 close(outfd);
633 free(outbuf);
635 return err;
638 void
639 got_object_raw_rewind(struct got_raw_object *obj)
641 if (obj->f)
642 rewind(obj->f);
645 size_t
646 got_object_raw_get_hdrlen(struct got_raw_object *obj)
648 return obj->hdrlen;
651 const uint8_t *
652 got_object_raw_get_read_buf(struct got_raw_object *obj)
654 return obj->read_buf;
657 const struct got_error *
658 got_object_raw_read_block(size_t *outlenp, struct got_raw_object *obj)
660 size_t n;
662 n = fread(obj->read_buf, 1, obj->blocksize, obj->f);
663 if (n == 0 && ferror(obj->f))
664 return got_ferror(obj->f, GOT_ERR_IO);
665 *outlenp = n;
666 return NULL;
669 const struct got_error *
670 got_object_raw_close(struct got_raw_object *obj)
672 const struct got_error *err = NULL;
674 free(obj->read_buf);
675 if (obj->f != NULL && fclose(obj->f) == EOF && err == NULL)
676 err = got_error_from_errno("fclose");
677 free(obj->data);
678 free(obj);
679 return err;
682 const struct got_error *
683 got_object_open_by_id_str(struct got_object **obj, struct got_repository *repo,
684 const char *id_str)
686 struct got_object_id id;
688 if (!got_parse_sha1_digest(id.sha1, id_str))
689 return got_error_path(id_str, GOT_ERR_BAD_OBJ_ID_STR);
691 return got_object_open(obj, repo, &id);
694 const struct got_error *
695 got_object_resolve_id_str(struct got_object_id **id,
696 struct got_repository *repo, const char *id_str)
698 const struct got_error *err = NULL;
699 struct got_object *obj;
701 err = got_object_open_by_id_str(&obj, repo, id_str);
702 if (err)
703 return err;
705 *id = got_object_id_dup(got_object_get_id(obj));
706 got_object_close(obj);
707 if (*id == NULL)
708 return got_error_from_errno("got_object_id_dup");
710 return NULL;
713 static const struct got_error *
714 request_packed_commit(struct got_commit_object **commit, struct got_pack *pack,
715 int pack_idx, struct got_object_id *id)
717 const struct got_error *err = NULL;
719 err = got_privsep_send_commit_req(pack->privsep_child->ibuf, -1, id,
720 pack_idx);
721 if (err)
722 return err;
724 err = got_privsep_recv_commit(commit, pack->privsep_child->ibuf);
725 if (err)
726 return err;
728 (*commit)->flags |= GOT_COMMIT_FLAG_PACKED;
729 return NULL;
732 static const struct got_error *
733 read_packed_commit_privsep(struct got_commit_object **commit,
734 struct got_pack *pack, struct got_packidx *packidx, int idx,
735 struct got_object_id *id)
737 const struct got_error *err = NULL;
739 if (pack->privsep_child)
740 return request_packed_commit(commit, pack, idx, id);
742 err = start_pack_privsep_child(pack, packidx);
743 if (err)
744 return err;
746 return request_packed_commit(commit, pack, idx, id);
749 static const struct got_error *
750 request_commit(struct got_commit_object **commit, struct got_repository *repo,
751 int fd, struct got_object_id *id)
753 const struct got_error *err = NULL;
754 struct imsgbuf *ibuf;
756 ibuf = repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_COMMIT].ibuf;
758 err = got_privsep_send_commit_req(ibuf, fd, id, -1);
759 if (err)
760 return err;
762 return got_privsep_recv_commit(commit, ibuf);
765 static const struct got_error *
766 read_commit_privsep(struct got_commit_object **commit, int obj_fd,
767 struct got_object_id *id, struct got_repository *repo)
769 const struct got_error *err;
770 int imsg_fds[2];
771 pid_t pid;
772 struct imsgbuf *ibuf;
774 if (repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_COMMIT].imsg_fd != -1)
775 return request_commit(commit, repo, obj_fd, id);
777 ibuf = calloc(1, sizeof(*ibuf));
778 if (ibuf == NULL)
779 return got_error_from_errno("calloc");
781 if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_fds) == -1) {
782 err = got_error_from_errno("socketpair");
783 free(ibuf);
784 return err;
787 pid = fork();
788 if (pid == -1) {
789 err = got_error_from_errno("fork");
790 free(ibuf);
791 return err;
793 else if (pid == 0) {
794 got_privsep_exec_child(imsg_fds, GOT_PATH_PROG_READ_COMMIT,
795 repo->path);
796 /* not reached */
799 if (close(imsg_fds[1]) == -1) {
800 err = got_error_from_errno("close");
801 free(ibuf);
802 return err;
804 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_COMMIT].imsg_fd =
805 imsg_fds[0];
806 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_COMMIT].pid = pid;
807 imsg_init(ibuf, imsg_fds[0]);
808 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_COMMIT].ibuf = ibuf;
810 return request_commit(commit, repo, obj_fd, id);
814 static const struct got_error *
815 open_commit(struct got_commit_object **commit,
816 struct got_repository *repo, struct got_object_id *id, int check_cache)
818 const struct got_error *err = NULL;
819 struct got_packidx *packidx = NULL;
820 int idx;
821 char *path_packfile = NULL;
823 if (check_cache) {
824 *commit = got_repo_get_cached_commit(repo, id);
825 if (*commit != NULL) {
826 (*commit)->refcnt++;
827 return NULL;
829 } else
830 *commit = NULL;
832 err = got_repo_search_packidx(&packidx, &idx, repo, id);
833 if (err == NULL) {
834 struct got_pack *pack = NULL;
836 err = got_packidx_get_packfile_path(&path_packfile,
837 packidx->path_packidx);
838 if (err)
839 return err;
841 pack = got_repo_get_cached_pack(repo, path_packfile);
842 if (pack == NULL) {
843 err = got_repo_cache_pack(&pack, repo, path_packfile,
844 packidx);
845 if (err)
846 goto done;
848 err = read_packed_commit_privsep(commit, pack,
849 packidx, idx, id);
850 } else if (err->code == GOT_ERR_NO_OBJ) {
851 int fd;
853 err = got_object_open_loose_fd(&fd, id, repo);
854 if (err)
855 return err;
856 err = read_commit_privsep(commit, fd, id, repo);
859 if (err == NULL) {
860 (*commit)->refcnt++;
861 err = got_repo_cache_commit(repo, id, *commit);
863 done:
864 free(path_packfile);
865 return err;
868 const struct got_error *
869 got_object_open_as_commit(struct got_commit_object **commit,
870 struct got_repository *repo, struct got_object_id *id)
872 *commit = got_repo_get_cached_commit(repo, id);
873 if (*commit != NULL) {
874 (*commit)->refcnt++;
875 return NULL;
878 return open_commit(commit, repo, id, 0);
881 const struct got_error *
882 got_object_commit_open(struct got_commit_object **commit,
883 struct got_repository *repo, struct got_object *obj)
885 return open_commit(commit, repo, got_object_get_id(obj), 1);
888 const struct got_error *
889 got_object_qid_alloc(struct got_object_qid **qid, struct got_object_id *id)
891 const struct got_error *err = NULL;
893 *qid = calloc(1, sizeof(**qid));
894 if (*qid == NULL)
895 return got_error_from_errno("calloc");
897 (*qid)->id = got_object_id_dup(id);
898 if ((*qid)->id == NULL) {
899 err = got_error_from_errno("got_object_id_dup");
900 got_object_qid_free(*qid);
901 *qid = NULL;
902 return err;
905 return NULL;
908 const struct got_error *
909 got_object_id_queue_copy(const struct got_object_id_queue *src,
910 struct got_object_id_queue *dest)
912 const struct got_error *err;
913 struct got_object_qid *qid;
915 STAILQ_FOREACH(qid, src, entry) {
916 struct got_object_qid *new;
917 /*
918 * Deep-copy the object ID only. Let the caller deal
919 * with setting up the new->data pointer if needed.
920 */
921 err = got_object_qid_alloc(&new, qid->id);
922 if (err) {
923 got_object_id_queue_free(dest);
924 return err;
926 STAILQ_INSERT_TAIL(dest, new, entry);
929 return NULL;
932 static const struct got_error *
933 request_packed_tree(struct got_tree_object **tree, struct got_pack *pack,
934 int pack_idx, struct got_object_id *id)
936 const struct got_error *err = NULL;
938 err = got_privsep_send_tree_req(pack->privsep_child->ibuf, -1, id,
939 pack_idx);
940 if (err)
941 return err;
943 return got_privsep_recv_tree(tree, pack->privsep_child->ibuf);
946 static const struct got_error *
947 read_packed_tree_privsep(struct got_tree_object **tree,
948 struct got_pack *pack, struct got_packidx *packidx, int idx,
949 struct got_object_id *id)
951 const struct got_error *err = NULL;
953 if (pack->privsep_child)
954 return request_packed_tree(tree, pack, idx, id);
956 err = start_pack_privsep_child(pack, packidx);
957 if (err)
958 return err;
960 return request_packed_tree(tree, pack, idx, id);
963 static const struct got_error *
964 request_tree(struct got_tree_object **tree, struct got_repository *repo,
965 int fd, struct got_object_id *id)
967 const struct got_error *err = NULL;
968 struct imsgbuf *ibuf;
970 ibuf = repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TREE].ibuf;
972 err = got_privsep_send_tree_req(ibuf, fd, id, -1);
973 if (err)
974 return err;
976 return got_privsep_recv_tree(tree, ibuf);
979 const struct got_error *
980 read_tree_privsep(struct got_tree_object **tree, int obj_fd,
981 struct got_object_id *id, struct got_repository *repo)
983 const struct got_error *err;
984 int imsg_fds[2];
985 pid_t pid;
986 struct imsgbuf *ibuf;
988 if (repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TREE].imsg_fd != -1)
989 return request_tree(tree, repo, obj_fd, id);
991 ibuf = calloc(1, sizeof(*ibuf));
992 if (ibuf == NULL)
993 return got_error_from_errno("calloc");
995 if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_fds) == -1) {
996 err = got_error_from_errno("socketpair");
997 free(ibuf);
998 return err;
1001 pid = fork();
1002 if (pid == -1) {
1003 err = got_error_from_errno("fork");
1004 free(ibuf);
1005 return err;
1007 else if (pid == 0) {
1008 got_privsep_exec_child(imsg_fds, GOT_PATH_PROG_READ_TREE,
1009 repo->path);
1010 /* not reached */
1013 if (close(imsg_fds[1]) == -1) {
1014 err = got_error_from_errno("close");
1015 free(ibuf);
1016 return err;
1018 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TREE].imsg_fd =
1019 imsg_fds[0];
1020 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TREE].pid = pid;
1021 imsg_init(ibuf, imsg_fds[0]);
1022 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TREE].ibuf = ibuf;
1025 return request_tree(tree, repo, obj_fd, id);
1028 static const struct got_error *
1029 open_tree(struct got_tree_object **tree, struct got_repository *repo,
1030 struct got_object_id *id, int check_cache)
1032 const struct got_error *err = NULL;
1033 struct got_packidx *packidx = NULL;
1034 int idx;
1035 char *path_packfile = NULL;
1037 if (check_cache) {
1038 *tree = got_repo_get_cached_tree(repo, id);
1039 if (*tree != NULL) {
1040 (*tree)->refcnt++;
1041 return NULL;
1043 } else
1044 *tree = NULL;
1046 err = got_repo_search_packidx(&packidx, &idx, repo, id);
1047 if (err == NULL) {
1048 struct got_pack *pack = NULL;
1050 err = got_packidx_get_packfile_path(&path_packfile,
1051 packidx->path_packidx);
1052 if (err)
1053 return err;
1055 pack = got_repo_get_cached_pack(repo, path_packfile);
1056 if (pack == NULL) {
1057 err = got_repo_cache_pack(&pack, repo, path_packfile,
1058 packidx);
1059 if (err)
1060 goto done;
1062 err = read_packed_tree_privsep(tree, pack,
1063 packidx, idx, id);
1064 } else if (err->code == GOT_ERR_NO_OBJ) {
1065 int fd;
1067 err = got_object_open_loose_fd(&fd, id, repo);
1068 if (err)
1069 return err;
1070 err = read_tree_privsep(tree, fd, id, repo);
1073 if (err == NULL) {
1074 (*tree)->refcnt++;
1075 err = got_repo_cache_tree(repo, id, *tree);
1077 done:
1078 free(path_packfile);
1079 return err;
1082 const struct got_error *
1083 got_object_open_as_tree(struct got_tree_object **tree,
1084 struct got_repository *repo, struct got_object_id *id)
1086 *tree = got_repo_get_cached_tree(repo, id);
1087 if (*tree != NULL) {
1088 (*tree)->refcnt++;
1089 return NULL;
1092 return open_tree(tree, repo, id, 0);
1095 const struct got_error *
1096 got_object_tree_open(struct got_tree_object **tree,
1097 struct got_repository *repo, struct got_object *obj)
1099 return open_tree(tree, repo, got_object_get_id(obj), 1);
1102 int
1103 got_object_tree_get_nentries(struct got_tree_object *tree)
1105 return tree->nentries;
1108 struct got_tree_entry *
1109 got_object_tree_get_first_entry(struct got_tree_object *tree)
1111 return got_object_tree_get_entry(tree, 0);
1114 struct got_tree_entry *
1115 got_object_tree_get_last_entry(struct got_tree_object *tree)
1117 return got_object_tree_get_entry(tree, tree->nentries - 1);
1120 struct got_tree_entry *
1121 got_object_tree_get_entry(struct got_tree_object *tree, int i)
1123 if (i < 0 || i >= tree->nentries)
1124 return NULL;
1125 return &tree->entries[i];
1128 mode_t
1129 got_tree_entry_get_mode(struct got_tree_entry *te)
1131 return te->mode;
1134 const char *
1135 got_tree_entry_get_name(struct got_tree_entry *te)
1137 return &te->name[0];
1140 struct got_object_id *
1141 got_tree_entry_get_id(struct got_tree_entry *te)
1143 return &te->id;
1146 const struct got_error *
1147 got_object_blob_read_to_str(char **s, struct got_blob_object *blob)
1149 const struct got_error *err = NULL;
1150 size_t len, totlen, hdrlen, offset;
1152 *s = NULL;
1154 hdrlen = got_object_blob_get_hdrlen(blob);
1155 totlen = 0;
1156 offset = 0;
1157 do {
1158 char *p;
1160 err = got_object_blob_read_block(&len, blob);
1161 if (err)
1162 return err;
1164 if (len == 0)
1165 break;
1167 totlen += len - hdrlen;
1168 p = realloc(*s, totlen + 1);
1169 if (p == NULL) {
1170 err = got_error_from_errno("realloc");
1171 free(*s);
1172 *s = NULL;
1173 return err;
1175 *s = p;
1176 /* Skip blob object header first time around. */
1177 memcpy(*s + offset,
1178 got_object_blob_get_read_buf(blob) + hdrlen, len - hdrlen);
1179 hdrlen = 0;
1180 offset = totlen;
1181 } while (len > 0);
1183 (*s)[totlen] = '\0';
1184 return NULL;
1187 const struct got_error *
1188 got_tree_entry_get_symlink_target(char **link_target, struct got_tree_entry *te,
1189 struct got_repository *repo)
1191 const struct got_error *err = NULL;
1192 struct got_blob_object *blob = NULL;
1194 *link_target = NULL;
1196 if (!got_object_tree_entry_is_symlink(te))
1197 return got_error(GOT_ERR_TREE_ENTRY_TYPE);
1199 err = got_object_open_as_blob(&blob, repo,
1200 got_tree_entry_get_id(te), PATH_MAX);
1201 if (err)
1202 return err;
1204 err = got_object_blob_read_to_str(link_target, blob);
1205 got_object_blob_close(blob);
1206 if (err) {
1207 free(*link_target);
1208 *link_target = NULL;
1210 return err;
1213 int
1214 got_tree_entry_get_index(struct got_tree_entry *te)
1216 return te->idx;
1219 struct got_tree_entry *
1220 got_tree_entry_get_next(struct got_tree_object *tree,
1221 struct got_tree_entry *te)
1223 return got_object_tree_get_entry(tree, te->idx + 1);
1226 struct got_tree_entry *
1227 got_tree_entry_get_prev(struct got_tree_object *tree,
1228 struct got_tree_entry *te)
1230 return got_object_tree_get_entry(tree, te->idx - 1);
1233 static const struct got_error *
1234 request_packed_blob(uint8_t **outbuf, size_t *size, size_t *hdrlen, int outfd,
1235 struct got_pack *pack, struct got_packidx *packidx, int idx,
1236 struct got_object_id *id)
1238 const struct got_error *err = NULL;
1239 int outfd_child;
1240 int basefd, accumfd; /* temporary files for delta application */
1242 basefd = got_opentempfd();
1243 if (basefd == -1)
1244 return got_error_from_errno("got_opentempfd");
1245 accumfd = got_opentempfd();
1246 if (accumfd == -1)
1247 return got_error_from_errno("got_opentempfd");
1249 outfd_child = dup(outfd);
1250 if (outfd_child == -1)
1251 return got_error_from_errno("dup");
1253 err = got_privsep_send_blob_req(pack->privsep_child->ibuf, -1, id, idx);
1254 if (err)
1255 return err;
1257 err = got_privsep_send_blob_outfd(pack->privsep_child->ibuf,
1258 outfd_child);
1259 if (err) {
1260 close(basefd);
1261 close(accumfd);
1262 return err;
1265 err = got_privsep_send_tmpfd(pack->privsep_child->ibuf,
1266 basefd);
1267 if (err) {
1268 close(accumfd);
1269 return err;
1272 err = got_privsep_send_tmpfd(pack->privsep_child->ibuf,
1273 accumfd);
1274 if (err)
1275 return err;
1277 err = got_privsep_recv_blob(outbuf, size, hdrlen,
1278 pack->privsep_child->ibuf);
1279 if (err)
1280 return err;
1282 if (lseek(outfd, SEEK_SET, 0) == -1)
1283 err = got_error_from_errno("lseek");
1285 return err;
1288 static const struct got_error *
1289 read_packed_blob_privsep(uint8_t **outbuf, size_t *size, size_t *hdrlen,
1290 int outfd, struct got_pack *pack, struct got_packidx *packidx, int idx,
1291 struct got_object_id *id)
1293 const struct got_error *err = NULL;
1295 if (pack->privsep_child == NULL) {
1296 err = start_pack_privsep_child(pack, packidx);
1297 if (err)
1298 return err;
1301 return request_packed_blob(outbuf, size, hdrlen, outfd, pack, packidx,
1302 idx, id);
1305 static const struct got_error *
1306 request_blob(uint8_t **outbuf, size_t *size, size_t *hdrlen, int outfd,
1307 int infd, struct got_object_id *id, struct imsgbuf *ibuf)
1309 const struct got_error *err = NULL;
1310 int outfd_child;
1312 outfd_child = dup(outfd);
1313 if (outfd_child == -1)
1314 return got_error_from_errno("dup");
1316 err = got_privsep_send_blob_req(ibuf, infd, id, -1);
1317 if (err)
1318 return err;
1320 err = got_privsep_send_blob_outfd(ibuf, outfd_child);
1321 if (err)
1322 return err;
1324 err = got_privsep_recv_blob(outbuf, size, hdrlen, ibuf);
1325 if (err)
1326 return err;
1328 if (lseek(outfd, SEEK_SET, 0) == -1)
1329 return got_error_from_errno("lseek");
1331 return err;
1334 static const struct got_error *
1335 read_blob_privsep(uint8_t **outbuf, size_t *size, size_t *hdrlen,
1336 int outfd, int infd, struct got_object_id *id, struct got_repository *repo)
1338 const struct got_error *err;
1339 int imsg_fds[2];
1340 pid_t pid;
1341 struct imsgbuf *ibuf;
1343 if (repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_BLOB].imsg_fd != -1) {
1344 ibuf = repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_BLOB].ibuf;
1345 return request_blob(outbuf, size, hdrlen, outfd, infd, id,
1346 ibuf);
1349 ibuf = calloc(1, sizeof(*ibuf));
1350 if (ibuf == NULL)
1351 return got_error_from_errno("calloc");
1353 if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_fds) == -1) {
1354 err = got_error_from_errno("socketpair");
1355 free(ibuf);
1356 return err;
1359 pid = fork();
1360 if (pid == -1) {
1361 err = got_error_from_errno("fork");
1362 free(ibuf);
1363 return err;
1365 else if (pid == 0) {
1366 got_privsep_exec_child(imsg_fds, GOT_PATH_PROG_READ_BLOB,
1367 repo->path);
1368 /* not reached */
1371 if (close(imsg_fds[1]) == -1) {
1372 err = got_error_from_errno("close");
1373 free(ibuf);
1374 return err;
1376 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_BLOB].imsg_fd =
1377 imsg_fds[0];
1378 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_BLOB].pid = pid;
1379 imsg_init(ibuf, imsg_fds[0]);
1380 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_BLOB].ibuf = ibuf;
1382 return request_blob(outbuf, size, hdrlen, outfd, infd, id, ibuf);
1385 static const struct got_error *
1386 open_blob(struct got_blob_object **blob, struct got_repository *repo,
1387 struct got_object_id *id, size_t blocksize)
1389 const struct got_error *err = NULL;
1390 struct got_packidx *packidx = NULL;
1391 int idx;
1392 char *path_packfile = NULL;
1393 uint8_t *outbuf;
1394 int outfd;
1395 size_t size, hdrlen;
1396 struct stat sb;
1398 *blob = calloc(1, sizeof(**blob));
1399 if (*blob == NULL)
1400 return got_error_from_errno("calloc");
1402 outfd = got_opentempfd();
1403 if (outfd == -1)
1404 return got_error_from_errno("got_opentempfd");
1406 (*blob)->read_buf = malloc(blocksize);
1407 if ((*blob)->read_buf == NULL) {
1408 err = got_error_from_errno("malloc");
1409 goto done;
1412 err = got_repo_search_packidx(&packidx, &idx, repo, id);
1413 if (err == NULL) {
1414 struct got_pack *pack = NULL;
1416 err = got_packidx_get_packfile_path(&path_packfile,
1417 packidx->path_packidx);
1418 if (err)
1419 goto done;
1421 pack = got_repo_get_cached_pack(repo, path_packfile);
1422 if (pack == NULL) {
1423 err = got_repo_cache_pack(&pack, repo, path_packfile,
1424 packidx);
1425 if (err)
1426 goto done;
1428 err = read_packed_blob_privsep(&outbuf, &size, &hdrlen, outfd,
1429 pack, packidx, idx, id);
1430 } else if (err->code == GOT_ERR_NO_OBJ) {
1431 int infd;
1433 err = got_object_open_loose_fd(&infd, id, repo);
1434 if (err)
1435 goto done;
1436 err = read_blob_privsep(&outbuf, &size, &hdrlen, outfd, infd,
1437 id, repo);
1439 if (err)
1440 goto done;
1442 if (hdrlen > size) {
1443 err = got_error(GOT_ERR_BAD_OBJ_HDR);
1444 goto done;
1447 if (outbuf) {
1448 if (close(outfd) == -1 && err == NULL)
1449 err = got_error_from_errno("close");
1450 outfd = -1;
1451 (*blob)->f = fmemopen(outbuf, size, "rb");
1452 if ((*blob)->f == NULL) {
1453 err = got_error_from_errno("fmemopen");
1454 free(outbuf);
1455 goto done;
1457 (*blob)->data = outbuf;
1458 } else {
1459 if (fstat(outfd, &sb) == -1) {
1460 err = got_error_from_errno("fstat");
1461 goto done;
1464 if (sb.st_size != size) {
1465 err = got_error(GOT_ERR_PRIVSEP_LEN);
1466 goto done;
1469 (*blob)->f = fdopen(outfd, "rb");
1470 if ((*blob)->f == NULL) {
1471 err = got_error_from_errno("fdopen");
1472 close(outfd);
1473 outfd = -1;
1474 goto done;
1478 (*blob)->hdrlen = hdrlen;
1479 (*blob)->blocksize = blocksize;
1480 memcpy(&(*blob)->id.sha1, id->sha1, SHA1_DIGEST_LENGTH);
1482 done:
1483 free(path_packfile);
1484 if (err) {
1485 if (*blob) {
1486 got_object_blob_close(*blob);
1487 *blob = NULL;
1488 } else if (outfd != -1)
1489 close(outfd);
1491 return err;
1494 const struct got_error *
1495 got_object_open_as_blob(struct got_blob_object **blob,
1496 struct got_repository *repo, struct got_object_id *id,
1497 size_t blocksize)
1499 return open_blob(blob, repo, id, blocksize);
1502 const struct got_error *
1503 got_object_blob_open(struct got_blob_object **blob,
1504 struct got_repository *repo, struct got_object *obj, size_t blocksize)
1506 return open_blob(blob, repo, got_object_get_id(obj), blocksize);
1509 const struct got_error *
1510 got_object_blob_close(struct got_blob_object *blob)
1512 const struct got_error *err = NULL;
1513 free(blob->read_buf);
1514 if (blob->f && fclose(blob->f) == EOF)
1515 err = got_error_from_errno("fclose");
1516 free(blob->data);
1517 free(blob);
1518 return err;
1521 void
1522 got_object_blob_rewind(struct got_blob_object *blob)
1524 if (blob->f)
1525 rewind(blob->f);
1528 char *
1529 got_object_blob_id_str(struct got_blob_object *blob, char *buf, size_t size)
1531 return got_sha1_digest_to_str(blob->id.sha1, buf, size);
1534 size_t
1535 got_object_blob_get_hdrlen(struct got_blob_object *blob)
1537 return blob->hdrlen;
1540 const uint8_t *
1541 got_object_blob_get_read_buf(struct got_blob_object *blob)
1543 return blob->read_buf;
1546 const struct got_error *
1547 got_object_blob_read_block(size_t *outlenp, struct got_blob_object *blob)
1549 size_t n;
1551 n = fread(blob->read_buf, 1, blob->blocksize, blob->f);
1552 if (n == 0 && ferror(blob->f))
1553 return got_ferror(blob->f, GOT_ERR_IO);
1554 *outlenp = n;
1555 return NULL;
1558 const struct got_error *
1559 got_object_blob_dump_to_file(off_t *filesize, int *nlines,
1560 off_t **line_offsets, FILE *outfile, struct got_blob_object *blob)
1562 const struct got_error *err = NULL;
1563 size_t n, len, hdrlen;
1564 const uint8_t *buf;
1565 int i;
1566 const int alloc_chunksz = 512;
1567 size_t nalloc = 0;
1568 off_t off = 0, total_len = 0;
1570 if (line_offsets)
1571 *line_offsets = NULL;
1572 if (filesize)
1573 *filesize = 0;
1574 if (nlines)
1575 *nlines = 0;
1577 hdrlen = got_object_blob_get_hdrlen(blob);
1578 do {
1579 err = got_object_blob_read_block(&len, blob);
1580 if (err)
1581 return err;
1582 if (len == 0)
1583 break;
1584 buf = got_object_blob_get_read_buf(blob);
1585 i = hdrlen;
1586 if (nlines) {
1587 if (line_offsets && *line_offsets == NULL) {
1588 /* Have some data but perhaps no '\n'. */
1589 *nlines = 1;
1590 nalloc = alloc_chunksz;
1591 *line_offsets = calloc(nalloc,
1592 sizeof(**line_offsets));
1593 if (*line_offsets == NULL)
1594 return got_error_from_errno("calloc");
1596 /* Skip forward over end of first line. */
1597 while (i < len) {
1598 if (buf[i] == '\n')
1599 break;
1600 i++;
1603 /* Scan '\n' offsets in remaining chunk of data. */
1604 while (i < len) {
1605 if (buf[i] != '\n') {
1606 i++;
1607 continue;
1609 (*nlines)++;
1610 if (line_offsets && nalloc < *nlines) {
1611 size_t n = *nlines + alloc_chunksz;
1612 off_t *o = recallocarray(*line_offsets,
1613 nalloc, n, sizeof(**line_offsets));
1614 if (o == NULL) {
1615 free(*line_offsets);
1616 *line_offsets = NULL;
1617 return got_error_from_errno(
1618 "recallocarray");
1620 *line_offsets = o;
1621 nalloc = n;
1623 if (line_offsets) {
1624 off = total_len + i - hdrlen + 1;
1625 (*line_offsets)[*nlines - 1] = off;
1627 i++;
1630 /* Skip blob object header first time around. */
1631 n = fwrite(buf + hdrlen, 1, len - hdrlen, outfile);
1632 if (n != len - hdrlen)
1633 return got_ferror(outfile, GOT_ERR_IO);
1634 total_len += len - hdrlen;
1635 hdrlen = 0;
1636 } while (len != 0);
1638 if (fflush(outfile) != 0)
1639 return got_error_from_errno("fflush");
1640 rewind(outfile);
1642 if (filesize)
1643 *filesize = total_len;
1645 return NULL;
1648 static const struct got_error *
1649 request_packed_tag(struct got_tag_object **tag, struct got_pack *pack,
1650 int pack_idx, struct got_object_id *id)
1652 const struct got_error *err = NULL;
1654 err = got_privsep_send_tag_req(pack->privsep_child->ibuf, -1, id,
1655 pack_idx);
1656 if (err)
1657 return err;
1659 return got_privsep_recv_tag(tag, pack->privsep_child->ibuf);
1662 static const struct got_error *
1663 read_packed_tag_privsep(struct got_tag_object **tag,
1664 struct got_pack *pack, struct got_packidx *packidx, int idx,
1665 struct got_object_id *id)
1667 const struct got_error *err = NULL;
1669 if (pack->privsep_child)
1670 return request_packed_tag(tag, pack, idx, id);
1672 err = start_pack_privsep_child(pack, packidx);
1673 if (err)
1674 return err;
1676 return request_packed_tag(tag, pack, idx, id);
1679 static const struct got_error *
1680 request_tag(struct got_tag_object **tag, struct got_repository *repo,
1681 int fd, struct got_object_id *id)
1683 const struct got_error *err = NULL;
1684 struct imsgbuf *ibuf;
1686 ibuf = repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TAG].ibuf;
1688 err = got_privsep_send_tag_req(ibuf, fd, id, -1);
1689 if (err)
1690 return err;
1692 return got_privsep_recv_tag(tag, ibuf);
1695 static const struct got_error *
1696 read_tag_privsep(struct got_tag_object **tag, int obj_fd,
1697 struct got_object_id *id, struct got_repository *repo)
1699 const struct got_error *err;
1700 int imsg_fds[2];
1701 pid_t pid;
1702 struct imsgbuf *ibuf;
1704 if (repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TAG].imsg_fd != -1)
1705 return request_tag(tag, repo, obj_fd, id);
1707 ibuf = calloc(1, sizeof(*ibuf));
1708 if (ibuf == NULL)
1709 return got_error_from_errno("calloc");
1711 if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_fds) == -1) {
1712 err = got_error_from_errno("socketpair");
1713 free(ibuf);
1714 return err;
1717 pid = fork();
1718 if (pid == -1) {
1719 err = got_error_from_errno("fork");
1720 free(ibuf);
1721 return err;
1723 else if (pid == 0) {
1724 got_privsep_exec_child(imsg_fds, GOT_PATH_PROG_READ_TAG,
1725 repo->path);
1726 /* not reached */
1729 if (close(imsg_fds[1]) == -1) {
1730 err = got_error_from_errno("close");
1731 free(ibuf);
1732 return err;
1734 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TAG].imsg_fd =
1735 imsg_fds[0];
1736 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TAG].pid = pid;
1737 imsg_init(ibuf, imsg_fds[0]);
1738 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TAG].ibuf = ibuf;
1740 return request_tag(tag, repo, obj_fd, id);
1743 static const struct got_error *
1744 open_tag(struct got_tag_object **tag, struct got_repository *repo,
1745 struct got_object_id *id, int check_cache)
1747 const struct got_error *err = NULL;
1748 struct got_packidx *packidx = NULL;
1749 int idx;
1750 char *path_packfile = NULL;
1751 struct got_object *obj = NULL;
1752 int obj_type = GOT_OBJ_TYPE_ANY;
1754 if (check_cache) {
1755 *tag = got_repo_get_cached_tag(repo, id);
1756 if (*tag != NULL) {
1757 (*tag)->refcnt++;
1758 return NULL;
1760 } else
1761 *tag = NULL;
1763 err = got_repo_search_packidx(&packidx, &idx, repo, id);
1764 if (err == NULL) {
1765 struct got_pack *pack = NULL;
1767 err = got_packidx_get_packfile_path(&path_packfile,
1768 packidx->path_packidx);
1769 if (err)
1770 return err;
1772 pack = got_repo_get_cached_pack(repo, path_packfile);
1773 if (pack == NULL) {
1774 err = got_repo_cache_pack(&pack, repo, path_packfile,
1775 packidx);
1776 if (err)
1777 goto done;
1780 /* Beware of "lightweight" tags: Check object type first. */
1781 err = read_packed_object_privsep(&obj, repo, pack, packidx,
1782 idx, id);
1783 if (err)
1784 goto done;
1785 obj_type = obj->type;
1786 got_object_close(obj);
1787 if (obj_type != GOT_OBJ_TYPE_TAG) {
1788 err = got_error(GOT_ERR_OBJ_TYPE);
1789 goto done;
1791 err = read_packed_tag_privsep(tag, pack, packidx, idx, id);
1792 } else if (err->code == GOT_ERR_NO_OBJ) {
1793 int fd;
1795 err = got_object_open_loose_fd(&fd, id, repo);
1796 if (err)
1797 return err;
1798 err = got_object_read_header_privsep(&obj, id, repo, fd);
1799 if (err)
1800 return err;
1801 obj_type = obj->type;
1802 got_object_close(obj);
1803 if (obj_type != GOT_OBJ_TYPE_TAG)
1804 return got_error(GOT_ERR_OBJ_TYPE);
1806 err = got_object_open_loose_fd(&fd, id, repo);
1807 if (err)
1808 return err;
1809 err = read_tag_privsep(tag, fd, id, repo);
1812 if (err == NULL) {
1813 (*tag)->refcnt++;
1814 err = got_repo_cache_tag(repo, id, *tag);
1816 done:
1817 free(path_packfile);
1818 return err;
1821 const struct got_error *
1822 got_object_open_as_tag(struct got_tag_object **tag,
1823 struct got_repository *repo, struct got_object_id *id)
1825 *tag = got_repo_get_cached_tag(repo, id);
1826 if (*tag != NULL) {
1827 (*tag)->refcnt++;
1828 return NULL;
1831 return open_tag(tag, repo, id, 0);
1834 const struct got_error *
1835 got_object_tag_open(struct got_tag_object **tag,
1836 struct got_repository *repo, struct got_object *obj)
1838 return open_tag(tag, repo, got_object_get_id(obj), 1);
1841 const char *
1842 got_object_tag_get_name(struct got_tag_object *tag)
1844 return tag->tag;
1847 int
1848 got_object_tag_get_object_type(struct got_tag_object *tag)
1850 return tag->obj_type;
1853 struct got_object_id *
1854 got_object_tag_get_object_id(struct got_tag_object *tag)
1856 return &tag->id;
1859 time_t
1860 got_object_tag_get_tagger_time(struct got_tag_object *tag)
1862 return tag->tagger_time;
1865 time_t
1866 got_object_tag_get_tagger_gmtoff(struct got_tag_object *tag)
1868 return tag->tagger_gmtoff;
1871 const char *
1872 got_object_tag_get_tagger(struct got_tag_object *tag)
1874 return tag->tagger;
1877 const char *
1878 got_object_tag_get_message(struct got_tag_object *tag)
1880 return tag->tagmsg;
1883 static struct got_tree_entry *
1884 find_entry_by_name(struct got_tree_object *tree, const char *name, size_t len)
1886 int i;
1888 /* Note that tree entries are sorted in strncmp() order. */
1889 for (i = 0; i < tree->nentries; i++) {
1890 struct got_tree_entry *te = &tree->entries[i];
1891 int cmp = strncmp(te->name, name, len);
1892 if (cmp < 0)
1893 continue;
1894 if (cmp > 0)
1895 break;
1896 if (te->name[len] == '\0')
1897 return te;
1899 return NULL;
1902 struct got_tree_entry *
1903 got_object_tree_find_entry(struct got_tree_object *tree, const char *name)
1905 return find_entry_by_name(tree, name, strlen(name));
1908 const struct got_error *
1909 got_object_id_by_path(struct got_object_id **id, struct got_repository *repo,
1910 struct got_object_id *commit_id, const char *path)
1912 const struct got_error *err = NULL;
1913 struct got_commit_object *commit = NULL;
1914 struct got_tree_object *tree = NULL;
1915 struct got_tree_entry *te = NULL;
1916 const char *seg, *s;
1917 size_t seglen;
1919 *id = NULL;
1921 err = got_object_open_as_commit(&commit, repo, commit_id);
1922 if (err)
1923 goto done;
1925 /* Handle opening of root of commit's tree. */
1926 if (got_path_is_root_dir(path)) {
1927 *id = got_object_id_dup(commit->tree_id);
1928 if (*id == NULL)
1929 err = got_error_from_errno("got_object_id_dup");
1930 goto done;
1933 err = got_object_open_as_tree(&tree, repo, commit->tree_id);
1934 if (err)
1935 goto done;
1937 s = path;
1938 while (s[0] == '/')
1939 s++;
1940 seg = s;
1941 seglen = 0;
1942 while (*s) {
1943 struct got_tree_object *next_tree;
1945 if (*s != '/') {
1946 s++;
1947 seglen++;
1948 if (*s)
1949 continue;
1952 te = find_entry_by_name(tree, seg, seglen);
1953 if (te == NULL) {
1954 err = got_error_path(path, GOT_ERR_NO_TREE_ENTRY);
1955 goto done;
1958 if (*s == '\0')
1959 break;
1961 seg = s + 1;
1962 seglen = 0;
1963 s++;
1964 if (*s) {
1965 err = got_object_open_as_tree(&next_tree, repo,
1966 &te->id);
1967 te = NULL;
1968 if (err)
1969 goto done;
1970 got_object_tree_close(tree);
1971 tree = next_tree;
1975 if (te) {
1976 *id = got_object_id_dup(&te->id);
1977 if (*id == NULL)
1978 return got_error_from_errno("got_object_id_dup");
1979 } else
1980 err = got_error_path(path, GOT_ERR_NO_TREE_ENTRY);
1981 done:
1982 if (commit)
1983 got_object_commit_close(commit);
1984 if (tree)
1985 got_object_tree_close(tree);
1986 return err;
1990 * Normalize file mode bits to avoid false positive tree entry differences
1991 * in case tree entries have unexpected mode bits set.
1993 static mode_t
1994 normalize_mode_for_comparison(mode_t mode)
1997 * For directories, the only relevant bit is the IFDIR bit.
1998 * This allows us to detect paths changing from a directory
1999 * to a file and vice versa.
2001 if (S_ISDIR(mode))
2002 return mode & S_IFDIR;
2005 * For symlinks, the only relevant bit is the IFLNK bit.
2006 * This allows us to detect paths changing from a symlinks
2007 * to a file or directory and vice versa.
2009 if (S_ISLNK(mode))
2010 return mode & S_IFLNK;
2012 /* For files, the only change we care about is the executable bit. */
2013 return mode & S_IXUSR;
2016 const struct got_error *
2017 got_object_tree_path_changed(int *changed,
2018 struct got_tree_object *tree01, struct got_tree_object *tree02,
2019 const char *path, struct got_repository *repo)
2021 const struct got_error *err = NULL;
2022 struct got_tree_object *tree1 = NULL, *tree2 = NULL;
2023 struct got_tree_entry *te1 = NULL, *te2 = NULL;
2024 const char *seg, *s;
2025 size_t seglen;
2027 *changed = 0;
2029 /* We not do support comparing the root path. */
2030 if (got_path_is_root_dir(path))
2031 return got_error_path(path, GOT_ERR_BAD_PATH);
2033 tree1 = tree01;
2034 tree2 = tree02;
2035 s = path;
2036 while (*s == '/')
2037 s++;
2038 seg = s;
2039 seglen = 0;
2040 while (*s) {
2041 struct got_tree_object *next_tree1, *next_tree2;
2042 mode_t mode1, mode2;
2044 if (*s != '/') {
2045 s++;
2046 seglen++;
2047 if (*s)
2048 continue;
2051 te1 = find_entry_by_name(tree1, seg, seglen);
2052 if (te1 == NULL) {
2053 err = got_error(GOT_ERR_NO_OBJ);
2054 goto done;
2057 if (tree2)
2058 te2 = find_entry_by_name(tree2, seg, seglen);
2060 if (te2) {
2061 mode1 = normalize_mode_for_comparison(te1->mode);
2062 mode2 = normalize_mode_for_comparison(te2->mode);
2063 if (mode1 != mode2) {
2064 *changed = 1;
2065 goto done;
2068 if (got_object_id_cmp(&te1->id, &te2->id) == 0) {
2069 *changed = 0;
2070 goto done;
2074 if (*s == '\0') { /* final path element */
2075 *changed = 1;
2076 goto done;
2079 seg = s + 1;
2080 s++;
2081 seglen = 0;
2082 if (*s) {
2083 err = got_object_open_as_tree(&next_tree1, repo,
2084 &te1->id);
2085 te1 = NULL;
2086 if (err)
2087 goto done;
2088 if (tree1 != tree01)
2089 got_object_tree_close(tree1);
2090 tree1 = next_tree1;
2092 if (te2) {
2093 err = got_object_open_as_tree(&next_tree2, repo,
2094 &te2->id);
2095 te2 = NULL;
2096 if (err)
2097 goto done;
2098 if (tree2 != tree02)
2099 got_object_tree_close(tree2);
2100 tree2 = next_tree2;
2101 } else if (tree2) {
2102 if (tree2 != tree02)
2103 got_object_tree_close(tree2);
2104 tree2 = NULL;
2108 done:
2109 if (tree1 && tree1 != tree01)
2110 got_object_tree_close(tree1);
2111 if (tree2 && tree2 != tree02)
2112 got_object_tree_close(tree2);
2113 return err;
2116 const struct got_error *
2117 got_object_tree_entry_dup(struct got_tree_entry **new_te,
2118 struct got_tree_entry *te)
2120 const struct got_error *err = NULL;
2122 *new_te = calloc(1, sizeof(**new_te));
2123 if (*new_te == NULL)
2124 return got_error_from_errno("calloc");
2126 (*new_te)->mode = te->mode;
2127 memcpy((*new_te)->name, te->name, sizeof((*new_te)->name));
2128 memcpy(&(*new_te)->id, &te->id, sizeof((*new_te)->id));
2129 return err;
2132 int
2133 got_object_tree_entry_is_submodule(struct got_tree_entry *te)
2135 return (te->mode & S_IFMT) == (S_IFDIR | S_IFLNK);
2138 int
2139 got_object_tree_entry_is_symlink(struct got_tree_entry *te)
2141 /* S_IFDIR check avoids confusing symlinks with submodules. */
2142 return ((te->mode & (S_IFDIR | S_IFLNK)) == S_IFLNK);
2145 static const struct got_error *
2146 resolve_symlink(char **link_target, const char *path,
2147 struct got_object_id *commit_id, struct got_repository *repo)
2149 const struct got_error *err = NULL;
2150 char buf[PATH_MAX];
2151 char *name, *parent_path = NULL;
2152 struct got_object_id *tree_obj_id = NULL;
2153 struct got_tree_object *tree = NULL;
2154 struct got_tree_entry *te = NULL;
2156 *link_target = NULL;
2158 if (strlcpy(buf, path, sizeof(buf)) >= sizeof(buf))
2159 return got_error(GOT_ERR_NO_SPACE);
2161 name = basename(buf);
2162 if (name == NULL)
2163 return got_error_from_errno2("basename", path);
2165 err = got_path_dirname(&parent_path, path);
2166 if (err)
2167 return err;
2169 err = got_object_id_by_path(&tree_obj_id, repo, commit_id,
2170 parent_path);
2171 if (err) {
2172 if (err->code == GOT_ERR_NO_TREE_ENTRY) {
2173 /* Display the complete path in error message. */
2174 err = got_error_path(path, err->code);
2176 goto done;
2179 err = got_object_open_as_tree(&tree, repo, tree_obj_id);
2180 if (err)
2181 goto done;
2183 te = got_object_tree_find_entry(tree, name);
2184 if (te == NULL) {
2185 err = got_error_path(path, GOT_ERR_NO_TREE_ENTRY);
2186 goto done;
2189 if (got_object_tree_entry_is_symlink(te)) {
2190 err = got_tree_entry_get_symlink_target(link_target, te, repo);
2191 if (err)
2192 goto done;
2193 if (!got_path_is_absolute(*link_target)) {
2194 char *abspath;
2195 if (asprintf(&abspath, "%s/%s", parent_path,
2196 *link_target) == -1) {
2197 err = got_error_from_errno("asprintf");
2198 goto done;
2200 free(*link_target);
2201 *link_target = malloc(PATH_MAX);
2202 if (*link_target == NULL) {
2203 err = got_error_from_errno("malloc");
2204 goto done;
2206 err = got_canonpath(abspath, *link_target, PATH_MAX);
2207 free(abspath);
2208 if (err)
2209 goto done;
2212 done:
2213 free(tree_obj_id);
2214 if (tree)
2215 got_object_tree_close(tree);
2216 if (err) {
2217 free(*link_target);
2218 *link_target = NULL;
2220 return err;
2223 const struct got_error *
2224 got_object_resolve_symlinks(char **link_target, const char *path,
2225 struct got_object_id *commit_id, struct got_repository *repo)
2227 const struct got_error *err = NULL;
2228 char *next_target = NULL;
2229 int max_recursion = 40; /* matches Git */
2231 *link_target = NULL;
2233 do {
2234 err = resolve_symlink(&next_target,
2235 *link_target ? *link_target : path, commit_id, repo);
2236 if (err)
2237 break;
2238 if (next_target) {
2239 free(*link_target);
2240 if (--max_recursion == 0) {
2241 err = got_error_path(path, GOT_ERR_RECURSION);
2242 *link_target = NULL;
2243 break;
2245 *link_target = next_target;
2247 } while (next_target);
2249 return err;
2252 const struct got_error *
2253 got_traverse_packed_commits(struct got_object_id_queue *traversed_commits,
2254 struct got_object_id *commit_id, const char *path,
2255 struct got_repository *repo)
2257 const struct got_error *err = NULL;
2258 struct got_pack *pack = NULL;
2259 struct got_packidx *packidx = NULL;
2260 char *path_packfile = NULL;
2261 struct got_commit_object *changed_commit = NULL;
2262 struct got_object_id *changed_commit_id = NULL;
2263 int idx;
2265 err = got_repo_search_packidx(&packidx, &idx, repo, commit_id);
2266 if (err) {
2267 if (err->code != GOT_ERR_NO_OBJ)
2268 return err;
2269 return NULL;
2272 err = got_packidx_get_packfile_path(&path_packfile,
2273 packidx->path_packidx);
2274 if (err)
2275 return err;
2277 pack = got_repo_get_cached_pack(repo, path_packfile);
2278 if (pack == NULL) {
2279 err = got_repo_cache_pack(&pack, repo, path_packfile, packidx);
2280 if (err)
2281 goto done;
2284 if (pack->privsep_child == NULL) {
2285 err = start_pack_privsep_child(pack, packidx);
2286 if (err)
2287 goto done;
2290 err = got_privsep_send_commit_traversal_request(
2291 pack->privsep_child->ibuf, commit_id, idx, path);
2292 if (err)
2293 goto done;
2295 err = got_privsep_recv_traversed_commits(&changed_commit,
2296 &changed_commit_id, traversed_commits, pack->privsep_child->ibuf);
2297 if (err)
2298 goto done;
2300 if (changed_commit) {
2302 * Cache the commit in which the path was changed.
2303 * This commit might be opened again soon.
2305 changed_commit->refcnt++;
2306 err = got_repo_cache_commit(repo, changed_commit_id,
2307 changed_commit);
2308 got_object_commit_close(changed_commit);
2310 done:
2311 free(path_packfile);
2312 free(changed_commit_id);
2313 return err;