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 if (obj->refcnt > 0)
427 return;
430 if (obj->flags & GOT_OBJ_FLAG_DELTIFIED) {
431 struct got_delta *delta;
432 while (!SIMPLEQ_EMPTY(&obj->deltas.entries)) {
433 delta = SIMPLEQ_FIRST(&obj->deltas.entries);
434 SIMPLEQ_REMOVE_HEAD(&obj->deltas.entries, entry);
435 got_delta_close(delta);
438 if (obj->flags & GOT_OBJ_FLAG_PACKED)
439 free(obj->path_packfile);
440 free(obj);
443 struct got_commit_object *
444 got_object_commit_alloc_partial(void)
446 struct got_commit_object *commit;
448 commit = calloc(1, sizeof(*commit));
449 if (commit == NULL)
450 return NULL;
451 commit->tree_id = calloc(1, sizeof(*commit->tree_id));
452 if (commit->tree_id == NULL) {
453 free(commit);
454 return NULL;
457 SIMPLEQ_INIT(&commit->parent_ids);
459 return commit;
462 const struct got_error *
463 got_object_open_as_commit(struct got_commit_object **commit,
464 struct got_repository *repo, struct got_object_id *id)
466 const struct got_error *err;
467 struct got_object *obj;
469 *commit = NULL;
471 err = got_object_open(&obj, repo, id);
472 if (err)
473 return err;
474 if (got_object_get_type(obj) != GOT_OBJ_TYPE_COMMIT) {
475 err = got_error(GOT_ERR_OBJ_TYPE);
476 goto done;
479 err = got_object_commit_open(commit, repo, obj);
480 done:
481 got_object_close(obj);
482 return err;
485 const struct got_error *
486 got_object_commit_add_parent(struct got_commit_object *commit,
487 const char *id_str)
489 const struct got_error *err = NULL;
490 struct got_object_qid *qid;
492 qid = calloc(1, sizeof(*qid));
493 if (qid == NULL)
494 return got_error_from_errno();
496 qid->id = calloc(1, sizeof(*qid->id));
497 if (qid->id == NULL) {
498 err = got_error_from_errno();
499 free(qid);
500 return err;
503 if (!got_parse_sha1_digest(qid->id->sha1, id_str)) {
504 err = got_error(GOT_ERR_BAD_OBJ_DATA);
505 free(qid->id);
506 free(qid);
507 return err;
510 SIMPLEQ_INSERT_TAIL(&commit->parent_ids, qid, entry);
511 commit->nparents++;
513 return NULL;
516 static const struct got_error *
517 parse_gmtoff(time_t *gmtoff, const char *tzstr)
519 int sign = 1;
520 const char *p = tzstr;
521 time_t h, m;
523 *gmtoff = 0;
525 if (*p == '-')
526 sign = -1;
527 else if (*p != '+')
528 return got_error(GOT_ERR_BAD_OBJ_DATA);
529 p++;
530 if (!isdigit(*p) && !isdigit(*(p + 1)))
531 return got_error(GOT_ERR_BAD_OBJ_DATA);
532 h = (((*p - '0') * 10) + (*(p + 1) - '0'));
534 p += 2;
535 if (!isdigit(*p) && !isdigit(*(p + 1)))
536 return got_error(GOT_ERR_BAD_OBJ_DATA);
537 m = ((*p - '0') * 10) + (*(p + 1) - '0');
539 *gmtoff = (h * 60 * 60 + m * 60) * sign;
540 return NULL;
543 static const struct got_error *
544 parse_commit_time(struct tm *tm, char *committer)
546 const struct got_error *err = NULL;
547 const char *errstr;
548 char *space, *tzstr;
549 time_t gmtoff;
550 time_t time;
552 /* Parse and strip off trailing timezone indicator string. */
553 space = strrchr(committer, ' ');
554 if (space == NULL)
555 return got_error(GOT_ERR_BAD_OBJ_DATA);
556 tzstr = strdup(space + 1);
557 if (tzstr == NULL)
558 return got_error_from_errno();
559 err = parse_gmtoff(&gmtoff, tzstr);
560 free(tzstr);
561 if (err)
562 return err;
563 *space = '\0';
565 /* Timestamp is separated from committer name + email by space. */
566 space = strrchr(committer, ' ');
567 if (space == NULL)
568 return got_error(GOT_ERR_BAD_OBJ_DATA);
570 /* Timestamp parsed here is expressed in comitter's local time. */
571 time = strtonum(space + 1, 0, INT64_MAX, &errstr);
572 if (errstr)
573 return got_error(GOT_ERR_BAD_OBJ_DATA);
575 /* Express the time stamp in UTC. */
576 memset(tm, 0, sizeof(*tm));
577 time -= gmtoff;
578 if (localtime_r(&time, tm) == NULL)
579 return got_error_from_errno();
580 tm->tm_gmtoff = gmtoff;
582 /* Strip off parsed time information, leaving just author and email. */
583 *space = '\0';
585 return NULL;
588 static const struct got_error *
589 parse_commit_object(struct got_commit_object **commit, char *buf, size_t len)
591 const struct got_error *err = NULL;
592 char *s = buf;
593 size_t tlen;
594 ssize_t remain = (ssize_t)len;
596 *commit = got_object_commit_alloc_partial();
597 if (*commit == NULL)
598 return got_error_from_errno();
600 tlen = strlen(GOT_COMMIT_TAG_TREE);
601 if (strncmp(s, GOT_COMMIT_TAG_TREE, tlen) == 0) {
602 remain -= tlen;
603 if (remain < SHA1_DIGEST_STRING_LENGTH) {
604 err = got_error(GOT_ERR_BAD_OBJ_DATA);
605 goto done;
607 s += tlen;
608 if (!got_parse_sha1_digest((*commit)->tree_id->sha1, s)) {
609 err = got_error(GOT_ERR_BAD_OBJ_DATA);
610 goto done;
612 remain -= SHA1_DIGEST_STRING_LENGTH;
613 s += SHA1_DIGEST_STRING_LENGTH;
614 } else {
615 err = got_error(GOT_ERR_BAD_OBJ_DATA);
616 goto done;
619 tlen = strlen(GOT_COMMIT_TAG_PARENT);
620 while (strncmp(s, GOT_COMMIT_TAG_PARENT, tlen) == 0) {
621 remain -= tlen;
622 if (remain < SHA1_DIGEST_STRING_LENGTH) {
623 err = got_error(GOT_ERR_BAD_OBJ_DATA);
624 goto done;
626 s += tlen;
627 err = got_object_commit_add_parent(*commit, s);
628 if (err)
629 goto done;
631 remain -= SHA1_DIGEST_STRING_LENGTH;
632 s += SHA1_DIGEST_STRING_LENGTH;
635 tlen = strlen(GOT_COMMIT_TAG_AUTHOR);
636 if (strncmp(s, GOT_COMMIT_TAG_AUTHOR, tlen) == 0) {
637 char *p;
638 size_t slen;
640 remain -= tlen;
641 if (remain <= 0) {
642 err = got_error(GOT_ERR_BAD_OBJ_DATA);
643 goto done;
645 s += tlen;
646 p = strchr(s, '\n');
647 if (p == NULL) {
648 err = got_error(GOT_ERR_BAD_OBJ_DATA);
649 goto done;
651 *p = '\0';
652 slen = strlen(s);
653 err = parse_commit_time(&(*commit)->tm_author, s);
654 if (err)
655 goto done;
656 (*commit)->author = strdup(s);
657 if ((*commit)->author == NULL) {
658 err = got_error_from_errno();
659 goto done;
661 s += slen + 1;
662 remain -= slen + 1;
665 tlen = strlen(GOT_COMMIT_TAG_COMMITTER);
666 if (strncmp(s, GOT_COMMIT_TAG_COMMITTER, tlen) == 0) {
667 char *p;
668 size_t slen;
670 remain -= tlen;
671 if (remain <= 0) {
672 err = got_error(GOT_ERR_BAD_OBJ_DATA);
673 goto done;
675 s += tlen;
676 p = strchr(s, '\n');
677 if (p == NULL) {
678 err = got_error(GOT_ERR_BAD_OBJ_DATA);
679 goto done;
681 *p = '\0';
682 slen = strlen(s);
683 err = parse_commit_time(&(*commit)->tm_committer, s);
684 if (err)
685 goto done;
686 (*commit)->committer = strdup(s);
687 if ((*commit)->committer == NULL) {
688 err = got_error_from_errno();
689 goto done;
691 s += slen + 1;
692 remain -= slen + 1;
695 (*commit)->logmsg = strndup(s, remain);
696 if ((*commit)->logmsg == NULL) {
697 err = got_error_from_errno();
698 goto done;
700 done:
701 if (err) {
702 got_object_commit_close(*commit);
703 *commit = NULL;
705 return err;
708 static void
709 tree_entry_close(struct got_tree_entry *te)
711 free(te->id);
712 free(te->name);
713 free(te);
716 struct got_tree_entry *
717 got_alloc_tree_entry_partial(void)
719 struct got_tree_entry *te;
721 te = calloc(1, sizeof(*te));
722 if (te == NULL)
723 return NULL;
725 te->id = calloc(1, sizeof(*te->id));
726 if (te->id == NULL) {
727 free(te);
728 te = NULL;
730 return te;
733 static const struct got_error *
734 parse_tree_entry(struct got_tree_entry **te, size_t *elen, char *buf,
735 size_t maxlen)
737 char *p = buf, *space;
738 const struct got_error *err = NULL;
740 *te = got_alloc_tree_entry_partial();
741 if (*te == NULL)
742 return got_error_from_errno();
744 *elen = strlen(buf) + 1;
745 if (*elen > maxlen) {
746 free(*te);
747 *te = NULL;
748 return got_error(GOT_ERR_BAD_OBJ_DATA);
751 space = strchr(buf, ' ');
752 if (space == NULL) {
753 err = got_error(GOT_ERR_BAD_OBJ_DATA);
754 free(*te);
755 *te = NULL;
756 return err;
758 while (*p != ' ') {
759 if (*p < '0' && *p > '7') {
760 err = got_error(GOT_ERR_BAD_OBJ_DATA);
761 goto done;
763 (*te)->mode <<= 3;
764 (*te)->mode |= *p - '0';
765 p++;
768 (*te)->name = strdup(space + 1);
769 if (*elen > maxlen || maxlen - *elen < SHA1_DIGEST_LENGTH) {
770 err = got_error(GOT_ERR_BAD_OBJ_DATA);
771 goto done;
773 buf += strlen(buf) + 1;
774 memcpy((*te)->id->sha1, buf, SHA1_DIGEST_LENGTH);
775 *elen += SHA1_DIGEST_LENGTH;
776 done:
777 if (err) {
778 tree_entry_close(*te);
779 *te = NULL;
781 return err;
784 static const struct got_error *
785 parse_tree_object(struct got_tree_object **tree, uint8_t *buf, size_t len)
787 const struct got_error *err;
788 size_t remain = len;
790 *tree = calloc(1, sizeof(**tree));
791 if (*tree == NULL)
792 return got_error_from_errno();
794 SIMPLEQ_INIT(&(*tree)->entries);
796 while (remain > 0) {
797 struct got_tree_entry *te;
798 size_t elen;
800 err = parse_tree_entry(&te, &elen, buf, remain);
801 if (err)
802 return err;
803 (*tree)->nentries++;
804 SIMPLEQ_INSERT_TAIL(&(*tree)->entries, te, entry);
805 buf += elen;
806 remain -= elen;
809 if (remain != 0) {
810 got_object_tree_close(*tree);
811 return got_error(GOT_ERR_BAD_OBJ_DATA);
814 return NULL;
817 static const struct got_error *
818 read_to_mem(uint8_t **outbuf, size_t *outlen, FILE *f)
820 const struct got_error *err = NULL;
821 static const size_t blocksize = 512;
822 size_t n, total, remain;
823 uint8_t *buf;
825 *outbuf = NULL;
826 *outlen = 0;
828 buf = calloc(1, blocksize);
829 if (buf == NULL)
830 return got_error_from_errno();
832 remain = blocksize;
833 total = 0;
834 while (1) {
835 if (remain == 0) {
836 uint8_t *newbuf;
837 newbuf = reallocarray(buf, 1, total + blocksize);
838 if (newbuf == NULL) {
839 err = got_error_from_errno();
840 goto done;
842 buf = newbuf;
843 remain += blocksize;
845 n = fread(buf + total, 1, remain, f);
846 if (n == 0) {
847 if (ferror(f)) {
848 err = got_ferror(f, GOT_ERR_IO);
849 goto done;
851 break; /* EOF */
853 remain -= n;
854 total += n;
855 };
857 done:
858 if (err == NULL) {
859 *outbuf = buf;
860 *outlen = total;
861 } else
862 free(buf);
863 return err;
866 static const struct got_error *
867 read_commit_object(struct got_commit_object **commit, struct got_object *obj,
868 FILE *f)
870 const struct got_error *err = NULL;
871 size_t len;
872 uint8_t *p;
874 if (obj->flags & GOT_OBJ_FLAG_PACKED)
875 err = read_to_mem(&p, &len, f);
876 else
877 err = got_inflate_to_mem(&p, &len, f);
878 if (err)
879 return err;
881 if (len < obj->hdrlen + obj->size) {
882 err = got_error(GOT_ERR_BAD_OBJ_DATA);
883 goto done;
886 /* Skip object header. */
887 len -= obj->hdrlen;
888 err = parse_commit_object(commit, p + obj->hdrlen, len);
889 free(p);
890 done:
891 return err;
894 static void
895 read_commit_object_privsep_child(struct got_object *obj, int obj_fd,
896 int imsg_fds[2])
898 const struct got_error *err = NULL;
899 struct got_commit_object *commit = NULL;
900 struct imsgbuf ibuf;
901 FILE *f = NULL;
902 int status = 0;
904 setproctitle("read commit object");
905 close(imsg_fds[0]);
906 imsg_init(&ibuf, imsg_fds[1]);
908 /* revoke access to most system calls */
909 if (pledge("stdio", NULL) == -1) {
910 err = got_error_from_errno();
911 goto done;
914 f = fdopen(obj_fd, "rb");
915 if (f == NULL) {
916 err = got_error_from_errno();
917 close(obj_fd);
918 goto done;
921 err = read_commit_object(&commit, obj, f);
922 if (err)
923 goto done;
925 err = got_privsep_send_commit(&ibuf, commit);
926 done:
927 if (commit)
928 got_object_commit_close(commit);
929 if (err) {
930 got_privsep_send_error(&ibuf, err);
931 status = 1;
933 if (f)
934 fclose(f);
935 imsg_clear(&ibuf);
936 close(imsg_fds[1]);
937 _exit(status);
940 static const struct got_error *
941 read_commit_object_privsep(struct got_commit_object **commit,
942 struct got_repository *repo, struct got_object *obj, int fd)
944 const struct got_error *err = NULL, *err_child = NULL;
945 struct imsgbuf parent_ibuf;
946 int imsg_fds[2];
947 pid_t pid;
949 if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_fds) == -1)
950 return got_error_from_errno();
952 pid = fork();
953 if (pid == -1)
954 return got_error_from_errno();
955 else if (pid == 0) {
956 read_commit_object_privsep_child(obj, fd, imsg_fds);
957 /* not reached */
960 close(imsg_fds[1]);
961 imsg_init(&parent_ibuf, imsg_fds[0]);
962 err = got_privsep_recv_commit(commit, &parent_ibuf);
963 imsg_clear(&parent_ibuf);
964 err_child = wait_for_child(pid);
965 close(imsg_fds[0]);
966 return err ? err : err_child;
969 const struct got_error *
970 got_object_commit_open(struct got_commit_object **commit,
971 struct got_repository *repo, struct got_object *obj)
973 const struct got_error *err = NULL;
975 *commit = got_repo_get_cached_commit(repo, &obj->id);
976 if (*commit != NULL) {
977 (*commit)->refcnt++;
978 return NULL;
981 if (obj->type != GOT_OBJ_TYPE_COMMIT)
982 return got_error(GOT_ERR_OBJ_TYPE);
984 if (obj->flags & GOT_OBJ_FLAG_PACKED) {
985 uint8_t *buf;
986 size_t len;
987 err = got_packfile_extract_object_to_mem(&buf, &len, obj, repo);
988 if (err)
989 return err;
990 obj->size = len;
991 err = parse_commit_object(commit, buf, len);
992 free(buf);
993 } else {
994 int fd;
995 err = open_loose_object(&fd, obj, repo);
996 if (err)
997 return err;
998 err = read_commit_object_privsep(commit, repo, obj, fd);
999 close(fd);
1002 if (err == NULL) {
1003 (*commit)->refcnt++;
1004 err = got_repo_cache_commit(repo, &obj->id, *commit);
1007 return err;
1010 void
1011 got_object_commit_close(struct got_commit_object *commit)
1013 struct got_object_qid *qid;
1015 if (commit->refcnt > 0) {
1016 commit->refcnt--;
1017 if (commit->refcnt > 0)
1018 return;
1021 while (!SIMPLEQ_EMPTY(&commit->parent_ids)) {
1022 qid = SIMPLEQ_FIRST(&commit->parent_ids);
1023 SIMPLEQ_REMOVE_HEAD(&commit->parent_ids, entry);
1024 free(qid->id);
1025 free(qid);
1028 free(commit->tree_id);
1029 free(commit->author);
1030 free(commit->committer);
1031 free(commit->logmsg);
1032 free(commit);
1035 static const struct got_error *
1036 read_tree_object(struct got_tree_object **tree, struct got_object *obj, FILE *f)
1038 const struct got_error *err = NULL;
1039 size_t len;
1040 uint8_t *p;
1042 if (obj->flags & GOT_OBJ_FLAG_PACKED)
1043 err = read_to_mem(&p, &len, f);
1044 else
1045 err = got_inflate_to_mem(&p, &len, f);
1046 if (err)
1047 return err;
1049 if (len < obj->hdrlen + obj->size) {
1050 err = got_error(GOT_ERR_BAD_OBJ_DATA);
1051 goto done;
1054 /* Skip object header. */
1055 len -= obj->hdrlen;
1056 err = parse_tree_object(tree, p + obj->hdrlen, len);
1057 free(p);
1058 done:
1059 return err;
1062 static void
1063 read_tree_object_privsep_child(struct got_object *obj, int obj_fd,
1064 int imsg_fds[2])
1066 const struct got_error *err = NULL;
1067 struct got_tree_object *tree = NULL;
1068 struct imsgbuf ibuf;
1069 FILE *f = NULL;
1070 int status = 0;
1072 setproctitle("read tree object");
1073 close(imsg_fds[0]);
1074 imsg_init(&ibuf, imsg_fds[1]);
1076 /* revoke access to most system calls */
1077 if (pledge("stdio", NULL) == -1) {
1078 err = got_error_from_errno();
1079 goto done;
1082 f = fdopen(obj_fd, "rb");
1083 if (f == NULL) {
1084 err = got_error_from_errno();
1085 close(obj_fd);
1086 goto done;
1089 err = read_tree_object(&tree, obj, f);
1090 if (err)
1091 goto done;
1093 err = got_privsep_send_tree(&ibuf, tree);
1094 done:
1095 if (tree)
1096 got_object_tree_close(tree);
1097 if (err) {
1098 got_privsep_send_error(&ibuf, err);
1099 status = 1;
1101 if (f)
1102 fclose(f);
1103 imsg_clear(&ibuf);
1104 close(imsg_fds[1]);
1105 _exit(status);
1108 static const struct got_error *
1109 read_tree_object_privsep(struct got_tree_object **tree, struct got_object *obj,
1110 int fd)
1112 const struct got_error *err = NULL, *err_child = NULL;
1113 struct imsgbuf parent_ibuf;
1114 int imsg_fds[2];
1115 pid_t pid;
1117 if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_fds) == -1)
1118 return got_error_from_errno();
1120 pid = fork();
1121 if (pid == -1)
1122 return got_error_from_errno();
1123 else if (pid == 0) {
1124 read_tree_object_privsep_child(obj, fd, imsg_fds);
1125 /* not reached */
1128 close(imsg_fds[1]);
1129 imsg_init(&parent_ibuf, imsg_fds[0]);
1130 err = got_privsep_recv_tree(tree, &parent_ibuf);
1131 imsg_clear(&parent_ibuf);
1132 err_child = wait_for_child(pid);
1133 close(imsg_fds[0]);
1134 return err ? err : err_child;
1137 const struct got_error *
1138 got_object_tree_open(struct got_tree_object **tree,
1139 struct got_repository *repo, struct got_object *obj)
1141 const struct got_error *err = NULL;
1143 *tree = got_repo_get_cached_tree(repo, &obj->id);
1144 if (*tree != NULL) {
1145 (*tree)->refcnt++;
1146 return NULL;
1149 if (obj->type != GOT_OBJ_TYPE_TREE)
1150 return got_error(GOT_ERR_OBJ_TYPE);
1152 if (obj->flags & GOT_OBJ_FLAG_PACKED) {
1153 uint8_t *buf;
1154 size_t len;
1155 err = got_packfile_extract_object_to_mem(&buf, &len, obj, repo);
1156 if (err)
1157 return err;
1158 obj->size = len;
1159 err = parse_tree_object(tree, buf, len);
1160 free(buf);
1161 } else {
1162 int fd;
1163 err = open_loose_object(&fd, obj, repo);
1164 if (err)
1165 return err;
1166 err = read_tree_object_privsep(tree, obj, fd);
1167 close(fd);
1170 if (err == NULL) {
1171 (*tree)->refcnt++;
1172 err = got_repo_cache_tree(repo, &obj->id, *tree);
1175 return err;
1178 const struct got_error *
1179 got_object_open_as_tree(struct got_tree_object **tree,
1180 struct got_repository *repo, struct got_object_id *id)
1182 const struct got_error *err;
1183 struct got_object *obj;
1185 *tree = NULL;
1187 err = got_object_open(&obj, repo, id);
1188 if (err)
1189 return err;
1190 if (got_object_get_type(obj) != GOT_OBJ_TYPE_TREE) {
1191 err = got_error(GOT_ERR_OBJ_TYPE);
1192 goto done;
1195 err = got_object_tree_open(tree, repo, obj);
1196 done:
1197 got_object_close(obj);
1198 return err;
1201 void
1202 got_object_tree_close(struct got_tree_object *tree)
1204 struct got_tree_entry *te;
1206 if (tree->refcnt > 0) {
1207 tree->refcnt--;
1208 if (tree->refcnt > 0)
1209 return;
1212 while (!SIMPLEQ_EMPTY(&tree->entries)) {
1213 te = SIMPLEQ_FIRST(&tree->entries);
1214 SIMPLEQ_REMOVE_HEAD(&tree->entries, entry);
1215 tree_entry_close(te);
1218 free(tree);
1221 static const struct got_error *
1222 read_blob_object_privsep_child(int outfd, int infd, int imsg_fds[2])
1224 const struct got_error *err = NULL;
1225 struct imsgbuf ibuf;
1226 int status = 0;
1227 size_t size;
1228 FILE *infile = NULL;
1230 setproctitle("read blob object");
1231 close(imsg_fds[0]);
1232 imsg_init(&ibuf, imsg_fds[1]);
1234 /* revoke access to most system calls */
1235 if (pledge("stdio", NULL) == -1) {
1236 err = got_error_from_errno();
1237 goto done;
1240 infile = fdopen(infd, "rb");
1241 if (infile == NULL) {
1242 err = got_error_from_errno();
1243 close(infd);
1244 goto done;
1246 err = got_inflate_to_fd(&size, infile, outfd);
1247 fclose(infile);
1248 if (err)
1249 goto done;
1251 err = got_privsep_send_blob(&ibuf, size);
1252 done:
1253 if (err) {
1254 got_privsep_send_error(&ibuf, err);
1255 status = 1;
1257 close(outfd);
1258 imsg_clear(&ibuf);
1259 close(imsg_fds[1]);
1260 _exit(status);
1263 static const struct got_error *
1264 read_blob_object_privsep(size_t *size, int outfd, int infd)
1266 struct imsgbuf parent_ibuf;
1267 int imsg_fds[2];
1268 const struct got_error *err = NULL, *err_child = NULL;
1269 pid_t pid;
1271 if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_fds) == -1)
1272 return got_error_from_errno();
1274 pid = fork();
1275 if (pid == -1)
1276 return got_error_from_errno();
1277 else if (pid == 0) {
1278 read_blob_object_privsep_child(outfd, infd, imsg_fds);
1279 /* not reached */
1282 close(imsg_fds[1]);
1283 imsg_init(&parent_ibuf, imsg_fds[0]);
1284 err = got_privsep_recv_blob(size, &parent_ibuf);
1285 imsg_clear(&parent_ibuf);
1286 err_child = wait_for_child(pid);
1287 close(imsg_fds[0]);
1288 if (lseek(outfd, SEEK_SET, 0) == -1)
1289 err = got_error_from_errno();
1290 return err ? err : err_child;
1293 const struct got_error *
1294 got_object_blob_open(struct got_blob_object **blob,
1295 struct got_repository *repo, struct got_object *obj, size_t blocksize)
1297 const struct got_error *err = NULL;
1299 if (obj->type != GOT_OBJ_TYPE_BLOB)
1300 return got_error(GOT_ERR_OBJ_TYPE);
1302 if (blocksize < obj->hdrlen)
1303 return got_error(GOT_ERR_NO_SPACE);
1305 *blob = calloc(1, sizeof(**blob));
1306 if (*blob == NULL)
1307 return got_error_from_errno();
1309 (*blob)->read_buf = calloc(1, blocksize);
1310 if ((*blob)->read_buf == NULL) {
1311 err = got_error_from_errno();
1312 goto done;
1314 if (obj->flags & GOT_OBJ_FLAG_PACKED) {
1315 err = got_packfile_extract_object(&((*blob)->f), obj, repo);
1316 if (err)
1317 goto done;
1318 } else {
1319 int infd, outfd;
1320 size_t size;
1321 struct stat sb;
1323 err = open_loose_object(&infd, obj, repo);
1324 if (err)
1325 goto done;
1328 outfd = got_opentempfd();
1329 if (outfd == -1) {
1330 err = got_error_from_errno();
1331 close(infd);
1332 goto done;
1335 err = read_blob_object_privsep(&size, outfd, infd);
1336 close(infd);
1337 if (err)
1338 goto done;
1340 if (size != obj->hdrlen + obj->size) {
1341 err = got_error(GOT_ERR_PRIVSEP_LEN);
1342 close(outfd);
1343 goto done;
1346 if (fstat(outfd, &sb) == -1) {
1347 err = got_error_from_errno();
1348 close(outfd);
1349 goto done;
1352 if (sb.st_size != size) {
1353 err = got_error(GOT_ERR_PRIVSEP_LEN);
1354 close(outfd);
1355 goto done;
1358 (*blob)->f = fdopen(outfd, "rb");
1359 if ((*blob)->f == NULL) {
1360 err = got_error_from_errno();
1361 close(outfd);
1362 goto done;
1366 (*blob)->hdrlen = obj->hdrlen;
1367 (*blob)->blocksize = blocksize;
1368 memcpy(&(*blob)->id.sha1, obj->id.sha1, SHA1_DIGEST_LENGTH);
1370 done:
1371 if (err && *blob) {
1372 if ((*blob)->f)
1373 fclose((*blob)->f);
1374 free((*blob)->read_buf);
1375 free(*blob);
1376 *blob = NULL;
1378 return err;
1381 const struct got_error *
1382 got_object_open_as_blob(struct got_blob_object **blob,
1383 struct got_repository *repo, struct got_object_id *id,
1384 size_t blocksize)
1386 const struct got_error *err;
1387 struct got_object *obj;
1389 *blob = NULL;
1391 err = got_object_open(&obj, repo, id);
1392 if (err)
1393 return err;
1394 if (got_object_get_type(obj) != GOT_OBJ_TYPE_BLOB) {
1395 err = got_error(GOT_ERR_OBJ_TYPE);
1396 goto done;
1399 err = got_object_blob_open(blob, repo, obj, blocksize);
1400 done:
1401 got_object_close(obj);
1402 return err;
1405 void
1406 got_object_blob_close(struct got_blob_object *blob)
1408 free(blob->read_buf);
1409 fclose(blob->f);
1410 free(blob);
1413 char *
1414 got_object_blob_id_str(struct got_blob_object *blob, char *buf, size_t size)
1416 return got_sha1_digest_to_str(blob->id.sha1, buf, size);
1419 size_t
1420 got_object_blob_get_hdrlen(struct got_blob_object *blob)
1422 return blob->hdrlen;
1425 const uint8_t *
1426 got_object_blob_get_read_buf(struct got_blob_object *blob)
1428 return blob->read_buf;
1431 const struct got_error *
1432 got_object_blob_read_block(size_t *outlenp, struct got_blob_object *blob)
1434 size_t n;
1436 n = fread(blob->read_buf, 1, blob->blocksize, blob->f);
1437 if (n == 0 && ferror(blob->f))
1438 return got_ferror(blob->f, GOT_ERR_IO);
1439 *outlenp = n;
1440 return NULL;
1443 const struct got_error *
1444 got_object_blob_dump_to_file(size_t *total_len, FILE *outfile,
1445 struct got_blob_object *blob)
1447 const struct got_error *err = NULL;
1448 size_t len, hdrlen;
1450 *total_len = 0;
1451 hdrlen = got_object_blob_get_hdrlen(blob);
1452 do {
1453 err = got_object_blob_read_block(&len, blob);
1454 if (err)
1455 return err;
1456 if (len == 0)
1457 break;
1458 *total_len += len;
1459 /* Skip blob object header first time around. */
1460 fwrite(got_object_blob_get_read_buf(blob) + hdrlen,
1461 len - hdrlen, 1, outfile);
1462 hdrlen = 0;
1463 } while (len != 0);
1465 fflush(outfile);
1466 rewind(outfile);
1468 return NULL;
1471 static struct got_tree_entry *
1472 find_entry_by_name(struct got_tree_object *tree, const char *name)
1474 struct got_tree_entry *te;
1476 SIMPLEQ_FOREACH(te, &tree->entries, entry) {
1477 if (strcmp(te->name, name) == 0)
1478 return te;
1480 return NULL;
1483 const struct got_error *
1484 got_object_open_by_path(struct got_object **obj, struct got_repository *repo,
1485 struct got_object_id *commit_id, const char *path)
1487 const struct got_error *err = NULL;
1488 struct got_commit_object *commit = NULL;
1489 struct got_tree_object *tree = NULL;
1490 struct got_tree_entry *te = NULL;
1491 char *seg, *s, *s0 = NULL;
1492 size_t len = strlen(path);
1494 *obj = NULL;
1496 /* We are expecting an absolute in-repository path. */
1497 if (path[0] != '/')
1498 return got_error(GOT_ERR_NOT_ABSPATH);
1500 err = got_object_open_as_commit(&commit, repo, commit_id);
1501 if (err)
1502 goto done;
1504 /* Handle opening of root of commit's tree. */
1505 if (path[1] == '\0') {
1506 err = got_object_open(obj, repo, commit->tree_id);
1507 if (err)
1508 goto done;
1509 return NULL;
1512 err = got_object_open_as_tree(&tree, repo, commit->tree_id);
1513 if (err)
1514 goto done;
1516 s0 = strdup(path);
1517 if (s0 == NULL) {
1518 err = got_error_from_errno();
1519 goto done;
1521 err = got_canonpath(path, s0, len + 1);
1522 if (err)
1523 goto done;
1525 s = s0;
1526 s++; /* skip leading '/' */
1527 len--;
1528 seg = s;
1529 while (len > 0) {
1530 struct got_tree_object *next_tree;
1532 if (*s != '/') {
1533 s++;
1534 len--;
1535 if (*s)
1536 continue;
1539 /* end of path segment */
1540 *s = '\0';
1542 te = find_entry_by_name(tree, seg);
1543 if (te == NULL) {
1544 err = got_error(GOT_ERR_NO_OBJ);
1545 goto done;
1548 if (len == 0)
1549 break;
1551 seg = s + 1;
1552 s++;
1553 len--;
1554 if (*s) {
1555 err = got_object_open_as_tree(&next_tree, repo,
1556 te->id);
1557 te = NULL;
1558 if (err)
1559 goto done;
1560 got_object_tree_close(tree);
1561 tree = next_tree;
1565 if (te) {
1566 err = got_object_open(obj, repo, te->id);
1567 if (err)
1568 goto done;
1569 } else
1570 err = got_error(GOT_ERR_NO_OBJ);
1571 done:
1572 free(s0);
1573 if (commit)
1574 got_object_commit_close(commit);
1575 if (tree)
1576 got_object_tree_close(tree);
1577 return err;