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>
28 #include "got_error.h"
29 #include "got_object.h"
30 #include "got_repository.h"
33 #include "got_lib_delta.h"
34 #include "got_lib_object.h"
35 #include "got_lib_object_cache.h"
36 #include "got_lib_object_parse.h"
37 #include "got_lib_pack.h"
38 #include "got_lib_repository.h"
40 const struct got_error *
41 got_object_open_packed(struct got_object **obj, struct got_object_id *id,
42 struct got_repository *repo)
44 const struct got_error *err = NULL;
45 struct got_pack *pack = NULL;
46 struct got_packidx *packidx = NULL;
50 err = got_repo_search_packidx(&packidx, &idx, repo, id);
54 err = got_packidx_get_packfile_path(&path_packfile,
55 packidx->path_packidx);
59 pack = got_repo_get_cached_pack(repo, path_packfile);
61 err = got_repo_cache_pack(&pack, repo, path_packfile, packidx);
66 err = got_packfile_open_object(obj, pack, packidx, idx, id);
71 err = got_repo_cache_object(repo, id, *obj);
73 if (err->code == GOT_ERR_OBJ_EXISTS ||
74 err->code == GOT_ERR_OBJ_TOO_LARGE)
82 const struct got_error *
83 got_object_open_from_packfile(struct got_object **obj, struct got_object_id *id,
84 struct got_pack *pack, struct got_packidx *packidx, int obj_idx,
85 struct got_repository *repo)
87 return got_error(GOT_ERR_NOT_IMPL);
90 const struct got_error *
91 got_object_read_raw_delta(uint64_t *base_size, uint64_t *result_size,
92 off_t *delta_size, off_t *delta_compressed_size, off_t *delta_offset,
93 off_t *delta_out_offset, struct got_object_id **base_id, int delta_cache_fd,
94 struct got_packidx *packidx, int obj_idx, struct got_object_id *id,
95 struct got_repository *repo)
97 return got_error(GOT_ERR_NOT_IMPL);
100 const struct got_error *
101 got_object_open(struct got_object **obj, struct got_repository *repo,
102 struct got_object_id *id)
104 const struct got_error *err = NULL;
107 *obj = got_repo_get_cached_object(repo, id);
113 err = got_object_open_packed(obj, id, repo);
115 if (err->code != GOT_ERR_NO_OBJ)
120 err = got_object_open_loose_fd(&fd, id, repo);
122 if (err->code == GOT_ERR_ERRNO && errno == ENOENT)
123 err = got_error_no_obj(id);
127 err = got_object_read_header(obj, fd);
131 memcpy(&(*obj)->id, id, sizeof((*obj)->id));
134 err = got_repo_cache_object(repo, id, *obj);
136 if (err->code == GOT_ERR_OBJ_EXISTS ||
137 err->code == GOT_ERR_OBJ_TOO_LARGE)
141 if (close(fd) == -1 && err == NULL)
142 err = got_error_from_errno("close");
146 static const struct got_error *
147 wrap_fd(FILE **f, int wrapped_fd)
149 const struct got_error *err = NULL;
152 if (ftruncate(wrapped_fd, 0L) == -1)
153 return got_error_from_errno("ftruncate");
155 if (lseek(wrapped_fd, 0L, SEEK_SET) == -1)
156 return got_error_from_errno("lseek");
158 fd = dup(wrapped_fd);
160 return got_error_from_errno("dup");
162 *f = fdopen(fd, "w+");
164 err = got_error_from_errno("fdopen");
170 static const struct got_error *
171 read_packed_object_raw(uint8_t **outbuf, off_t *size, size_t *hdrlen,
172 int outfd, struct got_pack *pack, struct got_packidx *packidx, int idx,
173 struct got_object_id *id)
175 const struct got_error *err = NULL;
176 uint64_t raw_size = 0;
177 struct got_object *obj;
178 FILE *outfile = NULL, *basefile = NULL, *accumfile = NULL;
184 err = got_packfile_open_object(&obj, pack, packidx, idx, id);
188 if (obj->flags & GOT_OBJ_FLAG_DELTIFIED) {
189 err = got_pack_get_max_delta_object_size(&raw_size, obj, pack);
193 raw_size = obj->size;
195 if (raw_size <= GOT_DELTA_RESULT_SIZE_CACHED_MAX) {
197 err = got_packfile_extract_object_to_mem(outbuf, &len,
204 * XXX This uses 3 file extra descriptors for no good reason.
205 * We should have got_packfile_extract_object_to_fd().
207 err = wrap_fd(&outfile, outfd);
210 err = wrap_fd(&basefile, pack->basefd);
213 err = wrap_fd(&accumfile, pack->accumfd);
216 err = got_packfile_extract_object(pack, obj, outfile, basefile,
223 *hdrlen = obj->hdrlen;
225 got_object_close(obj);
226 if (outfile && fclose(outfile) == EOF && err == NULL)
227 err = got_error_from_errno("fclose");
228 if (basefile && fclose(basefile) == EOF && err == NULL)
229 err = got_error_from_errno("fclose");
230 if (accumfile && fclose(accumfile) == EOF && err == NULL)
231 err = got_error_from_errno("fclose");
237 put_raw_object_tempfile(struct got_raw_object *obj)
239 struct got_repository *repo = obj->close_arg;
241 if (obj->tempfile_idx != -1)
242 got_repo_temp_fds_put(obj->tempfile_idx, repo);
245 /* *outfd must be initialized to -1 by caller */
246 const struct got_error *
247 got_object_raw_open(struct got_raw_object **obj, int *outfd,
248 struct got_repository *repo, struct got_object_id *id)
250 const struct got_error *err = NULL;
251 struct got_packidx *packidx = NULL;
252 int idx, tempfd, tempfile_idx;
253 uint8_t *outbuf = NULL;
256 char *path_packfile = NULL;
258 *obj = got_repo_get_cached_raw_object(repo, id);
264 err = got_repo_temp_fds_get(&tempfd, &tempfile_idx, repo);
268 err = got_repo_search_packidx(&packidx, &idx, repo, id);
270 struct got_pack *pack = NULL;
272 err = got_packidx_get_packfile_path(&path_packfile,
273 packidx->path_packidx);
277 pack = got_repo_get_cached_pack(repo, path_packfile);
279 err = got_repo_cache_pack(&pack, repo, path_packfile,
284 err = read_packed_object_raw(&outbuf, &size, &hdrlen,
285 tempfd, pack, packidx, idx, id);
288 } else if (err->code == GOT_ERR_NO_OBJ) {
291 err = got_object_open_loose_fd(&fd, id, repo);
294 err = got_object_read_raw(&outbuf, &size, &hdrlen,
295 GOT_DELTA_RESULT_SIZE_CACHED_MAX, tempfd, id, fd);
296 if (close(fd) == -1 && err == NULL)
297 err = got_error_from_errno("close");
302 if (outbuf == NULL) {
304 err = got_error_msg(GOT_ERR_NOT_IMPL, "bad outfd");
309 * Duplicate tempfile descriptor to allow use of
310 * fdopen(3) inside got_object_raw_alloc().
312 *outfd = dup(tempfd);
314 err = got_error_from_errno("dup");
319 err = got_object_raw_alloc(obj, outbuf, outfd,
320 GOT_DELTA_RESULT_SIZE_CACHED_MAX, hdrlen, size);
324 err = got_repo_cache_raw_object(repo, id, *obj);
326 if (err->code == GOT_ERR_OBJ_EXISTS ||
327 err->code == GOT_ERR_OBJ_TOO_LARGE)
334 got_object_raw_close(*obj);
338 got_repo_temp_fds_put(tempfile_idx, repo);
344 if (((*obj)->f == NULL && (*obj)->fd == -1)) {
345 /* This raw object is not backed by a file. */
346 got_repo_temp_fds_put(tempfile_idx, repo);
352 (*obj)->tempfile_idx = tempfile_idx;
353 (*obj)->close_cb = put_raw_object_tempfile;
354 (*obj)->close_arg = repo;
360 static const struct got_error *
361 open_commit(struct got_commit_object **commit,
362 struct got_repository *repo, struct got_object_id *id, int check_cache)
364 const struct got_error *err = NULL;
365 struct got_packidx *packidx = NULL;
367 char *path_packfile = NULL;
370 *commit = got_repo_get_cached_commit(repo, id);
371 if (*commit != NULL) {
378 err = got_repo_search_packidx(&packidx, &idx, repo, id);
380 struct got_pack *pack = NULL;
381 struct got_object *obj;
385 err = got_packidx_get_packfile_path(&path_packfile,
386 packidx->path_packidx);
390 pack = got_repo_get_cached_pack(repo, path_packfile);
392 err = got_repo_cache_pack(&pack, repo, path_packfile,
397 err = got_packfile_open_object(&obj, pack, packidx, idx, id);
400 err = got_packfile_extract_object_to_mem(&buf, &len,
402 got_object_close(obj);
405 err = got_object_parse_commit(commit, buf, len);
407 } else if (err->code == GOT_ERR_NO_OBJ) {
410 err = got_object_open_loose_fd(&fd, id, repo);
413 err = got_object_read_commit(commit, fd, id, 0);
414 if (close(fd) == -1 && err == NULL)
415 err = got_error_from_errno("close");
422 err = got_repo_cache_commit(repo, id, *commit);
424 if (err->code == GOT_ERR_OBJ_EXISTS ||
425 err->code == GOT_ERR_OBJ_TOO_LARGE)
434 const struct got_error *
435 got_object_open_as_commit(struct got_commit_object **commit,
436 struct got_repository *repo, struct got_object_id *id)
438 *commit = got_repo_get_cached_commit(repo, id);
439 if (*commit != NULL) {
444 return open_commit(commit, repo, id, 0);
447 const struct got_error *
448 got_object_commit_open(struct got_commit_object **commit,
449 struct got_repository *repo, struct got_object *obj)
451 return open_commit(commit, repo, got_object_get_id(obj), 1);
454 static const struct got_error *
455 open_tree(struct got_tree_object **tree,
456 struct got_repository *repo, struct got_object_id *id, int check_cache)
458 const struct got_error *err = NULL;
459 struct got_packidx *packidx = NULL;
461 char *path_packfile = NULL;
462 struct got_parsed_tree_entry *entries = NULL;
463 size_t nentries = 0, nentries_alloc = 0, i;
467 *tree = got_repo_get_cached_tree(repo, id);
475 err = got_repo_search_packidx(&packidx, &idx, repo, id);
477 struct got_pack *pack = NULL;
478 struct got_object *obj;
481 err = got_packidx_get_packfile_path(&path_packfile,
482 packidx->path_packidx);
486 pack = got_repo_get_cached_pack(repo, path_packfile);
488 err = got_repo_cache_pack(&pack, repo, path_packfile,
493 err = got_packfile_open_object(&obj, pack, packidx, idx, id);
496 err = got_packfile_extract_object_to_mem(&buf, &len,
498 got_object_close(obj);
501 err = got_object_parse_tree(&entries, &nentries,
502 &nentries_alloc, buf, len);
505 } else if (err->code == GOT_ERR_NO_OBJ) {
508 err = got_object_open_loose_fd(&fd, id, repo);
511 err = got_object_read_tree(&entries, &nentries,
512 &nentries_alloc, &buf, fd, id);
513 if (close(fd) == -1 && err == NULL)
514 err = got_error_from_errno("close");
520 *tree = malloc(sizeof(**tree));
522 err = got_error_from_errno("malloc");
525 (*tree)->entries = calloc(nentries, sizeof(struct got_tree_entry));
526 if ((*tree)->entries == NULL) {
527 err = got_error_from_errno("malloc");
530 (*tree)->nentries = nentries;
533 for (i = 0; i < nentries; i++) {
534 struct got_parsed_tree_entry *pe = &entries[i];
535 struct got_tree_entry *te = &(*tree)->entries[i];
537 if (strlcpy(te->name, pe->name,
538 sizeof(te->name)) >= sizeof(te->name)) {
539 err = got_error(GOT_ERR_NO_SPACE);
542 memcpy(te->id.sha1, pe->id, SHA1_DIGEST_LENGTH);
552 err = got_repo_cache_tree(repo, id, *tree);
554 if (err->code == GOT_ERR_OBJ_EXISTS ||
555 err->code == GOT_ERR_OBJ_TOO_LARGE)
561 free((*tree)->entries);
568 const struct got_error *
569 got_object_open_as_tree(struct got_tree_object **tree,
570 struct got_repository *repo, struct got_object_id *id)
572 *tree = got_repo_get_cached_tree(repo, id);
578 return open_tree(tree, repo, id, 0);
581 const struct got_error *
582 got_object_tree_open(struct got_tree_object **tree,
583 struct got_repository *repo, struct got_object *obj)
585 return open_tree(tree, repo, got_object_get_id(obj), 1);
588 const struct got_error *
589 got_object_open_as_blob(struct got_blob_object **blob,
590 struct got_repository *repo, struct got_object_id *id, size_t blocksize,
593 return got_error(GOT_ERR_NOT_IMPL);
596 const struct got_error *
597 got_object_blob_open(struct got_blob_object **blob,
598 struct got_repository *repo, struct got_object *obj, size_t blocksize,
601 return got_error(GOT_ERR_NOT_IMPL);
604 static const struct got_error *
605 open_tag(struct got_tag_object **tag, struct got_repository *repo,
606 struct got_object_id *id, int check_cache)
608 const struct got_error *err = NULL;
609 struct got_packidx *packidx = NULL;
611 char *path_packfile = NULL;
612 struct got_object *obj = NULL;
613 int obj_type = GOT_OBJ_TYPE_ANY;
616 *tag = got_repo_get_cached_tag(repo, id);
624 err = got_repo_search_packidx(&packidx, &idx, repo, id);
626 struct got_pack *pack = NULL;
630 err = got_packidx_get_packfile_path(&path_packfile,
631 packidx->path_packidx);
635 pack = got_repo_get_cached_pack(repo, path_packfile);
637 err = got_repo_cache_pack(&pack, repo, path_packfile,
643 /* Beware of "lightweight" tags: Check object type first. */
644 err = got_packfile_open_object(&obj, pack, packidx, idx, id);
647 obj_type = obj->type;
648 if (obj_type != GOT_OBJ_TYPE_TAG) {
649 err = got_error(GOT_ERR_OBJ_TYPE);
650 got_object_close(obj);
653 err = got_packfile_extract_object_to_mem(&buf, &len,
655 got_object_close(obj);
658 err = got_object_parse_tag(tag, buf, len);
660 } else if (err->code == GOT_ERR_NO_OBJ) {
663 err = got_object_open_loose_fd(&fd, id, repo);
666 err = got_object_read_header(&obj, fd);
667 if (close(fd) == -1 && err == NULL)
668 err = got_error_from_errno("close");
671 obj_type = obj->type;
672 got_object_close(obj);
673 if (obj_type != GOT_OBJ_TYPE_TAG)
674 return got_error(GOT_ERR_OBJ_TYPE);
676 err = got_object_open_loose_fd(&fd, id, repo);
679 err = got_object_read_tag(tag, fd, id, 0);
680 if (close(fd) == -1 && err == NULL)
681 err = got_error_from_errno("close");
688 err = got_repo_cache_tag(repo, id, *tag);
690 if (err->code == GOT_ERR_OBJ_EXISTS ||
691 err->code == GOT_ERR_OBJ_TOO_LARGE)
700 const struct got_error *
701 got_object_open_as_tag(struct got_tag_object **tag,
702 struct got_repository *repo, struct got_object_id *id)
704 *tag = got_repo_get_cached_tag(repo, id);
710 return open_tag(tag, repo, id, 0);
713 const struct got_error *
714 got_object_tag_open(struct got_tag_object **tag,
715 struct got_repository *repo, struct got_object *obj)
717 return open_tag(tag, repo, got_object_get_id(obj), 1);