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/uio.h>
21 #include <sys/socket.h>
22 #include <sys/wait.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 const struct got_error *
248 read_packed_object_privsep(struct got_object **obj,
249 struct got_repository *repo, struct got_pack *pack,
250 struct got_packidx *packidx, int idx, struct got_object_id *id)
252 const struct got_error *err = NULL;
254 if (pack->privsep_child == NULL) {
255 err = got_pack_start_privsep_child(pack, packidx);
256 if (err)
257 return err;
260 return request_packed_object(obj, pack, idx, id);
263 static const struct got_error *
264 read_packed_object_raw_privsep(uint8_t **outbuf, off_t *size, size_t *hdrlen,
265 int outfd, struct got_pack *pack, struct got_packidx *packidx, int idx,
266 struct got_object_id *id)
268 const struct got_error *err = NULL;
270 if (pack->privsep_child == NULL) {
271 err = got_pack_start_privsep_child(pack, packidx);
272 if (err)
273 return err;
276 return request_packed_object_raw(outbuf, size, hdrlen, outfd, pack,
277 idx, id);
280 const struct got_error *
281 got_object_open_packed(struct got_object **obj, struct got_object_id *id,
282 struct got_repository *repo)
284 const struct got_error *err = NULL;
285 struct got_pack *pack = NULL;
286 struct got_packidx *packidx = NULL;
287 int idx;
288 char *path_packfile;
290 err = got_repo_search_packidx(&packidx, &idx, repo, id);
291 if (err)
292 return err;
294 err = got_packidx_get_packfile_path(&path_packfile,
295 packidx->path_packidx);
296 if (err)
297 return err;
299 pack = got_repo_get_cached_pack(repo, path_packfile);
300 if (pack == NULL) {
301 err = got_repo_cache_pack(&pack, repo, path_packfile, packidx);
302 if (err)
303 goto done;
306 err = read_packed_object_privsep(obj, repo, pack, packidx, idx, id);
307 if (err)
308 goto done;
309 done:
310 free(path_packfile);
311 return err;
314 const struct got_error *
315 got_object_open_from_packfile(struct got_object **obj, struct got_object_id *id,
316 struct got_pack *pack, struct got_packidx *packidx, int obj_idx,
317 struct got_repository *repo)
319 return read_packed_object_privsep(obj, repo, pack, packidx,
320 obj_idx, id);
323 const struct got_error *
324 got_object_read_raw_delta(uint64_t *base_size, uint64_t *result_size,
325 off_t *delta_size, off_t *delta_compressed_size, off_t *delta_offset,
326 off_t *delta_out_offset, struct got_object_id **base_id, int delta_cache_fd,
327 struct got_packidx *packidx, int obj_idx, struct got_object_id *id,
328 struct got_repository *repo)
330 const struct got_error *err = NULL;
331 struct got_pack *pack = NULL;
332 char *path_packfile;
334 *base_size = 0;
335 *result_size = 0;
336 *delta_size = 0;
337 *delta_compressed_size = 0;
338 *delta_offset = 0;
339 *delta_out_offset = 0;
341 err = got_packidx_get_packfile_path(&path_packfile,
342 packidx->path_packidx);
343 if (err)
344 return err;
346 pack = got_repo_get_cached_pack(repo, path_packfile);
347 if (pack == NULL) {
348 err = got_repo_cache_pack(&pack, repo, path_packfile, packidx);
349 if (err)
350 return err;
353 if (pack->privsep_child == NULL) {
354 err = got_pack_start_privsep_child(pack, packidx);
355 if (err)
356 return err;
359 if (!pack->child_has_delta_outfd) {
360 int outfd_child;
361 outfd_child = dup(delta_cache_fd);
362 if (outfd_child == -1)
363 return got_error_from_errno("dup");
364 err = got_privsep_send_raw_delta_outfd(
365 pack->privsep_child->ibuf, outfd_child);
366 if (err)
367 return err;
368 pack->child_has_delta_outfd = 1;
371 err = got_privsep_send_raw_delta_req(pack->privsep_child->ibuf,
372 obj_idx, id);
373 if (err)
374 return err;
376 return got_privsep_recv_raw_delta(base_size, result_size, delta_size,
377 delta_compressed_size, delta_offset, delta_out_offset, base_id,
378 pack->privsep_child->ibuf);
381 static const struct got_error *
382 request_object(struct got_object **obj, struct got_object_id *id,
383 struct got_repository *repo, int fd)
385 const struct got_error *err = NULL;
386 struct imsgbuf *ibuf;
388 ibuf = repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_OBJECT].ibuf;
390 err = got_privsep_send_obj_req(ibuf, fd, id);
391 if (err)
392 return err;
394 return got_privsep_recv_obj(obj, ibuf);
397 static const struct got_error *
398 request_raw_object(uint8_t **outbuf, off_t *size, size_t *hdrlen, int outfd,
399 struct got_object_id *id, struct got_repository *repo, int infd)
401 const struct got_error *err = NULL;
402 struct imsgbuf *ibuf;
403 int outfd_child;
405 ibuf = repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_OBJECT].ibuf;
407 outfd_child = dup(outfd);
408 if (outfd_child == -1)
409 return got_error_from_errno("dup");
411 err = got_privsep_send_raw_obj_req(ibuf, infd, id);
412 if (err)
413 return err;
415 err = got_privsep_send_raw_obj_outfd(ibuf, outfd_child);
416 if (err)
417 return err;
419 return got_privsep_recv_raw_obj(outbuf, size, hdrlen, ibuf);
422 static const struct got_error *
423 start_read_object_child(struct got_repository *repo)
425 const struct got_error *err = NULL;
426 int imsg_fds[2];
427 pid_t pid;
428 struct imsgbuf *ibuf;
430 ibuf = calloc(1, sizeof(*ibuf));
431 if (ibuf == NULL)
432 return got_error_from_errno("calloc");
434 if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_fds) == -1) {
435 err = got_error_from_errno("socketpair");
436 free(ibuf);
437 return err;
440 pid = fork();
441 if (pid == -1) {
442 err = got_error_from_errno("fork");
443 free(ibuf);
444 return err;
446 else if (pid == 0) {
447 got_privsep_exec_child(imsg_fds, GOT_PATH_PROG_READ_OBJECT,
448 repo->path);
449 /* not reached */
452 if (close(imsg_fds[1]) == -1) {
453 err = got_error_from_errno("close");
454 free(ibuf);
455 return err;
458 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_OBJECT].imsg_fd =
459 imsg_fds[0];
460 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_OBJECT].pid = pid;
461 imsg_init(ibuf, imsg_fds[0]);
462 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_OBJECT].ibuf = ibuf;
464 return NULL;
467 const struct got_error *
468 got_object_read_header_privsep(struct got_object **obj,
469 struct got_object_id *id, struct got_repository *repo, int obj_fd)
471 const struct got_error *err;
473 if (repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_OBJECT].imsg_fd != -1)
474 return request_object(obj, id, repo, obj_fd);
476 err = start_read_object_child(repo);
477 if (err) {
478 close(obj_fd);
479 return err;
482 return request_object(obj, id, repo, obj_fd);
485 static const struct got_error *
486 read_object_raw_privsep(uint8_t **outbuf, off_t *size, size_t *hdrlen,
487 int outfd, struct got_object_id *id, struct got_repository *repo,
488 int obj_fd)
490 const struct got_error *err;
492 if (repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_OBJECT].imsg_fd != -1)
493 return request_raw_object(outbuf, size, hdrlen, outfd, id,
494 repo, obj_fd);
496 err = start_read_object_child(repo);
497 if (err)
498 return err;
500 return request_raw_object(outbuf, size, hdrlen, outfd, id, repo,
501 obj_fd);
504 const struct got_error *
505 got_object_open(struct got_object **obj, struct got_repository *repo,
506 struct got_object_id *id)
508 const struct got_error *err = NULL;
509 int fd;
511 *obj = got_repo_get_cached_object(repo, id);
512 if (*obj != NULL) {
513 (*obj)->refcnt++;
514 return NULL;
517 err = got_object_open_packed(obj, id, repo);
518 if (err && err->code != GOT_ERR_NO_OBJ)
519 return err;
520 if (*obj) {
521 (*obj)->refcnt++;
522 return got_repo_cache_object(repo, id, *obj);
525 err = got_object_open_loose_fd(&fd, id, repo);
526 if (err) {
527 if (err->code == GOT_ERR_ERRNO && errno == ENOENT)
528 err = got_error_no_obj(id);
529 return err;
532 err = got_object_read_header_privsep(obj, id, repo, fd);
533 if (err)
534 return err;
536 memcpy((*obj)->id.sha1, id->sha1, SHA1_DIGEST_LENGTH);
538 (*obj)->refcnt++;
539 return got_repo_cache_object(repo, id, *obj);
542 /* *outfd must be initialized to -1 by caller */
543 const struct got_error *
544 got_object_raw_open(struct got_raw_object **obj, int *outfd,
545 struct got_repository *repo, struct got_object_id *id)
547 const struct got_error *err = NULL;
548 struct got_packidx *packidx = NULL;
549 int idx;
550 uint8_t *outbuf = NULL;
551 off_t size = 0;
552 size_t hdrlen = 0;
553 char *path_packfile = NULL;
555 *obj = got_repo_get_cached_raw_object(repo, id);
556 if (*obj != NULL) {
557 (*obj)->refcnt++;
558 return NULL;
561 if (*outfd == -1) {
562 *outfd = got_opentempfd();
563 if (*outfd == -1)
564 return got_error_from_errno("got_opentempfd");
567 err = got_repo_search_packidx(&packidx, &idx, repo, id);
568 if (err == NULL) {
569 struct got_pack *pack = NULL;
571 err = got_packidx_get_packfile_path(&path_packfile,
572 packidx->path_packidx);
573 if (err)
574 goto done;
576 pack = got_repo_get_cached_pack(repo, path_packfile);
577 if (pack == NULL) {
578 err = got_repo_cache_pack(&pack, repo, path_packfile,
579 packidx);
580 if (err)
581 goto done;
583 err = read_packed_object_raw_privsep(&outbuf, &size, &hdrlen,
584 *outfd, pack, packidx, idx, id);
585 if (err)
586 goto done;
587 } else if (err->code == GOT_ERR_NO_OBJ) {
588 int fd;
590 err = got_object_open_loose_fd(&fd, id, repo);
591 if (err)
592 goto done;
593 err = read_object_raw_privsep(&outbuf, &size, &hdrlen, *outfd,
594 id, repo, fd);
595 if (err)
596 goto done;
599 *obj = calloc(1, sizeof(**obj));
600 if (*obj == NULL) {
601 err = got_error_from_errno("calloc");
602 goto done;
604 (*obj)->fd = -1;
606 if (outbuf) {
607 (*obj)->data = outbuf;
608 } else {
609 struct stat sb;
610 if (fstat(*outfd, &sb) == -1) {
611 err = got_error_from_errno("fstat");
612 goto done;
615 if (sb.st_size != hdrlen + size) {
616 err = got_error(GOT_ERR_PRIVSEP_LEN);
617 goto done;
619 #ifndef GOT_PACK_NO_MMAP
620 if (hdrlen + size > 0) {
621 (*obj)->data = mmap(NULL, hdrlen + size, PROT_READ,
622 MAP_PRIVATE, *outfd, 0);
623 if ((*obj)->data == MAP_FAILED) {
624 if (errno != ENOMEM) {
625 err = got_error_from_errno("mmap");
626 goto done;
628 (*obj)->data = NULL;
629 } else {
630 (*obj)->fd = *outfd;
631 *outfd = -1;
634 #endif
635 if (*outfd != -1) {
636 (*obj)->f = fdopen(*outfd, "r");
637 if ((*obj)->f == NULL) {
638 err = got_error_from_errno("fdopen");
639 goto done;
641 *outfd = -1;
644 (*obj)->hdrlen = hdrlen;
645 (*obj)->size = size;
646 err = got_repo_cache_raw_object(repo, id, *obj);
647 done:
648 free(path_packfile);
649 if (err) {
650 if (*obj) {
651 got_object_raw_close(*obj);
652 *obj = NULL;
654 free(outbuf);
655 } else
656 (*obj)->refcnt++;
657 return err;
660 const struct got_error *
661 got_object_open_by_id_str(struct got_object **obj, struct got_repository *repo,
662 const char *id_str)
664 struct got_object_id id;
666 if (!got_parse_sha1_digest(id.sha1, id_str))
667 return got_error_path(id_str, GOT_ERR_BAD_OBJ_ID_STR);
669 return got_object_open(obj, repo, &id);
672 const struct got_error *
673 got_object_resolve_id_str(struct got_object_id **id,
674 struct got_repository *repo, const char *id_str)
676 const struct got_error *err = NULL;
677 struct got_object *obj;
679 err = got_object_open_by_id_str(&obj, repo, id_str);
680 if (err)
681 return err;
683 *id = got_object_id_dup(got_object_get_id(obj));
684 got_object_close(obj);
685 if (*id == NULL)
686 return got_error_from_errno("got_object_id_dup");
688 return NULL;
691 static const struct got_error *
692 request_packed_commit(struct got_commit_object **commit, struct got_pack *pack,
693 int pack_idx, struct got_object_id *id)
695 const struct got_error *err = NULL;
697 err = got_privsep_send_commit_req(pack->privsep_child->ibuf, -1, id,
698 pack_idx);
699 if (err)
700 return err;
702 err = got_privsep_recv_commit(commit, pack->privsep_child->ibuf);
703 if (err)
704 return err;
706 (*commit)->flags |= GOT_COMMIT_FLAG_PACKED;
707 return NULL;
710 static const struct got_error *
711 read_packed_commit_privsep(struct got_commit_object **commit,
712 struct got_pack *pack, struct got_packidx *packidx, int idx,
713 struct got_object_id *id)
715 const struct got_error *err = NULL;
717 if (pack->privsep_child)
718 return request_packed_commit(commit, pack, idx, id);
720 err = got_pack_start_privsep_child(pack, packidx);
721 if (err)
722 return err;
724 return request_packed_commit(commit, pack, idx, id);
727 static const struct got_error *
728 request_commit(struct got_commit_object **commit, struct got_repository *repo,
729 int fd, struct got_object_id *id)
731 const struct got_error *err = NULL;
732 struct imsgbuf *ibuf;
734 ibuf = repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_COMMIT].ibuf;
736 err = got_privsep_send_commit_req(ibuf, fd, id, -1);
737 if (err)
738 return err;
740 return got_privsep_recv_commit(commit, ibuf);
743 static const struct got_error *
744 read_commit_privsep(struct got_commit_object **commit, int obj_fd,
745 struct got_object_id *id, struct got_repository *repo)
747 const struct got_error *err;
748 int imsg_fds[2];
749 pid_t pid;
750 struct imsgbuf *ibuf;
752 if (repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_COMMIT].imsg_fd != -1)
753 return request_commit(commit, repo, obj_fd, id);
755 ibuf = calloc(1, sizeof(*ibuf));
756 if (ibuf == NULL)
757 return got_error_from_errno("calloc");
759 if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_fds) == -1) {
760 err = got_error_from_errno("socketpair");
761 free(ibuf);
762 return err;
765 pid = fork();
766 if (pid == -1) {
767 err = got_error_from_errno("fork");
768 free(ibuf);
769 return err;
771 else if (pid == 0) {
772 got_privsep_exec_child(imsg_fds, GOT_PATH_PROG_READ_COMMIT,
773 repo->path);
774 /* not reached */
777 if (close(imsg_fds[1]) == -1) {
778 err = got_error_from_errno("close");
779 free(ibuf);
780 return err;
782 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_COMMIT].imsg_fd =
783 imsg_fds[0];
784 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_COMMIT].pid = pid;
785 imsg_init(ibuf, imsg_fds[0]);
786 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_COMMIT].ibuf = ibuf;
788 return request_commit(commit, repo, obj_fd, id);
792 static const struct got_error *
793 open_commit(struct got_commit_object **commit,
794 struct got_repository *repo, struct got_object_id *id, int check_cache)
796 const struct got_error *err = NULL;
797 struct got_packidx *packidx = NULL;
798 int idx;
799 char *path_packfile = NULL;
801 if (check_cache) {
802 *commit = got_repo_get_cached_commit(repo, id);
803 if (*commit != NULL) {
804 (*commit)->refcnt++;
805 return NULL;
807 } else
808 *commit = NULL;
810 err = got_repo_search_packidx(&packidx, &idx, repo, id);
811 if (err == NULL) {
812 struct got_pack *pack = NULL;
814 err = got_packidx_get_packfile_path(&path_packfile,
815 packidx->path_packidx);
816 if (err)
817 return err;
819 pack = got_repo_get_cached_pack(repo, path_packfile);
820 if (pack == NULL) {
821 err = got_repo_cache_pack(&pack, repo, path_packfile,
822 packidx);
823 if (err)
824 goto done;
826 err = read_packed_commit_privsep(commit, pack,
827 packidx, idx, id);
828 } else if (err->code == GOT_ERR_NO_OBJ) {
829 int fd;
831 err = got_object_open_loose_fd(&fd, id, repo);
832 if (err)
833 return err;
834 err = read_commit_privsep(commit, fd, id, repo);
837 if (err == NULL) {
838 (*commit)->refcnt++;
839 err = got_repo_cache_commit(repo, id, *commit);
841 done:
842 free(path_packfile);
843 return err;
846 const struct got_error *
847 got_object_open_as_commit(struct got_commit_object **commit,
848 struct got_repository *repo, struct got_object_id *id)
850 *commit = got_repo_get_cached_commit(repo, id);
851 if (*commit != NULL) {
852 (*commit)->refcnt++;
853 return NULL;
856 return open_commit(commit, repo, id, 0);
859 const struct got_error *
860 got_object_commit_open(struct got_commit_object **commit,
861 struct got_repository *repo, struct got_object *obj)
863 return open_commit(commit, repo, got_object_get_id(obj), 1);
866 const struct got_error *
867 got_object_qid_alloc(struct got_object_qid **qid, struct got_object_id *id)
869 *qid = calloc(1, sizeof(**qid));
870 if (*qid == NULL)
871 return got_error_from_errno("calloc");
873 memcpy(&(*qid)->id, id, sizeof((*qid)->id));
874 return NULL;
877 const struct got_error *
878 got_object_id_queue_copy(const struct got_object_id_queue *src,
879 struct got_object_id_queue *dest)
881 const struct got_error *err;
882 struct got_object_qid *qid;
884 STAILQ_FOREACH(qid, src, entry) {
885 struct got_object_qid *new;
886 /*
887 * Deep-copy the object ID only. Let the caller deal
888 * with setting up the new->data pointer if needed.
889 */
890 err = got_object_qid_alloc(&new, &qid->id);
891 if (err) {
892 got_object_id_queue_free(dest);
893 return err;
895 STAILQ_INSERT_TAIL(dest, new, entry);
898 return NULL;
901 static const struct got_error *
902 request_packed_tree(struct got_tree_object **tree, struct got_pack *pack,
903 int pack_idx, struct got_object_id *id)
905 const struct got_error *err = NULL;
907 err = got_privsep_send_tree_req(pack->privsep_child->ibuf, -1, id,
908 pack_idx);
909 if (err)
910 return err;
912 return got_privsep_recv_tree(tree, pack->privsep_child->ibuf);
915 static const struct got_error *
916 read_packed_tree_privsep(struct got_tree_object **tree,
917 struct got_pack *pack, struct got_packidx *packidx, int idx,
918 struct got_object_id *id)
920 const struct got_error *err = NULL;
922 if (pack->privsep_child)
923 return request_packed_tree(tree, pack, idx, id);
925 err = got_pack_start_privsep_child(pack, packidx);
926 if (err)
927 return err;
929 return request_packed_tree(tree, pack, idx, id);
932 static const struct got_error *
933 request_tree(struct got_tree_object **tree, struct got_repository *repo,
934 int fd, struct got_object_id *id)
936 const struct got_error *err = NULL;
937 struct imsgbuf *ibuf;
939 ibuf = repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TREE].ibuf;
941 err = got_privsep_send_tree_req(ibuf, fd, id, -1);
942 if (err)
943 return err;
945 return got_privsep_recv_tree(tree, ibuf);
948 static const struct got_error *
949 read_tree_privsep(struct got_tree_object **tree, int obj_fd,
950 struct got_object_id *id, struct got_repository *repo)
952 const struct got_error *err;
953 int imsg_fds[2];
954 pid_t pid;
955 struct imsgbuf *ibuf;
957 if (repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TREE].imsg_fd != -1)
958 return request_tree(tree, repo, obj_fd, id);
960 ibuf = calloc(1, sizeof(*ibuf));
961 if (ibuf == NULL)
962 return got_error_from_errno("calloc");
964 if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_fds) == -1) {
965 err = got_error_from_errno("socketpair");
966 free(ibuf);
967 return err;
970 pid = fork();
971 if (pid == -1) {
972 err = got_error_from_errno("fork");
973 free(ibuf);
974 return err;
976 else if (pid == 0) {
977 got_privsep_exec_child(imsg_fds, GOT_PATH_PROG_READ_TREE,
978 repo->path);
979 /* not reached */
982 if (close(imsg_fds[1]) == -1) {
983 err = got_error_from_errno("close");
984 free(ibuf);
985 return err;
987 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TREE].imsg_fd =
988 imsg_fds[0];
989 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TREE].pid = pid;
990 imsg_init(ibuf, imsg_fds[0]);
991 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TREE].ibuf = ibuf;
994 return request_tree(tree, repo, obj_fd, id);
997 static const struct got_error *
998 open_tree(struct got_tree_object **tree, struct got_repository *repo,
999 struct got_object_id *id, int check_cache)
1001 const struct got_error *err = NULL;
1002 struct got_packidx *packidx = NULL;
1003 int idx;
1004 char *path_packfile = NULL;
1006 if (check_cache) {
1007 *tree = got_repo_get_cached_tree(repo, id);
1008 if (*tree != NULL) {
1009 (*tree)->refcnt++;
1010 return NULL;
1012 } else
1013 *tree = NULL;
1015 err = got_repo_search_packidx(&packidx, &idx, repo, id);
1016 if (err == NULL) {
1017 struct got_pack *pack = NULL;
1019 err = got_packidx_get_packfile_path(&path_packfile,
1020 packidx->path_packidx);
1021 if (err)
1022 return err;
1024 pack = got_repo_get_cached_pack(repo, path_packfile);
1025 if (pack == NULL) {
1026 err = got_repo_cache_pack(&pack, repo, path_packfile,
1027 packidx);
1028 if (err)
1029 goto done;
1031 err = read_packed_tree_privsep(tree, pack,
1032 packidx, idx, id);
1033 } else if (err->code == GOT_ERR_NO_OBJ) {
1034 int fd;
1036 err = got_object_open_loose_fd(&fd, id, repo);
1037 if (err)
1038 return err;
1039 err = read_tree_privsep(tree, fd, id, repo);
1042 if (err == NULL) {
1043 (*tree)->refcnt++;
1044 err = got_repo_cache_tree(repo, id, *tree);
1046 done:
1047 free(path_packfile);
1048 return err;
1051 const struct got_error *
1052 got_object_open_as_tree(struct got_tree_object **tree,
1053 struct got_repository *repo, struct got_object_id *id)
1055 *tree = got_repo_get_cached_tree(repo, id);
1056 if (*tree != NULL) {
1057 (*tree)->refcnt++;
1058 return NULL;
1061 return open_tree(tree, repo, id, 0);
1064 const struct got_error *
1065 got_object_tree_open(struct got_tree_object **tree,
1066 struct got_repository *repo, struct got_object *obj)
1068 return open_tree(tree, repo, got_object_get_id(obj), 1);
1071 int
1072 got_object_tree_get_nentries(struct got_tree_object *tree)
1074 return tree->nentries;
1077 struct got_tree_entry *
1078 got_object_tree_get_first_entry(struct got_tree_object *tree)
1080 return got_object_tree_get_entry(tree, 0);
1083 struct got_tree_entry *
1084 got_object_tree_get_last_entry(struct got_tree_object *tree)
1086 return got_object_tree_get_entry(tree, tree->nentries - 1);
1089 struct got_tree_entry *
1090 got_object_tree_get_entry(struct got_tree_object *tree, int i)
1092 if (i < 0 || i >= tree->nentries)
1093 return NULL;
1094 return &tree->entries[i];
1097 mode_t
1098 got_tree_entry_get_mode(struct got_tree_entry *te)
1100 return te->mode;
1103 const char *
1104 got_tree_entry_get_name(struct got_tree_entry *te)
1106 return &te->name[0];
1109 struct got_object_id *
1110 got_tree_entry_get_id(struct got_tree_entry *te)
1112 return &te->id;
1115 const struct got_error *
1116 got_object_blob_read_to_str(char **s, struct got_blob_object *blob)
1118 const struct got_error *err = NULL;
1119 size_t len, totlen, hdrlen, offset;
1121 *s = NULL;
1123 hdrlen = got_object_blob_get_hdrlen(blob);
1124 totlen = 0;
1125 offset = 0;
1126 do {
1127 char *p;
1129 err = got_object_blob_read_block(&len, blob);
1130 if (err)
1131 return err;
1133 if (len == 0)
1134 break;
1136 totlen += len - hdrlen;
1137 p = realloc(*s, totlen + 1);
1138 if (p == NULL) {
1139 err = got_error_from_errno("realloc");
1140 free(*s);
1141 *s = NULL;
1142 return err;
1144 *s = p;
1145 /* Skip blob object header first time around. */
1146 memcpy(*s + offset,
1147 got_object_blob_get_read_buf(blob) + hdrlen, len - hdrlen);
1148 hdrlen = 0;
1149 offset = totlen;
1150 } while (len > 0);
1152 (*s)[totlen] = '\0';
1153 return NULL;
1156 const struct got_error *
1157 got_tree_entry_get_symlink_target(char **link_target, struct got_tree_entry *te,
1158 struct got_repository *repo)
1160 const struct got_error *err = NULL;
1161 struct got_blob_object *blob = NULL;
1162 int fd = -1;
1164 *link_target = NULL;
1166 if (!got_object_tree_entry_is_symlink(te))
1167 return got_error(GOT_ERR_TREE_ENTRY_TYPE);
1169 fd = got_opentempfd();
1170 if (fd == -1) {
1171 err = got_error_from_errno("got_opentempfd");
1172 goto done;
1175 err = got_object_open_as_blob(&blob, repo,
1176 got_tree_entry_get_id(te), PATH_MAX, fd);
1177 if (err)
1178 goto done;
1180 err = got_object_blob_read_to_str(link_target, blob);
1181 done:
1182 if (fd != -1 && close(fd) == -1 && err == NULL)
1183 err = got_error_from_errno("close");
1184 if (blob)
1185 got_object_blob_close(blob);
1186 if (err) {
1187 free(*link_target);
1188 *link_target = NULL;
1190 return err;
1193 int
1194 got_tree_entry_get_index(struct got_tree_entry *te)
1196 return te->idx;
1199 struct got_tree_entry *
1200 got_tree_entry_get_next(struct got_tree_object *tree,
1201 struct got_tree_entry *te)
1203 return got_object_tree_get_entry(tree, te->idx + 1);
1206 struct got_tree_entry *
1207 got_tree_entry_get_prev(struct got_tree_object *tree,
1208 struct got_tree_entry *te)
1210 return got_object_tree_get_entry(tree, te->idx - 1);
1213 static const struct got_error *
1214 request_packed_blob(uint8_t **outbuf, size_t *size, size_t *hdrlen, int outfd,
1215 struct got_pack *pack, struct got_packidx *packidx, int idx,
1216 struct got_object_id *id)
1218 const struct got_error *err = NULL;
1219 struct imsgbuf *ibuf = pack->privsep_child->ibuf;
1220 int outfd_child;
1222 err = pack_child_send_tempfiles(ibuf, pack);
1223 if (err)
1224 return err;
1226 outfd_child = dup(outfd);
1227 if (outfd_child == -1)
1228 return got_error_from_errno("dup");
1230 err = got_privsep_send_blob_req(pack->privsep_child->ibuf, -1, id, idx);
1231 if (err)
1232 return err;
1234 err = got_privsep_send_blob_outfd(pack->privsep_child->ibuf,
1235 outfd_child);
1236 if (err) {
1237 return err;
1240 err = got_privsep_recv_blob(outbuf, size, hdrlen,
1241 pack->privsep_child->ibuf);
1242 if (err)
1243 return err;
1245 if (lseek(outfd, SEEK_SET, 0) == -1)
1246 err = got_error_from_errno("lseek");
1248 return err;
1251 static const struct got_error *
1252 read_packed_blob_privsep(uint8_t **outbuf, size_t *size, size_t *hdrlen,
1253 int outfd, struct got_pack *pack, struct got_packidx *packidx, int idx,
1254 struct got_object_id *id)
1256 const struct got_error *err = NULL;
1258 if (pack->privsep_child == NULL) {
1259 err = got_pack_start_privsep_child(pack, packidx);
1260 if (err)
1261 return err;
1264 return request_packed_blob(outbuf, size, hdrlen, outfd, pack, packidx,
1265 idx, id);
1268 static const struct got_error *
1269 request_blob(uint8_t **outbuf, size_t *size, size_t *hdrlen, int outfd,
1270 int infd, struct got_object_id *id, struct imsgbuf *ibuf)
1272 const struct got_error *err = NULL;
1273 int outfd_child;
1275 outfd_child = dup(outfd);
1276 if (outfd_child == -1)
1277 return got_error_from_errno("dup");
1279 err = got_privsep_send_blob_req(ibuf, infd, id, -1);
1280 if (err)
1281 return err;
1283 err = got_privsep_send_blob_outfd(ibuf, outfd_child);
1284 if (err)
1285 return err;
1287 err = got_privsep_recv_blob(outbuf, size, hdrlen, ibuf);
1288 if (err)
1289 return err;
1291 if (lseek(outfd, SEEK_SET, 0) == -1)
1292 return got_error_from_errno("lseek");
1294 return err;
1297 static const struct got_error *
1298 read_blob_privsep(uint8_t **outbuf, size_t *size, size_t *hdrlen,
1299 int outfd, int infd, struct got_object_id *id, struct got_repository *repo)
1301 const struct got_error *err;
1302 int imsg_fds[2];
1303 pid_t pid;
1304 struct imsgbuf *ibuf;
1306 if (repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_BLOB].imsg_fd != -1) {
1307 ibuf = repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_BLOB].ibuf;
1308 return request_blob(outbuf, size, hdrlen, outfd, infd, id,
1309 ibuf);
1312 ibuf = calloc(1, sizeof(*ibuf));
1313 if (ibuf == NULL)
1314 return got_error_from_errno("calloc");
1316 if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_fds) == -1) {
1317 err = got_error_from_errno("socketpair");
1318 free(ibuf);
1319 return err;
1322 pid = fork();
1323 if (pid == -1) {
1324 err = got_error_from_errno("fork");
1325 free(ibuf);
1326 return err;
1328 else if (pid == 0) {
1329 got_privsep_exec_child(imsg_fds, GOT_PATH_PROG_READ_BLOB,
1330 repo->path);
1331 /* not reached */
1334 if (close(imsg_fds[1]) == -1) {
1335 err = got_error_from_errno("close");
1336 free(ibuf);
1337 return err;
1339 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_BLOB].imsg_fd =
1340 imsg_fds[0];
1341 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_BLOB].pid = pid;
1342 imsg_init(ibuf, imsg_fds[0]);
1343 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_BLOB].ibuf = ibuf;
1345 return request_blob(outbuf, size, hdrlen, outfd, infd, id, ibuf);
1348 static const struct got_error *
1349 open_blob(struct got_blob_object **blob, struct got_repository *repo,
1350 struct got_object_id *id, size_t blocksize, int outfd)
1352 const struct got_error *err = NULL;
1353 struct got_packidx *packidx = NULL;
1354 int idx, dfd = -1;
1355 char *path_packfile = NULL;
1356 uint8_t *outbuf;
1357 size_t size, hdrlen;
1358 struct stat sb;
1360 *blob = calloc(1, sizeof(**blob));
1361 if (*blob == NULL)
1362 return got_error_from_errno("calloc");
1364 (*blob)->read_buf = malloc(blocksize);
1365 if ((*blob)->read_buf == NULL) {
1366 err = got_error_from_errno("malloc");
1367 goto done;
1370 if (ftruncate(outfd, 0L) == -1) {
1371 err = got_error_from_errno("ftruncate");
1372 goto done;
1374 if (lseek(outfd, SEEK_SET, 0) == -1) {
1375 err = got_error_from_errno("lseek");
1376 goto done;
1379 err = got_repo_search_packidx(&packidx, &idx, repo, id);
1380 if (err == NULL) {
1381 struct got_pack *pack = NULL;
1383 err = got_packidx_get_packfile_path(&path_packfile,
1384 packidx->path_packidx);
1385 if (err)
1386 goto done;
1388 pack = got_repo_get_cached_pack(repo, path_packfile);
1389 if (pack == NULL) {
1390 err = got_repo_cache_pack(&pack, repo, path_packfile,
1391 packidx);
1392 if (err)
1393 goto done;
1395 err = read_packed_blob_privsep(&outbuf, &size, &hdrlen, outfd,
1396 pack, packidx, idx, id);
1397 } else if (err->code == GOT_ERR_NO_OBJ) {
1398 int infd;
1400 err = got_object_open_loose_fd(&infd, id, repo);
1401 if (err)
1402 goto done;
1403 err = read_blob_privsep(&outbuf, &size, &hdrlen, outfd, infd,
1404 id, repo);
1406 if (err)
1407 goto done;
1409 if (hdrlen > size) {
1410 err = got_error(GOT_ERR_BAD_OBJ_HDR);
1411 goto done;
1414 if (outbuf) {
1415 (*blob)->f = fmemopen(outbuf, size, "rb");
1416 if ((*blob)->f == NULL) {
1417 err = got_error_from_errno("fmemopen");
1418 free(outbuf);
1419 goto done;
1421 (*blob)->data = outbuf;
1422 } else {
1423 if (fstat(outfd, &sb) == -1) {
1424 err = got_error_from_errno("fstat");
1425 goto done;
1428 if (sb.st_size != size) {
1429 err = got_error(GOT_ERR_PRIVSEP_LEN);
1430 goto done;
1433 dfd = dup(outfd);
1434 if (dfd == -1) {
1435 err = got_error_from_errno("dup");
1436 goto done;
1439 (*blob)->f = fdopen(dfd, "rb");
1440 if ((*blob)->f == NULL) {
1441 err = got_error_from_errno("fdopen");
1442 close(dfd);
1443 dfd = -1;
1444 goto done;
1448 (*blob)->hdrlen = hdrlen;
1449 (*blob)->blocksize = blocksize;
1450 memcpy(&(*blob)->id.sha1, id->sha1, SHA1_DIGEST_LENGTH);
1452 done:
1453 free(path_packfile);
1454 if (err) {
1455 if (*blob) {
1456 got_object_blob_close(*blob);
1457 *blob = NULL;
1460 return err;
1463 const struct got_error *
1464 got_object_open_as_blob(struct got_blob_object **blob,
1465 struct got_repository *repo, struct got_object_id *id, size_t blocksize,
1466 int outfd)
1468 return open_blob(blob, repo, id, blocksize, outfd);
1471 const struct got_error *
1472 got_object_blob_open(struct got_blob_object **blob,
1473 struct got_repository *repo, struct got_object *obj, size_t blocksize,
1474 int outfd)
1476 return open_blob(blob, repo, got_object_get_id(obj), blocksize, outfd);
1479 const struct got_error *
1480 got_object_blob_close(struct got_blob_object *blob)
1482 const struct got_error *err = NULL;
1483 free(blob->read_buf);
1484 if (blob->f && fclose(blob->f) == EOF)
1485 err = got_error_from_errno("fclose");
1486 free(blob->data);
1487 free(blob);
1488 return err;
1491 void
1492 got_object_blob_rewind(struct got_blob_object *blob)
1494 if (blob->f)
1495 rewind(blob->f);
1498 char *
1499 got_object_blob_id_str(struct got_blob_object *blob, char *buf, size_t size)
1501 return got_sha1_digest_to_str(blob->id.sha1, buf, size);
1504 size_t
1505 got_object_blob_get_hdrlen(struct got_blob_object *blob)
1507 return blob->hdrlen;
1510 const uint8_t *
1511 got_object_blob_get_read_buf(struct got_blob_object *blob)
1513 return blob->read_buf;
1516 const struct got_error *
1517 got_object_blob_read_block(size_t *outlenp, struct got_blob_object *blob)
1519 size_t n;
1521 n = fread(blob->read_buf, 1, blob->blocksize, blob->f);
1522 if (n == 0 && ferror(blob->f))
1523 return got_ferror(blob->f, GOT_ERR_IO);
1524 *outlenp = n;
1525 return NULL;
1528 const struct got_error *
1529 got_object_blob_dump_to_file(off_t *filesize, int *nlines,
1530 off_t **line_offsets, FILE *outfile, struct got_blob_object *blob)
1532 const struct got_error *err = NULL;
1533 size_t n, len, hdrlen;
1534 const uint8_t *buf;
1535 int i;
1536 const int alloc_chunksz = 512;
1537 size_t nalloc = 0;
1538 off_t off = 0, total_len = 0;
1540 if (line_offsets)
1541 *line_offsets = NULL;
1542 if (filesize)
1543 *filesize = 0;
1544 if (nlines)
1545 *nlines = 0;
1547 hdrlen = got_object_blob_get_hdrlen(blob);
1548 do {
1549 err = got_object_blob_read_block(&len, blob);
1550 if (err)
1551 return err;
1552 if (len == 0)
1553 break;
1554 buf = got_object_blob_get_read_buf(blob);
1555 i = hdrlen;
1556 if (nlines) {
1557 if (line_offsets && *line_offsets == NULL) {
1558 /* Have some data but perhaps no '\n'. */
1559 *nlines = 1;
1560 nalloc = alloc_chunksz;
1561 *line_offsets = calloc(nalloc,
1562 sizeof(**line_offsets));
1563 if (*line_offsets == NULL)
1564 return got_error_from_errno("calloc");
1566 /* Skip forward over end of first line. */
1567 while (i < len) {
1568 if (buf[i] == '\n')
1569 break;
1570 i++;
1573 /* Scan '\n' offsets in remaining chunk of data. */
1574 while (i < len) {
1575 if (buf[i] != '\n') {
1576 i++;
1577 continue;
1579 (*nlines)++;
1580 if (line_offsets && nalloc < *nlines) {
1581 size_t n = *nlines + alloc_chunksz;
1582 off_t *o = recallocarray(*line_offsets,
1583 nalloc, n, sizeof(**line_offsets));
1584 if (o == NULL) {
1585 free(*line_offsets);
1586 *line_offsets = NULL;
1587 return got_error_from_errno(
1588 "recallocarray");
1590 *line_offsets = o;
1591 nalloc = n;
1593 if (line_offsets) {
1594 off = total_len + i - hdrlen + 1;
1595 (*line_offsets)[*nlines - 1] = off;
1597 i++;
1600 /* Skip blob object header first time around. */
1601 n = fwrite(buf + hdrlen, 1, len - hdrlen, outfile);
1602 if (n != len - hdrlen)
1603 return got_ferror(outfile, GOT_ERR_IO);
1604 total_len += len - hdrlen;
1605 hdrlen = 0;
1606 } while (len != 0);
1608 if (fflush(outfile) != 0)
1609 return got_error_from_errno("fflush");
1610 rewind(outfile);
1612 if (filesize)
1613 *filesize = total_len;
1615 return NULL;
1618 static const struct got_error *
1619 request_packed_tag(struct got_tag_object **tag, struct got_pack *pack,
1620 int pack_idx, struct got_object_id *id)
1622 const struct got_error *err = NULL;
1624 err = got_privsep_send_tag_req(pack->privsep_child->ibuf, -1, id,
1625 pack_idx);
1626 if (err)
1627 return err;
1629 return got_privsep_recv_tag(tag, pack->privsep_child->ibuf);
1632 static const struct got_error *
1633 read_packed_tag_privsep(struct got_tag_object **tag,
1634 struct got_pack *pack, struct got_packidx *packidx, int idx,
1635 struct got_object_id *id)
1637 const struct got_error *err = NULL;
1639 if (pack->privsep_child)
1640 return request_packed_tag(tag, pack, idx, id);
1642 err = got_pack_start_privsep_child(pack, packidx);
1643 if (err)
1644 return err;
1646 return request_packed_tag(tag, pack, idx, id);
1649 static const struct got_error *
1650 request_tag(struct got_tag_object **tag, struct got_repository *repo,
1651 int fd, struct got_object_id *id)
1653 const struct got_error *err = NULL;
1654 struct imsgbuf *ibuf;
1656 ibuf = repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TAG].ibuf;
1658 err = got_privsep_send_tag_req(ibuf, fd, id, -1);
1659 if (err)
1660 return err;
1662 return got_privsep_recv_tag(tag, ibuf);
1665 static const struct got_error *
1666 read_tag_privsep(struct got_tag_object **tag, int obj_fd,
1667 struct got_object_id *id, struct got_repository *repo)
1669 const struct got_error *err;
1670 int imsg_fds[2];
1671 pid_t pid;
1672 struct imsgbuf *ibuf;
1674 if (repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TAG].imsg_fd != -1)
1675 return request_tag(tag, repo, obj_fd, id);
1677 ibuf = calloc(1, sizeof(*ibuf));
1678 if (ibuf == NULL)
1679 return got_error_from_errno("calloc");
1681 if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_fds) == -1) {
1682 err = got_error_from_errno("socketpair");
1683 free(ibuf);
1684 return err;
1687 pid = fork();
1688 if (pid == -1) {
1689 err = got_error_from_errno("fork");
1690 free(ibuf);
1691 return err;
1693 else if (pid == 0) {
1694 got_privsep_exec_child(imsg_fds, GOT_PATH_PROG_READ_TAG,
1695 repo->path);
1696 /* not reached */
1699 if (close(imsg_fds[1]) == -1) {
1700 err = got_error_from_errno("close");
1701 free(ibuf);
1702 return err;
1704 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TAG].imsg_fd =
1705 imsg_fds[0];
1706 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TAG].pid = pid;
1707 imsg_init(ibuf, imsg_fds[0]);
1708 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TAG].ibuf = ibuf;
1710 return request_tag(tag, repo, obj_fd, id);
1713 static const struct got_error *
1714 open_tag(struct got_tag_object **tag, struct got_repository *repo,
1715 struct got_object_id *id, int check_cache)
1717 const struct got_error *err = NULL;
1718 struct got_packidx *packidx = NULL;
1719 int idx;
1720 char *path_packfile = NULL;
1721 struct got_object *obj = NULL;
1722 int obj_type = GOT_OBJ_TYPE_ANY;
1724 if (check_cache) {
1725 *tag = got_repo_get_cached_tag(repo, id);
1726 if (*tag != NULL) {
1727 (*tag)->refcnt++;
1728 return NULL;
1730 } else
1731 *tag = NULL;
1733 err = got_repo_search_packidx(&packidx, &idx, repo, id);
1734 if (err == NULL) {
1735 struct got_pack *pack = NULL;
1737 err = got_packidx_get_packfile_path(&path_packfile,
1738 packidx->path_packidx);
1739 if (err)
1740 return err;
1742 pack = got_repo_get_cached_pack(repo, path_packfile);
1743 if (pack == NULL) {
1744 err = got_repo_cache_pack(&pack, repo, path_packfile,
1745 packidx);
1746 if (err)
1747 goto done;
1750 /* Beware of "lightweight" tags: Check object type first. */
1751 err = read_packed_object_privsep(&obj, repo, pack, packidx,
1752 idx, id);
1753 if (err)
1754 goto done;
1755 obj_type = obj->type;
1756 got_object_close(obj);
1757 if (obj_type != GOT_OBJ_TYPE_TAG) {
1758 err = got_error(GOT_ERR_OBJ_TYPE);
1759 goto done;
1761 err = read_packed_tag_privsep(tag, pack, packidx, idx, id);
1762 } else if (err->code == GOT_ERR_NO_OBJ) {
1763 int fd;
1765 err = got_object_open_loose_fd(&fd, id, repo);
1766 if (err)
1767 return err;
1768 err = got_object_read_header_privsep(&obj, id, repo, fd);
1769 if (err)
1770 return err;
1771 obj_type = obj->type;
1772 got_object_close(obj);
1773 if (obj_type != GOT_OBJ_TYPE_TAG)
1774 return got_error(GOT_ERR_OBJ_TYPE);
1776 err = got_object_open_loose_fd(&fd, id, repo);
1777 if (err)
1778 return err;
1779 err = read_tag_privsep(tag, fd, id, repo);
1782 if (err == NULL) {
1783 (*tag)->refcnt++;
1784 err = got_repo_cache_tag(repo, id, *tag);
1786 done:
1787 free(path_packfile);
1788 return err;
1791 const struct got_error *
1792 got_object_open_as_tag(struct got_tag_object **tag,
1793 struct got_repository *repo, struct got_object_id *id)
1795 *tag = got_repo_get_cached_tag(repo, id);
1796 if (*tag != NULL) {
1797 (*tag)->refcnt++;
1798 return NULL;
1801 return open_tag(tag, repo, id, 0);
1804 const struct got_error *
1805 got_object_tag_open(struct got_tag_object **tag,
1806 struct got_repository *repo, struct got_object *obj)
1808 return open_tag(tag, repo, got_object_get_id(obj), 1);
1811 const char *
1812 got_object_tag_get_name(struct got_tag_object *tag)
1814 return tag->tag;
1817 int
1818 got_object_tag_get_object_type(struct got_tag_object *tag)
1820 return tag->obj_type;
1823 struct got_object_id *
1824 got_object_tag_get_object_id(struct got_tag_object *tag)
1826 return &tag->id;
1829 time_t
1830 got_object_tag_get_tagger_time(struct got_tag_object *tag)
1832 return tag->tagger_time;
1835 time_t
1836 got_object_tag_get_tagger_gmtoff(struct got_tag_object *tag)
1838 return tag->tagger_gmtoff;
1841 const char *
1842 got_object_tag_get_tagger(struct got_tag_object *tag)
1844 return tag->tagger;
1847 const char *
1848 got_object_tag_get_message(struct got_tag_object *tag)
1850 return tag->tagmsg;
1853 static struct got_tree_entry *
1854 find_entry_by_name(struct got_tree_object *tree, const char *name, size_t len)
1856 int i;
1858 /* Note that tree entries are sorted in strncmp() order. */
1859 for (i = 0; i < tree->nentries; i++) {
1860 struct got_tree_entry *te = &tree->entries[i];
1861 int cmp = strncmp(te->name, name, len);
1862 if (cmp < 0)
1863 continue;
1864 if (cmp > 0)
1865 break;
1866 if (te->name[len] == '\0')
1867 return te;
1869 return NULL;
1872 struct got_tree_entry *
1873 got_object_tree_find_entry(struct got_tree_object *tree, const char *name)
1875 return find_entry_by_name(tree, name, strlen(name));
1878 const struct got_error *
1879 got_object_tree_find_path(struct got_object_id **id, mode_t *mode,
1880 struct got_repository *repo, struct got_tree_object *tree,
1881 const char *path)
1883 const struct got_error *err = NULL;
1884 struct got_tree_object *subtree = NULL;
1885 struct got_tree_entry *te = NULL;
1886 const char *seg, *s;
1887 size_t seglen;
1889 *id = NULL;
1891 s = path;
1892 while (s[0] == '/')
1893 s++;
1894 seg = s;
1895 seglen = 0;
1896 subtree = tree;
1897 while (*s) {
1898 struct got_tree_object *next_tree;
1900 if (*s != '/') {
1901 s++;
1902 seglen++;
1903 if (*s)
1904 continue;
1907 te = find_entry_by_name(subtree, seg, seglen);
1908 if (te == NULL) {
1909 err = got_error_path(path, GOT_ERR_NO_TREE_ENTRY);
1910 goto done;
1913 if (*s == '\0')
1914 break;
1916 seg = s + 1;
1917 seglen = 0;
1918 s++;
1919 if (*s) {
1920 err = got_object_open_as_tree(&next_tree, repo,
1921 &te->id);
1922 te = NULL;
1923 if (err)
1924 goto done;
1925 if (subtree != tree)
1926 got_object_tree_close(subtree);
1927 subtree = next_tree;
1931 if (te) {
1932 *id = got_object_id_dup(&te->id);
1933 if (*id == NULL)
1934 return got_error_from_errno("got_object_id_dup");
1935 if (mode)
1936 *mode = te->mode;
1937 } else
1938 err = got_error_path(path, GOT_ERR_NO_TREE_ENTRY);
1939 done:
1940 if (subtree && subtree != tree)
1941 got_object_tree_close(subtree);
1942 return err;
1944 const struct got_error *
1945 got_object_id_by_path(struct got_object_id **id, struct got_repository *repo,
1946 struct got_commit_object *commit, const char *path)
1948 const struct got_error *err = NULL;
1949 struct got_tree_object *tree = NULL;
1951 *id = NULL;
1953 /* Handle opening of root of commit's tree. */
1954 if (got_path_is_root_dir(path)) {
1955 *id = got_object_id_dup(commit->tree_id);
1956 if (*id == NULL)
1957 err = got_error_from_errno("got_object_id_dup");
1958 } else {
1959 err = got_object_open_as_tree(&tree, repo, commit->tree_id);
1960 if (err)
1961 goto done;
1962 err = got_object_tree_find_path(id, NULL, repo, tree, path);
1964 done:
1965 if (tree)
1966 got_object_tree_close(tree);
1967 return err;
1971 * Normalize file mode bits to avoid false positive tree entry differences
1972 * in case tree entries have unexpected mode bits set.
1974 static mode_t
1975 normalize_mode_for_comparison(mode_t mode)
1978 * For directories, the only relevant bit is the IFDIR bit.
1979 * This allows us to detect paths changing from a directory
1980 * to a file and vice versa.
1982 if (S_ISDIR(mode))
1983 return mode & S_IFDIR;
1986 * For symlinks, the only relevant bit is the IFLNK bit.
1987 * This allows us to detect paths changing from a symlinks
1988 * to a file or directory and vice versa.
1990 if (S_ISLNK(mode))
1991 return mode & S_IFLNK;
1993 /* For files, the only change we care about is the executable bit. */
1994 return mode & S_IXUSR;
1997 const struct got_error *
1998 got_object_tree_path_changed(int *changed,
1999 struct got_tree_object *tree01, struct got_tree_object *tree02,
2000 const char *path, struct got_repository *repo)
2002 const struct got_error *err = NULL;
2003 struct got_tree_object *tree1 = NULL, *tree2 = NULL;
2004 struct got_tree_entry *te1 = NULL, *te2 = NULL;
2005 const char *seg, *s;
2006 size_t seglen;
2008 *changed = 0;
2010 /* We not do support comparing the root path. */
2011 if (got_path_is_root_dir(path))
2012 return got_error_path(path, GOT_ERR_BAD_PATH);
2014 tree1 = tree01;
2015 tree2 = tree02;
2016 s = path;
2017 while (*s == '/')
2018 s++;
2019 seg = s;
2020 seglen = 0;
2021 while (*s) {
2022 struct got_tree_object *next_tree1, *next_tree2;
2023 mode_t mode1, mode2;
2025 if (*s != '/') {
2026 s++;
2027 seglen++;
2028 if (*s)
2029 continue;
2032 te1 = find_entry_by_name(tree1, seg, seglen);
2033 if (te1 == NULL) {
2034 err = got_error(GOT_ERR_NO_OBJ);
2035 goto done;
2038 if (tree2)
2039 te2 = find_entry_by_name(tree2, seg, seglen);
2041 if (te2) {
2042 mode1 = normalize_mode_for_comparison(te1->mode);
2043 mode2 = normalize_mode_for_comparison(te2->mode);
2044 if (mode1 != mode2) {
2045 *changed = 1;
2046 goto done;
2049 if (got_object_id_cmp(&te1->id, &te2->id) == 0) {
2050 *changed = 0;
2051 goto done;
2055 if (*s == '\0') { /* final path element */
2056 *changed = 1;
2057 goto done;
2060 seg = s + 1;
2061 s++;
2062 seglen = 0;
2063 if (*s) {
2064 err = got_object_open_as_tree(&next_tree1, repo,
2065 &te1->id);
2066 te1 = NULL;
2067 if (err)
2068 goto done;
2069 if (tree1 != tree01)
2070 got_object_tree_close(tree1);
2071 tree1 = next_tree1;
2073 if (te2) {
2074 err = got_object_open_as_tree(&next_tree2, repo,
2075 &te2->id);
2076 te2 = NULL;
2077 if (err)
2078 goto done;
2079 if (tree2 != tree02)
2080 got_object_tree_close(tree2);
2081 tree2 = next_tree2;
2082 } else if (tree2) {
2083 if (tree2 != tree02)
2084 got_object_tree_close(tree2);
2085 tree2 = NULL;
2089 done:
2090 if (tree1 && tree1 != tree01)
2091 got_object_tree_close(tree1);
2092 if (tree2 && tree2 != tree02)
2093 got_object_tree_close(tree2);
2094 return err;
2097 const struct got_error *
2098 got_object_tree_entry_dup(struct got_tree_entry **new_te,
2099 struct got_tree_entry *te)
2101 const struct got_error *err = NULL;
2103 *new_te = calloc(1, sizeof(**new_te));
2104 if (*new_te == NULL)
2105 return got_error_from_errno("calloc");
2107 (*new_te)->mode = te->mode;
2108 memcpy((*new_te)->name, te->name, sizeof((*new_te)->name));
2109 memcpy(&(*new_te)->id, &te->id, sizeof((*new_te)->id));
2110 return err;
2113 int
2114 got_object_tree_entry_is_submodule(struct got_tree_entry *te)
2116 return (te->mode & S_IFMT) == (S_IFDIR | S_IFLNK);
2119 int
2120 got_object_tree_entry_is_symlink(struct got_tree_entry *te)
2122 /* S_IFDIR check avoids confusing symlinks with submodules. */
2123 return ((te->mode & (S_IFDIR | S_IFLNK)) == S_IFLNK);
2126 static const struct got_error *
2127 resolve_symlink(char **link_target, const char *path,
2128 struct got_commit_object *commit, struct got_repository *repo)
2130 const struct got_error *err = NULL;
2131 char buf[PATH_MAX];
2132 char *name, *parent_path = NULL;
2133 struct got_object_id *tree_obj_id = NULL;
2134 struct got_tree_object *tree = NULL;
2135 struct got_tree_entry *te = NULL;
2137 *link_target = NULL;
2139 if (strlcpy(buf, path, sizeof(buf)) >= sizeof(buf))
2140 return got_error(GOT_ERR_NO_SPACE);
2142 name = basename(buf);
2143 if (name == NULL)
2144 return got_error_from_errno2("basename", path);
2146 err = got_path_dirname(&parent_path, path);
2147 if (err)
2148 return err;
2150 err = got_object_id_by_path(&tree_obj_id, repo, commit,
2151 parent_path);
2152 if (err) {
2153 if (err->code == GOT_ERR_NO_TREE_ENTRY) {
2154 /* Display the complete path in error message. */
2155 err = got_error_path(path, err->code);
2157 goto done;
2160 err = got_object_open_as_tree(&tree, repo, tree_obj_id);
2161 if (err)
2162 goto done;
2164 te = got_object_tree_find_entry(tree, name);
2165 if (te == NULL) {
2166 err = got_error_path(path, GOT_ERR_NO_TREE_ENTRY);
2167 goto done;
2170 if (got_object_tree_entry_is_symlink(te)) {
2171 err = got_tree_entry_get_symlink_target(link_target, te, repo);
2172 if (err)
2173 goto done;
2174 if (!got_path_is_absolute(*link_target)) {
2175 char *abspath;
2176 if (asprintf(&abspath, "%s/%s", parent_path,
2177 *link_target) == -1) {
2178 err = got_error_from_errno("asprintf");
2179 goto done;
2181 free(*link_target);
2182 *link_target = malloc(PATH_MAX);
2183 if (*link_target == NULL) {
2184 err = got_error_from_errno("malloc");
2185 goto done;
2187 err = got_canonpath(abspath, *link_target, PATH_MAX);
2188 free(abspath);
2189 if (err)
2190 goto done;
2193 done:
2194 free(parent_path);
2195 free(tree_obj_id);
2196 if (tree)
2197 got_object_tree_close(tree);
2198 if (err) {
2199 free(*link_target);
2200 *link_target = NULL;
2202 return err;
2205 const struct got_error *
2206 got_object_resolve_symlinks(char **link_target, const char *path,
2207 struct got_commit_object *commit, struct got_repository *repo)
2209 const struct got_error *err = NULL;
2210 char *next_target = NULL;
2211 int max_recursion = 40; /* matches Git */
2213 *link_target = NULL;
2215 do {
2216 err = resolve_symlink(&next_target,
2217 *link_target ? *link_target : path, commit, repo);
2218 if (err)
2219 break;
2220 if (next_target) {
2221 free(*link_target);
2222 if (--max_recursion == 0) {
2223 err = got_error_path(path, GOT_ERR_RECURSION);
2224 *link_target = NULL;
2225 break;
2227 *link_target = next_target;
2229 } while (next_target);
2231 return err;
2234 const struct got_error *
2235 got_traverse_packed_commits(struct got_object_id_queue *traversed_commits,
2236 struct got_object_id *commit_id, const char *path,
2237 struct got_repository *repo)
2239 const struct got_error *err = NULL;
2240 struct got_pack *pack = NULL;
2241 struct got_packidx *packidx = NULL;
2242 char *path_packfile = NULL;
2243 struct got_commit_object *changed_commit = NULL;
2244 struct got_object_id *changed_commit_id = NULL;
2245 int idx;
2247 err = got_repo_search_packidx(&packidx, &idx, repo, commit_id);
2248 if (err) {
2249 if (err->code != GOT_ERR_NO_OBJ)
2250 return err;
2251 return NULL;
2254 err = got_packidx_get_packfile_path(&path_packfile,
2255 packidx->path_packidx);
2256 if (err)
2257 return err;
2259 pack = got_repo_get_cached_pack(repo, path_packfile);
2260 if (pack == NULL) {
2261 err = got_repo_cache_pack(&pack, repo, path_packfile, packidx);
2262 if (err)
2263 goto done;
2266 if (pack->privsep_child == NULL) {
2267 err = got_pack_start_privsep_child(pack, packidx);
2268 if (err)
2269 goto done;
2272 err = got_privsep_send_commit_traversal_request(
2273 pack->privsep_child->ibuf, commit_id, idx, path);
2274 if (err)
2275 goto done;
2277 err = got_privsep_recv_traversed_commits(&changed_commit,
2278 &changed_commit_id, traversed_commits, pack->privsep_child->ibuf);
2279 if (err)
2280 goto done;
2282 if (changed_commit) {
2284 * Cache the commit in which the path was changed.
2285 * This commit might be opened again soon.
2287 changed_commit->refcnt++;
2288 err = got_repo_cache_commit(repo, changed_commit_id,
2289 changed_commit);
2290 got_object_commit_close(changed_commit);
2292 done:
2293 free(path_packfile);
2294 free(changed_commit_id);
2295 return err;
2298 const struct got_error *
2299 got_object_enumerate(int *found_all_objects,
2300 got_object_enumerate_commit_cb cb_commit,
2301 got_object_enumerate_tree_cb cb_tree, void *cb_arg,
2302 struct got_object_id **ours, int nours,
2303 struct got_object_id **theirs, int ntheirs,
2304 struct got_packidx *packidx, struct got_repository *repo)
2306 const struct got_error *err = NULL;
2307 struct got_pack *pack;
2308 char *path_packfile = NULL;
2310 err = got_packidx_get_packfile_path(&path_packfile,
2311 packidx->path_packidx);
2312 if (err)
2313 return err;
2315 pack = got_repo_get_cached_pack(repo, path_packfile);
2316 if (pack == NULL) {
2317 err = got_repo_cache_pack(&pack, repo, path_packfile, packidx);
2318 if (err)
2319 goto done;
2322 if (pack->privsep_child == NULL) {
2323 err = got_pack_start_privsep_child(pack, packidx);
2324 if (err)
2325 goto done;
2328 err = got_privsep_send_object_enumeration_request(
2329 pack->privsep_child->ibuf);
2330 if (err)
2331 goto done;
2333 err = got_privsep_send_object_idlist(pack->privsep_child->ibuf,
2334 ours, nours);
2335 if (err)
2336 goto done;
2337 err = got_privsep_send_object_idlist_done(pack->privsep_child->ibuf);
2338 if (err)
2339 goto done;
2341 err = got_privsep_send_object_idlist(pack->privsep_child->ibuf,
2342 theirs, ntheirs);
2343 if (err)
2344 goto done;
2345 err = got_privsep_send_object_idlist_done(pack->privsep_child->ibuf);
2346 if (err)
2347 goto done;
2349 err = got_privsep_recv_enumerated_objects(found_all_objects,
2350 pack->privsep_child->ibuf, cb_commit, cb_tree, cb_arg, repo);
2351 done:
2352 free(path_packfile);
2353 return err;
2356 void
2357 got_object_commit_retain(struct got_commit_object *commit)
2359 commit->refcnt++;