Blob


1 /*
2 * Copyright (c) 2018 Stefan Sperling <stsp@openbsd.org>
3 *
4 * Permission to use, copy, modify, and distribute this software for any
5 * purpose with or without fee is hereby granted, provided that the above
6 * copyright notice and this permission notice appear in all copies.
7 *
8 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
15 */
17 #include <sys/types.h>
18 #include <sys/stat.h>
19 #include <sys/queue.h>
20 #include <sys/uio.h>
21 #include <sys/socket.h>
22 #include <sys/wait.h>
24 #include <errno.h>
25 #include <fcntl.h>
26 #include <stdio.h>
27 #include <stdlib.h>
28 #include <string.h>
29 #include <stdint.h>
30 #include <sha1.h>
31 #include <zlib.h>
32 #include <ctype.h>
33 #include <limits.h>
34 #include <imsg.h>
35 #include <time.h>
37 #include "got_error.h"
38 #include "got_object.h"
39 #include "got_repository.h"
40 #include "got_opentemp.h"
42 #include "got_lib_sha1.h"
43 #include "got_lib_delta.h"
44 #include "got_lib_pack.h"
45 #include "got_lib_path.h"
46 #include "got_lib_zbuf.h"
47 #include "got_lib_object.h"
48 #include "got_lib_privsep.h"
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 = calloc(1, 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, FILE *f)
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 = calloc(zbsize, sizeof(char));
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(&zb, f, &outlen);
208 if (err)
209 goto done;
210 if (strchr(zb.outbuf, '\0') == NULL) {
211 buf = recallocarray(buf, 1 + i, 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 FILE *f = NULL;
235 int status = 0;
237 setproctitle("read object header");
238 close(imsg_fds[0]);
239 imsg_init(&ibuf, imsg_fds[1]);
241 /* revoke access to most system calls */
242 if (pledge("stdio", NULL) == -1) {
243 err = got_error_from_errno();
244 goto done;
247 f = fdopen(obj_fd, "rb");
248 if (f == NULL) {
249 err = got_error_from_errno();
250 close(obj_fd);
251 goto done;
254 err = read_object_header(&obj, f);
255 if (err)
256 goto done;
258 err = got_privsep_send_obj(&ibuf, obj, 0);
259 done:
260 if (obj)
261 got_object_close(obj);
262 if (err) {
263 got_privsep_send_error(&ibuf, err);
264 status = 1;
266 if (f)
267 fclose(f);
268 imsg_clear(&ibuf);
269 close(imsg_fds[1]);
270 _exit(status);
273 static const struct got_error *
274 wait_for_child(pid_t pid)
276 int child_status;
278 waitpid(pid, &child_status, 0);
280 if (!WIFEXITED(child_status))
281 return got_error(GOT_ERR_PRIVSEP_DIED);
283 if (WEXITSTATUS(child_status) != 0)
284 return got_error(GOT_ERR_PRIVSEP_EXIT);
286 return NULL;
289 static const struct got_error *
290 read_object_header_privsep(struct got_object **obj, int fd)
292 struct imsgbuf parent_ibuf;
293 int imsg_fds[2];
294 const struct got_error *err = NULL, *err_child = NULL;
295 pid_t pid;
297 if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_fds) == -1)
298 return got_error_from_errno();
300 pid = fork();
301 if (pid == -1)
302 return got_error_from_errno();
303 else if (pid == 0) {
304 read_object_header_privsep_child(fd, imsg_fds);
305 /* not reached */
308 close(imsg_fds[1]);
309 imsg_init(&parent_ibuf, imsg_fds[0]);
310 err = got_privsep_recv_obj(obj, &parent_ibuf);
311 imsg_clear(&parent_ibuf);
312 err_child = wait_for_child(pid);
313 close(imsg_fds[0]);
314 return err ? err : err_child;
317 static const struct got_error *
318 object_path(char **path, struct got_object_id *id, struct got_repository *repo)
320 const struct got_error *err = NULL;
321 char *hex;
322 char *path_objects = got_repo_get_path_objects(repo);
324 *path = NULL;
326 if (path_objects == NULL)
327 return got_error_from_errno();
329 err = got_object_id_str(&hex, id);
330 if (err)
331 return err;
333 if (asprintf(path, "%s/%.2x/%s", path_objects,
334 id->sha1[0], hex + 2) == -1)
335 err = got_error_from_errno();
337 free(hex);
338 free(path_objects);
339 return err;
342 static const struct got_error *
343 open_loose_object(int *fd, struct got_object *obj, struct got_repository *repo)
345 const struct got_error *err = NULL;
346 char *path;
348 err = object_path(&path, &obj->id, repo);
349 if (err)
350 return err;
351 *fd = open(path, O_RDONLY | O_NOFOLLOW, GOT_DEFAULT_FILE_MODE);
352 if (*fd == -1) {
353 err = got_error_from_errno();
354 goto done;
356 done:
357 free(path);
358 return err;
361 const struct got_error *
362 got_object_open(struct got_object **obj, struct got_repository *repo,
363 struct got_object_id *id)
365 const struct got_error *err = NULL;
366 char *path;
367 int fd;
369 *obj = got_repo_get_cached_object(repo, id);
370 if (*obj != NULL) {
371 (*obj)->refcnt++;
372 return NULL;
375 err = object_path(&path, id, repo);
376 if (err)
377 return err;
379 fd = open(path, O_RDONLY | O_NOFOLLOW, GOT_DEFAULT_FILE_MODE);
380 if (fd == -1) {
381 if (errno != ENOENT) {
382 err = got_error_from_errno();
383 goto done;
385 err = got_packfile_open_object(obj, id, repo);
386 if (err)
387 goto done;
388 if (*obj == NULL)
389 err = got_error(GOT_ERR_NO_OBJ);
390 } else {
391 err = read_object_header_privsep(obj, fd);
392 if (err)
393 goto done;
394 memcpy((*obj)->id.sha1, id->sha1, SHA1_DIGEST_LENGTH);
397 if (err == NULL) {
398 (*obj)->refcnt++;
399 err = got_repo_cache_object(repo, id, *obj);
401 done:
402 free(path);
403 if (fd != -1)
404 close(fd);
405 return err;
409 const struct got_error *
410 got_object_open_by_id_str(struct got_object **obj, struct got_repository *repo,
411 const char *id_str)
413 struct got_object_id id;
415 if (!got_parse_sha1_digest(id.sha1, id_str))
416 return got_error(GOT_ERR_BAD_OBJ_ID_STR);
418 return got_object_open(obj, repo, &id);
421 void
422 got_object_close(struct got_object *obj)
424 if (obj->refcnt > 0) {
425 obj->refcnt--;
426 return;
429 if (obj->flags & GOT_OBJ_FLAG_DELTIFIED) {
430 struct got_delta *delta;
431 while (!SIMPLEQ_EMPTY(&obj->deltas.entries)) {
432 delta = SIMPLEQ_FIRST(&obj->deltas.entries);
433 SIMPLEQ_REMOVE_HEAD(&obj->deltas.entries, entry);
434 got_delta_close(delta);
437 if (obj->flags & GOT_OBJ_FLAG_PACKED)
438 free(obj->path_packfile);
439 free(obj);
442 struct got_commit_object *
443 got_object_commit_alloc_partial(void)
445 struct got_commit_object *commit;
447 commit = calloc(1, sizeof(*commit));
448 if (commit == NULL)
449 return NULL;
450 commit->tree_id = calloc(1, sizeof(*commit->tree_id));
451 if (commit->tree_id == NULL) {
452 free(commit);
453 return NULL;
456 SIMPLEQ_INIT(&commit->parent_ids);
458 return commit;
461 const struct got_error *
462 got_object_open_as_commit(struct got_commit_object **commit,
463 struct got_repository *repo, struct got_object_id *id)
465 const struct got_error *err;
466 struct got_object *obj;
468 *commit = NULL;
470 err = got_object_open(&obj, repo, id);
471 if (err)
472 return err;
473 if (got_object_get_type(obj) != GOT_OBJ_TYPE_COMMIT) {
474 err = got_error(GOT_ERR_OBJ_TYPE);
475 goto done;
478 err = got_object_commit_open(commit, repo, obj);
479 done:
480 got_object_close(obj);
481 return err;
484 const struct got_error *
485 got_object_commit_add_parent(struct got_commit_object *commit,
486 const char *id_str)
488 const struct got_error *err = NULL;
489 struct got_object_qid *qid;
491 qid = calloc(1, sizeof(*qid));
492 if (qid == NULL)
493 return got_error_from_errno();
495 qid->id = calloc(1, sizeof(*qid->id));
496 if (qid->id == NULL) {
497 err = got_error_from_errno();
498 free(qid);
499 return err;
502 if (!got_parse_sha1_digest(qid->id->sha1, id_str)) {
503 err = got_error(GOT_ERR_BAD_OBJ_DATA);
504 free(qid->id);
505 free(qid);
506 return err;
509 SIMPLEQ_INSERT_TAIL(&commit->parent_ids, qid, entry);
510 commit->nparents++;
512 return NULL;
515 static const struct got_error *
516 parse_gmtoff(time_t *gmtoff, const char *tzstr)
518 int sign = 1;
519 const char *p = tzstr;
520 time_t h, m;
522 *gmtoff = 0;
524 if (*p == '-')
525 sign = -1;
526 else if (*p != '+')
527 return got_error(GOT_ERR_BAD_OBJ_DATA);
528 p++;
529 if (!isdigit(*p) && !isdigit(*(p + 1)))
530 return got_error(GOT_ERR_BAD_OBJ_DATA);
531 h = (((*p - '0') * 10) + (*(p + 1) - '0'));
533 p += 2;
534 if (!isdigit(*p) && !isdigit(*(p + 1)))
535 return got_error(GOT_ERR_BAD_OBJ_DATA);
536 m = ((*p - '0') * 10) + (*(p + 1) - '0');
538 *gmtoff = (h * 60 * 60 + m * 60) * sign;
539 return NULL;
542 static const struct got_error *
543 parse_commit_time(struct tm *tm, char *committer)
545 const struct got_error *err = NULL;
546 const char *errstr;
547 char *space, *tzstr;
548 time_t gmtoff;
549 time_t time;
551 /* Parse and strip off trailing timezone indicator string. */
552 space = strrchr(committer, ' ');
553 if (space == NULL)
554 return got_error(GOT_ERR_BAD_OBJ_DATA);
555 tzstr = strdup(space + 1);
556 if (tzstr == NULL)
557 return got_error_from_errno();
558 err = parse_gmtoff(&gmtoff, tzstr);
559 free(tzstr);
560 if (err)
561 return err;
562 *space = '\0';
564 /* Timestamp is separated from committer name + email by space. */
565 space = strrchr(committer, ' ');
566 if (space == NULL)
567 return got_error(GOT_ERR_BAD_OBJ_DATA);
569 /* Timestamp parsed here is expressed in comitter's local time. */
570 time = strtonum(space + 1, 0, INT64_MAX, &errstr);
571 if (errstr)
572 return got_error(GOT_ERR_BAD_OBJ_DATA);
574 /* Express the time stamp in UTC. */
575 memset(tm, 0, sizeof(*tm));
576 time -= gmtoff;
577 if (localtime_r(&time, tm) == NULL)
578 return got_error_from_errno();
579 tm->tm_gmtoff = gmtoff;
581 /* Strip off parsed time information, leaving just author and email. */
582 *space = '\0';
584 return NULL;
587 static const struct got_error *
588 parse_commit_object(struct got_commit_object **commit, char *buf, size_t len)
590 const struct got_error *err = NULL;
591 char *s = buf;
592 size_t tlen;
593 ssize_t remain = (ssize_t)len;
595 *commit = got_object_commit_alloc_partial();
596 if (*commit == NULL)
597 return got_error_from_errno();
599 tlen = strlen(GOT_COMMIT_TAG_TREE);
600 if (strncmp(s, GOT_COMMIT_TAG_TREE, tlen) == 0) {
601 remain -= tlen;
602 if (remain < SHA1_DIGEST_STRING_LENGTH) {
603 err = got_error(GOT_ERR_BAD_OBJ_DATA);
604 goto done;
606 s += tlen;
607 if (!got_parse_sha1_digest((*commit)->tree_id->sha1, s)) {
608 err = got_error(GOT_ERR_BAD_OBJ_DATA);
609 goto done;
611 remain -= SHA1_DIGEST_STRING_LENGTH;
612 s += SHA1_DIGEST_STRING_LENGTH;
613 } else {
614 err = got_error(GOT_ERR_BAD_OBJ_DATA);
615 goto done;
618 tlen = strlen(GOT_COMMIT_TAG_PARENT);
619 while (strncmp(s, GOT_COMMIT_TAG_PARENT, 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 err = got_object_commit_add_parent(*commit, s);
627 if (err)
628 goto done;
630 remain -= SHA1_DIGEST_STRING_LENGTH;
631 s += SHA1_DIGEST_STRING_LENGTH;
634 tlen = strlen(GOT_COMMIT_TAG_AUTHOR);
635 if (strncmp(s, GOT_COMMIT_TAG_AUTHOR, tlen) == 0) {
636 char *p;
637 size_t slen;
639 remain -= tlen;
640 if (remain <= 0) {
641 err = got_error(GOT_ERR_BAD_OBJ_DATA);
642 goto done;
644 s += tlen;
645 p = strchr(s, '\n');
646 if (p == NULL) {
647 err = got_error(GOT_ERR_BAD_OBJ_DATA);
648 goto done;
650 *p = '\0';
651 slen = strlen(s);
652 err = parse_commit_time(&(*commit)->tm_author, s);
653 if (err)
654 goto done;
655 (*commit)->author = strdup(s);
656 if ((*commit)->author == NULL) {
657 err = got_error_from_errno();
658 goto done;
660 s += slen + 1;
661 remain -= slen + 1;
664 tlen = strlen(GOT_COMMIT_TAG_COMMITTER);
665 if (strncmp(s, GOT_COMMIT_TAG_COMMITTER, tlen) == 0) {
666 char *p;
667 size_t slen;
669 remain -= tlen;
670 if (remain <= 0) {
671 err = got_error(GOT_ERR_BAD_OBJ_DATA);
672 goto done;
674 s += tlen;
675 p = strchr(s, '\n');
676 if (p == NULL) {
677 err = got_error(GOT_ERR_BAD_OBJ_DATA);
678 goto done;
680 *p = '\0';
681 slen = strlen(s);
682 err = parse_commit_time(&(*commit)->tm_committer, s);
683 if (err)
684 goto done;
685 (*commit)->committer = strdup(s);
686 if ((*commit)->committer == NULL) {
687 err = got_error_from_errno();
688 goto done;
690 s += slen + 1;
691 remain -= slen + 1;
694 (*commit)->logmsg = strndup(s, remain);
695 if ((*commit)->logmsg == NULL) {
696 err = got_error_from_errno();
697 goto done;
699 done:
700 if (err) {
701 got_object_commit_close(*commit);
702 *commit = NULL;
704 return err;
707 static void
708 tree_entry_close(struct got_tree_entry *te)
710 free(te->id);
711 free(te->name);
712 free(te);
715 struct got_tree_entry *
716 got_alloc_tree_entry_partial(void)
718 struct got_tree_entry *te;
720 te = calloc(1, sizeof(*te));
721 if (te == NULL)
722 return NULL;
724 te->id = calloc(1, sizeof(*te->id));
725 if (te->id == NULL) {
726 free(te);
727 te = NULL;
729 return te;
732 static const struct got_error *
733 parse_tree_entry(struct got_tree_entry **te, size_t *elen, char *buf,
734 size_t maxlen)
736 char *p = buf, *space;
737 const struct got_error *err = NULL;
739 *te = got_alloc_tree_entry_partial();
740 if (*te == NULL)
741 return got_error_from_errno();
743 *elen = strlen(buf) + 1;
744 if (*elen > maxlen) {
745 free(*te);
746 *te = NULL;
747 return got_error(GOT_ERR_BAD_OBJ_DATA);
750 space = strchr(buf, ' ');
751 if (space == NULL) {
752 err = got_error(GOT_ERR_BAD_OBJ_DATA);
753 free(*te);
754 *te = NULL;
755 return err;
757 while (*p != ' ') {
758 if (*p < '0' && *p > '7') {
759 err = got_error(GOT_ERR_BAD_OBJ_DATA);
760 goto done;
762 (*te)->mode <<= 3;
763 (*te)->mode |= *p - '0';
764 p++;
767 (*te)->name = strdup(space + 1);
768 if (*elen > maxlen || maxlen - *elen < SHA1_DIGEST_LENGTH) {
769 err = got_error(GOT_ERR_BAD_OBJ_DATA);
770 goto done;
772 buf += strlen(buf) + 1;
773 memcpy((*te)->id->sha1, buf, SHA1_DIGEST_LENGTH);
774 *elen += SHA1_DIGEST_LENGTH;
775 done:
776 if (err) {
777 tree_entry_close(*te);
778 *te = NULL;
780 return err;
783 static const struct got_error *
784 parse_tree_object(struct got_tree_object **tree, uint8_t *buf, size_t len)
786 const struct got_error *err;
787 size_t remain = len;
789 *tree = calloc(1, sizeof(**tree));
790 if (*tree == NULL)
791 return got_error_from_errno();
793 SIMPLEQ_INIT(&(*tree)->entries);
795 while (remain > 0) {
796 struct got_tree_entry *te;
797 size_t elen;
799 err = parse_tree_entry(&te, &elen, buf, remain);
800 if (err)
801 return err;
802 (*tree)->nentries++;
803 SIMPLEQ_INSERT_TAIL(&(*tree)->entries, te, entry);
804 buf += elen;
805 remain -= elen;
808 if (remain != 0) {
809 got_object_tree_close(*tree);
810 return got_error(GOT_ERR_BAD_OBJ_DATA);
813 return NULL;
816 static const struct got_error *
817 read_to_mem(uint8_t **outbuf, size_t *outlen, FILE *f)
819 const struct got_error *err = NULL;
820 static const size_t blocksize = 512;
821 size_t n, total, remain;
822 uint8_t *buf;
824 *outbuf = NULL;
825 *outlen = 0;
827 buf = calloc(1, blocksize);
828 if (buf == NULL)
829 return got_error_from_errno();
831 remain = blocksize;
832 total = 0;
833 while (1) {
834 if (remain == 0) {
835 uint8_t *newbuf;
836 newbuf = reallocarray(buf, 1, total + blocksize);
837 if (newbuf == NULL) {
838 err = got_error_from_errno();
839 goto done;
841 buf = newbuf;
842 remain += blocksize;
844 n = fread(buf + total, 1, remain, f);
845 if (n == 0) {
846 if (ferror(f)) {
847 err = got_ferror(f, GOT_ERR_IO);
848 goto done;
850 break; /* EOF */
852 remain -= n;
853 total += n;
854 };
856 done:
857 if (err == NULL) {
858 *outbuf = buf;
859 *outlen = total;
860 } else
861 free(buf);
862 return err;
865 static const struct got_error *
866 read_commit_object(struct got_commit_object **commit, struct got_object *obj,
867 FILE *f)
869 const struct got_error *err = NULL;
870 size_t len;
871 uint8_t *p;
873 if (obj->flags & GOT_OBJ_FLAG_PACKED)
874 err = read_to_mem(&p, &len, f);
875 else
876 err = got_inflate_to_mem(&p, &len, f);
877 if (err)
878 return err;
880 if (len < obj->hdrlen + obj->size) {
881 err = got_error(GOT_ERR_BAD_OBJ_DATA);
882 goto done;
885 /* Skip object header. */
886 len -= obj->hdrlen;
887 err = parse_commit_object(commit, p + obj->hdrlen, len);
888 free(p);
889 done:
890 return err;
893 static void
894 read_commit_object_privsep_child(struct got_object *obj, int obj_fd,
895 int imsg_fds[2])
897 const struct got_error *err = NULL;
898 struct got_commit_object *commit = NULL;
899 struct imsgbuf ibuf;
900 FILE *f = NULL;
901 int status = 0;
903 setproctitle("read commit object");
904 close(imsg_fds[0]);
905 imsg_init(&ibuf, imsg_fds[1]);
907 /* revoke access to most system calls */
908 if (pledge("stdio", NULL) == -1) {
909 err = got_error_from_errno();
910 goto done;
913 f = fdopen(obj_fd, "rb");
914 if (f == NULL) {
915 err = got_error_from_errno();
916 close(obj_fd);
917 goto done;
920 err = read_commit_object(&commit, obj, f);
921 if (err)
922 goto done;
924 err = got_privsep_send_commit(&ibuf, commit);
925 done:
926 if (commit)
927 got_object_commit_close(commit);
928 if (err) {
929 got_privsep_send_error(&ibuf, err);
930 status = 1;
932 if (f)
933 fclose(f);
934 imsg_clear(&ibuf);
935 close(imsg_fds[1]);
936 _exit(status);
939 static const struct got_error *
940 read_commit_object_privsep(struct got_commit_object **commit,
941 struct got_repository *repo, struct got_object *obj, int fd)
943 const struct got_error *err = NULL, *err_child = NULL;
944 struct imsgbuf parent_ibuf;
945 int imsg_fds[2];
946 pid_t pid;
948 if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_fds) == -1)
949 return got_error_from_errno();
951 pid = fork();
952 if (pid == -1)
953 return got_error_from_errno();
954 else if (pid == 0) {
955 read_commit_object_privsep_child(obj, fd, imsg_fds);
956 /* not reached */
959 close(imsg_fds[1]);
960 imsg_init(&parent_ibuf, imsg_fds[0]);
961 err = got_privsep_recv_commit(commit, &parent_ibuf);
962 imsg_clear(&parent_ibuf);
963 err_child = wait_for_child(pid);
964 close(imsg_fds[0]);
965 return err ? err : err_child;
968 const struct got_error *
969 got_object_commit_open(struct got_commit_object **commit,
970 struct got_repository *repo, struct got_object *obj)
972 const struct got_error *err = NULL;
974 if (obj->type != GOT_OBJ_TYPE_COMMIT)
975 return got_error(GOT_ERR_OBJ_TYPE);
977 if (obj->flags & GOT_OBJ_FLAG_PACKED) {
978 uint8_t *buf;
979 size_t len;
980 err = got_packfile_extract_object_to_mem(&buf, &len, obj, repo);
981 if (err)
982 return err;
983 obj->size = len;
984 err = parse_commit_object(commit, buf, len);
985 free(buf);
986 } else {
987 int fd;
988 err = open_loose_object(&fd, obj, repo);
989 if (err)
990 return err;
991 err = read_commit_object_privsep(commit, repo, obj, fd);
992 close(fd);
994 return err;
997 void
998 got_object_commit_close(struct got_commit_object *commit)
1000 struct got_object_qid *qid;
1002 while (!SIMPLEQ_EMPTY(&commit->parent_ids)) {
1003 qid = SIMPLEQ_FIRST(&commit->parent_ids);
1004 SIMPLEQ_REMOVE_HEAD(&commit->parent_ids, entry);
1005 free(qid->id);
1006 free(qid);
1009 free(commit->tree_id);
1010 free(commit->author);
1011 free(commit->committer);
1012 free(commit->logmsg);
1013 free(commit);
1016 static const struct got_error *
1017 read_tree_object(struct got_tree_object **tree, struct got_object *obj, FILE *f)
1019 const struct got_error *err = NULL;
1020 size_t len;
1021 uint8_t *p;
1023 if (obj->flags & GOT_OBJ_FLAG_PACKED)
1024 err = read_to_mem(&p, &len, f);
1025 else
1026 err = got_inflate_to_mem(&p, &len, f);
1027 if (err)
1028 return err;
1030 if (len < obj->hdrlen + obj->size) {
1031 err = got_error(GOT_ERR_BAD_OBJ_DATA);
1032 goto done;
1035 /* Skip object header. */
1036 len -= obj->hdrlen;
1037 err = parse_tree_object(tree, p + obj->hdrlen, len);
1038 free(p);
1039 done:
1040 return err;
1043 static void
1044 read_tree_object_privsep_child(struct got_object *obj, int obj_fd,
1045 int imsg_fds[2])
1047 const struct got_error *err = NULL;
1048 struct got_tree_object *tree = NULL;
1049 struct imsgbuf ibuf;
1050 FILE *f = NULL;
1051 int status = 0;
1053 setproctitle("read tree object");
1054 close(imsg_fds[0]);
1055 imsg_init(&ibuf, imsg_fds[1]);
1057 /* revoke access to most system calls */
1058 if (pledge("stdio", NULL) == -1) {
1059 err = got_error_from_errno();
1060 goto done;
1063 f = fdopen(obj_fd, "rb");
1064 if (f == NULL) {
1065 err = got_error_from_errno();
1066 close(obj_fd);
1067 goto done;
1070 err = read_tree_object(&tree, obj, f);
1071 if (err)
1072 goto done;
1074 err = got_privsep_send_tree(&ibuf, tree);
1075 done:
1076 if (tree)
1077 got_object_tree_close(tree);
1078 if (err) {
1079 got_privsep_send_error(&ibuf, err);
1080 status = 1;
1082 if (f)
1083 fclose(f);
1084 imsg_clear(&ibuf);
1085 close(imsg_fds[1]);
1086 _exit(status);
1089 static const struct got_error *
1090 read_tree_object_privsep(struct got_tree_object **tree, struct got_object *obj,
1091 int fd)
1093 const struct got_error *err = NULL, *err_child = NULL;
1094 struct imsgbuf parent_ibuf;
1095 int imsg_fds[2];
1096 pid_t pid;
1098 if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_fds) == -1)
1099 return got_error_from_errno();
1101 pid = fork();
1102 if (pid == -1)
1103 return got_error_from_errno();
1104 else if (pid == 0) {
1105 read_tree_object_privsep_child(obj, fd, imsg_fds);
1106 /* not reached */
1109 close(imsg_fds[1]);
1110 imsg_init(&parent_ibuf, imsg_fds[0]);
1111 err = got_privsep_recv_tree(tree, &parent_ibuf);
1112 imsg_clear(&parent_ibuf);
1113 err_child = wait_for_child(pid);
1114 close(imsg_fds[0]);
1115 return err ? err : err_child;
1118 const struct got_error *
1119 got_object_tree_open(struct got_tree_object **tree,
1120 struct got_repository *repo, struct got_object *obj)
1122 const struct got_error *err = NULL;
1124 if (obj->type != GOT_OBJ_TYPE_TREE)
1125 return got_error(GOT_ERR_OBJ_TYPE);
1127 if (obj->flags & GOT_OBJ_FLAG_PACKED) {
1128 uint8_t *buf;
1129 size_t len;
1130 err = got_packfile_extract_object_to_mem(&buf, &len, obj, repo);
1131 if (err)
1132 return err;
1133 obj->size = len;
1134 err = parse_tree_object(tree, buf, len);
1135 free(buf);
1136 } else {
1137 int fd;
1138 err = open_loose_object(&fd, obj, repo);
1139 if (err)
1140 return err;
1141 err = read_tree_object_privsep(tree, obj, fd);
1142 close(fd);
1144 return err;
1147 const struct got_error *
1148 got_object_open_as_tree(struct got_tree_object **tree,
1149 struct got_repository *repo, struct got_object_id *id)
1151 const struct got_error *err;
1152 struct got_object *obj;
1154 *tree = NULL;
1156 err = got_object_open(&obj, repo, id);
1157 if (err)
1158 return err;
1159 if (got_object_get_type(obj) != GOT_OBJ_TYPE_TREE) {
1160 err = got_error(GOT_ERR_OBJ_TYPE);
1161 goto done;
1164 err = got_object_tree_open(tree, repo, obj);
1165 done:
1166 got_object_close(obj);
1167 return err;
1170 void
1171 got_object_tree_close(struct got_tree_object *tree)
1173 struct got_tree_entry *te;
1175 while (!SIMPLEQ_EMPTY(&tree->entries)) {
1176 te = SIMPLEQ_FIRST(&tree->entries);
1177 SIMPLEQ_REMOVE_HEAD(&tree->entries, entry);
1178 tree_entry_close(te);
1181 free(tree);
1184 static const struct got_error *
1185 read_blob_object_privsep_child(int outfd, int infd, int imsg_fds[2])
1187 const struct got_error *err = NULL;
1188 struct imsgbuf ibuf;
1189 int status = 0;
1190 size_t size;
1191 FILE *infile = NULL;
1193 setproctitle("read blob object");
1194 close(imsg_fds[0]);
1195 imsg_init(&ibuf, imsg_fds[1]);
1197 /* revoke access to most system calls */
1198 if (pledge("stdio", NULL) == -1) {
1199 err = got_error_from_errno();
1200 goto done;
1203 infile = fdopen(infd, "rb");
1204 if (infile == NULL) {
1205 err = got_error_from_errno();
1206 close(infd);
1207 goto done;
1209 err = got_inflate_to_fd(&size, infile, outfd);
1210 fclose(infile);
1211 if (err)
1212 goto done;
1214 err = got_privsep_send_blob(&ibuf, size);
1215 done:
1216 if (err) {
1217 got_privsep_send_error(&ibuf, err);
1218 status = 1;
1220 close(outfd);
1221 imsg_clear(&ibuf);
1222 close(imsg_fds[1]);
1223 _exit(status);
1226 static const struct got_error *
1227 read_blob_object_privsep(size_t *size, int outfd, int infd)
1229 struct imsgbuf parent_ibuf;
1230 int imsg_fds[2];
1231 const struct got_error *err = NULL, *err_child = NULL;
1232 pid_t pid;
1234 if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_fds) == -1)
1235 return got_error_from_errno();
1237 pid = fork();
1238 if (pid == -1)
1239 return got_error_from_errno();
1240 else if (pid == 0) {
1241 read_blob_object_privsep_child(outfd, infd, imsg_fds);
1242 /* not reached */
1245 close(imsg_fds[1]);
1246 imsg_init(&parent_ibuf, imsg_fds[0]);
1247 err = got_privsep_recv_blob(size, &parent_ibuf);
1248 imsg_clear(&parent_ibuf);
1249 err_child = wait_for_child(pid);
1250 close(imsg_fds[0]);
1251 if (lseek(outfd, SEEK_SET, 0) == -1)
1252 err = got_error_from_errno();
1253 return err ? err : err_child;
1256 const struct got_error *
1257 got_object_blob_open(struct got_blob_object **blob,
1258 struct got_repository *repo, struct got_object *obj, size_t blocksize)
1260 const struct got_error *err = NULL;
1262 if (obj->type != GOT_OBJ_TYPE_BLOB)
1263 return got_error(GOT_ERR_OBJ_TYPE);
1265 if (blocksize < obj->hdrlen)
1266 return got_error(GOT_ERR_NO_SPACE);
1268 *blob = calloc(1, sizeof(**blob));
1269 if (*blob == NULL)
1270 return got_error_from_errno();
1272 (*blob)->read_buf = calloc(1, blocksize);
1273 if ((*blob)->read_buf == NULL) {
1274 err = got_error_from_errno();
1275 goto done;
1277 if (obj->flags & GOT_OBJ_FLAG_PACKED) {
1278 err = got_packfile_extract_object(&((*blob)->f), obj, repo);
1279 if (err)
1280 goto done;
1281 } else {
1282 int infd, outfd;
1283 size_t size;
1284 struct stat sb;
1286 err = open_loose_object(&infd, obj, repo);
1287 if (err)
1288 goto done;
1291 outfd = got_opentempfd();
1292 if (outfd == -1) {
1293 err = got_error_from_errno();
1294 close(infd);
1295 goto done;
1298 err = read_blob_object_privsep(&size, outfd, infd);
1299 close(infd);
1300 if (err)
1301 goto done;
1303 if (size != obj->hdrlen + obj->size) {
1304 err = got_error(GOT_ERR_PRIVSEP_LEN);
1305 close(outfd);
1306 goto done;
1309 if (fstat(outfd, &sb) == -1) {
1310 err = got_error_from_errno();
1311 close(outfd);
1312 goto done;
1315 if (sb.st_size != size) {
1316 err = got_error(GOT_ERR_PRIVSEP_LEN);
1317 close(outfd);
1318 goto done;
1321 (*blob)->f = fdopen(outfd, "rb");
1322 if ((*blob)->f == NULL) {
1323 err = got_error_from_errno();
1324 close(outfd);
1325 goto done;
1329 (*blob)->hdrlen = obj->hdrlen;
1330 (*blob)->blocksize = blocksize;
1331 memcpy(&(*blob)->id.sha1, obj->id.sha1, SHA1_DIGEST_LENGTH);
1333 done:
1334 if (err && *blob) {
1335 if ((*blob)->f)
1336 fclose((*blob)->f);
1337 free((*blob)->read_buf);
1338 free(*blob);
1339 *blob = NULL;
1341 return err;
1344 const struct got_error *
1345 got_object_open_as_blob(struct got_blob_object **blob,
1346 struct got_repository *repo, struct got_object_id *id,
1347 size_t blocksize)
1349 const struct got_error *err;
1350 struct got_object *obj;
1352 *blob = NULL;
1354 err = got_object_open(&obj, repo, id);
1355 if (err)
1356 return err;
1357 if (got_object_get_type(obj) != GOT_OBJ_TYPE_BLOB) {
1358 err = got_error(GOT_ERR_OBJ_TYPE);
1359 goto done;
1362 err = got_object_blob_open(blob, repo, obj, blocksize);
1363 done:
1364 got_object_close(obj);
1365 return err;
1368 void
1369 got_object_blob_close(struct got_blob_object *blob)
1371 free(blob->read_buf);
1372 fclose(blob->f);
1373 free(blob);
1376 char *
1377 got_object_blob_id_str(struct got_blob_object *blob, char *buf, size_t size)
1379 return got_sha1_digest_to_str(blob->id.sha1, buf, size);
1382 size_t
1383 got_object_blob_get_hdrlen(struct got_blob_object *blob)
1385 return blob->hdrlen;
1388 const uint8_t *
1389 got_object_blob_get_read_buf(struct got_blob_object *blob)
1391 return blob->read_buf;
1394 const struct got_error *
1395 got_object_blob_read_block(size_t *outlenp, struct got_blob_object *blob)
1397 size_t n;
1399 n = fread(blob->read_buf, 1, blob->blocksize, blob->f);
1400 if (n == 0 && ferror(blob->f))
1401 return got_ferror(blob->f, GOT_ERR_IO);
1402 *outlenp = n;
1403 return NULL;
1406 const struct got_error *
1407 got_object_blob_dump_to_file(size_t *total_len, FILE *outfile,
1408 struct got_blob_object *blob)
1410 const struct got_error *err = NULL;
1411 size_t len, hdrlen;
1413 *total_len = 0;
1414 hdrlen = got_object_blob_get_hdrlen(blob);
1415 do {
1416 err = got_object_blob_read_block(&len, blob);
1417 if (err)
1418 return err;
1419 if (len == 0)
1420 break;
1421 *total_len += len;
1422 /* Skip blob object header first time around. */
1423 fwrite(got_object_blob_get_read_buf(blob) + hdrlen,
1424 len - hdrlen, 1, outfile);
1425 hdrlen = 0;
1426 } while (len != 0);
1428 fflush(outfile);
1429 rewind(outfile);
1431 return NULL;
1434 static struct got_tree_entry *
1435 find_entry_by_name(struct got_tree_object *tree, const char *name)
1437 struct got_tree_entry *te;
1439 SIMPLEQ_FOREACH(te, &tree->entries, entry) {
1440 if (strcmp(te->name, name) == 0)
1441 return te;
1443 return NULL;
1446 const struct got_error *
1447 got_object_open_by_path(struct got_object **obj, struct got_repository *repo,
1448 struct got_object_id *commit_id, const char *path)
1450 const struct got_error *err = NULL;
1451 struct got_commit_object *commit = NULL;
1452 struct got_tree_object *tree = NULL;
1453 struct got_tree_entry *te = NULL;
1454 char *seg, *s, *s0 = NULL;
1455 size_t len = strlen(path);
1457 *obj = NULL;
1459 /* We are expecting an absolute in-repository path. */
1460 if (path[0] != '/')
1461 return got_error(GOT_ERR_NOT_ABSPATH);
1463 err = got_object_open_as_commit(&commit, repo, commit_id);
1464 if (err)
1465 goto done;
1467 /* Handle opening of root of commit's tree. */
1468 if (path[1] == '\0') {
1469 err = got_object_open(obj, repo, commit->tree_id);
1470 if (err)
1471 goto done;
1472 return NULL;
1475 err = got_object_open_as_tree(&tree, repo, commit->tree_id);
1476 if (err)
1477 goto done;
1479 s0 = strdup(path);
1480 if (s0 == NULL) {
1481 err = got_error_from_errno();
1482 goto done;
1484 err = got_canonpath(path, s0, len + 1);
1485 if (err)
1486 goto done;
1488 s = s0;
1489 s++; /* skip leading '/' */
1490 len--;
1491 seg = s;
1492 while (len > 0) {
1493 struct got_tree_object *next_tree;
1495 if (*s != '/') {
1496 s++;
1497 len--;
1498 if (*s)
1499 continue;
1502 /* end of path segment */
1503 *s = '\0';
1505 te = find_entry_by_name(tree, seg);
1506 if (te == NULL) {
1507 err = got_error(GOT_ERR_NO_OBJ);
1508 goto done;
1511 if (len == 0)
1512 break;
1514 seg = s + 1;
1515 s++;
1516 len--;
1517 if (*s) {
1518 err = got_object_open_as_tree(&next_tree, repo,
1519 te->id);
1520 te = NULL;
1521 if (err)
1522 goto done;
1523 got_object_tree_close(tree);
1524 tree = next_tree;
1528 if (te) {
1529 err = got_object_open(obj, repo, te->id);
1530 if (err)
1531 goto done;
1532 } else
1533 err = got_error(GOT_ERR_NO_OBJ);
1534 done:
1535 free(s0);
1536 if (commit)
1537 got_object_commit_close(commit);
1538 if (tree)
1539 got_object_tree_close(tree);
1540 return err;