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;
119 *path = NULL;
121 path_objects = got_repo_get_path_objects(repo);
122 if (path_objects == NULL)
123 return got_error_from_errno("got_repo_get_path_objects");
125 err = got_object_id_str(&hex, id);
126 if (err)
127 goto done;
129 if (asprintf(path, "%s/%.2x/%s", path_objects,
130 id->sha1[0], hex + 2) == -1)
131 err = got_error_from_errno("asprintf");
133 done:
134 free(hex);
135 free(path_objects);
136 return err;
139 static const struct got_error *
140 open_loose_object(int *fd, struct got_object_id *id,
141 struct got_repository *repo)
143 const struct got_error *err = NULL;
144 char *path;
146 err = got_object_get_path(&path, id, repo);
147 if (err)
148 return err;
149 *fd = open(path, O_RDONLY | O_NOFOLLOW);
150 if (*fd == -1) {
151 err = got_error_from_errno2("open", path);
152 goto done;
154 done:
155 free(path);
156 return err;
159 static const struct got_error *
160 get_packfile_path(char **path_packfile, struct got_packidx *packidx)
162 size_t size;
164 /* Packfile path contains ".pack" instead of ".idx", so add one byte. */
165 size = strlen(packidx->path_packidx) + 2;
166 if (size < GOT_PACKFILE_NAMELEN + 1)
167 return got_error(GOT_ERR_BAD_PATH);
169 *path_packfile = malloc(size);
170 if (*path_packfile == NULL)
171 return got_error_from_errno("malloc");
173 /* Copy up to and excluding ".idx". */
174 if (strlcpy(*path_packfile, packidx->path_packidx,
175 size - strlen(GOT_PACKIDX_SUFFIX) - 1) >= size)
176 return got_error(GOT_ERR_NO_SPACE);
178 if (strlcat(*path_packfile, GOT_PACKFILE_SUFFIX, size) >= size)
179 return got_error(GOT_ERR_NO_SPACE);
181 return NULL;
184 static void
185 exec_privsep_child(int imsg_fds[2], const char *path, const char *repo_path)
187 if (close(imsg_fds[0]) != 0) {
188 fprintf(stderr, "%s: %s\n", getprogname(), strerror(errno));
189 _exit(1);
192 if (dup2(imsg_fds[1], GOT_IMSG_FD_CHILD) == -1) {
193 fprintf(stderr, "%s: %s\n", getprogname(), strerror(errno));
194 _exit(1);
196 if (closefrom(GOT_IMSG_FD_CHILD + 1) == -1) {
197 fprintf(stderr, "%s: %s\n", getprogname(), strerror(errno));
198 _exit(1);
201 if (execl(path, path, repo_path, (char *)NULL) == -1) {
202 fprintf(stderr, "%s: %s: %s\n", getprogname(), path,
203 strerror(errno));
204 _exit(1);
208 static const struct got_error *
209 request_packed_object(struct got_object **obj, struct got_pack *pack, int idx,
210 struct got_object_id *id)
212 const struct got_error *err = NULL;
213 struct imsgbuf *ibuf = pack->privsep_child->ibuf;
215 err = got_privsep_send_packed_obj_req(ibuf, idx, id);
216 if (err)
217 return err;
219 err = got_privsep_recv_obj(obj, ibuf);
220 if (err)
221 return err;
223 (*obj)->path_packfile = strdup(pack->path_packfile);
224 if ((*obj)->path_packfile == NULL) {
225 err = got_error_from_errno("strdup");
226 return err;
228 memcpy(&(*obj)->id, id, sizeof((*obj)->id));
230 return NULL;
233 static void
234 set_max_datasize(void)
236 struct rlimit rl;
238 if (getrlimit(RLIMIT_DATA, &rl) != 0)
239 return;
241 rl.rlim_cur = rl.rlim_max;
242 setrlimit(RLIMIT_DATA, &rl);
245 static const struct got_error *
246 start_pack_privsep_child(struct got_pack *pack, struct got_packidx *packidx)
248 const struct got_error *err = NULL;
249 int imsg_fds[2];
250 pid_t pid;
251 struct imsgbuf *ibuf;
253 ibuf = calloc(1, sizeof(*ibuf));
254 if (ibuf == NULL)
255 return got_error_from_errno("calloc");
257 pack->privsep_child = calloc(1, sizeof(*pack->privsep_child));
258 if (pack->privsep_child == NULL) {
259 err = got_error_from_errno("calloc");
260 free(ibuf);
261 return err;
264 if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_fds) == -1) {
265 err = got_error_from_errno("socketpair");
266 goto done;
269 pid = fork();
270 if (pid == -1) {
271 err = got_error_from_errno("fork");
272 goto done;
273 } else if (pid == 0) {
274 set_max_datasize();
275 exec_privsep_child(imsg_fds, GOT_PATH_PROG_READ_PACK,
276 pack->path_packfile);
277 /* not reached */
280 if (close(imsg_fds[1]) != 0)
281 return got_error_from_errno("close");
282 pack->privsep_child->imsg_fd = imsg_fds[0];
283 pack->privsep_child->pid = pid;
284 imsg_init(ibuf, imsg_fds[0]);
285 pack->privsep_child->ibuf = ibuf;
287 err = got_privsep_init_pack_child(ibuf, pack, packidx);
288 if (err) {
289 const struct got_error *child_err;
290 err = got_privsep_send_stop(pack->privsep_child->imsg_fd);
291 child_err = got_privsep_wait_for_child(
292 pack->privsep_child->pid);
293 if (child_err && err == NULL)
294 err = child_err;
296 done:
297 if (err) {
298 free(ibuf);
299 free(pack->privsep_child);
300 pack->privsep_child = NULL;
302 return err;
305 static const struct got_error *
306 read_packed_object_privsep(struct got_object **obj,
307 struct got_repository *repo, struct got_pack *pack,
308 struct got_packidx *packidx, int idx, struct got_object_id *id)
310 const struct got_error *err = NULL;
312 if (pack->privsep_child)
313 return request_packed_object(obj, pack, idx, id);
315 err = start_pack_privsep_child(pack, packidx);
316 if (err)
317 return err;
319 return request_packed_object(obj, pack, idx, id);
323 static const struct got_error *
324 open_packed_object(struct got_object **obj, struct got_object_id *id,
325 struct got_repository *repo)
327 const struct got_error *err = NULL;
328 struct got_pack *pack = NULL;
329 struct got_packidx *packidx = NULL;
330 int idx;
331 char *path_packfile;
333 err = got_repo_search_packidx(&packidx, &idx, repo, id);
334 if (err)
335 return err;
337 err = get_packfile_path(&path_packfile, packidx);
338 if (err)
339 return err;
341 pack = got_repo_get_cached_pack(repo, path_packfile);
342 if (pack == NULL) {
343 err = got_repo_cache_pack(&pack, repo, path_packfile, packidx);
344 if (err)
345 goto done;
348 err = read_packed_object_privsep(obj, repo, pack, packidx, idx, id);
349 if (err)
350 goto done;
352 err = got_repo_cache_pack(NULL, repo, (*obj)->path_packfile, packidx);
353 done:
354 free(path_packfile);
355 return err;
358 static const struct got_error *
359 request_object(struct got_object **obj, struct got_repository *repo, int fd)
361 const struct got_error *err = NULL;
362 struct imsgbuf *ibuf;
364 ibuf = repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_OBJECT].ibuf;
366 err = got_privsep_send_obj_req(ibuf, fd);
367 if (err)
368 return err;
370 return got_privsep_recv_obj(obj, ibuf);
373 static const struct got_error *
374 read_object_header_privsep(struct got_object **obj, struct got_repository *repo,
375 int obj_fd)
377 int imsg_fds[2];
378 pid_t pid;
379 struct imsgbuf *ibuf;
381 if (repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_OBJECT].imsg_fd != -1)
382 return request_object(obj, repo, obj_fd);
384 ibuf = calloc(1, sizeof(*ibuf));
385 if (ibuf == NULL)
386 return got_error_from_errno("calloc");
388 if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_fds) == -1)
389 return got_error_from_errno("socketpair");
391 pid = fork();
392 if (pid == -1)
393 return got_error_from_errno("fork");
394 else if (pid == 0) {
395 exec_privsep_child(imsg_fds, GOT_PATH_PROG_READ_OBJECT,
396 repo->path);
397 /* not reached */
400 if (close(imsg_fds[1]) != 0)
401 return got_error_from_errno("close");
402 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_OBJECT].imsg_fd =
403 imsg_fds[0];
404 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_OBJECT].pid = pid;
405 imsg_init(ibuf, imsg_fds[0]);
406 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_OBJECT].ibuf = ibuf;
408 return request_object(obj, repo, obj_fd);
412 const struct got_error *
413 got_object_open(struct got_object **obj, struct got_repository *repo,
414 struct got_object_id *id)
416 const struct got_error *err = NULL;
417 char *path;
418 int fd;
420 *obj = got_repo_get_cached_object(repo, id);
421 if (*obj != NULL) {
422 (*obj)->refcnt++;
423 return NULL;
426 err = open_packed_object(obj, id, repo);
427 if (err && err->code != GOT_ERR_NO_OBJ)
428 return err;
429 if (*obj) {
430 (*obj)->refcnt++;
431 return got_repo_cache_object(repo, id, *obj);
434 err = got_object_get_path(&path, id, repo);
435 if (err)
436 return err;
438 fd = open(path, O_RDONLY | O_NOFOLLOW);
439 if (fd == -1) {
440 if (errno == ENOENT)
441 err = got_error_no_obj(id);
442 else
443 err = got_error_from_errno2("open", path);
444 goto done;
445 } else {
446 err = read_object_header_privsep(obj, repo, fd);
447 if (err)
448 goto done;
449 memcpy((*obj)->id.sha1, id->sha1, SHA1_DIGEST_LENGTH);
452 (*obj)->refcnt++;
453 err = got_repo_cache_object(repo, id, *obj);
454 done:
455 free(path);
456 return err;
460 const struct got_error *
461 got_object_open_by_id_str(struct got_object **obj, struct got_repository *repo,
462 const char *id_str)
464 struct got_object_id id;
466 if (!got_parse_sha1_digest(id.sha1, id_str))
467 return got_error(GOT_ERR_BAD_OBJ_ID_STR);
469 return got_object_open(obj, repo, &id);
472 const struct got_error *
473 got_object_resolve_id_str(struct got_object_id **id,
474 struct got_repository *repo, const char *id_str)
476 const struct got_error *err = NULL;
477 struct got_object *obj;
479 err = got_object_open_by_id_str(&obj, repo, id_str);
480 if (err)
481 return err;
483 *id = got_object_id_dup(got_object_get_id(obj));
484 got_object_close(obj);
485 if (*id == NULL)
486 return got_error_from_errno("got_object_id_dup");
488 return NULL;
491 static const struct got_error *
492 request_packed_commit(struct got_commit_object **commit, struct got_pack *pack,
493 int pack_idx, struct got_object_id *id)
495 const struct got_error *err = NULL;
497 err = got_privsep_send_commit_req(pack->privsep_child->ibuf, -1, id,
498 pack_idx);
499 if (err)
500 return err;
502 return got_privsep_recv_commit(commit, pack->privsep_child->ibuf);
505 static const struct got_error *
506 read_packed_commit_privsep(struct got_commit_object **commit,
507 struct got_pack *pack, struct got_packidx *packidx, int idx,
508 struct got_object_id *id)
510 const struct got_error *err = NULL;
512 if (pack->privsep_child)
513 return request_packed_commit(commit, pack, idx, id);
515 err = start_pack_privsep_child(pack, packidx);
516 if (err)
517 return err;
519 return request_packed_commit(commit, pack, idx, id);
522 static const struct got_error *
523 request_commit(struct got_commit_object **commit, struct got_repository *repo,
524 int fd)
526 const struct got_error *err = NULL;
527 struct imsgbuf *ibuf;
529 ibuf = repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_COMMIT].ibuf;
531 err = got_privsep_send_commit_req(ibuf, fd, NULL, -1);
532 if (err)
533 return err;
535 return got_privsep_recv_commit(commit, ibuf);
538 static const struct got_error *
539 read_commit_privsep(struct got_commit_object **commit, int obj_fd,
540 struct got_repository *repo)
542 int imsg_fds[2];
543 pid_t pid;
544 struct imsgbuf *ibuf;
546 if (repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_COMMIT].imsg_fd != -1)
547 return request_commit(commit, repo, obj_fd);
549 ibuf = calloc(1, sizeof(*ibuf));
550 if (ibuf == NULL)
551 return got_error_from_errno("calloc");
553 if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_fds) == -1)
554 return got_error_from_errno("socketpair");
556 pid = fork();
557 if (pid == -1)
558 return got_error_from_errno("fork");
559 else if (pid == 0) {
560 exec_privsep_child(imsg_fds, GOT_PATH_PROG_READ_COMMIT,
561 repo->path);
562 /* not reached */
565 if (close(imsg_fds[1]) != 0)
566 return got_error_from_errno("close");
567 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_COMMIT].imsg_fd =
568 imsg_fds[0];
569 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_COMMIT].pid = pid;
570 imsg_init(ibuf, imsg_fds[0]);
571 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_COMMIT].ibuf = ibuf;
573 return request_commit(commit, repo, obj_fd);
577 static const struct got_error *
578 open_commit(struct got_commit_object **commit,
579 struct got_repository *repo, struct got_object_id *id, int check_cache)
581 const struct got_error *err = NULL;
582 struct got_packidx *packidx = NULL;
583 int idx;
584 char *path_packfile;
586 if (check_cache) {
587 *commit = got_repo_get_cached_commit(repo, id);
588 if (*commit != NULL) {
589 (*commit)->refcnt++;
590 return NULL;
592 } else
593 *commit = NULL;
595 err = got_repo_search_packidx(&packidx, &idx, repo, id);
596 if (err == NULL) {
597 struct got_pack *pack = NULL;
599 err = get_packfile_path(&path_packfile, packidx);
600 if (err)
601 return err;
603 pack = got_repo_get_cached_pack(repo, path_packfile);
604 if (pack == NULL) {
605 err = got_repo_cache_pack(&pack, repo, path_packfile,
606 packidx);
607 if (err)
608 return err;
610 err = read_packed_commit_privsep(commit, pack,
611 packidx, idx, id);
612 } else if (err->code == GOT_ERR_NO_OBJ) {
613 int fd;
615 err = open_loose_object(&fd, id, repo);
616 if (err)
617 return err;
618 err = read_commit_privsep(commit, fd, repo);
621 if (err == NULL) {
622 (*commit)->refcnt++;
623 err = got_repo_cache_commit(repo, id, *commit);
626 return err;
629 const struct got_error *
630 got_object_open_as_commit(struct got_commit_object **commit,
631 struct got_repository *repo, struct got_object_id *id)
633 *commit = got_repo_get_cached_commit(repo, id);
634 if (*commit != NULL) {
635 (*commit)->refcnt++;
636 return NULL;
639 return open_commit(commit, repo, id, 0);
642 const struct got_error *
643 got_object_commit_open(struct got_commit_object **commit,
644 struct got_repository *repo, struct got_object *obj)
646 return open_commit(commit, repo, got_object_get_id(obj), 1);
649 const struct got_error *
650 got_object_qid_alloc(struct got_object_qid **qid, struct got_object_id *id)
652 const struct got_error *err = NULL;
654 *qid = calloc(1, sizeof(**qid));
655 if (*qid == NULL)
656 return got_error_from_errno("calloc");
658 (*qid)->id = got_object_id_dup(id);
659 if ((*qid)->id == NULL) {
660 err = got_error_from_errno("got_object_id_dup");
661 got_object_qid_free(*qid);
662 *qid = NULL;
663 return err;
666 return NULL;
669 static const struct got_error *
670 request_packed_tree(struct got_tree_object **tree, struct got_pack *pack,
671 int pack_idx, struct got_object_id *id)
673 const struct got_error *err = NULL;
675 err = got_privsep_send_tree_req(pack->privsep_child->ibuf, -1, id,
676 pack_idx);
677 if (err)
678 return err;
680 return got_privsep_recv_tree(tree, pack->privsep_child->ibuf);
683 static const struct got_error *
684 read_packed_tree_privsep(struct got_tree_object **tree,
685 struct got_pack *pack, struct got_packidx *packidx, int idx,
686 struct got_object_id *id)
688 const struct got_error *err = NULL;
690 if (pack->privsep_child)
691 return request_packed_tree(tree, pack, idx, id);
693 err = start_pack_privsep_child(pack, packidx);
694 if (err)
695 return err;
697 return request_packed_tree(tree, pack, idx, id);
700 static const struct got_error *
701 request_tree(struct got_tree_object **tree, struct got_repository *repo,
702 int fd)
704 const struct got_error *err = NULL;
705 struct imsgbuf *ibuf;
707 ibuf = repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TREE].ibuf;
709 err = got_privsep_send_tree_req(ibuf, fd, NULL, -1);
710 if (err)
711 return err;
713 return got_privsep_recv_tree(tree, ibuf);
716 const struct got_error *
717 read_tree_privsep(struct got_tree_object **tree, int obj_fd,
718 struct got_repository *repo)
720 int imsg_fds[2];
721 pid_t pid;
722 struct imsgbuf *ibuf;
724 if (repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TREE].imsg_fd != -1)
725 return request_tree(tree, repo, obj_fd);
727 ibuf = calloc(1, sizeof(*ibuf));
728 if (ibuf == NULL)
729 return got_error_from_errno("calloc");
731 if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_fds) == -1)
732 return got_error_from_errno("socketpair");
734 pid = fork();
735 if (pid == -1)
736 return got_error_from_errno("fork");
737 else if (pid == 0) {
738 exec_privsep_child(imsg_fds, GOT_PATH_PROG_READ_TREE,
739 repo->path);
740 /* not reached */
743 if (close(imsg_fds[1]) != 0)
744 return got_error_from_errno("close");
745 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TREE].imsg_fd =
746 imsg_fds[0];
747 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TREE].pid = pid;
748 imsg_init(ibuf, imsg_fds[0]);
749 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TREE].ibuf = ibuf;
752 return request_tree(tree, repo, obj_fd);
755 static const struct got_error *
756 open_tree(struct got_tree_object **tree, struct got_repository *repo,
757 struct got_object_id *id, int check_cache)
759 const struct got_error *err = NULL;
760 struct got_packidx *packidx = NULL;
761 int idx;
762 char *path_packfile;
764 if (check_cache) {
765 *tree = got_repo_get_cached_tree(repo, id);
766 if (*tree != NULL) {
767 (*tree)->refcnt++;
768 return NULL;
770 } else
771 *tree = NULL;
773 err = got_repo_search_packidx(&packidx, &idx, repo, id);
774 if (err == NULL) {
775 struct got_pack *pack = NULL;
777 err = get_packfile_path(&path_packfile, packidx);
778 if (err)
779 return err;
781 pack = got_repo_get_cached_pack(repo, path_packfile);
782 if (pack == NULL) {
783 err = got_repo_cache_pack(&pack, repo, path_packfile,
784 packidx);
785 if (err)
786 return err;
788 err = read_packed_tree_privsep(tree, pack,
789 packidx, idx, id);
790 } else if (err->code == GOT_ERR_NO_OBJ) {
791 int fd;
793 err = open_loose_object(&fd, id, repo);
794 if (err)
795 return err;
796 err = read_tree_privsep(tree, fd, repo);
799 if (err == NULL) {
800 (*tree)->refcnt++;
801 err = got_repo_cache_tree(repo, id, *tree);
804 return err;
807 const struct got_error *
808 got_object_open_as_tree(struct got_tree_object **tree,
809 struct got_repository *repo, struct got_object_id *id)
811 *tree = got_repo_get_cached_tree(repo, id);
812 if (*tree != NULL) {
813 (*tree)->refcnt++;
814 return NULL;
817 return open_tree(tree, repo, id, 0);
820 const struct got_error *
821 got_object_tree_open(struct got_tree_object **tree,
822 struct got_repository *repo, struct got_object *obj)
824 return open_tree(tree, repo, got_object_get_id(obj), 1);
827 const struct got_tree_entries *
828 got_object_tree_get_entries(struct got_tree_object *tree)
830 return &tree->entries;
833 static const struct got_error *
834 request_packed_blob(uint8_t **outbuf, size_t *size, size_t *hdrlen, int outfd,
835 struct got_pack *pack, struct got_packidx *packidx, int idx,
836 struct got_object_id *id)
838 const struct got_error *err = NULL;
839 int outfd_child;
840 int basefd, accumfd; /* temporary files for delta application */
842 basefd = got_opentempfd();
843 if (basefd == -1)
844 return got_error_from_errno("got_opentempfd");
845 accumfd = got_opentempfd();
846 if (accumfd == -1)
847 return got_error_from_errno("got_opentempfd");
849 outfd_child = dup(outfd);
850 if (outfd_child == -1)
851 return got_error_from_errno("dup");
853 err = got_privsep_send_blob_req(pack->privsep_child->ibuf, -1, id, idx);
854 if (err)
855 return err;
857 err = got_privsep_send_blob_outfd(pack->privsep_child->ibuf,
858 outfd_child);
859 if (err) {
860 close(basefd);
861 close(accumfd);
862 return err;
865 err = got_privsep_send_tmpfd(pack->privsep_child->ibuf,
866 basefd);
867 if (err) {
868 close(accumfd);
869 return err;
872 err = got_privsep_send_tmpfd(pack->privsep_child->ibuf,
873 accumfd);
874 if (err)
875 return err;
877 err = got_privsep_recv_blob(outbuf, size, hdrlen,
878 pack->privsep_child->ibuf);
879 if (err)
880 return err;
882 if (lseek(outfd, SEEK_SET, 0) == -1)
883 err = got_error_from_errno("lseek");
885 return err;
888 static const struct got_error *
889 read_packed_blob_privsep(uint8_t **outbuf, size_t *size, size_t *hdrlen,
890 int outfd, struct got_pack *pack, struct got_packidx *packidx, int idx,
891 struct got_object_id *id)
893 const struct got_error *err = NULL;
895 if (pack->privsep_child == NULL) {
896 err = start_pack_privsep_child(pack, packidx);
897 if (err)
898 return err;
901 return request_packed_blob(outbuf, size, hdrlen, outfd, pack, packidx,
902 idx, id);
905 static const struct got_error *
906 request_blob(uint8_t **outbuf, size_t *size, size_t *hdrlen, int outfd,
907 int infd, struct imsgbuf *ibuf)
909 const struct got_error *err = NULL;
910 int outfd_child;
912 outfd_child = dup(outfd);
913 if (outfd_child == -1)
914 return got_error_from_errno("dup");
916 err = got_privsep_send_blob_req(ibuf, infd, NULL, -1);
917 if (err)
918 return err;
920 err = got_privsep_send_blob_outfd(ibuf, outfd_child);
921 if (err)
922 return err;
924 err = got_privsep_recv_blob(outbuf, size, hdrlen, ibuf);
925 if (err)
926 return err;
928 if (lseek(outfd, SEEK_SET, 0) == -1)
929 return got_error_from_errno("lseek");
931 return err;
934 static const struct got_error *
935 read_blob_privsep(uint8_t **outbuf, size_t *size, size_t *hdrlen,
936 int outfd, int infd, struct got_repository *repo)
938 int imsg_fds[2];
939 pid_t pid;
940 struct imsgbuf *ibuf;
942 if (repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_BLOB].imsg_fd != -1) {
943 ibuf = repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_BLOB].ibuf;
944 return request_blob(outbuf, size, hdrlen, outfd, infd, ibuf);
947 ibuf = calloc(1, sizeof(*ibuf));
948 if (ibuf == NULL)
949 return got_error_from_errno("calloc");
951 if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_fds) == -1)
952 return got_error_from_errno("socketpair");
954 pid = fork();
955 if (pid == -1)
956 return got_error_from_errno("fork");
957 else if (pid == 0) {
958 exec_privsep_child(imsg_fds, GOT_PATH_PROG_READ_BLOB,
959 repo->path);
960 /* not reached */
963 if (close(imsg_fds[1]) != 0)
964 return got_error_from_errno("close");
965 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_BLOB].imsg_fd =
966 imsg_fds[0];
967 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_BLOB].pid = pid;
968 imsg_init(ibuf, imsg_fds[0]);
969 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_BLOB].ibuf = ibuf;
971 return request_blob(outbuf, size, hdrlen, outfd, infd, ibuf);
974 static const struct got_error *
975 open_blob(struct got_blob_object **blob, struct got_repository *repo,
976 struct got_object_id *id, size_t blocksize)
978 const struct got_error *err = NULL;
979 struct got_packidx *packidx = NULL;
980 int idx;
981 char *path_packfile;
982 uint8_t *outbuf;
983 int outfd;
984 size_t size, hdrlen;
985 struct stat sb;
987 *blob = calloc(1, sizeof(**blob));
988 if (*blob == NULL)
989 return got_error_from_errno("calloc");
991 outfd = got_opentempfd();
992 if (outfd == -1)
993 return got_error_from_errno("got_opentempfd");
995 (*blob)->read_buf = malloc(blocksize);
996 if ((*blob)->read_buf == NULL) {
997 err = got_error_from_errno("malloc");
998 goto done;
1001 err = got_repo_search_packidx(&packidx, &idx, repo, id);
1002 if (err == NULL) {
1003 struct got_pack *pack = NULL;
1005 err = get_packfile_path(&path_packfile, packidx);
1006 if (err)
1007 goto done;
1009 pack = got_repo_get_cached_pack(repo, path_packfile);
1010 if (pack == NULL) {
1011 err = got_repo_cache_pack(&pack, repo, path_packfile,
1012 packidx);
1013 if (err)
1014 goto done;
1016 err = read_packed_blob_privsep(&outbuf, &size, &hdrlen, outfd,
1017 pack, packidx, idx, id);
1018 } else if (err->code == GOT_ERR_NO_OBJ) {
1019 int infd;
1021 err = open_loose_object(&infd, id, repo);
1022 if (err)
1023 goto done;
1024 err = read_blob_privsep(&outbuf, &size, &hdrlen, outfd, infd,
1025 repo);
1027 if (err)
1028 goto done;
1030 if (hdrlen > size) {
1031 err = got_error(GOT_ERR_BAD_OBJ_HDR);
1032 goto done;
1035 if (outbuf) {
1036 if (close(outfd) != 0 && err == NULL)
1037 err = got_error_from_errno("close");
1038 outfd = -1;
1039 (*blob)->f = fmemopen(outbuf, size, "rb");
1040 if ((*blob)->f == NULL) {
1041 err = got_error_from_errno("fmemopen");
1042 free(outbuf);
1043 goto done;
1045 (*blob)->data = outbuf;
1046 } else {
1047 if (fstat(outfd, &sb) == -1) {
1048 err = got_error_from_errno("fstat");
1049 goto done;
1052 if (sb.st_size != size) {
1053 err = got_error(GOT_ERR_PRIVSEP_LEN);
1054 goto done;
1057 (*blob)->f = fdopen(outfd, "rb");
1058 if ((*blob)->f == NULL) {
1059 err = got_error_from_errno("fdopen");
1060 close(outfd);
1061 outfd = -1;
1062 goto done;
1066 (*blob)->hdrlen = hdrlen;
1067 (*blob)->blocksize = blocksize;
1068 memcpy(&(*blob)->id.sha1, id->sha1, SHA1_DIGEST_LENGTH);
1070 done:
1071 if (err) {
1072 if (*blob) {
1073 got_object_blob_close(*blob);
1074 *blob = NULL;
1075 } else if (outfd != -1)
1076 close(outfd);
1078 return err;
1081 const struct got_error *
1082 got_object_open_as_blob(struct got_blob_object **blob,
1083 struct got_repository *repo, struct got_object_id *id,
1084 size_t blocksize)
1086 return open_blob(blob, repo, id, blocksize);
1089 const struct got_error *
1090 got_object_blob_open(struct got_blob_object **blob,
1091 struct got_repository *repo, struct got_object *obj, size_t blocksize)
1093 return open_blob(blob, repo, got_object_get_id(obj), blocksize);
1096 const struct got_error *
1097 got_object_blob_close(struct got_blob_object *blob)
1099 const struct got_error *err = NULL;
1100 free(blob->read_buf);
1101 if (blob->f && fclose(blob->f) != 0)
1102 err = got_error_from_errno("fclose");
1103 free(blob->data);
1104 free(blob);
1105 return err;
1108 char *
1109 got_object_blob_id_str(struct got_blob_object *blob, char *buf, size_t size)
1111 return got_sha1_digest_to_str(blob->id.sha1, buf, size);
1114 size_t
1115 got_object_blob_get_hdrlen(struct got_blob_object *blob)
1117 return blob->hdrlen;
1120 const uint8_t *
1121 got_object_blob_get_read_buf(struct got_blob_object *blob)
1123 return blob->read_buf;
1126 const struct got_error *
1127 got_object_blob_read_block(size_t *outlenp, struct got_blob_object *blob)
1129 size_t n;
1131 n = fread(blob->read_buf, 1, blob->blocksize, blob->f);
1132 if (n == 0 && ferror(blob->f))
1133 return got_ferror(blob->f, GOT_ERR_IO);
1134 *outlenp = n;
1135 return NULL;
1138 const struct got_error *
1139 got_object_blob_dump_to_file(size_t *total_len, int *nlines,
1140 off_t **line_offsets, FILE *outfile, struct got_blob_object *blob)
1142 const struct got_error *err = NULL;
1143 size_t n, len, hdrlen;
1144 const uint8_t *buf;
1145 int i;
1146 size_t noffsets = 0;
1147 off_t off = 0;
1149 if (line_offsets)
1150 *line_offsets = NULL;
1151 if (total_len)
1152 *total_len = 0;
1153 if (nlines)
1154 *nlines = 0;
1156 hdrlen = got_object_blob_get_hdrlen(blob);
1157 do {
1158 err = got_object_blob_read_block(&len, blob);
1159 if (err)
1160 return err;
1161 if (len == 0)
1162 break;
1163 buf = got_object_blob_get_read_buf(blob);
1164 for (i = hdrlen; i < len; i++) {
1165 if (buf[i] != '\n')
1166 continue;
1167 if (nlines)
1168 (*nlines)++;
1169 if (line_offsets && nlines && noffsets < *nlines) {
1170 off_t *o = recallocarray(*line_offsets,
1171 noffsets, *nlines, sizeof(**line_offsets));
1172 if (o == NULL) {
1173 free(*line_offsets);
1174 *line_offsets = NULL;
1175 return got_error_from_errno(
1176 "recallocarray");
1178 *line_offsets = o;
1179 noffsets = *nlines;
1181 if (line_offsets && nlines && total_len) {
1182 (*line_offsets)[*nlines - 1] = off;
1183 off = *total_len + i + 1 - got_object_blob_get_hdrlen(blob);
1186 if (total_len)
1187 *total_len += len;
1188 /* Skip blob object header first time around. */
1189 n = fwrite(buf + hdrlen, 1, len - hdrlen, outfile);
1190 if (n != len - hdrlen)
1191 return got_ferror(outfile, GOT_ERR_IO);
1192 hdrlen = 0;
1193 } while (len != 0);
1195 if (fflush(outfile) != 0)
1196 return got_error_from_errno("fflush");
1197 rewind(outfile);
1199 return NULL;
1202 static const struct got_error *
1203 request_packed_tag(struct got_tag_object **tag, struct got_pack *pack,
1204 int pack_idx, struct got_object_id *id)
1206 const struct got_error *err = NULL;
1208 err = got_privsep_send_tag_req(pack->privsep_child->ibuf, -1, id,
1209 pack_idx);
1210 if (err)
1211 return err;
1213 return got_privsep_recv_tag(tag, pack->privsep_child->ibuf);
1216 static const struct got_error *
1217 read_packed_tag_privsep(struct got_tag_object **tag,
1218 struct got_pack *pack, struct got_packidx *packidx, int idx,
1219 struct got_object_id *id)
1221 const struct got_error *err = NULL;
1223 if (pack->privsep_child)
1224 return request_packed_tag(tag, pack, idx, id);
1226 err = start_pack_privsep_child(pack, packidx);
1227 if (err)
1228 return err;
1230 return request_packed_tag(tag, pack, idx, id);
1233 static const struct got_error *
1234 request_tag(struct got_tag_object **tag, struct got_repository *repo,
1235 int fd)
1237 const struct got_error *err = NULL;
1238 struct imsgbuf *ibuf;
1240 ibuf = repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TAG].ibuf;
1242 err = got_privsep_send_tag_req(ibuf, fd, NULL, -1);
1243 if (err)
1244 return err;
1246 return got_privsep_recv_tag(tag, ibuf);
1249 static const struct got_error *
1250 read_tag_privsep(struct got_tag_object **tag, int obj_fd,
1251 struct got_repository *repo)
1253 int imsg_fds[2];
1254 pid_t pid;
1255 struct imsgbuf *ibuf;
1257 if (repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TAG].imsg_fd != -1)
1258 return request_tag(tag, repo, obj_fd);
1260 ibuf = calloc(1, sizeof(*ibuf));
1261 if (ibuf == NULL)
1262 return got_error_from_errno("calloc");
1264 if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_fds) == -1)
1265 return got_error_from_errno("socketpair");
1267 pid = fork();
1268 if (pid == -1)
1269 return got_error_from_errno("fork");
1270 else if (pid == 0) {
1271 exec_privsep_child(imsg_fds, GOT_PATH_PROG_READ_TAG,
1272 repo->path);
1273 /* not reached */
1276 if (close(imsg_fds[1]) != 0)
1277 return got_error_from_errno("close");
1278 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TAG].imsg_fd =
1279 imsg_fds[0];
1280 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TAG].pid = pid;
1281 imsg_init(ibuf, imsg_fds[0]);
1282 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TAG].ibuf = ibuf;
1284 return request_tag(tag, repo, obj_fd);
1287 static const struct got_error *
1288 open_tag(struct got_tag_object **tag, struct got_repository *repo,
1289 struct got_object_id *id, int check_cache)
1291 const struct got_error *err = NULL;
1292 struct got_packidx *packidx = NULL;
1293 int idx;
1294 char *path_packfile;
1296 if (check_cache) {
1297 *tag = got_repo_get_cached_tag(repo, id);
1298 if (*tag != NULL) {
1299 (*tag)->refcnt++;
1300 return NULL;
1302 } else
1303 *tag = NULL;
1305 err = got_repo_search_packidx(&packidx, &idx, repo, id);
1306 if (err == NULL) {
1307 struct got_pack *pack = NULL;
1309 err = get_packfile_path(&path_packfile, packidx);
1310 if (err)
1311 return err;
1313 pack = got_repo_get_cached_pack(repo, path_packfile);
1314 if (pack == NULL) {
1315 err = got_repo_cache_pack(&pack, repo, path_packfile,
1316 packidx);
1317 if (err)
1318 return err;
1320 err = read_packed_tag_privsep(tag, pack,
1321 packidx, idx, id);
1322 } else if (err->code == GOT_ERR_NO_OBJ) {
1323 int fd;
1325 err = open_loose_object(&fd, id, repo);
1326 if (err)
1327 return err;
1328 err = read_tag_privsep(tag, fd, repo);
1331 if (err == NULL) {
1332 (*tag)->refcnt++;
1333 err = got_repo_cache_tag(repo, id, *tag);
1336 return err;
1339 const struct got_error *
1340 got_object_open_as_tag(struct got_tag_object **tag,
1341 struct got_repository *repo, struct got_object_id *id)
1343 *tag = got_repo_get_cached_tag(repo, id);
1344 if (*tag != NULL) {
1345 (*tag)->refcnt++;
1346 return NULL;
1349 return open_tag(tag, repo, id, 0);
1352 const struct got_error *
1353 got_object_tag_open(struct got_tag_object **tag,
1354 struct got_repository *repo, struct got_object *obj)
1356 return open_tag(tag, repo, got_object_get_id(obj), 1);
1359 int
1360 got_object_tag_get_object_type(struct got_tag_object *tag)
1362 return tag->obj_type;
1365 struct got_object_id *
1366 got_object_tag_get_object_id(struct got_tag_object *tag)
1368 return &tag->id;
1371 static struct got_tree_entry *
1372 find_entry_by_name(struct got_tree_object *tree, const char *name, size_t len)
1374 struct got_tree_entry *te;
1376 /* Note that tree entries are sorted in strncmp() order. */
1377 SIMPLEQ_FOREACH(te, &tree->entries.head, entry) {
1378 int cmp = strncmp(te->name, name, len);
1379 if (cmp < 0)
1380 continue;
1381 if (cmp > 0)
1382 break;
1383 if (te->name[len] == '\0')
1384 return te;
1386 return NULL;
1389 const struct got_tree_entry *
1390 got_object_tree_find_entry(struct got_tree_object *tree, const char *name)
1392 return find_entry_by_name(tree, name, strlen(name));
1395 const struct got_error *
1396 got_object_id_by_path(struct got_object_id **id, struct got_repository *repo,
1397 struct got_object_id *commit_id, const char *path)
1399 const struct got_error *err = NULL;
1400 struct got_commit_object *commit = NULL;
1401 struct got_tree_object *tree = NULL;
1402 struct got_tree_entry *te = NULL;
1403 const char *seg, *s;
1404 size_t seglen;
1406 *id = NULL;
1408 err = got_object_open_as_commit(&commit, repo, commit_id);
1409 if (err)
1410 goto done;
1412 /* Handle opening of root of commit's tree. */
1413 if (got_path_is_root_dir(path)) {
1414 *id = got_object_id_dup(commit->tree_id);
1415 if (*id == NULL)
1416 err = got_error_from_errno("got_object_id_dup");
1417 goto done;
1420 err = got_object_open_as_tree(&tree, repo, commit->tree_id);
1421 if (err)
1422 goto done;
1424 s = path;
1425 while (s[0] == '/')
1426 s++;
1427 seg = s;
1428 seglen = 0;
1429 while (*s) {
1430 struct got_tree_object *next_tree;
1432 if (*s != '/') {
1433 s++;
1434 seglen++;
1435 if (*s)
1436 continue;
1439 te = find_entry_by_name(tree, seg, seglen);
1440 if (te == NULL) {
1441 err = got_error(GOT_ERR_NO_TREE_ENTRY);
1442 goto done;
1445 if (*s == '\0')
1446 break;
1448 seg = s + 1;
1449 seglen = 0;
1450 s++;
1451 if (*s) {
1452 err = got_object_open_as_tree(&next_tree, repo,
1453 te->id);
1454 te = NULL;
1455 if (err)
1456 goto done;
1457 got_object_tree_close(tree);
1458 tree = next_tree;
1462 if (te) {
1463 *id = got_object_id_dup(te->id);
1464 if (*id == NULL)
1465 return got_error_from_errno("got_object_id_dup");
1466 } else
1467 err = got_error(GOT_ERR_NO_TREE_ENTRY);
1468 done:
1469 if (commit)
1470 got_object_commit_close(commit);
1471 if (tree)
1472 got_object_tree_close(tree);
1473 return err;
1476 const struct got_error *
1477 got_object_tree_path_changed(int *changed,
1478 struct got_tree_object *tree01, struct got_tree_object *tree02,
1479 const char *path, struct got_repository *repo)
1481 const struct got_error *err = NULL;
1482 struct got_tree_object *tree1 = NULL, *tree2 = NULL;
1483 struct got_tree_entry *te1 = NULL, *te2 = NULL;
1484 const char *seg, *s;
1485 size_t seglen;
1487 *changed = 0;
1489 /* We are expecting an absolute in-repository path. */
1490 if (path[0] != '/')
1491 return got_error(GOT_ERR_NOT_ABSPATH);
1493 /* We not do support comparing the root path. */
1494 if (path[1] == '\0')
1495 return got_error(GOT_ERR_BAD_PATH);
1497 tree1 = tree01;
1498 tree2 = tree02;
1499 s = path;
1500 s++; /* skip leading '/' */
1501 seg = s;
1502 seglen = 0;
1503 while (*s) {
1504 struct got_tree_object *next_tree1, *next_tree2;
1506 if (*s != '/') {
1507 s++;
1508 seglen++;
1509 if (*s)
1510 continue;
1513 te1 = find_entry_by_name(tree1, seg, seglen);
1514 if (te1 == NULL) {
1515 err = got_error(GOT_ERR_NO_OBJ);
1516 goto done;
1519 te2 = find_entry_by_name(tree2, seg, seglen);
1520 if (te2 == NULL) {
1521 *changed = 1;
1522 goto done;
1525 if (te1->mode != te2->mode) {
1526 *changed = 1;
1527 goto done;
1530 if (got_object_id_cmp(te1->id, te2->id) == 0) {
1531 *changed = 0;
1532 goto done;
1535 if (*s == '\0') { /* final path element */
1536 *changed = 1;
1537 goto done;
1540 seg = s + 1;
1541 s++;
1542 seglen = 0;
1543 if (*s) {
1544 err = got_object_open_as_tree(&next_tree1, repo,
1545 te1->id);
1546 te1 = NULL;
1547 if (err)
1548 goto done;
1549 if (tree1 != tree01)
1550 got_object_tree_close(tree1);
1551 tree1 = next_tree1;
1553 err = got_object_open_as_tree(&next_tree2, repo,
1554 te2->id);
1555 te2 = NULL;
1556 if (err)
1557 goto done;
1558 if (tree2 != tree02)
1559 got_object_tree_close(tree2);
1560 tree2 = next_tree2;
1563 done:
1564 if (tree1 && tree1 != tree01)
1565 got_object_tree_close(tree1);
1566 if (tree2 && tree2 != tree02)
1567 got_object_tree_close(tree2);
1568 return err;
1571 const struct got_error *
1572 got_object_tree_entry_dup(struct got_tree_entry **new_te,
1573 struct got_tree_entry *te)
1575 const struct got_error *err = NULL;
1577 *new_te = calloc(1, sizeof(**new_te));
1578 if (*new_te == NULL)
1579 return got_error_from_errno("calloc");
1581 (*new_te)->mode = te->mode;
1582 (*new_te)->name = strdup(te->name);
1583 if ((*new_te)->name == NULL) {
1584 err = got_error_from_errno("strdup");
1585 goto done;
1588 (*new_te)->id = got_object_id_dup(te->id);
1589 if ((*new_te)->id == NULL) {
1590 err = got_error_from_errno("got_object_id_dup");
1591 goto done;
1593 done:
1594 if (err) {
1595 got_object_tree_entry_close(*new_te);
1596 *new_te = NULL;
1598 return err;