Blob


1 /*
2 * Copyright (c) 2018 Stefan Sperling <stsp@openbsd.org>
3 *
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.
7 *
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.
15 */
17 #include <sys/types.h>
18 #include <sys/stat.h>
19 #include <sys/queue.h>
20 #include <sys/uio.h>
21 #include <sys/socket.h>
22 #include <sys/wait.h>
24 #include <errno.h>
25 #include <fcntl.h>
26 #include <stdio.h>
27 #include <stdlib.h>
28 #include <string.h>
29 #include <stdint.h>
30 #include <sha1.h>
31 #include <zlib.h>
32 #include <ctype.h>
33 #include <limits.h>
34 #include <imsg.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"
48 #ifndef MIN
49 #define MIN(_a,_b) ((_a) < (_b) ? (_a) : (_b))
50 #endif
52 #ifndef nitems
53 #define nitems(_a) (sizeof(_a) / sizeof((_a)[0]))
54 #endif
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)
67 {
68 static const size_t len = SHA1_DIGEST_STRING_LENGTH;
70 *outbuf = calloc(1, len);
71 if (*outbuf == NULL)
72 return got_error_from_errno();
74 if (got_sha1_digest_to_str(id->sha1, *outbuf, len) == NULL) {
75 free(*outbuf);
76 *outbuf = NULL;
77 return got_error(GOT_ERR_BAD_OBJ_ID_STR);
78 }
80 return NULL;
81 }
83 int
84 got_object_id_cmp(struct got_object_id *id1, struct got_object_id *id2)
85 {
86 return memcmp(id1->sha1, id2->sha1, SHA1_DIGEST_LENGTH);
87 }
89 struct got_object_id *
90 got_object_id_dup(struct got_object_id *id1)
91 {
92 struct got_object_id *id2;
94 id2 = malloc(sizeof(*id2));
95 if (id2 == NULL)
96 return NULL;
97 memcpy(id2, id1, sizeof(*id2));
98 return id2;
99 }
101 struct got_object_id *
102 got_object_get_id(struct got_object *obj)
104 return got_object_id_dup(&obj->id);
107 int
108 got_object_get_type(struct got_object *obj)
110 switch (obj->type) {
111 case GOT_OBJ_TYPE_COMMIT:
112 case GOT_OBJ_TYPE_TREE:
113 case GOT_OBJ_TYPE_BLOB:
114 case GOT_OBJ_TYPE_TAG:
115 return obj->type;
116 default:
117 abort();
118 break;
121 /* not reached */
122 return 0;
125 static const struct got_error *
126 parse_object_header(struct got_object **obj, char *buf, size_t len)
128 const char *obj_tags[] = {
129 GOT_OBJ_TAG_COMMIT,
130 GOT_OBJ_TAG_TREE,
131 GOT_OBJ_TAG_BLOB
132 };
133 const int obj_types[] = {
134 GOT_OBJ_TYPE_COMMIT,
135 GOT_OBJ_TYPE_TREE,
136 GOT_OBJ_TYPE_BLOB,
137 };
138 int type = 0;
139 size_t size = 0, hdrlen = 0;
140 int i;
141 char *p = strchr(buf, '\0');
143 if (p == NULL)
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);
151 const char *errstr;
153 if (strncmp(buf, tag, tlen) != 0)
154 continue;
156 type = obj_types[i];
157 if (len <= tlen)
158 return got_error(GOT_ERR_BAD_OBJ_HDR);
159 size = strtonum(buf + tlen, 0, LONG_MAX, &errstr);
160 if (errstr != NULL)
161 return got_error(GOT_ERR_BAD_OBJ_HDR);
162 break;
165 if (type == 0)
166 return got_error(GOT_ERR_BAD_OBJ_HDR);
168 *obj = calloc(1, sizeof(**obj));
169 if (*obj == NULL)
170 return got_error_from_errno();
171 (*obj)->type = type;
172 (*obj)->hdrlen = hdrlen;
173 (*obj)->size = size;
174 return NULL;
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;
182 char *buf;
183 const size_t zbsize = 64;
184 size_t outlen, totlen;
185 int i;
187 buf = calloc(zbsize, sizeof(char));
188 if (buf == NULL)
189 return got_error_from_errno();
191 err = got_inflate_init(&zb, NULL, zbsize);
192 if (err)
193 return err;
195 i = 0;
196 totlen = 0;
197 do {
198 err = got_inflate_read(&zb, f, &outlen);
199 if (err)
200 goto done;
201 if (strchr(zb.outbuf, '\0') == NULL) {
202 buf = recallocarray(buf, 1 + i, 2 + i, zbsize);
203 if (buf == NULL) {
204 err = got_error_from_errno();
205 goto done;
208 memcpy(buf + totlen, zb.outbuf, outlen);
209 totlen += outlen;
210 i++;
211 } while (strchr(zb.outbuf, '\0') == NULL);
213 err = parse_object_header(obj, buf, totlen);
214 done:
215 got_inflate_end(&zb);
216 return err;
219 static const struct got_error *
220 read_object_header_privsep(struct got_object **obj, int fd)
222 struct imsgbuf parent_ibuf;
223 int imsg_fds[2];
224 const struct got_error *err = NULL;
225 pid_t pid;
226 int child_status;
228 if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_fds) == -1)
229 return got_error_from_errno();
231 pid = fork();
232 if (pid == -1)
233 return got_error_from_errno();
234 else if (pid == 0) {
235 struct got_object *child_obj = NULL;
236 struct imsgbuf child_ibuf;
237 FILE *f = NULL;
238 int status = 0;
240 setproctitle("got: read object header");
241 close(imsg_fds[0]);
242 imsg_init(&child_ibuf, imsg_fds[1]);
243 if (err)
244 goto done;
246 /* revoke access to most system calls */
247 if (pledge("stdio", NULL) == -1) {
248 err = got_error_from_errno();
249 goto done;
252 f = fdopen(fd, "rb");
253 if (f == NULL) {
254 err = got_error_from_errno();
255 goto done;
258 err = read_object_header(&child_obj, f);
259 if (err)
260 goto done;
262 err = got_privsep_send_obj(&child_ibuf, child_obj, 0);
263 done:
264 if (child_obj)
265 got_object_close(child_obj);
266 if (err) {
267 got_privsep_send_error(&child_ibuf, err);
268 status = 1;
270 if (f)
271 fclose(f);
272 imsg_clear(&child_ibuf);
273 _exit(status);
276 close(imsg_fds[1]);
277 imsg_init(&parent_ibuf, imsg_fds[0]);
278 err = got_privsep_recv_obj(obj, &parent_ibuf);
279 imsg_clear(&parent_ibuf);
280 waitpid(pid, &child_status, 0);
281 close(imsg_fds[0]);
282 return err;
285 static const struct got_error *
286 object_path(char **path, struct got_object_id *id, struct got_repository *repo)
288 const struct got_error *err = NULL;
289 char *hex;
290 char *path_objects = got_repo_get_path_objects(repo);
292 *path = NULL;
294 if (path_objects == NULL)
295 return got_error_from_errno();
297 err = got_object_id_str(&hex, id);
298 if (err)
299 return err;
301 if (asprintf(path, "%s/%.2x/%s", path_objects,
302 id->sha1[0], hex + 2) == -1)
303 err = got_error_from_errno();
305 free(hex);
306 free(path_objects);
307 return err;
310 static const struct got_error *
311 open_loose_object(FILE **f, struct got_object *obj, struct got_repository *repo)
313 const struct got_error *err = NULL;
314 char *path;
316 err = object_path(&path, &obj->id, repo);
317 if (err)
318 return err;
319 *f = fopen(path, "rb");
320 if (*f == NULL) {
321 err = got_error_from_errno();
322 goto done;
324 done:
325 free(path);
326 return err;
329 const struct got_error *
330 got_object_open(struct got_object **obj, struct got_repository *repo,
331 struct got_object_id *id)
333 const struct got_error *err = NULL;
334 char *path;
335 int fd;
337 err = object_path(&path, id, repo);
338 if (err)
339 return err;
341 fd = open(path, O_RDONLY | O_NOFOLLOW, GOT_DEFAULT_FILE_MODE);
342 if (fd == -1) {
343 if (errno != ENOENT) {
344 err = got_error_from_errno();
345 goto done;
347 err = got_packfile_open_object(obj, id, repo);
348 if (err)
349 goto done;
350 if (*obj == NULL)
351 err = got_error(GOT_ERR_NO_OBJ);
352 } else {
353 err = read_object_header_privsep(obj, fd);
354 if (err)
355 goto done;
356 memcpy((*obj)->id.sha1, id->sha1, SHA1_DIGEST_LENGTH);
358 done:
359 free(path);
360 if (fd != -1)
361 close(fd);
362 return err;
366 const struct got_error *
367 got_object_open_by_id_str(struct got_object **obj, struct got_repository *repo,
368 const char *id_str)
370 struct got_object_id id;
372 if (!got_parse_sha1_digest(id.sha1, id_str))
373 return got_error(GOT_ERR_BAD_OBJ_ID_STR);
375 return got_object_open(obj, repo, &id);
378 void
379 got_object_close(struct got_object *obj)
381 if (obj->flags & GOT_OBJ_FLAG_DELTIFIED) {
382 struct got_delta *delta;
383 while (!SIMPLEQ_EMPTY(&obj->deltas.entries)) {
384 delta = SIMPLEQ_FIRST(&obj->deltas.entries);
385 SIMPLEQ_REMOVE_HEAD(&obj->deltas.entries, entry);
386 got_delta_close(delta);
389 if (obj->flags & GOT_OBJ_FLAG_PACKED)
390 free(obj->path_packfile);
391 free(obj);
394 static const struct got_error *
395 parse_commit_object(struct got_commit_object **commit, char *buf, size_t len)
397 const struct got_error *err = NULL;
398 char *s = buf;
399 size_t tlen;
400 ssize_t remain = (ssize_t)len;
402 *commit = calloc(1, sizeof(**commit));
403 if (*commit == NULL)
404 return got_error_from_errno();
405 (*commit)->tree_id = calloc(1, sizeof(*(*commit)->tree_id));
406 if ((*commit)->tree_id == NULL) {
407 err = got_error_from_errno();
408 free(*commit);
409 *commit = NULL;
410 return err;
413 SIMPLEQ_INIT(&(*commit)->parent_ids);
415 tlen = strlen(GOT_COMMIT_TAG_TREE);
416 if (strncmp(s, GOT_COMMIT_TAG_TREE, tlen) == 0) {
417 remain -= tlen;
418 if (remain < SHA1_DIGEST_STRING_LENGTH) {
419 err = got_error(GOT_ERR_BAD_OBJ_DATA);
420 goto done;
422 s += tlen;
423 if (!got_parse_sha1_digest((*commit)->tree_id->sha1, s)) {
424 err = got_error(GOT_ERR_BAD_OBJ_DATA);
425 goto done;
427 remain -= SHA1_DIGEST_STRING_LENGTH;
428 s += SHA1_DIGEST_STRING_LENGTH;
429 } else {
430 err = got_error(GOT_ERR_BAD_OBJ_DATA);
431 goto done;
434 tlen = strlen(GOT_COMMIT_TAG_PARENT);
435 while (strncmp(s, GOT_COMMIT_TAG_PARENT, tlen) == 0) {
436 struct got_parent_id *pid;
438 remain -= tlen;
439 if (remain < SHA1_DIGEST_STRING_LENGTH) {
440 err = got_error(GOT_ERR_BAD_OBJ_DATA);
441 goto done;
444 pid = calloc(1, sizeof(*pid));
445 if (pid == NULL) {
446 err = got_error_from_errno();
447 goto done;
449 pid->id = calloc(1, sizeof(*pid->id));
450 if (pid->id == NULL) {
451 err = got_error_from_errno();
452 free(pid);
453 goto done;
455 s += tlen;
456 if (!got_parse_sha1_digest(pid->id->sha1, s)) {
457 err = got_error(GOT_ERR_BAD_OBJ_DATA);
458 free(pid->id);
459 free(pid);
460 goto done;
462 SIMPLEQ_INSERT_TAIL(&(*commit)->parent_ids, pid, entry);
463 (*commit)->nparents++;
465 remain -= SHA1_DIGEST_STRING_LENGTH;
466 s += SHA1_DIGEST_STRING_LENGTH;
469 tlen = strlen(GOT_COMMIT_TAG_AUTHOR);
470 if (strncmp(s, GOT_COMMIT_TAG_AUTHOR, tlen) == 0) {
471 char *p;
473 remain -= tlen;
474 if (remain <= 0) {
475 err = got_error(GOT_ERR_BAD_OBJ_DATA);
476 goto done;
478 s += tlen;
479 p = strchr(s, '\n');
480 if (p == NULL) {
481 err = got_error(GOT_ERR_BAD_OBJ_DATA);
482 goto done;
484 *p = '\0';
485 (*commit)->author = strdup(s);
486 if ((*commit)->author == NULL) {
487 err = got_error_from_errno();
488 goto done;
490 s += strlen((*commit)->author) + 1;
491 remain -= strlen((*commit)->author) + 1;
494 tlen = strlen(GOT_COMMIT_TAG_COMMITTER);
495 if (strncmp(s, GOT_COMMIT_TAG_COMMITTER, tlen) == 0) {
496 char *p;
498 remain -= tlen;
499 if (remain <= 0) {
500 err = got_error(GOT_ERR_BAD_OBJ_DATA);
501 goto done;
503 s += tlen;
504 p = strchr(s, '\n');
505 if (p == NULL) {
506 err = got_error(GOT_ERR_BAD_OBJ_DATA);
507 goto done;
509 *p = '\0';
510 (*commit)->committer = strdup(s);
511 if ((*commit)->committer == NULL) {
512 err = got_error_from_errno();
513 goto done;
515 s += strlen((*commit)->committer) + 1;
516 remain -= strlen((*commit)->committer) + 1;
519 (*commit)->logmsg = strndup(s, remain);
520 if ((*commit)->logmsg == NULL) {
521 err = got_error_from_errno();
522 goto done;
524 done:
525 if (err) {
526 got_object_commit_close(*commit);
527 *commit = NULL;
529 return err;
532 static void
533 tree_entry_close(struct got_tree_entry *te)
535 free(te->id);
536 free(te->name);
537 free(te);
540 static const struct got_error *
541 parse_tree_entry(struct got_tree_entry **te, size_t *elen, char *buf,
542 size_t maxlen)
544 char *p = buf, *space;
545 const struct got_error *err = NULL;
547 *te = calloc(1, sizeof(**te));
548 if (*te == NULL)
549 return got_error_from_errno();
551 (*te)->id = calloc(1, sizeof(*(*te)->id));
552 if ((*te)->id == NULL) {
553 err = got_error_from_errno();
554 free(*te);
555 *te = NULL;
556 return err;
559 *elen = strlen(buf) + 1;
560 if (*elen > maxlen) {
561 free(*te);
562 *te = NULL;
563 return got_error(GOT_ERR_BAD_OBJ_DATA);
566 space = strchr(buf, ' ');
567 if (space == NULL) {
568 err = got_error(GOT_ERR_BAD_OBJ_DATA);
569 free(*te);
570 *te = NULL;
571 return err;
573 while (*p != ' ') {
574 if (*p < '0' && *p > '7') {
575 err = got_error(GOT_ERR_BAD_OBJ_DATA);
576 goto done;
578 (*te)->mode <<= 3;
579 (*te)->mode |= *p - '0';
580 p++;
583 (*te)->name = strdup(space + 1);
584 if (*elen > maxlen || maxlen - *elen < SHA1_DIGEST_LENGTH) {
585 err = got_error(GOT_ERR_BAD_OBJ_DATA);
586 goto done;
588 buf += strlen(buf) + 1;
589 memcpy((*te)->id->sha1, buf, SHA1_DIGEST_LENGTH);
590 *elen += SHA1_DIGEST_LENGTH;
591 done:
592 if (err) {
593 tree_entry_close(*te);
594 *te = NULL;
596 return err;
599 static const struct got_error *
600 parse_tree_object(struct got_tree_object **tree, struct got_repository *repo,
601 uint8_t *buf, size_t len)
603 const struct got_error *err;
604 size_t remain = len;
606 *tree = calloc(1, sizeof(**tree));
607 if (*tree == NULL)
608 return got_error_from_errno();
610 SIMPLEQ_INIT(&(*tree)->entries);
612 while (remain > 0) {
613 struct got_tree_entry *te;
614 size_t elen;
616 err = parse_tree_entry(&te, &elen, buf, remain);
617 if (err)
618 return err;
619 (*tree)->nentries++;
620 SIMPLEQ_INSERT_TAIL(&(*tree)->entries, te, entry);
621 buf += elen;
622 remain -= elen;
625 if (remain != 0) {
626 got_object_tree_close(*tree);
627 return got_error(GOT_ERR_BAD_OBJ_DATA);
630 return NULL;
633 static const struct got_error *
634 read_to_mem(uint8_t **outbuf, size_t *outlen, FILE *f)
636 const struct got_error *err = NULL;
637 static const size_t blocksize = 512;
638 size_t n, total, remain;
639 uint8_t *buf;
641 *outbuf = NULL;
642 *outlen = 0;
644 buf = calloc(1, blocksize);
645 if (buf == NULL)
646 return got_error_from_errno();
648 remain = blocksize;
649 total = 0;
650 while (1) {
651 if (remain == 0) {
652 uint8_t *newbuf;
653 newbuf = reallocarray(buf, 1, total + blocksize);
654 if (newbuf == NULL) {
655 err = got_error_from_errno();
656 goto done;
658 buf = newbuf;
659 remain += blocksize;
661 n = fread(buf + total, 1, remain, f);
662 if (n == 0) {
663 if (ferror(f)) {
664 err = got_ferror(f, GOT_ERR_IO);
665 goto done;
667 break; /* EOF */
669 remain -= n;
670 total += n;
671 };
673 done:
674 if (err == NULL) {
675 *outbuf = buf;
676 *outlen = total;
677 } else
678 free(buf);
679 return err;
682 static const struct got_error *
683 read_commit_object(struct got_commit_object **commit,
684 struct got_repository *repo, struct got_object *obj, FILE *f)
686 const struct got_error *err = NULL;
687 size_t len;
688 uint8_t *p;
690 if (obj->flags & GOT_OBJ_FLAG_PACKED)
691 err = read_to_mem(&p, &len, f);
692 else
693 err = got_inflate_to_mem(&p, &len, f);
694 if (err)
695 return err;
697 if (len < obj->hdrlen + obj->size) {
698 err = got_error(GOT_ERR_BAD_OBJ_DATA);
699 goto done;
702 /* Skip object header. */
703 len -= obj->hdrlen;
704 err = parse_commit_object(commit, p + obj->hdrlen, len);
705 free(p);
706 done:
707 return err;
710 const struct got_error *
711 got_object_commit_open(struct got_commit_object **commit,
712 struct got_repository *repo, struct got_object *obj)
714 const struct got_error *err = NULL;
716 if (obj->type != GOT_OBJ_TYPE_COMMIT)
717 return got_error(GOT_ERR_OBJ_TYPE);
719 if (obj->flags & GOT_OBJ_FLAG_PACKED) {
720 uint8_t *buf;
721 size_t len;
722 err = got_packfile_extract_object_to_mem(&buf, &len, obj, repo);
723 if (err)
724 return err;
725 obj->size = len;
726 err = parse_commit_object(commit, buf, len);
727 free(buf);
728 } else {
729 FILE *f;
730 err = open_loose_object(&f, obj, repo);
731 if (err)
732 return err;
733 err = read_commit_object(commit, repo, obj, f);
734 fclose(f);
736 return err;
739 void
740 got_object_commit_close(struct got_commit_object *commit)
742 struct got_parent_id *pid;
744 while (!SIMPLEQ_EMPTY(&commit->parent_ids)) {
745 pid = SIMPLEQ_FIRST(&commit->parent_ids);
746 SIMPLEQ_REMOVE_HEAD(&commit->parent_ids, entry);
747 free(pid->id);
748 free(pid);
751 free(commit->tree_id);
752 free(commit->author);
753 free(commit->committer);
754 free(commit->logmsg);
755 free(commit);
758 static const struct got_error *
759 read_tree_object(struct got_tree_object **tree,
760 struct got_repository *repo, struct got_object *obj, FILE *f)
762 const struct got_error *err = NULL;
763 size_t len;
764 uint8_t *p;
766 if (obj->flags & GOT_OBJ_FLAG_PACKED)
767 err = read_to_mem(&p, &len, f);
768 else
769 err = got_inflate_to_mem(&p, &len, f);
770 if (err)
771 return err;
773 if (len < obj->hdrlen + obj->size) {
774 err = got_error(GOT_ERR_BAD_OBJ_DATA);
775 goto done;
778 /* Skip object header. */
779 len -= obj->hdrlen;
780 err = parse_tree_object(tree, repo, p + obj->hdrlen, len);
781 free(p);
782 done:
783 return err;
786 const struct got_error *
787 got_object_tree_open(struct got_tree_object **tree,
788 struct got_repository *repo, struct got_object *obj)
790 const struct got_error *err = NULL;
792 if (obj->type != GOT_OBJ_TYPE_TREE)
793 return got_error(GOT_ERR_OBJ_TYPE);
795 if (obj->flags & GOT_OBJ_FLAG_PACKED) {
796 uint8_t *buf;
797 size_t len;
798 err = got_packfile_extract_object_to_mem(&buf, &len, obj, repo);
799 if (err)
800 return err;
801 obj->size = len;
802 err = parse_tree_object(tree, repo, buf, len);
803 free(buf);
804 } else {
805 FILE *f;
806 err = open_loose_object(&f, obj, repo);
807 if (err)
808 return err;
809 err = read_tree_object(tree, repo, obj, f);
810 fclose(f);
812 return err;
815 void
816 got_object_tree_close(struct got_tree_object *tree)
818 struct got_tree_entry *te;
820 while (!SIMPLEQ_EMPTY(&tree->entries)) {
821 te = SIMPLEQ_FIRST(&tree->entries);
822 SIMPLEQ_REMOVE_HEAD(&tree->entries, entry);
823 tree_entry_close(te);
826 free(tree);
829 const struct got_error *
830 got_object_blob_open(struct got_blob_object **blob,
831 struct got_repository *repo, struct got_object *obj, size_t blocksize)
833 const struct got_error *err = NULL;
835 if (obj->type != GOT_OBJ_TYPE_BLOB)
836 return got_error(GOT_ERR_OBJ_TYPE);
838 if (blocksize < obj->hdrlen)
839 return got_error(GOT_ERR_NO_SPACE);
841 *blob = calloc(1, sizeof(**blob));
842 if (*blob == NULL)
843 return got_error_from_errno();
845 if (obj->flags & GOT_OBJ_FLAG_PACKED) {
846 (*blob)->read_buf = calloc(1, blocksize);
847 if ((*blob)->read_buf == NULL) {
848 err = got_error_from_errno();
849 free(*blob);
850 *blob = NULL;
851 return err;
853 err = got_packfile_extract_object(&((*blob)->f), obj, repo);
854 if (err) {
855 free((*blob)->read_buf);
856 free(*blob);
857 *blob = NULL;
858 return err;
860 } else {
861 err = open_loose_object(&((*blob)->f), obj, repo);
862 if (err) {
863 free(*blob);
864 *blob = NULL;
865 return err;
868 err = got_inflate_init(&(*blob)->zb, NULL, blocksize);
869 if (err != NULL) {
870 fclose((*blob)->f);
871 free(*blob);
872 *blob = NULL;
873 return err;
876 (*blob)->read_buf = (*blob)->zb.outbuf;
877 (*blob)->flags |= GOT_BLOB_F_COMPRESSED;
880 (*blob)->hdrlen = obj->hdrlen;
881 (*blob)->blocksize = blocksize;
882 memcpy(&(*blob)->id.sha1, obj->id.sha1, SHA1_DIGEST_LENGTH);
884 return err;
887 void
888 got_object_blob_close(struct got_blob_object *blob)
890 if (blob->flags & GOT_BLOB_F_COMPRESSED)
891 got_inflate_end(&blob->zb);
892 else
893 free(blob->read_buf);
894 fclose(blob->f);
895 free(blob);
898 char *
899 got_object_blob_id_str(struct got_blob_object *blob, char *buf, size_t size)
901 return got_sha1_digest_to_str(blob->id.sha1, buf, size);
904 size_t
905 got_object_blob_get_hdrlen(struct got_blob_object *blob)
907 return blob->hdrlen;
910 const uint8_t *
911 got_object_blob_get_read_buf(struct got_blob_object *blob)
913 return blob->read_buf;
916 const struct got_error *
917 got_object_blob_read_block(size_t *outlenp, struct got_blob_object *blob)
919 size_t n;
921 if (blob->flags & GOT_BLOB_F_COMPRESSED)
922 return got_inflate_read(&blob->zb, blob->f, outlenp);
924 n = fread(blob->read_buf, 1, blob->blocksize, blob->f);
925 if (n == 0 && ferror(blob->f))
926 return got_ferror(blob->f, GOT_ERR_IO);
927 *outlenp = n;
928 return NULL;