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>
24 #include <errno.h>
25 #include <fcntl.h>
26 #include <stdio.h>
27 #include <stdlib.h>
28 #include <string.h>
29 #include <stdint.h>
30 #include <sha1.h>
31 #include <zlib.h>
32 #include <ctype.h>
33 #include <limits.h>
34 #include <imsg.h>
36 #include "got_error.h"
37 #include "got_object.h"
38 #include "got_repository.h"
39 #include "got_opentemp.h"
41 #include "got_lib_sha1.h"
42 #include "got_lib_delta.h"
43 #include "got_lib_pack.h"
44 #include "got_lib_path.h"
45 #include "got_lib_zbuf.h"
46 #include "got_lib_object.h"
47 #include "got_lib_privsep.h"
49 #ifndef MIN
50 #define MIN(_a,_b) ((_a) < (_b) ? (_a) : (_b))
51 #endif
53 #ifndef nitems
54 #define nitems(_a) (sizeof(_a) / sizeof((_a)[0]))
55 #endif
57 #define GOT_OBJ_TAG_COMMIT "commit"
58 #define GOT_OBJ_TAG_TREE "tree"
59 #define GOT_OBJ_TAG_BLOB "blob"
61 #define GOT_COMMIT_TAG_TREE "tree "
62 #define GOT_COMMIT_TAG_PARENT "parent "
63 #define GOT_COMMIT_TAG_AUTHOR "author "
64 #define GOT_COMMIT_TAG_COMMITTER "committer "
66 const struct got_error *
67 got_object_id_str(char **outbuf, struct got_object_id *id)
68 {
69 static const size_t len = SHA1_DIGEST_STRING_LENGTH;
71 *outbuf = calloc(1, len);
72 if (*outbuf == NULL)
73 return got_error_from_errno();
75 if (got_sha1_digest_to_str(id->sha1, *outbuf, len) == NULL) {
76 free(*outbuf);
77 *outbuf = NULL;
78 return got_error(GOT_ERR_BAD_OBJ_ID_STR);
79 }
81 return NULL;
82 }
84 int
85 got_object_id_cmp(struct got_object_id *id1, struct got_object_id *id2)
86 {
87 return memcmp(id1->sha1, id2->sha1, SHA1_DIGEST_LENGTH);
88 }
90 struct got_object_id *
91 got_object_id_dup(struct got_object_id *id1)
92 {
93 struct got_object_id *id2;
95 id2 = malloc(sizeof(*id2));
96 if (id2 == NULL)
97 return NULL;
98 memcpy(id2, id1, sizeof(*id2));
99 return id2;
102 struct got_object_id *
103 got_object_get_id(struct got_object *obj)
105 return got_object_id_dup(&obj->id);
108 int
109 got_object_get_type(struct got_object *obj)
111 switch (obj->type) {
112 case GOT_OBJ_TYPE_COMMIT:
113 case GOT_OBJ_TYPE_TREE:
114 case GOT_OBJ_TYPE_BLOB:
115 case GOT_OBJ_TYPE_TAG:
116 return obj->type;
117 default:
118 abort();
119 break;
122 /* not reached */
123 return 0;
126 static const struct got_error *
127 parse_object_header(struct got_object **obj, char *buf, size_t len)
129 const char *obj_tags[] = {
130 GOT_OBJ_TAG_COMMIT,
131 GOT_OBJ_TAG_TREE,
132 GOT_OBJ_TAG_BLOB
133 };
134 const int obj_types[] = {
135 GOT_OBJ_TYPE_COMMIT,
136 GOT_OBJ_TYPE_TREE,
137 GOT_OBJ_TYPE_BLOB,
138 };
139 int type = 0;
140 size_t size = 0, hdrlen = 0;
141 int i;
142 char *p = strchr(buf, '\0');
144 if (p == NULL)
145 return got_error(GOT_ERR_BAD_OBJ_HDR);
147 hdrlen = strlen(buf) + 1 /* '\0' */;
149 for (i = 0; i < nitems(obj_tags); i++) {
150 const char *tag = obj_tags[i];
151 size_t tlen = strlen(tag);
152 const char *errstr;
154 if (strncmp(buf, tag, tlen) != 0)
155 continue;
157 type = obj_types[i];
158 if (len <= tlen)
159 return got_error(GOT_ERR_BAD_OBJ_HDR);
160 size = strtonum(buf + tlen, 0, LONG_MAX, &errstr);
161 if (errstr != NULL)
162 return got_error(GOT_ERR_BAD_OBJ_HDR);
163 break;
166 if (type == 0)
167 return got_error(GOT_ERR_BAD_OBJ_HDR);
169 *obj = calloc(1, sizeof(**obj));
170 if (*obj == NULL)
171 return got_error_from_errno();
172 (*obj)->type = type;
173 (*obj)->hdrlen = hdrlen;
174 (*obj)->size = size;
175 return NULL;
178 static const struct got_error *
179 read_object_header(struct got_object **obj, FILE *f)
181 const struct got_error *err;
182 struct got_zstream_buf zb;
183 char *buf;
184 const size_t zbsize = 64;
185 size_t outlen, totlen;
186 int i;
188 buf = calloc(zbsize, sizeof(char));
189 if (buf == NULL)
190 return got_error_from_errno();
192 err = got_inflate_init(&zb, NULL, zbsize);
193 if (err)
194 return err;
196 i = 0;
197 totlen = 0;
198 do {
199 err = got_inflate_read(&zb, f, &outlen);
200 if (err)
201 goto done;
202 if (strchr(zb.outbuf, '\0') == NULL) {
203 buf = recallocarray(buf, 1 + i, 2 + i, zbsize);
204 if (buf == NULL) {
205 err = got_error_from_errno();
206 goto done;
209 memcpy(buf + totlen, zb.outbuf, outlen);
210 totlen += outlen;
211 i++;
212 } while (strchr(zb.outbuf, '\0') == NULL);
214 err = parse_object_header(obj, buf, totlen);
215 done:
216 got_inflate_end(&zb);
217 return err;
220 static void
221 read_object_header_privsep_child(int obj_fd, int imsg_fds[2])
223 const struct got_error *err = NULL;
224 struct got_object *obj = NULL;
225 struct imsgbuf ibuf;
226 FILE *f = NULL;
227 int status = 0;
229 setproctitle("read object header");
230 close(imsg_fds[0]);
231 imsg_init(&ibuf, imsg_fds[1]);
233 /* revoke access to most system calls */
234 if (pledge("stdio", NULL) == -1) {
235 err = got_error_from_errno();
236 goto done;
239 f = fdopen(obj_fd, "rb");
240 if (f == NULL) {
241 err = got_error_from_errno();
242 close(obj_fd);
243 goto done;
246 err = read_object_header(&obj, f);
247 if (err)
248 goto done;
250 err = got_privsep_send_obj(&ibuf, obj, 0);
251 done:
252 if (obj)
253 got_object_close(obj);
254 if (err) {
255 got_privsep_send_error(&ibuf, err);
256 status = 1;
258 if (f)
259 fclose(f);
260 imsg_clear(&ibuf);
261 close(imsg_fds[1]);
262 _exit(status);
265 static const struct got_error *
266 wait_for_child(pid_t pid)
268 int child_status;
270 waitpid(pid, &child_status, 0);
272 if (!WIFEXITED(child_status))
273 return got_error(GOT_ERR_PRIVSEP_DIED);
275 if (WEXITSTATUS(child_status) != 0)
276 return got_error(GOT_ERR_PRIVSEP_EXIT);
278 return NULL;
281 static const struct got_error *
282 read_object_header_privsep(struct got_object **obj, int fd)
284 struct imsgbuf parent_ibuf;
285 int imsg_fds[2];
286 const struct got_error *err = NULL, *err_child = NULL;
287 pid_t pid;
289 if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_fds) == -1)
290 return got_error_from_errno();
292 pid = fork();
293 if (pid == -1)
294 return got_error_from_errno();
295 else if (pid == 0) {
296 read_object_header_privsep_child(fd, imsg_fds);
297 /* not reached */
300 close(imsg_fds[1]);
301 imsg_init(&parent_ibuf, imsg_fds[0]);
302 err = got_privsep_recv_obj(obj, &parent_ibuf);
303 imsg_clear(&parent_ibuf);
304 err_child = wait_for_child(pid);
305 close(imsg_fds[0]);
306 return err ? err : err_child;
309 static const struct got_error *
310 object_path(char **path, struct got_object_id *id, struct got_repository *repo)
312 const struct got_error *err = NULL;
313 char *hex;
314 char *path_objects = got_repo_get_path_objects(repo);
316 *path = NULL;
318 if (path_objects == NULL)
319 return got_error_from_errno();
321 err = got_object_id_str(&hex, id);
322 if (err)
323 return err;
325 if (asprintf(path, "%s/%.2x/%s", path_objects,
326 id->sha1[0], hex + 2) == -1)
327 err = got_error_from_errno();
329 free(hex);
330 free(path_objects);
331 return err;
334 static const struct got_error *
335 open_loose_object(int *fd, struct got_object *obj, struct got_repository *repo)
337 const struct got_error *err = NULL;
338 char *path;
340 err = object_path(&path, &obj->id, repo);
341 if (err)
342 return err;
343 *fd = open(path, O_RDONLY | O_NOFOLLOW, GOT_DEFAULT_FILE_MODE);
344 if (*fd == -1) {
345 err = got_error_from_errno();
346 goto done;
348 done:
349 free(path);
350 return err;
353 const struct got_error *
354 got_object_open(struct got_object **obj, struct got_repository *repo,
355 struct got_object_id *id)
357 const struct got_error *err = NULL;
358 char *path;
359 int fd;
361 err = object_path(&path, id, repo);
362 if (err)
363 return err;
365 fd = open(path, O_RDONLY | O_NOFOLLOW, GOT_DEFAULT_FILE_MODE);
366 if (fd == -1) {
367 if (errno != ENOENT) {
368 err = got_error_from_errno();
369 goto done;
371 err = got_packfile_open_object(obj, id, repo);
372 if (err)
373 goto done;
374 if (*obj == NULL)
375 err = got_error(GOT_ERR_NO_OBJ);
376 } else {
377 err = read_object_header_privsep(obj, fd);
378 if (err)
379 goto done;
380 memcpy((*obj)->id.sha1, id->sha1, SHA1_DIGEST_LENGTH);
382 done:
383 free(path);
384 if (fd != -1)
385 close(fd);
386 return err;
390 const struct got_error *
391 got_object_open_by_id_str(struct got_object **obj, struct got_repository *repo,
392 const char *id_str)
394 struct got_object_id id;
396 if (!got_parse_sha1_digest(id.sha1, id_str))
397 return got_error(GOT_ERR_BAD_OBJ_ID_STR);
399 return got_object_open(obj, repo, &id);
402 void
403 got_object_close(struct got_object *obj)
405 if (obj->flags & GOT_OBJ_FLAG_DELTIFIED) {
406 struct got_delta *delta;
407 while (!SIMPLEQ_EMPTY(&obj->deltas.entries)) {
408 delta = SIMPLEQ_FIRST(&obj->deltas.entries);
409 SIMPLEQ_REMOVE_HEAD(&obj->deltas.entries, entry);
410 got_delta_close(delta);
413 if (obj->flags & GOT_OBJ_FLAG_PACKED)
414 free(obj->path_packfile);
415 free(obj);
418 struct got_commit_object *
419 got_object_commit_alloc_partial(void)
421 struct got_commit_object *commit;
423 commit = calloc(1, sizeof(*commit));
424 if (commit == NULL)
425 return NULL;
426 commit->tree_id = calloc(1, sizeof(*commit->tree_id));
427 if (commit->tree_id == NULL) {
428 free(commit);
429 return NULL;
432 SIMPLEQ_INIT(&commit->parent_ids);
434 return commit;
437 const struct got_error *
438 got_object_commit_add_parent(struct got_commit_object *commit,
439 const char *id_str)
441 const struct got_error *err = NULL;
442 struct got_parent_id *pid;
444 pid = calloc(1, sizeof(*pid));
445 if (pid == NULL)
446 return got_error_from_errno();
448 pid->id = calloc(1, sizeof(*pid->id));
449 if (pid->id == NULL) {
450 err = got_error_from_errno();
451 free(pid);
452 return err;
455 if (!got_parse_sha1_digest(pid->id->sha1, id_str)) {
456 err = got_error(GOT_ERR_BAD_OBJ_DATA);
457 free(pid->id);
458 free(pid);
459 return err;
462 SIMPLEQ_INSERT_TAIL(&commit->parent_ids, pid, entry);
463 commit->nparents++;
465 return NULL;
468 static const struct got_error *
469 parse_commit_object(struct got_commit_object **commit, char *buf, size_t len)
471 const struct got_error *err = NULL;
472 char *s = buf;
473 size_t tlen;
474 ssize_t remain = (ssize_t)len;
476 *commit = got_object_commit_alloc_partial();
477 if (*commit == NULL)
478 return got_error_from_errno();
480 tlen = strlen(GOT_COMMIT_TAG_TREE);
481 if (strncmp(s, GOT_COMMIT_TAG_TREE, tlen) == 0) {
482 remain -= tlen;
483 if (remain < SHA1_DIGEST_STRING_LENGTH) {
484 err = got_error(GOT_ERR_BAD_OBJ_DATA);
485 goto done;
487 s += tlen;
488 if (!got_parse_sha1_digest((*commit)->tree_id->sha1, s)) {
489 err = got_error(GOT_ERR_BAD_OBJ_DATA);
490 goto done;
492 remain -= SHA1_DIGEST_STRING_LENGTH;
493 s += SHA1_DIGEST_STRING_LENGTH;
494 } else {
495 err = got_error(GOT_ERR_BAD_OBJ_DATA);
496 goto done;
499 tlen = strlen(GOT_COMMIT_TAG_PARENT);
500 while (strncmp(s, GOT_COMMIT_TAG_PARENT, tlen) == 0) {
501 remain -= tlen;
502 if (remain < SHA1_DIGEST_STRING_LENGTH) {
503 err = got_error(GOT_ERR_BAD_OBJ_DATA);
504 goto done;
506 s += tlen;
507 err = got_object_commit_add_parent(*commit, s);
508 if (err)
509 goto done;
511 remain -= SHA1_DIGEST_STRING_LENGTH;
512 s += SHA1_DIGEST_STRING_LENGTH;
515 tlen = strlen(GOT_COMMIT_TAG_AUTHOR);
516 if (strncmp(s, GOT_COMMIT_TAG_AUTHOR, tlen) == 0) {
517 char *p;
519 remain -= tlen;
520 if (remain <= 0) {
521 err = got_error(GOT_ERR_BAD_OBJ_DATA);
522 goto done;
524 s += tlen;
525 p = strchr(s, '\n');
526 if (p == NULL) {
527 err = got_error(GOT_ERR_BAD_OBJ_DATA);
528 goto done;
530 *p = '\0';
531 (*commit)->author = strdup(s);
532 if ((*commit)->author == NULL) {
533 err = got_error_from_errno();
534 goto done;
536 s += strlen((*commit)->author) + 1;
537 remain -= strlen((*commit)->author) + 1;
540 tlen = strlen(GOT_COMMIT_TAG_COMMITTER);
541 if (strncmp(s, GOT_COMMIT_TAG_COMMITTER, tlen) == 0) {
542 char *p;
544 remain -= tlen;
545 if (remain <= 0) {
546 err = got_error(GOT_ERR_BAD_OBJ_DATA);
547 goto done;
549 s += tlen;
550 p = strchr(s, '\n');
551 if (p == NULL) {
552 err = got_error(GOT_ERR_BAD_OBJ_DATA);
553 goto done;
555 *p = '\0';
556 (*commit)->committer = strdup(s);
557 if ((*commit)->committer == NULL) {
558 err = got_error_from_errno();
559 goto done;
561 s += strlen((*commit)->committer) + 1;
562 remain -= strlen((*commit)->committer) + 1;
565 (*commit)->logmsg = strndup(s, remain);
566 if ((*commit)->logmsg == NULL) {
567 err = got_error_from_errno();
568 goto done;
570 done:
571 if (err) {
572 got_object_commit_close(*commit);
573 *commit = NULL;
575 return err;
578 static void
579 tree_entry_close(struct got_tree_entry *te)
581 free(te->id);
582 free(te->name);
583 free(te);
586 struct got_tree_entry *
587 got_alloc_tree_entry_partial(void)
589 struct got_tree_entry *te;
591 te = calloc(1, sizeof(*te));
592 if (te == NULL)
593 return NULL;
595 te->id = calloc(1, sizeof(*te->id));
596 if (te->id == NULL) {
597 free(te);
598 te = NULL;
600 return te;
603 static const struct got_error *
604 parse_tree_entry(struct got_tree_entry **te, size_t *elen, char *buf,
605 size_t maxlen)
607 char *p = buf, *space;
608 const struct got_error *err = NULL;
610 *te = got_alloc_tree_entry_partial();
611 if (*te == NULL)
612 return got_error_from_errno();
614 *elen = strlen(buf) + 1;
615 if (*elen > maxlen) {
616 free(*te);
617 *te = NULL;
618 return got_error(GOT_ERR_BAD_OBJ_DATA);
621 space = strchr(buf, ' ');
622 if (space == NULL) {
623 err = got_error(GOT_ERR_BAD_OBJ_DATA);
624 free(*te);
625 *te = NULL;
626 return err;
628 while (*p != ' ') {
629 if (*p < '0' && *p > '7') {
630 err = got_error(GOT_ERR_BAD_OBJ_DATA);
631 goto done;
633 (*te)->mode <<= 3;
634 (*te)->mode |= *p - '0';
635 p++;
638 (*te)->name = strdup(space + 1);
639 if (*elen > maxlen || maxlen - *elen < SHA1_DIGEST_LENGTH) {
640 err = got_error(GOT_ERR_BAD_OBJ_DATA);
641 goto done;
643 buf += strlen(buf) + 1;
644 memcpy((*te)->id->sha1, buf, SHA1_DIGEST_LENGTH);
645 *elen += SHA1_DIGEST_LENGTH;
646 done:
647 if (err) {
648 tree_entry_close(*te);
649 *te = NULL;
651 return err;
654 static const struct got_error *
655 parse_tree_object(struct got_tree_object **tree, uint8_t *buf, size_t len)
657 const struct got_error *err;
658 size_t remain = len;
660 *tree = calloc(1, sizeof(**tree));
661 if (*tree == NULL)
662 return got_error_from_errno();
664 SIMPLEQ_INIT(&(*tree)->entries);
666 while (remain > 0) {
667 struct got_tree_entry *te;
668 size_t elen;
670 err = parse_tree_entry(&te, &elen, buf, remain);
671 if (err)
672 return err;
673 (*tree)->nentries++;
674 SIMPLEQ_INSERT_TAIL(&(*tree)->entries, te, entry);
675 buf += elen;
676 remain -= elen;
679 if (remain != 0) {
680 got_object_tree_close(*tree);
681 return got_error(GOT_ERR_BAD_OBJ_DATA);
684 return NULL;
687 static const struct got_error *
688 read_to_mem(uint8_t **outbuf, size_t *outlen, FILE *f)
690 const struct got_error *err = NULL;
691 static const size_t blocksize = 512;
692 size_t n, total, remain;
693 uint8_t *buf;
695 *outbuf = NULL;
696 *outlen = 0;
698 buf = calloc(1, blocksize);
699 if (buf == NULL)
700 return got_error_from_errno();
702 remain = blocksize;
703 total = 0;
704 while (1) {
705 if (remain == 0) {
706 uint8_t *newbuf;
707 newbuf = reallocarray(buf, 1, total + blocksize);
708 if (newbuf == NULL) {
709 err = got_error_from_errno();
710 goto done;
712 buf = newbuf;
713 remain += blocksize;
715 n = fread(buf + total, 1, remain, f);
716 if (n == 0) {
717 if (ferror(f)) {
718 err = got_ferror(f, GOT_ERR_IO);
719 goto done;
721 break; /* EOF */
723 remain -= n;
724 total += n;
725 };
727 done:
728 if (err == NULL) {
729 *outbuf = buf;
730 *outlen = total;
731 } else
732 free(buf);
733 return err;
736 static const struct got_error *
737 read_commit_object(struct got_commit_object **commit, struct got_object *obj,
738 FILE *f)
740 const struct got_error *err = NULL;
741 size_t len;
742 uint8_t *p;
744 if (obj->flags & GOT_OBJ_FLAG_PACKED)
745 err = read_to_mem(&p, &len, f);
746 else
747 err = got_inflate_to_mem(&p, &len, f);
748 if (err)
749 return err;
751 if (len < obj->hdrlen + obj->size) {
752 err = got_error(GOT_ERR_BAD_OBJ_DATA);
753 goto done;
756 /* Skip object header. */
757 len -= obj->hdrlen;
758 err = parse_commit_object(commit, p + obj->hdrlen, len);
759 free(p);
760 done:
761 return err;
764 static void
765 read_commit_object_privsep_child(struct got_object *obj, int obj_fd,
766 int imsg_fds[2])
768 const struct got_error *err = NULL;
769 struct got_commit_object *commit = NULL;
770 struct imsgbuf ibuf;
771 FILE *f = NULL;
772 int status = 0;
774 setproctitle("read commit object");
775 close(imsg_fds[0]);
776 imsg_init(&ibuf, imsg_fds[1]);
778 /* revoke access to most system calls */
779 if (pledge("stdio", NULL) == -1) {
780 err = got_error_from_errno();
781 goto done;
784 f = fdopen(obj_fd, "rb");
785 if (f == NULL) {
786 err = got_error_from_errno();
787 close(obj_fd);
788 goto done;
791 err = read_commit_object(&commit, obj, f);
792 if (err)
793 goto done;
795 err = got_privsep_send_commit(&ibuf, commit);
796 done:
797 if (commit)
798 got_object_commit_close(commit);
799 if (err) {
800 got_privsep_send_error(&ibuf, err);
801 status = 1;
803 if (f)
804 fclose(f);
805 imsg_clear(&ibuf);
806 close(imsg_fds[1]);
807 _exit(status);
810 static const struct got_error *
811 read_commit_object_privsep(struct got_commit_object **commit,
812 struct got_repository *repo, struct got_object *obj, int fd)
814 const struct got_error *err = NULL, *err_child = NULL;
815 struct imsgbuf parent_ibuf;
816 int imsg_fds[2];
817 pid_t pid;
819 if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_fds) == -1)
820 return got_error_from_errno();
822 pid = fork();
823 if (pid == -1)
824 return got_error_from_errno();
825 else if (pid == 0) {
826 read_commit_object_privsep_child(obj, fd, imsg_fds);
827 /* not reached */
830 close(imsg_fds[1]);
831 imsg_init(&parent_ibuf, imsg_fds[0]);
832 err = got_privsep_recv_commit(commit, &parent_ibuf);
833 imsg_clear(&parent_ibuf);
834 err_child = wait_for_child(pid);
835 close(imsg_fds[0]);
836 return err ? err : err_child;
839 const struct got_error *
840 got_object_commit_open(struct got_commit_object **commit,
841 struct got_repository *repo, struct got_object *obj)
843 const struct got_error *err = NULL;
845 if (obj->type != GOT_OBJ_TYPE_COMMIT)
846 return got_error(GOT_ERR_OBJ_TYPE);
848 if (obj->flags & GOT_OBJ_FLAG_PACKED) {
849 uint8_t *buf;
850 size_t len;
851 err = got_packfile_extract_object_to_mem(&buf, &len, obj, repo);
852 if (err)
853 return err;
854 obj->size = len;
855 err = parse_commit_object(commit, buf, len);
856 free(buf);
857 } else {
858 int fd;
859 err = open_loose_object(&fd, obj, repo);
860 if (err)
861 return err;
862 err = read_commit_object_privsep(commit, repo, obj, fd);
863 close(fd);
865 return err;
868 void
869 got_object_commit_close(struct got_commit_object *commit)
871 struct got_parent_id *pid;
873 while (!SIMPLEQ_EMPTY(&commit->parent_ids)) {
874 pid = SIMPLEQ_FIRST(&commit->parent_ids);
875 SIMPLEQ_REMOVE_HEAD(&commit->parent_ids, entry);
876 free(pid->id);
877 free(pid);
880 free(commit->tree_id);
881 free(commit->author);
882 free(commit->committer);
883 free(commit->logmsg);
884 free(commit);
887 static const struct got_error *
888 read_tree_object(struct got_tree_object **tree, struct got_object *obj, FILE *f)
890 const struct got_error *err = NULL;
891 size_t len;
892 uint8_t *p;
894 if (obj->flags & GOT_OBJ_FLAG_PACKED)
895 err = read_to_mem(&p, &len, f);
896 else
897 err = got_inflate_to_mem(&p, &len, f);
898 if (err)
899 return err;
901 if (len < obj->hdrlen + obj->size) {
902 err = got_error(GOT_ERR_BAD_OBJ_DATA);
903 goto done;
906 /* Skip object header. */
907 len -= obj->hdrlen;
908 err = parse_tree_object(tree, p + obj->hdrlen, len);
909 free(p);
910 done:
911 return err;
914 static void
915 read_tree_object_privsep_child(struct got_object *obj, int obj_fd,
916 int imsg_fds[2])
918 const struct got_error *err = NULL;
919 struct got_tree_object *tree = NULL;
920 struct imsgbuf ibuf;
921 FILE *f = NULL;
922 int status = 0;
924 setproctitle("read tree object");
925 close(imsg_fds[0]);
926 imsg_init(&ibuf, imsg_fds[1]);
928 /* revoke access to most system calls */
929 if (pledge("stdio", NULL) == -1) {
930 err = got_error_from_errno();
931 goto done;
934 f = fdopen(obj_fd, "rb");
935 if (f == NULL) {
936 err = got_error_from_errno();
937 close(obj_fd);
938 goto done;
941 err = read_tree_object(&tree, obj, f);
942 if (err)
943 goto done;
945 err = got_privsep_send_tree(&ibuf, tree);
946 done:
947 if (tree)
948 got_object_tree_close(tree);
949 if (err) {
950 got_privsep_send_error(&ibuf, err);
951 status = 1;
953 if (f)
954 fclose(f);
955 imsg_clear(&ibuf);
956 close(imsg_fds[1]);
957 _exit(status);
960 static const struct got_error *
961 read_tree_object_privsep(struct got_tree_object **tree, struct got_object *obj,
962 int fd)
964 const struct got_error *err = NULL, *err_child = NULL;
965 struct imsgbuf parent_ibuf;
966 int imsg_fds[2];
967 pid_t pid;
969 if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_fds) == -1)
970 return got_error_from_errno();
972 pid = fork();
973 if (pid == -1)
974 return got_error_from_errno();
975 else if (pid == 0) {
976 read_tree_object_privsep_child(obj, fd, imsg_fds);
977 /* not reached */
980 close(imsg_fds[1]);
981 imsg_init(&parent_ibuf, imsg_fds[0]);
982 err = got_privsep_recv_tree(tree, &parent_ibuf);
983 imsg_clear(&parent_ibuf);
984 err_child = wait_for_child(pid);
985 close(imsg_fds[0]);
986 return err ? err : err_child;
989 const struct got_error *
990 got_object_tree_open(struct got_tree_object **tree,
991 struct got_repository *repo, struct got_object *obj)
993 const struct got_error *err = NULL;
995 if (obj->type != GOT_OBJ_TYPE_TREE)
996 return got_error(GOT_ERR_OBJ_TYPE);
998 if (obj->flags & GOT_OBJ_FLAG_PACKED) {
999 uint8_t *buf;
1000 size_t len;
1001 err = got_packfile_extract_object_to_mem(&buf, &len, obj, repo);
1002 if (err)
1003 return err;
1004 obj->size = len;
1005 err = parse_tree_object(tree, buf, len);
1006 free(buf);
1007 } else {
1008 int fd;
1009 err = open_loose_object(&fd, obj, repo);
1010 if (err)
1011 return err;
1012 err = read_tree_object_privsep(tree, obj, fd);
1013 close(fd);
1015 return err;
1018 void
1019 got_object_tree_close(struct got_tree_object *tree)
1021 struct got_tree_entry *te;
1023 while (!SIMPLEQ_EMPTY(&tree->entries)) {
1024 te = SIMPLEQ_FIRST(&tree->entries);
1025 SIMPLEQ_REMOVE_HEAD(&tree->entries, entry);
1026 tree_entry_close(te);
1029 free(tree);
1032 static const struct got_error *
1033 read_blob_object_privsep_child(int outfd, int infd, int imsg_fds[2])
1035 const struct got_error *err = NULL;
1036 struct imsgbuf ibuf;
1037 int status = 0;
1038 size_t size;
1039 FILE *infile = NULL;
1041 setproctitle("read blob object");
1042 close(imsg_fds[0]);
1043 imsg_init(&ibuf, imsg_fds[1]);
1045 /* revoke access to most system calls */
1046 if (pledge("stdio", NULL) == -1) {
1047 err = got_error_from_errno();
1048 goto done;
1051 infile = fdopen(infd, "rb");
1052 if (infile == NULL) {
1053 err = got_error_from_errno();
1054 close(infd);
1055 goto done;
1057 err = got_inflate_to_fd(&size, infile, outfd);
1058 fclose(infile);
1059 if (err)
1060 goto done;
1062 err = got_privsep_send_blob(&ibuf, size);
1063 done:
1064 if (err) {
1065 got_privsep_send_error(&ibuf, err);
1066 status = 1;
1068 close(outfd);
1069 imsg_clear(&ibuf);
1070 close(imsg_fds[1]);
1071 _exit(status);
1074 static const struct got_error *
1075 read_blob_object_privsep(size_t *size, int outfd, int infd)
1077 struct imsgbuf parent_ibuf;
1078 int imsg_fds[2];
1079 const struct got_error *err = NULL, *err_child = NULL;
1080 pid_t pid;
1082 if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_fds) == -1)
1083 return got_error_from_errno();
1085 pid = fork();
1086 if (pid == -1)
1087 return got_error_from_errno();
1088 else if (pid == 0) {
1089 read_blob_object_privsep_child(outfd, infd, imsg_fds);
1090 /* not reached */
1093 close(imsg_fds[1]);
1094 imsg_init(&parent_ibuf, imsg_fds[0]);
1095 err = got_privsep_recv_blob(size, &parent_ibuf);
1096 imsg_clear(&parent_ibuf);
1097 err_child = wait_for_child(pid);
1098 close(imsg_fds[0]);
1099 if (lseek(outfd, SEEK_SET, 0) == -1)
1100 err = got_error_from_errno();
1101 return err ? err : err_child;
1104 const struct got_error *
1105 got_object_blob_open(struct got_blob_object **blob,
1106 struct got_repository *repo, struct got_object *obj, size_t blocksize)
1108 const struct got_error *err = NULL;
1110 if (obj->type != GOT_OBJ_TYPE_BLOB)
1111 return got_error(GOT_ERR_OBJ_TYPE);
1113 if (blocksize < obj->hdrlen)
1114 return got_error(GOT_ERR_NO_SPACE);
1116 *blob = calloc(1, sizeof(**blob));
1117 if (*blob == NULL)
1118 return got_error_from_errno();
1120 (*blob)->read_buf = calloc(1, blocksize);
1121 if ((*blob)->read_buf == NULL) {
1122 err = got_error_from_errno();
1123 goto done;
1125 if (obj->flags & GOT_OBJ_FLAG_PACKED) {
1126 err = got_packfile_extract_object(&((*blob)->f), obj, repo);
1127 if (err)
1128 goto done;
1129 } else {
1130 int infd, outfd;
1131 size_t size;
1132 struct stat sb;
1134 err = open_loose_object(&infd, obj, repo);
1135 if (err)
1136 goto done;
1139 outfd = got_opentempfd();
1140 if (outfd == -1) {
1141 err = got_error_from_errno();
1142 close(infd);
1143 goto done;
1146 err = read_blob_object_privsep(&size, outfd, infd);
1147 close(infd);
1148 if (err)
1149 goto done;
1151 if (size != obj->hdrlen + obj->size) {
1152 err = got_error(GOT_ERR_PRIVSEP_LEN);
1153 close(outfd);
1154 goto done;
1157 if (fstat(outfd, &sb) == -1) {
1158 err = got_error_from_errno();
1159 close(outfd);
1160 goto done;
1163 if (sb.st_size != size) {
1164 err = got_error(GOT_ERR_PRIVSEP_LEN);
1165 close(outfd);
1166 goto done;
1169 (*blob)->f = fdopen(outfd, "rb");
1170 if ((*blob)->f == NULL) {
1171 err = got_error_from_errno();
1172 close(outfd);
1173 goto done;
1177 (*blob)->hdrlen = obj->hdrlen;
1178 (*blob)->blocksize = blocksize;
1179 memcpy(&(*blob)->id.sha1, obj->id.sha1, SHA1_DIGEST_LENGTH);
1181 done:
1182 if (err && *blob) {
1183 if ((*blob)->f)
1184 fclose((*blob)->f);
1185 free((*blob)->read_buf);
1186 free(*blob);
1187 *blob = NULL;
1189 return err;
1192 void
1193 got_object_blob_close(struct got_blob_object *blob)
1195 free(blob->read_buf);
1196 fclose(blob->f);
1197 free(blob);
1200 char *
1201 got_object_blob_id_str(struct got_blob_object *blob, char *buf, size_t size)
1203 return got_sha1_digest_to_str(blob->id.sha1, buf, size);
1206 size_t
1207 got_object_blob_get_hdrlen(struct got_blob_object *blob)
1209 return blob->hdrlen;
1212 const uint8_t *
1213 got_object_blob_get_read_buf(struct got_blob_object *blob)
1215 return blob->read_buf;
1218 const struct got_error *
1219 got_object_blob_read_block(size_t *outlenp, struct got_blob_object *blob)
1221 size_t n;
1223 n = fread(blob->read_buf, 1, blob->blocksize, blob->f);
1224 if (n == 0 && ferror(blob->f))
1225 return got_ferror(blob->f, GOT_ERR_IO);
1226 *outlenp = n;
1227 return NULL;