Blob


1 /*
2 * Copyright (c) 2022 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/queue.h>
18 #include <sys/tree.h>
19 #include <sys/stat.h>
21 #include <errno.h>
22 #include <limits.h>
23 #include <sha1.h>
24 #include <sha2.h>
25 #include <stdio.h>
26 #include <stdlib.h>
27 #include <string.h>
28 #include <unistd.h>
29 #include <zlib.h>
31 #include "got_error.h"
32 #include "got_object.h"
33 #include "got_repository.h"
34 #include "got_path.h"
36 #include "got_lib_delta.h"
37 #include "got_lib_object.h"
38 #include "got_lib_object_cache.h"
39 #include "got_lib_object_parse.h"
40 #include "got_lib_pack.h"
41 #include "got_lib_repository.h"
42 #include "got_lib_inflate.h"
43 #include "got_lib_hash.h"
45 const struct got_error *
46 got_object_open_packed(struct got_object **obj, struct got_object_id *id,
47 struct got_repository *repo)
48 {
49 const struct got_error *err = NULL;
50 struct got_pack *pack = NULL;
51 struct got_packidx *packidx = NULL;
52 int idx;
53 char *path_packfile;
55 err = got_repo_search_packidx(&packidx, &idx, repo, id);
56 if (err)
57 return err;
59 err = got_packidx_get_packfile_path(&path_packfile,
60 packidx->path_packidx);
61 if (err)
62 return err;
64 pack = got_repo_get_cached_pack(repo, path_packfile);
65 if (pack == NULL) {
66 err = got_repo_cache_pack(&pack, repo, path_packfile, packidx);
67 if (err)
68 goto done;
69 }
71 err = got_packfile_open_object(obj, pack, packidx, idx, id);
72 if (err)
73 return err;
74 (*obj)->refcnt++;
76 err = got_repo_cache_object(repo, id, *obj);
77 if (err) {
78 if (err->code == GOT_ERR_OBJ_EXISTS ||
79 err->code == GOT_ERR_OBJ_TOO_LARGE)
80 err = NULL;
81 }
82 done:
83 free(path_packfile);
84 return err;
85 }
87 const struct got_error *
88 got_object_open_from_packfile(struct got_object **obj, struct got_object_id *id,
89 struct got_pack *pack, struct got_packidx *packidx, int obj_idx,
90 struct got_repository *repo)
91 {
92 const struct got_error *err;
94 *obj = got_repo_get_cached_object(repo, id);
95 if (*obj != NULL) {
96 (*obj)->refcnt++;
97 return NULL;
98 }
100 err = got_packfile_open_object(obj, pack, packidx, obj_idx, id);
101 if (err)
102 return err;
103 (*obj)->refcnt++;
105 err = got_repo_cache_object(repo, id, *obj);
106 if (err) {
107 if (err->code == GOT_ERR_OBJ_EXISTS ||
108 err->code == GOT_ERR_OBJ_TOO_LARGE)
109 err = NULL;
110 return err;
112 (*obj)->refcnt++;
113 return NULL;
116 const struct got_error *
117 got_object_read_raw_delta(uint64_t *base_size, uint64_t *result_size,
118 off_t *delta_size, off_t *delta_compressed_size, off_t *delta_offset,
119 off_t *delta_out_offset, struct got_object_id **base_id, int delta_cache_fd,
120 struct got_packidx *packidx, int obj_idx, struct got_object_id *id,
121 struct got_repository *repo)
123 return got_error(GOT_ERR_NOT_IMPL);
126 const struct got_error *
127 got_object_open(struct got_object **obj, struct got_repository *repo,
128 struct got_object_id *id)
130 const struct got_error *err = NULL;
131 int fd;
133 *obj = got_repo_get_cached_object(repo, id);
134 if (*obj != NULL) {
135 (*obj)->refcnt++;
136 return NULL;
139 err = got_object_open_packed(obj, id, repo);
140 if (err) {
141 if (err->code != GOT_ERR_NO_OBJ)
142 return err;
143 } else
144 return NULL;
146 err = got_object_open_loose_fd(&fd, id, repo);
147 if (err) {
148 if (err->code == GOT_ERR_ERRNO && errno == ENOENT)
149 err = got_error_no_obj(id);
150 return err;
153 err = got_object_read_header(obj, fd);
154 if (err)
155 goto done;
157 memcpy(&(*obj)->id, id, sizeof((*obj)->id));
158 (*obj)->refcnt++;
160 err = got_repo_cache_object(repo, id, *obj);
161 if (err) {
162 if (err->code == GOT_ERR_OBJ_EXISTS ||
163 err->code == GOT_ERR_OBJ_TOO_LARGE)
164 err = NULL;
166 done:
167 if (close(fd) == -1 && err == NULL)
168 err = got_error_from_errno("close");
169 return err;
172 static const struct got_error *
173 wrap_fd(FILE **f, int wrapped_fd)
175 const struct got_error *err = NULL;
176 int fd;
178 if (ftruncate(wrapped_fd, 0L) == -1)
179 return got_error_from_errno("ftruncate");
181 if (lseek(wrapped_fd, 0L, SEEK_SET) == -1)
182 return got_error_from_errno("lseek");
184 fd = dup(wrapped_fd);
185 if (fd == -1)
186 return got_error_from_errno("dup");
188 *f = fdopen(fd, "w+");
189 if (*f == NULL) {
190 err = got_error_from_errno("fdopen");
191 close(fd);
193 return err;
196 static const struct got_error *
197 read_packed_object_raw(uint8_t **outbuf, off_t *size, size_t *hdrlen,
198 int outfd, struct got_pack *pack, struct got_packidx *packidx, int idx,
199 struct got_object_id *id)
201 const struct got_error *err = NULL;
202 uint64_t raw_size = 0;
203 struct got_object *obj;
204 FILE *outfile = NULL, *basefile = NULL, *accumfile = NULL;
206 *outbuf = NULL;
207 *size = 0;
208 *hdrlen = 0;
210 err = got_packfile_open_object(&obj, pack, packidx, idx, id);
211 if (err)
212 return err;
214 if (obj->flags & GOT_OBJ_FLAG_DELTIFIED) {
215 err = got_pack_get_max_delta_object_size(&raw_size, obj, pack);
216 if (err)
217 goto done;
218 } else
219 raw_size = obj->size;
221 if (raw_size <= GOT_DELTA_RESULT_SIZE_CACHED_MAX) {
222 size_t len;
223 err = got_packfile_extract_object_to_mem(outbuf, &len,
224 obj, pack);
225 if (err)
226 goto done;
227 *size = (off_t)len;
228 } else {
229 /*
230 * XXX This uses 3 file extra descriptors for no good reason.
231 * We should have got_packfile_extract_object_to_fd().
232 */
233 err = wrap_fd(&outfile, outfd);
234 if (err)
235 goto done;
236 err = wrap_fd(&basefile, pack->basefd);
237 if (err)
238 goto done;
239 err = wrap_fd(&accumfile, pack->accumfd);
240 if (err)
241 goto done;
242 err = got_packfile_extract_object(pack, obj, outfile, basefile,
243 accumfile);
244 if (err)
245 goto done;
246 *size = obj->size;
249 *hdrlen = obj->hdrlen;
250 done:
251 got_object_close(obj);
252 if (outfile && fclose(outfile) == EOF && err == NULL)
253 err = got_error_from_errno("fclose");
254 if (basefile && fclose(basefile) == EOF && err == NULL)
255 err = got_error_from_errno("fclose");
256 if (accumfile && fclose(accumfile) == EOF && err == NULL)
257 err = got_error_from_errno("fclose");
258 return err;
262 static void
263 put_raw_object_tempfile(struct got_raw_object *obj)
265 struct got_repository *repo = obj->close_arg;
267 if (obj->tempfile_idx != -1)
268 got_repo_temp_fds_put(obj->tempfile_idx, repo);
271 /* *outfd must be initialized to -1 by caller */
272 const struct got_error *
273 got_object_raw_open(struct got_raw_object **obj, int *outfd,
274 struct got_repository *repo, struct got_object_id *id)
276 const struct got_error *err = NULL;
277 struct got_packidx *packidx = NULL;
278 int idx, tempfd, tempfile_idx;
279 uint8_t *outbuf = NULL;
280 off_t size = 0;
281 size_t hdrlen = 0;
282 char *path_packfile = NULL;
284 *obj = got_repo_get_cached_raw_object(repo, id);
285 if (*obj != NULL) {
286 (*obj)->refcnt++;
287 return NULL;
290 err = got_repo_temp_fds_get(&tempfd, &tempfile_idx, repo);
291 if (err)
292 return err;
294 err = got_repo_search_packidx(&packidx, &idx, repo, id);
295 if (err == NULL) {
296 struct got_pack *pack = NULL;
298 err = got_packidx_get_packfile_path(&path_packfile,
299 packidx->path_packidx);
300 if (err)
301 goto done;
303 pack = got_repo_get_cached_pack(repo, path_packfile);
304 if (pack == NULL) {
305 err = got_repo_cache_pack(&pack, repo, path_packfile,
306 packidx);
307 if (err)
308 goto done;
310 err = read_packed_object_raw(&outbuf, &size, &hdrlen,
311 tempfd, pack, packidx, idx, id);
312 if (err)
313 goto done;
314 } else if (err->code == GOT_ERR_NO_OBJ) {
315 int fd;
317 err = got_object_open_loose_fd(&fd, id, repo);
318 if (err)
319 goto done;
320 err = got_object_read_raw(&outbuf, &size, &hdrlen,
321 GOT_DELTA_RESULT_SIZE_CACHED_MAX, tempfd, id, fd);
322 if (close(fd) == -1 && err == NULL)
323 err = got_error_from_errno("close");
324 if (err)
325 goto done;
328 if (outbuf == NULL) {
329 if (*outfd != -1) {
330 err = got_error_msg(GOT_ERR_NOT_IMPL, "bad outfd");
331 goto done;
334 /*
335 * Duplicate tempfile descriptor to allow use of
336 * fdopen(3) inside got_object_raw_alloc().
337 */
338 *outfd = dup(tempfd);
339 if (*outfd == -1) {
340 err = got_error_from_errno("dup");
341 goto done;
345 err = got_object_raw_alloc(obj, outbuf, outfd,
346 GOT_DELTA_RESULT_SIZE_CACHED_MAX, hdrlen, size);
347 if (err)
348 goto done;
350 err = got_repo_cache_raw_object(repo, id, *obj);
351 if (err) {
352 if (err->code == GOT_ERR_OBJ_EXISTS ||
353 err->code == GOT_ERR_OBJ_TOO_LARGE)
354 err = NULL;
356 done:
357 free(path_packfile);
358 if (err) {
359 if (*obj) {
360 got_object_raw_close(*obj);
361 *obj = NULL;
363 free(outbuf);
364 got_repo_temp_fds_put(tempfile_idx, repo);
365 if (*outfd != -1) {
366 close(*outfd);
367 *outfd = -1;
369 } else {
370 if (((*obj)->f == NULL && (*obj)->fd == -1)) {
371 /* This raw object is not backed by a file. */
372 got_repo_temp_fds_put(tempfile_idx, repo);
373 if (*outfd != -1) {
374 close(*outfd);
375 *outfd = -1;
377 } else {
378 (*obj)->tempfile_idx = tempfile_idx;
379 (*obj)->close_cb = put_raw_object_tempfile;
380 (*obj)->close_arg = repo;
383 return err;
386 static const struct got_error *
387 open_commit(struct got_commit_object **commit,
388 struct got_repository *repo, struct got_object_id *id, int check_cache)
390 const struct got_error *err = NULL;
391 struct got_packidx *packidx = NULL;
392 int idx;
393 char *path_packfile = NULL;
395 if (check_cache) {
396 *commit = got_repo_get_cached_commit(repo, id);
397 if (*commit != NULL) {
398 (*commit)->refcnt++;
399 return NULL;
401 } else
402 *commit = NULL;
404 err = got_repo_search_packidx(&packidx, &idx, repo, id);
405 if (err == NULL) {
406 struct got_pack *pack = NULL;
407 struct got_object *obj;
408 uint8_t *buf;
409 size_t len;
411 err = got_packidx_get_packfile_path(&path_packfile,
412 packidx->path_packidx);
413 if (err)
414 return err;
416 pack = got_repo_get_cached_pack(repo, path_packfile);
417 if (pack == NULL) {
418 err = got_repo_cache_pack(&pack, repo, path_packfile,
419 packidx);
420 if (err)
421 goto done;
423 err = got_packfile_open_object(&obj, pack, packidx, idx, id);
424 if (err)
425 goto done;
426 err = got_packfile_extract_object_to_mem(&buf, &len,
427 obj, pack);
428 got_object_close(obj);
429 if (err)
430 goto done;
431 err = got_object_parse_commit(commit, buf, len);
432 free(buf);
433 } else if (err->code == GOT_ERR_NO_OBJ) {
434 int fd;
436 err = got_object_open_loose_fd(&fd, id, repo);
437 if (err)
438 return err;
439 err = got_object_read_commit(commit, fd, id, 0);
440 if (close(fd) == -1 && err == NULL)
441 err = got_error_from_errno("close");
442 if (err)
443 return err;
446 if (err == NULL) {
447 (*commit)->refcnt++;
448 err = got_repo_cache_commit(repo, id, *commit);
449 if (err) {
450 if (err->code == GOT_ERR_OBJ_EXISTS ||
451 err->code == GOT_ERR_OBJ_TOO_LARGE)
452 err = NULL;
455 done:
456 free(path_packfile);
457 return err;
460 const struct got_error *
461 got_object_open_as_commit(struct got_commit_object **commit,
462 struct got_repository *repo, struct got_object_id *id)
464 *commit = got_repo_get_cached_commit(repo, id);
465 if (*commit != NULL) {
466 (*commit)->refcnt++;
467 return NULL;
470 return open_commit(commit, repo, id, 0);
473 const struct got_error *
474 got_object_commit_open(struct got_commit_object **commit,
475 struct got_repository *repo, struct got_object *obj)
477 return open_commit(commit, repo, got_object_get_id(obj), 1);
480 static const struct got_error *
481 open_tree(struct got_tree_object **tree,
482 struct got_repository *repo, struct got_object_id *id, int check_cache)
484 const struct got_error *err = NULL;
485 struct got_packidx *packidx = NULL;
486 int idx;
487 char *path_packfile = NULL;
488 struct got_parsed_tree_entry *entries = NULL;
489 size_t nentries = 0, nentries_alloc = 0, i;
490 uint8_t *buf = NULL;
492 if (check_cache) {
493 *tree = got_repo_get_cached_tree(repo, id);
494 if (*tree != NULL) {
495 (*tree)->refcnt++;
496 return NULL;
498 } else
499 *tree = NULL;
501 err = got_repo_search_packidx(&packidx, &idx, repo, id);
502 if (err == NULL) {
503 struct got_pack *pack = NULL;
504 struct got_object *obj;
505 size_t len;
507 err = got_packidx_get_packfile_path(&path_packfile,
508 packidx->path_packidx);
509 if (err)
510 return err;
512 pack = got_repo_get_cached_pack(repo, path_packfile);
513 if (pack == NULL) {
514 err = got_repo_cache_pack(&pack, repo, path_packfile,
515 packidx);
516 if (err)
517 goto done;
519 err = got_packfile_open_object(&obj, pack, packidx, idx, id);
520 if (err)
521 goto done;
522 err = got_packfile_extract_object_to_mem(&buf, &len,
523 obj, pack);
524 got_object_close(obj);
525 if (err)
526 goto done;
527 err = got_object_parse_tree(&entries, &nentries,
528 &nentries_alloc, buf, len);
529 if (err)
530 goto done;
531 } else if (err->code == GOT_ERR_NO_OBJ) {
532 int fd;
534 err = got_object_open_loose_fd(&fd, id, repo);
535 if (err)
536 return err;
537 err = got_object_read_tree(&entries, &nentries,
538 &nentries_alloc, &buf, fd, id);
539 if (close(fd) == -1 && err == NULL)
540 err = got_error_from_errno("close");
541 if (err)
542 goto done;
543 } else
544 goto done;
546 *tree = malloc(sizeof(**tree));
547 if (*tree == NULL) {
548 err = got_error_from_errno("malloc");
549 goto done;
551 (*tree)->entries = calloc(nentries, sizeof(struct got_tree_entry));
552 if ((*tree)->entries == NULL) {
553 err = got_error_from_errno("malloc");
554 goto done;
556 (*tree)->nentries = nentries;
557 (*tree)->refcnt = 0;
559 for (i = 0; i < nentries; i++) {
560 struct got_parsed_tree_entry *pe = &entries[i];
561 struct got_tree_entry *te = &(*tree)->entries[i];
563 if (strlcpy(te->name, pe->name,
564 sizeof(te->name)) >= sizeof(te->name)) {
565 err = got_error(GOT_ERR_NO_SPACE);
566 goto done;
568 memcpy(te->id.sha1, pe->id, SHA1_DIGEST_LENGTH);
569 te->mode = pe->mode;
570 te->idx = i;
572 done:
573 free(path_packfile);
574 free(entries);
575 free(buf);
576 if (err == NULL) {
577 (*tree)->refcnt++;
578 err = got_repo_cache_tree(repo, id, *tree);
579 if (err) {
580 if (err->code == GOT_ERR_OBJ_EXISTS ||
581 err->code == GOT_ERR_OBJ_TOO_LARGE)
582 err = NULL;
585 if (err) {
586 if (*tree)
587 free((*tree)->entries);
588 free(*tree);
589 *tree = NULL;
591 return err;
594 const struct got_error *
595 got_object_open_as_tree(struct got_tree_object **tree,
596 struct got_repository *repo, struct got_object_id *id)
598 *tree = got_repo_get_cached_tree(repo, id);
599 if (*tree != NULL) {
600 (*tree)->refcnt++;
601 return NULL;
604 return open_tree(tree, repo, id, 0);
607 const struct got_error *
608 got_object_tree_open(struct got_tree_object **tree,
609 struct got_repository *repo, struct got_object *obj)
611 return open_tree(tree, repo, got_object_get_id(obj), 1);
614 static const struct got_error *
615 read_packed_blob(uint8_t **outbuf, size_t *size, size_t *hdrlen,
616 int outfd, struct got_pack *pack, struct got_packidx *packidx, int idx,
617 struct got_object_id *id, struct got_repository *repo)
619 const struct got_error *err = NULL;
620 struct got_object *obj;
621 FILE *outfile = NULL, *basefile = NULL, *accumfile = NULL;
622 uint64_t blob_size;
624 *hdrlen = 0;
626 err = got_object_open_from_packfile(&obj, id, pack, packidx, idx,
627 repo);
628 if (err)
629 return err;
631 if (obj->flags & GOT_OBJ_FLAG_DELTIFIED) {
632 err = got_pack_get_max_delta_object_size(&blob_size, obj,
633 pack);
634 if (err)
635 goto done;
636 } else
637 blob_size = obj->size;
639 if (blob_size <= GOT_DELTA_RESULT_SIZE_CACHED_MAX) {
640 err = got_packfile_extract_object_to_mem(outbuf, size,
641 obj, pack);
642 } else {
643 /*
644 * XXX This uses 3 file extra descriptors for no good reason.
645 * We should have got_packfile_extract_object_to_fd().
646 */
647 err = wrap_fd(&outfile, outfd);
648 if (err)
649 goto done;
650 err = wrap_fd(&basefile, pack->basefd);
651 if (err)
652 goto done;
653 err = wrap_fd(&accumfile, pack->accumfd);
654 if (err)
655 goto done;
656 err = got_packfile_extract_object(pack, obj, outfile, basefile,
657 accumfile);
658 if (err)
659 goto done;
660 *size = obj->size;
663 /* XXX verify checksum? */
664 done:
665 got_object_close(obj);
666 if (outfile && fclose(outfile) == EOF && err == NULL)
667 err = got_error_from_errno("fclose");
668 if (basefile && fclose(basefile) == EOF && err == NULL)
669 err = got_error_from_errno("fclose");
670 if (accumfile && fclose(accumfile) == EOF && err == NULL)
671 err = got_error_from_errno("fclose");
672 return err;
675 static const struct got_error *
676 read_blob(uint8_t **outbuf, size_t *size, size_t *hdrlen, int outfd, int infd,
677 struct got_object_id *id, struct got_repository *repo)
679 const struct got_error *err = NULL;
680 struct got_object *obj = NULL;
681 FILE *f = NULL;
682 struct got_object_id expected_id;
683 struct got_inflate_checksum csum;
684 struct got_hash ctx;
686 got_hash_init(&ctx, GOT_HASH_SHA1);
687 memset(&csum, 0, sizeof(csum));
688 csum.output_ctx = &ctx;
690 memcpy(&expected_id, id, sizeof(expected_id));
692 err = got_object_read_header(&obj, infd);
693 if (err)
694 goto done;
696 if (lseek(infd, SEEK_SET, 0) == -1) {
697 err = got_error_from_errno("lseek");
698 goto done;
701 f = fdopen(infd, "rb");
702 if (f == NULL) {
703 err = got_error_from_errno("fdopen");
704 goto done;
706 infd = -1;
708 if (obj->size + obj->hdrlen <= GOT_DELTA_RESULT_SIZE_CACHED_MAX) {
709 err = got_inflate_to_mem(outbuf, size, NULL, &csum, f);
710 if (err)
711 goto done;
712 } else {
713 err = got_inflate_to_fd(size, f, &csum, outfd);
714 if (err)
715 goto done;
718 if (*size < obj->hdrlen) {
719 err = got_error(GOT_ERR_BAD_OBJ_HDR);
720 goto done;
723 *hdrlen = obj->hdrlen;
725 got_hash_final_object_id(&ctx, id);
726 if (got_object_id_cmp(&expected_id, id) != 0) {
727 err = got_error_checksum(&expected_id);
728 goto done;
730 done:
731 if (f && fclose(f) == EOF && err == NULL)
732 err = got_error_from_errno("fclose");
733 if (infd != -1 && close(infd) == -1 && err == NULL)
734 err = got_error_from_errno("close");
736 return err;
739 static const struct got_error *
740 open_blob(struct got_blob_object **blob, struct got_repository *repo,
741 struct got_object_id *id, size_t blocksize, int outfd)
743 const struct got_error *err = NULL;
744 struct got_packidx *packidx = NULL;
745 int idx, dfd = -1;
746 char *path_packfile = NULL;
747 uint8_t *outbuf;
748 size_t size, hdrlen;
749 struct stat sb;
751 *blob = calloc(1, sizeof(**blob));
752 if (*blob == NULL)
753 return got_error_from_errno("calloc");
755 (*blob)->read_buf = malloc(blocksize);
756 if ((*blob)->read_buf == NULL) {
757 err = got_error_from_errno("malloc");
758 goto done;
761 if (ftruncate(outfd, 0L) == -1) {
762 err = got_error_from_errno("ftruncate");
763 goto done;
765 if (lseek(outfd, SEEK_SET, 0) == -1) {
766 err = got_error_from_errno("lseek");
767 goto done;
770 err = got_repo_search_packidx(&packidx, &idx, repo, id);
771 if (err == NULL) {
772 struct got_pack *pack = NULL;
774 err = got_packidx_get_packfile_path(&path_packfile,
775 packidx->path_packidx);
776 if (err)
777 goto done;
779 pack = got_repo_get_cached_pack(repo, path_packfile);
780 if (pack == NULL) {
781 err = got_repo_cache_pack(&pack, repo, path_packfile,
782 packidx);
783 if (err)
784 goto done;
786 err = read_packed_blob(&outbuf, &size, &hdrlen, outfd,
787 pack, packidx, idx, id, repo);
788 } else if (err->code == GOT_ERR_NO_OBJ) {
789 int infd;
791 err = got_object_open_loose_fd(&infd, id, repo);
792 if (err)
793 goto done;
794 err = read_blob(&outbuf, &size, &hdrlen, outfd, infd,
795 id, repo);
797 if (err)
798 goto done;
800 if (hdrlen > size) {
801 err = got_error(GOT_ERR_BAD_OBJ_HDR);
802 goto done;
805 if (outbuf) {
806 (*blob)->f = fmemopen(outbuf, size, "rb");
807 if ((*blob)->f == NULL) {
808 err = got_error_from_errno("fmemopen");
809 free(outbuf);
810 goto done;
812 (*blob)->data = outbuf;
813 } else {
814 if (fstat(outfd, &sb) == -1) {
815 err = got_error_from_errno("fstat");
816 goto done;
819 if (sb.st_size != size) {
820 err = got_error(GOT_ERR_PRIVSEP_LEN);
821 goto done;
824 dfd = dup(outfd);
825 if (dfd == -1) {
826 err = got_error_from_errno("dup");
827 goto done;
830 (*blob)->f = fdopen(dfd, "rb");
831 if ((*blob)->f == NULL) {
832 err = got_error_from_errno("fdopen");
833 close(dfd);
834 dfd = -1;
835 goto done;
839 (*blob)->hdrlen = hdrlen;
840 (*blob)->blocksize = blocksize;
841 memcpy(&(*blob)->id, id, sizeof(*id));
843 done:
844 free(path_packfile);
845 if (err) {
846 if (*blob) {
847 got_object_blob_close(*blob);
848 *blob = NULL;
851 return err;
854 const struct got_error *
855 got_object_open_as_blob(struct got_blob_object **blob,
856 struct got_repository *repo, struct got_object_id *id, size_t blocksize,
857 int outfd)
859 return open_blob(blob, repo, id, blocksize, outfd);
862 const struct got_error *
863 got_object_blob_open(struct got_blob_object **blob,
864 struct got_repository *repo, struct got_object *obj, size_t blocksize,
865 int outfd)
867 return open_blob(blob, repo, got_object_get_id(obj), blocksize, outfd);
870 static const struct got_error *
871 open_tag(struct got_tag_object **tag, struct got_repository *repo,
872 struct got_object_id *id, int check_cache)
874 const struct got_error *err = NULL;
875 struct got_packidx *packidx = NULL;
876 int idx;
877 char *path_packfile = NULL;
878 struct got_object *obj = NULL;
879 int obj_type = GOT_OBJ_TYPE_ANY;
881 if (check_cache) {
882 *tag = got_repo_get_cached_tag(repo, id);
883 if (*tag != NULL) {
884 (*tag)->refcnt++;
885 return NULL;
887 } else
888 *tag = NULL;
890 err = got_repo_search_packidx(&packidx, &idx, repo, id);
891 if (err == NULL) {
892 struct got_pack *pack = NULL;
893 uint8_t *buf = NULL;
894 size_t len;
896 err = got_packidx_get_packfile_path(&path_packfile,
897 packidx->path_packidx);
898 if (err)
899 return err;
901 pack = got_repo_get_cached_pack(repo, path_packfile);
902 if (pack == NULL) {
903 err = got_repo_cache_pack(&pack, repo, path_packfile,
904 packidx);
905 if (err)
906 goto done;
909 /* Beware of "lightweight" tags: Check object type first. */
910 err = got_packfile_open_object(&obj, pack, packidx, idx, id);
911 if (err)
912 goto done;
913 obj_type = obj->type;
914 if (obj_type != GOT_OBJ_TYPE_TAG) {
915 err = got_error(GOT_ERR_OBJ_TYPE);
916 got_object_close(obj);
917 goto done;
919 err = got_packfile_extract_object_to_mem(&buf, &len,
920 obj, pack);
921 got_object_close(obj);
922 if (err)
923 goto done;
924 err = got_object_parse_tag(tag, buf, len);
925 free(buf);
926 } else if (err->code == GOT_ERR_NO_OBJ) {
927 int fd;
929 err = got_object_open_loose_fd(&fd, id, repo);
930 if (err)
931 return err;
932 err = got_object_read_header(&obj, fd);
933 if (close(fd) == -1 && err == NULL)
934 err = got_error_from_errno("close");
935 if (err)
936 return err;
937 obj_type = obj->type;
938 got_object_close(obj);
939 if (obj_type != GOT_OBJ_TYPE_TAG)
940 return got_error(GOT_ERR_OBJ_TYPE);
942 err = got_object_open_loose_fd(&fd, id, repo);
943 if (err)
944 return err;
945 err = got_object_read_tag(tag, fd, id, 0);
946 if (close(fd) == -1 && err == NULL)
947 err = got_error_from_errno("close");
948 if (err)
949 return err;
952 if (err == NULL) {
953 (*tag)->refcnt++;
954 err = got_repo_cache_tag(repo, id, *tag);
955 if (err) {
956 if (err->code == GOT_ERR_OBJ_EXISTS ||
957 err->code == GOT_ERR_OBJ_TOO_LARGE)
958 err = NULL;
961 done:
962 free(path_packfile);
963 return err;
966 const struct got_error *
967 got_object_open_as_tag(struct got_tag_object **tag,
968 struct got_repository *repo, struct got_object_id *id)
970 *tag = got_repo_get_cached_tag(repo, id);
971 if (*tag != NULL) {
972 (*tag)->refcnt++;
973 return NULL;
976 return open_tag(tag, repo, id, 0);
979 const struct got_error *
980 got_object_tag_open(struct got_tag_object **tag,
981 struct got_repository *repo, struct got_object *obj)
983 return open_tag(tag, repo, got_object_get_id(obj), 1);