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 = 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, 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 = 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_fd(&zb, fd, &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 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_commit_add_parent(struct got_commit_object *commit,
478 const char *id_str)
480 const struct got_error *err = NULL;
481 struct got_object_qid *qid;
483 qid = calloc(1, sizeof(*qid));
484 if (qid == NULL)
485 return got_error_from_errno();
487 qid->id = calloc(1, sizeof(*qid->id));
488 if (qid->id == NULL) {
489 err = got_error_from_errno();
490 free(qid);
491 return err;
494 if (!got_parse_sha1_digest(qid->id->sha1, id_str)) {
495 err = got_error(GOT_ERR_BAD_OBJ_DATA);
496 free(qid->id);
497 free(qid);
498 return err;
501 SIMPLEQ_INSERT_TAIL(&commit->parent_ids, qid, entry);
502 commit->nparents++;
504 return NULL;
507 static const struct got_error *
508 parse_gmtoff(time_t *gmtoff, const char *tzstr)
510 int sign = 1;
511 const char *p = tzstr;
512 time_t h, m;
514 *gmtoff = 0;
516 if (*p == '-')
517 sign = -1;
518 else if (*p != '+')
519 return got_error(GOT_ERR_BAD_OBJ_DATA);
520 p++;
521 if (!isdigit(*p) && !isdigit(*(p + 1)))
522 return got_error(GOT_ERR_BAD_OBJ_DATA);
523 h = (((*p - '0') * 10) + (*(p + 1) - '0'));
525 p += 2;
526 if (!isdigit(*p) && !isdigit(*(p + 1)))
527 return got_error(GOT_ERR_BAD_OBJ_DATA);
528 m = ((*p - '0') * 10) + (*(p + 1) - '0');
530 *gmtoff = (h * 60 * 60 + m * 60) * sign;
531 return NULL;
534 static const struct got_error *
535 parse_commit_time(struct tm *tm, char *committer)
537 const struct got_error *err = NULL;
538 const char *errstr;
539 char *space, *tzstr;
540 time_t gmtoff;
541 time_t time;
543 /* Parse and strip off trailing timezone indicator string. */
544 space = strrchr(committer, ' ');
545 if (space == NULL)
546 return got_error(GOT_ERR_BAD_OBJ_DATA);
547 tzstr = strdup(space + 1);
548 if (tzstr == NULL)
549 return got_error_from_errno();
550 err = parse_gmtoff(&gmtoff, tzstr);
551 free(tzstr);
552 if (err)
553 return err;
554 *space = '\0';
556 /* Timestamp is separated from committer name + email by space. */
557 space = strrchr(committer, ' ');
558 if (space == NULL)
559 return got_error(GOT_ERR_BAD_OBJ_DATA);
561 /* Timestamp parsed here is expressed in comitter's local time. */
562 time = strtonum(space + 1, 0, INT64_MAX, &errstr);
563 if (errstr)
564 return got_error(GOT_ERR_BAD_OBJ_DATA);
566 /* Express the time stamp in UTC. */
567 memset(tm, 0, sizeof(*tm));
568 time -= gmtoff;
569 if (localtime_r(&time, tm) == NULL)
570 return got_error_from_errno();
571 tm->tm_gmtoff = gmtoff;
573 /* Strip off parsed time information, leaving just author and email. */
574 *space = '\0';
576 return NULL;
579 static const struct got_error *
580 parse_commit_object(struct got_commit_object **commit, char *buf, size_t len)
582 const struct got_error *err = NULL;
583 char *s = buf;
584 size_t tlen;
585 ssize_t remain = (ssize_t)len;
587 *commit = got_object_commit_alloc_partial();
588 if (*commit == NULL)
589 return got_error_from_errno();
591 tlen = strlen(GOT_COMMIT_TAG_TREE);
592 if (strncmp(s, GOT_COMMIT_TAG_TREE, tlen) == 0) {
593 remain -= tlen;
594 if (remain < SHA1_DIGEST_STRING_LENGTH) {
595 err = got_error(GOT_ERR_BAD_OBJ_DATA);
596 goto done;
598 s += tlen;
599 if (!got_parse_sha1_digest((*commit)->tree_id->sha1, s)) {
600 err = got_error(GOT_ERR_BAD_OBJ_DATA);
601 goto done;
603 remain -= SHA1_DIGEST_STRING_LENGTH;
604 s += SHA1_DIGEST_STRING_LENGTH;
605 } else {
606 err = got_error(GOT_ERR_BAD_OBJ_DATA);
607 goto done;
610 tlen = strlen(GOT_COMMIT_TAG_PARENT);
611 while (strncmp(s, GOT_COMMIT_TAG_PARENT, tlen) == 0) {
612 remain -= tlen;
613 if (remain < SHA1_DIGEST_STRING_LENGTH) {
614 err = got_error(GOT_ERR_BAD_OBJ_DATA);
615 goto done;
617 s += tlen;
618 err = got_object_commit_add_parent(*commit, s);
619 if (err)
620 goto done;
622 remain -= SHA1_DIGEST_STRING_LENGTH;
623 s += SHA1_DIGEST_STRING_LENGTH;
626 tlen = strlen(GOT_COMMIT_TAG_AUTHOR);
627 if (strncmp(s, GOT_COMMIT_TAG_AUTHOR, tlen) == 0) {
628 char *p;
629 size_t slen;
631 remain -= tlen;
632 if (remain <= 0) {
633 err = got_error(GOT_ERR_BAD_OBJ_DATA);
634 goto done;
636 s += tlen;
637 p = strchr(s, '\n');
638 if (p == NULL) {
639 err = got_error(GOT_ERR_BAD_OBJ_DATA);
640 goto done;
642 *p = '\0';
643 slen = strlen(s);
644 err = parse_commit_time(&(*commit)->tm_author, s);
645 if (err)
646 goto done;
647 (*commit)->author = strdup(s);
648 if ((*commit)->author == NULL) {
649 err = got_error_from_errno();
650 goto done;
652 s += slen + 1;
653 remain -= slen + 1;
656 tlen = strlen(GOT_COMMIT_TAG_COMMITTER);
657 if (strncmp(s, GOT_COMMIT_TAG_COMMITTER, tlen) == 0) {
658 char *p;
659 size_t slen;
661 remain -= tlen;
662 if (remain <= 0) {
663 err = got_error(GOT_ERR_BAD_OBJ_DATA);
664 goto done;
666 s += tlen;
667 p = strchr(s, '\n');
668 if (p == NULL) {
669 err = got_error(GOT_ERR_BAD_OBJ_DATA);
670 goto done;
672 *p = '\0';
673 slen = strlen(s);
674 err = parse_commit_time(&(*commit)->tm_committer, s);
675 if (err)
676 goto done;
677 (*commit)->committer = strdup(s);
678 if ((*commit)->committer == NULL) {
679 err = got_error_from_errno();
680 goto done;
682 s += slen + 1;
683 remain -= slen + 1;
686 (*commit)->logmsg = strndup(s, remain);
687 if ((*commit)->logmsg == NULL) {
688 err = got_error_from_errno();
689 goto done;
691 done:
692 if (err) {
693 got_object_commit_close(*commit);
694 *commit = NULL;
696 return err;
699 static void
700 tree_entry_close(struct got_tree_entry *te)
702 free(te->id);
703 free(te->name);
704 free(te);
707 struct got_tree_entry *
708 got_alloc_tree_entry_partial(void)
710 struct got_tree_entry *te;
712 te = calloc(1, sizeof(*te));
713 if (te == NULL)
714 return NULL;
716 te->id = calloc(1, sizeof(*te->id));
717 if (te->id == NULL) {
718 free(te);
719 te = NULL;
721 return te;
724 static const struct got_error *
725 parse_tree_entry(struct got_tree_entry **te, size_t *elen, char *buf,
726 size_t maxlen)
728 char *p = buf, *space;
729 const struct got_error *err = NULL;
731 *te = got_alloc_tree_entry_partial();
732 if (*te == NULL)
733 return got_error_from_errno();
735 *elen = strlen(buf) + 1;
736 if (*elen > maxlen) {
737 free(*te);
738 *te = NULL;
739 return got_error(GOT_ERR_BAD_OBJ_DATA);
742 space = strchr(buf, ' ');
743 if (space == NULL) {
744 err = got_error(GOT_ERR_BAD_OBJ_DATA);
745 free(*te);
746 *te = NULL;
747 return err;
749 while (*p != ' ') {
750 if (*p < '0' && *p > '7') {
751 err = got_error(GOT_ERR_BAD_OBJ_DATA);
752 goto done;
754 (*te)->mode <<= 3;
755 (*te)->mode |= *p - '0';
756 p++;
759 (*te)->name = strdup(space + 1);
760 if (*elen > maxlen || maxlen - *elen < SHA1_DIGEST_LENGTH) {
761 err = got_error(GOT_ERR_BAD_OBJ_DATA);
762 goto done;
764 buf += strlen(buf) + 1;
765 memcpy((*te)->id->sha1, buf, SHA1_DIGEST_LENGTH);
766 *elen += SHA1_DIGEST_LENGTH;
767 done:
768 if (err) {
769 tree_entry_close(*te);
770 *te = NULL;
772 return err;
775 static const struct got_error *
776 parse_tree_object(struct got_tree_object **tree, uint8_t *buf, size_t len)
778 const struct got_error *err;
779 size_t remain = len;
781 *tree = calloc(1, sizeof(**tree));
782 if (*tree == NULL)
783 return got_error_from_errno();
785 SIMPLEQ_INIT(&(*tree)->entries.head);
787 while (remain > 0) {
788 struct got_tree_entry *te;
789 size_t elen;
791 err = parse_tree_entry(&te, &elen, buf, remain);
792 if (err)
793 return err;
794 (*tree)->entries.nentries++;
795 SIMPLEQ_INSERT_TAIL(&(*tree)->entries.head, te, entry);
796 buf += elen;
797 remain -= elen;
800 if (remain != 0) {
801 got_object_tree_close(*tree);
802 return got_error(GOT_ERR_BAD_OBJ_DATA);
805 return NULL;
808 static const struct got_error *
809 read_to_mem(uint8_t **outbuf, size_t *outlen, FILE *f)
811 const struct got_error *err = NULL;
812 static const size_t blocksize = 512;
813 size_t n, total, remain;
814 uint8_t *buf;
816 *outbuf = NULL;
817 *outlen = 0;
819 buf = calloc(1, blocksize);
820 if (buf == NULL)
821 return got_error_from_errno();
823 remain = blocksize;
824 total = 0;
825 while (1) {
826 if (remain == 0) {
827 uint8_t *newbuf;
828 newbuf = reallocarray(buf, 1, total + blocksize);
829 if (newbuf == NULL) {
830 err = got_error_from_errno();
831 goto done;
833 buf = newbuf;
834 remain += blocksize;
836 n = fread(buf + total, 1, remain, f);
837 if (n == 0) {
838 if (ferror(f)) {
839 err = got_ferror(f, GOT_ERR_IO);
840 goto done;
842 break; /* EOF */
844 remain -= n;
845 total += n;
846 };
848 done:
849 if (err == NULL) {
850 *outbuf = buf;
851 *outlen = total;
852 } else
853 free(buf);
854 return err;
857 static const struct got_error *
858 read_commit_object(struct got_commit_object **commit, struct got_object *obj,
859 FILE *f)
861 const struct got_error *err = NULL;
862 size_t len;
863 uint8_t *p;
865 if (obj->flags & GOT_OBJ_FLAG_PACKED)
866 err = read_to_mem(&p, &len, f);
867 else
868 err = got_inflate_to_mem(&p, &len, f);
869 if (err)
870 return err;
872 if (len < obj->hdrlen + obj->size) {
873 err = got_error(GOT_ERR_BAD_OBJ_DATA);
874 goto done;
877 /* Skip object header. */
878 len -= obj->hdrlen;
879 err = parse_commit_object(commit, p + obj->hdrlen, len);
880 free(p);
881 done:
882 return err;
885 static void
886 read_commit_object_privsep_child(struct got_object *obj, int obj_fd,
887 int imsg_fds[2])
889 const struct got_error *err = NULL;
890 struct got_commit_object *commit = NULL;
891 struct imsgbuf ibuf;
892 FILE *f = NULL;
893 int status = 0;
895 setproctitle("read commit object");
896 close(imsg_fds[0]);
897 imsg_init(&ibuf, imsg_fds[1]);
899 /* revoke access to most system calls */
900 if (pledge("stdio", NULL) == -1) {
901 err = got_error_from_errno();
902 goto done;
905 f = fdopen(obj_fd, "rb");
906 if (f == NULL) {
907 err = got_error_from_errno();
908 close(obj_fd);
909 goto done;
912 err = read_commit_object(&commit, obj, f);
913 if (err)
914 goto done;
916 err = got_privsep_send_commit(&ibuf, commit);
917 done:
918 if (commit)
919 got_object_commit_close(commit);
920 if (err) {
921 got_privsep_send_error(&ibuf, err);
922 status = 1;
924 if (f)
925 fclose(f);
926 imsg_clear(&ibuf);
927 close(imsg_fds[1]);
928 _exit(status);
931 static const struct got_error *
932 read_commit_object_privsep(struct got_commit_object **commit,
933 struct got_repository *repo, struct got_object *obj, int fd)
935 const struct got_error *err = NULL, *err_child = NULL;
936 struct imsgbuf parent_ibuf;
937 int imsg_fds[2];
938 pid_t pid;
940 if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_fds) == -1)
941 return got_error_from_errno();
943 pid = fork();
944 if (pid == -1)
945 return got_error_from_errno();
946 else if (pid == 0) {
947 read_commit_object_privsep_child(obj, fd, imsg_fds);
948 /* not reached */
951 close(imsg_fds[1]);
952 imsg_init(&parent_ibuf, imsg_fds[0]);
953 err = got_privsep_recv_commit(commit, &parent_ibuf);
954 imsg_clear(&parent_ibuf);
955 err_child = wait_for_child(pid);
956 close(imsg_fds[0]);
957 return err ? err : err_child;
960 const struct got_error *
961 got_object_commit_open(struct got_commit_object **commit,
962 struct got_repository *repo, struct got_object *obj)
964 const struct got_error *err = NULL;
966 *commit = got_repo_get_cached_commit(repo, &obj->id);
967 if (*commit != NULL) {
968 (*commit)->refcnt++;
969 return NULL;
972 if (obj->type != GOT_OBJ_TYPE_COMMIT)
973 return got_error(GOT_ERR_OBJ_TYPE);
975 if (obj->flags & GOT_OBJ_FLAG_PACKED) {
976 uint8_t *buf;
977 size_t len;
978 err = got_packfile_extract_object_to_mem(&buf, &len, obj, repo);
979 if (err)
980 return err;
981 obj->size = len;
982 err = parse_commit_object(commit, buf, len);
983 free(buf);
984 } else {
985 int fd;
986 err = open_loose_object(&fd, obj, repo);
987 if (err)
988 return err;
989 err = read_commit_object_privsep(commit, repo, obj, fd);
990 close(fd);
993 if (err == NULL) {
994 (*commit)->refcnt++;
995 err = got_repo_cache_commit(repo, &obj->id, *commit);
998 return err;
1001 void
1002 got_object_commit_close(struct got_commit_object *commit)
1004 struct got_object_qid *qid;
1006 if (commit->refcnt > 0) {
1007 commit->refcnt--;
1008 if (commit->refcnt > 0)
1009 return;
1012 while (!SIMPLEQ_EMPTY(&commit->parent_ids)) {
1013 qid = SIMPLEQ_FIRST(&commit->parent_ids);
1014 SIMPLEQ_REMOVE_HEAD(&commit->parent_ids, entry);
1015 free(qid->id);
1016 free(qid);
1019 free(commit->tree_id);
1020 free(commit->author);
1021 free(commit->committer);
1022 free(commit->logmsg);
1023 free(commit);
1026 static const struct got_error *
1027 read_tree_object(struct got_tree_object **tree, struct got_object *obj, FILE *f)
1029 const struct got_error *err = NULL;
1030 size_t len;
1031 uint8_t *p;
1033 if (obj->flags & GOT_OBJ_FLAG_PACKED)
1034 err = read_to_mem(&p, &len, f);
1035 else
1036 err = got_inflate_to_mem(&p, &len, f);
1037 if (err)
1038 return err;
1040 if (len < obj->hdrlen + obj->size) {
1041 err = got_error(GOT_ERR_BAD_OBJ_DATA);
1042 goto done;
1045 /* Skip object header. */
1046 len -= obj->hdrlen;
1047 err = parse_tree_object(tree, p + obj->hdrlen, len);
1048 free(p);
1049 done:
1050 return err;
1053 static void
1054 read_tree_object_privsep_child(struct got_object *obj, int obj_fd,
1055 int imsg_fds[2])
1057 const struct got_error *err = NULL;
1058 struct got_tree_object *tree = NULL;
1059 struct imsgbuf ibuf;
1060 FILE *f = NULL;
1061 int status = 0;
1063 setproctitle("read tree object");
1064 close(imsg_fds[0]);
1065 imsg_init(&ibuf, imsg_fds[1]);
1067 /* revoke access to most system calls */
1068 if (pledge("stdio", NULL) == -1) {
1069 err = got_error_from_errno();
1070 goto done;
1073 f = fdopen(obj_fd, "rb");
1074 if (f == NULL) {
1075 err = got_error_from_errno();
1076 close(obj_fd);
1077 goto done;
1080 err = read_tree_object(&tree, obj, f);
1081 if (err)
1082 goto done;
1084 err = got_privsep_send_tree(&ibuf, tree);
1085 done:
1086 if (tree)
1087 got_object_tree_close(tree);
1088 if (err) {
1089 got_privsep_send_error(&ibuf, err);
1090 status = 1;
1092 if (f)
1093 fclose(f);
1094 imsg_clear(&ibuf);
1095 close(imsg_fds[1]);
1096 _exit(status);
1099 static const struct got_error *
1100 read_tree_object_privsep(struct got_tree_object **tree, struct got_object *obj,
1101 int fd)
1103 const struct got_error *err = NULL, *err_child = NULL;
1104 struct imsgbuf parent_ibuf;
1105 int imsg_fds[2];
1106 pid_t pid;
1108 if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_fds) == -1)
1109 return got_error_from_errno();
1111 pid = fork();
1112 if (pid == -1)
1113 return got_error_from_errno();
1114 else if (pid == 0) {
1115 read_tree_object_privsep_child(obj, fd, imsg_fds);
1116 /* not reached */
1119 close(imsg_fds[1]);
1120 imsg_init(&parent_ibuf, imsg_fds[0]);
1121 err = got_privsep_recv_tree(tree, &parent_ibuf);
1122 imsg_clear(&parent_ibuf);
1123 err_child = wait_for_child(pid);
1124 close(imsg_fds[0]);
1125 return err ? err : err_child;
1128 const struct got_error *
1129 got_object_tree_open(struct got_tree_object **tree,
1130 struct got_repository *repo, struct got_object *obj)
1132 const struct got_error *err = NULL;
1134 *tree = got_repo_get_cached_tree(repo, &obj->id);
1135 if (*tree != NULL) {
1136 (*tree)->refcnt++;
1137 return NULL;
1140 if (obj->type != GOT_OBJ_TYPE_TREE)
1141 return got_error(GOT_ERR_OBJ_TYPE);
1143 if (obj->flags & GOT_OBJ_FLAG_PACKED) {
1144 uint8_t *buf;
1145 size_t len;
1146 err = got_packfile_extract_object_to_mem(&buf, &len, obj, repo);
1147 if (err)
1148 return err;
1149 obj->size = len;
1150 err = parse_tree_object(tree, buf, len);
1151 free(buf);
1152 } else {
1153 int fd;
1154 err = open_loose_object(&fd, obj, repo);
1155 if (err)
1156 return err;
1157 err = read_tree_object_privsep(tree, obj, fd);
1158 close(fd);
1161 if (err == NULL) {
1162 (*tree)->refcnt++;
1163 err = got_repo_cache_tree(repo, &obj->id, *tree);
1166 return err;
1169 const struct got_error *
1170 got_object_open_as_tree(struct got_tree_object **tree,
1171 struct got_repository *repo, struct got_object_id *id)
1173 const struct got_error *err;
1174 struct got_object *obj;
1176 *tree = NULL;
1178 err = got_object_open(&obj, repo, id);
1179 if (err)
1180 return err;
1181 if (got_object_get_type(obj) != GOT_OBJ_TYPE_TREE) {
1182 err = got_error(GOT_ERR_OBJ_TYPE);
1183 goto done;
1186 err = got_object_tree_open(tree, repo, obj);
1187 done:
1188 got_object_close(obj);
1189 return err;
1192 void
1193 got_object_tree_close(struct got_tree_object *tree)
1195 struct got_tree_entry *te;
1197 if (tree->refcnt > 0) {
1198 tree->refcnt--;
1199 if (tree->refcnt > 0)
1200 return;
1203 while (!SIMPLEQ_EMPTY(&tree->entries.head)) {
1204 te = SIMPLEQ_FIRST(&tree->entries.head);
1205 SIMPLEQ_REMOVE_HEAD(&tree->entries.head, entry);
1206 tree_entry_close(te);
1209 free(tree);
1212 const struct got_tree_entries *
1213 got_object_tree_get_entries(struct got_tree_object *tree)
1215 return &tree->entries;
1218 static const struct got_error *
1219 read_blob_object_privsep_child(int outfd, int infd, int imsg_fds[2])
1221 const struct got_error *err = NULL;
1222 struct imsgbuf ibuf;
1223 int status = 0;
1224 size_t size;
1225 FILE *infile = NULL;
1227 setproctitle("read blob object");
1228 close(imsg_fds[0]);
1229 imsg_init(&ibuf, imsg_fds[1]);
1231 /* revoke access to most system calls */
1232 if (pledge("stdio", NULL) == -1) {
1233 err = got_error_from_errno();
1234 goto done;
1237 infile = fdopen(infd, "rb");
1238 if (infile == NULL) {
1239 err = got_error_from_errno();
1240 close(infd);
1241 goto done;
1243 err = got_inflate_to_fd(&size, infile, outfd);
1244 fclose(infile);
1245 if (err)
1246 goto done;
1248 err = got_privsep_send_blob(&ibuf, size);
1249 done:
1250 if (err) {
1251 got_privsep_send_error(&ibuf, err);
1252 status = 1;
1254 close(outfd);
1255 imsg_clear(&ibuf);
1256 close(imsg_fds[1]);
1257 _exit(status);
1260 static const struct got_error *
1261 read_blob_object_privsep(size_t *size, int outfd, int infd)
1263 struct imsgbuf parent_ibuf;
1264 int imsg_fds[2];
1265 const struct got_error *err = NULL, *err_child = NULL;
1266 pid_t pid;
1268 if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_fds) == -1)
1269 return got_error_from_errno();
1271 pid = fork();
1272 if (pid == -1)
1273 return got_error_from_errno();
1274 else if (pid == 0) {
1275 read_blob_object_privsep_child(outfd, infd, imsg_fds);
1276 /* not reached */
1279 close(imsg_fds[1]);
1280 imsg_init(&parent_ibuf, imsg_fds[0]);
1281 err = got_privsep_recv_blob(size, &parent_ibuf);
1282 imsg_clear(&parent_ibuf);
1283 err_child = wait_for_child(pid);
1284 close(imsg_fds[0]);
1285 if (lseek(outfd, SEEK_SET, 0) == -1)
1286 err = got_error_from_errno();
1287 return err ? err : err_child;
1290 const struct got_error *
1291 got_object_blob_open(struct got_blob_object **blob,
1292 struct got_repository *repo, struct got_object *obj, size_t blocksize)
1294 const struct got_error *err = NULL;
1296 if (obj->type != GOT_OBJ_TYPE_BLOB)
1297 return got_error(GOT_ERR_OBJ_TYPE);
1299 if (blocksize < obj->hdrlen)
1300 return got_error(GOT_ERR_NO_SPACE);
1302 *blob = calloc(1, sizeof(**blob));
1303 if (*blob == NULL)
1304 return got_error_from_errno();
1306 (*blob)->read_buf = calloc(1, blocksize);
1307 if ((*blob)->read_buf == NULL) {
1308 err = got_error_from_errno();
1309 goto done;
1311 if (obj->flags & GOT_OBJ_FLAG_PACKED) {
1312 err = got_packfile_extract_object(&((*blob)->f), obj, repo);
1313 if (err)
1314 goto done;
1315 } else {
1316 int infd, outfd;
1317 size_t size;
1318 struct stat sb;
1320 err = open_loose_object(&infd, obj, repo);
1321 if (err)
1322 goto done;
1325 outfd = got_opentempfd();
1326 if (outfd == -1) {
1327 err = got_error_from_errno();
1328 close(infd);
1329 goto done;
1332 err = read_blob_object_privsep(&size, outfd, infd);
1333 close(infd);
1334 if (err)
1335 goto done;
1337 if (size != obj->hdrlen + obj->size) {
1338 err = got_error(GOT_ERR_PRIVSEP_LEN);
1339 close(outfd);
1340 goto done;
1343 if (fstat(outfd, &sb) == -1) {
1344 err = got_error_from_errno();
1345 close(outfd);
1346 goto done;
1349 if (sb.st_size != size) {
1350 err = got_error(GOT_ERR_PRIVSEP_LEN);
1351 close(outfd);
1352 goto done;
1355 (*blob)->f = fdopen(outfd, "rb");
1356 if ((*blob)->f == NULL) {
1357 err = got_error_from_errno();
1358 close(outfd);
1359 goto done;
1363 (*blob)->hdrlen = obj->hdrlen;
1364 (*blob)->blocksize = blocksize;
1365 memcpy(&(*blob)->id.sha1, obj->id.sha1, SHA1_DIGEST_LENGTH);
1367 done:
1368 if (err && *blob) {
1369 if ((*blob)->f)
1370 fclose((*blob)->f);
1371 free((*blob)->read_buf);
1372 free(*blob);
1373 *blob = NULL;
1375 return err;
1378 const struct got_error *
1379 got_object_open_as_blob(struct got_blob_object **blob,
1380 struct got_repository *repo, struct got_object_id *id,
1381 size_t blocksize)
1383 const struct got_error *err;
1384 struct got_object *obj;
1386 *blob = NULL;
1388 err = got_object_open(&obj, repo, id);
1389 if (err)
1390 return err;
1391 if (got_object_get_type(obj) != GOT_OBJ_TYPE_BLOB) {
1392 err = got_error(GOT_ERR_OBJ_TYPE);
1393 goto done;
1396 err = got_object_blob_open(blob, repo, obj, blocksize);
1397 done:
1398 got_object_close(obj);
1399 return err;
1402 void
1403 got_object_blob_close(struct got_blob_object *blob)
1405 free(blob->read_buf);
1406 fclose(blob->f);
1407 free(blob);
1410 char *
1411 got_object_blob_id_str(struct got_blob_object *blob, char *buf, size_t size)
1413 return got_sha1_digest_to_str(blob->id.sha1, buf, size);
1416 size_t
1417 got_object_blob_get_hdrlen(struct got_blob_object *blob)
1419 return blob->hdrlen;
1422 const uint8_t *
1423 got_object_blob_get_read_buf(struct got_blob_object *blob)
1425 return blob->read_buf;
1428 const struct got_error *
1429 got_object_blob_read_block(size_t *outlenp, struct got_blob_object *blob)
1431 size_t n;
1433 n = fread(blob->read_buf, 1, blob->blocksize, blob->f);
1434 if (n == 0 && ferror(blob->f))
1435 return got_ferror(blob->f, GOT_ERR_IO);
1436 *outlenp = n;
1437 return NULL;
1440 const struct got_error *
1441 got_object_blob_dump_to_file(size_t *total_len, size_t *nlines,
1442 FILE *outfile, struct got_blob_object *blob)
1444 const struct got_error *err = NULL;
1445 size_t len, hdrlen;
1446 const uint8_t *buf;
1447 int i;
1449 if (total_len)
1450 *total_len = 0;
1451 if (nlines)
1452 *nlines = 0;
1454 hdrlen = got_object_blob_get_hdrlen(blob);
1455 do {
1456 err = got_object_blob_read_block(&len, blob);
1457 if (err)
1458 return err;
1459 if (len == 0)
1460 break;
1461 if (total_len)
1462 *total_len += len;
1463 buf = got_object_blob_get_read_buf(blob);
1464 if (nlines) {
1465 for (i = 0; i < len; i++) {
1466 if (buf[i] == '\n')
1467 (*nlines)++;
1470 /* Skip blob object header first time around. */
1471 fwrite(buf + hdrlen, len - hdrlen, 1, outfile);
1472 hdrlen = 0;
1473 } while (len != 0);
1475 fflush(outfile);
1476 rewind(outfile);
1478 return NULL;
1481 static struct got_tree_entry *
1482 find_entry_by_name(struct got_tree_object *tree, const char *name)
1484 struct got_tree_entry *te;
1486 SIMPLEQ_FOREACH(te, &tree->entries.head, entry) {
1487 if (strcmp(te->name, name) == 0)
1488 return te;
1490 return NULL;
1493 const struct got_error *
1494 got_object_open_by_path(struct got_object **obj, struct got_repository *repo,
1495 struct got_object_id *commit_id, const char *path)
1497 const struct got_error *err = NULL;
1498 struct got_commit_object *commit = NULL;
1499 struct got_tree_object *tree = NULL;
1500 struct got_tree_entry *te = NULL;
1501 char *seg, *s, *s0 = NULL;
1502 size_t len = strlen(path);
1504 *obj = NULL;
1506 /* We are expecting an absolute in-repository path. */
1507 if (path[0] != '/')
1508 return got_error(GOT_ERR_NOT_ABSPATH);
1510 err = got_object_open_as_commit(&commit, repo, commit_id);
1511 if (err)
1512 goto done;
1514 /* Handle opening of root of commit's tree. */
1515 if (path[1] == '\0') {
1516 err = got_object_open(obj, repo, commit->tree_id);
1517 if (err)
1518 goto done;
1519 return NULL;
1522 err = got_object_open_as_tree(&tree, repo, commit->tree_id);
1523 if (err)
1524 goto done;
1526 s0 = strdup(path);
1527 if (s0 == NULL) {
1528 err = got_error_from_errno();
1529 goto done;
1531 err = got_canonpath(path, s0, len + 1);
1532 if (err)
1533 goto done;
1535 s = s0;
1536 s++; /* skip leading '/' */
1537 len--;
1538 seg = s;
1539 while (len > 0) {
1540 struct got_tree_object *next_tree;
1542 if (*s != '/') {
1543 s++;
1544 len--;
1545 if (*s)
1546 continue;
1549 /* end of path segment */
1550 *s = '\0';
1552 te = find_entry_by_name(tree, seg);
1553 if (te == NULL) {
1554 err = got_error(GOT_ERR_NO_OBJ);
1555 goto done;
1558 if (len == 0)
1559 break;
1561 seg = s + 1;
1562 s++;
1563 len--;
1564 if (*s) {
1565 err = got_object_open_as_tree(&next_tree, repo,
1566 te->id);
1567 te = NULL;
1568 if (err)
1569 goto done;
1570 got_object_tree_close(tree);
1571 tree = next_tree;
1575 if (te) {
1576 err = got_object_open(obj, repo, te->id);
1577 if (err)
1578 goto done;
1579 } else
1580 err = got_error(GOT_ERR_NO_OBJ);
1581 done:
1582 free(s0);
1583 if (commit)
1584 got_object_commit_close(commit);
1585 if (tree)
1586 got_object_tree_close(tree);
1587 return err;