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 if (ftruncate(outfd, 0L) == -1) {
1372 err = got_error_from_errno("ftruncate");
1373 goto done;
1375 if (lseek(outfd, SEEK_SET, 0) == -1) {
1376 err = got_error_from_errno("lseek");
1377 goto done;
1380 err = got_repo_search_packidx(&packidx, &idx, repo, id);
1381 if (err == NULL) {
1382 struct got_pack *pack = NULL;
1384 err = got_packidx_get_packfile_path(&path_packfile,
1385 packidx->path_packidx);
1386 if (err)
1387 goto done;
1389 pack = got_repo_get_cached_pack(repo, path_packfile);
1390 if (pack == NULL) {
1391 err = got_repo_cache_pack(&pack, repo, path_packfile,
1392 packidx);
1393 if (err)
1394 goto done;
1396 err = read_packed_blob_privsep(&outbuf, &size, &hdrlen, outfd,
1397 pack, packidx, idx, id);
1398 } else if (err->code == GOT_ERR_NO_OBJ) {
1399 int infd;
1401 err = got_object_open_loose_fd(&infd, id, repo);
1402 if (err)
1403 goto done;
1404 err = read_blob_privsep(&outbuf, &size, &hdrlen, outfd, infd,
1405 id, repo);
1407 if (err)
1408 goto done;
1410 if (hdrlen > size) {
1411 err = got_error(GOT_ERR_BAD_OBJ_HDR);
1412 goto done;
1415 if (outbuf) {
1416 (*blob)->f = fmemopen(outbuf, size, "rb");
1417 if ((*blob)->f == NULL) {
1418 err = got_error_from_errno("fmemopen");
1419 free(outbuf);
1420 goto done;
1422 (*blob)->data = outbuf;
1423 } else {
1424 if (fstat(outfd, &sb) == -1) {
1425 err = got_error_from_errno("fstat");
1426 goto done;
1429 if (sb.st_size != size) {
1430 err = got_error(GOT_ERR_PRIVSEP_LEN);
1431 goto done;
1434 dfd = dup(outfd);
1435 if (dfd == -1) {
1436 err = got_error_from_errno("dup");
1437 goto done;
1440 (*blob)->f = fdopen(dfd, "rb");
1441 if ((*blob)->f == NULL) {
1442 err = got_error_from_errno("fdopen");
1443 close(dfd);
1444 dfd = -1;
1445 goto done;
1449 (*blob)->hdrlen = hdrlen;
1450 (*blob)->blocksize = blocksize;
1451 memcpy(&(*blob)->id.sha1, id->sha1, SHA1_DIGEST_LENGTH);
1453 done:
1454 free(path_packfile);
1455 if (err) {
1456 if (*blob) {
1457 got_object_blob_close(*blob);
1458 *blob = NULL;
1461 return err;
1464 const struct got_error *
1465 got_object_open_as_blob(struct got_blob_object **blob,
1466 struct got_repository *repo, struct got_object_id *id, size_t blocksize,
1467 int outfd)
1469 return open_blob(blob, repo, id, blocksize, outfd);
1472 const struct got_error *
1473 got_object_blob_open(struct got_blob_object **blob,
1474 struct got_repository *repo, struct got_object *obj, size_t blocksize,
1475 int outfd)
1477 return open_blob(blob, repo, got_object_get_id(obj), blocksize, outfd);
1480 const struct got_error *
1481 got_object_blob_close(struct got_blob_object *blob)
1483 const struct got_error *err = NULL;
1484 free(blob->read_buf);
1485 if (blob->f && fclose(blob->f) == EOF)
1486 err = got_error_from_errno("fclose");
1487 free(blob->data);
1488 free(blob);
1489 return err;
1492 void
1493 got_object_blob_rewind(struct got_blob_object *blob)
1495 if (blob->f)
1496 rewind(blob->f);
1499 char *
1500 got_object_blob_id_str(struct got_blob_object *blob, char *buf, size_t size)
1502 return got_sha1_digest_to_str(blob->id.sha1, buf, size);
1505 size_t
1506 got_object_blob_get_hdrlen(struct got_blob_object *blob)
1508 return blob->hdrlen;
1511 const uint8_t *
1512 got_object_blob_get_read_buf(struct got_blob_object *blob)
1514 return blob->read_buf;
1517 const struct got_error *
1518 got_object_blob_read_block(size_t *outlenp, struct got_blob_object *blob)
1520 size_t n;
1522 n = fread(blob->read_buf, 1, blob->blocksize, blob->f);
1523 if (n == 0 && ferror(blob->f))
1524 return got_ferror(blob->f, GOT_ERR_IO);
1525 *outlenp = n;
1526 return NULL;
1529 const struct got_error *
1530 got_object_blob_dump_to_file(off_t *filesize, int *nlines,
1531 off_t **line_offsets, FILE *outfile, struct got_blob_object *blob)
1533 const struct got_error *err = NULL;
1534 size_t n, len, hdrlen;
1535 const uint8_t *buf;
1536 int i;
1537 const int alloc_chunksz = 512;
1538 size_t nalloc = 0;
1539 off_t off = 0, total_len = 0;
1541 if (line_offsets)
1542 *line_offsets = NULL;
1543 if (filesize)
1544 *filesize = 0;
1545 if (nlines)
1546 *nlines = 0;
1548 hdrlen = got_object_blob_get_hdrlen(blob);
1549 do {
1550 err = got_object_blob_read_block(&len, blob);
1551 if (err)
1552 return err;
1553 if (len == 0)
1554 break;
1555 buf = got_object_blob_get_read_buf(blob);
1556 i = hdrlen;
1557 if (nlines) {
1558 if (line_offsets && *line_offsets == NULL) {
1559 /* Have some data but perhaps no '\n'. */
1560 *nlines = 1;
1561 nalloc = alloc_chunksz;
1562 *line_offsets = calloc(nalloc,
1563 sizeof(**line_offsets));
1564 if (*line_offsets == NULL)
1565 return got_error_from_errno("calloc");
1567 /* Skip forward over end of first line. */
1568 while (i < len) {
1569 if (buf[i] == '\n')
1570 break;
1571 i++;
1574 /* Scan '\n' offsets in remaining chunk of data. */
1575 while (i < len) {
1576 if (buf[i] != '\n') {
1577 i++;
1578 continue;
1580 (*nlines)++;
1581 if (line_offsets && nalloc < *nlines) {
1582 size_t n = *nlines + alloc_chunksz;
1583 off_t *o = recallocarray(*line_offsets,
1584 nalloc, n, sizeof(**line_offsets));
1585 if (o == NULL) {
1586 free(*line_offsets);
1587 *line_offsets = NULL;
1588 return got_error_from_errno(
1589 "recallocarray");
1591 *line_offsets = o;
1592 nalloc = n;
1594 if (line_offsets) {
1595 off = total_len + i - hdrlen + 1;
1596 (*line_offsets)[*nlines - 1] = off;
1598 i++;
1601 /* Skip blob object header first time around. */
1602 n = fwrite(buf + hdrlen, 1, len - hdrlen, outfile);
1603 if (n != len - hdrlen)
1604 return got_ferror(outfile, GOT_ERR_IO);
1605 total_len += len - hdrlen;
1606 hdrlen = 0;
1607 } while (len != 0);
1609 if (fflush(outfile) != 0)
1610 return got_error_from_errno("fflush");
1611 rewind(outfile);
1613 if (filesize)
1614 *filesize = total_len;
1616 return NULL;
1619 static const struct got_error *
1620 request_packed_tag(struct got_tag_object **tag, struct got_pack *pack,
1621 int pack_idx, struct got_object_id *id)
1623 const struct got_error *err = NULL;
1625 err = got_privsep_send_tag_req(pack->privsep_child->ibuf, -1, id,
1626 pack_idx);
1627 if (err)
1628 return err;
1630 return got_privsep_recv_tag(tag, pack->privsep_child->ibuf);
1633 static const struct got_error *
1634 read_packed_tag_privsep(struct got_tag_object **tag,
1635 struct got_pack *pack, struct got_packidx *packidx, int idx,
1636 struct got_object_id *id)
1638 const struct got_error *err = NULL;
1640 if (pack->privsep_child)
1641 return request_packed_tag(tag, pack, idx, id);
1643 err = got_pack_start_privsep_child(pack, packidx);
1644 if (err)
1645 return err;
1647 return request_packed_tag(tag, pack, idx, id);
1650 static const struct got_error *
1651 request_tag(struct got_tag_object **tag, struct got_repository *repo,
1652 int fd, struct got_object_id *id)
1654 const struct got_error *err = NULL;
1655 struct imsgbuf *ibuf;
1657 ibuf = repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TAG].ibuf;
1659 err = got_privsep_send_tag_req(ibuf, fd, id, -1);
1660 if (err)
1661 return err;
1663 return got_privsep_recv_tag(tag, ibuf);
1666 static const struct got_error *
1667 read_tag_privsep(struct got_tag_object **tag, int obj_fd,
1668 struct got_object_id *id, struct got_repository *repo)
1670 const struct got_error *err;
1671 int imsg_fds[2];
1672 pid_t pid;
1673 struct imsgbuf *ibuf;
1675 if (repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TAG].imsg_fd != -1)
1676 return request_tag(tag, repo, obj_fd, id);
1678 ibuf = calloc(1, sizeof(*ibuf));
1679 if (ibuf == NULL)
1680 return got_error_from_errno("calloc");
1682 if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_fds) == -1) {
1683 err = got_error_from_errno("socketpair");
1684 free(ibuf);
1685 return err;
1688 pid = fork();
1689 if (pid == -1) {
1690 err = got_error_from_errno("fork");
1691 free(ibuf);
1692 return err;
1694 else if (pid == 0) {
1695 got_privsep_exec_child(imsg_fds, GOT_PATH_PROG_READ_TAG,
1696 repo->path);
1697 /* not reached */
1700 if (close(imsg_fds[1]) == -1) {
1701 err = got_error_from_errno("close");
1702 free(ibuf);
1703 return err;
1705 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TAG].imsg_fd =
1706 imsg_fds[0];
1707 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TAG].pid = pid;
1708 imsg_init(ibuf, imsg_fds[0]);
1709 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TAG].ibuf = ibuf;
1711 return request_tag(tag, repo, obj_fd, id);
1714 static const struct got_error *
1715 open_tag(struct got_tag_object **tag, struct got_repository *repo,
1716 struct got_object_id *id, int check_cache)
1718 const struct got_error *err = NULL;
1719 struct got_packidx *packidx = NULL;
1720 int idx;
1721 char *path_packfile = NULL;
1722 struct got_object *obj = NULL;
1723 int obj_type = GOT_OBJ_TYPE_ANY;
1725 if (check_cache) {
1726 *tag = got_repo_get_cached_tag(repo, id);
1727 if (*tag != NULL) {
1728 (*tag)->refcnt++;
1729 return NULL;
1731 } else
1732 *tag = NULL;
1734 err = got_repo_search_packidx(&packidx, &idx, repo, id);
1735 if (err == NULL) {
1736 struct got_pack *pack = NULL;
1738 err = got_packidx_get_packfile_path(&path_packfile,
1739 packidx->path_packidx);
1740 if (err)
1741 return err;
1743 pack = got_repo_get_cached_pack(repo, path_packfile);
1744 if (pack == NULL) {
1745 err = got_repo_cache_pack(&pack, repo, path_packfile,
1746 packidx);
1747 if (err)
1748 goto done;
1751 /* Beware of "lightweight" tags: Check object type first. */
1752 err = read_packed_object_privsep(&obj, repo, pack, packidx,
1753 idx, id);
1754 if (err)
1755 goto done;
1756 obj_type = obj->type;
1757 got_object_close(obj);
1758 if (obj_type != GOT_OBJ_TYPE_TAG) {
1759 err = got_error(GOT_ERR_OBJ_TYPE);
1760 goto done;
1762 err = read_packed_tag_privsep(tag, pack, packidx, idx, id);
1763 } else if (err->code == GOT_ERR_NO_OBJ) {
1764 int fd;
1766 err = got_object_open_loose_fd(&fd, id, repo);
1767 if (err)
1768 return err;
1769 err = got_object_read_header_privsep(&obj, id, repo, fd);
1770 if (err)
1771 return err;
1772 obj_type = obj->type;
1773 got_object_close(obj);
1774 if (obj_type != GOT_OBJ_TYPE_TAG)
1775 return got_error(GOT_ERR_OBJ_TYPE);
1777 err = got_object_open_loose_fd(&fd, id, repo);
1778 if (err)
1779 return err;
1780 err = read_tag_privsep(tag, fd, id, repo);
1783 if (err == NULL) {
1784 (*tag)->refcnt++;
1785 err = got_repo_cache_tag(repo, id, *tag);
1787 done:
1788 free(path_packfile);
1789 return err;
1792 const struct got_error *
1793 got_object_open_as_tag(struct got_tag_object **tag,
1794 struct got_repository *repo, struct got_object_id *id)
1796 *tag = got_repo_get_cached_tag(repo, id);
1797 if (*tag != NULL) {
1798 (*tag)->refcnt++;
1799 return NULL;
1802 return open_tag(tag, repo, id, 0);
1805 const struct got_error *
1806 got_object_tag_open(struct got_tag_object **tag,
1807 struct got_repository *repo, struct got_object *obj)
1809 return open_tag(tag, repo, got_object_get_id(obj), 1);
1812 const char *
1813 got_object_tag_get_name(struct got_tag_object *tag)
1815 return tag->tag;
1818 int
1819 got_object_tag_get_object_type(struct got_tag_object *tag)
1821 return tag->obj_type;
1824 struct got_object_id *
1825 got_object_tag_get_object_id(struct got_tag_object *tag)
1827 return &tag->id;
1830 time_t
1831 got_object_tag_get_tagger_time(struct got_tag_object *tag)
1833 return tag->tagger_time;
1836 time_t
1837 got_object_tag_get_tagger_gmtoff(struct got_tag_object *tag)
1839 return tag->tagger_gmtoff;
1842 const char *
1843 got_object_tag_get_tagger(struct got_tag_object *tag)
1845 return tag->tagger;
1848 const char *
1849 got_object_tag_get_message(struct got_tag_object *tag)
1851 return tag->tagmsg;
1854 static struct got_tree_entry *
1855 find_entry_by_name(struct got_tree_object *tree, const char *name, size_t len)
1857 int i;
1859 /* Note that tree entries are sorted in strncmp() order. */
1860 for (i = 0; i < tree->nentries; i++) {
1861 struct got_tree_entry *te = &tree->entries[i];
1862 int cmp = strncmp(te->name, name, len);
1863 if (cmp < 0)
1864 continue;
1865 if (cmp > 0)
1866 break;
1867 if (te->name[len] == '\0')
1868 return te;
1870 return NULL;
1873 struct got_tree_entry *
1874 got_object_tree_find_entry(struct got_tree_object *tree, const char *name)
1876 return find_entry_by_name(tree, name, strlen(name));
1879 const struct got_error *
1880 got_object_tree_find_path(struct got_object_id **id, mode_t *mode,
1881 struct got_repository *repo, struct got_tree_object *tree,
1882 const char *path)
1884 const struct got_error *err = NULL;
1885 struct got_tree_object *subtree = NULL;
1886 struct got_tree_entry *te = NULL;
1887 const char *seg, *s;
1888 size_t seglen;
1890 *id = NULL;
1892 s = path;
1893 while (s[0] == '/')
1894 s++;
1895 seg = s;
1896 seglen = 0;
1897 subtree = tree;
1898 while (*s) {
1899 struct got_tree_object *next_tree;
1901 if (*s != '/') {
1902 s++;
1903 seglen++;
1904 if (*s)
1905 continue;
1908 te = find_entry_by_name(subtree, seg, seglen);
1909 if (te == NULL) {
1910 err = got_error_path(path, GOT_ERR_NO_TREE_ENTRY);
1911 goto done;
1914 if (*s == '\0')
1915 break;
1917 seg = s + 1;
1918 seglen = 0;
1919 s++;
1920 if (*s) {
1921 err = got_object_open_as_tree(&next_tree, repo,
1922 &te->id);
1923 te = NULL;
1924 if (err)
1925 goto done;
1926 if (subtree != tree)
1927 got_object_tree_close(subtree);
1928 subtree = next_tree;
1932 if (te) {
1933 *id = got_object_id_dup(&te->id);
1934 if (*id == NULL)
1935 return got_error_from_errno("got_object_id_dup");
1936 if (mode)
1937 *mode = te->mode;
1938 } else
1939 err = got_error_path(path, GOT_ERR_NO_TREE_ENTRY);
1940 done:
1941 if (subtree && subtree != tree)
1942 got_object_tree_close(subtree);
1943 return err;
1945 const struct got_error *
1946 got_object_id_by_path(struct got_object_id **id, struct got_repository *repo,
1947 struct got_commit_object *commit, const char *path)
1949 const struct got_error *err = NULL;
1950 struct got_tree_object *tree = NULL;
1952 *id = NULL;
1954 /* Handle opening of root of commit's tree. */
1955 if (got_path_is_root_dir(path)) {
1956 *id = got_object_id_dup(commit->tree_id);
1957 if (*id == NULL)
1958 err = got_error_from_errno("got_object_id_dup");
1959 } else {
1960 err = got_object_open_as_tree(&tree, repo, commit->tree_id);
1961 if (err)
1962 goto done;
1963 err = got_object_tree_find_path(id, NULL, repo, tree, path);
1965 done:
1966 if (tree)
1967 got_object_tree_close(tree);
1968 return err;
1972 * Normalize file mode bits to avoid false positive tree entry differences
1973 * in case tree entries have unexpected mode bits set.
1975 static mode_t
1976 normalize_mode_for_comparison(mode_t mode)
1979 * For directories, the only relevant bit is the IFDIR bit.
1980 * This allows us to detect paths changing from a directory
1981 * to a file and vice versa.
1983 if (S_ISDIR(mode))
1984 return mode & S_IFDIR;
1987 * For symlinks, the only relevant bit is the IFLNK bit.
1988 * This allows us to detect paths changing from a symlinks
1989 * to a file or directory and vice versa.
1991 if (S_ISLNK(mode))
1992 return mode & S_IFLNK;
1994 /* For files, the only change we care about is the executable bit. */
1995 return mode & S_IXUSR;
1998 const struct got_error *
1999 got_object_tree_path_changed(int *changed,
2000 struct got_tree_object *tree01, struct got_tree_object *tree02,
2001 const char *path, struct got_repository *repo)
2003 const struct got_error *err = NULL;
2004 struct got_tree_object *tree1 = NULL, *tree2 = NULL;
2005 struct got_tree_entry *te1 = NULL, *te2 = NULL;
2006 const char *seg, *s;
2007 size_t seglen;
2009 *changed = 0;
2011 /* We not do support comparing the root path. */
2012 if (got_path_is_root_dir(path))
2013 return got_error_path(path, GOT_ERR_BAD_PATH);
2015 tree1 = tree01;
2016 tree2 = tree02;
2017 s = path;
2018 while (*s == '/')
2019 s++;
2020 seg = s;
2021 seglen = 0;
2022 while (*s) {
2023 struct got_tree_object *next_tree1, *next_tree2;
2024 mode_t mode1, mode2;
2026 if (*s != '/') {
2027 s++;
2028 seglen++;
2029 if (*s)
2030 continue;
2033 te1 = find_entry_by_name(tree1, seg, seglen);
2034 if (te1 == NULL) {
2035 err = got_error(GOT_ERR_NO_OBJ);
2036 goto done;
2039 if (tree2)
2040 te2 = find_entry_by_name(tree2, seg, seglen);
2042 if (te2) {
2043 mode1 = normalize_mode_for_comparison(te1->mode);
2044 mode2 = normalize_mode_for_comparison(te2->mode);
2045 if (mode1 != mode2) {
2046 *changed = 1;
2047 goto done;
2050 if (got_object_id_cmp(&te1->id, &te2->id) == 0) {
2051 *changed = 0;
2052 goto done;
2056 if (*s == '\0') { /* final path element */
2057 *changed = 1;
2058 goto done;
2061 seg = s + 1;
2062 s++;
2063 seglen = 0;
2064 if (*s) {
2065 err = got_object_open_as_tree(&next_tree1, repo,
2066 &te1->id);
2067 te1 = NULL;
2068 if (err)
2069 goto done;
2070 if (tree1 != tree01)
2071 got_object_tree_close(tree1);
2072 tree1 = next_tree1;
2074 if (te2) {
2075 err = got_object_open_as_tree(&next_tree2, repo,
2076 &te2->id);
2077 te2 = NULL;
2078 if (err)
2079 goto done;
2080 if (tree2 != tree02)
2081 got_object_tree_close(tree2);
2082 tree2 = next_tree2;
2083 } else if (tree2) {
2084 if (tree2 != tree02)
2085 got_object_tree_close(tree2);
2086 tree2 = NULL;
2090 done:
2091 if (tree1 && tree1 != tree01)
2092 got_object_tree_close(tree1);
2093 if (tree2 && tree2 != tree02)
2094 got_object_tree_close(tree2);
2095 return err;
2098 const struct got_error *
2099 got_object_tree_entry_dup(struct got_tree_entry **new_te,
2100 struct got_tree_entry *te)
2102 const struct got_error *err = NULL;
2104 *new_te = calloc(1, sizeof(**new_te));
2105 if (*new_te == NULL)
2106 return got_error_from_errno("calloc");
2108 (*new_te)->mode = te->mode;
2109 memcpy((*new_te)->name, te->name, sizeof((*new_te)->name));
2110 memcpy(&(*new_te)->id, &te->id, sizeof((*new_te)->id));
2111 return err;
2114 int
2115 got_object_tree_entry_is_submodule(struct got_tree_entry *te)
2117 return (te->mode & S_IFMT) == (S_IFDIR | S_IFLNK);
2120 int
2121 got_object_tree_entry_is_symlink(struct got_tree_entry *te)
2123 /* S_IFDIR check avoids confusing symlinks with submodules. */
2124 return ((te->mode & (S_IFDIR | S_IFLNK)) == S_IFLNK);
2127 static const struct got_error *
2128 resolve_symlink(char **link_target, const char *path,
2129 struct got_commit_object *commit, struct got_repository *repo)
2131 const struct got_error *err = NULL;
2132 char buf[PATH_MAX];
2133 char *name, *parent_path = NULL;
2134 struct got_object_id *tree_obj_id = NULL;
2135 struct got_tree_object *tree = NULL;
2136 struct got_tree_entry *te = NULL;
2138 *link_target = NULL;
2140 if (strlcpy(buf, path, sizeof(buf)) >= sizeof(buf))
2141 return got_error(GOT_ERR_NO_SPACE);
2143 name = basename(buf);
2144 if (name == NULL)
2145 return got_error_from_errno2("basename", path);
2147 err = got_path_dirname(&parent_path, path);
2148 if (err)
2149 return err;
2151 err = got_object_id_by_path(&tree_obj_id, repo, commit,
2152 parent_path);
2153 if (err) {
2154 if (err->code == GOT_ERR_NO_TREE_ENTRY) {
2155 /* Display the complete path in error message. */
2156 err = got_error_path(path, err->code);
2158 goto done;
2161 err = got_object_open_as_tree(&tree, repo, tree_obj_id);
2162 if (err)
2163 goto done;
2165 te = got_object_tree_find_entry(tree, name);
2166 if (te == NULL) {
2167 err = got_error_path(path, GOT_ERR_NO_TREE_ENTRY);
2168 goto done;
2171 if (got_object_tree_entry_is_symlink(te)) {
2172 err = got_tree_entry_get_symlink_target(link_target, te, repo);
2173 if (err)
2174 goto done;
2175 if (!got_path_is_absolute(*link_target)) {
2176 char *abspath;
2177 if (asprintf(&abspath, "%s/%s", parent_path,
2178 *link_target) == -1) {
2179 err = got_error_from_errno("asprintf");
2180 goto done;
2182 free(*link_target);
2183 *link_target = malloc(PATH_MAX);
2184 if (*link_target == NULL) {
2185 err = got_error_from_errno("malloc");
2186 goto done;
2188 err = got_canonpath(abspath, *link_target, PATH_MAX);
2189 free(abspath);
2190 if (err)
2191 goto done;
2194 done:
2195 free(tree_obj_id);
2196 if (tree)
2197 got_object_tree_close(tree);
2198 if (err) {
2199 free(*link_target);
2200 *link_target = NULL;
2202 return err;
2205 const struct got_error *
2206 got_object_resolve_symlinks(char **link_target, const char *path,
2207 struct got_commit_object *commit, struct got_repository *repo)
2209 const struct got_error *err = NULL;
2210 char *next_target = NULL;
2211 int max_recursion = 40; /* matches Git */
2213 *link_target = NULL;
2215 do {
2216 err = resolve_symlink(&next_target,
2217 *link_target ? *link_target : path, commit, repo);
2218 if (err)
2219 break;
2220 if (next_target) {
2221 free(*link_target);
2222 if (--max_recursion == 0) {
2223 err = got_error_path(path, GOT_ERR_RECURSION);
2224 *link_target = NULL;
2225 break;
2227 *link_target = next_target;
2229 } while (next_target);
2231 return err;
2234 const struct got_error *
2235 got_traverse_packed_commits(struct got_object_id_queue *traversed_commits,
2236 struct got_object_id *commit_id, const char *path,
2237 struct got_repository *repo)
2239 const struct got_error *err = NULL;
2240 struct got_pack *pack = NULL;
2241 struct got_packidx *packidx = NULL;
2242 char *path_packfile = NULL;
2243 struct got_commit_object *changed_commit = NULL;
2244 struct got_object_id *changed_commit_id = NULL;
2245 int idx;
2247 err = got_repo_search_packidx(&packidx, &idx, repo, commit_id);
2248 if (err) {
2249 if (err->code != GOT_ERR_NO_OBJ)
2250 return err;
2251 return NULL;
2254 err = got_packidx_get_packfile_path(&path_packfile,
2255 packidx->path_packidx);
2256 if (err)
2257 return err;
2259 pack = got_repo_get_cached_pack(repo, path_packfile);
2260 if (pack == NULL) {
2261 err = got_repo_cache_pack(&pack, repo, path_packfile, packidx);
2262 if (err)
2263 goto done;
2266 if (pack->privsep_child == NULL) {
2267 err = got_pack_start_privsep_child(pack, packidx);
2268 if (err)
2269 goto done;
2272 err = got_privsep_send_commit_traversal_request(
2273 pack->privsep_child->ibuf, commit_id, idx, path);
2274 if (err)
2275 goto done;
2277 err = got_privsep_recv_traversed_commits(&changed_commit,
2278 &changed_commit_id, traversed_commits, pack->privsep_child->ibuf);
2279 if (err)
2280 goto done;
2282 if (changed_commit) {
2284 * Cache the commit in which the path was changed.
2285 * This commit might be opened again soon.
2287 changed_commit->refcnt++;
2288 err = got_repo_cache_commit(repo, changed_commit_id,
2289 changed_commit);
2290 got_object_commit_close(changed_commit);
2292 done:
2293 free(path_packfile);
2294 free(changed_commit_id);
2295 return err;
2298 const struct got_error *
2299 got_object_enumerate(int *found_all_objects,
2300 got_object_enumerate_commit_cb cb_commit,
2301 got_object_enumerate_tree_cb cb_tree, void *cb_arg,
2302 struct got_object_id **ours, int nours,
2303 struct got_object_id **theirs, int ntheirs,
2304 struct got_packidx *packidx, struct got_repository *repo)
2306 const struct got_error *err = NULL;
2307 struct got_pack *pack;
2308 char *path_packfile = NULL;
2310 err = got_packidx_get_packfile_path(&path_packfile,
2311 packidx->path_packidx);
2312 if (err)
2313 return err;
2315 pack = got_repo_get_cached_pack(repo, path_packfile);
2316 if (pack == NULL) {
2317 err = got_repo_cache_pack(&pack, repo, path_packfile, packidx);
2318 if (err)
2319 goto done;
2322 if (pack->privsep_child == NULL) {
2323 err = got_pack_start_privsep_child(pack, packidx);
2324 if (err)
2325 goto done;
2328 err = got_privsep_send_object_enumeration_request(
2329 pack->privsep_child->ibuf);
2330 if (err)
2331 goto done;
2333 err = got_privsep_send_object_idlist(pack->privsep_child->ibuf,
2334 ours, nours);
2335 if (err)
2336 goto done;
2337 err = got_privsep_send_object_idlist_done(pack->privsep_child->ibuf);
2338 if (err)
2339 goto done;
2341 err = got_privsep_send_object_idlist(pack->privsep_child->ibuf,
2342 theirs, ntheirs);
2343 if (err)
2344 goto done;
2345 err = got_privsep_send_object_idlist_done(pack->privsep_child->ibuf);
2346 if (err)
2347 goto done;
2349 err = got_privsep_recv_enumerated_objects(found_all_objects,
2350 pack->privsep_child->ibuf, cb_commit, cb_tree, cb_arg, repo);
2351 done:
2352 free(path_packfile);
2353 return err;