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 const struct got_error *
532 got_object_raw_open(struct got_raw_object **obj, struct got_repository *repo,
533 struct got_object_id *id, size_t blocksize)
535 const struct got_error *err = NULL;
536 struct got_packidx *packidx = NULL;
537 int idx;
538 uint8_t *outbuf = NULL;
539 int outfd = -1;
540 off_t size = 0;
541 size_t hdrlen = 0;
542 char *path_packfile = NULL;
544 *obj = NULL;
546 outfd = got_opentempfd();
547 if (outfd == -1)
548 return got_error_from_errno("got_opentempfd");
550 err = got_repo_search_packidx(&packidx, &idx, repo, id);
551 if (err == NULL) {
552 struct got_pack *pack = NULL;
554 err = got_packidx_get_packfile_path(&path_packfile,
555 packidx->path_packidx);
556 if (err)
557 goto done;
559 pack = got_repo_get_cached_pack(repo, path_packfile);
560 if (pack == NULL) {
561 err = got_repo_cache_pack(&pack, repo, path_packfile,
562 packidx);
563 if (err)
564 goto done;
566 err = read_packed_object_raw_privsep(&outbuf, &size, &hdrlen,
567 outfd, pack, packidx, idx, id);
568 } else if (err->code == GOT_ERR_NO_OBJ) {
569 int fd;
571 err = got_object_open_loose_fd(&fd, id, repo);
572 if (err)
573 goto done;
574 err = read_object_raw_privsep(&outbuf, &size, &hdrlen, outfd,
575 id, repo, fd);
578 *obj = calloc(1, sizeof(**obj));
579 if (*obj == NULL) {
580 err = got_error_from_errno("calloc");
581 goto done;
584 (*obj)->read_buf = malloc(blocksize);
585 if ((*obj)->read_buf == NULL) {
586 err = got_error_from_errno("malloc");
587 goto done;
590 if (outbuf) {
591 if (close(outfd) == -1) {
592 err = got_error_from_errno("close");
593 goto done;
595 outfd = -1;
596 (*obj)->f = fmemopen(outbuf, hdrlen + size, "r");
597 if ((*obj)->f == NULL) {
598 err = got_error_from_errno("fdopen");
599 goto done;
601 (*obj)->data = outbuf;
602 } else {
603 struct stat sb;
604 if (fstat(outfd, &sb) == -1) {
605 err = got_error_from_errno("fstat");
606 goto done;
609 if (sb.st_size != hdrlen + size) {
610 err = got_error(GOT_ERR_PRIVSEP_LEN);
611 goto done;
614 (*obj)->f = fdopen(outfd, "r");
615 if ((*obj)->f == NULL) {
616 err = got_error_from_errno("fdopen");
617 goto done;
619 outfd = -1;
620 (*obj)->data = NULL;
622 (*obj)->hdrlen = hdrlen;
623 (*obj)->size = size;
624 (*obj)->blocksize = blocksize;
625 done:
626 free(path_packfile);
627 if (err) {
628 if (*obj) {
629 got_object_raw_close(*obj);
630 *obj = NULL;
632 if (outfd != -1)
633 close(outfd);
634 free(outbuf);
636 return err;
639 void
640 got_object_raw_rewind(struct got_raw_object *obj)
642 if (obj->f)
643 rewind(obj->f);
646 size_t
647 got_object_raw_get_hdrlen(struct got_raw_object *obj)
649 return obj->hdrlen;
652 const uint8_t *
653 got_object_raw_get_read_buf(struct got_raw_object *obj)
655 return obj->read_buf;
658 const struct got_error *
659 got_object_raw_read_block(size_t *outlenp, struct got_raw_object *obj)
661 size_t n;
663 n = fread(obj->read_buf, 1, obj->blocksize, obj->f);
664 if (n == 0 && ferror(obj->f))
665 return got_ferror(obj->f, GOT_ERR_IO);
666 *outlenp = n;
667 return NULL;
670 const struct got_error *
671 got_object_raw_close(struct got_raw_object *obj)
673 const struct got_error *err = NULL;
675 free(obj->read_buf);
676 if (obj->f != NULL && fclose(obj->f) == EOF && err == NULL)
677 err = got_error_from_errno("fclose");
678 free(obj->data);
679 free(obj);
680 return err;
683 const struct got_error *
684 got_object_open_by_id_str(struct got_object **obj, struct got_repository *repo,
685 const char *id_str)
687 struct got_object_id id;
689 if (!got_parse_sha1_digest(id.sha1, id_str))
690 return got_error_path(id_str, GOT_ERR_BAD_OBJ_ID_STR);
692 return got_object_open(obj, repo, &id);
695 const struct got_error *
696 got_object_resolve_id_str(struct got_object_id **id,
697 struct got_repository *repo, const char *id_str)
699 const struct got_error *err = NULL;
700 struct got_object *obj;
702 err = got_object_open_by_id_str(&obj, repo, id_str);
703 if (err)
704 return err;
706 *id = got_object_id_dup(got_object_get_id(obj));
707 got_object_close(obj);
708 if (*id == NULL)
709 return got_error_from_errno("got_object_id_dup");
711 return NULL;
714 static const struct got_error *
715 request_packed_commit(struct got_commit_object **commit, struct got_pack *pack,
716 int pack_idx, struct got_object_id *id)
718 const struct got_error *err = NULL;
720 err = got_privsep_send_commit_req(pack->privsep_child->ibuf, -1, id,
721 pack_idx);
722 if (err)
723 return err;
725 err = got_privsep_recv_commit(commit, pack->privsep_child->ibuf);
726 if (err)
727 return err;
729 (*commit)->flags |= GOT_COMMIT_FLAG_PACKED;
730 return NULL;
733 static const struct got_error *
734 read_packed_commit_privsep(struct got_commit_object **commit,
735 struct got_pack *pack, struct got_packidx *packidx, int idx,
736 struct got_object_id *id)
738 const struct got_error *err = NULL;
740 if (pack->privsep_child)
741 return request_packed_commit(commit, pack, idx, id);
743 err = start_pack_privsep_child(pack, packidx);
744 if (err)
745 return err;
747 return request_packed_commit(commit, pack, idx, id);
750 static const struct got_error *
751 request_commit(struct got_commit_object **commit, struct got_repository *repo,
752 int fd, struct got_object_id *id)
754 const struct got_error *err = NULL;
755 struct imsgbuf *ibuf;
757 ibuf = repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_COMMIT].ibuf;
759 err = got_privsep_send_commit_req(ibuf, fd, id, -1);
760 if (err)
761 return err;
763 return got_privsep_recv_commit(commit, ibuf);
766 static const struct got_error *
767 read_commit_privsep(struct got_commit_object **commit, int obj_fd,
768 struct got_object_id *id, struct got_repository *repo)
770 const struct got_error *err;
771 int imsg_fds[2];
772 pid_t pid;
773 struct imsgbuf *ibuf;
775 if (repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_COMMIT].imsg_fd != -1)
776 return request_commit(commit, repo, obj_fd, id);
778 ibuf = calloc(1, sizeof(*ibuf));
779 if (ibuf == NULL)
780 return got_error_from_errno("calloc");
782 if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_fds) == -1) {
783 err = got_error_from_errno("socketpair");
784 free(ibuf);
785 return err;
788 pid = fork();
789 if (pid == -1) {
790 err = got_error_from_errno("fork");
791 free(ibuf);
792 return err;
794 else if (pid == 0) {
795 got_privsep_exec_child(imsg_fds, GOT_PATH_PROG_READ_COMMIT,
796 repo->path);
797 /* not reached */
800 if (close(imsg_fds[1]) == -1) {
801 err = got_error_from_errno("close");
802 free(ibuf);
803 return err;
805 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_COMMIT].imsg_fd =
806 imsg_fds[0];
807 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_COMMIT].pid = pid;
808 imsg_init(ibuf, imsg_fds[0]);
809 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_COMMIT].ibuf = ibuf;
811 return request_commit(commit, repo, obj_fd, id);
815 static const struct got_error *
816 open_commit(struct got_commit_object **commit,
817 struct got_repository *repo, struct got_object_id *id, int check_cache)
819 const struct got_error *err = NULL;
820 struct got_packidx *packidx = NULL;
821 int idx;
822 char *path_packfile = NULL;
824 if (check_cache) {
825 *commit = got_repo_get_cached_commit(repo, id);
826 if (*commit != NULL) {
827 (*commit)->refcnt++;
828 return NULL;
830 } else
831 *commit = NULL;
833 err = got_repo_search_packidx(&packidx, &idx, repo, id);
834 if (err == NULL) {
835 struct got_pack *pack = NULL;
837 err = got_packidx_get_packfile_path(&path_packfile,
838 packidx->path_packidx);
839 if (err)
840 return err;
842 pack = got_repo_get_cached_pack(repo, path_packfile);
843 if (pack == NULL) {
844 err = got_repo_cache_pack(&pack, repo, path_packfile,
845 packidx);
846 if (err)
847 goto done;
849 err = read_packed_commit_privsep(commit, pack,
850 packidx, idx, id);
851 } else if (err->code == GOT_ERR_NO_OBJ) {
852 int fd;
854 err = got_object_open_loose_fd(&fd, id, repo);
855 if (err)
856 return err;
857 err = read_commit_privsep(commit, fd, id, repo);
860 if (err == NULL) {
861 (*commit)->refcnt++;
862 err = got_repo_cache_commit(repo, id, *commit);
864 done:
865 free(path_packfile);
866 return err;
869 const struct got_error *
870 got_object_open_as_commit(struct got_commit_object **commit,
871 struct got_repository *repo, struct got_object_id *id)
873 *commit = got_repo_get_cached_commit(repo, id);
874 if (*commit != NULL) {
875 (*commit)->refcnt++;
876 return NULL;
879 return open_commit(commit, repo, id, 0);
882 const struct got_error *
883 got_object_commit_open(struct got_commit_object **commit,
884 struct got_repository *repo, struct got_object *obj)
886 return open_commit(commit, repo, got_object_get_id(obj), 1);
889 const struct got_error *
890 got_object_qid_alloc(struct got_object_qid **qid, struct got_object_id *id)
892 const struct got_error *err = NULL;
894 *qid = calloc(1, sizeof(**qid));
895 if (*qid == NULL)
896 return got_error_from_errno("calloc");
898 (*qid)->id = got_object_id_dup(id);
899 if ((*qid)->id == NULL) {
900 err = got_error_from_errno("got_object_id_dup");
901 got_object_qid_free(*qid);
902 *qid = NULL;
903 return err;
906 return NULL;
909 const struct got_error *
910 got_object_id_queue_copy(const struct got_object_id_queue *src,
911 struct got_object_id_queue *dest)
913 const struct got_error *err;
914 struct got_object_qid *qid;
916 STAILQ_FOREACH(qid, src, entry) {
917 struct got_object_qid *new;
918 /*
919 * Deep-copy the object ID only. Let the caller deal
920 * with setting up the new->data pointer if needed.
921 */
922 err = got_object_qid_alloc(&new, qid->id);
923 if (err) {
924 got_object_id_queue_free(dest);
925 return err;
927 STAILQ_INSERT_TAIL(dest, new, entry);
930 return NULL;
933 static const struct got_error *
934 request_packed_tree(struct got_tree_object **tree, struct got_pack *pack,
935 int pack_idx, struct got_object_id *id)
937 const struct got_error *err = NULL;
939 err = got_privsep_send_tree_req(pack->privsep_child->ibuf, -1, id,
940 pack_idx);
941 if (err)
942 return err;
944 return got_privsep_recv_tree(tree, pack->privsep_child->ibuf);
947 static const struct got_error *
948 read_packed_tree_privsep(struct got_tree_object **tree,
949 struct got_pack *pack, struct got_packidx *packidx, int idx,
950 struct got_object_id *id)
952 const struct got_error *err = NULL;
954 if (pack->privsep_child)
955 return request_packed_tree(tree, pack, idx, id);
957 err = start_pack_privsep_child(pack, packidx);
958 if (err)
959 return err;
961 return request_packed_tree(tree, pack, idx, id);
964 static const struct got_error *
965 request_tree(struct got_tree_object **tree, struct got_repository *repo,
966 int fd, struct got_object_id *id)
968 const struct got_error *err = NULL;
969 struct imsgbuf *ibuf;
971 ibuf = repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TREE].ibuf;
973 err = got_privsep_send_tree_req(ibuf, fd, id, -1);
974 if (err)
975 return err;
977 return got_privsep_recv_tree(tree, ibuf);
980 const struct got_error *
981 read_tree_privsep(struct got_tree_object **tree, int obj_fd,
982 struct got_object_id *id, struct got_repository *repo)
984 const struct got_error *err;
985 int imsg_fds[2];
986 pid_t pid;
987 struct imsgbuf *ibuf;
989 if (repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TREE].imsg_fd != -1)
990 return request_tree(tree, repo, obj_fd, id);
992 ibuf = calloc(1, sizeof(*ibuf));
993 if (ibuf == NULL)
994 return got_error_from_errno("calloc");
996 if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_fds) == -1) {
997 err = got_error_from_errno("socketpair");
998 free(ibuf);
999 return err;
1002 pid = fork();
1003 if (pid == -1) {
1004 err = got_error_from_errno("fork");
1005 free(ibuf);
1006 return err;
1008 else if (pid == 0) {
1009 got_privsep_exec_child(imsg_fds, GOT_PATH_PROG_READ_TREE,
1010 repo->path);
1011 /* not reached */
1014 if (close(imsg_fds[1]) == -1) {
1015 err = got_error_from_errno("close");
1016 free(ibuf);
1017 return err;
1019 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TREE].imsg_fd =
1020 imsg_fds[0];
1021 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TREE].pid = pid;
1022 imsg_init(ibuf, imsg_fds[0]);
1023 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TREE].ibuf = ibuf;
1026 return request_tree(tree, repo, obj_fd, id);
1029 static const struct got_error *
1030 open_tree(struct got_tree_object **tree, struct got_repository *repo,
1031 struct got_object_id *id, int check_cache)
1033 const struct got_error *err = NULL;
1034 struct got_packidx *packidx = NULL;
1035 int idx;
1036 char *path_packfile = NULL;
1038 if (check_cache) {
1039 *tree = got_repo_get_cached_tree(repo, id);
1040 if (*tree != NULL) {
1041 (*tree)->refcnt++;
1042 return NULL;
1044 } else
1045 *tree = NULL;
1047 err = got_repo_search_packidx(&packidx, &idx, repo, id);
1048 if (err == NULL) {
1049 struct got_pack *pack = NULL;
1051 err = got_packidx_get_packfile_path(&path_packfile,
1052 packidx->path_packidx);
1053 if (err)
1054 return err;
1056 pack = got_repo_get_cached_pack(repo, path_packfile);
1057 if (pack == NULL) {
1058 err = got_repo_cache_pack(&pack, repo, path_packfile,
1059 packidx);
1060 if (err)
1061 goto done;
1063 err = read_packed_tree_privsep(tree, pack,
1064 packidx, idx, id);
1065 } else if (err->code == GOT_ERR_NO_OBJ) {
1066 int fd;
1068 err = got_object_open_loose_fd(&fd, id, repo);
1069 if (err)
1070 return err;
1071 err = read_tree_privsep(tree, fd, id, repo);
1074 if (err == NULL) {
1075 (*tree)->refcnt++;
1076 err = got_repo_cache_tree(repo, id, *tree);
1078 done:
1079 free(path_packfile);
1080 return err;
1083 const struct got_error *
1084 got_object_open_as_tree(struct got_tree_object **tree,
1085 struct got_repository *repo, struct got_object_id *id)
1087 *tree = got_repo_get_cached_tree(repo, id);
1088 if (*tree != NULL) {
1089 (*tree)->refcnt++;
1090 return NULL;
1093 return open_tree(tree, repo, id, 0);
1096 const struct got_error *
1097 got_object_tree_open(struct got_tree_object **tree,
1098 struct got_repository *repo, struct got_object *obj)
1100 return open_tree(tree, repo, got_object_get_id(obj), 1);
1103 int
1104 got_object_tree_get_nentries(struct got_tree_object *tree)
1106 return tree->nentries;
1109 struct got_tree_entry *
1110 got_object_tree_get_first_entry(struct got_tree_object *tree)
1112 return got_object_tree_get_entry(tree, 0);
1115 struct got_tree_entry *
1116 got_object_tree_get_last_entry(struct got_tree_object *tree)
1118 return got_object_tree_get_entry(tree, tree->nentries - 1);
1121 struct got_tree_entry *
1122 got_object_tree_get_entry(struct got_tree_object *tree, int i)
1124 if (i < 0 || i >= tree->nentries)
1125 return NULL;
1126 return &tree->entries[i];
1129 mode_t
1130 got_tree_entry_get_mode(struct got_tree_entry *te)
1132 return te->mode;
1135 const char *
1136 got_tree_entry_get_name(struct got_tree_entry *te)
1138 return &te->name[0];
1141 struct got_object_id *
1142 got_tree_entry_get_id(struct got_tree_entry *te)
1144 return &te->id;
1147 const struct got_error *
1148 got_object_blob_read_to_str(char **s, struct got_blob_object *blob)
1150 const struct got_error *err = NULL;
1151 size_t len, totlen, hdrlen, offset;
1153 *s = NULL;
1155 hdrlen = got_object_blob_get_hdrlen(blob);
1156 totlen = 0;
1157 offset = 0;
1158 do {
1159 char *p;
1161 err = got_object_blob_read_block(&len, blob);
1162 if (err)
1163 return err;
1165 if (len == 0)
1166 break;
1168 totlen += len - hdrlen;
1169 p = realloc(*s, totlen + 1);
1170 if (p == NULL) {
1171 err = got_error_from_errno("realloc");
1172 free(*s);
1173 *s = NULL;
1174 return err;
1176 *s = p;
1177 /* Skip blob object header first time around. */
1178 memcpy(*s + offset,
1179 got_object_blob_get_read_buf(blob) + hdrlen, len - hdrlen);
1180 hdrlen = 0;
1181 offset = totlen;
1182 } while (len > 0);
1184 (*s)[totlen] = '\0';
1185 return NULL;
1188 const struct got_error *
1189 got_tree_entry_get_symlink_target(char **link_target, struct got_tree_entry *te,
1190 struct got_repository *repo)
1192 const struct got_error *err = NULL;
1193 struct got_blob_object *blob = NULL;
1195 *link_target = NULL;
1197 if (!got_object_tree_entry_is_symlink(te))
1198 return got_error(GOT_ERR_TREE_ENTRY_TYPE);
1200 err = got_object_open_as_blob(&blob, repo,
1201 got_tree_entry_get_id(te), PATH_MAX);
1202 if (err)
1203 return err;
1205 err = got_object_blob_read_to_str(link_target, blob);
1206 got_object_blob_close(blob);
1207 if (err) {
1208 free(*link_target);
1209 *link_target = NULL;
1211 return err;
1214 int
1215 got_tree_entry_get_index(struct got_tree_entry *te)
1217 return te->idx;
1220 struct got_tree_entry *
1221 got_tree_entry_get_next(struct got_tree_object *tree,
1222 struct got_tree_entry *te)
1224 return got_object_tree_get_entry(tree, te->idx + 1);
1227 struct got_tree_entry *
1228 got_tree_entry_get_prev(struct got_tree_object *tree,
1229 struct got_tree_entry *te)
1231 return got_object_tree_get_entry(tree, te->idx - 1);
1234 static const struct got_error *
1235 request_packed_blob(uint8_t **outbuf, size_t *size, size_t *hdrlen, int outfd,
1236 struct got_pack *pack, struct got_packidx *packidx, int idx,
1237 struct got_object_id *id)
1239 const struct got_error *err = NULL;
1240 int outfd_child;
1241 int basefd, accumfd; /* temporary files for delta application */
1243 basefd = got_opentempfd();
1244 if (basefd == -1)
1245 return got_error_from_errno("got_opentempfd");
1246 accumfd = got_opentempfd();
1247 if (accumfd == -1)
1248 return got_error_from_errno("got_opentempfd");
1250 outfd_child = dup(outfd);
1251 if (outfd_child == -1)
1252 return got_error_from_errno("dup");
1254 err = got_privsep_send_blob_req(pack->privsep_child->ibuf, -1, id, idx);
1255 if (err)
1256 return err;
1258 err = got_privsep_send_blob_outfd(pack->privsep_child->ibuf,
1259 outfd_child);
1260 if (err) {
1261 close(basefd);
1262 close(accumfd);
1263 return err;
1266 err = got_privsep_send_tmpfd(pack->privsep_child->ibuf,
1267 basefd);
1268 if (err) {
1269 close(accumfd);
1270 return err;
1273 err = got_privsep_send_tmpfd(pack->privsep_child->ibuf,
1274 accumfd);
1275 if (err)
1276 return err;
1278 err = got_privsep_recv_blob(outbuf, size, hdrlen,
1279 pack->privsep_child->ibuf);
1280 if (err)
1281 return err;
1283 if (lseek(outfd, SEEK_SET, 0) == -1)
1284 err = got_error_from_errno("lseek");
1286 return err;
1289 static const struct got_error *
1290 read_packed_blob_privsep(uint8_t **outbuf, size_t *size, size_t *hdrlen,
1291 int outfd, struct got_pack *pack, struct got_packidx *packidx, int idx,
1292 struct got_object_id *id)
1294 const struct got_error *err = NULL;
1296 if (pack->privsep_child == NULL) {
1297 err = start_pack_privsep_child(pack, packidx);
1298 if (err)
1299 return err;
1302 return request_packed_blob(outbuf, size, hdrlen, outfd, pack, packidx,
1303 idx, id);
1306 static const struct got_error *
1307 request_blob(uint8_t **outbuf, size_t *size, size_t *hdrlen, int outfd,
1308 int infd, struct got_object_id *id, struct imsgbuf *ibuf)
1310 const struct got_error *err = NULL;
1311 int outfd_child;
1313 outfd_child = dup(outfd);
1314 if (outfd_child == -1)
1315 return got_error_from_errno("dup");
1317 err = got_privsep_send_blob_req(ibuf, infd, id, -1);
1318 if (err)
1319 return err;
1321 err = got_privsep_send_blob_outfd(ibuf, outfd_child);
1322 if (err)
1323 return err;
1325 err = got_privsep_recv_blob(outbuf, size, hdrlen, ibuf);
1326 if (err)
1327 return err;
1329 if (lseek(outfd, SEEK_SET, 0) == -1)
1330 return got_error_from_errno("lseek");
1332 return err;
1335 static const struct got_error *
1336 read_blob_privsep(uint8_t **outbuf, size_t *size, size_t *hdrlen,
1337 int outfd, int infd, struct got_object_id *id, struct got_repository *repo)
1339 const struct got_error *err;
1340 int imsg_fds[2];
1341 pid_t pid;
1342 struct imsgbuf *ibuf;
1344 if (repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_BLOB].imsg_fd != -1) {
1345 ibuf = repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_BLOB].ibuf;
1346 return request_blob(outbuf, size, hdrlen, outfd, infd, id,
1347 ibuf);
1350 ibuf = calloc(1, sizeof(*ibuf));
1351 if (ibuf == NULL)
1352 return got_error_from_errno("calloc");
1354 if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_fds) == -1) {
1355 err = got_error_from_errno("socketpair");
1356 free(ibuf);
1357 return err;
1360 pid = fork();
1361 if (pid == -1) {
1362 err = got_error_from_errno("fork");
1363 free(ibuf);
1364 return err;
1366 else if (pid == 0) {
1367 got_privsep_exec_child(imsg_fds, GOT_PATH_PROG_READ_BLOB,
1368 repo->path);
1369 /* not reached */
1372 if (close(imsg_fds[1]) == -1) {
1373 err = got_error_from_errno("close");
1374 free(ibuf);
1375 return err;
1377 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_BLOB].imsg_fd =
1378 imsg_fds[0];
1379 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_BLOB].pid = pid;
1380 imsg_init(ibuf, imsg_fds[0]);
1381 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_BLOB].ibuf = ibuf;
1383 return request_blob(outbuf, size, hdrlen, outfd, infd, id, ibuf);
1386 static const struct got_error *
1387 open_blob(struct got_blob_object **blob, struct got_repository *repo,
1388 struct got_object_id *id, size_t blocksize)
1390 const struct got_error *err = NULL;
1391 struct got_packidx *packidx = NULL;
1392 int idx;
1393 char *path_packfile = NULL;
1394 uint8_t *outbuf;
1395 int outfd;
1396 size_t size, hdrlen;
1397 struct stat sb;
1399 *blob = calloc(1, sizeof(**blob));
1400 if (*blob == NULL)
1401 return got_error_from_errno("calloc");
1403 outfd = got_opentempfd();
1404 if (outfd == -1)
1405 return got_error_from_errno("got_opentempfd");
1407 (*blob)->read_buf = malloc(blocksize);
1408 if ((*blob)->read_buf == NULL) {
1409 err = got_error_from_errno("malloc");
1410 goto done;
1413 err = got_repo_search_packidx(&packidx, &idx, repo, id);
1414 if (err == NULL) {
1415 struct got_pack *pack = NULL;
1417 err = got_packidx_get_packfile_path(&path_packfile,
1418 packidx->path_packidx);
1419 if (err)
1420 goto done;
1422 pack = got_repo_get_cached_pack(repo, path_packfile);
1423 if (pack == NULL) {
1424 err = got_repo_cache_pack(&pack, repo, path_packfile,
1425 packidx);
1426 if (err)
1427 goto done;
1429 err = read_packed_blob_privsep(&outbuf, &size, &hdrlen, outfd,
1430 pack, packidx, idx, id);
1431 } else if (err->code == GOT_ERR_NO_OBJ) {
1432 int infd;
1434 err = got_object_open_loose_fd(&infd, id, repo);
1435 if (err)
1436 goto done;
1437 err = read_blob_privsep(&outbuf, &size, &hdrlen, outfd, infd,
1438 id, repo);
1440 if (err)
1441 goto done;
1443 if (hdrlen > size) {
1444 err = got_error(GOT_ERR_BAD_OBJ_HDR);
1445 goto done;
1448 if (outbuf) {
1449 if (close(outfd) == -1 && err == NULL)
1450 err = got_error_from_errno("close");
1451 outfd = -1;
1452 (*blob)->f = fmemopen(outbuf, size, "rb");
1453 if ((*blob)->f == NULL) {
1454 err = got_error_from_errno("fmemopen");
1455 free(outbuf);
1456 goto done;
1458 (*blob)->data = outbuf;
1459 } else {
1460 if (fstat(outfd, &sb) == -1) {
1461 err = got_error_from_errno("fstat");
1462 goto done;
1465 if (sb.st_size != size) {
1466 err = got_error(GOT_ERR_PRIVSEP_LEN);
1467 goto done;
1470 (*blob)->f = fdopen(outfd, "rb");
1471 if ((*blob)->f == NULL) {
1472 err = got_error_from_errno("fdopen");
1473 close(outfd);
1474 outfd = -1;
1475 goto done;
1479 (*blob)->hdrlen = hdrlen;
1480 (*blob)->blocksize = blocksize;
1481 memcpy(&(*blob)->id.sha1, id->sha1, SHA1_DIGEST_LENGTH);
1483 done:
1484 free(path_packfile);
1485 if (err) {
1486 if (*blob) {
1487 got_object_blob_close(*blob);
1488 *blob = NULL;
1489 } else if (outfd != -1)
1490 close(outfd);
1492 return err;
1495 const struct got_error *
1496 got_object_open_as_blob(struct got_blob_object **blob,
1497 struct got_repository *repo, struct got_object_id *id,
1498 size_t blocksize)
1500 return open_blob(blob, repo, id, blocksize);
1503 const struct got_error *
1504 got_object_blob_open(struct got_blob_object **blob,
1505 struct got_repository *repo, struct got_object *obj, size_t blocksize)
1507 return open_blob(blob, repo, got_object_get_id(obj), blocksize);
1510 const struct got_error *
1511 got_object_blob_close(struct got_blob_object *blob)
1513 const struct got_error *err = NULL;
1514 free(blob->read_buf);
1515 if (blob->f && fclose(blob->f) == EOF)
1516 err = got_error_from_errno("fclose");
1517 free(blob->data);
1518 free(blob);
1519 return err;
1522 void
1523 got_object_blob_rewind(struct got_blob_object *blob)
1525 if (blob->f)
1526 rewind(blob->f);
1529 char *
1530 got_object_blob_id_str(struct got_blob_object *blob, char *buf, size_t size)
1532 return got_sha1_digest_to_str(blob->id.sha1, buf, size);
1535 size_t
1536 got_object_blob_get_hdrlen(struct got_blob_object *blob)
1538 return blob->hdrlen;
1541 const uint8_t *
1542 got_object_blob_get_read_buf(struct got_blob_object *blob)
1544 return blob->read_buf;
1547 const struct got_error *
1548 got_object_blob_read_block(size_t *outlenp, struct got_blob_object *blob)
1550 size_t n;
1552 n = fread(blob->read_buf, 1, blob->blocksize, blob->f);
1553 if (n == 0 && ferror(blob->f))
1554 return got_ferror(blob->f, GOT_ERR_IO);
1555 *outlenp = n;
1556 return NULL;
1559 const struct got_error *
1560 got_object_blob_dump_to_file(off_t *filesize, int *nlines,
1561 off_t **line_offsets, FILE *outfile, struct got_blob_object *blob)
1563 const struct got_error *err = NULL;
1564 size_t n, len, hdrlen;
1565 const uint8_t *buf;
1566 int i;
1567 const int alloc_chunksz = 512;
1568 size_t nalloc = 0;
1569 off_t off = 0, total_len = 0;
1571 if (line_offsets)
1572 *line_offsets = NULL;
1573 if (filesize)
1574 *filesize = 0;
1575 if (nlines)
1576 *nlines = 0;
1578 hdrlen = got_object_blob_get_hdrlen(blob);
1579 do {
1580 err = got_object_blob_read_block(&len, blob);
1581 if (err)
1582 return err;
1583 if (len == 0)
1584 break;
1585 buf = got_object_blob_get_read_buf(blob);
1586 i = hdrlen;
1587 if (nlines) {
1588 if (line_offsets && *line_offsets == NULL) {
1589 /* Have some data but perhaps no '\n'. */
1590 *nlines = 1;
1591 nalloc = alloc_chunksz;
1592 *line_offsets = calloc(nalloc,
1593 sizeof(**line_offsets));
1594 if (*line_offsets == NULL)
1595 return got_error_from_errno("calloc");
1597 /* Skip forward over end of first line. */
1598 while (i < len) {
1599 if (buf[i] == '\n')
1600 break;
1601 i++;
1604 /* Scan '\n' offsets in remaining chunk of data. */
1605 while (i < len) {
1606 if (buf[i] != '\n') {
1607 i++;
1608 continue;
1610 (*nlines)++;
1611 if (line_offsets && nalloc < *nlines) {
1612 size_t n = *nlines + alloc_chunksz;
1613 off_t *o = recallocarray(*line_offsets,
1614 nalloc, n, sizeof(**line_offsets));
1615 if (o == NULL) {
1616 free(*line_offsets);
1617 *line_offsets = NULL;
1618 return got_error_from_errno(
1619 "recallocarray");
1621 *line_offsets = o;
1622 nalloc = n;
1624 if (line_offsets) {
1625 off = total_len + i - hdrlen + 1;
1626 (*line_offsets)[*nlines - 1] = off;
1628 i++;
1631 /* Skip blob object header first time around. */
1632 n = fwrite(buf + hdrlen, 1, len - hdrlen, outfile);
1633 if (n != len - hdrlen)
1634 return got_ferror(outfile, GOT_ERR_IO);
1635 total_len += len - hdrlen;
1636 hdrlen = 0;
1637 } while (len != 0);
1639 if (fflush(outfile) != 0)
1640 return got_error_from_errno("fflush");
1641 rewind(outfile);
1643 if (filesize)
1644 *filesize = total_len;
1646 return NULL;
1649 static const struct got_error *
1650 request_packed_tag(struct got_tag_object **tag, struct got_pack *pack,
1651 int pack_idx, struct got_object_id *id)
1653 const struct got_error *err = NULL;
1655 err = got_privsep_send_tag_req(pack->privsep_child->ibuf, -1, id,
1656 pack_idx);
1657 if (err)
1658 return err;
1660 return got_privsep_recv_tag(tag, pack->privsep_child->ibuf);
1663 static const struct got_error *
1664 read_packed_tag_privsep(struct got_tag_object **tag,
1665 struct got_pack *pack, struct got_packidx *packidx, int idx,
1666 struct got_object_id *id)
1668 const struct got_error *err = NULL;
1670 if (pack->privsep_child)
1671 return request_packed_tag(tag, pack, idx, id);
1673 err = start_pack_privsep_child(pack, packidx);
1674 if (err)
1675 return err;
1677 return request_packed_tag(tag, pack, idx, id);
1680 static const struct got_error *
1681 request_tag(struct got_tag_object **tag, struct got_repository *repo,
1682 int fd, struct got_object_id *id)
1684 const struct got_error *err = NULL;
1685 struct imsgbuf *ibuf;
1687 ibuf = repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TAG].ibuf;
1689 err = got_privsep_send_tag_req(ibuf, fd, id, -1);
1690 if (err)
1691 return err;
1693 return got_privsep_recv_tag(tag, ibuf);
1696 static const struct got_error *
1697 read_tag_privsep(struct got_tag_object **tag, int obj_fd,
1698 struct got_object_id *id, struct got_repository *repo)
1700 const struct got_error *err;
1701 int imsg_fds[2];
1702 pid_t pid;
1703 struct imsgbuf *ibuf;
1705 if (repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TAG].imsg_fd != -1)
1706 return request_tag(tag, repo, obj_fd, id);
1708 ibuf = calloc(1, sizeof(*ibuf));
1709 if (ibuf == NULL)
1710 return got_error_from_errno("calloc");
1712 if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_fds) == -1) {
1713 err = got_error_from_errno("socketpair");
1714 free(ibuf);
1715 return err;
1718 pid = fork();
1719 if (pid == -1) {
1720 err = got_error_from_errno("fork");
1721 free(ibuf);
1722 return err;
1724 else if (pid == 0) {
1725 got_privsep_exec_child(imsg_fds, GOT_PATH_PROG_READ_TAG,
1726 repo->path);
1727 /* not reached */
1730 if (close(imsg_fds[1]) == -1) {
1731 err = got_error_from_errno("close");
1732 free(ibuf);
1733 return err;
1735 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TAG].imsg_fd =
1736 imsg_fds[0];
1737 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TAG].pid = pid;
1738 imsg_init(ibuf, imsg_fds[0]);
1739 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TAG].ibuf = ibuf;
1741 return request_tag(tag, repo, obj_fd, id);
1744 static const struct got_error *
1745 open_tag(struct got_tag_object **tag, struct got_repository *repo,
1746 struct got_object_id *id, int check_cache)
1748 const struct got_error *err = NULL;
1749 struct got_packidx *packidx = NULL;
1750 int idx;
1751 char *path_packfile = NULL;
1752 struct got_object *obj = NULL;
1753 int obj_type = GOT_OBJ_TYPE_ANY;
1755 if (check_cache) {
1756 *tag = got_repo_get_cached_tag(repo, id);
1757 if (*tag != NULL) {
1758 (*tag)->refcnt++;
1759 return NULL;
1761 } else
1762 *tag = NULL;
1764 err = got_repo_search_packidx(&packidx, &idx, repo, id);
1765 if (err == NULL) {
1766 struct got_pack *pack = NULL;
1768 err = got_packidx_get_packfile_path(&path_packfile,
1769 packidx->path_packidx);
1770 if (err)
1771 return err;
1773 pack = got_repo_get_cached_pack(repo, path_packfile);
1774 if (pack == NULL) {
1775 err = got_repo_cache_pack(&pack, repo, path_packfile,
1776 packidx);
1777 if (err)
1778 goto done;
1781 /* Beware of "lightweight" tags: Check object type first. */
1782 err = read_packed_object_privsep(&obj, repo, pack, packidx,
1783 idx, id);
1784 if (err)
1785 goto done;
1786 obj_type = obj->type;
1787 got_object_close(obj);
1788 if (obj_type != GOT_OBJ_TYPE_TAG) {
1789 err = got_error(GOT_ERR_OBJ_TYPE);
1790 goto done;
1792 err = read_packed_tag_privsep(tag, pack, packidx, idx, id);
1793 } else if (err->code == GOT_ERR_NO_OBJ) {
1794 int fd;
1796 err = got_object_open_loose_fd(&fd, id, repo);
1797 if (err)
1798 return err;
1799 err = got_object_read_header_privsep(&obj, id, repo, fd);
1800 if (err)
1801 return err;
1802 obj_type = obj->type;
1803 got_object_close(obj);
1804 if (obj_type != GOT_OBJ_TYPE_TAG)
1805 return got_error(GOT_ERR_OBJ_TYPE);
1807 err = got_object_open_loose_fd(&fd, id, repo);
1808 if (err)
1809 return err;
1810 err = read_tag_privsep(tag, fd, id, repo);
1813 if (err == NULL) {
1814 (*tag)->refcnt++;
1815 err = got_repo_cache_tag(repo, id, *tag);
1817 done:
1818 free(path_packfile);
1819 return err;
1822 const struct got_error *
1823 got_object_open_as_tag(struct got_tag_object **tag,
1824 struct got_repository *repo, struct got_object_id *id)
1826 *tag = got_repo_get_cached_tag(repo, id);
1827 if (*tag != NULL) {
1828 (*tag)->refcnt++;
1829 return NULL;
1832 return open_tag(tag, repo, id, 0);
1835 const struct got_error *
1836 got_object_tag_open(struct got_tag_object **tag,
1837 struct got_repository *repo, struct got_object *obj)
1839 return open_tag(tag, repo, got_object_get_id(obj), 1);
1842 const char *
1843 got_object_tag_get_name(struct got_tag_object *tag)
1845 return tag->tag;
1848 int
1849 got_object_tag_get_object_type(struct got_tag_object *tag)
1851 return tag->obj_type;
1854 struct got_object_id *
1855 got_object_tag_get_object_id(struct got_tag_object *tag)
1857 return &tag->id;
1860 time_t
1861 got_object_tag_get_tagger_time(struct got_tag_object *tag)
1863 return tag->tagger_time;
1866 time_t
1867 got_object_tag_get_tagger_gmtoff(struct got_tag_object *tag)
1869 return tag->tagger_gmtoff;
1872 const char *
1873 got_object_tag_get_tagger(struct got_tag_object *tag)
1875 return tag->tagger;
1878 const char *
1879 got_object_tag_get_message(struct got_tag_object *tag)
1881 return tag->tagmsg;
1884 static struct got_tree_entry *
1885 find_entry_by_name(struct got_tree_object *tree, const char *name, size_t len)
1887 int i;
1889 /* Note that tree entries are sorted in strncmp() order. */
1890 for (i = 0; i < tree->nentries; i++) {
1891 struct got_tree_entry *te = &tree->entries[i];
1892 int cmp = strncmp(te->name, name, len);
1893 if (cmp < 0)
1894 continue;
1895 if (cmp > 0)
1896 break;
1897 if (te->name[len] == '\0')
1898 return te;
1900 return NULL;
1903 struct got_tree_entry *
1904 got_object_tree_find_entry(struct got_tree_object *tree, const char *name)
1906 return find_entry_by_name(tree, name, strlen(name));
1909 const struct got_error *
1910 got_object_tree_find_path(struct got_object_id **id, mode_t *mode,
1911 struct got_repository *repo, struct got_tree_object *tree,
1912 const char *path)
1914 const struct got_error *err = NULL;
1915 struct got_tree_object *subtree = NULL;
1916 struct got_tree_entry *te = NULL;
1917 const char *seg, *s;
1918 size_t seglen;
1920 *id = NULL;
1922 s = path;
1923 while (s[0] == '/')
1924 s++;
1925 seg = s;
1926 seglen = 0;
1927 subtree = tree;
1928 while (*s) {
1929 struct got_tree_object *next_tree;
1931 if (*s != '/') {
1932 s++;
1933 seglen++;
1934 if (*s)
1935 continue;
1938 te = find_entry_by_name(subtree, seg, seglen);
1939 if (te == NULL) {
1940 err = got_error_path(path, GOT_ERR_NO_TREE_ENTRY);
1941 goto done;
1944 if (*s == '\0')
1945 break;
1947 seg = s + 1;
1948 seglen = 0;
1949 s++;
1950 if (*s) {
1951 err = got_object_open_as_tree(&next_tree, repo,
1952 &te->id);
1953 te = NULL;
1954 if (err)
1955 goto done;
1956 if (subtree != tree)
1957 got_object_tree_close(subtree);
1958 subtree = next_tree;
1962 if (te) {
1963 *id = got_object_id_dup(&te->id);
1964 if (*id == NULL)
1965 return got_error_from_errno("got_object_id_dup");
1966 if (mode)
1967 *mode = te->mode;
1968 } else
1969 err = got_error_path(path, GOT_ERR_NO_TREE_ENTRY);
1970 done:
1971 if (subtree && subtree != tree)
1972 got_object_tree_close(subtree);
1973 return err;
1975 const struct got_error *
1976 got_object_id_by_path(struct got_object_id **id, struct got_repository *repo,
1977 struct got_object_id *commit_id, const char *path)
1979 const struct got_error *err = NULL;
1980 struct got_commit_object *commit = NULL;
1981 struct got_tree_object *tree = NULL;
1983 *id = NULL;
1985 err = got_object_open_as_commit(&commit, repo, commit_id);
1986 if (err)
1987 goto done;
1989 /* Handle opening of root of commit's tree. */
1990 if (got_path_is_root_dir(path)) {
1991 *id = got_object_id_dup(commit->tree_id);
1992 if (*id == NULL)
1993 err = got_error_from_errno("got_object_id_dup");
1994 } else {
1995 err = got_object_open_as_tree(&tree, repo, commit->tree_id);
1996 if (err)
1997 goto done;
1998 err = got_object_tree_find_path(id, NULL, repo, tree, path);
2000 done:
2001 if (commit)
2002 got_object_commit_close(commit);
2003 if (tree)
2004 got_object_tree_close(tree);
2005 return err;
2009 * Normalize file mode bits to avoid false positive tree entry differences
2010 * in case tree entries have unexpected mode bits set.
2012 static mode_t
2013 normalize_mode_for_comparison(mode_t mode)
2016 * For directories, the only relevant bit is the IFDIR bit.
2017 * This allows us to detect paths changing from a directory
2018 * to a file and vice versa.
2020 if (S_ISDIR(mode))
2021 return mode & S_IFDIR;
2024 * For symlinks, the only relevant bit is the IFLNK bit.
2025 * This allows us to detect paths changing from a symlinks
2026 * to a file or directory and vice versa.
2028 if (S_ISLNK(mode))
2029 return mode & S_IFLNK;
2031 /* For files, the only change we care about is the executable bit. */
2032 return mode & S_IXUSR;
2035 const struct got_error *
2036 got_object_tree_path_changed(int *changed,
2037 struct got_tree_object *tree01, struct got_tree_object *tree02,
2038 const char *path, struct got_repository *repo)
2040 const struct got_error *err = NULL;
2041 struct got_tree_object *tree1 = NULL, *tree2 = NULL;
2042 struct got_tree_entry *te1 = NULL, *te2 = NULL;
2043 const char *seg, *s;
2044 size_t seglen;
2046 *changed = 0;
2048 /* We not do support comparing the root path. */
2049 if (got_path_is_root_dir(path))
2050 return got_error_path(path, GOT_ERR_BAD_PATH);
2052 tree1 = tree01;
2053 tree2 = tree02;
2054 s = path;
2055 while (*s == '/')
2056 s++;
2057 seg = s;
2058 seglen = 0;
2059 while (*s) {
2060 struct got_tree_object *next_tree1, *next_tree2;
2061 mode_t mode1, mode2;
2063 if (*s != '/') {
2064 s++;
2065 seglen++;
2066 if (*s)
2067 continue;
2070 te1 = find_entry_by_name(tree1, seg, seglen);
2071 if (te1 == NULL) {
2072 err = got_error(GOT_ERR_NO_OBJ);
2073 goto done;
2076 if (tree2)
2077 te2 = find_entry_by_name(tree2, seg, seglen);
2079 if (te2) {
2080 mode1 = normalize_mode_for_comparison(te1->mode);
2081 mode2 = normalize_mode_for_comparison(te2->mode);
2082 if (mode1 != mode2) {
2083 *changed = 1;
2084 goto done;
2087 if (got_object_id_cmp(&te1->id, &te2->id) == 0) {
2088 *changed = 0;
2089 goto done;
2093 if (*s == '\0') { /* final path element */
2094 *changed = 1;
2095 goto done;
2098 seg = s + 1;
2099 s++;
2100 seglen = 0;
2101 if (*s) {
2102 err = got_object_open_as_tree(&next_tree1, repo,
2103 &te1->id);
2104 te1 = NULL;
2105 if (err)
2106 goto done;
2107 if (tree1 != tree01)
2108 got_object_tree_close(tree1);
2109 tree1 = next_tree1;
2111 if (te2) {
2112 err = got_object_open_as_tree(&next_tree2, repo,
2113 &te2->id);
2114 te2 = NULL;
2115 if (err)
2116 goto done;
2117 if (tree2 != tree02)
2118 got_object_tree_close(tree2);
2119 tree2 = next_tree2;
2120 } else if (tree2) {
2121 if (tree2 != tree02)
2122 got_object_tree_close(tree2);
2123 tree2 = NULL;
2127 done:
2128 if (tree1 && tree1 != tree01)
2129 got_object_tree_close(tree1);
2130 if (tree2 && tree2 != tree02)
2131 got_object_tree_close(tree2);
2132 return err;
2135 const struct got_error *
2136 got_object_tree_entry_dup(struct got_tree_entry **new_te,
2137 struct got_tree_entry *te)
2139 const struct got_error *err = NULL;
2141 *new_te = calloc(1, sizeof(**new_te));
2142 if (*new_te == NULL)
2143 return got_error_from_errno("calloc");
2145 (*new_te)->mode = te->mode;
2146 memcpy((*new_te)->name, te->name, sizeof((*new_te)->name));
2147 memcpy(&(*new_te)->id, &te->id, sizeof((*new_te)->id));
2148 return err;
2151 int
2152 got_object_tree_entry_is_submodule(struct got_tree_entry *te)
2154 return (te->mode & S_IFMT) == (S_IFDIR | S_IFLNK);
2157 int
2158 got_object_tree_entry_is_symlink(struct got_tree_entry *te)
2160 /* S_IFDIR check avoids confusing symlinks with submodules. */
2161 return ((te->mode & (S_IFDIR | S_IFLNK)) == S_IFLNK);
2164 static const struct got_error *
2165 resolve_symlink(char **link_target, const char *path,
2166 struct got_object_id *commit_id, struct got_repository *repo)
2168 const struct got_error *err = NULL;
2169 char buf[PATH_MAX];
2170 char *name, *parent_path = NULL;
2171 struct got_object_id *tree_obj_id = NULL;
2172 struct got_tree_object *tree = NULL;
2173 struct got_tree_entry *te = NULL;
2175 *link_target = NULL;
2177 if (strlcpy(buf, path, sizeof(buf)) >= sizeof(buf))
2178 return got_error(GOT_ERR_NO_SPACE);
2180 name = basename(buf);
2181 if (name == NULL)
2182 return got_error_from_errno2("basename", path);
2184 err = got_path_dirname(&parent_path, path);
2185 if (err)
2186 return err;
2188 err = got_object_id_by_path(&tree_obj_id, repo, commit_id,
2189 parent_path);
2190 if (err) {
2191 if (err->code == GOT_ERR_NO_TREE_ENTRY) {
2192 /* Display the complete path in error message. */
2193 err = got_error_path(path, err->code);
2195 goto done;
2198 err = got_object_open_as_tree(&tree, repo, tree_obj_id);
2199 if (err)
2200 goto done;
2202 te = got_object_tree_find_entry(tree, name);
2203 if (te == NULL) {
2204 err = got_error_path(path, GOT_ERR_NO_TREE_ENTRY);
2205 goto done;
2208 if (got_object_tree_entry_is_symlink(te)) {
2209 err = got_tree_entry_get_symlink_target(link_target, te, repo);
2210 if (err)
2211 goto done;
2212 if (!got_path_is_absolute(*link_target)) {
2213 char *abspath;
2214 if (asprintf(&abspath, "%s/%s", parent_path,
2215 *link_target) == -1) {
2216 err = got_error_from_errno("asprintf");
2217 goto done;
2219 free(*link_target);
2220 *link_target = malloc(PATH_MAX);
2221 if (*link_target == NULL) {
2222 err = got_error_from_errno("malloc");
2223 goto done;
2225 err = got_canonpath(abspath, *link_target, PATH_MAX);
2226 free(abspath);
2227 if (err)
2228 goto done;
2231 done:
2232 free(tree_obj_id);
2233 if (tree)
2234 got_object_tree_close(tree);
2235 if (err) {
2236 free(*link_target);
2237 *link_target = NULL;
2239 return err;
2242 const struct got_error *
2243 got_object_resolve_symlinks(char **link_target, const char *path,
2244 struct got_object_id *commit_id, struct got_repository *repo)
2246 const struct got_error *err = NULL;
2247 char *next_target = NULL;
2248 int max_recursion = 40; /* matches Git */
2250 *link_target = NULL;
2252 do {
2253 err = resolve_symlink(&next_target,
2254 *link_target ? *link_target : path, commit_id, repo);
2255 if (err)
2256 break;
2257 if (next_target) {
2258 free(*link_target);
2259 if (--max_recursion == 0) {
2260 err = got_error_path(path, GOT_ERR_RECURSION);
2261 *link_target = NULL;
2262 break;
2264 *link_target = next_target;
2266 } while (next_target);
2268 return err;
2271 const struct got_error *
2272 got_traverse_packed_commits(struct got_object_id_queue *traversed_commits,
2273 struct got_object_id *commit_id, const char *path,
2274 struct got_repository *repo)
2276 const struct got_error *err = NULL;
2277 struct got_pack *pack = NULL;
2278 struct got_packidx *packidx = NULL;
2279 char *path_packfile = NULL;
2280 struct got_commit_object *changed_commit = NULL;
2281 struct got_object_id *changed_commit_id = NULL;
2282 int idx;
2284 err = got_repo_search_packidx(&packidx, &idx, repo, commit_id);
2285 if (err) {
2286 if (err->code != GOT_ERR_NO_OBJ)
2287 return err;
2288 return NULL;
2291 err = got_packidx_get_packfile_path(&path_packfile,
2292 packidx->path_packidx);
2293 if (err)
2294 return err;
2296 pack = got_repo_get_cached_pack(repo, path_packfile);
2297 if (pack == NULL) {
2298 err = got_repo_cache_pack(&pack, repo, path_packfile, packidx);
2299 if (err)
2300 goto done;
2303 if (pack->privsep_child == NULL) {
2304 err = start_pack_privsep_child(pack, packidx);
2305 if (err)
2306 goto done;
2309 err = got_privsep_send_commit_traversal_request(
2310 pack->privsep_child->ibuf, commit_id, idx, path);
2311 if (err)
2312 goto done;
2314 err = got_privsep_recv_traversed_commits(&changed_commit,
2315 &changed_commit_id, traversed_commits, pack->privsep_child->ibuf);
2316 if (err)
2317 goto done;
2319 if (changed_commit) {
2321 * Cache the commit in which the path was changed.
2322 * This commit might be opened again soon.
2324 changed_commit->refcnt++;
2325 err = got_repo_cache_commit(repo, changed_commit_id,
2326 changed_commit);
2327 got_object_commit_close(changed_commit);
2329 done:
2330 free(path_packfile);
2331 free(changed_commit_id);
2332 return err;