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, tempfile_idx = -1;
254 uint8_t *outbuf = NULL;
257 char *path_packfile = NULL;
259 *obj = got_repo_get_cached_raw_object(repo, id);
268 err = got_repo_temp_fds_get(&tempfd, &tempfile_idx, repo);
272 /* Duplicate tempfile descriptor to allow use of fdopen(3). */
273 *outfd = dup(tempfd);
275 got_repo_temp_fds_put(tempfile_idx, repo);
276 return got_error_from_errno("dup");
280 err = got_repo_search_packidx(&packidx, &idx, repo, id);
282 struct got_pack *pack = NULL;
284 err = got_packidx_get_packfile_path(&path_packfile,
285 packidx->path_packidx);
289 pack = got_repo_get_cached_pack(repo, path_packfile);
291 err = got_repo_cache_pack(&pack, repo, path_packfile,
296 err = read_packed_object_raw(&outbuf, &size, &hdrlen,
297 *outfd, pack, packidx, idx, id);
300 } else if (err->code == GOT_ERR_NO_OBJ) {
303 err = got_object_open_loose_fd(&fd, id, repo);
306 err = got_object_read_raw(&outbuf, &size, &hdrlen,
307 GOT_DELTA_RESULT_SIZE_CACHED_MAX, *outfd, id, fd);
308 if (close(fd) == -1 && err == NULL)
309 err = got_error_from_errno("close");
314 err = got_object_raw_alloc(obj, outbuf, outfd,
315 GOT_DELTA_RESULT_SIZE_CACHED_MAX, hdrlen, size);
319 err = got_repo_cache_raw_object(repo, id, *obj);
321 if (err->code == GOT_ERR_OBJ_EXISTS ||
322 err->code == GOT_ERR_OBJ_TOO_LARGE)
329 got_object_raw_close(*obj);
333 if (tempfile_idx != -1)
334 got_repo_temp_fds_put(tempfile_idx, repo);
336 (*obj)->tempfile_idx = tempfile_idx;
337 (*obj)->close_cb = put_raw_object_tempfile;
338 (*obj)->close_arg = repo;
343 static const struct got_error *
344 open_commit(struct got_commit_object **commit,
345 struct got_repository *repo, struct got_object_id *id, int check_cache)
347 const struct got_error *err = NULL;
348 struct got_packidx *packidx = NULL;
350 char *path_packfile = NULL;
353 *commit = got_repo_get_cached_commit(repo, id);
354 if (*commit != NULL) {
361 err = got_repo_search_packidx(&packidx, &idx, repo, id);
363 struct got_pack *pack = NULL;
364 struct got_object *obj;
368 err = got_packidx_get_packfile_path(&path_packfile,
369 packidx->path_packidx);
373 pack = got_repo_get_cached_pack(repo, path_packfile);
375 err = got_repo_cache_pack(&pack, repo, path_packfile,
380 err = got_packfile_open_object(&obj, pack, packidx, idx, id);
383 err = got_packfile_extract_object_to_mem(&buf, &len,
385 got_object_close(obj);
388 err = got_object_parse_commit(commit, buf, len);
390 } else if (err->code == GOT_ERR_NO_OBJ) {
393 err = got_object_open_loose_fd(&fd, id, repo);
396 err = got_object_read_commit(commit, fd, id, 0);
397 if (close(fd) == -1 && err == NULL)
398 err = got_error_from_errno("close");
405 err = got_repo_cache_commit(repo, id, *commit);
407 if (err->code == GOT_ERR_OBJ_EXISTS ||
408 err->code == GOT_ERR_OBJ_TOO_LARGE)
417 const struct got_error *
418 got_object_open_as_commit(struct got_commit_object **commit,
419 struct got_repository *repo, struct got_object_id *id)
421 *commit = got_repo_get_cached_commit(repo, id);
422 if (*commit != NULL) {
427 return open_commit(commit, repo, id, 0);
430 const struct got_error *
431 got_object_commit_open(struct got_commit_object **commit,
432 struct got_repository *repo, struct got_object *obj)
434 return open_commit(commit, repo, got_object_get_id(obj), 1);
437 static const struct got_error *
438 open_tree(struct got_tree_object **tree,
439 struct got_repository *repo, struct got_object_id *id, int check_cache)
441 const struct got_error *err = NULL;
442 struct got_packidx *packidx = NULL;
444 char *path_packfile = NULL;
445 struct got_parsed_tree_entry *entries = NULL;
446 size_t nentries = 0, nentries_alloc = 0, i;
450 *tree = got_repo_get_cached_tree(repo, id);
458 err = got_repo_search_packidx(&packidx, &idx, repo, id);
460 struct got_pack *pack = NULL;
461 struct got_object *obj;
464 err = got_packidx_get_packfile_path(&path_packfile,
465 packidx->path_packidx);
469 pack = got_repo_get_cached_pack(repo, path_packfile);
471 err = got_repo_cache_pack(&pack, repo, path_packfile,
476 err = got_packfile_open_object(&obj, pack, packidx, idx, id);
479 err = got_packfile_extract_object_to_mem(&buf, &len,
481 got_object_close(obj);
484 err = got_object_parse_tree(&entries, &nentries,
485 &nentries_alloc, buf, len);
488 } else if (err->code == GOT_ERR_NO_OBJ) {
491 err = got_object_open_loose_fd(&fd, id, repo);
494 err = got_object_read_tree(&entries, &nentries,
495 &nentries_alloc, &buf, fd, id);
496 if (close(fd) == -1 && err == NULL)
497 err = got_error_from_errno("close");
503 *tree = malloc(sizeof(**tree));
505 err = got_error_from_errno("malloc");
508 (*tree)->entries = calloc(nentries, sizeof(struct got_tree_entry));
509 if ((*tree)->entries == NULL) {
510 err = got_error_from_errno("malloc");
513 (*tree)->nentries = nentries;
516 for (i = 0; i < nentries; i++) {
517 struct got_parsed_tree_entry *pe = &entries[i];
518 struct got_tree_entry *te = &(*tree)->entries[i];
520 if (strlcpy(te->name, pe->name,
521 sizeof(te->name)) >= sizeof(te->name)) {
522 err = got_error(GOT_ERR_NO_SPACE);
525 memcpy(te->id.sha1, pe->id, SHA1_DIGEST_LENGTH);
535 err = got_repo_cache_tree(repo, id, *tree);
537 if (err->code == GOT_ERR_OBJ_EXISTS ||
538 err->code == GOT_ERR_OBJ_TOO_LARGE)
544 free((*tree)->entries);
551 const struct got_error *
552 got_object_open_as_tree(struct got_tree_object **tree,
553 struct got_repository *repo, struct got_object_id *id)
555 *tree = got_repo_get_cached_tree(repo, id);
561 return open_tree(tree, repo, id, 0);
564 const struct got_error *
565 got_object_tree_open(struct got_tree_object **tree,
566 struct got_repository *repo, struct got_object *obj)
568 return open_tree(tree, repo, got_object_get_id(obj), 1);
571 const struct got_error *
572 got_object_open_as_blob(struct got_blob_object **blob,
573 struct got_repository *repo, struct got_object_id *id, size_t blocksize,
576 return got_error(GOT_ERR_NOT_IMPL);
579 const struct got_error *
580 got_object_blob_open(struct got_blob_object **blob,
581 struct got_repository *repo, struct got_object *obj, size_t blocksize,
584 return got_error(GOT_ERR_NOT_IMPL);
587 static const struct got_error *
588 open_tag(struct got_tag_object **tag, struct got_repository *repo,
589 struct got_object_id *id, int check_cache)
591 const struct got_error *err = NULL;
592 struct got_packidx *packidx = NULL;
594 char *path_packfile = NULL;
595 struct got_object *obj = NULL;
596 int obj_type = GOT_OBJ_TYPE_ANY;
599 *tag = got_repo_get_cached_tag(repo, id);
607 err = got_repo_search_packidx(&packidx, &idx, repo, id);
609 struct got_pack *pack = NULL;
613 err = got_packidx_get_packfile_path(&path_packfile,
614 packidx->path_packidx);
618 pack = got_repo_get_cached_pack(repo, path_packfile);
620 err = got_repo_cache_pack(&pack, repo, path_packfile,
626 /* Beware of "lightweight" tags: Check object type first. */
627 err = got_packfile_open_object(&obj, pack, packidx, idx, id);
630 obj_type = obj->type;
631 if (obj_type != GOT_OBJ_TYPE_TAG) {
632 err = got_error(GOT_ERR_OBJ_TYPE);
633 got_object_close(obj);
636 err = got_packfile_extract_object_to_mem(&buf, &len,
638 got_object_close(obj);
641 err = got_object_parse_tag(tag, buf, len);
643 } else if (err->code == GOT_ERR_NO_OBJ) {
646 err = got_object_open_loose_fd(&fd, id, repo);
649 err = got_object_read_header(&obj, fd);
650 if (close(fd) == -1 && err == NULL)
651 err = got_error_from_errno("close");
654 obj_type = obj->type;
655 got_object_close(obj);
656 if (obj_type != GOT_OBJ_TYPE_TAG)
657 return got_error(GOT_ERR_OBJ_TYPE);
659 err = got_object_open_loose_fd(&fd, id, repo);
662 err = got_object_read_tag(tag, fd, id, 0);
663 if (close(fd) == -1 && err == NULL)
664 err = got_error_from_errno("close");
671 err = got_repo_cache_tag(repo, id, *tag);
673 if (err->code == GOT_ERR_OBJ_EXISTS ||
674 err->code == GOT_ERR_OBJ_TOO_LARGE)
683 const struct got_error *
684 got_object_open_as_tag(struct got_tag_object **tag,
685 struct got_repository *repo, struct got_object_id *id)
687 *tag = got_repo_get_cached_tag(repo, id);
693 return open_tag(tag, repo, id, 0);
696 const struct got_error *
697 got_object_tag_open(struct got_tag_object **tag,
698 struct got_repository *repo, struct got_object *obj)
700 return open_tag(tag, repo, got_object_get_id(obj), 1);