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 int imsg_fds[2];
354 pid_t pid;
355 struct imsgbuf *ibuf;
357 if (repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_OBJECT].imsg_fd != -1)
358 return request_object(obj, repo, obj_fd);
360 ibuf = calloc(1, sizeof(*ibuf));
361 if (ibuf == NULL)
362 return got_error_from_errno("calloc");
364 if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_fds) == -1)
365 return got_error_from_errno("socketpair");
367 pid = fork();
368 if (pid == -1)
369 return got_error_from_errno("fork");
370 else if (pid == 0) {
371 got_privsep_exec_child(imsg_fds, GOT_PATH_PROG_READ_OBJECT,
372 repo->path);
373 /* not reached */
376 if (close(imsg_fds[1]) != 0)
377 return got_error_from_errno("close");
378 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_OBJECT].imsg_fd =
379 imsg_fds[0];
380 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_OBJECT].pid = pid;
381 imsg_init(ibuf, imsg_fds[0]);
382 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_OBJECT].ibuf = ibuf;
384 return request_object(obj, repo, obj_fd);
388 const struct got_error *
389 got_object_open(struct got_object **obj, struct got_repository *repo,
390 struct got_object_id *id)
392 const struct got_error *err = NULL;
393 char *path;
394 int fd;
396 *obj = got_repo_get_cached_object(repo, id);
397 if (*obj != NULL) {
398 (*obj)->refcnt++;
399 return NULL;
402 err = open_packed_object(obj, id, repo);
403 if (err && err->code != GOT_ERR_NO_OBJ)
404 return err;
405 if (*obj) {
406 (*obj)->refcnt++;
407 return got_repo_cache_object(repo, id, *obj);
410 err = got_object_get_path(&path, id, repo);
411 if (err)
412 return err;
414 fd = open(path, O_RDONLY | O_NOFOLLOW);
415 if (fd == -1) {
416 if (errno == ENOENT)
417 err = got_error_no_obj(id);
418 else
419 err = got_error_from_errno2("open", path);
420 goto done;
421 } else {
422 err = read_object_header_privsep(obj, repo, fd);
423 if (err)
424 goto done;
425 memcpy((*obj)->id.sha1, id->sha1, SHA1_DIGEST_LENGTH);
428 (*obj)->refcnt++;
429 err = got_repo_cache_object(repo, id, *obj);
430 done:
431 free(path);
432 return err;
436 const struct got_error *
437 got_object_open_by_id_str(struct got_object **obj, struct got_repository *repo,
438 const char *id_str)
440 struct got_object_id id;
442 if (!got_parse_sha1_digest(id.sha1, id_str))
443 return got_error(GOT_ERR_BAD_OBJ_ID_STR);
445 return got_object_open(obj, repo, &id);
448 const struct got_error *
449 got_object_resolve_id_str(struct got_object_id **id,
450 struct got_repository *repo, const char *id_str)
452 const struct got_error *err = NULL;
453 struct got_object *obj;
455 err = got_object_open_by_id_str(&obj, repo, id_str);
456 if (err)
457 return err;
459 *id = got_object_id_dup(got_object_get_id(obj));
460 got_object_close(obj);
461 if (*id == NULL)
462 return got_error_from_errno("got_object_id_dup");
464 return NULL;
467 static const struct got_error *
468 request_packed_commit(struct got_commit_object **commit, struct got_pack *pack,
469 int pack_idx, struct got_object_id *id)
471 const struct got_error *err = NULL;
473 err = got_privsep_send_commit_req(pack->privsep_child->ibuf, -1, id,
474 pack_idx);
475 if (err)
476 return err;
478 return got_privsep_recv_commit(commit, pack->privsep_child->ibuf);
481 static const struct got_error *
482 read_packed_commit_privsep(struct got_commit_object **commit,
483 struct got_pack *pack, struct got_packidx *packidx, int idx,
484 struct got_object_id *id)
486 const struct got_error *err = NULL;
488 if (pack->privsep_child)
489 return request_packed_commit(commit, pack, idx, id);
491 err = start_pack_privsep_child(pack, packidx);
492 if (err)
493 return err;
495 return request_packed_commit(commit, pack, idx, id);
498 static const struct got_error *
499 request_commit(struct got_commit_object **commit, struct got_repository *repo,
500 int fd)
502 const struct got_error *err = NULL;
503 struct imsgbuf *ibuf;
505 ibuf = repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_COMMIT].ibuf;
507 err = got_privsep_send_commit_req(ibuf, fd, NULL, -1);
508 if (err)
509 return err;
511 return got_privsep_recv_commit(commit, ibuf);
514 static const struct got_error *
515 read_commit_privsep(struct got_commit_object **commit, int obj_fd,
516 struct got_repository *repo)
518 int imsg_fds[2];
519 pid_t pid;
520 struct imsgbuf *ibuf;
522 if (repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_COMMIT].imsg_fd != -1)
523 return request_commit(commit, repo, obj_fd);
525 ibuf = calloc(1, sizeof(*ibuf));
526 if (ibuf == NULL)
527 return got_error_from_errno("calloc");
529 if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_fds) == -1)
530 return got_error_from_errno("socketpair");
532 pid = fork();
533 if (pid == -1)
534 return got_error_from_errno("fork");
535 else if (pid == 0) {
536 got_privsep_exec_child(imsg_fds, GOT_PATH_PROG_READ_COMMIT,
537 repo->path);
538 /* not reached */
541 if (close(imsg_fds[1]) != 0)
542 return got_error_from_errno("close");
543 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_COMMIT].imsg_fd =
544 imsg_fds[0];
545 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_COMMIT].pid = pid;
546 imsg_init(ibuf, imsg_fds[0]);
547 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_COMMIT].ibuf = ibuf;
549 return request_commit(commit, repo, obj_fd);
553 static const struct got_error *
554 open_commit(struct got_commit_object **commit,
555 struct got_repository *repo, struct got_object_id *id, int check_cache)
557 const struct got_error *err = NULL;
558 struct got_packidx *packidx = NULL;
559 int idx;
560 char *path_packfile = NULL;
562 if (check_cache) {
563 *commit = got_repo_get_cached_commit(repo, id);
564 if (*commit != NULL) {
565 (*commit)->refcnt++;
566 return NULL;
568 } else
569 *commit = NULL;
571 err = got_repo_search_packidx(&packidx, &idx, repo, id);
572 if (err == NULL) {
573 struct got_pack *pack = NULL;
575 err = get_packfile_path(&path_packfile, packidx);
576 if (err)
577 return err;
579 pack = got_repo_get_cached_pack(repo, path_packfile);
580 if (pack == NULL) {
581 err = got_repo_cache_pack(&pack, repo, path_packfile,
582 packidx);
583 if (err)
584 goto done;
586 err = read_packed_commit_privsep(commit, pack,
587 packidx, idx, id);
588 } else if (err->code == GOT_ERR_NO_OBJ) {
589 int fd;
591 err = open_loose_object(&fd, id, repo);
592 if (err)
593 return err;
594 err = read_commit_privsep(commit, fd, repo);
597 if (err == NULL) {
598 (*commit)->refcnt++;
599 err = got_repo_cache_commit(repo, id, *commit);
601 done:
602 free(path_packfile);
603 return err;
606 const struct got_error *
607 got_object_open_as_commit(struct got_commit_object **commit,
608 struct got_repository *repo, struct got_object_id *id)
610 *commit = got_repo_get_cached_commit(repo, id);
611 if (*commit != NULL) {
612 (*commit)->refcnt++;
613 return NULL;
616 return open_commit(commit, repo, id, 0);
619 const struct got_error *
620 got_object_commit_open(struct got_commit_object **commit,
621 struct got_repository *repo, struct got_object *obj)
623 return open_commit(commit, repo, got_object_get_id(obj), 1);
626 const struct got_error *
627 got_object_qid_alloc(struct got_object_qid **qid, struct got_object_id *id)
629 const struct got_error *err = NULL;
631 *qid = calloc(1, sizeof(**qid));
632 if (*qid == NULL)
633 return got_error_from_errno("calloc");
635 (*qid)->id = got_object_id_dup(id);
636 if ((*qid)->id == NULL) {
637 err = got_error_from_errno("got_object_id_dup");
638 got_object_qid_free(*qid);
639 *qid = NULL;
640 return err;
643 return NULL;
646 static const struct got_error *
647 request_packed_tree(struct got_tree_object **tree, struct got_pack *pack,
648 int pack_idx, struct got_object_id *id)
650 const struct got_error *err = NULL;
652 err = got_privsep_send_tree_req(pack->privsep_child->ibuf, -1, id,
653 pack_idx);
654 if (err)
655 return err;
657 return got_privsep_recv_tree(tree, pack->privsep_child->ibuf);
660 static const struct got_error *
661 read_packed_tree_privsep(struct got_tree_object **tree,
662 struct got_pack *pack, struct got_packidx *packidx, int idx,
663 struct got_object_id *id)
665 const struct got_error *err = NULL;
667 if (pack->privsep_child)
668 return request_packed_tree(tree, pack, idx, id);
670 err = start_pack_privsep_child(pack, packidx);
671 if (err)
672 return err;
674 return request_packed_tree(tree, pack, idx, id);
677 static const struct got_error *
678 request_tree(struct got_tree_object **tree, struct got_repository *repo,
679 int fd)
681 const struct got_error *err = NULL;
682 struct imsgbuf *ibuf;
684 ibuf = repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TREE].ibuf;
686 err = got_privsep_send_tree_req(ibuf, fd, NULL, -1);
687 if (err)
688 return err;
690 return got_privsep_recv_tree(tree, ibuf);
693 const struct got_error *
694 read_tree_privsep(struct got_tree_object **tree, int obj_fd,
695 struct got_repository *repo)
697 int imsg_fds[2];
698 pid_t pid;
699 struct imsgbuf *ibuf;
701 if (repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TREE].imsg_fd != -1)
702 return request_tree(tree, repo, obj_fd);
704 ibuf = calloc(1, sizeof(*ibuf));
705 if (ibuf == NULL)
706 return got_error_from_errno("calloc");
708 if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_fds) == -1)
709 return got_error_from_errno("socketpair");
711 pid = fork();
712 if (pid == -1)
713 return got_error_from_errno("fork");
714 else if (pid == 0) {
715 got_privsep_exec_child(imsg_fds, GOT_PATH_PROG_READ_TREE,
716 repo->path);
717 /* not reached */
720 if (close(imsg_fds[1]) != 0)
721 return got_error_from_errno("close");
722 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TREE].imsg_fd =
723 imsg_fds[0];
724 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TREE].pid = pid;
725 imsg_init(ibuf, imsg_fds[0]);
726 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TREE].ibuf = ibuf;
729 return request_tree(tree, repo, obj_fd);
732 static const struct got_error *
733 open_tree(struct got_tree_object **tree, struct got_repository *repo,
734 struct got_object_id *id, int check_cache)
736 const struct got_error *err = NULL;
737 struct got_packidx *packidx = NULL;
738 int idx;
739 char *path_packfile = NULL;
741 if (check_cache) {
742 *tree = got_repo_get_cached_tree(repo, id);
743 if (*tree != NULL) {
744 (*tree)->refcnt++;
745 return NULL;
747 } else
748 *tree = NULL;
750 err = got_repo_search_packidx(&packidx, &idx, repo, id);
751 if (err == NULL) {
752 struct got_pack *pack = NULL;
754 err = get_packfile_path(&path_packfile, packidx);
755 if (err)
756 return err;
758 pack = got_repo_get_cached_pack(repo, path_packfile);
759 if (pack == NULL) {
760 err = got_repo_cache_pack(&pack, repo, path_packfile,
761 packidx);
762 if (err)
763 goto done;
765 err = read_packed_tree_privsep(tree, pack,
766 packidx, idx, id);
767 } else if (err->code == GOT_ERR_NO_OBJ) {
768 int fd;
770 err = open_loose_object(&fd, id, repo);
771 if (err)
772 return err;
773 err = read_tree_privsep(tree, fd, repo);
776 if (err == NULL) {
777 (*tree)->refcnt++;
778 err = got_repo_cache_tree(repo, id, *tree);
780 done:
781 free(path_packfile);
782 return err;
785 const struct got_error *
786 got_object_open_as_tree(struct got_tree_object **tree,
787 struct got_repository *repo, struct got_object_id *id)
789 *tree = got_repo_get_cached_tree(repo, id);
790 if (*tree != NULL) {
791 (*tree)->refcnt++;
792 return NULL;
795 return open_tree(tree, repo, id, 0);
798 const struct got_error *
799 got_object_tree_open(struct got_tree_object **tree,
800 struct got_repository *repo, struct got_object *obj)
802 return open_tree(tree, repo, got_object_get_id(obj), 1);
805 const struct got_tree_entries *
806 got_object_tree_get_entries(struct got_tree_object *tree)
808 return &tree->entries;
811 static const struct got_error *
812 request_packed_blob(uint8_t **outbuf, size_t *size, size_t *hdrlen, int outfd,
813 struct got_pack *pack, struct got_packidx *packidx, int idx,
814 struct got_object_id *id)
816 const struct got_error *err = NULL;
817 int outfd_child;
818 int basefd, accumfd; /* temporary files for delta application */
820 basefd = got_opentempfd();
821 if (basefd == -1)
822 return got_error_from_errno("got_opentempfd");
823 accumfd = got_opentempfd();
824 if (accumfd == -1)
825 return got_error_from_errno("got_opentempfd");
827 outfd_child = dup(outfd);
828 if (outfd_child == -1)
829 return got_error_from_errno("dup");
831 err = got_privsep_send_blob_req(pack->privsep_child->ibuf, -1, id, idx);
832 if (err)
833 return err;
835 err = got_privsep_send_blob_outfd(pack->privsep_child->ibuf,
836 outfd_child);
837 if (err) {
838 close(basefd);
839 close(accumfd);
840 return err;
843 err = got_privsep_send_tmpfd(pack->privsep_child->ibuf,
844 basefd);
845 if (err) {
846 close(accumfd);
847 return err;
850 err = got_privsep_send_tmpfd(pack->privsep_child->ibuf,
851 accumfd);
852 if (err)
853 return err;
855 err = got_privsep_recv_blob(outbuf, size, hdrlen,
856 pack->privsep_child->ibuf);
857 if (err)
858 return err;
860 if (lseek(outfd, SEEK_SET, 0) == -1)
861 err = got_error_from_errno("lseek");
863 return err;
866 static const struct got_error *
867 read_packed_blob_privsep(uint8_t **outbuf, size_t *size, size_t *hdrlen,
868 int outfd, struct got_pack *pack, struct got_packidx *packidx, int idx,
869 struct got_object_id *id)
871 const struct got_error *err = NULL;
873 if (pack->privsep_child == NULL) {
874 err = start_pack_privsep_child(pack, packidx);
875 if (err)
876 return err;
879 return request_packed_blob(outbuf, size, hdrlen, outfd, pack, packidx,
880 idx, id);
883 static const struct got_error *
884 request_blob(uint8_t **outbuf, size_t *size, size_t *hdrlen, int outfd,
885 int infd, struct imsgbuf *ibuf)
887 const struct got_error *err = NULL;
888 int outfd_child;
890 outfd_child = dup(outfd);
891 if (outfd_child == -1)
892 return got_error_from_errno("dup");
894 err = got_privsep_send_blob_req(ibuf, infd, NULL, -1);
895 if (err)
896 return err;
898 err = got_privsep_send_blob_outfd(ibuf, outfd_child);
899 if (err)
900 return err;
902 err = got_privsep_recv_blob(outbuf, size, hdrlen, ibuf);
903 if (err)
904 return err;
906 if (lseek(outfd, SEEK_SET, 0) == -1)
907 return got_error_from_errno("lseek");
909 return err;
912 static const struct got_error *
913 read_blob_privsep(uint8_t **outbuf, size_t *size, size_t *hdrlen,
914 int outfd, int infd, struct got_repository *repo)
916 int imsg_fds[2];
917 pid_t pid;
918 struct imsgbuf *ibuf;
920 if (repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_BLOB].imsg_fd != -1) {
921 ibuf = repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_BLOB].ibuf;
922 return request_blob(outbuf, size, hdrlen, outfd, infd, ibuf);
925 ibuf = calloc(1, sizeof(*ibuf));
926 if (ibuf == NULL)
927 return got_error_from_errno("calloc");
929 if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_fds) == -1)
930 return got_error_from_errno("socketpair");
932 pid = fork();
933 if (pid == -1)
934 return got_error_from_errno("fork");
935 else if (pid == 0) {
936 got_privsep_exec_child(imsg_fds, GOT_PATH_PROG_READ_BLOB,
937 repo->path);
938 /* not reached */
941 if (close(imsg_fds[1]) != 0)
942 return got_error_from_errno("close");
943 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_BLOB].imsg_fd =
944 imsg_fds[0];
945 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_BLOB].pid = pid;
946 imsg_init(ibuf, imsg_fds[0]);
947 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_BLOB].ibuf = ibuf;
949 return request_blob(outbuf, size, hdrlen, outfd, infd, ibuf);
952 static const struct got_error *
953 open_blob(struct got_blob_object **blob, struct got_repository *repo,
954 struct got_object_id *id, size_t blocksize)
956 const struct got_error *err = NULL;
957 struct got_packidx *packidx = NULL;
958 int idx;
959 char *path_packfile = NULL;
960 uint8_t *outbuf;
961 int outfd;
962 size_t size, hdrlen;
963 struct stat sb;
965 *blob = calloc(1, sizeof(**blob));
966 if (*blob == NULL)
967 return got_error_from_errno("calloc");
969 outfd = got_opentempfd();
970 if (outfd == -1)
971 return got_error_from_errno("got_opentempfd");
973 (*blob)->read_buf = malloc(blocksize);
974 if ((*blob)->read_buf == NULL) {
975 err = got_error_from_errno("malloc");
976 goto done;
979 err = got_repo_search_packidx(&packidx, &idx, repo, id);
980 if (err == NULL) {
981 struct got_pack *pack = NULL;
983 err = get_packfile_path(&path_packfile, packidx);
984 if (err)
985 goto done;
987 pack = got_repo_get_cached_pack(repo, path_packfile);
988 if (pack == NULL) {
989 err = got_repo_cache_pack(&pack, repo, path_packfile,
990 packidx);
991 if (err)
992 goto done;
994 err = read_packed_blob_privsep(&outbuf, &size, &hdrlen, outfd,
995 pack, packidx, idx, id);
996 } else if (err->code == GOT_ERR_NO_OBJ) {
997 int infd;
999 err = open_loose_object(&infd, id, repo);
1000 if (err)
1001 goto done;
1002 err = read_blob_privsep(&outbuf, &size, &hdrlen, outfd, infd,
1003 repo);
1005 if (err)
1006 goto done;
1008 if (hdrlen > size) {
1009 err = got_error(GOT_ERR_BAD_OBJ_HDR);
1010 goto done;
1013 if (outbuf) {
1014 if (close(outfd) != 0 && err == NULL)
1015 err = got_error_from_errno("close");
1016 outfd = -1;
1017 (*blob)->f = fmemopen(outbuf, size, "rb");
1018 if ((*blob)->f == NULL) {
1019 err = got_error_from_errno("fmemopen");
1020 free(outbuf);
1021 goto done;
1023 (*blob)->data = outbuf;
1024 } else {
1025 if (fstat(outfd, &sb) == -1) {
1026 err = got_error_from_errno("fstat");
1027 goto done;
1030 if (sb.st_size != size) {
1031 err = got_error(GOT_ERR_PRIVSEP_LEN);
1032 goto done;
1035 (*blob)->f = fdopen(outfd, "rb");
1036 if ((*blob)->f == NULL) {
1037 err = got_error_from_errno("fdopen");
1038 close(outfd);
1039 outfd = -1;
1040 goto done;
1044 (*blob)->hdrlen = hdrlen;
1045 (*blob)->blocksize = blocksize;
1046 memcpy(&(*blob)->id.sha1, id->sha1, SHA1_DIGEST_LENGTH);
1048 done:
1049 free(path_packfile);
1050 if (err) {
1051 if (*blob) {
1052 got_object_blob_close(*blob);
1053 *blob = NULL;
1054 } else if (outfd != -1)
1055 close(outfd);
1057 return err;
1060 const struct got_error *
1061 got_object_open_as_blob(struct got_blob_object **blob,
1062 struct got_repository *repo, struct got_object_id *id,
1063 size_t blocksize)
1065 return open_blob(blob, repo, id, blocksize);
1068 const struct got_error *
1069 got_object_blob_open(struct got_blob_object **blob,
1070 struct got_repository *repo, struct got_object *obj, size_t blocksize)
1072 return open_blob(blob, repo, got_object_get_id(obj), blocksize);
1075 const struct got_error *
1076 got_object_blob_close(struct got_blob_object *blob)
1078 const struct got_error *err = NULL;
1079 free(blob->read_buf);
1080 if (blob->f && fclose(blob->f) != 0)
1081 err = got_error_from_errno("fclose");
1082 free(blob->data);
1083 free(blob);
1084 return err;
1087 char *
1088 got_object_blob_id_str(struct got_blob_object *blob, char *buf, size_t size)
1090 return got_sha1_digest_to_str(blob->id.sha1, buf, size);
1093 size_t
1094 got_object_blob_get_hdrlen(struct got_blob_object *blob)
1096 return blob->hdrlen;
1099 const uint8_t *
1100 got_object_blob_get_read_buf(struct got_blob_object *blob)
1102 return blob->read_buf;
1105 const struct got_error *
1106 got_object_blob_read_block(size_t *outlenp, struct got_blob_object *blob)
1108 size_t n;
1110 n = fread(blob->read_buf, 1, blob->blocksize, blob->f);
1111 if (n == 0 && ferror(blob->f))
1112 return got_ferror(blob->f, GOT_ERR_IO);
1113 *outlenp = n;
1114 return NULL;
1117 const struct got_error *
1118 got_object_blob_dump_to_file(size_t *filesize, int *nlines,
1119 off_t **line_offsets, FILE *outfile, struct got_blob_object *blob)
1121 const struct got_error *err = NULL;
1122 size_t n, len, hdrlen;
1123 const uint8_t *buf;
1124 int i;
1125 size_t noffsets = 0;
1126 off_t off = 0, total_len = 0;
1128 if (line_offsets)
1129 *line_offsets = NULL;
1130 if (filesize)
1131 *filesize = 0;
1132 if (nlines)
1133 *nlines = 0;
1135 hdrlen = got_object_blob_get_hdrlen(blob);
1136 do {
1137 err = got_object_blob_read_block(&len, blob);
1138 if (err)
1139 return err;
1140 if (len == 0)
1141 break;
1142 buf = got_object_blob_get_read_buf(blob);
1143 i = hdrlen;
1144 if (line_offsets && nlines) {
1145 if (*line_offsets == NULL) {
1146 /* Have some data but perhaps no '\n'. */
1147 noffsets = 1;
1148 *nlines = 1;
1149 *line_offsets = calloc(1, sizeof(**line_offsets));
1150 if (*line_offsets == NULL)
1151 return got_error_from_errno("malloc");
1153 /* Skip forward over end of first line. */
1154 while (i < len) {
1155 if (buf[i] == '\n')
1156 break;
1157 i++;
1160 /* Scan '\n' offsets in remaining chunk of data. */
1161 while (i < len) {
1162 if (buf[i] != '\n') {
1163 i++;
1164 continue;
1166 (*nlines)++;
1167 if (noffsets < *nlines) {
1168 off_t *o = recallocarray(*line_offsets,
1169 noffsets, *nlines,
1170 sizeof(**line_offsets));
1171 if (o == NULL) {
1172 free(*line_offsets);
1173 *line_offsets = NULL;
1174 return got_error_from_errno(
1175 "recallocarray");
1177 *line_offsets = o;
1178 noffsets = *nlines;
1180 off = total_len + i - hdrlen + 1;
1181 (*line_offsets)[*nlines - 1] = off;
1182 i++;
1185 /* Skip blob object header first time around. */
1186 n = fwrite(buf + hdrlen, 1, len - hdrlen, outfile);
1187 if (n != len - hdrlen)
1188 return got_ferror(outfile, GOT_ERR_IO);
1189 total_len += len - hdrlen;
1190 hdrlen = 0;
1191 } while (len != 0);
1193 if (fflush(outfile) != 0)
1194 return got_error_from_errno("fflush");
1195 rewind(outfile);
1197 if (filesize)
1198 *filesize = total_len;
1200 return NULL;
1203 static const struct got_error *
1204 request_packed_tag(struct got_tag_object **tag, struct got_pack *pack,
1205 int pack_idx, struct got_object_id *id)
1207 const struct got_error *err = NULL;
1209 err = got_privsep_send_tag_req(pack->privsep_child->ibuf, -1, id,
1210 pack_idx);
1211 if (err)
1212 return err;
1214 return got_privsep_recv_tag(tag, pack->privsep_child->ibuf);
1217 static const struct got_error *
1218 read_packed_tag_privsep(struct got_tag_object **tag,
1219 struct got_pack *pack, struct got_packidx *packidx, int idx,
1220 struct got_object_id *id)
1222 const struct got_error *err = NULL;
1224 if (pack->privsep_child)
1225 return request_packed_tag(tag, pack, idx, id);
1227 err = start_pack_privsep_child(pack, packidx);
1228 if (err)
1229 return err;
1231 return request_packed_tag(tag, pack, idx, id);
1234 static const struct got_error *
1235 request_tag(struct got_tag_object **tag, struct got_repository *repo,
1236 int fd)
1238 const struct got_error *err = NULL;
1239 struct imsgbuf *ibuf;
1241 ibuf = repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TAG].ibuf;
1243 err = got_privsep_send_tag_req(ibuf, fd, NULL, -1);
1244 if (err)
1245 return err;
1247 return got_privsep_recv_tag(tag, ibuf);
1250 static const struct got_error *
1251 read_tag_privsep(struct got_tag_object **tag, int obj_fd,
1252 struct got_repository *repo)
1254 int imsg_fds[2];
1255 pid_t pid;
1256 struct imsgbuf *ibuf;
1258 if (repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TAG].imsg_fd != -1)
1259 return request_tag(tag, repo, obj_fd);
1261 ibuf = calloc(1, sizeof(*ibuf));
1262 if (ibuf == NULL)
1263 return got_error_from_errno("calloc");
1265 if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_fds) == -1)
1266 return got_error_from_errno("socketpair");
1268 pid = fork();
1269 if (pid == -1)
1270 return got_error_from_errno("fork");
1271 else if (pid == 0) {
1272 got_privsep_exec_child(imsg_fds, GOT_PATH_PROG_READ_TAG,
1273 repo->path);
1274 /* not reached */
1277 if (close(imsg_fds[1]) != 0)
1278 return got_error_from_errno("close");
1279 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TAG].imsg_fd =
1280 imsg_fds[0];
1281 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TAG].pid = pid;
1282 imsg_init(ibuf, imsg_fds[0]);
1283 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TAG].ibuf = ibuf;
1285 return request_tag(tag, repo, obj_fd);
1288 static const struct got_error *
1289 open_tag(struct got_tag_object **tag, struct got_repository *repo,
1290 struct got_object_id *id, int check_cache)
1292 const struct got_error *err = NULL;
1293 struct got_packidx *packidx = NULL;
1294 int idx;
1295 char *path_packfile = NULL;
1296 struct got_object *obj = NULL;
1297 int obj_type = GOT_OBJ_TYPE_ANY;
1299 if (check_cache) {
1300 *tag = got_repo_get_cached_tag(repo, id);
1301 if (*tag != NULL) {
1302 (*tag)->refcnt++;
1303 return NULL;
1305 } else
1306 *tag = NULL;
1308 err = got_repo_search_packidx(&packidx, &idx, repo, id);
1309 if (err == NULL) {
1310 struct got_pack *pack = NULL;
1312 err = get_packfile_path(&path_packfile, packidx);
1313 if (err)
1314 return err;
1316 pack = got_repo_get_cached_pack(repo, path_packfile);
1317 if (pack == NULL) {
1318 err = got_repo_cache_pack(&pack, repo, path_packfile,
1319 packidx);
1320 if (err)
1321 goto done;
1324 /* Beware of "leightweight" tags: Check object type first. */
1325 err = read_packed_object_privsep(&obj, repo, pack, packidx,
1326 idx, id);
1327 if (err)
1328 goto done;
1329 obj_type = obj->type;
1330 got_object_close(obj);
1331 if (obj_type != GOT_OBJ_TYPE_TAG) {
1332 err = got_error(GOT_ERR_OBJ_TYPE);
1333 goto done;
1335 err = read_packed_tag_privsep(tag, pack, packidx, idx, id);
1336 } else if (err->code == GOT_ERR_NO_OBJ) {
1337 int fd;
1339 err = open_loose_object(&fd, id, repo);
1340 if (err)
1341 return err;
1342 err = read_object_header_privsep(&obj, repo, fd);
1343 if (err)
1344 return err;
1345 obj_type = obj->type;
1346 got_object_close(obj);
1347 if (obj_type != GOT_OBJ_TYPE_TAG)
1348 return got_error(GOT_ERR_OBJ_TYPE);
1350 err = open_loose_object(&fd, id, repo);
1351 if (err)
1352 return err;
1353 err = read_tag_privsep(tag, fd, repo);
1356 if (err == NULL) {
1357 (*tag)->refcnt++;
1358 err = got_repo_cache_tag(repo, id, *tag);
1360 done:
1361 free(path_packfile);
1362 return err;
1365 const struct got_error *
1366 got_object_open_as_tag(struct got_tag_object **tag,
1367 struct got_repository *repo, struct got_object_id *id)
1369 *tag = got_repo_get_cached_tag(repo, id);
1370 if (*tag != NULL) {
1371 (*tag)->refcnt++;
1372 return NULL;
1375 return open_tag(tag, repo, id, 0);
1378 const struct got_error *
1379 got_object_tag_open(struct got_tag_object **tag,
1380 struct got_repository *repo, struct got_object *obj)
1382 return open_tag(tag, repo, got_object_get_id(obj), 1);
1385 const char *
1386 got_object_tag_get_name(struct got_tag_object *tag)
1388 return tag->tag;
1391 int
1392 got_object_tag_get_object_type(struct got_tag_object *tag)
1394 return tag->obj_type;
1397 struct got_object_id *
1398 got_object_tag_get_object_id(struct got_tag_object *tag)
1400 return &tag->id;
1403 time_t
1404 got_object_tag_get_tagger_time(struct got_tag_object *tag)
1406 return tag->tagger_time;
1409 time_t
1410 got_object_tag_get_tagger_gmtoff(struct got_tag_object *tag)
1412 return tag->tagger_gmtoff;
1415 const char *
1416 got_object_tag_get_tagger(struct got_tag_object *tag)
1418 return tag->tagger;
1421 const char *
1422 got_object_tag_get_message(struct got_tag_object *tag)
1424 return tag->tagmsg;
1427 static struct got_tree_entry *
1428 find_entry_by_name(struct got_tree_object *tree, const char *name, size_t len)
1430 struct got_tree_entry *te;
1432 /* Note that tree entries are sorted in strncmp() order. */
1433 SIMPLEQ_FOREACH(te, &tree->entries.head, entry) {
1434 int cmp = strncmp(te->name, name, len);
1435 if (cmp < 0)
1436 continue;
1437 if (cmp > 0)
1438 break;
1439 if (te->name[len] == '\0')
1440 return te;
1442 return NULL;
1445 const struct got_tree_entry *
1446 got_object_tree_find_entry(struct got_tree_object *tree, const char *name)
1448 return find_entry_by_name(tree, name, strlen(name));
1451 const struct got_error *
1452 got_object_id_by_path(struct got_object_id **id, struct got_repository *repo,
1453 struct got_object_id *commit_id, const char *path)
1455 const struct got_error *err = NULL;
1456 struct got_commit_object *commit = NULL;
1457 struct got_tree_object *tree = NULL;
1458 struct got_tree_entry *te = NULL;
1459 const char *seg, *s;
1460 size_t seglen;
1462 *id = NULL;
1464 err = got_object_open_as_commit(&commit, repo, commit_id);
1465 if (err)
1466 goto done;
1468 /* Handle opening of root of commit's tree. */
1469 if (got_path_is_root_dir(path)) {
1470 *id = got_object_id_dup(commit->tree_id);
1471 if (*id == NULL)
1472 err = got_error_from_errno("got_object_id_dup");
1473 goto done;
1476 err = got_object_open_as_tree(&tree, repo, commit->tree_id);
1477 if (err)
1478 goto done;
1480 s = path;
1481 while (s[0] == '/')
1482 s++;
1483 seg = s;
1484 seglen = 0;
1485 while (*s) {
1486 struct got_tree_object *next_tree;
1488 if (*s != '/') {
1489 s++;
1490 seglen++;
1491 if (*s)
1492 continue;
1495 te = find_entry_by_name(tree, seg, seglen);
1496 if (te == NULL) {
1497 err = got_error(GOT_ERR_NO_TREE_ENTRY);
1498 goto done;
1501 if (*s == '\0')
1502 break;
1504 seg = s + 1;
1505 seglen = 0;
1506 s++;
1507 if (*s) {
1508 err = got_object_open_as_tree(&next_tree, repo,
1509 te->id);
1510 te = NULL;
1511 if (err)
1512 goto done;
1513 got_object_tree_close(tree);
1514 tree = next_tree;
1518 if (te) {
1519 *id = got_object_id_dup(te->id);
1520 if (*id == NULL)
1521 return got_error_from_errno("got_object_id_dup");
1522 } else
1523 err = got_error(GOT_ERR_NO_TREE_ENTRY);
1524 done:
1525 if (commit)
1526 got_object_commit_close(commit);
1527 if (tree)
1528 got_object_tree_close(tree);
1529 return err;
1532 const struct got_error *
1533 got_object_tree_path_changed(int *changed,
1534 struct got_tree_object *tree01, struct got_tree_object *tree02,
1535 const char *path, struct got_repository *repo)
1537 const struct got_error *err = NULL;
1538 struct got_tree_object *tree1 = NULL, *tree2 = NULL;
1539 struct got_tree_entry *te1 = NULL, *te2 = NULL;
1540 const char *seg, *s;
1541 size_t seglen;
1543 *changed = 0;
1545 /* We are expecting an absolute in-repository path. */
1546 if (path[0] != '/')
1547 return got_error(GOT_ERR_NOT_ABSPATH);
1549 /* We not do support comparing the root path. */
1550 if (path[1] == '\0')
1551 return got_error(GOT_ERR_BAD_PATH);
1553 tree1 = tree01;
1554 tree2 = tree02;
1555 s = path;
1556 s++; /* skip leading '/' */
1557 seg = s;
1558 seglen = 0;
1559 while (*s) {
1560 struct got_tree_object *next_tree1, *next_tree2;
1562 if (*s != '/') {
1563 s++;
1564 seglen++;
1565 if (*s)
1566 continue;
1569 te1 = find_entry_by_name(tree1, seg, seglen);
1570 if (te1 == NULL) {
1571 err = got_error(GOT_ERR_NO_OBJ);
1572 goto done;
1575 te2 = find_entry_by_name(tree2, seg, seglen);
1576 if (te2 == NULL) {
1577 *changed = 1;
1578 goto done;
1581 if (te1->mode != te2->mode) {
1582 *changed = 1;
1583 goto done;
1586 if (got_object_id_cmp(te1->id, te2->id) == 0) {
1587 *changed = 0;
1588 goto done;
1591 if (*s == '\0') { /* final path element */
1592 *changed = 1;
1593 goto done;
1596 seg = s + 1;
1597 s++;
1598 seglen = 0;
1599 if (*s) {
1600 err = got_object_open_as_tree(&next_tree1, repo,
1601 te1->id);
1602 te1 = NULL;
1603 if (err)
1604 goto done;
1605 if (tree1 != tree01)
1606 got_object_tree_close(tree1);
1607 tree1 = next_tree1;
1609 err = got_object_open_as_tree(&next_tree2, repo,
1610 te2->id);
1611 te2 = NULL;
1612 if (err)
1613 goto done;
1614 if (tree2 != tree02)
1615 got_object_tree_close(tree2);
1616 tree2 = next_tree2;
1619 done:
1620 if (tree1 && tree1 != tree01)
1621 got_object_tree_close(tree1);
1622 if (tree2 && tree2 != tree02)
1623 got_object_tree_close(tree2);
1624 return err;
1627 const struct got_error *
1628 got_object_tree_entry_dup(struct got_tree_entry **new_te,
1629 struct got_tree_entry *te)
1631 const struct got_error *err = NULL;
1633 *new_te = calloc(1, sizeof(**new_te));
1634 if (*new_te == NULL)
1635 return got_error_from_errno("calloc");
1637 (*new_te)->mode = te->mode;
1638 (*new_te)->name = strdup(te->name);
1639 if ((*new_te)->name == NULL) {
1640 err = got_error_from_errno("strdup");
1641 goto done;
1644 (*new_te)->id = got_object_id_dup(te->id);
1645 if ((*new_te)->id == NULL) {
1646 err = got_error_from_errno("got_object_id_dup");
1647 goto done;
1649 done:
1650 if (err) {
1651 got_object_tree_entry_close(*new_te);
1652 *new_te = NULL;
1654 return err;
1657 int
1658 got_object_tree_entry_is_submodule(const struct got_tree_entry *te)
1660 return (te->mode & S_IFMT) == (S_IFDIR | S_IFLNK);