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 err = got_object_open(&obj, repo, id);
452 if (err)
453 return err;
454 if (got_object_get_type(obj) != GOT_OBJ_TYPE_COMMIT) {
455 err = got_error(GOT_ERR_OBJ_TYPE);
456 goto done;
459 err = got_object_commit_open(commit, repo, obj);
460 done:
461 got_object_close(obj);
462 return err;
465 const struct got_error *
466 got_object_commit_add_parent(struct got_commit_object *commit,
467 const char *id_str)
469 const struct got_error *err = NULL;
470 struct got_object_qid *qid;
472 qid = calloc(1, sizeof(*qid));
473 if (qid == NULL)
474 return got_error_from_errno();
476 qid->id = calloc(1, sizeof(*qid->id));
477 if (qid->id == NULL) {
478 err = got_error_from_errno();
479 free(qid);
480 return err;
483 if (!got_parse_sha1_digest(qid->id->sha1, id_str)) {
484 err = got_error(GOT_ERR_BAD_OBJ_DATA);
485 free(qid->id);
486 free(qid);
487 return err;
490 SIMPLEQ_INSERT_TAIL(&commit->parent_ids, qid, entry);
491 commit->nparents++;
493 return NULL;
496 static const struct got_error *
497 parse_gmtoff(time_t *gmtoff, const char *tzstr)
499 int sign = 1;
500 const char *p = tzstr;
501 time_t h, m;
503 *gmtoff = 0;
505 if (*p == '-')
506 sign = -1;
507 else if (*p != '+')
508 return got_error(GOT_ERR_BAD_OBJ_DATA);
509 p++;
510 if (!isdigit(*p) && !isdigit(*(p + 1)))
511 return got_error(GOT_ERR_BAD_OBJ_DATA);
512 h = (((*p - '0') * 10) + (*(p + 1) - '0'));
514 p += 2;
515 if (!isdigit(*p) && !isdigit(*(p + 1)))
516 return got_error(GOT_ERR_BAD_OBJ_DATA);
517 m = ((*p - '0') * 10) + (*(p + 1) - '0');
519 *gmtoff = (h * 60 * 60 + m * 60) * sign;
520 return NULL;
523 static const struct got_error *
524 parse_commit_time(struct tm *tm, char *committer)
526 const struct got_error *err = NULL;
527 const char *errstr;
528 char *space, *tzstr;
529 time_t gmtoff;
530 time_t time;
532 /* Parse and strip off trailing timezone indicator string. */
533 space = strrchr(committer, ' ');
534 if (space == NULL)
535 return got_error(GOT_ERR_BAD_OBJ_DATA);
536 tzstr = strdup(space + 1);
537 if (tzstr == NULL)
538 return got_error_from_errno();
539 err = parse_gmtoff(&gmtoff, tzstr);
540 free(tzstr);
541 if (err)
542 return err;
543 *space = '\0';
545 /* Timestamp is separated from committer name + email by space. */
546 space = strrchr(committer, ' ');
547 if (space == NULL)
548 return got_error(GOT_ERR_BAD_OBJ_DATA);
550 /* Timestamp parsed here is expressed in comitter's local time. */
551 time = strtonum(space + 1, 0, INT64_MAX, &errstr);
552 if (errstr)
553 return got_error(GOT_ERR_BAD_OBJ_DATA);
555 /* Express the time stamp in UTC. */
556 memset(tm, 0, sizeof(*tm));
557 time -= gmtoff;
558 if (localtime_r(&time, tm) == NULL)
559 return got_error_from_errno();
560 tm->tm_gmtoff = gmtoff;
562 /* Strip off parsed time information, leaving just author and email. */
563 *space = '\0';
565 return NULL;
568 static const struct got_error *
569 parse_commit_object(struct got_commit_object **commit, char *buf, size_t len)
571 const struct got_error *err = NULL;
572 char *s = buf;
573 size_t tlen;
574 ssize_t remain = (ssize_t)len;
576 *commit = got_object_commit_alloc_partial();
577 if (*commit == NULL)
578 return got_error_from_errno();
580 tlen = strlen(GOT_COMMIT_TAG_TREE);
581 if (strncmp(s, GOT_COMMIT_TAG_TREE, tlen) == 0) {
582 remain -= tlen;
583 if (remain < SHA1_DIGEST_STRING_LENGTH) {
584 err = got_error(GOT_ERR_BAD_OBJ_DATA);
585 goto done;
587 s += tlen;
588 if (!got_parse_sha1_digest((*commit)->tree_id->sha1, s)) {
589 err = got_error(GOT_ERR_BAD_OBJ_DATA);
590 goto done;
592 remain -= SHA1_DIGEST_STRING_LENGTH;
593 s += SHA1_DIGEST_STRING_LENGTH;
594 } else {
595 err = got_error(GOT_ERR_BAD_OBJ_DATA);
596 goto done;
599 tlen = strlen(GOT_COMMIT_TAG_PARENT);
600 while (strncmp(s, GOT_COMMIT_TAG_PARENT, tlen) == 0) {
601 remain -= tlen;
602 if (remain < SHA1_DIGEST_STRING_LENGTH) {
603 err = got_error(GOT_ERR_BAD_OBJ_DATA);
604 goto done;
606 s += tlen;
607 err = got_object_commit_add_parent(*commit, s);
608 if (err)
609 goto done;
611 remain -= SHA1_DIGEST_STRING_LENGTH;
612 s += SHA1_DIGEST_STRING_LENGTH;
615 tlen = strlen(GOT_COMMIT_TAG_AUTHOR);
616 if (strncmp(s, GOT_COMMIT_TAG_AUTHOR, tlen) == 0) {
617 char *p;
618 size_t slen;
620 remain -= tlen;
621 if (remain <= 0) {
622 err = got_error(GOT_ERR_BAD_OBJ_DATA);
623 goto done;
625 s += tlen;
626 p = strchr(s, '\n');
627 if (p == NULL) {
628 err = got_error(GOT_ERR_BAD_OBJ_DATA);
629 goto done;
631 *p = '\0';
632 slen = strlen(s);
633 err = parse_commit_time(&(*commit)->tm_author, s);
634 if (err)
635 goto done;
636 (*commit)->author = strdup(s);
637 if ((*commit)->author == NULL) {
638 err = got_error_from_errno();
639 goto done;
641 s += slen + 1;
642 remain -= slen + 1;
645 tlen = strlen(GOT_COMMIT_TAG_COMMITTER);
646 if (strncmp(s, GOT_COMMIT_TAG_COMMITTER, tlen) == 0) {
647 char *p;
648 size_t slen;
650 remain -= tlen;
651 if (remain <= 0) {
652 err = got_error(GOT_ERR_BAD_OBJ_DATA);
653 goto done;
655 s += tlen;
656 p = strchr(s, '\n');
657 if (p == NULL) {
658 err = got_error(GOT_ERR_BAD_OBJ_DATA);
659 goto done;
661 *p = '\0';
662 slen = strlen(s);
663 err = parse_commit_time(&(*commit)->tm_committer, s);
664 if (err)
665 goto done;
666 (*commit)->committer = strdup(s);
667 if ((*commit)->committer == NULL) {
668 err = got_error_from_errno();
669 goto done;
671 s += slen + 1;
672 remain -= slen + 1;
675 (*commit)->logmsg = strndup(s, remain);
676 if ((*commit)->logmsg == NULL) {
677 err = got_error_from_errno();
678 goto done;
680 done:
681 if (err) {
682 got_object_commit_close(*commit);
683 *commit = NULL;
685 return err;
688 static void
689 tree_entry_close(struct got_tree_entry *te)
691 free(te->id);
692 free(te->name);
693 free(te);
696 struct got_tree_entry *
697 got_alloc_tree_entry_partial(void)
699 struct got_tree_entry *te;
701 te = calloc(1, sizeof(*te));
702 if (te == NULL)
703 return NULL;
705 te->id = calloc(1, sizeof(*te->id));
706 if (te->id == NULL) {
707 free(te);
708 te = NULL;
710 return te;
713 static const struct got_error *
714 parse_tree_entry(struct got_tree_entry **te, size_t *elen, char *buf,
715 size_t maxlen)
717 char *p = buf, *space;
718 const struct got_error *err = NULL;
720 *te = got_alloc_tree_entry_partial();
721 if (*te == NULL)
722 return got_error_from_errno();
724 *elen = strlen(buf) + 1;
725 if (*elen > maxlen) {
726 free(*te);
727 *te = NULL;
728 return got_error(GOT_ERR_BAD_OBJ_DATA);
731 space = strchr(buf, ' ');
732 if (space == NULL) {
733 err = got_error(GOT_ERR_BAD_OBJ_DATA);
734 free(*te);
735 *te = NULL;
736 return err;
738 while (*p != ' ') {
739 if (*p < '0' && *p > '7') {
740 err = got_error(GOT_ERR_BAD_OBJ_DATA);
741 goto done;
743 (*te)->mode <<= 3;
744 (*te)->mode |= *p - '0';
745 p++;
748 (*te)->name = strdup(space + 1);
749 if (*elen > maxlen || maxlen - *elen < SHA1_DIGEST_LENGTH) {
750 err = got_error(GOT_ERR_BAD_OBJ_DATA);
751 goto done;
753 buf += strlen(buf) + 1;
754 memcpy((*te)->id->sha1, buf, SHA1_DIGEST_LENGTH);
755 *elen += SHA1_DIGEST_LENGTH;
756 done:
757 if (err) {
758 tree_entry_close(*te);
759 *te = NULL;
761 return err;
764 static const struct got_error *
765 parse_tree_object(struct got_tree_object **tree, uint8_t *buf, size_t len)
767 const struct got_error *err;
768 size_t remain = len;
770 *tree = calloc(1, sizeof(**tree));
771 if (*tree == NULL)
772 return got_error_from_errno();
774 SIMPLEQ_INIT(&(*tree)->entries);
776 while (remain > 0) {
777 struct got_tree_entry *te;
778 size_t elen;
780 err = parse_tree_entry(&te, &elen, buf, remain);
781 if (err)
782 return err;
783 (*tree)->nentries++;
784 SIMPLEQ_INSERT_TAIL(&(*tree)->entries, te, entry);
785 buf += elen;
786 remain -= elen;
789 if (remain != 0) {
790 got_object_tree_close(*tree);
791 return got_error(GOT_ERR_BAD_OBJ_DATA);
794 return NULL;
797 static const struct got_error *
798 read_to_mem(uint8_t **outbuf, size_t *outlen, FILE *f)
800 const struct got_error *err = NULL;
801 static const size_t blocksize = 512;
802 size_t n, total, remain;
803 uint8_t *buf;
805 *outbuf = NULL;
806 *outlen = 0;
808 buf = calloc(1, blocksize);
809 if (buf == NULL)
810 return got_error_from_errno();
812 remain = blocksize;
813 total = 0;
814 while (1) {
815 if (remain == 0) {
816 uint8_t *newbuf;
817 newbuf = reallocarray(buf, 1, total + blocksize);
818 if (newbuf == NULL) {
819 err = got_error_from_errno();
820 goto done;
822 buf = newbuf;
823 remain += blocksize;
825 n = fread(buf + total, 1, remain, f);
826 if (n == 0) {
827 if (ferror(f)) {
828 err = got_ferror(f, GOT_ERR_IO);
829 goto done;
831 break; /* EOF */
833 remain -= n;
834 total += n;
835 };
837 done:
838 if (err == NULL) {
839 *outbuf = buf;
840 *outlen = total;
841 } else
842 free(buf);
843 return err;
846 static const struct got_error *
847 read_commit_object(struct got_commit_object **commit, struct got_object *obj,
848 FILE *f)
850 const struct got_error *err = NULL;
851 size_t len;
852 uint8_t *p;
854 if (obj->flags & GOT_OBJ_FLAG_PACKED)
855 err = read_to_mem(&p, &len, f);
856 else
857 err = got_inflate_to_mem(&p, &len, f);
858 if (err)
859 return err;
861 if (len < obj->hdrlen + obj->size) {
862 err = got_error(GOT_ERR_BAD_OBJ_DATA);
863 goto done;
866 /* Skip object header. */
867 len -= obj->hdrlen;
868 err = parse_commit_object(commit, p + obj->hdrlen, len);
869 free(p);
870 done:
871 return err;
874 static void
875 read_commit_object_privsep_child(struct got_object *obj, int obj_fd,
876 int imsg_fds[2])
878 const struct got_error *err = NULL;
879 struct got_commit_object *commit = NULL;
880 struct imsgbuf ibuf;
881 FILE *f = NULL;
882 int status = 0;
884 setproctitle("read commit object");
885 close(imsg_fds[0]);
886 imsg_init(&ibuf, imsg_fds[1]);
888 /* revoke access to most system calls */
889 if (pledge("stdio", NULL) == -1) {
890 err = got_error_from_errno();
891 goto done;
894 f = fdopen(obj_fd, "rb");
895 if (f == NULL) {
896 err = got_error_from_errno();
897 close(obj_fd);
898 goto done;
901 err = read_commit_object(&commit, obj, f);
902 if (err)
903 goto done;
905 err = got_privsep_send_commit(&ibuf, commit);
906 done:
907 if (commit)
908 got_object_commit_close(commit);
909 if (err) {
910 got_privsep_send_error(&ibuf, err);
911 status = 1;
913 if (f)
914 fclose(f);
915 imsg_clear(&ibuf);
916 close(imsg_fds[1]);
917 _exit(status);
920 static const struct got_error *
921 read_commit_object_privsep(struct got_commit_object **commit,
922 struct got_repository *repo, struct got_object *obj, int fd)
924 const struct got_error *err = NULL, *err_child = NULL;
925 struct imsgbuf parent_ibuf;
926 int imsg_fds[2];
927 pid_t pid;
929 if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_fds) == -1)
930 return got_error_from_errno();
932 pid = fork();
933 if (pid == -1)
934 return got_error_from_errno();
935 else if (pid == 0) {
936 read_commit_object_privsep_child(obj, fd, imsg_fds);
937 /* not reached */
940 close(imsg_fds[1]);
941 imsg_init(&parent_ibuf, imsg_fds[0]);
942 err = got_privsep_recv_commit(commit, &parent_ibuf);
943 imsg_clear(&parent_ibuf);
944 err_child = wait_for_child(pid);
945 close(imsg_fds[0]);
946 return err ? err : err_child;
949 const struct got_error *
950 got_object_commit_open(struct got_commit_object **commit,
951 struct got_repository *repo, struct got_object *obj)
953 const struct got_error *err = NULL;
955 if (obj->type != GOT_OBJ_TYPE_COMMIT)
956 return got_error(GOT_ERR_OBJ_TYPE);
958 if (obj->flags & GOT_OBJ_FLAG_PACKED) {
959 uint8_t *buf;
960 size_t len;
961 err = got_packfile_extract_object_to_mem(&buf, &len, obj, repo);
962 if (err)
963 return err;
964 obj->size = len;
965 err = parse_commit_object(commit, buf, len);
966 free(buf);
967 } else {
968 int fd;
969 err = open_loose_object(&fd, obj, repo);
970 if (err)
971 return err;
972 err = read_commit_object_privsep(commit, repo, obj, fd);
973 close(fd);
975 return err;
978 void
979 got_object_commit_close(struct got_commit_object *commit)
981 struct got_object_qid *qid;
983 while (!SIMPLEQ_EMPTY(&commit->parent_ids)) {
984 qid = SIMPLEQ_FIRST(&commit->parent_ids);
985 SIMPLEQ_REMOVE_HEAD(&commit->parent_ids, entry);
986 free(qid->id);
987 free(qid);
990 free(commit->tree_id);
991 free(commit->author);
992 free(commit->committer);
993 free(commit->logmsg);
994 free(commit);
997 static const struct got_error *
998 read_tree_object(struct got_tree_object **tree, struct got_object *obj, FILE *f)
1000 const struct got_error *err = NULL;
1001 size_t len;
1002 uint8_t *p;
1004 if (obj->flags & GOT_OBJ_FLAG_PACKED)
1005 err = read_to_mem(&p, &len, f);
1006 else
1007 err = got_inflate_to_mem(&p, &len, f);
1008 if (err)
1009 return err;
1011 if (len < obj->hdrlen + obj->size) {
1012 err = got_error(GOT_ERR_BAD_OBJ_DATA);
1013 goto done;
1016 /* Skip object header. */
1017 len -= obj->hdrlen;
1018 err = parse_tree_object(tree, p + obj->hdrlen, len);
1019 free(p);
1020 done:
1021 return err;
1024 static void
1025 read_tree_object_privsep_child(struct got_object *obj, int obj_fd,
1026 int imsg_fds[2])
1028 const struct got_error *err = NULL;
1029 struct got_tree_object *tree = NULL;
1030 struct imsgbuf ibuf;
1031 FILE *f = NULL;
1032 int status = 0;
1034 setproctitle("read tree object");
1035 close(imsg_fds[0]);
1036 imsg_init(&ibuf, imsg_fds[1]);
1038 /* revoke access to most system calls */
1039 if (pledge("stdio", NULL) == -1) {
1040 err = got_error_from_errno();
1041 goto done;
1044 f = fdopen(obj_fd, "rb");
1045 if (f == NULL) {
1046 err = got_error_from_errno();
1047 close(obj_fd);
1048 goto done;
1051 err = read_tree_object(&tree, obj, f);
1052 if (err)
1053 goto done;
1055 err = got_privsep_send_tree(&ibuf, tree);
1056 done:
1057 if (tree)
1058 got_object_tree_close(tree);
1059 if (err) {
1060 got_privsep_send_error(&ibuf, err);
1061 status = 1;
1063 if (f)
1064 fclose(f);
1065 imsg_clear(&ibuf);
1066 close(imsg_fds[1]);
1067 _exit(status);
1070 static const struct got_error *
1071 read_tree_object_privsep(struct got_tree_object **tree, struct got_object *obj,
1072 int fd)
1074 const struct got_error *err = NULL, *err_child = NULL;
1075 struct imsgbuf parent_ibuf;
1076 int imsg_fds[2];
1077 pid_t pid;
1079 if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_fds) == -1)
1080 return got_error_from_errno();
1082 pid = fork();
1083 if (pid == -1)
1084 return got_error_from_errno();
1085 else if (pid == 0) {
1086 read_tree_object_privsep_child(obj, fd, imsg_fds);
1087 /* not reached */
1090 close(imsg_fds[1]);
1091 imsg_init(&parent_ibuf, imsg_fds[0]);
1092 err = got_privsep_recv_tree(tree, &parent_ibuf);
1093 imsg_clear(&parent_ibuf);
1094 err_child = wait_for_child(pid);
1095 close(imsg_fds[0]);
1096 return err ? err : err_child;
1099 const struct got_error *
1100 got_object_tree_open(struct got_tree_object **tree,
1101 struct got_repository *repo, struct got_object *obj)
1103 const struct got_error *err = NULL;
1105 if (obj->type != GOT_OBJ_TYPE_TREE)
1106 return got_error(GOT_ERR_OBJ_TYPE);
1108 if (obj->flags & GOT_OBJ_FLAG_PACKED) {
1109 uint8_t *buf;
1110 size_t len;
1111 err = got_packfile_extract_object_to_mem(&buf, &len, obj, repo);
1112 if (err)
1113 return err;
1114 obj->size = len;
1115 err = parse_tree_object(tree, buf, len);
1116 free(buf);
1117 } else {
1118 int fd;
1119 err = open_loose_object(&fd, obj, repo);
1120 if (err)
1121 return err;
1122 err = read_tree_object_privsep(tree, obj, fd);
1123 close(fd);
1125 return err;
1128 const struct got_error *
1129 got_object_open_as_tree(struct got_tree_object **tree,
1130 struct got_repository *repo, struct got_object_id *id)
1132 const struct got_error *err;
1133 struct got_object *obj;
1135 err = got_object_open(&obj, repo, id);
1136 if (err)
1137 return err;
1138 if (got_object_get_type(obj) != GOT_OBJ_TYPE_TREE) {
1139 err = got_error(GOT_ERR_OBJ_TYPE);
1140 goto done;
1143 err = got_object_tree_open(tree, repo, obj);
1144 done:
1145 got_object_close(obj);
1146 return err;
1149 void
1150 got_object_tree_close(struct got_tree_object *tree)
1152 struct got_tree_entry *te;
1154 while (!SIMPLEQ_EMPTY(&tree->entries)) {
1155 te = SIMPLEQ_FIRST(&tree->entries);
1156 SIMPLEQ_REMOVE_HEAD(&tree->entries, entry);
1157 tree_entry_close(te);
1160 free(tree);
1163 static const struct got_error *
1164 read_blob_object_privsep_child(int outfd, int infd, int imsg_fds[2])
1166 const struct got_error *err = NULL;
1167 struct imsgbuf ibuf;
1168 int status = 0;
1169 size_t size;
1170 FILE *infile = NULL;
1172 setproctitle("read blob object");
1173 close(imsg_fds[0]);
1174 imsg_init(&ibuf, imsg_fds[1]);
1176 /* revoke access to most system calls */
1177 if (pledge("stdio", NULL) == -1) {
1178 err = got_error_from_errno();
1179 goto done;
1182 infile = fdopen(infd, "rb");
1183 if (infile == NULL) {
1184 err = got_error_from_errno();
1185 close(infd);
1186 goto done;
1188 err = got_inflate_to_fd(&size, infile, outfd);
1189 fclose(infile);
1190 if (err)
1191 goto done;
1193 err = got_privsep_send_blob(&ibuf, size);
1194 done:
1195 if (err) {
1196 got_privsep_send_error(&ibuf, err);
1197 status = 1;
1199 close(outfd);
1200 imsg_clear(&ibuf);
1201 close(imsg_fds[1]);
1202 _exit(status);
1205 static const struct got_error *
1206 read_blob_object_privsep(size_t *size, int outfd, int infd)
1208 struct imsgbuf parent_ibuf;
1209 int imsg_fds[2];
1210 const struct got_error *err = NULL, *err_child = NULL;
1211 pid_t pid;
1213 if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_fds) == -1)
1214 return got_error_from_errno();
1216 pid = fork();
1217 if (pid == -1)
1218 return got_error_from_errno();
1219 else if (pid == 0) {
1220 read_blob_object_privsep_child(outfd, infd, imsg_fds);
1221 /* not reached */
1224 close(imsg_fds[1]);
1225 imsg_init(&parent_ibuf, imsg_fds[0]);
1226 err = got_privsep_recv_blob(size, &parent_ibuf);
1227 imsg_clear(&parent_ibuf);
1228 err_child = wait_for_child(pid);
1229 close(imsg_fds[0]);
1230 if (lseek(outfd, SEEK_SET, 0) == -1)
1231 err = got_error_from_errno();
1232 return err ? err : err_child;
1235 const struct got_error *
1236 got_object_blob_open(struct got_blob_object **blob,
1237 struct got_repository *repo, struct got_object *obj, size_t blocksize)
1239 const struct got_error *err = NULL;
1241 if (obj->type != GOT_OBJ_TYPE_BLOB)
1242 return got_error(GOT_ERR_OBJ_TYPE);
1244 if (blocksize < obj->hdrlen)
1245 return got_error(GOT_ERR_NO_SPACE);
1247 *blob = calloc(1, sizeof(**blob));
1248 if (*blob == NULL)
1249 return got_error_from_errno();
1251 (*blob)->read_buf = calloc(1, blocksize);
1252 if ((*blob)->read_buf == NULL) {
1253 err = got_error_from_errno();
1254 goto done;
1256 if (obj->flags & GOT_OBJ_FLAG_PACKED) {
1257 err = got_packfile_extract_object(&((*blob)->f), obj, repo);
1258 if (err)
1259 goto done;
1260 } else {
1261 int infd, outfd;
1262 size_t size;
1263 struct stat sb;
1265 err = open_loose_object(&infd, obj, repo);
1266 if (err)
1267 goto done;
1270 outfd = got_opentempfd();
1271 if (outfd == -1) {
1272 err = got_error_from_errno();
1273 close(infd);
1274 goto done;
1277 err = read_blob_object_privsep(&size, outfd, infd);
1278 close(infd);
1279 if (err)
1280 goto done;
1282 if (size != obj->hdrlen + obj->size) {
1283 err = got_error(GOT_ERR_PRIVSEP_LEN);
1284 close(outfd);
1285 goto done;
1288 if (fstat(outfd, &sb) == -1) {
1289 err = got_error_from_errno();
1290 close(outfd);
1291 goto done;
1294 if (sb.st_size != size) {
1295 err = got_error(GOT_ERR_PRIVSEP_LEN);
1296 close(outfd);
1297 goto done;
1300 (*blob)->f = fdopen(outfd, "rb");
1301 if ((*blob)->f == NULL) {
1302 err = got_error_from_errno();
1303 close(outfd);
1304 goto done;
1308 (*blob)->hdrlen = obj->hdrlen;
1309 (*blob)->blocksize = blocksize;
1310 memcpy(&(*blob)->id.sha1, obj->id.sha1, SHA1_DIGEST_LENGTH);
1312 done:
1313 if (err && *blob) {
1314 if ((*blob)->f)
1315 fclose((*blob)->f);
1316 free((*blob)->read_buf);
1317 free(*blob);
1318 *blob = NULL;
1320 return err;
1323 const struct got_error *
1324 got_object_open_as_blob(struct got_blob_object **blob,
1325 struct got_repository *repo, struct got_object_id *id,
1326 size_t blocksize)
1328 const struct got_error *err;
1329 struct got_object *obj;
1331 err = got_object_open(&obj, repo, id);
1332 if (err)
1333 return err;
1334 if (got_object_get_type(obj) != GOT_OBJ_TYPE_BLOB) {
1335 err = got_error(GOT_ERR_OBJ_TYPE);
1336 goto done;
1339 err = got_object_blob_open(blob, repo, obj, blocksize);
1340 done:
1341 got_object_close(obj);
1342 return err;
1345 void
1346 got_object_blob_close(struct got_blob_object *blob)
1348 free(blob->read_buf);
1349 fclose(blob->f);
1350 free(blob);
1353 char *
1354 got_object_blob_id_str(struct got_blob_object *blob, char *buf, size_t size)
1356 return got_sha1_digest_to_str(blob->id.sha1, buf, size);
1359 size_t
1360 got_object_blob_get_hdrlen(struct got_blob_object *blob)
1362 return blob->hdrlen;
1365 const uint8_t *
1366 got_object_blob_get_read_buf(struct got_blob_object *blob)
1368 return blob->read_buf;
1371 const struct got_error *
1372 got_object_blob_read_block(size_t *outlenp, struct got_blob_object *blob)
1374 size_t n;
1376 n = fread(blob->read_buf, 1, blob->blocksize, blob->f);
1377 if (n == 0 && ferror(blob->f))
1378 return got_ferror(blob->f, GOT_ERR_IO);
1379 *outlenp = n;
1380 return NULL;
1383 const struct got_error *
1384 got_object_blob_dump_to_file(size_t *total_len, FILE *outfile,
1385 struct got_blob_object *blob)
1387 const struct got_error *err = NULL;
1388 size_t len, hdrlen;
1390 *total_len = 0;
1391 hdrlen = got_object_blob_get_hdrlen(blob);
1392 do {
1393 err = got_object_blob_read_block(&len, blob);
1394 if (err)
1395 return err;
1396 if (len == 0)
1397 break;
1398 *total_len += len;
1399 /* Skip blob object header first time around. */
1400 fwrite(got_object_blob_get_read_buf(blob) + hdrlen,
1401 len - hdrlen, 1, outfile);
1402 hdrlen = 0;
1403 } while (len != 0);
1405 fflush(outfile);
1406 rewind(outfile);
1408 return NULL;
1411 static struct got_tree_entry *
1412 find_entry_by_name(struct got_tree_object *tree, const char *name)
1414 struct got_tree_entry *te;
1416 SIMPLEQ_FOREACH(te, &tree->entries, entry) {
1417 if (strcmp(te->name, name) == 0)
1418 return te;
1420 return NULL;
1423 const struct got_error *
1424 got_object_open_by_path(struct got_object **obj, struct got_repository *repo,
1425 struct got_object_id *commit_id, const char *path)
1427 const struct got_error *err = NULL;
1428 struct got_commit_object *commit = NULL;
1429 struct got_tree_object *tree = NULL;
1430 struct got_tree_entry *te = NULL;
1431 char *seg, *s, *s0 = NULL;
1432 size_t len = strlen(path);
1434 *obj = NULL;
1436 /* We are expecting an absolute in-repository path. */
1437 if (path[0] != '/')
1438 return got_error(GOT_ERR_NOT_ABSPATH);
1440 err = got_object_open_as_commit(&commit, repo, commit_id);
1441 if (err)
1442 goto done;
1444 /* Handle opening of root of commit's tree. */
1445 if (path[1] == '\0') {
1446 err = got_object_open(obj, repo, commit->tree_id);
1447 if (err)
1448 goto done;
1449 return NULL;
1452 err = got_object_open_as_tree(&tree, repo, commit->tree_id);
1453 if (err)
1454 goto done;
1456 s0 = strdup(path);
1457 if (s0 == NULL) {
1458 err = got_error_from_errno();
1459 goto done;
1461 err = got_canonpath(path, s0, len + 1);
1462 if (err)
1463 goto done;
1465 s = s0;
1466 s++; /* skip leading '/' */
1467 len--;
1468 seg = s;
1469 while (len > 0) {
1470 struct got_tree_object *next_tree;
1472 if (*s != '/') {
1473 s++;
1474 len--;
1475 if (*s)
1476 continue;
1479 /* end of path segment */
1480 *s = '\0';
1482 te = find_entry_by_name(tree, seg);
1483 if (te == NULL) {
1484 err = got_error(GOT_ERR_NO_OBJ);
1485 goto done;
1488 if (len == 0)
1489 break;
1491 seg = s + 1;
1492 s++;
1493 len--;
1494 if (*s) {
1495 err = got_object_open_as_tree(&next_tree, repo,
1496 te->id);
1497 te = NULL;
1498 if (err)
1499 goto done;
1500 got_object_tree_close(tree);
1501 tree = next_tree;
1505 if (te)
1506 err = got_object_open(obj, repo, te->id);
1507 else
1508 err = got_error(GOT_ERR_NO_OBJ);
1509 done:
1510 free(s0);
1511 if (commit)
1512 got_object_commit_close(commit);
1513 if (tree)
1514 got_object_tree_close(tree);
1515 return err;