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 close(obj_fd);
490 return err;
493 return request_object(obj, repo, obj_fd);
496 static const struct got_error *
497 read_object_raw_privsep(uint8_t **outbuf, off_t *size, size_t *hdrlen,
498 int outfd, struct got_repository *repo, int obj_fd)
500 const struct got_error *err;
502 if (repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_OBJECT].imsg_fd != -1)
503 return request_raw_object(outbuf, size, hdrlen, outfd, repo,
504 obj_fd);
506 err = start_read_object_child(repo);
507 if (err)
508 return err;
510 return request_raw_object(outbuf, size, hdrlen, outfd, repo, obj_fd);
513 const struct got_error *
514 got_object_open(struct got_object **obj, struct got_repository *repo,
515 struct got_object_id *id)
517 const struct got_error *err = NULL;
518 char *path;
519 int fd;
521 *obj = got_repo_get_cached_object(repo, id);
522 if (*obj != NULL) {
523 (*obj)->refcnt++;
524 return NULL;
527 err = open_packed_object(obj, id, repo);
528 if (err && err->code != GOT_ERR_NO_OBJ)
529 return err;
530 if (*obj) {
531 (*obj)->refcnt++;
532 return got_repo_cache_object(repo, id, *obj);
535 err = got_object_get_path(&path, id, repo);
536 if (err)
537 return err;
539 fd = open(path, O_RDONLY | O_NOFOLLOW);
540 if (fd == -1) {
541 if (errno == ENOENT)
542 err = got_error_no_obj(id);
543 else
544 err = got_error_from_errno2("open", path);
545 goto done;
546 } else {
547 err = read_object_header_privsep(obj, repo, fd);
548 if (err)
549 goto done;
550 memcpy((*obj)->id.sha1, id->sha1, SHA1_DIGEST_LENGTH);
553 (*obj)->refcnt++;
554 err = got_repo_cache_object(repo, id, *obj);
555 done:
556 free(path);
557 return err;
560 const struct got_error *
561 got_object_raw_open(struct got_raw_object **obj, struct got_repository *repo,
562 struct got_object_id *id, size_t blocksize)
564 const struct got_error *err = NULL;
565 struct got_packidx *packidx = NULL;
566 int idx;
567 uint8_t *outbuf = NULL;
568 int outfd = -1;
569 off_t size = 0;
570 size_t hdrlen = 0;
571 char *path_packfile = NULL;
573 *obj = NULL;
575 outfd = got_opentempfd();
576 if (outfd == -1)
577 return got_error_from_errno("got_opentempfd");
579 err = got_repo_search_packidx(&packidx, &idx, repo, id);
580 if (err == NULL) {
581 struct got_pack *pack = NULL;
583 err = get_packfile_path(&path_packfile, packidx);
584 if (err)
585 goto done;
587 pack = got_repo_get_cached_pack(repo, path_packfile);
588 if (pack == NULL) {
589 err = got_repo_cache_pack(&pack, repo, path_packfile,
590 packidx);
591 if (err)
592 goto done;
594 err = read_packed_object_raw_privsep(&outbuf, &size, &hdrlen,
595 outfd, pack, packidx, idx, id);
596 } else if (err->code == GOT_ERR_NO_OBJ) {
597 int fd;
599 err = open_loose_object(&fd, id, repo);
600 if (err)
601 goto done;
602 err = read_object_raw_privsep(&outbuf, &size, &hdrlen, outfd,
603 repo, fd);
606 if (hdrlen > size) {
607 err = got_error(GOT_ERR_BAD_OBJ_HDR);
608 goto done;
611 *obj = calloc(1, sizeof(**obj));
612 if (*obj == NULL) {
613 err = got_error_from_errno("calloc");
614 goto done;
617 (*obj)->read_buf = malloc(blocksize);
618 if ((*obj)->read_buf == NULL) {
619 err = got_error_from_errno("malloc");
620 goto done;
623 if (outbuf) {
624 if (close(outfd) == -1) {
625 err = got_error_from_errno("close");
626 goto done;
628 outfd = -1;
629 (*obj)->f = fmemopen(outbuf, hdrlen + size, "r");
630 if ((*obj)->f == NULL) {
631 err = got_error_from_errno("fdopen");
632 goto done;
634 (*obj)->data = outbuf;
635 } else {
636 struct stat sb;
637 if (fstat(outfd, &sb) == -1) {
638 err = got_error_from_errno("fstat");
639 goto done;
642 if (sb.st_size != size) {
643 err = got_error(GOT_ERR_PRIVSEP_LEN);
644 goto done;
647 (*obj)->f = fdopen(outfd, "r");
648 if ((*obj)->f == NULL) {
649 err = got_error_from_errno("fdopen");
650 goto done;
652 outfd = -1;
653 (*obj)->data = NULL;
655 (*obj)->hdrlen = hdrlen;
656 (*obj)->size = size;
657 (*obj)->blocksize = blocksize;
658 done:
659 free(path_packfile);
660 if (err) {
661 if (*obj) {
662 got_object_raw_close(*obj);
663 *obj = NULL;
665 if (outfd != -1)
666 close(outfd);
667 free(outbuf);
669 return err;
672 void
673 got_object_raw_rewind(struct got_raw_object *obj)
675 if (obj->f)
676 rewind(obj->f);
679 size_t
680 got_object_raw_get_hdrlen(struct got_raw_object *obj)
682 return obj->hdrlen;
685 const uint8_t *
686 got_object_raw_get_read_buf(struct got_raw_object *obj)
688 return obj->read_buf;
691 const struct got_error *
692 got_object_raw_read_block(size_t *outlenp, struct got_raw_object *obj)
694 size_t n;
696 n = fread(obj->read_buf, 1, obj->blocksize, obj->f);
697 if (n == 0 && ferror(obj->f))
698 return got_ferror(obj->f, GOT_ERR_IO);
699 *outlenp = n;
700 return NULL;
703 const struct got_error *
704 got_object_raw_close(struct got_raw_object *obj)
706 const struct got_error *err = NULL;
708 free(obj->read_buf);
709 if (obj->f != NULL && fclose(obj->f) == EOF && err == NULL)
710 err = got_error_from_errno("fclose");
711 free(obj->data);
712 free(obj);
713 return err;
716 const struct got_error *
717 got_object_open_by_id_str(struct got_object **obj, struct got_repository *repo,
718 const char *id_str)
720 struct got_object_id id;
722 if (!got_parse_sha1_digest(id.sha1, id_str))
723 return got_error_path(id_str, GOT_ERR_BAD_OBJ_ID_STR);
725 return got_object_open(obj, repo, &id);
728 const struct got_error *
729 got_object_resolve_id_str(struct got_object_id **id,
730 struct got_repository *repo, const char *id_str)
732 const struct got_error *err = NULL;
733 struct got_object *obj;
735 err = got_object_open_by_id_str(&obj, repo, id_str);
736 if (err)
737 return err;
739 *id = got_object_id_dup(got_object_get_id(obj));
740 got_object_close(obj);
741 if (*id == NULL)
742 return got_error_from_errno("got_object_id_dup");
744 return NULL;
747 static const struct got_error *
748 request_packed_commit(struct got_commit_object **commit, struct got_pack *pack,
749 int pack_idx, struct got_object_id *id)
751 const struct got_error *err = NULL;
753 err = got_privsep_send_commit_req(pack->privsep_child->ibuf, -1, id,
754 pack_idx);
755 if (err)
756 return err;
758 err = got_privsep_recv_commit(commit, pack->privsep_child->ibuf);
759 if (err)
760 return err;
762 (*commit)->flags |= GOT_COMMIT_FLAG_PACKED;
763 return NULL;
766 static const struct got_error *
767 read_packed_commit_privsep(struct got_commit_object **commit,
768 struct got_pack *pack, struct got_packidx *packidx, int idx,
769 struct got_object_id *id)
771 const struct got_error *err = NULL;
773 if (pack->privsep_child)
774 return request_packed_commit(commit, pack, idx, id);
776 err = start_pack_privsep_child(pack, packidx);
777 if (err)
778 return err;
780 return request_packed_commit(commit, pack, idx, id);
783 static const struct got_error *
784 request_commit(struct got_commit_object **commit, struct got_repository *repo,
785 int fd)
787 const struct got_error *err = NULL;
788 struct imsgbuf *ibuf;
790 ibuf = repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_COMMIT].ibuf;
792 err = got_privsep_send_commit_req(ibuf, fd, NULL, -1);
793 if (err)
794 return err;
796 return got_privsep_recv_commit(commit, ibuf);
799 static const struct got_error *
800 read_commit_privsep(struct got_commit_object **commit, int obj_fd,
801 struct got_repository *repo)
803 const struct got_error *err;
804 int imsg_fds[2];
805 pid_t pid;
806 struct imsgbuf *ibuf;
808 if (repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_COMMIT].imsg_fd != -1)
809 return request_commit(commit, repo, obj_fd);
811 ibuf = calloc(1, sizeof(*ibuf));
812 if (ibuf == NULL)
813 return got_error_from_errno("calloc");
815 if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_fds) == -1) {
816 err = got_error_from_errno("socketpair");
817 free(ibuf);
818 return err;
821 pid = fork();
822 if (pid == -1) {
823 err = got_error_from_errno("fork");
824 free(ibuf);
825 return err;
827 else if (pid == 0) {
828 got_privsep_exec_child(imsg_fds, GOT_PATH_PROG_READ_COMMIT,
829 repo->path);
830 /* not reached */
833 if (close(imsg_fds[1]) == -1) {
834 err = got_error_from_errno("close");
835 free(ibuf);
836 return err;
838 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_COMMIT].imsg_fd =
839 imsg_fds[0];
840 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_COMMIT].pid = pid;
841 imsg_init(ibuf, imsg_fds[0]);
842 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_COMMIT].ibuf = ibuf;
844 return request_commit(commit, repo, obj_fd);
848 static const struct got_error *
849 open_commit(struct got_commit_object **commit,
850 struct got_repository *repo, struct got_object_id *id, int check_cache)
852 const struct got_error *err = NULL;
853 struct got_packidx *packidx = NULL;
854 int idx;
855 char *path_packfile = NULL;
857 if (check_cache) {
858 *commit = got_repo_get_cached_commit(repo, id);
859 if (*commit != NULL) {
860 (*commit)->refcnt++;
861 return NULL;
863 } else
864 *commit = NULL;
866 err = got_repo_search_packidx(&packidx, &idx, repo, id);
867 if (err == NULL) {
868 struct got_pack *pack = NULL;
870 err = get_packfile_path(&path_packfile, packidx);
871 if (err)
872 return err;
874 pack = got_repo_get_cached_pack(repo, path_packfile);
875 if (pack == NULL) {
876 err = got_repo_cache_pack(&pack, repo, path_packfile,
877 packidx);
878 if (err)
879 goto done;
881 err = read_packed_commit_privsep(commit, pack,
882 packidx, idx, id);
883 } else if (err->code == GOT_ERR_NO_OBJ) {
884 int fd;
886 err = open_loose_object(&fd, id, repo);
887 if (err)
888 return err;
889 err = read_commit_privsep(commit, fd, repo);
892 if (err == NULL) {
893 (*commit)->refcnt++;
894 err = got_repo_cache_commit(repo, id, *commit);
896 done:
897 free(path_packfile);
898 return err;
901 const struct got_error *
902 got_object_open_as_commit(struct got_commit_object **commit,
903 struct got_repository *repo, struct got_object_id *id)
905 *commit = got_repo_get_cached_commit(repo, id);
906 if (*commit != NULL) {
907 (*commit)->refcnt++;
908 return NULL;
911 return open_commit(commit, repo, id, 0);
914 const struct got_error *
915 got_object_commit_open(struct got_commit_object **commit,
916 struct got_repository *repo, struct got_object *obj)
918 return open_commit(commit, repo, got_object_get_id(obj), 1);
921 const struct got_error *
922 got_object_qid_alloc(struct got_object_qid **qid, struct got_object_id *id)
924 const struct got_error *err = NULL;
926 *qid = calloc(1, sizeof(**qid));
927 if (*qid == NULL)
928 return got_error_from_errno("calloc");
930 (*qid)->id = got_object_id_dup(id);
931 if ((*qid)->id == NULL) {
932 err = got_error_from_errno("got_object_id_dup");
933 got_object_qid_free(*qid);
934 *qid = NULL;
935 return err;
938 return NULL;
941 static const struct got_error *
942 request_packed_tree(struct got_tree_object **tree, struct got_pack *pack,
943 int pack_idx, struct got_object_id *id)
945 const struct got_error *err = NULL;
947 err = got_privsep_send_tree_req(pack->privsep_child->ibuf, -1, id,
948 pack_idx);
949 if (err)
950 return err;
952 return got_privsep_recv_tree(tree, pack->privsep_child->ibuf);
955 static const struct got_error *
956 read_packed_tree_privsep(struct got_tree_object **tree,
957 struct got_pack *pack, struct got_packidx *packidx, int idx,
958 struct got_object_id *id)
960 const struct got_error *err = NULL;
962 if (pack->privsep_child)
963 return request_packed_tree(tree, pack, idx, id);
965 err = start_pack_privsep_child(pack, packidx);
966 if (err)
967 return err;
969 return request_packed_tree(tree, pack, idx, id);
972 static const struct got_error *
973 request_tree(struct got_tree_object **tree, struct got_repository *repo,
974 int fd)
976 const struct got_error *err = NULL;
977 struct imsgbuf *ibuf;
979 ibuf = repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TREE].ibuf;
981 err = got_privsep_send_tree_req(ibuf, fd, NULL, -1);
982 if (err)
983 return err;
985 return got_privsep_recv_tree(tree, ibuf);
988 const struct got_error *
989 read_tree_privsep(struct got_tree_object **tree, int obj_fd,
990 struct got_repository *repo)
992 const struct got_error *err;
993 int imsg_fds[2];
994 pid_t pid;
995 struct imsgbuf *ibuf;
997 if (repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TREE].imsg_fd != -1)
998 return request_tree(tree, repo, obj_fd);
1000 ibuf = calloc(1, sizeof(*ibuf));
1001 if (ibuf == NULL)
1002 return got_error_from_errno("calloc");
1004 if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_fds) == -1) {
1005 err = got_error_from_errno("socketpair");
1006 free(ibuf);
1007 return err;
1010 pid = fork();
1011 if (pid == -1) {
1012 err = got_error_from_errno("fork");
1013 free(ibuf);
1014 return err;
1016 else if (pid == 0) {
1017 got_privsep_exec_child(imsg_fds, GOT_PATH_PROG_READ_TREE,
1018 repo->path);
1019 /* not reached */
1022 if (close(imsg_fds[1]) == -1) {
1023 err = got_error_from_errno("close");
1024 free(ibuf);
1025 return err;
1027 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TREE].imsg_fd =
1028 imsg_fds[0];
1029 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TREE].pid = pid;
1030 imsg_init(ibuf, imsg_fds[0]);
1031 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TREE].ibuf = ibuf;
1034 return request_tree(tree, repo, obj_fd);
1037 static const struct got_error *
1038 open_tree(struct got_tree_object **tree, struct got_repository *repo,
1039 struct got_object_id *id, int check_cache)
1041 const struct got_error *err = NULL;
1042 struct got_packidx *packidx = NULL;
1043 int idx;
1044 char *path_packfile = NULL;
1046 if (check_cache) {
1047 *tree = got_repo_get_cached_tree(repo, id);
1048 if (*tree != NULL) {
1049 (*tree)->refcnt++;
1050 return NULL;
1052 } else
1053 *tree = NULL;
1055 err = got_repo_search_packidx(&packidx, &idx, repo, id);
1056 if (err == NULL) {
1057 struct got_pack *pack = NULL;
1059 err = get_packfile_path(&path_packfile, packidx);
1060 if (err)
1061 return err;
1063 pack = got_repo_get_cached_pack(repo, path_packfile);
1064 if (pack == NULL) {
1065 err = got_repo_cache_pack(&pack, repo, path_packfile,
1066 packidx);
1067 if (err)
1068 goto done;
1070 err = read_packed_tree_privsep(tree, pack,
1071 packidx, idx, id);
1072 } else if (err->code == GOT_ERR_NO_OBJ) {
1073 int fd;
1075 err = open_loose_object(&fd, id, repo);
1076 if (err)
1077 return err;
1078 err = read_tree_privsep(tree, fd, repo);
1081 if (err == NULL) {
1082 (*tree)->refcnt++;
1083 err = got_repo_cache_tree(repo, id, *tree);
1085 done:
1086 free(path_packfile);
1087 return err;
1090 const struct got_error *
1091 got_object_open_as_tree(struct got_tree_object **tree,
1092 struct got_repository *repo, struct got_object_id *id)
1094 *tree = got_repo_get_cached_tree(repo, id);
1095 if (*tree != NULL) {
1096 (*tree)->refcnt++;
1097 return NULL;
1100 return open_tree(tree, repo, id, 0);
1103 const struct got_error *
1104 got_object_tree_open(struct got_tree_object **tree,
1105 struct got_repository *repo, struct got_object *obj)
1107 return open_tree(tree, repo, got_object_get_id(obj), 1);
1110 int
1111 got_object_tree_get_nentries(struct got_tree_object *tree)
1113 return tree->nentries;
1116 struct got_tree_entry *
1117 got_object_tree_get_first_entry(struct got_tree_object *tree)
1119 return got_object_tree_get_entry(tree, 0);
1122 struct got_tree_entry *
1123 got_object_tree_get_last_entry(struct got_tree_object *tree)
1125 return got_object_tree_get_entry(tree, tree->nentries - 1);
1128 struct got_tree_entry *
1129 got_object_tree_get_entry(struct got_tree_object *tree, int i)
1131 if (i < 0 || i >= tree->nentries)
1132 return NULL;
1133 return &tree->entries[i];
1136 mode_t
1137 got_tree_entry_get_mode(struct got_tree_entry *te)
1139 return te->mode;
1142 const char *
1143 got_tree_entry_get_name(struct got_tree_entry *te)
1145 return &te->name[0];
1148 struct got_object_id *
1149 got_tree_entry_get_id(struct got_tree_entry *te)
1151 return &te->id;
1154 const struct got_error *
1155 got_object_blob_read_to_str(char **s, struct got_blob_object *blob)
1157 const struct got_error *err = NULL;
1158 size_t len, totlen, hdrlen, offset;
1160 *s = NULL;
1162 hdrlen = got_object_blob_get_hdrlen(blob);
1163 totlen = 0;
1164 offset = 0;
1165 do {
1166 char *p;
1168 err = got_object_blob_read_block(&len, blob);
1169 if (err)
1170 return err;
1172 if (len == 0)
1173 break;
1175 totlen += len - hdrlen;
1176 p = realloc(*s, totlen + 1);
1177 if (p == NULL) {
1178 err = got_error_from_errno("realloc");
1179 free(*s);
1180 *s = NULL;
1181 return err;
1183 *s = p;
1184 /* Skip blob object header first time around. */
1185 memcpy(*s + offset,
1186 got_object_blob_get_read_buf(blob) + hdrlen, len - hdrlen);
1187 hdrlen = 0;
1188 offset = totlen;
1189 } while (len > 0);
1191 (*s)[totlen] = '\0';
1192 return NULL;
1195 const struct got_error *
1196 got_tree_entry_get_symlink_target(char **link_target, struct got_tree_entry *te,
1197 struct got_repository *repo)
1199 const struct got_error *err = NULL;
1200 struct got_blob_object *blob = NULL;
1202 *link_target = NULL;
1204 if (!got_object_tree_entry_is_symlink(te))
1205 return got_error(GOT_ERR_TREE_ENTRY_TYPE);
1207 err = got_object_open_as_blob(&blob, repo,
1208 got_tree_entry_get_id(te), PATH_MAX);
1209 if (err)
1210 return err;
1212 err = got_object_blob_read_to_str(link_target, blob);
1213 got_object_blob_close(blob);
1214 if (err) {
1215 free(*link_target);
1216 *link_target = NULL;
1218 return err;
1221 int
1222 got_tree_entry_get_index(struct got_tree_entry *te)
1224 return te->idx;
1227 struct got_tree_entry *
1228 got_tree_entry_get_next(struct got_tree_object *tree,
1229 struct got_tree_entry *te)
1231 return got_object_tree_get_entry(tree, te->idx + 1);
1234 struct got_tree_entry *
1235 got_tree_entry_get_prev(struct got_tree_object *tree,
1236 struct got_tree_entry *te)
1238 return got_object_tree_get_entry(tree, te->idx - 1);
1241 static const struct got_error *
1242 request_packed_blob(uint8_t **outbuf, size_t *size, size_t *hdrlen, int outfd,
1243 struct got_pack *pack, struct got_packidx *packidx, int idx,
1244 struct got_object_id *id)
1246 const struct got_error *err = NULL;
1247 int outfd_child;
1248 int basefd, accumfd; /* temporary files for delta application */
1250 basefd = got_opentempfd();
1251 if (basefd == -1)
1252 return got_error_from_errno("got_opentempfd");
1253 accumfd = got_opentempfd();
1254 if (accumfd == -1)
1255 return got_error_from_errno("got_opentempfd");
1257 outfd_child = dup(outfd);
1258 if (outfd_child == -1)
1259 return got_error_from_errno("dup");
1261 err = got_privsep_send_blob_req(pack->privsep_child->ibuf, -1, id, idx);
1262 if (err)
1263 return err;
1265 err = got_privsep_send_blob_outfd(pack->privsep_child->ibuf,
1266 outfd_child);
1267 if (err) {
1268 close(basefd);
1269 close(accumfd);
1270 return err;
1273 err = got_privsep_send_tmpfd(pack->privsep_child->ibuf,
1274 basefd);
1275 if (err) {
1276 close(accumfd);
1277 return err;
1280 err = got_privsep_send_tmpfd(pack->privsep_child->ibuf,
1281 accumfd);
1282 if (err)
1283 return err;
1285 err = got_privsep_recv_blob(outbuf, size, hdrlen,
1286 pack->privsep_child->ibuf);
1287 if (err)
1288 return err;
1290 if (lseek(outfd, SEEK_SET, 0) == -1)
1291 err = got_error_from_errno("lseek");
1293 return err;
1296 static const struct got_error *
1297 read_packed_blob_privsep(uint8_t **outbuf, size_t *size, size_t *hdrlen,
1298 int outfd, struct got_pack *pack, struct got_packidx *packidx, int idx,
1299 struct got_object_id *id)
1301 const struct got_error *err = NULL;
1303 if (pack->privsep_child == NULL) {
1304 err = start_pack_privsep_child(pack, packidx);
1305 if (err)
1306 return err;
1309 return request_packed_blob(outbuf, size, hdrlen, outfd, pack, packidx,
1310 idx, id);
1313 static const struct got_error *
1314 request_blob(uint8_t **outbuf, size_t *size, size_t *hdrlen, int outfd,
1315 int infd, struct imsgbuf *ibuf)
1317 const struct got_error *err = NULL;
1318 int outfd_child;
1320 outfd_child = dup(outfd);
1321 if (outfd_child == -1)
1322 return got_error_from_errno("dup");
1324 err = got_privsep_send_blob_req(ibuf, infd, NULL, -1);
1325 if (err)
1326 return err;
1328 err = got_privsep_send_blob_outfd(ibuf, outfd_child);
1329 if (err)
1330 return err;
1332 err = got_privsep_recv_blob(outbuf, size, hdrlen, ibuf);
1333 if (err)
1334 return err;
1336 if (lseek(outfd, SEEK_SET, 0) == -1)
1337 return got_error_from_errno("lseek");
1339 return err;
1342 static const struct got_error *
1343 read_blob_privsep(uint8_t **outbuf, size_t *size, size_t *hdrlen,
1344 int outfd, int infd, struct got_repository *repo)
1346 const struct got_error *err;
1347 int imsg_fds[2];
1348 pid_t pid;
1349 struct imsgbuf *ibuf;
1351 if (repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_BLOB].imsg_fd != -1) {
1352 ibuf = repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_BLOB].ibuf;
1353 return request_blob(outbuf, size, hdrlen, outfd, infd, ibuf);
1356 ibuf = calloc(1, sizeof(*ibuf));
1357 if (ibuf == NULL)
1358 return got_error_from_errno("calloc");
1360 if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_fds) == -1) {
1361 err = got_error_from_errno("socketpair");
1362 free(ibuf);
1363 return err;
1366 pid = fork();
1367 if (pid == -1) {
1368 err = got_error_from_errno("fork");
1369 free(ibuf);
1370 return err;
1372 else if (pid == 0) {
1373 got_privsep_exec_child(imsg_fds, GOT_PATH_PROG_READ_BLOB,
1374 repo->path);
1375 /* not reached */
1378 if (close(imsg_fds[1]) == -1) {
1379 err = got_error_from_errno("close");
1380 free(ibuf);
1381 return err;
1383 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_BLOB].imsg_fd =
1384 imsg_fds[0];
1385 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_BLOB].pid = pid;
1386 imsg_init(ibuf, imsg_fds[0]);
1387 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_BLOB].ibuf = ibuf;
1389 return request_blob(outbuf, size, hdrlen, outfd, infd, ibuf);
1392 static const struct got_error *
1393 open_blob(struct got_blob_object **blob, struct got_repository *repo,
1394 struct got_object_id *id, size_t blocksize)
1396 const struct got_error *err = NULL;
1397 struct got_packidx *packidx = NULL;
1398 int idx;
1399 char *path_packfile = NULL;
1400 uint8_t *outbuf;
1401 int outfd;
1402 size_t size, hdrlen;
1403 struct stat sb;
1405 *blob = calloc(1, sizeof(**blob));
1406 if (*blob == NULL)
1407 return got_error_from_errno("calloc");
1409 outfd = got_opentempfd();
1410 if (outfd == -1)
1411 return got_error_from_errno("got_opentempfd");
1413 (*blob)->read_buf = malloc(blocksize);
1414 if ((*blob)->read_buf == NULL) {
1415 err = got_error_from_errno("malloc");
1416 goto done;
1419 err = got_repo_search_packidx(&packidx, &idx, repo, id);
1420 if (err == NULL) {
1421 struct got_pack *pack = NULL;
1423 err = get_packfile_path(&path_packfile, packidx);
1424 if (err)
1425 goto done;
1427 pack = got_repo_get_cached_pack(repo, path_packfile);
1428 if (pack == NULL) {
1429 err = got_repo_cache_pack(&pack, repo, path_packfile,
1430 packidx);
1431 if (err)
1432 goto done;
1434 err = read_packed_blob_privsep(&outbuf, &size, &hdrlen, outfd,
1435 pack, packidx, idx, id);
1436 } else if (err->code == GOT_ERR_NO_OBJ) {
1437 int infd;
1439 err = open_loose_object(&infd, id, repo);
1440 if (err)
1441 goto done;
1442 err = read_blob_privsep(&outbuf, &size, &hdrlen, outfd, infd,
1443 repo);
1445 if (err)
1446 goto done;
1448 if (hdrlen > size) {
1449 err = got_error(GOT_ERR_BAD_OBJ_HDR);
1450 goto done;
1453 if (outbuf) {
1454 if (close(outfd) == -1 && err == NULL)
1455 err = got_error_from_errno("close");
1456 outfd = -1;
1457 (*blob)->f = fmemopen(outbuf, size, "rb");
1458 if ((*blob)->f == NULL) {
1459 err = got_error_from_errno("fmemopen");
1460 free(outbuf);
1461 goto done;
1463 (*blob)->data = outbuf;
1464 } else {
1465 if (fstat(outfd, &sb) == -1) {
1466 err = got_error_from_errno("fstat");
1467 goto done;
1470 if (sb.st_size != size) {
1471 err = got_error(GOT_ERR_PRIVSEP_LEN);
1472 goto done;
1475 (*blob)->f = fdopen(outfd, "rb");
1476 if ((*blob)->f == NULL) {
1477 err = got_error_from_errno("fdopen");
1478 close(outfd);
1479 outfd = -1;
1480 goto done;
1484 (*blob)->hdrlen = hdrlen;
1485 (*blob)->blocksize = blocksize;
1486 memcpy(&(*blob)->id.sha1, id->sha1, SHA1_DIGEST_LENGTH);
1488 done:
1489 free(path_packfile);
1490 if (err) {
1491 if (*blob) {
1492 got_object_blob_close(*blob);
1493 *blob = NULL;
1494 } else if (outfd != -1)
1495 close(outfd);
1497 return err;
1500 const struct got_error *
1501 got_object_open_as_blob(struct got_blob_object **blob,
1502 struct got_repository *repo, struct got_object_id *id,
1503 size_t blocksize)
1505 return open_blob(blob, repo, id, blocksize);
1508 const struct got_error *
1509 got_object_blob_open(struct got_blob_object **blob,
1510 struct got_repository *repo, struct got_object *obj, size_t blocksize)
1512 return open_blob(blob, repo, got_object_get_id(obj), blocksize);
1515 const struct got_error *
1516 got_object_blob_close(struct got_blob_object *blob)
1518 const struct got_error *err = NULL;
1519 free(blob->read_buf);
1520 if (blob->f && fclose(blob->f) == EOF)
1521 err = got_error_from_errno("fclose");
1522 free(blob->data);
1523 free(blob);
1524 return err;
1527 void
1528 got_object_blob_rewind(struct got_blob_object *blob)
1530 if (blob->f)
1531 rewind(blob->f);
1534 char *
1535 got_object_blob_id_str(struct got_blob_object *blob, char *buf, size_t size)
1537 return got_sha1_digest_to_str(blob->id.sha1, buf, size);
1540 size_t
1541 got_object_blob_get_hdrlen(struct got_blob_object *blob)
1543 return blob->hdrlen;
1546 const uint8_t *
1547 got_object_blob_get_read_buf(struct got_blob_object *blob)
1549 return blob->read_buf;
1552 const struct got_error *
1553 got_object_blob_read_block(size_t *outlenp, struct got_blob_object *blob)
1555 size_t n;
1557 n = fread(blob->read_buf, 1, blob->blocksize, blob->f);
1558 if (n == 0 && ferror(blob->f))
1559 return got_ferror(blob->f, GOT_ERR_IO);
1560 *outlenp = n;
1561 return NULL;
1564 const struct got_error *
1565 got_object_blob_dump_to_file(off_t *filesize, int *nlines,
1566 off_t **line_offsets, FILE *outfile, struct got_blob_object *blob)
1568 const struct got_error *err = NULL;
1569 size_t n, len, hdrlen;
1570 const uint8_t *buf;
1571 int i;
1572 const int alloc_chunksz = 512;
1573 size_t nalloc = 0;
1574 off_t off = 0, total_len = 0;
1576 if (line_offsets)
1577 *line_offsets = NULL;
1578 if (filesize)
1579 *filesize = 0;
1580 if (nlines)
1581 *nlines = 0;
1583 hdrlen = got_object_blob_get_hdrlen(blob);
1584 do {
1585 err = got_object_blob_read_block(&len, blob);
1586 if (err)
1587 return err;
1588 if (len == 0)
1589 break;
1590 buf = got_object_blob_get_read_buf(blob);
1591 i = hdrlen;
1592 if (nlines) {
1593 if (line_offsets && *line_offsets == NULL) {
1594 /* Have some data but perhaps no '\n'. */
1595 *nlines = 1;
1596 nalloc = alloc_chunksz;
1597 *line_offsets = calloc(nalloc,
1598 sizeof(**line_offsets));
1599 if (*line_offsets == NULL)
1600 return got_error_from_errno("calloc");
1602 /* Skip forward over end of first line. */
1603 while (i < len) {
1604 if (buf[i] == '\n')
1605 break;
1606 i++;
1609 /* Scan '\n' offsets in remaining chunk of data. */
1610 while (i < len) {
1611 if (buf[i] != '\n') {
1612 i++;
1613 continue;
1615 (*nlines)++;
1616 if (line_offsets && nalloc < *nlines) {
1617 size_t n = *nlines + alloc_chunksz;
1618 off_t *o = recallocarray(*line_offsets,
1619 nalloc, n, sizeof(**line_offsets));
1620 if (o == NULL) {
1621 free(*line_offsets);
1622 *line_offsets = NULL;
1623 return got_error_from_errno(
1624 "recallocarray");
1626 *line_offsets = o;
1627 nalloc = n;
1629 if (line_offsets) {
1630 off = total_len + i - hdrlen + 1;
1631 (*line_offsets)[*nlines - 1] = off;
1633 i++;
1636 /* Skip blob object header first time around. */
1637 n = fwrite(buf + hdrlen, 1, len - hdrlen, outfile);
1638 if (n != len - hdrlen)
1639 return got_ferror(outfile, GOT_ERR_IO);
1640 total_len += len - hdrlen;
1641 hdrlen = 0;
1642 } while (len != 0);
1644 if (fflush(outfile) != 0)
1645 return got_error_from_errno("fflush");
1646 rewind(outfile);
1648 if (filesize)
1649 *filesize = total_len;
1651 return NULL;
1654 static const struct got_error *
1655 request_packed_tag(struct got_tag_object **tag, struct got_pack *pack,
1656 int pack_idx, struct got_object_id *id)
1658 const struct got_error *err = NULL;
1660 err = got_privsep_send_tag_req(pack->privsep_child->ibuf, -1, id,
1661 pack_idx);
1662 if (err)
1663 return err;
1665 return got_privsep_recv_tag(tag, pack->privsep_child->ibuf);
1668 static const struct got_error *
1669 read_packed_tag_privsep(struct got_tag_object **tag,
1670 struct got_pack *pack, struct got_packidx *packidx, int idx,
1671 struct got_object_id *id)
1673 const struct got_error *err = NULL;
1675 if (pack->privsep_child)
1676 return request_packed_tag(tag, pack, idx, id);
1678 err = start_pack_privsep_child(pack, packidx);
1679 if (err)
1680 return err;
1682 return request_packed_tag(tag, pack, idx, id);
1685 static const struct got_error *
1686 request_tag(struct got_tag_object **tag, struct got_repository *repo,
1687 int fd)
1689 const struct got_error *err = NULL;
1690 struct imsgbuf *ibuf;
1692 ibuf = repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TAG].ibuf;
1694 err = got_privsep_send_tag_req(ibuf, fd, NULL, -1);
1695 if (err)
1696 return err;
1698 return got_privsep_recv_tag(tag, ibuf);
1701 static const struct got_error *
1702 read_tag_privsep(struct got_tag_object **tag, int obj_fd,
1703 struct got_repository *repo)
1705 const struct got_error *err;
1706 int imsg_fds[2];
1707 pid_t pid;
1708 struct imsgbuf *ibuf;
1710 if (repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TAG].imsg_fd != -1)
1711 return request_tag(tag, repo, obj_fd);
1713 ibuf = calloc(1, sizeof(*ibuf));
1714 if (ibuf == NULL)
1715 return got_error_from_errno("calloc");
1717 if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_fds) == -1) {
1718 err = got_error_from_errno("socketpair");
1719 free(ibuf);
1720 return err;
1723 pid = fork();
1724 if (pid == -1) {
1725 err = got_error_from_errno("fork");
1726 free(ibuf);
1727 return err;
1729 else if (pid == 0) {
1730 got_privsep_exec_child(imsg_fds, GOT_PATH_PROG_READ_TAG,
1731 repo->path);
1732 /* not reached */
1735 if (close(imsg_fds[1]) == -1) {
1736 err = got_error_from_errno("close");
1737 free(ibuf);
1738 return err;
1740 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TAG].imsg_fd =
1741 imsg_fds[0];
1742 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TAG].pid = pid;
1743 imsg_init(ibuf, imsg_fds[0]);
1744 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TAG].ibuf = ibuf;
1746 return request_tag(tag, repo, obj_fd);
1749 static const struct got_error *
1750 open_tag(struct got_tag_object **tag, struct got_repository *repo,
1751 struct got_object_id *id, int check_cache)
1753 const struct got_error *err = NULL;
1754 struct got_packidx *packidx = NULL;
1755 int idx;
1756 char *path_packfile = NULL;
1757 struct got_object *obj = NULL;
1758 int obj_type = GOT_OBJ_TYPE_ANY;
1760 if (check_cache) {
1761 *tag = got_repo_get_cached_tag(repo, id);
1762 if (*tag != NULL) {
1763 (*tag)->refcnt++;
1764 return NULL;
1766 } else
1767 *tag = NULL;
1769 err = got_repo_search_packidx(&packidx, &idx, repo, id);
1770 if (err == NULL) {
1771 struct got_pack *pack = NULL;
1773 err = get_packfile_path(&path_packfile, packidx);
1774 if (err)
1775 return err;
1777 pack = got_repo_get_cached_pack(repo, path_packfile);
1778 if (pack == NULL) {
1779 err = got_repo_cache_pack(&pack, repo, path_packfile,
1780 packidx);
1781 if (err)
1782 goto done;
1785 /* Beware of "lightweight" tags: Check object type first. */
1786 err = read_packed_object_privsep(&obj, repo, pack, packidx,
1787 idx, id);
1788 if (err)
1789 goto done;
1790 obj_type = obj->type;
1791 got_object_close(obj);
1792 if (obj_type != GOT_OBJ_TYPE_TAG) {
1793 err = got_error(GOT_ERR_OBJ_TYPE);
1794 goto done;
1796 err = read_packed_tag_privsep(tag, pack, packidx, idx, id);
1797 } else if (err->code == GOT_ERR_NO_OBJ) {
1798 int fd;
1800 err = open_loose_object(&fd, id, repo);
1801 if (err)
1802 return err;
1803 err = read_object_header_privsep(&obj, repo, fd);
1804 if (err)
1805 return err;
1806 obj_type = obj->type;
1807 got_object_close(obj);
1808 if (obj_type != GOT_OBJ_TYPE_TAG)
1809 return got_error(GOT_ERR_OBJ_TYPE);
1811 err = open_loose_object(&fd, id, repo);
1812 if (err)
1813 return err;
1814 err = read_tag_privsep(tag, fd, repo);
1817 if (err == NULL) {
1818 (*tag)->refcnt++;
1819 err = got_repo_cache_tag(repo, id, *tag);
1821 done:
1822 free(path_packfile);
1823 return err;
1826 const struct got_error *
1827 got_object_open_as_tag(struct got_tag_object **tag,
1828 struct got_repository *repo, struct got_object_id *id)
1830 *tag = got_repo_get_cached_tag(repo, id);
1831 if (*tag != NULL) {
1832 (*tag)->refcnt++;
1833 return NULL;
1836 return open_tag(tag, repo, id, 0);
1839 const struct got_error *
1840 got_object_tag_open(struct got_tag_object **tag,
1841 struct got_repository *repo, struct got_object *obj)
1843 return open_tag(tag, repo, got_object_get_id(obj), 1);
1846 const char *
1847 got_object_tag_get_name(struct got_tag_object *tag)
1849 return tag->tag;
1852 int
1853 got_object_tag_get_object_type(struct got_tag_object *tag)
1855 return tag->obj_type;
1858 struct got_object_id *
1859 got_object_tag_get_object_id(struct got_tag_object *tag)
1861 return &tag->id;
1864 time_t
1865 got_object_tag_get_tagger_time(struct got_tag_object *tag)
1867 return tag->tagger_time;
1870 time_t
1871 got_object_tag_get_tagger_gmtoff(struct got_tag_object *tag)
1873 return tag->tagger_gmtoff;
1876 const char *
1877 got_object_tag_get_tagger(struct got_tag_object *tag)
1879 return tag->tagger;
1882 const char *
1883 got_object_tag_get_message(struct got_tag_object *tag)
1885 return tag->tagmsg;
1888 static struct got_tree_entry *
1889 find_entry_by_name(struct got_tree_object *tree, const char *name, size_t len)
1891 int i;
1893 /* Note that tree entries are sorted in strncmp() order. */
1894 for (i = 0; i < tree->nentries; i++) {
1895 struct got_tree_entry *te = &tree->entries[i];
1896 int cmp = strncmp(te->name, name, len);
1897 if (cmp < 0)
1898 continue;
1899 if (cmp > 0)
1900 break;
1901 if (te->name[len] == '\0')
1902 return te;
1904 return NULL;
1907 struct got_tree_entry *
1908 got_object_tree_find_entry(struct got_tree_object *tree, const char *name)
1910 return find_entry_by_name(tree, name, strlen(name));
1913 const struct got_error *
1914 got_object_id_by_path(struct got_object_id **id, struct got_repository *repo,
1915 struct got_object_id *commit_id, const char *path)
1917 const struct got_error *err = NULL;
1918 struct got_commit_object *commit = NULL;
1919 struct got_tree_object *tree = NULL;
1920 struct got_tree_entry *te = NULL;
1921 const char *seg, *s;
1922 size_t seglen;
1924 *id = NULL;
1926 err = got_object_open_as_commit(&commit, repo, commit_id);
1927 if (err)
1928 goto done;
1930 /* Handle opening of root of commit's tree. */
1931 if (got_path_is_root_dir(path)) {
1932 *id = got_object_id_dup(commit->tree_id);
1933 if (*id == NULL)
1934 err = got_error_from_errno("got_object_id_dup");
1935 goto done;
1938 err = got_object_open_as_tree(&tree, repo, commit->tree_id);
1939 if (err)
1940 goto done;
1942 s = path;
1943 while (s[0] == '/')
1944 s++;
1945 seg = s;
1946 seglen = 0;
1947 while (*s) {
1948 struct got_tree_object *next_tree;
1950 if (*s != '/') {
1951 s++;
1952 seglen++;
1953 if (*s)
1954 continue;
1957 te = find_entry_by_name(tree, seg, seglen);
1958 if (te == NULL) {
1959 err = got_error_path(path, GOT_ERR_NO_TREE_ENTRY);
1960 goto done;
1963 if (*s == '\0')
1964 break;
1966 seg = s + 1;
1967 seglen = 0;
1968 s++;
1969 if (*s) {
1970 err = got_object_open_as_tree(&next_tree, repo,
1971 &te->id);
1972 te = NULL;
1973 if (err)
1974 goto done;
1975 got_object_tree_close(tree);
1976 tree = next_tree;
1980 if (te) {
1981 *id = got_object_id_dup(&te->id);
1982 if (*id == NULL)
1983 return got_error_from_errno("got_object_id_dup");
1984 } else
1985 err = got_error_path(path, GOT_ERR_NO_TREE_ENTRY);
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 = get_packfile_path(&path_packfile, packidx);
2278 if (err)
2279 return err;
2281 pack = got_repo_get_cached_pack(repo, path_packfile);
2282 if (pack == NULL) {
2283 err = got_repo_cache_pack(&pack, repo, path_packfile, packidx);
2284 if (err)
2285 goto done;
2288 if (pack->privsep_child == NULL) {
2289 err = start_pack_privsep_child(pack, packidx);
2290 if (err)
2291 goto done;
2294 err = got_privsep_send_commit_traversal_request(
2295 pack->privsep_child->ibuf, commit_id, idx, path);
2296 if (err)
2297 goto done;
2299 err = got_privsep_recv_traversed_commits(&changed_commit,
2300 &changed_commit_id, traversed_commits, pack->privsep_child->ibuf);
2301 if (err)
2302 goto done;
2304 if (changed_commit) {
2306 * Cache the commit in which the path was changed.
2307 * This commit might be opened again soon.
2309 changed_commit->refcnt++;
2310 err = got_repo_cache_commit(repo, changed_commit_id,
2311 changed_commit);
2312 got_object_commit_close(changed_commit);
2314 done:
2315 free(path_packfile);
2316 free(changed_commit_id);
2317 return err;