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_fetch.h"
55 #include "got_lib_repository.h"
57 #ifndef MIN
58 #define MIN(_a,_b) ((_a) < (_b) ? (_a) : (_b))
59 #endif
61 struct got_object_id *
62 got_object_get_id(struct got_object *obj)
63 {
64 return &obj->id;
65 }
67 const struct got_error *
68 got_object_get_id_str(char **outbuf, struct got_object *obj)
69 {
70 return got_object_id_str(outbuf, &obj->id);
71 }
73 const struct got_error *
74 got_object_get_type(int *type, struct got_repository *repo,
75 struct got_object_id *id)
76 {
77 const struct got_error *err = NULL;
78 struct got_object *obj;
80 err = got_object_open(&obj, repo, id);
81 if (err)
82 return err;
84 switch (obj->type) {
85 case GOT_OBJ_TYPE_COMMIT:
86 case GOT_OBJ_TYPE_TREE:
87 case GOT_OBJ_TYPE_BLOB:
88 case GOT_OBJ_TYPE_TAG:
89 *type = obj->type;
90 break;
91 default:
92 err = got_error(GOT_ERR_OBJ_TYPE);
93 break;
94 }
96 got_object_close(obj);
97 return err;
98 }
100 const struct got_error *
101 got_object_get_path(char **path, struct got_object_id *id,
102 struct got_repository *repo)
104 const struct got_error *err = NULL;
105 char *hex = NULL;
106 char *path_objects;
108 *path = NULL;
110 path_objects = got_repo_get_path_objects(repo);
111 if (path_objects == NULL)
112 return got_error_from_errno("got_repo_get_path_objects");
114 err = got_object_id_str(&hex, id);
115 if (err)
116 goto done;
118 if (asprintf(path, "%s/%.2x/%s", path_objects,
119 id->sha1[0], hex + 2) == -1)
120 err = got_error_from_errno("asprintf");
122 done:
123 free(hex);
124 free(path_objects);
125 return err;
128 static const struct got_error *
129 open_loose_object(int *fd, struct got_object_id *id,
130 struct got_repository *repo)
132 const struct got_error *err = NULL;
133 char *path;
135 err = got_object_get_path(&path, id, repo);
136 if (err)
137 return err;
138 *fd = open(path, O_RDONLY | O_NOFOLLOW);
139 if (*fd == -1) {
140 err = got_error_from_errno2("open", path);
141 goto done;
143 done:
144 free(path);
145 return err;
148 static const struct got_error *
149 get_packfile_path(char **path_packfile, struct got_packidx *packidx)
151 size_t size;
153 /* Packfile path contains ".pack" instead of ".idx", so add one byte. */
154 size = strlen(packidx->path_packidx) + 2;
155 if (size < GOT_PACKFILE_NAMELEN + 1)
156 return got_error_path(packidx->path_packidx, GOT_ERR_BAD_PATH);
158 *path_packfile = malloc(size);
159 if (*path_packfile == NULL)
160 return got_error_from_errno("malloc");
162 /* Copy up to and excluding ".idx". */
163 if (strlcpy(*path_packfile, packidx->path_packidx,
164 size - strlen(GOT_PACKIDX_SUFFIX) - 1) >= size)
165 return got_error(GOT_ERR_NO_SPACE);
167 if (strlcat(*path_packfile, GOT_PACKFILE_SUFFIX, size) >= size)
168 return got_error(GOT_ERR_NO_SPACE);
170 return NULL;
173 static const struct got_error *
174 request_packed_object(struct got_object **obj, struct got_pack *pack, int idx,
175 struct got_object_id *id)
177 const struct got_error *err = NULL;
178 struct imsgbuf *ibuf = pack->privsep_child->ibuf;
180 err = got_privsep_send_packed_obj_req(ibuf, idx, id);
181 if (err)
182 return err;
184 err = got_privsep_recv_obj(obj, ibuf);
185 if (err)
186 return err;
188 memcpy(&(*obj)->id, id, sizeof((*obj)->id));
190 return NULL;
193 static void
194 set_max_datasize(void)
196 struct rlimit rl;
198 if (getrlimit(RLIMIT_DATA, &rl) != 0)
199 return;
201 rl.rlim_cur = rl.rlim_max;
202 setrlimit(RLIMIT_DATA, &rl);
205 static const struct got_error *
206 start_pack_privsep_child(struct got_pack *pack, struct got_packidx *packidx)
208 const struct got_error *err = NULL;
209 int imsg_fds[2];
210 pid_t pid;
211 struct imsgbuf *ibuf;
213 ibuf = calloc(1, sizeof(*ibuf));
214 if (ibuf == NULL)
215 return got_error_from_errno("calloc");
217 pack->privsep_child = calloc(1, sizeof(*pack->privsep_child));
218 if (pack->privsep_child == NULL) {
219 err = got_error_from_errno("calloc");
220 free(ibuf);
221 return err;
224 if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_fds) == -1) {
225 err = got_error_from_errno("socketpair");
226 goto done;
229 pid = fork();
230 if (pid == -1) {
231 err = got_error_from_errno("fork");
232 goto done;
233 } else if (pid == 0) {
234 set_max_datasize();
235 got_privsep_exec_child(imsg_fds, GOT_PATH_PROG_READ_PACK,
236 pack->path_packfile);
237 /* not reached */
240 if (close(imsg_fds[1]) != 0)
241 return got_error_from_errno("close");
242 pack->privsep_child->imsg_fd = imsg_fds[0];
243 pack->privsep_child->pid = pid;
244 imsg_init(ibuf, imsg_fds[0]);
245 pack->privsep_child->ibuf = ibuf;
247 err = got_privsep_init_pack_child(ibuf, pack, packidx);
248 if (err) {
249 const struct got_error *child_err;
250 err = got_privsep_send_stop(pack->privsep_child->imsg_fd);
251 child_err = got_privsep_wait_for_child(
252 pack->privsep_child->pid);
253 if (child_err && err == NULL)
254 err = child_err;
256 done:
257 if (err) {
258 free(ibuf);
259 free(pack->privsep_child);
260 pack->privsep_child = NULL;
262 return err;
265 static const struct got_error *
266 read_packed_object_privsep(struct got_object **obj,
267 struct got_repository *repo, struct got_pack *pack,
268 struct got_packidx *packidx, int idx, struct got_object_id *id)
270 const struct got_error *err = NULL;
272 if (pack->privsep_child)
273 return request_packed_object(obj, pack, idx, id);
275 err = start_pack_privsep_child(pack, packidx);
276 if (err)
277 return err;
279 return request_packed_object(obj, pack, idx, id);
283 static const struct got_error *
284 open_packed_object(struct got_object **obj, struct got_object_id *id,
285 struct got_repository *repo)
287 const struct got_error *err = NULL;
288 struct got_pack *pack = NULL;
289 struct got_packidx *packidx = NULL;
290 int idx;
291 char *path_packfile;
293 err = got_repo_search_packidx(&packidx, &idx, repo, id);
294 if (err)
295 return err;
297 err = get_packfile_path(&path_packfile, packidx);
298 if (err)
299 return err;
301 pack = got_repo_get_cached_pack(repo, path_packfile);
302 if (pack == NULL) {
303 err = got_repo_cache_pack(&pack, repo, path_packfile, packidx);
304 if (err)
305 goto done;
308 err = read_packed_object_privsep(obj, repo, pack, packidx, idx, id);
309 if (err)
310 goto done;
311 done:
312 free(path_packfile);
313 return err;
316 static const struct got_error *
317 request_object(struct got_object **obj, struct got_repository *repo, int fd)
319 const struct got_error *err = NULL;
320 struct imsgbuf *ibuf;
322 ibuf = repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_OBJECT].ibuf;
324 err = got_privsep_send_obj_req(ibuf, fd);
325 if (err)
326 return err;
328 return got_privsep_recv_obj(obj, ibuf);
331 static const struct got_error *
332 read_object_header_privsep(struct got_object **obj, struct got_repository *repo,
333 int obj_fd)
335 const struct got_error *err;
336 int imsg_fds[2];
337 pid_t pid;
338 struct imsgbuf *ibuf;
340 if (repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_OBJECT].imsg_fd != -1)
341 return request_object(obj, repo, obj_fd);
343 ibuf = calloc(1, sizeof(*ibuf));
344 if (ibuf == NULL)
345 return got_error_from_errno("calloc");
347 if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_fds) == -1) {
348 err = got_error_from_errno("socketpair");
349 free(ibuf);
350 return err;
353 pid = fork();
354 if (pid == -1) {
355 err = got_error_from_errno("fork");
356 free(ibuf);
357 return err;
359 else if (pid == 0) {
360 got_privsep_exec_child(imsg_fds, GOT_PATH_PROG_READ_OBJECT,
361 repo->path);
362 /* not reached */
365 if (close(imsg_fds[1]) != 0) {
366 err = got_error_from_errno("close");
367 free(ibuf);
368 return err;
370 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_OBJECT].imsg_fd =
371 imsg_fds[0];
372 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_OBJECT].pid = pid;
373 imsg_init(ibuf, imsg_fds[0]);
374 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_OBJECT].ibuf = ibuf;
376 return request_object(obj, repo, obj_fd);
380 const struct got_error *
381 got_object_open(struct got_object **obj, struct got_repository *repo,
382 struct got_object_id *id)
384 const struct got_error *err = NULL;
385 char *path;
386 int fd;
388 *obj = got_repo_get_cached_object(repo, id);
389 if (*obj != NULL) {
390 (*obj)->refcnt++;
391 return NULL;
394 err = open_packed_object(obj, id, repo);
395 if (err && err->code != GOT_ERR_NO_OBJ)
396 return err;
397 if (*obj) {
398 (*obj)->refcnt++;
399 return got_repo_cache_object(repo, id, *obj);
402 err = got_object_get_path(&path, id, repo);
403 if (err)
404 return err;
406 fd = open(path, O_RDONLY | O_NOFOLLOW);
407 if (fd == -1) {
408 if (errno == ENOENT)
409 err = got_error_no_obj(id);
410 else
411 err = got_error_from_errno2("open", path);
412 goto done;
413 } else {
414 err = read_object_header_privsep(obj, repo, fd);
415 if (err)
416 goto done;
417 memcpy((*obj)->id.sha1, id->sha1, SHA1_DIGEST_LENGTH);
420 (*obj)->refcnt++;
421 err = got_repo_cache_object(repo, id, *obj);
422 done:
423 free(path);
424 return err;
428 const struct got_error *
429 got_object_open_by_id_str(struct got_object **obj, struct got_repository *repo,
430 const char *id_str)
432 struct got_object_id id;
434 if (!got_parse_sha1_digest(id.sha1, id_str))
435 return got_error_path(id_str, GOT_ERR_BAD_OBJ_ID_STR);
437 return got_object_open(obj, repo, &id);
440 const struct got_error *
441 got_object_resolve_id_str(struct got_object_id **id,
442 struct got_repository *repo, const char *id_str)
444 const struct got_error *err = NULL;
445 struct got_object *obj;
447 err = got_object_open_by_id_str(&obj, repo, id_str);
448 if (err)
449 return err;
451 *id = got_object_id_dup(got_object_get_id(obj));
452 got_object_close(obj);
453 if (*id == NULL)
454 return got_error_from_errno("got_object_id_dup");
456 return NULL;
459 static const struct got_error *
460 request_packed_commit(struct got_commit_object **commit, struct got_pack *pack,
461 int pack_idx, struct got_object_id *id)
463 const struct got_error *err = NULL;
465 err = got_privsep_send_commit_req(pack->privsep_child->ibuf, -1, id,
466 pack_idx);
467 if (err)
468 return err;
470 err = got_privsep_recv_commit(commit, pack->privsep_child->ibuf);
471 if (err)
472 return err;
474 (*commit)->flags |= GOT_COMMIT_FLAG_PACKED;
475 return NULL;
478 static const struct got_error *
479 read_packed_commit_privsep(struct got_commit_object **commit,
480 struct got_pack *pack, struct got_packidx *packidx, int idx,
481 struct got_object_id *id)
483 const struct got_error *err = NULL;
485 if (pack->privsep_child)
486 return request_packed_commit(commit, pack, idx, id);
488 err = start_pack_privsep_child(pack, packidx);
489 if (err)
490 return err;
492 return request_packed_commit(commit, pack, idx, id);
495 static const struct got_error *
496 request_commit(struct got_commit_object **commit, struct got_repository *repo,
497 int fd)
499 const struct got_error *err = NULL;
500 struct imsgbuf *ibuf;
502 ibuf = repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_COMMIT].ibuf;
504 err = got_privsep_send_commit_req(ibuf, fd, NULL, -1);
505 if (err)
506 return err;
508 return got_privsep_recv_commit(commit, ibuf);
511 static const struct got_error *
512 read_commit_privsep(struct got_commit_object **commit, int obj_fd,
513 struct got_repository *repo)
515 const struct got_error *err;
516 int imsg_fds[2];
517 pid_t pid;
518 struct imsgbuf *ibuf;
520 if (repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_COMMIT].imsg_fd != -1)
521 return request_commit(commit, repo, obj_fd);
523 ibuf = calloc(1, sizeof(*ibuf));
524 if (ibuf == NULL)
525 return got_error_from_errno("calloc");
527 if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_fds) == -1) {
528 err = got_error_from_errno("socketpair");
529 free(ibuf);
530 return err;
533 pid = fork();
534 if (pid == -1) {
535 err = got_error_from_errno("fork");
536 free(ibuf);
537 return err;
539 else if (pid == 0) {
540 got_privsep_exec_child(imsg_fds, GOT_PATH_PROG_READ_COMMIT,
541 repo->path);
542 /* not reached */
545 if (close(imsg_fds[1]) != 0) {
546 err = got_error_from_errno("close");
547 free(ibuf);
548 return err;
550 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_COMMIT].imsg_fd =
551 imsg_fds[0];
552 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_COMMIT].pid = pid;
553 imsg_init(ibuf, imsg_fds[0]);
554 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_COMMIT].ibuf = ibuf;
556 return request_commit(commit, repo, obj_fd);
560 static const struct got_error *
561 open_commit(struct got_commit_object **commit,
562 struct got_repository *repo, struct got_object_id *id, int check_cache)
564 const struct got_error *err = NULL;
565 struct got_packidx *packidx = NULL;
566 int idx;
567 char *path_packfile = NULL;
569 if (check_cache) {
570 *commit = got_repo_get_cached_commit(repo, id);
571 if (*commit != NULL) {
572 (*commit)->refcnt++;
573 return NULL;
575 } else
576 *commit = NULL;
578 err = got_repo_search_packidx(&packidx, &idx, repo, id);
579 if (err == NULL) {
580 struct got_pack *pack = NULL;
582 err = get_packfile_path(&path_packfile, packidx);
583 if (err)
584 return err;
586 pack = got_repo_get_cached_pack(repo, path_packfile);
587 if (pack == NULL) {
588 err = got_repo_cache_pack(&pack, repo, path_packfile,
589 packidx);
590 if (err)
591 goto done;
593 err = read_packed_commit_privsep(commit, pack,
594 packidx, idx, id);
595 } else if (err->code == GOT_ERR_NO_OBJ) {
596 int fd;
598 err = open_loose_object(&fd, id, repo);
599 if (err)
600 return err;
601 err = read_commit_privsep(commit, fd, repo);
604 if (err == NULL) {
605 (*commit)->refcnt++;
606 err = got_repo_cache_commit(repo, id, *commit);
608 done:
609 free(path_packfile);
610 return err;
613 const struct got_error *
614 got_object_open_as_commit(struct got_commit_object **commit,
615 struct got_repository *repo, struct got_object_id *id)
617 *commit = got_repo_get_cached_commit(repo, id);
618 if (*commit != NULL) {
619 (*commit)->refcnt++;
620 return NULL;
623 return open_commit(commit, repo, id, 0);
626 const struct got_error *
627 got_object_commit_open(struct got_commit_object **commit,
628 struct got_repository *repo, struct got_object *obj)
630 return open_commit(commit, repo, got_object_get_id(obj), 1);
633 const struct got_error *
634 got_object_qid_alloc(struct got_object_qid **qid, struct got_object_id *id)
636 const struct got_error *err = NULL;
638 *qid = calloc(1, sizeof(**qid));
639 if (*qid == NULL)
640 return got_error_from_errno("calloc");
642 (*qid)->id = got_object_id_dup(id);
643 if ((*qid)->id == NULL) {
644 err = got_error_from_errno("got_object_id_dup");
645 got_object_qid_free(*qid);
646 *qid = NULL;
647 return err;
650 return NULL;
653 static const struct got_error *
654 request_packed_tree(struct got_tree_object **tree, struct got_pack *pack,
655 int pack_idx, struct got_object_id *id)
657 const struct got_error *err = NULL;
659 err = got_privsep_send_tree_req(pack->privsep_child->ibuf, -1, id,
660 pack_idx);
661 if (err)
662 return err;
664 return got_privsep_recv_tree(tree, pack->privsep_child->ibuf);
667 static const struct got_error *
668 read_packed_tree_privsep(struct got_tree_object **tree,
669 struct got_pack *pack, struct got_packidx *packidx, int idx,
670 struct got_object_id *id)
672 const struct got_error *err = NULL;
674 if (pack->privsep_child)
675 return request_packed_tree(tree, pack, idx, id);
677 err = start_pack_privsep_child(pack, packidx);
678 if (err)
679 return err;
681 return request_packed_tree(tree, pack, idx, id);
684 static const struct got_error *
685 request_tree(struct got_tree_object **tree, struct got_repository *repo,
686 int fd)
688 const struct got_error *err = NULL;
689 struct imsgbuf *ibuf;
691 ibuf = repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TREE].ibuf;
693 err = got_privsep_send_tree_req(ibuf, fd, NULL, -1);
694 if (err)
695 return err;
697 return got_privsep_recv_tree(tree, ibuf);
700 const struct got_error *
701 read_tree_privsep(struct got_tree_object **tree, int obj_fd,
702 struct got_repository *repo)
704 const struct got_error *err;
705 int imsg_fds[2];
706 pid_t pid;
707 struct imsgbuf *ibuf;
709 if (repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TREE].imsg_fd != -1)
710 return request_tree(tree, repo, obj_fd);
712 ibuf = calloc(1, sizeof(*ibuf));
713 if (ibuf == NULL)
714 return got_error_from_errno("calloc");
716 if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_fds) == -1) {
717 err = got_error_from_errno("socketpair");
718 free(ibuf);
719 return err;
722 pid = fork();
723 if (pid == -1) {
724 err = got_error_from_errno("fork");
725 free(ibuf);
726 return err;
728 else if (pid == 0) {
729 got_privsep_exec_child(imsg_fds, GOT_PATH_PROG_READ_TREE,
730 repo->path);
731 /* not reached */
734 if (close(imsg_fds[1]) != 0) {
735 err = got_error_from_errno("close");
736 free(ibuf);
737 return err;
739 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TREE].imsg_fd =
740 imsg_fds[0];
741 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TREE].pid = pid;
742 imsg_init(ibuf, imsg_fds[0]);
743 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TREE].ibuf = ibuf;
746 return request_tree(tree, repo, obj_fd);
749 static const struct got_error *
750 open_tree(struct got_tree_object **tree, struct got_repository *repo,
751 struct got_object_id *id, int check_cache)
753 const struct got_error *err = NULL;
754 struct got_packidx *packidx = NULL;
755 int idx;
756 char *path_packfile = NULL;
758 if (check_cache) {
759 *tree = got_repo_get_cached_tree(repo, id);
760 if (*tree != NULL) {
761 (*tree)->refcnt++;
762 return NULL;
764 } else
765 *tree = NULL;
767 err = got_repo_search_packidx(&packidx, &idx, repo, id);
768 if (err == NULL) {
769 struct got_pack *pack = NULL;
771 err = get_packfile_path(&path_packfile, packidx);
772 if (err)
773 return err;
775 pack = got_repo_get_cached_pack(repo, path_packfile);
776 if (pack == NULL) {
777 err = got_repo_cache_pack(&pack, repo, path_packfile,
778 packidx);
779 if (err)
780 goto done;
782 err = read_packed_tree_privsep(tree, pack,
783 packidx, idx, id);
784 } else if (err->code == GOT_ERR_NO_OBJ) {
785 int fd;
787 err = open_loose_object(&fd, id, repo);
788 if (err)
789 return err;
790 err = read_tree_privsep(tree, fd, repo);
793 if (err == NULL) {
794 (*tree)->refcnt++;
795 err = got_repo_cache_tree(repo, id, *tree);
797 done:
798 free(path_packfile);
799 return err;
802 const struct got_error *
803 got_object_open_as_tree(struct got_tree_object **tree,
804 struct got_repository *repo, struct got_object_id *id)
806 *tree = got_repo_get_cached_tree(repo, id);
807 if (*tree != NULL) {
808 (*tree)->refcnt++;
809 return NULL;
812 return open_tree(tree, repo, id, 0);
815 const struct got_error *
816 got_object_tree_open(struct got_tree_object **tree,
817 struct got_repository *repo, struct got_object *obj)
819 return open_tree(tree, repo, got_object_get_id(obj), 1);
822 int
823 got_object_tree_get_nentries(struct got_tree_object *tree)
825 return tree->nentries;
828 struct got_tree_entry *
829 got_object_tree_get_first_entry(struct got_tree_object *tree)
831 return got_object_tree_get_entry(tree, 0);
834 struct got_tree_entry *
835 got_object_tree_get_last_entry(struct got_tree_object *tree)
837 return got_object_tree_get_entry(tree, tree->nentries - 1);
840 struct got_tree_entry *
841 got_object_tree_get_entry(struct got_tree_object *tree, int i)
843 if (i < 0 || i >= tree->nentries)
844 return NULL;
845 return &tree->entries[i];
848 mode_t
849 got_tree_entry_get_mode(struct got_tree_entry *te)
851 return te->mode;
854 const char *
855 got_tree_entry_get_name(struct got_tree_entry *te)
857 return &te->name[0];
860 struct got_object_id *
861 got_tree_entry_get_id(struct got_tree_entry *te)
863 return &te->id;
866 int
867 got_tree_entry_get_index(struct got_tree_entry *te)
869 return te->idx;
872 struct got_tree_entry *
873 got_tree_entry_get_next(struct got_tree_object *tree,
874 struct got_tree_entry *te)
876 return got_object_tree_get_entry(tree, te->idx + 1);
879 struct got_tree_entry *
880 got_tree_entry_get_prev(struct got_tree_object *tree,
881 struct got_tree_entry *te)
883 return got_object_tree_get_entry(tree, te->idx - 1);
886 static const struct got_error *
887 request_packed_blob(uint8_t **outbuf, size_t *size, size_t *hdrlen, int outfd,
888 struct got_pack *pack, struct got_packidx *packidx, int idx,
889 struct got_object_id *id)
891 const struct got_error *err = NULL;
892 int outfd_child;
893 int basefd, accumfd; /* temporary files for delta application */
895 basefd = got_opentempfd();
896 if (basefd == -1)
897 return got_error_from_errno("got_opentempfd");
898 accumfd = got_opentempfd();
899 if (accumfd == -1)
900 return got_error_from_errno("got_opentempfd");
902 outfd_child = dup(outfd);
903 if (outfd_child == -1)
904 return got_error_from_errno("dup");
906 err = got_privsep_send_blob_req(pack->privsep_child->ibuf, -1, id, idx);
907 if (err)
908 return err;
910 err = got_privsep_send_blob_outfd(pack->privsep_child->ibuf,
911 outfd_child);
912 if (err) {
913 close(basefd);
914 close(accumfd);
915 return err;
918 err = got_privsep_send_tmpfd(pack->privsep_child->ibuf,
919 basefd);
920 if (err) {
921 close(accumfd);
922 return err;
925 err = got_privsep_send_tmpfd(pack->privsep_child->ibuf,
926 accumfd);
927 if (err)
928 return err;
930 err = got_privsep_recv_blob(outbuf, size, hdrlen,
931 pack->privsep_child->ibuf);
932 if (err)
933 return err;
935 if (lseek(outfd, SEEK_SET, 0) == -1)
936 err = got_error_from_errno("lseek");
938 return err;
941 static const struct got_error *
942 read_packed_blob_privsep(uint8_t **outbuf, size_t *size, size_t *hdrlen,
943 int outfd, struct got_pack *pack, struct got_packidx *packidx, int idx,
944 struct got_object_id *id)
946 const struct got_error *err = NULL;
948 if (pack->privsep_child == NULL) {
949 err = start_pack_privsep_child(pack, packidx);
950 if (err)
951 return err;
954 return request_packed_blob(outbuf, size, hdrlen, outfd, pack, packidx,
955 idx, id);
958 static const struct got_error *
959 request_blob(uint8_t **outbuf, size_t *size, size_t *hdrlen, int outfd,
960 int infd, struct imsgbuf *ibuf)
962 const struct got_error *err = NULL;
963 int outfd_child;
965 outfd_child = dup(outfd);
966 if (outfd_child == -1)
967 return got_error_from_errno("dup");
969 err = got_privsep_send_blob_req(ibuf, infd, NULL, -1);
970 if (err)
971 return err;
973 err = got_privsep_send_blob_outfd(ibuf, outfd_child);
974 if (err)
975 return err;
977 err = got_privsep_recv_blob(outbuf, size, hdrlen, ibuf);
978 if (err)
979 return err;
981 if (lseek(outfd, SEEK_SET, 0) == -1)
982 return got_error_from_errno("lseek");
984 return err;
987 static const struct got_error *
988 read_blob_privsep(uint8_t **outbuf, size_t *size, size_t *hdrlen,
989 int outfd, int infd, struct got_repository *repo)
991 const struct got_error *err;
992 int imsg_fds[2];
993 pid_t pid;
994 struct imsgbuf *ibuf;
996 if (repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_BLOB].imsg_fd != -1) {
997 ibuf = repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_BLOB].ibuf;
998 return request_blob(outbuf, size, hdrlen, outfd, infd, ibuf);
1001 ibuf = calloc(1, sizeof(*ibuf));
1002 if (ibuf == NULL)
1003 return got_error_from_errno("calloc");
1005 if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_fds) == -1) {
1006 err = got_error_from_errno("socketpair");
1007 free(ibuf);
1008 return err;
1011 pid = fork();
1012 if (pid == -1) {
1013 err = got_error_from_errno("fork");
1014 free(ibuf);
1015 return err;
1017 else if (pid == 0) {
1018 got_privsep_exec_child(imsg_fds, GOT_PATH_PROG_READ_BLOB,
1019 repo->path);
1020 /* not reached */
1023 if (close(imsg_fds[1]) != 0) {
1024 err = got_error_from_errno("close");
1025 free(ibuf);
1026 return err;
1028 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_BLOB].imsg_fd =
1029 imsg_fds[0];
1030 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_BLOB].pid = pid;
1031 imsg_init(ibuf, imsg_fds[0]);
1032 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_BLOB].ibuf = ibuf;
1034 return request_blob(outbuf, size, hdrlen, outfd, infd, ibuf);
1037 static const struct got_error *
1038 open_blob(struct got_blob_object **blob, struct got_repository *repo,
1039 struct got_object_id *id, size_t blocksize)
1041 const struct got_error *err = NULL;
1042 struct got_packidx *packidx = NULL;
1043 int idx;
1044 char *path_packfile = NULL;
1045 uint8_t *outbuf;
1046 int outfd;
1047 size_t size, hdrlen;
1048 struct stat sb;
1050 *blob = calloc(1, sizeof(**blob));
1051 if (*blob == NULL)
1052 return got_error_from_errno("calloc");
1054 outfd = got_opentempfd();
1055 if (outfd == -1)
1056 return got_error_from_errno("got_opentempfd");
1058 (*blob)->read_buf = malloc(blocksize);
1059 if ((*blob)->read_buf == NULL) {
1060 err = got_error_from_errno("malloc");
1061 goto done;
1064 err = got_repo_search_packidx(&packidx, &idx, repo, id);
1065 if (err == NULL) {
1066 struct got_pack *pack = NULL;
1068 err = get_packfile_path(&path_packfile, packidx);
1069 if (err)
1070 goto done;
1072 pack = got_repo_get_cached_pack(repo, path_packfile);
1073 if (pack == NULL) {
1074 err = got_repo_cache_pack(&pack, repo, path_packfile,
1075 packidx);
1076 if (err)
1077 goto done;
1079 err = read_packed_blob_privsep(&outbuf, &size, &hdrlen, outfd,
1080 pack, packidx, idx, id);
1081 } else if (err->code == GOT_ERR_NO_OBJ) {
1082 int infd;
1084 err = open_loose_object(&infd, id, repo);
1085 if (err)
1086 goto done;
1087 err = read_blob_privsep(&outbuf, &size, &hdrlen, outfd, infd,
1088 repo);
1090 if (err)
1091 goto done;
1093 if (hdrlen > size) {
1094 err = got_error(GOT_ERR_BAD_OBJ_HDR);
1095 goto done;
1098 if (outbuf) {
1099 if (close(outfd) != 0 && err == NULL)
1100 err = got_error_from_errno("close");
1101 outfd = -1;
1102 (*blob)->f = fmemopen(outbuf, size, "rb");
1103 if ((*blob)->f == NULL) {
1104 err = got_error_from_errno("fmemopen");
1105 free(outbuf);
1106 goto done;
1108 (*blob)->data = outbuf;
1109 } else {
1110 if (fstat(outfd, &sb) == -1) {
1111 err = got_error_from_errno("fstat");
1112 goto done;
1115 if (sb.st_size != size) {
1116 err = got_error(GOT_ERR_PRIVSEP_LEN);
1117 goto done;
1120 (*blob)->f = fdopen(outfd, "rb");
1121 if ((*blob)->f == NULL) {
1122 err = got_error_from_errno("fdopen");
1123 close(outfd);
1124 outfd = -1;
1125 goto done;
1129 (*blob)->hdrlen = hdrlen;
1130 (*blob)->blocksize = blocksize;
1131 memcpy(&(*blob)->id.sha1, id->sha1, SHA1_DIGEST_LENGTH);
1133 done:
1134 free(path_packfile);
1135 if (err) {
1136 if (*blob) {
1137 got_object_blob_close(*blob);
1138 *blob = NULL;
1139 } else if (outfd != -1)
1140 close(outfd);
1142 return err;
1145 const struct got_error *
1146 got_object_open_as_blob(struct got_blob_object **blob,
1147 struct got_repository *repo, struct got_object_id *id,
1148 size_t blocksize)
1150 return open_blob(blob, repo, id, blocksize);
1153 const struct got_error *
1154 got_object_blob_open(struct got_blob_object **blob,
1155 struct got_repository *repo, struct got_object *obj, size_t blocksize)
1157 return open_blob(blob, repo, got_object_get_id(obj), blocksize);
1160 const struct got_error *
1161 got_object_blob_close(struct got_blob_object *blob)
1163 const struct got_error *err = NULL;
1164 free(blob->read_buf);
1165 if (blob->f && fclose(blob->f) != 0)
1166 err = got_error_from_errno("fclose");
1167 free(blob->data);
1168 free(blob);
1169 return err;
1172 char *
1173 got_object_blob_id_str(struct got_blob_object *blob, char *buf, size_t size)
1175 return got_sha1_digest_to_str(blob->id.sha1, buf, size);
1178 size_t
1179 got_object_blob_get_hdrlen(struct got_blob_object *blob)
1181 return blob->hdrlen;
1184 const uint8_t *
1185 got_object_blob_get_read_buf(struct got_blob_object *blob)
1187 return blob->read_buf;
1190 const struct got_error *
1191 got_object_blob_read_block(size_t *outlenp, struct got_blob_object *blob)
1193 size_t n;
1195 n = fread(blob->read_buf, 1, blob->blocksize, blob->f);
1196 if (n == 0 && ferror(blob->f))
1197 return got_ferror(blob->f, GOT_ERR_IO);
1198 *outlenp = n;
1199 return NULL;
1202 const struct got_error *
1203 got_object_blob_dump_to_file(size_t *filesize, int *nlines,
1204 off_t **line_offsets, FILE *outfile, struct got_blob_object *blob)
1206 const struct got_error *err = NULL;
1207 size_t n, len, hdrlen;
1208 const uint8_t *buf;
1209 int i;
1210 size_t noffsets = 0;
1211 off_t off = 0, total_len = 0;
1213 if (line_offsets)
1214 *line_offsets = NULL;
1215 if (filesize)
1216 *filesize = 0;
1217 if (nlines)
1218 *nlines = 0;
1220 hdrlen = got_object_blob_get_hdrlen(blob);
1221 do {
1222 err = got_object_blob_read_block(&len, blob);
1223 if (err)
1224 return err;
1225 if (len == 0)
1226 break;
1227 buf = got_object_blob_get_read_buf(blob);
1228 i = hdrlen;
1229 if (line_offsets && nlines) {
1230 if (*line_offsets == NULL) {
1231 /* Have some data but perhaps no '\n'. */
1232 noffsets = 1;
1233 *nlines = 1;
1234 *line_offsets = calloc(1, sizeof(**line_offsets));
1235 if (*line_offsets == NULL)
1236 return got_error_from_errno("calloc");
1238 /* Skip forward over end of first line. */
1239 while (i < len) {
1240 if (buf[i] == '\n')
1241 break;
1242 i++;
1245 /* Scan '\n' offsets in remaining chunk of data. */
1246 while (i < len) {
1247 if (buf[i] != '\n') {
1248 i++;
1249 continue;
1251 (*nlines)++;
1252 if (noffsets < *nlines) {
1253 off_t *o = recallocarray(*line_offsets,
1254 noffsets, *nlines,
1255 sizeof(**line_offsets));
1256 if (o == NULL) {
1257 free(*line_offsets);
1258 *line_offsets = NULL;
1259 return got_error_from_errno(
1260 "recallocarray");
1262 *line_offsets = o;
1263 noffsets = *nlines;
1265 off = total_len + i - hdrlen + 1;
1266 (*line_offsets)[*nlines - 1] = off;
1267 i++;
1270 /* Skip blob object header first time around. */
1271 n = fwrite(buf + hdrlen, 1, len - hdrlen, outfile);
1272 if (n != len - hdrlen)
1273 return got_ferror(outfile, GOT_ERR_IO);
1274 total_len += len - hdrlen;
1275 hdrlen = 0;
1276 } while (len != 0);
1278 if (fflush(outfile) != 0)
1279 return got_error_from_errno("fflush");
1280 rewind(outfile);
1282 if (filesize)
1283 *filesize = total_len;
1285 return NULL;
1288 static const struct got_error *
1289 request_packed_tag(struct got_tag_object **tag, struct got_pack *pack,
1290 int pack_idx, struct got_object_id *id)
1292 const struct got_error *err = NULL;
1294 err = got_privsep_send_tag_req(pack->privsep_child->ibuf, -1, id,
1295 pack_idx);
1296 if (err)
1297 return err;
1299 return got_privsep_recv_tag(tag, pack->privsep_child->ibuf);
1302 static const struct got_error *
1303 read_packed_tag_privsep(struct got_tag_object **tag,
1304 struct got_pack *pack, struct got_packidx *packidx, int idx,
1305 struct got_object_id *id)
1307 const struct got_error *err = NULL;
1309 if (pack->privsep_child)
1310 return request_packed_tag(tag, pack, idx, id);
1312 err = start_pack_privsep_child(pack, packidx);
1313 if (err)
1314 return err;
1316 return request_packed_tag(tag, pack, idx, id);
1319 static const struct got_error *
1320 request_tag(struct got_tag_object **tag, struct got_repository *repo,
1321 int fd)
1323 const struct got_error *err = NULL;
1324 struct imsgbuf *ibuf;
1326 ibuf = repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TAG].ibuf;
1328 err = got_privsep_send_tag_req(ibuf, fd, NULL, -1);
1329 if (err)
1330 return err;
1332 return got_privsep_recv_tag(tag, ibuf);
1335 static const struct got_error *
1336 read_tag_privsep(struct got_tag_object **tag, int obj_fd,
1337 struct got_repository *repo)
1339 const struct got_error *err;
1340 int imsg_fds[2];
1341 pid_t pid;
1342 struct imsgbuf *ibuf;
1344 if (repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TAG].imsg_fd != -1)
1345 return request_tag(tag, repo, obj_fd);
1347 ibuf = calloc(1, sizeof(*ibuf));
1348 if (ibuf == NULL)
1349 return got_error_from_errno("calloc");
1351 if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_fds) == -1) {
1352 err = got_error_from_errno("socketpair");
1353 free(ibuf);
1354 return err;
1357 pid = fork();
1358 if (pid == -1) {
1359 err = got_error_from_errno("fork");
1360 free(ibuf);
1361 return err;
1363 else if (pid == 0) {
1364 got_privsep_exec_child(imsg_fds, GOT_PATH_PROG_READ_TAG,
1365 repo->path);
1366 /* not reached */
1369 if (close(imsg_fds[1]) != 0) {
1370 err = got_error_from_errno("close");
1371 free(ibuf);
1372 return err;
1374 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TAG].imsg_fd =
1375 imsg_fds[0];
1376 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TAG].pid = pid;
1377 imsg_init(ibuf, imsg_fds[0]);
1378 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TAG].ibuf = ibuf;
1380 return request_tag(tag, repo, obj_fd);
1383 static const struct got_error *
1384 open_tag(struct got_tag_object **tag, struct got_repository *repo,
1385 struct got_object_id *id, int check_cache)
1387 const struct got_error *err = NULL;
1388 struct got_packidx *packidx = NULL;
1389 int idx;
1390 char *path_packfile = NULL;
1391 struct got_object *obj = NULL;
1392 int obj_type = GOT_OBJ_TYPE_ANY;
1394 if (check_cache) {
1395 *tag = got_repo_get_cached_tag(repo, id);
1396 if (*tag != NULL) {
1397 (*tag)->refcnt++;
1398 return NULL;
1400 } else
1401 *tag = NULL;
1403 err = got_repo_search_packidx(&packidx, &idx, repo, id);
1404 if (err == NULL) {
1405 struct got_pack *pack = NULL;
1407 err = get_packfile_path(&path_packfile, packidx);
1408 if (err)
1409 return err;
1411 pack = got_repo_get_cached_pack(repo, path_packfile);
1412 if (pack == NULL) {
1413 err = got_repo_cache_pack(&pack, repo, path_packfile,
1414 packidx);
1415 if (err)
1416 goto done;
1419 /* Beware of "lightweight" tags: Check object type first. */
1420 err = read_packed_object_privsep(&obj, repo, pack, packidx,
1421 idx, id);
1422 if (err)
1423 goto done;
1424 obj_type = obj->type;
1425 got_object_close(obj);
1426 if (obj_type != GOT_OBJ_TYPE_TAG) {
1427 err = got_error(GOT_ERR_OBJ_TYPE);
1428 goto done;
1430 err = read_packed_tag_privsep(tag, pack, packidx, idx, id);
1431 } else if (err->code == GOT_ERR_NO_OBJ) {
1432 int fd;
1434 err = open_loose_object(&fd, id, repo);
1435 if (err)
1436 return err;
1437 err = read_object_header_privsep(&obj, repo, fd);
1438 if (err)
1439 return err;
1440 obj_type = obj->type;
1441 got_object_close(obj);
1442 if (obj_type != GOT_OBJ_TYPE_TAG)
1443 return got_error(GOT_ERR_OBJ_TYPE);
1445 err = open_loose_object(&fd, id, repo);
1446 if (err)
1447 return err;
1448 err = read_tag_privsep(tag, fd, repo);
1451 if (err == NULL) {
1452 (*tag)->refcnt++;
1453 err = got_repo_cache_tag(repo, id, *tag);
1455 done:
1456 free(path_packfile);
1457 return err;
1460 const struct got_error *
1461 got_object_open_as_tag(struct got_tag_object **tag,
1462 struct got_repository *repo, struct got_object_id *id)
1464 *tag = got_repo_get_cached_tag(repo, id);
1465 if (*tag != NULL) {
1466 (*tag)->refcnt++;
1467 return NULL;
1470 return open_tag(tag, repo, id, 0);
1473 const struct got_error *
1474 got_object_tag_open(struct got_tag_object **tag,
1475 struct got_repository *repo, struct got_object *obj)
1477 return open_tag(tag, repo, got_object_get_id(obj), 1);
1480 const char *
1481 got_object_tag_get_name(struct got_tag_object *tag)
1483 return tag->tag;
1486 int
1487 got_object_tag_get_object_type(struct got_tag_object *tag)
1489 return tag->obj_type;
1492 struct got_object_id *
1493 got_object_tag_get_object_id(struct got_tag_object *tag)
1495 return &tag->id;
1498 time_t
1499 got_object_tag_get_tagger_time(struct got_tag_object *tag)
1501 return tag->tagger_time;
1504 time_t
1505 got_object_tag_get_tagger_gmtoff(struct got_tag_object *tag)
1507 return tag->tagger_gmtoff;
1510 const char *
1511 got_object_tag_get_tagger(struct got_tag_object *tag)
1513 return tag->tagger;
1516 const char *
1517 got_object_tag_get_message(struct got_tag_object *tag)
1519 return tag->tagmsg;
1522 static struct got_tree_entry *
1523 find_entry_by_name(struct got_tree_object *tree, const char *name, size_t len)
1525 int i;
1527 /* Note that tree entries are sorted in strncmp() order. */
1528 for (i = 0; i < tree->nentries; i++) {
1529 struct got_tree_entry *te = &tree->entries[i];
1530 int cmp = strncmp(te->name, name, len);
1531 if (cmp < 0)
1532 continue;
1533 if (cmp > 0)
1534 break;
1535 if (te->name[len] == '\0')
1536 return te;
1538 return NULL;
1541 struct got_tree_entry *
1542 got_object_tree_find_entry(struct got_tree_object *tree, const char *name)
1544 return find_entry_by_name(tree, name, strlen(name));
1547 const struct got_error *
1548 got_object_id_by_path(struct got_object_id **id, struct got_repository *repo,
1549 struct got_object_id *commit_id, const char *path)
1551 const struct got_error *err = NULL;
1552 struct got_commit_object *commit = NULL;
1553 struct got_tree_object *tree = NULL;
1554 struct got_tree_entry *te = NULL;
1555 const char *seg, *s;
1556 size_t seglen;
1558 *id = NULL;
1560 err = got_object_open_as_commit(&commit, repo, commit_id);
1561 if (err)
1562 goto done;
1564 /* Handle opening of root of commit's tree. */
1565 if (got_path_is_root_dir(path)) {
1566 *id = got_object_id_dup(commit->tree_id);
1567 if (*id == NULL)
1568 err = got_error_from_errno("got_object_id_dup");
1569 goto done;
1572 err = got_object_open_as_tree(&tree, repo, commit->tree_id);
1573 if (err)
1574 goto done;
1576 s = path;
1577 while (s[0] == '/')
1578 s++;
1579 seg = s;
1580 seglen = 0;
1581 while (*s) {
1582 struct got_tree_object *next_tree;
1584 if (*s != '/') {
1585 s++;
1586 seglen++;
1587 if (*s)
1588 continue;
1591 te = find_entry_by_name(tree, seg, seglen);
1592 if (te == NULL) {
1593 err = got_error(GOT_ERR_NO_TREE_ENTRY);
1594 goto done;
1597 if (*s == '\0')
1598 break;
1600 seg = s + 1;
1601 seglen = 0;
1602 s++;
1603 if (*s) {
1604 err = got_object_open_as_tree(&next_tree, repo,
1605 &te->id);
1606 te = NULL;
1607 if (err)
1608 goto done;
1609 got_object_tree_close(tree);
1610 tree = next_tree;
1614 if (te) {
1615 *id = got_object_id_dup(&te->id);
1616 if (*id == NULL)
1617 return got_error_from_errno("got_object_id_dup");
1618 } else
1619 err = got_error(GOT_ERR_NO_TREE_ENTRY);
1620 done:
1621 if (commit)
1622 got_object_commit_close(commit);
1623 if (tree)
1624 got_object_tree_close(tree);
1625 return err;
1628 const struct got_error *
1629 got_object_tree_path_changed(int *changed,
1630 struct got_tree_object *tree01, struct got_tree_object *tree02,
1631 const char *path, struct got_repository *repo)
1633 const struct got_error *err = NULL;
1634 struct got_tree_object *tree1 = NULL, *tree2 = NULL;
1635 struct got_tree_entry *te1 = NULL, *te2 = NULL;
1636 const char *seg, *s;
1637 size_t seglen;
1639 *changed = 0;
1641 /* We not do support comparing the root path. */
1642 if (got_path_is_root_dir(path))
1643 return got_error_path(path, GOT_ERR_BAD_PATH);
1645 tree1 = tree01;
1646 tree2 = tree02;
1647 s = path;
1648 while (*s == '/')
1649 s++;
1650 seg = s;
1651 seglen = 0;
1652 while (*s) {
1653 struct got_tree_object *next_tree1, *next_tree2;
1655 if (*s != '/') {
1656 s++;
1657 seglen++;
1658 if (*s)
1659 continue;
1662 te1 = find_entry_by_name(tree1, seg, seglen);
1663 if (te1 == NULL) {
1664 err = got_error(GOT_ERR_NO_OBJ);
1665 goto done;
1668 te2 = find_entry_by_name(tree2, seg, seglen);
1669 if (te2 == NULL) {
1670 *changed = 1;
1671 goto done;
1674 if (te1->mode != te2->mode) {
1675 *changed = 1;
1676 goto done;
1679 if (got_object_id_cmp(&te1->id, &te2->id) == 0) {
1680 *changed = 0;
1681 goto done;
1684 if (*s == '\0') { /* final path element */
1685 *changed = 1;
1686 goto done;
1689 seg = s + 1;
1690 s++;
1691 seglen = 0;
1692 if (*s) {
1693 err = got_object_open_as_tree(&next_tree1, repo,
1694 &te1->id);
1695 te1 = NULL;
1696 if (err)
1697 goto done;
1698 if (tree1 != tree01)
1699 got_object_tree_close(tree1);
1700 tree1 = next_tree1;
1702 err = got_object_open_as_tree(&next_tree2, repo,
1703 &te2->id);
1704 te2 = NULL;
1705 if (err)
1706 goto done;
1707 if (tree2 != tree02)
1708 got_object_tree_close(tree2);
1709 tree2 = next_tree2;
1712 done:
1713 if (tree1 && tree1 != tree01)
1714 got_object_tree_close(tree1);
1715 if (tree2 && tree2 != tree02)
1716 got_object_tree_close(tree2);
1717 return err;
1720 const struct got_error *
1721 got_object_tree_entry_dup(struct got_tree_entry **new_te,
1722 struct got_tree_entry *te)
1724 const struct got_error *err = NULL;
1726 *new_te = calloc(1, sizeof(**new_te));
1727 if (*new_te == NULL)
1728 return got_error_from_errno("calloc");
1730 (*new_te)->mode = te->mode;
1731 memcpy((*new_te)->name, te->name, sizeof((*new_te)->name));
1732 memcpy(&(*new_te)->id, &te->id, sizeof((*new_te)->id));
1733 return err;
1736 int
1737 got_object_tree_entry_is_submodule(struct got_tree_entry *te)
1739 return (te->mode & S_IFMT) == (S_IFDIR | S_IFLNK);
1742 const struct got_error *
1743 got_traverse_packed_commits(struct got_object_id_queue *traversed_commits,
1744 struct got_object_id *commit_id, const char *path,
1745 struct got_repository *repo)
1747 const struct got_error *err = NULL;
1748 struct got_pack *pack = NULL;
1749 struct got_packidx *packidx = NULL;
1750 char *path_packfile = NULL;
1751 struct got_commit_object *changed_commit = NULL;
1752 struct got_object_id *changed_commit_id = NULL;
1753 int idx;
1755 err = got_repo_search_packidx(&packidx, &idx, repo, commit_id);
1756 if (err) {
1757 if (err->code != GOT_ERR_NO_OBJ)
1758 return err;
1759 return NULL;
1762 err = get_packfile_path(&path_packfile, packidx);
1763 if (err)
1764 return err;
1766 pack = got_repo_get_cached_pack(repo, path_packfile);
1767 if (pack == NULL) {
1768 err = got_repo_cache_pack(&pack, repo, path_packfile, packidx);
1769 if (err)
1770 goto done;
1773 if (pack->privsep_child == NULL) {
1774 err = start_pack_privsep_child(pack, packidx);
1775 if (err)
1776 goto done;
1779 err = got_privsep_send_commit_traversal_request(
1780 pack->privsep_child->ibuf, commit_id, idx, path);
1781 if (err)
1782 goto done;
1784 err = got_privsep_recv_traversed_commits(&changed_commit,
1785 &changed_commit_id, traversed_commits, pack->privsep_child->ibuf);
1786 if (err)
1787 goto done;
1789 if (changed_commit) {
1791 * Cache the commit in which the path was changed.
1792 * This commit might be opened again soon.
1794 changed_commit->refcnt++;
1795 err = got_repo_cache_commit(repo, changed_commit_id,
1796 changed_commit);
1797 got_object_commit_close(changed_commit);
1799 done:
1800 free(path_packfile);
1801 free(changed_commit_id);
1802 return err;