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 <libgen.h>
36 #include <limits.h>
37 #include <imsg.h>
38 #include <time.h>
40 #include "got_error.h"
41 #include "got_object.h"
42 #include "got_repository.h"
43 #include "got_opentemp.h"
44 #include "got_path.h"
46 #include "got_lib_sha1.h"
47 #include "got_lib_delta.h"
48 #include "got_lib_inflate.h"
49 #include "got_lib_object.h"
50 #include "got_lib_privsep.h"
51 #include "got_lib_object_idcache.h"
52 #include "got_lib_object_cache.h"
53 #include "got_lib_object_parse.h"
54 #include "got_lib_pack.h"
55 #include "got_lib_repository.h"
57 #ifndef MIN
58 #define MIN(_a,_b) ((_a) < (_b) ? (_a) : (_b))
59 #endif
61 struct got_object_id *
62 got_object_get_id(struct got_object *obj)
63 {
64 return &obj->id;
65 }
67 const struct got_error *
68 got_object_get_id_str(char **outbuf, struct got_object *obj)
69 {
70 return got_object_id_str(outbuf, &obj->id);
71 }
73 const struct got_error *
74 got_object_get_type(int *type, struct got_repository *repo,
75 struct got_object_id *id)
76 {
77 const struct got_error *err = NULL;
78 struct got_object *obj;
80 err = got_object_open(&obj, repo, id);
81 if (err)
82 return err;
84 switch (obj->type) {
85 case GOT_OBJ_TYPE_COMMIT:
86 case GOT_OBJ_TYPE_TREE:
87 case GOT_OBJ_TYPE_BLOB:
88 case GOT_OBJ_TYPE_TAG:
89 *type = obj->type;
90 break;
91 default:
92 err = got_error(GOT_ERR_OBJ_TYPE);
93 break;
94 }
96 got_object_close(obj);
97 return err;
98 }
100 const struct got_error *
101 got_object_get_path(char **path, struct got_object_id *id,
102 struct got_repository *repo)
104 const struct got_error *err = NULL;
105 char *hex = NULL;
106 char *path_objects;
108 *path = NULL;
110 path_objects = got_repo_get_path_objects(repo);
111 if (path_objects == NULL)
112 return got_error_from_errno("got_repo_get_path_objects");
114 err = got_object_id_str(&hex, id);
115 if (err)
116 goto done;
118 if (asprintf(path, "%s/%.2x/%s", path_objects,
119 id->sha1[0], hex + 2) == -1)
120 err = got_error_from_errno("asprintf");
122 done:
123 free(hex);
124 free(path_objects);
125 return err;
128 static const struct got_error *
129 open_loose_object(int *fd, struct got_object_id *id,
130 struct got_repository *repo)
132 const struct got_error *err = NULL;
133 char *path;
135 err = got_object_get_path(&path, id, repo);
136 if (err)
137 return err;
138 *fd = open(path, O_RDONLY | O_NOFOLLOW);
139 if (*fd == -1) {
140 err = got_error_from_errno2("open", path);
141 goto done;
143 done:
144 free(path);
145 return err;
148 static const struct got_error *
149 get_packfile_path(char **path_packfile, struct got_packidx *packidx)
151 size_t size;
153 /* Packfile path contains ".pack" instead of ".idx", so add one byte. */
154 size = strlen(packidx->path_packidx) + 2;
155 if (size < GOT_PACKFILE_NAMELEN + 1)
156 return got_error_path(packidx->path_packidx, GOT_ERR_BAD_PATH);
158 *path_packfile = malloc(size);
159 if (*path_packfile == NULL)
160 return got_error_from_errno("malloc");
162 /* Copy up to and excluding ".idx". */
163 if (strlcpy(*path_packfile, packidx->path_packidx,
164 size - strlen(GOT_PACKIDX_SUFFIX) - 1) >= size)
165 return got_error(GOT_ERR_NO_SPACE);
167 if (strlcat(*path_packfile, GOT_PACKFILE_SUFFIX, size) >= size)
168 return got_error(GOT_ERR_NO_SPACE);
170 return NULL;
173 static const struct got_error *
174 request_packed_object(struct got_object **obj, struct got_pack *pack, int idx,
175 struct got_object_id *id)
177 const struct got_error *err = NULL;
178 struct imsgbuf *ibuf = pack->privsep_child->ibuf;
180 err = got_privsep_send_packed_obj_req(ibuf, idx, id);
181 if (err)
182 return err;
184 err = got_privsep_recv_obj(obj, ibuf);
185 if (err)
186 return err;
188 memcpy(&(*obj)->id, id, sizeof((*obj)->id));
190 return NULL;
193 static void
194 set_max_datasize(void)
196 struct rlimit rl;
198 if (getrlimit(RLIMIT_DATA, &rl) != 0)
199 return;
201 rl.rlim_cur = rl.rlim_max;
202 setrlimit(RLIMIT_DATA, &rl);
205 static const struct got_error *
206 start_pack_privsep_child(struct got_pack *pack, struct got_packidx *packidx)
208 const struct got_error *err = NULL;
209 int imsg_fds[2];
210 pid_t pid;
211 struct imsgbuf *ibuf;
213 ibuf = calloc(1, sizeof(*ibuf));
214 if (ibuf == NULL)
215 return got_error_from_errno("calloc");
217 pack->privsep_child = calloc(1, sizeof(*pack->privsep_child));
218 if (pack->privsep_child == NULL) {
219 err = got_error_from_errno("calloc");
220 free(ibuf);
221 return err;
224 if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_fds) == -1) {
225 err = got_error_from_errno("socketpair");
226 goto done;
229 pid = fork();
230 if (pid == -1) {
231 err = got_error_from_errno("fork");
232 goto done;
233 } else if (pid == 0) {
234 set_max_datasize();
235 got_privsep_exec_child(imsg_fds, GOT_PATH_PROG_READ_PACK,
236 pack->path_packfile);
237 /* not reached */
240 if (close(imsg_fds[1]) != 0)
241 return got_error_from_errno("close");
242 pack->privsep_child->imsg_fd = imsg_fds[0];
243 pack->privsep_child->pid = pid;
244 imsg_init(ibuf, imsg_fds[0]);
245 pack->privsep_child->ibuf = ibuf;
247 err = got_privsep_init_pack_child(ibuf, pack, packidx);
248 if (err) {
249 const struct got_error *child_err;
250 err = got_privsep_send_stop(pack->privsep_child->imsg_fd);
251 child_err = got_privsep_wait_for_child(
252 pack->privsep_child->pid);
253 if (child_err && err == NULL)
254 err = child_err;
256 done:
257 if (err) {
258 free(ibuf);
259 free(pack->privsep_child);
260 pack->privsep_child = NULL;
262 return err;
265 static const struct got_error *
266 read_packed_object_privsep(struct got_object **obj,
267 struct got_repository *repo, struct got_pack *pack,
268 struct got_packidx *packidx, int idx, struct got_object_id *id)
270 const struct got_error *err = NULL;
272 if (pack->privsep_child)
273 return request_packed_object(obj, pack, idx, id);
275 err = start_pack_privsep_child(pack, packidx);
276 if (err)
277 return err;
279 return request_packed_object(obj, pack, idx, id);
283 static const struct got_error *
284 open_packed_object(struct got_object **obj, struct got_object_id *id,
285 struct got_repository *repo)
287 const struct got_error *err = NULL;
288 struct got_pack *pack = NULL;
289 struct got_packidx *packidx = NULL;
290 int idx;
291 char *path_packfile;
293 err = got_repo_search_packidx(&packidx, &idx, repo, id);
294 if (err)
295 return err;
297 err = get_packfile_path(&path_packfile, packidx);
298 if (err)
299 return err;
301 pack = got_repo_get_cached_pack(repo, path_packfile);
302 if (pack == NULL) {
303 err = got_repo_cache_pack(&pack, repo, path_packfile, packidx);
304 if (err)
305 goto done;
308 err = read_packed_object_privsep(obj, repo, pack, packidx, idx, id);
309 if (err)
310 goto done;
311 done:
312 free(path_packfile);
313 return err;
316 static const struct got_error *
317 request_object(struct got_object **obj, struct got_repository *repo, int fd)
319 const struct got_error *err = NULL;
320 struct imsgbuf *ibuf;
322 ibuf = repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_OBJECT].ibuf;
324 err = got_privsep_send_obj_req(ibuf, fd);
325 if (err)
326 return err;
328 return got_privsep_recv_obj(obj, ibuf);
331 static const struct got_error *
332 read_object_header_privsep(struct got_object **obj, struct got_repository *repo,
333 int obj_fd)
335 const struct got_error *err;
336 int imsg_fds[2];
337 pid_t pid;
338 struct imsgbuf *ibuf;
340 if (repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_OBJECT].imsg_fd != -1)
341 return request_object(obj, repo, obj_fd);
343 ibuf = calloc(1, sizeof(*ibuf));
344 if (ibuf == NULL)
345 return got_error_from_errno("calloc");
347 if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_fds) == -1) {
348 err = got_error_from_errno("socketpair");
349 free(ibuf);
350 return err;
353 pid = fork();
354 if (pid == -1) {
355 err = got_error_from_errno("fork");
356 free(ibuf);
357 return err;
359 else if (pid == 0) {
360 got_privsep_exec_child(imsg_fds, GOT_PATH_PROG_READ_OBJECT,
361 repo->path);
362 /* not reached */
365 if (close(imsg_fds[1]) != 0) {
366 err = got_error_from_errno("close");
367 free(ibuf);
368 return err;
370 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_OBJECT].imsg_fd =
371 imsg_fds[0];
372 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_OBJECT].pid = pid;
373 imsg_init(ibuf, imsg_fds[0]);
374 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_OBJECT].ibuf = ibuf;
376 return request_object(obj, repo, obj_fd);
380 const struct got_error *
381 got_object_open(struct got_object **obj, struct got_repository *repo,
382 struct got_object_id *id)
384 const struct got_error *err = NULL;
385 char *path;
386 int fd;
388 *obj = got_repo_get_cached_object(repo, id);
389 if (*obj != NULL) {
390 (*obj)->refcnt++;
391 return NULL;
394 err = open_packed_object(obj, id, repo);
395 if (err && err->code != GOT_ERR_NO_OBJ)
396 return err;
397 if (*obj) {
398 (*obj)->refcnt++;
399 return got_repo_cache_object(repo, id, *obj);
402 err = got_object_get_path(&path, id, repo);
403 if (err)
404 return err;
406 fd = open(path, O_RDONLY | O_NOFOLLOW);
407 if (fd == -1) {
408 if (errno == ENOENT)
409 err = got_error_no_obj(id);
410 else
411 err = got_error_from_errno2("open", path);
412 goto done;
413 } else {
414 err = read_object_header_privsep(obj, repo, fd);
415 if (err)
416 goto done;
417 memcpy((*obj)->id.sha1, id->sha1, SHA1_DIGEST_LENGTH);
420 (*obj)->refcnt++;
421 err = got_repo_cache_object(repo, id, *obj);
422 done:
423 free(path);
424 return err;
428 const struct got_error *
429 got_object_open_by_id_str(struct got_object **obj, struct got_repository *repo,
430 const char *id_str)
432 struct got_object_id id;
434 if (!got_parse_sha1_digest(id.sha1, id_str))
435 return got_error_path(id_str, GOT_ERR_BAD_OBJ_ID_STR);
437 return got_object_open(obj, repo, &id);
440 const struct got_error *
441 got_object_resolve_id_str(struct got_object_id **id,
442 struct got_repository *repo, const char *id_str)
444 const struct got_error *err = NULL;
445 struct got_object *obj;
447 err = got_object_open_by_id_str(&obj, repo, id_str);
448 if (err)
449 return err;
451 *id = got_object_id_dup(got_object_get_id(obj));
452 got_object_close(obj);
453 if (*id == NULL)
454 return got_error_from_errno("got_object_id_dup");
456 return NULL;
459 static const struct got_error *
460 request_packed_commit(struct got_commit_object **commit, struct got_pack *pack,
461 int pack_idx, struct got_object_id *id)
463 const struct got_error *err = NULL;
465 err = got_privsep_send_commit_req(pack->privsep_child->ibuf, -1, id,
466 pack_idx);
467 if (err)
468 return err;
470 err = got_privsep_recv_commit(commit, pack->privsep_child->ibuf);
471 if (err)
472 return err;
474 (*commit)->flags |= GOT_COMMIT_FLAG_PACKED;
475 return NULL;
478 static const struct got_error *
479 read_packed_commit_privsep(struct got_commit_object **commit,
480 struct got_pack *pack, struct got_packidx *packidx, int idx,
481 struct got_object_id *id)
483 const struct got_error *err = NULL;
485 if (pack->privsep_child)
486 return request_packed_commit(commit, pack, idx, id);
488 err = start_pack_privsep_child(pack, packidx);
489 if (err)
490 return err;
492 return request_packed_commit(commit, pack, idx, id);
495 static const struct got_error *
496 request_commit(struct got_commit_object **commit, struct got_repository *repo,
497 int fd)
499 const struct got_error *err = NULL;
500 struct imsgbuf *ibuf;
502 ibuf = repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_COMMIT].ibuf;
504 err = got_privsep_send_commit_req(ibuf, fd, NULL, -1);
505 if (err)
506 return err;
508 return got_privsep_recv_commit(commit, ibuf);
511 static const struct got_error *
512 read_commit_privsep(struct got_commit_object **commit, int obj_fd,
513 struct got_repository *repo)
515 const struct got_error *err;
516 int imsg_fds[2];
517 pid_t pid;
518 struct imsgbuf *ibuf;
520 if (repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_COMMIT].imsg_fd != -1)
521 return request_commit(commit, repo, obj_fd);
523 ibuf = calloc(1, sizeof(*ibuf));
524 if (ibuf == NULL)
525 return got_error_from_errno("calloc");
527 if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_fds) == -1) {
528 err = got_error_from_errno("socketpair");
529 free(ibuf);
530 return err;
533 pid = fork();
534 if (pid == -1) {
535 err = got_error_from_errno("fork");
536 free(ibuf);
537 return err;
539 else if (pid == 0) {
540 got_privsep_exec_child(imsg_fds, GOT_PATH_PROG_READ_COMMIT,
541 repo->path);
542 /* not reached */
545 if (close(imsg_fds[1]) != 0) {
546 err = got_error_from_errno("close");
547 free(ibuf);
548 return err;
550 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_COMMIT].imsg_fd =
551 imsg_fds[0];
552 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_COMMIT].pid = pid;
553 imsg_init(ibuf, imsg_fds[0]);
554 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_COMMIT].ibuf = ibuf;
556 return request_commit(commit, repo, obj_fd);
560 static const struct got_error *
561 open_commit(struct got_commit_object **commit,
562 struct got_repository *repo, struct got_object_id *id, int check_cache)
564 const struct got_error *err = NULL;
565 struct got_packidx *packidx = NULL;
566 int idx;
567 char *path_packfile = NULL;
569 if (check_cache) {
570 *commit = got_repo_get_cached_commit(repo, id);
571 if (*commit != NULL) {
572 (*commit)->refcnt++;
573 return NULL;
575 } else
576 *commit = NULL;
578 err = got_repo_search_packidx(&packidx, &idx, repo, id);
579 if (err == NULL) {
580 struct got_pack *pack = NULL;
582 err = get_packfile_path(&path_packfile, packidx);
583 if (err)
584 return err;
586 pack = got_repo_get_cached_pack(repo, path_packfile);
587 if (pack == NULL) {
588 err = got_repo_cache_pack(&pack, repo, path_packfile,
589 packidx);
590 if (err)
591 goto done;
593 err = read_packed_commit_privsep(commit, pack,
594 packidx, idx, id);
595 } else if (err->code == GOT_ERR_NO_OBJ) {
596 int fd;
598 err = open_loose_object(&fd, id, repo);
599 if (err)
600 return err;
601 err = read_commit_privsep(commit, fd, repo);
604 if (err == NULL) {
605 (*commit)->refcnt++;
606 err = got_repo_cache_commit(repo, id, *commit);
608 done:
609 free(path_packfile);
610 return err;
613 const struct got_error *
614 got_object_open_as_commit(struct got_commit_object **commit,
615 struct got_repository *repo, struct got_object_id *id)
617 *commit = got_repo_get_cached_commit(repo, id);
618 if (*commit != NULL) {
619 (*commit)->refcnt++;
620 return NULL;
623 return open_commit(commit, repo, id, 0);
626 const struct got_error *
627 got_object_commit_open(struct got_commit_object **commit,
628 struct got_repository *repo, struct got_object *obj)
630 return open_commit(commit, repo, got_object_get_id(obj), 1);
633 const struct got_error *
634 got_object_qid_alloc(struct got_object_qid **qid, struct got_object_id *id)
636 const struct got_error *err = NULL;
638 *qid = calloc(1, sizeof(**qid));
639 if (*qid == NULL)
640 return got_error_from_errno("calloc");
642 (*qid)->id = got_object_id_dup(id);
643 if ((*qid)->id == NULL) {
644 err = got_error_from_errno("got_object_id_dup");
645 got_object_qid_free(*qid);
646 *qid = NULL;
647 return err;
650 return NULL;
653 static const struct got_error *
654 request_packed_tree(struct got_tree_object **tree, struct got_pack *pack,
655 int pack_idx, struct got_object_id *id)
657 const struct got_error *err = NULL;
659 err = got_privsep_send_tree_req(pack->privsep_child->ibuf, -1, id,
660 pack_idx);
661 if (err)
662 return err;
664 return got_privsep_recv_tree(tree, pack->privsep_child->ibuf);
667 static const struct got_error *
668 read_packed_tree_privsep(struct got_tree_object **tree,
669 struct got_pack *pack, struct got_packidx *packidx, int idx,
670 struct got_object_id *id)
672 const struct got_error *err = NULL;
674 if (pack->privsep_child)
675 return request_packed_tree(tree, pack, idx, id);
677 err = start_pack_privsep_child(pack, packidx);
678 if (err)
679 return err;
681 return request_packed_tree(tree, pack, idx, id);
684 static const struct got_error *
685 request_tree(struct got_tree_object **tree, struct got_repository *repo,
686 int fd)
688 const struct got_error *err = NULL;
689 struct imsgbuf *ibuf;
691 ibuf = repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TREE].ibuf;
693 err = got_privsep_send_tree_req(ibuf, fd, NULL, -1);
694 if (err)
695 return err;
697 return got_privsep_recv_tree(tree, ibuf);
700 const struct got_error *
701 read_tree_privsep(struct got_tree_object **tree, int obj_fd,
702 struct got_repository *repo)
704 const struct got_error *err;
705 int imsg_fds[2];
706 pid_t pid;
707 struct imsgbuf *ibuf;
709 if (repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TREE].imsg_fd != -1)
710 return request_tree(tree, repo, obj_fd);
712 ibuf = calloc(1, sizeof(*ibuf));
713 if (ibuf == NULL)
714 return got_error_from_errno("calloc");
716 if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_fds) == -1) {
717 err = got_error_from_errno("socketpair");
718 free(ibuf);
719 return err;
722 pid = fork();
723 if (pid == -1) {
724 err = got_error_from_errno("fork");
725 free(ibuf);
726 return err;
728 else if (pid == 0) {
729 got_privsep_exec_child(imsg_fds, GOT_PATH_PROG_READ_TREE,
730 repo->path);
731 /* not reached */
734 if (close(imsg_fds[1]) != 0) {
735 err = got_error_from_errno("close");
736 free(ibuf);
737 return err;
739 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TREE].imsg_fd =
740 imsg_fds[0];
741 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TREE].pid = pid;
742 imsg_init(ibuf, imsg_fds[0]);
743 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TREE].ibuf = ibuf;
746 return request_tree(tree, repo, obj_fd);
749 static const struct got_error *
750 open_tree(struct got_tree_object **tree, struct got_repository *repo,
751 struct got_object_id *id, int check_cache)
753 const struct got_error *err = NULL;
754 struct got_packidx *packidx = NULL;
755 int idx;
756 char *path_packfile = NULL;
758 if (check_cache) {
759 *tree = got_repo_get_cached_tree(repo, id);
760 if (*tree != NULL) {
761 (*tree)->refcnt++;
762 return NULL;
764 } else
765 *tree = NULL;
767 err = got_repo_search_packidx(&packidx, &idx, repo, id);
768 if (err == NULL) {
769 struct got_pack *pack = NULL;
771 err = get_packfile_path(&path_packfile, packidx);
772 if (err)
773 return err;
775 pack = got_repo_get_cached_pack(repo, path_packfile);
776 if (pack == NULL) {
777 err = got_repo_cache_pack(&pack, repo, path_packfile,
778 packidx);
779 if (err)
780 goto done;
782 err = read_packed_tree_privsep(tree, pack,
783 packidx, idx, id);
784 } else if (err->code == GOT_ERR_NO_OBJ) {
785 int fd;
787 err = open_loose_object(&fd, id, repo);
788 if (err)
789 return err;
790 err = read_tree_privsep(tree, fd, repo);
793 if (err == NULL) {
794 (*tree)->refcnt++;
795 err = got_repo_cache_tree(repo, id, *tree);
797 done:
798 free(path_packfile);
799 return err;
802 const struct got_error *
803 got_object_open_as_tree(struct got_tree_object **tree,
804 struct got_repository *repo, struct got_object_id *id)
806 *tree = got_repo_get_cached_tree(repo, id);
807 if (*tree != NULL) {
808 (*tree)->refcnt++;
809 return NULL;
812 return open_tree(tree, repo, id, 0);
815 const struct got_error *
816 got_object_tree_open(struct got_tree_object **tree,
817 struct got_repository *repo, struct got_object *obj)
819 return open_tree(tree, repo, got_object_get_id(obj), 1);
822 int
823 got_object_tree_get_nentries(struct got_tree_object *tree)
825 return tree->nentries;
828 struct got_tree_entry *
829 got_object_tree_get_first_entry(struct got_tree_object *tree)
831 return got_object_tree_get_entry(tree, 0);
834 struct got_tree_entry *
835 got_object_tree_get_last_entry(struct got_tree_object *tree)
837 return got_object_tree_get_entry(tree, tree->nentries - 1);
840 struct got_tree_entry *
841 got_object_tree_get_entry(struct got_tree_object *tree, int i)
843 if (i < 0 || i >= tree->nentries)
844 return NULL;
845 return &tree->entries[i];
848 mode_t
849 got_tree_entry_get_mode(struct got_tree_entry *te)
851 return te->mode;
854 const char *
855 got_tree_entry_get_name(struct got_tree_entry *te)
857 return &te->name[0];
860 struct got_object_id *
861 got_tree_entry_get_id(struct got_tree_entry *te)
863 return &te->id;
866 const struct got_error *
867 got_object_blob_read_to_str(char **s, struct got_blob_object *blob)
869 const struct got_error *err = NULL;
870 size_t len, totlen, hdrlen, offset;
872 hdrlen = got_object_blob_get_hdrlen(blob);
873 totlen = 0;
874 offset = 0;
875 do {
876 char *p;
878 err = got_object_blob_read_block(&len, blob);
879 if (err)
880 return err;
882 if (len == 0)
883 break;
885 totlen += len - hdrlen;
886 p = realloc(*s, totlen + 1);
887 if (p == NULL) {
888 err = got_error_from_errno("realloc");
889 free(*s);
890 *s = NULL;
891 return err;
893 *s = p;
894 /* Skip blob object header first time around. */
895 memcpy(*s + offset,
896 got_object_blob_get_read_buf(blob) + hdrlen, len - hdrlen);
897 hdrlen = 0;
898 offset = totlen;
899 } while (len > 0);
901 (*s)[totlen] = '\0';
902 return NULL;
905 const struct got_error *
906 got_tree_entry_get_symlink_target(char **link_target, struct got_tree_entry *te,
907 struct got_repository *repo)
909 const struct got_error *err = NULL;
910 struct got_blob_object *blob = NULL;
912 *link_target = NULL;
914 if (!got_object_tree_entry_is_symlink(te))
915 return got_error(GOT_ERR_TREE_ENTRY_TYPE);
917 err = got_object_open_as_blob(&blob, repo,
918 got_tree_entry_get_id(te), PATH_MAX);
919 if (err)
920 return err;
922 err = got_object_blob_read_to_str(link_target, blob);
923 got_object_blob_close(blob);
924 if (err) {
925 free(*link_target);
926 *link_target = NULL;
928 return err;
931 int
932 got_tree_entry_get_index(struct got_tree_entry *te)
934 return te->idx;
937 struct got_tree_entry *
938 got_tree_entry_get_next(struct got_tree_object *tree,
939 struct got_tree_entry *te)
941 return got_object_tree_get_entry(tree, te->idx + 1);
944 struct got_tree_entry *
945 got_tree_entry_get_prev(struct got_tree_object *tree,
946 struct got_tree_entry *te)
948 return got_object_tree_get_entry(tree, te->idx - 1);
951 static const struct got_error *
952 request_packed_blob(uint8_t **outbuf, size_t *size, size_t *hdrlen, int outfd,
953 struct got_pack *pack, struct got_packidx *packidx, int idx,
954 struct got_object_id *id)
956 const struct got_error *err = NULL;
957 int outfd_child;
958 int basefd, accumfd; /* temporary files for delta application */
960 basefd = got_opentempfd();
961 if (basefd == -1)
962 return got_error_from_errno("got_opentempfd");
963 accumfd = got_opentempfd();
964 if (accumfd == -1)
965 return got_error_from_errno("got_opentempfd");
967 outfd_child = dup(outfd);
968 if (outfd_child == -1)
969 return got_error_from_errno("dup");
971 err = got_privsep_send_blob_req(pack->privsep_child->ibuf, -1, id, idx);
972 if (err)
973 return err;
975 err = got_privsep_send_blob_outfd(pack->privsep_child->ibuf,
976 outfd_child);
977 if (err) {
978 close(basefd);
979 close(accumfd);
980 return err;
983 err = got_privsep_send_tmpfd(pack->privsep_child->ibuf,
984 basefd);
985 if (err) {
986 close(accumfd);
987 return err;
990 err = got_privsep_send_tmpfd(pack->privsep_child->ibuf,
991 accumfd);
992 if (err)
993 return err;
995 err = got_privsep_recv_blob(outbuf, size, hdrlen,
996 pack->privsep_child->ibuf);
997 if (err)
998 return err;
1000 if (lseek(outfd, SEEK_SET, 0) == -1)
1001 err = got_error_from_errno("lseek");
1003 return err;
1006 static const struct got_error *
1007 read_packed_blob_privsep(uint8_t **outbuf, size_t *size, size_t *hdrlen,
1008 int outfd, struct got_pack *pack, struct got_packidx *packidx, int idx,
1009 struct got_object_id *id)
1011 const struct got_error *err = NULL;
1013 if (pack->privsep_child == NULL) {
1014 err = start_pack_privsep_child(pack, packidx);
1015 if (err)
1016 return err;
1019 return request_packed_blob(outbuf, size, hdrlen, outfd, pack, packidx,
1020 idx, id);
1023 static const struct got_error *
1024 request_blob(uint8_t **outbuf, size_t *size, size_t *hdrlen, int outfd,
1025 int infd, struct imsgbuf *ibuf)
1027 const struct got_error *err = NULL;
1028 int outfd_child;
1030 outfd_child = dup(outfd);
1031 if (outfd_child == -1)
1032 return got_error_from_errno("dup");
1034 err = got_privsep_send_blob_req(ibuf, infd, NULL, -1);
1035 if (err)
1036 return err;
1038 err = got_privsep_send_blob_outfd(ibuf, outfd_child);
1039 if (err)
1040 return err;
1042 err = got_privsep_recv_blob(outbuf, size, hdrlen, ibuf);
1043 if (err)
1044 return err;
1046 if (lseek(outfd, SEEK_SET, 0) == -1)
1047 return got_error_from_errno("lseek");
1049 return err;
1052 static const struct got_error *
1053 read_blob_privsep(uint8_t **outbuf, size_t *size, size_t *hdrlen,
1054 int outfd, int infd, struct got_repository *repo)
1056 const struct got_error *err;
1057 int imsg_fds[2];
1058 pid_t pid;
1059 struct imsgbuf *ibuf;
1061 if (repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_BLOB].imsg_fd != -1) {
1062 ibuf = repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_BLOB].ibuf;
1063 return request_blob(outbuf, size, hdrlen, outfd, infd, ibuf);
1066 ibuf = calloc(1, sizeof(*ibuf));
1067 if (ibuf == NULL)
1068 return got_error_from_errno("calloc");
1070 if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_fds) == -1) {
1071 err = got_error_from_errno("socketpair");
1072 free(ibuf);
1073 return err;
1076 pid = fork();
1077 if (pid == -1) {
1078 err = got_error_from_errno("fork");
1079 free(ibuf);
1080 return err;
1082 else if (pid == 0) {
1083 got_privsep_exec_child(imsg_fds, GOT_PATH_PROG_READ_BLOB,
1084 repo->path);
1085 /* not reached */
1088 if (close(imsg_fds[1]) != 0) {
1089 err = got_error_from_errno("close");
1090 free(ibuf);
1091 return err;
1093 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_BLOB].imsg_fd =
1094 imsg_fds[0];
1095 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_BLOB].pid = pid;
1096 imsg_init(ibuf, imsg_fds[0]);
1097 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_BLOB].ibuf = ibuf;
1099 return request_blob(outbuf, size, hdrlen, outfd, infd, ibuf);
1102 static const struct got_error *
1103 open_blob(struct got_blob_object **blob, struct got_repository *repo,
1104 struct got_object_id *id, size_t blocksize)
1106 const struct got_error *err = NULL;
1107 struct got_packidx *packidx = NULL;
1108 int idx;
1109 char *path_packfile = NULL;
1110 uint8_t *outbuf;
1111 int outfd;
1112 size_t size, hdrlen;
1113 struct stat sb;
1115 *blob = calloc(1, sizeof(**blob));
1116 if (*blob == NULL)
1117 return got_error_from_errno("calloc");
1119 outfd = got_opentempfd();
1120 if (outfd == -1)
1121 return got_error_from_errno("got_opentempfd");
1123 (*blob)->read_buf = malloc(blocksize);
1124 if ((*blob)->read_buf == NULL) {
1125 err = got_error_from_errno("malloc");
1126 goto done;
1129 err = got_repo_search_packidx(&packidx, &idx, repo, id);
1130 if (err == NULL) {
1131 struct got_pack *pack = NULL;
1133 err = get_packfile_path(&path_packfile, packidx);
1134 if (err)
1135 goto done;
1137 pack = got_repo_get_cached_pack(repo, path_packfile);
1138 if (pack == NULL) {
1139 err = got_repo_cache_pack(&pack, repo, path_packfile,
1140 packidx);
1141 if (err)
1142 goto done;
1144 err = read_packed_blob_privsep(&outbuf, &size, &hdrlen, outfd,
1145 pack, packidx, idx, id);
1146 } else if (err->code == GOT_ERR_NO_OBJ) {
1147 int infd;
1149 err = open_loose_object(&infd, id, repo);
1150 if (err)
1151 goto done;
1152 err = read_blob_privsep(&outbuf, &size, &hdrlen, outfd, infd,
1153 repo);
1155 if (err)
1156 goto done;
1158 if (hdrlen > size) {
1159 err = got_error(GOT_ERR_BAD_OBJ_HDR);
1160 goto done;
1163 if (outbuf) {
1164 if (close(outfd) != 0 && err == NULL)
1165 err = got_error_from_errno("close");
1166 outfd = -1;
1167 (*blob)->f = fmemopen(outbuf, size, "rb");
1168 if ((*blob)->f == NULL) {
1169 err = got_error_from_errno("fmemopen");
1170 free(outbuf);
1171 goto done;
1173 (*blob)->data = outbuf;
1174 } else {
1175 if (fstat(outfd, &sb) == -1) {
1176 err = got_error_from_errno("fstat");
1177 goto done;
1180 if (sb.st_size != size) {
1181 err = got_error(GOT_ERR_PRIVSEP_LEN);
1182 goto done;
1185 (*blob)->f = fdopen(outfd, "rb");
1186 if ((*blob)->f == NULL) {
1187 err = got_error_from_errno("fdopen");
1188 close(outfd);
1189 outfd = -1;
1190 goto done;
1194 (*blob)->hdrlen = hdrlen;
1195 (*blob)->blocksize = blocksize;
1196 memcpy(&(*blob)->id.sha1, id->sha1, SHA1_DIGEST_LENGTH);
1198 done:
1199 free(path_packfile);
1200 if (err) {
1201 if (*blob) {
1202 got_object_blob_close(*blob);
1203 *blob = NULL;
1204 } else if (outfd != -1)
1205 close(outfd);
1207 return err;
1210 const struct got_error *
1211 got_object_open_as_blob(struct got_blob_object **blob,
1212 struct got_repository *repo, struct got_object_id *id,
1213 size_t blocksize)
1215 return open_blob(blob, repo, id, blocksize);
1218 const struct got_error *
1219 got_object_blob_open(struct got_blob_object **blob,
1220 struct got_repository *repo, struct got_object *obj, size_t blocksize)
1222 return open_blob(blob, repo, got_object_get_id(obj), blocksize);
1225 const struct got_error *
1226 got_object_blob_close(struct got_blob_object *blob)
1228 const struct got_error *err = NULL;
1229 free(blob->read_buf);
1230 if (blob->f && fclose(blob->f) != 0)
1231 err = got_error_from_errno("fclose");
1232 free(blob->data);
1233 free(blob);
1234 return err;
1237 void
1238 got_object_blob_rewind(struct got_blob_object *blob)
1240 if (blob->f)
1241 rewind(blob->f);
1244 char *
1245 got_object_blob_id_str(struct got_blob_object *blob, char *buf, size_t size)
1247 return got_sha1_digest_to_str(blob->id.sha1, buf, size);
1250 size_t
1251 got_object_blob_get_hdrlen(struct got_blob_object *blob)
1253 return blob->hdrlen;
1256 const uint8_t *
1257 got_object_blob_get_read_buf(struct got_blob_object *blob)
1259 return blob->read_buf;
1262 const struct got_error *
1263 got_object_blob_read_block(size_t *outlenp, struct got_blob_object *blob)
1265 size_t n;
1267 n = fread(blob->read_buf, 1, blob->blocksize, blob->f);
1268 if (n == 0 && ferror(blob->f))
1269 return got_ferror(blob->f, GOT_ERR_IO);
1270 *outlenp = n;
1271 return NULL;
1274 const struct got_error *
1275 got_object_blob_dump_to_file(size_t *filesize, int *nlines,
1276 off_t **line_offsets, FILE *outfile, struct got_blob_object *blob)
1278 const struct got_error *err = NULL;
1279 size_t n, len, hdrlen;
1280 const uint8_t *buf;
1281 int i;
1282 size_t noffsets = 0;
1283 off_t off = 0, total_len = 0;
1285 if (line_offsets)
1286 *line_offsets = NULL;
1287 if (filesize)
1288 *filesize = 0;
1289 if (nlines)
1290 *nlines = 0;
1292 hdrlen = got_object_blob_get_hdrlen(blob);
1293 do {
1294 err = got_object_blob_read_block(&len, blob);
1295 if (err)
1296 return err;
1297 if (len == 0)
1298 break;
1299 buf = got_object_blob_get_read_buf(blob);
1300 i = hdrlen;
1301 if (line_offsets && nlines) {
1302 if (*line_offsets == NULL) {
1303 /* Have some data but perhaps no '\n'. */
1304 noffsets = 1;
1305 *nlines = 1;
1306 *line_offsets = calloc(1, sizeof(**line_offsets));
1307 if (*line_offsets == NULL)
1308 return got_error_from_errno("calloc");
1310 /* Skip forward over end of first line. */
1311 while (i < len) {
1312 if (buf[i] == '\n')
1313 break;
1314 i++;
1317 /* Scan '\n' offsets in remaining chunk of data. */
1318 while (i < len) {
1319 if (buf[i] != '\n') {
1320 i++;
1321 continue;
1323 (*nlines)++;
1324 if (noffsets < *nlines) {
1325 off_t *o = recallocarray(*line_offsets,
1326 noffsets, *nlines,
1327 sizeof(**line_offsets));
1328 if (o == NULL) {
1329 free(*line_offsets);
1330 *line_offsets = NULL;
1331 return got_error_from_errno(
1332 "recallocarray");
1334 *line_offsets = o;
1335 noffsets = *nlines;
1337 off = total_len + i - hdrlen + 1;
1338 (*line_offsets)[*nlines - 1] = off;
1339 i++;
1342 /* Skip blob object header first time around. */
1343 n = fwrite(buf + hdrlen, 1, len - hdrlen, outfile);
1344 if (n != len - hdrlen)
1345 return got_ferror(outfile, GOT_ERR_IO);
1346 total_len += len - hdrlen;
1347 hdrlen = 0;
1348 } while (len != 0);
1350 if (fflush(outfile) != 0)
1351 return got_error_from_errno("fflush");
1352 rewind(outfile);
1354 if (filesize)
1355 *filesize = total_len;
1357 return NULL;
1360 static const struct got_error *
1361 request_packed_tag(struct got_tag_object **tag, struct got_pack *pack,
1362 int pack_idx, struct got_object_id *id)
1364 const struct got_error *err = NULL;
1366 err = got_privsep_send_tag_req(pack->privsep_child->ibuf, -1, id,
1367 pack_idx);
1368 if (err)
1369 return err;
1371 return got_privsep_recv_tag(tag, pack->privsep_child->ibuf);
1374 static const struct got_error *
1375 read_packed_tag_privsep(struct got_tag_object **tag,
1376 struct got_pack *pack, struct got_packidx *packidx, int idx,
1377 struct got_object_id *id)
1379 const struct got_error *err = NULL;
1381 if (pack->privsep_child)
1382 return request_packed_tag(tag, pack, idx, id);
1384 err = start_pack_privsep_child(pack, packidx);
1385 if (err)
1386 return err;
1388 return request_packed_tag(tag, pack, idx, id);
1391 static const struct got_error *
1392 request_tag(struct got_tag_object **tag, struct got_repository *repo,
1393 int fd)
1395 const struct got_error *err = NULL;
1396 struct imsgbuf *ibuf;
1398 ibuf = repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TAG].ibuf;
1400 err = got_privsep_send_tag_req(ibuf, fd, NULL, -1);
1401 if (err)
1402 return err;
1404 return got_privsep_recv_tag(tag, ibuf);
1407 static const struct got_error *
1408 read_tag_privsep(struct got_tag_object **tag, int obj_fd,
1409 struct got_repository *repo)
1411 const struct got_error *err;
1412 int imsg_fds[2];
1413 pid_t pid;
1414 struct imsgbuf *ibuf;
1416 if (repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TAG].imsg_fd != -1)
1417 return request_tag(tag, repo, obj_fd);
1419 ibuf = calloc(1, sizeof(*ibuf));
1420 if (ibuf == NULL)
1421 return got_error_from_errno("calloc");
1423 if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_fds) == -1) {
1424 err = got_error_from_errno("socketpair");
1425 free(ibuf);
1426 return err;
1429 pid = fork();
1430 if (pid == -1) {
1431 err = got_error_from_errno("fork");
1432 free(ibuf);
1433 return err;
1435 else if (pid == 0) {
1436 got_privsep_exec_child(imsg_fds, GOT_PATH_PROG_READ_TAG,
1437 repo->path);
1438 /* not reached */
1441 if (close(imsg_fds[1]) != 0) {
1442 err = got_error_from_errno("close");
1443 free(ibuf);
1444 return err;
1446 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TAG].imsg_fd =
1447 imsg_fds[0];
1448 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TAG].pid = pid;
1449 imsg_init(ibuf, imsg_fds[0]);
1450 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TAG].ibuf = ibuf;
1452 return request_tag(tag, repo, obj_fd);
1455 static const struct got_error *
1456 open_tag(struct got_tag_object **tag, struct got_repository *repo,
1457 struct got_object_id *id, int check_cache)
1459 const struct got_error *err = NULL;
1460 struct got_packidx *packidx = NULL;
1461 int idx;
1462 char *path_packfile = NULL;
1463 struct got_object *obj = NULL;
1464 int obj_type = GOT_OBJ_TYPE_ANY;
1466 if (check_cache) {
1467 *tag = got_repo_get_cached_tag(repo, id);
1468 if (*tag != NULL) {
1469 (*tag)->refcnt++;
1470 return NULL;
1472 } else
1473 *tag = NULL;
1475 err = got_repo_search_packidx(&packidx, &idx, repo, id);
1476 if (err == NULL) {
1477 struct got_pack *pack = NULL;
1479 err = get_packfile_path(&path_packfile, packidx);
1480 if (err)
1481 return err;
1483 pack = got_repo_get_cached_pack(repo, path_packfile);
1484 if (pack == NULL) {
1485 err = got_repo_cache_pack(&pack, repo, path_packfile,
1486 packidx);
1487 if (err)
1488 goto done;
1491 /* Beware of "lightweight" tags: Check object type first. */
1492 err = read_packed_object_privsep(&obj, repo, pack, packidx,
1493 idx, id);
1494 if (err)
1495 goto done;
1496 obj_type = obj->type;
1497 got_object_close(obj);
1498 if (obj_type != GOT_OBJ_TYPE_TAG) {
1499 err = got_error(GOT_ERR_OBJ_TYPE);
1500 goto done;
1502 err = read_packed_tag_privsep(tag, pack, packidx, idx, id);
1503 } else if (err->code == GOT_ERR_NO_OBJ) {
1504 int fd;
1506 err = open_loose_object(&fd, id, repo);
1507 if (err)
1508 return err;
1509 err = read_object_header_privsep(&obj, repo, fd);
1510 if (err)
1511 return err;
1512 obj_type = obj->type;
1513 got_object_close(obj);
1514 if (obj_type != GOT_OBJ_TYPE_TAG)
1515 return got_error(GOT_ERR_OBJ_TYPE);
1517 err = open_loose_object(&fd, id, repo);
1518 if (err)
1519 return err;
1520 err = read_tag_privsep(tag, fd, repo);
1523 if (err == NULL) {
1524 (*tag)->refcnt++;
1525 err = got_repo_cache_tag(repo, id, *tag);
1527 done:
1528 free(path_packfile);
1529 return err;
1532 const struct got_error *
1533 got_object_open_as_tag(struct got_tag_object **tag,
1534 struct got_repository *repo, struct got_object_id *id)
1536 *tag = got_repo_get_cached_tag(repo, id);
1537 if (*tag != NULL) {
1538 (*tag)->refcnt++;
1539 return NULL;
1542 return open_tag(tag, repo, id, 0);
1545 const struct got_error *
1546 got_object_tag_open(struct got_tag_object **tag,
1547 struct got_repository *repo, struct got_object *obj)
1549 return open_tag(tag, repo, got_object_get_id(obj), 1);
1552 const char *
1553 got_object_tag_get_name(struct got_tag_object *tag)
1555 return tag->tag;
1558 int
1559 got_object_tag_get_object_type(struct got_tag_object *tag)
1561 return tag->obj_type;
1564 struct got_object_id *
1565 got_object_tag_get_object_id(struct got_tag_object *tag)
1567 return &tag->id;
1570 time_t
1571 got_object_tag_get_tagger_time(struct got_tag_object *tag)
1573 return tag->tagger_time;
1576 time_t
1577 got_object_tag_get_tagger_gmtoff(struct got_tag_object *tag)
1579 return tag->tagger_gmtoff;
1582 const char *
1583 got_object_tag_get_tagger(struct got_tag_object *tag)
1585 return tag->tagger;
1588 const char *
1589 got_object_tag_get_message(struct got_tag_object *tag)
1591 return tag->tagmsg;
1594 static struct got_tree_entry *
1595 find_entry_by_name(struct got_tree_object *tree, const char *name, size_t len)
1597 int i;
1599 /* Note that tree entries are sorted in strncmp() order. */
1600 for (i = 0; i < tree->nentries; i++) {
1601 struct got_tree_entry *te = &tree->entries[i];
1602 int cmp = strncmp(te->name, name, len);
1603 if (cmp < 0)
1604 continue;
1605 if (cmp > 0)
1606 break;
1607 if (te->name[len] == '\0')
1608 return te;
1610 return NULL;
1613 struct got_tree_entry *
1614 got_object_tree_find_entry(struct got_tree_object *tree, const char *name)
1616 return find_entry_by_name(tree, name, strlen(name));
1619 const struct got_error *
1620 got_object_id_by_path(struct got_object_id **id, struct got_repository *repo,
1621 struct got_object_id *commit_id, const char *path)
1623 const struct got_error *err = NULL;
1624 struct got_commit_object *commit = NULL;
1625 struct got_tree_object *tree = NULL;
1626 struct got_tree_entry *te = NULL;
1627 const char *seg, *s;
1628 size_t seglen;
1630 *id = NULL;
1632 err = got_object_open_as_commit(&commit, repo, commit_id);
1633 if (err)
1634 goto done;
1636 /* Handle opening of root of commit's tree. */
1637 if (got_path_is_root_dir(path)) {
1638 *id = got_object_id_dup(commit->tree_id);
1639 if (*id == NULL)
1640 err = got_error_from_errno("got_object_id_dup");
1641 goto done;
1644 err = got_object_open_as_tree(&tree, repo, commit->tree_id);
1645 if (err)
1646 goto done;
1648 s = path;
1649 while (s[0] == '/')
1650 s++;
1651 seg = s;
1652 seglen = 0;
1653 while (*s) {
1654 struct got_tree_object *next_tree;
1656 if (*s != '/') {
1657 s++;
1658 seglen++;
1659 if (*s)
1660 continue;
1663 te = find_entry_by_name(tree, seg, seglen);
1664 if (te == NULL) {
1665 err = got_error(GOT_ERR_NO_TREE_ENTRY);
1666 goto done;
1669 if (*s == '\0')
1670 break;
1672 seg = s + 1;
1673 seglen = 0;
1674 s++;
1675 if (*s) {
1676 err = got_object_open_as_tree(&next_tree, repo,
1677 &te->id);
1678 te = NULL;
1679 if (err)
1680 goto done;
1681 got_object_tree_close(tree);
1682 tree = next_tree;
1686 if (te) {
1687 *id = got_object_id_dup(&te->id);
1688 if (*id == NULL)
1689 return got_error_from_errno("got_object_id_dup");
1690 } else
1691 err = got_error(GOT_ERR_NO_TREE_ENTRY);
1692 done:
1693 if (commit)
1694 got_object_commit_close(commit);
1695 if (tree)
1696 got_object_tree_close(tree);
1697 return err;
1701 * Normalize file mode bits to avoid false positive tree entry differences
1702 * in case tree entries have unexpected mode bits set.
1704 static mode_t
1705 normalize_mode_for_comparison(mode_t mode)
1708 * For directories, the only relevant bit is the IFDIR bit.
1709 * This allows us to detect paths changing from a directory
1710 * to a file and vice versa.
1712 if (S_ISDIR(mode))
1713 return mode & S_IFDIR;
1716 * For symlinks, the only relevant bit is the IFLNK bit.
1717 * This allows us to detect paths changing from a symlinks
1718 * to a file or directory and vice versa.
1720 if (S_ISLNK(mode))
1721 return mode & S_IFLNK;
1723 /* For files, the only change we care about is the executable bit. */
1724 return mode & S_IXUSR;
1727 const struct got_error *
1728 got_object_tree_path_changed(int *changed,
1729 struct got_tree_object *tree01, struct got_tree_object *tree02,
1730 const char *path, struct got_repository *repo)
1732 const struct got_error *err = NULL;
1733 struct got_tree_object *tree1 = NULL, *tree2 = NULL;
1734 struct got_tree_entry *te1 = NULL, *te2 = NULL;
1735 const char *seg, *s;
1736 size_t seglen;
1738 *changed = 0;
1740 /* We not do support comparing the root path. */
1741 if (got_path_is_root_dir(path))
1742 return got_error_path(path, GOT_ERR_BAD_PATH);
1744 tree1 = tree01;
1745 tree2 = tree02;
1746 s = path;
1747 while (*s == '/')
1748 s++;
1749 seg = s;
1750 seglen = 0;
1751 while (*s) {
1752 struct got_tree_object *next_tree1, *next_tree2;
1753 mode_t mode1, mode2;
1755 if (*s != '/') {
1756 s++;
1757 seglen++;
1758 if (*s)
1759 continue;
1762 te1 = find_entry_by_name(tree1, seg, seglen);
1763 if (te1 == NULL) {
1764 err = got_error(GOT_ERR_NO_OBJ);
1765 goto done;
1768 te2 = find_entry_by_name(tree2, seg, seglen);
1769 if (te2 == NULL) {
1770 *changed = 1;
1771 goto done;
1774 mode1 = normalize_mode_for_comparison(te1->mode);
1775 mode2 = normalize_mode_for_comparison(te2->mode);
1776 if (mode1 != mode2) {
1777 *changed = 1;
1778 goto done;
1781 if (got_object_id_cmp(&te1->id, &te2->id) == 0) {
1782 *changed = 0;
1783 goto done;
1786 if (*s == '\0') { /* final path element */
1787 *changed = 1;
1788 goto done;
1791 seg = s + 1;
1792 s++;
1793 seglen = 0;
1794 if (*s) {
1795 err = got_object_open_as_tree(&next_tree1, repo,
1796 &te1->id);
1797 te1 = NULL;
1798 if (err)
1799 goto done;
1800 if (tree1 != tree01)
1801 got_object_tree_close(tree1);
1802 tree1 = next_tree1;
1804 err = got_object_open_as_tree(&next_tree2, repo,
1805 &te2->id);
1806 te2 = NULL;
1807 if (err)
1808 goto done;
1809 if (tree2 != tree02)
1810 got_object_tree_close(tree2);
1811 tree2 = next_tree2;
1814 done:
1815 if (tree1 && tree1 != tree01)
1816 got_object_tree_close(tree1);
1817 if (tree2 && tree2 != tree02)
1818 got_object_tree_close(tree2);
1819 return err;
1822 const struct got_error *
1823 got_object_tree_entry_dup(struct got_tree_entry **new_te,
1824 struct got_tree_entry *te)
1826 const struct got_error *err = NULL;
1828 *new_te = calloc(1, sizeof(**new_te));
1829 if (*new_te == NULL)
1830 return got_error_from_errno("calloc");
1832 (*new_te)->mode = te->mode;
1833 memcpy((*new_te)->name, te->name, sizeof((*new_te)->name));
1834 memcpy(&(*new_te)->id, &te->id, sizeof((*new_te)->id));
1835 return err;
1838 int
1839 got_object_tree_entry_is_submodule(struct got_tree_entry *te)
1841 return (te->mode & S_IFMT) == (S_IFDIR | S_IFLNK);
1844 int
1845 got_object_tree_entry_is_symlink(struct got_tree_entry *te)
1847 /* S_IFDIR check avoids confusing symlinks with submodules. */
1848 return ((te->mode & (S_IFDIR | S_IFLNK)) == S_IFLNK);
1851 static const struct got_error *
1852 resolve_symlink(char **link_target, const char *path,
1853 struct got_object_id *commit_id, struct got_repository *repo)
1855 const struct got_error *err = NULL;
1856 char *name, *parent_path = NULL;
1857 struct got_object_id *tree_obj_id = NULL;
1858 struct got_tree_object *tree = NULL;
1859 struct got_tree_entry *te = NULL;
1861 *link_target = NULL;
1863 name = basename(path);
1864 if (name == NULL)
1865 return got_error_from_errno2("basename", path);
1867 err = got_path_dirname(&parent_path, path);
1868 if (err)
1869 return err;
1871 err = got_object_id_by_path(&tree_obj_id, repo, commit_id,
1872 parent_path);
1873 if (err) {
1874 if (err->code == GOT_ERR_NO_TREE_ENTRY) {
1875 /* Display the complete path in error message. */
1876 err = got_error_path(path, err->code);
1878 goto done;
1881 err = got_object_open_as_tree(&tree, repo, tree_obj_id);
1882 if (err)
1883 goto done;
1885 te = got_object_tree_find_entry(tree, name);
1886 if (te == NULL) {
1887 err = got_error_path(path, GOT_ERR_NO_TREE_ENTRY);
1888 goto done;
1891 if (got_object_tree_entry_is_symlink(te)) {
1892 err = got_tree_entry_get_symlink_target(link_target, te, repo);
1893 if (err)
1894 goto done;
1895 if (!got_path_is_absolute(*link_target)) {
1896 char *abspath;
1897 if (asprintf(&abspath, "%s/%s", parent_path,
1898 *link_target) == -1) {
1899 err = got_error_from_errno("asprintf");
1900 goto done;
1902 free(*link_target);
1903 *link_target = malloc(PATH_MAX);
1904 if (*link_target == NULL) {
1905 err = got_error_from_errno("malloc");
1906 goto done;
1908 err = got_canonpath(abspath, *link_target, PATH_MAX);
1909 free(abspath);
1910 if (err)
1911 goto done;
1914 done:
1915 free(tree_obj_id);
1916 if (tree)
1917 got_object_tree_close(tree);
1918 if (err) {
1919 free(*link_target);
1920 *link_target = NULL;
1922 return err;
1925 const struct got_error *
1926 got_object_resolve_symlinks(char **link_target, const char *path,
1927 struct got_object_id *commit_id, struct got_repository *repo)
1929 const struct got_error *err = NULL;
1930 char *next_target = NULL;
1931 int max_recursion = 40; /* matches Git */
1933 *link_target = NULL;
1935 do {
1936 err = resolve_symlink(&next_target,
1937 *link_target ? *link_target : path, commit_id, repo);
1938 if (err)
1939 break;
1940 if (next_target) {
1941 free(*link_target);
1942 if (--max_recursion == 0) {
1943 err = got_error_path(path, GOT_ERR_RECURSION);
1944 *link_target = NULL;
1945 break;
1947 *link_target = next_target;
1949 } while (next_target);
1951 return err;
1954 const struct got_error *
1955 got_traverse_packed_commits(struct got_object_id_queue *traversed_commits,
1956 struct got_object_id *commit_id, const char *path,
1957 struct got_repository *repo)
1959 const struct got_error *err = NULL;
1960 struct got_pack *pack = NULL;
1961 struct got_packidx *packidx = NULL;
1962 char *path_packfile = NULL;
1963 struct got_commit_object *changed_commit = NULL;
1964 struct got_object_id *changed_commit_id = NULL;
1965 int idx;
1967 err = got_repo_search_packidx(&packidx, &idx, repo, commit_id);
1968 if (err) {
1969 if (err->code != GOT_ERR_NO_OBJ)
1970 return err;
1971 return NULL;
1974 err = get_packfile_path(&path_packfile, packidx);
1975 if (err)
1976 return err;
1978 pack = got_repo_get_cached_pack(repo, path_packfile);
1979 if (pack == NULL) {
1980 err = got_repo_cache_pack(&pack, repo, path_packfile, packidx);
1981 if (err)
1982 goto done;
1985 if (pack->privsep_child == NULL) {
1986 err = start_pack_privsep_child(pack, packidx);
1987 if (err)
1988 goto done;
1991 err = got_privsep_send_commit_traversal_request(
1992 pack->privsep_child->ibuf, commit_id, idx, path);
1993 if (err)
1994 goto done;
1996 err = got_privsep_recv_traversed_commits(&changed_commit,
1997 &changed_commit_id, traversed_commits, pack->privsep_child->ibuf);
1998 if (err)
1999 goto done;
2001 if (changed_commit) {
2003 * Cache the commit in which the path was changed.
2004 * This commit might be opened again soon.
2006 changed_commit->refcnt++;
2007 err = got_repo_cache_commit(repo, changed_commit_id,
2008 changed_commit);
2009 got_object_commit_close(changed_commit);
2011 done:
2012 free(path_packfile);
2013 free(changed_commit_id);
2014 return err;