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 const struct got_error *
182 open_packed_object(struct got_object **obj, struct got_object_id *id,
183 struct got_repository *repo)
185 const struct got_error *err = NULL;
186 struct got_pack *pack = NULL;
187 struct got_packidx *packidx = NULL;
188 int idx;
189 char *path_packfile;
191 err = got_repo_search_packidx(&packidx, &idx, repo, id);
192 if (err)
193 return err;
195 err = get_packfile_path(&path_packfile, packidx);
196 if (err)
197 return err;
199 pack = got_repo_get_cached_pack(repo, path_packfile);
200 if (pack == NULL) {
201 err = got_repo_cache_pack(&pack, repo, path_packfile, packidx);
202 if (err)
203 goto done;
206 err = got_object_packed_read_privsep(obj, repo, pack, packidx, idx, id);
207 if (err)
208 goto done;
210 err = got_repo_cache_pack(NULL, repo, (*obj)->path_packfile, packidx);
211 done:
212 free(path_packfile);
213 return err;
216 const struct got_error *
217 got_object_open(struct got_object **obj, struct got_repository *repo,
218 struct got_object_id *id)
220 const struct got_error *err = NULL;
221 char *path;
222 int fd;
224 *obj = got_repo_get_cached_object(repo, id);
225 if (*obj != NULL) {
226 (*obj)->refcnt++;
227 return NULL;
230 err = open_packed_object(obj, id, repo);
231 if (err && err->code != GOT_ERR_NO_OBJ)
232 return err;
233 if (*obj) {
234 (*obj)->refcnt++;
235 return got_repo_cache_object(repo, id, *obj);
238 err = object_path(&path, id, repo);
239 if (err)
240 return err;
242 fd = open(path, O_RDONLY | O_NOFOLLOW, GOT_DEFAULT_FILE_MODE);
243 if (fd == -1) {
244 if (errno == ENOENT)
245 err = got_error_no_obj(id);
246 else
247 err = got_error_from_errno();
248 goto done;
249 } else {
250 err = got_object_read_header_privsep(obj, repo, fd);
251 if (err)
252 goto done;
253 memcpy((*obj)->id.sha1, id->sha1, SHA1_DIGEST_LENGTH);
256 (*obj)->refcnt++;
257 err = got_repo_cache_object(repo, id, *obj);
258 done:
259 free(path);
260 if (fd != -1)
261 close(fd);
262 return err;
266 const struct got_error *
267 got_object_open_by_id_str(struct got_object **obj, struct got_repository *repo,
268 const char *id_str)
270 struct got_object_id id;
272 if (!got_parse_sha1_digest(id.sha1, id_str))
273 return got_error(GOT_ERR_BAD_OBJ_ID_STR);
275 return got_object_open(obj, repo, &id);
278 const struct got_error *
279 got_object_resolve_id_str(struct got_object_id **id,
280 struct got_repository *repo, const char *id_str)
282 const struct got_error *err = NULL;
283 struct got_object *obj;
285 err = got_object_open_by_id_str(&obj, repo, id_str);
286 if (err)
287 return err;
289 *id = got_object_id_dup(got_object_get_id(obj));
290 got_object_close(obj);
291 if (*id == NULL)
292 return got_error_from_errno();
294 return NULL;
297 static const struct got_error *
298 open_commit(struct got_commit_object **commit,
299 struct got_repository *repo, struct got_object_id *id, int check_cache)
301 const struct got_error *err = NULL;
302 struct got_packidx *packidx = NULL;
303 int idx;
304 char *path_packfile;
306 if (check_cache) {
307 *commit = got_repo_get_cached_commit(repo, id);
308 if (*commit != NULL) {
309 (*commit)->refcnt++;
310 return NULL;
312 } else
313 *commit = NULL;
315 err = got_repo_search_packidx(&packidx, &idx, repo, id);
316 if (err == NULL) {
317 struct got_pack *pack = NULL;
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,
326 packidx);
327 if (err)
328 return err;
330 err = got_object_read_packed_commit_privsep(commit, pack,
331 packidx, idx, id);
332 } else if (err->code == GOT_ERR_NO_OBJ) {
333 int fd;
335 err = open_loose_object(&fd, id, repo);
336 if (err)
337 return err;
338 err = got_object_read_commit_privsep(commit, fd, repo);
339 close(fd);
342 if (err == NULL) {
343 (*commit)->refcnt++;
344 err = got_repo_cache_commit(repo, id, *commit);
347 return err;
350 const struct got_error *
351 got_object_open_as_commit(struct got_commit_object **commit,
352 struct got_repository *repo, struct got_object_id *id)
354 *commit = got_repo_get_cached_commit(repo, id);
355 if (*commit != NULL) {
356 (*commit)->refcnt++;
357 return NULL;
360 return open_commit(commit, repo, id, 0);
363 const struct got_error *
364 got_object_commit_open(struct got_commit_object **commit,
365 struct got_repository *repo, struct got_object *obj)
367 return open_commit(commit, repo, got_object_get_id(obj), 1);
370 const struct got_error *
371 got_object_qid_alloc(struct got_object_qid **qid, struct got_object_id *id)
373 const struct got_error *err = NULL;
375 *qid = calloc(1, sizeof(**qid));
376 if (*qid == NULL)
377 return got_error_from_errno();
379 (*qid)->id = got_object_id_dup(id);
380 if ((*qid)->id == NULL) {
381 err = got_error_from_errno();
382 got_object_qid_free(*qid);
383 *qid = NULL;
384 return err;
387 return NULL;
390 static const struct got_error *
391 open_tree(struct got_tree_object **tree,
392 struct got_repository *repo, struct got_object *obj, int check_cache)
394 const struct got_error *err = NULL;
396 if (check_cache) {
397 *tree = got_repo_get_cached_tree(repo, &obj->id);
398 if (*tree != NULL) {
399 (*tree)->refcnt++;
400 return NULL;
402 } else
403 *tree = NULL;
405 if (obj->type != GOT_OBJ_TYPE_TREE)
406 return got_error(GOT_ERR_OBJ_TYPE);
408 if (obj->flags & GOT_OBJ_FLAG_PACKED) {
409 struct got_pack *pack;
410 pack = got_repo_get_cached_pack(repo, obj->path_packfile);
411 if (pack == NULL) {
412 err = got_repo_cache_pack(&pack, repo,
413 obj->path_packfile, NULL);
414 if (err)
415 return err;
417 err = got_object_read_packed_tree_privsep(tree, obj, pack);
418 } else {
419 int fd;
420 err = open_loose_object(&fd, got_object_get_id(obj), repo);
421 if (err)
422 return err;
423 err = got_object_read_tree_privsep(tree, obj, fd, repo);
424 close(fd);
427 if (err == NULL) {
428 (*tree)->refcnt++;
429 err = got_repo_cache_tree(repo, &obj->id, *tree);
432 return err;
435 const struct got_error *
436 got_object_open_as_tree(struct got_tree_object **tree,
437 struct got_repository *repo, struct got_object_id *id)
439 const struct got_error *err;
440 struct got_object *obj;
442 *tree = got_repo_get_cached_tree(repo, id);
443 if (*tree != NULL) {
444 (*tree)->refcnt++;
445 return NULL;
448 err = got_object_open(&obj, repo, id);
449 if (err)
450 return err;
451 if (obj->type != GOT_OBJ_TYPE_TREE) {
452 err = got_error(GOT_ERR_OBJ_TYPE);
453 goto done;
456 err = open_tree(tree, repo, obj, 0);
457 done:
458 got_object_close(obj);
459 return err;
462 const struct got_error *
463 got_object_tree_open(struct got_tree_object **tree,
464 struct got_repository *repo, struct got_object *obj)
466 return open_tree(tree, repo, obj, 1);
469 const struct got_tree_entries *
470 got_object_tree_get_entries(struct got_tree_object *tree)
472 return &tree->entries;
475 static const struct got_error *
476 read_packed_blob_privsep(size_t *size, int outfd, struct got_object *obj,
477 struct got_pack *pack)
479 const struct got_error *err = NULL;
480 int outfd_child;
481 int basefd, accumfd; /* temporary files for delta application */
483 basefd = got_opentempfd();
484 if (basefd == -1)
485 return got_error_from_errno();
486 accumfd = got_opentempfd();
487 if (accumfd == -1)
488 return got_error_from_errno();
490 outfd_child = dup(outfd);
491 if (outfd_child == -1)
492 return got_error_from_errno();
494 err = got_privsep_send_obj_req(pack->privsep_child->ibuf, -1, obj);
495 if (err)
496 return err;
498 err = got_privsep_send_blob_outfd(pack->privsep_child->ibuf,
499 outfd_child);
500 if (err) {
501 close(outfd_child);
502 return err;
504 err = got_privsep_send_tmpfd(pack->privsep_child->ibuf,
505 basefd);
506 if (err) {
507 close(basefd);
508 close(accumfd);
509 close(outfd_child);
510 return err;
513 err = got_privsep_send_tmpfd(pack->privsep_child->ibuf,
514 accumfd);
515 if (err) {
516 close(accumfd);
517 close(outfd_child);
518 return err;
521 err = got_privsep_recv_blob(size, pack->privsep_child->ibuf);
522 if (err)
523 return err;
525 if (lseek(outfd, SEEK_SET, 0) == -1)
526 err = got_error_from_errno();
528 return err;
531 const struct got_error *
532 got_object_blob_open(struct got_blob_object **blob,
533 struct got_repository *repo, struct got_object *obj, size_t blocksize)
535 const struct got_error *err = NULL;
536 int outfd;
537 size_t size;
538 struct stat sb;
540 if (obj->type != GOT_OBJ_TYPE_BLOB)
541 return got_error(GOT_ERR_OBJ_TYPE);
543 if (blocksize < obj->hdrlen)
544 return got_error(GOT_ERR_NO_SPACE);
546 *blob = calloc(1, sizeof(**blob));
547 if (*blob == NULL)
548 return got_error_from_errno();
550 outfd = got_opentempfd();
551 if (outfd == -1)
552 return got_error_from_errno();
554 (*blob)->read_buf = malloc(blocksize);
555 if ((*blob)->read_buf == NULL) {
556 err = got_error_from_errno();
557 goto done;
559 if (obj->flags & GOT_OBJ_FLAG_PACKED) {
560 struct got_pack *pack;
561 pack = got_repo_get_cached_pack(repo, obj->path_packfile);
562 if (pack == NULL) {
563 err = got_repo_cache_pack(&pack, repo,
564 obj->path_packfile, NULL);
565 if (err)
566 goto done;
568 err = read_packed_blob_privsep(&size, outfd, obj, pack);
569 if (err)
570 goto done;
571 obj->size = size;
572 } else {
573 int infd;
575 err = open_loose_object(&infd, got_object_get_id(obj), repo);
576 if (err)
577 goto done;
579 err = got_object_read_blob_privsep(&size, outfd, infd, repo);
580 close(infd);
581 if (err)
582 goto done;
584 if (size != obj->hdrlen + obj->size) {
585 err = got_error(GOT_ERR_PRIVSEP_LEN);
586 goto done;
590 if (fstat(outfd, &sb) == -1) {
591 err = got_error_from_errno();
592 goto done;
595 if (sb.st_size != obj->hdrlen + obj->size) {
596 err = got_error(GOT_ERR_PRIVSEP_LEN);
597 goto done;
600 (*blob)->f = fdopen(outfd, "rb");
601 if ((*blob)->f == NULL) {
602 err = got_error_from_errno();
603 close(outfd);
604 goto done;
607 (*blob)->hdrlen = obj->hdrlen;
608 (*blob)->blocksize = blocksize;
609 memcpy(&(*blob)->id.sha1, obj->id.sha1, SHA1_DIGEST_LENGTH);
611 done:
612 if (err) {
613 if (*blob) {
614 if ((*blob)->f)
615 fclose((*blob)->f);
616 free((*blob)->read_buf);
617 free(*blob);
618 *blob = NULL;
619 } else if (outfd != -1)
620 close(outfd);
622 return err;
625 const struct got_error *
626 got_object_open_as_blob(struct got_blob_object **blob,
627 struct got_repository *repo, struct got_object_id *id,
628 size_t blocksize)
630 const struct got_error *err;
631 struct got_object *obj;
633 *blob = NULL;
635 err = got_object_open(&obj, repo, id);
636 if (err)
637 return err;
638 if (obj->type != GOT_OBJ_TYPE_BLOB) {
639 err = got_error(GOT_ERR_OBJ_TYPE);
640 goto done;
643 err = got_object_blob_open(blob, repo, obj, blocksize);
644 done:
645 got_object_close(obj);
646 return err;
649 void
650 got_object_blob_close(struct got_blob_object *blob)
652 free(blob->read_buf);
653 fclose(blob->f);
654 free(blob);
657 char *
658 got_object_blob_id_str(struct got_blob_object *blob, char *buf, size_t size)
660 return got_sha1_digest_to_str(blob->id.sha1, buf, size);
663 size_t
664 got_object_blob_get_hdrlen(struct got_blob_object *blob)
666 return blob->hdrlen;
669 const uint8_t *
670 got_object_blob_get_read_buf(struct got_blob_object *blob)
672 return blob->read_buf;
675 const struct got_error *
676 got_object_blob_read_block(size_t *outlenp, struct got_blob_object *blob)
678 size_t n;
680 n = fread(blob->read_buf, 1, blob->blocksize, blob->f);
681 if (n == 0 && ferror(blob->f))
682 return got_ferror(blob->f, GOT_ERR_IO);
683 *outlenp = n;
684 return NULL;
687 const struct got_error *
688 got_object_blob_dump_to_file(size_t *total_len, int *nlines,
689 FILE *outfile, struct got_blob_object *blob)
691 const struct got_error *err = NULL;
692 size_t len, hdrlen;
693 const uint8_t *buf;
694 int i;
696 if (total_len)
697 *total_len = 0;
698 if (nlines)
699 *nlines = 0;
701 hdrlen = got_object_blob_get_hdrlen(blob);
702 do {
703 err = got_object_blob_read_block(&len, blob);
704 if (err)
705 return err;
706 if (len == 0)
707 break;
708 if (total_len)
709 *total_len += len;
710 buf = got_object_blob_get_read_buf(blob);
711 if (nlines) {
712 for (i = 0; i < len; i++) {
713 if (buf[i] == '\n')
714 (*nlines)++;
717 /* Skip blob object header first time around. */
718 fwrite(buf + hdrlen, len - hdrlen, 1, outfile);
719 hdrlen = 0;
720 } while (len != 0);
722 fflush(outfile);
723 rewind(outfile);
725 return NULL;
728 static const struct got_error *
729 open_tag(struct got_tag_object **tag,
730 struct got_repository *repo, struct got_object *obj, int check_cache)
732 const struct got_error *err = NULL;
734 if (check_cache) {
735 *tag = got_repo_get_cached_tag(repo, &obj->id);
736 if (*tag != NULL) {
737 (*tag)->refcnt++;
738 return NULL;
740 } else
741 *tag = NULL;
743 if (obj->type != GOT_OBJ_TYPE_TAG)
744 return got_error(GOT_ERR_OBJ_TYPE);
746 if (obj->flags & GOT_OBJ_FLAG_PACKED) {
747 struct got_pack *pack;
748 pack = got_repo_get_cached_pack(repo, obj->path_packfile);
749 if (pack == NULL) {
750 err = got_repo_cache_pack(&pack, repo,
751 obj->path_packfile, NULL);
752 if (err)
753 return err;
755 err = got_object_read_packed_tag_privsep(tag, obj, pack);
756 } else {
757 int fd;
758 err = open_loose_object(&fd, got_object_get_id(obj), repo);
759 if (err)
760 return err;
761 err = got_object_read_tag_privsep(tag, obj, fd, repo);
762 close(fd);
765 if (err == NULL) {
766 (*tag)->refcnt++;
767 err = got_repo_cache_tag(repo, &obj->id, *tag);
770 return err;
773 const struct got_error *
774 got_object_open_as_tag(struct got_tag_object **tag,
775 struct got_repository *repo, struct got_object_id *id)
777 const struct got_error *err;
778 struct got_object *obj;
780 *tag = got_repo_get_cached_tag(repo, id);
781 if (*tag != NULL) {
782 (*tag)->refcnt++;
783 return NULL;
786 err = got_object_open(&obj, repo, id);
787 if (err)
788 return err;
789 if (obj->type != GOT_OBJ_TYPE_COMMIT) {
790 err = got_error(GOT_ERR_OBJ_TYPE);
791 goto done;
794 err = open_tag(tag, repo, obj, 0);
795 done:
796 got_object_close(obj);
797 return err;
800 const struct got_error *
801 got_object_tag_open(struct got_tag_object **tag,
802 struct got_repository *repo, struct got_object *obj)
804 return open_tag(tag, repo, obj, 1);
807 static struct got_tree_entry *
808 find_entry_by_name(struct got_tree_object *tree, const char *name, size_t len)
810 struct got_tree_entry *te;
812 /* Note that tree entries are sorted in strncmp() order. */
813 SIMPLEQ_FOREACH(te, &tree->entries.head, entry) {
814 int cmp = strncmp(te->name, name, len);
815 if (cmp < 0)
816 continue;
817 if (cmp > 0)
818 break;
819 if (te->name[len] == '\0')
820 return te;
822 return NULL;
825 const struct got_error *
826 got_object_id_by_path(struct got_object_id **id, struct got_repository *repo,
827 struct got_object_id *commit_id, const char *path)
829 const struct got_error *err = NULL;
830 struct got_commit_object *commit = NULL;
831 struct got_tree_object *tree = NULL;
832 struct got_tree_entry *te = NULL;
833 const char *seg, *s;
834 size_t seglen;
836 *id = NULL;
838 /* We are expecting an absolute in-repository path. */
839 if (path[0] != '/')
840 return got_error(GOT_ERR_NOT_ABSPATH);
842 err = got_object_open_as_commit(&commit, repo, commit_id);
843 if (err)
844 goto done;
846 /* Handle opening of root of commit's tree. */
847 if (path[1] == '\0') {
848 *id = got_object_id_dup(commit->tree_id);
849 if (*id == NULL)
850 err = got_error_from_errno();
851 goto done;
854 err = got_object_open_as_tree(&tree, repo, commit->tree_id);
855 if (err)
856 goto done;
858 s = path;
859 s++; /* skip leading '/' */
860 seg = s;
861 seglen = 0;
862 while (*s) {
863 struct got_tree_object *next_tree;
865 if (*s != '/') {
866 s++;
867 seglen++;
868 if (*s)
869 continue;
872 te = find_entry_by_name(tree, seg, seglen);
873 if (te == NULL) {
874 err = got_error(GOT_ERR_NO_TREE_ENTRY);
875 goto done;
878 if (*s == '\0')
879 break;
881 seg = s + 1;
882 seglen = 0;
883 s++;
884 if (*s) {
885 err = got_object_open_as_tree(&next_tree, repo,
886 te->id);
887 te = NULL;
888 if (err)
889 goto done;
890 got_object_tree_close(tree);
891 tree = next_tree;
895 if (te) {
896 *id = got_object_id_dup(te->id);
897 if (*id == NULL)
898 return got_error_from_errno();
899 } else
900 err = got_error(GOT_ERR_NO_TREE_ENTRY);
901 done:
902 if (commit)
903 got_object_commit_close(commit);
904 if (tree)
905 got_object_tree_close(tree);
906 return err;
909 const struct got_error *
910 got_object_tree_path_changed(int *changed,
911 struct got_tree_object *tree01, struct got_tree_object *tree02,
912 const char *path, struct got_repository *repo)
914 const struct got_error *err = NULL;
915 struct got_tree_object *tree1 = NULL, *tree2 = NULL;
916 struct got_tree_entry *te1 = NULL, *te2 = NULL;
917 const char *seg, *s;
918 size_t seglen;
920 *changed = 0;
922 /* We are expecting an absolute in-repository path. */
923 if (path[0] != '/')
924 return got_error(GOT_ERR_NOT_ABSPATH);
926 /* We not do support comparing the root path. */
927 if (path[1] == '\0')
928 return got_error(GOT_ERR_BAD_PATH);
930 tree1 = tree01;
931 tree2 = tree02;
932 s = path;
933 s++; /* skip leading '/' */
934 seg = s;
935 seglen = 0;
936 while (*s) {
937 struct got_tree_object *next_tree1, *next_tree2;
939 if (*s != '/') {
940 s++;
941 seglen++;
942 if (*s)
943 continue;
946 te1 = find_entry_by_name(tree1, seg, seglen);
947 if (te1 == NULL) {
948 err = got_error(GOT_ERR_NO_OBJ);
949 goto done;
952 te2 = find_entry_by_name(tree2, seg, seglen);
953 if (te2 == NULL) {
954 *changed = 1;
955 goto done;
958 if (te1->mode != te2->mode) {
959 *changed = 1;
960 goto done;
963 if (got_object_id_cmp(te1->id, te2->id) == 0) {
964 *changed = 0;
965 goto done;
968 if (*s == '\0') { /* final path element */
969 *changed = 1;
970 goto done;
973 seg = s + 1;
974 s++;
975 seglen = 0;
976 if (*s) {
977 err = got_object_open_as_tree(&next_tree1, repo,
978 te1->id);
979 te1 = NULL;
980 if (err)
981 goto done;
982 if (tree1 != tree01)
983 got_object_tree_close(tree1);
984 tree1 = next_tree1;
986 err = got_object_open_as_tree(&next_tree2, repo,
987 te2->id);
988 te2 = NULL;
989 if (err)
990 goto done;
991 if (tree2 != tree02)
992 got_object_tree_close(tree2);
993 tree2 = next_tree2;
996 done:
997 if (tree1 && tree1 != tree01)
998 got_object_tree_close(tree1);
999 if (tree2 && tree2 != tree02)
1000 got_object_tree_close(tree2);
1001 return err;
1004 static void
1005 exec_privsep_child(int imsg_fds[2], const char *path, const char *repo_path)
1007 close(imsg_fds[0]);
1009 if (dup2(imsg_fds[1], GOT_IMSG_FD_CHILD) == -1) {
1010 fprintf(stderr, "%s: %s\n", getprogname(),
1011 strerror(errno));
1012 _exit(1);
1014 if (closefrom(GOT_IMSG_FD_CHILD + 1) == -1) {
1015 fprintf(stderr, "%s: %s\n", getprogname(),
1016 strerror(errno));
1017 _exit(1);
1020 if (execl(path, path, repo_path, (char *)NULL) == -1) {
1021 fprintf(stderr, "%s: %s: %s\n", getprogname(), path,
1022 strerror(errno));
1023 _exit(1);
1027 static const struct got_error *
1028 request_object(struct got_object **obj, struct got_repository *repo, int fd)
1030 const struct got_error *err = NULL;
1031 struct imsgbuf *ibuf;
1033 ibuf = repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_OBJECT].ibuf;
1035 err = got_privsep_send_obj_req(ibuf, fd, NULL);
1036 if (err)
1037 return err;
1039 return got_privsep_recv_obj(obj, ibuf);
1042 const struct got_error *
1043 got_object_read_header_privsep(struct got_object **obj,
1044 struct got_repository *repo, int obj_fd)
1046 int imsg_fds[2];
1047 pid_t pid;
1048 struct imsgbuf *ibuf;
1050 if (repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_OBJECT].imsg_fd != -1)
1051 return request_object(obj, repo, obj_fd);
1053 ibuf = calloc(1, sizeof(*ibuf));
1054 if (ibuf == NULL)
1055 return got_error_from_errno();
1057 if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_fds) == -1)
1058 return got_error_from_errno();
1060 pid = fork();
1061 if (pid == -1)
1062 return got_error_from_errno();
1063 else if (pid == 0) {
1064 exec_privsep_child(imsg_fds, GOT_PATH_PROG_READ_OBJECT,
1065 repo->path);
1066 /* not reached */
1069 close(imsg_fds[1]);
1070 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_OBJECT].imsg_fd =
1071 imsg_fds[0];
1072 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_OBJECT].pid = pid;
1073 imsg_init(ibuf, imsg_fds[0]);
1074 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_OBJECT].ibuf = ibuf;
1076 return request_object(obj, repo, obj_fd);
1079 static const struct got_error *
1080 request_packed_object(struct got_object **obj, struct got_pack *pack, int idx,
1081 struct got_object_id *id)
1083 const struct got_error *err = NULL;
1084 struct imsgbuf *ibuf = pack->privsep_child->ibuf;
1086 err = got_privsep_send_packed_obj_req(ibuf, idx, id);
1087 if (err)
1088 return err;
1090 err = got_privsep_recv_obj(obj, ibuf);
1091 if (err)
1092 return err;
1094 (*obj)->path_packfile = strdup(pack->path_packfile);
1095 if ((*obj)->path_packfile == NULL) {
1096 err = got_error_from_errno();
1097 return err;
1099 memcpy(&(*obj)->id, id, sizeof((*obj)->id));
1101 return NULL;
1104 const struct got_error *
1105 got_object_packed_read_privsep(struct got_object **obj,
1106 struct got_repository *repo, struct got_pack *pack,
1107 struct got_packidx *packidx, int idx, struct got_object_id *id)
1109 const struct got_error *err = NULL;
1110 int imsg_fds[2];
1111 pid_t pid;
1112 struct imsgbuf *ibuf;
1114 if (pack->privsep_child)
1115 return request_packed_object(obj, pack, idx, id);
1117 ibuf = calloc(1, sizeof(*ibuf));
1118 if (ibuf == NULL)
1119 return got_error_from_errno();
1121 pack->privsep_child = calloc(1, sizeof(*pack->privsep_child));
1122 if (pack->privsep_child == NULL) {
1123 err = got_error_from_errno();
1124 free(ibuf);
1125 return err;
1128 if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_fds) == -1) {
1129 err = got_error_from_errno();
1130 goto done;
1133 pid = fork();
1134 if (pid == -1) {
1135 err = got_error_from_errno();
1136 goto done;
1137 } else if (pid == 0) {
1138 exec_privsep_child(imsg_fds, GOT_PATH_PROG_READ_PACK,
1139 pack->path_packfile);
1140 /* not reached */
1143 close(imsg_fds[1]);
1144 pack->privsep_child->imsg_fd = imsg_fds[0];
1145 pack->privsep_child->pid = pid;
1146 imsg_init(ibuf, imsg_fds[0]);
1147 pack->privsep_child->ibuf = ibuf;
1149 err = got_privsep_init_pack_child(ibuf, pack, packidx);
1150 if (err) {
1151 const struct got_error *child_err;
1152 err = got_privsep_send_stop(pack->privsep_child->imsg_fd);
1153 child_err = got_privsep_wait_for_child(
1154 pack->privsep_child->pid);
1155 if (child_err && err == NULL)
1156 err = child_err;
1157 free(ibuf);
1158 free(pack->privsep_child);
1159 pack->privsep_child = NULL;
1160 return err;
1163 done:
1164 if (err) {
1165 free(ibuf);
1166 free(pack->privsep_child);
1167 pack->privsep_child = NULL;
1168 } else
1169 err = request_packed_object(obj, pack, idx, id);
1170 return err;
1173 static const struct got_error *
1174 request_commit(struct got_commit_object **commit, struct got_repository *repo,
1175 int fd)
1177 const struct got_error *err = NULL;
1178 struct imsgbuf *ibuf;
1180 ibuf = repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_COMMIT].ibuf;
1182 err = got_privsep_send_commit_req(ibuf, fd, NULL, -1);
1183 if (err)
1184 return err;
1186 return got_privsep_recv_commit(commit, ibuf);
1189 static const struct got_error *
1190 request_packed_commit(struct got_commit_object **commit, struct got_pack *pack,
1191 int pack_idx, struct got_object_id *id)
1193 const struct got_error *err = NULL;
1195 err = got_privsep_send_commit_req(pack->privsep_child->ibuf, -1, id,
1196 pack_idx);
1197 if (err)
1198 return err;
1200 return got_privsep_recv_commit(commit, pack->privsep_child->ibuf);
1203 const struct got_error *
1204 got_object_read_packed_commit_privsep(struct got_commit_object **commit,
1205 struct got_pack *pack, struct got_packidx *packidx, int idx,
1206 struct got_object_id *id)
1208 const struct got_error *err = NULL;
1209 int imsg_fds[2];
1210 pid_t pid;
1211 struct imsgbuf *ibuf;
1213 if (pack->privsep_child)
1214 return request_packed_commit(commit, pack, idx, id);
1216 ibuf = calloc(1, sizeof(*ibuf));
1217 if (ibuf == NULL)
1218 return got_error_from_errno();
1220 pack->privsep_child = calloc(1, sizeof(*pack->privsep_child));
1221 if (pack->privsep_child == NULL) {
1222 err = got_error_from_errno();
1223 free(ibuf);
1224 return err;
1227 if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_fds) == -1) {
1228 err = got_error_from_errno();
1229 goto done;
1232 pid = fork();
1233 if (pid == -1) {
1234 err = got_error_from_errno();
1235 goto done;
1236 } else if (pid == 0) {
1237 exec_privsep_child(imsg_fds, GOT_PATH_PROG_READ_PACK,
1238 pack->path_packfile);
1239 /* not reached */
1242 close(imsg_fds[1]);
1243 pack->privsep_child->imsg_fd = imsg_fds[0];
1244 pack->privsep_child->pid = pid;
1245 imsg_init(ibuf, imsg_fds[0]);
1246 pack->privsep_child->ibuf = ibuf;
1248 err = got_privsep_init_pack_child(ibuf, pack, packidx);
1249 if (err) {
1250 const struct got_error *child_err;
1251 err = got_privsep_send_stop(pack->privsep_child->imsg_fd);
1252 child_err = got_privsep_wait_for_child(
1253 pack->privsep_child->pid);
1254 if (child_err && err == NULL)
1255 err = child_err;
1256 free(ibuf);
1257 free(pack->privsep_child);
1258 pack->privsep_child = NULL;
1259 return err;
1262 done:
1263 if (err) {
1264 free(ibuf);
1265 free(pack->privsep_child);
1266 pack->privsep_child = NULL;
1267 } else
1268 err = request_packed_commit(commit, pack, idx, id);
1269 return err;
1272 const struct got_error *
1273 got_object_read_commit_privsep(struct got_commit_object **commit,
1274 int obj_fd, struct got_repository *repo)
1276 int imsg_fds[2];
1277 pid_t pid;
1278 struct imsgbuf *ibuf;
1280 if (repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_COMMIT].imsg_fd != -1)
1281 return request_commit(commit, repo, obj_fd);
1283 ibuf = calloc(1, sizeof(*ibuf));
1284 if (ibuf == NULL)
1285 return got_error_from_errno();
1287 if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_fds) == -1)
1288 return got_error_from_errno();
1290 pid = fork();
1291 if (pid == -1)
1292 return got_error_from_errno();
1293 else if (pid == 0) {
1294 exec_privsep_child(imsg_fds, GOT_PATH_PROG_READ_COMMIT,
1295 repo->path);
1296 /* not reached */
1299 close(imsg_fds[1]);
1300 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_COMMIT].imsg_fd =
1301 imsg_fds[0];
1302 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_COMMIT].pid = pid;
1303 imsg_init(ibuf, imsg_fds[0]);
1304 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_COMMIT].ibuf = ibuf;
1306 return request_commit(commit, repo, obj_fd);
1309 static const struct got_error *
1310 request_tree(struct got_tree_object **tree, struct got_repository *repo,
1311 struct got_object *obj, int fd)
1313 const struct got_error *err = NULL;
1314 struct imsgbuf *ibuf;
1316 ibuf = repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TREE].ibuf;
1318 err = got_privsep_send_obj_req(ibuf, fd, obj);
1319 if (err)
1320 return err;
1322 return got_privsep_recv_tree(tree, ibuf);
1325 const struct got_error *
1326 got_object_read_tree_privsep(struct got_tree_object **tree,
1327 struct got_object *obj, int obj_fd, struct got_repository *repo)
1329 int imsg_fds[2];
1330 pid_t pid;
1331 struct imsgbuf *ibuf;
1333 if (repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TREE].imsg_fd != -1)
1334 return request_tree(tree, repo, obj, obj_fd);
1336 ibuf = calloc(1, sizeof(*ibuf));
1337 if (ibuf == NULL)
1338 return got_error_from_errno();
1340 if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_fds) == -1)
1341 return got_error_from_errno();
1343 pid = fork();
1344 if (pid == -1)
1345 return got_error_from_errno();
1346 else if (pid == 0) {
1347 exec_privsep_child(imsg_fds, GOT_PATH_PROG_READ_TREE,
1348 repo->path);
1349 /* not reached */
1352 close(imsg_fds[1]);
1354 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TREE].imsg_fd =
1355 imsg_fds[0];
1356 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TREE].pid = pid;
1357 imsg_init(ibuf, imsg_fds[0]);
1358 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TREE].ibuf = ibuf;
1361 return request_tree(tree, repo, obj, obj_fd);
1364 const struct got_error *
1365 got_object_read_packed_tree_privsep(struct got_tree_object **tree,
1366 struct got_object *obj, struct got_pack *pack)
1368 const struct got_error *err = NULL;
1370 err = got_privsep_send_obj_req(pack->privsep_child->ibuf, -1, obj);
1371 if (err)
1372 return err;
1374 return got_privsep_recv_tree(tree, pack->privsep_child->ibuf);
1377 static const struct got_error *
1378 request_blob(size_t *size, int outfd, int infd, struct imsgbuf *ibuf)
1380 const struct got_error *err = NULL;
1381 int outfd_child;
1383 outfd_child = dup(outfd);
1384 if (outfd_child == -1)
1385 return got_error_from_errno();
1387 err = got_privsep_send_blob_req(ibuf, infd);
1388 if (err)
1389 return err;
1391 err = got_privsep_send_blob_outfd(ibuf, outfd_child);
1392 if (err) {
1393 close(outfd_child);
1394 return err;
1397 err = got_privsep_recv_blob(size, ibuf);
1398 if (err)
1399 return err;
1401 if (lseek(outfd, SEEK_SET, 0) == -1)
1402 return got_error_from_errno();
1404 return err;
1407 const struct got_error *
1408 got_object_read_blob_privsep(size_t *size, int outfd, int infd,
1409 struct got_repository *repo)
1411 int imsg_fds[2];
1412 pid_t pid;
1413 struct imsgbuf *ibuf;
1415 if (repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_BLOB].imsg_fd != -1) {
1416 ibuf = repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_BLOB].ibuf;
1417 return request_blob(size, outfd, infd, ibuf);
1420 ibuf = calloc(1, sizeof(*ibuf));
1421 if (ibuf == NULL)
1422 return got_error_from_errno();
1424 if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_fds) == -1)
1425 return got_error_from_errno();
1427 pid = fork();
1428 if (pid == -1)
1429 return got_error_from_errno();
1430 else if (pid == 0) {
1431 exec_privsep_child(imsg_fds, GOT_PATH_PROG_READ_BLOB,
1432 repo->path);
1433 /* not reached */
1436 close(imsg_fds[1]);
1437 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_BLOB].imsg_fd =
1438 imsg_fds[0];
1439 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_BLOB].pid = pid;
1440 imsg_init(ibuf, imsg_fds[0]);
1441 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_BLOB].ibuf = ibuf;
1443 return request_blob(size, outfd, infd, ibuf);
1446 static const struct got_error *
1447 request_tag(struct got_tag_object **tag, struct got_repository *repo,
1448 struct got_object *obj, int fd)
1450 const struct got_error *err = NULL;
1451 struct imsgbuf *ibuf;
1453 ibuf = repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TAG].ibuf;
1455 err = got_privsep_send_obj_req(ibuf, fd, obj);
1456 if (err)
1457 return err;
1459 return got_privsep_recv_tag(tag, ibuf);
1462 const struct got_error *
1463 got_object_read_packed_tag_privsep(struct got_tag_object **tag,
1464 struct got_object *obj, struct got_pack *pack)
1466 const struct got_error *err = NULL;
1468 err = got_privsep_send_obj_req(pack->privsep_child->ibuf, -1, obj);
1469 if (err)
1470 return err;
1472 return got_privsep_recv_tag(tag, pack->privsep_child->ibuf);
1475 const struct got_error *
1476 got_object_read_tag_privsep(struct got_tag_object **tag,
1477 struct got_object *obj, int obj_fd, struct got_repository *repo)
1479 int imsg_fds[2];
1480 pid_t pid;
1481 struct imsgbuf *ibuf;
1483 if (repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TAG].imsg_fd != -1)
1484 return request_tag(tag, repo, obj, obj_fd);
1486 ibuf = calloc(1, sizeof(*ibuf));
1487 if (ibuf == NULL)
1488 return got_error_from_errno();
1490 if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_fds) == -1)
1491 return got_error_from_errno();
1493 pid = fork();
1494 if (pid == -1)
1495 return got_error_from_errno();
1496 else if (pid == 0) {
1497 exec_privsep_child(imsg_fds, GOT_PATH_PROG_READ_TAG,
1498 repo->path);
1499 /* not reached */
1502 close(imsg_fds[1]);
1503 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TAG].imsg_fd =
1504 imsg_fds[0];
1505 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TAG].pid = pid;
1506 imsg_init(ibuf, imsg_fds[0]);
1507 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TAG].ibuf = ibuf;
1509 return request_tag(tag, repo, obj, obj_fd);