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();
334 static const struct got_error *
335 open_loose_object(int *fd, struct got_object *obj, struct got_repository *repo)
337 const struct got_error *err = NULL;
340 err = object_path(&path, &obj->id, repo);
343 *fd = open(path, O_RDONLY | O_NOFOLLOW, GOT_DEFAULT_FILE_MODE);
345 err = got_error_from_errno();
353 const struct got_error *
354 got_object_open(struct got_object **obj, struct got_repository *repo,
355 struct got_object_id *id)
357 const struct got_error *err = NULL;
361 *obj = got_repo_get_cached_object(repo, id);
367 err = object_path(&path, id, repo);
371 fd = open(path, O_RDONLY | O_NOFOLLOW, GOT_DEFAULT_FILE_MODE);
373 if (errno != ENOENT) {
374 err = got_error_from_errno();
377 err = got_packfile_open_object(obj, id, repo);
381 err = got_error(GOT_ERR_NO_OBJ);
383 err = read_object_header_privsep(obj, fd);
386 memcpy((*obj)->id.sha1, id->sha1, SHA1_DIGEST_LENGTH);
391 err = got_repo_cache_object(repo, id, *obj);
401 const struct got_error *
402 got_object_open_by_id_str(struct got_object **obj, struct got_repository *repo,
405 struct got_object_id id;
407 if (!got_parse_sha1_digest(id.sha1, id_str))
408 return got_error(GOT_ERR_BAD_OBJ_ID_STR);
410 return got_object_open(obj, repo, &id);
414 got_object_close(struct got_object *obj)
416 if (obj->refcnt > 0) {
422 if (obj->flags & GOT_OBJ_FLAG_DELTIFIED) {
423 struct got_delta *delta;
424 while (!SIMPLEQ_EMPTY(&obj->deltas.entries)) {
425 delta = SIMPLEQ_FIRST(&obj->deltas.entries);
426 SIMPLEQ_REMOVE_HEAD(&obj->deltas.entries, entry);
427 got_delta_close(delta);
430 if (obj->flags & GOT_OBJ_FLAG_PACKED)
431 free(obj->path_packfile);
435 struct got_commit_object *
436 got_object_commit_alloc_partial(void)
438 struct got_commit_object *commit;
440 commit = calloc(1, sizeof(*commit));
443 commit->tree_id = calloc(1, sizeof(*commit->tree_id));
444 if (commit->tree_id == NULL) {
449 SIMPLEQ_INIT(&commit->parent_ids);
454 const struct got_error *
455 got_object_open_as_commit(struct got_commit_object **commit,
456 struct got_repository *repo, struct got_object_id *id)
458 const struct got_error *err;
459 struct got_object *obj;
463 err = got_object_open(&obj, repo, id);
466 if (got_object_get_type(obj) != GOT_OBJ_TYPE_COMMIT) {
467 err = got_error(GOT_ERR_OBJ_TYPE);
471 err = got_object_commit_open(commit, repo, obj);
473 got_object_close(obj);
477 const struct got_error *
478 got_object_qid_alloc(struct got_object_qid **qid, struct got_object_id *id)
480 const struct got_error *err = NULL;
482 *qid = calloc(1, sizeof(**qid));
484 return got_error_from_errno();
486 (*qid)->id = got_object_id_dup(id);
487 if ((*qid)->id == NULL) {
488 err = got_error_from_errno();
489 got_object_qid_free(*qid);
498 got_object_qid_free(struct got_object_qid *qid)
504 const struct got_error *
505 got_object_commit_add_parent(struct got_commit_object *commit,
508 const struct got_error *err = NULL;
509 struct got_object_qid *qid;
511 qid = malloc(sizeof(*qid));
513 return got_error_from_errno();
515 qid->id = malloc(sizeof(*qid->id));
516 if (qid->id == NULL) {
517 err = got_error_from_errno();
518 got_object_qid_free(qid);
522 if (!got_parse_sha1_digest(qid->id->sha1, id_str)) {
523 err = got_error(GOT_ERR_BAD_OBJ_DATA);
529 SIMPLEQ_INSERT_TAIL(&commit->parent_ids, qid, entry);
535 static const struct got_error *
536 parse_gmtoff(time_t *gmtoff, const char *tzstr)
539 const char *p = tzstr;
547 return got_error(GOT_ERR_BAD_OBJ_DATA);
549 if (!isdigit(*p) && !isdigit(*(p + 1)))
550 return got_error(GOT_ERR_BAD_OBJ_DATA);
551 h = (((*p - '0') * 10) + (*(p + 1) - '0'));
554 if (!isdigit(*p) && !isdigit(*(p + 1)))
555 return got_error(GOT_ERR_BAD_OBJ_DATA);
556 m = ((*p - '0') * 10) + (*(p + 1) - '0');
558 *gmtoff = (h * 60 * 60 + m * 60) * sign;
562 static const struct got_error *
563 parse_commit_time(struct tm *tm, char *committer)
565 const struct got_error *err = NULL;
571 /* Parse and strip off trailing timezone indicator string. */
572 space = strrchr(committer, ' ');
574 return got_error(GOT_ERR_BAD_OBJ_DATA);
575 tzstr = strdup(space + 1);
577 return got_error_from_errno();
578 err = parse_gmtoff(&gmtoff, tzstr);
584 /* Timestamp is separated from committer name + email by space. */
585 space = strrchr(committer, ' ');
587 return got_error(GOT_ERR_BAD_OBJ_DATA);
589 /* Timestamp parsed here is expressed in comitter's local time. */
590 time = strtonum(space + 1, 0, INT64_MAX, &errstr);
592 return got_error(GOT_ERR_BAD_OBJ_DATA);
594 /* Express the time stamp in UTC. */
595 memset(tm, 0, sizeof(*tm));
597 if (localtime_r(&time, tm) == NULL)
598 return got_error_from_errno();
599 tm->tm_gmtoff = gmtoff;
601 /* Strip off parsed time information, leaving just author and email. */
607 static const struct got_error *
608 parse_commit_object(struct got_commit_object **commit, char *buf, size_t len)
610 const struct got_error *err = NULL;
613 ssize_t remain = (ssize_t)len;
615 *commit = got_object_commit_alloc_partial();
617 return got_error_from_errno();
619 tlen = strlen(GOT_COMMIT_TAG_TREE);
620 if (strncmp(s, GOT_COMMIT_TAG_TREE, tlen) == 0) {
622 if (remain < SHA1_DIGEST_STRING_LENGTH) {
623 err = got_error(GOT_ERR_BAD_OBJ_DATA);
627 if (!got_parse_sha1_digest((*commit)->tree_id->sha1, s)) {
628 err = got_error(GOT_ERR_BAD_OBJ_DATA);
631 remain -= SHA1_DIGEST_STRING_LENGTH;
632 s += SHA1_DIGEST_STRING_LENGTH;
634 err = got_error(GOT_ERR_BAD_OBJ_DATA);
638 tlen = strlen(GOT_COMMIT_TAG_PARENT);
639 while (strncmp(s, GOT_COMMIT_TAG_PARENT, tlen) == 0) {
641 if (remain < SHA1_DIGEST_STRING_LENGTH) {
642 err = got_error(GOT_ERR_BAD_OBJ_DATA);
646 err = got_object_commit_add_parent(*commit, s);
650 remain -= SHA1_DIGEST_STRING_LENGTH;
651 s += SHA1_DIGEST_STRING_LENGTH;
654 tlen = strlen(GOT_COMMIT_TAG_AUTHOR);
655 if (strncmp(s, GOT_COMMIT_TAG_AUTHOR, tlen) == 0) {
661 err = got_error(GOT_ERR_BAD_OBJ_DATA);
667 err = got_error(GOT_ERR_BAD_OBJ_DATA);
672 err = parse_commit_time(&(*commit)->tm_author, s);
675 (*commit)->author = strdup(s);
676 if ((*commit)->author == NULL) {
677 err = got_error_from_errno();
684 tlen = strlen(GOT_COMMIT_TAG_COMMITTER);
685 if (strncmp(s, GOT_COMMIT_TAG_COMMITTER, tlen) == 0) {
691 err = got_error(GOT_ERR_BAD_OBJ_DATA);
697 err = got_error(GOT_ERR_BAD_OBJ_DATA);
702 err = parse_commit_time(&(*commit)->tm_committer, s);
705 (*commit)->committer = strdup(s);
706 if ((*commit)->committer == NULL) {
707 err = got_error_from_errno();
714 (*commit)->logmsg = strndup(s, remain);
715 if ((*commit)->logmsg == NULL) {
716 err = got_error_from_errno();
721 got_object_commit_close(*commit);
728 tree_entry_close(struct got_tree_entry *te)
735 struct got_tree_entry *
736 got_alloc_tree_entry_partial(void)
738 struct got_tree_entry *te;
740 te = calloc(1, sizeof(*te));
744 te->id = calloc(1, sizeof(*te->id));
745 if (te->id == NULL) {
752 static const struct got_error *
753 parse_tree_entry(struct got_tree_entry **te, size_t *elen, char *buf,
756 char *p = buf, *space;
757 const struct got_error *err = NULL;
759 *te = got_alloc_tree_entry_partial();
761 return got_error_from_errno();
763 *elen = strlen(buf) + 1;
764 if (*elen > maxlen) {
767 return got_error(GOT_ERR_BAD_OBJ_DATA);
770 space = strchr(buf, ' ');
772 err = got_error(GOT_ERR_BAD_OBJ_DATA);
778 if (*p < '0' && *p > '7') {
779 err = got_error(GOT_ERR_BAD_OBJ_DATA);
783 (*te)->mode |= *p - '0';
787 (*te)->name = strdup(space + 1);
788 if (*elen > maxlen || maxlen - *elen < SHA1_DIGEST_LENGTH) {
789 err = got_error(GOT_ERR_BAD_OBJ_DATA);
792 buf += strlen(buf) + 1;
793 memcpy((*te)->id->sha1, buf, SHA1_DIGEST_LENGTH);
794 *elen += SHA1_DIGEST_LENGTH;
797 tree_entry_close(*te);
803 static const struct got_error *
804 parse_tree_object(struct got_tree_object **tree, uint8_t *buf, size_t len)
806 const struct got_error *err;
809 *tree = calloc(1, sizeof(**tree));
811 return got_error_from_errno();
813 SIMPLEQ_INIT(&(*tree)->entries.head);
816 struct got_tree_entry *te;
819 err = parse_tree_entry(&te, &elen, buf, remain);
822 (*tree)->entries.nentries++;
823 SIMPLEQ_INSERT_TAIL(&(*tree)->entries.head, te, entry);
829 got_object_tree_close(*tree);
830 return got_error(GOT_ERR_BAD_OBJ_DATA);
836 static const struct got_error *
837 read_to_mem(uint8_t **outbuf, size_t *outlen, FILE *f)
839 const struct got_error *err = NULL;
840 static const size_t blocksize = 512;
841 size_t n, total, remain;
847 buf = malloc(blocksize);
849 return got_error_from_errno();
856 newbuf = reallocarray(buf, 1, total + blocksize);
857 if (newbuf == NULL) {
858 err = got_error_from_errno();
864 n = fread(buf + total, 1, remain, f);
867 err = got_ferror(f, GOT_ERR_IO);
885 static const struct got_error *
886 read_commit_object(struct got_commit_object **commit, struct got_object *obj,
889 const struct got_error *err = NULL;
893 if (obj->flags & GOT_OBJ_FLAG_PACKED)
894 err = read_to_mem(&p, &len, f);
896 err = got_inflate_to_mem(&p, &len, f);
900 if (len < obj->hdrlen + obj->size) {
901 err = got_error(GOT_ERR_BAD_OBJ_DATA);
905 /* Skip object header. */
907 err = parse_commit_object(commit, p + obj->hdrlen, len);
914 read_commit_object_privsep_child(struct got_object *obj, int obj_fd,
917 const struct got_error *err = NULL;
918 struct got_commit_object *commit = NULL;
923 setproctitle("read commit object");
925 imsg_init(&ibuf, imsg_fds[1]);
927 /* revoke access to most system calls */
928 if (pledge("stdio", NULL) == -1) {
929 err = got_error_from_errno();
933 f = fdopen(obj_fd, "rb");
935 err = got_error_from_errno();
940 err = read_commit_object(&commit, obj, f);
944 err = got_privsep_send_commit(&ibuf, commit);
947 got_object_commit_close(commit);
949 got_privsep_send_error(&ibuf, err);
959 static const struct got_error *
960 read_commit_object_privsep(struct got_commit_object **commit,
961 struct got_repository *repo, struct got_object *obj, int fd)
963 const struct got_error *err = NULL, *err_child = NULL;
964 struct imsgbuf parent_ibuf;
968 if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_fds) == -1)
969 return got_error_from_errno();
973 return got_error_from_errno();
975 read_commit_object_privsep_child(obj, fd, imsg_fds);
980 imsg_init(&parent_ibuf, imsg_fds[0]);
981 err = got_privsep_recv_commit(commit, &parent_ibuf);
982 imsg_clear(&parent_ibuf);
983 err_child = wait_for_child(pid);
985 return err ? err : err_child;
988 const struct got_error *
989 got_object_commit_open(struct got_commit_object **commit,
990 struct got_repository *repo, struct got_object *obj)
992 const struct got_error *err = NULL;
994 *commit = got_repo_get_cached_commit(repo, &obj->id);
995 if (*commit != NULL) {
1000 if (obj->type != GOT_OBJ_TYPE_COMMIT)
1001 return got_error(GOT_ERR_OBJ_TYPE);
1003 if (obj->flags & GOT_OBJ_FLAG_PACKED) {
1006 err = got_packfile_extract_object_to_mem(&buf, &len, obj, repo);
1010 err = parse_commit_object(commit, buf, len);
1014 err = open_loose_object(&fd, obj, repo);
1017 err = read_commit_object_privsep(commit, repo, obj, fd);
1022 (*commit)->refcnt++;
1023 err = got_repo_cache_commit(repo, &obj->id, *commit);
1030 got_object_commit_close(struct got_commit_object *commit)
1032 struct got_object_qid *qid;
1034 if (commit->refcnt > 0) {
1036 if (commit->refcnt > 0)
1040 while (!SIMPLEQ_EMPTY(&commit->parent_ids)) {
1041 qid = SIMPLEQ_FIRST(&commit->parent_ids);
1042 SIMPLEQ_REMOVE_HEAD(&commit->parent_ids, entry);
1043 got_object_qid_free(qid);
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);