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>
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 <zlib.h>
33 #include <ctype.h>
34 #include <limits.h>
35 #include <imsg.h>
36 #include <time.h>
38 #include "got_error.h"
39 #include "got_object.h"
40 #include "got_repository.h"
41 #include "got_opentemp.h"
43 #include "got_lib_sha1.h"
44 #include "got_lib_delta.h"
45 #include "got_lib_path.h"
46 #include "got_lib_inflate.h"
47 #include "got_lib_object.h"
48 #include "got_lib_privsep.h"
49 #include "got_lib_object_idcache.h"
50 #include "got_lib_object_cache.h"
51 #include "got_lib_object_parse.h"
52 #include "got_lib_pack.h"
53 #include "got_lib_repository.h"
55 #ifndef MIN
56 #define MIN(_a,_b) ((_a) < (_b) ? (_a) : (_b))
57 #endif
59 struct got_object_id *
60 got_object_id_dup(struct got_object_id *id1)
61 {
62 struct got_object_id *id2;
64 id2 = malloc(sizeof(*id2));
65 if (id2 == NULL)
66 return NULL;
67 memcpy(id2, id1, sizeof(*id2));
68 return id2;
69 }
71 struct got_object_id *
72 got_object_get_id(struct got_object *obj)
73 {
74 return &obj->id;
75 }
77 const struct got_error *
78 got_object_get_id_str(char **outbuf, struct got_object *obj)
79 {
80 return got_object_id_str(outbuf, &obj->id);
81 }
83 const struct got_error *
84 got_object_get_type(int *type, struct got_repository *repo,
85 struct got_object_id *id)
86 {
87 const struct got_error *err = NULL;
88 struct got_object *obj;
90 err = got_object_open(&obj, repo, id);
91 if (err)
92 return err;
94 switch (obj->type) {
95 case GOT_OBJ_TYPE_COMMIT:
96 case GOT_OBJ_TYPE_TREE:
97 case GOT_OBJ_TYPE_BLOB:
98 case GOT_OBJ_TYPE_TAG:
99 *type = obj->type;
100 break;
101 default:
102 err = got_error(GOT_ERR_OBJ_TYPE);
103 break;
106 got_object_close(obj);
107 return err;
110 static const struct got_error *
111 object_path(char **path, struct got_object_id *id, struct got_repository *repo)
113 const struct got_error *err = NULL;
114 char *hex = NULL;
115 char *path_objects = got_repo_get_path_objects(repo);
117 *path = NULL;
119 if (path_objects == NULL)
120 return got_error_from_errno();
122 err = got_object_id_str(&hex, id);
123 if (err)
124 goto done;
126 if (asprintf(path, "%s/%.2x/%s", path_objects,
127 id->sha1[0], hex + 2) == -1)
128 err = got_error_from_errno();
130 done:
131 free(hex);
132 free(path_objects);
133 return err;
136 static const struct got_error *
137 open_loose_object(int *fd, struct got_object_id *id,
138 struct got_repository *repo)
140 const struct got_error *err = NULL;
141 char *path;
143 err = object_path(&path, id, repo);
144 if (err)
145 return err;
146 *fd = open(path, O_RDONLY | O_NOFOLLOW, GOT_DEFAULT_FILE_MODE);
147 if (*fd == -1) {
148 err = got_error_from_errno();
149 goto done;
151 done:
152 free(path);
153 return err;
156 static const struct got_error *
157 get_packfile_path(char **path_packfile, struct got_packidx *packidx)
159 size_t size;
161 /* Packfile path contains ".pack" instead of ".idx", so add one byte. */
162 size = strlen(packidx->path_packidx) + 2;
163 if (size < GOT_PACKFILE_NAMELEN + 1)
164 return got_error(GOT_ERR_BAD_PATH);
166 *path_packfile = malloc(size);
167 if (*path_packfile == NULL)
168 return got_error_from_errno();
170 /* Copy up to and excluding ".idx". */
171 if (strlcpy(*path_packfile, packidx->path_packidx,
172 size - strlen(GOT_PACKIDX_SUFFIX) - 1) >= size)
173 return got_error(GOT_ERR_NO_SPACE);
175 if (strlcat(*path_packfile, GOT_PACKFILE_SUFFIX, size) >= size)
176 return got_error(GOT_ERR_NO_SPACE);
178 return NULL;
181 static void
182 exec_privsep_child(int imsg_fds[2], const char *path, const char *repo_path)
184 if (close(imsg_fds[0]) != 0) {
185 fprintf(stderr, "%s: %s\n", getprogname(),
186 strerror(errno));
187 _exit(1);
190 if (dup2(imsg_fds[1], GOT_IMSG_FD_CHILD) == -1) {
191 fprintf(stderr, "%s: %s\n", getprogname(),
192 strerror(errno));
193 _exit(1);
195 if (closefrom(GOT_IMSG_FD_CHILD + 1) == -1) {
196 fprintf(stderr, "%s: %s\n", getprogname(),
197 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();
226 return err;
228 memcpy(&(*obj)->id, id, sizeof((*obj)->id));
230 return NULL;
233 static const struct got_error *
234 start_pack_privsep_child(struct got_pack *pack, struct got_packidx *packidx)
236 const struct got_error *err = NULL;
237 int imsg_fds[2];
238 pid_t pid;
239 struct imsgbuf *ibuf;
241 ibuf = calloc(1, sizeof(*ibuf));
242 if (ibuf == NULL)
243 return got_error_from_errno();
245 pack->privsep_child = calloc(1, sizeof(*pack->privsep_child));
246 if (pack->privsep_child == NULL) {
247 err = got_error_from_errno();
248 free(ibuf);
249 return err;
252 if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_fds) == -1) {
253 err = got_error_from_errno();
254 goto done;
257 pid = fork();
258 if (pid == -1) {
259 err = got_error_from_errno();
260 goto done;
261 } else if (pid == 0) {
262 exec_privsep_child(imsg_fds, GOT_PATH_PROG_READ_PACK,
263 pack->path_packfile);
264 /* not reached */
267 if (close(imsg_fds[1]) != 0)
268 return got_error_from_errno();
269 pack->privsep_child->imsg_fd = imsg_fds[0];
270 pack->privsep_child->pid = pid;
271 imsg_init(ibuf, imsg_fds[0]);
272 pack->privsep_child->ibuf = ibuf;
274 err = got_privsep_init_pack_child(ibuf, pack, packidx);
275 if (err) {
276 const struct got_error *child_err;
277 err = got_privsep_send_stop(pack->privsep_child->imsg_fd);
278 child_err = got_privsep_wait_for_child(
279 pack->privsep_child->pid);
280 if (child_err && err == NULL)
281 err = child_err;
283 done:
284 if (err) {
285 free(ibuf);
286 free(pack->privsep_child);
287 pack->privsep_child = NULL;
289 return err;
292 static const struct got_error *
293 read_packed_object_privsep(struct got_object **obj,
294 struct got_repository *repo, struct got_pack *pack,
295 struct got_packidx *packidx, int idx, struct got_object_id *id)
297 const struct got_error *err = NULL;
299 if (pack->privsep_child)
300 return request_packed_object(obj, pack, idx, id);
302 err = start_pack_privsep_child(pack, packidx);
303 if (err)
304 return err;
306 return request_packed_object(obj, pack, idx, id);
310 static const struct got_error *
311 open_packed_object(struct got_object **obj, struct got_object_id *id,
312 struct got_repository *repo)
314 const struct got_error *err = NULL;
315 struct got_pack *pack = NULL;
316 struct got_packidx *packidx = NULL;
317 int idx;
318 char *path_packfile;
320 err = got_repo_search_packidx(&packidx, &idx, repo, id);
321 if (err)
322 return err;
324 err = get_packfile_path(&path_packfile, packidx);
325 if (err)
326 return err;
328 pack = got_repo_get_cached_pack(repo, path_packfile);
329 if (pack == NULL) {
330 err = got_repo_cache_pack(&pack, repo, path_packfile, packidx);
331 if (err)
332 goto done;
335 err = read_packed_object_privsep(obj, repo, pack, packidx, idx, id);
336 if (err)
337 goto done;
339 err = got_repo_cache_pack(NULL, repo, (*obj)->path_packfile, packidx);
340 done:
341 free(path_packfile);
342 return err;
345 static const struct got_error *
346 request_object(struct got_object **obj, struct got_repository *repo, int fd)
348 const struct got_error *err = NULL;
349 struct imsgbuf *ibuf;
351 ibuf = repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_OBJECT].ibuf;
353 err = got_privsep_send_obj_req(ibuf, fd);
354 if (err)
355 return err;
357 return got_privsep_recv_obj(obj, ibuf);
360 static const struct got_error *
361 read_object_header_privsep(struct got_object **obj, struct got_repository *repo,
362 int obj_fd)
364 int imsg_fds[2];
365 pid_t pid;
366 struct imsgbuf *ibuf;
368 if (repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_OBJECT].imsg_fd != -1)
369 return request_object(obj, repo, obj_fd);
371 ibuf = calloc(1, sizeof(*ibuf));
372 if (ibuf == NULL)
373 return got_error_from_errno();
375 if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_fds) == -1)
376 return got_error_from_errno();
378 pid = fork();
379 if (pid == -1)
380 return got_error_from_errno();
381 else if (pid == 0) {
382 exec_privsep_child(imsg_fds, GOT_PATH_PROG_READ_OBJECT,
383 repo->path);
384 /* not reached */
387 if (close(imsg_fds[1]) != 0)
388 return got_error_from_errno();
389 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_OBJECT].imsg_fd =
390 imsg_fds[0];
391 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_OBJECT].pid = pid;
392 imsg_init(ibuf, imsg_fds[0]);
393 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_OBJECT].ibuf = ibuf;
395 return request_object(obj, repo, obj_fd);
399 const struct got_error *
400 got_object_open(struct got_object **obj, struct got_repository *repo,
401 struct got_object_id *id)
403 const struct got_error *err = NULL;
404 char *path;
405 int fd;
407 *obj = got_repo_get_cached_object(repo, id);
408 if (*obj != NULL) {
409 (*obj)->refcnt++;
410 return NULL;
413 err = open_packed_object(obj, id, repo);
414 if (err && err->code != GOT_ERR_NO_OBJ)
415 return err;
416 if (*obj) {
417 (*obj)->refcnt++;
418 return got_repo_cache_object(repo, id, *obj);
421 err = object_path(&path, id, repo);
422 if (err)
423 return err;
425 fd = open(path, O_RDONLY | O_NOFOLLOW, GOT_DEFAULT_FILE_MODE);
426 if (fd == -1) {
427 if (errno == ENOENT)
428 err = got_error_no_obj(id);
429 else
430 err = got_error_from_errno();
431 goto done;
432 } else {
433 err = read_object_header_privsep(obj, repo, fd);
434 if (err)
435 goto done;
436 memcpy((*obj)->id.sha1, id->sha1, SHA1_DIGEST_LENGTH);
439 (*obj)->refcnt++;
440 err = got_repo_cache_object(repo, id, *obj);
441 done:
442 free(path);
443 return err;
447 const struct got_error *
448 got_object_open_by_id_str(struct got_object **obj, struct got_repository *repo,
449 const char *id_str)
451 struct got_object_id id;
453 if (!got_parse_sha1_digest(id.sha1, id_str))
454 return got_error(GOT_ERR_BAD_OBJ_ID_STR);
456 return got_object_open(obj, repo, &id);
459 const struct got_error *
460 got_object_resolve_id_str(struct got_object_id **id,
461 struct got_repository *repo, const char *id_str)
463 const struct got_error *err = NULL;
464 struct got_object *obj;
466 err = got_object_open_by_id_str(&obj, repo, id_str);
467 if (err)
468 return err;
470 *id = got_object_id_dup(got_object_get_id(obj));
471 got_object_close(obj);
472 if (*id == NULL)
473 return got_error_from_errno();
475 return NULL;
478 static const struct got_error *
479 request_packed_commit(struct got_commit_object **commit, struct got_pack *pack,
480 int pack_idx, struct got_object_id *id)
482 const struct got_error *err = NULL;
484 err = got_privsep_send_commit_req(pack->privsep_child->ibuf, -1, id,
485 pack_idx);
486 if (err)
487 return err;
489 return got_privsep_recv_commit(commit, pack->privsep_child->ibuf);
492 static const struct got_error *
493 read_packed_commit_privsep(struct got_commit_object **commit,
494 struct got_pack *pack, struct got_packidx *packidx, int idx,
495 struct got_object_id *id)
497 const struct got_error *err = NULL;
499 if (pack->privsep_child)
500 return request_packed_commit(commit, pack, idx, id);
502 err = start_pack_privsep_child(pack, packidx);
503 if (err)
504 return err;
506 return request_packed_commit(commit, pack, idx, id);
509 static const struct got_error *
510 request_commit(struct got_commit_object **commit, struct got_repository *repo,
511 int fd)
513 const struct got_error *err = NULL;
514 struct imsgbuf *ibuf;
516 ibuf = repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_COMMIT].ibuf;
518 err = got_privsep_send_commit_req(ibuf, fd, NULL, -1);
519 if (err)
520 return err;
522 return got_privsep_recv_commit(commit, ibuf);
525 static const struct got_error *
526 read_commit_privsep(struct got_commit_object **commit, int obj_fd,
527 struct got_repository *repo)
529 int imsg_fds[2];
530 pid_t pid;
531 struct imsgbuf *ibuf;
533 if (repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_COMMIT].imsg_fd != -1)
534 return request_commit(commit, repo, obj_fd);
536 ibuf = calloc(1, sizeof(*ibuf));
537 if (ibuf == NULL)
538 return got_error_from_errno();
540 if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_fds) == -1)
541 return got_error_from_errno();
543 pid = fork();
544 if (pid == -1)
545 return got_error_from_errno();
546 else if (pid == 0) {
547 exec_privsep_child(imsg_fds, GOT_PATH_PROG_READ_COMMIT,
548 repo->path);
549 /* not reached */
552 if (close(imsg_fds[1]) != 0)
553 return got_error_from_errno();
554 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_COMMIT].imsg_fd =
555 imsg_fds[0];
556 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_COMMIT].pid = pid;
557 imsg_init(ibuf, imsg_fds[0]);
558 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_COMMIT].ibuf = ibuf;
560 return request_commit(commit, repo, obj_fd);
564 static const struct got_error *
565 open_commit(struct got_commit_object **commit,
566 struct got_repository *repo, struct got_object_id *id, int check_cache)
568 const struct got_error *err = NULL;
569 struct got_packidx *packidx = NULL;
570 int idx;
571 char *path_packfile;
573 if (check_cache) {
574 *commit = got_repo_get_cached_commit(repo, id);
575 if (*commit != NULL) {
576 (*commit)->refcnt++;
577 return NULL;
579 } else
580 *commit = NULL;
582 err = got_repo_search_packidx(&packidx, &idx, repo, id);
583 if (err == NULL) {
584 struct got_pack *pack = NULL;
586 err = get_packfile_path(&path_packfile, packidx);
587 if (err)
588 return err;
590 pack = got_repo_get_cached_pack(repo, path_packfile);
591 if (pack == NULL) {
592 err = got_repo_cache_pack(&pack, repo, path_packfile,
593 packidx);
594 if (err)
595 return err;
597 err = read_packed_commit_privsep(commit, pack,
598 packidx, idx, id);
599 } else if (err->code == GOT_ERR_NO_OBJ) {
600 int fd;
602 err = open_loose_object(&fd, id, repo);
603 if (err)
604 return err;
605 err = read_commit_privsep(commit, fd, repo);
608 if (err == NULL) {
609 (*commit)->refcnt++;
610 err = got_repo_cache_commit(repo, id, *commit);
613 return err;
616 const struct got_error *
617 got_object_open_as_commit(struct got_commit_object **commit,
618 struct got_repository *repo, struct got_object_id *id)
620 *commit = got_repo_get_cached_commit(repo, id);
621 if (*commit != NULL) {
622 (*commit)->refcnt++;
623 return NULL;
626 return open_commit(commit, repo, id, 0);
629 const struct got_error *
630 got_object_commit_open(struct got_commit_object **commit,
631 struct got_repository *repo, struct got_object *obj)
633 return open_commit(commit, repo, got_object_get_id(obj), 1);
636 const struct got_error *
637 got_object_qid_alloc(struct got_object_qid **qid, struct got_object_id *id)
639 const struct got_error *err = NULL;
641 *qid = calloc(1, sizeof(**qid));
642 if (*qid == NULL)
643 return got_error_from_errno();
645 (*qid)->id = got_object_id_dup(id);
646 if ((*qid)->id == NULL) {
647 err = got_error_from_errno();
648 got_object_qid_free(*qid);
649 *qid = NULL;
650 return err;
653 return NULL;
656 static const struct got_error *
657 request_packed_tree(struct got_tree_object **tree, struct got_pack *pack,
658 int pack_idx, struct got_object_id *id)
660 const struct got_error *err = NULL;
662 err = got_privsep_send_tree_req(pack->privsep_child->ibuf, -1, id,
663 pack_idx);
664 if (err)
665 return err;
667 return got_privsep_recv_tree(tree, pack->privsep_child->ibuf);
670 static const struct got_error *
671 read_packed_tree_privsep(struct got_tree_object **tree,
672 struct got_pack *pack, struct got_packidx *packidx, int idx,
673 struct got_object_id *id)
675 const struct got_error *err = NULL;
677 if (pack->privsep_child)
678 return request_packed_tree(tree, pack, idx, id);
680 err = start_pack_privsep_child(pack, packidx);
681 if (err)
682 return err;
684 return request_packed_tree(tree, pack, idx, id);
687 static const struct got_error *
688 request_tree(struct got_tree_object **tree, struct got_repository *repo,
689 int fd)
691 const struct got_error *err = NULL;
692 struct imsgbuf *ibuf;
694 ibuf = repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TREE].ibuf;
696 err = got_privsep_send_tree_req(ibuf, fd, NULL, -1);
697 if (err)
698 return err;
700 return got_privsep_recv_tree(tree, ibuf);
703 const struct got_error *
704 read_tree_privsep(struct got_tree_object **tree, int obj_fd,
705 struct got_repository *repo)
707 int imsg_fds[2];
708 pid_t pid;
709 struct imsgbuf *ibuf;
711 if (repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TREE].imsg_fd != -1)
712 return request_tree(tree, repo, obj_fd);
714 ibuf = calloc(1, sizeof(*ibuf));
715 if (ibuf == NULL)
716 return got_error_from_errno();
718 if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_fds) == -1)
719 return got_error_from_errno();
721 pid = fork();
722 if (pid == -1)
723 return got_error_from_errno();
724 else if (pid == 0) {
725 exec_privsep_child(imsg_fds, GOT_PATH_PROG_READ_TREE,
726 repo->path);
727 /* not reached */
730 if (close(imsg_fds[1]) != 0)
731 return got_error_from_errno();
732 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TREE].imsg_fd =
733 imsg_fds[0];
734 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TREE].pid = pid;
735 imsg_init(ibuf, imsg_fds[0]);
736 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TREE].ibuf = ibuf;
739 return request_tree(tree, repo, obj_fd);
742 static const struct got_error *
743 open_tree(struct got_tree_object **tree, struct got_repository *repo,
744 struct got_object_id *id, int check_cache)
746 const struct got_error *err = NULL;
747 struct got_packidx *packidx = NULL;
748 int idx;
749 char *path_packfile;
751 if (check_cache) {
752 *tree = got_repo_get_cached_tree(repo, id);
753 if (*tree != NULL) {
754 (*tree)->refcnt++;
755 return NULL;
757 } else
758 *tree = NULL;
760 err = got_repo_search_packidx(&packidx, &idx, repo, id);
761 if (err == NULL) {
762 struct got_pack *pack = NULL;
764 err = get_packfile_path(&path_packfile, packidx);
765 if (err)
766 return err;
768 pack = got_repo_get_cached_pack(repo, path_packfile);
769 if (pack == NULL) {
770 err = got_repo_cache_pack(&pack, repo, path_packfile,
771 packidx);
772 if (err)
773 return err;
775 err = read_packed_tree_privsep(tree, pack,
776 packidx, idx, id);
777 } else if (err->code == GOT_ERR_NO_OBJ) {
778 int fd;
780 err = open_loose_object(&fd, id, repo);
781 if (err)
782 return err;
783 err = read_tree_privsep(tree, fd, repo);
786 if (err == NULL) {
787 (*tree)->refcnt++;
788 err = got_repo_cache_tree(repo, id, *tree);
791 return err;
794 const struct got_error *
795 got_object_open_as_tree(struct got_tree_object **tree,
796 struct got_repository *repo, struct got_object_id *id)
798 *tree = got_repo_get_cached_tree(repo, id);
799 if (*tree != NULL) {
800 (*tree)->refcnt++;
801 return NULL;
804 return open_tree(tree, repo, id, 0);
807 const struct got_error *
808 got_object_tree_open(struct got_tree_object **tree,
809 struct got_repository *repo, struct got_object *obj)
811 return open_tree(tree, repo, got_object_get_id(obj), 1);
814 const struct got_tree_entries *
815 got_object_tree_get_entries(struct got_tree_object *tree)
817 return &tree->entries;
820 static const struct got_error *
821 request_packed_blob(uint8_t **outbuf, size_t *size, size_t *hdrlen, int outfd,
822 struct got_pack *pack, struct got_packidx *packidx, int idx,
823 struct got_object_id *id)
825 const struct got_error *err = NULL;
826 int outfd_child;
827 int basefd, accumfd; /* temporary files for delta application */
829 basefd = got_opentempfd();
830 if (basefd == -1)
831 return got_error_from_errno();
832 accumfd = got_opentempfd();
833 if (accumfd == -1)
834 return got_error_from_errno();
836 outfd_child = dup(outfd);
837 if (outfd_child == -1)
838 return got_error_from_errno();
840 err = got_privsep_send_blob_req(pack->privsep_child->ibuf, -1, id, idx);
841 if (err)
842 return err;
844 err = got_privsep_send_blob_outfd(pack->privsep_child->ibuf,
845 outfd_child);
846 if (err) {
847 close(basefd);
848 close(accumfd);
849 return err;
852 err = got_privsep_send_tmpfd(pack->privsep_child->ibuf,
853 basefd);
854 if (err) {
855 close(accumfd);
856 return err;
859 err = got_privsep_send_tmpfd(pack->privsep_child->ibuf,
860 accumfd);
861 if (err)
862 return err;
864 err = got_privsep_recv_blob(outbuf, size, hdrlen,
865 pack->privsep_child->ibuf);
866 if (err)
867 return err;
869 if (lseek(outfd, SEEK_SET, 0) == -1)
870 err = got_error_from_errno();
872 return err;
875 static const struct got_error *
876 read_packed_blob_privsep(uint8_t **outbuf, size_t *size, size_t *hdrlen,
877 int outfd, struct got_pack *pack, struct got_packidx *packidx, int idx,
878 struct got_object_id *id)
880 const struct got_error *err = NULL;
882 if (pack->privsep_child == NULL) {
883 err = start_pack_privsep_child(pack, packidx);
884 if (err)
885 return err;
888 return request_packed_blob(outbuf, size, hdrlen, outfd, pack, packidx,
889 idx, id);
892 static const struct got_error *
893 request_blob(uint8_t **outbuf, size_t *size, size_t *hdrlen, int outfd,
894 int infd, struct imsgbuf *ibuf)
896 const struct got_error *err = NULL;
897 int outfd_child;
899 outfd_child = dup(outfd);
900 if (outfd_child == -1)
901 return got_error_from_errno();
903 err = got_privsep_send_blob_req(ibuf, infd, NULL, -1);
904 if (err)
905 return err;
907 err = got_privsep_send_blob_outfd(ibuf, outfd_child);
908 if (err)
909 return err;
911 err = got_privsep_recv_blob(outbuf, size, hdrlen, ibuf);
912 if (err)
913 return err;
915 if (lseek(outfd, SEEK_SET, 0) == -1)
916 return got_error_from_errno();
918 return err;
921 static const struct got_error *
922 read_blob_privsep(uint8_t **outbuf, size_t *size, size_t *hdrlen,
923 int outfd, int infd, struct got_repository *repo)
925 int imsg_fds[2];
926 pid_t pid;
927 struct imsgbuf *ibuf;
929 if (repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_BLOB].imsg_fd != -1) {
930 ibuf = repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_BLOB].ibuf;
931 return request_blob(outbuf, size, hdrlen, outfd, infd, ibuf);
934 ibuf = calloc(1, sizeof(*ibuf));
935 if (ibuf == NULL)
936 return got_error_from_errno();
938 if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_fds) == -1)
939 return got_error_from_errno();
941 pid = fork();
942 if (pid == -1)
943 return got_error_from_errno();
944 else if (pid == 0) {
945 exec_privsep_child(imsg_fds, GOT_PATH_PROG_READ_BLOB,
946 repo->path);
947 /* not reached */
950 if (close(imsg_fds[1]) != 0)
951 return got_error_from_errno();
952 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_BLOB].imsg_fd =
953 imsg_fds[0];
954 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_BLOB].pid = pid;
955 imsg_init(ibuf, imsg_fds[0]);
956 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_BLOB].ibuf = ibuf;
958 return request_blob(outbuf, size, hdrlen, outfd, infd, ibuf);
961 static const struct got_error *
962 open_blob(struct got_blob_object **blob, struct got_repository *repo,
963 struct got_object_id *id, size_t blocksize)
965 const struct got_error *err = NULL;
966 struct got_packidx *packidx = NULL;
967 int idx;
968 char *path_packfile;
969 uint8_t *outbuf;
970 int outfd;
971 size_t size, hdrlen;
972 struct stat sb;
974 *blob = calloc(1, sizeof(**blob));
975 if (*blob == NULL)
976 return got_error_from_errno();
978 outfd = got_opentempfd();
979 if (outfd == -1)
980 return got_error_from_errno();
982 (*blob)->read_buf = malloc(blocksize);
983 if ((*blob)->read_buf == NULL) {
984 err = got_error_from_errno();
985 goto done;
988 err = got_repo_search_packidx(&packidx, &idx, repo, id);
989 if (err == NULL) {
990 struct got_pack *pack = NULL;
992 err = get_packfile_path(&path_packfile, packidx);
993 if (err)
994 goto done;
996 pack = got_repo_get_cached_pack(repo, path_packfile);
997 if (pack == NULL) {
998 err = got_repo_cache_pack(&pack, repo, path_packfile,
999 packidx);
1000 if (err)
1001 goto done;
1003 err = read_packed_blob_privsep(&outbuf, &size, &hdrlen, outfd,
1004 pack, packidx, idx, id);
1005 } else if (err->code == GOT_ERR_NO_OBJ) {
1006 int infd;
1008 err = open_loose_object(&infd, id, repo);
1009 if (err)
1010 goto done;
1011 err = read_blob_privsep(&outbuf, &size, &hdrlen, outfd, infd,
1012 repo);
1014 if (err)
1015 goto done;
1017 if (hdrlen > size) {
1018 err = got_error(GOT_ERR_BAD_OBJ_HDR);
1019 goto done;
1022 if (outbuf) {
1023 if (close(outfd) != 0 && err == NULL)
1024 err = got_error_from_errno();
1025 outfd = -1;
1026 (*blob)->f = fmemopen(outbuf, size, "rb");
1027 if ((*blob)->f == NULL) {
1028 err = got_error_from_errno();
1029 free(outbuf);
1030 goto done;
1032 (*blob)->data = outbuf;
1033 } else {
1034 if (fstat(outfd, &sb) == -1) {
1035 err = got_error_from_errno();
1036 goto done;
1039 if (sb.st_size != size) {
1040 err = got_error(GOT_ERR_PRIVSEP_LEN);
1041 goto done;
1044 (*blob)->f = fdopen(outfd, "rb");
1045 if ((*blob)->f == NULL) {
1046 err = got_error_from_errno();
1047 close(outfd);
1048 outfd = -1;
1049 goto done;
1053 (*blob)->hdrlen = hdrlen;
1054 (*blob)->blocksize = blocksize;
1055 memcpy(&(*blob)->id.sha1, id->sha1, SHA1_DIGEST_LENGTH);
1057 done:
1058 if (err) {
1059 if (*blob) {
1060 if ((*blob)->f)
1061 fclose((*blob)->f);
1062 free((*blob)->read_buf);
1063 free((*blob)->data);
1064 free(*blob);
1065 *blob = NULL;
1066 } else if (outfd != -1)
1067 close(outfd);
1069 return err;
1072 const struct got_error *
1073 got_object_open_as_blob(struct got_blob_object **blob,
1074 struct got_repository *repo, struct got_object_id *id,
1075 size_t blocksize)
1077 return open_blob(blob, repo, id, blocksize);
1080 const struct got_error *
1081 got_object_blob_open(struct got_blob_object **blob,
1082 struct got_repository *repo, struct got_object *obj, size_t blocksize)
1084 return open_blob(blob, repo, got_object_get_id(obj), blocksize);
1087 const struct got_error *
1088 got_object_blob_close(struct got_blob_object *blob)
1090 const struct got_error *err = NULL;
1091 free(blob->read_buf);
1092 if (fclose(blob->f) != 0)
1093 err = got_error_from_errno();
1094 free(blob->data);
1095 free(blob);
1096 return err;
1099 char *
1100 got_object_blob_id_str(struct got_blob_object *blob, char *buf, size_t size)
1102 return got_sha1_digest_to_str(blob->id.sha1, buf, size);
1105 size_t
1106 got_object_blob_get_hdrlen(struct got_blob_object *blob)
1108 return blob->hdrlen;
1111 const uint8_t *
1112 got_object_blob_get_read_buf(struct got_blob_object *blob)
1114 return blob->read_buf;
1117 const struct got_error *
1118 got_object_blob_read_block(size_t *outlenp, struct got_blob_object *blob)
1120 size_t n;
1122 n = fread(blob->read_buf, 1, blob->blocksize, blob->f);
1123 if (n == 0 && ferror(blob->f))
1124 return got_ferror(blob->f, GOT_ERR_IO);
1125 *outlenp = n;
1126 return NULL;
1129 const struct got_error *
1130 got_object_blob_dump_to_file(size_t *total_len, int *nlines,
1131 FILE *outfile, struct got_blob_object *blob)
1133 const struct got_error *err = NULL;
1134 size_t n, len, hdrlen;
1135 const uint8_t *buf;
1136 int i;
1138 if (total_len)
1139 *total_len = 0;
1140 if (nlines)
1141 *nlines = 0;
1143 hdrlen = got_object_blob_get_hdrlen(blob);
1144 do {
1145 err = got_object_blob_read_block(&len, blob);
1146 if (err)
1147 return err;
1148 if (len == 0)
1149 break;
1150 if (total_len)
1151 *total_len += len;
1152 buf = got_object_blob_get_read_buf(blob);
1153 if (nlines) {
1154 for (i = 0; i < len; i++) {
1155 if (buf[i] == '\n')
1156 (*nlines)++;
1159 /* Skip blob object header first time around. */
1160 n = fwrite(buf + hdrlen, 1, len - hdrlen, outfile);
1161 if (n != len - hdrlen)
1162 return got_ferror(outfile, GOT_ERR_IO);
1163 hdrlen = 0;
1164 } while (len != 0);
1166 if (fflush(outfile) != 0)
1167 return got_error_from_errno();
1168 rewind(outfile);
1170 return NULL;
1173 static const struct got_error *
1174 request_packed_tag(struct got_tag_object **tag, struct got_pack *pack,
1175 int pack_idx, struct got_object_id *id)
1177 const struct got_error *err = NULL;
1179 err = got_privsep_send_tag_req(pack->privsep_child->ibuf, -1, id,
1180 pack_idx);
1181 if (err)
1182 return err;
1184 return got_privsep_recv_tag(tag, pack->privsep_child->ibuf);
1187 static const struct got_error *
1188 read_packed_tag_privsep(struct got_tag_object **tag,
1189 struct got_pack *pack, struct got_packidx *packidx, int idx,
1190 struct got_object_id *id)
1192 const struct got_error *err = NULL;
1194 if (pack->privsep_child)
1195 return request_packed_tag(tag, pack, idx, id);
1197 err = start_pack_privsep_child(pack, packidx);
1198 if (err)
1199 return err;
1201 return request_packed_tag(tag, pack, idx, id);
1204 static const struct got_error *
1205 request_tag(struct got_tag_object **tag, struct got_repository *repo,
1206 int fd)
1208 const struct got_error *err = NULL;
1209 struct imsgbuf *ibuf;
1211 ibuf = repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TAG].ibuf;
1213 err = got_privsep_send_tag_req(ibuf, fd, NULL, -1);
1214 if (err)
1215 return err;
1217 return got_privsep_recv_tag(tag, ibuf);
1220 static const struct got_error *
1221 read_tag_privsep(struct got_tag_object **tag, int obj_fd,
1222 struct got_repository *repo)
1224 int imsg_fds[2];
1225 pid_t pid;
1226 struct imsgbuf *ibuf;
1228 if (repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TAG].imsg_fd != -1)
1229 return request_tag(tag, repo, obj_fd);
1231 ibuf = calloc(1, sizeof(*ibuf));
1232 if (ibuf == NULL)
1233 return got_error_from_errno();
1235 if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_fds) == -1)
1236 return got_error_from_errno();
1238 pid = fork();
1239 if (pid == -1)
1240 return got_error_from_errno();
1241 else if (pid == 0) {
1242 exec_privsep_child(imsg_fds, GOT_PATH_PROG_READ_TAG,
1243 repo->path);
1244 /* not reached */
1247 if (close(imsg_fds[1]) != 0)
1248 return got_error_from_errno();
1249 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TAG].imsg_fd =
1250 imsg_fds[0];
1251 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TAG].pid = pid;
1252 imsg_init(ibuf, imsg_fds[0]);
1253 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TAG].ibuf = ibuf;
1255 return request_tag(tag, repo, obj_fd);
1258 static const struct got_error *
1259 open_tag(struct got_tag_object **tag, struct got_repository *repo,
1260 struct got_object_id *id, int check_cache)
1262 const struct got_error *err = NULL;
1263 struct got_packidx *packidx = NULL;
1264 int idx;
1265 char *path_packfile;
1267 if (check_cache) {
1268 *tag = got_repo_get_cached_tag(repo, id);
1269 if (*tag != NULL) {
1270 (*tag)->refcnt++;
1271 return NULL;
1273 } else
1274 *tag = NULL;
1276 err = got_repo_search_packidx(&packidx, &idx, repo, id);
1277 if (err == NULL) {
1278 struct got_pack *pack = NULL;
1280 err = get_packfile_path(&path_packfile, packidx);
1281 if (err)
1282 return err;
1284 pack = got_repo_get_cached_pack(repo, path_packfile);
1285 if (pack == NULL) {
1286 err = got_repo_cache_pack(&pack, repo, path_packfile,
1287 packidx);
1288 if (err)
1289 return err;
1291 err = read_packed_tag_privsep(tag, pack,
1292 packidx, idx, id);
1293 } else if (err->code == GOT_ERR_NO_OBJ) {
1294 int fd;
1296 err = open_loose_object(&fd, id, repo);
1297 if (err)
1298 return err;
1299 err = read_tag_privsep(tag, fd, repo);
1302 if (err == NULL) {
1303 (*tag)->refcnt++;
1304 err = got_repo_cache_tag(repo, id, *tag);
1307 return err;
1310 const struct got_error *
1311 got_object_open_as_tag(struct got_tag_object **tag,
1312 struct got_repository *repo, struct got_object_id *id)
1314 *tag = got_repo_get_cached_tag(repo, id);
1315 if (*tag != NULL) {
1316 (*tag)->refcnt++;
1317 return NULL;
1320 return open_tag(tag, repo, id, 0);
1323 const struct got_error *
1324 got_object_tag_open(struct got_tag_object **tag,
1325 struct got_repository *repo, struct got_object *obj)
1327 return open_tag(tag, repo, got_object_get_id(obj), 1);
1330 int
1331 got_object_tag_get_object_type(struct got_tag_object *tag)
1333 return tag->obj_type;
1336 struct got_object_id *
1337 got_object_tag_get_object_id(struct got_tag_object *tag)
1339 return &tag->id;
1342 static struct got_tree_entry *
1343 find_entry_by_name(struct got_tree_object *tree, const char *name, size_t len)
1345 struct got_tree_entry *te;
1347 /* Note that tree entries are sorted in strncmp() order. */
1348 SIMPLEQ_FOREACH(te, &tree->entries.head, entry) {
1349 int cmp = strncmp(te->name, name, len);
1350 if (cmp < 0)
1351 continue;
1352 if (cmp > 0)
1353 break;
1354 if (te->name[len] == '\0')
1355 return te;
1357 return NULL;
1360 const struct got_error *
1361 got_object_id_by_path(struct got_object_id **id, struct got_repository *repo,
1362 struct got_object_id *commit_id, const char *path)
1364 const struct got_error *err = NULL;
1365 struct got_commit_object *commit = NULL;
1366 struct got_tree_object *tree = NULL;
1367 struct got_tree_entry *te = NULL;
1368 const char *seg, *s;
1369 size_t seglen;
1371 *id = NULL;
1373 /* We are expecting an absolute in-repository path. */
1374 if (path[0] != '/')
1375 return got_error(GOT_ERR_NOT_ABSPATH);
1377 err = got_object_open_as_commit(&commit, repo, commit_id);
1378 if (err)
1379 goto done;
1381 /* Handle opening of root of commit's tree. */
1382 if (path[1] == '\0') {
1383 *id = got_object_id_dup(commit->tree_id);
1384 if (*id == NULL)
1385 err = got_error_from_errno();
1386 goto done;
1389 err = got_object_open_as_tree(&tree, repo, commit->tree_id);
1390 if (err)
1391 goto done;
1393 s = path;
1394 s++; /* skip leading '/' */
1395 seg = s;
1396 seglen = 0;
1397 while (*s) {
1398 struct got_tree_object *next_tree;
1400 if (*s != '/') {
1401 s++;
1402 seglen++;
1403 if (*s)
1404 continue;
1407 te = find_entry_by_name(tree, seg, seglen);
1408 if (te == NULL) {
1409 err = got_error(GOT_ERR_NO_TREE_ENTRY);
1410 goto done;
1413 if (*s == '\0')
1414 break;
1416 seg = s + 1;
1417 seglen = 0;
1418 s++;
1419 if (*s) {
1420 err = got_object_open_as_tree(&next_tree, repo,
1421 te->id);
1422 te = NULL;
1423 if (err)
1424 goto done;
1425 got_object_tree_close(tree);
1426 tree = next_tree;
1430 if (te) {
1431 *id = got_object_id_dup(te->id);
1432 if (*id == NULL)
1433 return got_error_from_errno();
1434 } else
1435 err = got_error(GOT_ERR_NO_TREE_ENTRY);
1436 done:
1437 if (commit)
1438 got_object_commit_close(commit);
1439 if (tree)
1440 got_object_tree_close(tree);
1441 return err;
1444 const struct got_error *
1445 got_object_tree_path_changed(int *changed,
1446 struct got_tree_object *tree01, struct got_tree_object *tree02,
1447 const char *path, struct got_repository *repo)
1449 const struct got_error *err = NULL;
1450 struct got_tree_object *tree1 = NULL, *tree2 = NULL;
1451 struct got_tree_entry *te1 = NULL, *te2 = NULL;
1452 const char *seg, *s;
1453 size_t seglen;
1455 *changed = 0;
1457 /* We are expecting an absolute in-repository path. */
1458 if (path[0] != '/')
1459 return got_error(GOT_ERR_NOT_ABSPATH);
1461 /* We not do support comparing the root path. */
1462 if (path[1] == '\0')
1463 return got_error(GOT_ERR_BAD_PATH);
1465 tree1 = tree01;
1466 tree2 = tree02;
1467 s = path;
1468 s++; /* skip leading '/' */
1469 seg = s;
1470 seglen = 0;
1471 while (*s) {
1472 struct got_tree_object *next_tree1, *next_tree2;
1474 if (*s != '/') {
1475 s++;
1476 seglen++;
1477 if (*s)
1478 continue;
1481 te1 = find_entry_by_name(tree1, seg, seglen);
1482 if (te1 == NULL) {
1483 err = got_error(GOT_ERR_NO_OBJ);
1484 goto done;
1487 te2 = find_entry_by_name(tree2, seg, seglen);
1488 if (te2 == NULL) {
1489 *changed = 1;
1490 goto done;
1493 if (te1->mode != te2->mode) {
1494 *changed = 1;
1495 goto done;
1498 if (got_object_id_cmp(te1->id, te2->id) == 0) {
1499 *changed = 0;
1500 goto done;
1503 if (*s == '\0') { /* final path element */
1504 *changed = 1;
1505 goto done;
1508 seg = s + 1;
1509 s++;
1510 seglen = 0;
1511 if (*s) {
1512 err = got_object_open_as_tree(&next_tree1, repo,
1513 te1->id);
1514 te1 = NULL;
1515 if (err)
1516 goto done;
1517 if (tree1 != tree01)
1518 got_object_tree_close(tree1);
1519 tree1 = next_tree1;
1521 err = got_object_open_as_tree(&next_tree2, repo,
1522 te2->id);
1523 te2 = NULL;
1524 if (err)
1525 goto done;
1526 if (tree2 != tree02)
1527 got_object_tree_close(tree2);
1528 tree2 = next_tree2;
1531 done:
1532 if (tree1 && tree1 != tree01)
1533 got_object_tree_close(tree1);
1534 if (tree2 && tree2 != tree02)
1535 got_object_tree_close(tree2);
1536 return err;