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 const struct got_error *
129 got_object_open_loose_fd(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 request_packed_object(struct got_object **obj, struct got_pack *pack, int idx,
150 struct got_object_id *id)
152 const struct got_error *err = NULL;
153 struct imsgbuf *ibuf = pack->privsep_child->ibuf;
155 err = got_privsep_send_packed_obj_req(ibuf, idx, id);
156 if (err)
157 return err;
159 err = got_privsep_recv_obj(obj, ibuf);
160 if (err)
161 return err;
163 memcpy(&(*obj)->id, id, sizeof((*obj)->id));
165 return NULL;
168 static const struct got_error *
169 request_packed_object_raw(uint8_t **outbuf, off_t *size, size_t *hdrlen,
170 int outfd, struct got_pack *pack, int idx, struct got_object_id *id)
172 const struct got_error *err = NULL;
173 struct imsgbuf *ibuf = pack->privsep_child->ibuf;
174 int outfd_child;
175 int basefd, accumfd; /* temporary files for delta application */
177 basefd = got_opentempfd();
178 if (basefd == -1)
179 return got_error_from_errno("got_opentempfd");
181 accumfd = got_opentempfd();
182 if (accumfd == -1) {
183 close(basefd);
184 return got_error_from_errno("got_opentempfd");
187 outfd_child = dup(outfd);
188 if (outfd_child == -1) {
189 err = got_error_from_errno("dup");
190 close(basefd);
191 close(accumfd);
192 return err;
195 err = got_privsep_send_packed_raw_obj_req(ibuf, idx, id);
196 if (err) {
197 close(basefd);
198 close(accumfd);
199 close(outfd_child);
200 return err;
203 err = got_privsep_send_raw_obj_outfd(ibuf, outfd_child);
204 if (err) {
205 close(basefd);
206 close(accumfd);
207 return err;
211 err = got_privsep_send_tmpfd(pack->privsep_child->ibuf,
212 basefd);
213 if (err) {
214 close(accumfd);
215 return err;
218 err = got_privsep_send_tmpfd(pack->privsep_child->ibuf,
219 accumfd);
220 if (err)
221 return err;
223 err = got_privsep_recv_raw_obj(outbuf, size, hdrlen, ibuf);
224 if (err)
225 return err;
227 return NULL;
230 static void
231 set_max_datasize(void)
233 struct rlimit rl;
235 if (getrlimit(RLIMIT_DATA, &rl) != 0)
236 return;
238 rl.rlim_cur = rl.rlim_max;
239 setrlimit(RLIMIT_DATA, &rl);
242 static const struct got_error *
243 start_pack_privsep_child(struct got_pack *pack, struct got_packidx *packidx)
245 const struct got_error *err = NULL;
246 int imsg_fds[2];
247 pid_t pid;
248 struct imsgbuf *ibuf;
250 ibuf = calloc(1, sizeof(*ibuf));
251 if (ibuf == NULL)
252 return got_error_from_errno("calloc");
254 pack->privsep_child = calloc(1, sizeof(*pack->privsep_child));
255 if (pack->privsep_child == NULL) {
256 err = got_error_from_errno("calloc");
257 free(ibuf);
258 return err;
261 if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_fds) == -1) {
262 err = got_error_from_errno("socketpair");
263 goto done;
266 pid = fork();
267 if (pid == -1) {
268 err = got_error_from_errno("fork");
269 goto done;
270 } else if (pid == 0) {
271 set_max_datasize();
272 got_privsep_exec_child(imsg_fds, GOT_PATH_PROG_READ_PACK,
273 pack->path_packfile);
274 /* not reached */
277 if (close(imsg_fds[1]) == -1)
278 return got_error_from_errno("close");
279 pack->privsep_child->imsg_fd = imsg_fds[0];
280 pack->privsep_child->pid = pid;
281 imsg_init(ibuf, imsg_fds[0]);
282 pack->privsep_child->ibuf = ibuf;
284 err = got_privsep_init_pack_child(ibuf, pack, packidx);
285 if (err) {
286 const struct got_error *child_err;
287 err = got_privsep_send_stop(pack->privsep_child->imsg_fd);
288 child_err = got_privsep_wait_for_child(
289 pack->privsep_child->pid);
290 if (child_err && err == NULL)
291 err = child_err;
293 done:
294 if (err) {
295 free(ibuf);
296 free(pack->privsep_child);
297 pack->privsep_child = NULL;
299 return err;
302 static const struct got_error *
303 read_packed_object_privsep(struct got_object **obj,
304 struct got_repository *repo, struct got_pack *pack,
305 struct got_packidx *packidx, int idx, struct got_object_id *id)
307 const struct got_error *err = NULL;
309 if (pack->privsep_child == NULL) {
310 err = start_pack_privsep_child(pack, packidx);
311 if (err)
312 return err;
315 return request_packed_object(obj, pack, idx, id);
318 static const struct got_error *
319 read_packed_object_raw_privsep(uint8_t **outbuf, off_t *size, size_t *hdrlen,
320 int outfd, struct got_pack *pack, struct got_packidx *packidx, int idx,
321 struct got_object_id *id)
323 const struct got_error *err = NULL;
325 if (pack->privsep_child == NULL) {
326 err = start_pack_privsep_child(pack, packidx);
327 if (err)
328 return err;
331 return request_packed_object_raw(outbuf, size, hdrlen, outfd, pack,
332 idx, id);
335 const struct got_error *
336 got_object_open_packed(struct got_object **obj, struct got_object_id *id,
337 struct got_repository *repo)
339 const struct got_error *err = NULL;
340 struct got_pack *pack = NULL;
341 struct got_packidx *packidx = NULL;
342 int idx;
343 char *path_packfile;
345 err = got_repo_search_packidx(&packidx, &idx, repo, id);
346 if (err)
347 return err;
349 err = got_packidx_get_packfile_path(&path_packfile,
350 packidx->path_packidx);
351 if (err)
352 return err;
354 pack = got_repo_get_cached_pack(repo, path_packfile);
355 if (pack == NULL) {
356 err = got_repo_cache_pack(&pack, repo, path_packfile, packidx);
357 if (err)
358 goto done;
361 err = read_packed_object_privsep(obj, repo, pack, packidx, idx, id);
362 if (err)
363 goto done;
364 done:
365 free(path_packfile);
366 return err;
369 static const struct got_error *
370 request_object(struct got_object **obj, struct got_repository *repo, int fd)
372 const struct got_error *err = NULL;
373 struct imsgbuf *ibuf;
375 ibuf = repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_OBJECT].ibuf;
377 err = got_privsep_send_obj_req(ibuf, fd);
378 if (err)
379 return err;
381 return got_privsep_recv_obj(obj, ibuf);
384 static const struct got_error *
385 request_raw_object(uint8_t **outbuf, off_t *size, size_t *hdrlen, int outfd,
386 struct got_repository *repo, int infd)
388 const struct got_error *err = NULL;
389 struct imsgbuf *ibuf;
390 int outfd_child;
392 ibuf = repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_OBJECT].ibuf;
394 outfd_child = dup(outfd);
395 if (outfd_child == -1)
396 return got_error_from_errno("dup");
398 err = got_privsep_send_raw_obj_req(ibuf, infd);
399 if (err)
400 return err;
402 err = got_privsep_send_raw_obj_outfd(ibuf, outfd_child);
403 if (err)
404 return err;
406 return got_privsep_recv_raw_obj(outbuf, size, hdrlen, ibuf);
409 static const struct got_error *
410 start_read_object_child(struct got_repository *repo)
412 const struct got_error *err = NULL;
413 int imsg_fds[2];
414 pid_t pid;
415 struct imsgbuf *ibuf;
417 ibuf = calloc(1, sizeof(*ibuf));
418 if (ibuf == NULL)
419 return got_error_from_errno("calloc");
421 if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_fds) == -1) {
422 err = got_error_from_errno("socketpair");
423 free(ibuf);
424 return err;
427 pid = fork();
428 if (pid == -1) {
429 err = got_error_from_errno("fork");
430 free(ibuf);
431 return err;
433 else if (pid == 0) {
434 got_privsep_exec_child(imsg_fds, GOT_PATH_PROG_READ_OBJECT,
435 repo->path);
436 /* not reached */
439 if (close(imsg_fds[1]) == -1) {
440 err = got_error_from_errno("close");
441 free(ibuf);
442 return err;
445 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_OBJECT].imsg_fd =
446 imsg_fds[0];
447 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_OBJECT].pid = pid;
448 imsg_init(ibuf, imsg_fds[0]);
449 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_OBJECT].ibuf = ibuf;
451 return NULL;
454 const struct got_error *
455 got_object_read_header_privsep(struct got_object **obj,
456 struct got_repository *repo, int obj_fd)
458 const struct got_error *err;
460 if (repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_OBJECT].imsg_fd != -1)
461 return request_object(obj, repo, obj_fd);
463 err = start_read_object_child(repo);
464 if (err) {
465 close(obj_fd);
466 return err;
469 return request_object(obj, repo, obj_fd);
472 static const struct got_error *
473 read_object_raw_privsep(uint8_t **outbuf, off_t *size, size_t *hdrlen,
474 int outfd, struct got_repository *repo, int obj_fd)
476 const struct got_error *err;
478 if (repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_OBJECT].imsg_fd != -1)
479 return request_raw_object(outbuf, size, hdrlen, outfd, repo,
480 obj_fd);
482 err = start_read_object_child(repo);
483 if (err)
484 return err;
486 return request_raw_object(outbuf, size, hdrlen, outfd, repo, obj_fd);
489 const struct got_error *
490 got_object_open(struct got_object **obj, struct got_repository *repo,
491 struct got_object_id *id)
493 const struct got_error *err = NULL;
494 int fd;
496 *obj = got_repo_get_cached_object(repo, id);
497 if (*obj != NULL) {
498 (*obj)->refcnt++;
499 return NULL;
502 err = got_object_open_packed(obj, id, repo);
503 if (err && err->code != GOT_ERR_NO_OBJ)
504 return err;
505 if (*obj) {
506 (*obj)->refcnt++;
507 return got_repo_cache_object(repo, id, *obj);
510 err = got_object_open_loose_fd(&fd, id, repo);
511 if (err) {
512 if (err->code == GOT_ERR_ERRNO && errno == ENOENT)
513 err = got_error_no_obj(id);
514 return err;
517 err = got_object_read_header_privsep(obj, repo, fd);
518 if (err)
519 return err;
521 memcpy((*obj)->id.sha1, id->sha1, SHA1_DIGEST_LENGTH);
523 (*obj)->refcnt++;
524 return got_repo_cache_object(repo, id, *obj);
527 const struct got_error *
528 got_object_raw_open(struct got_raw_object **obj, struct got_repository *repo,
529 struct got_object_id *id, size_t blocksize)
531 const struct got_error *err = NULL;
532 struct got_packidx *packidx = NULL;
533 int idx;
534 uint8_t *outbuf = NULL;
535 int outfd = -1;
536 off_t size = 0;
537 size_t hdrlen = 0;
538 char *path_packfile = NULL;
540 *obj = NULL;
542 outfd = got_opentempfd();
543 if (outfd == -1)
544 return got_error_from_errno("got_opentempfd");
546 err = got_repo_search_packidx(&packidx, &idx, repo, id);
547 if (err == NULL) {
548 struct got_pack *pack = NULL;
550 err = got_packidx_get_packfile_path(&path_packfile,
551 packidx->path_packidx);
552 if (err)
553 goto done;
555 pack = got_repo_get_cached_pack(repo, path_packfile);
556 if (pack == NULL) {
557 err = got_repo_cache_pack(&pack, repo, path_packfile,
558 packidx);
559 if (err)
560 goto done;
562 err = read_packed_object_raw_privsep(&outbuf, &size, &hdrlen,
563 outfd, pack, packidx, idx, id);
564 } else if (err->code == GOT_ERR_NO_OBJ) {
565 int fd;
567 err = got_object_open_loose_fd(&fd, id, repo);
568 if (err)
569 goto done;
570 err = read_object_raw_privsep(&outbuf, &size, &hdrlen, outfd,
571 repo, fd);
574 *obj = calloc(1, sizeof(**obj));
575 if (*obj == NULL) {
576 err = got_error_from_errno("calloc");
577 goto done;
580 (*obj)->read_buf = malloc(blocksize);
581 if ((*obj)->read_buf == NULL) {
582 err = got_error_from_errno("malloc");
583 goto done;
586 if (outbuf) {
587 if (close(outfd) == -1) {
588 err = got_error_from_errno("close");
589 goto done;
591 outfd = -1;
592 (*obj)->f = fmemopen(outbuf, hdrlen + size, "r");
593 if ((*obj)->f == NULL) {
594 err = got_error_from_errno("fdopen");
595 goto done;
597 (*obj)->data = outbuf;
598 } else {
599 struct stat sb;
600 if (fstat(outfd, &sb) == -1) {
601 err = got_error_from_errno("fstat");
602 goto done;
605 if (sb.st_size != hdrlen + size) {
606 err = got_error(GOT_ERR_PRIVSEP_LEN);
607 goto done;
610 (*obj)->f = fdopen(outfd, "r");
611 if ((*obj)->f == NULL) {
612 err = got_error_from_errno("fdopen");
613 goto done;
615 outfd = -1;
616 (*obj)->data = NULL;
618 (*obj)->hdrlen = hdrlen;
619 (*obj)->size = size;
620 (*obj)->blocksize = blocksize;
621 done:
622 free(path_packfile);
623 if (err) {
624 if (*obj) {
625 got_object_raw_close(*obj);
626 *obj = NULL;
628 if (outfd != -1)
629 close(outfd);
630 free(outbuf);
632 return err;
635 void
636 got_object_raw_rewind(struct got_raw_object *obj)
638 if (obj->f)
639 rewind(obj->f);
642 size_t
643 got_object_raw_get_hdrlen(struct got_raw_object *obj)
645 return obj->hdrlen;
648 const uint8_t *
649 got_object_raw_get_read_buf(struct got_raw_object *obj)
651 return obj->read_buf;
654 const struct got_error *
655 got_object_raw_read_block(size_t *outlenp, struct got_raw_object *obj)
657 size_t n;
659 n = fread(obj->read_buf, 1, obj->blocksize, obj->f);
660 if (n == 0 && ferror(obj->f))
661 return got_ferror(obj->f, GOT_ERR_IO);
662 *outlenp = n;
663 return NULL;
666 const struct got_error *
667 got_object_raw_close(struct got_raw_object *obj)
669 const struct got_error *err = NULL;
671 free(obj->read_buf);
672 if (obj->f != NULL && fclose(obj->f) == EOF && err == NULL)
673 err = got_error_from_errno("fclose");
674 free(obj->data);
675 free(obj);
676 return err;
679 const struct got_error *
680 got_object_open_by_id_str(struct got_object **obj, struct got_repository *repo,
681 const char *id_str)
683 struct got_object_id id;
685 if (!got_parse_sha1_digest(id.sha1, id_str))
686 return got_error_path(id_str, GOT_ERR_BAD_OBJ_ID_STR);
688 return got_object_open(obj, repo, &id);
691 const struct got_error *
692 got_object_resolve_id_str(struct got_object_id **id,
693 struct got_repository *repo, const char *id_str)
695 const struct got_error *err = NULL;
696 struct got_object *obj;
698 err = got_object_open_by_id_str(&obj, repo, id_str);
699 if (err)
700 return err;
702 *id = got_object_id_dup(got_object_get_id(obj));
703 got_object_close(obj);
704 if (*id == NULL)
705 return got_error_from_errno("got_object_id_dup");
707 return NULL;
710 static const struct got_error *
711 request_packed_commit(struct got_commit_object **commit, struct got_pack *pack,
712 int pack_idx, struct got_object_id *id)
714 const struct got_error *err = NULL;
716 err = got_privsep_send_commit_req(pack->privsep_child->ibuf, -1, id,
717 pack_idx);
718 if (err)
719 return err;
721 err = got_privsep_recv_commit(commit, pack->privsep_child->ibuf);
722 if (err)
723 return err;
725 (*commit)->flags |= GOT_COMMIT_FLAG_PACKED;
726 return NULL;
729 static const struct got_error *
730 read_packed_commit_privsep(struct got_commit_object **commit,
731 struct got_pack *pack, struct got_packidx *packidx, int idx,
732 struct got_object_id *id)
734 const struct got_error *err = NULL;
736 if (pack->privsep_child)
737 return request_packed_commit(commit, pack, idx, id);
739 err = start_pack_privsep_child(pack, packidx);
740 if (err)
741 return err;
743 return request_packed_commit(commit, pack, idx, id);
746 static const struct got_error *
747 request_commit(struct got_commit_object **commit, struct got_repository *repo,
748 int fd)
750 const struct got_error *err = NULL;
751 struct imsgbuf *ibuf;
753 ibuf = repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_COMMIT].ibuf;
755 err = got_privsep_send_commit_req(ibuf, fd, NULL, -1);
756 if (err)
757 return err;
759 return got_privsep_recv_commit(commit, ibuf);
762 static const struct got_error *
763 read_commit_privsep(struct got_commit_object **commit, int obj_fd,
764 struct got_repository *repo)
766 const struct got_error *err;
767 int imsg_fds[2];
768 pid_t pid;
769 struct imsgbuf *ibuf;
771 if (repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_COMMIT].imsg_fd != -1)
772 return request_commit(commit, repo, obj_fd);
774 ibuf = calloc(1, sizeof(*ibuf));
775 if (ibuf == NULL)
776 return got_error_from_errno("calloc");
778 if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_fds) == -1) {
779 err = got_error_from_errno("socketpair");
780 free(ibuf);
781 return err;
784 pid = fork();
785 if (pid == -1) {
786 err = got_error_from_errno("fork");
787 free(ibuf);
788 return err;
790 else if (pid == 0) {
791 got_privsep_exec_child(imsg_fds, GOT_PATH_PROG_READ_COMMIT,
792 repo->path);
793 /* not reached */
796 if (close(imsg_fds[1]) == -1) {
797 err = got_error_from_errno("close");
798 free(ibuf);
799 return err;
801 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_COMMIT].imsg_fd =
802 imsg_fds[0];
803 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_COMMIT].pid = pid;
804 imsg_init(ibuf, imsg_fds[0]);
805 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_COMMIT].ibuf = ibuf;
807 return request_commit(commit, repo, obj_fd);
811 static const struct got_error *
812 open_commit(struct got_commit_object **commit,
813 struct got_repository *repo, struct got_object_id *id, int check_cache)
815 const struct got_error *err = NULL;
816 struct got_packidx *packidx = NULL;
817 int idx;
818 char *path_packfile = NULL;
820 if (check_cache) {
821 *commit = got_repo_get_cached_commit(repo, id);
822 if (*commit != NULL) {
823 (*commit)->refcnt++;
824 return NULL;
826 } else
827 *commit = NULL;
829 err = got_repo_search_packidx(&packidx, &idx, repo, id);
830 if (err == NULL) {
831 struct got_pack *pack = NULL;
833 err = got_packidx_get_packfile_path(&path_packfile,
834 packidx->path_packidx);
835 if (err)
836 return err;
838 pack = got_repo_get_cached_pack(repo, path_packfile);
839 if (pack == NULL) {
840 err = got_repo_cache_pack(&pack, repo, path_packfile,
841 packidx);
842 if (err)
843 goto done;
845 err = read_packed_commit_privsep(commit, pack,
846 packidx, idx, id);
847 } else if (err->code == GOT_ERR_NO_OBJ) {
848 int fd;
850 err = got_object_open_loose_fd(&fd, id, repo);
851 if (err)
852 return err;
853 err = read_commit_privsep(commit, fd, repo);
856 if (err == NULL) {
857 (*commit)->refcnt++;
858 err = got_repo_cache_commit(repo, id, *commit);
860 done:
861 free(path_packfile);
862 return err;
865 const struct got_error *
866 got_object_open_as_commit(struct got_commit_object **commit,
867 struct got_repository *repo, struct got_object_id *id)
869 *commit = got_repo_get_cached_commit(repo, id);
870 if (*commit != NULL) {
871 (*commit)->refcnt++;
872 return NULL;
875 return open_commit(commit, repo, id, 0);
878 const struct got_error *
879 got_object_commit_open(struct got_commit_object **commit,
880 struct got_repository *repo, struct got_object *obj)
882 return open_commit(commit, repo, got_object_get_id(obj), 1);
885 const struct got_error *
886 got_object_qid_alloc(struct got_object_qid **qid, struct got_object_id *id)
888 const struct got_error *err = NULL;
890 *qid = calloc(1, sizeof(**qid));
891 if (*qid == NULL)
892 return got_error_from_errno("calloc");
894 (*qid)->id = got_object_id_dup(id);
895 if ((*qid)->id == NULL) {
896 err = got_error_from_errno("got_object_id_dup");
897 got_object_qid_free(*qid);
898 *qid = NULL;
899 return err;
902 return NULL;
905 const struct got_error *
906 got_object_id_queue_copy(const struct got_object_id_queue *src,
907 struct got_object_id_queue *dest)
909 const struct got_error *err;
910 struct got_object_qid *qid;
912 STAILQ_FOREACH(qid, src, entry) {
913 struct got_object_qid *new;
914 /*
915 * Deep-copy the object ID only. Let the caller deal
916 * with setting up the new->data pointer if needed.
917 */
918 err = got_object_qid_alloc(&new, qid->id);
919 if (err) {
920 got_object_id_queue_free(dest);
921 return err;
923 STAILQ_INSERT_TAIL(dest, new, entry);
926 return NULL;
929 static const struct got_error *
930 request_packed_tree(struct got_tree_object **tree, struct got_pack *pack,
931 int pack_idx, struct got_object_id *id)
933 const struct got_error *err = NULL;
935 err = got_privsep_send_tree_req(pack->privsep_child->ibuf, -1, id,
936 pack_idx);
937 if (err)
938 return err;
940 return got_privsep_recv_tree(tree, pack->privsep_child->ibuf);
943 static const struct got_error *
944 read_packed_tree_privsep(struct got_tree_object **tree,
945 struct got_pack *pack, struct got_packidx *packidx, int idx,
946 struct got_object_id *id)
948 const struct got_error *err = NULL;
950 if (pack->privsep_child)
951 return request_packed_tree(tree, pack, idx, id);
953 err = start_pack_privsep_child(pack, packidx);
954 if (err)
955 return err;
957 return request_packed_tree(tree, pack, idx, id);
960 static const struct got_error *
961 request_tree(struct got_tree_object **tree, struct got_repository *repo,
962 int fd)
964 const struct got_error *err = NULL;
965 struct imsgbuf *ibuf;
967 ibuf = repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TREE].ibuf;
969 err = got_privsep_send_tree_req(ibuf, fd, NULL, -1);
970 if (err)
971 return err;
973 return got_privsep_recv_tree(tree, ibuf);
976 const struct got_error *
977 read_tree_privsep(struct got_tree_object **tree, int obj_fd,
978 struct got_repository *repo)
980 const struct got_error *err;
981 int imsg_fds[2];
982 pid_t pid;
983 struct imsgbuf *ibuf;
985 if (repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TREE].imsg_fd != -1)
986 return request_tree(tree, repo, obj_fd);
988 ibuf = calloc(1, sizeof(*ibuf));
989 if (ibuf == NULL)
990 return got_error_from_errno("calloc");
992 if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_fds) == -1) {
993 err = got_error_from_errno("socketpair");
994 free(ibuf);
995 return err;
998 pid = fork();
999 if (pid == -1) {
1000 err = got_error_from_errno("fork");
1001 free(ibuf);
1002 return err;
1004 else if (pid == 0) {
1005 got_privsep_exec_child(imsg_fds, GOT_PATH_PROG_READ_TREE,
1006 repo->path);
1007 /* not reached */
1010 if (close(imsg_fds[1]) == -1) {
1011 err = got_error_from_errno("close");
1012 free(ibuf);
1013 return err;
1015 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TREE].imsg_fd =
1016 imsg_fds[0];
1017 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TREE].pid = pid;
1018 imsg_init(ibuf, imsg_fds[0]);
1019 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TREE].ibuf = ibuf;
1022 return request_tree(tree, repo, obj_fd);
1025 static const struct got_error *
1026 open_tree(struct got_tree_object **tree, struct got_repository *repo,
1027 struct got_object_id *id, int check_cache)
1029 const struct got_error *err = NULL;
1030 struct got_packidx *packidx = NULL;
1031 int idx;
1032 char *path_packfile = NULL;
1034 if (check_cache) {
1035 *tree = got_repo_get_cached_tree(repo, id);
1036 if (*tree != NULL) {
1037 (*tree)->refcnt++;
1038 return NULL;
1040 } else
1041 *tree = NULL;
1043 err = got_repo_search_packidx(&packidx, &idx, repo, id);
1044 if (err == NULL) {
1045 struct got_pack *pack = NULL;
1047 err = got_packidx_get_packfile_path(&path_packfile,
1048 packidx->path_packidx);
1049 if (err)
1050 return err;
1052 pack = got_repo_get_cached_pack(repo, path_packfile);
1053 if (pack == NULL) {
1054 err = got_repo_cache_pack(&pack, repo, path_packfile,
1055 packidx);
1056 if (err)
1057 goto done;
1059 err = read_packed_tree_privsep(tree, pack,
1060 packidx, idx, id);
1061 } else if (err->code == GOT_ERR_NO_OBJ) {
1062 int fd;
1064 err = got_object_open_loose_fd(&fd, id, repo);
1065 if (err)
1066 return err;
1067 err = read_tree_privsep(tree, fd, repo);
1070 if (err == NULL) {
1071 (*tree)->refcnt++;
1072 err = got_repo_cache_tree(repo, id, *tree);
1074 done:
1075 free(path_packfile);
1076 return err;
1079 const struct got_error *
1080 got_object_open_as_tree(struct got_tree_object **tree,
1081 struct got_repository *repo, struct got_object_id *id)
1083 *tree = got_repo_get_cached_tree(repo, id);
1084 if (*tree != NULL) {
1085 (*tree)->refcnt++;
1086 return NULL;
1089 return open_tree(tree, repo, id, 0);
1092 const struct got_error *
1093 got_object_tree_open(struct got_tree_object **tree,
1094 struct got_repository *repo, struct got_object *obj)
1096 return open_tree(tree, repo, got_object_get_id(obj), 1);
1099 int
1100 got_object_tree_get_nentries(struct got_tree_object *tree)
1102 return tree->nentries;
1105 struct got_tree_entry *
1106 got_object_tree_get_first_entry(struct got_tree_object *tree)
1108 return got_object_tree_get_entry(tree, 0);
1111 struct got_tree_entry *
1112 got_object_tree_get_last_entry(struct got_tree_object *tree)
1114 return got_object_tree_get_entry(tree, tree->nentries - 1);
1117 struct got_tree_entry *
1118 got_object_tree_get_entry(struct got_tree_object *tree, int i)
1120 if (i < 0 || i >= tree->nentries)
1121 return NULL;
1122 return &tree->entries[i];
1125 mode_t
1126 got_tree_entry_get_mode(struct got_tree_entry *te)
1128 return te->mode;
1131 const char *
1132 got_tree_entry_get_name(struct got_tree_entry *te)
1134 return &te->name[0];
1137 struct got_object_id *
1138 got_tree_entry_get_id(struct got_tree_entry *te)
1140 return &te->id;
1143 const struct got_error *
1144 got_object_blob_read_to_str(char **s, struct got_blob_object *blob)
1146 const struct got_error *err = NULL;
1147 size_t len, totlen, hdrlen, offset;
1149 *s = NULL;
1151 hdrlen = got_object_blob_get_hdrlen(blob);
1152 totlen = 0;
1153 offset = 0;
1154 do {
1155 char *p;
1157 err = got_object_blob_read_block(&len, blob);
1158 if (err)
1159 return err;
1161 if (len == 0)
1162 break;
1164 totlen += len - hdrlen;
1165 p = realloc(*s, totlen + 1);
1166 if (p == NULL) {
1167 err = got_error_from_errno("realloc");
1168 free(*s);
1169 *s = NULL;
1170 return err;
1172 *s = p;
1173 /* Skip blob object header first time around. */
1174 memcpy(*s + offset,
1175 got_object_blob_get_read_buf(blob) + hdrlen, len - hdrlen);
1176 hdrlen = 0;
1177 offset = totlen;
1178 } while (len > 0);
1180 (*s)[totlen] = '\0';
1181 return NULL;
1184 const struct got_error *
1185 got_tree_entry_get_symlink_target(char **link_target, struct got_tree_entry *te,
1186 struct got_repository *repo)
1188 const struct got_error *err = NULL;
1189 struct got_blob_object *blob = NULL;
1191 *link_target = NULL;
1193 if (!got_object_tree_entry_is_symlink(te))
1194 return got_error(GOT_ERR_TREE_ENTRY_TYPE);
1196 err = got_object_open_as_blob(&blob, repo,
1197 got_tree_entry_get_id(te), PATH_MAX);
1198 if (err)
1199 return err;
1201 err = got_object_blob_read_to_str(link_target, blob);
1202 got_object_blob_close(blob);
1203 if (err) {
1204 free(*link_target);
1205 *link_target = NULL;
1207 return err;
1210 int
1211 got_tree_entry_get_index(struct got_tree_entry *te)
1213 return te->idx;
1216 struct got_tree_entry *
1217 got_tree_entry_get_next(struct got_tree_object *tree,
1218 struct got_tree_entry *te)
1220 return got_object_tree_get_entry(tree, te->idx + 1);
1223 struct got_tree_entry *
1224 got_tree_entry_get_prev(struct got_tree_object *tree,
1225 struct got_tree_entry *te)
1227 return got_object_tree_get_entry(tree, te->idx - 1);
1230 static const struct got_error *
1231 request_packed_blob(uint8_t **outbuf, size_t *size, size_t *hdrlen, int outfd,
1232 struct got_pack *pack, struct got_packidx *packidx, int idx,
1233 struct got_object_id *id)
1235 const struct got_error *err = NULL;
1236 int outfd_child;
1237 int basefd, accumfd; /* temporary files for delta application */
1239 basefd = got_opentempfd();
1240 if (basefd == -1)
1241 return got_error_from_errno("got_opentempfd");
1242 accumfd = got_opentempfd();
1243 if (accumfd == -1)
1244 return got_error_from_errno("got_opentempfd");
1246 outfd_child = dup(outfd);
1247 if (outfd_child == -1)
1248 return got_error_from_errno("dup");
1250 err = got_privsep_send_blob_req(pack->privsep_child->ibuf, -1, id, idx);
1251 if (err)
1252 return err;
1254 err = got_privsep_send_blob_outfd(pack->privsep_child->ibuf,
1255 outfd_child);
1256 if (err) {
1257 close(basefd);
1258 close(accumfd);
1259 return err;
1262 err = got_privsep_send_tmpfd(pack->privsep_child->ibuf,
1263 basefd);
1264 if (err) {
1265 close(accumfd);
1266 return err;
1269 err = got_privsep_send_tmpfd(pack->privsep_child->ibuf,
1270 accumfd);
1271 if (err)
1272 return err;
1274 err = got_privsep_recv_blob(outbuf, size, hdrlen,
1275 pack->privsep_child->ibuf);
1276 if (err)
1277 return err;
1279 if (lseek(outfd, SEEK_SET, 0) == -1)
1280 err = got_error_from_errno("lseek");
1282 return err;
1285 static const struct got_error *
1286 read_packed_blob_privsep(uint8_t **outbuf, size_t *size, size_t *hdrlen,
1287 int outfd, struct got_pack *pack, struct got_packidx *packidx, int idx,
1288 struct got_object_id *id)
1290 const struct got_error *err = NULL;
1292 if (pack->privsep_child == NULL) {
1293 err = start_pack_privsep_child(pack, packidx);
1294 if (err)
1295 return err;
1298 return request_packed_blob(outbuf, size, hdrlen, outfd, pack, packidx,
1299 idx, id);
1302 static const struct got_error *
1303 request_blob(uint8_t **outbuf, size_t *size, size_t *hdrlen, int outfd,
1304 int infd, struct imsgbuf *ibuf)
1306 const struct got_error *err = NULL;
1307 int outfd_child;
1309 outfd_child = dup(outfd);
1310 if (outfd_child == -1)
1311 return got_error_from_errno("dup");
1313 err = got_privsep_send_blob_req(ibuf, infd, NULL, -1);
1314 if (err)
1315 return err;
1317 err = got_privsep_send_blob_outfd(ibuf, outfd_child);
1318 if (err)
1319 return err;
1321 err = got_privsep_recv_blob(outbuf, size, hdrlen, ibuf);
1322 if (err)
1323 return err;
1325 if (lseek(outfd, SEEK_SET, 0) == -1)
1326 return got_error_from_errno("lseek");
1328 return err;
1331 static const struct got_error *
1332 read_blob_privsep(uint8_t **outbuf, size_t *size, size_t *hdrlen,
1333 int outfd, int infd, struct got_repository *repo)
1335 const struct got_error *err;
1336 int imsg_fds[2];
1337 pid_t pid;
1338 struct imsgbuf *ibuf;
1340 if (repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_BLOB].imsg_fd != -1) {
1341 ibuf = repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_BLOB].ibuf;
1342 return request_blob(outbuf, size, hdrlen, outfd, infd, ibuf);
1345 ibuf = calloc(1, sizeof(*ibuf));
1346 if (ibuf == NULL)
1347 return got_error_from_errno("calloc");
1349 if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_fds) == -1) {
1350 err = got_error_from_errno("socketpair");
1351 free(ibuf);
1352 return err;
1355 pid = fork();
1356 if (pid == -1) {
1357 err = got_error_from_errno("fork");
1358 free(ibuf);
1359 return err;
1361 else if (pid == 0) {
1362 got_privsep_exec_child(imsg_fds, GOT_PATH_PROG_READ_BLOB,
1363 repo->path);
1364 /* not reached */
1367 if (close(imsg_fds[1]) == -1) {
1368 err = got_error_from_errno("close");
1369 free(ibuf);
1370 return err;
1372 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_BLOB].imsg_fd =
1373 imsg_fds[0];
1374 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_BLOB].pid = pid;
1375 imsg_init(ibuf, imsg_fds[0]);
1376 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_BLOB].ibuf = ibuf;
1378 return request_blob(outbuf, size, hdrlen, outfd, infd, ibuf);
1381 static const struct got_error *
1382 open_blob(struct got_blob_object **blob, struct got_repository *repo,
1383 struct got_object_id *id, size_t blocksize)
1385 const struct got_error *err = NULL;
1386 struct got_packidx *packidx = NULL;
1387 int idx;
1388 char *path_packfile = NULL;
1389 uint8_t *outbuf;
1390 int outfd;
1391 size_t size, hdrlen;
1392 struct stat sb;
1394 *blob = calloc(1, sizeof(**blob));
1395 if (*blob == NULL)
1396 return got_error_from_errno("calloc");
1398 outfd = got_opentempfd();
1399 if (outfd == -1)
1400 return got_error_from_errno("got_opentempfd");
1402 (*blob)->read_buf = malloc(blocksize);
1403 if ((*blob)->read_buf == NULL) {
1404 err = got_error_from_errno("malloc");
1405 goto done;
1408 err = got_repo_search_packidx(&packidx, &idx, repo, id);
1409 if (err == NULL) {
1410 struct got_pack *pack = NULL;
1412 err = got_packidx_get_packfile_path(&path_packfile,
1413 packidx->path_packidx);
1414 if (err)
1415 goto done;
1417 pack = got_repo_get_cached_pack(repo, path_packfile);
1418 if (pack == NULL) {
1419 err = got_repo_cache_pack(&pack, repo, path_packfile,
1420 packidx);
1421 if (err)
1422 goto done;
1424 err = read_packed_blob_privsep(&outbuf, &size, &hdrlen, outfd,
1425 pack, packidx, idx, id);
1426 } else if (err->code == GOT_ERR_NO_OBJ) {
1427 int infd;
1429 err = got_object_open_loose_fd(&infd, id, repo);
1430 if (err)
1431 goto done;
1432 err = read_blob_privsep(&outbuf, &size, &hdrlen, outfd, infd,
1433 repo);
1435 if (err)
1436 goto done;
1438 if (hdrlen > size) {
1439 err = got_error(GOT_ERR_BAD_OBJ_HDR);
1440 goto done;
1443 if (outbuf) {
1444 if (close(outfd) == -1 && err == NULL)
1445 err = got_error_from_errno("close");
1446 outfd = -1;
1447 (*blob)->f = fmemopen(outbuf, size, "rb");
1448 if ((*blob)->f == NULL) {
1449 err = got_error_from_errno("fmemopen");
1450 free(outbuf);
1451 goto done;
1453 (*blob)->data = outbuf;
1454 } else {
1455 if (fstat(outfd, &sb) == -1) {
1456 err = got_error_from_errno("fstat");
1457 goto done;
1460 if (sb.st_size != size) {
1461 err = got_error(GOT_ERR_PRIVSEP_LEN);
1462 goto done;
1465 (*blob)->f = fdopen(outfd, "rb");
1466 if ((*blob)->f == NULL) {
1467 err = got_error_from_errno("fdopen");
1468 close(outfd);
1469 outfd = -1;
1470 goto done;
1474 (*blob)->hdrlen = hdrlen;
1475 (*blob)->blocksize = blocksize;
1476 memcpy(&(*blob)->id.sha1, id->sha1, SHA1_DIGEST_LENGTH);
1478 done:
1479 free(path_packfile);
1480 if (err) {
1481 if (*blob) {
1482 got_object_blob_close(*blob);
1483 *blob = NULL;
1484 } else if (outfd != -1)
1485 close(outfd);
1487 return err;
1490 const struct got_error *
1491 got_object_open_as_blob(struct got_blob_object **blob,
1492 struct got_repository *repo, struct got_object_id *id,
1493 size_t blocksize)
1495 return open_blob(blob, repo, id, blocksize);
1498 const struct got_error *
1499 got_object_blob_open(struct got_blob_object **blob,
1500 struct got_repository *repo, struct got_object *obj, size_t blocksize)
1502 return open_blob(blob, repo, got_object_get_id(obj), blocksize);
1505 const struct got_error *
1506 got_object_blob_close(struct got_blob_object *blob)
1508 const struct got_error *err = NULL;
1509 free(blob->read_buf);
1510 if (blob->f && fclose(blob->f) == EOF)
1511 err = got_error_from_errno("fclose");
1512 free(blob->data);
1513 free(blob);
1514 return err;
1517 void
1518 got_object_blob_rewind(struct got_blob_object *blob)
1520 if (blob->f)
1521 rewind(blob->f);
1524 char *
1525 got_object_blob_id_str(struct got_blob_object *blob, char *buf, size_t size)
1527 return got_sha1_digest_to_str(blob->id.sha1, buf, size);
1530 size_t
1531 got_object_blob_get_hdrlen(struct got_blob_object *blob)
1533 return blob->hdrlen;
1536 const uint8_t *
1537 got_object_blob_get_read_buf(struct got_blob_object *blob)
1539 return blob->read_buf;
1542 const struct got_error *
1543 got_object_blob_read_block(size_t *outlenp, struct got_blob_object *blob)
1545 size_t n;
1547 n = fread(blob->read_buf, 1, blob->blocksize, blob->f);
1548 if (n == 0 && ferror(blob->f))
1549 return got_ferror(blob->f, GOT_ERR_IO);
1550 *outlenp = n;
1551 return NULL;
1554 const struct got_error *
1555 got_object_blob_dump_to_file(off_t *filesize, int *nlines,
1556 off_t **line_offsets, FILE *outfile, struct got_blob_object *blob)
1558 const struct got_error *err = NULL;
1559 size_t n, len, hdrlen;
1560 const uint8_t *buf;
1561 int i;
1562 const int alloc_chunksz = 512;
1563 size_t nalloc = 0;
1564 off_t off = 0, total_len = 0;
1566 if (line_offsets)
1567 *line_offsets = NULL;
1568 if (filesize)
1569 *filesize = 0;
1570 if (nlines)
1571 *nlines = 0;
1573 hdrlen = got_object_blob_get_hdrlen(blob);
1574 do {
1575 err = got_object_blob_read_block(&len, blob);
1576 if (err)
1577 return err;
1578 if (len == 0)
1579 break;
1580 buf = got_object_blob_get_read_buf(blob);
1581 i = hdrlen;
1582 if (nlines) {
1583 if (line_offsets && *line_offsets == NULL) {
1584 /* Have some data but perhaps no '\n'. */
1585 *nlines = 1;
1586 nalloc = alloc_chunksz;
1587 *line_offsets = calloc(nalloc,
1588 sizeof(**line_offsets));
1589 if (*line_offsets == NULL)
1590 return got_error_from_errno("calloc");
1592 /* Skip forward over end of first line. */
1593 while (i < len) {
1594 if (buf[i] == '\n')
1595 break;
1596 i++;
1599 /* Scan '\n' offsets in remaining chunk of data. */
1600 while (i < len) {
1601 if (buf[i] != '\n') {
1602 i++;
1603 continue;
1605 (*nlines)++;
1606 if (line_offsets && nalloc < *nlines) {
1607 size_t n = *nlines + alloc_chunksz;
1608 off_t *o = recallocarray(*line_offsets,
1609 nalloc, n, sizeof(**line_offsets));
1610 if (o == NULL) {
1611 free(*line_offsets);
1612 *line_offsets = NULL;
1613 return got_error_from_errno(
1614 "recallocarray");
1616 *line_offsets = o;
1617 nalloc = n;
1619 if (line_offsets) {
1620 off = total_len + i - hdrlen + 1;
1621 (*line_offsets)[*nlines - 1] = off;
1623 i++;
1626 /* Skip blob object header first time around. */
1627 n = fwrite(buf + hdrlen, 1, len - hdrlen, outfile);
1628 if (n != len - hdrlen)
1629 return got_ferror(outfile, GOT_ERR_IO);
1630 total_len += len - hdrlen;
1631 hdrlen = 0;
1632 } while (len != 0);
1634 if (fflush(outfile) != 0)
1635 return got_error_from_errno("fflush");
1636 rewind(outfile);
1638 if (filesize)
1639 *filesize = total_len;
1641 return NULL;
1644 static const struct got_error *
1645 request_packed_tag(struct got_tag_object **tag, struct got_pack *pack,
1646 int pack_idx, struct got_object_id *id)
1648 const struct got_error *err = NULL;
1650 err = got_privsep_send_tag_req(pack->privsep_child->ibuf, -1, id,
1651 pack_idx);
1652 if (err)
1653 return err;
1655 return got_privsep_recv_tag(tag, pack->privsep_child->ibuf);
1658 static const struct got_error *
1659 read_packed_tag_privsep(struct got_tag_object **tag,
1660 struct got_pack *pack, struct got_packidx *packidx, int idx,
1661 struct got_object_id *id)
1663 const struct got_error *err = NULL;
1665 if (pack->privsep_child)
1666 return request_packed_tag(tag, pack, idx, id);
1668 err = start_pack_privsep_child(pack, packidx);
1669 if (err)
1670 return err;
1672 return request_packed_tag(tag, pack, idx, id);
1675 static const struct got_error *
1676 request_tag(struct got_tag_object **tag, struct got_repository *repo,
1677 int fd)
1679 const struct got_error *err = NULL;
1680 struct imsgbuf *ibuf;
1682 ibuf = repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TAG].ibuf;
1684 err = got_privsep_send_tag_req(ibuf, fd, NULL, -1);
1685 if (err)
1686 return err;
1688 return got_privsep_recv_tag(tag, ibuf);
1691 static const struct got_error *
1692 read_tag_privsep(struct got_tag_object **tag, int obj_fd,
1693 struct got_repository *repo)
1695 const struct got_error *err;
1696 int imsg_fds[2];
1697 pid_t pid;
1698 struct imsgbuf *ibuf;
1700 if (repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TAG].imsg_fd != -1)
1701 return request_tag(tag, repo, obj_fd);
1703 ibuf = calloc(1, sizeof(*ibuf));
1704 if (ibuf == NULL)
1705 return got_error_from_errno("calloc");
1707 if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_fds) == -1) {
1708 err = got_error_from_errno("socketpair");
1709 free(ibuf);
1710 return err;
1713 pid = fork();
1714 if (pid == -1) {
1715 err = got_error_from_errno("fork");
1716 free(ibuf);
1717 return err;
1719 else if (pid == 0) {
1720 got_privsep_exec_child(imsg_fds, GOT_PATH_PROG_READ_TAG,
1721 repo->path);
1722 /* not reached */
1725 if (close(imsg_fds[1]) == -1) {
1726 err = got_error_from_errno("close");
1727 free(ibuf);
1728 return err;
1730 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TAG].imsg_fd =
1731 imsg_fds[0];
1732 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TAG].pid = pid;
1733 imsg_init(ibuf, imsg_fds[0]);
1734 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TAG].ibuf = ibuf;
1736 return request_tag(tag, repo, obj_fd);
1739 static const struct got_error *
1740 open_tag(struct got_tag_object **tag, struct got_repository *repo,
1741 struct got_object_id *id, int check_cache)
1743 const struct got_error *err = NULL;
1744 struct got_packidx *packidx = NULL;
1745 int idx;
1746 char *path_packfile = NULL;
1747 struct got_object *obj = NULL;
1748 int obj_type = GOT_OBJ_TYPE_ANY;
1750 if (check_cache) {
1751 *tag = got_repo_get_cached_tag(repo, id);
1752 if (*tag != NULL) {
1753 (*tag)->refcnt++;
1754 return NULL;
1756 } else
1757 *tag = NULL;
1759 err = got_repo_search_packidx(&packidx, &idx, repo, id);
1760 if (err == NULL) {
1761 struct got_pack *pack = NULL;
1763 err = got_packidx_get_packfile_path(&path_packfile,
1764 packidx->path_packidx);
1765 if (err)
1766 return err;
1768 pack = got_repo_get_cached_pack(repo, path_packfile);
1769 if (pack == NULL) {
1770 err = got_repo_cache_pack(&pack, repo, path_packfile,
1771 packidx);
1772 if (err)
1773 goto done;
1776 /* Beware of "lightweight" tags: Check object type first. */
1777 err = read_packed_object_privsep(&obj, repo, pack, packidx,
1778 idx, id);
1779 if (err)
1780 goto done;
1781 obj_type = obj->type;
1782 got_object_close(obj);
1783 if (obj_type != GOT_OBJ_TYPE_TAG) {
1784 err = got_error(GOT_ERR_OBJ_TYPE);
1785 goto done;
1787 err = read_packed_tag_privsep(tag, pack, packidx, idx, id);
1788 } else if (err->code == GOT_ERR_NO_OBJ) {
1789 int fd;
1791 err = got_object_open_loose_fd(&fd, id, repo);
1792 if (err)
1793 return err;
1794 err = got_object_read_header_privsep(&obj, repo, fd);
1795 if (err)
1796 return err;
1797 obj_type = obj->type;
1798 got_object_close(obj);
1799 if (obj_type != GOT_OBJ_TYPE_TAG)
1800 return got_error(GOT_ERR_OBJ_TYPE);
1802 err = got_object_open_loose_fd(&fd, id, repo);
1803 if (err)
1804 return err;
1805 err = read_tag_privsep(tag, fd, repo);
1808 if (err == NULL) {
1809 (*tag)->refcnt++;
1810 err = got_repo_cache_tag(repo, id, *tag);
1812 done:
1813 free(path_packfile);
1814 return err;
1817 const struct got_error *
1818 got_object_open_as_tag(struct got_tag_object **tag,
1819 struct got_repository *repo, struct got_object_id *id)
1821 *tag = got_repo_get_cached_tag(repo, id);
1822 if (*tag != NULL) {
1823 (*tag)->refcnt++;
1824 return NULL;
1827 return open_tag(tag, repo, id, 0);
1830 const struct got_error *
1831 got_object_tag_open(struct got_tag_object **tag,
1832 struct got_repository *repo, struct got_object *obj)
1834 return open_tag(tag, repo, got_object_get_id(obj), 1);
1837 const char *
1838 got_object_tag_get_name(struct got_tag_object *tag)
1840 return tag->tag;
1843 int
1844 got_object_tag_get_object_type(struct got_tag_object *tag)
1846 return tag->obj_type;
1849 struct got_object_id *
1850 got_object_tag_get_object_id(struct got_tag_object *tag)
1852 return &tag->id;
1855 time_t
1856 got_object_tag_get_tagger_time(struct got_tag_object *tag)
1858 return tag->tagger_time;
1861 time_t
1862 got_object_tag_get_tagger_gmtoff(struct got_tag_object *tag)
1864 return tag->tagger_gmtoff;
1867 const char *
1868 got_object_tag_get_tagger(struct got_tag_object *tag)
1870 return tag->tagger;
1873 const char *
1874 got_object_tag_get_message(struct got_tag_object *tag)
1876 return tag->tagmsg;
1879 static struct got_tree_entry *
1880 find_entry_by_name(struct got_tree_object *tree, const char *name, size_t len)
1882 int i;
1884 /* Note that tree entries are sorted in strncmp() order. */
1885 for (i = 0; i < tree->nentries; i++) {
1886 struct got_tree_entry *te = &tree->entries[i];
1887 int cmp = strncmp(te->name, name, len);
1888 if (cmp < 0)
1889 continue;
1890 if (cmp > 0)
1891 break;
1892 if (te->name[len] == '\0')
1893 return te;
1895 return NULL;
1898 struct got_tree_entry *
1899 got_object_tree_find_entry(struct got_tree_object *tree, const char *name)
1901 return find_entry_by_name(tree, name, strlen(name));
1904 const struct got_error *
1905 got_object_id_by_path(struct got_object_id **id, struct got_repository *repo,
1906 struct got_object_id *commit_id, const char *path)
1908 const struct got_error *err = NULL;
1909 struct got_commit_object *commit = NULL;
1910 struct got_tree_object *tree = NULL;
1911 struct got_tree_entry *te = NULL;
1912 const char *seg, *s;
1913 size_t seglen;
1915 *id = NULL;
1917 err = got_object_open_as_commit(&commit, repo, commit_id);
1918 if (err)
1919 goto done;
1921 /* Handle opening of root of commit's tree. */
1922 if (got_path_is_root_dir(path)) {
1923 *id = got_object_id_dup(commit->tree_id);
1924 if (*id == NULL)
1925 err = got_error_from_errno("got_object_id_dup");
1926 goto done;
1929 err = got_object_open_as_tree(&tree, repo, commit->tree_id);
1930 if (err)
1931 goto done;
1933 s = path;
1934 while (s[0] == '/')
1935 s++;
1936 seg = s;
1937 seglen = 0;
1938 while (*s) {
1939 struct got_tree_object *next_tree;
1941 if (*s != '/') {
1942 s++;
1943 seglen++;
1944 if (*s)
1945 continue;
1948 te = find_entry_by_name(tree, seg, seglen);
1949 if (te == NULL) {
1950 err = got_error_path(path, GOT_ERR_NO_TREE_ENTRY);
1951 goto done;
1954 if (*s == '\0')
1955 break;
1957 seg = s + 1;
1958 seglen = 0;
1959 s++;
1960 if (*s) {
1961 err = got_object_open_as_tree(&next_tree, repo,
1962 &te->id);
1963 te = NULL;
1964 if (err)
1965 goto done;
1966 got_object_tree_close(tree);
1967 tree = next_tree;
1971 if (te) {
1972 *id = got_object_id_dup(&te->id);
1973 if (*id == NULL)
1974 return got_error_from_errno("got_object_id_dup");
1975 } else
1976 err = got_error_path(path, GOT_ERR_NO_TREE_ENTRY);
1977 done:
1978 if (commit)
1979 got_object_commit_close(commit);
1980 if (tree)
1981 got_object_tree_close(tree);
1982 return err;
1986 * Normalize file mode bits to avoid false positive tree entry differences
1987 * in case tree entries have unexpected mode bits set.
1989 static mode_t
1990 normalize_mode_for_comparison(mode_t mode)
1993 * For directories, the only relevant bit is the IFDIR bit.
1994 * This allows us to detect paths changing from a directory
1995 * to a file and vice versa.
1997 if (S_ISDIR(mode))
1998 return mode & S_IFDIR;
2001 * For symlinks, the only relevant bit is the IFLNK bit.
2002 * This allows us to detect paths changing from a symlinks
2003 * to a file or directory and vice versa.
2005 if (S_ISLNK(mode))
2006 return mode & S_IFLNK;
2008 /* For files, the only change we care about is the executable bit. */
2009 return mode & S_IXUSR;
2012 const struct got_error *
2013 got_object_tree_path_changed(int *changed,
2014 struct got_tree_object *tree01, struct got_tree_object *tree02,
2015 const char *path, struct got_repository *repo)
2017 const struct got_error *err = NULL;
2018 struct got_tree_object *tree1 = NULL, *tree2 = NULL;
2019 struct got_tree_entry *te1 = NULL, *te2 = NULL;
2020 const char *seg, *s;
2021 size_t seglen;
2023 *changed = 0;
2025 /* We not do support comparing the root path. */
2026 if (got_path_is_root_dir(path))
2027 return got_error_path(path, GOT_ERR_BAD_PATH);
2029 tree1 = tree01;
2030 tree2 = tree02;
2031 s = path;
2032 while (*s == '/')
2033 s++;
2034 seg = s;
2035 seglen = 0;
2036 while (*s) {
2037 struct got_tree_object *next_tree1, *next_tree2;
2038 mode_t mode1, mode2;
2040 if (*s != '/') {
2041 s++;
2042 seglen++;
2043 if (*s)
2044 continue;
2047 te1 = find_entry_by_name(tree1, seg, seglen);
2048 if (te1 == NULL) {
2049 err = got_error(GOT_ERR_NO_OBJ);
2050 goto done;
2053 if (tree2)
2054 te2 = find_entry_by_name(tree2, seg, seglen);
2056 if (te2) {
2057 mode1 = normalize_mode_for_comparison(te1->mode);
2058 mode2 = normalize_mode_for_comparison(te2->mode);
2059 if (mode1 != mode2) {
2060 *changed = 1;
2061 goto done;
2064 if (got_object_id_cmp(&te1->id, &te2->id) == 0) {
2065 *changed = 0;
2066 goto done;
2070 if (*s == '\0') { /* final path element */
2071 *changed = 1;
2072 goto done;
2075 seg = s + 1;
2076 s++;
2077 seglen = 0;
2078 if (*s) {
2079 err = got_object_open_as_tree(&next_tree1, repo,
2080 &te1->id);
2081 te1 = NULL;
2082 if (err)
2083 goto done;
2084 if (tree1 != tree01)
2085 got_object_tree_close(tree1);
2086 tree1 = next_tree1;
2088 if (te2) {
2089 err = got_object_open_as_tree(&next_tree2, repo,
2090 &te2->id);
2091 te2 = NULL;
2092 if (err)
2093 goto done;
2094 if (tree2 != tree02)
2095 got_object_tree_close(tree2);
2096 tree2 = next_tree2;
2097 } else if (tree2) {
2098 if (tree2 != tree02)
2099 got_object_tree_close(tree2);
2100 tree2 = NULL;
2104 done:
2105 if (tree1 && tree1 != tree01)
2106 got_object_tree_close(tree1);
2107 if (tree2 && tree2 != tree02)
2108 got_object_tree_close(tree2);
2109 return err;
2112 const struct got_error *
2113 got_object_tree_entry_dup(struct got_tree_entry **new_te,
2114 struct got_tree_entry *te)
2116 const struct got_error *err = NULL;
2118 *new_te = calloc(1, sizeof(**new_te));
2119 if (*new_te == NULL)
2120 return got_error_from_errno("calloc");
2122 (*new_te)->mode = te->mode;
2123 memcpy((*new_te)->name, te->name, sizeof((*new_te)->name));
2124 memcpy(&(*new_te)->id, &te->id, sizeof((*new_te)->id));
2125 return err;
2128 int
2129 got_object_tree_entry_is_submodule(struct got_tree_entry *te)
2131 return (te->mode & S_IFMT) == (S_IFDIR | S_IFLNK);
2134 int
2135 got_object_tree_entry_is_symlink(struct got_tree_entry *te)
2137 /* S_IFDIR check avoids confusing symlinks with submodules. */
2138 return ((te->mode & (S_IFDIR | S_IFLNK)) == S_IFLNK);
2141 static const struct got_error *
2142 resolve_symlink(char **link_target, const char *path,
2143 struct got_object_id *commit_id, struct got_repository *repo)
2145 const struct got_error *err = NULL;
2146 char buf[PATH_MAX];
2147 char *name, *parent_path = NULL;
2148 struct got_object_id *tree_obj_id = NULL;
2149 struct got_tree_object *tree = NULL;
2150 struct got_tree_entry *te = NULL;
2152 *link_target = NULL;
2154 if (strlcpy(buf, path, sizeof(buf)) >= sizeof(buf))
2155 return got_error(GOT_ERR_NO_SPACE);
2157 name = basename(buf);
2158 if (name == NULL)
2159 return got_error_from_errno2("basename", path);
2161 err = got_path_dirname(&parent_path, path);
2162 if (err)
2163 return err;
2165 err = got_object_id_by_path(&tree_obj_id, repo, commit_id,
2166 parent_path);
2167 if (err) {
2168 if (err->code == GOT_ERR_NO_TREE_ENTRY) {
2169 /* Display the complete path in error message. */
2170 err = got_error_path(path, err->code);
2172 goto done;
2175 err = got_object_open_as_tree(&tree, repo, tree_obj_id);
2176 if (err)
2177 goto done;
2179 te = got_object_tree_find_entry(tree, name);
2180 if (te == NULL) {
2181 err = got_error_path(path, GOT_ERR_NO_TREE_ENTRY);
2182 goto done;
2185 if (got_object_tree_entry_is_symlink(te)) {
2186 err = got_tree_entry_get_symlink_target(link_target, te, repo);
2187 if (err)
2188 goto done;
2189 if (!got_path_is_absolute(*link_target)) {
2190 char *abspath;
2191 if (asprintf(&abspath, "%s/%s", parent_path,
2192 *link_target) == -1) {
2193 err = got_error_from_errno("asprintf");
2194 goto done;
2196 free(*link_target);
2197 *link_target = malloc(PATH_MAX);
2198 if (*link_target == NULL) {
2199 err = got_error_from_errno("malloc");
2200 goto done;
2202 err = got_canonpath(abspath, *link_target, PATH_MAX);
2203 free(abspath);
2204 if (err)
2205 goto done;
2208 done:
2209 free(tree_obj_id);
2210 if (tree)
2211 got_object_tree_close(tree);
2212 if (err) {
2213 free(*link_target);
2214 *link_target = NULL;
2216 return err;
2219 const struct got_error *
2220 got_object_resolve_symlinks(char **link_target, const char *path,
2221 struct got_object_id *commit_id, struct got_repository *repo)
2223 const struct got_error *err = NULL;
2224 char *next_target = NULL;
2225 int max_recursion = 40; /* matches Git */
2227 *link_target = NULL;
2229 do {
2230 err = resolve_symlink(&next_target,
2231 *link_target ? *link_target : path, commit_id, repo);
2232 if (err)
2233 break;
2234 if (next_target) {
2235 free(*link_target);
2236 if (--max_recursion == 0) {
2237 err = got_error_path(path, GOT_ERR_RECURSION);
2238 *link_target = NULL;
2239 break;
2241 *link_target = next_target;
2243 } while (next_target);
2245 return err;
2248 const struct got_error *
2249 got_traverse_packed_commits(struct got_object_id_queue *traversed_commits,
2250 struct got_object_id *commit_id, const char *path,
2251 struct got_repository *repo)
2253 const struct got_error *err = NULL;
2254 struct got_pack *pack = NULL;
2255 struct got_packidx *packidx = NULL;
2256 char *path_packfile = NULL;
2257 struct got_commit_object *changed_commit = NULL;
2258 struct got_object_id *changed_commit_id = NULL;
2259 int idx;
2261 err = got_repo_search_packidx(&packidx, &idx, repo, commit_id);
2262 if (err) {
2263 if (err->code != GOT_ERR_NO_OBJ)
2264 return err;
2265 return NULL;
2268 err = got_packidx_get_packfile_path(&path_packfile,
2269 packidx->path_packidx);
2270 if (err)
2271 return err;
2273 pack = got_repo_get_cached_pack(repo, path_packfile);
2274 if (pack == NULL) {
2275 err = got_repo_cache_pack(&pack, repo, path_packfile, packidx);
2276 if (err)
2277 goto done;
2280 if (pack->privsep_child == NULL) {
2281 err = start_pack_privsep_child(pack, packidx);
2282 if (err)
2283 goto done;
2286 err = got_privsep_send_commit_traversal_request(
2287 pack->privsep_child->ibuf, commit_id, idx, path);
2288 if (err)
2289 goto done;
2291 err = got_privsep_recv_traversed_commits(&changed_commit,
2292 &changed_commit_id, traversed_commits, pack->privsep_child->ibuf);
2293 if (err)
2294 goto done;
2296 if (changed_commit) {
2298 * Cache the commit in which the path was changed.
2299 * This commit might be opened again soon.
2301 changed_commit->refcnt++;
2302 err = got_repo_cache_commit(repo, changed_commit_id,
2303 changed_commit);
2304 got_object_commit_close(changed_commit);
2306 done:
2307 free(path_packfile);
2308 free(changed_commit_id);
2309 return err;