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>
35 #include <time.h>
37 #include "got_error.h"
38 #include "got_object.h"
39 #include "got_repository.h"
40 #include "got_opentemp.h"
42 #include "got_lib_sha1.h"
43 #include "got_lib_delta.h"
44 #include "got_lib_pack.h"
45 #include "got_lib_path.h"
46 #include "got_lib_zbuf.h"
47 #include "got_lib_object.h"
48 #include "got_lib_privsep.h"
50 #ifndef MIN
51 #define MIN(_a,_b) ((_a) < (_b) ? (_a) : (_b))
52 #endif
54 #ifndef nitems
55 #define nitems(_a) (sizeof(_a) / sizeof((_a)[0]))
56 #endif
58 #define GOT_OBJ_TAG_COMMIT "commit"
59 #define GOT_OBJ_TAG_TREE "tree"
60 #define GOT_OBJ_TAG_BLOB "blob"
62 #define GOT_COMMIT_TAG_TREE "tree "
63 #define GOT_COMMIT_TAG_PARENT "parent "
64 #define GOT_COMMIT_TAG_AUTHOR "author "
65 #define GOT_COMMIT_TAG_COMMITTER "committer "
67 const struct got_error *
68 got_object_id_str(char **outbuf, struct got_object_id *id)
69 {
70 static const size_t len = SHA1_DIGEST_STRING_LENGTH;
72 *outbuf = calloc(1, len);
73 if (*outbuf == NULL)
74 return got_error_from_errno();
76 if (got_sha1_digest_to_str(id->sha1, *outbuf, len) == NULL) {
77 free(*outbuf);
78 *outbuf = NULL;
79 return got_error(GOT_ERR_BAD_OBJ_ID_STR);
80 }
82 return NULL;
83 }
85 int
86 got_object_id_cmp(struct got_object_id *id1, struct got_object_id *id2)
87 {
88 return memcmp(id1->sha1, id2->sha1, SHA1_DIGEST_LENGTH);
89 }
91 struct got_object_id *
92 got_object_id_dup(struct got_object_id *id1)
93 {
94 struct got_object_id *id2;
96 id2 = malloc(sizeof(*id2));
97 if (id2 == NULL)
98 return NULL;
99 memcpy(id2, id1, sizeof(*id2));
100 return id2;
103 struct got_object_id *
104 got_object_get_id(struct got_object *obj)
106 return got_object_id_dup(&obj->id);
109 const struct got_error *
110 got_object_get_id_str(char **outbuf, struct got_object *obj)
112 return got_object_id_str(outbuf, &obj->id);
115 int
116 got_object_get_type(struct got_object *obj)
118 switch (obj->type) {
119 case GOT_OBJ_TYPE_COMMIT:
120 case GOT_OBJ_TYPE_TREE:
121 case GOT_OBJ_TYPE_BLOB:
122 case GOT_OBJ_TYPE_TAG:
123 return obj->type;
124 default:
125 abort();
126 break;
129 /* not reached */
130 return 0;
133 static const struct got_error *
134 parse_object_header(struct got_object **obj, char *buf, size_t len)
136 const char *obj_tags[] = {
137 GOT_OBJ_TAG_COMMIT,
138 GOT_OBJ_TAG_TREE,
139 GOT_OBJ_TAG_BLOB
140 };
141 const int obj_types[] = {
142 GOT_OBJ_TYPE_COMMIT,
143 GOT_OBJ_TYPE_TREE,
144 GOT_OBJ_TYPE_BLOB,
145 };
146 int type = 0;
147 size_t size = 0, hdrlen = 0;
148 int i;
149 char *p = strchr(buf, '\0');
151 if (p == NULL)
152 return got_error(GOT_ERR_BAD_OBJ_HDR);
154 hdrlen = strlen(buf) + 1 /* '\0' */;
156 for (i = 0; i < nitems(obj_tags); i++) {
157 const char *tag = obj_tags[i];
158 size_t tlen = strlen(tag);
159 const char *errstr;
161 if (strncmp(buf, tag, tlen) != 0)
162 continue;
164 type = obj_types[i];
165 if (len <= tlen)
166 return got_error(GOT_ERR_BAD_OBJ_HDR);
167 size = strtonum(buf + tlen, 0, LONG_MAX, &errstr);
168 if (errstr != NULL)
169 return got_error(GOT_ERR_BAD_OBJ_HDR);
170 break;
173 if (type == 0)
174 return got_error(GOT_ERR_BAD_OBJ_HDR);
176 *obj = calloc(1, sizeof(**obj));
177 if (*obj == NULL)
178 return got_error_from_errno();
179 (*obj)->type = type;
180 (*obj)->hdrlen = hdrlen;
181 (*obj)->size = size;
182 return NULL;
185 static const struct got_error *
186 read_object_header(struct got_object **obj, FILE *f)
188 const struct got_error *err;
189 struct got_zstream_buf zb;
190 char *buf;
191 const size_t zbsize = 64;
192 size_t outlen, totlen;
193 int i;
195 buf = calloc(zbsize, sizeof(char));
196 if (buf == NULL)
197 return got_error_from_errno();
199 err = got_inflate_init(&zb, NULL, zbsize);
200 if (err)
201 return err;
203 i = 0;
204 totlen = 0;
205 do {
206 err = got_inflate_read(&zb, f, &outlen);
207 if (err)
208 goto done;
209 if (strchr(zb.outbuf, '\0') == NULL) {
210 buf = recallocarray(buf, 1 + i, 2 + i, zbsize);
211 if (buf == NULL) {
212 err = got_error_from_errno();
213 goto done;
216 memcpy(buf + totlen, zb.outbuf, outlen);
217 totlen += outlen;
218 i++;
219 } while (strchr(zb.outbuf, '\0') == NULL);
221 err = parse_object_header(obj, buf, totlen);
222 done:
223 got_inflate_end(&zb);
224 return err;
227 static void
228 read_object_header_privsep_child(int obj_fd, int imsg_fds[2])
230 const struct got_error *err = NULL;
231 struct got_object *obj = NULL;
232 struct imsgbuf ibuf;
233 FILE *f = NULL;
234 int status = 0;
236 setproctitle("read object header");
237 close(imsg_fds[0]);
238 imsg_init(&ibuf, imsg_fds[1]);
240 /* revoke access to most system calls */
241 if (pledge("stdio", NULL) == -1) {
242 err = got_error_from_errno();
243 goto done;
246 f = fdopen(obj_fd, "rb");
247 if (f == NULL) {
248 err = got_error_from_errno();
249 close(obj_fd);
250 goto done;
253 err = read_object_header(&obj, f);
254 if (err)
255 goto done;
257 err = got_privsep_send_obj(&ibuf, obj, 0);
258 done:
259 if (obj)
260 got_object_close(obj);
261 if (err) {
262 got_privsep_send_error(&ibuf, err);
263 status = 1;
265 if (f)
266 fclose(f);
267 imsg_clear(&ibuf);
268 close(imsg_fds[1]);
269 _exit(status);
272 static const struct got_error *
273 wait_for_child(pid_t pid)
275 int child_status;
277 waitpid(pid, &child_status, 0);
279 if (!WIFEXITED(child_status))
280 return got_error(GOT_ERR_PRIVSEP_DIED);
282 if (WEXITSTATUS(child_status) != 0)
283 return got_error(GOT_ERR_PRIVSEP_EXIT);
285 return NULL;
288 static const struct got_error *
289 read_object_header_privsep(struct got_object **obj, int fd)
291 struct imsgbuf parent_ibuf;
292 int imsg_fds[2];
293 const struct got_error *err = NULL, *err_child = NULL;
294 pid_t pid;
296 if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_fds) == -1)
297 return got_error_from_errno();
299 pid = fork();
300 if (pid == -1)
301 return got_error_from_errno();
302 else if (pid == 0) {
303 read_object_header_privsep_child(fd, imsg_fds);
304 /* not reached */
307 close(imsg_fds[1]);
308 imsg_init(&parent_ibuf, imsg_fds[0]);
309 err = got_privsep_recv_obj(obj, &parent_ibuf);
310 imsg_clear(&parent_ibuf);
311 err_child = wait_for_child(pid);
312 close(imsg_fds[0]);
313 return err ? err : err_child;
316 static const struct got_error *
317 object_path(char **path, struct got_object_id *id, struct got_repository *repo)
319 const struct got_error *err = NULL;
320 char *hex;
321 char *path_objects = got_repo_get_path_objects(repo);
323 *path = NULL;
325 if (path_objects == NULL)
326 return got_error_from_errno();
328 err = got_object_id_str(&hex, id);
329 if (err)
330 return err;
332 if (asprintf(path, "%s/%.2x/%s", path_objects,
333 id->sha1[0], hex + 2) == -1)
334 err = got_error_from_errno();
336 free(hex);
337 free(path_objects);
338 return err;
341 static const struct got_error *
342 open_loose_object(int *fd, struct got_object *obj, struct got_repository *repo)
344 const struct got_error *err = NULL;
345 char *path;
347 err = object_path(&path, &obj->id, repo);
348 if (err)
349 return err;
350 *fd = open(path, O_RDONLY | O_NOFOLLOW, GOT_DEFAULT_FILE_MODE);
351 if (*fd == -1) {
352 err = got_error_from_errno();
353 goto done;
355 done:
356 free(path);
357 return err;
360 const struct got_error *
361 got_object_open(struct got_object **obj, struct got_repository *repo,
362 struct got_object_id *id)
364 const struct got_error *err = NULL;
365 char *path;
366 int fd;
368 err = object_path(&path, id, repo);
369 if (err)
370 return err;
372 fd = open(path, O_RDONLY | O_NOFOLLOW, GOT_DEFAULT_FILE_MODE);
373 if (fd == -1) {
374 if (errno != ENOENT) {
375 err = got_error_from_errno();
376 goto done;
378 err = got_packfile_open_object(obj, id, repo);
379 if (err)
380 goto done;
381 if (*obj == NULL)
382 err = got_error(GOT_ERR_NO_OBJ);
383 } else {
384 err = read_object_header_privsep(obj, fd);
385 if (err)
386 goto done;
387 memcpy((*obj)->id.sha1, id->sha1, SHA1_DIGEST_LENGTH);
389 done:
390 free(path);
391 if (fd != -1)
392 close(fd);
393 return err;
397 const struct got_error *
398 got_object_open_by_id_str(struct got_object **obj, struct got_repository *repo,
399 const char *id_str)
401 struct got_object_id id;
403 if (!got_parse_sha1_digest(id.sha1, id_str))
404 return got_error(GOT_ERR_BAD_OBJ_ID_STR);
406 return got_object_open(obj, repo, &id);
409 void
410 got_object_close(struct got_object *obj)
412 if (obj->flags & GOT_OBJ_FLAG_DELTIFIED) {
413 struct got_delta *delta;
414 while (!SIMPLEQ_EMPTY(&obj->deltas.entries)) {
415 delta = SIMPLEQ_FIRST(&obj->deltas.entries);
416 SIMPLEQ_REMOVE_HEAD(&obj->deltas.entries, entry);
417 got_delta_close(delta);
420 if (obj->flags & GOT_OBJ_FLAG_PACKED)
421 free(obj->path_packfile);
422 free(obj);
425 struct got_commit_object *
426 got_object_commit_alloc_partial(void)
428 struct got_commit_object *commit;
430 commit = calloc(1, sizeof(*commit));
431 if (commit == NULL)
432 return NULL;
433 commit->tree_id = calloc(1, sizeof(*commit->tree_id));
434 if (commit->tree_id == NULL) {
435 free(commit);
436 return NULL;
439 SIMPLEQ_INIT(&commit->parent_ids);
441 return commit;
444 const struct got_error *
445 got_object_open_as_commit(struct got_commit_object **commit,
446 struct got_repository *repo, struct got_object_id *id)
448 const struct got_error *err;
449 struct got_object *obj;
451 *commit = NULL;
453 err = got_object_open(&obj, repo, id);
454 if (err)
455 return err;
456 if (got_object_get_type(obj) != GOT_OBJ_TYPE_COMMIT) {
457 err = got_error(GOT_ERR_OBJ_TYPE);
458 goto done;
461 err = got_object_commit_open(commit, repo, obj);
462 done:
463 got_object_close(obj);
464 return err;
467 const struct got_error *
468 got_object_commit_add_parent(struct got_commit_object *commit,
469 const char *id_str)
471 const struct got_error *err = NULL;
472 struct got_object_qid *qid;
474 qid = calloc(1, sizeof(*qid));
475 if (qid == NULL)
476 return got_error_from_errno();
478 qid->id = calloc(1, sizeof(*qid->id));
479 if (qid->id == NULL) {
480 err = got_error_from_errno();
481 free(qid);
482 return err;
485 if (!got_parse_sha1_digest(qid->id->sha1, id_str)) {
486 err = got_error(GOT_ERR_BAD_OBJ_DATA);
487 free(qid->id);
488 free(qid);
489 return err;
492 SIMPLEQ_INSERT_TAIL(&commit->parent_ids, qid, entry);
493 commit->nparents++;
495 return NULL;
498 static const struct got_error *
499 parse_gmtoff(time_t *gmtoff, const char *tzstr)
501 int sign = 1;
502 const char *p = tzstr;
503 time_t h, m;
505 *gmtoff = 0;
507 if (*p == '-')
508 sign = -1;
509 else if (*p != '+')
510 return got_error(GOT_ERR_BAD_OBJ_DATA);
511 p++;
512 if (!isdigit(*p) && !isdigit(*(p + 1)))
513 return got_error(GOT_ERR_BAD_OBJ_DATA);
514 h = (((*p - '0') * 10) + (*(p + 1) - '0'));
516 p += 2;
517 if (!isdigit(*p) && !isdigit(*(p + 1)))
518 return got_error(GOT_ERR_BAD_OBJ_DATA);
519 m = ((*p - '0') * 10) + (*(p + 1) - '0');
521 *gmtoff = (h * 60 * 60 + m * 60) * sign;
522 return NULL;
525 static const struct got_error *
526 parse_commit_time(struct tm *tm, char *committer)
528 const struct got_error *err = NULL;
529 const char *errstr;
530 char *space, *tzstr;
531 time_t gmtoff;
532 time_t time;
534 /* Parse and strip off trailing timezone indicator string. */
535 space = strrchr(committer, ' ');
536 if (space == NULL)
537 return got_error(GOT_ERR_BAD_OBJ_DATA);
538 tzstr = strdup(space + 1);
539 if (tzstr == NULL)
540 return got_error_from_errno();
541 err = parse_gmtoff(&gmtoff, tzstr);
542 free(tzstr);
543 if (err)
544 return err;
545 *space = '\0';
547 /* Timestamp is separated from committer name + email by space. */
548 space = strrchr(committer, ' ');
549 if (space == NULL)
550 return got_error(GOT_ERR_BAD_OBJ_DATA);
552 /* Timestamp parsed here is expressed in comitter's local time. */
553 time = strtonum(space + 1, 0, INT64_MAX, &errstr);
554 if (errstr)
555 return got_error(GOT_ERR_BAD_OBJ_DATA);
557 /* Express the time stamp in UTC. */
558 memset(tm, 0, sizeof(*tm));
559 time -= gmtoff;
560 if (localtime_r(&time, tm) == NULL)
561 return got_error_from_errno();
562 tm->tm_gmtoff = gmtoff;
564 /* Strip off parsed time information, leaving just author and email. */
565 *space = '\0';
567 return NULL;
570 static const struct got_error *
571 parse_commit_object(struct got_commit_object **commit, char *buf, size_t len)
573 const struct got_error *err = NULL;
574 char *s = buf;
575 size_t tlen;
576 ssize_t remain = (ssize_t)len;
578 *commit = got_object_commit_alloc_partial();
579 if (*commit == NULL)
580 return got_error_from_errno();
582 tlen = strlen(GOT_COMMIT_TAG_TREE);
583 if (strncmp(s, GOT_COMMIT_TAG_TREE, tlen) == 0) {
584 remain -= tlen;
585 if (remain < SHA1_DIGEST_STRING_LENGTH) {
586 err = got_error(GOT_ERR_BAD_OBJ_DATA);
587 goto done;
589 s += tlen;
590 if (!got_parse_sha1_digest((*commit)->tree_id->sha1, s)) {
591 err = got_error(GOT_ERR_BAD_OBJ_DATA);
592 goto done;
594 remain -= SHA1_DIGEST_STRING_LENGTH;
595 s += SHA1_DIGEST_STRING_LENGTH;
596 } else {
597 err = got_error(GOT_ERR_BAD_OBJ_DATA);
598 goto done;
601 tlen = strlen(GOT_COMMIT_TAG_PARENT);
602 while (strncmp(s, GOT_COMMIT_TAG_PARENT, tlen) == 0) {
603 remain -= tlen;
604 if (remain < SHA1_DIGEST_STRING_LENGTH) {
605 err = got_error(GOT_ERR_BAD_OBJ_DATA);
606 goto done;
608 s += tlen;
609 err = got_object_commit_add_parent(*commit, s);
610 if (err)
611 goto done;
613 remain -= SHA1_DIGEST_STRING_LENGTH;
614 s += SHA1_DIGEST_STRING_LENGTH;
617 tlen = strlen(GOT_COMMIT_TAG_AUTHOR);
618 if (strncmp(s, GOT_COMMIT_TAG_AUTHOR, tlen) == 0) {
619 char *p;
620 size_t slen;
622 remain -= tlen;
623 if (remain <= 0) {
624 err = got_error(GOT_ERR_BAD_OBJ_DATA);
625 goto done;
627 s += tlen;
628 p = strchr(s, '\n');
629 if (p == NULL) {
630 err = got_error(GOT_ERR_BAD_OBJ_DATA);
631 goto done;
633 *p = '\0';
634 slen = strlen(s);
635 err = parse_commit_time(&(*commit)->tm_author, s);
636 if (err)
637 goto done;
638 (*commit)->author = strdup(s);
639 if ((*commit)->author == NULL) {
640 err = got_error_from_errno();
641 goto done;
643 s += slen + 1;
644 remain -= slen + 1;
647 tlen = strlen(GOT_COMMIT_TAG_COMMITTER);
648 if (strncmp(s, GOT_COMMIT_TAG_COMMITTER, tlen) == 0) {
649 char *p;
650 size_t slen;
652 remain -= tlen;
653 if (remain <= 0) {
654 err = got_error(GOT_ERR_BAD_OBJ_DATA);
655 goto done;
657 s += tlen;
658 p = strchr(s, '\n');
659 if (p == NULL) {
660 err = got_error(GOT_ERR_BAD_OBJ_DATA);
661 goto done;
663 *p = '\0';
664 slen = strlen(s);
665 err = parse_commit_time(&(*commit)->tm_committer, s);
666 if (err)
667 goto done;
668 (*commit)->committer = strdup(s);
669 if ((*commit)->committer == NULL) {
670 err = got_error_from_errno();
671 goto done;
673 s += slen + 1;
674 remain -= slen + 1;
677 (*commit)->logmsg = strndup(s, remain);
678 if ((*commit)->logmsg == NULL) {
679 err = got_error_from_errno();
680 goto done;
682 done:
683 if (err) {
684 got_object_commit_close(*commit);
685 *commit = NULL;
687 return err;
690 static void
691 tree_entry_close(struct got_tree_entry *te)
693 free(te->id);
694 free(te->name);
695 free(te);
698 struct got_tree_entry *
699 got_alloc_tree_entry_partial(void)
701 struct got_tree_entry *te;
703 te = calloc(1, sizeof(*te));
704 if (te == NULL)
705 return NULL;
707 te->id = calloc(1, sizeof(*te->id));
708 if (te->id == NULL) {
709 free(te);
710 te = NULL;
712 return te;
715 static const struct got_error *
716 parse_tree_entry(struct got_tree_entry **te, size_t *elen, char *buf,
717 size_t maxlen)
719 char *p = buf, *space;
720 const struct got_error *err = NULL;
722 *te = got_alloc_tree_entry_partial();
723 if (*te == NULL)
724 return got_error_from_errno();
726 *elen = strlen(buf) + 1;
727 if (*elen > maxlen) {
728 free(*te);
729 *te = NULL;
730 return got_error(GOT_ERR_BAD_OBJ_DATA);
733 space = strchr(buf, ' ');
734 if (space == NULL) {
735 err = got_error(GOT_ERR_BAD_OBJ_DATA);
736 free(*te);
737 *te = NULL;
738 return err;
740 while (*p != ' ') {
741 if (*p < '0' && *p > '7') {
742 err = got_error(GOT_ERR_BAD_OBJ_DATA);
743 goto done;
745 (*te)->mode <<= 3;
746 (*te)->mode |= *p - '0';
747 p++;
750 (*te)->name = strdup(space + 1);
751 if (*elen > maxlen || maxlen - *elen < SHA1_DIGEST_LENGTH) {
752 err = got_error(GOT_ERR_BAD_OBJ_DATA);
753 goto done;
755 buf += strlen(buf) + 1;
756 memcpy((*te)->id->sha1, buf, SHA1_DIGEST_LENGTH);
757 *elen += SHA1_DIGEST_LENGTH;
758 done:
759 if (err) {
760 tree_entry_close(*te);
761 *te = NULL;
763 return err;
766 static const struct got_error *
767 parse_tree_object(struct got_tree_object **tree, uint8_t *buf, size_t len)
769 const struct got_error *err;
770 size_t remain = len;
772 *tree = calloc(1, sizeof(**tree));
773 if (*tree == NULL)
774 return got_error_from_errno();
776 SIMPLEQ_INIT(&(*tree)->entries);
778 while (remain > 0) {
779 struct got_tree_entry *te;
780 size_t elen;
782 err = parse_tree_entry(&te, &elen, buf, remain);
783 if (err)
784 return err;
785 (*tree)->nentries++;
786 SIMPLEQ_INSERT_TAIL(&(*tree)->entries, te, entry);
787 buf += elen;
788 remain -= elen;
791 if (remain != 0) {
792 got_object_tree_close(*tree);
793 return got_error(GOT_ERR_BAD_OBJ_DATA);
796 return NULL;
799 static const struct got_error *
800 read_to_mem(uint8_t **outbuf, size_t *outlen, FILE *f)
802 const struct got_error *err = NULL;
803 static const size_t blocksize = 512;
804 size_t n, total, remain;
805 uint8_t *buf;
807 *outbuf = NULL;
808 *outlen = 0;
810 buf = calloc(1, blocksize);
811 if (buf == NULL)
812 return got_error_from_errno();
814 remain = blocksize;
815 total = 0;
816 while (1) {
817 if (remain == 0) {
818 uint8_t *newbuf;
819 newbuf = reallocarray(buf, 1, total + blocksize);
820 if (newbuf == NULL) {
821 err = got_error_from_errno();
822 goto done;
824 buf = newbuf;
825 remain += blocksize;
827 n = fread(buf + total, 1, remain, f);
828 if (n == 0) {
829 if (ferror(f)) {
830 err = got_ferror(f, GOT_ERR_IO);
831 goto done;
833 break; /* EOF */
835 remain -= n;
836 total += n;
837 };
839 done:
840 if (err == NULL) {
841 *outbuf = buf;
842 *outlen = total;
843 } else
844 free(buf);
845 return err;
848 static const struct got_error *
849 read_commit_object(struct got_commit_object **commit, struct got_object *obj,
850 FILE *f)
852 const struct got_error *err = NULL;
853 size_t len;
854 uint8_t *p;
856 if (obj->flags & GOT_OBJ_FLAG_PACKED)
857 err = read_to_mem(&p, &len, f);
858 else
859 err = got_inflate_to_mem(&p, &len, f);
860 if (err)
861 return err;
863 if (len < obj->hdrlen + obj->size) {
864 err = got_error(GOT_ERR_BAD_OBJ_DATA);
865 goto done;
868 /* Skip object header. */
869 len -= obj->hdrlen;
870 err = parse_commit_object(commit, p + obj->hdrlen, len);
871 free(p);
872 done:
873 return err;
876 static void
877 read_commit_object_privsep_child(struct got_object *obj, int obj_fd,
878 int imsg_fds[2])
880 const struct got_error *err = NULL;
881 struct got_commit_object *commit = NULL;
882 struct imsgbuf ibuf;
883 FILE *f = NULL;
884 int status = 0;
886 setproctitle("read commit object");
887 close(imsg_fds[0]);
888 imsg_init(&ibuf, imsg_fds[1]);
890 /* revoke access to most system calls */
891 if (pledge("stdio", NULL) == -1) {
892 err = got_error_from_errno();
893 goto done;
896 f = fdopen(obj_fd, "rb");
897 if (f == NULL) {
898 err = got_error_from_errno();
899 close(obj_fd);
900 goto done;
903 err = read_commit_object(&commit, obj, f);
904 if (err)
905 goto done;
907 err = got_privsep_send_commit(&ibuf, commit);
908 done:
909 if (commit)
910 got_object_commit_close(commit);
911 if (err) {
912 got_privsep_send_error(&ibuf, err);
913 status = 1;
915 if (f)
916 fclose(f);
917 imsg_clear(&ibuf);
918 close(imsg_fds[1]);
919 _exit(status);
922 static const struct got_error *
923 read_commit_object_privsep(struct got_commit_object **commit,
924 struct got_repository *repo, struct got_object *obj, int fd)
926 const struct got_error *err = NULL, *err_child = NULL;
927 struct imsgbuf parent_ibuf;
928 int imsg_fds[2];
929 pid_t pid;
931 if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_fds) == -1)
932 return got_error_from_errno();
934 pid = fork();
935 if (pid == -1)
936 return got_error_from_errno();
937 else if (pid == 0) {
938 read_commit_object_privsep_child(obj, fd, imsg_fds);
939 /* not reached */
942 close(imsg_fds[1]);
943 imsg_init(&parent_ibuf, imsg_fds[0]);
944 err = got_privsep_recv_commit(commit, &parent_ibuf);
945 imsg_clear(&parent_ibuf);
946 err_child = wait_for_child(pid);
947 close(imsg_fds[0]);
948 return err ? err : err_child;
951 const struct got_error *
952 got_object_commit_open(struct got_commit_object **commit,
953 struct got_repository *repo, struct got_object *obj)
955 const struct got_error *err = NULL;
957 if (obj->type != GOT_OBJ_TYPE_COMMIT)
958 return got_error(GOT_ERR_OBJ_TYPE);
960 if (obj->flags & GOT_OBJ_FLAG_PACKED) {
961 uint8_t *buf;
962 size_t len;
963 err = got_packfile_extract_object_to_mem(&buf, &len, obj, repo);
964 if (err)
965 return err;
966 obj->size = len;
967 err = parse_commit_object(commit, buf, len);
968 free(buf);
969 } else {
970 int fd;
971 err = open_loose_object(&fd, obj, repo);
972 if (err)
973 return err;
974 err = read_commit_object_privsep(commit, repo, obj, fd);
975 close(fd);
977 return err;
980 void
981 got_object_commit_close(struct got_commit_object *commit)
983 struct got_object_qid *qid;
985 while (!SIMPLEQ_EMPTY(&commit->parent_ids)) {
986 qid = SIMPLEQ_FIRST(&commit->parent_ids);
987 SIMPLEQ_REMOVE_HEAD(&commit->parent_ids, entry);
988 free(qid->id);
989 free(qid);
992 free(commit->tree_id);
993 free(commit->author);
994 free(commit->committer);
995 free(commit->logmsg);
996 free(commit);
999 static const struct got_error *
1000 read_tree_object(struct got_tree_object **tree, struct got_object *obj, FILE *f)
1002 const struct got_error *err = NULL;
1003 size_t len;
1004 uint8_t *p;
1006 if (obj->flags & GOT_OBJ_FLAG_PACKED)
1007 err = read_to_mem(&p, &len, f);
1008 else
1009 err = got_inflate_to_mem(&p, &len, f);
1010 if (err)
1011 return err;
1013 if (len < obj->hdrlen + obj->size) {
1014 err = got_error(GOT_ERR_BAD_OBJ_DATA);
1015 goto done;
1018 /* Skip object header. */
1019 len -= obj->hdrlen;
1020 err = parse_tree_object(tree, p + obj->hdrlen, len);
1021 free(p);
1022 done:
1023 return err;
1026 static void
1027 read_tree_object_privsep_child(struct got_object *obj, int obj_fd,
1028 int imsg_fds[2])
1030 const struct got_error *err = NULL;
1031 struct got_tree_object *tree = NULL;
1032 struct imsgbuf ibuf;
1033 FILE *f = NULL;
1034 int status = 0;
1036 setproctitle("read tree object");
1037 close(imsg_fds[0]);
1038 imsg_init(&ibuf, imsg_fds[1]);
1040 /* revoke access to most system calls */
1041 if (pledge("stdio", NULL) == -1) {
1042 err = got_error_from_errno();
1043 goto done;
1046 f = fdopen(obj_fd, "rb");
1047 if (f == NULL) {
1048 err = got_error_from_errno();
1049 close(obj_fd);
1050 goto done;
1053 err = read_tree_object(&tree, obj, f);
1054 if (err)
1055 goto done;
1057 err = got_privsep_send_tree(&ibuf, tree);
1058 done:
1059 if (tree)
1060 got_object_tree_close(tree);
1061 if (err) {
1062 got_privsep_send_error(&ibuf, err);
1063 status = 1;
1065 if (f)
1066 fclose(f);
1067 imsg_clear(&ibuf);
1068 close(imsg_fds[1]);
1069 _exit(status);
1072 static const struct got_error *
1073 read_tree_object_privsep(struct got_tree_object **tree, struct got_object *obj,
1074 int fd)
1076 const struct got_error *err = NULL, *err_child = NULL;
1077 struct imsgbuf parent_ibuf;
1078 int imsg_fds[2];
1079 pid_t pid;
1081 if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_fds) == -1)
1082 return got_error_from_errno();
1084 pid = fork();
1085 if (pid == -1)
1086 return got_error_from_errno();
1087 else if (pid == 0) {
1088 read_tree_object_privsep_child(obj, fd, imsg_fds);
1089 /* not reached */
1092 close(imsg_fds[1]);
1093 imsg_init(&parent_ibuf, imsg_fds[0]);
1094 err = got_privsep_recv_tree(tree, &parent_ibuf);
1095 imsg_clear(&parent_ibuf);
1096 err_child = wait_for_child(pid);
1097 close(imsg_fds[0]);
1098 return err ? err : err_child;
1101 const struct got_error *
1102 got_object_tree_open(struct got_tree_object **tree,
1103 struct got_repository *repo, struct got_object *obj)
1105 const struct got_error *err = NULL;
1107 if (obj->type != GOT_OBJ_TYPE_TREE)
1108 return got_error(GOT_ERR_OBJ_TYPE);
1110 if (obj->flags & GOT_OBJ_FLAG_PACKED) {
1111 uint8_t *buf;
1112 size_t len;
1113 err = got_packfile_extract_object_to_mem(&buf, &len, obj, repo);
1114 if (err)
1115 return err;
1116 obj->size = len;
1117 err = parse_tree_object(tree, buf, len);
1118 free(buf);
1119 } else {
1120 int fd;
1121 err = open_loose_object(&fd, obj, repo);
1122 if (err)
1123 return err;
1124 err = read_tree_object_privsep(tree, obj, fd);
1125 close(fd);
1127 return err;
1130 const struct got_error *
1131 got_object_open_as_tree(struct got_tree_object **tree,
1132 struct got_repository *repo, struct got_object_id *id)
1134 const struct got_error *err;
1135 struct got_object *obj;
1137 *tree = NULL;
1139 err = got_object_open(&obj, repo, id);
1140 if (err)
1141 return err;
1142 if (got_object_get_type(obj) != GOT_OBJ_TYPE_TREE) {
1143 err = got_error(GOT_ERR_OBJ_TYPE);
1144 goto done;
1147 err = got_object_tree_open(tree, repo, obj);
1148 done:
1149 got_object_close(obj);
1150 return err;
1153 void
1154 got_object_tree_close(struct got_tree_object *tree)
1156 struct got_tree_entry *te;
1158 while (!SIMPLEQ_EMPTY(&tree->entries)) {
1159 te = SIMPLEQ_FIRST(&tree->entries);
1160 SIMPLEQ_REMOVE_HEAD(&tree->entries, entry);
1161 tree_entry_close(te);
1164 free(tree);
1167 static const struct got_error *
1168 read_blob_object_privsep_child(int outfd, int infd, int imsg_fds[2])
1170 const struct got_error *err = NULL;
1171 struct imsgbuf ibuf;
1172 int status = 0;
1173 size_t size;
1174 FILE *infile = NULL;
1176 setproctitle("read blob object");
1177 close(imsg_fds[0]);
1178 imsg_init(&ibuf, imsg_fds[1]);
1180 /* revoke access to most system calls */
1181 if (pledge("stdio", NULL) == -1) {
1182 err = got_error_from_errno();
1183 goto done;
1186 infile = fdopen(infd, "rb");
1187 if (infile == NULL) {
1188 err = got_error_from_errno();
1189 close(infd);
1190 goto done;
1192 err = got_inflate_to_fd(&size, infile, outfd);
1193 fclose(infile);
1194 if (err)
1195 goto done;
1197 err = got_privsep_send_blob(&ibuf, size);
1198 done:
1199 if (err) {
1200 got_privsep_send_error(&ibuf, err);
1201 status = 1;
1203 close(outfd);
1204 imsg_clear(&ibuf);
1205 close(imsg_fds[1]);
1206 _exit(status);
1209 static const struct got_error *
1210 read_blob_object_privsep(size_t *size, int outfd, int infd)
1212 struct imsgbuf parent_ibuf;
1213 int imsg_fds[2];
1214 const struct got_error *err = NULL, *err_child = NULL;
1215 pid_t pid;
1217 if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_fds) == -1)
1218 return got_error_from_errno();
1220 pid = fork();
1221 if (pid == -1)
1222 return got_error_from_errno();
1223 else if (pid == 0) {
1224 read_blob_object_privsep_child(outfd, infd, imsg_fds);
1225 /* not reached */
1228 close(imsg_fds[1]);
1229 imsg_init(&parent_ibuf, imsg_fds[0]);
1230 err = got_privsep_recv_blob(size, &parent_ibuf);
1231 imsg_clear(&parent_ibuf);
1232 err_child = wait_for_child(pid);
1233 close(imsg_fds[0]);
1234 if (lseek(outfd, SEEK_SET, 0) == -1)
1235 err = got_error_from_errno();
1236 return err ? err : err_child;
1239 const struct got_error *
1240 got_object_blob_open(struct got_blob_object **blob,
1241 struct got_repository *repo, struct got_object *obj, size_t blocksize)
1243 const struct got_error *err = NULL;
1245 if (obj->type != GOT_OBJ_TYPE_BLOB)
1246 return got_error(GOT_ERR_OBJ_TYPE);
1248 if (blocksize < obj->hdrlen)
1249 return got_error(GOT_ERR_NO_SPACE);
1251 *blob = calloc(1, sizeof(**blob));
1252 if (*blob == NULL)
1253 return got_error_from_errno();
1255 (*blob)->read_buf = calloc(1, blocksize);
1256 if ((*blob)->read_buf == NULL) {
1257 err = got_error_from_errno();
1258 goto done;
1260 if (obj->flags & GOT_OBJ_FLAG_PACKED) {
1261 err = got_packfile_extract_object(&((*blob)->f), obj, repo);
1262 if (err)
1263 goto done;
1264 } else {
1265 int infd, outfd;
1266 size_t size;
1267 struct stat sb;
1269 err = open_loose_object(&infd, obj, repo);
1270 if (err)
1271 goto done;
1274 outfd = got_opentempfd();
1275 if (outfd == -1) {
1276 err = got_error_from_errno();
1277 close(infd);
1278 goto done;
1281 err = read_blob_object_privsep(&size, outfd, infd);
1282 close(infd);
1283 if (err)
1284 goto done;
1286 if (size != obj->hdrlen + obj->size) {
1287 err = got_error(GOT_ERR_PRIVSEP_LEN);
1288 close(outfd);
1289 goto done;
1292 if (fstat(outfd, &sb) == -1) {
1293 err = got_error_from_errno();
1294 close(outfd);
1295 goto done;
1298 if (sb.st_size != size) {
1299 err = got_error(GOT_ERR_PRIVSEP_LEN);
1300 close(outfd);
1301 goto done;
1304 (*blob)->f = fdopen(outfd, "rb");
1305 if ((*blob)->f == NULL) {
1306 err = got_error_from_errno();
1307 close(outfd);
1308 goto done;
1312 (*blob)->hdrlen = obj->hdrlen;
1313 (*blob)->blocksize = blocksize;
1314 memcpy(&(*blob)->id.sha1, obj->id.sha1, SHA1_DIGEST_LENGTH);
1316 done:
1317 if (err && *blob) {
1318 if ((*blob)->f)
1319 fclose((*blob)->f);
1320 free((*blob)->read_buf);
1321 free(*blob);
1322 *blob = NULL;
1324 return err;
1327 const struct got_error *
1328 got_object_open_as_blob(struct got_blob_object **blob,
1329 struct got_repository *repo, struct got_object_id *id,
1330 size_t blocksize)
1332 const struct got_error *err;
1333 struct got_object *obj;
1335 *blob = NULL;
1337 err = got_object_open(&obj, repo, id);
1338 if (err)
1339 return err;
1340 if (got_object_get_type(obj) != GOT_OBJ_TYPE_BLOB) {
1341 err = got_error(GOT_ERR_OBJ_TYPE);
1342 goto done;
1345 err = got_object_blob_open(blob, repo, obj, blocksize);
1346 done:
1347 got_object_close(obj);
1348 return err;
1351 void
1352 got_object_blob_close(struct got_blob_object *blob)
1354 free(blob->read_buf);
1355 fclose(blob->f);
1356 free(blob);
1359 char *
1360 got_object_blob_id_str(struct got_blob_object *blob, char *buf, size_t size)
1362 return got_sha1_digest_to_str(blob->id.sha1, buf, size);
1365 size_t
1366 got_object_blob_get_hdrlen(struct got_blob_object *blob)
1368 return blob->hdrlen;
1371 const uint8_t *
1372 got_object_blob_get_read_buf(struct got_blob_object *blob)
1374 return blob->read_buf;
1377 const struct got_error *
1378 got_object_blob_read_block(size_t *outlenp, struct got_blob_object *blob)
1380 size_t n;
1382 n = fread(blob->read_buf, 1, blob->blocksize, blob->f);
1383 if (n == 0 && ferror(blob->f))
1384 return got_ferror(blob->f, GOT_ERR_IO);
1385 *outlenp = n;
1386 return NULL;
1389 const struct got_error *
1390 got_object_blob_dump_to_file(size_t *total_len, FILE *outfile,
1391 struct got_blob_object *blob)
1393 const struct got_error *err = NULL;
1394 size_t len, hdrlen;
1396 *total_len = 0;
1397 hdrlen = got_object_blob_get_hdrlen(blob);
1398 do {
1399 err = got_object_blob_read_block(&len, blob);
1400 if (err)
1401 return err;
1402 if (len == 0)
1403 break;
1404 *total_len += len;
1405 /* Skip blob object header first time around. */
1406 fwrite(got_object_blob_get_read_buf(blob) + hdrlen,
1407 len - hdrlen, 1, outfile);
1408 hdrlen = 0;
1409 } while (len != 0);
1411 fflush(outfile);
1412 rewind(outfile);
1414 return NULL;
1417 static struct got_tree_entry *
1418 find_entry_by_name(struct got_tree_object *tree, const char *name)
1420 struct got_tree_entry *te;
1422 SIMPLEQ_FOREACH(te, &tree->entries, entry) {
1423 if (strcmp(te->name, name) == 0)
1424 return te;
1426 return NULL;
1429 const struct got_error *
1430 got_object_open_by_path(struct got_object **obj, struct got_repository *repo,
1431 struct got_object_id *commit_id, const char *path)
1433 const struct got_error *err = NULL;
1434 struct got_commit_object *commit = NULL;
1435 struct got_tree_object *tree = NULL;
1436 struct got_tree_entry *te = NULL;
1437 char *seg, *s, *s0 = NULL;
1438 size_t len = strlen(path);
1440 *obj = NULL;
1442 /* We are expecting an absolute in-repository path. */
1443 if (path[0] != '/')
1444 return got_error(GOT_ERR_NOT_ABSPATH);
1446 err = got_object_open_as_commit(&commit, repo, commit_id);
1447 if (err)
1448 goto done;
1450 /* Handle opening of root of commit's tree. */
1451 if (path[1] == '\0') {
1452 err = got_object_open(obj, repo, commit->tree_id);
1453 if (err)
1454 goto done;
1455 return NULL;
1458 err = got_object_open_as_tree(&tree, repo, commit->tree_id);
1459 if (err)
1460 goto done;
1462 s0 = strdup(path);
1463 if (s0 == NULL) {
1464 err = got_error_from_errno();
1465 goto done;
1467 err = got_canonpath(path, s0, len + 1);
1468 if (err)
1469 goto done;
1471 s = s0;
1472 s++; /* skip leading '/' */
1473 len--;
1474 seg = s;
1475 while (len > 0) {
1476 struct got_tree_object *next_tree;
1478 if (*s != '/') {
1479 s++;
1480 len--;
1481 if (*s)
1482 continue;
1485 /* end of path segment */
1486 *s = '\0';
1488 te = find_entry_by_name(tree, seg);
1489 if (te == NULL) {
1490 err = got_error(GOT_ERR_NO_OBJ);
1491 goto done;
1494 if (len == 0)
1495 break;
1497 seg = s + 1;
1498 s++;
1499 len--;
1500 if (*s) {
1501 err = got_object_open_as_tree(&next_tree, repo,
1502 te->id);
1503 te = NULL;
1504 if (err)
1505 goto done;
1506 got_object_tree_close(tree);
1507 tree = next_tree;
1511 if (te)
1512 err = got_object_open(obj, repo, te->id);
1513 else
1514 err = got_error(GOT_ERR_NO_OBJ);
1515 done:
1516 free(s0);
1517 if (commit)
1518 got_object_commit_close(commit);
1519 if (tree)
1520 got_object_tree_close(tree);
1521 return err;