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 if (fd != -1 && close(fd) != 0 && errno != EBADF && err == NULL)
444 err = got_error_from_errno();
445 return err;
449 const struct got_error *
450 got_object_open_by_id_str(struct got_object **obj, struct got_repository *repo,
451 const char *id_str)
453 struct got_object_id id;
455 if (!got_parse_sha1_digest(id.sha1, id_str))
456 return got_error(GOT_ERR_BAD_OBJ_ID_STR);
458 return got_object_open(obj, repo, &id);
461 const struct got_error *
462 got_object_resolve_id_str(struct got_object_id **id,
463 struct got_repository *repo, const char *id_str)
465 const struct got_error *err = NULL;
466 struct got_object *obj;
468 err = got_object_open_by_id_str(&obj, repo, id_str);
469 if (err)
470 return err;
472 *id = got_object_id_dup(got_object_get_id(obj));
473 got_object_close(obj);
474 if (*id == NULL)
475 return got_error_from_errno();
477 return NULL;
480 static const struct got_error *
481 request_packed_commit(struct got_commit_object **commit, struct got_pack *pack,
482 int pack_idx, struct got_object_id *id)
484 const struct got_error *err = NULL;
486 err = got_privsep_send_commit_req(pack->privsep_child->ibuf, -1, id,
487 pack_idx);
488 if (err)
489 return err;
491 return got_privsep_recv_commit(commit, pack->privsep_child->ibuf);
494 static const struct got_error *
495 read_packed_commit_privsep(struct got_commit_object **commit,
496 struct got_pack *pack, struct got_packidx *packidx, int idx,
497 struct got_object_id *id)
499 const struct got_error *err = NULL;
501 if (pack->privsep_child)
502 return request_packed_commit(commit, pack, idx, id);
504 err = start_pack_privsep_child(pack, packidx);
505 if (err)
506 return err;
508 return request_packed_commit(commit, pack, idx, id);
511 static const struct got_error *
512 request_commit(struct got_commit_object **commit, struct got_repository *repo,
513 int fd)
515 const struct got_error *err = NULL;
516 struct imsgbuf *ibuf;
518 ibuf = repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_COMMIT].ibuf;
520 err = got_privsep_send_commit_req(ibuf, fd, NULL, -1);
521 if (err)
522 return err;
524 return got_privsep_recv_commit(commit, ibuf);
527 static const struct got_error *
528 read_commit_privsep(struct got_commit_object **commit, int obj_fd,
529 struct got_repository *repo)
531 int imsg_fds[2];
532 pid_t pid;
533 struct imsgbuf *ibuf;
535 if (repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_COMMIT].imsg_fd != -1)
536 return request_commit(commit, repo, obj_fd);
538 ibuf = calloc(1, sizeof(*ibuf));
539 if (ibuf == NULL)
540 return got_error_from_errno();
542 if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_fds) == -1)
543 return got_error_from_errno();
545 pid = fork();
546 if (pid == -1)
547 return got_error_from_errno();
548 else if (pid == 0) {
549 exec_privsep_child(imsg_fds, GOT_PATH_PROG_READ_COMMIT,
550 repo->path);
551 /* not reached */
554 if (close(imsg_fds[1]) != 0)
555 return got_error_from_errno();
556 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_COMMIT].imsg_fd =
557 imsg_fds[0];
558 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_COMMIT].pid = pid;
559 imsg_init(ibuf, imsg_fds[0]);
560 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_COMMIT].ibuf = ibuf;
562 return request_commit(commit, repo, obj_fd);
566 static const struct got_error *
567 open_commit(struct got_commit_object **commit,
568 struct got_repository *repo, struct got_object_id *id, int check_cache)
570 const struct got_error *err = NULL;
571 struct got_packidx *packidx = NULL;
572 int idx;
573 char *path_packfile;
575 if (check_cache) {
576 *commit = got_repo_get_cached_commit(repo, id);
577 if (*commit != NULL) {
578 (*commit)->refcnt++;
579 return NULL;
581 } else
582 *commit = NULL;
584 err = got_repo_search_packidx(&packidx, &idx, repo, id);
585 if (err == NULL) {
586 struct got_pack *pack = NULL;
588 err = get_packfile_path(&path_packfile, packidx);
589 if (err)
590 return err;
592 pack = got_repo_get_cached_pack(repo, path_packfile);
593 if (pack == NULL) {
594 err = got_repo_cache_pack(&pack, repo, path_packfile,
595 packidx);
596 if (err)
597 return err;
599 err = read_packed_commit_privsep(commit, pack,
600 packidx, idx, id);
601 } else if (err->code == GOT_ERR_NO_OBJ) {
602 int fd;
604 err = open_loose_object(&fd, id, repo);
605 if (err)
606 return err;
607 err = read_commit_privsep(commit, fd, repo);
608 if (close(fd) != 0 && errno != EBADF && err == NULL)
609 err = got_error_from_errno();
612 if (err == NULL) {
613 (*commit)->refcnt++;
614 err = got_repo_cache_commit(repo, id, *commit);
617 return err;
620 const struct got_error *
621 got_object_open_as_commit(struct got_commit_object **commit,
622 struct got_repository *repo, struct got_object_id *id)
624 *commit = got_repo_get_cached_commit(repo, id);
625 if (*commit != NULL) {
626 (*commit)->refcnt++;
627 return NULL;
630 return open_commit(commit, repo, id, 0);
633 const struct got_error *
634 got_object_commit_open(struct got_commit_object **commit,
635 struct got_repository *repo, struct got_object *obj)
637 return open_commit(commit, repo, got_object_get_id(obj), 1);
640 const struct got_error *
641 got_object_qid_alloc(struct got_object_qid **qid, struct got_object_id *id)
643 const struct got_error *err = NULL;
645 *qid = calloc(1, sizeof(**qid));
646 if (*qid == NULL)
647 return got_error_from_errno();
649 (*qid)->id = got_object_id_dup(id);
650 if ((*qid)->id == NULL) {
651 err = got_error_from_errno();
652 got_object_qid_free(*qid);
653 *qid = NULL;
654 return err;
657 return NULL;
660 static const struct got_error *
661 request_packed_tree(struct got_tree_object **tree, struct got_pack *pack,
662 int pack_idx, struct got_object_id *id)
664 const struct got_error *err = NULL;
666 err = got_privsep_send_tree_req(pack->privsep_child->ibuf, -1, id,
667 pack_idx);
668 if (err)
669 return err;
671 return got_privsep_recv_tree(tree, pack->privsep_child->ibuf);
674 static const struct got_error *
675 read_packed_tree_privsep(struct got_tree_object **tree,
676 struct got_pack *pack, struct got_packidx *packidx, int idx,
677 struct got_object_id *id)
679 const struct got_error *err = NULL;
681 if (pack->privsep_child)
682 return request_packed_tree(tree, pack, idx, id);
684 err = start_pack_privsep_child(pack, packidx);
685 if (err)
686 return err;
688 return request_packed_tree(tree, pack, idx, id);
691 static const struct got_error *
692 request_tree(struct got_tree_object **tree, struct got_repository *repo,
693 int fd)
695 const struct got_error *err = NULL;
696 struct imsgbuf *ibuf;
698 ibuf = repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TREE].ibuf;
700 err = got_privsep_send_tree_req(ibuf, fd, NULL, -1);
701 if (err)
702 return err;
704 return got_privsep_recv_tree(tree, ibuf);
707 const struct got_error *
708 read_tree_privsep(struct got_tree_object **tree, int obj_fd,
709 struct got_repository *repo)
711 int imsg_fds[2];
712 pid_t pid;
713 struct imsgbuf *ibuf;
715 if (repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TREE].imsg_fd != -1)
716 return request_tree(tree, repo, obj_fd);
718 ibuf = calloc(1, sizeof(*ibuf));
719 if (ibuf == NULL)
720 return got_error_from_errno();
722 if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_fds) == -1)
723 return got_error_from_errno();
725 pid = fork();
726 if (pid == -1)
727 return got_error_from_errno();
728 else if (pid == 0) {
729 exec_privsep_child(imsg_fds, GOT_PATH_PROG_READ_TREE,
730 repo->path);
731 /* not reached */
734 if (close(imsg_fds[1]) != 0)
735 return got_error_from_errno();
736 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TREE].imsg_fd =
737 imsg_fds[0];
738 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TREE].pid = pid;
739 imsg_init(ibuf, imsg_fds[0]);
740 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TREE].ibuf = ibuf;
743 return request_tree(tree, repo, obj_fd);
746 static const struct got_error *
747 open_tree(struct got_tree_object **tree, struct got_repository *repo,
748 struct got_object_id *id, int check_cache)
750 const struct got_error *err = NULL;
751 struct got_packidx *packidx = NULL;
752 int idx;
753 char *path_packfile;
755 if (check_cache) {
756 *tree = got_repo_get_cached_tree(repo, id);
757 if (*tree != NULL) {
758 (*tree)->refcnt++;
759 return NULL;
761 } else
762 *tree = NULL;
764 err = got_repo_search_packidx(&packidx, &idx, repo, id);
765 if (err == NULL) {
766 struct got_pack *pack = NULL;
768 err = get_packfile_path(&path_packfile, packidx);
769 if (err)
770 return err;
772 pack = got_repo_get_cached_pack(repo, path_packfile);
773 if (pack == NULL) {
774 err = got_repo_cache_pack(&pack, repo, path_packfile,
775 packidx);
776 if (err)
777 return err;
779 err = read_packed_tree_privsep(tree, pack,
780 packidx, idx, id);
781 } else if (err->code == GOT_ERR_NO_OBJ) {
782 int fd;
784 err = open_loose_object(&fd, id, repo);
785 if (err)
786 return err;
787 err = read_tree_privsep(tree, fd, repo);
788 if (close(fd) != 0 && errno != EBADF && err == NULL)
789 err = got_error_from_errno();
792 if (err == NULL) {
793 (*tree)->refcnt++;
794 err = got_repo_cache_tree(repo, id, *tree);
797 return err;
800 const struct got_error *
801 got_object_open_as_tree(struct got_tree_object **tree,
802 struct got_repository *repo, struct got_object_id *id)
804 *tree = got_repo_get_cached_tree(repo, id);
805 if (*tree != NULL) {
806 (*tree)->refcnt++;
807 return NULL;
810 return open_tree(tree, repo, id, 0);
813 const struct got_error *
814 got_object_tree_open(struct got_tree_object **tree,
815 struct got_repository *repo, struct got_object *obj)
817 return open_tree(tree, repo, got_object_get_id(obj), 1);
820 const struct got_tree_entries *
821 got_object_tree_get_entries(struct got_tree_object *tree)
823 return &tree->entries;
826 static const struct got_error *
827 request_packed_blob(uint8_t **outbuf, size_t *size, size_t *hdrlen, int outfd,
828 struct got_pack *pack, struct got_packidx *packidx, int idx,
829 struct got_object_id *id)
831 const struct got_error *err = NULL;
832 int outfd_child;
833 int basefd, accumfd; /* temporary files for delta application */
835 basefd = got_opentempfd();
836 if (basefd == -1)
837 return got_error_from_errno();
838 accumfd = got_opentempfd();
839 if (accumfd == -1)
840 return got_error_from_errno();
842 outfd_child = dup(outfd);
843 if (outfd_child == -1)
844 return got_error_from_errno();
846 err = got_privsep_send_blob_req(pack->privsep_child->ibuf, -1, id, idx);
847 if (err)
848 return err;
850 err = got_privsep_send_blob_outfd(pack->privsep_child->ibuf,
851 outfd_child);
852 if (err) {
853 close(outfd_child);
854 return err;
856 err = got_privsep_send_tmpfd(pack->privsep_child->ibuf,
857 basefd);
858 if (err) {
859 close(basefd);
860 close(accumfd);
861 close(outfd_child);
862 return err;
865 err = got_privsep_send_tmpfd(pack->privsep_child->ibuf,
866 accumfd);
867 if (err) {
868 close(accumfd);
869 close(outfd_child);
870 return err;
873 err = got_privsep_recv_blob(outbuf, size, hdrlen,
874 pack->privsep_child->ibuf);
875 if (err)
876 return err;
878 if (lseek(outfd, SEEK_SET, 0) == -1)
879 err = got_error_from_errno();
881 return err;
884 static const struct got_error *
885 read_packed_blob_privsep(uint8_t **outbuf, size_t *size, size_t *hdrlen,
886 int outfd, struct got_pack *pack, struct got_packidx *packidx, int idx,
887 struct got_object_id *id)
889 const struct got_error *err = NULL;
891 if (pack->privsep_child == NULL) {
892 err = start_pack_privsep_child(pack, packidx);
893 if (err)
894 return err;
897 return request_packed_blob(outbuf, size, hdrlen, outfd, pack, packidx,
898 idx, id);
901 static const struct got_error *
902 request_blob(uint8_t **outbuf, size_t *size, size_t *hdrlen, int outfd,
903 int infd, struct imsgbuf *ibuf)
905 const struct got_error *err = NULL;
906 int outfd_child;
908 outfd_child = dup(outfd);
909 if (outfd_child == -1)
910 return got_error_from_errno();
912 err = got_privsep_send_blob_req(ibuf, infd, NULL, -1);
913 if (err)
914 return err;
916 err = got_privsep_send_blob_outfd(ibuf, outfd_child);
917 if (err) {
918 close(outfd_child);
919 return err;
922 err = got_privsep_recv_blob(outbuf, size, hdrlen, ibuf);
923 if (err)
924 return err;
926 if (lseek(outfd, SEEK_SET, 0) == -1)
927 return got_error_from_errno();
929 return err;
932 static const struct got_error *
933 read_blob_privsep(uint8_t **outbuf, size_t *size, size_t *hdrlen,
934 int outfd, int infd, struct got_repository *repo)
936 int imsg_fds[2];
937 pid_t pid;
938 struct imsgbuf *ibuf;
940 if (repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_BLOB].imsg_fd != -1) {
941 ibuf = repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_BLOB].ibuf;
942 return request_blob(outbuf, size, hdrlen, outfd, infd, ibuf);
945 ibuf = calloc(1, sizeof(*ibuf));
946 if (ibuf == NULL)
947 return got_error_from_errno();
949 if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_fds) == -1)
950 return got_error_from_errno();
952 pid = fork();
953 if (pid == -1)
954 return got_error_from_errno();
955 else if (pid == 0) {
956 exec_privsep_child(imsg_fds, GOT_PATH_PROG_READ_BLOB,
957 repo->path);
958 /* not reached */
961 if (close(imsg_fds[1]) != 0)
962 return got_error_from_errno();
963 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_BLOB].imsg_fd =
964 imsg_fds[0];
965 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_BLOB].pid = pid;
966 imsg_init(ibuf, imsg_fds[0]);
967 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_BLOB].ibuf = ibuf;
969 return request_blob(outbuf, size, hdrlen, outfd, infd, ibuf);
972 static const struct got_error *
973 open_blob(struct got_blob_object **blob, struct got_repository *repo,
974 struct got_object_id *id, size_t blocksize)
976 const struct got_error *err = NULL;
977 struct got_packidx *packidx = NULL;
978 int idx;
979 char *path_packfile;
980 uint8_t *outbuf;
981 int outfd;
982 size_t size, hdrlen;
983 struct stat sb;
985 *blob = calloc(1, sizeof(**blob));
986 if (*blob == NULL)
987 return got_error_from_errno();
989 outfd = got_opentempfd();
990 if (outfd == -1)
991 return got_error_from_errno();
993 (*blob)->read_buf = malloc(blocksize);
994 if ((*blob)->read_buf == NULL) {
995 err = got_error_from_errno();
996 goto done;
999 err = got_repo_search_packidx(&packidx, &idx, repo, id);
1000 if (err == NULL) {
1001 struct got_pack *pack = NULL;
1003 err = get_packfile_path(&path_packfile, packidx);
1004 if (err)
1005 goto done;
1007 pack = got_repo_get_cached_pack(repo, path_packfile);
1008 if (pack == NULL) {
1009 err = got_repo_cache_pack(&pack, repo, path_packfile,
1010 packidx);
1011 if (err)
1012 goto done;
1014 err = read_packed_blob_privsep(&outbuf, &size, &hdrlen, outfd,
1015 pack, packidx, idx, id);
1016 } else if (err->code == GOT_ERR_NO_OBJ) {
1017 int infd;
1019 err = open_loose_object(&infd, id, repo);
1020 if (err)
1021 goto done;
1022 err = read_blob_privsep(&outbuf, &size, &hdrlen, outfd, infd,
1023 repo);
1024 if (close(infd) != 0 && errno != EBADF && err == NULL)
1025 err = got_error_from_errno();
1027 if (err)
1028 goto done;
1030 if (hdrlen > size) {
1031 err = got_error(GOT_ERR_BAD_OBJ_HDR);
1032 goto done;
1035 if (outbuf) {
1036 if (close(outfd) != 0 && err == NULL)
1037 err = got_error_from_errno();
1038 outfd = -1;
1039 (*blob)->f = fmemopen(outbuf, size, "rb");
1040 if ((*blob)->f == NULL) {
1041 err = got_error_from_errno();
1042 free(outbuf);
1043 goto done;
1045 (*blob)->data = outbuf;
1046 } else {
1047 if (fstat(outfd, &sb) == -1) {
1048 err = got_error_from_errno();
1049 goto done;
1052 if (sb.st_size != size) {
1053 err = got_error(GOT_ERR_PRIVSEP_LEN);
1054 goto done;
1057 (*blob)->f = fdopen(outfd, "rb");
1058 if ((*blob)->f == NULL) {
1059 err = got_error_from_errno();
1060 close(outfd);
1061 outfd = -1;
1062 goto done;
1066 (*blob)->hdrlen = hdrlen;
1067 (*blob)->blocksize = blocksize;
1068 memcpy(&(*blob)->id.sha1, id->sha1, SHA1_DIGEST_LENGTH);
1070 done:
1071 if (err) {
1072 if (*blob) {
1073 if ((*blob)->f)
1074 fclose((*blob)->f);
1075 free((*blob)->read_buf);
1076 free((*blob)->data);
1077 free(*blob);
1078 *blob = NULL;
1079 } else if (outfd != -1)
1080 close(outfd);
1082 return err;
1085 const struct got_error *
1086 got_object_open_as_blob(struct got_blob_object **blob,
1087 struct got_repository *repo, struct got_object_id *id,
1088 size_t blocksize)
1090 return open_blob(blob, repo, id, blocksize);
1093 const struct got_error *
1094 got_object_blob_open(struct got_blob_object **blob,
1095 struct got_repository *repo, struct got_object *obj, size_t blocksize)
1097 return open_blob(blob, repo, got_object_get_id(obj), blocksize);
1100 const struct got_error *
1101 got_object_blob_close(struct got_blob_object *blob)
1103 const struct got_error *err = NULL;
1104 free(blob->read_buf);
1105 if (fclose(blob->f) != 0)
1106 err = got_error_from_errno();
1107 free(blob->data);
1108 free(blob);
1109 return err;
1112 char *
1113 got_object_blob_id_str(struct got_blob_object *blob, char *buf, size_t size)
1115 return got_sha1_digest_to_str(blob->id.sha1, buf, size);
1118 size_t
1119 got_object_blob_get_hdrlen(struct got_blob_object *blob)
1121 return blob->hdrlen;
1124 const uint8_t *
1125 got_object_blob_get_read_buf(struct got_blob_object *blob)
1127 return blob->read_buf;
1130 const struct got_error *
1131 got_object_blob_read_block(size_t *outlenp, struct got_blob_object *blob)
1133 size_t n;
1135 n = fread(blob->read_buf, 1, blob->blocksize, blob->f);
1136 if (n == 0 && ferror(blob->f))
1137 return got_ferror(blob->f, GOT_ERR_IO);
1138 *outlenp = n;
1139 return NULL;
1142 const struct got_error *
1143 got_object_blob_dump_to_file(size_t *total_len, int *nlines,
1144 FILE *outfile, struct got_blob_object *blob)
1146 const struct got_error *err = NULL;
1147 size_t n, len, hdrlen;
1148 const uint8_t *buf;
1149 int i;
1151 if (total_len)
1152 *total_len = 0;
1153 if (nlines)
1154 *nlines = 0;
1156 hdrlen = got_object_blob_get_hdrlen(blob);
1157 do {
1158 err = got_object_blob_read_block(&len, blob);
1159 if (err)
1160 return err;
1161 if (len == 0)
1162 break;
1163 if (total_len)
1164 *total_len += len;
1165 buf = got_object_blob_get_read_buf(blob);
1166 if (nlines) {
1167 for (i = 0; i < len; i++) {
1168 if (buf[i] == '\n')
1169 (*nlines)++;
1172 /* Skip blob object header first time around. */
1173 n = fwrite(buf + hdrlen, 1, len - hdrlen, outfile);
1174 if (n != len - hdrlen)
1175 return got_ferror(outfile, GOT_ERR_IO);
1176 hdrlen = 0;
1177 } while (len != 0);
1179 if (fflush(outfile) != 0)
1180 return got_error_from_errno();
1181 rewind(outfile);
1183 return NULL;
1186 static const struct got_error *
1187 request_packed_tag(struct got_tag_object **tag, struct got_pack *pack,
1188 int pack_idx, struct got_object_id *id)
1190 const struct got_error *err = NULL;
1192 err = got_privsep_send_tag_req(pack->privsep_child->ibuf, -1, id,
1193 pack_idx);
1194 if (err)
1195 return err;
1197 return got_privsep_recv_tag(tag, pack->privsep_child->ibuf);
1200 static const struct got_error *
1201 read_packed_tag_privsep(struct got_tag_object **tag,
1202 struct got_pack *pack, struct got_packidx *packidx, int idx,
1203 struct got_object_id *id)
1205 const struct got_error *err = NULL;
1207 if (pack->privsep_child)
1208 return request_packed_tag(tag, pack, idx, id);
1210 err = start_pack_privsep_child(pack, packidx);
1211 if (err)
1212 return err;
1214 return request_packed_tag(tag, pack, idx, id);
1217 static const struct got_error *
1218 request_tag(struct got_tag_object **tag, struct got_repository *repo,
1219 int fd)
1221 const struct got_error *err = NULL;
1222 struct imsgbuf *ibuf;
1224 ibuf = repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TAG].ibuf;
1226 err = got_privsep_send_tag_req(ibuf, fd, NULL, -1);
1227 if (err)
1228 return err;
1230 return got_privsep_recv_tag(tag, ibuf);
1233 static const struct got_error *
1234 read_tag_privsep(struct got_tag_object **tag, int obj_fd,
1235 struct got_repository *repo)
1237 int imsg_fds[2];
1238 pid_t pid;
1239 struct imsgbuf *ibuf;
1241 if (repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TAG].imsg_fd != -1)
1242 return request_tag(tag, repo, obj_fd);
1244 ibuf = calloc(1, sizeof(*ibuf));
1245 if (ibuf == NULL)
1246 return got_error_from_errno();
1248 if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_fds) == -1)
1249 return got_error_from_errno();
1251 pid = fork();
1252 if (pid == -1)
1253 return got_error_from_errno();
1254 else if (pid == 0) {
1255 exec_privsep_child(imsg_fds, GOT_PATH_PROG_READ_TAG,
1256 repo->path);
1257 /* not reached */
1260 if (close(imsg_fds[1]) != 0)
1261 return got_error_from_errno();
1262 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TAG].imsg_fd =
1263 imsg_fds[0];
1264 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TAG].pid = pid;
1265 imsg_init(ibuf, imsg_fds[0]);
1266 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TAG].ibuf = ibuf;
1268 return request_tag(tag, repo, obj_fd);
1271 static const struct got_error *
1272 open_tag(struct got_tag_object **tag, struct got_repository *repo,
1273 struct got_object_id *id, int check_cache)
1275 const struct got_error *err = NULL;
1276 struct got_packidx *packidx = NULL;
1277 int idx;
1278 char *path_packfile;
1280 if (check_cache) {
1281 *tag = got_repo_get_cached_tag(repo, id);
1282 if (*tag != NULL) {
1283 (*tag)->refcnt++;
1284 return NULL;
1286 } else
1287 *tag = NULL;
1289 err = got_repo_search_packidx(&packidx, &idx, repo, id);
1290 if (err == NULL) {
1291 struct got_pack *pack = NULL;
1293 err = get_packfile_path(&path_packfile, packidx);
1294 if (err)
1295 return err;
1297 pack = got_repo_get_cached_pack(repo, path_packfile);
1298 if (pack == NULL) {
1299 err = got_repo_cache_pack(&pack, repo, path_packfile,
1300 packidx);
1301 if (err)
1302 return err;
1304 err = read_packed_tag_privsep(tag, pack,
1305 packidx, idx, id);
1306 } else if (err->code == GOT_ERR_NO_OBJ) {
1307 int fd;
1309 err = open_loose_object(&fd, id, repo);
1310 if (err)
1311 return err;
1312 err = read_tag_privsep(tag, fd, repo);
1313 if (close(fd) != 0 && errno != EBADF && err == NULL)
1314 err = got_error_from_errno();
1317 if (err == NULL) {
1318 (*tag)->refcnt++;
1319 err = got_repo_cache_tag(repo, id, *tag);
1322 return err;
1325 const struct got_error *
1326 got_object_open_as_tag(struct got_tag_object **tag,
1327 struct got_repository *repo, struct got_object_id *id)
1329 *tag = got_repo_get_cached_tag(repo, id);
1330 if (*tag != NULL) {
1331 (*tag)->refcnt++;
1332 return NULL;
1335 return open_tag(tag, repo, id, 0);
1338 const struct got_error *
1339 got_object_tag_open(struct got_tag_object **tag,
1340 struct got_repository *repo, struct got_object *obj)
1342 return open_tag(tag, repo, got_object_get_id(obj), 1);
1345 int
1346 got_object_tag_get_object_type(struct got_tag_object *tag)
1348 return tag->obj_type;
1351 struct got_object_id *
1352 got_object_tag_get_object_id(struct got_tag_object *tag)
1354 return &tag->id;
1357 static struct got_tree_entry *
1358 find_entry_by_name(struct got_tree_object *tree, const char *name, size_t len)
1360 struct got_tree_entry *te;
1362 /* Note that tree entries are sorted in strncmp() order. */
1363 SIMPLEQ_FOREACH(te, &tree->entries.head, entry) {
1364 int cmp = strncmp(te->name, name, len);
1365 if (cmp < 0)
1366 continue;
1367 if (cmp > 0)
1368 break;
1369 if (te->name[len] == '\0')
1370 return te;
1372 return NULL;
1375 const struct got_error *
1376 got_object_id_by_path(struct got_object_id **id, struct got_repository *repo,
1377 struct got_object_id *commit_id, const char *path)
1379 const struct got_error *err = NULL;
1380 struct got_commit_object *commit = NULL;
1381 struct got_tree_object *tree = NULL;
1382 struct got_tree_entry *te = NULL;
1383 const char *seg, *s;
1384 size_t seglen;
1386 *id = NULL;
1388 /* We are expecting an absolute in-repository path. */
1389 if (path[0] != '/')
1390 return got_error(GOT_ERR_NOT_ABSPATH);
1392 err = got_object_open_as_commit(&commit, repo, commit_id);
1393 if (err)
1394 goto done;
1396 /* Handle opening of root of commit's tree. */
1397 if (path[1] == '\0') {
1398 *id = got_object_id_dup(commit->tree_id);
1399 if (*id == NULL)
1400 err = got_error_from_errno();
1401 goto done;
1404 err = got_object_open_as_tree(&tree, repo, commit->tree_id);
1405 if (err)
1406 goto done;
1408 s = path;
1409 s++; /* skip leading '/' */
1410 seg = s;
1411 seglen = 0;
1412 while (*s) {
1413 struct got_tree_object *next_tree;
1415 if (*s != '/') {
1416 s++;
1417 seglen++;
1418 if (*s)
1419 continue;
1422 te = find_entry_by_name(tree, seg, seglen);
1423 if (te == NULL) {
1424 err = got_error(GOT_ERR_NO_TREE_ENTRY);
1425 goto done;
1428 if (*s == '\0')
1429 break;
1431 seg = s + 1;
1432 seglen = 0;
1433 s++;
1434 if (*s) {
1435 err = got_object_open_as_tree(&next_tree, repo,
1436 te->id);
1437 te = NULL;
1438 if (err)
1439 goto done;
1440 got_object_tree_close(tree);
1441 tree = next_tree;
1445 if (te) {
1446 *id = got_object_id_dup(te->id);
1447 if (*id == NULL)
1448 return got_error_from_errno();
1449 } else
1450 err = got_error(GOT_ERR_NO_TREE_ENTRY);
1451 done:
1452 if (commit)
1453 got_object_commit_close(commit);
1454 if (tree)
1455 got_object_tree_close(tree);
1456 return err;
1459 const struct got_error *
1460 got_object_tree_path_changed(int *changed,
1461 struct got_tree_object *tree01, struct got_tree_object *tree02,
1462 const char *path, struct got_repository *repo)
1464 const struct got_error *err = NULL;
1465 struct got_tree_object *tree1 = NULL, *tree2 = NULL;
1466 struct got_tree_entry *te1 = NULL, *te2 = NULL;
1467 const char *seg, *s;
1468 size_t seglen;
1470 *changed = 0;
1472 /* We are expecting an absolute in-repository path. */
1473 if (path[0] != '/')
1474 return got_error(GOT_ERR_NOT_ABSPATH);
1476 /* We not do support comparing the root path. */
1477 if (path[1] == '\0')
1478 return got_error(GOT_ERR_BAD_PATH);
1480 tree1 = tree01;
1481 tree2 = tree02;
1482 s = path;
1483 s++; /* skip leading '/' */
1484 seg = s;
1485 seglen = 0;
1486 while (*s) {
1487 struct got_tree_object *next_tree1, *next_tree2;
1489 if (*s != '/') {
1490 s++;
1491 seglen++;
1492 if (*s)
1493 continue;
1496 te1 = find_entry_by_name(tree1, seg, seglen);
1497 if (te1 == NULL) {
1498 err = got_error(GOT_ERR_NO_OBJ);
1499 goto done;
1502 te2 = find_entry_by_name(tree2, seg, seglen);
1503 if (te2 == NULL) {
1504 *changed = 1;
1505 goto done;
1508 if (te1->mode != te2->mode) {
1509 *changed = 1;
1510 goto done;
1513 if (got_object_id_cmp(te1->id, te2->id) == 0) {
1514 *changed = 0;
1515 goto done;
1518 if (*s == '\0') { /* final path element */
1519 *changed = 1;
1520 goto done;
1523 seg = s + 1;
1524 s++;
1525 seglen = 0;
1526 if (*s) {
1527 err = got_object_open_as_tree(&next_tree1, repo,
1528 te1->id);
1529 te1 = NULL;
1530 if (err)
1531 goto done;
1532 if (tree1 != tree01)
1533 got_object_tree_close(tree1);
1534 tree1 = next_tree1;
1536 err = got_object_open_as_tree(&next_tree2, repo,
1537 te2->id);
1538 te2 = NULL;
1539 if (err)
1540 goto done;
1541 if (tree2 != tree02)
1542 got_object_tree_close(tree2);
1543 tree2 = next_tree2;
1546 done:
1547 if (tree1 && tree1 != tree01)
1548 got_object_tree_close(tree1);
1549 if (tree2 && tree2 != tree02)
1550 got_object_tree_close(tree2);
1551 return err;