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 int
60 got_object_id_cmp(struct got_object_id *id1, struct got_object_id *id2)
61 {
62 return memcmp(id1->sha1, id2->sha1, SHA1_DIGEST_LENGTH);
63 }
65 struct got_object_id *
66 got_object_id_dup(struct got_object_id *id1)
67 {
68 struct got_object_id *id2;
70 id2 = malloc(sizeof(*id2));
71 if (id2 == NULL)
72 return NULL;
73 memcpy(id2, id1, sizeof(*id2));
74 return id2;
75 }
77 struct got_object_id *
78 got_object_get_id(struct got_object *obj)
79 {
80 return &obj->id;
81 }
83 const struct got_error *
84 got_object_get_id_str(char **outbuf, struct got_object *obj)
85 {
86 return got_object_id_str(outbuf, &obj->id);
87 }
89 int
90 got_object_get_type(struct got_object *obj)
91 {
92 switch (obj->type) {
93 case GOT_OBJ_TYPE_COMMIT:
94 case GOT_OBJ_TYPE_TREE:
95 case GOT_OBJ_TYPE_BLOB:
96 case GOT_OBJ_TYPE_TAG:
97 return obj->type;
98 default:
99 abort();
100 break;
103 /* not reached */
104 return 0;
107 static const struct got_error *
108 object_path(char **path, struct got_object_id *id, struct got_repository *repo)
110 const struct got_error *err = NULL;
111 char *hex = NULL;
112 char *path_objects = got_repo_get_path_objects(repo);
114 *path = NULL;
116 if (path_objects == NULL)
117 return got_error_from_errno();
119 err = got_object_id_str(&hex, id);
120 if (err)
121 goto done;
123 if (asprintf(path, "%s/%.2x/%s", path_objects,
124 id->sha1[0], hex + 2) == -1)
125 err = got_error_from_errno();
127 done:
128 free(hex);
129 free(path_objects);
130 return err;
133 static const struct got_error *
134 open_loose_object(int *fd, struct got_object *obj, struct got_repository *repo)
136 const struct got_error *err = NULL;
137 char *path;
139 err = object_path(&path, &obj->id, repo);
140 if (err)
141 return err;
142 *fd = open(path, O_RDONLY | O_NOFOLLOW, GOT_DEFAULT_FILE_MODE);
143 if (*fd == -1) {
144 err = got_error_from_errno();
145 goto done;
147 done:
148 free(path);
149 return err;
152 static const struct got_error *
153 get_packfile_path(char **path_packfile, struct got_packidx *packidx)
155 size_t size;
157 /* Packfile path contains ".pack" instead of ".idx", so add one byte. */
158 size = strlen(packidx->path_packidx) + 2;
159 if (size < GOT_PACKFILE_NAMELEN + 1)
160 return got_error(GOT_ERR_BAD_PATH);
162 *path_packfile = calloc(size, sizeof(**path_packfile));
163 if (*path_packfile == NULL)
164 return got_error_from_errno();
166 /* Copy up to and excluding ".idx". */
167 if (strlcpy(*path_packfile, packidx->path_packidx,
168 size - strlen(GOT_PACKIDX_SUFFIX) - 1) >= size)
169 return got_error(GOT_ERR_NO_SPACE);
171 if (strlcat(*path_packfile, GOT_PACKFILE_SUFFIX, size) >= size)
172 return got_error(GOT_ERR_NO_SPACE);
174 return NULL;
177 static const struct got_error *
178 open_packed_object(struct got_object **obj, struct got_object_id *id,
179 struct got_repository *repo)
181 const struct got_error *err = NULL;
182 struct got_pack *pack = NULL;
183 struct got_packidx *packidx = NULL;
184 int idx;
185 char *path_packfile;
187 err = got_repo_search_packidx(&packidx, &idx, repo, id);
188 if (err)
189 return err;
191 err = get_packfile_path(&path_packfile, packidx);
192 if (err)
193 return err;
195 pack = got_repo_get_cached_pack(repo, path_packfile);
196 if (pack == NULL) {
197 err = got_repo_cache_pack(&pack, repo, path_packfile, packidx);
198 if (err)
199 goto done;
202 err = got_object_packed_read_privsep(obj, repo, pack, packidx, idx, id);
203 if (err)
204 goto done;
206 err = got_repo_cache_pack(NULL, repo, (*obj)->path_packfile, packidx);
207 done:
208 free(path_packfile);
209 return err;
212 const struct got_error *
213 got_object_open(struct got_object **obj, struct got_repository *repo,
214 struct got_object_id *id)
216 const struct got_error *err = NULL;
217 char *path;
218 int fd;
220 *obj = got_repo_get_cached_object(repo, id);
221 if (*obj != NULL) {
222 (*obj)->refcnt++;
223 return NULL;
226 err = open_packed_object(obj, id, repo);
227 if (err && err->code != GOT_ERR_NO_OBJ)
228 return err;
229 if (*obj) {
230 (*obj)->refcnt++;
231 return got_repo_cache_object(repo, id, *obj);
234 err = object_path(&path, id, repo);
235 if (err)
236 return err;
238 fd = open(path, O_RDONLY | O_NOFOLLOW, GOT_DEFAULT_FILE_MODE);
239 if (fd == -1) {
240 if (errno == ENOENT)
241 err = got_error(GOT_ERR_NO_OBJ);
242 else
243 err = got_error_from_errno();
244 goto done;
245 } else {
246 err = got_object_read_header_privsep(obj, repo, fd);
247 if (err)
248 goto done;
249 memcpy((*obj)->id.sha1, id->sha1, SHA1_DIGEST_LENGTH);
252 (*obj)->refcnt++;
253 err = got_repo_cache_object(repo, id, *obj);
254 done:
255 free(path);
256 if (fd != -1)
257 close(fd);
258 return err;
262 const struct got_error *
263 got_object_open_by_id_str(struct got_object **obj, struct got_repository *repo,
264 const char *id_str)
266 struct got_object_id id;
268 if (!got_parse_sha1_digest(id.sha1, id_str))
269 return got_error(GOT_ERR_BAD_OBJ_ID_STR);
271 return got_object_open(obj, repo, &id);
274 static const struct got_error *
275 open_commit(struct got_commit_object **commit,
276 struct got_repository *repo, struct got_object *obj, int check_cache)
278 const struct got_error *err = NULL;
280 if (check_cache) {
281 *commit = got_repo_get_cached_commit(repo, &obj->id);
282 if (*commit != NULL) {
283 (*commit)->refcnt++;
284 return NULL;
286 } else
287 *commit = NULL;
289 if (obj->type != GOT_OBJ_TYPE_COMMIT)
290 return got_error(GOT_ERR_OBJ_TYPE);
292 if (obj->flags & GOT_OBJ_FLAG_PACKED) {
293 struct got_pack *pack;
294 pack = got_repo_get_cached_pack(repo, obj->path_packfile);
295 if (pack == NULL) {
296 err = got_repo_cache_pack(&pack, repo,
297 obj->path_packfile, NULL);
298 if (err)
299 return err;
301 err = got_object_read_packed_commit_privsep(commit, obj, pack);
302 } else {
303 int fd;
304 err = open_loose_object(&fd, obj, repo);
305 if (err)
306 return err;
307 err = got_object_read_commit_privsep(commit, obj, fd, repo);
308 close(fd);
311 if (err == NULL) {
312 (*commit)->refcnt++;
313 err = got_repo_cache_commit(repo, &obj->id, *commit);
316 return err;
319 static const struct got_error *
320 open_mini_commit(struct got_commit_object_mini **commit,
321 struct got_repository *repo, struct got_object *obj)
323 const struct got_error *err = NULL;
325 *commit = NULL;
327 if (obj->type != GOT_OBJ_TYPE_COMMIT)
328 return got_error(GOT_ERR_OBJ_TYPE);
330 if (obj->flags & GOT_OBJ_FLAG_PACKED) {
331 struct got_pack *pack;
332 pack = got_repo_get_cached_pack(repo, obj->path_packfile);
333 if (pack == NULL) {
334 err = got_repo_cache_pack(&pack, repo,
335 obj->path_packfile, NULL);
336 if (err)
337 return err;
339 err = got_object_read_packed_mini_commit_privsep(commit, obj,
340 pack);
341 } else {
342 int fd;
343 err = open_loose_object(&fd, obj, repo);
344 if (err)
345 return err;
346 err = got_object_read_mini_commit_privsep(commit, obj, fd,
347 repo);
348 close(fd);
351 return err;
354 const struct got_error *
355 got_object_open_as_commit(struct got_commit_object **commit,
356 struct got_repository *repo, struct got_object_id *id)
358 const struct got_error *err;
359 struct got_object *obj;
361 *commit = got_repo_get_cached_commit(repo, id);
362 if (*commit != NULL) {
363 (*commit)->refcnt++;
364 return NULL;
367 err = got_object_open(&obj, repo, id);
368 if (err)
369 return err;
370 if (got_object_get_type(obj) != GOT_OBJ_TYPE_COMMIT) {
371 err = got_error(GOT_ERR_OBJ_TYPE);
372 goto done;
375 err = open_commit(commit, repo, obj, 0);
376 done:
377 got_object_close(obj);
378 return err;
381 const struct got_error *
382 got_object_commit_open(struct got_commit_object **commit,
383 struct got_repository *repo, struct got_object *obj)
385 return open_commit(commit, repo, obj, 1);
388 const struct got_error *
389 got_object_open_mini_commit(struct got_commit_object_mini **commit,
390 struct got_repository *repo, struct got_object_id *id)
392 const struct got_error *err;
393 struct got_object *obj;
395 err = got_object_open(&obj, repo, id);
396 if (err)
397 return err;
398 if (got_object_get_type(obj) != GOT_OBJ_TYPE_COMMIT) {
399 err = got_error(GOT_ERR_OBJ_TYPE);
400 goto done;
403 err = open_mini_commit(commit, repo, obj);
404 done:
405 got_object_close(obj);
406 return err;
409 const struct got_error *
410 got_object_qid_alloc(struct got_object_qid **qid, struct got_object_id *id)
412 const struct got_error *err = NULL;
414 *qid = calloc(1, sizeof(**qid));
415 if (*qid == NULL)
416 return got_error_from_errno();
418 (*qid)->id = got_object_id_dup(id);
419 if ((*qid)->id == NULL) {
420 err = got_error_from_errno();
421 got_object_qid_free(*qid);
422 *qid = NULL;
423 return err;
426 return NULL;
429 static const struct got_error *
430 open_tree(struct got_tree_object **tree,
431 struct got_repository *repo, struct got_object *obj, int check_cache)
433 const struct got_error *err = NULL;
435 if (check_cache) {
436 *tree = got_repo_get_cached_tree(repo, &obj->id);
437 if (*tree != NULL) {
438 (*tree)->refcnt++;
439 return NULL;
441 } else
442 *tree = NULL;
444 if (obj->type != GOT_OBJ_TYPE_TREE)
445 return got_error(GOT_ERR_OBJ_TYPE);
447 if (obj->flags & GOT_OBJ_FLAG_PACKED) {
448 struct got_pack *pack;
449 pack = got_repo_get_cached_pack(repo, obj->path_packfile);
450 if (pack == NULL) {
451 err = got_repo_cache_pack(&pack, repo,
452 obj->path_packfile, NULL);
453 if (err)
454 return err;
456 err = got_object_read_packed_tree_privsep(tree, obj, pack);
457 } else {
458 int fd;
459 err = open_loose_object(&fd, obj, repo);
460 if (err)
461 return err;
462 err = got_object_read_tree_privsep(tree, obj, fd, repo);
463 close(fd);
466 if (err == NULL) {
467 (*tree)->refcnt++;
468 err = got_repo_cache_tree(repo, &obj->id, *tree);
471 return err;
474 const struct got_error *
475 got_object_open_as_tree(struct got_tree_object **tree,
476 struct got_repository *repo, struct got_object_id *id)
478 const struct got_error *err;
479 struct got_object *obj;
481 *tree = got_repo_get_cached_tree(repo, id);
482 if (*tree != NULL) {
483 (*tree)->refcnt++;
484 return NULL;
487 err = got_object_open(&obj, repo, id);
488 if (err)
489 return err;
490 if (got_object_get_type(obj) != GOT_OBJ_TYPE_TREE) {
491 err = got_error(GOT_ERR_OBJ_TYPE);
492 goto done;
495 err = open_tree(tree, repo, obj, 0);
496 done:
497 got_object_close(obj);
498 return err;
501 const struct got_error *
502 got_object_tree_open(struct got_tree_object **tree,
503 struct got_repository *repo, struct got_object *obj)
505 return open_tree(tree, repo, obj, 1);
508 const struct got_tree_entries *
509 got_object_tree_get_entries(struct got_tree_object *tree)
511 return &tree->entries;
514 static const struct got_error *
515 read_packed_blob_privsep(size_t *size, int outfd, struct got_object *obj,
516 struct got_pack *pack)
518 const struct got_error *err = NULL;
519 int outfd_child;
520 int basefd, accumfd; /* temporary files for delta application */
522 basefd = got_opentempfd();
523 if (basefd == -1)
524 return got_error_from_errno();
525 accumfd = got_opentempfd();
526 if (accumfd == -1)
527 return got_error_from_errno();
529 outfd_child = dup(outfd);
530 if (outfd_child == -1)
531 return got_error_from_errno();
533 err = got_privsep_send_obj_req(pack->privsep_child->ibuf, -1, obj);
534 if (err)
535 return err;
537 err = got_privsep_send_blob_outfd(pack->privsep_child->ibuf,
538 outfd_child);
539 if (err) {
540 close(outfd_child);
541 return err;
543 err = got_privsep_send_tmpfd(pack->privsep_child->ibuf,
544 basefd);
545 if (err) {
546 close(basefd);
547 close(accumfd);
548 close(outfd_child);
549 return err;
552 err = got_privsep_send_tmpfd(pack->privsep_child->ibuf,
553 accumfd);
554 if (err) {
555 close(accumfd);
556 close(outfd_child);
557 return err;
560 err = got_privsep_recv_blob(size, pack->privsep_child->ibuf);
561 if (err)
562 return err;
564 if (lseek(outfd, SEEK_SET, 0) == -1)
565 err = got_error_from_errno();
567 return err;
570 const struct got_error *
571 got_object_blob_open(struct got_blob_object **blob,
572 struct got_repository *repo, struct got_object *obj, size_t blocksize)
574 const struct got_error *err = NULL;
575 int outfd;
576 size_t size;
577 struct stat sb;
579 if (obj->type != GOT_OBJ_TYPE_BLOB)
580 return got_error(GOT_ERR_OBJ_TYPE);
582 if (blocksize < obj->hdrlen)
583 return got_error(GOT_ERR_NO_SPACE);
585 *blob = calloc(1, sizeof(**blob));
586 if (*blob == NULL)
587 return got_error_from_errno();
589 outfd = got_opentempfd();
590 if (outfd == -1)
591 return got_error_from_errno();
593 (*blob)->read_buf = malloc(blocksize);
594 if ((*blob)->read_buf == NULL) {
595 err = got_error_from_errno();
596 goto done;
598 if (obj->flags & GOT_OBJ_FLAG_PACKED) {
599 struct got_pack *pack;
600 pack = got_repo_get_cached_pack(repo, obj->path_packfile);
601 if (pack == NULL) {
602 err = got_repo_cache_pack(&pack, repo,
603 obj->path_packfile, NULL);
604 if (err)
605 goto done;
607 err = read_packed_blob_privsep(&size, outfd, obj, pack);
608 if (err)
609 goto done;
610 obj->size = size;
611 } else {
612 int infd;
614 err = open_loose_object(&infd, obj, repo);
615 if (err)
616 goto done;
618 err = got_object_read_blob_privsep(&size, outfd, infd, repo);
619 close(infd);
620 if (err)
621 goto done;
623 if (size != obj->hdrlen + obj->size) {
624 err = got_error(GOT_ERR_PRIVSEP_LEN);
625 goto done;
629 if (fstat(outfd, &sb) == -1) {
630 err = got_error_from_errno();
631 goto done;
634 if (sb.st_size != obj->hdrlen + obj->size) {
635 err = got_error(GOT_ERR_PRIVSEP_LEN);
636 goto done;
639 (*blob)->f = fdopen(outfd, "rb");
640 if ((*blob)->f == NULL) {
641 err = got_error_from_errno();
642 close(outfd);
643 goto done;
646 (*blob)->hdrlen = obj->hdrlen;
647 (*blob)->blocksize = blocksize;
648 memcpy(&(*blob)->id.sha1, obj->id.sha1, SHA1_DIGEST_LENGTH);
650 done:
651 if (err) {
652 if (*blob) {
653 if ((*blob)->f)
654 fclose((*blob)->f);
655 free((*blob)->read_buf);
656 free(*blob);
657 *blob = NULL;
658 } else if (outfd != -1)
659 close(outfd);
661 return err;
664 const struct got_error *
665 got_object_open_as_blob(struct got_blob_object **blob,
666 struct got_repository *repo, struct got_object_id *id,
667 size_t blocksize)
669 const struct got_error *err;
670 struct got_object *obj;
672 *blob = NULL;
674 err = got_object_open(&obj, repo, id);
675 if (err)
676 return err;
677 if (got_object_get_type(obj) != GOT_OBJ_TYPE_BLOB) {
678 err = got_error(GOT_ERR_OBJ_TYPE);
679 goto done;
682 err = got_object_blob_open(blob, repo, obj, blocksize);
683 done:
684 got_object_close(obj);
685 return err;
688 void
689 got_object_blob_close(struct got_blob_object *blob)
691 free(blob->read_buf);
692 fclose(blob->f);
693 free(blob);
696 char *
697 got_object_blob_id_str(struct got_blob_object *blob, char *buf, size_t size)
699 return got_sha1_digest_to_str(blob->id.sha1, buf, size);
702 size_t
703 got_object_blob_get_hdrlen(struct got_blob_object *blob)
705 return blob->hdrlen;
708 const uint8_t *
709 got_object_blob_get_read_buf(struct got_blob_object *blob)
711 return blob->read_buf;
714 const struct got_error *
715 got_object_blob_read_block(size_t *outlenp, struct got_blob_object *blob)
717 size_t n;
719 n = fread(blob->read_buf, 1, blob->blocksize, blob->f);
720 if (n == 0 && ferror(blob->f))
721 return got_ferror(blob->f, GOT_ERR_IO);
722 *outlenp = n;
723 return NULL;
726 const struct got_error *
727 got_object_blob_dump_to_file(size_t *total_len, size_t *nlines,
728 FILE *outfile, struct got_blob_object *blob)
730 const struct got_error *err = NULL;
731 size_t len, hdrlen;
732 const uint8_t *buf;
733 int i;
735 if (total_len)
736 *total_len = 0;
737 if (nlines)
738 *nlines = 0;
740 hdrlen = got_object_blob_get_hdrlen(blob);
741 do {
742 err = got_object_blob_read_block(&len, blob);
743 if (err)
744 return err;
745 if (len == 0)
746 break;
747 if (total_len)
748 *total_len += len;
749 buf = got_object_blob_get_read_buf(blob);
750 if (nlines) {
751 for (i = 0; i < len; i++) {
752 if (buf[i] == '\n')
753 (*nlines)++;
756 /* Skip blob object header first time around. */
757 fwrite(buf + hdrlen, len - hdrlen, 1, outfile);
758 hdrlen = 0;
759 } while (len != 0);
761 fflush(outfile);
762 rewind(outfile);
764 return NULL;
767 static struct got_tree_entry *
768 find_entry_by_name(struct got_tree_object *tree, const char *name, size_t len)
770 struct got_tree_entry *te;
772 SIMPLEQ_FOREACH(te, &tree->entries.head, entry) {
773 if (strncmp(te->name, name, len) == 0 && te->name[len] == '\0')
774 return te;
776 return NULL;
779 const struct got_error *
780 got_object_id_by_path(struct got_object_id **id, struct got_repository *repo,
781 struct got_object_id *commit_id, const char *path)
783 const struct got_error *err = NULL;
784 struct got_commit_object *commit = NULL;
785 struct got_tree_object *tree = NULL;
786 struct got_tree_entry *te = NULL;
787 const char *seg, *s;
788 size_t seglen, len = strlen(path);
790 *id = NULL;
792 /* We are expecting an absolute in-repository path. */
793 if (path[0] != '/')
794 return got_error(GOT_ERR_NOT_ABSPATH);
796 err = got_object_open_as_commit(&commit, repo, commit_id);
797 if (err)
798 goto done;
800 /* Handle opening of root of commit's tree. */
801 if (path[1] == '\0') {
802 *id = got_object_id_dup(commit->tree_id);
803 if (*id == NULL)
804 err = got_error_from_errno();
805 goto done;
808 err = got_object_open_as_tree(&tree, repo, commit->tree_id);
809 if (err)
810 goto done;
812 s = path;
813 s++; /* skip leading '/' */
814 len--;
815 seg = s;
816 seglen = 0;
817 while (len > 0) {
818 struct got_tree_object *next_tree;
820 if (*s != '/') {
821 s++;
822 len--;
823 seglen++;
824 if (*s)
825 continue;
828 te = find_entry_by_name(tree, seg, seglen);
829 if (te == NULL) {
830 err = got_error(GOT_ERR_NO_OBJ);
831 goto done;
834 if (len == 0)
835 break;
837 seg = s + 1;
838 seglen = 0;
839 s++;
840 len--;
841 if (*s) {
842 err = got_object_open_as_tree(&next_tree, repo,
843 te->id);
844 te = NULL;
845 if (err)
846 goto done;
847 got_object_tree_close(tree);
848 tree = next_tree;
852 if (te) {
853 *id = got_object_id_dup(te->id);
854 if (*id == NULL)
855 return got_error_from_errno();
856 } else
857 err = got_error(GOT_ERR_NO_OBJ);
858 done:
859 if (commit)
860 got_object_commit_close(commit);
861 if (tree)
862 got_object_tree_close(tree);
863 return err;
866 const struct got_error *
867 got_object_tree_path_changed(int *changed,
868 struct got_tree_object *tree01, struct got_tree_object *tree02,
869 const char *path, struct got_repository *repo)
871 const struct got_error *err = NULL;
872 struct got_tree_object *tree1 = NULL, *tree2 = NULL;
873 struct got_tree_entry *te1 = NULL, *te2 = NULL;
874 const char *seg, *s;
875 size_t seglen, len = strlen(path);
877 *changed = 0;
879 /* We are expecting an absolute in-repository path. */
880 if (path[0] != '/')
881 return got_error(GOT_ERR_NOT_ABSPATH);
883 /* We not do support comparing the root path. */
884 if (path[1] == '\0')
885 return got_error(GOT_ERR_BAD_PATH);
887 tree1 = tree01;
888 tree2 = tree02;
889 s = path;
890 s++; /* skip leading '/' */
891 len--;
892 seg = s;
893 seglen = 0;
894 while (len > 0) {
895 struct got_tree_object *next_tree1, *next_tree2;
897 if (*s != '/') {
898 s++;
899 len--;
900 seglen++;
901 if (*s)
902 continue;
905 te1 = find_entry_by_name(tree1, seg, seglen);
906 if (te1 == NULL) {
907 err = got_error(GOT_ERR_NO_OBJ);
908 goto done;
911 te2 = find_entry_by_name(tree2, seg, seglen);
912 if (te2 == NULL) {
913 *changed = 1;
914 goto done;
917 if (te1->mode != te2->mode) {
918 *changed = 1;
919 goto done;
922 if (got_object_id_cmp(te1->id, te2->id) == 0) {
923 *changed = 0;
924 goto done;
927 if (len == 0) { /* final path element */
928 *changed = 1;
929 goto done;
932 seg = s + 1;
933 s++;
934 len--;
935 seglen = 0;
936 if (*s) {
937 err = got_object_open_as_tree(&next_tree1, repo,
938 te1->id);
939 te1 = NULL;
940 if (err)
941 goto done;
942 if (tree1 != tree01)
943 got_object_tree_close(tree1);
944 tree1 = next_tree1;
946 err = got_object_open_as_tree(&next_tree2, repo,
947 te2->id);
948 te2 = NULL;
949 if (err)
950 goto done;
951 if (tree2 != tree02)
952 got_object_tree_close(tree2);
953 tree2 = next_tree2;
956 done:
957 if (tree1 && tree1 != tree01)
958 got_object_tree_close(tree1);
959 if (tree2 && tree2 != tree02)
960 got_object_tree_close(tree2);
961 return err;
964 static void
965 exec_privsep_child(int imsg_fds[2], const char *path, const char *repo_path)
967 close(imsg_fds[0]);
969 if (dup2(imsg_fds[1], GOT_IMSG_FD_CHILD) == -1) {
970 fprintf(stderr, "%s: %s\n", getprogname(),
971 strerror(errno));
972 _exit(1);
974 if (closefrom(GOT_IMSG_FD_CHILD + 1) == -1) {
975 fprintf(stderr, "%s: %s\n", getprogname(),
976 strerror(errno));
977 _exit(1);
980 if (execl(path, path, repo_path, (char *)NULL) == -1) {
981 fprintf(stderr, "%s: %s: %s\n", getprogname(), path,
982 strerror(errno));
983 _exit(1);
987 static const struct got_error *
988 request_object(struct got_object **obj, struct got_repository *repo, int fd)
990 const struct got_error *err = NULL;
991 struct imsgbuf *ibuf;
993 ibuf = repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_OBJECT].ibuf;
995 err = got_privsep_send_obj_req(ibuf, fd, NULL);
996 if (err)
997 return err;
999 return got_privsep_recv_obj(obj, ibuf);
1002 const struct got_error *
1003 got_object_read_header_privsep(struct got_object **obj,
1004 struct got_repository *repo, int obj_fd)
1006 int imsg_fds[2];
1007 pid_t pid;
1008 struct imsgbuf *ibuf;
1010 if (repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_OBJECT].imsg_fd != -1)
1011 return request_object(obj, repo, obj_fd);
1013 ibuf = calloc(1, sizeof(*ibuf));
1014 if (ibuf == NULL)
1015 return got_error_from_errno();
1017 if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_fds) == -1)
1018 return got_error_from_errno();
1020 pid = fork();
1021 if (pid == -1)
1022 return got_error_from_errno();
1023 else if (pid == 0) {
1024 exec_privsep_child(imsg_fds, GOT_PATH_PROG_READ_OBJECT,
1025 repo->path);
1026 /* not reached */
1029 close(imsg_fds[1]);
1030 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_OBJECT].imsg_fd =
1031 imsg_fds[0];
1032 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_OBJECT].pid = pid;
1033 imsg_init(ibuf, imsg_fds[0]);
1034 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_OBJECT].ibuf = ibuf;
1036 return request_object(obj, repo, obj_fd);
1039 static const struct got_error *
1040 request_packed_object(struct got_object **obj, struct got_pack *pack, int idx,
1041 struct got_object_id *id)
1043 const struct got_error *err = NULL;
1044 struct imsgbuf *ibuf = pack->privsep_child->ibuf;
1046 err = got_privsep_send_packed_obj_req(ibuf, idx, id);
1047 if (err)
1048 return err;
1050 err = got_privsep_recv_obj(obj, ibuf);
1051 if (err)
1052 return err;
1054 (*obj)->path_packfile = strdup(pack->path_packfile);
1055 if ((*obj)->path_packfile == NULL) {
1056 err = got_error_from_errno();
1057 return err;
1059 memcpy(&(*obj)->id, id, sizeof((*obj)->id));
1061 return NULL;
1064 const struct got_error *
1065 got_object_packed_read_privsep(struct got_object **obj,
1066 struct got_repository *repo, struct got_pack *pack,
1067 struct got_packidx *packidx, int idx, struct got_object_id *id)
1069 const struct got_error *err = NULL;
1070 int imsg_fds[2];
1071 pid_t pid;
1072 struct imsgbuf *ibuf;
1074 if (pack->privsep_child)
1075 return request_packed_object(obj, pack, idx, id);
1077 ibuf = calloc(1, sizeof(*ibuf));
1078 if (ibuf == NULL)
1079 return got_error_from_errno();
1081 pack->privsep_child = calloc(1, sizeof(*pack->privsep_child));
1082 if (pack->privsep_child == NULL) {
1083 err = got_error_from_errno();
1084 free(ibuf);
1085 return err;
1088 if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_fds) == -1) {
1089 err = got_error_from_errno();
1090 goto done;
1093 pid = fork();
1094 if (pid == -1) {
1095 err = got_error_from_errno();
1096 goto done;
1097 } else if (pid == 0) {
1098 exec_privsep_child(imsg_fds, GOT_PATH_PROG_READ_PACK,
1099 pack->path_packfile);
1100 /* not reached */
1103 close(imsg_fds[1]);
1104 pack->privsep_child->imsg_fd = imsg_fds[0];
1105 pack->privsep_child->pid = pid;
1106 imsg_init(ibuf, imsg_fds[0]);
1107 pack->privsep_child->ibuf = ibuf;
1109 err = got_privsep_init_pack_child(ibuf, pack, packidx);
1110 if (err) {
1111 const struct got_error *child_err;
1112 err = got_privsep_send_stop(pack->privsep_child->imsg_fd);
1113 child_err = got_privsep_wait_for_child(
1114 pack->privsep_child->pid);
1115 if (child_err && err == NULL)
1116 err = child_err;
1117 free(ibuf);
1118 free(pack->privsep_child);
1119 pack->privsep_child = NULL;
1120 return err;
1123 done:
1124 if (err) {
1125 free(ibuf);
1126 free(pack->privsep_child);
1127 pack->privsep_child = NULL;
1128 } else
1129 err = request_packed_object(obj, pack, idx, id);
1130 return err;
1133 static const struct got_error *
1134 request_commit(struct got_commit_object **commit, struct got_repository *repo,
1135 struct got_object *obj, int fd)
1137 const struct got_error *err = NULL;
1138 struct imsgbuf *ibuf;
1140 ibuf = repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_COMMIT].ibuf;
1142 err = got_privsep_send_obj_req(ibuf, fd, obj);
1143 if (err)
1144 return err;
1146 return got_privsep_recv_commit(commit, ibuf);
1149 static const struct got_error *
1150 request_mini_commit(struct got_commit_object_mini **commit,
1151 struct got_repository *repo, struct got_object *obj, int fd)
1153 const struct got_error *err = NULL;
1154 struct imsgbuf *ibuf;
1156 ibuf = repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_COMMIT].ibuf;
1158 err = got_privsep_send_mini_commit_req(ibuf, fd, obj);
1159 if (err)
1160 return err;
1162 return got_privsep_recv_mini_commit(commit, ibuf);
1165 const struct got_error *
1166 got_object_read_packed_commit_privsep(struct got_commit_object **commit,
1167 struct got_object *obj, struct got_pack *pack)
1169 const struct got_error *err = NULL;
1171 err = got_privsep_send_obj_req(pack->privsep_child->ibuf, -1, obj);
1172 if (err)
1173 return err;
1175 return got_privsep_recv_commit(commit, pack->privsep_child->ibuf);
1178 const struct got_error *
1179 got_object_read_packed_mini_commit_privsep(struct got_commit_object_mini **commit,
1180 struct got_object *obj, struct got_pack *pack)
1182 const struct got_error *err = NULL;
1184 err = got_privsep_send_mini_commit_req(pack->privsep_child->ibuf, -1,
1185 obj);
1186 if (err)
1187 return err;
1189 return got_privsep_recv_mini_commit(commit, pack->privsep_child->ibuf);
1192 const struct got_error *
1193 got_object_read_commit_privsep(struct got_commit_object **commit,
1194 struct got_object *obj, int obj_fd, struct got_repository *repo)
1196 int imsg_fds[2];
1197 pid_t pid;
1198 struct imsgbuf *ibuf;
1200 if (repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_COMMIT].imsg_fd != -1)
1201 return request_commit(commit, repo, obj, obj_fd);
1203 ibuf = calloc(1, sizeof(*ibuf));
1204 if (ibuf == NULL)
1205 return got_error_from_errno();
1207 if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_fds) == -1)
1208 return got_error_from_errno();
1210 pid = fork();
1211 if (pid == -1)
1212 return got_error_from_errno();
1213 else if (pid == 0) {
1214 exec_privsep_child(imsg_fds, GOT_PATH_PROG_READ_COMMIT,
1215 repo->path);
1216 /* not reached */
1219 close(imsg_fds[1]);
1220 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_COMMIT].imsg_fd =
1221 imsg_fds[0];
1222 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_COMMIT].pid = pid;
1223 imsg_init(ibuf, imsg_fds[0]);
1224 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_COMMIT].ibuf = ibuf;
1226 return request_commit(commit, repo, obj, obj_fd);
1229 const struct got_error *
1230 got_object_read_mini_commit_privsep(struct got_commit_object_mini **commit,
1231 struct got_object *obj, int obj_fd, struct got_repository *repo)
1233 int imsg_fds[2];
1234 pid_t pid;
1235 struct imsgbuf *ibuf;
1237 if (repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_COMMIT].imsg_fd != -1)
1238 return request_mini_commit(commit, repo, obj, obj_fd);
1240 ibuf = calloc(1, sizeof(*ibuf));
1241 if (ibuf == NULL)
1242 return got_error_from_errno();
1244 if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_fds) == -1)
1245 return got_error_from_errno();
1247 pid = fork();
1248 if (pid == -1)
1249 return got_error_from_errno();
1250 else if (pid == 0) {
1251 exec_privsep_child(imsg_fds, GOT_PATH_PROG_READ_COMMIT,
1252 repo->path);
1253 /* not reached */
1256 close(imsg_fds[1]);
1257 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_COMMIT].imsg_fd =
1258 imsg_fds[0];
1259 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_COMMIT].pid = pid;
1260 imsg_init(ibuf, imsg_fds[0]);
1261 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_COMMIT].ibuf = ibuf;
1263 return request_mini_commit(commit, repo, obj, obj_fd);
1266 static const struct got_error *
1267 request_tree(struct got_tree_object **tree, struct got_repository *repo,
1268 struct got_object *obj, int fd)
1270 const struct got_error *err = NULL;
1271 struct imsgbuf *ibuf;
1273 ibuf = repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TREE].ibuf;
1275 err = got_privsep_send_obj_req(ibuf, fd, obj);
1276 if (err)
1277 return err;
1279 return got_privsep_recv_tree(tree, ibuf);
1282 const struct got_error *
1283 got_object_read_tree_privsep(struct got_tree_object **tree,
1284 struct got_object *obj, int obj_fd, struct got_repository *repo)
1286 int imsg_fds[2];
1287 pid_t pid;
1288 struct imsgbuf *ibuf;
1290 if (repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TREE].imsg_fd != -1)
1291 return request_tree(tree, repo, obj, obj_fd);
1293 ibuf = calloc(1, sizeof(*ibuf));
1294 if (ibuf == NULL)
1295 return got_error_from_errno();
1297 if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_fds) == -1)
1298 return got_error_from_errno();
1300 pid = fork();
1301 if (pid == -1)
1302 return got_error_from_errno();
1303 else if (pid == 0) {
1304 exec_privsep_child(imsg_fds, GOT_PATH_PROG_READ_TREE,
1305 repo->path);
1306 /* not reached */
1309 close(imsg_fds[1]);
1311 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TREE].imsg_fd =
1312 imsg_fds[0];
1313 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TREE].pid = pid;
1314 imsg_init(ibuf, imsg_fds[0]);
1315 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TREE].ibuf = ibuf;
1318 return request_tree(tree, repo, obj, obj_fd);
1321 const struct got_error *
1322 got_object_read_packed_tree_privsep(struct got_tree_object **tree,
1323 struct got_object *obj, struct got_pack *pack)
1325 const struct got_error *err = NULL;
1327 err = got_privsep_send_obj_req(pack->privsep_child->ibuf, -1, obj);
1328 if (err)
1329 return err;
1331 return got_privsep_recv_tree(tree, pack->privsep_child->ibuf);
1334 static const struct got_error *
1335 request_blob(size_t *size, int outfd, int infd, struct imsgbuf *ibuf)
1337 const struct got_error *err = NULL;
1338 int outfd_child;
1340 outfd_child = dup(outfd);
1341 if (outfd_child == -1)
1342 return got_error_from_errno();
1344 err = got_privsep_send_blob_req(ibuf, infd);
1345 if (err)
1346 return err;
1348 err = got_privsep_send_blob_outfd(ibuf, outfd_child);
1349 if (err) {
1350 close(outfd_child);
1351 return err;
1354 err = got_privsep_recv_blob(size, ibuf);
1355 if (err)
1356 return err;
1358 if (lseek(outfd, SEEK_SET, 0) == -1)
1359 return got_error_from_errno();
1361 return err;
1364 const struct got_error *
1365 got_object_read_blob_privsep(size_t *size, int outfd, int infd,
1366 struct got_repository *repo)
1368 int imsg_fds[2];
1369 pid_t pid;
1370 struct imsgbuf *ibuf;
1372 if (repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_BLOB].imsg_fd != -1) {
1373 ibuf = repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_BLOB].ibuf;
1374 return request_blob(size, outfd, infd, ibuf);
1377 ibuf = calloc(1, sizeof(*ibuf));
1378 if (ibuf == NULL)
1379 return got_error_from_errno();
1381 if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_fds) == -1)
1382 return got_error_from_errno();
1384 pid = fork();
1385 if (pid == -1)
1386 return got_error_from_errno();
1387 else if (pid == 0) {
1388 exec_privsep_child(imsg_fds, GOT_PATH_PROG_READ_BLOB,
1389 repo->path);
1390 /* not reached */
1393 close(imsg_fds[1]);
1394 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_BLOB].imsg_fd =
1395 imsg_fds[0];
1396 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_BLOB].pid = pid;
1397 imsg_init(ibuf, imsg_fds[0]);
1398 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_BLOB].ibuf = ibuf;
1400 return request_blob(size, outfd, infd, ibuf);