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/tree.h>
21 #include <sys/uio.h>
22 #include <sys/socket.h>
23 #include <sys/wait.h>
24 #include <sys/resource.h>
26 #include <errno.h>
27 #include <fcntl.h>
28 #include <stdio.h>
29 #include <stdlib.h>
30 #include <string.h>
31 #include <stdint.h>
32 #include <sha1.h>
33 #include <unistd.h>
34 #include <zlib.h>
35 #include <ctype.h>
36 #include <libgen.h>
37 #include <limits.h>
38 #include <imsg.h>
39 #include <time.h>
41 #include "got_error.h"
42 #include "got_object.h"
43 #include "got_repository.h"
44 #include "got_opentemp.h"
45 #include "got_path.h"
47 #include "got_lib_sha1.h"
48 #include "got_lib_delta.h"
49 #include "got_lib_inflate.h"
50 #include "got_lib_object.h"
51 #include "got_lib_privsep.h"
52 #include "got_lib_object_idcache.h"
53 #include "got_lib_object_cache.h"
54 #include "got_lib_object_parse.h"
55 #include "got_lib_pack.h"
56 #include "got_lib_repository.h"
58 #ifndef MIN
59 #define MIN(_a,_b) ((_a) < (_b) ? (_a) : (_b))
60 #endif
62 struct got_object_id *
63 got_object_get_id(struct got_object *obj)
64 {
65 return &obj->id;
66 }
68 const struct got_error *
69 got_object_get_id_str(char **outbuf, struct got_object *obj)
70 {
71 return got_object_id_str(outbuf, &obj->id);
72 }
74 const struct got_error *
75 got_object_get_type(int *type, struct got_repository *repo,
76 struct got_object_id *id)
77 {
78 const struct got_error *err = NULL;
79 struct got_object *obj;
81 err = got_object_open(&obj, repo, id);
82 if (err)
83 return err;
85 switch (obj->type) {
86 case GOT_OBJ_TYPE_COMMIT:
87 case GOT_OBJ_TYPE_TREE:
88 case GOT_OBJ_TYPE_BLOB:
89 case GOT_OBJ_TYPE_TAG:
90 *type = obj->type;
91 break;
92 default:
93 err = got_error(GOT_ERR_OBJ_TYPE);
94 break;
95 }
97 got_object_close(obj);
98 return err;
99 }
101 const struct got_error *
102 got_object_get_path(char **path, struct got_object_id *id,
103 struct got_repository *repo)
105 const struct got_error *err = NULL;
106 char *hex = NULL;
107 char *path_objects;
109 *path = NULL;
111 path_objects = got_repo_get_path_objects(repo);
112 if (path_objects == NULL)
113 return got_error_from_errno("got_repo_get_path_objects");
115 err = got_object_id_str(&hex, id);
116 if (err)
117 goto done;
119 if (asprintf(path, "%s/%.2x/%s", path_objects,
120 id->sha1[0], hex + 2) == -1)
121 err = got_error_from_errno("asprintf");
123 done:
124 free(hex);
125 free(path_objects);
126 return err;
129 const struct got_error *
130 got_object_open_loose_fd(int *fd, struct got_object_id *id,
131 struct got_repository *repo)
133 const struct got_error *err = NULL;
134 char *path;
136 err = got_object_get_path(&path, id, repo);
137 if (err)
138 return err;
139 *fd = open(path, O_RDONLY | O_NOFOLLOW);
140 if (*fd == -1) {
141 err = got_error_from_errno2("open", path);
142 goto done;
144 done:
145 free(path);
146 return err;
149 static const struct got_error *
150 request_packed_object(struct got_object **obj, struct got_pack *pack, int idx,
151 struct got_object_id *id)
153 const struct got_error *err = NULL;
154 struct imsgbuf *ibuf = pack->privsep_child->ibuf;
156 err = got_privsep_send_packed_obj_req(ibuf, idx, id);
157 if (err)
158 return err;
160 err = got_privsep_recv_obj(obj, ibuf);
161 if (err)
162 return err;
164 memcpy(&(*obj)->id, id, sizeof((*obj)->id));
166 return NULL;
169 static const struct got_error *
170 request_packed_object_raw(uint8_t **outbuf, off_t *size, size_t *hdrlen,
171 int outfd, struct got_pack *pack, int idx, struct got_object_id *id)
173 const struct got_error *err = NULL;
174 struct imsgbuf *ibuf = pack->privsep_child->ibuf;
175 int outfd_child;
176 int basefd, accumfd; /* temporary files for delta application */
178 basefd = got_opentempfd();
179 if (basefd == -1)
180 return got_error_from_errno("got_opentempfd");
182 accumfd = got_opentempfd();
183 if (accumfd == -1) {
184 close(basefd);
185 return got_error_from_errno("got_opentempfd");
188 outfd_child = dup(outfd);
189 if (outfd_child == -1) {
190 err = got_error_from_errno("dup");
191 close(basefd);
192 close(accumfd);
193 return err;
196 err = got_privsep_send_packed_raw_obj_req(ibuf, idx, id);
197 if (err) {
198 close(basefd);
199 close(accumfd);
200 close(outfd_child);
201 return err;
204 err = got_privsep_send_raw_obj_outfd(ibuf, outfd_child);
205 if (err) {
206 close(basefd);
207 close(accumfd);
208 return err;
212 err = got_privsep_send_tmpfd(pack->privsep_child->ibuf,
213 basefd);
214 if (err) {
215 close(accumfd);
216 return err;
219 err = got_privsep_send_tmpfd(pack->privsep_child->ibuf,
220 accumfd);
221 if (err)
222 return err;
224 err = got_privsep_recv_raw_obj(outbuf, size, hdrlen, ibuf);
225 if (err)
226 return err;
228 return NULL;
231 static void
232 set_max_datasize(void)
234 struct rlimit rl;
236 if (getrlimit(RLIMIT_DATA, &rl) != 0)
237 return;
239 rl.rlim_cur = rl.rlim_max;
240 setrlimit(RLIMIT_DATA, &rl);
243 static const struct got_error *
244 start_pack_privsep_child(struct got_pack *pack, struct got_packidx *packidx)
246 const struct got_error *err = NULL;
247 int imsg_fds[2];
248 pid_t pid;
249 struct imsgbuf *ibuf;
251 ibuf = calloc(1, sizeof(*ibuf));
252 if (ibuf == NULL)
253 return got_error_from_errno("calloc");
255 pack->privsep_child = calloc(1, sizeof(*pack->privsep_child));
256 if (pack->privsep_child == NULL) {
257 err = got_error_from_errno("calloc");
258 free(ibuf);
259 return err;
262 if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_fds) == -1) {
263 err = got_error_from_errno("socketpair");
264 goto done;
267 pid = fork();
268 if (pid == -1) {
269 err = got_error_from_errno("fork");
270 goto done;
271 } else if (pid == 0) {
272 set_max_datasize();
273 got_privsep_exec_child(imsg_fds, GOT_PATH_PROG_READ_PACK,
274 pack->path_packfile);
275 /* not reached */
278 if (close(imsg_fds[1]) == -1)
279 return got_error_from_errno("close");
280 pack->privsep_child->imsg_fd = imsg_fds[0];
281 pack->privsep_child->pid = pid;
282 imsg_init(ibuf, imsg_fds[0]);
283 pack->privsep_child->ibuf = ibuf;
285 err = got_privsep_init_pack_child(ibuf, pack, packidx);
286 if (err) {
287 const struct got_error *child_err;
288 err = got_privsep_send_stop(pack->privsep_child->imsg_fd);
289 child_err = got_privsep_wait_for_child(
290 pack->privsep_child->pid);
291 if (child_err && err == NULL)
292 err = child_err;
294 done:
295 if (err) {
296 free(ibuf);
297 free(pack->privsep_child);
298 pack->privsep_child = NULL;
300 return err;
303 static const struct got_error *
304 read_packed_object_privsep(struct got_object **obj,
305 struct got_repository *repo, struct got_pack *pack,
306 struct got_packidx *packidx, int idx, struct got_object_id *id)
308 const struct got_error *err = NULL;
310 if (pack->privsep_child == NULL) {
311 err = start_pack_privsep_child(pack, packidx);
312 if (err)
313 return err;
316 return request_packed_object(obj, pack, idx, id);
319 static const struct got_error *
320 read_packed_object_raw_privsep(uint8_t **outbuf, off_t *size, size_t *hdrlen,
321 int outfd, struct got_pack *pack, struct got_packidx *packidx, int idx,
322 struct got_object_id *id)
324 const struct got_error *err = NULL;
326 if (pack->privsep_child == NULL) {
327 err = start_pack_privsep_child(pack, packidx);
328 if (err)
329 return err;
332 return request_packed_object_raw(outbuf, size, hdrlen, outfd, pack,
333 idx, id);
336 const struct got_error *
337 got_object_open_packed(struct got_object **obj, struct got_object_id *id,
338 struct got_repository *repo)
340 const struct got_error *err = NULL;
341 struct got_pack *pack = NULL;
342 struct got_packidx *packidx = NULL;
343 int idx;
344 char *path_packfile;
346 err = got_repo_search_packidx(&packidx, &idx, repo, id);
347 if (err)
348 return err;
350 err = got_packidx_get_packfile_path(&path_packfile,
351 packidx->path_packidx);
352 if (err)
353 return err;
355 pack = got_repo_get_cached_pack(repo, path_packfile);
356 if (pack == NULL) {
357 err = got_repo_cache_pack(&pack, repo, path_packfile, packidx);
358 if (err)
359 goto done;
362 err = read_packed_object_privsep(obj, repo, pack, packidx, idx, id);
363 if (err)
364 goto done;
365 done:
366 free(path_packfile);
367 return err;
370 static const struct got_error *
371 request_object(struct got_object **obj, struct got_object_id *id,
372 struct got_repository *repo, int fd)
374 const struct got_error *err = NULL;
375 struct imsgbuf *ibuf;
377 ibuf = repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_OBJECT].ibuf;
379 err = got_privsep_send_obj_req(ibuf, fd, id);
380 if (err)
381 return err;
383 return got_privsep_recv_obj(obj, ibuf);
386 static const struct got_error *
387 request_raw_object(uint8_t **outbuf, off_t *size, size_t *hdrlen, int outfd,
388 struct got_object_id *id, struct got_repository *repo, int infd)
390 const struct got_error *err = NULL;
391 struct imsgbuf *ibuf;
392 int outfd_child;
394 ibuf = repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_OBJECT].ibuf;
396 outfd_child = dup(outfd);
397 if (outfd_child == -1)
398 return got_error_from_errno("dup");
400 err = got_privsep_send_raw_obj_req(ibuf, infd, id);
401 if (err)
402 return err;
404 err = got_privsep_send_raw_obj_outfd(ibuf, outfd_child);
405 if (err)
406 return err;
408 return got_privsep_recv_raw_obj(outbuf, size, hdrlen, ibuf);
411 static const struct got_error *
412 start_read_object_child(struct got_repository *repo)
414 const struct got_error *err = NULL;
415 int imsg_fds[2];
416 pid_t pid;
417 struct imsgbuf *ibuf;
419 ibuf = calloc(1, sizeof(*ibuf));
420 if (ibuf == NULL)
421 return got_error_from_errno("calloc");
423 if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_fds) == -1) {
424 err = got_error_from_errno("socketpair");
425 free(ibuf);
426 return err;
429 pid = fork();
430 if (pid == -1) {
431 err = got_error_from_errno("fork");
432 free(ibuf);
433 return err;
435 else if (pid == 0) {
436 got_privsep_exec_child(imsg_fds, GOT_PATH_PROG_READ_OBJECT,
437 repo->path);
438 /* not reached */
441 if (close(imsg_fds[1]) == -1) {
442 err = got_error_from_errno("close");
443 free(ibuf);
444 return err;
447 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_OBJECT].imsg_fd =
448 imsg_fds[0];
449 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_OBJECT].pid = pid;
450 imsg_init(ibuf, imsg_fds[0]);
451 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_OBJECT].ibuf = ibuf;
453 return NULL;
456 const struct got_error *
457 got_object_read_header_privsep(struct got_object **obj,
458 struct got_object_id *id, struct got_repository *repo, int obj_fd)
460 const struct got_error *err;
462 if (repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_OBJECT].imsg_fd != -1)
463 return request_object(obj, id, repo, obj_fd);
465 err = start_read_object_child(repo);
466 if (err) {
467 close(obj_fd);
468 return err;
471 return request_object(obj, id, repo, obj_fd);
474 static const struct got_error *
475 read_object_raw_privsep(uint8_t **outbuf, off_t *size, size_t *hdrlen,
476 int outfd, struct got_object_id *id, struct got_repository *repo,
477 int obj_fd)
479 const struct got_error *err;
481 if (repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_OBJECT].imsg_fd != -1)
482 return request_raw_object(outbuf, size, hdrlen, outfd, id,
483 repo, obj_fd);
485 err = start_read_object_child(repo);
486 if (err)
487 return err;
489 return request_raw_object(outbuf, size, hdrlen, outfd, id, repo,
490 obj_fd);
493 const struct got_error *
494 got_object_open(struct got_object **obj, struct got_repository *repo,
495 struct got_object_id *id)
497 const struct got_error *err = NULL;
498 int fd;
500 *obj = got_repo_get_cached_object(repo, id);
501 if (*obj != NULL) {
502 (*obj)->refcnt++;
503 return NULL;
506 err = got_object_open_packed(obj, id, repo);
507 if (err && err->code != GOT_ERR_NO_OBJ)
508 return err;
509 if (*obj) {
510 (*obj)->refcnt++;
511 return got_repo_cache_object(repo, id, *obj);
514 err = got_object_open_loose_fd(&fd, id, repo);
515 if (err) {
516 if (err->code == GOT_ERR_ERRNO && errno == ENOENT)
517 err = got_error_no_obj(id);
518 return err;
521 err = got_object_read_header_privsep(obj, id, repo, fd);
522 if (err)
523 return err;
525 memcpy((*obj)->id.sha1, id->sha1, SHA1_DIGEST_LENGTH);
527 (*obj)->refcnt++;
528 return got_repo_cache_object(repo, id, *obj);
531 const struct got_error *
532 got_object_raw_open(struct got_raw_object **obj, struct got_repository *repo,
533 struct got_object_id *id, size_t blocksize)
535 const struct got_error *err = NULL;
536 struct got_packidx *packidx = NULL;
537 int idx;
538 uint8_t *outbuf = NULL;
539 int outfd = -1;
540 off_t size = 0;
541 size_t hdrlen = 0;
542 char *path_packfile = NULL;
544 *obj = NULL;
546 outfd = got_opentempfd();
547 if (outfd == -1)
548 return got_error_from_errno("got_opentempfd");
550 err = got_repo_search_packidx(&packidx, &idx, repo, id);
551 if (err == NULL) {
552 struct got_pack *pack = NULL;
554 err = got_packidx_get_packfile_path(&path_packfile,
555 packidx->path_packidx);
556 if (err)
557 goto done;
559 pack = got_repo_get_cached_pack(repo, path_packfile);
560 if (pack == NULL) {
561 err = got_repo_cache_pack(&pack, repo, path_packfile,
562 packidx);
563 if (err)
564 goto done;
566 err = read_packed_object_raw_privsep(&outbuf, &size, &hdrlen,
567 outfd, pack, packidx, idx, id);
568 if (err)
569 goto done;
570 } else if (err->code == GOT_ERR_NO_OBJ) {
571 int fd;
573 err = got_object_open_loose_fd(&fd, id, repo);
574 if (err)
575 goto done;
576 err = read_object_raw_privsep(&outbuf, &size, &hdrlen, outfd,
577 id, repo, fd);
578 if (err)
579 goto done;
582 *obj = calloc(1, sizeof(**obj));
583 if (*obj == NULL) {
584 err = got_error_from_errno("calloc");
585 goto done;
588 (*obj)->read_buf = malloc(blocksize);
589 if ((*obj)->read_buf == NULL) {
590 err = got_error_from_errno("malloc");
591 goto done;
594 if (outbuf) {
595 if (close(outfd) == -1) {
596 err = got_error_from_errno("close");
597 goto done;
599 outfd = -1;
600 (*obj)->f = fmemopen(outbuf, hdrlen + size, "r");
601 if ((*obj)->f == NULL) {
602 err = got_error_from_errno("fdopen");
603 goto done;
605 (*obj)->data = outbuf;
606 } else {
607 struct stat sb;
608 if (fstat(outfd, &sb) == -1) {
609 err = got_error_from_errno("fstat");
610 goto done;
613 if (sb.st_size != hdrlen + size) {
614 err = got_error(GOT_ERR_PRIVSEP_LEN);
615 goto done;
618 (*obj)->f = fdopen(outfd, "r");
619 if ((*obj)->f == NULL) {
620 err = got_error_from_errno("fdopen");
621 goto done;
623 outfd = -1;
624 (*obj)->data = NULL;
626 (*obj)->hdrlen = hdrlen;
627 (*obj)->size = size;
628 (*obj)->blocksize = blocksize;
629 done:
630 free(path_packfile);
631 if (err) {
632 if (*obj) {
633 got_object_raw_close(*obj);
634 *obj = NULL;
636 if (outfd != -1)
637 close(outfd);
638 free(outbuf);
640 return err;
643 void
644 got_object_raw_rewind(struct got_raw_object *obj)
646 if (obj->f)
647 rewind(obj->f);
650 size_t
651 got_object_raw_get_hdrlen(struct got_raw_object *obj)
653 return obj->hdrlen;
656 const uint8_t *
657 got_object_raw_get_read_buf(struct got_raw_object *obj)
659 return obj->read_buf;
662 const struct got_error *
663 got_object_raw_read_block(size_t *outlenp, struct got_raw_object *obj)
665 size_t n;
667 n = fread(obj->read_buf, 1, obj->blocksize, obj->f);
668 if (n == 0 && ferror(obj->f))
669 return got_ferror(obj->f, GOT_ERR_IO);
670 *outlenp = n;
671 return NULL;
674 const struct got_error *
675 got_object_raw_close(struct got_raw_object *obj)
677 const struct got_error *err = NULL;
679 free(obj->read_buf);
680 if (obj->f != NULL && fclose(obj->f) == EOF && err == NULL)
681 err = got_error_from_errno("fclose");
682 free(obj->data);
683 free(obj);
684 return err;
687 const struct got_error *
688 got_object_open_by_id_str(struct got_object **obj, struct got_repository *repo,
689 const char *id_str)
691 struct got_object_id id;
693 if (!got_parse_sha1_digest(id.sha1, id_str))
694 return got_error_path(id_str, GOT_ERR_BAD_OBJ_ID_STR);
696 return got_object_open(obj, repo, &id);
699 const struct got_error *
700 got_object_resolve_id_str(struct got_object_id **id,
701 struct got_repository *repo, const char *id_str)
703 const struct got_error *err = NULL;
704 struct got_object *obj;
706 err = got_object_open_by_id_str(&obj, repo, id_str);
707 if (err)
708 return err;
710 *id = got_object_id_dup(got_object_get_id(obj));
711 got_object_close(obj);
712 if (*id == NULL)
713 return got_error_from_errno("got_object_id_dup");
715 return NULL;
718 static const struct got_error *
719 request_packed_commit(struct got_commit_object **commit, struct got_pack *pack,
720 int pack_idx, struct got_object_id *id)
722 const struct got_error *err = NULL;
724 err = got_privsep_send_commit_req(pack->privsep_child->ibuf, -1, id,
725 pack_idx);
726 if (err)
727 return err;
729 err = got_privsep_recv_commit(commit, pack->privsep_child->ibuf);
730 if (err)
731 return err;
733 (*commit)->flags |= GOT_COMMIT_FLAG_PACKED;
734 return NULL;
737 static const struct got_error *
738 read_packed_commit_privsep(struct got_commit_object **commit,
739 struct got_pack *pack, struct got_packidx *packidx, int idx,
740 struct got_object_id *id)
742 const struct got_error *err = NULL;
744 if (pack->privsep_child)
745 return request_packed_commit(commit, pack, idx, id);
747 err = start_pack_privsep_child(pack, packidx);
748 if (err)
749 return err;
751 return request_packed_commit(commit, pack, idx, id);
754 static const struct got_error *
755 request_commit(struct got_commit_object **commit, struct got_repository *repo,
756 int fd, struct got_object_id *id)
758 const struct got_error *err = NULL;
759 struct imsgbuf *ibuf;
761 ibuf = repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_COMMIT].ibuf;
763 err = got_privsep_send_commit_req(ibuf, fd, id, -1);
764 if (err)
765 return err;
767 return got_privsep_recv_commit(commit, ibuf);
770 static const struct got_error *
771 read_commit_privsep(struct got_commit_object **commit, int obj_fd,
772 struct got_object_id *id, struct got_repository *repo)
774 const struct got_error *err;
775 int imsg_fds[2];
776 pid_t pid;
777 struct imsgbuf *ibuf;
779 if (repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_COMMIT].imsg_fd != -1)
780 return request_commit(commit, repo, obj_fd, id);
782 ibuf = calloc(1, sizeof(*ibuf));
783 if (ibuf == NULL)
784 return got_error_from_errno("calloc");
786 if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_fds) == -1) {
787 err = got_error_from_errno("socketpair");
788 free(ibuf);
789 return err;
792 pid = fork();
793 if (pid == -1) {
794 err = got_error_from_errno("fork");
795 free(ibuf);
796 return err;
798 else if (pid == 0) {
799 got_privsep_exec_child(imsg_fds, GOT_PATH_PROG_READ_COMMIT,
800 repo->path);
801 /* not reached */
804 if (close(imsg_fds[1]) == -1) {
805 err = got_error_from_errno("close");
806 free(ibuf);
807 return err;
809 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_COMMIT].imsg_fd =
810 imsg_fds[0];
811 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_COMMIT].pid = pid;
812 imsg_init(ibuf, imsg_fds[0]);
813 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_COMMIT].ibuf = ibuf;
815 return request_commit(commit, repo, obj_fd, id);
819 static const struct got_error *
820 open_commit(struct got_commit_object **commit,
821 struct got_repository *repo, struct got_object_id *id, int check_cache)
823 const struct got_error *err = NULL;
824 struct got_packidx *packidx = NULL;
825 int idx;
826 char *path_packfile = NULL;
828 if (check_cache) {
829 *commit = got_repo_get_cached_commit(repo, id);
830 if (*commit != NULL) {
831 (*commit)->refcnt++;
832 return NULL;
834 } else
835 *commit = NULL;
837 err = got_repo_search_packidx(&packidx, &idx, repo, id);
838 if (err == NULL) {
839 struct got_pack *pack = NULL;
841 err = got_packidx_get_packfile_path(&path_packfile,
842 packidx->path_packidx);
843 if (err)
844 return err;
846 pack = got_repo_get_cached_pack(repo, path_packfile);
847 if (pack == NULL) {
848 err = got_repo_cache_pack(&pack, repo, path_packfile,
849 packidx);
850 if (err)
851 goto done;
853 err = read_packed_commit_privsep(commit, pack,
854 packidx, idx, id);
855 } else if (err->code == GOT_ERR_NO_OBJ) {
856 int fd;
858 err = got_object_open_loose_fd(&fd, id, repo);
859 if (err)
860 return err;
861 err = read_commit_privsep(commit, fd, id, repo);
864 if (err == NULL) {
865 (*commit)->refcnt++;
866 err = got_repo_cache_commit(repo, id, *commit);
868 done:
869 free(path_packfile);
870 return err;
873 const struct got_error *
874 got_object_open_as_commit(struct got_commit_object **commit,
875 struct got_repository *repo, struct got_object_id *id)
877 *commit = got_repo_get_cached_commit(repo, id);
878 if (*commit != NULL) {
879 (*commit)->refcnt++;
880 return NULL;
883 return open_commit(commit, repo, id, 0);
886 const struct got_error *
887 got_object_commit_open(struct got_commit_object **commit,
888 struct got_repository *repo, struct got_object *obj)
890 return open_commit(commit, repo, got_object_get_id(obj), 1);
893 const struct got_error *
894 got_object_qid_alloc(struct got_object_qid **qid, struct got_object_id *id)
896 const struct got_error *err = NULL;
898 *qid = calloc(1, sizeof(**qid));
899 if (*qid == NULL)
900 return got_error_from_errno("calloc");
902 (*qid)->id = got_object_id_dup(id);
903 if ((*qid)->id == NULL) {
904 err = got_error_from_errno("got_object_id_dup");
905 got_object_qid_free(*qid);
906 *qid = NULL;
907 return err;
910 return NULL;
913 const struct got_error *
914 got_object_id_queue_copy(const struct got_object_id_queue *src,
915 struct got_object_id_queue *dest)
917 const struct got_error *err;
918 struct got_object_qid *qid;
920 STAILQ_FOREACH(qid, src, entry) {
921 struct got_object_qid *new;
922 /*
923 * Deep-copy the object ID only. Let the caller deal
924 * with setting up the new->data pointer if needed.
925 */
926 err = got_object_qid_alloc(&new, qid->id);
927 if (err) {
928 got_object_id_queue_free(dest);
929 return err;
931 STAILQ_INSERT_TAIL(dest, new, entry);
934 return NULL;
937 static const struct got_error *
938 request_packed_tree(struct got_tree_object **tree, struct got_pack *pack,
939 int pack_idx, struct got_object_id *id)
941 const struct got_error *err = NULL;
943 err = got_privsep_send_tree_req(pack->privsep_child->ibuf, -1, id,
944 pack_idx);
945 if (err)
946 return err;
948 return got_privsep_recv_tree(tree, pack->privsep_child->ibuf);
951 static const struct got_error *
952 read_packed_tree_privsep(struct got_tree_object **tree,
953 struct got_pack *pack, struct got_packidx *packidx, int idx,
954 struct got_object_id *id)
956 const struct got_error *err = NULL;
958 if (pack->privsep_child)
959 return request_packed_tree(tree, pack, idx, id);
961 err = start_pack_privsep_child(pack, packidx);
962 if (err)
963 return err;
965 return request_packed_tree(tree, pack, idx, id);
968 static const struct got_error *
969 request_tree(struct got_tree_object **tree, struct got_repository *repo,
970 int fd, struct got_object_id *id)
972 const struct got_error *err = NULL;
973 struct imsgbuf *ibuf;
975 ibuf = repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TREE].ibuf;
977 err = got_privsep_send_tree_req(ibuf, fd, id, -1);
978 if (err)
979 return err;
981 return got_privsep_recv_tree(tree, ibuf);
984 const struct got_error *
985 read_tree_privsep(struct got_tree_object **tree, int obj_fd,
986 struct got_object_id *id, struct got_repository *repo)
988 const struct got_error *err;
989 int imsg_fds[2];
990 pid_t pid;
991 struct imsgbuf *ibuf;
993 if (repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TREE].imsg_fd != -1)
994 return request_tree(tree, repo, obj_fd, id);
996 ibuf = calloc(1, sizeof(*ibuf));
997 if (ibuf == NULL)
998 return got_error_from_errno("calloc");
1000 if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_fds) == -1) {
1001 err = got_error_from_errno("socketpair");
1002 free(ibuf);
1003 return err;
1006 pid = fork();
1007 if (pid == -1) {
1008 err = got_error_from_errno("fork");
1009 free(ibuf);
1010 return err;
1012 else if (pid == 0) {
1013 got_privsep_exec_child(imsg_fds, GOT_PATH_PROG_READ_TREE,
1014 repo->path);
1015 /* not reached */
1018 if (close(imsg_fds[1]) == -1) {
1019 err = got_error_from_errno("close");
1020 free(ibuf);
1021 return err;
1023 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TREE].imsg_fd =
1024 imsg_fds[0];
1025 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TREE].pid = pid;
1026 imsg_init(ibuf, imsg_fds[0]);
1027 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TREE].ibuf = ibuf;
1030 return request_tree(tree, repo, obj_fd, id);
1033 static const struct got_error *
1034 open_tree(struct got_tree_object **tree, struct got_repository *repo,
1035 struct got_object_id *id, int check_cache)
1037 const struct got_error *err = NULL;
1038 struct got_packidx *packidx = NULL;
1039 int idx;
1040 char *path_packfile = NULL;
1042 if (check_cache) {
1043 *tree = got_repo_get_cached_tree(repo, id);
1044 if (*tree != NULL) {
1045 (*tree)->refcnt++;
1046 return NULL;
1048 } else
1049 *tree = NULL;
1051 err = got_repo_search_packidx(&packidx, &idx, repo, id);
1052 if (err == NULL) {
1053 struct got_pack *pack = NULL;
1055 err = got_packidx_get_packfile_path(&path_packfile,
1056 packidx->path_packidx);
1057 if (err)
1058 return err;
1060 pack = got_repo_get_cached_pack(repo, path_packfile);
1061 if (pack == NULL) {
1062 err = got_repo_cache_pack(&pack, repo, path_packfile,
1063 packidx);
1064 if (err)
1065 goto done;
1067 err = read_packed_tree_privsep(tree, pack,
1068 packidx, idx, id);
1069 } else if (err->code == GOT_ERR_NO_OBJ) {
1070 int fd;
1072 err = got_object_open_loose_fd(&fd, id, repo);
1073 if (err)
1074 return err;
1075 err = read_tree_privsep(tree, fd, id, repo);
1078 if (err == NULL) {
1079 (*tree)->refcnt++;
1080 err = got_repo_cache_tree(repo, id, *tree);
1082 done:
1083 free(path_packfile);
1084 return err;
1087 const struct got_error *
1088 got_object_open_as_tree(struct got_tree_object **tree,
1089 struct got_repository *repo, struct got_object_id *id)
1091 *tree = got_repo_get_cached_tree(repo, id);
1092 if (*tree != NULL) {
1093 (*tree)->refcnt++;
1094 return NULL;
1097 return open_tree(tree, repo, id, 0);
1100 const struct got_error *
1101 got_object_tree_open(struct got_tree_object **tree,
1102 struct got_repository *repo, struct got_object *obj)
1104 return open_tree(tree, repo, got_object_get_id(obj), 1);
1107 int
1108 got_object_tree_get_nentries(struct got_tree_object *tree)
1110 return tree->nentries;
1113 struct got_tree_entry *
1114 got_object_tree_get_first_entry(struct got_tree_object *tree)
1116 return got_object_tree_get_entry(tree, 0);
1119 struct got_tree_entry *
1120 got_object_tree_get_last_entry(struct got_tree_object *tree)
1122 return got_object_tree_get_entry(tree, tree->nentries - 1);
1125 struct got_tree_entry *
1126 got_object_tree_get_entry(struct got_tree_object *tree, int i)
1128 if (i < 0 || i >= tree->nentries)
1129 return NULL;
1130 return &tree->entries[i];
1133 mode_t
1134 got_tree_entry_get_mode(struct got_tree_entry *te)
1136 return te->mode;
1139 const char *
1140 got_tree_entry_get_name(struct got_tree_entry *te)
1142 return &te->name[0];
1145 struct got_object_id *
1146 got_tree_entry_get_id(struct got_tree_entry *te)
1148 return &te->id;
1151 const struct got_error *
1152 got_object_blob_read_to_str(char **s, struct got_blob_object *blob)
1154 const struct got_error *err = NULL;
1155 size_t len, totlen, hdrlen, offset;
1157 *s = NULL;
1159 hdrlen = got_object_blob_get_hdrlen(blob);
1160 totlen = 0;
1161 offset = 0;
1162 do {
1163 char *p;
1165 err = got_object_blob_read_block(&len, blob);
1166 if (err)
1167 return err;
1169 if (len == 0)
1170 break;
1172 totlen += len - hdrlen;
1173 p = realloc(*s, totlen + 1);
1174 if (p == NULL) {
1175 err = got_error_from_errno("realloc");
1176 free(*s);
1177 *s = NULL;
1178 return err;
1180 *s = p;
1181 /* Skip blob object header first time around. */
1182 memcpy(*s + offset,
1183 got_object_blob_get_read_buf(blob) + hdrlen, len - hdrlen);
1184 hdrlen = 0;
1185 offset = totlen;
1186 } while (len > 0);
1188 (*s)[totlen] = '\0';
1189 return NULL;
1192 const struct got_error *
1193 got_tree_entry_get_symlink_target(char **link_target, struct got_tree_entry *te,
1194 struct got_repository *repo)
1196 const struct got_error *err = NULL;
1197 struct got_blob_object *blob = NULL;
1199 *link_target = NULL;
1201 if (!got_object_tree_entry_is_symlink(te))
1202 return got_error(GOT_ERR_TREE_ENTRY_TYPE);
1204 err = got_object_open_as_blob(&blob, repo,
1205 got_tree_entry_get_id(te), PATH_MAX);
1206 if (err)
1207 return err;
1209 err = got_object_blob_read_to_str(link_target, blob);
1210 got_object_blob_close(blob);
1211 if (err) {
1212 free(*link_target);
1213 *link_target = NULL;
1215 return err;
1218 int
1219 got_tree_entry_get_index(struct got_tree_entry *te)
1221 return te->idx;
1224 struct got_tree_entry *
1225 got_tree_entry_get_next(struct got_tree_object *tree,
1226 struct got_tree_entry *te)
1228 return got_object_tree_get_entry(tree, te->idx + 1);
1231 struct got_tree_entry *
1232 got_tree_entry_get_prev(struct got_tree_object *tree,
1233 struct got_tree_entry *te)
1235 return got_object_tree_get_entry(tree, te->idx - 1);
1238 static const struct got_error *
1239 request_packed_blob(uint8_t **outbuf, size_t *size, size_t *hdrlen, int outfd,
1240 struct got_pack *pack, struct got_packidx *packidx, int idx,
1241 struct got_object_id *id)
1243 const struct got_error *err = NULL;
1244 int outfd_child;
1245 int basefd, accumfd; /* temporary files for delta application */
1247 basefd = got_opentempfd();
1248 if (basefd == -1)
1249 return got_error_from_errno("got_opentempfd");
1250 accumfd = got_opentempfd();
1251 if (accumfd == -1)
1252 return got_error_from_errno("got_opentempfd");
1254 outfd_child = dup(outfd);
1255 if (outfd_child == -1)
1256 return got_error_from_errno("dup");
1258 err = got_privsep_send_blob_req(pack->privsep_child->ibuf, -1, id, idx);
1259 if (err)
1260 return err;
1262 err = got_privsep_send_blob_outfd(pack->privsep_child->ibuf,
1263 outfd_child);
1264 if (err) {
1265 close(basefd);
1266 close(accumfd);
1267 return err;
1270 err = got_privsep_send_tmpfd(pack->privsep_child->ibuf,
1271 basefd);
1272 if (err) {
1273 close(accumfd);
1274 return err;
1277 err = got_privsep_send_tmpfd(pack->privsep_child->ibuf,
1278 accumfd);
1279 if (err)
1280 return err;
1282 err = got_privsep_recv_blob(outbuf, size, hdrlen,
1283 pack->privsep_child->ibuf);
1284 if (err)
1285 return err;
1287 if (lseek(outfd, SEEK_SET, 0) == -1)
1288 err = got_error_from_errno("lseek");
1290 return err;
1293 static const struct got_error *
1294 read_packed_blob_privsep(uint8_t **outbuf, size_t *size, size_t *hdrlen,
1295 int outfd, struct got_pack *pack, struct got_packidx *packidx, int idx,
1296 struct got_object_id *id)
1298 const struct got_error *err = NULL;
1300 if (pack->privsep_child == NULL) {
1301 err = start_pack_privsep_child(pack, packidx);
1302 if (err)
1303 return err;
1306 return request_packed_blob(outbuf, size, hdrlen, outfd, pack, packidx,
1307 idx, id);
1310 static const struct got_error *
1311 request_blob(uint8_t **outbuf, size_t *size, size_t *hdrlen, int outfd,
1312 int infd, struct got_object_id *id, struct imsgbuf *ibuf)
1314 const struct got_error *err = NULL;
1315 int outfd_child;
1317 outfd_child = dup(outfd);
1318 if (outfd_child == -1)
1319 return got_error_from_errno("dup");
1321 err = got_privsep_send_blob_req(ibuf, infd, id, -1);
1322 if (err)
1323 return err;
1325 err = got_privsep_send_blob_outfd(ibuf, outfd_child);
1326 if (err)
1327 return err;
1329 err = got_privsep_recv_blob(outbuf, size, hdrlen, ibuf);
1330 if (err)
1331 return err;
1333 if (lseek(outfd, SEEK_SET, 0) == -1)
1334 return got_error_from_errno("lseek");
1336 return err;
1339 static const struct got_error *
1340 read_blob_privsep(uint8_t **outbuf, size_t *size, size_t *hdrlen,
1341 int outfd, int infd, struct got_object_id *id, struct got_repository *repo)
1343 const struct got_error *err;
1344 int imsg_fds[2];
1345 pid_t pid;
1346 struct imsgbuf *ibuf;
1348 if (repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_BLOB].imsg_fd != -1) {
1349 ibuf = repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_BLOB].ibuf;
1350 return request_blob(outbuf, size, hdrlen, outfd, infd, id,
1351 ibuf);
1354 ibuf = calloc(1, sizeof(*ibuf));
1355 if (ibuf == NULL)
1356 return got_error_from_errno("calloc");
1358 if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_fds) == -1) {
1359 err = got_error_from_errno("socketpair");
1360 free(ibuf);
1361 return err;
1364 pid = fork();
1365 if (pid == -1) {
1366 err = got_error_from_errno("fork");
1367 free(ibuf);
1368 return err;
1370 else if (pid == 0) {
1371 got_privsep_exec_child(imsg_fds, GOT_PATH_PROG_READ_BLOB,
1372 repo->path);
1373 /* not reached */
1376 if (close(imsg_fds[1]) == -1) {
1377 err = got_error_from_errno("close");
1378 free(ibuf);
1379 return err;
1381 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_BLOB].imsg_fd =
1382 imsg_fds[0];
1383 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_BLOB].pid = pid;
1384 imsg_init(ibuf, imsg_fds[0]);
1385 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_BLOB].ibuf = ibuf;
1387 return request_blob(outbuf, size, hdrlen, outfd, infd, id, ibuf);
1390 static const struct got_error *
1391 open_blob(struct got_blob_object **blob, struct got_repository *repo,
1392 struct got_object_id *id, size_t blocksize)
1394 const struct got_error *err = NULL;
1395 struct got_packidx *packidx = NULL;
1396 int idx;
1397 char *path_packfile = NULL;
1398 uint8_t *outbuf;
1399 int outfd;
1400 size_t size, hdrlen;
1401 struct stat sb;
1403 *blob = calloc(1, sizeof(**blob));
1404 if (*blob == NULL)
1405 return got_error_from_errno("calloc");
1407 outfd = got_opentempfd();
1408 if (outfd == -1)
1409 return got_error_from_errno("got_opentempfd");
1411 (*blob)->read_buf = malloc(blocksize);
1412 if ((*blob)->read_buf == NULL) {
1413 err = got_error_from_errno("malloc");
1414 goto done;
1417 err = got_repo_search_packidx(&packidx, &idx, repo, id);
1418 if (err == NULL) {
1419 struct got_pack *pack = NULL;
1421 err = got_packidx_get_packfile_path(&path_packfile,
1422 packidx->path_packidx);
1423 if (err)
1424 goto done;
1426 pack = got_repo_get_cached_pack(repo, path_packfile);
1427 if (pack == NULL) {
1428 err = got_repo_cache_pack(&pack, repo, path_packfile,
1429 packidx);
1430 if (err)
1431 goto done;
1433 err = read_packed_blob_privsep(&outbuf, &size, &hdrlen, outfd,
1434 pack, packidx, idx, id);
1435 } else if (err->code == GOT_ERR_NO_OBJ) {
1436 int infd;
1438 err = got_object_open_loose_fd(&infd, id, repo);
1439 if (err)
1440 goto done;
1441 err = read_blob_privsep(&outbuf, &size, &hdrlen, outfd, infd,
1442 id, repo);
1444 if (err)
1445 goto done;
1447 if (hdrlen > size) {
1448 err = got_error(GOT_ERR_BAD_OBJ_HDR);
1449 goto done;
1452 if (outbuf) {
1453 if (close(outfd) == -1 && err == NULL)
1454 err = got_error_from_errno("close");
1455 outfd = -1;
1456 (*blob)->f = fmemopen(outbuf, size, "rb");
1457 if ((*blob)->f == NULL) {
1458 err = got_error_from_errno("fmemopen");
1459 free(outbuf);
1460 goto done;
1462 (*blob)->data = outbuf;
1463 } else {
1464 if (fstat(outfd, &sb) == -1) {
1465 err = got_error_from_errno("fstat");
1466 goto done;
1469 if (sb.st_size != size) {
1470 err = got_error(GOT_ERR_PRIVSEP_LEN);
1471 goto done;
1474 (*blob)->f = fdopen(outfd, "rb");
1475 if ((*blob)->f == NULL) {
1476 err = got_error_from_errno("fdopen");
1477 close(outfd);
1478 outfd = -1;
1479 goto done;
1483 (*blob)->hdrlen = hdrlen;
1484 (*blob)->blocksize = blocksize;
1485 memcpy(&(*blob)->id.sha1, id->sha1, SHA1_DIGEST_LENGTH);
1487 done:
1488 free(path_packfile);
1489 if (err) {
1490 if (*blob) {
1491 got_object_blob_close(*blob);
1492 *blob = NULL;
1493 } else if (outfd != -1)
1494 close(outfd);
1496 return err;
1499 const struct got_error *
1500 got_object_open_as_blob(struct got_blob_object **blob,
1501 struct got_repository *repo, struct got_object_id *id,
1502 size_t blocksize)
1504 return open_blob(blob, repo, id, blocksize);
1507 const struct got_error *
1508 got_object_blob_open(struct got_blob_object **blob,
1509 struct got_repository *repo, struct got_object *obj, size_t blocksize)
1511 return open_blob(blob, repo, got_object_get_id(obj), blocksize);
1514 const struct got_error *
1515 got_object_blob_close(struct got_blob_object *blob)
1517 const struct got_error *err = NULL;
1518 free(blob->read_buf);
1519 if (blob->f && fclose(blob->f) == EOF)
1520 err = got_error_from_errno("fclose");
1521 free(blob->data);
1522 free(blob);
1523 return err;
1526 void
1527 got_object_blob_rewind(struct got_blob_object *blob)
1529 if (blob->f)
1530 rewind(blob->f);
1533 char *
1534 got_object_blob_id_str(struct got_blob_object *blob, char *buf, size_t size)
1536 return got_sha1_digest_to_str(blob->id.sha1, buf, size);
1539 size_t
1540 got_object_blob_get_hdrlen(struct got_blob_object *blob)
1542 return blob->hdrlen;
1545 const uint8_t *
1546 got_object_blob_get_read_buf(struct got_blob_object *blob)
1548 return blob->read_buf;
1551 const struct got_error *
1552 got_object_blob_read_block(size_t *outlenp, struct got_blob_object *blob)
1554 size_t n;
1556 n = fread(blob->read_buf, 1, blob->blocksize, blob->f);
1557 if (n == 0 && ferror(blob->f))
1558 return got_ferror(blob->f, GOT_ERR_IO);
1559 *outlenp = n;
1560 return NULL;
1563 const struct got_error *
1564 got_object_blob_dump_to_file(off_t *filesize, int *nlines,
1565 off_t **line_offsets, FILE *outfile, struct got_blob_object *blob)
1567 const struct got_error *err = NULL;
1568 size_t n, len, hdrlen;
1569 const uint8_t *buf;
1570 int i;
1571 const int alloc_chunksz = 512;
1572 size_t nalloc = 0;
1573 off_t off = 0, total_len = 0;
1575 if (line_offsets)
1576 *line_offsets = NULL;
1577 if (filesize)
1578 *filesize = 0;
1579 if (nlines)
1580 *nlines = 0;
1582 hdrlen = got_object_blob_get_hdrlen(blob);
1583 do {
1584 err = got_object_blob_read_block(&len, blob);
1585 if (err)
1586 return err;
1587 if (len == 0)
1588 break;
1589 buf = got_object_blob_get_read_buf(blob);
1590 i = hdrlen;
1591 if (nlines) {
1592 if (line_offsets && *line_offsets == NULL) {
1593 /* Have some data but perhaps no '\n'. */
1594 *nlines = 1;
1595 nalloc = alloc_chunksz;
1596 *line_offsets = calloc(nalloc,
1597 sizeof(**line_offsets));
1598 if (*line_offsets == NULL)
1599 return got_error_from_errno("calloc");
1601 /* Skip forward over end of first line. */
1602 while (i < len) {
1603 if (buf[i] == '\n')
1604 break;
1605 i++;
1608 /* Scan '\n' offsets in remaining chunk of data. */
1609 while (i < len) {
1610 if (buf[i] != '\n') {
1611 i++;
1612 continue;
1614 (*nlines)++;
1615 if (line_offsets && nalloc < *nlines) {
1616 size_t n = *nlines + alloc_chunksz;
1617 off_t *o = recallocarray(*line_offsets,
1618 nalloc, n, sizeof(**line_offsets));
1619 if (o == NULL) {
1620 free(*line_offsets);
1621 *line_offsets = NULL;
1622 return got_error_from_errno(
1623 "recallocarray");
1625 *line_offsets = o;
1626 nalloc = n;
1628 if (line_offsets) {
1629 off = total_len + i - hdrlen + 1;
1630 (*line_offsets)[*nlines - 1] = off;
1632 i++;
1635 /* Skip blob object header first time around. */
1636 n = fwrite(buf + hdrlen, 1, len - hdrlen, outfile);
1637 if (n != len - hdrlen)
1638 return got_ferror(outfile, GOT_ERR_IO);
1639 total_len += len - hdrlen;
1640 hdrlen = 0;
1641 } while (len != 0);
1643 if (fflush(outfile) != 0)
1644 return got_error_from_errno("fflush");
1645 rewind(outfile);
1647 if (filesize)
1648 *filesize = total_len;
1650 return NULL;
1653 static const struct got_error *
1654 request_packed_tag(struct got_tag_object **tag, struct got_pack *pack,
1655 int pack_idx, struct got_object_id *id)
1657 const struct got_error *err = NULL;
1659 err = got_privsep_send_tag_req(pack->privsep_child->ibuf, -1, id,
1660 pack_idx);
1661 if (err)
1662 return err;
1664 return got_privsep_recv_tag(tag, pack->privsep_child->ibuf);
1667 static const struct got_error *
1668 read_packed_tag_privsep(struct got_tag_object **tag,
1669 struct got_pack *pack, struct got_packidx *packidx, int idx,
1670 struct got_object_id *id)
1672 const struct got_error *err = NULL;
1674 if (pack->privsep_child)
1675 return request_packed_tag(tag, pack, idx, id);
1677 err = start_pack_privsep_child(pack, packidx);
1678 if (err)
1679 return err;
1681 return request_packed_tag(tag, pack, idx, id);
1684 static const struct got_error *
1685 request_tag(struct got_tag_object **tag, struct got_repository *repo,
1686 int fd, struct got_object_id *id)
1688 const struct got_error *err = NULL;
1689 struct imsgbuf *ibuf;
1691 ibuf = repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TAG].ibuf;
1693 err = got_privsep_send_tag_req(ibuf, fd, id, -1);
1694 if (err)
1695 return err;
1697 return got_privsep_recv_tag(tag, ibuf);
1700 static const struct got_error *
1701 read_tag_privsep(struct got_tag_object **tag, int obj_fd,
1702 struct got_object_id *id, struct got_repository *repo)
1704 const struct got_error *err;
1705 int imsg_fds[2];
1706 pid_t pid;
1707 struct imsgbuf *ibuf;
1709 if (repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TAG].imsg_fd != -1)
1710 return request_tag(tag, repo, obj_fd, id);
1712 ibuf = calloc(1, sizeof(*ibuf));
1713 if (ibuf == NULL)
1714 return got_error_from_errno("calloc");
1716 if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_fds) == -1) {
1717 err = got_error_from_errno("socketpair");
1718 free(ibuf);
1719 return err;
1722 pid = fork();
1723 if (pid == -1) {
1724 err = got_error_from_errno("fork");
1725 free(ibuf);
1726 return err;
1728 else if (pid == 0) {
1729 got_privsep_exec_child(imsg_fds, GOT_PATH_PROG_READ_TAG,
1730 repo->path);
1731 /* not reached */
1734 if (close(imsg_fds[1]) == -1) {
1735 err = got_error_from_errno("close");
1736 free(ibuf);
1737 return err;
1739 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TAG].imsg_fd =
1740 imsg_fds[0];
1741 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TAG].pid = pid;
1742 imsg_init(ibuf, imsg_fds[0]);
1743 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TAG].ibuf = ibuf;
1745 return request_tag(tag, repo, obj_fd, id);
1748 static const struct got_error *
1749 open_tag(struct got_tag_object **tag, struct got_repository *repo,
1750 struct got_object_id *id, int check_cache)
1752 const struct got_error *err = NULL;
1753 struct got_packidx *packidx = NULL;
1754 int idx;
1755 char *path_packfile = NULL;
1756 struct got_object *obj = NULL;
1757 int obj_type = GOT_OBJ_TYPE_ANY;
1759 if (check_cache) {
1760 *tag = got_repo_get_cached_tag(repo, id);
1761 if (*tag != NULL) {
1762 (*tag)->refcnt++;
1763 return NULL;
1765 } else
1766 *tag = NULL;
1768 err = got_repo_search_packidx(&packidx, &idx, repo, id);
1769 if (err == NULL) {
1770 struct got_pack *pack = NULL;
1772 err = got_packidx_get_packfile_path(&path_packfile,
1773 packidx->path_packidx);
1774 if (err)
1775 return err;
1777 pack = got_repo_get_cached_pack(repo, path_packfile);
1778 if (pack == NULL) {
1779 err = got_repo_cache_pack(&pack, repo, path_packfile,
1780 packidx);
1781 if (err)
1782 goto done;
1785 /* Beware of "lightweight" tags: Check object type first. */
1786 err = read_packed_object_privsep(&obj, repo, pack, packidx,
1787 idx, id);
1788 if (err)
1789 goto done;
1790 obj_type = obj->type;
1791 got_object_close(obj);
1792 if (obj_type != GOT_OBJ_TYPE_TAG) {
1793 err = got_error(GOT_ERR_OBJ_TYPE);
1794 goto done;
1796 err = read_packed_tag_privsep(tag, pack, packidx, idx, id);
1797 } else if (err->code == GOT_ERR_NO_OBJ) {
1798 int fd;
1800 err = got_object_open_loose_fd(&fd, id, repo);
1801 if (err)
1802 return err;
1803 err = got_object_read_header_privsep(&obj, id, repo, fd);
1804 if (err)
1805 return err;
1806 obj_type = obj->type;
1807 got_object_close(obj);
1808 if (obj_type != GOT_OBJ_TYPE_TAG)
1809 return got_error(GOT_ERR_OBJ_TYPE);
1811 err = got_object_open_loose_fd(&fd, id, repo);
1812 if (err)
1813 return err;
1814 err = read_tag_privsep(tag, fd, id, repo);
1817 if (err == NULL) {
1818 (*tag)->refcnt++;
1819 err = got_repo_cache_tag(repo, id, *tag);
1821 done:
1822 free(path_packfile);
1823 return err;
1826 const struct got_error *
1827 got_object_open_as_tag(struct got_tag_object **tag,
1828 struct got_repository *repo, struct got_object_id *id)
1830 *tag = got_repo_get_cached_tag(repo, id);
1831 if (*tag != NULL) {
1832 (*tag)->refcnt++;
1833 return NULL;
1836 return open_tag(tag, repo, id, 0);
1839 const struct got_error *
1840 got_object_tag_open(struct got_tag_object **tag,
1841 struct got_repository *repo, struct got_object *obj)
1843 return open_tag(tag, repo, got_object_get_id(obj), 1);
1846 const char *
1847 got_object_tag_get_name(struct got_tag_object *tag)
1849 return tag->tag;
1852 int
1853 got_object_tag_get_object_type(struct got_tag_object *tag)
1855 return tag->obj_type;
1858 struct got_object_id *
1859 got_object_tag_get_object_id(struct got_tag_object *tag)
1861 return &tag->id;
1864 time_t
1865 got_object_tag_get_tagger_time(struct got_tag_object *tag)
1867 return tag->tagger_time;
1870 time_t
1871 got_object_tag_get_tagger_gmtoff(struct got_tag_object *tag)
1873 return tag->tagger_gmtoff;
1876 const char *
1877 got_object_tag_get_tagger(struct got_tag_object *tag)
1879 return tag->tagger;
1882 const char *
1883 got_object_tag_get_message(struct got_tag_object *tag)
1885 return tag->tagmsg;
1888 static struct got_tree_entry *
1889 find_entry_by_name(struct got_tree_object *tree, const char *name, size_t len)
1891 int i;
1893 /* Note that tree entries are sorted in strncmp() order. */
1894 for (i = 0; i < tree->nentries; i++) {
1895 struct got_tree_entry *te = &tree->entries[i];
1896 int cmp = strncmp(te->name, name, len);
1897 if (cmp < 0)
1898 continue;
1899 if (cmp > 0)
1900 break;
1901 if (te->name[len] == '\0')
1902 return te;
1904 return NULL;
1907 struct got_tree_entry *
1908 got_object_tree_find_entry(struct got_tree_object *tree, const char *name)
1910 return find_entry_by_name(tree, name, strlen(name));
1913 const struct got_error *
1914 got_object_tree_find_path(struct got_object_id **id, mode_t *mode,
1915 struct got_repository *repo, struct got_tree_object *tree,
1916 const char *path)
1918 const struct got_error *err = NULL;
1919 struct got_tree_object *subtree = NULL;
1920 struct got_tree_entry *te = NULL;
1921 const char *seg, *s;
1922 size_t seglen;
1924 *id = NULL;
1926 s = path;
1927 while (s[0] == '/')
1928 s++;
1929 seg = s;
1930 seglen = 0;
1931 subtree = tree;
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(subtree, 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 if (subtree != tree)
1961 got_object_tree_close(subtree);
1962 subtree = next_tree;
1966 if (te) {
1967 *id = got_object_id_dup(&te->id);
1968 if (*id == NULL)
1969 return got_error_from_errno("got_object_id_dup");
1970 if (mode)
1971 *mode = te->mode;
1972 } else
1973 err = got_error_path(path, GOT_ERR_NO_TREE_ENTRY);
1974 done:
1975 if (subtree && subtree != tree)
1976 got_object_tree_close(subtree);
1977 return err;
1979 const struct got_error *
1980 got_object_id_by_path(struct got_object_id **id, struct got_repository *repo,
1981 struct got_object_id *commit_id, const char *path)
1983 const struct got_error *err = NULL;
1984 struct got_commit_object *commit = NULL;
1985 struct got_tree_object *tree = NULL;
1987 *id = NULL;
1989 err = got_object_open_as_commit(&commit, repo, commit_id);
1990 if (err)
1991 goto done;
1993 /* Handle opening of root of commit's tree. */
1994 if (got_path_is_root_dir(path)) {
1995 *id = got_object_id_dup(commit->tree_id);
1996 if (*id == NULL)
1997 err = got_error_from_errno("got_object_id_dup");
1998 } else {
1999 err = got_object_open_as_tree(&tree, repo, commit->tree_id);
2000 if (err)
2001 goto done;
2002 err = got_object_tree_find_path(id, NULL, repo, tree, path);
2004 done:
2005 if (commit)
2006 got_object_commit_close(commit);
2007 if (tree)
2008 got_object_tree_close(tree);
2009 return err;
2013 * Normalize file mode bits to avoid false positive tree entry differences
2014 * in case tree entries have unexpected mode bits set.
2016 static mode_t
2017 normalize_mode_for_comparison(mode_t mode)
2020 * For directories, the only relevant bit is the IFDIR bit.
2021 * This allows us to detect paths changing from a directory
2022 * to a file and vice versa.
2024 if (S_ISDIR(mode))
2025 return mode & S_IFDIR;
2028 * For symlinks, the only relevant bit is the IFLNK bit.
2029 * This allows us to detect paths changing from a symlinks
2030 * to a file or directory and vice versa.
2032 if (S_ISLNK(mode))
2033 return mode & S_IFLNK;
2035 /* For files, the only change we care about is the executable bit. */
2036 return mode & S_IXUSR;
2039 const struct got_error *
2040 got_object_tree_path_changed(int *changed,
2041 struct got_tree_object *tree01, struct got_tree_object *tree02,
2042 const char *path, struct got_repository *repo)
2044 const struct got_error *err = NULL;
2045 struct got_tree_object *tree1 = NULL, *tree2 = NULL;
2046 struct got_tree_entry *te1 = NULL, *te2 = NULL;
2047 const char *seg, *s;
2048 size_t seglen;
2050 *changed = 0;
2052 /* We not do support comparing the root path. */
2053 if (got_path_is_root_dir(path))
2054 return got_error_path(path, GOT_ERR_BAD_PATH);
2056 tree1 = tree01;
2057 tree2 = tree02;
2058 s = path;
2059 while (*s == '/')
2060 s++;
2061 seg = s;
2062 seglen = 0;
2063 while (*s) {
2064 struct got_tree_object *next_tree1, *next_tree2;
2065 mode_t mode1, mode2;
2067 if (*s != '/') {
2068 s++;
2069 seglen++;
2070 if (*s)
2071 continue;
2074 te1 = find_entry_by_name(tree1, seg, seglen);
2075 if (te1 == NULL) {
2076 err = got_error(GOT_ERR_NO_OBJ);
2077 goto done;
2080 if (tree2)
2081 te2 = find_entry_by_name(tree2, seg, seglen);
2083 if (te2) {
2084 mode1 = normalize_mode_for_comparison(te1->mode);
2085 mode2 = normalize_mode_for_comparison(te2->mode);
2086 if (mode1 != mode2) {
2087 *changed = 1;
2088 goto done;
2091 if (got_object_id_cmp(&te1->id, &te2->id) == 0) {
2092 *changed = 0;
2093 goto done;
2097 if (*s == '\0') { /* final path element */
2098 *changed = 1;
2099 goto done;
2102 seg = s + 1;
2103 s++;
2104 seglen = 0;
2105 if (*s) {
2106 err = got_object_open_as_tree(&next_tree1, repo,
2107 &te1->id);
2108 te1 = NULL;
2109 if (err)
2110 goto done;
2111 if (tree1 != tree01)
2112 got_object_tree_close(tree1);
2113 tree1 = next_tree1;
2115 if (te2) {
2116 err = got_object_open_as_tree(&next_tree2, repo,
2117 &te2->id);
2118 te2 = NULL;
2119 if (err)
2120 goto done;
2121 if (tree2 != tree02)
2122 got_object_tree_close(tree2);
2123 tree2 = next_tree2;
2124 } else if (tree2) {
2125 if (tree2 != tree02)
2126 got_object_tree_close(tree2);
2127 tree2 = NULL;
2131 done:
2132 if (tree1 && tree1 != tree01)
2133 got_object_tree_close(tree1);
2134 if (tree2 && tree2 != tree02)
2135 got_object_tree_close(tree2);
2136 return err;
2139 const struct got_error *
2140 got_object_tree_entry_dup(struct got_tree_entry **new_te,
2141 struct got_tree_entry *te)
2143 const struct got_error *err = NULL;
2145 *new_te = calloc(1, sizeof(**new_te));
2146 if (*new_te == NULL)
2147 return got_error_from_errno("calloc");
2149 (*new_te)->mode = te->mode;
2150 memcpy((*new_te)->name, te->name, sizeof((*new_te)->name));
2151 memcpy(&(*new_te)->id, &te->id, sizeof((*new_te)->id));
2152 return err;
2155 int
2156 got_object_tree_entry_is_submodule(struct got_tree_entry *te)
2158 return (te->mode & S_IFMT) == (S_IFDIR | S_IFLNK);
2161 int
2162 got_object_tree_entry_is_symlink(struct got_tree_entry *te)
2164 /* S_IFDIR check avoids confusing symlinks with submodules. */
2165 return ((te->mode & (S_IFDIR | S_IFLNK)) == S_IFLNK);
2168 static const struct got_error *
2169 resolve_symlink(char **link_target, const char *path,
2170 struct got_object_id *commit_id, struct got_repository *repo)
2172 const struct got_error *err = NULL;
2173 char buf[PATH_MAX];
2174 char *name, *parent_path = NULL;
2175 struct got_object_id *tree_obj_id = NULL;
2176 struct got_tree_object *tree = NULL;
2177 struct got_tree_entry *te = NULL;
2179 *link_target = NULL;
2181 if (strlcpy(buf, path, sizeof(buf)) >= sizeof(buf))
2182 return got_error(GOT_ERR_NO_SPACE);
2184 name = basename(buf);
2185 if (name == NULL)
2186 return got_error_from_errno2("basename", path);
2188 err = got_path_dirname(&parent_path, path);
2189 if (err)
2190 return err;
2192 err = got_object_id_by_path(&tree_obj_id, repo, commit_id,
2193 parent_path);
2194 if (err) {
2195 if (err->code == GOT_ERR_NO_TREE_ENTRY) {
2196 /* Display the complete path in error message. */
2197 err = got_error_path(path, err->code);
2199 goto done;
2202 err = got_object_open_as_tree(&tree, repo, tree_obj_id);
2203 if (err)
2204 goto done;
2206 te = got_object_tree_find_entry(tree, name);
2207 if (te == NULL) {
2208 err = got_error_path(path, GOT_ERR_NO_TREE_ENTRY);
2209 goto done;
2212 if (got_object_tree_entry_is_symlink(te)) {
2213 err = got_tree_entry_get_symlink_target(link_target, te, repo);
2214 if (err)
2215 goto done;
2216 if (!got_path_is_absolute(*link_target)) {
2217 char *abspath;
2218 if (asprintf(&abspath, "%s/%s", parent_path,
2219 *link_target) == -1) {
2220 err = got_error_from_errno("asprintf");
2221 goto done;
2223 free(*link_target);
2224 *link_target = malloc(PATH_MAX);
2225 if (*link_target == NULL) {
2226 err = got_error_from_errno("malloc");
2227 goto done;
2229 err = got_canonpath(abspath, *link_target, PATH_MAX);
2230 free(abspath);
2231 if (err)
2232 goto done;
2235 done:
2236 free(tree_obj_id);
2237 if (tree)
2238 got_object_tree_close(tree);
2239 if (err) {
2240 free(*link_target);
2241 *link_target = NULL;
2243 return err;
2246 const struct got_error *
2247 got_object_resolve_symlinks(char **link_target, const char *path,
2248 struct got_object_id *commit_id, struct got_repository *repo)
2250 const struct got_error *err = NULL;
2251 char *next_target = NULL;
2252 int max_recursion = 40; /* matches Git */
2254 *link_target = NULL;
2256 do {
2257 err = resolve_symlink(&next_target,
2258 *link_target ? *link_target : path, commit_id, repo);
2259 if (err)
2260 break;
2261 if (next_target) {
2262 free(*link_target);
2263 if (--max_recursion == 0) {
2264 err = got_error_path(path, GOT_ERR_RECURSION);
2265 *link_target = NULL;
2266 break;
2268 *link_target = next_target;
2270 } while (next_target);
2272 return err;
2275 const struct got_error *
2276 got_traverse_packed_commits(struct got_object_id_queue *traversed_commits,
2277 struct got_object_id *commit_id, const char *path,
2278 struct got_repository *repo)
2280 const struct got_error *err = NULL;
2281 struct got_pack *pack = NULL;
2282 struct got_packidx *packidx = NULL;
2283 char *path_packfile = NULL;
2284 struct got_commit_object *changed_commit = NULL;
2285 struct got_object_id *changed_commit_id = NULL;
2286 int idx;
2288 err = got_repo_search_packidx(&packidx, &idx, repo, commit_id);
2289 if (err) {
2290 if (err->code != GOT_ERR_NO_OBJ)
2291 return err;
2292 return NULL;
2295 err = got_packidx_get_packfile_path(&path_packfile,
2296 packidx->path_packidx);
2297 if (err)
2298 return err;
2300 pack = got_repo_get_cached_pack(repo, path_packfile);
2301 if (pack == NULL) {
2302 err = got_repo_cache_pack(&pack, repo, path_packfile, packidx);
2303 if (err)
2304 goto done;
2307 if (pack->privsep_child == NULL) {
2308 err = start_pack_privsep_child(pack, packidx);
2309 if (err)
2310 goto done;
2313 err = got_privsep_send_commit_traversal_request(
2314 pack->privsep_child->ibuf, commit_id, idx, path);
2315 if (err)
2316 goto done;
2318 err = got_privsep_recv_traversed_commits(&changed_commit,
2319 &changed_commit_id, traversed_commits, pack->privsep_child->ibuf);
2320 if (err)
2321 goto done;
2323 if (changed_commit) {
2325 * Cache the commit in which the path was changed.
2326 * This commit might be opened again soon.
2328 changed_commit->refcnt++;
2329 err = got_repo_cache_commit(repo, changed_commit_id,
2330 changed_commit);
2331 got_object_commit_close(changed_commit);
2333 done:
2334 free(path_packfile);
2335 free(changed_commit_id);
2336 return err;