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_get_id(struct got_object *obj)
62 {
63 return &obj->id;
64 }
66 const struct got_error *
67 got_object_get_id_str(char **outbuf, struct got_object *obj)
68 {
69 return got_object_id_str(outbuf, &obj->id);
70 }
72 const struct got_error *
73 got_object_get_type(int *type, struct got_repository *repo,
74 struct got_object_id *id)
75 {
76 const struct got_error *err = NULL;
77 struct got_object *obj;
79 err = got_object_open(&obj, repo, id);
80 if (err)
81 return err;
83 switch (obj->type) {
84 case GOT_OBJ_TYPE_COMMIT:
85 case GOT_OBJ_TYPE_TREE:
86 case GOT_OBJ_TYPE_BLOB:
87 case GOT_OBJ_TYPE_TAG:
88 *type = obj->type;
89 break;
90 default:
91 err = got_error(GOT_ERR_OBJ_TYPE);
92 break;
93 }
95 got_object_close(obj);
96 return err;
97 }
99 const struct got_error *
100 got_object_get_path(char **path, struct got_object_id *id,
101 struct got_repository *repo)
103 const struct got_error *err = NULL;
104 char *hex = NULL;
105 char *path_objects;
107 *path = NULL;
109 path_objects = got_repo_get_path_objects(repo);
110 if (path_objects == NULL)
111 return got_error_from_errno("got_repo_get_path_objects");
113 err = got_object_id_str(&hex, id);
114 if (err)
115 goto done;
117 if (asprintf(path, "%s/%.2x/%s", path_objects,
118 id->sha1[0], hex + 2) == -1)
119 err = got_error_from_errno("asprintf");
121 done:
122 free(hex);
123 free(path_objects);
124 return err;
127 static const struct got_error *
128 open_loose_object(int *fd, struct got_object_id *id,
129 struct got_repository *repo)
131 const struct got_error *err = NULL;
132 char *path;
134 err = got_object_get_path(&path, id, repo);
135 if (err)
136 return err;
137 *fd = open(path, O_RDONLY | O_NOFOLLOW);
138 if (*fd == -1) {
139 err = got_error_from_errno2("open", path);
140 goto done;
142 done:
143 free(path);
144 return err;
147 static const struct got_error *
148 get_packfile_path(char **path_packfile, struct got_packidx *packidx)
150 size_t size;
152 /* Packfile path contains ".pack" instead of ".idx", so add one byte. */
153 size = strlen(packidx->path_packidx) + 2;
154 if (size < GOT_PACKFILE_NAMELEN + 1)
155 return got_error(GOT_ERR_BAD_PATH);
157 *path_packfile = malloc(size);
158 if (*path_packfile == NULL)
159 return got_error_from_errno("malloc");
161 /* Copy up to and excluding ".idx". */
162 if (strlcpy(*path_packfile, packidx->path_packidx,
163 size - strlen(GOT_PACKIDX_SUFFIX) - 1) >= size)
164 return got_error(GOT_ERR_NO_SPACE);
166 if (strlcat(*path_packfile, GOT_PACKFILE_SUFFIX, size) >= size)
167 return got_error(GOT_ERR_NO_SPACE);
169 return NULL;
172 static const struct got_error *
173 request_packed_object(struct got_object **obj, struct got_pack *pack, int idx,
174 struct got_object_id *id)
176 const struct got_error *err = NULL;
177 struct imsgbuf *ibuf = pack->privsep_child->ibuf;
179 err = got_privsep_send_packed_obj_req(ibuf, idx, id);
180 if (err)
181 return err;
183 err = got_privsep_recv_obj(obj, ibuf);
184 if (err)
185 return err;
187 memcpy(&(*obj)->id, id, sizeof((*obj)->id));
189 return NULL;
192 static void
193 set_max_datasize(void)
195 struct rlimit rl;
197 if (getrlimit(RLIMIT_DATA, &rl) != 0)
198 return;
200 rl.rlim_cur = rl.rlim_max;
201 setrlimit(RLIMIT_DATA, &rl);
204 static const struct got_error *
205 start_pack_privsep_child(struct got_pack *pack, struct got_packidx *packidx)
207 const struct got_error *err = NULL;
208 int imsg_fds[2];
209 pid_t pid;
210 struct imsgbuf *ibuf;
212 ibuf = calloc(1, sizeof(*ibuf));
213 if (ibuf == NULL)
214 return got_error_from_errno("calloc");
216 pack->privsep_child = calloc(1, sizeof(*pack->privsep_child));
217 if (pack->privsep_child == NULL) {
218 err = got_error_from_errno("calloc");
219 free(ibuf);
220 return err;
223 if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_fds) == -1) {
224 err = got_error_from_errno("socketpair");
225 goto done;
228 pid = fork();
229 if (pid == -1) {
230 err = got_error_from_errno("fork");
231 goto done;
232 } else if (pid == 0) {
233 set_max_datasize();
234 got_privsep_exec_child(imsg_fds, GOT_PATH_PROG_READ_PACK,
235 pack->path_packfile);
236 /* not reached */
239 if (close(imsg_fds[1]) != 0)
240 return got_error_from_errno("close");
241 pack->privsep_child->imsg_fd = imsg_fds[0];
242 pack->privsep_child->pid = pid;
243 imsg_init(ibuf, imsg_fds[0]);
244 pack->privsep_child->ibuf = ibuf;
246 err = got_privsep_init_pack_child(ibuf, pack, packidx);
247 if (err) {
248 const struct got_error *child_err;
249 err = got_privsep_send_stop(pack->privsep_child->imsg_fd);
250 child_err = got_privsep_wait_for_child(
251 pack->privsep_child->pid);
252 if (child_err && err == NULL)
253 err = child_err;
255 done:
256 if (err) {
257 free(ibuf);
258 free(pack->privsep_child);
259 pack->privsep_child = NULL;
261 return err;
264 static const struct got_error *
265 read_packed_object_privsep(struct got_object **obj,
266 struct got_repository *repo, struct got_pack *pack,
267 struct got_packidx *packidx, int idx, struct got_object_id *id)
269 const struct got_error *err = NULL;
271 if (pack->privsep_child)
272 return request_packed_object(obj, pack, idx, id);
274 err = start_pack_privsep_child(pack, packidx);
275 if (err)
276 return err;
278 return request_packed_object(obj, pack, idx, id);
282 static const struct got_error *
283 open_packed_object(struct got_object **obj, struct got_object_id *id,
284 struct got_repository *repo)
286 const struct got_error *err = NULL;
287 struct got_pack *pack = NULL;
288 struct got_packidx *packidx = NULL;
289 int idx;
290 char *path_packfile;
292 err = got_repo_search_packidx(&packidx, &idx, repo, id);
293 if (err)
294 return err;
296 err = get_packfile_path(&path_packfile, packidx);
297 if (err)
298 return err;
300 pack = got_repo_get_cached_pack(repo, path_packfile);
301 if (pack == NULL) {
302 err = got_repo_cache_pack(&pack, repo, path_packfile, packidx);
303 if (err)
304 goto done;
307 err = read_packed_object_privsep(obj, repo, pack, packidx, idx, id);
308 if (err)
309 goto done;
311 err = got_repo_cache_pack(NULL, repo, path_packfile, packidx);
312 done:
313 free(path_packfile);
314 return err;
317 static const struct got_error *
318 request_object(struct got_object **obj, struct got_repository *repo, int fd)
320 const struct got_error *err = NULL;
321 struct imsgbuf *ibuf;
323 ibuf = repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_OBJECT].ibuf;
325 err = got_privsep_send_obj_req(ibuf, fd);
326 if (err)
327 return err;
329 return got_privsep_recv_obj(obj, ibuf);
332 static const struct got_error *
333 read_object_header_privsep(struct got_object **obj, struct got_repository *repo,
334 int obj_fd)
336 const struct got_error *err;
337 int imsg_fds[2];
338 pid_t pid;
339 struct imsgbuf *ibuf;
341 if (repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_OBJECT].imsg_fd != -1)
342 return request_object(obj, repo, obj_fd);
344 ibuf = calloc(1, sizeof(*ibuf));
345 if (ibuf == NULL)
346 return got_error_from_errno("calloc");
348 if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_fds) == -1) {
349 err = got_error_from_errno("socketpair");
350 free(ibuf);
351 return err;
354 pid = fork();
355 if (pid == -1) {
356 err = got_error_from_errno("fork");
357 free(ibuf);
358 return err;
360 else if (pid == 0) {
361 got_privsep_exec_child(imsg_fds, GOT_PATH_PROG_READ_OBJECT,
362 repo->path);
363 /* not reached */
366 if (close(imsg_fds[1]) != 0) {
367 err = got_error_from_errno("close");
368 free(ibuf);
369 return err;
371 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_OBJECT].imsg_fd =
372 imsg_fds[0];
373 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_OBJECT].pid = pid;
374 imsg_init(ibuf, imsg_fds[0]);
375 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_OBJECT].ibuf = ibuf;
377 return request_object(obj, repo, obj_fd);
381 const struct got_error *
382 got_object_open(struct got_object **obj, struct got_repository *repo,
383 struct got_object_id *id)
385 const struct got_error *err = NULL;
386 char *path;
387 int fd;
389 *obj = got_repo_get_cached_object(repo, id);
390 if (*obj != NULL) {
391 (*obj)->refcnt++;
392 return NULL;
395 err = open_packed_object(obj, id, repo);
396 if (err && err->code != GOT_ERR_NO_OBJ)
397 return err;
398 if (*obj) {
399 (*obj)->refcnt++;
400 return got_repo_cache_object(repo, id, *obj);
403 err = got_object_get_path(&path, id, repo);
404 if (err)
405 return err;
407 fd = open(path, O_RDONLY | O_NOFOLLOW);
408 if (fd == -1) {
409 if (errno == ENOENT)
410 err = got_error_no_obj(id);
411 else
412 err = got_error_from_errno2("open", path);
413 goto done;
414 } else {
415 err = read_object_header_privsep(obj, repo, fd);
416 if (err)
417 goto done;
418 memcpy((*obj)->id.sha1, id->sha1, SHA1_DIGEST_LENGTH);
421 (*obj)->refcnt++;
422 err = got_repo_cache_object(repo, id, *obj);
423 done:
424 free(path);
425 return err;
429 const struct got_error *
430 got_object_open_by_id_str(struct got_object **obj, struct got_repository *repo,
431 const char *id_str)
433 struct got_object_id id;
435 if (!got_parse_sha1_digest(id.sha1, id_str))
436 return got_error_path(id_str, GOT_ERR_BAD_OBJ_ID_STR);
438 return got_object_open(obj, repo, &id);
441 const struct got_error *
442 got_object_resolve_id_str(struct got_object_id **id,
443 struct got_repository *repo, const char *id_str)
445 const struct got_error *err = NULL;
446 struct got_object *obj;
448 err = got_object_open_by_id_str(&obj, repo, id_str);
449 if (err)
450 return err;
452 *id = got_object_id_dup(got_object_get_id(obj));
453 got_object_close(obj);
454 if (*id == NULL)
455 return got_error_from_errno("got_object_id_dup");
457 return NULL;
460 static const struct got_error *
461 request_packed_commit(struct got_commit_object **commit, struct got_pack *pack,
462 int pack_idx, struct got_object_id *id)
464 const struct got_error *err = NULL;
466 err = got_privsep_send_commit_req(pack->privsep_child->ibuf, -1, id,
467 pack_idx);
468 if (err)
469 return err;
471 err = got_privsep_recv_commit(commit, pack->privsep_child->ibuf);
472 if (err)
473 return err;
475 (*commit)->flags |= GOT_COMMIT_FLAG_PACKED;
476 return NULL;
479 static const struct got_error *
480 read_packed_commit_privsep(struct got_commit_object **commit,
481 struct got_pack *pack, struct got_packidx *packidx, int idx,
482 struct got_object_id *id)
484 const struct got_error *err = NULL;
486 if (pack->privsep_child)
487 return request_packed_commit(commit, pack, idx, id);
489 err = start_pack_privsep_child(pack, packidx);
490 if (err)
491 return err;
493 return request_packed_commit(commit, pack, idx, id);
496 static const struct got_error *
497 request_commit(struct got_commit_object **commit, struct got_repository *repo,
498 int fd)
500 const struct got_error *err = NULL;
501 struct imsgbuf *ibuf;
503 ibuf = repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_COMMIT].ibuf;
505 err = got_privsep_send_commit_req(ibuf, fd, NULL, -1);
506 if (err)
507 return err;
509 return got_privsep_recv_commit(commit, ibuf);
512 static const struct got_error *
513 read_commit_privsep(struct got_commit_object **commit, int obj_fd,
514 struct got_repository *repo)
516 const struct got_error *err;
517 int imsg_fds[2];
518 pid_t pid;
519 struct imsgbuf *ibuf;
521 if (repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_COMMIT].imsg_fd != -1)
522 return request_commit(commit, repo, obj_fd);
524 ibuf = calloc(1, sizeof(*ibuf));
525 if (ibuf == NULL)
526 return got_error_from_errno("calloc");
528 if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_fds) == -1) {
529 err = got_error_from_errno("socketpair");
530 free(ibuf);
531 return err;
534 pid = fork();
535 if (pid == -1) {
536 err = got_error_from_errno("fork");
537 free(ibuf);
538 return err;
540 else if (pid == 0) {
541 got_privsep_exec_child(imsg_fds, GOT_PATH_PROG_READ_COMMIT,
542 repo->path);
543 /* not reached */
546 if (close(imsg_fds[1]) != 0) {
547 err = got_error_from_errno("close");
548 free(ibuf);
549 return err;
551 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_COMMIT].imsg_fd =
552 imsg_fds[0];
553 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_COMMIT].pid = pid;
554 imsg_init(ibuf, imsg_fds[0]);
555 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_COMMIT].ibuf = ibuf;
557 return request_commit(commit, repo, obj_fd);
561 static const struct got_error *
562 open_commit(struct got_commit_object **commit,
563 struct got_repository *repo, struct got_object_id *id, int check_cache)
565 const struct got_error *err = NULL;
566 struct got_packidx *packidx = NULL;
567 int idx;
568 char *path_packfile = NULL;
570 if (check_cache) {
571 *commit = got_repo_get_cached_commit(repo, id);
572 if (*commit != NULL) {
573 (*commit)->refcnt++;
574 return NULL;
576 } else
577 *commit = NULL;
579 err = got_repo_search_packidx(&packidx, &idx, repo, id);
580 if (err == NULL) {
581 struct got_pack *pack = NULL;
583 err = get_packfile_path(&path_packfile, packidx);
584 if (err)
585 return err;
587 pack = got_repo_get_cached_pack(repo, path_packfile);
588 if (pack == NULL) {
589 err = got_repo_cache_pack(&pack, repo, path_packfile,
590 packidx);
591 if (err)
592 goto done;
594 err = read_packed_commit_privsep(commit, pack,
595 packidx, idx, id);
596 } else if (err->code == GOT_ERR_NO_OBJ) {
597 int fd;
599 err = open_loose_object(&fd, id, repo);
600 if (err)
601 return err;
602 err = read_commit_privsep(commit, fd, repo);
605 if (err == NULL) {
606 (*commit)->refcnt++;
607 err = got_repo_cache_commit(repo, id, *commit);
609 done:
610 free(path_packfile);
611 return err;
614 const struct got_error *
615 got_object_open_as_commit(struct got_commit_object **commit,
616 struct got_repository *repo, struct got_object_id *id)
618 *commit = got_repo_get_cached_commit(repo, id);
619 if (*commit != NULL) {
620 (*commit)->refcnt++;
621 return NULL;
624 return open_commit(commit, repo, id, 0);
627 const struct got_error *
628 got_object_commit_open(struct got_commit_object **commit,
629 struct got_repository *repo, struct got_object *obj)
631 return open_commit(commit, repo, got_object_get_id(obj), 1);
634 const struct got_error *
635 got_object_qid_alloc(struct got_object_qid **qid, struct got_object_id *id)
637 const struct got_error *err = NULL;
639 *qid = calloc(1, sizeof(**qid));
640 if (*qid == NULL)
641 return got_error_from_errno("calloc");
643 (*qid)->id = got_object_id_dup(id);
644 if ((*qid)->id == NULL) {
645 err = got_error_from_errno("got_object_id_dup");
646 got_object_qid_free(*qid);
647 *qid = NULL;
648 return err;
651 return NULL;
654 static const struct got_error *
655 request_packed_tree(struct got_tree_object **tree, struct got_pack *pack,
656 int pack_idx, struct got_object_id *id)
658 const struct got_error *err = NULL;
660 err = got_privsep_send_tree_req(pack->privsep_child->ibuf, -1, id,
661 pack_idx);
662 if (err)
663 return err;
665 return got_privsep_recv_tree(tree, pack->privsep_child->ibuf);
668 static const struct got_error *
669 read_packed_tree_privsep(struct got_tree_object **tree,
670 struct got_pack *pack, struct got_packidx *packidx, int idx,
671 struct got_object_id *id)
673 const struct got_error *err = NULL;
675 if (pack->privsep_child)
676 return request_packed_tree(tree, pack, idx, id);
678 err = start_pack_privsep_child(pack, packidx);
679 if (err)
680 return err;
682 return request_packed_tree(tree, pack, idx, id);
685 static const struct got_error *
686 request_tree(struct got_tree_object **tree, struct got_repository *repo,
687 int fd)
689 const struct got_error *err = NULL;
690 struct imsgbuf *ibuf;
692 ibuf = repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TREE].ibuf;
694 err = got_privsep_send_tree_req(ibuf, fd, NULL, -1);
695 if (err)
696 return err;
698 return got_privsep_recv_tree(tree, ibuf);
701 const struct got_error *
702 read_tree_privsep(struct got_tree_object **tree, int obj_fd,
703 struct got_repository *repo)
705 const struct got_error *err;
706 int imsg_fds[2];
707 pid_t pid;
708 struct imsgbuf *ibuf;
710 if (repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TREE].imsg_fd != -1)
711 return request_tree(tree, repo, obj_fd);
713 ibuf = calloc(1, sizeof(*ibuf));
714 if (ibuf == NULL)
715 return got_error_from_errno("calloc");
717 if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_fds) == -1) {
718 err = got_error_from_errno("socketpair");
719 free(ibuf);
720 return err;
723 pid = fork();
724 if (pid == -1) {
725 err = got_error_from_errno("fork");
726 free(ibuf);
727 return err;
729 else if (pid == 0) {
730 got_privsep_exec_child(imsg_fds, GOT_PATH_PROG_READ_TREE,
731 repo->path);
732 /* not reached */
735 if (close(imsg_fds[1]) != 0) {
736 err = got_error_from_errno("close");
737 free(ibuf);
738 return err;
740 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TREE].imsg_fd =
741 imsg_fds[0];
742 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TREE].pid = pid;
743 imsg_init(ibuf, imsg_fds[0]);
744 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TREE].ibuf = ibuf;
747 return request_tree(tree, repo, obj_fd);
750 static const struct got_error *
751 open_tree(struct got_tree_object **tree, struct got_repository *repo,
752 struct got_object_id *id, int check_cache)
754 const struct got_error *err = NULL;
755 struct got_packidx *packidx = NULL;
756 int idx;
757 char *path_packfile = NULL;
759 if (check_cache) {
760 *tree = got_repo_get_cached_tree(repo, id);
761 if (*tree != NULL) {
762 (*tree)->refcnt++;
763 return NULL;
765 } else
766 *tree = NULL;
768 err = got_repo_search_packidx(&packidx, &idx, repo, id);
769 if (err == NULL) {
770 struct got_pack *pack = NULL;
772 err = get_packfile_path(&path_packfile, packidx);
773 if (err)
774 return err;
776 pack = got_repo_get_cached_pack(repo, path_packfile);
777 if (pack == NULL) {
778 err = got_repo_cache_pack(&pack, repo, path_packfile,
779 packidx);
780 if (err)
781 goto done;
783 err = read_packed_tree_privsep(tree, pack,
784 packidx, idx, id);
785 } else if (err->code == GOT_ERR_NO_OBJ) {
786 int fd;
788 err = open_loose_object(&fd, id, repo);
789 if (err)
790 return err;
791 err = read_tree_privsep(tree, fd, repo);
794 if (err == NULL) {
795 (*tree)->refcnt++;
796 err = got_repo_cache_tree(repo, id, *tree);
798 done:
799 free(path_packfile);
800 return err;
803 const struct got_error *
804 got_object_open_as_tree(struct got_tree_object **tree,
805 struct got_repository *repo, struct got_object_id *id)
807 *tree = got_repo_get_cached_tree(repo, id);
808 if (*tree != NULL) {
809 (*tree)->refcnt++;
810 return NULL;
813 return open_tree(tree, repo, id, 0);
816 const struct got_error *
817 got_object_tree_open(struct got_tree_object **tree,
818 struct got_repository *repo, struct got_object *obj)
820 return open_tree(tree, repo, got_object_get_id(obj), 1);
823 int
824 got_object_tree_get_nentries(struct got_tree_object *tree)
826 return tree->nentries;
829 struct got_tree_entry *
830 got_object_tree_get_first_entry(struct got_tree_object *tree)
832 return got_object_tree_get_entry(tree, 0);
835 struct got_tree_entry *
836 got_object_tree_get_last_entry(struct got_tree_object *tree)
838 return got_object_tree_get_entry(tree, tree->nentries - 1);
841 struct got_tree_entry *
842 got_object_tree_get_entry(struct got_tree_object *tree, int i)
844 if (i < 0 || i >= tree->nentries)
845 return NULL;
846 return &tree->entries[i];
849 mode_t
850 got_tree_entry_get_mode(struct got_tree_entry *te)
852 return te->mode;
855 const char *
856 got_tree_entry_get_name(struct got_tree_entry *te)
858 return &te->name[0];
861 struct got_object_id *
862 got_tree_entry_get_id(struct got_tree_entry *te)
864 return &te->id;
867 int
868 got_tree_entry_get_index(struct got_tree_entry *te)
870 return te->idx;
873 struct got_tree_entry *
874 got_tree_entry_get_next(struct got_tree_object *tree,
875 struct got_tree_entry *te)
877 return got_object_tree_get_entry(tree, te->idx + 1);
880 struct got_tree_entry *
881 got_tree_entry_get_prev(struct got_tree_object *tree,
882 struct got_tree_entry *te)
884 return got_object_tree_get_entry(tree, te->idx - 1);
887 static const struct got_error *
888 request_packed_blob(uint8_t **outbuf, size_t *size, size_t *hdrlen, int outfd,
889 struct got_pack *pack, struct got_packidx *packidx, int idx,
890 struct got_object_id *id)
892 const struct got_error *err = NULL;
893 int outfd_child;
894 int basefd, accumfd; /* temporary files for delta application */
896 basefd = got_opentempfd();
897 if (basefd == -1)
898 return got_error_from_errno("got_opentempfd");
899 accumfd = got_opentempfd();
900 if (accumfd == -1)
901 return got_error_from_errno("got_opentempfd");
903 outfd_child = dup(outfd);
904 if (outfd_child == -1)
905 return got_error_from_errno("dup");
907 err = got_privsep_send_blob_req(pack->privsep_child->ibuf, -1, id, idx);
908 if (err)
909 return err;
911 err = got_privsep_send_blob_outfd(pack->privsep_child->ibuf,
912 outfd_child);
913 if (err) {
914 close(basefd);
915 close(accumfd);
916 return err;
919 err = got_privsep_send_tmpfd(pack->privsep_child->ibuf,
920 basefd);
921 if (err) {
922 close(accumfd);
923 return err;
926 err = got_privsep_send_tmpfd(pack->privsep_child->ibuf,
927 accumfd);
928 if (err)
929 return err;
931 err = got_privsep_recv_blob(outbuf, size, hdrlen,
932 pack->privsep_child->ibuf);
933 if (err)
934 return err;
936 if (lseek(outfd, SEEK_SET, 0) == -1)
937 err = got_error_from_errno("lseek");
939 return err;
942 static const struct got_error *
943 read_packed_blob_privsep(uint8_t **outbuf, size_t *size, size_t *hdrlen,
944 int outfd, struct got_pack *pack, struct got_packidx *packidx, int idx,
945 struct got_object_id *id)
947 const struct got_error *err = NULL;
949 if (pack->privsep_child == NULL) {
950 err = start_pack_privsep_child(pack, packidx);
951 if (err)
952 return err;
955 return request_packed_blob(outbuf, size, hdrlen, outfd, pack, packidx,
956 idx, id);
959 static const struct got_error *
960 request_blob(uint8_t **outbuf, size_t *size, size_t *hdrlen, int outfd,
961 int infd, struct imsgbuf *ibuf)
963 const struct got_error *err = NULL;
964 int outfd_child;
966 outfd_child = dup(outfd);
967 if (outfd_child == -1)
968 return got_error_from_errno("dup");
970 err = got_privsep_send_blob_req(ibuf, infd, NULL, -1);
971 if (err)
972 return err;
974 err = got_privsep_send_blob_outfd(ibuf, outfd_child);
975 if (err)
976 return err;
978 err = got_privsep_recv_blob(outbuf, size, hdrlen, ibuf);
979 if (err)
980 return err;
982 if (lseek(outfd, SEEK_SET, 0) == -1)
983 return got_error_from_errno("lseek");
985 return err;
988 static const struct got_error *
989 read_blob_privsep(uint8_t **outbuf, size_t *size, size_t *hdrlen,
990 int outfd, int infd, struct got_repository *repo)
992 const struct got_error *err;
993 int imsg_fds[2];
994 pid_t pid;
995 struct imsgbuf *ibuf;
997 if (repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_BLOB].imsg_fd != -1) {
998 ibuf = repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_BLOB].ibuf;
999 return request_blob(outbuf, size, hdrlen, outfd, infd, ibuf);
1002 ibuf = calloc(1, sizeof(*ibuf));
1003 if (ibuf == NULL)
1004 return got_error_from_errno("calloc");
1006 if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_fds) == -1) {
1007 err = got_error_from_errno("socketpair");
1008 free(ibuf);
1009 return err;
1012 pid = fork();
1013 if (pid == -1) {
1014 err = got_error_from_errno("fork");
1015 free(ibuf);
1016 return err;
1018 else if (pid == 0) {
1019 got_privsep_exec_child(imsg_fds, GOT_PATH_PROG_READ_BLOB,
1020 repo->path);
1021 /* not reached */
1024 if (close(imsg_fds[1]) != 0) {
1025 err = got_error_from_errno("close");
1026 free(ibuf);
1027 return err;
1029 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_BLOB].imsg_fd =
1030 imsg_fds[0];
1031 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_BLOB].pid = pid;
1032 imsg_init(ibuf, imsg_fds[0]);
1033 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_BLOB].ibuf = ibuf;
1035 return request_blob(outbuf, size, hdrlen, outfd, infd, ibuf);
1038 static const struct got_error *
1039 open_blob(struct got_blob_object **blob, struct got_repository *repo,
1040 struct got_object_id *id, size_t blocksize)
1042 const struct got_error *err = NULL;
1043 struct got_packidx *packidx = NULL;
1044 int idx;
1045 char *path_packfile = NULL;
1046 uint8_t *outbuf;
1047 int outfd;
1048 size_t size, hdrlen;
1049 struct stat sb;
1051 *blob = calloc(1, sizeof(**blob));
1052 if (*blob == NULL)
1053 return got_error_from_errno("calloc");
1055 outfd = got_opentempfd();
1056 if (outfd == -1)
1057 return got_error_from_errno("got_opentempfd");
1059 (*blob)->read_buf = malloc(blocksize);
1060 if ((*blob)->read_buf == NULL) {
1061 err = got_error_from_errno("malloc");
1062 goto done;
1065 err = got_repo_search_packidx(&packidx, &idx, repo, id);
1066 if (err == NULL) {
1067 struct got_pack *pack = NULL;
1069 err = get_packfile_path(&path_packfile, packidx);
1070 if (err)
1071 goto done;
1073 pack = got_repo_get_cached_pack(repo, path_packfile);
1074 if (pack == NULL) {
1075 err = got_repo_cache_pack(&pack, repo, path_packfile,
1076 packidx);
1077 if (err)
1078 goto done;
1080 err = read_packed_blob_privsep(&outbuf, &size, &hdrlen, outfd,
1081 pack, packidx, idx, id);
1082 } else if (err->code == GOT_ERR_NO_OBJ) {
1083 int infd;
1085 err = open_loose_object(&infd, id, repo);
1086 if (err)
1087 goto done;
1088 err = read_blob_privsep(&outbuf, &size, &hdrlen, outfd, infd,
1089 repo);
1091 if (err)
1092 goto done;
1094 if (hdrlen > size) {
1095 err = got_error(GOT_ERR_BAD_OBJ_HDR);
1096 goto done;
1099 if (outbuf) {
1100 if (close(outfd) != 0 && err == NULL)
1101 err = got_error_from_errno("close");
1102 outfd = -1;
1103 (*blob)->f = fmemopen(outbuf, size, "rb");
1104 if ((*blob)->f == NULL) {
1105 err = got_error_from_errno("fmemopen");
1106 free(outbuf);
1107 goto done;
1109 (*blob)->data = outbuf;
1110 } else {
1111 if (fstat(outfd, &sb) == -1) {
1112 err = got_error_from_errno("fstat");
1113 goto done;
1116 if (sb.st_size != size) {
1117 err = got_error(GOT_ERR_PRIVSEP_LEN);
1118 goto done;
1121 (*blob)->f = fdopen(outfd, "rb");
1122 if ((*blob)->f == NULL) {
1123 err = got_error_from_errno("fdopen");
1124 close(outfd);
1125 outfd = -1;
1126 goto done;
1130 (*blob)->hdrlen = hdrlen;
1131 (*blob)->blocksize = blocksize;
1132 memcpy(&(*blob)->id.sha1, id->sha1, SHA1_DIGEST_LENGTH);
1134 done:
1135 free(path_packfile);
1136 if (err) {
1137 if (*blob) {
1138 got_object_blob_close(*blob);
1139 *blob = NULL;
1140 } else if (outfd != -1)
1141 close(outfd);
1143 return err;
1146 const struct got_error *
1147 got_object_open_as_blob(struct got_blob_object **blob,
1148 struct got_repository *repo, struct got_object_id *id,
1149 size_t blocksize)
1151 return open_blob(blob, repo, id, blocksize);
1154 const struct got_error *
1155 got_object_blob_open(struct got_blob_object **blob,
1156 struct got_repository *repo, struct got_object *obj, size_t blocksize)
1158 return open_blob(blob, repo, got_object_get_id(obj), blocksize);
1161 const struct got_error *
1162 got_object_blob_close(struct got_blob_object *blob)
1164 const struct got_error *err = NULL;
1165 free(blob->read_buf);
1166 if (blob->f && fclose(blob->f) != 0)
1167 err = got_error_from_errno("fclose");
1168 free(blob->data);
1169 free(blob);
1170 return err;
1173 char *
1174 got_object_blob_id_str(struct got_blob_object *blob, char *buf, size_t size)
1176 return got_sha1_digest_to_str(blob->id.sha1, buf, size);
1179 size_t
1180 got_object_blob_get_hdrlen(struct got_blob_object *blob)
1182 return blob->hdrlen;
1185 const uint8_t *
1186 got_object_blob_get_read_buf(struct got_blob_object *blob)
1188 return blob->read_buf;
1191 const struct got_error *
1192 got_object_blob_read_block(size_t *outlenp, struct got_blob_object *blob)
1194 size_t n;
1196 n = fread(blob->read_buf, 1, blob->blocksize, blob->f);
1197 if (n == 0 && ferror(blob->f))
1198 return got_ferror(blob->f, GOT_ERR_IO);
1199 *outlenp = n;
1200 return NULL;
1203 const struct got_error *
1204 got_object_blob_dump_to_file(size_t *filesize, int *nlines,
1205 off_t **line_offsets, FILE *outfile, struct got_blob_object *blob)
1207 const struct got_error *err = NULL;
1208 size_t n, len, hdrlen;
1209 const uint8_t *buf;
1210 int i;
1211 size_t noffsets = 0;
1212 off_t off = 0, total_len = 0;
1214 if (line_offsets)
1215 *line_offsets = NULL;
1216 if (filesize)
1217 *filesize = 0;
1218 if (nlines)
1219 *nlines = 0;
1221 hdrlen = got_object_blob_get_hdrlen(blob);
1222 do {
1223 err = got_object_blob_read_block(&len, blob);
1224 if (err)
1225 return err;
1226 if (len == 0)
1227 break;
1228 buf = got_object_blob_get_read_buf(blob);
1229 i = hdrlen;
1230 if (line_offsets && nlines) {
1231 if (*line_offsets == NULL) {
1232 /* Have some data but perhaps no '\n'. */
1233 noffsets = 1;
1234 *nlines = 1;
1235 *line_offsets = calloc(1, sizeof(**line_offsets));
1236 if (*line_offsets == NULL)
1237 return got_error_from_errno("malloc");
1239 /* Skip forward over end of first line. */
1240 while (i < len) {
1241 if (buf[i] == '\n')
1242 break;
1243 i++;
1246 /* Scan '\n' offsets in remaining chunk of data. */
1247 while (i < len) {
1248 if (buf[i] != '\n') {
1249 i++;
1250 continue;
1252 (*nlines)++;
1253 if (noffsets < *nlines) {
1254 off_t *o = recallocarray(*line_offsets,
1255 noffsets, *nlines,
1256 sizeof(**line_offsets));
1257 if (o == NULL) {
1258 free(*line_offsets);
1259 *line_offsets = NULL;
1260 return got_error_from_errno(
1261 "recallocarray");
1263 *line_offsets = o;
1264 noffsets = *nlines;
1266 off = total_len + i - hdrlen + 1;
1267 (*line_offsets)[*nlines - 1] = off;
1268 i++;
1271 /* Skip blob object header first time around. */
1272 n = fwrite(buf + hdrlen, 1, len - hdrlen, outfile);
1273 if (n != len - hdrlen)
1274 return got_ferror(outfile, GOT_ERR_IO);
1275 total_len += len - hdrlen;
1276 hdrlen = 0;
1277 } while (len != 0);
1279 if (fflush(outfile) != 0)
1280 return got_error_from_errno("fflush");
1281 rewind(outfile);
1283 if (filesize)
1284 *filesize = total_len;
1286 return NULL;
1289 static const struct got_error *
1290 request_packed_tag(struct got_tag_object **tag, struct got_pack *pack,
1291 int pack_idx, struct got_object_id *id)
1293 const struct got_error *err = NULL;
1295 err = got_privsep_send_tag_req(pack->privsep_child->ibuf, -1, id,
1296 pack_idx);
1297 if (err)
1298 return err;
1300 return got_privsep_recv_tag(tag, pack->privsep_child->ibuf);
1303 static const struct got_error *
1304 read_packed_tag_privsep(struct got_tag_object **tag,
1305 struct got_pack *pack, struct got_packidx *packidx, int idx,
1306 struct got_object_id *id)
1308 const struct got_error *err = NULL;
1310 if (pack->privsep_child)
1311 return request_packed_tag(tag, pack, idx, id);
1313 err = start_pack_privsep_child(pack, packidx);
1314 if (err)
1315 return err;
1317 return request_packed_tag(tag, pack, idx, id);
1320 static const struct got_error *
1321 request_tag(struct got_tag_object **tag, struct got_repository *repo,
1322 int fd)
1324 const struct got_error *err = NULL;
1325 struct imsgbuf *ibuf;
1327 ibuf = repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TAG].ibuf;
1329 err = got_privsep_send_tag_req(ibuf, fd, NULL, -1);
1330 if (err)
1331 return err;
1333 return got_privsep_recv_tag(tag, ibuf);
1336 static const struct got_error *
1337 read_tag_privsep(struct got_tag_object **tag, int obj_fd,
1338 struct got_repository *repo)
1340 const struct got_error *err;
1341 int imsg_fds[2];
1342 pid_t pid;
1343 struct imsgbuf *ibuf;
1345 if (repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TAG].imsg_fd != -1)
1346 return request_tag(tag, repo, obj_fd);
1348 ibuf = calloc(1, sizeof(*ibuf));
1349 if (ibuf == NULL)
1350 return got_error_from_errno("calloc");
1352 if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_fds) == -1) {
1353 err = got_error_from_errno("socketpair");
1354 free(ibuf);
1355 return err;
1358 pid = fork();
1359 if (pid == -1) {
1360 err = got_error_from_errno("fork");
1361 free(ibuf);
1362 return err;
1364 else if (pid == 0) {
1365 got_privsep_exec_child(imsg_fds, GOT_PATH_PROG_READ_TAG,
1366 repo->path);
1367 /* not reached */
1370 if (close(imsg_fds[1]) != 0) {
1371 err = got_error_from_errno("close");
1372 free(ibuf);
1373 return err;
1375 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TAG].imsg_fd =
1376 imsg_fds[0];
1377 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TAG].pid = pid;
1378 imsg_init(ibuf, imsg_fds[0]);
1379 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TAG].ibuf = ibuf;
1381 return request_tag(tag, repo, obj_fd);
1384 static const struct got_error *
1385 open_tag(struct got_tag_object **tag, struct got_repository *repo,
1386 struct got_object_id *id, int check_cache)
1388 const struct got_error *err = NULL;
1389 struct got_packidx *packidx = NULL;
1390 int idx;
1391 char *path_packfile = NULL;
1392 struct got_object *obj = NULL;
1393 int obj_type = GOT_OBJ_TYPE_ANY;
1395 if (check_cache) {
1396 *tag = got_repo_get_cached_tag(repo, id);
1397 if (*tag != NULL) {
1398 (*tag)->refcnt++;
1399 return NULL;
1401 } else
1402 *tag = NULL;
1404 err = got_repo_search_packidx(&packidx, &idx, repo, id);
1405 if (err == NULL) {
1406 struct got_pack *pack = NULL;
1408 err = get_packfile_path(&path_packfile, packidx);
1409 if (err)
1410 return err;
1412 pack = got_repo_get_cached_pack(repo, path_packfile);
1413 if (pack == NULL) {
1414 err = got_repo_cache_pack(&pack, repo, path_packfile,
1415 packidx);
1416 if (err)
1417 goto done;
1420 /* Beware of "leightweight" tags: Check object type first. */
1421 err = read_packed_object_privsep(&obj, repo, pack, packidx,
1422 idx, id);
1423 if (err)
1424 goto done;
1425 obj_type = obj->type;
1426 got_object_close(obj);
1427 if (obj_type != GOT_OBJ_TYPE_TAG) {
1428 err = got_error(GOT_ERR_OBJ_TYPE);
1429 goto done;
1431 err = read_packed_tag_privsep(tag, pack, packidx, idx, id);
1432 } else if (err->code == GOT_ERR_NO_OBJ) {
1433 int fd;
1435 err = open_loose_object(&fd, id, repo);
1436 if (err)
1437 return err;
1438 err = read_object_header_privsep(&obj, repo, fd);
1439 if (err)
1440 return err;
1441 obj_type = obj->type;
1442 got_object_close(obj);
1443 if (obj_type != GOT_OBJ_TYPE_TAG)
1444 return got_error(GOT_ERR_OBJ_TYPE);
1446 err = open_loose_object(&fd, id, repo);
1447 if (err)
1448 return err;
1449 err = read_tag_privsep(tag, fd, repo);
1452 if (err == NULL) {
1453 (*tag)->refcnt++;
1454 err = got_repo_cache_tag(repo, id, *tag);
1456 done:
1457 free(path_packfile);
1458 return err;
1461 const struct got_error *
1462 got_object_open_as_tag(struct got_tag_object **tag,
1463 struct got_repository *repo, struct got_object_id *id)
1465 *tag = got_repo_get_cached_tag(repo, id);
1466 if (*tag != NULL) {
1467 (*tag)->refcnt++;
1468 return NULL;
1471 return open_tag(tag, repo, id, 0);
1474 const struct got_error *
1475 got_object_tag_open(struct got_tag_object **tag,
1476 struct got_repository *repo, struct got_object *obj)
1478 return open_tag(tag, repo, got_object_get_id(obj), 1);
1481 const char *
1482 got_object_tag_get_name(struct got_tag_object *tag)
1484 return tag->tag;
1487 int
1488 got_object_tag_get_object_type(struct got_tag_object *tag)
1490 return tag->obj_type;
1493 struct got_object_id *
1494 got_object_tag_get_object_id(struct got_tag_object *tag)
1496 return &tag->id;
1499 time_t
1500 got_object_tag_get_tagger_time(struct got_tag_object *tag)
1502 return tag->tagger_time;
1505 time_t
1506 got_object_tag_get_tagger_gmtoff(struct got_tag_object *tag)
1508 return tag->tagger_gmtoff;
1511 const char *
1512 got_object_tag_get_tagger(struct got_tag_object *tag)
1514 return tag->tagger;
1517 const char *
1518 got_object_tag_get_message(struct got_tag_object *tag)
1520 return tag->tagmsg;
1523 static struct got_tree_entry *
1524 find_entry_by_name(struct got_tree_object *tree, const char *name, size_t len)
1526 int i;
1528 /* Note that tree entries are sorted in strncmp() order. */
1529 for (i = 0; i < tree->nentries; i++) {
1530 struct got_tree_entry *te = &tree->entries[i];
1531 int cmp = strncmp(te->name, name, len);
1532 if (cmp < 0)
1533 continue;
1534 if (cmp > 0)
1535 break;
1536 if (te->name[len] == '\0')
1537 return te;
1539 return NULL;
1542 struct got_tree_entry *
1543 got_object_tree_find_entry(struct got_tree_object *tree, const char *name)
1545 return find_entry_by_name(tree, name, strlen(name));
1548 const struct got_error *
1549 got_object_id_by_path(struct got_object_id **id, struct got_repository *repo,
1550 struct got_object_id *commit_id, const char *path)
1552 const struct got_error *err = NULL;
1553 struct got_commit_object *commit = NULL;
1554 struct got_tree_object *tree = NULL;
1555 struct got_tree_entry *te = NULL;
1556 const char *seg, *s;
1557 size_t seglen;
1559 *id = NULL;
1561 err = got_object_open_as_commit(&commit, repo, commit_id);
1562 if (err)
1563 goto done;
1565 /* Handle opening of root of commit's tree. */
1566 if (got_path_is_root_dir(path)) {
1567 *id = got_object_id_dup(commit->tree_id);
1568 if (*id == NULL)
1569 err = got_error_from_errno("got_object_id_dup");
1570 goto done;
1573 err = got_object_open_as_tree(&tree, repo, commit->tree_id);
1574 if (err)
1575 goto done;
1577 s = path;
1578 while (s[0] == '/')
1579 s++;
1580 seg = s;
1581 seglen = 0;
1582 while (*s) {
1583 struct got_tree_object *next_tree;
1585 if (*s != '/') {
1586 s++;
1587 seglen++;
1588 if (*s)
1589 continue;
1592 te = find_entry_by_name(tree, seg, seglen);
1593 if (te == NULL) {
1594 err = got_error(GOT_ERR_NO_TREE_ENTRY);
1595 goto done;
1598 if (*s == '\0')
1599 break;
1601 seg = s + 1;
1602 seglen = 0;
1603 s++;
1604 if (*s) {
1605 err = got_object_open_as_tree(&next_tree, repo,
1606 &te->id);
1607 te = NULL;
1608 if (err)
1609 goto done;
1610 got_object_tree_close(tree);
1611 tree = next_tree;
1615 if (te) {
1616 *id = got_object_id_dup(&te->id);
1617 if (*id == NULL)
1618 return got_error_from_errno("got_object_id_dup");
1619 } else
1620 err = got_error(GOT_ERR_NO_TREE_ENTRY);
1621 done:
1622 if (commit)
1623 got_object_commit_close(commit);
1624 if (tree)
1625 got_object_tree_close(tree);
1626 return err;
1629 const struct got_error *
1630 got_object_tree_path_changed(int *changed,
1631 struct got_tree_object *tree01, struct got_tree_object *tree02,
1632 const char *path, struct got_repository *repo)
1634 const struct got_error *err = NULL;
1635 struct got_tree_object *tree1 = NULL, *tree2 = NULL;
1636 struct got_tree_entry *te1 = NULL, *te2 = NULL;
1637 const char *seg, *s;
1638 size_t seglen;
1640 *changed = 0;
1642 /* We are expecting an absolute in-repository path. */
1643 if (path[0] != '/')
1644 return got_error(GOT_ERR_NOT_ABSPATH);
1646 /* We not do support comparing the root path. */
1647 if (path[1] == '\0')
1648 return got_error(GOT_ERR_BAD_PATH);
1650 tree1 = tree01;
1651 tree2 = tree02;
1652 s = path;
1653 s++; /* skip leading '/' */
1654 seg = s;
1655 seglen = 0;
1656 while (*s) {
1657 struct got_tree_object *next_tree1, *next_tree2;
1659 if (*s != '/') {
1660 s++;
1661 seglen++;
1662 if (*s)
1663 continue;
1666 te1 = find_entry_by_name(tree1, seg, seglen);
1667 if (te1 == NULL) {
1668 err = got_error(GOT_ERR_NO_OBJ);
1669 goto done;
1672 te2 = find_entry_by_name(tree2, seg, seglen);
1673 if (te2 == NULL) {
1674 *changed = 1;
1675 goto done;
1678 if (te1->mode != te2->mode) {
1679 *changed = 1;
1680 goto done;
1683 if (got_object_id_cmp(&te1->id, &te2->id) == 0) {
1684 *changed = 0;
1685 goto done;
1688 if (*s == '\0') { /* final path element */
1689 *changed = 1;
1690 goto done;
1693 seg = s + 1;
1694 s++;
1695 seglen = 0;
1696 if (*s) {
1697 err = got_object_open_as_tree(&next_tree1, repo,
1698 &te1->id);
1699 te1 = NULL;
1700 if (err)
1701 goto done;
1702 if (tree1 != tree01)
1703 got_object_tree_close(tree1);
1704 tree1 = next_tree1;
1706 err = got_object_open_as_tree(&next_tree2, repo,
1707 &te2->id);
1708 te2 = NULL;
1709 if (err)
1710 goto done;
1711 if (tree2 != tree02)
1712 got_object_tree_close(tree2);
1713 tree2 = next_tree2;
1716 done:
1717 if (tree1 && tree1 != tree01)
1718 got_object_tree_close(tree1);
1719 if (tree2 && tree2 != tree02)
1720 got_object_tree_close(tree2);
1721 return err;
1724 const struct got_error *
1725 got_object_tree_entry_dup(struct got_tree_entry **new_te,
1726 struct got_tree_entry *te)
1728 const struct got_error *err = NULL;
1730 *new_te = calloc(1, sizeof(**new_te));
1731 if (*new_te == NULL)
1732 return got_error_from_errno("calloc");
1734 (*new_te)->mode = te->mode;
1735 memcpy((*new_te)->name, te->name, sizeof((*new_te)->name));
1736 memcpy(&(*new_te)->id, &te->id, sizeof((*new_te)->id));
1737 return err;
1740 int
1741 got_object_tree_entry_is_submodule(struct got_tree_entry *te)
1743 return (te->mode & S_IFMT) == (S_IFDIR | S_IFLNK);
1746 const struct got_error *
1747 got_traverse_packed_commits(struct got_object_id_queue *traversed_commits,
1748 struct got_object_id *commit_id, const char *path,
1749 struct got_repository *repo)
1751 const struct got_error *err = NULL;
1752 struct got_pack *pack = NULL;
1753 struct got_packidx *packidx = NULL;
1754 char *path_packfile = NULL;
1755 struct got_commit_object *changed_commit = NULL;
1756 struct got_object_id *changed_commit_id = NULL;
1757 int idx;
1759 err = got_repo_search_packidx(&packidx, &idx, repo, commit_id);
1760 if (err) {
1761 if (err->code != GOT_ERR_NO_OBJ)
1762 return err;
1763 return NULL;
1766 err = get_packfile_path(&path_packfile, packidx);
1767 if (err)
1768 return err;
1770 pack = got_repo_get_cached_pack(repo, path_packfile);
1771 if (pack == NULL) {
1772 err = got_repo_cache_pack(&pack, repo, path_packfile, packidx);
1773 if (err)
1774 goto done;
1777 if (pack->privsep_child == NULL) {
1778 err = start_pack_privsep_child(pack, packidx);
1779 if (err)
1780 goto done;
1783 err = got_privsep_send_commit_traversal_request(
1784 pack->privsep_child->ibuf, commit_id, idx, path);
1785 if (err)
1786 goto done;
1788 err = got_privsep_recv_traversed_commits(&changed_commit,
1789 &changed_commit_id, traversed_commits, pack->privsep_child->ibuf);
1790 if (err)
1791 goto done;
1793 if (changed_commit) {
1795 * Cache the commit in which the path was changed.
1796 * This commit might be opened again soon.
1798 changed_commit->refcnt++;
1799 err = got_repo_cache_commit(repo, changed_commit_id,
1800 changed_commit);
1801 got_object_commit_close(changed_commit);
1803 done:
1804 free(path_packfile);
1805 free(changed_commit_id);
1806 return err;