Blob


1 /*
2 * Copyright (c) 2018, 2019 Stefan Sperling <stsp@openbsd.org>
3 *
4 * Permission to use, copy, modify, and distribute this software for any
5 * purpose with or without fee is hereby granted, provided that the above
6 * copyright notice and this permission notice appear in all copies.
7 *
8 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
15 */
17 #include <sys/types.h>
18 #include <sys/stat.h>
19 #include <sys/queue.h>
20 #include <sys/tree.h>
21 #include <sys/uio.h>
22 #include <sys/socket.h>
23 #include <sys/wait.h>
24 #include <sys/resource.h>
25 #include <sys/mman.h>
27 #include <errno.h>
28 #include <fcntl.h>
29 #include <stdio.h>
30 #include <stdlib.h>
31 #include <string.h>
32 #include <stdint.h>
33 #include <sha1.h>
34 #include <unistd.h>
35 #include <zlib.h>
36 #include <ctype.h>
37 #include <libgen.h>
38 #include <limits.h>
39 #include <imsg.h>
40 #include <time.h>
42 #include "got_error.h"
43 #include "got_object.h"
44 #include "got_repository.h"
45 #include "got_opentemp.h"
46 #include "got_path.h"
48 #include "got_lib_sha1.h"
49 #include "got_lib_delta.h"
50 #include "got_lib_inflate.h"
51 #include "got_lib_object.h"
52 #include "got_lib_privsep.h"
53 #include "got_lib_object_idcache.h"
54 #include "got_lib_object_cache.h"
55 #include "got_lib_object_parse.h"
56 #include "got_lib_pack.h"
57 #include "got_lib_repository.h"
59 #ifndef MIN
60 #define MIN(_a,_b) ((_a) < (_b) ? (_a) : (_b))
61 #endif
63 #ifndef nitems
64 #define nitems(_a) (sizeof((_a)) / sizeof((_a)[0]))
65 #endif
67 struct got_object_id *
68 got_object_get_id(struct got_object *obj)
69 {
70 return &obj->id;
71 }
73 const struct got_error *
74 got_object_get_id_str(char **outbuf, struct got_object *obj)
75 {
76 return got_object_id_str(outbuf, &obj->id);
77 }
79 const struct got_error *
80 got_object_get_type(int *type, struct got_repository *repo,
81 struct got_object_id *id)
82 {
83 const struct got_error *err = NULL;
84 struct got_object *obj;
86 err = got_object_open(&obj, repo, id);
87 if (err)
88 return err;
90 switch (obj->type) {
91 case GOT_OBJ_TYPE_COMMIT:
92 case GOT_OBJ_TYPE_TREE:
93 case GOT_OBJ_TYPE_BLOB:
94 case GOT_OBJ_TYPE_TAG:
95 *type = obj->type;
96 break;
97 default:
98 err = got_error(GOT_ERR_OBJ_TYPE);
99 break;
102 got_object_close(obj);
103 return err;
106 const struct got_error *
107 got_object_get_path(char **path, struct got_object_id *id,
108 struct got_repository *repo)
110 const struct got_error *err = NULL;
111 char *hex = NULL;
112 char *path_objects;
114 *path = NULL;
116 path_objects = got_repo_get_path_objects(repo);
117 if (path_objects == NULL)
118 return got_error_from_errno("got_repo_get_path_objects");
120 err = got_object_id_str(&hex, id);
121 if (err)
122 goto done;
124 if (asprintf(path, "%s/%.2x/%s", path_objects,
125 id->sha1[0], hex + 2) == -1)
126 err = got_error_from_errno("asprintf");
128 done:
129 free(hex);
130 free(path_objects);
131 return err;
134 const struct got_error *
135 got_object_open_loose_fd(int *fd, struct got_object_id *id,
136 struct got_repository *repo)
138 const struct got_error *err = NULL;
139 char *path;
141 err = got_object_get_path(&path, id, repo);
142 if (err)
143 return err;
144 *fd = open(path, O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
145 if (*fd == -1) {
146 err = got_error_from_errno2("open", path);
147 goto done;
149 done:
150 free(path);
151 return err;
154 static const struct got_error *
155 request_packed_object(struct got_object **obj, struct got_pack *pack, int idx,
156 struct got_object_id *id)
158 const struct got_error *err = NULL;
159 struct imsgbuf *ibuf = pack->privsep_child->ibuf;
161 err = got_privsep_send_packed_obj_req(ibuf, idx, id);
162 if (err)
163 return err;
165 err = got_privsep_recv_obj(obj, ibuf);
166 if (err)
167 return err;
169 memcpy(&(*obj)->id, id, sizeof((*obj)->id));
171 return NULL;
174 /* Create temporary files used during delta application. */
175 static const struct got_error *
176 pack_child_send_tempfiles(struct imsgbuf *ibuf, struct got_pack *pack)
178 const struct got_error *err;
179 int basefd = -1, accumfd = -1;
181 /*
182 * For performance reasons, the child will keep reusing the
183 * same temporary files during every object request.
184 * Opening and closing new files for every object request is
185 * too expensive during operations such as 'gotadmin pack'.
186 */
187 if (pack->child_has_tempfiles)
188 return NULL;
190 basefd = dup(pack->basefd);
191 if (basefd == -1)
192 return got_error_from_errno("dup");
194 accumfd = dup(pack->accumfd);
195 if (accumfd == -1) {
196 err = got_error_from_errno("dup");
197 goto done;
200 err = got_privsep_send_tmpfd(ibuf, basefd);
201 if (err)
202 goto done;
204 err = got_privsep_send_tmpfd(ibuf, accumfd);
205 done:
206 if (err) {
207 if (basefd != -1)
208 close(basefd);
209 if (accumfd != -1)
210 close(accumfd);
211 } else
212 pack->child_has_tempfiles = 1;
213 return NULL;
216 static const struct got_error *
217 request_packed_object_raw(uint8_t **outbuf, off_t *size, size_t *hdrlen,
218 int outfd, struct got_pack *pack, int idx, struct got_object_id *id)
220 const struct got_error *err = NULL;
221 struct imsgbuf *ibuf = pack->privsep_child->ibuf;
222 int outfd_child;
224 err = pack_child_send_tempfiles(ibuf, pack);
225 if (err)
226 return err;
228 outfd_child = dup(outfd);
229 if (outfd_child == -1)
230 return got_error_from_errno("dup");
232 err = got_privsep_send_packed_raw_obj_req(ibuf, idx, id);
233 if (err) {
234 close(outfd_child);
235 return err;
238 err = got_privsep_send_raw_obj_outfd(ibuf, outfd_child);
239 if (err)
240 return err;
242 err = got_privsep_recv_raw_obj(outbuf, size, hdrlen, ibuf);
243 if (err)
244 return err;
246 return NULL;
249 static void
250 set_max_datasize(void)
252 struct rlimit rl;
254 if (getrlimit(RLIMIT_DATA, &rl) != 0)
255 return;
257 rl.rlim_cur = rl.rlim_max;
258 setrlimit(RLIMIT_DATA, &rl);
261 static const struct got_error *
262 start_pack_privsep_child(struct got_pack *pack, struct got_packidx *packidx)
264 const struct got_error *err = NULL;
265 int imsg_fds[2];
266 pid_t pid;
267 struct imsgbuf *ibuf;
269 ibuf = calloc(1, sizeof(*ibuf));
270 if (ibuf == NULL)
271 return got_error_from_errno("calloc");
273 pack->privsep_child = calloc(1, sizeof(*pack->privsep_child));
274 if (pack->privsep_child == NULL) {
275 err = got_error_from_errno("calloc");
276 free(ibuf);
277 return err;
279 pack->child_has_tempfiles = 0;
280 pack->child_has_delta_outfd = 0;
282 if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_fds) == -1) {
283 err = got_error_from_errno("socketpair");
284 goto done;
287 pid = fork();
288 if (pid == -1) {
289 err = got_error_from_errno("fork");
290 goto done;
291 } else if (pid == 0) {
292 set_max_datasize();
293 got_privsep_exec_child(imsg_fds, GOT_PATH_PROG_READ_PACK,
294 pack->path_packfile);
295 /* not reached */
298 if (close(imsg_fds[1]) == -1)
299 return got_error_from_errno("close");
300 pack->privsep_child->imsg_fd = imsg_fds[0];
301 pack->privsep_child->pid = pid;
302 imsg_init(ibuf, imsg_fds[0]);
303 pack->privsep_child->ibuf = ibuf;
305 err = got_privsep_init_pack_child(ibuf, pack, packidx);
306 if (err) {
307 const struct got_error *child_err;
308 err = got_privsep_send_stop(pack->privsep_child->imsg_fd);
309 child_err = got_privsep_wait_for_child(
310 pack->privsep_child->pid);
311 if (child_err && err == NULL)
312 err = child_err;
314 done:
315 if (err) {
316 free(ibuf);
317 free(pack->privsep_child);
318 pack->privsep_child = NULL;
320 return err;
323 static const struct got_error *
324 read_packed_object_privsep(struct got_object **obj,
325 struct got_repository *repo, struct got_pack *pack,
326 struct got_packidx *packidx, int idx, struct got_object_id *id)
328 const struct got_error *err = NULL;
330 if (pack->privsep_child == NULL) {
331 err = start_pack_privsep_child(pack, packidx);
332 if (err)
333 return err;
336 return request_packed_object(obj, pack, idx, id);
339 static const struct got_error *
340 read_packed_object_raw_privsep(uint8_t **outbuf, off_t *size, size_t *hdrlen,
341 int outfd, struct got_pack *pack, struct got_packidx *packidx, int idx,
342 struct got_object_id *id)
344 const struct got_error *err = NULL;
346 if (pack->privsep_child == NULL) {
347 err = start_pack_privsep_child(pack, packidx);
348 if (err)
349 return err;
352 return request_packed_object_raw(outbuf, size, hdrlen, outfd, pack,
353 idx, id);
356 const struct got_error *
357 got_object_open_packed(struct got_object **obj, struct got_object_id *id,
358 struct got_repository *repo)
360 const struct got_error *err = NULL;
361 struct got_pack *pack = NULL;
362 struct got_packidx *packidx = NULL;
363 int idx;
364 char *path_packfile;
366 err = got_repo_search_packidx(&packidx, &idx, repo, id);
367 if (err)
368 return err;
370 err = got_packidx_get_packfile_path(&path_packfile,
371 packidx->path_packidx);
372 if (err)
373 return err;
375 pack = got_repo_get_cached_pack(repo, path_packfile);
376 if (pack == NULL) {
377 err = got_repo_cache_pack(&pack, repo, path_packfile, packidx);
378 if (err)
379 goto done;
382 err = read_packed_object_privsep(obj, repo, pack, packidx, idx, id);
383 if (err)
384 goto done;
385 done:
386 free(path_packfile);
387 return err;
390 const struct got_error *
391 got_object_open_from_packfile(struct got_object **obj, struct got_object_id *id,
392 struct got_pack *pack, struct got_packidx *packidx, int obj_idx,
393 struct got_repository *repo)
395 return read_packed_object_privsep(obj, repo, pack, packidx,
396 obj_idx, id);
399 const struct got_error *
400 got_object_read_raw_delta(uint64_t *base_size, uint64_t *result_size,
401 off_t *delta_size, off_t *delta_compressed_size, off_t *delta_offset,
402 off_t *delta_out_offset, struct got_object_id **base_id, int delta_cache_fd,
403 struct got_packidx *packidx, int obj_idx, struct got_object_id *id,
404 struct got_repository *repo)
406 const struct got_error *err = NULL;
407 struct got_pack *pack = NULL;
408 char *path_packfile;
410 *base_size = 0;
411 *result_size = 0;
412 *delta_size = 0;
413 *delta_compressed_size = 0;
414 *delta_offset = 0;
415 *delta_out_offset = 0;
417 err = got_packidx_get_packfile_path(&path_packfile,
418 packidx->path_packidx);
419 if (err)
420 return err;
422 pack = got_repo_get_cached_pack(repo, path_packfile);
423 if (pack == NULL) {
424 err = got_repo_cache_pack(&pack, repo, path_packfile, packidx);
425 if (err)
426 return err;
429 if (pack->privsep_child == NULL) {
430 err = start_pack_privsep_child(pack, packidx);
431 if (err)
432 return err;
435 if (!pack->child_has_delta_outfd) {
436 int outfd_child;
437 outfd_child = dup(delta_cache_fd);
438 if (outfd_child == -1)
439 return got_error_from_errno("dup");
440 err = got_privsep_send_raw_delta_outfd(
441 pack->privsep_child->ibuf, outfd_child);
442 if (err)
443 return err;
444 pack->child_has_delta_outfd = 1;
447 err = got_privsep_send_raw_delta_req(pack->privsep_child->ibuf,
448 obj_idx, id);
449 if (err)
450 return err;
452 return got_privsep_recv_raw_delta(base_size, result_size, delta_size,
453 delta_compressed_size, delta_offset, delta_out_offset, base_id,
454 pack->privsep_child->ibuf);
457 /*
458 * XXX This function does not really belong in object.c. It is only here
459 * because it needs start_pack_privsep_child(); relevant code should
460 * probably be moved to pack.c/pack_create.c.
461 */
462 const struct got_error *
463 got_object_prepare_delta_reuse(struct got_pack **pack,
464 struct got_packidx *packidx, int delta_outfd, struct got_repository *repo)
466 const struct got_error *err = NULL;
467 char *path_packfile = NULL;
469 err = got_packidx_get_packfile_path(&path_packfile,
470 packidx->path_packidx);
471 if (err)
472 return err;
474 *pack = got_repo_get_cached_pack(repo, path_packfile);
475 if (*pack == NULL) {
476 err = got_repo_cache_pack(pack, repo, path_packfile, packidx);
477 if (err)
478 goto done;
480 if ((*pack)->privsep_child == NULL) {
481 err = start_pack_privsep_child(*pack, packidx);
482 if (err)
483 goto done;
486 if (!(*pack)->child_has_delta_outfd) {
487 int outfd_child;
488 outfd_child = dup(delta_outfd);
489 if (outfd_child == -1) {
490 err = got_error_from_errno("dup");
491 goto done;
493 err = got_privsep_send_raw_delta_outfd(
494 (*pack)->privsep_child->ibuf, outfd_child);
495 if (err)
496 goto done;
497 (*pack)->child_has_delta_outfd = 1;
500 err = got_privsep_send_delta_reuse_req((*pack)->privsep_child->ibuf);
501 done:
502 free(path_packfile);
503 return err;
506 static const struct got_error *
507 request_object(struct got_object **obj, struct got_object_id *id,
508 struct got_repository *repo, int fd)
510 const struct got_error *err = NULL;
511 struct imsgbuf *ibuf;
513 ibuf = repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_OBJECT].ibuf;
515 err = got_privsep_send_obj_req(ibuf, fd, id);
516 if (err)
517 return err;
519 return got_privsep_recv_obj(obj, ibuf);
522 static const struct got_error *
523 request_raw_object(uint8_t **outbuf, off_t *size, size_t *hdrlen, int outfd,
524 struct got_object_id *id, struct got_repository *repo, int infd)
526 const struct got_error *err = NULL;
527 struct imsgbuf *ibuf;
528 int outfd_child;
530 ibuf = repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_OBJECT].ibuf;
532 outfd_child = dup(outfd);
533 if (outfd_child == -1)
534 return got_error_from_errno("dup");
536 err = got_privsep_send_raw_obj_req(ibuf, infd, id);
537 if (err)
538 return err;
540 err = got_privsep_send_raw_obj_outfd(ibuf, outfd_child);
541 if (err)
542 return err;
544 return got_privsep_recv_raw_obj(outbuf, size, hdrlen, ibuf);
547 static const struct got_error *
548 start_read_object_child(struct got_repository *repo)
550 const struct got_error *err = NULL;
551 int imsg_fds[2];
552 pid_t pid;
553 struct imsgbuf *ibuf;
555 ibuf = calloc(1, sizeof(*ibuf));
556 if (ibuf == NULL)
557 return got_error_from_errno("calloc");
559 if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_fds) == -1) {
560 err = got_error_from_errno("socketpair");
561 free(ibuf);
562 return err;
565 pid = fork();
566 if (pid == -1) {
567 err = got_error_from_errno("fork");
568 free(ibuf);
569 return err;
571 else if (pid == 0) {
572 got_privsep_exec_child(imsg_fds, GOT_PATH_PROG_READ_OBJECT,
573 repo->path);
574 /* not reached */
577 if (close(imsg_fds[1]) == -1) {
578 err = got_error_from_errno("close");
579 free(ibuf);
580 return err;
583 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_OBJECT].imsg_fd =
584 imsg_fds[0];
585 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_OBJECT].pid = pid;
586 imsg_init(ibuf, imsg_fds[0]);
587 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_OBJECT].ibuf = ibuf;
589 return NULL;
592 const struct got_error *
593 got_object_read_header_privsep(struct got_object **obj,
594 struct got_object_id *id, struct got_repository *repo, int obj_fd)
596 const struct got_error *err;
598 if (repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_OBJECT].imsg_fd != -1)
599 return request_object(obj, id, repo, obj_fd);
601 err = start_read_object_child(repo);
602 if (err) {
603 close(obj_fd);
604 return err;
607 return request_object(obj, id, repo, obj_fd);
610 static const struct got_error *
611 read_object_raw_privsep(uint8_t **outbuf, off_t *size, size_t *hdrlen,
612 int outfd, struct got_object_id *id, struct got_repository *repo,
613 int obj_fd)
615 const struct got_error *err;
617 if (repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_OBJECT].imsg_fd != -1)
618 return request_raw_object(outbuf, size, hdrlen, outfd, id,
619 repo, obj_fd);
621 err = start_read_object_child(repo);
622 if (err)
623 return err;
625 return request_raw_object(outbuf, size, hdrlen, outfd, id, repo,
626 obj_fd);
629 const struct got_error *
630 got_object_open(struct got_object **obj, struct got_repository *repo,
631 struct got_object_id *id)
633 const struct got_error *err = NULL;
634 int fd;
636 *obj = got_repo_get_cached_object(repo, id);
637 if (*obj != NULL) {
638 (*obj)->refcnt++;
639 return NULL;
642 err = got_object_open_packed(obj, id, repo);
643 if (err && err->code != GOT_ERR_NO_OBJ)
644 return err;
645 if (*obj) {
646 (*obj)->refcnt++;
647 return got_repo_cache_object(repo, id, *obj);
650 err = got_object_open_loose_fd(&fd, id, repo);
651 if (err) {
652 if (err->code == GOT_ERR_ERRNO && errno == ENOENT)
653 err = got_error_no_obj(id);
654 return err;
657 err = got_object_read_header_privsep(obj, id, repo, fd);
658 if (err)
659 return err;
661 memcpy((*obj)->id.sha1, id->sha1, SHA1_DIGEST_LENGTH);
663 (*obj)->refcnt++;
664 return got_repo_cache_object(repo, id, *obj);
667 /* *outfd must be initialized to -1 by caller */
668 const struct got_error *
669 got_object_raw_open(struct got_raw_object **obj, int *outfd,
670 struct got_repository *repo, struct got_object_id *id)
672 const struct got_error *err = NULL;
673 struct got_packidx *packidx = NULL;
674 int idx;
675 uint8_t *outbuf = NULL;
676 off_t size = 0;
677 size_t hdrlen = 0;
678 char *path_packfile = NULL;
680 *obj = got_repo_get_cached_raw_object(repo, id);
681 if (*obj != NULL) {
682 (*obj)->refcnt++;
683 return NULL;
686 if (*outfd == -1) {
687 *outfd = got_opentempfd();
688 if (*outfd == -1)
689 return got_error_from_errno("got_opentempfd");
692 err = got_repo_search_packidx(&packidx, &idx, repo, id);
693 if (err == NULL) {
694 struct got_pack *pack = NULL;
696 err = got_packidx_get_packfile_path(&path_packfile,
697 packidx->path_packidx);
698 if (err)
699 goto done;
701 pack = got_repo_get_cached_pack(repo, path_packfile);
702 if (pack == NULL) {
703 err = got_repo_cache_pack(&pack, repo, path_packfile,
704 packidx);
705 if (err)
706 goto done;
708 err = read_packed_object_raw_privsep(&outbuf, &size, &hdrlen,
709 *outfd, pack, packidx, idx, id);
710 if (err)
711 goto done;
712 } else if (err->code == GOT_ERR_NO_OBJ) {
713 int fd;
715 err = got_object_open_loose_fd(&fd, id, repo);
716 if (err)
717 goto done;
718 err = read_object_raw_privsep(&outbuf, &size, &hdrlen, *outfd,
719 id, repo, fd);
720 if (err)
721 goto done;
724 *obj = calloc(1, sizeof(**obj));
725 if (*obj == NULL) {
726 err = got_error_from_errno("calloc");
727 goto done;
729 (*obj)->fd = -1;
731 if (outbuf) {
732 (*obj)->data = outbuf;
733 } else {
734 struct stat sb;
735 if (fstat(*outfd, &sb) == -1) {
736 err = got_error_from_errno("fstat");
737 goto done;
740 if (sb.st_size != hdrlen + size) {
741 err = got_error(GOT_ERR_PRIVSEP_LEN);
742 goto done;
744 #ifndef GOT_PACK_NO_MMAP
745 if (hdrlen + size > 0) {
746 (*obj)->data = mmap(NULL, hdrlen + size, PROT_READ,
747 MAP_PRIVATE, *outfd, 0);
748 if ((*obj)->data == MAP_FAILED) {
749 if (errno != ENOMEM) {
750 err = got_error_from_errno("mmap");
751 goto done;
753 (*obj)->data = NULL;
754 } else {
755 (*obj)->fd = *outfd;
756 *outfd = -1;
759 #endif
760 if (*outfd != -1) {
761 (*obj)->f = fdopen(*outfd, "r");
762 if ((*obj)->f == NULL) {
763 err = got_error_from_errno("fdopen");
764 goto done;
766 *outfd = -1;
769 (*obj)->hdrlen = hdrlen;
770 (*obj)->size = size;
771 err = got_repo_cache_raw_object(repo, id, *obj);
772 done:
773 free(path_packfile);
774 if (err) {
775 if (*obj) {
776 got_object_raw_close(*obj);
777 *obj = NULL;
779 free(outbuf);
780 } else
781 (*obj)->refcnt++;
782 return err;
785 const struct got_error *
786 got_object_open_by_id_str(struct got_object **obj, struct got_repository *repo,
787 const char *id_str)
789 struct got_object_id id;
791 if (!got_parse_sha1_digest(id.sha1, id_str))
792 return got_error_path(id_str, GOT_ERR_BAD_OBJ_ID_STR);
794 return got_object_open(obj, repo, &id);
797 const struct got_error *
798 got_object_resolve_id_str(struct got_object_id **id,
799 struct got_repository *repo, const char *id_str)
801 const struct got_error *err = NULL;
802 struct got_object *obj;
804 err = got_object_open_by_id_str(&obj, repo, id_str);
805 if (err)
806 return err;
808 *id = got_object_id_dup(got_object_get_id(obj));
809 got_object_close(obj);
810 if (*id == NULL)
811 return got_error_from_errno("got_object_id_dup");
813 return NULL;
816 static const struct got_error *
817 request_packed_commit(struct got_commit_object **commit, struct got_pack *pack,
818 int pack_idx, struct got_object_id *id)
820 const struct got_error *err = NULL;
822 err = got_privsep_send_commit_req(pack->privsep_child->ibuf, -1, id,
823 pack_idx);
824 if (err)
825 return err;
827 err = got_privsep_recv_commit(commit, pack->privsep_child->ibuf);
828 if (err)
829 return err;
831 (*commit)->flags |= GOT_COMMIT_FLAG_PACKED;
832 return NULL;
835 static const struct got_error *
836 read_packed_commit_privsep(struct got_commit_object **commit,
837 struct got_pack *pack, struct got_packidx *packidx, int idx,
838 struct got_object_id *id)
840 const struct got_error *err = NULL;
842 if (pack->privsep_child)
843 return request_packed_commit(commit, pack, idx, id);
845 err = start_pack_privsep_child(pack, packidx);
846 if (err)
847 return err;
849 return request_packed_commit(commit, pack, idx, id);
852 static const struct got_error *
853 request_commit(struct got_commit_object **commit, struct got_repository *repo,
854 int fd, struct got_object_id *id)
856 const struct got_error *err = NULL;
857 struct imsgbuf *ibuf;
859 ibuf = repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_COMMIT].ibuf;
861 err = got_privsep_send_commit_req(ibuf, fd, id, -1);
862 if (err)
863 return err;
865 return got_privsep_recv_commit(commit, ibuf);
868 static const struct got_error *
869 read_commit_privsep(struct got_commit_object **commit, int obj_fd,
870 struct got_object_id *id, struct got_repository *repo)
872 const struct got_error *err;
873 int imsg_fds[2];
874 pid_t pid;
875 struct imsgbuf *ibuf;
877 if (repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_COMMIT].imsg_fd != -1)
878 return request_commit(commit, repo, obj_fd, id);
880 ibuf = calloc(1, sizeof(*ibuf));
881 if (ibuf == NULL)
882 return got_error_from_errno("calloc");
884 if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_fds) == -1) {
885 err = got_error_from_errno("socketpair");
886 free(ibuf);
887 return err;
890 pid = fork();
891 if (pid == -1) {
892 err = got_error_from_errno("fork");
893 free(ibuf);
894 return err;
896 else if (pid == 0) {
897 got_privsep_exec_child(imsg_fds, GOT_PATH_PROG_READ_COMMIT,
898 repo->path);
899 /* not reached */
902 if (close(imsg_fds[1]) == -1) {
903 err = got_error_from_errno("close");
904 free(ibuf);
905 return err;
907 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_COMMIT].imsg_fd =
908 imsg_fds[0];
909 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_COMMIT].pid = pid;
910 imsg_init(ibuf, imsg_fds[0]);
911 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_COMMIT].ibuf = ibuf;
913 return request_commit(commit, repo, obj_fd, id);
917 static const struct got_error *
918 open_commit(struct got_commit_object **commit,
919 struct got_repository *repo, struct got_object_id *id, int check_cache)
921 const struct got_error *err = NULL;
922 struct got_packidx *packidx = NULL;
923 int idx;
924 char *path_packfile = NULL;
926 if (check_cache) {
927 *commit = got_repo_get_cached_commit(repo, id);
928 if (*commit != NULL) {
929 (*commit)->refcnt++;
930 return NULL;
932 } else
933 *commit = NULL;
935 err = got_repo_search_packidx(&packidx, &idx, repo, id);
936 if (err == NULL) {
937 struct got_pack *pack = NULL;
939 err = got_packidx_get_packfile_path(&path_packfile,
940 packidx->path_packidx);
941 if (err)
942 return err;
944 pack = got_repo_get_cached_pack(repo, path_packfile);
945 if (pack == NULL) {
946 err = got_repo_cache_pack(&pack, repo, path_packfile,
947 packidx);
948 if (err)
949 goto done;
951 err = read_packed_commit_privsep(commit, pack,
952 packidx, idx, id);
953 } else if (err->code == GOT_ERR_NO_OBJ) {
954 int fd;
956 err = got_object_open_loose_fd(&fd, id, repo);
957 if (err)
958 return err;
959 err = read_commit_privsep(commit, fd, id, repo);
962 if (err == NULL) {
963 (*commit)->refcnt++;
964 err = got_repo_cache_commit(repo, id, *commit);
966 done:
967 free(path_packfile);
968 return err;
971 const struct got_error *
972 got_object_open_as_commit(struct got_commit_object **commit,
973 struct got_repository *repo, struct got_object_id *id)
975 *commit = got_repo_get_cached_commit(repo, id);
976 if (*commit != NULL) {
977 (*commit)->refcnt++;
978 return NULL;
981 return open_commit(commit, repo, id, 0);
984 const struct got_error *
985 got_object_commit_open(struct got_commit_object **commit,
986 struct got_repository *repo, struct got_object *obj)
988 return open_commit(commit, repo, got_object_get_id(obj), 1);
991 const struct got_error *
992 got_object_qid_alloc(struct got_object_qid **qid, struct got_object_id *id)
994 *qid = calloc(1, sizeof(**qid));
995 if (*qid == NULL)
996 return got_error_from_errno("calloc");
998 memcpy(&(*qid)->id, id, sizeof((*qid)->id));
999 return NULL;
1002 const struct got_error *
1003 got_object_id_queue_copy(const struct got_object_id_queue *src,
1004 struct got_object_id_queue *dest)
1006 const struct got_error *err;
1007 struct got_object_qid *qid;
1009 STAILQ_FOREACH(qid, src, entry) {
1010 struct got_object_qid *new;
1012 * Deep-copy the object ID only. Let the caller deal
1013 * with setting up the new->data pointer if needed.
1015 err = got_object_qid_alloc(&new, &qid->id);
1016 if (err) {
1017 got_object_id_queue_free(dest);
1018 return err;
1020 STAILQ_INSERT_TAIL(dest, new, entry);
1023 return NULL;
1026 static const struct got_error *
1027 request_packed_tree(struct got_tree_object **tree, struct got_pack *pack,
1028 int pack_idx, struct got_object_id *id)
1030 const struct got_error *err = NULL;
1032 err = got_privsep_send_tree_req(pack->privsep_child->ibuf, -1, id,
1033 pack_idx);
1034 if (err)
1035 return err;
1037 return got_privsep_recv_tree(tree, pack->privsep_child->ibuf);
1040 static const struct got_error *
1041 read_packed_tree_privsep(struct got_tree_object **tree,
1042 struct got_pack *pack, struct got_packidx *packidx, int idx,
1043 struct got_object_id *id)
1045 const struct got_error *err = NULL;
1047 if (pack->privsep_child)
1048 return request_packed_tree(tree, pack, idx, id);
1050 err = start_pack_privsep_child(pack, packidx);
1051 if (err)
1052 return err;
1054 return request_packed_tree(tree, pack, idx, id);
1057 static const struct got_error *
1058 request_tree(struct got_tree_object **tree, struct got_repository *repo,
1059 int fd, struct got_object_id *id)
1061 const struct got_error *err = NULL;
1062 struct imsgbuf *ibuf;
1064 ibuf = repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TREE].ibuf;
1066 err = got_privsep_send_tree_req(ibuf, fd, id, -1);
1067 if (err)
1068 return err;
1070 return got_privsep_recv_tree(tree, ibuf);
1073 const struct got_error *
1074 read_tree_privsep(struct got_tree_object **tree, int obj_fd,
1075 struct got_object_id *id, struct got_repository *repo)
1077 const struct got_error *err;
1078 int imsg_fds[2];
1079 pid_t pid;
1080 struct imsgbuf *ibuf;
1082 if (repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TREE].imsg_fd != -1)
1083 return request_tree(tree, repo, obj_fd, id);
1085 ibuf = calloc(1, sizeof(*ibuf));
1086 if (ibuf == NULL)
1087 return got_error_from_errno("calloc");
1089 if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_fds) == -1) {
1090 err = got_error_from_errno("socketpair");
1091 free(ibuf);
1092 return err;
1095 pid = fork();
1096 if (pid == -1) {
1097 err = got_error_from_errno("fork");
1098 free(ibuf);
1099 return err;
1101 else if (pid == 0) {
1102 got_privsep_exec_child(imsg_fds, GOT_PATH_PROG_READ_TREE,
1103 repo->path);
1104 /* not reached */
1107 if (close(imsg_fds[1]) == -1) {
1108 err = got_error_from_errno("close");
1109 free(ibuf);
1110 return err;
1112 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TREE].imsg_fd =
1113 imsg_fds[0];
1114 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TREE].pid = pid;
1115 imsg_init(ibuf, imsg_fds[0]);
1116 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TREE].ibuf = ibuf;
1119 return request_tree(tree, repo, obj_fd, id);
1122 static const struct got_error *
1123 open_tree(struct got_tree_object **tree, struct got_repository *repo,
1124 struct got_object_id *id, int check_cache)
1126 const struct got_error *err = NULL;
1127 struct got_packidx *packidx = NULL;
1128 int idx;
1129 char *path_packfile = NULL;
1131 if (check_cache) {
1132 *tree = got_repo_get_cached_tree(repo, id);
1133 if (*tree != NULL) {
1134 (*tree)->refcnt++;
1135 return NULL;
1137 } else
1138 *tree = NULL;
1140 err = got_repo_search_packidx(&packidx, &idx, repo, id);
1141 if (err == NULL) {
1142 struct got_pack *pack = NULL;
1144 err = got_packidx_get_packfile_path(&path_packfile,
1145 packidx->path_packidx);
1146 if (err)
1147 return err;
1149 pack = got_repo_get_cached_pack(repo, path_packfile);
1150 if (pack == NULL) {
1151 err = got_repo_cache_pack(&pack, repo, path_packfile,
1152 packidx);
1153 if (err)
1154 goto done;
1156 err = read_packed_tree_privsep(tree, pack,
1157 packidx, idx, id);
1158 } else if (err->code == GOT_ERR_NO_OBJ) {
1159 int fd;
1161 err = got_object_open_loose_fd(&fd, id, repo);
1162 if (err)
1163 return err;
1164 err = read_tree_privsep(tree, fd, id, repo);
1167 if (err == NULL) {
1168 (*tree)->refcnt++;
1169 err = got_repo_cache_tree(repo, id, *tree);
1171 done:
1172 free(path_packfile);
1173 return err;
1176 const struct got_error *
1177 got_object_open_as_tree(struct got_tree_object **tree,
1178 struct got_repository *repo, struct got_object_id *id)
1180 *tree = got_repo_get_cached_tree(repo, id);
1181 if (*tree != NULL) {
1182 (*tree)->refcnt++;
1183 return NULL;
1186 return open_tree(tree, repo, id, 0);
1189 const struct got_error *
1190 got_object_tree_open(struct got_tree_object **tree,
1191 struct got_repository *repo, struct got_object *obj)
1193 return open_tree(tree, repo, got_object_get_id(obj), 1);
1196 int
1197 got_object_tree_get_nentries(struct got_tree_object *tree)
1199 return tree->nentries;
1202 struct got_tree_entry *
1203 got_object_tree_get_first_entry(struct got_tree_object *tree)
1205 return got_object_tree_get_entry(tree, 0);
1208 struct got_tree_entry *
1209 got_object_tree_get_last_entry(struct got_tree_object *tree)
1211 return got_object_tree_get_entry(tree, tree->nentries - 1);
1214 struct got_tree_entry *
1215 got_object_tree_get_entry(struct got_tree_object *tree, int i)
1217 if (i < 0 || i >= tree->nentries)
1218 return NULL;
1219 return &tree->entries[i];
1222 mode_t
1223 got_tree_entry_get_mode(struct got_tree_entry *te)
1225 return te->mode;
1228 const char *
1229 got_tree_entry_get_name(struct got_tree_entry *te)
1231 return &te->name[0];
1234 struct got_object_id *
1235 got_tree_entry_get_id(struct got_tree_entry *te)
1237 return &te->id;
1240 const struct got_error *
1241 got_object_blob_read_to_str(char **s, struct got_blob_object *blob)
1243 const struct got_error *err = NULL;
1244 size_t len, totlen, hdrlen, offset;
1246 *s = NULL;
1248 hdrlen = got_object_blob_get_hdrlen(blob);
1249 totlen = 0;
1250 offset = 0;
1251 do {
1252 char *p;
1254 err = got_object_blob_read_block(&len, blob);
1255 if (err)
1256 return err;
1258 if (len == 0)
1259 break;
1261 totlen += len - hdrlen;
1262 p = realloc(*s, totlen + 1);
1263 if (p == NULL) {
1264 err = got_error_from_errno("realloc");
1265 free(*s);
1266 *s = NULL;
1267 return err;
1269 *s = p;
1270 /* Skip blob object header first time around. */
1271 memcpy(*s + offset,
1272 got_object_blob_get_read_buf(blob) + hdrlen, len - hdrlen);
1273 hdrlen = 0;
1274 offset = totlen;
1275 } while (len > 0);
1277 (*s)[totlen] = '\0';
1278 return NULL;
1281 const struct got_error *
1282 got_tree_entry_get_symlink_target(char **link_target, struct got_tree_entry *te,
1283 struct got_repository *repo)
1285 const struct got_error *err = NULL;
1286 struct got_blob_object *blob = NULL;
1288 *link_target = NULL;
1290 if (!got_object_tree_entry_is_symlink(te))
1291 return got_error(GOT_ERR_TREE_ENTRY_TYPE);
1293 err = got_object_open_as_blob(&blob, repo,
1294 got_tree_entry_get_id(te), PATH_MAX);
1295 if (err)
1296 return err;
1298 err = got_object_blob_read_to_str(link_target, blob);
1299 got_object_blob_close(blob);
1300 if (err) {
1301 free(*link_target);
1302 *link_target = NULL;
1304 return err;
1307 int
1308 got_tree_entry_get_index(struct got_tree_entry *te)
1310 return te->idx;
1313 struct got_tree_entry *
1314 got_tree_entry_get_next(struct got_tree_object *tree,
1315 struct got_tree_entry *te)
1317 return got_object_tree_get_entry(tree, te->idx + 1);
1320 struct got_tree_entry *
1321 got_tree_entry_get_prev(struct got_tree_object *tree,
1322 struct got_tree_entry *te)
1324 return got_object_tree_get_entry(tree, te->idx - 1);
1327 static const struct got_error *
1328 request_packed_blob(uint8_t **outbuf, size_t *size, size_t *hdrlen, int outfd,
1329 struct got_pack *pack, struct got_packidx *packidx, int idx,
1330 struct got_object_id *id)
1332 const struct got_error *err = NULL;
1333 struct imsgbuf *ibuf = pack->privsep_child->ibuf;
1334 int outfd_child;
1336 err = pack_child_send_tempfiles(ibuf, pack);
1337 if (err)
1338 return err;
1340 outfd_child = dup(outfd);
1341 if (outfd_child == -1)
1342 return got_error_from_errno("dup");
1344 err = got_privsep_send_blob_req(pack->privsep_child->ibuf, -1, id, idx);
1345 if (err)
1346 return err;
1348 err = got_privsep_send_blob_outfd(pack->privsep_child->ibuf,
1349 outfd_child);
1350 if (err) {
1351 return err;
1354 err = got_privsep_recv_blob(outbuf, size, hdrlen,
1355 pack->privsep_child->ibuf);
1356 if (err)
1357 return err;
1359 if (lseek(outfd, SEEK_SET, 0) == -1)
1360 err = got_error_from_errno("lseek");
1362 return err;
1365 static const struct got_error *
1366 read_packed_blob_privsep(uint8_t **outbuf, size_t *size, size_t *hdrlen,
1367 int outfd, struct got_pack *pack, struct got_packidx *packidx, int idx,
1368 struct got_object_id *id)
1370 const struct got_error *err = NULL;
1372 if (pack->privsep_child == NULL) {
1373 err = start_pack_privsep_child(pack, packidx);
1374 if (err)
1375 return err;
1378 return request_packed_blob(outbuf, size, hdrlen, outfd, pack, packidx,
1379 idx, id);
1382 static const struct got_error *
1383 request_blob(uint8_t **outbuf, size_t *size, size_t *hdrlen, int outfd,
1384 int infd, struct got_object_id *id, struct imsgbuf *ibuf)
1386 const struct got_error *err = NULL;
1387 int outfd_child;
1389 outfd_child = dup(outfd);
1390 if (outfd_child == -1)
1391 return got_error_from_errno("dup");
1393 err = got_privsep_send_blob_req(ibuf, infd, id, -1);
1394 if (err)
1395 return err;
1397 err = got_privsep_send_blob_outfd(ibuf, outfd_child);
1398 if (err)
1399 return err;
1401 err = got_privsep_recv_blob(outbuf, size, hdrlen, ibuf);
1402 if (err)
1403 return err;
1405 if (lseek(outfd, SEEK_SET, 0) == -1)
1406 return got_error_from_errno("lseek");
1408 return err;
1411 static const struct got_error *
1412 read_blob_privsep(uint8_t **outbuf, size_t *size, size_t *hdrlen,
1413 int outfd, int infd, struct got_object_id *id, struct got_repository *repo)
1415 const struct got_error *err;
1416 int imsg_fds[2];
1417 pid_t pid;
1418 struct imsgbuf *ibuf;
1420 if (repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_BLOB].imsg_fd != -1) {
1421 ibuf = repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_BLOB].ibuf;
1422 return request_blob(outbuf, size, hdrlen, outfd, infd, id,
1423 ibuf);
1426 ibuf = calloc(1, sizeof(*ibuf));
1427 if (ibuf == NULL)
1428 return got_error_from_errno("calloc");
1430 if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_fds) == -1) {
1431 err = got_error_from_errno("socketpair");
1432 free(ibuf);
1433 return err;
1436 pid = fork();
1437 if (pid == -1) {
1438 err = got_error_from_errno("fork");
1439 free(ibuf);
1440 return err;
1442 else if (pid == 0) {
1443 got_privsep_exec_child(imsg_fds, GOT_PATH_PROG_READ_BLOB,
1444 repo->path);
1445 /* not reached */
1448 if (close(imsg_fds[1]) == -1) {
1449 err = got_error_from_errno("close");
1450 free(ibuf);
1451 return err;
1453 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_BLOB].imsg_fd =
1454 imsg_fds[0];
1455 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_BLOB].pid = pid;
1456 imsg_init(ibuf, imsg_fds[0]);
1457 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_BLOB].ibuf = ibuf;
1459 return request_blob(outbuf, size, hdrlen, outfd, infd, id, ibuf);
1462 static const struct got_error *
1463 open_blob(struct got_blob_object **blob, struct got_repository *repo,
1464 struct got_object_id *id, size_t blocksize)
1466 const struct got_error *err = NULL;
1467 struct got_packidx *packidx = NULL;
1468 int idx;
1469 char *path_packfile = NULL;
1470 uint8_t *outbuf;
1471 int outfd;
1472 size_t size, hdrlen;
1473 struct stat sb;
1475 *blob = calloc(1, sizeof(**blob));
1476 if (*blob == NULL)
1477 return got_error_from_errno("calloc");
1479 outfd = got_opentempfd();
1480 if (outfd == -1)
1481 return got_error_from_errno("got_opentempfd");
1483 (*blob)->read_buf = malloc(blocksize);
1484 if ((*blob)->read_buf == NULL) {
1485 err = got_error_from_errno("malloc");
1486 goto done;
1489 err = got_repo_search_packidx(&packidx, &idx, repo, id);
1490 if (err == NULL) {
1491 struct got_pack *pack = NULL;
1493 err = got_packidx_get_packfile_path(&path_packfile,
1494 packidx->path_packidx);
1495 if (err)
1496 goto done;
1498 pack = got_repo_get_cached_pack(repo, path_packfile);
1499 if (pack == NULL) {
1500 err = got_repo_cache_pack(&pack, repo, path_packfile,
1501 packidx);
1502 if (err)
1503 goto done;
1505 err = read_packed_blob_privsep(&outbuf, &size, &hdrlen, outfd,
1506 pack, packidx, idx, id);
1507 } else if (err->code == GOT_ERR_NO_OBJ) {
1508 int infd;
1510 err = got_object_open_loose_fd(&infd, id, repo);
1511 if (err)
1512 goto done;
1513 err = read_blob_privsep(&outbuf, &size, &hdrlen, outfd, infd,
1514 id, repo);
1516 if (err)
1517 goto done;
1519 if (hdrlen > size) {
1520 err = got_error(GOT_ERR_BAD_OBJ_HDR);
1521 goto done;
1524 if (outbuf) {
1525 if (close(outfd) == -1 && err == NULL)
1526 err = got_error_from_errno("close");
1527 outfd = -1;
1528 (*blob)->f = fmemopen(outbuf, size, "rb");
1529 if ((*blob)->f == NULL) {
1530 err = got_error_from_errno("fmemopen");
1531 free(outbuf);
1532 goto done;
1534 (*blob)->data = outbuf;
1535 } else {
1536 if (fstat(outfd, &sb) == -1) {
1537 err = got_error_from_errno("fstat");
1538 goto done;
1541 if (sb.st_size != size) {
1542 err = got_error(GOT_ERR_PRIVSEP_LEN);
1543 goto done;
1546 (*blob)->f = fdopen(outfd, "rb");
1547 if ((*blob)->f == NULL) {
1548 err = got_error_from_errno("fdopen");
1549 close(outfd);
1550 outfd = -1;
1551 goto done;
1555 (*blob)->hdrlen = hdrlen;
1556 (*blob)->blocksize = blocksize;
1557 memcpy(&(*blob)->id.sha1, id->sha1, SHA1_DIGEST_LENGTH);
1559 done:
1560 free(path_packfile);
1561 if (err) {
1562 if (*blob) {
1563 got_object_blob_close(*blob);
1564 *blob = NULL;
1565 } else if (outfd != -1)
1566 close(outfd);
1568 return err;
1571 const struct got_error *
1572 got_object_open_as_blob(struct got_blob_object **blob,
1573 struct got_repository *repo, struct got_object_id *id,
1574 size_t blocksize)
1576 return open_blob(blob, repo, id, blocksize);
1579 const struct got_error *
1580 got_object_blob_open(struct got_blob_object **blob,
1581 struct got_repository *repo, struct got_object *obj, size_t blocksize)
1583 return open_blob(blob, repo, got_object_get_id(obj), blocksize);
1586 const struct got_error *
1587 got_object_blob_close(struct got_blob_object *blob)
1589 const struct got_error *err = NULL;
1590 free(blob->read_buf);
1591 if (blob->f && fclose(blob->f) == EOF)
1592 err = got_error_from_errno("fclose");
1593 free(blob->data);
1594 free(blob);
1595 return err;
1598 void
1599 got_object_blob_rewind(struct got_blob_object *blob)
1601 if (blob->f)
1602 rewind(blob->f);
1605 char *
1606 got_object_blob_id_str(struct got_blob_object *blob, char *buf, size_t size)
1608 return got_sha1_digest_to_str(blob->id.sha1, buf, size);
1611 size_t
1612 got_object_blob_get_hdrlen(struct got_blob_object *blob)
1614 return blob->hdrlen;
1617 const uint8_t *
1618 got_object_blob_get_read_buf(struct got_blob_object *blob)
1620 return blob->read_buf;
1623 const struct got_error *
1624 got_object_blob_read_block(size_t *outlenp, struct got_blob_object *blob)
1626 size_t n;
1628 n = fread(blob->read_buf, 1, blob->blocksize, blob->f);
1629 if (n == 0 && ferror(blob->f))
1630 return got_ferror(blob->f, GOT_ERR_IO);
1631 *outlenp = n;
1632 return NULL;
1635 const struct got_error *
1636 got_object_blob_dump_to_file(off_t *filesize, int *nlines,
1637 off_t **line_offsets, FILE *outfile, struct got_blob_object *blob)
1639 const struct got_error *err = NULL;
1640 size_t n, len, hdrlen;
1641 const uint8_t *buf;
1642 int i;
1643 const int alloc_chunksz = 512;
1644 size_t nalloc = 0;
1645 off_t off = 0, total_len = 0;
1647 if (line_offsets)
1648 *line_offsets = NULL;
1649 if (filesize)
1650 *filesize = 0;
1651 if (nlines)
1652 *nlines = 0;
1654 hdrlen = got_object_blob_get_hdrlen(blob);
1655 do {
1656 err = got_object_blob_read_block(&len, blob);
1657 if (err)
1658 return err;
1659 if (len == 0)
1660 break;
1661 buf = got_object_blob_get_read_buf(blob);
1662 i = hdrlen;
1663 if (nlines) {
1664 if (line_offsets && *line_offsets == NULL) {
1665 /* Have some data but perhaps no '\n'. */
1666 *nlines = 1;
1667 nalloc = alloc_chunksz;
1668 *line_offsets = calloc(nalloc,
1669 sizeof(**line_offsets));
1670 if (*line_offsets == NULL)
1671 return got_error_from_errno("calloc");
1673 /* Skip forward over end of first line. */
1674 while (i < len) {
1675 if (buf[i] == '\n')
1676 break;
1677 i++;
1680 /* Scan '\n' offsets in remaining chunk of data. */
1681 while (i < len) {
1682 if (buf[i] != '\n') {
1683 i++;
1684 continue;
1686 (*nlines)++;
1687 if (line_offsets && nalloc < *nlines) {
1688 size_t n = *nlines + alloc_chunksz;
1689 off_t *o = recallocarray(*line_offsets,
1690 nalloc, n, sizeof(**line_offsets));
1691 if (o == NULL) {
1692 free(*line_offsets);
1693 *line_offsets = NULL;
1694 return got_error_from_errno(
1695 "recallocarray");
1697 *line_offsets = o;
1698 nalloc = n;
1700 if (line_offsets) {
1701 off = total_len + i - hdrlen + 1;
1702 (*line_offsets)[*nlines - 1] = off;
1704 i++;
1707 /* Skip blob object header first time around. */
1708 n = fwrite(buf + hdrlen, 1, len - hdrlen, outfile);
1709 if (n != len - hdrlen)
1710 return got_ferror(outfile, GOT_ERR_IO);
1711 total_len += len - hdrlen;
1712 hdrlen = 0;
1713 } while (len != 0);
1715 if (fflush(outfile) != 0)
1716 return got_error_from_errno("fflush");
1717 rewind(outfile);
1719 if (filesize)
1720 *filesize = total_len;
1722 return NULL;
1725 static const struct got_error *
1726 request_packed_tag(struct got_tag_object **tag, struct got_pack *pack,
1727 int pack_idx, struct got_object_id *id)
1729 const struct got_error *err = NULL;
1731 err = got_privsep_send_tag_req(pack->privsep_child->ibuf, -1, id,
1732 pack_idx);
1733 if (err)
1734 return err;
1736 return got_privsep_recv_tag(tag, pack->privsep_child->ibuf);
1739 static const struct got_error *
1740 read_packed_tag_privsep(struct got_tag_object **tag,
1741 struct got_pack *pack, struct got_packidx *packidx, int idx,
1742 struct got_object_id *id)
1744 const struct got_error *err = NULL;
1746 if (pack->privsep_child)
1747 return request_packed_tag(tag, pack, idx, id);
1749 err = start_pack_privsep_child(pack, packidx);
1750 if (err)
1751 return err;
1753 return request_packed_tag(tag, pack, idx, id);
1756 static const struct got_error *
1757 request_tag(struct got_tag_object **tag, struct got_repository *repo,
1758 int fd, struct got_object_id *id)
1760 const struct got_error *err = NULL;
1761 struct imsgbuf *ibuf;
1763 ibuf = repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TAG].ibuf;
1765 err = got_privsep_send_tag_req(ibuf, fd, id, -1);
1766 if (err)
1767 return err;
1769 return got_privsep_recv_tag(tag, ibuf);
1772 static const struct got_error *
1773 read_tag_privsep(struct got_tag_object **tag, int obj_fd,
1774 struct got_object_id *id, struct got_repository *repo)
1776 const struct got_error *err;
1777 int imsg_fds[2];
1778 pid_t pid;
1779 struct imsgbuf *ibuf;
1781 if (repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TAG].imsg_fd != -1)
1782 return request_tag(tag, repo, obj_fd, id);
1784 ibuf = calloc(1, sizeof(*ibuf));
1785 if (ibuf == NULL)
1786 return got_error_from_errno("calloc");
1788 if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_fds) == -1) {
1789 err = got_error_from_errno("socketpair");
1790 free(ibuf);
1791 return err;
1794 pid = fork();
1795 if (pid == -1) {
1796 err = got_error_from_errno("fork");
1797 free(ibuf);
1798 return err;
1800 else if (pid == 0) {
1801 got_privsep_exec_child(imsg_fds, GOT_PATH_PROG_READ_TAG,
1802 repo->path);
1803 /* not reached */
1806 if (close(imsg_fds[1]) == -1) {
1807 err = got_error_from_errno("close");
1808 free(ibuf);
1809 return err;
1811 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TAG].imsg_fd =
1812 imsg_fds[0];
1813 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TAG].pid = pid;
1814 imsg_init(ibuf, imsg_fds[0]);
1815 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TAG].ibuf = ibuf;
1817 return request_tag(tag, repo, obj_fd, id);
1820 static const struct got_error *
1821 open_tag(struct got_tag_object **tag, struct got_repository *repo,
1822 struct got_object_id *id, int check_cache)
1824 const struct got_error *err = NULL;
1825 struct got_packidx *packidx = NULL;
1826 int idx;
1827 char *path_packfile = NULL;
1828 struct got_object *obj = NULL;
1829 int obj_type = GOT_OBJ_TYPE_ANY;
1831 if (check_cache) {
1832 *tag = got_repo_get_cached_tag(repo, id);
1833 if (*tag != NULL) {
1834 (*tag)->refcnt++;
1835 return NULL;
1837 } else
1838 *tag = NULL;
1840 err = got_repo_search_packidx(&packidx, &idx, repo, id);
1841 if (err == NULL) {
1842 struct got_pack *pack = NULL;
1844 err = got_packidx_get_packfile_path(&path_packfile,
1845 packidx->path_packidx);
1846 if (err)
1847 return err;
1849 pack = got_repo_get_cached_pack(repo, path_packfile);
1850 if (pack == NULL) {
1851 err = got_repo_cache_pack(&pack, repo, path_packfile,
1852 packidx);
1853 if (err)
1854 goto done;
1857 /* Beware of "lightweight" tags: Check object type first. */
1858 err = read_packed_object_privsep(&obj, repo, pack, packidx,
1859 idx, id);
1860 if (err)
1861 goto done;
1862 obj_type = obj->type;
1863 got_object_close(obj);
1864 if (obj_type != GOT_OBJ_TYPE_TAG) {
1865 err = got_error(GOT_ERR_OBJ_TYPE);
1866 goto done;
1868 err = read_packed_tag_privsep(tag, pack, packidx, idx, id);
1869 } else if (err->code == GOT_ERR_NO_OBJ) {
1870 int fd;
1872 err = got_object_open_loose_fd(&fd, id, repo);
1873 if (err)
1874 return err;
1875 err = got_object_read_header_privsep(&obj, id, repo, fd);
1876 if (err)
1877 return err;
1878 obj_type = obj->type;
1879 got_object_close(obj);
1880 if (obj_type != GOT_OBJ_TYPE_TAG)
1881 return got_error(GOT_ERR_OBJ_TYPE);
1883 err = got_object_open_loose_fd(&fd, id, repo);
1884 if (err)
1885 return err;
1886 err = read_tag_privsep(tag, fd, id, repo);
1889 if (err == NULL) {
1890 (*tag)->refcnt++;
1891 err = got_repo_cache_tag(repo, id, *tag);
1893 done:
1894 free(path_packfile);
1895 return err;
1898 const struct got_error *
1899 got_object_open_as_tag(struct got_tag_object **tag,
1900 struct got_repository *repo, struct got_object_id *id)
1902 *tag = got_repo_get_cached_tag(repo, id);
1903 if (*tag != NULL) {
1904 (*tag)->refcnt++;
1905 return NULL;
1908 return open_tag(tag, repo, id, 0);
1911 const struct got_error *
1912 got_object_tag_open(struct got_tag_object **tag,
1913 struct got_repository *repo, struct got_object *obj)
1915 return open_tag(tag, repo, got_object_get_id(obj), 1);
1918 const char *
1919 got_object_tag_get_name(struct got_tag_object *tag)
1921 return tag->tag;
1924 int
1925 got_object_tag_get_object_type(struct got_tag_object *tag)
1927 return tag->obj_type;
1930 struct got_object_id *
1931 got_object_tag_get_object_id(struct got_tag_object *tag)
1933 return &tag->id;
1936 time_t
1937 got_object_tag_get_tagger_time(struct got_tag_object *tag)
1939 return tag->tagger_time;
1942 time_t
1943 got_object_tag_get_tagger_gmtoff(struct got_tag_object *tag)
1945 return tag->tagger_gmtoff;
1948 const char *
1949 got_object_tag_get_tagger(struct got_tag_object *tag)
1951 return tag->tagger;
1954 const char *
1955 got_object_tag_get_message(struct got_tag_object *tag)
1957 return tag->tagmsg;
1960 static struct got_tree_entry *
1961 find_entry_by_name(struct got_tree_object *tree, const char *name, size_t len)
1963 int i;
1965 /* Note that tree entries are sorted in strncmp() order. */
1966 for (i = 0; i < tree->nentries; i++) {
1967 struct got_tree_entry *te = &tree->entries[i];
1968 int cmp = strncmp(te->name, name, len);
1969 if (cmp < 0)
1970 continue;
1971 if (cmp > 0)
1972 break;
1973 if (te->name[len] == '\0')
1974 return te;
1976 return NULL;
1979 struct got_tree_entry *
1980 got_object_tree_find_entry(struct got_tree_object *tree, const char *name)
1982 return find_entry_by_name(tree, name, strlen(name));
1985 const struct got_error *
1986 got_object_tree_find_path(struct got_object_id **id, mode_t *mode,
1987 struct got_repository *repo, struct got_tree_object *tree,
1988 const char *path)
1990 const struct got_error *err = NULL;
1991 struct got_tree_object *subtree = NULL;
1992 struct got_tree_entry *te = NULL;
1993 const char *seg, *s;
1994 size_t seglen;
1996 *id = NULL;
1998 s = path;
1999 while (s[0] == '/')
2000 s++;
2001 seg = s;
2002 seglen = 0;
2003 subtree = tree;
2004 while (*s) {
2005 struct got_tree_object *next_tree;
2007 if (*s != '/') {
2008 s++;
2009 seglen++;
2010 if (*s)
2011 continue;
2014 te = find_entry_by_name(subtree, seg, seglen);
2015 if (te == NULL) {
2016 err = got_error_path(path, GOT_ERR_NO_TREE_ENTRY);
2017 goto done;
2020 if (*s == '\0')
2021 break;
2023 seg = s + 1;
2024 seglen = 0;
2025 s++;
2026 if (*s) {
2027 err = got_object_open_as_tree(&next_tree, repo,
2028 &te->id);
2029 te = NULL;
2030 if (err)
2031 goto done;
2032 if (subtree != tree)
2033 got_object_tree_close(subtree);
2034 subtree = next_tree;
2038 if (te) {
2039 *id = got_object_id_dup(&te->id);
2040 if (*id == NULL)
2041 return got_error_from_errno("got_object_id_dup");
2042 if (mode)
2043 *mode = te->mode;
2044 } else
2045 err = got_error_path(path, GOT_ERR_NO_TREE_ENTRY);
2046 done:
2047 if (subtree && subtree != tree)
2048 got_object_tree_close(subtree);
2049 return err;
2051 const struct got_error *
2052 got_object_id_by_path(struct got_object_id **id, struct got_repository *repo,
2053 struct got_commit_object *commit, const char *path)
2055 const struct got_error *err = NULL;
2056 struct got_tree_object *tree = NULL;
2058 *id = NULL;
2060 /* Handle opening of root of commit's tree. */
2061 if (got_path_is_root_dir(path)) {
2062 *id = got_object_id_dup(commit->tree_id);
2063 if (*id == NULL)
2064 err = got_error_from_errno("got_object_id_dup");
2065 } else {
2066 err = got_object_open_as_tree(&tree, repo, commit->tree_id);
2067 if (err)
2068 goto done;
2069 err = got_object_tree_find_path(id, NULL, repo, tree, path);
2071 done:
2072 if (tree)
2073 got_object_tree_close(tree);
2074 return err;
2078 * Normalize file mode bits to avoid false positive tree entry differences
2079 * in case tree entries have unexpected mode bits set.
2081 static mode_t
2082 normalize_mode_for_comparison(mode_t mode)
2085 * For directories, the only relevant bit is the IFDIR bit.
2086 * This allows us to detect paths changing from a directory
2087 * to a file and vice versa.
2089 if (S_ISDIR(mode))
2090 return mode & S_IFDIR;
2093 * For symlinks, the only relevant bit is the IFLNK bit.
2094 * This allows us to detect paths changing from a symlinks
2095 * to a file or directory and vice versa.
2097 if (S_ISLNK(mode))
2098 return mode & S_IFLNK;
2100 /* For files, the only change we care about is the executable bit. */
2101 return mode & S_IXUSR;
2104 const struct got_error *
2105 got_object_tree_path_changed(int *changed,
2106 struct got_tree_object *tree01, struct got_tree_object *tree02,
2107 const char *path, struct got_repository *repo)
2109 const struct got_error *err = NULL;
2110 struct got_tree_object *tree1 = NULL, *tree2 = NULL;
2111 struct got_tree_entry *te1 = NULL, *te2 = NULL;
2112 const char *seg, *s;
2113 size_t seglen;
2115 *changed = 0;
2117 /* We not do support comparing the root path. */
2118 if (got_path_is_root_dir(path))
2119 return got_error_path(path, GOT_ERR_BAD_PATH);
2121 tree1 = tree01;
2122 tree2 = tree02;
2123 s = path;
2124 while (*s == '/')
2125 s++;
2126 seg = s;
2127 seglen = 0;
2128 while (*s) {
2129 struct got_tree_object *next_tree1, *next_tree2;
2130 mode_t mode1, mode2;
2132 if (*s != '/') {
2133 s++;
2134 seglen++;
2135 if (*s)
2136 continue;
2139 te1 = find_entry_by_name(tree1, seg, seglen);
2140 if (te1 == NULL) {
2141 err = got_error(GOT_ERR_NO_OBJ);
2142 goto done;
2145 if (tree2)
2146 te2 = find_entry_by_name(tree2, seg, seglen);
2148 if (te2) {
2149 mode1 = normalize_mode_for_comparison(te1->mode);
2150 mode2 = normalize_mode_for_comparison(te2->mode);
2151 if (mode1 != mode2) {
2152 *changed = 1;
2153 goto done;
2156 if (got_object_id_cmp(&te1->id, &te2->id) == 0) {
2157 *changed = 0;
2158 goto done;
2162 if (*s == '\0') { /* final path element */
2163 *changed = 1;
2164 goto done;
2167 seg = s + 1;
2168 s++;
2169 seglen = 0;
2170 if (*s) {
2171 err = got_object_open_as_tree(&next_tree1, repo,
2172 &te1->id);
2173 te1 = NULL;
2174 if (err)
2175 goto done;
2176 if (tree1 != tree01)
2177 got_object_tree_close(tree1);
2178 tree1 = next_tree1;
2180 if (te2) {
2181 err = got_object_open_as_tree(&next_tree2, repo,
2182 &te2->id);
2183 te2 = NULL;
2184 if (err)
2185 goto done;
2186 if (tree2 != tree02)
2187 got_object_tree_close(tree2);
2188 tree2 = next_tree2;
2189 } else if (tree2) {
2190 if (tree2 != tree02)
2191 got_object_tree_close(tree2);
2192 tree2 = NULL;
2196 done:
2197 if (tree1 && tree1 != tree01)
2198 got_object_tree_close(tree1);
2199 if (tree2 && tree2 != tree02)
2200 got_object_tree_close(tree2);
2201 return err;
2204 const struct got_error *
2205 got_object_tree_entry_dup(struct got_tree_entry **new_te,
2206 struct got_tree_entry *te)
2208 const struct got_error *err = NULL;
2210 *new_te = calloc(1, sizeof(**new_te));
2211 if (*new_te == NULL)
2212 return got_error_from_errno("calloc");
2214 (*new_te)->mode = te->mode;
2215 memcpy((*new_te)->name, te->name, sizeof((*new_te)->name));
2216 memcpy(&(*new_te)->id, &te->id, sizeof((*new_te)->id));
2217 return err;
2220 int
2221 got_object_tree_entry_is_submodule(struct got_tree_entry *te)
2223 return (te->mode & S_IFMT) == (S_IFDIR | S_IFLNK);
2226 int
2227 got_object_tree_entry_is_symlink(struct got_tree_entry *te)
2229 /* S_IFDIR check avoids confusing symlinks with submodules. */
2230 return ((te->mode & (S_IFDIR | S_IFLNK)) == S_IFLNK);
2233 static const struct got_error *
2234 resolve_symlink(char **link_target, const char *path,
2235 struct got_commit_object *commit, struct got_repository *repo)
2237 const struct got_error *err = NULL;
2238 char buf[PATH_MAX];
2239 char *name, *parent_path = NULL;
2240 struct got_object_id *tree_obj_id = NULL;
2241 struct got_tree_object *tree = NULL;
2242 struct got_tree_entry *te = NULL;
2244 *link_target = NULL;
2246 if (strlcpy(buf, path, sizeof(buf)) >= sizeof(buf))
2247 return got_error(GOT_ERR_NO_SPACE);
2249 name = basename(buf);
2250 if (name == NULL)
2251 return got_error_from_errno2("basename", path);
2253 err = got_path_dirname(&parent_path, path);
2254 if (err)
2255 return err;
2257 err = got_object_id_by_path(&tree_obj_id, repo, commit,
2258 parent_path);
2259 if (err) {
2260 if (err->code == GOT_ERR_NO_TREE_ENTRY) {
2261 /* Display the complete path in error message. */
2262 err = got_error_path(path, err->code);
2264 goto done;
2267 err = got_object_open_as_tree(&tree, repo, tree_obj_id);
2268 if (err)
2269 goto done;
2271 te = got_object_tree_find_entry(tree, name);
2272 if (te == NULL) {
2273 err = got_error_path(path, GOT_ERR_NO_TREE_ENTRY);
2274 goto done;
2277 if (got_object_tree_entry_is_symlink(te)) {
2278 err = got_tree_entry_get_symlink_target(link_target, te, repo);
2279 if (err)
2280 goto done;
2281 if (!got_path_is_absolute(*link_target)) {
2282 char *abspath;
2283 if (asprintf(&abspath, "%s/%s", parent_path,
2284 *link_target) == -1) {
2285 err = got_error_from_errno("asprintf");
2286 goto done;
2288 free(*link_target);
2289 *link_target = malloc(PATH_MAX);
2290 if (*link_target == NULL) {
2291 err = got_error_from_errno("malloc");
2292 goto done;
2294 err = got_canonpath(abspath, *link_target, PATH_MAX);
2295 free(abspath);
2296 if (err)
2297 goto done;
2300 done:
2301 free(tree_obj_id);
2302 if (tree)
2303 got_object_tree_close(tree);
2304 if (err) {
2305 free(*link_target);
2306 *link_target = NULL;
2308 return err;
2311 const struct got_error *
2312 got_object_resolve_symlinks(char **link_target, const char *path,
2313 struct got_commit_object *commit, struct got_repository *repo)
2315 const struct got_error *err = NULL;
2316 char *next_target = NULL;
2317 int max_recursion = 40; /* matches Git */
2319 *link_target = NULL;
2321 do {
2322 err = resolve_symlink(&next_target,
2323 *link_target ? *link_target : path, commit, repo);
2324 if (err)
2325 break;
2326 if (next_target) {
2327 free(*link_target);
2328 if (--max_recursion == 0) {
2329 err = got_error_path(path, GOT_ERR_RECURSION);
2330 *link_target = NULL;
2331 break;
2333 *link_target = next_target;
2335 } while (next_target);
2337 return err;
2340 const struct got_error *
2341 got_traverse_packed_commits(struct got_object_id_queue *traversed_commits,
2342 struct got_object_id *commit_id, const char *path,
2343 struct got_repository *repo)
2345 const struct got_error *err = NULL;
2346 struct got_pack *pack = NULL;
2347 struct got_packidx *packidx = NULL;
2348 char *path_packfile = NULL;
2349 struct got_commit_object *changed_commit = NULL;
2350 struct got_object_id *changed_commit_id = NULL;
2351 int idx;
2353 err = got_repo_search_packidx(&packidx, &idx, repo, commit_id);
2354 if (err) {
2355 if (err->code != GOT_ERR_NO_OBJ)
2356 return err;
2357 return NULL;
2360 err = got_packidx_get_packfile_path(&path_packfile,
2361 packidx->path_packidx);
2362 if (err)
2363 return err;
2365 pack = got_repo_get_cached_pack(repo, path_packfile);
2366 if (pack == NULL) {
2367 err = got_repo_cache_pack(&pack, repo, path_packfile, packidx);
2368 if (err)
2369 goto done;
2372 if (pack->privsep_child == NULL) {
2373 err = start_pack_privsep_child(pack, packidx);
2374 if (err)
2375 goto done;
2378 err = got_privsep_send_commit_traversal_request(
2379 pack->privsep_child->ibuf, commit_id, idx, path);
2380 if (err)
2381 goto done;
2383 err = got_privsep_recv_traversed_commits(&changed_commit,
2384 &changed_commit_id, traversed_commits, pack->privsep_child->ibuf);
2385 if (err)
2386 goto done;
2388 if (changed_commit) {
2390 * Cache the commit in which the path was changed.
2391 * This commit might be opened again soon.
2393 changed_commit->refcnt++;
2394 err = got_repo_cache_commit(repo, changed_commit_id,
2395 changed_commit);
2396 got_object_commit_close(changed_commit);
2398 done:
2399 free(path_packfile);
2400 free(changed_commit_id);
2401 return err;
2404 const struct got_error *
2405 got_object_enumerate(int *found_all_objects,
2406 got_object_enumerate_commit_cb cb_commit,
2407 got_object_enumerate_tree_cb cb_tree, void *cb_arg,
2408 struct got_object_id **ours, int nours,
2409 struct got_object_id **theirs, int ntheirs,
2410 struct got_packidx *packidx, struct got_repository *repo)
2412 const struct got_error *err = NULL;
2413 struct got_pack *pack;
2414 char *path_packfile = NULL;
2416 err = got_packidx_get_packfile_path(&path_packfile,
2417 packidx->path_packidx);
2418 if (err)
2419 return err;
2421 pack = got_repo_get_cached_pack(repo, path_packfile);
2422 if (pack == NULL) {
2423 err = got_repo_cache_pack(&pack, repo, path_packfile, packidx);
2424 if (err)
2425 goto done;
2428 if (pack->privsep_child == NULL) {
2429 err = start_pack_privsep_child(pack, packidx);
2430 if (err)
2431 goto done;
2434 err = got_privsep_send_object_enumeration_request(
2435 pack->privsep_child->ibuf);
2436 if (err)
2437 goto done;
2439 err = got_privsep_send_object_idlist(pack->privsep_child->ibuf,
2440 ours, nours);
2441 if (err)
2442 goto done;
2443 err = got_privsep_send_object_idlist_done(pack->privsep_child->ibuf);
2444 if (err)
2445 goto done;
2447 err = got_privsep_send_object_idlist(pack->privsep_child->ibuf,
2448 theirs, ntheirs);
2449 if (err)
2450 goto done;
2451 err = got_privsep_send_object_idlist_done(pack->privsep_child->ibuf);
2452 if (err)
2453 goto done;
2455 err = got_privsep_recv_enumerated_objects(found_all_objects,
2456 pack->privsep_child->ibuf, cb_commit, cb_tree, cb_arg, repo);
2457 done:
2458 free(path_packfile);
2459 return err;