Blob


1 /*
2 * Copyright (c) 2018 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/uio.h>
21 #include <sys/socket.h>
22 #include <sys/wait.h>
23 #include <sys/syslimits.h>
25 #include <errno.h>
26 #include <fcntl.h>
27 #include <stdio.h>
28 #include <stdlib.h>
29 #include <string.h>
30 #include <stdint.h>
31 #include <sha1.h>
32 #include <zlib.h>
33 #include <ctype.h>
34 #include <limits.h>
35 #include <imsg.h>
36 #include <time.h>
38 #include "got_error.h"
39 #include "got_object.h"
40 #include "got_repository.h"
41 #include "got_opentemp.h"
43 #include "got_lib_sha1.h"
44 #include "got_lib_delta.h"
45 #include "got_lib_path.h"
46 #include "got_lib_inflate.h"
47 #include "got_lib_object.h"
48 #include "got_lib_privsep.h"
49 #include "got_lib_object_idcache.h"
50 #include "got_lib_object_cache.h"
51 #include "got_lib_object_parse.h"
52 #include "got_lib_pack.h"
53 #include "got_lib_repository.h"
55 #ifndef MIN
56 #define MIN(_a,_b) ((_a) < (_b) ? (_a) : (_b))
57 #endif
59 struct got_object_id *
60 got_object_id_dup(struct got_object_id *id1)
61 {
62 struct got_object_id *id2;
64 id2 = malloc(sizeof(*id2));
65 if (id2 == NULL)
66 return NULL;
67 memcpy(id2, id1, sizeof(*id2));
68 return id2;
69 }
71 struct got_object_id *
72 got_object_get_id(struct got_object *obj)
73 {
74 return &obj->id;
75 }
77 const struct got_error *
78 got_object_get_id_str(char **outbuf, struct got_object *obj)
79 {
80 return got_object_id_str(outbuf, &obj->id);
81 }
83 const struct got_error *
84 got_object_get_type(int *type, struct got_repository *repo,
85 struct got_object_id *id)
86 {
87 const struct got_error *err = NULL;
88 struct got_object *obj;
90 err = got_object_open(&obj, repo, id);
91 if (err)
92 return err;
94 switch (obj->type) {
95 case GOT_OBJ_TYPE_COMMIT:
96 case GOT_OBJ_TYPE_TREE:
97 case GOT_OBJ_TYPE_BLOB:
98 case GOT_OBJ_TYPE_TAG:
99 *type = obj->type;
100 break;
101 default:
102 err = got_error(GOT_ERR_OBJ_TYPE);
103 break;
106 got_object_close(obj);
107 return err;
110 static const struct got_error *
111 object_path(char **path, struct got_object_id *id, struct got_repository *repo)
113 const struct got_error *err = NULL;
114 char *hex = NULL;
115 char *path_objects = got_repo_get_path_objects(repo);
117 *path = NULL;
119 if (path_objects == NULL)
120 return got_error_from_errno();
122 err = got_object_id_str(&hex, id);
123 if (err)
124 goto done;
126 if (asprintf(path, "%s/%.2x/%s", path_objects,
127 id->sha1[0], hex + 2) == -1)
128 err = got_error_from_errno();
130 done:
131 free(hex);
132 free(path_objects);
133 return err;
136 static const struct got_error *
137 open_loose_object(int *fd, struct got_object_id *id,
138 struct got_repository *repo)
140 const struct got_error *err = NULL;
141 char *path;
143 err = object_path(&path, id, repo);
144 if (err)
145 return err;
146 *fd = open(path, O_RDONLY | O_NOFOLLOW, GOT_DEFAULT_FILE_MODE);
147 if (*fd == -1) {
148 err = got_error_from_errno();
149 goto done;
151 done:
152 free(path);
153 return err;
156 static const struct got_error *
157 get_packfile_path(char **path_packfile, struct got_packidx *packidx)
159 size_t size;
161 /* Packfile path contains ".pack" instead of ".idx", so add one byte. */
162 size = strlen(packidx->path_packidx) + 2;
163 if (size < GOT_PACKFILE_NAMELEN + 1)
164 return got_error(GOT_ERR_BAD_PATH);
166 *path_packfile = malloc(size);
167 if (*path_packfile == NULL)
168 return got_error_from_errno();
170 /* Copy up to and excluding ".idx". */
171 if (strlcpy(*path_packfile, packidx->path_packidx,
172 size - strlen(GOT_PACKIDX_SUFFIX) - 1) >= size)
173 return got_error(GOT_ERR_NO_SPACE);
175 if (strlcat(*path_packfile, GOT_PACKFILE_SUFFIX, size) >= size)
176 return got_error(GOT_ERR_NO_SPACE);
178 return NULL;
181 static void
182 exec_privsep_child(int imsg_fds[2], const char *path, const char *repo_path)
184 close(imsg_fds[0]);
186 if (dup2(imsg_fds[1], GOT_IMSG_FD_CHILD) == -1) {
187 fprintf(stderr, "%s: %s\n", getprogname(),
188 strerror(errno));
189 _exit(1);
191 if (closefrom(GOT_IMSG_FD_CHILD + 1) == -1) {
192 fprintf(stderr, "%s: %s\n", getprogname(),
193 strerror(errno));
194 _exit(1);
197 if (execl(path, path, repo_path, (char *)NULL) == -1) {
198 fprintf(stderr, "%s: %s: %s\n", getprogname(), path,
199 strerror(errno));
200 _exit(1);
204 static const struct got_error *
205 request_packed_object(struct got_object **obj, struct got_pack *pack, int idx,
206 struct got_object_id *id)
208 const struct got_error *err = NULL;
209 struct imsgbuf *ibuf = pack->privsep_child->ibuf;
211 err = got_privsep_send_packed_obj_req(ibuf, idx, id);
212 if (err)
213 return err;
215 err = got_privsep_recv_obj(obj, ibuf);
216 if (err)
217 return err;
219 (*obj)->path_packfile = strdup(pack->path_packfile);
220 if ((*obj)->path_packfile == NULL) {
221 err = got_error_from_errno();
222 return err;
224 memcpy(&(*obj)->id, id, sizeof((*obj)->id));
226 return NULL;
229 static const struct got_error *
230 start_pack_privsep_child(struct got_pack *pack, struct got_packidx *packidx)
232 const struct got_error *err = NULL;
233 int imsg_fds[2];
234 pid_t pid;
235 struct imsgbuf *ibuf;
237 ibuf = calloc(1, sizeof(*ibuf));
238 if (ibuf == NULL)
239 return got_error_from_errno();
241 pack->privsep_child = calloc(1, sizeof(*pack->privsep_child));
242 if (pack->privsep_child == NULL) {
243 err = got_error_from_errno();
244 free(ibuf);
245 return err;
248 if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_fds) == -1) {
249 err = got_error_from_errno();
250 goto done;
253 pid = fork();
254 if (pid == -1) {
255 err = got_error_from_errno();
256 goto done;
257 } else if (pid == 0) {
258 exec_privsep_child(imsg_fds, GOT_PATH_PROG_READ_PACK,
259 pack->path_packfile);
260 /* not reached */
263 close(imsg_fds[1]);
264 pack->privsep_child->imsg_fd = imsg_fds[0];
265 pack->privsep_child->pid = pid;
266 imsg_init(ibuf, imsg_fds[0]);
267 pack->privsep_child->ibuf = ibuf;
269 err = got_privsep_init_pack_child(ibuf, pack, packidx);
270 if (err) {
271 const struct got_error *child_err;
272 err = got_privsep_send_stop(pack->privsep_child->imsg_fd);
273 child_err = got_privsep_wait_for_child(
274 pack->privsep_child->pid);
275 if (child_err && err == NULL)
276 err = child_err;
278 done:
279 if (err) {
280 free(ibuf);
281 free(pack->privsep_child);
282 pack->privsep_child = NULL;
284 return err;
287 static const struct got_error *
288 read_packed_object_privsep(struct got_object **obj,
289 struct got_repository *repo, struct got_pack *pack,
290 struct got_packidx *packidx, int idx, struct got_object_id *id)
292 const struct got_error *err = NULL;
294 if (pack->privsep_child)
295 return request_packed_object(obj, pack, idx, id);
297 err = start_pack_privsep_child(pack, packidx);
298 if (err)
299 return err;
301 return request_packed_object(obj, pack, idx, id);
305 static const struct got_error *
306 open_packed_object(struct got_object **obj, struct got_object_id *id,
307 struct got_repository *repo)
309 const struct got_error *err = NULL;
310 struct got_pack *pack = NULL;
311 struct got_packidx *packidx = NULL;
312 int idx;
313 char *path_packfile;
315 err = got_repo_search_packidx(&packidx, &idx, repo, id);
316 if (err)
317 return err;
319 err = get_packfile_path(&path_packfile, packidx);
320 if (err)
321 return err;
323 pack = got_repo_get_cached_pack(repo, path_packfile);
324 if (pack == NULL) {
325 err = got_repo_cache_pack(&pack, repo, path_packfile, packidx);
326 if (err)
327 goto done;
330 err = read_packed_object_privsep(obj, repo, pack, packidx, idx, id);
331 if (err)
332 goto done;
334 err = got_repo_cache_pack(NULL, repo, (*obj)->path_packfile, packidx);
335 done:
336 free(path_packfile);
337 return err;
340 const struct got_error *
341 got_object_open(struct got_object **obj, struct got_repository *repo,
342 struct got_object_id *id)
344 const struct got_error *err = NULL;
345 char *path;
346 int fd;
348 *obj = got_repo_get_cached_object(repo, id);
349 if (*obj != NULL) {
350 (*obj)->refcnt++;
351 return NULL;
354 err = open_packed_object(obj, id, repo);
355 if (err && err->code != GOT_ERR_NO_OBJ)
356 return err;
357 if (*obj) {
358 (*obj)->refcnt++;
359 return got_repo_cache_object(repo, id, *obj);
362 err = object_path(&path, id, repo);
363 if (err)
364 return err;
366 fd = open(path, O_RDONLY | O_NOFOLLOW, GOT_DEFAULT_FILE_MODE);
367 if (fd == -1) {
368 if (errno == ENOENT)
369 err = got_error_no_obj(id);
370 else
371 err = got_error_from_errno();
372 goto done;
373 } else {
374 err = got_object_read_header_privsep(obj, repo, fd);
375 if (err)
376 goto done;
377 memcpy((*obj)->id.sha1, id->sha1, SHA1_DIGEST_LENGTH);
380 (*obj)->refcnt++;
381 err = got_repo_cache_object(repo, id, *obj);
382 done:
383 free(path);
384 if (fd != -1)
385 close(fd);
386 return err;
390 const struct got_error *
391 got_object_open_by_id_str(struct got_object **obj, struct got_repository *repo,
392 const char *id_str)
394 struct got_object_id id;
396 if (!got_parse_sha1_digest(id.sha1, id_str))
397 return got_error(GOT_ERR_BAD_OBJ_ID_STR);
399 return got_object_open(obj, repo, &id);
402 const struct got_error *
403 got_object_resolve_id_str(struct got_object_id **id,
404 struct got_repository *repo, const char *id_str)
406 const struct got_error *err = NULL;
407 struct got_object *obj;
409 err = got_object_open_by_id_str(&obj, repo, id_str);
410 if (err)
411 return err;
413 *id = got_object_id_dup(got_object_get_id(obj));
414 got_object_close(obj);
415 if (*id == NULL)
416 return got_error_from_errno();
418 return NULL;
421 static const struct got_error *
422 request_packed_commit(struct got_commit_object **commit, struct got_pack *pack,
423 int pack_idx, struct got_object_id *id)
425 const struct got_error *err = NULL;
427 err = got_privsep_send_commit_req(pack->privsep_child->ibuf, -1, id,
428 pack_idx);
429 if (err)
430 return err;
432 return got_privsep_recv_commit(commit, pack->privsep_child->ibuf);
435 static const struct got_error *
436 read_packed_commit_privsep(struct got_commit_object **commit,
437 struct got_pack *pack, struct got_packidx *packidx, int idx,
438 struct got_object_id *id)
440 const struct got_error *err = NULL;
442 if (pack->privsep_child)
443 return request_packed_commit(commit, pack, idx, id);
445 err = start_pack_privsep_child(pack, packidx);
446 if (err)
447 return err;
449 return request_packed_commit(commit, pack, idx, id);
452 static const struct got_error *
453 open_commit(struct got_commit_object **commit,
454 struct got_repository *repo, struct got_object_id *id, int check_cache)
456 const struct got_error *err = NULL;
457 struct got_packidx *packidx = NULL;
458 int idx;
459 char *path_packfile;
461 if (check_cache) {
462 *commit = got_repo_get_cached_commit(repo, id);
463 if (*commit != NULL) {
464 (*commit)->refcnt++;
465 return NULL;
467 } else
468 *commit = NULL;
470 err = got_repo_search_packidx(&packidx, &idx, repo, id);
471 if (err == NULL) {
472 struct got_pack *pack = NULL;
474 err = get_packfile_path(&path_packfile, packidx);
475 if (err)
476 return err;
478 pack = got_repo_get_cached_pack(repo, path_packfile);
479 if (pack == NULL) {
480 err = got_repo_cache_pack(&pack, repo, path_packfile,
481 packidx);
482 if (err)
483 return err;
485 err = read_packed_commit_privsep(commit, pack,
486 packidx, idx, id);
487 } else if (err->code == GOT_ERR_NO_OBJ) {
488 int fd;
490 err = open_loose_object(&fd, id, repo);
491 if (err)
492 return err;
493 err = got_object_read_commit_privsep(commit, fd, repo);
494 close(fd);
497 if (err == NULL) {
498 (*commit)->refcnt++;
499 err = got_repo_cache_commit(repo, id, *commit);
502 return err;
505 const struct got_error *
506 got_object_open_as_commit(struct got_commit_object **commit,
507 struct got_repository *repo, struct got_object_id *id)
509 *commit = got_repo_get_cached_commit(repo, id);
510 if (*commit != NULL) {
511 (*commit)->refcnt++;
512 return NULL;
515 return open_commit(commit, repo, id, 0);
518 const struct got_error *
519 got_object_commit_open(struct got_commit_object **commit,
520 struct got_repository *repo, struct got_object *obj)
522 return open_commit(commit, repo, got_object_get_id(obj), 1);
525 const struct got_error *
526 got_object_qid_alloc(struct got_object_qid **qid, struct got_object_id *id)
528 const struct got_error *err = NULL;
530 *qid = calloc(1, sizeof(**qid));
531 if (*qid == NULL)
532 return got_error_from_errno();
534 (*qid)->id = got_object_id_dup(id);
535 if ((*qid)->id == NULL) {
536 err = got_error_from_errno();
537 got_object_qid_free(*qid);
538 *qid = NULL;
539 return err;
542 return NULL;
545 static const struct got_error *
546 request_packed_tree(struct got_tree_object **tree, struct got_pack *pack,
547 int pack_idx, struct got_object_id *id)
549 const struct got_error *err = NULL;
551 err = got_privsep_send_tree_req(pack->privsep_child->ibuf, -1, id,
552 pack_idx);
553 if (err)
554 return err;
556 return got_privsep_recv_tree(tree, pack->privsep_child->ibuf);
559 static const struct got_error *
560 read_packed_tree_privsep(struct got_tree_object **tree,
561 struct got_pack *pack, struct got_packidx *packidx, int idx,
562 struct got_object_id *id)
564 const struct got_error *err = NULL;
566 if (pack->privsep_child)
567 return request_packed_tree(tree, pack, idx, id);
569 err = start_pack_privsep_child(pack, packidx);
570 if (err)
571 return err;
573 return request_packed_tree(tree, pack, idx, id);
576 static const struct got_error *
577 open_tree(struct got_tree_object **tree, struct got_repository *repo,
578 struct got_object_id *id, int check_cache)
580 const struct got_error *err = NULL;
581 struct got_packidx *packidx = NULL;
582 int idx;
583 char *path_packfile;
585 if (check_cache) {
586 *tree = got_repo_get_cached_tree(repo, id);
587 if (*tree != NULL) {
588 (*tree)->refcnt++;
589 return NULL;
591 } else
592 *tree = NULL;
594 err = got_repo_search_packidx(&packidx, &idx, repo, id);
595 if (err == NULL) {
596 struct got_pack *pack = NULL;
598 err = get_packfile_path(&path_packfile, packidx);
599 if (err)
600 return err;
602 pack = got_repo_get_cached_pack(repo, path_packfile);
603 if (pack == NULL) {
604 err = got_repo_cache_pack(&pack, repo, path_packfile,
605 packidx);
606 if (err)
607 return err;
609 err = read_packed_tree_privsep(tree, pack,
610 packidx, idx, id);
611 } else if (err->code == GOT_ERR_NO_OBJ) {
612 int fd;
614 err = open_loose_object(&fd, id, repo);
615 if (err)
616 return err;
617 err = got_object_read_tree_privsep(tree, fd, repo);
618 close(fd);
621 if (err == NULL) {
622 (*tree)->refcnt++;
623 err = got_repo_cache_tree(repo, id, *tree);
626 return err;
629 const struct got_error *
630 got_object_open_as_tree(struct got_tree_object **tree,
631 struct got_repository *repo, struct got_object_id *id)
633 *tree = got_repo_get_cached_tree(repo, id);
634 if (*tree != NULL) {
635 (*tree)->refcnt++;
636 return NULL;
639 return open_tree(tree, repo, id, 0);
642 const struct got_error *
643 got_object_tree_open(struct got_tree_object **tree,
644 struct got_repository *repo, struct got_object *obj)
646 return open_tree(tree, repo, got_object_get_id(obj), 1);
649 const struct got_tree_entries *
650 got_object_tree_get_entries(struct got_tree_object *tree)
652 return &tree->entries;
655 static const struct got_error *
656 read_packed_blob_privsep(size_t *size, int outfd, struct got_object *obj,
657 struct got_pack *pack)
659 const struct got_error *err = NULL;
660 int outfd_child;
661 int basefd, accumfd; /* temporary files for delta application */
663 basefd = got_opentempfd();
664 if (basefd == -1)
665 return got_error_from_errno();
666 accumfd = got_opentempfd();
667 if (accumfd == -1)
668 return got_error_from_errno();
670 outfd_child = dup(outfd);
671 if (outfd_child == -1)
672 return got_error_from_errno();
674 err = got_privsep_send_obj_req(pack->privsep_child->ibuf, -1, obj);
675 if (err)
676 return err;
678 err = got_privsep_send_blob_outfd(pack->privsep_child->ibuf,
679 outfd_child);
680 if (err) {
681 close(outfd_child);
682 return err;
684 err = got_privsep_send_tmpfd(pack->privsep_child->ibuf,
685 basefd);
686 if (err) {
687 close(basefd);
688 close(accumfd);
689 close(outfd_child);
690 return err;
693 err = got_privsep_send_tmpfd(pack->privsep_child->ibuf,
694 accumfd);
695 if (err) {
696 close(accumfd);
697 close(outfd_child);
698 return err;
701 err = got_privsep_recv_blob(size, pack->privsep_child->ibuf);
702 if (err)
703 return err;
705 if (lseek(outfd, SEEK_SET, 0) == -1)
706 err = got_error_from_errno();
708 return err;
711 const struct got_error *
712 got_object_blob_open(struct got_blob_object **blob,
713 struct got_repository *repo, struct got_object *obj, size_t blocksize)
715 const struct got_error *err = NULL;
716 int outfd;
717 size_t size;
718 struct stat sb;
720 if (obj->type != GOT_OBJ_TYPE_BLOB)
721 return got_error(GOT_ERR_OBJ_TYPE);
723 if (blocksize < obj->hdrlen)
724 return got_error(GOT_ERR_NO_SPACE);
726 *blob = calloc(1, sizeof(**blob));
727 if (*blob == NULL)
728 return got_error_from_errno();
730 outfd = got_opentempfd();
731 if (outfd == -1)
732 return got_error_from_errno();
734 (*blob)->read_buf = malloc(blocksize);
735 if ((*blob)->read_buf == NULL) {
736 err = got_error_from_errno();
737 goto done;
739 if (obj->flags & GOT_OBJ_FLAG_PACKED) {
740 struct got_pack *pack;
741 pack = got_repo_get_cached_pack(repo, obj->path_packfile);
742 if (pack == NULL) {
743 err = got_repo_cache_pack(&pack, repo,
744 obj->path_packfile, NULL);
745 if (err)
746 goto done;
748 err = read_packed_blob_privsep(&size, outfd, obj, pack);
749 if (err)
750 goto done;
751 obj->size = size;
752 } else {
753 int infd;
755 err = open_loose_object(&infd, got_object_get_id(obj), repo);
756 if (err)
757 goto done;
759 err = got_object_read_blob_privsep(&size, outfd, infd, repo);
760 close(infd);
761 if (err)
762 goto done;
764 if (size != obj->hdrlen + obj->size) {
765 err = got_error(GOT_ERR_PRIVSEP_LEN);
766 goto done;
770 if (fstat(outfd, &sb) == -1) {
771 err = got_error_from_errno();
772 goto done;
775 if (sb.st_size != obj->hdrlen + obj->size) {
776 err = got_error(GOT_ERR_PRIVSEP_LEN);
777 goto done;
780 (*blob)->f = fdopen(outfd, "rb");
781 if ((*blob)->f == NULL) {
782 err = got_error_from_errno();
783 close(outfd);
784 goto done;
787 (*blob)->hdrlen = obj->hdrlen;
788 (*blob)->blocksize = blocksize;
789 memcpy(&(*blob)->id.sha1, obj->id.sha1, SHA1_DIGEST_LENGTH);
791 done:
792 if (err) {
793 if (*blob) {
794 if ((*blob)->f)
795 fclose((*blob)->f);
796 free((*blob)->read_buf);
797 free(*blob);
798 *blob = NULL;
799 } else if (outfd != -1)
800 close(outfd);
802 return err;
805 const struct got_error *
806 got_object_open_as_blob(struct got_blob_object **blob,
807 struct got_repository *repo, struct got_object_id *id,
808 size_t blocksize)
810 const struct got_error *err;
811 struct got_object *obj;
813 *blob = NULL;
815 err = got_object_open(&obj, repo, id);
816 if (err)
817 return err;
818 if (obj->type != GOT_OBJ_TYPE_BLOB) {
819 err = got_error(GOT_ERR_OBJ_TYPE);
820 goto done;
823 err = got_object_blob_open(blob, repo, obj, blocksize);
824 done:
825 got_object_close(obj);
826 return err;
829 void
830 got_object_blob_close(struct got_blob_object *blob)
832 free(blob->read_buf);
833 fclose(blob->f);
834 free(blob);
837 char *
838 got_object_blob_id_str(struct got_blob_object *blob, char *buf, size_t size)
840 return got_sha1_digest_to_str(blob->id.sha1, buf, size);
843 size_t
844 got_object_blob_get_hdrlen(struct got_blob_object *blob)
846 return blob->hdrlen;
849 const uint8_t *
850 got_object_blob_get_read_buf(struct got_blob_object *blob)
852 return blob->read_buf;
855 const struct got_error *
856 got_object_blob_read_block(size_t *outlenp, struct got_blob_object *blob)
858 size_t n;
860 n = fread(blob->read_buf, 1, blob->blocksize, blob->f);
861 if (n == 0 && ferror(blob->f))
862 return got_ferror(blob->f, GOT_ERR_IO);
863 *outlenp = n;
864 return NULL;
867 const struct got_error *
868 got_object_blob_dump_to_file(size_t *total_len, int *nlines,
869 FILE *outfile, struct got_blob_object *blob)
871 const struct got_error *err = NULL;
872 size_t len, hdrlen;
873 const uint8_t *buf;
874 int i;
876 if (total_len)
877 *total_len = 0;
878 if (nlines)
879 *nlines = 0;
881 hdrlen = got_object_blob_get_hdrlen(blob);
882 do {
883 err = got_object_blob_read_block(&len, blob);
884 if (err)
885 return err;
886 if (len == 0)
887 break;
888 if (total_len)
889 *total_len += len;
890 buf = got_object_blob_get_read_buf(blob);
891 if (nlines) {
892 for (i = 0; i < len; i++) {
893 if (buf[i] == '\n')
894 (*nlines)++;
897 /* Skip blob object header first time around. */
898 fwrite(buf + hdrlen, len - hdrlen, 1, outfile);
899 hdrlen = 0;
900 } while (len != 0);
902 fflush(outfile);
903 rewind(outfile);
905 return NULL;
908 static const struct got_error *
909 read_packed_tag_privsep(struct got_tag_object **tag,
910 struct got_object *obj, struct got_pack *pack)
912 const struct got_error *err = NULL;
914 err = got_privsep_send_obj_req(pack->privsep_child->ibuf, -1, obj);
915 if (err)
916 return err;
918 return got_privsep_recv_tag(tag, pack->privsep_child->ibuf);
922 static const struct got_error *
923 open_tag(struct got_tag_object **tag,
924 struct got_repository *repo, struct got_object *obj, int check_cache)
926 const struct got_error *err = NULL;
928 if (check_cache) {
929 *tag = got_repo_get_cached_tag(repo, &obj->id);
930 if (*tag != NULL) {
931 (*tag)->refcnt++;
932 return NULL;
934 } else
935 *tag = NULL;
937 if (obj->type != GOT_OBJ_TYPE_TAG)
938 return got_error(GOT_ERR_OBJ_TYPE);
940 if (obj->flags & GOT_OBJ_FLAG_PACKED) {
941 struct got_pack *pack;
942 pack = got_repo_get_cached_pack(repo, obj->path_packfile);
943 if (pack == NULL) {
944 err = got_repo_cache_pack(&pack, repo,
945 obj->path_packfile, NULL);
946 if (err)
947 return err;
949 err = read_packed_tag_privsep(tag, obj, pack);
950 } else {
951 int fd;
952 err = open_loose_object(&fd, got_object_get_id(obj), repo);
953 if (err)
954 return err;
955 err = got_object_read_tag_privsep(tag, obj, fd, repo);
956 close(fd);
959 if (err == NULL) {
960 (*tag)->refcnt++;
961 err = got_repo_cache_tag(repo, &obj->id, *tag);
964 return err;
967 const struct got_error *
968 got_object_open_as_tag(struct got_tag_object **tag,
969 struct got_repository *repo, struct got_object_id *id)
971 const struct got_error *err;
972 struct got_object *obj;
974 *tag = got_repo_get_cached_tag(repo, id);
975 if (*tag != NULL) {
976 (*tag)->refcnt++;
977 return NULL;
980 err = got_object_open(&obj, repo, id);
981 if (err)
982 return err;
983 if (obj->type != GOT_OBJ_TYPE_COMMIT) {
984 err = got_error(GOT_ERR_OBJ_TYPE);
985 goto done;
988 err = open_tag(tag, repo, obj, 0);
989 done:
990 got_object_close(obj);
991 return err;
994 const struct got_error *
995 got_object_tag_open(struct got_tag_object **tag,
996 struct got_repository *repo, struct got_object *obj)
998 return open_tag(tag, repo, obj, 1);
1001 static struct got_tree_entry *
1002 find_entry_by_name(struct got_tree_object *tree, const char *name, size_t len)
1004 struct got_tree_entry *te;
1006 /* Note that tree entries are sorted in strncmp() order. */
1007 SIMPLEQ_FOREACH(te, &tree->entries.head, entry) {
1008 int cmp = strncmp(te->name, name, len);
1009 if (cmp < 0)
1010 continue;
1011 if (cmp > 0)
1012 break;
1013 if (te->name[len] == '\0')
1014 return te;
1016 return NULL;
1019 const struct got_error *
1020 got_object_id_by_path(struct got_object_id **id, struct got_repository *repo,
1021 struct got_object_id *commit_id, const char *path)
1023 const struct got_error *err = NULL;
1024 struct got_commit_object *commit = NULL;
1025 struct got_tree_object *tree = NULL;
1026 struct got_tree_entry *te = NULL;
1027 const char *seg, *s;
1028 size_t seglen;
1030 *id = NULL;
1032 /* We are expecting an absolute in-repository path. */
1033 if (path[0] != '/')
1034 return got_error(GOT_ERR_NOT_ABSPATH);
1036 err = got_object_open_as_commit(&commit, repo, commit_id);
1037 if (err)
1038 goto done;
1040 /* Handle opening of root of commit's tree. */
1041 if (path[1] == '\0') {
1042 *id = got_object_id_dup(commit->tree_id);
1043 if (*id == NULL)
1044 err = got_error_from_errno();
1045 goto done;
1048 err = got_object_open_as_tree(&tree, repo, commit->tree_id);
1049 if (err)
1050 goto done;
1052 s = path;
1053 s++; /* skip leading '/' */
1054 seg = s;
1055 seglen = 0;
1056 while (*s) {
1057 struct got_tree_object *next_tree;
1059 if (*s != '/') {
1060 s++;
1061 seglen++;
1062 if (*s)
1063 continue;
1066 te = find_entry_by_name(tree, seg, seglen);
1067 if (te == NULL) {
1068 err = got_error(GOT_ERR_NO_TREE_ENTRY);
1069 goto done;
1072 if (*s == '\0')
1073 break;
1075 seg = s + 1;
1076 seglen = 0;
1077 s++;
1078 if (*s) {
1079 err = got_object_open_as_tree(&next_tree, repo,
1080 te->id);
1081 te = NULL;
1082 if (err)
1083 goto done;
1084 got_object_tree_close(tree);
1085 tree = next_tree;
1089 if (te) {
1090 *id = got_object_id_dup(te->id);
1091 if (*id == NULL)
1092 return got_error_from_errno();
1093 } else
1094 err = got_error(GOT_ERR_NO_TREE_ENTRY);
1095 done:
1096 if (commit)
1097 got_object_commit_close(commit);
1098 if (tree)
1099 got_object_tree_close(tree);
1100 return err;
1103 const struct got_error *
1104 got_object_tree_path_changed(int *changed,
1105 struct got_tree_object *tree01, struct got_tree_object *tree02,
1106 const char *path, struct got_repository *repo)
1108 const struct got_error *err = NULL;
1109 struct got_tree_object *tree1 = NULL, *tree2 = NULL;
1110 struct got_tree_entry *te1 = NULL, *te2 = NULL;
1111 const char *seg, *s;
1112 size_t seglen;
1114 *changed = 0;
1116 /* We are expecting an absolute in-repository path. */
1117 if (path[0] != '/')
1118 return got_error(GOT_ERR_NOT_ABSPATH);
1120 /* We not do support comparing the root path. */
1121 if (path[1] == '\0')
1122 return got_error(GOT_ERR_BAD_PATH);
1124 tree1 = tree01;
1125 tree2 = tree02;
1126 s = path;
1127 s++; /* skip leading '/' */
1128 seg = s;
1129 seglen = 0;
1130 while (*s) {
1131 struct got_tree_object *next_tree1, *next_tree2;
1133 if (*s != '/') {
1134 s++;
1135 seglen++;
1136 if (*s)
1137 continue;
1140 te1 = find_entry_by_name(tree1, seg, seglen);
1141 if (te1 == NULL) {
1142 err = got_error(GOT_ERR_NO_OBJ);
1143 goto done;
1146 te2 = find_entry_by_name(tree2, seg, seglen);
1147 if (te2 == NULL) {
1148 *changed = 1;
1149 goto done;
1152 if (te1->mode != te2->mode) {
1153 *changed = 1;
1154 goto done;
1157 if (got_object_id_cmp(te1->id, te2->id) == 0) {
1158 *changed = 0;
1159 goto done;
1162 if (*s == '\0') { /* final path element */
1163 *changed = 1;
1164 goto done;
1167 seg = s + 1;
1168 s++;
1169 seglen = 0;
1170 if (*s) {
1171 err = got_object_open_as_tree(&next_tree1, repo,
1172 te1->id);
1173 te1 = NULL;
1174 if (err)
1175 goto done;
1176 if (tree1 != tree01)
1177 got_object_tree_close(tree1);
1178 tree1 = next_tree1;
1180 err = got_object_open_as_tree(&next_tree2, repo,
1181 te2->id);
1182 te2 = NULL;
1183 if (err)
1184 goto done;
1185 if (tree2 != tree02)
1186 got_object_tree_close(tree2);
1187 tree2 = next_tree2;
1190 done:
1191 if (tree1 && tree1 != tree01)
1192 got_object_tree_close(tree1);
1193 if (tree2 && tree2 != tree02)
1194 got_object_tree_close(tree2);
1195 return err;
1198 static const struct got_error *
1199 request_object(struct got_object **obj, struct got_repository *repo, int fd)
1201 const struct got_error *err = NULL;
1202 struct imsgbuf *ibuf;
1204 ibuf = repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_OBJECT].ibuf;
1206 err = got_privsep_send_obj_req(ibuf, fd, NULL);
1207 if (err)
1208 return err;
1210 return got_privsep_recv_obj(obj, ibuf);
1213 const struct got_error *
1214 got_object_read_header_privsep(struct got_object **obj,
1215 struct got_repository *repo, int obj_fd)
1217 int imsg_fds[2];
1218 pid_t pid;
1219 struct imsgbuf *ibuf;
1221 if (repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_OBJECT].imsg_fd != -1)
1222 return request_object(obj, repo, obj_fd);
1224 ibuf = calloc(1, sizeof(*ibuf));
1225 if (ibuf == NULL)
1226 return got_error_from_errno();
1228 if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_fds) == -1)
1229 return got_error_from_errno();
1231 pid = fork();
1232 if (pid == -1)
1233 return got_error_from_errno();
1234 else if (pid == 0) {
1235 exec_privsep_child(imsg_fds, GOT_PATH_PROG_READ_OBJECT,
1236 repo->path);
1237 /* not reached */
1240 close(imsg_fds[1]);
1241 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_OBJECT].imsg_fd =
1242 imsg_fds[0];
1243 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_OBJECT].pid = pid;
1244 imsg_init(ibuf, imsg_fds[0]);
1245 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_OBJECT].ibuf = ibuf;
1247 return request_object(obj, repo, obj_fd);
1250 static const struct got_error *
1251 request_commit(struct got_commit_object **commit, struct got_repository *repo,
1252 int fd)
1254 const struct got_error *err = NULL;
1255 struct imsgbuf *ibuf;
1257 ibuf = repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_COMMIT].ibuf;
1259 err = got_privsep_send_commit_req(ibuf, fd, NULL, -1);
1260 if (err)
1261 return err;
1263 return got_privsep_recv_commit(commit, ibuf);
1266 const struct got_error *
1267 got_object_read_commit_privsep(struct got_commit_object **commit,
1268 int obj_fd, struct got_repository *repo)
1270 int imsg_fds[2];
1271 pid_t pid;
1272 struct imsgbuf *ibuf;
1274 if (repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_COMMIT].imsg_fd != -1)
1275 return request_commit(commit, repo, obj_fd);
1277 ibuf = calloc(1, sizeof(*ibuf));
1278 if (ibuf == NULL)
1279 return got_error_from_errno();
1281 if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_fds) == -1)
1282 return got_error_from_errno();
1284 pid = fork();
1285 if (pid == -1)
1286 return got_error_from_errno();
1287 else if (pid == 0) {
1288 exec_privsep_child(imsg_fds, GOT_PATH_PROG_READ_COMMIT,
1289 repo->path);
1290 /* not reached */
1293 close(imsg_fds[1]);
1294 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_COMMIT].imsg_fd =
1295 imsg_fds[0];
1296 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_COMMIT].pid = pid;
1297 imsg_init(ibuf, imsg_fds[0]);
1298 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_COMMIT].ibuf = ibuf;
1300 return request_commit(commit, repo, obj_fd);
1303 static const struct got_error *
1304 request_tree(struct got_tree_object **tree, struct got_repository *repo,
1305 int fd)
1307 const struct got_error *err = NULL;
1308 struct imsgbuf *ibuf;
1310 ibuf = repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TREE].ibuf;
1312 err = got_privsep_send_tree_req(ibuf, fd, NULL, -1);
1313 if (err)
1314 return err;
1316 return got_privsep_recv_tree(tree, ibuf);
1319 const struct got_error *
1320 got_object_read_tree_privsep(struct got_tree_object **tree,
1321 int obj_fd, struct got_repository *repo)
1323 int imsg_fds[2];
1324 pid_t pid;
1325 struct imsgbuf *ibuf;
1327 if (repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TREE].imsg_fd != -1)
1328 return request_tree(tree, repo, obj_fd);
1330 ibuf = calloc(1, sizeof(*ibuf));
1331 if (ibuf == NULL)
1332 return got_error_from_errno();
1334 if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_fds) == -1)
1335 return got_error_from_errno();
1337 pid = fork();
1338 if (pid == -1)
1339 return got_error_from_errno();
1340 else if (pid == 0) {
1341 exec_privsep_child(imsg_fds, GOT_PATH_PROG_READ_TREE,
1342 repo->path);
1343 /* not reached */
1346 close(imsg_fds[1]);
1348 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TREE].imsg_fd =
1349 imsg_fds[0];
1350 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TREE].pid = pid;
1351 imsg_init(ibuf, imsg_fds[0]);
1352 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TREE].ibuf = ibuf;
1355 return request_tree(tree, repo, obj_fd);
1358 static const struct got_error *
1359 request_blob(size_t *size, int outfd, int infd, struct imsgbuf *ibuf)
1361 const struct got_error *err = NULL;
1362 int outfd_child;
1364 outfd_child = dup(outfd);
1365 if (outfd_child == -1)
1366 return got_error_from_errno();
1368 err = got_privsep_send_blob_req(ibuf, infd);
1369 if (err)
1370 return err;
1372 err = got_privsep_send_blob_outfd(ibuf, outfd_child);
1373 if (err) {
1374 close(outfd_child);
1375 return err;
1378 err = got_privsep_recv_blob(size, ibuf);
1379 if (err)
1380 return err;
1382 if (lseek(outfd, SEEK_SET, 0) == -1)
1383 return got_error_from_errno();
1385 return err;
1388 const struct got_error *
1389 got_object_read_blob_privsep(size_t *size, int outfd, int infd,
1390 struct got_repository *repo)
1392 int imsg_fds[2];
1393 pid_t pid;
1394 struct imsgbuf *ibuf;
1396 if (repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_BLOB].imsg_fd != -1) {
1397 ibuf = repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_BLOB].ibuf;
1398 return request_blob(size, outfd, infd, ibuf);
1401 ibuf = calloc(1, sizeof(*ibuf));
1402 if (ibuf == NULL)
1403 return got_error_from_errno();
1405 if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_fds) == -1)
1406 return got_error_from_errno();
1408 pid = fork();
1409 if (pid == -1)
1410 return got_error_from_errno();
1411 else if (pid == 0) {
1412 exec_privsep_child(imsg_fds, GOT_PATH_PROG_READ_BLOB,
1413 repo->path);
1414 /* not reached */
1417 close(imsg_fds[1]);
1418 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_BLOB].imsg_fd =
1419 imsg_fds[0];
1420 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_BLOB].pid = pid;
1421 imsg_init(ibuf, imsg_fds[0]);
1422 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_BLOB].ibuf = ibuf;
1424 return request_blob(size, outfd, infd, ibuf);
1427 static const struct got_error *
1428 request_tag(struct got_tag_object **tag, struct got_repository *repo,
1429 struct got_object *obj, int fd)
1431 const struct got_error *err = NULL;
1432 struct imsgbuf *ibuf;
1434 ibuf = repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TAG].ibuf;
1436 err = got_privsep_send_obj_req(ibuf, fd, obj);
1437 if (err)
1438 return err;
1440 return got_privsep_recv_tag(tag, ibuf);
1443 const struct got_error *
1444 got_object_read_tag_privsep(struct got_tag_object **tag,
1445 struct got_object *obj, int obj_fd, struct got_repository *repo)
1447 int imsg_fds[2];
1448 pid_t pid;
1449 struct imsgbuf *ibuf;
1451 if (repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TAG].imsg_fd != -1)
1452 return request_tag(tag, repo, obj, obj_fd);
1454 ibuf = calloc(1, sizeof(*ibuf));
1455 if (ibuf == NULL)
1456 return got_error_from_errno();
1458 if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_fds) == -1)
1459 return got_error_from_errno();
1461 pid = fork();
1462 if (pid == -1)
1463 return got_error_from_errno();
1464 else if (pid == 0) {
1465 exec_privsep_child(imsg_fds, GOT_PATH_PROG_READ_TAG,
1466 repo->path);
1467 /* not reached */
1470 close(imsg_fds[1]);
1471 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TAG].imsg_fd =
1472 imsg_fds[0];
1473 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TAG].pid = pid;
1474 imsg_init(ibuf, imsg_fds[0]);
1475 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TAG].ibuf = ibuf;
1477 return request_tag(tag, repo, obj, obj_fd);