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 = -1, accumfd = -1;
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 = dup(pack->basefd);
185 if (basefd == -1)
186 return got_error_from_errno("dup");
188 accumfd = dup(pack->accumfd);
189 if (accumfd == -1) {
190 err = got_error_from_errno("dup");
191 goto done;
194 err = got_privsep_send_tmpfd(ibuf, basefd);
195 if (err)
196 goto done;
198 err = got_privsep_send_tmpfd(ibuf, accumfd);
199 done:
200 if (err) {
201 if (basefd != -1)
202 close(basefd);
203 if (accumfd != -1)
204 close(accumfd);
205 } else
206 pack->child_has_tempfiles = 1;
207 return NULL;
210 static const struct got_error *
211 request_packed_object_raw(uint8_t **outbuf, off_t *size, size_t *hdrlen,
212 int outfd, struct got_pack *pack, int idx, struct got_object_id *id)
214 const struct got_error *err = NULL;
215 struct imsgbuf *ibuf = pack->privsep_child->ibuf;
216 int outfd_child;
218 err = pack_child_send_tempfiles(ibuf, pack);
219 if (err)
220 return err;
222 outfd_child = dup(outfd);
223 if (outfd_child == -1)
224 return got_error_from_errno("dup");
226 err = got_privsep_send_packed_raw_obj_req(ibuf, idx, id);
227 if (err) {
228 close(outfd_child);
229 return err;
232 err = got_privsep_send_raw_obj_outfd(ibuf, outfd_child);
233 if (err)
234 return err;
236 err = got_privsep_recv_raw_obj(outbuf, size, hdrlen, ibuf);
237 if (err)
238 return err;
240 return NULL;
243 static void
244 set_max_datasize(void)
246 struct rlimit rl;
248 if (getrlimit(RLIMIT_DATA, &rl) != 0)
249 return;
251 rl.rlim_cur = rl.rlim_max;
252 setrlimit(RLIMIT_DATA, &rl);
255 static const struct got_error *
256 start_pack_privsep_child(struct got_pack *pack, struct got_packidx *packidx)
258 const struct got_error *err = NULL;
259 int imsg_fds[2];
260 pid_t pid;
261 struct imsgbuf *ibuf;
263 ibuf = calloc(1, sizeof(*ibuf));
264 if (ibuf == NULL)
265 return got_error_from_errno("calloc");
267 pack->privsep_child = calloc(1, sizeof(*pack->privsep_child));
268 if (pack->privsep_child == NULL) {
269 err = got_error_from_errno("calloc");
270 free(ibuf);
271 return err;
273 pack->child_has_tempfiles = 0;
274 pack->child_has_delta_outfd = 0;
276 if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_fds) == -1) {
277 err = got_error_from_errno("socketpair");
278 goto done;
281 pid = fork();
282 if (pid == -1) {
283 err = got_error_from_errno("fork");
284 goto done;
285 } else if (pid == 0) {
286 set_max_datasize();
287 got_privsep_exec_child(imsg_fds, GOT_PATH_PROG_READ_PACK,
288 pack->path_packfile);
289 /* not reached */
292 if (close(imsg_fds[1]) == -1)
293 return got_error_from_errno("close");
294 pack->privsep_child->imsg_fd = imsg_fds[0];
295 pack->privsep_child->pid = pid;
296 imsg_init(ibuf, imsg_fds[0]);
297 pack->privsep_child->ibuf = ibuf;
299 err = got_privsep_init_pack_child(ibuf, pack, packidx);
300 if (err) {
301 const struct got_error *child_err;
302 err = got_privsep_send_stop(pack->privsep_child->imsg_fd);
303 child_err = got_privsep_wait_for_child(
304 pack->privsep_child->pid);
305 if (child_err && err == NULL)
306 err = child_err;
308 done:
309 if (err) {
310 free(ibuf);
311 free(pack->privsep_child);
312 pack->privsep_child = NULL;
314 return err;
317 static const struct got_error *
318 read_packed_object_privsep(struct got_object **obj,
319 struct got_repository *repo, struct got_pack *pack,
320 struct got_packidx *packidx, int idx, struct got_object_id *id)
322 const struct got_error *err = NULL;
324 if (pack->privsep_child == NULL) {
325 err = start_pack_privsep_child(pack, packidx);
326 if (err)
327 return err;
330 return request_packed_object(obj, pack, idx, id);
333 static const struct got_error *
334 read_packed_object_raw_privsep(uint8_t **outbuf, off_t *size, size_t *hdrlen,
335 int outfd, struct got_pack *pack, struct got_packidx *packidx, int idx,
336 struct got_object_id *id)
338 const struct got_error *err = NULL;
340 if (pack->privsep_child == NULL) {
341 err = start_pack_privsep_child(pack, packidx);
342 if (err)
343 return err;
346 return request_packed_object_raw(outbuf, size, hdrlen, outfd, pack,
347 idx, id);
350 const struct got_error *
351 got_object_open_packed(struct got_object **obj, struct got_object_id *id,
352 struct got_repository *repo)
354 const struct got_error *err = NULL;
355 struct got_pack *pack = NULL;
356 struct got_packidx *packidx = NULL;
357 int idx;
358 char *path_packfile;
360 err = got_repo_search_packidx(&packidx, &idx, repo, id);
361 if (err)
362 return err;
364 err = got_packidx_get_packfile_path(&path_packfile,
365 packidx->path_packidx);
366 if (err)
367 return err;
369 pack = got_repo_get_cached_pack(repo, path_packfile);
370 if (pack == NULL) {
371 err = got_repo_cache_pack(&pack, repo, path_packfile, packidx);
372 if (err)
373 goto done;
376 err = read_packed_object_privsep(obj, repo, pack, packidx, idx, id);
377 if (err)
378 goto done;
379 done:
380 free(path_packfile);
381 return err;
384 const struct got_error *
385 got_object_open_from_packfile(struct got_object **obj, struct got_object_id *id,
386 struct got_pack *pack, struct got_packidx *packidx, int obj_idx,
387 struct got_repository *repo)
389 return read_packed_object_privsep(obj, repo, pack, packidx,
390 obj_idx, id);
393 const struct got_error *
394 got_object_read_raw_delta(uint64_t *base_size, uint64_t *result_size,
395 off_t *delta_size, off_t *delta_compressed_size, off_t *delta_offset,
396 off_t *delta_out_offset, struct got_object_id **base_id, int delta_cache_fd,
397 struct got_packidx *packidx, int obj_idx, struct got_object_id *id,
398 struct got_repository *repo)
400 const struct got_error *err = NULL;
401 struct got_pack *pack = NULL;
402 char *path_packfile;
404 *base_size = 0;
405 *result_size = 0;
406 *delta_size = 0;
407 *delta_compressed_size = 0;
408 *delta_offset = 0;
409 *delta_out_offset = 0;
411 err = got_packidx_get_packfile_path(&path_packfile,
412 packidx->path_packidx);
413 if (err)
414 return err;
416 pack = got_repo_get_cached_pack(repo, path_packfile);
417 if (pack == NULL) {
418 err = got_repo_cache_pack(&pack, repo, path_packfile, packidx);
419 if (err)
420 return err;
423 if (pack->privsep_child == NULL) {
424 err = start_pack_privsep_child(pack, packidx);
425 if (err)
426 return err;
429 if (!pack->child_has_delta_outfd) {
430 int outfd_child;
431 outfd_child = dup(delta_cache_fd);
432 if (outfd_child == -1)
433 return got_error_from_errno("dup");
434 err = got_privsep_send_raw_delta_outfd(
435 pack->privsep_child->ibuf, outfd_child);
436 if (err)
437 return err;
438 pack->child_has_delta_outfd = 1;
441 err = got_privsep_send_raw_delta_req(pack->privsep_child->ibuf,
442 obj_idx, id);
443 if (err)
444 return err;
446 return got_privsep_recv_raw_delta(base_size, result_size, delta_size,
447 delta_compressed_size, delta_offset, delta_out_offset, base_id,
448 pack->privsep_child->ibuf);
451 /*
452 * XXX This function does not really belong in object.c. It is only here
453 * because it needs start_pack_privsep_child(); relevant code should
454 * probably be moved to pack.c/pack_create.c.
455 */
456 const struct got_error *
457 got_object_prepare_delta_reuse(struct got_pack **pack,
458 struct got_packidx *packidx, int delta_outfd, struct got_repository *repo)
460 const struct got_error *err = NULL;
461 char *path_packfile = NULL;
463 err = got_packidx_get_packfile_path(&path_packfile,
464 packidx->path_packidx);
465 if (err)
466 return err;
468 *pack = got_repo_get_cached_pack(repo, path_packfile);
469 if (*pack == NULL) {
470 err = got_repo_cache_pack(pack, repo, path_packfile, packidx);
471 if (err)
472 goto done;
474 if ((*pack)->privsep_child == NULL) {
475 err = start_pack_privsep_child(*pack, packidx);
476 if (err)
477 goto done;
480 if (!(*pack)->child_has_delta_outfd) {
481 int outfd_child;
482 outfd_child = dup(delta_outfd);
483 if (outfd_child == -1) {
484 err = got_error_from_errno("dup");
485 goto done;
487 err = got_privsep_send_raw_delta_outfd(
488 (*pack)->privsep_child->ibuf, outfd_child);
489 if (err)
490 goto done;
491 (*pack)->child_has_delta_outfd = 1;
494 err = got_privsep_send_delta_reuse_req((*pack)->privsep_child->ibuf);
495 done:
496 free(path_packfile);
497 return err;
500 static const struct got_error *
501 request_object(struct got_object **obj, struct got_object_id *id,
502 struct got_repository *repo, int fd)
504 const struct got_error *err = NULL;
505 struct imsgbuf *ibuf;
507 ibuf = repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_OBJECT].ibuf;
509 err = got_privsep_send_obj_req(ibuf, fd, id);
510 if (err)
511 return err;
513 return got_privsep_recv_obj(obj, ibuf);
516 static const struct got_error *
517 request_raw_object(uint8_t **outbuf, off_t *size, size_t *hdrlen, int outfd,
518 struct got_object_id *id, struct got_repository *repo, int infd)
520 const struct got_error *err = NULL;
521 struct imsgbuf *ibuf;
522 int outfd_child;
524 ibuf = repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_OBJECT].ibuf;
526 outfd_child = dup(outfd);
527 if (outfd_child == -1)
528 return got_error_from_errno("dup");
530 err = got_privsep_send_raw_obj_req(ibuf, infd, id);
531 if (err)
532 return err;
534 err = got_privsep_send_raw_obj_outfd(ibuf, outfd_child);
535 if (err)
536 return err;
538 return got_privsep_recv_raw_obj(outbuf, size, hdrlen, ibuf);
541 static const struct got_error *
542 start_read_object_child(struct got_repository *repo)
544 const struct got_error *err = NULL;
545 int imsg_fds[2];
546 pid_t pid;
547 struct imsgbuf *ibuf;
549 ibuf = calloc(1, sizeof(*ibuf));
550 if (ibuf == NULL)
551 return got_error_from_errno("calloc");
553 if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_fds) == -1) {
554 err = got_error_from_errno("socketpair");
555 free(ibuf);
556 return err;
559 pid = fork();
560 if (pid == -1) {
561 err = got_error_from_errno("fork");
562 free(ibuf);
563 return err;
565 else if (pid == 0) {
566 got_privsep_exec_child(imsg_fds, GOT_PATH_PROG_READ_OBJECT,
567 repo->path);
568 /* not reached */
571 if (close(imsg_fds[1]) == -1) {
572 err = got_error_from_errno("close");
573 free(ibuf);
574 return err;
577 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_OBJECT].imsg_fd =
578 imsg_fds[0];
579 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_OBJECT].pid = pid;
580 imsg_init(ibuf, imsg_fds[0]);
581 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_OBJECT].ibuf = ibuf;
583 return NULL;
586 const struct got_error *
587 got_object_read_header_privsep(struct got_object **obj,
588 struct got_object_id *id, struct got_repository *repo, int obj_fd)
590 const struct got_error *err;
592 if (repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_OBJECT].imsg_fd != -1)
593 return request_object(obj, id, repo, obj_fd);
595 err = start_read_object_child(repo);
596 if (err) {
597 close(obj_fd);
598 return err;
601 return request_object(obj, id, repo, obj_fd);
604 static const struct got_error *
605 read_object_raw_privsep(uint8_t **outbuf, off_t *size, size_t *hdrlen,
606 int outfd, struct got_object_id *id, struct got_repository *repo,
607 int obj_fd)
609 const struct got_error *err;
611 if (repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_OBJECT].imsg_fd != -1)
612 return request_raw_object(outbuf, size, hdrlen, outfd, id,
613 repo, obj_fd);
615 err = start_read_object_child(repo);
616 if (err)
617 return err;
619 return request_raw_object(outbuf, size, hdrlen, outfd, id, repo,
620 obj_fd);
623 const struct got_error *
624 got_object_open(struct got_object **obj, struct got_repository *repo,
625 struct got_object_id *id)
627 const struct got_error *err = NULL;
628 int fd;
630 *obj = got_repo_get_cached_object(repo, id);
631 if (*obj != NULL) {
632 (*obj)->refcnt++;
633 return NULL;
636 err = got_object_open_packed(obj, id, repo);
637 if (err && err->code != GOT_ERR_NO_OBJ)
638 return err;
639 if (*obj) {
640 (*obj)->refcnt++;
641 return got_repo_cache_object(repo, id, *obj);
644 err = got_object_open_loose_fd(&fd, id, repo);
645 if (err) {
646 if (err->code == GOT_ERR_ERRNO && errno == ENOENT)
647 err = got_error_no_obj(id);
648 return err;
651 err = got_object_read_header_privsep(obj, id, repo, fd);
652 if (err)
653 return err;
655 memcpy((*obj)->id.sha1, id->sha1, SHA1_DIGEST_LENGTH);
657 (*obj)->refcnt++;
658 return got_repo_cache_object(repo, id, *obj);
661 /* *outfd must be initialized to -1 by caller */
662 const struct got_error *
663 got_object_raw_open(struct got_raw_object **obj, int *outfd,
664 struct got_repository *repo, struct got_object_id *id)
666 const struct got_error *err = NULL;
667 struct got_packidx *packidx = NULL;
668 int idx;
669 uint8_t *outbuf = NULL;
670 off_t size = 0;
671 size_t hdrlen = 0;
672 char *path_packfile = NULL;
674 *obj = got_repo_get_cached_raw_object(repo, id);
675 if (*obj != NULL) {
676 (*obj)->refcnt++;
677 return NULL;
680 if (*outfd == -1) {
681 *outfd = got_opentempfd();
682 if (*outfd == -1)
683 return got_error_from_errno("got_opentempfd");
686 err = got_repo_search_packidx(&packidx, &idx, repo, id);
687 if (err == NULL) {
688 struct got_pack *pack = NULL;
690 err = got_packidx_get_packfile_path(&path_packfile,
691 packidx->path_packidx);
692 if (err)
693 goto done;
695 pack = got_repo_get_cached_pack(repo, path_packfile);
696 if (pack == NULL) {
697 err = got_repo_cache_pack(&pack, repo, path_packfile,
698 packidx);
699 if (err)
700 goto done;
702 err = read_packed_object_raw_privsep(&outbuf, &size, &hdrlen,
703 *outfd, pack, packidx, idx, id);
704 if (err)
705 goto done;
706 } else if (err->code == GOT_ERR_NO_OBJ) {
707 int fd;
709 err = got_object_open_loose_fd(&fd, id, repo);
710 if (err)
711 goto done;
712 err = read_object_raw_privsep(&outbuf, &size, &hdrlen, *outfd,
713 id, repo, fd);
714 if (err)
715 goto done;
718 *obj = calloc(1, sizeof(**obj));
719 if (*obj == NULL) {
720 err = got_error_from_errno("calloc");
721 goto done;
723 (*obj)->fd = -1;
725 if (outbuf) {
726 (*obj)->data = outbuf;
727 } else {
728 struct stat sb;
729 if (fstat(*outfd, &sb) == -1) {
730 err = got_error_from_errno("fstat");
731 goto done;
734 if (sb.st_size != hdrlen + size) {
735 err = got_error(GOT_ERR_PRIVSEP_LEN);
736 goto done;
738 #ifndef GOT_PACK_NO_MMAP
739 if (hdrlen + size > 0) {
740 (*obj)->data = mmap(NULL, hdrlen + size, PROT_READ,
741 MAP_PRIVATE, *outfd, 0);
742 if ((*obj)->data == MAP_FAILED) {
743 if (errno != ENOMEM) {
744 err = got_error_from_errno("mmap");
745 goto done;
747 (*obj)->data = NULL;
748 } else {
749 (*obj)->fd = *outfd;
750 *outfd = -1;
753 #endif
754 if (*outfd != -1) {
755 (*obj)->f = fdopen(*outfd, "r");
756 if ((*obj)->f == NULL) {
757 err = got_error_from_errno("fdopen");
758 goto done;
760 *outfd = -1;
763 (*obj)->hdrlen = hdrlen;
764 (*obj)->size = size;
765 err = got_repo_cache_raw_object(repo, id, *obj);
766 done:
767 free(path_packfile);
768 if (err) {
769 if (*obj) {
770 got_object_raw_close(*obj);
771 *obj = NULL;
773 free(outbuf);
774 } else
775 (*obj)->refcnt++;
776 return err;
779 const struct got_error *
780 got_object_open_by_id_str(struct got_object **obj, struct got_repository *repo,
781 const char *id_str)
783 struct got_object_id id;
785 if (!got_parse_sha1_digest(id.sha1, id_str))
786 return got_error_path(id_str, GOT_ERR_BAD_OBJ_ID_STR);
788 return got_object_open(obj, repo, &id);
791 const struct got_error *
792 got_object_resolve_id_str(struct got_object_id **id,
793 struct got_repository *repo, const char *id_str)
795 const struct got_error *err = NULL;
796 struct got_object *obj;
798 err = got_object_open_by_id_str(&obj, repo, id_str);
799 if (err)
800 return err;
802 *id = got_object_id_dup(got_object_get_id(obj));
803 got_object_close(obj);
804 if (*id == NULL)
805 return got_error_from_errno("got_object_id_dup");
807 return NULL;
810 static const struct got_error *
811 request_packed_commit(struct got_commit_object **commit, struct got_pack *pack,
812 int pack_idx, struct got_object_id *id)
814 const struct got_error *err = NULL;
816 err = got_privsep_send_commit_req(pack->privsep_child->ibuf, -1, id,
817 pack_idx);
818 if (err)
819 return err;
821 err = got_privsep_recv_commit(commit, pack->privsep_child->ibuf);
822 if (err)
823 return err;
825 (*commit)->flags |= GOT_COMMIT_FLAG_PACKED;
826 return NULL;
829 static const struct got_error *
830 read_packed_commit_privsep(struct got_commit_object **commit,
831 struct got_pack *pack, struct got_packidx *packidx, int idx,
832 struct got_object_id *id)
834 const struct got_error *err = NULL;
836 if (pack->privsep_child)
837 return request_packed_commit(commit, pack, idx, id);
839 err = start_pack_privsep_child(pack, packidx);
840 if (err)
841 return err;
843 return request_packed_commit(commit, pack, idx, id);
846 static const struct got_error *
847 request_commit(struct got_commit_object **commit, struct got_repository *repo,
848 int fd, struct got_object_id *id)
850 const struct got_error *err = NULL;
851 struct imsgbuf *ibuf;
853 ibuf = repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_COMMIT].ibuf;
855 err = got_privsep_send_commit_req(ibuf, fd, id, -1);
856 if (err)
857 return err;
859 return got_privsep_recv_commit(commit, ibuf);
862 static const struct got_error *
863 read_commit_privsep(struct got_commit_object **commit, int obj_fd,
864 struct got_object_id *id, struct got_repository *repo)
866 const struct got_error *err;
867 int imsg_fds[2];
868 pid_t pid;
869 struct imsgbuf *ibuf;
871 if (repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_COMMIT].imsg_fd != -1)
872 return request_commit(commit, repo, obj_fd, id);
874 ibuf = calloc(1, sizeof(*ibuf));
875 if (ibuf == NULL)
876 return got_error_from_errno("calloc");
878 if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_fds) == -1) {
879 err = got_error_from_errno("socketpair");
880 free(ibuf);
881 return err;
884 pid = fork();
885 if (pid == -1) {
886 err = got_error_from_errno("fork");
887 free(ibuf);
888 return err;
890 else if (pid == 0) {
891 got_privsep_exec_child(imsg_fds, GOT_PATH_PROG_READ_COMMIT,
892 repo->path);
893 /* not reached */
896 if (close(imsg_fds[1]) == -1) {
897 err = got_error_from_errno("close");
898 free(ibuf);
899 return err;
901 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_COMMIT].imsg_fd =
902 imsg_fds[0];
903 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_COMMIT].pid = pid;
904 imsg_init(ibuf, imsg_fds[0]);
905 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_COMMIT].ibuf = ibuf;
907 return request_commit(commit, repo, obj_fd, id);
911 static const struct got_error *
912 open_commit(struct got_commit_object **commit,
913 struct got_repository *repo, struct got_object_id *id, int check_cache)
915 const struct got_error *err = NULL;
916 struct got_packidx *packidx = NULL;
917 int idx;
918 char *path_packfile = NULL;
920 if (check_cache) {
921 *commit = got_repo_get_cached_commit(repo, id);
922 if (*commit != NULL) {
923 (*commit)->refcnt++;
924 return NULL;
926 } else
927 *commit = NULL;
929 err = got_repo_search_packidx(&packidx, &idx, repo, id);
930 if (err == NULL) {
931 struct got_pack *pack = NULL;
933 err = got_packidx_get_packfile_path(&path_packfile,
934 packidx->path_packidx);
935 if (err)
936 return err;
938 pack = got_repo_get_cached_pack(repo, path_packfile);
939 if (pack == NULL) {
940 err = got_repo_cache_pack(&pack, repo, path_packfile,
941 packidx);
942 if (err)
943 goto done;
945 err = read_packed_commit_privsep(commit, pack,
946 packidx, idx, id);
947 } else if (err->code == GOT_ERR_NO_OBJ) {
948 int fd;
950 err = got_object_open_loose_fd(&fd, id, repo);
951 if (err)
952 return err;
953 err = read_commit_privsep(commit, fd, id, repo);
956 if (err == NULL) {
957 (*commit)->refcnt++;
958 err = got_repo_cache_commit(repo, id, *commit);
960 done:
961 free(path_packfile);
962 return err;
965 const struct got_error *
966 got_object_open_as_commit(struct got_commit_object **commit,
967 struct got_repository *repo, struct got_object_id *id)
969 *commit = got_repo_get_cached_commit(repo, id);
970 if (*commit != NULL) {
971 (*commit)->refcnt++;
972 return NULL;
975 return open_commit(commit, repo, id, 0);
978 const struct got_error *
979 got_object_commit_open(struct got_commit_object **commit,
980 struct got_repository *repo, struct got_object *obj)
982 return open_commit(commit, repo, got_object_get_id(obj), 1);
985 const struct got_error *
986 got_object_qid_alloc(struct got_object_qid **qid, struct got_object_id *id)
988 *qid = calloc(1, sizeof(**qid));
989 if (*qid == NULL)
990 return got_error_from_errno("calloc");
992 memcpy(&(*qid)->id, id, sizeof((*qid)->id));
993 return NULL;
996 const struct got_error *
997 got_object_id_queue_copy(const struct got_object_id_queue *src,
998 struct got_object_id_queue *dest)
1000 const struct got_error *err;
1001 struct got_object_qid *qid;
1003 STAILQ_FOREACH(qid, src, entry) {
1004 struct got_object_qid *new;
1006 * Deep-copy the object ID only. Let the caller deal
1007 * with setting up the new->data pointer if needed.
1009 err = got_object_qid_alloc(&new, &qid->id);
1010 if (err) {
1011 got_object_id_queue_free(dest);
1012 return err;
1014 STAILQ_INSERT_TAIL(dest, new, entry);
1017 return NULL;
1020 static const struct got_error *
1021 request_packed_tree(struct got_tree_object **tree, struct got_pack *pack,
1022 int pack_idx, struct got_object_id *id)
1024 const struct got_error *err = NULL;
1026 err = got_privsep_send_tree_req(pack->privsep_child->ibuf, -1, id,
1027 pack_idx);
1028 if (err)
1029 return err;
1031 return got_privsep_recv_tree(tree, pack->privsep_child->ibuf);
1034 static const struct got_error *
1035 read_packed_tree_privsep(struct got_tree_object **tree,
1036 struct got_pack *pack, struct got_packidx *packidx, int idx,
1037 struct got_object_id *id)
1039 const struct got_error *err = NULL;
1041 if (pack->privsep_child)
1042 return request_packed_tree(tree, pack, idx, id);
1044 err = start_pack_privsep_child(pack, packidx);
1045 if (err)
1046 return err;
1048 return request_packed_tree(tree, pack, idx, id);
1051 static const struct got_error *
1052 request_tree(struct got_tree_object **tree, struct got_repository *repo,
1053 int fd, struct got_object_id *id)
1055 const struct got_error *err = NULL;
1056 struct imsgbuf *ibuf;
1058 ibuf = repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TREE].ibuf;
1060 err = got_privsep_send_tree_req(ibuf, fd, id, -1);
1061 if (err)
1062 return err;
1064 return got_privsep_recv_tree(tree, ibuf);
1067 const struct got_error *
1068 read_tree_privsep(struct got_tree_object **tree, int obj_fd,
1069 struct got_object_id *id, struct got_repository *repo)
1071 const struct got_error *err;
1072 int imsg_fds[2];
1073 pid_t pid;
1074 struct imsgbuf *ibuf;
1076 if (repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TREE].imsg_fd != -1)
1077 return request_tree(tree, repo, obj_fd, id);
1079 ibuf = calloc(1, sizeof(*ibuf));
1080 if (ibuf == NULL)
1081 return got_error_from_errno("calloc");
1083 if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_fds) == -1) {
1084 err = got_error_from_errno("socketpair");
1085 free(ibuf);
1086 return err;
1089 pid = fork();
1090 if (pid == -1) {
1091 err = got_error_from_errno("fork");
1092 free(ibuf);
1093 return err;
1095 else if (pid == 0) {
1096 got_privsep_exec_child(imsg_fds, GOT_PATH_PROG_READ_TREE,
1097 repo->path);
1098 /* not reached */
1101 if (close(imsg_fds[1]) == -1) {
1102 err = got_error_from_errno("close");
1103 free(ibuf);
1104 return err;
1106 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TREE].imsg_fd =
1107 imsg_fds[0];
1108 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TREE].pid = pid;
1109 imsg_init(ibuf, imsg_fds[0]);
1110 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TREE].ibuf = ibuf;
1113 return request_tree(tree, repo, obj_fd, id);
1116 static const struct got_error *
1117 open_tree(struct got_tree_object **tree, struct got_repository *repo,
1118 struct got_object_id *id, int check_cache)
1120 const struct got_error *err = NULL;
1121 struct got_packidx *packidx = NULL;
1122 int idx;
1123 char *path_packfile = NULL;
1125 if (check_cache) {
1126 *tree = got_repo_get_cached_tree(repo, id);
1127 if (*tree != NULL) {
1128 (*tree)->refcnt++;
1129 return NULL;
1131 } else
1132 *tree = NULL;
1134 err = got_repo_search_packidx(&packidx, &idx, repo, id);
1135 if (err == NULL) {
1136 struct got_pack *pack = NULL;
1138 err = got_packidx_get_packfile_path(&path_packfile,
1139 packidx->path_packidx);
1140 if (err)
1141 return err;
1143 pack = got_repo_get_cached_pack(repo, path_packfile);
1144 if (pack == NULL) {
1145 err = got_repo_cache_pack(&pack, repo, path_packfile,
1146 packidx);
1147 if (err)
1148 goto done;
1150 err = read_packed_tree_privsep(tree, pack,
1151 packidx, idx, id);
1152 } else if (err->code == GOT_ERR_NO_OBJ) {
1153 int fd;
1155 err = got_object_open_loose_fd(&fd, id, repo);
1156 if (err)
1157 return err;
1158 err = read_tree_privsep(tree, fd, id, repo);
1161 if (err == NULL) {
1162 (*tree)->refcnt++;
1163 err = got_repo_cache_tree(repo, id, *tree);
1165 done:
1166 free(path_packfile);
1167 return err;
1170 const struct got_error *
1171 got_object_open_as_tree(struct got_tree_object **tree,
1172 struct got_repository *repo, struct got_object_id *id)
1174 *tree = got_repo_get_cached_tree(repo, id);
1175 if (*tree != NULL) {
1176 (*tree)->refcnt++;
1177 return NULL;
1180 return open_tree(tree, repo, id, 0);
1183 const struct got_error *
1184 got_object_tree_open(struct got_tree_object **tree,
1185 struct got_repository *repo, struct got_object *obj)
1187 return open_tree(tree, repo, got_object_get_id(obj), 1);
1190 int
1191 got_object_tree_get_nentries(struct got_tree_object *tree)
1193 return tree->nentries;
1196 struct got_tree_entry *
1197 got_object_tree_get_first_entry(struct got_tree_object *tree)
1199 return got_object_tree_get_entry(tree, 0);
1202 struct got_tree_entry *
1203 got_object_tree_get_last_entry(struct got_tree_object *tree)
1205 return got_object_tree_get_entry(tree, tree->nentries - 1);
1208 struct got_tree_entry *
1209 got_object_tree_get_entry(struct got_tree_object *tree, int i)
1211 if (i < 0 || i >= tree->nentries)
1212 return NULL;
1213 return &tree->entries[i];
1216 mode_t
1217 got_tree_entry_get_mode(struct got_tree_entry *te)
1219 return te->mode;
1222 const char *
1223 got_tree_entry_get_name(struct got_tree_entry *te)
1225 return &te->name[0];
1228 struct got_object_id *
1229 got_tree_entry_get_id(struct got_tree_entry *te)
1231 return &te->id;
1234 const struct got_error *
1235 got_object_blob_read_to_str(char **s, struct got_blob_object *blob)
1237 const struct got_error *err = NULL;
1238 size_t len, totlen, hdrlen, offset;
1240 *s = NULL;
1242 hdrlen = got_object_blob_get_hdrlen(blob);
1243 totlen = 0;
1244 offset = 0;
1245 do {
1246 char *p;
1248 err = got_object_blob_read_block(&len, blob);
1249 if (err)
1250 return err;
1252 if (len == 0)
1253 break;
1255 totlen += len - hdrlen;
1256 p = realloc(*s, totlen + 1);
1257 if (p == NULL) {
1258 err = got_error_from_errno("realloc");
1259 free(*s);
1260 *s = NULL;
1261 return err;
1263 *s = p;
1264 /* Skip blob object header first time around. */
1265 memcpy(*s + offset,
1266 got_object_blob_get_read_buf(blob) + hdrlen, len - hdrlen);
1267 hdrlen = 0;
1268 offset = totlen;
1269 } while (len > 0);
1271 (*s)[totlen] = '\0';
1272 return NULL;
1275 const struct got_error *
1276 got_tree_entry_get_symlink_target(char **link_target, struct got_tree_entry *te,
1277 struct got_repository *repo)
1279 const struct got_error *err = NULL;
1280 struct got_blob_object *blob = NULL;
1282 *link_target = NULL;
1284 if (!got_object_tree_entry_is_symlink(te))
1285 return got_error(GOT_ERR_TREE_ENTRY_TYPE);
1287 err = got_object_open_as_blob(&blob, repo,
1288 got_tree_entry_get_id(te), PATH_MAX);
1289 if (err)
1290 return err;
1292 err = got_object_blob_read_to_str(link_target, blob);
1293 got_object_blob_close(blob);
1294 if (err) {
1295 free(*link_target);
1296 *link_target = NULL;
1298 return err;
1301 int
1302 got_tree_entry_get_index(struct got_tree_entry *te)
1304 return te->idx;
1307 struct got_tree_entry *
1308 got_tree_entry_get_next(struct got_tree_object *tree,
1309 struct got_tree_entry *te)
1311 return got_object_tree_get_entry(tree, te->idx + 1);
1314 struct got_tree_entry *
1315 got_tree_entry_get_prev(struct got_tree_object *tree,
1316 struct got_tree_entry *te)
1318 return got_object_tree_get_entry(tree, te->idx - 1);
1321 static const struct got_error *
1322 request_packed_blob(uint8_t **outbuf, size_t *size, size_t *hdrlen, int outfd,
1323 struct got_pack *pack, struct got_packidx *packidx, int idx,
1324 struct got_object_id *id)
1326 const struct got_error *err = NULL;
1327 struct imsgbuf *ibuf = pack->privsep_child->ibuf;
1328 int outfd_child;
1330 err = pack_child_send_tempfiles(ibuf, pack);
1331 if (err)
1332 return err;
1334 outfd_child = dup(outfd);
1335 if (outfd_child == -1)
1336 return got_error_from_errno("dup");
1338 err = got_privsep_send_blob_req(pack->privsep_child->ibuf, -1, id, idx);
1339 if (err)
1340 return err;
1342 err = got_privsep_send_blob_outfd(pack->privsep_child->ibuf,
1343 outfd_child);
1344 if (err) {
1345 return err;
1348 err = got_privsep_recv_blob(outbuf, size, hdrlen,
1349 pack->privsep_child->ibuf);
1350 if (err)
1351 return err;
1353 if (lseek(outfd, SEEK_SET, 0) == -1)
1354 err = got_error_from_errno("lseek");
1356 return err;
1359 static const struct got_error *
1360 read_packed_blob_privsep(uint8_t **outbuf, size_t *size, size_t *hdrlen,
1361 int outfd, struct got_pack *pack, struct got_packidx *packidx, int idx,
1362 struct got_object_id *id)
1364 const struct got_error *err = NULL;
1366 if (pack->privsep_child == NULL) {
1367 err = start_pack_privsep_child(pack, packidx);
1368 if (err)
1369 return err;
1372 return request_packed_blob(outbuf, size, hdrlen, outfd, pack, packidx,
1373 idx, id);
1376 static const struct got_error *
1377 request_blob(uint8_t **outbuf, size_t *size, size_t *hdrlen, int outfd,
1378 int infd, struct got_object_id *id, struct imsgbuf *ibuf)
1380 const struct got_error *err = NULL;
1381 int outfd_child;
1383 outfd_child = dup(outfd);
1384 if (outfd_child == -1)
1385 return got_error_from_errno("dup");
1387 err = got_privsep_send_blob_req(ibuf, infd, id, -1);
1388 if (err)
1389 return err;
1391 err = got_privsep_send_blob_outfd(ibuf, outfd_child);
1392 if (err)
1393 return err;
1395 err = got_privsep_recv_blob(outbuf, size, hdrlen, ibuf);
1396 if (err)
1397 return err;
1399 if (lseek(outfd, SEEK_SET, 0) == -1)
1400 return got_error_from_errno("lseek");
1402 return err;
1405 static const struct got_error *
1406 read_blob_privsep(uint8_t **outbuf, size_t *size, size_t *hdrlen,
1407 int outfd, int infd, struct got_object_id *id, struct got_repository *repo)
1409 const struct got_error *err;
1410 int imsg_fds[2];
1411 pid_t pid;
1412 struct imsgbuf *ibuf;
1414 if (repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_BLOB].imsg_fd != -1) {
1415 ibuf = repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_BLOB].ibuf;
1416 return request_blob(outbuf, size, hdrlen, outfd, infd, id,
1417 ibuf);
1420 ibuf = calloc(1, sizeof(*ibuf));
1421 if (ibuf == NULL)
1422 return got_error_from_errno("calloc");
1424 if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_fds) == -1) {
1425 err = got_error_from_errno("socketpair");
1426 free(ibuf);
1427 return err;
1430 pid = fork();
1431 if (pid == -1) {
1432 err = got_error_from_errno("fork");
1433 free(ibuf);
1434 return err;
1436 else if (pid == 0) {
1437 got_privsep_exec_child(imsg_fds, GOT_PATH_PROG_READ_BLOB,
1438 repo->path);
1439 /* not reached */
1442 if (close(imsg_fds[1]) == -1) {
1443 err = got_error_from_errno("close");
1444 free(ibuf);
1445 return err;
1447 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_BLOB].imsg_fd =
1448 imsg_fds[0];
1449 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_BLOB].pid = pid;
1450 imsg_init(ibuf, imsg_fds[0]);
1451 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_BLOB].ibuf = ibuf;
1453 return request_blob(outbuf, size, hdrlen, outfd, infd, id, ibuf);
1456 static const struct got_error *
1457 open_blob(struct got_blob_object **blob, struct got_repository *repo,
1458 struct got_object_id *id, size_t blocksize)
1460 const struct got_error *err = NULL;
1461 struct got_packidx *packidx = NULL;
1462 int idx;
1463 char *path_packfile = NULL;
1464 uint8_t *outbuf;
1465 int outfd;
1466 size_t size, hdrlen;
1467 struct stat sb;
1469 *blob = calloc(1, sizeof(**blob));
1470 if (*blob == NULL)
1471 return got_error_from_errno("calloc");
1473 outfd = got_opentempfd();
1474 if (outfd == -1)
1475 return got_error_from_errno("got_opentempfd");
1477 (*blob)->read_buf = malloc(blocksize);
1478 if ((*blob)->read_buf == NULL) {
1479 err = got_error_from_errno("malloc");
1480 goto done;
1483 err = got_repo_search_packidx(&packidx, &idx, repo, id);
1484 if (err == NULL) {
1485 struct got_pack *pack = NULL;
1487 err = got_packidx_get_packfile_path(&path_packfile,
1488 packidx->path_packidx);
1489 if (err)
1490 goto done;
1492 pack = got_repo_get_cached_pack(repo, path_packfile);
1493 if (pack == NULL) {
1494 err = got_repo_cache_pack(&pack, repo, path_packfile,
1495 packidx);
1496 if (err)
1497 goto done;
1499 err = read_packed_blob_privsep(&outbuf, &size, &hdrlen, outfd,
1500 pack, packidx, idx, id);
1501 } else if (err->code == GOT_ERR_NO_OBJ) {
1502 int infd;
1504 err = got_object_open_loose_fd(&infd, id, repo);
1505 if (err)
1506 goto done;
1507 err = read_blob_privsep(&outbuf, &size, &hdrlen, outfd, infd,
1508 id, repo);
1510 if (err)
1511 goto done;
1513 if (hdrlen > size) {
1514 err = got_error(GOT_ERR_BAD_OBJ_HDR);
1515 goto done;
1518 if (outbuf) {
1519 if (close(outfd) == -1 && err == NULL)
1520 err = got_error_from_errno("close");
1521 outfd = -1;
1522 (*blob)->f = fmemopen(outbuf, size, "rb");
1523 if ((*blob)->f == NULL) {
1524 err = got_error_from_errno("fmemopen");
1525 free(outbuf);
1526 goto done;
1528 (*blob)->data = outbuf;
1529 } else {
1530 if (fstat(outfd, &sb) == -1) {
1531 err = got_error_from_errno("fstat");
1532 goto done;
1535 if (sb.st_size != size) {
1536 err = got_error(GOT_ERR_PRIVSEP_LEN);
1537 goto done;
1540 (*blob)->f = fdopen(outfd, "rb");
1541 if ((*blob)->f == NULL) {
1542 err = got_error_from_errno("fdopen");
1543 close(outfd);
1544 outfd = -1;
1545 goto done;
1549 (*blob)->hdrlen = hdrlen;
1550 (*blob)->blocksize = blocksize;
1551 memcpy(&(*blob)->id.sha1, id->sha1, SHA1_DIGEST_LENGTH);
1553 done:
1554 free(path_packfile);
1555 if (err) {
1556 if (*blob) {
1557 got_object_blob_close(*blob);
1558 *blob = NULL;
1559 } else if (outfd != -1)
1560 close(outfd);
1562 return err;
1565 const struct got_error *
1566 got_object_open_as_blob(struct got_blob_object **blob,
1567 struct got_repository *repo, struct got_object_id *id,
1568 size_t blocksize)
1570 return open_blob(blob, repo, id, blocksize);
1573 const struct got_error *
1574 got_object_blob_open(struct got_blob_object **blob,
1575 struct got_repository *repo, struct got_object *obj, size_t blocksize)
1577 return open_blob(blob, repo, got_object_get_id(obj), blocksize);
1580 const struct got_error *
1581 got_object_blob_close(struct got_blob_object *blob)
1583 const struct got_error *err = NULL;
1584 free(blob->read_buf);
1585 if (blob->f && fclose(blob->f) == EOF)
1586 err = got_error_from_errno("fclose");
1587 free(blob->data);
1588 free(blob);
1589 return err;
1592 void
1593 got_object_blob_rewind(struct got_blob_object *blob)
1595 if (blob->f)
1596 rewind(blob->f);
1599 char *
1600 got_object_blob_id_str(struct got_blob_object *blob, char *buf, size_t size)
1602 return got_sha1_digest_to_str(blob->id.sha1, buf, size);
1605 size_t
1606 got_object_blob_get_hdrlen(struct got_blob_object *blob)
1608 return blob->hdrlen;
1611 const uint8_t *
1612 got_object_blob_get_read_buf(struct got_blob_object *blob)
1614 return blob->read_buf;
1617 const struct got_error *
1618 got_object_blob_read_block(size_t *outlenp, struct got_blob_object *blob)
1620 size_t n;
1622 n = fread(blob->read_buf, 1, blob->blocksize, blob->f);
1623 if (n == 0 && ferror(blob->f))
1624 return got_ferror(blob->f, GOT_ERR_IO);
1625 *outlenp = n;
1626 return NULL;
1629 const struct got_error *
1630 got_object_blob_dump_to_file(off_t *filesize, int *nlines,
1631 off_t **line_offsets, FILE *outfile, struct got_blob_object *blob)
1633 const struct got_error *err = NULL;
1634 size_t n, len, hdrlen;
1635 const uint8_t *buf;
1636 int i;
1637 const int alloc_chunksz = 512;
1638 size_t nalloc = 0;
1639 off_t off = 0, total_len = 0;
1641 if (line_offsets)
1642 *line_offsets = NULL;
1643 if (filesize)
1644 *filesize = 0;
1645 if (nlines)
1646 *nlines = 0;
1648 hdrlen = got_object_blob_get_hdrlen(blob);
1649 do {
1650 err = got_object_blob_read_block(&len, blob);
1651 if (err)
1652 return err;
1653 if (len == 0)
1654 break;
1655 buf = got_object_blob_get_read_buf(blob);
1656 i = hdrlen;
1657 if (nlines) {
1658 if (line_offsets && *line_offsets == NULL) {
1659 /* Have some data but perhaps no '\n'. */
1660 *nlines = 1;
1661 nalloc = alloc_chunksz;
1662 *line_offsets = calloc(nalloc,
1663 sizeof(**line_offsets));
1664 if (*line_offsets == NULL)
1665 return got_error_from_errno("calloc");
1667 /* Skip forward over end of first line. */
1668 while (i < len) {
1669 if (buf[i] == '\n')
1670 break;
1671 i++;
1674 /* Scan '\n' offsets in remaining chunk of data. */
1675 while (i < len) {
1676 if (buf[i] != '\n') {
1677 i++;
1678 continue;
1680 (*nlines)++;
1681 if (line_offsets && nalloc < *nlines) {
1682 size_t n = *nlines + alloc_chunksz;
1683 off_t *o = recallocarray(*line_offsets,
1684 nalloc, n, sizeof(**line_offsets));
1685 if (o == NULL) {
1686 free(*line_offsets);
1687 *line_offsets = NULL;
1688 return got_error_from_errno(
1689 "recallocarray");
1691 *line_offsets = o;
1692 nalloc = n;
1694 if (line_offsets) {
1695 off = total_len + i - hdrlen + 1;
1696 (*line_offsets)[*nlines - 1] = off;
1698 i++;
1701 /* Skip blob object header first time around. */
1702 n = fwrite(buf + hdrlen, 1, len - hdrlen, outfile);
1703 if (n != len - hdrlen)
1704 return got_ferror(outfile, GOT_ERR_IO);
1705 total_len += len - hdrlen;
1706 hdrlen = 0;
1707 } while (len != 0);
1709 if (fflush(outfile) != 0)
1710 return got_error_from_errno("fflush");
1711 rewind(outfile);
1713 if (filesize)
1714 *filesize = total_len;
1716 return NULL;
1719 static const struct got_error *
1720 request_packed_tag(struct got_tag_object **tag, struct got_pack *pack,
1721 int pack_idx, struct got_object_id *id)
1723 const struct got_error *err = NULL;
1725 err = got_privsep_send_tag_req(pack->privsep_child->ibuf, -1, id,
1726 pack_idx);
1727 if (err)
1728 return err;
1730 return got_privsep_recv_tag(tag, pack->privsep_child->ibuf);
1733 static const struct got_error *
1734 read_packed_tag_privsep(struct got_tag_object **tag,
1735 struct got_pack *pack, struct got_packidx *packidx, int idx,
1736 struct got_object_id *id)
1738 const struct got_error *err = NULL;
1740 if (pack->privsep_child)
1741 return request_packed_tag(tag, pack, idx, id);
1743 err = start_pack_privsep_child(pack, packidx);
1744 if (err)
1745 return err;
1747 return request_packed_tag(tag, pack, idx, id);
1750 static const struct got_error *
1751 request_tag(struct got_tag_object **tag, struct got_repository *repo,
1752 int fd, struct got_object_id *id)
1754 const struct got_error *err = NULL;
1755 struct imsgbuf *ibuf;
1757 ibuf = repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TAG].ibuf;
1759 err = got_privsep_send_tag_req(ibuf, fd, id, -1);
1760 if (err)
1761 return err;
1763 return got_privsep_recv_tag(tag, ibuf);
1766 static const struct got_error *
1767 read_tag_privsep(struct got_tag_object **tag, int obj_fd,
1768 struct got_object_id *id, struct got_repository *repo)
1770 const struct got_error *err;
1771 int imsg_fds[2];
1772 pid_t pid;
1773 struct imsgbuf *ibuf;
1775 if (repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TAG].imsg_fd != -1)
1776 return request_tag(tag, repo, obj_fd, id);
1778 ibuf = calloc(1, sizeof(*ibuf));
1779 if (ibuf == NULL)
1780 return got_error_from_errno("calloc");
1782 if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_fds) == -1) {
1783 err = got_error_from_errno("socketpair");
1784 free(ibuf);
1785 return err;
1788 pid = fork();
1789 if (pid == -1) {
1790 err = got_error_from_errno("fork");
1791 free(ibuf);
1792 return err;
1794 else if (pid == 0) {
1795 got_privsep_exec_child(imsg_fds, GOT_PATH_PROG_READ_TAG,
1796 repo->path);
1797 /* not reached */
1800 if (close(imsg_fds[1]) == -1) {
1801 err = got_error_from_errno("close");
1802 free(ibuf);
1803 return err;
1805 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TAG].imsg_fd =
1806 imsg_fds[0];
1807 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TAG].pid = pid;
1808 imsg_init(ibuf, imsg_fds[0]);
1809 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TAG].ibuf = ibuf;
1811 return request_tag(tag, repo, obj_fd, id);
1814 static const struct got_error *
1815 open_tag(struct got_tag_object **tag, struct got_repository *repo,
1816 struct got_object_id *id, int check_cache)
1818 const struct got_error *err = NULL;
1819 struct got_packidx *packidx = NULL;
1820 int idx;
1821 char *path_packfile = NULL;
1822 struct got_object *obj = NULL;
1823 int obj_type = GOT_OBJ_TYPE_ANY;
1825 if (check_cache) {
1826 *tag = got_repo_get_cached_tag(repo, id);
1827 if (*tag != NULL) {
1828 (*tag)->refcnt++;
1829 return NULL;
1831 } else
1832 *tag = NULL;
1834 err = got_repo_search_packidx(&packidx, &idx, repo, id);
1835 if (err == NULL) {
1836 struct got_pack *pack = NULL;
1838 err = got_packidx_get_packfile_path(&path_packfile,
1839 packidx->path_packidx);
1840 if (err)
1841 return err;
1843 pack = got_repo_get_cached_pack(repo, path_packfile);
1844 if (pack == NULL) {
1845 err = got_repo_cache_pack(&pack, repo, path_packfile,
1846 packidx);
1847 if (err)
1848 goto done;
1851 /* Beware of "lightweight" tags: Check object type first. */
1852 err = read_packed_object_privsep(&obj, repo, pack, packidx,
1853 idx, id);
1854 if (err)
1855 goto done;
1856 obj_type = obj->type;
1857 got_object_close(obj);
1858 if (obj_type != GOT_OBJ_TYPE_TAG) {
1859 err = got_error(GOT_ERR_OBJ_TYPE);
1860 goto done;
1862 err = read_packed_tag_privsep(tag, pack, packidx, idx, id);
1863 } else if (err->code == GOT_ERR_NO_OBJ) {
1864 int fd;
1866 err = got_object_open_loose_fd(&fd, id, repo);
1867 if (err)
1868 return err;
1869 err = got_object_read_header_privsep(&obj, id, repo, fd);
1870 if (err)
1871 return err;
1872 obj_type = obj->type;
1873 got_object_close(obj);
1874 if (obj_type != GOT_OBJ_TYPE_TAG)
1875 return got_error(GOT_ERR_OBJ_TYPE);
1877 err = got_object_open_loose_fd(&fd, id, repo);
1878 if (err)
1879 return err;
1880 err = read_tag_privsep(tag, fd, id, repo);
1883 if (err == NULL) {
1884 (*tag)->refcnt++;
1885 err = got_repo_cache_tag(repo, id, *tag);
1887 done:
1888 free(path_packfile);
1889 return err;
1892 const struct got_error *
1893 got_object_open_as_tag(struct got_tag_object **tag,
1894 struct got_repository *repo, struct got_object_id *id)
1896 *tag = got_repo_get_cached_tag(repo, id);
1897 if (*tag != NULL) {
1898 (*tag)->refcnt++;
1899 return NULL;
1902 return open_tag(tag, repo, id, 0);
1905 const struct got_error *
1906 got_object_tag_open(struct got_tag_object **tag,
1907 struct got_repository *repo, struct got_object *obj)
1909 return open_tag(tag, repo, got_object_get_id(obj), 1);
1912 const char *
1913 got_object_tag_get_name(struct got_tag_object *tag)
1915 return tag->tag;
1918 int
1919 got_object_tag_get_object_type(struct got_tag_object *tag)
1921 return tag->obj_type;
1924 struct got_object_id *
1925 got_object_tag_get_object_id(struct got_tag_object *tag)
1927 return &tag->id;
1930 time_t
1931 got_object_tag_get_tagger_time(struct got_tag_object *tag)
1933 return tag->tagger_time;
1936 time_t
1937 got_object_tag_get_tagger_gmtoff(struct got_tag_object *tag)
1939 return tag->tagger_gmtoff;
1942 const char *
1943 got_object_tag_get_tagger(struct got_tag_object *tag)
1945 return tag->tagger;
1948 const char *
1949 got_object_tag_get_message(struct got_tag_object *tag)
1951 return tag->tagmsg;
1954 static struct got_tree_entry *
1955 find_entry_by_name(struct got_tree_object *tree, const char *name, size_t len)
1957 int i;
1959 /* Note that tree entries are sorted in strncmp() order. */
1960 for (i = 0; i < tree->nentries; i++) {
1961 struct got_tree_entry *te = &tree->entries[i];
1962 int cmp = strncmp(te->name, name, len);
1963 if (cmp < 0)
1964 continue;
1965 if (cmp > 0)
1966 break;
1967 if (te->name[len] == '\0')
1968 return te;
1970 return NULL;
1973 struct got_tree_entry *
1974 got_object_tree_find_entry(struct got_tree_object *tree, const char *name)
1976 return find_entry_by_name(tree, name, strlen(name));
1979 const struct got_error *
1980 got_object_tree_find_path(struct got_object_id **id, mode_t *mode,
1981 struct got_repository *repo, struct got_tree_object *tree,
1982 const char *path)
1984 const struct got_error *err = NULL;
1985 struct got_tree_object *subtree = NULL;
1986 struct got_tree_entry *te = NULL;
1987 const char *seg, *s;
1988 size_t seglen;
1990 *id = NULL;
1992 s = path;
1993 while (s[0] == '/')
1994 s++;
1995 seg = s;
1996 seglen = 0;
1997 subtree = tree;
1998 while (*s) {
1999 struct got_tree_object *next_tree;
2001 if (*s != '/') {
2002 s++;
2003 seglen++;
2004 if (*s)
2005 continue;
2008 te = find_entry_by_name(subtree, seg, seglen);
2009 if (te == NULL) {
2010 err = got_error_path(path, GOT_ERR_NO_TREE_ENTRY);
2011 goto done;
2014 if (*s == '\0')
2015 break;
2017 seg = s + 1;
2018 seglen = 0;
2019 s++;
2020 if (*s) {
2021 err = got_object_open_as_tree(&next_tree, repo,
2022 &te->id);
2023 te = NULL;
2024 if (err)
2025 goto done;
2026 if (subtree != tree)
2027 got_object_tree_close(subtree);
2028 subtree = next_tree;
2032 if (te) {
2033 *id = got_object_id_dup(&te->id);
2034 if (*id == NULL)
2035 return got_error_from_errno("got_object_id_dup");
2036 if (mode)
2037 *mode = te->mode;
2038 } else
2039 err = got_error_path(path, GOT_ERR_NO_TREE_ENTRY);
2040 done:
2041 if (subtree && subtree != tree)
2042 got_object_tree_close(subtree);
2043 return err;
2045 const struct got_error *
2046 got_object_id_by_path(struct got_object_id **id, struct got_repository *repo,
2047 struct got_commit_object *commit, const char *path)
2049 const struct got_error *err = NULL;
2050 struct got_tree_object *tree = NULL;
2052 *id = NULL;
2054 /* Handle opening of root of commit's tree. */
2055 if (got_path_is_root_dir(path)) {
2056 *id = got_object_id_dup(commit->tree_id);
2057 if (*id == NULL)
2058 err = got_error_from_errno("got_object_id_dup");
2059 } else {
2060 err = got_object_open_as_tree(&tree, repo, commit->tree_id);
2061 if (err)
2062 goto done;
2063 err = got_object_tree_find_path(id, NULL, repo, tree, path);
2065 done:
2066 if (tree)
2067 got_object_tree_close(tree);
2068 return err;
2072 * Normalize file mode bits to avoid false positive tree entry differences
2073 * in case tree entries have unexpected mode bits set.
2075 static mode_t
2076 normalize_mode_for_comparison(mode_t mode)
2079 * For directories, the only relevant bit is the IFDIR bit.
2080 * This allows us to detect paths changing from a directory
2081 * to a file and vice versa.
2083 if (S_ISDIR(mode))
2084 return mode & S_IFDIR;
2087 * For symlinks, the only relevant bit is the IFLNK bit.
2088 * This allows us to detect paths changing from a symlinks
2089 * to a file or directory and vice versa.
2091 if (S_ISLNK(mode))
2092 return mode & S_IFLNK;
2094 /* For files, the only change we care about is the executable bit. */
2095 return mode & S_IXUSR;
2098 const struct got_error *
2099 got_object_tree_path_changed(int *changed,
2100 struct got_tree_object *tree01, struct got_tree_object *tree02,
2101 const char *path, struct got_repository *repo)
2103 const struct got_error *err = NULL;
2104 struct got_tree_object *tree1 = NULL, *tree2 = NULL;
2105 struct got_tree_entry *te1 = NULL, *te2 = NULL;
2106 const char *seg, *s;
2107 size_t seglen;
2109 *changed = 0;
2111 /* We not do support comparing the root path. */
2112 if (got_path_is_root_dir(path))
2113 return got_error_path(path, GOT_ERR_BAD_PATH);
2115 tree1 = tree01;
2116 tree2 = tree02;
2117 s = path;
2118 while (*s == '/')
2119 s++;
2120 seg = s;
2121 seglen = 0;
2122 while (*s) {
2123 struct got_tree_object *next_tree1, *next_tree2;
2124 mode_t mode1, mode2;
2126 if (*s != '/') {
2127 s++;
2128 seglen++;
2129 if (*s)
2130 continue;
2133 te1 = find_entry_by_name(tree1, seg, seglen);
2134 if (te1 == NULL) {
2135 err = got_error(GOT_ERR_NO_OBJ);
2136 goto done;
2139 if (tree2)
2140 te2 = find_entry_by_name(tree2, seg, seglen);
2142 if (te2) {
2143 mode1 = normalize_mode_for_comparison(te1->mode);
2144 mode2 = normalize_mode_for_comparison(te2->mode);
2145 if (mode1 != mode2) {
2146 *changed = 1;
2147 goto done;
2150 if (got_object_id_cmp(&te1->id, &te2->id) == 0) {
2151 *changed = 0;
2152 goto done;
2156 if (*s == '\0') { /* final path element */
2157 *changed = 1;
2158 goto done;
2161 seg = s + 1;
2162 s++;
2163 seglen = 0;
2164 if (*s) {
2165 err = got_object_open_as_tree(&next_tree1, repo,
2166 &te1->id);
2167 te1 = NULL;
2168 if (err)
2169 goto done;
2170 if (tree1 != tree01)
2171 got_object_tree_close(tree1);
2172 tree1 = next_tree1;
2174 if (te2) {
2175 err = got_object_open_as_tree(&next_tree2, repo,
2176 &te2->id);
2177 te2 = NULL;
2178 if (err)
2179 goto done;
2180 if (tree2 != tree02)
2181 got_object_tree_close(tree2);
2182 tree2 = next_tree2;
2183 } else if (tree2) {
2184 if (tree2 != tree02)
2185 got_object_tree_close(tree2);
2186 tree2 = NULL;
2190 done:
2191 if (tree1 && tree1 != tree01)
2192 got_object_tree_close(tree1);
2193 if (tree2 && tree2 != tree02)
2194 got_object_tree_close(tree2);
2195 return err;
2198 const struct got_error *
2199 got_object_tree_entry_dup(struct got_tree_entry **new_te,
2200 struct got_tree_entry *te)
2202 const struct got_error *err = NULL;
2204 *new_te = calloc(1, sizeof(**new_te));
2205 if (*new_te == NULL)
2206 return got_error_from_errno("calloc");
2208 (*new_te)->mode = te->mode;
2209 memcpy((*new_te)->name, te->name, sizeof((*new_te)->name));
2210 memcpy(&(*new_te)->id, &te->id, sizeof((*new_te)->id));
2211 return err;
2214 int
2215 got_object_tree_entry_is_submodule(struct got_tree_entry *te)
2217 return (te->mode & S_IFMT) == (S_IFDIR | S_IFLNK);
2220 int
2221 got_object_tree_entry_is_symlink(struct got_tree_entry *te)
2223 /* S_IFDIR check avoids confusing symlinks with submodules. */
2224 return ((te->mode & (S_IFDIR | S_IFLNK)) == S_IFLNK);
2227 static const struct got_error *
2228 resolve_symlink(char **link_target, const char *path,
2229 struct got_commit_object *commit, struct got_repository *repo)
2231 const struct got_error *err = NULL;
2232 char buf[PATH_MAX];
2233 char *name, *parent_path = NULL;
2234 struct got_object_id *tree_obj_id = NULL;
2235 struct got_tree_object *tree = NULL;
2236 struct got_tree_entry *te = NULL;
2238 *link_target = NULL;
2240 if (strlcpy(buf, path, sizeof(buf)) >= sizeof(buf))
2241 return got_error(GOT_ERR_NO_SPACE);
2243 name = basename(buf);
2244 if (name == NULL)
2245 return got_error_from_errno2("basename", path);
2247 err = got_path_dirname(&parent_path, path);
2248 if (err)
2249 return err;
2251 err = got_object_id_by_path(&tree_obj_id, repo, commit,
2252 parent_path);
2253 if (err) {
2254 if (err->code == GOT_ERR_NO_TREE_ENTRY) {
2255 /* Display the complete path in error message. */
2256 err = got_error_path(path, err->code);
2258 goto done;
2261 err = got_object_open_as_tree(&tree, repo, tree_obj_id);
2262 if (err)
2263 goto done;
2265 te = got_object_tree_find_entry(tree, name);
2266 if (te == NULL) {
2267 err = got_error_path(path, GOT_ERR_NO_TREE_ENTRY);
2268 goto done;
2271 if (got_object_tree_entry_is_symlink(te)) {
2272 err = got_tree_entry_get_symlink_target(link_target, te, repo);
2273 if (err)
2274 goto done;
2275 if (!got_path_is_absolute(*link_target)) {
2276 char *abspath;
2277 if (asprintf(&abspath, "%s/%s", parent_path,
2278 *link_target) == -1) {
2279 err = got_error_from_errno("asprintf");
2280 goto done;
2282 free(*link_target);
2283 *link_target = malloc(PATH_MAX);
2284 if (*link_target == NULL) {
2285 err = got_error_from_errno("malloc");
2286 goto done;
2288 err = got_canonpath(abspath, *link_target, PATH_MAX);
2289 free(abspath);
2290 if (err)
2291 goto done;
2294 done:
2295 free(tree_obj_id);
2296 if (tree)
2297 got_object_tree_close(tree);
2298 if (err) {
2299 free(*link_target);
2300 *link_target = NULL;
2302 return err;
2305 const struct got_error *
2306 got_object_resolve_symlinks(char **link_target, const char *path,
2307 struct got_commit_object *commit, struct got_repository *repo)
2309 const struct got_error *err = NULL;
2310 char *next_target = NULL;
2311 int max_recursion = 40; /* matches Git */
2313 *link_target = NULL;
2315 do {
2316 err = resolve_symlink(&next_target,
2317 *link_target ? *link_target : path, commit, repo);
2318 if (err)
2319 break;
2320 if (next_target) {
2321 free(*link_target);
2322 if (--max_recursion == 0) {
2323 err = got_error_path(path, GOT_ERR_RECURSION);
2324 *link_target = NULL;
2325 break;
2327 *link_target = next_target;
2329 } while (next_target);
2331 return err;
2334 const struct got_error *
2335 got_traverse_packed_commits(struct got_object_id_queue *traversed_commits,
2336 struct got_object_id *commit_id, const char *path,
2337 struct got_repository *repo)
2339 const struct got_error *err = NULL;
2340 struct got_pack *pack = NULL;
2341 struct got_packidx *packidx = NULL;
2342 char *path_packfile = NULL;
2343 struct got_commit_object *changed_commit = NULL;
2344 struct got_object_id *changed_commit_id = NULL;
2345 int idx;
2347 err = got_repo_search_packidx(&packidx, &idx, repo, commit_id);
2348 if (err) {
2349 if (err->code != GOT_ERR_NO_OBJ)
2350 return err;
2351 return NULL;
2354 err = got_packidx_get_packfile_path(&path_packfile,
2355 packidx->path_packidx);
2356 if (err)
2357 return err;
2359 pack = got_repo_get_cached_pack(repo, path_packfile);
2360 if (pack == NULL) {
2361 err = got_repo_cache_pack(&pack, repo, path_packfile, packidx);
2362 if (err)
2363 goto done;
2366 if (pack->privsep_child == NULL) {
2367 err = start_pack_privsep_child(pack, packidx);
2368 if (err)
2369 goto done;
2372 err = got_privsep_send_commit_traversal_request(
2373 pack->privsep_child->ibuf, commit_id, idx, path);
2374 if (err)
2375 goto done;
2377 err = got_privsep_recv_traversed_commits(&changed_commit,
2378 &changed_commit_id, traversed_commits, pack->privsep_child->ibuf);
2379 if (err)
2380 goto done;
2382 if (changed_commit) {
2384 * Cache the commit in which the path was changed.
2385 * This commit might be opened again soon.
2387 changed_commit->refcnt++;
2388 err = got_repo_cache_commit(repo, changed_commit_id,
2389 changed_commit);
2390 got_object_commit_close(changed_commit);
2392 done:
2393 free(path_packfile);
2394 free(changed_commit_id);
2395 return err;