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 /*
446 * XXX This function does not really belong in object.c. It is only here
447 * because it needs start_pack_privsep_child(); relevant code should
448 * probably be moved to pack.c/pack_create.c.
449 */
450 const struct got_error *
451 got_object_prepare_delta_reuse(struct got_pack **pack,
452 struct got_packidx *packidx, int delta_outfd, struct got_repository *repo)
454 const struct got_error *err = NULL;
455 char *path_packfile = NULL;
457 err = got_packidx_get_packfile_path(&path_packfile,
458 packidx->path_packidx);
459 if (err)
460 return err;
462 *pack = got_repo_get_cached_pack(repo, path_packfile);
463 if (*pack == NULL) {
464 err = got_repo_cache_pack(pack, repo, path_packfile, packidx);
465 if (err)
466 goto done;
468 if ((*pack)->privsep_child == NULL) {
469 err = start_pack_privsep_child(*pack, packidx);
470 if (err)
471 goto done;
474 if (!(*pack)->child_has_delta_outfd) {
475 int outfd_child;
476 outfd_child = dup(delta_outfd);
477 if (outfd_child == -1) {
478 err = got_error_from_errno("dup");
479 goto done;
481 err = got_privsep_send_raw_delta_outfd(
482 (*pack)->privsep_child->ibuf, outfd_child);
483 if (err)
484 goto done;
485 (*pack)->child_has_delta_outfd = 1;
488 err = got_privsep_send_delta_reuse_req((*pack)->privsep_child->ibuf);
489 done:
490 free(path_packfile);
491 return err;
494 static const struct got_error *
495 request_object(struct got_object **obj, struct got_object_id *id,
496 struct got_repository *repo, int fd)
498 const struct got_error *err = NULL;
499 struct imsgbuf *ibuf;
501 ibuf = repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_OBJECT].ibuf;
503 err = got_privsep_send_obj_req(ibuf, fd, id);
504 if (err)
505 return err;
507 return got_privsep_recv_obj(obj, ibuf);
510 static const struct got_error *
511 request_raw_object(uint8_t **outbuf, off_t *size, size_t *hdrlen, int outfd,
512 struct got_object_id *id, struct got_repository *repo, int infd)
514 const struct got_error *err = NULL;
515 struct imsgbuf *ibuf;
516 int outfd_child;
518 ibuf = repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_OBJECT].ibuf;
520 outfd_child = dup(outfd);
521 if (outfd_child == -1)
522 return got_error_from_errno("dup");
524 err = got_privsep_send_raw_obj_req(ibuf, infd, id);
525 if (err)
526 return err;
528 err = got_privsep_send_raw_obj_outfd(ibuf, outfd_child);
529 if (err)
530 return err;
532 return got_privsep_recv_raw_obj(outbuf, size, hdrlen, ibuf);
535 static const struct got_error *
536 start_read_object_child(struct got_repository *repo)
538 const struct got_error *err = NULL;
539 int imsg_fds[2];
540 pid_t pid;
541 struct imsgbuf *ibuf;
543 ibuf = calloc(1, sizeof(*ibuf));
544 if (ibuf == NULL)
545 return got_error_from_errno("calloc");
547 if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_fds) == -1) {
548 err = got_error_from_errno("socketpair");
549 free(ibuf);
550 return err;
553 pid = fork();
554 if (pid == -1) {
555 err = got_error_from_errno("fork");
556 free(ibuf);
557 return err;
559 else if (pid == 0) {
560 got_privsep_exec_child(imsg_fds, GOT_PATH_PROG_READ_OBJECT,
561 repo->path);
562 /* not reached */
565 if (close(imsg_fds[1]) == -1) {
566 err = got_error_from_errno("close");
567 free(ibuf);
568 return err;
571 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_OBJECT].imsg_fd =
572 imsg_fds[0];
573 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_OBJECT].pid = pid;
574 imsg_init(ibuf, imsg_fds[0]);
575 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_OBJECT].ibuf = ibuf;
577 return NULL;
580 const struct got_error *
581 got_object_read_header_privsep(struct got_object **obj,
582 struct got_object_id *id, struct got_repository *repo, int obj_fd)
584 const struct got_error *err;
586 if (repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_OBJECT].imsg_fd != -1)
587 return request_object(obj, id, repo, obj_fd);
589 err = start_read_object_child(repo);
590 if (err) {
591 close(obj_fd);
592 return err;
595 return request_object(obj, id, repo, obj_fd);
598 static const struct got_error *
599 read_object_raw_privsep(uint8_t **outbuf, off_t *size, size_t *hdrlen,
600 int outfd, struct got_object_id *id, struct got_repository *repo,
601 int obj_fd)
603 const struct got_error *err;
605 if (repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_OBJECT].imsg_fd != -1)
606 return request_raw_object(outbuf, size, hdrlen, outfd, id,
607 repo, obj_fd);
609 err = start_read_object_child(repo);
610 if (err)
611 return err;
613 return request_raw_object(outbuf, size, hdrlen, outfd, id, repo,
614 obj_fd);
617 const struct got_error *
618 got_object_open(struct got_object **obj, struct got_repository *repo,
619 struct got_object_id *id)
621 const struct got_error *err = NULL;
622 int fd;
624 *obj = got_repo_get_cached_object(repo, id);
625 if (*obj != NULL) {
626 (*obj)->refcnt++;
627 return NULL;
630 err = got_object_open_packed(obj, id, repo);
631 if (err && err->code != GOT_ERR_NO_OBJ)
632 return err;
633 if (*obj) {
634 (*obj)->refcnt++;
635 return got_repo_cache_object(repo, id, *obj);
638 err = got_object_open_loose_fd(&fd, id, repo);
639 if (err) {
640 if (err->code == GOT_ERR_ERRNO && errno == ENOENT)
641 err = got_error_no_obj(id);
642 return err;
645 err = got_object_read_header_privsep(obj, id, repo, fd);
646 if (err)
647 return err;
649 memcpy((*obj)->id.sha1, id->sha1, SHA1_DIGEST_LENGTH);
651 (*obj)->refcnt++;
652 return got_repo_cache_object(repo, id, *obj);
655 /* *outfd must be initialized to -1 by caller */
656 const struct got_error *
657 got_object_raw_open(struct got_raw_object **obj, int *outfd,
658 struct got_repository *repo, struct got_object_id *id)
660 const struct got_error *err = NULL;
661 struct got_packidx *packidx = NULL;
662 int idx;
663 uint8_t *outbuf = NULL;
664 off_t size = 0;
665 size_t hdrlen = 0;
666 char *path_packfile = NULL;
668 *obj = got_repo_get_cached_raw_object(repo, id);
669 if (*obj != NULL) {
670 (*obj)->refcnt++;
671 return NULL;
674 if (*outfd == -1) {
675 *outfd = got_opentempfd();
676 if (*outfd == -1)
677 return got_error_from_errno("got_opentempfd");
680 err = got_repo_search_packidx(&packidx, &idx, repo, id);
681 if (err == NULL) {
682 struct got_pack *pack = NULL;
684 err = got_packidx_get_packfile_path(&path_packfile,
685 packidx->path_packidx);
686 if (err)
687 goto done;
689 pack = got_repo_get_cached_pack(repo, path_packfile);
690 if (pack == NULL) {
691 err = got_repo_cache_pack(&pack, repo, path_packfile,
692 packidx);
693 if (err)
694 goto done;
696 err = read_packed_object_raw_privsep(&outbuf, &size, &hdrlen,
697 *outfd, pack, packidx, idx, id);
698 if (err)
699 goto done;
700 } else if (err->code == GOT_ERR_NO_OBJ) {
701 int fd;
703 err = got_object_open_loose_fd(&fd, id, repo);
704 if (err)
705 goto done;
706 err = read_object_raw_privsep(&outbuf, &size, &hdrlen, *outfd,
707 id, repo, fd);
708 if (err)
709 goto done;
712 *obj = calloc(1, sizeof(**obj));
713 if (*obj == NULL) {
714 err = got_error_from_errno("calloc");
715 goto done;
717 (*obj)->fd = -1;
719 if (outbuf) {
720 (*obj)->data = outbuf;
721 } else {
722 struct stat sb;
723 if (fstat(*outfd, &sb) == -1) {
724 err = got_error_from_errno("fstat");
725 goto done;
728 if (sb.st_size != hdrlen + size) {
729 err = got_error(GOT_ERR_PRIVSEP_LEN);
730 goto done;
732 #ifndef GOT_PACK_NO_MMAP
733 if (hdrlen + size > 0) {
734 (*obj)->data = mmap(NULL, hdrlen + size, PROT_READ,
735 MAP_PRIVATE, *outfd, 0);
736 if ((*obj)->data == MAP_FAILED) {
737 if (errno != ENOMEM) {
738 err = got_error_from_errno("mmap");
739 goto done;
741 (*obj)->data = NULL;
742 } else {
743 (*obj)->fd = *outfd;
744 *outfd = -1;
747 #endif
748 if (*outfd != -1) {
749 (*obj)->f = fdopen(*outfd, "r");
750 if ((*obj)->f == NULL) {
751 err = got_error_from_errno("fdopen");
752 goto done;
754 *outfd = -1;
757 (*obj)->hdrlen = hdrlen;
758 (*obj)->size = size;
759 err = got_repo_cache_raw_object(repo, id, *obj);
760 done:
761 free(path_packfile);
762 if (err) {
763 if (*obj) {
764 got_object_raw_close(*obj);
765 *obj = NULL;
767 free(outbuf);
768 } else
769 (*obj)->refcnt++;
770 return err;
773 const struct got_error *
774 got_object_open_by_id_str(struct got_object **obj, struct got_repository *repo,
775 const char *id_str)
777 struct got_object_id id;
779 if (!got_parse_sha1_digest(id.sha1, id_str))
780 return got_error_path(id_str, GOT_ERR_BAD_OBJ_ID_STR);
782 return got_object_open(obj, repo, &id);
785 const struct got_error *
786 got_object_resolve_id_str(struct got_object_id **id,
787 struct got_repository *repo, const char *id_str)
789 const struct got_error *err = NULL;
790 struct got_object *obj;
792 err = got_object_open_by_id_str(&obj, repo, id_str);
793 if (err)
794 return err;
796 *id = got_object_id_dup(got_object_get_id(obj));
797 got_object_close(obj);
798 if (*id == NULL)
799 return got_error_from_errno("got_object_id_dup");
801 return NULL;
804 static const struct got_error *
805 request_packed_commit(struct got_commit_object **commit, struct got_pack *pack,
806 int pack_idx, struct got_object_id *id)
808 const struct got_error *err = NULL;
810 err = got_privsep_send_commit_req(pack->privsep_child->ibuf, -1, id,
811 pack_idx);
812 if (err)
813 return err;
815 err = got_privsep_recv_commit(commit, pack->privsep_child->ibuf);
816 if (err)
817 return err;
819 (*commit)->flags |= GOT_COMMIT_FLAG_PACKED;
820 return NULL;
823 static const struct got_error *
824 read_packed_commit_privsep(struct got_commit_object **commit,
825 struct got_pack *pack, struct got_packidx *packidx, int idx,
826 struct got_object_id *id)
828 const struct got_error *err = NULL;
830 if (pack->privsep_child)
831 return request_packed_commit(commit, pack, idx, id);
833 err = start_pack_privsep_child(pack, packidx);
834 if (err)
835 return err;
837 return request_packed_commit(commit, pack, idx, id);
840 static const struct got_error *
841 request_commit(struct got_commit_object **commit, struct got_repository *repo,
842 int fd, struct got_object_id *id)
844 const struct got_error *err = NULL;
845 struct imsgbuf *ibuf;
847 ibuf = repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_COMMIT].ibuf;
849 err = got_privsep_send_commit_req(ibuf, fd, id, -1);
850 if (err)
851 return err;
853 return got_privsep_recv_commit(commit, ibuf);
856 static const struct got_error *
857 read_commit_privsep(struct got_commit_object **commit, int obj_fd,
858 struct got_object_id *id, struct got_repository *repo)
860 const struct got_error *err;
861 int imsg_fds[2];
862 pid_t pid;
863 struct imsgbuf *ibuf;
865 if (repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_COMMIT].imsg_fd != -1)
866 return request_commit(commit, repo, obj_fd, id);
868 ibuf = calloc(1, sizeof(*ibuf));
869 if (ibuf == NULL)
870 return got_error_from_errno("calloc");
872 if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_fds) == -1) {
873 err = got_error_from_errno("socketpair");
874 free(ibuf);
875 return err;
878 pid = fork();
879 if (pid == -1) {
880 err = got_error_from_errno("fork");
881 free(ibuf);
882 return err;
884 else if (pid == 0) {
885 got_privsep_exec_child(imsg_fds, GOT_PATH_PROG_READ_COMMIT,
886 repo->path);
887 /* not reached */
890 if (close(imsg_fds[1]) == -1) {
891 err = got_error_from_errno("close");
892 free(ibuf);
893 return err;
895 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_COMMIT].imsg_fd =
896 imsg_fds[0];
897 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_COMMIT].pid = pid;
898 imsg_init(ibuf, imsg_fds[0]);
899 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_COMMIT].ibuf = ibuf;
901 return request_commit(commit, repo, obj_fd, id);
905 static const struct got_error *
906 open_commit(struct got_commit_object **commit,
907 struct got_repository *repo, struct got_object_id *id, int check_cache)
909 const struct got_error *err = NULL;
910 struct got_packidx *packidx = NULL;
911 int idx;
912 char *path_packfile = NULL;
914 if (check_cache) {
915 *commit = got_repo_get_cached_commit(repo, id);
916 if (*commit != NULL) {
917 (*commit)->refcnt++;
918 return NULL;
920 } else
921 *commit = NULL;
923 err = got_repo_search_packidx(&packidx, &idx, repo, id);
924 if (err == NULL) {
925 struct got_pack *pack = NULL;
927 err = got_packidx_get_packfile_path(&path_packfile,
928 packidx->path_packidx);
929 if (err)
930 return err;
932 pack = got_repo_get_cached_pack(repo, path_packfile);
933 if (pack == NULL) {
934 err = got_repo_cache_pack(&pack, repo, path_packfile,
935 packidx);
936 if (err)
937 goto done;
939 err = read_packed_commit_privsep(commit, pack,
940 packidx, idx, id);
941 } else if (err->code == GOT_ERR_NO_OBJ) {
942 int fd;
944 err = got_object_open_loose_fd(&fd, id, repo);
945 if (err)
946 return err;
947 err = read_commit_privsep(commit, fd, id, repo);
950 if (err == NULL) {
951 (*commit)->refcnt++;
952 err = got_repo_cache_commit(repo, id, *commit);
954 done:
955 free(path_packfile);
956 return err;
959 const struct got_error *
960 got_object_open_as_commit(struct got_commit_object **commit,
961 struct got_repository *repo, struct got_object_id *id)
963 *commit = got_repo_get_cached_commit(repo, id);
964 if (*commit != NULL) {
965 (*commit)->refcnt++;
966 return NULL;
969 return open_commit(commit, repo, id, 0);
972 const struct got_error *
973 got_object_commit_open(struct got_commit_object **commit,
974 struct got_repository *repo, struct got_object *obj)
976 return open_commit(commit, repo, got_object_get_id(obj), 1);
979 const struct got_error *
980 got_object_qid_alloc(struct got_object_qid **qid, struct got_object_id *id)
982 *qid = calloc(1, sizeof(**qid));
983 if (*qid == NULL)
984 return got_error_from_errno("calloc");
986 memcpy(&(*qid)->id, id, sizeof((*qid)->id));
987 return NULL;
990 const struct got_error *
991 got_object_id_queue_copy(const struct got_object_id_queue *src,
992 struct got_object_id_queue *dest)
994 const struct got_error *err;
995 struct got_object_qid *qid;
997 STAILQ_FOREACH(qid, src, entry) {
998 struct got_object_qid *new;
999 /*
1000 * Deep-copy the object ID only. Let the caller deal
1001 * with setting up the new->data pointer if needed.
1003 err = got_object_qid_alloc(&new, &qid->id);
1004 if (err) {
1005 got_object_id_queue_free(dest);
1006 return err;
1008 STAILQ_INSERT_TAIL(dest, new, entry);
1011 return NULL;
1014 static const struct got_error *
1015 request_packed_tree(struct got_tree_object **tree, struct got_pack *pack,
1016 int pack_idx, struct got_object_id *id)
1018 const struct got_error *err = NULL;
1020 err = got_privsep_send_tree_req(pack->privsep_child->ibuf, -1, id,
1021 pack_idx);
1022 if (err)
1023 return err;
1025 return got_privsep_recv_tree(tree, pack->privsep_child->ibuf);
1028 static const struct got_error *
1029 read_packed_tree_privsep(struct got_tree_object **tree,
1030 struct got_pack *pack, struct got_packidx *packidx, int idx,
1031 struct got_object_id *id)
1033 const struct got_error *err = NULL;
1035 if (pack->privsep_child)
1036 return request_packed_tree(tree, pack, idx, id);
1038 err = start_pack_privsep_child(pack, packidx);
1039 if (err)
1040 return err;
1042 return request_packed_tree(tree, pack, idx, id);
1045 static const struct got_error *
1046 request_tree(struct got_tree_object **tree, struct got_repository *repo,
1047 int fd, struct got_object_id *id)
1049 const struct got_error *err = NULL;
1050 struct imsgbuf *ibuf;
1052 ibuf = repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TREE].ibuf;
1054 err = got_privsep_send_tree_req(ibuf, fd, id, -1);
1055 if (err)
1056 return err;
1058 return got_privsep_recv_tree(tree, ibuf);
1061 const struct got_error *
1062 read_tree_privsep(struct got_tree_object **tree, int obj_fd,
1063 struct got_object_id *id, struct got_repository *repo)
1065 const struct got_error *err;
1066 int imsg_fds[2];
1067 pid_t pid;
1068 struct imsgbuf *ibuf;
1070 if (repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TREE].imsg_fd != -1)
1071 return request_tree(tree, repo, obj_fd, id);
1073 ibuf = calloc(1, sizeof(*ibuf));
1074 if (ibuf == NULL)
1075 return got_error_from_errno("calloc");
1077 if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_fds) == -1) {
1078 err = got_error_from_errno("socketpair");
1079 free(ibuf);
1080 return err;
1083 pid = fork();
1084 if (pid == -1) {
1085 err = got_error_from_errno("fork");
1086 free(ibuf);
1087 return err;
1089 else if (pid == 0) {
1090 got_privsep_exec_child(imsg_fds, GOT_PATH_PROG_READ_TREE,
1091 repo->path);
1092 /* not reached */
1095 if (close(imsg_fds[1]) == -1) {
1096 err = got_error_from_errno("close");
1097 free(ibuf);
1098 return err;
1100 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TREE].imsg_fd =
1101 imsg_fds[0];
1102 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TREE].pid = pid;
1103 imsg_init(ibuf, imsg_fds[0]);
1104 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TREE].ibuf = ibuf;
1107 return request_tree(tree, repo, obj_fd, id);
1110 static const struct got_error *
1111 open_tree(struct got_tree_object **tree, struct got_repository *repo,
1112 struct got_object_id *id, int check_cache)
1114 const struct got_error *err = NULL;
1115 struct got_packidx *packidx = NULL;
1116 int idx;
1117 char *path_packfile = NULL;
1119 if (check_cache) {
1120 *tree = got_repo_get_cached_tree(repo, id);
1121 if (*tree != NULL) {
1122 (*tree)->refcnt++;
1123 return NULL;
1125 } else
1126 *tree = NULL;
1128 err = got_repo_search_packidx(&packidx, &idx, repo, id);
1129 if (err == NULL) {
1130 struct got_pack *pack = NULL;
1132 err = got_packidx_get_packfile_path(&path_packfile,
1133 packidx->path_packidx);
1134 if (err)
1135 return err;
1137 pack = got_repo_get_cached_pack(repo, path_packfile);
1138 if (pack == NULL) {
1139 err = got_repo_cache_pack(&pack, repo, path_packfile,
1140 packidx);
1141 if (err)
1142 goto done;
1144 err = read_packed_tree_privsep(tree, pack,
1145 packidx, idx, id);
1146 } else if (err->code == GOT_ERR_NO_OBJ) {
1147 int fd;
1149 err = got_object_open_loose_fd(&fd, id, repo);
1150 if (err)
1151 return err;
1152 err = read_tree_privsep(tree, fd, id, repo);
1155 if (err == NULL) {
1156 (*tree)->refcnt++;
1157 err = got_repo_cache_tree(repo, id, *tree);
1159 done:
1160 free(path_packfile);
1161 return err;
1164 const struct got_error *
1165 got_object_open_as_tree(struct got_tree_object **tree,
1166 struct got_repository *repo, struct got_object_id *id)
1168 *tree = got_repo_get_cached_tree(repo, id);
1169 if (*tree != NULL) {
1170 (*tree)->refcnt++;
1171 return NULL;
1174 return open_tree(tree, repo, id, 0);
1177 const struct got_error *
1178 got_object_tree_open(struct got_tree_object **tree,
1179 struct got_repository *repo, struct got_object *obj)
1181 return open_tree(tree, repo, got_object_get_id(obj), 1);
1184 int
1185 got_object_tree_get_nentries(struct got_tree_object *tree)
1187 return tree->nentries;
1190 struct got_tree_entry *
1191 got_object_tree_get_first_entry(struct got_tree_object *tree)
1193 return got_object_tree_get_entry(tree, 0);
1196 struct got_tree_entry *
1197 got_object_tree_get_last_entry(struct got_tree_object *tree)
1199 return got_object_tree_get_entry(tree, tree->nentries - 1);
1202 struct got_tree_entry *
1203 got_object_tree_get_entry(struct got_tree_object *tree, int i)
1205 if (i < 0 || i >= tree->nentries)
1206 return NULL;
1207 return &tree->entries[i];
1210 mode_t
1211 got_tree_entry_get_mode(struct got_tree_entry *te)
1213 return te->mode;
1216 const char *
1217 got_tree_entry_get_name(struct got_tree_entry *te)
1219 return &te->name[0];
1222 struct got_object_id *
1223 got_tree_entry_get_id(struct got_tree_entry *te)
1225 return &te->id;
1228 const struct got_error *
1229 got_object_blob_read_to_str(char **s, struct got_blob_object *blob)
1231 const struct got_error *err = NULL;
1232 size_t len, totlen, hdrlen, offset;
1234 *s = NULL;
1236 hdrlen = got_object_blob_get_hdrlen(blob);
1237 totlen = 0;
1238 offset = 0;
1239 do {
1240 char *p;
1242 err = got_object_blob_read_block(&len, blob);
1243 if (err)
1244 return err;
1246 if (len == 0)
1247 break;
1249 totlen += len - hdrlen;
1250 p = realloc(*s, totlen + 1);
1251 if (p == NULL) {
1252 err = got_error_from_errno("realloc");
1253 free(*s);
1254 *s = NULL;
1255 return err;
1257 *s = p;
1258 /* Skip blob object header first time around. */
1259 memcpy(*s + offset,
1260 got_object_blob_get_read_buf(blob) + hdrlen, len - hdrlen);
1261 hdrlen = 0;
1262 offset = totlen;
1263 } while (len > 0);
1265 (*s)[totlen] = '\0';
1266 return NULL;
1269 const struct got_error *
1270 got_tree_entry_get_symlink_target(char **link_target, struct got_tree_entry *te,
1271 struct got_repository *repo)
1273 const struct got_error *err = NULL;
1274 struct got_blob_object *blob = NULL;
1276 *link_target = NULL;
1278 if (!got_object_tree_entry_is_symlink(te))
1279 return got_error(GOT_ERR_TREE_ENTRY_TYPE);
1281 err = got_object_open_as_blob(&blob, repo,
1282 got_tree_entry_get_id(te), PATH_MAX);
1283 if (err)
1284 return err;
1286 err = got_object_blob_read_to_str(link_target, blob);
1287 got_object_blob_close(blob);
1288 if (err) {
1289 free(*link_target);
1290 *link_target = NULL;
1292 return err;
1295 int
1296 got_tree_entry_get_index(struct got_tree_entry *te)
1298 return te->idx;
1301 struct got_tree_entry *
1302 got_tree_entry_get_next(struct got_tree_object *tree,
1303 struct got_tree_entry *te)
1305 return got_object_tree_get_entry(tree, te->idx + 1);
1308 struct got_tree_entry *
1309 got_tree_entry_get_prev(struct got_tree_object *tree,
1310 struct got_tree_entry *te)
1312 return got_object_tree_get_entry(tree, te->idx - 1);
1315 static const struct got_error *
1316 request_packed_blob(uint8_t **outbuf, size_t *size, size_t *hdrlen, int outfd,
1317 struct got_pack *pack, struct got_packidx *packidx, int idx,
1318 struct got_object_id *id)
1320 const struct got_error *err = NULL;
1321 struct imsgbuf *ibuf = pack->privsep_child->ibuf;
1322 int outfd_child;
1324 err = pack_child_send_tempfiles(ibuf, pack);
1325 if (err)
1326 return err;
1328 outfd_child = dup(outfd);
1329 if (outfd_child == -1)
1330 return got_error_from_errno("dup");
1332 err = got_privsep_send_blob_req(pack->privsep_child->ibuf, -1, id, idx);
1333 if (err)
1334 return err;
1336 err = got_privsep_send_blob_outfd(pack->privsep_child->ibuf,
1337 outfd_child);
1338 if (err) {
1339 return err;
1342 err = got_privsep_recv_blob(outbuf, size, hdrlen,
1343 pack->privsep_child->ibuf);
1344 if (err)
1345 return err;
1347 if (lseek(outfd, SEEK_SET, 0) == -1)
1348 err = got_error_from_errno("lseek");
1350 return err;
1353 static const struct got_error *
1354 read_packed_blob_privsep(uint8_t **outbuf, size_t *size, size_t *hdrlen,
1355 int outfd, struct got_pack *pack, struct got_packidx *packidx, int idx,
1356 struct got_object_id *id)
1358 const struct got_error *err = NULL;
1360 if (pack->privsep_child == NULL) {
1361 err = start_pack_privsep_child(pack, packidx);
1362 if (err)
1363 return err;
1366 return request_packed_blob(outbuf, size, hdrlen, outfd, pack, packidx,
1367 idx, id);
1370 static const struct got_error *
1371 request_blob(uint8_t **outbuf, size_t *size, size_t *hdrlen, int outfd,
1372 int infd, struct got_object_id *id, struct imsgbuf *ibuf)
1374 const struct got_error *err = NULL;
1375 int outfd_child;
1377 outfd_child = dup(outfd);
1378 if (outfd_child == -1)
1379 return got_error_from_errno("dup");
1381 err = got_privsep_send_blob_req(ibuf, infd, id, -1);
1382 if (err)
1383 return err;
1385 err = got_privsep_send_blob_outfd(ibuf, outfd_child);
1386 if (err)
1387 return err;
1389 err = got_privsep_recv_blob(outbuf, size, hdrlen, ibuf);
1390 if (err)
1391 return err;
1393 if (lseek(outfd, SEEK_SET, 0) == -1)
1394 return got_error_from_errno("lseek");
1396 return err;
1399 static const struct got_error *
1400 read_blob_privsep(uint8_t **outbuf, size_t *size, size_t *hdrlen,
1401 int outfd, int infd, struct got_object_id *id, struct got_repository *repo)
1403 const struct got_error *err;
1404 int imsg_fds[2];
1405 pid_t pid;
1406 struct imsgbuf *ibuf;
1408 if (repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_BLOB].imsg_fd != -1) {
1409 ibuf = repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_BLOB].ibuf;
1410 return request_blob(outbuf, size, hdrlen, outfd, infd, id,
1411 ibuf);
1414 ibuf = calloc(1, sizeof(*ibuf));
1415 if (ibuf == NULL)
1416 return got_error_from_errno("calloc");
1418 if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_fds) == -1) {
1419 err = got_error_from_errno("socketpair");
1420 free(ibuf);
1421 return err;
1424 pid = fork();
1425 if (pid == -1) {
1426 err = got_error_from_errno("fork");
1427 free(ibuf);
1428 return err;
1430 else if (pid == 0) {
1431 got_privsep_exec_child(imsg_fds, GOT_PATH_PROG_READ_BLOB,
1432 repo->path);
1433 /* not reached */
1436 if (close(imsg_fds[1]) == -1) {
1437 err = got_error_from_errno("close");
1438 free(ibuf);
1439 return err;
1441 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_BLOB].imsg_fd =
1442 imsg_fds[0];
1443 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_BLOB].pid = pid;
1444 imsg_init(ibuf, imsg_fds[0]);
1445 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_BLOB].ibuf = ibuf;
1447 return request_blob(outbuf, size, hdrlen, outfd, infd, id, ibuf);
1450 static const struct got_error *
1451 open_blob(struct got_blob_object **blob, struct got_repository *repo,
1452 struct got_object_id *id, size_t blocksize)
1454 const struct got_error *err = NULL;
1455 struct got_packidx *packidx = NULL;
1456 int idx;
1457 char *path_packfile = NULL;
1458 uint8_t *outbuf;
1459 int outfd;
1460 size_t size, hdrlen;
1461 struct stat sb;
1463 *blob = calloc(1, sizeof(**blob));
1464 if (*blob == NULL)
1465 return got_error_from_errno("calloc");
1467 outfd = got_opentempfd();
1468 if (outfd == -1)
1469 return got_error_from_errno("got_opentempfd");
1471 (*blob)->read_buf = malloc(blocksize);
1472 if ((*blob)->read_buf == NULL) {
1473 err = got_error_from_errno("malloc");
1474 goto done;
1477 err = got_repo_search_packidx(&packidx, &idx, repo, id);
1478 if (err == NULL) {
1479 struct got_pack *pack = NULL;
1481 err = got_packidx_get_packfile_path(&path_packfile,
1482 packidx->path_packidx);
1483 if (err)
1484 goto done;
1486 pack = got_repo_get_cached_pack(repo, path_packfile);
1487 if (pack == NULL) {
1488 err = got_repo_cache_pack(&pack, repo, path_packfile,
1489 packidx);
1490 if (err)
1491 goto done;
1493 err = read_packed_blob_privsep(&outbuf, &size, &hdrlen, outfd,
1494 pack, packidx, idx, id);
1495 } else if (err->code == GOT_ERR_NO_OBJ) {
1496 int infd;
1498 err = got_object_open_loose_fd(&infd, id, repo);
1499 if (err)
1500 goto done;
1501 err = read_blob_privsep(&outbuf, &size, &hdrlen, outfd, infd,
1502 id, repo);
1504 if (err)
1505 goto done;
1507 if (hdrlen > size) {
1508 err = got_error(GOT_ERR_BAD_OBJ_HDR);
1509 goto done;
1512 if (outbuf) {
1513 if (close(outfd) == -1 && err == NULL)
1514 err = got_error_from_errno("close");
1515 outfd = -1;
1516 (*blob)->f = fmemopen(outbuf, size, "rb");
1517 if ((*blob)->f == NULL) {
1518 err = got_error_from_errno("fmemopen");
1519 free(outbuf);
1520 goto done;
1522 (*blob)->data = outbuf;
1523 } else {
1524 if (fstat(outfd, &sb) == -1) {
1525 err = got_error_from_errno("fstat");
1526 goto done;
1529 if (sb.st_size != size) {
1530 err = got_error(GOT_ERR_PRIVSEP_LEN);
1531 goto done;
1534 (*blob)->f = fdopen(outfd, "rb");
1535 if ((*blob)->f == NULL) {
1536 err = got_error_from_errno("fdopen");
1537 close(outfd);
1538 outfd = -1;
1539 goto done;
1543 (*blob)->hdrlen = hdrlen;
1544 (*blob)->blocksize = blocksize;
1545 memcpy(&(*blob)->id.sha1, id->sha1, SHA1_DIGEST_LENGTH);
1547 done:
1548 free(path_packfile);
1549 if (err) {
1550 if (*blob) {
1551 got_object_blob_close(*blob);
1552 *blob = NULL;
1553 } else if (outfd != -1)
1554 close(outfd);
1556 return err;
1559 const struct got_error *
1560 got_object_open_as_blob(struct got_blob_object **blob,
1561 struct got_repository *repo, struct got_object_id *id,
1562 size_t blocksize)
1564 return open_blob(blob, repo, id, blocksize);
1567 const struct got_error *
1568 got_object_blob_open(struct got_blob_object **blob,
1569 struct got_repository *repo, struct got_object *obj, size_t blocksize)
1571 return open_blob(blob, repo, got_object_get_id(obj), blocksize);
1574 const struct got_error *
1575 got_object_blob_close(struct got_blob_object *blob)
1577 const struct got_error *err = NULL;
1578 free(blob->read_buf);
1579 if (blob->f && fclose(blob->f) == EOF)
1580 err = got_error_from_errno("fclose");
1581 free(blob->data);
1582 free(blob);
1583 return err;
1586 void
1587 got_object_blob_rewind(struct got_blob_object *blob)
1589 if (blob->f)
1590 rewind(blob->f);
1593 char *
1594 got_object_blob_id_str(struct got_blob_object *blob, char *buf, size_t size)
1596 return got_sha1_digest_to_str(blob->id.sha1, buf, size);
1599 size_t
1600 got_object_blob_get_hdrlen(struct got_blob_object *blob)
1602 return blob->hdrlen;
1605 const uint8_t *
1606 got_object_blob_get_read_buf(struct got_blob_object *blob)
1608 return blob->read_buf;
1611 const struct got_error *
1612 got_object_blob_read_block(size_t *outlenp, struct got_blob_object *blob)
1614 size_t n;
1616 n = fread(blob->read_buf, 1, blob->blocksize, blob->f);
1617 if (n == 0 && ferror(blob->f))
1618 return got_ferror(blob->f, GOT_ERR_IO);
1619 *outlenp = n;
1620 return NULL;
1623 const struct got_error *
1624 got_object_blob_dump_to_file(off_t *filesize, int *nlines,
1625 off_t **line_offsets, FILE *outfile, struct got_blob_object *blob)
1627 const struct got_error *err = NULL;
1628 size_t n, len, hdrlen;
1629 const uint8_t *buf;
1630 int i;
1631 const int alloc_chunksz = 512;
1632 size_t nalloc = 0;
1633 off_t off = 0, total_len = 0;
1635 if (line_offsets)
1636 *line_offsets = NULL;
1637 if (filesize)
1638 *filesize = 0;
1639 if (nlines)
1640 *nlines = 0;
1642 hdrlen = got_object_blob_get_hdrlen(blob);
1643 do {
1644 err = got_object_blob_read_block(&len, blob);
1645 if (err)
1646 return err;
1647 if (len == 0)
1648 break;
1649 buf = got_object_blob_get_read_buf(blob);
1650 i = hdrlen;
1651 if (nlines) {
1652 if (line_offsets && *line_offsets == NULL) {
1653 /* Have some data but perhaps no '\n'. */
1654 *nlines = 1;
1655 nalloc = alloc_chunksz;
1656 *line_offsets = calloc(nalloc,
1657 sizeof(**line_offsets));
1658 if (*line_offsets == NULL)
1659 return got_error_from_errno("calloc");
1661 /* Skip forward over end of first line. */
1662 while (i < len) {
1663 if (buf[i] == '\n')
1664 break;
1665 i++;
1668 /* Scan '\n' offsets in remaining chunk of data. */
1669 while (i < len) {
1670 if (buf[i] != '\n') {
1671 i++;
1672 continue;
1674 (*nlines)++;
1675 if (line_offsets && nalloc < *nlines) {
1676 size_t n = *nlines + alloc_chunksz;
1677 off_t *o = recallocarray(*line_offsets,
1678 nalloc, n, sizeof(**line_offsets));
1679 if (o == NULL) {
1680 free(*line_offsets);
1681 *line_offsets = NULL;
1682 return got_error_from_errno(
1683 "recallocarray");
1685 *line_offsets = o;
1686 nalloc = n;
1688 if (line_offsets) {
1689 off = total_len + i - hdrlen + 1;
1690 (*line_offsets)[*nlines - 1] = off;
1692 i++;
1695 /* Skip blob object header first time around. */
1696 n = fwrite(buf + hdrlen, 1, len - hdrlen, outfile);
1697 if (n != len - hdrlen)
1698 return got_ferror(outfile, GOT_ERR_IO);
1699 total_len += len - hdrlen;
1700 hdrlen = 0;
1701 } while (len != 0);
1703 if (fflush(outfile) != 0)
1704 return got_error_from_errno("fflush");
1705 rewind(outfile);
1707 if (filesize)
1708 *filesize = total_len;
1710 return NULL;
1713 static const struct got_error *
1714 request_packed_tag(struct got_tag_object **tag, struct got_pack *pack,
1715 int pack_idx, struct got_object_id *id)
1717 const struct got_error *err = NULL;
1719 err = got_privsep_send_tag_req(pack->privsep_child->ibuf, -1, id,
1720 pack_idx);
1721 if (err)
1722 return err;
1724 return got_privsep_recv_tag(tag, pack->privsep_child->ibuf);
1727 static const struct got_error *
1728 read_packed_tag_privsep(struct got_tag_object **tag,
1729 struct got_pack *pack, struct got_packidx *packidx, int idx,
1730 struct got_object_id *id)
1732 const struct got_error *err = NULL;
1734 if (pack->privsep_child)
1735 return request_packed_tag(tag, pack, idx, id);
1737 err = start_pack_privsep_child(pack, packidx);
1738 if (err)
1739 return err;
1741 return request_packed_tag(tag, pack, idx, id);
1744 static const struct got_error *
1745 request_tag(struct got_tag_object **tag, struct got_repository *repo,
1746 int fd, struct got_object_id *id)
1748 const struct got_error *err = NULL;
1749 struct imsgbuf *ibuf;
1751 ibuf = repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TAG].ibuf;
1753 err = got_privsep_send_tag_req(ibuf, fd, id, -1);
1754 if (err)
1755 return err;
1757 return got_privsep_recv_tag(tag, ibuf);
1760 static const struct got_error *
1761 read_tag_privsep(struct got_tag_object **tag, int obj_fd,
1762 struct got_object_id *id, struct got_repository *repo)
1764 const struct got_error *err;
1765 int imsg_fds[2];
1766 pid_t pid;
1767 struct imsgbuf *ibuf;
1769 if (repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TAG].imsg_fd != -1)
1770 return request_tag(tag, repo, obj_fd, id);
1772 ibuf = calloc(1, sizeof(*ibuf));
1773 if (ibuf == NULL)
1774 return got_error_from_errno("calloc");
1776 if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_fds) == -1) {
1777 err = got_error_from_errno("socketpair");
1778 free(ibuf);
1779 return err;
1782 pid = fork();
1783 if (pid == -1) {
1784 err = got_error_from_errno("fork");
1785 free(ibuf);
1786 return err;
1788 else if (pid == 0) {
1789 got_privsep_exec_child(imsg_fds, GOT_PATH_PROG_READ_TAG,
1790 repo->path);
1791 /* not reached */
1794 if (close(imsg_fds[1]) == -1) {
1795 err = got_error_from_errno("close");
1796 free(ibuf);
1797 return err;
1799 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TAG].imsg_fd =
1800 imsg_fds[0];
1801 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TAG].pid = pid;
1802 imsg_init(ibuf, imsg_fds[0]);
1803 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TAG].ibuf = ibuf;
1805 return request_tag(tag, repo, obj_fd, id);
1808 static const struct got_error *
1809 open_tag(struct got_tag_object **tag, struct got_repository *repo,
1810 struct got_object_id *id, int check_cache)
1812 const struct got_error *err = NULL;
1813 struct got_packidx *packidx = NULL;
1814 int idx;
1815 char *path_packfile = NULL;
1816 struct got_object *obj = NULL;
1817 int obj_type = GOT_OBJ_TYPE_ANY;
1819 if (check_cache) {
1820 *tag = got_repo_get_cached_tag(repo, id);
1821 if (*tag != NULL) {
1822 (*tag)->refcnt++;
1823 return NULL;
1825 } else
1826 *tag = NULL;
1828 err = got_repo_search_packidx(&packidx, &idx, repo, id);
1829 if (err == NULL) {
1830 struct got_pack *pack = NULL;
1832 err = got_packidx_get_packfile_path(&path_packfile,
1833 packidx->path_packidx);
1834 if (err)
1835 return err;
1837 pack = got_repo_get_cached_pack(repo, path_packfile);
1838 if (pack == NULL) {
1839 err = got_repo_cache_pack(&pack, repo, path_packfile,
1840 packidx);
1841 if (err)
1842 goto done;
1845 /* Beware of "lightweight" tags: Check object type first. */
1846 err = read_packed_object_privsep(&obj, repo, pack, packidx,
1847 idx, id);
1848 if (err)
1849 goto done;
1850 obj_type = obj->type;
1851 got_object_close(obj);
1852 if (obj_type != GOT_OBJ_TYPE_TAG) {
1853 err = got_error(GOT_ERR_OBJ_TYPE);
1854 goto done;
1856 err = read_packed_tag_privsep(tag, pack, packidx, idx, id);
1857 } else if (err->code == GOT_ERR_NO_OBJ) {
1858 int fd;
1860 err = got_object_open_loose_fd(&fd, id, repo);
1861 if (err)
1862 return err;
1863 err = got_object_read_header_privsep(&obj, id, repo, fd);
1864 if (err)
1865 return err;
1866 obj_type = obj->type;
1867 got_object_close(obj);
1868 if (obj_type != GOT_OBJ_TYPE_TAG)
1869 return got_error(GOT_ERR_OBJ_TYPE);
1871 err = got_object_open_loose_fd(&fd, id, repo);
1872 if (err)
1873 return err;
1874 err = read_tag_privsep(tag, fd, id, repo);
1877 if (err == NULL) {
1878 (*tag)->refcnt++;
1879 err = got_repo_cache_tag(repo, id, *tag);
1881 done:
1882 free(path_packfile);
1883 return err;
1886 const struct got_error *
1887 got_object_open_as_tag(struct got_tag_object **tag,
1888 struct got_repository *repo, struct got_object_id *id)
1890 *tag = got_repo_get_cached_tag(repo, id);
1891 if (*tag != NULL) {
1892 (*tag)->refcnt++;
1893 return NULL;
1896 return open_tag(tag, repo, id, 0);
1899 const struct got_error *
1900 got_object_tag_open(struct got_tag_object **tag,
1901 struct got_repository *repo, struct got_object *obj)
1903 return open_tag(tag, repo, got_object_get_id(obj), 1);
1906 const char *
1907 got_object_tag_get_name(struct got_tag_object *tag)
1909 return tag->tag;
1912 int
1913 got_object_tag_get_object_type(struct got_tag_object *tag)
1915 return tag->obj_type;
1918 struct got_object_id *
1919 got_object_tag_get_object_id(struct got_tag_object *tag)
1921 return &tag->id;
1924 time_t
1925 got_object_tag_get_tagger_time(struct got_tag_object *tag)
1927 return tag->tagger_time;
1930 time_t
1931 got_object_tag_get_tagger_gmtoff(struct got_tag_object *tag)
1933 return tag->tagger_gmtoff;
1936 const char *
1937 got_object_tag_get_tagger(struct got_tag_object *tag)
1939 return tag->tagger;
1942 const char *
1943 got_object_tag_get_message(struct got_tag_object *tag)
1945 return tag->tagmsg;
1948 static struct got_tree_entry *
1949 find_entry_by_name(struct got_tree_object *tree, const char *name, size_t len)
1951 int i;
1953 /* Note that tree entries are sorted in strncmp() order. */
1954 for (i = 0; i < tree->nentries; i++) {
1955 struct got_tree_entry *te = &tree->entries[i];
1956 int cmp = strncmp(te->name, name, len);
1957 if (cmp < 0)
1958 continue;
1959 if (cmp > 0)
1960 break;
1961 if (te->name[len] == '\0')
1962 return te;
1964 return NULL;
1967 struct got_tree_entry *
1968 got_object_tree_find_entry(struct got_tree_object *tree, const char *name)
1970 return find_entry_by_name(tree, name, strlen(name));
1973 const struct got_error *
1974 got_object_tree_find_path(struct got_object_id **id, mode_t *mode,
1975 struct got_repository *repo, struct got_tree_object *tree,
1976 const char *path)
1978 const struct got_error *err = NULL;
1979 struct got_tree_object *subtree = NULL;
1980 struct got_tree_entry *te = NULL;
1981 const char *seg, *s;
1982 size_t seglen;
1984 *id = NULL;
1986 s = path;
1987 while (s[0] == '/')
1988 s++;
1989 seg = s;
1990 seglen = 0;
1991 subtree = tree;
1992 while (*s) {
1993 struct got_tree_object *next_tree;
1995 if (*s != '/') {
1996 s++;
1997 seglen++;
1998 if (*s)
1999 continue;
2002 te = find_entry_by_name(subtree, seg, seglen);
2003 if (te == NULL) {
2004 err = got_error_path(path, GOT_ERR_NO_TREE_ENTRY);
2005 goto done;
2008 if (*s == '\0')
2009 break;
2011 seg = s + 1;
2012 seglen = 0;
2013 s++;
2014 if (*s) {
2015 err = got_object_open_as_tree(&next_tree, repo,
2016 &te->id);
2017 te = NULL;
2018 if (err)
2019 goto done;
2020 if (subtree != tree)
2021 got_object_tree_close(subtree);
2022 subtree = next_tree;
2026 if (te) {
2027 *id = got_object_id_dup(&te->id);
2028 if (*id == NULL)
2029 return got_error_from_errno("got_object_id_dup");
2030 if (mode)
2031 *mode = te->mode;
2032 } else
2033 err = got_error_path(path, GOT_ERR_NO_TREE_ENTRY);
2034 done:
2035 if (subtree && subtree != tree)
2036 got_object_tree_close(subtree);
2037 return err;
2039 const struct got_error *
2040 got_object_id_by_path(struct got_object_id **id, struct got_repository *repo,
2041 struct got_commit_object *commit, const char *path)
2043 const struct got_error *err = NULL;
2044 struct got_tree_object *tree = NULL;
2046 *id = NULL;
2048 /* Handle opening of root of commit's tree. */
2049 if (got_path_is_root_dir(path)) {
2050 *id = got_object_id_dup(commit->tree_id);
2051 if (*id == NULL)
2052 err = got_error_from_errno("got_object_id_dup");
2053 } else {
2054 err = got_object_open_as_tree(&tree, repo, commit->tree_id);
2055 if (err)
2056 goto done;
2057 err = got_object_tree_find_path(id, NULL, repo, tree, path);
2059 done:
2060 if (tree)
2061 got_object_tree_close(tree);
2062 return err;
2066 * Normalize file mode bits to avoid false positive tree entry differences
2067 * in case tree entries have unexpected mode bits set.
2069 static mode_t
2070 normalize_mode_for_comparison(mode_t mode)
2073 * For directories, the only relevant bit is the IFDIR bit.
2074 * This allows us to detect paths changing from a directory
2075 * to a file and vice versa.
2077 if (S_ISDIR(mode))
2078 return mode & S_IFDIR;
2081 * For symlinks, the only relevant bit is the IFLNK bit.
2082 * This allows us to detect paths changing from a symlinks
2083 * to a file or directory and vice versa.
2085 if (S_ISLNK(mode))
2086 return mode & S_IFLNK;
2088 /* For files, the only change we care about is the executable bit. */
2089 return mode & S_IXUSR;
2092 const struct got_error *
2093 got_object_tree_path_changed(int *changed,
2094 struct got_tree_object *tree01, struct got_tree_object *tree02,
2095 const char *path, struct got_repository *repo)
2097 const struct got_error *err = NULL;
2098 struct got_tree_object *tree1 = NULL, *tree2 = NULL;
2099 struct got_tree_entry *te1 = NULL, *te2 = NULL;
2100 const char *seg, *s;
2101 size_t seglen;
2103 *changed = 0;
2105 /* We not do support comparing the root path. */
2106 if (got_path_is_root_dir(path))
2107 return got_error_path(path, GOT_ERR_BAD_PATH);
2109 tree1 = tree01;
2110 tree2 = tree02;
2111 s = path;
2112 while (*s == '/')
2113 s++;
2114 seg = s;
2115 seglen = 0;
2116 while (*s) {
2117 struct got_tree_object *next_tree1, *next_tree2;
2118 mode_t mode1, mode2;
2120 if (*s != '/') {
2121 s++;
2122 seglen++;
2123 if (*s)
2124 continue;
2127 te1 = find_entry_by_name(tree1, seg, seglen);
2128 if (te1 == NULL) {
2129 err = got_error(GOT_ERR_NO_OBJ);
2130 goto done;
2133 if (tree2)
2134 te2 = find_entry_by_name(tree2, seg, seglen);
2136 if (te2) {
2137 mode1 = normalize_mode_for_comparison(te1->mode);
2138 mode2 = normalize_mode_for_comparison(te2->mode);
2139 if (mode1 != mode2) {
2140 *changed = 1;
2141 goto done;
2144 if (got_object_id_cmp(&te1->id, &te2->id) == 0) {
2145 *changed = 0;
2146 goto done;
2150 if (*s == '\0') { /* final path element */
2151 *changed = 1;
2152 goto done;
2155 seg = s + 1;
2156 s++;
2157 seglen = 0;
2158 if (*s) {
2159 err = got_object_open_as_tree(&next_tree1, repo,
2160 &te1->id);
2161 te1 = NULL;
2162 if (err)
2163 goto done;
2164 if (tree1 != tree01)
2165 got_object_tree_close(tree1);
2166 tree1 = next_tree1;
2168 if (te2) {
2169 err = got_object_open_as_tree(&next_tree2, repo,
2170 &te2->id);
2171 te2 = NULL;
2172 if (err)
2173 goto done;
2174 if (tree2 != tree02)
2175 got_object_tree_close(tree2);
2176 tree2 = next_tree2;
2177 } else if (tree2) {
2178 if (tree2 != tree02)
2179 got_object_tree_close(tree2);
2180 tree2 = NULL;
2184 done:
2185 if (tree1 && tree1 != tree01)
2186 got_object_tree_close(tree1);
2187 if (tree2 && tree2 != tree02)
2188 got_object_tree_close(tree2);
2189 return err;
2192 const struct got_error *
2193 got_object_tree_entry_dup(struct got_tree_entry **new_te,
2194 struct got_tree_entry *te)
2196 const struct got_error *err = NULL;
2198 *new_te = calloc(1, sizeof(**new_te));
2199 if (*new_te == NULL)
2200 return got_error_from_errno("calloc");
2202 (*new_te)->mode = te->mode;
2203 memcpy((*new_te)->name, te->name, sizeof((*new_te)->name));
2204 memcpy(&(*new_te)->id, &te->id, sizeof((*new_te)->id));
2205 return err;
2208 int
2209 got_object_tree_entry_is_submodule(struct got_tree_entry *te)
2211 return (te->mode & S_IFMT) == (S_IFDIR | S_IFLNK);
2214 int
2215 got_object_tree_entry_is_symlink(struct got_tree_entry *te)
2217 /* S_IFDIR check avoids confusing symlinks with submodules. */
2218 return ((te->mode & (S_IFDIR | S_IFLNK)) == S_IFLNK);
2221 static const struct got_error *
2222 resolve_symlink(char **link_target, const char *path,
2223 struct got_commit_object *commit, struct got_repository *repo)
2225 const struct got_error *err = NULL;
2226 char buf[PATH_MAX];
2227 char *name, *parent_path = NULL;
2228 struct got_object_id *tree_obj_id = NULL;
2229 struct got_tree_object *tree = NULL;
2230 struct got_tree_entry *te = NULL;
2232 *link_target = NULL;
2234 if (strlcpy(buf, path, sizeof(buf)) >= sizeof(buf))
2235 return got_error(GOT_ERR_NO_SPACE);
2237 name = basename(buf);
2238 if (name == NULL)
2239 return got_error_from_errno2("basename", path);
2241 err = got_path_dirname(&parent_path, path);
2242 if (err)
2243 return err;
2245 err = got_object_id_by_path(&tree_obj_id, repo, commit,
2246 parent_path);
2247 if (err) {
2248 if (err->code == GOT_ERR_NO_TREE_ENTRY) {
2249 /* Display the complete path in error message. */
2250 err = got_error_path(path, err->code);
2252 goto done;
2255 err = got_object_open_as_tree(&tree, repo, tree_obj_id);
2256 if (err)
2257 goto done;
2259 te = got_object_tree_find_entry(tree, name);
2260 if (te == NULL) {
2261 err = got_error_path(path, GOT_ERR_NO_TREE_ENTRY);
2262 goto done;
2265 if (got_object_tree_entry_is_symlink(te)) {
2266 err = got_tree_entry_get_symlink_target(link_target, te, repo);
2267 if (err)
2268 goto done;
2269 if (!got_path_is_absolute(*link_target)) {
2270 char *abspath;
2271 if (asprintf(&abspath, "%s/%s", parent_path,
2272 *link_target) == -1) {
2273 err = got_error_from_errno("asprintf");
2274 goto done;
2276 free(*link_target);
2277 *link_target = malloc(PATH_MAX);
2278 if (*link_target == NULL) {
2279 err = got_error_from_errno("malloc");
2280 goto done;
2282 err = got_canonpath(abspath, *link_target, PATH_MAX);
2283 free(abspath);
2284 if (err)
2285 goto done;
2288 done:
2289 free(tree_obj_id);
2290 if (tree)
2291 got_object_tree_close(tree);
2292 if (err) {
2293 free(*link_target);
2294 *link_target = NULL;
2296 return err;
2299 const struct got_error *
2300 got_object_resolve_symlinks(char **link_target, const char *path,
2301 struct got_commit_object *commit, struct got_repository *repo)
2303 const struct got_error *err = NULL;
2304 char *next_target = NULL;
2305 int max_recursion = 40; /* matches Git */
2307 *link_target = NULL;
2309 do {
2310 err = resolve_symlink(&next_target,
2311 *link_target ? *link_target : path, commit, repo);
2312 if (err)
2313 break;
2314 if (next_target) {
2315 free(*link_target);
2316 if (--max_recursion == 0) {
2317 err = got_error_path(path, GOT_ERR_RECURSION);
2318 *link_target = NULL;
2319 break;
2321 *link_target = next_target;
2323 } while (next_target);
2325 return err;
2328 const struct got_error *
2329 got_traverse_packed_commits(struct got_object_id_queue *traversed_commits,
2330 struct got_object_id *commit_id, const char *path,
2331 struct got_repository *repo)
2333 const struct got_error *err = NULL;
2334 struct got_pack *pack = NULL;
2335 struct got_packidx *packidx = NULL;
2336 char *path_packfile = NULL;
2337 struct got_commit_object *changed_commit = NULL;
2338 struct got_object_id *changed_commit_id = NULL;
2339 int idx;
2341 err = got_repo_search_packidx(&packidx, &idx, repo, commit_id);
2342 if (err) {
2343 if (err->code != GOT_ERR_NO_OBJ)
2344 return err;
2345 return NULL;
2348 err = got_packidx_get_packfile_path(&path_packfile,
2349 packidx->path_packidx);
2350 if (err)
2351 return err;
2353 pack = got_repo_get_cached_pack(repo, path_packfile);
2354 if (pack == NULL) {
2355 err = got_repo_cache_pack(&pack, repo, path_packfile, packidx);
2356 if (err)
2357 goto done;
2360 if (pack->privsep_child == NULL) {
2361 err = start_pack_privsep_child(pack, packidx);
2362 if (err)
2363 goto done;
2366 err = got_privsep_send_commit_traversal_request(
2367 pack->privsep_child->ibuf, commit_id, idx, path);
2368 if (err)
2369 goto done;
2371 err = got_privsep_recv_traversed_commits(&changed_commit,
2372 &changed_commit_id, traversed_commits, pack->privsep_child->ibuf);
2373 if (err)
2374 goto done;
2376 if (changed_commit) {
2378 * Cache the commit in which the path was changed.
2379 * This commit might be opened again soon.
2381 changed_commit->refcnt++;
2382 err = got_repo_cache_commit(repo, changed_commit_id,
2383 changed_commit);
2384 got_object_commit_close(changed_commit);
2386 done:
2387 free(path_packfile);
2388 free(changed_commit_id);
2389 return err;