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 void
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;
224 struct imsgbuf ibuf;
225 FILE *f = NULL;
226 int status = 0;
228 setproctitle("got: read object header");
229 close(imsg_fds[0]);
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();
235 goto done;
238 f = fdopen(obj_fd, "rb");
239 if (f == NULL) {
240 err = got_error_from_errno();
241 close(obj_fd);
242 goto done;
245 err = read_object_header(&obj, f);
246 if (err)
247 goto done;
249 err = got_privsep_send_obj(&ibuf, obj, 0);
250 done:
251 if (obj)
252 got_object_close(obj);
253 if (err) {
254 got_privsep_send_error(&ibuf, err);
255 status = 1;
257 if (f)
258 fclose(f);
259 imsg_clear(&ibuf);
260 close(imsg_fds[1]);
261 _exit(status);
264 static const struct got_error *
265 read_object_header_privsep(struct got_object **obj, int fd)
267 struct imsgbuf parent_ibuf;
268 int imsg_fds[2];
269 const struct got_error *err = NULL;
270 pid_t pid;
271 int child_status;
273 if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_fds) == -1)
274 return got_error_from_errno();
276 pid = fork();
277 if (pid == -1)
278 return got_error_from_errno();
279 else if (pid == 0) {
280 read_object_header_privsep_child(fd, imsg_fds);
281 /* not reached */
284 close(imsg_fds[1]);
285 imsg_init(&parent_ibuf, imsg_fds[0]);
286 err = got_privsep_recv_obj(obj, &parent_ibuf);
287 imsg_clear(&parent_ibuf);
288 waitpid(pid, &child_status, 0);
289 close(imsg_fds[0]);
290 return err;
293 static const struct got_error *
294 object_path(char **path, struct got_object_id *id, struct got_repository *repo)
296 const struct got_error *err = NULL;
297 char *hex;
298 char *path_objects = got_repo_get_path_objects(repo);
300 *path = NULL;
302 if (path_objects == NULL)
303 return got_error_from_errno();
305 err = got_object_id_str(&hex, id);
306 if (err)
307 return err;
309 if (asprintf(path, "%s/%.2x/%s", path_objects,
310 id->sha1[0], hex + 2) == -1)
311 err = got_error_from_errno();
313 free(hex);
314 free(path_objects);
315 return err;
318 static const struct got_error *
319 open_loose_object(int *fd, struct got_object *obj, struct got_repository *repo)
321 const struct got_error *err = NULL;
322 char *path;
324 err = object_path(&path, &obj->id, repo);
325 if (err)
326 return err;
327 *fd = open(path, O_RDONLY | O_NOFOLLOW, GOT_DEFAULT_FILE_MODE);
328 if (*fd == -1) {
329 err = got_error_from_errno();
330 goto done;
332 done:
333 free(path);
334 return err;
337 const struct got_error *
338 got_object_open(struct got_object **obj, struct got_repository *repo,
339 struct got_object_id *id)
341 const struct got_error *err = NULL;
342 char *path;
343 int fd;
345 err = object_path(&path, id, repo);
346 if (err)
347 return err;
349 fd = open(path, O_RDONLY | O_NOFOLLOW, GOT_DEFAULT_FILE_MODE);
350 if (fd == -1) {
351 if (errno != ENOENT) {
352 err = got_error_from_errno();
353 goto done;
355 err = got_packfile_open_object(obj, id, repo);
356 if (err)
357 goto done;
358 if (*obj == NULL)
359 err = got_error(GOT_ERR_NO_OBJ);
360 } else {
361 err = read_object_header_privsep(obj, fd);
362 if (err)
363 goto done;
364 memcpy((*obj)->id.sha1, id->sha1, SHA1_DIGEST_LENGTH);
366 done:
367 free(path);
368 if (fd != -1)
369 close(fd);
370 return err;
374 const struct got_error *
375 got_object_open_by_id_str(struct got_object **obj, struct got_repository *repo,
376 const char *id_str)
378 struct got_object_id id;
380 if (!got_parse_sha1_digest(id.sha1, id_str))
381 return got_error(GOT_ERR_BAD_OBJ_ID_STR);
383 return got_object_open(obj, repo, &id);
386 void
387 got_object_close(struct got_object *obj)
389 if (obj->flags & GOT_OBJ_FLAG_DELTIFIED) {
390 struct got_delta *delta;
391 while (!SIMPLEQ_EMPTY(&obj->deltas.entries)) {
392 delta = SIMPLEQ_FIRST(&obj->deltas.entries);
393 SIMPLEQ_REMOVE_HEAD(&obj->deltas.entries, entry);
394 got_delta_close(delta);
397 if (obj->flags & GOT_OBJ_FLAG_PACKED)
398 free(obj->path_packfile);
399 free(obj);
402 static const struct got_error *
403 parse_commit_object(struct got_commit_object **commit, char *buf, size_t len)
405 const struct got_error *err = NULL;
406 char *s = buf;
407 size_t tlen;
408 ssize_t remain = (ssize_t)len;
410 *commit = calloc(1, sizeof(**commit));
411 if (*commit == NULL)
412 return got_error_from_errno();
413 (*commit)->tree_id = calloc(1, sizeof(*(*commit)->tree_id));
414 if ((*commit)->tree_id == NULL) {
415 err = got_error_from_errno();
416 free(*commit);
417 *commit = NULL;
418 return err;
421 SIMPLEQ_INIT(&(*commit)->parent_ids);
423 tlen = strlen(GOT_COMMIT_TAG_TREE);
424 if (strncmp(s, GOT_COMMIT_TAG_TREE, tlen) == 0) {
425 remain -= tlen;
426 if (remain < SHA1_DIGEST_STRING_LENGTH) {
427 err = got_error(GOT_ERR_BAD_OBJ_DATA);
428 goto done;
430 s += tlen;
431 if (!got_parse_sha1_digest((*commit)->tree_id->sha1, s)) {
432 err = got_error(GOT_ERR_BAD_OBJ_DATA);
433 goto done;
435 remain -= SHA1_DIGEST_STRING_LENGTH;
436 s += SHA1_DIGEST_STRING_LENGTH;
437 } else {
438 err = got_error(GOT_ERR_BAD_OBJ_DATA);
439 goto done;
442 tlen = strlen(GOT_COMMIT_TAG_PARENT);
443 while (strncmp(s, GOT_COMMIT_TAG_PARENT, tlen) == 0) {
444 struct got_parent_id *pid;
446 remain -= tlen;
447 if (remain < SHA1_DIGEST_STRING_LENGTH) {
448 err = got_error(GOT_ERR_BAD_OBJ_DATA);
449 goto done;
452 pid = calloc(1, sizeof(*pid));
453 if (pid == NULL) {
454 err = got_error_from_errno();
455 goto done;
457 pid->id = calloc(1, sizeof(*pid->id));
458 if (pid->id == NULL) {
459 err = got_error_from_errno();
460 free(pid);
461 goto done;
463 s += tlen;
464 if (!got_parse_sha1_digest(pid->id->sha1, s)) {
465 err = got_error(GOT_ERR_BAD_OBJ_DATA);
466 free(pid->id);
467 free(pid);
468 goto done;
470 SIMPLEQ_INSERT_TAIL(&(*commit)->parent_ids, pid, entry);
471 (*commit)->nparents++;
473 remain -= SHA1_DIGEST_STRING_LENGTH;
474 s += SHA1_DIGEST_STRING_LENGTH;
477 tlen = strlen(GOT_COMMIT_TAG_AUTHOR);
478 if (strncmp(s, GOT_COMMIT_TAG_AUTHOR, tlen) == 0) {
479 char *p;
481 remain -= tlen;
482 if (remain <= 0) {
483 err = got_error(GOT_ERR_BAD_OBJ_DATA);
484 goto done;
486 s += tlen;
487 p = strchr(s, '\n');
488 if (p == NULL) {
489 err = got_error(GOT_ERR_BAD_OBJ_DATA);
490 goto done;
492 *p = '\0';
493 (*commit)->author = strdup(s);
494 if ((*commit)->author == NULL) {
495 err = got_error_from_errno();
496 goto done;
498 s += strlen((*commit)->author) + 1;
499 remain -= strlen((*commit)->author) + 1;
502 tlen = strlen(GOT_COMMIT_TAG_COMMITTER);
503 if (strncmp(s, GOT_COMMIT_TAG_COMMITTER, tlen) == 0) {
504 char *p;
506 remain -= tlen;
507 if (remain <= 0) {
508 err = got_error(GOT_ERR_BAD_OBJ_DATA);
509 goto done;
511 s += tlen;
512 p = strchr(s, '\n');
513 if (p == NULL) {
514 err = got_error(GOT_ERR_BAD_OBJ_DATA);
515 goto done;
517 *p = '\0';
518 (*commit)->committer = strdup(s);
519 if ((*commit)->committer == NULL) {
520 err = got_error_from_errno();
521 goto done;
523 s += strlen((*commit)->committer) + 1;
524 remain -= strlen((*commit)->committer) + 1;
527 (*commit)->logmsg = strndup(s, remain);
528 if ((*commit)->logmsg == NULL) {
529 err = got_error_from_errno();
530 goto done;
532 done:
533 if (err) {
534 got_object_commit_close(*commit);
535 *commit = NULL;
537 return err;
540 static void
541 tree_entry_close(struct got_tree_entry *te)
543 free(te->id);
544 free(te->name);
545 free(te);
548 static const struct got_error *
549 parse_tree_entry(struct got_tree_entry **te, size_t *elen, char *buf,
550 size_t maxlen)
552 char *p = buf, *space;
553 const struct got_error *err = NULL;
555 *te = calloc(1, sizeof(**te));
556 if (*te == NULL)
557 return got_error_from_errno();
559 (*te)->id = calloc(1, sizeof(*(*te)->id));
560 if ((*te)->id == NULL) {
561 err = got_error_from_errno();
562 free(*te);
563 *te = NULL;
564 return err;
567 *elen = strlen(buf) + 1;
568 if (*elen > maxlen) {
569 free(*te);
570 *te = NULL;
571 return got_error(GOT_ERR_BAD_OBJ_DATA);
574 space = strchr(buf, ' ');
575 if (space == NULL) {
576 err = got_error(GOT_ERR_BAD_OBJ_DATA);
577 free(*te);
578 *te = NULL;
579 return err;
581 while (*p != ' ') {
582 if (*p < '0' && *p > '7') {
583 err = got_error(GOT_ERR_BAD_OBJ_DATA);
584 goto done;
586 (*te)->mode <<= 3;
587 (*te)->mode |= *p - '0';
588 p++;
591 (*te)->name = strdup(space + 1);
592 if (*elen > maxlen || maxlen - *elen < SHA1_DIGEST_LENGTH) {
593 err = got_error(GOT_ERR_BAD_OBJ_DATA);
594 goto done;
596 buf += strlen(buf) + 1;
597 memcpy((*te)->id->sha1, buf, SHA1_DIGEST_LENGTH);
598 *elen += SHA1_DIGEST_LENGTH;
599 done:
600 if (err) {
601 tree_entry_close(*te);
602 *te = NULL;
604 return err;
607 static const struct got_error *
608 parse_tree_object(struct got_tree_object **tree, struct got_repository *repo,
609 uint8_t *buf, size_t len)
611 const struct got_error *err;
612 size_t remain = len;
614 *tree = calloc(1, sizeof(**tree));
615 if (*tree == NULL)
616 return got_error_from_errno();
618 SIMPLEQ_INIT(&(*tree)->entries);
620 while (remain > 0) {
621 struct got_tree_entry *te;
622 size_t elen;
624 err = parse_tree_entry(&te, &elen, buf, remain);
625 if (err)
626 return err;
627 (*tree)->nentries++;
628 SIMPLEQ_INSERT_TAIL(&(*tree)->entries, te, entry);
629 buf += elen;
630 remain -= elen;
633 if (remain != 0) {
634 got_object_tree_close(*tree);
635 return got_error(GOT_ERR_BAD_OBJ_DATA);
638 return NULL;
641 static const struct got_error *
642 read_to_mem(uint8_t **outbuf, size_t *outlen, FILE *f)
644 const struct got_error *err = NULL;
645 static const size_t blocksize = 512;
646 size_t n, total, remain;
647 uint8_t *buf;
649 *outbuf = NULL;
650 *outlen = 0;
652 buf = calloc(1, blocksize);
653 if (buf == NULL)
654 return got_error_from_errno();
656 remain = blocksize;
657 total = 0;
658 while (1) {
659 if (remain == 0) {
660 uint8_t *newbuf;
661 newbuf = reallocarray(buf, 1, total + blocksize);
662 if (newbuf == NULL) {
663 err = got_error_from_errno();
664 goto done;
666 buf = newbuf;
667 remain += blocksize;
669 n = fread(buf + total, 1, remain, f);
670 if (n == 0) {
671 if (ferror(f)) {
672 err = got_ferror(f, GOT_ERR_IO);
673 goto done;
675 break; /* EOF */
677 remain -= n;
678 total += n;
679 };
681 done:
682 if (err == NULL) {
683 *outbuf = buf;
684 *outlen = total;
685 } else
686 free(buf);
687 return err;
690 static const struct got_error *
691 read_commit_object(struct got_commit_object **commit,
692 struct got_repository *repo, struct got_object *obj, FILE *f)
694 const struct got_error *err = NULL;
695 size_t len;
696 uint8_t *p;
698 if (obj->flags & GOT_OBJ_FLAG_PACKED)
699 err = read_to_mem(&p, &len, f);
700 else
701 err = got_inflate_to_mem(&p, &len, f);
702 if (err)
703 return err;
705 if (len < obj->hdrlen + obj->size) {
706 err = got_error(GOT_ERR_BAD_OBJ_DATA);
707 goto done;
710 /* Skip object header. */
711 len -= obj->hdrlen;
712 err = parse_commit_object(commit, p + obj->hdrlen, len);
713 free(p);
714 done:
715 return err;
718 const struct got_error *
719 got_object_commit_open(struct got_commit_object **commit,
720 struct got_repository *repo, struct got_object *obj)
722 const struct got_error *err = NULL;
724 if (obj->type != GOT_OBJ_TYPE_COMMIT)
725 return got_error(GOT_ERR_OBJ_TYPE);
727 if (obj->flags & GOT_OBJ_FLAG_PACKED) {
728 uint8_t *buf;
729 size_t len;
730 err = got_packfile_extract_object_to_mem(&buf, &len, obj, repo);
731 if (err)
732 return err;
733 obj->size = len;
734 err = parse_commit_object(commit, buf, len);
735 free(buf);
736 } else {
737 FILE *f;
738 int fd;
739 err = open_loose_object(&fd, obj, repo);
740 if (err)
741 return err;
742 f = fdopen(fd, "rb");
743 if (f == NULL) {
744 err = got_error_from_errno();
745 close(fd);
746 return err;
748 err = read_commit_object(commit, repo, obj, f);
749 fclose(f);
751 return err;
754 void
755 got_object_commit_close(struct got_commit_object *commit)
757 struct got_parent_id *pid;
759 while (!SIMPLEQ_EMPTY(&commit->parent_ids)) {
760 pid = SIMPLEQ_FIRST(&commit->parent_ids);
761 SIMPLEQ_REMOVE_HEAD(&commit->parent_ids, entry);
762 free(pid->id);
763 free(pid);
766 free(commit->tree_id);
767 free(commit->author);
768 free(commit->committer);
769 free(commit->logmsg);
770 free(commit);
773 static const struct got_error *
774 read_tree_object(struct got_tree_object **tree,
775 struct got_repository *repo, struct got_object *obj, FILE *f)
777 const struct got_error *err = NULL;
778 size_t len;
779 uint8_t *p;
781 if (obj->flags & GOT_OBJ_FLAG_PACKED)
782 err = read_to_mem(&p, &len, f);
783 else
784 err = got_inflate_to_mem(&p, &len, f);
785 if (err)
786 return err;
788 if (len < obj->hdrlen + obj->size) {
789 err = got_error(GOT_ERR_BAD_OBJ_DATA);
790 goto done;
793 /* Skip object header. */
794 len -= obj->hdrlen;
795 err = parse_tree_object(tree, repo, p + obj->hdrlen, len);
796 free(p);
797 done:
798 return err;
801 const struct got_error *
802 got_object_tree_open(struct got_tree_object **tree,
803 struct got_repository *repo, struct got_object *obj)
805 const struct got_error *err = NULL;
807 if (obj->type != GOT_OBJ_TYPE_TREE)
808 return got_error(GOT_ERR_OBJ_TYPE);
810 if (obj->flags & GOT_OBJ_FLAG_PACKED) {
811 uint8_t *buf;
812 size_t len;
813 err = got_packfile_extract_object_to_mem(&buf, &len, obj, repo);
814 if (err)
815 return err;
816 obj->size = len;
817 err = parse_tree_object(tree, repo, buf, len);
818 free(buf);
819 } else {
820 FILE *f;
821 int fd;
822 err = open_loose_object(&fd, obj, repo);
823 if (err)
824 return err;
825 f = fdopen(fd, "rb");
826 if (f == NULL) {
827 close(fd);
828 return got_error_from_errno();
830 err = read_tree_object(tree, repo, obj, f);
831 fclose(f);
832 close(fd);
834 return err;
837 void
838 got_object_tree_close(struct got_tree_object *tree)
840 struct got_tree_entry *te;
842 while (!SIMPLEQ_EMPTY(&tree->entries)) {
843 te = SIMPLEQ_FIRST(&tree->entries);
844 SIMPLEQ_REMOVE_HEAD(&tree->entries, entry);
845 tree_entry_close(te);
848 free(tree);
851 const struct got_error *
852 got_object_blob_open(struct got_blob_object **blob,
853 struct got_repository *repo, struct got_object *obj, size_t blocksize)
855 const struct got_error *err = NULL;
857 if (obj->type != GOT_OBJ_TYPE_BLOB)
858 return got_error(GOT_ERR_OBJ_TYPE);
860 if (blocksize < obj->hdrlen)
861 return got_error(GOT_ERR_NO_SPACE);
863 *blob = calloc(1, sizeof(**blob));
864 if (*blob == NULL)
865 return got_error_from_errno();
867 if (obj->flags & GOT_OBJ_FLAG_PACKED) {
868 (*blob)->read_buf = calloc(1, blocksize);
869 if ((*blob)->read_buf == NULL) {
870 err = got_error_from_errno();
871 free(*blob);
872 *blob = NULL;
873 return err;
875 err = got_packfile_extract_object(&((*blob)->f), obj, repo);
876 if (err) {
877 free((*blob)->read_buf);
878 free(*blob);
879 *blob = NULL;
880 return err;
882 } else {
883 int fd;
884 err = open_loose_object(&fd, obj, repo);
885 if (err) {
886 free(*blob);
887 *blob = NULL;
888 return err;
890 (*blob)->f = fdopen(fd, "rb");
891 if ((*blob)->f == NULL) {
892 free(*blob);
893 *blob = NULL;
894 close(fd);
895 return err;
898 err = got_inflate_init(&(*blob)->zb, NULL, blocksize);
899 if (err != NULL) {
900 fclose((*blob)->f);
901 free(*blob);
902 *blob = NULL;
903 return err;
906 (*blob)->read_buf = (*blob)->zb.outbuf;
907 (*blob)->flags |= GOT_BLOB_F_COMPRESSED;
910 (*blob)->hdrlen = obj->hdrlen;
911 (*blob)->blocksize = blocksize;
912 memcpy(&(*blob)->id.sha1, obj->id.sha1, SHA1_DIGEST_LENGTH);
914 return err;
917 void
918 got_object_blob_close(struct got_blob_object *blob)
920 if (blob->flags & GOT_BLOB_F_COMPRESSED)
921 got_inflate_end(&blob->zb);
922 else
923 free(blob->read_buf);
924 fclose(blob->f);
925 free(blob);
928 char *
929 got_object_blob_id_str(struct got_blob_object *blob, char *buf, size_t size)
931 return got_sha1_digest_to_str(blob->id.sha1, buf, size);
934 size_t
935 got_object_blob_get_hdrlen(struct got_blob_object *blob)
937 return blob->hdrlen;
940 const uint8_t *
941 got_object_blob_get_read_buf(struct got_blob_object *blob)
943 return blob->read_buf;
946 const struct got_error *
947 got_object_blob_read_block(size_t *outlenp, struct got_blob_object *blob)
949 size_t n;
951 if (blob->flags & GOT_BLOB_F_COMPRESSED)
952 return got_inflate_read(&blob->zb, blob->f, outlenp);
954 n = fread(blob->read_buf, 1, blob->blocksize, blob->f);
955 if (n == 0 && ferror(blob->f))
956 return got_ferror(blob->f, GOT_ERR_IO);
957 *outlenp = n;
958 return NULL;