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 *commit = got_repo_get_cached_commit(repo, &obj->id);
975 if (*commit != NULL) {
976 (*commit)->refcnt++;
977 return NULL;
980 if (obj->type != GOT_OBJ_TYPE_COMMIT)
981 return got_error(GOT_ERR_OBJ_TYPE);
983 if (obj->flags & GOT_OBJ_FLAG_PACKED) {
984 uint8_t *buf;
985 size_t len;
986 err = got_packfile_extract_object_to_mem(&buf, &len, obj, repo);
987 if (err)
988 return err;
989 obj->size = len;
990 err = parse_commit_object(commit, buf, len);
991 free(buf);
992 } else {
993 int fd;
994 err = open_loose_object(&fd, obj, repo);
995 if (err)
996 return err;
997 err = read_commit_object_privsep(commit, repo, obj, fd);
998 close(fd);
1001 if (err == NULL) {
1002 (*commit)->refcnt++;
1003 err = got_repo_cache_commit(repo, &obj->id, *commit);
1006 return err;
1009 void
1010 got_object_commit_close(struct got_commit_object *commit)
1012 struct got_object_qid *qid;
1014 if (commit->refcnt > 0) {
1015 commit->refcnt--;
1016 return;
1019 while (!SIMPLEQ_EMPTY(&commit->parent_ids)) {
1020 qid = SIMPLEQ_FIRST(&commit->parent_ids);
1021 SIMPLEQ_REMOVE_HEAD(&commit->parent_ids, entry);
1022 free(qid->id);
1023 free(qid);
1026 free(commit->tree_id);
1027 free(commit->author);
1028 free(commit->committer);
1029 free(commit->logmsg);
1030 free(commit);
1033 static const struct got_error *
1034 read_tree_object(struct got_tree_object **tree, struct got_object *obj, FILE *f)
1036 const struct got_error *err = NULL;
1037 size_t len;
1038 uint8_t *p;
1040 if (obj->flags & GOT_OBJ_FLAG_PACKED)
1041 err = read_to_mem(&p, &len, f);
1042 else
1043 err = got_inflate_to_mem(&p, &len, f);
1044 if (err)
1045 return err;
1047 if (len < obj->hdrlen + obj->size) {
1048 err = got_error(GOT_ERR_BAD_OBJ_DATA);
1049 goto done;
1052 /* Skip object header. */
1053 len -= obj->hdrlen;
1054 err = parse_tree_object(tree, p + obj->hdrlen, len);
1055 free(p);
1056 done:
1057 return err;
1060 static void
1061 read_tree_object_privsep_child(struct got_object *obj, int obj_fd,
1062 int imsg_fds[2])
1064 const struct got_error *err = NULL;
1065 struct got_tree_object *tree = NULL;
1066 struct imsgbuf ibuf;
1067 FILE *f = NULL;
1068 int status = 0;
1070 setproctitle("read tree object");
1071 close(imsg_fds[0]);
1072 imsg_init(&ibuf, imsg_fds[1]);
1074 /* revoke access to most system calls */
1075 if (pledge("stdio", NULL) == -1) {
1076 err = got_error_from_errno();
1077 goto done;
1080 f = fdopen(obj_fd, "rb");
1081 if (f == NULL) {
1082 err = got_error_from_errno();
1083 close(obj_fd);
1084 goto done;
1087 err = read_tree_object(&tree, obj, f);
1088 if (err)
1089 goto done;
1091 err = got_privsep_send_tree(&ibuf, tree);
1092 done:
1093 if (tree)
1094 got_object_tree_close(tree);
1095 if (err) {
1096 got_privsep_send_error(&ibuf, err);
1097 status = 1;
1099 if (f)
1100 fclose(f);
1101 imsg_clear(&ibuf);
1102 close(imsg_fds[1]);
1103 _exit(status);
1106 static const struct got_error *
1107 read_tree_object_privsep(struct got_tree_object **tree, struct got_object *obj,
1108 int fd)
1110 const struct got_error *err = NULL, *err_child = NULL;
1111 struct imsgbuf parent_ibuf;
1112 int imsg_fds[2];
1113 pid_t pid;
1115 if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_fds) == -1)
1116 return got_error_from_errno();
1118 pid = fork();
1119 if (pid == -1)
1120 return got_error_from_errno();
1121 else if (pid == 0) {
1122 read_tree_object_privsep_child(obj, fd, imsg_fds);
1123 /* not reached */
1126 close(imsg_fds[1]);
1127 imsg_init(&parent_ibuf, imsg_fds[0]);
1128 err = got_privsep_recv_tree(tree, &parent_ibuf);
1129 imsg_clear(&parent_ibuf);
1130 err_child = wait_for_child(pid);
1131 close(imsg_fds[0]);
1132 return err ? err : err_child;
1135 const struct got_error *
1136 got_object_tree_open(struct got_tree_object **tree,
1137 struct got_repository *repo, struct got_object *obj)
1139 const struct got_error *err = NULL;
1141 *tree = got_repo_get_cached_tree(repo, &obj->id);
1142 if (*tree != NULL) {
1143 (*tree)->refcnt++;
1144 return NULL;
1147 if (obj->type != GOT_OBJ_TYPE_TREE)
1148 return got_error(GOT_ERR_OBJ_TYPE);
1150 if (obj->flags & GOT_OBJ_FLAG_PACKED) {
1151 uint8_t *buf;
1152 size_t len;
1153 err = got_packfile_extract_object_to_mem(&buf, &len, obj, repo);
1154 if (err)
1155 return err;
1156 obj->size = len;
1157 err = parse_tree_object(tree, buf, len);
1158 free(buf);
1159 } else {
1160 int fd;
1161 err = open_loose_object(&fd, obj, repo);
1162 if (err)
1163 return err;
1164 err = read_tree_object_privsep(tree, obj, fd);
1165 close(fd);
1168 if (err == NULL) {
1169 (*tree)->refcnt++;
1170 err = got_repo_cache_tree(repo, &obj->id, *tree);
1173 return err;
1176 const struct got_error *
1177 got_object_open_as_tree(struct got_tree_object **tree,
1178 struct got_repository *repo, struct got_object_id *id)
1180 const struct got_error *err;
1181 struct got_object *obj;
1183 *tree = NULL;
1185 err = got_object_open(&obj, repo, id);
1186 if (err)
1187 return err;
1188 if (got_object_get_type(obj) != GOT_OBJ_TYPE_TREE) {
1189 err = got_error(GOT_ERR_OBJ_TYPE);
1190 goto done;
1193 err = got_object_tree_open(tree, repo, obj);
1194 done:
1195 got_object_close(obj);
1196 return err;
1199 void
1200 got_object_tree_close(struct got_tree_object *tree)
1202 struct got_tree_entry *te;
1204 if (tree->refcnt > 0) {
1205 tree->refcnt--;
1206 return;
1209 while (!SIMPLEQ_EMPTY(&tree->entries)) {
1210 te = SIMPLEQ_FIRST(&tree->entries);
1211 SIMPLEQ_REMOVE_HEAD(&tree->entries, entry);
1212 tree_entry_close(te);
1215 free(tree);
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, FILE *outfile,
1442 struct got_blob_object *blob)
1444 const struct got_error *err = NULL;
1445 size_t len, hdrlen;
1447 *total_len = 0;
1448 hdrlen = got_object_blob_get_hdrlen(blob);
1449 do {
1450 err = got_object_blob_read_block(&len, blob);
1451 if (err)
1452 return err;
1453 if (len == 0)
1454 break;
1455 *total_len += len;
1456 /* Skip blob object header first time around. */
1457 fwrite(got_object_blob_get_read_buf(blob) + hdrlen,
1458 len - hdrlen, 1, outfile);
1459 hdrlen = 0;
1460 } while (len != 0);
1462 fflush(outfile);
1463 rewind(outfile);
1465 return NULL;
1468 static struct got_tree_entry *
1469 find_entry_by_name(struct got_tree_object *tree, const char *name)
1471 struct got_tree_entry *te;
1473 SIMPLEQ_FOREACH(te, &tree->entries, entry) {
1474 if (strcmp(te->name, name) == 0)
1475 return te;
1477 return NULL;
1480 const struct got_error *
1481 got_object_open_by_path(struct got_object **obj, struct got_repository *repo,
1482 struct got_object_id *commit_id, const char *path)
1484 const struct got_error *err = NULL;
1485 struct got_commit_object *commit = NULL;
1486 struct got_tree_object *tree = NULL;
1487 struct got_tree_entry *te = NULL;
1488 char *seg, *s, *s0 = NULL;
1489 size_t len = strlen(path);
1491 *obj = NULL;
1493 /* We are expecting an absolute in-repository path. */
1494 if (path[0] != '/')
1495 return got_error(GOT_ERR_NOT_ABSPATH);
1497 err = got_object_open_as_commit(&commit, repo, commit_id);
1498 if (err)
1499 goto done;
1501 /* Handle opening of root of commit's tree. */
1502 if (path[1] == '\0') {
1503 err = got_object_open(obj, repo, commit->tree_id);
1504 if (err)
1505 goto done;
1506 return NULL;
1509 err = got_object_open_as_tree(&tree, repo, commit->tree_id);
1510 if (err)
1511 goto done;
1513 s0 = strdup(path);
1514 if (s0 == NULL) {
1515 err = got_error_from_errno();
1516 goto done;
1518 err = got_canonpath(path, s0, len + 1);
1519 if (err)
1520 goto done;
1522 s = s0;
1523 s++; /* skip leading '/' */
1524 len--;
1525 seg = s;
1526 while (len > 0) {
1527 struct got_tree_object *next_tree;
1529 if (*s != '/') {
1530 s++;
1531 len--;
1532 if (*s)
1533 continue;
1536 /* end of path segment */
1537 *s = '\0';
1539 te = find_entry_by_name(tree, seg);
1540 if (te == NULL) {
1541 err = got_error(GOT_ERR_NO_OBJ);
1542 goto done;
1545 if (len == 0)
1546 break;
1548 seg = s + 1;
1549 s++;
1550 len--;
1551 if (*s) {
1552 err = got_object_open_as_tree(&next_tree, repo,
1553 te->id);
1554 te = NULL;
1555 if (err)
1556 goto done;
1557 got_object_tree_close(tree);
1558 tree = next_tree;
1562 if (te) {
1563 err = got_object_open(obj, repo, te->id);
1564 if (err)
1565 goto done;
1566 } else
1567 err = got_error(GOT_ERR_NO_OBJ);
1568 done:
1569 free(s0);
1570 if (commit)
1571 got_object_commit_close(commit);
1572 if (tree)
1573 got_object_tree_close(tree);
1574 return err;