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"
40 #include "got_lib_sha1.h"
41 #include "got_lib_delta.h"
42 #include "got_lib_pack.h"
43 #include "got_lib_path.h"
44 #include "got_lib_zbuf.h"
45 #include "got_lib_object.h"
46 #include "got_lib_privsep.h"
49 #define MIN(_a,_b) ((_a) < (_b) ? (_a) : (_b))
53 #define nitems(_a) (sizeof(_a) / sizeof((_a)[0]))
56 #define GOT_OBJ_TAG_COMMIT "commit"
57 #define GOT_OBJ_TAG_TREE "tree"
58 #define GOT_OBJ_TAG_BLOB "blob"
60 #define GOT_COMMIT_TAG_TREE "tree "
61 #define GOT_COMMIT_TAG_PARENT "parent "
62 #define GOT_COMMIT_TAG_AUTHOR "author "
63 #define GOT_COMMIT_TAG_COMMITTER "committer "
65 const struct got_error *
66 got_object_id_str(char **outbuf, struct got_object_id *id)
68 static const size_t len = SHA1_DIGEST_STRING_LENGTH;
70 *outbuf = calloc(1, len);
72 return got_error_from_errno();
74 if (got_sha1_digest_to_str(id->sha1, *outbuf, len) == NULL) {
77 return got_error(GOT_ERR_BAD_OBJ_ID_STR);
84 got_object_id_cmp(struct got_object_id *id1, struct got_object_id *id2)
86 return memcmp(id1->sha1, id2->sha1, SHA1_DIGEST_LENGTH);
89 struct got_object_id *
90 got_object_id_dup(struct got_object_id *id1)
92 struct got_object_id *id2;
94 id2 = malloc(sizeof(*id2));
97 memcpy(id2, id1, sizeof(*id2));
101 struct got_object_id *
102 got_object_get_id(struct got_object *obj)
104 return got_object_id_dup(&obj->id);
108 got_object_get_type(struct got_object *obj)
111 case GOT_OBJ_TYPE_COMMIT:
112 case GOT_OBJ_TYPE_TREE:
113 case GOT_OBJ_TYPE_BLOB:
114 case GOT_OBJ_TYPE_TAG:
125 static const struct got_error *
126 parse_object_header(struct got_object **obj, char *buf, size_t len)
128 const char *obj_tags[] = {
133 const int obj_types[] = {
139 size_t size = 0, hdrlen = 0;
141 char *p = strchr(buf, '\0');
144 return got_error(GOT_ERR_BAD_OBJ_HDR);
146 hdrlen = strlen(buf) + 1 /* '\0' */;
148 for (i = 0; i < nitems(obj_tags); i++) {
149 const char *tag = obj_tags[i];
150 size_t tlen = strlen(tag);
153 if (strncmp(buf, tag, tlen) != 0)
158 return got_error(GOT_ERR_BAD_OBJ_HDR);
159 size = strtonum(buf + tlen, 0, LONG_MAX, &errstr);
161 return got_error(GOT_ERR_BAD_OBJ_HDR);
166 return got_error(GOT_ERR_BAD_OBJ_HDR);
168 *obj = calloc(1, sizeof(**obj));
170 return got_error_from_errno();
172 (*obj)->hdrlen = hdrlen;
177 static const struct got_error *
178 read_object_header(struct got_object **obj, FILE *f)
180 const struct got_error *err;
181 struct got_zstream_buf zb;
183 const size_t zbsize = 64;
184 size_t outlen, totlen;
187 buf = calloc(zbsize, sizeof(char));
189 return got_error_from_errno();
191 err = got_inflate_init(&zb, NULL, zbsize);
198 err = got_inflate_read(&zb, f, &outlen);
201 if (strchr(zb.outbuf, '\0') == NULL) {
202 buf = recallocarray(buf, 1 + i, 2 + i, zbsize);
204 err = got_error_from_errno();
208 memcpy(buf + totlen, zb.outbuf, outlen);
211 } while (strchr(zb.outbuf, '\0') == NULL);
213 err = parse_object_header(obj, buf, totlen);
215 got_inflate_end(&zb);
220 read_object_header_privsep_child(int obj_fd, int imsg_fds[2])
222 const struct got_error *err = NULL;
223 struct got_object *obj = NULL;
228 setproctitle("read object header");
230 imsg_init(&ibuf, imsg_fds[1]);
232 /* revoke access to most system calls */
233 if (pledge("stdio", NULL) == -1) {
234 err = got_error_from_errno();
238 f = fdopen(obj_fd, "rb");
240 err = got_error_from_errno();
245 err = read_object_header(&obj, f);
249 err = got_privsep_send_obj(&ibuf, obj, 0);
252 got_object_close(obj);
254 got_privsep_send_error(&ibuf, err);
264 static const struct got_error *
265 wait_for_child(pid_t pid)
269 waitpid(pid, &child_status, 0);
271 if (!WIFEXITED(child_status))
272 return got_error(GOT_ERR_PRIVSEP_DIED);
274 if (WEXITSTATUS(child_status) != 0)
275 return got_error(GOT_ERR_PRIVSEP_EXIT);
280 static const struct got_error *
281 read_object_header_privsep(struct got_object **obj, int fd)
283 struct imsgbuf parent_ibuf;
285 const struct got_error *err = NULL, *err_child = NULL;
288 if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_fds) == -1)
289 return got_error_from_errno();
293 return got_error_from_errno();
295 read_object_header_privsep_child(fd, imsg_fds);
300 imsg_init(&parent_ibuf, imsg_fds[0]);
301 err = got_privsep_recv_obj(obj, &parent_ibuf);
302 imsg_clear(&parent_ibuf);
303 err_child = wait_for_child(pid);
305 return err ? err : err_child;
308 static const struct got_error *
309 object_path(char **path, struct got_object_id *id, struct got_repository *repo)
311 const struct got_error *err = NULL;
313 char *path_objects = got_repo_get_path_objects(repo);
317 if (path_objects == NULL)
318 return got_error_from_errno();
320 err = got_object_id_str(&hex, id);
324 if (asprintf(path, "%s/%.2x/%s", path_objects,
325 id->sha1[0], hex + 2) == -1)
326 err = got_error_from_errno();
333 static const struct got_error *
334 open_loose_object(int *fd, struct got_object *obj, struct got_repository *repo)
336 const struct got_error *err = NULL;
339 err = object_path(&path, &obj->id, repo);
342 *fd = open(path, O_RDONLY | O_NOFOLLOW, GOT_DEFAULT_FILE_MODE);
344 err = got_error_from_errno();
352 const struct got_error *
353 got_object_open(struct got_object **obj, struct got_repository *repo,
354 struct got_object_id *id)
356 const struct got_error *err = NULL;
360 err = object_path(&path, id, repo);
364 fd = open(path, O_RDONLY | O_NOFOLLOW, GOT_DEFAULT_FILE_MODE);
366 if (errno != ENOENT) {
367 err = got_error_from_errno();
370 err = got_packfile_open_object(obj, id, repo);
374 err = got_error(GOT_ERR_NO_OBJ);
376 err = read_object_header_privsep(obj, fd);
379 memcpy((*obj)->id.sha1, id->sha1, SHA1_DIGEST_LENGTH);
389 const struct got_error *
390 got_object_open_by_id_str(struct got_object **obj, struct got_repository *repo,
393 struct got_object_id id;
395 if (!got_parse_sha1_digest(id.sha1, id_str))
396 return got_error(GOT_ERR_BAD_OBJ_ID_STR);
398 return got_object_open(obj, repo, &id);
402 got_object_close(struct got_object *obj)
404 if (obj->flags & GOT_OBJ_FLAG_DELTIFIED) {
405 struct got_delta *delta;
406 while (!SIMPLEQ_EMPTY(&obj->deltas.entries)) {
407 delta = SIMPLEQ_FIRST(&obj->deltas.entries);
408 SIMPLEQ_REMOVE_HEAD(&obj->deltas.entries, entry);
409 got_delta_close(delta);
412 if (obj->flags & GOT_OBJ_FLAG_PACKED)
413 free(obj->path_packfile);
417 struct got_commit_object *
418 got_object_commit_alloc_partial(void)
420 struct got_commit_object *commit;
422 commit = calloc(1, sizeof(*commit));
425 commit->tree_id = calloc(1, sizeof(*commit->tree_id));
426 if (commit->tree_id == NULL) {
431 SIMPLEQ_INIT(&commit->parent_ids);
436 const struct got_error *
437 got_object_commit_add_parent(struct got_commit_object *commit,
440 const struct got_error *err = NULL;
441 struct got_parent_id *pid;
443 pid = calloc(1, sizeof(*pid));
445 return got_error_from_errno();
447 pid->id = calloc(1, sizeof(*pid->id));
448 if (pid->id == NULL) {
449 err = got_error_from_errno();
454 if (!got_parse_sha1_digest(pid->id->sha1, id_str)) {
455 err = got_error(GOT_ERR_BAD_OBJ_DATA);
461 SIMPLEQ_INSERT_TAIL(&commit->parent_ids, pid, entry);
467 static const struct got_error *
468 parse_commit_object(struct got_commit_object **commit, char *buf, size_t len)
470 const struct got_error *err = NULL;
473 ssize_t remain = (ssize_t)len;
475 *commit = got_object_commit_alloc_partial();
477 return got_error_from_errno();
479 tlen = strlen(GOT_COMMIT_TAG_TREE);
480 if (strncmp(s, GOT_COMMIT_TAG_TREE, tlen) == 0) {
482 if (remain < SHA1_DIGEST_STRING_LENGTH) {
483 err = got_error(GOT_ERR_BAD_OBJ_DATA);
487 if (!got_parse_sha1_digest((*commit)->tree_id->sha1, s)) {
488 err = got_error(GOT_ERR_BAD_OBJ_DATA);
491 remain -= SHA1_DIGEST_STRING_LENGTH;
492 s += SHA1_DIGEST_STRING_LENGTH;
494 err = got_error(GOT_ERR_BAD_OBJ_DATA);
498 tlen = strlen(GOT_COMMIT_TAG_PARENT);
499 while (strncmp(s, GOT_COMMIT_TAG_PARENT, tlen) == 0) {
501 if (remain < SHA1_DIGEST_STRING_LENGTH) {
502 err = got_error(GOT_ERR_BAD_OBJ_DATA);
506 err = got_object_commit_add_parent(*commit, s);
510 remain -= SHA1_DIGEST_STRING_LENGTH;
511 s += SHA1_DIGEST_STRING_LENGTH;
514 tlen = strlen(GOT_COMMIT_TAG_AUTHOR);
515 if (strncmp(s, GOT_COMMIT_TAG_AUTHOR, tlen) == 0) {
520 err = got_error(GOT_ERR_BAD_OBJ_DATA);
526 err = got_error(GOT_ERR_BAD_OBJ_DATA);
530 (*commit)->author = strdup(s);
531 if ((*commit)->author == NULL) {
532 err = got_error_from_errno();
535 s += strlen((*commit)->author) + 1;
536 remain -= strlen((*commit)->author) + 1;
539 tlen = strlen(GOT_COMMIT_TAG_COMMITTER);
540 if (strncmp(s, GOT_COMMIT_TAG_COMMITTER, tlen) == 0) {
545 err = got_error(GOT_ERR_BAD_OBJ_DATA);
551 err = got_error(GOT_ERR_BAD_OBJ_DATA);
555 (*commit)->committer = strdup(s);
556 if ((*commit)->committer == NULL) {
557 err = got_error_from_errno();
560 s += strlen((*commit)->committer) + 1;
561 remain -= strlen((*commit)->committer) + 1;
564 (*commit)->logmsg = strndup(s, remain);
565 if ((*commit)->logmsg == NULL) {
566 err = got_error_from_errno();
571 got_object_commit_close(*commit);
578 tree_entry_close(struct got_tree_entry *te)
585 struct got_tree_entry *
586 got_alloc_tree_entry_partial(void)
588 struct got_tree_entry *te;
590 te = calloc(1, sizeof(*te));
594 te->id = calloc(1, sizeof(*te->id));
595 if (te->id == NULL) {
602 static const struct got_error *
603 parse_tree_entry(struct got_tree_entry **te, size_t *elen, char *buf,
606 char *p = buf, *space;
607 const struct got_error *err = NULL;
609 *te = got_alloc_tree_entry_partial();
611 return got_error_from_errno();
613 *elen = strlen(buf) + 1;
614 if (*elen > maxlen) {
617 return got_error(GOT_ERR_BAD_OBJ_DATA);
620 space = strchr(buf, ' ');
622 err = got_error(GOT_ERR_BAD_OBJ_DATA);
628 if (*p < '0' && *p > '7') {
629 err = got_error(GOT_ERR_BAD_OBJ_DATA);
633 (*te)->mode |= *p - '0';
637 (*te)->name = strdup(space + 1);
638 if (*elen > maxlen || maxlen - *elen < SHA1_DIGEST_LENGTH) {
639 err = got_error(GOT_ERR_BAD_OBJ_DATA);
642 buf += strlen(buf) + 1;
643 memcpy((*te)->id->sha1, buf, SHA1_DIGEST_LENGTH);
644 *elen += SHA1_DIGEST_LENGTH;
647 tree_entry_close(*te);
653 static const struct got_error *
654 parse_tree_object(struct got_tree_object **tree, uint8_t *buf, size_t len)
656 const struct got_error *err;
659 *tree = calloc(1, sizeof(**tree));
661 return got_error_from_errno();
663 SIMPLEQ_INIT(&(*tree)->entries);
666 struct got_tree_entry *te;
669 err = parse_tree_entry(&te, &elen, buf, remain);
673 SIMPLEQ_INSERT_TAIL(&(*tree)->entries, te, entry);
679 got_object_tree_close(*tree);
680 return got_error(GOT_ERR_BAD_OBJ_DATA);
686 static const struct got_error *
687 read_to_mem(uint8_t **outbuf, size_t *outlen, FILE *f)
689 const struct got_error *err = NULL;
690 static const size_t blocksize = 512;
691 size_t n, total, remain;
697 buf = calloc(1, blocksize);
699 return got_error_from_errno();
706 newbuf = reallocarray(buf, 1, total + blocksize);
707 if (newbuf == NULL) {
708 err = got_error_from_errno();
714 n = fread(buf + total, 1, remain, f);
717 err = got_ferror(f, GOT_ERR_IO);
735 static const struct got_error *
736 read_commit_object(struct got_commit_object **commit, struct got_object *obj,
739 const struct got_error *err = NULL;
743 if (obj->flags & GOT_OBJ_FLAG_PACKED)
744 err = read_to_mem(&p, &len, f);
746 err = got_inflate_to_mem(&p, &len, f);
750 if (len < obj->hdrlen + obj->size) {
751 err = got_error(GOT_ERR_BAD_OBJ_DATA);
755 /* Skip object header. */
757 err = parse_commit_object(commit, p + obj->hdrlen, len);
764 read_commit_object_privsep_child(struct got_object *obj, int obj_fd,
767 const struct got_error *err = NULL;
768 struct got_commit_object *commit = NULL;
773 setproctitle("read commit object");
775 imsg_init(&ibuf, imsg_fds[1]);
777 /* revoke access to most system calls */
778 if (pledge("stdio", NULL) == -1) {
779 err = got_error_from_errno();
783 f = fdopen(obj_fd, "rb");
785 err = got_error_from_errno();
790 err = read_commit_object(&commit, obj, f);
794 err = got_privsep_send_commit(&ibuf, commit);
797 got_object_commit_close(commit);
799 got_privsep_send_error(&ibuf, err);
809 static const struct got_error *
810 read_commit_object_privsep(struct got_commit_object **commit,
811 struct got_repository *repo, struct got_object *obj, int fd)
813 const struct got_error *err = NULL, *err_child = NULL;
814 struct imsgbuf parent_ibuf;
818 if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_fds) == -1)
819 return got_error_from_errno();
823 return got_error_from_errno();
825 read_commit_object_privsep_child(obj, fd, imsg_fds);
830 imsg_init(&parent_ibuf, imsg_fds[0]);
831 err = got_privsep_recv_commit(commit, &parent_ibuf);
832 imsg_clear(&parent_ibuf);
833 err_child = wait_for_child(pid);
835 return err ? err : err_child;
838 const struct got_error *
839 got_object_commit_open(struct got_commit_object **commit,
840 struct got_repository *repo, struct got_object *obj)
842 const struct got_error *err = NULL;
844 if (obj->type != GOT_OBJ_TYPE_COMMIT)
845 return got_error(GOT_ERR_OBJ_TYPE);
847 if (obj->flags & GOT_OBJ_FLAG_PACKED) {
850 err = got_packfile_extract_object_to_mem(&buf, &len, obj, repo);
854 err = parse_commit_object(commit, buf, len);
858 err = open_loose_object(&fd, obj, repo);
861 err = read_commit_object_privsep(commit, repo, obj, fd);
868 got_object_commit_close(struct got_commit_object *commit)
870 struct got_parent_id *pid;
872 while (!SIMPLEQ_EMPTY(&commit->parent_ids)) {
873 pid = SIMPLEQ_FIRST(&commit->parent_ids);
874 SIMPLEQ_REMOVE_HEAD(&commit->parent_ids, entry);
879 free(commit->tree_id);
880 free(commit->author);
881 free(commit->committer);
882 free(commit->logmsg);
886 static const struct got_error *
887 read_tree_object(struct got_tree_object **tree, struct got_object *obj, FILE *f)
889 const struct got_error *err = NULL;
893 if (obj->flags & GOT_OBJ_FLAG_PACKED)
894 err = read_to_mem(&p, &len, f);
896 err = got_inflate_to_mem(&p, &len, f);
900 if (len < obj->hdrlen + obj->size) {
901 err = got_error(GOT_ERR_BAD_OBJ_DATA);
905 /* Skip object header. */
907 err = parse_tree_object(tree, p + obj->hdrlen, len);
914 read_tree_object_privsep_child(struct got_object *obj, int obj_fd,
917 const struct got_error *err = NULL;
918 struct got_tree_object *tree = NULL;
923 setproctitle("read tree object");
925 imsg_init(&ibuf, imsg_fds[1]);
927 /* revoke access to most system calls */
928 if (pledge("stdio", NULL) == -1) {
929 err = got_error_from_errno();
933 f = fdopen(obj_fd, "rb");
935 err = got_error_from_errno();
940 err = read_tree_object(&tree, obj, f);
944 err = got_privsep_send_tree(&ibuf, tree);
947 got_object_tree_close(tree);
949 got_privsep_send_error(&ibuf, err);
959 static const struct got_error *
960 read_tree_object_privsep(struct got_tree_object **tree, struct got_object *obj,
963 const struct got_error *err = NULL, *err_child = NULL;
964 struct imsgbuf parent_ibuf;
968 if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_fds) == -1)
969 return got_error_from_errno();
973 return got_error_from_errno();
975 read_tree_object_privsep_child(obj, fd, imsg_fds);
980 imsg_init(&parent_ibuf, imsg_fds[0]);
981 err = got_privsep_recv_tree(tree, &parent_ibuf);
982 imsg_clear(&parent_ibuf);
983 err_child = wait_for_child(pid);
985 return err ? err : err_child;
988 const struct got_error *
989 got_object_tree_open(struct got_tree_object **tree,
990 struct got_repository *repo, struct got_object *obj)
992 const struct got_error *err = NULL;
994 if (obj->type != GOT_OBJ_TYPE_TREE)
995 return got_error(GOT_ERR_OBJ_TYPE);
997 if (obj->flags & GOT_OBJ_FLAG_PACKED) {
1000 err = got_packfile_extract_object_to_mem(&buf, &len, obj, repo);
1004 err = parse_tree_object(tree, buf, len);
1008 err = open_loose_object(&fd, obj, repo);
1011 err = read_tree_object_privsep(tree, obj, fd);
1018 got_object_tree_close(struct got_tree_object *tree)
1020 struct got_tree_entry *te;
1022 while (!SIMPLEQ_EMPTY(&tree->entries)) {
1023 te = SIMPLEQ_FIRST(&tree->entries);
1024 SIMPLEQ_REMOVE_HEAD(&tree->entries, entry);
1025 tree_entry_close(te);
1031 static const struct got_error *
1032 read_blob_object_privsep_child(int outfd, int infd, int imsg_fds[2])
1034 const struct got_error *err = NULL;
1035 struct imsgbuf ibuf;
1038 FILE *infile = NULL;
1040 setproctitle("read blob object");
1042 imsg_init(&ibuf, imsg_fds[1]);
1044 /* revoke access to most system calls */
1045 if (pledge("stdio", NULL) == -1) {
1046 err = got_error_from_errno();
1050 infile = fdopen(infd, "rb");
1051 if (infile == NULL) {
1052 err = got_error_from_errno();
1056 err = got_inflate_to_fd(&size, infile, outfd);
1061 err = got_privsep_send_blob(&ibuf, size);
1064 got_privsep_send_error(&ibuf, err);
1073 static const struct got_error *
1074 read_blob_object_privsep(size_t *size, int outfd, int infd)
1076 struct imsgbuf parent_ibuf;
1078 const struct got_error *err = NULL, *err_child = NULL;
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_blob_object_privsep_child(outfd, infd, imsg_fds);
1093 imsg_init(&parent_ibuf, imsg_fds[0]);
1094 err = got_privsep_recv_blob(size, &parent_ibuf);
1095 imsg_clear(&parent_ibuf);
1096 err_child = wait_for_child(pid);
1098 if (lseek(outfd, SEEK_SET, 0) == -1)
1099 err = got_error_from_errno();
1100 return err ? err : err_child;
1103 const struct got_error *
1104 got_object_blob_open(struct got_blob_object **blob,
1105 struct got_repository *repo, struct got_object *obj, size_t blocksize)
1107 const struct got_error *err = NULL;
1109 if (obj->type != GOT_OBJ_TYPE_BLOB)
1110 return got_error(GOT_ERR_OBJ_TYPE);
1112 if (blocksize < obj->hdrlen)
1113 return got_error(GOT_ERR_NO_SPACE);
1115 *blob = calloc(1, sizeof(**blob));
1117 return got_error_from_errno();
1119 (*blob)->read_buf = calloc(1, blocksize);
1120 if ((*blob)->read_buf == NULL) {
1121 err = got_error_from_errno();
1124 if (obj->flags & GOT_OBJ_FLAG_PACKED) {
1125 err = got_packfile_extract_object(&((*blob)->f), obj, repo);
1133 err = open_loose_object(&infd, obj, repo);
1138 outfd = got_opentempfd();
1140 err = got_error_from_errno();
1145 err = read_blob_object_privsep(&size, outfd, infd);
1150 if (size != obj->hdrlen + obj->size) {
1151 err = got_error(GOT_ERR_PRIVSEP_LEN);
1156 if (fstat(outfd, &sb) == -1) {
1157 err = got_error_from_errno();
1162 if (sb.st_size != size) {
1163 err = got_error(GOT_ERR_PRIVSEP_LEN);
1168 (*blob)->f = fdopen(outfd, "rb");
1169 if ((*blob)->f == NULL) {
1170 err = got_error_from_errno();
1176 (*blob)->hdrlen = obj->hdrlen;
1177 (*blob)->blocksize = blocksize;
1178 memcpy(&(*blob)->id.sha1, obj->id.sha1, SHA1_DIGEST_LENGTH);
1184 free((*blob)->read_buf);
1192 got_object_blob_close(struct got_blob_object *blob)
1194 free(blob->read_buf);
1200 got_object_blob_id_str(struct got_blob_object *blob, char *buf, size_t size)
1202 return got_sha1_digest_to_str(blob->id.sha1, buf, size);
1206 got_object_blob_get_hdrlen(struct got_blob_object *blob)
1208 return blob->hdrlen;
1212 got_object_blob_get_read_buf(struct got_blob_object *blob)
1214 return blob->read_buf;
1217 const struct got_error *
1218 got_object_blob_read_block(size_t *outlenp, struct got_blob_object *blob)
1222 n = fread(blob->read_buf, 1, blob->blocksize, blob->f);
1223 if (n == 0 && ferror(blob->f))
1224 return got_ferror(blob->f, GOT_ERR_IO);