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;
1164 *link_target = NULL;
1166 if (!got_object_tree_entry_is_symlink(te))
1167 return got_error(GOT_ERR_TREE_ENTRY_TYPE);
1169 err = got_object_open_as_blob(&blob, repo,
1170 got_tree_entry_get_id(te), PATH_MAX);
1171 if (err)
1172 return err;
1174 err = got_object_blob_read_to_str(link_target, blob);
1175 got_object_blob_close(blob);
1176 if (err) {
1177 free(*link_target);
1178 *link_target = NULL;
1180 return err;
1183 int
1184 got_tree_entry_get_index(struct got_tree_entry *te)
1186 return te->idx;
1189 struct got_tree_entry *
1190 got_tree_entry_get_next(struct got_tree_object *tree,
1191 struct got_tree_entry *te)
1193 return got_object_tree_get_entry(tree, te->idx + 1);
1196 struct got_tree_entry *
1197 got_tree_entry_get_prev(struct got_tree_object *tree,
1198 struct got_tree_entry *te)
1200 return got_object_tree_get_entry(tree, te->idx - 1);
1203 static const struct got_error *
1204 request_packed_blob(uint8_t **outbuf, size_t *size, size_t *hdrlen, int outfd,
1205 struct got_pack *pack, struct got_packidx *packidx, int idx,
1206 struct got_object_id *id)
1208 const struct got_error *err = NULL;
1209 struct imsgbuf *ibuf = pack->privsep_child->ibuf;
1210 int outfd_child;
1212 err = pack_child_send_tempfiles(ibuf, pack);
1213 if (err)
1214 return err;
1216 outfd_child = dup(outfd);
1217 if (outfd_child == -1)
1218 return got_error_from_errno("dup");
1220 err = got_privsep_send_blob_req(pack->privsep_child->ibuf, -1, id, idx);
1221 if (err)
1222 return err;
1224 err = got_privsep_send_blob_outfd(pack->privsep_child->ibuf,
1225 outfd_child);
1226 if (err) {
1227 return err;
1230 err = got_privsep_recv_blob(outbuf, size, hdrlen,
1231 pack->privsep_child->ibuf);
1232 if (err)
1233 return err;
1235 if (lseek(outfd, SEEK_SET, 0) == -1)
1236 err = got_error_from_errno("lseek");
1238 return err;
1241 static const struct got_error *
1242 read_packed_blob_privsep(uint8_t **outbuf, size_t *size, size_t *hdrlen,
1243 int outfd, struct got_pack *pack, struct got_packidx *packidx, int idx,
1244 struct got_object_id *id)
1246 const struct got_error *err = NULL;
1248 if (pack->privsep_child == NULL) {
1249 err = got_pack_start_privsep_child(pack, packidx);
1250 if (err)
1251 return err;
1254 return request_packed_blob(outbuf, size, hdrlen, outfd, pack, packidx,
1255 idx, id);
1258 static const struct got_error *
1259 request_blob(uint8_t **outbuf, size_t *size, size_t *hdrlen, int outfd,
1260 int infd, struct got_object_id *id, struct imsgbuf *ibuf)
1262 const struct got_error *err = NULL;
1263 int outfd_child;
1265 outfd_child = dup(outfd);
1266 if (outfd_child == -1)
1267 return got_error_from_errno("dup");
1269 err = got_privsep_send_blob_req(ibuf, infd, id, -1);
1270 if (err)
1271 return err;
1273 err = got_privsep_send_blob_outfd(ibuf, outfd_child);
1274 if (err)
1275 return err;
1277 err = got_privsep_recv_blob(outbuf, size, hdrlen, ibuf);
1278 if (err)
1279 return err;
1281 if (lseek(outfd, SEEK_SET, 0) == -1)
1282 return got_error_from_errno("lseek");
1284 return err;
1287 static const struct got_error *
1288 read_blob_privsep(uint8_t **outbuf, size_t *size, size_t *hdrlen,
1289 int outfd, int infd, struct got_object_id *id, struct got_repository *repo)
1291 const struct got_error *err;
1292 int imsg_fds[2];
1293 pid_t pid;
1294 struct imsgbuf *ibuf;
1296 if (repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_BLOB].imsg_fd != -1) {
1297 ibuf = repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_BLOB].ibuf;
1298 return request_blob(outbuf, size, hdrlen, outfd, infd, id,
1299 ibuf);
1302 ibuf = calloc(1, sizeof(*ibuf));
1303 if (ibuf == NULL)
1304 return got_error_from_errno("calloc");
1306 if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_fds) == -1) {
1307 err = got_error_from_errno("socketpair");
1308 free(ibuf);
1309 return err;
1312 pid = fork();
1313 if (pid == -1) {
1314 err = got_error_from_errno("fork");
1315 free(ibuf);
1316 return err;
1318 else if (pid == 0) {
1319 got_privsep_exec_child(imsg_fds, GOT_PATH_PROG_READ_BLOB,
1320 repo->path);
1321 /* not reached */
1324 if (close(imsg_fds[1]) == -1) {
1325 err = got_error_from_errno("close");
1326 free(ibuf);
1327 return err;
1329 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_BLOB].imsg_fd =
1330 imsg_fds[0];
1331 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_BLOB].pid = pid;
1332 imsg_init(ibuf, imsg_fds[0]);
1333 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_BLOB].ibuf = ibuf;
1335 return request_blob(outbuf, size, hdrlen, outfd, infd, id, ibuf);
1338 static const struct got_error *
1339 open_blob(struct got_blob_object **blob, struct got_repository *repo,
1340 struct got_object_id *id, size_t blocksize)
1342 const struct got_error *err = NULL;
1343 struct got_packidx *packidx = NULL;
1344 int idx;
1345 char *path_packfile = NULL;
1346 uint8_t *outbuf;
1347 int outfd;
1348 size_t size, hdrlen;
1349 struct stat sb;
1351 *blob = calloc(1, sizeof(**blob));
1352 if (*blob == NULL)
1353 return got_error_from_errno("calloc");
1355 outfd = got_opentempfd();
1356 if (outfd == -1)
1357 return got_error_from_errno("got_opentempfd");
1359 (*blob)->read_buf = malloc(blocksize);
1360 if ((*blob)->read_buf == NULL) {
1361 err = got_error_from_errno("malloc");
1362 goto done;
1365 err = got_repo_search_packidx(&packidx, &idx, repo, id);
1366 if (err == NULL) {
1367 struct got_pack *pack = NULL;
1369 err = got_packidx_get_packfile_path(&path_packfile,
1370 packidx->path_packidx);
1371 if (err)
1372 goto done;
1374 pack = got_repo_get_cached_pack(repo, path_packfile);
1375 if (pack == NULL) {
1376 err = got_repo_cache_pack(&pack, repo, path_packfile,
1377 packidx);
1378 if (err)
1379 goto done;
1381 err = read_packed_blob_privsep(&outbuf, &size, &hdrlen, outfd,
1382 pack, packidx, idx, id);
1383 } else if (err->code == GOT_ERR_NO_OBJ) {
1384 int infd;
1386 err = got_object_open_loose_fd(&infd, id, repo);
1387 if (err)
1388 goto done;
1389 err = read_blob_privsep(&outbuf, &size, &hdrlen, outfd, infd,
1390 id, repo);
1392 if (err)
1393 goto done;
1395 if (hdrlen > size) {
1396 err = got_error(GOT_ERR_BAD_OBJ_HDR);
1397 goto done;
1400 if (outbuf) {
1401 if (close(outfd) == -1 && err == NULL)
1402 err = got_error_from_errno("close");
1403 outfd = -1;
1404 (*blob)->f = fmemopen(outbuf, size, "rb");
1405 if ((*blob)->f == NULL) {
1406 err = got_error_from_errno("fmemopen");
1407 free(outbuf);
1408 goto done;
1410 (*blob)->data = outbuf;
1411 } else {
1412 if (fstat(outfd, &sb) == -1) {
1413 err = got_error_from_errno("fstat");
1414 goto done;
1417 if (sb.st_size != size) {
1418 err = got_error(GOT_ERR_PRIVSEP_LEN);
1419 goto done;
1422 (*blob)->f = fdopen(outfd, "rb");
1423 if ((*blob)->f == NULL) {
1424 err = got_error_from_errno("fdopen");
1425 close(outfd);
1426 outfd = -1;
1427 goto done;
1431 (*blob)->hdrlen = hdrlen;
1432 (*blob)->blocksize = blocksize;
1433 memcpy(&(*blob)->id.sha1, id->sha1, SHA1_DIGEST_LENGTH);
1435 done:
1436 free(path_packfile);
1437 if (err) {
1438 if (*blob) {
1439 got_object_blob_close(*blob);
1440 *blob = NULL;
1441 } else if (outfd != -1)
1442 close(outfd);
1444 return err;
1447 const struct got_error *
1448 got_object_open_as_blob(struct got_blob_object **blob,
1449 struct got_repository *repo, struct got_object_id *id,
1450 size_t blocksize)
1452 return open_blob(blob, repo, id, blocksize);
1455 const struct got_error *
1456 got_object_blob_open(struct got_blob_object **blob,
1457 struct got_repository *repo, struct got_object *obj, size_t blocksize)
1459 return open_blob(blob, repo, got_object_get_id(obj), blocksize);
1462 const struct got_error *
1463 got_object_blob_close(struct got_blob_object *blob)
1465 const struct got_error *err = NULL;
1466 free(blob->read_buf);
1467 if (blob->f && fclose(blob->f) == EOF)
1468 err = got_error_from_errno("fclose");
1469 free(blob->data);
1470 free(blob);
1471 return err;
1474 void
1475 got_object_blob_rewind(struct got_blob_object *blob)
1477 if (blob->f)
1478 rewind(blob->f);
1481 char *
1482 got_object_blob_id_str(struct got_blob_object *blob, char *buf, size_t size)
1484 return got_sha1_digest_to_str(blob->id.sha1, buf, size);
1487 size_t
1488 got_object_blob_get_hdrlen(struct got_blob_object *blob)
1490 return blob->hdrlen;
1493 const uint8_t *
1494 got_object_blob_get_read_buf(struct got_blob_object *blob)
1496 return blob->read_buf;
1499 const struct got_error *
1500 got_object_blob_read_block(size_t *outlenp, struct got_blob_object *blob)
1502 size_t n;
1504 n = fread(blob->read_buf, 1, blob->blocksize, blob->f);
1505 if (n == 0 && ferror(blob->f))
1506 return got_ferror(blob->f, GOT_ERR_IO);
1507 *outlenp = n;
1508 return NULL;
1511 const struct got_error *
1512 got_object_blob_dump_to_file(off_t *filesize, int *nlines,
1513 off_t **line_offsets, FILE *outfile, struct got_blob_object *blob)
1515 const struct got_error *err = NULL;
1516 size_t n, len, hdrlen;
1517 const uint8_t *buf;
1518 int i;
1519 const int alloc_chunksz = 512;
1520 size_t nalloc = 0;
1521 off_t off = 0, total_len = 0;
1523 if (line_offsets)
1524 *line_offsets = NULL;
1525 if (filesize)
1526 *filesize = 0;
1527 if (nlines)
1528 *nlines = 0;
1530 hdrlen = got_object_blob_get_hdrlen(blob);
1531 do {
1532 err = got_object_blob_read_block(&len, blob);
1533 if (err)
1534 return err;
1535 if (len == 0)
1536 break;
1537 buf = got_object_blob_get_read_buf(blob);
1538 i = hdrlen;
1539 if (nlines) {
1540 if (line_offsets && *line_offsets == NULL) {
1541 /* Have some data but perhaps no '\n'. */
1542 *nlines = 1;
1543 nalloc = alloc_chunksz;
1544 *line_offsets = calloc(nalloc,
1545 sizeof(**line_offsets));
1546 if (*line_offsets == NULL)
1547 return got_error_from_errno("calloc");
1549 /* Skip forward over end of first line. */
1550 while (i < len) {
1551 if (buf[i] == '\n')
1552 break;
1553 i++;
1556 /* Scan '\n' offsets in remaining chunk of data. */
1557 while (i < len) {
1558 if (buf[i] != '\n') {
1559 i++;
1560 continue;
1562 (*nlines)++;
1563 if (line_offsets && nalloc < *nlines) {
1564 size_t n = *nlines + alloc_chunksz;
1565 off_t *o = recallocarray(*line_offsets,
1566 nalloc, n, sizeof(**line_offsets));
1567 if (o == NULL) {
1568 free(*line_offsets);
1569 *line_offsets = NULL;
1570 return got_error_from_errno(
1571 "recallocarray");
1573 *line_offsets = o;
1574 nalloc = n;
1576 if (line_offsets) {
1577 off = total_len + i - hdrlen + 1;
1578 (*line_offsets)[*nlines - 1] = off;
1580 i++;
1583 /* Skip blob object header first time around. */
1584 n = fwrite(buf + hdrlen, 1, len - hdrlen, outfile);
1585 if (n != len - hdrlen)
1586 return got_ferror(outfile, GOT_ERR_IO);
1587 total_len += len - hdrlen;
1588 hdrlen = 0;
1589 } while (len != 0);
1591 if (fflush(outfile) != 0)
1592 return got_error_from_errno("fflush");
1593 rewind(outfile);
1595 if (filesize)
1596 *filesize = total_len;
1598 return NULL;
1601 static const struct got_error *
1602 request_packed_tag(struct got_tag_object **tag, struct got_pack *pack,
1603 int pack_idx, struct got_object_id *id)
1605 const struct got_error *err = NULL;
1607 err = got_privsep_send_tag_req(pack->privsep_child->ibuf, -1, id,
1608 pack_idx);
1609 if (err)
1610 return err;
1612 return got_privsep_recv_tag(tag, pack->privsep_child->ibuf);
1615 static const struct got_error *
1616 read_packed_tag_privsep(struct got_tag_object **tag,
1617 struct got_pack *pack, struct got_packidx *packidx, int idx,
1618 struct got_object_id *id)
1620 const struct got_error *err = NULL;
1622 if (pack->privsep_child)
1623 return request_packed_tag(tag, pack, idx, id);
1625 err = got_pack_start_privsep_child(pack, packidx);
1626 if (err)
1627 return err;
1629 return request_packed_tag(tag, pack, idx, id);
1632 static const struct got_error *
1633 request_tag(struct got_tag_object **tag, struct got_repository *repo,
1634 int fd, struct got_object_id *id)
1636 const struct got_error *err = NULL;
1637 struct imsgbuf *ibuf;
1639 ibuf = repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TAG].ibuf;
1641 err = got_privsep_send_tag_req(ibuf, fd, id, -1);
1642 if (err)
1643 return err;
1645 return got_privsep_recv_tag(tag, ibuf);
1648 static const struct got_error *
1649 read_tag_privsep(struct got_tag_object **tag, int obj_fd,
1650 struct got_object_id *id, struct got_repository *repo)
1652 const struct got_error *err;
1653 int imsg_fds[2];
1654 pid_t pid;
1655 struct imsgbuf *ibuf;
1657 if (repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TAG].imsg_fd != -1)
1658 return request_tag(tag, repo, obj_fd, id);
1660 ibuf = calloc(1, sizeof(*ibuf));
1661 if (ibuf == NULL)
1662 return got_error_from_errno("calloc");
1664 if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_fds) == -1) {
1665 err = got_error_from_errno("socketpair");
1666 free(ibuf);
1667 return err;
1670 pid = fork();
1671 if (pid == -1) {
1672 err = got_error_from_errno("fork");
1673 free(ibuf);
1674 return err;
1676 else if (pid == 0) {
1677 got_privsep_exec_child(imsg_fds, GOT_PATH_PROG_READ_TAG,
1678 repo->path);
1679 /* not reached */
1682 if (close(imsg_fds[1]) == -1) {
1683 err = got_error_from_errno("close");
1684 free(ibuf);
1685 return err;
1687 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TAG].imsg_fd =
1688 imsg_fds[0];
1689 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TAG].pid = pid;
1690 imsg_init(ibuf, imsg_fds[0]);
1691 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TAG].ibuf = ibuf;
1693 return request_tag(tag, repo, obj_fd, id);
1696 static const struct got_error *
1697 open_tag(struct got_tag_object **tag, struct got_repository *repo,
1698 struct got_object_id *id, int check_cache)
1700 const struct got_error *err = NULL;
1701 struct got_packidx *packidx = NULL;
1702 int idx;
1703 char *path_packfile = NULL;
1704 struct got_object *obj = NULL;
1705 int obj_type = GOT_OBJ_TYPE_ANY;
1707 if (check_cache) {
1708 *tag = got_repo_get_cached_tag(repo, id);
1709 if (*tag != NULL) {
1710 (*tag)->refcnt++;
1711 return NULL;
1713 } else
1714 *tag = NULL;
1716 err = got_repo_search_packidx(&packidx, &idx, repo, id);
1717 if (err == NULL) {
1718 struct got_pack *pack = NULL;
1720 err = got_packidx_get_packfile_path(&path_packfile,
1721 packidx->path_packidx);
1722 if (err)
1723 return err;
1725 pack = got_repo_get_cached_pack(repo, path_packfile);
1726 if (pack == NULL) {
1727 err = got_repo_cache_pack(&pack, repo, path_packfile,
1728 packidx);
1729 if (err)
1730 goto done;
1733 /* Beware of "lightweight" tags: Check object type first. */
1734 err = read_packed_object_privsep(&obj, repo, pack, packidx,
1735 idx, id);
1736 if (err)
1737 goto done;
1738 obj_type = obj->type;
1739 got_object_close(obj);
1740 if (obj_type != GOT_OBJ_TYPE_TAG) {
1741 err = got_error(GOT_ERR_OBJ_TYPE);
1742 goto done;
1744 err = read_packed_tag_privsep(tag, pack, packidx, idx, id);
1745 } else if (err->code == GOT_ERR_NO_OBJ) {
1746 int fd;
1748 err = got_object_open_loose_fd(&fd, id, repo);
1749 if (err)
1750 return err;
1751 err = got_object_read_header_privsep(&obj, id, repo, fd);
1752 if (err)
1753 return err;
1754 obj_type = obj->type;
1755 got_object_close(obj);
1756 if (obj_type != GOT_OBJ_TYPE_TAG)
1757 return got_error(GOT_ERR_OBJ_TYPE);
1759 err = got_object_open_loose_fd(&fd, id, repo);
1760 if (err)
1761 return err;
1762 err = read_tag_privsep(tag, fd, id, repo);
1765 if (err == NULL) {
1766 (*tag)->refcnt++;
1767 err = got_repo_cache_tag(repo, id, *tag);
1769 done:
1770 free(path_packfile);
1771 return err;
1774 const struct got_error *
1775 got_object_open_as_tag(struct got_tag_object **tag,
1776 struct got_repository *repo, struct got_object_id *id)
1778 *tag = got_repo_get_cached_tag(repo, id);
1779 if (*tag != NULL) {
1780 (*tag)->refcnt++;
1781 return NULL;
1784 return open_tag(tag, repo, id, 0);
1787 const struct got_error *
1788 got_object_tag_open(struct got_tag_object **tag,
1789 struct got_repository *repo, struct got_object *obj)
1791 return open_tag(tag, repo, got_object_get_id(obj), 1);
1794 const char *
1795 got_object_tag_get_name(struct got_tag_object *tag)
1797 return tag->tag;
1800 int
1801 got_object_tag_get_object_type(struct got_tag_object *tag)
1803 return tag->obj_type;
1806 struct got_object_id *
1807 got_object_tag_get_object_id(struct got_tag_object *tag)
1809 return &tag->id;
1812 time_t
1813 got_object_tag_get_tagger_time(struct got_tag_object *tag)
1815 return tag->tagger_time;
1818 time_t
1819 got_object_tag_get_tagger_gmtoff(struct got_tag_object *tag)
1821 return tag->tagger_gmtoff;
1824 const char *
1825 got_object_tag_get_tagger(struct got_tag_object *tag)
1827 return tag->tagger;
1830 const char *
1831 got_object_tag_get_message(struct got_tag_object *tag)
1833 return tag->tagmsg;
1836 static struct got_tree_entry *
1837 find_entry_by_name(struct got_tree_object *tree, const char *name, size_t len)
1839 int i;
1841 /* Note that tree entries are sorted in strncmp() order. */
1842 for (i = 0; i < tree->nentries; i++) {
1843 struct got_tree_entry *te = &tree->entries[i];
1844 int cmp = strncmp(te->name, name, len);
1845 if (cmp < 0)
1846 continue;
1847 if (cmp > 0)
1848 break;
1849 if (te->name[len] == '\0')
1850 return te;
1852 return NULL;
1855 struct got_tree_entry *
1856 got_object_tree_find_entry(struct got_tree_object *tree, const char *name)
1858 return find_entry_by_name(tree, name, strlen(name));
1861 const struct got_error *
1862 got_object_tree_find_path(struct got_object_id **id, mode_t *mode,
1863 struct got_repository *repo, struct got_tree_object *tree,
1864 const char *path)
1866 const struct got_error *err = NULL;
1867 struct got_tree_object *subtree = NULL;
1868 struct got_tree_entry *te = NULL;
1869 const char *seg, *s;
1870 size_t seglen;
1872 *id = NULL;
1874 s = path;
1875 while (s[0] == '/')
1876 s++;
1877 seg = s;
1878 seglen = 0;
1879 subtree = tree;
1880 while (*s) {
1881 struct got_tree_object *next_tree;
1883 if (*s != '/') {
1884 s++;
1885 seglen++;
1886 if (*s)
1887 continue;
1890 te = find_entry_by_name(subtree, seg, seglen);
1891 if (te == NULL) {
1892 err = got_error_path(path, GOT_ERR_NO_TREE_ENTRY);
1893 goto done;
1896 if (*s == '\0')
1897 break;
1899 seg = s + 1;
1900 seglen = 0;
1901 s++;
1902 if (*s) {
1903 err = got_object_open_as_tree(&next_tree, repo,
1904 &te->id);
1905 te = NULL;
1906 if (err)
1907 goto done;
1908 if (subtree != tree)
1909 got_object_tree_close(subtree);
1910 subtree = next_tree;
1914 if (te) {
1915 *id = got_object_id_dup(&te->id);
1916 if (*id == NULL)
1917 return got_error_from_errno("got_object_id_dup");
1918 if (mode)
1919 *mode = te->mode;
1920 } else
1921 err = got_error_path(path, GOT_ERR_NO_TREE_ENTRY);
1922 done:
1923 if (subtree && subtree != tree)
1924 got_object_tree_close(subtree);
1925 return err;
1927 const struct got_error *
1928 got_object_id_by_path(struct got_object_id **id, struct got_repository *repo,
1929 struct got_commit_object *commit, const char *path)
1931 const struct got_error *err = NULL;
1932 struct got_tree_object *tree = NULL;
1934 *id = NULL;
1936 /* Handle opening of root of commit's tree. */
1937 if (got_path_is_root_dir(path)) {
1938 *id = got_object_id_dup(commit->tree_id);
1939 if (*id == NULL)
1940 err = got_error_from_errno("got_object_id_dup");
1941 } else {
1942 err = got_object_open_as_tree(&tree, repo, commit->tree_id);
1943 if (err)
1944 goto done;
1945 err = got_object_tree_find_path(id, NULL, repo, tree, path);
1947 done:
1948 if (tree)
1949 got_object_tree_close(tree);
1950 return err;
1954 * Normalize file mode bits to avoid false positive tree entry differences
1955 * in case tree entries have unexpected mode bits set.
1957 static mode_t
1958 normalize_mode_for_comparison(mode_t mode)
1961 * For directories, the only relevant bit is the IFDIR bit.
1962 * This allows us to detect paths changing from a directory
1963 * to a file and vice versa.
1965 if (S_ISDIR(mode))
1966 return mode & S_IFDIR;
1969 * For symlinks, the only relevant bit is the IFLNK bit.
1970 * This allows us to detect paths changing from a symlinks
1971 * to a file or directory and vice versa.
1973 if (S_ISLNK(mode))
1974 return mode & S_IFLNK;
1976 /* For files, the only change we care about is the executable bit. */
1977 return mode & S_IXUSR;
1980 const struct got_error *
1981 got_object_tree_path_changed(int *changed,
1982 struct got_tree_object *tree01, struct got_tree_object *tree02,
1983 const char *path, struct got_repository *repo)
1985 const struct got_error *err = NULL;
1986 struct got_tree_object *tree1 = NULL, *tree2 = NULL;
1987 struct got_tree_entry *te1 = NULL, *te2 = NULL;
1988 const char *seg, *s;
1989 size_t seglen;
1991 *changed = 0;
1993 /* We not do support comparing the root path. */
1994 if (got_path_is_root_dir(path))
1995 return got_error_path(path, GOT_ERR_BAD_PATH);
1997 tree1 = tree01;
1998 tree2 = tree02;
1999 s = path;
2000 while (*s == '/')
2001 s++;
2002 seg = s;
2003 seglen = 0;
2004 while (*s) {
2005 struct got_tree_object *next_tree1, *next_tree2;
2006 mode_t mode1, mode2;
2008 if (*s != '/') {
2009 s++;
2010 seglen++;
2011 if (*s)
2012 continue;
2015 te1 = find_entry_by_name(tree1, seg, seglen);
2016 if (te1 == NULL) {
2017 err = got_error(GOT_ERR_NO_OBJ);
2018 goto done;
2021 if (tree2)
2022 te2 = find_entry_by_name(tree2, seg, seglen);
2024 if (te2) {
2025 mode1 = normalize_mode_for_comparison(te1->mode);
2026 mode2 = normalize_mode_for_comparison(te2->mode);
2027 if (mode1 != mode2) {
2028 *changed = 1;
2029 goto done;
2032 if (got_object_id_cmp(&te1->id, &te2->id) == 0) {
2033 *changed = 0;
2034 goto done;
2038 if (*s == '\0') { /* final path element */
2039 *changed = 1;
2040 goto done;
2043 seg = s + 1;
2044 s++;
2045 seglen = 0;
2046 if (*s) {
2047 err = got_object_open_as_tree(&next_tree1, repo,
2048 &te1->id);
2049 te1 = NULL;
2050 if (err)
2051 goto done;
2052 if (tree1 != tree01)
2053 got_object_tree_close(tree1);
2054 tree1 = next_tree1;
2056 if (te2) {
2057 err = got_object_open_as_tree(&next_tree2, repo,
2058 &te2->id);
2059 te2 = NULL;
2060 if (err)
2061 goto done;
2062 if (tree2 != tree02)
2063 got_object_tree_close(tree2);
2064 tree2 = next_tree2;
2065 } else if (tree2) {
2066 if (tree2 != tree02)
2067 got_object_tree_close(tree2);
2068 tree2 = NULL;
2072 done:
2073 if (tree1 && tree1 != tree01)
2074 got_object_tree_close(tree1);
2075 if (tree2 && tree2 != tree02)
2076 got_object_tree_close(tree2);
2077 return err;
2080 const struct got_error *
2081 got_object_tree_entry_dup(struct got_tree_entry **new_te,
2082 struct got_tree_entry *te)
2084 const struct got_error *err = NULL;
2086 *new_te = calloc(1, sizeof(**new_te));
2087 if (*new_te == NULL)
2088 return got_error_from_errno("calloc");
2090 (*new_te)->mode = te->mode;
2091 memcpy((*new_te)->name, te->name, sizeof((*new_te)->name));
2092 memcpy(&(*new_te)->id, &te->id, sizeof((*new_te)->id));
2093 return err;
2096 int
2097 got_object_tree_entry_is_submodule(struct got_tree_entry *te)
2099 return (te->mode & S_IFMT) == (S_IFDIR | S_IFLNK);
2102 int
2103 got_object_tree_entry_is_symlink(struct got_tree_entry *te)
2105 /* S_IFDIR check avoids confusing symlinks with submodules. */
2106 return ((te->mode & (S_IFDIR | S_IFLNK)) == S_IFLNK);
2109 static const struct got_error *
2110 resolve_symlink(char **link_target, const char *path,
2111 struct got_commit_object *commit, struct got_repository *repo)
2113 const struct got_error *err = NULL;
2114 char buf[PATH_MAX];
2115 char *name, *parent_path = NULL;
2116 struct got_object_id *tree_obj_id = NULL;
2117 struct got_tree_object *tree = NULL;
2118 struct got_tree_entry *te = NULL;
2120 *link_target = NULL;
2122 if (strlcpy(buf, path, sizeof(buf)) >= sizeof(buf))
2123 return got_error(GOT_ERR_NO_SPACE);
2125 name = basename(buf);
2126 if (name == NULL)
2127 return got_error_from_errno2("basename", path);
2129 err = got_path_dirname(&parent_path, path);
2130 if (err)
2131 return err;
2133 err = got_object_id_by_path(&tree_obj_id, repo, commit,
2134 parent_path);
2135 if (err) {
2136 if (err->code == GOT_ERR_NO_TREE_ENTRY) {
2137 /* Display the complete path in error message. */
2138 err = got_error_path(path, err->code);
2140 goto done;
2143 err = got_object_open_as_tree(&tree, repo, tree_obj_id);
2144 if (err)
2145 goto done;
2147 te = got_object_tree_find_entry(tree, name);
2148 if (te == NULL) {
2149 err = got_error_path(path, GOT_ERR_NO_TREE_ENTRY);
2150 goto done;
2153 if (got_object_tree_entry_is_symlink(te)) {
2154 err = got_tree_entry_get_symlink_target(link_target, te, repo);
2155 if (err)
2156 goto done;
2157 if (!got_path_is_absolute(*link_target)) {
2158 char *abspath;
2159 if (asprintf(&abspath, "%s/%s", parent_path,
2160 *link_target) == -1) {
2161 err = got_error_from_errno("asprintf");
2162 goto done;
2164 free(*link_target);
2165 *link_target = malloc(PATH_MAX);
2166 if (*link_target == NULL) {
2167 err = got_error_from_errno("malloc");
2168 goto done;
2170 err = got_canonpath(abspath, *link_target, PATH_MAX);
2171 free(abspath);
2172 if (err)
2173 goto done;
2176 done:
2177 free(tree_obj_id);
2178 if (tree)
2179 got_object_tree_close(tree);
2180 if (err) {
2181 free(*link_target);
2182 *link_target = NULL;
2184 return err;
2187 const struct got_error *
2188 got_object_resolve_symlinks(char **link_target, const char *path,
2189 struct got_commit_object *commit, struct got_repository *repo)
2191 const struct got_error *err = NULL;
2192 char *next_target = NULL;
2193 int max_recursion = 40; /* matches Git */
2195 *link_target = NULL;
2197 do {
2198 err = resolve_symlink(&next_target,
2199 *link_target ? *link_target : path, commit, repo);
2200 if (err)
2201 break;
2202 if (next_target) {
2203 free(*link_target);
2204 if (--max_recursion == 0) {
2205 err = got_error_path(path, GOT_ERR_RECURSION);
2206 *link_target = NULL;
2207 break;
2209 *link_target = next_target;
2211 } while (next_target);
2213 return err;
2216 const struct got_error *
2217 got_traverse_packed_commits(struct got_object_id_queue *traversed_commits,
2218 struct got_object_id *commit_id, const char *path,
2219 struct got_repository *repo)
2221 const struct got_error *err = NULL;
2222 struct got_pack *pack = NULL;
2223 struct got_packidx *packidx = NULL;
2224 char *path_packfile = NULL;
2225 struct got_commit_object *changed_commit = NULL;
2226 struct got_object_id *changed_commit_id = NULL;
2227 int idx;
2229 err = got_repo_search_packidx(&packidx, &idx, repo, commit_id);
2230 if (err) {
2231 if (err->code != GOT_ERR_NO_OBJ)
2232 return err;
2233 return NULL;
2236 err = got_packidx_get_packfile_path(&path_packfile,
2237 packidx->path_packidx);
2238 if (err)
2239 return err;
2241 pack = got_repo_get_cached_pack(repo, path_packfile);
2242 if (pack == NULL) {
2243 err = got_repo_cache_pack(&pack, repo, path_packfile, packidx);
2244 if (err)
2245 goto done;
2248 if (pack->privsep_child == NULL) {
2249 err = got_pack_start_privsep_child(pack, packidx);
2250 if (err)
2251 goto done;
2254 err = got_privsep_send_commit_traversal_request(
2255 pack->privsep_child->ibuf, commit_id, idx, path);
2256 if (err)
2257 goto done;
2259 err = got_privsep_recv_traversed_commits(&changed_commit,
2260 &changed_commit_id, traversed_commits, pack->privsep_child->ibuf);
2261 if (err)
2262 goto done;
2264 if (changed_commit) {
2266 * Cache the commit in which the path was changed.
2267 * This commit might be opened again soon.
2269 changed_commit->refcnt++;
2270 err = got_repo_cache_commit(repo, changed_commit_id,
2271 changed_commit);
2272 got_object_commit_close(changed_commit);
2274 done:
2275 free(path_packfile);
2276 free(changed_commit_id);
2277 return err;
2280 const struct got_error *
2281 got_object_enumerate(int *found_all_objects,
2282 got_object_enumerate_commit_cb cb_commit,
2283 got_object_enumerate_tree_cb cb_tree, void *cb_arg,
2284 struct got_object_id **ours, int nours,
2285 struct got_object_id **theirs, int ntheirs,
2286 struct got_packidx *packidx, struct got_repository *repo)
2288 const struct got_error *err = NULL;
2289 struct got_pack *pack;
2290 char *path_packfile = NULL;
2292 err = got_packidx_get_packfile_path(&path_packfile,
2293 packidx->path_packidx);
2294 if (err)
2295 return err;
2297 pack = got_repo_get_cached_pack(repo, path_packfile);
2298 if (pack == NULL) {
2299 err = got_repo_cache_pack(&pack, repo, path_packfile, packidx);
2300 if (err)
2301 goto done;
2304 if (pack->privsep_child == NULL) {
2305 err = got_pack_start_privsep_child(pack, packidx);
2306 if (err)
2307 goto done;
2310 err = got_privsep_send_object_enumeration_request(
2311 pack->privsep_child->ibuf);
2312 if (err)
2313 goto done;
2315 err = got_privsep_send_object_idlist(pack->privsep_child->ibuf,
2316 ours, nours);
2317 if (err)
2318 goto done;
2319 err = got_privsep_send_object_idlist_done(pack->privsep_child->ibuf);
2320 if (err)
2321 goto done;
2323 err = got_privsep_send_object_idlist(pack->privsep_child->ibuf,
2324 theirs, ntheirs);
2325 if (err)
2326 goto done;
2327 err = got_privsep_send_object_idlist_done(pack->privsep_child->ibuf);
2328 if (err)
2329 goto done;
2331 err = got_privsep_recv_enumerated_objects(found_all_objects,
2332 pack->privsep_child->ibuf, cb_commit, cb_tree, cb_arg, repo);
2333 done:
2334 free(path_packfile);
2335 return err;