2 * Copyright (c) 2018 Stefan Sperling <stsp@openbsd.org>
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.
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.
17 #include <sys/types.h>
19 #include <sys/queue.h>
21 #include <sys/socket.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"
52 #define MIN(_a,_b) ((_a) < (_b) ? (_a) : (_b))
56 #define nitems(_a) (sizeof(_a) / sizeof((_a)[0]))
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)
71 static const size_t len = SHA1_DIGEST_STRING_LENGTH;
73 *outbuf = malloc(len);
75 return got_error_from_errno();
77 if (got_sha1_digest_to_str(id->sha1, *outbuf, len) == NULL) {
80 return got_error(GOT_ERR_BAD_OBJ_ID_STR);
87 got_object_id_cmp(struct got_object_id *id1, struct got_object_id *id2)
89 return memcmp(id1->sha1, id2->sha1, SHA1_DIGEST_LENGTH);
92 struct got_object_id *
93 got_object_id_dup(struct got_object_id *id1)
95 struct got_object_id *id2;
97 id2 = malloc(sizeof(*id2));
100 memcpy(id2, id1, sizeof(*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);
117 got_object_get_type(struct got_object *obj)
120 case GOT_OBJ_TYPE_COMMIT:
121 case GOT_OBJ_TYPE_TREE:
122 case GOT_OBJ_TYPE_BLOB:
123 case GOT_OBJ_TYPE_TAG:
134 static const struct got_error *
135 parse_object_header(struct got_object **obj, char *buf, size_t len)
137 const char *obj_tags[] = {
142 const int obj_types[] = {
148 size_t size = 0, hdrlen = 0;
150 char *p = strchr(buf, '\0');
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);
162 if (strncmp(buf, tag, tlen) != 0)
167 return got_error(GOT_ERR_BAD_OBJ_HDR);
168 size = strtonum(buf + tlen, 0, LONG_MAX, &errstr);
170 return got_error(GOT_ERR_BAD_OBJ_HDR);
175 return got_error(GOT_ERR_BAD_OBJ_HDR);
177 *obj = calloc(1, sizeof(**obj));
179 return got_error_from_errno();
181 (*obj)->hdrlen = hdrlen;
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;
192 const size_t zbsize = 64;
193 size_t outlen, totlen;
196 buf = malloc(zbsize);
198 return got_error_from_errno();
200 err = got_inflate_init(&zb, NULL, zbsize);
207 err = got_inflate_read_fd(&zb, fd, &outlen);
210 if (strchr(zb.outbuf, '\0') == NULL) {
211 buf = reallocarray(buf, 2 + i, zbsize);
213 err = got_error_from_errno();
217 memcpy(buf + totlen, zb.outbuf, outlen);
220 } while (strchr(zb.outbuf, '\0') == NULL);
222 err = parse_object_header(obj, buf, totlen);
224 got_inflate_end(&zb);
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;
236 setproctitle("read object header");
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();
246 err = read_object_header(&obj, obj_fd);
250 err = got_privsep_send_obj(&ibuf, obj, 0);
253 got_object_close(obj);
255 got_privsep_send_error(&ibuf, err);
264 static const struct got_error *
265 wait_for_child(pid_t pid)
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);
280 static const struct got_error *
281 read_object_header_privsep(struct got_object **obj, int fd)
283 struct imsgbuf parent_ibuf;
285 const struct got_error *err = NULL, *err_child = NULL;
288 if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_fds) == -1)
289 return got_error_from_errno();
293 return got_error_from_errno();
295 read_object_header_privsep_child(fd, imsg_fds);
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);
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;
313 char *path_objects = got_repo_get_path_objects(repo);
317 if (path_objects == NULL)
318 return got_error_from_errno();
320 err = got_object_id_str(&hex, id);
324 if (asprintf(path, "%s/%.2x/%s", path_objects,
325 id->sha1[0], hex + 2) == -1)
326 err = got_error_from_errno();
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;
339 err = object_path(&path, &obj->id, repo);
342 *fd = open(path, O_RDONLY | O_NOFOLLOW, GOT_DEFAULT_FILE_MODE);
344 err = got_error_from_errno();
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;
360 *obj = got_repo_get_cached_object(repo, id);
366 err = object_path(&path, id, repo);
370 fd = open(path, O_RDONLY | O_NOFOLLOW, GOT_DEFAULT_FILE_MODE);
372 if (errno != ENOENT) {
373 err = got_error_from_errno();
376 err = got_packfile_open_object(obj, id, repo);
380 err = got_error(GOT_ERR_NO_OBJ);
382 err = read_object_header_privsep(obj, fd);
385 memcpy((*obj)->id.sha1, id->sha1, SHA1_DIGEST_LENGTH);
390 err = got_repo_cache_object(repo, id, *obj);
400 const struct got_error *
401 got_object_open_by_id_str(struct got_object **obj, struct got_repository *repo,
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);
413 got_object_close(struct got_object *obj)
415 if (obj->refcnt > 0) {
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);
434 struct got_commit_object *
435 got_object_commit_alloc_partial(void)
437 struct got_commit_object *commit;
439 commit = calloc(1, sizeof(*commit));
442 commit->tree_id = calloc(1, sizeof(*commit->tree_id));
443 if (commit->tree_id == NULL) {
448 SIMPLEQ_INIT(&commit->parent_ids);
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;
462 err = got_object_open(&obj, repo, id);
465 if (got_object_get_type(obj) != GOT_OBJ_TYPE_COMMIT) {
466 err = got_error(GOT_ERR_OBJ_TYPE);
470 err = got_object_commit_open(commit, repo, obj);
472 got_object_close(obj);
476 const struct got_error *
477 got_object_qid_alloc(struct got_object_qid **qid, struct got_object_id *id)
479 const struct got_error *err = NULL;
481 *qid = calloc(1, sizeof(**qid));
483 return got_error_from_errno();
485 (*qid)->id = got_object_id_dup(id);
486 if ((*qid)->id == NULL) {
487 err = got_error_from_errno();
497 got_object_qid_free(struct got_object_qid *qid)
503 const struct got_error *
504 got_object_commit_add_parent(struct got_commit_object *commit,
507 const struct got_error *err = NULL;
508 struct got_object_qid *qid;
510 qid = malloc(sizeof(*qid));
512 return got_error_from_errno();
514 qid->id = malloc(sizeof(*qid->id));
515 if (qid->id == NULL) {
516 err = got_error_from_errno();
521 if (!got_parse_sha1_digest(qid->id->sha1, id_str)) {
522 err = got_error(GOT_ERR_BAD_OBJ_DATA);
528 SIMPLEQ_INSERT_TAIL(&commit->parent_ids, qid, entry);
534 static const struct got_error *
535 parse_gmtoff(time_t *gmtoff, const char *tzstr)
538 const char *p = tzstr;
546 return got_error(GOT_ERR_BAD_OBJ_DATA);
548 if (!isdigit(*p) && !isdigit(*(p + 1)))
549 return got_error(GOT_ERR_BAD_OBJ_DATA);
550 h = (((*p - '0') * 10) + (*(p + 1) - '0'));
553 if (!isdigit(*p) && !isdigit(*(p + 1)))
554 return got_error(GOT_ERR_BAD_OBJ_DATA);
555 m = ((*p - '0') * 10) + (*(p + 1) - '0');
557 *gmtoff = (h * 60 * 60 + m * 60) * sign;
561 static const struct got_error *
562 parse_commit_time(struct tm *tm, char *committer)
564 const struct got_error *err = NULL;
570 /* Parse and strip off trailing timezone indicator string. */
571 space = strrchr(committer, ' ');
573 return got_error(GOT_ERR_BAD_OBJ_DATA);
574 tzstr = strdup(space + 1);
576 return got_error_from_errno();
577 err = parse_gmtoff(&gmtoff, tzstr);
583 /* Timestamp is separated from committer name + email by space. */
584 space = strrchr(committer, ' ');
586 return got_error(GOT_ERR_BAD_OBJ_DATA);
588 /* Timestamp parsed here is expressed in comitter's local time. */
589 time = strtonum(space + 1, 0, INT64_MAX, &errstr);
591 return got_error(GOT_ERR_BAD_OBJ_DATA);
593 /* Express the time stamp in UTC. */
594 memset(tm, 0, sizeof(*tm));
596 if (localtime_r(&time, tm) == NULL)
597 return got_error_from_errno();
598 tm->tm_gmtoff = gmtoff;
600 /* Strip off parsed time information, leaving just author and email. */
606 static const struct got_error *
607 parse_commit_object(struct got_commit_object **commit, char *buf, size_t len)
609 const struct got_error *err = NULL;
612 ssize_t remain = (ssize_t)len;
614 *commit = got_object_commit_alloc_partial();
616 return got_error_from_errno();
618 tlen = strlen(GOT_COMMIT_TAG_TREE);
619 if (strncmp(s, GOT_COMMIT_TAG_TREE, tlen) == 0) {
621 if (remain < SHA1_DIGEST_STRING_LENGTH) {
622 err = got_error(GOT_ERR_BAD_OBJ_DATA);
626 if (!got_parse_sha1_digest((*commit)->tree_id->sha1, s)) {
627 err = got_error(GOT_ERR_BAD_OBJ_DATA);
630 remain -= SHA1_DIGEST_STRING_LENGTH;
631 s += SHA1_DIGEST_STRING_LENGTH;
633 err = got_error(GOT_ERR_BAD_OBJ_DATA);
637 tlen = strlen(GOT_COMMIT_TAG_PARENT);
638 while (strncmp(s, GOT_COMMIT_TAG_PARENT, tlen) == 0) {
640 if (remain < SHA1_DIGEST_STRING_LENGTH) {
641 err = got_error(GOT_ERR_BAD_OBJ_DATA);
645 err = got_object_commit_add_parent(*commit, s);
649 remain -= SHA1_DIGEST_STRING_LENGTH;
650 s += SHA1_DIGEST_STRING_LENGTH;
653 tlen = strlen(GOT_COMMIT_TAG_AUTHOR);
654 if (strncmp(s, GOT_COMMIT_TAG_AUTHOR, tlen) == 0) {
660 err = got_error(GOT_ERR_BAD_OBJ_DATA);
666 err = got_error(GOT_ERR_BAD_OBJ_DATA);
671 err = parse_commit_time(&(*commit)->tm_author, s);
674 (*commit)->author = strdup(s);
675 if ((*commit)->author == NULL) {
676 err = got_error_from_errno();
683 tlen = strlen(GOT_COMMIT_TAG_COMMITTER);
684 if (strncmp(s, GOT_COMMIT_TAG_COMMITTER, tlen) == 0) {
690 err = got_error(GOT_ERR_BAD_OBJ_DATA);
696 err = got_error(GOT_ERR_BAD_OBJ_DATA);
701 err = parse_commit_time(&(*commit)->tm_committer, s);
704 (*commit)->committer = strdup(s);
705 if ((*commit)->committer == NULL) {
706 err = got_error_from_errno();
713 (*commit)->logmsg = strndup(s, remain);
714 if ((*commit)->logmsg == NULL) {
715 err = got_error_from_errno();
720 got_object_commit_close(*commit);
727 tree_entry_close(struct got_tree_entry *te)
734 struct got_tree_entry *
735 got_alloc_tree_entry_partial(void)
737 struct got_tree_entry *te;
739 te = calloc(1, sizeof(*te));
743 te->id = calloc(1, sizeof(*te->id));
744 if (te->id == NULL) {
751 static const struct got_error *
752 parse_tree_entry(struct got_tree_entry **te, size_t *elen, char *buf,
755 char *p = buf, *space;
756 const struct got_error *err = NULL;
758 *te = got_alloc_tree_entry_partial();
760 return got_error_from_errno();
762 *elen = strlen(buf) + 1;
763 if (*elen > maxlen) {
766 return got_error(GOT_ERR_BAD_OBJ_DATA);
769 space = strchr(buf, ' ');
771 err = got_error(GOT_ERR_BAD_OBJ_DATA);
777 if (*p < '0' && *p > '7') {
778 err = got_error(GOT_ERR_BAD_OBJ_DATA);
782 (*te)->mode |= *p - '0';
786 (*te)->name = strdup(space + 1);
787 if (*elen > maxlen || maxlen - *elen < SHA1_DIGEST_LENGTH) {
788 err = got_error(GOT_ERR_BAD_OBJ_DATA);
791 buf += strlen(buf) + 1;
792 memcpy((*te)->id->sha1, buf, SHA1_DIGEST_LENGTH);
793 *elen += SHA1_DIGEST_LENGTH;
796 tree_entry_close(*te);
802 static const struct got_error *
803 parse_tree_object(struct got_tree_object **tree, uint8_t *buf, size_t len)
805 const struct got_error *err;
808 *tree = calloc(1, sizeof(**tree));
810 return got_error_from_errno();
812 SIMPLEQ_INIT(&(*tree)->entries.head);
815 struct got_tree_entry *te;
818 err = parse_tree_entry(&te, &elen, buf, remain);
821 (*tree)->entries.nentries++;
822 SIMPLEQ_INSERT_TAIL(&(*tree)->entries.head, te, entry);
828 got_object_tree_close(*tree);
829 return got_error(GOT_ERR_BAD_OBJ_DATA);
835 static const struct got_error *
836 read_to_mem(uint8_t **outbuf, size_t *outlen, FILE *f)
838 const struct got_error *err = NULL;
839 static const size_t blocksize = 512;
840 size_t n, total, remain;
846 buf = malloc(blocksize);
848 return got_error_from_errno();
855 newbuf = reallocarray(buf, 1, total + blocksize);
856 if (newbuf == NULL) {
857 err = got_error_from_errno();
863 n = fread(buf + total, 1, remain, f);
866 err = got_ferror(f, GOT_ERR_IO);
884 static const struct got_error *
885 read_commit_object(struct got_commit_object **commit, struct got_object *obj,
888 const struct got_error *err = NULL;
892 if (obj->flags & GOT_OBJ_FLAG_PACKED)
893 err = read_to_mem(&p, &len, f);
895 err = got_inflate_to_mem(&p, &len, f);
899 if (len < obj->hdrlen + obj->size) {
900 err = got_error(GOT_ERR_BAD_OBJ_DATA);
904 /* Skip object header. */
906 err = parse_commit_object(commit, p + obj->hdrlen, len);
913 read_commit_object_privsep_child(struct got_object *obj, int obj_fd,
916 const struct got_error *err = NULL;
917 struct got_commit_object *commit = NULL;
922 setproctitle("read commit object");
924 imsg_init(&ibuf, imsg_fds[1]);
926 /* revoke access to most system calls */
927 if (pledge("stdio", NULL) == -1) {
928 err = got_error_from_errno();
932 f = fdopen(obj_fd, "rb");
934 err = got_error_from_errno();
939 err = read_commit_object(&commit, obj, f);
943 err = got_privsep_send_commit(&ibuf, commit);
946 got_object_commit_close(commit);
948 got_privsep_send_error(&ibuf, err);
958 static const struct got_error *
959 read_commit_object_privsep(struct got_commit_object **commit,
960 struct got_repository *repo, struct got_object *obj, int fd)
962 const struct got_error *err = NULL, *err_child = NULL;
963 struct imsgbuf parent_ibuf;
967 if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_fds) == -1)
968 return got_error_from_errno();
972 return got_error_from_errno();
974 read_commit_object_privsep_child(obj, fd, imsg_fds);
979 imsg_init(&parent_ibuf, imsg_fds[0]);
980 err = got_privsep_recv_commit(commit, &parent_ibuf);
981 imsg_clear(&parent_ibuf);
982 err_child = wait_for_child(pid);
984 return err ? err : err_child;
987 const struct got_error *
988 got_object_commit_open(struct got_commit_object **commit,
989 struct got_repository *repo, struct got_object *obj)
991 const struct got_error *err = NULL;
993 *commit = got_repo_get_cached_commit(repo, &obj->id);
994 if (*commit != NULL) {
999 if (obj->type != GOT_OBJ_TYPE_COMMIT)
1000 return got_error(GOT_ERR_OBJ_TYPE);
1002 if (obj->flags & GOT_OBJ_FLAG_PACKED) {
1005 err = got_packfile_extract_object_to_mem(&buf, &len, obj, repo);
1009 err = parse_commit_object(commit, buf, len);
1013 err = open_loose_object(&fd, obj, repo);
1016 err = read_commit_object_privsep(commit, repo, obj, fd);
1021 (*commit)->refcnt++;
1022 err = got_repo_cache_commit(repo, &obj->id, *commit);
1029 got_object_commit_close(struct got_commit_object *commit)
1031 struct got_object_qid *qid;
1033 if (commit->refcnt > 0) {
1035 if (commit->refcnt > 0)
1039 while (!SIMPLEQ_EMPTY(&commit->parent_ids)) {
1040 qid = SIMPLEQ_FIRST(&commit->parent_ids);
1041 SIMPLEQ_REMOVE_HEAD(&commit->parent_ids, entry);
1046 free(commit->tree_id);
1047 free(commit->author);
1048 free(commit->committer);
1049 free(commit->logmsg);
1053 static const struct got_error *
1054 read_tree_object(struct got_tree_object **tree, struct got_object *obj, FILE *f)
1056 const struct got_error *err = NULL;
1060 if (obj->flags & GOT_OBJ_FLAG_PACKED)
1061 err = read_to_mem(&p, &len, f);
1063 err = got_inflate_to_mem(&p, &len, f);
1067 if (len < obj->hdrlen + obj->size) {
1068 err = got_error(GOT_ERR_BAD_OBJ_DATA);
1072 /* Skip object header. */
1074 err = parse_tree_object(tree, p + obj->hdrlen, len);
1081 read_tree_object_privsep_child(struct got_object *obj, int obj_fd,
1084 const struct got_error *err = NULL;
1085 struct got_tree_object *tree = NULL;
1086 struct imsgbuf ibuf;
1090 setproctitle("read tree object");
1092 imsg_init(&ibuf, imsg_fds[1]);
1094 /* revoke access to most system calls */
1095 if (pledge("stdio", NULL) == -1) {
1096 err = got_error_from_errno();
1100 f = fdopen(obj_fd, "rb");
1102 err = got_error_from_errno();
1107 err = read_tree_object(&tree, obj, f);
1111 err = got_privsep_send_tree(&ibuf, tree);
1114 got_object_tree_close(tree);
1116 got_privsep_send_error(&ibuf, err);
1126 static const struct got_error *
1127 read_tree_object_privsep(struct got_tree_object **tree, struct got_object *obj,
1130 const struct got_error *err = NULL, *err_child = NULL;
1131 struct imsgbuf parent_ibuf;
1135 if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_fds) == -1)
1136 return got_error_from_errno();
1140 return got_error_from_errno();
1141 else if (pid == 0) {
1142 read_tree_object_privsep_child(obj, fd, imsg_fds);
1147 imsg_init(&parent_ibuf, imsg_fds[0]);
1148 err = got_privsep_recv_tree(tree, &parent_ibuf);
1149 imsg_clear(&parent_ibuf);
1150 err_child = wait_for_child(pid);
1152 return err ? err : err_child;
1155 const struct got_error *
1156 got_object_tree_open(struct got_tree_object **tree,
1157 struct got_repository *repo, struct got_object *obj)
1159 const struct got_error *err = NULL;
1161 *tree = got_repo_get_cached_tree(repo, &obj->id);
1162 if (*tree != NULL) {
1167 if (obj->type != GOT_OBJ_TYPE_TREE)
1168 return got_error(GOT_ERR_OBJ_TYPE);
1170 if (obj->flags & GOT_OBJ_FLAG_PACKED) {
1173 err = got_packfile_extract_object_to_mem(&buf, &len, obj, repo);
1177 err = parse_tree_object(tree, buf, len);
1181 err = open_loose_object(&fd, obj, repo);
1184 err = read_tree_object_privsep(tree, obj, fd);
1190 err = got_repo_cache_tree(repo, &obj->id, *tree);
1196 const struct got_error *
1197 got_object_open_as_tree(struct got_tree_object **tree,
1198 struct got_repository *repo, struct got_object_id *id)
1200 const struct got_error *err;
1201 struct got_object *obj;
1205 err = got_object_open(&obj, repo, id);
1208 if (got_object_get_type(obj) != GOT_OBJ_TYPE_TREE) {
1209 err = got_error(GOT_ERR_OBJ_TYPE);
1213 err = got_object_tree_open(tree, repo, obj);
1215 got_object_close(obj);
1220 got_object_tree_close(struct got_tree_object *tree)
1222 struct got_tree_entry *te;
1224 if (tree->refcnt > 0) {
1226 if (tree->refcnt > 0)
1230 while (!SIMPLEQ_EMPTY(&tree->entries.head)) {
1231 te = SIMPLEQ_FIRST(&tree->entries.head);
1232 SIMPLEQ_REMOVE_HEAD(&tree->entries.head, entry);
1233 tree_entry_close(te);
1239 const struct got_tree_entries *
1240 got_object_tree_get_entries(struct got_tree_object *tree)
1242 return &tree->entries;
1245 static const struct got_error *
1246 read_blob_object_privsep_child(int outfd, int infd, int imsg_fds[2])
1248 const struct got_error *err = NULL;
1249 struct imsgbuf ibuf;
1252 FILE *infile = NULL;
1254 setproctitle("read blob object");
1256 imsg_init(&ibuf, imsg_fds[1]);
1258 /* revoke access to most system calls */
1259 if (pledge("stdio", NULL) == -1) {
1260 err = got_error_from_errno();
1264 infile = fdopen(infd, "rb");
1265 if (infile == NULL) {
1266 err = got_error_from_errno();
1270 err = got_inflate_to_fd(&size, infile, outfd);
1275 err = got_privsep_send_blob(&ibuf, size);
1278 got_privsep_send_error(&ibuf, err);
1287 static const struct got_error *
1288 read_blob_object_privsep(size_t *size, int outfd, int infd)
1290 struct imsgbuf parent_ibuf;
1292 const struct got_error *err = NULL, *err_child = NULL;
1295 if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_fds) == -1)
1296 return got_error_from_errno();
1300 return got_error_from_errno();
1301 else if (pid == 0) {
1302 read_blob_object_privsep_child(outfd, infd, imsg_fds);
1307 imsg_init(&parent_ibuf, imsg_fds[0]);
1308 err = got_privsep_recv_blob(size, &parent_ibuf);
1309 imsg_clear(&parent_ibuf);
1310 err_child = wait_for_child(pid);
1312 if (lseek(outfd, SEEK_SET, 0) == -1)
1313 err = got_error_from_errno();
1314 return err ? err : err_child;
1317 const struct got_error *
1318 got_object_blob_open(struct got_blob_object **blob,
1319 struct got_repository *repo, struct got_object *obj, size_t blocksize)
1321 const struct got_error *err = NULL;
1323 if (obj->type != GOT_OBJ_TYPE_BLOB)
1324 return got_error(GOT_ERR_OBJ_TYPE);
1326 if (blocksize < obj->hdrlen)
1327 return got_error(GOT_ERR_NO_SPACE);
1329 *blob = calloc(1, sizeof(**blob));
1331 return got_error_from_errno();
1333 (*blob)->read_buf = malloc(blocksize);
1334 if ((*blob)->read_buf == NULL) {
1335 err = got_error_from_errno();
1338 if (obj->flags & GOT_OBJ_FLAG_PACKED) {
1339 err = got_packfile_extract_object(&((*blob)->f), obj, repo);
1347 err = open_loose_object(&infd, obj, repo);
1352 outfd = got_opentempfd();
1354 err = got_error_from_errno();
1359 err = read_blob_object_privsep(&size, outfd, infd);
1364 if (size != obj->hdrlen + obj->size) {
1365 err = got_error(GOT_ERR_PRIVSEP_LEN);
1370 if (fstat(outfd, &sb) == -1) {
1371 err = got_error_from_errno();
1376 if (sb.st_size != size) {
1377 err = got_error(GOT_ERR_PRIVSEP_LEN);
1382 (*blob)->f = fdopen(outfd, "rb");
1383 if ((*blob)->f == NULL) {
1384 err = got_error_from_errno();
1390 (*blob)->hdrlen = obj->hdrlen;
1391 (*blob)->blocksize = blocksize;
1392 memcpy(&(*blob)->id.sha1, obj->id.sha1, SHA1_DIGEST_LENGTH);
1398 free((*blob)->read_buf);
1405 const struct got_error *
1406 got_object_open_as_blob(struct got_blob_object **blob,
1407 struct got_repository *repo, struct got_object_id *id,
1410 const struct got_error *err;
1411 struct got_object *obj;
1415 err = got_object_open(&obj, repo, id);
1418 if (got_object_get_type(obj) != GOT_OBJ_TYPE_BLOB) {
1419 err = got_error(GOT_ERR_OBJ_TYPE);
1423 err = got_object_blob_open(blob, repo, obj, blocksize);
1425 got_object_close(obj);
1430 got_object_blob_close(struct got_blob_object *blob)
1432 free(blob->read_buf);
1438 got_object_blob_id_str(struct got_blob_object *blob, char *buf, size_t size)
1440 return got_sha1_digest_to_str(blob->id.sha1, buf, size);
1444 got_object_blob_get_hdrlen(struct got_blob_object *blob)
1446 return blob->hdrlen;
1450 got_object_blob_get_read_buf(struct got_blob_object *blob)
1452 return blob->read_buf;
1455 const struct got_error *
1456 got_object_blob_read_block(size_t *outlenp, struct got_blob_object *blob)
1460 n = fread(blob->read_buf, 1, blob->blocksize, blob->f);
1461 if (n == 0 && ferror(blob->f))
1462 return got_ferror(blob->f, GOT_ERR_IO);
1467 const struct got_error *
1468 got_object_blob_dump_to_file(size_t *total_len, size_t *nlines,
1469 FILE *outfile, struct got_blob_object *blob)
1471 const struct got_error *err = NULL;
1481 hdrlen = got_object_blob_get_hdrlen(blob);
1483 err = got_object_blob_read_block(&len, blob);
1490 buf = got_object_blob_get_read_buf(blob);
1492 for (i = 0; i < len; i++) {
1497 /* Skip blob object header first time around. */
1498 fwrite(buf + hdrlen, len - hdrlen, 1, outfile);
1508 static struct got_tree_entry *
1509 find_entry_by_name(struct got_tree_object *tree, const char *name)
1511 struct got_tree_entry *te;
1513 SIMPLEQ_FOREACH(te, &tree->entries.head, entry) {
1514 if (strcmp(te->name, name) == 0)
1520 const struct got_error *
1521 got_object_open_by_path(struct got_object **obj, struct got_repository *repo,
1522 struct got_object_id *commit_id, const char *path)
1524 const struct got_error *err = NULL;
1525 struct got_commit_object *commit = NULL;
1526 struct got_tree_object *tree = NULL;
1527 struct got_tree_entry *te = NULL;
1528 char *seg, *s, *s0 = NULL;
1529 size_t len = strlen(path);
1533 /* We are expecting an absolute in-repository path. */
1535 return got_error(GOT_ERR_NOT_ABSPATH);
1537 err = got_object_open_as_commit(&commit, repo, commit_id);
1541 /* Handle opening of root of commit's tree. */
1542 if (path[1] == '\0') {
1543 err = got_object_open(obj, repo, commit->tree_id);
1547 err = got_object_open_as_tree(&tree, repo, commit->tree_id);
1553 err = got_error_from_errno();
1556 err = got_canonpath(path, s0, len + 1);
1561 s++; /* skip leading '/' */
1565 struct got_tree_object *next_tree;
1574 /* end of path segment */
1577 te = find_entry_by_name(tree, seg);
1579 err = got_error(GOT_ERR_NO_OBJ);
1590 err = got_object_open_as_tree(&next_tree, repo,
1595 got_object_tree_close(tree);
1601 err = got_object_open(obj, repo, te->id);
1603 err = got_error(GOT_ERR_NO_OBJ);
1607 got_object_commit_close(commit);
1609 got_object_tree_close(tree);