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 "got_compat.h"
19 #include <sys/queue.h>
20 #include <sys/stat.h>
22 #include <errno.h>
23 #include <limits.h>
24 #include <stdio.h>
25 #include <stdlib.h>
26 #include <string.h>
27 #include <unistd.h>
29 #include "got_error.h"
30 #include "got_object.h"
31 #include "got_repository.h"
32 #include "got_path.h"
34 #include "got_lib_delta.h"
35 #include "got_lib_object.h"
36 #include "got_lib_object_cache.h"
37 #include "got_lib_object_parse.h"
38 #include "got_lib_pack.h"
39 #include "got_lib_repository.h"
41 const struct got_error *
42 got_object_open_packed(struct got_object **obj, struct got_object_id *id,
43 struct got_repository *repo)
44 {
45 const struct got_error *err = NULL;
46 struct got_pack *pack = NULL;
47 struct got_packidx *packidx = NULL;
48 int idx;
49 char *path_packfile;
51 err = got_repo_search_packidx(&packidx, &idx, repo, id);
52 if (err)
53 return err;
55 err = got_packidx_get_packfile_path(&path_packfile,
56 packidx->path_packidx);
57 if (err)
58 return err;
60 pack = got_repo_get_cached_pack(repo, path_packfile);
61 if (pack == NULL) {
62 err = got_repo_cache_pack(&pack, repo, path_packfile, packidx);
63 if (err)
64 goto done;
65 }
67 err = got_packfile_open_object(obj, pack, packidx, idx, id);
68 if (err)
69 return err;
70 (*obj)->refcnt++;
72 err = got_repo_cache_object(repo, id, *obj);
73 if (err) {
74 if (err->code == GOT_ERR_OBJ_EXISTS ||
75 err->code == GOT_ERR_OBJ_TOO_LARGE)
76 err = NULL;
77 }
78 done:
79 free(path_packfile);
80 return err;
81 }
83 const struct got_error *
84 got_object_open_from_packfile(struct got_object **obj, struct got_object_id *id,
85 struct got_pack *pack, struct got_packidx *packidx, int obj_idx,
86 struct got_repository *repo)
87 {
88 const struct got_error *err;
90 *obj = got_repo_get_cached_object(repo, id);
91 if (*obj != NULL) {
92 (*obj)->refcnt++;
93 return NULL;
94 }
96 err = got_packfile_open_object(obj, pack, packidx, obj_idx, id);
97 if (err)
98 return err;
99 (*obj)->refcnt++;
101 err = got_repo_cache_object(repo, id, *obj);
102 if (err) {
103 if (err->code == GOT_ERR_OBJ_EXISTS ||
104 err->code == GOT_ERR_OBJ_TOO_LARGE)
105 err = NULL;
106 return err;
108 (*obj)->refcnt++;
109 return NULL;
112 const struct got_error *
113 got_object_read_raw_delta(uint64_t *base_size, uint64_t *result_size,
114 off_t *delta_size, off_t *delta_compressed_size, off_t *delta_offset,
115 off_t *delta_out_offset, struct got_object_id **base_id, int delta_cache_fd,
116 struct got_packidx *packidx, int obj_idx, struct got_object_id *id,
117 struct got_repository *repo)
119 return got_error(GOT_ERR_NOT_IMPL);
122 const struct got_error *
123 got_object_open(struct got_object **obj, struct got_repository *repo,
124 struct got_object_id *id)
126 const struct got_error *err = NULL;
127 int fd;
129 *obj = got_repo_get_cached_object(repo, id);
130 if (*obj != NULL) {
131 (*obj)->refcnt++;
132 return NULL;
135 err = got_object_open_packed(obj, id, repo);
136 if (err) {
137 if (err->code != GOT_ERR_NO_OBJ)
138 return err;
139 } else
140 return NULL;
142 err = got_object_open_loose_fd(&fd, id, repo);
143 if (err) {
144 if (err->code == GOT_ERR_ERRNO && errno == ENOENT)
145 err = got_error_no_obj(id);
146 return err;
149 err = got_object_read_header(obj, fd);
150 if (err)
151 goto done;
153 memcpy(&(*obj)->id, id, sizeof((*obj)->id));
154 (*obj)->refcnt++;
156 err = got_repo_cache_object(repo, id, *obj);
157 if (err) {
158 if (err->code == GOT_ERR_OBJ_EXISTS ||
159 err->code == GOT_ERR_OBJ_TOO_LARGE)
160 err = NULL;
162 done:
163 if (close(fd) == -1 && err == NULL)
164 err = got_error_from_errno("close");
165 return err;
168 static const struct got_error *
169 wrap_fd(FILE **f, int wrapped_fd)
171 const struct got_error *err = NULL;
172 int fd;
174 if (ftruncate(wrapped_fd, 0L) == -1)
175 return got_error_from_errno("ftruncate");
177 if (lseek(wrapped_fd, 0L, SEEK_SET) == -1)
178 return got_error_from_errno("lseek");
180 fd = dup(wrapped_fd);
181 if (fd == -1)
182 return got_error_from_errno("dup");
184 *f = fdopen(fd, "w+");
185 if (*f == NULL) {
186 err = got_error_from_errno("fdopen");
187 close(fd);
189 return err;
192 static const struct got_error *
193 read_packed_object_raw(uint8_t **outbuf, off_t *size, size_t *hdrlen,
194 int outfd, struct got_pack *pack, struct got_packidx *packidx, int idx,
195 struct got_object_id *id)
197 const struct got_error *err = NULL;
198 uint64_t raw_size = 0;
199 struct got_object *obj;
200 FILE *outfile = NULL, *basefile = NULL, *accumfile = NULL;
202 *outbuf = NULL;
203 *size = 0;
204 *hdrlen = 0;
206 err = got_packfile_open_object(&obj, pack, packidx, idx, id);
207 if (err)
208 return err;
210 if (obj->flags & GOT_OBJ_FLAG_DELTIFIED) {
211 err = got_pack_get_max_delta_object_size(&raw_size, obj, pack);
212 if (err)
213 goto done;
214 } else
215 raw_size = obj->size;
217 if (raw_size <= GOT_DELTA_RESULT_SIZE_CACHED_MAX) {
218 size_t len;
219 err = got_packfile_extract_object_to_mem(outbuf, &len,
220 obj, pack);
221 if (err)
222 goto done;
223 *size = (off_t)len;
224 } else {
225 /*
226 * XXX This uses 3 file extra descriptors for no good reason.
227 * We should have got_packfile_extract_object_to_fd().
228 */
229 err = wrap_fd(&outfile, outfd);
230 if (err)
231 goto done;
232 err = wrap_fd(&basefile, pack->basefd);
233 if (err)
234 goto done;
235 err = wrap_fd(&accumfile, pack->accumfd);
236 if (err)
237 goto done;
238 err = got_packfile_extract_object(pack, obj, outfile, basefile,
239 accumfile);
240 if (err)
241 goto done;
242 *size = obj->size;
245 *hdrlen = obj->hdrlen;
246 done:
247 got_object_close(obj);
248 if (outfile && fclose(outfile) == EOF && err == NULL)
249 err = got_error_from_errno("fclose");
250 if (basefile && fclose(basefile) == EOF && err == NULL)
251 err = got_error_from_errno("fclose");
252 if (accumfile && fclose(accumfile) == EOF && err == NULL)
253 err = got_error_from_errno("fclose");
254 return err;
258 static void
259 put_raw_object_tempfile(struct got_raw_object *obj)
261 struct got_repository *repo = obj->close_arg;
263 if (obj->tempfile_idx != -1)
264 got_repo_temp_fds_put(obj->tempfile_idx, repo);
267 /* *outfd must be initialized to -1 by caller */
268 const struct got_error *
269 got_object_raw_open(struct got_raw_object **obj, int *outfd,
270 struct got_repository *repo, struct got_object_id *id)
272 const struct got_error *err = NULL;
273 struct got_packidx *packidx = NULL;
274 int idx, tempfd, tempfile_idx;
275 uint8_t *outbuf = NULL;
276 off_t size = 0;
277 size_t hdrlen = 0;
278 char *path_packfile = NULL;
280 *obj = got_repo_get_cached_raw_object(repo, id);
281 if (*obj != NULL) {
282 (*obj)->refcnt++;
283 return NULL;
286 err = got_repo_temp_fds_get(&tempfd, &tempfile_idx, repo);
287 if (err)
288 return err;
290 err = got_repo_search_packidx(&packidx, &idx, repo, id);
291 if (err == NULL) {
292 struct got_pack *pack = NULL;
294 err = got_packidx_get_packfile_path(&path_packfile,
295 packidx->path_packidx);
296 if (err)
297 goto done;
299 pack = got_repo_get_cached_pack(repo, path_packfile);
300 if (pack == NULL) {
301 err = got_repo_cache_pack(&pack, repo, path_packfile,
302 packidx);
303 if (err)
304 goto done;
306 err = read_packed_object_raw(&outbuf, &size, &hdrlen,
307 tempfd, pack, packidx, idx, id);
308 if (err)
309 goto done;
310 } else if (err->code == GOT_ERR_NO_OBJ) {
311 int fd;
313 err = got_object_open_loose_fd(&fd, id, repo);
314 if (err)
315 goto done;
316 err = got_object_read_raw(&outbuf, &size, &hdrlen,
317 GOT_DELTA_RESULT_SIZE_CACHED_MAX, tempfd, id, fd);
318 if (close(fd) == -1 && err == NULL)
319 err = got_error_from_errno("close");
320 if (err)
321 goto done;
324 if (outbuf == NULL) {
325 if (*outfd != -1) {
326 err = got_error_msg(GOT_ERR_NOT_IMPL, "bad outfd");
327 goto done;
330 /*
331 * Duplicate tempfile descriptor to allow use of
332 * fdopen(3) inside got_object_raw_alloc().
333 */
334 *outfd = dup(tempfd);
335 if (*outfd == -1) {
336 err = got_error_from_errno("dup");
337 goto done;
341 err = got_object_raw_alloc(obj, outbuf, outfd,
342 GOT_DELTA_RESULT_SIZE_CACHED_MAX, hdrlen, size);
343 if (err)
344 goto done;
346 err = got_repo_cache_raw_object(repo, id, *obj);
347 if (err) {
348 if (err->code == GOT_ERR_OBJ_EXISTS ||
349 err->code == GOT_ERR_OBJ_TOO_LARGE)
350 err = NULL;
352 done:
353 free(path_packfile);
354 if (err) {
355 if (*obj) {
356 got_object_raw_close(*obj);
357 *obj = NULL;
359 free(outbuf);
360 got_repo_temp_fds_put(tempfile_idx, repo);
361 if (*outfd != -1) {
362 close(*outfd);
363 *outfd = -1;
365 } else {
366 if (((*obj)->f == NULL && (*obj)->fd == -1)) {
367 /* This raw object is not backed by a file. */
368 got_repo_temp_fds_put(tempfile_idx, repo);
369 if (*outfd != -1) {
370 close(*outfd);
371 *outfd = -1;
373 } else {
374 (*obj)->tempfile_idx = tempfile_idx;
375 (*obj)->close_cb = put_raw_object_tempfile;
376 (*obj)->close_arg = repo;
379 return err;
382 static const struct got_error *
383 open_commit(struct got_commit_object **commit,
384 struct got_repository *repo, struct got_object_id *id, int check_cache)
386 const struct got_error *err = NULL;
387 struct got_packidx *packidx = NULL;
388 int idx;
389 char *path_packfile = NULL;
391 if (check_cache) {
392 *commit = got_repo_get_cached_commit(repo, id);
393 if (*commit != NULL) {
394 (*commit)->refcnt++;
395 return NULL;
397 } else
398 *commit = NULL;
400 err = got_repo_search_packidx(&packidx, &idx, repo, id);
401 if (err == NULL) {
402 struct got_pack *pack = NULL;
403 struct got_object *obj;
404 uint8_t *buf;
405 size_t len;
407 err = got_packidx_get_packfile_path(&path_packfile,
408 packidx->path_packidx);
409 if (err)
410 return err;
412 pack = got_repo_get_cached_pack(repo, path_packfile);
413 if (pack == NULL) {
414 err = got_repo_cache_pack(&pack, repo, path_packfile,
415 packidx);
416 if (err)
417 goto done;
419 err = got_packfile_open_object(&obj, pack, packidx, idx, id);
420 if (err)
421 goto done;
422 err = got_packfile_extract_object_to_mem(&buf, &len,
423 obj, pack);
424 got_object_close(obj);
425 if (err)
426 goto done;
427 err = got_object_parse_commit(commit, buf, len);
428 free(buf);
429 } else if (err->code == GOT_ERR_NO_OBJ) {
430 int fd;
432 err = got_object_open_loose_fd(&fd, id, repo);
433 if (err)
434 return err;
435 err = got_object_read_commit(commit, fd, id, 0);
436 if (close(fd) == -1 && err == NULL)
437 err = got_error_from_errno("close");
438 if (err)
439 return err;
442 if (err == NULL) {
443 (*commit)->refcnt++;
444 err = got_repo_cache_commit(repo, id, *commit);
445 if (err) {
446 if (err->code == GOT_ERR_OBJ_EXISTS ||
447 err->code == GOT_ERR_OBJ_TOO_LARGE)
448 err = NULL;
451 done:
452 free(path_packfile);
453 return err;
456 const struct got_error *
457 got_object_open_as_commit(struct got_commit_object **commit,
458 struct got_repository *repo, struct got_object_id *id)
460 *commit = got_repo_get_cached_commit(repo, id);
461 if (*commit != NULL) {
462 (*commit)->refcnt++;
463 return NULL;
466 return open_commit(commit, repo, id, 0);
469 const struct got_error *
470 got_object_commit_open(struct got_commit_object **commit,
471 struct got_repository *repo, struct got_object *obj)
473 return open_commit(commit, repo, got_object_get_id(obj), 1);
476 static const struct got_error *
477 open_tree(struct got_tree_object **tree,
478 struct got_repository *repo, struct got_object_id *id, int check_cache)
480 const struct got_error *err = NULL;
481 struct got_packidx *packidx = NULL;
482 int idx;
483 char *path_packfile = NULL;
484 struct got_parsed_tree_entry *entries = NULL;
485 size_t nentries = 0, nentries_alloc = 0, i;
486 uint8_t *buf = NULL;
488 if (check_cache) {
489 *tree = got_repo_get_cached_tree(repo, id);
490 if (*tree != NULL) {
491 (*tree)->refcnt++;
492 return NULL;
494 } else
495 *tree = NULL;
497 err = got_repo_search_packidx(&packidx, &idx, repo, id);
498 if (err == NULL) {
499 struct got_pack *pack = NULL;
500 struct got_object *obj;
501 size_t len;
503 err = got_packidx_get_packfile_path(&path_packfile,
504 packidx->path_packidx);
505 if (err)
506 return err;
508 pack = got_repo_get_cached_pack(repo, path_packfile);
509 if (pack == NULL) {
510 err = got_repo_cache_pack(&pack, repo, path_packfile,
511 packidx);
512 if (err)
513 goto done;
515 err = got_packfile_open_object(&obj, pack, packidx, idx, id);
516 if (err)
517 goto done;
518 err = got_packfile_extract_object_to_mem(&buf, &len,
519 obj, pack);
520 got_object_close(obj);
521 if (err)
522 goto done;
523 err = got_object_parse_tree(&entries, &nentries,
524 &nentries_alloc, buf, len);
525 if (err)
526 goto done;
527 } else if (err->code == GOT_ERR_NO_OBJ) {
528 int fd;
530 err = got_object_open_loose_fd(&fd, id, repo);
531 if (err)
532 return err;
533 err = got_object_read_tree(&entries, &nentries,
534 &nentries_alloc, &buf, fd, id);
535 if (close(fd) == -1 && err == NULL)
536 err = got_error_from_errno("close");
537 if (err)
538 goto done;
539 } else
540 goto done;
542 *tree = malloc(sizeof(**tree));
543 if (*tree == NULL) {
544 err = got_error_from_errno("malloc");
545 goto done;
547 (*tree)->entries = calloc(nentries, sizeof(struct got_tree_entry));
548 if ((*tree)->entries == NULL) {
549 err = got_error_from_errno("malloc");
550 goto done;
552 (*tree)->nentries = nentries;
553 (*tree)->refcnt = 0;
555 for (i = 0; i < nentries; i++) {
556 struct got_parsed_tree_entry *pe = &entries[i];
557 struct got_tree_entry *te = &(*tree)->entries[i];
559 if (strlcpy(te->name, pe->name,
560 sizeof(te->name)) >= sizeof(te->name)) {
561 err = got_error(GOT_ERR_NO_SPACE);
562 goto done;
564 memcpy(te->id.sha1, pe->id, SHA1_DIGEST_LENGTH);
565 te->mode = pe->mode;
566 te->idx = i;
568 done:
569 free(path_packfile);
570 free(entries);
571 free(buf);
572 if (err == NULL) {
573 (*tree)->refcnt++;
574 err = got_repo_cache_tree(repo, id, *tree);
575 if (err) {
576 if (err->code == GOT_ERR_OBJ_EXISTS ||
577 err->code == GOT_ERR_OBJ_TOO_LARGE)
578 err = NULL;
581 if (err) {
582 if (*tree)
583 free((*tree)->entries);
584 free(*tree);
585 *tree = NULL;
587 return err;
590 const struct got_error *
591 got_object_open_as_tree(struct got_tree_object **tree,
592 struct got_repository *repo, struct got_object_id *id)
594 *tree = got_repo_get_cached_tree(repo, id);
595 if (*tree != NULL) {
596 (*tree)->refcnt++;
597 return NULL;
600 return open_tree(tree, repo, id, 0);
603 const struct got_error *
604 got_object_tree_open(struct got_tree_object **tree,
605 struct got_repository *repo, struct got_object *obj)
607 return open_tree(tree, repo, got_object_get_id(obj), 1);
610 const struct got_error *
611 got_object_open_as_blob(struct got_blob_object **blob,
612 struct got_repository *repo, struct got_object_id *id, size_t blocksize,
613 int outfd)
615 return got_error(GOT_ERR_NOT_IMPL);
618 const struct got_error *
619 got_object_blob_open(struct got_blob_object **blob,
620 struct got_repository *repo, struct got_object *obj, size_t blocksize,
621 int outfd)
623 return got_error(GOT_ERR_NOT_IMPL);
626 static const struct got_error *
627 open_tag(struct got_tag_object **tag, struct got_repository *repo,
628 struct got_object_id *id, int check_cache)
630 const struct got_error *err = NULL;
631 struct got_packidx *packidx = NULL;
632 int idx;
633 char *path_packfile = NULL;
634 struct got_object *obj = NULL;
635 int obj_type = GOT_OBJ_TYPE_ANY;
637 if (check_cache) {
638 *tag = got_repo_get_cached_tag(repo, id);
639 if (*tag != NULL) {
640 (*tag)->refcnt++;
641 return NULL;
643 } else
644 *tag = NULL;
646 err = got_repo_search_packidx(&packidx, &idx, repo, id);
647 if (err == NULL) {
648 struct got_pack *pack = NULL;
649 uint8_t *buf = NULL;
650 size_t len;
652 err = got_packidx_get_packfile_path(&path_packfile,
653 packidx->path_packidx);
654 if (err)
655 return err;
657 pack = got_repo_get_cached_pack(repo, path_packfile);
658 if (pack == NULL) {
659 err = got_repo_cache_pack(&pack, repo, path_packfile,
660 packidx);
661 if (err)
662 goto done;
665 /* Beware of "lightweight" tags: Check object type first. */
666 err = got_packfile_open_object(&obj, pack, packidx, idx, id);
667 if (err)
668 goto done;
669 obj_type = obj->type;
670 if (obj_type != GOT_OBJ_TYPE_TAG) {
671 err = got_error(GOT_ERR_OBJ_TYPE);
672 got_object_close(obj);
673 goto done;
675 err = got_packfile_extract_object_to_mem(&buf, &len,
676 obj, pack);
677 got_object_close(obj);
678 if (err)
679 goto done;
680 err = got_object_parse_tag(tag, buf, len);
681 free(buf);
682 } else if (err->code == GOT_ERR_NO_OBJ) {
683 int fd;
685 err = got_object_open_loose_fd(&fd, id, repo);
686 if (err)
687 return err;
688 err = got_object_read_header(&obj, fd);
689 if (close(fd) == -1 && err == NULL)
690 err = got_error_from_errno("close");
691 if (err)
692 return err;
693 obj_type = obj->type;
694 got_object_close(obj);
695 if (obj_type != GOT_OBJ_TYPE_TAG)
696 return got_error(GOT_ERR_OBJ_TYPE);
698 err = got_object_open_loose_fd(&fd, id, repo);
699 if (err)
700 return err;
701 err = got_object_read_tag(tag, fd, id, 0);
702 if (close(fd) == -1 && err == NULL)
703 err = got_error_from_errno("close");
704 if (err)
705 return err;
708 if (err == NULL) {
709 (*tag)->refcnt++;
710 err = got_repo_cache_tag(repo, id, *tag);
711 if (err) {
712 if (err->code == GOT_ERR_OBJ_EXISTS ||
713 err->code == GOT_ERR_OBJ_TOO_LARGE)
714 err = NULL;
717 done:
718 free(path_packfile);
719 return err;
722 const struct got_error *
723 got_object_open_as_tag(struct got_tag_object **tag,
724 struct got_repository *repo, struct got_object_id *id)
726 *tag = got_repo_get_cached_tag(repo, id);
727 if (*tag != NULL) {
728 (*tag)->refcnt++;
729 return NULL;
732 return open_tag(tag, repo, id, 0);
735 const struct got_error *
736 got_object_tag_open(struct got_tag_object **tag,
737 struct got_repository *repo, struct got_object *obj)
739 return open_tag(tag, repo, got_object_get_id(obj), 1);