Blob


1 /*
2 * Copyright (c) 2018, 2019 Stefan Sperling <stsp@openbsd.org>
3 *
4 * Permission to use, copy, modify, and distribute this software for any
5 * purpose with or without fee is hereby granted, provided that the above
6 * copyright notice and this permission notice appear in all copies.
7 *
8 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
15 */
17 #include <sys/types.h>
18 #include <sys/stat.h>
19 #include <sys/queue.h>
20 #include <sys/tree.h>
21 #include <sys/uio.h>
22 #include <sys/socket.h>
23 #include <sys/wait.h>
24 #include <sys/resource.h>
25 #include <sys/mman.h>
27 #include <errno.h>
28 #include <fcntl.h>
29 #include <stdio.h>
30 #include <stdlib.h>
31 #include <string.h>
32 #include <stdint.h>
33 #include <sha1.h>
34 #include <unistd.h>
35 #include <zlib.h>
36 #include <ctype.h>
37 #include <libgen.h>
38 #include <limits.h>
39 #include <imsg.h>
40 #include <time.h>
42 #include "got_error.h"
43 #include "got_object.h"
44 #include "got_repository.h"
45 #include "got_opentemp.h"
46 #include "got_path.h"
48 #include "got_lib_sha1.h"
49 #include "got_lib_delta.h"
50 #include "got_lib_inflate.h"
51 #include "got_lib_object.h"
52 #include "got_lib_privsep.h"
53 #include "got_lib_object_idcache.h"
54 #include "got_lib_object_cache.h"
55 #include "got_lib_object_parse.h"
56 #include "got_lib_pack.h"
57 #include "got_lib_repository.h"
59 #ifndef MIN
60 #define MIN(_a,_b) ((_a) < (_b) ? (_a) : (_b))
61 #endif
63 struct got_object_id *
64 got_object_get_id(struct got_object *obj)
65 {
66 return &obj->id;
67 }
69 const struct got_error *
70 got_object_get_id_str(char **outbuf, struct got_object *obj)
71 {
72 return got_object_id_str(outbuf, &obj->id);
73 }
75 const struct got_error *
76 got_object_get_type(int *type, struct got_repository *repo,
77 struct got_object_id *id)
78 {
79 const struct got_error *err = NULL;
80 struct got_object *obj;
82 err = got_object_open(&obj, repo, id);
83 if (err)
84 return err;
86 switch (obj->type) {
87 case GOT_OBJ_TYPE_COMMIT:
88 case GOT_OBJ_TYPE_TREE:
89 case GOT_OBJ_TYPE_BLOB:
90 case GOT_OBJ_TYPE_TAG:
91 *type = obj->type;
92 break;
93 default:
94 err = got_error(GOT_ERR_OBJ_TYPE);
95 break;
96 }
98 got_object_close(obj);
99 return err;
102 const struct got_error *
103 got_object_get_path(char **path, struct got_object_id *id,
104 struct got_repository *repo)
106 const struct got_error *err = NULL;
107 char *hex = NULL;
108 char *path_objects;
110 *path = NULL;
112 path_objects = got_repo_get_path_objects(repo);
113 if (path_objects == NULL)
114 return got_error_from_errno("got_repo_get_path_objects");
116 err = got_object_id_str(&hex, id);
117 if (err)
118 goto done;
120 if (asprintf(path, "%s/%.2x/%s", path_objects,
121 id->sha1[0], hex + 2) == -1)
122 err = got_error_from_errno("asprintf");
124 done:
125 free(hex);
126 free(path_objects);
127 return err;
130 const struct got_error *
131 got_object_open_loose_fd(int *fd, struct got_object_id *id,
132 struct got_repository *repo)
134 const struct got_error *err = NULL;
135 char *path;
137 err = got_object_get_path(&path, id, repo);
138 if (err)
139 return err;
140 *fd = open(path, O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
141 if (*fd == -1) {
142 err = got_error_from_errno2("open", path);
143 goto done;
145 done:
146 free(path);
147 return err;
150 static const struct got_error *
151 request_packed_object(struct got_object **obj, struct got_pack *pack, int idx,
152 struct got_object_id *id)
154 const struct got_error *err = NULL;
155 struct imsgbuf *ibuf = pack->privsep_child->ibuf;
157 err = got_privsep_send_packed_obj_req(ibuf, idx, id);
158 if (err)
159 return err;
161 err = got_privsep_recv_obj(obj, ibuf);
162 if (err)
163 return err;
165 memcpy(&(*obj)->id, id, sizeof((*obj)->id));
167 return NULL;
170 /* Create temporary files used during delta application. */
171 static const struct got_error *
172 pack_child_send_tempfiles(struct imsgbuf *ibuf, struct got_pack *pack)
174 const struct got_error *err;
175 int basefd = -1, accumfd = -1;
177 /*
178 * For performance reasons, the child will keep reusing the
179 * same temporary files during every object request.
180 * Opening and closing new files for every object request is
181 * too expensive during operations such as 'gotadmin pack'.
182 */
183 if (pack->child_has_tempfiles)
184 return NULL;
186 basefd = dup(pack->basefd);
187 if (basefd == -1)
188 return got_error_from_errno("dup");
190 accumfd = dup(pack->accumfd);
191 if (accumfd == -1) {
192 err = got_error_from_errno("dup");
193 goto done;
196 err = got_privsep_send_tmpfd(ibuf, basefd);
197 if (err)
198 goto done;
200 err = got_privsep_send_tmpfd(ibuf, accumfd);
201 done:
202 if (err) {
203 if (basefd != -1)
204 close(basefd);
205 if (accumfd != -1)
206 close(accumfd);
207 } else
208 pack->child_has_tempfiles = 1;
209 return NULL;
212 static const struct got_error *
213 request_packed_object_raw(uint8_t **outbuf, off_t *size, size_t *hdrlen,
214 int outfd, struct got_pack *pack, int idx, struct got_object_id *id)
216 const struct got_error *err = NULL;
217 struct imsgbuf *ibuf = pack->privsep_child->ibuf;
218 int outfd_child;
220 err = pack_child_send_tempfiles(ibuf, pack);
221 if (err)
222 return err;
224 outfd_child = dup(outfd);
225 if (outfd_child == -1)
226 return got_error_from_errno("dup");
228 err = got_privsep_send_packed_raw_obj_req(ibuf, idx, id);
229 if (err) {
230 close(outfd_child);
231 return err;
234 err = got_privsep_send_raw_obj_outfd(ibuf, outfd_child);
235 if (err)
236 return err;
238 err = got_privsep_recv_raw_obj(outbuf, size, hdrlen, ibuf);
239 if (err)
240 return err;
242 return NULL;
245 static void
246 set_max_datasize(void)
248 struct rlimit rl;
250 if (getrlimit(RLIMIT_DATA, &rl) != 0)
251 return;
253 rl.rlim_cur = rl.rlim_max;
254 setrlimit(RLIMIT_DATA, &rl);
257 static const struct got_error *
258 start_pack_privsep_child(struct got_pack *pack, struct got_packidx *packidx)
260 const struct got_error *err = NULL;
261 int imsg_fds[2];
262 pid_t pid;
263 struct imsgbuf *ibuf;
265 ibuf = calloc(1, sizeof(*ibuf));
266 if (ibuf == NULL)
267 return got_error_from_errno("calloc");
269 pack->privsep_child = calloc(1, sizeof(*pack->privsep_child));
270 if (pack->privsep_child == NULL) {
271 err = got_error_from_errno("calloc");
272 free(ibuf);
273 return err;
275 pack->child_has_tempfiles = 0;
276 pack->child_has_delta_outfd = 0;
278 if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_fds) == -1) {
279 err = got_error_from_errno("socketpair");
280 goto done;
283 pid = fork();
284 if (pid == -1) {
285 err = got_error_from_errno("fork");
286 goto done;
287 } else if (pid == 0) {
288 set_max_datasize();
289 got_privsep_exec_child(imsg_fds, GOT_PATH_PROG_READ_PACK,
290 pack->path_packfile);
291 /* not reached */
294 if (close(imsg_fds[1]) == -1)
295 return got_error_from_errno("close");
296 pack->privsep_child->imsg_fd = imsg_fds[0];
297 pack->privsep_child->pid = pid;
298 imsg_init(ibuf, imsg_fds[0]);
299 pack->privsep_child->ibuf = ibuf;
301 err = got_privsep_init_pack_child(ibuf, pack, packidx);
302 if (err) {
303 const struct got_error *child_err;
304 err = got_privsep_send_stop(pack->privsep_child->imsg_fd);
305 child_err = got_privsep_wait_for_child(
306 pack->privsep_child->pid);
307 if (child_err && err == NULL)
308 err = child_err;
310 done:
311 if (err) {
312 free(ibuf);
313 free(pack->privsep_child);
314 pack->privsep_child = NULL;
316 return err;
319 static const struct got_error *
320 read_packed_object_privsep(struct got_object **obj,
321 struct got_repository *repo, struct got_pack *pack,
322 struct got_packidx *packidx, int idx, struct got_object_id *id)
324 const struct got_error *err = NULL;
326 if (pack->privsep_child == NULL) {
327 err = start_pack_privsep_child(pack, packidx);
328 if (err)
329 return err;
332 return request_packed_object(obj, pack, idx, id);
335 static const struct got_error *
336 read_packed_object_raw_privsep(uint8_t **outbuf, off_t *size, size_t *hdrlen,
337 int outfd, struct got_pack *pack, struct got_packidx *packidx, int idx,
338 struct got_object_id *id)
340 const struct got_error *err = NULL;
342 if (pack->privsep_child == NULL) {
343 err = start_pack_privsep_child(pack, packidx);
344 if (err)
345 return err;
348 return request_packed_object_raw(outbuf, size, hdrlen, outfd, pack,
349 idx, id);
352 const struct got_error *
353 got_object_open_packed(struct got_object **obj, struct got_object_id *id,
354 struct got_repository *repo)
356 const struct got_error *err = NULL;
357 struct got_pack *pack = NULL;
358 struct got_packidx *packidx = NULL;
359 int idx;
360 char *path_packfile;
362 err = got_repo_search_packidx(&packidx, &idx, repo, id);
363 if (err)
364 return err;
366 err = got_packidx_get_packfile_path(&path_packfile,
367 packidx->path_packidx);
368 if (err)
369 return err;
371 pack = got_repo_get_cached_pack(repo, path_packfile);
372 if (pack == NULL) {
373 err = got_repo_cache_pack(&pack, repo, path_packfile, packidx);
374 if (err)
375 goto done;
378 err = read_packed_object_privsep(obj, repo, pack, packidx, idx, id);
379 if (err)
380 goto done;
381 done:
382 free(path_packfile);
383 return err;
386 const struct got_error *
387 got_object_open_from_packfile(struct got_object **obj, struct got_object_id *id,
388 struct got_pack *pack, struct got_packidx *packidx, int obj_idx,
389 struct got_repository *repo)
391 return read_packed_object_privsep(obj, repo, pack, packidx,
392 obj_idx, id);
395 const struct got_error *
396 got_object_read_raw_delta(uint64_t *base_size, uint64_t *result_size,
397 off_t *delta_size, off_t *delta_compressed_size, off_t *delta_offset,
398 off_t *delta_out_offset, struct got_object_id **base_id, int delta_cache_fd,
399 struct got_packidx *packidx, int obj_idx, struct got_object_id *id,
400 struct got_repository *repo)
402 const struct got_error *err = NULL;
403 struct got_pack *pack = NULL;
404 char *path_packfile;
406 *base_size = 0;
407 *result_size = 0;
408 *delta_size = 0;
409 *delta_compressed_size = 0;
410 *delta_offset = 0;
411 *delta_out_offset = 0;
413 err = got_packidx_get_packfile_path(&path_packfile,
414 packidx->path_packidx);
415 if (err)
416 return err;
418 pack = got_repo_get_cached_pack(repo, path_packfile);
419 if (pack == NULL) {
420 err = got_repo_cache_pack(&pack, repo, path_packfile, packidx);
421 if (err)
422 return err;
425 if (pack->privsep_child == NULL) {
426 err = start_pack_privsep_child(pack, packidx);
427 if (err)
428 return err;
431 if (!pack->child_has_delta_outfd) {
432 int outfd_child;
433 outfd_child = dup(delta_cache_fd);
434 if (outfd_child == -1)
435 return got_error_from_errno("dup");
436 err = got_privsep_send_raw_delta_outfd(
437 pack->privsep_child->ibuf, outfd_child);
438 if (err)
439 return err;
440 pack->child_has_delta_outfd = 1;
443 err = got_privsep_send_raw_delta_req(pack->privsep_child->ibuf,
444 obj_idx, id);
445 if (err)
446 return err;
448 return got_privsep_recv_raw_delta(base_size, result_size, delta_size,
449 delta_compressed_size, delta_offset, delta_out_offset, base_id,
450 pack->privsep_child->ibuf);
453 /*
454 * XXX This function does not really belong in object.c. It is only here
455 * because it needs start_pack_privsep_child(); relevant code should
456 * probably be moved to pack.c/pack_create.c.
457 */
458 const struct got_error *
459 got_object_prepare_delta_reuse(struct got_pack **pack,
460 struct got_packidx *packidx, int delta_outfd, struct got_repository *repo)
462 const struct got_error *err = NULL;
463 char *path_packfile = NULL;
465 err = got_packidx_get_packfile_path(&path_packfile,
466 packidx->path_packidx);
467 if (err)
468 return err;
470 *pack = got_repo_get_cached_pack(repo, path_packfile);
471 if (*pack == NULL) {
472 err = got_repo_cache_pack(pack, repo, path_packfile, packidx);
473 if (err)
474 goto done;
476 if ((*pack)->privsep_child == NULL) {
477 err = start_pack_privsep_child(*pack, packidx);
478 if (err)
479 goto done;
482 if (!(*pack)->child_has_delta_outfd) {
483 int outfd_child;
484 outfd_child = dup(delta_outfd);
485 if (outfd_child == -1) {
486 err = got_error_from_errno("dup");
487 goto done;
489 err = got_privsep_send_raw_delta_outfd(
490 (*pack)->privsep_child->ibuf, outfd_child);
491 if (err)
492 goto done;
493 (*pack)->child_has_delta_outfd = 1;
496 err = got_privsep_send_delta_reuse_req((*pack)->privsep_child->ibuf);
497 done:
498 free(path_packfile);
499 return err;
502 static const struct got_error *
503 request_object(struct got_object **obj, struct got_object_id *id,
504 struct got_repository *repo, int fd)
506 const struct got_error *err = NULL;
507 struct imsgbuf *ibuf;
509 ibuf = repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_OBJECT].ibuf;
511 err = got_privsep_send_obj_req(ibuf, fd, id);
512 if (err)
513 return err;
515 return got_privsep_recv_obj(obj, ibuf);
518 static const struct got_error *
519 request_raw_object(uint8_t **outbuf, off_t *size, size_t *hdrlen, int outfd,
520 struct got_object_id *id, struct got_repository *repo, int infd)
522 const struct got_error *err = NULL;
523 struct imsgbuf *ibuf;
524 int outfd_child;
526 ibuf = repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_OBJECT].ibuf;
528 outfd_child = dup(outfd);
529 if (outfd_child == -1)
530 return got_error_from_errno("dup");
532 err = got_privsep_send_raw_obj_req(ibuf, infd, id);
533 if (err)
534 return err;
536 err = got_privsep_send_raw_obj_outfd(ibuf, outfd_child);
537 if (err)
538 return err;
540 return got_privsep_recv_raw_obj(outbuf, size, hdrlen, ibuf);
543 static const struct got_error *
544 start_read_object_child(struct got_repository *repo)
546 const struct got_error *err = NULL;
547 int imsg_fds[2];
548 pid_t pid;
549 struct imsgbuf *ibuf;
551 ibuf = calloc(1, sizeof(*ibuf));
552 if (ibuf == NULL)
553 return got_error_from_errno("calloc");
555 if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_fds) == -1) {
556 err = got_error_from_errno("socketpair");
557 free(ibuf);
558 return err;
561 pid = fork();
562 if (pid == -1) {
563 err = got_error_from_errno("fork");
564 free(ibuf);
565 return err;
567 else if (pid == 0) {
568 got_privsep_exec_child(imsg_fds, GOT_PATH_PROG_READ_OBJECT,
569 repo->path);
570 /* not reached */
573 if (close(imsg_fds[1]) == -1) {
574 err = got_error_from_errno("close");
575 free(ibuf);
576 return err;
579 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_OBJECT].imsg_fd =
580 imsg_fds[0];
581 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_OBJECT].pid = pid;
582 imsg_init(ibuf, imsg_fds[0]);
583 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_OBJECT].ibuf = ibuf;
585 return NULL;
588 const struct got_error *
589 got_object_read_header_privsep(struct got_object **obj,
590 struct got_object_id *id, struct got_repository *repo, int obj_fd)
592 const struct got_error *err;
594 if (repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_OBJECT].imsg_fd != -1)
595 return request_object(obj, id, repo, obj_fd);
597 err = start_read_object_child(repo);
598 if (err) {
599 close(obj_fd);
600 return err;
603 return request_object(obj, id, repo, obj_fd);
606 static const struct got_error *
607 read_object_raw_privsep(uint8_t **outbuf, off_t *size, size_t *hdrlen,
608 int outfd, struct got_object_id *id, struct got_repository *repo,
609 int obj_fd)
611 const struct got_error *err;
613 if (repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_OBJECT].imsg_fd != -1)
614 return request_raw_object(outbuf, size, hdrlen, outfd, id,
615 repo, obj_fd);
617 err = start_read_object_child(repo);
618 if (err)
619 return err;
621 return request_raw_object(outbuf, size, hdrlen, outfd, id, repo,
622 obj_fd);
625 const struct got_error *
626 got_object_open(struct got_object **obj, struct got_repository *repo,
627 struct got_object_id *id)
629 const struct got_error *err = NULL;
630 int fd;
632 *obj = got_repo_get_cached_object(repo, id);
633 if (*obj != NULL) {
634 (*obj)->refcnt++;
635 return NULL;
638 err = got_object_open_packed(obj, id, repo);
639 if (err && err->code != GOT_ERR_NO_OBJ)
640 return err;
641 if (*obj) {
642 (*obj)->refcnt++;
643 return got_repo_cache_object(repo, id, *obj);
646 err = got_object_open_loose_fd(&fd, id, repo);
647 if (err) {
648 if (err->code == GOT_ERR_ERRNO && errno == ENOENT)
649 err = got_error_no_obj(id);
650 return err;
653 err = got_object_read_header_privsep(obj, id, repo, fd);
654 if (err)
655 return err;
657 memcpy((*obj)->id.sha1, id->sha1, SHA1_DIGEST_LENGTH);
659 (*obj)->refcnt++;
660 return got_repo_cache_object(repo, id, *obj);
663 /* *outfd must be initialized to -1 by caller */
664 const struct got_error *
665 got_object_raw_open(struct got_raw_object **obj, int *outfd,
666 struct got_repository *repo, struct got_object_id *id)
668 const struct got_error *err = NULL;
669 struct got_packidx *packidx = NULL;
670 int idx;
671 uint8_t *outbuf = NULL;
672 off_t size = 0;
673 size_t hdrlen = 0;
674 char *path_packfile = NULL;
676 *obj = got_repo_get_cached_raw_object(repo, id);
677 if (*obj != NULL) {
678 (*obj)->refcnt++;
679 return NULL;
682 if (*outfd == -1) {
683 *outfd = got_opentempfd();
684 if (*outfd == -1)
685 return got_error_from_errno("got_opentempfd");
688 err = got_repo_search_packidx(&packidx, &idx, repo, id);
689 if (err == NULL) {
690 struct got_pack *pack = NULL;
692 err = got_packidx_get_packfile_path(&path_packfile,
693 packidx->path_packidx);
694 if (err)
695 goto done;
697 pack = got_repo_get_cached_pack(repo, path_packfile);
698 if (pack == NULL) {
699 err = got_repo_cache_pack(&pack, repo, path_packfile,
700 packidx);
701 if (err)
702 goto done;
704 err = read_packed_object_raw_privsep(&outbuf, &size, &hdrlen,
705 *outfd, pack, packidx, idx, id);
706 if (err)
707 goto done;
708 } else if (err->code == GOT_ERR_NO_OBJ) {
709 int fd;
711 err = got_object_open_loose_fd(&fd, id, repo);
712 if (err)
713 goto done;
714 err = read_object_raw_privsep(&outbuf, &size, &hdrlen, *outfd,
715 id, repo, fd);
716 if (err)
717 goto done;
720 *obj = calloc(1, sizeof(**obj));
721 if (*obj == NULL) {
722 err = got_error_from_errno("calloc");
723 goto done;
725 (*obj)->fd = -1;
727 if (outbuf) {
728 (*obj)->data = outbuf;
729 } else {
730 struct stat sb;
731 if (fstat(*outfd, &sb) == -1) {
732 err = got_error_from_errno("fstat");
733 goto done;
736 if (sb.st_size != hdrlen + size) {
737 err = got_error(GOT_ERR_PRIVSEP_LEN);
738 goto done;
740 #ifndef GOT_PACK_NO_MMAP
741 if (hdrlen + size > 0) {
742 (*obj)->data = mmap(NULL, hdrlen + size, PROT_READ,
743 MAP_PRIVATE, *outfd, 0);
744 if ((*obj)->data == MAP_FAILED) {
745 if (errno != ENOMEM) {
746 err = got_error_from_errno("mmap");
747 goto done;
749 (*obj)->data = NULL;
750 } else {
751 (*obj)->fd = *outfd;
752 *outfd = -1;
755 #endif
756 if (*outfd != -1) {
757 (*obj)->f = fdopen(*outfd, "r");
758 if ((*obj)->f == NULL) {
759 err = got_error_from_errno("fdopen");
760 goto done;
762 *outfd = -1;
765 (*obj)->hdrlen = hdrlen;
766 (*obj)->size = size;
767 err = got_repo_cache_raw_object(repo, id, *obj);
768 done:
769 free(path_packfile);
770 if (err) {
771 if (*obj) {
772 got_object_raw_close(*obj);
773 *obj = NULL;
775 free(outbuf);
776 } else
777 (*obj)->refcnt++;
778 return err;
781 const struct got_error *
782 got_object_open_by_id_str(struct got_object **obj, struct got_repository *repo,
783 const char *id_str)
785 struct got_object_id id;
787 if (!got_parse_sha1_digest(id.sha1, id_str))
788 return got_error_path(id_str, GOT_ERR_BAD_OBJ_ID_STR);
790 return got_object_open(obj, repo, &id);
793 const struct got_error *
794 got_object_resolve_id_str(struct got_object_id **id,
795 struct got_repository *repo, const char *id_str)
797 const struct got_error *err = NULL;
798 struct got_object *obj;
800 err = got_object_open_by_id_str(&obj, repo, id_str);
801 if (err)
802 return err;
804 *id = got_object_id_dup(got_object_get_id(obj));
805 got_object_close(obj);
806 if (*id == NULL)
807 return got_error_from_errno("got_object_id_dup");
809 return NULL;
812 static const struct got_error *
813 request_packed_commit(struct got_commit_object **commit, struct got_pack *pack,
814 int pack_idx, struct got_object_id *id)
816 const struct got_error *err = NULL;
818 err = got_privsep_send_commit_req(pack->privsep_child->ibuf, -1, id,
819 pack_idx);
820 if (err)
821 return err;
823 err = got_privsep_recv_commit(commit, pack->privsep_child->ibuf);
824 if (err)
825 return err;
827 (*commit)->flags |= GOT_COMMIT_FLAG_PACKED;
828 return NULL;
831 static const struct got_error *
832 read_packed_commit_privsep(struct got_commit_object **commit,
833 struct got_pack *pack, struct got_packidx *packidx, int idx,
834 struct got_object_id *id)
836 const struct got_error *err = NULL;
838 if (pack->privsep_child)
839 return request_packed_commit(commit, pack, idx, id);
841 err = start_pack_privsep_child(pack, packidx);
842 if (err)
843 return err;
845 return request_packed_commit(commit, pack, idx, id);
848 static const struct got_error *
849 request_commit(struct got_commit_object **commit, struct got_repository *repo,
850 int fd, struct got_object_id *id)
852 const struct got_error *err = NULL;
853 struct imsgbuf *ibuf;
855 ibuf = repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_COMMIT].ibuf;
857 err = got_privsep_send_commit_req(ibuf, fd, id, -1);
858 if (err)
859 return err;
861 return got_privsep_recv_commit(commit, ibuf);
864 static const struct got_error *
865 read_commit_privsep(struct got_commit_object **commit, int obj_fd,
866 struct got_object_id *id, struct got_repository *repo)
868 const struct got_error *err;
869 int imsg_fds[2];
870 pid_t pid;
871 struct imsgbuf *ibuf;
873 if (repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_COMMIT].imsg_fd != -1)
874 return request_commit(commit, repo, obj_fd, id);
876 ibuf = calloc(1, sizeof(*ibuf));
877 if (ibuf == NULL)
878 return got_error_from_errno("calloc");
880 if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_fds) == -1) {
881 err = got_error_from_errno("socketpair");
882 free(ibuf);
883 return err;
886 pid = fork();
887 if (pid == -1) {
888 err = got_error_from_errno("fork");
889 free(ibuf);
890 return err;
892 else if (pid == 0) {
893 got_privsep_exec_child(imsg_fds, GOT_PATH_PROG_READ_COMMIT,
894 repo->path);
895 /* not reached */
898 if (close(imsg_fds[1]) == -1) {
899 err = got_error_from_errno("close");
900 free(ibuf);
901 return err;
903 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_COMMIT].imsg_fd =
904 imsg_fds[0];
905 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_COMMIT].pid = pid;
906 imsg_init(ibuf, imsg_fds[0]);
907 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_COMMIT].ibuf = ibuf;
909 return request_commit(commit, repo, obj_fd, id);
913 static const struct got_error *
914 open_commit(struct got_commit_object **commit,
915 struct got_repository *repo, struct got_object_id *id, int check_cache)
917 const struct got_error *err = NULL;
918 struct got_packidx *packidx = NULL;
919 int idx;
920 char *path_packfile = NULL;
922 if (check_cache) {
923 *commit = got_repo_get_cached_commit(repo, id);
924 if (*commit != NULL) {
925 (*commit)->refcnt++;
926 return NULL;
928 } else
929 *commit = NULL;
931 err = got_repo_search_packidx(&packidx, &idx, repo, id);
932 if (err == NULL) {
933 struct got_pack *pack = NULL;
935 err = got_packidx_get_packfile_path(&path_packfile,
936 packidx->path_packidx);
937 if (err)
938 return err;
940 pack = got_repo_get_cached_pack(repo, path_packfile);
941 if (pack == NULL) {
942 err = got_repo_cache_pack(&pack, repo, path_packfile,
943 packidx);
944 if (err)
945 goto done;
947 err = read_packed_commit_privsep(commit, pack,
948 packidx, idx, id);
949 } else if (err->code == GOT_ERR_NO_OBJ) {
950 int fd;
952 err = got_object_open_loose_fd(&fd, id, repo);
953 if (err)
954 return err;
955 err = read_commit_privsep(commit, fd, id, repo);
958 if (err == NULL) {
959 (*commit)->refcnt++;
960 err = got_repo_cache_commit(repo, id, *commit);
962 done:
963 free(path_packfile);
964 return err;
967 const struct got_error *
968 got_object_open_as_commit(struct got_commit_object **commit,
969 struct got_repository *repo, struct got_object_id *id)
971 *commit = got_repo_get_cached_commit(repo, id);
972 if (*commit != NULL) {
973 (*commit)->refcnt++;
974 return NULL;
977 return open_commit(commit, repo, id, 0);
980 const struct got_error *
981 got_object_commit_open(struct got_commit_object **commit,
982 struct got_repository *repo, struct got_object *obj)
984 return open_commit(commit, repo, got_object_get_id(obj), 1);
987 const struct got_error *
988 got_object_qid_alloc(struct got_object_qid **qid, struct got_object_id *id)
990 *qid = calloc(1, sizeof(**qid));
991 if (*qid == NULL)
992 return got_error_from_errno("calloc");
994 memcpy(&(*qid)->id, id, sizeof((*qid)->id));
995 return NULL;
998 const struct got_error *
999 got_object_id_queue_copy(const struct got_object_id_queue *src,
1000 struct got_object_id_queue *dest)
1002 const struct got_error *err;
1003 struct got_object_qid *qid;
1005 STAILQ_FOREACH(qid, src, entry) {
1006 struct got_object_qid *new;
1008 * Deep-copy the object ID only. Let the caller deal
1009 * with setting up the new->data pointer if needed.
1011 err = got_object_qid_alloc(&new, &qid->id);
1012 if (err) {
1013 got_object_id_queue_free(dest);
1014 return err;
1016 STAILQ_INSERT_TAIL(dest, new, entry);
1019 return NULL;
1022 static const struct got_error *
1023 request_packed_tree(struct got_tree_object **tree, struct got_pack *pack,
1024 int pack_idx, struct got_object_id *id)
1026 const struct got_error *err = NULL;
1028 err = got_privsep_send_tree_req(pack->privsep_child->ibuf, -1, id,
1029 pack_idx);
1030 if (err)
1031 return err;
1033 return got_privsep_recv_tree(tree, pack->privsep_child->ibuf);
1036 static const struct got_error *
1037 read_packed_tree_privsep(struct got_tree_object **tree,
1038 struct got_pack *pack, struct got_packidx *packidx, int idx,
1039 struct got_object_id *id)
1041 const struct got_error *err = NULL;
1043 if (pack->privsep_child)
1044 return request_packed_tree(tree, pack, idx, id);
1046 err = start_pack_privsep_child(pack, packidx);
1047 if (err)
1048 return err;
1050 return request_packed_tree(tree, pack, idx, id);
1053 static const struct got_error *
1054 request_tree(struct got_tree_object **tree, struct got_repository *repo,
1055 int fd, struct got_object_id *id)
1057 const struct got_error *err = NULL;
1058 struct imsgbuf *ibuf;
1060 ibuf = repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TREE].ibuf;
1062 err = got_privsep_send_tree_req(ibuf, fd, id, -1);
1063 if (err)
1064 return err;
1066 return got_privsep_recv_tree(tree, ibuf);
1069 const struct got_error *
1070 read_tree_privsep(struct got_tree_object **tree, int obj_fd,
1071 struct got_object_id *id, struct got_repository *repo)
1073 const struct got_error *err;
1074 int imsg_fds[2];
1075 pid_t pid;
1076 struct imsgbuf *ibuf;
1078 if (repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TREE].imsg_fd != -1)
1079 return request_tree(tree, repo, obj_fd, id);
1081 ibuf = calloc(1, sizeof(*ibuf));
1082 if (ibuf == NULL)
1083 return got_error_from_errno("calloc");
1085 if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_fds) == -1) {
1086 err = got_error_from_errno("socketpair");
1087 free(ibuf);
1088 return err;
1091 pid = fork();
1092 if (pid == -1) {
1093 err = got_error_from_errno("fork");
1094 free(ibuf);
1095 return err;
1097 else if (pid == 0) {
1098 got_privsep_exec_child(imsg_fds, GOT_PATH_PROG_READ_TREE,
1099 repo->path);
1100 /* not reached */
1103 if (close(imsg_fds[1]) == -1) {
1104 err = got_error_from_errno("close");
1105 free(ibuf);
1106 return err;
1108 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TREE].imsg_fd =
1109 imsg_fds[0];
1110 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TREE].pid = pid;
1111 imsg_init(ibuf, imsg_fds[0]);
1112 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TREE].ibuf = ibuf;
1115 return request_tree(tree, repo, obj_fd, id);
1118 static const struct got_error *
1119 open_tree(struct got_tree_object **tree, struct got_repository *repo,
1120 struct got_object_id *id, int check_cache)
1122 const struct got_error *err = NULL;
1123 struct got_packidx *packidx = NULL;
1124 int idx;
1125 char *path_packfile = NULL;
1127 if (check_cache) {
1128 *tree = got_repo_get_cached_tree(repo, id);
1129 if (*tree != NULL) {
1130 (*tree)->refcnt++;
1131 return NULL;
1133 } else
1134 *tree = NULL;
1136 err = got_repo_search_packidx(&packidx, &idx, repo, id);
1137 if (err == NULL) {
1138 struct got_pack *pack = NULL;
1140 err = got_packidx_get_packfile_path(&path_packfile,
1141 packidx->path_packidx);
1142 if (err)
1143 return err;
1145 pack = got_repo_get_cached_pack(repo, path_packfile);
1146 if (pack == NULL) {
1147 err = got_repo_cache_pack(&pack, repo, path_packfile,
1148 packidx);
1149 if (err)
1150 goto done;
1152 err = read_packed_tree_privsep(tree, pack,
1153 packidx, idx, id);
1154 } else if (err->code == GOT_ERR_NO_OBJ) {
1155 int fd;
1157 err = got_object_open_loose_fd(&fd, id, repo);
1158 if (err)
1159 return err;
1160 err = read_tree_privsep(tree, fd, id, repo);
1163 if (err == NULL) {
1164 (*tree)->refcnt++;
1165 err = got_repo_cache_tree(repo, id, *tree);
1167 done:
1168 free(path_packfile);
1169 return err;
1172 const struct got_error *
1173 got_object_open_as_tree(struct got_tree_object **tree,
1174 struct got_repository *repo, struct got_object_id *id)
1176 *tree = got_repo_get_cached_tree(repo, id);
1177 if (*tree != NULL) {
1178 (*tree)->refcnt++;
1179 return NULL;
1182 return open_tree(tree, repo, id, 0);
1185 const struct got_error *
1186 got_object_tree_open(struct got_tree_object **tree,
1187 struct got_repository *repo, struct got_object *obj)
1189 return open_tree(tree, repo, got_object_get_id(obj), 1);
1192 int
1193 got_object_tree_get_nentries(struct got_tree_object *tree)
1195 return tree->nentries;
1198 struct got_tree_entry *
1199 got_object_tree_get_first_entry(struct got_tree_object *tree)
1201 return got_object_tree_get_entry(tree, 0);
1204 struct got_tree_entry *
1205 got_object_tree_get_last_entry(struct got_tree_object *tree)
1207 return got_object_tree_get_entry(tree, tree->nentries - 1);
1210 struct got_tree_entry *
1211 got_object_tree_get_entry(struct got_tree_object *tree, int i)
1213 if (i < 0 || i >= tree->nentries)
1214 return NULL;
1215 return &tree->entries[i];
1218 mode_t
1219 got_tree_entry_get_mode(struct got_tree_entry *te)
1221 return te->mode;
1224 const char *
1225 got_tree_entry_get_name(struct got_tree_entry *te)
1227 return &te->name[0];
1230 struct got_object_id *
1231 got_tree_entry_get_id(struct got_tree_entry *te)
1233 return &te->id;
1236 const struct got_error *
1237 got_object_blob_read_to_str(char **s, struct got_blob_object *blob)
1239 const struct got_error *err = NULL;
1240 size_t len, totlen, hdrlen, offset;
1242 *s = NULL;
1244 hdrlen = got_object_blob_get_hdrlen(blob);
1245 totlen = 0;
1246 offset = 0;
1247 do {
1248 char *p;
1250 err = got_object_blob_read_block(&len, blob);
1251 if (err)
1252 return err;
1254 if (len == 0)
1255 break;
1257 totlen += len - hdrlen;
1258 p = realloc(*s, totlen + 1);
1259 if (p == NULL) {
1260 err = got_error_from_errno("realloc");
1261 free(*s);
1262 *s = NULL;
1263 return err;
1265 *s = p;
1266 /* Skip blob object header first time around. */
1267 memcpy(*s + offset,
1268 got_object_blob_get_read_buf(blob) + hdrlen, len - hdrlen);
1269 hdrlen = 0;
1270 offset = totlen;
1271 } while (len > 0);
1273 (*s)[totlen] = '\0';
1274 return NULL;
1277 const struct got_error *
1278 got_tree_entry_get_symlink_target(char **link_target, struct got_tree_entry *te,
1279 struct got_repository *repo)
1281 const struct got_error *err = NULL;
1282 struct got_blob_object *blob = NULL;
1284 *link_target = NULL;
1286 if (!got_object_tree_entry_is_symlink(te))
1287 return got_error(GOT_ERR_TREE_ENTRY_TYPE);
1289 err = got_object_open_as_blob(&blob, repo,
1290 got_tree_entry_get_id(te), PATH_MAX);
1291 if (err)
1292 return err;
1294 err = got_object_blob_read_to_str(link_target, blob);
1295 got_object_blob_close(blob);
1296 if (err) {
1297 free(*link_target);
1298 *link_target = NULL;
1300 return err;
1303 int
1304 got_tree_entry_get_index(struct got_tree_entry *te)
1306 return te->idx;
1309 struct got_tree_entry *
1310 got_tree_entry_get_next(struct got_tree_object *tree,
1311 struct got_tree_entry *te)
1313 return got_object_tree_get_entry(tree, te->idx + 1);
1316 struct got_tree_entry *
1317 got_tree_entry_get_prev(struct got_tree_object *tree,
1318 struct got_tree_entry *te)
1320 return got_object_tree_get_entry(tree, te->idx - 1);
1323 static const struct got_error *
1324 request_packed_blob(uint8_t **outbuf, size_t *size, size_t *hdrlen, int outfd,
1325 struct got_pack *pack, struct got_packidx *packidx, int idx,
1326 struct got_object_id *id)
1328 const struct got_error *err = NULL;
1329 struct imsgbuf *ibuf = pack->privsep_child->ibuf;
1330 int outfd_child;
1332 err = pack_child_send_tempfiles(ibuf, pack);
1333 if (err)
1334 return err;
1336 outfd_child = dup(outfd);
1337 if (outfd_child == -1)
1338 return got_error_from_errno("dup");
1340 err = got_privsep_send_blob_req(pack->privsep_child->ibuf, -1, id, idx);
1341 if (err)
1342 return err;
1344 err = got_privsep_send_blob_outfd(pack->privsep_child->ibuf,
1345 outfd_child);
1346 if (err) {
1347 return err;
1350 err = got_privsep_recv_blob(outbuf, size, hdrlen,
1351 pack->privsep_child->ibuf);
1352 if (err)
1353 return err;
1355 if (lseek(outfd, SEEK_SET, 0) == -1)
1356 err = got_error_from_errno("lseek");
1358 return err;
1361 static const struct got_error *
1362 read_packed_blob_privsep(uint8_t **outbuf, size_t *size, size_t *hdrlen,
1363 int outfd, struct got_pack *pack, struct got_packidx *packidx, int idx,
1364 struct got_object_id *id)
1366 const struct got_error *err = NULL;
1368 if (pack->privsep_child == NULL) {
1369 err = start_pack_privsep_child(pack, packidx);
1370 if (err)
1371 return err;
1374 return request_packed_blob(outbuf, size, hdrlen, outfd, pack, packidx,
1375 idx, id);
1378 static const struct got_error *
1379 request_blob(uint8_t **outbuf, size_t *size, size_t *hdrlen, int outfd,
1380 int infd, struct got_object_id *id, struct imsgbuf *ibuf)
1382 const struct got_error *err = NULL;
1383 int outfd_child;
1385 outfd_child = dup(outfd);
1386 if (outfd_child == -1)
1387 return got_error_from_errno("dup");
1389 err = got_privsep_send_blob_req(ibuf, infd, id, -1);
1390 if (err)
1391 return err;
1393 err = got_privsep_send_blob_outfd(ibuf, outfd_child);
1394 if (err)
1395 return err;
1397 err = got_privsep_recv_blob(outbuf, size, hdrlen, ibuf);
1398 if (err)
1399 return err;
1401 if (lseek(outfd, SEEK_SET, 0) == -1)
1402 return got_error_from_errno("lseek");
1404 return err;
1407 static const struct got_error *
1408 read_blob_privsep(uint8_t **outbuf, size_t *size, size_t *hdrlen,
1409 int outfd, int infd, struct got_object_id *id, struct got_repository *repo)
1411 const struct got_error *err;
1412 int imsg_fds[2];
1413 pid_t pid;
1414 struct imsgbuf *ibuf;
1416 if (repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_BLOB].imsg_fd != -1) {
1417 ibuf = repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_BLOB].ibuf;
1418 return request_blob(outbuf, size, hdrlen, outfd, infd, id,
1419 ibuf);
1422 ibuf = calloc(1, sizeof(*ibuf));
1423 if (ibuf == NULL)
1424 return got_error_from_errno("calloc");
1426 if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_fds) == -1) {
1427 err = got_error_from_errno("socketpair");
1428 free(ibuf);
1429 return err;
1432 pid = fork();
1433 if (pid == -1) {
1434 err = got_error_from_errno("fork");
1435 free(ibuf);
1436 return err;
1438 else if (pid == 0) {
1439 got_privsep_exec_child(imsg_fds, GOT_PATH_PROG_READ_BLOB,
1440 repo->path);
1441 /* not reached */
1444 if (close(imsg_fds[1]) == -1) {
1445 err = got_error_from_errno("close");
1446 free(ibuf);
1447 return err;
1449 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_BLOB].imsg_fd =
1450 imsg_fds[0];
1451 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_BLOB].pid = pid;
1452 imsg_init(ibuf, imsg_fds[0]);
1453 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_BLOB].ibuf = ibuf;
1455 return request_blob(outbuf, size, hdrlen, outfd, infd, id, ibuf);
1458 static const struct got_error *
1459 open_blob(struct got_blob_object **blob, struct got_repository *repo,
1460 struct got_object_id *id, size_t blocksize)
1462 const struct got_error *err = NULL;
1463 struct got_packidx *packidx = NULL;
1464 int idx;
1465 char *path_packfile = NULL;
1466 uint8_t *outbuf;
1467 int outfd;
1468 size_t size, hdrlen;
1469 struct stat sb;
1471 *blob = calloc(1, sizeof(**blob));
1472 if (*blob == NULL)
1473 return got_error_from_errno("calloc");
1475 outfd = got_opentempfd();
1476 if (outfd == -1)
1477 return got_error_from_errno("got_opentempfd");
1479 (*blob)->read_buf = malloc(blocksize);
1480 if ((*blob)->read_buf == NULL) {
1481 err = got_error_from_errno("malloc");
1482 goto done;
1485 err = got_repo_search_packidx(&packidx, &idx, repo, id);
1486 if (err == NULL) {
1487 struct got_pack *pack = NULL;
1489 err = got_packidx_get_packfile_path(&path_packfile,
1490 packidx->path_packidx);
1491 if (err)
1492 goto done;
1494 pack = got_repo_get_cached_pack(repo, path_packfile);
1495 if (pack == NULL) {
1496 err = got_repo_cache_pack(&pack, repo, path_packfile,
1497 packidx);
1498 if (err)
1499 goto done;
1501 err = read_packed_blob_privsep(&outbuf, &size, &hdrlen, outfd,
1502 pack, packidx, idx, id);
1503 } else if (err->code == GOT_ERR_NO_OBJ) {
1504 int infd;
1506 err = got_object_open_loose_fd(&infd, id, repo);
1507 if (err)
1508 goto done;
1509 err = read_blob_privsep(&outbuf, &size, &hdrlen, outfd, infd,
1510 id, repo);
1512 if (err)
1513 goto done;
1515 if (hdrlen > size) {
1516 err = got_error(GOT_ERR_BAD_OBJ_HDR);
1517 goto done;
1520 if (outbuf) {
1521 if (close(outfd) == -1 && err == NULL)
1522 err = got_error_from_errno("close");
1523 outfd = -1;
1524 (*blob)->f = fmemopen(outbuf, size, "rb");
1525 if ((*blob)->f == NULL) {
1526 err = got_error_from_errno("fmemopen");
1527 free(outbuf);
1528 goto done;
1530 (*blob)->data = outbuf;
1531 } else {
1532 if (fstat(outfd, &sb) == -1) {
1533 err = got_error_from_errno("fstat");
1534 goto done;
1537 if (sb.st_size != size) {
1538 err = got_error(GOT_ERR_PRIVSEP_LEN);
1539 goto done;
1542 (*blob)->f = fdopen(outfd, "rb");
1543 if ((*blob)->f == NULL) {
1544 err = got_error_from_errno("fdopen");
1545 close(outfd);
1546 outfd = -1;
1547 goto done;
1551 (*blob)->hdrlen = hdrlen;
1552 (*blob)->blocksize = blocksize;
1553 memcpy(&(*blob)->id.sha1, id->sha1, SHA1_DIGEST_LENGTH);
1555 done:
1556 free(path_packfile);
1557 if (err) {
1558 if (*blob) {
1559 got_object_blob_close(*blob);
1560 *blob = NULL;
1561 } else if (outfd != -1)
1562 close(outfd);
1564 return err;
1567 const struct got_error *
1568 got_object_open_as_blob(struct got_blob_object **blob,
1569 struct got_repository *repo, struct got_object_id *id,
1570 size_t blocksize)
1572 return open_blob(blob, repo, id, blocksize);
1575 const struct got_error *
1576 got_object_blob_open(struct got_blob_object **blob,
1577 struct got_repository *repo, struct got_object *obj, size_t blocksize)
1579 return open_blob(blob, repo, got_object_get_id(obj), blocksize);
1582 const struct got_error *
1583 got_object_blob_close(struct got_blob_object *blob)
1585 const struct got_error *err = NULL;
1586 free(blob->read_buf);
1587 if (blob->f && fclose(blob->f) == EOF)
1588 err = got_error_from_errno("fclose");
1589 free(blob->data);
1590 free(blob);
1591 return err;
1594 void
1595 got_object_blob_rewind(struct got_blob_object *blob)
1597 if (blob->f)
1598 rewind(blob->f);
1601 char *
1602 got_object_blob_id_str(struct got_blob_object *blob, char *buf, size_t size)
1604 return got_sha1_digest_to_str(blob->id.sha1, buf, size);
1607 size_t
1608 got_object_blob_get_hdrlen(struct got_blob_object *blob)
1610 return blob->hdrlen;
1613 const uint8_t *
1614 got_object_blob_get_read_buf(struct got_blob_object *blob)
1616 return blob->read_buf;
1619 const struct got_error *
1620 got_object_blob_read_block(size_t *outlenp, struct got_blob_object *blob)
1622 size_t n;
1624 n = fread(blob->read_buf, 1, blob->blocksize, blob->f);
1625 if (n == 0 && ferror(blob->f))
1626 return got_ferror(blob->f, GOT_ERR_IO);
1627 *outlenp = n;
1628 return NULL;
1631 const struct got_error *
1632 got_object_blob_dump_to_file(off_t *filesize, int *nlines,
1633 off_t **line_offsets, FILE *outfile, struct got_blob_object *blob)
1635 const struct got_error *err = NULL;
1636 size_t n, len, hdrlen;
1637 const uint8_t *buf;
1638 int i;
1639 const int alloc_chunksz = 512;
1640 size_t nalloc = 0;
1641 off_t off = 0, total_len = 0;
1643 if (line_offsets)
1644 *line_offsets = NULL;
1645 if (filesize)
1646 *filesize = 0;
1647 if (nlines)
1648 *nlines = 0;
1650 hdrlen = got_object_blob_get_hdrlen(blob);
1651 do {
1652 err = got_object_blob_read_block(&len, blob);
1653 if (err)
1654 return err;
1655 if (len == 0)
1656 break;
1657 buf = got_object_blob_get_read_buf(blob);
1658 i = hdrlen;
1659 if (nlines) {
1660 if (line_offsets && *line_offsets == NULL) {
1661 /* Have some data but perhaps no '\n'. */
1662 *nlines = 1;
1663 nalloc = alloc_chunksz;
1664 *line_offsets = calloc(nalloc,
1665 sizeof(**line_offsets));
1666 if (*line_offsets == NULL)
1667 return got_error_from_errno("calloc");
1669 /* Skip forward over end of first line. */
1670 while (i < len) {
1671 if (buf[i] == '\n')
1672 break;
1673 i++;
1676 /* Scan '\n' offsets in remaining chunk of data. */
1677 while (i < len) {
1678 if (buf[i] != '\n') {
1679 i++;
1680 continue;
1682 (*nlines)++;
1683 if (line_offsets && nalloc < *nlines) {
1684 size_t n = *nlines + alloc_chunksz;
1685 off_t *o = recallocarray(*line_offsets,
1686 nalloc, n, sizeof(**line_offsets));
1687 if (o == NULL) {
1688 free(*line_offsets);
1689 *line_offsets = NULL;
1690 return got_error_from_errno(
1691 "recallocarray");
1693 *line_offsets = o;
1694 nalloc = n;
1696 if (line_offsets) {
1697 off = total_len + i - hdrlen + 1;
1698 (*line_offsets)[*nlines - 1] = off;
1700 i++;
1703 /* Skip blob object header first time around. */
1704 n = fwrite(buf + hdrlen, 1, len - hdrlen, outfile);
1705 if (n != len - hdrlen)
1706 return got_ferror(outfile, GOT_ERR_IO);
1707 total_len += len - hdrlen;
1708 hdrlen = 0;
1709 } while (len != 0);
1711 if (fflush(outfile) != 0)
1712 return got_error_from_errno("fflush");
1713 rewind(outfile);
1715 if (filesize)
1716 *filesize = total_len;
1718 return NULL;
1721 static const struct got_error *
1722 request_packed_tag(struct got_tag_object **tag, struct got_pack *pack,
1723 int pack_idx, struct got_object_id *id)
1725 const struct got_error *err = NULL;
1727 err = got_privsep_send_tag_req(pack->privsep_child->ibuf, -1, id,
1728 pack_idx);
1729 if (err)
1730 return err;
1732 return got_privsep_recv_tag(tag, pack->privsep_child->ibuf);
1735 static const struct got_error *
1736 read_packed_tag_privsep(struct got_tag_object **tag,
1737 struct got_pack *pack, struct got_packidx *packidx, int idx,
1738 struct got_object_id *id)
1740 const struct got_error *err = NULL;
1742 if (pack->privsep_child)
1743 return request_packed_tag(tag, pack, idx, id);
1745 err = start_pack_privsep_child(pack, packidx);
1746 if (err)
1747 return err;
1749 return request_packed_tag(tag, pack, idx, id);
1752 static const struct got_error *
1753 request_tag(struct got_tag_object **tag, struct got_repository *repo,
1754 int fd, struct got_object_id *id)
1756 const struct got_error *err = NULL;
1757 struct imsgbuf *ibuf;
1759 ibuf = repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TAG].ibuf;
1761 err = got_privsep_send_tag_req(ibuf, fd, id, -1);
1762 if (err)
1763 return err;
1765 return got_privsep_recv_tag(tag, ibuf);
1768 static const struct got_error *
1769 read_tag_privsep(struct got_tag_object **tag, int obj_fd,
1770 struct got_object_id *id, struct got_repository *repo)
1772 const struct got_error *err;
1773 int imsg_fds[2];
1774 pid_t pid;
1775 struct imsgbuf *ibuf;
1777 if (repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TAG].imsg_fd != -1)
1778 return request_tag(tag, repo, obj_fd, id);
1780 ibuf = calloc(1, sizeof(*ibuf));
1781 if (ibuf == NULL)
1782 return got_error_from_errno("calloc");
1784 if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_fds) == -1) {
1785 err = got_error_from_errno("socketpair");
1786 free(ibuf);
1787 return err;
1790 pid = fork();
1791 if (pid == -1) {
1792 err = got_error_from_errno("fork");
1793 free(ibuf);
1794 return err;
1796 else if (pid == 0) {
1797 got_privsep_exec_child(imsg_fds, GOT_PATH_PROG_READ_TAG,
1798 repo->path);
1799 /* not reached */
1802 if (close(imsg_fds[1]) == -1) {
1803 err = got_error_from_errno("close");
1804 free(ibuf);
1805 return err;
1807 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TAG].imsg_fd =
1808 imsg_fds[0];
1809 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TAG].pid = pid;
1810 imsg_init(ibuf, imsg_fds[0]);
1811 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TAG].ibuf = ibuf;
1813 return request_tag(tag, repo, obj_fd, id);
1816 static const struct got_error *
1817 open_tag(struct got_tag_object **tag, struct got_repository *repo,
1818 struct got_object_id *id, int check_cache)
1820 const struct got_error *err = NULL;
1821 struct got_packidx *packidx = NULL;
1822 int idx;
1823 char *path_packfile = NULL;
1824 struct got_object *obj = NULL;
1825 int obj_type = GOT_OBJ_TYPE_ANY;
1827 if (check_cache) {
1828 *tag = got_repo_get_cached_tag(repo, id);
1829 if (*tag != NULL) {
1830 (*tag)->refcnt++;
1831 return NULL;
1833 } else
1834 *tag = NULL;
1836 err = got_repo_search_packidx(&packidx, &idx, repo, id);
1837 if (err == NULL) {
1838 struct got_pack *pack = NULL;
1840 err = got_packidx_get_packfile_path(&path_packfile,
1841 packidx->path_packidx);
1842 if (err)
1843 return err;
1845 pack = got_repo_get_cached_pack(repo, path_packfile);
1846 if (pack == NULL) {
1847 err = got_repo_cache_pack(&pack, repo, path_packfile,
1848 packidx);
1849 if (err)
1850 goto done;
1853 /* Beware of "lightweight" tags: Check object type first. */
1854 err = read_packed_object_privsep(&obj, repo, pack, packidx,
1855 idx, id);
1856 if (err)
1857 goto done;
1858 obj_type = obj->type;
1859 got_object_close(obj);
1860 if (obj_type != GOT_OBJ_TYPE_TAG) {
1861 err = got_error(GOT_ERR_OBJ_TYPE);
1862 goto done;
1864 err = read_packed_tag_privsep(tag, pack, packidx, idx, id);
1865 } else if (err->code == GOT_ERR_NO_OBJ) {
1866 int fd;
1868 err = got_object_open_loose_fd(&fd, id, repo);
1869 if (err)
1870 return err;
1871 err = got_object_read_header_privsep(&obj, id, repo, fd);
1872 if (err)
1873 return err;
1874 obj_type = obj->type;
1875 got_object_close(obj);
1876 if (obj_type != GOT_OBJ_TYPE_TAG)
1877 return got_error(GOT_ERR_OBJ_TYPE);
1879 err = got_object_open_loose_fd(&fd, id, repo);
1880 if (err)
1881 return err;
1882 err = read_tag_privsep(tag, fd, id, repo);
1885 if (err == NULL) {
1886 (*tag)->refcnt++;
1887 err = got_repo_cache_tag(repo, id, *tag);
1889 done:
1890 free(path_packfile);
1891 return err;
1894 const struct got_error *
1895 got_object_open_as_tag(struct got_tag_object **tag,
1896 struct got_repository *repo, struct got_object_id *id)
1898 *tag = got_repo_get_cached_tag(repo, id);
1899 if (*tag != NULL) {
1900 (*tag)->refcnt++;
1901 return NULL;
1904 return open_tag(tag, repo, id, 0);
1907 const struct got_error *
1908 got_object_tag_open(struct got_tag_object **tag,
1909 struct got_repository *repo, struct got_object *obj)
1911 return open_tag(tag, repo, got_object_get_id(obj), 1);
1914 const char *
1915 got_object_tag_get_name(struct got_tag_object *tag)
1917 return tag->tag;
1920 int
1921 got_object_tag_get_object_type(struct got_tag_object *tag)
1923 return tag->obj_type;
1926 struct got_object_id *
1927 got_object_tag_get_object_id(struct got_tag_object *tag)
1929 return &tag->id;
1932 time_t
1933 got_object_tag_get_tagger_time(struct got_tag_object *tag)
1935 return tag->tagger_time;
1938 time_t
1939 got_object_tag_get_tagger_gmtoff(struct got_tag_object *tag)
1941 return tag->tagger_gmtoff;
1944 const char *
1945 got_object_tag_get_tagger(struct got_tag_object *tag)
1947 return tag->tagger;
1950 const char *
1951 got_object_tag_get_message(struct got_tag_object *tag)
1953 return tag->tagmsg;
1956 static struct got_tree_entry *
1957 find_entry_by_name(struct got_tree_object *tree, const char *name, size_t len)
1959 int i;
1961 /* Note that tree entries are sorted in strncmp() order. */
1962 for (i = 0; i < tree->nentries; i++) {
1963 struct got_tree_entry *te = &tree->entries[i];
1964 int cmp = strncmp(te->name, name, len);
1965 if (cmp < 0)
1966 continue;
1967 if (cmp > 0)
1968 break;
1969 if (te->name[len] == '\0')
1970 return te;
1972 return NULL;
1975 struct got_tree_entry *
1976 got_object_tree_find_entry(struct got_tree_object *tree, const char *name)
1978 return find_entry_by_name(tree, name, strlen(name));
1981 const struct got_error *
1982 got_object_tree_find_path(struct got_object_id **id, mode_t *mode,
1983 struct got_repository *repo, struct got_tree_object *tree,
1984 const char *path)
1986 const struct got_error *err = NULL;
1987 struct got_tree_object *subtree = NULL;
1988 struct got_tree_entry *te = NULL;
1989 const char *seg, *s;
1990 size_t seglen;
1992 *id = NULL;
1994 s = path;
1995 while (s[0] == '/')
1996 s++;
1997 seg = s;
1998 seglen = 0;
1999 subtree = tree;
2000 while (*s) {
2001 struct got_tree_object *next_tree;
2003 if (*s != '/') {
2004 s++;
2005 seglen++;
2006 if (*s)
2007 continue;
2010 te = find_entry_by_name(subtree, seg, seglen);
2011 if (te == NULL) {
2012 err = got_error_path(path, GOT_ERR_NO_TREE_ENTRY);
2013 goto done;
2016 if (*s == '\0')
2017 break;
2019 seg = s + 1;
2020 seglen = 0;
2021 s++;
2022 if (*s) {
2023 err = got_object_open_as_tree(&next_tree, repo,
2024 &te->id);
2025 te = NULL;
2026 if (err)
2027 goto done;
2028 if (subtree != tree)
2029 got_object_tree_close(subtree);
2030 subtree = next_tree;
2034 if (te) {
2035 *id = got_object_id_dup(&te->id);
2036 if (*id == NULL)
2037 return got_error_from_errno("got_object_id_dup");
2038 if (mode)
2039 *mode = te->mode;
2040 } else
2041 err = got_error_path(path, GOT_ERR_NO_TREE_ENTRY);
2042 done:
2043 if (subtree && subtree != tree)
2044 got_object_tree_close(subtree);
2045 return err;
2047 const struct got_error *
2048 got_object_id_by_path(struct got_object_id **id, struct got_repository *repo,
2049 struct got_commit_object *commit, const char *path)
2051 const struct got_error *err = NULL;
2052 struct got_tree_object *tree = NULL;
2054 *id = NULL;
2056 /* Handle opening of root of commit's tree. */
2057 if (got_path_is_root_dir(path)) {
2058 *id = got_object_id_dup(commit->tree_id);
2059 if (*id == NULL)
2060 err = got_error_from_errno("got_object_id_dup");
2061 } else {
2062 err = got_object_open_as_tree(&tree, repo, commit->tree_id);
2063 if (err)
2064 goto done;
2065 err = got_object_tree_find_path(id, NULL, repo, tree, path);
2067 done:
2068 if (tree)
2069 got_object_tree_close(tree);
2070 return err;
2074 * Normalize file mode bits to avoid false positive tree entry differences
2075 * in case tree entries have unexpected mode bits set.
2077 static mode_t
2078 normalize_mode_for_comparison(mode_t mode)
2081 * For directories, the only relevant bit is the IFDIR bit.
2082 * This allows us to detect paths changing from a directory
2083 * to a file and vice versa.
2085 if (S_ISDIR(mode))
2086 return mode & S_IFDIR;
2089 * For symlinks, the only relevant bit is the IFLNK bit.
2090 * This allows us to detect paths changing from a symlinks
2091 * to a file or directory and vice versa.
2093 if (S_ISLNK(mode))
2094 return mode & S_IFLNK;
2096 /* For files, the only change we care about is the executable bit. */
2097 return mode & S_IXUSR;
2100 const struct got_error *
2101 got_object_tree_path_changed(int *changed,
2102 struct got_tree_object *tree01, struct got_tree_object *tree02,
2103 const char *path, struct got_repository *repo)
2105 const struct got_error *err = NULL;
2106 struct got_tree_object *tree1 = NULL, *tree2 = NULL;
2107 struct got_tree_entry *te1 = NULL, *te2 = NULL;
2108 const char *seg, *s;
2109 size_t seglen;
2111 *changed = 0;
2113 /* We not do support comparing the root path. */
2114 if (got_path_is_root_dir(path))
2115 return got_error_path(path, GOT_ERR_BAD_PATH);
2117 tree1 = tree01;
2118 tree2 = tree02;
2119 s = path;
2120 while (*s == '/')
2121 s++;
2122 seg = s;
2123 seglen = 0;
2124 while (*s) {
2125 struct got_tree_object *next_tree1, *next_tree2;
2126 mode_t mode1, mode2;
2128 if (*s != '/') {
2129 s++;
2130 seglen++;
2131 if (*s)
2132 continue;
2135 te1 = find_entry_by_name(tree1, seg, seglen);
2136 if (te1 == NULL) {
2137 err = got_error(GOT_ERR_NO_OBJ);
2138 goto done;
2141 if (tree2)
2142 te2 = find_entry_by_name(tree2, seg, seglen);
2144 if (te2) {
2145 mode1 = normalize_mode_for_comparison(te1->mode);
2146 mode2 = normalize_mode_for_comparison(te2->mode);
2147 if (mode1 != mode2) {
2148 *changed = 1;
2149 goto done;
2152 if (got_object_id_cmp(&te1->id, &te2->id) == 0) {
2153 *changed = 0;
2154 goto done;
2158 if (*s == '\0') { /* final path element */
2159 *changed = 1;
2160 goto done;
2163 seg = s + 1;
2164 s++;
2165 seglen = 0;
2166 if (*s) {
2167 err = got_object_open_as_tree(&next_tree1, repo,
2168 &te1->id);
2169 te1 = NULL;
2170 if (err)
2171 goto done;
2172 if (tree1 != tree01)
2173 got_object_tree_close(tree1);
2174 tree1 = next_tree1;
2176 if (te2) {
2177 err = got_object_open_as_tree(&next_tree2, repo,
2178 &te2->id);
2179 te2 = NULL;
2180 if (err)
2181 goto done;
2182 if (tree2 != tree02)
2183 got_object_tree_close(tree2);
2184 tree2 = next_tree2;
2185 } else if (tree2) {
2186 if (tree2 != tree02)
2187 got_object_tree_close(tree2);
2188 tree2 = NULL;
2192 done:
2193 if (tree1 && tree1 != tree01)
2194 got_object_tree_close(tree1);
2195 if (tree2 && tree2 != tree02)
2196 got_object_tree_close(tree2);
2197 return err;
2200 const struct got_error *
2201 got_object_tree_entry_dup(struct got_tree_entry **new_te,
2202 struct got_tree_entry *te)
2204 const struct got_error *err = NULL;
2206 *new_te = calloc(1, sizeof(**new_te));
2207 if (*new_te == NULL)
2208 return got_error_from_errno("calloc");
2210 (*new_te)->mode = te->mode;
2211 memcpy((*new_te)->name, te->name, sizeof((*new_te)->name));
2212 memcpy(&(*new_te)->id, &te->id, sizeof((*new_te)->id));
2213 return err;
2216 int
2217 got_object_tree_entry_is_submodule(struct got_tree_entry *te)
2219 return (te->mode & S_IFMT) == (S_IFDIR | S_IFLNK);
2222 int
2223 got_object_tree_entry_is_symlink(struct got_tree_entry *te)
2225 /* S_IFDIR check avoids confusing symlinks with submodules. */
2226 return ((te->mode & (S_IFDIR | S_IFLNK)) == S_IFLNK);
2229 static const struct got_error *
2230 resolve_symlink(char **link_target, const char *path,
2231 struct got_commit_object *commit, struct got_repository *repo)
2233 const struct got_error *err = NULL;
2234 char buf[PATH_MAX];
2235 char *name, *parent_path = NULL;
2236 struct got_object_id *tree_obj_id = NULL;
2237 struct got_tree_object *tree = NULL;
2238 struct got_tree_entry *te = NULL;
2240 *link_target = NULL;
2242 if (strlcpy(buf, path, sizeof(buf)) >= sizeof(buf))
2243 return got_error(GOT_ERR_NO_SPACE);
2245 name = basename(buf);
2246 if (name == NULL)
2247 return got_error_from_errno2("basename", path);
2249 err = got_path_dirname(&parent_path, path);
2250 if (err)
2251 return err;
2253 err = got_object_id_by_path(&tree_obj_id, repo, commit,
2254 parent_path);
2255 if (err) {
2256 if (err->code == GOT_ERR_NO_TREE_ENTRY) {
2257 /* Display the complete path in error message. */
2258 err = got_error_path(path, err->code);
2260 goto done;
2263 err = got_object_open_as_tree(&tree, repo, tree_obj_id);
2264 if (err)
2265 goto done;
2267 te = got_object_tree_find_entry(tree, name);
2268 if (te == NULL) {
2269 err = got_error_path(path, GOT_ERR_NO_TREE_ENTRY);
2270 goto done;
2273 if (got_object_tree_entry_is_symlink(te)) {
2274 err = got_tree_entry_get_symlink_target(link_target, te, repo);
2275 if (err)
2276 goto done;
2277 if (!got_path_is_absolute(*link_target)) {
2278 char *abspath;
2279 if (asprintf(&abspath, "%s/%s", parent_path,
2280 *link_target) == -1) {
2281 err = got_error_from_errno("asprintf");
2282 goto done;
2284 free(*link_target);
2285 *link_target = malloc(PATH_MAX);
2286 if (*link_target == NULL) {
2287 err = got_error_from_errno("malloc");
2288 goto done;
2290 err = got_canonpath(abspath, *link_target, PATH_MAX);
2291 free(abspath);
2292 if (err)
2293 goto done;
2296 done:
2297 free(tree_obj_id);
2298 if (tree)
2299 got_object_tree_close(tree);
2300 if (err) {
2301 free(*link_target);
2302 *link_target = NULL;
2304 return err;
2307 const struct got_error *
2308 got_object_resolve_symlinks(char **link_target, const char *path,
2309 struct got_commit_object *commit, struct got_repository *repo)
2311 const struct got_error *err = NULL;
2312 char *next_target = NULL;
2313 int max_recursion = 40; /* matches Git */
2315 *link_target = NULL;
2317 do {
2318 err = resolve_symlink(&next_target,
2319 *link_target ? *link_target : path, commit, repo);
2320 if (err)
2321 break;
2322 if (next_target) {
2323 free(*link_target);
2324 if (--max_recursion == 0) {
2325 err = got_error_path(path, GOT_ERR_RECURSION);
2326 *link_target = NULL;
2327 break;
2329 *link_target = next_target;
2331 } while (next_target);
2333 return err;
2336 const struct got_error *
2337 got_traverse_packed_commits(struct got_object_id_queue *traversed_commits,
2338 struct got_object_id *commit_id, const char *path,
2339 struct got_repository *repo)
2341 const struct got_error *err = NULL;
2342 struct got_pack *pack = NULL;
2343 struct got_packidx *packidx = NULL;
2344 char *path_packfile = NULL;
2345 struct got_commit_object *changed_commit = NULL;
2346 struct got_object_id *changed_commit_id = NULL;
2347 int idx;
2349 err = got_repo_search_packidx(&packidx, &idx, repo, commit_id);
2350 if (err) {
2351 if (err->code != GOT_ERR_NO_OBJ)
2352 return err;
2353 return NULL;
2356 err = got_packidx_get_packfile_path(&path_packfile,
2357 packidx->path_packidx);
2358 if (err)
2359 return err;
2361 pack = got_repo_get_cached_pack(repo, path_packfile);
2362 if (pack == NULL) {
2363 err = got_repo_cache_pack(&pack, repo, path_packfile, packidx);
2364 if (err)
2365 goto done;
2368 if (pack->privsep_child == NULL) {
2369 err = start_pack_privsep_child(pack, packidx);
2370 if (err)
2371 goto done;
2374 err = got_privsep_send_commit_traversal_request(
2375 pack->privsep_child->ibuf, commit_id, idx, path);
2376 if (err)
2377 goto done;
2379 err = got_privsep_recv_traversed_commits(&changed_commit,
2380 &changed_commit_id, traversed_commits, pack->privsep_child->ibuf);
2381 if (err)
2382 goto done;
2384 if (changed_commit) {
2386 * Cache the commit in which the path was changed.
2387 * This commit might be opened again soon.
2389 changed_commit->refcnt++;
2390 err = got_repo_cache_commit(repo, changed_commit_id,
2391 changed_commit);
2392 got_object_commit_close(changed_commit);
2394 done:
2395 free(path_packfile);
2396 free(changed_commit_id);
2397 return err;