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>
30 #include "got_error.h"
31 #include "got_object.h"
32 #include "got_repository.h"
35 #include "got_lib_delta.h"
36 #include "got_lib_object.h"
37 #include "got_lib_object_cache.h"
38 #include "got_lib_object_parse.h"
39 #include "got_lib_pack.h"
40 #include "got_lib_repository.h"
42 const struct got_error *
43 got_object_open_packed(struct got_object **obj, struct got_object_id *id,
44 struct got_repository *repo)
46 const struct got_error *err = NULL;
47 struct got_pack *pack = NULL;
48 struct got_packidx *packidx = NULL;
52 err = got_repo_search_packidx(&packidx, &idx, repo, id);
56 err = got_packidx_get_packfile_path(&path_packfile,
57 packidx->path_packidx);
61 pack = got_repo_get_cached_pack(repo, path_packfile);
63 err = got_repo_cache_pack(&pack, repo, path_packfile, packidx);
68 err = got_packfile_open_object(obj, pack, packidx, idx, id);
73 err = got_repo_cache_object(repo, id, *obj);
75 if (err->code == GOT_ERR_OBJ_EXISTS ||
76 err->code == GOT_ERR_OBJ_TOO_LARGE)
84 const struct got_error *
85 got_object_open_from_packfile(struct got_object **obj, struct got_object_id *id,
86 struct got_pack *pack, struct got_packidx *packidx, int obj_idx,
87 struct got_repository *repo)
89 const struct got_error *err;
91 *obj = got_repo_get_cached_object(repo, id);
97 err = got_packfile_open_object(obj, pack, packidx, obj_idx, id);
102 err = got_repo_cache_object(repo, id, *obj);
104 if (err->code == GOT_ERR_OBJ_EXISTS ||
105 err->code == GOT_ERR_OBJ_TOO_LARGE)
113 const struct got_error *
114 got_object_read_raw_delta(uint64_t *base_size, uint64_t *result_size,
115 off_t *delta_size, off_t *delta_compressed_size, off_t *delta_offset,
116 off_t *delta_out_offset, struct got_object_id **base_id, int delta_cache_fd,
117 struct got_packidx *packidx, int obj_idx, struct got_object_id *id,
118 struct got_repository *repo)
120 return got_error(GOT_ERR_NOT_IMPL);
123 const struct got_error *
124 got_object_open(struct got_object **obj, struct got_repository *repo,
125 struct got_object_id *id)
127 const struct got_error *err = NULL;
130 *obj = got_repo_get_cached_object(repo, id);
136 err = got_object_open_packed(obj, id, repo);
138 if (err->code != GOT_ERR_NO_OBJ)
143 err = got_object_open_loose_fd(&fd, id, repo);
145 if (err->code == GOT_ERR_ERRNO && errno == ENOENT)
146 err = got_error_no_obj(id);
150 err = got_object_read_header(obj, fd);
154 memcpy(&(*obj)->id, id, sizeof((*obj)->id));
157 err = got_repo_cache_object(repo, id, *obj);
159 if (err->code == GOT_ERR_OBJ_EXISTS ||
160 err->code == GOT_ERR_OBJ_TOO_LARGE)
164 if (close(fd) == -1 && err == NULL)
165 err = got_error_from_errno("close");
169 static const struct got_error *
170 wrap_fd(FILE **f, int wrapped_fd)
172 const struct got_error *err = NULL;
175 if (ftruncate(wrapped_fd, 0L) == -1)
176 return got_error_from_errno("ftruncate");
178 if (lseek(wrapped_fd, 0L, SEEK_SET) == -1)
179 return got_error_from_errno("lseek");
181 fd = dup(wrapped_fd);
183 return got_error_from_errno("dup");
185 *f = fdopen(fd, "w+");
187 err = got_error_from_errno("fdopen");
193 static const struct got_error *
194 read_packed_object_raw(uint8_t **outbuf, off_t *size, size_t *hdrlen,
195 int outfd, struct got_pack *pack, struct got_packidx *packidx, int idx,
196 struct got_object_id *id)
198 const struct got_error *err = NULL;
199 uint64_t raw_size = 0;
200 struct got_object *obj;
201 FILE *outfile = NULL, *basefile = NULL, *accumfile = NULL;
207 err = got_packfile_open_object(&obj, pack, packidx, idx, id);
211 if (obj->flags & GOT_OBJ_FLAG_DELTIFIED) {
212 err = got_pack_get_max_delta_object_size(&raw_size, obj, pack);
216 raw_size = obj->size;
218 if (raw_size <= GOT_DELTA_RESULT_SIZE_CACHED_MAX) {
220 err = got_packfile_extract_object_to_mem(outbuf, &len,
227 * XXX This uses 3 file extra descriptors for no good reason.
228 * We should have got_packfile_extract_object_to_fd().
230 err = wrap_fd(&outfile, outfd);
233 err = wrap_fd(&basefile, pack->basefd);
236 err = wrap_fd(&accumfile, pack->accumfd);
239 err = got_packfile_extract_object(pack, obj, outfile, basefile,
246 *hdrlen = obj->hdrlen;
248 got_object_close(obj);
249 if (outfile && fclose(outfile) == EOF && err == NULL)
250 err = got_error_from_errno("fclose");
251 if (basefile && fclose(basefile) == EOF && err == NULL)
252 err = got_error_from_errno("fclose");
253 if (accumfile && fclose(accumfile) == EOF && err == NULL)
254 err = got_error_from_errno("fclose");
260 put_raw_object_tempfile(struct got_raw_object *obj)
262 struct got_repository *repo = obj->close_arg;
264 if (obj->tempfile_idx != -1)
265 got_repo_temp_fds_put(obj->tempfile_idx, repo);
268 /* *outfd must be initialized to -1 by caller */
269 const struct got_error *
270 got_object_raw_open(struct got_raw_object **obj, int *outfd,
271 struct got_repository *repo, struct got_object_id *id)
273 const struct got_error *err = NULL;
274 struct got_packidx *packidx = NULL;
275 int idx, tempfd, tempfile_idx;
276 uint8_t *outbuf = NULL;
279 char *path_packfile = NULL;
281 *obj = got_repo_get_cached_raw_object(repo, id);
287 err = got_repo_temp_fds_get(&tempfd, &tempfile_idx, repo);
291 err = got_repo_search_packidx(&packidx, &idx, repo, id);
293 struct got_pack *pack = NULL;
295 err = got_packidx_get_packfile_path(&path_packfile,
296 packidx->path_packidx);
300 pack = got_repo_get_cached_pack(repo, path_packfile);
302 err = got_repo_cache_pack(&pack, repo, path_packfile,
307 err = read_packed_object_raw(&outbuf, &size, &hdrlen,
308 tempfd, pack, packidx, idx, id);
311 } else if (err->code == GOT_ERR_NO_OBJ) {
314 err = got_object_open_loose_fd(&fd, id, repo);
317 err = got_object_read_raw(&outbuf, &size, &hdrlen,
318 GOT_DELTA_RESULT_SIZE_CACHED_MAX, tempfd, id, fd);
319 if (close(fd) == -1 && err == NULL)
320 err = got_error_from_errno("close");
325 if (outbuf == NULL) {
327 err = got_error_msg(GOT_ERR_NOT_IMPL, "bad outfd");
332 * Duplicate tempfile descriptor to allow use of
333 * fdopen(3) inside got_object_raw_alloc().
335 *outfd = dup(tempfd);
337 err = got_error_from_errno("dup");
342 err = got_object_raw_alloc(obj, outbuf, outfd,
343 GOT_DELTA_RESULT_SIZE_CACHED_MAX, hdrlen, size);
347 err = got_repo_cache_raw_object(repo, id, *obj);
349 if (err->code == GOT_ERR_OBJ_EXISTS ||
350 err->code == GOT_ERR_OBJ_TOO_LARGE)
357 got_object_raw_close(*obj);
361 got_repo_temp_fds_put(tempfile_idx, repo);
367 if (((*obj)->f == NULL && (*obj)->fd == -1)) {
368 /* This raw object is not backed by a file. */
369 got_repo_temp_fds_put(tempfile_idx, repo);
375 (*obj)->tempfile_idx = tempfile_idx;
376 (*obj)->close_cb = put_raw_object_tempfile;
377 (*obj)->close_arg = repo;
383 static const struct got_error *
384 open_commit(struct got_commit_object **commit,
385 struct got_repository *repo, struct got_object_id *id, int check_cache)
387 const struct got_error *err = NULL;
388 struct got_packidx *packidx = NULL;
390 char *path_packfile = NULL;
393 *commit = got_repo_get_cached_commit(repo, id);
394 if (*commit != NULL) {
401 err = got_repo_search_packidx(&packidx, &idx, repo, id);
403 struct got_pack *pack = NULL;
404 struct got_object *obj;
408 err = got_packidx_get_packfile_path(&path_packfile,
409 packidx->path_packidx);
413 pack = got_repo_get_cached_pack(repo, path_packfile);
415 err = got_repo_cache_pack(&pack, repo, path_packfile,
420 err = got_packfile_open_object(&obj, pack, packidx, idx, id);
423 err = got_packfile_extract_object_to_mem(&buf, &len,
425 got_object_close(obj);
428 err = got_object_parse_commit(commit, buf, len);
430 } else if (err->code == GOT_ERR_NO_OBJ) {
433 err = got_object_open_loose_fd(&fd, id, repo);
436 err = got_object_read_commit(commit, fd, id, 0);
437 if (close(fd) == -1 && err == NULL)
438 err = got_error_from_errno("close");
445 err = got_repo_cache_commit(repo, id, *commit);
447 if (err->code == GOT_ERR_OBJ_EXISTS ||
448 err->code == GOT_ERR_OBJ_TOO_LARGE)
457 const struct got_error *
458 got_object_open_as_commit(struct got_commit_object **commit,
459 struct got_repository *repo, struct got_object_id *id)
461 *commit = got_repo_get_cached_commit(repo, id);
462 if (*commit != NULL) {
467 return open_commit(commit, repo, id, 0);
470 const struct got_error *
471 got_object_commit_open(struct got_commit_object **commit,
472 struct got_repository *repo, struct got_object *obj)
474 return open_commit(commit, repo, got_object_get_id(obj), 1);
477 static const struct got_error *
478 open_tree(struct got_tree_object **tree,
479 struct got_repository *repo, struct got_object_id *id, int check_cache)
481 const struct got_error *err = NULL;
482 struct got_packidx *packidx = NULL;
484 char *path_packfile = NULL;
485 struct got_parsed_tree_entry *entries = NULL;
486 size_t nentries = 0, nentries_alloc = 0, i;
490 *tree = got_repo_get_cached_tree(repo, id);
498 err = got_repo_search_packidx(&packidx, &idx, repo, id);
500 struct got_pack *pack = NULL;
501 struct got_object *obj;
504 err = got_packidx_get_packfile_path(&path_packfile,
505 packidx->path_packidx);
509 pack = got_repo_get_cached_pack(repo, path_packfile);
511 err = got_repo_cache_pack(&pack, repo, path_packfile,
516 err = got_packfile_open_object(&obj, pack, packidx, idx, id);
519 err = got_packfile_extract_object_to_mem(&buf, &len,
521 got_object_close(obj);
524 err = got_object_parse_tree(&entries, &nentries,
525 &nentries_alloc, buf, len);
528 } else if (err->code == GOT_ERR_NO_OBJ) {
531 err = got_object_open_loose_fd(&fd, id, repo);
534 err = got_object_read_tree(&entries, &nentries,
535 &nentries_alloc, &buf, fd, id);
536 if (close(fd) == -1 && err == NULL)
537 err = got_error_from_errno("close");
543 *tree = malloc(sizeof(**tree));
545 err = got_error_from_errno("malloc");
548 (*tree)->entries = calloc(nentries, sizeof(struct got_tree_entry));
549 if ((*tree)->entries == NULL) {
550 err = got_error_from_errno("malloc");
553 (*tree)->nentries = nentries;
556 for (i = 0; i < nentries; i++) {
557 struct got_parsed_tree_entry *pe = &entries[i];
558 struct got_tree_entry *te = &(*tree)->entries[i];
560 if (strlcpy(te->name, pe->name,
561 sizeof(te->name)) >= sizeof(te->name)) {
562 err = got_error(GOT_ERR_NO_SPACE);
565 memcpy(te->id.sha1, pe->id, SHA1_DIGEST_LENGTH);
575 err = got_repo_cache_tree(repo, id, *tree);
577 if (err->code == GOT_ERR_OBJ_EXISTS ||
578 err->code == GOT_ERR_OBJ_TOO_LARGE)
584 free((*tree)->entries);
591 const struct got_error *
592 got_object_open_as_tree(struct got_tree_object **tree,
593 struct got_repository *repo, struct got_object_id *id)
595 *tree = got_repo_get_cached_tree(repo, id);
601 return open_tree(tree, repo, id, 0);
604 const struct got_error *
605 got_object_tree_open(struct got_tree_object **tree,
606 struct got_repository *repo, struct got_object *obj)
608 return open_tree(tree, repo, got_object_get_id(obj), 1);
611 const struct got_error *
612 got_object_open_as_blob(struct got_blob_object **blob,
613 struct got_repository *repo, struct got_object_id *id, size_t blocksize,
616 return got_error(GOT_ERR_NOT_IMPL);
619 const struct got_error *
620 got_object_blob_open(struct got_blob_object **blob,
621 struct got_repository *repo, struct got_object *obj, size_t blocksize,
624 return got_error(GOT_ERR_NOT_IMPL);
627 static const struct got_error *
628 open_tag(struct got_tag_object **tag, struct got_repository *repo,
629 struct got_object_id *id, int check_cache)
631 const struct got_error *err = NULL;
632 struct got_packidx *packidx = NULL;
634 char *path_packfile = NULL;
635 struct got_object *obj = NULL;
636 int obj_type = GOT_OBJ_TYPE_ANY;
639 *tag = got_repo_get_cached_tag(repo, id);
647 err = got_repo_search_packidx(&packidx, &idx, repo, id);
649 struct got_pack *pack = NULL;
653 err = got_packidx_get_packfile_path(&path_packfile,
654 packidx->path_packidx);
658 pack = got_repo_get_cached_pack(repo, path_packfile);
660 err = got_repo_cache_pack(&pack, repo, path_packfile,
666 /* Beware of "lightweight" tags: Check object type first. */
667 err = got_packfile_open_object(&obj, pack, packidx, idx, id);
670 obj_type = obj->type;
671 if (obj_type != GOT_OBJ_TYPE_TAG) {
672 err = got_error(GOT_ERR_OBJ_TYPE);
673 got_object_close(obj);
676 err = got_packfile_extract_object_to_mem(&buf, &len,
678 got_object_close(obj);
681 err = got_object_parse_tag(tag, buf, len);
683 } else if (err->code == GOT_ERR_NO_OBJ) {
686 err = got_object_open_loose_fd(&fd, id, repo);
689 err = got_object_read_header(&obj, fd);
690 if (close(fd) == -1 && err == NULL)
691 err = got_error_from_errno("close");
694 obj_type = obj->type;
695 got_object_close(obj);
696 if (obj_type != GOT_OBJ_TYPE_TAG)
697 return got_error(GOT_ERR_OBJ_TYPE);
699 err = got_object_open_loose_fd(&fd, id, repo);
702 err = got_object_read_tag(tag, fd, id, 0);
703 if (close(fd) == -1 && err == NULL)
704 err = got_error_from_errno("close");
711 err = got_repo_cache_tag(repo, id, *tag);
713 if (err->code == GOT_ERR_OBJ_EXISTS ||
714 err->code == GOT_ERR_OBJ_TOO_LARGE)
723 const struct got_error *
724 got_object_open_as_tag(struct got_tag_object **tag,
725 struct got_repository *repo, struct got_object_id *id)
727 *tag = got_repo_get_cached_tag(repo, id);
733 return open_tag(tag, repo, id, 0);
736 const struct got_error *
737 got_object_tag_open(struct got_tag_object **tag,
738 struct got_repository *repo, struct got_object *obj)
740 return open_tag(tag, repo, got_object_get_id(obj), 1);