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/uio.h>
20 #include <sys/socket.h>
21 #include <sys/wait.h>
22 #include <sys/resource.h>
23 #include <sys/mman.h>
25 #include <errno.h>
26 #include <fcntl.h>
27 #include <stdio.h>
28 #include <stdlib.h>
29 #include <string.h>
30 #include <stdint.h>
31 #include <unistd.h>
32 #include <zlib.h>
33 #include <ctype.h>
34 #include <libgen.h>
35 #include <limits.h>
36 #include <time.h>
38 #include "got_compat.h"
40 #include "got_error.h"
41 #include "got_object.h"
42 #include "got_repository.h"
43 #include "got_opentemp.h"
44 #include "got_path.h"
46 #include "got_lib_sha1.h"
47 #include "got_lib_delta.h"
48 #include "got_lib_inflate.h"
49 #include "got_lib_object.h"
50 #include "got_lib_privsep.h"
51 #include "got_lib_object_idcache.h"
52 #include "got_lib_object_cache.h"
53 #include "got_lib_object_parse.h"
54 #include "got_lib_pack.h"
55 #include "got_lib_repository.h"
57 #ifndef MIN
58 #define MIN(_a,_b) ((_a) < (_b) ? (_a) : (_b))
59 #endif
61 struct got_object_id *
62 got_object_get_id(struct got_object *obj)
63 {
64 return &obj->id;
65 }
67 const struct got_error *
68 got_object_get_id_str(char **outbuf, struct got_object *obj)
69 {
70 return got_object_id_str(outbuf, &obj->id);
71 }
73 const struct got_error *
74 got_object_get_type(int *type, struct got_repository *repo,
75 struct got_object_id *id)
76 {
77 const struct got_error *err = NULL;
78 struct got_object *obj;
80 err = got_object_open(&obj, repo, id);
81 if (err)
82 return err;
84 switch (obj->type) {
85 case GOT_OBJ_TYPE_COMMIT:
86 case GOT_OBJ_TYPE_TREE:
87 case GOT_OBJ_TYPE_BLOB:
88 case GOT_OBJ_TYPE_TAG:
89 *type = obj->type;
90 break;
91 default:
92 err = got_error(GOT_ERR_OBJ_TYPE);
93 break;
94 }
96 got_object_close(obj);
97 return err;
98 }
100 const struct got_error *
101 got_object_get_path(char **path, struct got_object_id *id,
102 struct got_repository *repo)
104 const struct got_error *err = NULL;
105 char *hex = NULL;
106 char *path_objects;
108 *path = NULL;
110 path_objects = got_repo_get_path_objects(repo);
111 if (path_objects == NULL)
112 return got_error_from_errno("got_repo_get_path_objects");
114 err = got_object_id_str(&hex, id);
115 if (err)
116 goto done;
118 if (asprintf(path, "%s/%.2x/%s", path_objects,
119 id->sha1[0], hex + 2) == -1)
120 err = got_error_from_errno("asprintf");
122 done:
123 free(hex);
124 free(path_objects);
125 return err;
128 const struct got_error *
129 got_object_open_loose_fd(int *fd, struct got_object_id *id,
130 struct got_repository *repo)
132 const struct got_error *err = NULL;
133 char *path;
135 err = got_object_get_path(&path, id, repo);
136 if (err)
137 return err;
138 *fd = open(path, O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
139 if (*fd == -1) {
140 err = got_error_from_errno2("open", path);
141 goto done;
143 done:
144 free(path);
145 return err;
148 static const struct got_error *
149 request_packed_object(struct got_object **obj, struct got_pack *pack, int idx,
150 struct got_object_id *id)
152 const struct got_error *err = NULL;
153 struct imsgbuf *ibuf = pack->privsep_child->ibuf;
155 err = got_privsep_send_packed_obj_req(ibuf, idx, id);
156 if (err)
157 return err;
159 err = got_privsep_recv_obj(obj, ibuf);
160 if (err)
161 return err;
163 memcpy(&(*obj)->id, id, sizeof((*obj)->id));
165 return NULL;
168 /* Create temporary files used during delta application. */
169 static const struct got_error *
170 pack_child_send_tempfiles(struct imsgbuf *ibuf, struct got_pack *pack)
172 const struct got_error *err;
173 int basefd, accumfd;
175 /*
176 * For performance reasons, the child will keep reusing the
177 * same temporary files during every object request.
178 * Opening and closing new files for every object request is
179 * too expensive during operations such as 'gotadmin pack'.
180 */
181 if (pack->child_has_tempfiles)
182 return NULL;
184 basefd = got_opentempfd();
185 if (basefd == -1)
186 return got_error_from_errno("got_opentempfd");
188 err = got_privsep_send_tmpfd(ibuf, basefd);
189 if (err)
190 return err;
192 accumfd = got_opentempfd();
193 if (accumfd == -1)
194 return got_error_from_errno("got_opentempfd");
196 err = got_privsep_send_tmpfd(ibuf, accumfd);
197 if (err)
198 return err;
200 pack->child_has_tempfiles = 1;
201 return NULL;
204 static const struct got_error *
205 request_packed_object_raw(uint8_t **outbuf, off_t *size, size_t *hdrlen,
206 int outfd, struct got_pack *pack, int idx, struct got_object_id *id)
208 const struct got_error *err = NULL;
209 struct imsgbuf *ibuf = pack->privsep_child->ibuf;
210 int outfd_child;
212 err = pack_child_send_tempfiles(ibuf, pack);
213 if (err)
214 return err;
216 outfd_child = dup(outfd);
217 if (outfd_child == -1)
218 return got_error_from_errno("dup");
220 err = got_privsep_send_packed_raw_obj_req(ibuf, idx, id);
221 if (err) {
222 close(outfd_child);
223 return err;
226 err = got_privsep_send_raw_obj_outfd(ibuf, outfd_child);
227 if (err)
228 return err;
230 err = got_privsep_recv_raw_obj(outbuf, size, hdrlen, ibuf);
231 if (err)
232 return err;
234 return NULL;
237 static void
238 set_max_datasize(void)
240 struct rlimit rl;
242 if (getrlimit(RLIMIT_DATA, &rl) != 0)
243 return;
245 rl.rlim_cur = rl.rlim_max;
246 setrlimit(RLIMIT_DATA, &rl);
249 static const struct got_error *
250 start_pack_privsep_child(struct got_pack *pack, struct got_packidx *packidx)
252 const struct got_error *err = NULL;
253 int imsg_fds[2];
254 pid_t pid;
255 struct imsgbuf *ibuf;
257 ibuf = calloc(1, sizeof(*ibuf));
258 if (ibuf == NULL)
259 return got_error_from_errno("calloc");
261 pack->privsep_child = calloc(1, sizeof(*pack->privsep_child));
262 if (pack->privsep_child == NULL) {
263 err = got_error_from_errno("calloc");
264 free(ibuf);
265 return err;
267 pack->child_has_tempfiles = 0;
268 pack->child_has_delta_outfd = 0;
270 if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_fds) == -1) {
271 err = got_error_from_errno("socketpair");
272 goto done;
275 pid = fork();
276 if (pid == -1) {
277 err = got_error_from_errno("fork");
278 goto done;
279 } else if (pid == 0) {
280 set_max_datasize();
281 got_privsep_exec_child(imsg_fds, GOT_PATH_PROG_READ_PACK,
282 pack->path_packfile);
283 /* not reached */
286 if (close(imsg_fds[1]) == -1)
287 return got_error_from_errno("close");
288 pack->privsep_child->imsg_fd = imsg_fds[0];
289 pack->privsep_child->pid = pid;
290 imsg_init(ibuf, imsg_fds[0]);
291 pack->privsep_child->ibuf = ibuf;
293 err = got_privsep_init_pack_child(ibuf, pack, packidx);
294 if (err) {
295 const struct got_error *child_err;
296 err = got_privsep_send_stop(pack->privsep_child->imsg_fd);
297 child_err = got_privsep_wait_for_child(
298 pack->privsep_child->pid);
299 if (child_err && err == NULL)
300 err = child_err;
302 done:
303 if (err) {
304 free(ibuf);
305 free(pack->privsep_child);
306 pack->privsep_child = NULL;
308 return err;
311 static const struct got_error *
312 read_packed_object_privsep(struct got_object **obj,
313 struct got_repository *repo, struct got_pack *pack,
314 struct got_packidx *packidx, int idx, struct got_object_id *id)
316 const struct got_error *err = NULL;
318 if (pack->privsep_child == NULL) {
319 err = start_pack_privsep_child(pack, packidx);
320 if (err)
321 return err;
324 return request_packed_object(obj, pack, idx, id);
327 static const struct got_error *
328 read_packed_object_raw_privsep(uint8_t **outbuf, off_t *size, size_t *hdrlen,
329 int outfd, struct got_pack *pack, struct got_packidx *packidx, int idx,
330 struct got_object_id *id)
332 const struct got_error *err = NULL;
334 if (pack->privsep_child == NULL) {
335 err = start_pack_privsep_child(pack, packidx);
336 if (err)
337 return err;
340 return request_packed_object_raw(outbuf, size, hdrlen, outfd, pack,
341 idx, id);
344 const struct got_error *
345 got_object_open_packed(struct got_object **obj, struct got_object_id *id,
346 struct got_repository *repo)
348 const struct got_error *err = NULL;
349 struct got_pack *pack = NULL;
350 struct got_packidx *packidx = NULL;
351 int idx;
352 char *path_packfile;
354 err = got_repo_search_packidx(&packidx, &idx, repo, id);
355 if (err)
356 return err;
358 err = got_packidx_get_packfile_path(&path_packfile,
359 packidx->path_packidx);
360 if (err)
361 return err;
363 pack = got_repo_get_cached_pack(repo, path_packfile);
364 if (pack == NULL) {
365 err = got_repo_cache_pack(&pack, repo, path_packfile, packidx);
366 if (err)
367 goto done;
370 err = read_packed_object_privsep(obj, repo, pack, packidx, idx, id);
371 if (err)
372 goto done;
373 done:
374 free(path_packfile);
375 return err;
378 const struct got_error *
379 got_object_open_from_packfile(struct got_object **obj, struct got_object_id *id,
380 struct got_pack *pack, struct got_packidx *packidx, int obj_idx,
381 struct got_repository *repo)
383 return read_packed_object_privsep(obj, repo, pack, packidx,
384 obj_idx, id);
387 const struct got_error *
388 got_object_read_raw_delta(uint64_t *base_size, uint64_t *result_size,
389 off_t *delta_size, off_t *delta_offset, off_t *delta_out_offset,
390 struct got_object_id **base_id, int delta_cache_fd,
391 struct got_packidx *packidx, int obj_idx, struct got_object_id *id,
392 struct got_repository *repo)
394 const struct got_error *err = NULL;
395 struct got_pack *pack = NULL;
396 char *path_packfile;
398 *base_size = 0;
399 *result_size = 0;
400 *delta_size = 0;
401 *delta_offset = 0;
402 *delta_out_offset = 0;
404 err = got_packidx_get_packfile_path(&path_packfile,
405 packidx->path_packidx);
406 if (err)
407 return err;
409 pack = got_repo_get_cached_pack(repo, path_packfile);
410 if (pack == NULL) {
411 err = got_repo_cache_pack(&pack, repo, path_packfile, packidx);
412 if (err)
413 return err;
416 if (pack->privsep_child == NULL) {
417 err = start_pack_privsep_child(pack, packidx);
418 if (err)
419 return err;
422 if (!pack->child_has_delta_outfd) {
423 int outfd_child;
424 outfd_child = dup(delta_cache_fd);
425 if (outfd_child == -1)
426 return got_error_from_errno("dup");
427 err = got_privsep_send_raw_delta_outfd(
428 pack->privsep_child->ibuf, outfd_child);
429 if (err)
430 return err;
431 pack->child_has_delta_outfd = 1;
434 err = got_privsep_send_raw_delta_req(pack->privsep_child->ibuf,
435 obj_idx, id);
436 if (err)
437 return err;
439 return got_privsep_recv_raw_delta(base_size, result_size, delta_size,
440 delta_offset, delta_out_offset, base_id, pack->privsep_child->ibuf);
443 static const struct got_error *
444 request_object(struct got_object **obj, struct got_object_id *id,
445 struct got_repository *repo, int fd)
447 const struct got_error *err = NULL;
448 struct imsgbuf *ibuf;
450 ibuf = repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_OBJECT].ibuf;
452 err = got_privsep_send_obj_req(ibuf, fd, id);
453 if (err)
454 return err;
456 return got_privsep_recv_obj(obj, ibuf);
459 static const struct got_error *
460 request_raw_object(uint8_t **outbuf, off_t *size, size_t *hdrlen, int outfd,
461 struct got_object_id *id, struct got_repository *repo, int infd)
463 const struct got_error *err = NULL;
464 struct imsgbuf *ibuf;
465 int outfd_child;
467 ibuf = repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_OBJECT].ibuf;
469 outfd_child = dup(outfd);
470 if (outfd_child == -1)
471 return got_error_from_errno("dup");
473 err = got_privsep_send_raw_obj_req(ibuf, infd, id);
474 if (err)
475 return err;
477 err = got_privsep_send_raw_obj_outfd(ibuf, outfd_child);
478 if (err)
479 return err;
481 return got_privsep_recv_raw_obj(outbuf, size, hdrlen, ibuf);
484 static const struct got_error *
485 start_read_object_child(struct got_repository *repo)
487 const struct got_error *err = NULL;
488 int imsg_fds[2];
489 pid_t pid;
490 struct imsgbuf *ibuf;
492 ibuf = calloc(1, sizeof(*ibuf));
493 if (ibuf == NULL)
494 return got_error_from_errno("calloc");
496 if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_fds) == -1) {
497 err = got_error_from_errno("socketpair");
498 free(ibuf);
499 return err;
502 pid = fork();
503 if (pid == -1) {
504 err = got_error_from_errno("fork");
505 free(ibuf);
506 return err;
508 else if (pid == 0) {
509 got_privsep_exec_child(imsg_fds, GOT_PATH_PROG_READ_OBJECT,
510 repo->path);
511 /* not reached */
514 if (close(imsg_fds[1]) == -1) {
515 err = got_error_from_errno("close");
516 free(ibuf);
517 return err;
520 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_OBJECT].imsg_fd =
521 imsg_fds[0];
522 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_OBJECT].pid = pid;
523 imsg_init(ibuf, imsg_fds[0]);
524 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_OBJECT].ibuf = ibuf;
526 return NULL;
529 const struct got_error *
530 got_object_read_header_privsep(struct got_object **obj,
531 struct got_object_id *id, struct got_repository *repo, int obj_fd)
533 const struct got_error *err;
535 if (repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_OBJECT].imsg_fd != -1)
536 return request_object(obj, id, repo, obj_fd);
538 err = start_read_object_child(repo);
539 if (err) {
540 close(obj_fd);
541 return err;
544 return request_object(obj, id, repo, obj_fd);
547 static const struct got_error *
548 read_object_raw_privsep(uint8_t **outbuf, off_t *size, size_t *hdrlen,
549 int outfd, struct got_object_id *id, struct got_repository *repo,
550 int obj_fd)
552 const struct got_error *err;
554 if (repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_OBJECT].imsg_fd != -1)
555 return request_raw_object(outbuf, size, hdrlen, outfd, id,
556 repo, obj_fd);
558 err = start_read_object_child(repo);
559 if (err)
560 return err;
562 return request_raw_object(outbuf, size, hdrlen, outfd, id, repo,
563 obj_fd);
566 const struct got_error *
567 got_object_open(struct got_object **obj, struct got_repository *repo,
568 struct got_object_id *id)
570 const struct got_error *err = NULL;
571 int fd;
573 *obj = got_repo_get_cached_object(repo, id);
574 if (*obj != NULL) {
575 (*obj)->refcnt++;
576 return NULL;
579 err = got_object_open_packed(obj, id, repo);
580 if (err && err->code != GOT_ERR_NO_OBJ)
581 return err;
582 if (*obj) {
583 (*obj)->refcnt++;
584 return got_repo_cache_object(repo, id, *obj);
587 err = got_object_open_loose_fd(&fd, id, repo);
588 if (err) {
589 if (err->code == GOT_ERR_ERRNO && errno == ENOENT)
590 err = got_error_no_obj(id);
591 return err;
594 err = got_object_read_header_privsep(obj, id, repo, fd);
595 if (err)
596 return err;
598 memcpy((*obj)->id.sha1, id->sha1, SHA1_DIGEST_LENGTH);
600 (*obj)->refcnt++;
601 return got_repo_cache_object(repo, id, *obj);
604 /* *outfd must be initialized to -1 by caller */
605 const struct got_error *
606 got_object_raw_open(struct got_raw_object **obj, int *outfd,
607 struct got_repository *repo, struct got_object_id *id)
609 const struct got_error *err = NULL;
610 struct got_packidx *packidx = NULL;
611 int idx;
612 uint8_t *outbuf = NULL;
613 off_t size = 0;
614 size_t hdrlen = 0;
615 char *path_packfile = NULL;
617 *obj = got_repo_get_cached_raw_object(repo, id);
618 if (*obj != NULL) {
619 (*obj)->refcnt++;
620 return NULL;
623 if (*outfd == -1) {
624 *outfd = got_opentempfd();
625 if (*outfd == -1)
626 return got_error_from_errno("got_opentempfd");
629 err = got_repo_search_packidx(&packidx, &idx, repo, id);
630 if (err == NULL) {
631 struct got_pack *pack = NULL;
633 err = got_packidx_get_packfile_path(&path_packfile,
634 packidx->path_packidx);
635 if (err)
636 goto done;
638 pack = got_repo_get_cached_pack(repo, path_packfile);
639 if (pack == NULL) {
640 err = got_repo_cache_pack(&pack, repo, path_packfile,
641 packidx);
642 if (err)
643 goto done;
645 err = read_packed_object_raw_privsep(&outbuf, &size, &hdrlen,
646 *outfd, pack, packidx, idx, id);
647 if (err)
648 goto done;
649 } else if (err->code == GOT_ERR_NO_OBJ) {
650 int fd;
652 err = got_object_open_loose_fd(&fd, id, repo);
653 if (err)
654 goto done;
655 err = read_object_raw_privsep(&outbuf, &size, &hdrlen, *outfd,
656 id, repo, fd);
657 if (err)
658 goto done;
661 *obj = calloc(1, sizeof(**obj));
662 if (*obj == NULL) {
663 err = got_error_from_errno("calloc");
664 goto done;
666 (*obj)->fd = -1;
668 if (outbuf) {
669 (*obj)->data = outbuf;
670 } else {
671 struct stat sb;
672 if (fstat(*outfd, &sb) == -1) {
673 err = got_error_from_errno("fstat");
674 goto done;
677 if (sb.st_size != hdrlen + size) {
678 err = got_error(GOT_ERR_PRIVSEP_LEN);
679 goto done;
681 #ifndef GOT_PACK_NO_MMAP
682 if (hdrlen + size > 0) {
683 (*obj)->data = mmap(NULL, hdrlen + size, PROT_READ,
684 MAP_PRIVATE, *outfd, 0);
685 if ((*obj)->data == MAP_FAILED) {
686 if (errno != ENOMEM) {
687 err = got_error_from_errno("mmap");
688 goto done;
690 (*obj)->data = NULL;
691 } else {
692 (*obj)->fd = *outfd;
693 *outfd = -1;
696 #endif
697 if (*outfd != -1) {
698 (*obj)->f = fdopen(*outfd, "r");
699 if ((*obj)->f == NULL) {
700 err = got_error_from_errno("fdopen");
701 goto done;
703 *outfd = -1;
706 (*obj)->hdrlen = hdrlen;
707 (*obj)->size = size;
708 err = got_repo_cache_raw_object(repo, id, *obj);
709 done:
710 free(path_packfile);
711 if (err) {
712 if (*obj) {
713 got_object_raw_close(*obj);
714 *obj = NULL;
716 free(outbuf);
717 } else
718 (*obj)->refcnt++;
719 return err;
722 const struct got_error *
723 got_object_open_by_id_str(struct got_object **obj, struct got_repository *repo,
724 const char *id_str)
726 struct got_object_id id;
728 if (!got_parse_sha1_digest(id.sha1, id_str))
729 return got_error_path(id_str, GOT_ERR_BAD_OBJ_ID_STR);
731 return got_object_open(obj, repo, &id);
734 const struct got_error *
735 got_object_resolve_id_str(struct got_object_id **id,
736 struct got_repository *repo, const char *id_str)
738 const struct got_error *err = NULL;
739 struct got_object *obj;
741 err = got_object_open_by_id_str(&obj, repo, id_str);
742 if (err)
743 return err;
745 *id = got_object_id_dup(got_object_get_id(obj));
746 got_object_close(obj);
747 if (*id == NULL)
748 return got_error_from_errno("got_object_id_dup");
750 return NULL;
753 static const struct got_error *
754 request_packed_commit(struct got_commit_object **commit, struct got_pack *pack,
755 int pack_idx, struct got_object_id *id)
757 const struct got_error *err = NULL;
759 err = got_privsep_send_commit_req(pack->privsep_child->ibuf, -1, id,
760 pack_idx);
761 if (err)
762 return err;
764 err = got_privsep_recv_commit(commit, pack->privsep_child->ibuf);
765 if (err)
766 return err;
768 (*commit)->flags |= GOT_COMMIT_FLAG_PACKED;
769 return NULL;
772 static const struct got_error *
773 read_packed_commit_privsep(struct got_commit_object **commit,
774 struct got_pack *pack, struct got_packidx *packidx, int idx,
775 struct got_object_id *id)
777 const struct got_error *err = NULL;
779 if (pack->privsep_child)
780 return request_packed_commit(commit, pack, idx, id);
782 err = start_pack_privsep_child(pack, packidx);
783 if (err)
784 return err;
786 return request_packed_commit(commit, pack, idx, id);
789 static const struct got_error *
790 request_commit(struct got_commit_object **commit, struct got_repository *repo,
791 int fd, struct got_object_id *id)
793 const struct got_error *err = NULL;
794 struct imsgbuf *ibuf;
796 ibuf = repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_COMMIT].ibuf;
798 err = got_privsep_send_commit_req(ibuf, fd, id, -1);
799 if (err)
800 return err;
802 return got_privsep_recv_commit(commit, ibuf);
805 static const struct got_error *
806 read_commit_privsep(struct got_commit_object **commit, int obj_fd,
807 struct got_object_id *id, struct got_repository *repo)
809 const struct got_error *err;
810 int imsg_fds[2];
811 pid_t pid;
812 struct imsgbuf *ibuf;
814 if (repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_COMMIT].imsg_fd != -1)
815 return request_commit(commit, repo, obj_fd, id);
817 ibuf = calloc(1, sizeof(*ibuf));
818 if (ibuf == NULL)
819 return got_error_from_errno("calloc");
821 if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_fds) == -1) {
822 err = got_error_from_errno("socketpair");
823 free(ibuf);
824 return err;
827 pid = fork();
828 if (pid == -1) {
829 err = got_error_from_errno("fork");
830 free(ibuf);
831 return err;
833 else if (pid == 0) {
834 got_privsep_exec_child(imsg_fds, GOT_PATH_PROG_READ_COMMIT,
835 repo->path);
836 /* not reached */
839 if (close(imsg_fds[1]) == -1) {
840 err = got_error_from_errno("close");
841 free(ibuf);
842 return err;
844 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_COMMIT].imsg_fd =
845 imsg_fds[0];
846 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_COMMIT].pid = pid;
847 imsg_init(ibuf, imsg_fds[0]);
848 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_COMMIT].ibuf = ibuf;
850 return request_commit(commit, repo, obj_fd, id);
854 static const struct got_error *
855 open_commit(struct got_commit_object **commit,
856 struct got_repository *repo, struct got_object_id *id, int check_cache)
858 const struct got_error *err = NULL;
859 struct got_packidx *packidx = NULL;
860 int idx;
861 char *path_packfile = NULL;
863 if (check_cache) {
864 *commit = got_repo_get_cached_commit(repo, id);
865 if (*commit != NULL) {
866 (*commit)->refcnt++;
867 return NULL;
869 } else
870 *commit = NULL;
872 err = got_repo_search_packidx(&packidx, &idx, repo, id);
873 if (err == NULL) {
874 struct got_pack *pack = NULL;
876 err = got_packidx_get_packfile_path(&path_packfile,
877 packidx->path_packidx);
878 if (err)
879 return err;
881 pack = got_repo_get_cached_pack(repo, path_packfile);
882 if (pack == NULL) {
883 err = got_repo_cache_pack(&pack, repo, path_packfile,
884 packidx);
885 if (err)
886 goto done;
888 err = read_packed_commit_privsep(commit, pack,
889 packidx, idx, id);
890 } else if (err->code == GOT_ERR_NO_OBJ) {
891 int fd;
893 err = got_object_open_loose_fd(&fd, id, repo);
894 if (err)
895 return err;
896 err = read_commit_privsep(commit, fd, id, repo);
899 if (err == NULL) {
900 (*commit)->refcnt++;
901 err = got_repo_cache_commit(repo, id, *commit);
903 done:
904 free(path_packfile);
905 return err;
908 const struct got_error *
909 got_object_open_as_commit(struct got_commit_object **commit,
910 struct got_repository *repo, struct got_object_id *id)
912 *commit = got_repo_get_cached_commit(repo, id);
913 if (*commit != NULL) {
914 (*commit)->refcnt++;
915 return NULL;
918 return open_commit(commit, repo, id, 0);
921 const struct got_error *
922 got_object_commit_open(struct got_commit_object **commit,
923 struct got_repository *repo, struct got_object *obj)
925 return open_commit(commit, repo, got_object_get_id(obj), 1);
928 const struct got_error *
929 got_object_qid_alloc(struct got_object_qid **qid, struct got_object_id *id)
931 const struct got_error *err = NULL;
933 *qid = calloc(1, sizeof(**qid));
934 if (*qid == NULL)
935 return got_error_from_errno("calloc");
937 (*qid)->id = got_object_id_dup(id);
938 if ((*qid)->id == NULL) {
939 err = got_error_from_errno("got_object_id_dup");
940 got_object_qid_free(*qid);
941 *qid = NULL;
942 return err;
945 return NULL;
948 const struct got_error *
949 got_object_id_queue_copy(const struct got_object_id_queue *src,
950 struct got_object_id_queue *dest)
952 const struct got_error *err;
953 struct got_object_qid *qid;
955 STAILQ_FOREACH(qid, src, entry) {
956 struct got_object_qid *new;
957 /*
958 * Deep-copy the object ID only. Let the caller deal
959 * with setting up the new->data pointer if needed.
960 */
961 err = got_object_qid_alloc(&new, qid->id);
962 if (err) {
963 got_object_id_queue_free(dest);
964 return err;
966 STAILQ_INSERT_TAIL(dest, new, entry);
969 return NULL;
972 static const struct got_error *
973 request_packed_tree(struct got_tree_object **tree, struct got_pack *pack,
974 int pack_idx, struct got_object_id *id)
976 const struct got_error *err = NULL;
978 err = got_privsep_send_tree_req(pack->privsep_child->ibuf, -1, id,
979 pack_idx);
980 if (err)
981 return err;
983 return got_privsep_recv_tree(tree, pack->privsep_child->ibuf);
986 static const struct got_error *
987 read_packed_tree_privsep(struct got_tree_object **tree,
988 struct got_pack *pack, struct got_packidx *packidx, int idx,
989 struct got_object_id *id)
991 const struct got_error *err = NULL;
993 if (pack->privsep_child)
994 return request_packed_tree(tree, pack, idx, id);
996 err = start_pack_privsep_child(pack, packidx);
997 if (err)
998 return err;
1000 return request_packed_tree(tree, pack, idx, id);
1003 static const struct got_error *
1004 request_tree(struct got_tree_object **tree, struct got_repository *repo,
1005 int fd, struct got_object_id *id)
1007 const struct got_error *err = NULL;
1008 struct imsgbuf *ibuf;
1010 ibuf = repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TREE].ibuf;
1012 err = got_privsep_send_tree_req(ibuf, fd, id, -1);
1013 if (err)
1014 return err;
1016 return got_privsep_recv_tree(tree, ibuf);
1019 const struct got_error *
1020 read_tree_privsep(struct got_tree_object **tree, int obj_fd,
1021 struct got_object_id *id, struct got_repository *repo)
1023 const struct got_error *err;
1024 int imsg_fds[2];
1025 pid_t pid;
1026 struct imsgbuf *ibuf;
1028 if (repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TREE].imsg_fd != -1)
1029 return request_tree(tree, repo, obj_fd, id);
1031 ibuf = calloc(1, sizeof(*ibuf));
1032 if (ibuf == NULL)
1033 return got_error_from_errno("calloc");
1035 if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_fds) == -1) {
1036 err = got_error_from_errno("socketpair");
1037 free(ibuf);
1038 return err;
1041 pid = fork();
1042 if (pid == -1) {
1043 err = got_error_from_errno("fork");
1044 free(ibuf);
1045 return err;
1047 else if (pid == 0) {
1048 got_privsep_exec_child(imsg_fds, GOT_PATH_PROG_READ_TREE,
1049 repo->path);
1050 /* not reached */
1053 if (close(imsg_fds[1]) == -1) {
1054 err = got_error_from_errno("close");
1055 free(ibuf);
1056 return err;
1058 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TREE].imsg_fd =
1059 imsg_fds[0];
1060 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TREE].pid = pid;
1061 imsg_init(ibuf, imsg_fds[0]);
1062 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TREE].ibuf = ibuf;
1065 return request_tree(tree, repo, obj_fd, id);
1068 static const struct got_error *
1069 open_tree(struct got_tree_object **tree, struct got_repository *repo,
1070 struct got_object_id *id, int check_cache)
1072 const struct got_error *err = NULL;
1073 struct got_packidx *packidx = NULL;
1074 int idx;
1075 char *path_packfile = NULL;
1077 if (check_cache) {
1078 *tree = got_repo_get_cached_tree(repo, id);
1079 if (*tree != NULL) {
1080 (*tree)->refcnt++;
1081 return NULL;
1083 } else
1084 *tree = NULL;
1086 err = got_repo_search_packidx(&packidx, &idx, repo, id);
1087 if (err == NULL) {
1088 struct got_pack *pack = NULL;
1090 err = got_packidx_get_packfile_path(&path_packfile,
1091 packidx->path_packidx);
1092 if (err)
1093 return err;
1095 pack = got_repo_get_cached_pack(repo, path_packfile);
1096 if (pack == NULL) {
1097 err = got_repo_cache_pack(&pack, repo, path_packfile,
1098 packidx);
1099 if (err)
1100 goto done;
1102 err = read_packed_tree_privsep(tree, pack,
1103 packidx, idx, id);
1104 } else if (err->code == GOT_ERR_NO_OBJ) {
1105 int fd;
1107 err = got_object_open_loose_fd(&fd, id, repo);
1108 if (err)
1109 return err;
1110 err = read_tree_privsep(tree, fd, id, repo);
1113 if (err == NULL) {
1114 (*tree)->refcnt++;
1115 err = got_repo_cache_tree(repo, id, *tree);
1117 done:
1118 free(path_packfile);
1119 return err;
1122 const struct got_error *
1123 got_object_open_as_tree(struct got_tree_object **tree,
1124 struct got_repository *repo, struct got_object_id *id)
1126 *tree = got_repo_get_cached_tree(repo, id);
1127 if (*tree != NULL) {
1128 (*tree)->refcnt++;
1129 return NULL;
1132 return open_tree(tree, repo, id, 0);
1135 const struct got_error *
1136 got_object_tree_open(struct got_tree_object **tree,
1137 struct got_repository *repo, struct got_object *obj)
1139 return open_tree(tree, repo, got_object_get_id(obj), 1);
1142 int
1143 got_object_tree_get_nentries(struct got_tree_object *tree)
1145 return tree->nentries;
1148 struct got_tree_entry *
1149 got_object_tree_get_first_entry(struct got_tree_object *tree)
1151 return got_object_tree_get_entry(tree, 0);
1154 struct got_tree_entry *
1155 got_object_tree_get_last_entry(struct got_tree_object *tree)
1157 return got_object_tree_get_entry(tree, tree->nentries - 1);
1160 struct got_tree_entry *
1161 got_object_tree_get_entry(struct got_tree_object *tree, int i)
1163 if (i < 0 || i >= tree->nentries)
1164 return NULL;
1165 return &tree->entries[i];
1168 mode_t
1169 got_tree_entry_get_mode(struct got_tree_entry *te)
1171 return te->mode;
1174 const char *
1175 got_tree_entry_get_name(struct got_tree_entry *te)
1177 return &te->name[0];
1180 struct got_object_id *
1181 got_tree_entry_get_id(struct got_tree_entry *te)
1183 return &te->id;
1186 const struct got_error *
1187 got_object_blob_read_to_str(char **s, struct got_blob_object *blob)
1189 const struct got_error *err = NULL;
1190 size_t len, totlen, hdrlen, offset;
1192 *s = NULL;
1194 hdrlen = got_object_blob_get_hdrlen(blob);
1195 totlen = 0;
1196 offset = 0;
1197 do {
1198 char *p;
1200 err = got_object_blob_read_block(&len, blob);
1201 if (err)
1202 return err;
1204 if (len == 0)
1205 break;
1207 totlen += len - hdrlen;
1208 p = realloc(*s, totlen + 1);
1209 if (p == NULL) {
1210 err = got_error_from_errno("realloc");
1211 free(*s);
1212 *s = NULL;
1213 return err;
1215 *s = p;
1216 /* Skip blob object header first time around. */
1217 memcpy(*s + offset,
1218 got_object_blob_get_read_buf(blob) + hdrlen, len - hdrlen);
1219 hdrlen = 0;
1220 offset = totlen;
1221 } while (len > 0);
1223 (*s)[totlen] = '\0';
1224 return NULL;
1227 const struct got_error *
1228 got_tree_entry_get_symlink_target(char **link_target, struct got_tree_entry *te,
1229 struct got_repository *repo)
1231 const struct got_error *err = NULL;
1232 struct got_blob_object *blob = NULL;
1234 *link_target = NULL;
1236 if (!got_object_tree_entry_is_symlink(te))
1237 return got_error(GOT_ERR_TREE_ENTRY_TYPE);
1239 err = got_object_open_as_blob(&blob, repo,
1240 got_tree_entry_get_id(te), PATH_MAX);
1241 if (err)
1242 return err;
1244 err = got_object_blob_read_to_str(link_target, blob);
1245 got_object_blob_close(blob);
1246 if (err) {
1247 free(*link_target);
1248 *link_target = NULL;
1250 return err;
1253 int
1254 got_tree_entry_get_index(struct got_tree_entry *te)
1256 return te->idx;
1259 struct got_tree_entry *
1260 got_tree_entry_get_next(struct got_tree_object *tree,
1261 struct got_tree_entry *te)
1263 return got_object_tree_get_entry(tree, te->idx + 1);
1266 struct got_tree_entry *
1267 got_tree_entry_get_prev(struct got_tree_object *tree,
1268 struct got_tree_entry *te)
1270 return got_object_tree_get_entry(tree, te->idx - 1);
1273 static const struct got_error *
1274 request_packed_blob(uint8_t **outbuf, size_t *size, size_t *hdrlen, int outfd,
1275 struct got_pack *pack, struct got_packidx *packidx, int idx,
1276 struct got_object_id *id)
1278 const struct got_error *err = NULL;
1279 struct imsgbuf *ibuf = pack->privsep_child->ibuf;
1280 int outfd_child;
1282 err = pack_child_send_tempfiles(ibuf, pack);
1283 if (err)
1284 return err;
1286 outfd_child = dup(outfd);
1287 if (outfd_child == -1)
1288 return got_error_from_errno("dup");
1290 err = got_privsep_send_blob_req(pack->privsep_child->ibuf, -1, id, idx);
1291 if (err)
1292 return err;
1294 err = got_privsep_send_blob_outfd(pack->privsep_child->ibuf,
1295 outfd_child);
1296 if (err) {
1297 return err;
1300 err = got_privsep_recv_blob(outbuf, size, hdrlen,
1301 pack->privsep_child->ibuf);
1302 if (err)
1303 return err;
1305 if (lseek(outfd, SEEK_SET, 0) == -1)
1306 err = got_error_from_errno("lseek");
1308 return err;
1311 static const struct got_error *
1312 read_packed_blob_privsep(uint8_t **outbuf, size_t *size, size_t *hdrlen,
1313 int outfd, struct got_pack *pack, struct got_packidx *packidx, int idx,
1314 struct got_object_id *id)
1316 const struct got_error *err = NULL;
1318 if (pack->privsep_child == NULL) {
1319 err = start_pack_privsep_child(pack, packidx);
1320 if (err)
1321 return err;
1324 return request_packed_blob(outbuf, size, hdrlen, outfd, pack, packidx,
1325 idx, id);
1328 static const struct got_error *
1329 request_blob(uint8_t **outbuf, size_t *size, size_t *hdrlen, int outfd,
1330 int infd, struct got_object_id *id, struct imsgbuf *ibuf)
1332 const struct got_error *err = NULL;
1333 int outfd_child;
1335 outfd_child = dup(outfd);
1336 if (outfd_child == -1)
1337 return got_error_from_errno("dup");
1339 err = got_privsep_send_blob_req(ibuf, infd, id, -1);
1340 if (err)
1341 return err;
1343 err = got_privsep_send_blob_outfd(ibuf, outfd_child);
1344 if (err)
1345 return err;
1347 err = got_privsep_recv_blob(outbuf, size, hdrlen, ibuf);
1348 if (err)
1349 return err;
1351 if (lseek(outfd, SEEK_SET, 0) == -1)
1352 return got_error_from_errno("lseek");
1354 return err;
1357 static const struct got_error *
1358 read_blob_privsep(uint8_t **outbuf, size_t *size, size_t *hdrlen,
1359 int outfd, int infd, struct got_object_id *id, struct got_repository *repo)
1361 const struct got_error *err;
1362 int imsg_fds[2];
1363 pid_t pid;
1364 struct imsgbuf *ibuf;
1366 if (repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_BLOB].imsg_fd != -1) {
1367 ibuf = repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_BLOB].ibuf;
1368 return request_blob(outbuf, size, hdrlen, outfd, infd, id,
1369 ibuf);
1372 ibuf = calloc(1, sizeof(*ibuf));
1373 if (ibuf == NULL)
1374 return got_error_from_errno("calloc");
1376 if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_fds) == -1) {
1377 err = got_error_from_errno("socketpair");
1378 free(ibuf);
1379 return err;
1382 pid = fork();
1383 if (pid == -1) {
1384 err = got_error_from_errno("fork");
1385 free(ibuf);
1386 return err;
1388 else if (pid == 0) {
1389 got_privsep_exec_child(imsg_fds, GOT_PATH_PROG_READ_BLOB,
1390 repo->path);
1391 /* not reached */
1394 if (close(imsg_fds[1]) == -1) {
1395 err = got_error_from_errno("close");
1396 free(ibuf);
1397 return err;
1399 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_BLOB].imsg_fd =
1400 imsg_fds[0];
1401 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_BLOB].pid = pid;
1402 imsg_init(ibuf, imsg_fds[0]);
1403 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_BLOB].ibuf = ibuf;
1405 return request_blob(outbuf, size, hdrlen, outfd, infd, id, ibuf);
1408 static const struct got_error *
1409 open_blob(struct got_blob_object **blob, struct got_repository *repo,
1410 struct got_object_id *id, size_t blocksize)
1412 const struct got_error *err = NULL;
1413 struct got_packidx *packidx = NULL;
1414 int idx;
1415 char *path_packfile = NULL;
1416 uint8_t *outbuf;
1417 int outfd;
1418 size_t size, hdrlen;
1419 struct stat sb;
1421 *blob = calloc(1, sizeof(**blob));
1422 if (*blob == NULL)
1423 return got_error_from_errno("calloc");
1425 outfd = got_opentempfd();
1426 if (outfd == -1)
1427 return got_error_from_errno("got_opentempfd");
1429 (*blob)->read_buf = malloc(blocksize);
1430 if ((*blob)->read_buf == NULL) {
1431 err = got_error_from_errno("malloc");
1432 goto done;
1435 err = got_repo_search_packidx(&packidx, &idx, repo, id);
1436 if (err == NULL) {
1437 struct got_pack *pack = NULL;
1439 err = got_packidx_get_packfile_path(&path_packfile,
1440 packidx->path_packidx);
1441 if (err)
1442 goto done;
1444 pack = got_repo_get_cached_pack(repo, path_packfile);
1445 if (pack == NULL) {
1446 err = got_repo_cache_pack(&pack, repo, path_packfile,
1447 packidx);
1448 if (err)
1449 goto done;
1451 err = read_packed_blob_privsep(&outbuf, &size, &hdrlen, outfd,
1452 pack, packidx, idx, id);
1453 } else if (err->code == GOT_ERR_NO_OBJ) {
1454 int infd;
1456 err = got_object_open_loose_fd(&infd, id, repo);
1457 if (err)
1458 goto done;
1459 err = read_blob_privsep(&outbuf, &size, &hdrlen, outfd, infd,
1460 id, repo);
1462 if (err)
1463 goto done;
1465 if (hdrlen > size) {
1466 err = got_error(GOT_ERR_BAD_OBJ_HDR);
1467 goto done;
1470 if (outbuf) {
1471 if (close(outfd) == -1 && err == NULL)
1472 err = got_error_from_errno("close");
1473 outfd = -1;
1474 (*blob)->f = fmemopen(outbuf, size, "rb");
1475 if ((*blob)->f == NULL) {
1476 err = got_error_from_errno("fmemopen");
1477 free(outbuf);
1478 goto done;
1480 (*blob)->data = outbuf;
1481 } else {
1482 if (fstat(outfd, &sb) == -1) {
1483 err = got_error_from_errno("fstat");
1484 goto done;
1487 if (sb.st_size != size) {
1488 err = got_error(GOT_ERR_PRIVSEP_LEN);
1489 goto done;
1492 (*blob)->f = fdopen(outfd, "rb");
1493 if ((*blob)->f == NULL) {
1494 err = got_error_from_errno("fdopen");
1495 close(outfd);
1496 outfd = -1;
1497 goto done;
1501 (*blob)->hdrlen = hdrlen;
1502 (*blob)->blocksize = blocksize;
1503 memcpy(&(*blob)->id.sha1, id->sha1, SHA1_DIGEST_LENGTH);
1505 done:
1506 free(path_packfile);
1507 if (err) {
1508 if (*blob) {
1509 got_object_blob_close(*blob);
1510 *blob = NULL;
1511 } else if (outfd != -1)
1512 close(outfd);
1514 return err;
1517 const struct got_error *
1518 got_object_open_as_blob(struct got_blob_object **blob,
1519 struct got_repository *repo, struct got_object_id *id,
1520 size_t blocksize)
1522 return open_blob(blob, repo, id, blocksize);
1525 const struct got_error *
1526 got_object_blob_open(struct got_blob_object **blob,
1527 struct got_repository *repo, struct got_object *obj, size_t blocksize)
1529 return open_blob(blob, repo, got_object_get_id(obj), blocksize);
1532 const struct got_error *
1533 got_object_blob_close(struct got_blob_object *blob)
1535 const struct got_error *err = NULL;
1536 free(blob->read_buf);
1537 if (blob->f && fclose(blob->f) == EOF)
1538 err = got_error_from_errno("fclose");
1539 free(blob->data);
1540 free(blob);
1541 return err;
1544 void
1545 got_object_blob_rewind(struct got_blob_object *blob)
1547 if (blob->f)
1548 rewind(blob->f);
1551 char *
1552 got_object_blob_id_str(struct got_blob_object *blob, char *buf, size_t size)
1554 return got_sha1_digest_to_str(blob->id.sha1, buf, size);
1557 size_t
1558 got_object_blob_get_hdrlen(struct got_blob_object *blob)
1560 return blob->hdrlen;
1563 const uint8_t *
1564 got_object_blob_get_read_buf(struct got_blob_object *blob)
1566 return blob->read_buf;
1569 const struct got_error *
1570 got_object_blob_read_block(size_t *outlenp, struct got_blob_object *blob)
1572 size_t n;
1574 n = fread(blob->read_buf, 1, blob->blocksize, blob->f);
1575 if (n == 0 && ferror(blob->f))
1576 return got_ferror(blob->f, GOT_ERR_IO);
1577 *outlenp = n;
1578 return NULL;
1581 const struct got_error *
1582 got_object_blob_dump_to_file(off_t *filesize, int *nlines,
1583 off_t **line_offsets, FILE *outfile, struct got_blob_object *blob)
1585 const struct got_error *err = NULL;
1586 size_t n, len, hdrlen;
1587 const uint8_t *buf;
1588 int i;
1589 const int alloc_chunksz = 512;
1590 size_t nalloc = 0;
1591 off_t off = 0, total_len = 0;
1593 if (line_offsets)
1594 *line_offsets = NULL;
1595 if (filesize)
1596 *filesize = 0;
1597 if (nlines)
1598 *nlines = 0;
1600 hdrlen = got_object_blob_get_hdrlen(blob);
1601 do {
1602 err = got_object_blob_read_block(&len, blob);
1603 if (err)
1604 return err;
1605 if (len == 0)
1606 break;
1607 buf = got_object_blob_get_read_buf(blob);
1608 i = hdrlen;
1609 if (nlines) {
1610 if (line_offsets && *line_offsets == NULL) {
1611 /* Have some data but perhaps no '\n'. */
1612 *nlines = 1;
1613 nalloc = alloc_chunksz;
1614 *line_offsets = calloc(nalloc,
1615 sizeof(**line_offsets));
1616 if (*line_offsets == NULL)
1617 return got_error_from_errno("calloc");
1619 /* Skip forward over end of first line. */
1620 while (i < len) {
1621 if (buf[i] == '\n')
1622 break;
1623 i++;
1626 /* Scan '\n' offsets in remaining chunk of data. */
1627 while (i < len) {
1628 if (buf[i] != '\n') {
1629 i++;
1630 continue;
1632 (*nlines)++;
1633 if (line_offsets && nalloc < *nlines) {
1634 size_t n = *nlines + alloc_chunksz;
1635 off_t *o = recallocarray(*line_offsets,
1636 nalloc, n, sizeof(**line_offsets));
1637 if (o == NULL) {
1638 free(*line_offsets);
1639 *line_offsets = NULL;
1640 return got_error_from_errno(
1641 "recallocarray");
1643 *line_offsets = o;
1644 nalloc = n;
1646 if (line_offsets) {
1647 off = total_len + i - hdrlen + 1;
1648 (*line_offsets)[*nlines - 1] = off;
1650 i++;
1653 /* Skip blob object header first time around. */
1654 n = fwrite(buf + hdrlen, 1, len - hdrlen, outfile);
1655 if (n != len - hdrlen)
1656 return got_ferror(outfile, GOT_ERR_IO);
1657 total_len += len - hdrlen;
1658 hdrlen = 0;
1659 } while (len != 0);
1661 if (fflush(outfile) != 0)
1662 return got_error_from_errno("fflush");
1663 rewind(outfile);
1665 if (filesize)
1666 *filesize = total_len;
1668 return NULL;
1671 static const struct got_error *
1672 request_packed_tag(struct got_tag_object **tag, struct got_pack *pack,
1673 int pack_idx, struct got_object_id *id)
1675 const struct got_error *err = NULL;
1677 err = got_privsep_send_tag_req(pack->privsep_child->ibuf, -1, id,
1678 pack_idx);
1679 if (err)
1680 return err;
1682 return got_privsep_recv_tag(tag, pack->privsep_child->ibuf);
1685 static const struct got_error *
1686 read_packed_tag_privsep(struct got_tag_object **tag,
1687 struct got_pack *pack, struct got_packidx *packidx, int idx,
1688 struct got_object_id *id)
1690 const struct got_error *err = NULL;
1692 if (pack->privsep_child)
1693 return request_packed_tag(tag, pack, idx, id);
1695 err = start_pack_privsep_child(pack, packidx);
1696 if (err)
1697 return err;
1699 return request_packed_tag(tag, pack, idx, id);
1702 static const struct got_error *
1703 request_tag(struct got_tag_object **tag, struct got_repository *repo,
1704 int fd, struct got_object_id *id)
1706 const struct got_error *err = NULL;
1707 struct imsgbuf *ibuf;
1709 ibuf = repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TAG].ibuf;
1711 err = got_privsep_send_tag_req(ibuf, fd, id, -1);
1712 if (err)
1713 return err;
1715 return got_privsep_recv_tag(tag, ibuf);
1718 static const struct got_error *
1719 read_tag_privsep(struct got_tag_object **tag, int obj_fd,
1720 struct got_object_id *id, struct got_repository *repo)
1722 const struct got_error *err;
1723 int imsg_fds[2];
1724 pid_t pid;
1725 struct imsgbuf *ibuf;
1727 if (repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TAG].imsg_fd != -1)
1728 return request_tag(tag, repo, obj_fd, id);
1730 ibuf = calloc(1, sizeof(*ibuf));
1731 if (ibuf == NULL)
1732 return got_error_from_errno("calloc");
1734 if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_fds) == -1) {
1735 err = got_error_from_errno("socketpair");
1736 free(ibuf);
1737 return err;
1740 pid = fork();
1741 if (pid == -1) {
1742 err = got_error_from_errno("fork");
1743 free(ibuf);
1744 return err;
1746 else if (pid == 0) {
1747 got_privsep_exec_child(imsg_fds, GOT_PATH_PROG_READ_TAG,
1748 repo->path);
1749 /* not reached */
1752 if (close(imsg_fds[1]) == -1) {
1753 err = got_error_from_errno("close");
1754 free(ibuf);
1755 return err;
1757 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TAG].imsg_fd =
1758 imsg_fds[0];
1759 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TAG].pid = pid;
1760 imsg_init(ibuf, imsg_fds[0]);
1761 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TAG].ibuf = ibuf;
1763 return request_tag(tag, repo, obj_fd, id);
1766 static const struct got_error *
1767 open_tag(struct got_tag_object **tag, struct got_repository *repo,
1768 struct got_object_id *id, int check_cache)
1770 const struct got_error *err = NULL;
1771 struct got_packidx *packidx = NULL;
1772 int idx;
1773 char *path_packfile = NULL;
1774 struct got_object *obj = NULL;
1775 int obj_type = GOT_OBJ_TYPE_ANY;
1777 if (check_cache) {
1778 *tag = got_repo_get_cached_tag(repo, id);
1779 if (*tag != NULL) {
1780 (*tag)->refcnt++;
1781 return NULL;
1783 } else
1784 *tag = NULL;
1786 err = got_repo_search_packidx(&packidx, &idx, repo, id);
1787 if (err == NULL) {
1788 struct got_pack *pack = NULL;
1790 err = got_packidx_get_packfile_path(&path_packfile,
1791 packidx->path_packidx);
1792 if (err)
1793 return err;
1795 pack = got_repo_get_cached_pack(repo, path_packfile);
1796 if (pack == NULL) {
1797 err = got_repo_cache_pack(&pack, repo, path_packfile,
1798 packidx);
1799 if (err)
1800 goto done;
1803 /* Beware of "lightweight" tags: Check object type first. */
1804 err = read_packed_object_privsep(&obj, repo, pack, packidx,
1805 idx, id);
1806 if (err)
1807 goto done;
1808 obj_type = obj->type;
1809 got_object_close(obj);
1810 if (obj_type != GOT_OBJ_TYPE_TAG) {
1811 err = got_error(GOT_ERR_OBJ_TYPE);
1812 goto done;
1814 err = read_packed_tag_privsep(tag, pack, packidx, idx, id);
1815 } else if (err->code == GOT_ERR_NO_OBJ) {
1816 int fd;
1818 err = got_object_open_loose_fd(&fd, id, repo);
1819 if (err)
1820 return err;
1821 err = got_object_read_header_privsep(&obj, id, repo, fd);
1822 if (err)
1823 return err;
1824 obj_type = obj->type;
1825 got_object_close(obj);
1826 if (obj_type != GOT_OBJ_TYPE_TAG)
1827 return got_error(GOT_ERR_OBJ_TYPE);
1829 err = got_object_open_loose_fd(&fd, id, repo);
1830 if (err)
1831 return err;
1832 err = read_tag_privsep(tag, fd, id, repo);
1835 if (err == NULL) {
1836 (*tag)->refcnt++;
1837 err = got_repo_cache_tag(repo, id, *tag);
1839 done:
1840 free(path_packfile);
1841 return err;
1844 const struct got_error *
1845 got_object_open_as_tag(struct got_tag_object **tag,
1846 struct got_repository *repo, struct got_object_id *id)
1848 *tag = got_repo_get_cached_tag(repo, id);
1849 if (*tag != NULL) {
1850 (*tag)->refcnt++;
1851 return NULL;
1854 return open_tag(tag, repo, id, 0);
1857 const struct got_error *
1858 got_object_tag_open(struct got_tag_object **tag,
1859 struct got_repository *repo, struct got_object *obj)
1861 return open_tag(tag, repo, got_object_get_id(obj), 1);
1864 const char *
1865 got_object_tag_get_name(struct got_tag_object *tag)
1867 return tag->tag;
1870 int
1871 got_object_tag_get_object_type(struct got_tag_object *tag)
1873 return tag->obj_type;
1876 struct got_object_id *
1877 got_object_tag_get_object_id(struct got_tag_object *tag)
1879 return &tag->id;
1882 time_t
1883 got_object_tag_get_tagger_time(struct got_tag_object *tag)
1885 return tag->tagger_time;
1888 time_t
1889 got_object_tag_get_tagger_gmtoff(struct got_tag_object *tag)
1891 return tag->tagger_gmtoff;
1894 const char *
1895 got_object_tag_get_tagger(struct got_tag_object *tag)
1897 return tag->tagger;
1900 const char *
1901 got_object_tag_get_message(struct got_tag_object *tag)
1903 return tag->tagmsg;
1906 static struct got_tree_entry *
1907 find_entry_by_name(struct got_tree_object *tree, const char *name, size_t len)
1909 int i;
1911 /* Note that tree entries are sorted in strncmp() order. */
1912 for (i = 0; i < tree->nentries; i++) {
1913 struct got_tree_entry *te = &tree->entries[i];
1914 int cmp = strncmp(te->name, name, len);
1915 if (cmp < 0)
1916 continue;
1917 if (cmp > 0)
1918 break;
1919 if (te->name[len] == '\0')
1920 return te;
1922 return NULL;
1925 struct got_tree_entry *
1926 got_object_tree_find_entry(struct got_tree_object *tree, const char *name)
1928 return find_entry_by_name(tree, name, strlen(name));
1931 const struct got_error *
1932 got_object_tree_find_path(struct got_object_id **id, mode_t *mode,
1933 struct got_repository *repo, struct got_tree_object *tree,
1934 const char *path)
1936 const struct got_error *err = NULL;
1937 struct got_tree_object *subtree = NULL;
1938 struct got_tree_entry *te = NULL;
1939 const char *seg, *s;
1940 size_t seglen;
1942 *id = NULL;
1944 s = path;
1945 while (s[0] == '/')
1946 s++;
1947 seg = s;
1948 seglen = 0;
1949 subtree = tree;
1950 while (*s) {
1951 struct got_tree_object *next_tree;
1953 if (*s != '/') {
1954 s++;
1955 seglen++;
1956 if (*s)
1957 continue;
1960 te = find_entry_by_name(subtree, seg, seglen);
1961 if (te == NULL) {
1962 err = got_error_path(path, GOT_ERR_NO_TREE_ENTRY);
1963 goto done;
1966 if (*s == '\0')
1967 break;
1969 seg = s + 1;
1970 seglen = 0;
1971 s++;
1972 if (*s) {
1973 err = got_object_open_as_tree(&next_tree, repo,
1974 &te->id);
1975 te = NULL;
1976 if (err)
1977 goto done;
1978 if (subtree != tree)
1979 got_object_tree_close(subtree);
1980 subtree = next_tree;
1984 if (te) {
1985 *id = got_object_id_dup(&te->id);
1986 if (*id == NULL)
1987 return got_error_from_errno("got_object_id_dup");
1988 if (mode)
1989 *mode = te->mode;
1990 } else
1991 err = got_error_path(path, GOT_ERR_NO_TREE_ENTRY);
1992 done:
1993 if (subtree && subtree != tree)
1994 got_object_tree_close(subtree);
1995 return err;
1997 const struct got_error *
1998 got_object_id_by_path(struct got_object_id **id, struct got_repository *repo,
1999 struct got_object_id *commit_id, const char *path)
2001 const struct got_error *err = NULL;
2002 struct got_commit_object *commit = NULL;
2003 struct got_tree_object *tree = NULL;
2005 *id = NULL;
2007 err = got_object_open_as_commit(&commit, repo, commit_id);
2008 if (err)
2009 goto done;
2011 /* Handle opening of root of commit's tree. */
2012 if (got_path_is_root_dir(path)) {
2013 *id = got_object_id_dup(commit->tree_id);
2014 if (*id == NULL)
2015 err = got_error_from_errno("got_object_id_dup");
2016 } else {
2017 err = got_object_open_as_tree(&tree, repo, commit->tree_id);
2018 if (err)
2019 goto done;
2020 err = got_object_tree_find_path(id, NULL, repo, tree, path);
2022 done:
2023 if (commit)
2024 got_object_commit_close(commit);
2025 if (tree)
2026 got_object_tree_close(tree);
2027 return err;
2031 * Normalize file mode bits to avoid false positive tree entry differences
2032 * in case tree entries have unexpected mode bits set.
2034 static mode_t
2035 normalize_mode_for_comparison(mode_t mode)
2038 * For directories, the only relevant bit is the IFDIR bit.
2039 * This allows us to detect paths changing from a directory
2040 * to a file and vice versa.
2042 if (S_ISDIR(mode))
2043 return mode & S_IFDIR;
2046 * For symlinks, the only relevant bit is the IFLNK bit.
2047 * This allows us to detect paths changing from a symlinks
2048 * to a file or directory and vice versa.
2050 if (S_ISLNK(mode))
2051 return mode & S_IFLNK;
2053 /* For files, the only change we care about is the executable bit. */
2054 return mode & S_IXUSR;
2057 const struct got_error *
2058 got_object_tree_path_changed(int *changed,
2059 struct got_tree_object *tree01, struct got_tree_object *tree02,
2060 const char *path, struct got_repository *repo)
2062 const struct got_error *err = NULL;
2063 struct got_tree_object *tree1 = NULL, *tree2 = NULL;
2064 struct got_tree_entry *te1 = NULL, *te2 = NULL;
2065 const char *seg, *s;
2066 size_t seglen;
2068 *changed = 0;
2070 /* We not do support comparing the root path. */
2071 if (got_path_is_root_dir(path))
2072 return got_error_path(path, GOT_ERR_BAD_PATH);
2074 tree1 = tree01;
2075 tree2 = tree02;
2076 s = path;
2077 while (*s == '/')
2078 s++;
2079 seg = s;
2080 seglen = 0;
2081 while (*s) {
2082 struct got_tree_object *next_tree1, *next_tree2;
2083 mode_t mode1, mode2;
2085 if (*s != '/') {
2086 s++;
2087 seglen++;
2088 if (*s)
2089 continue;
2092 te1 = find_entry_by_name(tree1, seg, seglen);
2093 if (te1 == NULL) {
2094 err = got_error(GOT_ERR_NO_OBJ);
2095 goto done;
2098 if (tree2)
2099 te2 = find_entry_by_name(tree2, seg, seglen);
2101 if (te2) {
2102 mode1 = normalize_mode_for_comparison(te1->mode);
2103 mode2 = normalize_mode_for_comparison(te2->mode);
2104 if (mode1 != mode2) {
2105 *changed = 1;
2106 goto done;
2109 if (got_object_id_cmp(&te1->id, &te2->id) == 0) {
2110 *changed = 0;
2111 goto done;
2115 if (*s == '\0') { /* final path element */
2116 *changed = 1;
2117 goto done;
2120 seg = s + 1;
2121 s++;
2122 seglen = 0;
2123 if (*s) {
2124 err = got_object_open_as_tree(&next_tree1, repo,
2125 &te1->id);
2126 te1 = NULL;
2127 if (err)
2128 goto done;
2129 if (tree1 != tree01)
2130 got_object_tree_close(tree1);
2131 tree1 = next_tree1;
2133 if (te2) {
2134 err = got_object_open_as_tree(&next_tree2, repo,
2135 &te2->id);
2136 te2 = NULL;
2137 if (err)
2138 goto done;
2139 if (tree2 != tree02)
2140 got_object_tree_close(tree2);
2141 tree2 = next_tree2;
2142 } else if (tree2) {
2143 if (tree2 != tree02)
2144 got_object_tree_close(tree2);
2145 tree2 = NULL;
2149 done:
2150 if (tree1 && tree1 != tree01)
2151 got_object_tree_close(tree1);
2152 if (tree2 && tree2 != tree02)
2153 got_object_tree_close(tree2);
2154 return err;
2157 const struct got_error *
2158 got_object_tree_entry_dup(struct got_tree_entry **new_te,
2159 struct got_tree_entry *te)
2161 const struct got_error *err = NULL;
2163 *new_te = calloc(1, sizeof(**new_te));
2164 if (*new_te == NULL)
2165 return got_error_from_errno("calloc");
2167 (*new_te)->mode = te->mode;
2168 memcpy((*new_te)->name, te->name, sizeof((*new_te)->name));
2169 memcpy(&(*new_te)->id, &te->id, sizeof((*new_te)->id));
2170 return err;
2173 int
2174 got_object_tree_entry_is_submodule(struct got_tree_entry *te)
2176 return (te->mode & S_IFMT) == (S_IFDIR | S_IFLNK);
2179 int
2180 got_object_tree_entry_is_symlink(struct got_tree_entry *te)
2182 /* S_IFDIR check avoids confusing symlinks with submodules. */
2183 return ((te->mode & (S_IFDIR | S_IFLNK)) == S_IFLNK);
2186 static const struct got_error *
2187 resolve_symlink(char **link_target, const char *path,
2188 struct got_object_id *commit_id, struct got_repository *repo)
2190 const struct got_error *err = NULL;
2191 char buf[PATH_MAX];
2192 char *name, *parent_path = NULL;
2193 struct got_object_id *tree_obj_id = NULL;
2194 struct got_tree_object *tree = NULL;
2195 struct got_tree_entry *te = NULL;
2197 *link_target = NULL;
2199 if (strlcpy(buf, path, sizeof(buf)) >= sizeof(buf))
2200 return got_error(GOT_ERR_NO_SPACE);
2202 name = basename(buf);
2203 if (name == NULL)
2204 return got_error_from_errno2("basename", path);
2206 err = got_path_dirname(&parent_path, path);
2207 if (err)
2208 return err;
2210 err = got_object_id_by_path(&tree_obj_id, repo, commit_id,
2211 parent_path);
2212 if (err) {
2213 if (err->code == GOT_ERR_NO_TREE_ENTRY) {
2214 /* Display the complete path in error message. */
2215 err = got_error_path(path, err->code);
2217 goto done;
2220 err = got_object_open_as_tree(&tree, repo, tree_obj_id);
2221 if (err)
2222 goto done;
2224 te = got_object_tree_find_entry(tree, name);
2225 if (te == NULL) {
2226 err = got_error_path(path, GOT_ERR_NO_TREE_ENTRY);
2227 goto done;
2230 if (got_object_tree_entry_is_symlink(te)) {
2231 err = got_tree_entry_get_symlink_target(link_target, te, repo);
2232 if (err)
2233 goto done;
2234 if (!got_path_is_absolute(*link_target)) {
2235 char *abspath;
2236 if (asprintf(&abspath, "%s/%s", parent_path,
2237 *link_target) == -1) {
2238 err = got_error_from_errno("asprintf");
2239 goto done;
2241 free(*link_target);
2242 *link_target = malloc(PATH_MAX);
2243 if (*link_target == NULL) {
2244 err = got_error_from_errno("malloc");
2245 goto done;
2247 err = got_canonpath(abspath, *link_target, PATH_MAX);
2248 free(abspath);
2249 if (err)
2250 goto done;
2253 done:
2254 free(tree_obj_id);
2255 if (tree)
2256 got_object_tree_close(tree);
2257 if (err) {
2258 free(*link_target);
2259 *link_target = NULL;
2261 return err;
2264 const struct got_error *
2265 got_object_resolve_symlinks(char **link_target, const char *path,
2266 struct got_object_id *commit_id, struct got_repository *repo)
2268 const struct got_error *err = NULL;
2269 char *next_target = NULL;
2270 int max_recursion = 40; /* matches Git */
2272 *link_target = NULL;
2274 do {
2275 err = resolve_symlink(&next_target,
2276 *link_target ? *link_target : path, commit_id, repo);
2277 if (err)
2278 break;
2279 if (next_target) {
2280 free(*link_target);
2281 if (--max_recursion == 0) {
2282 err = got_error_path(path, GOT_ERR_RECURSION);
2283 *link_target = NULL;
2284 break;
2286 *link_target = next_target;
2288 } while (next_target);
2290 return err;
2293 const struct got_error *
2294 got_traverse_packed_commits(struct got_object_id_queue *traversed_commits,
2295 struct got_object_id *commit_id, const char *path,
2296 struct got_repository *repo)
2298 const struct got_error *err = NULL;
2299 struct got_pack *pack = NULL;
2300 struct got_packidx *packidx = NULL;
2301 char *path_packfile = NULL;
2302 struct got_commit_object *changed_commit = NULL;
2303 struct got_object_id *changed_commit_id = NULL;
2304 int idx;
2306 err = got_repo_search_packidx(&packidx, &idx, repo, commit_id);
2307 if (err) {
2308 if (err->code != GOT_ERR_NO_OBJ)
2309 return err;
2310 return NULL;
2313 err = got_packidx_get_packfile_path(&path_packfile,
2314 packidx->path_packidx);
2315 if (err)
2316 return err;
2318 pack = got_repo_get_cached_pack(repo, path_packfile);
2319 if (pack == NULL) {
2320 err = got_repo_cache_pack(&pack, repo, path_packfile, packidx);
2321 if (err)
2322 goto done;
2325 if (pack->privsep_child == NULL) {
2326 err = start_pack_privsep_child(pack, packidx);
2327 if (err)
2328 goto done;
2331 err = got_privsep_send_commit_traversal_request(
2332 pack->privsep_child->ibuf, commit_id, idx, path);
2333 if (err)
2334 goto done;
2336 err = got_privsep_recv_traversed_commits(&changed_commit,
2337 &changed_commit_id, traversed_commits, pack->privsep_child->ibuf);
2338 if (err)
2339 goto done;
2341 if (changed_commit) {
2343 * Cache the commit in which the path was changed.
2344 * This commit might be opened again soon.
2346 changed_commit->refcnt++;
2347 err = got_repo_cache_commit(repo, changed_commit_id,
2348 changed_commit);
2349 got_object_commit_close(changed_commit);
2351 done:
2352 free(path_packfile);
2353 free(changed_commit_id);
2354 return err;