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"
42 #include "got_path.h"
44 #include "got_lib_sha1.h"
45 #include "got_lib_delta.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_prefix_errno("got_repo_get_path_objects");
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_prefix_errno("asprintf");
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_prefix_errno2("open", path);
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_prefix_errno("malloc");
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(), strerror(errno));
187 _exit(1);
190 if (dup2(imsg_fds[1], GOT_IMSG_FD_CHILD) == -1) {
191 fprintf(stderr, "%s: %s\n", getprogname(), strerror(errno));
192 _exit(1);
194 if (closefrom(GOT_IMSG_FD_CHILD + 1) == -1) {
195 fprintf(stderr, "%s: %s\n", getprogname(), strerror(errno));
196 _exit(1);
199 if (execl(path, path, repo_path, (char *)NULL) == -1) {
200 fprintf(stderr, "%s: %s: %s\n", getprogname(), path,
201 strerror(errno));
202 _exit(1);
206 static const struct got_error *
207 request_packed_object(struct got_object **obj, struct got_pack *pack, int idx,
208 struct got_object_id *id)
210 const struct got_error *err = NULL;
211 struct imsgbuf *ibuf = pack->privsep_child->ibuf;
213 err = got_privsep_send_packed_obj_req(ibuf, idx, id);
214 if (err)
215 return err;
217 err = got_privsep_recv_obj(obj, ibuf);
218 if (err)
219 return err;
221 (*obj)->path_packfile = strdup(pack->path_packfile);
222 if ((*obj)->path_packfile == NULL) {
223 err = got_error_prefix_errno("strdup");
224 return err;
226 memcpy(&(*obj)->id, id, sizeof((*obj)->id));
228 return NULL;
231 static const struct got_error *
232 start_pack_privsep_child(struct got_pack *pack, struct got_packidx *packidx)
234 const struct got_error *err = NULL;
235 int imsg_fds[2];
236 pid_t pid;
237 struct imsgbuf *ibuf;
239 ibuf = calloc(1, sizeof(*ibuf));
240 if (ibuf == NULL)
241 return got_error_prefix_errno("calloc");
243 pack->privsep_child = calloc(1, sizeof(*pack->privsep_child));
244 if (pack->privsep_child == NULL) {
245 err = got_error_prefix_errno("calloc");
246 free(ibuf);
247 return err;
250 if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_fds) == -1) {
251 err = got_error_prefix_errno("socketpair");
252 goto done;
255 pid = fork();
256 if (pid == -1) {
257 err = got_error_prefix_errno("fork");
258 goto done;
259 } else if (pid == 0) {
260 exec_privsep_child(imsg_fds, GOT_PATH_PROG_READ_PACK,
261 pack->path_packfile);
262 /* not reached */
265 if (close(imsg_fds[1]) != 0)
266 return got_error_prefix_errno("close");
267 pack->privsep_child->imsg_fd = imsg_fds[0];
268 pack->privsep_child->pid = pid;
269 imsg_init(ibuf, imsg_fds[0]);
270 pack->privsep_child->ibuf = ibuf;
272 err = got_privsep_init_pack_child(ibuf, pack, packidx);
273 if (err) {
274 const struct got_error *child_err;
275 err = got_privsep_send_stop(pack->privsep_child->imsg_fd);
276 child_err = got_privsep_wait_for_child(
277 pack->privsep_child->pid);
278 if (child_err && err == NULL)
279 err = child_err;
281 done:
282 if (err) {
283 free(ibuf);
284 free(pack->privsep_child);
285 pack->privsep_child = NULL;
287 return err;
290 static const struct got_error *
291 read_packed_object_privsep(struct got_object **obj,
292 struct got_repository *repo, struct got_pack *pack,
293 struct got_packidx *packidx, int idx, struct got_object_id *id)
295 const struct got_error *err = NULL;
297 if (pack->privsep_child)
298 return request_packed_object(obj, pack, idx, id);
300 err = start_pack_privsep_child(pack, packidx);
301 if (err)
302 return err;
304 return request_packed_object(obj, pack, idx, id);
308 static const struct got_error *
309 open_packed_object(struct got_object **obj, struct got_object_id *id,
310 struct got_repository *repo)
312 const struct got_error *err = NULL;
313 struct got_pack *pack = NULL;
314 struct got_packidx *packidx = NULL;
315 int idx;
316 char *path_packfile;
318 err = got_repo_search_packidx(&packidx, &idx, repo, id);
319 if (err)
320 return err;
322 err = get_packfile_path(&path_packfile, packidx);
323 if (err)
324 return err;
326 pack = got_repo_get_cached_pack(repo, path_packfile);
327 if (pack == NULL) {
328 err = got_repo_cache_pack(&pack, repo, path_packfile, packidx);
329 if (err)
330 goto done;
333 err = read_packed_object_privsep(obj, repo, pack, packidx, idx, id);
334 if (err)
335 goto done;
337 err = got_repo_cache_pack(NULL, repo, (*obj)->path_packfile, packidx);
338 done:
339 free(path_packfile);
340 return err;
343 static const struct got_error *
344 request_object(struct got_object **obj, struct got_repository *repo, int fd)
346 const struct got_error *err = NULL;
347 struct imsgbuf *ibuf;
349 ibuf = repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_OBJECT].ibuf;
351 err = got_privsep_send_obj_req(ibuf, fd);
352 if (err)
353 return err;
355 return got_privsep_recv_obj(obj, ibuf);
358 static const struct got_error *
359 read_object_header_privsep(struct got_object **obj, struct got_repository *repo,
360 int obj_fd)
362 int imsg_fds[2];
363 pid_t pid;
364 struct imsgbuf *ibuf;
366 if (repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_OBJECT].imsg_fd != -1)
367 return request_object(obj, repo, obj_fd);
369 ibuf = calloc(1, sizeof(*ibuf));
370 if (ibuf == NULL)
371 return got_error_prefix_errno("calloc");
373 if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_fds) == -1)
374 return got_error_prefix_errno("socketpair");
376 pid = fork();
377 if (pid == -1)
378 return got_error_prefix_errno("fork");
379 else if (pid == 0) {
380 exec_privsep_child(imsg_fds, GOT_PATH_PROG_READ_OBJECT,
381 repo->path);
382 /* not reached */
385 if (close(imsg_fds[1]) != 0)
386 return got_error_prefix_errno("close");
387 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_OBJECT].imsg_fd =
388 imsg_fds[0];
389 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_OBJECT].pid = pid;
390 imsg_init(ibuf, imsg_fds[0]);
391 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_OBJECT].ibuf = ibuf;
393 return request_object(obj, repo, obj_fd);
397 const struct got_error *
398 got_object_open(struct got_object **obj, struct got_repository *repo,
399 struct got_object_id *id)
401 const struct got_error *err = NULL;
402 char *path;
403 int fd;
405 *obj = got_repo_get_cached_object(repo, id);
406 if (*obj != NULL) {
407 (*obj)->refcnt++;
408 return NULL;
411 err = open_packed_object(obj, id, repo);
412 if (err && err->code != GOT_ERR_NO_OBJ)
413 return err;
414 if (*obj) {
415 (*obj)->refcnt++;
416 return got_repo_cache_object(repo, id, *obj);
419 err = got_object_get_path(&path, id, repo);
420 if (err)
421 return err;
423 fd = open(path, O_RDONLY | O_NOFOLLOW);
424 if (fd == -1) {
425 if (errno == ENOENT)
426 err = got_error_no_obj(id);
427 else
428 err = got_error_prefix_errno2("open", path);
429 goto done;
430 } else {
431 err = read_object_header_privsep(obj, repo, fd);
432 if (err)
433 goto done;
434 memcpy((*obj)->id.sha1, id->sha1, SHA1_DIGEST_LENGTH);
437 (*obj)->refcnt++;
438 err = got_repo_cache_object(repo, id, *obj);
439 done:
440 free(path);
441 return err;
445 const struct got_error *
446 got_object_open_by_id_str(struct got_object **obj, struct got_repository *repo,
447 const char *id_str)
449 struct got_object_id id;
451 if (!got_parse_sha1_digest(id.sha1, id_str))
452 return got_error(GOT_ERR_BAD_OBJ_ID_STR);
454 return got_object_open(obj, repo, &id);
457 const struct got_error *
458 got_object_resolve_id_str(struct got_object_id **id,
459 struct got_repository *repo, const char *id_str)
461 const struct got_error *err = NULL;
462 struct got_object *obj;
464 err = got_object_open_by_id_str(&obj, repo, id_str);
465 if (err)
466 return err;
468 *id = got_object_id_dup(got_object_get_id(obj));
469 got_object_close(obj);
470 if (*id == NULL)
471 return got_error_prefix_errno("got_object_id_dup");
473 return NULL;
476 static const struct got_error *
477 request_packed_commit(struct got_commit_object **commit, struct got_pack *pack,
478 int pack_idx, struct got_object_id *id)
480 const struct got_error *err = NULL;
482 err = got_privsep_send_commit_req(pack->privsep_child->ibuf, -1, id,
483 pack_idx);
484 if (err)
485 return err;
487 return got_privsep_recv_commit(commit, pack->privsep_child->ibuf);
490 static const struct got_error *
491 read_packed_commit_privsep(struct got_commit_object **commit,
492 struct got_pack *pack, struct got_packidx *packidx, int idx,
493 struct got_object_id *id)
495 const struct got_error *err = NULL;
497 if (pack->privsep_child)
498 return request_packed_commit(commit, pack, idx, id);
500 err = start_pack_privsep_child(pack, packidx);
501 if (err)
502 return err;
504 return request_packed_commit(commit, pack, idx, id);
507 static const struct got_error *
508 request_commit(struct got_commit_object **commit, struct got_repository *repo,
509 int fd)
511 const struct got_error *err = NULL;
512 struct imsgbuf *ibuf;
514 ibuf = repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_COMMIT].ibuf;
516 err = got_privsep_send_commit_req(ibuf, fd, NULL, -1);
517 if (err)
518 return err;
520 return got_privsep_recv_commit(commit, ibuf);
523 static const struct got_error *
524 read_commit_privsep(struct got_commit_object **commit, int obj_fd,
525 struct got_repository *repo)
527 int imsg_fds[2];
528 pid_t pid;
529 struct imsgbuf *ibuf;
531 if (repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_COMMIT].imsg_fd != -1)
532 return request_commit(commit, repo, obj_fd);
534 ibuf = calloc(1, sizeof(*ibuf));
535 if (ibuf == NULL)
536 return got_error_prefix_errno("calloc");
538 if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_fds) == -1)
539 return got_error_prefix_errno("socketpair");
541 pid = fork();
542 if (pid == -1)
543 return got_error_prefix_errno("fork");
544 else if (pid == 0) {
545 exec_privsep_child(imsg_fds, GOT_PATH_PROG_READ_COMMIT,
546 repo->path);
547 /* not reached */
550 if (close(imsg_fds[1]) != 0)
551 return got_error_prefix_errno("close");
552 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_COMMIT].imsg_fd =
553 imsg_fds[0];
554 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_COMMIT].pid = pid;
555 imsg_init(ibuf, imsg_fds[0]);
556 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_COMMIT].ibuf = ibuf;
558 return request_commit(commit, repo, obj_fd);
562 static const struct got_error *
563 open_commit(struct got_commit_object **commit,
564 struct got_repository *repo, struct got_object_id *id, int check_cache)
566 const struct got_error *err = NULL;
567 struct got_packidx *packidx = NULL;
568 int idx;
569 char *path_packfile;
571 if (check_cache) {
572 *commit = got_repo_get_cached_commit(repo, id);
573 if (*commit != NULL) {
574 (*commit)->refcnt++;
575 return NULL;
577 } else
578 *commit = NULL;
580 err = got_repo_search_packidx(&packidx, &idx, repo, id);
581 if (err == NULL) {
582 struct got_pack *pack = NULL;
584 err = get_packfile_path(&path_packfile, packidx);
585 if (err)
586 return err;
588 pack = got_repo_get_cached_pack(repo, path_packfile);
589 if (pack == NULL) {
590 err = got_repo_cache_pack(&pack, repo, path_packfile,
591 packidx);
592 if (err)
593 return err;
595 err = read_packed_commit_privsep(commit, pack,
596 packidx, idx, id);
597 } else if (err->code == GOT_ERR_NO_OBJ) {
598 int fd;
600 err = open_loose_object(&fd, id, repo);
601 if (err)
602 return err;
603 err = read_commit_privsep(commit, fd, repo);
606 if (err == NULL) {
607 (*commit)->refcnt++;
608 err = got_repo_cache_commit(repo, id, *commit);
611 return err;
614 const struct got_error *
615 got_object_open_as_commit(struct got_commit_object **commit,
616 struct got_repository *repo, struct got_object_id *id)
618 *commit = got_repo_get_cached_commit(repo, id);
619 if (*commit != NULL) {
620 (*commit)->refcnt++;
621 return NULL;
624 return open_commit(commit, repo, id, 0);
627 const struct got_error *
628 got_object_commit_open(struct got_commit_object **commit,
629 struct got_repository *repo, struct got_object *obj)
631 return open_commit(commit, repo, got_object_get_id(obj), 1);
634 const struct got_error *
635 got_object_qid_alloc(struct got_object_qid **qid, struct got_object_id *id)
637 const struct got_error *err = NULL;
639 *qid = calloc(1, sizeof(**qid));
640 if (*qid == NULL)
641 return got_error_prefix_errno("calloc");
643 (*qid)->id = got_object_id_dup(id);
644 if ((*qid)->id == NULL) {
645 err = got_error_prefix_errno("got_object_id_dup");
646 got_object_qid_free(*qid);
647 *qid = NULL;
648 return err;
651 return NULL;
654 static const struct got_error *
655 request_packed_tree(struct got_tree_object **tree, struct got_pack *pack,
656 int pack_idx, struct got_object_id *id)
658 const struct got_error *err = NULL;
660 err = got_privsep_send_tree_req(pack->privsep_child->ibuf, -1, id,
661 pack_idx);
662 if (err)
663 return err;
665 return got_privsep_recv_tree(tree, pack->privsep_child->ibuf);
668 static const struct got_error *
669 read_packed_tree_privsep(struct got_tree_object **tree,
670 struct got_pack *pack, struct got_packidx *packidx, int idx,
671 struct got_object_id *id)
673 const struct got_error *err = NULL;
675 if (pack->privsep_child)
676 return request_packed_tree(tree, pack, idx, id);
678 err = start_pack_privsep_child(pack, packidx);
679 if (err)
680 return err;
682 return request_packed_tree(tree, pack, idx, id);
685 static const struct got_error *
686 request_tree(struct got_tree_object **tree, struct got_repository *repo,
687 int fd)
689 const struct got_error *err = NULL;
690 struct imsgbuf *ibuf;
692 ibuf = repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TREE].ibuf;
694 err = got_privsep_send_tree_req(ibuf, fd, NULL, -1);
695 if (err)
696 return err;
698 return got_privsep_recv_tree(tree, ibuf);
701 const struct got_error *
702 read_tree_privsep(struct got_tree_object **tree, int obj_fd,
703 struct got_repository *repo)
705 int imsg_fds[2];
706 pid_t pid;
707 struct imsgbuf *ibuf;
709 if (repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TREE].imsg_fd != -1)
710 return request_tree(tree, repo, obj_fd);
712 ibuf = calloc(1, sizeof(*ibuf));
713 if (ibuf == NULL)
714 return got_error_prefix_errno("calloc");
716 if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_fds) == -1)
717 return got_error_prefix_errno("socketpair");
719 pid = fork();
720 if (pid == -1)
721 return got_error_prefix_errno("fork");
722 else if (pid == 0) {
723 exec_privsep_child(imsg_fds, GOT_PATH_PROG_READ_TREE,
724 repo->path);
725 /* not reached */
728 if (close(imsg_fds[1]) != 0)
729 return got_error_prefix_errno("close");
730 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TREE].imsg_fd =
731 imsg_fds[0];
732 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TREE].pid = pid;
733 imsg_init(ibuf, imsg_fds[0]);
734 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TREE].ibuf = ibuf;
737 return request_tree(tree, repo, obj_fd);
740 static const struct got_error *
741 open_tree(struct got_tree_object **tree, struct got_repository *repo,
742 struct got_object_id *id, int check_cache)
744 const struct got_error *err = NULL;
745 struct got_packidx *packidx = NULL;
746 int idx;
747 char *path_packfile;
749 if (check_cache) {
750 *tree = got_repo_get_cached_tree(repo, id);
751 if (*tree != NULL) {
752 (*tree)->refcnt++;
753 return NULL;
755 } else
756 *tree = NULL;
758 err = got_repo_search_packidx(&packidx, &idx, repo, id);
759 if (err == NULL) {
760 struct got_pack *pack = NULL;
762 err = get_packfile_path(&path_packfile, packidx);
763 if (err)
764 return err;
766 pack = got_repo_get_cached_pack(repo, path_packfile);
767 if (pack == NULL) {
768 err = got_repo_cache_pack(&pack, repo, path_packfile,
769 packidx);
770 if (err)
771 return err;
773 err = read_packed_tree_privsep(tree, pack,
774 packidx, idx, id);
775 } else if (err->code == GOT_ERR_NO_OBJ) {
776 int fd;
778 err = open_loose_object(&fd, id, repo);
779 if (err)
780 return err;
781 err = read_tree_privsep(tree, fd, repo);
784 if (err == NULL) {
785 (*tree)->refcnt++;
786 err = got_repo_cache_tree(repo, id, *tree);
789 return err;
792 const struct got_error *
793 got_object_open_as_tree(struct got_tree_object **tree,
794 struct got_repository *repo, struct got_object_id *id)
796 *tree = got_repo_get_cached_tree(repo, id);
797 if (*tree != NULL) {
798 (*tree)->refcnt++;
799 return NULL;
802 return open_tree(tree, repo, id, 0);
805 const struct got_error *
806 got_object_tree_open(struct got_tree_object **tree,
807 struct got_repository *repo, struct got_object *obj)
809 return open_tree(tree, repo, got_object_get_id(obj), 1);
812 const struct got_tree_entries *
813 got_object_tree_get_entries(struct got_tree_object *tree)
815 return &tree->entries;
818 static const struct got_error *
819 request_packed_blob(uint8_t **outbuf, size_t *size, size_t *hdrlen, int outfd,
820 struct got_pack *pack, struct got_packidx *packidx, int idx,
821 struct got_object_id *id)
823 const struct got_error *err = NULL;
824 int outfd_child;
825 int basefd, accumfd; /* temporary files for delta application */
827 basefd = got_opentempfd();
828 if (basefd == -1)
829 return got_error_prefix_errno("got_opentempfd");
830 accumfd = got_opentempfd();
831 if (accumfd == -1)
832 return got_error_prefix_errno("got_opentempfd");
834 outfd_child = dup(outfd);
835 if (outfd_child == -1)
836 return got_error_prefix_errno("dup");
838 err = got_privsep_send_blob_req(pack->privsep_child->ibuf, -1, id, idx);
839 if (err)
840 return err;
842 err = got_privsep_send_blob_outfd(pack->privsep_child->ibuf,
843 outfd_child);
844 if (err) {
845 close(basefd);
846 close(accumfd);
847 return err;
850 err = got_privsep_send_tmpfd(pack->privsep_child->ibuf,
851 basefd);
852 if (err) {
853 close(accumfd);
854 return err;
857 err = got_privsep_send_tmpfd(pack->privsep_child->ibuf,
858 accumfd);
859 if (err)
860 return err;
862 err = got_privsep_recv_blob(outbuf, size, hdrlen,
863 pack->privsep_child->ibuf);
864 if (err)
865 return err;
867 if (lseek(outfd, SEEK_SET, 0) == -1)
868 err = got_error_prefix_errno("lseek");
870 return err;
873 static const struct got_error *
874 read_packed_blob_privsep(uint8_t **outbuf, size_t *size, size_t *hdrlen,
875 int outfd, struct got_pack *pack, struct got_packidx *packidx, int idx,
876 struct got_object_id *id)
878 const struct got_error *err = NULL;
880 if (pack->privsep_child == NULL) {
881 err = start_pack_privsep_child(pack, packidx);
882 if (err)
883 return err;
886 return request_packed_blob(outbuf, size, hdrlen, outfd, pack, packidx,
887 idx, id);
890 static const struct got_error *
891 request_blob(uint8_t **outbuf, size_t *size, size_t *hdrlen, int outfd,
892 int infd, struct imsgbuf *ibuf)
894 const struct got_error *err = NULL;
895 int outfd_child;
897 outfd_child = dup(outfd);
898 if (outfd_child == -1)
899 return got_error_prefix_errno("dup");
901 err = got_privsep_send_blob_req(ibuf, infd, NULL, -1);
902 if (err)
903 return err;
905 err = got_privsep_send_blob_outfd(ibuf, outfd_child);
906 if (err)
907 return err;
909 err = got_privsep_recv_blob(outbuf, size, hdrlen, ibuf);
910 if (err)
911 return err;
913 if (lseek(outfd, SEEK_SET, 0) == -1)
914 return got_error_prefix_errno("lseek");
916 return err;
919 static const struct got_error *
920 read_blob_privsep(uint8_t **outbuf, size_t *size, size_t *hdrlen,
921 int outfd, int infd, struct got_repository *repo)
923 int imsg_fds[2];
924 pid_t pid;
925 struct imsgbuf *ibuf;
927 if (repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_BLOB].imsg_fd != -1) {
928 ibuf = repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_BLOB].ibuf;
929 return request_blob(outbuf, size, hdrlen, outfd, infd, ibuf);
932 ibuf = calloc(1, sizeof(*ibuf));
933 if (ibuf == NULL)
934 return got_error_prefix_errno("calloc");
936 if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_fds) == -1)
937 return got_error_prefix_errno("socketpair");
939 pid = fork();
940 if (pid == -1)
941 return got_error_prefix_errno("fork");
942 else if (pid == 0) {
943 exec_privsep_child(imsg_fds, GOT_PATH_PROG_READ_BLOB,
944 repo->path);
945 /* not reached */
948 if (close(imsg_fds[1]) != 0)
949 return got_error_prefix_errno("close");
950 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_BLOB].imsg_fd =
951 imsg_fds[0];
952 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_BLOB].pid = pid;
953 imsg_init(ibuf, imsg_fds[0]);
954 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_BLOB].ibuf = ibuf;
956 return request_blob(outbuf, size, hdrlen, outfd, infd, ibuf);
959 static const struct got_error *
960 open_blob(struct got_blob_object **blob, struct got_repository *repo,
961 struct got_object_id *id, size_t blocksize)
963 const struct got_error *err = NULL;
964 struct got_packidx *packidx = NULL;
965 int idx;
966 char *path_packfile;
967 uint8_t *outbuf;
968 int outfd;
969 size_t size, hdrlen;
970 struct stat sb;
972 *blob = calloc(1, sizeof(**blob));
973 if (*blob == NULL)
974 return got_error_prefix_errno("calloc");
976 outfd = got_opentempfd();
977 if (outfd == -1)
978 return got_error_prefix_errno("got_opentempfd");
980 (*blob)->read_buf = malloc(blocksize);
981 if ((*blob)->read_buf == NULL) {
982 err = got_error_prefix_errno("malloc");
983 goto done;
986 err = got_repo_search_packidx(&packidx, &idx, repo, id);
987 if (err == NULL) {
988 struct got_pack *pack = NULL;
990 err = get_packfile_path(&path_packfile, packidx);
991 if (err)
992 goto done;
994 pack = got_repo_get_cached_pack(repo, path_packfile);
995 if (pack == NULL) {
996 err = got_repo_cache_pack(&pack, repo, path_packfile,
997 packidx);
998 if (err)
999 goto done;
1001 err = read_packed_blob_privsep(&outbuf, &size, &hdrlen, outfd,
1002 pack, packidx, idx, id);
1003 } else if (err->code == GOT_ERR_NO_OBJ) {
1004 int infd;
1006 err = open_loose_object(&infd, id, repo);
1007 if (err)
1008 goto done;
1009 err = read_blob_privsep(&outbuf, &size, &hdrlen, outfd, infd,
1010 repo);
1012 if (err)
1013 goto done;
1015 if (hdrlen > size) {
1016 err = got_error(GOT_ERR_BAD_OBJ_HDR);
1017 goto done;
1020 if (outbuf) {
1021 if (close(outfd) != 0 && err == NULL)
1022 err = got_error_prefix_errno("close");
1023 outfd = -1;
1024 (*blob)->f = fmemopen(outbuf, size, "rb");
1025 if ((*blob)->f == NULL) {
1026 err = got_error_prefix_errno("fmemopen");
1027 free(outbuf);
1028 goto done;
1030 (*blob)->data = outbuf;
1031 } else {
1032 if (fstat(outfd, &sb) == -1) {
1033 err = got_error_prefix_errno("fstat");
1034 goto done;
1037 if (sb.st_size != size) {
1038 err = got_error(GOT_ERR_PRIVSEP_LEN);
1039 goto done;
1042 (*blob)->f = fdopen(outfd, "rb");
1043 if ((*blob)->f == NULL) {
1044 err = got_error_prefix_errno("fdopen");
1045 close(outfd);
1046 outfd = -1;
1047 goto done;
1051 (*blob)->hdrlen = hdrlen;
1052 (*blob)->blocksize = blocksize;
1053 memcpy(&(*blob)->id.sha1, id->sha1, SHA1_DIGEST_LENGTH);
1055 done:
1056 if (err) {
1057 if (*blob) {
1058 got_object_blob_close(*blob);
1059 *blob = NULL;
1060 } else if (outfd != -1)
1061 close(outfd);
1063 return err;
1066 const struct got_error *
1067 got_object_open_as_blob(struct got_blob_object **blob,
1068 struct got_repository *repo, struct got_object_id *id,
1069 size_t blocksize)
1071 return open_blob(blob, repo, id, blocksize);
1074 const struct got_error *
1075 got_object_blob_open(struct got_blob_object **blob,
1076 struct got_repository *repo, struct got_object *obj, size_t blocksize)
1078 return open_blob(blob, repo, got_object_get_id(obj), blocksize);
1081 const struct got_error *
1082 got_object_blob_close(struct got_blob_object *blob)
1084 const struct got_error *err = NULL;
1085 free(blob->read_buf);
1086 if (blob->f && fclose(blob->f) != 0)
1087 err = got_error_prefix_errno("fclose");
1088 free(blob->data);
1089 free(blob);
1090 return err;
1093 char *
1094 got_object_blob_id_str(struct got_blob_object *blob, char *buf, size_t size)
1096 return got_sha1_digest_to_str(blob->id.sha1, buf, size);
1099 size_t
1100 got_object_blob_get_hdrlen(struct got_blob_object *blob)
1102 return blob->hdrlen;
1105 const uint8_t *
1106 got_object_blob_get_read_buf(struct got_blob_object *blob)
1108 return blob->read_buf;
1111 const struct got_error *
1112 got_object_blob_read_block(size_t *outlenp, struct got_blob_object *blob)
1114 size_t n;
1116 n = fread(blob->read_buf, 1, blob->blocksize, blob->f);
1117 if (n == 0 && ferror(blob->f))
1118 return got_ferror(blob->f, GOT_ERR_IO);
1119 *outlenp = n;
1120 return NULL;
1123 const struct got_error *
1124 got_object_blob_dump_to_file(size_t *total_len, int *nlines,
1125 FILE *outfile, struct got_blob_object *blob)
1127 const struct got_error *err = NULL;
1128 size_t n, len, hdrlen;
1129 const uint8_t *buf;
1130 int i;
1132 if (total_len)
1133 *total_len = 0;
1134 if (nlines)
1135 *nlines = 0;
1137 hdrlen = got_object_blob_get_hdrlen(blob);
1138 do {
1139 err = got_object_blob_read_block(&len, blob);
1140 if (err)
1141 return err;
1142 if (len == 0)
1143 break;
1144 if (total_len)
1145 *total_len += len;
1146 buf = got_object_blob_get_read_buf(blob);
1147 if (nlines) {
1148 for (i = 0; i < len; i++) {
1149 if (buf[i] == '\n')
1150 (*nlines)++;
1153 /* Skip blob object header first time around. */
1154 n = fwrite(buf + hdrlen, 1, len - hdrlen, outfile);
1155 if (n != len - hdrlen)
1156 return got_ferror(outfile, GOT_ERR_IO);
1157 hdrlen = 0;
1158 } while (len != 0);
1160 if (fflush(outfile) != 0)
1161 return got_error_prefix_errno("fflush");
1162 rewind(outfile);
1164 return NULL;
1167 static const struct got_error *
1168 request_packed_tag(struct got_tag_object **tag, struct got_pack *pack,
1169 int pack_idx, struct got_object_id *id)
1171 const struct got_error *err = NULL;
1173 err = got_privsep_send_tag_req(pack->privsep_child->ibuf, -1, id,
1174 pack_idx);
1175 if (err)
1176 return err;
1178 return got_privsep_recv_tag(tag, pack->privsep_child->ibuf);
1181 static const struct got_error *
1182 read_packed_tag_privsep(struct got_tag_object **tag,
1183 struct got_pack *pack, struct got_packidx *packidx, int idx,
1184 struct got_object_id *id)
1186 const struct got_error *err = NULL;
1188 if (pack->privsep_child)
1189 return request_packed_tag(tag, pack, idx, id);
1191 err = start_pack_privsep_child(pack, packidx);
1192 if (err)
1193 return err;
1195 return request_packed_tag(tag, pack, idx, id);
1198 static const struct got_error *
1199 request_tag(struct got_tag_object **tag, struct got_repository *repo,
1200 int fd)
1202 const struct got_error *err = NULL;
1203 struct imsgbuf *ibuf;
1205 ibuf = repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TAG].ibuf;
1207 err = got_privsep_send_tag_req(ibuf, fd, NULL, -1);
1208 if (err)
1209 return err;
1211 return got_privsep_recv_tag(tag, ibuf);
1214 static const struct got_error *
1215 read_tag_privsep(struct got_tag_object **tag, int obj_fd,
1216 struct got_repository *repo)
1218 int imsg_fds[2];
1219 pid_t pid;
1220 struct imsgbuf *ibuf;
1222 if (repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TAG].imsg_fd != -1)
1223 return request_tag(tag, repo, obj_fd);
1225 ibuf = calloc(1, sizeof(*ibuf));
1226 if (ibuf == NULL)
1227 return got_error_prefix_errno("calloc");
1229 if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_fds) == -1)
1230 return got_error_prefix_errno("socketpair");
1232 pid = fork();
1233 if (pid == -1)
1234 return got_error_prefix_errno("fork");
1235 else if (pid == 0) {
1236 exec_privsep_child(imsg_fds, GOT_PATH_PROG_READ_TAG,
1237 repo->path);
1238 /* not reached */
1241 if (close(imsg_fds[1]) != 0)
1242 return got_error_prefix_errno("close");
1243 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TAG].imsg_fd =
1244 imsg_fds[0];
1245 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TAG].pid = pid;
1246 imsg_init(ibuf, imsg_fds[0]);
1247 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TAG].ibuf = ibuf;
1249 return request_tag(tag, repo, obj_fd);
1252 static const struct got_error *
1253 open_tag(struct got_tag_object **tag, struct got_repository *repo,
1254 struct got_object_id *id, int check_cache)
1256 const struct got_error *err = NULL;
1257 struct got_packidx *packidx = NULL;
1258 int idx;
1259 char *path_packfile;
1261 if (check_cache) {
1262 *tag = got_repo_get_cached_tag(repo, id);
1263 if (*tag != NULL) {
1264 (*tag)->refcnt++;
1265 return NULL;
1267 } else
1268 *tag = NULL;
1270 err = got_repo_search_packidx(&packidx, &idx, repo, id);
1271 if (err == NULL) {
1272 struct got_pack *pack = NULL;
1274 err = get_packfile_path(&path_packfile, packidx);
1275 if (err)
1276 return err;
1278 pack = got_repo_get_cached_pack(repo, path_packfile);
1279 if (pack == NULL) {
1280 err = got_repo_cache_pack(&pack, repo, path_packfile,
1281 packidx);
1282 if (err)
1283 return err;
1285 err = read_packed_tag_privsep(tag, pack,
1286 packidx, idx, id);
1287 } else if (err->code == GOT_ERR_NO_OBJ) {
1288 int fd;
1290 err = open_loose_object(&fd, id, repo);
1291 if (err)
1292 return err;
1293 err = read_tag_privsep(tag, fd, repo);
1296 if (err == NULL) {
1297 (*tag)->refcnt++;
1298 err = got_repo_cache_tag(repo, id, *tag);
1301 return err;
1304 const struct got_error *
1305 got_object_open_as_tag(struct got_tag_object **tag,
1306 struct got_repository *repo, struct got_object_id *id)
1308 *tag = got_repo_get_cached_tag(repo, id);
1309 if (*tag != NULL) {
1310 (*tag)->refcnt++;
1311 return NULL;
1314 return open_tag(tag, repo, id, 0);
1317 const struct got_error *
1318 got_object_tag_open(struct got_tag_object **tag,
1319 struct got_repository *repo, struct got_object *obj)
1321 return open_tag(tag, repo, got_object_get_id(obj), 1);
1324 int
1325 got_object_tag_get_object_type(struct got_tag_object *tag)
1327 return tag->obj_type;
1330 struct got_object_id *
1331 got_object_tag_get_object_id(struct got_tag_object *tag)
1333 return &tag->id;
1336 static struct got_tree_entry *
1337 find_entry_by_name(struct got_tree_object *tree, const char *name, size_t len)
1339 struct got_tree_entry *te;
1341 /* Note that tree entries are sorted in strncmp() order. */
1342 SIMPLEQ_FOREACH(te, &tree->entries.head, entry) {
1343 int cmp = strncmp(te->name, name, len);
1344 if (cmp < 0)
1345 continue;
1346 if (cmp > 0)
1347 break;
1348 if (te->name[len] == '\0')
1349 return te;
1351 return NULL;
1354 const struct got_tree_entry *
1355 got_object_tree_find_entry(struct got_tree_object *tree, const char *name)
1357 return find_entry_by_name(tree, name, strlen(name));
1360 const struct got_error *
1361 got_object_id_by_path(struct got_object_id **id, struct got_repository *repo,
1362 struct got_object_id *commit_id, const char *path)
1364 const struct got_error *err = NULL;
1365 struct got_commit_object *commit = NULL;
1366 struct got_tree_object *tree = NULL;
1367 struct got_tree_entry *te = NULL;
1368 const char *seg, *s;
1369 size_t seglen;
1371 *id = NULL;
1373 /* We are expecting an absolute in-repository path. */
1374 if (path[0] != '/')
1375 return got_error(GOT_ERR_NOT_ABSPATH);
1377 err = got_object_open_as_commit(&commit, repo, commit_id);
1378 if (err)
1379 goto done;
1381 /* Handle opening of root of commit's tree. */
1382 if (path[1] == '\0') {
1383 *id = got_object_id_dup(commit->tree_id);
1384 if (*id == NULL)
1385 err = got_error_prefix_errno("got_object_id_dup");
1386 goto done;
1389 err = got_object_open_as_tree(&tree, repo, commit->tree_id);
1390 if (err)
1391 goto done;
1393 s = path;
1394 s++; /* skip leading '/' */
1395 seg = s;
1396 seglen = 0;
1397 while (*s) {
1398 struct got_tree_object *next_tree;
1400 if (*s != '/') {
1401 s++;
1402 seglen++;
1403 if (*s)
1404 continue;
1407 te = find_entry_by_name(tree, seg, seglen);
1408 if (te == NULL) {
1409 err = got_error(GOT_ERR_NO_TREE_ENTRY);
1410 goto done;
1413 if (*s == '\0')
1414 break;
1416 seg = s + 1;
1417 seglen = 0;
1418 s++;
1419 if (*s) {
1420 err = got_object_open_as_tree(&next_tree, repo,
1421 te->id);
1422 te = NULL;
1423 if (err)
1424 goto done;
1425 got_object_tree_close(tree);
1426 tree = next_tree;
1430 if (te) {
1431 *id = got_object_id_dup(te->id);
1432 if (*id == NULL)
1433 return got_error_prefix_errno("got_object_id_dup");
1434 } else
1435 err = got_error(GOT_ERR_NO_TREE_ENTRY);
1436 done:
1437 if (commit)
1438 got_object_commit_close(commit);
1439 if (tree)
1440 got_object_tree_close(tree);
1441 return err;
1444 const struct got_error *
1445 got_object_tree_path_changed(int *changed,
1446 struct got_tree_object *tree01, struct got_tree_object *tree02,
1447 const char *path, struct got_repository *repo)
1449 const struct got_error *err = NULL;
1450 struct got_tree_object *tree1 = NULL, *tree2 = NULL;
1451 struct got_tree_entry *te1 = NULL, *te2 = NULL;
1452 const char *seg, *s;
1453 size_t seglen;
1455 *changed = 0;
1457 /* We are expecting an absolute in-repository path. */
1458 if (path[0] != '/')
1459 return got_error(GOT_ERR_NOT_ABSPATH);
1461 /* We not do support comparing the root path. */
1462 if (path[1] == '\0')
1463 return got_error(GOT_ERR_BAD_PATH);
1465 tree1 = tree01;
1466 tree2 = tree02;
1467 s = path;
1468 s++; /* skip leading '/' */
1469 seg = s;
1470 seglen = 0;
1471 while (*s) {
1472 struct got_tree_object *next_tree1, *next_tree2;
1474 if (*s != '/') {
1475 s++;
1476 seglen++;
1477 if (*s)
1478 continue;
1481 te1 = find_entry_by_name(tree1, seg, seglen);
1482 if (te1 == NULL) {
1483 err = got_error(GOT_ERR_NO_OBJ);
1484 goto done;
1487 te2 = find_entry_by_name(tree2, seg, seglen);
1488 if (te2 == NULL) {
1489 *changed = 1;
1490 goto done;
1493 if (te1->mode != te2->mode) {
1494 *changed = 1;
1495 goto done;
1498 if (got_object_id_cmp(te1->id, te2->id) == 0) {
1499 *changed = 0;
1500 goto done;
1503 if (*s == '\0') { /* final path element */
1504 *changed = 1;
1505 goto done;
1508 seg = s + 1;
1509 s++;
1510 seglen = 0;
1511 if (*s) {
1512 err = got_object_open_as_tree(&next_tree1, repo,
1513 te1->id);
1514 te1 = NULL;
1515 if (err)
1516 goto done;
1517 if (tree1 != tree01)
1518 got_object_tree_close(tree1);
1519 tree1 = next_tree1;
1521 err = got_object_open_as_tree(&next_tree2, repo,
1522 te2->id);
1523 te2 = NULL;
1524 if (err)
1525 goto done;
1526 if (tree2 != tree02)
1527 got_object_tree_close(tree2);
1528 tree2 = next_tree2;
1531 done:
1532 if (tree1 && tree1 != tree01)
1533 got_object_tree_close(tree1);
1534 if (tree2 && tree2 != tree02)
1535 got_object_tree_close(tree2);
1536 return err;
1539 const struct got_error *
1540 got_object_tree_entry_dup(struct got_tree_entry **new_te,
1541 struct got_tree_entry *te)
1543 const struct got_error *err = NULL;
1545 *new_te = calloc(1, sizeof(**new_te));
1546 if (*new_te == NULL)
1547 return got_error_prefix_errno("calloc");
1549 (*new_te)->mode = te->mode;
1550 (*new_te)->name = strdup(te->name);
1551 if ((*new_te)->name == NULL) {
1552 err = got_error_prefix_errno("strdup");
1553 goto done;
1556 (*new_te)->id = got_object_id_dup(te->id);
1557 if ((*new_te)->id == NULL) {
1558 err = got_error_prefix_errno("got_object_id_dup");
1559 goto done;
1561 done:
1562 if (err) {
1563 got_object_tree_entry_close(*new_te);
1564 *new_te = NULL;
1566 return err;