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 void
185 exec_privsep_child(int imsg_fds[2], const char *path, const char *repo_path)
187 if (close(imsg_fds[0]) != 0) {
188 fprintf(stderr, "%s: %s\n", getprogname(), strerror(errno));
189 _exit(1);
192 if (dup2(imsg_fds[1], GOT_IMSG_FD_CHILD) == -1) {
193 fprintf(stderr, "%s: %s\n", getprogname(), strerror(errno));
194 _exit(1);
196 if (closefrom(GOT_IMSG_FD_CHILD + 1) == -1) {
197 fprintf(stderr, "%s: %s\n", getprogname(), 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("strdup");
226 return err;
228 memcpy(&(*obj)->id, id, sizeof((*obj)->id));
230 return NULL;
233 static void
234 set_max_datasize(void)
236 struct rlimit rl;
238 if (getrlimit(RLIMIT_DATA, &rl) != 0)
239 return;
241 rl.rlim_cur = rl.rlim_max;
242 setrlimit(RLIMIT_DATA, &rl);
245 static const struct got_error *
246 start_pack_privsep_child(struct got_pack *pack, struct got_packidx *packidx)
248 const struct got_error *err = NULL;
249 int imsg_fds[2];
250 pid_t pid;
251 struct imsgbuf *ibuf;
253 ibuf = calloc(1, sizeof(*ibuf));
254 if (ibuf == NULL)
255 return got_error_from_errno("calloc");
257 pack->privsep_child = calloc(1, sizeof(*pack->privsep_child));
258 if (pack->privsep_child == NULL) {
259 err = got_error_from_errno("calloc");
260 free(ibuf);
261 return err;
264 if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_fds) == -1) {
265 err = got_error_from_errno("socketpair");
266 goto done;
269 pid = fork();
270 if (pid == -1) {
271 err = got_error_from_errno("fork");
272 goto done;
273 } else if (pid == 0) {
274 set_max_datasize();
275 exec_privsep_child(imsg_fds, GOT_PATH_PROG_READ_PACK,
276 pack->path_packfile);
277 /* not reached */
280 if (close(imsg_fds[1]) != 0)
281 return got_error_from_errno("close");
282 pack->privsep_child->imsg_fd = imsg_fds[0];
283 pack->privsep_child->pid = pid;
284 imsg_init(ibuf, imsg_fds[0]);
285 pack->privsep_child->ibuf = ibuf;
287 err = got_privsep_init_pack_child(ibuf, pack, packidx);
288 if (err) {
289 const struct got_error *child_err;
290 err = got_privsep_send_stop(pack->privsep_child->imsg_fd);
291 child_err = got_privsep_wait_for_child(
292 pack->privsep_child->pid);
293 if (child_err && err == NULL)
294 err = child_err;
296 done:
297 if (err) {
298 free(ibuf);
299 free(pack->privsep_child);
300 pack->privsep_child = NULL;
302 return err;
305 static const struct got_error *
306 read_packed_object_privsep(struct got_object **obj,
307 struct got_repository *repo, struct got_pack *pack,
308 struct got_packidx *packidx, int idx, struct got_object_id *id)
310 const struct got_error *err = NULL;
312 if (pack->privsep_child)
313 return request_packed_object(obj, pack, idx, id);
315 err = start_pack_privsep_child(pack, packidx);
316 if (err)
317 return err;
319 return request_packed_object(obj, pack, idx, id);
323 static const struct got_error *
324 open_packed_object(struct got_object **obj, struct got_object_id *id,
325 struct got_repository *repo)
327 const struct got_error *err = NULL;
328 struct got_pack *pack = NULL;
329 struct got_packidx *packidx = NULL;
330 int idx;
331 char *path_packfile;
333 err = got_repo_search_packidx(&packidx, &idx, repo, id);
334 if (err)
335 return err;
337 err = get_packfile_path(&path_packfile, packidx);
338 if (err)
339 return err;
341 pack = got_repo_get_cached_pack(repo, path_packfile);
342 if (pack == NULL) {
343 err = got_repo_cache_pack(&pack, repo, path_packfile, packidx);
344 if (err)
345 goto done;
348 err = read_packed_object_privsep(obj, repo, pack, packidx, idx, id);
349 if (err)
350 goto done;
352 err = got_repo_cache_pack(NULL, repo, (*obj)->path_packfile, packidx);
353 done:
354 free(path_packfile);
355 return err;
358 static const struct got_error *
359 request_object(struct got_object **obj, struct got_repository *repo, int fd)
361 const struct got_error *err = NULL;
362 struct imsgbuf *ibuf;
364 ibuf = repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_OBJECT].ibuf;
366 err = got_privsep_send_obj_req(ibuf, fd);
367 if (err)
368 return err;
370 return got_privsep_recv_obj(obj, ibuf);
373 static const struct got_error *
374 read_object_header_privsep(struct got_object **obj, struct got_repository *repo,
375 int obj_fd)
377 int imsg_fds[2];
378 pid_t pid;
379 struct imsgbuf *ibuf;
381 if (repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_OBJECT].imsg_fd != -1)
382 return request_object(obj, repo, obj_fd);
384 ibuf = calloc(1, sizeof(*ibuf));
385 if (ibuf == NULL)
386 return got_error_from_errno("calloc");
388 if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_fds) == -1)
389 return got_error_from_errno("socketpair");
391 pid = fork();
392 if (pid == -1)
393 return got_error_from_errno("fork");
394 else if (pid == 0) {
395 exec_privsep_child(imsg_fds, GOT_PATH_PROG_READ_OBJECT,
396 repo->path);
397 /* not reached */
400 if (close(imsg_fds[1]) != 0)
401 return got_error_from_errno("close");
402 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_OBJECT].imsg_fd =
403 imsg_fds[0];
404 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_OBJECT].pid = pid;
405 imsg_init(ibuf, imsg_fds[0]);
406 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_OBJECT].ibuf = ibuf;
408 return request_object(obj, repo, obj_fd);
412 const struct got_error *
413 got_object_open(struct got_object **obj, struct got_repository *repo,
414 struct got_object_id *id)
416 const struct got_error *err = NULL;
417 char *path;
418 int fd;
420 *obj = got_repo_get_cached_object(repo, id);
421 if (*obj != NULL) {
422 (*obj)->refcnt++;
423 return NULL;
426 err = open_packed_object(obj, id, repo);
427 if (err && err->code != GOT_ERR_NO_OBJ)
428 return err;
429 if (*obj) {
430 (*obj)->refcnt++;
431 return got_repo_cache_object(repo, id, *obj);
434 err = got_object_get_path(&path, id, repo);
435 if (err)
436 return err;
438 fd = open(path, O_RDONLY | O_NOFOLLOW);
439 if (fd == -1) {
440 if (errno == ENOENT)
441 err = got_error_no_obj(id);
442 else
443 err = got_error_from_errno2("open", path);
444 goto done;
445 } else {
446 err = read_object_header_privsep(obj, repo, fd);
447 if (err)
448 goto done;
449 memcpy((*obj)->id.sha1, id->sha1, SHA1_DIGEST_LENGTH);
452 (*obj)->refcnt++;
453 err = got_repo_cache_object(repo, id, *obj);
454 done:
455 free(path);
456 return err;
460 const struct got_error *
461 got_object_open_by_id_str(struct got_object **obj, struct got_repository *repo,
462 const char *id_str)
464 struct got_object_id id;
466 if (!got_parse_sha1_digest(id.sha1, id_str))
467 return got_error(GOT_ERR_BAD_OBJ_ID_STR);
469 return got_object_open(obj, repo, &id);
472 const struct got_error *
473 got_object_resolve_id_str(struct got_object_id **id,
474 struct got_repository *repo, const char *id_str)
476 const struct got_error *err = NULL;
477 struct got_object *obj;
479 err = got_object_open_by_id_str(&obj, repo, id_str);
480 if (err)
481 return err;
483 *id = got_object_id_dup(got_object_get_id(obj));
484 got_object_close(obj);
485 if (*id == NULL)
486 return got_error_from_errno("got_object_id_dup");
488 return NULL;
491 static const struct got_error *
492 request_packed_commit(struct got_commit_object **commit, struct got_pack *pack,
493 int pack_idx, struct got_object_id *id)
495 const struct got_error *err = NULL;
497 err = got_privsep_send_commit_req(pack->privsep_child->ibuf, -1, id,
498 pack_idx);
499 if (err)
500 return err;
502 return got_privsep_recv_commit(commit, pack->privsep_child->ibuf);
505 static const struct got_error *
506 read_packed_commit_privsep(struct got_commit_object **commit,
507 struct got_pack *pack, struct got_packidx *packidx, int idx,
508 struct got_object_id *id)
510 const struct got_error *err = NULL;
512 if (pack->privsep_child)
513 return request_packed_commit(commit, pack, idx, id);
515 err = start_pack_privsep_child(pack, packidx);
516 if (err)
517 return err;
519 return request_packed_commit(commit, pack, idx, id);
522 static const struct got_error *
523 request_commit(struct got_commit_object **commit, struct got_repository *repo,
524 int fd)
526 const struct got_error *err = NULL;
527 struct imsgbuf *ibuf;
529 ibuf = repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_COMMIT].ibuf;
531 err = got_privsep_send_commit_req(ibuf, fd, NULL, -1);
532 if (err)
533 return err;
535 return got_privsep_recv_commit(commit, ibuf);
538 static const struct got_error *
539 read_commit_privsep(struct got_commit_object **commit, int obj_fd,
540 struct got_repository *repo)
542 int imsg_fds[2];
543 pid_t pid;
544 struct imsgbuf *ibuf;
546 if (repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_COMMIT].imsg_fd != -1)
547 return request_commit(commit, repo, obj_fd);
549 ibuf = calloc(1, sizeof(*ibuf));
550 if (ibuf == NULL)
551 return got_error_from_errno("calloc");
553 if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_fds) == -1)
554 return got_error_from_errno("socketpair");
556 pid = fork();
557 if (pid == -1)
558 return got_error_from_errno("fork");
559 else if (pid == 0) {
560 exec_privsep_child(imsg_fds, GOT_PATH_PROG_READ_COMMIT,
561 repo->path);
562 /* not reached */
565 if (close(imsg_fds[1]) != 0)
566 return got_error_from_errno("close");
567 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_COMMIT].imsg_fd =
568 imsg_fds[0];
569 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_COMMIT].pid = pid;
570 imsg_init(ibuf, imsg_fds[0]);
571 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_COMMIT].ibuf = ibuf;
573 return request_commit(commit, repo, obj_fd);
577 static const struct got_error *
578 open_commit(struct got_commit_object **commit,
579 struct got_repository *repo, struct got_object_id *id, int check_cache)
581 const struct got_error *err = NULL;
582 struct got_packidx *packidx = NULL;
583 int idx;
584 char *path_packfile = NULL;
586 if (check_cache) {
587 *commit = got_repo_get_cached_commit(repo, id);
588 if (*commit != NULL) {
589 (*commit)->refcnt++;
590 return NULL;
592 } else
593 *commit = NULL;
595 err = got_repo_search_packidx(&packidx, &idx, repo, id);
596 if (err == NULL) {
597 struct got_pack *pack = NULL;
599 err = get_packfile_path(&path_packfile, packidx);
600 if (err)
601 return err;
603 pack = got_repo_get_cached_pack(repo, path_packfile);
604 if (pack == NULL) {
605 err = got_repo_cache_pack(&pack, repo, path_packfile,
606 packidx);
607 if (err)
608 goto done;
610 err = read_packed_commit_privsep(commit, pack,
611 packidx, idx, id);
612 } else if (err->code == GOT_ERR_NO_OBJ) {
613 int fd;
615 err = open_loose_object(&fd, id, repo);
616 if (err)
617 return err;
618 err = read_commit_privsep(commit, fd, repo);
621 if (err == NULL) {
622 (*commit)->refcnt++;
623 err = got_repo_cache_commit(repo, id, *commit);
625 done:
626 free(path_packfile);
627 return err;
630 const struct got_error *
631 got_object_open_as_commit(struct got_commit_object **commit,
632 struct got_repository *repo, struct got_object_id *id)
634 *commit = got_repo_get_cached_commit(repo, id);
635 if (*commit != NULL) {
636 (*commit)->refcnt++;
637 return NULL;
640 return open_commit(commit, repo, id, 0);
643 const struct got_error *
644 got_object_commit_open(struct got_commit_object **commit,
645 struct got_repository *repo, struct got_object *obj)
647 return open_commit(commit, repo, got_object_get_id(obj), 1);
650 const struct got_error *
651 got_object_qid_alloc(struct got_object_qid **qid, struct got_object_id *id)
653 const struct got_error *err = NULL;
655 *qid = calloc(1, sizeof(**qid));
656 if (*qid == NULL)
657 return got_error_from_errno("calloc");
659 (*qid)->id = got_object_id_dup(id);
660 if ((*qid)->id == NULL) {
661 err = got_error_from_errno("got_object_id_dup");
662 got_object_qid_free(*qid);
663 *qid = NULL;
664 return err;
667 return NULL;
670 static const struct got_error *
671 request_packed_tree(struct got_tree_object **tree, struct got_pack *pack,
672 int pack_idx, struct got_object_id *id)
674 const struct got_error *err = NULL;
676 err = got_privsep_send_tree_req(pack->privsep_child->ibuf, -1, id,
677 pack_idx);
678 if (err)
679 return err;
681 return got_privsep_recv_tree(tree, pack->privsep_child->ibuf);
684 static const struct got_error *
685 read_packed_tree_privsep(struct got_tree_object **tree,
686 struct got_pack *pack, struct got_packidx *packidx, int idx,
687 struct got_object_id *id)
689 const struct got_error *err = NULL;
691 if (pack->privsep_child)
692 return request_packed_tree(tree, pack, idx, id);
694 err = start_pack_privsep_child(pack, packidx);
695 if (err)
696 return err;
698 return request_packed_tree(tree, pack, idx, id);
701 static const struct got_error *
702 request_tree(struct got_tree_object **tree, struct got_repository *repo,
703 int fd)
705 const struct got_error *err = NULL;
706 struct imsgbuf *ibuf;
708 ibuf = repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TREE].ibuf;
710 err = got_privsep_send_tree_req(ibuf, fd, NULL, -1);
711 if (err)
712 return err;
714 return got_privsep_recv_tree(tree, ibuf);
717 const struct got_error *
718 read_tree_privsep(struct got_tree_object **tree, int obj_fd,
719 struct got_repository *repo)
721 int imsg_fds[2];
722 pid_t pid;
723 struct imsgbuf *ibuf;
725 if (repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TREE].imsg_fd != -1)
726 return request_tree(tree, repo, obj_fd);
728 ibuf = calloc(1, sizeof(*ibuf));
729 if (ibuf == NULL)
730 return got_error_from_errno("calloc");
732 if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_fds) == -1)
733 return got_error_from_errno("socketpair");
735 pid = fork();
736 if (pid == -1)
737 return got_error_from_errno("fork");
738 else if (pid == 0) {
739 exec_privsep_child(imsg_fds, GOT_PATH_PROG_READ_TREE,
740 repo->path);
741 /* not reached */
744 if (close(imsg_fds[1]) != 0)
745 return got_error_from_errno("close");
746 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TREE].imsg_fd =
747 imsg_fds[0];
748 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TREE].pid = pid;
749 imsg_init(ibuf, imsg_fds[0]);
750 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TREE].ibuf = ibuf;
753 return request_tree(tree, repo, obj_fd);
756 static const struct got_error *
757 open_tree(struct got_tree_object **tree, struct got_repository *repo,
758 struct got_object_id *id, int check_cache)
760 const struct got_error *err = NULL;
761 struct got_packidx *packidx = NULL;
762 int idx;
763 char *path_packfile = NULL;
765 if (check_cache) {
766 *tree = got_repo_get_cached_tree(repo, id);
767 if (*tree != NULL) {
768 (*tree)->refcnt++;
769 return NULL;
771 } else
772 *tree = NULL;
774 err = got_repo_search_packidx(&packidx, &idx, repo, id);
775 if (err == NULL) {
776 struct got_pack *pack = NULL;
778 err = get_packfile_path(&path_packfile, packidx);
779 if (err)
780 return err;
782 pack = got_repo_get_cached_pack(repo, path_packfile);
783 if (pack == NULL) {
784 err = got_repo_cache_pack(&pack, repo, path_packfile,
785 packidx);
786 if (err)
787 goto done;
789 err = read_packed_tree_privsep(tree, pack,
790 packidx, idx, id);
791 } else if (err->code == GOT_ERR_NO_OBJ) {
792 int fd;
794 err = open_loose_object(&fd, id, repo);
795 if (err)
796 return err;
797 err = read_tree_privsep(tree, fd, repo);
800 if (err == NULL) {
801 (*tree)->refcnt++;
802 err = got_repo_cache_tree(repo, id, *tree);
804 done:
805 free(path_packfile);
806 return err;
809 const struct got_error *
810 got_object_open_as_tree(struct got_tree_object **tree,
811 struct got_repository *repo, struct got_object_id *id)
813 *tree = got_repo_get_cached_tree(repo, id);
814 if (*tree != NULL) {
815 (*tree)->refcnt++;
816 return NULL;
819 return open_tree(tree, repo, id, 0);
822 const struct got_error *
823 got_object_tree_open(struct got_tree_object **tree,
824 struct got_repository *repo, struct got_object *obj)
826 return open_tree(tree, repo, got_object_get_id(obj), 1);
829 const struct got_tree_entries *
830 got_object_tree_get_entries(struct got_tree_object *tree)
832 return &tree->entries;
835 static const struct got_error *
836 request_packed_blob(uint8_t **outbuf, size_t *size, size_t *hdrlen, int outfd,
837 struct got_pack *pack, struct got_packidx *packidx, int idx,
838 struct got_object_id *id)
840 const struct got_error *err = NULL;
841 int outfd_child;
842 int basefd, accumfd; /* temporary files for delta application */
844 basefd = got_opentempfd();
845 if (basefd == -1)
846 return got_error_from_errno("got_opentempfd");
847 accumfd = got_opentempfd();
848 if (accumfd == -1)
849 return got_error_from_errno("got_opentempfd");
851 outfd_child = dup(outfd);
852 if (outfd_child == -1)
853 return got_error_from_errno("dup");
855 err = got_privsep_send_blob_req(pack->privsep_child->ibuf, -1, id, idx);
856 if (err)
857 return err;
859 err = got_privsep_send_blob_outfd(pack->privsep_child->ibuf,
860 outfd_child);
861 if (err) {
862 close(basefd);
863 close(accumfd);
864 return err;
867 err = got_privsep_send_tmpfd(pack->privsep_child->ibuf,
868 basefd);
869 if (err) {
870 close(accumfd);
871 return err;
874 err = got_privsep_send_tmpfd(pack->privsep_child->ibuf,
875 accumfd);
876 if (err)
877 return err;
879 err = got_privsep_recv_blob(outbuf, size, hdrlen,
880 pack->privsep_child->ibuf);
881 if (err)
882 return err;
884 if (lseek(outfd, SEEK_SET, 0) == -1)
885 err = got_error_from_errno("lseek");
887 return err;
890 static const struct got_error *
891 read_packed_blob_privsep(uint8_t **outbuf, size_t *size, size_t *hdrlen,
892 int outfd, struct got_pack *pack, struct got_packidx *packidx, int idx,
893 struct got_object_id *id)
895 const struct got_error *err = NULL;
897 if (pack->privsep_child == NULL) {
898 err = start_pack_privsep_child(pack, packidx);
899 if (err)
900 return err;
903 return request_packed_blob(outbuf, size, hdrlen, outfd, pack, packidx,
904 idx, id);
907 static const struct got_error *
908 request_blob(uint8_t **outbuf, size_t *size, size_t *hdrlen, int outfd,
909 int infd, struct imsgbuf *ibuf)
911 const struct got_error *err = NULL;
912 int outfd_child;
914 outfd_child = dup(outfd);
915 if (outfd_child == -1)
916 return got_error_from_errno("dup");
918 err = got_privsep_send_blob_req(ibuf, infd, NULL, -1);
919 if (err)
920 return err;
922 err = got_privsep_send_blob_outfd(ibuf, outfd_child);
923 if (err)
924 return err;
926 err = got_privsep_recv_blob(outbuf, size, hdrlen, ibuf);
927 if (err)
928 return err;
930 if (lseek(outfd, SEEK_SET, 0) == -1)
931 return got_error_from_errno("lseek");
933 return err;
936 static const struct got_error *
937 read_blob_privsep(uint8_t **outbuf, size_t *size, size_t *hdrlen,
938 int outfd, int infd, struct got_repository *repo)
940 int imsg_fds[2];
941 pid_t pid;
942 struct imsgbuf *ibuf;
944 if (repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_BLOB].imsg_fd != -1) {
945 ibuf = repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_BLOB].ibuf;
946 return request_blob(outbuf, size, hdrlen, outfd, infd, ibuf);
949 ibuf = calloc(1, sizeof(*ibuf));
950 if (ibuf == NULL)
951 return got_error_from_errno("calloc");
953 if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_fds) == -1)
954 return got_error_from_errno("socketpair");
956 pid = fork();
957 if (pid == -1)
958 return got_error_from_errno("fork");
959 else if (pid == 0) {
960 exec_privsep_child(imsg_fds, GOT_PATH_PROG_READ_BLOB,
961 repo->path);
962 /* not reached */
965 if (close(imsg_fds[1]) != 0)
966 return got_error_from_errno("close");
967 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_BLOB].imsg_fd =
968 imsg_fds[0];
969 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_BLOB].pid = pid;
970 imsg_init(ibuf, imsg_fds[0]);
971 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_BLOB].ibuf = ibuf;
973 return request_blob(outbuf, size, hdrlen, outfd, infd, ibuf);
976 static const struct got_error *
977 open_blob(struct got_blob_object **blob, struct got_repository *repo,
978 struct got_object_id *id, size_t blocksize)
980 const struct got_error *err = NULL;
981 struct got_packidx *packidx = NULL;
982 int idx;
983 char *path_packfile = NULL;
984 uint8_t *outbuf;
985 int outfd;
986 size_t size, hdrlen;
987 struct stat sb;
989 *blob = calloc(1, sizeof(**blob));
990 if (*blob == NULL)
991 return got_error_from_errno("calloc");
993 outfd = got_opentempfd();
994 if (outfd == -1)
995 return got_error_from_errno("got_opentempfd");
997 (*blob)->read_buf = malloc(blocksize);
998 if ((*blob)->read_buf == NULL) {
999 err = got_error_from_errno("malloc");
1000 goto done;
1003 err = got_repo_search_packidx(&packidx, &idx, repo, id);
1004 if (err == NULL) {
1005 struct got_pack *pack = NULL;
1007 err = get_packfile_path(&path_packfile, packidx);
1008 if (err)
1009 goto done;
1011 pack = got_repo_get_cached_pack(repo, path_packfile);
1012 if (pack == NULL) {
1013 err = got_repo_cache_pack(&pack, repo, path_packfile,
1014 packidx);
1015 if (err)
1016 goto done;
1018 err = read_packed_blob_privsep(&outbuf, &size, &hdrlen, outfd,
1019 pack, packidx, idx, id);
1020 } else if (err->code == GOT_ERR_NO_OBJ) {
1021 int infd;
1023 err = open_loose_object(&infd, id, repo);
1024 if (err)
1025 goto done;
1026 err = read_blob_privsep(&outbuf, &size, &hdrlen, outfd, infd,
1027 repo);
1029 if (err)
1030 goto done;
1032 if (hdrlen > size) {
1033 err = got_error(GOT_ERR_BAD_OBJ_HDR);
1034 goto done;
1037 if (outbuf) {
1038 if (close(outfd) != 0 && err == NULL)
1039 err = got_error_from_errno("close");
1040 outfd = -1;
1041 (*blob)->f = fmemopen(outbuf, size, "rb");
1042 if ((*blob)->f == NULL) {
1043 err = got_error_from_errno("fmemopen");
1044 free(outbuf);
1045 goto done;
1047 (*blob)->data = outbuf;
1048 } else {
1049 if (fstat(outfd, &sb) == -1) {
1050 err = got_error_from_errno("fstat");
1051 goto done;
1054 if (sb.st_size != size) {
1055 err = got_error(GOT_ERR_PRIVSEP_LEN);
1056 goto done;
1059 (*blob)->f = fdopen(outfd, "rb");
1060 if ((*blob)->f == NULL) {
1061 err = got_error_from_errno("fdopen");
1062 close(outfd);
1063 outfd = -1;
1064 goto done;
1068 (*blob)->hdrlen = hdrlen;
1069 (*blob)->blocksize = blocksize;
1070 memcpy(&(*blob)->id.sha1, id->sha1, SHA1_DIGEST_LENGTH);
1072 done:
1073 free(path_packfile);
1074 if (err) {
1075 if (*blob) {
1076 got_object_blob_close(*blob);
1077 *blob = NULL;
1078 } else if (outfd != -1)
1079 close(outfd);
1081 return err;
1084 const struct got_error *
1085 got_object_open_as_blob(struct got_blob_object **blob,
1086 struct got_repository *repo, struct got_object_id *id,
1087 size_t blocksize)
1089 return open_blob(blob, repo, id, blocksize);
1092 const struct got_error *
1093 got_object_blob_open(struct got_blob_object **blob,
1094 struct got_repository *repo, struct got_object *obj, size_t blocksize)
1096 return open_blob(blob, repo, got_object_get_id(obj), blocksize);
1099 const struct got_error *
1100 got_object_blob_close(struct got_blob_object *blob)
1102 const struct got_error *err = NULL;
1103 free(blob->read_buf);
1104 if (blob->f && fclose(blob->f) != 0)
1105 err = got_error_from_errno("fclose");
1106 free(blob->data);
1107 free(blob);
1108 return err;
1111 char *
1112 got_object_blob_id_str(struct got_blob_object *blob, char *buf, size_t size)
1114 return got_sha1_digest_to_str(blob->id.sha1, buf, size);
1117 size_t
1118 got_object_blob_get_hdrlen(struct got_blob_object *blob)
1120 return blob->hdrlen;
1123 const uint8_t *
1124 got_object_blob_get_read_buf(struct got_blob_object *blob)
1126 return blob->read_buf;
1129 const struct got_error *
1130 got_object_blob_read_block(size_t *outlenp, struct got_blob_object *blob)
1132 size_t n;
1134 n = fread(blob->read_buf, 1, blob->blocksize, blob->f);
1135 if (n == 0 && ferror(blob->f))
1136 return got_ferror(blob->f, GOT_ERR_IO);
1137 *outlenp = n;
1138 return NULL;
1141 const struct got_error *
1142 got_object_blob_dump_to_file(size_t *filesize, int *nlines,
1143 off_t **line_offsets, FILE *outfile, struct got_blob_object *blob)
1145 const struct got_error *err = NULL;
1146 size_t n, len, hdrlen;
1147 const uint8_t *buf;
1148 int i;
1149 size_t noffsets = 0;
1150 off_t off = 0, total_len = 0;
1152 if (line_offsets)
1153 *line_offsets = NULL;
1154 if (filesize)
1155 *filesize = 0;
1156 if (nlines)
1157 *nlines = 0;
1159 hdrlen = got_object_blob_get_hdrlen(blob);
1160 do {
1161 err = got_object_blob_read_block(&len, blob);
1162 if (err)
1163 return err;
1164 if (len == 0)
1165 break;
1166 buf = got_object_blob_get_read_buf(blob);
1167 i = hdrlen;
1168 if (line_offsets && nlines) {
1169 if (*line_offsets == NULL) {
1170 /* Have some data but perhaps no '\n'. */
1171 noffsets = 1;
1172 *nlines = 1;
1173 *line_offsets = calloc(1, sizeof(**line_offsets));
1174 if (*line_offsets == NULL)
1175 return got_error_from_errno("malloc");
1177 /* Skip forward over end of first line. */
1178 while (i < len) {
1179 if (buf[i] == '\n')
1180 break;
1181 i++;
1184 /* Scan '\n' offsets in remaining chunk of data. */
1185 while (i < len) {
1186 if (buf[i] != '\n') {
1187 i++;
1188 continue;
1190 (*nlines)++;
1191 if (noffsets < *nlines) {
1192 off_t *o = recallocarray(*line_offsets,
1193 noffsets, *nlines,
1194 sizeof(**line_offsets));
1195 if (o == NULL) {
1196 free(*line_offsets);
1197 *line_offsets = NULL;
1198 return got_error_from_errno(
1199 "recallocarray");
1201 *line_offsets = o;
1202 noffsets = *nlines;
1204 off = total_len + i - hdrlen + 1;
1205 (*line_offsets)[*nlines - 1] = off;
1206 i++;
1209 /* Skip blob object header first time around. */
1210 n = fwrite(buf + hdrlen, 1, len - hdrlen, outfile);
1211 if (n != len - hdrlen)
1212 return got_ferror(outfile, GOT_ERR_IO);
1213 total_len += len - hdrlen;
1214 hdrlen = 0;
1215 } while (len != 0);
1217 if (fflush(outfile) != 0)
1218 return got_error_from_errno("fflush");
1219 rewind(outfile);
1221 if (filesize)
1222 *filesize = total_len;
1224 return NULL;
1227 static const struct got_error *
1228 request_packed_tag(struct got_tag_object **tag, struct got_pack *pack,
1229 int pack_idx, struct got_object_id *id)
1231 const struct got_error *err = NULL;
1233 err = got_privsep_send_tag_req(pack->privsep_child->ibuf, -1, id,
1234 pack_idx);
1235 if (err)
1236 return err;
1238 return got_privsep_recv_tag(tag, pack->privsep_child->ibuf);
1241 static const struct got_error *
1242 read_packed_tag_privsep(struct got_tag_object **tag,
1243 struct got_pack *pack, struct got_packidx *packidx, int idx,
1244 struct got_object_id *id)
1246 const struct got_error *err = NULL;
1248 if (pack->privsep_child)
1249 return request_packed_tag(tag, pack, idx, id);
1251 err = start_pack_privsep_child(pack, packidx);
1252 if (err)
1253 return err;
1255 return request_packed_tag(tag, pack, idx, id);
1258 static const struct got_error *
1259 request_tag(struct got_tag_object **tag, struct got_repository *repo,
1260 int fd)
1262 const struct got_error *err = NULL;
1263 struct imsgbuf *ibuf;
1265 ibuf = repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TAG].ibuf;
1267 err = got_privsep_send_tag_req(ibuf, fd, NULL, -1);
1268 if (err)
1269 return err;
1271 return got_privsep_recv_tag(tag, ibuf);
1274 static const struct got_error *
1275 read_tag_privsep(struct got_tag_object **tag, int obj_fd,
1276 struct got_repository *repo)
1278 int imsg_fds[2];
1279 pid_t pid;
1280 struct imsgbuf *ibuf;
1282 if (repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TAG].imsg_fd != -1)
1283 return request_tag(tag, repo, obj_fd);
1285 ibuf = calloc(1, sizeof(*ibuf));
1286 if (ibuf == NULL)
1287 return got_error_from_errno("calloc");
1289 if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_fds) == -1)
1290 return got_error_from_errno("socketpair");
1292 pid = fork();
1293 if (pid == -1)
1294 return got_error_from_errno("fork");
1295 else if (pid == 0) {
1296 exec_privsep_child(imsg_fds, GOT_PATH_PROG_READ_TAG,
1297 repo->path);
1298 /* not reached */
1301 if (close(imsg_fds[1]) != 0)
1302 return got_error_from_errno("close");
1303 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TAG].imsg_fd =
1304 imsg_fds[0];
1305 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TAG].pid = pid;
1306 imsg_init(ibuf, imsg_fds[0]);
1307 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TAG].ibuf = ibuf;
1309 return request_tag(tag, repo, obj_fd);
1312 static const struct got_error *
1313 open_tag(struct got_tag_object **tag, struct got_repository *repo,
1314 struct got_object_id *id, int check_cache)
1316 const struct got_error *err = NULL;
1317 struct got_packidx *packidx = NULL;
1318 int idx;
1319 char *path_packfile = NULL;
1320 struct got_object *obj = NULL;
1321 int obj_type = GOT_OBJ_TYPE_ANY;
1323 if (check_cache) {
1324 *tag = got_repo_get_cached_tag(repo, id);
1325 if (*tag != NULL) {
1326 (*tag)->refcnt++;
1327 return NULL;
1329 } else
1330 *tag = NULL;
1332 err = got_repo_search_packidx(&packidx, &idx, repo, id);
1333 if (err == NULL) {
1334 struct got_pack *pack = NULL;
1336 err = get_packfile_path(&path_packfile, packidx);
1337 if (err)
1338 return err;
1340 pack = got_repo_get_cached_pack(repo, path_packfile);
1341 if (pack == NULL) {
1342 err = got_repo_cache_pack(&pack, repo, path_packfile,
1343 packidx);
1344 if (err)
1345 goto done;
1348 /* Beware of "leightweight" tags: Check object type first. */
1349 err = read_packed_object_privsep(&obj, repo, pack, packidx,
1350 idx, id);
1351 if (err)
1352 goto done;
1353 obj_type = obj->type;
1354 got_object_close(obj);
1355 if (obj_type != GOT_OBJ_TYPE_TAG) {
1356 err = got_error(GOT_ERR_OBJ_TYPE);
1357 goto done;
1359 err = read_packed_tag_privsep(tag, pack, packidx, idx, id);
1360 } else if (err->code == GOT_ERR_NO_OBJ) {
1361 int fd;
1363 err = open_loose_object(&fd, id, repo);
1364 if (err)
1365 return err;
1366 err = read_object_header_privsep(&obj, repo, fd);
1367 if (err)
1368 return err;
1369 obj_type = obj->type;
1370 got_object_close(obj);
1371 if (obj_type != GOT_OBJ_TYPE_TAG)
1372 return got_error(GOT_ERR_OBJ_TYPE);
1374 err = open_loose_object(&fd, id, repo);
1375 if (err)
1376 return err;
1377 err = read_tag_privsep(tag, fd, repo);
1380 if (err == NULL) {
1381 (*tag)->refcnt++;
1382 err = got_repo_cache_tag(repo, id, *tag);
1384 done:
1385 free(path_packfile);
1386 return err;
1389 const struct got_error *
1390 got_object_open_as_tag(struct got_tag_object **tag,
1391 struct got_repository *repo, struct got_object_id *id)
1393 *tag = got_repo_get_cached_tag(repo, id);
1394 if (*tag != NULL) {
1395 (*tag)->refcnt++;
1396 return NULL;
1399 return open_tag(tag, repo, id, 0);
1402 const struct got_error *
1403 got_object_tag_open(struct got_tag_object **tag,
1404 struct got_repository *repo, struct got_object *obj)
1406 return open_tag(tag, repo, got_object_get_id(obj), 1);
1409 const char *
1410 got_object_tag_get_name(struct got_tag_object *tag)
1412 return tag->tag;
1415 int
1416 got_object_tag_get_object_type(struct got_tag_object *tag)
1418 return tag->obj_type;
1421 struct got_object_id *
1422 got_object_tag_get_object_id(struct got_tag_object *tag)
1424 return &tag->id;
1427 time_t
1428 got_object_tag_get_tagger_time(struct got_tag_object *tag)
1430 return tag->tagger_time;
1433 time_t
1434 got_object_tag_get_tagger_gmtoff(struct got_tag_object *tag)
1436 return tag->tagger_gmtoff;
1439 const char *
1440 got_object_tag_get_tagger(struct got_tag_object *tag)
1442 return tag->tagger;
1445 const char *
1446 got_object_tag_get_message(struct got_tag_object *tag)
1448 return tag->tagmsg;
1451 static struct got_tree_entry *
1452 find_entry_by_name(struct got_tree_object *tree, const char *name, size_t len)
1454 struct got_tree_entry *te;
1456 /* Note that tree entries are sorted in strncmp() order. */
1457 SIMPLEQ_FOREACH(te, &tree->entries.head, entry) {
1458 int cmp = strncmp(te->name, name, len);
1459 if (cmp < 0)
1460 continue;
1461 if (cmp > 0)
1462 break;
1463 if (te->name[len] == '\0')
1464 return te;
1466 return NULL;
1469 const struct got_tree_entry *
1470 got_object_tree_find_entry(struct got_tree_object *tree, const char *name)
1472 return find_entry_by_name(tree, name, strlen(name));
1475 const struct got_error *
1476 got_object_id_by_path(struct got_object_id **id, struct got_repository *repo,
1477 struct got_object_id *commit_id, const char *path)
1479 const struct got_error *err = NULL;
1480 struct got_commit_object *commit = NULL;
1481 struct got_tree_object *tree = NULL;
1482 struct got_tree_entry *te = NULL;
1483 const char *seg, *s;
1484 size_t seglen;
1486 *id = NULL;
1488 err = got_object_open_as_commit(&commit, repo, commit_id);
1489 if (err)
1490 goto done;
1492 /* Handle opening of root of commit's tree. */
1493 if (got_path_is_root_dir(path)) {
1494 *id = got_object_id_dup(commit->tree_id);
1495 if (*id == NULL)
1496 err = got_error_from_errno("got_object_id_dup");
1497 goto done;
1500 err = got_object_open_as_tree(&tree, repo, commit->tree_id);
1501 if (err)
1502 goto done;
1504 s = path;
1505 while (s[0] == '/')
1506 s++;
1507 seg = s;
1508 seglen = 0;
1509 while (*s) {
1510 struct got_tree_object *next_tree;
1512 if (*s != '/') {
1513 s++;
1514 seglen++;
1515 if (*s)
1516 continue;
1519 te = find_entry_by_name(tree, seg, seglen);
1520 if (te == NULL) {
1521 err = got_error(GOT_ERR_NO_TREE_ENTRY);
1522 goto done;
1525 if (*s == '\0')
1526 break;
1528 seg = s + 1;
1529 seglen = 0;
1530 s++;
1531 if (*s) {
1532 err = got_object_open_as_tree(&next_tree, repo,
1533 te->id);
1534 te = NULL;
1535 if (err)
1536 goto done;
1537 got_object_tree_close(tree);
1538 tree = next_tree;
1542 if (te) {
1543 *id = got_object_id_dup(te->id);
1544 if (*id == NULL)
1545 return got_error_from_errno("got_object_id_dup");
1546 } else
1547 err = got_error(GOT_ERR_NO_TREE_ENTRY);
1548 done:
1549 if (commit)
1550 got_object_commit_close(commit);
1551 if (tree)
1552 got_object_tree_close(tree);
1553 return err;
1556 const struct got_error *
1557 got_object_tree_path_changed(int *changed,
1558 struct got_tree_object *tree01, struct got_tree_object *tree02,
1559 const char *path, struct got_repository *repo)
1561 const struct got_error *err = NULL;
1562 struct got_tree_object *tree1 = NULL, *tree2 = NULL;
1563 struct got_tree_entry *te1 = NULL, *te2 = NULL;
1564 const char *seg, *s;
1565 size_t seglen;
1567 *changed = 0;
1569 /* We are expecting an absolute in-repository path. */
1570 if (path[0] != '/')
1571 return got_error(GOT_ERR_NOT_ABSPATH);
1573 /* We not do support comparing the root path. */
1574 if (path[1] == '\0')
1575 return got_error(GOT_ERR_BAD_PATH);
1577 tree1 = tree01;
1578 tree2 = tree02;
1579 s = path;
1580 s++; /* skip leading '/' */
1581 seg = s;
1582 seglen = 0;
1583 while (*s) {
1584 struct got_tree_object *next_tree1, *next_tree2;
1586 if (*s != '/') {
1587 s++;
1588 seglen++;
1589 if (*s)
1590 continue;
1593 te1 = find_entry_by_name(tree1, seg, seglen);
1594 if (te1 == NULL) {
1595 err = got_error(GOT_ERR_NO_OBJ);
1596 goto done;
1599 te2 = find_entry_by_name(tree2, seg, seglen);
1600 if (te2 == NULL) {
1601 *changed = 1;
1602 goto done;
1605 if (te1->mode != te2->mode) {
1606 *changed = 1;
1607 goto done;
1610 if (got_object_id_cmp(te1->id, te2->id) == 0) {
1611 *changed = 0;
1612 goto done;
1615 if (*s == '\0') { /* final path element */
1616 *changed = 1;
1617 goto done;
1620 seg = s + 1;
1621 s++;
1622 seglen = 0;
1623 if (*s) {
1624 err = got_object_open_as_tree(&next_tree1, repo,
1625 te1->id);
1626 te1 = NULL;
1627 if (err)
1628 goto done;
1629 if (tree1 != tree01)
1630 got_object_tree_close(tree1);
1631 tree1 = next_tree1;
1633 err = got_object_open_as_tree(&next_tree2, repo,
1634 te2->id);
1635 te2 = NULL;
1636 if (err)
1637 goto done;
1638 if (tree2 != tree02)
1639 got_object_tree_close(tree2);
1640 tree2 = next_tree2;
1643 done:
1644 if (tree1 && tree1 != tree01)
1645 got_object_tree_close(tree1);
1646 if (tree2 && tree2 != tree02)
1647 got_object_tree_close(tree2);
1648 return err;
1651 const struct got_error *
1652 got_object_tree_entry_dup(struct got_tree_entry **new_te,
1653 struct got_tree_entry *te)
1655 const struct got_error *err = NULL;
1657 *new_te = calloc(1, sizeof(**new_te));
1658 if (*new_te == NULL)
1659 return got_error_from_errno("calloc");
1661 (*new_te)->mode = te->mode;
1662 (*new_te)->name = strdup(te->name);
1663 if ((*new_te)->name == NULL) {
1664 err = got_error_from_errno("strdup");
1665 goto done;
1668 (*new_te)->id = got_object_id_dup(te->id);
1669 if ((*new_te)->id == NULL) {
1670 err = got_error_from_errno("got_object_id_dup");
1671 goto done;
1673 done:
1674 if (err) {
1675 got_object_tree_entry_close(*new_te);
1676 *new_te = NULL;
1678 return err;