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_zbuf.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 = calloc(1, 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, FILE *f)
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 = calloc(zbsize, sizeof(char));
198 return got_error_from_errno();
200 err = got_inflate_init(&zb, NULL, zbsize);
207 err = got_inflate_read(&zb, f, &outlen);
210 if (strchr(zb.outbuf, '\0') == NULL) {
211 buf = recallocarray(buf, 1 + i, 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;
237 setproctitle("read object header");
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();
247 f = fdopen(obj_fd, "rb");
249 err = got_error_from_errno();
254 err = read_object_header(&obj, f);
258 err = got_privsep_send_obj(&ibuf, obj, 0);
261 got_object_close(obj);
263 got_privsep_send_error(&ibuf, err);
273 static const struct got_error *
274 wait_for_child(pid_t pid)
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);
289 static const struct got_error *
290 read_object_header_privsep(struct got_object **obj, int fd)
292 struct imsgbuf parent_ibuf;
294 const struct got_error *err = NULL, *err_child = NULL;
297 if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_fds) == -1)
298 return got_error_from_errno();
302 return got_error_from_errno();
304 read_object_header_privsep_child(fd, imsg_fds);
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);
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;
322 char *path_objects = got_repo_get_path_objects(repo);
326 if (path_objects == NULL)
327 return got_error_from_errno();
329 err = got_object_id_str(&hex, id);
333 if (asprintf(path, "%s/%.2x/%s", path_objects,
334 id->sha1[0], hex + 2) == -1)
335 err = got_error_from_errno();
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;
348 err = object_path(&path, &obj->id, repo);
351 *fd = open(path, O_RDONLY | O_NOFOLLOW, GOT_DEFAULT_FILE_MODE);
353 err = got_error_from_errno();
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;
369 *obj = got_repo_get_cached_object(repo, id);
375 err = object_path(&path, id, repo);
379 fd = open(path, O_RDONLY | O_NOFOLLOW, GOT_DEFAULT_FILE_MODE);
381 if (errno != ENOENT) {
382 err = got_error_from_errno();
385 err = got_packfile_open_object(obj, id, repo);
389 err = got_error(GOT_ERR_NO_OBJ);
391 err = read_object_header_privsep(obj, fd);
394 memcpy((*obj)->id.sha1, id->sha1, SHA1_DIGEST_LENGTH);
399 err = got_repo_cache_object(repo, id, *obj);
409 const struct got_error *
410 got_object_open_by_id_str(struct got_object **obj, struct got_repository *repo,
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);
422 got_object_close(struct got_object *obj)
424 if (obj->refcnt > 0) {
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);
443 struct got_commit_object *
444 got_object_commit_alloc_partial(void)
446 struct got_commit_object *commit;
448 commit = calloc(1, sizeof(*commit));
451 commit->tree_id = calloc(1, sizeof(*commit->tree_id));
452 if (commit->tree_id == NULL) {
457 SIMPLEQ_INIT(&commit->parent_ids);
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;
471 err = got_object_open(&obj, repo, id);
474 if (got_object_get_type(obj) != GOT_OBJ_TYPE_COMMIT) {
475 err = got_error(GOT_ERR_OBJ_TYPE);
479 err = got_object_commit_open(commit, repo, obj);
481 got_object_close(obj);
485 const struct got_error *
486 got_object_commit_add_parent(struct got_commit_object *commit,
489 const struct got_error *err = NULL;
490 struct got_object_qid *qid;
492 qid = calloc(1, sizeof(*qid));
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();
503 if (!got_parse_sha1_digest(qid->id->sha1, id_str)) {
504 err = got_error(GOT_ERR_BAD_OBJ_DATA);
510 SIMPLEQ_INSERT_TAIL(&commit->parent_ids, qid, entry);
516 static const struct got_error *
517 parse_gmtoff(time_t *gmtoff, const char *tzstr)
520 const char *p = tzstr;
528 return got_error(GOT_ERR_BAD_OBJ_DATA);
530 if (!isdigit(*p) && !isdigit(*(p + 1)))
531 return got_error(GOT_ERR_BAD_OBJ_DATA);
532 h = (((*p - '0') * 10) + (*(p + 1) - '0'));
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;
543 static const struct got_error *
544 parse_commit_time(struct tm *tm, char *committer)
546 const struct got_error *err = NULL;
552 /* Parse and strip off trailing timezone indicator string. */
553 space = strrchr(committer, ' ');
555 return got_error(GOT_ERR_BAD_OBJ_DATA);
556 tzstr = strdup(space + 1);
558 return got_error_from_errno();
559 err = parse_gmtoff(&gmtoff, tzstr);
565 /* Timestamp is separated from committer name + email by space. */
566 space = strrchr(committer, ' ');
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);
573 return got_error(GOT_ERR_BAD_OBJ_DATA);
575 /* Express the time stamp in UTC. */
576 memset(tm, 0, sizeof(*tm));
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. */
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;
594 ssize_t remain = (ssize_t)len;
596 *commit = got_object_commit_alloc_partial();
598 return got_error_from_errno();
600 tlen = strlen(GOT_COMMIT_TAG_TREE);
601 if (strncmp(s, GOT_COMMIT_TAG_TREE, tlen) == 0) {
603 if (remain < SHA1_DIGEST_STRING_LENGTH) {
604 err = got_error(GOT_ERR_BAD_OBJ_DATA);
608 if (!got_parse_sha1_digest((*commit)->tree_id->sha1, s)) {
609 err = got_error(GOT_ERR_BAD_OBJ_DATA);
612 remain -= SHA1_DIGEST_STRING_LENGTH;
613 s += SHA1_DIGEST_STRING_LENGTH;
615 err = got_error(GOT_ERR_BAD_OBJ_DATA);
619 tlen = strlen(GOT_COMMIT_TAG_PARENT);
620 while (strncmp(s, GOT_COMMIT_TAG_PARENT, tlen) == 0) {
622 if (remain < SHA1_DIGEST_STRING_LENGTH) {
623 err = got_error(GOT_ERR_BAD_OBJ_DATA);
627 err = got_object_commit_add_parent(*commit, s);
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) {
642 err = got_error(GOT_ERR_BAD_OBJ_DATA);
648 err = got_error(GOT_ERR_BAD_OBJ_DATA);
653 err = parse_commit_time(&(*commit)->tm_author, s);
656 (*commit)->author = strdup(s);
657 if ((*commit)->author == NULL) {
658 err = got_error_from_errno();
665 tlen = strlen(GOT_COMMIT_TAG_COMMITTER);
666 if (strncmp(s, GOT_COMMIT_TAG_COMMITTER, tlen) == 0) {
672 err = got_error(GOT_ERR_BAD_OBJ_DATA);
678 err = got_error(GOT_ERR_BAD_OBJ_DATA);
683 err = parse_commit_time(&(*commit)->tm_committer, s);
686 (*commit)->committer = strdup(s);
687 if ((*commit)->committer == NULL) {
688 err = got_error_from_errno();
695 (*commit)->logmsg = strndup(s, remain);
696 if ((*commit)->logmsg == NULL) {
697 err = got_error_from_errno();
702 got_object_commit_close(*commit);
709 tree_entry_close(struct got_tree_entry *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));
725 te->id = calloc(1, sizeof(*te->id));
726 if (te->id == NULL) {
733 static const struct got_error *
734 parse_tree_entry(struct got_tree_entry **te, size_t *elen, char *buf,
737 char *p = buf, *space;
738 const struct got_error *err = NULL;
740 *te = got_alloc_tree_entry_partial();
742 return got_error_from_errno();
744 *elen = strlen(buf) + 1;
745 if (*elen > maxlen) {
748 return got_error(GOT_ERR_BAD_OBJ_DATA);
751 space = strchr(buf, ' ');
753 err = got_error(GOT_ERR_BAD_OBJ_DATA);
759 if (*p < '0' && *p > '7') {
760 err = got_error(GOT_ERR_BAD_OBJ_DATA);
764 (*te)->mode |= *p - '0';
768 (*te)->name = strdup(space + 1);
769 if (*elen > maxlen || maxlen - *elen < SHA1_DIGEST_LENGTH) {
770 err = got_error(GOT_ERR_BAD_OBJ_DATA);
773 buf += strlen(buf) + 1;
774 memcpy((*te)->id->sha1, buf, SHA1_DIGEST_LENGTH);
775 *elen += SHA1_DIGEST_LENGTH;
778 tree_entry_close(*te);
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;
790 *tree = calloc(1, sizeof(**tree));
792 return got_error_from_errno();
794 SIMPLEQ_INIT(&(*tree)->entries);
797 struct got_tree_entry *te;
800 err = parse_tree_entry(&te, &elen, buf, remain);
804 SIMPLEQ_INSERT_TAIL(&(*tree)->entries, te, entry);
810 got_object_tree_close(*tree);
811 return got_error(GOT_ERR_BAD_OBJ_DATA);
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;
828 buf = calloc(1, blocksize);
830 return got_error_from_errno();
837 newbuf = reallocarray(buf, 1, total + blocksize);
838 if (newbuf == NULL) {
839 err = got_error_from_errno();
845 n = fread(buf + total, 1, remain, f);
848 err = got_ferror(f, GOT_ERR_IO);
866 static const struct got_error *
867 read_commit_object(struct got_commit_object **commit, struct got_object *obj,
870 const struct got_error *err = NULL;
874 if (obj->flags & GOT_OBJ_FLAG_PACKED)
875 err = read_to_mem(&p, &len, f);
877 err = got_inflate_to_mem(&p, &len, f);
881 if (len < obj->hdrlen + obj->size) {
882 err = got_error(GOT_ERR_BAD_OBJ_DATA);
886 /* Skip object header. */
888 err = parse_commit_object(commit, p + obj->hdrlen, len);
895 read_commit_object_privsep_child(struct got_object *obj, int obj_fd,
898 const struct got_error *err = NULL;
899 struct got_commit_object *commit = NULL;
904 setproctitle("read commit object");
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();
914 f = fdopen(obj_fd, "rb");
916 err = got_error_from_errno();
921 err = read_commit_object(&commit, obj, f);
925 err = got_privsep_send_commit(&ibuf, commit);
928 got_object_commit_close(commit);
930 got_privsep_send_error(&ibuf, err);
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;
949 if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_fds) == -1)
950 return got_error_from_errno();
954 return got_error_from_errno();
956 read_commit_object_privsep_child(obj, fd, imsg_fds);
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);
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) {
981 if (obj->type != GOT_OBJ_TYPE_COMMIT)
982 return got_error(GOT_ERR_OBJ_TYPE);
984 if (obj->flags & GOT_OBJ_FLAG_PACKED) {
987 err = got_packfile_extract_object_to_mem(&buf, &len, obj, repo);
991 err = parse_commit_object(commit, buf, len);
995 err = open_loose_object(&fd, obj, repo);
998 err = read_commit_object_privsep(commit, repo, obj, fd);
1003 (*commit)->refcnt++;
1004 err = got_repo_cache_commit(repo, &obj->id, *commit);
1011 got_object_commit_close(struct got_commit_object *commit)
1013 struct got_object_qid *qid;
1015 if (commit->refcnt > 0) {
1017 if (commit->refcnt > 0)
1021 while (!SIMPLEQ_EMPTY(&commit->parent_ids)) {
1022 qid = SIMPLEQ_FIRST(&commit->parent_ids);
1023 SIMPLEQ_REMOVE_HEAD(&commit->parent_ids, entry);
1028 free(commit->tree_id);
1029 free(commit->author);
1030 free(commit->committer);
1031 free(commit->logmsg);
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;
1042 if (obj->flags & GOT_OBJ_FLAG_PACKED)
1043 err = read_to_mem(&p, &len, f);
1045 err = got_inflate_to_mem(&p, &len, f);
1049 if (len < obj->hdrlen + obj->size) {
1050 err = got_error(GOT_ERR_BAD_OBJ_DATA);
1054 /* Skip object header. */
1056 err = parse_tree_object(tree, p + obj->hdrlen, len);
1063 read_tree_object_privsep_child(struct got_object *obj, int obj_fd,
1066 const struct got_error *err = NULL;
1067 struct got_tree_object *tree = NULL;
1068 struct imsgbuf ibuf;
1072 setproctitle("read tree object");
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();
1082 f = fdopen(obj_fd, "rb");
1084 err = got_error_from_errno();
1089 err = read_tree_object(&tree, obj, f);
1093 err = got_privsep_send_tree(&ibuf, tree);
1096 got_object_tree_close(tree);
1098 got_privsep_send_error(&ibuf, err);
1108 static const struct got_error *
1109 read_tree_object_privsep(struct got_tree_object **tree, struct got_object *obj,
1112 const struct got_error *err = NULL, *err_child = NULL;
1113 struct imsgbuf parent_ibuf;
1117 if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_fds) == -1)
1118 return got_error_from_errno();
1122 return got_error_from_errno();
1123 else if (pid == 0) {
1124 read_tree_object_privsep_child(obj, fd, imsg_fds);
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);
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) {
1149 if (obj->type != GOT_OBJ_TYPE_TREE)
1150 return got_error(GOT_ERR_OBJ_TYPE);
1152 if (obj->flags & GOT_OBJ_FLAG_PACKED) {
1155 err = got_packfile_extract_object_to_mem(&buf, &len, obj, repo);
1159 err = parse_tree_object(tree, buf, len);
1163 err = open_loose_object(&fd, obj, repo);
1166 err = read_tree_object_privsep(tree, obj, fd);
1172 err = got_repo_cache_tree(repo, &obj->id, *tree);
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;
1187 err = got_object_open(&obj, repo, id);
1190 if (got_object_get_type(obj) != GOT_OBJ_TYPE_TREE) {
1191 err = got_error(GOT_ERR_OBJ_TYPE);
1195 err = got_object_tree_open(tree, repo, obj);
1197 got_object_close(obj);
1202 got_object_tree_close(struct got_tree_object *tree)
1204 struct got_tree_entry *te;
1206 if (tree->refcnt > 0) {
1208 if (tree->refcnt > 0)
1212 while (!SIMPLEQ_EMPTY(&tree->entries)) {
1213 te = SIMPLEQ_FIRST(&tree->entries);
1214 SIMPLEQ_REMOVE_HEAD(&tree->entries, entry);
1215 tree_entry_close(te);
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;
1228 FILE *infile = NULL;
1230 setproctitle("read blob object");
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();
1240 infile = fdopen(infd, "rb");
1241 if (infile == NULL) {
1242 err = got_error_from_errno();
1246 err = got_inflate_to_fd(&size, infile, outfd);
1251 err = got_privsep_send_blob(&ibuf, size);
1254 got_privsep_send_error(&ibuf, err);
1263 static const struct got_error *
1264 read_blob_object_privsep(size_t *size, int outfd, int infd)
1266 struct imsgbuf parent_ibuf;
1268 const struct got_error *err = NULL, *err_child = NULL;
1271 if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_fds) == -1)
1272 return got_error_from_errno();
1276 return got_error_from_errno();
1277 else if (pid == 0) {
1278 read_blob_object_privsep_child(outfd, infd, imsg_fds);
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);
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));
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();
1314 if (obj->flags & GOT_OBJ_FLAG_PACKED) {
1315 err = got_packfile_extract_object(&((*blob)->f), obj, repo);
1323 err = open_loose_object(&infd, obj, repo);
1328 outfd = got_opentempfd();
1330 err = got_error_from_errno();
1335 err = read_blob_object_privsep(&size, outfd, infd);
1340 if (size != obj->hdrlen + obj->size) {
1341 err = got_error(GOT_ERR_PRIVSEP_LEN);
1346 if (fstat(outfd, &sb) == -1) {
1347 err = got_error_from_errno();
1352 if (sb.st_size != size) {
1353 err = got_error(GOT_ERR_PRIVSEP_LEN);
1358 (*blob)->f = fdopen(outfd, "rb");
1359 if ((*blob)->f == NULL) {
1360 err = got_error_from_errno();
1366 (*blob)->hdrlen = obj->hdrlen;
1367 (*blob)->blocksize = blocksize;
1368 memcpy(&(*blob)->id.sha1, obj->id.sha1, SHA1_DIGEST_LENGTH);
1374 free((*blob)->read_buf);
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,
1386 const struct got_error *err;
1387 struct got_object *obj;
1391 err = got_object_open(&obj, repo, id);
1394 if (got_object_get_type(obj) != GOT_OBJ_TYPE_BLOB) {
1395 err = got_error(GOT_ERR_OBJ_TYPE);
1399 err = got_object_blob_open(blob, repo, obj, blocksize);
1401 got_object_close(obj);
1406 got_object_blob_close(struct got_blob_object *blob)
1408 free(blob->read_buf);
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);
1420 got_object_blob_get_hdrlen(struct got_blob_object *blob)
1422 return blob->hdrlen;
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)
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);
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;
1451 hdrlen = got_object_blob_get_hdrlen(blob);
1453 err = got_object_blob_read_block(&len, blob);
1459 /* Skip blob object header first time around. */
1460 fwrite(got_object_blob_get_read_buf(blob) + hdrlen,
1461 len - hdrlen, 1, outfile);
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)
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);
1496 /* We are expecting an absolute in-repository path. */
1498 return got_error(GOT_ERR_NOT_ABSPATH);
1500 err = got_object_open_as_commit(&commit, repo, commit_id);
1504 /* Handle opening of root of commit's tree. */
1505 if (path[1] == '\0') {
1506 err = got_object_open(obj, repo, commit->tree_id);
1512 err = got_object_open_as_tree(&tree, repo, commit->tree_id);
1518 err = got_error_from_errno();
1521 err = got_canonpath(path, s0, len + 1);
1526 s++; /* skip leading '/' */
1530 struct got_tree_object *next_tree;
1539 /* end of path segment */
1542 te = find_entry_by_name(tree, seg);
1544 err = got_error(GOT_ERR_NO_OBJ);
1555 err = got_object_open_as_tree(&next_tree, repo,
1560 got_object_tree_close(tree);
1566 err = got_object_open(obj, repo, te->id);
1570 err = got_error(GOT_ERR_NO_OBJ);
1574 got_object_commit_close(commit);
1576 got_object_tree_close(tree);