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 memcpy(&(*obj)->id, id, sizeof((*obj)->id));
201 return NULL;
204 static void
205 set_max_datasize(void)
207 struct rlimit rl;
209 if (getrlimit(RLIMIT_DATA, &rl) != 0)
210 return;
212 rl.rlim_cur = rl.rlim_max;
213 setrlimit(RLIMIT_DATA, &rl);
216 static const struct got_error *
217 start_pack_privsep_child(struct got_pack *pack, struct got_packidx *packidx)
219 const struct got_error *err = NULL;
220 int imsg_fds[2];
221 pid_t pid;
222 struct imsgbuf *ibuf;
224 ibuf = calloc(1, sizeof(*ibuf));
225 if (ibuf == NULL)
226 return got_error_from_errno("calloc");
228 pack->privsep_child = calloc(1, sizeof(*pack->privsep_child));
229 if (pack->privsep_child == NULL) {
230 err = got_error_from_errno("calloc");
231 free(ibuf);
232 return err;
235 if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_fds) == -1) {
236 err = got_error_from_errno("socketpair");
237 goto done;
240 pid = fork();
241 if (pid == -1) {
242 err = got_error_from_errno("fork");
243 goto done;
244 } else if (pid == 0) {
245 set_max_datasize();
246 got_privsep_exec_child(imsg_fds, GOT_PATH_PROG_READ_PACK,
247 pack->path_packfile);
248 /* not reached */
251 if (close(imsg_fds[1]) != 0)
252 return got_error_from_errno("close");
253 pack->privsep_child->imsg_fd = imsg_fds[0];
254 pack->privsep_child->pid = pid;
255 imsg_init(ibuf, imsg_fds[0]);
256 pack->privsep_child->ibuf = ibuf;
258 err = got_privsep_init_pack_child(ibuf, pack, packidx);
259 if (err) {
260 const struct got_error *child_err;
261 err = got_privsep_send_stop(pack->privsep_child->imsg_fd);
262 child_err = got_privsep_wait_for_child(
263 pack->privsep_child->pid);
264 if (child_err && err == NULL)
265 err = child_err;
267 done:
268 if (err) {
269 free(ibuf);
270 free(pack->privsep_child);
271 pack->privsep_child = NULL;
273 return err;
276 static const struct got_error *
277 read_packed_object_privsep(struct got_object **obj,
278 struct got_repository *repo, struct got_pack *pack,
279 struct got_packidx *packidx, int idx, struct got_object_id *id)
281 const struct got_error *err = NULL;
283 if (pack->privsep_child)
284 return request_packed_object(obj, pack, idx, id);
286 err = start_pack_privsep_child(pack, packidx);
287 if (err)
288 return err;
290 return request_packed_object(obj, pack, idx, id);
294 static const struct got_error *
295 open_packed_object(struct got_object **obj, struct got_object_id *id,
296 struct got_repository *repo)
298 const struct got_error *err = NULL;
299 struct got_pack *pack = NULL;
300 struct got_packidx *packidx = NULL;
301 int idx;
302 char *path_packfile;
304 err = got_repo_search_packidx(&packidx, &idx, repo, id);
305 if (err)
306 return err;
308 err = get_packfile_path(&path_packfile, packidx);
309 if (err)
310 return err;
312 pack = got_repo_get_cached_pack(repo, path_packfile);
313 if (pack == NULL) {
314 err = got_repo_cache_pack(&pack, repo, path_packfile, packidx);
315 if (err)
316 goto done;
319 err = read_packed_object_privsep(obj, repo, pack, packidx, idx, id);
320 if (err)
321 goto done;
323 err = got_repo_cache_pack(NULL, repo, path_packfile, packidx);
324 done:
325 free(path_packfile);
326 return err;
329 static const struct got_error *
330 request_object(struct got_object **obj, struct got_repository *repo, int fd)
332 const struct got_error *err = NULL;
333 struct imsgbuf *ibuf;
335 ibuf = repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_OBJECT].ibuf;
337 err = got_privsep_send_obj_req(ibuf, fd);
338 if (err)
339 return err;
341 return got_privsep_recv_obj(obj, ibuf);
344 static const struct got_error *
345 read_object_header_privsep(struct got_object **obj, struct got_repository *repo,
346 int obj_fd)
348 const struct got_error *err;
349 int imsg_fds[2];
350 pid_t pid;
351 struct imsgbuf *ibuf;
353 if (repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_OBJECT].imsg_fd != -1)
354 return request_object(obj, repo, obj_fd);
356 ibuf = calloc(1, sizeof(*ibuf));
357 if (ibuf == NULL)
358 return got_error_from_errno("calloc");
360 if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_fds) == -1) {
361 err = got_error_from_errno("socketpair");
362 free(ibuf);
363 return err;
366 pid = fork();
367 if (pid == -1) {
368 err = got_error_from_errno("fork");
369 free(ibuf);
370 return err;
372 else if (pid == 0) {
373 got_privsep_exec_child(imsg_fds, GOT_PATH_PROG_READ_OBJECT,
374 repo->path);
375 /* not reached */
378 if (close(imsg_fds[1]) != 0) {
379 err = got_error_from_errno("close");
380 free(ibuf);
381 return err;
383 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_OBJECT].imsg_fd =
384 imsg_fds[0];
385 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_OBJECT].pid = pid;
386 imsg_init(ibuf, imsg_fds[0]);
387 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_OBJECT].ibuf = ibuf;
389 return request_object(obj, repo, obj_fd);
393 const struct got_error *
394 got_object_open(struct got_object **obj, struct got_repository *repo,
395 struct got_object_id *id)
397 const struct got_error *err = NULL;
398 char *path;
399 int fd;
401 *obj = got_repo_get_cached_object(repo, id);
402 if (*obj != NULL) {
403 (*obj)->refcnt++;
404 return NULL;
407 err = open_packed_object(obj, id, repo);
408 if (err && err->code != GOT_ERR_NO_OBJ)
409 return err;
410 if (*obj) {
411 (*obj)->refcnt++;
412 return got_repo_cache_object(repo, id, *obj);
415 err = got_object_get_path(&path, id, repo);
416 if (err)
417 return err;
419 fd = open(path, O_RDONLY | O_NOFOLLOW);
420 if (fd == -1) {
421 if (errno == ENOENT)
422 err = got_error_no_obj(id);
423 else
424 err = got_error_from_errno2("open", path);
425 goto done;
426 } else {
427 err = read_object_header_privsep(obj, repo, fd);
428 if (err)
429 goto done;
430 memcpy((*obj)->id.sha1, id->sha1, SHA1_DIGEST_LENGTH);
433 (*obj)->refcnt++;
434 err = got_repo_cache_object(repo, id, *obj);
435 done:
436 free(path);
437 return err;
441 const struct got_error *
442 got_object_open_by_id_str(struct got_object **obj, struct got_repository *repo,
443 const char *id_str)
445 struct got_object_id id;
447 if (!got_parse_sha1_digest(id.sha1, id_str))
448 return got_error_path(id_str, GOT_ERR_BAD_OBJ_ID_STR);
450 return got_object_open(obj, repo, &id);
453 const struct got_error *
454 got_object_resolve_id_str(struct got_object_id **id,
455 struct got_repository *repo, const char *id_str)
457 const struct got_error *err = NULL;
458 struct got_object *obj;
460 err = got_object_open_by_id_str(&obj, repo, id_str);
461 if (err)
462 return err;
464 *id = got_object_id_dup(got_object_get_id(obj));
465 got_object_close(obj);
466 if (*id == NULL)
467 return got_error_from_errno("got_object_id_dup");
469 return NULL;
472 static const struct got_error *
473 request_packed_commit(struct got_commit_object **commit, struct got_pack *pack,
474 int pack_idx, struct got_object_id *id)
476 const struct got_error *err = NULL;
478 err = got_privsep_send_commit_req(pack->privsep_child->ibuf, -1, id,
479 pack_idx);
480 if (err)
481 return err;
483 return got_privsep_recv_commit(commit, pack->privsep_child->ibuf);
486 static const struct got_error *
487 read_packed_commit_privsep(struct got_commit_object **commit,
488 struct got_pack *pack, struct got_packidx *packidx, int idx,
489 struct got_object_id *id)
491 const struct got_error *err = NULL;
493 if (pack->privsep_child)
494 return request_packed_commit(commit, pack, idx, id);
496 err = start_pack_privsep_child(pack, packidx);
497 if (err)
498 return err;
500 return request_packed_commit(commit, pack, idx, id);
503 static const struct got_error *
504 request_commit(struct got_commit_object **commit, struct got_repository *repo,
505 int fd)
507 const struct got_error *err = NULL;
508 struct imsgbuf *ibuf;
510 ibuf = repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_COMMIT].ibuf;
512 err = got_privsep_send_commit_req(ibuf, fd, NULL, -1);
513 if (err)
514 return err;
516 return got_privsep_recv_commit(commit, ibuf);
519 static const struct got_error *
520 read_commit_privsep(struct got_commit_object **commit, int obj_fd,
521 struct got_repository *repo)
523 const struct got_error *err;
524 int imsg_fds[2];
525 pid_t pid;
526 struct imsgbuf *ibuf;
528 if (repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_COMMIT].imsg_fd != -1)
529 return request_commit(commit, repo, obj_fd);
531 ibuf = calloc(1, sizeof(*ibuf));
532 if (ibuf == NULL)
533 return got_error_from_errno("calloc");
535 if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_fds) == -1) {
536 err = got_error_from_errno("socketpair");
537 free(ibuf);
538 return err;
541 pid = fork();
542 if (pid == -1) {
543 err = got_error_from_errno("fork");
544 free(ibuf);
545 return err;
547 else if (pid == 0) {
548 got_privsep_exec_child(imsg_fds, GOT_PATH_PROG_READ_COMMIT,
549 repo->path);
550 /* not reached */
553 if (close(imsg_fds[1]) != 0) {
554 err = got_error_from_errno("close");
555 free(ibuf);
556 return err;
558 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_COMMIT].imsg_fd =
559 imsg_fds[0];
560 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_COMMIT].pid = pid;
561 imsg_init(ibuf, imsg_fds[0]);
562 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_COMMIT].ibuf = ibuf;
564 return request_commit(commit, repo, obj_fd);
568 static const struct got_error *
569 open_commit(struct got_commit_object **commit,
570 struct got_repository *repo, struct got_object_id *id, int check_cache)
572 const struct got_error *err = NULL;
573 struct got_packidx *packidx = NULL;
574 int idx;
575 char *path_packfile = NULL;
577 if (check_cache) {
578 *commit = got_repo_get_cached_commit(repo, id);
579 if (*commit != NULL) {
580 (*commit)->refcnt++;
581 return NULL;
583 } else
584 *commit = NULL;
586 err = got_repo_search_packidx(&packidx, &idx, repo, id);
587 if (err == NULL) {
588 struct got_pack *pack = NULL;
590 err = get_packfile_path(&path_packfile, packidx);
591 if (err)
592 return err;
594 pack = got_repo_get_cached_pack(repo, path_packfile);
595 if (pack == NULL) {
596 err = got_repo_cache_pack(&pack, repo, path_packfile,
597 packidx);
598 if (err)
599 goto done;
601 err = read_packed_commit_privsep(commit, pack,
602 packidx, idx, id);
603 } else if (err->code == GOT_ERR_NO_OBJ) {
604 int fd;
606 err = open_loose_object(&fd, id, repo);
607 if (err)
608 return err;
609 err = read_commit_privsep(commit, fd, repo);
612 if (err == NULL) {
613 (*commit)->refcnt++;
614 err = got_repo_cache_commit(repo, id, *commit);
616 done:
617 free(path_packfile);
618 return err;
621 const struct got_error *
622 got_object_open_as_commit(struct got_commit_object **commit,
623 struct got_repository *repo, struct got_object_id *id)
625 *commit = got_repo_get_cached_commit(repo, id);
626 if (*commit != NULL) {
627 (*commit)->refcnt++;
628 return NULL;
631 return open_commit(commit, repo, id, 0);
634 const struct got_error *
635 got_object_commit_open(struct got_commit_object **commit,
636 struct got_repository *repo, struct got_object *obj)
638 return open_commit(commit, repo, got_object_get_id(obj), 1);
641 const struct got_error *
642 got_object_qid_alloc(struct got_object_qid **qid, struct got_object_id *id)
644 const struct got_error *err = NULL;
646 *qid = calloc(1, sizeof(**qid));
647 if (*qid == NULL)
648 return got_error_from_errno("calloc");
650 (*qid)->id = got_object_id_dup(id);
651 if ((*qid)->id == NULL) {
652 err = got_error_from_errno("got_object_id_dup");
653 got_object_qid_free(*qid);
654 *qid = NULL;
655 return err;
658 return NULL;
661 static const struct got_error *
662 request_packed_tree(struct got_tree_object **tree, struct got_pack *pack,
663 int pack_idx, struct got_object_id *id)
665 const struct got_error *err = NULL;
667 err = got_privsep_send_tree_req(pack->privsep_child->ibuf, -1, id,
668 pack_idx);
669 if (err)
670 return err;
672 return got_privsep_recv_tree(tree, pack->privsep_child->ibuf);
675 static const struct got_error *
676 read_packed_tree_privsep(struct got_tree_object **tree,
677 struct got_pack *pack, struct got_packidx *packidx, int idx,
678 struct got_object_id *id)
680 const struct got_error *err = NULL;
682 if (pack->privsep_child)
683 return request_packed_tree(tree, pack, idx, id);
685 err = start_pack_privsep_child(pack, packidx);
686 if (err)
687 return err;
689 return request_packed_tree(tree, pack, idx, id);
692 static const struct got_error *
693 request_tree(struct got_tree_object **tree, struct got_repository *repo,
694 int fd)
696 const struct got_error *err = NULL;
697 struct imsgbuf *ibuf;
699 ibuf = repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TREE].ibuf;
701 err = got_privsep_send_tree_req(ibuf, fd, NULL, -1);
702 if (err)
703 return err;
705 return got_privsep_recv_tree(tree, ibuf);
708 const struct got_error *
709 read_tree_privsep(struct got_tree_object **tree, int obj_fd,
710 struct got_repository *repo)
712 const struct got_error *err;
713 int imsg_fds[2];
714 pid_t pid;
715 struct imsgbuf *ibuf;
717 if (repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TREE].imsg_fd != -1)
718 return request_tree(tree, repo, obj_fd);
720 ibuf = calloc(1, sizeof(*ibuf));
721 if (ibuf == NULL)
722 return got_error_from_errno("calloc");
724 if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_fds) == -1) {
725 err = got_error_from_errno("socketpair");
726 free(ibuf);
727 return err;
730 pid = fork();
731 if (pid == -1) {
732 err = got_error_from_errno("fork");
733 free(ibuf);
734 return err;
736 else if (pid == 0) {
737 got_privsep_exec_child(imsg_fds, GOT_PATH_PROG_READ_TREE,
738 repo->path);
739 /* not reached */
742 if (close(imsg_fds[1]) != 0) {
743 err = got_error_from_errno("close");
744 free(ibuf);
745 return err;
747 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TREE].imsg_fd =
748 imsg_fds[0];
749 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TREE].pid = pid;
750 imsg_init(ibuf, imsg_fds[0]);
751 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TREE].ibuf = ibuf;
754 return request_tree(tree, repo, obj_fd);
757 static const struct got_error *
758 open_tree(struct got_tree_object **tree, struct got_repository *repo,
759 struct got_object_id *id, int check_cache)
761 const struct got_error *err = NULL;
762 struct got_packidx *packidx = NULL;
763 int idx;
764 char *path_packfile = NULL;
766 if (check_cache) {
767 *tree = got_repo_get_cached_tree(repo, id);
768 if (*tree != NULL) {
769 (*tree)->refcnt++;
770 return NULL;
772 } else
773 *tree = NULL;
775 err = got_repo_search_packidx(&packidx, &idx, repo, id);
776 if (err == NULL) {
777 struct got_pack *pack = NULL;
779 err = get_packfile_path(&path_packfile, packidx);
780 if (err)
781 return err;
783 pack = got_repo_get_cached_pack(repo, path_packfile);
784 if (pack == NULL) {
785 err = got_repo_cache_pack(&pack, repo, path_packfile,
786 packidx);
787 if (err)
788 goto done;
790 err = read_packed_tree_privsep(tree, pack,
791 packidx, idx, id);
792 } else if (err->code == GOT_ERR_NO_OBJ) {
793 int fd;
795 err = open_loose_object(&fd, id, repo);
796 if (err)
797 return err;
798 err = read_tree_privsep(tree, fd, repo);
801 if (err == NULL) {
802 (*tree)->refcnt++;
803 err = got_repo_cache_tree(repo, id, *tree);
805 done:
806 free(path_packfile);
807 return err;
810 const struct got_error *
811 got_object_open_as_tree(struct got_tree_object **tree,
812 struct got_repository *repo, struct got_object_id *id)
814 *tree = got_repo_get_cached_tree(repo, id);
815 if (*tree != NULL) {
816 (*tree)->refcnt++;
817 return NULL;
820 return open_tree(tree, repo, id, 0);
823 const struct got_error *
824 got_object_tree_open(struct got_tree_object **tree,
825 struct got_repository *repo, struct got_object *obj)
827 return open_tree(tree, repo, got_object_get_id(obj), 1);
830 int
831 got_object_tree_get_nentries(struct got_tree_object *tree)
833 return tree->nentries;
836 struct got_tree_entry *
837 got_object_tree_get_first_entry(struct got_tree_object *tree)
839 return got_object_tree_get_entry(tree, 0);
842 struct got_tree_entry *
843 got_object_tree_get_last_entry(struct got_tree_object *tree)
845 return got_object_tree_get_entry(tree, tree->nentries - 1);
848 struct got_tree_entry *
849 got_object_tree_get_entry(struct got_tree_object *tree, int i)
851 if (i < 0 || i >= tree->nentries)
852 return NULL;
853 return &tree->entries[i];
856 mode_t
857 got_tree_entry_get_mode(struct got_tree_entry *te)
859 return te->mode;
862 const char *
863 got_tree_entry_get_name(struct got_tree_entry *te)
865 return &te->name[0];
868 struct got_object_id *
869 got_tree_entry_get_id(struct got_tree_entry *te)
871 return &te->id;
874 int
875 got_tree_entry_get_index(struct got_tree_entry *te)
877 return te->idx;
880 struct got_tree_entry *
881 got_tree_entry_get_next(struct got_tree_object *tree,
882 struct got_tree_entry *te)
884 return got_object_tree_get_entry(tree, te->idx + 1);
887 struct got_tree_entry *
888 got_tree_entry_get_prev(struct got_tree_object *tree,
889 struct got_tree_entry *te)
891 return got_object_tree_get_entry(tree, te->idx - 1);
894 static const struct got_error *
895 request_packed_blob(uint8_t **outbuf, size_t *size, size_t *hdrlen, int outfd,
896 struct got_pack *pack, struct got_packidx *packidx, int idx,
897 struct got_object_id *id)
899 const struct got_error *err = NULL;
900 int outfd_child;
901 int basefd, accumfd; /* temporary files for delta application */
903 basefd = got_opentempfd();
904 if (basefd == -1)
905 return got_error_from_errno("got_opentempfd");
906 accumfd = got_opentempfd();
907 if (accumfd == -1)
908 return got_error_from_errno("got_opentempfd");
910 outfd_child = dup(outfd);
911 if (outfd_child == -1)
912 return got_error_from_errno("dup");
914 err = got_privsep_send_blob_req(pack->privsep_child->ibuf, -1, id, idx);
915 if (err)
916 return err;
918 err = got_privsep_send_blob_outfd(pack->privsep_child->ibuf,
919 outfd_child);
920 if (err) {
921 close(basefd);
922 close(accumfd);
923 return err;
926 err = got_privsep_send_tmpfd(pack->privsep_child->ibuf,
927 basefd);
928 if (err) {
929 close(accumfd);
930 return err;
933 err = got_privsep_send_tmpfd(pack->privsep_child->ibuf,
934 accumfd);
935 if (err)
936 return err;
938 err = got_privsep_recv_blob(outbuf, size, hdrlen,
939 pack->privsep_child->ibuf);
940 if (err)
941 return err;
943 if (lseek(outfd, SEEK_SET, 0) == -1)
944 err = got_error_from_errno("lseek");
946 return err;
949 static const struct got_error *
950 read_packed_blob_privsep(uint8_t **outbuf, size_t *size, size_t *hdrlen,
951 int outfd, struct got_pack *pack, struct got_packidx *packidx, int idx,
952 struct got_object_id *id)
954 const struct got_error *err = NULL;
956 if (pack->privsep_child == NULL) {
957 err = start_pack_privsep_child(pack, packidx);
958 if (err)
959 return err;
962 return request_packed_blob(outbuf, size, hdrlen, outfd, pack, packidx,
963 idx, id);
966 static const struct got_error *
967 request_blob(uint8_t **outbuf, size_t *size, size_t *hdrlen, int outfd,
968 int infd, struct imsgbuf *ibuf)
970 const struct got_error *err = NULL;
971 int outfd_child;
973 outfd_child = dup(outfd);
974 if (outfd_child == -1)
975 return got_error_from_errno("dup");
977 err = got_privsep_send_blob_req(ibuf, infd, NULL, -1);
978 if (err)
979 return err;
981 err = got_privsep_send_blob_outfd(ibuf, outfd_child);
982 if (err)
983 return err;
985 err = got_privsep_recv_blob(outbuf, size, hdrlen, ibuf);
986 if (err)
987 return err;
989 if (lseek(outfd, SEEK_SET, 0) == -1)
990 return got_error_from_errno("lseek");
992 return err;
995 static const struct got_error *
996 read_blob_privsep(uint8_t **outbuf, size_t *size, size_t *hdrlen,
997 int outfd, int infd, struct got_repository *repo)
999 const struct got_error *err;
1000 int imsg_fds[2];
1001 pid_t pid;
1002 struct imsgbuf *ibuf;
1004 if (repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_BLOB].imsg_fd != -1) {
1005 ibuf = repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_BLOB].ibuf;
1006 return request_blob(outbuf, size, hdrlen, outfd, infd, ibuf);
1009 ibuf = calloc(1, sizeof(*ibuf));
1010 if (ibuf == NULL)
1011 return got_error_from_errno("calloc");
1013 if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_fds) == -1) {
1014 err = got_error_from_errno("socketpair");
1015 free(ibuf);
1016 return err;
1019 pid = fork();
1020 if (pid == -1) {
1021 err = got_error_from_errno("fork");
1022 free(ibuf);
1023 return err;
1025 else if (pid == 0) {
1026 got_privsep_exec_child(imsg_fds, GOT_PATH_PROG_READ_BLOB,
1027 repo->path);
1028 /* not reached */
1031 if (close(imsg_fds[1]) != 0) {
1032 err = got_error_from_errno("close");
1033 free(ibuf);
1034 return err;
1036 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_BLOB].imsg_fd =
1037 imsg_fds[0];
1038 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_BLOB].pid = pid;
1039 imsg_init(ibuf, imsg_fds[0]);
1040 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_BLOB].ibuf = ibuf;
1042 return request_blob(outbuf, size, hdrlen, outfd, infd, ibuf);
1045 static const struct got_error *
1046 open_blob(struct got_blob_object **blob, struct got_repository *repo,
1047 struct got_object_id *id, size_t blocksize)
1049 const struct got_error *err = NULL;
1050 struct got_packidx *packidx = NULL;
1051 int idx;
1052 char *path_packfile = NULL;
1053 uint8_t *outbuf;
1054 int outfd;
1055 size_t size, hdrlen;
1056 struct stat sb;
1058 *blob = calloc(1, sizeof(**blob));
1059 if (*blob == NULL)
1060 return got_error_from_errno("calloc");
1062 outfd = got_opentempfd();
1063 if (outfd == -1)
1064 return got_error_from_errno("got_opentempfd");
1066 (*blob)->read_buf = malloc(blocksize);
1067 if ((*blob)->read_buf == NULL) {
1068 err = got_error_from_errno("malloc");
1069 goto done;
1072 err = got_repo_search_packidx(&packidx, &idx, repo, id);
1073 if (err == NULL) {
1074 struct got_pack *pack = NULL;
1076 err = get_packfile_path(&path_packfile, packidx);
1077 if (err)
1078 goto done;
1080 pack = got_repo_get_cached_pack(repo, path_packfile);
1081 if (pack == NULL) {
1082 err = got_repo_cache_pack(&pack, repo, path_packfile,
1083 packidx);
1084 if (err)
1085 goto done;
1087 err = read_packed_blob_privsep(&outbuf, &size, &hdrlen, outfd,
1088 pack, packidx, idx, id);
1089 } else if (err->code == GOT_ERR_NO_OBJ) {
1090 int infd;
1092 err = open_loose_object(&infd, id, repo);
1093 if (err)
1094 goto done;
1095 err = read_blob_privsep(&outbuf, &size, &hdrlen, outfd, infd,
1096 repo);
1098 if (err)
1099 goto done;
1101 if (hdrlen > size) {
1102 err = got_error(GOT_ERR_BAD_OBJ_HDR);
1103 goto done;
1106 if (outbuf) {
1107 if (close(outfd) != 0 && err == NULL)
1108 err = got_error_from_errno("close");
1109 outfd = -1;
1110 (*blob)->f = fmemopen(outbuf, size, "rb");
1111 if ((*blob)->f == NULL) {
1112 err = got_error_from_errno("fmemopen");
1113 free(outbuf);
1114 goto done;
1116 (*blob)->data = outbuf;
1117 } else {
1118 if (fstat(outfd, &sb) == -1) {
1119 err = got_error_from_errno("fstat");
1120 goto done;
1123 if (sb.st_size != size) {
1124 err = got_error(GOT_ERR_PRIVSEP_LEN);
1125 goto done;
1128 (*blob)->f = fdopen(outfd, "rb");
1129 if ((*blob)->f == NULL) {
1130 err = got_error_from_errno("fdopen");
1131 close(outfd);
1132 outfd = -1;
1133 goto done;
1137 (*blob)->hdrlen = hdrlen;
1138 (*blob)->blocksize = blocksize;
1139 memcpy(&(*blob)->id.sha1, id->sha1, SHA1_DIGEST_LENGTH);
1141 done:
1142 free(path_packfile);
1143 if (err) {
1144 if (*blob) {
1145 got_object_blob_close(*blob);
1146 *blob = NULL;
1147 } else if (outfd != -1)
1148 close(outfd);
1150 return err;
1153 const struct got_error *
1154 got_object_open_as_blob(struct got_blob_object **blob,
1155 struct got_repository *repo, struct got_object_id *id,
1156 size_t blocksize)
1158 return open_blob(blob, repo, id, blocksize);
1161 const struct got_error *
1162 got_object_blob_open(struct got_blob_object **blob,
1163 struct got_repository *repo, struct got_object *obj, size_t blocksize)
1165 return open_blob(blob, repo, got_object_get_id(obj), blocksize);
1168 const struct got_error *
1169 got_object_blob_close(struct got_blob_object *blob)
1171 const struct got_error *err = NULL;
1172 free(blob->read_buf);
1173 if (blob->f && fclose(blob->f) != 0)
1174 err = got_error_from_errno("fclose");
1175 free(blob->data);
1176 free(blob);
1177 return err;
1180 char *
1181 got_object_blob_id_str(struct got_blob_object *blob, char *buf, size_t size)
1183 return got_sha1_digest_to_str(blob->id.sha1, buf, size);
1186 size_t
1187 got_object_blob_get_hdrlen(struct got_blob_object *blob)
1189 return blob->hdrlen;
1192 const uint8_t *
1193 got_object_blob_get_read_buf(struct got_blob_object *blob)
1195 return blob->read_buf;
1198 const struct got_error *
1199 got_object_blob_read_block(size_t *outlenp, struct got_blob_object *blob)
1201 size_t n;
1203 n = fread(blob->read_buf, 1, blob->blocksize, blob->f);
1204 if (n == 0 && ferror(blob->f))
1205 return got_ferror(blob->f, GOT_ERR_IO);
1206 *outlenp = n;
1207 return NULL;
1210 const struct got_error *
1211 got_object_blob_dump_to_file(size_t *filesize, int *nlines,
1212 off_t **line_offsets, FILE *outfile, struct got_blob_object *blob)
1214 const struct got_error *err = NULL;
1215 size_t n, len, hdrlen;
1216 const uint8_t *buf;
1217 int i;
1218 size_t noffsets = 0;
1219 off_t off = 0, total_len = 0;
1221 if (line_offsets)
1222 *line_offsets = NULL;
1223 if (filesize)
1224 *filesize = 0;
1225 if (nlines)
1226 *nlines = 0;
1228 hdrlen = got_object_blob_get_hdrlen(blob);
1229 do {
1230 err = got_object_blob_read_block(&len, blob);
1231 if (err)
1232 return err;
1233 if (len == 0)
1234 break;
1235 buf = got_object_blob_get_read_buf(blob);
1236 i = hdrlen;
1237 if (line_offsets && nlines) {
1238 if (*line_offsets == NULL) {
1239 /* Have some data but perhaps no '\n'. */
1240 noffsets = 1;
1241 *nlines = 1;
1242 *line_offsets = calloc(1, sizeof(**line_offsets));
1243 if (*line_offsets == NULL)
1244 return got_error_from_errno("malloc");
1246 /* Skip forward over end of first line. */
1247 while (i < len) {
1248 if (buf[i] == '\n')
1249 break;
1250 i++;
1253 /* Scan '\n' offsets in remaining chunk of data. */
1254 while (i < len) {
1255 if (buf[i] != '\n') {
1256 i++;
1257 continue;
1259 (*nlines)++;
1260 if (noffsets < *nlines) {
1261 off_t *o = recallocarray(*line_offsets,
1262 noffsets, *nlines,
1263 sizeof(**line_offsets));
1264 if (o == NULL) {
1265 free(*line_offsets);
1266 *line_offsets = NULL;
1267 return got_error_from_errno(
1268 "recallocarray");
1270 *line_offsets = o;
1271 noffsets = *nlines;
1273 off = total_len + i - hdrlen + 1;
1274 (*line_offsets)[*nlines - 1] = off;
1275 i++;
1278 /* Skip blob object header first time around. */
1279 n = fwrite(buf + hdrlen, 1, len - hdrlen, outfile);
1280 if (n != len - hdrlen)
1281 return got_ferror(outfile, GOT_ERR_IO);
1282 total_len += len - hdrlen;
1283 hdrlen = 0;
1284 } while (len != 0);
1286 if (fflush(outfile) != 0)
1287 return got_error_from_errno("fflush");
1288 rewind(outfile);
1290 if (filesize)
1291 *filesize = total_len;
1293 return NULL;
1296 static const struct got_error *
1297 request_packed_tag(struct got_tag_object **tag, struct got_pack *pack,
1298 int pack_idx, struct got_object_id *id)
1300 const struct got_error *err = NULL;
1302 err = got_privsep_send_tag_req(pack->privsep_child->ibuf, -1, id,
1303 pack_idx);
1304 if (err)
1305 return err;
1307 return got_privsep_recv_tag(tag, pack->privsep_child->ibuf);
1310 static const struct got_error *
1311 read_packed_tag_privsep(struct got_tag_object **tag,
1312 struct got_pack *pack, struct got_packidx *packidx, int idx,
1313 struct got_object_id *id)
1315 const struct got_error *err = NULL;
1317 if (pack->privsep_child)
1318 return request_packed_tag(tag, pack, idx, id);
1320 err = start_pack_privsep_child(pack, packidx);
1321 if (err)
1322 return err;
1324 return request_packed_tag(tag, pack, idx, id);
1327 static const struct got_error *
1328 request_tag(struct got_tag_object **tag, struct got_repository *repo,
1329 int fd)
1331 const struct got_error *err = NULL;
1332 struct imsgbuf *ibuf;
1334 ibuf = repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TAG].ibuf;
1336 err = got_privsep_send_tag_req(ibuf, fd, NULL, -1);
1337 if (err)
1338 return err;
1340 return got_privsep_recv_tag(tag, ibuf);
1343 static const struct got_error *
1344 read_tag_privsep(struct got_tag_object **tag, int obj_fd,
1345 struct got_repository *repo)
1347 const struct got_error *err;
1348 int imsg_fds[2];
1349 pid_t pid;
1350 struct imsgbuf *ibuf;
1352 if (repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TAG].imsg_fd != -1)
1353 return request_tag(tag, repo, obj_fd);
1355 ibuf = calloc(1, sizeof(*ibuf));
1356 if (ibuf == NULL)
1357 return got_error_from_errno("calloc");
1359 if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_fds) == -1) {
1360 err = got_error_from_errno("socketpair");
1361 free(ibuf);
1362 return err;
1365 pid = fork();
1366 if (pid == -1) {
1367 err = got_error_from_errno("fork");
1368 free(ibuf);
1369 return err;
1371 else if (pid == 0) {
1372 got_privsep_exec_child(imsg_fds, GOT_PATH_PROG_READ_TAG,
1373 repo->path);
1374 /* not reached */
1377 if (close(imsg_fds[1]) != 0) {
1378 err = got_error_from_errno("close");
1379 free(ibuf);
1380 return err;
1382 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TAG].imsg_fd =
1383 imsg_fds[0];
1384 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TAG].pid = pid;
1385 imsg_init(ibuf, imsg_fds[0]);
1386 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TAG].ibuf = ibuf;
1388 return request_tag(tag, repo, obj_fd);
1391 static const struct got_error *
1392 open_tag(struct got_tag_object **tag, struct got_repository *repo,
1393 struct got_object_id *id, int check_cache)
1395 const struct got_error *err = NULL;
1396 struct got_packidx *packidx = NULL;
1397 int idx;
1398 char *path_packfile = NULL;
1399 struct got_object *obj = NULL;
1400 int obj_type = GOT_OBJ_TYPE_ANY;
1402 if (check_cache) {
1403 *tag = got_repo_get_cached_tag(repo, id);
1404 if (*tag != NULL) {
1405 (*tag)->refcnt++;
1406 return NULL;
1408 } else
1409 *tag = NULL;
1411 err = got_repo_search_packidx(&packidx, &idx, repo, id);
1412 if (err == NULL) {
1413 struct got_pack *pack = NULL;
1415 err = get_packfile_path(&path_packfile, packidx);
1416 if (err)
1417 return err;
1419 pack = got_repo_get_cached_pack(repo, path_packfile);
1420 if (pack == NULL) {
1421 err = got_repo_cache_pack(&pack, repo, path_packfile,
1422 packidx);
1423 if (err)
1424 goto done;
1427 /* Beware of "leightweight" tags: Check object type first. */
1428 err = read_packed_object_privsep(&obj, repo, pack, packidx,
1429 idx, id);
1430 if (err)
1431 goto done;
1432 obj_type = obj->type;
1433 got_object_close(obj);
1434 if (obj_type != GOT_OBJ_TYPE_TAG) {
1435 err = got_error(GOT_ERR_OBJ_TYPE);
1436 goto done;
1438 err = read_packed_tag_privsep(tag, pack, packidx, idx, id);
1439 } else if (err->code == GOT_ERR_NO_OBJ) {
1440 int fd;
1442 err = open_loose_object(&fd, id, repo);
1443 if (err)
1444 return err;
1445 err = read_object_header_privsep(&obj, repo, fd);
1446 if (err)
1447 return err;
1448 obj_type = obj->type;
1449 got_object_close(obj);
1450 if (obj_type != GOT_OBJ_TYPE_TAG)
1451 return got_error(GOT_ERR_OBJ_TYPE);
1453 err = open_loose_object(&fd, id, repo);
1454 if (err)
1455 return err;
1456 err = read_tag_privsep(tag, fd, repo);
1459 if (err == NULL) {
1460 (*tag)->refcnt++;
1461 err = got_repo_cache_tag(repo, id, *tag);
1463 done:
1464 free(path_packfile);
1465 return err;
1468 const struct got_error *
1469 got_object_open_as_tag(struct got_tag_object **tag,
1470 struct got_repository *repo, struct got_object_id *id)
1472 *tag = got_repo_get_cached_tag(repo, id);
1473 if (*tag != NULL) {
1474 (*tag)->refcnt++;
1475 return NULL;
1478 return open_tag(tag, repo, id, 0);
1481 const struct got_error *
1482 got_object_tag_open(struct got_tag_object **tag,
1483 struct got_repository *repo, struct got_object *obj)
1485 return open_tag(tag, repo, got_object_get_id(obj), 1);
1488 const char *
1489 got_object_tag_get_name(struct got_tag_object *tag)
1491 return tag->tag;
1494 int
1495 got_object_tag_get_object_type(struct got_tag_object *tag)
1497 return tag->obj_type;
1500 struct got_object_id *
1501 got_object_tag_get_object_id(struct got_tag_object *tag)
1503 return &tag->id;
1506 time_t
1507 got_object_tag_get_tagger_time(struct got_tag_object *tag)
1509 return tag->tagger_time;
1512 time_t
1513 got_object_tag_get_tagger_gmtoff(struct got_tag_object *tag)
1515 return tag->tagger_gmtoff;
1518 const char *
1519 got_object_tag_get_tagger(struct got_tag_object *tag)
1521 return tag->tagger;
1524 const char *
1525 got_object_tag_get_message(struct got_tag_object *tag)
1527 return tag->tagmsg;
1530 static struct got_tree_entry *
1531 find_entry_by_name(struct got_tree_object *tree, const char *name, size_t len)
1533 int i;
1535 /* Note that tree entries are sorted in strncmp() order. */
1536 for (i = 0; i < tree->nentries; i++) {
1537 struct got_tree_entry *te = &tree->entries[i];
1538 int cmp = strncmp(te->name, name, len);
1539 if (cmp < 0)
1540 continue;
1541 if (cmp > 0)
1542 break;
1543 if (te->name[len] == '\0')
1544 return te;
1546 return NULL;
1549 struct got_tree_entry *
1550 got_object_tree_find_entry(struct got_tree_object *tree, const char *name)
1552 return find_entry_by_name(tree, name, strlen(name));
1555 const struct got_error *
1556 got_object_id_by_path(struct got_object_id **id, struct got_repository *repo,
1557 struct got_object_id *commit_id, const char *path)
1559 const struct got_error *err = NULL;
1560 struct got_commit_object *commit = NULL;
1561 struct got_tree_object *tree = NULL;
1562 struct got_tree_entry *te = NULL;
1563 const char *seg, *s;
1564 size_t seglen;
1566 *id = NULL;
1568 err = got_object_open_as_commit(&commit, repo, commit_id);
1569 if (err)
1570 goto done;
1572 /* Handle opening of root of commit's tree. */
1573 if (got_path_is_root_dir(path)) {
1574 *id = got_object_id_dup(commit->tree_id);
1575 if (*id == NULL)
1576 err = got_error_from_errno("got_object_id_dup");
1577 goto done;
1580 err = got_object_open_as_tree(&tree, repo, commit->tree_id);
1581 if (err)
1582 goto done;
1584 s = path;
1585 while (s[0] == '/')
1586 s++;
1587 seg = s;
1588 seglen = 0;
1589 while (*s) {
1590 struct got_tree_object *next_tree;
1592 if (*s != '/') {
1593 s++;
1594 seglen++;
1595 if (*s)
1596 continue;
1599 te = find_entry_by_name(tree, seg, seglen);
1600 if (te == NULL) {
1601 err = got_error(GOT_ERR_NO_TREE_ENTRY);
1602 goto done;
1605 if (*s == '\0')
1606 break;
1608 seg = s + 1;
1609 seglen = 0;
1610 s++;
1611 if (*s) {
1612 err = got_object_open_as_tree(&next_tree, repo,
1613 &te->id);
1614 te = NULL;
1615 if (err)
1616 goto done;
1617 got_object_tree_close(tree);
1618 tree = next_tree;
1622 if (te) {
1623 *id = got_object_id_dup(&te->id);
1624 if (*id == NULL)
1625 return got_error_from_errno("got_object_id_dup");
1626 } else
1627 err = got_error(GOT_ERR_NO_TREE_ENTRY);
1628 done:
1629 if (commit)
1630 got_object_commit_close(commit);
1631 if (tree)
1632 got_object_tree_close(tree);
1633 return err;
1636 const struct got_error *
1637 got_object_tree_path_changed(int *changed,
1638 struct got_tree_object *tree01, struct got_tree_object *tree02,
1639 const char *path, struct got_repository *repo)
1641 const struct got_error *err = NULL;
1642 struct got_tree_object *tree1 = NULL, *tree2 = NULL;
1643 struct got_tree_entry *te1 = NULL, *te2 = NULL;
1644 const char *seg, *s;
1645 size_t seglen;
1647 *changed = 0;
1649 /* We are expecting an absolute in-repository path. */
1650 if (path[0] != '/')
1651 return got_error(GOT_ERR_NOT_ABSPATH);
1653 /* We not do support comparing the root path. */
1654 if (path[1] == '\0')
1655 return got_error(GOT_ERR_BAD_PATH);
1657 tree1 = tree01;
1658 tree2 = tree02;
1659 s = path;
1660 s++; /* skip leading '/' */
1661 seg = s;
1662 seglen = 0;
1663 while (*s) {
1664 struct got_tree_object *next_tree1, *next_tree2;
1666 if (*s != '/') {
1667 s++;
1668 seglen++;
1669 if (*s)
1670 continue;
1673 te1 = find_entry_by_name(tree1, seg, seglen);
1674 if (te1 == NULL) {
1675 err = got_error(GOT_ERR_NO_OBJ);
1676 goto done;
1679 te2 = find_entry_by_name(tree2, seg, seglen);
1680 if (te2 == NULL) {
1681 *changed = 1;
1682 goto done;
1685 if (te1->mode != te2->mode) {
1686 *changed = 1;
1687 goto done;
1690 if (got_object_id_cmp(&te1->id, &te2->id) == 0) {
1691 *changed = 0;
1692 goto done;
1695 if (*s == '\0') { /* final path element */
1696 *changed = 1;
1697 goto done;
1700 seg = s + 1;
1701 s++;
1702 seglen = 0;
1703 if (*s) {
1704 err = got_object_open_as_tree(&next_tree1, repo,
1705 &te1->id);
1706 te1 = NULL;
1707 if (err)
1708 goto done;
1709 if (tree1 != tree01)
1710 got_object_tree_close(tree1);
1711 tree1 = next_tree1;
1713 err = got_object_open_as_tree(&next_tree2, repo,
1714 &te2->id);
1715 te2 = NULL;
1716 if (err)
1717 goto done;
1718 if (tree2 != tree02)
1719 got_object_tree_close(tree2);
1720 tree2 = next_tree2;
1723 done:
1724 if (tree1 && tree1 != tree01)
1725 got_object_tree_close(tree1);
1726 if (tree2 && tree2 != tree02)
1727 got_object_tree_close(tree2);
1728 return err;
1731 const struct got_error *
1732 got_object_tree_entry_dup(struct got_tree_entry **new_te,
1733 struct got_tree_entry *te)
1735 const struct got_error *err = NULL;
1737 *new_te = calloc(1, sizeof(**new_te));
1738 if (*new_te == NULL)
1739 return got_error_from_errno("calloc");
1741 (*new_te)->mode = te->mode;
1742 memcpy((*new_te)->name, te->name, sizeof((*new_te)->name));
1743 memcpy(&(*new_te)->id, &te->id, sizeof((*new_te)->id));
1744 return err;
1747 int
1748 got_object_tree_entry_is_submodule(struct got_tree_entry *te)
1750 return (te->mode & S_IFMT) == (S_IFDIR | S_IFLNK);