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) {
429 if (obj->flags & GOT_OBJ_FLAG_DELTIFIED) {
430 struct got_delta *delta;
431 while (!SIMPLEQ_EMPTY(&obj->deltas.entries)) {
432 delta = SIMPLEQ_FIRST(&obj->deltas.entries);
433 SIMPLEQ_REMOVE_HEAD(&obj->deltas.entries, entry);
434 got_delta_close(delta);
437 if (obj->flags & GOT_OBJ_FLAG_PACKED)
438 free(obj->path_packfile);
442 struct got_commit_object *
443 got_object_commit_alloc_partial(void)
445 struct got_commit_object *commit;
447 commit = calloc(1, sizeof(*commit));
450 commit->tree_id = calloc(1, sizeof(*commit->tree_id));
451 if (commit->tree_id == NULL) {
456 SIMPLEQ_INIT(&commit->parent_ids);
461 const struct got_error *
462 got_object_open_as_commit(struct got_commit_object **commit,
463 struct got_repository *repo, struct got_object_id *id)
465 const struct got_error *err;
466 struct got_object *obj;
470 err = got_object_open(&obj, repo, id);
473 if (got_object_get_type(obj) != GOT_OBJ_TYPE_COMMIT) {
474 err = got_error(GOT_ERR_OBJ_TYPE);
478 err = got_object_commit_open(commit, repo, obj);
480 got_object_close(obj);
484 const struct got_error *
485 got_object_commit_add_parent(struct got_commit_object *commit,
488 const struct got_error *err = NULL;
489 struct got_object_qid *qid;
491 qid = calloc(1, sizeof(*qid));
493 return got_error_from_errno();
495 qid->id = calloc(1, sizeof(*qid->id));
496 if (qid->id == NULL) {
497 err = got_error_from_errno();
502 if (!got_parse_sha1_digest(qid->id->sha1, id_str)) {
503 err = got_error(GOT_ERR_BAD_OBJ_DATA);
509 SIMPLEQ_INSERT_TAIL(&commit->parent_ids, qid, entry);
515 static const struct got_error *
516 parse_gmtoff(time_t *gmtoff, const char *tzstr)
519 const char *p = tzstr;
527 return got_error(GOT_ERR_BAD_OBJ_DATA);
529 if (!isdigit(*p) && !isdigit(*(p + 1)))
530 return got_error(GOT_ERR_BAD_OBJ_DATA);
531 h = (((*p - '0') * 10) + (*(p + 1) - '0'));
534 if (!isdigit(*p) && !isdigit(*(p + 1)))
535 return got_error(GOT_ERR_BAD_OBJ_DATA);
536 m = ((*p - '0') * 10) + (*(p + 1) - '0');
538 *gmtoff = (h * 60 * 60 + m * 60) * sign;
542 static const struct got_error *
543 parse_commit_time(struct tm *tm, char *committer)
545 const struct got_error *err = NULL;
551 /* Parse and strip off trailing timezone indicator string. */
552 space = strrchr(committer, ' ');
554 return got_error(GOT_ERR_BAD_OBJ_DATA);
555 tzstr = strdup(space + 1);
557 return got_error_from_errno();
558 err = parse_gmtoff(&gmtoff, tzstr);
564 /* Timestamp is separated from committer name + email by space. */
565 space = strrchr(committer, ' ');
567 return got_error(GOT_ERR_BAD_OBJ_DATA);
569 /* Timestamp parsed here is expressed in comitter's local time. */
570 time = strtonum(space + 1, 0, INT64_MAX, &errstr);
572 return got_error(GOT_ERR_BAD_OBJ_DATA);
574 /* Express the time stamp in UTC. */
575 memset(tm, 0, sizeof(*tm));
577 if (localtime_r(&time, tm) == NULL)
578 return got_error_from_errno();
579 tm->tm_gmtoff = gmtoff;
581 /* Strip off parsed time information, leaving just author and email. */
587 static const struct got_error *
588 parse_commit_object(struct got_commit_object **commit, char *buf, size_t len)
590 const struct got_error *err = NULL;
593 ssize_t remain = (ssize_t)len;
595 *commit = got_object_commit_alloc_partial();
597 return got_error_from_errno();
599 tlen = strlen(GOT_COMMIT_TAG_TREE);
600 if (strncmp(s, GOT_COMMIT_TAG_TREE, tlen) == 0) {
602 if (remain < SHA1_DIGEST_STRING_LENGTH) {
603 err = got_error(GOT_ERR_BAD_OBJ_DATA);
607 if (!got_parse_sha1_digest((*commit)->tree_id->sha1, s)) {
608 err = got_error(GOT_ERR_BAD_OBJ_DATA);
611 remain -= SHA1_DIGEST_STRING_LENGTH;
612 s += SHA1_DIGEST_STRING_LENGTH;
614 err = got_error(GOT_ERR_BAD_OBJ_DATA);
618 tlen = strlen(GOT_COMMIT_TAG_PARENT);
619 while (strncmp(s, GOT_COMMIT_TAG_PARENT, tlen) == 0) {
621 if (remain < SHA1_DIGEST_STRING_LENGTH) {
622 err = got_error(GOT_ERR_BAD_OBJ_DATA);
626 err = got_object_commit_add_parent(*commit, s);
630 remain -= SHA1_DIGEST_STRING_LENGTH;
631 s += SHA1_DIGEST_STRING_LENGTH;
634 tlen = strlen(GOT_COMMIT_TAG_AUTHOR);
635 if (strncmp(s, GOT_COMMIT_TAG_AUTHOR, tlen) == 0) {
641 err = got_error(GOT_ERR_BAD_OBJ_DATA);
647 err = got_error(GOT_ERR_BAD_OBJ_DATA);
652 err = parse_commit_time(&(*commit)->tm_author, s);
655 (*commit)->author = strdup(s);
656 if ((*commit)->author == NULL) {
657 err = got_error_from_errno();
664 tlen = strlen(GOT_COMMIT_TAG_COMMITTER);
665 if (strncmp(s, GOT_COMMIT_TAG_COMMITTER, tlen) == 0) {
671 err = got_error(GOT_ERR_BAD_OBJ_DATA);
677 err = got_error(GOT_ERR_BAD_OBJ_DATA);
682 err = parse_commit_time(&(*commit)->tm_committer, s);
685 (*commit)->committer = strdup(s);
686 if ((*commit)->committer == NULL) {
687 err = got_error_from_errno();
694 (*commit)->logmsg = strndup(s, remain);
695 if ((*commit)->logmsg == NULL) {
696 err = got_error_from_errno();
701 got_object_commit_close(*commit);
708 tree_entry_close(struct got_tree_entry *te)
715 struct got_tree_entry *
716 got_alloc_tree_entry_partial(void)
718 struct got_tree_entry *te;
720 te = calloc(1, sizeof(*te));
724 te->id = calloc(1, sizeof(*te->id));
725 if (te->id == NULL) {
732 static const struct got_error *
733 parse_tree_entry(struct got_tree_entry **te, size_t *elen, char *buf,
736 char *p = buf, *space;
737 const struct got_error *err = NULL;
739 *te = got_alloc_tree_entry_partial();
741 return got_error_from_errno();
743 *elen = strlen(buf) + 1;
744 if (*elen > maxlen) {
747 return got_error(GOT_ERR_BAD_OBJ_DATA);
750 space = strchr(buf, ' ');
752 err = got_error(GOT_ERR_BAD_OBJ_DATA);
758 if (*p < '0' && *p > '7') {
759 err = got_error(GOT_ERR_BAD_OBJ_DATA);
763 (*te)->mode |= *p - '0';
767 (*te)->name = strdup(space + 1);
768 if (*elen > maxlen || maxlen - *elen < SHA1_DIGEST_LENGTH) {
769 err = got_error(GOT_ERR_BAD_OBJ_DATA);
772 buf += strlen(buf) + 1;
773 memcpy((*te)->id->sha1, buf, SHA1_DIGEST_LENGTH);
774 *elen += SHA1_DIGEST_LENGTH;
777 tree_entry_close(*te);
783 static const struct got_error *
784 parse_tree_object(struct got_tree_object **tree, uint8_t *buf, size_t len)
786 const struct got_error *err;
789 *tree = calloc(1, sizeof(**tree));
791 return got_error_from_errno();
793 SIMPLEQ_INIT(&(*tree)->entries);
796 struct got_tree_entry *te;
799 err = parse_tree_entry(&te, &elen, buf, remain);
803 SIMPLEQ_INSERT_TAIL(&(*tree)->entries, te, entry);
809 got_object_tree_close(*tree);
810 return got_error(GOT_ERR_BAD_OBJ_DATA);
816 static const struct got_error *
817 read_to_mem(uint8_t **outbuf, size_t *outlen, FILE *f)
819 const struct got_error *err = NULL;
820 static const size_t blocksize = 512;
821 size_t n, total, remain;
827 buf = calloc(1, blocksize);
829 return got_error_from_errno();
836 newbuf = reallocarray(buf, 1, total + blocksize);
837 if (newbuf == NULL) {
838 err = got_error_from_errno();
844 n = fread(buf + total, 1, remain, f);
847 err = got_ferror(f, GOT_ERR_IO);
865 static const struct got_error *
866 read_commit_object(struct got_commit_object **commit, struct got_object *obj,
869 const struct got_error *err = NULL;
873 if (obj->flags & GOT_OBJ_FLAG_PACKED)
874 err = read_to_mem(&p, &len, f);
876 err = got_inflate_to_mem(&p, &len, f);
880 if (len < obj->hdrlen + obj->size) {
881 err = got_error(GOT_ERR_BAD_OBJ_DATA);
885 /* Skip object header. */
887 err = parse_commit_object(commit, p + obj->hdrlen, len);
894 read_commit_object_privsep_child(struct got_object *obj, int obj_fd,
897 const struct got_error *err = NULL;
898 struct got_commit_object *commit = NULL;
903 setproctitle("read commit object");
905 imsg_init(&ibuf, imsg_fds[1]);
907 /* revoke access to most system calls */
908 if (pledge("stdio", NULL) == -1) {
909 err = got_error_from_errno();
913 f = fdopen(obj_fd, "rb");
915 err = got_error_from_errno();
920 err = read_commit_object(&commit, obj, f);
924 err = got_privsep_send_commit(&ibuf, commit);
927 got_object_commit_close(commit);
929 got_privsep_send_error(&ibuf, err);
939 static const struct got_error *
940 read_commit_object_privsep(struct got_commit_object **commit,
941 struct got_repository *repo, struct got_object *obj, int fd)
943 const struct got_error *err = NULL, *err_child = NULL;
944 struct imsgbuf parent_ibuf;
948 if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_fds) == -1)
949 return got_error_from_errno();
953 return got_error_from_errno();
955 read_commit_object_privsep_child(obj, fd, imsg_fds);
960 imsg_init(&parent_ibuf, imsg_fds[0]);
961 err = got_privsep_recv_commit(commit, &parent_ibuf);
962 imsg_clear(&parent_ibuf);
963 err_child = wait_for_child(pid);
965 return err ? err : err_child;
968 const struct got_error *
969 got_object_commit_open(struct got_commit_object **commit,
970 struct got_repository *repo, struct got_object *obj)
972 const struct got_error *err = NULL;
974 if (obj->type != GOT_OBJ_TYPE_COMMIT)
975 return got_error(GOT_ERR_OBJ_TYPE);
977 if (obj->flags & GOT_OBJ_FLAG_PACKED) {
980 err = got_packfile_extract_object_to_mem(&buf, &len, obj, repo);
984 err = parse_commit_object(commit, buf, len);
988 err = open_loose_object(&fd, obj, repo);
991 err = read_commit_object_privsep(commit, repo, obj, fd);
998 got_object_commit_close(struct got_commit_object *commit)
1000 struct got_object_qid *qid;
1002 while (!SIMPLEQ_EMPTY(&commit->parent_ids)) {
1003 qid = SIMPLEQ_FIRST(&commit->parent_ids);
1004 SIMPLEQ_REMOVE_HEAD(&commit->parent_ids, entry);
1009 free(commit->tree_id);
1010 free(commit->author);
1011 free(commit->committer);
1012 free(commit->logmsg);
1016 static const struct got_error *
1017 read_tree_object(struct got_tree_object **tree, struct got_object *obj, FILE *f)
1019 const struct got_error *err = NULL;
1023 if (obj->flags & GOT_OBJ_FLAG_PACKED)
1024 err = read_to_mem(&p, &len, f);
1026 err = got_inflate_to_mem(&p, &len, f);
1030 if (len < obj->hdrlen + obj->size) {
1031 err = got_error(GOT_ERR_BAD_OBJ_DATA);
1035 /* Skip object header. */
1037 err = parse_tree_object(tree, p + obj->hdrlen, len);
1044 read_tree_object_privsep_child(struct got_object *obj, int obj_fd,
1047 const struct got_error *err = NULL;
1048 struct got_tree_object *tree = NULL;
1049 struct imsgbuf ibuf;
1053 setproctitle("read tree object");
1055 imsg_init(&ibuf, imsg_fds[1]);
1057 /* revoke access to most system calls */
1058 if (pledge("stdio", NULL) == -1) {
1059 err = got_error_from_errno();
1063 f = fdopen(obj_fd, "rb");
1065 err = got_error_from_errno();
1070 err = read_tree_object(&tree, obj, f);
1074 err = got_privsep_send_tree(&ibuf, tree);
1077 got_object_tree_close(tree);
1079 got_privsep_send_error(&ibuf, err);
1089 static const struct got_error *
1090 read_tree_object_privsep(struct got_tree_object **tree, struct got_object *obj,
1093 const struct got_error *err = NULL, *err_child = NULL;
1094 struct imsgbuf parent_ibuf;
1098 if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_fds) == -1)
1099 return got_error_from_errno();
1103 return got_error_from_errno();
1104 else if (pid == 0) {
1105 read_tree_object_privsep_child(obj, fd, imsg_fds);
1110 imsg_init(&parent_ibuf, imsg_fds[0]);
1111 err = got_privsep_recv_tree(tree, &parent_ibuf);
1112 imsg_clear(&parent_ibuf);
1113 err_child = wait_for_child(pid);
1115 return err ? err : err_child;
1118 const struct got_error *
1119 got_object_tree_open(struct got_tree_object **tree,
1120 struct got_repository *repo, struct got_object *obj)
1122 const struct got_error *err = NULL;
1124 if (obj->type != GOT_OBJ_TYPE_TREE)
1125 return got_error(GOT_ERR_OBJ_TYPE);
1127 if (obj->flags & GOT_OBJ_FLAG_PACKED) {
1130 err = got_packfile_extract_object_to_mem(&buf, &len, obj, repo);
1134 err = parse_tree_object(tree, buf, len);
1138 err = open_loose_object(&fd, obj, repo);
1141 err = read_tree_object_privsep(tree, obj, fd);
1147 const struct got_error *
1148 got_object_open_as_tree(struct got_tree_object **tree,
1149 struct got_repository *repo, struct got_object_id *id)
1151 const struct got_error *err;
1152 struct got_object *obj;
1156 err = got_object_open(&obj, repo, id);
1159 if (got_object_get_type(obj) != GOT_OBJ_TYPE_TREE) {
1160 err = got_error(GOT_ERR_OBJ_TYPE);
1164 err = got_object_tree_open(tree, repo, obj);
1166 got_object_close(obj);
1171 got_object_tree_close(struct got_tree_object *tree)
1173 struct got_tree_entry *te;
1175 while (!SIMPLEQ_EMPTY(&tree->entries)) {
1176 te = SIMPLEQ_FIRST(&tree->entries);
1177 SIMPLEQ_REMOVE_HEAD(&tree->entries, entry);
1178 tree_entry_close(te);
1184 static const struct got_error *
1185 read_blob_object_privsep_child(int outfd, int infd, int imsg_fds[2])
1187 const struct got_error *err = NULL;
1188 struct imsgbuf ibuf;
1191 FILE *infile = NULL;
1193 setproctitle("read blob object");
1195 imsg_init(&ibuf, imsg_fds[1]);
1197 /* revoke access to most system calls */
1198 if (pledge("stdio", NULL) == -1) {
1199 err = got_error_from_errno();
1203 infile = fdopen(infd, "rb");
1204 if (infile == NULL) {
1205 err = got_error_from_errno();
1209 err = got_inflate_to_fd(&size, infile, outfd);
1214 err = got_privsep_send_blob(&ibuf, size);
1217 got_privsep_send_error(&ibuf, err);
1226 static const struct got_error *
1227 read_blob_object_privsep(size_t *size, int outfd, int infd)
1229 struct imsgbuf parent_ibuf;
1231 const struct got_error *err = NULL, *err_child = NULL;
1234 if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_fds) == -1)
1235 return got_error_from_errno();
1239 return got_error_from_errno();
1240 else if (pid == 0) {
1241 read_blob_object_privsep_child(outfd, infd, imsg_fds);
1246 imsg_init(&parent_ibuf, imsg_fds[0]);
1247 err = got_privsep_recv_blob(size, &parent_ibuf);
1248 imsg_clear(&parent_ibuf);
1249 err_child = wait_for_child(pid);
1251 if (lseek(outfd, SEEK_SET, 0) == -1)
1252 err = got_error_from_errno();
1253 return err ? err : err_child;
1256 const struct got_error *
1257 got_object_blob_open(struct got_blob_object **blob,
1258 struct got_repository *repo, struct got_object *obj, size_t blocksize)
1260 const struct got_error *err = NULL;
1262 if (obj->type != GOT_OBJ_TYPE_BLOB)
1263 return got_error(GOT_ERR_OBJ_TYPE);
1265 if (blocksize < obj->hdrlen)
1266 return got_error(GOT_ERR_NO_SPACE);
1268 *blob = calloc(1, sizeof(**blob));
1270 return got_error_from_errno();
1272 (*blob)->read_buf = calloc(1, blocksize);
1273 if ((*blob)->read_buf == NULL) {
1274 err = got_error_from_errno();
1277 if (obj->flags & GOT_OBJ_FLAG_PACKED) {
1278 err = got_packfile_extract_object(&((*blob)->f), obj, repo);
1286 err = open_loose_object(&infd, obj, repo);
1291 outfd = got_opentempfd();
1293 err = got_error_from_errno();
1298 err = read_blob_object_privsep(&size, outfd, infd);
1303 if (size != obj->hdrlen + obj->size) {
1304 err = got_error(GOT_ERR_PRIVSEP_LEN);
1309 if (fstat(outfd, &sb) == -1) {
1310 err = got_error_from_errno();
1315 if (sb.st_size != size) {
1316 err = got_error(GOT_ERR_PRIVSEP_LEN);
1321 (*blob)->f = fdopen(outfd, "rb");
1322 if ((*blob)->f == NULL) {
1323 err = got_error_from_errno();
1329 (*blob)->hdrlen = obj->hdrlen;
1330 (*blob)->blocksize = blocksize;
1331 memcpy(&(*blob)->id.sha1, obj->id.sha1, SHA1_DIGEST_LENGTH);
1337 free((*blob)->read_buf);
1344 const struct got_error *
1345 got_object_open_as_blob(struct got_blob_object **blob,
1346 struct got_repository *repo, struct got_object_id *id,
1349 const struct got_error *err;
1350 struct got_object *obj;
1354 err = got_object_open(&obj, repo, id);
1357 if (got_object_get_type(obj) != GOT_OBJ_TYPE_BLOB) {
1358 err = got_error(GOT_ERR_OBJ_TYPE);
1362 err = got_object_blob_open(blob, repo, obj, blocksize);
1364 got_object_close(obj);
1369 got_object_blob_close(struct got_blob_object *blob)
1371 free(blob->read_buf);
1377 got_object_blob_id_str(struct got_blob_object *blob, char *buf, size_t size)
1379 return got_sha1_digest_to_str(blob->id.sha1, buf, size);
1383 got_object_blob_get_hdrlen(struct got_blob_object *blob)
1385 return blob->hdrlen;
1389 got_object_blob_get_read_buf(struct got_blob_object *blob)
1391 return blob->read_buf;
1394 const struct got_error *
1395 got_object_blob_read_block(size_t *outlenp, struct got_blob_object *blob)
1399 n = fread(blob->read_buf, 1, blob->blocksize, blob->f);
1400 if (n == 0 && ferror(blob->f))
1401 return got_ferror(blob->f, GOT_ERR_IO);
1406 const struct got_error *
1407 got_object_blob_dump_to_file(size_t *total_len, FILE *outfile,
1408 struct got_blob_object *blob)
1410 const struct got_error *err = NULL;
1414 hdrlen = got_object_blob_get_hdrlen(blob);
1416 err = got_object_blob_read_block(&len, blob);
1422 /* Skip blob object header first time around. */
1423 fwrite(got_object_blob_get_read_buf(blob) + hdrlen,
1424 len - hdrlen, 1, outfile);
1434 static struct got_tree_entry *
1435 find_entry_by_name(struct got_tree_object *tree, const char *name)
1437 struct got_tree_entry *te;
1439 SIMPLEQ_FOREACH(te, &tree->entries, entry) {
1440 if (strcmp(te->name, name) == 0)
1446 const struct got_error *
1447 got_object_open_by_path(struct got_object **obj, struct got_repository *repo,
1448 struct got_object_id *commit_id, const char *path)
1450 const struct got_error *err = NULL;
1451 struct got_commit_object *commit = NULL;
1452 struct got_tree_object *tree = NULL;
1453 struct got_tree_entry *te = NULL;
1454 char *seg, *s, *s0 = NULL;
1455 size_t len = strlen(path);
1459 /* We are expecting an absolute in-repository path. */
1461 return got_error(GOT_ERR_NOT_ABSPATH);
1463 err = got_object_open_as_commit(&commit, repo, commit_id);
1467 /* Handle opening of root of commit's tree. */
1468 if (path[1] == '\0') {
1469 err = got_object_open(obj, repo, commit->tree_id);
1475 err = got_object_open_as_tree(&tree, repo, commit->tree_id);
1481 err = got_error_from_errno();
1484 err = got_canonpath(path, s0, len + 1);
1489 s++; /* skip leading '/' */
1493 struct got_tree_object *next_tree;
1502 /* end of path segment */
1505 te = find_entry_by_name(tree, seg);
1507 err = got_error(GOT_ERR_NO_OBJ);
1518 err = got_object_open_as_tree(&next_tree, repo,
1523 got_object_tree_close(tree);
1529 err = got_object_open(obj, repo, te->id);
1533 err = got_error(GOT_ERR_NO_OBJ);
1537 got_object_commit_close(commit);
1539 got_object_tree_close(tree);