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"
51 #define MIN(_a,_b) ((_a) < (_b) ? (_a) : (_b))
55 #define nitems(_a) (sizeof(_a) / sizeof((_a)[0]))
58 #define GOT_OBJ_TAG_COMMIT "commit"
59 #define GOT_OBJ_TAG_TREE "tree"
60 #define GOT_OBJ_TAG_BLOB "blob"
62 #define GOT_COMMIT_TAG_TREE "tree "
63 #define GOT_COMMIT_TAG_PARENT "parent "
64 #define GOT_COMMIT_TAG_AUTHOR "author "
65 #define GOT_COMMIT_TAG_COMMITTER "committer "
67 const struct got_error *
68 got_object_id_str(char **outbuf, struct got_object_id *id)
70 static const size_t len = SHA1_DIGEST_STRING_LENGTH;
72 *outbuf = calloc(1, len);
74 return got_error_from_errno();
76 if (got_sha1_digest_to_str(id->sha1, *outbuf, len) == NULL) {
79 return got_error(GOT_ERR_BAD_OBJ_ID_STR);
86 got_object_id_cmp(struct got_object_id *id1, struct got_object_id *id2)
88 return memcmp(id1->sha1, id2->sha1, SHA1_DIGEST_LENGTH);
91 struct got_object_id *
92 got_object_id_dup(struct got_object_id *id1)
94 struct got_object_id *id2;
96 id2 = malloc(sizeof(*id2));
99 memcpy(id2, id1, sizeof(*id2));
103 struct got_object_id *
104 got_object_get_id(struct got_object *obj)
106 return got_object_id_dup(&obj->id);
109 const struct got_error *
110 got_object_get_id_str(char **outbuf, struct got_object *obj)
112 return got_object_id_str(outbuf, &obj->id);
116 got_object_get_type(struct got_object *obj)
119 case GOT_OBJ_TYPE_COMMIT:
120 case GOT_OBJ_TYPE_TREE:
121 case GOT_OBJ_TYPE_BLOB:
122 case GOT_OBJ_TYPE_TAG:
133 static const struct got_error *
134 parse_object_header(struct got_object **obj, char *buf, size_t len)
136 const char *obj_tags[] = {
141 const int obj_types[] = {
147 size_t size = 0, hdrlen = 0;
149 char *p = strchr(buf, '\0');
152 return got_error(GOT_ERR_BAD_OBJ_HDR);
154 hdrlen = strlen(buf) + 1 /* '\0' */;
156 for (i = 0; i < nitems(obj_tags); i++) {
157 const char *tag = obj_tags[i];
158 size_t tlen = strlen(tag);
161 if (strncmp(buf, tag, tlen) != 0)
166 return got_error(GOT_ERR_BAD_OBJ_HDR);
167 size = strtonum(buf + tlen, 0, LONG_MAX, &errstr);
169 return got_error(GOT_ERR_BAD_OBJ_HDR);
174 return got_error(GOT_ERR_BAD_OBJ_HDR);
176 *obj = calloc(1, sizeof(**obj));
178 return got_error_from_errno();
180 (*obj)->hdrlen = hdrlen;
185 static const struct got_error *
186 read_object_header(struct got_object **obj, FILE *f)
188 const struct got_error *err;
189 struct got_zstream_buf zb;
191 const size_t zbsize = 64;
192 size_t outlen, totlen;
195 buf = calloc(zbsize, sizeof(char));
197 return got_error_from_errno();
199 err = got_inflate_init(&zb, NULL, zbsize);
206 err = got_inflate_read(&zb, f, &outlen);
209 if (strchr(zb.outbuf, '\0') == NULL) {
210 buf = recallocarray(buf, 1 + i, 2 + i, zbsize);
212 err = got_error_from_errno();
216 memcpy(buf + totlen, zb.outbuf, outlen);
219 } while (strchr(zb.outbuf, '\0') == NULL);
221 err = parse_object_header(obj, buf, totlen);
223 got_inflate_end(&zb);
228 read_object_header_privsep_child(int obj_fd, int imsg_fds[2])
230 const struct got_error *err = NULL;
231 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 f = fdopen(obj_fd, "rb");
248 err = got_error_from_errno();
253 err = read_object_header(&obj, f);
257 err = got_privsep_send_obj(&ibuf, obj, 0);
260 got_object_close(obj);
262 got_privsep_send_error(&ibuf, err);
272 static const struct got_error *
273 wait_for_child(pid_t pid)
277 waitpid(pid, &child_status, 0);
279 if (!WIFEXITED(child_status))
280 return got_error(GOT_ERR_PRIVSEP_DIED);
282 if (WEXITSTATUS(child_status) != 0)
283 return got_error(GOT_ERR_PRIVSEP_EXIT);
288 static const struct got_error *
289 read_object_header_privsep(struct got_object **obj, int fd)
291 struct imsgbuf parent_ibuf;
293 const struct got_error *err = NULL, *err_child = NULL;
296 if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_fds) == -1)
297 return got_error_from_errno();
301 return got_error_from_errno();
303 read_object_header_privsep_child(fd, imsg_fds);
308 imsg_init(&parent_ibuf, imsg_fds[0]);
309 err = got_privsep_recv_obj(obj, &parent_ibuf);
310 imsg_clear(&parent_ibuf);
311 err_child = wait_for_child(pid);
313 return err ? err : err_child;
316 static const struct got_error *
317 object_path(char **path, struct got_object_id *id, struct got_repository *repo)
319 const struct got_error *err = NULL;
321 char *path_objects = got_repo_get_path_objects(repo);
325 if (path_objects == NULL)
326 return got_error_from_errno();
328 err = got_object_id_str(&hex, id);
332 if (asprintf(path, "%s/%.2x/%s", path_objects,
333 id->sha1[0], hex + 2) == -1)
334 err = got_error_from_errno();
341 static const struct got_error *
342 open_loose_object(int *fd, struct got_object *obj, struct got_repository *repo)
344 const struct got_error *err = NULL;
347 err = object_path(&path, &obj->id, repo);
350 *fd = open(path, O_RDONLY | O_NOFOLLOW, GOT_DEFAULT_FILE_MODE);
352 err = got_error_from_errno();
360 const struct got_error *
361 got_object_open(struct got_object **obj, struct got_repository *repo,
362 struct got_object_id *id)
364 const struct got_error *err = NULL;
368 err = object_path(&path, id, repo);
372 fd = open(path, O_RDONLY | O_NOFOLLOW, GOT_DEFAULT_FILE_MODE);
374 if (errno != ENOENT) {
375 err = got_error_from_errno();
378 err = got_packfile_open_object(obj, id, repo);
382 err = got_error(GOT_ERR_NO_OBJ);
384 err = read_object_header_privsep(obj, fd);
387 memcpy((*obj)->id.sha1, id->sha1, SHA1_DIGEST_LENGTH);
397 const struct got_error *
398 got_object_open_by_id_str(struct got_object **obj, struct got_repository *repo,
401 struct got_object_id id;
403 if (!got_parse_sha1_digest(id.sha1, id_str))
404 return got_error(GOT_ERR_BAD_OBJ_ID_STR);
406 return got_object_open(obj, repo, &id);
410 got_object_close(struct got_object *obj)
412 if (obj->flags & GOT_OBJ_FLAG_DELTIFIED) {
413 struct got_delta *delta;
414 while (!SIMPLEQ_EMPTY(&obj->deltas.entries)) {
415 delta = SIMPLEQ_FIRST(&obj->deltas.entries);
416 SIMPLEQ_REMOVE_HEAD(&obj->deltas.entries, entry);
417 got_delta_close(delta);
420 if (obj->flags & GOT_OBJ_FLAG_PACKED)
421 free(obj->path_packfile);
425 struct got_commit_object *
426 got_object_commit_alloc_partial(void)
428 struct got_commit_object *commit;
430 commit = calloc(1, sizeof(*commit));
433 commit->tree_id = calloc(1, sizeof(*commit->tree_id));
434 if (commit->tree_id == NULL) {
439 SIMPLEQ_INIT(&commit->parent_ids);
444 const struct got_error *
445 got_object_open_as_commit(struct got_commit_object **commit,
446 struct got_repository *repo, struct got_object_id *id)
448 const struct got_error *err;
449 struct got_object *obj;
451 err = got_object_open(&obj, repo, id);
454 if (got_object_get_type(obj) != GOT_OBJ_TYPE_COMMIT) {
455 err = got_error(GOT_ERR_OBJ_TYPE);
459 err = got_object_commit_open(commit, repo, obj);
461 got_object_close(obj);
465 const struct got_error *
466 got_object_commit_add_parent(struct got_commit_object *commit,
469 const struct got_error *err = NULL;
470 struct got_object_qid *qid;
472 qid = calloc(1, sizeof(*qid));
474 return got_error_from_errno();
476 qid->id = calloc(1, sizeof(*qid->id));
477 if (qid->id == NULL) {
478 err = got_error_from_errno();
483 if (!got_parse_sha1_digest(qid->id->sha1, id_str)) {
484 err = got_error(GOT_ERR_BAD_OBJ_DATA);
490 SIMPLEQ_INSERT_TAIL(&commit->parent_ids, qid, entry);
496 static const struct got_error *
497 parse_gmtoff(time_t *gmtoff, const char *tzstr)
500 const char *p = tzstr;
508 return got_error(GOT_ERR_BAD_OBJ_DATA);
510 if (!isdigit(*p) && !isdigit(*(p + 1)))
511 return got_error(GOT_ERR_BAD_OBJ_DATA);
512 h = (((*p - '0') * 10) + (*(p + 1) - '0'));
515 if (!isdigit(*p) && !isdigit(*(p + 1)))
516 return got_error(GOT_ERR_BAD_OBJ_DATA);
517 m = ((*p - '0') * 10) + (*(p + 1) - '0');
519 *gmtoff = (h * 60 * 60 + m * 60) * sign;
523 static const struct got_error *
524 parse_commit_time(struct tm *tm, char *committer)
526 const struct got_error *err = NULL;
532 /* Parse and strip off trailing timezone indicator string. */
533 space = strrchr(committer, ' ');
535 return got_error(GOT_ERR_BAD_OBJ_DATA);
536 tzstr = strdup(space + 1);
538 return got_error_from_errno();
539 err = parse_gmtoff(&gmtoff, tzstr);
545 /* Timestamp is separated from committer name + email by space. */
546 space = strrchr(committer, ' ');
548 return got_error(GOT_ERR_BAD_OBJ_DATA);
550 /* Timestamp parsed here is expressed in comitter's local time. */
551 time = strtonum(space + 1, 0, INT64_MAX, &errstr);
553 return got_error(GOT_ERR_BAD_OBJ_DATA);
555 /* Express the time stamp in UTC. */
556 memset(tm, 0, sizeof(*tm));
558 if (localtime_r(&time, tm) == NULL)
559 return got_error_from_errno();
560 tm->tm_gmtoff = gmtoff;
562 /* Strip off parsed time information, leaving just author and email. */
568 static const struct got_error *
569 parse_commit_object(struct got_commit_object **commit, char *buf, size_t len)
571 const struct got_error *err = NULL;
574 ssize_t remain = (ssize_t)len;
576 *commit = got_object_commit_alloc_partial();
578 return got_error_from_errno();
580 tlen = strlen(GOT_COMMIT_TAG_TREE);
581 if (strncmp(s, GOT_COMMIT_TAG_TREE, tlen) == 0) {
583 if (remain < SHA1_DIGEST_STRING_LENGTH) {
584 err = got_error(GOT_ERR_BAD_OBJ_DATA);
588 if (!got_parse_sha1_digest((*commit)->tree_id->sha1, s)) {
589 err = got_error(GOT_ERR_BAD_OBJ_DATA);
592 remain -= SHA1_DIGEST_STRING_LENGTH;
593 s += SHA1_DIGEST_STRING_LENGTH;
595 err = got_error(GOT_ERR_BAD_OBJ_DATA);
599 tlen = strlen(GOT_COMMIT_TAG_PARENT);
600 while (strncmp(s, GOT_COMMIT_TAG_PARENT, tlen) == 0) {
602 if (remain < SHA1_DIGEST_STRING_LENGTH) {
603 err = got_error(GOT_ERR_BAD_OBJ_DATA);
607 err = got_object_commit_add_parent(*commit, s);
611 remain -= SHA1_DIGEST_STRING_LENGTH;
612 s += SHA1_DIGEST_STRING_LENGTH;
615 tlen = strlen(GOT_COMMIT_TAG_AUTHOR);
616 if (strncmp(s, GOT_COMMIT_TAG_AUTHOR, tlen) == 0) {
622 err = got_error(GOT_ERR_BAD_OBJ_DATA);
628 err = got_error(GOT_ERR_BAD_OBJ_DATA);
633 err = parse_commit_time(&(*commit)->tm_author, s);
636 (*commit)->author = strdup(s);
637 if ((*commit)->author == NULL) {
638 err = got_error_from_errno();
645 tlen = strlen(GOT_COMMIT_TAG_COMMITTER);
646 if (strncmp(s, GOT_COMMIT_TAG_COMMITTER, tlen) == 0) {
652 err = got_error(GOT_ERR_BAD_OBJ_DATA);
658 err = got_error(GOT_ERR_BAD_OBJ_DATA);
663 err = parse_commit_time(&(*commit)->tm_committer, s);
666 (*commit)->committer = strdup(s);
667 if ((*commit)->committer == NULL) {
668 err = got_error_from_errno();
675 (*commit)->logmsg = strndup(s, remain);
676 if ((*commit)->logmsg == NULL) {
677 err = got_error_from_errno();
682 got_object_commit_close(*commit);
689 tree_entry_close(struct got_tree_entry *te)
696 struct got_tree_entry *
697 got_alloc_tree_entry_partial(void)
699 struct got_tree_entry *te;
701 te = calloc(1, sizeof(*te));
705 te->id = calloc(1, sizeof(*te->id));
706 if (te->id == NULL) {
713 static const struct got_error *
714 parse_tree_entry(struct got_tree_entry **te, size_t *elen, char *buf,
717 char *p = buf, *space;
718 const struct got_error *err = NULL;
720 *te = got_alloc_tree_entry_partial();
722 return got_error_from_errno();
724 *elen = strlen(buf) + 1;
725 if (*elen > maxlen) {
728 return got_error(GOT_ERR_BAD_OBJ_DATA);
731 space = strchr(buf, ' ');
733 err = got_error(GOT_ERR_BAD_OBJ_DATA);
739 if (*p < '0' && *p > '7') {
740 err = got_error(GOT_ERR_BAD_OBJ_DATA);
744 (*te)->mode |= *p - '0';
748 (*te)->name = strdup(space + 1);
749 if (*elen > maxlen || maxlen - *elen < SHA1_DIGEST_LENGTH) {
750 err = got_error(GOT_ERR_BAD_OBJ_DATA);
753 buf += strlen(buf) + 1;
754 memcpy((*te)->id->sha1, buf, SHA1_DIGEST_LENGTH);
755 *elen += SHA1_DIGEST_LENGTH;
758 tree_entry_close(*te);
764 static const struct got_error *
765 parse_tree_object(struct got_tree_object **tree, uint8_t *buf, size_t len)
767 const struct got_error *err;
770 *tree = calloc(1, sizeof(**tree));
772 return got_error_from_errno();
774 SIMPLEQ_INIT(&(*tree)->entries);
777 struct got_tree_entry *te;
780 err = parse_tree_entry(&te, &elen, buf, remain);
784 SIMPLEQ_INSERT_TAIL(&(*tree)->entries, te, entry);
790 got_object_tree_close(*tree);
791 return got_error(GOT_ERR_BAD_OBJ_DATA);
797 static const struct got_error *
798 read_to_mem(uint8_t **outbuf, size_t *outlen, FILE *f)
800 const struct got_error *err = NULL;
801 static const size_t blocksize = 512;
802 size_t n, total, remain;
808 buf = calloc(1, blocksize);
810 return got_error_from_errno();
817 newbuf = reallocarray(buf, 1, total + blocksize);
818 if (newbuf == NULL) {
819 err = got_error_from_errno();
825 n = fread(buf + total, 1, remain, f);
828 err = got_ferror(f, GOT_ERR_IO);
846 static const struct got_error *
847 read_commit_object(struct got_commit_object **commit, struct got_object *obj,
850 const struct got_error *err = NULL;
854 if (obj->flags & GOT_OBJ_FLAG_PACKED)
855 err = read_to_mem(&p, &len, f);
857 err = got_inflate_to_mem(&p, &len, f);
861 if (len < obj->hdrlen + obj->size) {
862 err = got_error(GOT_ERR_BAD_OBJ_DATA);
866 /* Skip object header. */
868 err = parse_commit_object(commit, p + obj->hdrlen, len);
875 read_commit_object_privsep_child(struct got_object *obj, int obj_fd,
878 const struct got_error *err = NULL;
879 struct got_commit_object *commit = NULL;
884 setproctitle("read commit object");
886 imsg_init(&ibuf, imsg_fds[1]);
888 /* revoke access to most system calls */
889 if (pledge("stdio", NULL) == -1) {
890 err = got_error_from_errno();
894 f = fdopen(obj_fd, "rb");
896 err = got_error_from_errno();
901 err = read_commit_object(&commit, obj, f);
905 err = got_privsep_send_commit(&ibuf, commit);
908 got_object_commit_close(commit);
910 got_privsep_send_error(&ibuf, err);
920 static const struct got_error *
921 read_commit_object_privsep(struct got_commit_object **commit,
922 struct got_repository *repo, struct got_object *obj, int fd)
924 const struct got_error *err = NULL, *err_child = NULL;
925 struct imsgbuf parent_ibuf;
929 if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_fds) == -1)
930 return got_error_from_errno();
934 return got_error_from_errno();
936 read_commit_object_privsep_child(obj, fd, imsg_fds);
941 imsg_init(&parent_ibuf, imsg_fds[0]);
942 err = got_privsep_recv_commit(commit, &parent_ibuf);
943 imsg_clear(&parent_ibuf);
944 err_child = wait_for_child(pid);
946 return err ? err : err_child;
949 const struct got_error *
950 got_object_commit_open(struct got_commit_object **commit,
951 struct got_repository *repo, struct got_object *obj)
953 const struct got_error *err = NULL;
955 if (obj->type != GOT_OBJ_TYPE_COMMIT)
956 return got_error(GOT_ERR_OBJ_TYPE);
958 if (obj->flags & GOT_OBJ_FLAG_PACKED) {
961 err = got_packfile_extract_object_to_mem(&buf, &len, obj, repo);
965 err = parse_commit_object(commit, buf, len);
969 err = open_loose_object(&fd, obj, repo);
972 err = read_commit_object_privsep(commit, repo, obj, fd);
979 got_object_commit_close(struct got_commit_object *commit)
981 struct got_object_qid *qid;
983 while (!SIMPLEQ_EMPTY(&commit->parent_ids)) {
984 qid = SIMPLEQ_FIRST(&commit->parent_ids);
985 SIMPLEQ_REMOVE_HEAD(&commit->parent_ids, entry);
990 free(commit->tree_id);
991 free(commit->author);
992 free(commit->committer);
993 free(commit->logmsg);
997 static const struct got_error *
998 read_tree_object(struct got_tree_object **tree, struct got_object *obj, FILE *f)
1000 const struct got_error *err = NULL;
1004 if (obj->flags & GOT_OBJ_FLAG_PACKED)
1005 err = read_to_mem(&p, &len, f);
1007 err = got_inflate_to_mem(&p, &len, f);
1011 if (len < obj->hdrlen + obj->size) {
1012 err = got_error(GOT_ERR_BAD_OBJ_DATA);
1016 /* Skip object header. */
1018 err = parse_tree_object(tree, p + obj->hdrlen, len);
1025 read_tree_object_privsep_child(struct got_object *obj, int obj_fd,
1028 const struct got_error *err = NULL;
1029 struct got_tree_object *tree = NULL;
1030 struct imsgbuf ibuf;
1034 setproctitle("read tree object");
1036 imsg_init(&ibuf, imsg_fds[1]);
1038 /* revoke access to most system calls */
1039 if (pledge("stdio", NULL) == -1) {
1040 err = got_error_from_errno();
1044 f = fdopen(obj_fd, "rb");
1046 err = got_error_from_errno();
1051 err = read_tree_object(&tree, obj, f);
1055 err = got_privsep_send_tree(&ibuf, tree);
1058 got_object_tree_close(tree);
1060 got_privsep_send_error(&ibuf, err);
1070 static const struct got_error *
1071 read_tree_object_privsep(struct got_tree_object **tree, struct got_object *obj,
1074 const struct got_error *err = NULL, *err_child = NULL;
1075 struct imsgbuf parent_ibuf;
1079 if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_fds) == -1)
1080 return got_error_from_errno();
1084 return got_error_from_errno();
1085 else if (pid == 0) {
1086 read_tree_object_privsep_child(obj, fd, imsg_fds);
1091 imsg_init(&parent_ibuf, imsg_fds[0]);
1092 err = got_privsep_recv_tree(tree, &parent_ibuf);
1093 imsg_clear(&parent_ibuf);
1094 err_child = wait_for_child(pid);
1096 return err ? err : err_child;
1099 const struct got_error *
1100 got_object_tree_open(struct got_tree_object **tree,
1101 struct got_repository *repo, struct got_object *obj)
1103 const struct got_error *err = NULL;
1105 if (obj->type != GOT_OBJ_TYPE_TREE)
1106 return got_error(GOT_ERR_OBJ_TYPE);
1108 if (obj->flags & GOT_OBJ_FLAG_PACKED) {
1111 err = got_packfile_extract_object_to_mem(&buf, &len, obj, repo);
1115 err = parse_tree_object(tree, buf, len);
1119 err = open_loose_object(&fd, obj, repo);
1122 err = read_tree_object_privsep(tree, obj, fd);
1128 const struct got_error *
1129 got_object_open_as_tree(struct got_tree_object **tree,
1130 struct got_repository *repo, struct got_object_id *id)
1132 const struct got_error *err;
1133 struct got_object *obj;
1135 err = got_object_open(&obj, repo, id);
1138 if (got_object_get_type(obj) != GOT_OBJ_TYPE_TREE) {
1139 err = got_error(GOT_ERR_OBJ_TYPE);
1143 err = got_object_tree_open(tree, repo, obj);
1145 got_object_close(obj);
1150 got_object_tree_close(struct got_tree_object *tree)
1152 struct got_tree_entry *te;
1154 while (!SIMPLEQ_EMPTY(&tree->entries)) {
1155 te = SIMPLEQ_FIRST(&tree->entries);
1156 SIMPLEQ_REMOVE_HEAD(&tree->entries, entry);
1157 tree_entry_close(te);
1163 static const struct got_error *
1164 read_blob_object_privsep_child(int outfd, int infd, int imsg_fds[2])
1166 const struct got_error *err = NULL;
1167 struct imsgbuf ibuf;
1170 FILE *infile = NULL;
1172 setproctitle("read blob object");
1174 imsg_init(&ibuf, imsg_fds[1]);
1176 /* revoke access to most system calls */
1177 if (pledge("stdio", NULL) == -1) {
1178 err = got_error_from_errno();
1182 infile = fdopen(infd, "rb");
1183 if (infile == NULL) {
1184 err = got_error_from_errno();
1188 err = got_inflate_to_fd(&size, infile, outfd);
1193 err = got_privsep_send_blob(&ibuf, size);
1196 got_privsep_send_error(&ibuf, err);
1205 static const struct got_error *
1206 read_blob_object_privsep(size_t *size, int outfd, int infd)
1208 struct imsgbuf parent_ibuf;
1210 const struct got_error *err = NULL, *err_child = NULL;
1213 if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_fds) == -1)
1214 return got_error_from_errno();
1218 return got_error_from_errno();
1219 else if (pid == 0) {
1220 read_blob_object_privsep_child(outfd, infd, imsg_fds);
1225 imsg_init(&parent_ibuf, imsg_fds[0]);
1226 err = got_privsep_recv_blob(size, &parent_ibuf);
1227 imsg_clear(&parent_ibuf);
1228 err_child = wait_for_child(pid);
1230 if (lseek(outfd, SEEK_SET, 0) == -1)
1231 err = got_error_from_errno();
1232 return err ? err : err_child;
1235 const struct got_error *
1236 got_object_blob_open(struct got_blob_object **blob,
1237 struct got_repository *repo, struct got_object *obj, size_t blocksize)
1239 const struct got_error *err = NULL;
1241 if (obj->type != GOT_OBJ_TYPE_BLOB)
1242 return got_error(GOT_ERR_OBJ_TYPE);
1244 if (blocksize < obj->hdrlen)
1245 return got_error(GOT_ERR_NO_SPACE);
1247 *blob = calloc(1, sizeof(**blob));
1249 return got_error_from_errno();
1251 (*blob)->read_buf = calloc(1, blocksize);
1252 if ((*blob)->read_buf == NULL) {
1253 err = got_error_from_errno();
1256 if (obj->flags & GOT_OBJ_FLAG_PACKED) {
1257 err = got_packfile_extract_object(&((*blob)->f), obj, repo);
1265 err = open_loose_object(&infd, obj, repo);
1270 outfd = got_opentempfd();
1272 err = got_error_from_errno();
1277 err = read_blob_object_privsep(&size, outfd, infd);
1282 if (size != obj->hdrlen + obj->size) {
1283 err = got_error(GOT_ERR_PRIVSEP_LEN);
1288 if (fstat(outfd, &sb) == -1) {
1289 err = got_error_from_errno();
1294 if (sb.st_size != size) {
1295 err = got_error(GOT_ERR_PRIVSEP_LEN);
1300 (*blob)->f = fdopen(outfd, "rb");
1301 if ((*blob)->f == NULL) {
1302 err = got_error_from_errno();
1308 (*blob)->hdrlen = obj->hdrlen;
1309 (*blob)->blocksize = blocksize;
1310 memcpy(&(*blob)->id.sha1, obj->id.sha1, SHA1_DIGEST_LENGTH);
1316 free((*blob)->read_buf);
1323 const struct got_error *
1324 got_object_open_as_blob(struct got_blob_object **blob,
1325 struct got_repository *repo, struct got_object_id *id,
1328 const struct got_error *err;
1329 struct got_object *obj;
1331 err = got_object_open(&obj, repo, id);
1334 if (got_object_get_type(obj) != GOT_OBJ_TYPE_BLOB) {
1335 err = got_error(GOT_ERR_OBJ_TYPE);
1339 err = got_object_blob_open(blob, repo, obj, blocksize);
1341 got_object_close(obj);
1346 got_object_blob_close(struct got_blob_object *blob)
1348 free(blob->read_buf);
1354 got_object_blob_id_str(struct got_blob_object *blob, char *buf, size_t size)
1356 return got_sha1_digest_to_str(blob->id.sha1, buf, size);
1360 got_object_blob_get_hdrlen(struct got_blob_object *blob)
1362 return blob->hdrlen;
1366 got_object_blob_get_read_buf(struct got_blob_object *blob)
1368 return blob->read_buf;
1371 const struct got_error *
1372 got_object_blob_read_block(size_t *outlenp, struct got_blob_object *blob)
1376 n = fread(blob->read_buf, 1, blob->blocksize, blob->f);
1377 if (n == 0 && ferror(blob->f))
1378 return got_ferror(blob->f, GOT_ERR_IO);
1383 const struct got_error *
1384 got_object_blob_dump_to_file(size_t *total_len, FILE *outfile,
1385 struct got_blob_object *blob)
1387 const struct got_error *err = NULL;
1391 hdrlen = got_object_blob_get_hdrlen(blob);
1393 err = got_object_blob_read_block(&len, blob);
1399 /* Skip blob object header first time around. */
1400 fwrite(got_object_blob_get_read_buf(blob) + hdrlen,
1401 len - hdrlen, 1, outfile);
1411 static struct got_tree_entry *
1412 find_entry_by_name(struct got_tree_object *tree, const char *name)
1414 struct got_tree_entry *te;
1416 SIMPLEQ_FOREACH(te, &tree->entries, entry) {
1417 if (strcmp(te->name, name) == 0)
1423 const struct got_error *
1424 got_object_open_by_path(struct got_object **obj, struct got_repository *repo,
1425 struct got_object_id *commit_id, const char *path)
1427 const struct got_error *err = NULL;
1428 struct got_commit_object *commit = NULL;
1429 struct got_tree_object *tree = NULL;
1430 struct got_tree_entry *te = NULL;
1431 char *seg, *s, *s0 = NULL;
1432 size_t len = strlen(path);
1436 /* We are expecting an absolute in-repository path. */
1438 return got_error(GOT_ERR_NOT_ABSPATH);
1440 err = got_object_open_as_commit(&commit, repo, commit_id);
1444 /* Handle opening of root of commit's tree. */
1445 if (path[1] == '\0') {
1446 err = got_object_open(obj, repo, commit->tree_id);
1452 err = got_object_open_as_tree(&tree, repo, commit->tree_id);
1458 err = got_error_from_errno();
1461 err = got_canonpath(path, s0, len + 1);
1466 s++; /* skip leading '/' */
1470 struct got_tree_object *next_tree;
1479 /* end of path segment */
1482 te = find_entry_by_name(tree, seg);
1484 err = got_error(GOT_ERR_NO_OBJ);
1495 err = got_object_open_as_tree(&next_tree, repo,
1500 got_object_tree_close(tree);
1506 err = got_object_open(obj, repo, te->id);
1508 err = got_error(GOT_ERR_NO_OBJ);
1512 got_object_commit_close(commit);
1514 got_object_tree_close(tree);