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 close(fd);
256 goto done;
259 err = read_object_header(&child_obj, f);
260 if (err)
261 goto done;
263 err = got_privsep_send_obj(&child_ibuf, child_obj, 0);
264 done:
265 if (child_obj)
266 got_object_close(child_obj);
267 if (err) {
268 got_privsep_send_error(&child_ibuf, err);
269 status = 1;
271 if (f)
272 fclose(f);
273 imsg_clear(&child_ibuf);
274 close(imsg_fds[1]);
275 _exit(status);
278 close(imsg_fds[1]);
279 imsg_init(&parent_ibuf, imsg_fds[0]);
280 err = got_privsep_recv_obj(obj, &parent_ibuf);
281 imsg_clear(&parent_ibuf);
282 waitpid(pid, &child_status, 0);
283 close(imsg_fds[0]);
284 return err;
287 static const struct got_error *
288 object_path(char **path, struct got_object_id *id, struct got_repository *repo)
290 const struct got_error *err = NULL;
291 char *hex;
292 char *path_objects = got_repo_get_path_objects(repo);
294 *path = NULL;
296 if (path_objects == NULL)
297 return got_error_from_errno();
299 err = got_object_id_str(&hex, id);
300 if (err)
301 return err;
303 if (asprintf(path, "%s/%.2x/%s", path_objects,
304 id->sha1[0], hex + 2) == -1)
305 err = got_error_from_errno();
307 free(hex);
308 free(path_objects);
309 return err;
312 static const struct got_error *
313 open_loose_object(int *fd, struct got_object *obj, struct got_repository *repo)
315 const struct got_error *err = NULL;
316 char *path;
318 err = object_path(&path, &obj->id, repo);
319 if (err)
320 return err;
321 *fd = open(path, O_RDONLY | O_NOFOLLOW, GOT_DEFAULT_FILE_MODE);
322 if (*fd == -1) {
323 err = got_error_from_errno();
324 goto done;
326 done:
327 free(path);
328 return err;
331 const struct got_error *
332 got_object_open(struct got_object **obj, struct got_repository *repo,
333 struct got_object_id *id)
335 const struct got_error *err = NULL;
336 char *path;
337 int fd;
339 err = object_path(&path, id, repo);
340 if (err)
341 return err;
343 fd = open(path, O_RDONLY | O_NOFOLLOW, GOT_DEFAULT_FILE_MODE);
344 if (fd == -1) {
345 if (errno != ENOENT) {
346 err = got_error_from_errno();
347 goto done;
349 err = got_packfile_open_object(obj, id, repo);
350 if (err)
351 goto done;
352 if (*obj == NULL)
353 err = got_error(GOT_ERR_NO_OBJ);
354 } else {
355 err = read_object_header_privsep(obj, fd);
356 if (err)
357 goto done;
358 memcpy((*obj)->id.sha1, id->sha1, SHA1_DIGEST_LENGTH);
360 done:
361 free(path);
362 if (fd != -1)
363 close(fd);
364 return err;
368 const struct got_error *
369 got_object_open_by_id_str(struct got_object **obj, struct got_repository *repo,
370 const char *id_str)
372 struct got_object_id id;
374 if (!got_parse_sha1_digest(id.sha1, id_str))
375 return got_error(GOT_ERR_BAD_OBJ_ID_STR);
377 return got_object_open(obj, repo, &id);
380 void
381 got_object_close(struct got_object *obj)
383 if (obj->flags & GOT_OBJ_FLAG_DELTIFIED) {
384 struct got_delta *delta;
385 while (!SIMPLEQ_EMPTY(&obj->deltas.entries)) {
386 delta = SIMPLEQ_FIRST(&obj->deltas.entries);
387 SIMPLEQ_REMOVE_HEAD(&obj->deltas.entries, entry);
388 got_delta_close(delta);
391 if (obj->flags & GOT_OBJ_FLAG_PACKED)
392 free(obj->path_packfile);
393 free(obj);
396 static const struct got_error *
397 parse_commit_object(struct got_commit_object **commit, char *buf, size_t len)
399 const struct got_error *err = NULL;
400 char *s = buf;
401 size_t tlen;
402 ssize_t remain = (ssize_t)len;
404 *commit = calloc(1, sizeof(**commit));
405 if (*commit == NULL)
406 return got_error_from_errno();
407 (*commit)->tree_id = calloc(1, sizeof(*(*commit)->tree_id));
408 if ((*commit)->tree_id == NULL) {
409 err = got_error_from_errno();
410 free(*commit);
411 *commit = NULL;
412 return err;
415 SIMPLEQ_INIT(&(*commit)->parent_ids);
417 tlen = strlen(GOT_COMMIT_TAG_TREE);
418 if (strncmp(s, GOT_COMMIT_TAG_TREE, tlen) == 0) {
419 remain -= tlen;
420 if (remain < SHA1_DIGEST_STRING_LENGTH) {
421 err = got_error(GOT_ERR_BAD_OBJ_DATA);
422 goto done;
424 s += tlen;
425 if (!got_parse_sha1_digest((*commit)->tree_id->sha1, s)) {
426 err = got_error(GOT_ERR_BAD_OBJ_DATA);
427 goto done;
429 remain -= SHA1_DIGEST_STRING_LENGTH;
430 s += SHA1_DIGEST_STRING_LENGTH;
431 } else {
432 err = got_error(GOT_ERR_BAD_OBJ_DATA);
433 goto done;
436 tlen = strlen(GOT_COMMIT_TAG_PARENT);
437 while (strncmp(s, GOT_COMMIT_TAG_PARENT, tlen) == 0) {
438 struct got_parent_id *pid;
440 remain -= tlen;
441 if (remain < SHA1_DIGEST_STRING_LENGTH) {
442 err = got_error(GOT_ERR_BAD_OBJ_DATA);
443 goto done;
446 pid = calloc(1, sizeof(*pid));
447 if (pid == NULL) {
448 err = got_error_from_errno();
449 goto done;
451 pid->id = calloc(1, sizeof(*pid->id));
452 if (pid->id == NULL) {
453 err = got_error_from_errno();
454 free(pid);
455 goto done;
457 s += tlen;
458 if (!got_parse_sha1_digest(pid->id->sha1, s)) {
459 err = got_error(GOT_ERR_BAD_OBJ_DATA);
460 free(pid->id);
461 free(pid);
462 goto done;
464 SIMPLEQ_INSERT_TAIL(&(*commit)->parent_ids, pid, entry);
465 (*commit)->nparents++;
467 remain -= SHA1_DIGEST_STRING_LENGTH;
468 s += SHA1_DIGEST_STRING_LENGTH;
471 tlen = strlen(GOT_COMMIT_TAG_AUTHOR);
472 if (strncmp(s, GOT_COMMIT_TAG_AUTHOR, tlen) == 0) {
473 char *p;
475 remain -= tlen;
476 if (remain <= 0) {
477 err = got_error(GOT_ERR_BAD_OBJ_DATA);
478 goto done;
480 s += tlen;
481 p = strchr(s, '\n');
482 if (p == NULL) {
483 err = got_error(GOT_ERR_BAD_OBJ_DATA);
484 goto done;
486 *p = '\0';
487 (*commit)->author = strdup(s);
488 if ((*commit)->author == NULL) {
489 err = got_error_from_errno();
490 goto done;
492 s += strlen((*commit)->author) + 1;
493 remain -= strlen((*commit)->author) + 1;
496 tlen = strlen(GOT_COMMIT_TAG_COMMITTER);
497 if (strncmp(s, GOT_COMMIT_TAG_COMMITTER, tlen) == 0) {
498 char *p;
500 remain -= tlen;
501 if (remain <= 0) {
502 err = got_error(GOT_ERR_BAD_OBJ_DATA);
503 goto done;
505 s += tlen;
506 p = strchr(s, '\n');
507 if (p == NULL) {
508 err = got_error(GOT_ERR_BAD_OBJ_DATA);
509 goto done;
511 *p = '\0';
512 (*commit)->committer = strdup(s);
513 if ((*commit)->committer == NULL) {
514 err = got_error_from_errno();
515 goto done;
517 s += strlen((*commit)->committer) + 1;
518 remain -= strlen((*commit)->committer) + 1;
521 (*commit)->logmsg = strndup(s, remain);
522 if ((*commit)->logmsg == NULL) {
523 err = got_error_from_errno();
524 goto done;
526 done:
527 if (err) {
528 got_object_commit_close(*commit);
529 *commit = NULL;
531 return err;
534 static void
535 tree_entry_close(struct got_tree_entry *te)
537 free(te->id);
538 free(te->name);
539 free(te);
542 static const struct got_error *
543 parse_tree_entry(struct got_tree_entry **te, size_t *elen, char *buf,
544 size_t maxlen)
546 char *p = buf, *space;
547 const struct got_error *err = NULL;
549 *te = calloc(1, sizeof(**te));
550 if (*te == NULL)
551 return got_error_from_errno();
553 (*te)->id = calloc(1, sizeof(*(*te)->id));
554 if ((*te)->id == NULL) {
555 err = got_error_from_errno();
556 free(*te);
557 *te = NULL;
558 return err;
561 *elen = strlen(buf) + 1;
562 if (*elen > maxlen) {
563 free(*te);
564 *te = NULL;
565 return got_error(GOT_ERR_BAD_OBJ_DATA);
568 space = strchr(buf, ' ');
569 if (space == NULL) {
570 err = got_error(GOT_ERR_BAD_OBJ_DATA);
571 free(*te);
572 *te = NULL;
573 return err;
575 while (*p != ' ') {
576 if (*p < '0' && *p > '7') {
577 err = got_error(GOT_ERR_BAD_OBJ_DATA);
578 goto done;
580 (*te)->mode <<= 3;
581 (*te)->mode |= *p - '0';
582 p++;
585 (*te)->name = strdup(space + 1);
586 if (*elen > maxlen || maxlen - *elen < SHA1_DIGEST_LENGTH) {
587 err = got_error(GOT_ERR_BAD_OBJ_DATA);
588 goto done;
590 buf += strlen(buf) + 1;
591 memcpy((*te)->id->sha1, buf, SHA1_DIGEST_LENGTH);
592 *elen += SHA1_DIGEST_LENGTH;
593 done:
594 if (err) {
595 tree_entry_close(*te);
596 *te = NULL;
598 return err;
601 static const struct got_error *
602 parse_tree_object(struct got_tree_object **tree, struct got_repository *repo,
603 uint8_t *buf, size_t len)
605 const struct got_error *err;
606 size_t remain = len;
608 *tree = calloc(1, sizeof(**tree));
609 if (*tree == NULL)
610 return got_error_from_errno();
612 SIMPLEQ_INIT(&(*tree)->entries);
614 while (remain > 0) {
615 struct got_tree_entry *te;
616 size_t elen;
618 err = parse_tree_entry(&te, &elen, buf, remain);
619 if (err)
620 return err;
621 (*tree)->nentries++;
622 SIMPLEQ_INSERT_TAIL(&(*tree)->entries, te, entry);
623 buf += elen;
624 remain -= elen;
627 if (remain != 0) {
628 got_object_tree_close(*tree);
629 return got_error(GOT_ERR_BAD_OBJ_DATA);
632 return NULL;
635 static const struct got_error *
636 read_to_mem(uint8_t **outbuf, size_t *outlen, FILE *f)
638 const struct got_error *err = NULL;
639 static const size_t blocksize = 512;
640 size_t n, total, remain;
641 uint8_t *buf;
643 *outbuf = NULL;
644 *outlen = 0;
646 buf = calloc(1, blocksize);
647 if (buf == NULL)
648 return got_error_from_errno();
650 remain = blocksize;
651 total = 0;
652 while (1) {
653 if (remain == 0) {
654 uint8_t *newbuf;
655 newbuf = reallocarray(buf, 1, total + blocksize);
656 if (newbuf == NULL) {
657 err = got_error_from_errno();
658 goto done;
660 buf = newbuf;
661 remain += blocksize;
663 n = fread(buf + total, 1, remain, f);
664 if (n == 0) {
665 if (ferror(f)) {
666 err = got_ferror(f, GOT_ERR_IO);
667 goto done;
669 break; /* EOF */
671 remain -= n;
672 total += n;
673 };
675 done:
676 if (err == NULL) {
677 *outbuf = buf;
678 *outlen = total;
679 } else
680 free(buf);
681 return err;
684 static const struct got_error *
685 read_commit_object(struct got_commit_object **commit,
686 struct got_repository *repo, struct got_object *obj, FILE *f)
688 const struct got_error *err = NULL;
689 size_t len;
690 uint8_t *p;
692 if (obj->flags & GOT_OBJ_FLAG_PACKED)
693 err = read_to_mem(&p, &len, f);
694 else
695 err = got_inflate_to_mem(&p, &len, f);
696 if (err)
697 return err;
699 if (len < obj->hdrlen + obj->size) {
700 err = got_error(GOT_ERR_BAD_OBJ_DATA);
701 goto done;
704 /* Skip object header. */
705 len -= obj->hdrlen;
706 err = parse_commit_object(commit, p + obj->hdrlen, len);
707 free(p);
708 done:
709 return err;
712 const struct got_error *
713 got_object_commit_open(struct got_commit_object **commit,
714 struct got_repository *repo, struct got_object *obj)
716 const struct got_error *err = NULL;
718 if (obj->type != GOT_OBJ_TYPE_COMMIT)
719 return got_error(GOT_ERR_OBJ_TYPE);
721 if (obj->flags & GOT_OBJ_FLAG_PACKED) {
722 uint8_t *buf;
723 size_t len;
724 err = got_packfile_extract_object_to_mem(&buf, &len, obj, repo);
725 if (err)
726 return err;
727 obj->size = len;
728 err = parse_commit_object(commit, buf, len);
729 free(buf);
730 } else {
731 FILE *f;
732 int fd;
733 err = open_loose_object(&fd, obj, repo);
734 if (err)
735 return err;
736 f = fdopen(fd, "rb");
737 if (f == NULL) {
738 err = got_error_from_errno();
739 close(fd);
740 return err;
742 err = read_commit_object(commit, repo, obj, f);
743 fclose(f);
745 return err;
748 void
749 got_object_commit_close(struct got_commit_object *commit)
751 struct got_parent_id *pid;
753 while (!SIMPLEQ_EMPTY(&commit->parent_ids)) {
754 pid = SIMPLEQ_FIRST(&commit->parent_ids);
755 SIMPLEQ_REMOVE_HEAD(&commit->parent_ids, entry);
756 free(pid->id);
757 free(pid);
760 free(commit->tree_id);
761 free(commit->author);
762 free(commit->committer);
763 free(commit->logmsg);
764 free(commit);
767 static const struct got_error *
768 read_tree_object(struct got_tree_object **tree,
769 struct got_repository *repo, struct got_object *obj, FILE *f)
771 const struct got_error *err = NULL;
772 size_t len;
773 uint8_t *p;
775 if (obj->flags & GOT_OBJ_FLAG_PACKED)
776 err = read_to_mem(&p, &len, f);
777 else
778 err = got_inflate_to_mem(&p, &len, f);
779 if (err)
780 return err;
782 if (len < obj->hdrlen + obj->size) {
783 err = got_error(GOT_ERR_BAD_OBJ_DATA);
784 goto done;
787 /* Skip object header. */
788 len -= obj->hdrlen;
789 err = parse_tree_object(tree, repo, p + obj->hdrlen, len);
790 free(p);
791 done:
792 return err;
795 const struct got_error *
796 got_object_tree_open(struct got_tree_object **tree,
797 struct got_repository *repo, struct got_object *obj)
799 const struct got_error *err = NULL;
801 if (obj->type != GOT_OBJ_TYPE_TREE)
802 return got_error(GOT_ERR_OBJ_TYPE);
804 if (obj->flags & GOT_OBJ_FLAG_PACKED) {
805 uint8_t *buf;
806 size_t len;
807 err = got_packfile_extract_object_to_mem(&buf, &len, obj, repo);
808 if (err)
809 return err;
810 obj->size = len;
811 err = parse_tree_object(tree, repo, buf, len);
812 free(buf);
813 } else {
814 FILE *f;
815 int fd;
816 err = open_loose_object(&fd, obj, repo);
817 if (err)
818 return err;
819 f = fdopen(fd, "rb");
820 if (f == NULL) {
821 close(fd);
822 return got_error_from_errno();
824 err = read_tree_object(tree, repo, obj, f);
825 fclose(f);
826 close(fd);
828 return err;
831 void
832 got_object_tree_close(struct got_tree_object *tree)
834 struct got_tree_entry *te;
836 while (!SIMPLEQ_EMPTY(&tree->entries)) {
837 te = SIMPLEQ_FIRST(&tree->entries);
838 SIMPLEQ_REMOVE_HEAD(&tree->entries, entry);
839 tree_entry_close(te);
842 free(tree);
845 const struct got_error *
846 got_object_blob_open(struct got_blob_object **blob,
847 struct got_repository *repo, struct got_object *obj, size_t blocksize)
849 const struct got_error *err = NULL;
851 if (obj->type != GOT_OBJ_TYPE_BLOB)
852 return got_error(GOT_ERR_OBJ_TYPE);
854 if (blocksize < obj->hdrlen)
855 return got_error(GOT_ERR_NO_SPACE);
857 *blob = calloc(1, sizeof(**blob));
858 if (*blob == NULL)
859 return got_error_from_errno();
861 if (obj->flags & GOT_OBJ_FLAG_PACKED) {
862 (*blob)->read_buf = calloc(1, blocksize);
863 if ((*blob)->read_buf == NULL) {
864 err = got_error_from_errno();
865 free(*blob);
866 *blob = NULL;
867 return err;
869 err = got_packfile_extract_object(&((*blob)->f), obj, repo);
870 if (err) {
871 free((*blob)->read_buf);
872 free(*blob);
873 *blob = NULL;
874 return err;
876 } else {
877 int fd;
878 err = open_loose_object(&fd, obj, repo);
879 if (err) {
880 free(*blob);
881 *blob = NULL;
882 return err;
884 (*blob)->f = fdopen(fd, "rb");
885 if ((*blob)->f == NULL) {
886 free(*blob);
887 *blob = NULL;
888 close(fd);
889 return err;
892 err = got_inflate_init(&(*blob)->zb, NULL, blocksize);
893 if (err != NULL) {
894 fclose((*blob)->f);
895 free(*blob);
896 *blob = NULL;
897 return err;
900 (*blob)->read_buf = (*blob)->zb.outbuf;
901 (*blob)->flags |= GOT_BLOB_F_COMPRESSED;
904 (*blob)->hdrlen = obj->hdrlen;
905 (*blob)->blocksize = blocksize;
906 memcpy(&(*blob)->id.sha1, obj->id.sha1, SHA1_DIGEST_LENGTH);
908 return err;
911 void
912 got_object_blob_close(struct got_blob_object *blob)
914 if (blob->flags & GOT_BLOB_F_COMPRESSED)
915 got_inflate_end(&blob->zb);
916 else
917 free(blob->read_buf);
918 fclose(blob->f);
919 free(blob);
922 char *
923 got_object_blob_id_str(struct got_blob_object *blob, char *buf, size_t size)
925 return got_sha1_digest_to_str(blob->id.sha1, buf, size);
928 size_t
929 got_object_blob_get_hdrlen(struct got_blob_object *blob)
931 return blob->hdrlen;
934 const uint8_t *
935 got_object_blob_get_read_buf(struct got_blob_object *blob)
937 return blob->read_buf;
940 const struct got_error *
941 got_object_blob_read_block(size_t *outlenp, struct got_blob_object *blob)
943 size_t n;
945 if (blob->flags & GOT_BLOB_F_COMPRESSED)
946 return got_inflate_read(&blob->zb, blob->f, outlenp);
948 n = fread(blob->read_buf, 1, blob->blocksize, blob->f);
949 if (n == 0 && ferror(blob->f))
950 return got_ferror(blob->f, GOT_ERR_IO);
951 *outlenp = n;
952 return NULL;