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 #ifndef nitems
62 #define nitems(_a) (sizeof((_a)) / sizeof((_a)[0]))
63 #endif
65 struct got_object_id *
66 got_object_get_id(struct got_object *obj)
67 {
68 return &obj->id;
69 }
71 const struct got_error *
72 got_object_get_id_str(char **outbuf, struct got_object *obj)
73 {
74 return got_object_id_str(outbuf, &obj->id);
75 }
77 const struct got_error *
78 got_object_get_type(int *type, struct got_repository *repo,
79 struct got_object_id *id)
80 {
81 const struct got_error *err = NULL;
82 struct got_object *obj;
84 err = got_object_open(&obj, repo, id);
85 if (err)
86 return err;
88 switch (obj->type) {
89 case GOT_OBJ_TYPE_COMMIT:
90 case GOT_OBJ_TYPE_TREE:
91 case GOT_OBJ_TYPE_BLOB:
92 case GOT_OBJ_TYPE_TAG:
93 *type = obj->type;
94 break;
95 default:
96 err = got_error(GOT_ERR_OBJ_TYPE);
97 break;
98 }
100 got_object_close(obj);
101 return err;
104 const struct got_error *
105 got_object_get_path(char **path, struct got_object_id *id,
106 struct got_repository *repo)
108 const struct got_error *err = NULL;
109 char *hex = NULL;
110 char *path_objects;
112 *path = NULL;
114 path_objects = got_repo_get_path_objects(repo);
115 if (path_objects == NULL)
116 return got_error_from_errno("got_repo_get_path_objects");
118 err = got_object_id_str(&hex, id);
119 if (err)
120 goto done;
122 if (asprintf(path, "%s/%.2x/%s", path_objects,
123 id->sha1[0], hex + 2) == -1)
124 err = got_error_from_errno("asprintf");
126 done:
127 free(hex);
128 free(path_objects);
129 return err;
132 const struct got_error *
133 got_object_open_loose_fd(int *fd, struct got_object_id *id,
134 struct got_repository *repo)
136 const struct got_error *err = NULL;
137 char *path;
139 err = got_object_get_path(&path, id, repo);
140 if (err)
141 return err;
142 *fd = open(path, O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
143 if (*fd == -1) {
144 err = got_error_from_errno2("open", path);
145 goto done;
147 done:
148 free(path);
149 return err;
152 static const struct got_error *
153 request_packed_object(struct got_object **obj, struct got_pack *pack, int idx,
154 struct got_object_id *id)
156 const struct got_error *err = NULL;
157 struct imsgbuf *ibuf = pack->privsep_child->ibuf;
159 err = got_privsep_send_packed_obj_req(ibuf, idx, id);
160 if (err)
161 return err;
163 err = got_privsep_recv_obj(obj, ibuf);
164 if (err)
165 return err;
167 memcpy(&(*obj)->id, id, sizeof((*obj)->id));
169 return NULL;
172 /* Create temporary files used during delta application. */
173 static const struct got_error *
174 pack_child_send_tempfiles(struct imsgbuf *ibuf, struct got_pack *pack)
176 const struct got_error *err;
177 int basefd = -1, accumfd = -1;
179 /*
180 * For performance reasons, the child will keep reusing the
181 * same temporary files during every object request.
182 * Opening and closing new files for every object request is
183 * too expensive during operations such as 'gotadmin pack'.
184 */
185 if (pack->child_has_tempfiles)
186 return NULL;
188 basefd = dup(pack->basefd);
189 if (basefd == -1)
190 return got_error_from_errno("dup");
192 accumfd = dup(pack->accumfd);
193 if (accumfd == -1) {
194 err = got_error_from_errno("dup");
195 goto done;
198 err = got_privsep_send_tmpfd(ibuf, basefd);
199 if (err)
200 goto done;
202 err = got_privsep_send_tmpfd(ibuf, accumfd);
203 done:
204 if (err) {
205 if (basefd != -1)
206 close(basefd);
207 if (accumfd != -1)
208 close(accumfd);
209 } else
210 pack->child_has_tempfiles = 1;
211 return NULL;
214 static const struct got_error *
215 request_packed_object_raw(uint8_t **outbuf, off_t *size, size_t *hdrlen,
216 int outfd, struct got_pack *pack, int idx, struct got_object_id *id)
218 const struct got_error *err = NULL;
219 struct imsgbuf *ibuf = pack->privsep_child->ibuf;
220 int outfd_child;
222 err = pack_child_send_tempfiles(ibuf, pack);
223 if (err)
224 return err;
226 outfd_child = dup(outfd);
227 if (outfd_child == -1)
228 return got_error_from_errno("dup");
230 err = got_privsep_send_packed_raw_obj_req(ibuf, idx, id);
231 if (err) {
232 close(outfd_child);
233 return err;
236 err = got_privsep_send_raw_obj_outfd(ibuf, outfd_child);
237 if (err)
238 return err;
240 err = got_privsep_recv_raw_obj(outbuf, size, hdrlen, ibuf);
241 if (err)
242 return err;
244 return NULL;
247 static void
248 set_max_datasize(void)
250 struct rlimit rl;
252 if (getrlimit(RLIMIT_DATA, &rl) != 0)
253 return;
255 rl.rlim_cur = rl.rlim_max;
256 setrlimit(RLIMIT_DATA, &rl);
259 static const struct got_error *
260 start_pack_privsep_child(struct got_pack *pack, struct got_packidx *packidx)
262 const struct got_error *err = NULL;
263 int imsg_fds[2];
264 pid_t pid;
265 struct imsgbuf *ibuf;
267 ibuf = calloc(1, sizeof(*ibuf));
268 if (ibuf == NULL)
269 return got_error_from_errno("calloc");
271 pack->privsep_child = calloc(1, sizeof(*pack->privsep_child));
272 if (pack->privsep_child == NULL) {
273 err = got_error_from_errno("calloc");
274 free(ibuf);
275 return err;
277 pack->child_has_tempfiles = 0;
278 pack->child_has_delta_outfd = 0;
280 if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_fds) == -1) {
281 err = got_error_from_errno("socketpair");
282 goto done;
285 pid = fork();
286 if (pid == -1) {
287 err = got_error_from_errno("fork");
288 goto done;
289 } else if (pid == 0) {
290 set_max_datasize();
291 got_privsep_exec_child(imsg_fds, GOT_PATH_PROG_READ_PACK,
292 pack->path_packfile);
293 /* not reached */
296 if (close(imsg_fds[1]) == -1)
297 return got_error_from_errno("close");
298 pack->privsep_child->imsg_fd = imsg_fds[0];
299 pack->privsep_child->pid = pid;
300 imsg_init(ibuf, imsg_fds[0]);
301 pack->privsep_child->ibuf = ibuf;
303 err = got_privsep_init_pack_child(ibuf, pack, packidx);
304 if (err) {
305 const struct got_error *child_err;
306 err = got_privsep_send_stop(pack->privsep_child->imsg_fd);
307 child_err = got_privsep_wait_for_child(
308 pack->privsep_child->pid);
309 if (child_err && err == NULL)
310 err = child_err;
312 done:
313 if (err) {
314 free(ibuf);
315 free(pack->privsep_child);
316 pack->privsep_child = NULL;
318 return err;
321 static const struct got_error *
322 read_packed_object_privsep(struct got_object **obj,
323 struct got_repository *repo, struct got_pack *pack,
324 struct got_packidx *packidx, int idx, struct got_object_id *id)
326 const struct got_error *err = NULL;
328 if (pack->privsep_child == NULL) {
329 err = start_pack_privsep_child(pack, packidx);
330 if (err)
331 return err;
334 return request_packed_object(obj, pack, idx, id);
337 static const struct got_error *
338 read_packed_object_raw_privsep(uint8_t **outbuf, off_t *size, size_t *hdrlen,
339 int outfd, struct got_pack *pack, struct got_packidx *packidx, int idx,
340 struct got_object_id *id)
342 const struct got_error *err = NULL;
344 if (pack->privsep_child == NULL) {
345 err = start_pack_privsep_child(pack, packidx);
346 if (err)
347 return err;
350 return request_packed_object_raw(outbuf, size, hdrlen, outfd, pack,
351 idx, id);
354 const struct got_error *
355 got_object_open_packed(struct got_object **obj, struct got_object_id *id,
356 struct got_repository *repo)
358 const struct got_error *err = NULL;
359 struct got_pack *pack = NULL;
360 struct got_packidx *packidx = NULL;
361 int idx;
362 char *path_packfile;
364 err = got_repo_search_packidx(&packidx, &idx, repo, id);
365 if (err)
366 return err;
368 err = got_packidx_get_packfile_path(&path_packfile,
369 packidx->path_packidx);
370 if (err)
371 return err;
373 pack = got_repo_get_cached_pack(repo, path_packfile);
374 if (pack == NULL) {
375 err = got_repo_cache_pack(&pack, repo, path_packfile, packidx);
376 if (err)
377 goto done;
380 err = read_packed_object_privsep(obj, repo, pack, packidx, idx, id);
381 if (err)
382 goto done;
383 done:
384 free(path_packfile);
385 return err;
388 const struct got_error *
389 got_object_open_from_packfile(struct got_object **obj, struct got_object_id *id,
390 struct got_pack *pack, struct got_packidx *packidx, int obj_idx,
391 struct got_repository *repo)
393 return read_packed_object_privsep(obj, repo, pack, packidx,
394 obj_idx, id);
397 const struct got_error *
398 got_object_read_raw_delta(uint64_t *base_size, uint64_t *result_size,
399 off_t *delta_size, off_t *delta_compressed_size, off_t *delta_offset,
400 off_t *delta_out_offset, struct got_object_id **base_id, int delta_cache_fd,
401 struct got_packidx *packidx, int obj_idx, struct got_object_id *id,
402 struct got_repository *repo)
404 const struct got_error *err = NULL;
405 struct got_pack *pack = NULL;
406 char *path_packfile;
408 *base_size = 0;
409 *result_size = 0;
410 *delta_size = 0;
411 *delta_compressed_size = 0;
412 *delta_offset = 0;
413 *delta_out_offset = 0;
415 err = got_packidx_get_packfile_path(&path_packfile,
416 packidx->path_packidx);
417 if (err)
418 return err;
420 pack = got_repo_get_cached_pack(repo, path_packfile);
421 if (pack == NULL) {
422 err = got_repo_cache_pack(&pack, repo, path_packfile, packidx);
423 if (err)
424 return err;
427 if (pack->privsep_child == NULL) {
428 err = start_pack_privsep_child(pack, packidx);
429 if (err)
430 return err;
433 if (!pack->child_has_delta_outfd) {
434 int outfd_child;
435 outfd_child = dup(delta_cache_fd);
436 if (outfd_child == -1)
437 return got_error_from_errno("dup");
438 err = got_privsep_send_raw_delta_outfd(
439 pack->privsep_child->ibuf, outfd_child);
440 if (err)
441 return err;
442 pack->child_has_delta_outfd = 1;
445 err = got_privsep_send_raw_delta_req(pack->privsep_child->ibuf,
446 obj_idx, id);
447 if (err)
448 return err;
450 return got_privsep_recv_raw_delta(base_size, result_size, delta_size,
451 delta_compressed_size, delta_offset, delta_out_offset, base_id,
452 pack->privsep_child->ibuf);
455 /*
456 * XXX This function does not really belong in object.c. It is only here
457 * because it needs start_pack_privsep_child(); relevant code should
458 * probably be moved to pack.c/pack_create.c.
459 */
460 const struct got_error *
461 got_object_prepare_delta_reuse(struct got_pack **pack,
462 struct got_packidx *packidx, int delta_outfd, struct got_repository *repo)
464 const struct got_error *err = NULL;
465 char *path_packfile = NULL;
467 err = got_packidx_get_packfile_path(&path_packfile,
468 packidx->path_packidx);
469 if (err)
470 return err;
472 *pack = got_repo_get_cached_pack(repo, path_packfile);
473 if (*pack == NULL) {
474 err = got_repo_cache_pack(pack, repo, path_packfile, packidx);
475 if (err)
476 goto done;
478 if ((*pack)->privsep_child == NULL) {
479 err = start_pack_privsep_child(*pack, packidx);
480 if (err)
481 goto done;
484 if (!(*pack)->child_has_delta_outfd) {
485 int outfd_child;
486 outfd_child = dup(delta_outfd);
487 if (outfd_child == -1) {
488 err = got_error_from_errno("dup");
489 goto done;
491 err = got_privsep_send_raw_delta_outfd(
492 (*pack)->privsep_child->ibuf, outfd_child);
493 if (err)
494 goto done;
495 (*pack)->child_has_delta_outfd = 1;
498 err = got_privsep_send_delta_reuse_req((*pack)->privsep_child->ibuf);
499 done:
500 free(path_packfile);
501 return err;
504 static const struct got_error *
505 request_object(struct got_object **obj, struct got_object_id *id,
506 struct got_repository *repo, int fd)
508 const struct got_error *err = NULL;
509 struct imsgbuf *ibuf;
511 ibuf = repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_OBJECT].ibuf;
513 err = got_privsep_send_obj_req(ibuf, fd, id);
514 if (err)
515 return err;
517 return got_privsep_recv_obj(obj, ibuf);
520 static const struct got_error *
521 request_raw_object(uint8_t **outbuf, off_t *size, size_t *hdrlen, int outfd,
522 struct got_object_id *id, struct got_repository *repo, int infd)
524 const struct got_error *err = NULL;
525 struct imsgbuf *ibuf;
526 int outfd_child;
528 ibuf = repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_OBJECT].ibuf;
530 outfd_child = dup(outfd);
531 if (outfd_child == -1)
532 return got_error_from_errno("dup");
534 err = got_privsep_send_raw_obj_req(ibuf, infd, id);
535 if (err)
536 return err;
538 err = got_privsep_send_raw_obj_outfd(ibuf, outfd_child);
539 if (err)
540 return err;
542 return got_privsep_recv_raw_obj(outbuf, size, hdrlen, ibuf);
545 static const struct got_error *
546 start_read_object_child(struct got_repository *repo)
548 const struct got_error *err = NULL;
549 int imsg_fds[2];
550 pid_t pid;
551 struct imsgbuf *ibuf;
553 ibuf = calloc(1, sizeof(*ibuf));
554 if (ibuf == NULL)
555 return got_error_from_errno("calloc");
557 if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_fds) == -1) {
558 err = got_error_from_errno("socketpair");
559 free(ibuf);
560 return err;
563 pid = fork();
564 if (pid == -1) {
565 err = got_error_from_errno("fork");
566 free(ibuf);
567 return err;
569 else if (pid == 0) {
570 got_privsep_exec_child(imsg_fds, GOT_PATH_PROG_READ_OBJECT,
571 repo->path);
572 /* not reached */
575 if (close(imsg_fds[1]) == -1) {
576 err = got_error_from_errno("close");
577 free(ibuf);
578 return err;
581 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_OBJECT].imsg_fd =
582 imsg_fds[0];
583 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_OBJECT].pid = pid;
584 imsg_init(ibuf, imsg_fds[0]);
585 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_OBJECT].ibuf = ibuf;
587 return NULL;
590 const struct got_error *
591 got_object_read_header_privsep(struct got_object **obj,
592 struct got_object_id *id, struct got_repository *repo, int obj_fd)
594 const struct got_error *err;
596 if (repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_OBJECT].imsg_fd != -1)
597 return request_object(obj, id, repo, obj_fd);
599 err = start_read_object_child(repo);
600 if (err) {
601 close(obj_fd);
602 return err;
605 return request_object(obj, id, repo, obj_fd);
608 static const struct got_error *
609 read_object_raw_privsep(uint8_t **outbuf, off_t *size, size_t *hdrlen,
610 int outfd, struct got_object_id *id, struct got_repository *repo,
611 int obj_fd)
613 const struct got_error *err;
615 if (repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_OBJECT].imsg_fd != -1)
616 return request_raw_object(outbuf, size, hdrlen, outfd, id,
617 repo, obj_fd);
619 err = start_read_object_child(repo);
620 if (err)
621 return err;
623 return request_raw_object(outbuf, size, hdrlen, outfd, id, repo,
624 obj_fd);
627 const struct got_error *
628 got_object_open(struct got_object **obj, struct got_repository *repo,
629 struct got_object_id *id)
631 const struct got_error *err = NULL;
632 int fd;
634 *obj = got_repo_get_cached_object(repo, id);
635 if (*obj != NULL) {
636 (*obj)->refcnt++;
637 return NULL;
640 err = got_object_open_packed(obj, id, repo);
641 if (err && err->code != GOT_ERR_NO_OBJ)
642 return err;
643 if (*obj) {
644 (*obj)->refcnt++;
645 return got_repo_cache_object(repo, id, *obj);
648 err = got_object_open_loose_fd(&fd, id, repo);
649 if (err) {
650 if (err->code == GOT_ERR_ERRNO && errno == ENOENT)
651 err = got_error_no_obj(id);
652 return err;
655 err = got_object_read_header_privsep(obj, id, repo, fd);
656 if (err)
657 return err;
659 memcpy((*obj)->id.sha1, id->sha1, SHA1_DIGEST_LENGTH);
661 (*obj)->refcnt++;
662 return got_repo_cache_object(repo, id, *obj);
665 /* *outfd must be initialized to -1 by caller */
666 const struct got_error *
667 got_object_raw_open(struct got_raw_object **obj, int *outfd,
668 struct got_repository *repo, struct got_object_id *id)
670 const struct got_error *err = NULL;
671 struct got_packidx *packidx = NULL;
672 int idx;
673 uint8_t *outbuf = NULL;
674 off_t size = 0;
675 size_t hdrlen = 0;
676 char *path_packfile = NULL;
678 *obj = got_repo_get_cached_raw_object(repo, id);
679 if (*obj != NULL) {
680 (*obj)->refcnt++;
681 return NULL;
684 if (*outfd == -1) {
685 *outfd = got_opentempfd();
686 if (*outfd == -1)
687 return got_error_from_errno("got_opentempfd");
690 err = got_repo_search_packidx(&packidx, &idx, repo, id);
691 if (err == NULL) {
692 struct got_pack *pack = NULL;
694 err = got_packidx_get_packfile_path(&path_packfile,
695 packidx->path_packidx);
696 if (err)
697 goto done;
699 pack = got_repo_get_cached_pack(repo, path_packfile);
700 if (pack == NULL) {
701 err = got_repo_cache_pack(&pack, repo, path_packfile,
702 packidx);
703 if (err)
704 goto done;
706 err = read_packed_object_raw_privsep(&outbuf, &size, &hdrlen,
707 *outfd, pack, packidx, idx, id);
708 if (err)
709 goto done;
710 } else if (err->code == GOT_ERR_NO_OBJ) {
711 int fd;
713 err = got_object_open_loose_fd(&fd, id, repo);
714 if (err)
715 goto done;
716 err = read_object_raw_privsep(&outbuf, &size, &hdrlen, *outfd,
717 id, repo, fd);
718 if (err)
719 goto done;
722 *obj = calloc(1, sizeof(**obj));
723 if (*obj == NULL) {
724 err = got_error_from_errno("calloc");
725 goto done;
727 (*obj)->fd = -1;
729 if (outbuf) {
730 (*obj)->data = outbuf;
731 } else {
732 struct stat sb;
733 if (fstat(*outfd, &sb) == -1) {
734 err = got_error_from_errno("fstat");
735 goto done;
738 if (sb.st_size != hdrlen + size) {
739 err = got_error(GOT_ERR_PRIVSEP_LEN);
740 goto done;
742 #ifndef GOT_PACK_NO_MMAP
743 if (hdrlen + size > 0) {
744 (*obj)->data = mmap(NULL, hdrlen + size, PROT_READ,
745 MAP_PRIVATE, *outfd, 0);
746 if ((*obj)->data == MAP_FAILED) {
747 if (errno != ENOMEM) {
748 err = got_error_from_errno("mmap");
749 goto done;
751 (*obj)->data = NULL;
752 } else {
753 (*obj)->fd = *outfd;
754 *outfd = -1;
757 #endif
758 if (*outfd != -1) {
759 (*obj)->f = fdopen(*outfd, "r");
760 if ((*obj)->f == NULL) {
761 err = got_error_from_errno("fdopen");
762 goto done;
764 *outfd = -1;
767 (*obj)->hdrlen = hdrlen;
768 (*obj)->size = size;
769 err = got_repo_cache_raw_object(repo, id, *obj);
770 done:
771 free(path_packfile);
772 if (err) {
773 if (*obj) {
774 got_object_raw_close(*obj);
775 *obj = NULL;
777 free(outbuf);
778 } else
779 (*obj)->refcnt++;
780 return err;
783 const struct got_error *
784 got_object_open_by_id_str(struct got_object **obj, struct got_repository *repo,
785 const char *id_str)
787 struct got_object_id id;
789 if (!got_parse_sha1_digest(id.sha1, id_str))
790 return got_error_path(id_str, GOT_ERR_BAD_OBJ_ID_STR);
792 return got_object_open(obj, repo, &id);
795 const struct got_error *
796 got_object_resolve_id_str(struct got_object_id **id,
797 struct got_repository *repo, const char *id_str)
799 const struct got_error *err = NULL;
800 struct got_object *obj;
802 err = got_object_open_by_id_str(&obj, repo, id_str);
803 if (err)
804 return err;
806 *id = got_object_id_dup(got_object_get_id(obj));
807 got_object_close(obj);
808 if (*id == NULL)
809 return got_error_from_errno("got_object_id_dup");
811 return NULL;
814 static const struct got_error *
815 request_packed_commit(struct got_commit_object **commit, struct got_pack *pack,
816 int pack_idx, struct got_object_id *id)
818 const struct got_error *err = NULL;
820 err = got_privsep_send_commit_req(pack->privsep_child->ibuf, -1, id,
821 pack_idx);
822 if (err)
823 return err;
825 err = got_privsep_recv_commit(commit, pack->privsep_child->ibuf);
826 if (err)
827 return err;
829 (*commit)->flags |= GOT_COMMIT_FLAG_PACKED;
830 return NULL;
833 static const struct got_error *
834 read_packed_commit_privsep(struct got_commit_object **commit,
835 struct got_pack *pack, struct got_packidx *packidx, int idx,
836 struct got_object_id *id)
838 const struct got_error *err = NULL;
840 if (pack->privsep_child)
841 return request_packed_commit(commit, pack, idx, id);
843 err = start_pack_privsep_child(pack, packidx);
844 if (err)
845 return err;
847 return request_packed_commit(commit, pack, idx, id);
850 static const struct got_error *
851 request_commit(struct got_commit_object **commit, struct got_repository *repo,
852 int fd, struct got_object_id *id)
854 const struct got_error *err = NULL;
855 struct imsgbuf *ibuf;
857 ibuf = repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_COMMIT].ibuf;
859 err = got_privsep_send_commit_req(ibuf, fd, id, -1);
860 if (err)
861 return err;
863 return got_privsep_recv_commit(commit, ibuf);
866 static const struct got_error *
867 read_commit_privsep(struct got_commit_object **commit, int obj_fd,
868 struct got_object_id *id, struct got_repository *repo)
870 const struct got_error *err;
871 int imsg_fds[2];
872 pid_t pid;
873 struct imsgbuf *ibuf;
875 if (repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_COMMIT].imsg_fd != -1)
876 return request_commit(commit, repo, obj_fd, id);
878 ibuf = calloc(1, sizeof(*ibuf));
879 if (ibuf == NULL)
880 return got_error_from_errno("calloc");
882 if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_fds) == -1) {
883 err = got_error_from_errno("socketpair");
884 free(ibuf);
885 return err;
888 pid = fork();
889 if (pid == -1) {
890 err = got_error_from_errno("fork");
891 free(ibuf);
892 return err;
894 else if (pid == 0) {
895 got_privsep_exec_child(imsg_fds, GOT_PATH_PROG_READ_COMMIT,
896 repo->path);
897 /* not reached */
900 if (close(imsg_fds[1]) == -1) {
901 err = got_error_from_errno("close");
902 free(ibuf);
903 return err;
905 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_COMMIT].imsg_fd =
906 imsg_fds[0];
907 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_COMMIT].pid = pid;
908 imsg_init(ibuf, imsg_fds[0]);
909 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_COMMIT].ibuf = ibuf;
911 return request_commit(commit, repo, obj_fd, id);
915 static const struct got_error *
916 open_commit(struct got_commit_object **commit,
917 struct got_repository *repo, struct got_object_id *id, int check_cache)
919 const struct got_error *err = NULL;
920 struct got_packidx *packidx = NULL;
921 int idx;
922 char *path_packfile = NULL;
924 if (check_cache) {
925 *commit = got_repo_get_cached_commit(repo, id);
926 if (*commit != NULL) {
927 (*commit)->refcnt++;
928 return NULL;
930 } else
931 *commit = NULL;
933 err = got_repo_search_packidx(&packidx, &idx, repo, id);
934 if (err == NULL) {
935 struct got_pack *pack = NULL;
937 err = got_packidx_get_packfile_path(&path_packfile,
938 packidx->path_packidx);
939 if (err)
940 return err;
942 pack = got_repo_get_cached_pack(repo, path_packfile);
943 if (pack == NULL) {
944 err = got_repo_cache_pack(&pack, repo, path_packfile,
945 packidx);
946 if (err)
947 goto done;
949 err = read_packed_commit_privsep(commit, pack,
950 packidx, idx, id);
951 } else if (err->code == GOT_ERR_NO_OBJ) {
952 int fd;
954 err = got_object_open_loose_fd(&fd, id, repo);
955 if (err)
956 return err;
957 err = read_commit_privsep(commit, fd, id, repo);
960 if (err == NULL) {
961 (*commit)->refcnt++;
962 err = got_repo_cache_commit(repo, id, *commit);
964 done:
965 free(path_packfile);
966 return err;
969 const struct got_error *
970 got_object_open_as_commit(struct got_commit_object **commit,
971 struct got_repository *repo, struct got_object_id *id)
973 *commit = got_repo_get_cached_commit(repo, id);
974 if (*commit != NULL) {
975 (*commit)->refcnt++;
976 return NULL;
979 return open_commit(commit, repo, id, 0);
982 const struct got_error *
983 got_object_commit_open(struct got_commit_object **commit,
984 struct got_repository *repo, struct got_object *obj)
986 return open_commit(commit, repo, got_object_get_id(obj), 1);
989 const struct got_error *
990 got_object_qid_alloc(struct got_object_qid **qid, struct got_object_id *id)
992 *qid = calloc(1, sizeof(**qid));
993 if (*qid == NULL)
994 return got_error_from_errno("calloc");
996 memcpy(&(*qid)->id, id, sizeof((*qid)->id));
997 return NULL;
1000 const struct got_error *
1001 got_object_id_queue_copy(const struct got_object_id_queue *src,
1002 struct got_object_id_queue *dest)
1004 const struct got_error *err;
1005 struct got_object_qid *qid;
1007 STAILQ_FOREACH(qid, src, entry) {
1008 struct got_object_qid *new;
1010 * Deep-copy the object ID only. Let the caller deal
1011 * with setting up the new->data pointer if needed.
1013 err = got_object_qid_alloc(&new, &qid->id);
1014 if (err) {
1015 got_object_id_queue_free(dest);
1016 return err;
1018 STAILQ_INSERT_TAIL(dest, new, entry);
1021 return NULL;
1024 static const struct got_error *
1025 request_packed_tree(struct got_tree_object **tree, struct got_pack *pack,
1026 int pack_idx, struct got_object_id *id)
1028 const struct got_error *err = NULL;
1030 err = got_privsep_send_tree_req(pack->privsep_child->ibuf, -1, id,
1031 pack_idx);
1032 if (err)
1033 return err;
1035 return got_privsep_recv_tree(tree, pack->privsep_child->ibuf);
1038 static const struct got_error *
1039 read_packed_tree_privsep(struct got_tree_object **tree,
1040 struct got_pack *pack, struct got_packidx *packidx, int idx,
1041 struct got_object_id *id)
1043 const struct got_error *err = NULL;
1045 if (pack->privsep_child)
1046 return request_packed_tree(tree, pack, idx, id);
1048 err = start_pack_privsep_child(pack, packidx);
1049 if (err)
1050 return err;
1052 return request_packed_tree(tree, pack, idx, id);
1055 static const struct got_error *
1056 request_tree(struct got_tree_object **tree, struct got_repository *repo,
1057 int fd, struct got_object_id *id)
1059 const struct got_error *err = NULL;
1060 struct imsgbuf *ibuf;
1062 ibuf = repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TREE].ibuf;
1064 err = got_privsep_send_tree_req(ibuf, fd, id, -1);
1065 if (err)
1066 return err;
1068 return got_privsep_recv_tree(tree, ibuf);
1071 const struct got_error *
1072 read_tree_privsep(struct got_tree_object **tree, int obj_fd,
1073 struct got_object_id *id, struct got_repository *repo)
1075 const struct got_error *err;
1076 int imsg_fds[2];
1077 pid_t pid;
1078 struct imsgbuf *ibuf;
1080 if (repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TREE].imsg_fd != -1)
1081 return request_tree(tree, repo, obj_fd, id);
1083 ibuf = calloc(1, sizeof(*ibuf));
1084 if (ibuf == NULL)
1085 return got_error_from_errno("calloc");
1087 if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_fds) == -1) {
1088 err = got_error_from_errno("socketpair");
1089 free(ibuf);
1090 return err;
1093 pid = fork();
1094 if (pid == -1) {
1095 err = got_error_from_errno("fork");
1096 free(ibuf);
1097 return err;
1099 else if (pid == 0) {
1100 got_privsep_exec_child(imsg_fds, GOT_PATH_PROG_READ_TREE,
1101 repo->path);
1102 /* not reached */
1105 if (close(imsg_fds[1]) == -1) {
1106 err = got_error_from_errno("close");
1107 free(ibuf);
1108 return err;
1110 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TREE].imsg_fd =
1111 imsg_fds[0];
1112 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TREE].pid = pid;
1113 imsg_init(ibuf, imsg_fds[0]);
1114 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TREE].ibuf = ibuf;
1117 return request_tree(tree, repo, obj_fd, id);
1120 static const struct got_error *
1121 open_tree(struct got_tree_object **tree, struct got_repository *repo,
1122 struct got_object_id *id, int check_cache)
1124 const struct got_error *err = NULL;
1125 struct got_packidx *packidx = NULL;
1126 int idx;
1127 char *path_packfile = NULL;
1129 if (check_cache) {
1130 *tree = got_repo_get_cached_tree(repo, id);
1131 if (*tree != NULL) {
1132 (*tree)->refcnt++;
1133 return NULL;
1135 } else
1136 *tree = NULL;
1138 err = got_repo_search_packidx(&packidx, &idx, repo, id);
1139 if (err == NULL) {
1140 struct got_pack *pack = NULL;
1142 err = got_packidx_get_packfile_path(&path_packfile,
1143 packidx->path_packidx);
1144 if (err)
1145 return err;
1147 pack = got_repo_get_cached_pack(repo, path_packfile);
1148 if (pack == NULL) {
1149 err = got_repo_cache_pack(&pack, repo, path_packfile,
1150 packidx);
1151 if (err)
1152 goto done;
1154 err = read_packed_tree_privsep(tree, pack,
1155 packidx, idx, id);
1156 } else if (err->code == GOT_ERR_NO_OBJ) {
1157 int fd;
1159 err = got_object_open_loose_fd(&fd, id, repo);
1160 if (err)
1161 return err;
1162 err = read_tree_privsep(tree, fd, id, repo);
1165 if (err == NULL) {
1166 (*tree)->refcnt++;
1167 err = got_repo_cache_tree(repo, id, *tree);
1169 done:
1170 free(path_packfile);
1171 return err;
1174 const struct got_error *
1175 got_object_open_as_tree(struct got_tree_object **tree,
1176 struct got_repository *repo, struct got_object_id *id)
1178 *tree = got_repo_get_cached_tree(repo, id);
1179 if (*tree != NULL) {
1180 (*tree)->refcnt++;
1181 return NULL;
1184 return open_tree(tree, repo, id, 0);
1187 const struct got_error *
1188 got_object_tree_open(struct got_tree_object **tree,
1189 struct got_repository *repo, struct got_object *obj)
1191 return open_tree(tree, repo, got_object_get_id(obj), 1);
1194 int
1195 got_object_tree_get_nentries(struct got_tree_object *tree)
1197 return tree->nentries;
1200 struct got_tree_entry *
1201 got_object_tree_get_first_entry(struct got_tree_object *tree)
1203 return got_object_tree_get_entry(tree, 0);
1206 struct got_tree_entry *
1207 got_object_tree_get_last_entry(struct got_tree_object *tree)
1209 return got_object_tree_get_entry(tree, tree->nentries - 1);
1212 struct got_tree_entry *
1213 got_object_tree_get_entry(struct got_tree_object *tree, int i)
1215 if (i < 0 || i >= tree->nentries)
1216 return NULL;
1217 return &tree->entries[i];
1220 mode_t
1221 got_tree_entry_get_mode(struct got_tree_entry *te)
1223 return te->mode;
1226 const char *
1227 got_tree_entry_get_name(struct got_tree_entry *te)
1229 return &te->name[0];
1232 struct got_object_id *
1233 got_tree_entry_get_id(struct got_tree_entry *te)
1235 return &te->id;
1238 const struct got_error *
1239 got_object_blob_read_to_str(char **s, struct got_blob_object *blob)
1241 const struct got_error *err = NULL;
1242 size_t len, totlen, hdrlen, offset;
1244 *s = NULL;
1246 hdrlen = got_object_blob_get_hdrlen(blob);
1247 totlen = 0;
1248 offset = 0;
1249 do {
1250 char *p;
1252 err = got_object_blob_read_block(&len, blob);
1253 if (err)
1254 return err;
1256 if (len == 0)
1257 break;
1259 totlen += len - hdrlen;
1260 p = realloc(*s, totlen + 1);
1261 if (p == NULL) {
1262 err = got_error_from_errno("realloc");
1263 free(*s);
1264 *s = NULL;
1265 return err;
1267 *s = p;
1268 /* Skip blob object header first time around. */
1269 memcpy(*s + offset,
1270 got_object_blob_get_read_buf(blob) + hdrlen, len - hdrlen);
1271 hdrlen = 0;
1272 offset = totlen;
1273 } while (len > 0);
1275 (*s)[totlen] = '\0';
1276 return NULL;
1279 const struct got_error *
1280 got_tree_entry_get_symlink_target(char **link_target, struct got_tree_entry *te,
1281 struct got_repository *repo)
1283 const struct got_error *err = NULL;
1284 struct got_blob_object *blob = NULL;
1286 *link_target = NULL;
1288 if (!got_object_tree_entry_is_symlink(te))
1289 return got_error(GOT_ERR_TREE_ENTRY_TYPE);
1291 err = got_object_open_as_blob(&blob, repo,
1292 got_tree_entry_get_id(te), PATH_MAX);
1293 if (err)
1294 return err;
1296 err = got_object_blob_read_to_str(link_target, blob);
1297 got_object_blob_close(blob);
1298 if (err) {
1299 free(*link_target);
1300 *link_target = NULL;
1302 return err;
1305 int
1306 got_tree_entry_get_index(struct got_tree_entry *te)
1308 return te->idx;
1311 struct got_tree_entry *
1312 got_tree_entry_get_next(struct got_tree_object *tree,
1313 struct got_tree_entry *te)
1315 return got_object_tree_get_entry(tree, te->idx + 1);
1318 struct got_tree_entry *
1319 got_tree_entry_get_prev(struct got_tree_object *tree,
1320 struct got_tree_entry *te)
1322 return got_object_tree_get_entry(tree, te->idx - 1);
1325 static const struct got_error *
1326 request_packed_blob(uint8_t **outbuf, size_t *size, size_t *hdrlen, int outfd,
1327 struct got_pack *pack, struct got_packidx *packidx, int idx,
1328 struct got_object_id *id)
1330 const struct got_error *err = NULL;
1331 struct imsgbuf *ibuf = pack->privsep_child->ibuf;
1332 int outfd_child;
1334 err = pack_child_send_tempfiles(ibuf, pack);
1335 if (err)
1336 return err;
1338 outfd_child = dup(outfd);
1339 if (outfd_child == -1)
1340 return got_error_from_errno("dup");
1342 err = got_privsep_send_blob_req(pack->privsep_child->ibuf, -1, id, idx);
1343 if (err)
1344 return err;
1346 err = got_privsep_send_blob_outfd(pack->privsep_child->ibuf,
1347 outfd_child);
1348 if (err) {
1349 return err;
1352 err = got_privsep_recv_blob(outbuf, size, hdrlen,
1353 pack->privsep_child->ibuf);
1354 if (err)
1355 return err;
1357 if (lseek(outfd, SEEK_SET, 0) == -1)
1358 err = got_error_from_errno("lseek");
1360 return err;
1363 static const struct got_error *
1364 read_packed_blob_privsep(uint8_t **outbuf, size_t *size, size_t *hdrlen,
1365 int outfd, struct got_pack *pack, struct got_packidx *packidx, int idx,
1366 struct got_object_id *id)
1368 const struct got_error *err = NULL;
1370 if (pack->privsep_child == NULL) {
1371 err = start_pack_privsep_child(pack, packidx);
1372 if (err)
1373 return err;
1376 return request_packed_blob(outbuf, size, hdrlen, outfd, pack, packidx,
1377 idx, id);
1380 static const struct got_error *
1381 request_blob(uint8_t **outbuf, size_t *size, size_t *hdrlen, int outfd,
1382 int infd, struct got_object_id *id, struct imsgbuf *ibuf)
1384 const struct got_error *err = NULL;
1385 int outfd_child;
1387 outfd_child = dup(outfd);
1388 if (outfd_child == -1)
1389 return got_error_from_errno("dup");
1391 err = got_privsep_send_blob_req(ibuf, infd, id, -1);
1392 if (err)
1393 return err;
1395 err = got_privsep_send_blob_outfd(ibuf, outfd_child);
1396 if (err)
1397 return err;
1399 err = got_privsep_recv_blob(outbuf, size, hdrlen, ibuf);
1400 if (err)
1401 return err;
1403 if (lseek(outfd, SEEK_SET, 0) == -1)
1404 return got_error_from_errno("lseek");
1406 return err;
1409 static const struct got_error *
1410 read_blob_privsep(uint8_t **outbuf, size_t *size, size_t *hdrlen,
1411 int outfd, int infd, struct got_object_id *id, struct got_repository *repo)
1413 const struct got_error *err;
1414 int imsg_fds[2];
1415 pid_t pid;
1416 struct imsgbuf *ibuf;
1418 if (repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_BLOB].imsg_fd != -1) {
1419 ibuf = repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_BLOB].ibuf;
1420 return request_blob(outbuf, size, hdrlen, outfd, infd, id,
1421 ibuf);
1424 ibuf = calloc(1, sizeof(*ibuf));
1425 if (ibuf == NULL)
1426 return got_error_from_errno("calloc");
1428 if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_fds) == -1) {
1429 err = got_error_from_errno("socketpair");
1430 free(ibuf);
1431 return err;
1434 pid = fork();
1435 if (pid == -1) {
1436 err = got_error_from_errno("fork");
1437 free(ibuf);
1438 return err;
1440 else if (pid == 0) {
1441 got_privsep_exec_child(imsg_fds, GOT_PATH_PROG_READ_BLOB,
1442 repo->path);
1443 /* not reached */
1446 if (close(imsg_fds[1]) == -1) {
1447 err = got_error_from_errno("close");
1448 free(ibuf);
1449 return err;
1451 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_BLOB].imsg_fd =
1452 imsg_fds[0];
1453 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_BLOB].pid = pid;
1454 imsg_init(ibuf, imsg_fds[0]);
1455 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_BLOB].ibuf = ibuf;
1457 return request_blob(outbuf, size, hdrlen, outfd, infd, id, ibuf);
1460 static const struct got_error *
1461 open_blob(struct got_blob_object **blob, struct got_repository *repo,
1462 struct got_object_id *id, size_t blocksize)
1464 const struct got_error *err = NULL;
1465 struct got_packidx *packidx = NULL;
1466 int idx;
1467 char *path_packfile = NULL;
1468 uint8_t *outbuf;
1469 int outfd;
1470 size_t size, hdrlen;
1471 struct stat sb;
1473 *blob = calloc(1, sizeof(**blob));
1474 if (*blob == NULL)
1475 return got_error_from_errno("calloc");
1477 outfd = got_opentempfd();
1478 if (outfd == -1)
1479 return got_error_from_errno("got_opentempfd");
1481 (*blob)->read_buf = malloc(blocksize);
1482 if ((*blob)->read_buf == NULL) {
1483 err = got_error_from_errno("malloc");
1484 goto done;
1487 err = got_repo_search_packidx(&packidx, &idx, repo, id);
1488 if (err == NULL) {
1489 struct got_pack *pack = NULL;
1491 err = got_packidx_get_packfile_path(&path_packfile,
1492 packidx->path_packidx);
1493 if (err)
1494 goto done;
1496 pack = got_repo_get_cached_pack(repo, path_packfile);
1497 if (pack == NULL) {
1498 err = got_repo_cache_pack(&pack, repo, path_packfile,
1499 packidx);
1500 if (err)
1501 goto done;
1503 err = read_packed_blob_privsep(&outbuf, &size, &hdrlen, outfd,
1504 pack, packidx, idx, id);
1505 } else if (err->code == GOT_ERR_NO_OBJ) {
1506 int infd;
1508 err = got_object_open_loose_fd(&infd, id, repo);
1509 if (err)
1510 goto done;
1511 err = read_blob_privsep(&outbuf, &size, &hdrlen, outfd, infd,
1512 id, repo);
1514 if (err)
1515 goto done;
1517 if (hdrlen > size) {
1518 err = got_error(GOT_ERR_BAD_OBJ_HDR);
1519 goto done;
1522 if (outbuf) {
1523 if (close(outfd) == -1 && err == NULL)
1524 err = got_error_from_errno("close");
1525 outfd = -1;
1526 (*blob)->f = fmemopen(outbuf, size, "rb");
1527 if ((*blob)->f == NULL) {
1528 err = got_error_from_errno("fmemopen");
1529 free(outbuf);
1530 goto done;
1532 (*blob)->data = outbuf;
1533 } else {
1534 if (fstat(outfd, &sb) == -1) {
1535 err = got_error_from_errno("fstat");
1536 goto done;
1539 if (sb.st_size != size) {
1540 err = got_error(GOT_ERR_PRIVSEP_LEN);
1541 goto done;
1544 (*blob)->f = fdopen(outfd, "rb");
1545 if ((*blob)->f == NULL) {
1546 err = got_error_from_errno("fdopen");
1547 close(outfd);
1548 outfd = -1;
1549 goto done;
1553 (*blob)->hdrlen = hdrlen;
1554 (*blob)->blocksize = blocksize;
1555 memcpy(&(*blob)->id.sha1, id->sha1, SHA1_DIGEST_LENGTH);
1557 done:
1558 free(path_packfile);
1559 if (err) {
1560 if (*blob) {
1561 got_object_blob_close(*blob);
1562 *blob = NULL;
1563 } else if (outfd != -1)
1564 close(outfd);
1566 return err;
1569 const struct got_error *
1570 got_object_open_as_blob(struct got_blob_object **blob,
1571 struct got_repository *repo, struct got_object_id *id,
1572 size_t blocksize)
1574 return open_blob(blob, repo, id, blocksize);
1577 const struct got_error *
1578 got_object_blob_open(struct got_blob_object **blob,
1579 struct got_repository *repo, struct got_object *obj, size_t blocksize)
1581 return open_blob(blob, repo, got_object_get_id(obj), blocksize);
1584 const struct got_error *
1585 got_object_blob_close(struct got_blob_object *blob)
1587 const struct got_error *err = NULL;
1588 free(blob->read_buf);
1589 if (blob->f && fclose(blob->f) == EOF)
1590 err = got_error_from_errno("fclose");
1591 free(blob->data);
1592 free(blob);
1593 return err;
1596 void
1597 got_object_blob_rewind(struct got_blob_object *blob)
1599 if (blob->f)
1600 rewind(blob->f);
1603 char *
1604 got_object_blob_id_str(struct got_blob_object *blob, char *buf, size_t size)
1606 return got_sha1_digest_to_str(blob->id.sha1, buf, size);
1609 size_t
1610 got_object_blob_get_hdrlen(struct got_blob_object *blob)
1612 return blob->hdrlen;
1615 const uint8_t *
1616 got_object_blob_get_read_buf(struct got_blob_object *blob)
1618 return blob->read_buf;
1621 const struct got_error *
1622 got_object_blob_read_block(size_t *outlenp, struct got_blob_object *blob)
1624 size_t n;
1626 n = fread(blob->read_buf, 1, blob->blocksize, blob->f);
1627 if (n == 0 && ferror(blob->f))
1628 return got_ferror(blob->f, GOT_ERR_IO);
1629 *outlenp = n;
1630 return NULL;
1633 const struct got_error *
1634 got_object_blob_dump_to_file(off_t *filesize, int *nlines,
1635 off_t **line_offsets, FILE *outfile, struct got_blob_object *blob)
1637 const struct got_error *err = NULL;
1638 size_t n, len, hdrlen;
1639 const uint8_t *buf;
1640 int i;
1641 const int alloc_chunksz = 512;
1642 size_t nalloc = 0;
1643 off_t off = 0, total_len = 0;
1645 if (line_offsets)
1646 *line_offsets = NULL;
1647 if (filesize)
1648 *filesize = 0;
1649 if (nlines)
1650 *nlines = 0;
1652 hdrlen = got_object_blob_get_hdrlen(blob);
1653 do {
1654 err = got_object_blob_read_block(&len, blob);
1655 if (err)
1656 return err;
1657 if (len == 0)
1658 break;
1659 buf = got_object_blob_get_read_buf(blob);
1660 i = hdrlen;
1661 if (nlines) {
1662 if (line_offsets && *line_offsets == NULL) {
1663 /* Have some data but perhaps no '\n'. */
1664 *nlines = 1;
1665 nalloc = alloc_chunksz;
1666 *line_offsets = calloc(nalloc,
1667 sizeof(**line_offsets));
1668 if (*line_offsets == NULL)
1669 return got_error_from_errno("calloc");
1671 /* Skip forward over end of first line. */
1672 while (i < len) {
1673 if (buf[i] == '\n')
1674 break;
1675 i++;
1678 /* Scan '\n' offsets in remaining chunk of data. */
1679 while (i < len) {
1680 if (buf[i] != '\n') {
1681 i++;
1682 continue;
1684 (*nlines)++;
1685 if (line_offsets && nalloc < *nlines) {
1686 size_t n = *nlines + alloc_chunksz;
1687 off_t *o = recallocarray(*line_offsets,
1688 nalloc, n, sizeof(**line_offsets));
1689 if (o == NULL) {
1690 free(*line_offsets);
1691 *line_offsets = NULL;
1692 return got_error_from_errno(
1693 "recallocarray");
1695 *line_offsets = o;
1696 nalloc = n;
1698 if (line_offsets) {
1699 off = total_len + i - hdrlen + 1;
1700 (*line_offsets)[*nlines - 1] = off;
1702 i++;
1705 /* Skip blob object header first time around. */
1706 n = fwrite(buf + hdrlen, 1, len - hdrlen, outfile);
1707 if (n != len - hdrlen)
1708 return got_ferror(outfile, GOT_ERR_IO);
1709 total_len += len - hdrlen;
1710 hdrlen = 0;
1711 } while (len != 0);
1713 if (fflush(outfile) != 0)
1714 return got_error_from_errno("fflush");
1715 rewind(outfile);
1717 if (filesize)
1718 *filesize = total_len;
1720 return NULL;
1723 static const struct got_error *
1724 request_packed_tag(struct got_tag_object **tag, struct got_pack *pack,
1725 int pack_idx, struct got_object_id *id)
1727 const struct got_error *err = NULL;
1729 err = got_privsep_send_tag_req(pack->privsep_child->ibuf, -1, id,
1730 pack_idx);
1731 if (err)
1732 return err;
1734 return got_privsep_recv_tag(tag, pack->privsep_child->ibuf);
1737 static const struct got_error *
1738 read_packed_tag_privsep(struct got_tag_object **tag,
1739 struct got_pack *pack, struct got_packidx *packidx, int idx,
1740 struct got_object_id *id)
1742 const struct got_error *err = NULL;
1744 if (pack->privsep_child)
1745 return request_packed_tag(tag, pack, idx, id);
1747 err = start_pack_privsep_child(pack, packidx);
1748 if (err)
1749 return err;
1751 return request_packed_tag(tag, pack, idx, id);
1754 static const struct got_error *
1755 request_tag(struct got_tag_object **tag, struct got_repository *repo,
1756 int fd, struct got_object_id *id)
1758 const struct got_error *err = NULL;
1759 struct imsgbuf *ibuf;
1761 ibuf = repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TAG].ibuf;
1763 err = got_privsep_send_tag_req(ibuf, fd, id, -1);
1764 if (err)
1765 return err;
1767 return got_privsep_recv_tag(tag, ibuf);
1770 static const struct got_error *
1771 read_tag_privsep(struct got_tag_object **tag, int obj_fd,
1772 struct got_object_id *id, struct got_repository *repo)
1774 const struct got_error *err;
1775 int imsg_fds[2];
1776 pid_t pid;
1777 struct imsgbuf *ibuf;
1779 if (repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TAG].imsg_fd != -1)
1780 return request_tag(tag, repo, obj_fd, id);
1782 ibuf = calloc(1, sizeof(*ibuf));
1783 if (ibuf == NULL)
1784 return got_error_from_errno("calloc");
1786 if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_fds) == -1) {
1787 err = got_error_from_errno("socketpair");
1788 free(ibuf);
1789 return err;
1792 pid = fork();
1793 if (pid == -1) {
1794 err = got_error_from_errno("fork");
1795 free(ibuf);
1796 return err;
1798 else if (pid == 0) {
1799 got_privsep_exec_child(imsg_fds, GOT_PATH_PROG_READ_TAG,
1800 repo->path);
1801 /* not reached */
1804 if (close(imsg_fds[1]) == -1) {
1805 err = got_error_from_errno("close");
1806 free(ibuf);
1807 return err;
1809 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TAG].imsg_fd =
1810 imsg_fds[0];
1811 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TAG].pid = pid;
1812 imsg_init(ibuf, imsg_fds[0]);
1813 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TAG].ibuf = ibuf;
1815 return request_tag(tag, repo, obj_fd, id);
1818 static const struct got_error *
1819 open_tag(struct got_tag_object **tag, struct got_repository *repo,
1820 struct got_object_id *id, int check_cache)
1822 const struct got_error *err = NULL;
1823 struct got_packidx *packidx = NULL;
1824 int idx;
1825 char *path_packfile = NULL;
1826 struct got_object *obj = NULL;
1827 int obj_type = GOT_OBJ_TYPE_ANY;
1829 if (check_cache) {
1830 *tag = got_repo_get_cached_tag(repo, id);
1831 if (*tag != NULL) {
1832 (*tag)->refcnt++;
1833 return NULL;
1835 } else
1836 *tag = NULL;
1838 err = got_repo_search_packidx(&packidx, &idx, repo, id);
1839 if (err == NULL) {
1840 struct got_pack *pack = NULL;
1842 err = got_packidx_get_packfile_path(&path_packfile,
1843 packidx->path_packidx);
1844 if (err)
1845 return err;
1847 pack = got_repo_get_cached_pack(repo, path_packfile);
1848 if (pack == NULL) {
1849 err = got_repo_cache_pack(&pack, repo, path_packfile,
1850 packidx);
1851 if (err)
1852 goto done;
1855 /* Beware of "lightweight" tags: Check object type first. */
1856 err = read_packed_object_privsep(&obj, repo, pack, packidx,
1857 idx, id);
1858 if (err)
1859 goto done;
1860 obj_type = obj->type;
1861 got_object_close(obj);
1862 if (obj_type != GOT_OBJ_TYPE_TAG) {
1863 err = got_error(GOT_ERR_OBJ_TYPE);
1864 goto done;
1866 err = read_packed_tag_privsep(tag, pack, packidx, idx, id);
1867 } else if (err->code == GOT_ERR_NO_OBJ) {
1868 int fd;
1870 err = got_object_open_loose_fd(&fd, id, repo);
1871 if (err)
1872 return err;
1873 err = got_object_read_header_privsep(&obj, id, repo, fd);
1874 if (err)
1875 return err;
1876 obj_type = obj->type;
1877 got_object_close(obj);
1878 if (obj_type != GOT_OBJ_TYPE_TAG)
1879 return got_error(GOT_ERR_OBJ_TYPE);
1881 err = got_object_open_loose_fd(&fd, id, repo);
1882 if (err)
1883 return err;
1884 err = read_tag_privsep(tag, fd, id, repo);
1887 if (err == NULL) {
1888 (*tag)->refcnt++;
1889 err = got_repo_cache_tag(repo, id, *tag);
1891 done:
1892 free(path_packfile);
1893 return err;
1896 const struct got_error *
1897 got_object_open_as_tag(struct got_tag_object **tag,
1898 struct got_repository *repo, struct got_object_id *id)
1900 *tag = got_repo_get_cached_tag(repo, id);
1901 if (*tag != NULL) {
1902 (*tag)->refcnt++;
1903 return NULL;
1906 return open_tag(tag, repo, id, 0);
1909 const struct got_error *
1910 got_object_tag_open(struct got_tag_object **tag,
1911 struct got_repository *repo, struct got_object *obj)
1913 return open_tag(tag, repo, got_object_get_id(obj), 1);
1916 const char *
1917 got_object_tag_get_name(struct got_tag_object *tag)
1919 return tag->tag;
1922 int
1923 got_object_tag_get_object_type(struct got_tag_object *tag)
1925 return tag->obj_type;
1928 struct got_object_id *
1929 got_object_tag_get_object_id(struct got_tag_object *tag)
1931 return &tag->id;
1934 time_t
1935 got_object_tag_get_tagger_time(struct got_tag_object *tag)
1937 return tag->tagger_time;
1940 time_t
1941 got_object_tag_get_tagger_gmtoff(struct got_tag_object *tag)
1943 return tag->tagger_gmtoff;
1946 const char *
1947 got_object_tag_get_tagger(struct got_tag_object *tag)
1949 return tag->tagger;
1952 const char *
1953 got_object_tag_get_message(struct got_tag_object *tag)
1955 return tag->tagmsg;
1958 static struct got_tree_entry *
1959 find_entry_by_name(struct got_tree_object *tree, const char *name, size_t len)
1961 int i;
1963 /* Note that tree entries are sorted in strncmp() order. */
1964 for (i = 0; i < tree->nentries; i++) {
1965 struct got_tree_entry *te = &tree->entries[i];
1966 int cmp = strncmp(te->name, name, len);
1967 if (cmp < 0)
1968 continue;
1969 if (cmp > 0)
1970 break;
1971 if (te->name[len] == '\0')
1972 return te;
1974 return NULL;
1977 struct got_tree_entry *
1978 got_object_tree_find_entry(struct got_tree_object *tree, const char *name)
1980 return find_entry_by_name(tree, name, strlen(name));
1983 const struct got_error *
1984 got_object_tree_find_path(struct got_object_id **id, mode_t *mode,
1985 struct got_repository *repo, struct got_tree_object *tree,
1986 const char *path)
1988 const struct got_error *err = NULL;
1989 struct got_tree_object *subtree = NULL;
1990 struct got_tree_entry *te = NULL;
1991 const char *seg, *s;
1992 size_t seglen;
1994 *id = NULL;
1996 s = path;
1997 while (s[0] == '/')
1998 s++;
1999 seg = s;
2000 seglen = 0;
2001 subtree = tree;
2002 while (*s) {
2003 struct got_tree_object *next_tree;
2005 if (*s != '/') {
2006 s++;
2007 seglen++;
2008 if (*s)
2009 continue;
2012 te = find_entry_by_name(subtree, seg, seglen);
2013 if (te == NULL) {
2014 err = got_error_path(path, GOT_ERR_NO_TREE_ENTRY);
2015 goto done;
2018 if (*s == '\0')
2019 break;
2021 seg = s + 1;
2022 seglen = 0;
2023 s++;
2024 if (*s) {
2025 err = got_object_open_as_tree(&next_tree, repo,
2026 &te->id);
2027 te = NULL;
2028 if (err)
2029 goto done;
2030 if (subtree != tree)
2031 got_object_tree_close(subtree);
2032 subtree = next_tree;
2036 if (te) {
2037 *id = got_object_id_dup(&te->id);
2038 if (*id == NULL)
2039 return got_error_from_errno("got_object_id_dup");
2040 if (mode)
2041 *mode = te->mode;
2042 } else
2043 err = got_error_path(path, GOT_ERR_NO_TREE_ENTRY);
2044 done:
2045 if (subtree && subtree != tree)
2046 got_object_tree_close(subtree);
2047 return err;
2049 const struct got_error *
2050 got_object_id_by_path(struct got_object_id **id, struct got_repository *repo,
2051 struct got_commit_object *commit, const char *path)
2053 const struct got_error *err = NULL;
2054 struct got_tree_object *tree = NULL;
2056 *id = NULL;
2058 /* Handle opening of root of commit's tree. */
2059 if (got_path_is_root_dir(path)) {
2060 *id = got_object_id_dup(commit->tree_id);
2061 if (*id == NULL)
2062 err = got_error_from_errno("got_object_id_dup");
2063 } else {
2064 err = got_object_open_as_tree(&tree, repo, commit->tree_id);
2065 if (err)
2066 goto done;
2067 err = got_object_tree_find_path(id, NULL, repo, tree, path);
2069 done:
2070 if (tree)
2071 got_object_tree_close(tree);
2072 return err;
2076 * Normalize file mode bits to avoid false positive tree entry differences
2077 * in case tree entries have unexpected mode bits set.
2079 static mode_t
2080 normalize_mode_for_comparison(mode_t mode)
2083 * For directories, the only relevant bit is the IFDIR bit.
2084 * This allows us to detect paths changing from a directory
2085 * to a file and vice versa.
2087 if (S_ISDIR(mode))
2088 return mode & S_IFDIR;
2091 * For symlinks, the only relevant bit is the IFLNK bit.
2092 * This allows us to detect paths changing from a symlinks
2093 * to a file or directory and vice versa.
2095 if (S_ISLNK(mode))
2096 return mode & S_IFLNK;
2098 /* For files, the only change we care about is the executable bit. */
2099 return mode & S_IXUSR;
2102 const struct got_error *
2103 got_object_tree_path_changed(int *changed,
2104 struct got_tree_object *tree01, struct got_tree_object *tree02,
2105 const char *path, struct got_repository *repo)
2107 const struct got_error *err = NULL;
2108 struct got_tree_object *tree1 = NULL, *tree2 = NULL;
2109 struct got_tree_entry *te1 = NULL, *te2 = NULL;
2110 const char *seg, *s;
2111 size_t seglen;
2113 *changed = 0;
2115 /* We not do support comparing the root path. */
2116 if (got_path_is_root_dir(path))
2117 return got_error_path(path, GOT_ERR_BAD_PATH);
2119 tree1 = tree01;
2120 tree2 = tree02;
2121 s = path;
2122 while (*s == '/')
2123 s++;
2124 seg = s;
2125 seglen = 0;
2126 while (*s) {
2127 struct got_tree_object *next_tree1, *next_tree2;
2128 mode_t mode1, mode2;
2130 if (*s != '/') {
2131 s++;
2132 seglen++;
2133 if (*s)
2134 continue;
2137 te1 = find_entry_by_name(tree1, seg, seglen);
2138 if (te1 == NULL) {
2139 err = got_error(GOT_ERR_NO_OBJ);
2140 goto done;
2143 if (tree2)
2144 te2 = find_entry_by_name(tree2, seg, seglen);
2146 if (te2) {
2147 mode1 = normalize_mode_for_comparison(te1->mode);
2148 mode2 = normalize_mode_for_comparison(te2->mode);
2149 if (mode1 != mode2) {
2150 *changed = 1;
2151 goto done;
2154 if (got_object_id_cmp(&te1->id, &te2->id) == 0) {
2155 *changed = 0;
2156 goto done;
2160 if (*s == '\0') { /* final path element */
2161 *changed = 1;
2162 goto done;
2165 seg = s + 1;
2166 s++;
2167 seglen = 0;
2168 if (*s) {
2169 err = got_object_open_as_tree(&next_tree1, repo,
2170 &te1->id);
2171 te1 = NULL;
2172 if (err)
2173 goto done;
2174 if (tree1 != tree01)
2175 got_object_tree_close(tree1);
2176 tree1 = next_tree1;
2178 if (te2) {
2179 err = got_object_open_as_tree(&next_tree2, repo,
2180 &te2->id);
2181 te2 = NULL;
2182 if (err)
2183 goto done;
2184 if (tree2 != tree02)
2185 got_object_tree_close(tree2);
2186 tree2 = next_tree2;
2187 } else if (tree2) {
2188 if (tree2 != tree02)
2189 got_object_tree_close(tree2);
2190 tree2 = NULL;
2194 done:
2195 if (tree1 && tree1 != tree01)
2196 got_object_tree_close(tree1);
2197 if (tree2 && tree2 != tree02)
2198 got_object_tree_close(tree2);
2199 return err;
2202 const struct got_error *
2203 got_object_tree_entry_dup(struct got_tree_entry **new_te,
2204 struct got_tree_entry *te)
2206 const struct got_error *err = NULL;
2208 *new_te = calloc(1, sizeof(**new_te));
2209 if (*new_te == NULL)
2210 return got_error_from_errno("calloc");
2212 (*new_te)->mode = te->mode;
2213 memcpy((*new_te)->name, te->name, sizeof((*new_te)->name));
2214 memcpy(&(*new_te)->id, &te->id, sizeof((*new_te)->id));
2215 return err;
2218 int
2219 got_object_tree_entry_is_submodule(struct got_tree_entry *te)
2221 return (te->mode & S_IFMT) == (S_IFDIR | S_IFLNK);
2224 int
2225 got_object_tree_entry_is_symlink(struct got_tree_entry *te)
2227 /* S_IFDIR check avoids confusing symlinks with submodules. */
2228 return ((te->mode & (S_IFDIR | S_IFLNK)) == S_IFLNK);
2231 static const struct got_error *
2232 resolve_symlink(char **link_target, const char *path,
2233 struct got_commit_object *commit, struct got_repository *repo)
2235 const struct got_error *err = NULL;
2236 char buf[PATH_MAX];
2237 char *name, *parent_path = NULL;
2238 struct got_object_id *tree_obj_id = NULL;
2239 struct got_tree_object *tree = NULL;
2240 struct got_tree_entry *te = NULL;
2242 *link_target = NULL;
2244 if (strlcpy(buf, path, sizeof(buf)) >= sizeof(buf))
2245 return got_error(GOT_ERR_NO_SPACE);
2247 name = basename(buf);
2248 if (name == NULL)
2249 return got_error_from_errno2("basename", path);
2251 err = got_path_dirname(&parent_path, path);
2252 if (err)
2253 return err;
2255 err = got_object_id_by_path(&tree_obj_id, repo, commit,
2256 parent_path);
2257 if (err) {
2258 if (err->code == GOT_ERR_NO_TREE_ENTRY) {
2259 /* Display the complete path in error message. */
2260 err = got_error_path(path, err->code);
2262 goto done;
2265 err = got_object_open_as_tree(&tree, repo, tree_obj_id);
2266 if (err)
2267 goto done;
2269 te = got_object_tree_find_entry(tree, name);
2270 if (te == NULL) {
2271 err = got_error_path(path, GOT_ERR_NO_TREE_ENTRY);
2272 goto done;
2275 if (got_object_tree_entry_is_symlink(te)) {
2276 err = got_tree_entry_get_symlink_target(link_target, te, repo);
2277 if (err)
2278 goto done;
2279 if (!got_path_is_absolute(*link_target)) {
2280 char *abspath;
2281 if (asprintf(&abspath, "%s/%s", parent_path,
2282 *link_target) == -1) {
2283 err = got_error_from_errno("asprintf");
2284 goto done;
2286 free(*link_target);
2287 *link_target = malloc(PATH_MAX);
2288 if (*link_target == NULL) {
2289 err = got_error_from_errno("malloc");
2290 goto done;
2292 err = got_canonpath(abspath, *link_target, PATH_MAX);
2293 free(abspath);
2294 if (err)
2295 goto done;
2298 done:
2299 free(tree_obj_id);
2300 if (tree)
2301 got_object_tree_close(tree);
2302 if (err) {
2303 free(*link_target);
2304 *link_target = NULL;
2306 return err;
2309 const struct got_error *
2310 got_object_resolve_symlinks(char **link_target, const char *path,
2311 struct got_commit_object *commit, struct got_repository *repo)
2313 const struct got_error *err = NULL;
2314 char *next_target = NULL;
2315 int max_recursion = 40; /* matches Git */
2317 *link_target = NULL;
2319 do {
2320 err = resolve_symlink(&next_target,
2321 *link_target ? *link_target : path, commit, repo);
2322 if (err)
2323 break;
2324 if (next_target) {
2325 free(*link_target);
2326 if (--max_recursion == 0) {
2327 err = got_error_path(path, GOT_ERR_RECURSION);
2328 *link_target = NULL;
2329 break;
2331 *link_target = next_target;
2333 } while (next_target);
2335 return err;
2338 const struct got_error *
2339 got_traverse_packed_commits(struct got_object_id_queue *traversed_commits,
2340 struct got_object_id *commit_id, const char *path,
2341 struct got_repository *repo)
2343 const struct got_error *err = NULL;
2344 struct got_pack *pack = NULL;
2345 struct got_packidx *packidx = NULL;
2346 char *path_packfile = NULL;
2347 struct got_commit_object *changed_commit = NULL;
2348 struct got_object_id *changed_commit_id = NULL;
2349 int idx;
2351 err = got_repo_search_packidx(&packidx, &idx, repo, commit_id);
2352 if (err) {
2353 if (err->code != GOT_ERR_NO_OBJ)
2354 return err;
2355 return NULL;
2358 err = got_packidx_get_packfile_path(&path_packfile,
2359 packidx->path_packidx);
2360 if (err)
2361 return err;
2363 pack = got_repo_get_cached_pack(repo, path_packfile);
2364 if (pack == NULL) {
2365 err = got_repo_cache_pack(&pack, repo, path_packfile, packidx);
2366 if (err)
2367 goto done;
2370 if (pack->privsep_child == NULL) {
2371 err = start_pack_privsep_child(pack, packidx);
2372 if (err)
2373 goto done;
2376 err = got_privsep_send_commit_traversal_request(
2377 pack->privsep_child->ibuf, commit_id, idx, path);
2378 if (err)
2379 goto done;
2381 err = got_privsep_recv_traversed_commits(&changed_commit,
2382 &changed_commit_id, traversed_commits, pack->privsep_child->ibuf);
2383 if (err)
2384 goto done;
2386 if (changed_commit) {
2388 * Cache the commit in which the path was changed.
2389 * This commit might be opened again soon.
2391 changed_commit->refcnt++;
2392 err = got_repo_cache_commit(repo, changed_commit_id,
2393 changed_commit);
2394 got_object_commit_close(changed_commit);
2396 done:
2397 free(path_packfile);
2398 free(changed_commit_id);
2399 return err;
2402 const struct got_error *
2403 got_object_enumerate(int *found_all_objects,
2404 got_object_enumerate_commit_cb cb_commit,
2405 got_object_enumerate_tree_cb cb_tree, void *cb_arg,
2406 struct got_object_id **ours, int nours,
2407 struct got_object_id **theirs, int ntheirs,
2408 struct got_packidx *packidx, struct got_repository *repo)
2410 const struct got_error *err = NULL;
2411 struct got_pack *pack;
2412 char *path_packfile = NULL;
2414 err = got_packidx_get_packfile_path(&path_packfile,
2415 packidx->path_packidx);
2416 if (err)
2417 return err;
2419 pack = got_repo_get_cached_pack(repo, path_packfile);
2420 if (pack == NULL) {
2421 err = got_repo_cache_pack(&pack, repo, path_packfile, packidx);
2422 if (err)
2423 goto done;
2426 if (pack->privsep_child == NULL) {
2427 err = start_pack_privsep_child(pack, packidx);
2428 if (err)
2429 goto done;
2432 err = got_privsep_send_object_enumeration_request(
2433 pack->privsep_child->ibuf);
2434 if (err)
2435 goto done;
2437 err = got_privsep_send_object_idlist(pack->privsep_child->ibuf,
2438 ours, nours);
2439 if (err)
2440 goto done;
2441 err = got_privsep_send_object_idlist_done(pack->privsep_child->ibuf);
2442 if (err)
2443 goto done;
2445 err = got_privsep_send_object_idlist(pack->privsep_child->ibuf,
2446 theirs, ntheirs);
2447 if (err)
2448 goto done;
2449 err = got_privsep_send_object_idlist_done(pack->privsep_child->ibuf);
2450 if (err)
2451 goto done;
2453 err = got_privsep_recv_enumerated_objects(found_all_objects,
2454 pack->privsep_child->ibuf, cb_commit, cb_tree, cb_arg, repo);
2455 done:
2456 free(path_packfile);
2457 return err;