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 <unistd.h>
33 #include <zlib.h>
34 #include <ctype.h>
35 #include <libgen.h>
36 #include <limits.h>
37 #include <time.h>
39 #include "got_compat.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 if (outbuf) {
595 (*obj)->f = fmemopen(outbuf, hdrlen + size, "r");
596 if ((*obj)->f == NULL) {
597 err = got_error_from_errno("fdopen");
598 goto done;
600 (*obj)->data = outbuf;
601 } else {
602 struct stat sb;
603 if (fstat(*outfd, &sb) == -1) {
604 err = got_error_from_errno("fstat");
605 goto done;
608 if (sb.st_size != hdrlen + size) {
609 err = got_error(GOT_ERR_PRIVSEP_LEN);
610 goto done;
613 (*obj)->f = fdopen(*outfd, "r");
614 if ((*obj)->f == NULL) {
615 err = got_error_from_errno("fdopen");
616 goto done;
618 (*obj)->data = NULL;
619 *outfd = -1;
621 (*obj)->hdrlen = hdrlen;
622 (*obj)->size = size;
623 (*obj)->blocksize = blocksize;
624 err = got_repo_cache_raw_object(repo, id, *obj);
625 done:
626 free(path_packfile);
627 if (err) {
628 if (*obj) {
629 got_object_raw_close(*obj);
630 *obj = NULL;
632 free(outbuf);
633 } else
634 (*obj)->refcnt++;
635 return err;
638 void
639 got_object_raw_rewind(struct got_raw_object *obj)
641 if (obj->f)
642 rewind(obj->f);
645 size_t
646 got_object_raw_get_hdrlen(struct got_raw_object *obj)
648 return obj->hdrlen;
651 const uint8_t *
652 got_object_raw_get_read_buf(struct got_raw_object *obj)
654 return obj->read_buf;
657 const struct got_error *
658 got_object_raw_read_block(size_t *outlenp, struct got_raw_object *obj)
660 size_t n;
662 n = fread(obj->read_buf, 1, obj->blocksize, obj->f);
663 if (n == 0 && ferror(obj->f))
664 return got_ferror(obj->f, GOT_ERR_IO);
665 *outlenp = n;
666 return NULL;
669 const struct got_error *
670 got_object_open_by_id_str(struct got_object **obj, struct got_repository *repo,
671 const char *id_str)
673 struct got_object_id id;
675 if (!got_parse_sha1_digest(id.sha1, id_str))
676 return got_error_path(id_str, GOT_ERR_BAD_OBJ_ID_STR);
678 return got_object_open(obj, repo, &id);
681 const struct got_error *
682 got_object_resolve_id_str(struct got_object_id **id,
683 struct got_repository *repo, const char *id_str)
685 const struct got_error *err = NULL;
686 struct got_object *obj;
688 err = got_object_open_by_id_str(&obj, repo, id_str);
689 if (err)
690 return err;
692 *id = got_object_id_dup(got_object_get_id(obj));
693 got_object_close(obj);
694 if (*id == NULL)
695 return got_error_from_errno("got_object_id_dup");
697 return NULL;
700 static const struct got_error *
701 request_packed_commit(struct got_commit_object **commit, struct got_pack *pack,
702 int pack_idx, struct got_object_id *id)
704 const struct got_error *err = NULL;
706 err = got_privsep_send_commit_req(pack->privsep_child->ibuf, -1, id,
707 pack_idx);
708 if (err)
709 return err;
711 err = got_privsep_recv_commit(commit, pack->privsep_child->ibuf);
712 if (err)
713 return err;
715 (*commit)->flags |= GOT_COMMIT_FLAG_PACKED;
716 return NULL;
719 static const struct got_error *
720 read_packed_commit_privsep(struct got_commit_object **commit,
721 struct got_pack *pack, struct got_packidx *packidx, int idx,
722 struct got_object_id *id)
724 const struct got_error *err = NULL;
726 if (pack->privsep_child)
727 return request_packed_commit(commit, pack, idx, id);
729 err = start_pack_privsep_child(pack, packidx);
730 if (err)
731 return err;
733 return request_packed_commit(commit, pack, idx, id);
736 static const struct got_error *
737 request_commit(struct got_commit_object **commit, struct got_repository *repo,
738 int fd, struct got_object_id *id)
740 const struct got_error *err = NULL;
741 struct imsgbuf *ibuf;
743 ibuf = repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_COMMIT].ibuf;
745 err = got_privsep_send_commit_req(ibuf, fd, id, -1);
746 if (err)
747 return err;
749 return got_privsep_recv_commit(commit, ibuf);
752 static const struct got_error *
753 read_commit_privsep(struct got_commit_object **commit, int obj_fd,
754 struct got_object_id *id, struct got_repository *repo)
756 const struct got_error *err;
757 int imsg_fds[2];
758 pid_t pid;
759 struct imsgbuf *ibuf;
761 if (repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_COMMIT].imsg_fd != -1)
762 return request_commit(commit, repo, obj_fd, id);
764 ibuf = calloc(1, sizeof(*ibuf));
765 if (ibuf == NULL)
766 return got_error_from_errno("calloc");
768 if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_fds) == -1) {
769 err = got_error_from_errno("socketpair");
770 free(ibuf);
771 return err;
774 pid = fork();
775 if (pid == -1) {
776 err = got_error_from_errno("fork");
777 free(ibuf);
778 return err;
780 else if (pid == 0) {
781 got_privsep_exec_child(imsg_fds, GOT_PATH_PROG_READ_COMMIT,
782 repo->path);
783 /* not reached */
786 if (close(imsg_fds[1]) == -1) {
787 err = got_error_from_errno("close");
788 free(ibuf);
789 return err;
791 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_COMMIT].imsg_fd =
792 imsg_fds[0];
793 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_COMMIT].pid = pid;
794 imsg_init(ibuf, imsg_fds[0]);
795 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_COMMIT].ibuf = ibuf;
797 return request_commit(commit, repo, obj_fd, id);
801 static const struct got_error *
802 open_commit(struct got_commit_object **commit,
803 struct got_repository *repo, struct got_object_id *id, int check_cache)
805 const struct got_error *err = NULL;
806 struct got_packidx *packidx = NULL;
807 int idx;
808 char *path_packfile = NULL;
810 if (check_cache) {
811 *commit = got_repo_get_cached_commit(repo, id);
812 if (*commit != NULL) {
813 (*commit)->refcnt++;
814 return NULL;
816 } else
817 *commit = NULL;
819 err = got_repo_search_packidx(&packidx, &idx, repo, id);
820 if (err == NULL) {
821 struct got_pack *pack = NULL;
823 err = got_packidx_get_packfile_path(&path_packfile,
824 packidx->path_packidx);
825 if (err)
826 return err;
828 pack = got_repo_get_cached_pack(repo, path_packfile);
829 if (pack == NULL) {
830 err = got_repo_cache_pack(&pack, repo, path_packfile,
831 packidx);
832 if (err)
833 goto done;
835 err = read_packed_commit_privsep(commit, pack,
836 packidx, idx, id);
837 } else if (err->code == GOT_ERR_NO_OBJ) {
838 int fd;
840 err = got_object_open_loose_fd(&fd, id, repo);
841 if (err)
842 return err;
843 err = read_commit_privsep(commit, fd, id, repo);
846 if (err == NULL) {
847 (*commit)->refcnt++;
848 err = got_repo_cache_commit(repo, id, *commit);
850 done:
851 free(path_packfile);
852 return err;
855 const struct got_error *
856 got_object_open_as_commit(struct got_commit_object **commit,
857 struct got_repository *repo, struct got_object_id *id)
859 *commit = got_repo_get_cached_commit(repo, id);
860 if (*commit != NULL) {
861 (*commit)->refcnt++;
862 return NULL;
865 return open_commit(commit, repo, id, 0);
868 const struct got_error *
869 got_object_commit_open(struct got_commit_object **commit,
870 struct got_repository *repo, struct got_object *obj)
872 return open_commit(commit, repo, got_object_get_id(obj), 1);
875 const struct got_error *
876 got_object_qid_alloc(struct got_object_qid **qid, struct got_object_id *id)
878 const struct got_error *err = NULL;
880 *qid = calloc(1, sizeof(**qid));
881 if (*qid == NULL)
882 return got_error_from_errno("calloc");
884 (*qid)->id = got_object_id_dup(id);
885 if ((*qid)->id == NULL) {
886 err = got_error_from_errno("got_object_id_dup");
887 got_object_qid_free(*qid);
888 *qid = NULL;
889 return err;
892 return NULL;
895 const struct got_error *
896 got_object_id_queue_copy(const struct got_object_id_queue *src,
897 struct got_object_id_queue *dest)
899 const struct got_error *err;
900 struct got_object_qid *qid;
902 STAILQ_FOREACH(qid, src, entry) {
903 struct got_object_qid *new;
904 /*
905 * Deep-copy the object ID only. Let the caller deal
906 * with setting up the new->data pointer if needed.
907 */
908 err = got_object_qid_alloc(&new, qid->id);
909 if (err) {
910 got_object_id_queue_free(dest);
911 return err;
913 STAILQ_INSERT_TAIL(dest, new, entry);
916 return NULL;
919 static const struct got_error *
920 request_packed_tree(struct got_tree_object **tree, struct got_pack *pack,
921 int pack_idx, struct got_object_id *id)
923 const struct got_error *err = NULL;
925 err = got_privsep_send_tree_req(pack->privsep_child->ibuf, -1, id,
926 pack_idx);
927 if (err)
928 return err;
930 return got_privsep_recv_tree(tree, pack->privsep_child->ibuf);
933 static const struct got_error *
934 read_packed_tree_privsep(struct got_tree_object **tree,
935 struct got_pack *pack, struct got_packidx *packidx, int idx,
936 struct got_object_id *id)
938 const struct got_error *err = NULL;
940 if (pack->privsep_child)
941 return request_packed_tree(tree, pack, idx, id);
943 err = start_pack_privsep_child(pack, packidx);
944 if (err)
945 return err;
947 return request_packed_tree(tree, pack, idx, id);
950 static const struct got_error *
951 request_tree(struct got_tree_object **tree, struct got_repository *repo,
952 int fd, struct got_object_id *id)
954 const struct got_error *err = NULL;
955 struct imsgbuf *ibuf;
957 ibuf = repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TREE].ibuf;
959 err = got_privsep_send_tree_req(ibuf, fd, id, -1);
960 if (err)
961 return err;
963 return got_privsep_recv_tree(tree, ibuf);
966 const struct got_error *
967 read_tree_privsep(struct got_tree_object **tree, int obj_fd,
968 struct got_object_id *id, struct got_repository *repo)
970 const struct got_error *err;
971 int imsg_fds[2];
972 pid_t pid;
973 struct imsgbuf *ibuf;
975 if (repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TREE].imsg_fd != -1)
976 return request_tree(tree, repo, obj_fd, id);
978 ibuf = calloc(1, sizeof(*ibuf));
979 if (ibuf == NULL)
980 return got_error_from_errno("calloc");
982 if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_fds) == -1) {
983 err = got_error_from_errno("socketpair");
984 free(ibuf);
985 return err;
988 pid = fork();
989 if (pid == -1) {
990 err = got_error_from_errno("fork");
991 free(ibuf);
992 return err;
994 else if (pid == 0) {
995 got_privsep_exec_child(imsg_fds, GOT_PATH_PROG_READ_TREE,
996 repo->path);
997 /* not reached */
1000 if (close(imsg_fds[1]) == -1) {
1001 err = got_error_from_errno("close");
1002 free(ibuf);
1003 return err;
1005 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TREE].imsg_fd =
1006 imsg_fds[0];
1007 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TREE].pid = pid;
1008 imsg_init(ibuf, imsg_fds[0]);
1009 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TREE].ibuf = ibuf;
1012 return request_tree(tree, repo, obj_fd, id);
1015 static const struct got_error *
1016 open_tree(struct got_tree_object **tree, struct got_repository *repo,
1017 struct got_object_id *id, int check_cache)
1019 const struct got_error *err = NULL;
1020 struct got_packidx *packidx = NULL;
1021 int idx;
1022 char *path_packfile = NULL;
1024 if (check_cache) {
1025 *tree = got_repo_get_cached_tree(repo, id);
1026 if (*tree != NULL) {
1027 (*tree)->refcnt++;
1028 return NULL;
1030 } else
1031 *tree = NULL;
1033 err = got_repo_search_packidx(&packidx, &idx, repo, id);
1034 if (err == NULL) {
1035 struct got_pack *pack = NULL;
1037 err = got_packidx_get_packfile_path(&path_packfile,
1038 packidx->path_packidx);
1039 if (err)
1040 return err;
1042 pack = got_repo_get_cached_pack(repo, path_packfile);
1043 if (pack == NULL) {
1044 err = got_repo_cache_pack(&pack, repo, path_packfile,
1045 packidx);
1046 if (err)
1047 goto done;
1049 err = read_packed_tree_privsep(tree, pack,
1050 packidx, idx, id);
1051 } else if (err->code == GOT_ERR_NO_OBJ) {
1052 int fd;
1054 err = got_object_open_loose_fd(&fd, id, repo);
1055 if (err)
1056 return err;
1057 err = read_tree_privsep(tree, fd, id, repo);
1060 if (err == NULL) {
1061 (*tree)->refcnt++;
1062 err = got_repo_cache_tree(repo, id, *tree);
1064 done:
1065 free(path_packfile);
1066 return err;
1069 const struct got_error *
1070 got_object_open_as_tree(struct got_tree_object **tree,
1071 struct got_repository *repo, struct got_object_id *id)
1073 *tree = got_repo_get_cached_tree(repo, id);
1074 if (*tree != NULL) {
1075 (*tree)->refcnt++;
1076 return NULL;
1079 return open_tree(tree, repo, id, 0);
1082 const struct got_error *
1083 got_object_tree_open(struct got_tree_object **tree,
1084 struct got_repository *repo, struct got_object *obj)
1086 return open_tree(tree, repo, got_object_get_id(obj), 1);
1089 int
1090 got_object_tree_get_nentries(struct got_tree_object *tree)
1092 return tree->nentries;
1095 struct got_tree_entry *
1096 got_object_tree_get_first_entry(struct got_tree_object *tree)
1098 return got_object_tree_get_entry(tree, 0);
1101 struct got_tree_entry *
1102 got_object_tree_get_last_entry(struct got_tree_object *tree)
1104 return got_object_tree_get_entry(tree, tree->nentries - 1);
1107 struct got_tree_entry *
1108 got_object_tree_get_entry(struct got_tree_object *tree, int i)
1110 if (i < 0 || i >= tree->nentries)
1111 return NULL;
1112 return &tree->entries[i];
1115 mode_t
1116 got_tree_entry_get_mode(struct got_tree_entry *te)
1118 return te->mode;
1121 const char *
1122 got_tree_entry_get_name(struct got_tree_entry *te)
1124 return &te->name[0];
1127 struct got_object_id *
1128 got_tree_entry_get_id(struct got_tree_entry *te)
1130 return &te->id;
1133 const struct got_error *
1134 got_object_blob_read_to_str(char **s, struct got_blob_object *blob)
1136 const struct got_error *err = NULL;
1137 size_t len, totlen, hdrlen, offset;
1139 *s = NULL;
1141 hdrlen = got_object_blob_get_hdrlen(blob);
1142 totlen = 0;
1143 offset = 0;
1144 do {
1145 char *p;
1147 err = got_object_blob_read_block(&len, blob);
1148 if (err)
1149 return err;
1151 if (len == 0)
1152 break;
1154 totlen += len - hdrlen;
1155 p = realloc(*s, totlen + 1);
1156 if (p == NULL) {
1157 err = got_error_from_errno("realloc");
1158 free(*s);
1159 *s = NULL;
1160 return err;
1162 *s = p;
1163 /* Skip blob object header first time around. */
1164 memcpy(*s + offset,
1165 got_object_blob_get_read_buf(blob) + hdrlen, len - hdrlen);
1166 hdrlen = 0;
1167 offset = totlen;
1168 } while (len > 0);
1170 (*s)[totlen] = '\0';
1171 return NULL;
1174 const struct got_error *
1175 got_tree_entry_get_symlink_target(char **link_target, struct got_tree_entry *te,
1176 struct got_repository *repo)
1178 const struct got_error *err = NULL;
1179 struct got_blob_object *blob = NULL;
1181 *link_target = NULL;
1183 if (!got_object_tree_entry_is_symlink(te))
1184 return got_error(GOT_ERR_TREE_ENTRY_TYPE);
1186 err = got_object_open_as_blob(&blob, repo,
1187 got_tree_entry_get_id(te), PATH_MAX);
1188 if (err)
1189 return err;
1191 err = got_object_blob_read_to_str(link_target, blob);
1192 got_object_blob_close(blob);
1193 if (err) {
1194 free(*link_target);
1195 *link_target = NULL;
1197 return err;
1200 int
1201 got_tree_entry_get_index(struct got_tree_entry *te)
1203 return te->idx;
1206 struct got_tree_entry *
1207 got_tree_entry_get_next(struct got_tree_object *tree,
1208 struct got_tree_entry *te)
1210 return got_object_tree_get_entry(tree, te->idx + 1);
1213 struct got_tree_entry *
1214 got_tree_entry_get_prev(struct got_tree_object *tree,
1215 struct got_tree_entry *te)
1217 return got_object_tree_get_entry(tree, te->idx - 1);
1220 static const struct got_error *
1221 request_packed_blob(uint8_t **outbuf, size_t *size, size_t *hdrlen, int outfd,
1222 struct got_pack *pack, struct got_packidx *packidx, int idx,
1223 struct got_object_id *id)
1225 const struct got_error *err = NULL;
1226 int outfd_child;
1227 int basefd, accumfd; /* temporary files for delta application */
1229 basefd = got_opentempfd();
1230 if (basefd == -1)
1231 return got_error_from_errno("got_opentempfd");
1232 accumfd = got_opentempfd();
1233 if (accumfd == -1)
1234 return got_error_from_errno("got_opentempfd");
1236 outfd_child = dup(outfd);
1237 if (outfd_child == -1)
1238 return got_error_from_errno("dup");
1240 err = got_privsep_send_blob_req(pack->privsep_child->ibuf, -1, id, idx);
1241 if (err)
1242 return err;
1244 err = got_privsep_send_blob_outfd(pack->privsep_child->ibuf,
1245 outfd_child);
1246 if (err) {
1247 close(basefd);
1248 close(accumfd);
1249 return err;
1252 err = got_privsep_send_tmpfd(pack->privsep_child->ibuf,
1253 basefd);
1254 if (err) {
1255 close(accumfd);
1256 return err;
1259 err = got_privsep_send_tmpfd(pack->privsep_child->ibuf,
1260 accumfd);
1261 if (err)
1262 return err;
1264 err = got_privsep_recv_blob(outbuf, size, hdrlen,
1265 pack->privsep_child->ibuf);
1266 if (err)
1267 return err;
1269 if (lseek(outfd, SEEK_SET, 0) == -1)
1270 err = got_error_from_errno("lseek");
1272 return err;
1275 static const struct got_error *
1276 read_packed_blob_privsep(uint8_t **outbuf, size_t *size, size_t *hdrlen,
1277 int outfd, struct got_pack *pack, struct got_packidx *packidx, int idx,
1278 struct got_object_id *id)
1280 const struct got_error *err = NULL;
1282 if (pack->privsep_child == NULL) {
1283 err = start_pack_privsep_child(pack, packidx);
1284 if (err)
1285 return err;
1288 return request_packed_blob(outbuf, size, hdrlen, outfd, pack, packidx,
1289 idx, id);
1292 static const struct got_error *
1293 request_blob(uint8_t **outbuf, size_t *size, size_t *hdrlen, int outfd,
1294 int infd, struct got_object_id *id, struct imsgbuf *ibuf)
1296 const struct got_error *err = NULL;
1297 int outfd_child;
1299 outfd_child = dup(outfd);
1300 if (outfd_child == -1)
1301 return got_error_from_errno("dup");
1303 err = got_privsep_send_blob_req(ibuf, infd, id, -1);
1304 if (err)
1305 return err;
1307 err = got_privsep_send_blob_outfd(ibuf, outfd_child);
1308 if (err)
1309 return err;
1311 err = got_privsep_recv_blob(outbuf, size, hdrlen, ibuf);
1312 if (err)
1313 return err;
1315 if (lseek(outfd, SEEK_SET, 0) == -1)
1316 return got_error_from_errno("lseek");
1318 return err;
1321 static const struct got_error *
1322 read_blob_privsep(uint8_t **outbuf, size_t *size, size_t *hdrlen,
1323 int outfd, int infd, struct got_object_id *id, struct got_repository *repo)
1325 const struct got_error *err;
1326 int imsg_fds[2];
1327 pid_t pid;
1328 struct imsgbuf *ibuf;
1330 if (repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_BLOB].imsg_fd != -1) {
1331 ibuf = repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_BLOB].ibuf;
1332 return request_blob(outbuf, size, hdrlen, outfd, infd, id,
1333 ibuf);
1336 ibuf = calloc(1, sizeof(*ibuf));
1337 if (ibuf == NULL)
1338 return got_error_from_errno("calloc");
1340 if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_fds) == -1) {
1341 err = got_error_from_errno("socketpair");
1342 free(ibuf);
1343 return err;
1346 pid = fork();
1347 if (pid == -1) {
1348 err = got_error_from_errno("fork");
1349 free(ibuf);
1350 return err;
1352 else if (pid == 0) {
1353 got_privsep_exec_child(imsg_fds, GOT_PATH_PROG_READ_BLOB,
1354 repo->path);
1355 /* not reached */
1358 if (close(imsg_fds[1]) == -1) {
1359 err = got_error_from_errno("close");
1360 free(ibuf);
1361 return err;
1363 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_BLOB].imsg_fd =
1364 imsg_fds[0];
1365 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_BLOB].pid = pid;
1366 imsg_init(ibuf, imsg_fds[0]);
1367 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_BLOB].ibuf = ibuf;
1369 return request_blob(outbuf, size, hdrlen, outfd, infd, id, ibuf);
1372 static const struct got_error *
1373 open_blob(struct got_blob_object **blob, struct got_repository *repo,
1374 struct got_object_id *id, size_t blocksize)
1376 const struct got_error *err = NULL;
1377 struct got_packidx *packidx = NULL;
1378 int idx;
1379 char *path_packfile = NULL;
1380 uint8_t *outbuf;
1381 int outfd;
1382 size_t size, hdrlen;
1383 struct stat sb;
1385 *blob = calloc(1, sizeof(**blob));
1386 if (*blob == NULL)
1387 return got_error_from_errno("calloc");
1389 outfd = got_opentempfd();
1390 if (outfd == -1)
1391 return got_error_from_errno("got_opentempfd");
1393 (*blob)->read_buf = malloc(blocksize);
1394 if ((*blob)->read_buf == NULL) {
1395 err = got_error_from_errno("malloc");
1396 goto done;
1399 err = got_repo_search_packidx(&packidx, &idx, repo, id);
1400 if (err == NULL) {
1401 struct got_pack *pack = NULL;
1403 err = got_packidx_get_packfile_path(&path_packfile,
1404 packidx->path_packidx);
1405 if (err)
1406 goto done;
1408 pack = got_repo_get_cached_pack(repo, path_packfile);
1409 if (pack == NULL) {
1410 err = got_repo_cache_pack(&pack, repo, path_packfile,
1411 packidx);
1412 if (err)
1413 goto done;
1415 err = read_packed_blob_privsep(&outbuf, &size, &hdrlen, outfd,
1416 pack, packidx, idx, id);
1417 } else if (err->code == GOT_ERR_NO_OBJ) {
1418 int infd;
1420 err = got_object_open_loose_fd(&infd, id, repo);
1421 if (err)
1422 goto done;
1423 err = read_blob_privsep(&outbuf, &size, &hdrlen, outfd, infd,
1424 id, repo);
1426 if (err)
1427 goto done;
1429 if (hdrlen > size) {
1430 err = got_error(GOT_ERR_BAD_OBJ_HDR);
1431 goto done;
1434 if (outbuf) {
1435 if (close(outfd) == -1 && err == NULL)
1436 err = got_error_from_errno("close");
1437 outfd = -1;
1438 (*blob)->f = fmemopen(outbuf, size, "rb");
1439 if ((*blob)->f == NULL) {
1440 err = got_error_from_errno("fmemopen");
1441 free(outbuf);
1442 goto done;
1444 (*blob)->data = outbuf;
1445 } else {
1446 if (fstat(outfd, &sb) == -1) {
1447 err = got_error_from_errno("fstat");
1448 goto done;
1451 if (sb.st_size != size) {
1452 err = got_error(GOT_ERR_PRIVSEP_LEN);
1453 goto done;
1456 (*blob)->f = fdopen(outfd, "rb");
1457 if ((*blob)->f == NULL) {
1458 err = got_error_from_errno("fdopen");
1459 close(outfd);
1460 outfd = -1;
1461 goto done;
1465 (*blob)->hdrlen = hdrlen;
1466 (*blob)->blocksize = blocksize;
1467 memcpy(&(*blob)->id.sha1, id->sha1, SHA1_DIGEST_LENGTH);
1469 done:
1470 free(path_packfile);
1471 if (err) {
1472 if (*blob) {
1473 got_object_blob_close(*blob);
1474 *blob = NULL;
1475 } else if (outfd != -1)
1476 close(outfd);
1478 return err;
1481 const struct got_error *
1482 got_object_open_as_blob(struct got_blob_object **blob,
1483 struct got_repository *repo, struct got_object_id *id,
1484 size_t blocksize)
1486 return open_blob(blob, repo, id, blocksize);
1489 const struct got_error *
1490 got_object_blob_open(struct got_blob_object **blob,
1491 struct got_repository *repo, struct got_object *obj, size_t blocksize)
1493 return open_blob(blob, repo, got_object_get_id(obj), blocksize);
1496 const struct got_error *
1497 got_object_blob_close(struct got_blob_object *blob)
1499 const struct got_error *err = NULL;
1500 free(blob->read_buf);
1501 if (blob->f && fclose(blob->f) == EOF)
1502 err = got_error_from_errno("fclose");
1503 free(blob->data);
1504 free(blob);
1505 return err;
1508 void
1509 got_object_blob_rewind(struct got_blob_object *blob)
1511 if (blob->f)
1512 rewind(blob->f);
1515 char *
1516 got_object_blob_id_str(struct got_blob_object *blob, char *buf, size_t size)
1518 return got_sha1_digest_to_str(blob->id.sha1, buf, size);
1521 size_t
1522 got_object_blob_get_hdrlen(struct got_blob_object *blob)
1524 return blob->hdrlen;
1527 const uint8_t *
1528 got_object_blob_get_read_buf(struct got_blob_object *blob)
1530 return blob->read_buf;
1533 const struct got_error *
1534 got_object_blob_read_block(size_t *outlenp, struct got_blob_object *blob)
1536 size_t n;
1538 n = fread(blob->read_buf, 1, blob->blocksize, blob->f);
1539 if (n == 0 && ferror(blob->f))
1540 return got_ferror(blob->f, GOT_ERR_IO);
1541 *outlenp = n;
1542 return NULL;
1545 const struct got_error *
1546 got_object_blob_dump_to_file(off_t *filesize, int *nlines,
1547 off_t **line_offsets, FILE *outfile, struct got_blob_object *blob)
1549 const struct got_error *err = NULL;
1550 size_t n, len, hdrlen;
1551 const uint8_t *buf;
1552 int i;
1553 const int alloc_chunksz = 512;
1554 size_t nalloc = 0;
1555 off_t off = 0, total_len = 0;
1557 if (line_offsets)
1558 *line_offsets = NULL;
1559 if (filesize)
1560 *filesize = 0;
1561 if (nlines)
1562 *nlines = 0;
1564 hdrlen = got_object_blob_get_hdrlen(blob);
1565 do {
1566 err = got_object_blob_read_block(&len, blob);
1567 if (err)
1568 return err;
1569 if (len == 0)
1570 break;
1571 buf = got_object_blob_get_read_buf(blob);
1572 i = hdrlen;
1573 if (nlines) {
1574 if (line_offsets && *line_offsets == NULL) {
1575 /* Have some data but perhaps no '\n'. */
1576 *nlines = 1;
1577 nalloc = alloc_chunksz;
1578 *line_offsets = calloc(nalloc,
1579 sizeof(**line_offsets));
1580 if (*line_offsets == NULL)
1581 return got_error_from_errno("calloc");
1583 /* Skip forward over end of first line. */
1584 while (i < len) {
1585 if (buf[i] == '\n')
1586 break;
1587 i++;
1590 /* Scan '\n' offsets in remaining chunk of data. */
1591 while (i < len) {
1592 if (buf[i] != '\n') {
1593 i++;
1594 continue;
1596 (*nlines)++;
1597 if (line_offsets && nalloc < *nlines) {
1598 size_t n = *nlines + alloc_chunksz;
1599 off_t *o = recallocarray(*line_offsets,
1600 nalloc, n, sizeof(**line_offsets));
1601 if (o == NULL) {
1602 free(*line_offsets);
1603 *line_offsets = NULL;
1604 return got_error_from_errno(
1605 "recallocarray");
1607 *line_offsets = o;
1608 nalloc = n;
1610 if (line_offsets) {
1611 off = total_len + i - hdrlen + 1;
1612 (*line_offsets)[*nlines - 1] = off;
1614 i++;
1617 /* Skip blob object header first time around. */
1618 n = fwrite(buf + hdrlen, 1, len - hdrlen, outfile);
1619 if (n != len - hdrlen)
1620 return got_ferror(outfile, GOT_ERR_IO);
1621 total_len += len - hdrlen;
1622 hdrlen = 0;
1623 } while (len != 0);
1625 if (fflush(outfile) != 0)
1626 return got_error_from_errno("fflush");
1627 rewind(outfile);
1629 if (filesize)
1630 *filesize = total_len;
1632 return NULL;
1635 static const struct got_error *
1636 request_packed_tag(struct got_tag_object **tag, struct got_pack *pack,
1637 int pack_idx, struct got_object_id *id)
1639 const struct got_error *err = NULL;
1641 err = got_privsep_send_tag_req(pack->privsep_child->ibuf, -1, id,
1642 pack_idx);
1643 if (err)
1644 return err;
1646 return got_privsep_recv_tag(tag, pack->privsep_child->ibuf);
1649 static const struct got_error *
1650 read_packed_tag_privsep(struct got_tag_object **tag,
1651 struct got_pack *pack, struct got_packidx *packidx, int idx,
1652 struct got_object_id *id)
1654 const struct got_error *err = NULL;
1656 if (pack->privsep_child)
1657 return request_packed_tag(tag, pack, idx, id);
1659 err = start_pack_privsep_child(pack, packidx);
1660 if (err)
1661 return err;
1663 return request_packed_tag(tag, pack, idx, id);
1666 static const struct got_error *
1667 request_tag(struct got_tag_object **tag, struct got_repository *repo,
1668 int fd, struct got_object_id *id)
1670 const struct got_error *err = NULL;
1671 struct imsgbuf *ibuf;
1673 ibuf = repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TAG].ibuf;
1675 err = got_privsep_send_tag_req(ibuf, fd, id, -1);
1676 if (err)
1677 return err;
1679 return got_privsep_recv_tag(tag, ibuf);
1682 static const struct got_error *
1683 read_tag_privsep(struct got_tag_object **tag, int obj_fd,
1684 struct got_object_id *id, struct got_repository *repo)
1686 const struct got_error *err;
1687 int imsg_fds[2];
1688 pid_t pid;
1689 struct imsgbuf *ibuf;
1691 if (repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TAG].imsg_fd != -1)
1692 return request_tag(tag, repo, obj_fd, id);
1694 ibuf = calloc(1, sizeof(*ibuf));
1695 if (ibuf == NULL)
1696 return got_error_from_errno("calloc");
1698 if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_fds) == -1) {
1699 err = got_error_from_errno("socketpair");
1700 free(ibuf);
1701 return err;
1704 pid = fork();
1705 if (pid == -1) {
1706 err = got_error_from_errno("fork");
1707 free(ibuf);
1708 return err;
1710 else if (pid == 0) {
1711 got_privsep_exec_child(imsg_fds, GOT_PATH_PROG_READ_TAG,
1712 repo->path);
1713 /* not reached */
1716 if (close(imsg_fds[1]) == -1) {
1717 err = got_error_from_errno("close");
1718 free(ibuf);
1719 return err;
1721 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TAG].imsg_fd =
1722 imsg_fds[0];
1723 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TAG].pid = pid;
1724 imsg_init(ibuf, imsg_fds[0]);
1725 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TAG].ibuf = ibuf;
1727 return request_tag(tag, repo, obj_fd, id);
1730 static const struct got_error *
1731 open_tag(struct got_tag_object **tag, struct got_repository *repo,
1732 struct got_object_id *id, int check_cache)
1734 const struct got_error *err = NULL;
1735 struct got_packidx *packidx = NULL;
1736 int idx;
1737 char *path_packfile = NULL;
1738 struct got_object *obj = NULL;
1739 int obj_type = GOT_OBJ_TYPE_ANY;
1741 if (check_cache) {
1742 *tag = got_repo_get_cached_tag(repo, id);
1743 if (*tag != NULL) {
1744 (*tag)->refcnt++;
1745 return NULL;
1747 } else
1748 *tag = NULL;
1750 err = got_repo_search_packidx(&packidx, &idx, repo, id);
1751 if (err == NULL) {
1752 struct got_pack *pack = NULL;
1754 err = got_packidx_get_packfile_path(&path_packfile,
1755 packidx->path_packidx);
1756 if (err)
1757 return err;
1759 pack = got_repo_get_cached_pack(repo, path_packfile);
1760 if (pack == NULL) {
1761 err = got_repo_cache_pack(&pack, repo, path_packfile,
1762 packidx);
1763 if (err)
1764 goto done;
1767 /* Beware of "lightweight" tags: Check object type first. */
1768 err = read_packed_object_privsep(&obj, repo, pack, packidx,
1769 idx, id);
1770 if (err)
1771 goto done;
1772 obj_type = obj->type;
1773 got_object_close(obj);
1774 if (obj_type != GOT_OBJ_TYPE_TAG) {
1775 err = got_error(GOT_ERR_OBJ_TYPE);
1776 goto done;
1778 err = read_packed_tag_privsep(tag, pack, packidx, idx, id);
1779 } else if (err->code == GOT_ERR_NO_OBJ) {
1780 int fd;
1782 err = got_object_open_loose_fd(&fd, id, repo);
1783 if (err)
1784 return err;
1785 err = got_object_read_header_privsep(&obj, id, repo, fd);
1786 if (err)
1787 return err;
1788 obj_type = obj->type;
1789 got_object_close(obj);
1790 if (obj_type != GOT_OBJ_TYPE_TAG)
1791 return got_error(GOT_ERR_OBJ_TYPE);
1793 err = got_object_open_loose_fd(&fd, id, repo);
1794 if (err)
1795 return err;
1796 err = read_tag_privsep(tag, fd, id, repo);
1799 if (err == NULL) {
1800 (*tag)->refcnt++;
1801 err = got_repo_cache_tag(repo, id, *tag);
1803 done:
1804 free(path_packfile);
1805 return err;
1808 const struct got_error *
1809 got_object_open_as_tag(struct got_tag_object **tag,
1810 struct got_repository *repo, struct got_object_id *id)
1812 *tag = got_repo_get_cached_tag(repo, id);
1813 if (*tag != NULL) {
1814 (*tag)->refcnt++;
1815 return NULL;
1818 return open_tag(tag, repo, id, 0);
1821 const struct got_error *
1822 got_object_tag_open(struct got_tag_object **tag,
1823 struct got_repository *repo, struct got_object *obj)
1825 return open_tag(tag, repo, got_object_get_id(obj), 1);
1828 const char *
1829 got_object_tag_get_name(struct got_tag_object *tag)
1831 return tag->tag;
1834 int
1835 got_object_tag_get_object_type(struct got_tag_object *tag)
1837 return tag->obj_type;
1840 struct got_object_id *
1841 got_object_tag_get_object_id(struct got_tag_object *tag)
1843 return &tag->id;
1846 time_t
1847 got_object_tag_get_tagger_time(struct got_tag_object *tag)
1849 return tag->tagger_time;
1852 time_t
1853 got_object_tag_get_tagger_gmtoff(struct got_tag_object *tag)
1855 return tag->tagger_gmtoff;
1858 const char *
1859 got_object_tag_get_tagger(struct got_tag_object *tag)
1861 return tag->tagger;
1864 const char *
1865 got_object_tag_get_message(struct got_tag_object *tag)
1867 return tag->tagmsg;
1870 static struct got_tree_entry *
1871 find_entry_by_name(struct got_tree_object *tree, const char *name, size_t len)
1873 int i;
1875 /* Note that tree entries are sorted in strncmp() order. */
1876 for (i = 0; i < tree->nentries; i++) {
1877 struct got_tree_entry *te = &tree->entries[i];
1878 int cmp = strncmp(te->name, name, len);
1879 if (cmp < 0)
1880 continue;
1881 if (cmp > 0)
1882 break;
1883 if (te->name[len] == '\0')
1884 return te;
1886 return NULL;
1889 struct got_tree_entry *
1890 got_object_tree_find_entry(struct got_tree_object *tree, const char *name)
1892 return find_entry_by_name(tree, name, strlen(name));
1895 const struct got_error *
1896 got_object_tree_find_path(struct got_object_id **id, mode_t *mode,
1897 struct got_repository *repo, struct got_tree_object *tree,
1898 const char *path)
1900 const struct got_error *err = NULL;
1901 struct got_tree_object *subtree = NULL;
1902 struct got_tree_entry *te = NULL;
1903 const char *seg, *s;
1904 size_t seglen;
1906 *id = NULL;
1908 s = path;
1909 while (s[0] == '/')
1910 s++;
1911 seg = s;
1912 seglen = 0;
1913 subtree = tree;
1914 while (*s) {
1915 struct got_tree_object *next_tree;
1917 if (*s != '/') {
1918 s++;
1919 seglen++;
1920 if (*s)
1921 continue;
1924 te = find_entry_by_name(subtree, seg, seglen);
1925 if (te == NULL) {
1926 err = got_error_path(path, GOT_ERR_NO_TREE_ENTRY);
1927 goto done;
1930 if (*s == '\0')
1931 break;
1933 seg = s + 1;
1934 seglen = 0;
1935 s++;
1936 if (*s) {
1937 err = got_object_open_as_tree(&next_tree, repo,
1938 &te->id);
1939 te = NULL;
1940 if (err)
1941 goto done;
1942 if (subtree != tree)
1943 got_object_tree_close(subtree);
1944 subtree = next_tree;
1948 if (te) {
1949 *id = got_object_id_dup(&te->id);
1950 if (*id == NULL)
1951 return got_error_from_errno("got_object_id_dup");
1952 if (mode)
1953 *mode = te->mode;
1954 } else
1955 err = got_error_path(path, GOT_ERR_NO_TREE_ENTRY);
1956 done:
1957 if (subtree && subtree != tree)
1958 got_object_tree_close(subtree);
1959 return err;
1961 const struct got_error *
1962 got_object_id_by_path(struct got_object_id **id, struct got_repository *repo,
1963 struct got_object_id *commit_id, const char *path)
1965 const struct got_error *err = NULL;
1966 struct got_commit_object *commit = NULL;
1967 struct got_tree_object *tree = NULL;
1969 *id = NULL;
1971 err = got_object_open_as_commit(&commit, repo, commit_id);
1972 if (err)
1973 goto done;
1975 /* Handle opening of root of commit's tree. */
1976 if (got_path_is_root_dir(path)) {
1977 *id = got_object_id_dup(commit->tree_id);
1978 if (*id == NULL)
1979 err = got_error_from_errno("got_object_id_dup");
1980 } else {
1981 err = got_object_open_as_tree(&tree, repo, commit->tree_id);
1982 if (err)
1983 goto done;
1984 err = got_object_tree_find_path(id, NULL, repo, tree, path);
1986 done:
1987 if (commit)
1988 got_object_commit_close(commit);
1989 if (tree)
1990 got_object_tree_close(tree);
1991 return err;
1995 * Normalize file mode bits to avoid false positive tree entry differences
1996 * in case tree entries have unexpected mode bits set.
1998 static mode_t
1999 normalize_mode_for_comparison(mode_t mode)
2002 * For directories, the only relevant bit is the IFDIR bit.
2003 * This allows us to detect paths changing from a directory
2004 * to a file and vice versa.
2006 if (S_ISDIR(mode))
2007 return mode & S_IFDIR;
2010 * For symlinks, the only relevant bit is the IFLNK bit.
2011 * This allows us to detect paths changing from a symlinks
2012 * to a file or directory and vice versa.
2014 if (S_ISLNK(mode))
2015 return mode & S_IFLNK;
2017 /* For files, the only change we care about is the executable bit. */
2018 return mode & S_IXUSR;
2021 const struct got_error *
2022 got_object_tree_path_changed(int *changed,
2023 struct got_tree_object *tree01, struct got_tree_object *tree02,
2024 const char *path, struct got_repository *repo)
2026 const struct got_error *err = NULL;
2027 struct got_tree_object *tree1 = NULL, *tree2 = NULL;
2028 struct got_tree_entry *te1 = NULL, *te2 = NULL;
2029 const char *seg, *s;
2030 size_t seglen;
2032 *changed = 0;
2034 /* We not do support comparing the root path. */
2035 if (got_path_is_root_dir(path))
2036 return got_error_path(path, GOT_ERR_BAD_PATH);
2038 tree1 = tree01;
2039 tree2 = tree02;
2040 s = path;
2041 while (*s == '/')
2042 s++;
2043 seg = s;
2044 seglen = 0;
2045 while (*s) {
2046 struct got_tree_object *next_tree1, *next_tree2;
2047 mode_t mode1, mode2;
2049 if (*s != '/') {
2050 s++;
2051 seglen++;
2052 if (*s)
2053 continue;
2056 te1 = find_entry_by_name(tree1, seg, seglen);
2057 if (te1 == NULL) {
2058 err = got_error(GOT_ERR_NO_OBJ);
2059 goto done;
2062 if (tree2)
2063 te2 = find_entry_by_name(tree2, seg, seglen);
2065 if (te2) {
2066 mode1 = normalize_mode_for_comparison(te1->mode);
2067 mode2 = normalize_mode_for_comparison(te2->mode);
2068 if (mode1 != mode2) {
2069 *changed = 1;
2070 goto done;
2073 if (got_object_id_cmp(&te1->id, &te2->id) == 0) {
2074 *changed = 0;
2075 goto done;
2079 if (*s == '\0') { /* final path element */
2080 *changed = 1;
2081 goto done;
2084 seg = s + 1;
2085 s++;
2086 seglen = 0;
2087 if (*s) {
2088 err = got_object_open_as_tree(&next_tree1, repo,
2089 &te1->id);
2090 te1 = NULL;
2091 if (err)
2092 goto done;
2093 if (tree1 != tree01)
2094 got_object_tree_close(tree1);
2095 tree1 = next_tree1;
2097 if (te2) {
2098 err = got_object_open_as_tree(&next_tree2, repo,
2099 &te2->id);
2100 te2 = NULL;
2101 if (err)
2102 goto done;
2103 if (tree2 != tree02)
2104 got_object_tree_close(tree2);
2105 tree2 = next_tree2;
2106 } else if (tree2) {
2107 if (tree2 != tree02)
2108 got_object_tree_close(tree2);
2109 tree2 = NULL;
2113 done:
2114 if (tree1 && tree1 != tree01)
2115 got_object_tree_close(tree1);
2116 if (tree2 && tree2 != tree02)
2117 got_object_tree_close(tree2);
2118 return err;
2121 const struct got_error *
2122 got_object_tree_entry_dup(struct got_tree_entry **new_te,
2123 struct got_tree_entry *te)
2125 const struct got_error *err = NULL;
2127 *new_te = calloc(1, sizeof(**new_te));
2128 if (*new_te == NULL)
2129 return got_error_from_errno("calloc");
2131 (*new_te)->mode = te->mode;
2132 memcpy((*new_te)->name, te->name, sizeof((*new_te)->name));
2133 memcpy(&(*new_te)->id, &te->id, sizeof((*new_te)->id));
2134 return err;
2137 int
2138 got_object_tree_entry_is_submodule(struct got_tree_entry *te)
2140 return (te->mode & S_IFMT) == (S_IFDIR | S_IFLNK);
2143 int
2144 got_object_tree_entry_is_symlink(struct got_tree_entry *te)
2146 /* S_IFDIR check avoids confusing symlinks with submodules. */
2147 return ((te->mode & (S_IFDIR | S_IFLNK)) == S_IFLNK);
2150 static const struct got_error *
2151 resolve_symlink(char **link_target, const char *path,
2152 struct got_object_id *commit_id, struct got_repository *repo)
2154 const struct got_error *err = NULL;
2155 char buf[PATH_MAX];
2156 char *name, *parent_path = NULL;
2157 struct got_object_id *tree_obj_id = NULL;
2158 struct got_tree_object *tree = NULL;
2159 struct got_tree_entry *te = NULL;
2161 *link_target = NULL;
2163 if (strlcpy(buf, path, sizeof(buf)) >= sizeof(buf))
2164 return got_error(GOT_ERR_NO_SPACE);
2166 name = basename(buf);
2167 if (name == NULL)
2168 return got_error_from_errno2("basename", path);
2170 err = got_path_dirname(&parent_path, path);
2171 if (err)
2172 return err;
2174 err = got_object_id_by_path(&tree_obj_id, repo, commit_id,
2175 parent_path);
2176 if (err) {
2177 if (err->code == GOT_ERR_NO_TREE_ENTRY) {
2178 /* Display the complete path in error message. */
2179 err = got_error_path(path, err->code);
2181 goto done;
2184 err = got_object_open_as_tree(&tree, repo, tree_obj_id);
2185 if (err)
2186 goto done;
2188 te = got_object_tree_find_entry(tree, name);
2189 if (te == NULL) {
2190 err = got_error_path(path, GOT_ERR_NO_TREE_ENTRY);
2191 goto done;
2194 if (got_object_tree_entry_is_symlink(te)) {
2195 err = got_tree_entry_get_symlink_target(link_target, te, repo);
2196 if (err)
2197 goto done;
2198 if (!got_path_is_absolute(*link_target)) {
2199 char *abspath;
2200 if (asprintf(&abspath, "%s/%s", parent_path,
2201 *link_target) == -1) {
2202 err = got_error_from_errno("asprintf");
2203 goto done;
2205 free(*link_target);
2206 *link_target = malloc(PATH_MAX);
2207 if (*link_target == NULL) {
2208 err = got_error_from_errno("malloc");
2209 goto done;
2211 err = got_canonpath(abspath, *link_target, PATH_MAX);
2212 free(abspath);
2213 if (err)
2214 goto done;
2217 done:
2218 free(tree_obj_id);
2219 if (tree)
2220 got_object_tree_close(tree);
2221 if (err) {
2222 free(*link_target);
2223 *link_target = NULL;
2225 return err;
2228 const struct got_error *
2229 got_object_resolve_symlinks(char **link_target, const char *path,
2230 struct got_object_id *commit_id, struct got_repository *repo)
2232 const struct got_error *err = NULL;
2233 char *next_target = NULL;
2234 int max_recursion = 40; /* matches Git */
2236 *link_target = NULL;
2238 do {
2239 err = resolve_symlink(&next_target,
2240 *link_target ? *link_target : path, commit_id, repo);
2241 if (err)
2242 break;
2243 if (next_target) {
2244 free(*link_target);
2245 if (--max_recursion == 0) {
2246 err = got_error_path(path, GOT_ERR_RECURSION);
2247 *link_target = NULL;
2248 break;
2250 *link_target = next_target;
2252 } while (next_target);
2254 return err;
2257 const struct got_error *
2258 got_traverse_packed_commits(struct got_object_id_queue *traversed_commits,
2259 struct got_object_id *commit_id, const char *path,
2260 struct got_repository *repo)
2262 const struct got_error *err = NULL;
2263 struct got_pack *pack = NULL;
2264 struct got_packidx *packidx = NULL;
2265 char *path_packfile = NULL;
2266 struct got_commit_object *changed_commit = NULL;
2267 struct got_object_id *changed_commit_id = NULL;
2268 int idx;
2270 err = got_repo_search_packidx(&packidx, &idx, repo, commit_id);
2271 if (err) {
2272 if (err->code != GOT_ERR_NO_OBJ)
2273 return err;
2274 return NULL;
2277 err = got_packidx_get_packfile_path(&path_packfile,
2278 packidx->path_packidx);
2279 if (err)
2280 return err;
2282 pack = got_repo_get_cached_pack(repo, path_packfile);
2283 if (pack == NULL) {
2284 err = got_repo_cache_pack(&pack, repo, path_packfile, packidx);
2285 if (err)
2286 goto done;
2289 if (pack->privsep_child == NULL) {
2290 err = start_pack_privsep_child(pack, packidx);
2291 if (err)
2292 goto done;
2295 err = got_privsep_send_commit_traversal_request(
2296 pack->privsep_child->ibuf, commit_id, idx, path);
2297 if (err)
2298 goto done;
2300 err = got_privsep_recv_traversed_commits(&changed_commit,
2301 &changed_commit_id, traversed_commits, pack->privsep_child->ibuf);
2302 if (err)
2303 goto done;
2305 if (changed_commit) {
2307 * Cache the commit in which the path was changed.
2308 * This commit might be opened again soon.
2310 changed_commit->refcnt++;
2311 err = got_repo_cache_commit(repo, changed_commit_id,
2312 changed_commit);
2313 got_object_commit_close(changed_commit);
2315 done:
2316 free(path_packfile);
2317 free(changed_commit_id);
2318 return err;