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 /* *outfd must be initialized to -1 by caller */
532 const struct got_error *
533 got_object_raw_open(struct got_raw_object **obj, int *outfd,
534 struct got_repository *repo, struct got_object_id *id)
536 const struct got_error *err = NULL;
537 struct got_packidx *packidx = NULL;
538 int idx;
539 uint8_t *outbuf = NULL;
540 off_t size = 0;
541 size_t hdrlen = 0;
542 char *path_packfile = NULL;
544 *obj = got_repo_get_cached_raw_object(repo, id);
545 if (*obj != NULL) {
546 (*obj)->refcnt++;
547 return NULL;
550 if (*outfd == -1) {
551 *outfd = got_opentempfd();
552 if (*outfd == -1)
553 return got_error_from_errno("got_opentempfd");
556 err = got_repo_search_packidx(&packidx, &idx, repo, id);
557 if (err == NULL) {
558 struct got_pack *pack = NULL;
560 err = got_packidx_get_packfile_path(&path_packfile,
561 packidx->path_packidx);
562 if (err)
563 goto done;
565 pack = got_repo_get_cached_pack(repo, path_packfile);
566 if (pack == NULL) {
567 err = got_repo_cache_pack(&pack, repo, path_packfile,
568 packidx);
569 if (err)
570 goto done;
572 err = read_packed_object_raw_privsep(&outbuf, &size, &hdrlen,
573 *outfd, pack, packidx, idx, id);
574 if (err)
575 goto done;
576 } else if (err->code == GOT_ERR_NO_OBJ) {
577 int fd;
579 err = got_object_open_loose_fd(&fd, id, repo);
580 if (err)
581 goto done;
582 err = read_object_raw_privsep(&outbuf, &size, &hdrlen, *outfd,
583 id, repo, fd);
584 if (err)
585 goto done;
588 *obj = calloc(1, sizeof(**obj));
589 if (*obj == NULL) {
590 err = got_error_from_errno("calloc");
591 goto done;
594 if (outbuf) {
595 (*obj)->f = fmemopen(outbuf, hdrlen + size, "r");
596 if ((*obj)->f == NULL) {
597 err = got_error_from_errno("fdopen");
598 goto done;
600 (*obj)->data = outbuf;
601 } else {
602 struct stat sb;
603 if (fstat(*outfd, &sb) == -1) {
604 err = got_error_from_errno("fstat");
605 goto done;
608 if (sb.st_size != hdrlen + size) {
609 err = got_error(GOT_ERR_PRIVSEP_LEN);
610 goto done;
613 (*obj)->f = fdopen(*outfd, "r");
614 if ((*obj)->f == NULL) {
615 err = got_error_from_errno("fdopen");
616 goto done;
618 (*obj)->data = NULL;
619 *outfd = -1;
621 (*obj)->hdrlen = hdrlen;
622 (*obj)->size = size;
623 err = got_repo_cache_raw_object(repo, id, *obj);
624 done:
625 free(path_packfile);
626 if (err) {
627 if (*obj) {
628 got_object_raw_close(*obj);
629 *obj = NULL;
631 free(outbuf);
632 } else
633 (*obj)->refcnt++;
634 return err;
637 const struct got_error *
638 got_object_open_by_id_str(struct got_object **obj, struct got_repository *repo,
639 const char *id_str)
641 struct got_object_id id;
643 if (!got_parse_sha1_digest(id.sha1, id_str))
644 return got_error_path(id_str, GOT_ERR_BAD_OBJ_ID_STR);
646 return got_object_open(obj, repo, &id);
649 const struct got_error *
650 got_object_resolve_id_str(struct got_object_id **id,
651 struct got_repository *repo, const char *id_str)
653 const struct got_error *err = NULL;
654 struct got_object *obj;
656 err = got_object_open_by_id_str(&obj, repo, id_str);
657 if (err)
658 return err;
660 *id = got_object_id_dup(got_object_get_id(obj));
661 got_object_close(obj);
662 if (*id == NULL)
663 return got_error_from_errno("got_object_id_dup");
665 return NULL;
668 static const struct got_error *
669 request_packed_commit(struct got_commit_object **commit, struct got_pack *pack,
670 int pack_idx, struct got_object_id *id)
672 const struct got_error *err = NULL;
674 err = got_privsep_send_commit_req(pack->privsep_child->ibuf, -1, id,
675 pack_idx);
676 if (err)
677 return err;
679 err = got_privsep_recv_commit(commit, pack->privsep_child->ibuf);
680 if (err)
681 return err;
683 (*commit)->flags |= GOT_COMMIT_FLAG_PACKED;
684 return NULL;
687 static const struct got_error *
688 read_packed_commit_privsep(struct got_commit_object **commit,
689 struct got_pack *pack, struct got_packidx *packidx, int idx,
690 struct got_object_id *id)
692 const struct got_error *err = NULL;
694 if (pack->privsep_child)
695 return request_packed_commit(commit, pack, idx, id);
697 err = start_pack_privsep_child(pack, packidx);
698 if (err)
699 return err;
701 return request_packed_commit(commit, pack, idx, id);
704 static const struct got_error *
705 request_commit(struct got_commit_object **commit, struct got_repository *repo,
706 int fd, struct got_object_id *id)
708 const struct got_error *err = NULL;
709 struct imsgbuf *ibuf;
711 ibuf = repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_COMMIT].ibuf;
713 err = got_privsep_send_commit_req(ibuf, fd, id, -1);
714 if (err)
715 return err;
717 return got_privsep_recv_commit(commit, ibuf);
720 static const struct got_error *
721 read_commit_privsep(struct got_commit_object **commit, int obj_fd,
722 struct got_object_id *id, struct got_repository *repo)
724 const struct got_error *err;
725 int imsg_fds[2];
726 pid_t pid;
727 struct imsgbuf *ibuf;
729 if (repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_COMMIT].imsg_fd != -1)
730 return request_commit(commit, repo, obj_fd, id);
732 ibuf = calloc(1, sizeof(*ibuf));
733 if (ibuf == NULL)
734 return got_error_from_errno("calloc");
736 if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_fds) == -1) {
737 err = got_error_from_errno("socketpair");
738 free(ibuf);
739 return err;
742 pid = fork();
743 if (pid == -1) {
744 err = got_error_from_errno("fork");
745 free(ibuf);
746 return err;
748 else if (pid == 0) {
749 got_privsep_exec_child(imsg_fds, GOT_PATH_PROG_READ_COMMIT,
750 repo->path);
751 /* not reached */
754 if (close(imsg_fds[1]) == -1) {
755 err = got_error_from_errno("close");
756 free(ibuf);
757 return err;
759 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_COMMIT].imsg_fd =
760 imsg_fds[0];
761 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_COMMIT].pid = pid;
762 imsg_init(ibuf, imsg_fds[0]);
763 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_COMMIT].ibuf = ibuf;
765 return request_commit(commit, repo, obj_fd, id);
769 static const struct got_error *
770 open_commit(struct got_commit_object **commit,
771 struct got_repository *repo, struct got_object_id *id, int check_cache)
773 const struct got_error *err = NULL;
774 struct got_packidx *packidx = NULL;
775 int idx;
776 char *path_packfile = NULL;
778 if (check_cache) {
779 *commit = got_repo_get_cached_commit(repo, id);
780 if (*commit != NULL) {
781 (*commit)->refcnt++;
782 return NULL;
784 } else
785 *commit = NULL;
787 err = got_repo_search_packidx(&packidx, &idx, repo, id);
788 if (err == NULL) {
789 struct got_pack *pack = NULL;
791 err = got_packidx_get_packfile_path(&path_packfile,
792 packidx->path_packidx);
793 if (err)
794 return err;
796 pack = got_repo_get_cached_pack(repo, path_packfile);
797 if (pack == NULL) {
798 err = got_repo_cache_pack(&pack, repo, path_packfile,
799 packidx);
800 if (err)
801 goto done;
803 err = read_packed_commit_privsep(commit, pack,
804 packidx, idx, id);
805 } else if (err->code == GOT_ERR_NO_OBJ) {
806 int fd;
808 err = got_object_open_loose_fd(&fd, id, repo);
809 if (err)
810 return err;
811 err = read_commit_privsep(commit, fd, id, repo);
814 if (err == NULL) {
815 (*commit)->refcnt++;
816 err = got_repo_cache_commit(repo, id, *commit);
818 done:
819 free(path_packfile);
820 return err;
823 const struct got_error *
824 got_object_open_as_commit(struct got_commit_object **commit,
825 struct got_repository *repo, struct got_object_id *id)
827 *commit = got_repo_get_cached_commit(repo, id);
828 if (*commit != NULL) {
829 (*commit)->refcnt++;
830 return NULL;
833 return open_commit(commit, repo, id, 0);
836 const struct got_error *
837 got_object_commit_open(struct got_commit_object **commit,
838 struct got_repository *repo, struct got_object *obj)
840 return open_commit(commit, repo, got_object_get_id(obj), 1);
843 const struct got_error *
844 got_object_qid_alloc(struct got_object_qid **qid, struct got_object_id *id)
846 const struct got_error *err = NULL;
848 *qid = calloc(1, sizeof(**qid));
849 if (*qid == NULL)
850 return got_error_from_errno("calloc");
852 (*qid)->id = got_object_id_dup(id);
853 if ((*qid)->id == NULL) {
854 err = got_error_from_errno("got_object_id_dup");
855 got_object_qid_free(*qid);
856 *qid = NULL;
857 return err;
860 return NULL;
863 const struct got_error *
864 got_object_id_queue_copy(const struct got_object_id_queue *src,
865 struct got_object_id_queue *dest)
867 const struct got_error *err;
868 struct got_object_qid *qid;
870 STAILQ_FOREACH(qid, src, entry) {
871 struct got_object_qid *new;
872 /*
873 * Deep-copy the object ID only. Let the caller deal
874 * with setting up the new->data pointer if needed.
875 */
876 err = got_object_qid_alloc(&new, qid->id);
877 if (err) {
878 got_object_id_queue_free(dest);
879 return err;
881 STAILQ_INSERT_TAIL(dest, new, entry);
884 return NULL;
887 static const struct got_error *
888 request_packed_tree(struct got_tree_object **tree, struct got_pack *pack,
889 int pack_idx, struct got_object_id *id)
891 const struct got_error *err = NULL;
893 err = got_privsep_send_tree_req(pack->privsep_child->ibuf, -1, id,
894 pack_idx);
895 if (err)
896 return err;
898 return got_privsep_recv_tree(tree, pack->privsep_child->ibuf);
901 static const struct got_error *
902 read_packed_tree_privsep(struct got_tree_object **tree,
903 struct got_pack *pack, struct got_packidx *packidx, int idx,
904 struct got_object_id *id)
906 const struct got_error *err = NULL;
908 if (pack->privsep_child)
909 return request_packed_tree(tree, pack, idx, id);
911 err = start_pack_privsep_child(pack, packidx);
912 if (err)
913 return err;
915 return request_packed_tree(tree, pack, idx, id);
918 static const struct got_error *
919 request_tree(struct got_tree_object **tree, struct got_repository *repo,
920 int fd, struct got_object_id *id)
922 const struct got_error *err = NULL;
923 struct imsgbuf *ibuf;
925 ibuf = repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TREE].ibuf;
927 err = got_privsep_send_tree_req(ibuf, fd, id, -1);
928 if (err)
929 return err;
931 return got_privsep_recv_tree(tree, ibuf);
934 const struct got_error *
935 read_tree_privsep(struct got_tree_object **tree, int obj_fd,
936 struct got_object_id *id, struct got_repository *repo)
938 const struct got_error *err;
939 int imsg_fds[2];
940 pid_t pid;
941 struct imsgbuf *ibuf;
943 if (repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TREE].imsg_fd != -1)
944 return request_tree(tree, repo, obj_fd, id);
946 ibuf = calloc(1, sizeof(*ibuf));
947 if (ibuf == NULL)
948 return got_error_from_errno("calloc");
950 if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_fds) == -1) {
951 err = got_error_from_errno("socketpair");
952 free(ibuf);
953 return err;
956 pid = fork();
957 if (pid == -1) {
958 err = got_error_from_errno("fork");
959 free(ibuf);
960 return err;
962 else if (pid == 0) {
963 got_privsep_exec_child(imsg_fds, GOT_PATH_PROG_READ_TREE,
964 repo->path);
965 /* not reached */
968 if (close(imsg_fds[1]) == -1) {
969 err = got_error_from_errno("close");
970 free(ibuf);
971 return err;
973 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TREE].imsg_fd =
974 imsg_fds[0];
975 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TREE].pid = pid;
976 imsg_init(ibuf, imsg_fds[0]);
977 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TREE].ibuf = ibuf;
980 return request_tree(tree, repo, obj_fd, id);
983 static const struct got_error *
984 open_tree(struct got_tree_object **tree, struct got_repository *repo,
985 struct got_object_id *id, int check_cache)
987 const struct got_error *err = NULL;
988 struct got_packidx *packidx = NULL;
989 int idx;
990 char *path_packfile = NULL;
992 if (check_cache) {
993 *tree = got_repo_get_cached_tree(repo, id);
994 if (*tree != NULL) {
995 (*tree)->refcnt++;
996 return NULL;
998 } else
999 *tree = NULL;
1001 err = got_repo_search_packidx(&packidx, &idx, repo, id);
1002 if (err == NULL) {
1003 struct got_pack *pack = NULL;
1005 err = got_packidx_get_packfile_path(&path_packfile,
1006 packidx->path_packidx);
1007 if (err)
1008 return err;
1010 pack = got_repo_get_cached_pack(repo, path_packfile);
1011 if (pack == NULL) {
1012 err = got_repo_cache_pack(&pack, repo, path_packfile,
1013 packidx);
1014 if (err)
1015 goto done;
1017 err = read_packed_tree_privsep(tree, pack,
1018 packidx, idx, id);
1019 } else if (err->code == GOT_ERR_NO_OBJ) {
1020 int fd;
1022 err = got_object_open_loose_fd(&fd, id, repo);
1023 if (err)
1024 return err;
1025 err = read_tree_privsep(tree, fd, id, repo);
1028 if (err == NULL) {
1029 (*tree)->refcnt++;
1030 err = got_repo_cache_tree(repo, id, *tree);
1032 done:
1033 free(path_packfile);
1034 return err;
1037 const struct got_error *
1038 got_object_open_as_tree(struct got_tree_object **tree,
1039 struct got_repository *repo, struct got_object_id *id)
1041 *tree = got_repo_get_cached_tree(repo, id);
1042 if (*tree != NULL) {
1043 (*tree)->refcnt++;
1044 return NULL;
1047 return open_tree(tree, repo, id, 0);
1050 const struct got_error *
1051 got_object_tree_open(struct got_tree_object **tree,
1052 struct got_repository *repo, struct got_object *obj)
1054 return open_tree(tree, repo, got_object_get_id(obj), 1);
1057 int
1058 got_object_tree_get_nentries(struct got_tree_object *tree)
1060 return tree->nentries;
1063 struct got_tree_entry *
1064 got_object_tree_get_first_entry(struct got_tree_object *tree)
1066 return got_object_tree_get_entry(tree, 0);
1069 struct got_tree_entry *
1070 got_object_tree_get_last_entry(struct got_tree_object *tree)
1072 return got_object_tree_get_entry(tree, tree->nentries - 1);
1075 struct got_tree_entry *
1076 got_object_tree_get_entry(struct got_tree_object *tree, int i)
1078 if (i < 0 || i >= tree->nentries)
1079 return NULL;
1080 return &tree->entries[i];
1083 mode_t
1084 got_tree_entry_get_mode(struct got_tree_entry *te)
1086 return te->mode;
1089 const char *
1090 got_tree_entry_get_name(struct got_tree_entry *te)
1092 return &te->name[0];
1095 struct got_object_id *
1096 got_tree_entry_get_id(struct got_tree_entry *te)
1098 return &te->id;
1101 const struct got_error *
1102 got_object_blob_read_to_str(char **s, struct got_blob_object *blob)
1104 const struct got_error *err = NULL;
1105 size_t len, totlen, hdrlen, offset;
1107 *s = NULL;
1109 hdrlen = got_object_blob_get_hdrlen(blob);
1110 totlen = 0;
1111 offset = 0;
1112 do {
1113 char *p;
1115 err = got_object_blob_read_block(&len, blob);
1116 if (err)
1117 return err;
1119 if (len == 0)
1120 break;
1122 totlen += len - hdrlen;
1123 p = realloc(*s, totlen + 1);
1124 if (p == NULL) {
1125 err = got_error_from_errno("realloc");
1126 free(*s);
1127 *s = NULL;
1128 return err;
1130 *s = p;
1131 /* Skip blob object header first time around. */
1132 memcpy(*s + offset,
1133 got_object_blob_get_read_buf(blob) + hdrlen, len - hdrlen);
1134 hdrlen = 0;
1135 offset = totlen;
1136 } while (len > 0);
1138 (*s)[totlen] = '\0';
1139 return NULL;
1142 const struct got_error *
1143 got_tree_entry_get_symlink_target(char **link_target, struct got_tree_entry *te,
1144 struct got_repository *repo)
1146 const struct got_error *err = NULL;
1147 struct got_blob_object *blob = NULL;
1149 *link_target = NULL;
1151 if (!got_object_tree_entry_is_symlink(te))
1152 return got_error(GOT_ERR_TREE_ENTRY_TYPE);
1154 err = got_object_open_as_blob(&blob, repo,
1155 got_tree_entry_get_id(te), PATH_MAX);
1156 if (err)
1157 return err;
1159 err = got_object_blob_read_to_str(link_target, blob);
1160 got_object_blob_close(blob);
1161 if (err) {
1162 free(*link_target);
1163 *link_target = NULL;
1165 return err;
1168 int
1169 got_tree_entry_get_index(struct got_tree_entry *te)
1171 return te->idx;
1174 struct got_tree_entry *
1175 got_tree_entry_get_next(struct got_tree_object *tree,
1176 struct got_tree_entry *te)
1178 return got_object_tree_get_entry(tree, te->idx + 1);
1181 struct got_tree_entry *
1182 got_tree_entry_get_prev(struct got_tree_object *tree,
1183 struct got_tree_entry *te)
1185 return got_object_tree_get_entry(tree, te->idx - 1);
1188 static const struct got_error *
1189 request_packed_blob(uint8_t **outbuf, size_t *size, size_t *hdrlen, int outfd,
1190 struct got_pack *pack, struct got_packidx *packidx, int idx,
1191 struct got_object_id *id)
1193 const struct got_error *err = NULL;
1194 int outfd_child;
1195 int basefd, accumfd; /* temporary files for delta application */
1197 basefd = got_opentempfd();
1198 if (basefd == -1)
1199 return got_error_from_errno("got_opentempfd");
1200 accumfd = got_opentempfd();
1201 if (accumfd == -1)
1202 return got_error_from_errno("got_opentempfd");
1204 outfd_child = dup(outfd);
1205 if (outfd_child == -1)
1206 return got_error_from_errno("dup");
1208 err = got_privsep_send_blob_req(pack->privsep_child->ibuf, -1, id, idx);
1209 if (err)
1210 return err;
1212 err = got_privsep_send_blob_outfd(pack->privsep_child->ibuf,
1213 outfd_child);
1214 if (err) {
1215 close(basefd);
1216 close(accumfd);
1217 return err;
1220 err = got_privsep_send_tmpfd(pack->privsep_child->ibuf,
1221 basefd);
1222 if (err) {
1223 close(accumfd);
1224 return err;
1227 err = got_privsep_send_tmpfd(pack->privsep_child->ibuf,
1228 accumfd);
1229 if (err)
1230 return err;
1232 err = got_privsep_recv_blob(outbuf, size, hdrlen,
1233 pack->privsep_child->ibuf);
1234 if (err)
1235 return err;
1237 if (lseek(outfd, SEEK_SET, 0) == -1)
1238 err = got_error_from_errno("lseek");
1240 return err;
1243 static const struct got_error *
1244 read_packed_blob_privsep(uint8_t **outbuf, size_t *size, size_t *hdrlen,
1245 int outfd, struct got_pack *pack, struct got_packidx *packidx, int idx,
1246 struct got_object_id *id)
1248 const struct got_error *err = NULL;
1250 if (pack->privsep_child == NULL) {
1251 err = start_pack_privsep_child(pack, packidx);
1252 if (err)
1253 return err;
1256 return request_packed_blob(outbuf, size, hdrlen, outfd, pack, packidx,
1257 idx, id);
1260 static const struct got_error *
1261 request_blob(uint8_t **outbuf, size_t *size, size_t *hdrlen, int outfd,
1262 int infd, struct got_object_id *id, struct imsgbuf *ibuf)
1264 const struct got_error *err = NULL;
1265 int outfd_child;
1267 outfd_child = dup(outfd);
1268 if (outfd_child == -1)
1269 return got_error_from_errno("dup");
1271 err = got_privsep_send_blob_req(ibuf, infd, id, -1);
1272 if (err)
1273 return err;
1275 err = got_privsep_send_blob_outfd(ibuf, outfd_child);
1276 if (err)
1277 return err;
1279 err = got_privsep_recv_blob(outbuf, size, hdrlen, ibuf);
1280 if (err)
1281 return err;
1283 if (lseek(outfd, SEEK_SET, 0) == -1)
1284 return got_error_from_errno("lseek");
1286 return err;
1289 static const struct got_error *
1290 read_blob_privsep(uint8_t **outbuf, size_t *size, size_t *hdrlen,
1291 int outfd, int infd, struct got_object_id *id, struct got_repository *repo)
1293 const struct got_error *err;
1294 int imsg_fds[2];
1295 pid_t pid;
1296 struct imsgbuf *ibuf;
1298 if (repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_BLOB].imsg_fd != -1) {
1299 ibuf = repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_BLOB].ibuf;
1300 return request_blob(outbuf, size, hdrlen, outfd, infd, id,
1301 ibuf);
1304 ibuf = calloc(1, sizeof(*ibuf));
1305 if (ibuf == NULL)
1306 return got_error_from_errno("calloc");
1308 if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_fds) == -1) {
1309 err = got_error_from_errno("socketpair");
1310 free(ibuf);
1311 return err;
1314 pid = fork();
1315 if (pid == -1) {
1316 err = got_error_from_errno("fork");
1317 free(ibuf);
1318 return err;
1320 else if (pid == 0) {
1321 got_privsep_exec_child(imsg_fds, GOT_PATH_PROG_READ_BLOB,
1322 repo->path);
1323 /* not reached */
1326 if (close(imsg_fds[1]) == -1) {
1327 err = got_error_from_errno("close");
1328 free(ibuf);
1329 return err;
1331 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_BLOB].imsg_fd =
1332 imsg_fds[0];
1333 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_BLOB].pid = pid;
1334 imsg_init(ibuf, imsg_fds[0]);
1335 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_BLOB].ibuf = ibuf;
1337 return request_blob(outbuf, size, hdrlen, outfd, infd, id, ibuf);
1340 static const struct got_error *
1341 open_blob(struct got_blob_object **blob, struct got_repository *repo,
1342 struct got_object_id *id, size_t blocksize)
1344 const struct got_error *err = NULL;
1345 struct got_packidx *packidx = NULL;
1346 int idx;
1347 char *path_packfile = NULL;
1348 uint8_t *outbuf;
1349 int outfd;
1350 size_t size, hdrlen;
1351 struct stat sb;
1353 *blob = calloc(1, sizeof(**blob));
1354 if (*blob == NULL)
1355 return got_error_from_errno("calloc");
1357 outfd = got_opentempfd();
1358 if (outfd == -1)
1359 return got_error_from_errno("got_opentempfd");
1361 (*blob)->read_buf = malloc(blocksize);
1362 if ((*blob)->read_buf == NULL) {
1363 err = got_error_from_errno("malloc");
1364 goto done;
1367 err = got_repo_search_packidx(&packidx, &idx, repo, id);
1368 if (err == NULL) {
1369 struct got_pack *pack = NULL;
1371 err = got_packidx_get_packfile_path(&path_packfile,
1372 packidx->path_packidx);
1373 if (err)
1374 goto done;
1376 pack = got_repo_get_cached_pack(repo, path_packfile);
1377 if (pack == NULL) {
1378 err = got_repo_cache_pack(&pack, repo, path_packfile,
1379 packidx);
1380 if (err)
1381 goto done;
1383 err = read_packed_blob_privsep(&outbuf, &size, &hdrlen, outfd,
1384 pack, packidx, idx, id);
1385 } else if (err->code == GOT_ERR_NO_OBJ) {
1386 int infd;
1388 err = got_object_open_loose_fd(&infd, id, repo);
1389 if (err)
1390 goto done;
1391 err = read_blob_privsep(&outbuf, &size, &hdrlen, outfd, infd,
1392 id, repo);
1394 if (err)
1395 goto done;
1397 if (hdrlen > size) {
1398 err = got_error(GOT_ERR_BAD_OBJ_HDR);
1399 goto done;
1402 if (outbuf) {
1403 if (close(outfd) == -1 && err == NULL)
1404 err = got_error_from_errno("close");
1405 outfd = -1;
1406 (*blob)->f = fmemopen(outbuf, size, "rb");
1407 if ((*blob)->f == NULL) {
1408 err = got_error_from_errno("fmemopen");
1409 free(outbuf);
1410 goto done;
1412 (*blob)->data = outbuf;
1413 } else {
1414 if (fstat(outfd, &sb) == -1) {
1415 err = got_error_from_errno("fstat");
1416 goto done;
1419 if (sb.st_size != size) {
1420 err = got_error(GOT_ERR_PRIVSEP_LEN);
1421 goto done;
1424 (*blob)->f = fdopen(outfd, "rb");
1425 if ((*blob)->f == NULL) {
1426 err = got_error_from_errno("fdopen");
1427 close(outfd);
1428 outfd = -1;
1429 goto done;
1433 (*blob)->hdrlen = hdrlen;
1434 (*blob)->blocksize = blocksize;
1435 memcpy(&(*blob)->id.sha1, id->sha1, SHA1_DIGEST_LENGTH);
1437 done:
1438 free(path_packfile);
1439 if (err) {
1440 if (*blob) {
1441 got_object_blob_close(*blob);
1442 *blob = NULL;
1443 } else if (outfd != -1)
1444 close(outfd);
1446 return err;
1449 const struct got_error *
1450 got_object_open_as_blob(struct got_blob_object **blob,
1451 struct got_repository *repo, struct got_object_id *id,
1452 size_t blocksize)
1454 return open_blob(blob, repo, id, blocksize);
1457 const struct got_error *
1458 got_object_blob_open(struct got_blob_object **blob,
1459 struct got_repository *repo, struct got_object *obj, size_t blocksize)
1461 return open_blob(blob, repo, got_object_get_id(obj), blocksize);
1464 const struct got_error *
1465 got_object_blob_close(struct got_blob_object *blob)
1467 const struct got_error *err = NULL;
1468 free(blob->read_buf);
1469 if (blob->f && fclose(blob->f) == EOF)
1470 err = got_error_from_errno("fclose");
1471 free(blob->data);
1472 free(blob);
1473 return err;
1476 void
1477 got_object_blob_rewind(struct got_blob_object *blob)
1479 if (blob->f)
1480 rewind(blob->f);
1483 char *
1484 got_object_blob_id_str(struct got_blob_object *blob, char *buf, size_t size)
1486 return got_sha1_digest_to_str(blob->id.sha1, buf, size);
1489 size_t
1490 got_object_blob_get_hdrlen(struct got_blob_object *blob)
1492 return blob->hdrlen;
1495 const uint8_t *
1496 got_object_blob_get_read_buf(struct got_blob_object *blob)
1498 return blob->read_buf;
1501 const struct got_error *
1502 got_object_blob_read_block(size_t *outlenp, struct got_blob_object *blob)
1504 size_t n;
1506 n = fread(blob->read_buf, 1, blob->blocksize, blob->f);
1507 if (n == 0 && ferror(blob->f))
1508 return got_ferror(blob->f, GOT_ERR_IO);
1509 *outlenp = n;
1510 return NULL;
1513 const struct got_error *
1514 got_object_blob_dump_to_file(off_t *filesize, int *nlines,
1515 off_t **line_offsets, FILE *outfile, struct got_blob_object *blob)
1517 const struct got_error *err = NULL;
1518 size_t n, len, hdrlen;
1519 const uint8_t *buf;
1520 int i;
1521 const int alloc_chunksz = 512;
1522 size_t nalloc = 0;
1523 off_t off = 0, total_len = 0;
1525 if (line_offsets)
1526 *line_offsets = NULL;
1527 if (filesize)
1528 *filesize = 0;
1529 if (nlines)
1530 *nlines = 0;
1532 hdrlen = got_object_blob_get_hdrlen(blob);
1533 do {
1534 err = got_object_blob_read_block(&len, blob);
1535 if (err)
1536 return err;
1537 if (len == 0)
1538 break;
1539 buf = got_object_blob_get_read_buf(blob);
1540 i = hdrlen;
1541 if (nlines) {
1542 if (line_offsets && *line_offsets == NULL) {
1543 /* Have some data but perhaps no '\n'. */
1544 *nlines = 1;
1545 nalloc = alloc_chunksz;
1546 *line_offsets = calloc(nalloc,
1547 sizeof(**line_offsets));
1548 if (*line_offsets == NULL)
1549 return got_error_from_errno("calloc");
1551 /* Skip forward over end of first line. */
1552 while (i < len) {
1553 if (buf[i] == '\n')
1554 break;
1555 i++;
1558 /* Scan '\n' offsets in remaining chunk of data. */
1559 while (i < len) {
1560 if (buf[i] != '\n') {
1561 i++;
1562 continue;
1564 (*nlines)++;
1565 if (line_offsets && nalloc < *nlines) {
1566 size_t n = *nlines + alloc_chunksz;
1567 off_t *o = recallocarray(*line_offsets,
1568 nalloc, n, sizeof(**line_offsets));
1569 if (o == NULL) {
1570 free(*line_offsets);
1571 *line_offsets = NULL;
1572 return got_error_from_errno(
1573 "recallocarray");
1575 *line_offsets = o;
1576 nalloc = n;
1578 if (line_offsets) {
1579 off = total_len + i - hdrlen + 1;
1580 (*line_offsets)[*nlines - 1] = off;
1582 i++;
1585 /* Skip blob object header first time around. */
1586 n = fwrite(buf + hdrlen, 1, len - hdrlen, outfile);
1587 if (n != len - hdrlen)
1588 return got_ferror(outfile, GOT_ERR_IO);
1589 total_len += len - hdrlen;
1590 hdrlen = 0;
1591 } while (len != 0);
1593 if (fflush(outfile) != 0)
1594 return got_error_from_errno("fflush");
1595 rewind(outfile);
1597 if (filesize)
1598 *filesize = total_len;
1600 return NULL;
1603 static const struct got_error *
1604 request_packed_tag(struct got_tag_object **tag, struct got_pack *pack,
1605 int pack_idx, struct got_object_id *id)
1607 const struct got_error *err = NULL;
1609 err = got_privsep_send_tag_req(pack->privsep_child->ibuf, -1, id,
1610 pack_idx);
1611 if (err)
1612 return err;
1614 return got_privsep_recv_tag(tag, pack->privsep_child->ibuf);
1617 static const struct got_error *
1618 read_packed_tag_privsep(struct got_tag_object **tag,
1619 struct got_pack *pack, struct got_packidx *packidx, int idx,
1620 struct got_object_id *id)
1622 const struct got_error *err = NULL;
1624 if (pack->privsep_child)
1625 return request_packed_tag(tag, pack, idx, id);
1627 err = start_pack_privsep_child(pack, packidx);
1628 if (err)
1629 return err;
1631 return request_packed_tag(tag, pack, idx, id);
1634 static const struct got_error *
1635 request_tag(struct got_tag_object **tag, struct got_repository *repo,
1636 int fd, struct got_object_id *id)
1638 const struct got_error *err = NULL;
1639 struct imsgbuf *ibuf;
1641 ibuf = repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TAG].ibuf;
1643 err = got_privsep_send_tag_req(ibuf, fd, id, -1);
1644 if (err)
1645 return err;
1647 return got_privsep_recv_tag(tag, ibuf);
1650 static const struct got_error *
1651 read_tag_privsep(struct got_tag_object **tag, int obj_fd,
1652 struct got_object_id *id, struct got_repository *repo)
1654 const struct got_error *err;
1655 int imsg_fds[2];
1656 pid_t pid;
1657 struct imsgbuf *ibuf;
1659 if (repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TAG].imsg_fd != -1)
1660 return request_tag(tag, repo, obj_fd, id);
1662 ibuf = calloc(1, sizeof(*ibuf));
1663 if (ibuf == NULL)
1664 return got_error_from_errno("calloc");
1666 if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_fds) == -1) {
1667 err = got_error_from_errno("socketpair");
1668 free(ibuf);
1669 return err;
1672 pid = fork();
1673 if (pid == -1) {
1674 err = got_error_from_errno("fork");
1675 free(ibuf);
1676 return err;
1678 else if (pid == 0) {
1679 got_privsep_exec_child(imsg_fds, GOT_PATH_PROG_READ_TAG,
1680 repo->path);
1681 /* not reached */
1684 if (close(imsg_fds[1]) == -1) {
1685 err = got_error_from_errno("close");
1686 free(ibuf);
1687 return err;
1689 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TAG].imsg_fd =
1690 imsg_fds[0];
1691 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TAG].pid = pid;
1692 imsg_init(ibuf, imsg_fds[0]);
1693 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TAG].ibuf = ibuf;
1695 return request_tag(tag, repo, obj_fd, id);
1698 static const struct got_error *
1699 open_tag(struct got_tag_object **tag, struct got_repository *repo,
1700 struct got_object_id *id, int check_cache)
1702 const struct got_error *err = NULL;
1703 struct got_packidx *packidx = NULL;
1704 int idx;
1705 char *path_packfile = NULL;
1706 struct got_object *obj = NULL;
1707 int obj_type = GOT_OBJ_TYPE_ANY;
1709 if (check_cache) {
1710 *tag = got_repo_get_cached_tag(repo, id);
1711 if (*tag != NULL) {
1712 (*tag)->refcnt++;
1713 return NULL;
1715 } else
1716 *tag = NULL;
1718 err = got_repo_search_packidx(&packidx, &idx, repo, id);
1719 if (err == NULL) {
1720 struct got_pack *pack = NULL;
1722 err = got_packidx_get_packfile_path(&path_packfile,
1723 packidx->path_packidx);
1724 if (err)
1725 return err;
1727 pack = got_repo_get_cached_pack(repo, path_packfile);
1728 if (pack == NULL) {
1729 err = got_repo_cache_pack(&pack, repo, path_packfile,
1730 packidx);
1731 if (err)
1732 goto done;
1735 /* Beware of "lightweight" tags: Check object type first. */
1736 err = read_packed_object_privsep(&obj, repo, pack, packidx,
1737 idx, id);
1738 if (err)
1739 goto done;
1740 obj_type = obj->type;
1741 got_object_close(obj);
1742 if (obj_type != GOT_OBJ_TYPE_TAG) {
1743 err = got_error(GOT_ERR_OBJ_TYPE);
1744 goto done;
1746 err = read_packed_tag_privsep(tag, pack, packidx, idx, id);
1747 } else if (err->code == GOT_ERR_NO_OBJ) {
1748 int fd;
1750 err = got_object_open_loose_fd(&fd, id, repo);
1751 if (err)
1752 return err;
1753 err = got_object_read_header_privsep(&obj, id, repo, fd);
1754 if (err)
1755 return err;
1756 obj_type = obj->type;
1757 got_object_close(obj);
1758 if (obj_type != GOT_OBJ_TYPE_TAG)
1759 return got_error(GOT_ERR_OBJ_TYPE);
1761 err = got_object_open_loose_fd(&fd, id, repo);
1762 if (err)
1763 return err;
1764 err = read_tag_privsep(tag, fd, id, repo);
1767 if (err == NULL) {
1768 (*tag)->refcnt++;
1769 err = got_repo_cache_tag(repo, id, *tag);
1771 done:
1772 free(path_packfile);
1773 return err;
1776 const struct got_error *
1777 got_object_open_as_tag(struct got_tag_object **tag,
1778 struct got_repository *repo, struct got_object_id *id)
1780 *tag = got_repo_get_cached_tag(repo, id);
1781 if (*tag != NULL) {
1782 (*tag)->refcnt++;
1783 return NULL;
1786 return open_tag(tag, repo, id, 0);
1789 const struct got_error *
1790 got_object_tag_open(struct got_tag_object **tag,
1791 struct got_repository *repo, struct got_object *obj)
1793 return open_tag(tag, repo, got_object_get_id(obj), 1);
1796 const char *
1797 got_object_tag_get_name(struct got_tag_object *tag)
1799 return tag->tag;
1802 int
1803 got_object_tag_get_object_type(struct got_tag_object *tag)
1805 return tag->obj_type;
1808 struct got_object_id *
1809 got_object_tag_get_object_id(struct got_tag_object *tag)
1811 return &tag->id;
1814 time_t
1815 got_object_tag_get_tagger_time(struct got_tag_object *tag)
1817 return tag->tagger_time;
1820 time_t
1821 got_object_tag_get_tagger_gmtoff(struct got_tag_object *tag)
1823 return tag->tagger_gmtoff;
1826 const char *
1827 got_object_tag_get_tagger(struct got_tag_object *tag)
1829 return tag->tagger;
1832 const char *
1833 got_object_tag_get_message(struct got_tag_object *tag)
1835 return tag->tagmsg;
1838 static struct got_tree_entry *
1839 find_entry_by_name(struct got_tree_object *tree, const char *name, size_t len)
1841 int i;
1843 /* Note that tree entries are sorted in strncmp() order. */
1844 for (i = 0; i < tree->nentries; i++) {
1845 struct got_tree_entry *te = &tree->entries[i];
1846 int cmp = strncmp(te->name, name, len);
1847 if (cmp < 0)
1848 continue;
1849 if (cmp > 0)
1850 break;
1851 if (te->name[len] == '\0')
1852 return te;
1854 return NULL;
1857 struct got_tree_entry *
1858 got_object_tree_find_entry(struct got_tree_object *tree, const char *name)
1860 return find_entry_by_name(tree, name, strlen(name));
1863 const struct got_error *
1864 got_object_tree_find_path(struct got_object_id **id, mode_t *mode,
1865 struct got_repository *repo, struct got_tree_object *tree,
1866 const char *path)
1868 const struct got_error *err = NULL;
1869 struct got_tree_object *subtree = NULL;
1870 struct got_tree_entry *te = NULL;
1871 const char *seg, *s;
1872 size_t seglen;
1874 *id = NULL;
1876 s = path;
1877 while (s[0] == '/')
1878 s++;
1879 seg = s;
1880 seglen = 0;
1881 subtree = tree;
1882 while (*s) {
1883 struct got_tree_object *next_tree;
1885 if (*s != '/') {
1886 s++;
1887 seglen++;
1888 if (*s)
1889 continue;
1892 te = find_entry_by_name(subtree, seg, seglen);
1893 if (te == NULL) {
1894 err = got_error_path(path, GOT_ERR_NO_TREE_ENTRY);
1895 goto done;
1898 if (*s == '\0')
1899 break;
1901 seg = s + 1;
1902 seglen = 0;
1903 s++;
1904 if (*s) {
1905 err = got_object_open_as_tree(&next_tree, repo,
1906 &te->id);
1907 te = NULL;
1908 if (err)
1909 goto done;
1910 if (subtree != tree)
1911 got_object_tree_close(subtree);
1912 subtree = next_tree;
1916 if (te) {
1917 *id = got_object_id_dup(&te->id);
1918 if (*id == NULL)
1919 return got_error_from_errno("got_object_id_dup");
1920 if (mode)
1921 *mode = te->mode;
1922 } else
1923 err = got_error_path(path, GOT_ERR_NO_TREE_ENTRY);
1924 done:
1925 if (subtree && subtree != tree)
1926 got_object_tree_close(subtree);
1927 return err;
1929 const struct got_error *
1930 got_object_id_by_path(struct got_object_id **id, struct got_repository *repo,
1931 struct got_object_id *commit_id, const char *path)
1933 const struct got_error *err = NULL;
1934 struct got_commit_object *commit = NULL;
1935 struct got_tree_object *tree = NULL;
1937 *id = NULL;
1939 err = got_object_open_as_commit(&commit, repo, commit_id);
1940 if (err)
1941 goto done;
1943 /* Handle opening of root of commit's tree. */
1944 if (got_path_is_root_dir(path)) {
1945 *id = got_object_id_dup(commit->tree_id);
1946 if (*id == NULL)
1947 err = got_error_from_errno("got_object_id_dup");
1948 } else {
1949 err = got_object_open_as_tree(&tree, repo, commit->tree_id);
1950 if (err)
1951 goto done;
1952 err = got_object_tree_find_path(id, NULL, repo, tree, path);
1954 done:
1955 if (commit)
1956 got_object_commit_close(commit);
1957 if (tree)
1958 got_object_tree_close(tree);
1959 return err;
1963 * Normalize file mode bits to avoid false positive tree entry differences
1964 * in case tree entries have unexpected mode bits set.
1966 static mode_t
1967 normalize_mode_for_comparison(mode_t mode)
1970 * For directories, the only relevant bit is the IFDIR bit.
1971 * This allows us to detect paths changing from a directory
1972 * to a file and vice versa.
1974 if (S_ISDIR(mode))
1975 return mode & S_IFDIR;
1978 * For symlinks, the only relevant bit is the IFLNK bit.
1979 * This allows us to detect paths changing from a symlinks
1980 * to a file or directory and vice versa.
1982 if (S_ISLNK(mode))
1983 return mode & S_IFLNK;
1985 /* For files, the only change we care about is the executable bit. */
1986 return mode & S_IXUSR;
1989 const struct got_error *
1990 got_object_tree_path_changed(int *changed,
1991 struct got_tree_object *tree01, struct got_tree_object *tree02,
1992 const char *path, struct got_repository *repo)
1994 const struct got_error *err = NULL;
1995 struct got_tree_object *tree1 = NULL, *tree2 = NULL;
1996 struct got_tree_entry *te1 = NULL, *te2 = NULL;
1997 const char *seg, *s;
1998 size_t seglen;
2000 *changed = 0;
2002 /* We not do support comparing the root path. */
2003 if (got_path_is_root_dir(path))
2004 return got_error_path(path, GOT_ERR_BAD_PATH);
2006 tree1 = tree01;
2007 tree2 = tree02;
2008 s = path;
2009 while (*s == '/')
2010 s++;
2011 seg = s;
2012 seglen = 0;
2013 while (*s) {
2014 struct got_tree_object *next_tree1, *next_tree2;
2015 mode_t mode1, mode2;
2017 if (*s != '/') {
2018 s++;
2019 seglen++;
2020 if (*s)
2021 continue;
2024 te1 = find_entry_by_name(tree1, seg, seglen);
2025 if (te1 == NULL) {
2026 err = got_error(GOT_ERR_NO_OBJ);
2027 goto done;
2030 if (tree2)
2031 te2 = find_entry_by_name(tree2, seg, seglen);
2033 if (te2) {
2034 mode1 = normalize_mode_for_comparison(te1->mode);
2035 mode2 = normalize_mode_for_comparison(te2->mode);
2036 if (mode1 != mode2) {
2037 *changed = 1;
2038 goto done;
2041 if (got_object_id_cmp(&te1->id, &te2->id) == 0) {
2042 *changed = 0;
2043 goto done;
2047 if (*s == '\0') { /* final path element */
2048 *changed = 1;
2049 goto done;
2052 seg = s + 1;
2053 s++;
2054 seglen = 0;
2055 if (*s) {
2056 err = got_object_open_as_tree(&next_tree1, repo,
2057 &te1->id);
2058 te1 = NULL;
2059 if (err)
2060 goto done;
2061 if (tree1 != tree01)
2062 got_object_tree_close(tree1);
2063 tree1 = next_tree1;
2065 if (te2) {
2066 err = got_object_open_as_tree(&next_tree2, repo,
2067 &te2->id);
2068 te2 = NULL;
2069 if (err)
2070 goto done;
2071 if (tree2 != tree02)
2072 got_object_tree_close(tree2);
2073 tree2 = next_tree2;
2074 } else if (tree2) {
2075 if (tree2 != tree02)
2076 got_object_tree_close(tree2);
2077 tree2 = NULL;
2081 done:
2082 if (tree1 && tree1 != tree01)
2083 got_object_tree_close(tree1);
2084 if (tree2 && tree2 != tree02)
2085 got_object_tree_close(tree2);
2086 return err;
2089 const struct got_error *
2090 got_object_tree_entry_dup(struct got_tree_entry **new_te,
2091 struct got_tree_entry *te)
2093 const struct got_error *err = NULL;
2095 *new_te = calloc(1, sizeof(**new_te));
2096 if (*new_te == NULL)
2097 return got_error_from_errno("calloc");
2099 (*new_te)->mode = te->mode;
2100 memcpy((*new_te)->name, te->name, sizeof((*new_te)->name));
2101 memcpy(&(*new_te)->id, &te->id, sizeof((*new_te)->id));
2102 return err;
2105 int
2106 got_object_tree_entry_is_submodule(struct got_tree_entry *te)
2108 return (te->mode & S_IFMT) == (S_IFDIR | S_IFLNK);
2111 int
2112 got_object_tree_entry_is_symlink(struct got_tree_entry *te)
2114 /* S_IFDIR check avoids confusing symlinks with submodules. */
2115 return ((te->mode & (S_IFDIR | S_IFLNK)) == S_IFLNK);
2118 static const struct got_error *
2119 resolve_symlink(char **link_target, const char *path,
2120 struct got_object_id *commit_id, struct got_repository *repo)
2122 const struct got_error *err = NULL;
2123 char buf[PATH_MAX];
2124 char *name, *parent_path = NULL;
2125 struct got_object_id *tree_obj_id = NULL;
2126 struct got_tree_object *tree = NULL;
2127 struct got_tree_entry *te = NULL;
2129 *link_target = NULL;
2131 if (strlcpy(buf, path, sizeof(buf)) >= sizeof(buf))
2132 return got_error(GOT_ERR_NO_SPACE);
2134 name = basename(buf);
2135 if (name == NULL)
2136 return got_error_from_errno2("basename", path);
2138 err = got_path_dirname(&parent_path, path);
2139 if (err)
2140 return err;
2142 err = got_object_id_by_path(&tree_obj_id, repo, commit_id,
2143 parent_path);
2144 if (err) {
2145 if (err->code == GOT_ERR_NO_TREE_ENTRY) {
2146 /* Display the complete path in error message. */
2147 err = got_error_path(path, err->code);
2149 goto done;
2152 err = got_object_open_as_tree(&tree, repo, tree_obj_id);
2153 if (err)
2154 goto done;
2156 te = got_object_tree_find_entry(tree, name);
2157 if (te == NULL) {
2158 err = got_error_path(path, GOT_ERR_NO_TREE_ENTRY);
2159 goto done;
2162 if (got_object_tree_entry_is_symlink(te)) {
2163 err = got_tree_entry_get_symlink_target(link_target, te, repo);
2164 if (err)
2165 goto done;
2166 if (!got_path_is_absolute(*link_target)) {
2167 char *abspath;
2168 if (asprintf(&abspath, "%s/%s", parent_path,
2169 *link_target) == -1) {
2170 err = got_error_from_errno("asprintf");
2171 goto done;
2173 free(*link_target);
2174 *link_target = malloc(PATH_MAX);
2175 if (*link_target == NULL) {
2176 err = got_error_from_errno("malloc");
2177 goto done;
2179 err = got_canonpath(abspath, *link_target, PATH_MAX);
2180 free(abspath);
2181 if (err)
2182 goto done;
2185 done:
2186 free(tree_obj_id);
2187 if (tree)
2188 got_object_tree_close(tree);
2189 if (err) {
2190 free(*link_target);
2191 *link_target = NULL;
2193 return err;
2196 const struct got_error *
2197 got_object_resolve_symlinks(char **link_target, const char *path,
2198 struct got_object_id *commit_id, struct got_repository *repo)
2200 const struct got_error *err = NULL;
2201 char *next_target = NULL;
2202 int max_recursion = 40; /* matches Git */
2204 *link_target = NULL;
2206 do {
2207 err = resolve_symlink(&next_target,
2208 *link_target ? *link_target : path, commit_id, repo);
2209 if (err)
2210 break;
2211 if (next_target) {
2212 free(*link_target);
2213 if (--max_recursion == 0) {
2214 err = got_error_path(path, GOT_ERR_RECURSION);
2215 *link_target = NULL;
2216 break;
2218 *link_target = next_target;
2220 } while (next_target);
2222 return err;
2225 const struct got_error *
2226 got_traverse_packed_commits(struct got_object_id_queue *traversed_commits,
2227 struct got_object_id *commit_id, const char *path,
2228 struct got_repository *repo)
2230 const struct got_error *err = NULL;
2231 struct got_pack *pack = NULL;
2232 struct got_packidx *packidx = NULL;
2233 char *path_packfile = NULL;
2234 struct got_commit_object *changed_commit = NULL;
2235 struct got_object_id *changed_commit_id = NULL;
2236 int idx;
2238 err = got_repo_search_packidx(&packidx, &idx, repo, commit_id);
2239 if (err) {
2240 if (err->code != GOT_ERR_NO_OBJ)
2241 return err;
2242 return NULL;
2245 err = got_packidx_get_packfile_path(&path_packfile,
2246 packidx->path_packidx);
2247 if (err)
2248 return err;
2250 pack = got_repo_get_cached_pack(repo, path_packfile);
2251 if (pack == NULL) {
2252 err = got_repo_cache_pack(&pack, repo, path_packfile, packidx);
2253 if (err)
2254 goto done;
2257 if (pack->privsep_child == NULL) {
2258 err = start_pack_privsep_child(pack, packidx);
2259 if (err)
2260 goto done;
2263 err = got_privsep_send_commit_traversal_request(
2264 pack->privsep_child->ibuf, commit_id, idx, path);
2265 if (err)
2266 goto done;
2268 err = got_privsep_recv_traversed_commits(&changed_commit,
2269 &changed_commit_id, traversed_commits, pack->privsep_child->ibuf);
2270 if (err)
2271 goto done;
2273 if (changed_commit) {
2275 * Cache the commit in which the path was changed.
2276 * This commit might be opened again soon.
2278 changed_commit->refcnt++;
2279 err = got_repo_cache_commit(repo, changed_commit_id,
2280 changed_commit);
2281 got_object_commit_close(changed_commit);
2283 done:
2284 free(path_packfile);
2285 free(changed_commit_id);
2286 return err;