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, 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 = calloc(zbsize, sizeof(char));
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 = 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;
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_commit_add_parent(struct got_commit_object *commit,
480 const struct got_error *err = NULL;
481 struct got_object_qid *qid;
483 qid = calloc(1, sizeof(*qid));
485 return got_error_from_errno();
487 qid->id = calloc(1, sizeof(*qid->id));
488 if (qid->id == NULL) {
489 err = got_error_from_errno();
494 if (!got_parse_sha1_digest(qid->id->sha1, id_str)) {
495 err = got_error(GOT_ERR_BAD_OBJ_DATA);
501 SIMPLEQ_INSERT_TAIL(&commit->parent_ids, qid, entry);
507 static const struct got_error *
508 parse_gmtoff(time_t *gmtoff, const char *tzstr)
511 const char *p = tzstr;
519 return got_error(GOT_ERR_BAD_OBJ_DATA);
521 if (!isdigit(*p) && !isdigit(*(p + 1)))
522 return got_error(GOT_ERR_BAD_OBJ_DATA);
523 h = (((*p - '0') * 10) + (*(p + 1) - '0'));
526 if (!isdigit(*p) && !isdigit(*(p + 1)))
527 return got_error(GOT_ERR_BAD_OBJ_DATA);
528 m = ((*p - '0') * 10) + (*(p + 1) - '0');
530 *gmtoff = (h * 60 * 60 + m * 60) * sign;
534 static const struct got_error *
535 parse_commit_time(struct tm *tm, char *committer)
537 const struct got_error *err = NULL;
543 /* Parse and strip off trailing timezone indicator string. */
544 space = strrchr(committer, ' ');
546 return got_error(GOT_ERR_BAD_OBJ_DATA);
547 tzstr = strdup(space + 1);
549 return got_error_from_errno();
550 err = parse_gmtoff(&gmtoff, tzstr);
556 /* Timestamp is separated from committer name + email by space. */
557 space = strrchr(committer, ' ');
559 return got_error(GOT_ERR_BAD_OBJ_DATA);
561 /* Timestamp parsed here is expressed in comitter's local time. */
562 time = strtonum(space + 1, 0, INT64_MAX, &errstr);
564 return got_error(GOT_ERR_BAD_OBJ_DATA);
566 /* Express the time stamp in UTC. */
567 memset(tm, 0, sizeof(*tm));
569 if (localtime_r(&time, tm) == NULL)
570 return got_error_from_errno();
571 tm->tm_gmtoff = gmtoff;
573 /* Strip off parsed time information, leaving just author and email. */
579 static const struct got_error *
580 parse_commit_object(struct got_commit_object **commit, char *buf, size_t len)
582 const struct got_error *err = NULL;
585 ssize_t remain = (ssize_t)len;
587 *commit = got_object_commit_alloc_partial();
589 return got_error_from_errno();
591 tlen = strlen(GOT_COMMIT_TAG_TREE);
592 if (strncmp(s, GOT_COMMIT_TAG_TREE, tlen) == 0) {
594 if (remain < SHA1_DIGEST_STRING_LENGTH) {
595 err = got_error(GOT_ERR_BAD_OBJ_DATA);
599 if (!got_parse_sha1_digest((*commit)->tree_id->sha1, s)) {
600 err = got_error(GOT_ERR_BAD_OBJ_DATA);
603 remain -= SHA1_DIGEST_STRING_LENGTH;
604 s += SHA1_DIGEST_STRING_LENGTH;
606 err = got_error(GOT_ERR_BAD_OBJ_DATA);
610 tlen = strlen(GOT_COMMIT_TAG_PARENT);
611 while (strncmp(s, GOT_COMMIT_TAG_PARENT, tlen) == 0) {
613 if (remain < SHA1_DIGEST_STRING_LENGTH) {
614 err = got_error(GOT_ERR_BAD_OBJ_DATA);
618 err = got_object_commit_add_parent(*commit, s);
622 remain -= SHA1_DIGEST_STRING_LENGTH;
623 s += SHA1_DIGEST_STRING_LENGTH;
626 tlen = strlen(GOT_COMMIT_TAG_AUTHOR);
627 if (strncmp(s, GOT_COMMIT_TAG_AUTHOR, tlen) == 0) {
633 err = got_error(GOT_ERR_BAD_OBJ_DATA);
639 err = got_error(GOT_ERR_BAD_OBJ_DATA);
644 err = parse_commit_time(&(*commit)->tm_author, s);
647 (*commit)->author = strdup(s);
648 if ((*commit)->author == NULL) {
649 err = got_error_from_errno();
656 tlen = strlen(GOT_COMMIT_TAG_COMMITTER);
657 if (strncmp(s, GOT_COMMIT_TAG_COMMITTER, tlen) == 0) {
663 err = got_error(GOT_ERR_BAD_OBJ_DATA);
669 err = got_error(GOT_ERR_BAD_OBJ_DATA);
674 err = parse_commit_time(&(*commit)->tm_committer, s);
677 (*commit)->committer = strdup(s);
678 if ((*commit)->committer == NULL) {
679 err = got_error_from_errno();
686 (*commit)->logmsg = strndup(s, remain);
687 if ((*commit)->logmsg == NULL) {
688 err = got_error_from_errno();
693 got_object_commit_close(*commit);
700 tree_entry_close(struct got_tree_entry *te)
707 struct got_tree_entry *
708 got_alloc_tree_entry_partial(void)
710 struct got_tree_entry *te;
712 te = calloc(1, sizeof(*te));
716 te->id = calloc(1, sizeof(*te->id));
717 if (te->id == NULL) {
724 static const struct got_error *
725 parse_tree_entry(struct got_tree_entry **te, size_t *elen, char *buf,
728 char *p = buf, *space;
729 const struct got_error *err = NULL;
731 *te = got_alloc_tree_entry_partial();
733 return got_error_from_errno();
735 *elen = strlen(buf) + 1;
736 if (*elen > maxlen) {
739 return got_error(GOT_ERR_BAD_OBJ_DATA);
742 space = strchr(buf, ' ');
744 err = got_error(GOT_ERR_BAD_OBJ_DATA);
750 if (*p < '0' && *p > '7') {
751 err = got_error(GOT_ERR_BAD_OBJ_DATA);
755 (*te)->mode |= *p - '0';
759 (*te)->name = strdup(space + 1);
760 if (*elen > maxlen || maxlen - *elen < SHA1_DIGEST_LENGTH) {
761 err = got_error(GOT_ERR_BAD_OBJ_DATA);
764 buf += strlen(buf) + 1;
765 memcpy((*te)->id->sha1, buf, SHA1_DIGEST_LENGTH);
766 *elen += SHA1_DIGEST_LENGTH;
769 tree_entry_close(*te);
775 static const struct got_error *
776 parse_tree_object(struct got_tree_object **tree, uint8_t *buf, size_t len)
778 const struct got_error *err;
781 *tree = calloc(1, sizeof(**tree));
783 return got_error_from_errno();
785 SIMPLEQ_INIT(&(*tree)->entries.head);
788 struct got_tree_entry *te;
791 err = parse_tree_entry(&te, &elen, buf, remain);
794 (*tree)->entries.nentries++;
795 SIMPLEQ_INSERT_TAIL(&(*tree)->entries.head, te, entry);
801 got_object_tree_close(*tree);
802 return got_error(GOT_ERR_BAD_OBJ_DATA);
808 static const struct got_error *
809 read_to_mem(uint8_t **outbuf, size_t *outlen, FILE *f)
811 const struct got_error *err = NULL;
812 static const size_t blocksize = 512;
813 size_t n, total, remain;
819 buf = calloc(1, blocksize);
821 return got_error_from_errno();
828 newbuf = reallocarray(buf, 1, total + blocksize);
829 if (newbuf == NULL) {
830 err = got_error_from_errno();
836 n = fread(buf + total, 1, remain, f);
839 err = got_ferror(f, GOT_ERR_IO);
857 static const struct got_error *
858 read_commit_object(struct got_commit_object **commit, struct got_object *obj,
861 const struct got_error *err = NULL;
865 if (obj->flags & GOT_OBJ_FLAG_PACKED)
866 err = read_to_mem(&p, &len, f);
868 err = got_inflate_to_mem(&p, &len, f);
872 if (len < obj->hdrlen + obj->size) {
873 err = got_error(GOT_ERR_BAD_OBJ_DATA);
877 /* Skip object header. */
879 err = parse_commit_object(commit, p + obj->hdrlen, len);
886 read_commit_object_privsep_child(struct got_object *obj, int obj_fd,
889 const struct got_error *err = NULL;
890 struct got_commit_object *commit = NULL;
895 setproctitle("read commit object");
897 imsg_init(&ibuf, imsg_fds[1]);
899 /* revoke access to most system calls */
900 if (pledge("stdio", NULL) == -1) {
901 err = got_error_from_errno();
905 f = fdopen(obj_fd, "rb");
907 err = got_error_from_errno();
912 err = read_commit_object(&commit, obj, f);
916 err = got_privsep_send_commit(&ibuf, commit);
919 got_object_commit_close(commit);
921 got_privsep_send_error(&ibuf, err);
931 static const struct got_error *
932 read_commit_object_privsep(struct got_commit_object **commit,
933 struct got_repository *repo, struct got_object *obj, int fd)
935 const struct got_error *err = NULL, *err_child = NULL;
936 struct imsgbuf parent_ibuf;
940 if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_fds) == -1)
941 return got_error_from_errno();
945 return got_error_from_errno();
947 read_commit_object_privsep_child(obj, fd, imsg_fds);
952 imsg_init(&parent_ibuf, imsg_fds[0]);
953 err = got_privsep_recv_commit(commit, &parent_ibuf);
954 imsg_clear(&parent_ibuf);
955 err_child = wait_for_child(pid);
957 return err ? err : err_child;
960 const struct got_error *
961 got_object_commit_open(struct got_commit_object **commit,
962 struct got_repository *repo, struct got_object *obj)
964 const struct got_error *err = NULL;
966 *commit = got_repo_get_cached_commit(repo, &obj->id);
967 if (*commit != NULL) {
972 if (obj->type != GOT_OBJ_TYPE_COMMIT)
973 return got_error(GOT_ERR_OBJ_TYPE);
975 if (obj->flags & GOT_OBJ_FLAG_PACKED) {
978 err = got_packfile_extract_object_to_mem(&buf, &len, obj, repo);
982 err = parse_commit_object(commit, buf, len);
986 err = open_loose_object(&fd, obj, repo);
989 err = read_commit_object_privsep(commit, repo, obj, fd);
995 err = got_repo_cache_commit(repo, &obj->id, *commit);
1002 got_object_commit_close(struct got_commit_object *commit)
1004 struct got_object_qid *qid;
1006 if (commit->refcnt > 0) {
1008 if (commit->refcnt > 0)
1012 while (!SIMPLEQ_EMPTY(&commit->parent_ids)) {
1013 qid = SIMPLEQ_FIRST(&commit->parent_ids);
1014 SIMPLEQ_REMOVE_HEAD(&commit->parent_ids, entry);
1019 free(commit->tree_id);
1020 free(commit->author);
1021 free(commit->committer);
1022 free(commit->logmsg);
1026 static const struct got_error *
1027 read_tree_object(struct got_tree_object **tree, struct got_object *obj, FILE *f)
1029 const struct got_error *err = NULL;
1033 if (obj->flags & GOT_OBJ_FLAG_PACKED)
1034 err = read_to_mem(&p, &len, f);
1036 err = got_inflate_to_mem(&p, &len, f);
1040 if (len < obj->hdrlen + obj->size) {
1041 err = got_error(GOT_ERR_BAD_OBJ_DATA);
1045 /* Skip object header. */
1047 err = parse_tree_object(tree, p + obj->hdrlen, len);
1054 read_tree_object_privsep_child(struct got_object *obj, int obj_fd,
1057 const struct got_error *err = NULL;
1058 struct got_tree_object *tree = NULL;
1059 struct imsgbuf ibuf;
1063 setproctitle("read tree object");
1065 imsg_init(&ibuf, imsg_fds[1]);
1067 /* revoke access to most system calls */
1068 if (pledge("stdio", NULL) == -1) {
1069 err = got_error_from_errno();
1073 f = fdopen(obj_fd, "rb");
1075 err = got_error_from_errno();
1080 err = read_tree_object(&tree, obj, f);
1084 err = got_privsep_send_tree(&ibuf, tree);
1087 got_object_tree_close(tree);
1089 got_privsep_send_error(&ibuf, err);
1099 static const struct got_error *
1100 read_tree_object_privsep(struct got_tree_object **tree, struct got_object *obj,
1103 const struct got_error *err = NULL, *err_child = NULL;
1104 struct imsgbuf parent_ibuf;
1108 if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_fds) == -1)
1109 return got_error_from_errno();
1113 return got_error_from_errno();
1114 else if (pid == 0) {
1115 read_tree_object_privsep_child(obj, fd, imsg_fds);
1120 imsg_init(&parent_ibuf, imsg_fds[0]);
1121 err = got_privsep_recv_tree(tree, &parent_ibuf);
1122 imsg_clear(&parent_ibuf);
1123 err_child = wait_for_child(pid);
1125 return err ? err : err_child;
1128 const struct got_error *
1129 got_object_tree_open(struct got_tree_object **tree,
1130 struct got_repository *repo, struct got_object *obj)
1132 const struct got_error *err = NULL;
1134 *tree = got_repo_get_cached_tree(repo, &obj->id);
1135 if (*tree != NULL) {
1140 if (obj->type != GOT_OBJ_TYPE_TREE)
1141 return got_error(GOT_ERR_OBJ_TYPE);
1143 if (obj->flags & GOT_OBJ_FLAG_PACKED) {
1146 err = got_packfile_extract_object_to_mem(&buf, &len, obj, repo);
1150 err = parse_tree_object(tree, buf, len);
1154 err = open_loose_object(&fd, obj, repo);
1157 err = read_tree_object_privsep(tree, obj, fd);
1163 err = got_repo_cache_tree(repo, &obj->id, *tree);
1169 const struct got_error *
1170 got_object_open_as_tree(struct got_tree_object **tree,
1171 struct got_repository *repo, struct got_object_id *id)
1173 const struct got_error *err;
1174 struct got_object *obj;
1178 err = got_object_open(&obj, repo, id);
1181 if (got_object_get_type(obj) != GOT_OBJ_TYPE_TREE) {
1182 err = got_error(GOT_ERR_OBJ_TYPE);
1186 err = got_object_tree_open(tree, repo, obj);
1188 got_object_close(obj);
1193 got_object_tree_close(struct got_tree_object *tree)
1195 struct got_tree_entry *te;
1197 if (tree->refcnt > 0) {
1199 if (tree->refcnt > 0)
1203 while (!SIMPLEQ_EMPTY(&tree->entries.head)) {
1204 te = SIMPLEQ_FIRST(&tree->entries.head);
1205 SIMPLEQ_REMOVE_HEAD(&tree->entries.head, entry);
1206 tree_entry_close(te);
1212 const struct got_tree_entries *
1213 got_object_tree_get_entries(struct got_tree_object *tree)
1215 return &tree->entries;
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;
1225 FILE *infile = NULL;
1227 setproctitle("read blob object");
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();
1237 infile = fdopen(infd, "rb");
1238 if (infile == NULL) {
1239 err = got_error_from_errno();
1243 err = got_inflate_to_fd(&size, infile, outfd);
1248 err = got_privsep_send_blob(&ibuf, size);
1251 got_privsep_send_error(&ibuf, err);
1260 static const struct got_error *
1261 read_blob_object_privsep(size_t *size, int outfd, int infd)
1263 struct imsgbuf parent_ibuf;
1265 const struct got_error *err = NULL, *err_child = NULL;
1268 if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_fds) == -1)
1269 return got_error_from_errno();
1273 return got_error_from_errno();
1274 else if (pid == 0) {
1275 read_blob_object_privsep_child(outfd, infd, imsg_fds);
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);
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));
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();
1311 if (obj->flags & GOT_OBJ_FLAG_PACKED) {
1312 err = got_packfile_extract_object(&((*blob)->f), obj, repo);
1320 err = open_loose_object(&infd, obj, repo);
1325 outfd = got_opentempfd();
1327 err = got_error_from_errno();
1332 err = read_blob_object_privsep(&size, outfd, infd);
1337 if (size != obj->hdrlen + obj->size) {
1338 err = got_error(GOT_ERR_PRIVSEP_LEN);
1343 if (fstat(outfd, &sb) == -1) {
1344 err = got_error_from_errno();
1349 if (sb.st_size != size) {
1350 err = got_error(GOT_ERR_PRIVSEP_LEN);
1355 (*blob)->f = fdopen(outfd, "rb");
1356 if ((*blob)->f == NULL) {
1357 err = got_error_from_errno();
1363 (*blob)->hdrlen = obj->hdrlen;
1364 (*blob)->blocksize = blocksize;
1365 memcpy(&(*blob)->id.sha1, obj->id.sha1, SHA1_DIGEST_LENGTH);
1371 free((*blob)->read_buf);
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,
1383 const struct got_error *err;
1384 struct got_object *obj;
1388 err = got_object_open(&obj, repo, id);
1391 if (got_object_get_type(obj) != GOT_OBJ_TYPE_BLOB) {
1392 err = got_error(GOT_ERR_OBJ_TYPE);
1396 err = got_object_blob_open(blob, repo, obj, blocksize);
1398 got_object_close(obj);
1403 got_object_blob_close(struct got_blob_object *blob)
1405 free(blob->read_buf);
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);
1417 got_object_blob_get_hdrlen(struct got_blob_object *blob)
1419 return blob->hdrlen;
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)
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);
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;
1448 hdrlen = got_object_blob_get_hdrlen(blob);
1450 err = got_object_blob_read_block(&len, blob);
1456 /* Skip blob object header first time around. */
1457 fwrite(got_object_blob_get_read_buf(blob) + hdrlen,
1458 len - hdrlen, 1, outfile);
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.head, entry) {
1474 if (strcmp(te->name, name) == 0)
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);
1493 /* We are expecting an absolute in-repository path. */
1495 return got_error(GOT_ERR_NOT_ABSPATH);
1497 err = got_object_open_as_commit(&commit, repo, commit_id);
1501 /* Handle opening of root of commit's tree. */
1502 if (path[1] == '\0') {
1503 err = got_object_open(obj, repo, commit->tree_id);
1509 err = got_object_open_as_tree(&tree, repo, commit->tree_id);
1515 err = got_error_from_errno();
1518 err = got_canonpath(path, s0, len + 1);
1523 s++; /* skip leading '/' */
1527 struct got_tree_object *next_tree;
1536 /* end of path segment */
1539 te = find_entry_by_name(tree, seg);
1541 err = got_error(GOT_ERR_NO_OBJ);
1552 err = got_object_open_as_tree(&next_tree, repo,
1557 got_object_tree_close(tree);
1563 err = got_object_open(obj, repo, te->id);
1567 err = got_error(GOT_ERR_NO_OBJ);
1571 got_object_commit_close(commit);
1573 got_object_tree_close(tree);