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;
453 err = got_object_open(&obj, repo, id);
456 if (got_object_get_type(obj) != GOT_OBJ_TYPE_COMMIT) {
457 err = got_error(GOT_ERR_OBJ_TYPE);
461 err = got_object_commit_open(commit, repo, obj);
463 got_object_close(obj);
467 const struct got_error *
468 got_object_commit_add_parent(struct got_commit_object *commit,
471 const struct got_error *err = NULL;
472 struct got_object_qid *qid;
474 qid = calloc(1, sizeof(*qid));
476 return got_error_from_errno();
478 qid->id = calloc(1, sizeof(*qid->id));
479 if (qid->id == NULL) {
480 err = got_error_from_errno();
485 if (!got_parse_sha1_digest(qid->id->sha1, id_str)) {
486 err = got_error(GOT_ERR_BAD_OBJ_DATA);
492 SIMPLEQ_INSERT_TAIL(&commit->parent_ids, qid, entry);
498 static const struct got_error *
499 parse_gmtoff(time_t *gmtoff, const char *tzstr)
502 const char *p = tzstr;
510 return got_error(GOT_ERR_BAD_OBJ_DATA);
512 if (!isdigit(*p) && !isdigit(*(p + 1)))
513 return got_error(GOT_ERR_BAD_OBJ_DATA);
514 h = (((*p - '0') * 10) + (*(p + 1) - '0'));
517 if (!isdigit(*p) && !isdigit(*(p + 1)))
518 return got_error(GOT_ERR_BAD_OBJ_DATA);
519 m = ((*p - '0') * 10) + (*(p + 1) - '0');
521 *gmtoff = (h * 60 * 60 + m * 60) * sign;
525 static const struct got_error *
526 parse_commit_time(struct tm *tm, char *committer)
528 const struct got_error *err = NULL;
534 /* Parse and strip off trailing timezone indicator string. */
535 space = strrchr(committer, ' ');
537 return got_error(GOT_ERR_BAD_OBJ_DATA);
538 tzstr = strdup(space + 1);
540 return got_error_from_errno();
541 err = parse_gmtoff(&gmtoff, tzstr);
547 /* Timestamp is separated from committer name + email by space. */
548 space = strrchr(committer, ' ');
550 return got_error(GOT_ERR_BAD_OBJ_DATA);
552 /* Timestamp parsed here is expressed in comitter's local time. */
553 time = strtonum(space + 1, 0, INT64_MAX, &errstr);
555 return got_error(GOT_ERR_BAD_OBJ_DATA);
557 /* Express the time stamp in UTC. */
558 memset(tm, 0, sizeof(*tm));
560 if (localtime_r(&time, tm) == NULL)
561 return got_error_from_errno();
562 tm->tm_gmtoff = gmtoff;
564 /* Strip off parsed time information, leaving just author and email. */
570 static const struct got_error *
571 parse_commit_object(struct got_commit_object **commit, char *buf, size_t len)
573 const struct got_error *err = NULL;
576 ssize_t remain = (ssize_t)len;
578 *commit = got_object_commit_alloc_partial();
580 return got_error_from_errno();
582 tlen = strlen(GOT_COMMIT_TAG_TREE);
583 if (strncmp(s, GOT_COMMIT_TAG_TREE, tlen) == 0) {
585 if (remain < SHA1_DIGEST_STRING_LENGTH) {
586 err = got_error(GOT_ERR_BAD_OBJ_DATA);
590 if (!got_parse_sha1_digest((*commit)->tree_id->sha1, s)) {
591 err = got_error(GOT_ERR_BAD_OBJ_DATA);
594 remain -= SHA1_DIGEST_STRING_LENGTH;
595 s += SHA1_DIGEST_STRING_LENGTH;
597 err = got_error(GOT_ERR_BAD_OBJ_DATA);
601 tlen = strlen(GOT_COMMIT_TAG_PARENT);
602 while (strncmp(s, GOT_COMMIT_TAG_PARENT, tlen) == 0) {
604 if (remain < SHA1_DIGEST_STRING_LENGTH) {
605 err = got_error(GOT_ERR_BAD_OBJ_DATA);
609 err = got_object_commit_add_parent(*commit, s);
613 remain -= SHA1_DIGEST_STRING_LENGTH;
614 s += SHA1_DIGEST_STRING_LENGTH;
617 tlen = strlen(GOT_COMMIT_TAG_AUTHOR);
618 if (strncmp(s, GOT_COMMIT_TAG_AUTHOR, tlen) == 0) {
624 err = got_error(GOT_ERR_BAD_OBJ_DATA);
630 err = got_error(GOT_ERR_BAD_OBJ_DATA);
635 err = parse_commit_time(&(*commit)->tm_author, s);
638 (*commit)->author = strdup(s);
639 if ((*commit)->author == NULL) {
640 err = got_error_from_errno();
647 tlen = strlen(GOT_COMMIT_TAG_COMMITTER);
648 if (strncmp(s, GOT_COMMIT_TAG_COMMITTER, tlen) == 0) {
654 err = got_error(GOT_ERR_BAD_OBJ_DATA);
660 err = got_error(GOT_ERR_BAD_OBJ_DATA);
665 err = parse_commit_time(&(*commit)->tm_committer, s);
668 (*commit)->committer = strdup(s);
669 if ((*commit)->committer == NULL) {
670 err = got_error_from_errno();
677 (*commit)->logmsg = strndup(s, remain);
678 if ((*commit)->logmsg == NULL) {
679 err = got_error_from_errno();
684 got_object_commit_close(*commit);
691 tree_entry_close(struct got_tree_entry *te)
698 struct got_tree_entry *
699 got_alloc_tree_entry_partial(void)
701 struct got_tree_entry *te;
703 te = calloc(1, sizeof(*te));
707 te->id = calloc(1, sizeof(*te->id));
708 if (te->id == NULL) {
715 static const struct got_error *
716 parse_tree_entry(struct got_tree_entry **te, size_t *elen, char *buf,
719 char *p = buf, *space;
720 const struct got_error *err = NULL;
722 *te = got_alloc_tree_entry_partial();
724 return got_error_from_errno();
726 *elen = strlen(buf) + 1;
727 if (*elen > maxlen) {
730 return got_error(GOT_ERR_BAD_OBJ_DATA);
733 space = strchr(buf, ' ');
735 err = got_error(GOT_ERR_BAD_OBJ_DATA);
741 if (*p < '0' && *p > '7') {
742 err = got_error(GOT_ERR_BAD_OBJ_DATA);
746 (*te)->mode |= *p - '0';
750 (*te)->name = strdup(space + 1);
751 if (*elen > maxlen || maxlen - *elen < SHA1_DIGEST_LENGTH) {
752 err = got_error(GOT_ERR_BAD_OBJ_DATA);
755 buf += strlen(buf) + 1;
756 memcpy((*te)->id->sha1, buf, SHA1_DIGEST_LENGTH);
757 *elen += SHA1_DIGEST_LENGTH;
760 tree_entry_close(*te);
766 static const struct got_error *
767 parse_tree_object(struct got_tree_object **tree, uint8_t *buf, size_t len)
769 const struct got_error *err;
772 *tree = calloc(1, sizeof(**tree));
774 return got_error_from_errno();
776 SIMPLEQ_INIT(&(*tree)->entries);
779 struct got_tree_entry *te;
782 err = parse_tree_entry(&te, &elen, buf, remain);
786 SIMPLEQ_INSERT_TAIL(&(*tree)->entries, te, entry);
792 got_object_tree_close(*tree);
793 return got_error(GOT_ERR_BAD_OBJ_DATA);
799 static const struct got_error *
800 read_to_mem(uint8_t **outbuf, size_t *outlen, FILE *f)
802 const struct got_error *err = NULL;
803 static const size_t blocksize = 512;
804 size_t n, total, remain;
810 buf = calloc(1, blocksize);
812 return got_error_from_errno();
819 newbuf = reallocarray(buf, 1, total + blocksize);
820 if (newbuf == NULL) {
821 err = got_error_from_errno();
827 n = fread(buf + total, 1, remain, f);
830 err = got_ferror(f, GOT_ERR_IO);
848 static const struct got_error *
849 read_commit_object(struct got_commit_object **commit, struct got_object *obj,
852 const struct got_error *err = NULL;
856 if (obj->flags & GOT_OBJ_FLAG_PACKED)
857 err = read_to_mem(&p, &len, f);
859 err = got_inflate_to_mem(&p, &len, f);
863 if (len < obj->hdrlen + obj->size) {
864 err = got_error(GOT_ERR_BAD_OBJ_DATA);
868 /* Skip object header. */
870 err = parse_commit_object(commit, p + obj->hdrlen, len);
877 read_commit_object_privsep_child(struct got_object *obj, int obj_fd,
880 const struct got_error *err = NULL;
881 struct got_commit_object *commit = NULL;
886 setproctitle("read commit object");
888 imsg_init(&ibuf, imsg_fds[1]);
890 /* revoke access to most system calls */
891 if (pledge("stdio", NULL) == -1) {
892 err = got_error_from_errno();
896 f = fdopen(obj_fd, "rb");
898 err = got_error_from_errno();
903 err = read_commit_object(&commit, obj, f);
907 err = got_privsep_send_commit(&ibuf, commit);
910 got_object_commit_close(commit);
912 got_privsep_send_error(&ibuf, err);
922 static const struct got_error *
923 read_commit_object_privsep(struct got_commit_object **commit,
924 struct got_repository *repo, struct got_object *obj, int fd)
926 const struct got_error *err = NULL, *err_child = NULL;
927 struct imsgbuf parent_ibuf;
931 if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_fds) == -1)
932 return got_error_from_errno();
936 return got_error_from_errno();
938 read_commit_object_privsep_child(obj, fd, imsg_fds);
943 imsg_init(&parent_ibuf, imsg_fds[0]);
944 err = got_privsep_recv_commit(commit, &parent_ibuf);
945 imsg_clear(&parent_ibuf);
946 err_child = wait_for_child(pid);
948 return err ? err : err_child;
951 const struct got_error *
952 got_object_commit_open(struct got_commit_object **commit,
953 struct got_repository *repo, struct got_object *obj)
955 const struct got_error *err = NULL;
957 if (obj->type != GOT_OBJ_TYPE_COMMIT)
958 return got_error(GOT_ERR_OBJ_TYPE);
960 if (obj->flags & GOT_OBJ_FLAG_PACKED) {
963 err = got_packfile_extract_object_to_mem(&buf, &len, obj, repo);
967 err = parse_commit_object(commit, buf, len);
971 err = open_loose_object(&fd, obj, repo);
974 err = read_commit_object_privsep(commit, repo, obj, fd);
981 got_object_commit_close(struct got_commit_object *commit)
983 struct got_object_qid *qid;
985 while (!SIMPLEQ_EMPTY(&commit->parent_ids)) {
986 qid = SIMPLEQ_FIRST(&commit->parent_ids);
987 SIMPLEQ_REMOVE_HEAD(&commit->parent_ids, entry);
992 free(commit->tree_id);
993 free(commit->author);
994 free(commit->committer);
995 free(commit->logmsg);
999 static const struct got_error *
1000 read_tree_object(struct got_tree_object **tree, struct got_object *obj, FILE *f)
1002 const struct got_error *err = NULL;
1006 if (obj->flags & GOT_OBJ_FLAG_PACKED)
1007 err = read_to_mem(&p, &len, f);
1009 err = got_inflate_to_mem(&p, &len, f);
1013 if (len < obj->hdrlen + obj->size) {
1014 err = got_error(GOT_ERR_BAD_OBJ_DATA);
1018 /* Skip object header. */
1020 err = parse_tree_object(tree, p + obj->hdrlen, len);
1027 read_tree_object_privsep_child(struct got_object *obj, int obj_fd,
1030 const struct got_error *err = NULL;
1031 struct got_tree_object *tree = NULL;
1032 struct imsgbuf ibuf;
1036 setproctitle("read tree object");
1038 imsg_init(&ibuf, imsg_fds[1]);
1040 /* revoke access to most system calls */
1041 if (pledge("stdio", NULL) == -1) {
1042 err = got_error_from_errno();
1046 f = fdopen(obj_fd, "rb");
1048 err = got_error_from_errno();
1053 err = read_tree_object(&tree, obj, f);
1057 err = got_privsep_send_tree(&ibuf, tree);
1060 got_object_tree_close(tree);
1062 got_privsep_send_error(&ibuf, err);
1072 static const struct got_error *
1073 read_tree_object_privsep(struct got_tree_object **tree, struct got_object *obj,
1076 const struct got_error *err = NULL, *err_child = NULL;
1077 struct imsgbuf parent_ibuf;
1081 if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_fds) == -1)
1082 return got_error_from_errno();
1086 return got_error_from_errno();
1087 else if (pid == 0) {
1088 read_tree_object_privsep_child(obj, fd, imsg_fds);
1093 imsg_init(&parent_ibuf, imsg_fds[0]);
1094 err = got_privsep_recv_tree(tree, &parent_ibuf);
1095 imsg_clear(&parent_ibuf);
1096 err_child = wait_for_child(pid);
1098 return err ? err : err_child;
1101 const struct got_error *
1102 got_object_tree_open(struct got_tree_object **tree,
1103 struct got_repository *repo, struct got_object *obj)
1105 const struct got_error *err = NULL;
1107 if (obj->type != GOT_OBJ_TYPE_TREE)
1108 return got_error(GOT_ERR_OBJ_TYPE);
1110 if (obj->flags & GOT_OBJ_FLAG_PACKED) {
1113 err = got_packfile_extract_object_to_mem(&buf, &len, obj, repo);
1117 err = parse_tree_object(tree, buf, len);
1121 err = open_loose_object(&fd, obj, repo);
1124 err = read_tree_object_privsep(tree, obj, fd);
1130 const struct got_error *
1131 got_object_open_as_tree(struct got_tree_object **tree,
1132 struct got_repository *repo, struct got_object_id *id)
1134 const struct got_error *err;
1135 struct got_object *obj;
1139 err = got_object_open(&obj, repo, id);
1142 if (got_object_get_type(obj) != GOT_OBJ_TYPE_TREE) {
1143 err = got_error(GOT_ERR_OBJ_TYPE);
1147 err = got_object_tree_open(tree, repo, obj);
1149 got_object_close(obj);
1154 got_object_tree_close(struct got_tree_object *tree)
1156 struct got_tree_entry *te;
1158 while (!SIMPLEQ_EMPTY(&tree->entries)) {
1159 te = SIMPLEQ_FIRST(&tree->entries);
1160 SIMPLEQ_REMOVE_HEAD(&tree->entries, entry);
1161 tree_entry_close(te);
1167 static const struct got_error *
1168 read_blob_object_privsep_child(int outfd, int infd, int imsg_fds[2])
1170 const struct got_error *err = NULL;
1171 struct imsgbuf ibuf;
1174 FILE *infile = NULL;
1176 setproctitle("read blob object");
1178 imsg_init(&ibuf, imsg_fds[1]);
1180 /* revoke access to most system calls */
1181 if (pledge("stdio", NULL) == -1) {
1182 err = got_error_from_errno();
1186 infile = fdopen(infd, "rb");
1187 if (infile == NULL) {
1188 err = got_error_from_errno();
1192 err = got_inflate_to_fd(&size, infile, outfd);
1197 err = got_privsep_send_blob(&ibuf, size);
1200 got_privsep_send_error(&ibuf, err);
1209 static const struct got_error *
1210 read_blob_object_privsep(size_t *size, int outfd, int infd)
1212 struct imsgbuf parent_ibuf;
1214 const struct got_error *err = NULL, *err_child = NULL;
1217 if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_fds) == -1)
1218 return got_error_from_errno();
1222 return got_error_from_errno();
1223 else if (pid == 0) {
1224 read_blob_object_privsep_child(outfd, infd, imsg_fds);
1229 imsg_init(&parent_ibuf, imsg_fds[0]);
1230 err = got_privsep_recv_blob(size, &parent_ibuf);
1231 imsg_clear(&parent_ibuf);
1232 err_child = wait_for_child(pid);
1234 if (lseek(outfd, SEEK_SET, 0) == -1)
1235 err = got_error_from_errno();
1236 return err ? err : err_child;
1239 const struct got_error *
1240 got_object_blob_open(struct got_blob_object **blob,
1241 struct got_repository *repo, struct got_object *obj, size_t blocksize)
1243 const struct got_error *err = NULL;
1245 if (obj->type != GOT_OBJ_TYPE_BLOB)
1246 return got_error(GOT_ERR_OBJ_TYPE);
1248 if (blocksize < obj->hdrlen)
1249 return got_error(GOT_ERR_NO_SPACE);
1251 *blob = calloc(1, sizeof(**blob));
1253 return got_error_from_errno();
1255 (*blob)->read_buf = calloc(1, blocksize);
1256 if ((*blob)->read_buf == NULL) {
1257 err = got_error_from_errno();
1260 if (obj->flags & GOT_OBJ_FLAG_PACKED) {
1261 err = got_packfile_extract_object(&((*blob)->f), obj, repo);
1269 err = open_loose_object(&infd, obj, repo);
1274 outfd = got_opentempfd();
1276 err = got_error_from_errno();
1281 err = read_blob_object_privsep(&size, outfd, infd);
1286 if (size != obj->hdrlen + obj->size) {
1287 err = got_error(GOT_ERR_PRIVSEP_LEN);
1292 if (fstat(outfd, &sb) == -1) {
1293 err = got_error_from_errno();
1298 if (sb.st_size != size) {
1299 err = got_error(GOT_ERR_PRIVSEP_LEN);
1304 (*blob)->f = fdopen(outfd, "rb");
1305 if ((*blob)->f == NULL) {
1306 err = got_error_from_errno();
1312 (*blob)->hdrlen = obj->hdrlen;
1313 (*blob)->blocksize = blocksize;
1314 memcpy(&(*blob)->id.sha1, obj->id.sha1, SHA1_DIGEST_LENGTH);
1320 free((*blob)->read_buf);
1327 const struct got_error *
1328 got_object_open_as_blob(struct got_blob_object **blob,
1329 struct got_repository *repo, struct got_object_id *id,
1332 const struct got_error *err;
1333 struct got_object *obj;
1337 err = got_object_open(&obj, repo, id);
1340 if (got_object_get_type(obj) != GOT_OBJ_TYPE_BLOB) {
1341 err = got_error(GOT_ERR_OBJ_TYPE);
1345 err = got_object_blob_open(blob, repo, obj, blocksize);
1347 got_object_close(obj);
1352 got_object_blob_close(struct got_blob_object *blob)
1354 free(blob->read_buf);
1360 got_object_blob_id_str(struct got_blob_object *blob, char *buf, size_t size)
1362 return got_sha1_digest_to_str(blob->id.sha1, buf, size);
1366 got_object_blob_get_hdrlen(struct got_blob_object *blob)
1368 return blob->hdrlen;
1372 got_object_blob_get_read_buf(struct got_blob_object *blob)
1374 return blob->read_buf;
1377 const struct got_error *
1378 got_object_blob_read_block(size_t *outlenp, struct got_blob_object *blob)
1382 n = fread(blob->read_buf, 1, blob->blocksize, blob->f);
1383 if (n == 0 && ferror(blob->f))
1384 return got_ferror(blob->f, GOT_ERR_IO);
1389 const struct got_error *
1390 got_object_blob_dump_to_file(size_t *total_len, FILE *outfile,
1391 struct got_blob_object *blob)
1393 const struct got_error *err = NULL;
1397 hdrlen = got_object_blob_get_hdrlen(blob);
1399 err = got_object_blob_read_block(&len, blob);
1405 /* Skip blob object header first time around. */
1406 fwrite(got_object_blob_get_read_buf(blob) + hdrlen,
1407 len - hdrlen, 1, outfile);
1417 static struct got_tree_entry *
1418 find_entry_by_name(struct got_tree_object *tree, const char *name)
1420 struct got_tree_entry *te;
1422 SIMPLEQ_FOREACH(te, &tree->entries, entry) {
1423 if (strcmp(te->name, name) == 0)
1429 const struct got_error *
1430 got_object_open_by_path(struct got_object **obj, struct got_repository *repo,
1431 struct got_object_id *commit_id, const char *path)
1433 const struct got_error *err = NULL;
1434 struct got_commit_object *commit = NULL;
1435 struct got_tree_object *tree = NULL;
1436 struct got_tree_entry *te = NULL;
1437 char *seg, *s, *s0 = NULL;
1438 size_t len = strlen(path);
1442 /* We are expecting an absolute in-repository path. */
1444 return got_error(GOT_ERR_NOT_ABSPATH);
1446 err = got_object_open_as_commit(&commit, repo, commit_id);
1450 /* Handle opening of root of commit's tree. */
1451 if (path[1] == '\0') {
1452 err = got_object_open(obj, repo, commit->tree_id);
1458 err = got_object_open_as_tree(&tree, repo, commit->tree_id);
1464 err = got_error_from_errno();
1467 err = got_canonpath(path, s0, len + 1);
1472 s++; /* skip leading '/' */
1476 struct got_tree_object *next_tree;
1485 /* end of path segment */
1488 te = find_entry_by_name(tree, seg);
1490 err = got_error(GOT_ERR_NO_OBJ);
1501 err = got_object_open_as_tree(&next_tree, repo,
1506 got_object_tree_close(tree);
1512 err = got_object_open(obj, repo, te->id);
1514 err = got_error(GOT_ERR_NO_OBJ);
1518 got_object_commit_close(commit);
1520 got_object_tree_close(tree);