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>
36 #include "got_error.h"
37 #include "got_object.h"
38 #include "got_repository.h"
39 #include "got_opentemp.h"
41 #include "got_lib_sha1.h"
42 #include "got_lib_delta.h"
43 #include "got_lib_pack.h"
44 #include "got_lib_path.h"
45 #include "got_lib_zbuf.h"
46 #include "got_lib_object.h"
47 #include "got_lib_privsep.h"
50 #define MIN(_a,_b) ((_a) < (_b) ? (_a) : (_b))
54 #define nitems(_a) (sizeof(_a) / sizeof((_a)[0]))
57 #define GOT_OBJ_TAG_COMMIT "commit"
58 #define GOT_OBJ_TAG_TREE "tree"
59 #define GOT_OBJ_TAG_BLOB "blob"
61 #define GOT_COMMIT_TAG_TREE "tree "
62 #define GOT_COMMIT_TAG_PARENT "parent "
63 #define GOT_COMMIT_TAG_AUTHOR "author "
64 #define GOT_COMMIT_TAG_COMMITTER "committer "
66 const struct got_error *
67 got_object_id_str(char **outbuf, struct got_object_id *id)
69 static const size_t len = SHA1_DIGEST_STRING_LENGTH;
71 *outbuf = calloc(1, len);
73 return got_error_from_errno();
75 if (got_sha1_digest_to_str(id->sha1, *outbuf, len) == NULL) {
78 return got_error(GOT_ERR_BAD_OBJ_ID_STR);
85 got_object_id_cmp(struct got_object_id *id1, struct got_object_id *id2)
87 return memcmp(id1->sha1, id2->sha1, SHA1_DIGEST_LENGTH);
90 struct got_object_id *
91 got_object_id_dup(struct got_object_id *id1)
93 struct got_object_id *id2;
95 id2 = malloc(sizeof(*id2));
98 memcpy(id2, id1, sizeof(*id2));
102 struct got_object_id *
103 got_object_get_id(struct got_object *obj)
105 return got_object_id_dup(&obj->id);
108 const struct got_error *
109 got_object_get_id_str(char **outbuf, struct got_object *obj)
111 return got_object_id_str(outbuf, &obj->id);
115 got_object_get_type(struct got_object *obj)
118 case GOT_OBJ_TYPE_COMMIT:
119 case GOT_OBJ_TYPE_TREE:
120 case GOT_OBJ_TYPE_BLOB:
121 case GOT_OBJ_TYPE_TAG:
132 static const struct got_error *
133 parse_object_header(struct got_object **obj, char *buf, size_t len)
135 const char *obj_tags[] = {
140 const int obj_types[] = {
146 size_t size = 0, hdrlen = 0;
148 char *p = strchr(buf, '\0');
151 return got_error(GOT_ERR_BAD_OBJ_HDR);
153 hdrlen = strlen(buf) + 1 /* '\0' */;
155 for (i = 0; i < nitems(obj_tags); i++) {
156 const char *tag = obj_tags[i];
157 size_t tlen = strlen(tag);
160 if (strncmp(buf, tag, tlen) != 0)
165 return got_error(GOT_ERR_BAD_OBJ_HDR);
166 size = strtonum(buf + tlen, 0, LONG_MAX, &errstr);
168 return got_error(GOT_ERR_BAD_OBJ_HDR);
173 return got_error(GOT_ERR_BAD_OBJ_HDR);
175 *obj = calloc(1, sizeof(**obj));
177 return got_error_from_errno();
179 (*obj)->hdrlen = hdrlen;
184 static const struct got_error *
185 read_object_header(struct got_object **obj, FILE *f)
187 const struct got_error *err;
188 struct got_zstream_buf zb;
190 const size_t zbsize = 64;
191 size_t outlen, totlen;
194 buf = calloc(zbsize, sizeof(char));
196 return got_error_from_errno();
198 err = got_inflate_init(&zb, NULL, zbsize);
205 err = got_inflate_read(&zb, f, &outlen);
208 if (strchr(zb.outbuf, '\0') == NULL) {
209 buf = recallocarray(buf, 1 + i, 2 + i, zbsize);
211 err = got_error_from_errno();
215 memcpy(buf + totlen, zb.outbuf, outlen);
218 } while (strchr(zb.outbuf, '\0') == NULL);
220 err = parse_object_header(obj, buf, totlen);
222 got_inflate_end(&zb);
227 read_object_header_privsep_child(int obj_fd, int imsg_fds[2])
229 const struct got_error *err = NULL;
230 struct got_object *obj = NULL;
235 setproctitle("read object header");
237 imsg_init(&ibuf, imsg_fds[1]);
239 /* revoke access to most system calls */
240 if (pledge("stdio", NULL) == -1) {
241 err = got_error_from_errno();
245 f = fdopen(obj_fd, "rb");
247 err = got_error_from_errno();
252 err = read_object_header(&obj, f);
256 err = got_privsep_send_obj(&ibuf, obj, 0);
259 got_object_close(obj);
261 got_privsep_send_error(&ibuf, err);
271 static const struct got_error *
272 wait_for_child(pid_t pid)
276 waitpid(pid, &child_status, 0);
278 if (!WIFEXITED(child_status))
279 return got_error(GOT_ERR_PRIVSEP_DIED);
281 if (WEXITSTATUS(child_status) != 0)
282 return got_error(GOT_ERR_PRIVSEP_EXIT);
287 static const struct got_error *
288 read_object_header_privsep(struct got_object **obj, int fd)
290 struct imsgbuf parent_ibuf;
292 const struct got_error *err = NULL, *err_child = NULL;
295 if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_fds) == -1)
296 return got_error_from_errno();
300 return got_error_from_errno();
302 read_object_header_privsep_child(fd, imsg_fds);
307 imsg_init(&parent_ibuf, imsg_fds[0]);
308 err = got_privsep_recv_obj(obj, &parent_ibuf);
309 imsg_clear(&parent_ibuf);
310 err_child = wait_for_child(pid);
312 return err ? err : err_child;
315 static const struct got_error *
316 object_path(char **path, struct got_object_id *id, struct got_repository *repo)
318 const struct got_error *err = NULL;
320 char *path_objects = got_repo_get_path_objects(repo);
324 if (path_objects == NULL)
325 return got_error_from_errno();
327 err = got_object_id_str(&hex, id);
331 if (asprintf(path, "%s/%.2x/%s", path_objects,
332 id->sha1[0], hex + 2) == -1)
333 err = got_error_from_errno();
340 static const struct got_error *
341 open_loose_object(int *fd, struct got_object *obj, struct got_repository *repo)
343 const struct got_error *err = NULL;
346 err = object_path(&path, &obj->id, repo);
349 *fd = open(path, O_RDONLY | O_NOFOLLOW, GOT_DEFAULT_FILE_MODE);
351 err = got_error_from_errno();
359 const struct got_error *
360 got_object_open(struct got_object **obj, struct got_repository *repo,
361 struct got_object_id *id)
363 const struct got_error *err = NULL;
367 err = object_path(&path, id, repo);
371 fd = open(path, O_RDONLY | O_NOFOLLOW, GOT_DEFAULT_FILE_MODE);
373 if (errno != ENOENT) {
374 err = got_error_from_errno();
377 err = got_packfile_open_object(obj, id, repo);
381 err = got_error(GOT_ERR_NO_OBJ);
383 err = read_object_header_privsep(obj, fd);
386 memcpy((*obj)->id.sha1, id->sha1, SHA1_DIGEST_LENGTH);
396 const struct got_error *
397 got_object_open_by_id_str(struct got_object **obj, struct got_repository *repo,
400 struct got_object_id id;
402 if (!got_parse_sha1_digest(id.sha1, id_str))
403 return got_error(GOT_ERR_BAD_OBJ_ID_STR);
405 return got_object_open(obj, repo, &id);
409 got_object_close(struct got_object *obj)
411 if (obj->flags & GOT_OBJ_FLAG_DELTIFIED) {
412 struct got_delta *delta;
413 while (!SIMPLEQ_EMPTY(&obj->deltas.entries)) {
414 delta = SIMPLEQ_FIRST(&obj->deltas.entries);
415 SIMPLEQ_REMOVE_HEAD(&obj->deltas.entries, entry);
416 got_delta_close(delta);
419 if (obj->flags & GOT_OBJ_FLAG_PACKED)
420 free(obj->path_packfile);
424 struct got_commit_object *
425 got_object_commit_alloc_partial(void)
427 struct got_commit_object *commit;
429 commit = calloc(1, sizeof(*commit));
432 commit->tree_id = calloc(1, sizeof(*commit->tree_id));
433 if (commit->tree_id == NULL) {
438 SIMPLEQ_INIT(&commit->parent_ids);
443 const struct got_error *
444 got_object_commit_add_parent(struct got_commit_object *commit,
447 const struct got_error *err = NULL;
448 struct got_parent_id *pid;
450 pid = calloc(1, sizeof(*pid));
452 return got_error_from_errno();
454 pid->id = calloc(1, sizeof(*pid->id));
455 if (pid->id == NULL) {
456 err = got_error_from_errno();
461 if (!got_parse_sha1_digest(pid->id->sha1, id_str)) {
462 err = got_error(GOT_ERR_BAD_OBJ_DATA);
468 SIMPLEQ_INSERT_TAIL(&commit->parent_ids, pid, entry);
474 static const struct got_error *
475 parse_commit_object(struct got_commit_object **commit, char *buf, size_t len)
477 const struct got_error *err = NULL;
480 ssize_t remain = (ssize_t)len;
482 *commit = got_object_commit_alloc_partial();
484 return got_error_from_errno();
486 tlen = strlen(GOT_COMMIT_TAG_TREE);
487 if (strncmp(s, GOT_COMMIT_TAG_TREE, tlen) == 0) {
489 if (remain < SHA1_DIGEST_STRING_LENGTH) {
490 err = got_error(GOT_ERR_BAD_OBJ_DATA);
494 if (!got_parse_sha1_digest((*commit)->tree_id->sha1, s)) {
495 err = got_error(GOT_ERR_BAD_OBJ_DATA);
498 remain -= SHA1_DIGEST_STRING_LENGTH;
499 s += SHA1_DIGEST_STRING_LENGTH;
501 err = got_error(GOT_ERR_BAD_OBJ_DATA);
505 tlen = strlen(GOT_COMMIT_TAG_PARENT);
506 while (strncmp(s, GOT_COMMIT_TAG_PARENT, tlen) == 0) {
508 if (remain < SHA1_DIGEST_STRING_LENGTH) {
509 err = got_error(GOT_ERR_BAD_OBJ_DATA);
513 err = got_object_commit_add_parent(*commit, s);
517 remain -= SHA1_DIGEST_STRING_LENGTH;
518 s += SHA1_DIGEST_STRING_LENGTH;
521 tlen = strlen(GOT_COMMIT_TAG_AUTHOR);
522 if (strncmp(s, GOT_COMMIT_TAG_AUTHOR, tlen) == 0) {
527 err = got_error(GOT_ERR_BAD_OBJ_DATA);
533 err = got_error(GOT_ERR_BAD_OBJ_DATA);
537 (*commit)->author = strdup(s);
538 if ((*commit)->author == NULL) {
539 err = got_error_from_errno();
542 s += strlen((*commit)->author) + 1;
543 remain -= strlen((*commit)->author) + 1;
546 tlen = strlen(GOT_COMMIT_TAG_COMMITTER);
547 if (strncmp(s, GOT_COMMIT_TAG_COMMITTER, tlen) == 0) {
552 err = got_error(GOT_ERR_BAD_OBJ_DATA);
558 err = got_error(GOT_ERR_BAD_OBJ_DATA);
562 (*commit)->committer = strdup(s);
563 if ((*commit)->committer == NULL) {
564 err = got_error_from_errno();
567 s += strlen((*commit)->committer) + 1;
568 remain -= strlen((*commit)->committer) + 1;
571 (*commit)->logmsg = strndup(s, remain);
572 if ((*commit)->logmsg == NULL) {
573 err = got_error_from_errno();
578 got_object_commit_close(*commit);
585 tree_entry_close(struct got_tree_entry *te)
592 struct got_tree_entry *
593 got_alloc_tree_entry_partial(void)
595 struct got_tree_entry *te;
597 te = calloc(1, sizeof(*te));
601 te->id = calloc(1, sizeof(*te->id));
602 if (te->id == NULL) {
609 static const struct got_error *
610 parse_tree_entry(struct got_tree_entry **te, size_t *elen, char *buf,
613 char *p = buf, *space;
614 const struct got_error *err = NULL;
616 *te = got_alloc_tree_entry_partial();
618 return got_error_from_errno();
620 *elen = strlen(buf) + 1;
621 if (*elen > maxlen) {
624 return got_error(GOT_ERR_BAD_OBJ_DATA);
627 space = strchr(buf, ' ');
629 err = got_error(GOT_ERR_BAD_OBJ_DATA);
635 if (*p < '0' && *p > '7') {
636 err = got_error(GOT_ERR_BAD_OBJ_DATA);
640 (*te)->mode |= *p - '0';
644 (*te)->name = strdup(space + 1);
645 if (*elen > maxlen || maxlen - *elen < SHA1_DIGEST_LENGTH) {
646 err = got_error(GOT_ERR_BAD_OBJ_DATA);
649 buf += strlen(buf) + 1;
650 memcpy((*te)->id->sha1, buf, SHA1_DIGEST_LENGTH);
651 *elen += SHA1_DIGEST_LENGTH;
654 tree_entry_close(*te);
660 static const struct got_error *
661 parse_tree_object(struct got_tree_object **tree, uint8_t *buf, size_t len)
663 const struct got_error *err;
666 *tree = calloc(1, sizeof(**tree));
668 return got_error_from_errno();
670 SIMPLEQ_INIT(&(*tree)->entries);
673 struct got_tree_entry *te;
676 err = parse_tree_entry(&te, &elen, buf, remain);
680 SIMPLEQ_INSERT_TAIL(&(*tree)->entries, te, entry);
686 got_object_tree_close(*tree);
687 return got_error(GOT_ERR_BAD_OBJ_DATA);
693 static const struct got_error *
694 read_to_mem(uint8_t **outbuf, size_t *outlen, FILE *f)
696 const struct got_error *err = NULL;
697 static const size_t blocksize = 512;
698 size_t n, total, remain;
704 buf = calloc(1, blocksize);
706 return got_error_from_errno();
713 newbuf = reallocarray(buf, 1, total + blocksize);
714 if (newbuf == NULL) {
715 err = got_error_from_errno();
721 n = fread(buf + total, 1, remain, f);
724 err = got_ferror(f, GOT_ERR_IO);
742 static const struct got_error *
743 read_commit_object(struct got_commit_object **commit, struct got_object *obj,
746 const struct got_error *err = NULL;
750 if (obj->flags & GOT_OBJ_FLAG_PACKED)
751 err = read_to_mem(&p, &len, f);
753 err = got_inflate_to_mem(&p, &len, f);
757 if (len < obj->hdrlen + obj->size) {
758 err = got_error(GOT_ERR_BAD_OBJ_DATA);
762 /* Skip object header. */
764 err = parse_commit_object(commit, p + obj->hdrlen, len);
771 read_commit_object_privsep_child(struct got_object *obj, int obj_fd,
774 const struct got_error *err = NULL;
775 struct got_commit_object *commit = NULL;
780 setproctitle("read commit object");
782 imsg_init(&ibuf, imsg_fds[1]);
784 /* revoke access to most system calls */
785 if (pledge("stdio", NULL) == -1) {
786 err = got_error_from_errno();
790 f = fdopen(obj_fd, "rb");
792 err = got_error_from_errno();
797 err = read_commit_object(&commit, obj, f);
801 err = got_privsep_send_commit(&ibuf, commit);
804 got_object_commit_close(commit);
806 got_privsep_send_error(&ibuf, err);
816 static const struct got_error *
817 read_commit_object_privsep(struct got_commit_object **commit,
818 struct got_repository *repo, struct got_object *obj, int fd)
820 const struct got_error *err = NULL, *err_child = NULL;
821 struct imsgbuf parent_ibuf;
825 if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_fds) == -1)
826 return got_error_from_errno();
830 return got_error_from_errno();
832 read_commit_object_privsep_child(obj, fd, imsg_fds);
837 imsg_init(&parent_ibuf, imsg_fds[0]);
838 err = got_privsep_recv_commit(commit, &parent_ibuf);
839 imsg_clear(&parent_ibuf);
840 err_child = wait_for_child(pid);
842 return err ? err : err_child;
845 const struct got_error *
846 got_object_commit_open(struct got_commit_object **commit,
847 struct got_repository *repo, struct got_object *obj)
849 const struct got_error *err = NULL;
851 if (obj->type != GOT_OBJ_TYPE_COMMIT)
852 return got_error(GOT_ERR_OBJ_TYPE);
854 if (obj->flags & GOT_OBJ_FLAG_PACKED) {
857 err = got_packfile_extract_object_to_mem(&buf, &len, obj, repo);
861 err = parse_commit_object(commit, buf, len);
865 err = open_loose_object(&fd, obj, repo);
868 err = read_commit_object_privsep(commit, repo, obj, fd);
875 got_object_commit_close(struct got_commit_object *commit)
877 struct got_parent_id *pid;
879 while (!SIMPLEQ_EMPTY(&commit->parent_ids)) {
880 pid = SIMPLEQ_FIRST(&commit->parent_ids);
881 SIMPLEQ_REMOVE_HEAD(&commit->parent_ids, entry);
886 free(commit->tree_id);
887 free(commit->author);
888 free(commit->committer);
889 free(commit->logmsg);
893 static const struct got_error *
894 read_tree_object(struct got_tree_object **tree, struct got_object *obj, FILE *f)
896 const struct got_error *err = NULL;
900 if (obj->flags & GOT_OBJ_FLAG_PACKED)
901 err = read_to_mem(&p, &len, f);
903 err = got_inflate_to_mem(&p, &len, f);
907 if (len < obj->hdrlen + obj->size) {
908 err = got_error(GOT_ERR_BAD_OBJ_DATA);
912 /* Skip object header. */
914 err = parse_tree_object(tree, p + obj->hdrlen, len);
921 read_tree_object_privsep_child(struct got_object *obj, int obj_fd,
924 const struct got_error *err = NULL;
925 struct got_tree_object *tree = NULL;
930 setproctitle("read tree object");
932 imsg_init(&ibuf, imsg_fds[1]);
934 /* revoke access to most system calls */
935 if (pledge("stdio", NULL) == -1) {
936 err = got_error_from_errno();
940 f = fdopen(obj_fd, "rb");
942 err = got_error_from_errno();
947 err = read_tree_object(&tree, obj, f);
951 err = got_privsep_send_tree(&ibuf, tree);
954 got_object_tree_close(tree);
956 got_privsep_send_error(&ibuf, err);
966 static const struct got_error *
967 read_tree_object_privsep(struct got_tree_object **tree, struct got_object *obj,
970 const struct got_error *err = NULL, *err_child = NULL;
971 struct imsgbuf parent_ibuf;
975 if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_fds) == -1)
976 return got_error_from_errno();
980 return got_error_from_errno();
982 read_tree_object_privsep_child(obj, fd, imsg_fds);
987 imsg_init(&parent_ibuf, imsg_fds[0]);
988 err = got_privsep_recv_tree(tree, &parent_ibuf);
989 imsg_clear(&parent_ibuf);
990 err_child = wait_for_child(pid);
992 return err ? err : err_child;
995 const struct got_error *
996 got_object_tree_open(struct got_tree_object **tree,
997 struct got_repository *repo, struct got_object *obj)
999 const struct got_error *err = NULL;
1001 if (obj->type != GOT_OBJ_TYPE_TREE)
1002 return got_error(GOT_ERR_OBJ_TYPE);
1004 if (obj->flags & GOT_OBJ_FLAG_PACKED) {
1007 err = got_packfile_extract_object_to_mem(&buf, &len, obj, repo);
1011 err = parse_tree_object(tree, buf, len);
1015 err = open_loose_object(&fd, obj, repo);
1018 err = read_tree_object_privsep(tree, obj, fd);
1025 got_object_tree_close(struct got_tree_object *tree)
1027 struct got_tree_entry *te;
1029 while (!SIMPLEQ_EMPTY(&tree->entries)) {
1030 te = SIMPLEQ_FIRST(&tree->entries);
1031 SIMPLEQ_REMOVE_HEAD(&tree->entries, entry);
1032 tree_entry_close(te);
1038 static const struct got_error *
1039 read_blob_object_privsep_child(int outfd, int infd, int imsg_fds[2])
1041 const struct got_error *err = NULL;
1042 struct imsgbuf ibuf;
1045 FILE *infile = NULL;
1047 setproctitle("read blob object");
1049 imsg_init(&ibuf, imsg_fds[1]);
1051 /* revoke access to most system calls */
1052 if (pledge("stdio", NULL) == -1) {
1053 err = got_error_from_errno();
1057 infile = fdopen(infd, "rb");
1058 if (infile == NULL) {
1059 err = got_error_from_errno();
1063 err = got_inflate_to_fd(&size, infile, outfd);
1068 err = got_privsep_send_blob(&ibuf, size);
1071 got_privsep_send_error(&ibuf, err);
1080 static const struct got_error *
1081 read_blob_object_privsep(size_t *size, int outfd, int infd)
1083 struct imsgbuf parent_ibuf;
1085 const struct got_error *err = NULL, *err_child = NULL;
1088 if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_fds) == -1)
1089 return got_error_from_errno();
1093 return got_error_from_errno();
1094 else if (pid == 0) {
1095 read_blob_object_privsep_child(outfd, infd, imsg_fds);
1100 imsg_init(&parent_ibuf, imsg_fds[0]);
1101 err = got_privsep_recv_blob(size, &parent_ibuf);
1102 imsg_clear(&parent_ibuf);
1103 err_child = wait_for_child(pid);
1105 if (lseek(outfd, SEEK_SET, 0) == -1)
1106 err = got_error_from_errno();
1107 return err ? err : err_child;
1110 const struct got_error *
1111 got_object_blob_open(struct got_blob_object **blob,
1112 struct got_repository *repo, struct got_object *obj, size_t blocksize)
1114 const struct got_error *err = NULL;
1116 if (obj->type != GOT_OBJ_TYPE_BLOB)
1117 return got_error(GOT_ERR_OBJ_TYPE);
1119 if (blocksize < obj->hdrlen)
1120 return got_error(GOT_ERR_NO_SPACE);
1122 *blob = calloc(1, sizeof(**blob));
1124 return got_error_from_errno();
1126 (*blob)->read_buf = calloc(1, blocksize);
1127 if ((*blob)->read_buf == NULL) {
1128 err = got_error_from_errno();
1131 if (obj->flags & GOT_OBJ_FLAG_PACKED) {
1132 err = got_packfile_extract_object(&((*blob)->f), obj, repo);
1140 err = open_loose_object(&infd, obj, repo);
1145 outfd = got_opentempfd();
1147 err = got_error_from_errno();
1152 err = read_blob_object_privsep(&size, outfd, infd);
1157 if (size != obj->hdrlen + obj->size) {
1158 err = got_error(GOT_ERR_PRIVSEP_LEN);
1163 if (fstat(outfd, &sb) == -1) {
1164 err = got_error_from_errno();
1169 if (sb.st_size != size) {
1170 err = got_error(GOT_ERR_PRIVSEP_LEN);
1175 (*blob)->f = fdopen(outfd, "rb");
1176 if ((*blob)->f == NULL) {
1177 err = got_error_from_errno();
1183 (*blob)->hdrlen = obj->hdrlen;
1184 (*blob)->blocksize = blocksize;
1185 memcpy(&(*blob)->id.sha1, obj->id.sha1, SHA1_DIGEST_LENGTH);
1191 free((*blob)->read_buf);
1199 got_object_blob_close(struct got_blob_object *blob)
1201 free(blob->read_buf);
1207 got_object_blob_id_str(struct got_blob_object *blob, char *buf, size_t size)
1209 return got_sha1_digest_to_str(blob->id.sha1, buf, size);
1213 got_object_blob_get_hdrlen(struct got_blob_object *blob)
1215 return blob->hdrlen;
1219 got_object_blob_get_read_buf(struct got_blob_object *blob)
1221 return blob->read_buf;
1224 const struct got_error *
1225 got_object_blob_read_block(size_t *outlenp, struct got_blob_object *blob)
1229 n = fread(blob->read_buf, 1, blob->blocksize, blob->f);
1230 if (n == 0 && ferror(blob->f))
1231 return got_ferror(blob->f, GOT_ERR_IO);