Blob


1 /*
2 * Copyright (c) 2018, 2019 Stefan Sperling <stsp@openbsd.org>
3 *
4 * Permission to use, copy, modify, and distribute this software for any
5 * purpose with or without fee is hereby granted, provided that the above
6 * copyright notice and this permission notice appear in all copies.
7 *
8 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
15 */
17 #include <sys/types.h>
18 #include <sys/stat.h>
19 #include <sys/queue.h>
20 #include <sys/tree.h>
21 #include <sys/uio.h>
22 #include <sys/socket.h>
23 #include <sys/wait.h>
24 #include <sys/resource.h>
25 #include <sys/mman.h>
27 #include <errno.h>
28 #include <fcntl.h>
29 #include <stdio.h>
30 #include <stdlib.h>
31 #include <string.h>
32 #include <stdint.h>
33 #include <sha1.h>
34 #include <unistd.h>
35 #include <zlib.h>
36 #include <ctype.h>
37 #include <libgen.h>
38 #include <limits.h>
39 #include <imsg.h>
40 #include <time.h>
42 #include "got_error.h"
43 #include "got_object.h"
44 #include "got_repository.h"
45 #include "got_opentemp.h"
46 #include "got_path.h"
48 #include "got_lib_sha1.h"
49 #include "got_lib_delta.h"
50 #include "got_lib_inflate.h"
51 #include "got_lib_object.h"
52 #include "got_lib_privsep.h"
53 #include "got_lib_object_idcache.h"
54 #include "got_lib_object_cache.h"
55 #include "got_lib_object_parse.h"
56 #include "got_lib_pack.h"
57 #include "got_lib_repository.h"
59 #ifndef MIN
60 #define MIN(_a,_b) ((_a) < (_b) ? (_a) : (_b))
61 #endif
63 struct got_object_id *
64 got_object_get_id(struct got_object *obj)
65 {
66 return &obj->id;
67 }
69 const struct got_error *
70 got_object_get_id_str(char **outbuf, struct got_object *obj)
71 {
72 return got_object_id_str(outbuf, &obj->id);
73 }
75 const struct got_error *
76 got_object_get_type(int *type, struct got_repository *repo,
77 struct got_object_id *id)
78 {
79 const struct got_error *err = NULL;
80 struct got_object *obj;
82 err = got_object_open(&obj, repo, id);
83 if (err)
84 return err;
86 switch (obj->type) {
87 case GOT_OBJ_TYPE_COMMIT:
88 case GOT_OBJ_TYPE_TREE:
89 case GOT_OBJ_TYPE_BLOB:
90 case GOT_OBJ_TYPE_TAG:
91 *type = obj->type;
92 break;
93 default:
94 err = got_error(GOT_ERR_OBJ_TYPE);
95 break;
96 }
98 got_object_close(obj);
99 return err;
102 const struct got_error *
103 got_object_get_path(char **path, struct got_object_id *id,
104 struct got_repository *repo)
106 const struct got_error *err = NULL;
107 char *hex = NULL;
108 char *path_objects;
110 *path = NULL;
112 path_objects = got_repo_get_path_objects(repo);
113 if (path_objects == NULL)
114 return got_error_from_errno("got_repo_get_path_objects");
116 err = got_object_id_str(&hex, id);
117 if (err)
118 goto done;
120 if (asprintf(path, "%s/%.2x/%s", path_objects,
121 id->sha1[0], hex + 2) == -1)
122 err = got_error_from_errno("asprintf");
124 done:
125 free(hex);
126 free(path_objects);
127 return err;
130 const struct got_error *
131 got_object_open_loose_fd(int *fd, struct got_object_id *id,
132 struct got_repository *repo)
134 const struct got_error *err = NULL;
135 char *path;
137 err = got_object_get_path(&path, id, repo);
138 if (err)
139 return err;
140 *fd = open(path, O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
141 if (*fd == -1) {
142 err = got_error_from_errno2("open", path);
143 goto done;
145 done:
146 free(path);
147 return err;
150 static const struct got_error *
151 request_packed_object(struct got_object **obj, struct got_pack *pack, int idx,
152 struct got_object_id *id)
154 const struct got_error *err = NULL;
155 struct imsgbuf *ibuf = pack->privsep_child->ibuf;
157 err = got_privsep_send_packed_obj_req(ibuf, idx, id);
158 if (err)
159 return err;
161 err = got_privsep_recv_obj(obj, ibuf);
162 if (err)
163 return err;
165 memcpy(&(*obj)->id, id, sizeof((*obj)->id));
167 return NULL;
170 /* Create temporary files used during delta application. */
171 static const struct got_error *
172 pack_child_send_tempfiles(struct imsgbuf *ibuf, struct got_pack *pack)
174 const struct got_error *err;
175 int basefd, accumfd;
177 /*
178 * For performance reasons, the child will keep reusing the
179 * same temporary files during every object request.
180 * Opening and closing new files for every object request is
181 * too expensive during operations such as 'gotadmin pack'.
182 */
183 if (pack->child_has_tempfiles)
184 return NULL;
186 basefd = got_opentempfd();
187 if (basefd == -1)
188 return got_error_from_errno("got_opentempfd");
190 err = got_privsep_send_tmpfd(ibuf, basefd);
191 if (err)
192 return err;
194 accumfd = got_opentempfd();
195 if (accumfd == -1)
196 return got_error_from_errno("got_opentempfd");
198 err = got_privsep_send_tmpfd(ibuf, accumfd);
199 if (err)
200 return err;
202 pack->child_has_tempfiles = 1;
203 return NULL;
206 static const struct got_error *
207 request_packed_object_raw(uint8_t **outbuf, off_t *size, size_t *hdrlen,
208 int outfd, struct got_pack *pack, int idx, struct got_object_id *id)
210 const struct got_error *err = NULL;
211 struct imsgbuf *ibuf = pack->privsep_child->ibuf;
212 int outfd_child;
214 err = pack_child_send_tempfiles(ibuf, pack);
215 if (err)
216 return err;
218 outfd_child = dup(outfd);
219 if (outfd_child == -1)
220 return got_error_from_errno("dup");
222 err = got_privsep_send_packed_raw_obj_req(ibuf, idx, id);
223 if (err) {
224 close(outfd_child);
225 return err;
228 err = got_privsep_send_raw_obj_outfd(ibuf, outfd_child);
229 if (err)
230 return err;
232 err = got_privsep_recv_raw_obj(outbuf, size, hdrlen, ibuf);
233 if (err)
234 return err;
236 return NULL;
239 static void
240 set_max_datasize(void)
242 struct rlimit rl;
244 if (getrlimit(RLIMIT_DATA, &rl) != 0)
245 return;
247 rl.rlim_cur = rl.rlim_max;
248 setrlimit(RLIMIT_DATA, &rl);
251 static const struct got_error *
252 start_pack_privsep_child(struct got_pack *pack, struct got_packidx *packidx)
254 const struct got_error *err = NULL;
255 int imsg_fds[2];
256 pid_t pid;
257 struct imsgbuf *ibuf;
259 ibuf = calloc(1, sizeof(*ibuf));
260 if (ibuf == NULL)
261 return got_error_from_errno("calloc");
263 pack->privsep_child = calloc(1, sizeof(*pack->privsep_child));
264 if (pack->privsep_child == NULL) {
265 err = got_error_from_errno("calloc");
266 free(ibuf);
267 return err;
269 pack->child_has_tempfiles = 0;
270 pack->child_has_delta_outfd = 0;
272 if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_fds) == -1) {
273 err = got_error_from_errno("socketpair");
274 goto done;
277 pid = fork();
278 if (pid == -1) {
279 err = got_error_from_errno("fork");
280 goto done;
281 } else if (pid == 0) {
282 set_max_datasize();
283 got_privsep_exec_child(imsg_fds, GOT_PATH_PROG_READ_PACK,
284 pack->path_packfile);
285 /* not reached */
288 if (close(imsg_fds[1]) == -1)
289 return got_error_from_errno("close");
290 pack->privsep_child->imsg_fd = imsg_fds[0];
291 pack->privsep_child->pid = pid;
292 imsg_init(ibuf, imsg_fds[0]);
293 pack->privsep_child->ibuf = ibuf;
295 err = got_privsep_init_pack_child(ibuf, pack, packidx);
296 if (err) {
297 const struct got_error *child_err;
298 err = got_privsep_send_stop(pack->privsep_child->imsg_fd);
299 child_err = got_privsep_wait_for_child(
300 pack->privsep_child->pid);
301 if (child_err && err == NULL)
302 err = child_err;
304 done:
305 if (err) {
306 free(ibuf);
307 free(pack->privsep_child);
308 pack->privsep_child = NULL;
310 return err;
313 static const struct got_error *
314 read_packed_object_privsep(struct got_object **obj,
315 struct got_repository *repo, struct got_pack *pack,
316 struct got_packidx *packidx, int idx, struct got_object_id *id)
318 const struct got_error *err = NULL;
320 if (pack->privsep_child == NULL) {
321 err = start_pack_privsep_child(pack, packidx);
322 if (err)
323 return err;
326 return request_packed_object(obj, pack, idx, id);
329 static const struct got_error *
330 read_packed_object_raw_privsep(uint8_t **outbuf, off_t *size, size_t *hdrlen,
331 int outfd, struct got_pack *pack, struct got_packidx *packidx, int idx,
332 struct got_object_id *id)
334 const struct got_error *err = NULL;
336 if (pack->privsep_child == NULL) {
337 err = start_pack_privsep_child(pack, packidx);
338 if (err)
339 return err;
342 return request_packed_object_raw(outbuf, size, hdrlen, outfd, pack,
343 idx, id);
346 const struct got_error *
347 got_object_open_packed(struct got_object **obj, struct got_object_id *id,
348 struct got_repository *repo)
350 const struct got_error *err = NULL;
351 struct got_pack *pack = NULL;
352 struct got_packidx *packidx = NULL;
353 int idx;
354 char *path_packfile;
356 err = got_repo_search_packidx(&packidx, &idx, repo, id);
357 if (err)
358 return err;
360 err = got_packidx_get_packfile_path(&path_packfile,
361 packidx->path_packidx);
362 if (err)
363 return err;
365 pack = got_repo_get_cached_pack(repo, path_packfile);
366 if (pack == NULL) {
367 err = got_repo_cache_pack(&pack, repo, path_packfile, packidx);
368 if (err)
369 goto done;
372 err = read_packed_object_privsep(obj, repo, pack, packidx, idx, id);
373 if (err)
374 goto done;
375 done:
376 free(path_packfile);
377 return err;
380 const struct got_error *
381 got_object_open_from_packfile(struct got_object **obj, struct got_object_id *id,
382 struct got_pack *pack, struct got_packidx *packidx, int obj_idx,
383 struct got_repository *repo)
385 return read_packed_object_privsep(obj, repo, pack, packidx,
386 obj_idx, id);
389 const struct got_error *
390 got_object_read_raw_delta(uint64_t *base_size, uint64_t *result_size,
391 off_t *delta_size, off_t *delta_offset, off_t *delta_out_offset,
392 struct got_object_id **base_id, int delta_cache_fd,
393 struct got_packidx *packidx, int obj_idx, struct got_object_id *id,
394 struct got_repository *repo)
396 const struct got_error *err = NULL;
397 struct got_pack *pack = NULL;
398 char *path_packfile;
400 *base_size = 0;
401 *result_size = 0;
402 *delta_size = 0;
403 *delta_offset = 0;
404 *delta_out_offset = 0;
406 err = got_packidx_get_packfile_path(&path_packfile,
407 packidx->path_packidx);
408 if (err)
409 return err;
411 pack = got_repo_get_cached_pack(repo, path_packfile);
412 if (pack == NULL) {
413 err = got_repo_cache_pack(&pack, repo, path_packfile, packidx);
414 if (err)
415 return err;
418 if (pack->privsep_child == NULL) {
419 err = start_pack_privsep_child(pack, packidx);
420 if (err)
421 return err;
424 if (!pack->child_has_delta_outfd) {
425 int outfd_child;
426 outfd_child = dup(delta_cache_fd);
427 if (outfd_child == -1)
428 return got_error_from_errno("dup");
429 err = got_privsep_send_raw_delta_outfd(
430 pack->privsep_child->ibuf, outfd_child);
431 if (err)
432 return err;
433 pack->child_has_delta_outfd = 1;
436 err = got_privsep_send_raw_delta_req(pack->privsep_child->ibuf,
437 obj_idx, id);
438 if (err)
439 return err;
441 return got_privsep_recv_raw_delta(base_size, result_size, delta_size,
442 delta_offset, delta_out_offset, base_id, pack->privsep_child->ibuf);
445 static const struct got_error *
446 request_object(struct got_object **obj, struct got_object_id *id,
447 struct got_repository *repo, int fd)
449 const struct got_error *err = NULL;
450 struct imsgbuf *ibuf;
452 ibuf = repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_OBJECT].ibuf;
454 err = got_privsep_send_obj_req(ibuf, fd, id);
455 if (err)
456 return err;
458 return got_privsep_recv_obj(obj, ibuf);
461 static const struct got_error *
462 request_raw_object(uint8_t **outbuf, off_t *size, size_t *hdrlen, int outfd,
463 struct got_object_id *id, struct got_repository *repo, int infd)
465 const struct got_error *err = NULL;
466 struct imsgbuf *ibuf;
467 int outfd_child;
469 ibuf = repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_OBJECT].ibuf;
471 outfd_child = dup(outfd);
472 if (outfd_child == -1)
473 return got_error_from_errno("dup");
475 err = got_privsep_send_raw_obj_req(ibuf, infd, id);
476 if (err)
477 return err;
479 err = got_privsep_send_raw_obj_outfd(ibuf, outfd_child);
480 if (err)
481 return err;
483 return got_privsep_recv_raw_obj(outbuf, size, hdrlen, ibuf);
486 static const struct got_error *
487 start_read_object_child(struct got_repository *repo)
489 const struct got_error *err = NULL;
490 int imsg_fds[2];
491 pid_t pid;
492 struct imsgbuf *ibuf;
494 ibuf = calloc(1, sizeof(*ibuf));
495 if (ibuf == NULL)
496 return got_error_from_errno("calloc");
498 if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_fds) == -1) {
499 err = got_error_from_errno("socketpair");
500 free(ibuf);
501 return err;
504 pid = fork();
505 if (pid == -1) {
506 err = got_error_from_errno("fork");
507 free(ibuf);
508 return err;
510 else if (pid == 0) {
511 got_privsep_exec_child(imsg_fds, GOT_PATH_PROG_READ_OBJECT,
512 repo->path);
513 /* not reached */
516 if (close(imsg_fds[1]) == -1) {
517 err = got_error_from_errno("close");
518 free(ibuf);
519 return err;
522 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_OBJECT].imsg_fd =
523 imsg_fds[0];
524 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_OBJECT].pid = pid;
525 imsg_init(ibuf, imsg_fds[0]);
526 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_OBJECT].ibuf = ibuf;
528 return NULL;
531 const struct got_error *
532 got_object_read_header_privsep(struct got_object **obj,
533 struct got_object_id *id, struct got_repository *repo, int obj_fd)
535 const struct got_error *err;
537 if (repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_OBJECT].imsg_fd != -1)
538 return request_object(obj, id, repo, obj_fd);
540 err = start_read_object_child(repo);
541 if (err) {
542 close(obj_fd);
543 return err;
546 return request_object(obj, id, repo, obj_fd);
549 static const struct got_error *
550 read_object_raw_privsep(uint8_t **outbuf, off_t *size, size_t *hdrlen,
551 int outfd, struct got_object_id *id, struct got_repository *repo,
552 int obj_fd)
554 const struct got_error *err;
556 if (repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_OBJECT].imsg_fd != -1)
557 return request_raw_object(outbuf, size, hdrlen, outfd, id,
558 repo, obj_fd);
560 err = start_read_object_child(repo);
561 if (err)
562 return err;
564 return request_raw_object(outbuf, size, hdrlen, outfd, id, repo,
565 obj_fd);
568 const struct got_error *
569 got_object_open(struct got_object **obj, struct got_repository *repo,
570 struct got_object_id *id)
572 const struct got_error *err = NULL;
573 int fd;
575 *obj = got_repo_get_cached_object(repo, id);
576 if (*obj != NULL) {
577 (*obj)->refcnt++;
578 return NULL;
581 err = got_object_open_packed(obj, id, repo);
582 if (err && err->code != GOT_ERR_NO_OBJ)
583 return err;
584 if (*obj) {
585 (*obj)->refcnt++;
586 return got_repo_cache_object(repo, id, *obj);
589 err = got_object_open_loose_fd(&fd, id, repo);
590 if (err) {
591 if (err->code == GOT_ERR_ERRNO && errno == ENOENT)
592 err = got_error_no_obj(id);
593 return err;
596 err = got_object_read_header_privsep(obj, id, repo, fd);
597 if (err)
598 return err;
600 memcpy((*obj)->id.sha1, id->sha1, SHA1_DIGEST_LENGTH);
602 (*obj)->refcnt++;
603 return got_repo_cache_object(repo, id, *obj);
606 /* *outfd must be initialized to -1 by caller */
607 const struct got_error *
608 got_object_raw_open(struct got_raw_object **obj, int *outfd,
609 struct got_repository *repo, struct got_object_id *id)
611 const struct got_error *err = NULL;
612 struct got_packidx *packidx = NULL;
613 int idx;
614 uint8_t *outbuf = NULL;
615 off_t size = 0;
616 size_t hdrlen = 0;
617 char *path_packfile = NULL;
619 *obj = got_repo_get_cached_raw_object(repo, id);
620 if (*obj != NULL) {
621 (*obj)->refcnt++;
622 return NULL;
625 if (*outfd == -1) {
626 *outfd = got_opentempfd();
627 if (*outfd == -1)
628 return got_error_from_errno("got_opentempfd");
631 err = got_repo_search_packidx(&packidx, &idx, repo, id);
632 if (err == NULL) {
633 struct got_pack *pack = NULL;
635 err = got_packidx_get_packfile_path(&path_packfile,
636 packidx->path_packidx);
637 if (err)
638 goto done;
640 pack = got_repo_get_cached_pack(repo, path_packfile);
641 if (pack == NULL) {
642 err = got_repo_cache_pack(&pack, repo, path_packfile,
643 packidx);
644 if (err)
645 goto done;
647 err = read_packed_object_raw_privsep(&outbuf, &size, &hdrlen,
648 *outfd, pack, packidx, idx, id);
649 if (err)
650 goto done;
651 } else if (err->code == GOT_ERR_NO_OBJ) {
652 int fd;
654 err = got_object_open_loose_fd(&fd, id, repo);
655 if (err)
656 goto done;
657 err = read_object_raw_privsep(&outbuf, &size, &hdrlen, *outfd,
658 id, repo, fd);
659 if (err)
660 goto done;
663 *obj = calloc(1, sizeof(**obj));
664 if (*obj == NULL) {
665 err = got_error_from_errno("calloc");
666 goto done;
668 (*obj)->fd = -1;
670 if (outbuf) {
671 (*obj)->data = outbuf;
672 } else {
673 struct stat sb;
674 if (fstat(*outfd, &sb) == -1) {
675 err = got_error_from_errno("fstat");
676 goto done;
679 if (sb.st_size != hdrlen + size) {
680 err = got_error(GOT_ERR_PRIVSEP_LEN);
681 goto done;
683 #ifndef GOT_PACK_NO_MMAP
684 if (hdrlen + size > 0) {
685 (*obj)->data = mmap(NULL, hdrlen + size, PROT_READ,
686 MAP_PRIVATE, *outfd, 0);
687 if ((*obj)->data == MAP_FAILED) {
688 if (errno != ENOMEM) {
689 err = got_error_from_errno("mmap");
690 goto done;
692 (*obj)->data = NULL;
693 } else {
694 (*obj)->fd = *outfd;
695 *outfd = -1;
698 #endif
699 if (*outfd != -1) {
700 (*obj)->f = fdopen(*outfd, "r");
701 if ((*obj)->f == NULL) {
702 err = got_error_from_errno("fdopen");
703 goto done;
705 *outfd = -1;
708 (*obj)->hdrlen = hdrlen;
709 (*obj)->size = size;
710 err = got_repo_cache_raw_object(repo, id, *obj);
711 done:
712 free(path_packfile);
713 if (err) {
714 if (*obj) {
715 got_object_raw_close(*obj);
716 *obj = NULL;
718 free(outbuf);
719 } else
720 (*obj)->refcnt++;
721 return err;
724 const struct got_error *
725 got_object_open_by_id_str(struct got_object **obj, struct got_repository *repo,
726 const char *id_str)
728 struct got_object_id id;
730 if (!got_parse_sha1_digest(id.sha1, id_str))
731 return got_error_path(id_str, GOT_ERR_BAD_OBJ_ID_STR);
733 return got_object_open(obj, repo, &id);
736 const struct got_error *
737 got_object_resolve_id_str(struct got_object_id **id,
738 struct got_repository *repo, const char *id_str)
740 const struct got_error *err = NULL;
741 struct got_object *obj;
743 err = got_object_open_by_id_str(&obj, repo, id_str);
744 if (err)
745 return err;
747 *id = got_object_id_dup(got_object_get_id(obj));
748 got_object_close(obj);
749 if (*id == NULL)
750 return got_error_from_errno("got_object_id_dup");
752 return NULL;
755 static const struct got_error *
756 request_packed_commit(struct got_commit_object **commit, struct got_pack *pack,
757 int pack_idx, struct got_object_id *id)
759 const struct got_error *err = NULL;
761 err = got_privsep_send_commit_req(pack->privsep_child->ibuf, -1, id,
762 pack_idx);
763 if (err)
764 return err;
766 err = got_privsep_recv_commit(commit, pack->privsep_child->ibuf);
767 if (err)
768 return err;
770 (*commit)->flags |= GOT_COMMIT_FLAG_PACKED;
771 return NULL;
774 static const struct got_error *
775 read_packed_commit_privsep(struct got_commit_object **commit,
776 struct got_pack *pack, struct got_packidx *packidx, int idx,
777 struct got_object_id *id)
779 const struct got_error *err = NULL;
781 if (pack->privsep_child)
782 return request_packed_commit(commit, pack, idx, id);
784 err = start_pack_privsep_child(pack, packidx);
785 if (err)
786 return err;
788 return request_packed_commit(commit, pack, idx, id);
791 static const struct got_error *
792 request_commit(struct got_commit_object **commit, struct got_repository *repo,
793 int fd, struct got_object_id *id)
795 const struct got_error *err = NULL;
796 struct imsgbuf *ibuf;
798 ibuf = repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_COMMIT].ibuf;
800 err = got_privsep_send_commit_req(ibuf, fd, id, -1);
801 if (err)
802 return err;
804 return got_privsep_recv_commit(commit, ibuf);
807 static const struct got_error *
808 read_commit_privsep(struct got_commit_object **commit, int obj_fd,
809 struct got_object_id *id, struct got_repository *repo)
811 const struct got_error *err;
812 int imsg_fds[2];
813 pid_t pid;
814 struct imsgbuf *ibuf;
816 if (repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_COMMIT].imsg_fd != -1)
817 return request_commit(commit, repo, obj_fd, id);
819 ibuf = calloc(1, sizeof(*ibuf));
820 if (ibuf == NULL)
821 return got_error_from_errno("calloc");
823 if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_fds) == -1) {
824 err = got_error_from_errno("socketpair");
825 free(ibuf);
826 return err;
829 pid = fork();
830 if (pid == -1) {
831 err = got_error_from_errno("fork");
832 free(ibuf);
833 return err;
835 else if (pid == 0) {
836 got_privsep_exec_child(imsg_fds, GOT_PATH_PROG_READ_COMMIT,
837 repo->path);
838 /* not reached */
841 if (close(imsg_fds[1]) == -1) {
842 err = got_error_from_errno("close");
843 free(ibuf);
844 return err;
846 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_COMMIT].imsg_fd =
847 imsg_fds[0];
848 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_COMMIT].pid = pid;
849 imsg_init(ibuf, imsg_fds[0]);
850 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_COMMIT].ibuf = ibuf;
852 return request_commit(commit, repo, obj_fd, id);
856 static const struct got_error *
857 open_commit(struct got_commit_object **commit,
858 struct got_repository *repo, struct got_object_id *id, int check_cache)
860 const struct got_error *err = NULL;
861 struct got_packidx *packidx = NULL;
862 int idx;
863 char *path_packfile = NULL;
865 if (check_cache) {
866 *commit = got_repo_get_cached_commit(repo, id);
867 if (*commit != NULL) {
868 (*commit)->refcnt++;
869 return NULL;
871 } else
872 *commit = NULL;
874 err = got_repo_search_packidx(&packidx, &idx, repo, id);
875 if (err == NULL) {
876 struct got_pack *pack = NULL;
878 err = got_packidx_get_packfile_path(&path_packfile,
879 packidx->path_packidx);
880 if (err)
881 return err;
883 pack = got_repo_get_cached_pack(repo, path_packfile);
884 if (pack == NULL) {
885 err = got_repo_cache_pack(&pack, repo, path_packfile,
886 packidx);
887 if (err)
888 goto done;
890 err = read_packed_commit_privsep(commit, pack,
891 packidx, idx, id);
892 } else if (err->code == GOT_ERR_NO_OBJ) {
893 int fd;
895 err = got_object_open_loose_fd(&fd, id, repo);
896 if (err)
897 return err;
898 err = read_commit_privsep(commit, fd, id, repo);
901 if (err == NULL) {
902 (*commit)->refcnt++;
903 err = got_repo_cache_commit(repo, id, *commit);
905 done:
906 free(path_packfile);
907 return err;
910 const struct got_error *
911 got_object_open_as_commit(struct got_commit_object **commit,
912 struct got_repository *repo, struct got_object_id *id)
914 *commit = got_repo_get_cached_commit(repo, id);
915 if (*commit != NULL) {
916 (*commit)->refcnt++;
917 return NULL;
920 return open_commit(commit, repo, id, 0);
923 const struct got_error *
924 got_object_commit_open(struct got_commit_object **commit,
925 struct got_repository *repo, struct got_object *obj)
927 return open_commit(commit, repo, got_object_get_id(obj), 1);
930 const struct got_error *
931 got_object_qid_alloc(struct got_object_qid **qid, struct got_object_id *id)
933 const struct got_error *err = NULL;
935 *qid = calloc(1, sizeof(**qid));
936 if (*qid == NULL)
937 return got_error_from_errno("calloc");
939 (*qid)->id = got_object_id_dup(id);
940 if ((*qid)->id == NULL) {
941 err = got_error_from_errno("got_object_id_dup");
942 got_object_qid_free(*qid);
943 *qid = NULL;
944 return err;
947 return NULL;
950 const struct got_error *
951 got_object_id_queue_copy(const struct got_object_id_queue *src,
952 struct got_object_id_queue *dest)
954 const struct got_error *err;
955 struct got_object_qid *qid;
957 STAILQ_FOREACH(qid, src, entry) {
958 struct got_object_qid *new;
959 /*
960 * Deep-copy the object ID only. Let the caller deal
961 * with setting up the new->data pointer if needed.
962 */
963 err = got_object_qid_alloc(&new, qid->id);
964 if (err) {
965 got_object_id_queue_free(dest);
966 return err;
968 STAILQ_INSERT_TAIL(dest, new, entry);
971 return NULL;
974 static const struct got_error *
975 request_packed_tree(struct got_tree_object **tree, struct got_pack *pack,
976 int pack_idx, struct got_object_id *id)
978 const struct got_error *err = NULL;
980 err = got_privsep_send_tree_req(pack->privsep_child->ibuf, -1, id,
981 pack_idx);
982 if (err)
983 return err;
985 return got_privsep_recv_tree(tree, pack->privsep_child->ibuf);
988 static const struct got_error *
989 read_packed_tree_privsep(struct got_tree_object **tree,
990 struct got_pack *pack, struct got_packidx *packidx, int idx,
991 struct got_object_id *id)
993 const struct got_error *err = NULL;
995 if (pack->privsep_child)
996 return request_packed_tree(tree, pack, idx, id);
998 err = start_pack_privsep_child(pack, packidx);
999 if (err)
1000 return err;
1002 return request_packed_tree(tree, pack, idx, id);
1005 static const struct got_error *
1006 request_tree(struct got_tree_object **tree, struct got_repository *repo,
1007 int fd, struct got_object_id *id)
1009 const struct got_error *err = NULL;
1010 struct imsgbuf *ibuf;
1012 ibuf = repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TREE].ibuf;
1014 err = got_privsep_send_tree_req(ibuf, fd, id, -1);
1015 if (err)
1016 return err;
1018 return got_privsep_recv_tree(tree, ibuf);
1021 const struct got_error *
1022 read_tree_privsep(struct got_tree_object **tree, int obj_fd,
1023 struct got_object_id *id, struct got_repository *repo)
1025 const struct got_error *err;
1026 int imsg_fds[2];
1027 pid_t pid;
1028 struct imsgbuf *ibuf;
1030 if (repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TREE].imsg_fd != -1)
1031 return request_tree(tree, repo, obj_fd, id);
1033 ibuf = calloc(1, sizeof(*ibuf));
1034 if (ibuf == NULL)
1035 return got_error_from_errno("calloc");
1037 if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_fds) == -1) {
1038 err = got_error_from_errno("socketpair");
1039 free(ibuf);
1040 return err;
1043 pid = fork();
1044 if (pid == -1) {
1045 err = got_error_from_errno("fork");
1046 free(ibuf);
1047 return err;
1049 else if (pid == 0) {
1050 got_privsep_exec_child(imsg_fds, GOT_PATH_PROG_READ_TREE,
1051 repo->path);
1052 /* not reached */
1055 if (close(imsg_fds[1]) == -1) {
1056 err = got_error_from_errno("close");
1057 free(ibuf);
1058 return err;
1060 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TREE].imsg_fd =
1061 imsg_fds[0];
1062 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TREE].pid = pid;
1063 imsg_init(ibuf, imsg_fds[0]);
1064 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TREE].ibuf = ibuf;
1067 return request_tree(tree, repo, obj_fd, id);
1070 static const struct got_error *
1071 open_tree(struct got_tree_object **tree, struct got_repository *repo,
1072 struct got_object_id *id, int check_cache)
1074 const struct got_error *err = NULL;
1075 struct got_packidx *packidx = NULL;
1076 int idx;
1077 char *path_packfile = NULL;
1079 if (check_cache) {
1080 *tree = got_repo_get_cached_tree(repo, id);
1081 if (*tree != NULL) {
1082 (*tree)->refcnt++;
1083 return NULL;
1085 } else
1086 *tree = NULL;
1088 err = got_repo_search_packidx(&packidx, &idx, repo, id);
1089 if (err == NULL) {
1090 struct got_pack *pack = NULL;
1092 err = got_packidx_get_packfile_path(&path_packfile,
1093 packidx->path_packidx);
1094 if (err)
1095 return err;
1097 pack = got_repo_get_cached_pack(repo, path_packfile);
1098 if (pack == NULL) {
1099 err = got_repo_cache_pack(&pack, repo, path_packfile,
1100 packidx);
1101 if (err)
1102 goto done;
1104 err = read_packed_tree_privsep(tree, pack,
1105 packidx, idx, id);
1106 } else if (err->code == GOT_ERR_NO_OBJ) {
1107 int fd;
1109 err = got_object_open_loose_fd(&fd, id, repo);
1110 if (err)
1111 return err;
1112 err = read_tree_privsep(tree, fd, id, repo);
1115 if (err == NULL) {
1116 (*tree)->refcnt++;
1117 err = got_repo_cache_tree(repo, id, *tree);
1119 done:
1120 free(path_packfile);
1121 return err;
1124 const struct got_error *
1125 got_object_open_as_tree(struct got_tree_object **tree,
1126 struct got_repository *repo, struct got_object_id *id)
1128 *tree = got_repo_get_cached_tree(repo, id);
1129 if (*tree != NULL) {
1130 (*tree)->refcnt++;
1131 return NULL;
1134 return open_tree(tree, repo, id, 0);
1137 const struct got_error *
1138 got_object_tree_open(struct got_tree_object **tree,
1139 struct got_repository *repo, struct got_object *obj)
1141 return open_tree(tree, repo, got_object_get_id(obj), 1);
1144 int
1145 got_object_tree_get_nentries(struct got_tree_object *tree)
1147 return tree->nentries;
1150 struct got_tree_entry *
1151 got_object_tree_get_first_entry(struct got_tree_object *tree)
1153 return got_object_tree_get_entry(tree, 0);
1156 struct got_tree_entry *
1157 got_object_tree_get_last_entry(struct got_tree_object *tree)
1159 return got_object_tree_get_entry(tree, tree->nentries - 1);
1162 struct got_tree_entry *
1163 got_object_tree_get_entry(struct got_tree_object *tree, int i)
1165 if (i < 0 || i >= tree->nentries)
1166 return NULL;
1167 return &tree->entries[i];
1170 mode_t
1171 got_tree_entry_get_mode(struct got_tree_entry *te)
1173 return te->mode;
1176 const char *
1177 got_tree_entry_get_name(struct got_tree_entry *te)
1179 return &te->name[0];
1182 struct got_object_id *
1183 got_tree_entry_get_id(struct got_tree_entry *te)
1185 return &te->id;
1188 const struct got_error *
1189 got_object_blob_read_to_str(char **s, struct got_blob_object *blob)
1191 const struct got_error *err = NULL;
1192 size_t len, totlen, hdrlen, offset;
1194 *s = NULL;
1196 hdrlen = got_object_blob_get_hdrlen(blob);
1197 totlen = 0;
1198 offset = 0;
1199 do {
1200 char *p;
1202 err = got_object_blob_read_block(&len, blob);
1203 if (err)
1204 return err;
1206 if (len == 0)
1207 break;
1209 totlen += len - hdrlen;
1210 p = realloc(*s, totlen + 1);
1211 if (p == NULL) {
1212 err = got_error_from_errno("realloc");
1213 free(*s);
1214 *s = NULL;
1215 return err;
1217 *s = p;
1218 /* Skip blob object header first time around. */
1219 memcpy(*s + offset,
1220 got_object_blob_get_read_buf(blob) + hdrlen, len - hdrlen);
1221 hdrlen = 0;
1222 offset = totlen;
1223 } while (len > 0);
1225 (*s)[totlen] = '\0';
1226 return NULL;
1229 const struct got_error *
1230 got_tree_entry_get_symlink_target(char **link_target, struct got_tree_entry *te,
1231 struct got_repository *repo)
1233 const struct got_error *err = NULL;
1234 struct got_blob_object *blob = NULL;
1236 *link_target = NULL;
1238 if (!got_object_tree_entry_is_symlink(te))
1239 return got_error(GOT_ERR_TREE_ENTRY_TYPE);
1241 err = got_object_open_as_blob(&blob, repo,
1242 got_tree_entry_get_id(te), PATH_MAX);
1243 if (err)
1244 return err;
1246 err = got_object_blob_read_to_str(link_target, blob);
1247 got_object_blob_close(blob);
1248 if (err) {
1249 free(*link_target);
1250 *link_target = NULL;
1252 return err;
1255 int
1256 got_tree_entry_get_index(struct got_tree_entry *te)
1258 return te->idx;
1261 struct got_tree_entry *
1262 got_tree_entry_get_next(struct got_tree_object *tree,
1263 struct got_tree_entry *te)
1265 return got_object_tree_get_entry(tree, te->idx + 1);
1268 struct got_tree_entry *
1269 got_tree_entry_get_prev(struct got_tree_object *tree,
1270 struct got_tree_entry *te)
1272 return got_object_tree_get_entry(tree, te->idx - 1);
1275 static const struct got_error *
1276 request_packed_blob(uint8_t **outbuf, size_t *size, size_t *hdrlen, int outfd,
1277 struct got_pack *pack, struct got_packidx *packidx, int idx,
1278 struct got_object_id *id)
1280 const struct got_error *err = NULL;
1281 struct imsgbuf *ibuf = pack->privsep_child->ibuf;
1282 int outfd_child;
1284 err = pack_child_send_tempfiles(ibuf, pack);
1285 if (err)
1286 return err;
1288 outfd_child = dup(outfd);
1289 if (outfd_child == -1)
1290 return got_error_from_errno("dup");
1292 err = got_privsep_send_blob_req(pack->privsep_child->ibuf, -1, id, idx);
1293 if (err)
1294 return err;
1296 err = got_privsep_send_blob_outfd(pack->privsep_child->ibuf,
1297 outfd_child);
1298 if (err) {
1299 return err;
1302 err = got_privsep_recv_blob(outbuf, size, hdrlen,
1303 pack->privsep_child->ibuf);
1304 if (err)
1305 return err;
1307 if (lseek(outfd, SEEK_SET, 0) == -1)
1308 err = got_error_from_errno("lseek");
1310 return err;
1313 static const struct got_error *
1314 read_packed_blob_privsep(uint8_t **outbuf, size_t *size, size_t *hdrlen,
1315 int outfd, struct got_pack *pack, struct got_packidx *packidx, int idx,
1316 struct got_object_id *id)
1318 const struct got_error *err = NULL;
1320 if (pack->privsep_child == NULL) {
1321 err = start_pack_privsep_child(pack, packidx);
1322 if (err)
1323 return err;
1326 return request_packed_blob(outbuf, size, hdrlen, outfd, pack, packidx,
1327 idx, id);
1330 static const struct got_error *
1331 request_blob(uint8_t **outbuf, size_t *size, size_t *hdrlen, int outfd,
1332 int infd, struct got_object_id *id, struct imsgbuf *ibuf)
1334 const struct got_error *err = NULL;
1335 int outfd_child;
1337 outfd_child = dup(outfd);
1338 if (outfd_child == -1)
1339 return got_error_from_errno("dup");
1341 err = got_privsep_send_blob_req(ibuf, infd, id, -1);
1342 if (err)
1343 return err;
1345 err = got_privsep_send_blob_outfd(ibuf, outfd_child);
1346 if (err)
1347 return err;
1349 err = got_privsep_recv_blob(outbuf, size, hdrlen, ibuf);
1350 if (err)
1351 return err;
1353 if (lseek(outfd, SEEK_SET, 0) == -1)
1354 return got_error_from_errno("lseek");
1356 return err;
1359 static const struct got_error *
1360 read_blob_privsep(uint8_t **outbuf, size_t *size, size_t *hdrlen,
1361 int outfd, int infd, struct got_object_id *id, struct got_repository *repo)
1363 const struct got_error *err;
1364 int imsg_fds[2];
1365 pid_t pid;
1366 struct imsgbuf *ibuf;
1368 if (repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_BLOB].imsg_fd != -1) {
1369 ibuf = repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_BLOB].ibuf;
1370 return request_blob(outbuf, size, hdrlen, outfd, infd, id,
1371 ibuf);
1374 ibuf = calloc(1, sizeof(*ibuf));
1375 if (ibuf == NULL)
1376 return got_error_from_errno("calloc");
1378 if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_fds) == -1) {
1379 err = got_error_from_errno("socketpair");
1380 free(ibuf);
1381 return err;
1384 pid = fork();
1385 if (pid == -1) {
1386 err = got_error_from_errno("fork");
1387 free(ibuf);
1388 return err;
1390 else if (pid == 0) {
1391 got_privsep_exec_child(imsg_fds, GOT_PATH_PROG_READ_BLOB,
1392 repo->path);
1393 /* not reached */
1396 if (close(imsg_fds[1]) == -1) {
1397 err = got_error_from_errno("close");
1398 free(ibuf);
1399 return err;
1401 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_BLOB].imsg_fd =
1402 imsg_fds[0];
1403 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_BLOB].pid = pid;
1404 imsg_init(ibuf, imsg_fds[0]);
1405 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_BLOB].ibuf = ibuf;
1407 return request_blob(outbuf, size, hdrlen, outfd, infd, id, ibuf);
1410 static const struct got_error *
1411 open_blob(struct got_blob_object **blob, struct got_repository *repo,
1412 struct got_object_id *id, size_t blocksize)
1414 const struct got_error *err = NULL;
1415 struct got_packidx *packidx = NULL;
1416 int idx;
1417 char *path_packfile = NULL;
1418 uint8_t *outbuf;
1419 int outfd;
1420 size_t size, hdrlen;
1421 struct stat sb;
1423 *blob = calloc(1, sizeof(**blob));
1424 if (*blob == NULL)
1425 return got_error_from_errno("calloc");
1427 outfd = got_opentempfd();
1428 if (outfd == -1)
1429 return got_error_from_errno("got_opentempfd");
1431 (*blob)->read_buf = malloc(blocksize);
1432 if ((*blob)->read_buf == NULL) {
1433 err = got_error_from_errno("malloc");
1434 goto done;
1437 err = got_repo_search_packidx(&packidx, &idx, repo, id);
1438 if (err == NULL) {
1439 struct got_pack *pack = NULL;
1441 err = got_packidx_get_packfile_path(&path_packfile,
1442 packidx->path_packidx);
1443 if (err)
1444 goto done;
1446 pack = got_repo_get_cached_pack(repo, path_packfile);
1447 if (pack == NULL) {
1448 err = got_repo_cache_pack(&pack, repo, path_packfile,
1449 packidx);
1450 if (err)
1451 goto done;
1453 err = read_packed_blob_privsep(&outbuf, &size, &hdrlen, outfd,
1454 pack, packidx, idx, id);
1455 } else if (err->code == GOT_ERR_NO_OBJ) {
1456 int infd;
1458 err = got_object_open_loose_fd(&infd, id, repo);
1459 if (err)
1460 goto done;
1461 err = read_blob_privsep(&outbuf, &size, &hdrlen, outfd, infd,
1462 id, repo);
1464 if (err)
1465 goto done;
1467 if (hdrlen > size) {
1468 err = got_error(GOT_ERR_BAD_OBJ_HDR);
1469 goto done;
1472 if (outbuf) {
1473 if (close(outfd) == -1 && err == NULL)
1474 err = got_error_from_errno("close");
1475 outfd = -1;
1476 (*blob)->f = fmemopen(outbuf, size, "rb");
1477 if ((*blob)->f == NULL) {
1478 err = got_error_from_errno("fmemopen");
1479 free(outbuf);
1480 goto done;
1482 (*blob)->data = outbuf;
1483 } else {
1484 if (fstat(outfd, &sb) == -1) {
1485 err = got_error_from_errno("fstat");
1486 goto done;
1489 if (sb.st_size != size) {
1490 err = got_error(GOT_ERR_PRIVSEP_LEN);
1491 goto done;
1494 (*blob)->f = fdopen(outfd, "rb");
1495 if ((*blob)->f == NULL) {
1496 err = got_error_from_errno("fdopen");
1497 close(outfd);
1498 outfd = -1;
1499 goto done;
1503 (*blob)->hdrlen = hdrlen;
1504 (*blob)->blocksize = blocksize;
1505 memcpy(&(*blob)->id.sha1, id->sha1, SHA1_DIGEST_LENGTH);
1507 done:
1508 free(path_packfile);
1509 if (err) {
1510 if (*blob) {
1511 got_object_blob_close(*blob);
1512 *blob = NULL;
1513 } else if (outfd != -1)
1514 close(outfd);
1516 return err;
1519 const struct got_error *
1520 got_object_open_as_blob(struct got_blob_object **blob,
1521 struct got_repository *repo, struct got_object_id *id,
1522 size_t blocksize)
1524 return open_blob(blob, repo, id, blocksize);
1527 const struct got_error *
1528 got_object_blob_open(struct got_blob_object **blob,
1529 struct got_repository *repo, struct got_object *obj, size_t blocksize)
1531 return open_blob(blob, repo, got_object_get_id(obj), blocksize);
1534 const struct got_error *
1535 got_object_blob_close(struct got_blob_object *blob)
1537 const struct got_error *err = NULL;
1538 free(blob->read_buf);
1539 if (blob->f && fclose(blob->f) == EOF)
1540 err = got_error_from_errno("fclose");
1541 free(blob->data);
1542 free(blob);
1543 return err;
1546 void
1547 got_object_blob_rewind(struct got_blob_object *blob)
1549 if (blob->f)
1550 rewind(blob->f);
1553 char *
1554 got_object_blob_id_str(struct got_blob_object *blob, char *buf, size_t size)
1556 return got_sha1_digest_to_str(blob->id.sha1, buf, size);
1559 size_t
1560 got_object_blob_get_hdrlen(struct got_blob_object *blob)
1562 return blob->hdrlen;
1565 const uint8_t *
1566 got_object_blob_get_read_buf(struct got_blob_object *blob)
1568 return blob->read_buf;
1571 const struct got_error *
1572 got_object_blob_read_block(size_t *outlenp, struct got_blob_object *blob)
1574 size_t n;
1576 n = fread(blob->read_buf, 1, blob->blocksize, blob->f);
1577 if (n == 0 && ferror(blob->f))
1578 return got_ferror(blob->f, GOT_ERR_IO);
1579 *outlenp = n;
1580 return NULL;
1583 const struct got_error *
1584 got_object_blob_dump_to_file(off_t *filesize, int *nlines,
1585 off_t **line_offsets, FILE *outfile, struct got_blob_object *blob)
1587 const struct got_error *err = NULL;
1588 size_t n, len, hdrlen;
1589 const uint8_t *buf;
1590 int i;
1591 const int alloc_chunksz = 512;
1592 size_t nalloc = 0;
1593 off_t off = 0, total_len = 0;
1595 if (line_offsets)
1596 *line_offsets = NULL;
1597 if (filesize)
1598 *filesize = 0;
1599 if (nlines)
1600 *nlines = 0;
1602 hdrlen = got_object_blob_get_hdrlen(blob);
1603 do {
1604 err = got_object_blob_read_block(&len, blob);
1605 if (err)
1606 return err;
1607 if (len == 0)
1608 break;
1609 buf = got_object_blob_get_read_buf(blob);
1610 i = hdrlen;
1611 if (nlines) {
1612 if (line_offsets && *line_offsets == NULL) {
1613 /* Have some data but perhaps no '\n'. */
1614 *nlines = 1;
1615 nalloc = alloc_chunksz;
1616 *line_offsets = calloc(nalloc,
1617 sizeof(**line_offsets));
1618 if (*line_offsets == NULL)
1619 return got_error_from_errno("calloc");
1621 /* Skip forward over end of first line. */
1622 while (i < len) {
1623 if (buf[i] == '\n')
1624 break;
1625 i++;
1628 /* Scan '\n' offsets in remaining chunk of data. */
1629 while (i < len) {
1630 if (buf[i] != '\n') {
1631 i++;
1632 continue;
1634 (*nlines)++;
1635 if (line_offsets && nalloc < *nlines) {
1636 size_t n = *nlines + alloc_chunksz;
1637 off_t *o = recallocarray(*line_offsets,
1638 nalloc, n, sizeof(**line_offsets));
1639 if (o == NULL) {
1640 free(*line_offsets);
1641 *line_offsets = NULL;
1642 return got_error_from_errno(
1643 "recallocarray");
1645 *line_offsets = o;
1646 nalloc = n;
1648 if (line_offsets) {
1649 off = total_len + i - hdrlen + 1;
1650 (*line_offsets)[*nlines - 1] = off;
1652 i++;
1655 /* Skip blob object header first time around. */
1656 n = fwrite(buf + hdrlen, 1, len - hdrlen, outfile);
1657 if (n != len - hdrlen)
1658 return got_ferror(outfile, GOT_ERR_IO);
1659 total_len += len - hdrlen;
1660 hdrlen = 0;
1661 } while (len != 0);
1663 if (fflush(outfile) != 0)
1664 return got_error_from_errno("fflush");
1665 rewind(outfile);
1667 if (filesize)
1668 *filesize = total_len;
1670 return NULL;
1673 static const struct got_error *
1674 request_packed_tag(struct got_tag_object **tag, struct got_pack *pack,
1675 int pack_idx, struct got_object_id *id)
1677 const struct got_error *err = NULL;
1679 err = got_privsep_send_tag_req(pack->privsep_child->ibuf, -1, id,
1680 pack_idx);
1681 if (err)
1682 return err;
1684 return got_privsep_recv_tag(tag, pack->privsep_child->ibuf);
1687 static const struct got_error *
1688 read_packed_tag_privsep(struct got_tag_object **tag,
1689 struct got_pack *pack, struct got_packidx *packidx, int idx,
1690 struct got_object_id *id)
1692 const struct got_error *err = NULL;
1694 if (pack->privsep_child)
1695 return request_packed_tag(tag, pack, idx, id);
1697 err = start_pack_privsep_child(pack, packidx);
1698 if (err)
1699 return err;
1701 return request_packed_tag(tag, pack, idx, id);
1704 static const struct got_error *
1705 request_tag(struct got_tag_object **tag, struct got_repository *repo,
1706 int fd, struct got_object_id *id)
1708 const struct got_error *err = NULL;
1709 struct imsgbuf *ibuf;
1711 ibuf = repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TAG].ibuf;
1713 err = got_privsep_send_tag_req(ibuf, fd, id, -1);
1714 if (err)
1715 return err;
1717 return got_privsep_recv_tag(tag, ibuf);
1720 static const struct got_error *
1721 read_tag_privsep(struct got_tag_object **tag, int obj_fd,
1722 struct got_object_id *id, struct got_repository *repo)
1724 const struct got_error *err;
1725 int imsg_fds[2];
1726 pid_t pid;
1727 struct imsgbuf *ibuf;
1729 if (repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TAG].imsg_fd != -1)
1730 return request_tag(tag, repo, obj_fd, id);
1732 ibuf = calloc(1, sizeof(*ibuf));
1733 if (ibuf == NULL)
1734 return got_error_from_errno("calloc");
1736 if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_fds) == -1) {
1737 err = got_error_from_errno("socketpair");
1738 free(ibuf);
1739 return err;
1742 pid = fork();
1743 if (pid == -1) {
1744 err = got_error_from_errno("fork");
1745 free(ibuf);
1746 return err;
1748 else if (pid == 0) {
1749 got_privsep_exec_child(imsg_fds, GOT_PATH_PROG_READ_TAG,
1750 repo->path);
1751 /* not reached */
1754 if (close(imsg_fds[1]) == -1) {
1755 err = got_error_from_errno("close");
1756 free(ibuf);
1757 return err;
1759 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TAG].imsg_fd =
1760 imsg_fds[0];
1761 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TAG].pid = pid;
1762 imsg_init(ibuf, imsg_fds[0]);
1763 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TAG].ibuf = ibuf;
1765 return request_tag(tag, repo, obj_fd, id);
1768 static const struct got_error *
1769 open_tag(struct got_tag_object **tag, struct got_repository *repo,
1770 struct got_object_id *id, int check_cache)
1772 const struct got_error *err = NULL;
1773 struct got_packidx *packidx = NULL;
1774 int idx;
1775 char *path_packfile = NULL;
1776 struct got_object *obj = NULL;
1777 int obj_type = GOT_OBJ_TYPE_ANY;
1779 if (check_cache) {
1780 *tag = got_repo_get_cached_tag(repo, id);
1781 if (*tag != NULL) {
1782 (*tag)->refcnt++;
1783 return NULL;
1785 } else
1786 *tag = NULL;
1788 err = got_repo_search_packidx(&packidx, &idx, repo, id);
1789 if (err == NULL) {
1790 struct got_pack *pack = NULL;
1792 err = got_packidx_get_packfile_path(&path_packfile,
1793 packidx->path_packidx);
1794 if (err)
1795 return err;
1797 pack = got_repo_get_cached_pack(repo, path_packfile);
1798 if (pack == NULL) {
1799 err = got_repo_cache_pack(&pack, repo, path_packfile,
1800 packidx);
1801 if (err)
1802 goto done;
1805 /* Beware of "lightweight" tags: Check object type first. */
1806 err = read_packed_object_privsep(&obj, repo, pack, packidx,
1807 idx, id);
1808 if (err)
1809 goto done;
1810 obj_type = obj->type;
1811 got_object_close(obj);
1812 if (obj_type != GOT_OBJ_TYPE_TAG) {
1813 err = got_error(GOT_ERR_OBJ_TYPE);
1814 goto done;
1816 err = read_packed_tag_privsep(tag, pack, packidx, idx, id);
1817 } else if (err->code == GOT_ERR_NO_OBJ) {
1818 int fd;
1820 err = got_object_open_loose_fd(&fd, id, repo);
1821 if (err)
1822 return err;
1823 err = got_object_read_header_privsep(&obj, id, repo, fd);
1824 if (err)
1825 return err;
1826 obj_type = obj->type;
1827 got_object_close(obj);
1828 if (obj_type != GOT_OBJ_TYPE_TAG)
1829 return got_error(GOT_ERR_OBJ_TYPE);
1831 err = got_object_open_loose_fd(&fd, id, repo);
1832 if (err)
1833 return err;
1834 err = read_tag_privsep(tag, fd, id, repo);
1837 if (err == NULL) {
1838 (*tag)->refcnt++;
1839 err = got_repo_cache_tag(repo, id, *tag);
1841 done:
1842 free(path_packfile);
1843 return err;
1846 const struct got_error *
1847 got_object_open_as_tag(struct got_tag_object **tag,
1848 struct got_repository *repo, struct got_object_id *id)
1850 *tag = got_repo_get_cached_tag(repo, id);
1851 if (*tag != NULL) {
1852 (*tag)->refcnt++;
1853 return NULL;
1856 return open_tag(tag, repo, id, 0);
1859 const struct got_error *
1860 got_object_tag_open(struct got_tag_object **tag,
1861 struct got_repository *repo, struct got_object *obj)
1863 return open_tag(tag, repo, got_object_get_id(obj), 1);
1866 const char *
1867 got_object_tag_get_name(struct got_tag_object *tag)
1869 return tag->tag;
1872 int
1873 got_object_tag_get_object_type(struct got_tag_object *tag)
1875 return tag->obj_type;
1878 struct got_object_id *
1879 got_object_tag_get_object_id(struct got_tag_object *tag)
1881 return &tag->id;
1884 time_t
1885 got_object_tag_get_tagger_time(struct got_tag_object *tag)
1887 return tag->tagger_time;
1890 time_t
1891 got_object_tag_get_tagger_gmtoff(struct got_tag_object *tag)
1893 return tag->tagger_gmtoff;
1896 const char *
1897 got_object_tag_get_tagger(struct got_tag_object *tag)
1899 return tag->tagger;
1902 const char *
1903 got_object_tag_get_message(struct got_tag_object *tag)
1905 return tag->tagmsg;
1908 static struct got_tree_entry *
1909 find_entry_by_name(struct got_tree_object *tree, const char *name, size_t len)
1911 int i;
1913 /* Note that tree entries are sorted in strncmp() order. */
1914 for (i = 0; i < tree->nentries; i++) {
1915 struct got_tree_entry *te = &tree->entries[i];
1916 int cmp = strncmp(te->name, name, len);
1917 if (cmp < 0)
1918 continue;
1919 if (cmp > 0)
1920 break;
1921 if (te->name[len] == '\0')
1922 return te;
1924 return NULL;
1927 struct got_tree_entry *
1928 got_object_tree_find_entry(struct got_tree_object *tree, const char *name)
1930 return find_entry_by_name(tree, name, strlen(name));
1933 const struct got_error *
1934 got_object_tree_find_path(struct got_object_id **id, mode_t *mode,
1935 struct got_repository *repo, struct got_tree_object *tree,
1936 const char *path)
1938 const struct got_error *err = NULL;
1939 struct got_tree_object *subtree = NULL;
1940 struct got_tree_entry *te = NULL;
1941 const char *seg, *s;
1942 size_t seglen;
1944 *id = NULL;
1946 s = path;
1947 while (s[0] == '/')
1948 s++;
1949 seg = s;
1950 seglen = 0;
1951 subtree = tree;
1952 while (*s) {
1953 struct got_tree_object *next_tree;
1955 if (*s != '/') {
1956 s++;
1957 seglen++;
1958 if (*s)
1959 continue;
1962 te = find_entry_by_name(subtree, seg, seglen);
1963 if (te == NULL) {
1964 err = got_error_path(path, GOT_ERR_NO_TREE_ENTRY);
1965 goto done;
1968 if (*s == '\0')
1969 break;
1971 seg = s + 1;
1972 seglen = 0;
1973 s++;
1974 if (*s) {
1975 err = got_object_open_as_tree(&next_tree, repo,
1976 &te->id);
1977 te = NULL;
1978 if (err)
1979 goto done;
1980 if (subtree != tree)
1981 got_object_tree_close(subtree);
1982 subtree = next_tree;
1986 if (te) {
1987 *id = got_object_id_dup(&te->id);
1988 if (*id == NULL)
1989 return got_error_from_errno("got_object_id_dup");
1990 if (mode)
1991 *mode = te->mode;
1992 } else
1993 err = got_error_path(path, GOT_ERR_NO_TREE_ENTRY);
1994 done:
1995 if (subtree && subtree != tree)
1996 got_object_tree_close(subtree);
1997 return err;
1999 const struct got_error *
2000 got_object_id_by_path(struct got_object_id **id, struct got_repository *repo,
2001 struct got_commit_object *commit, const char *path)
2003 const struct got_error *err = NULL;
2004 struct got_tree_object *tree = NULL;
2006 *id = NULL;
2008 /* Handle opening of root of commit's tree. */
2009 if (got_path_is_root_dir(path)) {
2010 *id = got_object_id_dup(commit->tree_id);
2011 if (*id == NULL)
2012 err = got_error_from_errno("got_object_id_dup");
2013 } else {
2014 err = got_object_open_as_tree(&tree, repo, commit->tree_id);
2015 if (err)
2016 goto done;
2017 err = got_object_tree_find_path(id, NULL, repo, tree, path);
2019 done:
2020 if (tree)
2021 got_object_tree_close(tree);
2022 return err;
2026 * Normalize file mode bits to avoid false positive tree entry differences
2027 * in case tree entries have unexpected mode bits set.
2029 static mode_t
2030 normalize_mode_for_comparison(mode_t mode)
2033 * For directories, the only relevant bit is the IFDIR bit.
2034 * This allows us to detect paths changing from a directory
2035 * to a file and vice versa.
2037 if (S_ISDIR(mode))
2038 return mode & S_IFDIR;
2041 * For symlinks, the only relevant bit is the IFLNK bit.
2042 * This allows us to detect paths changing from a symlinks
2043 * to a file or directory and vice versa.
2045 if (S_ISLNK(mode))
2046 return mode & S_IFLNK;
2048 /* For files, the only change we care about is the executable bit. */
2049 return mode & S_IXUSR;
2052 const struct got_error *
2053 got_object_tree_path_changed(int *changed,
2054 struct got_tree_object *tree01, struct got_tree_object *tree02,
2055 const char *path, struct got_repository *repo)
2057 const struct got_error *err = NULL;
2058 struct got_tree_object *tree1 = NULL, *tree2 = NULL;
2059 struct got_tree_entry *te1 = NULL, *te2 = NULL;
2060 const char *seg, *s;
2061 size_t seglen;
2063 *changed = 0;
2065 /* We not do support comparing the root path. */
2066 if (got_path_is_root_dir(path))
2067 return got_error_path(path, GOT_ERR_BAD_PATH);
2069 tree1 = tree01;
2070 tree2 = tree02;
2071 s = path;
2072 while (*s == '/')
2073 s++;
2074 seg = s;
2075 seglen = 0;
2076 while (*s) {
2077 struct got_tree_object *next_tree1, *next_tree2;
2078 mode_t mode1, mode2;
2080 if (*s != '/') {
2081 s++;
2082 seglen++;
2083 if (*s)
2084 continue;
2087 te1 = find_entry_by_name(tree1, seg, seglen);
2088 if (te1 == NULL) {
2089 err = got_error(GOT_ERR_NO_OBJ);
2090 goto done;
2093 if (tree2)
2094 te2 = find_entry_by_name(tree2, seg, seglen);
2096 if (te2) {
2097 mode1 = normalize_mode_for_comparison(te1->mode);
2098 mode2 = normalize_mode_for_comparison(te2->mode);
2099 if (mode1 != mode2) {
2100 *changed = 1;
2101 goto done;
2104 if (got_object_id_cmp(&te1->id, &te2->id) == 0) {
2105 *changed = 0;
2106 goto done;
2110 if (*s == '\0') { /* final path element */
2111 *changed = 1;
2112 goto done;
2115 seg = s + 1;
2116 s++;
2117 seglen = 0;
2118 if (*s) {
2119 err = got_object_open_as_tree(&next_tree1, repo,
2120 &te1->id);
2121 te1 = NULL;
2122 if (err)
2123 goto done;
2124 if (tree1 != tree01)
2125 got_object_tree_close(tree1);
2126 tree1 = next_tree1;
2128 if (te2) {
2129 err = got_object_open_as_tree(&next_tree2, repo,
2130 &te2->id);
2131 te2 = NULL;
2132 if (err)
2133 goto done;
2134 if (tree2 != tree02)
2135 got_object_tree_close(tree2);
2136 tree2 = next_tree2;
2137 } else if (tree2) {
2138 if (tree2 != tree02)
2139 got_object_tree_close(tree2);
2140 tree2 = NULL;
2144 done:
2145 if (tree1 && tree1 != tree01)
2146 got_object_tree_close(tree1);
2147 if (tree2 && tree2 != tree02)
2148 got_object_tree_close(tree2);
2149 return err;
2152 const struct got_error *
2153 got_object_tree_entry_dup(struct got_tree_entry **new_te,
2154 struct got_tree_entry *te)
2156 const struct got_error *err = NULL;
2158 *new_te = calloc(1, sizeof(**new_te));
2159 if (*new_te == NULL)
2160 return got_error_from_errno("calloc");
2162 (*new_te)->mode = te->mode;
2163 memcpy((*new_te)->name, te->name, sizeof((*new_te)->name));
2164 memcpy(&(*new_te)->id, &te->id, sizeof((*new_te)->id));
2165 return err;
2168 int
2169 got_object_tree_entry_is_submodule(struct got_tree_entry *te)
2171 return (te->mode & S_IFMT) == (S_IFDIR | S_IFLNK);
2174 int
2175 got_object_tree_entry_is_symlink(struct got_tree_entry *te)
2177 /* S_IFDIR check avoids confusing symlinks with submodules. */
2178 return ((te->mode & (S_IFDIR | S_IFLNK)) == S_IFLNK);
2181 static const struct got_error *
2182 resolve_symlink(char **link_target, const char *path,
2183 struct got_commit_object *commit, struct got_repository *repo)
2185 const struct got_error *err = NULL;
2186 char buf[PATH_MAX];
2187 char *name, *parent_path = NULL;
2188 struct got_object_id *tree_obj_id = NULL;
2189 struct got_tree_object *tree = NULL;
2190 struct got_tree_entry *te = NULL;
2192 *link_target = NULL;
2194 if (strlcpy(buf, path, sizeof(buf)) >= sizeof(buf))
2195 return got_error(GOT_ERR_NO_SPACE);
2197 name = basename(buf);
2198 if (name == NULL)
2199 return got_error_from_errno2("basename", path);
2201 err = got_path_dirname(&parent_path, path);
2202 if (err)
2203 return err;
2205 err = got_object_id_by_path(&tree_obj_id, repo, commit,
2206 parent_path);
2207 if (err) {
2208 if (err->code == GOT_ERR_NO_TREE_ENTRY) {
2209 /* Display the complete path in error message. */
2210 err = got_error_path(path, err->code);
2212 goto done;
2215 err = got_object_open_as_tree(&tree, repo, tree_obj_id);
2216 if (err)
2217 goto done;
2219 te = got_object_tree_find_entry(tree, name);
2220 if (te == NULL) {
2221 err = got_error_path(path, GOT_ERR_NO_TREE_ENTRY);
2222 goto done;
2225 if (got_object_tree_entry_is_symlink(te)) {
2226 err = got_tree_entry_get_symlink_target(link_target, te, repo);
2227 if (err)
2228 goto done;
2229 if (!got_path_is_absolute(*link_target)) {
2230 char *abspath;
2231 if (asprintf(&abspath, "%s/%s", parent_path,
2232 *link_target) == -1) {
2233 err = got_error_from_errno("asprintf");
2234 goto done;
2236 free(*link_target);
2237 *link_target = malloc(PATH_MAX);
2238 if (*link_target == NULL) {
2239 err = got_error_from_errno("malloc");
2240 goto done;
2242 err = got_canonpath(abspath, *link_target, PATH_MAX);
2243 free(abspath);
2244 if (err)
2245 goto done;
2248 done:
2249 free(tree_obj_id);
2250 if (tree)
2251 got_object_tree_close(tree);
2252 if (err) {
2253 free(*link_target);
2254 *link_target = NULL;
2256 return err;
2259 const struct got_error *
2260 got_object_resolve_symlinks(char **link_target, const char *path,
2261 struct got_commit_object *commit, struct got_repository *repo)
2263 const struct got_error *err = NULL;
2264 char *next_target = NULL;
2265 int max_recursion = 40; /* matches Git */
2267 *link_target = NULL;
2269 do {
2270 err = resolve_symlink(&next_target,
2271 *link_target ? *link_target : path, commit, repo);
2272 if (err)
2273 break;
2274 if (next_target) {
2275 free(*link_target);
2276 if (--max_recursion == 0) {
2277 err = got_error_path(path, GOT_ERR_RECURSION);
2278 *link_target = NULL;
2279 break;
2281 *link_target = next_target;
2283 } while (next_target);
2285 return err;
2288 const struct got_error *
2289 got_traverse_packed_commits(struct got_object_id_queue *traversed_commits,
2290 struct got_object_id *commit_id, const char *path,
2291 struct got_repository *repo)
2293 const struct got_error *err = NULL;
2294 struct got_pack *pack = NULL;
2295 struct got_packidx *packidx = NULL;
2296 char *path_packfile = NULL;
2297 struct got_commit_object *changed_commit = NULL;
2298 struct got_object_id *changed_commit_id = NULL;
2299 int idx;
2301 err = got_repo_search_packidx(&packidx, &idx, repo, commit_id);
2302 if (err) {
2303 if (err->code != GOT_ERR_NO_OBJ)
2304 return err;
2305 return NULL;
2308 err = got_packidx_get_packfile_path(&path_packfile,
2309 packidx->path_packidx);
2310 if (err)
2311 return err;
2313 pack = got_repo_get_cached_pack(repo, path_packfile);
2314 if (pack == NULL) {
2315 err = got_repo_cache_pack(&pack, repo, path_packfile, packidx);
2316 if (err)
2317 goto done;
2320 if (pack->privsep_child == NULL) {
2321 err = start_pack_privsep_child(pack, packidx);
2322 if (err)
2323 goto done;
2326 err = got_privsep_send_commit_traversal_request(
2327 pack->privsep_child->ibuf, commit_id, idx, path);
2328 if (err)
2329 goto done;
2331 err = got_privsep_recv_traversed_commits(&changed_commit,
2332 &changed_commit_id, traversed_commits, pack->privsep_child->ibuf);
2333 if (err)
2334 goto done;
2336 if (changed_commit) {
2338 * Cache the commit in which the path was changed.
2339 * This commit might be opened again soon.
2341 changed_commit->refcnt++;
2342 err = got_repo_cache_commit(repo, changed_commit_id,
2343 changed_commit);
2344 got_object_commit_close(changed_commit);
2346 done:
2347 free(path_packfile);
2348 free(changed_commit_id);
2349 return err;