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/stat.h>
20 #include <errno.h>
21 #include <limits.h>
22 #include <stdio.h>
23 #include <stdlib.h>
24 #include <string.h>
25 #include <unistd.h>
27 #include "got_error.h"
28 #include "got_object.h"
29 #include "got_repository.h"
30 #include "got_path.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)
42 {
43 const struct got_error *err = NULL;
44 struct got_pack *pack = NULL;
45 struct got_packidx *packidx = NULL;
46 int idx;
47 char *path_packfile;
49 err = got_repo_search_packidx(&packidx, &idx, repo, id);
50 if (err)
51 return err;
53 err = got_packidx_get_packfile_path(&path_packfile,
54 packidx->path_packidx);
55 if (err)
56 return err;
58 pack = got_repo_get_cached_pack(repo, path_packfile);
59 if (pack == NULL) {
60 err = got_repo_cache_pack(&pack, repo, path_packfile, packidx);
61 if (err)
62 goto done;
63 }
65 err = got_packfile_open_object(obj, pack, packidx, idx, id);
66 if (err)
67 return err;
68 (*obj)->refcnt++;
70 err = got_repo_cache_object(repo, id, *obj);
71 if (err) {
72 if (err->code == GOT_ERR_OBJ_EXISTS ||
73 err->code == GOT_ERR_OBJ_TOO_LARGE)
74 err = NULL;
75 }
76 done:
77 free(path_packfile);
78 return err;
79 }
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)
85 {
86 return got_error(GOT_ERR_NOT_IMPL);
87 }
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)
95 {
96 return got_error(GOT_ERR_NOT_IMPL);
97 }
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;
104 int fd;
106 *obj = got_repo_get_cached_object(repo, id);
107 if (*obj != NULL) {
108 (*obj)->refcnt++;
109 return NULL;
112 err = got_object_open_packed(obj, id, repo);
113 if (err) {
114 if (err->code != GOT_ERR_NO_OBJ)
115 return err;
116 } else
117 return NULL;
119 err = got_object_open_loose_fd(&fd, id, repo);
120 if (err) {
121 if (err->code == GOT_ERR_ERRNO && errno == ENOENT)
122 err = got_error_no_obj(id);
123 return err;
126 err = got_object_read_header(obj, fd);
127 if (err)
128 goto done;
130 memcpy(&(*obj)->id, id, sizeof((*obj)->id));
131 (*obj)->refcnt++;
133 err = got_repo_cache_object(repo, id, *obj);
134 if (err) {
135 if (err->code == GOT_ERR_OBJ_EXISTS ||
136 err->code == GOT_ERR_OBJ_TOO_LARGE)
137 err = NULL;
139 done:
140 if (close(fd) == -1 && err == NULL)
141 err = got_error_from_errno("close");
142 return err;
145 static const struct got_error *
146 wrap_fd(FILE **f, int wrapped_fd)
148 const struct got_error *err = NULL;
149 int fd;
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);
158 if (fd == -1)
159 return got_error_from_errno("dup");
161 *f = fdopen(fd, "w+");
162 if (*f == NULL) {
163 err = got_error_from_errno("fdopen");
164 close(fd);
166 return err;
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;
179 *outbuf = NULL;
180 *size = 0;
181 *hdrlen = 0;
183 err = got_packfile_open_object(&obj, pack, packidx, idx, id);
184 if (err)
185 return err;
187 if (obj->flags & GOT_OBJ_FLAG_DELTIFIED) {
188 err = got_pack_get_max_delta_object_size(&raw_size, obj, pack);
189 if (err)
190 goto done;
191 } else
192 raw_size = obj->size;
194 if (raw_size <= GOT_DELTA_RESULT_SIZE_CACHED_MAX) {
195 size_t len;
196 err = got_packfile_extract_object_to_mem(outbuf, &len,
197 obj, pack);
198 if (err)
199 goto done;
200 *size = (off_t)len;
201 } else {
202 /*
203 * XXX This uses 3 file extra descriptors for no good reason.
204 * We should have got_packfile_extract_object_to_fd().
205 */
206 err = wrap_fd(&outfile, outfd);
207 if (err)
208 goto done;
209 err = wrap_fd(&basefile, pack->basefd);
210 if (err)
211 goto done;
212 err = wrap_fd(&accumfile, pack->accumfd);
213 if (err)
214 goto done;
215 err = got_packfile_extract_object(pack, obj, outfile, basefile,
216 accumfile);
217 if (err)
218 goto done;
219 *size = obj->size;
222 *hdrlen = obj->hdrlen;
223 done:
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");
231 return err;
235 static void
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;
253 off_t size = 0;
254 size_t hdrlen = 0;
255 char *path_packfile = NULL;
257 *obj = got_repo_get_cached_raw_object(repo, id);
258 if (*obj != NULL) {
259 (*obj)->refcnt++;
260 return NULL;
263 err = got_repo_temp_fds_get(&tempfd, &tempfile_idx, repo);
264 if (err)
265 return err;
267 err = got_repo_search_packidx(&packidx, &idx, repo, id);
268 if (err == NULL) {
269 struct got_pack *pack = NULL;
271 err = got_packidx_get_packfile_path(&path_packfile,
272 packidx->path_packidx);
273 if (err)
274 goto done;
276 pack = got_repo_get_cached_pack(repo, path_packfile);
277 if (pack == NULL) {
278 err = got_repo_cache_pack(&pack, repo, path_packfile,
279 packidx);
280 if (err)
281 goto done;
283 err = read_packed_object_raw(&outbuf, &size, &hdrlen,
284 tempfd, pack, packidx, idx, id);
285 if (err)
286 goto done;
287 } else if (err->code == GOT_ERR_NO_OBJ) {
288 int fd;
290 err = got_object_open_loose_fd(&fd, id, repo);
291 if (err)
292 goto done;
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");
297 if (err)
298 goto done;
301 if (outbuf == NULL) {
302 if (*outfd != -1) {
303 err = got_error_msg(GOT_ERR_NOT_IMPL, "bad outfd");
304 goto done;
307 /*
308 * Duplicate tempfile descriptor to allow use of
309 * fdopen(3) inside got_object_raw_alloc().
310 */
311 *outfd = dup(tempfd);
312 if (*outfd == -1) {
313 err = got_error_from_errno("dup");
314 goto done;
318 err = got_object_raw_alloc(obj, outbuf, outfd,
319 GOT_DELTA_RESULT_SIZE_CACHED_MAX, hdrlen, size);
320 if (err)
321 goto done;
323 err = got_repo_cache_raw_object(repo, id, *obj);
324 if (err) {
325 if (err->code == GOT_ERR_OBJ_EXISTS ||
326 err->code == GOT_ERR_OBJ_TOO_LARGE)
327 err = NULL;
329 done:
330 free(path_packfile);
331 if (err) {
332 if (*obj) {
333 got_object_raw_close(*obj);
334 *obj = NULL;
336 free(outbuf);
337 got_repo_temp_fds_put(tempfile_idx, repo);
338 if (*outfd != -1) {
339 close(*outfd);
340 *outfd = -1;
342 } else {
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);
346 if (*outfd != -1) {
347 close(*outfd);
348 *outfd = -1;
350 } else {
351 (*obj)->tempfile_idx = tempfile_idx;
352 (*obj)->close_cb = put_raw_object_tempfile;
353 (*obj)->close_arg = repo;
356 return err;
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;
365 int idx;
366 char *path_packfile = NULL;
368 if (check_cache) {
369 *commit = got_repo_get_cached_commit(repo, id);
370 if (*commit != NULL) {
371 (*commit)->refcnt++;
372 return NULL;
374 } else
375 *commit = NULL;
377 err = got_repo_search_packidx(&packidx, &idx, repo, id);
378 if (err == NULL) {
379 struct got_pack *pack = NULL;
380 struct got_object *obj;
381 uint8_t *buf;
382 size_t len;
384 err = got_packidx_get_packfile_path(&path_packfile,
385 packidx->path_packidx);
386 if (err)
387 return err;
389 pack = got_repo_get_cached_pack(repo, path_packfile);
390 if (pack == NULL) {
391 err = got_repo_cache_pack(&pack, repo, path_packfile,
392 packidx);
393 if (err)
394 goto done;
396 err = got_packfile_open_object(&obj, pack, packidx, idx, id);
397 if (err)
398 goto done;
399 err = got_packfile_extract_object_to_mem(&buf, &len,
400 obj, pack);
401 got_object_close(obj);
402 if (err)
403 goto done;
404 err = got_object_parse_commit(commit, buf, len);
405 free(buf);
406 } else if (err->code == GOT_ERR_NO_OBJ) {
407 int fd;
409 err = got_object_open_loose_fd(&fd, id, repo);
410 if (err)
411 return err;
412 err = got_object_read_commit(commit, fd, id, 0);
413 if (close(fd) == -1 && err == NULL)
414 err = got_error_from_errno("close");
415 if (err)
416 return err;
419 if (err == NULL) {
420 (*commit)->refcnt++;
421 err = got_repo_cache_commit(repo, id, *commit);
422 if (err) {
423 if (err->code == GOT_ERR_OBJ_EXISTS ||
424 err->code == GOT_ERR_OBJ_TOO_LARGE)
425 err = NULL;
428 done:
429 free(path_packfile);
430 return err;
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) {
439 (*commit)->refcnt++;
440 return 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;
459 int idx;
460 char *path_packfile = NULL;
461 struct got_parsed_tree_entry *entries = NULL;
462 size_t nentries = 0, nentries_alloc = 0, i;
463 uint8_t *buf = NULL;
465 if (check_cache) {
466 *tree = got_repo_get_cached_tree(repo, id);
467 if (*tree != NULL) {
468 (*tree)->refcnt++;
469 return NULL;
471 } else
472 *tree = NULL;
474 err = got_repo_search_packidx(&packidx, &idx, repo, id);
475 if (err == NULL) {
476 struct got_pack *pack = NULL;
477 struct got_object *obj;
478 size_t len;
480 err = got_packidx_get_packfile_path(&path_packfile,
481 packidx->path_packidx);
482 if (err)
483 return err;
485 pack = got_repo_get_cached_pack(repo, path_packfile);
486 if (pack == NULL) {
487 err = got_repo_cache_pack(&pack, repo, path_packfile,
488 packidx);
489 if (err)
490 goto done;
492 err = got_packfile_open_object(&obj, pack, packidx, idx, id);
493 if (err)
494 goto done;
495 err = got_packfile_extract_object_to_mem(&buf, &len,
496 obj, pack);
497 got_object_close(obj);
498 if (err)
499 goto done;
500 err = got_object_parse_tree(&entries, &nentries,
501 &nentries_alloc, buf, len);
502 if (err)
503 goto done;
504 } else if (err->code == GOT_ERR_NO_OBJ) {
505 int fd;
507 err = got_object_open_loose_fd(&fd, id, repo);
508 if (err)
509 return err;
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");
514 if (err)
515 goto done;
516 } else
517 goto done;
519 *tree = malloc(sizeof(**tree));
520 if (*tree == NULL) {
521 err = got_error_from_errno("malloc");
522 goto done;
524 (*tree)->entries = calloc(nentries, sizeof(struct got_tree_entry));
525 if ((*tree)->entries == NULL) {
526 err = got_error_from_errno("malloc");
527 goto done;
529 (*tree)->nentries = nentries;
530 (*tree)->refcnt = 0;
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);
539 goto done;
541 memcpy(te->id.sha1, pe->id, SHA1_DIGEST_LENGTH);
542 te->mode = pe->mode;
543 te->idx = i;
545 done:
546 free(path_packfile);
547 free(entries);
548 free(buf);
549 if (err == NULL) {
550 (*tree)->refcnt++;
551 err = got_repo_cache_tree(repo, id, *tree);
552 if (err) {
553 if (err->code == GOT_ERR_OBJ_EXISTS ||
554 err->code == GOT_ERR_OBJ_TOO_LARGE)
555 err = NULL;
558 if (err) {
559 if (*tree)
560 free((*tree)->entries);
561 free(*tree);
562 *tree = NULL;
564 return err;
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);
572 if (*tree != NULL) {
573 (*tree)->refcnt++;
574 return NULL;
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,
590 int outfd)
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,
598 int outfd)
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;
609 int idx;
610 char *path_packfile = NULL;
611 struct got_object *obj = NULL;
612 int obj_type = GOT_OBJ_TYPE_ANY;
614 if (check_cache) {
615 *tag = got_repo_get_cached_tag(repo, id);
616 if (*tag != NULL) {
617 (*tag)->refcnt++;
618 return NULL;
620 } else
621 *tag = NULL;
623 err = got_repo_search_packidx(&packidx, &idx, repo, id);
624 if (err == NULL) {
625 struct got_pack *pack = NULL;
626 uint8_t *buf = NULL;
627 size_t len;
629 err = got_packidx_get_packfile_path(&path_packfile,
630 packidx->path_packidx);
631 if (err)
632 return err;
634 pack = got_repo_get_cached_pack(repo, path_packfile);
635 if (pack == NULL) {
636 err = got_repo_cache_pack(&pack, repo, path_packfile,
637 packidx);
638 if (err)
639 goto done;
642 /* Beware of "lightweight" tags: Check object type first. */
643 err = got_packfile_open_object(&obj, pack, packidx, idx, id);
644 if (err)
645 goto done;
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);
650 goto done;
652 err = got_packfile_extract_object_to_mem(&buf, &len,
653 obj, pack);
654 got_object_close(obj);
655 if (err)
656 goto done;
657 err = got_object_parse_tag(tag, buf, len);
658 free(buf);
659 } else if (err->code == GOT_ERR_NO_OBJ) {
660 int fd;
662 err = got_object_open_loose_fd(&fd, id, repo);
663 if (err)
664 return err;
665 err = got_object_read_header(&obj, fd);
666 if (close(fd) == -1 && err == NULL)
667 err = got_error_from_errno("close");
668 if (err)
669 return err;
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);
676 if (err)
677 return err;
678 err = got_object_read_tag(tag, fd, id, 0);
679 if (close(fd) == -1 && err == NULL)
680 err = got_error_from_errno("close");
681 if (err)
682 return err;
685 if (err == NULL) {
686 (*tag)->refcnt++;
687 err = got_repo_cache_tag(repo, id, *tag);
688 if (err) {
689 if (err->code == GOT_ERR_OBJ_EXISTS ||
690 err->code == GOT_ERR_OBJ_TOO_LARGE)
691 err = NULL;
694 done:
695 free(path_packfile);
696 return err;
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);
704 if (*tag != NULL) {
705 (*tag)->refcnt++;
706 return NULL;
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);