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 close(imsg_fds[0]);
186 if (dup2(imsg_fds[1], GOT_IMSG_FD_CHILD) == -1) {
187 fprintf(stderr, "%s: %s\n", getprogname(),
188 strerror(errno));
189 _exit(1);
191 if (closefrom(GOT_IMSG_FD_CHILD + 1) == -1) {
192 fprintf(stderr, "%s: %s\n", getprogname(),
193 strerror(errno));
194 _exit(1);
197 if (execl(path, path, repo_path, (char *)NULL) == -1) {
198 fprintf(stderr, "%s: %s: %s\n", getprogname(), path,
199 strerror(errno));
200 _exit(1);
204 static const struct got_error *
205 request_packed_object(struct got_object **obj, struct got_pack *pack, int idx,
206 struct got_object_id *id)
208 const struct got_error *err = NULL;
209 struct imsgbuf *ibuf = pack->privsep_child->ibuf;
211 err = got_privsep_send_packed_obj_req(ibuf, idx, id);
212 if (err)
213 return err;
215 err = got_privsep_recv_obj(obj, ibuf);
216 if (err)
217 return err;
219 (*obj)->path_packfile = strdup(pack->path_packfile);
220 if ((*obj)->path_packfile == NULL) {
221 err = got_error_from_errno();
222 return err;
224 memcpy(&(*obj)->id, id, sizeof((*obj)->id));
226 return NULL;
229 static const struct got_error *
230 start_pack_privsep_child(struct got_pack *pack, struct got_packidx *packidx)
232 const struct got_error *err = NULL;
233 int imsg_fds[2];
234 pid_t pid;
235 struct imsgbuf *ibuf;
237 ibuf = calloc(1, sizeof(*ibuf));
238 if (ibuf == NULL)
239 return got_error_from_errno();
241 pack->privsep_child = calloc(1, sizeof(*pack->privsep_child));
242 if (pack->privsep_child == NULL) {
243 err = got_error_from_errno();
244 free(ibuf);
245 return err;
248 if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_fds) == -1) {
249 err = got_error_from_errno();
250 goto done;
253 pid = fork();
254 if (pid == -1) {
255 err = got_error_from_errno();
256 goto done;
257 } else if (pid == 0) {
258 exec_privsep_child(imsg_fds, GOT_PATH_PROG_READ_PACK,
259 pack->path_packfile);
260 /* not reached */
263 close(imsg_fds[1]);
264 pack->privsep_child->imsg_fd = imsg_fds[0];
265 pack->privsep_child->pid = pid;
266 imsg_init(ibuf, imsg_fds[0]);
267 pack->privsep_child->ibuf = ibuf;
269 err = got_privsep_init_pack_child(ibuf, pack, packidx);
270 if (err) {
271 const struct got_error *child_err;
272 err = got_privsep_send_stop(pack->privsep_child->imsg_fd);
273 child_err = got_privsep_wait_for_child(
274 pack->privsep_child->pid);
275 if (child_err && err == NULL)
276 err = child_err;
278 done:
279 if (err) {
280 free(ibuf);
281 free(pack->privsep_child);
282 pack->privsep_child = NULL;
284 return err;
287 static const struct got_error *
288 read_packed_object_privsep(struct got_object **obj,
289 struct got_repository *repo, struct got_pack *pack,
290 struct got_packidx *packidx, int idx, struct got_object_id *id)
292 const struct got_error *err = NULL;
294 if (pack->privsep_child)
295 return request_packed_object(obj, pack, idx, id);
297 err = start_pack_privsep_child(pack, packidx);
298 if (err)
299 return err;
301 return request_packed_object(obj, pack, idx, id);
305 static const struct got_error *
306 open_packed_object(struct got_object **obj, struct got_object_id *id,
307 struct got_repository *repo)
309 const struct got_error *err = NULL;
310 struct got_pack *pack = NULL;
311 struct got_packidx *packidx = NULL;
312 int idx;
313 char *path_packfile;
315 err = got_repo_search_packidx(&packidx, &idx, repo, id);
316 if (err)
317 return err;
319 err = get_packfile_path(&path_packfile, packidx);
320 if (err)
321 return err;
323 pack = got_repo_get_cached_pack(repo, path_packfile);
324 if (pack == NULL) {
325 err = got_repo_cache_pack(&pack, repo, path_packfile, packidx);
326 if (err)
327 goto done;
330 err = read_packed_object_privsep(obj, repo, pack, packidx, idx, id);
331 if (err)
332 goto done;
334 err = got_repo_cache_pack(NULL, repo, (*obj)->path_packfile, packidx);
335 done:
336 free(path_packfile);
337 return err;
340 static const struct got_error *
341 request_object(struct got_object **obj, struct got_repository *repo, int fd)
343 const struct got_error *err = NULL;
344 struct imsgbuf *ibuf;
346 ibuf = repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_OBJECT].ibuf;
348 err = got_privsep_send_obj_req(ibuf, fd);
349 if (err)
350 return err;
352 return got_privsep_recv_obj(obj, ibuf);
355 static const struct got_error *
356 read_object_header_privsep(struct got_object **obj, struct got_repository *repo,
357 int obj_fd)
359 int imsg_fds[2];
360 pid_t pid;
361 struct imsgbuf *ibuf;
363 if (repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_OBJECT].imsg_fd != -1)
364 return request_object(obj, repo, obj_fd);
366 ibuf = calloc(1, sizeof(*ibuf));
367 if (ibuf == NULL)
368 return got_error_from_errno();
370 if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_fds) == -1)
371 return got_error_from_errno();
373 pid = fork();
374 if (pid == -1)
375 return got_error_from_errno();
376 else if (pid == 0) {
377 exec_privsep_child(imsg_fds, GOT_PATH_PROG_READ_OBJECT,
378 repo->path);
379 /* not reached */
382 close(imsg_fds[1]);
383 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_OBJECT].imsg_fd =
384 imsg_fds[0];
385 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_OBJECT].pid = pid;
386 imsg_init(ibuf, imsg_fds[0]);
387 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_OBJECT].ibuf = ibuf;
389 return request_object(obj, repo, obj_fd);
393 const struct got_error *
394 got_object_open(struct got_object **obj, struct got_repository *repo,
395 struct got_object_id *id)
397 const struct got_error *err = NULL;
398 char *path;
399 int fd;
401 *obj = got_repo_get_cached_object(repo, id);
402 if (*obj != NULL) {
403 (*obj)->refcnt++;
404 return NULL;
407 err = open_packed_object(obj, id, repo);
408 if (err && err->code != GOT_ERR_NO_OBJ)
409 return err;
410 if (*obj) {
411 (*obj)->refcnt++;
412 return got_repo_cache_object(repo, id, *obj);
415 err = object_path(&path, id, repo);
416 if (err)
417 return err;
419 fd = open(path, O_RDONLY | O_NOFOLLOW, GOT_DEFAULT_FILE_MODE);
420 if (fd == -1) {
421 if (errno == ENOENT)
422 err = got_error_no_obj(id);
423 else
424 err = got_error_from_errno();
425 goto done;
426 } else {
427 err = read_object_header_privsep(obj, repo, fd);
428 if (err)
429 goto done;
430 memcpy((*obj)->id.sha1, id->sha1, SHA1_DIGEST_LENGTH);
433 (*obj)->refcnt++;
434 err = got_repo_cache_object(repo, id, *obj);
435 done:
436 free(path);
437 if (fd != -1)
438 close(fd);
439 return err;
443 const struct got_error *
444 got_object_open_by_id_str(struct got_object **obj, struct got_repository *repo,
445 const char *id_str)
447 struct got_object_id id;
449 if (!got_parse_sha1_digest(id.sha1, id_str))
450 return got_error(GOT_ERR_BAD_OBJ_ID_STR);
452 return got_object_open(obj, repo, &id);
455 const struct got_error *
456 got_object_resolve_id_str(struct got_object_id **id,
457 struct got_repository *repo, const char *id_str)
459 const struct got_error *err = NULL;
460 struct got_object *obj;
462 err = got_object_open_by_id_str(&obj, repo, id_str);
463 if (err)
464 return err;
466 *id = got_object_id_dup(got_object_get_id(obj));
467 got_object_close(obj);
468 if (*id == NULL)
469 return got_error_from_errno();
471 return NULL;
474 static const struct got_error *
475 request_packed_commit(struct got_commit_object **commit, struct got_pack *pack,
476 int pack_idx, struct got_object_id *id)
478 const struct got_error *err = NULL;
480 err = got_privsep_send_commit_req(pack->privsep_child->ibuf, -1, id,
481 pack_idx);
482 if (err)
483 return err;
485 return got_privsep_recv_commit(commit, pack->privsep_child->ibuf);
488 static const struct got_error *
489 read_packed_commit_privsep(struct got_commit_object **commit,
490 struct got_pack *pack, struct got_packidx *packidx, int idx,
491 struct got_object_id *id)
493 const struct got_error *err = NULL;
495 if (pack->privsep_child)
496 return request_packed_commit(commit, pack, idx, id);
498 err = start_pack_privsep_child(pack, packidx);
499 if (err)
500 return err;
502 return request_packed_commit(commit, pack, idx, id);
505 static const struct got_error *
506 request_commit(struct got_commit_object **commit, struct got_repository *repo,
507 int fd)
509 const struct got_error *err = NULL;
510 struct imsgbuf *ibuf;
512 ibuf = repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_COMMIT].ibuf;
514 err = got_privsep_send_commit_req(ibuf, fd, NULL, -1);
515 if (err)
516 return err;
518 return got_privsep_recv_commit(commit, ibuf);
521 static const struct got_error *
522 read_commit_privsep(struct got_commit_object **commit, int obj_fd,
523 struct got_repository *repo)
525 int imsg_fds[2];
526 pid_t pid;
527 struct imsgbuf *ibuf;
529 if (repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_COMMIT].imsg_fd != -1)
530 return request_commit(commit, repo, obj_fd);
532 ibuf = calloc(1, sizeof(*ibuf));
533 if (ibuf == NULL)
534 return got_error_from_errno();
536 if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_fds) == -1)
537 return got_error_from_errno();
539 pid = fork();
540 if (pid == -1)
541 return got_error_from_errno();
542 else if (pid == 0) {
543 exec_privsep_child(imsg_fds, GOT_PATH_PROG_READ_COMMIT,
544 repo->path);
545 /* not reached */
548 close(imsg_fds[1]);
549 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_COMMIT].imsg_fd =
550 imsg_fds[0];
551 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_COMMIT].pid = pid;
552 imsg_init(ibuf, imsg_fds[0]);
553 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_COMMIT].ibuf = ibuf;
555 return request_commit(commit, repo, obj_fd);
559 static const struct got_error *
560 open_commit(struct got_commit_object **commit,
561 struct got_repository *repo, struct got_object_id *id, int check_cache)
563 const struct got_error *err = NULL;
564 struct got_packidx *packidx = NULL;
565 int idx;
566 char *path_packfile;
568 if (check_cache) {
569 *commit = got_repo_get_cached_commit(repo, id);
570 if (*commit != NULL) {
571 (*commit)->refcnt++;
572 return NULL;
574 } else
575 *commit = NULL;
577 err = got_repo_search_packidx(&packidx, &idx, repo, id);
578 if (err == NULL) {
579 struct got_pack *pack = NULL;
581 err = get_packfile_path(&path_packfile, packidx);
582 if (err)
583 return err;
585 pack = got_repo_get_cached_pack(repo, path_packfile);
586 if (pack == NULL) {
587 err = got_repo_cache_pack(&pack, repo, path_packfile,
588 packidx);
589 if (err)
590 return err;
592 err = read_packed_commit_privsep(commit, pack,
593 packidx, idx, id);
594 } else if (err->code == GOT_ERR_NO_OBJ) {
595 int fd;
597 err = open_loose_object(&fd, id, repo);
598 if (err)
599 return err;
600 err = read_commit_privsep(commit, fd, repo);
601 close(fd);
604 if (err == NULL) {
605 (*commit)->refcnt++;
606 err = got_repo_cache_commit(repo, id, *commit);
609 return err;
612 const struct got_error *
613 got_object_open_as_commit(struct got_commit_object **commit,
614 struct got_repository *repo, struct got_object_id *id)
616 *commit = got_repo_get_cached_commit(repo, id);
617 if (*commit != NULL) {
618 (*commit)->refcnt++;
619 return NULL;
622 return open_commit(commit, repo, id, 0);
625 const struct got_error *
626 got_object_commit_open(struct got_commit_object **commit,
627 struct got_repository *repo, struct got_object *obj)
629 return open_commit(commit, repo, got_object_get_id(obj), 1);
632 const struct got_error *
633 got_object_qid_alloc(struct got_object_qid **qid, struct got_object_id *id)
635 const struct got_error *err = NULL;
637 *qid = calloc(1, sizeof(**qid));
638 if (*qid == NULL)
639 return got_error_from_errno();
641 (*qid)->id = got_object_id_dup(id);
642 if ((*qid)->id == NULL) {
643 err = got_error_from_errno();
644 got_object_qid_free(*qid);
645 *qid = NULL;
646 return err;
649 return NULL;
652 static const struct got_error *
653 request_packed_tree(struct got_tree_object **tree, struct got_pack *pack,
654 int pack_idx, struct got_object_id *id)
656 const struct got_error *err = NULL;
658 err = got_privsep_send_tree_req(pack->privsep_child->ibuf, -1, id,
659 pack_idx);
660 if (err)
661 return err;
663 return got_privsep_recv_tree(tree, pack->privsep_child->ibuf);
666 static const struct got_error *
667 read_packed_tree_privsep(struct got_tree_object **tree,
668 struct got_pack *pack, struct got_packidx *packidx, int idx,
669 struct got_object_id *id)
671 const struct got_error *err = NULL;
673 if (pack->privsep_child)
674 return request_packed_tree(tree, pack, idx, id);
676 err = start_pack_privsep_child(pack, packidx);
677 if (err)
678 return err;
680 return request_packed_tree(tree, pack, idx, id);
683 static const struct got_error *
684 request_tree(struct got_tree_object **tree, struct got_repository *repo,
685 int fd)
687 const struct got_error *err = NULL;
688 struct imsgbuf *ibuf;
690 ibuf = repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TREE].ibuf;
692 err = got_privsep_send_tree_req(ibuf, fd, NULL, -1);
693 if (err)
694 return err;
696 return got_privsep_recv_tree(tree, ibuf);
699 const struct got_error *
700 read_tree_privsep(struct got_tree_object **tree, int obj_fd,
701 struct got_repository *repo)
703 int imsg_fds[2];
704 pid_t pid;
705 struct imsgbuf *ibuf;
707 if (repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TREE].imsg_fd != -1)
708 return request_tree(tree, repo, obj_fd);
710 ibuf = calloc(1, sizeof(*ibuf));
711 if (ibuf == NULL)
712 return got_error_from_errno();
714 if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_fds) == -1)
715 return got_error_from_errno();
717 pid = fork();
718 if (pid == -1)
719 return got_error_from_errno();
720 else if (pid == 0) {
721 exec_privsep_child(imsg_fds, GOT_PATH_PROG_READ_TREE,
722 repo->path);
723 /* not reached */
726 close(imsg_fds[1]);
728 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TREE].imsg_fd =
729 imsg_fds[0];
730 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TREE].pid = pid;
731 imsg_init(ibuf, imsg_fds[0]);
732 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TREE].ibuf = ibuf;
735 return request_tree(tree, repo, obj_fd);
738 static const struct got_error *
739 open_tree(struct got_tree_object **tree, struct got_repository *repo,
740 struct got_object_id *id, int check_cache)
742 const struct got_error *err = NULL;
743 struct got_packidx *packidx = NULL;
744 int idx;
745 char *path_packfile;
747 if (check_cache) {
748 *tree = got_repo_get_cached_tree(repo, id);
749 if (*tree != NULL) {
750 (*tree)->refcnt++;
751 return NULL;
753 } else
754 *tree = NULL;
756 err = got_repo_search_packidx(&packidx, &idx, repo, id);
757 if (err == NULL) {
758 struct got_pack *pack = NULL;
760 err = get_packfile_path(&path_packfile, packidx);
761 if (err)
762 return err;
764 pack = got_repo_get_cached_pack(repo, path_packfile);
765 if (pack == NULL) {
766 err = got_repo_cache_pack(&pack, repo, path_packfile,
767 packidx);
768 if (err)
769 return err;
771 err = read_packed_tree_privsep(tree, pack,
772 packidx, idx, id);
773 } else if (err->code == GOT_ERR_NO_OBJ) {
774 int fd;
776 err = open_loose_object(&fd, id, repo);
777 if (err)
778 return err;
779 err = read_tree_privsep(tree, fd, repo);
780 close(fd);
783 if (err == NULL) {
784 (*tree)->refcnt++;
785 err = got_repo_cache_tree(repo, id, *tree);
788 return err;
791 const struct got_error *
792 got_object_open_as_tree(struct got_tree_object **tree,
793 struct got_repository *repo, struct got_object_id *id)
795 *tree = got_repo_get_cached_tree(repo, id);
796 if (*tree != NULL) {
797 (*tree)->refcnt++;
798 return NULL;
801 return open_tree(tree, repo, id, 0);
804 const struct got_error *
805 got_object_tree_open(struct got_tree_object **tree,
806 struct got_repository *repo, struct got_object *obj)
808 return open_tree(tree, repo, got_object_get_id(obj), 1);
811 const struct got_tree_entries *
812 got_object_tree_get_entries(struct got_tree_object *tree)
814 return &tree->entries;
817 static const struct got_error *
818 request_packed_blob(uint8_t **outbuf, size_t *size, size_t *hdrlen, int outfd,
819 struct got_pack *pack, struct got_packidx *packidx, int idx,
820 struct got_object_id *id)
822 const struct got_error *err = NULL;
823 int outfd_child;
824 int basefd, accumfd; /* temporary files for delta application */
826 basefd = got_opentempfd();
827 if (basefd == -1)
828 return got_error_from_errno();
829 accumfd = got_opentempfd();
830 if (accumfd == -1)
831 return got_error_from_errno();
833 outfd_child = dup(outfd);
834 if (outfd_child == -1)
835 return got_error_from_errno();
837 err = got_privsep_send_blob_req(pack->privsep_child->ibuf, -1, id, idx);
838 if (err)
839 return err;
841 err = got_privsep_send_blob_outfd(pack->privsep_child->ibuf,
842 outfd_child);
843 if (err) {
844 close(outfd_child);
845 return err;
847 err = got_privsep_send_tmpfd(pack->privsep_child->ibuf,
848 basefd);
849 if (err) {
850 close(basefd);
851 close(accumfd);
852 close(outfd_child);
853 return err;
856 err = got_privsep_send_tmpfd(pack->privsep_child->ibuf,
857 accumfd);
858 if (err) {
859 close(accumfd);
860 close(outfd_child);
861 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 close(outfd_child);
910 return err;
913 err = got_privsep_recv_blob(outbuf, size, hdrlen, ibuf);
914 if (err)
915 return err;
917 if (lseek(outfd, SEEK_SET, 0) == -1)
918 return got_error_from_errno();
920 return err;
923 static const struct got_error *
924 read_blob_privsep(uint8_t **outbuf, size_t *size, size_t *hdrlen,
925 int outfd, int infd, struct got_repository *repo)
927 int imsg_fds[2];
928 pid_t pid;
929 struct imsgbuf *ibuf;
931 if (repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_BLOB].imsg_fd != -1) {
932 ibuf = repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_BLOB].ibuf;
933 return request_blob(outbuf, size, hdrlen, outfd, infd, ibuf);
936 ibuf = calloc(1, sizeof(*ibuf));
937 if (ibuf == NULL)
938 return got_error_from_errno();
940 if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_fds) == -1)
941 return got_error_from_errno();
943 pid = fork();
944 if (pid == -1)
945 return got_error_from_errno();
946 else if (pid == 0) {
947 exec_privsep_child(imsg_fds, GOT_PATH_PROG_READ_BLOB,
948 repo->path);
949 /* not reached */
952 close(imsg_fds[1]);
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);
1014 close(infd);
1016 if (err)
1017 goto done;
1019 if (hdrlen > size) {
1020 err = got_error(GOT_ERR_BAD_OBJ_HDR);
1021 goto done;
1024 if (outbuf) {
1025 close(outfd);
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 if ((*blob)->f)
1062 fclose((*blob)->f);
1063 free((*blob)->read_buf);
1064 free((*blob)->data);
1065 free(*blob);
1066 *blob = NULL;
1067 } else if (outfd != -1)
1068 close(outfd);
1070 return err;
1073 const struct got_error *
1074 got_object_open_as_blob(struct got_blob_object **blob,
1075 struct got_repository *repo, struct got_object_id *id,
1076 size_t blocksize)
1078 return open_blob(blob, repo, id, blocksize);
1081 const struct got_error *
1082 got_object_blob_open(struct got_blob_object **blob,
1083 struct got_repository *repo, struct got_object *obj, size_t blocksize)
1085 return open_blob(blob, repo, got_object_get_id(obj), blocksize);
1088 void
1089 got_object_blob_close(struct got_blob_object *blob)
1091 free(blob->read_buf);
1092 fclose(blob->f);
1093 free(blob->data);
1094 free(blob);
1097 char *
1098 got_object_blob_id_str(struct got_blob_object *blob, char *buf, size_t size)
1100 return got_sha1_digest_to_str(blob->id.sha1, buf, size);
1103 size_t
1104 got_object_blob_get_hdrlen(struct got_blob_object *blob)
1106 return blob->hdrlen;
1109 const uint8_t *
1110 got_object_blob_get_read_buf(struct got_blob_object *blob)
1112 return blob->read_buf;
1115 const struct got_error *
1116 got_object_blob_read_block(size_t *outlenp, struct got_blob_object *blob)
1118 size_t n;
1120 n = fread(blob->read_buf, 1, blob->blocksize, blob->f);
1121 if (n == 0 && ferror(blob->f))
1122 return got_ferror(blob->f, GOT_ERR_IO);
1123 *outlenp = n;
1124 return NULL;
1127 const struct got_error *
1128 got_object_blob_dump_to_file(size_t *total_len, int *nlines,
1129 FILE *outfile, struct got_blob_object *blob)
1131 const struct got_error *err = NULL;
1132 size_t n, len, hdrlen;
1133 const uint8_t *buf;
1134 int i;
1136 if (total_len)
1137 *total_len = 0;
1138 if (nlines)
1139 *nlines = 0;
1141 hdrlen = got_object_blob_get_hdrlen(blob);
1142 do {
1143 err = got_object_blob_read_block(&len, blob);
1144 if (err)
1145 return err;
1146 if (len == 0)
1147 break;
1148 if (total_len)
1149 *total_len += len;
1150 buf = got_object_blob_get_read_buf(blob);
1151 if (nlines) {
1152 for (i = 0; i < len; i++) {
1153 if (buf[i] == '\n')
1154 (*nlines)++;
1157 /* Skip blob object header first time around. */
1158 n = fwrite(buf + hdrlen, 1, len - hdrlen, outfile);
1159 if (n != len - hdrlen)
1160 return got_ferror(outfile, GOT_ERR_IO);
1161 hdrlen = 0;
1162 } while (len != 0);
1164 fflush(outfile);
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 close(imsg_fds[1]);
1245 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TAG].imsg_fd =
1246 imsg_fds[0];
1247 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TAG].pid = pid;
1248 imsg_init(ibuf, imsg_fds[0]);
1249 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TAG].ibuf = ibuf;
1251 return request_tag(tag, repo, obj_fd);
1254 static const struct got_error *
1255 open_tag(struct got_tag_object **tag, struct got_repository *repo,
1256 struct got_object_id *id, int check_cache)
1258 const struct got_error *err = NULL;
1259 struct got_packidx *packidx = NULL;
1260 int idx;
1261 char *path_packfile;
1263 if (check_cache) {
1264 *tag = got_repo_get_cached_tag(repo, id);
1265 if (*tag != NULL) {
1266 (*tag)->refcnt++;
1267 return NULL;
1269 } else
1270 *tag = NULL;
1272 err = got_repo_search_packidx(&packidx, &idx, repo, id);
1273 if (err == NULL) {
1274 struct got_pack *pack = NULL;
1276 err = get_packfile_path(&path_packfile, packidx);
1277 if (err)
1278 return err;
1280 pack = got_repo_get_cached_pack(repo, path_packfile);
1281 if (pack == NULL) {
1282 err = got_repo_cache_pack(&pack, repo, path_packfile,
1283 packidx);
1284 if (err)
1285 return err;
1287 err = read_packed_tag_privsep(tag, pack,
1288 packidx, idx, id);
1289 } else if (err->code == GOT_ERR_NO_OBJ) {
1290 int fd;
1292 err = open_loose_object(&fd, id, repo);
1293 if (err)
1294 return err;
1295 err = read_tag_privsep(tag, fd, repo);
1296 close(fd);
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 static struct got_tree_entry *
1328 find_entry_by_name(struct got_tree_object *tree, const char *name, size_t len)
1330 struct got_tree_entry *te;
1332 /* Note that tree entries are sorted in strncmp() order. */
1333 SIMPLEQ_FOREACH(te, &tree->entries.head, entry) {
1334 int cmp = strncmp(te->name, name, len);
1335 if (cmp < 0)
1336 continue;
1337 if (cmp > 0)
1338 break;
1339 if (te->name[len] == '\0')
1340 return te;
1342 return NULL;
1345 const struct got_error *
1346 got_object_id_by_path(struct got_object_id **id, struct got_repository *repo,
1347 struct got_object_id *commit_id, const char *path)
1349 const struct got_error *err = NULL;
1350 struct got_commit_object *commit = NULL;
1351 struct got_tree_object *tree = NULL;
1352 struct got_tree_entry *te = NULL;
1353 const char *seg, *s;
1354 size_t seglen;
1356 *id = NULL;
1358 /* We are expecting an absolute in-repository path. */
1359 if (path[0] != '/')
1360 return got_error(GOT_ERR_NOT_ABSPATH);
1362 err = got_object_open_as_commit(&commit, repo, commit_id);
1363 if (err)
1364 goto done;
1366 /* Handle opening of root of commit's tree. */
1367 if (path[1] == '\0') {
1368 *id = got_object_id_dup(commit->tree_id);
1369 if (*id == NULL)
1370 err = got_error_from_errno();
1371 goto done;
1374 err = got_object_open_as_tree(&tree, repo, commit->tree_id);
1375 if (err)
1376 goto done;
1378 s = path;
1379 s++; /* skip leading '/' */
1380 seg = s;
1381 seglen = 0;
1382 while (*s) {
1383 struct got_tree_object *next_tree;
1385 if (*s != '/') {
1386 s++;
1387 seglen++;
1388 if (*s)
1389 continue;
1392 te = find_entry_by_name(tree, seg, seglen);
1393 if (te == NULL) {
1394 err = got_error(GOT_ERR_NO_TREE_ENTRY);
1395 goto done;
1398 if (*s == '\0')
1399 break;
1401 seg = s + 1;
1402 seglen = 0;
1403 s++;
1404 if (*s) {
1405 err = got_object_open_as_tree(&next_tree, repo,
1406 te->id);
1407 te = NULL;
1408 if (err)
1409 goto done;
1410 got_object_tree_close(tree);
1411 tree = next_tree;
1415 if (te) {
1416 *id = got_object_id_dup(te->id);
1417 if (*id == NULL)
1418 return got_error_from_errno();
1419 } else
1420 err = got_error(GOT_ERR_NO_TREE_ENTRY);
1421 done:
1422 if (commit)
1423 got_object_commit_close(commit);
1424 if (tree)
1425 got_object_tree_close(tree);
1426 return err;
1429 const struct got_error *
1430 got_object_tree_path_changed(int *changed,
1431 struct got_tree_object *tree01, struct got_tree_object *tree02,
1432 const char *path, struct got_repository *repo)
1434 const struct got_error *err = NULL;
1435 struct got_tree_object *tree1 = NULL, *tree2 = NULL;
1436 struct got_tree_entry *te1 = NULL, *te2 = NULL;
1437 const char *seg, *s;
1438 size_t seglen;
1440 *changed = 0;
1442 /* We are expecting an absolute in-repository path. */
1443 if (path[0] != '/')
1444 return got_error(GOT_ERR_NOT_ABSPATH);
1446 /* We not do support comparing the root path. */
1447 if (path[1] == '\0')
1448 return got_error(GOT_ERR_BAD_PATH);
1450 tree1 = tree01;
1451 tree2 = tree02;
1452 s = path;
1453 s++; /* skip leading '/' */
1454 seg = s;
1455 seglen = 0;
1456 while (*s) {
1457 struct got_tree_object *next_tree1, *next_tree2;
1459 if (*s != '/') {
1460 s++;
1461 seglen++;
1462 if (*s)
1463 continue;
1466 te1 = find_entry_by_name(tree1, seg, seglen);
1467 if (te1 == NULL) {
1468 err = got_error(GOT_ERR_NO_OBJ);
1469 goto done;
1472 te2 = find_entry_by_name(tree2, seg, seglen);
1473 if (te2 == NULL) {
1474 *changed = 1;
1475 goto done;
1478 if (te1->mode != te2->mode) {
1479 *changed = 1;
1480 goto done;
1483 if (got_object_id_cmp(te1->id, te2->id) == 0) {
1484 *changed = 0;
1485 goto done;
1488 if (*s == '\0') { /* final path element */
1489 *changed = 1;
1490 goto done;
1493 seg = s + 1;
1494 s++;
1495 seglen = 0;
1496 if (*s) {
1497 err = got_object_open_as_tree(&next_tree1, repo,
1498 te1->id);
1499 te1 = NULL;
1500 if (err)
1501 goto done;
1502 if (tree1 != tree01)
1503 got_object_tree_close(tree1);
1504 tree1 = next_tree1;
1506 err = got_object_open_as_tree(&next_tree2, repo,
1507 te2->id);
1508 te2 = NULL;
1509 if (err)
1510 goto done;
1511 if (tree2 != tree02)
1512 got_object_tree_close(tree2);
1513 tree2 = next_tree2;
1516 done:
1517 if (tree1 && tree1 != tree01)
1518 got_object_tree_close(tree1);
1519 if (tree2 && tree2 != tree02)
1520 got_object_tree_close(tree2);
1521 return err;