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 const struct got_error *
111 got_object_get_path(char **path, struct got_object_id *id,
112 struct got_repository *repo)
114 const struct got_error *err = NULL;
115 char *hex = NULL;
116 char *path_objects = got_repo_get_path_objects(repo);
118 *path = NULL;
120 if (path_objects == NULL)
121 return got_error_from_errno();
123 err = got_object_id_str(&hex, id);
124 if (err)
125 goto done;
127 if (asprintf(path, "%s/%.2x/%s", path_objects,
128 id->sha1[0], hex + 2) == -1)
129 err = got_error_from_errno();
131 done:
132 free(hex);
133 free(path_objects);
134 return err;
137 static const struct got_error *
138 open_loose_object(int *fd, struct got_object_id *id,
139 struct got_repository *repo)
141 const struct got_error *err = NULL;
142 char *path;
144 err = got_object_get_path(&path, id, repo);
145 if (err)
146 return err;
147 *fd = open(path, O_RDONLY | O_NOFOLLOW);
148 if (*fd == -1) {
149 err = got_error_from_errno();
150 goto done;
152 done:
153 free(path);
154 return err;
157 static const struct got_error *
158 get_packfile_path(char **path_packfile, struct got_packidx *packidx)
160 size_t size;
162 /* Packfile path contains ".pack" instead of ".idx", so add one byte. */
163 size = strlen(packidx->path_packidx) + 2;
164 if (size < GOT_PACKFILE_NAMELEN + 1)
165 return got_error(GOT_ERR_BAD_PATH);
167 *path_packfile = malloc(size);
168 if (*path_packfile == NULL)
169 return got_error_from_errno();
171 /* Copy up to and excluding ".idx". */
172 if (strlcpy(*path_packfile, packidx->path_packidx,
173 size - strlen(GOT_PACKIDX_SUFFIX) - 1) >= size)
174 return got_error(GOT_ERR_NO_SPACE);
176 if (strlcat(*path_packfile, GOT_PACKFILE_SUFFIX, size) >= size)
177 return got_error(GOT_ERR_NO_SPACE);
179 return NULL;
182 static void
183 exec_privsep_child(int imsg_fds[2], const char *path, const char *repo_path)
185 if (close(imsg_fds[0]) != 0) {
186 fprintf(stderr, "%s: %s\n", getprogname(),
187 strerror(errno));
188 _exit(1);
191 if (dup2(imsg_fds[1], GOT_IMSG_FD_CHILD) == -1) {
192 fprintf(stderr, "%s: %s\n", getprogname(),
193 strerror(errno));
194 _exit(1);
196 if (closefrom(GOT_IMSG_FD_CHILD + 1) == -1) {
197 fprintf(stderr, "%s: %s\n", getprogname(),
198 strerror(errno));
199 _exit(1);
202 if (execl(path, path, repo_path, (char *)NULL) == -1) {
203 fprintf(stderr, "%s: %s: %s\n", getprogname(), path,
204 strerror(errno));
205 _exit(1);
209 static const struct got_error *
210 request_packed_object(struct got_object **obj, struct got_pack *pack, int idx,
211 struct got_object_id *id)
213 const struct got_error *err = NULL;
214 struct imsgbuf *ibuf = pack->privsep_child->ibuf;
216 err = got_privsep_send_packed_obj_req(ibuf, idx, id);
217 if (err)
218 return err;
220 err = got_privsep_recv_obj(obj, ibuf);
221 if (err)
222 return err;
224 (*obj)->path_packfile = strdup(pack->path_packfile);
225 if ((*obj)->path_packfile == NULL) {
226 err = got_error_from_errno();
227 return err;
229 memcpy(&(*obj)->id, id, sizeof((*obj)->id));
231 return NULL;
234 static const struct got_error *
235 start_pack_privsep_child(struct got_pack *pack, struct got_packidx *packidx)
237 const struct got_error *err = NULL;
238 int imsg_fds[2];
239 pid_t pid;
240 struct imsgbuf *ibuf;
242 ibuf = calloc(1, sizeof(*ibuf));
243 if (ibuf == NULL)
244 return got_error_from_errno();
246 pack->privsep_child = calloc(1, sizeof(*pack->privsep_child));
247 if (pack->privsep_child == NULL) {
248 err = got_error_from_errno();
249 free(ibuf);
250 return err;
253 if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_fds) == -1) {
254 err = got_error_from_errno();
255 goto done;
258 pid = fork();
259 if (pid == -1) {
260 err = got_error_from_errno();
261 goto done;
262 } else if (pid == 0) {
263 exec_privsep_child(imsg_fds, GOT_PATH_PROG_READ_PACK,
264 pack->path_packfile);
265 /* not reached */
268 if (close(imsg_fds[1]) != 0)
269 return got_error_from_errno();
270 pack->privsep_child->imsg_fd = imsg_fds[0];
271 pack->privsep_child->pid = pid;
272 imsg_init(ibuf, imsg_fds[0]);
273 pack->privsep_child->ibuf = ibuf;
275 err = got_privsep_init_pack_child(ibuf, pack, packidx);
276 if (err) {
277 const struct got_error *child_err;
278 err = got_privsep_send_stop(pack->privsep_child->imsg_fd);
279 child_err = got_privsep_wait_for_child(
280 pack->privsep_child->pid);
281 if (child_err && err == NULL)
282 err = child_err;
284 done:
285 if (err) {
286 free(ibuf);
287 free(pack->privsep_child);
288 pack->privsep_child = NULL;
290 return err;
293 static const struct got_error *
294 read_packed_object_privsep(struct got_object **obj,
295 struct got_repository *repo, struct got_pack *pack,
296 struct got_packidx *packidx, int idx, struct got_object_id *id)
298 const struct got_error *err = NULL;
300 if (pack->privsep_child)
301 return request_packed_object(obj, pack, idx, id);
303 err = start_pack_privsep_child(pack, packidx);
304 if (err)
305 return err;
307 return request_packed_object(obj, pack, idx, id);
311 static const struct got_error *
312 open_packed_object(struct got_object **obj, struct got_object_id *id,
313 struct got_repository *repo)
315 const struct got_error *err = NULL;
316 struct got_pack *pack = NULL;
317 struct got_packidx *packidx = NULL;
318 int idx;
319 char *path_packfile;
321 err = got_repo_search_packidx(&packidx, &idx, repo, id);
322 if (err)
323 return err;
325 err = get_packfile_path(&path_packfile, packidx);
326 if (err)
327 return err;
329 pack = got_repo_get_cached_pack(repo, path_packfile);
330 if (pack == NULL) {
331 err = got_repo_cache_pack(&pack, repo, path_packfile, packidx);
332 if (err)
333 goto done;
336 err = read_packed_object_privsep(obj, repo, pack, packidx, idx, id);
337 if (err)
338 goto done;
340 err = got_repo_cache_pack(NULL, repo, (*obj)->path_packfile, packidx);
341 done:
342 free(path_packfile);
343 return err;
346 static const struct got_error *
347 request_object(struct got_object **obj, struct got_repository *repo, int fd)
349 const struct got_error *err = NULL;
350 struct imsgbuf *ibuf;
352 ibuf = repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_OBJECT].ibuf;
354 err = got_privsep_send_obj_req(ibuf, fd);
355 if (err)
356 return err;
358 return got_privsep_recv_obj(obj, ibuf);
361 static const struct got_error *
362 read_object_header_privsep(struct got_object **obj, struct got_repository *repo,
363 int obj_fd)
365 int imsg_fds[2];
366 pid_t pid;
367 struct imsgbuf *ibuf;
369 if (repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_OBJECT].imsg_fd != -1)
370 return request_object(obj, repo, obj_fd);
372 ibuf = calloc(1, sizeof(*ibuf));
373 if (ibuf == NULL)
374 return got_error_from_errno();
376 if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_fds) == -1)
377 return got_error_from_errno();
379 pid = fork();
380 if (pid == -1)
381 return got_error_from_errno();
382 else if (pid == 0) {
383 exec_privsep_child(imsg_fds, GOT_PATH_PROG_READ_OBJECT,
384 repo->path);
385 /* not reached */
388 if (close(imsg_fds[1]) != 0)
389 return got_error_from_errno();
390 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_OBJECT].imsg_fd =
391 imsg_fds[0];
392 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_OBJECT].pid = pid;
393 imsg_init(ibuf, imsg_fds[0]);
394 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_OBJECT].ibuf = ibuf;
396 return request_object(obj, repo, obj_fd);
400 const struct got_error *
401 got_object_open(struct got_object **obj, struct got_repository *repo,
402 struct got_object_id *id)
404 const struct got_error *err = NULL;
405 char *path;
406 int fd;
408 *obj = got_repo_get_cached_object(repo, id);
409 if (*obj != NULL) {
410 (*obj)->refcnt++;
411 return NULL;
414 err = open_packed_object(obj, id, repo);
415 if (err && err->code != GOT_ERR_NO_OBJ)
416 return err;
417 if (*obj) {
418 (*obj)->refcnt++;
419 return got_repo_cache_object(repo, id, *obj);
422 err = got_object_get_path(&path, id, repo);
423 if (err)
424 return err;
426 fd = open(path, O_RDONLY | O_NOFOLLOW);
427 if (fd == -1) {
428 if (errno == ENOENT)
429 err = got_error_no_obj(id);
430 else
431 err = got_error_from_errno();
432 goto done;
433 } else {
434 err = read_object_header_privsep(obj, repo, fd);
435 if (err)
436 goto done;
437 memcpy((*obj)->id.sha1, id->sha1, SHA1_DIGEST_LENGTH);
440 (*obj)->refcnt++;
441 err = got_repo_cache_object(repo, id, *obj);
442 done:
443 free(path);
444 return err;
448 const struct got_error *
449 got_object_open_by_id_str(struct got_object **obj, struct got_repository *repo,
450 const char *id_str)
452 struct got_object_id id;
454 if (!got_parse_sha1_digest(id.sha1, id_str))
455 return got_error(GOT_ERR_BAD_OBJ_ID_STR);
457 return got_object_open(obj, repo, &id);
460 const struct got_error *
461 got_object_resolve_id_str(struct got_object_id **id,
462 struct got_repository *repo, const char *id_str)
464 const struct got_error *err = NULL;
465 struct got_object *obj;
467 err = got_object_open_by_id_str(&obj, repo, id_str);
468 if (err)
469 return err;
471 *id = got_object_id_dup(got_object_get_id(obj));
472 got_object_close(obj);
473 if (*id == NULL)
474 return got_error_from_errno();
476 return NULL;
479 static const struct got_error *
480 request_packed_commit(struct got_commit_object **commit, struct got_pack *pack,
481 int pack_idx, struct got_object_id *id)
483 const struct got_error *err = NULL;
485 err = got_privsep_send_commit_req(pack->privsep_child->ibuf, -1, id,
486 pack_idx);
487 if (err)
488 return err;
490 return got_privsep_recv_commit(commit, pack->privsep_child->ibuf);
493 static const struct got_error *
494 read_packed_commit_privsep(struct got_commit_object **commit,
495 struct got_pack *pack, struct got_packidx *packidx, int idx,
496 struct got_object_id *id)
498 const struct got_error *err = NULL;
500 if (pack->privsep_child)
501 return request_packed_commit(commit, pack, idx, id);
503 err = start_pack_privsep_child(pack, packidx);
504 if (err)
505 return err;
507 return request_packed_commit(commit, pack, idx, id);
510 static const struct got_error *
511 request_commit(struct got_commit_object **commit, struct got_repository *repo,
512 int fd)
514 const struct got_error *err = NULL;
515 struct imsgbuf *ibuf;
517 ibuf = repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_COMMIT].ibuf;
519 err = got_privsep_send_commit_req(ibuf, fd, NULL, -1);
520 if (err)
521 return err;
523 return got_privsep_recv_commit(commit, ibuf);
526 static const struct got_error *
527 read_commit_privsep(struct got_commit_object **commit, int obj_fd,
528 struct got_repository *repo)
530 int imsg_fds[2];
531 pid_t pid;
532 struct imsgbuf *ibuf;
534 if (repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_COMMIT].imsg_fd != -1)
535 return request_commit(commit, repo, obj_fd);
537 ibuf = calloc(1, sizeof(*ibuf));
538 if (ibuf == NULL)
539 return got_error_from_errno();
541 if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_fds) == -1)
542 return got_error_from_errno();
544 pid = fork();
545 if (pid == -1)
546 return got_error_from_errno();
547 else if (pid == 0) {
548 exec_privsep_child(imsg_fds, GOT_PATH_PROG_READ_COMMIT,
549 repo->path);
550 /* not reached */
553 if (close(imsg_fds[1]) != 0)
554 return got_error_from_errno();
555 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_COMMIT].imsg_fd =
556 imsg_fds[0];
557 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_COMMIT].pid = pid;
558 imsg_init(ibuf, imsg_fds[0]);
559 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_COMMIT].ibuf = ibuf;
561 return request_commit(commit, repo, obj_fd);
565 static const struct got_error *
566 open_commit(struct got_commit_object **commit,
567 struct got_repository *repo, struct got_object_id *id, int check_cache)
569 const struct got_error *err = NULL;
570 struct got_packidx *packidx = NULL;
571 int idx;
572 char *path_packfile;
574 if (check_cache) {
575 *commit = got_repo_get_cached_commit(repo, id);
576 if (*commit != NULL) {
577 (*commit)->refcnt++;
578 return NULL;
580 } else
581 *commit = NULL;
583 err = got_repo_search_packidx(&packidx, &idx, repo, id);
584 if (err == NULL) {
585 struct got_pack *pack = NULL;
587 err = get_packfile_path(&path_packfile, packidx);
588 if (err)
589 return err;
591 pack = got_repo_get_cached_pack(repo, path_packfile);
592 if (pack == NULL) {
593 err = got_repo_cache_pack(&pack, repo, path_packfile,
594 packidx);
595 if (err)
596 return err;
598 err = read_packed_commit_privsep(commit, pack,
599 packidx, idx, id);
600 } else if (err->code == GOT_ERR_NO_OBJ) {
601 int fd;
603 err = open_loose_object(&fd, id, repo);
604 if (err)
605 return err;
606 err = read_commit_privsep(commit, fd, repo);
609 if (err == NULL) {
610 (*commit)->refcnt++;
611 err = got_repo_cache_commit(repo, id, *commit);
614 return err;
617 const struct got_error *
618 got_object_open_as_commit(struct got_commit_object **commit,
619 struct got_repository *repo, struct got_object_id *id)
621 *commit = got_repo_get_cached_commit(repo, id);
622 if (*commit != NULL) {
623 (*commit)->refcnt++;
624 return NULL;
627 return open_commit(commit, repo, id, 0);
630 const struct got_error *
631 got_object_commit_open(struct got_commit_object **commit,
632 struct got_repository *repo, struct got_object *obj)
634 return open_commit(commit, repo, got_object_get_id(obj), 1);
637 const struct got_error *
638 got_object_qid_alloc(struct got_object_qid **qid, struct got_object_id *id)
640 const struct got_error *err = NULL;
642 *qid = calloc(1, sizeof(**qid));
643 if (*qid == NULL)
644 return got_error_from_errno();
646 (*qid)->id = got_object_id_dup(id);
647 if ((*qid)->id == NULL) {
648 err = got_error_from_errno();
649 got_object_qid_free(*qid);
650 *qid = NULL;
651 return err;
654 return NULL;
657 static const struct got_error *
658 request_packed_tree(struct got_tree_object **tree, struct got_pack *pack,
659 int pack_idx, struct got_object_id *id)
661 const struct got_error *err = NULL;
663 err = got_privsep_send_tree_req(pack->privsep_child->ibuf, -1, id,
664 pack_idx);
665 if (err)
666 return err;
668 return got_privsep_recv_tree(tree, pack->privsep_child->ibuf);
671 static const struct got_error *
672 read_packed_tree_privsep(struct got_tree_object **tree,
673 struct got_pack *pack, struct got_packidx *packidx, int idx,
674 struct got_object_id *id)
676 const struct got_error *err = NULL;
678 if (pack->privsep_child)
679 return request_packed_tree(tree, pack, idx, id);
681 err = start_pack_privsep_child(pack, packidx);
682 if (err)
683 return err;
685 return request_packed_tree(tree, pack, idx, id);
688 static const struct got_error *
689 request_tree(struct got_tree_object **tree, struct got_repository *repo,
690 int fd)
692 const struct got_error *err = NULL;
693 struct imsgbuf *ibuf;
695 ibuf = repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TREE].ibuf;
697 err = got_privsep_send_tree_req(ibuf, fd, NULL, -1);
698 if (err)
699 return err;
701 return got_privsep_recv_tree(tree, ibuf);
704 const struct got_error *
705 read_tree_privsep(struct got_tree_object **tree, int obj_fd,
706 struct got_repository *repo)
708 int imsg_fds[2];
709 pid_t pid;
710 struct imsgbuf *ibuf;
712 if (repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TREE].imsg_fd != -1)
713 return request_tree(tree, repo, obj_fd);
715 ibuf = calloc(1, sizeof(*ibuf));
716 if (ibuf == NULL)
717 return got_error_from_errno();
719 if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_fds) == -1)
720 return got_error_from_errno();
722 pid = fork();
723 if (pid == -1)
724 return got_error_from_errno();
725 else if (pid == 0) {
726 exec_privsep_child(imsg_fds, GOT_PATH_PROG_READ_TREE,
727 repo->path);
728 /* not reached */
731 if (close(imsg_fds[1]) != 0)
732 return got_error_from_errno();
733 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TREE].imsg_fd =
734 imsg_fds[0];
735 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TREE].pid = pid;
736 imsg_init(ibuf, imsg_fds[0]);
737 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TREE].ibuf = ibuf;
740 return request_tree(tree, repo, obj_fd);
743 static const struct got_error *
744 open_tree(struct got_tree_object **tree, struct got_repository *repo,
745 struct got_object_id *id, int check_cache)
747 const struct got_error *err = NULL;
748 struct got_packidx *packidx = NULL;
749 int idx;
750 char *path_packfile;
752 if (check_cache) {
753 *tree = got_repo_get_cached_tree(repo, id);
754 if (*tree != NULL) {
755 (*tree)->refcnt++;
756 return NULL;
758 } else
759 *tree = NULL;
761 err = got_repo_search_packidx(&packidx, &idx, repo, id);
762 if (err == NULL) {
763 struct got_pack *pack = NULL;
765 err = get_packfile_path(&path_packfile, packidx);
766 if (err)
767 return err;
769 pack = got_repo_get_cached_pack(repo, path_packfile);
770 if (pack == NULL) {
771 err = got_repo_cache_pack(&pack, repo, path_packfile,
772 packidx);
773 if (err)
774 return err;
776 err = read_packed_tree_privsep(tree, pack,
777 packidx, idx, id);
778 } else if (err->code == GOT_ERR_NO_OBJ) {
779 int fd;
781 err = open_loose_object(&fd, id, repo);
782 if (err)
783 return err;
784 err = read_tree_privsep(tree, fd, repo);
787 if (err == NULL) {
788 (*tree)->refcnt++;
789 err = got_repo_cache_tree(repo, id, *tree);
792 return err;
795 const struct got_error *
796 got_object_open_as_tree(struct got_tree_object **tree,
797 struct got_repository *repo, struct got_object_id *id)
799 *tree = got_repo_get_cached_tree(repo, id);
800 if (*tree != NULL) {
801 (*tree)->refcnt++;
802 return NULL;
805 return open_tree(tree, repo, id, 0);
808 const struct got_error *
809 got_object_tree_open(struct got_tree_object **tree,
810 struct got_repository *repo, struct got_object *obj)
812 return open_tree(tree, repo, got_object_get_id(obj), 1);
815 const struct got_tree_entries *
816 got_object_tree_get_entries(struct got_tree_object *tree)
818 return &tree->entries;
821 static const struct got_error *
822 request_packed_blob(uint8_t **outbuf, size_t *size, size_t *hdrlen, int outfd,
823 struct got_pack *pack, struct got_packidx *packidx, int idx,
824 struct got_object_id *id)
826 const struct got_error *err = NULL;
827 int outfd_child;
828 int basefd, accumfd; /* temporary files for delta application */
830 basefd = got_opentempfd();
831 if (basefd == -1)
832 return got_error_from_errno();
833 accumfd = got_opentempfd();
834 if (accumfd == -1)
835 return got_error_from_errno();
837 outfd_child = dup(outfd);
838 if (outfd_child == -1)
839 return got_error_from_errno();
841 err = got_privsep_send_blob_req(pack->privsep_child->ibuf, -1, id, idx);
842 if (err)
843 return err;
845 err = got_privsep_send_blob_outfd(pack->privsep_child->ibuf,
846 outfd_child);
847 if (err) {
848 close(basefd);
849 close(accumfd);
850 return err;
853 err = got_privsep_send_tmpfd(pack->privsep_child->ibuf,
854 basefd);
855 if (err) {
856 close(accumfd);
857 return err;
860 err = got_privsep_send_tmpfd(pack->privsep_child->ibuf,
861 accumfd);
862 if (err)
863 return err;
865 err = got_privsep_recv_blob(outbuf, size, hdrlen,
866 pack->privsep_child->ibuf);
867 if (err)
868 return err;
870 if (lseek(outfd, SEEK_SET, 0) == -1)
871 err = got_error_from_errno();
873 return err;
876 static const struct got_error *
877 read_packed_blob_privsep(uint8_t **outbuf, size_t *size, size_t *hdrlen,
878 int outfd, struct got_pack *pack, struct got_packidx *packidx, int idx,
879 struct got_object_id *id)
881 const struct got_error *err = NULL;
883 if (pack->privsep_child == NULL) {
884 err = start_pack_privsep_child(pack, packidx);
885 if (err)
886 return err;
889 return request_packed_blob(outbuf, size, hdrlen, outfd, pack, packidx,
890 idx, id);
893 static const struct got_error *
894 request_blob(uint8_t **outbuf, size_t *size, size_t *hdrlen, int outfd,
895 int infd, struct imsgbuf *ibuf)
897 const struct got_error *err = NULL;
898 int outfd_child;
900 outfd_child = dup(outfd);
901 if (outfd_child == -1)
902 return got_error_from_errno();
904 err = got_privsep_send_blob_req(ibuf, infd, NULL, -1);
905 if (err)
906 return err;
908 err = got_privsep_send_blob_outfd(ibuf, outfd_child);
909 if (err)
910 return err;
912 err = got_privsep_recv_blob(outbuf, size, hdrlen, ibuf);
913 if (err)
914 return err;
916 if (lseek(outfd, SEEK_SET, 0) == -1)
917 return got_error_from_errno();
919 return err;
922 static const struct got_error *
923 read_blob_privsep(uint8_t **outbuf, size_t *size, size_t *hdrlen,
924 int outfd, int infd, struct got_repository *repo)
926 int imsg_fds[2];
927 pid_t pid;
928 struct imsgbuf *ibuf;
930 if (repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_BLOB].imsg_fd != -1) {
931 ibuf = repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_BLOB].ibuf;
932 return request_blob(outbuf, size, hdrlen, outfd, infd, ibuf);
935 ibuf = calloc(1, sizeof(*ibuf));
936 if (ibuf == NULL)
937 return got_error_from_errno();
939 if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_fds) == -1)
940 return got_error_from_errno();
942 pid = fork();
943 if (pid == -1)
944 return got_error_from_errno();
945 else if (pid == 0) {
946 exec_privsep_child(imsg_fds, GOT_PATH_PROG_READ_BLOB,
947 repo->path);
948 /* not reached */
951 if (close(imsg_fds[1]) != 0)
952 return got_error_from_errno();
953 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_BLOB].imsg_fd =
954 imsg_fds[0];
955 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_BLOB].pid = pid;
956 imsg_init(ibuf, imsg_fds[0]);
957 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_BLOB].ibuf = ibuf;
959 return request_blob(outbuf, size, hdrlen, outfd, infd, ibuf);
962 static const struct got_error *
963 open_blob(struct got_blob_object **blob, struct got_repository *repo,
964 struct got_object_id *id, size_t blocksize)
966 const struct got_error *err = NULL;
967 struct got_packidx *packidx = NULL;
968 int idx;
969 char *path_packfile;
970 uint8_t *outbuf;
971 int outfd;
972 size_t size, hdrlen;
973 struct stat sb;
975 *blob = calloc(1, sizeof(**blob));
976 if (*blob == NULL)
977 return got_error_from_errno();
979 outfd = got_opentempfd();
980 if (outfd == -1)
981 return got_error_from_errno();
983 (*blob)->read_buf = malloc(blocksize);
984 if ((*blob)->read_buf == NULL) {
985 err = got_error_from_errno();
986 goto done;
989 err = got_repo_search_packidx(&packidx, &idx, repo, id);
990 if (err == NULL) {
991 struct got_pack *pack = NULL;
993 err = get_packfile_path(&path_packfile, packidx);
994 if (err)
995 goto done;
997 pack = got_repo_get_cached_pack(repo, path_packfile);
998 if (pack == NULL) {
999 err = got_repo_cache_pack(&pack, repo, path_packfile,
1000 packidx);
1001 if (err)
1002 goto done;
1004 err = read_packed_blob_privsep(&outbuf, &size, &hdrlen, outfd,
1005 pack, packidx, idx, id);
1006 } else if (err->code == GOT_ERR_NO_OBJ) {
1007 int infd;
1009 err = open_loose_object(&infd, id, repo);
1010 if (err)
1011 goto done;
1012 err = read_blob_privsep(&outbuf, &size, &hdrlen, outfd, infd,
1013 repo);
1015 if (err)
1016 goto done;
1018 if (hdrlen > size) {
1019 err = got_error(GOT_ERR_BAD_OBJ_HDR);
1020 goto done;
1023 if (outbuf) {
1024 if (close(outfd) != 0 && err == NULL)
1025 err = got_error_from_errno();
1026 outfd = -1;
1027 (*blob)->f = fmemopen(outbuf, size, "rb");
1028 if ((*blob)->f == NULL) {
1029 err = got_error_from_errno();
1030 free(outbuf);
1031 goto done;
1033 (*blob)->data = outbuf;
1034 } else {
1035 if (fstat(outfd, &sb) == -1) {
1036 err = got_error_from_errno();
1037 goto done;
1040 if (sb.st_size != size) {
1041 err = got_error(GOT_ERR_PRIVSEP_LEN);
1042 goto done;
1045 (*blob)->f = fdopen(outfd, "rb");
1046 if ((*blob)->f == NULL) {
1047 err = got_error_from_errno();
1048 close(outfd);
1049 outfd = -1;
1050 goto done;
1054 (*blob)->hdrlen = hdrlen;
1055 (*blob)->blocksize = blocksize;
1056 memcpy(&(*blob)->id.sha1, id->sha1, SHA1_DIGEST_LENGTH);
1058 done:
1059 if (err) {
1060 if (*blob) {
1061 got_object_blob_close(*blob);
1062 *blob = NULL;
1063 } else if (outfd != -1)
1064 close(outfd);
1066 return err;
1069 const struct got_error *
1070 got_object_open_as_blob(struct got_blob_object **blob,
1071 struct got_repository *repo, struct got_object_id *id,
1072 size_t blocksize)
1074 return open_blob(blob, repo, id, blocksize);
1077 const struct got_error *
1078 got_object_blob_open(struct got_blob_object **blob,
1079 struct got_repository *repo, struct got_object *obj, size_t blocksize)
1081 return open_blob(blob, repo, got_object_get_id(obj), blocksize);
1084 const struct got_error *
1085 got_object_blob_close(struct got_blob_object *blob)
1087 const struct got_error *err = NULL;
1088 free(blob->read_buf);
1089 if (blob->f && fclose(blob->f) != 0)
1090 err = got_error_from_errno();
1091 free(blob->data);
1092 free(blob);
1093 return err;
1096 char *
1097 got_object_blob_id_str(struct got_blob_object *blob, char *buf, size_t size)
1099 return got_sha1_digest_to_str(blob->id.sha1, buf, size);
1102 size_t
1103 got_object_blob_get_hdrlen(struct got_blob_object *blob)
1105 return blob->hdrlen;
1108 const uint8_t *
1109 got_object_blob_get_read_buf(struct got_blob_object *blob)
1111 return blob->read_buf;
1114 const struct got_error *
1115 got_object_blob_read_block(size_t *outlenp, struct got_blob_object *blob)
1117 size_t n;
1119 n = fread(blob->read_buf, 1, blob->blocksize, blob->f);
1120 if (n == 0 && ferror(blob->f))
1121 return got_ferror(blob->f, GOT_ERR_IO);
1122 *outlenp = n;
1123 return NULL;
1126 const struct got_error *
1127 got_object_blob_dump_to_file(size_t *total_len, int *nlines,
1128 FILE *outfile, struct got_blob_object *blob)
1130 const struct got_error *err = NULL;
1131 size_t n, len, hdrlen;
1132 const uint8_t *buf;
1133 int i;
1135 if (total_len)
1136 *total_len = 0;
1137 if (nlines)
1138 *nlines = 0;
1140 hdrlen = got_object_blob_get_hdrlen(blob);
1141 do {
1142 err = got_object_blob_read_block(&len, blob);
1143 if (err)
1144 return err;
1145 if (len == 0)
1146 break;
1147 if (total_len)
1148 *total_len += len;
1149 buf = got_object_blob_get_read_buf(blob);
1150 if (nlines) {
1151 for (i = 0; i < len; i++) {
1152 if (buf[i] == '\n')
1153 (*nlines)++;
1156 /* Skip blob object header first time around. */
1157 n = fwrite(buf + hdrlen, 1, len - hdrlen, outfile);
1158 if (n != len - hdrlen)
1159 return got_ferror(outfile, GOT_ERR_IO);
1160 hdrlen = 0;
1161 } while (len != 0);
1163 if (fflush(outfile) != 0)
1164 return got_error_from_errno();
1165 rewind(outfile);
1167 return NULL;
1170 static const struct got_error *
1171 request_packed_tag(struct got_tag_object **tag, struct got_pack *pack,
1172 int pack_idx, struct got_object_id *id)
1174 const struct got_error *err = NULL;
1176 err = got_privsep_send_tag_req(pack->privsep_child->ibuf, -1, id,
1177 pack_idx);
1178 if (err)
1179 return err;
1181 return got_privsep_recv_tag(tag, pack->privsep_child->ibuf);
1184 static const struct got_error *
1185 read_packed_tag_privsep(struct got_tag_object **tag,
1186 struct got_pack *pack, struct got_packidx *packidx, int idx,
1187 struct got_object_id *id)
1189 const struct got_error *err = NULL;
1191 if (pack->privsep_child)
1192 return request_packed_tag(tag, pack, idx, id);
1194 err = start_pack_privsep_child(pack, packidx);
1195 if (err)
1196 return err;
1198 return request_packed_tag(tag, pack, idx, id);
1201 static const struct got_error *
1202 request_tag(struct got_tag_object **tag, struct got_repository *repo,
1203 int fd)
1205 const struct got_error *err = NULL;
1206 struct imsgbuf *ibuf;
1208 ibuf = repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TAG].ibuf;
1210 err = got_privsep_send_tag_req(ibuf, fd, NULL, -1);
1211 if (err)
1212 return err;
1214 return got_privsep_recv_tag(tag, ibuf);
1217 static const struct got_error *
1218 read_tag_privsep(struct got_tag_object **tag, int obj_fd,
1219 struct got_repository *repo)
1221 int imsg_fds[2];
1222 pid_t pid;
1223 struct imsgbuf *ibuf;
1225 if (repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TAG].imsg_fd != -1)
1226 return request_tag(tag, repo, obj_fd);
1228 ibuf = calloc(1, sizeof(*ibuf));
1229 if (ibuf == NULL)
1230 return got_error_from_errno();
1232 if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_fds) == -1)
1233 return got_error_from_errno();
1235 pid = fork();
1236 if (pid == -1)
1237 return got_error_from_errno();
1238 else if (pid == 0) {
1239 exec_privsep_child(imsg_fds, GOT_PATH_PROG_READ_TAG,
1240 repo->path);
1241 /* not reached */
1244 if (close(imsg_fds[1]) != 0)
1245 return got_error_from_errno();
1246 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TAG].imsg_fd =
1247 imsg_fds[0];
1248 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TAG].pid = pid;
1249 imsg_init(ibuf, imsg_fds[0]);
1250 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TAG].ibuf = ibuf;
1252 return request_tag(tag, repo, obj_fd);
1255 static const struct got_error *
1256 open_tag(struct got_tag_object **tag, struct got_repository *repo,
1257 struct got_object_id *id, int check_cache)
1259 const struct got_error *err = NULL;
1260 struct got_packidx *packidx = NULL;
1261 int idx;
1262 char *path_packfile;
1264 if (check_cache) {
1265 *tag = got_repo_get_cached_tag(repo, id);
1266 if (*tag != NULL) {
1267 (*tag)->refcnt++;
1268 return NULL;
1270 } else
1271 *tag = NULL;
1273 err = got_repo_search_packidx(&packidx, &idx, repo, id);
1274 if (err == NULL) {
1275 struct got_pack *pack = NULL;
1277 err = get_packfile_path(&path_packfile, packidx);
1278 if (err)
1279 return err;
1281 pack = got_repo_get_cached_pack(repo, path_packfile);
1282 if (pack == NULL) {
1283 err = got_repo_cache_pack(&pack, repo, path_packfile,
1284 packidx);
1285 if (err)
1286 return err;
1288 err = read_packed_tag_privsep(tag, pack,
1289 packidx, idx, id);
1290 } else if (err->code == GOT_ERR_NO_OBJ) {
1291 int fd;
1293 err = open_loose_object(&fd, id, repo);
1294 if (err)
1295 return err;
1296 err = read_tag_privsep(tag, fd, repo);
1299 if (err == NULL) {
1300 (*tag)->refcnt++;
1301 err = got_repo_cache_tag(repo, id, *tag);
1304 return err;
1307 const struct got_error *
1308 got_object_open_as_tag(struct got_tag_object **tag,
1309 struct got_repository *repo, struct got_object_id *id)
1311 *tag = got_repo_get_cached_tag(repo, id);
1312 if (*tag != NULL) {
1313 (*tag)->refcnt++;
1314 return NULL;
1317 return open_tag(tag, repo, id, 0);
1320 const struct got_error *
1321 got_object_tag_open(struct got_tag_object **tag,
1322 struct got_repository *repo, struct got_object *obj)
1324 return open_tag(tag, repo, got_object_get_id(obj), 1);
1327 int
1328 got_object_tag_get_object_type(struct got_tag_object *tag)
1330 return tag->obj_type;
1333 struct got_object_id *
1334 got_object_tag_get_object_id(struct got_tag_object *tag)
1336 return &tag->id;
1339 static struct got_tree_entry *
1340 find_entry_by_name(struct got_tree_object *tree, const char *name, size_t len)
1342 struct got_tree_entry *te;
1344 /* Note that tree entries are sorted in strncmp() order. */
1345 SIMPLEQ_FOREACH(te, &tree->entries.head, entry) {
1346 int cmp = strncmp(te->name, name, len);
1347 if (cmp < 0)
1348 continue;
1349 if (cmp > 0)
1350 break;
1351 if (te->name[len] == '\0')
1352 return te;
1354 return NULL;
1357 const struct got_tree_entry *
1358 got_object_tree_find_entry(struct got_tree_object *tree, const char *name)
1360 return find_entry_by_name(tree, name, strlen(name));
1363 const struct got_error *
1364 got_object_id_by_path(struct got_object_id **id, struct got_repository *repo,
1365 struct got_object_id *commit_id, const char *path)
1367 const struct got_error *err = NULL;
1368 struct got_commit_object *commit = NULL;
1369 struct got_tree_object *tree = NULL;
1370 struct got_tree_entry *te = NULL;
1371 const char *seg, *s;
1372 size_t seglen;
1374 *id = NULL;
1376 /* We are expecting an absolute in-repository path. */
1377 if (path[0] != '/')
1378 return got_error(GOT_ERR_NOT_ABSPATH);
1380 err = got_object_open_as_commit(&commit, repo, commit_id);
1381 if (err)
1382 goto done;
1384 /* Handle opening of root of commit's tree. */
1385 if (path[1] == '\0') {
1386 *id = got_object_id_dup(commit->tree_id);
1387 if (*id == NULL)
1388 err = got_error_from_errno();
1389 goto done;
1392 err = got_object_open_as_tree(&tree, repo, commit->tree_id);
1393 if (err)
1394 goto done;
1396 s = path;
1397 s++; /* skip leading '/' */
1398 seg = s;
1399 seglen = 0;
1400 while (*s) {
1401 struct got_tree_object *next_tree;
1403 if (*s != '/') {
1404 s++;
1405 seglen++;
1406 if (*s)
1407 continue;
1410 te = find_entry_by_name(tree, seg, seglen);
1411 if (te == NULL) {
1412 err = got_error(GOT_ERR_NO_TREE_ENTRY);
1413 goto done;
1416 if (*s == '\0')
1417 break;
1419 seg = s + 1;
1420 seglen = 0;
1421 s++;
1422 if (*s) {
1423 err = got_object_open_as_tree(&next_tree, repo,
1424 te->id);
1425 te = NULL;
1426 if (err)
1427 goto done;
1428 got_object_tree_close(tree);
1429 tree = next_tree;
1433 if (te) {
1434 *id = got_object_id_dup(te->id);
1435 if (*id == NULL)
1436 return got_error_from_errno();
1437 } else
1438 err = got_error(GOT_ERR_NO_TREE_ENTRY);
1439 done:
1440 if (commit)
1441 got_object_commit_close(commit);
1442 if (tree)
1443 got_object_tree_close(tree);
1444 return err;
1447 const struct got_error *
1448 got_object_tree_path_changed(int *changed,
1449 struct got_tree_object *tree01, struct got_tree_object *tree02,
1450 const char *path, struct got_repository *repo)
1452 const struct got_error *err = NULL;
1453 struct got_tree_object *tree1 = NULL, *tree2 = NULL;
1454 struct got_tree_entry *te1 = NULL, *te2 = NULL;
1455 const char *seg, *s;
1456 size_t seglen;
1458 *changed = 0;
1460 /* We are expecting an absolute in-repository path. */
1461 if (path[0] != '/')
1462 return got_error(GOT_ERR_NOT_ABSPATH);
1464 /* We not do support comparing the root path. */
1465 if (path[1] == '\0')
1466 return got_error(GOT_ERR_BAD_PATH);
1468 tree1 = tree01;
1469 tree2 = tree02;
1470 s = path;
1471 s++; /* skip leading '/' */
1472 seg = s;
1473 seglen = 0;
1474 while (*s) {
1475 struct got_tree_object *next_tree1, *next_tree2;
1477 if (*s != '/') {
1478 s++;
1479 seglen++;
1480 if (*s)
1481 continue;
1484 te1 = find_entry_by_name(tree1, seg, seglen);
1485 if (te1 == NULL) {
1486 err = got_error(GOT_ERR_NO_OBJ);
1487 goto done;
1490 te2 = find_entry_by_name(tree2, seg, seglen);
1491 if (te2 == NULL) {
1492 *changed = 1;
1493 goto done;
1496 if (te1->mode != te2->mode) {
1497 *changed = 1;
1498 goto done;
1501 if (got_object_id_cmp(te1->id, te2->id) == 0) {
1502 *changed = 0;
1503 goto done;
1506 if (*s == '\0') { /* final path element */
1507 *changed = 1;
1508 goto done;
1511 seg = s + 1;
1512 s++;
1513 seglen = 0;
1514 if (*s) {
1515 err = got_object_open_as_tree(&next_tree1, repo,
1516 te1->id);
1517 te1 = NULL;
1518 if (err)
1519 goto done;
1520 if (tree1 != tree01)
1521 got_object_tree_close(tree1);
1522 tree1 = next_tree1;
1524 err = got_object_open_as_tree(&next_tree2, repo,
1525 te2->id);
1526 te2 = NULL;
1527 if (err)
1528 goto done;
1529 if (tree2 != tree02)
1530 got_object_tree_close(tree2);
1531 tree2 = next_tree2;
1534 done:
1535 if (tree1 && tree1 != tree01)
1536 got_object_tree_close(tree1);
1537 if (tree2 && tree2 != tree02)
1538 got_object_tree_close(tree2);
1539 return err;
1542 const struct got_error *
1543 got_object_tree_entry_dup(struct got_tree_entry **new_te,
1544 struct got_tree_entry *te)
1546 const struct got_error *err = NULL;
1548 *new_te = calloc(1, sizeof(**new_te));
1549 if (*new_te == NULL)
1550 return got_error_from_errno();
1552 (*new_te)->mode = te->mode;
1553 (*new_te)->name = strdup(te->name);
1554 if ((*new_te)->name == NULL) {
1555 err = got_error_from_errno();
1556 got_object_tree_entry_close(*new_te);
1557 return err;
1560 (*new_te)->id = got_object_id_dup(te->id);
1561 if ((*new_te)->id == NULL) {
1562 err = got_error_from_errno();
1563 got_object_tree_entry_close(*new_te);
1564 return err;
1567 return NULL;