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>
27 #include "got_error.h"
28 #include "got_object.h"
29 #include "got_repository.h"
32 #include "got_lib_delta.h"
33 #include "got_lib_object.h"
34 #include "got_lib_object_cache.h"
35 #include "got_lib_object_parse.h"
36 #include "got_lib_pack.h"
37 #include "got_lib_repository.h"
39 const struct got_error *
40 got_object_open_packed(struct got_object **obj, struct got_object_id *id,
41 struct got_repository *repo)
43 const struct got_error *err = NULL;
44 struct got_pack *pack = NULL;
45 struct got_packidx *packidx = NULL;
49 err = got_repo_search_packidx(&packidx, &idx, repo, id);
53 err = got_packidx_get_packfile_path(&path_packfile,
54 packidx->path_packidx);
58 pack = got_repo_get_cached_pack(repo, path_packfile);
60 err = got_repo_cache_pack(&pack, repo, path_packfile, packidx);
65 err = got_packfile_open_object(obj, pack, packidx, idx, id);
70 err = got_repo_cache_object(repo, id, *obj);
72 if (err->code == GOT_ERR_OBJ_EXISTS ||
73 err->code == GOT_ERR_OBJ_TOO_LARGE)
81 const struct got_error *
82 got_object_open_from_packfile(struct got_object **obj, struct got_object_id *id,
83 struct got_pack *pack, struct got_packidx *packidx, int obj_idx,
84 struct got_repository *repo)
86 return got_error(GOT_ERR_NOT_IMPL);
89 const struct got_error *
90 got_object_read_raw_delta(uint64_t *base_size, uint64_t *result_size,
91 off_t *delta_size, off_t *delta_compressed_size, off_t *delta_offset,
92 off_t *delta_out_offset, struct got_object_id **base_id, int delta_cache_fd,
93 struct got_packidx *packidx, int obj_idx, struct got_object_id *id,
94 struct got_repository *repo)
96 return got_error(GOT_ERR_NOT_IMPL);
99 const struct got_error *
100 got_object_open(struct got_object **obj, struct got_repository *repo,
101 struct got_object_id *id)
103 const struct got_error *err = NULL;
106 *obj = got_repo_get_cached_object(repo, id);
112 err = got_object_open_packed(obj, id, repo);
114 if (err->code != GOT_ERR_NO_OBJ)
119 err = got_object_open_loose_fd(&fd, id, repo);
121 if (err->code == GOT_ERR_ERRNO && errno == ENOENT)
122 err = got_error_no_obj(id);
126 err = got_object_read_header(obj, fd);
130 memcpy(&(*obj)->id, id, sizeof((*obj)->id));
133 err = got_repo_cache_object(repo, id, *obj);
135 if (err->code == GOT_ERR_OBJ_EXISTS ||
136 err->code == GOT_ERR_OBJ_TOO_LARGE)
140 if (close(fd) == -1 && err == NULL)
141 err = got_error_from_errno("close");
145 static const struct got_error *
146 wrap_fd(FILE **f, int wrapped_fd)
148 const struct got_error *err = NULL;
151 if (ftruncate(wrapped_fd, 0L) == -1)
152 return got_error_from_errno("ftruncate");
154 if (lseek(wrapped_fd, 0L, SEEK_SET) == -1)
155 return got_error_from_errno("lseek");
157 fd = dup(wrapped_fd);
159 return got_error_from_errno("dup");
161 *f = fdopen(fd, "w+");
163 err = got_error_from_errno("fdopen");
169 static const struct got_error *
170 read_packed_object_raw(uint8_t **outbuf, off_t *size, size_t *hdrlen,
171 int outfd, struct got_pack *pack, struct got_packidx *packidx, int idx,
172 struct got_object_id *id)
174 const struct got_error *err = NULL;
175 uint64_t raw_size = 0;
176 struct got_object *obj;
177 FILE *outfile = NULL, *basefile = NULL, *accumfile = NULL;
183 err = got_packfile_open_object(&obj, pack, packidx, idx, id);
187 if (obj->flags & GOT_OBJ_FLAG_DELTIFIED) {
188 err = got_pack_get_max_delta_object_size(&raw_size, obj, pack);
192 raw_size = obj->size;
194 if (raw_size <= GOT_DELTA_RESULT_SIZE_CACHED_MAX) {
196 err = got_packfile_extract_object_to_mem(outbuf, &len,
203 * XXX This uses 3 file extra descriptors for no good reason.
204 * We should have got_packfile_extract_object_to_fd().
206 err = wrap_fd(&outfile, outfd);
209 err = wrap_fd(&basefile, pack->basefd);
212 err = wrap_fd(&accumfile, pack->accumfd);
215 err = got_packfile_extract_object(pack, obj, outfile, basefile,
222 *hdrlen = obj->hdrlen;
224 got_object_close(obj);
225 if (outfile && fclose(outfile) == EOF && err == NULL)
226 err = got_error_from_errno("fclose");
227 if (basefile && fclose(basefile) == EOF && err == NULL)
228 err = got_error_from_errno("fclose");
229 if (accumfile && fclose(accumfile) == EOF && err == NULL)
230 err = got_error_from_errno("fclose");
236 put_raw_object_tempfile(struct got_raw_object *obj)
238 struct got_repository *repo = obj->close_arg;
240 if (obj->tempfile_idx != -1)
241 got_repo_temp_fds_put(obj->tempfile_idx, repo);
244 /* *outfd must be initialized to -1 by caller */
245 const struct got_error *
246 got_object_raw_open(struct got_raw_object **obj, int *outfd,
247 struct got_repository *repo, struct got_object_id *id)
249 const struct got_error *err = NULL;
250 struct got_packidx *packidx = NULL;
251 int idx, tempfd, tempfile_idx;
252 uint8_t *outbuf = NULL;
255 char *path_packfile = NULL;
257 *obj = got_repo_get_cached_raw_object(repo, id);
263 err = got_repo_temp_fds_get(&tempfd, &tempfile_idx, repo);
267 err = got_repo_search_packidx(&packidx, &idx, repo, id);
269 struct got_pack *pack = NULL;
271 err = got_packidx_get_packfile_path(&path_packfile,
272 packidx->path_packidx);
276 pack = got_repo_get_cached_pack(repo, path_packfile);
278 err = got_repo_cache_pack(&pack, repo, path_packfile,
283 err = read_packed_object_raw(&outbuf, &size, &hdrlen,
284 tempfd, pack, packidx, idx, id);
287 } else if (err->code == GOT_ERR_NO_OBJ) {
290 err = got_object_open_loose_fd(&fd, id, repo);
293 err = got_object_read_raw(&outbuf, &size, &hdrlen,
294 GOT_DELTA_RESULT_SIZE_CACHED_MAX, tempfd, id, fd);
295 if (close(fd) == -1 && err == NULL)
296 err = got_error_from_errno("close");
301 if (outbuf == NULL) {
303 err = got_error_msg(GOT_ERR_NOT_IMPL, "bad outfd");
308 * Duplicate tempfile descriptor to allow use of
309 * fdopen(3) inside got_object_raw_alloc().
311 *outfd = dup(tempfd);
313 err = got_error_from_errno("dup");
318 err = got_object_raw_alloc(obj, outbuf, outfd,
319 GOT_DELTA_RESULT_SIZE_CACHED_MAX, hdrlen, size);
323 err = got_repo_cache_raw_object(repo, id, *obj);
325 if (err->code == GOT_ERR_OBJ_EXISTS ||
326 err->code == GOT_ERR_OBJ_TOO_LARGE)
333 got_object_raw_close(*obj);
337 got_repo_temp_fds_put(tempfile_idx, repo);
343 if (((*obj)->f == NULL && (*obj)->fd == -1)) {
344 /* This raw object is not backed by a file. */
345 got_repo_temp_fds_put(tempfile_idx, repo);
351 (*obj)->tempfile_idx = tempfile_idx;
352 (*obj)->close_cb = put_raw_object_tempfile;
353 (*obj)->close_arg = repo;
359 static const struct got_error *
360 open_commit(struct got_commit_object **commit,
361 struct got_repository *repo, struct got_object_id *id, int check_cache)
363 const struct got_error *err = NULL;
364 struct got_packidx *packidx = NULL;
366 char *path_packfile = NULL;
369 *commit = got_repo_get_cached_commit(repo, id);
370 if (*commit != NULL) {
377 err = got_repo_search_packidx(&packidx, &idx, repo, id);
379 struct got_pack *pack = NULL;
380 struct got_object *obj;
384 err = got_packidx_get_packfile_path(&path_packfile,
385 packidx->path_packidx);
389 pack = got_repo_get_cached_pack(repo, path_packfile);
391 err = got_repo_cache_pack(&pack, repo, path_packfile,
396 err = got_packfile_open_object(&obj, pack, packidx, idx, id);
399 err = got_packfile_extract_object_to_mem(&buf, &len,
401 got_object_close(obj);
404 err = got_object_parse_commit(commit, buf, len);
406 } else if (err->code == GOT_ERR_NO_OBJ) {
409 err = got_object_open_loose_fd(&fd, id, repo);
412 err = got_object_read_commit(commit, fd, id, 0);
413 if (close(fd) == -1 && err == NULL)
414 err = got_error_from_errno("close");
421 err = got_repo_cache_commit(repo, id, *commit);
423 if (err->code == GOT_ERR_OBJ_EXISTS ||
424 err->code == GOT_ERR_OBJ_TOO_LARGE)
433 const struct got_error *
434 got_object_open_as_commit(struct got_commit_object **commit,
435 struct got_repository *repo, struct got_object_id *id)
437 *commit = got_repo_get_cached_commit(repo, id);
438 if (*commit != NULL) {
443 return open_commit(commit, repo, id, 0);
446 const struct got_error *
447 got_object_commit_open(struct got_commit_object **commit,
448 struct got_repository *repo, struct got_object *obj)
450 return open_commit(commit, repo, got_object_get_id(obj), 1);
453 static const struct got_error *
454 open_tree(struct got_tree_object **tree,
455 struct got_repository *repo, struct got_object_id *id, int check_cache)
457 const struct got_error *err = NULL;
458 struct got_packidx *packidx = NULL;
460 char *path_packfile = NULL;
461 struct got_parsed_tree_entry *entries = NULL;
462 size_t nentries = 0, nentries_alloc = 0, i;
466 *tree = got_repo_get_cached_tree(repo, id);
474 err = got_repo_search_packidx(&packidx, &idx, repo, id);
476 struct got_pack *pack = NULL;
477 struct got_object *obj;
480 err = got_packidx_get_packfile_path(&path_packfile,
481 packidx->path_packidx);
485 pack = got_repo_get_cached_pack(repo, path_packfile);
487 err = got_repo_cache_pack(&pack, repo, path_packfile,
492 err = got_packfile_open_object(&obj, pack, packidx, idx, id);
495 err = got_packfile_extract_object_to_mem(&buf, &len,
497 got_object_close(obj);
500 err = got_object_parse_tree(&entries, &nentries,
501 &nentries_alloc, buf, len);
504 } else if (err->code == GOT_ERR_NO_OBJ) {
507 err = got_object_open_loose_fd(&fd, id, repo);
510 err = got_object_read_tree(&entries, &nentries,
511 &nentries_alloc, &buf, fd, id);
512 if (close(fd) == -1 && err == NULL)
513 err = got_error_from_errno("close");
519 *tree = malloc(sizeof(**tree));
521 err = got_error_from_errno("malloc");
524 (*tree)->entries = calloc(nentries, sizeof(struct got_tree_entry));
525 if ((*tree)->entries == NULL) {
526 err = got_error_from_errno("malloc");
529 (*tree)->nentries = nentries;
532 for (i = 0; i < nentries; i++) {
533 struct got_parsed_tree_entry *pe = &entries[i];
534 struct got_tree_entry *te = &(*tree)->entries[i];
536 if (strlcpy(te->name, pe->name,
537 sizeof(te->name)) >= sizeof(te->name)) {
538 err = got_error(GOT_ERR_NO_SPACE);
541 memcpy(te->id.sha1, pe->id, SHA1_DIGEST_LENGTH);
551 err = got_repo_cache_tree(repo, id, *tree);
553 if (err->code == GOT_ERR_OBJ_EXISTS ||
554 err->code == GOT_ERR_OBJ_TOO_LARGE)
560 free((*tree)->entries);
567 const struct got_error *
568 got_object_open_as_tree(struct got_tree_object **tree,
569 struct got_repository *repo, struct got_object_id *id)
571 *tree = got_repo_get_cached_tree(repo, id);
577 return open_tree(tree, repo, id, 0);
580 const struct got_error *
581 got_object_tree_open(struct got_tree_object **tree,
582 struct got_repository *repo, struct got_object *obj)
584 return open_tree(tree, repo, got_object_get_id(obj), 1);
587 const struct got_error *
588 got_object_open_as_blob(struct got_blob_object **blob,
589 struct got_repository *repo, struct got_object_id *id, size_t blocksize,
592 return got_error(GOT_ERR_NOT_IMPL);
595 const struct got_error *
596 got_object_blob_open(struct got_blob_object **blob,
597 struct got_repository *repo, struct got_object *obj, size_t blocksize,
600 return got_error(GOT_ERR_NOT_IMPL);
603 static const struct got_error *
604 open_tag(struct got_tag_object **tag, struct got_repository *repo,
605 struct got_object_id *id, int check_cache)
607 const struct got_error *err = NULL;
608 struct got_packidx *packidx = NULL;
610 char *path_packfile = NULL;
611 struct got_object *obj = NULL;
612 int obj_type = GOT_OBJ_TYPE_ANY;
615 *tag = got_repo_get_cached_tag(repo, id);
623 err = got_repo_search_packidx(&packidx, &idx, repo, id);
625 struct got_pack *pack = NULL;
629 err = got_packidx_get_packfile_path(&path_packfile,
630 packidx->path_packidx);
634 pack = got_repo_get_cached_pack(repo, path_packfile);
636 err = got_repo_cache_pack(&pack, repo, path_packfile,
642 /* Beware of "lightweight" tags: Check object type first. */
643 err = got_packfile_open_object(&obj, pack, packidx, idx, id);
646 obj_type = obj->type;
647 if (obj_type != GOT_OBJ_TYPE_TAG) {
648 err = got_error(GOT_ERR_OBJ_TYPE);
649 got_object_close(obj);
652 err = got_packfile_extract_object_to_mem(&buf, &len,
654 got_object_close(obj);
657 err = got_object_parse_tag(tag, buf, len);
659 } else if (err->code == GOT_ERR_NO_OBJ) {
662 err = got_object_open_loose_fd(&fd, id, repo);
665 err = got_object_read_header(&obj, fd);
666 if (close(fd) == -1 && err == NULL)
667 err = got_error_from_errno("close");
670 obj_type = obj->type;
671 got_object_close(obj);
672 if (obj_type != GOT_OBJ_TYPE_TAG)
673 return got_error(GOT_ERR_OBJ_TYPE);
675 err = got_object_open_loose_fd(&fd, id, repo);
678 err = got_object_read_tag(tag, fd, id, 0);
679 if (close(fd) == -1 && err == NULL)
680 err = got_error_from_errno("close");
687 err = got_repo_cache_tag(repo, id, *tag);
689 if (err->code == GOT_ERR_OBJ_EXISTS ||
690 err->code == GOT_ERR_OBJ_TOO_LARGE)
699 const struct got_error *
700 got_object_open_as_tag(struct got_tag_object **tag,
701 struct got_repository *repo, struct got_object_id *id)
703 *tag = got_repo_get_cached_tag(repo, id);
709 return open_tag(tag, repo, id, 0);
712 const struct got_error *
713 got_object_tag_open(struct got_tag_object **tag,
714 struct got_repository *repo, struct got_object *obj)
716 return open_tag(tag, repo, got_object_get_id(obj), 1);