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 <unistd.h>
34 #include <zlib.h>
35 #include <ctype.h>
36 #include <libgen.h>
37 #include <limits.h>
38 #include <imsg.h>
39 #include <time.h>
41 #include "got_error.h"
42 #include "got_object.h"
43 #include "got_repository.h"
44 #include "got_opentemp.h"
45 #include "got_path.h"
47 #include "got_lib_sha1.h"
48 #include "got_lib_delta.h"
49 #include "got_lib_inflate.h"
50 #include "got_lib_object.h"
51 #include "got_lib_privsep.h"
52 #include "got_lib_object_idcache.h"
53 #include "got_lib_object_cache.h"
54 #include "got_lib_object_parse.h"
55 #include "got_lib_pack.h"
56 #include "got_lib_repository.h"
58 #ifndef MIN
59 #define MIN(_a,_b) ((_a) < (_b) ? (_a) : (_b))
60 #endif
62 struct got_object_id *
63 got_object_get_id(struct got_object *obj)
64 {
65 return &obj->id;
66 }
68 const struct got_error *
69 got_object_get_id_str(char **outbuf, struct got_object *obj)
70 {
71 return got_object_id_str(outbuf, &obj->id);
72 }
74 const struct got_error *
75 got_object_get_type(int *type, struct got_repository *repo,
76 struct got_object_id *id)
77 {
78 const struct got_error *err = NULL;
79 struct got_object *obj;
81 err = got_object_open(&obj, repo, id);
82 if (err)
83 return err;
85 switch (obj->type) {
86 case GOT_OBJ_TYPE_COMMIT:
87 case GOT_OBJ_TYPE_TREE:
88 case GOT_OBJ_TYPE_BLOB:
89 case GOT_OBJ_TYPE_TAG:
90 *type = obj->type;
91 break;
92 default:
93 err = got_error(GOT_ERR_OBJ_TYPE);
94 break;
95 }
97 got_object_close(obj);
98 return err;
99 }
101 const struct got_error *
102 got_object_get_path(char **path, struct got_object_id *id,
103 struct got_repository *repo)
105 const struct got_error *err = NULL;
106 char *hex = NULL;
107 char *path_objects;
109 *path = NULL;
111 path_objects = got_repo_get_path_objects(repo);
112 if (path_objects == NULL)
113 return got_error_from_errno("got_repo_get_path_objects");
115 err = got_object_id_str(&hex, id);
116 if (err)
117 goto done;
119 if (asprintf(path, "%s/%.2x/%s", path_objects,
120 id->sha1[0], hex + 2) == -1)
121 err = got_error_from_errno("asprintf");
123 done:
124 free(hex);
125 free(path_objects);
126 return err;
129 static const struct got_error *
130 open_loose_object(int *fd, struct got_object_id *id,
131 struct got_repository *repo)
133 const struct got_error *err = NULL;
134 char *path;
136 err = got_object_get_path(&path, id, repo);
137 if (err)
138 return err;
139 *fd = open(path, O_RDONLY | O_NOFOLLOW);
140 if (*fd == -1) {
141 err = got_error_from_errno2("open", path);
142 goto done;
144 done:
145 free(path);
146 return err;
149 static const struct got_error *
150 get_packfile_path(char **path_packfile, struct got_packidx *packidx)
152 size_t size;
154 /* Packfile path contains ".pack" instead of ".idx", so add one byte. */
155 size = strlen(packidx->path_packidx) + 2;
156 if (size < GOT_PACKFILE_NAMELEN + 1)
157 return got_error_path(packidx->path_packidx, GOT_ERR_BAD_PATH);
159 *path_packfile = malloc(size);
160 if (*path_packfile == NULL)
161 return got_error_from_errno("malloc");
163 /* Copy up to and excluding ".idx". */
164 if (strlcpy(*path_packfile, packidx->path_packidx,
165 size - strlen(GOT_PACKIDX_SUFFIX) - 1) >= size)
166 return got_error(GOT_ERR_NO_SPACE);
168 if (strlcat(*path_packfile, GOT_PACKFILE_SUFFIX, size) >= size)
169 return got_error(GOT_ERR_NO_SPACE);
171 return NULL;
174 static const struct got_error *
175 request_packed_object(struct got_object **obj, struct got_pack *pack, int idx,
176 struct got_object_id *id)
178 const struct got_error *err = NULL;
179 struct imsgbuf *ibuf = pack->privsep_child->ibuf;
181 err = got_privsep_send_packed_obj_req(ibuf, idx, id);
182 if (err)
183 return err;
185 err = got_privsep_recv_obj(obj, ibuf);
186 if (err)
187 return err;
189 memcpy(&(*obj)->id, id, sizeof((*obj)->id));
191 return NULL;
194 static void
195 set_max_datasize(void)
197 struct rlimit rl;
199 if (getrlimit(RLIMIT_DATA, &rl) != 0)
200 return;
202 rl.rlim_cur = rl.rlim_max;
203 setrlimit(RLIMIT_DATA, &rl);
206 static const struct got_error *
207 start_pack_privsep_child(struct got_pack *pack, struct got_packidx *packidx)
209 const struct got_error *err = NULL;
210 int imsg_fds[2];
211 pid_t pid;
212 struct imsgbuf *ibuf;
214 ibuf = calloc(1, sizeof(*ibuf));
215 if (ibuf == NULL)
216 return got_error_from_errno("calloc");
218 pack->privsep_child = calloc(1, sizeof(*pack->privsep_child));
219 if (pack->privsep_child == NULL) {
220 err = got_error_from_errno("calloc");
221 free(ibuf);
222 return err;
225 if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_fds) == -1) {
226 err = got_error_from_errno("socketpair");
227 goto done;
230 pid = fork();
231 if (pid == -1) {
232 err = got_error_from_errno("fork");
233 goto done;
234 } else if (pid == 0) {
235 set_max_datasize();
236 got_privsep_exec_child(imsg_fds, GOT_PATH_PROG_READ_PACK,
237 pack->path_packfile);
238 /* not reached */
241 if (close(imsg_fds[1]) != 0)
242 return got_error_from_errno("close");
243 pack->privsep_child->imsg_fd = imsg_fds[0];
244 pack->privsep_child->pid = pid;
245 imsg_init(ibuf, imsg_fds[0]);
246 pack->privsep_child->ibuf = ibuf;
248 err = got_privsep_init_pack_child(ibuf, pack, packidx);
249 if (err) {
250 const struct got_error *child_err;
251 err = got_privsep_send_stop(pack->privsep_child->imsg_fd);
252 child_err = got_privsep_wait_for_child(
253 pack->privsep_child->pid);
254 if (child_err && err == NULL)
255 err = child_err;
257 done:
258 if (err) {
259 free(ibuf);
260 free(pack->privsep_child);
261 pack->privsep_child = NULL;
263 return err;
266 static const struct got_error *
267 read_packed_object_privsep(struct got_object **obj,
268 struct got_repository *repo, struct got_pack *pack,
269 struct got_packidx *packidx, int idx, struct got_object_id *id)
271 const struct got_error *err = NULL;
273 if (pack->privsep_child)
274 return request_packed_object(obj, pack, idx, id);
276 err = start_pack_privsep_child(pack, packidx);
277 if (err)
278 return err;
280 return request_packed_object(obj, pack, idx, id);
284 static const struct got_error *
285 open_packed_object(struct got_object **obj, struct got_object_id *id,
286 struct got_repository *repo)
288 const struct got_error *err = NULL;
289 struct got_pack *pack = NULL;
290 struct got_packidx *packidx = NULL;
291 int idx;
292 char *path_packfile;
294 err = got_repo_search_packidx(&packidx, &idx, repo, id);
295 if (err)
296 return err;
298 err = get_packfile_path(&path_packfile, packidx);
299 if (err)
300 return err;
302 pack = got_repo_get_cached_pack(repo, path_packfile);
303 if (pack == NULL) {
304 err = got_repo_cache_pack(&pack, repo, path_packfile, packidx);
305 if (err)
306 goto done;
309 err = read_packed_object_privsep(obj, repo, pack, packidx, idx, id);
310 if (err)
311 goto done;
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 const struct got_error *
868 got_object_blob_read_to_str(char **s, struct got_blob_object *blob)
870 const struct got_error *err = NULL;
871 size_t len, totlen, hdrlen, offset;
873 *s = NULL;
875 hdrlen = got_object_blob_get_hdrlen(blob);
876 totlen = 0;
877 offset = 0;
878 do {
879 char *p;
881 err = got_object_blob_read_block(&len, blob);
882 if (err)
883 return err;
885 if (len == 0)
886 break;
888 totlen += len - hdrlen;
889 p = realloc(*s, totlen + 1);
890 if (p == NULL) {
891 err = got_error_from_errno("realloc");
892 free(*s);
893 *s = NULL;
894 return err;
896 *s = p;
897 /* Skip blob object header first time around. */
898 memcpy(*s + offset,
899 got_object_blob_get_read_buf(blob) + hdrlen, len - hdrlen);
900 hdrlen = 0;
901 offset = totlen;
902 } while (len > 0);
904 (*s)[totlen] = '\0';
905 return NULL;
908 const struct got_error *
909 got_tree_entry_get_symlink_target(char **link_target, struct got_tree_entry *te,
910 struct got_repository *repo)
912 const struct got_error *err = NULL;
913 struct got_blob_object *blob = NULL;
915 *link_target = NULL;
917 if (!got_object_tree_entry_is_symlink(te))
918 return got_error(GOT_ERR_TREE_ENTRY_TYPE);
920 err = got_object_open_as_blob(&blob, repo,
921 got_tree_entry_get_id(te), PATH_MAX);
922 if (err)
923 return err;
925 err = got_object_blob_read_to_str(link_target, blob);
926 got_object_blob_close(blob);
927 if (err) {
928 free(*link_target);
929 *link_target = NULL;
931 return err;
934 int
935 got_tree_entry_get_index(struct got_tree_entry *te)
937 return te->idx;
940 struct got_tree_entry *
941 got_tree_entry_get_next(struct got_tree_object *tree,
942 struct got_tree_entry *te)
944 return got_object_tree_get_entry(tree, te->idx + 1);
947 struct got_tree_entry *
948 got_tree_entry_get_prev(struct got_tree_object *tree,
949 struct got_tree_entry *te)
951 return got_object_tree_get_entry(tree, te->idx - 1);
954 static const struct got_error *
955 request_packed_blob(uint8_t **outbuf, size_t *size, size_t *hdrlen, int outfd,
956 struct got_pack *pack, struct got_packidx *packidx, int idx,
957 struct got_object_id *id)
959 const struct got_error *err = NULL;
960 int outfd_child;
961 int basefd, accumfd; /* temporary files for delta application */
963 basefd = got_opentempfd();
964 if (basefd == -1)
965 return got_error_from_errno("got_opentempfd");
966 accumfd = got_opentempfd();
967 if (accumfd == -1)
968 return got_error_from_errno("got_opentempfd");
970 outfd_child = dup(outfd);
971 if (outfd_child == -1)
972 return got_error_from_errno("dup");
974 err = got_privsep_send_blob_req(pack->privsep_child->ibuf, -1, id, idx);
975 if (err)
976 return err;
978 err = got_privsep_send_blob_outfd(pack->privsep_child->ibuf,
979 outfd_child);
980 if (err) {
981 close(basefd);
982 close(accumfd);
983 return err;
986 err = got_privsep_send_tmpfd(pack->privsep_child->ibuf,
987 basefd);
988 if (err) {
989 close(accumfd);
990 return err;
993 err = got_privsep_send_tmpfd(pack->privsep_child->ibuf,
994 accumfd);
995 if (err)
996 return err;
998 err = got_privsep_recv_blob(outbuf, size, hdrlen,
999 pack->privsep_child->ibuf);
1000 if (err)
1001 return err;
1003 if (lseek(outfd, SEEK_SET, 0) == -1)
1004 err = got_error_from_errno("lseek");
1006 return err;
1009 static const struct got_error *
1010 read_packed_blob_privsep(uint8_t **outbuf, size_t *size, size_t *hdrlen,
1011 int outfd, struct got_pack *pack, struct got_packidx *packidx, int idx,
1012 struct got_object_id *id)
1014 const struct got_error *err = NULL;
1016 if (pack->privsep_child == NULL) {
1017 err = start_pack_privsep_child(pack, packidx);
1018 if (err)
1019 return err;
1022 return request_packed_blob(outbuf, size, hdrlen, outfd, pack, packidx,
1023 idx, id);
1026 static const struct got_error *
1027 request_blob(uint8_t **outbuf, size_t *size, size_t *hdrlen, int outfd,
1028 int infd, struct imsgbuf *ibuf)
1030 const struct got_error *err = NULL;
1031 int outfd_child;
1033 outfd_child = dup(outfd);
1034 if (outfd_child == -1)
1035 return got_error_from_errno("dup");
1037 err = got_privsep_send_blob_req(ibuf, infd, NULL, -1);
1038 if (err)
1039 return err;
1041 err = got_privsep_send_blob_outfd(ibuf, outfd_child);
1042 if (err)
1043 return err;
1045 err = got_privsep_recv_blob(outbuf, size, hdrlen, ibuf);
1046 if (err)
1047 return err;
1049 if (lseek(outfd, SEEK_SET, 0) == -1)
1050 return got_error_from_errno("lseek");
1052 return err;
1055 static const struct got_error *
1056 read_blob_privsep(uint8_t **outbuf, size_t *size, size_t *hdrlen,
1057 int outfd, int infd, struct got_repository *repo)
1059 const struct got_error *err;
1060 int imsg_fds[2];
1061 pid_t pid;
1062 struct imsgbuf *ibuf;
1064 if (repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_BLOB].imsg_fd != -1) {
1065 ibuf = repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_BLOB].ibuf;
1066 return request_blob(outbuf, size, hdrlen, outfd, infd, ibuf);
1069 ibuf = calloc(1, sizeof(*ibuf));
1070 if (ibuf == NULL)
1071 return got_error_from_errno("calloc");
1073 if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_fds) == -1) {
1074 err = got_error_from_errno("socketpair");
1075 free(ibuf);
1076 return err;
1079 pid = fork();
1080 if (pid == -1) {
1081 err = got_error_from_errno("fork");
1082 free(ibuf);
1083 return err;
1085 else if (pid == 0) {
1086 got_privsep_exec_child(imsg_fds, GOT_PATH_PROG_READ_BLOB,
1087 repo->path);
1088 /* not reached */
1091 if (close(imsg_fds[1]) != 0) {
1092 err = got_error_from_errno("close");
1093 free(ibuf);
1094 return err;
1096 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_BLOB].imsg_fd =
1097 imsg_fds[0];
1098 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_BLOB].pid = pid;
1099 imsg_init(ibuf, imsg_fds[0]);
1100 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_BLOB].ibuf = ibuf;
1102 return request_blob(outbuf, size, hdrlen, outfd, infd, ibuf);
1105 static const struct got_error *
1106 open_blob(struct got_blob_object **blob, struct got_repository *repo,
1107 struct got_object_id *id, size_t blocksize)
1109 const struct got_error *err = NULL;
1110 struct got_packidx *packidx = NULL;
1111 int idx;
1112 char *path_packfile = NULL;
1113 uint8_t *outbuf;
1114 int outfd;
1115 size_t size, hdrlen;
1116 struct stat sb;
1118 *blob = calloc(1, sizeof(**blob));
1119 if (*blob == NULL)
1120 return got_error_from_errno("calloc");
1122 outfd = got_opentempfd();
1123 if (outfd == -1)
1124 return got_error_from_errno("got_opentempfd");
1126 (*blob)->read_buf = malloc(blocksize);
1127 if ((*blob)->read_buf == NULL) {
1128 err = got_error_from_errno("malloc");
1129 goto done;
1132 err = got_repo_search_packidx(&packidx, &idx, repo, id);
1133 if (err == NULL) {
1134 struct got_pack *pack = NULL;
1136 err = get_packfile_path(&path_packfile, packidx);
1137 if (err)
1138 goto done;
1140 pack = got_repo_get_cached_pack(repo, path_packfile);
1141 if (pack == NULL) {
1142 err = got_repo_cache_pack(&pack, repo, path_packfile,
1143 packidx);
1144 if (err)
1145 goto done;
1147 err = read_packed_blob_privsep(&outbuf, &size, &hdrlen, outfd,
1148 pack, packidx, idx, id);
1149 } else if (err->code == GOT_ERR_NO_OBJ) {
1150 int infd;
1152 err = open_loose_object(&infd, id, repo);
1153 if (err)
1154 goto done;
1155 err = read_blob_privsep(&outbuf, &size, &hdrlen, outfd, infd,
1156 repo);
1158 if (err)
1159 goto done;
1161 if (hdrlen > size) {
1162 err = got_error(GOT_ERR_BAD_OBJ_HDR);
1163 goto done;
1166 if (outbuf) {
1167 if (close(outfd) != 0 && err == NULL)
1168 err = got_error_from_errno("close");
1169 outfd = -1;
1170 (*blob)->f = fmemopen(outbuf, size, "rb");
1171 if ((*blob)->f == NULL) {
1172 err = got_error_from_errno("fmemopen");
1173 free(outbuf);
1174 goto done;
1176 (*blob)->data = outbuf;
1177 } else {
1178 if (fstat(outfd, &sb) == -1) {
1179 err = got_error_from_errno("fstat");
1180 goto done;
1183 if (sb.st_size != size) {
1184 err = got_error(GOT_ERR_PRIVSEP_LEN);
1185 goto done;
1188 (*blob)->f = fdopen(outfd, "rb");
1189 if ((*blob)->f == NULL) {
1190 err = got_error_from_errno("fdopen");
1191 close(outfd);
1192 outfd = -1;
1193 goto done;
1197 (*blob)->hdrlen = hdrlen;
1198 (*blob)->blocksize = blocksize;
1199 memcpy(&(*blob)->id.sha1, id->sha1, SHA1_DIGEST_LENGTH);
1201 done:
1202 free(path_packfile);
1203 if (err) {
1204 if (*blob) {
1205 got_object_blob_close(*blob);
1206 *blob = NULL;
1207 } else if (outfd != -1)
1208 close(outfd);
1210 return err;
1213 const struct got_error *
1214 got_object_open_as_blob(struct got_blob_object **blob,
1215 struct got_repository *repo, struct got_object_id *id,
1216 size_t blocksize)
1218 return open_blob(blob, repo, id, blocksize);
1221 const struct got_error *
1222 got_object_blob_open(struct got_blob_object **blob,
1223 struct got_repository *repo, struct got_object *obj, size_t blocksize)
1225 return open_blob(blob, repo, got_object_get_id(obj), blocksize);
1228 const struct got_error *
1229 got_object_blob_close(struct got_blob_object *blob)
1231 const struct got_error *err = NULL;
1232 free(blob->read_buf);
1233 if (blob->f && fclose(blob->f) != 0)
1234 err = got_error_from_errno("fclose");
1235 free(blob->data);
1236 free(blob);
1237 return err;
1240 void
1241 got_object_blob_rewind(struct got_blob_object *blob)
1243 if (blob->f)
1244 rewind(blob->f);
1247 char *
1248 got_object_blob_id_str(struct got_blob_object *blob, char *buf, size_t size)
1250 return got_sha1_digest_to_str(blob->id.sha1, buf, size);
1253 size_t
1254 got_object_blob_get_hdrlen(struct got_blob_object *blob)
1256 return blob->hdrlen;
1259 const uint8_t *
1260 got_object_blob_get_read_buf(struct got_blob_object *blob)
1262 return blob->read_buf;
1265 const struct got_error *
1266 got_object_blob_read_block(size_t *outlenp, struct got_blob_object *blob)
1268 size_t n;
1270 n = fread(blob->read_buf, 1, blob->blocksize, blob->f);
1271 if (n == 0 && ferror(blob->f))
1272 return got_ferror(blob->f, GOT_ERR_IO);
1273 *outlenp = n;
1274 return NULL;
1277 const struct got_error *
1278 got_object_blob_dump_to_file(size_t *filesize, int *nlines,
1279 off_t **line_offsets, FILE *outfile, struct got_blob_object *blob)
1281 const struct got_error *err = NULL;
1282 size_t n, len, hdrlen;
1283 const uint8_t *buf;
1284 int i;
1285 size_t noffsets = 0;
1286 off_t off = 0, total_len = 0;
1288 if (line_offsets)
1289 *line_offsets = NULL;
1290 if (filesize)
1291 *filesize = 0;
1292 if (nlines)
1293 *nlines = 0;
1295 hdrlen = got_object_blob_get_hdrlen(blob);
1296 do {
1297 err = got_object_blob_read_block(&len, blob);
1298 if (err)
1299 return err;
1300 if (len == 0)
1301 break;
1302 buf = got_object_blob_get_read_buf(blob);
1303 i = hdrlen;
1304 if (line_offsets && nlines) {
1305 if (*line_offsets == NULL) {
1306 /* Have some data but perhaps no '\n'. */
1307 noffsets = 1;
1308 *nlines = 1;
1309 *line_offsets = calloc(1, sizeof(**line_offsets));
1310 if (*line_offsets == NULL)
1311 return got_error_from_errno("calloc");
1313 /* Skip forward over end of first line. */
1314 while (i < len) {
1315 if (buf[i] == '\n')
1316 break;
1317 i++;
1320 /* Scan '\n' offsets in remaining chunk of data. */
1321 while (i < len) {
1322 if (buf[i] != '\n') {
1323 i++;
1324 continue;
1326 (*nlines)++;
1327 if (noffsets < *nlines) {
1328 off_t *o = recallocarray(*line_offsets,
1329 noffsets, *nlines,
1330 sizeof(**line_offsets));
1331 if (o == NULL) {
1332 free(*line_offsets);
1333 *line_offsets = NULL;
1334 return got_error_from_errno(
1335 "recallocarray");
1337 *line_offsets = o;
1338 noffsets = *nlines;
1340 off = total_len + i - hdrlen + 1;
1341 (*line_offsets)[*nlines - 1] = off;
1342 i++;
1345 /* Skip blob object header first time around. */
1346 n = fwrite(buf + hdrlen, 1, len - hdrlen, outfile);
1347 if (n != len - hdrlen)
1348 return got_ferror(outfile, GOT_ERR_IO);
1349 total_len += len - hdrlen;
1350 hdrlen = 0;
1351 } while (len != 0);
1353 if (fflush(outfile) != 0)
1354 return got_error_from_errno("fflush");
1355 rewind(outfile);
1357 if (filesize)
1358 *filesize = total_len;
1360 return NULL;
1363 static const struct got_error *
1364 request_packed_tag(struct got_tag_object **tag, struct got_pack *pack,
1365 int pack_idx, struct got_object_id *id)
1367 const struct got_error *err = NULL;
1369 err = got_privsep_send_tag_req(pack->privsep_child->ibuf, -1, id,
1370 pack_idx);
1371 if (err)
1372 return err;
1374 return got_privsep_recv_tag(tag, pack->privsep_child->ibuf);
1377 static const struct got_error *
1378 read_packed_tag_privsep(struct got_tag_object **tag,
1379 struct got_pack *pack, struct got_packidx *packidx, int idx,
1380 struct got_object_id *id)
1382 const struct got_error *err = NULL;
1384 if (pack->privsep_child)
1385 return request_packed_tag(tag, pack, idx, id);
1387 err = start_pack_privsep_child(pack, packidx);
1388 if (err)
1389 return err;
1391 return request_packed_tag(tag, pack, idx, id);
1394 static const struct got_error *
1395 request_tag(struct got_tag_object **tag, struct got_repository *repo,
1396 int fd)
1398 const struct got_error *err = NULL;
1399 struct imsgbuf *ibuf;
1401 ibuf = repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TAG].ibuf;
1403 err = got_privsep_send_tag_req(ibuf, fd, NULL, -1);
1404 if (err)
1405 return err;
1407 return got_privsep_recv_tag(tag, ibuf);
1410 static const struct got_error *
1411 read_tag_privsep(struct got_tag_object **tag, int obj_fd,
1412 struct got_repository *repo)
1414 const struct got_error *err;
1415 int imsg_fds[2];
1416 pid_t pid;
1417 struct imsgbuf *ibuf;
1419 if (repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TAG].imsg_fd != -1)
1420 return request_tag(tag, repo, obj_fd);
1422 ibuf = calloc(1, sizeof(*ibuf));
1423 if (ibuf == NULL)
1424 return got_error_from_errno("calloc");
1426 if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_fds) == -1) {
1427 err = got_error_from_errno("socketpair");
1428 free(ibuf);
1429 return err;
1432 pid = fork();
1433 if (pid == -1) {
1434 err = got_error_from_errno("fork");
1435 free(ibuf);
1436 return err;
1438 else if (pid == 0) {
1439 got_privsep_exec_child(imsg_fds, GOT_PATH_PROG_READ_TAG,
1440 repo->path);
1441 /* not reached */
1444 if (close(imsg_fds[1]) != 0) {
1445 err = got_error_from_errno("close");
1446 free(ibuf);
1447 return err;
1449 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TAG].imsg_fd =
1450 imsg_fds[0];
1451 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TAG].pid = pid;
1452 imsg_init(ibuf, imsg_fds[0]);
1453 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TAG].ibuf = ibuf;
1455 return request_tag(tag, repo, obj_fd);
1458 static const struct got_error *
1459 open_tag(struct got_tag_object **tag, struct got_repository *repo,
1460 struct got_object_id *id, int check_cache)
1462 const struct got_error *err = NULL;
1463 struct got_packidx *packidx = NULL;
1464 int idx;
1465 char *path_packfile = NULL;
1466 struct got_object *obj = NULL;
1467 int obj_type = GOT_OBJ_TYPE_ANY;
1469 if (check_cache) {
1470 *tag = got_repo_get_cached_tag(repo, id);
1471 if (*tag != NULL) {
1472 (*tag)->refcnt++;
1473 return NULL;
1475 } else
1476 *tag = NULL;
1478 err = got_repo_search_packidx(&packidx, &idx, repo, id);
1479 if (err == NULL) {
1480 struct got_pack *pack = NULL;
1482 err = get_packfile_path(&path_packfile, packidx);
1483 if (err)
1484 return err;
1486 pack = got_repo_get_cached_pack(repo, path_packfile);
1487 if (pack == NULL) {
1488 err = got_repo_cache_pack(&pack, repo, path_packfile,
1489 packidx);
1490 if (err)
1491 goto done;
1494 /* Beware of "lightweight" tags: Check object type first. */
1495 err = read_packed_object_privsep(&obj, repo, pack, packidx,
1496 idx, id);
1497 if (err)
1498 goto done;
1499 obj_type = obj->type;
1500 got_object_close(obj);
1501 if (obj_type != GOT_OBJ_TYPE_TAG) {
1502 err = got_error(GOT_ERR_OBJ_TYPE);
1503 goto done;
1505 err = read_packed_tag_privsep(tag, pack, packidx, idx, id);
1506 } else if (err->code == GOT_ERR_NO_OBJ) {
1507 int fd;
1509 err = open_loose_object(&fd, id, repo);
1510 if (err)
1511 return err;
1512 err = read_object_header_privsep(&obj, repo, fd);
1513 if (err)
1514 return err;
1515 obj_type = obj->type;
1516 got_object_close(obj);
1517 if (obj_type != GOT_OBJ_TYPE_TAG)
1518 return got_error(GOT_ERR_OBJ_TYPE);
1520 err = open_loose_object(&fd, id, repo);
1521 if (err)
1522 return err;
1523 err = read_tag_privsep(tag, fd, repo);
1526 if (err == NULL) {
1527 (*tag)->refcnt++;
1528 err = got_repo_cache_tag(repo, id, *tag);
1530 done:
1531 free(path_packfile);
1532 return err;
1535 const struct got_error *
1536 got_object_open_as_tag(struct got_tag_object **tag,
1537 struct got_repository *repo, struct got_object_id *id)
1539 *tag = got_repo_get_cached_tag(repo, id);
1540 if (*tag != NULL) {
1541 (*tag)->refcnt++;
1542 return NULL;
1545 return open_tag(tag, repo, id, 0);
1548 const struct got_error *
1549 got_object_tag_open(struct got_tag_object **tag,
1550 struct got_repository *repo, struct got_object *obj)
1552 return open_tag(tag, repo, got_object_get_id(obj), 1);
1555 const char *
1556 got_object_tag_get_name(struct got_tag_object *tag)
1558 return tag->tag;
1561 int
1562 got_object_tag_get_object_type(struct got_tag_object *tag)
1564 return tag->obj_type;
1567 struct got_object_id *
1568 got_object_tag_get_object_id(struct got_tag_object *tag)
1570 return &tag->id;
1573 time_t
1574 got_object_tag_get_tagger_time(struct got_tag_object *tag)
1576 return tag->tagger_time;
1579 time_t
1580 got_object_tag_get_tagger_gmtoff(struct got_tag_object *tag)
1582 return tag->tagger_gmtoff;
1585 const char *
1586 got_object_tag_get_tagger(struct got_tag_object *tag)
1588 return tag->tagger;
1591 const char *
1592 got_object_tag_get_message(struct got_tag_object *tag)
1594 return tag->tagmsg;
1597 static struct got_tree_entry *
1598 find_entry_by_name(struct got_tree_object *tree, const char *name, size_t len)
1600 int i;
1602 /* Note that tree entries are sorted in strncmp() order. */
1603 for (i = 0; i < tree->nentries; i++) {
1604 struct got_tree_entry *te = &tree->entries[i];
1605 int cmp = strncmp(te->name, name, len);
1606 if (cmp < 0)
1607 continue;
1608 if (cmp > 0)
1609 break;
1610 if (te->name[len] == '\0')
1611 return te;
1613 return NULL;
1616 struct got_tree_entry *
1617 got_object_tree_find_entry(struct got_tree_object *tree, const char *name)
1619 return find_entry_by_name(tree, name, strlen(name));
1622 const struct got_error *
1623 got_object_id_by_path(struct got_object_id **id, struct got_repository *repo,
1624 struct got_object_id *commit_id, const char *path)
1626 const struct got_error *err = NULL;
1627 struct got_commit_object *commit = NULL;
1628 struct got_tree_object *tree = NULL;
1629 struct got_tree_entry *te = NULL;
1630 const char *seg, *s;
1631 size_t seglen;
1633 *id = NULL;
1635 err = got_object_open_as_commit(&commit, repo, commit_id);
1636 if (err)
1637 goto done;
1639 /* Handle opening of root of commit's tree. */
1640 if (got_path_is_root_dir(path)) {
1641 *id = got_object_id_dup(commit->tree_id);
1642 if (*id == NULL)
1643 err = got_error_from_errno("got_object_id_dup");
1644 goto done;
1647 err = got_object_open_as_tree(&tree, repo, commit->tree_id);
1648 if (err)
1649 goto done;
1651 s = path;
1652 while (s[0] == '/')
1653 s++;
1654 seg = s;
1655 seglen = 0;
1656 while (*s) {
1657 struct got_tree_object *next_tree;
1659 if (*s != '/') {
1660 s++;
1661 seglen++;
1662 if (*s)
1663 continue;
1666 te = find_entry_by_name(tree, seg, seglen);
1667 if (te == NULL) {
1668 err = got_error_path(path, GOT_ERR_NO_TREE_ENTRY);
1669 goto done;
1672 if (*s == '\0')
1673 break;
1675 seg = s + 1;
1676 seglen = 0;
1677 s++;
1678 if (*s) {
1679 err = got_object_open_as_tree(&next_tree, repo,
1680 &te->id);
1681 te = NULL;
1682 if (err)
1683 goto done;
1684 got_object_tree_close(tree);
1685 tree = next_tree;
1689 if (te) {
1690 *id = got_object_id_dup(&te->id);
1691 if (*id == NULL)
1692 return got_error_from_errno("got_object_id_dup");
1693 } else
1694 err = got_error_path(path, GOT_ERR_NO_TREE_ENTRY);
1695 done:
1696 if (commit)
1697 got_object_commit_close(commit);
1698 if (tree)
1699 got_object_tree_close(tree);
1700 return err;
1704 * Normalize file mode bits to avoid false positive tree entry differences
1705 * in case tree entries have unexpected mode bits set.
1707 static mode_t
1708 normalize_mode_for_comparison(mode_t mode)
1711 * For directories, the only relevant bit is the IFDIR bit.
1712 * This allows us to detect paths changing from a directory
1713 * to a file and vice versa.
1715 if (S_ISDIR(mode))
1716 return mode & S_IFDIR;
1719 * For symlinks, the only relevant bit is the IFLNK bit.
1720 * This allows us to detect paths changing from a symlinks
1721 * to a file or directory and vice versa.
1723 if (S_ISLNK(mode))
1724 return mode & S_IFLNK;
1726 /* For files, the only change we care about is the executable bit. */
1727 return mode & S_IXUSR;
1730 const struct got_error *
1731 got_object_tree_path_changed(int *changed,
1732 struct got_tree_object *tree01, struct got_tree_object *tree02,
1733 const char *path, struct got_repository *repo)
1735 const struct got_error *err = NULL;
1736 struct got_tree_object *tree1 = NULL, *tree2 = NULL;
1737 struct got_tree_entry *te1 = NULL, *te2 = NULL;
1738 const char *seg, *s;
1739 size_t seglen;
1741 *changed = 0;
1743 /* We not do support comparing the root path. */
1744 if (got_path_is_root_dir(path))
1745 return got_error_path(path, GOT_ERR_BAD_PATH);
1747 tree1 = tree01;
1748 tree2 = tree02;
1749 s = path;
1750 while (*s == '/')
1751 s++;
1752 seg = s;
1753 seglen = 0;
1754 while (*s) {
1755 struct got_tree_object *next_tree1, *next_tree2;
1756 mode_t mode1, mode2;
1758 if (*s != '/') {
1759 s++;
1760 seglen++;
1761 if (*s)
1762 continue;
1765 te1 = find_entry_by_name(tree1, seg, seglen);
1766 if (te1 == NULL) {
1767 err = got_error(GOT_ERR_NO_OBJ);
1768 goto done;
1771 te2 = find_entry_by_name(tree2, seg, seglen);
1772 if (te2 == NULL) {
1773 *changed = 1;
1774 goto done;
1777 mode1 = normalize_mode_for_comparison(te1->mode);
1778 mode2 = normalize_mode_for_comparison(te2->mode);
1779 if (mode1 != mode2) {
1780 *changed = 1;
1781 goto done;
1784 if (got_object_id_cmp(&te1->id, &te2->id) == 0) {
1785 *changed = 0;
1786 goto done;
1789 if (*s == '\0') { /* final path element */
1790 *changed = 1;
1791 goto done;
1794 seg = s + 1;
1795 s++;
1796 seglen = 0;
1797 if (*s) {
1798 err = got_object_open_as_tree(&next_tree1, repo,
1799 &te1->id);
1800 te1 = NULL;
1801 if (err)
1802 goto done;
1803 if (tree1 != tree01)
1804 got_object_tree_close(tree1);
1805 tree1 = next_tree1;
1807 err = got_object_open_as_tree(&next_tree2, repo,
1808 &te2->id);
1809 te2 = NULL;
1810 if (err)
1811 goto done;
1812 if (tree2 != tree02)
1813 got_object_tree_close(tree2);
1814 tree2 = next_tree2;
1817 done:
1818 if (tree1 && tree1 != tree01)
1819 got_object_tree_close(tree1);
1820 if (tree2 && tree2 != tree02)
1821 got_object_tree_close(tree2);
1822 return err;
1825 const struct got_error *
1826 got_object_tree_entry_dup(struct got_tree_entry **new_te,
1827 struct got_tree_entry *te)
1829 const struct got_error *err = NULL;
1831 *new_te = calloc(1, sizeof(**new_te));
1832 if (*new_te == NULL)
1833 return got_error_from_errno("calloc");
1835 (*new_te)->mode = te->mode;
1836 memcpy((*new_te)->name, te->name, sizeof((*new_te)->name));
1837 memcpy(&(*new_te)->id, &te->id, sizeof((*new_te)->id));
1838 return err;
1841 int
1842 got_object_tree_entry_is_submodule(struct got_tree_entry *te)
1844 return (te->mode & S_IFMT) == (S_IFDIR | S_IFLNK);
1847 int
1848 got_object_tree_entry_is_symlink(struct got_tree_entry *te)
1850 /* S_IFDIR check avoids confusing symlinks with submodules. */
1851 return ((te->mode & (S_IFDIR | S_IFLNK)) == S_IFLNK);
1854 static const struct got_error *
1855 resolve_symlink(char **link_target, const char *path,
1856 struct got_object_id *commit_id, struct got_repository *repo)
1858 const struct got_error *err = NULL;
1859 char *name, *parent_path = NULL;
1860 struct got_object_id *tree_obj_id = NULL;
1861 struct got_tree_object *tree = NULL;
1862 struct got_tree_entry *te = NULL;
1864 *link_target = NULL;
1866 name = basename(path);
1867 if (name == NULL)
1868 return got_error_from_errno2("basename", path);
1870 err = got_path_dirname(&parent_path, path);
1871 if (err)
1872 return err;
1874 err = got_object_id_by_path(&tree_obj_id, repo, commit_id,
1875 parent_path);
1876 if (err) {
1877 if (err->code == GOT_ERR_NO_TREE_ENTRY) {
1878 /* Display the complete path in error message. */
1879 err = got_error_path(path, err->code);
1881 goto done;
1884 err = got_object_open_as_tree(&tree, repo, tree_obj_id);
1885 if (err)
1886 goto done;
1888 te = got_object_tree_find_entry(tree, name);
1889 if (te == NULL) {
1890 err = got_error_path(path, GOT_ERR_NO_TREE_ENTRY);
1891 goto done;
1894 if (got_object_tree_entry_is_symlink(te)) {
1895 err = got_tree_entry_get_symlink_target(link_target, te, repo);
1896 if (err)
1897 goto done;
1898 if (!got_path_is_absolute(*link_target)) {
1899 char *abspath;
1900 if (asprintf(&abspath, "%s/%s", parent_path,
1901 *link_target) == -1) {
1902 err = got_error_from_errno("asprintf");
1903 goto done;
1905 free(*link_target);
1906 *link_target = malloc(PATH_MAX);
1907 if (*link_target == NULL) {
1908 err = got_error_from_errno("malloc");
1909 goto done;
1911 err = got_canonpath(abspath, *link_target, PATH_MAX);
1912 free(abspath);
1913 if (err)
1914 goto done;
1917 done:
1918 free(tree_obj_id);
1919 if (tree)
1920 got_object_tree_close(tree);
1921 if (err) {
1922 free(*link_target);
1923 *link_target = NULL;
1925 return err;
1928 const struct got_error *
1929 got_object_resolve_symlinks(char **link_target, const char *path,
1930 struct got_object_id *commit_id, struct got_repository *repo)
1932 const struct got_error *err = NULL;
1933 char *next_target = NULL;
1934 int max_recursion = 40; /* matches Git */
1936 *link_target = NULL;
1938 do {
1939 err = resolve_symlink(&next_target,
1940 *link_target ? *link_target : path, commit_id, repo);
1941 if (err)
1942 break;
1943 if (next_target) {
1944 free(*link_target);
1945 if (--max_recursion == 0) {
1946 err = got_error_path(path, GOT_ERR_RECURSION);
1947 *link_target = NULL;
1948 break;
1950 *link_target = next_target;
1952 } while (next_target);
1954 return err;
1957 const struct got_error *
1958 got_traverse_packed_commits(struct got_object_id_queue *traversed_commits,
1959 struct got_object_id *commit_id, const char *path,
1960 struct got_repository *repo)
1962 const struct got_error *err = NULL;
1963 struct got_pack *pack = NULL;
1964 struct got_packidx *packidx = NULL;
1965 char *path_packfile = NULL;
1966 struct got_commit_object *changed_commit = NULL;
1967 struct got_object_id *changed_commit_id = NULL;
1968 int idx;
1970 err = got_repo_search_packidx(&packidx, &idx, repo, commit_id);
1971 if (err) {
1972 if (err->code != GOT_ERR_NO_OBJ)
1973 return err;
1974 return NULL;
1977 err = get_packfile_path(&path_packfile, packidx);
1978 if (err)
1979 return err;
1981 pack = got_repo_get_cached_pack(repo, path_packfile);
1982 if (pack == NULL) {
1983 err = got_repo_cache_pack(&pack, repo, path_packfile, packidx);
1984 if (err)
1985 goto done;
1988 if (pack->privsep_child == NULL) {
1989 err = start_pack_privsep_child(pack, packidx);
1990 if (err)
1991 goto done;
1994 err = got_privsep_send_commit_traversal_request(
1995 pack->privsep_child->ibuf, commit_id, idx, path);
1996 if (err)
1997 goto done;
1999 err = got_privsep_recv_traversed_commits(&changed_commit,
2000 &changed_commit_id, traversed_commits, pack->privsep_child->ibuf);
2001 if (err)
2002 goto done;
2004 if (changed_commit) {
2006 * Cache the commit in which the path was changed.
2007 * This commit might be opened again soon.
2009 changed_commit->refcnt++;
2010 err = got_repo_cache_commit(repo, changed_commit_id,
2011 changed_commit);
2012 got_object_commit_close(changed_commit);
2014 done:
2015 free(path_packfile);
2016 free(changed_commit_id);
2017 return err;