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 i;
196 buf = malloc(zbsize);
197 if (buf == NULL)
198 return got_error_from_errno();
200 err = got_inflate_init(&zb, NULL, zbsize);
201 if (err)
202 return err;
204 i = 0;
205 totlen = 0;
206 do {
207 err = got_inflate_read_fd(&zb, fd, &outlen);
208 if (err)
209 goto done;
210 if (strchr(zb.outbuf, '\0') == NULL) {
211 buf = reallocarray(buf, 2 + i, zbsize);
212 if (buf == NULL) {
213 err = got_error_from_errno();
214 goto done;
217 memcpy(buf + totlen, zb.outbuf, outlen);
218 totlen += outlen;
219 i++;
220 } while (strchr(zb.outbuf, '\0') == NULL);
222 err = parse_object_header(obj, buf, totlen);
223 done:
224 got_inflate_end(&zb);
225 return err;
228 static void
229 read_object_header_privsep_child(int obj_fd, int imsg_fds[2])
231 const struct got_error *err = NULL;
232 struct got_object *obj = NULL;
233 struct imsgbuf ibuf;
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 err = read_object_header(&obj, obj_fd);
247 if (err)
248 goto done;
250 err = got_privsep_send_obj(&ibuf, obj, 0);
251 done:
252 if (obj)
253 got_object_close(obj);
254 if (err) {
255 got_privsep_send_error(&ibuf, err);
256 status = 1;
258 close(obj_fd);
259 imsg_clear(&ibuf);
260 close(imsg_fds[1]);
261 _exit(status);
264 static const struct got_error *
265 wait_for_child(pid_t pid)
267 int child_status;
269 waitpid(pid, &child_status, 0);
271 if (!WIFEXITED(child_status))
272 return got_error(GOT_ERR_PRIVSEP_DIED);
274 if (WEXITSTATUS(child_status) != 0)
275 return got_error(GOT_ERR_PRIVSEP_EXIT);
277 return NULL;
280 static const struct got_error *
281 read_object_header_privsep(struct got_object **obj, int fd)
283 struct imsgbuf parent_ibuf;
284 int imsg_fds[2];
285 const struct got_error *err = NULL, *err_child = NULL;
286 pid_t pid;
288 if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_fds) == -1)
289 return got_error_from_errno();
291 pid = fork();
292 if (pid == -1)
293 return got_error_from_errno();
294 else if (pid == 0) {
295 read_object_header_privsep_child(fd, imsg_fds);
296 /* not reached */
299 close(imsg_fds[1]);
300 imsg_init(&parent_ibuf, imsg_fds[0]);
301 err = got_privsep_recv_obj(obj, &parent_ibuf);
302 imsg_clear(&parent_ibuf);
303 err_child = wait_for_child(pid);
304 close(imsg_fds[0]);
305 return err ? err : err_child;
308 static const struct got_error *
309 object_path(char **path, struct got_object_id *id, struct got_repository *repo)
311 const struct got_error *err = NULL;
312 char *hex = NULL;
313 char *path_objects = got_repo_get_path_objects(repo);
315 *path = NULL;
317 if (path_objects == NULL)
318 return got_error_from_errno();
320 err = got_object_id_str(&hex, id);
321 if (err)
322 goto done;
324 if (asprintf(path, "%s/%.2x/%s", path_objects,
325 id->sha1[0], hex + 2) == -1)
326 err = got_error_from_errno();
328 done:
329 free(hex);
330 free(path_objects);
331 return err;
334 static const struct got_error *
335 open_loose_object(int *fd, struct got_object *obj, struct got_repository *repo)
337 const struct got_error *err = NULL;
338 char *path;
340 err = object_path(&path, &obj->id, repo);
341 if (err)
342 return err;
343 *fd = open(path, O_RDONLY | O_NOFOLLOW, GOT_DEFAULT_FILE_MODE);
344 if (*fd == -1) {
345 err = got_error_from_errno();
346 goto done;
348 done:
349 free(path);
350 return err;
353 const struct got_error *
354 got_object_open(struct got_object **obj, struct got_repository *repo,
355 struct got_object_id *id)
357 const struct got_error *err = NULL;
358 char *path;
359 int fd;
361 *obj = got_repo_get_cached_object(repo, id);
362 if (*obj != NULL) {
363 (*obj)->refcnt++;
364 return NULL;
367 err = object_path(&path, id, repo);
368 if (err)
369 return err;
371 fd = open(path, O_RDONLY | O_NOFOLLOW, GOT_DEFAULT_FILE_MODE);
372 if (fd == -1) {
373 if (errno != ENOENT) {
374 err = got_error_from_errno();
375 goto done;
377 err = got_packfile_open_object(obj, id, repo);
378 if (err)
379 goto done;
380 if (*obj == NULL)
381 err = got_error(GOT_ERR_NO_OBJ);
382 } else {
383 err = read_object_header_privsep(obj, fd);
384 if (err)
385 goto done;
386 memcpy((*obj)->id.sha1, id->sha1, SHA1_DIGEST_LENGTH);
389 if (err == NULL) {
390 (*obj)->refcnt++;
391 err = got_repo_cache_object(repo, id, *obj);
393 done:
394 free(path);
395 if (fd != -1)
396 close(fd);
397 return err;
401 const struct got_error *
402 got_object_open_by_id_str(struct got_object **obj, struct got_repository *repo,
403 const char *id_str)
405 struct got_object_id id;
407 if (!got_parse_sha1_digest(id.sha1, id_str))
408 return got_error(GOT_ERR_BAD_OBJ_ID_STR);
410 return got_object_open(obj, repo, &id);
413 void
414 got_object_close(struct got_object *obj)
416 if (obj->refcnt > 0) {
417 obj->refcnt--;
418 if (obj->refcnt > 0)
419 return;
422 if (obj->flags & GOT_OBJ_FLAG_DELTIFIED) {
423 struct got_delta *delta;
424 while (!SIMPLEQ_EMPTY(&obj->deltas.entries)) {
425 delta = SIMPLEQ_FIRST(&obj->deltas.entries);
426 SIMPLEQ_REMOVE_HEAD(&obj->deltas.entries, entry);
427 got_delta_close(delta);
430 if (obj->flags & GOT_OBJ_FLAG_PACKED)
431 free(obj->path_packfile);
432 free(obj);
435 struct got_commit_object *
436 got_object_commit_alloc_partial(void)
438 struct got_commit_object *commit;
440 commit = calloc(1, sizeof(*commit));
441 if (commit == NULL)
442 return NULL;
443 commit->tree_id = calloc(1, sizeof(*commit->tree_id));
444 if (commit->tree_id == NULL) {
445 free(commit);
446 return NULL;
449 SIMPLEQ_INIT(&commit->parent_ids);
451 return commit;
454 const struct got_error *
455 got_object_open_as_commit(struct got_commit_object **commit,
456 struct got_repository *repo, struct got_object_id *id)
458 const struct got_error *err;
459 struct got_object *obj;
461 *commit = NULL;
463 err = got_object_open(&obj, repo, id);
464 if (err)
465 return err;
466 if (got_object_get_type(obj) != GOT_OBJ_TYPE_COMMIT) {
467 err = got_error(GOT_ERR_OBJ_TYPE);
468 goto done;
471 err = got_object_commit_open(commit, repo, obj);
472 done:
473 got_object_close(obj);
474 return err;
477 const struct got_error *
478 got_object_qid_alloc(struct got_object_qid **qid, struct got_object_id *id)
480 const struct got_error *err = NULL;
482 *qid = calloc(1, sizeof(**qid));
483 if (*qid == NULL)
484 return got_error_from_errno();
486 (*qid)->id = got_object_id_dup(id);
487 if ((*qid)->id == NULL) {
488 err = got_error_from_errno();
489 got_object_qid_free(*qid);
490 *qid = NULL;
491 return err;
494 return NULL;
497 void
498 got_object_qid_free(struct got_object_qid *qid)
500 free(qid->id);
501 free(qid);
504 const struct got_error *
505 got_object_commit_add_parent(struct got_commit_object *commit,
506 const char *id_str)
508 const struct got_error *err = NULL;
509 struct got_object_qid *qid;
511 qid = malloc(sizeof(*qid));
512 if (qid == NULL)
513 return got_error_from_errno();
515 qid->id = malloc(sizeof(*qid->id));
516 if (qid->id == NULL) {
517 err = got_error_from_errno();
518 got_object_qid_free(qid);
519 return err;
522 if (!got_parse_sha1_digest(qid->id->sha1, id_str)) {
523 err = got_error(GOT_ERR_BAD_OBJ_DATA);
524 free(qid->id);
525 free(qid);
526 return err;
529 SIMPLEQ_INSERT_TAIL(&commit->parent_ids, qid, entry);
530 commit->nparents++;
532 return NULL;
535 static const struct got_error *
536 parse_gmtoff(time_t *gmtoff, const char *tzstr)
538 int sign = 1;
539 const char *p = tzstr;
540 time_t h, m;
542 *gmtoff = 0;
544 if (*p == '-')
545 sign = -1;
546 else if (*p != '+')
547 return got_error(GOT_ERR_BAD_OBJ_DATA);
548 p++;
549 if (!isdigit(*p) && !isdigit(*(p + 1)))
550 return got_error(GOT_ERR_BAD_OBJ_DATA);
551 h = (((*p - '0') * 10) + (*(p + 1) - '0'));
553 p += 2;
554 if (!isdigit(*p) && !isdigit(*(p + 1)))
555 return got_error(GOT_ERR_BAD_OBJ_DATA);
556 m = ((*p - '0') * 10) + (*(p + 1) - '0');
558 *gmtoff = (h * 60 * 60 + m * 60) * sign;
559 return NULL;
562 static const struct got_error *
563 parse_commit_time(struct tm *tm, char *committer)
565 const struct got_error *err = NULL;
566 const char *errstr;
567 char *space, *tzstr;
568 time_t gmtoff;
569 time_t time;
571 /* Parse and strip off trailing timezone indicator string. */
572 space = strrchr(committer, ' ');
573 if (space == NULL)
574 return got_error(GOT_ERR_BAD_OBJ_DATA);
575 tzstr = strdup(space + 1);
576 if (tzstr == NULL)
577 return got_error_from_errno();
578 err = parse_gmtoff(&gmtoff, tzstr);
579 free(tzstr);
580 if (err)
581 return err;
582 *space = '\0';
584 /* Timestamp is separated from committer name + email by space. */
585 space = strrchr(committer, ' ');
586 if (space == NULL)
587 return got_error(GOT_ERR_BAD_OBJ_DATA);
589 /* Timestamp parsed here is expressed in comitter's local time. */
590 time = strtonum(space + 1, 0, INT64_MAX, &errstr);
591 if (errstr)
592 return got_error(GOT_ERR_BAD_OBJ_DATA);
594 /* Express the time stamp in UTC. */
595 memset(tm, 0, sizeof(*tm));
596 time -= gmtoff;
597 if (localtime_r(&time, tm) == NULL)
598 return got_error_from_errno();
599 tm->tm_gmtoff = gmtoff;
601 /* Strip off parsed time information, leaving just author and email. */
602 *space = '\0';
604 return NULL;
607 static const struct got_error *
608 parse_commit_object(struct got_commit_object **commit, char *buf, size_t len)
610 const struct got_error *err = NULL;
611 char *s = buf;
612 size_t tlen;
613 ssize_t remain = (ssize_t)len;
615 *commit = got_object_commit_alloc_partial();
616 if (*commit == NULL)
617 return got_error_from_errno();
619 tlen = strlen(GOT_COMMIT_TAG_TREE);
620 if (strncmp(s, GOT_COMMIT_TAG_TREE, tlen) == 0) {
621 remain -= tlen;
622 if (remain < SHA1_DIGEST_STRING_LENGTH) {
623 err = got_error(GOT_ERR_BAD_OBJ_DATA);
624 goto done;
626 s += tlen;
627 if (!got_parse_sha1_digest((*commit)->tree_id->sha1, s)) {
628 err = got_error(GOT_ERR_BAD_OBJ_DATA);
629 goto done;
631 remain -= SHA1_DIGEST_STRING_LENGTH;
632 s += SHA1_DIGEST_STRING_LENGTH;
633 } else {
634 err = got_error(GOT_ERR_BAD_OBJ_DATA);
635 goto done;
638 tlen = strlen(GOT_COMMIT_TAG_PARENT);
639 while (strncmp(s, GOT_COMMIT_TAG_PARENT, tlen) == 0) {
640 remain -= tlen;
641 if (remain < SHA1_DIGEST_STRING_LENGTH) {
642 err = got_error(GOT_ERR_BAD_OBJ_DATA);
643 goto done;
645 s += tlen;
646 err = got_object_commit_add_parent(*commit, s);
647 if (err)
648 goto done;
650 remain -= SHA1_DIGEST_STRING_LENGTH;
651 s += SHA1_DIGEST_STRING_LENGTH;
654 tlen = strlen(GOT_COMMIT_TAG_AUTHOR);
655 if (strncmp(s, GOT_COMMIT_TAG_AUTHOR, tlen) == 0) {
656 char *p;
657 size_t slen;
659 remain -= tlen;
660 if (remain <= 0) {
661 err = got_error(GOT_ERR_BAD_OBJ_DATA);
662 goto done;
664 s += tlen;
665 p = strchr(s, '\n');
666 if (p == NULL) {
667 err = got_error(GOT_ERR_BAD_OBJ_DATA);
668 goto done;
670 *p = '\0';
671 slen = strlen(s);
672 err = parse_commit_time(&(*commit)->tm_author, s);
673 if (err)
674 goto done;
675 (*commit)->author = strdup(s);
676 if ((*commit)->author == NULL) {
677 err = got_error_from_errno();
678 goto done;
680 s += slen + 1;
681 remain -= slen + 1;
684 tlen = strlen(GOT_COMMIT_TAG_COMMITTER);
685 if (strncmp(s, GOT_COMMIT_TAG_COMMITTER, tlen) == 0) {
686 char *p;
687 size_t slen;
689 remain -= tlen;
690 if (remain <= 0) {
691 err = got_error(GOT_ERR_BAD_OBJ_DATA);
692 goto done;
694 s += tlen;
695 p = strchr(s, '\n');
696 if (p == NULL) {
697 err = got_error(GOT_ERR_BAD_OBJ_DATA);
698 goto done;
700 *p = '\0';
701 slen = strlen(s);
702 err = parse_commit_time(&(*commit)->tm_committer, s);
703 if (err)
704 goto done;
705 (*commit)->committer = strdup(s);
706 if ((*commit)->committer == NULL) {
707 err = got_error_from_errno();
708 goto done;
710 s += slen + 1;
711 remain -= slen + 1;
714 (*commit)->logmsg = strndup(s, remain);
715 if ((*commit)->logmsg == NULL) {
716 err = got_error_from_errno();
717 goto done;
719 done:
720 if (err) {
721 got_object_commit_close(*commit);
722 *commit = NULL;
724 return err;
727 static void
728 tree_entry_close(struct got_tree_entry *te)
730 free(te->id);
731 free(te->name);
732 free(te);
735 struct got_tree_entry *
736 got_alloc_tree_entry_partial(void)
738 struct got_tree_entry *te;
740 te = calloc(1, sizeof(*te));
741 if (te == NULL)
742 return NULL;
744 te->id = calloc(1, sizeof(*te->id));
745 if (te->id == NULL) {
746 free(te);
747 te = NULL;
749 return te;
752 static const struct got_error *
753 parse_tree_entry(struct got_tree_entry **te, size_t *elen, char *buf,
754 size_t maxlen)
756 char *p = buf, *space;
757 const struct got_error *err = NULL;
759 *te = got_alloc_tree_entry_partial();
760 if (*te == NULL)
761 return got_error_from_errno();
763 *elen = strlen(buf) + 1;
764 if (*elen > maxlen) {
765 free(*te);
766 *te = NULL;
767 return got_error(GOT_ERR_BAD_OBJ_DATA);
770 space = strchr(buf, ' ');
771 if (space == NULL) {
772 err = got_error(GOT_ERR_BAD_OBJ_DATA);
773 free(*te);
774 *te = NULL;
775 return err;
777 while (*p != ' ') {
778 if (*p < '0' && *p > '7') {
779 err = got_error(GOT_ERR_BAD_OBJ_DATA);
780 goto done;
782 (*te)->mode <<= 3;
783 (*te)->mode |= *p - '0';
784 p++;
787 (*te)->name = strdup(space + 1);
788 if (*elen > maxlen || maxlen - *elen < SHA1_DIGEST_LENGTH) {
789 err = got_error(GOT_ERR_BAD_OBJ_DATA);
790 goto done;
792 buf += strlen(buf) + 1;
793 memcpy((*te)->id->sha1, buf, SHA1_DIGEST_LENGTH);
794 *elen += SHA1_DIGEST_LENGTH;
795 done:
796 if (err) {
797 tree_entry_close(*te);
798 *te = NULL;
800 return err;
803 static const struct got_error *
804 parse_tree_object(struct got_tree_object **tree, uint8_t *buf, size_t len)
806 const struct got_error *err;
807 size_t remain = len;
809 *tree = calloc(1, sizeof(**tree));
810 if (*tree == NULL)
811 return got_error_from_errno();
813 SIMPLEQ_INIT(&(*tree)->entries.head);
815 while (remain > 0) {
816 struct got_tree_entry *te;
817 size_t elen;
819 err = parse_tree_entry(&te, &elen, buf, remain);
820 if (err)
821 return err;
822 (*tree)->entries.nentries++;
823 SIMPLEQ_INSERT_TAIL(&(*tree)->entries.head, te, entry);
824 buf += elen;
825 remain -= elen;
828 if (remain != 0) {
829 got_object_tree_close(*tree);
830 return got_error(GOT_ERR_BAD_OBJ_DATA);
833 return NULL;
836 static const struct got_error *
837 read_to_mem(uint8_t **outbuf, size_t *outlen, FILE *f)
839 const struct got_error *err = NULL;
840 static const size_t blocksize = 512;
841 size_t n, total, remain;
842 uint8_t *buf;
844 *outbuf = NULL;
845 *outlen = 0;
847 buf = malloc(blocksize);
848 if (buf == NULL)
849 return got_error_from_errno();
851 remain = blocksize;
852 total = 0;
853 while (1) {
854 if (remain == 0) {
855 uint8_t *newbuf;
856 newbuf = reallocarray(buf, 1, total + blocksize);
857 if (newbuf == NULL) {
858 err = got_error_from_errno();
859 goto done;
861 buf = newbuf;
862 remain += blocksize;
864 n = fread(buf + total, 1, remain, f);
865 if (n == 0) {
866 if (ferror(f)) {
867 err = got_ferror(f, GOT_ERR_IO);
868 goto done;
870 break; /* EOF */
872 remain -= n;
873 total += n;
874 };
876 done:
877 if (err == NULL) {
878 *outbuf = buf;
879 *outlen = total;
880 } else
881 free(buf);
882 return err;
885 static const struct got_error *
886 read_commit_object(struct got_commit_object **commit, struct got_object *obj,
887 FILE *f)
889 const struct got_error *err = NULL;
890 size_t len;
891 uint8_t *p;
893 if (obj->flags & GOT_OBJ_FLAG_PACKED)
894 err = read_to_mem(&p, &len, f);
895 else
896 err = got_inflate_to_mem(&p, &len, f);
897 if (err)
898 return err;
900 if (len < obj->hdrlen + obj->size) {
901 err = got_error(GOT_ERR_BAD_OBJ_DATA);
902 goto done;
905 /* Skip object header. */
906 len -= obj->hdrlen;
907 err = parse_commit_object(commit, p + obj->hdrlen, len);
908 free(p);
909 done:
910 return err;
913 static void
914 read_commit_object_privsep_child(struct got_object *obj, int obj_fd,
915 int imsg_fds[2])
917 const struct got_error *err = NULL;
918 struct got_commit_object *commit = NULL;
919 struct imsgbuf ibuf;
920 FILE *f = NULL;
921 int status = 0;
923 setproctitle("read commit object");
924 close(imsg_fds[0]);
925 imsg_init(&ibuf, imsg_fds[1]);
927 /* revoke access to most system calls */
928 if (pledge("stdio", NULL) == -1) {
929 err = got_error_from_errno();
930 goto done;
933 f = fdopen(obj_fd, "rb");
934 if (f == NULL) {
935 err = got_error_from_errno();
936 close(obj_fd);
937 goto done;
940 err = read_commit_object(&commit, obj, f);
941 if (err)
942 goto done;
944 err = got_privsep_send_commit(&ibuf, commit);
945 done:
946 if (commit)
947 got_object_commit_close(commit);
948 if (err) {
949 got_privsep_send_error(&ibuf, err);
950 status = 1;
952 if (f)
953 fclose(f);
954 imsg_clear(&ibuf);
955 close(imsg_fds[1]);
956 _exit(status);
959 static const struct got_error *
960 read_commit_object_privsep(struct got_commit_object **commit,
961 struct got_repository *repo, struct got_object *obj, int fd)
963 const struct got_error *err = NULL, *err_child = NULL;
964 struct imsgbuf parent_ibuf;
965 int imsg_fds[2];
966 pid_t pid;
968 if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_fds) == -1)
969 return got_error_from_errno();
971 pid = fork();
972 if (pid == -1)
973 return got_error_from_errno();
974 else if (pid == 0) {
975 read_commit_object_privsep_child(obj, fd, imsg_fds);
976 /* not reached */
979 close(imsg_fds[1]);
980 imsg_init(&parent_ibuf, imsg_fds[0]);
981 err = got_privsep_recv_commit(commit, &parent_ibuf);
982 imsg_clear(&parent_ibuf);
983 err_child = wait_for_child(pid);
984 close(imsg_fds[0]);
985 return err ? err : err_child;
988 const struct got_error *
989 got_object_commit_open(struct got_commit_object **commit,
990 struct got_repository *repo, struct got_object *obj)
992 const struct got_error *err = NULL;
994 *commit = got_repo_get_cached_commit(repo, &obj->id);
995 if (*commit != NULL) {
996 (*commit)->refcnt++;
997 return NULL;
1000 if (obj->type != GOT_OBJ_TYPE_COMMIT)
1001 return got_error(GOT_ERR_OBJ_TYPE);
1003 if (obj->flags & GOT_OBJ_FLAG_PACKED) {
1004 uint8_t *buf;
1005 size_t len;
1006 err = got_packfile_extract_object_to_mem(&buf, &len, obj, repo);
1007 if (err)
1008 return err;
1009 obj->size = len;
1010 err = parse_commit_object(commit, buf, len);
1011 free(buf);
1012 } else {
1013 int fd;
1014 err = open_loose_object(&fd, obj, repo);
1015 if (err)
1016 return err;
1017 err = read_commit_object_privsep(commit, repo, obj, fd);
1018 close(fd);
1021 if (err == NULL) {
1022 (*commit)->refcnt++;
1023 err = got_repo_cache_commit(repo, &obj->id, *commit);
1026 return err;
1029 void
1030 got_object_commit_close(struct got_commit_object *commit)
1032 struct got_object_qid *qid;
1034 if (commit->refcnt > 0) {
1035 commit->refcnt--;
1036 if (commit->refcnt > 0)
1037 return;
1040 while (!SIMPLEQ_EMPTY(&commit->parent_ids)) {
1041 qid = SIMPLEQ_FIRST(&commit->parent_ids);
1042 SIMPLEQ_REMOVE_HEAD(&commit->parent_ids, entry);
1043 got_object_qid_free(qid);
1046 free(commit->tree_id);
1047 free(commit->author);
1048 free(commit->committer);
1049 free(commit->logmsg);
1050 free(commit);
1053 static const struct got_error *
1054 read_tree_object(struct got_tree_object **tree, struct got_object *obj, FILE *f)
1056 const struct got_error *err = NULL;
1057 size_t len;
1058 uint8_t *p;
1060 if (obj->flags & GOT_OBJ_FLAG_PACKED)
1061 err = read_to_mem(&p, &len, f);
1062 else
1063 err = got_inflate_to_mem(&p, &len, f);
1064 if (err)
1065 return err;
1067 if (len < obj->hdrlen + obj->size) {
1068 err = got_error(GOT_ERR_BAD_OBJ_DATA);
1069 goto done;
1072 /* Skip object header. */
1073 len -= obj->hdrlen;
1074 err = parse_tree_object(tree, p + obj->hdrlen, len);
1075 free(p);
1076 done:
1077 return err;
1080 static void
1081 read_tree_object_privsep_child(struct got_object *obj, int obj_fd,
1082 int imsg_fds[2])
1084 const struct got_error *err = NULL;
1085 struct got_tree_object *tree = NULL;
1086 struct imsgbuf ibuf;
1087 FILE *f = NULL;
1088 int status = 0;
1090 setproctitle("read tree object");
1091 close(imsg_fds[0]);
1092 imsg_init(&ibuf, imsg_fds[1]);
1094 /* revoke access to most system calls */
1095 if (pledge("stdio", NULL) == -1) {
1096 err = got_error_from_errno();
1097 goto done;
1100 f = fdopen(obj_fd, "rb");
1101 if (f == NULL) {
1102 err = got_error_from_errno();
1103 close(obj_fd);
1104 goto done;
1107 err = read_tree_object(&tree, obj, f);
1108 if (err)
1109 goto done;
1111 err = got_privsep_send_tree(&ibuf, tree);
1112 done:
1113 if (tree)
1114 got_object_tree_close(tree);
1115 if (err) {
1116 got_privsep_send_error(&ibuf, err);
1117 status = 1;
1119 if (f)
1120 fclose(f);
1121 imsg_clear(&ibuf);
1122 close(imsg_fds[1]);
1123 _exit(status);
1126 static const struct got_error *
1127 read_tree_object_privsep(struct got_tree_object **tree, struct got_object *obj,
1128 int fd)
1130 const struct got_error *err = NULL, *err_child = NULL;
1131 struct imsgbuf parent_ibuf;
1132 int imsg_fds[2];
1133 pid_t pid;
1135 if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_fds) == -1)
1136 return got_error_from_errno();
1138 pid = fork();
1139 if (pid == -1)
1140 return got_error_from_errno();
1141 else if (pid == 0) {
1142 read_tree_object_privsep_child(obj, fd, imsg_fds);
1143 /* not reached */
1146 close(imsg_fds[1]);
1147 imsg_init(&parent_ibuf, imsg_fds[0]);
1148 err = got_privsep_recv_tree(tree, &parent_ibuf);
1149 imsg_clear(&parent_ibuf);
1150 err_child = wait_for_child(pid);
1151 close(imsg_fds[0]);
1152 return err ? err : err_child;
1155 const struct got_error *
1156 got_object_tree_open(struct got_tree_object **tree,
1157 struct got_repository *repo, struct got_object *obj)
1159 const struct got_error *err = NULL;
1161 *tree = got_repo_get_cached_tree(repo, &obj->id);
1162 if (*tree != NULL) {
1163 (*tree)->refcnt++;
1164 return NULL;
1167 if (obj->type != GOT_OBJ_TYPE_TREE)
1168 return got_error(GOT_ERR_OBJ_TYPE);
1170 if (obj->flags & GOT_OBJ_FLAG_PACKED) {
1171 uint8_t *buf;
1172 size_t len;
1173 err = got_packfile_extract_object_to_mem(&buf, &len, obj, repo);
1174 if (err)
1175 return err;
1176 obj->size = len;
1177 err = parse_tree_object(tree, buf, len);
1178 free(buf);
1179 } else {
1180 int fd;
1181 err = open_loose_object(&fd, obj, repo);
1182 if (err)
1183 return err;
1184 err = read_tree_object_privsep(tree, obj, fd);
1185 close(fd);
1188 if (err == NULL) {
1189 (*tree)->refcnt++;
1190 err = got_repo_cache_tree(repo, &obj->id, *tree);
1193 return err;
1196 const struct got_error *
1197 got_object_open_as_tree(struct got_tree_object **tree,
1198 struct got_repository *repo, struct got_object_id *id)
1200 const struct got_error *err;
1201 struct got_object *obj;
1203 *tree = NULL;
1205 err = got_object_open(&obj, repo, id);
1206 if (err)
1207 return err;
1208 if (got_object_get_type(obj) != GOT_OBJ_TYPE_TREE) {
1209 err = got_error(GOT_ERR_OBJ_TYPE);
1210 goto done;
1213 err = got_object_tree_open(tree, repo, obj);
1214 done:
1215 got_object_close(obj);
1216 return err;
1219 void
1220 got_object_tree_close(struct got_tree_object *tree)
1222 struct got_tree_entry *te;
1224 if (tree->refcnt > 0) {
1225 tree->refcnt--;
1226 if (tree->refcnt > 0)
1227 return;
1230 while (!SIMPLEQ_EMPTY(&tree->entries.head)) {
1231 te = SIMPLEQ_FIRST(&tree->entries.head);
1232 SIMPLEQ_REMOVE_HEAD(&tree->entries.head, entry);
1233 tree_entry_close(te);
1236 free(tree);
1239 const struct got_tree_entries *
1240 got_object_tree_get_entries(struct got_tree_object *tree)
1242 return &tree->entries;
1245 static const struct got_error *
1246 read_blob_object_privsep_child(int outfd, int infd, int imsg_fds[2])
1248 const struct got_error *err = NULL;
1249 struct imsgbuf ibuf;
1250 int status = 0;
1251 size_t size;
1252 FILE *infile = NULL;
1254 setproctitle("read blob object");
1255 close(imsg_fds[0]);
1256 imsg_init(&ibuf, imsg_fds[1]);
1258 /* revoke access to most system calls */
1259 if (pledge("stdio", NULL) == -1) {
1260 err = got_error_from_errno();
1261 goto done;
1264 infile = fdopen(infd, "rb");
1265 if (infile == NULL) {
1266 err = got_error_from_errno();
1267 close(infd);
1268 goto done;
1270 err = got_inflate_to_fd(&size, infile, outfd);
1271 fclose(infile);
1272 if (err)
1273 goto done;
1275 err = got_privsep_send_blob(&ibuf, size);
1276 done:
1277 if (err) {
1278 got_privsep_send_error(&ibuf, err);
1279 status = 1;
1281 close(outfd);
1282 imsg_clear(&ibuf);
1283 close(imsg_fds[1]);
1284 _exit(status);
1287 static const struct got_error *
1288 read_blob_object_privsep(size_t *size, int outfd, int infd)
1290 struct imsgbuf parent_ibuf;
1291 int imsg_fds[2];
1292 const struct got_error *err = NULL, *err_child = NULL;
1293 pid_t pid;
1295 if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_fds) == -1)
1296 return got_error_from_errno();
1298 pid = fork();
1299 if (pid == -1)
1300 return got_error_from_errno();
1301 else if (pid == 0) {
1302 read_blob_object_privsep_child(outfd, infd, imsg_fds);
1303 /* not reached */
1306 close(imsg_fds[1]);
1307 imsg_init(&parent_ibuf, imsg_fds[0]);
1308 err = got_privsep_recv_blob(size, &parent_ibuf);
1309 imsg_clear(&parent_ibuf);
1310 err_child = wait_for_child(pid);
1311 close(imsg_fds[0]);
1312 if (lseek(outfd, SEEK_SET, 0) == -1)
1313 err = got_error_from_errno();
1314 return err ? err : err_child;
1317 const struct got_error *
1318 got_object_blob_open(struct got_blob_object **blob,
1319 struct got_repository *repo, struct got_object *obj, size_t blocksize)
1321 const struct got_error *err = NULL;
1323 if (obj->type != GOT_OBJ_TYPE_BLOB)
1324 return got_error(GOT_ERR_OBJ_TYPE);
1326 if (blocksize < obj->hdrlen)
1327 return got_error(GOT_ERR_NO_SPACE);
1329 *blob = calloc(1, sizeof(**blob));
1330 if (*blob == NULL)
1331 return got_error_from_errno();
1333 (*blob)->read_buf = malloc(blocksize);
1334 if ((*blob)->read_buf == NULL) {
1335 err = got_error_from_errno();
1336 goto done;
1338 if (obj->flags & GOT_OBJ_FLAG_PACKED) {
1339 err = got_packfile_extract_object(&((*blob)->f), obj, repo);
1340 if (err)
1341 goto done;
1342 } else {
1343 int infd, outfd;
1344 size_t size;
1345 struct stat sb;
1347 err = open_loose_object(&infd, obj, repo);
1348 if (err)
1349 goto done;
1352 outfd = got_opentempfd();
1353 if (outfd == -1) {
1354 err = got_error_from_errno();
1355 close(infd);
1356 goto done;
1359 err = read_blob_object_privsep(&size, outfd, infd);
1360 close(infd);
1361 if (err)
1362 goto done;
1364 if (size != obj->hdrlen + obj->size) {
1365 err = got_error(GOT_ERR_PRIVSEP_LEN);
1366 close(outfd);
1367 goto done;
1370 if (fstat(outfd, &sb) == -1) {
1371 err = got_error_from_errno();
1372 close(outfd);
1373 goto done;
1376 if (sb.st_size != size) {
1377 err = got_error(GOT_ERR_PRIVSEP_LEN);
1378 close(outfd);
1379 goto done;
1382 (*blob)->f = fdopen(outfd, "rb");
1383 if ((*blob)->f == NULL) {
1384 err = got_error_from_errno();
1385 close(outfd);
1386 goto done;
1390 (*blob)->hdrlen = obj->hdrlen;
1391 (*blob)->blocksize = blocksize;
1392 memcpy(&(*blob)->id.sha1, obj->id.sha1, SHA1_DIGEST_LENGTH);
1394 done:
1395 if (err && *blob) {
1396 if ((*blob)->f)
1397 fclose((*blob)->f);
1398 free((*blob)->read_buf);
1399 free(*blob);
1400 *blob = NULL;
1402 return err;
1405 const struct got_error *
1406 got_object_open_as_blob(struct got_blob_object **blob,
1407 struct got_repository *repo, struct got_object_id *id,
1408 size_t blocksize)
1410 const struct got_error *err;
1411 struct got_object *obj;
1413 *blob = NULL;
1415 err = got_object_open(&obj, repo, id);
1416 if (err)
1417 return err;
1418 if (got_object_get_type(obj) != GOT_OBJ_TYPE_BLOB) {
1419 err = got_error(GOT_ERR_OBJ_TYPE);
1420 goto done;
1423 err = got_object_blob_open(blob, repo, obj, blocksize);
1424 done:
1425 got_object_close(obj);
1426 return err;
1429 void
1430 got_object_blob_close(struct got_blob_object *blob)
1432 free(blob->read_buf);
1433 fclose(blob->f);
1434 free(blob);
1437 char *
1438 got_object_blob_id_str(struct got_blob_object *blob, char *buf, size_t size)
1440 return got_sha1_digest_to_str(blob->id.sha1, buf, size);
1443 size_t
1444 got_object_blob_get_hdrlen(struct got_blob_object *blob)
1446 return blob->hdrlen;
1449 const uint8_t *
1450 got_object_blob_get_read_buf(struct got_blob_object *blob)
1452 return blob->read_buf;
1455 const struct got_error *
1456 got_object_blob_read_block(size_t *outlenp, struct got_blob_object *blob)
1458 size_t n;
1460 n = fread(blob->read_buf, 1, blob->blocksize, blob->f);
1461 if (n == 0 && ferror(blob->f))
1462 return got_ferror(blob->f, GOT_ERR_IO);
1463 *outlenp = n;
1464 return NULL;
1467 const struct got_error *
1468 got_object_blob_dump_to_file(size_t *total_len, size_t *nlines,
1469 FILE *outfile, struct got_blob_object *blob)
1471 const struct got_error *err = NULL;
1472 size_t len, hdrlen;
1473 const uint8_t *buf;
1474 int i;
1476 if (total_len)
1477 *total_len = 0;
1478 if (nlines)
1479 *nlines = 0;
1481 hdrlen = got_object_blob_get_hdrlen(blob);
1482 do {
1483 err = got_object_blob_read_block(&len, blob);
1484 if (err)
1485 return err;
1486 if (len == 0)
1487 break;
1488 if (total_len)
1489 *total_len += len;
1490 buf = got_object_blob_get_read_buf(blob);
1491 if (nlines) {
1492 for (i = 0; i < len; i++) {
1493 if (buf[i] == '\n')
1494 (*nlines)++;
1497 /* Skip blob object header first time around. */
1498 fwrite(buf + hdrlen, len - hdrlen, 1, outfile);
1499 hdrlen = 0;
1500 } while (len != 0);
1502 fflush(outfile);
1503 rewind(outfile);
1505 return NULL;
1508 static struct got_tree_entry *
1509 find_entry_by_name(struct got_tree_object *tree, const char *name)
1511 struct got_tree_entry *te;
1513 SIMPLEQ_FOREACH(te, &tree->entries.head, entry) {
1514 if (strcmp(te->name, name) == 0)
1515 return te;
1517 return NULL;
1520 const struct got_error *
1521 got_object_open_by_path(struct got_object **obj, struct got_repository *repo,
1522 struct got_object_id *commit_id, const char *path)
1524 const struct got_error *err = NULL;
1525 struct got_commit_object *commit = NULL;
1526 struct got_tree_object *tree = NULL;
1527 struct got_tree_entry *te = NULL;
1528 char *seg, *s, *s0 = NULL;
1529 size_t len = strlen(path);
1531 *obj = NULL;
1533 /* We are expecting an absolute in-repository path. */
1534 if (path[0] != '/')
1535 return got_error(GOT_ERR_NOT_ABSPATH);
1537 err = got_object_open_as_commit(&commit, repo, commit_id);
1538 if (err)
1539 goto done;
1541 /* Handle opening of root of commit's tree. */
1542 if (path[1] == '\0') {
1543 err = got_object_open(obj, repo, commit->tree_id);
1544 goto done;
1547 err = got_object_open_as_tree(&tree, repo, commit->tree_id);
1548 if (err)
1549 goto done;
1551 s0 = strdup(path);
1552 if (s0 == NULL) {
1553 err = got_error_from_errno();
1554 goto done;
1556 err = got_canonpath(path, s0, len + 1);
1557 if (err)
1558 goto done;
1560 s = s0;
1561 s++; /* skip leading '/' */
1562 len--;
1563 seg = s;
1564 while (len > 0) {
1565 struct got_tree_object *next_tree;
1567 if (*s != '/') {
1568 s++;
1569 len--;
1570 if (*s)
1571 continue;
1574 /* end of path segment */
1575 *s = '\0';
1577 te = find_entry_by_name(tree, seg);
1578 if (te == NULL) {
1579 err = got_error(GOT_ERR_NO_OBJ);
1580 goto done;
1583 if (len == 0)
1584 break;
1586 seg = s + 1;
1587 s++;
1588 len--;
1589 if (*s) {
1590 err = got_object_open_as_tree(&next_tree, repo,
1591 te->id);
1592 te = NULL;
1593 if (err)
1594 goto done;
1595 got_object_tree_close(tree);
1596 tree = next_tree;
1600 if (te)
1601 err = got_object_open(obj, repo, te->id);
1602 else
1603 err = got_error(GOT_ERR_NO_OBJ);
1604 done:
1605 free(s0);
1606 if (commit)
1607 got_object_commit_close(commit);
1608 if (tree)
1609 got_object_tree_close(tree);
1610 return err;