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,
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, tempfile_idx = -1;
253 uint8_t *outbuf = NULL;
256 char *path_packfile = NULL;
258 *obj = got_repo_get_cached_raw_object(repo, id);
267 err = got_repo_temp_fds_get(&tempfd, &tempfile_idx, repo);
271 /* Duplicate tempfile descriptor to allow use of fdopen(3). */
272 *outfd = dup(tempfd);
274 got_repo_temp_fds_put(tempfile_idx, repo);
275 return got_error_from_errno("dup");
279 err = got_repo_search_packidx(&packidx, &idx, repo, id);
281 struct got_pack *pack = NULL;
283 err = got_packidx_get_packfile_path(&path_packfile,
284 packidx->path_packidx);
288 pack = got_repo_get_cached_pack(repo, path_packfile);
290 err = got_repo_cache_pack(&pack, repo, path_packfile,
295 err = read_packed_object_raw(&outbuf, &size, &hdrlen,
296 *outfd, pack, packidx, idx, id);
299 } else if (err->code == GOT_ERR_NO_OBJ) {
302 err = got_object_open_loose_fd(&fd, id, repo);
305 err = got_object_read_raw(&outbuf, &size, &hdrlen,
306 GOT_DELTA_RESULT_SIZE_CACHED_MAX, *outfd, id, fd);
307 if (close(fd) == -1 && err == NULL)
308 err = got_error_from_errno("close");
313 err = got_object_raw_alloc(obj, outbuf, outfd, hdrlen, size);
317 err = got_repo_cache_raw_object(repo, id, *obj);
319 if (err->code == GOT_ERR_OBJ_EXISTS ||
320 err->code == GOT_ERR_OBJ_TOO_LARGE)
327 got_object_raw_close(*obj);
331 if (tempfile_idx != -1)
332 got_repo_temp_fds_put(tempfile_idx, repo);
334 (*obj)->tempfile_idx = tempfile_idx;
335 (*obj)->close_cb = put_raw_object_tempfile;
336 (*obj)->close_arg = repo;
341 static const struct got_error *
342 open_commit(struct got_commit_object **commit,
343 struct got_repository *repo, struct got_object_id *id, int check_cache)
345 const struct got_error *err = NULL;
346 struct got_packidx *packidx = NULL;
348 char *path_packfile = NULL;
351 *commit = got_repo_get_cached_commit(repo, id);
352 if (*commit != NULL) {
359 err = got_repo_search_packidx(&packidx, &idx, repo, id);
361 struct got_pack *pack = NULL;
362 struct got_object *obj;
366 err = got_packidx_get_packfile_path(&path_packfile,
367 packidx->path_packidx);
371 pack = got_repo_get_cached_pack(repo, path_packfile);
373 err = got_repo_cache_pack(&pack, repo, path_packfile,
378 err = got_packfile_open_object(&obj, pack, packidx, idx, id);
381 err = got_packfile_extract_object_to_mem(&buf, &len,
383 got_object_close(obj);
386 err = got_object_parse_commit(commit, buf, len);
388 } else if (err->code == GOT_ERR_NO_OBJ) {
391 err = got_object_open_loose_fd(&fd, id, repo);
394 err = got_object_read_commit(commit, fd, id, 0);
395 if (close(fd) == -1 && err == NULL)
396 err = got_error_from_errno("close");
403 err = got_repo_cache_commit(repo, id, *commit);
405 if (err->code == GOT_ERR_OBJ_EXISTS ||
406 err->code == GOT_ERR_OBJ_TOO_LARGE)
415 const struct got_error *
416 got_object_open_as_commit(struct got_commit_object **commit,
417 struct got_repository *repo, struct got_object_id *id)
419 *commit = got_repo_get_cached_commit(repo, id);
420 if (*commit != NULL) {
425 return open_commit(commit, repo, id, 0);
428 const struct got_error *
429 got_object_commit_open(struct got_commit_object **commit,
430 struct got_repository *repo, struct got_object *obj)
432 return open_commit(commit, repo, got_object_get_id(obj), 1);
435 static const struct got_error *
436 open_tree(struct got_tree_object **tree,
437 struct got_repository *repo, struct got_object_id *id, int check_cache)
439 const struct got_error *err = NULL;
440 struct got_packidx *packidx = NULL;
442 char *path_packfile = NULL;
443 struct got_parsed_tree_entry *entries = NULL;
444 size_t nentries = 0, nentries_alloc = 0, i;
448 *tree = got_repo_get_cached_tree(repo, id);
456 err = got_repo_search_packidx(&packidx, &idx, repo, id);
458 struct got_pack *pack = NULL;
459 struct got_object *obj;
462 err = got_packidx_get_packfile_path(&path_packfile,
463 packidx->path_packidx);
467 pack = got_repo_get_cached_pack(repo, path_packfile);
469 err = got_repo_cache_pack(&pack, repo, path_packfile,
474 err = got_packfile_open_object(&obj, pack, packidx, idx, id);
477 err = got_packfile_extract_object_to_mem(&buf, &len,
479 got_object_close(obj);
482 err = got_object_parse_tree(&entries, &nentries,
483 &nentries_alloc, buf, len);
486 } else if (err->code == GOT_ERR_NO_OBJ) {
489 err = got_object_open_loose_fd(&fd, id, repo);
492 err = got_object_read_tree(&entries, &nentries,
493 &nentries_alloc, &buf, fd, id);
494 if (close(fd) == -1 && err == NULL)
495 err = got_error_from_errno("close");
501 *tree = malloc(sizeof(**tree));
503 err = got_error_from_errno("malloc");
506 (*tree)->entries = calloc(nentries, sizeof(struct got_tree_entry));
507 if ((*tree)->entries == NULL) {
508 err = got_error_from_errno("malloc");
511 (*tree)->nentries = nentries;
514 for (i = 0; i < nentries; i++) {
515 struct got_parsed_tree_entry *pe = &entries[i];
516 struct got_tree_entry *te = &(*tree)->entries[i];
518 if (strlcpy(te->name, pe->name,
519 sizeof(te->name)) >= sizeof(te->name)) {
520 err = got_error(GOT_ERR_NO_SPACE);
523 memcpy(te->id.sha1, pe->id, SHA1_DIGEST_LENGTH);
533 err = got_repo_cache_tree(repo, id, *tree);
535 if (err->code == GOT_ERR_OBJ_EXISTS ||
536 err->code == GOT_ERR_OBJ_TOO_LARGE)
542 free((*tree)->entries);
549 const struct got_error *
550 got_object_open_as_tree(struct got_tree_object **tree,
551 struct got_repository *repo, struct got_object_id *id)
553 *tree = got_repo_get_cached_tree(repo, id);
559 return open_tree(tree, repo, id, 0);
562 const struct got_error *
563 got_object_tree_open(struct got_tree_object **tree,
564 struct got_repository *repo, struct got_object *obj)
566 return open_tree(tree, repo, got_object_get_id(obj), 1);
569 const struct got_error *
570 got_object_open_as_blob(struct got_blob_object **blob,
571 struct got_repository *repo, struct got_object_id *id, size_t blocksize,
574 return got_error(GOT_ERR_NOT_IMPL);
577 const struct got_error *
578 got_object_blob_open(struct got_blob_object **blob,
579 struct got_repository *repo, struct got_object *obj, size_t blocksize,
582 return got_error(GOT_ERR_NOT_IMPL);
585 static const struct got_error *
586 open_tag(struct got_tag_object **tag, struct got_repository *repo,
587 struct got_object_id *id, int check_cache)
589 const struct got_error *err = NULL;
590 struct got_packidx *packidx = NULL;
592 char *path_packfile = NULL;
593 struct got_object *obj = NULL;
594 int obj_type = GOT_OBJ_TYPE_ANY;
597 *tag = got_repo_get_cached_tag(repo, id);
605 err = got_repo_search_packidx(&packidx, &idx, repo, id);
607 struct got_pack *pack = NULL;
611 err = got_packidx_get_packfile_path(&path_packfile,
612 packidx->path_packidx);
616 pack = got_repo_get_cached_pack(repo, path_packfile);
618 err = got_repo_cache_pack(&pack, repo, path_packfile,
624 /* Beware of "lightweight" tags: Check object type first. */
625 err = got_packfile_open_object(&obj, pack, packidx, idx, id);
628 obj_type = obj->type;
629 if (obj_type != GOT_OBJ_TYPE_TAG) {
630 err = got_error(GOT_ERR_OBJ_TYPE);
631 got_object_close(obj);
634 err = got_packfile_extract_object_to_mem(&buf, &len,
636 got_object_close(obj);
639 err = got_object_parse_tag(tag, buf, len);
641 } else if (err->code == GOT_ERR_NO_OBJ) {
644 err = got_object_open_loose_fd(&fd, id, repo);
647 err = got_object_read_header(&obj, fd);
648 if (close(fd) == -1 && err == NULL)
649 err = got_error_from_errno("close");
652 obj_type = obj->type;
653 got_object_close(obj);
654 if (obj_type != GOT_OBJ_TYPE_TAG)
655 return got_error(GOT_ERR_OBJ_TYPE);
657 err = got_object_open_loose_fd(&fd, id, repo);
660 err = got_object_read_tag(tag, fd, id, 0);
661 if (close(fd) == -1 && err == NULL)
662 err = got_error_from_errno("close");
669 err = got_repo_cache_tag(repo, id, *tag);
671 if (err->code == GOT_ERR_OBJ_EXISTS ||
672 err->code == GOT_ERR_OBJ_TOO_LARGE)
681 const struct got_error *
682 got_object_open_as_tag(struct got_tag_object **tag,
683 struct got_repository *repo, struct got_object_id *id)
685 *tag = got_repo_get_cached_tag(repo, id);
691 return open_tag(tag, repo, id, 0);
694 const struct got_error *
695 got_object_tag_open(struct got_tag_object **tag,
696 struct got_repository *repo, struct got_object *obj)
698 return open_tag(tag, repo, got_object_get_id(obj), 1);