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>
24 #include <sys/resource.h>
26 #include <errno.h>
27 #include <fcntl.h>
28 #include <stdio.h>
29 #include <stdlib.h>
30 #include <string.h>
31 #include <stdint.h>
32 #include <sha1.h>
33 #include <zlib.h>
34 #include <ctype.h>
35 #include <limits.h>
36 #include <imsg.h>
37 #include <time.h>
39 #include "got_error.h"
40 #include "got_object.h"
41 #include "got_repository.h"
42 #include "got_opentemp.h"
43 #include "got_path.h"
45 #include "got_lib_sha1.h"
46 #include "got_lib_delta.h"
47 #include "got_lib_inflate.h"
48 #include "got_lib_object.h"
49 #include "got_lib_privsep.h"
50 #include "got_lib_object_idcache.h"
51 #include "got_lib_object_cache.h"
52 #include "got_lib_object_parse.h"
53 #include "got_lib_pack.h"
54 #include "got_lib_repository.h"
56 #ifndef MIN
57 #define MIN(_a,_b) ((_a) < (_b) ? (_a) : (_b))
58 #endif
60 struct got_object_id *
61 got_object_id_dup(struct got_object_id *id1)
62 {
63 struct got_object_id *id2;
65 id2 = malloc(sizeof(*id2));
66 if (id2 == NULL)
67 return NULL;
68 memcpy(id2, id1, sizeof(*id2));
69 return id2;
70 }
72 struct got_object_id *
73 got_object_get_id(struct got_object *obj)
74 {
75 return &obj->id;
76 }
78 const struct got_error *
79 got_object_get_id_str(char **outbuf, struct got_object *obj)
80 {
81 return got_object_id_str(outbuf, &obj->id);
82 }
84 const struct got_error *
85 got_object_get_type(int *type, struct got_repository *repo,
86 struct got_object_id *id)
87 {
88 const struct got_error *err = NULL;
89 struct got_object *obj;
91 err = got_object_open(&obj, repo, id);
92 if (err)
93 return err;
95 switch (obj->type) {
96 case GOT_OBJ_TYPE_COMMIT:
97 case GOT_OBJ_TYPE_TREE:
98 case GOT_OBJ_TYPE_BLOB:
99 case GOT_OBJ_TYPE_TAG:
100 *type = obj->type;
101 break;
102 default:
103 err = got_error(GOT_ERR_OBJ_TYPE);
104 break;
107 got_object_close(obj);
108 return err;
111 const struct got_error *
112 got_object_get_path(char **path, struct got_object_id *id,
113 struct got_repository *repo)
115 const struct got_error *err = NULL;
116 char *hex = NULL;
117 char *path_objects;
119 *path = NULL;
121 path_objects = got_repo_get_path_objects(repo);
122 if (path_objects == NULL)
123 return got_error_from_errno("got_repo_get_path_objects");
125 err = got_object_id_str(&hex, id);
126 if (err)
127 goto done;
129 if (asprintf(path, "%s/%.2x/%s", path_objects,
130 id->sha1[0], hex + 2) == -1)
131 err = got_error_from_errno("asprintf");
133 done:
134 free(hex);
135 free(path_objects);
136 return err;
139 static const struct got_error *
140 open_loose_object(int *fd, struct got_object_id *id,
141 struct got_repository *repo)
143 const struct got_error *err = NULL;
144 char *path;
146 err = got_object_get_path(&path, id, repo);
147 if (err)
148 return err;
149 *fd = open(path, O_RDONLY | O_NOFOLLOW);
150 if (*fd == -1) {
151 err = got_error_from_errno2("open", path);
152 goto done;
154 done:
155 free(path);
156 return err;
159 static const struct got_error *
160 get_packfile_path(char **path_packfile, struct got_packidx *packidx)
162 size_t size;
164 /* Packfile path contains ".pack" instead of ".idx", so add one byte. */
165 size = strlen(packidx->path_packidx) + 2;
166 if (size < GOT_PACKFILE_NAMELEN + 1)
167 return got_error(GOT_ERR_BAD_PATH);
169 *path_packfile = malloc(size);
170 if (*path_packfile == NULL)
171 return got_error_from_errno("malloc");
173 /* Copy up to and excluding ".idx". */
174 if (strlcpy(*path_packfile, packidx->path_packidx,
175 size - strlen(GOT_PACKIDX_SUFFIX) - 1) >= size)
176 return got_error(GOT_ERR_NO_SPACE);
178 if (strlcat(*path_packfile, GOT_PACKFILE_SUFFIX, size) >= size)
179 return got_error(GOT_ERR_NO_SPACE);
181 return NULL;
184 static const struct got_error *
185 request_packed_object(struct got_object **obj, struct got_pack *pack, int idx,
186 struct got_object_id *id)
188 const struct got_error *err = NULL;
189 struct imsgbuf *ibuf = pack->privsep_child->ibuf;
191 err = got_privsep_send_packed_obj_req(ibuf, idx, id);
192 if (err)
193 return err;
195 err = got_privsep_recv_obj(obj, ibuf);
196 if (err)
197 return err;
199 (*obj)->path_packfile = strdup(pack->path_packfile);
200 if ((*obj)->path_packfile == NULL) {
201 err = got_error_from_errno("strdup");
202 return err;
204 memcpy(&(*obj)->id, id, sizeof((*obj)->id));
206 return NULL;
209 static void
210 set_max_datasize(void)
212 struct rlimit rl;
214 if (getrlimit(RLIMIT_DATA, &rl) != 0)
215 return;
217 rl.rlim_cur = rl.rlim_max;
218 setrlimit(RLIMIT_DATA, &rl);
221 static const struct got_error *
222 start_pack_privsep_child(struct got_pack *pack, struct got_packidx *packidx)
224 const struct got_error *err = NULL;
225 int imsg_fds[2];
226 pid_t pid;
227 struct imsgbuf *ibuf;
229 ibuf = calloc(1, sizeof(*ibuf));
230 if (ibuf == NULL)
231 return got_error_from_errno("calloc");
233 pack->privsep_child = calloc(1, sizeof(*pack->privsep_child));
234 if (pack->privsep_child == NULL) {
235 err = got_error_from_errno("calloc");
236 free(ibuf);
237 return err;
240 if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_fds) == -1) {
241 err = got_error_from_errno("socketpair");
242 goto done;
245 pid = fork();
246 if (pid == -1) {
247 err = got_error_from_errno("fork");
248 goto done;
249 } else if (pid == 0) {
250 set_max_datasize();
251 got_privsep_exec_child(imsg_fds, GOT_PATH_PROG_READ_PACK,
252 pack->path_packfile);
253 /* not reached */
256 if (close(imsg_fds[1]) != 0)
257 return got_error_from_errno("close");
258 pack->privsep_child->imsg_fd = imsg_fds[0];
259 pack->privsep_child->pid = pid;
260 imsg_init(ibuf, imsg_fds[0]);
261 pack->privsep_child->ibuf = ibuf;
263 err = got_privsep_init_pack_child(ibuf, pack, packidx);
264 if (err) {
265 const struct got_error *child_err;
266 err = got_privsep_send_stop(pack->privsep_child->imsg_fd);
267 child_err = got_privsep_wait_for_child(
268 pack->privsep_child->pid);
269 if (child_err && err == NULL)
270 err = child_err;
272 done:
273 if (err) {
274 free(ibuf);
275 free(pack->privsep_child);
276 pack->privsep_child = NULL;
278 return err;
281 static const struct got_error *
282 read_packed_object_privsep(struct got_object **obj,
283 struct got_repository *repo, struct got_pack *pack,
284 struct got_packidx *packidx, int idx, struct got_object_id *id)
286 const struct got_error *err = NULL;
288 if (pack->privsep_child)
289 return request_packed_object(obj, pack, idx, id);
291 err = start_pack_privsep_child(pack, packidx);
292 if (err)
293 return err;
295 return request_packed_object(obj, pack, idx, id);
299 static const struct got_error *
300 open_packed_object(struct got_object **obj, struct got_object_id *id,
301 struct got_repository *repo)
303 const struct got_error *err = NULL;
304 struct got_pack *pack = NULL;
305 struct got_packidx *packidx = NULL;
306 int idx;
307 char *path_packfile;
309 err = got_repo_search_packidx(&packidx, &idx, repo, id);
310 if (err)
311 return err;
313 err = get_packfile_path(&path_packfile, packidx);
314 if (err)
315 return err;
317 pack = got_repo_get_cached_pack(repo, path_packfile);
318 if (pack == NULL) {
319 err = got_repo_cache_pack(&pack, repo, path_packfile, packidx);
320 if (err)
321 goto done;
324 err = read_packed_object_privsep(obj, repo, pack, packidx, idx, id);
325 if (err)
326 goto done;
328 err = got_repo_cache_pack(NULL, repo, (*obj)->path_packfile, packidx);
329 done:
330 free(path_packfile);
331 return err;
334 static const struct got_error *
335 request_object(struct got_object **obj, struct got_repository *repo, int fd)
337 const struct got_error *err = NULL;
338 struct imsgbuf *ibuf;
340 ibuf = repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_OBJECT].ibuf;
342 err = got_privsep_send_obj_req(ibuf, fd);
343 if (err)
344 return err;
346 return got_privsep_recv_obj(obj, ibuf);
349 static const struct got_error *
350 read_object_header_privsep(struct got_object **obj, struct got_repository *repo,
351 int obj_fd)
353 const struct got_error *err;
354 int imsg_fds[2];
355 pid_t pid;
356 struct imsgbuf *ibuf;
358 if (repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_OBJECT].imsg_fd != -1)
359 return request_object(obj, repo, obj_fd);
361 ibuf = calloc(1, sizeof(*ibuf));
362 if (ibuf == NULL)
363 return got_error_from_errno("calloc");
365 if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_fds) == -1) {
366 err = got_error_from_errno("socketpair");
367 free(ibuf);
368 return err;
371 pid = fork();
372 if (pid == -1) {
373 err = got_error_from_errno("fork");
374 free(ibuf);
375 return err;
377 else if (pid == 0) {
378 got_privsep_exec_child(imsg_fds, GOT_PATH_PROG_READ_OBJECT,
379 repo->path);
380 /* not reached */
383 if (close(imsg_fds[1]) != 0) {
384 err = got_error_from_errno("close");
385 free(ibuf);
386 return err;
388 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_OBJECT].imsg_fd =
389 imsg_fds[0];
390 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_OBJECT].pid = pid;
391 imsg_init(ibuf, imsg_fds[0]);
392 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_OBJECT].ibuf = ibuf;
394 return request_object(obj, repo, obj_fd);
398 const struct got_error *
399 got_object_open(struct got_object **obj, struct got_repository *repo,
400 struct got_object_id *id)
402 const struct got_error *err = NULL;
403 char *path;
404 int fd;
406 *obj = got_repo_get_cached_object(repo, id);
407 if (*obj != NULL) {
408 (*obj)->refcnt++;
409 return NULL;
412 err = open_packed_object(obj, id, repo);
413 if (err && err->code != GOT_ERR_NO_OBJ)
414 return err;
415 if (*obj) {
416 (*obj)->refcnt++;
417 return got_repo_cache_object(repo, id, *obj);
420 err = got_object_get_path(&path, id, repo);
421 if (err)
422 return err;
424 fd = open(path, O_RDONLY | O_NOFOLLOW);
425 if (fd == -1) {
426 if (errno == ENOENT)
427 err = got_error_no_obj(id);
428 else
429 err = got_error_from_errno2("open", path);
430 goto done;
431 } else {
432 err = read_object_header_privsep(obj, repo, fd);
433 if (err)
434 goto done;
435 memcpy((*obj)->id.sha1, id->sha1, SHA1_DIGEST_LENGTH);
438 (*obj)->refcnt++;
439 err = got_repo_cache_object(repo, id, *obj);
440 done:
441 free(path);
442 return err;
446 const struct got_error *
447 got_object_open_by_id_str(struct got_object **obj, struct got_repository *repo,
448 const char *id_str)
450 struct got_object_id id;
452 if (!got_parse_sha1_digest(id.sha1, id_str))
453 return got_error_path(id_str, GOT_ERR_BAD_OBJ_ID_STR);
455 return got_object_open(obj, repo, &id);
458 const struct got_error *
459 got_object_resolve_id_str(struct got_object_id **id,
460 struct got_repository *repo, const char *id_str)
462 const struct got_error *err = NULL;
463 struct got_object *obj;
465 err = got_object_open_by_id_str(&obj, repo, id_str);
466 if (err)
467 return err;
469 *id = got_object_id_dup(got_object_get_id(obj));
470 got_object_close(obj);
471 if (*id == NULL)
472 return got_error_from_errno("got_object_id_dup");
474 return NULL;
477 static const struct got_error *
478 request_packed_commit(struct got_commit_object **commit, struct got_pack *pack,
479 int pack_idx, struct got_object_id *id)
481 const struct got_error *err = NULL;
483 err = got_privsep_send_commit_req(pack->privsep_child->ibuf, -1, id,
484 pack_idx);
485 if (err)
486 return err;
488 return got_privsep_recv_commit(commit, pack->privsep_child->ibuf);
491 static const struct got_error *
492 read_packed_commit_privsep(struct got_commit_object **commit,
493 struct got_pack *pack, struct got_packidx *packidx, int idx,
494 struct got_object_id *id)
496 const struct got_error *err = NULL;
498 if (pack->privsep_child)
499 return request_packed_commit(commit, pack, idx, id);
501 err = start_pack_privsep_child(pack, packidx);
502 if (err)
503 return err;
505 return request_packed_commit(commit, pack, idx, id);
508 static const struct got_error *
509 request_commit(struct got_commit_object **commit, struct got_repository *repo,
510 int fd)
512 const struct got_error *err = NULL;
513 struct imsgbuf *ibuf;
515 ibuf = repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_COMMIT].ibuf;
517 err = got_privsep_send_commit_req(ibuf, fd, NULL, -1);
518 if (err)
519 return err;
521 return got_privsep_recv_commit(commit, ibuf);
524 static const struct got_error *
525 read_commit_privsep(struct got_commit_object **commit, int obj_fd,
526 struct got_repository *repo)
528 const struct got_error *err;
529 int imsg_fds[2];
530 pid_t pid;
531 struct imsgbuf *ibuf;
533 if (repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_COMMIT].imsg_fd != -1)
534 return request_commit(commit, repo, obj_fd);
536 ibuf = calloc(1, sizeof(*ibuf));
537 if (ibuf == NULL)
538 return got_error_from_errno("calloc");
540 if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_fds) == -1) {
541 err = got_error_from_errno("socketpair");
542 free(ibuf);
543 return err;
546 pid = fork();
547 if (pid == -1) {
548 err = got_error_from_errno("fork");
549 free(ibuf);
550 return err;
552 else if (pid == 0) {
553 got_privsep_exec_child(imsg_fds, GOT_PATH_PROG_READ_COMMIT,
554 repo->path);
555 /* not reached */
558 if (close(imsg_fds[1]) != 0) {
559 err = got_error_from_errno("close");
560 free(ibuf);
561 return err;
563 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_COMMIT].imsg_fd =
564 imsg_fds[0];
565 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_COMMIT].pid = pid;
566 imsg_init(ibuf, imsg_fds[0]);
567 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_COMMIT].ibuf = ibuf;
569 return request_commit(commit, repo, obj_fd);
573 static const struct got_error *
574 open_commit(struct got_commit_object **commit,
575 struct got_repository *repo, struct got_object_id *id, int check_cache)
577 const struct got_error *err = NULL;
578 struct got_packidx *packidx = NULL;
579 int idx;
580 char *path_packfile = NULL;
582 if (check_cache) {
583 *commit = got_repo_get_cached_commit(repo, id);
584 if (*commit != NULL) {
585 (*commit)->refcnt++;
586 return NULL;
588 } else
589 *commit = NULL;
591 err = got_repo_search_packidx(&packidx, &idx, repo, id);
592 if (err == NULL) {
593 struct got_pack *pack = NULL;
595 err = get_packfile_path(&path_packfile, packidx);
596 if (err)
597 return err;
599 pack = got_repo_get_cached_pack(repo, path_packfile);
600 if (pack == NULL) {
601 err = got_repo_cache_pack(&pack, repo, path_packfile,
602 packidx);
603 if (err)
604 goto done;
606 err = read_packed_commit_privsep(commit, pack,
607 packidx, idx, id);
608 } else if (err->code == GOT_ERR_NO_OBJ) {
609 int fd;
611 err = open_loose_object(&fd, id, repo);
612 if (err)
613 return err;
614 err = read_commit_privsep(commit, fd, repo);
617 if (err == NULL) {
618 (*commit)->refcnt++;
619 err = got_repo_cache_commit(repo, id, *commit);
621 done:
622 free(path_packfile);
623 return err;
626 const struct got_error *
627 got_object_open_as_commit(struct got_commit_object **commit,
628 struct got_repository *repo, struct got_object_id *id)
630 *commit = got_repo_get_cached_commit(repo, id);
631 if (*commit != NULL) {
632 (*commit)->refcnt++;
633 return NULL;
636 return open_commit(commit, repo, id, 0);
639 const struct got_error *
640 got_object_commit_open(struct got_commit_object **commit,
641 struct got_repository *repo, struct got_object *obj)
643 return open_commit(commit, repo, got_object_get_id(obj), 1);
646 const struct got_error *
647 got_object_qid_alloc(struct got_object_qid **qid, struct got_object_id *id)
649 const struct got_error *err = NULL;
651 *qid = calloc(1, sizeof(**qid));
652 if (*qid == NULL)
653 return got_error_from_errno("calloc");
655 (*qid)->id = got_object_id_dup(id);
656 if ((*qid)->id == NULL) {
657 err = got_error_from_errno("got_object_id_dup");
658 got_object_qid_free(*qid);
659 *qid = NULL;
660 return err;
663 return NULL;
666 static const struct got_error *
667 request_packed_tree(struct got_tree_object **tree, struct got_pack *pack,
668 int pack_idx, struct got_object_id *id)
670 const struct got_error *err = NULL;
672 err = got_privsep_send_tree_req(pack->privsep_child->ibuf, -1, id,
673 pack_idx);
674 if (err)
675 return err;
677 return got_privsep_recv_tree(tree, pack->privsep_child->ibuf);
680 static const struct got_error *
681 read_packed_tree_privsep(struct got_tree_object **tree,
682 struct got_pack *pack, struct got_packidx *packidx, int idx,
683 struct got_object_id *id)
685 const struct got_error *err = NULL;
687 if (pack->privsep_child)
688 return request_packed_tree(tree, pack, idx, id);
690 err = start_pack_privsep_child(pack, packidx);
691 if (err)
692 return err;
694 return request_packed_tree(tree, pack, idx, id);
697 static const struct got_error *
698 request_tree(struct got_tree_object **tree, struct got_repository *repo,
699 int fd)
701 const struct got_error *err = NULL;
702 struct imsgbuf *ibuf;
704 ibuf = repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TREE].ibuf;
706 err = got_privsep_send_tree_req(ibuf, fd, NULL, -1);
707 if (err)
708 return err;
710 return got_privsep_recv_tree(tree, ibuf);
713 const struct got_error *
714 read_tree_privsep(struct got_tree_object **tree, int obj_fd,
715 struct got_repository *repo)
717 const struct got_error *err;
718 int imsg_fds[2];
719 pid_t pid;
720 struct imsgbuf *ibuf;
722 if (repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TREE].imsg_fd != -1)
723 return request_tree(tree, repo, obj_fd);
725 ibuf = calloc(1, sizeof(*ibuf));
726 if (ibuf == NULL)
727 return got_error_from_errno("calloc");
729 if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_fds) == -1) {
730 err = got_error_from_errno("socketpair");
731 free(ibuf);
732 return err;
735 pid = fork();
736 if (pid == -1) {
737 err = got_error_from_errno("fork");
738 free(ibuf);
739 return err;
741 else if (pid == 0) {
742 got_privsep_exec_child(imsg_fds, GOT_PATH_PROG_READ_TREE,
743 repo->path);
744 /* not reached */
747 if (close(imsg_fds[1]) != 0) {
748 err = got_error_from_errno("close");
749 free(ibuf);
750 return err;
752 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TREE].imsg_fd =
753 imsg_fds[0];
754 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TREE].pid = pid;
755 imsg_init(ibuf, imsg_fds[0]);
756 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TREE].ibuf = ibuf;
759 return request_tree(tree, repo, obj_fd);
762 static const struct got_error *
763 open_tree(struct got_tree_object **tree, struct got_repository *repo,
764 struct got_object_id *id, int check_cache)
766 const struct got_error *err = NULL;
767 struct got_packidx *packidx = NULL;
768 int idx;
769 char *path_packfile = NULL;
771 if (check_cache) {
772 *tree = got_repo_get_cached_tree(repo, id);
773 if (*tree != NULL) {
774 (*tree)->refcnt++;
775 return NULL;
777 } else
778 *tree = NULL;
780 err = got_repo_search_packidx(&packidx, &idx, repo, id);
781 if (err == NULL) {
782 struct got_pack *pack = NULL;
784 err = get_packfile_path(&path_packfile, packidx);
785 if (err)
786 return err;
788 pack = got_repo_get_cached_pack(repo, path_packfile);
789 if (pack == NULL) {
790 err = got_repo_cache_pack(&pack, repo, path_packfile,
791 packidx);
792 if (err)
793 goto done;
795 err = read_packed_tree_privsep(tree, pack,
796 packidx, idx, id);
797 } else if (err->code == GOT_ERR_NO_OBJ) {
798 int fd;
800 err = open_loose_object(&fd, id, repo);
801 if (err)
802 return err;
803 err = read_tree_privsep(tree, fd, repo);
806 if (err == NULL) {
807 (*tree)->refcnt++;
808 err = got_repo_cache_tree(repo, id, *tree);
810 done:
811 free(path_packfile);
812 return err;
815 const struct got_error *
816 got_object_open_as_tree(struct got_tree_object **tree,
817 struct got_repository *repo, struct got_object_id *id)
819 *tree = got_repo_get_cached_tree(repo, id);
820 if (*tree != NULL) {
821 (*tree)->refcnt++;
822 return NULL;
825 return open_tree(tree, repo, id, 0);
828 const struct got_error *
829 got_object_tree_open(struct got_tree_object **tree,
830 struct got_repository *repo, struct got_object *obj)
832 return open_tree(tree, repo, got_object_get_id(obj), 1);
835 const struct got_tree_entries *
836 got_object_tree_get_entries(struct got_tree_object *tree)
838 return &tree->entries;
841 static const struct got_error *
842 request_packed_blob(uint8_t **outbuf, size_t *size, size_t *hdrlen, int outfd,
843 struct got_pack *pack, struct got_packidx *packidx, int idx,
844 struct got_object_id *id)
846 const struct got_error *err = NULL;
847 int outfd_child;
848 int basefd, accumfd; /* temporary files for delta application */
850 basefd = got_opentempfd();
851 if (basefd == -1)
852 return got_error_from_errno("got_opentempfd");
853 accumfd = got_opentempfd();
854 if (accumfd == -1)
855 return got_error_from_errno("got_opentempfd");
857 outfd_child = dup(outfd);
858 if (outfd_child == -1)
859 return got_error_from_errno("dup");
861 err = got_privsep_send_blob_req(pack->privsep_child->ibuf, -1, id, idx);
862 if (err)
863 return err;
865 err = got_privsep_send_blob_outfd(pack->privsep_child->ibuf,
866 outfd_child);
867 if (err) {
868 close(basefd);
869 close(accumfd);
870 return err;
873 err = got_privsep_send_tmpfd(pack->privsep_child->ibuf,
874 basefd);
875 if (err) {
876 close(accumfd);
877 return err;
880 err = got_privsep_send_tmpfd(pack->privsep_child->ibuf,
881 accumfd);
882 if (err)
883 return err;
885 err = got_privsep_recv_blob(outbuf, size, hdrlen,
886 pack->privsep_child->ibuf);
887 if (err)
888 return err;
890 if (lseek(outfd, SEEK_SET, 0) == -1)
891 err = got_error_from_errno("lseek");
893 return err;
896 static const struct got_error *
897 read_packed_blob_privsep(uint8_t **outbuf, size_t *size, size_t *hdrlen,
898 int outfd, struct got_pack *pack, struct got_packidx *packidx, int idx,
899 struct got_object_id *id)
901 const struct got_error *err = NULL;
903 if (pack->privsep_child == NULL) {
904 err = start_pack_privsep_child(pack, packidx);
905 if (err)
906 return err;
909 return request_packed_blob(outbuf, size, hdrlen, outfd, pack, packidx,
910 idx, id);
913 static const struct got_error *
914 request_blob(uint8_t **outbuf, size_t *size, size_t *hdrlen, int outfd,
915 int infd, struct imsgbuf *ibuf)
917 const struct got_error *err = NULL;
918 int outfd_child;
920 outfd_child = dup(outfd);
921 if (outfd_child == -1)
922 return got_error_from_errno("dup");
924 err = got_privsep_send_blob_req(ibuf, infd, NULL, -1);
925 if (err)
926 return err;
928 err = got_privsep_send_blob_outfd(ibuf, outfd_child);
929 if (err)
930 return err;
932 err = got_privsep_recv_blob(outbuf, size, hdrlen, ibuf);
933 if (err)
934 return err;
936 if (lseek(outfd, SEEK_SET, 0) == -1)
937 return got_error_from_errno("lseek");
939 return err;
942 static const struct got_error *
943 read_blob_privsep(uint8_t **outbuf, size_t *size, size_t *hdrlen,
944 int outfd, int infd, struct got_repository *repo)
946 const struct got_error *err;
947 int imsg_fds[2];
948 pid_t pid;
949 struct imsgbuf *ibuf;
951 if (repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_BLOB].imsg_fd != -1) {
952 ibuf = repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_BLOB].ibuf;
953 return request_blob(outbuf, size, hdrlen, outfd, infd, ibuf);
956 ibuf = calloc(1, sizeof(*ibuf));
957 if (ibuf == NULL)
958 return got_error_from_errno("calloc");
960 if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_fds) == -1) {
961 err = got_error_from_errno("socketpair");
962 free(ibuf);
963 return err;
966 pid = fork();
967 if (pid == -1) {
968 err = got_error_from_errno("fork");
969 free(ibuf);
970 return err;
972 else if (pid == 0) {
973 got_privsep_exec_child(imsg_fds, GOT_PATH_PROG_READ_BLOB,
974 repo->path);
975 /* not reached */
978 if (close(imsg_fds[1]) != 0) {
979 err = got_error_from_errno("close");
980 free(ibuf);
981 return err;
983 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_BLOB].imsg_fd =
984 imsg_fds[0];
985 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_BLOB].pid = pid;
986 imsg_init(ibuf, imsg_fds[0]);
987 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_BLOB].ibuf = ibuf;
989 return request_blob(outbuf, size, hdrlen, outfd, infd, ibuf);
992 static const struct got_error *
993 open_blob(struct got_blob_object **blob, struct got_repository *repo,
994 struct got_object_id *id, size_t blocksize)
996 const struct got_error *err = NULL;
997 struct got_packidx *packidx = NULL;
998 int idx;
999 char *path_packfile = NULL;
1000 uint8_t *outbuf;
1001 int outfd;
1002 size_t size, hdrlen;
1003 struct stat sb;
1005 *blob = calloc(1, sizeof(**blob));
1006 if (*blob == NULL)
1007 return got_error_from_errno("calloc");
1009 outfd = got_opentempfd();
1010 if (outfd == -1)
1011 return got_error_from_errno("got_opentempfd");
1013 (*blob)->read_buf = malloc(blocksize);
1014 if ((*blob)->read_buf == NULL) {
1015 err = got_error_from_errno("malloc");
1016 goto done;
1019 err = got_repo_search_packidx(&packidx, &idx, repo, id);
1020 if (err == NULL) {
1021 struct got_pack *pack = NULL;
1023 err = get_packfile_path(&path_packfile, packidx);
1024 if (err)
1025 goto done;
1027 pack = got_repo_get_cached_pack(repo, path_packfile);
1028 if (pack == NULL) {
1029 err = got_repo_cache_pack(&pack, repo, path_packfile,
1030 packidx);
1031 if (err)
1032 goto done;
1034 err = read_packed_blob_privsep(&outbuf, &size, &hdrlen, outfd,
1035 pack, packidx, idx, id);
1036 } else if (err->code == GOT_ERR_NO_OBJ) {
1037 int infd;
1039 err = open_loose_object(&infd, id, repo);
1040 if (err)
1041 goto done;
1042 err = read_blob_privsep(&outbuf, &size, &hdrlen, outfd, infd,
1043 repo);
1045 if (err)
1046 goto done;
1048 if (hdrlen > size) {
1049 err = got_error(GOT_ERR_BAD_OBJ_HDR);
1050 goto done;
1053 if (outbuf) {
1054 if (close(outfd) != 0 && err == NULL)
1055 err = got_error_from_errno("close");
1056 outfd = -1;
1057 (*blob)->f = fmemopen(outbuf, size, "rb");
1058 if ((*blob)->f == NULL) {
1059 err = got_error_from_errno("fmemopen");
1060 free(outbuf);
1061 goto done;
1063 (*blob)->data = outbuf;
1064 } else {
1065 if (fstat(outfd, &sb) == -1) {
1066 err = got_error_from_errno("fstat");
1067 goto done;
1070 if (sb.st_size != size) {
1071 err = got_error(GOT_ERR_PRIVSEP_LEN);
1072 goto done;
1075 (*blob)->f = fdopen(outfd, "rb");
1076 if ((*blob)->f == NULL) {
1077 err = got_error_from_errno("fdopen");
1078 close(outfd);
1079 outfd = -1;
1080 goto done;
1084 (*blob)->hdrlen = hdrlen;
1085 (*blob)->blocksize = blocksize;
1086 memcpy(&(*blob)->id.sha1, id->sha1, SHA1_DIGEST_LENGTH);
1088 done:
1089 free(path_packfile);
1090 if (err) {
1091 if (*blob) {
1092 got_object_blob_close(*blob);
1093 *blob = NULL;
1094 } else if (outfd != -1)
1095 close(outfd);
1097 return err;
1100 const struct got_error *
1101 got_object_open_as_blob(struct got_blob_object **blob,
1102 struct got_repository *repo, struct got_object_id *id,
1103 size_t blocksize)
1105 return open_blob(blob, repo, id, blocksize);
1108 const struct got_error *
1109 got_object_blob_open(struct got_blob_object **blob,
1110 struct got_repository *repo, struct got_object *obj, size_t blocksize)
1112 return open_blob(blob, repo, got_object_get_id(obj), blocksize);
1115 const struct got_error *
1116 got_object_blob_close(struct got_blob_object *blob)
1118 const struct got_error *err = NULL;
1119 free(blob->read_buf);
1120 if (blob->f && fclose(blob->f) != 0)
1121 err = got_error_from_errno("fclose");
1122 free(blob->data);
1123 free(blob);
1124 return err;
1127 char *
1128 got_object_blob_id_str(struct got_blob_object *blob, char *buf, size_t size)
1130 return got_sha1_digest_to_str(blob->id.sha1, buf, size);
1133 size_t
1134 got_object_blob_get_hdrlen(struct got_blob_object *blob)
1136 return blob->hdrlen;
1139 const uint8_t *
1140 got_object_blob_get_read_buf(struct got_blob_object *blob)
1142 return blob->read_buf;
1145 const struct got_error *
1146 got_object_blob_read_block(size_t *outlenp, struct got_blob_object *blob)
1148 size_t n;
1150 n = fread(blob->read_buf, 1, blob->blocksize, blob->f);
1151 if (n == 0 && ferror(blob->f))
1152 return got_ferror(blob->f, GOT_ERR_IO);
1153 *outlenp = n;
1154 return NULL;
1157 const struct got_error *
1158 got_object_blob_dump_to_file(size_t *filesize, int *nlines,
1159 off_t **line_offsets, FILE *outfile, struct got_blob_object *blob)
1161 const struct got_error *err = NULL;
1162 size_t n, len, hdrlen;
1163 const uint8_t *buf;
1164 int i;
1165 size_t noffsets = 0;
1166 off_t off = 0, total_len = 0;
1168 if (line_offsets)
1169 *line_offsets = NULL;
1170 if (filesize)
1171 *filesize = 0;
1172 if (nlines)
1173 *nlines = 0;
1175 hdrlen = got_object_blob_get_hdrlen(blob);
1176 do {
1177 err = got_object_blob_read_block(&len, blob);
1178 if (err)
1179 return err;
1180 if (len == 0)
1181 break;
1182 buf = got_object_blob_get_read_buf(blob);
1183 i = hdrlen;
1184 if (line_offsets && nlines) {
1185 if (*line_offsets == NULL) {
1186 /* Have some data but perhaps no '\n'. */
1187 noffsets = 1;
1188 *nlines = 1;
1189 *line_offsets = calloc(1, sizeof(**line_offsets));
1190 if (*line_offsets == NULL)
1191 return got_error_from_errno("malloc");
1193 /* Skip forward over end of first line. */
1194 while (i < len) {
1195 if (buf[i] == '\n')
1196 break;
1197 i++;
1200 /* Scan '\n' offsets in remaining chunk of data. */
1201 while (i < len) {
1202 if (buf[i] != '\n') {
1203 i++;
1204 continue;
1206 (*nlines)++;
1207 if (noffsets < *nlines) {
1208 off_t *o = recallocarray(*line_offsets,
1209 noffsets, *nlines,
1210 sizeof(**line_offsets));
1211 if (o == NULL) {
1212 free(*line_offsets);
1213 *line_offsets = NULL;
1214 return got_error_from_errno(
1215 "recallocarray");
1217 *line_offsets = o;
1218 noffsets = *nlines;
1220 off = total_len + i - hdrlen + 1;
1221 (*line_offsets)[*nlines - 1] = off;
1222 i++;
1225 /* Skip blob object header first time around. */
1226 n = fwrite(buf + hdrlen, 1, len - hdrlen, outfile);
1227 if (n != len - hdrlen)
1228 return got_ferror(outfile, GOT_ERR_IO);
1229 total_len += len - hdrlen;
1230 hdrlen = 0;
1231 } while (len != 0);
1233 if (fflush(outfile) != 0)
1234 return got_error_from_errno("fflush");
1235 rewind(outfile);
1237 if (filesize)
1238 *filesize = total_len;
1240 return NULL;
1243 static const struct got_error *
1244 request_packed_tag(struct got_tag_object **tag, struct got_pack *pack,
1245 int pack_idx, struct got_object_id *id)
1247 const struct got_error *err = NULL;
1249 err = got_privsep_send_tag_req(pack->privsep_child->ibuf, -1, id,
1250 pack_idx);
1251 if (err)
1252 return err;
1254 return got_privsep_recv_tag(tag, pack->privsep_child->ibuf);
1257 static const struct got_error *
1258 read_packed_tag_privsep(struct got_tag_object **tag,
1259 struct got_pack *pack, struct got_packidx *packidx, int idx,
1260 struct got_object_id *id)
1262 const struct got_error *err = NULL;
1264 if (pack->privsep_child)
1265 return request_packed_tag(tag, pack, idx, id);
1267 err = start_pack_privsep_child(pack, packidx);
1268 if (err)
1269 return err;
1271 return request_packed_tag(tag, pack, idx, id);
1274 static const struct got_error *
1275 request_tag(struct got_tag_object **tag, struct got_repository *repo,
1276 int fd)
1278 const struct got_error *err = NULL;
1279 struct imsgbuf *ibuf;
1281 ibuf = repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TAG].ibuf;
1283 err = got_privsep_send_tag_req(ibuf, fd, NULL, -1);
1284 if (err)
1285 return err;
1287 return got_privsep_recv_tag(tag, ibuf);
1290 static const struct got_error *
1291 read_tag_privsep(struct got_tag_object **tag, int obj_fd,
1292 struct got_repository *repo)
1294 const struct got_error *err;
1295 int imsg_fds[2];
1296 pid_t pid;
1297 struct imsgbuf *ibuf;
1299 if (repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TAG].imsg_fd != -1)
1300 return request_tag(tag, repo, obj_fd);
1302 ibuf = calloc(1, sizeof(*ibuf));
1303 if (ibuf == NULL)
1304 return got_error_from_errno("calloc");
1306 if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_fds) == -1) {
1307 err = got_error_from_errno("socketpair");
1308 free(ibuf);
1309 return err;
1312 pid = fork();
1313 if (pid == -1) {
1314 err = got_error_from_errno("fork");
1315 free(ibuf);
1316 return err;
1318 else if (pid == 0) {
1319 got_privsep_exec_child(imsg_fds, GOT_PATH_PROG_READ_TAG,
1320 repo->path);
1321 /* not reached */
1324 if (close(imsg_fds[1]) != 0) {
1325 err = got_error_from_errno("close");
1326 free(ibuf);
1327 return err;
1329 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TAG].imsg_fd =
1330 imsg_fds[0];
1331 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TAG].pid = pid;
1332 imsg_init(ibuf, imsg_fds[0]);
1333 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TAG].ibuf = ibuf;
1335 return request_tag(tag, repo, obj_fd);
1338 static const struct got_error *
1339 open_tag(struct got_tag_object **tag, struct got_repository *repo,
1340 struct got_object_id *id, int check_cache)
1342 const struct got_error *err = NULL;
1343 struct got_packidx *packidx = NULL;
1344 int idx;
1345 char *path_packfile = NULL;
1346 struct got_object *obj = NULL;
1347 int obj_type = GOT_OBJ_TYPE_ANY;
1349 if (check_cache) {
1350 *tag = got_repo_get_cached_tag(repo, id);
1351 if (*tag != NULL) {
1352 (*tag)->refcnt++;
1353 return NULL;
1355 } else
1356 *tag = NULL;
1358 err = got_repo_search_packidx(&packidx, &idx, repo, id);
1359 if (err == NULL) {
1360 struct got_pack *pack = NULL;
1362 err = get_packfile_path(&path_packfile, packidx);
1363 if (err)
1364 return err;
1366 pack = got_repo_get_cached_pack(repo, path_packfile);
1367 if (pack == NULL) {
1368 err = got_repo_cache_pack(&pack, repo, path_packfile,
1369 packidx);
1370 if (err)
1371 goto done;
1374 /* Beware of "leightweight" tags: Check object type first. */
1375 err = read_packed_object_privsep(&obj, repo, pack, packidx,
1376 idx, id);
1377 if (err)
1378 goto done;
1379 obj_type = obj->type;
1380 got_object_close(obj);
1381 if (obj_type != GOT_OBJ_TYPE_TAG) {
1382 err = got_error(GOT_ERR_OBJ_TYPE);
1383 goto done;
1385 err = read_packed_tag_privsep(tag, pack, packidx, idx, id);
1386 } else if (err->code == GOT_ERR_NO_OBJ) {
1387 int fd;
1389 err = open_loose_object(&fd, id, repo);
1390 if (err)
1391 return err;
1392 err = read_object_header_privsep(&obj, repo, fd);
1393 if (err)
1394 return err;
1395 obj_type = obj->type;
1396 got_object_close(obj);
1397 if (obj_type != GOT_OBJ_TYPE_TAG)
1398 return got_error(GOT_ERR_OBJ_TYPE);
1400 err = open_loose_object(&fd, id, repo);
1401 if (err)
1402 return err;
1403 err = read_tag_privsep(tag, fd, repo);
1406 if (err == NULL) {
1407 (*tag)->refcnt++;
1408 err = got_repo_cache_tag(repo, id, *tag);
1410 done:
1411 free(path_packfile);
1412 return err;
1415 const struct got_error *
1416 got_object_open_as_tag(struct got_tag_object **tag,
1417 struct got_repository *repo, struct got_object_id *id)
1419 *tag = got_repo_get_cached_tag(repo, id);
1420 if (*tag != NULL) {
1421 (*tag)->refcnt++;
1422 return NULL;
1425 return open_tag(tag, repo, id, 0);
1428 const struct got_error *
1429 got_object_tag_open(struct got_tag_object **tag,
1430 struct got_repository *repo, struct got_object *obj)
1432 return open_tag(tag, repo, got_object_get_id(obj), 1);
1435 const char *
1436 got_object_tag_get_name(struct got_tag_object *tag)
1438 return tag->tag;
1441 int
1442 got_object_tag_get_object_type(struct got_tag_object *tag)
1444 return tag->obj_type;
1447 struct got_object_id *
1448 got_object_tag_get_object_id(struct got_tag_object *tag)
1450 return &tag->id;
1453 time_t
1454 got_object_tag_get_tagger_time(struct got_tag_object *tag)
1456 return tag->tagger_time;
1459 time_t
1460 got_object_tag_get_tagger_gmtoff(struct got_tag_object *tag)
1462 return tag->tagger_gmtoff;
1465 const char *
1466 got_object_tag_get_tagger(struct got_tag_object *tag)
1468 return tag->tagger;
1471 const char *
1472 got_object_tag_get_message(struct got_tag_object *tag)
1474 return tag->tagmsg;
1477 static struct got_tree_entry *
1478 find_entry_by_name(struct got_tree_object *tree, const char *name, size_t len)
1480 struct got_tree_entry *te;
1482 /* Note that tree entries are sorted in strncmp() order. */
1483 SIMPLEQ_FOREACH(te, &tree->entries.head, entry) {
1484 int cmp = strncmp(te->name, name, len);
1485 if (cmp < 0)
1486 continue;
1487 if (cmp > 0)
1488 break;
1489 if (te->name[len] == '\0')
1490 return te;
1492 return NULL;
1495 const struct got_tree_entry *
1496 got_object_tree_find_entry(struct got_tree_object *tree, const char *name)
1498 return find_entry_by_name(tree, name, strlen(name));
1501 const struct got_error *
1502 got_object_id_by_path(struct got_object_id **id, struct got_repository *repo,
1503 struct got_object_id *commit_id, const char *path)
1505 const struct got_error *err = NULL;
1506 struct got_commit_object *commit = NULL;
1507 struct got_tree_object *tree = NULL;
1508 struct got_tree_entry *te = NULL;
1509 const char *seg, *s;
1510 size_t seglen;
1512 *id = NULL;
1514 err = got_object_open_as_commit(&commit, repo, commit_id);
1515 if (err)
1516 goto done;
1518 /* Handle opening of root of commit's tree. */
1519 if (got_path_is_root_dir(path)) {
1520 *id = got_object_id_dup(commit->tree_id);
1521 if (*id == NULL)
1522 err = got_error_from_errno("got_object_id_dup");
1523 goto done;
1526 err = got_object_open_as_tree(&tree, repo, commit->tree_id);
1527 if (err)
1528 goto done;
1530 s = path;
1531 while (s[0] == '/')
1532 s++;
1533 seg = s;
1534 seglen = 0;
1535 while (*s) {
1536 struct got_tree_object *next_tree;
1538 if (*s != '/') {
1539 s++;
1540 seglen++;
1541 if (*s)
1542 continue;
1545 te = find_entry_by_name(tree, seg, seglen);
1546 if (te == NULL) {
1547 err = got_error(GOT_ERR_NO_TREE_ENTRY);
1548 goto done;
1551 if (*s == '\0')
1552 break;
1554 seg = s + 1;
1555 seglen = 0;
1556 s++;
1557 if (*s) {
1558 err = got_object_open_as_tree(&next_tree, repo,
1559 te->id);
1560 te = NULL;
1561 if (err)
1562 goto done;
1563 got_object_tree_close(tree);
1564 tree = next_tree;
1568 if (te) {
1569 *id = got_object_id_dup(te->id);
1570 if (*id == NULL)
1571 return got_error_from_errno("got_object_id_dup");
1572 } else
1573 err = got_error(GOT_ERR_NO_TREE_ENTRY);
1574 done:
1575 if (commit)
1576 got_object_commit_close(commit);
1577 if (tree)
1578 got_object_tree_close(tree);
1579 return err;
1582 const struct got_error *
1583 got_object_tree_path_changed(int *changed,
1584 struct got_tree_object *tree01, struct got_tree_object *tree02,
1585 const char *path, struct got_repository *repo)
1587 const struct got_error *err = NULL;
1588 struct got_tree_object *tree1 = NULL, *tree2 = NULL;
1589 struct got_tree_entry *te1 = NULL, *te2 = NULL;
1590 const char *seg, *s;
1591 size_t seglen;
1593 *changed = 0;
1595 /* We are expecting an absolute in-repository path. */
1596 if (path[0] != '/')
1597 return got_error(GOT_ERR_NOT_ABSPATH);
1599 /* We not do support comparing the root path. */
1600 if (path[1] == '\0')
1601 return got_error(GOT_ERR_BAD_PATH);
1603 tree1 = tree01;
1604 tree2 = tree02;
1605 s = path;
1606 s++; /* skip leading '/' */
1607 seg = s;
1608 seglen = 0;
1609 while (*s) {
1610 struct got_tree_object *next_tree1, *next_tree2;
1612 if (*s != '/') {
1613 s++;
1614 seglen++;
1615 if (*s)
1616 continue;
1619 te1 = find_entry_by_name(tree1, seg, seglen);
1620 if (te1 == NULL) {
1621 err = got_error(GOT_ERR_NO_OBJ);
1622 goto done;
1625 te2 = find_entry_by_name(tree2, seg, seglen);
1626 if (te2 == NULL) {
1627 *changed = 1;
1628 goto done;
1631 if (te1->mode != te2->mode) {
1632 *changed = 1;
1633 goto done;
1636 if (got_object_id_cmp(te1->id, te2->id) == 0) {
1637 *changed = 0;
1638 goto done;
1641 if (*s == '\0') { /* final path element */
1642 *changed = 1;
1643 goto done;
1646 seg = s + 1;
1647 s++;
1648 seglen = 0;
1649 if (*s) {
1650 err = got_object_open_as_tree(&next_tree1, repo,
1651 te1->id);
1652 te1 = NULL;
1653 if (err)
1654 goto done;
1655 if (tree1 != tree01)
1656 got_object_tree_close(tree1);
1657 tree1 = next_tree1;
1659 err = got_object_open_as_tree(&next_tree2, repo,
1660 te2->id);
1661 te2 = NULL;
1662 if (err)
1663 goto done;
1664 if (tree2 != tree02)
1665 got_object_tree_close(tree2);
1666 tree2 = next_tree2;
1669 done:
1670 if (tree1 && tree1 != tree01)
1671 got_object_tree_close(tree1);
1672 if (tree2 && tree2 != tree02)
1673 got_object_tree_close(tree2);
1674 return err;
1677 const struct got_error *
1678 got_object_tree_entry_dup(struct got_tree_entry **new_te,
1679 struct got_tree_entry *te)
1681 const struct got_error *err = NULL;
1683 *new_te = calloc(1, sizeof(**new_te));
1684 if (*new_te == NULL)
1685 return got_error_from_errno("calloc");
1687 (*new_te)->mode = te->mode;
1688 (*new_te)->name = strdup(te->name);
1689 if ((*new_te)->name == NULL) {
1690 err = got_error_from_errno("strdup");
1691 goto done;
1694 (*new_te)->id = got_object_id_dup(te->id);
1695 if ((*new_te)->id == NULL) {
1696 err = got_error_from_errno("got_object_id_dup");
1697 goto done;
1699 done:
1700 if (err) {
1701 got_object_tree_entry_close(*new_te);
1702 *new_te = NULL;
1704 return err;
1707 int
1708 got_object_tree_entry_is_submodule(const struct got_tree_entry *te)
1710 return (te->mode & S_IFMT) == (S_IFDIR | S_IFLNK);