2 * Copyright (c) 2022 Stefan Sperling <stsp@openbsd.org>
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.
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.
17 #include <sys/queue.h>
29 #include "got_error.h"
30 #include "got_object.h"
31 #include "got_repository.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)
45 const struct got_error *err = NULL;
46 struct got_pack *pack = NULL;
47 struct got_packidx *packidx = NULL;
51 err = got_repo_search_packidx(&packidx, &idx, repo, id);
55 err = got_packidx_get_packfile_path(&path_packfile,
56 packidx->path_packidx);
60 pack = got_repo_get_cached_pack(repo, path_packfile);
62 err = got_repo_cache_pack(&pack, repo, path_packfile, packidx);
67 err = got_packfile_open_object(obj, pack, packidx, idx, id);
72 err = got_repo_cache_object(repo, id, *obj);
74 if (err->code == GOT_ERR_OBJ_EXISTS ||
75 err->code == GOT_ERR_OBJ_TOO_LARGE)
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)
88 return got_error(GOT_ERR_NOT_IMPL);
91 const struct got_error *
92 got_object_read_raw_delta(uint64_t *base_size, uint64_t *result_size,
93 off_t *delta_size, off_t *delta_compressed_size, off_t *delta_offset,
94 off_t *delta_out_offset, struct got_object_id **base_id, int delta_cache_fd,
95 struct got_packidx *packidx, int obj_idx, struct got_object_id *id,
96 struct got_repository *repo)
98 return got_error(GOT_ERR_NOT_IMPL);
101 const struct got_error *
102 got_object_open(struct got_object **obj, struct got_repository *repo,
103 struct got_object_id *id)
105 const struct got_error *err = NULL;
108 *obj = got_repo_get_cached_object(repo, id);
114 err = got_object_open_packed(obj, id, repo);
116 if (err->code != GOT_ERR_NO_OBJ)
121 err = got_object_open_loose_fd(&fd, id, repo);
123 if (err->code == GOT_ERR_ERRNO && errno == ENOENT)
124 err = got_error_no_obj(id);
128 err = got_object_read_header(obj, fd);
132 memcpy(&(*obj)->id, id, sizeof((*obj)->id));
135 err = got_repo_cache_object(repo, id, *obj);
137 if (err->code == GOT_ERR_OBJ_EXISTS ||
138 err->code == GOT_ERR_OBJ_TOO_LARGE)
142 if (close(fd) == -1 && err == NULL)
143 err = got_error_from_errno("close");
147 static const struct got_error *
148 wrap_fd(FILE **f, int wrapped_fd)
150 const struct got_error *err = NULL;
153 if (ftruncate(wrapped_fd, 0L) == -1)
154 return got_error_from_errno("ftruncate");
156 if (lseek(wrapped_fd, 0L, SEEK_SET) == -1)
157 return got_error_from_errno("lseek");
159 fd = dup(wrapped_fd);
161 return got_error_from_errno("dup");
163 *f = fdopen(fd, "w+");
165 err = got_error_from_errno("fdopen");
171 static const struct got_error *
172 read_packed_object_raw(uint8_t **outbuf, off_t *size, size_t *hdrlen,
173 int outfd, struct got_pack *pack, struct got_packidx *packidx, int idx,
174 struct got_object_id *id)
176 const struct got_error *err = NULL;
177 uint64_t raw_size = 0;
178 struct got_object *obj;
179 FILE *outfile = NULL, *basefile = NULL, *accumfile = NULL;
185 err = got_packfile_open_object(&obj, pack, packidx, idx, id);
189 if (obj->flags & GOT_OBJ_FLAG_DELTIFIED) {
190 err = got_pack_get_max_delta_object_size(&raw_size, obj, pack);
194 raw_size = obj->size;
196 if (raw_size <= GOT_DELTA_RESULT_SIZE_CACHED_MAX) {
198 err = got_packfile_extract_object_to_mem(outbuf, &len,
205 * XXX This uses 3 file extra descriptors for no good reason.
206 * We should have got_packfile_extract_object_to_fd().
208 err = wrap_fd(&outfile, outfd);
211 err = wrap_fd(&basefile, pack->basefd);
214 err = wrap_fd(&accumfile, pack->accumfd);
217 err = got_packfile_extract_object(pack, obj, outfile, basefile,
224 *hdrlen = obj->hdrlen;
226 got_object_close(obj);
227 if (outfile && fclose(outfile) == EOF && err == NULL)
228 err = got_error_from_errno("fclose");
229 if (basefile && fclose(basefile) == EOF && err == NULL)
230 err = got_error_from_errno("fclose");
231 if (accumfile && fclose(accumfile) == EOF && err == NULL)
232 err = got_error_from_errno("fclose");
238 put_raw_object_tempfile(struct got_raw_object *obj)
240 struct got_repository *repo = obj->close_arg;
242 if (obj->tempfile_idx != -1)
243 got_repo_temp_fds_put(obj->tempfile_idx, repo);
246 /* *outfd must be initialized to -1 by caller */
247 const struct got_error *
248 got_object_raw_open(struct got_raw_object **obj, int *outfd,
249 struct got_repository *repo, struct got_object_id *id)
251 const struct got_error *err = NULL;
252 struct got_packidx *packidx = NULL;
253 int idx, tempfd, tempfile_idx;
254 uint8_t *outbuf = NULL;
257 char *path_packfile = NULL;
259 *obj = got_repo_get_cached_raw_object(repo, id);
265 err = got_repo_temp_fds_get(&tempfd, &tempfile_idx, repo);
269 err = got_repo_search_packidx(&packidx, &idx, repo, id);
271 struct got_pack *pack = NULL;
273 err = got_packidx_get_packfile_path(&path_packfile,
274 packidx->path_packidx);
278 pack = got_repo_get_cached_pack(repo, path_packfile);
280 err = got_repo_cache_pack(&pack, repo, path_packfile,
285 err = read_packed_object_raw(&outbuf, &size, &hdrlen,
286 tempfd, pack, packidx, idx, id);
289 } else if (err->code == GOT_ERR_NO_OBJ) {
292 err = got_object_open_loose_fd(&fd, id, repo);
295 err = got_object_read_raw(&outbuf, &size, &hdrlen,
296 GOT_DELTA_RESULT_SIZE_CACHED_MAX, tempfd, id, fd);
297 if (close(fd) == -1 && err == NULL)
298 err = got_error_from_errno("close");
303 if (outbuf == NULL) {
305 err = got_error_msg(GOT_ERR_NOT_IMPL, "bad outfd");
310 * Duplicate tempfile descriptor to allow use of
311 * fdopen(3) inside got_object_raw_alloc().
313 *outfd = dup(tempfd);
315 err = got_error_from_errno("dup");
320 err = got_object_raw_alloc(obj, outbuf, outfd,
321 GOT_DELTA_RESULT_SIZE_CACHED_MAX, hdrlen, size);
325 err = got_repo_cache_raw_object(repo, id, *obj);
327 if (err->code == GOT_ERR_OBJ_EXISTS ||
328 err->code == GOT_ERR_OBJ_TOO_LARGE)
335 got_object_raw_close(*obj);
339 got_repo_temp_fds_put(tempfile_idx, repo);
345 if (((*obj)->f == NULL && (*obj)->fd == -1)) {
346 /* This raw object is not backed by a file. */
347 got_repo_temp_fds_put(tempfile_idx, repo);
353 (*obj)->tempfile_idx = tempfile_idx;
354 (*obj)->close_cb = put_raw_object_tempfile;
355 (*obj)->close_arg = repo;
361 static const struct got_error *
362 open_commit(struct got_commit_object **commit,
363 struct got_repository *repo, struct got_object_id *id, int check_cache)
365 const struct got_error *err = NULL;
366 struct got_packidx *packidx = NULL;
368 char *path_packfile = NULL;
371 *commit = got_repo_get_cached_commit(repo, id);
372 if (*commit != NULL) {
379 err = got_repo_search_packidx(&packidx, &idx, repo, id);
381 struct got_pack *pack = NULL;
382 struct got_object *obj;
386 err = got_packidx_get_packfile_path(&path_packfile,
387 packidx->path_packidx);
391 pack = got_repo_get_cached_pack(repo, path_packfile);
393 err = got_repo_cache_pack(&pack, repo, path_packfile,
398 err = got_packfile_open_object(&obj, pack, packidx, idx, id);
401 err = got_packfile_extract_object_to_mem(&buf, &len,
403 got_object_close(obj);
406 err = got_object_parse_commit(commit, buf, len);
408 } else if (err->code == GOT_ERR_NO_OBJ) {
411 err = got_object_open_loose_fd(&fd, id, repo);
414 err = got_object_read_commit(commit, fd, id, 0);
415 if (close(fd) == -1 && err == NULL)
416 err = got_error_from_errno("close");
423 err = got_repo_cache_commit(repo, id, *commit);
425 if (err->code == GOT_ERR_OBJ_EXISTS ||
426 err->code == GOT_ERR_OBJ_TOO_LARGE)
435 const struct got_error *
436 got_object_open_as_commit(struct got_commit_object **commit,
437 struct got_repository *repo, struct got_object_id *id)
439 *commit = got_repo_get_cached_commit(repo, id);
440 if (*commit != NULL) {
445 return open_commit(commit, repo, id, 0);
448 const struct got_error *
449 got_object_commit_open(struct got_commit_object **commit,
450 struct got_repository *repo, struct got_object *obj)
452 return open_commit(commit, repo, got_object_get_id(obj), 1);
455 static const struct got_error *
456 open_tree(struct got_tree_object **tree,
457 struct got_repository *repo, struct got_object_id *id, int check_cache)
459 const struct got_error *err = NULL;
460 struct got_packidx *packidx = NULL;
462 char *path_packfile = NULL;
463 struct got_parsed_tree_entry *entries = NULL;
464 size_t nentries = 0, nentries_alloc = 0, i;
468 *tree = got_repo_get_cached_tree(repo, id);
476 err = got_repo_search_packidx(&packidx, &idx, repo, id);
478 struct got_pack *pack = NULL;
479 struct got_object *obj;
482 err = got_packidx_get_packfile_path(&path_packfile,
483 packidx->path_packidx);
487 pack = got_repo_get_cached_pack(repo, path_packfile);
489 err = got_repo_cache_pack(&pack, repo, path_packfile,
494 err = got_packfile_open_object(&obj, pack, packidx, idx, id);
497 err = got_packfile_extract_object_to_mem(&buf, &len,
499 got_object_close(obj);
502 err = got_object_parse_tree(&entries, &nentries,
503 &nentries_alloc, buf, len);
506 } else if (err->code == GOT_ERR_NO_OBJ) {
509 err = got_object_open_loose_fd(&fd, id, repo);
512 err = got_object_read_tree(&entries, &nentries,
513 &nentries_alloc, &buf, fd, id);
514 if (close(fd) == -1 && err == NULL)
515 err = got_error_from_errno("close");
521 *tree = malloc(sizeof(**tree));
523 err = got_error_from_errno("malloc");
526 (*tree)->entries = calloc(nentries, sizeof(struct got_tree_entry));
527 if ((*tree)->entries == NULL) {
528 err = got_error_from_errno("malloc");
531 (*tree)->nentries = nentries;
534 for (i = 0; i < nentries; i++) {
535 struct got_parsed_tree_entry *pe = &entries[i];
536 struct got_tree_entry *te = &(*tree)->entries[i];
538 if (strlcpy(te->name, pe->name,
539 sizeof(te->name)) >= sizeof(te->name)) {
540 err = got_error(GOT_ERR_NO_SPACE);
543 memcpy(te->id.sha1, pe->id, SHA1_DIGEST_LENGTH);
553 err = got_repo_cache_tree(repo, id, *tree);
555 if (err->code == GOT_ERR_OBJ_EXISTS ||
556 err->code == GOT_ERR_OBJ_TOO_LARGE)
562 free((*tree)->entries);
569 const struct got_error *
570 got_object_open_as_tree(struct got_tree_object **tree,
571 struct got_repository *repo, struct got_object_id *id)
573 *tree = got_repo_get_cached_tree(repo, id);
579 return open_tree(tree, repo, id, 0);
582 const struct got_error *
583 got_object_tree_open(struct got_tree_object **tree,
584 struct got_repository *repo, struct got_object *obj)
586 return open_tree(tree, repo, got_object_get_id(obj), 1);
589 const struct got_error *
590 got_object_open_as_blob(struct got_blob_object **blob,
591 struct got_repository *repo, struct got_object_id *id, size_t blocksize,
594 return got_error(GOT_ERR_NOT_IMPL);
597 const struct got_error *
598 got_object_blob_open(struct got_blob_object **blob,
599 struct got_repository *repo, struct got_object *obj, size_t blocksize,
602 return got_error(GOT_ERR_NOT_IMPL);
605 static const struct got_error *
606 open_tag(struct got_tag_object **tag, struct got_repository *repo,
607 struct got_object_id *id, int check_cache)
609 const struct got_error *err = NULL;
610 struct got_packidx *packidx = NULL;
612 char *path_packfile = NULL;
613 struct got_object *obj = NULL;
614 int obj_type = GOT_OBJ_TYPE_ANY;
617 *tag = got_repo_get_cached_tag(repo, id);
625 err = got_repo_search_packidx(&packidx, &idx, repo, id);
627 struct got_pack *pack = NULL;
631 err = got_packidx_get_packfile_path(&path_packfile,
632 packidx->path_packidx);
636 pack = got_repo_get_cached_pack(repo, path_packfile);
638 err = got_repo_cache_pack(&pack, repo, path_packfile,
644 /* Beware of "lightweight" tags: Check object type first. */
645 err = got_packfile_open_object(&obj, pack, packidx, idx, id);
648 obj_type = obj->type;
649 if (obj_type != GOT_OBJ_TYPE_TAG) {
650 err = got_error(GOT_ERR_OBJ_TYPE);
651 got_object_close(obj);
654 err = got_packfile_extract_object_to_mem(&buf, &len,
656 got_object_close(obj);
659 err = got_object_parse_tag(tag, buf, len);
661 } else if (err->code == GOT_ERR_NO_OBJ) {
664 err = got_object_open_loose_fd(&fd, id, repo);
667 err = got_object_read_header(&obj, fd);
668 if (close(fd) == -1 && err == NULL)
669 err = got_error_from_errno("close");
672 obj_type = obj->type;
673 got_object_close(obj);
674 if (obj_type != GOT_OBJ_TYPE_TAG)
675 return got_error(GOT_ERR_OBJ_TYPE);
677 err = got_object_open_loose_fd(&fd, id, repo);
680 err = got_object_read_tag(tag, fd, id, 0);
681 if (close(fd) == -1 && err == NULL)
682 err = got_error_from_errno("close");
689 err = got_repo_cache_tag(repo, id, *tag);
691 if (err->code == GOT_ERR_OBJ_EXISTS ||
692 err->code == GOT_ERR_OBJ_TOO_LARGE)
701 const struct got_error *
702 got_object_open_as_tag(struct got_tag_object **tag,
703 struct got_repository *repo, struct got_object_id *id)
705 *tag = got_repo_get_cached_tag(repo, id);
711 return open_tag(tag, repo, id, 0);
714 const struct got_error *
715 got_object_tag_open(struct got_tag_object **tag,
716 struct got_repository *repo, struct got_object *obj)
718 return open_tag(tag, repo, got_object_get_id(obj), 1);