Blob


1 /*
2 * Copyright (c) 2018, 2019 Stefan Sperling <stsp@openbsd.org>
3 *
4 * Permission to use, copy, modify, and distribute this software for any
5 * purpose with or without fee is hereby granted, provided that the above
6 * copyright notice and this permission notice appear in all copies.
7 *
8 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
15 */
17 #include <sys/types.h>
18 #include <sys/stat.h>
19 #include <sys/queue.h>
20 #include <sys/uio.h>
21 #include <sys/socket.h>
22 #include <sys/wait.h>
23 #include <sys/resource.h>
25 #include <errno.h>
26 #include <fcntl.h>
27 #include <stdio.h>
28 #include <stdlib.h>
29 #include <string.h>
30 #include <stdint.h>
31 #include <sha1.h>
32 #include <unistd.h>
33 #include <zlib.h>
34 #include <ctype.h>
35 #include <libgen.h>
36 #include <limits.h>
37 #include <imsg.h>
38 #include <time.h>
40 #include "got_error.h"
41 #include "got_object.h"
42 #include "got_repository.h"
43 #include "got_opentemp.h"
44 #include "got_path.h"
46 #include "got_lib_sha1.h"
47 #include "got_lib_delta.h"
48 #include "got_lib_inflate.h"
49 #include "got_lib_object.h"
50 #include "got_lib_privsep.h"
51 #include "got_lib_object_idcache.h"
52 #include "got_lib_object_cache.h"
53 #include "got_lib_object_parse.h"
54 #include "got_lib_pack.h"
55 #include "got_lib_repository.h"
57 #ifndef MIN
58 #define MIN(_a,_b) ((_a) < (_b) ? (_a) : (_b))
59 #endif
61 struct got_object_id *
62 got_object_get_id(struct got_object *obj)
63 {
64 return &obj->id;
65 }
67 const struct got_error *
68 got_object_get_id_str(char **outbuf, struct got_object *obj)
69 {
70 return got_object_id_str(outbuf, &obj->id);
71 }
73 const struct got_error *
74 got_object_get_type(int *type, struct got_repository *repo,
75 struct got_object_id *id)
76 {
77 const struct got_error *err = NULL;
78 struct got_object *obj;
80 err = got_object_open(&obj, repo, id);
81 if (err)
82 return err;
84 switch (obj->type) {
85 case GOT_OBJ_TYPE_COMMIT:
86 case GOT_OBJ_TYPE_TREE:
87 case GOT_OBJ_TYPE_BLOB:
88 case GOT_OBJ_TYPE_TAG:
89 *type = obj->type;
90 break;
91 default:
92 err = got_error(GOT_ERR_OBJ_TYPE);
93 break;
94 }
96 got_object_close(obj);
97 return err;
98 }
100 const struct got_error *
101 got_object_get_path(char **path, struct got_object_id *id,
102 struct got_repository *repo)
104 const struct got_error *err = NULL;
105 char *hex = NULL;
106 char *path_objects;
108 *path = NULL;
110 path_objects = got_repo_get_path_objects(repo);
111 if (path_objects == NULL)
112 return got_error_from_errno("got_repo_get_path_objects");
114 err = got_object_id_str(&hex, id);
115 if (err)
116 goto done;
118 if (asprintf(path, "%s/%.2x/%s", path_objects,
119 id->sha1[0], hex + 2) == -1)
120 err = got_error_from_errno("asprintf");
122 done:
123 free(hex);
124 free(path_objects);
125 return err;
128 static const struct got_error *
129 open_loose_object(int *fd, struct got_object_id *id,
130 struct got_repository *repo)
132 const struct got_error *err = NULL;
133 char *path;
135 err = got_object_get_path(&path, id, repo);
136 if (err)
137 return err;
138 *fd = open(path, O_RDONLY | O_NOFOLLOW);
139 if (*fd == -1) {
140 err = got_error_from_errno2("open", path);
141 goto done;
143 done:
144 free(path);
145 return err;
148 static const struct got_error *
149 get_packfile_path(char **path_packfile, struct got_packidx *packidx)
151 size_t size;
153 /* Packfile path contains ".pack" instead of ".idx", so add one byte. */
154 size = strlen(packidx->path_packidx) + 2;
155 if (size < GOT_PACKFILE_NAMELEN + 1)
156 return got_error_path(packidx->path_packidx, GOT_ERR_BAD_PATH);
158 *path_packfile = malloc(size);
159 if (*path_packfile == NULL)
160 return got_error_from_errno("malloc");
162 /* Copy up to and excluding ".idx". */
163 if (strlcpy(*path_packfile, packidx->path_packidx,
164 size - strlen(GOT_PACKIDX_SUFFIX) - 1) >= size)
165 return got_error(GOT_ERR_NO_SPACE);
167 if (strlcat(*path_packfile, GOT_PACKFILE_SUFFIX, size) >= size)
168 return got_error(GOT_ERR_NO_SPACE);
170 return NULL;
173 static const struct got_error *
174 request_packed_object(struct got_object **obj, struct got_pack *pack, int idx,
175 struct got_object_id *id)
177 const struct got_error *err = NULL;
178 struct imsgbuf *ibuf = pack->privsep_child->ibuf;
180 err = got_privsep_send_packed_obj_req(ibuf, idx, id);
181 if (err)
182 return err;
184 err = got_privsep_recv_obj(obj, ibuf);
185 if (err)
186 return err;
188 memcpy(&(*obj)->id, id, sizeof((*obj)->id));
190 return NULL;
193 static const struct got_error *
194 request_packed_object_raw(uint8_t **outbuf, off_t *size, size_t *hdrlen,
195 int outfd, struct got_pack *pack, int idx, struct got_object_id *id)
197 const struct got_error *err = NULL;
198 struct imsgbuf *ibuf = pack->privsep_child->ibuf;
199 int outfd_child;
200 int basefd, accumfd; /* temporary files for delta application */
202 basefd = got_opentempfd();
203 if (basefd == -1)
204 return got_error_from_errno("got_opentempfd");
206 accumfd = got_opentempfd();
207 if (accumfd == -1) {
208 close(basefd);
209 return got_error_from_errno("got_opentempfd");
212 outfd_child = dup(outfd);
213 if (outfd_child == -1) {
214 err = got_error_from_errno("dup");
215 close(basefd);
216 close(accumfd);
217 return err;
220 err = got_privsep_send_packed_raw_obj_req(ibuf, idx, id);
221 if (err) {
222 close(basefd);
223 close(accumfd);
224 close(outfd_child);
225 return err;
228 err = got_privsep_send_raw_obj_outfd(ibuf, outfd_child);
229 if (err) {
230 close(basefd);
231 close(accumfd);
232 return err;
236 err = got_privsep_send_tmpfd(pack->privsep_child->ibuf,
237 basefd);
238 if (err) {
239 close(accumfd);
240 return err;
243 err = got_privsep_send_tmpfd(pack->privsep_child->ibuf,
244 accumfd);
245 if (err)
246 return err;
248 err = got_privsep_recv_raw_obj(outbuf, size, hdrlen, ibuf);
249 if (err)
250 return err;
252 return NULL;
255 static void
256 set_max_datasize(void)
258 struct rlimit rl;
260 if (getrlimit(RLIMIT_DATA, &rl) != 0)
261 return;
263 rl.rlim_cur = rl.rlim_max;
264 setrlimit(RLIMIT_DATA, &rl);
267 static const struct got_error *
268 start_pack_privsep_child(struct got_pack *pack, struct got_packidx *packidx)
270 const struct got_error *err = NULL;
271 int imsg_fds[2];
272 pid_t pid;
273 struct imsgbuf *ibuf;
275 ibuf = calloc(1, sizeof(*ibuf));
276 if (ibuf == NULL)
277 return got_error_from_errno("calloc");
279 pack->privsep_child = calloc(1, sizeof(*pack->privsep_child));
280 if (pack->privsep_child == NULL) {
281 err = got_error_from_errno("calloc");
282 free(ibuf);
283 return err;
286 if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_fds) == -1) {
287 err = got_error_from_errno("socketpair");
288 goto done;
291 pid = fork();
292 if (pid == -1) {
293 err = got_error_from_errno("fork");
294 goto done;
295 } else if (pid == 0) {
296 set_max_datasize();
297 got_privsep_exec_child(imsg_fds, GOT_PATH_PROG_READ_PACK,
298 pack->path_packfile);
299 /* not reached */
302 if (close(imsg_fds[1]) == -1)
303 return got_error_from_errno("close");
304 pack->privsep_child->imsg_fd = imsg_fds[0];
305 pack->privsep_child->pid = pid;
306 imsg_init(ibuf, imsg_fds[0]);
307 pack->privsep_child->ibuf = ibuf;
309 err = got_privsep_init_pack_child(ibuf, pack, packidx);
310 if (err) {
311 const struct got_error *child_err;
312 err = got_privsep_send_stop(pack->privsep_child->imsg_fd);
313 child_err = got_privsep_wait_for_child(
314 pack->privsep_child->pid);
315 if (child_err && err == NULL)
316 err = child_err;
318 done:
319 if (err) {
320 free(ibuf);
321 free(pack->privsep_child);
322 pack->privsep_child = NULL;
324 return err;
327 static const struct got_error *
328 read_packed_object_privsep(struct got_object **obj,
329 struct got_repository *repo, struct got_pack *pack,
330 struct got_packidx *packidx, int idx, struct got_object_id *id)
332 const struct got_error *err = NULL;
334 if (pack->privsep_child == NULL) {
335 err = start_pack_privsep_child(pack, packidx);
336 if (err)
337 return err;
340 return request_packed_object(obj, pack, idx, id);
343 static const struct got_error *
344 read_packed_object_raw_privsep(uint8_t **outbuf, off_t *size, size_t *hdrlen,
345 int outfd, struct got_pack *pack, struct got_packidx *packidx, int idx,
346 struct got_object_id *id)
348 const struct got_error *err = NULL;
350 if (pack->privsep_child == NULL) {
351 err = start_pack_privsep_child(pack, packidx);
352 if (err)
353 return err;
356 return request_packed_object_raw(outbuf, size, hdrlen, outfd, pack,
357 idx, id);
360 static const struct got_error *
361 open_packed_object(struct got_object **obj, struct got_object_id *id,
362 struct got_repository *repo)
364 const struct got_error *err = NULL;
365 struct got_pack *pack = NULL;
366 struct got_packidx *packidx = NULL;
367 int idx;
368 char *path_packfile;
370 err = got_repo_search_packidx(&packidx, &idx, repo, id);
371 if (err)
372 return err;
374 err = get_packfile_path(&path_packfile, packidx);
375 if (err)
376 return err;
378 pack = got_repo_get_cached_pack(repo, path_packfile);
379 if (pack == NULL) {
380 err = got_repo_cache_pack(&pack, repo, path_packfile, packidx);
381 if (err)
382 goto done;
385 err = read_packed_object_privsep(obj, repo, pack, packidx, idx, id);
386 if (err)
387 goto done;
388 done:
389 free(path_packfile);
390 return err;
393 static const struct got_error *
394 request_object(struct got_object **obj, struct got_repository *repo, int fd)
396 const struct got_error *err = NULL;
397 struct imsgbuf *ibuf;
399 ibuf = repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_OBJECT].ibuf;
401 err = got_privsep_send_obj_req(ibuf, fd);
402 if (err)
403 return err;
405 return got_privsep_recv_obj(obj, ibuf);
408 static const struct got_error *
409 request_raw_object(uint8_t **outbuf, off_t *size, size_t *hdrlen, int outfd,
410 struct got_repository *repo, int infd)
412 const struct got_error *err = NULL;
413 struct imsgbuf *ibuf;
414 int outfd_child;
416 ibuf = repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_OBJECT].ibuf;
418 outfd_child = dup(outfd);
419 if (outfd_child == -1)
420 return got_error_from_errno("dup");
422 err = got_privsep_send_raw_obj_req(ibuf, infd);
423 if (err)
424 return err;
426 err = got_privsep_send_raw_obj_outfd(ibuf, outfd_child);
427 if (err)
428 return err;
430 return got_privsep_recv_raw_obj(outbuf, size, hdrlen, ibuf);
433 static const struct got_error *
434 start_read_object_child(struct got_repository *repo)
436 const struct got_error *err = NULL;
437 int imsg_fds[2];
438 pid_t pid;
439 struct imsgbuf *ibuf;
441 ibuf = calloc(1, sizeof(*ibuf));
442 if (ibuf == NULL)
443 return got_error_from_errno("calloc");
445 if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_fds) == -1) {
446 err = got_error_from_errno("socketpair");
447 free(ibuf);
448 return err;
451 pid = fork();
452 if (pid == -1) {
453 err = got_error_from_errno("fork");
454 free(ibuf);
455 return err;
457 else if (pid == 0) {
458 got_privsep_exec_child(imsg_fds, GOT_PATH_PROG_READ_OBJECT,
459 repo->path);
460 /* not reached */
463 if (close(imsg_fds[1]) == -1) {
464 err = got_error_from_errno("close");
465 free(ibuf);
466 return err;
469 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_OBJECT].imsg_fd =
470 imsg_fds[0];
471 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_OBJECT].pid = pid;
472 imsg_init(ibuf, imsg_fds[0]);
473 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_OBJECT].ibuf = ibuf;
475 return NULL;
478 static const struct got_error *
479 read_object_header_privsep(struct got_object **obj, struct got_repository *repo,
480 int obj_fd)
482 const struct got_error *err;
484 if (repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_OBJECT].imsg_fd != -1)
485 return request_object(obj, repo, obj_fd);
487 err = start_read_object_child(repo);
488 if (err)
489 return err;
491 return request_object(obj, repo, obj_fd);
494 static const struct got_error *
495 read_object_raw_privsep(uint8_t **outbuf, off_t *size, size_t *hdrlen,
496 int outfd, struct got_repository *repo, int obj_fd)
498 const struct got_error *err;
500 if (repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_OBJECT].imsg_fd != -1)
501 return request_raw_object(outbuf, size, hdrlen, outfd, repo,
502 obj_fd);
504 err = start_read_object_child(repo);
505 if (err)
506 return err;
508 return request_raw_object(outbuf, size, hdrlen, outfd, repo, obj_fd);
511 const struct got_error *
512 got_object_open(struct got_object **obj, struct got_repository *repo,
513 struct got_object_id *id)
515 const struct got_error *err = NULL;
516 char *path;
517 int fd;
519 *obj = got_repo_get_cached_object(repo, id);
520 if (*obj != NULL) {
521 (*obj)->refcnt++;
522 return NULL;
525 err = open_packed_object(obj, id, repo);
526 if (err && err->code != GOT_ERR_NO_OBJ)
527 return err;
528 if (*obj) {
529 (*obj)->refcnt++;
530 return got_repo_cache_object(repo, id, *obj);
533 err = got_object_get_path(&path, id, repo);
534 if (err)
535 return err;
537 fd = open(path, O_RDONLY | O_NOFOLLOW);
538 if (fd == -1) {
539 if (errno == ENOENT)
540 err = got_error_no_obj(id);
541 else
542 err = got_error_from_errno2("open", path);
543 goto done;
544 } else {
545 err = read_object_header_privsep(obj, repo, fd);
546 if (err)
547 goto done;
548 memcpy((*obj)->id.sha1, id->sha1, SHA1_DIGEST_LENGTH);
551 (*obj)->refcnt++;
552 err = got_repo_cache_object(repo, id, *obj);
553 done:
554 free(path);
555 return err;
558 const struct got_error *
559 got_object_raw_open(struct got_raw_object **obj, struct got_repository *repo,
560 struct got_object_id *id, size_t blocksize)
562 const struct got_error *err = NULL;
563 struct got_packidx *packidx = NULL;
564 int idx;
565 uint8_t *outbuf = NULL;
566 int outfd = -1;
567 off_t size = 0;
568 size_t hdrlen = 0;
569 char *path_packfile = NULL;
571 *obj = NULL;
573 outfd = got_opentempfd();
574 if (outfd == -1)
575 return got_error_from_errno("got_opentempfd");
577 err = got_repo_search_packidx(&packidx, &idx, repo, id);
578 if (err == NULL) {
579 struct got_pack *pack = NULL;
581 err = get_packfile_path(&path_packfile, packidx);
582 if (err)
583 goto done;
585 pack = got_repo_get_cached_pack(repo, path_packfile);
586 if (pack == NULL) {
587 err = got_repo_cache_pack(&pack, repo, path_packfile,
588 packidx);
589 if (err)
590 goto done;
592 err = read_packed_object_raw_privsep(&outbuf, &size, &hdrlen,
593 outfd, pack, packidx, idx, id);
594 } else if (err->code == GOT_ERR_NO_OBJ) {
595 int fd;
597 err = open_loose_object(&fd, id, repo);
598 if (err)
599 goto done;
600 err = read_object_raw_privsep(&outbuf, &size, &hdrlen, outfd,
601 repo, fd);
604 if (hdrlen > size) {
605 err = got_error(GOT_ERR_BAD_OBJ_HDR);
606 goto done;
609 *obj = calloc(1, sizeof(**obj));
610 if (*obj == NULL) {
611 err = got_error_from_errno("calloc");
612 goto done;
615 (*obj)->read_buf = malloc(blocksize);
616 if ((*obj)->read_buf == NULL) {
617 err = got_error_from_errno("malloc");
618 goto done;
621 if (outbuf) {
622 if (close(outfd) == -1) {
623 err = got_error_from_errno("close");
624 goto done;
626 outfd = -1;
627 (*obj)->f = fmemopen(outbuf, hdrlen + size, "r");
628 if ((*obj)->f == NULL) {
629 err = got_error_from_errno("fdopen");
630 goto done;
632 (*obj)->data = outbuf;
633 } else {
634 struct stat sb;
635 if (fstat(outfd, &sb) == -1) {
636 err = got_error_from_errno("fstat");
637 goto done;
640 if (sb.st_size != size) {
641 err = got_error(GOT_ERR_PRIVSEP_LEN);
642 goto done;
645 (*obj)->f = fdopen(outfd, "r");
646 if ((*obj)->f == NULL) {
647 err = got_error_from_errno("fdopen");
648 goto done;
650 outfd = -1;
651 (*obj)->data = NULL;
653 (*obj)->hdrlen = hdrlen;
654 (*obj)->size = size;
655 (*obj)->blocksize = blocksize;
656 done:
657 free(path_packfile);
658 if (err) {
659 if (*obj) {
660 got_object_raw_close(*obj);
661 *obj = NULL;
663 if (outfd != -1)
664 close(outfd);
665 free(outbuf);
667 return err;
670 void
671 got_object_raw_rewind(struct got_raw_object *obj)
673 if (obj->f)
674 rewind(obj->f);
677 size_t
678 got_object_raw_get_hdrlen(struct got_raw_object *obj)
680 return obj->hdrlen;
683 const uint8_t *
684 got_object_raw_get_read_buf(struct got_raw_object *obj)
686 return obj->read_buf;
689 const struct got_error *
690 got_object_raw_read_block(size_t *outlenp, struct got_raw_object *obj)
692 size_t n;
694 n = fread(obj->read_buf, 1, obj->blocksize, obj->f);
695 if (n == 0 && ferror(obj->f))
696 return got_ferror(obj->f, GOT_ERR_IO);
697 *outlenp = n;
698 return NULL;
701 const struct got_error *
702 got_object_raw_close(struct got_raw_object *obj)
704 const struct got_error *err = NULL;
706 free(obj->read_buf);
707 if (obj->f != NULL && fclose(obj->f) == EOF && err == NULL)
708 err = got_error_from_errno("fclose");
709 free(obj->data);
710 free(obj);
711 return err;
714 const struct got_error *
715 got_object_open_by_id_str(struct got_object **obj, struct got_repository *repo,
716 const char *id_str)
718 struct got_object_id id;
720 if (!got_parse_sha1_digest(id.sha1, id_str))
721 return got_error_path(id_str, GOT_ERR_BAD_OBJ_ID_STR);
723 return got_object_open(obj, repo, &id);
726 const struct got_error *
727 got_object_resolve_id_str(struct got_object_id **id,
728 struct got_repository *repo, const char *id_str)
730 const struct got_error *err = NULL;
731 struct got_object *obj;
733 err = got_object_open_by_id_str(&obj, repo, id_str);
734 if (err)
735 return err;
737 *id = got_object_id_dup(got_object_get_id(obj));
738 got_object_close(obj);
739 if (*id == NULL)
740 return got_error_from_errno("got_object_id_dup");
742 return NULL;
745 static const struct got_error *
746 request_packed_commit(struct got_commit_object **commit, struct got_pack *pack,
747 int pack_idx, struct got_object_id *id)
749 const struct got_error *err = NULL;
751 err = got_privsep_send_commit_req(pack->privsep_child->ibuf, -1, id,
752 pack_idx);
753 if (err)
754 return err;
756 err = got_privsep_recv_commit(commit, pack->privsep_child->ibuf);
757 if (err)
758 return err;
760 (*commit)->flags |= GOT_COMMIT_FLAG_PACKED;
761 return NULL;
764 static const struct got_error *
765 read_packed_commit_privsep(struct got_commit_object **commit,
766 struct got_pack *pack, struct got_packidx *packidx, int idx,
767 struct got_object_id *id)
769 const struct got_error *err = NULL;
771 if (pack->privsep_child)
772 return request_packed_commit(commit, pack, idx, id);
774 err = start_pack_privsep_child(pack, packidx);
775 if (err)
776 return err;
778 return request_packed_commit(commit, pack, idx, id);
781 static const struct got_error *
782 request_commit(struct got_commit_object **commit, struct got_repository *repo,
783 int fd)
785 const struct got_error *err = NULL;
786 struct imsgbuf *ibuf;
788 ibuf = repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_COMMIT].ibuf;
790 err = got_privsep_send_commit_req(ibuf, fd, NULL, -1);
791 if (err)
792 return err;
794 return got_privsep_recv_commit(commit, ibuf);
797 static const struct got_error *
798 read_commit_privsep(struct got_commit_object **commit, int obj_fd,
799 struct got_repository *repo)
801 const struct got_error *err;
802 int imsg_fds[2];
803 pid_t pid;
804 struct imsgbuf *ibuf;
806 if (repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_COMMIT].imsg_fd != -1)
807 return request_commit(commit, repo, obj_fd);
809 ibuf = calloc(1, sizeof(*ibuf));
810 if (ibuf == NULL)
811 return got_error_from_errno("calloc");
813 if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_fds) == -1) {
814 err = got_error_from_errno("socketpair");
815 free(ibuf);
816 return err;
819 pid = fork();
820 if (pid == -1) {
821 err = got_error_from_errno("fork");
822 free(ibuf);
823 return err;
825 else if (pid == 0) {
826 got_privsep_exec_child(imsg_fds, GOT_PATH_PROG_READ_COMMIT,
827 repo->path);
828 /* not reached */
831 if (close(imsg_fds[1]) == -1) {
832 err = got_error_from_errno("close");
833 free(ibuf);
834 return err;
836 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_COMMIT].imsg_fd =
837 imsg_fds[0];
838 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_COMMIT].pid = pid;
839 imsg_init(ibuf, imsg_fds[0]);
840 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_COMMIT].ibuf = ibuf;
842 return request_commit(commit, repo, obj_fd);
846 static const struct got_error *
847 open_commit(struct got_commit_object **commit,
848 struct got_repository *repo, struct got_object_id *id, int check_cache)
850 const struct got_error *err = NULL;
851 struct got_packidx *packidx = NULL;
852 int idx;
853 char *path_packfile = NULL;
855 if (check_cache) {
856 *commit = got_repo_get_cached_commit(repo, id);
857 if (*commit != NULL) {
858 (*commit)->refcnt++;
859 return NULL;
861 } else
862 *commit = NULL;
864 err = got_repo_search_packidx(&packidx, &idx, repo, id);
865 if (err == NULL) {
866 struct got_pack *pack = NULL;
868 err = get_packfile_path(&path_packfile, packidx);
869 if (err)
870 return err;
872 pack = got_repo_get_cached_pack(repo, path_packfile);
873 if (pack == NULL) {
874 err = got_repo_cache_pack(&pack, repo, path_packfile,
875 packidx);
876 if (err)
877 goto done;
879 err = read_packed_commit_privsep(commit, pack,
880 packidx, idx, id);
881 } else if (err->code == GOT_ERR_NO_OBJ) {
882 int fd;
884 err = open_loose_object(&fd, id, repo);
885 if (err)
886 return err;
887 err = read_commit_privsep(commit, fd, repo);
890 if (err == NULL) {
891 (*commit)->refcnt++;
892 err = got_repo_cache_commit(repo, id, *commit);
894 done:
895 free(path_packfile);
896 return err;
899 const struct got_error *
900 got_object_open_as_commit(struct got_commit_object **commit,
901 struct got_repository *repo, struct got_object_id *id)
903 *commit = got_repo_get_cached_commit(repo, id);
904 if (*commit != NULL) {
905 (*commit)->refcnt++;
906 return NULL;
909 return open_commit(commit, repo, id, 0);
912 const struct got_error *
913 got_object_commit_open(struct got_commit_object **commit,
914 struct got_repository *repo, struct got_object *obj)
916 return open_commit(commit, repo, got_object_get_id(obj), 1);
919 const struct got_error *
920 got_object_qid_alloc(struct got_object_qid **qid, struct got_object_id *id)
922 const struct got_error *err = NULL;
924 *qid = calloc(1, sizeof(**qid));
925 if (*qid == NULL)
926 return got_error_from_errno("calloc");
928 (*qid)->id = got_object_id_dup(id);
929 if ((*qid)->id == NULL) {
930 err = got_error_from_errno("got_object_id_dup");
931 got_object_qid_free(*qid);
932 *qid = NULL;
933 return err;
936 return NULL;
939 static const struct got_error *
940 request_packed_tree(struct got_tree_object **tree, struct got_pack *pack,
941 int pack_idx, struct got_object_id *id)
943 const struct got_error *err = NULL;
945 err = got_privsep_send_tree_req(pack->privsep_child->ibuf, -1, id,
946 pack_idx);
947 if (err)
948 return err;
950 return got_privsep_recv_tree(tree, pack->privsep_child->ibuf);
953 static const struct got_error *
954 read_packed_tree_privsep(struct got_tree_object **tree,
955 struct got_pack *pack, struct got_packidx *packidx, int idx,
956 struct got_object_id *id)
958 const struct got_error *err = NULL;
960 if (pack->privsep_child)
961 return request_packed_tree(tree, pack, idx, id);
963 err = start_pack_privsep_child(pack, packidx);
964 if (err)
965 return err;
967 return request_packed_tree(tree, pack, idx, id);
970 static const struct got_error *
971 request_tree(struct got_tree_object **tree, struct got_repository *repo,
972 int fd)
974 const struct got_error *err = NULL;
975 struct imsgbuf *ibuf;
977 ibuf = repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TREE].ibuf;
979 err = got_privsep_send_tree_req(ibuf, fd, NULL, -1);
980 if (err)
981 return err;
983 return got_privsep_recv_tree(tree, ibuf);
986 const struct got_error *
987 read_tree_privsep(struct got_tree_object **tree, int obj_fd,
988 struct got_repository *repo)
990 const struct got_error *err;
991 int imsg_fds[2];
992 pid_t pid;
993 struct imsgbuf *ibuf;
995 if (repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TREE].imsg_fd != -1)
996 return request_tree(tree, repo, obj_fd);
998 ibuf = calloc(1, sizeof(*ibuf));
999 if (ibuf == NULL)
1000 return got_error_from_errno("calloc");
1002 if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_fds) == -1) {
1003 err = got_error_from_errno("socketpair");
1004 free(ibuf);
1005 return err;
1008 pid = fork();
1009 if (pid == -1) {
1010 err = got_error_from_errno("fork");
1011 free(ibuf);
1012 return err;
1014 else if (pid == 0) {
1015 got_privsep_exec_child(imsg_fds, GOT_PATH_PROG_READ_TREE,
1016 repo->path);
1017 /* not reached */
1020 if (close(imsg_fds[1]) == -1) {
1021 err = got_error_from_errno("close");
1022 free(ibuf);
1023 return err;
1025 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TREE].imsg_fd =
1026 imsg_fds[0];
1027 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TREE].pid = pid;
1028 imsg_init(ibuf, imsg_fds[0]);
1029 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TREE].ibuf = ibuf;
1032 return request_tree(tree, repo, obj_fd);
1035 static const struct got_error *
1036 open_tree(struct got_tree_object **tree, struct got_repository *repo,
1037 struct got_object_id *id, int check_cache)
1039 const struct got_error *err = NULL;
1040 struct got_packidx *packidx = NULL;
1041 int idx;
1042 char *path_packfile = NULL;
1044 if (check_cache) {
1045 *tree = got_repo_get_cached_tree(repo, id);
1046 if (*tree != NULL) {
1047 (*tree)->refcnt++;
1048 return NULL;
1050 } else
1051 *tree = NULL;
1053 err = got_repo_search_packidx(&packidx, &idx, repo, id);
1054 if (err == NULL) {
1055 struct got_pack *pack = NULL;
1057 err = get_packfile_path(&path_packfile, packidx);
1058 if (err)
1059 return err;
1061 pack = got_repo_get_cached_pack(repo, path_packfile);
1062 if (pack == NULL) {
1063 err = got_repo_cache_pack(&pack, repo, path_packfile,
1064 packidx);
1065 if (err)
1066 goto done;
1068 err = read_packed_tree_privsep(tree, pack,
1069 packidx, idx, id);
1070 } else if (err->code == GOT_ERR_NO_OBJ) {
1071 int fd;
1073 err = open_loose_object(&fd, id, repo);
1074 if (err)
1075 return err;
1076 err = read_tree_privsep(tree, fd, repo);
1079 if (err == NULL) {
1080 (*tree)->refcnt++;
1081 err = got_repo_cache_tree(repo, id, *tree);
1083 done:
1084 free(path_packfile);
1085 return err;
1088 const struct got_error *
1089 got_object_open_as_tree(struct got_tree_object **tree,
1090 struct got_repository *repo, struct got_object_id *id)
1092 *tree = got_repo_get_cached_tree(repo, id);
1093 if (*tree != NULL) {
1094 (*tree)->refcnt++;
1095 return NULL;
1098 return open_tree(tree, repo, id, 0);
1101 const struct got_error *
1102 got_object_tree_open(struct got_tree_object **tree,
1103 struct got_repository *repo, struct got_object *obj)
1105 return open_tree(tree, repo, got_object_get_id(obj), 1);
1108 int
1109 got_object_tree_get_nentries(struct got_tree_object *tree)
1111 return tree->nentries;
1114 struct got_tree_entry *
1115 got_object_tree_get_first_entry(struct got_tree_object *tree)
1117 return got_object_tree_get_entry(tree, 0);
1120 struct got_tree_entry *
1121 got_object_tree_get_last_entry(struct got_tree_object *tree)
1123 return got_object_tree_get_entry(tree, tree->nentries - 1);
1126 struct got_tree_entry *
1127 got_object_tree_get_entry(struct got_tree_object *tree, int i)
1129 if (i < 0 || i >= tree->nentries)
1130 return NULL;
1131 return &tree->entries[i];
1134 mode_t
1135 got_tree_entry_get_mode(struct got_tree_entry *te)
1137 return te->mode;
1140 const char *
1141 got_tree_entry_get_name(struct got_tree_entry *te)
1143 return &te->name[0];
1146 struct got_object_id *
1147 got_tree_entry_get_id(struct got_tree_entry *te)
1149 return &te->id;
1152 const struct got_error *
1153 got_object_blob_read_to_str(char **s, struct got_blob_object *blob)
1155 const struct got_error *err = NULL;
1156 size_t len, totlen, hdrlen, offset;
1158 *s = NULL;
1160 hdrlen = got_object_blob_get_hdrlen(blob);
1161 totlen = 0;
1162 offset = 0;
1163 do {
1164 char *p;
1166 err = got_object_blob_read_block(&len, blob);
1167 if (err)
1168 return err;
1170 if (len == 0)
1171 break;
1173 totlen += len - hdrlen;
1174 p = realloc(*s, totlen + 1);
1175 if (p == NULL) {
1176 err = got_error_from_errno("realloc");
1177 free(*s);
1178 *s = NULL;
1179 return err;
1181 *s = p;
1182 /* Skip blob object header first time around. */
1183 memcpy(*s + offset,
1184 got_object_blob_get_read_buf(blob) + hdrlen, len - hdrlen);
1185 hdrlen = 0;
1186 offset = totlen;
1187 } while (len > 0);
1189 (*s)[totlen] = '\0';
1190 return NULL;
1193 const struct got_error *
1194 got_tree_entry_get_symlink_target(char **link_target, struct got_tree_entry *te,
1195 struct got_repository *repo)
1197 const struct got_error *err = NULL;
1198 struct got_blob_object *blob = NULL;
1200 *link_target = NULL;
1202 if (!got_object_tree_entry_is_symlink(te))
1203 return got_error(GOT_ERR_TREE_ENTRY_TYPE);
1205 err = got_object_open_as_blob(&blob, repo,
1206 got_tree_entry_get_id(te), PATH_MAX);
1207 if (err)
1208 return err;
1210 err = got_object_blob_read_to_str(link_target, blob);
1211 got_object_blob_close(blob);
1212 if (err) {
1213 free(*link_target);
1214 *link_target = NULL;
1216 return err;
1219 int
1220 got_tree_entry_get_index(struct got_tree_entry *te)
1222 return te->idx;
1225 struct got_tree_entry *
1226 got_tree_entry_get_next(struct got_tree_object *tree,
1227 struct got_tree_entry *te)
1229 return got_object_tree_get_entry(tree, te->idx + 1);
1232 struct got_tree_entry *
1233 got_tree_entry_get_prev(struct got_tree_object *tree,
1234 struct got_tree_entry *te)
1236 return got_object_tree_get_entry(tree, te->idx - 1);
1239 static const struct got_error *
1240 request_packed_blob(uint8_t **outbuf, size_t *size, size_t *hdrlen, int outfd,
1241 struct got_pack *pack, struct got_packidx *packidx, int idx,
1242 struct got_object_id *id)
1244 const struct got_error *err = NULL;
1245 int outfd_child;
1246 int basefd, accumfd; /* temporary files for delta application */
1248 basefd = got_opentempfd();
1249 if (basefd == -1)
1250 return got_error_from_errno("got_opentempfd");
1251 accumfd = got_opentempfd();
1252 if (accumfd == -1)
1253 return got_error_from_errno("got_opentempfd");
1255 outfd_child = dup(outfd);
1256 if (outfd_child == -1)
1257 return got_error_from_errno("dup");
1259 err = got_privsep_send_blob_req(pack->privsep_child->ibuf, -1, id, idx);
1260 if (err)
1261 return err;
1263 err = got_privsep_send_blob_outfd(pack->privsep_child->ibuf,
1264 outfd_child);
1265 if (err) {
1266 close(basefd);
1267 close(accumfd);
1268 return err;
1271 err = got_privsep_send_tmpfd(pack->privsep_child->ibuf,
1272 basefd);
1273 if (err) {
1274 close(accumfd);
1275 return err;
1278 err = got_privsep_send_tmpfd(pack->privsep_child->ibuf,
1279 accumfd);
1280 if (err)
1281 return err;
1283 err = got_privsep_recv_blob(outbuf, size, hdrlen,
1284 pack->privsep_child->ibuf);
1285 if (err)
1286 return err;
1288 if (lseek(outfd, SEEK_SET, 0) == -1)
1289 err = got_error_from_errno("lseek");
1291 return err;
1294 static const struct got_error *
1295 read_packed_blob_privsep(uint8_t **outbuf, size_t *size, size_t *hdrlen,
1296 int outfd, struct got_pack *pack, struct got_packidx *packidx, int idx,
1297 struct got_object_id *id)
1299 const struct got_error *err = NULL;
1301 if (pack->privsep_child == NULL) {
1302 err = start_pack_privsep_child(pack, packidx);
1303 if (err)
1304 return err;
1307 return request_packed_blob(outbuf, size, hdrlen, outfd, pack, packidx,
1308 idx, id);
1311 static const struct got_error *
1312 request_blob(uint8_t **outbuf, size_t *size, size_t *hdrlen, int outfd,
1313 int infd, struct imsgbuf *ibuf)
1315 const struct got_error *err = NULL;
1316 int outfd_child;
1318 outfd_child = dup(outfd);
1319 if (outfd_child == -1)
1320 return got_error_from_errno("dup");
1322 err = got_privsep_send_blob_req(ibuf, infd, NULL, -1);
1323 if (err)
1324 return err;
1326 err = got_privsep_send_blob_outfd(ibuf, outfd_child);
1327 if (err)
1328 return err;
1330 err = got_privsep_recv_blob(outbuf, size, hdrlen, ibuf);
1331 if (err)
1332 return err;
1334 if (lseek(outfd, SEEK_SET, 0) == -1)
1335 return got_error_from_errno("lseek");
1337 return err;
1340 static const struct got_error *
1341 read_blob_privsep(uint8_t **outbuf, size_t *size, size_t *hdrlen,
1342 int outfd, int infd, struct got_repository *repo)
1344 const struct got_error *err;
1345 int imsg_fds[2];
1346 pid_t pid;
1347 struct imsgbuf *ibuf;
1349 if (repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_BLOB].imsg_fd != -1) {
1350 ibuf = repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_BLOB].ibuf;
1351 return request_blob(outbuf, size, hdrlen, outfd, infd, ibuf);
1354 ibuf = calloc(1, sizeof(*ibuf));
1355 if (ibuf == NULL)
1356 return got_error_from_errno("calloc");
1358 if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_fds) == -1) {
1359 err = got_error_from_errno("socketpair");
1360 free(ibuf);
1361 return err;
1364 pid = fork();
1365 if (pid == -1) {
1366 err = got_error_from_errno("fork");
1367 free(ibuf);
1368 return err;
1370 else if (pid == 0) {
1371 got_privsep_exec_child(imsg_fds, GOT_PATH_PROG_READ_BLOB,
1372 repo->path);
1373 /* not reached */
1376 if (close(imsg_fds[1]) == -1) {
1377 err = got_error_from_errno("close");
1378 free(ibuf);
1379 return err;
1381 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_BLOB].imsg_fd =
1382 imsg_fds[0];
1383 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_BLOB].pid = pid;
1384 imsg_init(ibuf, imsg_fds[0]);
1385 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_BLOB].ibuf = ibuf;
1387 return request_blob(outbuf, size, hdrlen, outfd, infd, ibuf);
1390 static const struct got_error *
1391 open_blob(struct got_blob_object **blob, struct got_repository *repo,
1392 struct got_object_id *id, size_t blocksize)
1394 const struct got_error *err = NULL;
1395 struct got_packidx *packidx = NULL;
1396 int idx;
1397 char *path_packfile = NULL;
1398 uint8_t *outbuf;
1399 int outfd;
1400 size_t size, hdrlen;
1401 struct stat sb;
1403 *blob = calloc(1, sizeof(**blob));
1404 if (*blob == NULL)
1405 return got_error_from_errno("calloc");
1407 outfd = got_opentempfd();
1408 if (outfd == -1)
1409 return got_error_from_errno("got_opentempfd");
1411 (*blob)->read_buf = malloc(blocksize);
1412 if ((*blob)->read_buf == NULL) {
1413 err = got_error_from_errno("malloc");
1414 goto done;
1417 err = got_repo_search_packidx(&packidx, &idx, repo, id);
1418 if (err == NULL) {
1419 struct got_pack *pack = NULL;
1421 err = get_packfile_path(&path_packfile, packidx);
1422 if (err)
1423 goto done;
1425 pack = got_repo_get_cached_pack(repo, path_packfile);
1426 if (pack == NULL) {
1427 err = got_repo_cache_pack(&pack, repo, path_packfile,
1428 packidx);
1429 if (err)
1430 goto done;
1432 err = read_packed_blob_privsep(&outbuf, &size, &hdrlen, outfd,
1433 pack, packidx, idx, id);
1434 } else if (err->code == GOT_ERR_NO_OBJ) {
1435 int infd;
1437 err = open_loose_object(&infd, id, repo);
1438 if (err)
1439 goto done;
1440 err = read_blob_privsep(&outbuf, &size, &hdrlen, outfd, infd,
1441 repo);
1443 if (err)
1444 goto done;
1446 if (hdrlen > size) {
1447 err = got_error(GOT_ERR_BAD_OBJ_HDR);
1448 goto done;
1451 if (outbuf) {
1452 if (close(outfd) == -1 && err == NULL)
1453 err = got_error_from_errno("close");
1454 outfd = -1;
1455 (*blob)->f = fmemopen(outbuf, size, "rb");
1456 if ((*blob)->f == NULL) {
1457 err = got_error_from_errno("fmemopen");
1458 free(outbuf);
1459 goto done;
1461 (*blob)->data = outbuf;
1462 } else {
1463 if (fstat(outfd, &sb) == -1) {
1464 err = got_error_from_errno("fstat");
1465 goto done;
1468 if (sb.st_size != size) {
1469 err = got_error(GOT_ERR_PRIVSEP_LEN);
1470 goto done;
1473 (*blob)->f = fdopen(outfd, "rb");
1474 if ((*blob)->f == NULL) {
1475 err = got_error_from_errno("fdopen");
1476 close(outfd);
1477 outfd = -1;
1478 goto done;
1482 (*blob)->hdrlen = hdrlen;
1483 (*blob)->blocksize = blocksize;
1484 memcpy(&(*blob)->id.sha1, id->sha1, SHA1_DIGEST_LENGTH);
1486 done:
1487 free(path_packfile);
1488 if (err) {
1489 if (*blob) {
1490 got_object_blob_close(*blob);
1491 *blob = NULL;
1492 } else if (outfd != -1)
1493 close(outfd);
1495 return err;
1498 const struct got_error *
1499 got_object_open_as_blob(struct got_blob_object **blob,
1500 struct got_repository *repo, struct got_object_id *id,
1501 size_t blocksize)
1503 return open_blob(blob, repo, id, blocksize);
1506 const struct got_error *
1507 got_object_blob_open(struct got_blob_object **blob,
1508 struct got_repository *repo, struct got_object *obj, size_t blocksize)
1510 return open_blob(blob, repo, got_object_get_id(obj), blocksize);
1513 const struct got_error *
1514 got_object_blob_close(struct got_blob_object *blob)
1516 const struct got_error *err = NULL;
1517 free(blob->read_buf);
1518 if (blob->f && fclose(blob->f) == EOF)
1519 err = got_error_from_errno("fclose");
1520 free(blob->data);
1521 free(blob);
1522 return err;
1525 void
1526 got_object_blob_rewind(struct got_blob_object *blob)
1528 if (blob->f)
1529 rewind(blob->f);
1532 char *
1533 got_object_blob_id_str(struct got_blob_object *blob, char *buf, size_t size)
1535 return got_sha1_digest_to_str(blob->id.sha1, buf, size);
1538 size_t
1539 got_object_blob_get_hdrlen(struct got_blob_object *blob)
1541 return blob->hdrlen;
1544 const uint8_t *
1545 got_object_blob_get_read_buf(struct got_blob_object *blob)
1547 return blob->read_buf;
1550 const struct got_error *
1551 got_object_blob_read_block(size_t *outlenp, struct got_blob_object *blob)
1553 size_t n;
1555 n = fread(blob->read_buf, 1, blob->blocksize, blob->f);
1556 if (n == 0 && ferror(blob->f))
1557 return got_ferror(blob->f, GOT_ERR_IO);
1558 *outlenp = n;
1559 return NULL;
1562 const struct got_error *
1563 got_object_blob_dump_to_file(off_t *filesize, int *nlines,
1564 off_t **line_offsets, FILE *outfile, struct got_blob_object *blob)
1566 const struct got_error *err = NULL;
1567 size_t n, len, hdrlen;
1568 const uint8_t *buf;
1569 int i;
1570 const int alloc_chunksz = 512;
1571 size_t nalloc = 0;
1572 off_t off = 0, total_len = 0;
1574 if (line_offsets)
1575 *line_offsets = NULL;
1576 if (filesize)
1577 *filesize = 0;
1578 if (nlines)
1579 *nlines = 0;
1581 hdrlen = got_object_blob_get_hdrlen(blob);
1582 do {
1583 err = got_object_blob_read_block(&len, blob);
1584 if (err)
1585 return err;
1586 if (len == 0)
1587 break;
1588 buf = got_object_blob_get_read_buf(blob);
1589 i = hdrlen;
1590 if (nlines) {
1591 if (line_offsets && *line_offsets == NULL) {
1592 /* Have some data but perhaps no '\n'. */
1593 *nlines = 1;
1594 nalloc = alloc_chunksz;
1595 *line_offsets = calloc(nalloc,
1596 sizeof(**line_offsets));
1597 if (*line_offsets == NULL)
1598 return got_error_from_errno("calloc");
1600 /* Skip forward over end of first line. */
1601 while (i < len) {
1602 if (buf[i] == '\n')
1603 break;
1604 i++;
1607 /* Scan '\n' offsets in remaining chunk of data. */
1608 while (i < len) {
1609 if (buf[i] != '\n') {
1610 i++;
1611 continue;
1613 (*nlines)++;
1614 if (line_offsets && nalloc < *nlines) {
1615 size_t n = *nlines + alloc_chunksz;
1616 off_t *o = recallocarray(*line_offsets,
1617 nalloc, n, sizeof(**line_offsets));
1618 if (o == NULL) {
1619 free(*line_offsets);
1620 *line_offsets = NULL;
1621 return got_error_from_errno(
1622 "recallocarray");
1624 *line_offsets = o;
1625 nalloc = n;
1627 if (line_offsets) {
1628 off = total_len + i - hdrlen + 1;
1629 (*line_offsets)[*nlines - 1] = off;
1631 i++;
1634 /* Skip blob object header first time around. */
1635 n = fwrite(buf + hdrlen, 1, len - hdrlen, outfile);
1636 if (n != len - hdrlen)
1637 return got_ferror(outfile, GOT_ERR_IO);
1638 total_len += len - hdrlen;
1639 hdrlen = 0;
1640 } while (len != 0);
1642 if (fflush(outfile) != 0)
1643 return got_error_from_errno("fflush");
1644 rewind(outfile);
1646 if (filesize)
1647 *filesize = total_len;
1649 return NULL;
1652 static const struct got_error *
1653 request_packed_tag(struct got_tag_object **tag, struct got_pack *pack,
1654 int pack_idx, struct got_object_id *id)
1656 const struct got_error *err = NULL;
1658 err = got_privsep_send_tag_req(pack->privsep_child->ibuf, -1, id,
1659 pack_idx);
1660 if (err)
1661 return err;
1663 return got_privsep_recv_tag(tag, pack->privsep_child->ibuf);
1666 static const struct got_error *
1667 read_packed_tag_privsep(struct got_tag_object **tag,
1668 struct got_pack *pack, struct got_packidx *packidx, int idx,
1669 struct got_object_id *id)
1671 const struct got_error *err = NULL;
1673 if (pack->privsep_child)
1674 return request_packed_tag(tag, pack, idx, id);
1676 err = start_pack_privsep_child(pack, packidx);
1677 if (err)
1678 return err;
1680 return request_packed_tag(tag, pack, idx, id);
1683 static const struct got_error *
1684 request_tag(struct got_tag_object **tag, struct got_repository *repo,
1685 int fd)
1687 const struct got_error *err = NULL;
1688 struct imsgbuf *ibuf;
1690 ibuf = repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TAG].ibuf;
1692 err = got_privsep_send_tag_req(ibuf, fd, NULL, -1);
1693 if (err)
1694 return err;
1696 return got_privsep_recv_tag(tag, ibuf);
1699 static const struct got_error *
1700 read_tag_privsep(struct got_tag_object **tag, int obj_fd,
1701 struct got_repository *repo)
1703 const struct got_error *err;
1704 int imsg_fds[2];
1705 pid_t pid;
1706 struct imsgbuf *ibuf;
1708 if (repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TAG].imsg_fd != -1)
1709 return request_tag(tag, repo, obj_fd);
1711 ibuf = calloc(1, sizeof(*ibuf));
1712 if (ibuf == NULL)
1713 return got_error_from_errno("calloc");
1715 if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_fds) == -1) {
1716 err = got_error_from_errno("socketpair");
1717 free(ibuf);
1718 return err;
1721 pid = fork();
1722 if (pid == -1) {
1723 err = got_error_from_errno("fork");
1724 free(ibuf);
1725 return err;
1727 else if (pid == 0) {
1728 got_privsep_exec_child(imsg_fds, GOT_PATH_PROG_READ_TAG,
1729 repo->path);
1730 /* not reached */
1733 if (close(imsg_fds[1]) == -1) {
1734 err = got_error_from_errno("close");
1735 free(ibuf);
1736 return err;
1738 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TAG].imsg_fd =
1739 imsg_fds[0];
1740 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TAG].pid = pid;
1741 imsg_init(ibuf, imsg_fds[0]);
1742 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TAG].ibuf = ibuf;
1744 return request_tag(tag, repo, obj_fd);
1747 static const struct got_error *
1748 open_tag(struct got_tag_object **tag, struct got_repository *repo,
1749 struct got_object_id *id, int check_cache)
1751 const struct got_error *err = NULL;
1752 struct got_packidx *packidx = NULL;
1753 int idx;
1754 char *path_packfile = NULL;
1755 struct got_object *obj = NULL;
1756 int obj_type = GOT_OBJ_TYPE_ANY;
1758 if (check_cache) {
1759 *tag = got_repo_get_cached_tag(repo, id);
1760 if (*tag != NULL) {
1761 (*tag)->refcnt++;
1762 return NULL;
1764 } else
1765 *tag = NULL;
1767 err = got_repo_search_packidx(&packidx, &idx, repo, id);
1768 if (err == NULL) {
1769 struct got_pack *pack = NULL;
1771 err = get_packfile_path(&path_packfile, packidx);
1772 if (err)
1773 return err;
1775 pack = got_repo_get_cached_pack(repo, path_packfile);
1776 if (pack == NULL) {
1777 err = got_repo_cache_pack(&pack, repo, path_packfile,
1778 packidx);
1779 if (err)
1780 goto done;
1783 /* Beware of "lightweight" tags: Check object type first. */
1784 err = read_packed_object_privsep(&obj, repo, pack, packidx,
1785 idx, id);
1786 if (err)
1787 goto done;
1788 obj_type = obj->type;
1789 got_object_close(obj);
1790 if (obj_type != GOT_OBJ_TYPE_TAG) {
1791 err = got_error(GOT_ERR_OBJ_TYPE);
1792 goto done;
1794 err = read_packed_tag_privsep(tag, pack, packidx, idx, id);
1795 } else if (err->code == GOT_ERR_NO_OBJ) {
1796 int fd;
1798 err = open_loose_object(&fd, id, repo);
1799 if (err)
1800 return err;
1801 err = read_object_header_privsep(&obj, repo, fd);
1802 if (err)
1803 return err;
1804 obj_type = obj->type;
1805 got_object_close(obj);
1806 if (obj_type != GOT_OBJ_TYPE_TAG)
1807 return got_error(GOT_ERR_OBJ_TYPE);
1809 err = open_loose_object(&fd, id, repo);
1810 if (err)
1811 return err;
1812 err = read_tag_privsep(tag, fd, repo);
1815 if (err == NULL) {
1816 (*tag)->refcnt++;
1817 err = got_repo_cache_tag(repo, id, *tag);
1819 done:
1820 free(path_packfile);
1821 return err;
1824 const struct got_error *
1825 got_object_open_as_tag(struct got_tag_object **tag,
1826 struct got_repository *repo, struct got_object_id *id)
1828 *tag = got_repo_get_cached_tag(repo, id);
1829 if (*tag != NULL) {
1830 (*tag)->refcnt++;
1831 return NULL;
1834 return open_tag(tag, repo, id, 0);
1837 const struct got_error *
1838 got_object_tag_open(struct got_tag_object **tag,
1839 struct got_repository *repo, struct got_object *obj)
1841 return open_tag(tag, repo, got_object_get_id(obj), 1);
1844 const char *
1845 got_object_tag_get_name(struct got_tag_object *tag)
1847 return tag->tag;
1850 int
1851 got_object_tag_get_object_type(struct got_tag_object *tag)
1853 return tag->obj_type;
1856 struct got_object_id *
1857 got_object_tag_get_object_id(struct got_tag_object *tag)
1859 return &tag->id;
1862 time_t
1863 got_object_tag_get_tagger_time(struct got_tag_object *tag)
1865 return tag->tagger_time;
1868 time_t
1869 got_object_tag_get_tagger_gmtoff(struct got_tag_object *tag)
1871 return tag->tagger_gmtoff;
1874 const char *
1875 got_object_tag_get_tagger(struct got_tag_object *tag)
1877 return tag->tagger;
1880 const char *
1881 got_object_tag_get_message(struct got_tag_object *tag)
1883 return tag->tagmsg;
1886 static struct got_tree_entry *
1887 find_entry_by_name(struct got_tree_object *tree, const char *name, size_t len)
1889 int i;
1891 /* Note that tree entries are sorted in strncmp() order. */
1892 for (i = 0; i < tree->nentries; i++) {
1893 struct got_tree_entry *te = &tree->entries[i];
1894 int cmp = strncmp(te->name, name, len);
1895 if (cmp < 0)
1896 continue;
1897 if (cmp > 0)
1898 break;
1899 if (te->name[len] == '\0')
1900 return te;
1902 return NULL;
1905 struct got_tree_entry *
1906 got_object_tree_find_entry(struct got_tree_object *tree, const char *name)
1908 return find_entry_by_name(tree, name, strlen(name));
1911 const struct got_error *
1912 got_object_id_by_path(struct got_object_id **id, struct got_repository *repo,
1913 struct got_object_id *commit_id, const char *path)
1915 const struct got_error *err = NULL;
1916 struct got_commit_object *commit = NULL;
1917 struct got_tree_object *tree = NULL;
1918 struct got_tree_entry *te = NULL;
1919 const char *seg, *s;
1920 size_t seglen;
1922 *id = NULL;
1924 err = got_object_open_as_commit(&commit, repo, commit_id);
1925 if (err)
1926 goto done;
1928 /* Handle opening of root of commit's tree. */
1929 if (got_path_is_root_dir(path)) {
1930 *id = got_object_id_dup(commit->tree_id);
1931 if (*id == NULL)
1932 err = got_error_from_errno("got_object_id_dup");
1933 goto done;
1936 err = got_object_open_as_tree(&tree, repo, commit->tree_id);
1937 if (err)
1938 goto done;
1940 s = path;
1941 while (s[0] == '/')
1942 s++;
1943 seg = s;
1944 seglen = 0;
1945 while (*s) {
1946 struct got_tree_object *next_tree;
1948 if (*s != '/') {
1949 s++;
1950 seglen++;
1951 if (*s)
1952 continue;
1955 te = find_entry_by_name(tree, seg, seglen);
1956 if (te == NULL) {
1957 err = got_error_path(path, GOT_ERR_NO_TREE_ENTRY);
1958 goto done;
1961 if (*s == '\0')
1962 break;
1964 seg = s + 1;
1965 seglen = 0;
1966 s++;
1967 if (*s) {
1968 err = got_object_open_as_tree(&next_tree, repo,
1969 &te->id);
1970 te = NULL;
1971 if (err)
1972 goto done;
1973 got_object_tree_close(tree);
1974 tree = next_tree;
1978 if (te) {
1979 *id = got_object_id_dup(&te->id);
1980 if (*id == NULL)
1981 return got_error_from_errno("got_object_id_dup");
1982 } else
1983 err = got_error_path(path, GOT_ERR_NO_TREE_ENTRY);
1984 done:
1985 if (commit)
1986 got_object_commit_close(commit);
1987 if (tree)
1988 got_object_tree_close(tree);
1989 return err;
1993 * Normalize file mode bits to avoid false positive tree entry differences
1994 * in case tree entries have unexpected mode bits set.
1996 static mode_t
1997 normalize_mode_for_comparison(mode_t mode)
2000 * For directories, the only relevant bit is the IFDIR bit.
2001 * This allows us to detect paths changing from a directory
2002 * to a file and vice versa.
2004 if (S_ISDIR(mode))
2005 return mode & S_IFDIR;
2008 * For symlinks, the only relevant bit is the IFLNK bit.
2009 * This allows us to detect paths changing from a symlinks
2010 * to a file or directory and vice versa.
2012 if (S_ISLNK(mode))
2013 return mode & S_IFLNK;
2015 /* For files, the only change we care about is the executable bit. */
2016 return mode & S_IXUSR;
2019 const struct got_error *
2020 got_object_tree_path_changed(int *changed,
2021 struct got_tree_object *tree01, struct got_tree_object *tree02,
2022 const char *path, struct got_repository *repo)
2024 const struct got_error *err = NULL;
2025 struct got_tree_object *tree1 = NULL, *tree2 = NULL;
2026 struct got_tree_entry *te1 = NULL, *te2 = NULL;
2027 const char *seg, *s;
2028 size_t seglen;
2030 *changed = 0;
2032 /* We not do support comparing the root path. */
2033 if (got_path_is_root_dir(path))
2034 return got_error_path(path, GOT_ERR_BAD_PATH);
2036 tree1 = tree01;
2037 tree2 = tree02;
2038 s = path;
2039 while (*s == '/')
2040 s++;
2041 seg = s;
2042 seglen = 0;
2043 while (*s) {
2044 struct got_tree_object *next_tree1, *next_tree2;
2045 mode_t mode1, mode2;
2047 if (*s != '/') {
2048 s++;
2049 seglen++;
2050 if (*s)
2051 continue;
2054 te1 = find_entry_by_name(tree1, seg, seglen);
2055 if (te1 == NULL) {
2056 err = got_error(GOT_ERR_NO_OBJ);
2057 goto done;
2060 if (tree2)
2061 te2 = find_entry_by_name(tree2, seg, seglen);
2063 if (te2) {
2064 mode1 = normalize_mode_for_comparison(te1->mode);
2065 mode2 = normalize_mode_for_comparison(te2->mode);
2066 if (mode1 != mode2) {
2067 *changed = 1;
2068 goto done;
2071 if (got_object_id_cmp(&te1->id, &te2->id) == 0) {
2072 *changed = 0;
2073 goto done;
2077 if (*s == '\0') { /* final path element */
2078 *changed = 1;
2079 goto done;
2082 seg = s + 1;
2083 s++;
2084 seglen = 0;
2085 if (*s) {
2086 err = got_object_open_as_tree(&next_tree1, repo,
2087 &te1->id);
2088 te1 = NULL;
2089 if (err)
2090 goto done;
2091 if (tree1 != tree01)
2092 got_object_tree_close(tree1);
2093 tree1 = next_tree1;
2095 if (te2) {
2096 err = got_object_open_as_tree(&next_tree2, repo,
2097 &te2->id);
2098 te2 = NULL;
2099 if (err)
2100 goto done;
2101 if (tree2 != tree02)
2102 got_object_tree_close(tree2);
2103 tree2 = next_tree2;
2104 } else if (tree2) {
2105 if (tree2 != tree02)
2106 got_object_tree_close(tree2);
2107 tree2 = NULL;
2111 done:
2112 if (tree1 && tree1 != tree01)
2113 got_object_tree_close(tree1);
2114 if (tree2 && tree2 != tree02)
2115 got_object_tree_close(tree2);
2116 return err;
2119 const struct got_error *
2120 got_object_tree_entry_dup(struct got_tree_entry **new_te,
2121 struct got_tree_entry *te)
2123 const struct got_error *err = NULL;
2125 *new_te = calloc(1, sizeof(**new_te));
2126 if (*new_te == NULL)
2127 return got_error_from_errno("calloc");
2129 (*new_te)->mode = te->mode;
2130 memcpy((*new_te)->name, te->name, sizeof((*new_te)->name));
2131 memcpy(&(*new_te)->id, &te->id, sizeof((*new_te)->id));
2132 return err;
2135 int
2136 got_object_tree_entry_is_submodule(struct got_tree_entry *te)
2138 return (te->mode & S_IFMT) == (S_IFDIR | S_IFLNK);
2141 int
2142 got_object_tree_entry_is_symlink(struct got_tree_entry *te)
2144 /* S_IFDIR check avoids confusing symlinks with submodules. */
2145 return ((te->mode & (S_IFDIR | S_IFLNK)) == S_IFLNK);
2148 static const struct got_error *
2149 resolve_symlink(char **link_target, const char *path,
2150 struct got_object_id *commit_id, struct got_repository *repo)
2152 const struct got_error *err = NULL;
2153 char buf[PATH_MAX];
2154 char *name, *parent_path = NULL;
2155 struct got_object_id *tree_obj_id = NULL;
2156 struct got_tree_object *tree = NULL;
2157 struct got_tree_entry *te = NULL;
2159 *link_target = NULL;
2161 if (strlcpy(buf, path, sizeof(buf)) >= sizeof(buf))
2162 return got_error(GOT_ERR_NO_SPACE);
2164 name = basename(buf);
2165 if (name == NULL)
2166 return got_error_from_errno2("basename", path);
2168 err = got_path_dirname(&parent_path, path);
2169 if (err)
2170 return err;
2172 err = got_object_id_by_path(&tree_obj_id, repo, commit_id,
2173 parent_path);
2174 if (err) {
2175 if (err->code == GOT_ERR_NO_TREE_ENTRY) {
2176 /* Display the complete path in error message. */
2177 err = got_error_path(path, err->code);
2179 goto done;
2182 err = got_object_open_as_tree(&tree, repo, tree_obj_id);
2183 if (err)
2184 goto done;
2186 te = got_object_tree_find_entry(tree, name);
2187 if (te == NULL) {
2188 err = got_error_path(path, GOT_ERR_NO_TREE_ENTRY);
2189 goto done;
2192 if (got_object_tree_entry_is_symlink(te)) {
2193 err = got_tree_entry_get_symlink_target(link_target, te, repo);
2194 if (err)
2195 goto done;
2196 if (!got_path_is_absolute(*link_target)) {
2197 char *abspath;
2198 if (asprintf(&abspath, "%s/%s", parent_path,
2199 *link_target) == -1) {
2200 err = got_error_from_errno("asprintf");
2201 goto done;
2203 free(*link_target);
2204 *link_target = malloc(PATH_MAX);
2205 if (*link_target == NULL) {
2206 err = got_error_from_errno("malloc");
2207 goto done;
2209 err = got_canonpath(abspath, *link_target, PATH_MAX);
2210 free(abspath);
2211 if (err)
2212 goto done;
2215 done:
2216 free(tree_obj_id);
2217 if (tree)
2218 got_object_tree_close(tree);
2219 if (err) {
2220 free(*link_target);
2221 *link_target = NULL;
2223 return err;
2226 const struct got_error *
2227 got_object_resolve_symlinks(char **link_target, const char *path,
2228 struct got_object_id *commit_id, struct got_repository *repo)
2230 const struct got_error *err = NULL;
2231 char *next_target = NULL;
2232 int max_recursion = 40; /* matches Git */
2234 *link_target = NULL;
2236 do {
2237 err = resolve_symlink(&next_target,
2238 *link_target ? *link_target : path, commit_id, repo);
2239 if (err)
2240 break;
2241 if (next_target) {
2242 free(*link_target);
2243 if (--max_recursion == 0) {
2244 err = got_error_path(path, GOT_ERR_RECURSION);
2245 *link_target = NULL;
2246 break;
2248 *link_target = next_target;
2250 } while (next_target);
2252 return err;
2255 const struct got_error *
2256 got_traverse_packed_commits(struct got_object_id_queue *traversed_commits,
2257 struct got_object_id *commit_id, const char *path,
2258 struct got_repository *repo)
2260 const struct got_error *err = NULL;
2261 struct got_pack *pack = NULL;
2262 struct got_packidx *packidx = NULL;
2263 char *path_packfile = NULL;
2264 struct got_commit_object *changed_commit = NULL;
2265 struct got_object_id *changed_commit_id = NULL;
2266 int idx;
2268 err = got_repo_search_packidx(&packidx, &idx, repo, commit_id);
2269 if (err) {
2270 if (err->code != GOT_ERR_NO_OBJ)
2271 return err;
2272 return NULL;
2275 err = get_packfile_path(&path_packfile, packidx);
2276 if (err)
2277 return err;
2279 pack = got_repo_get_cached_pack(repo, path_packfile);
2280 if (pack == NULL) {
2281 err = got_repo_cache_pack(&pack, repo, path_packfile, packidx);
2282 if (err)
2283 goto done;
2286 if (pack->privsep_child == NULL) {
2287 err = start_pack_privsep_child(pack, packidx);
2288 if (err)
2289 goto done;
2292 err = got_privsep_send_commit_traversal_request(
2293 pack->privsep_child->ibuf, commit_id, idx, path);
2294 if (err)
2295 goto done;
2297 err = got_privsep_recv_traversed_commits(&changed_commit,
2298 &changed_commit_id, traversed_commits, pack->privsep_child->ibuf);
2299 if (err)
2300 goto done;
2302 if (changed_commit) {
2304 * Cache the commit in which the path was changed.
2305 * This commit might be opened again soon.
2307 changed_commit->refcnt++;
2308 err = got_repo_cache_commit(repo, changed_commit_id,
2309 changed_commit);
2310 got_object_commit_close(changed_commit);
2312 done:
2313 free(path_packfile);
2314 free(changed_commit_id);
2315 return err;