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/mman.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 #ifndef nitems
63 #define nitems(_a) (sizeof((_a)) / sizeof((_a)[0]))
64 #endif
66 struct got_object_id *
67 got_object_get_id(struct got_object *obj)
68 {
69 return &obj->id;
70 }
72 const struct got_error *
73 got_object_get_id_str(char **outbuf, struct got_object *obj)
74 {
75 return got_object_id_str(outbuf, &obj->id);
76 }
78 const struct got_error *
79 got_object_get_type(int *type, struct got_repository *repo,
80 struct got_object_id *id)
81 {
82 const struct got_error *err = NULL;
83 struct got_object *obj;
85 err = got_object_open(&obj, repo, id);
86 if (err)
87 return err;
89 switch (obj->type) {
90 case GOT_OBJ_TYPE_COMMIT:
91 case GOT_OBJ_TYPE_TREE:
92 case GOT_OBJ_TYPE_BLOB:
93 case GOT_OBJ_TYPE_TAG:
94 *type = obj->type;
95 break;
96 default:
97 err = got_error(GOT_ERR_OBJ_TYPE);
98 break;
99 }
101 got_object_close(obj);
102 return err;
105 const struct got_error *
106 got_object_get_path(char **path, struct got_object_id *id,
107 struct got_repository *repo)
109 const struct got_error *err = NULL;
110 char *hex = NULL;
111 char *path_objects;
113 *path = NULL;
115 path_objects = got_repo_get_path_objects(repo);
116 if (path_objects == NULL)
117 return got_error_from_errno("got_repo_get_path_objects");
119 err = got_object_id_str(&hex, id);
120 if (err)
121 goto done;
123 if (asprintf(path, "%s/%.2x/%s", path_objects,
124 id->sha1[0], hex + 2) == -1)
125 err = got_error_from_errno("asprintf");
127 done:
128 free(hex);
129 free(path_objects);
130 return err;
133 const struct got_error *
134 got_object_open_loose_fd(int *fd, struct got_object_id *id,
135 struct got_repository *repo)
137 const struct got_error *err = NULL;
138 char *path;
140 err = got_object_get_path(&path, id, repo);
141 if (err)
142 return err;
143 *fd = open(path, O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
144 if (*fd == -1) {
145 err = got_error_from_errno2("open", path);
146 goto done;
148 done:
149 free(path);
150 return err;
153 static const struct got_error *
154 request_packed_object(struct got_object **obj, struct got_pack *pack, int idx,
155 struct got_object_id *id)
157 const struct got_error *err = NULL;
158 struct imsgbuf *ibuf = pack->privsep_child->ibuf;
160 err = got_privsep_send_packed_obj_req(ibuf, idx, id);
161 if (err)
162 return err;
164 err = got_privsep_recv_obj(obj, ibuf);
165 if (err)
166 return err;
168 memcpy(&(*obj)->id, id, sizeof((*obj)->id));
170 return NULL;
173 /* Create temporary files used during delta application. */
174 static const struct got_error *
175 pack_child_send_tempfiles(struct imsgbuf *ibuf, struct got_pack *pack)
177 const struct got_error *err;
178 int basefd = -1, accumfd = -1;
180 /*
181 * For performance reasons, the child will keep reusing the
182 * same temporary files during every object request.
183 * Opening and closing new files for every object request is
184 * too expensive during operations such as 'gotadmin pack'.
185 */
186 if (pack->child_has_tempfiles)
187 return NULL;
189 basefd = dup(pack->basefd);
190 if (basefd == -1)
191 return got_error_from_errno("dup");
193 accumfd = dup(pack->accumfd);
194 if (accumfd == -1) {
195 err = got_error_from_errno("dup");
196 goto done;
199 err = got_privsep_send_tmpfd(ibuf, basefd);
200 if (err)
201 goto done;
203 err = got_privsep_send_tmpfd(ibuf, accumfd);
204 done:
205 if (err) {
206 if (basefd != -1)
207 close(basefd);
208 if (accumfd != -1)
209 close(accumfd);
210 } else
211 pack->child_has_tempfiles = 1;
212 return NULL;
215 static const struct got_error *
216 request_packed_object_raw(uint8_t **outbuf, off_t *size, size_t *hdrlen,
217 int outfd, struct got_pack *pack, int idx, struct got_object_id *id)
219 const struct got_error *err = NULL;
220 struct imsgbuf *ibuf = pack->privsep_child->ibuf;
221 int outfd_child;
223 err = pack_child_send_tempfiles(ibuf, pack);
224 if (err)
225 return err;
227 outfd_child = dup(outfd);
228 if (outfd_child == -1)
229 return got_error_from_errno("dup");
231 err = got_privsep_send_packed_raw_obj_req(ibuf, idx, id);
232 if (err) {
233 close(outfd_child);
234 return err;
237 err = got_privsep_send_raw_obj_outfd(ibuf, outfd_child);
238 if (err)
239 return err;
241 err = got_privsep_recv_raw_obj(outbuf, size, hdrlen, ibuf);
242 if (err)
243 return err;
245 return NULL;
248 static const struct got_error *
249 read_packed_object_privsep(struct got_object **obj,
250 struct got_repository *repo, struct got_pack *pack,
251 struct got_packidx *packidx, int idx, struct got_object_id *id)
253 const struct got_error *err = NULL;
255 if (pack->privsep_child == NULL) {
256 err = got_pack_start_privsep_child(pack, packidx);
257 if (err)
258 return err;
261 return request_packed_object(obj, pack, idx, id);
264 static const struct got_error *
265 read_packed_object_raw_privsep(uint8_t **outbuf, off_t *size, size_t *hdrlen,
266 int outfd, struct got_pack *pack, struct got_packidx *packidx, int idx,
267 struct got_object_id *id)
269 const struct got_error *err = NULL;
271 if (pack->privsep_child == NULL) {
272 err = got_pack_start_privsep_child(pack, packidx);
273 if (err)
274 return err;
277 return request_packed_object_raw(outbuf, size, hdrlen, outfd, pack,
278 idx, id);
281 const struct got_error *
282 got_object_open_packed(struct got_object **obj, struct got_object_id *id,
283 struct got_repository *repo)
285 const struct got_error *err = NULL;
286 struct got_pack *pack = NULL;
287 struct got_packidx *packidx = NULL;
288 int idx;
289 char *path_packfile;
291 err = got_repo_search_packidx(&packidx, &idx, repo, id);
292 if (err)
293 return err;
295 err = got_packidx_get_packfile_path(&path_packfile,
296 packidx->path_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 const struct got_error *
316 got_object_open_from_packfile(struct got_object **obj, struct got_object_id *id,
317 struct got_pack *pack, struct got_packidx *packidx, int obj_idx,
318 struct got_repository *repo)
320 return read_packed_object_privsep(obj, repo, pack, packidx,
321 obj_idx, id);
324 const struct got_error *
325 got_object_read_raw_delta(uint64_t *base_size, uint64_t *result_size,
326 off_t *delta_size, off_t *delta_compressed_size, off_t *delta_offset,
327 off_t *delta_out_offset, struct got_object_id **base_id, int delta_cache_fd,
328 struct got_packidx *packidx, int obj_idx, struct got_object_id *id,
329 struct got_repository *repo)
331 const struct got_error *err = NULL;
332 struct got_pack *pack = NULL;
333 char *path_packfile;
335 *base_size = 0;
336 *result_size = 0;
337 *delta_size = 0;
338 *delta_compressed_size = 0;
339 *delta_offset = 0;
340 *delta_out_offset = 0;
342 err = got_packidx_get_packfile_path(&path_packfile,
343 packidx->path_packidx);
344 if (err)
345 return err;
347 pack = got_repo_get_cached_pack(repo, path_packfile);
348 if (pack == NULL) {
349 err = got_repo_cache_pack(&pack, repo, path_packfile, packidx);
350 if (err)
351 return err;
354 if (pack->privsep_child == NULL) {
355 err = got_pack_start_privsep_child(pack, packidx);
356 if (err)
357 return err;
360 if (!pack->child_has_delta_outfd) {
361 int outfd_child;
362 outfd_child = dup(delta_cache_fd);
363 if (outfd_child == -1)
364 return got_error_from_errno("dup");
365 err = got_privsep_send_raw_delta_outfd(
366 pack->privsep_child->ibuf, outfd_child);
367 if (err)
368 return err;
369 pack->child_has_delta_outfd = 1;
372 err = got_privsep_send_raw_delta_req(pack->privsep_child->ibuf,
373 obj_idx, id);
374 if (err)
375 return err;
377 return got_privsep_recv_raw_delta(base_size, result_size, delta_size,
378 delta_compressed_size, delta_offset, delta_out_offset, base_id,
379 pack->privsep_child->ibuf);
382 static const struct got_error *
383 request_object(struct got_object **obj, struct got_object_id *id,
384 struct got_repository *repo, int fd)
386 const struct got_error *err = NULL;
387 struct imsgbuf *ibuf;
389 ibuf = repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_OBJECT].ibuf;
391 err = got_privsep_send_obj_req(ibuf, fd, id);
392 if (err)
393 return err;
395 return got_privsep_recv_obj(obj, ibuf);
398 static const struct got_error *
399 request_raw_object(uint8_t **outbuf, off_t *size, size_t *hdrlen, int outfd,
400 struct got_object_id *id, struct got_repository *repo, int infd)
402 const struct got_error *err = NULL;
403 struct imsgbuf *ibuf;
404 int outfd_child;
406 ibuf = repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_OBJECT].ibuf;
408 outfd_child = dup(outfd);
409 if (outfd_child == -1)
410 return got_error_from_errno("dup");
412 err = got_privsep_send_raw_obj_req(ibuf, infd, id);
413 if (err)
414 return err;
416 err = got_privsep_send_raw_obj_outfd(ibuf, outfd_child);
417 if (err)
418 return err;
420 return got_privsep_recv_raw_obj(outbuf, size, hdrlen, ibuf);
423 static const struct got_error *
424 start_read_object_child(struct got_repository *repo)
426 const struct got_error *err = NULL;
427 int imsg_fds[2];
428 pid_t pid;
429 struct imsgbuf *ibuf;
431 ibuf = calloc(1, sizeof(*ibuf));
432 if (ibuf == NULL)
433 return got_error_from_errno("calloc");
435 if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_fds) == -1) {
436 err = got_error_from_errno("socketpair");
437 free(ibuf);
438 return err;
441 pid = fork();
442 if (pid == -1) {
443 err = got_error_from_errno("fork");
444 free(ibuf);
445 return err;
447 else if (pid == 0) {
448 got_privsep_exec_child(imsg_fds, GOT_PATH_PROG_READ_OBJECT,
449 repo->path);
450 /* not reached */
453 if (close(imsg_fds[1]) == -1) {
454 err = got_error_from_errno("close");
455 free(ibuf);
456 return err;
459 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_OBJECT].imsg_fd =
460 imsg_fds[0];
461 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_OBJECT].pid = pid;
462 imsg_init(ibuf, imsg_fds[0]);
463 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_OBJECT].ibuf = ibuf;
465 return NULL;
468 const struct got_error *
469 got_object_read_header_privsep(struct got_object **obj,
470 struct got_object_id *id, struct got_repository *repo, int obj_fd)
472 const struct got_error *err;
474 if (repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_OBJECT].imsg_fd != -1)
475 return request_object(obj, id, repo, obj_fd);
477 err = start_read_object_child(repo);
478 if (err) {
479 close(obj_fd);
480 return err;
483 return request_object(obj, id, repo, obj_fd);
486 static const struct got_error *
487 read_object_raw_privsep(uint8_t **outbuf, off_t *size, size_t *hdrlen,
488 int outfd, struct got_object_id *id, struct got_repository *repo,
489 int obj_fd)
491 const struct got_error *err;
493 if (repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_OBJECT].imsg_fd != -1)
494 return request_raw_object(outbuf, size, hdrlen, outfd, id,
495 repo, obj_fd);
497 err = start_read_object_child(repo);
498 if (err)
499 return err;
501 return request_raw_object(outbuf, size, hdrlen, outfd, id, repo,
502 obj_fd);
505 const struct got_error *
506 got_object_open(struct got_object **obj, struct got_repository *repo,
507 struct got_object_id *id)
509 const struct got_error *err = NULL;
510 int fd;
512 *obj = got_repo_get_cached_object(repo, id);
513 if (*obj != NULL) {
514 (*obj)->refcnt++;
515 return NULL;
518 err = got_object_open_packed(obj, id, repo);
519 if (err && err->code != GOT_ERR_NO_OBJ)
520 return err;
521 if (*obj) {
522 (*obj)->refcnt++;
523 return got_repo_cache_object(repo, id, *obj);
526 err = got_object_open_loose_fd(&fd, id, repo);
527 if (err) {
528 if (err->code == GOT_ERR_ERRNO && errno == ENOENT)
529 err = got_error_no_obj(id);
530 return err;
533 err = got_object_read_header_privsep(obj, id, repo, fd);
534 if (err)
535 return err;
537 memcpy((*obj)->id.sha1, id->sha1, SHA1_DIGEST_LENGTH);
539 (*obj)->refcnt++;
540 return got_repo_cache_object(repo, id, *obj);
543 /* *outfd must be initialized to -1 by caller */
544 const struct got_error *
545 got_object_raw_open(struct got_raw_object **obj, int *outfd,
546 struct got_repository *repo, struct got_object_id *id)
548 const struct got_error *err = NULL;
549 struct got_packidx *packidx = NULL;
550 int idx;
551 uint8_t *outbuf = NULL;
552 off_t size = 0;
553 size_t hdrlen = 0;
554 char *path_packfile = NULL;
556 *obj = got_repo_get_cached_raw_object(repo, id);
557 if (*obj != NULL) {
558 (*obj)->refcnt++;
559 return NULL;
562 if (*outfd == -1) {
563 *outfd = got_opentempfd();
564 if (*outfd == -1)
565 return got_error_from_errno("got_opentempfd");
568 err = got_repo_search_packidx(&packidx, &idx, repo, id);
569 if (err == NULL) {
570 struct got_pack *pack = NULL;
572 err = got_packidx_get_packfile_path(&path_packfile,
573 packidx->path_packidx);
574 if (err)
575 goto done;
577 pack = got_repo_get_cached_pack(repo, path_packfile);
578 if (pack == NULL) {
579 err = got_repo_cache_pack(&pack, repo, path_packfile,
580 packidx);
581 if (err)
582 goto done;
584 err = read_packed_object_raw_privsep(&outbuf, &size, &hdrlen,
585 *outfd, pack, packidx, idx, id);
586 if (err)
587 goto done;
588 } else if (err->code == GOT_ERR_NO_OBJ) {
589 int fd;
591 err = got_object_open_loose_fd(&fd, id, repo);
592 if (err)
593 goto done;
594 err = read_object_raw_privsep(&outbuf, &size, &hdrlen, *outfd,
595 id, repo, fd);
596 if (err)
597 goto done;
600 *obj = calloc(1, sizeof(**obj));
601 if (*obj == NULL) {
602 err = got_error_from_errno("calloc");
603 goto done;
605 (*obj)->fd = -1;
607 if (outbuf) {
608 (*obj)->data = outbuf;
609 } else {
610 struct stat sb;
611 if (fstat(*outfd, &sb) == -1) {
612 err = got_error_from_errno("fstat");
613 goto done;
616 if (sb.st_size != hdrlen + size) {
617 err = got_error(GOT_ERR_PRIVSEP_LEN);
618 goto done;
620 #ifndef GOT_PACK_NO_MMAP
621 if (hdrlen + size > 0) {
622 (*obj)->data = mmap(NULL, hdrlen + size, PROT_READ,
623 MAP_PRIVATE, *outfd, 0);
624 if ((*obj)->data == MAP_FAILED) {
625 if (errno != ENOMEM) {
626 err = got_error_from_errno("mmap");
627 goto done;
629 (*obj)->data = NULL;
630 } else {
631 (*obj)->fd = *outfd;
632 *outfd = -1;
635 #endif
636 if (*outfd != -1) {
637 (*obj)->f = fdopen(*outfd, "r");
638 if ((*obj)->f == NULL) {
639 err = got_error_from_errno("fdopen");
640 goto done;
642 *outfd = -1;
645 (*obj)->hdrlen = hdrlen;
646 (*obj)->size = size;
647 err = got_repo_cache_raw_object(repo, id, *obj);
648 done:
649 free(path_packfile);
650 if (err) {
651 if (*obj) {
652 got_object_raw_close(*obj);
653 *obj = NULL;
655 free(outbuf);
656 } else
657 (*obj)->refcnt++;
658 return err;
661 const struct got_error *
662 got_object_open_by_id_str(struct got_object **obj, struct got_repository *repo,
663 const char *id_str)
665 struct got_object_id id;
667 if (!got_parse_sha1_digest(id.sha1, id_str))
668 return got_error_path(id_str, GOT_ERR_BAD_OBJ_ID_STR);
670 return got_object_open(obj, repo, &id);
673 const struct got_error *
674 got_object_resolve_id_str(struct got_object_id **id,
675 struct got_repository *repo, const char *id_str)
677 const struct got_error *err = NULL;
678 struct got_object *obj;
680 err = got_object_open_by_id_str(&obj, repo, id_str);
681 if (err)
682 return err;
684 *id = got_object_id_dup(got_object_get_id(obj));
685 got_object_close(obj);
686 if (*id == NULL)
687 return got_error_from_errno("got_object_id_dup");
689 return NULL;
692 static const struct got_error *
693 request_packed_commit(struct got_commit_object **commit, struct got_pack *pack,
694 int pack_idx, struct got_object_id *id)
696 const struct got_error *err = NULL;
698 err = got_privsep_send_commit_req(pack->privsep_child->ibuf, -1, id,
699 pack_idx);
700 if (err)
701 return err;
703 err = got_privsep_recv_commit(commit, pack->privsep_child->ibuf);
704 if (err)
705 return err;
707 (*commit)->flags |= GOT_COMMIT_FLAG_PACKED;
708 return NULL;
711 static const struct got_error *
712 read_packed_commit_privsep(struct got_commit_object **commit,
713 struct got_pack *pack, struct got_packidx *packidx, int idx,
714 struct got_object_id *id)
716 const struct got_error *err = NULL;
718 if (pack->privsep_child)
719 return request_packed_commit(commit, pack, idx, id);
721 err = got_pack_start_privsep_child(pack, packidx);
722 if (err)
723 return err;
725 return request_packed_commit(commit, pack, idx, id);
728 static const struct got_error *
729 request_commit(struct got_commit_object **commit, struct got_repository *repo,
730 int fd, struct got_object_id *id)
732 const struct got_error *err = NULL;
733 struct imsgbuf *ibuf;
735 ibuf = repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_COMMIT].ibuf;
737 err = got_privsep_send_commit_req(ibuf, fd, id, -1);
738 if (err)
739 return err;
741 return got_privsep_recv_commit(commit, ibuf);
744 static const struct got_error *
745 read_commit_privsep(struct got_commit_object **commit, int obj_fd,
746 struct got_object_id *id, struct got_repository *repo)
748 const struct got_error *err;
749 int imsg_fds[2];
750 pid_t pid;
751 struct imsgbuf *ibuf;
753 if (repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_COMMIT].imsg_fd != -1)
754 return request_commit(commit, repo, obj_fd, id);
756 ibuf = calloc(1, sizeof(*ibuf));
757 if (ibuf == NULL)
758 return got_error_from_errno("calloc");
760 if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_fds) == -1) {
761 err = got_error_from_errno("socketpair");
762 free(ibuf);
763 return err;
766 pid = fork();
767 if (pid == -1) {
768 err = got_error_from_errno("fork");
769 free(ibuf);
770 return err;
772 else if (pid == 0) {
773 got_privsep_exec_child(imsg_fds, GOT_PATH_PROG_READ_COMMIT,
774 repo->path);
775 /* not reached */
778 if (close(imsg_fds[1]) == -1) {
779 err = got_error_from_errno("close");
780 free(ibuf);
781 return err;
783 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_COMMIT].imsg_fd =
784 imsg_fds[0];
785 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_COMMIT].pid = pid;
786 imsg_init(ibuf, imsg_fds[0]);
787 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_COMMIT].ibuf = ibuf;
789 return request_commit(commit, repo, obj_fd, id);
793 static const struct got_error *
794 open_commit(struct got_commit_object **commit,
795 struct got_repository *repo, struct got_object_id *id, int check_cache)
797 const struct got_error *err = NULL;
798 struct got_packidx *packidx = NULL;
799 int idx;
800 char *path_packfile = NULL;
802 if (check_cache) {
803 *commit = got_repo_get_cached_commit(repo, id);
804 if (*commit != NULL) {
805 (*commit)->refcnt++;
806 return NULL;
808 } else
809 *commit = NULL;
811 err = got_repo_search_packidx(&packidx, &idx, repo, id);
812 if (err == NULL) {
813 struct got_pack *pack = NULL;
815 err = got_packidx_get_packfile_path(&path_packfile,
816 packidx->path_packidx);
817 if (err)
818 return err;
820 pack = got_repo_get_cached_pack(repo, path_packfile);
821 if (pack == NULL) {
822 err = got_repo_cache_pack(&pack, repo, path_packfile,
823 packidx);
824 if (err)
825 goto done;
827 err = read_packed_commit_privsep(commit, pack,
828 packidx, idx, id);
829 } else if (err->code == GOT_ERR_NO_OBJ) {
830 int fd;
832 err = got_object_open_loose_fd(&fd, id, repo);
833 if (err)
834 return err;
835 err = read_commit_privsep(commit, fd, id, repo);
838 if (err == NULL) {
839 (*commit)->refcnt++;
840 err = got_repo_cache_commit(repo, id, *commit);
842 done:
843 free(path_packfile);
844 return err;
847 const struct got_error *
848 got_object_open_as_commit(struct got_commit_object **commit,
849 struct got_repository *repo, struct got_object_id *id)
851 *commit = got_repo_get_cached_commit(repo, id);
852 if (*commit != NULL) {
853 (*commit)->refcnt++;
854 return NULL;
857 return open_commit(commit, repo, id, 0);
860 const struct got_error *
861 got_object_commit_open(struct got_commit_object **commit,
862 struct got_repository *repo, struct got_object *obj)
864 return open_commit(commit, repo, got_object_get_id(obj), 1);
867 const struct got_error *
868 got_object_qid_alloc(struct got_object_qid **qid, struct got_object_id *id)
870 *qid = calloc(1, sizeof(**qid));
871 if (*qid == NULL)
872 return got_error_from_errno("calloc");
874 memcpy(&(*qid)->id, id, sizeof((*qid)->id));
875 return NULL;
878 const struct got_error *
879 got_object_id_queue_copy(const struct got_object_id_queue *src,
880 struct got_object_id_queue *dest)
882 const struct got_error *err;
883 struct got_object_qid *qid;
885 STAILQ_FOREACH(qid, src, entry) {
886 struct got_object_qid *new;
887 /*
888 * Deep-copy the object ID only. Let the caller deal
889 * with setting up the new->data pointer if needed.
890 */
891 err = got_object_qid_alloc(&new, &qid->id);
892 if (err) {
893 got_object_id_queue_free(dest);
894 return err;
896 STAILQ_INSERT_TAIL(dest, new, entry);
899 return NULL;
902 static const struct got_error *
903 request_packed_tree(struct got_tree_object **tree, struct got_pack *pack,
904 int pack_idx, struct got_object_id *id)
906 const struct got_error *err = NULL;
908 err = got_privsep_send_tree_req(pack->privsep_child->ibuf, -1, id,
909 pack_idx);
910 if (err)
911 return err;
913 return got_privsep_recv_tree(tree, pack->privsep_child->ibuf);
916 static const struct got_error *
917 read_packed_tree_privsep(struct got_tree_object **tree,
918 struct got_pack *pack, struct got_packidx *packidx, int idx,
919 struct got_object_id *id)
921 const struct got_error *err = NULL;
923 if (pack->privsep_child)
924 return request_packed_tree(tree, pack, idx, id);
926 err = got_pack_start_privsep_child(pack, packidx);
927 if (err)
928 return err;
930 return request_packed_tree(tree, pack, idx, id);
933 static const struct got_error *
934 request_tree(struct got_tree_object **tree, struct got_repository *repo,
935 int fd, struct got_object_id *id)
937 const struct got_error *err = NULL;
938 struct imsgbuf *ibuf;
940 ibuf = repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TREE].ibuf;
942 err = got_privsep_send_tree_req(ibuf, fd, id, -1);
943 if (err)
944 return err;
946 return got_privsep_recv_tree(tree, ibuf);
949 static const struct got_error *
950 read_tree_privsep(struct got_tree_object **tree, int obj_fd,
951 struct got_object_id *id, struct got_repository *repo)
953 const struct got_error *err;
954 int imsg_fds[2];
955 pid_t pid;
956 struct imsgbuf *ibuf;
958 if (repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TREE].imsg_fd != -1)
959 return request_tree(tree, repo, obj_fd, id);
961 ibuf = calloc(1, sizeof(*ibuf));
962 if (ibuf == NULL)
963 return got_error_from_errno("calloc");
965 if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_fds) == -1) {
966 err = got_error_from_errno("socketpair");
967 free(ibuf);
968 return err;
971 pid = fork();
972 if (pid == -1) {
973 err = got_error_from_errno("fork");
974 free(ibuf);
975 return err;
977 else if (pid == 0) {
978 got_privsep_exec_child(imsg_fds, GOT_PATH_PROG_READ_TREE,
979 repo->path);
980 /* not reached */
983 if (close(imsg_fds[1]) == -1) {
984 err = got_error_from_errno("close");
985 free(ibuf);
986 return err;
988 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TREE].imsg_fd =
989 imsg_fds[0];
990 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TREE].pid = pid;
991 imsg_init(ibuf, imsg_fds[0]);
992 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TREE].ibuf = ibuf;
995 return request_tree(tree, repo, obj_fd, id);
998 static const struct got_error *
999 open_tree(struct got_tree_object **tree, struct got_repository *repo,
1000 struct got_object_id *id, int check_cache)
1002 const struct got_error *err = NULL;
1003 struct got_packidx *packidx = NULL;
1004 int idx;
1005 char *path_packfile = NULL;
1007 if (check_cache) {
1008 *tree = got_repo_get_cached_tree(repo, id);
1009 if (*tree != NULL) {
1010 (*tree)->refcnt++;
1011 return NULL;
1013 } else
1014 *tree = NULL;
1016 err = got_repo_search_packidx(&packidx, &idx, repo, id);
1017 if (err == NULL) {
1018 struct got_pack *pack = NULL;
1020 err = got_packidx_get_packfile_path(&path_packfile,
1021 packidx->path_packidx);
1022 if (err)
1023 return err;
1025 pack = got_repo_get_cached_pack(repo, path_packfile);
1026 if (pack == NULL) {
1027 err = got_repo_cache_pack(&pack, repo, path_packfile,
1028 packidx);
1029 if (err)
1030 goto done;
1032 err = read_packed_tree_privsep(tree, pack,
1033 packidx, idx, id);
1034 } else if (err->code == GOT_ERR_NO_OBJ) {
1035 int fd;
1037 err = got_object_open_loose_fd(&fd, id, repo);
1038 if (err)
1039 return err;
1040 err = read_tree_privsep(tree, fd, id, repo);
1043 if (err == NULL) {
1044 (*tree)->refcnt++;
1045 err = got_repo_cache_tree(repo, id, *tree);
1047 done:
1048 free(path_packfile);
1049 return err;
1052 const struct got_error *
1053 got_object_open_as_tree(struct got_tree_object **tree,
1054 struct got_repository *repo, struct got_object_id *id)
1056 *tree = got_repo_get_cached_tree(repo, id);
1057 if (*tree != NULL) {
1058 (*tree)->refcnt++;
1059 return NULL;
1062 return open_tree(tree, repo, id, 0);
1065 const struct got_error *
1066 got_object_tree_open(struct got_tree_object **tree,
1067 struct got_repository *repo, struct got_object *obj)
1069 return open_tree(tree, repo, got_object_get_id(obj), 1);
1072 int
1073 got_object_tree_get_nentries(struct got_tree_object *tree)
1075 return tree->nentries;
1078 struct got_tree_entry *
1079 got_object_tree_get_first_entry(struct got_tree_object *tree)
1081 return got_object_tree_get_entry(tree, 0);
1084 struct got_tree_entry *
1085 got_object_tree_get_last_entry(struct got_tree_object *tree)
1087 return got_object_tree_get_entry(tree, tree->nentries - 1);
1090 struct got_tree_entry *
1091 got_object_tree_get_entry(struct got_tree_object *tree, int i)
1093 if (i < 0 || i >= tree->nentries)
1094 return NULL;
1095 return &tree->entries[i];
1098 mode_t
1099 got_tree_entry_get_mode(struct got_tree_entry *te)
1101 return te->mode;
1104 const char *
1105 got_tree_entry_get_name(struct got_tree_entry *te)
1107 return &te->name[0];
1110 struct got_object_id *
1111 got_tree_entry_get_id(struct got_tree_entry *te)
1113 return &te->id;
1116 const struct got_error *
1117 got_object_blob_read_to_str(char **s, struct got_blob_object *blob)
1119 const struct got_error *err = NULL;
1120 size_t len, totlen, hdrlen, offset;
1122 *s = NULL;
1124 hdrlen = got_object_blob_get_hdrlen(blob);
1125 totlen = 0;
1126 offset = 0;
1127 do {
1128 char *p;
1130 err = got_object_blob_read_block(&len, blob);
1131 if (err)
1132 return err;
1134 if (len == 0)
1135 break;
1137 totlen += len - hdrlen;
1138 p = realloc(*s, totlen + 1);
1139 if (p == NULL) {
1140 err = got_error_from_errno("realloc");
1141 free(*s);
1142 *s = NULL;
1143 return err;
1145 *s = p;
1146 /* Skip blob object header first time around. */
1147 memcpy(*s + offset,
1148 got_object_blob_get_read_buf(blob) + hdrlen, len - hdrlen);
1149 hdrlen = 0;
1150 offset = totlen;
1151 } while (len > 0);
1153 (*s)[totlen] = '\0';
1154 return NULL;
1157 const struct got_error *
1158 got_tree_entry_get_symlink_target(char **link_target, struct got_tree_entry *te,
1159 struct got_repository *repo)
1161 const struct got_error *err = NULL;
1162 struct got_blob_object *blob = NULL;
1163 int fd = -1;
1165 *link_target = NULL;
1167 if (!got_object_tree_entry_is_symlink(te))
1168 return got_error(GOT_ERR_TREE_ENTRY_TYPE);
1170 fd = got_opentempfd();
1171 if (fd == -1) {
1172 err = got_error_from_errno("got_opentempfd");
1173 goto done;
1176 err = got_object_open_as_blob(&blob, repo,
1177 got_tree_entry_get_id(te), PATH_MAX, fd);
1178 if (err)
1179 goto done;
1181 err = got_object_blob_read_to_str(link_target, blob);
1182 done:
1183 if (fd != -1 && close(fd) == -1 && err == NULL)
1184 err = got_error_from_errno("close");
1185 if (blob)
1186 got_object_blob_close(blob);
1187 if (err) {
1188 free(*link_target);
1189 *link_target = NULL;
1191 return err;
1194 int
1195 got_tree_entry_get_index(struct got_tree_entry *te)
1197 return te->idx;
1200 struct got_tree_entry *
1201 got_tree_entry_get_next(struct got_tree_object *tree,
1202 struct got_tree_entry *te)
1204 return got_object_tree_get_entry(tree, te->idx + 1);
1207 struct got_tree_entry *
1208 got_tree_entry_get_prev(struct got_tree_object *tree,
1209 struct got_tree_entry *te)
1211 return got_object_tree_get_entry(tree, te->idx - 1);
1214 static const struct got_error *
1215 request_packed_blob(uint8_t **outbuf, size_t *size, size_t *hdrlen, int outfd,
1216 struct got_pack *pack, struct got_packidx *packidx, int idx,
1217 struct got_object_id *id)
1219 const struct got_error *err = NULL;
1220 struct imsgbuf *ibuf = pack->privsep_child->ibuf;
1221 int outfd_child;
1223 err = pack_child_send_tempfiles(ibuf, pack);
1224 if (err)
1225 return err;
1227 outfd_child = dup(outfd);
1228 if (outfd_child == -1)
1229 return got_error_from_errno("dup");
1231 err = got_privsep_send_blob_req(pack->privsep_child->ibuf, -1, id, idx);
1232 if (err)
1233 return err;
1235 err = got_privsep_send_blob_outfd(pack->privsep_child->ibuf,
1236 outfd_child);
1237 if (err) {
1238 return err;
1241 err = got_privsep_recv_blob(outbuf, size, hdrlen,
1242 pack->privsep_child->ibuf);
1243 if (err)
1244 return err;
1246 if (lseek(outfd, SEEK_SET, 0) == -1)
1247 err = got_error_from_errno("lseek");
1249 return err;
1252 static const struct got_error *
1253 read_packed_blob_privsep(uint8_t **outbuf, size_t *size, size_t *hdrlen,
1254 int outfd, struct got_pack *pack, struct got_packidx *packidx, int idx,
1255 struct got_object_id *id)
1257 const struct got_error *err = NULL;
1259 if (pack->privsep_child == NULL) {
1260 err = got_pack_start_privsep_child(pack, packidx);
1261 if (err)
1262 return err;
1265 return request_packed_blob(outbuf, size, hdrlen, outfd, pack, packidx,
1266 idx, id);
1269 static const struct got_error *
1270 request_blob(uint8_t **outbuf, size_t *size, size_t *hdrlen, int outfd,
1271 int infd, struct got_object_id *id, struct imsgbuf *ibuf)
1273 const struct got_error *err = NULL;
1274 int outfd_child;
1276 outfd_child = dup(outfd);
1277 if (outfd_child == -1)
1278 return got_error_from_errno("dup");
1280 err = got_privsep_send_blob_req(ibuf, infd, id, -1);
1281 if (err)
1282 return err;
1284 err = got_privsep_send_blob_outfd(ibuf, outfd_child);
1285 if (err)
1286 return err;
1288 err = got_privsep_recv_blob(outbuf, size, hdrlen, ibuf);
1289 if (err)
1290 return err;
1292 if (lseek(outfd, SEEK_SET, 0) == -1)
1293 return got_error_from_errno("lseek");
1295 return err;
1298 static const struct got_error *
1299 read_blob_privsep(uint8_t **outbuf, size_t *size, size_t *hdrlen,
1300 int outfd, int infd, struct got_object_id *id, struct got_repository *repo)
1302 const struct got_error *err;
1303 int imsg_fds[2];
1304 pid_t pid;
1305 struct imsgbuf *ibuf;
1307 if (repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_BLOB].imsg_fd != -1) {
1308 ibuf = repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_BLOB].ibuf;
1309 return request_blob(outbuf, size, hdrlen, outfd, infd, id,
1310 ibuf);
1313 ibuf = calloc(1, sizeof(*ibuf));
1314 if (ibuf == NULL)
1315 return got_error_from_errno("calloc");
1317 if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_fds) == -1) {
1318 err = got_error_from_errno("socketpair");
1319 free(ibuf);
1320 return err;
1323 pid = fork();
1324 if (pid == -1) {
1325 err = got_error_from_errno("fork");
1326 free(ibuf);
1327 return err;
1329 else if (pid == 0) {
1330 got_privsep_exec_child(imsg_fds, GOT_PATH_PROG_READ_BLOB,
1331 repo->path);
1332 /* not reached */
1335 if (close(imsg_fds[1]) == -1) {
1336 err = got_error_from_errno("close");
1337 free(ibuf);
1338 return err;
1340 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_BLOB].imsg_fd =
1341 imsg_fds[0];
1342 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_BLOB].pid = pid;
1343 imsg_init(ibuf, imsg_fds[0]);
1344 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_BLOB].ibuf = ibuf;
1346 return request_blob(outbuf, size, hdrlen, outfd, infd, id, ibuf);
1349 static const struct got_error *
1350 open_blob(struct got_blob_object **blob, struct got_repository *repo,
1351 struct got_object_id *id, size_t blocksize, int outfd)
1353 const struct got_error *err = NULL;
1354 struct got_packidx *packidx = NULL;
1355 int idx, dfd = -1;
1356 char *path_packfile = NULL;
1357 uint8_t *outbuf;
1358 size_t size, hdrlen;
1359 struct stat sb;
1361 *blob = calloc(1, sizeof(**blob));
1362 if (*blob == NULL)
1363 return got_error_from_errno("calloc");
1365 (*blob)->read_buf = malloc(blocksize);
1366 if ((*blob)->read_buf == NULL) {
1367 err = got_error_from_errno("malloc");
1368 goto done;
1371 err = got_repo_search_packidx(&packidx, &idx, repo, id);
1372 if (err == NULL) {
1373 struct got_pack *pack = NULL;
1375 err = got_packidx_get_packfile_path(&path_packfile,
1376 packidx->path_packidx);
1377 if (err)
1378 goto done;
1380 pack = got_repo_get_cached_pack(repo, path_packfile);
1381 if (pack == NULL) {
1382 err = got_repo_cache_pack(&pack, repo, path_packfile,
1383 packidx);
1384 if (err)
1385 goto done;
1387 err = read_packed_blob_privsep(&outbuf, &size, &hdrlen, outfd,
1388 pack, packidx, idx, id);
1389 } else if (err->code == GOT_ERR_NO_OBJ) {
1390 int infd;
1392 err = got_object_open_loose_fd(&infd, id, repo);
1393 if (err)
1394 goto done;
1395 err = read_blob_privsep(&outbuf, &size, &hdrlen, outfd, infd,
1396 id, repo);
1398 if (err)
1399 goto done;
1401 if (hdrlen > size) {
1402 err = got_error(GOT_ERR_BAD_OBJ_HDR);
1403 goto done;
1406 if (outbuf) {
1407 (*blob)->f = fmemopen(outbuf, size, "rb");
1408 if ((*blob)->f == NULL) {
1409 err = got_error_from_errno("fmemopen");
1410 free(outbuf);
1411 goto done;
1413 (*blob)->data = outbuf;
1414 } else {
1415 if (fstat(outfd, &sb) == -1) {
1416 err = got_error_from_errno("fstat");
1417 goto done;
1420 if (sb.st_size != size) {
1421 err = got_error(GOT_ERR_PRIVSEP_LEN);
1422 goto done;
1425 dfd = dup(outfd);
1426 if (dfd == -1) {
1427 err = got_error_from_errno("dup");
1428 goto done;
1431 (*blob)->f = fdopen(dfd, "rb");
1432 if ((*blob)->f == NULL) {
1433 err = got_error_from_errno("fdopen");
1434 close(dfd);
1435 dfd = -1;
1436 goto done;
1440 (*blob)->hdrlen = hdrlen;
1441 (*blob)->blocksize = blocksize;
1442 memcpy(&(*blob)->id.sha1, id->sha1, SHA1_DIGEST_LENGTH);
1444 done:
1445 free(path_packfile);
1446 if (err) {
1447 if (*blob) {
1448 got_object_blob_close(*blob);
1449 *blob = NULL;
1452 return err;
1455 const struct got_error *
1456 got_object_open_as_blob(struct got_blob_object **blob,
1457 struct got_repository *repo, struct got_object_id *id, size_t blocksize,
1458 int outfd)
1460 return open_blob(blob, repo, id, blocksize, outfd);
1463 const struct got_error *
1464 got_object_blob_open(struct got_blob_object **blob,
1465 struct got_repository *repo, struct got_object *obj, size_t blocksize,
1466 int outfd)
1468 return open_blob(blob, repo, got_object_get_id(obj), blocksize, outfd);
1471 const struct got_error *
1472 got_object_blob_close(struct got_blob_object *blob)
1474 const struct got_error *err = NULL;
1475 free(blob->read_buf);
1476 if (blob->f && fclose(blob->f) == EOF)
1477 err = got_error_from_errno("fclose");
1478 free(blob->data);
1479 free(blob);
1480 return err;
1483 void
1484 got_object_blob_rewind(struct got_blob_object *blob)
1486 if (blob->f)
1487 rewind(blob->f);
1490 char *
1491 got_object_blob_id_str(struct got_blob_object *blob, char *buf, size_t size)
1493 return got_sha1_digest_to_str(blob->id.sha1, buf, size);
1496 size_t
1497 got_object_blob_get_hdrlen(struct got_blob_object *blob)
1499 return blob->hdrlen;
1502 const uint8_t *
1503 got_object_blob_get_read_buf(struct got_blob_object *blob)
1505 return blob->read_buf;
1508 const struct got_error *
1509 got_object_blob_read_block(size_t *outlenp, struct got_blob_object *blob)
1511 size_t n;
1513 n = fread(blob->read_buf, 1, blob->blocksize, blob->f);
1514 if (n == 0 && ferror(blob->f))
1515 return got_ferror(blob->f, GOT_ERR_IO);
1516 *outlenp = n;
1517 return NULL;
1520 const struct got_error *
1521 got_object_blob_dump_to_file(off_t *filesize, int *nlines,
1522 off_t **line_offsets, FILE *outfile, struct got_blob_object *blob)
1524 const struct got_error *err = NULL;
1525 size_t n, len, hdrlen;
1526 const uint8_t *buf;
1527 int i;
1528 const int alloc_chunksz = 512;
1529 size_t nalloc = 0;
1530 off_t off = 0, total_len = 0;
1532 if (line_offsets)
1533 *line_offsets = NULL;
1534 if (filesize)
1535 *filesize = 0;
1536 if (nlines)
1537 *nlines = 0;
1539 hdrlen = got_object_blob_get_hdrlen(blob);
1540 do {
1541 err = got_object_blob_read_block(&len, blob);
1542 if (err)
1543 return err;
1544 if (len == 0)
1545 break;
1546 buf = got_object_blob_get_read_buf(blob);
1547 i = hdrlen;
1548 if (nlines) {
1549 if (line_offsets && *line_offsets == NULL) {
1550 /* Have some data but perhaps no '\n'. */
1551 *nlines = 1;
1552 nalloc = alloc_chunksz;
1553 *line_offsets = calloc(nalloc,
1554 sizeof(**line_offsets));
1555 if (*line_offsets == NULL)
1556 return got_error_from_errno("calloc");
1558 /* Skip forward over end of first line. */
1559 while (i < len) {
1560 if (buf[i] == '\n')
1561 break;
1562 i++;
1565 /* Scan '\n' offsets in remaining chunk of data. */
1566 while (i < len) {
1567 if (buf[i] != '\n') {
1568 i++;
1569 continue;
1571 (*nlines)++;
1572 if (line_offsets && nalloc < *nlines) {
1573 size_t n = *nlines + alloc_chunksz;
1574 off_t *o = recallocarray(*line_offsets,
1575 nalloc, n, sizeof(**line_offsets));
1576 if (o == NULL) {
1577 free(*line_offsets);
1578 *line_offsets = NULL;
1579 return got_error_from_errno(
1580 "recallocarray");
1582 *line_offsets = o;
1583 nalloc = n;
1585 if (line_offsets) {
1586 off = total_len + i - hdrlen + 1;
1587 (*line_offsets)[*nlines - 1] = off;
1589 i++;
1592 /* Skip blob object header first time around. */
1593 n = fwrite(buf + hdrlen, 1, len - hdrlen, outfile);
1594 if (n != len - hdrlen)
1595 return got_ferror(outfile, GOT_ERR_IO);
1596 total_len += len - hdrlen;
1597 hdrlen = 0;
1598 } while (len != 0);
1600 if (fflush(outfile) != 0)
1601 return got_error_from_errno("fflush");
1602 rewind(outfile);
1604 if (filesize)
1605 *filesize = total_len;
1607 return NULL;
1610 static const struct got_error *
1611 request_packed_tag(struct got_tag_object **tag, struct got_pack *pack,
1612 int pack_idx, struct got_object_id *id)
1614 const struct got_error *err = NULL;
1616 err = got_privsep_send_tag_req(pack->privsep_child->ibuf, -1, id,
1617 pack_idx);
1618 if (err)
1619 return err;
1621 return got_privsep_recv_tag(tag, pack->privsep_child->ibuf);
1624 static const struct got_error *
1625 read_packed_tag_privsep(struct got_tag_object **tag,
1626 struct got_pack *pack, struct got_packidx *packidx, int idx,
1627 struct got_object_id *id)
1629 const struct got_error *err = NULL;
1631 if (pack->privsep_child)
1632 return request_packed_tag(tag, pack, idx, id);
1634 err = got_pack_start_privsep_child(pack, packidx);
1635 if (err)
1636 return err;
1638 return request_packed_tag(tag, pack, idx, id);
1641 static const struct got_error *
1642 request_tag(struct got_tag_object **tag, struct got_repository *repo,
1643 int fd, struct got_object_id *id)
1645 const struct got_error *err = NULL;
1646 struct imsgbuf *ibuf;
1648 ibuf = repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TAG].ibuf;
1650 err = got_privsep_send_tag_req(ibuf, fd, id, -1);
1651 if (err)
1652 return err;
1654 return got_privsep_recv_tag(tag, ibuf);
1657 static const struct got_error *
1658 read_tag_privsep(struct got_tag_object **tag, int obj_fd,
1659 struct got_object_id *id, struct got_repository *repo)
1661 const struct got_error *err;
1662 int imsg_fds[2];
1663 pid_t pid;
1664 struct imsgbuf *ibuf;
1666 if (repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TAG].imsg_fd != -1)
1667 return request_tag(tag, repo, obj_fd, id);
1669 ibuf = calloc(1, sizeof(*ibuf));
1670 if (ibuf == NULL)
1671 return got_error_from_errno("calloc");
1673 if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_fds) == -1) {
1674 err = got_error_from_errno("socketpair");
1675 free(ibuf);
1676 return err;
1679 pid = fork();
1680 if (pid == -1) {
1681 err = got_error_from_errno("fork");
1682 free(ibuf);
1683 return err;
1685 else if (pid == 0) {
1686 got_privsep_exec_child(imsg_fds, GOT_PATH_PROG_READ_TAG,
1687 repo->path);
1688 /* not reached */
1691 if (close(imsg_fds[1]) == -1) {
1692 err = got_error_from_errno("close");
1693 free(ibuf);
1694 return err;
1696 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TAG].imsg_fd =
1697 imsg_fds[0];
1698 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TAG].pid = pid;
1699 imsg_init(ibuf, imsg_fds[0]);
1700 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TAG].ibuf = ibuf;
1702 return request_tag(tag, repo, obj_fd, id);
1705 static const struct got_error *
1706 open_tag(struct got_tag_object **tag, struct got_repository *repo,
1707 struct got_object_id *id, int check_cache)
1709 const struct got_error *err = NULL;
1710 struct got_packidx *packidx = NULL;
1711 int idx;
1712 char *path_packfile = NULL;
1713 struct got_object *obj = NULL;
1714 int obj_type = GOT_OBJ_TYPE_ANY;
1716 if (check_cache) {
1717 *tag = got_repo_get_cached_tag(repo, id);
1718 if (*tag != NULL) {
1719 (*tag)->refcnt++;
1720 return NULL;
1722 } else
1723 *tag = NULL;
1725 err = got_repo_search_packidx(&packidx, &idx, repo, id);
1726 if (err == NULL) {
1727 struct got_pack *pack = NULL;
1729 err = got_packidx_get_packfile_path(&path_packfile,
1730 packidx->path_packidx);
1731 if (err)
1732 return err;
1734 pack = got_repo_get_cached_pack(repo, path_packfile);
1735 if (pack == NULL) {
1736 err = got_repo_cache_pack(&pack, repo, path_packfile,
1737 packidx);
1738 if (err)
1739 goto done;
1742 /* Beware of "lightweight" tags: Check object type first. */
1743 err = read_packed_object_privsep(&obj, repo, pack, packidx,
1744 idx, id);
1745 if (err)
1746 goto done;
1747 obj_type = obj->type;
1748 got_object_close(obj);
1749 if (obj_type != GOT_OBJ_TYPE_TAG) {
1750 err = got_error(GOT_ERR_OBJ_TYPE);
1751 goto done;
1753 err = read_packed_tag_privsep(tag, pack, packidx, idx, id);
1754 } else if (err->code == GOT_ERR_NO_OBJ) {
1755 int fd;
1757 err = got_object_open_loose_fd(&fd, id, repo);
1758 if (err)
1759 return err;
1760 err = got_object_read_header_privsep(&obj, id, repo, fd);
1761 if (err)
1762 return err;
1763 obj_type = obj->type;
1764 got_object_close(obj);
1765 if (obj_type != GOT_OBJ_TYPE_TAG)
1766 return got_error(GOT_ERR_OBJ_TYPE);
1768 err = got_object_open_loose_fd(&fd, id, repo);
1769 if (err)
1770 return err;
1771 err = read_tag_privsep(tag, fd, id, repo);
1774 if (err == NULL) {
1775 (*tag)->refcnt++;
1776 err = got_repo_cache_tag(repo, id, *tag);
1778 done:
1779 free(path_packfile);
1780 return err;
1783 const struct got_error *
1784 got_object_open_as_tag(struct got_tag_object **tag,
1785 struct got_repository *repo, struct got_object_id *id)
1787 *tag = got_repo_get_cached_tag(repo, id);
1788 if (*tag != NULL) {
1789 (*tag)->refcnt++;
1790 return NULL;
1793 return open_tag(tag, repo, id, 0);
1796 const struct got_error *
1797 got_object_tag_open(struct got_tag_object **tag,
1798 struct got_repository *repo, struct got_object *obj)
1800 return open_tag(tag, repo, got_object_get_id(obj), 1);
1803 const char *
1804 got_object_tag_get_name(struct got_tag_object *tag)
1806 return tag->tag;
1809 int
1810 got_object_tag_get_object_type(struct got_tag_object *tag)
1812 return tag->obj_type;
1815 struct got_object_id *
1816 got_object_tag_get_object_id(struct got_tag_object *tag)
1818 return &tag->id;
1821 time_t
1822 got_object_tag_get_tagger_time(struct got_tag_object *tag)
1824 return tag->tagger_time;
1827 time_t
1828 got_object_tag_get_tagger_gmtoff(struct got_tag_object *tag)
1830 return tag->tagger_gmtoff;
1833 const char *
1834 got_object_tag_get_tagger(struct got_tag_object *tag)
1836 return tag->tagger;
1839 const char *
1840 got_object_tag_get_message(struct got_tag_object *tag)
1842 return tag->tagmsg;
1845 static struct got_tree_entry *
1846 find_entry_by_name(struct got_tree_object *tree, const char *name, size_t len)
1848 int i;
1850 /* Note that tree entries are sorted in strncmp() order. */
1851 for (i = 0; i < tree->nentries; i++) {
1852 struct got_tree_entry *te = &tree->entries[i];
1853 int cmp = strncmp(te->name, name, len);
1854 if (cmp < 0)
1855 continue;
1856 if (cmp > 0)
1857 break;
1858 if (te->name[len] == '\0')
1859 return te;
1861 return NULL;
1864 struct got_tree_entry *
1865 got_object_tree_find_entry(struct got_tree_object *tree, const char *name)
1867 return find_entry_by_name(tree, name, strlen(name));
1870 const struct got_error *
1871 got_object_tree_find_path(struct got_object_id **id, mode_t *mode,
1872 struct got_repository *repo, struct got_tree_object *tree,
1873 const char *path)
1875 const struct got_error *err = NULL;
1876 struct got_tree_object *subtree = NULL;
1877 struct got_tree_entry *te = NULL;
1878 const char *seg, *s;
1879 size_t seglen;
1881 *id = NULL;
1883 s = path;
1884 while (s[0] == '/')
1885 s++;
1886 seg = s;
1887 seglen = 0;
1888 subtree = tree;
1889 while (*s) {
1890 struct got_tree_object *next_tree;
1892 if (*s != '/') {
1893 s++;
1894 seglen++;
1895 if (*s)
1896 continue;
1899 te = find_entry_by_name(subtree, seg, seglen);
1900 if (te == NULL) {
1901 err = got_error_path(path, GOT_ERR_NO_TREE_ENTRY);
1902 goto done;
1905 if (*s == '\0')
1906 break;
1908 seg = s + 1;
1909 seglen = 0;
1910 s++;
1911 if (*s) {
1912 err = got_object_open_as_tree(&next_tree, repo,
1913 &te->id);
1914 te = NULL;
1915 if (err)
1916 goto done;
1917 if (subtree != tree)
1918 got_object_tree_close(subtree);
1919 subtree = next_tree;
1923 if (te) {
1924 *id = got_object_id_dup(&te->id);
1925 if (*id == NULL)
1926 return got_error_from_errno("got_object_id_dup");
1927 if (mode)
1928 *mode = te->mode;
1929 } else
1930 err = got_error_path(path, GOT_ERR_NO_TREE_ENTRY);
1931 done:
1932 if (subtree && subtree != tree)
1933 got_object_tree_close(subtree);
1934 return err;
1936 const struct got_error *
1937 got_object_id_by_path(struct got_object_id **id, struct got_repository *repo,
1938 struct got_commit_object *commit, const char *path)
1940 const struct got_error *err = NULL;
1941 struct got_tree_object *tree = NULL;
1943 *id = NULL;
1945 /* Handle opening of root of commit's tree. */
1946 if (got_path_is_root_dir(path)) {
1947 *id = got_object_id_dup(commit->tree_id);
1948 if (*id == NULL)
1949 err = got_error_from_errno("got_object_id_dup");
1950 } else {
1951 err = got_object_open_as_tree(&tree, repo, commit->tree_id);
1952 if (err)
1953 goto done;
1954 err = got_object_tree_find_path(id, NULL, repo, tree, path);
1956 done:
1957 if (tree)
1958 got_object_tree_close(tree);
1959 return err;
1963 * Normalize file mode bits to avoid false positive tree entry differences
1964 * in case tree entries have unexpected mode bits set.
1966 static mode_t
1967 normalize_mode_for_comparison(mode_t mode)
1970 * For directories, the only relevant bit is the IFDIR bit.
1971 * This allows us to detect paths changing from a directory
1972 * to a file and vice versa.
1974 if (S_ISDIR(mode))
1975 return mode & S_IFDIR;
1978 * For symlinks, the only relevant bit is the IFLNK bit.
1979 * This allows us to detect paths changing from a symlinks
1980 * to a file or directory and vice versa.
1982 if (S_ISLNK(mode))
1983 return mode & S_IFLNK;
1985 /* For files, the only change we care about is the executable bit. */
1986 return mode & S_IXUSR;
1989 const struct got_error *
1990 got_object_tree_path_changed(int *changed,
1991 struct got_tree_object *tree01, struct got_tree_object *tree02,
1992 const char *path, struct got_repository *repo)
1994 const struct got_error *err = NULL;
1995 struct got_tree_object *tree1 = NULL, *tree2 = NULL;
1996 struct got_tree_entry *te1 = NULL, *te2 = NULL;
1997 const char *seg, *s;
1998 size_t seglen;
2000 *changed = 0;
2002 /* We not do support comparing the root path. */
2003 if (got_path_is_root_dir(path))
2004 return got_error_path(path, GOT_ERR_BAD_PATH);
2006 tree1 = tree01;
2007 tree2 = tree02;
2008 s = path;
2009 while (*s == '/')
2010 s++;
2011 seg = s;
2012 seglen = 0;
2013 while (*s) {
2014 struct got_tree_object *next_tree1, *next_tree2;
2015 mode_t mode1, mode2;
2017 if (*s != '/') {
2018 s++;
2019 seglen++;
2020 if (*s)
2021 continue;
2024 te1 = find_entry_by_name(tree1, seg, seglen);
2025 if (te1 == NULL) {
2026 err = got_error(GOT_ERR_NO_OBJ);
2027 goto done;
2030 if (tree2)
2031 te2 = find_entry_by_name(tree2, seg, seglen);
2033 if (te2) {
2034 mode1 = normalize_mode_for_comparison(te1->mode);
2035 mode2 = normalize_mode_for_comparison(te2->mode);
2036 if (mode1 != mode2) {
2037 *changed = 1;
2038 goto done;
2041 if (got_object_id_cmp(&te1->id, &te2->id) == 0) {
2042 *changed = 0;
2043 goto done;
2047 if (*s == '\0') { /* final path element */
2048 *changed = 1;
2049 goto done;
2052 seg = s + 1;
2053 s++;
2054 seglen = 0;
2055 if (*s) {
2056 err = got_object_open_as_tree(&next_tree1, repo,
2057 &te1->id);
2058 te1 = NULL;
2059 if (err)
2060 goto done;
2061 if (tree1 != tree01)
2062 got_object_tree_close(tree1);
2063 tree1 = next_tree1;
2065 if (te2) {
2066 err = got_object_open_as_tree(&next_tree2, repo,
2067 &te2->id);
2068 te2 = NULL;
2069 if (err)
2070 goto done;
2071 if (tree2 != tree02)
2072 got_object_tree_close(tree2);
2073 tree2 = next_tree2;
2074 } else if (tree2) {
2075 if (tree2 != tree02)
2076 got_object_tree_close(tree2);
2077 tree2 = NULL;
2081 done:
2082 if (tree1 && tree1 != tree01)
2083 got_object_tree_close(tree1);
2084 if (tree2 && tree2 != tree02)
2085 got_object_tree_close(tree2);
2086 return err;
2089 const struct got_error *
2090 got_object_tree_entry_dup(struct got_tree_entry **new_te,
2091 struct got_tree_entry *te)
2093 const struct got_error *err = NULL;
2095 *new_te = calloc(1, sizeof(**new_te));
2096 if (*new_te == NULL)
2097 return got_error_from_errno("calloc");
2099 (*new_te)->mode = te->mode;
2100 memcpy((*new_te)->name, te->name, sizeof((*new_te)->name));
2101 memcpy(&(*new_te)->id, &te->id, sizeof((*new_te)->id));
2102 return err;
2105 int
2106 got_object_tree_entry_is_submodule(struct got_tree_entry *te)
2108 return (te->mode & S_IFMT) == (S_IFDIR | S_IFLNK);
2111 int
2112 got_object_tree_entry_is_symlink(struct got_tree_entry *te)
2114 /* S_IFDIR check avoids confusing symlinks with submodules. */
2115 return ((te->mode & (S_IFDIR | S_IFLNK)) == S_IFLNK);
2118 static const struct got_error *
2119 resolve_symlink(char **link_target, const char *path,
2120 struct got_commit_object *commit, struct got_repository *repo)
2122 const struct got_error *err = NULL;
2123 char buf[PATH_MAX];
2124 char *name, *parent_path = NULL;
2125 struct got_object_id *tree_obj_id = NULL;
2126 struct got_tree_object *tree = NULL;
2127 struct got_tree_entry *te = NULL;
2129 *link_target = NULL;
2131 if (strlcpy(buf, path, sizeof(buf)) >= sizeof(buf))
2132 return got_error(GOT_ERR_NO_SPACE);
2134 name = basename(buf);
2135 if (name == NULL)
2136 return got_error_from_errno2("basename", path);
2138 err = got_path_dirname(&parent_path, path);
2139 if (err)
2140 return err;
2142 err = got_object_id_by_path(&tree_obj_id, repo, commit,
2143 parent_path);
2144 if (err) {
2145 if (err->code == GOT_ERR_NO_TREE_ENTRY) {
2146 /* Display the complete path in error message. */
2147 err = got_error_path(path, err->code);
2149 goto done;
2152 err = got_object_open_as_tree(&tree, repo, tree_obj_id);
2153 if (err)
2154 goto done;
2156 te = got_object_tree_find_entry(tree, name);
2157 if (te == NULL) {
2158 err = got_error_path(path, GOT_ERR_NO_TREE_ENTRY);
2159 goto done;
2162 if (got_object_tree_entry_is_symlink(te)) {
2163 err = got_tree_entry_get_symlink_target(link_target, te, repo);
2164 if (err)
2165 goto done;
2166 if (!got_path_is_absolute(*link_target)) {
2167 char *abspath;
2168 if (asprintf(&abspath, "%s/%s", parent_path,
2169 *link_target) == -1) {
2170 err = got_error_from_errno("asprintf");
2171 goto done;
2173 free(*link_target);
2174 *link_target = malloc(PATH_MAX);
2175 if (*link_target == NULL) {
2176 err = got_error_from_errno("malloc");
2177 goto done;
2179 err = got_canonpath(abspath, *link_target, PATH_MAX);
2180 free(abspath);
2181 if (err)
2182 goto done;
2185 done:
2186 free(tree_obj_id);
2187 if (tree)
2188 got_object_tree_close(tree);
2189 if (err) {
2190 free(*link_target);
2191 *link_target = NULL;
2193 return err;
2196 const struct got_error *
2197 got_object_resolve_symlinks(char **link_target, const char *path,
2198 struct got_commit_object *commit, struct got_repository *repo)
2200 const struct got_error *err = NULL;
2201 char *next_target = NULL;
2202 int max_recursion = 40; /* matches Git */
2204 *link_target = NULL;
2206 do {
2207 err = resolve_symlink(&next_target,
2208 *link_target ? *link_target : path, commit, repo);
2209 if (err)
2210 break;
2211 if (next_target) {
2212 free(*link_target);
2213 if (--max_recursion == 0) {
2214 err = got_error_path(path, GOT_ERR_RECURSION);
2215 *link_target = NULL;
2216 break;
2218 *link_target = next_target;
2220 } while (next_target);
2222 return err;
2225 const struct got_error *
2226 got_traverse_packed_commits(struct got_object_id_queue *traversed_commits,
2227 struct got_object_id *commit_id, const char *path,
2228 struct got_repository *repo)
2230 const struct got_error *err = NULL;
2231 struct got_pack *pack = NULL;
2232 struct got_packidx *packidx = NULL;
2233 char *path_packfile = NULL;
2234 struct got_commit_object *changed_commit = NULL;
2235 struct got_object_id *changed_commit_id = NULL;
2236 int idx;
2238 err = got_repo_search_packidx(&packidx, &idx, repo, commit_id);
2239 if (err) {
2240 if (err->code != GOT_ERR_NO_OBJ)
2241 return err;
2242 return NULL;
2245 err = got_packidx_get_packfile_path(&path_packfile,
2246 packidx->path_packidx);
2247 if (err)
2248 return err;
2250 pack = got_repo_get_cached_pack(repo, path_packfile);
2251 if (pack == NULL) {
2252 err = got_repo_cache_pack(&pack, repo, path_packfile, packidx);
2253 if (err)
2254 goto done;
2257 if (pack->privsep_child == NULL) {
2258 err = got_pack_start_privsep_child(pack, packidx);
2259 if (err)
2260 goto done;
2263 err = got_privsep_send_commit_traversal_request(
2264 pack->privsep_child->ibuf, commit_id, idx, path);
2265 if (err)
2266 goto done;
2268 err = got_privsep_recv_traversed_commits(&changed_commit,
2269 &changed_commit_id, traversed_commits, pack->privsep_child->ibuf);
2270 if (err)
2271 goto done;
2273 if (changed_commit) {
2275 * Cache the commit in which the path was changed.
2276 * This commit might be opened again soon.
2278 changed_commit->refcnt++;
2279 err = got_repo_cache_commit(repo, changed_commit_id,
2280 changed_commit);
2281 got_object_commit_close(changed_commit);
2283 done:
2284 free(path_packfile);
2285 free(changed_commit_id);
2286 return err;
2289 const struct got_error *
2290 got_object_enumerate(int *found_all_objects,
2291 got_object_enumerate_commit_cb cb_commit,
2292 got_object_enumerate_tree_cb cb_tree, void *cb_arg,
2293 struct got_object_id **ours, int nours,
2294 struct got_object_id **theirs, int ntheirs,
2295 struct got_packidx *packidx, struct got_repository *repo)
2297 const struct got_error *err = NULL;
2298 struct got_pack *pack;
2299 char *path_packfile = NULL;
2301 err = got_packidx_get_packfile_path(&path_packfile,
2302 packidx->path_packidx);
2303 if (err)
2304 return err;
2306 pack = got_repo_get_cached_pack(repo, path_packfile);
2307 if (pack == NULL) {
2308 err = got_repo_cache_pack(&pack, repo, path_packfile, packidx);
2309 if (err)
2310 goto done;
2313 if (pack->privsep_child == NULL) {
2314 err = got_pack_start_privsep_child(pack, packidx);
2315 if (err)
2316 goto done;
2319 err = got_privsep_send_object_enumeration_request(
2320 pack->privsep_child->ibuf);
2321 if (err)
2322 goto done;
2324 err = got_privsep_send_object_idlist(pack->privsep_child->ibuf,
2325 ours, nours);
2326 if (err)
2327 goto done;
2328 err = got_privsep_send_object_idlist_done(pack->privsep_child->ibuf);
2329 if (err)
2330 goto done;
2332 err = got_privsep_send_object_idlist(pack->privsep_child->ibuf,
2333 theirs, ntheirs);
2334 if (err)
2335 goto done;
2336 err = got_privsep_send_object_idlist_done(pack->privsep_child->ibuf);
2337 if (err)
2338 goto done;
2340 err = got_privsep_recv_enumerated_objects(found_all_objects,
2341 pack->privsep_child->ibuf, cb_commit, cb_tree, cb_arg, repo);
2342 done:
2343 free(path_packfile);
2344 return err;