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 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 int fd;
495 *obj = got_repo_get_cached_object(repo, id);
496 if (*obj != NULL) {
497 (*obj)->refcnt++;
498 return NULL;
501 err = open_packed_object(obj, id, repo);
502 if (err && err->code != GOT_ERR_NO_OBJ)
503 return err;
504 if (*obj) {
505 (*obj)->refcnt++;
506 return got_repo_cache_object(repo, id, *obj);
509 err = got_object_open_loose_fd(&fd, id, repo);
510 if (err) {
511 if (err->code == GOT_ERR_ERRNO && errno == ENOENT)
512 err = got_error_no_obj(id);
513 return err;
516 err = read_object_header_privsep(obj, repo, fd);
517 if (err)
518 return err;
520 memcpy((*obj)->id.sha1, id->sha1, SHA1_DIGEST_LENGTH);
522 (*obj)->refcnt++;
523 return got_repo_cache_object(repo, id, *obj);
526 const struct got_error *
527 got_object_raw_open(struct got_raw_object **obj, struct got_repository *repo,
528 struct got_object_id *id, size_t blocksize)
530 const struct got_error *err = NULL;
531 struct got_packidx *packidx = NULL;
532 int idx;
533 uint8_t *outbuf = NULL;
534 int outfd = -1;
535 off_t size = 0;
536 size_t hdrlen = 0;
537 char *path_packfile = NULL;
539 *obj = NULL;
541 outfd = got_opentempfd();
542 if (outfd == -1)
543 return got_error_from_errno("got_opentempfd");
545 err = got_repo_search_packidx(&packidx, &idx, repo, id);
546 if (err == NULL) {
547 struct got_pack *pack = NULL;
549 err = got_packidx_get_packfile_path(&path_packfile, packidx);
550 if (err)
551 goto done;
553 pack = got_repo_get_cached_pack(repo, path_packfile);
554 if (pack == NULL) {
555 err = got_repo_cache_pack(&pack, repo, path_packfile,
556 packidx);
557 if (err)
558 goto done;
560 err = read_packed_object_raw_privsep(&outbuf, &size, &hdrlen,
561 outfd, pack, packidx, idx, id);
562 } else if (err->code == GOT_ERR_NO_OBJ) {
563 int fd;
565 err = got_object_open_loose_fd(&fd, id, repo);
566 if (err)
567 goto done;
568 err = read_object_raw_privsep(&outbuf, &size, &hdrlen, outfd,
569 repo, fd);
572 *obj = calloc(1, sizeof(**obj));
573 if (*obj == NULL) {
574 err = got_error_from_errno("calloc");
575 goto done;
578 (*obj)->read_buf = malloc(blocksize);
579 if ((*obj)->read_buf == NULL) {
580 err = got_error_from_errno("malloc");
581 goto done;
584 if (outbuf) {
585 if (close(outfd) == -1) {
586 err = got_error_from_errno("close");
587 goto done;
589 outfd = -1;
590 (*obj)->f = fmemopen(outbuf, hdrlen + size, "r");
591 if ((*obj)->f == NULL) {
592 err = got_error_from_errno("fdopen");
593 goto done;
595 (*obj)->data = outbuf;
596 } else {
597 struct stat sb;
598 if (fstat(outfd, &sb) == -1) {
599 err = got_error_from_errno("fstat");
600 goto done;
603 if (sb.st_size != hdrlen + size) {
604 err = got_error(GOT_ERR_PRIVSEP_LEN);
605 goto done;
608 (*obj)->f = fdopen(outfd, "r");
609 if ((*obj)->f == NULL) {
610 err = got_error_from_errno("fdopen");
611 goto done;
613 outfd = -1;
614 (*obj)->data = NULL;
616 (*obj)->hdrlen = hdrlen;
617 (*obj)->size = size;
618 (*obj)->blocksize = blocksize;
619 done:
620 free(path_packfile);
621 if (err) {
622 if (*obj) {
623 got_object_raw_close(*obj);
624 *obj = NULL;
626 if (outfd != -1)
627 close(outfd);
628 free(outbuf);
630 return err;
633 void
634 got_object_raw_rewind(struct got_raw_object *obj)
636 if (obj->f)
637 rewind(obj->f);
640 size_t
641 got_object_raw_get_hdrlen(struct got_raw_object *obj)
643 return obj->hdrlen;
646 const uint8_t *
647 got_object_raw_get_read_buf(struct got_raw_object *obj)
649 return obj->read_buf;
652 const struct got_error *
653 got_object_raw_read_block(size_t *outlenp, struct got_raw_object *obj)
655 size_t n;
657 n = fread(obj->read_buf, 1, obj->blocksize, obj->f);
658 if (n == 0 && ferror(obj->f))
659 return got_ferror(obj->f, GOT_ERR_IO);
660 *outlenp = n;
661 return NULL;
664 const struct got_error *
665 got_object_raw_close(struct got_raw_object *obj)
667 const struct got_error *err = NULL;
669 free(obj->read_buf);
670 if (obj->f != NULL && fclose(obj->f) == EOF && err == NULL)
671 err = got_error_from_errno("fclose");
672 free(obj->data);
673 free(obj);
674 return err;
677 const struct got_error *
678 got_object_open_by_id_str(struct got_object **obj, struct got_repository *repo,
679 const char *id_str)
681 struct got_object_id id;
683 if (!got_parse_sha1_digest(id.sha1, id_str))
684 return got_error_path(id_str, GOT_ERR_BAD_OBJ_ID_STR);
686 return got_object_open(obj, repo, &id);
689 const struct got_error *
690 got_object_resolve_id_str(struct got_object_id **id,
691 struct got_repository *repo, const char *id_str)
693 const struct got_error *err = NULL;
694 struct got_object *obj;
696 err = got_object_open_by_id_str(&obj, repo, id_str);
697 if (err)
698 return err;
700 *id = got_object_id_dup(got_object_get_id(obj));
701 got_object_close(obj);
702 if (*id == NULL)
703 return got_error_from_errno("got_object_id_dup");
705 return NULL;
708 static const struct got_error *
709 request_packed_commit(struct got_commit_object **commit, struct got_pack *pack,
710 int pack_idx, struct got_object_id *id)
712 const struct got_error *err = NULL;
714 err = got_privsep_send_commit_req(pack->privsep_child->ibuf, -1, id,
715 pack_idx);
716 if (err)
717 return err;
719 err = got_privsep_recv_commit(commit, pack->privsep_child->ibuf);
720 if (err)
721 return err;
723 (*commit)->flags |= GOT_COMMIT_FLAG_PACKED;
724 return NULL;
727 static const struct got_error *
728 read_packed_commit_privsep(struct got_commit_object **commit,
729 struct got_pack *pack, struct got_packidx *packidx, int idx,
730 struct got_object_id *id)
732 const struct got_error *err = NULL;
734 if (pack->privsep_child)
735 return request_packed_commit(commit, pack, idx, id);
737 err = start_pack_privsep_child(pack, packidx);
738 if (err)
739 return err;
741 return request_packed_commit(commit, pack, idx, id);
744 static const struct got_error *
745 request_commit(struct got_commit_object **commit, struct got_repository *repo,
746 int fd)
748 const struct got_error *err = NULL;
749 struct imsgbuf *ibuf;
751 ibuf = repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_COMMIT].ibuf;
753 err = got_privsep_send_commit_req(ibuf, fd, NULL, -1);
754 if (err)
755 return err;
757 return got_privsep_recv_commit(commit, ibuf);
760 static const struct got_error *
761 read_commit_privsep(struct got_commit_object **commit, int obj_fd,
762 struct got_repository *repo)
764 const struct got_error *err;
765 int imsg_fds[2];
766 pid_t pid;
767 struct imsgbuf *ibuf;
769 if (repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_COMMIT].imsg_fd != -1)
770 return request_commit(commit, repo, obj_fd);
772 ibuf = calloc(1, sizeof(*ibuf));
773 if (ibuf == NULL)
774 return got_error_from_errno("calloc");
776 if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_fds) == -1) {
777 err = got_error_from_errno("socketpair");
778 free(ibuf);
779 return err;
782 pid = fork();
783 if (pid == -1) {
784 err = got_error_from_errno("fork");
785 free(ibuf);
786 return err;
788 else if (pid == 0) {
789 got_privsep_exec_child(imsg_fds, GOT_PATH_PROG_READ_COMMIT,
790 repo->path);
791 /* not reached */
794 if (close(imsg_fds[1]) == -1) {
795 err = got_error_from_errno("close");
796 free(ibuf);
797 return err;
799 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_COMMIT].imsg_fd =
800 imsg_fds[0];
801 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_COMMIT].pid = pid;
802 imsg_init(ibuf, imsg_fds[0]);
803 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_COMMIT].ibuf = ibuf;
805 return request_commit(commit, repo, obj_fd);
809 static const struct got_error *
810 open_commit(struct got_commit_object **commit,
811 struct got_repository *repo, struct got_object_id *id, int check_cache)
813 const struct got_error *err = NULL;
814 struct got_packidx *packidx = NULL;
815 int idx;
816 char *path_packfile = NULL;
818 if (check_cache) {
819 *commit = got_repo_get_cached_commit(repo, id);
820 if (*commit != NULL) {
821 (*commit)->refcnt++;
822 return NULL;
824 } else
825 *commit = NULL;
827 err = got_repo_search_packidx(&packidx, &idx, repo, id);
828 if (err == NULL) {
829 struct got_pack *pack = NULL;
831 err = got_packidx_get_packfile_path(&path_packfile, packidx);
832 if (err)
833 return err;
835 pack = got_repo_get_cached_pack(repo, path_packfile);
836 if (pack == NULL) {
837 err = got_repo_cache_pack(&pack, repo, path_packfile,
838 packidx);
839 if (err)
840 goto done;
842 err = read_packed_commit_privsep(commit, pack,
843 packidx, idx, id);
844 } else if (err->code == GOT_ERR_NO_OBJ) {
845 int fd;
847 err = got_object_open_loose_fd(&fd, id, repo);
848 if (err)
849 return err;
850 err = read_commit_privsep(commit, fd, repo);
853 if (err == NULL) {
854 (*commit)->refcnt++;
855 err = got_repo_cache_commit(repo, id, *commit);
857 done:
858 free(path_packfile);
859 return err;
862 const struct got_error *
863 got_object_open_as_commit(struct got_commit_object **commit,
864 struct got_repository *repo, struct got_object_id *id)
866 *commit = got_repo_get_cached_commit(repo, id);
867 if (*commit != NULL) {
868 (*commit)->refcnt++;
869 return NULL;
872 return open_commit(commit, repo, id, 0);
875 const struct got_error *
876 got_object_commit_open(struct got_commit_object **commit,
877 struct got_repository *repo, struct got_object *obj)
879 return open_commit(commit, repo, got_object_get_id(obj), 1);
882 const struct got_error *
883 got_object_qid_alloc(struct got_object_qid **qid, struct got_object_id *id)
885 const struct got_error *err = NULL;
887 *qid = calloc(1, sizeof(**qid));
888 if (*qid == NULL)
889 return got_error_from_errno("calloc");
891 (*qid)->id = got_object_id_dup(id);
892 if ((*qid)->id == NULL) {
893 err = got_error_from_errno("got_object_id_dup");
894 got_object_qid_free(*qid);
895 *qid = NULL;
896 return err;
899 return NULL;
902 const struct got_error *
903 got_object_id_queue_copy(const struct got_object_id_queue *src,
904 struct got_object_id_queue *dest)
906 const struct got_error *err;
907 struct got_object_qid *qid;
909 SIMPLEQ_FOREACH(qid, src, entry) {
910 struct got_object_qid *new;
911 /*
912 * Deep-copy the object ID only. Let the caller deal
913 * with setting up the new->data pointer if needed.
914 */
915 err = got_object_qid_alloc(&new, qid->id);
916 if (err) {
917 got_object_id_queue_free(dest);
918 return err;
920 SIMPLEQ_INSERT_TAIL(dest, new, entry);
923 return NULL;
926 static const struct got_error *
927 request_packed_tree(struct got_tree_object **tree, struct got_pack *pack,
928 int pack_idx, struct got_object_id *id)
930 const struct got_error *err = NULL;
932 err = got_privsep_send_tree_req(pack->privsep_child->ibuf, -1, id,
933 pack_idx);
934 if (err)
935 return err;
937 return got_privsep_recv_tree(tree, pack->privsep_child->ibuf);
940 static const struct got_error *
941 read_packed_tree_privsep(struct got_tree_object **tree,
942 struct got_pack *pack, struct got_packidx *packidx, int idx,
943 struct got_object_id *id)
945 const struct got_error *err = NULL;
947 if (pack->privsep_child)
948 return request_packed_tree(tree, pack, idx, id);
950 err = start_pack_privsep_child(pack, packidx);
951 if (err)
952 return err;
954 return request_packed_tree(tree, pack, idx, id);
957 static const struct got_error *
958 request_tree(struct got_tree_object **tree, struct got_repository *repo,
959 int fd)
961 const struct got_error *err = NULL;
962 struct imsgbuf *ibuf;
964 ibuf = repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TREE].ibuf;
966 err = got_privsep_send_tree_req(ibuf, fd, NULL, -1);
967 if (err)
968 return err;
970 return got_privsep_recv_tree(tree, ibuf);
973 const struct got_error *
974 read_tree_privsep(struct got_tree_object **tree, int obj_fd,
975 struct got_repository *repo)
977 const struct got_error *err;
978 int imsg_fds[2];
979 pid_t pid;
980 struct imsgbuf *ibuf;
982 if (repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TREE].imsg_fd != -1)
983 return request_tree(tree, repo, obj_fd);
985 ibuf = calloc(1, sizeof(*ibuf));
986 if (ibuf == NULL)
987 return got_error_from_errno("calloc");
989 if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_fds) == -1) {
990 err = got_error_from_errno("socketpair");
991 free(ibuf);
992 return err;
995 pid = fork();
996 if (pid == -1) {
997 err = got_error_from_errno("fork");
998 free(ibuf);
999 return err;
1001 else if (pid == 0) {
1002 got_privsep_exec_child(imsg_fds, GOT_PATH_PROG_READ_TREE,
1003 repo->path);
1004 /* not reached */
1007 if (close(imsg_fds[1]) == -1) {
1008 err = got_error_from_errno("close");
1009 free(ibuf);
1010 return err;
1012 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TREE].imsg_fd =
1013 imsg_fds[0];
1014 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TREE].pid = pid;
1015 imsg_init(ibuf, imsg_fds[0]);
1016 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TREE].ibuf = ibuf;
1019 return request_tree(tree, repo, obj_fd);
1022 static const struct got_error *
1023 open_tree(struct got_tree_object **tree, struct got_repository *repo,
1024 struct got_object_id *id, int check_cache)
1026 const struct got_error *err = NULL;
1027 struct got_packidx *packidx = NULL;
1028 int idx;
1029 char *path_packfile = NULL;
1031 if (check_cache) {
1032 *tree = got_repo_get_cached_tree(repo, id);
1033 if (*tree != NULL) {
1034 (*tree)->refcnt++;
1035 return NULL;
1037 } else
1038 *tree = NULL;
1040 err = got_repo_search_packidx(&packidx, &idx, repo, id);
1041 if (err == NULL) {
1042 struct got_pack *pack = NULL;
1044 err = got_packidx_get_packfile_path(&path_packfile, packidx);
1045 if (err)
1046 return err;
1048 pack = got_repo_get_cached_pack(repo, path_packfile);
1049 if (pack == NULL) {
1050 err = got_repo_cache_pack(&pack, repo, path_packfile,
1051 packidx);
1052 if (err)
1053 goto done;
1055 err = read_packed_tree_privsep(tree, pack,
1056 packidx, idx, id);
1057 } else if (err->code == GOT_ERR_NO_OBJ) {
1058 int fd;
1060 err = got_object_open_loose_fd(&fd, id, repo);
1061 if (err)
1062 return err;
1063 err = read_tree_privsep(tree, fd, repo);
1066 if (err == NULL) {
1067 (*tree)->refcnt++;
1068 err = got_repo_cache_tree(repo, id, *tree);
1070 done:
1071 free(path_packfile);
1072 return err;
1075 const struct got_error *
1076 got_object_open_as_tree(struct got_tree_object **tree,
1077 struct got_repository *repo, struct got_object_id *id)
1079 *tree = got_repo_get_cached_tree(repo, id);
1080 if (*tree != NULL) {
1081 (*tree)->refcnt++;
1082 return NULL;
1085 return open_tree(tree, repo, id, 0);
1088 const struct got_error *
1089 got_object_tree_open(struct got_tree_object **tree,
1090 struct got_repository *repo, struct got_object *obj)
1092 return open_tree(tree, repo, got_object_get_id(obj), 1);
1095 int
1096 got_object_tree_get_nentries(struct got_tree_object *tree)
1098 return tree->nentries;
1101 struct got_tree_entry *
1102 got_object_tree_get_first_entry(struct got_tree_object *tree)
1104 return got_object_tree_get_entry(tree, 0);
1107 struct got_tree_entry *
1108 got_object_tree_get_last_entry(struct got_tree_object *tree)
1110 return got_object_tree_get_entry(tree, tree->nentries - 1);
1113 struct got_tree_entry *
1114 got_object_tree_get_entry(struct got_tree_object *tree, int i)
1116 if (i < 0 || i >= tree->nentries)
1117 return NULL;
1118 return &tree->entries[i];
1121 mode_t
1122 got_tree_entry_get_mode(struct got_tree_entry *te)
1124 return te->mode;
1127 const char *
1128 got_tree_entry_get_name(struct got_tree_entry *te)
1130 return &te->name[0];
1133 struct got_object_id *
1134 got_tree_entry_get_id(struct got_tree_entry *te)
1136 return &te->id;
1139 const struct got_error *
1140 got_object_blob_read_to_str(char **s, struct got_blob_object *blob)
1142 const struct got_error *err = NULL;
1143 size_t len, totlen, hdrlen, offset;
1145 *s = NULL;
1147 hdrlen = got_object_blob_get_hdrlen(blob);
1148 totlen = 0;
1149 offset = 0;
1150 do {
1151 char *p;
1153 err = got_object_blob_read_block(&len, blob);
1154 if (err)
1155 return err;
1157 if (len == 0)
1158 break;
1160 totlen += len - hdrlen;
1161 p = realloc(*s, totlen + 1);
1162 if (p == NULL) {
1163 err = got_error_from_errno("realloc");
1164 free(*s);
1165 *s = NULL;
1166 return err;
1168 *s = p;
1169 /* Skip blob object header first time around. */
1170 memcpy(*s + offset,
1171 got_object_blob_get_read_buf(blob) + hdrlen, len - hdrlen);
1172 hdrlen = 0;
1173 offset = totlen;
1174 } while (len > 0);
1176 (*s)[totlen] = '\0';
1177 return NULL;
1180 const struct got_error *
1181 got_tree_entry_get_symlink_target(char **link_target, struct got_tree_entry *te,
1182 struct got_repository *repo)
1184 const struct got_error *err = NULL;
1185 struct got_blob_object *blob = NULL;
1187 *link_target = NULL;
1189 if (!got_object_tree_entry_is_symlink(te))
1190 return got_error(GOT_ERR_TREE_ENTRY_TYPE);
1192 err = got_object_open_as_blob(&blob, repo,
1193 got_tree_entry_get_id(te), PATH_MAX);
1194 if (err)
1195 return err;
1197 err = got_object_blob_read_to_str(link_target, blob);
1198 got_object_blob_close(blob);
1199 if (err) {
1200 free(*link_target);
1201 *link_target = NULL;
1203 return err;
1206 int
1207 got_tree_entry_get_index(struct got_tree_entry *te)
1209 return te->idx;
1212 struct got_tree_entry *
1213 got_tree_entry_get_next(struct got_tree_object *tree,
1214 struct got_tree_entry *te)
1216 return got_object_tree_get_entry(tree, te->idx + 1);
1219 struct got_tree_entry *
1220 got_tree_entry_get_prev(struct got_tree_object *tree,
1221 struct got_tree_entry *te)
1223 return got_object_tree_get_entry(tree, te->idx - 1);
1226 static const struct got_error *
1227 request_packed_blob(uint8_t **outbuf, size_t *size, size_t *hdrlen, int outfd,
1228 struct got_pack *pack, struct got_packidx *packidx, int idx,
1229 struct got_object_id *id)
1231 const struct got_error *err = NULL;
1232 int outfd_child;
1233 int basefd, accumfd; /* temporary files for delta application */
1235 basefd = got_opentempfd();
1236 if (basefd == -1)
1237 return got_error_from_errno("got_opentempfd");
1238 accumfd = got_opentempfd();
1239 if (accumfd == -1)
1240 return got_error_from_errno("got_opentempfd");
1242 outfd_child = dup(outfd);
1243 if (outfd_child == -1)
1244 return got_error_from_errno("dup");
1246 err = got_privsep_send_blob_req(pack->privsep_child->ibuf, -1, id, idx);
1247 if (err)
1248 return err;
1250 err = got_privsep_send_blob_outfd(pack->privsep_child->ibuf,
1251 outfd_child);
1252 if (err) {
1253 close(basefd);
1254 close(accumfd);
1255 return err;
1258 err = got_privsep_send_tmpfd(pack->privsep_child->ibuf,
1259 basefd);
1260 if (err) {
1261 close(accumfd);
1262 return err;
1265 err = got_privsep_send_tmpfd(pack->privsep_child->ibuf,
1266 accumfd);
1267 if (err)
1268 return err;
1270 err = got_privsep_recv_blob(outbuf, size, hdrlen,
1271 pack->privsep_child->ibuf);
1272 if (err)
1273 return err;
1275 if (lseek(outfd, SEEK_SET, 0) == -1)
1276 err = got_error_from_errno("lseek");
1278 return err;
1281 static const struct got_error *
1282 read_packed_blob_privsep(uint8_t **outbuf, size_t *size, size_t *hdrlen,
1283 int outfd, struct got_pack *pack, struct got_packidx *packidx, int idx,
1284 struct got_object_id *id)
1286 const struct got_error *err = NULL;
1288 if (pack->privsep_child == NULL) {
1289 err = start_pack_privsep_child(pack, packidx);
1290 if (err)
1291 return err;
1294 return request_packed_blob(outbuf, size, hdrlen, outfd, pack, packidx,
1295 idx, id);
1298 static const struct got_error *
1299 request_blob(uint8_t **outbuf, size_t *size, size_t *hdrlen, int outfd,
1300 int infd, struct imsgbuf *ibuf)
1302 const struct got_error *err = NULL;
1303 int outfd_child;
1305 outfd_child = dup(outfd);
1306 if (outfd_child == -1)
1307 return got_error_from_errno("dup");
1309 err = got_privsep_send_blob_req(ibuf, infd, NULL, -1);
1310 if (err)
1311 return err;
1313 err = got_privsep_send_blob_outfd(ibuf, outfd_child);
1314 if (err)
1315 return err;
1317 err = got_privsep_recv_blob(outbuf, size, hdrlen, ibuf);
1318 if (err)
1319 return err;
1321 if (lseek(outfd, SEEK_SET, 0) == -1)
1322 return got_error_from_errno("lseek");
1324 return err;
1327 static const struct got_error *
1328 read_blob_privsep(uint8_t **outbuf, size_t *size, size_t *hdrlen,
1329 int outfd, int infd, struct got_repository *repo)
1331 const struct got_error *err;
1332 int imsg_fds[2];
1333 pid_t pid;
1334 struct imsgbuf *ibuf;
1336 if (repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_BLOB].imsg_fd != -1) {
1337 ibuf = repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_BLOB].ibuf;
1338 return request_blob(outbuf, size, hdrlen, outfd, infd, ibuf);
1341 ibuf = calloc(1, sizeof(*ibuf));
1342 if (ibuf == NULL)
1343 return got_error_from_errno("calloc");
1345 if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_fds) == -1) {
1346 err = got_error_from_errno("socketpair");
1347 free(ibuf);
1348 return err;
1351 pid = fork();
1352 if (pid == -1) {
1353 err = got_error_from_errno("fork");
1354 free(ibuf);
1355 return err;
1357 else if (pid == 0) {
1358 got_privsep_exec_child(imsg_fds, GOT_PATH_PROG_READ_BLOB,
1359 repo->path);
1360 /* not reached */
1363 if (close(imsg_fds[1]) == -1) {
1364 err = got_error_from_errno("close");
1365 free(ibuf);
1366 return err;
1368 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_BLOB].imsg_fd =
1369 imsg_fds[0];
1370 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_BLOB].pid = pid;
1371 imsg_init(ibuf, imsg_fds[0]);
1372 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_BLOB].ibuf = ibuf;
1374 return request_blob(outbuf, size, hdrlen, outfd, infd, ibuf);
1377 static const struct got_error *
1378 open_blob(struct got_blob_object **blob, struct got_repository *repo,
1379 struct got_object_id *id, size_t blocksize)
1381 const struct got_error *err = NULL;
1382 struct got_packidx *packidx = NULL;
1383 int idx;
1384 char *path_packfile = NULL;
1385 uint8_t *outbuf;
1386 int outfd;
1387 size_t size, hdrlen;
1388 struct stat sb;
1390 *blob = calloc(1, sizeof(**blob));
1391 if (*blob == NULL)
1392 return got_error_from_errno("calloc");
1394 outfd = got_opentempfd();
1395 if (outfd == -1)
1396 return got_error_from_errno("got_opentempfd");
1398 (*blob)->read_buf = malloc(blocksize);
1399 if ((*blob)->read_buf == NULL) {
1400 err = got_error_from_errno("malloc");
1401 goto done;
1404 err = got_repo_search_packidx(&packidx, &idx, repo, id);
1405 if (err == NULL) {
1406 struct got_pack *pack = NULL;
1408 err = got_packidx_get_packfile_path(&path_packfile, packidx);
1409 if (err)
1410 goto done;
1412 pack = got_repo_get_cached_pack(repo, path_packfile);
1413 if (pack == NULL) {
1414 err = got_repo_cache_pack(&pack, repo, path_packfile,
1415 packidx);
1416 if (err)
1417 goto done;
1419 err = read_packed_blob_privsep(&outbuf, &size, &hdrlen, outfd,
1420 pack, packidx, idx, id);
1421 } else if (err->code == GOT_ERR_NO_OBJ) {
1422 int infd;
1424 err = got_object_open_loose_fd(&infd, id, repo);
1425 if (err)
1426 goto done;
1427 err = read_blob_privsep(&outbuf, &size, &hdrlen, outfd, infd,
1428 repo);
1430 if (err)
1431 goto done;
1433 if (hdrlen > size) {
1434 err = got_error(GOT_ERR_BAD_OBJ_HDR);
1435 goto done;
1438 if (outbuf) {
1439 if (close(outfd) == -1 && err == NULL)
1440 err = got_error_from_errno("close");
1441 outfd = -1;
1442 (*blob)->f = fmemopen(outbuf, size, "rb");
1443 if ((*blob)->f == NULL) {
1444 err = got_error_from_errno("fmemopen");
1445 free(outbuf);
1446 goto done;
1448 (*blob)->data = outbuf;
1449 } else {
1450 if (fstat(outfd, &sb) == -1) {
1451 err = got_error_from_errno("fstat");
1452 goto done;
1455 if (sb.st_size != size) {
1456 err = got_error(GOT_ERR_PRIVSEP_LEN);
1457 goto done;
1460 (*blob)->f = fdopen(outfd, "rb");
1461 if ((*blob)->f == NULL) {
1462 err = got_error_from_errno("fdopen");
1463 close(outfd);
1464 outfd = -1;
1465 goto done;
1469 (*blob)->hdrlen = hdrlen;
1470 (*blob)->blocksize = blocksize;
1471 memcpy(&(*blob)->id.sha1, id->sha1, SHA1_DIGEST_LENGTH);
1473 done:
1474 free(path_packfile);
1475 if (err) {
1476 if (*blob) {
1477 got_object_blob_close(*blob);
1478 *blob = NULL;
1479 } else if (outfd != -1)
1480 close(outfd);
1482 return err;
1485 const struct got_error *
1486 got_object_open_as_blob(struct got_blob_object **blob,
1487 struct got_repository *repo, struct got_object_id *id,
1488 size_t blocksize)
1490 return open_blob(blob, repo, id, blocksize);
1493 const struct got_error *
1494 got_object_blob_open(struct got_blob_object **blob,
1495 struct got_repository *repo, struct got_object *obj, size_t blocksize)
1497 return open_blob(blob, repo, got_object_get_id(obj), blocksize);
1500 const struct got_error *
1501 got_object_blob_close(struct got_blob_object *blob)
1503 const struct got_error *err = NULL;
1504 free(blob->read_buf);
1505 if (blob->f && fclose(blob->f) == EOF)
1506 err = got_error_from_errno("fclose");
1507 free(blob->data);
1508 free(blob);
1509 return err;
1512 void
1513 got_object_blob_rewind(struct got_blob_object *blob)
1515 if (blob->f)
1516 rewind(blob->f);
1519 char *
1520 got_object_blob_id_str(struct got_blob_object *blob, char *buf, size_t size)
1522 return got_sha1_digest_to_str(blob->id.sha1, buf, size);
1525 size_t
1526 got_object_blob_get_hdrlen(struct got_blob_object *blob)
1528 return blob->hdrlen;
1531 const uint8_t *
1532 got_object_blob_get_read_buf(struct got_blob_object *blob)
1534 return blob->read_buf;
1537 const struct got_error *
1538 got_object_blob_read_block(size_t *outlenp, struct got_blob_object *blob)
1540 size_t n;
1542 n = fread(blob->read_buf, 1, blob->blocksize, blob->f);
1543 if (n == 0 && ferror(blob->f))
1544 return got_ferror(blob->f, GOT_ERR_IO);
1545 *outlenp = n;
1546 return NULL;
1549 const struct got_error *
1550 got_object_blob_dump_to_file(off_t *filesize, int *nlines,
1551 off_t **line_offsets, FILE *outfile, struct got_blob_object *blob)
1553 const struct got_error *err = NULL;
1554 size_t n, len, hdrlen;
1555 const uint8_t *buf;
1556 int i;
1557 const int alloc_chunksz = 512;
1558 size_t nalloc = 0;
1559 off_t off = 0, total_len = 0;
1561 if (line_offsets)
1562 *line_offsets = NULL;
1563 if (filesize)
1564 *filesize = 0;
1565 if (nlines)
1566 *nlines = 0;
1568 hdrlen = got_object_blob_get_hdrlen(blob);
1569 do {
1570 err = got_object_blob_read_block(&len, blob);
1571 if (err)
1572 return err;
1573 if (len == 0)
1574 break;
1575 buf = got_object_blob_get_read_buf(blob);
1576 i = hdrlen;
1577 if (nlines) {
1578 if (line_offsets && *line_offsets == NULL) {
1579 /* Have some data but perhaps no '\n'. */
1580 *nlines = 1;
1581 nalloc = alloc_chunksz;
1582 *line_offsets = calloc(nalloc,
1583 sizeof(**line_offsets));
1584 if (*line_offsets == NULL)
1585 return got_error_from_errno("calloc");
1587 /* Skip forward over end of first line. */
1588 while (i < len) {
1589 if (buf[i] == '\n')
1590 break;
1591 i++;
1594 /* Scan '\n' offsets in remaining chunk of data. */
1595 while (i < len) {
1596 if (buf[i] != '\n') {
1597 i++;
1598 continue;
1600 (*nlines)++;
1601 if (line_offsets && nalloc < *nlines) {
1602 size_t n = *nlines + alloc_chunksz;
1603 off_t *o = recallocarray(*line_offsets,
1604 nalloc, n, sizeof(**line_offsets));
1605 if (o == NULL) {
1606 free(*line_offsets);
1607 *line_offsets = NULL;
1608 return got_error_from_errno(
1609 "recallocarray");
1611 *line_offsets = o;
1612 nalloc = n;
1614 if (line_offsets) {
1615 off = total_len + i - hdrlen + 1;
1616 (*line_offsets)[*nlines - 1] = off;
1618 i++;
1621 /* Skip blob object header first time around. */
1622 n = fwrite(buf + hdrlen, 1, len - hdrlen, outfile);
1623 if (n != len - hdrlen)
1624 return got_ferror(outfile, GOT_ERR_IO);
1625 total_len += len - hdrlen;
1626 hdrlen = 0;
1627 } while (len != 0);
1629 if (fflush(outfile) != 0)
1630 return got_error_from_errno("fflush");
1631 rewind(outfile);
1633 if (filesize)
1634 *filesize = total_len;
1636 return NULL;
1639 static const struct got_error *
1640 request_packed_tag(struct got_tag_object **tag, struct got_pack *pack,
1641 int pack_idx, struct got_object_id *id)
1643 const struct got_error *err = NULL;
1645 err = got_privsep_send_tag_req(pack->privsep_child->ibuf, -1, id,
1646 pack_idx);
1647 if (err)
1648 return err;
1650 return got_privsep_recv_tag(tag, pack->privsep_child->ibuf);
1653 static const struct got_error *
1654 read_packed_tag_privsep(struct got_tag_object **tag,
1655 struct got_pack *pack, struct got_packidx *packidx, int idx,
1656 struct got_object_id *id)
1658 const struct got_error *err = NULL;
1660 if (pack->privsep_child)
1661 return request_packed_tag(tag, pack, idx, id);
1663 err = start_pack_privsep_child(pack, packidx);
1664 if (err)
1665 return err;
1667 return request_packed_tag(tag, pack, idx, id);
1670 static const struct got_error *
1671 request_tag(struct got_tag_object **tag, struct got_repository *repo,
1672 int fd)
1674 const struct got_error *err = NULL;
1675 struct imsgbuf *ibuf;
1677 ibuf = repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TAG].ibuf;
1679 err = got_privsep_send_tag_req(ibuf, fd, NULL, -1);
1680 if (err)
1681 return err;
1683 return got_privsep_recv_tag(tag, ibuf);
1686 static const struct got_error *
1687 read_tag_privsep(struct got_tag_object **tag, int obj_fd,
1688 struct got_repository *repo)
1690 const struct got_error *err;
1691 int imsg_fds[2];
1692 pid_t pid;
1693 struct imsgbuf *ibuf;
1695 if (repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TAG].imsg_fd != -1)
1696 return request_tag(tag, repo, obj_fd);
1698 ibuf = calloc(1, sizeof(*ibuf));
1699 if (ibuf == NULL)
1700 return got_error_from_errno("calloc");
1702 if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_fds) == -1) {
1703 err = got_error_from_errno("socketpair");
1704 free(ibuf);
1705 return err;
1708 pid = fork();
1709 if (pid == -1) {
1710 err = got_error_from_errno("fork");
1711 free(ibuf);
1712 return err;
1714 else if (pid == 0) {
1715 got_privsep_exec_child(imsg_fds, GOT_PATH_PROG_READ_TAG,
1716 repo->path);
1717 /* not reached */
1720 if (close(imsg_fds[1]) == -1) {
1721 err = got_error_from_errno("close");
1722 free(ibuf);
1723 return err;
1725 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TAG].imsg_fd =
1726 imsg_fds[0];
1727 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TAG].pid = pid;
1728 imsg_init(ibuf, imsg_fds[0]);
1729 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TAG].ibuf = ibuf;
1731 return request_tag(tag, repo, obj_fd);
1734 static const struct got_error *
1735 open_tag(struct got_tag_object **tag, struct got_repository *repo,
1736 struct got_object_id *id, int check_cache)
1738 const struct got_error *err = NULL;
1739 struct got_packidx *packidx = NULL;
1740 int idx;
1741 char *path_packfile = NULL;
1742 struct got_object *obj = NULL;
1743 int obj_type = GOT_OBJ_TYPE_ANY;
1745 if (check_cache) {
1746 *tag = got_repo_get_cached_tag(repo, id);
1747 if (*tag != NULL) {
1748 (*tag)->refcnt++;
1749 return NULL;
1751 } else
1752 *tag = NULL;
1754 err = got_repo_search_packidx(&packidx, &idx, repo, id);
1755 if (err == NULL) {
1756 struct got_pack *pack = NULL;
1758 err = got_packidx_get_packfile_path(&path_packfile, packidx);
1759 if (err)
1760 return err;
1762 pack = got_repo_get_cached_pack(repo, path_packfile);
1763 if (pack == NULL) {
1764 err = got_repo_cache_pack(&pack, repo, path_packfile,
1765 packidx);
1766 if (err)
1767 goto done;
1770 /* Beware of "lightweight" tags: Check object type first. */
1771 err = read_packed_object_privsep(&obj, repo, pack, packidx,
1772 idx, id);
1773 if (err)
1774 goto done;
1775 obj_type = obj->type;
1776 got_object_close(obj);
1777 if (obj_type != GOT_OBJ_TYPE_TAG) {
1778 err = got_error(GOT_ERR_OBJ_TYPE);
1779 goto done;
1781 err = read_packed_tag_privsep(tag, pack, packidx, idx, id);
1782 } else if (err->code == GOT_ERR_NO_OBJ) {
1783 int fd;
1785 err = got_object_open_loose_fd(&fd, id, repo);
1786 if (err)
1787 return err;
1788 err = read_object_header_privsep(&obj, repo, fd);
1789 if (err)
1790 return err;
1791 obj_type = obj->type;
1792 got_object_close(obj);
1793 if (obj_type != GOT_OBJ_TYPE_TAG)
1794 return got_error(GOT_ERR_OBJ_TYPE);
1796 err = got_object_open_loose_fd(&fd, id, repo);
1797 if (err)
1798 return err;
1799 err = read_tag_privsep(tag, fd, repo);
1802 if (err == NULL) {
1803 (*tag)->refcnt++;
1804 err = got_repo_cache_tag(repo, id, *tag);
1806 done:
1807 free(path_packfile);
1808 return err;
1811 const struct got_error *
1812 got_object_open_as_tag(struct got_tag_object **tag,
1813 struct got_repository *repo, struct got_object_id *id)
1815 *tag = got_repo_get_cached_tag(repo, id);
1816 if (*tag != NULL) {
1817 (*tag)->refcnt++;
1818 return NULL;
1821 return open_tag(tag, repo, id, 0);
1824 const struct got_error *
1825 got_object_tag_open(struct got_tag_object **tag,
1826 struct got_repository *repo, struct got_object *obj)
1828 return open_tag(tag, repo, got_object_get_id(obj), 1);
1831 const char *
1832 got_object_tag_get_name(struct got_tag_object *tag)
1834 return tag->tag;
1837 int
1838 got_object_tag_get_object_type(struct got_tag_object *tag)
1840 return tag->obj_type;
1843 struct got_object_id *
1844 got_object_tag_get_object_id(struct got_tag_object *tag)
1846 return &tag->id;
1849 time_t
1850 got_object_tag_get_tagger_time(struct got_tag_object *tag)
1852 return tag->tagger_time;
1855 time_t
1856 got_object_tag_get_tagger_gmtoff(struct got_tag_object *tag)
1858 return tag->tagger_gmtoff;
1861 const char *
1862 got_object_tag_get_tagger(struct got_tag_object *tag)
1864 return tag->tagger;
1867 const char *
1868 got_object_tag_get_message(struct got_tag_object *tag)
1870 return tag->tagmsg;
1873 static struct got_tree_entry *
1874 find_entry_by_name(struct got_tree_object *tree, const char *name, size_t len)
1876 int i;
1878 /* Note that tree entries are sorted in strncmp() order. */
1879 for (i = 0; i < tree->nentries; i++) {
1880 struct got_tree_entry *te = &tree->entries[i];
1881 int cmp = strncmp(te->name, name, len);
1882 if (cmp < 0)
1883 continue;
1884 if (cmp > 0)
1885 break;
1886 if (te->name[len] == '\0')
1887 return te;
1889 return NULL;
1892 struct got_tree_entry *
1893 got_object_tree_find_entry(struct got_tree_object *tree, const char *name)
1895 return find_entry_by_name(tree, name, strlen(name));
1898 const struct got_error *
1899 got_object_id_by_path(struct got_object_id **id, struct got_repository *repo,
1900 struct got_object_id *commit_id, const char *path)
1902 const struct got_error *err = NULL;
1903 struct got_commit_object *commit = NULL;
1904 struct got_tree_object *tree = NULL;
1905 struct got_tree_entry *te = NULL;
1906 const char *seg, *s;
1907 size_t seglen;
1909 *id = NULL;
1911 err = got_object_open_as_commit(&commit, repo, commit_id);
1912 if (err)
1913 goto done;
1915 /* Handle opening of root of commit's tree. */
1916 if (got_path_is_root_dir(path)) {
1917 *id = got_object_id_dup(commit->tree_id);
1918 if (*id == NULL)
1919 err = got_error_from_errno("got_object_id_dup");
1920 goto done;
1923 err = got_object_open_as_tree(&tree, repo, commit->tree_id);
1924 if (err)
1925 goto done;
1927 s = path;
1928 while (s[0] == '/')
1929 s++;
1930 seg = s;
1931 seglen = 0;
1932 while (*s) {
1933 struct got_tree_object *next_tree;
1935 if (*s != '/') {
1936 s++;
1937 seglen++;
1938 if (*s)
1939 continue;
1942 te = find_entry_by_name(tree, seg, seglen);
1943 if (te == NULL) {
1944 err = got_error_path(path, GOT_ERR_NO_TREE_ENTRY);
1945 goto done;
1948 if (*s == '\0')
1949 break;
1951 seg = s + 1;
1952 seglen = 0;
1953 s++;
1954 if (*s) {
1955 err = got_object_open_as_tree(&next_tree, repo,
1956 &te->id);
1957 te = NULL;
1958 if (err)
1959 goto done;
1960 got_object_tree_close(tree);
1961 tree = next_tree;
1965 if (te) {
1966 *id = got_object_id_dup(&te->id);
1967 if (*id == NULL)
1968 return got_error_from_errno("got_object_id_dup");
1969 } else
1970 err = got_error_path(path, GOT_ERR_NO_TREE_ENTRY);
1971 done:
1972 if (commit)
1973 got_object_commit_close(commit);
1974 if (tree)
1975 got_object_tree_close(tree);
1976 return err;
1980 * Normalize file mode bits to avoid false positive tree entry differences
1981 * in case tree entries have unexpected mode bits set.
1983 static mode_t
1984 normalize_mode_for_comparison(mode_t mode)
1987 * For directories, the only relevant bit is the IFDIR bit.
1988 * This allows us to detect paths changing from a directory
1989 * to a file and vice versa.
1991 if (S_ISDIR(mode))
1992 return mode & S_IFDIR;
1995 * For symlinks, the only relevant bit is the IFLNK bit.
1996 * This allows us to detect paths changing from a symlinks
1997 * to a file or directory and vice versa.
1999 if (S_ISLNK(mode))
2000 return mode & S_IFLNK;
2002 /* For files, the only change we care about is the executable bit. */
2003 return mode & S_IXUSR;
2006 const struct got_error *
2007 got_object_tree_path_changed(int *changed,
2008 struct got_tree_object *tree01, struct got_tree_object *tree02,
2009 const char *path, struct got_repository *repo)
2011 const struct got_error *err = NULL;
2012 struct got_tree_object *tree1 = NULL, *tree2 = NULL;
2013 struct got_tree_entry *te1 = NULL, *te2 = NULL;
2014 const char *seg, *s;
2015 size_t seglen;
2017 *changed = 0;
2019 /* We not do support comparing the root path. */
2020 if (got_path_is_root_dir(path))
2021 return got_error_path(path, GOT_ERR_BAD_PATH);
2023 tree1 = tree01;
2024 tree2 = tree02;
2025 s = path;
2026 while (*s == '/')
2027 s++;
2028 seg = s;
2029 seglen = 0;
2030 while (*s) {
2031 struct got_tree_object *next_tree1, *next_tree2;
2032 mode_t mode1, mode2;
2034 if (*s != '/') {
2035 s++;
2036 seglen++;
2037 if (*s)
2038 continue;
2041 te1 = find_entry_by_name(tree1, seg, seglen);
2042 if (te1 == NULL) {
2043 err = got_error(GOT_ERR_NO_OBJ);
2044 goto done;
2047 if (tree2)
2048 te2 = find_entry_by_name(tree2, seg, seglen);
2050 if (te2) {
2051 mode1 = normalize_mode_for_comparison(te1->mode);
2052 mode2 = normalize_mode_for_comparison(te2->mode);
2053 if (mode1 != mode2) {
2054 *changed = 1;
2055 goto done;
2058 if (got_object_id_cmp(&te1->id, &te2->id) == 0) {
2059 *changed = 0;
2060 goto done;
2064 if (*s == '\0') { /* final path element */
2065 *changed = 1;
2066 goto done;
2069 seg = s + 1;
2070 s++;
2071 seglen = 0;
2072 if (*s) {
2073 err = got_object_open_as_tree(&next_tree1, repo,
2074 &te1->id);
2075 te1 = NULL;
2076 if (err)
2077 goto done;
2078 if (tree1 != tree01)
2079 got_object_tree_close(tree1);
2080 tree1 = next_tree1;
2082 if (te2) {
2083 err = got_object_open_as_tree(&next_tree2, repo,
2084 &te2->id);
2085 te2 = NULL;
2086 if (err)
2087 goto done;
2088 if (tree2 != tree02)
2089 got_object_tree_close(tree2);
2090 tree2 = next_tree2;
2091 } else if (tree2) {
2092 if (tree2 != tree02)
2093 got_object_tree_close(tree2);
2094 tree2 = NULL;
2098 done:
2099 if (tree1 && tree1 != tree01)
2100 got_object_tree_close(tree1);
2101 if (tree2 && tree2 != tree02)
2102 got_object_tree_close(tree2);
2103 return err;
2106 const struct got_error *
2107 got_object_tree_entry_dup(struct got_tree_entry **new_te,
2108 struct got_tree_entry *te)
2110 const struct got_error *err = NULL;
2112 *new_te = calloc(1, sizeof(**new_te));
2113 if (*new_te == NULL)
2114 return got_error_from_errno("calloc");
2116 (*new_te)->mode = te->mode;
2117 memcpy((*new_te)->name, te->name, sizeof((*new_te)->name));
2118 memcpy(&(*new_te)->id, &te->id, sizeof((*new_te)->id));
2119 return err;
2122 int
2123 got_object_tree_entry_is_submodule(struct got_tree_entry *te)
2125 return (te->mode & S_IFMT) == (S_IFDIR | S_IFLNK);
2128 int
2129 got_object_tree_entry_is_symlink(struct got_tree_entry *te)
2131 /* S_IFDIR check avoids confusing symlinks with submodules. */
2132 return ((te->mode & (S_IFDIR | S_IFLNK)) == S_IFLNK);
2135 static const struct got_error *
2136 resolve_symlink(char **link_target, const char *path,
2137 struct got_object_id *commit_id, struct got_repository *repo)
2139 const struct got_error *err = NULL;
2140 char buf[PATH_MAX];
2141 char *name, *parent_path = NULL;
2142 struct got_object_id *tree_obj_id = NULL;
2143 struct got_tree_object *tree = NULL;
2144 struct got_tree_entry *te = NULL;
2146 *link_target = NULL;
2148 if (strlcpy(buf, path, sizeof(buf)) >= sizeof(buf))
2149 return got_error(GOT_ERR_NO_SPACE);
2151 name = basename(buf);
2152 if (name == NULL)
2153 return got_error_from_errno2("basename", path);
2155 err = got_path_dirname(&parent_path, path);
2156 if (err)
2157 return err;
2159 err = got_object_id_by_path(&tree_obj_id, repo, commit_id,
2160 parent_path);
2161 if (err) {
2162 if (err->code == GOT_ERR_NO_TREE_ENTRY) {
2163 /* Display the complete path in error message. */
2164 err = got_error_path(path, err->code);
2166 goto done;
2169 err = got_object_open_as_tree(&tree, repo, tree_obj_id);
2170 if (err)
2171 goto done;
2173 te = got_object_tree_find_entry(tree, name);
2174 if (te == NULL) {
2175 err = got_error_path(path, GOT_ERR_NO_TREE_ENTRY);
2176 goto done;
2179 if (got_object_tree_entry_is_symlink(te)) {
2180 err = got_tree_entry_get_symlink_target(link_target, te, repo);
2181 if (err)
2182 goto done;
2183 if (!got_path_is_absolute(*link_target)) {
2184 char *abspath;
2185 if (asprintf(&abspath, "%s/%s", parent_path,
2186 *link_target) == -1) {
2187 err = got_error_from_errno("asprintf");
2188 goto done;
2190 free(*link_target);
2191 *link_target = malloc(PATH_MAX);
2192 if (*link_target == NULL) {
2193 err = got_error_from_errno("malloc");
2194 goto done;
2196 err = got_canonpath(abspath, *link_target, PATH_MAX);
2197 free(abspath);
2198 if (err)
2199 goto done;
2202 done:
2203 free(tree_obj_id);
2204 if (tree)
2205 got_object_tree_close(tree);
2206 if (err) {
2207 free(*link_target);
2208 *link_target = NULL;
2210 return err;
2213 const struct got_error *
2214 got_object_resolve_symlinks(char **link_target, const char *path,
2215 struct got_object_id *commit_id, struct got_repository *repo)
2217 const struct got_error *err = NULL;
2218 char *next_target = NULL;
2219 int max_recursion = 40; /* matches Git */
2221 *link_target = NULL;
2223 do {
2224 err = resolve_symlink(&next_target,
2225 *link_target ? *link_target : path, commit_id, repo);
2226 if (err)
2227 break;
2228 if (next_target) {
2229 free(*link_target);
2230 if (--max_recursion == 0) {
2231 err = got_error_path(path, GOT_ERR_RECURSION);
2232 *link_target = NULL;
2233 break;
2235 *link_target = next_target;
2237 } while (next_target);
2239 return err;
2242 const struct got_error *
2243 got_traverse_packed_commits(struct got_object_id_queue *traversed_commits,
2244 struct got_object_id *commit_id, const char *path,
2245 struct got_repository *repo)
2247 const struct got_error *err = NULL;
2248 struct got_pack *pack = NULL;
2249 struct got_packidx *packidx = NULL;
2250 char *path_packfile = NULL;
2251 struct got_commit_object *changed_commit = NULL;
2252 struct got_object_id *changed_commit_id = NULL;
2253 int idx;
2255 err = got_repo_search_packidx(&packidx, &idx, repo, commit_id);
2256 if (err) {
2257 if (err->code != GOT_ERR_NO_OBJ)
2258 return err;
2259 return NULL;
2262 err = got_packidx_get_packfile_path(&path_packfile, packidx);
2263 if (err)
2264 return err;
2266 pack = got_repo_get_cached_pack(repo, path_packfile);
2267 if (pack == NULL) {
2268 err = got_repo_cache_pack(&pack, repo, path_packfile, packidx);
2269 if (err)
2270 goto done;
2273 if (pack->privsep_child == NULL) {
2274 err = start_pack_privsep_child(pack, packidx);
2275 if (err)
2276 goto done;
2279 err = got_privsep_send_commit_traversal_request(
2280 pack->privsep_child->ibuf, commit_id, idx, path);
2281 if (err)
2282 goto done;
2284 err = got_privsep_recv_traversed_commits(&changed_commit,
2285 &changed_commit_id, traversed_commits, pack->privsep_child->ibuf);
2286 if (err)
2287 goto done;
2289 if (changed_commit) {
2291 * Cache the commit in which the path was changed.
2292 * This commit might be opened again soon.
2294 changed_commit->refcnt++;
2295 err = got_repo_cache_commit(repo, changed_commit_id,
2296 changed_commit);
2297 got_object_commit_close(changed_commit);
2299 done:
2300 free(path_packfile);
2301 free(changed_commit_id);
2302 return err;