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 static const struct got_error *
129 open_loose_object(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 static const struct got_error *
336 open_packed_object(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, 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_repository *repo, int fd)
371 const struct got_error *err = NULL;
372 struct imsgbuf *ibuf;
374 ibuf = repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_OBJECT].ibuf;
376 err = got_privsep_send_obj_req(ibuf, fd);
377 if (err)
378 return err;
380 return got_privsep_recv_obj(obj, ibuf);
383 static const struct got_error *
384 request_raw_object(uint8_t **outbuf, off_t *size, size_t *hdrlen, int outfd,
385 struct got_repository *repo, int infd)
387 const struct got_error *err = NULL;
388 struct imsgbuf *ibuf;
389 int outfd_child;
391 ibuf = repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_OBJECT].ibuf;
393 outfd_child = dup(outfd);
394 if (outfd_child == -1)
395 return got_error_from_errno("dup");
397 err = got_privsep_send_raw_obj_req(ibuf, infd);
398 if (err)
399 return err;
401 err = got_privsep_send_raw_obj_outfd(ibuf, outfd_child);
402 if (err)
403 return err;
405 return got_privsep_recv_raw_obj(outbuf, size, hdrlen, ibuf);
408 static const struct got_error *
409 start_read_object_child(struct got_repository *repo)
411 const struct got_error *err = NULL;
412 int imsg_fds[2];
413 pid_t pid;
414 struct imsgbuf *ibuf;
416 ibuf = calloc(1, sizeof(*ibuf));
417 if (ibuf == NULL)
418 return got_error_from_errno("calloc");
420 if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_fds) == -1) {
421 err = got_error_from_errno("socketpair");
422 free(ibuf);
423 return err;
426 pid = fork();
427 if (pid == -1) {
428 err = got_error_from_errno("fork");
429 free(ibuf);
430 return err;
432 else if (pid == 0) {
433 got_privsep_exec_child(imsg_fds, GOT_PATH_PROG_READ_OBJECT,
434 repo->path);
435 /* not reached */
438 if (close(imsg_fds[1]) == -1) {
439 err = got_error_from_errno("close");
440 free(ibuf);
441 return err;
444 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_OBJECT].imsg_fd =
445 imsg_fds[0];
446 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_OBJECT].pid = pid;
447 imsg_init(ibuf, imsg_fds[0]);
448 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_OBJECT].ibuf = ibuf;
450 return NULL;
453 static const struct got_error *
454 read_object_header_privsep(struct got_object **obj, struct got_repository *repo,
455 int obj_fd)
457 const struct got_error *err;
459 if (repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_OBJECT].imsg_fd != -1)
460 return request_object(obj, repo, obj_fd);
462 err = start_read_object_child(repo);
463 if (err) {
464 close(obj_fd);
465 return err;
468 return request_object(obj, repo, obj_fd);
471 static const struct got_error *
472 read_object_raw_privsep(uint8_t **outbuf, off_t *size, size_t *hdrlen,
473 int outfd, struct got_repository *repo, int obj_fd)
475 const struct got_error *err;
477 if (repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_OBJECT].imsg_fd != -1)
478 return request_raw_object(outbuf, size, hdrlen, outfd, repo,
479 obj_fd);
481 err = start_read_object_child(repo);
482 if (err)
483 return err;
485 return request_raw_object(outbuf, size, hdrlen, outfd, repo, obj_fd);
488 const struct got_error *
489 got_object_open(struct got_object **obj, struct got_repository *repo,
490 struct got_object_id *id)
492 const struct got_error *err = NULL;
493 char *path;
494 int fd;
496 *obj = got_repo_get_cached_object(repo, id);
497 if (*obj != NULL) {
498 (*obj)->refcnt++;
499 return NULL;
502 err = open_packed_object(obj, id, repo);
503 if (err && err->code != GOT_ERR_NO_OBJ)
504 return err;
505 if (*obj) {
506 (*obj)->refcnt++;
507 return got_repo_cache_object(repo, id, *obj);
510 err = got_object_get_path(&path, id, repo);
511 if (err)
512 return err;
514 fd = open(path, O_RDONLY | O_NOFOLLOW);
515 if (fd == -1) {
516 if (errno == ENOENT)
517 err = got_error_no_obj(id);
518 else
519 err = got_error_from_errno2("open", path);
520 goto done;
521 } else {
522 err = read_object_header_privsep(obj, repo, fd);
523 if (err)
524 goto done;
525 memcpy((*obj)->id.sha1, id->sha1, SHA1_DIGEST_LENGTH);
528 (*obj)->refcnt++;
529 err = got_repo_cache_object(repo, id, *obj);
530 done:
531 free(path);
532 return err;
535 const struct got_error *
536 got_object_raw_open(struct got_raw_object **obj, struct got_repository *repo,
537 struct got_object_id *id, size_t blocksize)
539 const struct got_error *err = NULL;
540 struct got_packidx *packidx = NULL;
541 int idx;
542 uint8_t *outbuf = NULL;
543 int outfd = -1;
544 off_t size = 0;
545 size_t hdrlen = 0;
546 char *path_packfile = NULL;
548 *obj = NULL;
550 outfd = got_opentempfd();
551 if (outfd == -1)
552 return got_error_from_errno("got_opentempfd");
554 err = got_repo_search_packidx(&packidx, &idx, repo, id);
555 if (err == NULL) {
556 struct got_pack *pack = NULL;
558 err = got_packidx_get_packfile_path(&path_packfile, packidx);
559 if (err)
560 goto done;
562 pack = got_repo_get_cached_pack(repo, path_packfile);
563 if (pack == NULL) {
564 err = got_repo_cache_pack(&pack, repo, path_packfile,
565 packidx);
566 if (err)
567 goto done;
569 err = read_packed_object_raw_privsep(&outbuf, &size, &hdrlen,
570 outfd, pack, packidx, idx, id);
571 } else if (err->code == GOT_ERR_NO_OBJ) {
572 int fd;
574 err = open_loose_object(&fd, id, repo);
575 if (err)
576 goto done;
577 err = read_object_raw_privsep(&outbuf, &size, &hdrlen, outfd,
578 repo, fd);
581 if (hdrlen > size) {
582 err = got_error(GOT_ERR_BAD_OBJ_HDR);
583 goto done;
586 *obj = calloc(1, sizeof(**obj));
587 if (*obj == NULL) {
588 err = got_error_from_errno("calloc");
589 goto done;
592 (*obj)->read_buf = malloc(blocksize);
593 if ((*obj)->read_buf == NULL) {
594 err = got_error_from_errno("malloc");
595 goto done;
598 if (outbuf) {
599 if (close(outfd) == -1) {
600 err = got_error_from_errno("close");
601 goto done;
603 outfd = -1;
604 (*obj)->f = fmemopen(outbuf, hdrlen + size, "r");
605 if ((*obj)->f == NULL) {
606 err = got_error_from_errno("fdopen");
607 goto done;
609 (*obj)->data = outbuf;
610 } else {
611 struct stat sb;
612 if (fstat(outfd, &sb) == -1) {
613 err = got_error_from_errno("fstat");
614 goto done;
617 if (sb.st_size != size) {
618 err = got_error(GOT_ERR_PRIVSEP_LEN);
619 goto done;
622 (*obj)->f = fdopen(outfd, "r");
623 if ((*obj)->f == NULL) {
624 err = got_error_from_errno("fdopen");
625 goto done;
627 outfd = -1;
628 (*obj)->data = NULL;
630 (*obj)->hdrlen = hdrlen;
631 (*obj)->size = size;
632 (*obj)->blocksize = blocksize;
633 done:
634 free(path_packfile);
635 if (err) {
636 if (*obj) {
637 got_object_raw_close(*obj);
638 *obj = NULL;
640 if (outfd != -1)
641 close(outfd);
642 free(outbuf);
644 return err;
647 void
648 got_object_raw_rewind(struct got_raw_object *obj)
650 if (obj->f)
651 rewind(obj->f);
654 size_t
655 got_object_raw_get_hdrlen(struct got_raw_object *obj)
657 return obj->hdrlen;
660 const uint8_t *
661 got_object_raw_get_read_buf(struct got_raw_object *obj)
663 return obj->read_buf;
666 const struct got_error *
667 got_object_raw_read_block(size_t *outlenp, struct got_raw_object *obj)
669 size_t n;
671 n = fread(obj->read_buf, 1, obj->blocksize, obj->f);
672 if (n == 0 && ferror(obj->f))
673 return got_ferror(obj->f, GOT_ERR_IO);
674 *outlenp = n;
675 return NULL;
678 const struct got_error *
679 got_object_raw_close(struct got_raw_object *obj)
681 const struct got_error *err = NULL;
683 free(obj->read_buf);
684 if (obj->f != NULL && fclose(obj->f) == EOF && err == NULL)
685 err = got_error_from_errno("fclose");
686 free(obj->data);
687 free(obj);
688 return err;
691 const struct got_error *
692 got_object_open_by_id_str(struct got_object **obj, struct got_repository *repo,
693 const char *id_str)
695 struct got_object_id id;
697 if (!got_parse_sha1_digest(id.sha1, id_str))
698 return got_error_path(id_str, GOT_ERR_BAD_OBJ_ID_STR);
700 return got_object_open(obj, repo, &id);
703 const struct got_error *
704 got_object_resolve_id_str(struct got_object_id **id,
705 struct got_repository *repo, const char *id_str)
707 const struct got_error *err = NULL;
708 struct got_object *obj;
710 err = got_object_open_by_id_str(&obj, repo, id_str);
711 if (err)
712 return err;
714 *id = got_object_id_dup(got_object_get_id(obj));
715 got_object_close(obj);
716 if (*id == NULL)
717 return got_error_from_errno("got_object_id_dup");
719 return NULL;
722 static const struct got_error *
723 request_packed_commit(struct got_commit_object **commit, struct got_pack *pack,
724 int pack_idx, struct got_object_id *id)
726 const struct got_error *err = NULL;
728 err = got_privsep_send_commit_req(pack->privsep_child->ibuf, -1, id,
729 pack_idx);
730 if (err)
731 return err;
733 err = got_privsep_recv_commit(commit, pack->privsep_child->ibuf);
734 if (err)
735 return err;
737 (*commit)->flags |= GOT_COMMIT_FLAG_PACKED;
738 return NULL;
741 static const struct got_error *
742 read_packed_commit_privsep(struct got_commit_object **commit,
743 struct got_pack *pack, struct got_packidx *packidx, int idx,
744 struct got_object_id *id)
746 const struct got_error *err = NULL;
748 if (pack->privsep_child)
749 return request_packed_commit(commit, pack, idx, id);
751 err = start_pack_privsep_child(pack, packidx);
752 if (err)
753 return err;
755 return request_packed_commit(commit, pack, idx, id);
758 static const struct got_error *
759 request_commit(struct got_commit_object **commit, struct got_repository *repo,
760 int fd)
762 const struct got_error *err = NULL;
763 struct imsgbuf *ibuf;
765 ibuf = repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_COMMIT].ibuf;
767 err = got_privsep_send_commit_req(ibuf, fd, NULL, -1);
768 if (err)
769 return err;
771 return got_privsep_recv_commit(commit, ibuf);
774 static const struct got_error *
775 read_commit_privsep(struct got_commit_object **commit, int obj_fd,
776 struct got_repository *repo)
778 const struct got_error *err;
779 int imsg_fds[2];
780 pid_t pid;
781 struct imsgbuf *ibuf;
783 if (repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_COMMIT].imsg_fd != -1)
784 return request_commit(commit, repo, obj_fd);
786 ibuf = calloc(1, sizeof(*ibuf));
787 if (ibuf == NULL)
788 return got_error_from_errno("calloc");
790 if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_fds) == -1) {
791 err = got_error_from_errno("socketpair");
792 free(ibuf);
793 return err;
796 pid = fork();
797 if (pid == -1) {
798 err = got_error_from_errno("fork");
799 free(ibuf);
800 return err;
802 else if (pid == 0) {
803 got_privsep_exec_child(imsg_fds, GOT_PATH_PROG_READ_COMMIT,
804 repo->path);
805 /* not reached */
808 if (close(imsg_fds[1]) == -1) {
809 err = got_error_from_errno("close");
810 free(ibuf);
811 return err;
813 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_COMMIT].imsg_fd =
814 imsg_fds[0];
815 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_COMMIT].pid = pid;
816 imsg_init(ibuf, imsg_fds[0]);
817 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_COMMIT].ibuf = ibuf;
819 return request_commit(commit, repo, obj_fd);
823 static const struct got_error *
824 open_commit(struct got_commit_object **commit,
825 struct got_repository *repo, struct got_object_id *id, int check_cache)
827 const struct got_error *err = NULL;
828 struct got_packidx *packidx = NULL;
829 int idx;
830 char *path_packfile = NULL;
832 if (check_cache) {
833 *commit = got_repo_get_cached_commit(repo, id);
834 if (*commit != NULL) {
835 (*commit)->refcnt++;
836 return NULL;
838 } else
839 *commit = NULL;
841 err = got_repo_search_packidx(&packidx, &idx, repo, id);
842 if (err == NULL) {
843 struct got_pack *pack = NULL;
845 err = got_packidx_get_packfile_path(&path_packfile, packidx);
846 if (err)
847 return err;
849 pack = got_repo_get_cached_pack(repo, path_packfile);
850 if (pack == NULL) {
851 err = got_repo_cache_pack(&pack, repo, path_packfile,
852 packidx);
853 if (err)
854 goto done;
856 err = read_packed_commit_privsep(commit, pack,
857 packidx, idx, id);
858 } else if (err->code == GOT_ERR_NO_OBJ) {
859 int fd;
861 err = open_loose_object(&fd, id, repo);
862 if (err)
863 return err;
864 err = read_commit_privsep(commit, fd, repo);
867 if (err == NULL) {
868 (*commit)->refcnt++;
869 err = got_repo_cache_commit(repo, id, *commit);
871 done:
872 free(path_packfile);
873 return err;
876 const struct got_error *
877 got_object_open_as_commit(struct got_commit_object **commit,
878 struct got_repository *repo, struct got_object_id *id)
880 *commit = got_repo_get_cached_commit(repo, id);
881 if (*commit != NULL) {
882 (*commit)->refcnt++;
883 return NULL;
886 return open_commit(commit, repo, id, 0);
889 const struct got_error *
890 got_object_commit_open(struct got_commit_object **commit,
891 struct got_repository *repo, struct got_object *obj)
893 return open_commit(commit, repo, got_object_get_id(obj), 1);
896 const struct got_error *
897 got_object_qid_alloc(struct got_object_qid **qid, struct got_object_id *id)
899 const struct got_error *err = NULL;
901 *qid = calloc(1, sizeof(**qid));
902 if (*qid == NULL)
903 return got_error_from_errno("calloc");
905 (*qid)->id = got_object_id_dup(id);
906 if ((*qid)->id == NULL) {
907 err = got_error_from_errno("got_object_id_dup");
908 got_object_qid_free(*qid);
909 *qid = NULL;
910 return err;
913 return NULL;
916 static const struct got_error *
917 request_packed_tree(struct got_tree_object **tree, struct got_pack *pack,
918 int pack_idx, struct got_object_id *id)
920 const struct got_error *err = NULL;
922 err = got_privsep_send_tree_req(pack->privsep_child->ibuf, -1, id,
923 pack_idx);
924 if (err)
925 return err;
927 return got_privsep_recv_tree(tree, pack->privsep_child->ibuf);
930 static const struct got_error *
931 read_packed_tree_privsep(struct got_tree_object **tree,
932 struct got_pack *pack, struct got_packidx *packidx, int idx,
933 struct got_object_id *id)
935 const struct got_error *err = NULL;
937 if (pack->privsep_child)
938 return request_packed_tree(tree, pack, idx, id);
940 err = start_pack_privsep_child(pack, packidx);
941 if (err)
942 return err;
944 return request_packed_tree(tree, pack, idx, id);
947 static const struct got_error *
948 request_tree(struct got_tree_object **tree, struct got_repository *repo,
949 int fd)
951 const struct got_error *err = NULL;
952 struct imsgbuf *ibuf;
954 ibuf = repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TREE].ibuf;
956 err = got_privsep_send_tree_req(ibuf, fd, NULL, -1);
957 if (err)
958 return err;
960 return got_privsep_recv_tree(tree, ibuf);
963 const struct got_error *
964 read_tree_privsep(struct got_tree_object **tree, int obj_fd,
965 struct got_repository *repo)
967 const struct got_error *err;
968 int imsg_fds[2];
969 pid_t pid;
970 struct imsgbuf *ibuf;
972 if (repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TREE].imsg_fd != -1)
973 return request_tree(tree, repo, obj_fd);
975 ibuf = calloc(1, sizeof(*ibuf));
976 if (ibuf == NULL)
977 return got_error_from_errno("calloc");
979 if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_fds) == -1) {
980 err = got_error_from_errno("socketpair");
981 free(ibuf);
982 return err;
985 pid = fork();
986 if (pid == -1) {
987 err = got_error_from_errno("fork");
988 free(ibuf);
989 return err;
991 else if (pid == 0) {
992 got_privsep_exec_child(imsg_fds, GOT_PATH_PROG_READ_TREE,
993 repo->path);
994 /* not reached */
997 if (close(imsg_fds[1]) == -1) {
998 err = got_error_from_errno("close");
999 free(ibuf);
1000 return err;
1002 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TREE].imsg_fd =
1003 imsg_fds[0];
1004 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TREE].pid = pid;
1005 imsg_init(ibuf, imsg_fds[0]);
1006 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TREE].ibuf = ibuf;
1009 return request_tree(tree, repo, obj_fd);
1012 static const struct got_error *
1013 open_tree(struct got_tree_object **tree, struct got_repository *repo,
1014 struct got_object_id *id, int check_cache)
1016 const struct got_error *err = NULL;
1017 struct got_packidx *packidx = NULL;
1018 int idx;
1019 char *path_packfile = NULL;
1021 if (check_cache) {
1022 *tree = got_repo_get_cached_tree(repo, id);
1023 if (*tree != NULL) {
1024 (*tree)->refcnt++;
1025 return NULL;
1027 } else
1028 *tree = NULL;
1030 err = got_repo_search_packidx(&packidx, &idx, repo, id);
1031 if (err == NULL) {
1032 struct got_pack *pack = NULL;
1034 err = got_packidx_get_packfile_path(&path_packfile, packidx);
1035 if (err)
1036 return err;
1038 pack = got_repo_get_cached_pack(repo, path_packfile);
1039 if (pack == NULL) {
1040 err = got_repo_cache_pack(&pack, repo, path_packfile,
1041 packidx);
1042 if (err)
1043 goto done;
1045 err = read_packed_tree_privsep(tree, pack,
1046 packidx, idx, id);
1047 } else if (err->code == GOT_ERR_NO_OBJ) {
1048 int fd;
1050 err = open_loose_object(&fd, id, repo);
1051 if (err)
1052 return err;
1053 err = read_tree_privsep(tree, fd, repo);
1056 if (err == NULL) {
1057 (*tree)->refcnt++;
1058 err = got_repo_cache_tree(repo, id, *tree);
1060 done:
1061 free(path_packfile);
1062 return err;
1065 const struct got_error *
1066 got_object_open_as_tree(struct got_tree_object **tree,
1067 struct got_repository *repo, struct got_object_id *id)
1069 *tree = got_repo_get_cached_tree(repo, id);
1070 if (*tree != NULL) {
1071 (*tree)->refcnt++;
1072 return NULL;
1075 return open_tree(tree, repo, id, 0);
1078 const struct got_error *
1079 got_object_tree_open(struct got_tree_object **tree,
1080 struct got_repository *repo, struct got_object *obj)
1082 return open_tree(tree, repo, got_object_get_id(obj), 1);
1085 int
1086 got_object_tree_get_nentries(struct got_tree_object *tree)
1088 return tree->nentries;
1091 struct got_tree_entry *
1092 got_object_tree_get_first_entry(struct got_tree_object *tree)
1094 return got_object_tree_get_entry(tree, 0);
1097 struct got_tree_entry *
1098 got_object_tree_get_last_entry(struct got_tree_object *tree)
1100 return got_object_tree_get_entry(tree, tree->nentries - 1);
1103 struct got_tree_entry *
1104 got_object_tree_get_entry(struct got_tree_object *tree, int i)
1106 if (i < 0 || i >= tree->nentries)
1107 return NULL;
1108 return &tree->entries[i];
1111 mode_t
1112 got_tree_entry_get_mode(struct got_tree_entry *te)
1114 return te->mode;
1117 const char *
1118 got_tree_entry_get_name(struct got_tree_entry *te)
1120 return &te->name[0];
1123 struct got_object_id *
1124 got_tree_entry_get_id(struct got_tree_entry *te)
1126 return &te->id;
1129 const struct got_error *
1130 got_object_blob_read_to_str(char **s, struct got_blob_object *blob)
1132 const struct got_error *err = NULL;
1133 size_t len, totlen, hdrlen, offset;
1135 *s = NULL;
1137 hdrlen = got_object_blob_get_hdrlen(blob);
1138 totlen = 0;
1139 offset = 0;
1140 do {
1141 char *p;
1143 err = got_object_blob_read_block(&len, blob);
1144 if (err)
1145 return err;
1147 if (len == 0)
1148 break;
1150 totlen += len - hdrlen;
1151 p = realloc(*s, totlen + 1);
1152 if (p == NULL) {
1153 err = got_error_from_errno("realloc");
1154 free(*s);
1155 *s = NULL;
1156 return err;
1158 *s = p;
1159 /* Skip blob object header first time around. */
1160 memcpy(*s + offset,
1161 got_object_blob_get_read_buf(blob) + hdrlen, len - hdrlen);
1162 hdrlen = 0;
1163 offset = totlen;
1164 } while (len > 0);
1166 (*s)[totlen] = '\0';
1167 return NULL;
1170 const struct got_error *
1171 got_tree_entry_get_symlink_target(char **link_target, struct got_tree_entry *te,
1172 struct got_repository *repo)
1174 const struct got_error *err = NULL;
1175 struct got_blob_object *blob = NULL;
1177 *link_target = NULL;
1179 if (!got_object_tree_entry_is_symlink(te))
1180 return got_error(GOT_ERR_TREE_ENTRY_TYPE);
1182 err = got_object_open_as_blob(&blob, repo,
1183 got_tree_entry_get_id(te), PATH_MAX);
1184 if (err)
1185 return err;
1187 err = got_object_blob_read_to_str(link_target, blob);
1188 got_object_blob_close(blob);
1189 if (err) {
1190 free(*link_target);
1191 *link_target = NULL;
1193 return err;
1196 int
1197 got_tree_entry_get_index(struct got_tree_entry *te)
1199 return te->idx;
1202 struct got_tree_entry *
1203 got_tree_entry_get_next(struct got_tree_object *tree,
1204 struct got_tree_entry *te)
1206 return got_object_tree_get_entry(tree, te->idx + 1);
1209 struct got_tree_entry *
1210 got_tree_entry_get_prev(struct got_tree_object *tree,
1211 struct got_tree_entry *te)
1213 return got_object_tree_get_entry(tree, te->idx - 1);
1216 static const struct got_error *
1217 request_packed_blob(uint8_t **outbuf, size_t *size, size_t *hdrlen, int outfd,
1218 struct got_pack *pack, struct got_packidx *packidx, int idx,
1219 struct got_object_id *id)
1221 const struct got_error *err = NULL;
1222 int outfd_child;
1223 int basefd, accumfd; /* temporary files for delta application */
1225 basefd = got_opentempfd();
1226 if (basefd == -1)
1227 return got_error_from_errno("got_opentempfd");
1228 accumfd = got_opentempfd();
1229 if (accumfd == -1)
1230 return got_error_from_errno("got_opentempfd");
1232 outfd_child = dup(outfd);
1233 if (outfd_child == -1)
1234 return got_error_from_errno("dup");
1236 err = got_privsep_send_blob_req(pack->privsep_child->ibuf, -1, id, idx);
1237 if (err)
1238 return err;
1240 err = got_privsep_send_blob_outfd(pack->privsep_child->ibuf,
1241 outfd_child);
1242 if (err) {
1243 close(basefd);
1244 close(accumfd);
1245 return err;
1248 err = got_privsep_send_tmpfd(pack->privsep_child->ibuf,
1249 basefd);
1250 if (err) {
1251 close(accumfd);
1252 return err;
1255 err = got_privsep_send_tmpfd(pack->privsep_child->ibuf,
1256 accumfd);
1257 if (err)
1258 return err;
1260 err = got_privsep_recv_blob(outbuf, size, hdrlen,
1261 pack->privsep_child->ibuf);
1262 if (err)
1263 return err;
1265 if (lseek(outfd, SEEK_SET, 0) == -1)
1266 err = got_error_from_errno("lseek");
1268 return err;
1271 static const struct got_error *
1272 read_packed_blob_privsep(uint8_t **outbuf, size_t *size, size_t *hdrlen,
1273 int outfd, struct got_pack *pack, struct got_packidx *packidx, int idx,
1274 struct got_object_id *id)
1276 const struct got_error *err = NULL;
1278 if (pack->privsep_child == NULL) {
1279 err = start_pack_privsep_child(pack, packidx);
1280 if (err)
1281 return err;
1284 return request_packed_blob(outbuf, size, hdrlen, outfd, pack, packidx,
1285 idx, id);
1288 static const struct got_error *
1289 request_blob(uint8_t **outbuf, size_t *size, size_t *hdrlen, int outfd,
1290 int infd, struct imsgbuf *ibuf)
1292 const struct got_error *err = NULL;
1293 int outfd_child;
1295 outfd_child = dup(outfd);
1296 if (outfd_child == -1)
1297 return got_error_from_errno("dup");
1299 err = got_privsep_send_blob_req(ibuf, infd, NULL, -1);
1300 if (err)
1301 return err;
1303 err = got_privsep_send_blob_outfd(ibuf, outfd_child);
1304 if (err)
1305 return err;
1307 err = got_privsep_recv_blob(outbuf, size, hdrlen, ibuf);
1308 if (err)
1309 return err;
1311 if (lseek(outfd, SEEK_SET, 0) == -1)
1312 return got_error_from_errno("lseek");
1314 return err;
1317 static const struct got_error *
1318 read_blob_privsep(uint8_t **outbuf, size_t *size, size_t *hdrlen,
1319 int outfd, int infd, struct got_repository *repo)
1321 const struct got_error *err;
1322 int imsg_fds[2];
1323 pid_t pid;
1324 struct imsgbuf *ibuf;
1326 if (repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_BLOB].imsg_fd != -1) {
1327 ibuf = repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_BLOB].ibuf;
1328 return request_blob(outbuf, size, hdrlen, outfd, infd, ibuf);
1331 ibuf = calloc(1, sizeof(*ibuf));
1332 if (ibuf == NULL)
1333 return got_error_from_errno("calloc");
1335 if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_fds) == -1) {
1336 err = got_error_from_errno("socketpair");
1337 free(ibuf);
1338 return err;
1341 pid = fork();
1342 if (pid == -1) {
1343 err = got_error_from_errno("fork");
1344 free(ibuf);
1345 return err;
1347 else if (pid == 0) {
1348 got_privsep_exec_child(imsg_fds, GOT_PATH_PROG_READ_BLOB,
1349 repo->path);
1350 /* not reached */
1353 if (close(imsg_fds[1]) == -1) {
1354 err = got_error_from_errno("close");
1355 free(ibuf);
1356 return err;
1358 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_BLOB].imsg_fd =
1359 imsg_fds[0];
1360 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_BLOB].pid = pid;
1361 imsg_init(ibuf, imsg_fds[0]);
1362 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_BLOB].ibuf = ibuf;
1364 return request_blob(outbuf, size, hdrlen, outfd, infd, ibuf);
1367 static const struct got_error *
1368 open_blob(struct got_blob_object **blob, struct got_repository *repo,
1369 struct got_object_id *id, size_t blocksize)
1371 const struct got_error *err = NULL;
1372 struct got_packidx *packidx = NULL;
1373 int idx;
1374 char *path_packfile = NULL;
1375 uint8_t *outbuf;
1376 int outfd;
1377 size_t size, hdrlen;
1378 struct stat sb;
1380 *blob = calloc(1, sizeof(**blob));
1381 if (*blob == NULL)
1382 return got_error_from_errno("calloc");
1384 outfd = got_opentempfd();
1385 if (outfd == -1)
1386 return got_error_from_errno("got_opentempfd");
1388 (*blob)->read_buf = malloc(blocksize);
1389 if ((*blob)->read_buf == NULL) {
1390 err = got_error_from_errno("malloc");
1391 goto done;
1394 err = got_repo_search_packidx(&packidx, &idx, repo, id);
1395 if (err == NULL) {
1396 struct got_pack *pack = NULL;
1398 err = got_packidx_get_packfile_path(&path_packfile, packidx);
1399 if (err)
1400 goto done;
1402 pack = got_repo_get_cached_pack(repo, path_packfile);
1403 if (pack == NULL) {
1404 err = got_repo_cache_pack(&pack, repo, path_packfile,
1405 packidx);
1406 if (err)
1407 goto done;
1409 err = read_packed_blob_privsep(&outbuf, &size, &hdrlen, outfd,
1410 pack, packidx, idx, id);
1411 } else if (err->code == GOT_ERR_NO_OBJ) {
1412 int infd;
1414 err = open_loose_object(&infd, id, repo);
1415 if (err)
1416 goto done;
1417 err = read_blob_privsep(&outbuf, &size, &hdrlen, outfd, infd,
1418 repo);
1420 if (err)
1421 goto done;
1423 if (hdrlen > size) {
1424 err = got_error(GOT_ERR_BAD_OBJ_HDR);
1425 goto done;
1428 if (outbuf) {
1429 if (close(outfd) == -1 && err == NULL)
1430 err = got_error_from_errno("close");
1431 outfd = -1;
1432 (*blob)->f = fmemopen(outbuf, size, "rb");
1433 if ((*blob)->f == NULL) {
1434 err = got_error_from_errno("fmemopen");
1435 free(outbuf);
1436 goto done;
1438 (*blob)->data = outbuf;
1439 } else {
1440 if (fstat(outfd, &sb) == -1) {
1441 err = got_error_from_errno("fstat");
1442 goto done;
1445 if (sb.st_size != size) {
1446 err = got_error(GOT_ERR_PRIVSEP_LEN);
1447 goto done;
1450 (*blob)->f = fdopen(outfd, "rb");
1451 if ((*blob)->f == NULL) {
1452 err = got_error_from_errno("fdopen");
1453 close(outfd);
1454 outfd = -1;
1455 goto done;
1459 (*blob)->hdrlen = hdrlen;
1460 (*blob)->blocksize = blocksize;
1461 memcpy(&(*blob)->id.sha1, id->sha1, SHA1_DIGEST_LENGTH);
1463 done:
1464 free(path_packfile);
1465 if (err) {
1466 if (*blob) {
1467 got_object_blob_close(*blob);
1468 *blob = NULL;
1469 } else if (outfd != -1)
1470 close(outfd);
1472 return err;
1475 const struct got_error *
1476 got_object_open_as_blob(struct got_blob_object **blob,
1477 struct got_repository *repo, struct got_object_id *id,
1478 size_t blocksize)
1480 return open_blob(blob, repo, id, blocksize);
1483 const struct got_error *
1484 got_object_blob_open(struct got_blob_object **blob,
1485 struct got_repository *repo, struct got_object *obj, size_t blocksize)
1487 return open_blob(blob, repo, got_object_get_id(obj), blocksize);
1490 const struct got_error *
1491 got_object_blob_close(struct got_blob_object *blob)
1493 const struct got_error *err = NULL;
1494 free(blob->read_buf);
1495 if (blob->f && fclose(blob->f) == EOF)
1496 err = got_error_from_errno("fclose");
1497 free(blob->data);
1498 free(blob);
1499 return err;
1502 void
1503 got_object_blob_rewind(struct got_blob_object *blob)
1505 if (blob->f)
1506 rewind(blob->f);
1509 char *
1510 got_object_blob_id_str(struct got_blob_object *blob, char *buf, size_t size)
1512 return got_sha1_digest_to_str(blob->id.sha1, buf, size);
1515 size_t
1516 got_object_blob_get_hdrlen(struct got_blob_object *blob)
1518 return blob->hdrlen;
1521 const uint8_t *
1522 got_object_blob_get_read_buf(struct got_blob_object *blob)
1524 return blob->read_buf;
1527 const struct got_error *
1528 got_object_blob_read_block(size_t *outlenp, struct got_blob_object *blob)
1530 size_t n;
1532 n = fread(blob->read_buf, 1, blob->blocksize, blob->f);
1533 if (n == 0 && ferror(blob->f))
1534 return got_ferror(blob->f, GOT_ERR_IO);
1535 *outlenp = n;
1536 return NULL;
1539 const struct got_error *
1540 got_object_blob_dump_to_file(off_t *filesize, int *nlines,
1541 off_t **line_offsets, FILE *outfile, struct got_blob_object *blob)
1543 const struct got_error *err = NULL;
1544 size_t n, len, hdrlen;
1545 const uint8_t *buf;
1546 int i;
1547 const int alloc_chunksz = 512;
1548 size_t nalloc = 0;
1549 off_t off = 0, total_len = 0;
1551 if (line_offsets)
1552 *line_offsets = NULL;
1553 if (filesize)
1554 *filesize = 0;
1555 if (nlines)
1556 *nlines = 0;
1558 hdrlen = got_object_blob_get_hdrlen(blob);
1559 do {
1560 err = got_object_blob_read_block(&len, blob);
1561 if (err)
1562 return err;
1563 if (len == 0)
1564 break;
1565 buf = got_object_blob_get_read_buf(blob);
1566 i = hdrlen;
1567 if (nlines) {
1568 if (line_offsets && *line_offsets == NULL) {
1569 /* Have some data but perhaps no '\n'. */
1570 *nlines = 1;
1571 nalloc = alloc_chunksz;
1572 *line_offsets = calloc(nalloc,
1573 sizeof(**line_offsets));
1574 if (*line_offsets == NULL)
1575 return got_error_from_errno("calloc");
1577 /* Skip forward over end of first line. */
1578 while (i < len) {
1579 if (buf[i] == '\n')
1580 break;
1581 i++;
1584 /* Scan '\n' offsets in remaining chunk of data. */
1585 while (i < len) {
1586 if (buf[i] != '\n') {
1587 i++;
1588 continue;
1590 (*nlines)++;
1591 if (line_offsets && nalloc < *nlines) {
1592 size_t n = *nlines + alloc_chunksz;
1593 off_t *o = recallocarray(*line_offsets,
1594 nalloc, n, sizeof(**line_offsets));
1595 if (o == NULL) {
1596 free(*line_offsets);
1597 *line_offsets = NULL;
1598 return got_error_from_errno(
1599 "recallocarray");
1601 *line_offsets = o;
1602 nalloc = n;
1604 if (line_offsets) {
1605 off = total_len + i - hdrlen + 1;
1606 (*line_offsets)[*nlines - 1] = off;
1608 i++;
1611 /* Skip blob object header first time around. */
1612 n = fwrite(buf + hdrlen, 1, len - hdrlen, outfile);
1613 if (n != len - hdrlen)
1614 return got_ferror(outfile, GOT_ERR_IO);
1615 total_len += len - hdrlen;
1616 hdrlen = 0;
1617 } while (len != 0);
1619 if (fflush(outfile) != 0)
1620 return got_error_from_errno("fflush");
1621 rewind(outfile);
1623 if (filesize)
1624 *filesize = total_len;
1626 return NULL;
1629 static const struct got_error *
1630 request_packed_tag(struct got_tag_object **tag, struct got_pack *pack,
1631 int pack_idx, struct got_object_id *id)
1633 const struct got_error *err = NULL;
1635 err = got_privsep_send_tag_req(pack->privsep_child->ibuf, -1, id,
1636 pack_idx);
1637 if (err)
1638 return err;
1640 return got_privsep_recv_tag(tag, pack->privsep_child->ibuf);
1643 static const struct got_error *
1644 read_packed_tag_privsep(struct got_tag_object **tag,
1645 struct got_pack *pack, struct got_packidx *packidx, int idx,
1646 struct got_object_id *id)
1648 const struct got_error *err = NULL;
1650 if (pack->privsep_child)
1651 return request_packed_tag(tag, pack, idx, id);
1653 err = start_pack_privsep_child(pack, packidx);
1654 if (err)
1655 return err;
1657 return request_packed_tag(tag, pack, idx, id);
1660 static const struct got_error *
1661 request_tag(struct got_tag_object **tag, struct got_repository *repo,
1662 int fd)
1664 const struct got_error *err = NULL;
1665 struct imsgbuf *ibuf;
1667 ibuf = repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TAG].ibuf;
1669 err = got_privsep_send_tag_req(ibuf, fd, NULL, -1);
1670 if (err)
1671 return err;
1673 return got_privsep_recv_tag(tag, ibuf);
1676 static const struct got_error *
1677 read_tag_privsep(struct got_tag_object **tag, int obj_fd,
1678 struct got_repository *repo)
1680 const struct got_error *err;
1681 int imsg_fds[2];
1682 pid_t pid;
1683 struct imsgbuf *ibuf;
1685 if (repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TAG].imsg_fd != -1)
1686 return request_tag(tag, repo, obj_fd);
1688 ibuf = calloc(1, sizeof(*ibuf));
1689 if (ibuf == NULL)
1690 return got_error_from_errno("calloc");
1692 if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_fds) == -1) {
1693 err = got_error_from_errno("socketpair");
1694 free(ibuf);
1695 return err;
1698 pid = fork();
1699 if (pid == -1) {
1700 err = got_error_from_errno("fork");
1701 free(ibuf);
1702 return err;
1704 else if (pid == 0) {
1705 got_privsep_exec_child(imsg_fds, GOT_PATH_PROG_READ_TAG,
1706 repo->path);
1707 /* not reached */
1710 if (close(imsg_fds[1]) == -1) {
1711 err = got_error_from_errno("close");
1712 free(ibuf);
1713 return err;
1715 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TAG].imsg_fd =
1716 imsg_fds[0];
1717 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TAG].pid = pid;
1718 imsg_init(ibuf, imsg_fds[0]);
1719 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TAG].ibuf = ibuf;
1721 return request_tag(tag, repo, obj_fd);
1724 static const struct got_error *
1725 open_tag(struct got_tag_object **tag, struct got_repository *repo,
1726 struct got_object_id *id, int check_cache)
1728 const struct got_error *err = NULL;
1729 struct got_packidx *packidx = NULL;
1730 int idx;
1731 char *path_packfile = NULL;
1732 struct got_object *obj = NULL;
1733 int obj_type = GOT_OBJ_TYPE_ANY;
1735 if (check_cache) {
1736 *tag = got_repo_get_cached_tag(repo, id);
1737 if (*tag != NULL) {
1738 (*tag)->refcnt++;
1739 return NULL;
1741 } else
1742 *tag = NULL;
1744 err = got_repo_search_packidx(&packidx, &idx, repo, id);
1745 if (err == NULL) {
1746 struct got_pack *pack = NULL;
1748 err = got_packidx_get_packfile_path(&path_packfile, packidx);
1749 if (err)
1750 return err;
1752 pack = got_repo_get_cached_pack(repo, path_packfile);
1753 if (pack == NULL) {
1754 err = got_repo_cache_pack(&pack, repo, path_packfile,
1755 packidx);
1756 if (err)
1757 goto done;
1760 /* Beware of "lightweight" tags: Check object type first. */
1761 err = read_packed_object_privsep(&obj, repo, pack, packidx,
1762 idx, id);
1763 if (err)
1764 goto done;
1765 obj_type = obj->type;
1766 got_object_close(obj);
1767 if (obj_type != GOT_OBJ_TYPE_TAG) {
1768 err = got_error(GOT_ERR_OBJ_TYPE);
1769 goto done;
1771 err = read_packed_tag_privsep(tag, pack, packidx, idx, id);
1772 } else if (err->code == GOT_ERR_NO_OBJ) {
1773 int fd;
1775 err = open_loose_object(&fd, id, repo);
1776 if (err)
1777 return err;
1778 err = read_object_header_privsep(&obj, repo, fd);
1779 if (err)
1780 return err;
1781 obj_type = obj->type;
1782 got_object_close(obj);
1783 if (obj_type != GOT_OBJ_TYPE_TAG)
1784 return got_error(GOT_ERR_OBJ_TYPE);
1786 err = open_loose_object(&fd, id, repo);
1787 if (err)
1788 return err;
1789 err = read_tag_privsep(tag, fd, repo);
1792 if (err == NULL) {
1793 (*tag)->refcnt++;
1794 err = got_repo_cache_tag(repo, id, *tag);
1796 done:
1797 free(path_packfile);
1798 return err;
1801 const struct got_error *
1802 got_object_open_as_tag(struct got_tag_object **tag,
1803 struct got_repository *repo, struct got_object_id *id)
1805 *tag = got_repo_get_cached_tag(repo, id);
1806 if (*tag != NULL) {
1807 (*tag)->refcnt++;
1808 return NULL;
1811 return open_tag(tag, repo, id, 0);
1814 const struct got_error *
1815 got_object_tag_open(struct got_tag_object **tag,
1816 struct got_repository *repo, struct got_object *obj)
1818 return open_tag(tag, repo, got_object_get_id(obj), 1);
1821 const char *
1822 got_object_tag_get_name(struct got_tag_object *tag)
1824 return tag->tag;
1827 int
1828 got_object_tag_get_object_type(struct got_tag_object *tag)
1830 return tag->obj_type;
1833 struct got_object_id *
1834 got_object_tag_get_object_id(struct got_tag_object *tag)
1836 return &tag->id;
1839 time_t
1840 got_object_tag_get_tagger_time(struct got_tag_object *tag)
1842 return tag->tagger_time;
1845 time_t
1846 got_object_tag_get_tagger_gmtoff(struct got_tag_object *tag)
1848 return tag->tagger_gmtoff;
1851 const char *
1852 got_object_tag_get_tagger(struct got_tag_object *tag)
1854 return tag->tagger;
1857 const char *
1858 got_object_tag_get_message(struct got_tag_object *tag)
1860 return tag->tagmsg;
1863 static struct got_tree_entry *
1864 find_entry_by_name(struct got_tree_object *tree, const char *name, size_t len)
1866 int i;
1868 /* Note that tree entries are sorted in strncmp() order. */
1869 for (i = 0; i < tree->nentries; i++) {
1870 struct got_tree_entry *te = &tree->entries[i];
1871 int cmp = strncmp(te->name, name, len);
1872 if (cmp < 0)
1873 continue;
1874 if (cmp > 0)
1875 break;
1876 if (te->name[len] == '\0')
1877 return te;
1879 return NULL;
1882 struct got_tree_entry *
1883 got_object_tree_find_entry(struct got_tree_object *tree, const char *name)
1885 return find_entry_by_name(tree, name, strlen(name));
1888 const struct got_error *
1889 got_object_id_by_path(struct got_object_id **id, struct got_repository *repo,
1890 struct got_object_id *commit_id, const char *path)
1892 const struct got_error *err = NULL;
1893 struct got_commit_object *commit = NULL;
1894 struct got_tree_object *tree = NULL;
1895 struct got_tree_entry *te = NULL;
1896 const char *seg, *s;
1897 size_t seglen;
1899 *id = NULL;
1901 err = got_object_open_as_commit(&commit, repo, commit_id);
1902 if (err)
1903 goto done;
1905 /* Handle opening of root of commit's tree. */
1906 if (got_path_is_root_dir(path)) {
1907 *id = got_object_id_dup(commit->tree_id);
1908 if (*id == NULL)
1909 err = got_error_from_errno("got_object_id_dup");
1910 goto done;
1913 err = got_object_open_as_tree(&tree, repo, commit->tree_id);
1914 if (err)
1915 goto done;
1917 s = path;
1918 while (s[0] == '/')
1919 s++;
1920 seg = s;
1921 seglen = 0;
1922 while (*s) {
1923 struct got_tree_object *next_tree;
1925 if (*s != '/') {
1926 s++;
1927 seglen++;
1928 if (*s)
1929 continue;
1932 te = find_entry_by_name(tree, seg, seglen);
1933 if (te == NULL) {
1934 err = got_error_path(path, GOT_ERR_NO_TREE_ENTRY);
1935 goto done;
1938 if (*s == '\0')
1939 break;
1941 seg = s + 1;
1942 seglen = 0;
1943 s++;
1944 if (*s) {
1945 err = got_object_open_as_tree(&next_tree, repo,
1946 &te->id);
1947 te = NULL;
1948 if (err)
1949 goto done;
1950 got_object_tree_close(tree);
1951 tree = next_tree;
1955 if (te) {
1956 *id = got_object_id_dup(&te->id);
1957 if (*id == NULL)
1958 return got_error_from_errno("got_object_id_dup");
1959 } else
1960 err = got_error_path(path, GOT_ERR_NO_TREE_ENTRY);
1961 done:
1962 if (commit)
1963 got_object_commit_close(commit);
1964 if (tree)
1965 got_object_tree_close(tree);
1966 return err;
1970 * Normalize file mode bits to avoid false positive tree entry differences
1971 * in case tree entries have unexpected mode bits set.
1973 static mode_t
1974 normalize_mode_for_comparison(mode_t mode)
1977 * For directories, the only relevant bit is the IFDIR bit.
1978 * This allows us to detect paths changing from a directory
1979 * to a file and vice versa.
1981 if (S_ISDIR(mode))
1982 return mode & S_IFDIR;
1985 * For symlinks, the only relevant bit is the IFLNK bit.
1986 * This allows us to detect paths changing from a symlinks
1987 * to a file or directory and vice versa.
1989 if (S_ISLNK(mode))
1990 return mode & S_IFLNK;
1992 /* For files, the only change we care about is the executable bit. */
1993 return mode & S_IXUSR;
1996 const struct got_error *
1997 got_object_tree_path_changed(int *changed,
1998 struct got_tree_object *tree01, struct got_tree_object *tree02,
1999 const char *path, struct got_repository *repo)
2001 const struct got_error *err = NULL;
2002 struct got_tree_object *tree1 = NULL, *tree2 = NULL;
2003 struct got_tree_entry *te1 = NULL, *te2 = NULL;
2004 const char *seg, *s;
2005 size_t seglen;
2007 *changed = 0;
2009 /* We not do support comparing the root path. */
2010 if (got_path_is_root_dir(path))
2011 return got_error_path(path, GOT_ERR_BAD_PATH);
2013 tree1 = tree01;
2014 tree2 = tree02;
2015 s = path;
2016 while (*s == '/')
2017 s++;
2018 seg = s;
2019 seglen = 0;
2020 while (*s) {
2021 struct got_tree_object *next_tree1, *next_tree2;
2022 mode_t mode1, mode2;
2024 if (*s != '/') {
2025 s++;
2026 seglen++;
2027 if (*s)
2028 continue;
2031 te1 = find_entry_by_name(tree1, seg, seglen);
2032 if (te1 == NULL) {
2033 err = got_error(GOT_ERR_NO_OBJ);
2034 goto done;
2037 if (tree2)
2038 te2 = find_entry_by_name(tree2, seg, seglen);
2040 if (te2) {
2041 mode1 = normalize_mode_for_comparison(te1->mode);
2042 mode2 = normalize_mode_for_comparison(te2->mode);
2043 if (mode1 != mode2) {
2044 *changed = 1;
2045 goto done;
2048 if (got_object_id_cmp(&te1->id, &te2->id) == 0) {
2049 *changed = 0;
2050 goto done;
2054 if (*s == '\0') { /* final path element */
2055 *changed = 1;
2056 goto done;
2059 seg = s + 1;
2060 s++;
2061 seglen = 0;
2062 if (*s) {
2063 err = got_object_open_as_tree(&next_tree1, repo,
2064 &te1->id);
2065 te1 = NULL;
2066 if (err)
2067 goto done;
2068 if (tree1 != tree01)
2069 got_object_tree_close(tree1);
2070 tree1 = next_tree1;
2072 if (te2) {
2073 err = got_object_open_as_tree(&next_tree2, repo,
2074 &te2->id);
2075 te2 = NULL;
2076 if (err)
2077 goto done;
2078 if (tree2 != tree02)
2079 got_object_tree_close(tree2);
2080 tree2 = next_tree2;
2081 } else if (tree2) {
2082 if (tree2 != tree02)
2083 got_object_tree_close(tree2);
2084 tree2 = NULL;
2088 done:
2089 if (tree1 && tree1 != tree01)
2090 got_object_tree_close(tree1);
2091 if (tree2 && tree2 != tree02)
2092 got_object_tree_close(tree2);
2093 return err;
2096 const struct got_error *
2097 got_object_tree_entry_dup(struct got_tree_entry **new_te,
2098 struct got_tree_entry *te)
2100 const struct got_error *err = NULL;
2102 *new_te = calloc(1, sizeof(**new_te));
2103 if (*new_te == NULL)
2104 return got_error_from_errno("calloc");
2106 (*new_te)->mode = te->mode;
2107 memcpy((*new_te)->name, te->name, sizeof((*new_te)->name));
2108 memcpy(&(*new_te)->id, &te->id, sizeof((*new_te)->id));
2109 return err;
2112 int
2113 got_object_tree_entry_is_submodule(struct got_tree_entry *te)
2115 return (te->mode & S_IFMT) == (S_IFDIR | S_IFLNK);
2118 int
2119 got_object_tree_entry_is_symlink(struct got_tree_entry *te)
2121 /* S_IFDIR check avoids confusing symlinks with submodules. */
2122 return ((te->mode & (S_IFDIR | S_IFLNK)) == S_IFLNK);
2125 static const struct got_error *
2126 resolve_symlink(char **link_target, const char *path,
2127 struct got_object_id *commit_id, struct got_repository *repo)
2129 const struct got_error *err = NULL;
2130 char buf[PATH_MAX];
2131 char *name, *parent_path = NULL;
2132 struct got_object_id *tree_obj_id = NULL;
2133 struct got_tree_object *tree = NULL;
2134 struct got_tree_entry *te = NULL;
2136 *link_target = NULL;
2138 if (strlcpy(buf, path, sizeof(buf)) >= sizeof(buf))
2139 return got_error(GOT_ERR_NO_SPACE);
2141 name = basename(buf);
2142 if (name == NULL)
2143 return got_error_from_errno2("basename", path);
2145 err = got_path_dirname(&parent_path, path);
2146 if (err)
2147 return err;
2149 err = got_object_id_by_path(&tree_obj_id, repo, commit_id,
2150 parent_path);
2151 if (err) {
2152 if (err->code == GOT_ERR_NO_TREE_ENTRY) {
2153 /* Display the complete path in error message. */
2154 err = got_error_path(path, err->code);
2156 goto done;
2159 err = got_object_open_as_tree(&tree, repo, tree_obj_id);
2160 if (err)
2161 goto done;
2163 te = got_object_tree_find_entry(tree, name);
2164 if (te == NULL) {
2165 err = got_error_path(path, GOT_ERR_NO_TREE_ENTRY);
2166 goto done;
2169 if (got_object_tree_entry_is_symlink(te)) {
2170 err = got_tree_entry_get_symlink_target(link_target, te, repo);
2171 if (err)
2172 goto done;
2173 if (!got_path_is_absolute(*link_target)) {
2174 char *abspath;
2175 if (asprintf(&abspath, "%s/%s", parent_path,
2176 *link_target) == -1) {
2177 err = got_error_from_errno("asprintf");
2178 goto done;
2180 free(*link_target);
2181 *link_target = malloc(PATH_MAX);
2182 if (*link_target == NULL) {
2183 err = got_error_from_errno("malloc");
2184 goto done;
2186 err = got_canonpath(abspath, *link_target, PATH_MAX);
2187 free(abspath);
2188 if (err)
2189 goto done;
2192 done:
2193 free(tree_obj_id);
2194 if (tree)
2195 got_object_tree_close(tree);
2196 if (err) {
2197 free(*link_target);
2198 *link_target = NULL;
2200 return err;
2203 const struct got_error *
2204 got_object_resolve_symlinks(char **link_target, const char *path,
2205 struct got_object_id *commit_id, struct got_repository *repo)
2207 const struct got_error *err = NULL;
2208 char *next_target = NULL;
2209 int max_recursion = 40; /* matches Git */
2211 *link_target = NULL;
2213 do {
2214 err = resolve_symlink(&next_target,
2215 *link_target ? *link_target : path, commit_id, repo);
2216 if (err)
2217 break;
2218 if (next_target) {
2219 free(*link_target);
2220 if (--max_recursion == 0) {
2221 err = got_error_path(path, GOT_ERR_RECURSION);
2222 *link_target = NULL;
2223 break;
2225 *link_target = next_target;
2227 } while (next_target);
2229 return err;
2232 const struct got_error *
2233 got_traverse_packed_commits(struct got_object_id_queue *traversed_commits,
2234 struct got_object_id *commit_id, const char *path,
2235 struct got_repository *repo)
2237 const struct got_error *err = NULL;
2238 struct got_pack *pack = NULL;
2239 struct got_packidx *packidx = NULL;
2240 char *path_packfile = NULL;
2241 struct got_commit_object *changed_commit = NULL;
2242 struct got_object_id *changed_commit_id = NULL;
2243 int idx;
2245 err = got_repo_search_packidx(&packidx, &idx, repo, commit_id);
2246 if (err) {
2247 if (err->code != GOT_ERR_NO_OBJ)
2248 return err;
2249 return NULL;
2252 err = got_packidx_get_packfile_path(&path_packfile, packidx);
2253 if (err)
2254 return err;
2256 pack = got_repo_get_cached_pack(repo, path_packfile);
2257 if (pack == NULL) {
2258 err = got_repo_cache_pack(&pack, repo, path_packfile, packidx);
2259 if (err)
2260 goto done;
2263 if (pack->privsep_child == NULL) {
2264 err = start_pack_privsep_child(pack, packidx);
2265 if (err)
2266 goto done;
2269 err = got_privsep_send_commit_traversal_request(
2270 pack->privsep_child->ibuf, commit_id, idx, path);
2271 if (err)
2272 goto done;
2274 err = got_privsep_recv_traversed_commits(&changed_commit,
2275 &changed_commit_id, traversed_commits, pack->privsep_child->ibuf);
2276 if (err)
2277 goto done;
2279 if (changed_commit) {
2281 * Cache the commit in which the path was changed.
2282 * This commit might be opened again soon.
2284 changed_commit->refcnt++;
2285 err = got_repo_cache_commit(repo, changed_commit_id,
2286 changed_commit);
2287 got_object_commit_close(changed_commit);
2289 done:
2290 free(path_packfile);
2291 free(changed_commit_id);
2292 return err;