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 *qid = calloc(1, sizeof(**qid));
932 if (*qid == NULL)
933 return got_error_from_errno("calloc");
935 memcpy(&(*qid)->id, id, sizeof((*qid)->id));
936 return NULL;
939 const struct got_error *
940 got_object_id_queue_copy(const struct got_object_id_queue *src,
941 struct got_object_id_queue *dest)
943 const struct got_error *err;
944 struct got_object_qid *qid;
946 STAILQ_FOREACH(qid, src, entry) {
947 struct got_object_qid *new;
948 /*
949 * Deep-copy the object ID only. Let the caller deal
950 * with setting up the new->data pointer if needed.
951 */
952 err = got_object_qid_alloc(&new, &qid->id);
953 if (err) {
954 got_object_id_queue_free(dest);
955 return err;
957 STAILQ_INSERT_TAIL(dest, new, entry);
960 return NULL;
963 static const struct got_error *
964 request_packed_tree(struct got_tree_object **tree, struct got_pack *pack,
965 int pack_idx, struct got_object_id *id)
967 const struct got_error *err = NULL;
969 err = got_privsep_send_tree_req(pack->privsep_child->ibuf, -1, id,
970 pack_idx);
971 if (err)
972 return err;
974 return got_privsep_recv_tree(tree, pack->privsep_child->ibuf);
977 static const struct got_error *
978 read_packed_tree_privsep(struct got_tree_object **tree,
979 struct got_pack *pack, struct got_packidx *packidx, int idx,
980 struct got_object_id *id)
982 const struct got_error *err = NULL;
984 if (pack->privsep_child)
985 return request_packed_tree(tree, pack, idx, id);
987 err = start_pack_privsep_child(pack, packidx);
988 if (err)
989 return err;
991 return request_packed_tree(tree, pack, idx, id);
994 static const struct got_error *
995 request_tree(struct got_tree_object **tree, struct got_repository *repo,
996 int fd, struct got_object_id *id)
998 const struct got_error *err = NULL;
999 struct imsgbuf *ibuf;
1001 ibuf = repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TREE].ibuf;
1003 err = got_privsep_send_tree_req(ibuf, fd, id, -1);
1004 if (err)
1005 return err;
1007 return got_privsep_recv_tree(tree, ibuf);
1010 const struct got_error *
1011 read_tree_privsep(struct got_tree_object **tree, int obj_fd,
1012 struct got_object_id *id, struct got_repository *repo)
1014 const struct got_error *err;
1015 int imsg_fds[2];
1016 pid_t pid;
1017 struct imsgbuf *ibuf;
1019 if (repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TREE].imsg_fd != -1)
1020 return request_tree(tree, repo, obj_fd, id);
1022 ibuf = calloc(1, sizeof(*ibuf));
1023 if (ibuf == NULL)
1024 return got_error_from_errno("calloc");
1026 if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_fds) == -1) {
1027 err = got_error_from_errno("socketpair");
1028 free(ibuf);
1029 return err;
1032 pid = fork();
1033 if (pid == -1) {
1034 err = got_error_from_errno("fork");
1035 free(ibuf);
1036 return err;
1038 else if (pid == 0) {
1039 got_privsep_exec_child(imsg_fds, GOT_PATH_PROG_READ_TREE,
1040 repo->path);
1041 /* not reached */
1044 if (close(imsg_fds[1]) == -1) {
1045 err = got_error_from_errno("close");
1046 free(ibuf);
1047 return err;
1049 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TREE].imsg_fd =
1050 imsg_fds[0];
1051 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TREE].pid = pid;
1052 imsg_init(ibuf, imsg_fds[0]);
1053 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TREE].ibuf = ibuf;
1056 return request_tree(tree, repo, obj_fd, id);
1059 static const struct got_error *
1060 open_tree(struct got_tree_object **tree, struct got_repository *repo,
1061 struct got_object_id *id, int check_cache)
1063 const struct got_error *err = NULL;
1064 struct got_packidx *packidx = NULL;
1065 int idx;
1066 char *path_packfile = NULL;
1068 if (check_cache) {
1069 *tree = got_repo_get_cached_tree(repo, id);
1070 if (*tree != NULL) {
1071 (*tree)->refcnt++;
1072 return NULL;
1074 } else
1075 *tree = NULL;
1077 err = got_repo_search_packidx(&packidx, &idx, repo, id);
1078 if (err == NULL) {
1079 struct got_pack *pack = NULL;
1081 err = got_packidx_get_packfile_path(&path_packfile,
1082 packidx->path_packidx);
1083 if (err)
1084 return err;
1086 pack = got_repo_get_cached_pack(repo, path_packfile);
1087 if (pack == NULL) {
1088 err = got_repo_cache_pack(&pack, repo, path_packfile,
1089 packidx);
1090 if (err)
1091 goto done;
1093 err = read_packed_tree_privsep(tree, pack,
1094 packidx, idx, id);
1095 } else if (err->code == GOT_ERR_NO_OBJ) {
1096 int fd;
1098 err = got_object_open_loose_fd(&fd, id, repo);
1099 if (err)
1100 return err;
1101 err = read_tree_privsep(tree, fd, id, repo);
1104 if (err == NULL) {
1105 (*tree)->refcnt++;
1106 err = got_repo_cache_tree(repo, id, *tree);
1108 done:
1109 free(path_packfile);
1110 return err;
1113 const struct got_error *
1114 got_object_open_as_tree(struct got_tree_object **tree,
1115 struct got_repository *repo, struct got_object_id *id)
1117 *tree = got_repo_get_cached_tree(repo, id);
1118 if (*tree != NULL) {
1119 (*tree)->refcnt++;
1120 return NULL;
1123 return open_tree(tree, repo, id, 0);
1126 const struct got_error *
1127 got_object_tree_open(struct got_tree_object **tree,
1128 struct got_repository *repo, struct got_object *obj)
1130 return open_tree(tree, repo, got_object_get_id(obj), 1);
1133 int
1134 got_object_tree_get_nentries(struct got_tree_object *tree)
1136 return tree->nentries;
1139 struct got_tree_entry *
1140 got_object_tree_get_first_entry(struct got_tree_object *tree)
1142 return got_object_tree_get_entry(tree, 0);
1145 struct got_tree_entry *
1146 got_object_tree_get_last_entry(struct got_tree_object *tree)
1148 return got_object_tree_get_entry(tree, tree->nentries - 1);
1151 struct got_tree_entry *
1152 got_object_tree_get_entry(struct got_tree_object *tree, int i)
1154 if (i < 0 || i >= tree->nentries)
1155 return NULL;
1156 return &tree->entries[i];
1159 mode_t
1160 got_tree_entry_get_mode(struct got_tree_entry *te)
1162 return te->mode;
1165 const char *
1166 got_tree_entry_get_name(struct got_tree_entry *te)
1168 return &te->name[0];
1171 struct got_object_id *
1172 got_tree_entry_get_id(struct got_tree_entry *te)
1174 return &te->id;
1177 const struct got_error *
1178 got_object_blob_read_to_str(char **s, struct got_blob_object *blob)
1180 const struct got_error *err = NULL;
1181 size_t len, totlen, hdrlen, offset;
1183 *s = NULL;
1185 hdrlen = got_object_blob_get_hdrlen(blob);
1186 totlen = 0;
1187 offset = 0;
1188 do {
1189 char *p;
1191 err = got_object_blob_read_block(&len, blob);
1192 if (err)
1193 return err;
1195 if (len == 0)
1196 break;
1198 totlen += len - hdrlen;
1199 p = realloc(*s, totlen + 1);
1200 if (p == NULL) {
1201 err = got_error_from_errno("realloc");
1202 free(*s);
1203 *s = NULL;
1204 return err;
1206 *s = p;
1207 /* Skip blob object header first time around. */
1208 memcpy(*s + offset,
1209 got_object_blob_get_read_buf(blob) + hdrlen, len - hdrlen);
1210 hdrlen = 0;
1211 offset = totlen;
1212 } while (len > 0);
1214 (*s)[totlen] = '\0';
1215 return NULL;
1218 const struct got_error *
1219 got_tree_entry_get_symlink_target(char **link_target, struct got_tree_entry *te,
1220 struct got_repository *repo)
1222 const struct got_error *err = NULL;
1223 struct got_blob_object *blob = NULL;
1225 *link_target = NULL;
1227 if (!got_object_tree_entry_is_symlink(te))
1228 return got_error(GOT_ERR_TREE_ENTRY_TYPE);
1230 err = got_object_open_as_blob(&blob, repo,
1231 got_tree_entry_get_id(te), PATH_MAX);
1232 if (err)
1233 return err;
1235 err = got_object_blob_read_to_str(link_target, blob);
1236 got_object_blob_close(blob);
1237 if (err) {
1238 free(*link_target);
1239 *link_target = NULL;
1241 return err;
1244 int
1245 got_tree_entry_get_index(struct got_tree_entry *te)
1247 return te->idx;
1250 struct got_tree_entry *
1251 got_tree_entry_get_next(struct got_tree_object *tree,
1252 struct got_tree_entry *te)
1254 return got_object_tree_get_entry(tree, te->idx + 1);
1257 struct got_tree_entry *
1258 got_tree_entry_get_prev(struct got_tree_object *tree,
1259 struct got_tree_entry *te)
1261 return got_object_tree_get_entry(tree, te->idx - 1);
1264 static const struct got_error *
1265 request_packed_blob(uint8_t **outbuf, size_t *size, size_t *hdrlen, int outfd,
1266 struct got_pack *pack, struct got_packidx *packidx, int idx,
1267 struct got_object_id *id)
1269 const struct got_error *err = NULL;
1270 struct imsgbuf *ibuf = pack->privsep_child->ibuf;
1271 int outfd_child;
1273 err = pack_child_send_tempfiles(ibuf, pack);
1274 if (err)
1275 return err;
1277 outfd_child = dup(outfd);
1278 if (outfd_child == -1)
1279 return got_error_from_errno("dup");
1281 err = got_privsep_send_blob_req(pack->privsep_child->ibuf, -1, id, idx);
1282 if (err)
1283 return err;
1285 err = got_privsep_send_blob_outfd(pack->privsep_child->ibuf,
1286 outfd_child);
1287 if (err) {
1288 return err;
1291 err = got_privsep_recv_blob(outbuf, size, hdrlen,
1292 pack->privsep_child->ibuf);
1293 if (err)
1294 return err;
1296 if (lseek(outfd, SEEK_SET, 0) == -1)
1297 err = got_error_from_errno("lseek");
1299 return err;
1302 static const struct got_error *
1303 read_packed_blob_privsep(uint8_t **outbuf, size_t *size, size_t *hdrlen,
1304 int outfd, struct got_pack *pack, struct got_packidx *packidx, int idx,
1305 struct got_object_id *id)
1307 const struct got_error *err = NULL;
1309 if (pack->privsep_child == NULL) {
1310 err = start_pack_privsep_child(pack, packidx);
1311 if (err)
1312 return err;
1315 return request_packed_blob(outbuf, size, hdrlen, outfd, pack, packidx,
1316 idx, id);
1319 static const struct got_error *
1320 request_blob(uint8_t **outbuf, size_t *size, size_t *hdrlen, int outfd,
1321 int infd, struct got_object_id *id, struct imsgbuf *ibuf)
1323 const struct got_error *err = NULL;
1324 int outfd_child;
1326 outfd_child = dup(outfd);
1327 if (outfd_child == -1)
1328 return got_error_from_errno("dup");
1330 err = got_privsep_send_blob_req(ibuf, infd, id, -1);
1331 if (err)
1332 return err;
1334 err = got_privsep_send_blob_outfd(ibuf, outfd_child);
1335 if (err)
1336 return err;
1338 err = got_privsep_recv_blob(outbuf, size, hdrlen, ibuf);
1339 if (err)
1340 return err;
1342 if (lseek(outfd, SEEK_SET, 0) == -1)
1343 return got_error_from_errno("lseek");
1345 return err;
1348 static const struct got_error *
1349 read_blob_privsep(uint8_t **outbuf, size_t *size, size_t *hdrlen,
1350 int outfd, int infd, struct got_object_id *id, struct got_repository *repo)
1352 const struct got_error *err;
1353 int imsg_fds[2];
1354 pid_t pid;
1355 struct imsgbuf *ibuf;
1357 if (repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_BLOB].imsg_fd != -1) {
1358 ibuf = repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_BLOB].ibuf;
1359 return request_blob(outbuf, size, hdrlen, outfd, infd, id,
1360 ibuf);
1363 ibuf = calloc(1, sizeof(*ibuf));
1364 if (ibuf == NULL)
1365 return got_error_from_errno("calloc");
1367 if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_fds) == -1) {
1368 err = got_error_from_errno("socketpair");
1369 free(ibuf);
1370 return err;
1373 pid = fork();
1374 if (pid == -1) {
1375 err = got_error_from_errno("fork");
1376 free(ibuf);
1377 return err;
1379 else if (pid == 0) {
1380 got_privsep_exec_child(imsg_fds, GOT_PATH_PROG_READ_BLOB,
1381 repo->path);
1382 /* not reached */
1385 if (close(imsg_fds[1]) == -1) {
1386 err = got_error_from_errno("close");
1387 free(ibuf);
1388 return err;
1390 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_BLOB].imsg_fd =
1391 imsg_fds[0];
1392 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_BLOB].pid = pid;
1393 imsg_init(ibuf, imsg_fds[0]);
1394 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_BLOB].ibuf = ibuf;
1396 return request_blob(outbuf, size, hdrlen, outfd, infd, id, ibuf);
1399 static const struct got_error *
1400 open_blob(struct got_blob_object **blob, struct got_repository *repo,
1401 struct got_object_id *id, size_t blocksize)
1403 const struct got_error *err = NULL;
1404 struct got_packidx *packidx = NULL;
1405 int idx;
1406 char *path_packfile = NULL;
1407 uint8_t *outbuf;
1408 int outfd;
1409 size_t size, hdrlen;
1410 struct stat sb;
1412 *blob = calloc(1, sizeof(**blob));
1413 if (*blob == NULL)
1414 return got_error_from_errno("calloc");
1416 outfd = got_opentempfd();
1417 if (outfd == -1)
1418 return got_error_from_errno("got_opentempfd");
1420 (*blob)->read_buf = malloc(blocksize);
1421 if ((*blob)->read_buf == NULL) {
1422 err = got_error_from_errno("malloc");
1423 goto done;
1426 err = got_repo_search_packidx(&packidx, &idx, repo, id);
1427 if (err == NULL) {
1428 struct got_pack *pack = NULL;
1430 err = got_packidx_get_packfile_path(&path_packfile,
1431 packidx->path_packidx);
1432 if (err)
1433 goto done;
1435 pack = got_repo_get_cached_pack(repo, path_packfile);
1436 if (pack == NULL) {
1437 err = got_repo_cache_pack(&pack, repo, path_packfile,
1438 packidx);
1439 if (err)
1440 goto done;
1442 err = read_packed_blob_privsep(&outbuf, &size, &hdrlen, outfd,
1443 pack, packidx, idx, id);
1444 } else if (err->code == GOT_ERR_NO_OBJ) {
1445 int infd;
1447 err = got_object_open_loose_fd(&infd, id, repo);
1448 if (err)
1449 goto done;
1450 err = read_blob_privsep(&outbuf, &size, &hdrlen, outfd, infd,
1451 id, repo);
1453 if (err)
1454 goto done;
1456 if (hdrlen > size) {
1457 err = got_error(GOT_ERR_BAD_OBJ_HDR);
1458 goto done;
1461 if (outbuf) {
1462 if (close(outfd) == -1 && err == NULL)
1463 err = got_error_from_errno("close");
1464 outfd = -1;
1465 (*blob)->f = fmemopen(outbuf, size, "rb");
1466 if ((*blob)->f == NULL) {
1467 err = got_error_from_errno("fmemopen");
1468 free(outbuf);
1469 goto done;
1471 (*blob)->data = outbuf;
1472 } else {
1473 if (fstat(outfd, &sb) == -1) {
1474 err = got_error_from_errno("fstat");
1475 goto done;
1478 if (sb.st_size != size) {
1479 err = got_error(GOT_ERR_PRIVSEP_LEN);
1480 goto done;
1483 (*blob)->f = fdopen(outfd, "rb");
1484 if ((*blob)->f == NULL) {
1485 err = got_error_from_errno("fdopen");
1486 close(outfd);
1487 outfd = -1;
1488 goto done;
1492 (*blob)->hdrlen = hdrlen;
1493 (*blob)->blocksize = blocksize;
1494 memcpy(&(*blob)->id.sha1, id->sha1, SHA1_DIGEST_LENGTH);
1496 done:
1497 free(path_packfile);
1498 if (err) {
1499 if (*blob) {
1500 got_object_blob_close(*blob);
1501 *blob = NULL;
1502 } else if (outfd != -1)
1503 close(outfd);
1505 return err;
1508 const struct got_error *
1509 got_object_open_as_blob(struct got_blob_object **blob,
1510 struct got_repository *repo, struct got_object_id *id,
1511 size_t blocksize)
1513 return open_blob(blob, repo, id, blocksize);
1516 const struct got_error *
1517 got_object_blob_open(struct got_blob_object **blob,
1518 struct got_repository *repo, struct got_object *obj, size_t blocksize)
1520 return open_blob(blob, repo, got_object_get_id(obj), blocksize);
1523 const struct got_error *
1524 got_object_blob_close(struct got_blob_object *blob)
1526 const struct got_error *err = NULL;
1527 free(blob->read_buf);
1528 if (blob->f && fclose(blob->f) == EOF)
1529 err = got_error_from_errno("fclose");
1530 free(blob->data);
1531 free(blob);
1532 return err;
1535 void
1536 got_object_blob_rewind(struct got_blob_object *blob)
1538 if (blob->f)
1539 rewind(blob->f);
1542 char *
1543 got_object_blob_id_str(struct got_blob_object *blob, char *buf, size_t size)
1545 return got_sha1_digest_to_str(blob->id.sha1, buf, size);
1548 size_t
1549 got_object_blob_get_hdrlen(struct got_blob_object *blob)
1551 return blob->hdrlen;
1554 const uint8_t *
1555 got_object_blob_get_read_buf(struct got_blob_object *blob)
1557 return blob->read_buf;
1560 const struct got_error *
1561 got_object_blob_read_block(size_t *outlenp, struct got_blob_object *blob)
1563 size_t n;
1565 n = fread(blob->read_buf, 1, blob->blocksize, blob->f);
1566 if (n == 0 && ferror(blob->f))
1567 return got_ferror(blob->f, GOT_ERR_IO);
1568 *outlenp = n;
1569 return NULL;
1572 const struct got_error *
1573 got_object_blob_dump_to_file(off_t *filesize, int *nlines,
1574 off_t **line_offsets, FILE *outfile, struct got_blob_object *blob)
1576 const struct got_error *err = NULL;
1577 size_t n, len, hdrlen;
1578 const uint8_t *buf;
1579 int i;
1580 const int alloc_chunksz = 512;
1581 size_t nalloc = 0;
1582 off_t off = 0, total_len = 0;
1584 if (line_offsets)
1585 *line_offsets = NULL;
1586 if (filesize)
1587 *filesize = 0;
1588 if (nlines)
1589 *nlines = 0;
1591 hdrlen = got_object_blob_get_hdrlen(blob);
1592 do {
1593 err = got_object_blob_read_block(&len, blob);
1594 if (err)
1595 return err;
1596 if (len == 0)
1597 break;
1598 buf = got_object_blob_get_read_buf(blob);
1599 i = hdrlen;
1600 if (nlines) {
1601 if (line_offsets && *line_offsets == NULL) {
1602 /* Have some data but perhaps no '\n'. */
1603 *nlines = 1;
1604 nalloc = alloc_chunksz;
1605 *line_offsets = calloc(nalloc,
1606 sizeof(**line_offsets));
1607 if (*line_offsets == NULL)
1608 return got_error_from_errno("calloc");
1610 /* Skip forward over end of first line. */
1611 while (i < len) {
1612 if (buf[i] == '\n')
1613 break;
1614 i++;
1617 /* Scan '\n' offsets in remaining chunk of data. */
1618 while (i < len) {
1619 if (buf[i] != '\n') {
1620 i++;
1621 continue;
1623 (*nlines)++;
1624 if (line_offsets && nalloc < *nlines) {
1625 size_t n = *nlines + alloc_chunksz;
1626 off_t *o = recallocarray(*line_offsets,
1627 nalloc, n, sizeof(**line_offsets));
1628 if (o == NULL) {
1629 free(*line_offsets);
1630 *line_offsets = NULL;
1631 return got_error_from_errno(
1632 "recallocarray");
1634 *line_offsets = o;
1635 nalloc = n;
1637 if (line_offsets) {
1638 off = total_len + i - hdrlen + 1;
1639 (*line_offsets)[*nlines - 1] = off;
1641 i++;
1644 /* Skip blob object header first time around. */
1645 n = fwrite(buf + hdrlen, 1, len - hdrlen, outfile);
1646 if (n != len - hdrlen)
1647 return got_ferror(outfile, GOT_ERR_IO);
1648 total_len += len - hdrlen;
1649 hdrlen = 0;
1650 } while (len != 0);
1652 if (fflush(outfile) != 0)
1653 return got_error_from_errno("fflush");
1654 rewind(outfile);
1656 if (filesize)
1657 *filesize = total_len;
1659 return NULL;
1662 static const struct got_error *
1663 request_packed_tag(struct got_tag_object **tag, struct got_pack *pack,
1664 int pack_idx, struct got_object_id *id)
1666 const struct got_error *err = NULL;
1668 err = got_privsep_send_tag_req(pack->privsep_child->ibuf, -1, id,
1669 pack_idx);
1670 if (err)
1671 return err;
1673 return got_privsep_recv_tag(tag, pack->privsep_child->ibuf);
1676 static const struct got_error *
1677 read_packed_tag_privsep(struct got_tag_object **tag,
1678 struct got_pack *pack, struct got_packidx *packidx, int idx,
1679 struct got_object_id *id)
1681 const struct got_error *err = NULL;
1683 if (pack->privsep_child)
1684 return request_packed_tag(tag, pack, idx, id);
1686 err = start_pack_privsep_child(pack, packidx);
1687 if (err)
1688 return err;
1690 return request_packed_tag(tag, pack, idx, id);
1693 static const struct got_error *
1694 request_tag(struct got_tag_object **tag, struct got_repository *repo,
1695 int fd, struct got_object_id *id)
1697 const struct got_error *err = NULL;
1698 struct imsgbuf *ibuf;
1700 ibuf = repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TAG].ibuf;
1702 err = got_privsep_send_tag_req(ibuf, fd, id, -1);
1703 if (err)
1704 return err;
1706 return got_privsep_recv_tag(tag, ibuf);
1709 static const struct got_error *
1710 read_tag_privsep(struct got_tag_object **tag, int obj_fd,
1711 struct got_object_id *id, struct got_repository *repo)
1713 const struct got_error *err;
1714 int imsg_fds[2];
1715 pid_t pid;
1716 struct imsgbuf *ibuf;
1718 if (repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TAG].imsg_fd != -1)
1719 return request_tag(tag, repo, obj_fd, id);
1721 ibuf = calloc(1, sizeof(*ibuf));
1722 if (ibuf == NULL)
1723 return got_error_from_errno("calloc");
1725 if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_fds) == -1) {
1726 err = got_error_from_errno("socketpair");
1727 free(ibuf);
1728 return err;
1731 pid = fork();
1732 if (pid == -1) {
1733 err = got_error_from_errno("fork");
1734 free(ibuf);
1735 return err;
1737 else if (pid == 0) {
1738 got_privsep_exec_child(imsg_fds, GOT_PATH_PROG_READ_TAG,
1739 repo->path);
1740 /* not reached */
1743 if (close(imsg_fds[1]) == -1) {
1744 err = got_error_from_errno("close");
1745 free(ibuf);
1746 return err;
1748 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TAG].imsg_fd =
1749 imsg_fds[0];
1750 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TAG].pid = pid;
1751 imsg_init(ibuf, imsg_fds[0]);
1752 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TAG].ibuf = ibuf;
1754 return request_tag(tag, repo, obj_fd, id);
1757 static const struct got_error *
1758 open_tag(struct got_tag_object **tag, struct got_repository *repo,
1759 struct got_object_id *id, int check_cache)
1761 const struct got_error *err = NULL;
1762 struct got_packidx *packidx = NULL;
1763 int idx;
1764 char *path_packfile = NULL;
1765 struct got_object *obj = NULL;
1766 int obj_type = GOT_OBJ_TYPE_ANY;
1768 if (check_cache) {
1769 *tag = got_repo_get_cached_tag(repo, id);
1770 if (*tag != NULL) {
1771 (*tag)->refcnt++;
1772 return NULL;
1774 } else
1775 *tag = NULL;
1777 err = got_repo_search_packidx(&packidx, &idx, repo, id);
1778 if (err == NULL) {
1779 struct got_pack *pack = NULL;
1781 err = got_packidx_get_packfile_path(&path_packfile,
1782 packidx->path_packidx);
1783 if (err)
1784 return err;
1786 pack = got_repo_get_cached_pack(repo, path_packfile);
1787 if (pack == NULL) {
1788 err = got_repo_cache_pack(&pack, repo, path_packfile,
1789 packidx);
1790 if (err)
1791 goto done;
1794 /* Beware of "lightweight" tags: Check object type first. */
1795 err = read_packed_object_privsep(&obj, repo, pack, packidx,
1796 idx, id);
1797 if (err)
1798 goto done;
1799 obj_type = obj->type;
1800 got_object_close(obj);
1801 if (obj_type != GOT_OBJ_TYPE_TAG) {
1802 err = got_error(GOT_ERR_OBJ_TYPE);
1803 goto done;
1805 err = read_packed_tag_privsep(tag, pack, packidx, idx, id);
1806 } else if (err->code == GOT_ERR_NO_OBJ) {
1807 int fd;
1809 err = got_object_open_loose_fd(&fd, id, repo);
1810 if (err)
1811 return err;
1812 err = got_object_read_header_privsep(&obj, id, repo, fd);
1813 if (err)
1814 return err;
1815 obj_type = obj->type;
1816 got_object_close(obj);
1817 if (obj_type != GOT_OBJ_TYPE_TAG)
1818 return got_error(GOT_ERR_OBJ_TYPE);
1820 err = got_object_open_loose_fd(&fd, id, repo);
1821 if (err)
1822 return err;
1823 err = read_tag_privsep(tag, fd, id, repo);
1826 if (err == NULL) {
1827 (*tag)->refcnt++;
1828 err = got_repo_cache_tag(repo, id, *tag);
1830 done:
1831 free(path_packfile);
1832 return err;
1835 const struct got_error *
1836 got_object_open_as_tag(struct got_tag_object **tag,
1837 struct got_repository *repo, struct got_object_id *id)
1839 *tag = got_repo_get_cached_tag(repo, id);
1840 if (*tag != NULL) {
1841 (*tag)->refcnt++;
1842 return NULL;
1845 return open_tag(tag, repo, id, 0);
1848 const struct got_error *
1849 got_object_tag_open(struct got_tag_object **tag,
1850 struct got_repository *repo, struct got_object *obj)
1852 return open_tag(tag, repo, got_object_get_id(obj), 1);
1855 const char *
1856 got_object_tag_get_name(struct got_tag_object *tag)
1858 return tag->tag;
1861 int
1862 got_object_tag_get_object_type(struct got_tag_object *tag)
1864 return tag->obj_type;
1867 struct got_object_id *
1868 got_object_tag_get_object_id(struct got_tag_object *tag)
1870 return &tag->id;
1873 time_t
1874 got_object_tag_get_tagger_time(struct got_tag_object *tag)
1876 return tag->tagger_time;
1879 time_t
1880 got_object_tag_get_tagger_gmtoff(struct got_tag_object *tag)
1882 return tag->tagger_gmtoff;
1885 const char *
1886 got_object_tag_get_tagger(struct got_tag_object *tag)
1888 return tag->tagger;
1891 const char *
1892 got_object_tag_get_message(struct got_tag_object *tag)
1894 return tag->tagmsg;
1897 static struct got_tree_entry *
1898 find_entry_by_name(struct got_tree_object *tree, const char *name, size_t len)
1900 int i;
1902 /* Note that tree entries are sorted in strncmp() order. */
1903 for (i = 0; i < tree->nentries; i++) {
1904 struct got_tree_entry *te = &tree->entries[i];
1905 int cmp = strncmp(te->name, name, len);
1906 if (cmp < 0)
1907 continue;
1908 if (cmp > 0)
1909 break;
1910 if (te->name[len] == '\0')
1911 return te;
1913 return NULL;
1916 struct got_tree_entry *
1917 got_object_tree_find_entry(struct got_tree_object *tree, const char *name)
1919 return find_entry_by_name(tree, name, strlen(name));
1922 const struct got_error *
1923 got_object_tree_find_path(struct got_object_id **id, mode_t *mode,
1924 struct got_repository *repo, struct got_tree_object *tree,
1925 const char *path)
1927 const struct got_error *err = NULL;
1928 struct got_tree_object *subtree = NULL;
1929 struct got_tree_entry *te = NULL;
1930 const char *seg, *s;
1931 size_t seglen;
1933 *id = NULL;
1935 s = path;
1936 while (s[0] == '/')
1937 s++;
1938 seg = s;
1939 seglen = 0;
1940 subtree = tree;
1941 while (*s) {
1942 struct got_tree_object *next_tree;
1944 if (*s != '/') {
1945 s++;
1946 seglen++;
1947 if (*s)
1948 continue;
1951 te = find_entry_by_name(subtree, seg, seglen);
1952 if (te == NULL) {
1953 err = got_error_path(path, GOT_ERR_NO_TREE_ENTRY);
1954 goto done;
1957 if (*s == '\0')
1958 break;
1960 seg = s + 1;
1961 seglen = 0;
1962 s++;
1963 if (*s) {
1964 err = got_object_open_as_tree(&next_tree, repo,
1965 &te->id);
1966 te = NULL;
1967 if (err)
1968 goto done;
1969 if (subtree != tree)
1970 got_object_tree_close(subtree);
1971 subtree = next_tree;
1975 if (te) {
1976 *id = got_object_id_dup(&te->id);
1977 if (*id == NULL)
1978 return got_error_from_errno("got_object_id_dup");
1979 if (mode)
1980 *mode = te->mode;
1981 } else
1982 err = got_error_path(path, GOT_ERR_NO_TREE_ENTRY);
1983 done:
1984 if (subtree && subtree != tree)
1985 got_object_tree_close(subtree);
1986 return err;
1988 const struct got_error *
1989 got_object_id_by_path(struct got_object_id **id, struct got_repository *repo,
1990 struct got_commit_object *commit, const char *path)
1992 const struct got_error *err = NULL;
1993 struct got_tree_object *tree = NULL;
1995 *id = NULL;
1997 /* Handle opening of root of commit's tree. */
1998 if (got_path_is_root_dir(path)) {
1999 *id = got_object_id_dup(commit->tree_id);
2000 if (*id == NULL)
2001 err = got_error_from_errno("got_object_id_dup");
2002 } else {
2003 err = got_object_open_as_tree(&tree, repo, commit->tree_id);
2004 if (err)
2005 goto done;
2006 err = got_object_tree_find_path(id, NULL, repo, tree, path);
2008 done:
2009 if (tree)
2010 got_object_tree_close(tree);
2011 return err;
2015 * Normalize file mode bits to avoid false positive tree entry differences
2016 * in case tree entries have unexpected mode bits set.
2018 static mode_t
2019 normalize_mode_for_comparison(mode_t mode)
2022 * For directories, the only relevant bit is the IFDIR bit.
2023 * This allows us to detect paths changing from a directory
2024 * to a file and vice versa.
2026 if (S_ISDIR(mode))
2027 return mode & S_IFDIR;
2030 * For symlinks, the only relevant bit is the IFLNK bit.
2031 * This allows us to detect paths changing from a symlinks
2032 * to a file or directory and vice versa.
2034 if (S_ISLNK(mode))
2035 return mode & S_IFLNK;
2037 /* For files, the only change we care about is the executable bit. */
2038 return mode & S_IXUSR;
2041 const struct got_error *
2042 got_object_tree_path_changed(int *changed,
2043 struct got_tree_object *tree01, struct got_tree_object *tree02,
2044 const char *path, struct got_repository *repo)
2046 const struct got_error *err = NULL;
2047 struct got_tree_object *tree1 = NULL, *tree2 = NULL;
2048 struct got_tree_entry *te1 = NULL, *te2 = NULL;
2049 const char *seg, *s;
2050 size_t seglen;
2052 *changed = 0;
2054 /* We not do support comparing the root path. */
2055 if (got_path_is_root_dir(path))
2056 return got_error_path(path, GOT_ERR_BAD_PATH);
2058 tree1 = tree01;
2059 tree2 = tree02;
2060 s = path;
2061 while (*s == '/')
2062 s++;
2063 seg = s;
2064 seglen = 0;
2065 while (*s) {
2066 struct got_tree_object *next_tree1, *next_tree2;
2067 mode_t mode1, mode2;
2069 if (*s != '/') {
2070 s++;
2071 seglen++;
2072 if (*s)
2073 continue;
2076 te1 = find_entry_by_name(tree1, seg, seglen);
2077 if (te1 == NULL) {
2078 err = got_error(GOT_ERR_NO_OBJ);
2079 goto done;
2082 if (tree2)
2083 te2 = find_entry_by_name(tree2, seg, seglen);
2085 if (te2) {
2086 mode1 = normalize_mode_for_comparison(te1->mode);
2087 mode2 = normalize_mode_for_comparison(te2->mode);
2088 if (mode1 != mode2) {
2089 *changed = 1;
2090 goto done;
2093 if (got_object_id_cmp(&te1->id, &te2->id) == 0) {
2094 *changed = 0;
2095 goto done;
2099 if (*s == '\0') { /* final path element */
2100 *changed = 1;
2101 goto done;
2104 seg = s + 1;
2105 s++;
2106 seglen = 0;
2107 if (*s) {
2108 err = got_object_open_as_tree(&next_tree1, repo,
2109 &te1->id);
2110 te1 = NULL;
2111 if (err)
2112 goto done;
2113 if (tree1 != tree01)
2114 got_object_tree_close(tree1);
2115 tree1 = next_tree1;
2117 if (te2) {
2118 err = got_object_open_as_tree(&next_tree2, repo,
2119 &te2->id);
2120 te2 = NULL;
2121 if (err)
2122 goto done;
2123 if (tree2 != tree02)
2124 got_object_tree_close(tree2);
2125 tree2 = next_tree2;
2126 } else if (tree2) {
2127 if (tree2 != tree02)
2128 got_object_tree_close(tree2);
2129 tree2 = NULL;
2133 done:
2134 if (tree1 && tree1 != tree01)
2135 got_object_tree_close(tree1);
2136 if (tree2 && tree2 != tree02)
2137 got_object_tree_close(tree2);
2138 return err;
2141 const struct got_error *
2142 got_object_tree_entry_dup(struct got_tree_entry **new_te,
2143 struct got_tree_entry *te)
2145 const struct got_error *err = NULL;
2147 *new_te = calloc(1, sizeof(**new_te));
2148 if (*new_te == NULL)
2149 return got_error_from_errno("calloc");
2151 (*new_te)->mode = te->mode;
2152 memcpy((*new_te)->name, te->name, sizeof((*new_te)->name));
2153 memcpy(&(*new_te)->id, &te->id, sizeof((*new_te)->id));
2154 return err;
2157 int
2158 got_object_tree_entry_is_submodule(struct got_tree_entry *te)
2160 return (te->mode & S_IFMT) == (S_IFDIR | S_IFLNK);
2163 int
2164 got_object_tree_entry_is_symlink(struct got_tree_entry *te)
2166 /* S_IFDIR check avoids confusing symlinks with submodules. */
2167 return ((te->mode & (S_IFDIR | S_IFLNK)) == S_IFLNK);
2170 static const struct got_error *
2171 resolve_symlink(char **link_target, const char *path,
2172 struct got_commit_object *commit, struct got_repository *repo)
2174 const struct got_error *err = NULL;
2175 char buf[PATH_MAX];
2176 char *name, *parent_path = NULL;
2177 struct got_object_id *tree_obj_id = NULL;
2178 struct got_tree_object *tree = NULL;
2179 struct got_tree_entry *te = NULL;
2181 *link_target = NULL;
2183 if (strlcpy(buf, path, sizeof(buf)) >= sizeof(buf))
2184 return got_error(GOT_ERR_NO_SPACE);
2186 name = basename(buf);
2187 if (name == NULL)
2188 return got_error_from_errno2("basename", path);
2190 err = got_path_dirname(&parent_path, path);
2191 if (err)
2192 return err;
2194 err = got_object_id_by_path(&tree_obj_id, repo, commit,
2195 parent_path);
2196 if (err) {
2197 if (err->code == GOT_ERR_NO_TREE_ENTRY) {
2198 /* Display the complete path in error message. */
2199 err = got_error_path(path, err->code);
2201 goto done;
2204 err = got_object_open_as_tree(&tree, repo, tree_obj_id);
2205 if (err)
2206 goto done;
2208 te = got_object_tree_find_entry(tree, name);
2209 if (te == NULL) {
2210 err = got_error_path(path, GOT_ERR_NO_TREE_ENTRY);
2211 goto done;
2214 if (got_object_tree_entry_is_symlink(te)) {
2215 err = got_tree_entry_get_symlink_target(link_target, te, repo);
2216 if (err)
2217 goto done;
2218 if (!got_path_is_absolute(*link_target)) {
2219 char *abspath;
2220 if (asprintf(&abspath, "%s/%s", parent_path,
2221 *link_target) == -1) {
2222 err = got_error_from_errno("asprintf");
2223 goto done;
2225 free(*link_target);
2226 *link_target = malloc(PATH_MAX);
2227 if (*link_target == NULL) {
2228 err = got_error_from_errno("malloc");
2229 goto done;
2231 err = got_canonpath(abspath, *link_target, PATH_MAX);
2232 free(abspath);
2233 if (err)
2234 goto done;
2237 done:
2238 free(tree_obj_id);
2239 if (tree)
2240 got_object_tree_close(tree);
2241 if (err) {
2242 free(*link_target);
2243 *link_target = NULL;
2245 return err;
2248 const struct got_error *
2249 got_object_resolve_symlinks(char **link_target, const char *path,
2250 struct got_commit_object *commit, struct got_repository *repo)
2252 const struct got_error *err = NULL;
2253 char *next_target = NULL;
2254 int max_recursion = 40; /* matches Git */
2256 *link_target = NULL;
2258 do {
2259 err = resolve_symlink(&next_target,
2260 *link_target ? *link_target : path, commit, repo);
2261 if (err)
2262 break;
2263 if (next_target) {
2264 free(*link_target);
2265 if (--max_recursion == 0) {
2266 err = got_error_path(path, GOT_ERR_RECURSION);
2267 *link_target = NULL;
2268 break;
2270 *link_target = next_target;
2272 } while (next_target);
2274 return err;
2277 const struct got_error *
2278 got_traverse_packed_commits(struct got_object_id_queue *traversed_commits,
2279 struct got_object_id *commit_id, const char *path,
2280 struct got_repository *repo)
2282 const struct got_error *err = NULL;
2283 struct got_pack *pack = NULL;
2284 struct got_packidx *packidx = NULL;
2285 char *path_packfile = NULL;
2286 struct got_commit_object *changed_commit = NULL;
2287 struct got_object_id *changed_commit_id = NULL;
2288 int idx;
2290 err = got_repo_search_packidx(&packidx, &idx, repo, commit_id);
2291 if (err) {
2292 if (err->code != GOT_ERR_NO_OBJ)
2293 return err;
2294 return NULL;
2297 err = got_packidx_get_packfile_path(&path_packfile,
2298 packidx->path_packidx);
2299 if (err)
2300 return err;
2302 pack = got_repo_get_cached_pack(repo, path_packfile);
2303 if (pack == NULL) {
2304 err = got_repo_cache_pack(&pack, repo, path_packfile, packidx);
2305 if (err)
2306 goto done;
2309 if (pack->privsep_child == NULL) {
2310 err = start_pack_privsep_child(pack, packidx);
2311 if (err)
2312 goto done;
2315 err = got_privsep_send_commit_traversal_request(
2316 pack->privsep_child->ibuf, commit_id, idx, path);
2317 if (err)
2318 goto done;
2320 err = got_privsep_recv_traversed_commits(&changed_commit,
2321 &changed_commit_id, traversed_commits, pack->privsep_child->ibuf);
2322 if (err)
2323 goto done;
2325 if (changed_commit) {
2327 * Cache the commit in which the path was changed.
2328 * This commit might be opened again soon.
2330 changed_commit->refcnt++;
2331 err = got_repo_cache_commit(repo, changed_commit_id,
2332 changed_commit);
2333 got_object_commit_close(changed_commit);
2335 done:
2336 free(path_packfile);
2337 free(changed_commit_id);
2338 return err;