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;
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 return err;
324 if (asprintf(path, "%s/%.2x/%s", path_objects,
325 id->sha1[0], hex + 2) == -1)
326 err = got_error_from_errno();
328 free(hex);
329 free(path_objects);
330 return err;
333 static const struct got_error *
334 open_loose_object(int *fd, struct got_object *obj, struct got_repository *repo)
336 const struct got_error *err = NULL;
337 char *path;
339 err = object_path(&path, &obj->id, repo);
340 if (err)
341 return err;
342 *fd = open(path, O_RDONLY | O_NOFOLLOW, GOT_DEFAULT_FILE_MODE);
343 if (*fd == -1) {
344 err = got_error_from_errno();
345 goto done;
347 done:
348 free(path);
349 return err;
352 const struct got_error *
353 got_object_open(struct got_object **obj, struct got_repository *repo,
354 struct got_object_id *id)
356 const struct got_error *err = NULL;
357 char *path;
358 int fd;
360 *obj = got_repo_get_cached_object(repo, id);
361 if (*obj != NULL) {
362 (*obj)->refcnt++;
363 return NULL;
366 err = object_path(&path, id, repo);
367 if (err)
368 return err;
370 fd = open(path, O_RDONLY | O_NOFOLLOW, GOT_DEFAULT_FILE_MODE);
371 if (fd == -1) {
372 if (errno != ENOENT) {
373 err = got_error_from_errno();
374 goto done;
376 err = got_packfile_open_object(obj, id, repo);
377 if (err)
378 goto done;
379 if (*obj == NULL)
380 err = got_error(GOT_ERR_NO_OBJ);
381 } else {
382 err = read_object_header_privsep(obj, fd);
383 if (err)
384 goto done;
385 memcpy((*obj)->id.sha1, id->sha1, SHA1_DIGEST_LENGTH);
388 if (err == NULL) {
389 (*obj)->refcnt++;
390 err = got_repo_cache_object(repo, id, *obj);
392 done:
393 free(path);
394 if (fd != -1)
395 close(fd);
396 return err;
400 const struct got_error *
401 got_object_open_by_id_str(struct got_object **obj, struct got_repository *repo,
402 const char *id_str)
404 struct got_object_id id;
406 if (!got_parse_sha1_digest(id.sha1, id_str))
407 return got_error(GOT_ERR_BAD_OBJ_ID_STR);
409 return got_object_open(obj, repo, &id);
412 void
413 got_object_close(struct got_object *obj)
415 if (obj->refcnt > 0) {
416 obj->refcnt--;
417 if (obj->refcnt > 0)
418 return;
421 if (obj->flags & GOT_OBJ_FLAG_DELTIFIED) {
422 struct got_delta *delta;
423 while (!SIMPLEQ_EMPTY(&obj->deltas.entries)) {
424 delta = SIMPLEQ_FIRST(&obj->deltas.entries);
425 SIMPLEQ_REMOVE_HEAD(&obj->deltas.entries, entry);
426 got_delta_close(delta);
429 if (obj->flags & GOT_OBJ_FLAG_PACKED)
430 free(obj->path_packfile);
431 free(obj);
434 struct got_commit_object *
435 got_object_commit_alloc_partial(void)
437 struct got_commit_object *commit;
439 commit = calloc(1, sizeof(*commit));
440 if (commit == NULL)
441 return NULL;
442 commit->tree_id = calloc(1, sizeof(*commit->tree_id));
443 if (commit->tree_id == NULL) {
444 free(commit);
445 return NULL;
448 SIMPLEQ_INIT(&commit->parent_ids);
450 return commit;
453 const struct got_error *
454 got_object_open_as_commit(struct got_commit_object **commit,
455 struct got_repository *repo, struct got_object_id *id)
457 const struct got_error *err;
458 struct got_object *obj;
460 *commit = NULL;
462 err = got_object_open(&obj, repo, id);
463 if (err)
464 return err;
465 if (got_object_get_type(obj) != GOT_OBJ_TYPE_COMMIT) {
466 err = got_error(GOT_ERR_OBJ_TYPE);
467 goto done;
470 err = got_object_commit_open(commit, repo, obj);
471 done:
472 got_object_close(obj);
473 return err;
476 const struct got_error *
477 got_object_qid_alloc(struct got_object_qid **qid, struct got_object_id *id)
479 const struct got_error *err = NULL;
481 *qid = calloc(1, sizeof(**qid));
482 if (*qid == NULL)
483 return got_error_from_errno();
485 (*qid)->id = got_object_id_dup(id);
486 if ((*qid)->id == NULL) {
487 err = got_error_from_errno();
488 free(*qid);
489 *qid = NULL;
490 return err;
493 return NULL;
496 void
497 got_object_qid_free(struct got_object_qid *qid)
499 free(qid->id);
500 free(qid);
503 const struct got_error *
504 got_object_commit_add_parent(struct got_commit_object *commit,
505 const char *id_str)
507 const struct got_error *err = NULL;
508 struct got_object_qid *qid;
510 qid = malloc(sizeof(*qid));
511 if (qid == NULL)
512 return got_error_from_errno();
514 qid->id = malloc(sizeof(*qid->id));
515 if (qid->id == NULL) {
516 err = got_error_from_errno();
517 free(qid);
518 return err;
521 if (!got_parse_sha1_digest(qid->id->sha1, id_str)) {
522 err = got_error(GOT_ERR_BAD_OBJ_DATA);
523 free(qid->id);
524 free(qid);
525 return err;
528 SIMPLEQ_INSERT_TAIL(&commit->parent_ids, qid, entry);
529 commit->nparents++;
531 return NULL;
534 static const struct got_error *
535 parse_gmtoff(time_t *gmtoff, const char *tzstr)
537 int sign = 1;
538 const char *p = tzstr;
539 time_t h, m;
541 *gmtoff = 0;
543 if (*p == '-')
544 sign = -1;
545 else if (*p != '+')
546 return got_error(GOT_ERR_BAD_OBJ_DATA);
547 p++;
548 if (!isdigit(*p) && !isdigit(*(p + 1)))
549 return got_error(GOT_ERR_BAD_OBJ_DATA);
550 h = (((*p - '0') * 10) + (*(p + 1) - '0'));
552 p += 2;
553 if (!isdigit(*p) && !isdigit(*(p + 1)))
554 return got_error(GOT_ERR_BAD_OBJ_DATA);
555 m = ((*p - '0') * 10) + (*(p + 1) - '0');
557 *gmtoff = (h * 60 * 60 + m * 60) * sign;
558 return NULL;
561 static const struct got_error *
562 parse_commit_time(struct tm *tm, char *committer)
564 const struct got_error *err = NULL;
565 const char *errstr;
566 char *space, *tzstr;
567 time_t gmtoff;
568 time_t time;
570 /* Parse and strip off trailing timezone indicator string. */
571 space = strrchr(committer, ' ');
572 if (space == NULL)
573 return got_error(GOT_ERR_BAD_OBJ_DATA);
574 tzstr = strdup(space + 1);
575 if (tzstr == NULL)
576 return got_error_from_errno();
577 err = parse_gmtoff(&gmtoff, tzstr);
578 free(tzstr);
579 if (err)
580 return err;
581 *space = '\0';
583 /* Timestamp is separated from committer name + email by space. */
584 space = strrchr(committer, ' ');
585 if (space == NULL)
586 return got_error(GOT_ERR_BAD_OBJ_DATA);
588 /* Timestamp parsed here is expressed in comitter's local time. */
589 time = strtonum(space + 1, 0, INT64_MAX, &errstr);
590 if (errstr)
591 return got_error(GOT_ERR_BAD_OBJ_DATA);
593 /* Express the time stamp in UTC. */
594 memset(tm, 0, sizeof(*tm));
595 time -= gmtoff;
596 if (localtime_r(&time, tm) == NULL)
597 return got_error_from_errno();
598 tm->tm_gmtoff = gmtoff;
600 /* Strip off parsed time information, leaving just author and email. */
601 *space = '\0';
603 return NULL;
606 static const struct got_error *
607 parse_commit_object(struct got_commit_object **commit, char *buf, size_t len)
609 const struct got_error *err = NULL;
610 char *s = buf;
611 size_t tlen;
612 ssize_t remain = (ssize_t)len;
614 *commit = got_object_commit_alloc_partial();
615 if (*commit == NULL)
616 return got_error_from_errno();
618 tlen = strlen(GOT_COMMIT_TAG_TREE);
619 if (strncmp(s, GOT_COMMIT_TAG_TREE, tlen) == 0) {
620 remain -= tlen;
621 if (remain < SHA1_DIGEST_STRING_LENGTH) {
622 err = got_error(GOT_ERR_BAD_OBJ_DATA);
623 goto done;
625 s += tlen;
626 if (!got_parse_sha1_digest((*commit)->tree_id->sha1, s)) {
627 err = got_error(GOT_ERR_BAD_OBJ_DATA);
628 goto done;
630 remain -= SHA1_DIGEST_STRING_LENGTH;
631 s += SHA1_DIGEST_STRING_LENGTH;
632 } else {
633 err = got_error(GOT_ERR_BAD_OBJ_DATA);
634 goto done;
637 tlen = strlen(GOT_COMMIT_TAG_PARENT);
638 while (strncmp(s, GOT_COMMIT_TAG_PARENT, tlen) == 0) {
639 remain -= tlen;
640 if (remain < SHA1_DIGEST_STRING_LENGTH) {
641 err = got_error(GOT_ERR_BAD_OBJ_DATA);
642 goto done;
644 s += tlen;
645 err = got_object_commit_add_parent(*commit, s);
646 if (err)
647 goto done;
649 remain -= SHA1_DIGEST_STRING_LENGTH;
650 s += SHA1_DIGEST_STRING_LENGTH;
653 tlen = strlen(GOT_COMMIT_TAG_AUTHOR);
654 if (strncmp(s, GOT_COMMIT_TAG_AUTHOR, tlen) == 0) {
655 char *p;
656 size_t slen;
658 remain -= tlen;
659 if (remain <= 0) {
660 err = got_error(GOT_ERR_BAD_OBJ_DATA);
661 goto done;
663 s += tlen;
664 p = strchr(s, '\n');
665 if (p == NULL) {
666 err = got_error(GOT_ERR_BAD_OBJ_DATA);
667 goto done;
669 *p = '\0';
670 slen = strlen(s);
671 err = parse_commit_time(&(*commit)->tm_author, s);
672 if (err)
673 goto done;
674 (*commit)->author = strdup(s);
675 if ((*commit)->author == NULL) {
676 err = got_error_from_errno();
677 goto done;
679 s += slen + 1;
680 remain -= slen + 1;
683 tlen = strlen(GOT_COMMIT_TAG_COMMITTER);
684 if (strncmp(s, GOT_COMMIT_TAG_COMMITTER, tlen) == 0) {
685 char *p;
686 size_t slen;
688 remain -= tlen;
689 if (remain <= 0) {
690 err = got_error(GOT_ERR_BAD_OBJ_DATA);
691 goto done;
693 s += tlen;
694 p = strchr(s, '\n');
695 if (p == NULL) {
696 err = got_error(GOT_ERR_BAD_OBJ_DATA);
697 goto done;
699 *p = '\0';
700 slen = strlen(s);
701 err = parse_commit_time(&(*commit)->tm_committer, s);
702 if (err)
703 goto done;
704 (*commit)->committer = strdup(s);
705 if ((*commit)->committer == NULL) {
706 err = got_error_from_errno();
707 goto done;
709 s += slen + 1;
710 remain -= slen + 1;
713 (*commit)->logmsg = strndup(s, remain);
714 if ((*commit)->logmsg == NULL) {
715 err = got_error_from_errno();
716 goto done;
718 done:
719 if (err) {
720 got_object_commit_close(*commit);
721 *commit = NULL;
723 return err;
726 static void
727 tree_entry_close(struct got_tree_entry *te)
729 free(te->id);
730 free(te->name);
731 free(te);
734 struct got_tree_entry *
735 got_alloc_tree_entry_partial(void)
737 struct got_tree_entry *te;
739 te = calloc(1, sizeof(*te));
740 if (te == NULL)
741 return NULL;
743 te->id = calloc(1, sizeof(*te->id));
744 if (te->id == NULL) {
745 free(te);
746 te = NULL;
748 return te;
751 static const struct got_error *
752 parse_tree_entry(struct got_tree_entry **te, size_t *elen, char *buf,
753 size_t maxlen)
755 char *p = buf, *space;
756 const struct got_error *err = NULL;
758 *te = got_alloc_tree_entry_partial();
759 if (*te == NULL)
760 return got_error_from_errno();
762 *elen = strlen(buf) + 1;
763 if (*elen > maxlen) {
764 free(*te);
765 *te = NULL;
766 return got_error(GOT_ERR_BAD_OBJ_DATA);
769 space = strchr(buf, ' ');
770 if (space == NULL) {
771 err = got_error(GOT_ERR_BAD_OBJ_DATA);
772 free(*te);
773 *te = NULL;
774 return err;
776 while (*p != ' ') {
777 if (*p < '0' && *p > '7') {
778 err = got_error(GOT_ERR_BAD_OBJ_DATA);
779 goto done;
781 (*te)->mode <<= 3;
782 (*te)->mode |= *p - '0';
783 p++;
786 (*te)->name = strdup(space + 1);
787 if (*elen > maxlen || maxlen - *elen < SHA1_DIGEST_LENGTH) {
788 err = got_error(GOT_ERR_BAD_OBJ_DATA);
789 goto done;
791 buf += strlen(buf) + 1;
792 memcpy((*te)->id->sha1, buf, SHA1_DIGEST_LENGTH);
793 *elen += SHA1_DIGEST_LENGTH;
794 done:
795 if (err) {
796 tree_entry_close(*te);
797 *te = NULL;
799 return err;
802 static const struct got_error *
803 parse_tree_object(struct got_tree_object **tree, uint8_t *buf, size_t len)
805 const struct got_error *err;
806 size_t remain = len;
808 *tree = calloc(1, sizeof(**tree));
809 if (*tree == NULL)
810 return got_error_from_errno();
812 SIMPLEQ_INIT(&(*tree)->entries.head);
814 while (remain > 0) {
815 struct got_tree_entry *te;
816 size_t elen;
818 err = parse_tree_entry(&te, &elen, buf, remain);
819 if (err)
820 return err;
821 (*tree)->entries.nentries++;
822 SIMPLEQ_INSERT_TAIL(&(*tree)->entries.head, te, entry);
823 buf += elen;
824 remain -= elen;
827 if (remain != 0) {
828 got_object_tree_close(*tree);
829 return got_error(GOT_ERR_BAD_OBJ_DATA);
832 return NULL;
835 static const struct got_error *
836 read_to_mem(uint8_t **outbuf, size_t *outlen, FILE *f)
838 const struct got_error *err = NULL;
839 static const size_t blocksize = 512;
840 size_t n, total, remain;
841 uint8_t *buf;
843 *outbuf = NULL;
844 *outlen = 0;
846 buf = malloc(blocksize);
847 if (buf == NULL)
848 return got_error_from_errno();
850 remain = blocksize;
851 total = 0;
852 while (1) {
853 if (remain == 0) {
854 uint8_t *newbuf;
855 newbuf = reallocarray(buf, 1, total + blocksize);
856 if (newbuf == NULL) {
857 err = got_error_from_errno();
858 goto done;
860 buf = newbuf;
861 remain += blocksize;
863 n = fread(buf + total, 1, remain, f);
864 if (n == 0) {
865 if (ferror(f)) {
866 err = got_ferror(f, GOT_ERR_IO);
867 goto done;
869 break; /* EOF */
871 remain -= n;
872 total += n;
873 };
875 done:
876 if (err == NULL) {
877 *outbuf = buf;
878 *outlen = total;
879 } else
880 free(buf);
881 return err;
884 static const struct got_error *
885 read_commit_object(struct got_commit_object **commit, struct got_object *obj,
886 FILE *f)
888 const struct got_error *err = NULL;
889 size_t len;
890 uint8_t *p;
892 if (obj->flags & GOT_OBJ_FLAG_PACKED)
893 err = read_to_mem(&p, &len, f);
894 else
895 err = got_inflate_to_mem(&p, &len, f);
896 if (err)
897 return err;
899 if (len < obj->hdrlen + obj->size) {
900 err = got_error(GOT_ERR_BAD_OBJ_DATA);
901 goto done;
904 /* Skip object header. */
905 len -= obj->hdrlen;
906 err = parse_commit_object(commit, p + obj->hdrlen, len);
907 free(p);
908 done:
909 return err;
912 static void
913 read_commit_object_privsep_child(struct got_object *obj, int obj_fd,
914 int imsg_fds[2])
916 const struct got_error *err = NULL;
917 struct got_commit_object *commit = NULL;
918 struct imsgbuf ibuf;
919 FILE *f = NULL;
920 int status = 0;
922 setproctitle("read commit object");
923 close(imsg_fds[0]);
924 imsg_init(&ibuf, imsg_fds[1]);
926 /* revoke access to most system calls */
927 if (pledge("stdio", NULL) == -1) {
928 err = got_error_from_errno();
929 goto done;
932 f = fdopen(obj_fd, "rb");
933 if (f == NULL) {
934 err = got_error_from_errno();
935 close(obj_fd);
936 goto done;
939 err = read_commit_object(&commit, obj, f);
940 if (err)
941 goto done;
943 err = got_privsep_send_commit(&ibuf, commit);
944 done:
945 if (commit)
946 got_object_commit_close(commit);
947 if (err) {
948 got_privsep_send_error(&ibuf, err);
949 status = 1;
951 if (f)
952 fclose(f);
953 imsg_clear(&ibuf);
954 close(imsg_fds[1]);
955 _exit(status);
958 static const struct got_error *
959 read_commit_object_privsep(struct got_commit_object **commit,
960 struct got_repository *repo, struct got_object *obj, int fd)
962 const struct got_error *err = NULL, *err_child = NULL;
963 struct imsgbuf parent_ibuf;
964 int imsg_fds[2];
965 pid_t pid;
967 if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_fds) == -1)
968 return got_error_from_errno();
970 pid = fork();
971 if (pid == -1)
972 return got_error_from_errno();
973 else if (pid == 0) {
974 read_commit_object_privsep_child(obj, fd, imsg_fds);
975 /* not reached */
978 close(imsg_fds[1]);
979 imsg_init(&parent_ibuf, imsg_fds[0]);
980 err = got_privsep_recv_commit(commit, &parent_ibuf);
981 imsg_clear(&parent_ibuf);
982 err_child = wait_for_child(pid);
983 close(imsg_fds[0]);
984 return err ? err : err_child;
987 const struct got_error *
988 got_object_commit_open(struct got_commit_object **commit,
989 struct got_repository *repo, struct got_object *obj)
991 const struct got_error *err = NULL;
993 *commit = got_repo_get_cached_commit(repo, &obj->id);
994 if (*commit != NULL) {
995 (*commit)->refcnt++;
996 return NULL;
999 if (obj->type != GOT_OBJ_TYPE_COMMIT)
1000 return got_error(GOT_ERR_OBJ_TYPE);
1002 if (obj->flags & GOT_OBJ_FLAG_PACKED) {
1003 uint8_t *buf;
1004 size_t len;
1005 err = got_packfile_extract_object_to_mem(&buf, &len, obj, repo);
1006 if (err)
1007 return err;
1008 obj->size = len;
1009 err = parse_commit_object(commit, buf, len);
1010 free(buf);
1011 } else {
1012 int fd;
1013 err = open_loose_object(&fd, obj, repo);
1014 if (err)
1015 return err;
1016 err = read_commit_object_privsep(commit, repo, obj, fd);
1017 close(fd);
1020 if (err == NULL) {
1021 (*commit)->refcnt++;
1022 err = got_repo_cache_commit(repo, &obj->id, *commit);
1025 return err;
1028 void
1029 got_object_commit_close(struct got_commit_object *commit)
1031 struct got_object_qid *qid;
1033 if (commit->refcnt > 0) {
1034 commit->refcnt--;
1035 if (commit->refcnt > 0)
1036 return;
1039 while (!SIMPLEQ_EMPTY(&commit->parent_ids)) {
1040 qid = SIMPLEQ_FIRST(&commit->parent_ids);
1041 SIMPLEQ_REMOVE_HEAD(&commit->parent_ids, entry);
1042 free(qid->id);
1043 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;