Blob


1 /*
2 * Copyright (c) 2018, 2019 Stefan Sperling <stsp@openbsd.org>
3 *
4 * Permission to use, copy, modify, and distribute this software for any
5 * purpose with or without fee is hereby granted, provided that the above
6 * copyright notice and this permission notice appear in all copies.
7 *
8 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
15 */
17 #include <sys/types.h>
18 #include <sys/stat.h>
19 #include <sys/queue.h>
20 #include <sys/uio.h>
21 #include <sys/socket.h>
22 #include <sys/wait.h>
23 #include <sys/syslimits.h>
24 #include <sys/resource.h>
26 #include <errno.h>
27 #include <fcntl.h>
28 #include <stdio.h>
29 #include <stdlib.h>
30 #include <string.h>
31 #include <stdint.h>
32 #include <sha1.h>
33 #include <zlib.h>
34 #include <ctype.h>
35 #include <limits.h>
36 #include <imsg.h>
37 #include <time.h>
39 #include "got_error.h"
40 #include "got_object.h"
41 #include "got_repository.h"
42 #include "got_opentemp.h"
43 #include "got_path.h"
45 #include "got_lib_sha1.h"
46 #include "got_lib_delta.h"
47 #include "got_lib_inflate.h"
48 #include "got_lib_object.h"
49 #include "got_lib_privsep.h"
50 #include "got_lib_object_idcache.h"
51 #include "got_lib_object_cache.h"
52 #include "got_lib_object_parse.h"
53 #include "got_lib_pack.h"
54 #include "got_lib_repository.h"
56 #ifndef MIN
57 #define MIN(_a,_b) ((_a) < (_b) ? (_a) : (_b))
58 #endif
60 struct got_object_id *
61 got_object_get_id(struct got_object *obj)
62 {
63 return &obj->id;
64 }
66 const struct got_error *
67 got_object_get_id_str(char **outbuf, struct got_object *obj)
68 {
69 return got_object_id_str(outbuf, &obj->id);
70 }
72 const struct got_error *
73 got_object_get_type(int *type, struct got_repository *repo,
74 struct got_object_id *id)
75 {
76 const struct got_error *err = NULL;
77 struct got_object *obj;
79 err = got_object_open(&obj, repo, id);
80 if (err)
81 return err;
83 switch (obj->type) {
84 case GOT_OBJ_TYPE_COMMIT:
85 case GOT_OBJ_TYPE_TREE:
86 case GOT_OBJ_TYPE_BLOB:
87 case GOT_OBJ_TYPE_TAG:
88 *type = obj->type;
89 break;
90 default:
91 err = got_error(GOT_ERR_OBJ_TYPE);
92 break;
93 }
95 got_object_close(obj);
96 return err;
97 }
99 const struct got_error *
100 got_object_get_path(char **path, struct got_object_id *id,
101 struct got_repository *repo)
103 const struct got_error *err = NULL;
104 char *hex = NULL;
105 char *path_objects;
107 *path = NULL;
109 path_objects = got_repo_get_path_objects(repo);
110 if (path_objects == NULL)
111 return got_error_from_errno("got_repo_get_path_objects");
113 err = got_object_id_str(&hex, id);
114 if (err)
115 goto done;
117 if (asprintf(path, "%s/%.2x/%s", path_objects,
118 id->sha1[0], hex + 2) == -1)
119 err = got_error_from_errno("asprintf");
121 done:
122 free(hex);
123 free(path_objects);
124 return err;
127 static const struct got_error *
128 open_loose_object(int *fd, struct got_object_id *id,
129 struct got_repository *repo)
131 const struct got_error *err = NULL;
132 char *path;
134 err = got_object_get_path(&path, id, repo);
135 if (err)
136 return err;
137 *fd = open(path, O_RDONLY | O_NOFOLLOW);
138 if (*fd == -1) {
139 err = got_error_from_errno2("open", path);
140 goto done;
142 done:
143 free(path);
144 return err;
147 static const struct got_error *
148 get_packfile_path(char **path_packfile, struct got_packidx *packidx)
150 size_t size;
152 /* Packfile path contains ".pack" instead of ".idx", so add one byte. */
153 size = strlen(packidx->path_packidx) + 2;
154 if (size < GOT_PACKFILE_NAMELEN + 1)
155 return got_error_path(packidx->path_packidx, GOT_ERR_BAD_PATH);
157 *path_packfile = malloc(size);
158 if (*path_packfile == NULL)
159 return got_error_from_errno("malloc");
161 /* Copy up to and excluding ".idx". */
162 if (strlcpy(*path_packfile, packidx->path_packidx,
163 size - strlen(GOT_PACKIDX_SUFFIX) - 1) >= size)
164 return got_error(GOT_ERR_NO_SPACE);
166 if (strlcat(*path_packfile, GOT_PACKFILE_SUFFIX, size) >= size)
167 return got_error(GOT_ERR_NO_SPACE);
169 return NULL;
172 static const struct got_error *
173 request_packed_object(struct got_object **obj, struct got_pack *pack, int idx,
174 struct got_object_id *id)
176 const struct got_error *err = NULL;
177 struct imsgbuf *ibuf = pack->privsep_child->ibuf;
179 err = got_privsep_send_packed_obj_req(ibuf, idx, id);
180 if (err)
181 return err;
183 err = got_privsep_recv_obj(obj, ibuf);
184 if (err)
185 return err;
187 memcpy(&(*obj)->id, id, sizeof((*obj)->id));
189 return NULL;
192 static void
193 set_max_datasize(void)
195 struct rlimit rl;
197 if (getrlimit(RLIMIT_DATA, &rl) != 0)
198 return;
200 rl.rlim_cur = rl.rlim_max;
201 setrlimit(RLIMIT_DATA, &rl);
204 static const struct got_error *
205 start_pack_privsep_child(struct got_pack *pack, struct got_packidx *packidx)
207 const struct got_error *err = NULL;
208 int imsg_fds[2];
209 pid_t pid;
210 struct imsgbuf *ibuf;
212 ibuf = calloc(1, sizeof(*ibuf));
213 if (ibuf == NULL)
214 return got_error_from_errno("calloc");
216 pack->privsep_child = calloc(1, sizeof(*pack->privsep_child));
217 if (pack->privsep_child == NULL) {
218 err = got_error_from_errno("calloc");
219 free(ibuf);
220 return err;
223 if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_fds) == -1) {
224 err = got_error_from_errno("socketpair");
225 goto done;
228 pid = fork();
229 if (pid == -1) {
230 err = got_error_from_errno("fork");
231 goto done;
232 } else if (pid == 0) {
233 set_max_datasize();
234 got_privsep_exec_child(imsg_fds, GOT_PATH_PROG_READ_PACK,
235 pack->path_packfile);
236 /* not reached */
239 if (close(imsg_fds[1]) != 0)
240 return got_error_from_errno("close");
241 pack->privsep_child->imsg_fd = imsg_fds[0];
242 pack->privsep_child->pid = pid;
243 imsg_init(ibuf, imsg_fds[0]);
244 pack->privsep_child->ibuf = ibuf;
246 err = got_privsep_init_pack_child(ibuf, pack, packidx);
247 if (err) {
248 const struct got_error *child_err;
249 err = got_privsep_send_stop(pack->privsep_child->imsg_fd);
250 child_err = got_privsep_wait_for_child(
251 pack->privsep_child->pid);
252 if (child_err && err == NULL)
253 err = child_err;
255 done:
256 if (err) {
257 free(ibuf);
258 free(pack->privsep_child);
259 pack->privsep_child = NULL;
261 return err;
264 static const struct got_error *
265 read_packed_object_privsep(struct got_object **obj,
266 struct got_repository *repo, struct got_pack *pack,
267 struct got_packidx *packidx, int idx, struct got_object_id *id)
269 const struct got_error *err = NULL;
271 if (pack->privsep_child)
272 return request_packed_object(obj, pack, idx, id);
274 err = start_pack_privsep_child(pack, packidx);
275 if (err)
276 return err;
278 return request_packed_object(obj, pack, idx, id);
282 static const struct got_error *
283 open_packed_object(struct got_object **obj, struct got_object_id *id,
284 struct got_repository *repo)
286 const struct got_error *err = NULL;
287 struct got_pack *pack = NULL;
288 struct got_packidx *packidx = NULL;
289 int idx;
290 char *path_packfile;
292 err = got_repo_search_packidx(&packidx, &idx, repo, id);
293 if (err)
294 return err;
296 err = get_packfile_path(&path_packfile, packidx);
297 if (err)
298 return err;
300 pack = got_repo_get_cached_pack(repo, path_packfile);
301 if (pack == NULL) {
302 err = got_repo_cache_pack(&pack, repo, path_packfile, packidx);
303 if (err)
304 goto done;
307 err = read_packed_object_privsep(obj, repo, pack, packidx, idx, id);
308 if (err)
309 goto done;
310 done:
311 free(path_packfile);
312 return err;
315 static const struct got_error *
316 request_object(struct got_object **obj, struct got_repository *repo, int fd)
318 const struct got_error *err = NULL;
319 struct imsgbuf *ibuf;
321 ibuf = repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_OBJECT].ibuf;
323 err = got_privsep_send_obj_req(ibuf, fd);
324 if (err)
325 return err;
327 return got_privsep_recv_obj(obj, ibuf);
330 static const struct got_error *
331 read_object_header_privsep(struct got_object **obj, struct got_repository *repo,
332 int obj_fd)
334 const struct got_error *err;
335 int imsg_fds[2];
336 pid_t pid;
337 struct imsgbuf *ibuf;
339 if (repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_OBJECT].imsg_fd != -1)
340 return request_object(obj, repo, obj_fd);
342 ibuf = calloc(1, sizeof(*ibuf));
343 if (ibuf == NULL)
344 return got_error_from_errno("calloc");
346 if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_fds) == -1) {
347 err = got_error_from_errno("socketpair");
348 free(ibuf);
349 return err;
352 pid = fork();
353 if (pid == -1) {
354 err = got_error_from_errno("fork");
355 free(ibuf);
356 return err;
358 else if (pid == 0) {
359 got_privsep_exec_child(imsg_fds, GOT_PATH_PROG_READ_OBJECT,
360 repo->path);
361 /* not reached */
364 if (close(imsg_fds[1]) != 0) {
365 err = got_error_from_errno("close");
366 free(ibuf);
367 return err;
369 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_OBJECT].imsg_fd =
370 imsg_fds[0];
371 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_OBJECT].pid = pid;
372 imsg_init(ibuf, imsg_fds[0]);
373 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_OBJECT].ibuf = ibuf;
375 return request_object(obj, repo, obj_fd);
379 const struct got_error *
380 got_object_open(struct got_object **obj, struct got_repository *repo,
381 struct got_object_id *id)
383 const struct got_error *err = NULL;
384 char *path;
385 int fd;
387 *obj = got_repo_get_cached_object(repo, id);
388 if (*obj != NULL) {
389 (*obj)->refcnt++;
390 return NULL;
393 err = open_packed_object(obj, id, repo);
394 if (err && err->code != GOT_ERR_NO_OBJ)
395 return err;
396 if (*obj) {
397 (*obj)->refcnt++;
398 return got_repo_cache_object(repo, id, *obj);
401 err = got_object_get_path(&path, id, repo);
402 if (err)
403 return err;
405 fd = open(path, O_RDONLY | O_NOFOLLOW);
406 if (fd == -1) {
407 if (errno == ENOENT)
408 err = got_error_no_obj(id);
409 else
410 err = got_error_from_errno2("open", path);
411 goto done;
412 } else {
413 err = read_object_header_privsep(obj, repo, fd);
414 if (err)
415 goto done;
416 memcpy((*obj)->id.sha1, id->sha1, SHA1_DIGEST_LENGTH);
419 (*obj)->refcnt++;
420 err = got_repo_cache_object(repo, id, *obj);
421 done:
422 free(path);
423 return err;
427 const struct got_error *
428 got_object_open_by_id_str(struct got_object **obj, struct got_repository *repo,
429 const char *id_str)
431 struct got_object_id id;
433 if (!got_parse_sha1_digest(id.sha1, id_str))
434 return got_error_path(id_str, GOT_ERR_BAD_OBJ_ID_STR);
436 return got_object_open(obj, repo, &id);
439 const struct got_error *
440 got_object_resolve_id_str(struct got_object_id **id,
441 struct got_repository *repo, const char *id_str)
443 const struct got_error *err = NULL;
444 struct got_object *obj;
446 err = got_object_open_by_id_str(&obj, repo, id_str);
447 if (err)
448 return err;
450 *id = got_object_id_dup(got_object_get_id(obj));
451 got_object_close(obj);
452 if (*id == NULL)
453 return got_error_from_errno("got_object_id_dup");
455 return NULL;
458 static const struct got_error *
459 request_packed_commit(struct got_commit_object **commit, struct got_pack *pack,
460 int pack_idx, struct got_object_id *id)
462 const struct got_error *err = NULL;
464 err = got_privsep_send_commit_req(pack->privsep_child->ibuf, -1, id,
465 pack_idx);
466 if (err)
467 return err;
469 err = got_privsep_recv_commit(commit, pack->privsep_child->ibuf);
470 if (err)
471 return err;
473 (*commit)->flags |= GOT_COMMIT_FLAG_PACKED;
474 return NULL;
477 static const struct got_error *
478 read_packed_commit_privsep(struct got_commit_object **commit,
479 struct got_pack *pack, struct got_packidx *packidx, int idx,
480 struct got_object_id *id)
482 const struct got_error *err = NULL;
484 if (pack->privsep_child)
485 return request_packed_commit(commit, pack, idx, id);
487 err = start_pack_privsep_child(pack, packidx);
488 if (err)
489 return err;
491 return request_packed_commit(commit, pack, idx, id);
494 static const struct got_error *
495 request_commit(struct got_commit_object **commit, struct got_repository *repo,
496 int fd)
498 const struct got_error *err = NULL;
499 struct imsgbuf *ibuf;
501 ibuf = repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_COMMIT].ibuf;
503 err = got_privsep_send_commit_req(ibuf, fd, NULL, -1);
504 if (err)
505 return err;
507 return got_privsep_recv_commit(commit, ibuf);
510 static const struct got_error *
511 read_commit_privsep(struct got_commit_object **commit, int obj_fd,
512 struct got_repository *repo)
514 const struct got_error *err;
515 int imsg_fds[2];
516 pid_t pid;
517 struct imsgbuf *ibuf;
519 if (repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_COMMIT].imsg_fd != -1)
520 return request_commit(commit, repo, obj_fd);
522 ibuf = calloc(1, sizeof(*ibuf));
523 if (ibuf == NULL)
524 return got_error_from_errno("calloc");
526 if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_fds) == -1) {
527 err = got_error_from_errno("socketpair");
528 free(ibuf);
529 return err;
532 pid = fork();
533 if (pid == -1) {
534 err = got_error_from_errno("fork");
535 free(ibuf);
536 return err;
538 else if (pid == 0) {
539 got_privsep_exec_child(imsg_fds, GOT_PATH_PROG_READ_COMMIT,
540 repo->path);
541 /* not reached */
544 if (close(imsg_fds[1]) != 0) {
545 err = got_error_from_errno("close");
546 free(ibuf);
547 return err;
549 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_COMMIT].imsg_fd =
550 imsg_fds[0];
551 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_COMMIT].pid = pid;
552 imsg_init(ibuf, imsg_fds[0]);
553 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_COMMIT].ibuf = ibuf;
555 return request_commit(commit, repo, obj_fd);
559 static const struct got_error *
560 open_commit(struct got_commit_object **commit,
561 struct got_repository *repo, struct got_object_id *id, int check_cache)
563 const struct got_error *err = NULL;
564 struct got_packidx *packidx = NULL;
565 int idx;
566 char *path_packfile = NULL;
568 if (check_cache) {
569 *commit = got_repo_get_cached_commit(repo, id);
570 if (*commit != NULL) {
571 (*commit)->refcnt++;
572 return NULL;
574 } else
575 *commit = NULL;
577 err = got_repo_search_packidx(&packidx, &idx, repo, id);
578 if (err == NULL) {
579 struct got_pack *pack = NULL;
581 err = get_packfile_path(&path_packfile, packidx);
582 if (err)
583 return err;
585 pack = got_repo_get_cached_pack(repo, path_packfile);
586 if (pack == NULL) {
587 err = got_repo_cache_pack(&pack, repo, path_packfile,
588 packidx);
589 if (err)
590 goto done;
592 err = read_packed_commit_privsep(commit, pack,
593 packidx, idx, id);
594 } else if (err->code == GOT_ERR_NO_OBJ) {
595 int fd;
597 err = open_loose_object(&fd, id, repo);
598 if (err)
599 return err;
600 err = read_commit_privsep(commit, fd, repo);
603 if (err == NULL) {
604 (*commit)->refcnt++;
605 err = got_repo_cache_commit(repo, id, *commit);
607 done:
608 free(path_packfile);
609 return err;
612 const struct got_error *
613 got_object_open_as_commit(struct got_commit_object **commit,
614 struct got_repository *repo, struct got_object_id *id)
616 *commit = got_repo_get_cached_commit(repo, id);
617 if (*commit != NULL) {
618 (*commit)->refcnt++;
619 return NULL;
622 return open_commit(commit, repo, id, 0);
625 const struct got_error *
626 got_object_commit_open(struct got_commit_object **commit,
627 struct got_repository *repo, struct got_object *obj)
629 return open_commit(commit, repo, got_object_get_id(obj), 1);
632 const struct got_error *
633 got_object_qid_alloc(struct got_object_qid **qid, struct got_object_id *id)
635 const struct got_error *err = NULL;
637 *qid = calloc(1, sizeof(**qid));
638 if (*qid == NULL)
639 return got_error_from_errno("calloc");
641 (*qid)->id = got_object_id_dup(id);
642 if ((*qid)->id == NULL) {
643 err = got_error_from_errno("got_object_id_dup");
644 got_object_qid_free(*qid);
645 *qid = NULL;
646 return err;
649 return NULL;
652 static const struct got_error *
653 request_packed_tree(struct got_tree_object **tree, struct got_pack *pack,
654 int pack_idx, struct got_object_id *id)
656 const struct got_error *err = NULL;
658 err = got_privsep_send_tree_req(pack->privsep_child->ibuf, -1, id,
659 pack_idx);
660 if (err)
661 return err;
663 return got_privsep_recv_tree(tree, pack->privsep_child->ibuf);
666 static const struct got_error *
667 read_packed_tree_privsep(struct got_tree_object **tree,
668 struct got_pack *pack, struct got_packidx *packidx, int idx,
669 struct got_object_id *id)
671 const struct got_error *err = NULL;
673 if (pack->privsep_child)
674 return request_packed_tree(tree, pack, idx, id);
676 err = start_pack_privsep_child(pack, packidx);
677 if (err)
678 return err;
680 return request_packed_tree(tree, pack, idx, id);
683 static const struct got_error *
684 request_tree(struct got_tree_object **tree, struct got_repository *repo,
685 int fd)
687 const struct got_error *err = NULL;
688 struct imsgbuf *ibuf;
690 ibuf = repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TREE].ibuf;
692 err = got_privsep_send_tree_req(ibuf, fd, NULL, -1);
693 if (err)
694 return err;
696 return got_privsep_recv_tree(tree, ibuf);
699 const struct got_error *
700 read_tree_privsep(struct got_tree_object **tree, int obj_fd,
701 struct got_repository *repo)
703 const struct got_error *err;
704 int imsg_fds[2];
705 pid_t pid;
706 struct imsgbuf *ibuf;
708 if (repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TREE].imsg_fd != -1)
709 return request_tree(tree, repo, obj_fd);
711 ibuf = calloc(1, sizeof(*ibuf));
712 if (ibuf == NULL)
713 return got_error_from_errno("calloc");
715 if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_fds) == -1) {
716 err = got_error_from_errno("socketpair");
717 free(ibuf);
718 return err;
721 pid = fork();
722 if (pid == -1) {
723 err = got_error_from_errno("fork");
724 free(ibuf);
725 return err;
727 else if (pid == 0) {
728 got_privsep_exec_child(imsg_fds, GOT_PATH_PROG_READ_TREE,
729 repo->path);
730 /* not reached */
733 if (close(imsg_fds[1]) != 0) {
734 err = got_error_from_errno("close");
735 free(ibuf);
736 return err;
738 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TREE].imsg_fd =
739 imsg_fds[0];
740 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TREE].pid = pid;
741 imsg_init(ibuf, imsg_fds[0]);
742 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TREE].ibuf = ibuf;
745 return request_tree(tree, repo, obj_fd);
748 static const struct got_error *
749 open_tree(struct got_tree_object **tree, struct got_repository *repo,
750 struct got_object_id *id, int check_cache)
752 const struct got_error *err = NULL;
753 struct got_packidx *packidx = NULL;
754 int idx;
755 char *path_packfile = NULL;
757 if (check_cache) {
758 *tree = got_repo_get_cached_tree(repo, id);
759 if (*tree != NULL) {
760 (*tree)->refcnt++;
761 return NULL;
763 } else
764 *tree = NULL;
766 err = got_repo_search_packidx(&packidx, &idx, repo, id);
767 if (err == NULL) {
768 struct got_pack *pack = NULL;
770 err = get_packfile_path(&path_packfile, packidx);
771 if (err)
772 return err;
774 pack = got_repo_get_cached_pack(repo, path_packfile);
775 if (pack == NULL) {
776 err = got_repo_cache_pack(&pack, repo, path_packfile,
777 packidx);
778 if (err)
779 goto done;
781 err = read_packed_tree_privsep(tree, pack,
782 packidx, idx, id);
783 } else if (err->code == GOT_ERR_NO_OBJ) {
784 int fd;
786 err = open_loose_object(&fd, id, repo);
787 if (err)
788 return err;
789 err = read_tree_privsep(tree, fd, repo);
792 if (err == NULL) {
793 (*tree)->refcnt++;
794 err = got_repo_cache_tree(repo, id, *tree);
796 done:
797 free(path_packfile);
798 return err;
801 const struct got_error *
802 got_object_open_as_tree(struct got_tree_object **tree,
803 struct got_repository *repo, struct got_object_id *id)
805 *tree = got_repo_get_cached_tree(repo, id);
806 if (*tree != NULL) {
807 (*tree)->refcnt++;
808 return NULL;
811 return open_tree(tree, repo, id, 0);
814 const struct got_error *
815 got_object_tree_open(struct got_tree_object **tree,
816 struct got_repository *repo, struct got_object *obj)
818 return open_tree(tree, repo, got_object_get_id(obj), 1);
821 int
822 got_object_tree_get_nentries(struct got_tree_object *tree)
824 return tree->nentries;
827 struct got_tree_entry *
828 got_object_tree_get_first_entry(struct got_tree_object *tree)
830 return got_object_tree_get_entry(tree, 0);
833 struct got_tree_entry *
834 got_object_tree_get_last_entry(struct got_tree_object *tree)
836 return got_object_tree_get_entry(tree, tree->nentries - 1);
839 struct got_tree_entry *
840 got_object_tree_get_entry(struct got_tree_object *tree, int i)
842 if (i < 0 || i >= tree->nentries)
843 return NULL;
844 return &tree->entries[i];
847 mode_t
848 got_tree_entry_get_mode(struct got_tree_entry *te)
850 return te->mode;
853 const char *
854 got_tree_entry_get_name(struct got_tree_entry *te)
856 return &te->name[0];
859 struct got_object_id *
860 got_tree_entry_get_id(struct got_tree_entry *te)
862 return &te->id;
865 const struct got_error *
866 got_tree_entry_get_symlink_target(char **link_target, struct got_tree_entry *te,
867 struct got_repository *repo)
869 const struct got_error *err = NULL;
870 struct got_blob_object *blob = NULL;
871 size_t len;
873 *link_target = NULL;
875 /* S_IFDIR check avoids confusing symlinks with submodules. */
876 if ((te->mode & (S_IFDIR | S_IFLNK)) != S_IFLNK)
877 return got_error(GOT_ERR_TREE_ENTRY_TYPE);
879 err = got_object_open_as_blob(&blob, repo,
880 got_tree_entry_get_id(te), PATH_MAX);
881 if (err)
882 return err;
884 err = got_object_blob_read_block(&len, blob);
885 if (err)
886 goto done;
888 *link_target = malloc(len + 1);
889 if (*link_target == NULL) {
890 err = got_error_from_errno("malloc");
891 goto done;
893 memcpy(*link_target, got_object_blob_get_read_buf(blob), len);
894 (*link_target)[len] = '\0';
895 done:
896 if (blob)
897 got_object_blob_close(blob);
898 return err;
901 int
902 got_tree_entry_get_index(struct got_tree_entry *te)
904 return te->idx;
907 struct got_tree_entry *
908 got_tree_entry_get_next(struct got_tree_object *tree,
909 struct got_tree_entry *te)
911 return got_object_tree_get_entry(tree, te->idx + 1);
914 struct got_tree_entry *
915 got_tree_entry_get_prev(struct got_tree_object *tree,
916 struct got_tree_entry *te)
918 return got_object_tree_get_entry(tree, te->idx - 1);
921 static const struct got_error *
922 request_packed_blob(uint8_t **outbuf, size_t *size, size_t *hdrlen, int outfd,
923 struct got_pack *pack, struct got_packidx *packidx, int idx,
924 struct got_object_id *id)
926 const struct got_error *err = NULL;
927 int outfd_child;
928 int basefd, accumfd; /* temporary files for delta application */
930 basefd = got_opentempfd();
931 if (basefd == -1)
932 return got_error_from_errno("got_opentempfd");
933 accumfd = got_opentempfd();
934 if (accumfd == -1)
935 return got_error_from_errno("got_opentempfd");
937 outfd_child = dup(outfd);
938 if (outfd_child == -1)
939 return got_error_from_errno("dup");
941 err = got_privsep_send_blob_req(pack->privsep_child->ibuf, -1, id, idx);
942 if (err)
943 return err;
945 err = got_privsep_send_blob_outfd(pack->privsep_child->ibuf,
946 outfd_child);
947 if (err) {
948 close(basefd);
949 close(accumfd);
950 return err;
953 err = got_privsep_send_tmpfd(pack->privsep_child->ibuf,
954 basefd);
955 if (err) {
956 close(accumfd);
957 return err;
960 err = got_privsep_send_tmpfd(pack->privsep_child->ibuf,
961 accumfd);
962 if (err)
963 return err;
965 err = got_privsep_recv_blob(outbuf, size, hdrlen,
966 pack->privsep_child->ibuf);
967 if (err)
968 return err;
970 if (lseek(outfd, SEEK_SET, 0) == -1)
971 err = got_error_from_errno("lseek");
973 return err;
976 static const struct got_error *
977 read_packed_blob_privsep(uint8_t **outbuf, size_t *size, size_t *hdrlen,
978 int outfd, struct got_pack *pack, struct got_packidx *packidx, int idx,
979 struct got_object_id *id)
981 const struct got_error *err = NULL;
983 if (pack->privsep_child == NULL) {
984 err = start_pack_privsep_child(pack, packidx);
985 if (err)
986 return err;
989 return request_packed_blob(outbuf, size, hdrlen, outfd, pack, packidx,
990 idx, id);
993 static const struct got_error *
994 request_blob(uint8_t **outbuf, size_t *size, size_t *hdrlen, int outfd,
995 int infd, struct imsgbuf *ibuf)
997 const struct got_error *err = NULL;
998 int outfd_child;
1000 outfd_child = dup(outfd);
1001 if (outfd_child == -1)
1002 return got_error_from_errno("dup");
1004 err = got_privsep_send_blob_req(ibuf, infd, NULL, -1);
1005 if (err)
1006 return err;
1008 err = got_privsep_send_blob_outfd(ibuf, outfd_child);
1009 if (err)
1010 return err;
1012 err = got_privsep_recv_blob(outbuf, size, hdrlen, ibuf);
1013 if (err)
1014 return err;
1016 if (lseek(outfd, SEEK_SET, 0) == -1)
1017 return got_error_from_errno("lseek");
1019 return err;
1022 static const struct got_error *
1023 read_blob_privsep(uint8_t **outbuf, size_t *size, size_t *hdrlen,
1024 int outfd, int infd, struct got_repository *repo)
1026 const struct got_error *err;
1027 int imsg_fds[2];
1028 pid_t pid;
1029 struct imsgbuf *ibuf;
1031 if (repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_BLOB].imsg_fd != -1) {
1032 ibuf = repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_BLOB].ibuf;
1033 return request_blob(outbuf, size, hdrlen, outfd, infd, ibuf);
1036 ibuf = calloc(1, sizeof(*ibuf));
1037 if (ibuf == NULL)
1038 return got_error_from_errno("calloc");
1040 if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_fds) == -1) {
1041 err = got_error_from_errno("socketpair");
1042 free(ibuf);
1043 return err;
1046 pid = fork();
1047 if (pid == -1) {
1048 err = got_error_from_errno("fork");
1049 free(ibuf);
1050 return err;
1052 else if (pid == 0) {
1053 got_privsep_exec_child(imsg_fds, GOT_PATH_PROG_READ_BLOB,
1054 repo->path);
1055 /* not reached */
1058 if (close(imsg_fds[1]) != 0) {
1059 err = got_error_from_errno("close");
1060 free(ibuf);
1061 return err;
1063 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_BLOB].imsg_fd =
1064 imsg_fds[0];
1065 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_BLOB].pid = pid;
1066 imsg_init(ibuf, imsg_fds[0]);
1067 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_BLOB].ibuf = ibuf;
1069 return request_blob(outbuf, size, hdrlen, outfd, infd, ibuf);
1072 static const struct got_error *
1073 open_blob(struct got_blob_object **blob, struct got_repository *repo,
1074 struct got_object_id *id, size_t blocksize)
1076 const struct got_error *err = NULL;
1077 struct got_packidx *packidx = NULL;
1078 int idx;
1079 char *path_packfile = NULL;
1080 uint8_t *outbuf;
1081 int outfd;
1082 size_t size, hdrlen;
1083 struct stat sb;
1085 *blob = calloc(1, sizeof(**blob));
1086 if (*blob == NULL)
1087 return got_error_from_errno("calloc");
1089 outfd = got_opentempfd();
1090 if (outfd == -1)
1091 return got_error_from_errno("got_opentempfd");
1093 (*blob)->read_buf = malloc(blocksize);
1094 if ((*blob)->read_buf == NULL) {
1095 err = got_error_from_errno("malloc");
1096 goto done;
1099 err = got_repo_search_packidx(&packidx, &idx, repo, id);
1100 if (err == NULL) {
1101 struct got_pack *pack = NULL;
1103 err = get_packfile_path(&path_packfile, packidx);
1104 if (err)
1105 goto done;
1107 pack = got_repo_get_cached_pack(repo, path_packfile);
1108 if (pack == NULL) {
1109 err = got_repo_cache_pack(&pack, repo, path_packfile,
1110 packidx);
1111 if (err)
1112 goto done;
1114 err = read_packed_blob_privsep(&outbuf, &size, &hdrlen, outfd,
1115 pack, packidx, idx, id);
1116 } else if (err->code == GOT_ERR_NO_OBJ) {
1117 int infd;
1119 err = open_loose_object(&infd, id, repo);
1120 if (err)
1121 goto done;
1122 err = read_blob_privsep(&outbuf, &size, &hdrlen, outfd, infd,
1123 repo);
1125 if (err)
1126 goto done;
1128 if (hdrlen > size) {
1129 err = got_error(GOT_ERR_BAD_OBJ_HDR);
1130 goto done;
1133 if (outbuf) {
1134 if (close(outfd) != 0 && err == NULL)
1135 err = got_error_from_errno("close");
1136 outfd = -1;
1137 (*blob)->f = fmemopen(outbuf, size, "rb");
1138 if ((*blob)->f == NULL) {
1139 err = got_error_from_errno("fmemopen");
1140 free(outbuf);
1141 goto done;
1143 (*blob)->data = outbuf;
1144 } else {
1145 if (fstat(outfd, &sb) == -1) {
1146 err = got_error_from_errno("fstat");
1147 goto done;
1150 if (sb.st_size != size) {
1151 err = got_error(GOT_ERR_PRIVSEP_LEN);
1152 goto done;
1155 (*blob)->f = fdopen(outfd, "rb");
1156 if ((*blob)->f == NULL) {
1157 err = got_error_from_errno("fdopen");
1158 close(outfd);
1159 outfd = -1;
1160 goto done;
1164 (*blob)->hdrlen = hdrlen;
1165 (*blob)->blocksize = blocksize;
1166 memcpy(&(*blob)->id.sha1, id->sha1, SHA1_DIGEST_LENGTH);
1168 done:
1169 free(path_packfile);
1170 if (err) {
1171 if (*blob) {
1172 got_object_blob_close(*blob);
1173 *blob = NULL;
1174 } else if (outfd != -1)
1175 close(outfd);
1177 return err;
1180 const struct got_error *
1181 got_object_open_as_blob(struct got_blob_object **blob,
1182 struct got_repository *repo, struct got_object_id *id,
1183 size_t blocksize)
1185 return open_blob(blob, repo, id, blocksize);
1188 const struct got_error *
1189 got_object_blob_open(struct got_blob_object **blob,
1190 struct got_repository *repo, struct got_object *obj, size_t blocksize)
1192 return open_blob(blob, repo, got_object_get_id(obj), blocksize);
1195 const struct got_error *
1196 got_object_blob_close(struct got_blob_object *blob)
1198 const struct got_error *err = NULL;
1199 free(blob->read_buf);
1200 if (blob->f && fclose(blob->f) != 0)
1201 err = got_error_from_errno("fclose");
1202 free(blob->data);
1203 free(blob);
1204 return err;
1207 void
1208 got_object_blob_rewind(struct got_blob_object *blob)
1210 if (blob->f)
1211 rewind(blob->f);
1214 char *
1215 got_object_blob_id_str(struct got_blob_object *blob, char *buf, size_t size)
1217 return got_sha1_digest_to_str(blob->id.sha1, buf, size);
1220 size_t
1221 got_object_blob_get_hdrlen(struct got_blob_object *blob)
1223 return blob->hdrlen;
1226 const uint8_t *
1227 got_object_blob_get_read_buf(struct got_blob_object *blob)
1229 return blob->read_buf;
1232 const struct got_error *
1233 got_object_blob_read_block(size_t *outlenp, struct got_blob_object *blob)
1235 size_t n;
1237 n = fread(blob->read_buf, 1, blob->blocksize, blob->f);
1238 if (n == 0 && ferror(blob->f))
1239 return got_ferror(blob->f, GOT_ERR_IO);
1240 *outlenp = n;
1241 return NULL;
1244 const struct got_error *
1245 got_object_blob_dump_to_file(size_t *filesize, int *nlines,
1246 off_t **line_offsets, FILE *outfile, struct got_blob_object *blob)
1248 const struct got_error *err = NULL;
1249 size_t n, len, hdrlen;
1250 const uint8_t *buf;
1251 int i;
1252 size_t noffsets = 0;
1253 off_t off = 0, total_len = 0;
1255 if (line_offsets)
1256 *line_offsets = NULL;
1257 if (filesize)
1258 *filesize = 0;
1259 if (nlines)
1260 *nlines = 0;
1262 hdrlen = got_object_blob_get_hdrlen(blob);
1263 do {
1264 err = got_object_blob_read_block(&len, blob);
1265 if (err)
1266 return err;
1267 if (len == 0)
1268 break;
1269 buf = got_object_blob_get_read_buf(blob);
1270 i = hdrlen;
1271 if (line_offsets && nlines) {
1272 if (*line_offsets == NULL) {
1273 /* Have some data but perhaps no '\n'. */
1274 noffsets = 1;
1275 *nlines = 1;
1276 *line_offsets = calloc(1, sizeof(**line_offsets));
1277 if (*line_offsets == NULL)
1278 return got_error_from_errno("calloc");
1280 /* Skip forward over end of first line. */
1281 while (i < len) {
1282 if (buf[i] == '\n')
1283 break;
1284 i++;
1287 /* Scan '\n' offsets in remaining chunk of data. */
1288 while (i < len) {
1289 if (buf[i] != '\n') {
1290 i++;
1291 continue;
1293 (*nlines)++;
1294 if (noffsets < *nlines) {
1295 off_t *o = recallocarray(*line_offsets,
1296 noffsets, *nlines,
1297 sizeof(**line_offsets));
1298 if (o == NULL) {
1299 free(*line_offsets);
1300 *line_offsets = NULL;
1301 return got_error_from_errno(
1302 "recallocarray");
1304 *line_offsets = o;
1305 noffsets = *nlines;
1307 off = total_len + i - hdrlen + 1;
1308 (*line_offsets)[*nlines - 1] = off;
1309 i++;
1312 /* Skip blob object header first time around. */
1313 n = fwrite(buf + hdrlen, 1, len - hdrlen, outfile);
1314 if (n != len - hdrlen)
1315 return got_ferror(outfile, GOT_ERR_IO);
1316 total_len += len - hdrlen;
1317 hdrlen = 0;
1318 } while (len != 0);
1320 if (fflush(outfile) != 0)
1321 return got_error_from_errno("fflush");
1322 rewind(outfile);
1324 if (filesize)
1325 *filesize = total_len;
1327 return NULL;
1330 static const struct got_error *
1331 request_packed_tag(struct got_tag_object **tag, struct got_pack *pack,
1332 int pack_idx, struct got_object_id *id)
1334 const struct got_error *err = NULL;
1336 err = got_privsep_send_tag_req(pack->privsep_child->ibuf, -1, id,
1337 pack_idx);
1338 if (err)
1339 return err;
1341 return got_privsep_recv_tag(tag, pack->privsep_child->ibuf);
1344 static const struct got_error *
1345 read_packed_tag_privsep(struct got_tag_object **tag,
1346 struct got_pack *pack, struct got_packidx *packidx, int idx,
1347 struct got_object_id *id)
1349 const struct got_error *err = NULL;
1351 if (pack->privsep_child)
1352 return request_packed_tag(tag, pack, idx, id);
1354 err = start_pack_privsep_child(pack, packidx);
1355 if (err)
1356 return err;
1358 return request_packed_tag(tag, pack, idx, id);
1361 static const struct got_error *
1362 request_tag(struct got_tag_object **tag, struct got_repository *repo,
1363 int fd)
1365 const struct got_error *err = NULL;
1366 struct imsgbuf *ibuf;
1368 ibuf = repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TAG].ibuf;
1370 err = got_privsep_send_tag_req(ibuf, fd, NULL, -1);
1371 if (err)
1372 return err;
1374 return got_privsep_recv_tag(tag, ibuf);
1377 static const struct got_error *
1378 read_tag_privsep(struct got_tag_object **tag, int obj_fd,
1379 struct got_repository *repo)
1381 const struct got_error *err;
1382 int imsg_fds[2];
1383 pid_t pid;
1384 struct imsgbuf *ibuf;
1386 if (repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TAG].imsg_fd != -1)
1387 return request_tag(tag, repo, obj_fd);
1389 ibuf = calloc(1, sizeof(*ibuf));
1390 if (ibuf == NULL)
1391 return got_error_from_errno("calloc");
1393 if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_fds) == -1) {
1394 err = got_error_from_errno("socketpair");
1395 free(ibuf);
1396 return err;
1399 pid = fork();
1400 if (pid == -1) {
1401 err = got_error_from_errno("fork");
1402 free(ibuf);
1403 return err;
1405 else if (pid == 0) {
1406 got_privsep_exec_child(imsg_fds, GOT_PATH_PROG_READ_TAG,
1407 repo->path);
1408 /* not reached */
1411 if (close(imsg_fds[1]) != 0) {
1412 err = got_error_from_errno("close");
1413 free(ibuf);
1414 return err;
1416 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TAG].imsg_fd =
1417 imsg_fds[0];
1418 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TAG].pid = pid;
1419 imsg_init(ibuf, imsg_fds[0]);
1420 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TAG].ibuf = ibuf;
1422 return request_tag(tag, repo, obj_fd);
1425 static const struct got_error *
1426 open_tag(struct got_tag_object **tag, struct got_repository *repo,
1427 struct got_object_id *id, int check_cache)
1429 const struct got_error *err = NULL;
1430 struct got_packidx *packidx = NULL;
1431 int idx;
1432 char *path_packfile = NULL;
1433 struct got_object *obj = NULL;
1434 int obj_type = GOT_OBJ_TYPE_ANY;
1436 if (check_cache) {
1437 *tag = got_repo_get_cached_tag(repo, id);
1438 if (*tag != NULL) {
1439 (*tag)->refcnt++;
1440 return NULL;
1442 } else
1443 *tag = NULL;
1445 err = got_repo_search_packidx(&packidx, &idx, repo, id);
1446 if (err == NULL) {
1447 struct got_pack *pack = NULL;
1449 err = get_packfile_path(&path_packfile, packidx);
1450 if (err)
1451 return err;
1453 pack = got_repo_get_cached_pack(repo, path_packfile);
1454 if (pack == NULL) {
1455 err = got_repo_cache_pack(&pack, repo, path_packfile,
1456 packidx);
1457 if (err)
1458 goto done;
1461 /* Beware of "lightweight" tags: Check object type first. */
1462 err = read_packed_object_privsep(&obj, repo, pack, packidx,
1463 idx, id);
1464 if (err)
1465 goto done;
1466 obj_type = obj->type;
1467 got_object_close(obj);
1468 if (obj_type != GOT_OBJ_TYPE_TAG) {
1469 err = got_error(GOT_ERR_OBJ_TYPE);
1470 goto done;
1472 err = read_packed_tag_privsep(tag, pack, packidx, idx, id);
1473 } else if (err->code == GOT_ERR_NO_OBJ) {
1474 int fd;
1476 err = open_loose_object(&fd, id, repo);
1477 if (err)
1478 return err;
1479 err = read_object_header_privsep(&obj, repo, fd);
1480 if (err)
1481 return err;
1482 obj_type = obj->type;
1483 got_object_close(obj);
1484 if (obj_type != GOT_OBJ_TYPE_TAG)
1485 return got_error(GOT_ERR_OBJ_TYPE);
1487 err = open_loose_object(&fd, id, repo);
1488 if (err)
1489 return err;
1490 err = read_tag_privsep(tag, fd, repo);
1493 if (err == NULL) {
1494 (*tag)->refcnt++;
1495 err = got_repo_cache_tag(repo, id, *tag);
1497 done:
1498 free(path_packfile);
1499 return err;
1502 const struct got_error *
1503 got_object_open_as_tag(struct got_tag_object **tag,
1504 struct got_repository *repo, struct got_object_id *id)
1506 *tag = got_repo_get_cached_tag(repo, id);
1507 if (*tag != NULL) {
1508 (*tag)->refcnt++;
1509 return NULL;
1512 return open_tag(tag, repo, id, 0);
1515 const struct got_error *
1516 got_object_tag_open(struct got_tag_object **tag,
1517 struct got_repository *repo, struct got_object *obj)
1519 return open_tag(tag, repo, got_object_get_id(obj), 1);
1522 const char *
1523 got_object_tag_get_name(struct got_tag_object *tag)
1525 return tag->tag;
1528 int
1529 got_object_tag_get_object_type(struct got_tag_object *tag)
1531 return tag->obj_type;
1534 struct got_object_id *
1535 got_object_tag_get_object_id(struct got_tag_object *tag)
1537 return &tag->id;
1540 time_t
1541 got_object_tag_get_tagger_time(struct got_tag_object *tag)
1543 return tag->tagger_time;
1546 time_t
1547 got_object_tag_get_tagger_gmtoff(struct got_tag_object *tag)
1549 return tag->tagger_gmtoff;
1552 const char *
1553 got_object_tag_get_tagger(struct got_tag_object *tag)
1555 return tag->tagger;
1558 const char *
1559 got_object_tag_get_message(struct got_tag_object *tag)
1561 return tag->tagmsg;
1564 static struct got_tree_entry *
1565 find_entry_by_name(struct got_tree_object *tree, const char *name, size_t len)
1567 int i;
1569 /* Note that tree entries are sorted in strncmp() order. */
1570 for (i = 0; i < tree->nentries; i++) {
1571 struct got_tree_entry *te = &tree->entries[i];
1572 int cmp = strncmp(te->name, name, len);
1573 if (cmp < 0)
1574 continue;
1575 if (cmp > 0)
1576 break;
1577 if (te->name[len] == '\0')
1578 return te;
1580 return NULL;
1583 struct got_tree_entry *
1584 got_object_tree_find_entry(struct got_tree_object *tree, const char *name)
1586 return find_entry_by_name(tree, name, strlen(name));
1589 const struct got_error *
1590 got_object_id_by_path(struct got_object_id **id, struct got_repository *repo,
1591 struct got_object_id *commit_id, const char *path)
1593 const struct got_error *err = NULL;
1594 struct got_commit_object *commit = NULL;
1595 struct got_tree_object *tree = NULL;
1596 struct got_tree_entry *te = NULL;
1597 const char *seg, *s;
1598 size_t seglen;
1600 *id = NULL;
1602 err = got_object_open_as_commit(&commit, repo, commit_id);
1603 if (err)
1604 goto done;
1606 /* Handle opening of root of commit's tree. */
1607 if (got_path_is_root_dir(path)) {
1608 *id = got_object_id_dup(commit->tree_id);
1609 if (*id == NULL)
1610 err = got_error_from_errno("got_object_id_dup");
1611 goto done;
1614 err = got_object_open_as_tree(&tree, repo, commit->tree_id);
1615 if (err)
1616 goto done;
1618 s = path;
1619 while (s[0] == '/')
1620 s++;
1621 seg = s;
1622 seglen = 0;
1623 while (*s) {
1624 struct got_tree_object *next_tree;
1626 if (*s != '/') {
1627 s++;
1628 seglen++;
1629 if (*s)
1630 continue;
1633 te = find_entry_by_name(tree, seg, seglen);
1634 if (te == NULL) {
1635 err = got_error(GOT_ERR_NO_TREE_ENTRY);
1636 goto done;
1639 if (*s == '\0')
1640 break;
1642 seg = s + 1;
1643 seglen = 0;
1644 s++;
1645 if (*s) {
1646 err = got_object_open_as_tree(&next_tree, repo,
1647 &te->id);
1648 te = NULL;
1649 if (err)
1650 goto done;
1651 got_object_tree_close(tree);
1652 tree = next_tree;
1656 if (te) {
1657 *id = got_object_id_dup(&te->id);
1658 if (*id == NULL)
1659 return got_error_from_errno("got_object_id_dup");
1660 } else
1661 err = got_error(GOT_ERR_NO_TREE_ENTRY);
1662 done:
1663 if (commit)
1664 got_object_commit_close(commit);
1665 if (tree)
1666 got_object_tree_close(tree);
1667 return err;
1671 * Normalize file mode bits to avoid false positive tree entry differences
1672 * in case tree entries have unexpected mode bits set.
1674 static mode_t
1675 normalize_mode_for_comparison(mode_t mode)
1678 * For directories, the only relevant bit is the IFDIR bit.
1679 * This allows us to detect paths changing from a directory
1680 * to a file and vice versa.
1682 if (S_ISDIR(mode))
1683 return mode & S_IFDIR;
1685 /* For files, the only change we care about is the executable bit. */
1686 return mode & S_IXUSR;
1689 const struct got_error *
1690 got_object_tree_path_changed(int *changed,
1691 struct got_tree_object *tree01, struct got_tree_object *tree02,
1692 const char *path, struct got_repository *repo)
1694 const struct got_error *err = NULL;
1695 struct got_tree_object *tree1 = NULL, *tree2 = NULL;
1696 struct got_tree_entry *te1 = NULL, *te2 = NULL;
1697 const char *seg, *s;
1698 size_t seglen;
1700 *changed = 0;
1702 /* We not do support comparing the root path. */
1703 if (got_path_is_root_dir(path))
1704 return got_error_path(path, GOT_ERR_BAD_PATH);
1706 tree1 = tree01;
1707 tree2 = tree02;
1708 s = path;
1709 while (*s == '/')
1710 s++;
1711 seg = s;
1712 seglen = 0;
1713 while (*s) {
1714 struct got_tree_object *next_tree1, *next_tree2;
1715 mode_t mode1, mode2;
1717 if (*s != '/') {
1718 s++;
1719 seglen++;
1720 if (*s)
1721 continue;
1724 te1 = find_entry_by_name(tree1, seg, seglen);
1725 if (te1 == NULL) {
1726 err = got_error(GOT_ERR_NO_OBJ);
1727 goto done;
1730 te2 = find_entry_by_name(tree2, seg, seglen);
1731 if (te2 == NULL) {
1732 *changed = 1;
1733 goto done;
1736 mode1 = normalize_mode_for_comparison(te1->mode);
1737 mode2 = normalize_mode_for_comparison(te2->mode);
1738 if (mode1 != mode2) {
1739 *changed = 1;
1740 goto done;
1743 if (got_object_id_cmp(&te1->id, &te2->id) == 0) {
1744 *changed = 0;
1745 goto done;
1748 if (*s == '\0') { /* final path element */
1749 *changed = 1;
1750 goto done;
1753 seg = s + 1;
1754 s++;
1755 seglen = 0;
1756 if (*s) {
1757 err = got_object_open_as_tree(&next_tree1, repo,
1758 &te1->id);
1759 te1 = NULL;
1760 if (err)
1761 goto done;
1762 if (tree1 != tree01)
1763 got_object_tree_close(tree1);
1764 tree1 = next_tree1;
1766 err = got_object_open_as_tree(&next_tree2, repo,
1767 &te2->id);
1768 te2 = NULL;
1769 if (err)
1770 goto done;
1771 if (tree2 != tree02)
1772 got_object_tree_close(tree2);
1773 tree2 = next_tree2;
1776 done:
1777 if (tree1 && tree1 != tree01)
1778 got_object_tree_close(tree1);
1779 if (tree2 && tree2 != tree02)
1780 got_object_tree_close(tree2);
1781 return err;
1784 const struct got_error *
1785 got_object_tree_entry_dup(struct got_tree_entry **new_te,
1786 struct got_tree_entry *te)
1788 const struct got_error *err = NULL;
1790 *new_te = calloc(1, sizeof(**new_te));
1791 if (*new_te == NULL)
1792 return got_error_from_errno("calloc");
1794 (*new_te)->mode = te->mode;
1795 memcpy((*new_te)->name, te->name, sizeof((*new_te)->name));
1796 memcpy(&(*new_te)->id, &te->id, sizeof((*new_te)->id));
1797 return err;
1800 int
1801 got_object_tree_entry_is_submodule(struct got_tree_entry *te)
1803 return (te->mode & S_IFMT) == (S_IFDIR | S_IFLNK);
1806 const struct got_error *
1807 got_traverse_packed_commits(struct got_object_id_queue *traversed_commits,
1808 struct got_object_id *commit_id, const char *path,
1809 struct got_repository *repo)
1811 const struct got_error *err = NULL;
1812 struct got_pack *pack = NULL;
1813 struct got_packidx *packidx = NULL;
1814 char *path_packfile = NULL;
1815 struct got_commit_object *changed_commit = NULL;
1816 struct got_object_id *changed_commit_id = NULL;
1817 int idx;
1819 err = got_repo_search_packidx(&packidx, &idx, repo, commit_id);
1820 if (err) {
1821 if (err->code != GOT_ERR_NO_OBJ)
1822 return err;
1823 return NULL;
1826 err = get_packfile_path(&path_packfile, packidx);
1827 if (err)
1828 return err;
1830 pack = got_repo_get_cached_pack(repo, path_packfile);
1831 if (pack == NULL) {
1832 err = got_repo_cache_pack(&pack, repo, path_packfile, packidx);
1833 if (err)
1834 goto done;
1837 if (pack->privsep_child == NULL) {
1838 err = start_pack_privsep_child(pack, packidx);
1839 if (err)
1840 goto done;
1843 err = got_privsep_send_commit_traversal_request(
1844 pack->privsep_child->ibuf, commit_id, idx, path);
1845 if (err)
1846 goto done;
1848 err = got_privsep_recv_traversed_commits(&changed_commit,
1849 &changed_commit_id, traversed_commits, pack->privsep_child->ibuf);
1850 if (err)
1851 goto done;
1853 if (changed_commit) {
1855 * Cache the commit in which the path was changed.
1856 * This commit might be opened again soon.
1858 changed_commit->refcnt++;
1859 err = got_repo_cache_commit(repo, changed_commit_id,
1860 changed_commit);
1861 got_object_commit_close(changed_commit);
1863 done:
1864 free(path_packfile);
1865 free(changed_commit_id);
1866 return err;