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_pack.h"
46 #include "got_lib_path.h"
47 #include "got_lib_inflate.h"
48 #include "got_lib_object.h"
49 #include "got_lib_privsep.h"
50 #include "got_lib_object_idcache.h"
51 #include "got_lib_object_cache.h"
52 #include "got_lib_object_parse.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 int
84 got_object_get_type(struct got_object *obj)
85 {
86 switch (obj->type) {
87 case GOT_OBJ_TYPE_COMMIT:
88 case GOT_OBJ_TYPE_TREE:
89 case GOT_OBJ_TYPE_BLOB:
90 case GOT_OBJ_TYPE_TAG:
91 return obj->type;
92 default:
93 abort();
94 break;
95 }
97 /* not reached */
98 return 0;
99 }
101 static const struct got_error *
102 object_path(char **path, struct got_object_id *id, struct got_repository *repo)
104 const struct got_error *err = NULL;
105 char *hex = NULL;
106 char *path_objects = got_repo_get_path_objects(repo);
108 *path = NULL;
110 if (path_objects == NULL)
111 return got_error_from_errno();
113 err = got_object_id_str(&hex, id);
114 if (err)
115 goto done;
117 if (asprintf(path, "%s/%.2x/%s", path_objects,
118 id->sha1[0], hex + 2) == -1)
119 err = got_error_from_errno();
121 done:
122 free(hex);
123 free(path_objects);
124 return err;
127 static const struct got_error *
128 open_loose_object(int *fd, struct got_object *obj, struct got_repository *repo)
130 const struct got_error *err = NULL;
131 char *path;
133 err = object_path(&path, &obj->id, repo);
134 if (err)
135 return err;
136 *fd = open(path, O_RDONLY | O_NOFOLLOW, GOT_DEFAULT_FILE_MODE);
137 if (*fd == -1) {
138 err = got_error_from_errno();
139 goto done;
141 done:
142 free(path);
143 return err;
146 static const struct got_error *
147 get_packfile_path(char **path_packfile, struct got_packidx *packidx)
149 size_t size;
151 /* Packfile path contains ".pack" instead of ".idx", so add one byte. */
152 size = strlen(packidx->path_packidx) + 2;
153 if (size < GOT_PACKFILE_NAMELEN + 1)
154 return got_error(GOT_ERR_BAD_PATH);
156 *path_packfile = malloc(size);
157 if (*path_packfile == NULL)
158 return got_error_from_errno();
160 /* Copy up to and excluding ".idx". */
161 if (strlcpy(*path_packfile, packidx->path_packidx,
162 size - strlen(GOT_PACKIDX_SUFFIX) - 1) >= size)
163 return got_error(GOT_ERR_NO_SPACE);
165 if (strlcat(*path_packfile, GOT_PACKFILE_SUFFIX, size) >= size)
166 return got_error(GOT_ERR_NO_SPACE);
168 return NULL;
171 static const struct got_error *
172 open_packed_object(struct got_object **obj, struct got_object_id *id,
173 struct got_repository *repo)
175 const struct got_error *err = NULL;
176 struct got_pack *pack = NULL;
177 struct got_packidx *packidx = NULL;
178 int idx;
179 char *path_packfile;
181 err = got_repo_search_packidx(&packidx, &idx, repo, id);
182 if (err)
183 return err;
185 err = get_packfile_path(&path_packfile, packidx);
186 if (err)
187 return err;
189 pack = got_repo_get_cached_pack(repo, path_packfile);
190 if (pack == NULL) {
191 err = got_repo_cache_pack(&pack, repo, path_packfile, packidx);
192 if (err)
193 goto done;
196 err = got_object_packed_read_privsep(obj, repo, pack, packidx, idx, id);
197 if (err)
198 goto done;
200 err = got_repo_cache_pack(NULL, repo, (*obj)->path_packfile, packidx);
201 done:
202 free(path_packfile);
203 return err;
206 const struct got_error *
207 got_object_open(struct got_object **obj, struct got_repository *repo,
208 struct got_object_id *id)
210 const struct got_error *err = NULL;
211 char *path;
212 int fd;
214 *obj = got_repo_get_cached_object(repo, id);
215 if (*obj != NULL) {
216 (*obj)->refcnt++;
217 return NULL;
220 err = open_packed_object(obj, id, repo);
221 if (err && err->code != GOT_ERR_NO_OBJ)
222 return err;
223 if (*obj) {
224 (*obj)->refcnt++;
225 return got_repo_cache_object(repo, id, *obj);
228 err = object_path(&path, id, repo);
229 if (err)
230 return err;
232 fd = open(path, O_RDONLY | O_NOFOLLOW, GOT_DEFAULT_FILE_MODE);
233 if (fd == -1) {
234 if (errno == ENOENT)
235 err = got_error_no_obj(id);
236 else
237 err = got_error_from_errno();
238 goto done;
239 } else {
240 err = got_object_read_header_privsep(obj, repo, fd);
241 if (err)
242 goto done;
243 memcpy((*obj)->id.sha1, id->sha1, SHA1_DIGEST_LENGTH);
246 (*obj)->refcnt++;
247 err = got_repo_cache_object(repo, id, *obj);
248 done:
249 free(path);
250 if (fd != -1)
251 close(fd);
252 return err;
256 const struct got_error *
257 got_object_open_by_id_str(struct got_object **obj, struct got_repository *repo,
258 const char *id_str)
260 struct got_object_id id;
262 if (!got_parse_sha1_digest(id.sha1, id_str))
263 return got_error(GOT_ERR_BAD_OBJ_ID_STR);
265 return got_object_open(obj, repo, &id);
268 static const struct got_error *
269 open_commit(struct got_commit_object **commit,
270 struct got_repository *repo, struct got_object *obj, int check_cache)
272 const struct got_error *err = NULL;
274 if (check_cache) {
275 *commit = got_repo_get_cached_commit(repo, &obj->id);
276 if (*commit != NULL) {
277 (*commit)->refcnt++;
278 return NULL;
280 } else
281 *commit = NULL;
283 if (obj->type != GOT_OBJ_TYPE_COMMIT)
284 return got_error(GOT_ERR_OBJ_TYPE);
286 if (obj->flags & GOT_OBJ_FLAG_PACKED) {
287 struct got_pack *pack;
288 pack = got_repo_get_cached_pack(repo, obj->path_packfile);
289 if (pack == NULL) {
290 err = got_repo_cache_pack(&pack, repo,
291 obj->path_packfile, NULL);
292 if (err)
293 return err;
295 err = got_object_read_packed_commit_privsep(commit, obj, pack);
296 } else {
297 int fd;
298 err = open_loose_object(&fd, obj, repo);
299 if (err)
300 return err;
301 err = got_object_read_commit_privsep(commit, obj, fd, repo);
302 close(fd);
305 if (err == NULL) {
306 (*commit)->refcnt++;
307 err = got_repo_cache_commit(repo, &obj->id, *commit);
310 return err;
313 const struct got_error *
314 got_object_open_as_commit(struct got_commit_object **commit,
315 struct got_repository *repo, struct got_object_id *id)
317 const struct got_error *err;
318 struct got_object *obj;
320 *commit = got_repo_get_cached_commit(repo, id);
321 if (*commit != NULL) {
322 (*commit)->refcnt++;
323 return NULL;
326 err = got_object_open(&obj, repo, id);
327 if (err)
328 return err;
329 if (got_object_get_type(obj) != GOT_OBJ_TYPE_COMMIT) {
330 err = got_error(GOT_ERR_OBJ_TYPE);
331 goto done;
334 err = open_commit(commit, repo, obj, 0);
335 done:
336 got_object_close(obj);
337 return err;
340 const struct got_error *
341 got_object_commit_open(struct got_commit_object **commit,
342 struct got_repository *repo, struct got_object *obj)
344 return open_commit(commit, repo, obj, 1);
347 const struct got_error *
348 got_object_qid_alloc(struct got_object_qid **qid, struct got_object_id *id)
350 const struct got_error *err = NULL;
352 *qid = calloc(1, sizeof(**qid));
353 if (*qid == NULL)
354 return got_error_from_errno();
356 (*qid)->id = got_object_id_dup(id);
357 if ((*qid)->id == NULL) {
358 err = got_error_from_errno();
359 got_object_qid_free(*qid);
360 *qid = NULL;
361 return err;
364 return NULL;
367 static const struct got_error *
368 open_tree(struct got_tree_object **tree,
369 struct got_repository *repo, struct got_object *obj, int check_cache)
371 const struct got_error *err = NULL;
373 if (check_cache) {
374 *tree = got_repo_get_cached_tree(repo, &obj->id);
375 if (*tree != NULL) {
376 (*tree)->refcnt++;
377 return NULL;
379 } else
380 *tree = NULL;
382 if (obj->type != GOT_OBJ_TYPE_TREE)
383 return got_error(GOT_ERR_OBJ_TYPE);
385 if (obj->flags & GOT_OBJ_FLAG_PACKED) {
386 struct got_pack *pack;
387 pack = got_repo_get_cached_pack(repo, obj->path_packfile);
388 if (pack == NULL) {
389 err = got_repo_cache_pack(&pack, repo,
390 obj->path_packfile, NULL);
391 if (err)
392 return err;
394 err = got_object_read_packed_tree_privsep(tree, obj, pack);
395 } else {
396 int fd;
397 err = open_loose_object(&fd, obj, repo);
398 if (err)
399 return err;
400 err = got_object_read_tree_privsep(tree, obj, fd, repo);
401 close(fd);
404 if (err == NULL) {
405 (*tree)->refcnt++;
406 err = got_repo_cache_tree(repo, &obj->id, *tree);
409 return err;
412 const struct got_error *
413 got_object_open_as_tree(struct got_tree_object **tree,
414 struct got_repository *repo, struct got_object_id *id)
416 const struct got_error *err;
417 struct got_object *obj;
419 *tree = got_repo_get_cached_tree(repo, id);
420 if (*tree != NULL) {
421 (*tree)->refcnt++;
422 return NULL;
425 err = got_object_open(&obj, repo, id);
426 if (err)
427 return err;
428 if (got_object_get_type(obj) != GOT_OBJ_TYPE_TREE) {
429 err = got_error(GOT_ERR_OBJ_TYPE);
430 goto done;
433 err = open_tree(tree, repo, obj, 0);
434 done:
435 got_object_close(obj);
436 return err;
439 const struct got_error *
440 got_object_tree_open(struct got_tree_object **tree,
441 struct got_repository *repo, struct got_object *obj)
443 return open_tree(tree, repo, obj, 1);
446 const struct got_tree_entries *
447 got_object_tree_get_entries(struct got_tree_object *tree)
449 return &tree->entries;
452 static const struct got_error *
453 read_packed_blob_privsep(size_t *size, int outfd, struct got_object *obj,
454 struct got_pack *pack)
456 const struct got_error *err = NULL;
457 int outfd_child;
458 int basefd, accumfd; /* temporary files for delta application */
460 basefd = got_opentempfd();
461 if (basefd == -1)
462 return got_error_from_errno();
463 accumfd = got_opentempfd();
464 if (accumfd == -1)
465 return got_error_from_errno();
467 outfd_child = dup(outfd);
468 if (outfd_child == -1)
469 return got_error_from_errno();
471 err = got_privsep_send_obj_req(pack->privsep_child->ibuf, -1, obj);
472 if (err)
473 return err;
475 err = got_privsep_send_blob_outfd(pack->privsep_child->ibuf,
476 outfd_child);
477 if (err) {
478 close(outfd_child);
479 return err;
481 err = got_privsep_send_tmpfd(pack->privsep_child->ibuf,
482 basefd);
483 if (err) {
484 close(basefd);
485 close(accumfd);
486 close(outfd_child);
487 return err;
490 err = got_privsep_send_tmpfd(pack->privsep_child->ibuf,
491 accumfd);
492 if (err) {
493 close(accumfd);
494 close(outfd_child);
495 return err;
498 err = got_privsep_recv_blob(size, pack->privsep_child->ibuf);
499 if (err)
500 return err;
502 if (lseek(outfd, SEEK_SET, 0) == -1)
503 err = got_error_from_errno();
505 return err;
508 const struct got_error *
509 got_object_blob_open(struct got_blob_object **blob,
510 struct got_repository *repo, struct got_object *obj, size_t blocksize)
512 const struct got_error *err = NULL;
513 int outfd;
514 size_t size;
515 struct stat sb;
517 if (obj->type != GOT_OBJ_TYPE_BLOB)
518 return got_error(GOT_ERR_OBJ_TYPE);
520 if (blocksize < obj->hdrlen)
521 return got_error(GOT_ERR_NO_SPACE);
523 *blob = calloc(1, sizeof(**blob));
524 if (*blob == NULL)
525 return got_error_from_errno();
527 outfd = got_opentempfd();
528 if (outfd == -1)
529 return got_error_from_errno();
531 (*blob)->read_buf = malloc(blocksize);
532 if ((*blob)->read_buf == NULL) {
533 err = got_error_from_errno();
534 goto done;
536 if (obj->flags & GOT_OBJ_FLAG_PACKED) {
537 struct got_pack *pack;
538 pack = got_repo_get_cached_pack(repo, obj->path_packfile);
539 if (pack == NULL) {
540 err = got_repo_cache_pack(&pack, repo,
541 obj->path_packfile, NULL);
542 if (err)
543 goto done;
545 err = read_packed_blob_privsep(&size, outfd, obj, pack);
546 if (err)
547 goto done;
548 obj->size = size;
549 } else {
550 int infd;
552 err = open_loose_object(&infd, obj, repo);
553 if (err)
554 goto done;
556 err = got_object_read_blob_privsep(&size, outfd, infd, repo);
557 close(infd);
558 if (err)
559 goto done;
561 if (size != obj->hdrlen + obj->size) {
562 err = got_error(GOT_ERR_PRIVSEP_LEN);
563 goto done;
567 if (fstat(outfd, &sb) == -1) {
568 err = got_error_from_errno();
569 goto done;
572 if (sb.st_size != obj->hdrlen + obj->size) {
573 err = got_error(GOT_ERR_PRIVSEP_LEN);
574 goto done;
577 (*blob)->f = fdopen(outfd, "rb");
578 if ((*blob)->f == NULL) {
579 err = got_error_from_errno();
580 close(outfd);
581 goto done;
584 (*blob)->hdrlen = obj->hdrlen;
585 (*blob)->blocksize = blocksize;
586 memcpy(&(*blob)->id.sha1, obj->id.sha1, SHA1_DIGEST_LENGTH);
588 done:
589 if (err) {
590 if (*blob) {
591 if ((*blob)->f)
592 fclose((*blob)->f);
593 free((*blob)->read_buf);
594 free(*blob);
595 *blob = NULL;
596 } else if (outfd != -1)
597 close(outfd);
599 return err;
602 const struct got_error *
603 got_object_open_as_blob(struct got_blob_object **blob,
604 struct got_repository *repo, struct got_object_id *id,
605 size_t blocksize)
607 const struct got_error *err;
608 struct got_object *obj;
610 *blob = NULL;
612 err = got_object_open(&obj, repo, id);
613 if (err)
614 return err;
615 if (got_object_get_type(obj) != GOT_OBJ_TYPE_BLOB) {
616 err = got_error(GOT_ERR_OBJ_TYPE);
617 goto done;
620 err = got_object_blob_open(blob, repo, obj, blocksize);
621 done:
622 got_object_close(obj);
623 return err;
626 void
627 got_object_blob_close(struct got_blob_object *blob)
629 free(blob->read_buf);
630 fclose(blob->f);
631 free(blob);
634 char *
635 got_object_blob_id_str(struct got_blob_object *blob, char *buf, size_t size)
637 return got_sha1_digest_to_str(blob->id.sha1, buf, size);
640 size_t
641 got_object_blob_get_hdrlen(struct got_blob_object *blob)
643 return blob->hdrlen;
646 const uint8_t *
647 got_object_blob_get_read_buf(struct got_blob_object *blob)
649 return blob->read_buf;
652 const struct got_error *
653 got_object_blob_read_block(size_t *outlenp, struct got_blob_object *blob)
655 size_t n;
657 n = fread(blob->read_buf, 1, blob->blocksize, blob->f);
658 if (n == 0 && ferror(blob->f))
659 return got_ferror(blob->f, GOT_ERR_IO);
660 *outlenp = n;
661 return NULL;
664 const struct got_error *
665 got_object_blob_dump_to_file(size_t *total_len, size_t *nlines,
666 FILE *outfile, struct got_blob_object *blob)
668 const struct got_error *err = NULL;
669 size_t len, hdrlen;
670 const uint8_t *buf;
671 int i;
673 if (total_len)
674 *total_len = 0;
675 if (nlines)
676 *nlines = 0;
678 hdrlen = got_object_blob_get_hdrlen(blob);
679 do {
680 err = got_object_blob_read_block(&len, blob);
681 if (err)
682 return err;
683 if (len == 0)
684 break;
685 if (total_len)
686 *total_len += len;
687 buf = got_object_blob_get_read_buf(blob);
688 if (nlines) {
689 for (i = 0; i < len; i++) {
690 if (buf[i] == '\n')
691 (*nlines)++;
694 /* Skip blob object header first time around. */
695 fwrite(buf + hdrlen, len - hdrlen, 1, outfile);
696 hdrlen = 0;
697 } while (len != 0);
699 fflush(outfile);
700 rewind(outfile);
702 return NULL;
705 static const struct got_error *
706 open_tag(struct got_tag_object **tag,
707 struct got_repository *repo, struct got_object *obj, int check_cache)
709 const struct got_error *err = NULL;
711 if (check_cache) {
712 *tag = got_repo_get_cached_tag(repo, &obj->id);
713 if (*tag != NULL) {
714 (*tag)->refcnt++;
715 return NULL;
717 } else
718 *tag = NULL;
720 if (obj->type != GOT_OBJ_TYPE_TAG)
721 return got_error(GOT_ERR_OBJ_TYPE);
723 if (obj->flags & GOT_OBJ_FLAG_PACKED) {
724 struct got_pack *pack;
725 pack = got_repo_get_cached_pack(repo, obj->path_packfile);
726 if (pack == NULL) {
727 err = got_repo_cache_pack(&pack, repo,
728 obj->path_packfile, NULL);
729 if (err)
730 return err;
732 err = got_object_read_packed_tag_privsep(tag, obj, pack);
733 } else {
734 int fd;
735 err = open_loose_object(&fd, obj, repo);
736 if (err)
737 return err;
738 err = got_object_read_tag_privsep(tag, obj, fd, repo);
739 close(fd);
742 if (err == NULL) {
743 (*tag)->refcnt++;
744 err = got_repo_cache_tag(repo, &obj->id, *tag);
747 return err;
750 const struct got_error *
751 got_object_open_as_tag(struct got_tag_object **tag,
752 struct got_repository *repo, struct got_object_id *id)
754 const struct got_error *err;
755 struct got_object *obj;
757 *tag = got_repo_get_cached_tag(repo, id);
758 if (*tag != NULL) {
759 (*tag)->refcnt++;
760 return NULL;
763 err = got_object_open(&obj, repo, id);
764 if (err)
765 return err;
766 if (got_object_get_type(obj) != GOT_OBJ_TYPE_COMMIT) {
767 err = got_error(GOT_ERR_OBJ_TYPE);
768 goto done;
771 err = open_tag(tag, repo, obj, 0);
772 done:
773 got_object_close(obj);
774 return err;
777 const struct got_error *
778 got_object_tag_open(struct got_tag_object **tag,
779 struct got_repository *repo, struct got_object *obj)
781 return open_tag(tag, repo, obj, 1);
784 static struct got_tree_entry *
785 find_entry_by_name(struct got_tree_object *tree, const char *name, size_t len)
787 struct got_tree_entry *te;
789 /* Note that tree entries are sorted in strncmp() order. */
790 SIMPLEQ_FOREACH(te, &tree->entries.head, entry) {
791 int cmp = strncmp(te->name, name, len);
792 if (cmp < 0)
793 continue;
794 if (cmp > 0)
795 break;
796 if (te->name[len] == '\0')
797 return te;
799 return NULL;
802 const struct got_error *
803 got_object_id_by_path(struct got_object_id **id, struct got_repository *repo,
804 struct got_object_id *commit_id, const char *path)
806 const struct got_error *err = NULL;
807 struct got_commit_object *commit = NULL;
808 struct got_tree_object *tree = NULL;
809 struct got_tree_entry *te = NULL;
810 const char *seg, *s;
811 size_t seglen, len = strlen(path);
813 *id = NULL;
815 /* We are expecting an absolute in-repository path. */
816 if (path[0] != '/')
817 return got_error(GOT_ERR_NOT_ABSPATH);
819 err = got_object_open_as_commit(&commit, repo, commit_id);
820 if (err)
821 goto done;
823 /* Handle opening of root of commit's tree. */
824 if (path[1] == '\0') {
825 *id = got_object_id_dup(commit->tree_id);
826 if (*id == NULL)
827 err = got_error_from_errno();
828 goto done;
831 err = got_object_open_as_tree(&tree, repo, commit->tree_id);
832 if (err)
833 goto done;
835 s = path;
836 s++; /* skip leading '/' */
837 len--;
838 seg = s;
839 seglen = 0;
840 while (len > 0) {
841 struct got_tree_object *next_tree;
843 if (*s != '/') {
844 s++;
845 len--;
846 seglen++;
847 if (*s)
848 continue;
851 te = find_entry_by_name(tree, seg, seglen);
852 if (te == NULL) {
853 err = got_error(GOT_ERR_NO_TREE_ENTRY);
854 goto done;
857 if (len == 0)
858 break;
860 seg = s + 1;
861 seglen = 0;
862 s++;
863 len--;
864 if (*s) {
865 err = got_object_open_as_tree(&next_tree, repo,
866 te->id);
867 te = NULL;
868 if (err)
869 goto done;
870 got_object_tree_close(tree);
871 tree = next_tree;
875 if (te) {
876 *id = got_object_id_dup(te->id);
877 if (*id == NULL)
878 return got_error_from_errno();
879 } else
880 err = got_error(GOT_ERR_NO_TREE_ENTRY);
881 done:
882 if (commit)
883 got_object_commit_close(commit);
884 if (tree)
885 got_object_tree_close(tree);
886 return err;
889 const struct got_error *
890 got_object_tree_path_changed(int *changed,
891 struct got_tree_object *tree01, struct got_tree_object *tree02,
892 const char *path, struct got_repository *repo)
894 const struct got_error *err = NULL;
895 struct got_tree_object *tree1 = NULL, *tree2 = NULL;
896 struct got_tree_entry *te1 = NULL, *te2 = NULL;
897 const char *seg, *s;
898 size_t seglen, remain = strlen(path);
900 *changed = 0;
902 /* We are expecting an absolute in-repository path. */
903 if (path[0] != '/')
904 return got_error(GOT_ERR_NOT_ABSPATH);
906 /* We not do support comparing the root path. */
907 if (path[1] == '\0')
908 return got_error(GOT_ERR_BAD_PATH);
910 tree1 = tree01;
911 tree2 = tree02;
912 s = path;
913 s++; /* skip leading '/' */
914 remain--;
915 seg = s;
916 seglen = 0;
917 while (remain > 0) {
918 struct got_tree_object *next_tree1, *next_tree2;
920 if (*s != '/') {
921 s++;
922 remain--;
923 seglen++;
924 if (*s)
925 continue;
928 te1 = find_entry_by_name(tree1, seg, seglen);
929 if (te1 == NULL) {
930 err = got_error(GOT_ERR_NO_OBJ);
931 goto done;
934 te2 = find_entry_by_name(tree2, seg, seglen);
935 if (te2 == NULL) {
936 *changed = 1;
937 goto done;
940 if (te1->mode != te2->mode) {
941 *changed = 1;
942 goto done;
945 if (got_object_id_cmp(te1->id, te2->id) == 0) {
946 *changed = 0;
947 goto done;
950 if (remain == 0) { /* final path element */
951 *changed = 1;
952 goto done;
955 seg = s + 1;
956 s++;
957 remain--;
958 seglen = 0;
959 if (*s) {
960 err = got_object_open_as_tree(&next_tree1, repo,
961 te1->id);
962 te1 = NULL;
963 if (err)
964 goto done;
965 if (tree1 != tree01)
966 got_object_tree_close(tree1);
967 tree1 = next_tree1;
969 err = got_object_open_as_tree(&next_tree2, repo,
970 te2->id);
971 te2 = NULL;
972 if (err)
973 goto done;
974 if (tree2 != tree02)
975 got_object_tree_close(tree2);
976 tree2 = next_tree2;
979 done:
980 if (tree1 && tree1 != tree01)
981 got_object_tree_close(tree1);
982 if (tree2 && tree2 != tree02)
983 got_object_tree_close(tree2);
984 return err;
987 static void
988 exec_privsep_child(int imsg_fds[2], const char *path, const char *repo_path)
990 close(imsg_fds[0]);
992 if (dup2(imsg_fds[1], GOT_IMSG_FD_CHILD) == -1) {
993 fprintf(stderr, "%s: %s\n", getprogname(),
994 strerror(errno));
995 _exit(1);
997 if (closefrom(GOT_IMSG_FD_CHILD + 1) == -1) {
998 fprintf(stderr, "%s: %s\n", getprogname(),
999 strerror(errno));
1000 _exit(1);
1003 if (execl(path, path, repo_path, (char *)NULL) == -1) {
1004 fprintf(stderr, "%s: %s: %s\n", getprogname(), path,
1005 strerror(errno));
1006 _exit(1);
1010 static const struct got_error *
1011 request_object(struct got_object **obj, struct got_repository *repo, int fd)
1013 const struct got_error *err = NULL;
1014 struct imsgbuf *ibuf;
1016 ibuf = repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_OBJECT].ibuf;
1018 err = got_privsep_send_obj_req(ibuf, fd, NULL);
1019 if (err)
1020 return err;
1022 return got_privsep_recv_obj(obj, ibuf);
1025 const struct got_error *
1026 got_object_read_header_privsep(struct got_object **obj,
1027 struct got_repository *repo, int obj_fd)
1029 int imsg_fds[2];
1030 pid_t pid;
1031 struct imsgbuf *ibuf;
1033 if (repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_OBJECT].imsg_fd != -1)
1034 return request_object(obj, repo, obj_fd);
1036 ibuf = calloc(1, sizeof(*ibuf));
1037 if (ibuf == NULL)
1038 return got_error_from_errno();
1040 if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_fds) == -1)
1041 return got_error_from_errno();
1043 pid = fork();
1044 if (pid == -1)
1045 return got_error_from_errno();
1046 else if (pid == 0) {
1047 exec_privsep_child(imsg_fds, GOT_PATH_PROG_READ_OBJECT,
1048 repo->path);
1049 /* not reached */
1052 close(imsg_fds[1]);
1053 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_OBJECT].imsg_fd =
1054 imsg_fds[0];
1055 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_OBJECT].pid = pid;
1056 imsg_init(ibuf, imsg_fds[0]);
1057 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_OBJECT].ibuf = ibuf;
1059 return request_object(obj, repo, obj_fd);
1062 static const struct got_error *
1063 request_packed_object(struct got_object **obj, struct got_pack *pack, int idx,
1064 struct got_object_id *id)
1066 const struct got_error *err = NULL;
1067 struct imsgbuf *ibuf = pack->privsep_child->ibuf;
1069 err = got_privsep_send_packed_obj_req(ibuf, idx, id);
1070 if (err)
1071 return err;
1073 err = got_privsep_recv_obj(obj, ibuf);
1074 if (err)
1075 return err;
1077 (*obj)->path_packfile = strdup(pack->path_packfile);
1078 if ((*obj)->path_packfile == NULL) {
1079 err = got_error_from_errno();
1080 return err;
1082 memcpy(&(*obj)->id, id, sizeof((*obj)->id));
1084 return NULL;
1087 const struct got_error *
1088 got_object_packed_read_privsep(struct got_object **obj,
1089 struct got_repository *repo, struct got_pack *pack,
1090 struct got_packidx *packidx, int idx, struct got_object_id *id)
1092 const struct got_error *err = NULL;
1093 int imsg_fds[2];
1094 pid_t pid;
1095 struct imsgbuf *ibuf;
1097 if (pack->privsep_child)
1098 return request_packed_object(obj, pack, idx, id);
1100 ibuf = calloc(1, sizeof(*ibuf));
1101 if (ibuf == NULL)
1102 return got_error_from_errno();
1104 pack->privsep_child = calloc(1, sizeof(*pack->privsep_child));
1105 if (pack->privsep_child == NULL) {
1106 err = got_error_from_errno();
1107 free(ibuf);
1108 return err;
1111 if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_fds) == -1) {
1112 err = got_error_from_errno();
1113 goto done;
1116 pid = fork();
1117 if (pid == -1) {
1118 err = got_error_from_errno();
1119 goto done;
1120 } else if (pid == 0) {
1121 exec_privsep_child(imsg_fds, GOT_PATH_PROG_READ_PACK,
1122 pack->path_packfile);
1123 /* not reached */
1126 close(imsg_fds[1]);
1127 pack->privsep_child->imsg_fd = imsg_fds[0];
1128 pack->privsep_child->pid = pid;
1129 imsg_init(ibuf, imsg_fds[0]);
1130 pack->privsep_child->ibuf = ibuf;
1132 err = got_privsep_init_pack_child(ibuf, pack, packidx);
1133 if (err) {
1134 const struct got_error *child_err;
1135 err = got_privsep_send_stop(pack->privsep_child->imsg_fd);
1136 child_err = got_privsep_wait_for_child(
1137 pack->privsep_child->pid);
1138 if (child_err && err == NULL)
1139 err = child_err;
1140 free(ibuf);
1141 free(pack->privsep_child);
1142 pack->privsep_child = NULL;
1143 return err;
1146 done:
1147 if (err) {
1148 free(ibuf);
1149 free(pack->privsep_child);
1150 pack->privsep_child = NULL;
1151 } else
1152 err = request_packed_object(obj, pack, idx, id);
1153 return err;
1156 static const struct got_error *
1157 request_commit(struct got_commit_object **commit, struct got_repository *repo,
1158 struct got_object *obj, int fd)
1160 const struct got_error *err = NULL;
1161 struct imsgbuf *ibuf;
1163 ibuf = repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_COMMIT].ibuf;
1165 err = got_privsep_send_obj_req(ibuf, fd, obj);
1166 if (err)
1167 return err;
1169 return got_privsep_recv_commit(commit, ibuf);
1172 const struct got_error *
1173 got_object_read_packed_commit_privsep(struct got_commit_object **commit,
1174 struct got_object *obj, struct got_pack *pack)
1176 const struct got_error *err = NULL;
1178 err = got_privsep_send_obj_req(pack->privsep_child->ibuf, -1, obj);
1179 if (err)
1180 return err;
1182 return got_privsep_recv_commit(commit, pack->privsep_child->ibuf);
1185 const struct got_error *
1186 got_object_read_commit_privsep(struct got_commit_object **commit,
1187 struct got_object *obj, int obj_fd, struct got_repository *repo)
1189 int imsg_fds[2];
1190 pid_t pid;
1191 struct imsgbuf *ibuf;
1193 if (repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_COMMIT].imsg_fd != -1)
1194 return request_commit(commit, repo, obj, obj_fd);
1196 ibuf = calloc(1, sizeof(*ibuf));
1197 if (ibuf == NULL)
1198 return got_error_from_errno();
1200 if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_fds) == -1)
1201 return got_error_from_errno();
1203 pid = fork();
1204 if (pid == -1)
1205 return got_error_from_errno();
1206 else if (pid == 0) {
1207 exec_privsep_child(imsg_fds, GOT_PATH_PROG_READ_COMMIT,
1208 repo->path);
1209 /* not reached */
1212 close(imsg_fds[1]);
1213 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_COMMIT].imsg_fd =
1214 imsg_fds[0];
1215 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_COMMIT].pid = pid;
1216 imsg_init(ibuf, imsg_fds[0]);
1217 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_COMMIT].ibuf = ibuf;
1219 return request_commit(commit, repo, obj, obj_fd);
1222 static const struct got_error *
1223 request_tree(struct got_tree_object **tree, struct got_repository *repo,
1224 struct got_object *obj, int fd)
1226 const struct got_error *err = NULL;
1227 struct imsgbuf *ibuf;
1229 ibuf = repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TREE].ibuf;
1231 err = got_privsep_send_obj_req(ibuf, fd, obj);
1232 if (err)
1233 return err;
1235 return got_privsep_recv_tree(tree, ibuf);
1238 const struct got_error *
1239 got_object_read_tree_privsep(struct got_tree_object **tree,
1240 struct got_object *obj, int obj_fd, struct got_repository *repo)
1242 int imsg_fds[2];
1243 pid_t pid;
1244 struct imsgbuf *ibuf;
1246 if (repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TREE].imsg_fd != -1)
1247 return request_tree(tree, repo, obj, obj_fd);
1249 ibuf = calloc(1, sizeof(*ibuf));
1250 if (ibuf == NULL)
1251 return got_error_from_errno();
1253 if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_fds) == -1)
1254 return got_error_from_errno();
1256 pid = fork();
1257 if (pid == -1)
1258 return got_error_from_errno();
1259 else if (pid == 0) {
1260 exec_privsep_child(imsg_fds, GOT_PATH_PROG_READ_TREE,
1261 repo->path);
1262 /* not reached */
1265 close(imsg_fds[1]);
1267 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TREE].imsg_fd =
1268 imsg_fds[0];
1269 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TREE].pid = pid;
1270 imsg_init(ibuf, imsg_fds[0]);
1271 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TREE].ibuf = ibuf;
1274 return request_tree(tree, repo, obj, obj_fd);
1277 const struct got_error *
1278 got_object_read_packed_tree_privsep(struct got_tree_object **tree,
1279 struct got_object *obj, struct got_pack *pack)
1281 const struct got_error *err = NULL;
1283 err = got_privsep_send_obj_req(pack->privsep_child->ibuf, -1, obj);
1284 if (err)
1285 return err;
1287 return got_privsep_recv_tree(tree, pack->privsep_child->ibuf);
1290 static const struct got_error *
1291 request_blob(size_t *size, int outfd, int infd, struct imsgbuf *ibuf)
1293 const struct got_error *err = NULL;
1294 int outfd_child;
1296 outfd_child = dup(outfd);
1297 if (outfd_child == -1)
1298 return got_error_from_errno();
1300 err = got_privsep_send_blob_req(ibuf, infd);
1301 if (err)
1302 return err;
1304 err = got_privsep_send_blob_outfd(ibuf, outfd_child);
1305 if (err) {
1306 close(outfd_child);
1307 return err;
1310 err = got_privsep_recv_blob(size, ibuf);
1311 if (err)
1312 return err;
1314 if (lseek(outfd, SEEK_SET, 0) == -1)
1315 return got_error_from_errno();
1317 return err;
1320 const struct got_error *
1321 got_object_read_blob_privsep(size_t *size, int outfd, int infd,
1322 struct got_repository *repo)
1324 int imsg_fds[2];
1325 pid_t pid;
1326 struct imsgbuf *ibuf;
1328 if (repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_BLOB].imsg_fd != -1) {
1329 ibuf = repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_BLOB].ibuf;
1330 return request_blob(size, outfd, infd, ibuf);
1333 ibuf = calloc(1, sizeof(*ibuf));
1334 if (ibuf == NULL)
1335 return got_error_from_errno();
1337 if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_fds) == -1)
1338 return got_error_from_errno();
1340 pid = fork();
1341 if (pid == -1)
1342 return got_error_from_errno();
1343 else if (pid == 0) {
1344 exec_privsep_child(imsg_fds, GOT_PATH_PROG_READ_BLOB,
1345 repo->path);
1346 /* not reached */
1349 close(imsg_fds[1]);
1350 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_BLOB].imsg_fd =
1351 imsg_fds[0];
1352 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_BLOB].pid = pid;
1353 imsg_init(ibuf, imsg_fds[0]);
1354 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_BLOB].ibuf = ibuf;
1356 return request_blob(size, outfd, infd, ibuf);
1359 static const struct got_error *
1360 request_tag(struct got_tag_object **tag, struct got_repository *repo,
1361 struct got_object *obj, int fd)
1363 const struct got_error *err = NULL;
1364 struct imsgbuf *ibuf;
1366 ibuf = repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TAG].ibuf;
1368 err = got_privsep_send_obj_req(ibuf, fd, obj);
1369 if (err)
1370 return err;
1372 return got_privsep_recv_tag(tag, ibuf);
1375 const struct got_error *
1376 got_object_read_packed_tag_privsep(struct got_tag_object **tag,
1377 struct got_object *obj, struct got_pack *pack)
1379 const struct got_error *err = NULL;
1381 err = got_privsep_send_obj_req(pack->privsep_child->ibuf, -1, obj);
1382 if (err)
1383 return err;
1385 return got_privsep_recv_tag(tag, pack->privsep_child->ibuf);
1388 const struct got_error *
1389 got_object_read_tag_privsep(struct got_tag_object **tag,
1390 struct got_object *obj, int obj_fd, 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_TAG].imsg_fd != -1)
1397 return request_tag(tag, repo, obj, obj_fd);
1399 ibuf = calloc(1, sizeof(*ibuf));
1400 if (ibuf == NULL)
1401 return got_error_from_errno();
1403 if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_fds) == -1)
1404 return got_error_from_errno();
1406 pid = fork();
1407 if (pid == -1)
1408 return got_error_from_errno();
1409 else if (pid == 0) {
1410 exec_privsep_child(imsg_fds, GOT_PATH_PROG_READ_TAG,
1411 repo->path);
1412 /* not reached */
1415 close(imsg_fds[1]);
1416 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TAG].imsg_fd =
1417 imsg_fds[0];
1418 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TAG].pid = pid;
1419 imsg_init(ibuf, imsg_fds[0]);
1420 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TAG].ibuf = ibuf;
1422 return request_tag(tag, repo, obj, obj_fd);