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"
40 #include "got_lib_sha1.h"
41 #include "got_lib_delta.h"
42 #include "got_lib_pack.h"
43 #include "got_lib_path.h"
44 #include "got_lib_zbuf.h"
45 #include "got_lib_object.h"
46 #include "got_lib_privsep.h"
48 #ifndef MIN
49 #define MIN(_a,_b) ((_a) < (_b) ? (_a) : (_b))
50 #endif
52 #ifndef nitems
53 #define nitems(_a) (sizeof(_a) / sizeof((_a)[0]))
54 #endif
56 #define GOT_OBJ_TAG_COMMIT "commit"
57 #define GOT_OBJ_TAG_TREE "tree"
58 #define GOT_OBJ_TAG_BLOB "blob"
60 #define GOT_COMMIT_TAG_TREE "tree "
61 #define GOT_COMMIT_TAG_PARENT "parent "
62 #define GOT_COMMIT_TAG_AUTHOR "author "
63 #define GOT_COMMIT_TAG_COMMITTER "committer "
65 const struct got_error *
66 got_object_id_str(char **outbuf, struct got_object_id *id)
67 {
68 static const size_t len = SHA1_DIGEST_STRING_LENGTH;
70 *outbuf = calloc(1, len);
71 if (*outbuf == NULL)
72 return got_error_from_errno();
74 if (got_sha1_digest_to_str(id->sha1, *outbuf, len) == NULL) {
75 free(*outbuf);
76 *outbuf = NULL;
77 return got_error(GOT_ERR_BAD_OBJ_ID_STR);
78 }
80 return NULL;
81 }
83 int
84 got_object_id_cmp(struct got_object_id *id1, struct got_object_id *id2)
85 {
86 return memcmp(id1->sha1, id2->sha1, SHA1_DIGEST_LENGTH);
87 }
89 struct got_object_id *
90 got_object_id_dup(struct got_object_id *id1)
91 {
92 struct got_object_id *id2;
94 id2 = malloc(sizeof(*id2));
95 if (id2 == NULL)
96 return NULL;
97 memcpy(id2, id1, sizeof(*id2));
98 return id2;
99 }
101 struct got_object_id *
102 got_object_get_id(struct got_object *obj)
104 return got_object_id_dup(&obj->id);
107 int
108 got_object_get_type(struct got_object *obj)
110 switch (obj->type) {
111 case GOT_OBJ_TYPE_COMMIT:
112 case GOT_OBJ_TYPE_TREE:
113 case GOT_OBJ_TYPE_BLOB:
114 case GOT_OBJ_TYPE_TAG:
115 return obj->type;
116 default:
117 abort();
118 break;
121 /* not reached */
122 return 0;
125 static const struct got_error *
126 parse_object_header(struct got_object **obj, char *buf, size_t len)
128 const char *obj_tags[] = {
129 GOT_OBJ_TAG_COMMIT,
130 GOT_OBJ_TAG_TREE,
131 GOT_OBJ_TAG_BLOB
132 };
133 const int obj_types[] = {
134 GOT_OBJ_TYPE_COMMIT,
135 GOT_OBJ_TYPE_TREE,
136 GOT_OBJ_TYPE_BLOB,
137 };
138 int type = 0;
139 size_t size = 0, hdrlen = 0;
140 int i;
141 char *p = strchr(buf, '\0');
143 if (p == NULL)
144 return got_error(GOT_ERR_BAD_OBJ_HDR);
146 hdrlen = strlen(buf) + 1 /* '\0' */;
148 for (i = 0; i < nitems(obj_tags); i++) {
149 const char *tag = obj_tags[i];
150 size_t tlen = strlen(tag);
151 const char *errstr;
153 if (strncmp(buf, tag, tlen) != 0)
154 continue;
156 type = obj_types[i];
157 if (len <= tlen)
158 return got_error(GOT_ERR_BAD_OBJ_HDR);
159 size = strtonum(buf + tlen, 0, LONG_MAX, &errstr);
160 if (errstr != NULL)
161 return got_error(GOT_ERR_BAD_OBJ_HDR);
162 break;
165 if (type == 0)
166 return got_error(GOT_ERR_BAD_OBJ_HDR);
168 *obj = calloc(1, sizeof(**obj));
169 if (*obj == NULL)
170 return got_error_from_errno();
171 (*obj)->type = type;
172 (*obj)->hdrlen = hdrlen;
173 (*obj)->size = size;
174 return NULL;
177 static const struct got_error *
178 read_object_header(struct got_object **obj, FILE *f)
180 const struct got_error *err;
181 struct got_zstream_buf zb;
182 char *buf;
183 const size_t zbsize = 64;
184 size_t outlen, totlen;
185 int i;
187 buf = calloc(zbsize, sizeof(char));
188 if (buf == NULL)
189 return got_error_from_errno();
191 err = got_inflate_init(&zb, NULL, zbsize);
192 if (err)
193 return err;
195 i = 0;
196 totlen = 0;
197 do {
198 err = got_inflate_read(&zb, f, &outlen);
199 if (err)
200 goto done;
201 if (strchr(zb.outbuf, '\0') == NULL) {
202 buf = recallocarray(buf, 1 + i, 2 + i, zbsize);
203 if (buf == NULL) {
204 err = got_error_from_errno();
205 goto done;
208 memcpy(buf + totlen, zb.outbuf, outlen);
209 totlen += outlen;
210 i++;
211 } while (strchr(zb.outbuf, '\0') == NULL);
213 err = parse_object_header(obj, buf, totlen);
214 done:
215 got_inflate_end(&zb);
216 return err;
219 static void
220 read_object_header_privsep_child(int obj_fd, int imsg_fds[2])
222 const struct got_error *err = NULL;
223 struct got_object *obj = NULL;
224 struct imsgbuf ibuf;
225 FILE *f = NULL;
226 int status = 0;
228 setproctitle("got: read object header");
229 close(imsg_fds[0]);
230 imsg_init(&ibuf, imsg_fds[1]);
232 /* revoke access to most system calls */
233 if (pledge("stdio", NULL) == -1) {
234 err = got_error_from_errno();
235 goto done;
238 f = fdopen(obj_fd, "rb");
239 if (f == NULL) {
240 err = got_error_from_errno();
241 close(obj_fd);
242 goto done;
245 err = read_object_header(&obj, f);
246 if (err)
247 goto done;
249 err = got_privsep_send_obj(&ibuf, obj, 0);
250 done:
251 if (obj)
252 got_object_close(obj);
253 if (err) {
254 got_privsep_send_error(&ibuf, err);
255 status = 1;
257 if (f)
258 fclose(f);
259 imsg_clear(&ibuf);
260 close(imsg_fds[1]);
261 _exit(status);
264 static const struct got_error *
265 read_object_header_privsep(struct got_object **obj, int fd)
267 struct imsgbuf parent_ibuf;
268 int imsg_fds[2];
269 const struct got_error *err = NULL;
270 pid_t pid;
271 int child_status;
273 if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_fds) == -1)
274 return got_error_from_errno();
276 pid = fork();
277 if (pid == -1)
278 return got_error_from_errno();
279 else if (pid == 0) {
280 read_object_header_privsep_child(fd, imsg_fds);
281 /* not reached */
284 close(imsg_fds[1]);
285 imsg_init(&parent_ibuf, imsg_fds[0]);
286 err = got_privsep_recv_obj(obj, &parent_ibuf);
287 imsg_clear(&parent_ibuf);
288 waitpid(pid, &child_status, 0);
289 close(imsg_fds[0]);
290 return err;
293 static const struct got_error *
294 object_path(char **path, struct got_object_id *id, struct got_repository *repo)
296 const struct got_error *err = NULL;
297 char *hex;
298 char *path_objects = got_repo_get_path_objects(repo);
300 *path = NULL;
302 if (path_objects == NULL)
303 return got_error_from_errno();
305 err = got_object_id_str(&hex, id);
306 if (err)
307 return err;
309 if (asprintf(path, "%s/%.2x/%s", path_objects,
310 id->sha1[0], hex + 2) == -1)
311 err = got_error_from_errno();
313 free(hex);
314 free(path_objects);
315 return err;
318 static const struct got_error *
319 open_loose_object(int *fd, struct got_object *obj, struct got_repository *repo)
321 const struct got_error *err = NULL;
322 char *path;
324 err = object_path(&path, &obj->id, repo);
325 if (err)
326 return err;
327 *fd = open(path, O_RDONLY | O_NOFOLLOW, GOT_DEFAULT_FILE_MODE);
328 if (*fd == -1) {
329 err = got_error_from_errno();
330 goto done;
332 done:
333 free(path);
334 return err;
337 const struct got_error *
338 got_object_open(struct got_object **obj, struct got_repository *repo,
339 struct got_object_id *id)
341 const struct got_error *err = NULL;
342 char *path;
343 int fd;
345 err = object_path(&path, id, repo);
346 if (err)
347 return err;
349 fd = open(path, O_RDONLY | O_NOFOLLOW, GOT_DEFAULT_FILE_MODE);
350 if (fd == -1) {
351 if (errno != ENOENT) {
352 err = got_error_from_errno();
353 goto done;
355 err = got_packfile_open_object(obj, id, repo);
356 if (err)
357 goto done;
358 if (*obj == NULL)
359 err = got_error(GOT_ERR_NO_OBJ);
360 } else {
361 err = read_object_header_privsep(obj, fd);
362 if (err)
363 goto done;
364 memcpy((*obj)->id.sha1, id->sha1, SHA1_DIGEST_LENGTH);
366 done:
367 free(path);
368 if (fd != -1)
369 close(fd);
370 return err;
374 const struct got_error *
375 got_object_open_by_id_str(struct got_object **obj, struct got_repository *repo,
376 const char *id_str)
378 struct got_object_id id;
380 if (!got_parse_sha1_digest(id.sha1, id_str))
381 return got_error(GOT_ERR_BAD_OBJ_ID_STR);
383 return got_object_open(obj, repo, &id);
386 void
387 got_object_close(struct got_object *obj)
389 if (obj->flags & GOT_OBJ_FLAG_DELTIFIED) {
390 struct got_delta *delta;
391 while (!SIMPLEQ_EMPTY(&obj->deltas.entries)) {
392 delta = SIMPLEQ_FIRST(&obj->deltas.entries);
393 SIMPLEQ_REMOVE_HEAD(&obj->deltas.entries, entry);
394 got_delta_close(delta);
397 if (obj->flags & GOT_OBJ_FLAG_PACKED)
398 free(obj->path_packfile);
399 free(obj);
402 struct got_commit_object *
403 got_object_commit_alloc_partial(void)
405 struct got_commit_object *commit;
407 commit = calloc(1, sizeof(*commit));
408 if (commit == NULL)
409 return NULL;
410 commit->tree_id = calloc(1, sizeof(*commit->tree_id));
411 if (commit->tree_id == NULL) {
412 free(commit);
413 return NULL;
416 SIMPLEQ_INIT(&commit->parent_ids);
418 return commit;
421 const struct got_error *
422 got_object_commit_add_parent(struct got_commit_object *commit,
423 const char *id_str)
425 const struct got_error *err = NULL;
426 struct got_parent_id *pid;
428 pid = calloc(1, sizeof(*pid));
429 if (pid == NULL)
430 return got_error_from_errno();
432 pid->id = calloc(1, sizeof(*pid->id));
433 if (pid->id == NULL) {
434 err = got_error_from_errno();
435 free(pid);
436 return err;
439 if (!got_parse_sha1_digest(pid->id->sha1, id_str)) {
440 err = got_error(GOT_ERR_BAD_OBJ_DATA);
441 free(pid->id);
442 free(pid);
443 return err;
446 SIMPLEQ_INSERT_TAIL(&commit->parent_ids, pid, entry);
447 commit->nparents++;
449 return NULL;
452 static const struct got_error *
453 parse_commit_object(struct got_commit_object **commit, char *buf, size_t len)
455 const struct got_error *err = NULL;
456 char *s = buf;
457 size_t tlen;
458 ssize_t remain = (ssize_t)len;
460 *commit = got_object_commit_alloc_partial();
461 if (*commit == NULL)
462 return got_error_from_errno();
464 tlen = strlen(GOT_COMMIT_TAG_TREE);
465 if (strncmp(s, GOT_COMMIT_TAG_TREE, tlen) == 0) {
466 remain -= tlen;
467 if (remain < SHA1_DIGEST_STRING_LENGTH) {
468 err = got_error(GOT_ERR_BAD_OBJ_DATA);
469 goto done;
471 s += tlen;
472 if (!got_parse_sha1_digest((*commit)->tree_id->sha1, s)) {
473 err = got_error(GOT_ERR_BAD_OBJ_DATA);
474 goto done;
476 remain -= SHA1_DIGEST_STRING_LENGTH;
477 s += SHA1_DIGEST_STRING_LENGTH;
478 } else {
479 err = got_error(GOT_ERR_BAD_OBJ_DATA);
480 goto done;
483 tlen = strlen(GOT_COMMIT_TAG_PARENT);
484 while (strncmp(s, GOT_COMMIT_TAG_PARENT, tlen) == 0) {
485 remain -= tlen;
486 if (remain < SHA1_DIGEST_STRING_LENGTH) {
487 err = got_error(GOT_ERR_BAD_OBJ_DATA);
488 goto done;
490 s += tlen;
491 err = got_object_commit_add_parent(*commit, s);
492 if (err)
493 goto done;
495 remain -= SHA1_DIGEST_STRING_LENGTH;
496 s += SHA1_DIGEST_STRING_LENGTH;
499 tlen = strlen(GOT_COMMIT_TAG_AUTHOR);
500 if (strncmp(s, GOT_COMMIT_TAG_AUTHOR, tlen) == 0) {
501 char *p;
503 remain -= tlen;
504 if (remain <= 0) {
505 err = got_error(GOT_ERR_BAD_OBJ_DATA);
506 goto done;
508 s += tlen;
509 p = strchr(s, '\n');
510 if (p == NULL) {
511 err = got_error(GOT_ERR_BAD_OBJ_DATA);
512 goto done;
514 *p = '\0';
515 (*commit)->author = strdup(s);
516 if ((*commit)->author == NULL) {
517 err = got_error_from_errno();
518 goto done;
520 s += strlen((*commit)->author) + 1;
521 remain -= strlen((*commit)->author) + 1;
524 tlen = strlen(GOT_COMMIT_TAG_COMMITTER);
525 if (strncmp(s, GOT_COMMIT_TAG_COMMITTER, tlen) == 0) {
526 char *p;
528 remain -= tlen;
529 if (remain <= 0) {
530 err = got_error(GOT_ERR_BAD_OBJ_DATA);
531 goto done;
533 s += tlen;
534 p = strchr(s, '\n');
535 if (p == NULL) {
536 err = got_error(GOT_ERR_BAD_OBJ_DATA);
537 goto done;
539 *p = '\0';
540 (*commit)->committer = strdup(s);
541 if ((*commit)->committer == NULL) {
542 err = got_error_from_errno();
543 goto done;
545 s += strlen((*commit)->committer) + 1;
546 remain -= strlen((*commit)->committer) + 1;
549 (*commit)->logmsg = strndup(s, remain);
550 if ((*commit)->logmsg == NULL) {
551 err = got_error_from_errno();
552 goto done;
554 done:
555 if (err) {
556 got_object_commit_close(*commit);
557 *commit = NULL;
559 return err;
562 static void
563 tree_entry_close(struct got_tree_entry *te)
565 free(te->id);
566 free(te->name);
567 free(te);
570 struct got_tree_entry *
571 got_alloc_tree_entry_partial(void)
573 struct got_tree_entry *te;
575 te = calloc(1, sizeof(*te));
576 if (te == NULL)
577 return NULL;
579 te->id = calloc(1, sizeof(*te->id));
580 if (te->id == NULL) {
581 free(te);
582 te = NULL;
584 return te;
587 static const struct got_error *
588 parse_tree_entry(struct got_tree_entry **te, size_t *elen, char *buf,
589 size_t maxlen)
591 char *p = buf, *space;
592 const struct got_error *err = NULL;
594 *te = got_alloc_tree_entry_partial();
595 if (*te == NULL)
596 return got_error_from_errno();
598 *elen = strlen(buf) + 1;
599 if (*elen > maxlen) {
600 free(*te);
601 *te = NULL;
602 return got_error(GOT_ERR_BAD_OBJ_DATA);
605 space = strchr(buf, ' ');
606 if (space == NULL) {
607 err = got_error(GOT_ERR_BAD_OBJ_DATA);
608 free(*te);
609 *te = NULL;
610 return err;
612 while (*p != ' ') {
613 if (*p < '0' && *p > '7') {
614 err = got_error(GOT_ERR_BAD_OBJ_DATA);
615 goto done;
617 (*te)->mode <<= 3;
618 (*te)->mode |= *p - '0';
619 p++;
622 (*te)->name = strdup(space + 1);
623 if (*elen > maxlen || maxlen - *elen < SHA1_DIGEST_LENGTH) {
624 err = got_error(GOT_ERR_BAD_OBJ_DATA);
625 goto done;
627 buf += strlen(buf) + 1;
628 memcpy((*te)->id->sha1, buf, SHA1_DIGEST_LENGTH);
629 *elen += SHA1_DIGEST_LENGTH;
630 done:
631 if (err) {
632 tree_entry_close(*te);
633 *te = NULL;
635 return err;
638 static const struct got_error *
639 parse_tree_object(struct got_tree_object **tree, uint8_t *buf, size_t len)
641 const struct got_error *err;
642 size_t remain = len;
644 *tree = calloc(1, sizeof(**tree));
645 if (*tree == NULL)
646 return got_error_from_errno();
648 SIMPLEQ_INIT(&(*tree)->entries);
650 while (remain > 0) {
651 struct got_tree_entry *te;
652 size_t elen;
654 err = parse_tree_entry(&te, &elen, buf, remain);
655 if (err)
656 return err;
657 (*tree)->nentries++;
658 SIMPLEQ_INSERT_TAIL(&(*tree)->entries, te, entry);
659 buf += elen;
660 remain -= elen;
663 if (remain != 0) {
664 got_object_tree_close(*tree);
665 return got_error(GOT_ERR_BAD_OBJ_DATA);
668 return NULL;
671 static const struct got_error *
672 read_to_mem(uint8_t **outbuf, size_t *outlen, FILE *f)
674 const struct got_error *err = NULL;
675 static const size_t blocksize = 512;
676 size_t n, total, remain;
677 uint8_t *buf;
679 *outbuf = NULL;
680 *outlen = 0;
682 buf = calloc(1, blocksize);
683 if (buf == NULL)
684 return got_error_from_errno();
686 remain = blocksize;
687 total = 0;
688 while (1) {
689 if (remain == 0) {
690 uint8_t *newbuf;
691 newbuf = reallocarray(buf, 1, total + blocksize);
692 if (newbuf == NULL) {
693 err = got_error_from_errno();
694 goto done;
696 buf = newbuf;
697 remain += blocksize;
699 n = fread(buf + total, 1, remain, f);
700 if (n == 0) {
701 if (ferror(f)) {
702 err = got_ferror(f, GOT_ERR_IO);
703 goto done;
705 break; /* EOF */
707 remain -= n;
708 total += n;
709 };
711 done:
712 if (err == NULL) {
713 *outbuf = buf;
714 *outlen = total;
715 } else
716 free(buf);
717 return err;
720 static const struct got_error *
721 read_commit_object(struct got_commit_object **commit, struct got_object *obj,
722 FILE *f)
724 const struct got_error *err = NULL;
725 size_t len;
726 uint8_t *p;
728 if (obj->flags & GOT_OBJ_FLAG_PACKED)
729 err = read_to_mem(&p, &len, f);
730 else
731 err = got_inflate_to_mem(&p, &len, f);
732 if (err)
733 return err;
735 if (len < obj->hdrlen + obj->size) {
736 err = got_error(GOT_ERR_BAD_OBJ_DATA);
737 goto done;
740 /* Skip object header. */
741 len -= obj->hdrlen;
742 err = parse_commit_object(commit, p + obj->hdrlen, len);
743 free(p);
744 done:
745 return err;
748 static void
749 read_commit_object_privsep_child(struct got_object *obj, int obj_fd,
750 int imsg_fds[2])
752 const struct got_error *err = NULL;
753 struct got_commit_object *commit = NULL;
754 struct imsgbuf ibuf;
755 FILE *f = NULL;
756 int status = 0;
758 setproctitle("got: read commit object");
759 close(imsg_fds[0]);
760 imsg_init(&ibuf, imsg_fds[1]);
762 /* revoke access to most system calls */
763 if (pledge("stdio", NULL) == -1) {
764 err = got_error_from_errno();
765 goto done;
768 f = fdopen(obj_fd, "rb");
769 if (f == NULL) {
770 err = got_error_from_errno();
771 close(obj_fd);
772 goto done;
775 err = read_commit_object(&commit, obj, f);
776 if (err)
777 goto done;
779 err = got_privsep_send_commit(&ibuf, commit);
780 done:
781 if (commit)
782 got_object_commit_close(commit);
783 if (err) {
784 got_privsep_send_error(&ibuf, err);
785 status = 1;
787 if (f)
788 fclose(f);
789 imsg_clear(&ibuf);
790 close(imsg_fds[1]);
791 _exit(status);
794 static const struct got_error *
795 read_commit_object_privsep(struct got_commit_object **commit,
796 struct got_repository *repo, struct got_object *obj, int fd)
798 const struct got_error *err = NULL;
799 struct imsgbuf parent_ibuf;
800 int imsg_fds[2];
801 pid_t pid;
802 int child_status;
804 if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_fds) == -1)
805 return got_error_from_errno();
807 pid = fork();
808 if (pid == -1)
809 return got_error_from_errno();
810 else if (pid == 0) {
811 read_commit_object_privsep_child(obj, fd, imsg_fds);
812 /* not reached */
815 close(imsg_fds[1]);
816 imsg_init(&parent_ibuf, imsg_fds[0]);
817 err = got_privsep_recv_commit(commit, &parent_ibuf);
818 imsg_clear(&parent_ibuf);
819 waitpid(pid, &child_status, 0);
820 close(imsg_fds[0]);
821 return err;
824 const struct got_error *
825 got_object_commit_open(struct got_commit_object **commit,
826 struct got_repository *repo, struct got_object *obj)
828 const struct got_error *err = NULL;
830 if (obj->type != GOT_OBJ_TYPE_COMMIT)
831 return got_error(GOT_ERR_OBJ_TYPE);
833 if (obj->flags & GOT_OBJ_FLAG_PACKED) {
834 uint8_t *buf;
835 size_t len;
836 err = got_packfile_extract_object_to_mem(&buf, &len, obj, repo);
837 if (err)
838 return err;
839 obj->size = len;
840 err = parse_commit_object(commit, buf, len);
841 free(buf);
842 } else {
843 int fd;
844 err = open_loose_object(&fd, obj, repo);
845 if (err)
846 return err;
847 err = read_commit_object_privsep(commit, repo, obj, fd);
848 close(fd);
850 return err;
853 void
854 got_object_commit_close(struct got_commit_object *commit)
856 struct got_parent_id *pid;
858 while (!SIMPLEQ_EMPTY(&commit->parent_ids)) {
859 pid = SIMPLEQ_FIRST(&commit->parent_ids);
860 SIMPLEQ_REMOVE_HEAD(&commit->parent_ids, entry);
861 free(pid->id);
862 free(pid);
865 free(commit->tree_id);
866 free(commit->author);
867 free(commit->committer);
868 free(commit->logmsg);
869 free(commit);
872 static const struct got_error *
873 read_tree_object(struct got_tree_object **tree, struct got_object *obj, FILE *f)
875 const struct got_error *err = NULL;
876 size_t len;
877 uint8_t *p;
879 if (obj->flags & GOT_OBJ_FLAG_PACKED)
880 err = read_to_mem(&p, &len, f);
881 else
882 err = got_inflate_to_mem(&p, &len, f);
883 if (err)
884 return err;
886 if (len < obj->hdrlen + obj->size) {
887 err = got_error(GOT_ERR_BAD_OBJ_DATA);
888 goto done;
891 /* Skip object header. */
892 len -= obj->hdrlen;
893 err = parse_tree_object(tree, p + obj->hdrlen, len);
894 free(p);
895 done:
896 return err;
899 static void
900 read_tree_object_privsep_child(struct got_object *obj, int obj_fd,
901 int imsg_fds[2])
903 const struct got_error *err = NULL;
904 struct got_tree_object *tree = NULL;
905 struct imsgbuf ibuf;
906 FILE *f = NULL;
907 int status = 0;
909 setproctitle("got: read tree object");
910 close(imsg_fds[0]);
911 imsg_init(&ibuf, imsg_fds[1]);
913 /* revoke access to most system calls */
914 if (pledge("stdio", NULL) == -1) {
915 err = got_error_from_errno();
916 goto done;
919 f = fdopen(obj_fd, "rb");
920 if (f == NULL) {
921 err = got_error_from_errno();
922 close(obj_fd);
923 goto done;
926 err = read_tree_object(&tree, obj, f);
927 if (err)
928 goto done;
930 err = got_privsep_send_tree(&ibuf, tree);
931 done:
932 if (tree)
933 got_object_tree_close(tree);
934 if (err) {
935 got_privsep_send_error(&ibuf, err);
936 status = 1;
938 if (f)
939 fclose(f);
940 imsg_clear(&ibuf);
941 close(imsg_fds[1]);
942 _exit(status);
945 static const struct got_error *
946 read_tree_object_privsep(struct got_tree_object **tree, struct got_object *obj,
947 int fd)
949 const struct got_error *err = NULL;
950 struct imsgbuf parent_ibuf;
951 int imsg_fds[2];
952 pid_t pid;
953 int child_status;
955 if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_fds) == -1)
956 return got_error_from_errno();
958 pid = fork();
959 if (pid == -1)
960 return got_error_from_errno();
961 else if (pid == 0) {
962 read_tree_object_privsep_child(obj, fd, imsg_fds);
963 /* not reached */
966 close(imsg_fds[1]);
967 imsg_init(&parent_ibuf, imsg_fds[0]);
968 err = got_privsep_recv_tree(tree, &parent_ibuf);
969 imsg_clear(&parent_ibuf);
970 waitpid(pid, &child_status, 0);
971 close(imsg_fds[0]);
972 return err;
975 const struct got_error *
976 got_object_tree_open(struct got_tree_object **tree,
977 struct got_repository *repo, struct got_object *obj)
979 const struct got_error *err = NULL;
981 if (obj->type != GOT_OBJ_TYPE_TREE)
982 return got_error(GOT_ERR_OBJ_TYPE);
984 if (obj->flags & GOT_OBJ_FLAG_PACKED) {
985 uint8_t *buf;
986 size_t len;
987 err = got_packfile_extract_object_to_mem(&buf, &len, obj, repo);
988 if (err)
989 return err;
990 obj->size = len;
991 err = parse_tree_object(tree, buf, len);
992 free(buf);
993 } else {
994 int fd;
995 err = open_loose_object(&fd, obj, repo);
996 if (err)
997 return err;
998 err = read_tree_object_privsep(tree, obj, fd);
999 close(fd);
1001 return err;
1004 void
1005 got_object_tree_close(struct got_tree_object *tree)
1007 struct got_tree_entry *te;
1009 while (!SIMPLEQ_EMPTY(&tree->entries)) {
1010 te = SIMPLEQ_FIRST(&tree->entries);
1011 SIMPLEQ_REMOVE_HEAD(&tree->entries, entry);
1012 tree_entry_close(te);
1015 free(tree);
1018 static const struct got_error *
1019 read_blob_object(size_t *size, int outfd, int infd)
1021 return got_inflate_to_fd(size, infd, outfd);
1024 static const struct got_error *
1025 read_blob_object_privsep_child(int outfd, int infd, int imsg_fds[2])
1027 const struct got_error *err = NULL;
1028 struct imsgbuf ibuf;
1029 int status = 0;
1030 size_t size;
1032 setproctitle("got: read blob object");
1033 close(imsg_fds[0]);
1034 imsg_init(&ibuf, imsg_fds[1]);
1036 /* revoke access to most system calls */
1037 if (pledge("stdio", NULL) == -1) {
1038 err = got_error_from_errno();
1039 goto done;
1042 err = read_blob_object(&size, outfd, infd);
1043 close(infd);
1044 if (err)
1045 goto done;
1047 err = got_privsep_send_blob(&ibuf, size);
1048 done:
1049 if (err) {
1050 got_privsep_send_error(&ibuf, err);
1051 status = 1;
1053 close(outfd);
1054 imsg_clear(&ibuf);
1055 close(imsg_fds[1]);
1056 _exit(status);
1059 static const struct got_error *
1060 read_blob_object_privsep(size_t *size, int outfd, int infd)
1062 struct imsgbuf parent_ibuf;
1063 int imsg_fds[2];
1064 const struct got_error *err = NULL;
1065 pid_t pid;
1066 int child_status;
1068 if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_fds) == -1)
1069 return got_error_from_errno();
1071 pid = fork();
1072 if (pid == -1)
1073 return got_error_from_errno();
1074 else if (pid == 0) {
1075 read_blob_object_privsep_child(outfd, infd, imsg_fds);
1076 /* not reached */
1079 close(imsg_fds[1]);
1080 imsg_init(&parent_ibuf, imsg_fds[0]);
1081 err = got_privsep_recv_blob(size, &parent_ibuf);
1082 imsg_clear(&parent_ibuf);
1083 waitpid(pid, &child_status, 0);
1084 close(imsg_fds[0]);
1085 if (lseek(outfd, SEEK_SET, 0) == -1)
1086 err = got_error_from_errno();
1087 return err;
1090 const struct got_error *
1091 got_object_blob_open(struct got_blob_object **blob,
1092 struct got_repository *repo, struct got_object *obj, size_t blocksize)
1094 const struct got_error *err = NULL;
1096 if (obj->type != GOT_OBJ_TYPE_BLOB)
1097 return got_error(GOT_ERR_OBJ_TYPE);
1099 if (blocksize < obj->hdrlen)
1100 return got_error(GOT_ERR_NO_SPACE);
1102 *blob = calloc(1, sizeof(**blob));
1103 if (*blob == NULL)
1104 return got_error_from_errno();
1106 (*blob)->read_buf = calloc(1, blocksize);
1107 if ((*blob)->read_buf == NULL) {
1108 err = got_error_from_errno();
1109 goto done;
1111 if (obj->flags & GOT_OBJ_FLAG_PACKED) {
1112 err = got_packfile_extract_object(&((*blob)->f), obj, repo);
1113 if (err)
1114 goto done;
1115 } else {
1116 int infd, outfd;
1117 size_t size;
1118 struct stat sb;
1120 err = open_loose_object(&infd, obj, repo);
1121 if (err)
1122 goto done;
1125 outfd = got_opentempfd();
1126 if (outfd == -1) {
1127 err = got_error_from_errno();
1128 close(infd);
1129 goto done;
1132 err = read_blob_object_privsep(&size, outfd, infd);
1133 close(infd);
1134 if (err)
1135 goto done;
1137 if (size != obj->hdrlen + obj->size) {
1138 err = got_error(GOT_ERR_PRIVSEP_LEN);
1139 close(outfd);
1140 goto done;
1143 if (fstat(outfd, &sb) == -1) {
1144 err = got_error_from_errno();
1145 close(outfd);
1146 goto done;
1149 if (sb.st_size != size) {
1150 err = got_error(GOT_ERR_PRIVSEP_LEN);
1151 close(outfd);
1152 goto done;
1155 (*blob)->f = fdopen(outfd, "rb");
1156 if ((*blob)->f == NULL) {
1157 err = got_error_from_errno();
1158 close(outfd);
1159 goto done;
1163 (*blob)->hdrlen = obj->hdrlen;
1164 (*blob)->blocksize = blocksize;
1165 memcpy(&(*blob)->id.sha1, obj->id.sha1, SHA1_DIGEST_LENGTH);
1167 done:
1168 if (err && *blob) {
1169 if ((*blob)->f)
1170 fclose((*blob)->f);
1171 free((*blob)->read_buf);
1172 free(*blob);
1173 *blob = NULL;
1175 return err;
1178 void
1179 got_object_blob_close(struct got_blob_object *blob)
1181 free(blob->read_buf);
1182 fclose(blob->f);
1183 free(blob);
1186 char *
1187 got_object_blob_id_str(struct got_blob_object *blob, char *buf, size_t size)
1189 return got_sha1_digest_to_str(blob->id.sha1, buf, size);
1192 size_t
1193 got_object_blob_get_hdrlen(struct got_blob_object *blob)
1195 return blob->hdrlen;
1198 const uint8_t *
1199 got_object_blob_get_read_buf(struct got_blob_object *blob)
1201 return blob->read_buf;
1204 const struct got_error *
1205 got_object_blob_read_block(size_t *outlenp, struct got_blob_object *blob)
1207 size_t n;
1209 n = fread(blob->read_buf, 1, blob->blocksize, blob->f);
1210 if (n == 0 && ferror(blob->f))
1211 return got_ferror(blob->f, GOT_ERR_IO);
1212 *outlenp = n;
1213 return NULL;