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_compressed_size, off_t *delta_offset,
390 off_t *delta_out_offset, 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_compressed_size = 0;
402 *delta_offset = 0;
403 *delta_out_offset = 0;
405 err = got_packidx_get_packfile_path(&path_packfile,
406 packidx->path_packidx);
407 if (err)
408 return err;
410 pack = got_repo_get_cached_pack(repo, path_packfile);
411 if (pack == NULL) {
412 err = got_repo_cache_pack(&pack, repo, path_packfile, packidx);
413 if (err)
414 return err;
417 if (pack->privsep_child == NULL) {
418 err = start_pack_privsep_child(pack, packidx);
419 if (err)
420 return err;
423 if (!pack->child_has_delta_outfd) {
424 int outfd_child;
425 outfd_child = dup(delta_cache_fd);
426 if (outfd_child == -1)
427 return got_error_from_errno("dup");
428 err = got_privsep_send_raw_delta_outfd(
429 pack->privsep_child->ibuf, outfd_child);
430 if (err)
431 return err;
432 pack->child_has_delta_outfd = 1;
435 err = got_privsep_send_raw_delta_req(pack->privsep_child->ibuf,
436 obj_idx, id);
437 if (err)
438 return err;
440 return got_privsep_recv_raw_delta(base_size, result_size, delta_size,
441 delta_compressed_size, delta_offset, delta_out_offset, base_id,
442 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 *qid = calloc(1, sizeof(**qid));
934 if (*qid == NULL)
935 return got_error_from_errno("calloc");
937 memcpy(&(*qid)->id, id, sizeof((*qid)->id));
938 return NULL;
941 const struct got_error *
942 got_object_id_queue_copy(const struct got_object_id_queue *src,
943 struct got_object_id_queue *dest)
945 const struct got_error *err;
946 struct got_object_qid *qid;
948 STAILQ_FOREACH(qid, src, entry) {
949 struct got_object_qid *new;
950 /*
951 * Deep-copy the object ID only. Let the caller deal
952 * with setting up the new->data pointer if needed.
953 */
954 err = got_object_qid_alloc(&new, &qid->id);
955 if (err) {
956 got_object_id_queue_free(dest);
957 return err;
959 STAILQ_INSERT_TAIL(dest, new, entry);
962 return NULL;
965 static const struct got_error *
966 request_packed_tree(struct got_tree_object **tree, struct got_pack *pack,
967 int pack_idx, struct got_object_id *id)
969 const struct got_error *err = NULL;
971 err = got_privsep_send_tree_req(pack->privsep_child->ibuf, -1, id,
972 pack_idx);
973 if (err)
974 return err;
976 return got_privsep_recv_tree(tree, pack->privsep_child->ibuf);
979 static const struct got_error *
980 read_packed_tree_privsep(struct got_tree_object **tree,
981 struct got_pack *pack, struct got_packidx *packidx, int idx,
982 struct got_object_id *id)
984 const struct got_error *err = NULL;
986 if (pack->privsep_child)
987 return request_packed_tree(tree, pack, idx, id);
989 err = start_pack_privsep_child(pack, packidx);
990 if (err)
991 return err;
993 return request_packed_tree(tree, pack, idx, id);
996 static const struct got_error *
997 request_tree(struct got_tree_object **tree, struct got_repository *repo,
998 int fd, struct got_object_id *id)
1000 const struct got_error *err = NULL;
1001 struct imsgbuf *ibuf;
1003 ibuf = repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TREE].ibuf;
1005 err = got_privsep_send_tree_req(ibuf, fd, id, -1);
1006 if (err)
1007 return err;
1009 return got_privsep_recv_tree(tree, ibuf);
1012 const struct got_error *
1013 read_tree_privsep(struct got_tree_object **tree, int obj_fd,
1014 struct got_object_id *id, struct got_repository *repo)
1016 const struct got_error *err;
1017 int imsg_fds[2];
1018 pid_t pid;
1019 struct imsgbuf *ibuf;
1021 if (repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TREE].imsg_fd != -1)
1022 return request_tree(tree, repo, obj_fd, id);
1024 ibuf = calloc(1, sizeof(*ibuf));
1025 if (ibuf == NULL)
1026 return got_error_from_errno("calloc");
1028 if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_fds) == -1) {
1029 err = got_error_from_errno("socketpair");
1030 free(ibuf);
1031 return err;
1034 pid = fork();
1035 if (pid == -1) {
1036 err = got_error_from_errno("fork");
1037 free(ibuf);
1038 return err;
1040 else if (pid == 0) {
1041 got_privsep_exec_child(imsg_fds, GOT_PATH_PROG_READ_TREE,
1042 repo->path);
1043 /* not reached */
1046 if (close(imsg_fds[1]) == -1) {
1047 err = got_error_from_errno("close");
1048 free(ibuf);
1049 return err;
1051 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TREE].imsg_fd =
1052 imsg_fds[0];
1053 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TREE].pid = pid;
1054 imsg_init(ibuf, imsg_fds[0]);
1055 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TREE].ibuf = ibuf;
1058 return request_tree(tree, repo, obj_fd, id);
1061 static const struct got_error *
1062 open_tree(struct got_tree_object **tree, struct got_repository *repo,
1063 struct got_object_id *id, int check_cache)
1065 const struct got_error *err = NULL;
1066 struct got_packidx *packidx = NULL;
1067 int idx;
1068 char *path_packfile = NULL;
1070 if (check_cache) {
1071 *tree = got_repo_get_cached_tree(repo, id);
1072 if (*tree != NULL) {
1073 (*tree)->refcnt++;
1074 return NULL;
1076 } else
1077 *tree = NULL;
1079 err = got_repo_search_packidx(&packidx, &idx, repo, id);
1080 if (err == NULL) {
1081 struct got_pack *pack = NULL;
1083 err = got_packidx_get_packfile_path(&path_packfile,
1084 packidx->path_packidx);
1085 if (err)
1086 return err;
1088 pack = got_repo_get_cached_pack(repo, path_packfile);
1089 if (pack == NULL) {
1090 err = got_repo_cache_pack(&pack, repo, path_packfile,
1091 packidx);
1092 if (err)
1093 goto done;
1095 err = read_packed_tree_privsep(tree, pack,
1096 packidx, idx, id);
1097 } else if (err->code == GOT_ERR_NO_OBJ) {
1098 int fd;
1100 err = got_object_open_loose_fd(&fd, id, repo);
1101 if (err)
1102 return err;
1103 err = read_tree_privsep(tree, fd, id, repo);
1106 if (err == NULL) {
1107 (*tree)->refcnt++;
1108 err = got_repo_cache_tree(repo, id, *tree);
1110 done:
1111 free(path_packfile);
1112 return err;
1115 const struct got_error *
1116 got_object_open_as_tree(struct got_tree_object **tree,
1117 struct got_repository *repo, struct got_object_id *id)
1119 *tree = got_repo_get_cached_tree(repo, id);
1120 if (*tree != NULL) {
1121 (*tree)->refcnt++;
1122 return NULL;
1125 return open_tree(tree, repo, id, 0);
1128 const struct got_error *
1129 got_object_tree_open(struct got_tree_object **tree,
1130 struct got_repository *repo, struct got_object *obj)
1132 return open_tree(tree, repo, got_object_get_id(obj), 1);
1135 int
1136 got_object_tree_get_nentries(struct got_tree_object *tree)
1138 return tree->nentries;
1141 struct got_tree_entry *
1142 got_object_tree_get_first_entry(struct got_tree_object *tree)
1144 return got_object_tree_get_entry(tree, 0);
1147 struct got_tree_entry *
1148 got_object_tree_get_last_entry(struct got_tree_object *tree)
1150 return got_object_tree_get_entry(tree, tree->nentries - 1);
1153 struct got_tree_entry *
1154 got_object_tree_get_entry(struct got_tree_object *tree, int i)
1156 if (i < 0 || i >= tree->nentries)
1157 return NULL;
1158 return &tree->entries[i];
1161 mode_t
1162 got_tree_entry_get_mode(struct got_tree_entry *te)
1164 return te->mode;
1167 const char *
1168 got_tree_entry_get_name(struct got_tree_entry *te)
1170 return &te->name[0];
1173 struct got_object_id *
1174 got_tree_entry_get_id(struct got_tree_entry *te)
1176 return &te->id;
1179 const struct got_error *
1180 got_object_blob_read_to_str(char **s, struct got_blob_object *blob)
1182 const struct got_error *err = NULL;
1183 size_t len, totlen, hdrlen, offset;
1185 *s = NULL;
1187 hdrlen = got_object_blob_get_hdrlen(blob);
1188 totlen = 0;
1189 offset = 0;
1190 do {
1191 char *p;
1193 err = got_object_blob_read_block(&len, blob);
1194 if (err)
1195 return err;
1197 if (len == 0)
1198 break;
1200 totlen += len - hdrlen;
1201 p = realloc(*s, totlen + 1);
1202 if (p == NULL) {
1203 err = got_error_from_errno("realloc");
1204 free(*s);
1205 *s = NULL;
1206 return err;
1208 *s = p;
1209 /* Skip blob object header first time around. */
1210 memcpy(*s + offset,
1211 got_object_blob_get_read_buf(blob) + hdrlen, len - hdrlen);
1212 hdrlen = 0;
1213 offset = totlen;
1214 } while (len > 0);
1216 (*s)[totlen] = '\0';
1217 return NULL;
1220 const struct got_error *
1221 got_tree_entry_get_symlink_target(char **link_target, struct got_tree_entry *te,
1222 struct got_repository *repo)
1224 const struct got_error *err = NULL;
1225 struct got_blob_object *blob = NULL;
1227 *link_target = NULL;
1229 if (!got_object_tree_entry_is_symlink(te))
1230 return got_error(GOT_ERR_TREE_ENTRY_TYPE);
1232 err = got_object_open_as_blob(&blob, repo,
1233 got_tree_entry_get_id(te), PATH_MAX);
1234 if (err)
1235 return err;
1237 err = got_object_blob_read_to_str(link_target, blob);
1238 got_object_blob_close(blob);
1239 if (err) {
1240 free(*link_target);
1241 *link_target = NULL;
1243 return err;
1246 int
1247 got_tree_entry_get_index(struct got_tree_entry *te)
1249 return te->idx;
1252 struct got_tree_entry *
1253 got_tree_entry_get_next(struct got_tree_object *tree,
1254 struct got_tree_entry *te)
1256 return got_object_tree_get_entry(tree, te->idx + 1);
1259 struct got_tree_entry *
1260 got_tree_entry_get_prev(struct got_tree_object *tree,
1261 struct got_tree_entry *te)
1263 return got_object_tree_get_entry(tree, te->idx - 1);
1266 static const struct got_error *
1267 request_packed_blob(uint8_t **outbuf, size_t *size, size_t *hdrlen, int outfd,
1268 struct got_pack *pack, struct got_packidx *packidx, int idx,
1269 struct got_object_id *id)
1271 const struct got_error *err = NULL;
1272 struct imsgbuf *ibuf = pack->privsep_child->ibuf;
1273 int outfd_child;
1275 err = pack_child_send_tempfiles(ibuf, pack);
1276 if (err)
1277 return err;
1279 outfd_child = dup(outfd);
1280 if (outfd_child == -1)
1281 return got_error_from_errno("dup");
1283 err = got_privsep_send_blob_req(pack->privsep_child->ibuf, -1, id, idx);
1284 if (err)
1285 return err;
1287 err = got_privsep_send_blob_outfd(pack->privsep_child->ibuf,
1288 outfd_child);
1289 if (err) {
1290 return err;
1293 err = got_privsep_recv_blob(outbuf, size, hdrlen,
1294 pack->privsep_child->ibuf);
1295 if (err)
1296 return err;
1298 if (lseek(outfd, SEEK_SET, 0) == -1)
1299 err = got_error_from_errno("lseek");
1301 return err;
1304 static const struct got_error *
1305 read_packed_blob_privsep(uint8_t **outbuf, size_t *size, size_t *hdrlen,
1306 int outfd, struct got_pack *pack, struct got_packidx *packidx, int idx,
1307 struct got_object_id *id)
1309 const struct got_error *err = NULL;
1311 if (pack->privsep_child == NULL) {
1312 err = start_pack_privsep_child(pack, packidx);
1313 if (err)
1314 return err;
1317 return request_packed_blob(outbuf, size, hdrlen, outfd, pack, packidx,
1318 idx, id);
1321 static const struct got_error *
1322 request_blob(uint8_t **outbuf, size_t *size, size_t *hdrlen, int outfd,
1323 int infd, struct got_object_id *id, struct imsgbuf *ibuf)
1325 const struct got_error *err = NULL;
1326 int outfd_child;
1328 outfd_child = dup(outfd);
1329 if (outfd_child == -1)
1330 return got_error_from_errno("dup");
1332 err = got_privsep_send_blob_req(ibuf, infd, id, -1);
1333 if (err)
1334 return err;
1336 err = got_privsep_send_blob_outfd(ibuf, outfd_child);
1337 if (err)
1338 return err;
1340 err = got_privsep_recv_blob(outbuf, size, hdrlen, ibuf);
1341 if (err)
1342 return err;
1344 if (lseek(outfd, SEEK_SET, 0) == -1)
1345 return got_error_from_errno("lseek");
1347 return err;
1350 static const struct got_error *
1351 read_blob_privsep(uint8_t **outbuf, size_t *size, size_t *hdrlen,
1352 int outfd, int infd, struct got_object_id *id, struct got_repository *repo)
1354 const struct got_error *err;
1355 int imsg_fds[2];
1356 pid_t pid;
1357 struct imsgbuf *ibuf;
1359 if (repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_BLOB].imsg_fd != -1) {
1360 ibuf = repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_BLOB].ibuf;
1361 return request_blob(outbuf, size, hdrlen, outfd, infd, id,
1362 ibuf);
1365 ibuf = calloc(1, sizeof(*ibuf));
1366 if (ibuf == NULL)
1367 return got_error_from_errno("calloc");
1369 if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_fds) == -1) {
1370 err = got_error_from_errno("socketpair");
1371 free(ibuf);
1372 return err;
1375 pid = fork();
1376 if (pid == -1) {
1377 err = got_error_from_errno("fork");
1378 free(ibuf);
1379 return err;
1381 else if (pid == 0) {
1382 got_privsep_exec_child(imsg_fds, GOT_PATH_PROG_READ_BLOB,
1383 repo->path);
1384 /* not reached */
1387 if (close(imsg_fds[1]) == -1) {
1388 err = got_error_from_errno("close");
1389 free(ibuf);
1390 return err;
1392 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_BLOB].imsg_fd =
1393 imsg_fds[0];
1394 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_BLOB].pid = pid;
1395 imsg_init(ibuf, imsg_fds[0]);
1396 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_BLOB].ibuf = ibuf;
1398 return request_blob(outbuf, size, hdrlen, outfd, infd, id, ibuf);
1401 static const struct got_error *
1402 open_blob(struct got_blob_object **blob, struct got_repository *repo,
1403 struct got_object_id *id, size_t blocksize)
1405 const struct got_error *err = NULL;
1406 struct got_packidx *packidx = NULL;
1407 int idx;
1408 char *path_packfile = NULL;
1409 uint8_t *outbuf;
1410 int outfd;
1411 size_t size, hdrlen;
1412 struct stat sb;
1414 *blob = calloc(1, sizeof(**blob));
1415 if (*blob == NULL)
1416 return got_error_from_errno("calloc");
1418 outfd = got_opentempfd();
1419 if (outfd == -1)
1420 return got_error_from_errno("got_opentempfd");
1422 (*blob)->read_buf = malloc(blocksize);
1423 if ((*blob)->read_buf == NULL) {
1424 err = got_error_from_errno("malloc");
1425 goto done;
1428 err = got_repo_search_packidx(&packidx, &idx, repo, id);
1429 if (err == NULL) {
1430 struct got_pack *pack = NULL;
1432 err = got_packidx_get_packfile_path(&path_packfile,
1433 packidx->path_packidx);
1434 if (err)
1435 goto done;
1437 pack = got_repo_get_cached_pack(repo, path_packfile);
1438 if (pack == NULL) {
1439 err = got_repo_cache_pack(&pack, repo, path_packfile,
1440 packidx);
1441 if (err)
1442 goto done;
1444 err = read_packed_blob_privsep(&outbuf, &size, &hdrlen, outfd,
1445 pack, packidx, idx, id);
1446 } else if (err->code == GOT_ERR_NO_OBJ) {
1447 int infd;
1449 err = got_object_open_loose_fd(&infd, id, repo);
1450 if (err)
1451 goto done;
1452 err = read_blob_privsep(&outbuf, &size, &hdrlen, outfd, infd,
1453 id, repo);
1455 if (err)
1456 goto done;
1458 if (hdrlen > size) {
1459 err = got_error(GOT_ERR_BAD_OBJ_HDR);
1460 goto done;
1463 if (outbuf) {
1464 if (close(outfd) == -1 && err == NULL)
1465 err = got_error_from_errno("close");
1466 outfd = -1;
1467 (*blob)->f = fmemopen(outbuf, size, "rb");
1468 if ((*blob)->f == NULL) {
1469 err = got_error_from_errno("fmemopen");
1470 free(outbuf);
1471 goto done;
1473 (*blob)->data = outbuf;
1474 } else {
1475 if (fstat(outfd, &sb) == -1) {
1476 err = got_error_from_errno("fstat");
1477 goto done;
1480 if (sb.st_size != size) {
1481 err = got_error(GOT_ERR_PRIVSEP_LEN);
1482 goto done;
1485 (*blob)->f = fdopen(outfd, "rb");
1486 if ((*blob)->f == NULL) {
1487 err = got_error_from_errno("fdopen");
1488 close(outfd);
1489 outfd = -1;
1490 goto done;
1494 (*blob)->hdrlen = hdrlen;
1495 (*blob)->blocksize = blocksize;
1496 memcpy(&(*blob)->id.sha1, id->sha1, SHA1_DIGEST_LENGTH);
1498 done:
1499 free(path_packfile);
1500 if (err) {
1501 if (*blob) {
1502 got_object_blob_close(*blob);
1503 *blob = NULL;
1504 } else if (outfd != -1)
1505 close(outfd);
1507 return err;
1510 const struct got_error *
1511 got_object_open_as_blob(struct got_blob_object **blob,
1512 struct got_repository *repo, struct got_object_id *id,
1513 size_t blocksize)
1515 return open_blob(blob, repo, id, blocksize);
1518 const struct got_error *
1519 got_object_blob_open(struct got_blob_object **blob,
1520 struct got_repository *repo, struct got_object *obj, size_t blocksize)
1522 return open_blob(blob, repo, got_object_get_id(obj), blocksize);
1525 const struct got_error *
1526 got_object_blob_close(struct got_blob_object *blob)
1528 const struct got_error *err = NULL;
1529 free(blob->read_buf);
1530 if (blob->f && fclose(blob->f) == EOF)
1531 err = got_error_from_errno("fclose");
1532 free(blob->data);
1533 free(blob);
1534 return err;
1537 void
1538 got_object_blob_rewind(struct got_blob_object *blob)
1540 if (blob->f)
1541 rewind(blob->f);
1544 char *
1545 got_object_blob_id_str(struct got_blob_object *blob, char *buf, size_t size)
1547 return got_sha1_digest_to_str(blob->id.sha1, buf, size);
1550 size_t
1551 got_object_blob_get_hdrlen(struct got_blob_object *blob)
1553 return blob->hdrlen;
1556 const uint8_t *
1557 got_object_blob_get_read_buf(struct got_blob_object *blob)
1559 return blob->read_buf;
1562 const struct got_error *
1563 got_object_blob_read_block(size_t *outlenp, struct got_blob_object *blob)
1565 size_t n;
1567 n = fread(blob->read_buf, 1, blob->blocksize, blob->f);
1568 if (n == 0 && ferror(blob->f))
1569 return got_ferror(blob->f, GOT_ERR_IO);
1570 *outlenp = n;
1571 return NULL;
1574 const struct got_error *
1575 got_object_blob_dump_to_file(off_t *filesize, int *nlines,
1576 off_t **line_offsets, FILE *outfile, struct got_blob_object *blob)
1578 const struct got_error *err = NULL;
1579 size_t n, len, hdrlen;
1580 const uint8_t *buf;
1581 int i;
1582 const int alloc_chunksz = 512;
1583 size_t nalloc = 0;
1584 off_t off = 0, total_len = 0;
1586 if (line_offsets)
1587 *line_offsets = NULL;
1588 if (filesize)
1589 *filesize = 0;
1590 if (nlines)
1591 *nlines = 0;
1593 hdrlen = got_object_blob_get_hdrlen(blob);
1594 do {
1595 err = got_object_blob_read_block(&len, blob);
1596 if (err)
1597 return err;
1598 if (len == 0)
1599 break;
1600 buf = got_object_blob_get_read_buf(blob);
1601 i = hdrlen;
1602 if (nlines) {
1603 if (line_offsets && *line_offsets == NULL) {
1604 /* Have some data but perhaps no '\n'. */
1605 *nlines = 1;
1606 nalloc = alloc_chunksz;
1607 *line_offsets = calloc(nalloc,
1608 sizeof(**line_offsets));
1609 if (*line_offsets == NULL)
1610 return got_error_from_errno("calloc");
1612 /* Skip forward over end of first line. */
1613 while (i < len) {
1614 if (buf[i] == '\n')
1615 break;
1616 i++;
1619 /* Scan '\n' offsets in remaining chunk of data. */
1620 while (i < len) {
1621 if (buf[i] != '\n') {
1622 i++;
1623 continue;
1625 (*nlines)++;
1626 if (line_offsets && nalloc < *nlines) {
1627 size_t n = *nlines + alloc_chunksz;
1628 off_t *o = recallocarray(*line_offsets,
1629 nalloc, n, sizeof(**line_offsets));
1630 if (o == NULL) {
1631 free(*line_offsets);
1632 *line_offsets = NULL;
1633 return got_error_from_errno(
1634 "recallocarray");
1636 *line_offsets = o;
1637 nalloc = n;
1639 if (line_offsets) {
1640 off = total_len + i - hdrlen + 1;
1641 (*line_offsets)[*nlines - 1] = off;
1643 i++;
1646 /* Skip blob object header first time around. */
1647 n = fwrite(buf + hdrlen, 1, len - hdrlen, outfile);
1648 if (n != len - hdrlen)
1649 return got_ferror(outfile, GOT_ERR_IO);
1650 total_len += len - hdrlen;
1651 hdrlen = 0;
1652 } while (len != 0);
1654 if (fflush(outfile) != 0)
1655 return got_error_from_errno("fflush");
1656 rewind(outfile);
1658 if (filesize)
1659 *filesize = total_len;
1661 return NULL;
1664 static const struct got_error *
1665 request_packed_tag(struct got_tag_object **tag, struct got_pack *pack,
1666 int pack_idx, struct got_object_id *id)
1668 const struct got_error *err = NULL;
1670 err = got_privsep_send_tag_req(pack->privsep_child->ibuf, -1, id,
1671 pack_idx);
1672 if (err)
1673 return err;
1675 return got_privsep_recv_tag(tag, pack->privsep_child->ibuf);
1678 static const struct got_error *
1679 read_packed_tag_privsep(struct got_tag_object **tag,
1680 struct got_pack *pack, struct got_packidx *packidx, int idx,
1681 struct got_object_id *id)
1683 const struct got_error *err = NULL;
1685 if (pack->privsep_child)
1686 return request_packed_tag(tag, pack, idx, id);
1688 err = start_pack_privsep_child(pack, packidx);
1689 if (err)
1690 return err;
1692 return request_packed_tag(tag, pack, idx, id);
1695 static const struct got_error *
1696 request_tag(struct got_tag_object **tag, struct got_repository *repo,
1697 int fd, struct got_object_id *id)
1699 const struct got_error *err = NULL;
1700 struct imsgbuf *ibuf;
1702 ibuf = repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TAG].ibuf;
1704 err = got_privsep_send_tag_req(ibuf, fd, id, -1);
1705 if (err)
1706 return err;
1708 return got_privsep_recv_tag(tag, ibuf);
1711 static const struct got_error *
1712 read_tag_privsep(struct got_tag_object **tag, int obj_fd,
1713 struct got_object_id *id, struct got_repository *repo)
1715 const struct got_error *err;
1716 int imsg_fds[2];
1717 pid_t pid;
1718 struct imsgbuf *ibuf;
1720 if (repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TAG].imsg_fd != -1)
1721 return request_tag(tag, repo, obj_fd, id);
1723 ibuf = calloc(1, sizeof(*ibuf));
1724 if (ibuf == NULL)
1725 return got_error_from_errno("calloc");
1727 if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_fds) == -1) {
1728 err = got_error_from_errno("socketpair");
1729 free(ibuf);
1730 return err;
1733 pid = fork();
1734 if (pid == -1) {
1735 err = got_error_from_errno("fork");
1736 free(ibuf);
1737 return err;
1739 else if (pid == 0) {
1740 got_privsep_exec_child(imsg_fds, GOT_PATH_PROG_READ_TAG,
1741 repo->path);
1742 /* not reached */
1745 if (close(imsg_fds[1]) == -1) {
1746 err = got_error_from_errno("close");
1747 free(ibuf);
1748 return err;
1750 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TAG].imsg_fd =
1751 imsg_fds[0];
1752 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TAG].pid = pid;
1753 imsg_init(ibuf, imsg_fds[0]);
1754 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TAG].ibuf = ibuf;
1756 return request_tag(tag, repo, obj_fd, id);
1759 static const struct got_error *
1760 open_tag(struct got_tag_object **tag, struct got_repository *repo,
1761 struct got_object_id *id, int check_cache)
1763 const struct got_error *err = NULL;
1764 struct got_packidx *packidx = NULL;
1765 int idx;
1766 char *path_packfile = NULL;
1767 struct got_object *obj = NULL;
1768 int obj_type = GOT_OBJ_TYPE_ANY;
1770 if (check_cache) {
1771 *tag = got_repo_get_cached_tag(repo, id);
1772 if (*tag != NULL) {
1773 (*tag)->refcnt++;
1774 return NULL;
1776 } else
1777 *tag = NULL;
1779 err = got_repo_search_packidx(&packidx, &idx, repo, id);
1780 if (err == NULL) {
1781 struct got_pack *pack = NULL;
1783 err = got_packidx_get_packfile_path(&path_packfile,
1784 packidx->path_packidx);
1785 if (err)
1786 return err;
1788 pack = got_repo_get_cached_pack(repo, path_packfile);
1789 if (pack == NULL) {
1790 err = got_repo_cache_pack(&pack, repo, path_packfile,
1791 packidx);
1792 if (err)
1793 goto done;
1796 /* Beware of "lightweight" tags: Check object type first. */
1797 err = read_packed_object_privsep(&obj, repo, pack, packidx,
1798 idx, id);
1799 if (err)
1800 goto done;
1801 obj_type = obj->type;
1802 got_object_close(obj);
1803 if (obj_type != GOT_OBJ_TYPE_TAG) {
1804 err = got_error(GOT_ERR_OBJ_TYPE);
1805 goto done;
1807 err = read_packed_tag_privsep(tag, pack, packidx, idx, id);
1808 } else if (err->code == GOT_ERR_NO_OBJ) {
1809 int fd;
1811 err = got_object_open_loose_fd(&fd, id, repo);
1812 if (err)
1813 return err;
1814 err = got_object_read_header_privsep(&obj, id, repo, fd);
1815 if (err)
1816 return err;
1817 obj_type = obj->type;
1818 got_object_close(obj);
1819 if (obj_type != GOT_OBJ_TYPE_TAG)
1820 return got_error(GOT_ERR_OBJ_TYPE);
1822 err = got_object_open_loose_fd(&fd, id, repo);
1823 if (err)
1824 return err;
1825 err = read_tag_privsep(tag, fd, id, repo);
1828 if (err == NULL) {
1829 (*tag)->refcnt++;
1830 err = got_repo_cache_tag(repo, id, *tag);
1832 done:
1833 free(path_packfile);
1834 return err;
1837 const struct got_error *
1838 got_object_open_as_tag(struct got_tag_object **tag,
1839 struct got_repository *repo, struct got_object_id *id)
1841 *tag = got_repo_get_cached_tag(repo, id);
1842 if (*tag != NULL) {
1843 (*tag)->refcnt++;
1844 return NULL;
1847 return open_tag(tag, repo, id, 0);
1850 const struct got_error *
1851 got_object_tag_open(struct got_tag_object **tag,
1852 struct got_repository *repo, struct got_object *obj)
1854 return open_tag(tag, repo, got_object_get_id(obj), 1);
1857 const char *
1858 got_object_tag_get_name(struct got_tag_object *tag)
1860 return tag->tag;
1863 int
1864 got_object_tag_get_object_type(struct got_tag_object *tag)
1866 return tag->obj_type;
1869 struct got_object_id *
1870 got_object_tag_get_object_id(struct got_tag_object *tag)
1872 return &tag->id;
1875 time_t
1876 got_object_tag_get_tagger_time(struct got_tag_object *tag)
1878 return tag->tagger_time;
1881 time_t
1882 got_object_tag_get_tagger_gmtoff(struct got_tag_object *tag)
1884 return tag->tagger_gmtoff;
1887 const char *
1888 got_object_tag_get_tagger(struct got_tag_object *tag)
1890 return tag->tagger;
1893 const char *
1894 got_object_tag_get_message(struct got_tag_object *tag)
1896 return tag->tagmsg;
1899 static struct got_tree_entry *
1900 find_entry_by_name(struct got_tree_object *tree, const char *name, size_t len)
1902 int i;
1904 /* Note that tree entries are sorted in strncmp() order. */
1905 for (i = 0; i < tree->nentries; i++) {
1906 struct got_tree_entry *te = &tree->entries[i];
1907 int cmp = strncmp(te->name, name, len);
1908 if (cmp < 0)
1909 continue;
1910 if (cmp > 0)
1911 break;
1912 if (te->name[len] == '\0')
1913 return te;
1915 return NULL;
1918 struct got_tree_entry *
1919 got_object_tree_find_entry(struct got_tree_object *tree, const char *name)
1921 return find_entry_by_name(tree, name, strlen(name));
1924 const struct got_error *
1925 got_object_tree_find_path(struct got_object_id **id, mode_t *mode,
1926 struct got_repository *repo, struct got_tree_object *tree,
1927 const char *path)
1929 const struct got_error *err = NULL;
1930 struct got_tree_object *subtree = NULL;
1931 struct got_tree_entry *te = NULL;
1932 const char *seg, *s;
1933 size_t seglen;
1935 *id = NULL;
1937 s = path;
1938 while (s[0] == '/')
1939 s++;
1940 seg = s;
1941 seglen = 0;
1942 subtree = tree;
1943 while (*s) {
1944 struct got_tree_object *next_tree;
1946 if (*s != '/') {
1947 s++;
1948 seglen++;
1949 if (*s)
1950 continue;
1953 te = find_entry_by_name(subtree, seg, seglen);
1954 if (te == NULL) {
1955 err = got_error_path(path, GOT_ERR_NO_TREE_ENTRY);
1956 goto done;
1959 if (*s == '\0')
1960 break;
1962 seg = s + 1;
1963 seglen = 0;
1964 s++;
1965 if (*s) {
1966 err = got_object_open_as_tree(&next_tree, repo,
1967 &te->id);
1968 te = NULL;
1969 if (err)
1970 goto done;
1971 if (subtree != tree)
1972 got_object_tree_close(subtree);
1973 subtree = next_tree;
1977 if (te) {
1978 *id = got_object_id_dup(&te->id);
1979 if (*id == NULL)
1980 return got_error_from_errno("got_object_id_dup");
1981 if (mode)
1982 *mode = te->mode;
1983 } else
1984 err = got_error_path(path, GOT_ERR_NO_TREE_ENTRY);
1985 done:
1986 if (subtree && subtree != tree)
1987 got_object_tree_close(subtree);
1988 return err;
1990 const struct got_error *
1991 got_object_id_by_path(struct got_object_id **id, struct got_repository *repo,
1992 struct got_commit_object *commit, const char *path)
1994 const struct got_error *err = NULL;
1995 struct got_tree_object *tree = NULL;
1997 *id = NULL;
1999 /* Handle opening of root of commit's tree. */
2000 if (got_path_is_root_dir(path)) {
2001 *id = got_object_id_dup(commit->tree_id);
2002 if (*id == NULL)
2003 err = got_error_from_errno("got_object_id_dup");
2004 } else {
2005 err = got_object_open_as_tree(&tree, repo, commit->tree_id);
2006 if (err)
2007 goto done;
2008 err = got_object_tree_find_path(id, NULL, repo, tree, path);
2010 done:
2011 if (tree)
2012 got_object_tree_close(tree);
2013 return err;
2017 * Normalize file mode bits to avoid false positive tree entry differences
2018 * in case tree entries have unexpected mode bits set.
2020 static mode_t
2021 normalize_mode_for_comparison(mode_t mode)
2024 * For directories, the only relevant bit is the IFDIR bit.
2025 * This allows us to detect paths changing from a directory
2026 * to a file and vice versa.
2028 if (S_ISDIR(mode))
2029 return mode & S_IFDIR;
2032 * For symlinks, the only relevant bit is the IFLNK bit.
2033 * This allows us to detect paths changing from a symlinks
2034 * to a file or directory and vice versa.
2036 if (S_ISLNK(mode))
2037 return mode & S_IFLNK;
2039 /* For files, the only change we care about is the executable bit. */
2040 return mode & S_IXUSR;
2043 const struct got_error *
2044 got_object_tree_path_changed(int *changed,
2045 struct got_tree_object *tree01, struct got_tree_object *tree02,
2046 const char *path, struct got_repository *repo)
2048 const struct got_error *err = NULL;
2049 struct got_tree_object *tree1 = NULL, *tree2 = NULL;
2050 struct got_tree_entry *te1 = NULL, *te2 = NULL;
2051 const char *seg, *s;
2052 size_t seglen;
2054 *changed = 0;
2056 /* We not do support comparing the root path. */
2057 if (got_path_is_root_dir(path))
2058 return got_error_path(path, GOT_ERR_BAD_PATH);
2060 tree1 = tree01;
2061 tree2 = tree02;
2062 s = path;
2063 while (*s == '/')
2064 s++;
2065 seg = s;
2066 seglen = 0;
2067 while (*s) {
2068 struct got_tree_object *next_tree1, *next_tree2;
2069 mode_t mode1, mode2;
2071 if (*s != '/') {
2072 s++;
2073 seglen++;
2074 if (*s)
2075 continue;
2078 te1 = find_entry_by_name(tree1, seg, seglen);
2079 if (te1 == NULL) {
2080 err = got_error(GOT_ERR_NO_OBJ);
2081 goto done;
2084 if (tree2)
2085 te2 = find_entry_by_name(tree2, seg, seglen);
2087 if (te2) {
2088 mode1 = normalize_mode_for_comparison(te1->mode);
2089 mode2 = normalize_mode_for_comparison(te2->mode);
2090 if (mode1 != mode2) {
2091 *changed = 1;
2092 goto done;
2095 if (got_object_id_cmp(&te1->id, &te2->id) == 0) {
2096 *changed = 0;
2097 goto done;
2101 if (*s == '\0') { /* final path element */
2102 *changed = 1;
2103 goto done;
2106 seg = s + 1;
2107 s++;
2108 seglen = 0;
2109 if (*s) {
2110 err = got_object_open_as_tree(&next_tree1, repo,
2111 &te1->id);
2112 te1 = NULL;
2113 if (err)
2114 goto done;
2115 if (tree1 != tree01)
2116 got_object_tree_close(tree1);
2117 tree1 = next_tree1;
2119 if (te2) {
2120 err = got_object_open_as_tree(&next_tree2, repo,
2121 &te2->id);
2122 te2 = NULL;
2123 if (err)
2124 goto done;
2125 if (tree2 != tree02)
2126 got_object_tree_close(tree2);
2127 tree2 = next_tree2;
2128 } else if (tree2) {
2129 if (tree2 != tree02)
2130 got_object_tree_close(tree2);
2131 tree2 = NULL;
2135 done:
2136 if (tree1 && tree1 != tree01)
2137 got_object_tree_close(tree1);
2138 if (tree2 && tree2 != tree02)
2139 got_object_tree_close(tree2);
2140 return err;
2143 const struct got_error *
2144 got_object_tree_entry_dup(struct got_tree_entry **new_te,
2145 struct got_tree_entry *te)
2147 const struct got_error *err = NULL;
2149 *new_te = calloc(1, sizeof(**new_te));
2150 if (*new_te == NULL)
2151 return got_error_from_errno("calloc");
2153 (*new_te)->mode = te->mode;
2154 memcpy((*new_te)->name, te->name, sizeof((*new_te)->name));
2155 memcpy(&(*new_te)->id, &te->id, sizeof((*new_te)->id));
2156 return err;
2159 int
2160 got_object_tree_entry_is_submodule(struct got_tree_entry *te)
2162 return (te->mode & S_IFMT) == (S_IFDIR | S_IFLNK);
2165 int
2166 got_object_tree_entry_is_symlink(struct got_tree_entry *te)
2168 /* S_IFDIR check avoids confusing symlinks with submodules. */
2169 return ((te->mode & (S_IFDIR | S_IFLNK)) == S_IFLNK);
2172 static const struct got_error *
2173 resolve_symlink(char **link_target, const char *path,
2174 struct got_commit_object *commit, struct got_repository *repo)
2176 const struct got_error *err = NULL;
2177 char buf[PATH_MAX];
2178 char *name, *parent_path = NULL;
2179 struct got_object_id *tree_obj_id = NULL;
2180 struct got_tree_object *tree = NULL;
2181 struct got_tree_entry *te = NULL;
2183 *link_target = NULL;
2185 if (strlcpy(buf, path, sizeof(buf)) >= sizeof(buf))
2186 return got_error(GOT_ERR_NO_SPACE);
2188 name = basename(buf);
2189 if (name == NULL)
2190 return got_error_from_errno2("basename", path);
2192 err = got_path_dirname(&parent_path, path);
2193 if (err)
2194 return err;
2196 err = got_object_id_by_path(&tree_obj_id, repo, commit,
2197 parent_path);
2198 if (err) {
2199 if (err->code == GOT_ERR_NO_TREE_ENTRY) {
2200 /* Display the complete path in error message. */
2201 err = got_error_path(path, err->code);
2203 goto done;
2206 err = got_object_open_as_tree(&tree, repo, tree_obj_id);
2207 if (err)
2208 goto done;
2210 te = got_object_tree_find_entry(tree, name);
2211 if (te == NULL) {
2212 err = got_error_path(path, GOT_ERR_NO_TREE_ENTRY);
2213 goto done;
2216 if (got_object_tree_entry_is_symlink(te)) {
2217 err = got_tree_entry_get_symlink_target(link_target, te, repo);
2218 if (err)
2219 goto done;
2220 if (!got_path_is_absolute(*link_target)) {
2221 char *abspath;
2222 if (asprintf(&abspath, "%s/%s", parent_path,
2223 *link_target) == -1) {
2224 err = got_error_from_errno("asprintf");
2225 goto done;
2227 free(*link_target);
2228 *link_target = malloc(PATH_MAX);
2229 if (*link_target == NULL) {
2230 err = got_error_from_errno("malloc");
2231 goto done;
2233 err = got_canonpath(abspath, *link_target, PATH_MAX);
2234 free(abspath);
2235 if (err)
2236 goto done;
2239 done:
2240 free(tree_obj_id);
2241 if (tree)
2242 got_object_tree_close(tree);
2243 if (err) {
2244 free(*link_target);
2245 *link_target = NULL;
2247 return err;
2250 const struct got_error *
2251 got_object_resolve_symlinks(char **link_target, const char *path,
2252 struct got_commit_object *commit, struct got_repository *repo)
2254 const struct got_error *err = NULL;
2255 char *next_target = NULL;
2256 int max_recursion = 40; /* matches Git */
2258 *link_target = NULL;
2260 do {
2261 err = resolve_symlink(&next_target,
2262 *link_target ? *link_target : path, commit, repo);
2263 if (err)
2264 break;
2265 if (next_target) {
2266 free(*link_target);
2267 if (--max_recursion == 0) {
2268 err = got_error_path(path, GOT_ERR_RECURSION);
2269 *link_target = NULL;
2270 break;
2272 *link_target = next_target;
2274 } while (next_target);
2276 return err;
2279 const struct got_error *
2280 got_traverse_packed_commits(struct got_object_id_queue *traversed_commits,
2281 struct got_object_id *commit_id, const char *path,
2282 struct got_repository *repo)
2284 const struct got_error *err = NULL;
2285 struct got_pack *pack = NULL;
2286 struct got_packidx *packidx = NULL;
2287 char *path_packfile = NULL;
2288 struct got_commit_object *changed_commit = NULL;
2289 struct got_object_id *changed_commit_id = NULL;
2290 int idx;
2292 err = got_repo_search_packidx(&packidx, &idx, repo, commit_id);
2293 if (err) {
2294 if (err->code != GOT_ERR_NO_OBJ)
2295 return err;
2296 return NULL;
2299 err = got_packidx_get_packfile_path(&path_packfile,
2300 packidx->path_packidx);
2301 if (err)
2302 return err;
2304 pack = got_repo_get_cached_pack(repo, path_packfile);
2305 if (pack == NULL) {
2306 err = got_repo_cache_pack(&pack, repo, path_packfile, packidx);
2307 if (err)
2308 goto done;
2311 if (pack->privsep_child == NULL) {
2312 err = start_pack_privsep_child(pack, packidx);
2313 if (err)
2314 goto done;
2317 err = got_privsep_send_commit_traversal_request(
2318 pack->privsep_child->ibuf, commit_id, idx, path);
2319 if (err)
2320 goto done;
2322 err = got_privsep_recv_traversed_commits(&changed_commit,
2323 &changed_commit_id, traversed_commits, pack->privsep_child->ibuf);
2324 if (err)
2325 goto done;
2327 if (changed_commit) {
2329 * Cache the commit in which the path was changed.
2330 * This commit might be opened again soon.
2332 changed_commit->refcnt++;
2333 err = got_repo_cache_commit(repo, changed_commit_id,
2334 changed_commit);
2335 got_object_commit_close(changed_commit);
2337 done:
2338 free(path_packfile);
2339 free(changed_commit_id);
2340 return err;