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/syslimits.h>
24 #include <sys/resource.h>
26 #include <errno.h>
27 #include <fcntl.h>
28 #include <stdio.h>
29 #include <stdlib.h>
30 #include <string.h>
31 #include <stdint.h>
32 #include <sha1.h>
33 #include <zlib.h>
34 #include <ctype.h>
35 #include <limits.h>
36 #include <imsg.h>
37 #include <time.h>
39 #include "got_error.h"
40 #include "got_object.h"
41 #include "got_repository.h"
42 #include "got_opentemp.h"
43 #include "got_path.h"
45 #include "got_lib_sha1.h"
46 #include "got_lib_delta.h"
47 #include "got_lib_inflate.h"
48 #include "got_lib_object.h"
49 #include "got_lib_privsep.h"
50 #include "got_lib_object_idcache.h"
51 #include "got_lib_object_cache.h"
52 #include "got_lib_object_parse.h"
53 #include "got_lib_pack.h"
54 #include "got_lib_repository.h"
56 #ifndef MIN
57 #define MIN(_a,_b) ((_a) < (_b) ? (_a) : (_b))
58 #endif
60 struct got_object_id *
61 got_object_id_dup(struct got_object_id *id1)
62 {
63 struct got_object_id *id2;
65 id2 = malloc(sizeof(*id2));
66 if (id2 == NULL)
67 return NULL;
68 memcpy(id2, id1, sizeof(*id2));
69 return id2;
70 }
72 struct got_object_id *
73 got_object_get_id(struct got_object *obj)
74 {
75 return &obj->id;
76 }
78 const struct got_error *
79 got_object_get_id_str(char **outbuf, struct got_object *obj)
80 {
81 return got_object_id_str(outbuf, &obj->id);
82 }
84 const struct got_error *
85 got_object_get_type(int *type, struct got_repository *repo,
86 struct got_object_id *id)
87 {
88 const struct got_error *err = NULL;
89 struct got_object *obj;
91 err = got_object_open(&obj, repo, id);
92 if (err)
93 return err;
95 switch (obj->type) {
96 case GOT_OBJ_TYPE_COMMIT:
97 case GOT_OBJ_TYPE_TREE:
98 case GOT_OBJ_TYPE_BLOB:
99 case GOT_OBJ_TYPE_TAG:
100 *type = obj->type;
101 break;
102 default:
103 err = got_error(GOT_ERR_OBJ_TYPE);
104 break;
107 got_object_close(obj);
108 return err;
111 const struct got_error *
112 got_object_get_path(char **path, struct got_object_id *id,
113 struct got_repository *repo)
115 const struct got_error *err = NULL;
116 char *hex = NULL;
117 char *path_objects = got_repo_get_path_objects(repo);
119 *path = NULL;
121 if (path_objects == NULL)
122 return got_error_from_errno("got_repo_get_path_objects");
124 err = got_object_id_str(&hex, id);
125 if (err)
126 goto done;
128 if (asprintf(path, "%s/%.2x/%s", path_objects,
129 id->sha1[0], hex + 2) == -1)
130 err = got_error_from_errno("asprintf");
132 done:
133 free(hex);
134 free(path_objects);
135 return err;
138 static const struct got_error *
139 open_loose_object(int *fd, struct got_object_id *id,
140 struct got_repository *repo)
142 const struct got_error *err = NULL;
143 char *path;
145 err = got_object_get_path(&path, id, repo);
146 if (err)
147 return err;
148 *fd = open(path, O_RDONLY | O_NOFOLLOW);
149 if (*fd == -1) {
150 err = got_error_from_errno2("open", path);
151 goto done;
153 done:
154 free(path);
155 return err;
158 static const struct got_error *
159 get_packfile_path(char **path_packfile, struct got_packidx *packidx)
161 size_t size;
163 /* Packfile path contains ".pack" instead of ".idx", so add one byte. */
164 size = strlen(packidx->path_packidx) + 2;
165 if (size < GOT_PACKFILE_NAMELEN + 1)
166 return got_error(GOT_ERR_BAD_PATH);
168 *path_packfile = malloc(size);
169 if (*path_packfile == NULL)
170 return got_error_from_errno("malloc");
172 /* Copy up to and excluding ".idx". */
173 if (strlcpy(*path_packfile, packidx->path_packidx,
174 size - strlen(GOT_PACKIDX_SUFFIX) - 1) >= size)
175 return got_error(GOT_ERR_NO_SPACE);
177 if (strlcat(*path_packfile, GOT_PACKFILE_SUFFIX, size) >= size)
178 return got_error(GOT_ERR_NO_SPACE);
180 return NULL;
183 static void
184 exec_privsep_child(int imsg_fds[2], const char *path, const char *repo_path)
186 if (close(imsg_fds[0]) != 0) {
187 fprintf(stderr, "%s: %s\n", getprogname(), strerror(errno));
188 _exit(1);
191 if (dup2(imsg_fds[1], GOT_IMSG_FD_CHILD) == -1) {
192 fprintf(stderr, "%s: %s\n", getprogname(), strerror(errno));
193 _exit(1);
195 if (closefrom(GOT_IMSG_FD_CHILD + 1) == -1) {
196 fprintf(stderr, "%s: %s\n", getprogname(), strerror(errno));
197 _exit(1);
200 if (execl(path, path, repo_path, (char *)NULL) == -1) {
201 fprintf(stderr, "%s: %s: %s\n", getprogname(), path,
202 strerror(errno));
203 _exit(1);
207 static const struct got_error *
208 request_packed_object(struct got_object **obj, struct got_pack *pack, int idx,
209 struct got_object_id *id)
211 const struct got_error *err = NULL;
212 struct imsgbuf *ibuf = pack->privsep_child->ibuf;
214 err = got_privsep_send_packed_obj_req(ibuf, idx, id);
215 if (err)
216 return err;
218 err = got_privsep_recv_obj(obj, ibuf);
219 if (err)
220 return err;
222 (*obj)->path_packfile = strdup(pack->path_packfile);
223 if ((*obj)->path_packfile == NULL) {
224 err = got_error_from_errno("strdup");
225 return err;
227 memcpy(&(*obj)->id, id, sizeof((*obj)->id));
229 return NULL;
232 static void
233 set_max_datasize(void)
235 struct rlimit rl;
237 if (getrlimit(RLIMIT_DATA, &rl) != 0)
238 return;
240 rl.rlim_cur = rl.rlim_max;
241 setrlimit(RLIMIT_DATA, &rl);
244 static const struct got_error *
245 start_pack_privsep_child(struct got_pack *pack, struct got_packidx *packidx)
247 const struct got_error *err = NULL;
248 int imsg_fds[2];
249 pid_t pid;
250 struct imsgbuf *ibuf;
252 ibuf = calloc(1, sizeof(*ibuf));
253 if (ibuf == NULL)
254 return got_error_from_errno("calloc");
256 pack->privsep_child = calloc(1, sizeof(*pack->privsep_child));
257 if (pack->privsep_child == NULL) {
258 err = got_error_from_errno("calloc");
259 free(ibuf);
260 return err;
263 if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_fds) == -1) {
264 err = got_error_from_errno("socketpair");
265 goto done;
268 pid = fork();
269 if (pid == -1) {
270 err = got_error_from_errno("fork");
271 goto done;
272 } else if (pid == 0) {
273 set_max_datasize();
274 exec_privsep_child(imsg_fds, GOT_PATH_PROG_READ_PACK,
275 pack->path_packfile);
276 /* not reached */
279 if (close(imsg_fds[1]) != 0)
280 return got_error_from_errno("close");
281 pack->privsep_child->imsg_fd = imsg_fds[0];
282 pack->privsep_child->pid = pid;
283 imsg_init(ibuf, imsg_fds[0]);
284 pack->privsep_child->ibuf = ibuf;
286 err = got_privsep_init_pack_child(ibuf, pack, packidx);
287 if (err) {
288 const struct got_error *child_err;
289 err = got_privsep_send_stop(pack->privsep_child->imsg_fd);
290 child_err = got_privsep_wait_for_child(
291 pack->privsep_child->pid);
292 if (child_err && err == NULL)
293 err = child_err;
295 done:
296 if (err) {
297 free(ibuf);
298 free(pack->privsep_child);
299 pack->privsep_child = NULL;
301 return err;
304 static const struct got_error *
305 read_packed_object_privsep(struct got_object **obj,
306 struct got_repository *repo, struct got_pack *pack,
307 struct got_packidx *packidx, int idx, struct got_object_id *id)
309 const struct got_error *err = NULL;
311 if (pack->privsep_child)
312 return request_packed_object(obj, pack, idx, id);
314 err = start_pack_privsep_child(pack, packidx);
315 if (err)
316 return err;
318 return request_packed_object(obj, pack, idx, id);
322 static const struct got_error *
323 open_packed_object(struct got_object **obj, struct got_object_id *id,
324 struct got_repository *repo)
326 const struct got_error *err = NULL;
327 struct got_pack *pack = NULL;
328 struct got_packidx *packidx = NULL;
329 int idx;
330 char *path_packfile;
332 err = got_repo_search_packidx(&packidx, &idx, repo, id);
333 if (err)
334 return err;
336 err = get_packfile_path(&path_packfile, packidx);
337 if (err)
338 return err;
340 pack = got_repo_get_cached_pack(repo, path_packfile);
341 if (pack == NULL) {
342 err = got_repo_cache_pack(&pack, repo, path_packfile, packidx);
343 if (err)
344 goto done;
347 err = read_packed_object_privsep(obj, repo, pack, packidx, idx, id);
348 if (err)
349 goto done;
351 err = got_repo_cache_pack(NULL, repo, (*obj)->path_packfile, packidx);
352 done:
353 free(path_packfile);
354 return err;
357 static const struct got_error *
358 request_object(struct got_object **obj, struct got_repository *repo, int fd)
360 const struct got_error *err = NULL;
361 struct imsgbuf *ibuf;
363 ibuf = repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_OBJECT].ibuf;
365 err = got_privsep_send_obj_req(ibuf, fd);
366 if (err)
367 return err;
369 return got_privsep_recv_obj(obj, ibuf);
372 static const struct got_error *
373 read_object_header_privsep(struct got_object **obj, struct got_repository *repo,
374 int obj_fd)
376 int imsg_fds[2];
377 pid_t pid;
378 struct imsgbuf *ibuf;
380 if (repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_OBJECT].imsg_fd != -1)
381 return request_object(obj, repo, obj_fd);
383 ibuf = calloc(1, sizeof(*ibuf));
384 if (ibuf == NULL)
385 return got_error_from_errno("calloc");
387 if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_fds) == -1)
388 return got_error_from_errno("socketpair");
390 pid = fork();
391 if (pid == -1)
392 return got_error_from_errno("fork");
393 else if (pid == 0) {
394 exec_privsep_child(imsg_fds, GOT_PATH_PROG_READ_OBJECT,
395 repo->path);
396 /* not reached */
399 if (close(imsg_fds[1]) != 0)
400 return got_error_from_errno("close");
401 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_OBJECT].imsg_fd =
402 imsg_fds[0];
403 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_OBJECT].pid = pid;
404 imsg_init(ibuf, imsg_fds[0]);
405 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_OBJECT].ibuf = ibuf;
407 return request_object(obj, repo, obj_fd);
411 const struct got_error *
412 got_object_open(struct got_object **obj, struct got_repository *repo,
413 struct got_object_id *id)
415 const struct got_error *err = NULL;
416 char *path;
417 int fd;
419 *obj = got_repo_get_cached_object(repo, id);
420 if (*obj != NULL) {
421 (*obj)->refcnt++;
422 return NULL;
425 err = open_packed_object(obj, id, repo);
426 if (err && err->code != GOT_ERR_NO_OBJ)
427 return err;
428 if (*obj) {
429 (*obj)->refcnt++;
430 return got_repo_cache_object(repo, id, *obj);
433 err = got_object_get_path(&path, id, repo);
434 if (err)
435 return err;
437 fd = open(path, O_RDONLY | O_NOFOLLOW);
438 if (fd == -1) {
439 if (errno == ENOENT)
440 err = got_error_no_obj(id);
441 else
442 err = got_error_from_errno2("open", path);
443 goto done;
444 } else {
445 err = read_object_header_privsep(obj, repo, fd);
446 if (err)
447 goto done;
448 memcpy((*obj)->id.sha1, id->sha1, SHA1_DIGEST_LENGTH);
451 (*obj)->refcnt++;
452 err = got_repo_cache_object(repo, id, *obj);
453 done:
454 free(path);
455 return err;
459 const struct got_error *
460 got_object_open_by_id_str(struct got_object **obj, struct got_repository *repo,
461 const char *id_str)
463 struct got_object_id id;
465 if (!got_parse_sha1_digest(id.sha1, id_str))
466 return got_error(GOT_ERR_BAD_OBJ_ID_STR);
468 return got_object_open(obj, repo, &id);
471 const struct got_error *
472 got_object_resolve_id_str(struct got_object_id **id,
473 struct got_repository *repo, const char *id_str)
475 const struct got_error *err = NULL;
476 struct got_object *obj;
478 err = got_object_open_by_id_str(&obj, repo, id_str);
479 if (err)
480 return err;
482 *id = got_object_id_dup(got_object_get_id(obj));
483 got_object_close(obj);
484 if (*id == NULL)
485 return got_error_from_errno("got_object_id_dup");
487 return NULL;
490 static const struct got_error *
491 request_packed_commit(struct got_commit_object **commit, struct got_pack *pack,
492 int pack_idx, struct got_object_id *id)
494 const struct got_error *err = NULL;
496 err = got_privsep_send_commit_req(pack->privsep_child->ibuf, -1, id,
497 pack_idx);
498 if (err)
499 return err;
501 return got_privsep_recv_commit(commit, pack->privsep_child->ibuf);
504 static const struct got_error *
505 read_packed_commit_privsep(struct got_commit_object **commit,
506 struct got_pack *pack, struct got_packidx *packidx, int idx,
507 struct got_object_id *id)
509 const struct got_error *err = NULL;
511 if (pack->privsep_child)
512 return request_packed_commit(commit, pack, idx, id);
514 err = start_pack_privsep_child(pack, packidx);
515 if (err)
516 return err;
518 return request_packed_commit(commit, pack, idx, id);
521 static const struct got_error *
522 request_commit(struct got_commit_object **commit, struct got_repository *repo,
523 int fd)
525 const struct got_error *err = NULL;
526 struct imsgbuf *ibuf;
528 ibuf = repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_COMMIT].ibuf;
530 err = got_privsep_send_commit_req(ibuf, fd, NULL, -1);
531 if (err)
532 return err;
534 return got_privsep_recv_commit(commit, ibuf);
537 static const struct got_error *
538 read_commit_privsep(struct got_commit_object **commit, int obj_fd,
539 struct got_repository *repo)
541 int imsg_fds[2];
542 pid_t pid;
543 struct imsgbuf *ibuf;
545 if (repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_COMMIT].imsg_fd != -1)
546 return request_commit(commit, repo, obj_fd);
548 ibuf = calloc(1, sizeof(*ibuf));
549 if (ibuf == NULL)
550 return got_error_from_errno("calloc");
552 if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_fds) == -1)
553 return got_error_from_errno("socketpair");
555 pid = fork();
556 if (pid == -1)
557 return got_error_from_errno("fork");
558 else if (pid == 0) {
559 exec_privsep_child(imsg_fds, GOT_PATH_PROG_READ_COMMIT,
560 repo->path);
561 /* not reached */
564 if (close(imsg_fds[1]) != 0)
565 return got_error_from_errno("close");
566 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_COMMIT].imsg_fd =
567 imsg_fds[0];
568 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_COMMIT].pid = pid;
569 imsg_init(ibuf, imsg_fds[0]);
570 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_COMMIT].ibuf = ibuf;
572 return request_commit(commit, repo, obj_fd);
576 static const struct got_error *
577 open_commit(struct got_commit_object **commit,
578 struct got_repository *repo, struct got_object_id *id, int check_cache)
580 const struct got_error *err = NULL;
581 struct got_packidx *packidx = NULL;
582 int idx;
583 char *path_packfile;
585 if (check_cache) {
586 *commit = got_repo_get_cached_commit(repo, id);
587 if (*commit != NULL) {
588 (*commit)->refcnt++;
589 return NULL;
591 } else
592 *commit = NULL;
594 err = got_repo_search_packidx(&packidx, &idx, repo, id);
595 if (err == NULL) {
596 struct got_pack *pack = NULL;
598 err = get_packfile_path(&path_packfile, packidx);
599 if (err)
600 return err;
602 pack = got_repo_get_cached_pack(repo, path_packfile);
603 if (pack == NULL) {
604 err = got_repo_cache_pack(&pack, repo, path_packfile,
605 packidx);
606 if (err)
607 return err;
609 err = read_packed_commit_privsep(commit, pack,
610 packidx, idx, id);
611 } else if (err->code == GOT_ERR_NO_OBJ) {
612 int fd;
614 err = open_loose_object(&fd, id, repo);
615 if (err)
616 return err;
617 err = read_commit_privsep(commit, fd, repo);
620 if (err == NULL) {
621 (*commit)->refcnt++;
622 err = got_repo_cache_commit(repo, id, *commit);
625 return err;
628 const struct got_error *
629 got_object_open_as_commit(struct got_commit_object **commit,
630 struct got_repository *repo, struct got_object_id *id)
632 *commit = got_repo_get_cached_commit(repo, id);
633 if (*commit != NULL) {
634 (*commit)->refcnt++;
635 return NULL;
638 return open_commit(commit, repo, id, 0);
641 const struct got_error *
642 got_object_commit_open(struct got_commit_object **commit,
643 struct got_repository *repo, struct got_object *obj)
645 return open_commit(commit, repo, got_object_get_id(obj), 1);
648 const struct got_error *
649 got_object_qid_alloc(struct got_object_qid **qid, struct got_object_id *id)
651 const struct got_error *err = NULL;
653 *qid = calloc(1, sizeof(**qid));
654 if (*qid == NULL)
655 return got_error_from_errno("calloc");
657 (*qid)->id = got_object_id_dup(id);
658 if ((*qid)->id == NULL) {
659 err = got_error_from_errno("got_object_id_dup");
660 got_object_qid_free(*qid);
661 *qid = NULL;
662 return err;
665 return NULL;
668 static const struct got_error *
669 request_packed_tree(struct got_tree_object **tree, struct got_pack *pack,
670 int pack_idx, struct got_object_id *id)
672 const struct got_error *err = NULL;
674 err = got_privsep_send_tree_req(pack->privsep_child->ibuf, -1, id,
675 pack_idx);
676 if (err)
677 return err;
679 return got_privsep_recv_tree(tree, pack->privsep_child->ibuf);
682 static const struct got_error *
683 read_packed_tree_privsep(struct got_tree_object **tree,
684 struct got_pack *pack, struct got_packidx *packidx, int idx,
685 struct got_object_id *id)
687 const struct got_error *err = NULL;
689 if (pack->privsep_child)
690 return request_packed_tree(tree, pack, idx, id);
692 err = start_pack_privsep_child(pack, packidx);
693 if (err)
694 return err;
696 return request_packed_tree(tree, pack, idx, id);
699 static const struct got_error *
700 request_tree(struct got_tree_object **tree, struct got_repository *repo,
701 int fd)
703 const struct got_error *err = NULL;
704 struct imsgbuf *ibuf;
706 ibuf = repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TREE].ibuf;
708 err = got_privsep_send_tree_req(ibuf, fd, NULL, -1);
709 if (err)
710 return err;
712 return got_privsep_recv_tree(tree, ibuf);
715 const struct got_error *
716 read_tree_privsep(struct got_tree_object **tree, int obj_fd,
717 struct got_repository *repo)
719 int imsg_fds[2];
720 pid_t pid;
721 struct imsgbuf *ibuf;
723 if (repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TREE].imsg_fd != -1)
724 return request_tree(tree, repo, obj_fd);
726 ibuf = calloc(1, sizeof(*ibuf));
727 if (ibuf == NULL)
728 return got_error_from_errno("calloc");
730 if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_fds) == -1)
731 return got_error_from_errno("socketpair");
733 pid = fork();
734 if (pid == -1)
735 return got_error_from_errno("fork");
736 else if (pid == 0) {
737 exec_privsep_child(imsg_fds, GOT_PATH_PROG_READ_TREE,
738 repo->path);
739 /* not reached */
742 if (close(imsg_fds[1]) != 0)
743 return got_error_from_errno("close");
744 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TREE].imsg_fd =
745 imsg_fds[0];
746 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TREE].pid = pid;
747 imsg_init(ibuf, imsg_fds[0]);
748 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TREE].ibuf = ibuf;
751 return request_tree(tree, repo, obj_fd);
754 static const struct got_error *
755 open_tree(struct got_tree_object **tree, struct got_repository *repo,
756 struct got_object_id *id, int check_cache)
758 const struct got_error *err = NULL;
759 struct got_packidx *packidx = NULL;
760 int idx;
761 char *path_packfile;
763 if (check_cache) {
764 *tree = got_repo_get_cached_tree(repo, id);
765 if (*tree != NULL) {
766 (*tree)->refcnt++;
767 return NULL;
769 } else
770 *tree = NULL;
772 err = got_repo_search_packidx(&packidx, &idx, repo, id);
773 if (err == NULL) {
774 struct got_pack *pack = NULL;
776 err = get_packfile_path(&path_packfile, packidx);
777 if (err)
778 return err;
780 pack = got_repo_get_cached_pack(repo, path_packfile);
781 if (pack == NULL) {
782 err = got_repo_cache_pack(&pack, repo, path_packfile,
783 packidx);
784 if (err)
785 return err;
787 err = read_packed_tree_privsep(tree, pack,
788 packidx, idx, id);
789 } else if (err->code == GOT_ERR_NO_OBJ) {
790 int fd;
792 err = open_loose_object(&fd, id, repo);
793 if (err)
794 return err;
795 err = read_tree_privsep(tree, fd, repo);
798 if (err == NULL) {
799 (*tree)->refcnt++;
800 err = got_repo_cache_tree(repo, id, *tree);
803 return err;
806 const struct got_error *
807 got_object_open_as_tree(struct got_tree_object **tree,
808 struct got_repository *repo, struct got_object_id *id)
810 *tree = got_repo_get_cached_tree(repo, id);
811 if (*tree != NULL) {
812 (*tree)->refcnt++;
813 return NULL;
816 return open_tree(tree, repo, id, 0);
819 const struct got_error *
820 got_object_tree_open(struct got_tree_object **tree,
821 struct got_repository *repo, struct got_object *obj)
823 return open_tree(tree, repo, got_object_get_id(obj), 1);
826 const struct got_tree_entries *
827 got_object_tree_get_entries(struct got_tree_object *tree)
829 return &tree->entries;
832 static const struct got_error *
833 request_packed_blob(uint8_t **outbuf, size_t *size, size_t *hdrlen, int outfd,
834 struct got_pack *pack, struct got_packidx *packidx, int idx,
835 struct got_object_id *id)
837 const struct got_error *err = NULL;
838 int outfd_child;
839 int basefd, accumfd; /* temporary files for delta application */
841 basefd = got_opentempfd();
842 if (basefd == -1)
843 return got_error_from_errno("got_opentempfd");
844 accumfd = got_opentempfd();
845 if (accumfd == -1)
846 return got_error_from_errno("got_opentempfd");
848 outfd_child = dup(outfd);
849 if (outfd_child == -1)
850 return got_error_from_errno("dup");
852 err = got_privsep_send_blob_req(pack->privsep_child->ibuf, -1, id, idx);
853 if (err)
854 return err;
856 err = got_privsep_send_blob_outfd(pack->privsep_child->ibuf,
857 outfd_child);
858 if (err) {
859 close(basefd);
860 close(accumfd);
861 return err;
864 err = got_privsep_send_tmpfd(pack->privsep_child->ibuf,
865 basefd);
866 if (err) {
867 close(accumfd);
868 return err;
871 err = got_privsep_send_tmpfd(pack->privsep_child->ibuf,
872 accumfd);
873 if (err)
874 return err;
876 err = got_privsep_recv_blob(outbuf, size, hdrlen,
877 pack->privsep_child->ibuf);
878 if (err)
879 return err;
881 if (lseek(outfd, SEEK_SET, 0) == -1)
882 err = got_error_from_errno("lseek");
884 return err;
887 static const struct got_error *
888 read_packed_blob_privsep(uint8_t **outbuf, size_t *size, size_t *hdrlen,
889 int outfd, struct got_pack *pack, struct got_packidx *packidx, int idx,
890 struct got_object_id *id)
892 const struct got_error *err = NULL;
894 if (pack->privsep_child == NULL) {
895 err = start_pack_privsep_child(pack, packidx);
896 if (err)
897 return err;
900 return request_packed_blob(outbuf, size, hdrlen, outfd, pack, packidx,
901 idx, id);
904 static const struct got_error *
905 request_blob(uint8_t **outbuf, size_t *size, size_t *hdrlen, int outfd,
906 int infd, struct imsgbuf *ibuf)
908 const struct got_error *err = NULL;
909 int outfd_child;
911 outfd_child = dup(outfd);
912 if (outfd_child == -1)
913 return got_error_from_errno("dup");
915 err = got_privsep_send_blob_req(ibuf, infd, NULL, -1);
916 if (err)
917 return err;
919 err = got_privsep_send_blob_outfd(ibuf, outfd_child);
920 if (err)
921 return err;
923 err = got_privsep_recv_blob(outbuf, size, hdrlen, ibuf);
924 if (err)
925 return err;
927 if (lseek(outfd, SEEK_SET, 0) == -1)
928 return got_error_from_errno("lseek");
930 return err;
933 static const struct got_error *
934 read_blob_privsep(uint8_t **outbuf, size_t *size, size_t *hdrlen,
935 int outfd, int infd, struct got_repository *repo)
937 int imsg_fds[2];
938 pid_t pid;
939 struct imsgbuf *ibuf;
941 if (repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_BLOB].imsg_fd != -1) {
942 ibuf = repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_BLOB].ibuf;
943 return request_blob(outbuf, size, hdrlen, outfd, infd, ibuf);
946 ibuf = calloc(1, sizeof(*ibuf));
947 if (ibuf == NULL)
948 return got_error_from_errno("calloc");
950 if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_fds) == -1)
951 return got_error_from_errno("socketpair");
953 pid = fork();
954 if (pid == -1)
955 return got_error_from_errno("fork");
956 else if (pid == 0) {
957 exec_privsep_child(imsg_fds, GOT_PATH_PROG_READ_BLOB,
958 repo->path);
959 /* not reached */
962 if (close(imsg_fds[1]) != 0)
963 return got_error_from_errno("close");
964 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_BLOB].imsg_fd =
965 imsg_fds[0];
966 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_BLOB].pid = pid;
967 imsg_init(ibuf, imsg_fds[0]);
968 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_BLOB].ibuf = ibuf;
970 return request_blob(outbuf, size, hdrlen, outfd, infd, ibuf);
973 static const struct got_error *
974 open_blob(struct got_blob_object **blob, struct got_repository *repo,
975 struct got_object_id *id, size_t blocksize)
977 const struct got_error *err = NULL;
978 struct got_packidx *packidx = NULL;
979 int idx;
980 char *path_packfile;
981 uint8_t *outbuf;
982 int outfd;
983 size_t size, hdrlen;
984 struct stat sb;
986 *blob = calloc(1, sizeof(**blob));
987 if (*blob == NULL)
988 return got_error_from_errno("calloc");
990 outfd = got_opentempfd();
991 if (outfd == -1)
992 return got_error_from_errno("got_opentempfd");
994 (*blob)->read_buf = malloc(blocksize);
995 if ((*blob)->read_buf == NULL) {
996 err = got_error_from_errno("malloc");
997 goto done;
1000 err = got_repo_search_packidx(&packidx, &idx, repo, id);
1001 if (err == NULL) {
1002 struct got_pack *pack = NULL;
1004 err = get_packfile_path(&path_packfile, packidx);
1005 if (err)
1006 goto done;
1008 pack = got_repo_get_cached_pack(repo, path_packfile);
1009 if (pack == NULL) {
1010 err = got_repo_cache_pack(&pack, repo, path_packfile,
1011 packidx);
1012 if (err)
1013 goto done;
1015 err = read_packed_blob_privsep(&outbuf, &size, &hdrlen, outfd,
1016 pack, packidx, idx, id);
1017 } else if (err->code == GOT_ERR_NO_OBJ) {
1018 int infd;
1020 err = open_loose_object(&infd, id, repo);
1021 if (err)
1022 goto done;
1023 err = read_blob_privsep(&outbuf, &size, &hdrlen, outfd, infd,
1024 repo);
1026 if (err)
1027 goto done;
1029 if (hdrlen > size) {
1030 err = got_error(GOT_ERR_BAD_OBJ_HDR);
1031 goto done;
1034 if (outbuf) {
1035 if (close(outfd) != 0 && err == NULL)
1036 err = got_error_from_errno("close");
1037 outfd = -1;
1038 (*blob)->f = fmemopen(outbuf, size, "rb");
1039 if ((*blob)->f == NULL) {
1040 err = got_error_from_errno("fmemopen");
1041 free(outbuf);
1042 goto done;
1044 (*blob)->data = outbuf;
1045 } else {
1046 if (fstat(outfd, &sb) == -1) {
1047 err = got_error_from_errno("fstat");
1048 goto done;
1051 if (sb.st_size != size) {
1052 err = got_error(GOT_ERR_PRIVSEP_LEN);
1053 goto done;
1056 (*blob)->f = fdopen(outfd, "rb");
1057 if ((*blob)->f == NULL) {
1058 err = got_error_from_errno("fdopen");
1059 close(outfd);
1060 outfd = -1;
1061 goto done;
1065 (*blob)->hdrlen = hdrlen;
1066 (*blob)->blocksize = blocksize;
1067 memcpy(&(*blob)->id.sha1, id->sha1, SHA1_DIGEST_LENGTH);
1069 done:
1070 if (err) {
1071 if (*blob) {
1072 got_object_blob_close(*blob);
1073 *blob = NULL;
1074 } else if (outfd != -1)
1075 close(outfd);
1077 return err;
1080 const struct got_error *
1081 got_object_open_as_blob(struct got_blob_object **blob,
1082 struct got_repository *repo, struct got_object_id *id,
1083 size_t blocksize)
1085 return open_blob(blob, repo, id, blocksize);
1088 const struct got_error *
1089 got_object_blob_open(struct got_blob_object **blob,
1090 struct got_repository *repo, struct got_object *obj, size_t blocksize)
1092 return open_blob(blob, repo, got_object_get_id(obj), blocksize);
1095 const struct got_error *
1096 got_object_blob_close(struct got_blob_object *blob)
1098 const struct got_error *err = NULL;
1099 free(blob->read_buf);
1100 if (blob->f && fclose(blob->f) != 0)
1101 err = got_error_from_errno("fclose");
1102 free(blob->data);
1103 free(blob);
1104 return err;
1107 char *
1108 got_object_blob_id_str(struct got_blob_object *blob, char *buf, size_t size)
1110 return got_sha1_digest_to_str(blob->id.sha1, buf, size);
1113 size_t
1114 got_object_blob_get_hdrlen(struct got_blob_object *blob)
1116 return blob->hdrlen;
1119 const uint8_t *
1120 got_object_blob_get_read_buf(struct got_blob_object *blob)
1122 return blob->read_buf;
1125 const struct got_error *
1126 got_object_blob_read_block(size_t *outlenp, struct got_blob_object *blob)
1128 size_t n;
1130 n = fread(blob->read_buf, 1, blob->blocksize, blob->f);
1131 if (n == 0 && ferror(blob->f))
1132 return got_ferror(blob->f, GOT_ERR_IO);
1133 *outlenp = n;
1134 return NULL;
1137 const struct got_error *
1138 got_object_blob_dump_to_file(size_t *total_len, int *nlines,
1139 off_t **line_offsets, FILE *outfile, struct got_blob_object *blob)
1141 const struct got_error *err = NULL;
1142 size_t n, len, hdrlen;
1143 const uint8_t *buf;
1144 int i;
1145 size_t noffsets = 0;
1146 off_t off = 0;
1148 if (line_offsets)
1149 *line_offsets = NULL;
1150 if (total_len)
1151 *total_len = 0;
1152 if (nlines)
1153 *nlines = 0;
1155 hdrlen = got_object_blob_get_hdrlen(blob);
1156 do {
1157 err = got_object_blob_read_block(&len, blob);
1158 if (err)
1159 return err;
1160 if (len == 0)
1161 break;
1162 buf = got_object_blob_get_read_buf(blob);
1163 for (i = hdrlen; i < len; i++) {
1164 if (buf[i] != '\n')
1165 continue;
1166 if (nlines)
1167 (*nlines)++;
1168 if (line_offsets && nlines && noffsets < *nlines) {
1169 off_t *o = recallocarray(*line_offsets,
1170 noffsets, *nlines, sizeof(**line_offsets));
1171 if (o == NULL) {
1172 free(*line_offsets);
1173 *line_offsets = NULL;
1174 return got_error_from_errno(
1175 "recallocarray");
1177 *line_offsets = o;
1178 noffsets = *nlines;
1180 if (line_offsets && nlines && total_len) {
1181 (*line_offsets)[*nlines - 1] = off;
1182 off = *total_len + i + 1 - got_object_blob_get_hdrlen(blob);
1185 if (total_len)
1186 *total_len += len;
1187 /* Skip blob object header first time around. */
1188 n = fwrite(buf + hdrlen, 1, len - hdrlen, outfile);
1189 if (n != len - hdrlen)
1190 return got_ferror(outfile, GOT_ERR_IO);
1191 hdrlen = 0;
1192 } while (len != 0);
1194 if (fflush(outfile) != 0)
1195 return got_error_from_errno("fflush");
1196 rewind(outfile);
1198 return NULL;
1201 static const struct got_error *
1202 request_packed_tag(struct got_tag_object **tag, struct got_pack *pack,
1203 int pack_idx, struct got_object_id *id)
1205 const struct got_error *err = NULL;
1207 err = got_privsep_send_tag_req(pack->privsep_child->ibuf, -1, id,
1208 pack_idx);
1209 if (err)
1210 return err;
1212 return got_privsep_recv_tag(tag, pack->privsep_child->ibuf);
1215 static const struct got_error *
1216 read_packed_tag_privsep(struct got_tag_object **tag,
1217 struct got_pack *pack, struct got_packidx *packidx, int idx,
1218 struct got_object_id *id)
1220 const struct got_error *err = NULL;
1222 if (pack->privsep_child)
1223 return request_packed_tag(tag, pack, idx, id);
1225 err = start_pack_privsep_child(pack, packidx);
1226 if (err)
1227 return err;
1229 return request_packed_tag(tag, pack, idx, id);
1232 static const struct got_error *
1233 request_tag(struct got_tag_object **tag, struct got_repository *repo,
1234 int fd)
1236 const struct got_error *err = NULL;
1237 struct imsgbuf *ibuf;
1239 ibuf = repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TAG].ibuf;
1241 err = got_privsep_send_tag_req(ibuf, fd, NULL, -1);
1242 if (err)
1243 return err;
1245 return got_privsep_recv_tag(tag, ibuf);
1248 static const struct got_error *
1249 read_tag_privsep(struct got_tag_object **tag, int obj_fd,
1250 struct got_repository *repo)
1252 int imsg_fds[2];
1253 pid_t pid;
1254 struct imsgbuf *ibuf;
1256 if (repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TAG].imsg_fd != -1)
1257 return request_tag(tag, repo, obj_fd);
1259 ibuf = calloc(1, sizeof(*ibuf));
1260 if (ibuf == NULL)
1261 return got_error_from_errno("calloc");
1263 if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_fds) == -1)
1264 return got_error_from_errno("socketpair");
1266 pid = fork();
1267 if (pid == -1)
1268 return got_error_from_errno("fork");
1269 else if (pid == 0) {
1270 exec_privsep_child(imsg_fds, GOT_PATH_PROG_READ_TAG,
1271 repo->path);
1272 /* not reached */
1275 if (close(imsg_fds[1]) != 0)
1276 return got_error_from_errno("close");
1277 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TAG].imsg_fd =
1278 imsg_fds[0];
1279 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TAG].pid = pid;
1280 imsg_init(ibuf, imsg_fds[0]);
1281 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TAG].ibuf = ibuf;
1283 return request_tag(tag, repo, obj_fd);
1286 static const struct got_error *
1287 open_tag(struct got_tag_object **tag, struct got_repository *repo,
1288 struct got_object_id *id, int check_cache)
1290 const struct got_error *err = NULL;
1291 struct got_packidx *packidx = NULL;
1292 int idx;
1293 char *path_packfile;
1295 if (check_cache) {
1296 *tag = got_repo_get_cached_tag(repo, id);
1297 if (*tag != NULL) {
1298 (*tag)->refcnt++;
1299 return NULL;
1301 } else
1302 *tag = NULL;
1304 err = got_repo_search_packidx(&packidx, &idx, repo, id);
1305 if (err == NULL) {
1306 struct got_pack *pack = NULL;
1308 err = get_packfile_path(&path_packfile, packidx);
1309 if (err)
1310 return err;
1312 pack = got_repo_get_cached_pack(repo, path_packfile);
1313 if (pack == NULL) {
1314 err = got_repo_cache_pack(&pack, repo, path_packfile,
1315 packidx);
1316 if (err)
1317 return err;
1319 err = read_packed_tag_privsep(tag, pack,
1320 packidx, idx, id);
1321 } else if (err->code == GOT_ERR_NO_OBJ) {
1322 int fd;
1324 err = open_loose_object(&fd, id, repo);
1325 if (err)
1326 return err;
1327 err = read_tag_privsep(tag, fd, repo);
1330 if (err == NULL) {
1331 (*tag)->refcnt++;
1332 err = got_repo_cache_tag(repo, id, *tag);
1335 return err;
1338 const struct got_error *
1339 got_object_open_as_tag(struct got_tag_object **tag,
1340 struct got_repository *repo, struct got_object_id *id)
1342 *tag = got_repo_get_cached_tag(repo, id);
1343 if (*tag != NULL) {
1344 (*tag)->refcnt++;
1345 return NULL;
1348 return open_tag(tag, repo, id, 0);
1351 const struct got_error *
1352 got_object_tag_open(struct got_tag_object **tag,
1353 struct got_repository *repo, struct got_object *obj)
1355 return open_tag(tag, repo, got_object_get_id(obj), 1);
1358 int
1359 got_object_tag_get_object_type(struct got_tag_object *tag)
1361 return tag->obj_type;
1364 struct got_object_id *
1365 got_object_tag_get_object_id(struct got_tag_object *tag)
1367 return &tag->id;
1370 static struct got_tree_entry *
1371 find_entry_by_name(struct got_tree_object *tree, const char *name, size_t len)
1373 struct got_tree_entry *te;
1375 /* Note that tree entries are sorted in strncmp() order. */
1376 SIMPLEQ_FOREACH(te, &tree->entries.head, entry) {
1377 int cmp = strncmp(te->name, name, len);
1378 if (cmp < 0)
1379 continue;
1380 if (cmp > 0)
1381 break;
1382 if (te->name[len] == '\0')
1383 return te;
1385 return NULL;
1388 const struct got_tree_entry *
1389 got_object_tree_find_entry(struct got_tree_object *tree, const char *name)
1391 return find_entry_by_name(tree, name, strlen(name));
1394 const struct got_error *
1395 got_object_id_by_path(struct got_object_id **id, struct got_repository *repo,
1396 struct got_object_id *commit_id, const char *path)
1398 const struct got_error *err = NULL;
1399 struct got_commit_object *commit = NULL;
1400 struct got_tree_object *tree = NULL;
1401 struct got_tree_entry *te = NULL;
1402 const char *seg, *s;
1403 size_t seglen;
1405 *id = NULL;
1407 err = got_object_open_as_commit(&commit, repo, commit_id);
1408 if (err)
1409 goto done;
1411 /* Handle opening of root of commit's tree. */
1412 if (got_path_is_root_dir(path)) {
1413 *id = got_object_id_dup(commit->tree_id);
1414 if (*id == NULL)
1415 err = got_error_from_errno("got_object_id_dup");
1416 goto done;
1419 err = got_object_open_as_tree(&tree, repo, commit->tree_id);
1420 if (err)
1421 goto done;
1423 s = path;
1424 while (s[0] == '/')
1425 s++;
1426 seg = s;
1427 seglen = 0;
1428 while (*s) {
1429 struct got_tree_object *next_tree;
1431 if (*s != '/') {
1432 s++;
1433 seglen++;
1434 if (*s)
1435 continue;
1438 te = find_entry_by_name(tree, seg, seglen);
1439 if (te == NULL) {
1440 err = got_error(GOT_ERR_NO_TREE_ENTRY);
1441 goto done;
1444 if (*s == '\0')
1445 break;
1447 seg = s + 1;
1448 seglen = 0;
1449 s++;
1450 if (*s) {
1451 err = got_object_open_as_tree(&next_tree, repo,
1452 te->id);
1453 te = NULL;
1454 if (err)
1455 goto done;
1456 got_object_tree_close(tree);
1457 tree = next_tree;
1461 if (te) {
1462 *id = got_object_id_dup(te->id);
1463 if (*id == NULL)
1464 return got_error_from_errno("got_object_id_dup");
1465 } else
1466 err = got_error(GOT_ERR_NO_TREE_ENTRY);
1467 done:
1468 if (commit)
1469 got_object_commit_close(commit);
1470 if (tree)
1471 got_object_tree_close(tree);
1472 return err;
1475 const struct got_error *
1476 got_object_tree_path_changed(int *changed,
1477 struct got_tree_object *tree01, struct got_tree_object *tree02,
1478 const char *path, struct got_repository *repo)
1480 const struct got_error *err = NULL;
1481 struct got_tree_object *tree1 = NULL, *tree2 = NULL;
1482 struct got_tree_entry *te1 = NULL, *te2 = NULL;
1483 const char *seg, *s;
1484 size_t seglen;
1486 *changed = 0;
1488 /* We are expecting an absolute in-repository path. */
1489 if (path[0] != '/')
1490 return got_error(GOT_ERR_NOT_ABSPATH);
1492 /* We not do support comparing the root path. */
1493 if (path[1] == '\0')
1494 return got_error(GOT_ERR_BAD_PATH);
1496 tree1 = tree01;
1497 tree2 = tree02;
1498 s = path;
1499 s++; /* skip leading '/' */
1500 seg = s;
1501 seglen = 0;
1502 while (*s) {
1503 struct got_tree_object *next_tree1, *next_tree2;
1505 if (*s != '/') {
1506 s++;
1507 seglen++;
1508 if (*s)
1509 continue;
1512 te1 = find_entry_by_name(tree1, seg, seglen);
1513 if (te1 == NULL) {
1514 err = got_error(GOT_ERR_NO_OBJ);
1515 goto done;
1518 te2 = find_entry_by_name(tree2, seg, seglen);
1519 if (te2 == NULL) {
1520 *changed = 1;
1521 goto done;
1524 if (te1->mode != te2->mode) {
1525 *changed = 1;
1526 goto done;
1529 if (got_object_id_cmp(te1->id, te2->id) == 0) {
1530 *changed = 0;
1531 goto done;
1534 if (*s == '\0') { /* final path element */
1535 *changed = 1;
1536 goto done;
1539 seg = s + 1;
1540 s++;
1541 seglen = 0;
1542 if (*s) {
1543 err = got_object_open_as_tree(&next_tree1, repo,
1544 te1->id);
1545 te1 = NULL;
1546 if (err)
1547 goto done;
1548 if (tree1 != tree01)
1549 got_object_tree_close(tree1);
1550 tree1 = next_tree1;
1552 err = got_object_open_as_tree(&next_tree2, repo,
1553 te2->id);
1554 te2 = NULL;
1555 if (err)
1556 goto done;
1557 if (tree2 != tree02)
1558 got_object_tree_close(tree2);
1559 tree2 = next_tree2;
1562 done:
1563 if (tree1 && tree1 != tree01)
1564 got_object_tree_close(tree1);
1565 if (tree2 && tree2 != tree02)
1566 got_object_tree_close(tree2);
1567 return err;
1570 const struct got_error *
1571 got_object_tree_entry_dup(struct got_tree_entry **new_te,
1572 struct got_tree_entry *te)
1574 const struct got_error *err = NULL;
1576 *new_te = calloc(1, sizeof(**new_te));
1577 if (*new_te == NULL)
1578 return got_error_from_errno("calloc");
1580 (*new_te)->mode = te->mode;
1581 (*new_te)->name = strdup(te->name);
1582 if ((*new_te)->name == NULL) {
1583 err = got_error_from_errno("strdup");
1584 goto done;
1587 (*new_te)->id = got_object_id_dup(te->id);
1588 if ((*new_te)->id == NULL) {
1589 err = got_error_from_errno("got_object_id_dup");
1590 goto done;
1592 done:
1593 if (err) {
1594 got_object_tree_entry_close(*new_te);
1595 *new_te = NULL;
1597 return err;