Blob


1 /*
2 * Copyright (c) 2022 Stefan Sperling <stsp@openbsd.org>
3 *
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.
7 *
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.
15 */
17 #include <sys/queue.h>
18 #include <sys/tree.h>
19 #include <sys/stat.h>
21 #include <errno.h>
22 #include <limits.h>
23 #include <sha1.h>
24 #include <stdio.h>
25 #include <stdlib.h>
26 #include <string.h>
27 #include <unistd.h>
29 #include "got_error.h"
30 #include "got_object.h"
31 #include "got_repository.h"
32 #include "got_path.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)
44 {
45 const struct got_error *err = NULL;
46 struct got_pack *pack = NULL;
47 struct got_packidx *packidx = NULL;
48 int idx;
49 char *path_packfile;
51 err = got_repo_search_packidx(&packidx, &idx, repo, id);
52 if (err)
53 return err;
55 err = got_packidx_get_packfile_path(&path_packfile,
56 packidx->path_packidx);
57 if (err)
58 return err;
60 pack = got_repo_get_cached_pack(repo, path_packfile);
61 if (pack == NULL) {
62 err = got_repo_cache_pack(&pack, repo, path_packfile, packidx);
63 if (err)
64 goto done;
65 }
67 err = got_packfile_open_object(obj, pack, packidx, idx, id);
68 if (err)
69 return err;
70 (*obj)->refcnt++;
72 err = got_repo_cache_object(repo, id, *obj);
73 if (err) {
74 if (err->code == GOT_ERR_OBJ_EXISTS ||
75 err->code == GOT_ERR_OBJ_TOO_LARGE)
76 err = NULL;
77 }
78 done:
79 free(path_packfile);
80 return err;
81 }
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)
87 {
88 return got_error(GOT_ERR_NOT_IMPL);
89 }
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)
97 {
98 return got_error(GOT_ERR_NOT_IMPL);
99 }
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;
106 int fd;
108 *obj = got_repo_get_cached_object(repo, id);
109 if (*obj != NULL) {
110 (*obj)->refcnt++;
111 return NULL;
114 err = got_object_open_packed(obj, id, repo);
115 if (err) {
116 if (err->code != GOT_ERR_NO_OBJ)
117 return err;
118 } else
119 return NULL;
121 err = got_object_open_loose_fd(&fd, id, repo);
122 if (err) {
123 if (err->code == GOT_ERR_ERRNO && errno == ENOENT)
124 err = got_error_no_obj(id);
125 return err;
128 err = got_object_read_header(obj, fd);
129 if (err)
130 goto done;
132 memcpy(&(*obj)->id, id, sizeof((*obj)->id));
133 (*obj)->refcnt++;
135 err = got_repo_cache_object(repo, id, *obj);
136 if (err) {
137 if (err->code == GOT_ERR_OBJ_EXISTS ||
138 err->code == GOT_ERR_OBJ_TOO_LARGE)
139 err = NULL;
141 done:
142 if (close(fd) == -1 && err == NULL)
143 err = got_error_from_errno("close");
144 return err;
147 static const struct got_error *
148 wrap_fd(FILE **f, int wrapped_fd)
150 const struct got_error *err = NULL;
151 int fd;
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);
160 if (fd == -1)
161 return got_error_from_errno("dup");
163 *f = fdopen(fd, "w+");
164 if (*f == NULL) {
165 err = got_error_from_errno("fdopen");
166 close(fd);
168 return err;
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;
181 *outbuf = NULL;
182 *size = 0;
183 *hdrlen = 0;
185 err = got_packfile_open_object(&obj, pack, packidx, idx, id);
186 if (err)
187 return err;
189 if (obj->flags & GOT_OBJ_FLAG_DELTIFIED) {
190 err = got_pack_get_max_delta_object_size(&raw_size, obj, pack);
191 if (err)
192 goto done;
193 } else
194 raw_size = obj->size;
196 if (raw_size <= GOT_DELTA_RESULT_SIZE_CACHED_MAX) {
197 size_t len;
198 err = got_packfile_extract_object_to_mem(outbuf, &len,
199 obj, pack);
200 if (err)
201 goto done;
202 *size = (off_t)len;
203 } else {
204 /*
205 * XXX This uses 3 file extra descriptors for no good reason.
206 * We should have got_packfile_extract_object_to_fd().
207 */
208 err = wrap_fd(&outfile, outfd);
209 if (err)
210 goto done;
211 err = wrap_fd(&basefile, pack->basefd);
212 if (err)
213 goto done;
214 err = wrap_fd(&accumfile, pack->accumfd);
215 if (err)
216 goto done;
217 err = got_packfile_extract_object(pack, obj, outfile, basefile,
218 accumfile);
219 if (err)
220 goto done;
221 *size = obj->size;
224 *hdrlen = obj->hdrlen;
225 done:
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");
233 return err;
237 static void
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, tempfd, tempfile_idx;
254 uint8_t *outbuf = NULL;
255 off_t size = 0;
256 size_t hdrlen = 0;
257 char *path_packfile = NULL;
259 *obj = got_repo_get_cached_raw_object(repo, id);
260 if (*obj != NULL) {
261 (*obj)->refcnt++;
262 return NULL;
265 err = got_repo_temp_fds_get(&tempfd, &tempfile_idx, repo);
266 if (err)
267 return err;
269 err = got_repo_search_packidx(&packidx, &idx, repo, id);
270 if (err == NULL) {
271 struct got_pack *pack = NULL;
273 err = got_packidx_get_packfile_path(&path_packfile,
274 packidx->path_packidx);
275 if (err)
276 goto done;
278 pack = got_repo_get_cached_pack(repo, path_packfile);
279 if (pack == NULL) {
280 err = got_repo_cache_pack(&pack, repo, path_packfile,
281 packidx);
282 if (err)
283 goto done;
285 err = read_packed_object_raw(&outbuf, &size, &hdrlen,
286 tempfd, pack, packidx, idx, id);
287 if (err)
288 goto done;
289 } else if (err->code == GOT_ERR_NO_OBJ) {
290 int fd;
292 err = got_object_open_loose_fd(&fd, id, repo);
293 if (err)
294 goto done;
295 err = got_object_read_raw(&outbuf, &size, &hdrlen,
296 GOT_DELTA_RESULT_SIZE_CACHED_MAX, tempfd, id, fd);
297 if (close(fd) == -1 && err == NULL)
298 err = got_error_from_errno("close");
299 if (err)
300 goto done;
303 if (outbuf == NULL) {
304 if (*outfd != -1) {
305 err = got_error_msg(GOT_ERR_NOT_IMPL, "bad outfd");
306 goto done;
309 /*
310 * Duplicate tempfile descriptor to allow use of
311 * fdopen(3) inside got_object_raw_alloc().
312 */
313 *outfd = dup(tempfd);
314 if (*outfd == -1) {
315 err = got_error_from_errno("dup");
316 goto done;
320 err = got_object_raw_alloc(obj, outbuf, outfd,
321 GOT_DELTA_RESULT_SIZE_CACHED_MAX, hdrlen, size);
322 if (err)
323 goto done;
325 err = got_repo_cache_raw_object(repo, id, *obj);
326 if (err) {
327 if (err->code == GOT_ERR_OBJ_EXISTS ||
328 err->code == GOT_ERR_OBJ_TOO_LARGE)
329 err = NULL;
331 done:
332 free(path_packfile);
333 if (err) {
334 if (*obj) {
335 got_object_raw_close(*obj);
336 *obj = NULL;
338 free(outbuf);
339 got_repo_temp_fds_put(tempfile_idx, repo);
340 if (*outfd != -1) {
341 close(*outfd);
342 *outfd = -1;
344 } else {
345 if (((*obj)->f == NULL && (*obj)->fd == -1)) {
346 /* This raw object is not backed by a file. */
347 got_repo_temp_fds_put(tempfile_idx, repo);
348 if (*outfd != -1) {
349 close(*outfd);
350 *outfd = -1;
352 } else {
353 (*obj)->tempfile_idx = tempfile_idx;
354 (*obj)->close_cb = put_raw_object_tempfile;
355 (*obj)->close_arg = repo;
358 return err;
361 static const struct got_error *
362 open_commit(struct got_commit_object **commit,
363 struct got_repository *repo, struct got_object_id *id, int check_cache)
365 const struct got_error *err = NULL;
366 struct got_packidx *packidx = NULL;
367 int idx;
368 char *path_packfile = NULL;
370 if (check_cache) {
371 *commit = got_repo_get_cached_commit(repo, id);
372 if (*commit != NULL) {
373 (*commit)->refcnt++;
374 return NULL;
376 } else
377 *commit = NULL;
379 err = got_repo_search_packidx(&packidx, &idx, repo, id);
380 if (err == NULL) {
381 struct got_pack *pack = NULL;
382 struct got_object *obj;
383 uint8_t *buf;
384 size_t len;
386 err = got_packidx_get_packfile_path(&path_packfile,
387 packidx->path_packidx);
388 if (err)
389 return err;
391 pack = got_repo_get_cached_pack(repo, path_packfile);
392 if (pack == NULL) {
393 err = got_repo_cache_pack(&pack, repo, path_packfile,
394 packidx);
395 if (err)
396 goto done;
398 err = got_packfile_open_object(&obj, pack, packidx, idx, id);
399 if (err)
400 goto done;
401 err = got_packfile_extract_object_to_mem(&buf, &len,
402 obj, pack);
403 got_object_close(obj);
404 if (err)
405 goto done;
406 err = got_object_parse_commit(commit, buf, len);
407 free(buf);
408 } else if (err->code == GOT_ERR_NO_OBJ) {
409 int fd;
411 err = got_object_open_loose_fd(&fd, id, repo);
412 if (err)
413 return err;
414 err = got_object_read_commit(commit, fd, id, 0);
415 if (close(fd) == -1 && err == NULL)
416 err = got_error_from_errno("close");
417 if (err)
418 return err;
421 if (err == NULL) {
422 (*commit)->refcnt++;
423 err = got_repo_cache_commit(repo, id, *commit);
424 if (err) {
425 if (err->code == GOT_ERR_OBJ_EXISTS ||
426 err->code == GOT_ERR_OBJ_TOO_LARGE)
427 err = NULL;
430 done:
431 free(path_packfile);
432 return err;
435 const struct got_error *
436 got_object_open_as_commit(struct got_commit_object **commit,
437 struct got_repository *repo, struct got_object_id *id)
439 *commit = got_repo_get_cached_commit(repo, id);
440 if (*commit != NULL) {
441 (*commit)->refcnt++;
442 return NULL;
445 return open_commit(commit, repo, id, 0);
448 const struct got_error *
449 got_object_commit_open(struct got_commit_object **commit,
450 struct got_repository *repo, struct got_object *obj)
452 return open_commit(commit, repo, got_object_get_id(obj), 1);
455 static const struct got_error *
456 open_tree(struct got_tree_object **tree,
457 struct got_repository *repo, struct got_object_id *id, int check_cache)
459 const struct got_error *err = NULL;
460 struct got_packidx *packidx = NULL;
461 int idx;
462 char *path_packfile = NULL;
463 struct got_parsed_tree_entry *entries = NULL;
464 size_t nentries = 0, nentries_alloc = 0, i;
465 uint8_t *buf = NULL;
467 if (check_cache) {
468 *tree = got_repo_get_cached_tree(repo, id);
469 if (*tree != NULL) {
470 (*tree)->refcnt++;
471 return NULL;
473 } else
474 *tree = NULL;
476 err = got_repo_search_packidx(&packidx, &idx, repo, id);
477 if (err == NULL) {
478 struct got_pack *pack = NULL;
479 struct got_object *obj;
480 size_t len;
482 err = got_packidx_get_packfile_path(&path_packfile,
483 packidx->path_packidx);
484 if (err)
485 return err;
487 pack = got_repo_get_cached_pack(repo, path_packfile);
488 if (pack == NULL) {
489 err = got_repo_cache_pack(&pack, repo, path_packfile,
490 packidx);
491 if (err)
492 goto done;
494 err = got_packfile_open_object(&obj, pack, packidx, idx, id);
495 if (err)
496 goto done;
497 err = got_packfile_extract_object_to_mem(&buf, &len,
498 obj, pack);
499 got_object_close(obj);
500 if (err)
501 goto done;
502 err = got_object_parse_tree(&entries, &nentries,
503 &nentries_alloc, buf, len);
504 if (err)
505 goto done;
506 } else if (err->code == GOT_ERR_NO_OBJ) {
507 int fd;
509 err = got_object_open_loose_fd(&fd, id, repo);
510 if (err)
511 return err;
512 err = got_object_read_tree(&entries, &nentries,
513 &nentries_alloc, &buf, fd, id);
514 if (close(fd) == -1 && err == NULL)
515 err = got_error_from_errno("close");
516 if (err)
517 goto done;
518 } else
519 goto done;
521 *tree = malloc(sizeof(**tree));
522 if (*tree == NULL) {
523 err = got_error_from_errno("malloc");
524 goto done;
526 (*tree)->entries = calloc(nentries, sizeof(struct got_tree_entry));
527 if ((*tree)->entries == NULL) {
528 err = got_error_from_errno("malloc");
529 goto done;
531 (*tree)->nentries = nentries;
532 (*tree)->refcnt = 0;
534 for (i = 0; i < nentries; i++) {
535 struct got_parsed_tree_entry *pe = &entries[i];
536 struct got_tree_entry *te = &(*tree)->entries[i];
538 if (strlcpy(te->name, pe->name,
539 sizeof(te->name)) >= sizeof(te->name)) {
540 err = got_error(GOT_ERR_NO_SPACE);
541 goto done;
543 memcpy(te->id.sha1, pe->id, SHA1_DIGEST_LENGTH);
544 te->mode = pe->mode;
545 te->idx = i;
547 done:
548 free(path_packfile);
549 free(entries);
550 free(buf);
551 if (err == NULL) {
552 (*tree)->refcnt++;
553 err = got_repo_cache_tree(repo, id, *tree);
554 if (err) {
555 if (err->code == GOT_ERR_OBJ_EXISTS ||
556 err->code == GOT_ERR_OBJ_TOO_LARGE)
557 err = NULL;
560 if (err) {
561 if (*tree)
562 free((*tree)->entries);
563 free(*tree);
564 *tree = NULL;
566 return err;
569 const struct got_error *
570 got_object_open_as_tree(struct got_tree_object **tree,
571 struct got_repository *repo, struct got_object_id *id)
573 *tree = got_repo_get_cached_tree(repo, id);
574 if (*tree != NULL) {
575 (*tree)->refcnt++;
576 return NULL;
579 return open_tree(tree, repo, id, 0);
582 const struct got_error *
583 got_object_tree_open(struct got_tree_object **tree,
584 struct got_repository *repo, struct got_object *obj)
586 return open_tree(tree, repo, got_object_get_id(obj), 1);
589 const struct got_error *
590 got_object_open_as_blob(struct got_blob_object **blob,
591 struct got_repository *repo, struct got_object_id *id, size_t blocksize,
592 int outfd)
594 return got_error(GOT_ERR_NOT_IMPL);
597 const struct got_error *
598 got_object_blob_open(struct got_blob_object **blob,
599 struct got_repository *repo, struct got_object *obj, size_t blocksize,
600 int outfd)
602 return got_error(GOT_ERR_NOT_IMPL);
605 static const struct got_error *
606 open_tag(struct got_tag_object **tag, struct got_repository *repo,
607 struct got_object_id *id, int check_cache)
609 const struct got_error *err = NULL;
610 struct got_packidx *packidx = NULL;
611 int idx;
612 char *path_packfile = NULL;
613 struct got_object *obj = NULL;
614 int obj_type = GOT_OBJ_TYPE_ANY;
616 if (check_cache) {
617 *tag = got_repo_get_cached_tag(repo, id);
618 if (*tag != NULL) {
619 (*tag)->refcnt++;
620 return NULL;
622 } else
623 *tag = NULL;
625 err = got_repo_search_packidx(&packidx, &idx, repo, id);
626 if (err == NULL) {
627 struct got_pack *pack = NULL;
628 uint8_t *buf = NULL;
629 size_t len;
631 err = got_packidx_get_packfile_path(&path_packfile,
632 packidx->path_packidx);
633 if (err)
634 return err;
636 pack = got_repo_get_cached_pack(repo, path_packfile);
637 if (pack == NULL) {
638 err = got_repo_cache_pack(&pack, repo, path_packfile,
639 packidx);
640 if (err)
641 goto done;
644 /* Beware of "lightweight" tags: Check object type first. */
645 err = got_packfile_open_object(&obj, pack, packidx, idx, id);
646 if (err)
647 goto done;
648 obj_type = obj->type;
649 if (obj_type != GOT_OBJ_TYPE_TAG) {
650 err = got_error(GOT_ERR_OBJ_TYPE);
651 got_object_close(obj);
652 goto done;
654 err = got_packfile_extract_object_to_mem(&buf, &len,
655 obj, pack);
656 got_object_close(obj);
657 if (err)
658 goto done;
659 err = got_object_parse_tag(tag, buf, len);
660 free(buf);
661 } else if (err->code == GOT_ERR_NO_OBJ) {
662 int fd;
664 err = got_object_open_loose_fd(&fd, id, repo);
665 if (err)
666 return err;
667 err = got_object_read_header(&obj, fd);
668 if (close(fd) == -1 && err == NULL)
669 err = got_error_from_errno("close");
670 if (err)
671 return err;
672 obj_type = obj->type;
673 got_object_close(obj);
674 if (obj_type != GOT_OBJ_TYPE_TAG)
675 return got_error(GOT_ERR_OBJ_TYPE);
677 err = got_object_open_loose_fd(&fd, id, repo);
678 if (err)
679 return err;
680 err = got_object_read_tag(tag, fd, id, 0);
681 if (close(fd) == -1 && err == NULL)
682 err = got_error_from_errno("close");
683 if (err)
684 return err;
687 if (err == NULL) {
688 (*tag)->refcnt++;
689 err = got_repo_cache_tag(repo, id, *tag);
690 if (err) {
691 if (err->code == GOT_ERR_OBJ_EXISTS ||
692 err->code == GOT_ERR_OBJ_TOO_LARGE)
693 err = NULL;
696 done:
697 free(path_packfile);
698 return err;
701 const struct got_error *
702 got_object_open_as_tag(struct got_tag_object **tag,
703 struct got_repository *repo, struct got_object_id *id)
705 *tag = got_repo_get_cached_tag(repo, id);
706 if (*tag != NULL) {
707 (*tag)->refcnt++;
708 return NULL;
711 return open_tag(tag, repo, id, 0);
714 const struct got_error *
715 got_object_tag_open(struct got_tag_object **tag,
716 struct got_repository *repo, struct got_object *obj)
718 return open_tag(tag, repo, got_object_get_id(obj), 1);