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>
26 #include <errno.h>
27 #include <fcntl.h>
28 #include <stdio.h>
29 #include <stdlib.h>
30 #include <string.h>
31 #include <stdint.h>
32 #include <sha1.h>
33 #include <unistd.h>
34 #include <zlib.h>
35 #include <ctype.h>
36 #include <libgen.h>
37 #include <limits.h>
38 #include <imsg.h>
39 #include <time.h>
41 #include "got_error.h"
42 #include "got_object.h"
43 #include "got_repository.h"
44 #include "got_opentemp.h"
45 #include "got_path.h"
47 #include "got_lib_sha1.h"
48 #include "got_lib_delta.h"
49 #include "got_lib_inflate.h"
50 #include "got_lib_object.h"
51 #include "got_lib_privsep.h"
52 #include "got_lib_object_idcache.h"
53 #include "got_lib_object_cache.h"
54 #include "got_lib_object_parse.h"
55 #include "got_lib_pack.h"
56 #include "got_lib_repository.h"
58 #ifndef MIN
59 #define MIN(_a,_b) ((_a) < (_b) ? (_a) : (_b))
60 #endif
62 struct got_object_id *
63 got_object_get_id(struct got_object *obj)
64 {
65 return &obj->id;
66 }
68 const struct got_error *
69 got_object_get_id_str(char **outbuf, struct got_object *obj)
70 {
71 return got_object_id_str(outbuf, &obj->id);
72 }
74 const struct got_error *
75 got_object_get_type(int *type, struct got_repository *repo,
76 struct got_object_id *id)
77 {
78 const struct got_error *err = NULL;
79 struct got_object *obj;
81 err = got_object_open(&obj, repo, id);
82 if (err)
83 return err;
85 switch (obj->type) {
86 case GOT_OBJ_TYPE_COMMIT:
87 case GOT_OBJ_TYPE_TREE:
88 case GOT_OBJ_TYPE_BLOB:
89 case GOT_OBJ_TYPE_TAG:
90 *type = obj->type;
91 break;
92 default:
93 err = got_error(GOT_ERR_OBJ_TYPE);
94 break;
95 }
97 got_object_close(obj);
98 return err;
99 }
101 const struct got_error *
102 got_object_get_path(char **path, struct got_object_id *id,
103 struct got_repository *repo)
105 const struct got_error *err = NULL;
106 char *hex = NULL;
107 char *path_objects;
109 *path = NULL;
111 path_objects = got_repo_get_path_objects(repo);
112 if (path_objects == NULL)
113 return got_error_from_errno("got_repo_get_path_objects");
115 err = got_object_id_str(&hex, id);
116 if (err)
117 goto done;
119 if (asprintf(path, "%s/%.2x/%s", path_objects,
120 id->sha1[0], hex + 2) == -1)
121 err = got_error_from_errno("asprintf");
123 done:
124 free(hex);
125 free(path_objects);
126 return err;
129 const struct got_error *
130 got_object_open_loose_fd(int *fd, struct got_object_id *id,
131 struct got_repository *repo)
133 const struct got_error *err = NULL;
134 char *path;
136 err = got_object_get_path(&path, id, repo);
137 if (err)
138 return err;
139 *fd = open(path, O_RDONLY | O_NOFOLLOW);
140 if (*fd == -1) {
141 err = got_error_from_errno2("open", path);
142 goto done;
144 done:
145 free(path);
146 return err;
149 static const struct got_error *
150 request_packed_object(struct got_object **obj, struct got_pack *pack, int idx,
151 struct got_object_id *id)
153 const struct got_error *err = NULL;
154 struct imsgbuf *ibuf = pack->privsep_child->ibuf;
156 err = got_privsep_send_packed_obj_req(ibuf, idx, id);
157 if (err)
158 return err;
160 err = got_privsep_recv_obj(obj, ibuf);
161 if (err)
162 return err;
164 memcpy(&(*obj)->id, id, sizeof((*obj)->id));
166 return NULL;
169 static const struct got_error *
170 request_packed_object_raw(uint8_t **outbuf, off_t *size, size_t *hdrlen,
171 int outfd, struct got_pack *pack, int idx, struct got_object_id *id)
173 const struct got_error *err = NULL;
174 struct imsgbuf *ibuf = pack->privsep_child->ibuf;
175 int outfd_child;
176 int basefd, accumfd; /* temporary files for delta application */
178 basefd = got_opentempfd();
179 if (basefd == -1)
180 return got_error_from_errno("got_opentempfd");
182 accumfd = got_opentempfd();
183 if (accumfd == -1) {
184 close(basefd);
185 return got_error_from_errno("got_opentempfd");
188 outfd_child = dup(outfd);
189 if (outfd_child == -1) {
190 err = got_error_from_errno("dup");
191 close(basefd);
192 close(accumfd);
193 return err;
196 err = got_privsep_send_packed_raw_obj_req(ibuf, idx, id);
197 if (err) {
198 close(basefd);
199 close(accumfd);
200 close(outfd_child);
201 return err;
204 err = got_privsep_send_raw_obj_outfd(ibuf, outfd_child);
205 if (err) {
206 close(basefd);
207 close(accumfd);
208 return err;
212 err = got_privsep_send_tmpfd(pack->privsep_child->ibuf,
213 basefd);
214 if (err) {
215 close(accumfd);
216 return err;
219 err = got_privsep_send_tmpfd(pack->privsep_child->ibuf,
220 accumfd);
221 if (err)
222 return err;
224 err = got_privsep_recv_raw_obj(outbuf, size, hdrlen, ibuf);
225 if (err)
226 return err;
228 return NULL;
231 static void
232 set_max_datasize(void)
234 struct rlimit rl;
236 if (getrlimit(RLIMIT_DATA, &rl) != 0)
237 return;
239 rl.rlim_cur = rl.rlim_max;
240 setrlimit(RLIMIT_DATA, &rl);
243 static const struct got_error *
244 start_pack_privsep_child(struct got_pack *pack, struct got_packidx *packidx)
246 const struct got_error *err = NULL;
247 int imsg_fds[2];
248 pid_t pid;
249 struct imsgbuf *ibuf;
251 ibuf = calloc(1, sizeof(*ibuf));
252 if (ibuf == NULL)
253 return got_error_from_errno("calloc");
255 pack->privsep_child = calloc(1, sizeof(*pack->privsep_child));
256 if (pack->privsep_child == NULL) {
257 err = got_error_from_errno("calloc");
258 free(ibuf);
259 return err;
262 if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_fds) == -1) {
263 err = got_error_from_errno("socketpair");
264 goto done;
267 pid = fork();
268 if (pid == -1) {
269 err = got_error_from_errno("fork");
270 goto done;
271 } else if (pid == 0) {
272 set_max_datasize();
273 got_privsep_exec_child(imsg_fds, GOT_PATH_PROG_READ_PACK,
274 pack->path_packfile);
275 /* not reached */
278 if (close(imsg_fds[1]) == -1)
279 return got_error_from_errno("close");
280 pack->privsep_child->imsg_fd = imsg_fds[0];
281 pack->privsep_child->pid = pid;
282 imsg_init(ibuf, imsg_fds[0]);
283 pack->privsep_child->ibuf = ibuf;
285 err = got_privsep_init_pack_child(ibuf, pack, packidx);
286 if (err) {
287 const struct got_error *child_err;
288 err = got_privsep_send_stop(pack->privsep_child->imsg_fd);
289 child_err = got_privsep_wait_for_child(
290 pack->privsep_child->pid);
291 if (child_err && err == NULL)
292 err = child_err;
294 done:
295 if (err) {
296 free(ibuf);
297 free(pack->privsep_child);
298 pack->privsep_child = NULL;
300 return err;
303 static const struct got_error *
304 read_packed_object_privsep(struct got_object **obj,
305 struct got_repository *repo, struct got_pack *pack,
306 struct got_packidx *packidx, int idx, struct got_object_id *id)
308 const struct got_error *err = NULL;
310 if (pack->privsep_child == NULL) {
311 err = start_pack_privsep_child(pack, packidx);
312 if (err)
313 return err;
316 return request_packed_object(obj, pack, idx, id);
319 static const struct got_error *
320 read_packed_object_raw_privsep(uint8_t **outbuf, off_t *size, size_t *hdrlen,
321 int outfd, struct got_pack *pack, struct got_packidx *packidx, int idx,
322 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_raw(outbuf, size, hdrlen, outfd, pack,
333 idx, id);
336 const struct got_error *
337 got_object_open_packed(struct got_object **obj, struct got_object_id *id,
338 struct got_repository *repo)
340 const struct got_error *err = NULL;
341 struct got_pack *pack = NULL;
342 struct got_packidx *packidx = NULL;
343 int idx;
344 char *path_packfile;
346 err = got_repo_search_packidx(&packidx, &idx, repo, id);
347 if (err)
348 return err;
350 err = got_packidx_get_packfile_path(&path_packfile,
351 packidx->path_packidx);
352 if (err)
353 return err;
355 pack = got_repo_get_cached_pack(repo, path_packfile);
356 if (pack == NULL) {
357 err = got_repo_cache_pack(&pack, repo, path_packfile, packidx);
358 if (err)
359 goto done;
362 err = read_packed_object_privsep(obj, repo, pack, packidx, idx, id);
363 if (err)
364 goto done;
365 done:
366 free(path_packfile);
367 return err;
370 static const struct got_error *
371 request_object(struct got_object **obj, struct got_object_id *id,
372 struct got_repository *repo, int fd)
374 const struct got_error *err = NULL;
375 struct imsgbuf *ibuf;
377 ibuf = repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_OBJECT].ibuf;
379 err = got_privsep_send_obj_req(ibuf, fd, id);
380 if (err)
381 return err;
383 return got_privsep_recv_obj(obj, ibuf);
386 static const struct got_error *
387 request_raw_object(uint8_t **outbuf, off_t *size, size_t *hdrlen, int outfd,
388 struct got_object_id *id, struct got_repository *repo, int infd)
390 const struct got_error *err = NULL;
391 struct imsgbuf *ibuf;
392 int outfd_child;
394 ibuf = repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_OBJECT].ibuf;
396 outfd_child = dup(outfd);
397 if (outfd_child == -1)
398 return got_error_from_errno("dup");
400 err = got_privsep_send_raw_obj_req(ibuf, infd, id);
401 if (err)
402 return err;
404 err = got_privsep_send_raw_obj_outfd(ibuf, outfd_child);
405 if (err)
406 return err;
408 return got_privsep_recv_raw_obj(outbuf, size, hdrlen, ibuf);
411 static const struct got_error *
412 start_read_object_child(struct got_repository *repo)
414 const struct got_error *err = NULL;
415 int imsg_fds[2];
416 pid_t pid;
417 struct imsgbuf *ibuf;
419 ibuf = calloc(1, sizeof(*ibuf));
420 if (ibuf == NULL)
421 return got_error_from_errno("calloc");
423 if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_fds) == -1) {
424 err = got_error_from_errno("socketpair");
425 free(ibuf);
426 return err;
429 pid = fork();
430 if (pid == -1) {
431 err = got_error_from_errno("fork");
432 free(ibuf);
433 return err;
435 else if (pid == 0) {
436 got_privsep_exec_child(imsg_fds, GOT_PATH_PROG_READ_OBJECT,
437 repo->path);
438 /* not reached */
441 if (close(imsg_fds[1]) == -1) {
442 err = got_error_from_errno("close");
443 free(ibuf);
444 return err;
447 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_OBJECT].imsg_fd =
448 imsg_fds[0];
449 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_OBJECT].pid = pid;
450 imsg_init(ibuf, imsg_fds[0]);
451 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_OBJECT].ibuf = ibuf;
453 return NULL;
456 const struct got_error *
457 got_object_read_header_privsep(struct got_object **obj,
458 struct got_object_id *id, struct got_repository *repo, int obj_fd)
460 const struct got_error *err;
462 if (repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_OBJECT].imsg_fd != -1)
463 return request_object(obj, id, repo, obj_fd);
465 err = start_read_object_child(repo);
466 if (err) {
467 close(obj_fd);
468 return err;
471 return request_object(obj, id, repo, obj_fd);
474 static const struct got_error *
475 read_object_raw_privsep(uint8_t **outbuf, off_t *size, size_t *hdrlen,
476 int outfd, struct got_object_id *id, struct got_repository *repo,
477 int obj_fd)
479 const struct got_error *err;
481 if (repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_OBJECT].imsg_fd != -1)
482 return request_raw_object(outbuf, size, hdrlen, outfd, id,
483 repo, obj_fd);
485 err = start_read_object_child(repo);
486 if (err)
487 return err;
489 return request_raw_object(outbuf, size, hdrlen, outfd, id, repo,
490 obj_fd);
493 const struct got_error *
494 got_object_open(struct got_object **obj, struct got_repository *repo,
495 struct got_object_id *id)
497 const struct got_error *err = NULL;
498 int fd;
500 *obj = got_repo_get_cached_object(repo, id);
501 if (*obj != NULL) {
502 (*obj)->refcnt++;
503 return NULL;
506 err = got_object_open_packed(obj, id, repo);
507 if (err && err->code != GOT_ERR_NO_OBJ)
508 return err;
509 if (*obj) {
510 (*obj)->refcnt++;
511 return got_repo_cache_object(repo, id, *obj);
514 err = got_object_open_loose_fd(&fd, id, repo);
515 if (err) {
516 if (err->code == GOT_ERR_ERRNO && errno == ENOENT)
517 err = got_error_no_obj(id);
518 return err;
521 err = got_object_read_header_privsep(obj, id, repo, fd);
522 if (err)
523 return err;
525 memcpy((*obj)->id.sha1, id->sha1, SHA1_DIGEST_LENGTH);
527 (*obj)->refcnt++;
528 return got_repo_cache_object(repo, id, *obj);
531 /* *outfd must be initialized to -1 by caller */
532 const struct got_error *
533 got_object_raw_open(struct got_raw_object **obj, int *outfd,
534 struct got_repository *repo, struct got_object_id *id, size_t blocksize)
536 const struct got_error *err = NULL;
537 struct got_packidx *packidx = NULL;
538 int idx;
539 uint8_t *outbuf = NULL;
540 off_t size = 0;
541 size_t hdrlen = 0;
542 char *path_packfile = NULL;
544 *obj = got_repo_get_cached_raw_object(repo, id);
545 if (*obj != NULL) {
546 (*obj)->refcnt++;
547 return NULL;
550 if (*outfd == -1) {
551 *outfd = got_opentempfd();
552 if (*outfd == -1)
553 return got_error_from_errno("got_opentempfd");
556 err = got_repo_search_packidx(&packidx, &idx, repo, id);
557 if (err == NULL) {
558 struct got_pack *pack = NULL;
560 err = got_packidx_get_packfile_path(&path_packfile,
561 packidx->path_packidx);
562 if (err)
563 goto done;
565 pack = got_repo_get_cached_pack(repo, path_packfile);
566 if (pack == NULL) {
567 err = got_repo_cache_pack(&pack, repo, path_packfile,
568 packidx);
569 if (err)
570 goto done;
572 err = read_packed_object_raw_privsep(&outbuf, &size, &hdrlen,
573 *outfd, pack, packidx, idx, id);
574 if (err)
575 goto done;
576 } else if (err->code == GOT_ERR_NO_OBJ) {
577 int fd;
579 err = got_object_open_loose_fd(&fd, id, repo);
580 if (err)
581 goto done;
582 err = read_object_raw_privsep(&outbuf, &size, &hdrlen, *outfd,
583 id, repo, fd);
584 if (err)
585 goto done;
588 *obj = calloc(1, sizeof(**obj));
589 if (*obj == NULL) {
590 err = got_error_from_errno("calloc");
591 goto done;
594 (*obj)->read_buf = malloc(blocksize);
595 if ((*obj)->read_buf == NULL) {
596 err = got_error_from_errno("malloc");
597 goto done;
600 if (outbuf) {
601 (*obj)->f = fmemopen(outbuf, hdrlen + size, "r");
602 if ((*obj)->f == NULL) {
603 err = got_error_from_errno("fdopen");
604 goto done;
606 (*obj)->data = outbuf;
607 } else {
608 struct stat sb;
609 if (fstat(*outfd, &sb) == -1) {
610 err = got_error_from_errno("fstat");
611 goto done;
614 if (sb.st_size != hdrlen + size) {
615 err = got_error(GOT_ERR_PRIVSEP_LEN);
616 goto done;
619 (*obj)->f = fdopen(*outfd, "r");
620 if ((*obj)->f == NULL) {
621 err = got_error_from_errno("fdopen");
622 goto done;
624 (*obj)->data = NULL;
625 *outfd = -1;
627 (*obj)->hdrlen = hdrlen;
628 (*obj)->size = size;
629 (*obj)->blocksize = blocksize;
630 err = got_repo_cache_raw_object(repo, id, *obj);
631 done:
632 free(path_packfile);
633 if (err) {
634 if (*obj) {
635 got_object_raw_close(*obj);
636 *obj = NULL;
638 free(outbuf);
639 } else
640 (*obj)->refcnt++;
641 return err;
644 void
645 got_object_raw_rewind(struct got_raw_object *obj)
647 if (obj->f)
648 rewind(obj->f);
651 size_t
652 got_object_raw_get_hdrlen(struct got_raw_object *obj)
654 return obj->hdrlen;
657 const uint8_t *
658 got_object_raw_get_read_buf(struct got_raw_object *obj)
660 return obj->read_buf;
663 const struct got_error *
664 got_object_raw_read_block(size_t *outlenp, struct got_raw_object *obj)
666 size_t n;
668 n = fread(obj->read_buf, 1, obj->blocksize, obj->f);
669 if (n == 0 && ferror(obj->f))
670 return got_ferror(obj->f, GOT_ERR_IO);
671 *outlenp = n;
672 return NULL;
675 const struct got_error *
676 got_object_open_by_id_str(struct got_object **obj, struct got_repository *repo,
677 const char *id_str)
679 struct got_object_id id;
681 if (!got_parse_sha1_digest(id.sha1, id_str))
682 return got_error_path(id_str, GOT_ERR_BAD_OBJ_ID_STR);
684 return got_object_open(obj, repo, &id);
687 const struct got_error *
688 got_object_resolve_id_str(struct got_object_id **id,
689 struct got_repository *repo, const char *id_str)
691 const struct got_error *err = NULL;
692 struct got_object *obj;
694 err = got_object_open_by_id_str(&obj, repo, id_str);
695 if (err)
696 return err;
698 *id = got_object_id_dup(got_object_get_id(obj));
699 got_object_close(obj);
700 if (*id == NULL)
701 return got_error_from_errno("got_object_id_dup");
703 return NULL;
706 static const struct got_error *
707 request_packed_commit(struct got_commit_object **commit, struct got_pack *pack,
708 int pack_idx, struct got_object_id *id)
710 const struct got_error *err = NULL;
712 err = got_privsep_send_commit_req(pack->privsep_child->ibuf, -1, id,
713 pack_idx);
714 if (err)
715 return err;
717 err = got_privsep_recv_commit(commit, pack->privsep_child->ibuf);
718 if (err)
719 return err;
721 (*commit)->flags |= GOT_COMMIT_FLAG_PACKED;
722 return NULL;
725 static const struct got_error *
726 read_packed_commit_privsep(struct got_commit_object **commit,
727 struct got_pack *pack, struct got_packidx *packidx, int idx,
728 struct got_object_id *id)
730 const struct got_error *err = NULL;
732 if (pack->privsep_child)
733 return request_packed_commit(commit, pack, idx, id);
735 err = start_pack_privsep_child(pack, packidx);
736 if (err)
737 return err;
739 return request_packed_commit(commit, pack, idx, id);
742 static const struct got_error *
743 request_commit(struct got_commit_object **commit, struct got_repository *repo,
744 int fd, struct got_object_id *id)
746 const struct got_error *err = NULL;
747 struct imsgbuf *ibuf;
749 ibuf = repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_COMMIT].ibuf;
751 err = got_privsep_send_commit_req(ibuf, fd, id, -1);
752 if (err)
753 return err;
755 return got_privsep_recv_commit(commit, ibuf);
758 static const struct got_error *
759 read_commit_privsep(struct got_commit_object **commit, int obj_fd,
760 struct got_object_id *id, struct got_repository *repo)
762 const struct got_error *err;
763 int imsg_fds[2];
764 pid_t pid;
765 struct imsgbuf *ibuf;
767 if (repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_COMMIT].imsg_fd != -1)
768 return request_commit(commit, repo, obj_fd, id);
770 ibuf = calloc(1, sizeof(*ibuf));
771 if (ibuf == NULL)
772 return got_error_from_errno("calloc");
774 if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_fds) == -1) {
775 err = got_error_from_errno("socketpair");
776 free(ibuf);
777 return err;
780 pid = fork();
781 if (pid == -1) {
782 err = got_error_from_errno("fork");
783 free(ibuf);
784 return err;
786 else if (pid == 0) {
787 got_privsep_exec_child(imsg_fds, GOT_PATH_PROG_READ_COMMIT,
788 repo->path);
789 /* not reached */
792 if (close(imsg_fds[1]) == -1) {
793 err = got_error_from_errno("close");
794 free(ibuf);
795 return err;
797 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_COMMIT].imsg_fd =
798 imsg_fds[0];
799 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_COMMIT].pid = pid;
800 imsg_init(ibuf, imsg_fds[0]);
801 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_COMMIT].ibuf = ibuf;
803 return request_commit(commit, repo, obj_fd, id);
807 static const struct got_error *
808 open_commit(struct got_commit_object **commit,
809 struct got_repository *repo, struct got_object_id *id, int check_cache)
811 const struct got_error *err = NULL;
812 struct got_packidx *packidx = NULL;
813 int idx;
814 char *path_packfile = NULL;
816 if (check_cache) {
817 *commit = got_repo_get_cached_commit(repo, id);
818 if (*commit != NULL) {
819 (*commit)->refcnt++;
820 return NULL;
822 } else
823 *commit = NULL;
825 err = got_repo_search_packidx(&packidx, &idx, repo, id);
826 if (err == NULL) {
827 struct got_pack *pack = NULL;
829 err = got_packidx_get_packfile_path(&path_packfile,
830 packidx->path_packidx);
831 if (err)
832 return err;
834 pack = got_repo_get_cached_pack(repo, path_packfile);
835 if (pack == NULL) {
836 err = got_repo_cache_pack(&pack, repo, path_packfile,
837 packidx);
838 if (err)
839 goto done;
841 err = read_packed_commit_privsep(commit, pack,
842 packidx, idx, id);
843 } else if (err->code == GOT_ERR_NO_OBJ) {
844 int fd;
846 err = got_object_open_loose_fd(&fd, id, repo);
847 if (err)
848 return err;
849 err = read_commit_privsep(commit, fd, id, repo);
852 if (err == NULL) {
853 (*commit)->refcnt++;
854 err = got_repo_cache_commit(repo, id, *commit);
856 done:
857 free(path_packfile);
858 return err;
861 const struct got_error *
862 got_object_open_as_commit(struct got_commit_object **commit,
863 struct got_repository *repo, struct got_object_id *id)
865 *commit = got_repo_get_cached_commit(repo, id);
866 if (*commit != NULL) {
867 (*commit)->refcnt++;
868 return NULL;
871 return open_commit(commit, repo, id, 0);
874 const struct got_error *
875 got_object_commit_open(struct got_commit_object **commit,
876 struct got_repository *repo, struct got_object *obj)
878 return open_commit(commit, repo, got_object_get_id(obj), 1);
881 const struct got_error *
882 got_object_qid_alloc(struct got_object_qid **qid, struct got_object_id *id)
884 const struct got_error *err = NULL;
886 *qid = calloc(1, sizeof(**qid));
887 if (*qid == NULL)
888 return got_error_from_errno("calloc");
890 (*qid)->id = got_object_id_dup(id);
891 if ((*qid)->id == NULL) {
892 err = got_error_from_errno("got_object_id_dup");
893 got_object_qid_free(*qid);
894 *qid = NULL;
895 return err;
898 return NULL;
901 const struct got_error *
902 got_object_id_queue_copy(const struct got_object_id_queue *src,
903 struct got_object_id_queue *dest)
905 const struct got_error *err;
906 struct got_object_qid *qid;
908 STAILQ_FOREACH(qid, src, entry) {
909 struct got_object_qid *new;
910 /*
911 * Deep-copy the object ID only. Let the caller deal
912 * with setting up the new->data pointer if needed.
913 */
914 err = got_object_qid_alloc(&new, qid->id);
915 if (err) {
916 got_object_id_queue_free(dest);
917 return err;
919 STAILQ_INSERT_TAIL(dest, new, entry);
922 return NULL;
925 static const struct got_error *
926 request_packed_tree(struct got_tree_object **tree, struct got_pack *pack,
927 int pack_idx, struct got_object_id *id)
929 const struct got_error *err = NULL;
931 err = got_privsep_send_tree_req(pack->privsep_child->ibuf, -1, id,
932 pack_idx);
933 if (err)
934 return err;
936 return got_privsep_recv_tree(tree, pack->privsep_child->ibuf);
939 static const struct got_error *
940 read_packed_tree_privsep(struct got_tree_object **tree,
941 struct got_pack *pack, struct got_packidx *packidx, int idx,
942 struct got_object_id *id)
944 const struct got_error *err = NULL;
946 if (pack->privsep_child)
947 return request_packed_tree(tree, pack, idx, id);
949 err = start_pack_privsep_child(pack, packidx);
950 if (err)
951 return err;
953 return request_packed_tree(tree, pack, idx, id);
956 static const struct got_error *
957 request_tree(struct got_tree_object **tree, struct got_repository *repo,
958 int fd, struct got_object_id *id)
960 const struct got_error *err = NULL;
961 struct imsgbuf *ibuf;
963 ibuf = repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TREE].ibuf;
965 err = got_privsep_send_tree_req(ibuf, fd, id, -1);
966 if (err)
967 return err;
969 return got_privsep_recv_tree(tree, ibuf);
972 const struct got_error *
973 read_tree_privsep(struct got_tree_object **tree, int obj_fd,
974 struct got_object_id *id, struct got_repository *repo)
976 const struct got_error *err;
977 int imsg_fds[2];
978 pid_t pid;
979 struct imsgbuf *ibuf;
981 if (repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TREE].imsg_fd != -1)
982 return request_tree(tree, repo, obj_fd, id);
984 ibuf = calloc(1, sizeof(*ibuf));
985 if (ibuf == NULL)
986 return got_error_from_errno("calloc");
988 if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_fds) == -1) {
989 err = got_error_from_errno("socketpair");
990 free(ibuf);
991 return err;
994 pid = fork();
995 if (pid == -1) {
996 err = got_error_from_errno("fork");
997 free(ibuf);
998 return err;
1000 else if (pid == 0) {
1001 got_privsep_exec_child(imsg_fds, GOT_PATH_PROG_READ_TREE,
1002 repo->path);
1003 /* not reached */
1006 if (close(imsg_fds[1]) == -1) {
1007 err = got_error_from_errno("close");
1008 free(ibuf);
1009 return err;
1011 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TREE].imsg_fd =
1012 imsg_fds[0];
1013 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TREE].pid = pid;
1014 imsg_init(ibuf, imsg_fds[0]);
1015 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TREE].ibuf = ibuf;
1018 return request_tree(tree, repo, obj_fd, id);
1021 static const struct got_error *
1022 open_tree(struct got_tree_object **tree, struct got_repository *repo,
1023 struct got_object_id *id, int check_cache)
1025 const struct got_error *err = NULL;
1026 struct got_packidx *packidx = NULL;
1027 int idx;
1028 char *path_packfile = NULL;
1030 if (check_cache) {
1031 *tree = got_repo_get_cached_tree(repo, id);
1032 if (*tree != NULL) {
1033 (*tree)->refcnt++;
1034 return NULL;
1036 } else
1037 *tree = NULL;
1039 err = got_repo_search_packidx(&packidx, &idx, repo, id);
1040 if (err == NULL) {
1041 struct got_pack *pack = NULL;
1043 err = got_packidx_get_packfile_path(&path_packfile,
1044 packidx->path_packidx);
1045 if (err)
1046 return err;
1048 pack = got_repo_get_cached_pack(repo, path_packfile);
1049 if (pack == NULL) {
1050 err = got_repo_cache_pack(&pack, repo, path_packfile,
1051 packidx);
1052 if (err)
1053 goto done;
1055 err = read_packed_tree_privsep(tree, pack,
1056 packidx, idx, id);
1057 } else if (err->code == GOT_ERR_NO_OBJ) {
1058 int fd;
1060 err = got_object_open_loose_fd(&fd, id, repo);
1061 if (err)
1062 return err;
1063 err = read_tree_privsep(tree, fd, id, repo);
1066 if (err == NULL) {
1067 (*tree)->refcnt++;
1068 err = got_repo_cache_tree(repo, id, *tree);
1070 done:
1071 free(path_packfile);
1072 return err;
1075 const struct got_error *
1076 got_object_open_as_tree(struct got_tree_object **tree,
1077 struct got_repository *repo, struct got_object_id *id)
1079 *tree = got_repo_get_cached_tree(repo, id);
1080 if (*tree != NULL) {
1081 (*tree)->refcnt++;
1082 return NULL;
1085 return open_tree(tree, repo, id, 0);
1088 const struct got_error *
1089 got_object_tree_open(struct got_tree_object **tree,
1090 struct got_repository *repo, struct got_object *obj)
1092 return open_tree(tree, repo, got_object_get_id(obj), 1);
1095 int
1096 got_object_tree_get_nentries(struct got_tree_object *tree)
1098 return tree->nentries;
1101 struct got_tree_entry *
1102 got_object_tree_get_first_entry(struct got_tree_object *tree)
1104 return got_object_tree_get_entry(tree, 0);
1107 struct got_tree_entry *
1108 got_object_tree_get_last_entry(struct got_tree_object *tree)
1110 return got_object_tree_get_entry(tree, tree->nentries - 1);
1113 struct got_tree_entry *
1114 got_object_tree_get_entry(struct got_tree_object *tree, int i)
1116 if (i < 0 || i >= tree->nentries)
1117 return NULL;
1118 return &tree->entries[i];
1121 mode_t
1122 got_tree_entry_get_mode(struct got_tree_entry *te)
1124 return te->mode;
1127 const char *
1128 got_tree_entry_get_name(struct got_tree_entry *te)
1130 return &te->name[0];
1133 struct got_object_id *
1134 got_tree_entry_get_id(struct got_tree_entry *te)
1136 return &te->id;
1139 const struct got_error *
1140 got_object_blob_read_to_str(char **s, struct got_blob_object *blob)
1142 const struct got_error *err = NULL;
1143 size_t len, totlen, hdrlen, offset;
1145 *s = NULL;
1147 hdrlen = got_object_blob_get_hdrlen(blob);
1148 totlen = 0;
1149 offset = 0;
1150 do {
1151 char *p;
1153 err = got_object_blob_read_block(&len, blob);
1154 if (err)
1155 return err;
1157 if (len == 0)
1158 break;
1160 totlen += len - hdrlen;
1161 p = realloc(*s, totlen + 1);
1162 if (p == NULL) {
1163 err = got_error_from_errno("realloc");
1164 free(*s);
1165 *s = NULL;
1166 return err;
1168 *s = p;
1169 /* Skip blob object header first time around. */
1170 memcpy(*s + offset,
1171 got_object_blob_get_read_buf(blob) + hdrlen, len - hdrlen);
1172 hdrlen = 0;
1173 offset = totlen;
1174 } while (len > 0);
1176 (*s)[totlen] = '\0';
1177 return NULL;
1180 const struct got_error *
1181 got_tree_entry_get_symlink_target(char **link_target, struct got_tree_entry *te,
1182 struct got_repository *repo)
1184 const struct got_error *err = NULL;
1185 struct got_blob_object *blob = NULL;
1187 *link_target = NULL;
1189 if (!got_object_tree_entry_is_symlink(te))
1190 return got_error(GOT_ERR_TREE_ENTRY_TYPE);
1192 err = got_object_open_as_blob(&blob, repo,
1193 got_tree_entry_get_id(te), PATH_MAX);
1194 if (err)
1195 return err;
1197 err = got_object_blob_read_to_str(link_target, blob);
1198 got_object_blob_close(blob);
1199 if (err) {
1200 free(*link_target);
1201 *link_target = NULL;
1203 return err;
1206 int
1207 got_tree_entry_get_index(struct got_tree_entry *te)
1209 return te->idx;
1212 struct got_tree_entry *
1213 got_tree_entry_get_next(struct got_tree_object *tree,
1214 struct got_tree_entry *te)
1216 return got_object_tree_get_entry(tree, te->idx + 1);
1219 struct got_tree_entry *
1220 got_tree_entry_get_prev(struct got_tree_object *tree,
1221 struct got_tree_entry *te)
1223 return got_object_tree_get_entry(tree, te->idx - 1);
1226 static const struct got_error *
1227 request_packed_blob(uint8_t **outbuf, size_t *size, size_t *hdrlen, int outfd,
1228 struct got_pack *pack, struct got_packidx *packidx, int idx,
1229 struct got_object_id *id)
1231 const struct got_error *err = NULL;
1232 int outfd_child;
1233 int basefd, accumfd; /* temporary files for delta application */
1235 basefd = got_opentempfd();
1236 if (basefd == -1)
1237 return got_error_from_errno("got_opentempfd");
1238 accumfd = got_opentempfd();
1239 if (accumfd == -1)
1240 return got_error_from_errno("got_opentempfd");
1242 outfd_child = dup(outfd);
1243 if (outfd_child == -1)
1244 return got_error_from_errno("dup");
1246 err = got_privsep_send_blob_req(pack->privsep_child->ibuf, -1, id, idx);
1247 if (err)
1248 return err;
1250 err = got_privsep_send_blob_outfd(pack->privsep_child->ibuf,
1251 outfd_child);
1252 if (err) {
1253 close(basefd);
1254 close(accumfd);
1255 return err;
1258 err = got_privsep_send_tmpfd(pack->privsep_child->ibuf,
1259 basefd);
1260 if (err) {
1261 close(accumfd);
1262 return err;
1265 err = got_privsep_send_tmpfd(pack->privsep_child->ibuf,
1266 accumfd);
1267 if (err)
1268 return err;
1270 err = got_privsep_recv_blob(outbuf, size, hdrlen,
1271 pack->privsep_child->ibuf);
1272 if (err)
1273 return err;
1275 if (lseek(outfd, SEEK_SET, 0) == -1)
1276 err = got_error_from_errno("lseek");
1278 return err;
1281 static const struct got_error *
1282 read_packed_blob_privsep(uint8_t **outbuf, size_t *size, size_t *hdrlen,
1283 int outfd, struct got_pack *pack, struct got_packidx *packidx, int idx,
1284 struct got_object_id *id)
1286 const struct got_error *err = NULL;
1288 if (pack->privsep_child == NULL) {
1289 err = start_pack_privsep_child(pack, packidx);
1290 if (err)
1291 return err;
1294 return request_packed_blob(outbuf, size, hdrlen, outfd, pack, packidx,
1295 idx, id);
1298 static const struct got_error *
1299 request_blob(uint8_t **outbuf, size_t *size, size_t *hdrlen, int outfd,
1300 int infd, struct got_object_id *id, struct imsgbuf *ibuf)
1302 const struct got_error *err = NULL;
1303 int outfd_child;
1305 outfd_child = dup(outfd);
1306 if (outfd_child == -1)
1307 return got_error_from_errno("dup");
1309 err = got_privsep_send_blob_req(ibuf, infd, id, -1);
1310 if (err)
1311 return err;
1313 err = got_privsep_send_blob_outfd(ibuf, outfd_child);
1314 if (err)
1315 return err;
1317 err = got_privsep_recv_blob(outbuf, size, hdrlen, ibuf);
1318 if (err)
1319 return err;
1321 if (lseek(outfd, SEEK_SET, 0) == -1)
1322 return got_error_from_errno("lseek");
1324 return err;
1327 static const struct got_error *
1328 read_blob_privsep(uint8_t **outbuf, size_t *size, size_t *hdrlen,
1329 int outfd, int infd, struct got_object_id *id, struct got_repository *repo)
1331 const struct got_error *err;
1332 int imsg_fds[2];
1333 pid_t pid;
1334 struct imsgbuf *ibuf;
1336 if (repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_BLOB].imsg_fd != -1) {
1337 ibuf = repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_BLOB].ibuf;
1338 return request_blob(outbuf, size, hdrlen, outfd, infd, id,
1339 ibuf);
1342 ibuf = calloc(1, sizeof(*ibuf));
1343 if (ibuf == NULL)
1344 return got_error_from_errno("calloc");
1346 if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_fds) == -1) {
1347 err = got_error_from_errno("socketpair");
1348 free(ibuf);
1349 return err;
1352 pid = fork();
1353 if (pid == -1) {
1354 err = got_error_from_errno("fork");
1355 free(ibuf);
1356 return err;
1358 else if (pid == 0) {
1359 got_privsep_exec_child(imsg_fds, GOT_PATH_PROG_READ_BLOB,
1360 repo->path);
1361 /* not reached */
1364 if (close(imsg_fds[1]) == -1) {
1365 err = got_error_from_errno("close");
1366 free(ibuf);
1367 return err;
1369 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_BLOB].imsg_fd =
1370 imsg_fds[0];
1371 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_BLOB].pid = pid;
1372 imsg_init(ibuf, imsg_fds[0]);
1373 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_BLOB].ibuf = ibuf;
1375 return request_blob(outbuf, size, hdrlen, outfd, infd, id, ibuf);
1378 static const struct got_error *
1379 open_blob(struct got_blob_object **blob, struct got_repository *repo,
1380 struct got_object_id *id, size_t blocksize)
1382 const struct got_error *err = NULL;
1383 struct got_packidx *packidx = NULL;
1384 int idx;
1385 char *path_packfile = NULL;
1386 uint8_t *outbuf;
1387 int outfd;
1388 size_t size, hdrlen;
1389 struct stat sb;
1391 *blob = calloc(1, sizeof(**blob));
1392 if (*blob == NULL)
1393 return got_error_from_errno("calloc");
1395 outfd = got_opentempfd();
1396 if (outfd == -1)
1397 return got_error_from_errno("got_opentempfd");
1399 (*blob)->read_buf = malloc(blocksize);
1400 if ((*blob)->read_buf == NULL) {
1401 err = got_error_from_errno("malloc");
1402 goto done;
1405 err = got_repo_search_packidx(&packidx, &idx, repo, id);
1406 if (err == NULL) {
1407 struct got_pack *pack = NULL;
1409 err = got_packidx_get_packfile_path(&path_packfile,
1410 packidx->path_packidx);
1411 if (err)
1412 goto done;
1414 pack = got_repo_get_cached_pack(repo, path_packfile);
1415 if (pack == NULL) {
1416 err = got_repo_cache_pack(&pack, repo, path_packfile,
1417 packidx);
1418 if (err)
1419 goto done;
1421 err = read_packed_blob_privsep(&outbuf, &size, &hdrlen, outfd,
1422 pack, packidx, idx, id);
1423 } else if (err->code == GOT_ERR_NO_OBJ) {
1424 int infd;
1426 err = got_object_open_loose_fd(&infd, id, repo);
1427 if (err)
1428 goto done;
1429 err = read_blob_privsep(&outbuf, &size, &hdrlen, outfd, infd,
1430 id, repo);
1432 if (err)
1433 goto done;
1435 if (hdrlen > size) {
1436 err = got_error(GOT_ERR_BAD_OBJ_HDR);
1437 goto done;
1440 if (outbuf) {
1441 if (close(outfd) == -1 && err == NULL)
1442 err = got_error_from_errno("close");
1443 outfd = -1;
1444 (*blob)->f = fmemopen(outbuf, size, "rb");
1445 if ((*blob)->f == NULL) {
1446 err = got_error_from_errno("fmemopen");
1447 free(outbuf);
1448 goto done;
1450 (*blob)->data = outbuf;
1451 } else {
1452 if (fstat(outfd, &sb) == -1) {
1453 err = got_error_from_errno("fstat");
1454 goto done;
1457 if (sb.st_size != size) {
1458 err = got_error(GOT_ERR_PRIVSEP_LEN);
1459 goto done;
1462 (*blob)->f = fdopen(outfd, "rb");
1463 if ((*blob)->f == NULL) {
1464 err = got_error_from_errno("fdopen");
1465 close(outfd);
1466 outfd = -1;
1467 goto done;
1471 (*blob)->hdrlen = hdrlen;
1472 (*blob)->blocksize = blocksize;
1473 memcpy(&(*blob)->id.sha1, id->sha1, SHA1_DIGEST_LENGTH);
1475 done:
1476 free(path_packfile);
1477 if (err) {
1478 if (*blob) {
1479 got_object_blob_close(*blob);
1480 *blob = NULL;
1481 } else if (outfd != -1)
1482 close(outfd);
1484 return err;
1487 const struct got_error *
1488 got_object_open_as_blob(struct got_blob_object **blob,
1489 struct got_repository *repo, struct got_object_id *id,
1490 size_t blocksize)
1492 return open_blob(blob, repo, id, blocksize);
1495 const struct got_error *
1496 got_object_blob_open(struct got_blob_object **blob,
1497 struct got_repository *repo, struct got_object *obj, size_t blocksize)
1499 return open_blob(blob, repo, got_object_get_id(obj), blocksize);
1502 const struct got_error *
1503 got_object_blob_close(struct got_blob_object *blob)
1505 const struct got_error *err = NULL;
1506 free(blob->read_buf);
1507 if (blob->f && fclose(blob->f) == EOF)
1508 err = got_error_from_errno("fclose");
1509 free(blob->data);
1510 free(blob);
1511 return err;
1514 void
1515 got_object_blob_rewind(struct got_blob_object *blob)
1517 if (blob->f)
1518 rewind(blob->f);
1521 char *
1522 got_object_blob_id_str(struct got_blob_object *blob, char *buf, size_t size)
1524 return got_sha1_digest_to_str(blob->id.sha1, buf, size);
1527 size_t
1528 got_object_blob_get_hdrlen(struct got_blob_object *blob)
1530 return blob->hdrlen;
1533 const uint8_t *
1534 got_object_blob_get_read_buf(struct got_blob_object *blob)
1536 return blob->read_buf;
1539 const struct got_error *
1540 got_object_blob_read_block(size_t *outlenp, struct got_blob_object *blob)
1542 size_t n;
1544 n = fread(blob->read_buf, 1, blob->blocksize, blob->f);
1545 if (n == 0 && ferror(blob->f))
1546 return got_ferror(blob->f, GOT_ERR_IO);
1547 *outlenp = n;
1548 return NULL;
1551 const struct got_error *
1552 got_object_blob_dump_to_file(off_t *filesize, int *nlines,
1553 off_t **line_offsets, FILE *outfile, struct got_blob_object *blob)
1555 const struct got_error *err = NULL;
1556 size_t n, len, hdrlen;
1557 const uint8_t *buf;
1558 int i;
1559 const int alloc_chunksz = 512;
1560 size_t nalloc = 0;
1561 off_t off = 0, total_len = 0;
1563 if (line_offsets)
1564 *line_offsets = NULL;
1565 if (filesize)
1566 *filesize = 0;
1567 if (nlines)
1568 *nlines = 0;
1570 hdrlen = got_object_blob_get_hdrlen(blob);
1571 do {
1572 err = got_object_blob_read_block(&len, blob);
1573 if (err)
1574 return err;
1575 if (len == 0)
1576 break;
1577 buf = got_object_blob_get_read_buf(blob);
1578 i = hdrlen;
1579 if (nlines) {
1580 if (line_offsets && *line_offsets == NULL) {
1581 /* Have some data but perhaps no '\n'. */
1582 *nlines = 1;
1583 nalloc = alloc_chunksz;
1584 *line_offsets = calloc(nalloc,
1585 sizeof(**line_offsets));
1586 if (*line_offsets == NULL)
1587 return got_error_from_errno("calloc");
1589 /* Skip forward over end of first line. */
1590 while (i < len) {
1591 if (buf[i] == '\n')
1592 break;
1593 i++;
1596 /* Scan '\n' offsets in remaining chunk of data. */
1597 while (i < len) {
1598 if (buf[i] != '\n') {
1599 i++;
1600 continue;
1602 (*nlines)++;
1603 if (line_offsets && nalloc < *nlines) {
1604 size_t n = *nlines + alloc_chunksz;
1605 off_t *o = recallocarray(*line_offsets,
1606 nalloc, n, sizeof(**line_offsets));
1607 if (o == NULL) {
1608 free(*line_offsets);
1609 *line_offsets = NULL;
1610 return got_error_from_errno(
1611 "recallocarray");
1613 *line_offsets = o;
1614 nalloc = n;
1616 if (line_offsets) {
1617 off = total_len + i - hdrlen + 1;
1618 (*line_offsets)[*nlines - 1] = off;
1620 i++;
1623 /* Skip blob object header first time around. */
1624 n = fwrite(buf + hdrlen, 1, len - hdrlen, outfile);
1625 if (n != len - hdrlen)
1626 return got_ferror(outfile, GOT_ERR_IO);
1627 total_len += len - hdrlen;
1628 hdrlen = 0;
1629 } while (len != 0);
1631 if (fflush(outfile) != 0)
1632 return got_error_from_errno("fflush");
1633 rewind(outfile);
1635 if (filesize)
1636 *filesize = total_len;
1638 return NULL;
1641 static const struct got_error *
1642 request_packed_tag(struct got_tag_object **tag, struct got_pack *pack,
1643 int pack_idx, struct got_object_id *id)
1645 const struct got_error *err = NULL;
1647 err = got_privsep_send_tag_req(pack->privsep_child->ibuf, -1, id,
1648 pack_idx);
1649 if (err)
1650 return err;
1652 return got_privsep_recv_tag(tag, pack->privsep_child->ibuf);
1655 static const struct got_error *
1656 read_packed_tag_privsep(struct got_tag_object **tag,
1657 struct got_pack *pack, struct got_packidx *packidx, int idx,
1658 struct got_object_id *id)
1660 const struct got_error *err = NULL;
1662 if (pack->privsep_child)
1663 return request_packed_tag(tag, pack, idx, id);
1665 err = start_pack_privsep_child(pack, packidx);
1666 if (err)
1667 return err;
1669 return request_packed_tag(tag, pack, idx, id);
1672 static const struct got_error *
1673 request_tag(struct got_tag_object **tag, struct got_repository *repo,
1674 int fd, struct got_object_id *id)
1676 const struct got_error *err = NULL;
1677 struct imsgbuf *ibuf;
1679 ibuf = repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TAG].ibuf;
1681 err = got_privsep_send_tag_req(ibuf, fd, id, -1);
1682 if (err)
1683 return err;
1685 return got_privsep_recv_tag(tag, ibuf);
1688 static const struct got_error *
1689 read_tag_privsep(struct got_tag_object **tag, int obj_fd,
1690 struct got_object_id *id, struct got_repository *repo)
1692 const struct got_error *err;
1693 int imsg_fds[2];
1694 pid_t pid;
1695 struct imsgbuf *ibuf;
1697 if (repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TAG].imsg_fd != -1)
1698 return request_tag(tag, repo, obj_fd, id);
1700 ibuf = calloc(1, sizeof(*ibuf));
1701 if (ibuf == NULL)
1702 return got_error_from_errno("calloc");
1704 if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_fds) == -1) {
1705 err = got_error_from_errno("socketpair");
1706 free(ibuf);
1707 return err;
1710 pid = fork();
1711 if (pid == -1) {
1712 err = got_error_from_errno("fork");
1713 free(ibuf);
1714 return err;
1716 else if (pid == 0) {
1717 got_privsep_exec_child(imsg_fds, GOT_PATH_PROG_READ_TAG,
1718 repo->path);
1719 /* not reached */
1722 if (close(imsg_fds[1]) == -1) {
1723 err = got_error_from_errno("close");
1724 free(ibuf);
1725 return err;
1727 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TAG].imsg_fd =
1728 imsg_fds[0];
1729 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TAG].pid = pid;
1730 imsg_init(ibuf, imsg_fds[0]);
1731 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TAG].ibuf = ibuf;
1733 return request_tag(tag, repo, obj_fd, id);
1736 static const struct got_error *
1737 open_tag(struct got_tag_object **tag, struct got_repository *repo,
1738 struct got_object_id *id, int check_cache)
1740 const struct got_error *err = NULL;
1741 struct got_packidx *packidx = NULL;
1742 int idx;
1743 char *path_packfile = NULL;
1744 struct got_object *obj = NULL;
1745 int obj_type = GOT_OBJ_TYPE_ANY;
1747 if (check_cache) {
1748 *tag = got_repo_get_cached_tag(repo, id);
1749 if (*tag != NULL) {
1750 (*tag)->refcnt++;
1751 return NULL;
1753 } else
1754 *tag = NULL;
1756 err = got_repo_search_packidx(&packidx, &idx, repo, id);
1757 if (err == NULL) {
1758 struct got_pack *pack = NULL;
1760 err = got_packidx_get_packfile_path(&path_packfile,
1761 packidx->path_packidx);
1762 if (err)
1763 return err;
1765 pack = got_repo_get_cached_pack(repo, path_packfile);
1766 if (pack == NULL) {
1767 err = got_repo_cache_pack(&pack, repo, path_packfile,
1768 packidx);
1769 if (err)
1770 goto done;
1773 /* Beware of "lightweight" tags: Check object type first. */
1774 err = read_packed_object_privsep(&obj, repo, pack, packidx,
1775 idx, id);
1776 if (err)
1777 goto done;
1778 obj_type = obj->type;
1779 got_object_close(obj);
1780 if (obj_type != GOT_OBJ_TYPE_TAG) {
1781 err = got_error(GOT_ERR_OBJ_TYPE);
1782 goto done;
1784 err = read_packed_tag_privsep(tag, pack, packidx, idx, id);
1785 } else if (err->code == GOT_ERR_NO_OBJ) {
1786 int fd;
1788 err = got_object_open_loose_fd(&fd, id, repo);
1789 if (err)
1790 return err;
1791 err = got_object_read_header_privsep(&obj, id, repo, fd);
1792 if (err)
1793 return err;
1794 obj_type = obj->type;
1795 got_object_close(obj);
1796 if (obj_type != GOT_OBJ_TYPE_TAG)
1797 return got_error(GOT_ERR_OBJ_TYPE);
1799 err = got_object_open_loose_fd(&fd, id, repo);
1800 if (err)
1801 return err;
1802 err = read_tag_privsep(tag, fd, id, repo);
1805 if (err == NULL) {
1806 (*tag)->refcnt++;
1807 err = got_repo_cache_tag(repo, id, *tag);
1809 done:
1810 free(path_packfile);
1811 return err;
1814 const struct got_error *
1815 got_object_open_as_tag(struct got_tag_object **tag,
1816 struct got_repository *repo, struct got_object_id *id)
1818 *tag = got_repo_get_cached_tag(repo, id);
1819 if (*tag != NULL) {
1820 (*tag)->refcnt++;
1821 return NULL;
1824 return open_tag(tag, repo, id, 0);
1827 const struct got_error *
1828 got_object_tag_open(struct got_tag_object **tag,
1829 struct got_repository *repo, struct got_object *obj)
1831 return open_tag(tag, repo, got_object_get_id(obj), 1);
1834 const char *
1835 got_object_tag_get_name(struct got_tag_object *tag)
1837 return tag->tag;
1840 int
1841 got_object_tag_get_object_type(struct got_tag_object *tag)
1843 return tag->obj_type;
1846 struct got_object_id *
1847 got_object_tag_get_object_id(struct got_tag_object *tag)
1849 return &tag->id;
1852 time_t
1853 got_object_tag_get_tagger_time(struct got_tag_object *tag)
1855 return tag->tagger_time;
1858 time_t
1859 got_object_tag_get_tagger_gmtoff(struct got_tag_object *tag)
1861 return tag->tagger_gmtoff;
1864 const char *
1865 got_object_tag_get_tagger(struct got_tag_object *tag)
1867 return tag->tagger;
1870 const char *
1871 got_object_tag_get_message(struct got_tag_object *tag)
1873 return tag->tagmsg;
1876 static struct got_tree_entry *
1877 find_entry_by_name(struct got_tree_object *tree, const char *name, size_t len)
1879 int i;
1881 /* Note that tree entries are sorted in strncmp() order. */
1882 for (i = 0; i < tree->nentries; i++) {
1883 struct got_tree_entry *te = &tree->entries[i];
1884 int cmp = strncmp(te->name, name, len);
1885 if (cmp < 0)
1886 continue;
1887 if (cmp > 0)
1888 break;
1889 if (te->name[len] == '\0')
1890 return te;
1892 return NULL;
1895 struct got_tree_entry *
1896 got_object_tree_find_entry(struct got_tree_object *tree, const char *name)
1898 return find_entry_by_name(tree, name, strlen(name));
1901 const struct got_error *
1902 got_object_tree_find_path(struct got_object_id **id, mode_t *mode,
1903 struct got_repository *repo, struct got_tree_object *tree,
1904 const char *path)
1906 const struct got_error *err = NULL;
1907 struct got_tree_object *subtree = NULL;
1908 struct got_tree_entry *te = NULL;
1909 const char *seg, *s;
1910 size_t seglen;
1912 *id = NULL;
1914 s = path;
1915 while (s[0] == '/')
1916 s++;
1917 seg = s;
1918 seglen = 0;
1919 subtree = tree;
1920 while (*s) {
1921 struct got_tree_object *next_tree;
1923 if (*s != '/') {
1924 s++;
1925 seglen++;
1926 if (*s)
1927 continue;
1930 te = find_entry_by_name(subtree, seg, seglen);
1931 if (te == NULL) {
1932 err = got_error_path(path, GOT_ERR_NO_TREE_ENTRY);
1933 goto done;
1936 if (*s == '\0')
1937 break;
1939 seg = s + 1;
1940 seglen = 0;
1941 s++;
1942 if (*s) {
1943 err = got_object_open_as_tree(&next_tree, repo,
1944 &te->id);
1945 te = NULL;
1946 if (err)
1947 goto done;
1948 if (subtree != tree)
1949 got_object_tree_close(subtree);
1950 subtree = next_tree;
1954 if (te) {
1955 *id = got_object_id_dup(&te->id);
1956 if (*id == NULL)
1957 return got_error_from_errno("got_object_id_dup");
1958 if (mode)
1959 *mode = te->mode;
1960 } else
1961 err = got_error_path(path, GOT_ERR_NO_TREE_ENTRY);
1962 done:
1963 if (subtree && subtree != tree)
1964 got_object_tree_close(subtree);
1965 return err;
1967 const struct got_error *
1968 got_object_id_by_path(struct got_object_id **id, struct got_repository *repo,
1969 struct got_object_id *commit_id, const char *path)
1971 const struct got_error *err = NULL;
1972 struct got_commit_object *commit = NULL;
1973 struct got_tree_object *tree = NULL;
1975 *id = NULL;
1977 err = got_object_open_as_commit(&commit, repo, commit_id);
1978 if (err)
1979 goto done;
1981 /* Handle opening of root of commit's tree. */
1982 if (got_path_is_root_dir(path)) {
1983 *id = got_object_id_dup(commit->tree_id);
1984 if (*id == NULL)
1985 err = got_error_from_errno("got_object_id_dup");
1986 } else {
1987 err = got_object_open_as_tree(&tree, repo, commit->tree_id);
1988 if (err)
1989 goto done;
1990 err = got_object_tree_find_path(id, NULL, repo, tree, path);
1992 done:
1993 if (commit)
1994 got_object_commit_close(commit);
1995 if (tree)
1996 got_object_tree_close(tree);
1997 return err;
2001 * Normalize file mode bits to avoid false positive tree entry differences
2002 * in case tree entries have unexpected mode bits set.
2004 static mode_t
2005 normalize_mode_for_comparison(mode_t mode)
2008 * For directories, the only relevant bit is the IFDIR bit.
2009 * This allows us to detect paths changing from a directory
2010 * to a file and vice versa.
2012 if (S_ISDIR(mode))
2013 return mode & S_IFDIR;
2016 * For symlinks, the only relevant bit is the IFLNK bit.
2017 * This allows us to detect paths changing from a symlinks
2018 * to a file or directory and vice versa.
2020 if (S_ISLNK(mode))
2021 return mode & S_IFLNK;
2023 /* For files, the only change we care about is the executable bit. */
2024 return mode & S_IXUSR;
2027 const struct got_error *
2028 got_object_tree_path_changed(int *changed,
2029 struct got_tree_object *tree01, struct got_tree_object *tree02,
2030 const char *path, struct got_repository *repo)
2032 const struct got_error *err = NULL;
2033 struct got_tree_object *tree1 = NULL, *tree2 = NULL;
2034 struct got_tree_entry *te1 = NULL, *te2 = NULL;
2035 const char *seg, *s;
2036 size_t seglen;
2038 *changed = 0;
2040 /* We not do support comparing the root path. */
2041 if (got_path_is_root_dir(path))
2042 return got_error_path(path, GOT_ERR_BAD_PATH);
2044 tree1 = tree01;
2045 tree2 = tree02;
2046 s = path;
2047 while (*s == '/')
2048 s++;
2049 seg = s;
2050 seglen = 0;
2051 while (*s) {
2052 struct got_tree_object *next_tree1, *next_tree2;
2053 mode_t mode1, mode2;
2055 if (*s != '/') {
2056 s++;
2057 seglen++;
2058 if (*s)
2059 continue;
2062 te1 = find_entry_by_name(tree1, seg, seglen);
2063 if (te1 == NULL) {
2064 err = got_error(GOT_ERR_NO_OBJ);
2065 goto done;
2068 if (tree2)
2069 te2 = find_entry_by_name(tree2, seg, seglen);
2071 if (te2) {
2072 mode1 = normalize_mode_for_comparison(te1->mode);
2073 mode2 = normalize_mode_for_comparison(te2->mode);
2074 if (mode1 != mode2) {
2075 *changed = 1;
2076 goto done;
2079 if (got_object_id_cmp(&te1->id, &te2->id) == 0) {
2080 *changed = 0;
2081 goto done;
2085 if (*s == '\0') { /* final path element */
2086 *changed = 1;
2087 goto done;
2090 seg = s + 1;
2091 s++;
2092 seglen = 0;
2093 if (*s) {
2094 err = got_object_open_as_tree(&next_tree1, repo,
2095 &te1->id);
2096 te1 = NULL;
2097 if (err)
2098 goto done;
2099 if (tree1 != tree01)
2100 got_object_tree_close(tree1);
2101 tree1 = next_tree1;
2103 if (te2) {
2104 err = got_object_open_as_tree(&next_tree2, repo,
2105 &te2->id);
2106 te2 = NULL;
2107 if (err)
2108 goto done;
2109 if (tree2 != tree02)
2110 got_object_tree_close(tree2);
2111 tree2 = next_tree2;
2112 } else if (tree2) {
2113 if (tree2 != tree02)
2114 got_object_tree_close(tree2);
2115 tree2 = NULL;
2119 done:
2120 if (tree1 && tree1 != tree01)
2121 got_object_tree_close(tree1);
2122 if (tree2 && tree2 != tree02)
2123 got_object_tree_close(tree2);
2124 return err;
2127 const struct got_error *
2128 got_object_tree_entry_dup(struct got_tree_entry **new_te,
2129 struct got_tree_entry *te)
2131 const struct got_error *err = NULL;
2133 *new_te = calloc(1, sizeof(**new_te));
2134 if (*new_te == NULL)
2135 return got_error_from_errno("calloc");
2137 (*new_te)->mode = te->mode;
2138 memcpy((*new_te)->name, te->name, sizeof((*new_te)->name));
2139 memcpy(&(*new_te)->id, &te->id, sizeof((*new_te)->id));
2140 return err;
2143 int
2144 got_object_tree_entry_is_submodule(struct got_tree_entry *te)
2146 return (te->mode & S_IFMT) == (S_IFDIR | S_IFLNK);
2149 int
2150 got_object_tree_entry_is_symlink(struct got_tree_entry *te)
2152 /* S_IFDIR check avoids confusing symlinks with submodules. */
2153 return ((te->mode & (S_IFDIR | S_IFLNK)) == S_IFLNK);
2156 static const struct got_error *
2157 resolve_symlink(char **link_target, const char *path,
2158 struct got_object_id *commit_id, struct got_repository *repo)
2160 const struct got_error *err = NULL;
2161 char buf[PATH_MAX];
2162 char *name, *parent_path = NULL;
2163 struct got_object_id *tree_obj_id = NULL;
2164 struct got_tree_object *tree = NULL;
2165 struct got_tree_entry *te = NULL;
2167 *link_target = NULL;
2169 if (strlcpy(buf, path, sizeof(buf)) >= sizeof(buf))
2170 return got_error(GOT_ERR_NO_SPACE);
2172 name = basename(buf);
2173 if (name == NULL)
2174 return got_error_from_errno2("basename", path);
2176 err = got_path_dirname(&parent_path, path);
2177 if (err)
2178 return err;
2180 err = got_object_id_by_path(&tree_obj_id, repo, commit_id,
2181 parent_path);
2182 if (err) {
2183 if (err->code == GOT_ERR_NO_TREE_ENTRY) {
2184 /* Display the complete path in error message. */
2185 err = got_error_path(path, err->code);
2187 goto done;
2190 err = got_object_open_as_tree(&tree, repo, tree_obj_id);
2191 if (err)
2192 goto done;
2194 te = got_object_tree_find_entry(tree, name);
2195 if (te == NULL) {
2196 err = got_error_path(path, GOT_ERR_NO_TREE_ENTRY);
2197 goto done;
2200 if (got_object_tree_entry_is_symlink(te)) {
2201 err = got_tree_entry_get_symlink_target(link_target, te, repo);
2202 if (err)
2203 goto done;
2204 if (!got_path_is_absolute(*link_target)) {
2205 char *abspath;
2206 if (asprintf(&abspath, "%s/%s", parent_path,
2207 *link_target) == -1) {
2208 err = got_error_from_errno("asprintf");
2209 goto done;
2211 free(*link_target);
2212 *link_target = malloc(PATH_MAX);
2213 if (*link_target == NULL) {
2214 err = got_error_from_errno("malloc");
2215 goto done;
2217 err = got_canonpath(abspath, *link_target, PATH_MAX);
2218 free(abspath);
2219 if (err)
2220 goto done;
2223 done:
2224 free(tree_obj_id);
2225 if (tree)
2226 got_object_tree_close(tree);
2227 if (err) {
2228 free(*link_target);
2229 *link_target = NULL;
2231 return err;
2234 const struct got_error *
2235 got_object_resolve_symlinks(char **link_target, const char *path,
2236 struct got_object_id *commit_id, struct got_repository *repo)
2238 const struct got_error *err = NULL;
2239 char *next_target = NULL;
2240 int max_recursion = 40; /* matches Git */
2242 *link_target = NULL;
2244 do {
2245 err = resolve_symlink(&next_target,
2246 *link_target ? *link_target : path, commit_id, repo);
2247 if (err)
2248 break;
2249 if (next_target) {
2250 free(*link_target);
2251 if (--max_recursion == 0) {
2252 err = got_error_path(path, GOT_ERR_RECURSION);
2253 *link_target = NULL;
2254 break;
2256 *link_target = next_target;
2258 } while (next_target);
2260 return err;
2263 const struct got_error *
2264 got_traverse_packed_commits(struct got_object_id_queue *traversed_commits,
2265 struct got_object_id *commit_id, const char *path,
2266 struct got_repository *repo)
2268 const struct got_error *err = NULL;
2269 struct got_pack *pack = NULL;
2270 struct got_packidx *packidx = NULL;
2271 char *path_packfile = NULL;
2272 struct got_commit_object *changed_commit = NULL;
2273 struct got_object_id *changed_commit_id = NULL;
2274 int idx;
2276 err = got_repo_search_packidx(&packidx, &idx, repo, commit_id);
2277 if (err) {
2278 if (err->code != GOT_ERR_NO_OBJ)
2279 return err;
2280 return NULL;
2283 err = got_packidx_get_packfile_path(&path_packfile,
2284 packidx->path_packidx);
2285 if (err)
2286 return err;
2288 pack = got_repo_get_cached_pack(repo, path_packfile);
2289 if (pack == NULL) {
2290 err = got_repo_cache_pack(&pack, repo, path_packfile, packidx);
2291 if (err)
2292 goto done;
2295 if (pack->privsep_child == NULL) {
2296 err = start_pack_privsep_child(pack, packidx);
2297 if (err)
2298 goto done;
2301 err = got_privsep_send_commit_traversal_request(
2302 pack->privsep_child->ibuf, commit_id, idx, path);
2303 if (err)
2304 goto done;
2306 err = got_privsep_recv_traversed_commits(&changed_commit,
2307 &changed_commit_id, traversed_commits, pack->privsep_child->ibuf);
2308 if (err)
2309 goto done;
2311 if (changed_commit) {
2313 * Cache the commit in which the path was changed.
2314 * This commit might be opened again soon.
2316 changed_commit->refcnt++;
2317 err = got_repo_cache_commit(repo, changed_commit_id,
2318 changed_commit);
2319 got_object_commit_close(changed_commit);
2321 done:
2322 free(path_packfile);
2323 free(changed_commit_id);
2324 return err;