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);
109 got_object_get_type(struct got_object *obj)
112 case GOT_OBJ_TYPE_COMMIT:
113 case GOT_OBJ_TYPE_TREE:
114 case GOT_OBJ_TYPE_BLOB:
115 case GOT_OBJ_TYPE_TAG:
126 static const struct got_error *
127 parse_object_header(struct got_object **obj, char *buf, size_t len)
129 const char *obj_tags[] = {
134 const int obj_types[] = {
140 size_t size = 0, hdrlen = 0;
142 char *p = strchr(buf, '\0');
145 return got_error(GOT_ERR_BAD_OBJ_HDR);
147 hdrlen = strlen(buf) + 1 /* '\0' */;
149 for (i = 0; i < nitems(obj_tags); i++) {
150 const char *tag = obj_tags[i];
151 size_t tlen = strlen(tag);
154 if (strncmp(buf, tag, tlen) != 0)
159 return got_error(GOT_ERR_BAD_OBJ_HDR);
160 size = strtonum(buf + tlen, 0, LONG_MAX, &errstr);
162 return got_error(GOT_ERR_BAD_OBJ_HDR);
167 return got_error(GOT_ERR_BAD_OBJ_HDR);
169 *obj = calloc(1, sizeof(**obj));
171 return got_error_from_errno();
173 (*obj)->hdrlen = hdrlen;
178 static const struct got_error *
179 read_object_header(struct got_object **obj, FILE *f)
181 const struct got_error *err;
182 struct got_zstream_buf zb;
184 const size_t zbsize = 64;
185 size_t outlen, totlen;
188 buf = calloc(zbsize, sizeof(char));
190 return got_error_from_errno();
192 err = got_inflate_init(&zb, NULL, zbsize);
199 err = got_inflate_read(&zb, f, &outlen);
202 if (strchr(zb.outbuf, '\0') == NULL) {
203 buf = recallocarray(buf, 1 + i, 2 + i, zbsize);
205 err = got_error_from_errno();
209 memcpy(buf + totlen, zb.outbuf, outlen);
212 } while (strchr(zb.outbuf, '\0') == NULL);
214 err = parse_object_header(obj, buf, totlen);
216 got_inflate_end(&zb);
221 read_object_header_privsep_child(int obj_fd, int imsg_fds[2])
223 const struct got_error *err = NULL;
224 struct got_object *obj = NULL;
229 setproctitle("read object header");
231 imsg_init(&ibuf, imsg_fds[1]);
233 /* revoke access to most system calls */
234 if (pledge("stdio", NULL) == -1) {
235 err = got_error_from_errno();
239 f = fdopen(obj_fd, "rb");
241 err = got_error_from_errno();
246 err = read_object_header(&obj, f);
250 err = got_privsep_send_obj(&ibuf, obj, 0);
253 got_object_close(obj);
255 got_privsep_send_error(&ibuf, err);
265 static const struct got_error *
266 wait_for_child(pid_t pid)
270 waitpid(pid, &child_status, 0);
272 if (!WIFEXITED(child_status))
273 return got_error(GOT_ERR_PRIVSEP_DIED);
275 if (WEXITSTATUS(child_status) != 0)
276 return got_error(GOT_ERR_PRIVSEP_EXIT);
281 static const struct got_error *
282 read_object_header_privsep(struct got_object **obj, int fd)
284 struct imsgbuf parent_ibuf;
286 const struct got_error *err = NULL, *err_child = NULL;
289 if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_fds) == -1)
290 return got_error_from_errno();
294 return got_error_from_errno();
296 read_object_header_privsep_child(fd, imsg_fds);
301 imsg_init(&parent_ibuf, imsg_fds[0]);
302 err = got_privsep_recv_obj(obj, &parent_ibuf);
303 imsg_clear(&parent_ibuf);
304 err_child = wait_for_child(pid);
306 return err ? err : err_child;
309 static const struct got_error *
310 object_path(char **path, struct got_object_id *id, struct got_repository *repo)
312 const struct got_error *err = NULL;
314 char *path_objects = got_repo_get_path_objects(repo);
318 if (path_objects == NULL)
319 return got_error_from_errno();
321 err = got_object_id_str(&hex, id);
325 if (asprintf(path, "%s/%.2x/%s", path_objects,
326 id->sha1[0], hex + 2) == -1)
327 err = got_error_from_errno();
334 static const struct got_error *
335 open_loose_object(int *fd, struct got_object *obj, struct got_repository *repo)
337 const struct got_error *err = NULL;
340 err = object_path(&path, &obj->id, repo);
343 *fd = open(path, O_RDONLY | O_NOFOLLOW, GOT_DEFAULT_FILE_MODE);
345 err = got_error_from_errno();
353 const struct got_error *
354 got_object_open(struct got_object **obj, struct got_repository *repo,
355 struct got_object_id *id)
357 const struct got_error *err = NULL;
361 err = object_path(&path, id, repo);
365 fd = open(path, O_RDONLY | O_NOFOLLOW, GOT_DEFAULT_FILE_MODE);
367 if (errno != ENOENT) {
368 err = got_error_from_errno();
371 err = got_packfile_open_object(obj, id, repo);
375 err = got_error(GOT_ERR_NO_OBJ);
377 err = read_object_header_privsep(obj, fd);
380 memcpy((*obj)->id.sha1, id->sha1, SHA1_DIGEST_LENGTH);
390 const struct got_error *
391 got_object_open_by_id_str(struct got_object **obj, struct got_repository *repo,
394 struct got_object_id id;
396 if (!got_parse_sha1_digest(id.sha1, id_str))
397 return got_error(GOT_ERR_BAD_OBJ_ID_STR);
399 return got_object_open(obj, repo, &id);
403 got_object_close(struct got_object *obj)
405 if (obj->flags & GOT_OBJ_FLAG_DELTIFIED) {
406 struct got_delta *delta;
407 while (!SIMPLEQ_EMPTY(&obj->deltas.entries)) {
408 delta = SIMPLEQ_FIRST(&obj->deltas.entries);
409 SIMPLEQ_REMOVE_HEAD(&obj->deltas.entries, entry);
410 got_delta_close(delta);
413 if (obj->flags & GOT_OBJ_FLAG_PACKED)
414 free(obj->path_packfile);
418 struct got_commit_object *
419 got_object_commit_alloc_partial(void)
421 struct got_commit_object *commit;
423 commit = calloc(1, sizeof(*commit));
426 commit->tree_id = calloc(1, sizeof(*commit->tree_id));
427 if (commit->tree_id == NULL) {
432 SIMPLEQ_INIT(&commit->parent_ids);
437 const struct got_error *
438 got_object_commit_add_parent(struct got_commit_object *commit,
441 const struct got_error *err = NULL;
442 struct got_parent_id *pid;
444 pid = calloc(1, sizeof(*pid));
446 return got_error_from_errno();
448 pid->id = calloc(1, sizeof(*pid->id));
449 if (pid->id == NULL) {
450 err = got_error_from_errno();
455 if (!got_parse_sha1_digest(pid->id->sha1, id_str)) {
456 err = got_error(GOT_ERR_BAD_OBJ_DATA);
462 SIMPLEQ_INSERT_TAIL(&commit->parent_ids, pid, entry);
468 static const struct got_error *
469 parse_commit_object(struct got_commit_object **commit, char *buf, size_t len)
471 const struct got_error *err = NULL;
474 ssize_t remain = (ssize_t)len;
476 *commit = got_object_commit_alloc_partial();
478 return got_error_from_errno();
480 tlen = strlen(GOT_COMMIT_TAG_TREE);
481 if (strncmp(s, GOT_COMMIT_TAG_TREE, tlen) == 0) {
483 if (remain < SHA1_DIGEST_STRING_LENGTH) {
484 err = got_error(GOT_ERR_BAD_OBJ_DATA);
488 if (!got_parse_sha1_digest((*commit)->tree_id->sha1, s)) {
489 err = got_error(GOT_ERR_BAD_OBJ_DATA);
492 remain -= SHA1_DIGEST_STRING_LENGTH;
493 s += SHA1_DIGEST_STRING_LENGTH;
495 err = got_error(GOT_ERR_BAD_OBJ_DATA);
499 tlen = strlen(GOT_COMMIT_TAG_PARENT);
500 while (strncmp(s, GOT_COMMIT_TAG_PARENT, tlen) == 0) {
502 if (remain < SHA1_DIGEST_STRING_LENGTH) {
503 err = got_error(GOT_ERR_BAD_OBJ_DATA);
507 err = got_object_commit_add_parent(*commit, s);
511 remain -= SHA1_DIGEST_STRING_LENGTH;
512 s += SHA1_DIGEST_STRING_LENGTH;
515 tlen = strlen(GOT_COMMIT_TAG_AUTHOR);
516 if (strncmp(s, GOT_COMMIT_TAG_AUTHOR, tlen) == 0) {
521 err = got_error(GOT_ERR_BAD_OBJ_DATA);
527 err = got_error(GOT_ERR_BAD_OBJ_DATA);
531 (*commit)->author = strdup(s);
532 if ((*commit)->author == NULL) {
533 err = got_error_from_errno();
536 s += strlen((*commit)->author) + 1;
537 remain -= strlen((*commit)->author) + 1;
540 tlen = strlen(GOT_COMMIT_TAG_COMMITTER);
541 if (strncmp(s, GOT_COMMIT_TAG_COMMITTER, tlen) == 0) {
546 err = got_error(GOT_ERR_BAD_OBJ_DATA);
552 err = got_error(GOT_ERR_BAD_OBJ_DATA);
556 (*commit)->committer = strdup(s);
557 if ((*commit)->committer == NULL) {
558 err = got_error_from_errno();
561 s += strlen((*commit)->committer) + 1;
562 remain -= strlen((*commit)->committer) + 1;
565 (*commit)->logmsg = strndup(s, remain);
566 if ((*commit)->logmsg == NULL) {
567 err = got_error_from_errno();
572 got_object_commit_close(*commit);
579 tree_entry_close(struct got_tree_entry *te)
586 struct got_tree_entry *
587 got_alloc_tree_entry_partial(void)
589 struct got_tree_entry *te;
591 te = calloc(1, sizeof(*te));
595 te->id = calloc(1, sizeof(*te->id));
596 if (te->id == NULL) {
603 static const struct got_error *
604 parse_tree_entry(struct got_tree_entry **te, size_t *elen, char *buf,
607 char *p = buf, *space;
608 const struct got_error *err = NULL;
610 *te = got_alloc_tree_entry_partial();
612 return got_error_from_errno();
614 *elen = strlen(buf) + 1;
615 if (*elen > maxlen) {
618 return got_error(GOT_ERR_BAD_OBJ_DATA);
621 space = strchr(buf, ' ');
623 err = got_error(GOT_ERR_BAD_OBJ_DATA);
629 if (*p < '0' && *p > '7') {
630 err = got_error(GOT_ERR_BAD_OBJ_DATA);
634 (*te)->mode |= *p - '0';
638 (*te)->name = strdup(space + 1);
639 if (*elen > maxlen || maxlen - *elen < SHA1_DIGEST_LENGTH) {
640 err = got_error(GOT_ERR_BAD_OBJ_DATA);
643 buf += strlen(buf) + 1;
644 memcpy((*te)->id->sha1, buf, SHA1_DIGEST_LENGTH);
645 *elen += SHA1_DIGEST_LENGTH;
648 tree_entry_close(*te);
654 static const struct got_error *
655 parse_tree_object(struct got_tree_object **tree, uint8_t *buf, size_t len)
657 const struct got_error *err;
660 *tree = calloc(1, sizeof(**tree));
662 return got_error_from_errno();
664 SIMPLEQ_INIT(&(*tree)->entries);
667 struct got_tree_entry *te;
670 err = parse_tree_entry(&te, &elen, buf, remain);
674 SIMPLEQ_INSERT_TAIL(&(*tree)->entries, te, entry);
680 got_object_tree_close(*tree);
681 return got_error(GOT_ERR_BAD_OBJ_DATA);
687 static const struct got_error *
688 read_to_mem(uint8_t **outbuf, size_t *outlen, FILE *f)
690 const struct got_error *err = NULL;
691 static const size_t blocksize = 512;
692 size_t n, total, remain;
698 buf = calloc(1, blocksize);
700 return got_error_from_errno();
707 newbuf = reallocarray(buf, 1, total + blocksize);
708 if (newbuf == NULL) {
709 err = got_error_from_errno();
715 n = fread(buf + total, 1, remain, f);
718 err = got_ferror(f, GOT_ERR_IO);
736 static const struct got_error *
737 read_commit_object(struct got_commit_object **commit, struct got_object *obj,
740 const struct got_error *err = NULL;
744 if (obj->flags & GOT_OBJ_FLAG_PACKED)
745 err = read_to_mem(&p, &len, f);
747 err = got_inflate_to_mem(&p, &len, f);
751 if (len < obj->hdrlen + obj->size) {
752 err = got_error(GOT_ERR_BAD_OBJ_DATA);
756 /* Skip object header. */
758 err = parse_commit_object(commit, p + obj->hdrlen, len);
765 read_commit_object_privsep_child(struct got_object *obj, int obj_fd,
768 const struct got_error *err = NULL;
769 struct got_commit_object *commit = NULL;
774 setproctitle("read commit object");
776 imsg_init(&ibuf, imsg_fds[1]);
778 /* revoke access to most system calls */
779 if (pledge("stdio", NULL) == -1) {
780 err = got_error_from_errno();
784 f = fdopen(obj_fd, "rb");
786 err = got_error_from_errno();
791 err = read_commit_object(&commit, obj, f);
795 err = got_privsep_send_commit(&ibuf, commit);
798 got_object_commit_close(commit);
800 got_privsep_send_error(&ibuf, err);
810 static const struct got_error *
811 read_commit_object_privsep(struct got_commit_object **commit,
812 struct got_repository *repo, struct got_object *obj, int fd)
814 const struct got_error *err = NULL, *err_child = NULL;
815 struct imsgbuf parent_ibuf;
819 if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_fds) == -1)
820 return got_error_from_errno();
824 return got_error_from_errno();
826 read_commit_object_privsep_child(obj, fd, imsg_fds);
831 imsg_init(&parent_ibuf, imsg_fds[0]);
832 err = got_privsep_recv_commit(commit, &parent_ibuf);
833 imsg_clear(&parent_ibuf);
834 err_child = wait_for_child(pid);
836 return err ? err : err_child;
839 const struct got_error *
840 got_object_commit_open(struct got_commit_object **commit,
841 struct got_repository *repo, struct got_object *obj)
843 const struct got_error *err = NULL;
845 if (obj->type != GOT_OBJ_TYPE_COMMIT)
846 return got_error(GOT_ERR_OBJ_TYPE);
848 if (obj->flags & GOT_OBJ_FLAG_PACKED) {
851 err = got_packfile_extract_object_to_mem(&buf, &len, obj, repo);
855 err = parse_commit_object(commit, buf, len);
859 err = open_loose_object(&fd, obj, repo);
862 err = read_commit_object_privsep(commit, repo, obj, fd);
869 got_object_commit_close(struct got_commit_object *commit)
871 struct got_parent_id *pid;
873 while (!SIMPLEQ_EMPTY(&commit->parent_ids)) {
874 pid = SIMPLEQ_FIRST(&commit->parent_ids);
875 SIMPLEQ_REMOVE_HEAD(&commit->parent_ids, entry);
880 free(commit->tree_id);
881 free(commit->author);
882 free(commit->committer);
883 free(commit->logmsg);
887 static const struct got_error *
888 read_tree_object(struct got_tree_object **tree, struct got_object *obj, FILE *f)
890 const struct got_error *err = NULL;
894 if (obj->flags & GOT_OBJ_FLAG_PACKED)
895 err = read_to_mem(&p, &len, f);
897 err = got_inflate_to_mem(&p, &len, f);
901 if (len < obj->hdrlen + obj->size) {
902 err = got_error(GOT_ERR_BAD_OBJ_DATA);
906 /* Skip object header. */
908 err = parse_tree_object(tree, p + obj->hdrlen, len);
915 read_tree_object_privsep_child(struct got_object *obj, int obj_fd,
918 const struct got_error *err = NULL;
919 struct got_tree_object *tree = NULL;
924 setproctitle("read tree object");
926 imsg_init(&ibuf, imsg_fds[1]);
928 /* revoke access to most system calls */
929 if (pledge("stdio", NULL) == -1) {
930 err = got_error_from_errno();
934 f = fdopen(obj_fd, "rb");
936 err = got_error_from_errno();
941 err = read_tree_object(&tree, obj, f);
945 err = got_privsep_send_tree(&ibuf, tree);
948 got_object_tree_close(tree);
950 got_privsep_send_error(&ibuf, err);
960 static const struct got_error *
961 read_tree_object_privsep(struct got_tree_object **tree, struct got_object *obj,
964 const struct got_error *err = NULL, *err_child = NULL;
965 struct imsgbuf parent_ibuf;
969 if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_fds) == -1)
970 return got_error_from_errno();
974 return got_error_from_errno();
976 read_tree_object_privsep_child(obj, fd, imsg_fds);
981 imsg_init(&parent_ibuf, imsg_fds[0]);
982 err = got_privsep_recv_tree(tree, &parent_ibuf);
983 imsg_clear(&parent_ibuf);
984 err_child = wait_for_child(pid);
986 return err ? err : err_child;
989 const struct got_error *
990 got_object_tree_open(struct got_tree_object **tree,
991 struct got_repository *repo, struct got_object *obj)
993 const struct got_error *err = NULL;
995 if (obj->type != GOT_OBJ_TYPE_TREE)
996 return got_error(GOT_ERR_OBJ_TYPE);
998 if (obj->flags & GOT_OBJ_FLAG_PACKED) {
1001 err = got_packfile_extract_object_to_mem(&buf, &len, obj, repo);
1005 err = parse_tree_object(tree, buf, len);
1009 err = open_loose_object(&fd, obj, repo);
1012 err = read_tree_object_privsep(tree, obj, fd);
1019 got_object_tree_close(struct got_tree_object *tree)
1021 struct got_tree_entry *te;
1023 while (!SIMPLEQ_EMPTY(&tree->entries)) {
1024 te = SIMPLEQ_FIRST(&tree->entries);
1025 SIMPLEQ_REMOVE_HEAD(&tree->entries, entry);
1026 tree_entry_close(te);
1032 static const struct got_error *
1033 read_blob_object_privsep_child(int outfd, int infd, int imsg_fds[2])
1035 const struct got_error *err = NULL;
1036 struct imsgbuf ibuf;
1039 FILE *infile = NULL;
1041 setproctitle("read blob object");
1043 imsg_init(&ibuf, imsg_fds[1]);
1045 /* revoke access to most system calls */
1046 if (pledge("stdio", NULL) == -1) {
1047 err = got_error_from_errno();
1051 infile = fdopen(infd, "rb");
1052 if (infile == NULL) {
1053 err = got_error_from_errno();
1057 err = got_inflate_to_fd(&size, infile, outfd);
1062 err = got_privsep_send_blob(&ibuf, size);
1065 got_privsep_send_error(&ibuf, err);
1074 static const struct got_error *
1075 read_blob_object_privsep(size_t *size, int outfd, int infd)
1077 struct imsgbuf parent_ibuf;
1079 const struct got_error *err = NULL, *err_child = NULL;
1082 if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_fds) == -1)
1083 return got_error_from_errno();
1087 return got_error_from_errno();
1088 else if (pid == 0) {
1089 read_blob_object_privsep_child(outfd, infd, imsg_fds);
1094 imsg_init(&parent_ibuf, imsg_fds[0]);
1095 err = got_privsep_recv_blob(size, &parent_ibuf);
1096 imsg_clear(&parent_ibuf);
1097 err_child = wait_for_child(pid);
1099 if (lseek(outfd, SEEK_SET, 0) == -1)
1100 err = got_error_from_errno();
1101 return err ? err : err_child;
1104 const struct got_error *
1105 got_object_blob_open(struct got_blob_object **blob,
1106 struct got_repository *repo, struct got_object *obj, size_t blocksize)
1108 const struct got_error *err = NULL;
1110 if (obj->type != GOT_OBJ_TYPE_BLOB)
1111 return got_error(GOT_ERR_OBJ_TYPE);
1113 if (blocksize < obj->hdrlen)
1114 return got_error(GOT_ERR_NO_SPACE);
1116 *blob = calloc(1, sizeof(**blob));
1118 return got_error_from_errno();
1120 (*blob)->read_buf = calloc(1, blocksize);
1121 if ((*blob)->read_buf == NULL) {
1122 err = got_error_from_errno();
1125 if (obj->flags & GOT_OBJ_FLAG_PACKED) {
1126 err = got_packfile_extract_object(&((*blob)->f), obj, repo);
1134 err = open_loose_object(&infd, obj, repo);
1139 outfd = got_opentempfd();
1141 err = got_error_from_errno();
1146 err = read_blob_object_privsep(&size, outfd, infd);
1151 if (size != obj->hdrlen + obj->size) {
1152 err = got_error(GOT_ERR_PRIVSEP_LEN);
1157 if (fstat(outfd, &sb) == -1) {
1158 err = got_error_from_errno();
1163 if (sb.st_size != size) {
1164 err = got_error(GOT_ERR_PRIVSEP_LEN);
1169 (*blob)->f = fdopen(outfd, "rb");
1170 if ((*blob)->f == NULL) {
1171 err = got_error_from_errno();
1177 (*blob)->hdrlen = obj->hdrlen;
1178 (*blob)->blocksize = blocksize;
1179 memcpy(&(*blob)->id.sha1, obj->id.sha1, SHA1_DIGEST_LENGTH);
1185 free((*blob)->read_buf);
1193 got_object_blob_close(struct got_blob_object *blob)
1195 free(blob->read_buf);
1201 got_object_blob_id_str(struct got_blob_object *blob, char *buf, size_t size)
1203 return got_sha1_digest_to_str(blob->id.sha1, buf, size);
1207 got_object_blob_get_hdrlen(struct got_blob_object *blob)
1209 return blob->hdrlen;
1213 got_object_blob_get_read_buf(struct got_blob_object *blob)
1215 return blob->read_buf;
1218 const struct got_error *
1219 got_object_blob_read_block(size_t *outlenp, struct got_blob_object *blob)
1223 n = fread(blob->read_buf, 1, blob->blocksize, blob->f);
1224 if (n == 0 && ferror(blob->f))
1225 return got_ferror(blob->f, GOT_ERR_IO);