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_inflate.h"
47 #include "got_lib_object.h"
48 #include "got_lib_privsep.h"
49 #include "got_lib_repository.h"
51 #ifndef MIN
52 #define MIN(_a,_b) ((_a) < (_b) ? (_a) : (_b))
53 #endif
55 #ifndef nitems
56 #define nitems(_a) (sizeof(_a) / sizeof((_a)[0]))
57 #endif
59 #define GOT_OBJ_TAG_COMMIT "commit"
60 #define GOT_OBJ_TAG_TREE "tree"
61 #define GOT_OBJ_TAG_BLOB "blob"
63 #define GOT_COMMIT_TAG_TREE "tree "
64 #define GOT_COMMIT_TAG_PARENT "parent "
65 #define GOT_COMMIT_TAG_AUTHOR "author "
66 #define GOT_COMMIT_TAG_COMMITTER "committer "
68 const struct got_error *
69 got_object_id_str(char **outbuf, struct got_object_id *id)
70 {
71 static const size_t len = SHA1_DIGEST_STRING_LENGTH;
73 *outbuf = malloc(len);
74 if (*outbuf == NULL)
75 return got_error_from_errno();
77 if (got_sha1_digest_to_str(id->sha1, *outbuf, len) == NULL) {
78 free(*outbuf);
79 *outbuf = NULL;
80 return got_error(GOT_ERR_BAD_OBJ_ID_STR);
81 }
83 return NULL;
84 }
86 int
87 got_object_id_cmp(struct got_object_id *id1, struct got_object_id *id2)
88 {
89 return memcmp(id1->sha1, id2->sha1, SHA1_DIGEST_LENGTH);
90 }
92 struct got_object_id *
93 got_object_id_dup(struct got_object_id *id1)
94 {
95 struct got_object_id *id2;
97 id2 = malloc(sizeof(*id2));
98 if (id2 == NULL)
99 return NULL;
100 memcpy(id2, id1, sizeof(*id2));
101 return id2;
104 struct got_object_id *
105 got_object_get_id(struct got_object *obj)
107 return got_object_id_dup(&obj->id);
110 const struct got_error *
111 got_object_get_id_str(char **outbuf, struct got_object *obj)
113 return got_object_id_str(outbuf, &obj->id);
116 int
117 got_object_get_type(struct got_object *obj)
119 switch (obj->type) {
120 case GOT_OBJ_TYPE_COMMIT:
121 case GOT_OBJ_TYPE_TREE:
122 case GOT_OBJ_TYPE_BLOB:
123 case GOT_OBJ_TYPE_TAG:
124 return obj->type;
125 default:
126 abort();
127 break;
130 /* not reached */
131 return 0;
134 static const struct got_error *
135 parse_object_header(struct got_object **obj, char *buf, size_t len)
137 const char *obj_tags[] = {
138 GOT_OBJ_TAG_COMMIT,
139 GOT_OBJ_TAG_TREE,
140 GOT_OBJ_TAG_BLOB
141 };
142 const int obj_types[] = {
143 GOT_OBJ_TYPE_COMMIT,
144 GOT_OBJ_TYPE_TREE,
145 GOT_OBJ_TYPE_BLOB,
146 };
147 int type = 0;
148 size_t size = 0, hdrlen = 0;
149 int i;
150 char *p = strchr(buf, '\0');
152 if (p == NULL)
153 return got_error(GOT_ERR_BAD_OBJ_HDR);
155 hdrlen = strlen(buf) + 1 /* '\0' */;
157 for (i = 0; i < nitems(obj_tags); i++) {
158 const char *tag = obj_tags[i];
159 size_t tlen = strlen(tag);
160 const char *errstr;
162 if (strncmp(buf, tag, tlen) != 0)
163 continue;
165 type = obj_types[i];
166 if (len <= tlen)
167 return got_error(GOT_ERR_BAD_OBJ_HDR);
168 size = strtonum(buf + tlen, 0, LONG_MAX, &errstr);
169 if (errstr != NULL)
170 return got_error(GOT_ERR_BAD_OBJ_HDR);
171 break;
174 if (type == 0)
175 return got_error(GOT_ERR_BAD_OBJ_HDR);
177 *obj = calloc(1, sizeof(**obj));
178 if (*obj == NULL)
179 return got_error_from_errno();
180 (*obj)->type = type;
181 (*obj)->hdrlen = hdrlen;
182 (*obj)->size = size;
183 return NULL;
186 static const struct got_error *
187 read_object_header(struct got_object **obj, int fd)
189 const struct got_error *err;
190 struct got_zstream_buf zb;
191 char *buf;
192 const size_t zbsize = 64;
193 size_t outlen, totlen;
194 int nbuf = 1;
196 buf = malloc(zbsize);
197 if (buf == NULL)
198 return got_error_from_errno();
200 err = got_inflate_init(&zb, buf, zbsize);
201 if (err)
202 return err;
204 totlen = 0;
205 do {
206 err = got_inflate_read_fd(&zb, fd, &outlen);
207 if (err)
208 goto done;
209 if (outlen == 0)
210 break;
211 totlen += outlen;
212 if (strchr(zb.outbuf, '\0') == NULL) {
213 char *newbuf;
214 nbuf++;
215 newbuf = recallocarray(buf, nbuf - 1, nbuf, zbsize);
216 if (newbuf == NULL) {
217 err = got_error_from_errno();
218 goto done;
220 buf = newbuf;
221 zb.outbuf = newbuf + totlen;
222 zb.outlen = (nbuf * zbsize) - totlen;
224 } while (strchr(zb.outbuf, '\0') == NULL);
226 err = parse_object_header(obj, buf, totlen);
227 done:
228 free(buf);
229 got_inflate_end(&zb);
230 return err;
233 static void
234 read_object_header_privsep_child(int obj_fd, int imsg_fds[2])
236 const struct got_error *err = NULL;
237 struct got_object *obj = NULL;
238 struct imsgbuf ibuf;
239 int status = 0;
241 setproctitle("read object header");
242 close(imsg_fds[0]);
243 imsg_init(&ibuf, imsg_fds[1]);
245 /* revoke access to most system calls */
246 if (pledge("stdio", NULL) == -1) {
247 err = got_error_from_errno();
248 goto done;
251 err = read_object_header(&obj, obj_fd);
252 if (err)
253 goto done;
255 err = got_privsep_send_obj(&ibuf, obj, 0);
256 done:
257 if (obj)
258 got_object_close(obj);
259 if (err) {
260 got_privsep_send_error(&ibuf, err);
261 status = 1;
263 close(obj_fd);
264 imsg_clear(&ibuf);
265 close(imsg_fds[1]);
266 _exit(status);
269 static const struct got_error *
270 wait_for_child(pid_t pid)
272 int child_status;
274 waitpid(pid, &child_status, 0);
276 if (!WIFEXITED(child_status))
277 return got_error(GOT_ERR_PRIVSEP_DIED);
279 if (WEXITSTATUS(child_status) != 0)
280 return got_error(GOT_ERR_PRIVSEP_EXIT);
282 return NULL;
285 static const struct got_error *
286 read_object_header_privsep(struct got_object **obj, int fd)
288 struct imsgbuf parent_ibuf;
289 int imsg_fds[2];
290 const struct got_error *err = NULL, *err_child = NULL;
291 pid_t pid;
293 if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_fds) == -1)
294 return got_error_from_errno();
296 pid = fork();
297 if (pid == -1)
298 return got_error_from_errno();
299 else if (pid == 0) {
300 read_object_header_privsep_child(fd, imsg_fds);
301 /* not reached */
304 close(imsg_fds[1]);
305 imsg_init(&parent_ibuf, imsg_fds[0]);
306 err = got_privsep_recv_obj(obj, &parent_ibuf);
307 imsg_clear(&parent_ibuf);
308 err_child = wait_for_child(pid);
309 close(imsg_fds[0]);
310 return err ? err : err_child;
313 static const struct got_error *
314 object_path(char **path, struct got_object_id *id, struct got_repository *repo)
316 const struct got_error *err = NULL;
317 char *hex = NULL;
318 char *path_objects = got_repo_get_path_objects(repo);
320 *path = NULL;
322 if (path_objects == NULL)
323 return got_error_from_errno();
325 err = got_object_id_str(&hex, id);
326 if (err)
327 goto done;
329 if (asprintf(path, "%s/%.2x/%s", path_objects,
330 id->sha1[0], hex + 2) == -1)
331 err = got_error_from_errno();
333 done:
334 free(hex);
335 free(path_objects);
336 return err;
339 static const struct got_error *
340 open_loose_object(int *fd, struct got_object *obj, struct got_repository *repo)
342 const struct got_error *err = NULL;
343 char *path;
345 err = object_path(&path, &obj->id, repo);
346 if (err)
347 return err;
348 *fd = open(path, O_RDONLY | O_NOFOLLOW, GOT_DEFAULT_FILE_MODE);
349 if (*fd == -1) {
350 err = got_error_from_errno();
351 goto done;
353 done:
354 free(path);
355 return err;
358 const struct got_error *
359 got_object_open(struct got_object **obj, struct got_repository *repo,
360 struct got_object_id *id)
362 const struct got_error *err = NULL;
363 char *path;
364 int fd;
366 *obj = got_repo_get_cached_object(repo, id);
367 if (*obj != NULL) {
368 (*obj)->refcnt++;
369 return NULL;
372 err = object_path(&path, id, repo);
373 if (err)
374 return err;
376 fd = open(path, O_RDONLY | O_NOFOLLOW, GOT_DEFAULT_FILE_MODE);
377 if (fd == -1) {
378 if (errno != ENOENT) {
379 err = got_error_from_errno();
380 goto done;
382 err = got_packfile_open_object(obj, id, repo);
383 if (err)
384 goto done;
385 if (*obj == NULL)
386 err = got_error(GOT_ERR_NO_OBJ);
387 } else {
388 err = read_object_header_privsep(obj, fd);
389 if (err)
390 goto done;
391 memcpy((*obj)->id.sha1, id->sha1, SHA1_DIGEST_LENGTH);
394 if (err == NULL) {
395 (*obj)->refcnt++;
396 err = got_repo_cache_object(repo, id, *obj);
398 done:
399 free(path);
400 if (fd != -1)
401 close(fd);
402 return err;
406 const struct got_error *
407 got_object_open_by_id_str(struct got_object **obj, struct got_repository *repo,
408 const char *id_str)
410 struct got_object_id id;
412 if (!got_parse_sha1_digest(id.sha1, id_str))
413 return got_error(GOT_ERR_BAD_OBJ_ID_STR);
415 return got_object_open(obj, repo, &id);
418 void
419 got_object_close(struct got_object *obj)
421 if (obj->refcnt > 0) {
422 obj->refcnt--;
423 if (obj->refcnt > 0)
424 return;
427 if (obj->flags & GOT_OBJ_FLAG_DELTIFIED) {
428 struct got_delta *delta;
429 while (!SIMPLEQ_EMPTY(&obj->deltas.entries)) {
430 delta = SIMPLEQ_FIRST(&obj->deltas.entries);
431 SIMPLEQ_REMOVE_HEAD(&obj->deltas.entries, entry);
432 got_delta_close(delta);
435 if (obj->flags & GOT_OBJ_FLAG_PACKED)
436 free(obj->path_packfile);
437 free(obj);
440 struct got_commit_object *
441 got_object_commit_alloc_partial(void)
443 struct got_commit_object *commit;
445 commit = calloc(1, sizeof(*commit));
446 if (commit == NULL)
447 return NULL;
448 commit->tree_id = calloc(1, sizeof(*commit->tree_id));
449 if (commit->tree_id == NULL) {
450 free(commit);
451 return NULL;
454 SIMPLEQ_INIT(&commit->parent_ids);
456 return commit;
459 const struct got_error *
460 got_object_open_as_commit(struct got_commit_object **commit,
461 struct got_repository *repo, struct got_object_id *id)
463 const struct got_error *err;
464 struct got_object *obj;
466 *commit = NULL;
468 err = got_object_open(&obj, repo, id);
469 if (err)
470 return err;
471 if (got_object_get_type(obj) != GOT_OBJ_TYPE_COMMIT) {
472 err = got_error(GOT_ERR_OBJ_TYPE);
473 goto done;
476 err = got_object_commit_open(commit, repo, obj);
477 done:
478 got_object_close(obj);
479 return err;
482 const struct got_error *
483 got_object_qid_alloc(struct got_object_qid **qid, struct got_object_id *id)
485 const struct got_error *err = NULL;
487 *qid = calloc(1, sizeof(**qid));
488 if (*qid == NULL)
489 return got_error_from_errno();
491 (*qid)->id = got_object_id_dup(id);
492 if ((*qid)->id == NULL) {
493 err = got_error_from_errno();
494 got_object_qid_free(*qid);
495 *qid = NULL;
496 return err;
499 return NULL;
502 void
503 got_object_qid_free(struct got_object_qid *qid)
505 free(qid->id);
506 free(qid);
509 const struct got_error *
510 got_object_commit_add_parent(struct got_commit_object *commit,
511 const char *id_str)
513 const struct got_error *err = NULL;
514 struct got_object_qid *qid;
516 qid = malloc(sizeof(*qid));
517 if (qid == NULL)
518 return got_error_from_errno();
520 qid->id = malloc(sizeof(*qid->id));
521 if (qid->id == NULL) {
522 err = got_error_from_errno();
523 got_object_qid_free(qid);
524 return err;
527 if (!got_parse_sha1_digest(qid->id->sha1, id_str)) {
528 err = got_error(GOT_ERR_BAD_OBJ_DATA);
529 free(qid->id);
530 free(qid);
531 return err;
534 SIMPLEQ_INSERT_TAIL(&commit->parent_ids, qid, entry);
535 commit->nparents++;
537 return NULL;
540 static const struct got_error *
541 parse_gmtoff(time_t *gmtoff, const char *tzstr)
543 int sign = 1;
544 const char *p = tzstr;
545 time_t h, m;
547 *gmtoff = 0;
549 if (*p == '-')
550 sign = -1;
551 else if (*p != '+')
552 return got_error(GOT_ERR_BAD_OBJ_DATA);
553 p++;
554 if (!isdigit(*p) && !isdigit(*(p + 1)))
555 return got_error(GOT_ERR_BAD_OBJ_DATA);
556 h = (((*p - '0') * 10) + (*(p + 1) - '0'));
558 p += 2;
559 if (!isdigit(*p) && !isdigit(*(p + 1)))
560 return got_error(GOT_ERR_BAD_OBJ_DATA);
561 m = ((*p - '0') * 10) + (*(p + 1) - '0');
563 *gmtoff = (h * 60 * 60 + m * 60) * sign;
564 return NULL;
567 static const struct got_error *
568 parse_commit_time(struct tm *tm, char *committer)
570 const struct got_error *err = NULL;
571 const char *errstr;
572 char *space, *tzstr;
573 time_t gmtoff;
574 time_t time;
576 /* Parse and strip off trailing timezone indicator string. */
577 space = strrchr(committer, ' ');
578 if (space == NULL)
579 return got_error(GOT_ERR_BAD_OBJ_DATA);
580 tzstr = strdup(space + 1);
581 if (tzstr == NULL)
582 return got_error_from_errno();
583 err = parse_gmtoff(&gmtoff, tzstr);
584 free(tzstr);
585 if (err)
586 return err;
587 *space = '\0';
589 /* Timestamp is separated from committer name + email by space. */
590 space = strrchr(committer, ' ');
591 if (space == NULL)
592 return got_error(GOT_ERR_BAD_OBJ_DATA);
594 /* Timestamp parsed here is expressed in comitter's local time. */
595 time = strtonum(space + 1, 0, INT64_MAX, &errstr);
596 if (errstr)
597 return got_error(GOT_ERR_BAD_OBJ_DATA);
599 /* Express the time stamp in UTC. */
600 memset(tm, 0, sizeof(*tm));
601 time -= gmtoff;
602 if (localtime_r(&time, tm) == NULL)
603 return got_error_from_errno();
604 tm->tm_gmtoff = gmtoff;
606 /* Strip off parsed time information, leaving just author and email. */
607 *space = '\0';
609 return NULL;
612 static const struct got_error *
613 parse_commit_object(struct got_commit_object **commit, char *buf, size_t len)
615 const struct got_error *err = NULL;
616 char *s = buf;
617 size_t tlen;
618 ssize_t remain = (ssize_t)len;
620 *commit = got_object_commit_alloc_partial();
621 if (*commit == NULL)
622 return got_error_from_errno();
624 tlen = strlen(GOT_COMMIT_TAG_TREE);
625 if (strncmp(s, GOT_COMMIT_TAG_TREE, tlen) == 0) {
626 remain -= tlen;
627 if (remain < SHA1_DIGEST_STRING_LENGTH) {
628 err = got_error(GOT_ERR_BAD_OBJ_DATA);
629 goto done;
631 s += tlen;
632 if (!got_parse_sha1_digest((*commit)->tree_id->sha1, s)) {
633 err = got_error(GOT_ERR_BAD_OBJ_DATA);
634 goto done;
636 remain -= SHA1_DIGEST_STRING_LENGTH;
637 s += SHA1_DIGEST_STRING_LENGTH;
638 } else {
639 err = got_error(GOT_ERR_BAD_OBJ_DATA);
640 goto done;
643 tlen = strlen(GOT_COMMIT_TAG_PARENT);
644 while (strncmp(s, GOT_COMMIT_TAG_PARENT, tlen) == 0) {
645 remain -= tlen;
646 if (remain < SHA1_DIGEST_STRING_LENGTH) {
647 err = got_error(GOT_ERR_BAD_OBJ_DATA);
648 goto done;
650 s += tlen;
651 err = got_object_commit_add_parent(*commit, s);
652 if (err)
653 goto done;
655 remain -= SHA1_DIGEST_STRING_LENGTH;
656 s += SHA1_DIGEST_STRING_LENGTH;
659 tlen = strlen(GOT_COMMIT_TAG_AUTHOR);
660 if (strncmp(s, GOT_COMMIT_TAG_AUTHOR, tlen) == 0) {
661 char *p;
662 size_t slen;
664 remain -= tlen;
665 if (remain <= 0) {
666 err = got_error(GOT_ERR_BAD_OBJ_DATA);
667 goto done;
669 s += tlen;
670 p = strchr(s, '\n');
671 if (p == NULL) {
672 err = got_error(GOT_ERR_BAD_OBJ_DATA);
673 goto done;
675 *p = '\0';
676 slen = strlen(s);
677 err = parse_commit_time(&(*commit)->tm_author, s);
678 if (err)
679 goto done;
680 (*commit)->author = strdup(s);
681 if ((*commit)->author == NULL) {
682 err = got_error_from_errno();
683 goto done;
685 s += slen + 1;
686 remain -= slen + 1;
689 tlen = strlen(GOT_COMMIT_TAG_COMMITTER);
690 if (strncmp(s, GOT_COMMIT_TAG_COMMITTER, tlen) == 0) {
691 char *p;
692 size_t slen;
694 remain -= tlen;
695 if (remain <= 0) {
696 err = got_error(GOT_ERR_BAD_OBJ_DATA);
697 goto done;
699 s += tlen;
700 p = strchr(s, '\n');
701 if (p == NULL) {
702 err = got_error(GOT_ERR_BAD_OBJ_DATA);
703 goto done;
705 *p = '\0';
706 slen = strlen(s);
707 err = parse_commit_time(&(*commit)->tm_committer, s);
708 if (err)
709 goto done;
710 (*commit)->committer = strdup(s);
711 if ((*commit)->committer == NULL) {
712 err = got_error_from_errno();
713 goto done;
715 s += slen + 1;
716 remain -= slen + 1;
719 (*commit)->logmsg = strndup(s, remain);
720 if ((*commit)->logmsg == NULL) {
721 err = got_error_from_errno();
722 goto done;
724 done:
725 if (err) {
726 got_object_commit_close(*commit);
727 *commit = NULL;
729 return err;
732 static void
733 tree_entry_close(struct got_tree_entry *te)
735 free(te->id);
736 free(te->name);
737 free(te);
740 struct got_tree_entry *
741 got_alloc_tree_entry_partial(void)
743 struct got_tree_entry *te;
745 te = calloc(1, sizeof(*te));
746 if (te == NULL)
747 return NULL;
749 te->id = calloc(1, sizeof(*te->id));
750 if (te->id == NULL) {
751 free(te);
752 te = NULL;
754 return te;
757 static const struct got_error *
758 parse_tree_entry(struct got_tree_entry **te, size_t *elen, char *buf,
759 size_t maxlen)
761 char *p = buf, *space;
762 const struct got_error *err = NULL;
764 *te = got_alloc_tree_entry_partial();
765 if (*te == NULL)
766 return got_error_from_errno();
768 *elen = strlen(buf) + 1;
769 if (*elen > maxlen) {
770 free(*te);
771 *te = NULL;
772 return got_error(GOT_ERR_BAD_OBJ_DATA);
775 space = strchr(buf, ' ');
776 if (space == NULL) {
777 err = got_error(GOT_ERR_BAD_OBJ_DATA);
778 free(*te);
779 *te = NULL;
780 return err;
782 while (*p != ' ') {
783 if (*p < '0' && *p > '7') {
784 err = got_error(GOT_ERR_BAD_OBJ_DATA);
785 goto done;
787 (*te)->mode <<= 3;
788 (*te)->mode |= *p - '0';
789 p++;
792 (*te)->name = strdup(space + 1);
793 if (*elen > maxlen || maxlen - *elen < SHA1_DIGEST_LENGTH) {
794 err = got_error(GOT_ERR_BAD_OBJ_DATA);
795 goto done;
797 buf += strlen(buf) + 1;
798 memcpy((*te)->id->sha1, buf, SHA1_DIGEST_LENGTH);
799 *elen += SHA1_DIGEST_LENGTH;
800 done:
801 if (err) {
802 tree_entry_close(*te);
803 *te = NULL;
805 return err;
808 static const struct got_error *
809 parse_tree_object(struct got_tree_object **tree, uint8_t *buf, size_t len)
811 const struct got_error *err;
812 size_t remain = len;
814 *tree = calloc(1, sizeof(**tree));
815 if (*tree == NULL)
816 return got_error_from_errno();
818 SIMPLEQ_INIT(&(*tree)->entries.head);
820 while (remain > 0) {
821 struct got_tree_entry *te;
822 size_t elen;
824 err = parse_tree_entry(&te, &elen, buf, remain);
825 if (err)
826 return err;
827 (*tree)->entries.nentries++;
828 SIMPLEQ_INSERT_TAIL(&(*tree)->entries.head, te, entry);
829 buf += elen;
830 remain -= elen;
833 if (remain != 0) {
834 got_object_tree_close(*tree);
835 return got_error(GOT_ERR_BAD_OBJ_DATA);
838 return NULL;
841 static const struct got_error *
842 read_to_mem(uint8_t **outbuf, size_t *outlen, FILE *f)
844 const struct got_error *err = NULL;
845 static const size_t blocksize = 512;
846 size_t n, total, remain;
847 uint8_t *buf;
849 *outbuf = NULL;
850 *outlen = 0;
852 buf = malloc(blocksize);
853 if (buf == NULL)
854 return got_error_from_errno();
856 remain = blocksize;
857 total = 0;
858 while (1) {
859 if (remain == 0) {
860 uint8_t *newbuf;
861 newbuf = reallocarray(buf, 1, total + blocksize);
862 if (newbuf == NULL) {
863 err = got_error_from_errno();
864 goto done;
866 buf = newbuf;
867 remain += blocksize;
869 n = fread(buf + total, 1, remain, f);
870 if (n == 0) {
871 if (ferror(f)) {
872 err = got_ferror(f, GOT_ERR_IO);
873 goto done;
875 break; /* EOF */
877 remain -= n;
878 total += n;
879 };
881 done:
882 if (err == NULL) {
883 *outbuf = buf;
884 *outlen = total;
885 } else
886 free(buf);
887 return err;
890 static const struct got_error *
891 read_commit_object(struct got_commit_object **commit, struct got_object *obj,
892 FILE *f)
894 const struct got_error *err = NULL;
895 size_t len;
896 uint8_t *p;
898 if (obj->flags & GOT_OBJ_FLAG_PACKED)
899 err = read_to_mem(&p, &len, f);
900 else
901 err = got_inflate_to_mem(&p, &len, f);
902 if (err)
903 return err;
905 if (len < obj->hdrlen + obj->size) {
906 err = got_error(GOT_ERR_BAD_OBJ_DATA);
907 goto done;
910 /* Skip object header. */
911 len -= obj->hdrlen;
912 err = parse_commit_object(commit, p + obj->hdrlen, len);
913 free(p);
914 done:
915 return err;
918 static void
919 read_commit_object_privsep_child(struct got_object *obj, int obj_fd,
920 int imsg_fds[2])
922 const struct got_error *err = NULL;
923 struct got_commit_object *commit = NULL;
924 struct imsgbuf ibuf;
925 FILE *f = NULL;
926 int status = 0;
928 setproctitle("read commit object");
929 close(imsg_fds[0]);
930 imsg_init(&ibuf, imsg_fds[1]);
932 /* revoke access to most system calls */
933 if (pledge("stdio", NULL) == -1) {
934 err = got_error_from_errno();
935 goto done;
938 f = fdopen(obj_fd, "rb");
939 if (f == NULL) {
940 err = got_error_from_errno();
941 close(obj_fd);
942 goto done;
945 err = read_commit_object(&commit, obj, f);
946 if (err)
947 goto done;
949 err = got_privsep_send_commit(&ibuf, commit);
950 done:
951 if (commit)
952 got_object_commit_close(commit);
953 if (err) {
954 got_privsep_send_error(&ibuf, err);
955 status = 1;
957 if (f)
958 fclose(f);
959 imsg_clear(&ibuf);
960 close(imsg_fds[1]);
961 _exit(status);
964 static const struct got_error *
965 read_commit_object_privsep(struct got_commit_object **commit,
966 struct got_repository *repo, struct got_object *obj, int fd)
968 const struct got_error *err = NULL, *err_child = NULL;
969 struct imsgbuf parent_ibuf;
970 int imsg_fds[2];
971 pid_t pid;
973 if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_fds) == -1)
974 return got_error_from_errno();
976 pid = fork();
977 if (pid == -1)
978 return got_error_from_errno();
979 else if (pid == 0) {
980 read_commit_object_privsep_child(obj, fd, imsg_fds);
981 /* not reached */
984 close(imsg_fds[1]);
985 imsg_init(&parent_ibuf, imsg_fds[0]);
986 err = got_privsep_recv_commit(commit, &parent_ibuf);
987 imsg_clear(&parent_ibuf);
988 err_child = wait_for_child(pid);
989 close(imsg_fds[0]);
990 return err ? err : err_child;
993 const struct got_error *
994 got_object_commit_open(struct got_commit_object **commit,
995 struct got_repository *repo, struct got_object *obj)
997 const struct got_error *err = NULL;
999 *commit = got_repo_get_cached_commit(repo, &obj->id);
1000 if (*commit != NULL) {
1001 (*commit)->refcnt++;
1002 return NULL;
1005 if (obj->type != GOT_OBJ_TYPE_COMMIT)
1006 return got_error(GOT_ERR_OBJ_TYPE);
1008 if (obj->flags & GOT_OBJ_FLAG_PACKED) {
1009 uint8_t *buf;
1010 size_t len;
1011 err = got_packfile_extract_object_to_mem(&buf, &len, obj, repo);
1012 if (err)
1013 return err;
1014 obj->size = len;
1015 err = parse_commit_object(commit, buf, len);
1016 free(buf);
1017 } else {
1018 int fd;
1019 err = open_loose_object(&fd, obj, repo);
1020 if (err)
1021 return err;
1022 err = read_commit_object_privsep(commit, repo, obj, fd);
1023 close(fd);
1026 if (err == NULL) {
1027 (*commit)->refcnt++;
1028 err = got_repo_cache_commit(repo, &obj->id, *commit);
1031 return err;
1034 void
1035 got_object_commit_close(struct got_commit_object *commit)
1037 struct got_object_qid *qid;
1039 if (commit->refcnt > 0) {
1040 commit->refcnt--;
1041 if (commit->refcnt > 0)
1042 return;
1045 while (!SIMPLEQ_EMPTY(&commit->parent_ids)) {
1046 qid = SIMPLEQ_FIRST(&commit->parent_ids);
1047 SIMPLEQ_REMOVE_HEAD(&commit->parent_ids, entry);
1048 got_object_qid_free(qid);
1051 free(commit->tree_id);
1052 free(commit->author);
1053 free(commit->committer);
1054 free(commit->logmsg);
1055 free(commit);
1058 static const struct got_error *
1059 read_tree_object(struct got_tree_object **tree, struct got_object *obj, FILE *f)
1061 const struct got_error *err = NULL;
1062 size_t len;
1063 uint8_t *p;
1065 if (obj->flags & GOT_OBJ_FLAG_PACKED)
1066 err = read_to_mem(&p, &len, f);
1067 else
1068 err = got_inflate_to_mem(&p, &len, f);
1069 if (err)
1070 return err;
1072 if (len < obj->hdrlen + obj->size) {
1073 err = got_error(GOT_ERR_BAD_OBJ_DATA);
1074 goto done;
1077 /* Skip object header. */
1078 len -= obj->hdrlen;
1079 err = parse_tree_object(tree, p + obj->hdrlen, len);
1080 free(p);
1081 done:
1082 return err;
1085 static void
1086 read_tree_object_privsep_child(struct got_object *obj, int obj_fd,
1087 int imsg_fds[2])
1089 const struct got_error *err = NULL;
1090 struct got_tree_object *tree = NULL;
1091 struct imsgbuf ibuf;
1092 FILE *f = NULL;
1093 int status = 0;
1095 setproctitle("read tree object");
1096 close(imsg_fds[0]);
1097 imsg_init(&ibuf, imsg_fds[1]);
1099 /* revoke access to most system calls */
1100 if (pledge("stdio", NULL) == -1) {
1101 err = got_error_from_errno();
1102 goto done;
1105 f = fdopen(obj_fd, "rb");
1106 if (f == NULL) {
1107 err = got_error_from_errno();
1108 close(obj_fd);
1109 goto done;
1112 err = read_tree_object(&tree, obj, f);
1113 if (err)
1114 goto done;
1116 err = got_privsep_send_tree(&ibuf, tree);
1117 done:
1118 if (tree)
1119 got_object_tree_close(tree);
1120 if (err) {
1121 got_privsep_send_error(&ibuf, err);
1122 status = 1;
1124 if (f)
1125 fclose(f);
1126 imsg_clear(&ibuf);
1127 close(imsg_fds[1]);
1128 _exit(status);
1131 static const struct got_error *
1132 read_tree_object_privsep(struct got_tree_object **tree, struct got_object *obj,
1133 int fd)
1135 const struct got_error *err = NULL, *err_child = NULL;
1136 struct imsgbuf parent_ibuf;
1137 int imsg_fds[2];
1138 pid_t pid;
1140 if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_fds) == -1)
1141 return got_error_from_errno();
1143 pid = fork();
1144 if (pid == -1)
1145 return got_error_from_errno();
1146 else if (pid == 0) {
1147 read_tree_object_privsep_child(obj, fd, imsg_fds);
1148 /* not reached */
1151 close(imsg_fds[1]);
1152 imsg_init(&parent_ibuf, imsg_fds[0]);
1153 err = got_privsep_recv_tree(tree, &parent_ibuf);
1154 imsg_clear(&parent_ibuf);
1155 err_child = wait_for_child(pid);
1156 close(imsg_fds[0]);
1157 return err ? err : err_child;
1160 const struct got_error *
1161 got_object_tree_open(struct got_tree_object **tree,
1162 struct got_repository *repo, struct got_object *obj)
1164 const struct got_error *err = NULL;
1166 *tree = got_repo_get_cached_tree(repo, &obj->id);
1167 if (*tree != NULL) {
1168 (*tree)->refcnt++;
1169 return NULL;
1172 if (obj->type != GOT_OBJ_TYPE_TREE)
1173 return got_error(GOT_ERR_OBJ_TYPE);
1175 if (obj->flags & GOT_OBJ_FLAG_PACKED) {
1176 uint8_t *buf;
1177 size_t len;
1178 err = got_packfile_extract_object_to_mem(&buf, &len, obj, repo);
1179 if (err)
1180 return err;
1181 obj->size = len;
1182 err = parse_tree_object(tree, buf, len);
1183 free(buf);
1184 } else {
1185 int fd;
1186 err = open_loose_object(&fd, obj, repo);
1187 if (err)
1188 return err;
1189 err = read_tree_object_privsep(tree, obj, fd);
1190 close(fd);
1193 if (err == NULL) {
1194 (*tree)->refcnt++;
1195 err = got_repo_cache_tree(repo, &obj->id, *tree);
1198 return err;
1201 const struct got_error *
1202 got_object_open_as_tree(struct got_tree_object **tree,
1203 struct got_repository *repo, struct got_object_id *id)
1205 const struct got_error *err;
1206 struct got_object *obj;
1208 *tree = NULL;
1210 err = got_object_open(&obj, repo, id);
1211 if (err)
1212 return err;
1213 if (got_object_get_type(obj) != GOT_OBJ_TYPE_TREE) {
1214 err = got_error(GOT_ERR_OBJ_TYPE);
1215 goto done;
1218 err = got_object_tree_open(tree, repo, obj);
1219 done:
1220 got_object_close(obj);
1221 return err;
1224 void
1225 got_object_tree_close(struct got_tree_object *tree)
1227 struct got_tree_entry *te;
1229 if (tree->refcnt > 0) {
1230 tree->refcnt--;
1231 if (tree->refcnt > 0)
1232 return;
1235 while (!SIMPLEQ_EMPTY(&tree->entries.head)) {
1236 te = SIMPLEQ_FIRST(&tree->entries.head);
1237 SIMPLEQ_REMOVE_HEAD(&tree->entries.head, entry);
1238 tree_entry_close(te);
1241 free(tree);
1244 const struct got_tree_entries *
1245 got_object_tree_get_entries(struct got_tree_object *tree)
1247 return &tree->entries;
1250 static const struct got_error *
1251 read_blob_object_privsep_child(int outfd, int infd, int imsg_fds[2])
1253 const struct got_error *err = NULL;
1254 struct imsgbuf ibuf;
1255 int status = 0;
1256 size_t size;
1257 FILE *infile = NULL;
1259 setproctitle("read blob object");
1260 close(imsg_fds[0]);
1261 imsg_init(&ibuf, imsg_fds[1]);
1263 /* revoke access to most system calls */
1264 if (pledge("stdio", NULL) == -1) {
1265 err = got_error_from_errno();
1266 goto done;
1269 infile = fdopen(infd, "rb");
1270 if (infile == NULL) {
1271 err = got_error_from_errno();
1272 close(infd);
1273 goto done;
1275 err = got_inflate_to_fd(&size, infile, outfd);
1276 fclose(infile);
1277 if (err)
1278 goto done;
1280 err = got_privsep_send_blob(&ibuf, size);
1281 done:
1282 if (err) {
1283 got_privsep_send_error(&ibuf, err);
1284 status = 1;
1286 close(outfd);
1287 imsg_clear(&ibuf);
1288 close(imsg_fds[1]);
1289 _exit(status);
1292 static const struct got_error *
1293 read_blob_object_privsep(size_t *size, int outfd, int infd)
1295 struct imsgbuf parent_ibuf;
1296 int imsg_fds[2];
1297 const struct got_error *err = NULL, *err_child = NULL;
1298 pid_t pid;
1300 if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_fds) == -1)
1301 return got_error_from_errno();
1303 pid = fork();
1304 if (pid == -1)
1305 return got_error_from_errno();
1306 else if (pid == 0) {
1307 read_blob_object_privsep_child(outfd, infd, imsg_fds);
1308 /* not reached */
1311 close(imsg_fds[1]);
1312 imsg_init(&parent_ibuf, imsg_fds[0]);
1313 err = got_privsep_recv_blob(size, &parent_ibuf);
1314 imsg_clear(&parent_ibuf);
1315 err_child = wait_for_child(pid);
1316 close(imsg_fds[0]);
1317 if (lseek(outfd, SEEK_SET, 0) == -1)
1318 err = got_error_from_errno();
1319 return err ? err : err_child;
1322 const struct got_error *
1323 got_object_blob_open(struct got_blob_object **blob,
1324 struct got_repository *repo, struct got_object *obj, size_t blocksize)
1326 const struct got_error *err = NULL;
1328 if (obj->type != GOT_OBJ_TYPE_BLOB)
1329 return got_error(GOT_ERR_OBJ_TYPE);
1331 if (blocksize < obj->hdrlen)
1332 return got_error(GOT_ERR_NO_SPACE);
1334 *blob = calloc(1, sizeof(**blob));
1335 if (*blob == NULL)
1336 return got_error_from_errno();
1338 (*blob)->read_buf = malloc(blocksize);
1339 if ((*blob)->read_buf == NULL) {
1340 err = got_error_from_errno();
1341 goto done;
1343 if (obj->flags & GOT_OBJ_FLAG_PACKED) {
1344 err = got_packfile_extract_object(&((*blob)->f), obj, repo);
1345 if (err)
1346 goto done;
1347 } else {
1348 int infd, outfd;
1349 size_t size;
1350 struct stat sb;
1352 err = open_loose_object(&infd, obj, repo);
1353 if (err)
1354 goto done;
1357 outfd = got_opentempfd();
1358 if (outfd == -1) {
1359 err = got_error_from_errno();
1360 close(infd);
1361 goto done;
1364 err = read_blob_object_privsep(&size, outfd, infd);
1365 close(infd);
1366 if (err)
1367 goto done;
1369 if (size != obj->hdrlen + obj->size) {
1370 err = got_error(GOT_ERR_PRIVSEP_LEN);
1371 close(outfd);
1372 goto done;
1375 if (fstat(outfd, &sb) == -1) {
1376 err = got_error_from_errno();
1377 close(outfd);
1378 goto done;
1381 if (sb.st_size != size) {
1382 err = got_error(GOT_ERR_PRIVSEP_LEN);
1383 close(outfd);
1384 goto done;
1387 (*blob)->f = fdopen(outfd, "rb");
1388 if ((*blob)->f == NULL) {
1389 err = got_error_from_errno();
1390 close(outfd);
1391 goto done;
1395 (*blob)->hdrlen = obj->hdrlen;
1396 (*blob)->blocksize = blocksize;
1397 memcpy(&(*blob)->id.sha1, obj->id.sha1, SHA1_DIGEST_LENGTH);
1399 done:
1400 if (err && *blob) {
1401 if ((*blob)->f)
1402 fclose((*blob)->f);
1403 free((*blob)->read_buf);
1404 free(*blob);
1405 *blob = NULL;
1407 return err;
1410 const struct got_error *
1411 got_object_open_as_blob(struct got_blob_object **blob,
1412 struct got_repository *repo, struct got_object_id *id,
1413 size_t blocksize)
1415 const struct got_error *err;
1416 struct got_object *obj;
1418 *blob = NULL;
1420 err = got_object_open(&obj, repo, id);
1421 if (err)
1422 return err;
1423 if (got_object_get_type(obj) != GOT_OBJ_TYPE_BLOB) {
1424 err = got_error(GOT_ERR_OBJ_TYPE);
1425 goto done;
1428 err = got_object_blob_open(blob, repo, obj, blocksize);
1429 done:
1430 got_object_close(obj);
1431 return err;
1434 void
1435 got_object_blob_close(struct got_blob_object *blob)
1437 free(blob->read_buf);
1438 fclose(blob->f);
1439 free(blob);
1442 char *
1443 got_object_blob_id_str(struct got_blob_object *blob, char *buf, size_t size)
1445 return got_sha1_digest_to_str(blob->id.sha1, buf, size);
1448 size_t
1449 got_object_blob_get_hdrlen(struct got_blob_object *blob)
1451 return blob->hdrlen;
1454 const uint8_t *
1455 got_object_blob_get_read_buf(struct got_blob_object *blob)
1457 return blob->read_buf;
1460 const struct got_error *
1461 got_object_blob_read_block(size_t *outlenp, struct got_blob_object *blob)
1463 size_t n;
1465 n = fread(blob->read_buf, 1, blob->blocksize, blob->f);
1466 if (n == 0 && ferror(blob->f))
1467 return got_ferror(blob->f, GOT_ERR_IO);
1468 *outlenp = n;
1469 return NULL;
1472 const struct got_error *
1473 got_object_blob_dump_to_file(size_t *total_len, size_t *nlines,
1474 FILE *outfile, struct got_blob_object *blob)
1476 const struct got_error *err = NULL;
1477 size_t len, hdrlen;
1478 const uint8_t *buf;
1479 int i;
1481 if (total_len)
1482 *total_len = 0;
1483 if (nlines)
1484 *nlines = 0;
1486 hdrlen = got_object_blob_get_hdrlen(blob);
1487 do {
1488 err = got_object_blob_read_block(&len, blob);
1489 if (err)
1490 return err;
1491 if (len == 0)
1492 break;
1493 if (total_len)
1494 *total_len += len;
1495 buf = got_object_blob_get_read_buf(blob);
1496 if (nlines) {
1497 for (i = 0; i < len; i++) {
1498 if (buf[i] == '\n')
1499 (*nlines)++;
1502 /* Skip blob object header first time around. */
1503 fwrite(buf + hdrlen, len - hdrlen, 1, outfile);
1504 hdrlen = 0;
1505 } while (len != 0);
1507 fflush(outfile);
1508 rewind(outfile);
1510 return NULL;
1513 static struct got_tree_entry *
1514 find_entry_by_name(struct got_tree_object *tree, const char *name)
1516 struct got_tree_entry *te;
1518 SIMPLEQ_FOREACH(te, &tree->entries.head, entry) {
1519 if (strcmp(te->name, name) == 0)
1520 return te;
1522 return NULL;
1525 const struct got_error *
1526 got_object_open_by_path(struct got_object **obj, struct got_repository *repo,
1527 struct got_object_id *commit_id, const char *path)
1529 const struct got_error *err = NULL;
1530 struct got_commit_object *commit = NULL;
1531 struct got_tree_object *tree = NULL;
1532 struct got_tree_entry *te = NULL;
1533 char *seg, *s, *s0 = NULL;
1534 size_t len = strlen(path);
1536 *obj = NULL;
1538 /* We are expecting an absolute in-repository path. */
1539 if (path[0] != '/')
1540 return got_error(GOT_ERR_NOT_ABSPATH);
1542 err = got_object_open_as_commit(&commit, repo, commit_id);
1543 if (err)
1544 goto done;
1546 /* Handle opening of root of commit's tree. */
1547 if (path[1] == '\0') {
1548 err = got_object_open(obj, repo, commit->tree_id);
1549 goto done;
1552 err = got_object_open_as_tree(&tree, repo, commit->tree_id);
1553 if (err)
1554 goto done;
1556 s0 = strdup(path);
1557 if (s0 == NULL) {
1558 err = got_error_from_errno();
1559 goto done;
1561 err = got_canonpath(path, s0, len + 1);
1562 if (err)
1563 goto done;
1565 s = s0;
1566 s++; /* skip leading '/' */
1567 len--;
1568 seg = s;
1569 while (len > 0) {
1570 struct got_tree_object *next_tree;
1572 if (*s != '/') {
1573 s++;
1574 len--;
1575 if (*s)
1576 continue;
1579 /* end of path segment */
1580 *s = '\0';
1582 te = find_entry_by_name(tree, seg);
1583 if (te == NULL) {
1584 err = got_error(GOT_ERR_NO_OBJ);
1585 goto done;
1588 if (len == 0)
1589 break;
1591 seg = s + 1;
1592 s++;
1593 len--;
1594 if (*s) {
1595 err = got_object_open_as_tree(&next_tree, repo,
1596 te->id);
1597 te = NULL;
1598 if (err)
1599 goto done;
1600 got_object_tree_close(tree);
1601 tree = next_tree;
1605 if (te)
1606 err = got_object_open(obj, repo, te->id);
1607 else
1608 err = got_error(GOT_ERR_NO_OBJ);
1609 done:
1610 free(s0);
1611 if (commit)
1612 got_object_commit_close(commit);
1613 if (tree)
1614 got_object_tree_close(tree);
1615 return err;