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/tree.h>
21 #include <sys/uio.h>
22 #include <sys/socket.h>
23 #include <sys/wait.h>
24 #include <sys/resource.h>
26 #include <errno.h>
27 #include <fcntl.h>
28 #include <stdio.h>
29 #include <stdlib.h>
30 #include <string.h>
31 #include <stdint.h>
32 #include <sha1.h>
33 #include <unistd.h>
34 #include <zlib.h>
35 #include <ctype.h>
36 #include <libgen.h>
37 #include <limits.h>
38 #include <imsg.h>
39 #include <time.h>
41 #include "got_error.h"
42 #include "got_object.h"
43 #include "got_repository.h"
44 #include "got_opentemp.h"
45 #include "got_path.h"
47 #include "got_lib_sha1.h"
48 #include "got_lib_delta.h"
49 #include "got_lib_inflate.h"
50 #include "got_lib_object.h"
51 #include "got_lib_privsep.h"
52 #include "got_lib_object_idcache.h"
53 #include "got_lib_object_cache.h"
54 #include "got_lib_object_parse.h"
55 #include "got_lib_pack.h"
56 #include "got_lib_repository.h"
58 #ifndef MIN
59 #define MIN(_a,_b) ((_a) < (_b) ? (_a) : (_b))
60 #endif
62 struct got_object_id *
63 got_object_get_id(struct got_object *obj)
64 {
65 return &obj->id;
66 }
68 const struct got_error *
69 got_object_get_id_str(char **outbuf, struct got_object *obj)
70 {
71 return got_object_id_str(outbuf, &obj->id);
72 }
74 const struct got_error *
75 got_object_get_type(int *type, struct got_repository *repo,
76 struct got_object_id *id)
77 {
78 const struct got_error *err = NULL;
79 struct got_object *obj;
81 err = got_object_open(&obj, repo, id);
82 if (err)
83 return err;
85 switch (obj->type) {
86 case GOT_OBJ_TYPE_COMMIT:
87 case GOT_OBJ_TYPE_TREE:
88 case GOT_OBJ_TYPE_BLOB:
89 case GOT_OBJ_TYPE_TAG:
90 *type = obj->type;
91 break;
92 default:
93 err = got_error(GOT_ERR_OBJ_TYPE);
94 break;
95 }
97 got_object_close(obj);
98 return err;
99 }
101 const struct got_error *
102 got_object_get_path(char **path, struct got_object_id *id,
103 struct got_repository *repo)
105 const struct got_error *err = NULL;
106 char *hex = NULL;
107 char *path_objects;
109 *path = NULL;
111 path_objects = got_repo_get_path_objects(repo);
112 if (path_objects == NULL)
113 return got_error_from_errno("got_repo_get_path_objects");
115 err = got_object_id_str(&hex, id);
116 if (err)
117 goto done;
119 if (asprintf(path, "%s/%.2x/%s", path_objects,
120 id->sha1[0], hex + 2) == -1)
121 err = got_error_from_errno("asprintf");
123 done:
124 free(hex);
125 free(path_objects);
126 return err;
129 const struct got_error *
130 got_object_open_loose_fd(int *fd, struct got_object_id *id,
131 struct got_repository *repo)
133 const struct got_error *err = NULL;
134 char *path;
136 err = got_object_get_path(&path, id, repo);
137 if (err)
138 return err;
139 *fd = open(path, O_RDONLY | O_NOFOLLOW);
140 if (*fd == -1) {
141 err = got_error_from_errno2("open", path);
142 goto done;
144 done:
145 free(path);
146 return err;
149 static const struct got_error *
150 request_packed_object(struct got_object **obj, struct got_pack *pack, int idx,
151 struct got_object_id *id)
153 const struct got_error *err = NULL;
154 struct imsgbuf *ibuf = pack->privsep_child->ibuf;
156 err = got_privsep_send_packed_obj_req(ibuf, idx, id);
157 if (err)
158 return err;
160 err = got_privsep_recv_obj(obj, ibuf);
161 if (err)
162 return err;
164 memcpy(&(*obj)->id, id, sizeof((*obj)->id));
166 return NULL;
169 static const struct got_error *
170 request_packed_object_raw(uint8_t **outbuf, off_t *size, size_t *hdrlen,
171 int outfd, struct got_pack *pack, int idx, struct got_object_id *id)
173 const struct got_error *err = NULL;
174 struct imsgbuf *ibuf = pack->privsep_child->ibuf;
175 int outfd_child;
176 int basefd, accumfd; /* temporary files for delta application */
178 basefd = got_opentempfd();
179 if (basefd == -1)
180 return got_error_from_errno("got_opentempfd");
182 accumfd = got_opentempfd();
183 if (accumfd == -1) {
184 close(basefd);
185 return got_error_from_errno("got_opentempfd");
188 outfd_child = dup(outfd);
189 if (outfd_child == -1) {
190 err = got_error_from_errno("dup");
191 close(basefd);
192 close(accumfd);
193 return err;
196 err = got_privsep_send_packed_raw_obj_req(ibuf, idx, id);
197 if (err) {
198 close(basefd);
199 close(accumfd);
200 close(outfd_child);
201 return err;
204 err = got_privsep_send_raw_obj_outfd(ibuf, outfd_child);
205 if (err) {
206 close(basefd);
207 close(accumfd);
208 return err;
212 err = got_privsep_send_tmpfd(pack->privsep_child->ibuf,
213 basefd);
214 if (err) {
215 close(accumfd);
216 return err;
219 err = got_privsep_send_tmpfd(pack->privsep_child->ibuf,
220 accumfd);
221 if (err)
222 return err;
224 err = got_privsep_recv_raw_obj(outbuf, size, hdrlen, ibuf);
225 if (err)
226 return err;
228 return NULL;
231 static void
232 set_max_datasize(void)
234 struct rlimit rl;
236 if (getrlimit(RLIMIT_DATA, &rl) != 0)
237 return;
239 rl.rlim_cur = rl.rlim_max;
240 setrlimit(RLIMIT_DATA, &rl);
243 static const struct got_error *
244 start_pack_privsep_child(struct got_pack *pack, struct got_packidx *packidx)
246 const struct got_error *err = NULL;
247 int imsg_fds[2];
248 pid_t pid;
249 struct imsgbuf *ibuf;
251 ibuf = calloc(1, sizeof(*ibuf));
252 if (ibuf == NULL)
253 return got_error_from_errno("calloc");
255 pack->privsep_child = calloc(1, sizeof(*pack->privsep_child));
256 if (pack->privsep_child == NULL) {
257 err = got_error_from_errno("calloc");
258 free(ibuf);
259 return err;
262 if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_fds) == -1) {
263 err = got_error_from_errno("socketpair");
264 goto done;
267 pid = fork();
268 if (pid == -1) {
269 err = got_error_from_errno("fork");
270 goto done;
271 } else if (pid == 0) {
272 set_max_datasize();
273 got_privsep_exec_child(imsg_fds, GOT_PATH_PROG_READ_PACK,
274 pack->path_packfile);
275 /* not reached */
278 if (close(imsg_fds[1]) == -1)
279 return got_error_from_errno("close");
280 pack->privsep_child->imsg_fd = imsg_fds[0];
281 pack->privsep_child->pid = pid;
282 imsg_init(ibuf, imsg_fds[0]);
283 pack->privsep_child->ibuf = ibuf;
285 err = got_privsep_init_pack_child(ibuf, pack, packidx);
286 if (err) {
287 const struct got_error *child_err;
288 err = got_privsep_send_stop(pack->privsep_child->imsg_fd);
289 child_err = got_privsep_wait_for_child(
290 pack->privsep_child->pid);
291 if (child_err && err == NULL)
292 err = child_err;
294 done:
295 if (err) {
296 free(ibuf);
297 free(pack->privsep_child);
298 pack->privsep_child = NULL;
300 return err;
303 static const struct got_error *
304 read_packed_object_privsep(struct got_object **obj,
305 struct got_repository *repo, struct got_pack *pack,
306 struct got_packidx *packidx, int idx, struct got_object_id *id)
308 const struct got_error *err = NULL;
310 if (pack->privsep_child == NULL) {
311 err = start_pack_privsep_child(pack, packidx);
312 if (err)
313 return err;
316 return request_packed_object(obj, pack, idx, id);
319 static const struct got_error *
320 read_packed_object_raw_privsep(uint8_t **outbuf, off_t *size, size_t *hdrlen,
321 int outfd, struct got_pack *pack, struct got_packidx *packidx, int idx,
322 struct got_object_id *id)
324 const struct got_error *err = NULL;
326 if (pack->privsep_child == NULL) {
327 err = start_pack_privsep_child(pack, packidx);
328 if (err)
329 return err;
332 return request_packed_object_raw(outbuf, size, hdrlen, outfd, pack,
333 idx, id);
336 const struct got_error *
337 got_object_open_packed(struct got_object **obj, struct got_object_id *id,
338 struct got_repository *repo)
340 const struct got_error *err = NULL;
341 struct got_pack *pack = NULL;
342 struct got_packidx *packidx = NULL;
343 int idx;
344 char *path_packfile;
346 err = got_repo_search_packidx(&packidx, &idx, repo, id);
347 if (err)
348 return err;
350 err = got_packidx_get_packfile_path(&path_packfile,
351 packidx->path_packidx);
352 if (err)
353 return err;
355 pack = got_repo_get_cached_pack(repo, path_packfile);
356 if (pack == NULL) {
357 err = got_repo_cache_pack(&pack, repo, path_packfile, packidx);
358 if (err)
359 goto done;
362 err = read_packed_object_privsep(obj, repo, pack, packidx, idx, id);
363 if (err)
364 goto done;
365 done:
366 free(path_packfile);
367 return err;
370 static const struct got_error *
371 request_object(struct got_object **obj, struct got_object_id *id,
372 struct got_repository *repo, int fd)
374 const struct got_error *err = NULL;
375 struct imsgbuf *ibuf;
377 ibuf = repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_OBJECT].ibuf;
379 err = got_privsep_send_obj_req(ibuf, fd, id);
380 if (err)
381 return err;
383 return got_privsep_recv_obj(obj, ibuf);
386 static const struct got_error *
387 request_raw_object(uint8_t **outbuf, off_t *size, size_t *hdrlen, int outfd,
388 struct got_object_id *id, struct got_repository *repo, int infd)
390 const struct got_error *err = NULL;
391 struct imsgbuf *ibuf;
392 int outfd_child;
394 ibuf = repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_OBJECT].ibuf;
396 outfd_child = dup(outfd);
397 if (outfd_child == -1)
398 return got_error_from_errno("dup");
400 err = got_privsep_send_raw_obj_req(ibuf, infd, id);
401 if (err)
402 return err;
404 err = got_privsep_send_raw_obj_outfd(ibuf, outfd_child);
405 if (err)
406 return err;
408 return got_privsep_recv_raw_obj(outbuf, size, hdrlen, ibuf);
411 static const struct got_error *
412 start_read_object_child(struct got_repository *repo)
414 const struct got_error *err = NULL;
415 int imsg_fds[2];
416 pid_t pid;
417 struct imsgbuf *ibuf;
419 ibuf = calloc(1, sizeof(*ibuf));
420 if (ibuf == NULL)
421 return got_error_from_errno("calloc");
423 if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_fds) == -1) {
424 err = got_error_from_errno("socketpair");
425 free(ibuf);
426 return err;
429 pid = fork();
430 if (pid == -1) {
431 err = got_error_from_errno("fork");
432 free(ibuf);
433 return err;
435 else if (pid == 0) {
436 got_privsep_exec_child(imsg_fds, GOT_PATH_PROG_READ_OBJECT,
437 repo->path);
438 /* not reached */
441 if (close(imsg_fds[1]) == -1) {
442 err = got_error_from_errno("close");
443 free(ibuf);
444 return err;
447 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_OBJECT].imsg_fd =
448 imsg_fds[0];
449 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_OBJECT].pid = pid;
450 imsg_init(ibuf, imsg_fds[0]);
451 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_OBJECT].ibuf = ibuf;
453 return NULL;
456 const struct got_error *
457 got_object_read_header_privsep(struct got_object **obj,
458 struct got_object_id *id, struct got_repository *repo, int obj_fd)
460 const struct got_error *err;
462 if (repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_OBJECT].imsg_fd != -1)
463 return request_object(obj, id, repo, obj_fd);
465 err = start_read_object_child(repo);
466 if (err) {
467 close(obj_fd);
468 return err;
471 return request_object(obj, id, repo, obj_fd);
474 static const struct got_error *
475 read_object_raw_privsep(uint8_t **outbuf, off_t *size, size_t *hdrlen,
476 int outfd, struct got_object_id *id, struct got_repository *repo,
477 int obj_fd)
479 const struct got_error *err;
481 if (repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_OBJECT].imsg_fd != -1)
482 return request_raw_object(outbuf, size, hdrlen, outfd, id,
483 repo, obj_fd);
485 err = start_read_object_child(repo);
486 if (err)
487 return err;
489 return request_raw_object(outbuf, size, hdrlen, outfd, id, repo,
490 obj_fd);
493 const struct got_error *
494 got_object_open(struct got_object **obj, struct got_repository *repo,
495 struct got_object_id *id)
497 const struct got_error *err = NULL;
498 int fd;
500 *obj = got_repo_get_cached_object(repo, id);
501 if (*obj != NULL) {
502 (*obj)->refcnt++;
503 return NULL;
506 err = got_object_open_packed(obj, id, repo);
507 if (err && err->code != GOT_ERR_NO_OBJ)
508 return err;
509 if (*obj) {
510 (*obj)->refcnt++;
511 return got_repo_cache_object(repo, id, *obj);
514 err = got_object_open_loose_fd(&fd, id, repo);
515 if (err) {
516 if (err->code == GOT_ERR_ERRNO && errno == ENOENT)
517 err = got_error_no_obj(id);
518 return err;
521 err = got_object_read_header_privsep(obj, id, repo, fd);
522 if (err)
523 return err;
525 memcpy((*obj)->id.sha1, id->sha1, SHA1_DIGEST_LENGTH);
527 (*obj)->refcnt++;
528 return got_repo_cache_object(repo, id, *obj);
531 const struct got_error *
532 got_object_raw_open(struct got_raw_object **obj, int outfd,
533 struct got_repository *repo, struct got_object_id *id, size_t blocksize)
535 const struct got_error *err = NULL;
536 struct got_packidx *packidx = NULL;
537 int idx;
538 uint8_t *outbuf = NULL;
539 off_t size = 0;
540 size_t hdrlen = 0;
541 char *path_packfile = NULL;
543 *obj = NULL;
545 err = got_repo_search_packidx(&packidx, &idx, repo, id);
546 if (err == NULL) {
547 struct got_pack *pack = NULL;
549 err = got_packidx_get_packfile_path(&path_packfile,
550 packidx->path_packidx);
551 if (err)
552 goto done;
554 pack = got_repo_get_cached_pack(repo, path_packfile);
555 if (pack == NULL) {
556 err = got_repo_cache_pack(&pack, repo, path_packfile,
557 packidx);
558 if (err)
559 goto done;
561 err = read_packed_object_raw_privsep(&outbuf, &size, &hdrlen,
562 outfd, pack, packidx, idx, id);
563 if (err)
564 goto done;
565 } else if (err->code == GOT_ERR_NO_OBJ) {
566 int fd;
568 err = got_object_open_loose_fd(&fd, id, repo);
569 if (err)
570 goto done;
571 err = read_object_raw_privsep(&outbuf, &size, &hdrlen, outfd,
572 id, repo, fd);
573 if (err)
574 goto done;
577 *obj = calloc(1, sizeof(**obj));
578 if (*obj == NULL) {
579 err = got_error_from_errno("calloc");
580 goto done;
583 (*obj)->read_buf = malloc(blocksize);
584 if ((*obj)->read_buf == NULL) {
585 err = got_error_from_errno("malloc");
586 goto done;
589 if (outbuf) {
590 (*obj)->f = fmemopen(outbuf, hdrlen + size, "r");
591 if ((*obj)->f == NULL) {
592 err = got_error_from_errno("fdopen");
593 goto done;
595 (*obj)->data = outbuf;
596 } else {
597 struct stat sb;
598 if (fstat(outfd, &sb) == -1) {
599 err = got_error_from_errno("fstat");
600 goto done;
603 if (sb.st_size != hdrlen + size) {
604 err = got_error(GOT_ERR_PRIVSEP_LEN);
605 goto done;
608 (*obj)->f = fdopen(outfd, "r");
609 if ((*obj)->f == NULL) {
610 err = got_error_from_errno("fdopen");
611 goto done;
613 (*obj)->data = NULL;
615 (*obj)->hdrlen = hdrlen;
616 (*obj)->size = size;
617 (*obj)->blocksize = blocksize;
618 done:
619 free(path_packfile);
620 if (err) {
621 if (*obj) {
622 got_object_raw_close(*obj);
623 *obj = NULL;
625 free(outbuf);
627 return err;
630 void
631 got_object_raw_rewind(struct got_raw_object *obj)
633 if (obj->f)
634 rewind(obj->f);
637 size_t
638 got_object_raw_get_hdrlen(struct got_raw_object *obj)
640 return obj->hdrlen;
643 const uint8_t *
644 got_object_raw_get_read_buf(struct got_raw_object *obj)
646 return obj->read_buf;
649 const struct got_error *
650 got_object_raw_read_block(size_t *outlenp, struct got_raw_object *obj)
652 size_t n;
654 n = fread(obj->read_buf, 1, obj->blocksize, obj->f);
655 if (n == 0 && ferror(obj->f))
656 return got_ferror(obj->f, GOT_ERR_IO);
657 *outlenp = n;
658 return NULL;
661 const struct got_error *
662 got_object_raw_close(struct got_raw_object *obj)
664 const struct got_error *err = NULL;
666 free(obj->read_buf);
667 if (obj->f != NULL && fclose(obj->f) == EOF && err == NULL)
668 err = got_error_from_errno("fclose");
669 free(obj->data);
670 free(obj);
671 return err;
674 const struct got_error *
675 got_object_open_by_id_str(struct got_object **obj, struct got_repository *repo,
676 const char *id_str)
678 struct got_object_id id;
680 if (!got_parse_sha1_digest(id.sha1, id_str))
681 return got_error_path(id_str, GOT_ERR_BAD_OBJ_ID_STR);
683 return got_object_open(obj, repo, &id);
686 const struct got_error *
687 got_object_resolve_id_str(struct got_object_id **id,
688 struct got_repository *repo, const char *id_str)
690 const struct got_error *err = NULL;
691 struct got_object *obj;
693 err = got_object_open_by_id_str(&obj, repo, id_str);
694 if (err)
695 return err;
697 *id = got_object_id_dup(got_object_get_id(obj));
698 got_object_close(obj);
699 if (*id == NULL)
700 return got_error_from_errno("got_object_id_dup");
702 return NULL;
705 static const struct got_error *
706 request_packed_commit(struct got_commit_object **commit, struct got_pack *pack,
707 int pack_idx, struct got_object_id *id)
709 const struct got_error *err = NULL;
711 err = got_privsep_send_commit_req(pack->privsep_child->ibuf, -1, id,
712 pack_idx);
713 if (err)
714 return err;
716 err = got_privsep_recv_commit(commit, pack->privsep_child->ibuf);
717 if (err)
718 return err;
720 (*commit)->flags |= GOT_COMMIT_FLAG_PACKED;
721 return NULL;
724 static const struct got_error *
725 read_packed_commit_privsep(struct got_commit_object **commit,
726 struct got_pack *pack, struct got_packidx *packidx, int idx,
727 struct got_object_id *id)
729 const struct got_error *err = NULL;
731 if (pack->privsep_child)
732 return request_packed_commit(commit, pack, idx, id);
734 err = start_pack_privsep_child(pack, packidx);
735 if (err)
736 return err;
738 return request_packed_commit(commit, pack, idx, id);
741 static const struct got_error *
742 request_commit(struct got_commit_object **commit, struct got_repository *repo,
743 int fd, struct got_object_id *id)
745 const struct got_error *err = NULL;
746 struct imsgbuf *ibuf;
748 ibuf = repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_COMMIT].ibuf;
750 err = got_privsep_send_commit_req(ibuf, fd, id, -1);
751 if (err)
752 return err;
754 return got_privsep_recv_commit(commit, ibuf);
757 static const struct got_error *
758 read_commit_privsep(struct got_commit_object **commit, int obj_fd,
759 struct got_object_id *id, struct got_repository *repo)
761 const struct got_error *err;
762 int imsg_fds[2];
763 pid_t pid;
764 struct imsgbuf *ibuf;
766 if (repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_COMMIT].imsg_fd != -1)
767 return request_commit(commit, repo, obj_fd, id);
769 ibuf = calloc(1, sizeof(*ibuf));
770 if (ibuf == NULL)
771 return got_error_from_errno("calloc");
773 if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_fds) == -1) {
774 err = got_error_from_errno("socketpair");
775 free(ibuf);
776 return err;
779 pid = fork();
780 if (pid == -1) {
781 err = got_error_from_errno("fork");
782 free(ibuf);
783 return err;
785 else if (pid == 0) {
786 got_privsep_exec_child(imsg_fds, GOT_PATH_PROG_READ_COMMIT,
787 repo->path);
788 /* not reached */
791 if (close(imsg_fds[1]) == -1) {
792 err = got_error_from_errno("close");
793 free(ibuf);
794 return err;
796 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_COMMIT].imsg_fd =
797 imsg_fds[0];
798 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_COMMIT].pid = pid;
799 imsg_init(ibuf, imsg_fds[0]);
800 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_COMMIT].ibuf = ibuf;
802 return request_commit(commit, repo, obj_fd, id);
806 static const struct got_error *
807 open_commit(struct got_commit_object **commit,
808 struct got_repository *repo, struct got_object_id *id, int check_cache)
810 const struct got_error *err = NULL;
811 struct got_packidx *packidx = NULL;
812 int idx;
813 char *path_packfile = NULL;
815 if (check_cache) {
816 *commit = got_repo_get_cached_commit(repo, id);
817 if (*commit != NULL) {
818 (*commit)->refcnt++;
819 return NULL;
821 } else
822 *commit = NULL;
824 err = got_repo_search_packidx(&packidx, &idx, repo, id);
825 if (err == NULL) {
826 struct got_pack *pack = NULL;
828 err = got_packidx_get_packfile_path(&path_packfile,
829 packidx->path_packidx);
830 if (err)
831 return err;
833 pack = got_repo_get_cached_pack(repo, path_packfile);
834 if (pack == NULL) {
835 err = got_repo_cache_pack(&pack, repo, path_packfile,
836 packidx);
837 if (err)
838 goto done;
840 err = read_packed_commit_privsep(commit, pack,
841 packidx, idx, id);
842 } else if (err->code == GOT_ERR_NO_OBJ) {
843 int fd;
845 err = got_object_open_loose_fd(&fd, id, repo);
846 if (err)
847 return err;
848 err = read_commit_privsep(commit, fd, id, repo);
851 if (err == NULL) {
852 (*commit)->refcnt++;
853 err = got_repo_cache_commit(repo, id, *commit);
855 done:
856 free(path_packfile);
857 return err;
860 const struct got_error *
861 got_object_open_as_commit(struct got_commit_object **commit,
862 struct got_repository *repo, struct got_object_id *id)
864 *commit = got_repo_get_cached_commit(repo, id);
865 if (*commit != NULL) {
866 (*commit)->refcnt++;
867 return NULL;
870 return open_commit(commit, repo, id, 0);
873 const struct got_error *
874 got_object_commit_open(struct got_commit_object **commit,
875 struct got_repository *repo, struct got_object *obj)
877 return open_commit(commit, repo, got_object_get_id(obj), 1);
880 const struct got_error *
881 got_object_qid_alloc(struct got_object_qid **qid, struct got_object_id *id)
883 const struct got_error *err = NULL;
885 *qid = calloc(1, sizeof(**qid));
886 if (*qid == NULL)
887 return got_error_from_errno("calloc");
889 (*qid)->id = got_object_id_dup(id);
890 if ((*qid)->id == NULL) {
891 err = got_error_from_errno("got_object_id_dup");
892 got_object_qid_free(*qid);
893 *qid = NULL;
894 return err;
897 return NULL;
900 const struct got_error *
901 got_object_id_queue_copy(const struct got_object_id_queue *src,
902 struct got_object_id_queue *dest)
904 const struct got_error *err;
905 struct got_object_qid *qid;
907 STAILQ_FOREACH(qid, src, entry) {
908 struct got_object_qid *new;
909 /*
910 * Deep-copy the object ID only. Let the caller deal
911 * with setting up the new->data pointer if needed.
912 */
913 err = got_object_qid_alloc(&new, qid->id);
914 if (err) {
915 got_object_id_queue_free(dest);
916 return err;
918 STAILQ_INSERT_TAIL(dest, new, entry);
921 return NULL;
924 static const struct got_error *
925 request_packed_tree(struct got_tree_object **tree, struct got_pack *pack,
926 int pack_idx, struct got_object_id *id)
928 const struct got_error *err = NULL;
930 err = got_privsep_send_tree_req(pack->privsep_child->ibuf, -1, id,
931 pack_idx);
932 if (err)
933 return err;
935 return got_privsep_recv_tree(tree, pack->privsep_child->ibuf);
938 static const struct got_error *
939 read_packed_tree_privsep(struct got_tree_object **tree,
940 struct got_pack *pack, struct got_packidx *packidx, int idx,
941 struct got_object_id *id)
943 const struct got_error *err = NULL;
945 if (pack->privsep_child)
946 return request_packed_tree(tree, pack, idx, id);
948 err = start_pack_privsep_child(pack, packidx);
949 if (err)
950 return err;
952 return request_packed_tree(tree, pack, idx, id);
955 static const struct got_error *
956 request_tree(struct got_tree_object **tree, struct got_repository *repo,
957 int fd, struct got_object_id *id)
959 const struct got_error *err = NULL;
960 struct imsgbuf *ibuf;
962 ibuf = repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TREE].ibuf;
964 err = got_privsep_send_tree_req(ibuf, fd, id, -1);
965 if (err)
966 return err;
968 return got_privsep_recv_tree(tree, ibuf);
971 const struct got_error *
972 read_tree_privsep(struct got_tree_object **tree, int obj_fd,
973 struct got_object_id *id, struct got_repository *repo)
975 const struct got_error *err;
976 int imsg_fds[2];
977 pid_t pid;
978 struct imsgbuf *ibuf;
980 if (repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TREE].imsg_fd != -1)
981 return request_tree(tree, repo, obj_fd, id);
983 ibuf = calloc(1, sizeof(*ibuf));
984 if (ibuf == NULL)
985 return got_error_from_errno("calloc");
987 if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_fds) == -1) {
988 err = got_error_from_errno("socketpair");
989 free(ibuf);
990 return err;
993 pid = fork();
994 if (pid == -1) {
995 err = got_error_from_errno("fork");
996 free(ibuf);
997 return err;
999 else if (pid == 0) {
1000 got_privsep_exec_child(imsg_fds, GOT_PATH_PROG_READ_TREE,
1001 repo->path);
1002 /* not reached */
1005 if (close(imsg_fds[1]) == -1) {
1006 err = got_error_from_errno("close");
1007 free(ibuf);
1008 return err;
1010 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TREE].imsg_fd =
1011 imsg_fds[0];
1012 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TREE].pid = pid;
1013 imsg_init(ibuf, imsg_fds[0]);
1014 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TREE].ibuf = ibuf;
1017 return request_tree(tree, repo, obj_fd, id);
1020 static const struct got_error *
1021 open_tree(struct got_tree_object **tree, struct got_repository *repo,
1022 struct got_object_id *id, int check_cache)
1024 const struct got_error *err = NULL;
1025 struct got_packidx *packidx = NULL;
1026 int idx;
1027 char *path_packfile = NULL;
1029 if (check_cache) {
1030 *tree = got_repo_get_cached_tree(repo, id);
1031 if (*tree != NULL) {
1032 (*tree)->refcnt++;
1033 return NULL;
1035 } else
1036 *tree = NULL;
1038 err = got_repo_search_packidx(&packidx, &idx, repo, id);
1039 if (err == NULL) {
1040 struct got_pack *pack = NULL;
1042 err = got_packidx_get_packfile_path(&path_packfile,
1043 packidx->path_packidx);
1044 if (err)
1045 return err;
1047 pack = got_repo_get_cached_pack(repo, path_packfile);
1048 if (pack == NULL) {
1049 err = got_repo_cache_pack(&pack, repo, path_packfile,
1050 packidx);
1051 if (err)
1052 goto done;
1054 err = read_packed_tree_privsep(tree, pack,
1055 packidx, idx, id);
1056 } else if (err->code == GOT_ERR_NO_OBJ) {
1057 int fd;
1059 err = got_object_open_loose_fd(&fd, id, repo);
1060 if (err)
1061 return err;
1062 err = read_tree_privsep(tree, fd, id, repo);
1065 if (err == NULL) {
1066 (*tree)->refcnt++;
1067 err = got_repo_cache_tree(repo, id, *tree);
1069 done:
1070 free(path_packfile);
1071 return err;
1074 const struct got_error *
1075 got_object_open_as_tree(struct got_tree_object **tree,
1076 struct got_repository *repo, struct got_object_id *id)
1078 *tree = got_repo_get_cached_tree(repo, id);
1079 if (*tree != NULL) {
1080 (*tree)->refcnt++;
1081 return NULL;
1084 return open_tree(tree, repo, id, 0);
1087 const struct got_error *
1088 got_object_tree_open(struct got_tree_object **tree,
1089 struct got_repository *repo, struct got_object *obj)
1091 return open_tree(tree, repo, got_object_get_id(obj), 1);
1094 int
1095 got_object_tree_get_nentries(struct got_tree_object *tree)
1097 return tree->nentries;
1100 struct got_tree_entry *
1101 got_object_tree_get_first_entry(struct got_tree_object *tree)
1103 return got_object_tree_get_entry(tree, 0);
1106 struct got_tree_entry *
1107 got_object_tree_get_last_entry(struct got_tree_object *tree)
1109 return got_object_tree_get_entry(tree, tree->nentries - 1);
1112 struct got_tree_entry *
1113 got_object_tree_get_entry(struct got_tree_object *tree, int i)
1115 if (i < 0 || i >= tree->nentries)
1116 return NULL;
1117 return &tree->entries[i];
1120 mode_t
1121 got_tree_entry_get_mode(struct got_tree_entry *te)
1123 return te->mode;
1126 const char *
1127 got_tree_entry_get_name(struct got_tree_entry *te)
1129 return &te->name[0];
1132 struct got_object_id *
1133 got_tree_entry_get_id(struct got_tree_entry *te)
1135 return &te->id;
1138 const struct got_error *
1139 got_object_blob_read_to_str(char **s, struct got_blob_object *blob)
1141 const struct got_error *err = NULL;
1142 size_t len, totlen, hdrlen, offset;
1144 *s = NULL;
1146 hdrlen = got_object_blob_get_hdrlen(blob);
1147 totlen = 0;
1148 offset = 0;
1149 do {
1150 char *p;
1152 err = got_object_blob_read_block(&len, blob);
1153 if (err)
1154 return err;
1156 if (len == 0)
1157 break;
1159 totlen += len - hdrlen;
1160 p = realloc(*s, totlen + 1);
1161 if (p == NULL) {
1162 err = got_error_from_errno("realloc");
1163 free(*s);
1164 *s = NULL;
1165 return err;
1167 *s = p;
1168 /* Skip blob object header first time around. */
1169 memcpy(*s + offset,
1170 got_object_blob_get_read_buf(blob) + hdrlen, len - hdrlen);
1171 hdrlen = 0;
1172 offset = totlen;
1173 } while (len > 0);
1175 (*s)[totlen] = '\0';
1176 return NULL;
1179 const struct got_error *
1180 got_tree_entry_get_symlink_target(char **link_target, struct got_tree_entry *te,
1181 struct got_repository *repo)
1183 const struct got_error *err = NULL;
1184 struct got_blob_object *blob = NULL;
1186 *link_target = NULL;
1188 if (!got_object_tree_entry_is_symlink(te))
1189 return got_error(GOT_ERR_TREE_ENTRY_TYPE);
1191 err = got_object_open_as_blob(&blob, repo,
1192 got_tree_entry_get_id(te), PATH_MAX);
1193 if (err)
1194 return err;
1196 err = got_object_blob_read_to_str(link_target, blob);
1197 got_object_blob_close(blob);
1198 if (err) {
1199 free(*link_target);
1200 *link_target = NULL;
1202 return err;
1205 int
1206 got_tree_entry_get_index(struct got_tree_entry *te)
1208 return te->idx;
1211 struct got_tree_entry *
1212 got_tree_entry_get_next(struct got_tree_object *tree,
1213 struct got_tree_entry *te)
1215 return got_object_tree_get_entry(tree, te->idx + 1);
1218 struct got_tree_entry *
1219 got_tree_entry_get_prev(struct got_tree_object *tree,
1220 struct got_tree_entry *te)
1222 return got_object_tree_get_entry(tree, te->idx - 1);
1225 static const struct got_error *
1226 request_packed_blob(uint8_t **outbuf, size_t *size, size_t *hdrlen, int outfd,
1227 struct got_pack *pack, struct got_packidx *packidx, int idx,
1228 struct got_object_id *id)
1230 const struct got_error *err = NULL;
1231 int outfd_child;
1232 int basefd, accumfd; /* temporary files for delta application */
1234 basefd = got_opentempfd();
1235 if (basefd == -1)
1236 return got_error_from_errno("got_opentempfd");
1237 accumfd = got_opentempfd();
1238 if (accumfd == -1)
1239 return got_error_from_errno("got_opentempfd");
1241 outfd_child = dup(outfd);
1242 if (outfd_child == -1)
1243 return got_error_from_errno("dup");
1245 err = got_privsep_send_blob_req(pack->privsep_child->ibuf, -1, id, idx);
1246 if (err)
1247 return err;
1249 err = got_privsep_send_blob_outfd(pack->privsep_child->ibuf,
1250 outfd_child);
1251 if (err) {
1252 close(basefd);
1253 close(accumfd);
1254 return err;
1257 err = got_privsep_send_tmpfd(pack->privsep_child->ibuf,
1258 basefd);
1259 if (err) {
1260 close(accumfd);
1261 return err;
1264 err = got_privsep_send_tmpfd(pack->privsep_child->ibuf,
1265 accumfd);
1266 if (err)
1267 return err;
1269 err = got_privsep_recv_blob(outbuf, size, hdrlen,
1270 pack->privsep_child->ibuf);
1271 if (err)
1272 return err;
1274 if (lseek(outfd, SEEK_SET, 0) == -1)
1275 err = got_error_from_errno("lseek");
1277 return err;
1280 static const struct got_error *
1281 read_packed_blob_privsep(uint8_t **outbuf, size_t *size, size_t *hdrlen,
1282 int outfd, struct got_pack *pack, struct got_packidx *packidx, int idx,
1283 struct got_object_id *id)
1285 const struct got_error *err = NULL;
1287 if (pack->privsep_child == NULL) {
1288 err = start_pack_privsep_child(pack, packidx);
1289 if (err)
1290 return err;
1293 return request_packed_blob(outbuf, size, hdrlen, outfd, pack, packidx,
1294 idx, id);
1297 static const struct got_error *
1298 request_blob(uint8_t **outbuf, size_t *size, size_t *hdrlen, int outfd,
1299 int infd, struct got_object_id *id, struct imsgbuf *ibuf)
1301 const struct got_error *err = NULL;
1302 int outfd_child;
1304 outfd_child = dup(outfd);
1305 if (outfd_child == -1)
1306 return got_error_from_errno("dup");
1308 err = got_privsep_send_blob_req(ibuf, infd, id, -1);
1309 if (err)
1310 return err;
1312 err = got_privsep_send_blob_outfd(ibuf, outfd_child);
1313 if (err)
1314 return err;
1316 err = got_privsep_recv_blob(outbuf, size, hdrlen, ibuf);
1317 if (err)
1318 return err;
1320 if (lseek(outfd, SEEK_SET, 0) == -1)
1321 return got_error_from_errno("lseek");
1323 return err;
1326 static const struct got_error *
1327 read_blob_privsep(uint8_t **outbuf, size_t *size, size_t *hdrlen,
1328 int outfd, int infd, struct got_object_id *id, struct got_repository *repo)
1330 const struct got_error *err;
1331 int imsg_fds[2];
1332 pid_t pid;
1333 struct imsgbuf *ibuf;
1335 if (repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_BLOB].imsg_fd != -1) {
1336 ibuf = repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_BLOB].ibuf;
1337 return request_blob(outbuf, size, hdrlen, outfd, infd, id,
1338 ibuf);
1341 ibuf = calloc(1, sizeof(*ibuf));
1342 if (ibuf == NULL)
1343 return got_error_from_errno("calloc");
1345 if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_fds) == -1) {
1346 err = got_error_from_errno("socketpair");
1347 free(ibuf);
1348 return err;
1351 pid = fork();
1352 if (pid == -1) {
1353 err = got_error_from_errno("fork");
1354 free(ibuf);
1355 return err;
1357 else if (pid == 0) {
1358 got_privsep_exec_child(imsg_fds, GOT_PATH_PROG_READ_BLOB,
1359 repo->path);
1360 /* not reached */
1363 if (close(imsg_fds[1]) == -1) {
1364 err = got_error_from_errno("close");
1365 free(ibuf);
1366 return err;
1368 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_BLOB].imsg_fd =
1369 imsg_fds[0];
1370 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_BLOB].pid = pid;
1371 imsg_init(ibuf, imsg_fds[0]);
1372 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_BLOB].ibuf = ibuf;
1374 return request_blob(outbuf, size, hdrlen, outfd, infd, id, ibuf);
1377 static const struct got_error *
1378 open_blob(struct got_blob_object **blob, struct got_repository *repo,
1379 struct got_object_id *id, size_t blocksize)
1381 const struct got_error *err = NULL;
1382 struct got_packidx *packidx = NULL;
1383 int idx;
1384 char *path_packfile = NULL;
1385 uint8_t *outbuf;
1386 int outfd;
1387 size_t size, hdrlen;
1388 struct stat sb;
1390 *blob = calloc(1, sizeof(**blob));
1391 if (*blob == NULL)
1392 return got_error_from_errno("calloc");
1394 outfd = got_opentempfd();
1395 if (outfd == -1)
1396 return got_error_from_errno("got_opentempfd");
1398 (*blob)->read_buf = malloc(blocksize);
1399 if ((*blob)->read_buf == NULL) {
1400 err = got_error_from_errno("malloc");
1401 goto done;
1404 err = got_repo_search_packidx(&packidx, &idx, repo, id);
1405 if (err == NULL) {
1406 struct got_pack *pack = NULL;
1408 err = got_packidx_get_packfile_path(&path_packfile,
1409 packidx->path_packidx);
1410 if (err)
1411 goto done;
1413 pack = got_repo_get_cached_pack(repo, path_packfile);
1414 if (pack == NULL) {
1415 err = got_repo_cache_pack(&pack, repo, path_packfile,
1416 packidx);
1417 if (err)
1418 goto done;
1420 err = read_packed_blob_privsep(&outbuf, &size, &hdrlen, outfd,
1421 pack, packidx, idx, id);
1422 } else if (err->code == GOT_ERR_NO_OBJ) {
1423 int infd;
1425 err = got_object_open_loose_fd(&infd, id, repo);
1426 if (err)
1427 goto done;
1428 err = read_blob_privsep(&outbuf, &size, &hdrlen, outfd, infd,
1429 id, repo);
1431 if (err)
1432 goto done;
1434 if (hdrlen > size) {
1435 err = got_error(GOT_ERR_BAD_OBJ_HDR);
1436 goto done;
1439 if (outbuf) {
1440 if (close(outfd) == -1 && err == NULL)
1441 err = got_error_from_errno("close");
1442 outfd = -1;
1443 (*blob)->f = fmemopen(outbuf, size, "rb");
1444 if ((*blob)->f == NULL) {
1445 err = got_error_from_errno("fmemopen");
1446 free(outbuf);
1447 goto done;
1449 (*blob)->data = outbuf;
1450 } else {
1451 if (fstat(outfd, &sb) == -1) {
1452 err = got_error_from_errno("fstat");
1453 goto done;
1456 if (sb.st_size != size) {
1457 err = got_error(GOT_ERR_PRIVSEP_LEN);
1458 goto done;
1461 (*blob)->f = fdopen(outfd, "rb");
1462 if ((*blob)->f == NULL) {
1463 err = got_error_from_errno("fdopen");
1464 close(outfd);
1465 outfd = -1;
1466 goto done;
1470 (*blob)->hdrlen = hdrlen;
1471 (*blob)->blocksize = blocksize;
1472 memcpy(&(*blob)->id.sha1, id->sha1, SHA1_DIGEST_LENGTH);
1474 done:
1475 free(path_packfile);
1476 if (err) {
1477 if (*blob) {
1478 got_object_blob_close(*blob);
1479 *blob = NULL;
1480 } else if (outfd != -1)
1481 close(outfd);
1483 return err;
1486 const struct got_error *
1487 got_object_open_as_blob(struct got_blob_object **blob,
1488 struct got_repository *repo, struct got_object_id *id,
1489 size_t blocksize)
1491 return open_blob(blob, repo, id, blocksize);
1494 const struct got_error *
1495 got_object_blob_open(struct got_blob_object **blob,
1496 struct got_repository *repo, struct got_object *obj, size_t blocksize)
1498 return open_blob(blob, repo, got_object_get_id(obj), blocksize);
1501 const struct got_error *
1502 got_object_blob_close(struct got_blob_object *blob)
1504 const struct got_error *err = NULL;
1505 free(blob->read_buf);
1506 if (blob->f && fclose(blob->f) == EOF)
1507 err = got_error_from_errno("fclose");
1508 free(blob->data);
1509 free(blob);
1510 return err;
1513 void
1514 got_object_blob_rewind(struct got_blob_object *blob)
1516 if (blob->f)
1517 rewind(blob->f);
1520 char *
1521 got_object_blob_id_str(struct got_blob_object *blob, char *buf, size_t size)
1523 return got_sha1_digest_to_str(blob->id.sha1, buf, size);
1526 size_t
1527 got_object_blob_get_hdrlen(struct got_blob_object *blob)
1529 return blob->hdrlen;
1532 const uint8_t *
1533 got_object_blob_get_read_buf(struct got_blob_object *blob)
1535 return blob->read_buf;
1538 const struct got_error *
1539 got_object_blob_read_block(size_t *outlenp, struct got_blob_object *blob)
1541 size_t n;
1543 n = fread(blob->read_buf, 1, blob->blocksize, blob->f);
1544 if (n == 0 && ferror(blob->f))
1545 return got_ferror(blob->f, GOT_ERR_IO);
1546 *outlenp = n;
1547 return NULL;
1550 const struct got_error *
1551 got_object_blob_dump_to_file(off_t *filesize, int *nlines,
1552 off_t **line_offsets, FILE *outfile, struct got_blob_object *blob)
1554 const struct got_error *err = NULL;
1555 size_t n, len, hdrlen;
1556 const uint8_t *buf;
1557 int i;
1558 const int alloc_chunksz = 512;
1559 size_t nalloc = 0;
1560 off_t off = 0, total_len = 0;
1562 if (line_offsets)
1563 *line_offsets = NULL;
1564 if (filesize)
1565 *filesize = 0;
1566 if (nlines)
1567 *nlines = 0;
1569 hdrlen = got_object_blob_get_hdrlen(blob);
1570 do {
1571 err = got_object_blob_read_block(&len, blob);
1572 if (err)
1573 return err;
1574 if (len == 0)
1575 break;
1576 buf = got_object_blob_get_read_buf(blob);
1577 i = hdrlen;
1578 if (nlines) {
1579 if (line_offsets && *line_offsets == NULL) {
1580 /* Have some data but perhaps no '\n'. */
1581 *nlines = 1;
1582 nalloc = alloc_chunksz;
1583 *line_offsets = calloc(nalloc,
1584 sizeof(**line_offsets));
1585 if (*line_offsets == NULL)
1586 return got_error_from_errno("calloc");
1588 /* Skip forward over end of first line. */
1589 while (i < len) {
1590 if (buf[i] == '\n')
1591 break;
1592 i++;
1595 /* Scan '\n' offsets in remaining chunk of data. */
1596 while (i < len) {
1597 if (buf[i] != '\n') {
1598 i++;
1599 continue;
1601 (*nlines)++;
1602 if (line_offsets && nalloc < *nlines) {
1603 size_t n = *nlines + alloc_chunksz;
1604 off_t *o = recallocarray(*line_offsets,
1605 nalloc, n, sizeof(**line_offsets));
1606 if (o == NULL) {
1607 free(*line_offsets);
1608 *line_offsets = NULL;
1609 return got_error_from_errno(
1610 "recallocarray");
1612 *line_offsets = o;
1613 nalloc = n;
1615 if (line_offsets) {
1616 off = total_len + i - hdrlen + 1;
1617 (*line_offsets)[*nlines - 1] = off;
1619 i++;
1622 /* Skip blob object header first time around. */
1623 n = fwrite(buf + hdrlen, 1, len - hdrlen, outfile);
1624 if (n != len - hdrlen)
1625 return got_ferror(outfile, GOT_ERR_IO);
1626 total_len += len - hdrlen;
1627 hdrlen = 0;
1628 } while (len != 0);
1630 if (fflush(outfile) != 0)
1631 return got_error_from_errno("fflush");
1632 rewind(outfile);
1634 if (filesize)
1635 *filesize = total_len;
1637 return NULL;
1640 static const struct got_error *
1641 request_packed_tag(struct got_tag_object **tag, struct got_pack *pack,
1642 int pack_idx, struct got_object_id *id)
1644 const struct got_error *err = NULL;
1646 err = got_privsep_send_tag_req(pack->privsep_child->ibuf, -1, id,
1647 pack_idx);
1648 if (err)
1649 return err;
1651 return got_privsep_recv_tag(tag, pack->privsep_child->ibuf);
1654 static const struct got_error *
1655 read_packed_tag_privsep(struct got_tag_object **tag,
1656 struct got_pack *pack, struct got_packidx *packidx, int idx,
1657 struct got_object_id *id)
1659 const struct got_error *err = NULL;
1661 if (pack->privsep_child)
1662 return request_packed_tag(tag, pack, idx, id);
1664 err = start_pack_privsep_child(pack, packidx);
1665 if (err)
1666 return err;
1668 return request_packed_tag(tag, pack, idx, id);
1671 static const struct got_error *
1672 request_tag(struct got_tag_object **tag, struct got_repository *repo,
1673 int fd, struct got_object_id *id)
1675 const struct got_error *err = NULL;
1676 struct imsgbuf *ibuf;
1678 ibuf = repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TAG].ibuf;
1680 err = got_privsep_send_tag_req(ibuf, fd, id, -1);
1681 if (err)
1682 return err;
1684 return got_privsep_recv_tag(tag, ibuf);
1687 static const struct got_error *
1688 read_tag_privsep(struct got_tag_object **tag, int obj_fd,
1689 struct got_object_id *id, struct got_repository *repo)
1691 const struct got_error *err;
1692 int imsg_fds[2];
1693 pid_t pid;
1694 struct imsgbuf *ibuf;
1696 if (repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TAG].imsg_fd != -1)
1697 return request_tag(tag, repo, obj_fd, id);
1699 ibuf = calloc(1, sizeof(*ibuf));
1700 if (ibuf == NULL)
1701 return got_error_from_errno("calloc");
1703 if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_fds) == -1) {
1704 err = got_error_from_errno("socketpair");
1705 free(ibuf);
1706 return err;
1709 pid = fork();
1710 if (pid == -1) {
1711 err = got_error_from_errno("fork");
1712 free(ibuf);
1713 return err;
1715 else if (pid == 0) {
1716 got_privsep_exec_child(imsg_fds, GOT_PATH_PROG_READ_TAG,
1717 repo->path);
1718 /* not reached */
1721 if (close(imsg_fds[1]) == -1) {
1722 err = got_error_from_errno("close");
1723 free(ibuf);
1724 return err;
1726 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TAG].imsg_fd =
1727 imsg_fds[0];
1728 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TAG].pid = pid;
1729 imsg_init(ibuf, imsg_fds[0]);
1730 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TAG].ibuf = ibuf;
1732 return request_tag(tag, repo, obj_fd, id);
1735 static const struct got_error *
1736 open_tag(struct got_tag_object **tag, struct got_repository *repo,
1737 struct got_object_id *id, int check_cache)
1739 const struct got_error *err = NULL;
1740 struct got_packidx *packidx = NULL;
1741 int idx;
1742 char *path_packfile = NULL;
1743 struct got_object *obj = NULL;
1744 int obj_type = GOT_OBJ_TYPE_ANY;
1746 if (check_cache) {
1747 *tag = got_repo_get_cached_tag(repo, id);
1748 if (*tag != NULL) {
1749 (*tag)->refcnt++;
1750 return NULL;
1752 } else
1753 *tag = NULL;
1755 err = got_repo_search_packidx(&packidx, &idx, repo, id);
1756 if (err == NULL) {
1757 struct got_pack *pack = NULL;
1759 err = got_packidx_get_packfile_path(&path_packfile,
1760 packidx->path_packidx);
1761 if (err)
1762 return err;
1764 pack = got_repo_get_cached_pack(repo, path_packfile);
1765 if (pack == NULL) {
1766 err = got_repo_cache_pack(&pack, repo, path_packfile,
1767 packidx);
1768 if (err)
1769 goto done;
1772 /* Beware of "lightweight" tags: Check object type first. */
1773 err = read_packed_object_privsep(&obj, repo, pack, packidx,
1774 idx, id);
1775 if (err)
1776 goto done;
1777 obj_type = obj->type;
1778 got_object_close(obj);
1779 if (obj_type != GOT_OBJ_TYPE_TAG) {
1780 err = got_error(GOT_ERR_OBJ_TYPE);
1781 goto done;
1783 err = read_packed_tag_privsep(tag, pack, packidx, idx, id);
1784 } else if (err->code == GOT_ERR_NO_OBJ) {
1785 int fd;
1787 err = got_object_open_loose_fd(&fd, id, repo);
1788 if (err)
1789 return err;
1790 err = got_object_read_header_privsep(&obj, id, repo, fd);
1791 if (err)
1792 return err;
1793 obj_type = obj->type;
1794 got_object_close(obj);
1795 if (obj_type != GOT_OBJ_TYPE_TAG)
1796 return got_error(GOT_ERR_OBJ_TYPE);
1798 err = got_object_open_loose_fd(&fd, id, repo);
1799 if (err)
1800 return err;
1801 err = read_tag_privsep(tag, fd, id, repo);
1804 if (err == NULL) {
1805 (*tag)->refcnt++;
1806 err = got_repo_cache_tag(repo, id, *tag);
1808 done:
1809 free(path_packfile);
1810 return err;
1813 const struct got_error *
1814 got_object_open_as_tag(struct got_tag_object **tag,
1815 struct got_repository *repo, struct got_object_id *id)
1817 *tag = got_repo_get_cached_tag(repo, id);
1818 if (*tag != NULL) {
1819 (*tag)->refcnt++;
1820 return NULL;
1823 return open_tag(tag, repo, id, 0);
1826 const struct got_error *
1827 got_object_tag_open(struct got_tag_object **tag,
1828 struct got_repository *repo, struct got_object *obj)
1830 return open_tag(tag, repo, got_object_get_id(obj), 1);
1833 const char *
1834 got_object_tag_get_name(struct got_tag_object *tag)
1836 return tag->tag;
1839 int
1840 got_object_tag_get_object_type(struct got_tag_object *tag)
1842 return tag->obj_type;
1845 struct got_object_id *
1846 got_object_tag_get_object_id(struct got_tag_object *tag)
1848 return &tag->id;
1851 time_t
1852 got_object_tag_get_tagger_time(struct got_tag_object *tag)
1854 return tag->tagger_time;
1857 time_t
1858 got_object_tag_get_tagger_gmtoff(struct got_tag_object *tag)
1860 return tag->tagger_gmtoff;
1863 const char *
1864 got_object_tag_get_tagger(struct got_tag_object *tag)
1866 return tag->tagger;
1869 const char *
1870 got_object_tag_get_message(struct got_tag_object *tag)
1872 return tag->tagmsg;
1875 static struct got_tree_entry *
1876 find_entry_by_name(struct got_tree_object *tree, const char *name, size_t len)
1878 int i;
1880 /* Note that tree entries are sorted in strncmp() order. */
1881 for (i = 0; i < tree->nentries; i++) {
1882 struct got_tree_entry *te = &tree->entries[i];
1883 int cmp = strncmp(te->name, name, len);
1884 if (cmp < 0)
1885 continue;
1886 if (cmp > 0)
1887 break;
1888 if (te->name[len] == '\0')
1889 return te;
1891 return NULL;
1894 struct got_tree_entry *
1895 got_object_tree_find_entry(struct got_tree_object *tree, const char *name)
1897 return find_entry_by_name(tree, name, strlen(name));
1900 const struct got_error *
1901 got_object_tree_find_path(struct got_object_id **id, mode_t *mode,
1902 struct got_repository *repo, struct got_tree_object *tree,
1903 const char *path)
1905 const struct got_error *err = NULL;
1906 struct got_tree_object *subtree = NULL;
1907 struct got_tree_entry *te = NULL;
1908 const char *seg, *s;
1909 size_t seglen;
1911 *id = NULL;
1913 s = path;
1914 while (s[0] == '/')
1915 s++;
1916 seg = s;
1917 seglen = 0;
1918 subtree = tree;
1919 while (*s) {
1920 struct got_tree_object *next_tree;
1922 if (*s != '/') {
1923 s++;
1924 seglen++;
1925 if (*s)
1926 continue;
1929 te = find_entry_by_name(subtree, seg, seglen);
1930 if (te == NULL) {
1931 err = got_error_path(path, GOT_ERR_NO_TREE_ENTRY);
1932 goto done;
1935 if (*s == '\0')
1936 break;
1938 seg = s + 1;
1939 seglen = 0;
1940 s++;
1941 if (*s) {
1942 err = got_object_open_as_tree(&next_tree, repo,
1943 &te->id);
1944 te = NULL;
1945 if (err)
1946 goto done;
1947 if (subtree != tree)
1948 got_object_tree_close(subtree);
1949 subtree = next_tree;
1953 if (te) {
1954 *id = got_object_id_dup(&te->id);
1955 if (*id == NULL)
1956 return got_error_from_errno("got_object_id_dup");
1957 if (mode)
1958 *mode = te->mode;
1959 } else
1960 err = got_error_path(path, GOT_ERR_NO_TREE_ENTRY);
1961 done:
1962 if (subtree && subtree != tree)
1963 got_object_tree_close(subtree);
1964 return err;
1966 const struct got_error *
1967 got_object_id_by_path(struct got_object_id **id, struct got_repository *repo,
1968 struct got_object_id *commit_id, const char *path)
1970 const struct got_error *err = NULL;
1971 struct got_commit_object *commit = NULL;
1972 struct got_tree_object *tree = NULL;
1974 *id = NULL;
1976 err = got_object_open_as_commit(&commit, repo, commit_id);
1977 if (err)
1978 goto done;
1980 /* Handle opening of root of commit's tree. */
1981 if (got_path_is_root_dir(path)) {
1982 *id = got_object_id_dup(commit->tree_id);
1983 if (*id == NULL)
1984 err = got_error_from_errno("got_object_id_dup");
1985 } else {
1986 err = got_object_open_as_tree(&tree, repo, commit->tree_id);
1987 if (err)
1988 goto done;
1989 err = got_object_tree_find_path(id, NULL, repo, tree, path);
1991 done:
1992 if (commit)
1993 got_object_commit_close(commit);
1994 if (tree)
1995 got_object_tree_close(tree);
1996 return err;
2000 * Normalize file mode bits to avoid false positive tree entry differences
2001 * in case tree entries have unexpected mode bits set.
2003 static mode_t
2004 normalize_mode_for_comparison(mode_t mode)
2007 * For directories, the only relevant bit is the IFDIR bit.
2008 * This allows us to detect paths changing from a directory
2009 * to a file and vice versa.
2011 if (S_ISDIR(mode))
2012 return mode & S_IFDIR;
2015 * For symlinks, the only relevant bit is the IFLNK bit.
2016 * This allows us to detect paths changing from a symlinks
2017 * to a file or directory and vice versa.
2019 if (S_ISLNK(mode))
2020 return mode & S_IFLNK;
2022 /* For files, the only change we care about is the executable bit. */
2023 return mode & S_IXUSR;
2026 const struct got_error *
2027 got_object_tree_path_changed(int *changed,
2028 struct got_tree_object *tree01, struct got_tree_object *tree02,
2029 const char *path, struct got_repository *repo)
2031 const struct got_error *err = NULL;
2032 struct got_tree_object *tree1 = NULL, *tree2 = NULL;
2033 struct got_tree_entry *te1 = NULL, *te2 = NULL;
2034 const char *seg, *s;
2035 size_t seglen;
2037 *changed = 0;
2039 /* We not do support comparing the root path. */
2040 if (got_path_is_root_dir(path))
2041 return got_error_path(path, GOT_ERR_BAD_PATH);
2043 tree1 = tree01;
2044 tree2 = tree02;
2045 s = path;
2046 while (*s == '/')
2047 s++;
2048 seg = s;
2049 seglen = 0;
2050 while (*s) {
2051 struct got_tree_object *next_tree1, *next_tree2;
2052 mode_t mode1, mode2;
2054 if (*s != '/') {
2055 s++;
2056 seglen++;
2057 if (*s)
2058 continue;
2061 te1 = find_entry_by_name(tree1, seg, seglen);
2062 if (te1 == NULL) {
2063 err = got_error(GOT_ERR_NO_OBJ);
2064 goto done;
2067 if (tree2)
2068 te2 = find_entry_by_name(tree2, seg, seglen);
2070 if (te2) {
2071 mode1 = normalize_mode_for_comparison(te1->mode);
2072 mode2 = normalize_mode_for_comparison(te2->mode);
2073 if (mode1 != mode2) {
2074 *changed = 1;
2075 goto done;
2078 if (got_object_id_cmp(&te1->id, &te2->id) == 0) {
2079 *changed = 0;
2080 goto done;
2084 if (*s == '\0') { /* final path element */
2085 *changed = 1;
2086 goto done;
2089 seg = s + 1;
2090 s++;
2091 seglen = 0;
2092 if (*s) {
2093 err = got_object_open_as_tree(&next_tree1, repo,
2094 &te1->id);
2095 te1 = NULL;
2096 if (err)
2097 goto done;
2098 if (tree1 != tree01)
2099 got_object_tree_close(tree1);
2100 tree1 = next_tree1;
2102 if (te2) {
2103 err = got_object_open_as_tree(&next_tree2, repo,
2104 &te2->id);
2105 te2 = NULL;
2106 if (err)
2107 goto done;
2108 if (tree2 != tree02)
2109 got_object_tree_close(tree2);
2110 tree2 = next_tree2;
2111 } else if (tree2) {
2112 if (tree2 != tree02)
2113 got_object_tree_close(tree2);
2114 tree2 = NULL;
2118 done:
2119 if (tree1 && tree1 != tree01)
2120 got_object_tree_close(tree1);
2121 if (tree2 && tree2 != tree02)
2122 got_object_tree_close(tree2);
2123 return err;
2126 const struct got_error *
2127 got_object_tree_entry_dup(struct got_tree_entry **new_te,
2128 struct got_tree_entry *te)
2130 const struct got_error *err = NULL;
2132 *new_te = calloc(1, sizeof(**new_te));
2133 if (*new_te == NULL)
2134 return got_error_from_errno("calloc");
2136 (*new_te)->mode = te->mode;
2137 memcpy((*new_te)->name, te->name, sizeof((*new_te)->name));
2138 memcpy(&(*new_te)->id, &te->id, sizeof((*new_te)->id));
2139 return err;
2142 int
2143 got_object_tree_entry_is_submodule(struct got_tree_entry *te)
2145 return (te->mode & S_IFMT) == (S_IFDIR | S_IFLNK);
2148 int
2149 got_object_tree_entry_is_symlink(struct got_tree_entry *te)
2151 /* S_IFDIR check avoids confusing symlinks with submodules. */
2152 return ((te->mode & (S_IFDIR | S_IFLNK)) == S_IFLNK);
2155 static const struct got_error *
2156 resolve_symlink(char **link_target, const char *path,
2157 struct got_object_id *commit_id, struct got_repository *repo)
2159 const struct got_error *err = NULL;
2160 char buf[PATH_MAX];
2161 char *name, *parent_path = NULL;
2162 struct got_object_id *tree_obj_id = NULL;
2163 struct got_tree_object *tree = NULL;
2164 struct got_tree_entry *te = NULL;
2166 *link_target = NULL;
2168 if (strlcpy(buf, path, sizeof(buf)) >= sizeof(buf))
2169 return got_error(GOT_ERR_NO_SPACE);
2171 name = basename(buf);
2172 if (name == NULL)
2173 return got_error_from_errno2("basename", path);
2175 err = got_path_dirname(&parent_path, path);
2176 if (err)
2177 return err;
2179 err = got_object_id_by_path(&tree_obj_id, repo, commit_id,
2180 parent_path);
2181 if (err) {
2182 if (err->code == GOT_ERR_NO_TREE_ENTRY) {
2183 /* Display the complete path in error message. */
2184 err = got_error_path(path, err->code);
2186 goto done;
2189 err = got_object_open_as_tree(&tree, repo, tree_obj_id);
2190 if (err)
2191 goto done;
2193 te = got_object_tree_find_entry(tree, name);
2194 if (te == NULL) {
2195 err = got_error_path(path, GOT_ERR_NO_TREE_ENTRY);
2196 goto done;
2199 if (got_object_tree_entry_is_symlink(te)) {
2200 err = got_tree_entry_get_symlink_target(link_target, te, repo);
2201 if (err)
2202 goto done;
2203 if (!got_path_is_absolute(*link_target)) {
2204 char *abspath;
2205 if (asprintf(&abspath, "%s/%s", parent_path,
2206 *link_target) == -1) {
2207 err = got_error_from_errno("asprintf");
2208 goto done;
2210 free(*link_target);
2211 *link_target = malloc(PATH_MAX);
2212 if (*link_target == NULL) {
2213 err = got_error_from_errno("malloc");
2214 goto done;
2216 err = got_canonpath(abspath, *link_target, PATH_MAX);
2217 free(abspath);
2218 if (err)
2219 goto done;
2222 done:
2223 free(tree_obj_id);
2224 if (tree)
2225 got_object_tree_close(tree);
2226 if (err) {
2227 free(*link_target);
2228 *link_target = NULL;
2230 return err;
2233 const struct got_error *
2234 got_object_resolve_symlinks(char **link_target, const char *path,
2235 struct got_object_id *commit_id, struct got_repository *repo)
2237 const struct got_error *err = NULL;
2238 char *next_target = NULL;
2239 int max_recursion = 40; /* matches Git */
2241 *link_target = NULL;
2243 do {
2244 err = resolve_symlink(&next_target,
2245 *link_target ? *link_target : path, commit_id, repo);
2246 if (err)
2247 break;
2248 if (next_target) {
2249 free(*link_target);
2250 if (--max_recursion == 0) {
2251 err = got_error_path(path, GOT_ERR_RECURSION);
2252 *link_target = NULL;
2253 break;
2255 *link_target = next_target;
2257 } while (next_target);
2259 return err;
2262 const struct got_error *
2263 got_traverse_packed_commits(struct got_object_id_queue *traversed_commits,
2264 struct got_object_id *commit_id, const char *path,
2265 struct got_repository *repo)
2267 const struct got_error *err = NULL;
2268 struct got_pack *pack = NULL;
2269 struct got_packidx *packidx = NULL;
2270 char *path_packfile = NULL;
2271 struct got_commit_object *changed_commit = NULL;
2272 struct got_object_id *changed_commit_id = NULL;
2273 int idx;
2275 err = got_repo_search_packidx(&packidx, &idx, repo, commit_id);
2276 if (err) {
2277 if (err->code != GOT_ERR_NO_OBJ)
2278 return err;
2279 return NULL;
2282 err = got_packidx_get_packfile_path(&path_packfile,
2283 packidx->path_packidx);
2284 if (err)
2285 return err;
2287 pack = got_repo_get_cached_pack(repo, path_packfile);
2288 if (pack == NULL) {
2289 err = got_repo_cache_pack(&pack, repo, path_packfile, packidx);
2290 if (err)
2291 goto done;
2294 if (pack->privsep_child == NULL) {
2295 err = start_pack_privsep_child(pack, packidx);
2296 if (err)
2297 goto done;
2300 err = got_privsep_send_commit_traversal_request(
2301 pack->privsep_child->ibuf, commit_id, idx, path);
2302 if (err)
2303 goto done;
2305 err = got_privsep_recv_traversed_commits(&changed_commit,
2306 &changed_commit_id, traversed_commits, pack->privsep_child->ibuf);
2307 if (err)
2308 goto done;
2310 if (changed_commit) {
2312 * Cache the commit in which the path was changed.
2313 * This commit might be opened again soon.
2315 changed_commit->refcnt++;
2316 err = got_repo_cache_commit(repo, changed_commit_id,
2317 changed_commit);
2318 got_object_commit_close(changed_commit);
2320 done:
2321 free(path_packfile);
2322 free(changed_commit_id);
2323 return err;