Blob


1 /*
2 * Copyright (c) 2018, 2019 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/types.h>
18 #include <sys/stat.h>
19 #include <sys/queue.h>
20 #include <sys/tree.h>
21 #include <sys/uio.h>
22 #include <sys/mman.h>
24 #include <errno.h>
25 #include <fcntl.h>
26 #include <stdio.h>
27 #include <stdlib.h>
28 #include <string.h>
29 #include <sha1.h>
30 #include <sha2.h>
31 #include <unistd.h>
32 #include <zlib.h>
33 #include <libgen.h>
34 #include <limits.h>
35 #include <imsg.h>
37 #include "got_error.h"
38 #include "got_object.h"
39 #include "got_repository.h"
40 #include "got_opentemp.h"
41 #include "got_path.h"
43 #include "got_lib_hash.h"
44 #include "got_lib_delta.h"
45 #include "got_lib_inflate.h"
46 #include "got_lib_object.h"
47 #include "got_lib_object_idcache.h"
48 #include "got_lib_object_cache.h"
49 #include "got_lib_object_parse.h"
50 #include "got_lib_pack.h"
51 #include "got_lib_repository.h"
53 #ifndef MIN
54 #define MIN(_a,_b) ((_a) < (_b) ? (_a) : (_b))
55 #endif
57 #ifndef nitems
58 #define nitems(_a) (sizeof((_a)) / sizeof((_a)[0]))
59 #endif
61 struct got_object_id *
62 got_object_get_id(struct got_object *obj)
63 {
64 return &obj->id;
65 }
67 const struct got_error *
68 got_object_get_id_str(char **outbuf, struct got_object *obj)
69 {
70 return got_object_id_str(outbuf, &obj->id);
71 }
73 const struct got_error *
74 got_object_get_type(int *type, struct got_repository *repo,
75 struct got_object_id *id)
76 {
77 const struct got_error *err = NULL;
78 struct got_object *obj;
80 err = got_object_open(&obj, repo, id);
81 if (err)
82 return err;
84 switch (obj->type) {
85 case GOT_OBJ_TYPE_COMMIT:
86 case GOT_OBJ_TYPE_TREE:
87 case GOT_OBJ_TYPE_BLOB:
88 case GOT_OBJ_TYPE_TAG:
89 *type = obj->type;
90 break;
91 default:
92 err = got_error(GOT_ERR_OBJ_TYPE);
93 break;
94 }
96 got_object_close(obj);
97 return err;
98 }
100 const struct got_error *
101 got_object_get_path(char **path, struct got_object_id *id,
102 struct got_repository *repo)
104 const struct got_error *err = NULL;
105 char *hex = NULL;
106 char *path_objects;
108 *path = NULL;
110 path_objects = got_repo_get_path_objects(repo);
111 if (path_objects == NULL)
112 return got_error_from_errno("got_repo_get_path_objects");
114 err = got_object_id_str(&hex, id);
115 if (err)
116 goto done;
118 if (asprintf(path, "%s/%.2x/%s", path_objects,
119 id->sha1[0], hex + 2) == -1)
120 err = got_error_from_errno("asprintf");
122 done:
123 free(hex);
124 free(path_objects);
125 return err;
128 const struct got_error *
129 got_object_open_loose_fd(int *fd, struct got_object_id *id,
130 struct got_repository *repo)
132 const struct got_error *err = NULL;
133 char *path;
135 err = got_object_get_path(&path, id, repo);
136 if (err)
137 return err;
138 *fd = open(path, O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
139 if (*fd == -1) {
140 err = got_error_from_errno2("open", path);
141 goto done;
143 done:
144 free(path);
145 return err;
148 const struct got_error *
149 got_object_open_by_id_str(struct got_object **obj, struct got_repository *repo,
150 const char *id_str)
152 struct got_object_id id;
154 if (!got_parse_object_id(&id, id_str, GOT_HASH_SHA1))
155 return got_error_path(id_str, GOT_ERR_BAD_OBJ_ID_STR);
157 return got_object_open(obj, repo, &id);
160 const struct got_error *
161 got_object_resolve_id_str(struct got_object_id **id,
162 struct got_repository *repo, const char *id_str)
164 const struct got_error *err = NULL;
165 struct got_object *obj;
167 err = got_object_open_by_id_str(&obj, repo, id_str);
168 if (err)
169 return err;
171 *id = got_object_id_dup(got_object_get_id(obj));
172 got_object_close(obj);
173 if (*id == NULL)
174 return got_error_from_errno("got_object_id_dup");
176 return NULL;
179 int
180 got_object_tree_get_nentries(struct got_tree_object *tree)
182 return tree->nentries;
185 struct got_tree_entry *
186 got_object_tree_get_first_entry(struct got_tree_object *tree)
188 return got_object_tree_get_entry(tree, 0);
191 struct got_tree_entry *
192 got_object_tree_get_last_entry(struct got_tree_object *tree)
194 return got_object_tree_get_entry(tree, tree->nentries - 1);
197 struct got_tree_entry *
198 got_object_tree_get_entry(struct got_tree_object *tree, int i)
200 if (i < 0 || i >= tree->nentries)
201 return NULL;
202 return &tree->entries[i];
205 mode_t
206 got_tree_entry_get_mode(struct got_tree_entry *te)
208 return te->mode;
211 const char *
212 got_tree_entry_get_name(struct got_tree_entry *te)
214 return &te->name[0];
217 struct got_object_id *
218 got_tree_entry_get_id(struct got_tree_entry *te)
220 return &te->id;
223 const struct got_error *
224 got_object_blob_read_to_str(char **s, struct got_blob_object *blob)
226 const struct got_error *err = NULL;
227 size_t len, totlen, hdrlen, offset;
229 *s = NULL;
231 hdrlen = got_object_blob_get_hdrlen(blob);
232 totlen = 0;
233 offset = 0;
234 do {
235 char *p;
237 err = got_object_blob_read_block(&len, blob);
238 if (err)
239 return err;
241 if (len == 0)
242 break;
244 totlen += len - hdrlen;
245 p = realloc(*s, totlen + 1);
246 if (p == NULL) {
247 err = got_error_from_errno("realloc");
248 free(*s);
249 *s = NULL;
250 return err;
252 *s = p;
253 /* Skip blob object header first time around. */
254 memcpy(*s + offset,
255 got_object_blob_get_read_buf(blob) + hdrlen, len - hdrlen);
256 hdrlen = 0;
257 offset = totlen;
258 } while (len > 0);
260 (*s)[totlen] = '\0';
261 return NULL;
264 const struct got_error *
265 got_tree_entry_get_symlink_target(char **link_target, struct got_tree_entry *te,
266 struct got_repository *repo)
268 const struct got_error *err = NULL;
269 struct got_blob_object *blob = NULL;
270 int fd = -1;
272 *link_target = NULL;
274 if (!got_object_tree_entry_is_symlink(te))
275 return got_error(GOT_ERR_TREE_ENTRY_TYPE);
277 fd = got_opentempfd();
278 if (fd == -1) {
279 err = got_error_from_errno("got_opentempfd");
280 goto done;
283 err = got_object_open_as_blob(&blob, repo,
284 got_tree_entry_get_id(te), PATH_MAX, fd);
285 if (err)
286 goto done;
288 err = got_object_blob_read_to_str(link_target, blob);
289 done:
290 if (fd != -1 && close(fd) == -1 && err == NULL)
291 err = got_error_from_errno("close");
292 if (blob)
293 got_object_blob_close(blob);
294 if (err) {
295 free(*link_target);
296 *link_target = NULL;
298 return err;
301 int
302 got_tree_entry_get_index(struct got_tree_entry *te)
304 return te->idx;
307 struct got_tree_entry *
308 got_tree_entry_get_next(struct got_tree_object *tree,
309 struct got_tree_entry *te)
311 return got_object_tree_get_entry(tree, te->idx + 1);
314 struct got_tree_entry *
315 got_tree_entry_get_prev(struct got_tree_object *tree,
316 struct got_tree_entry *te)
318 return got_object_tree_get_entry(tree, te->idx - 1);
321 const struct got_error *
322 got_object_blob_close(struct got_blob_object *blob)
324 const struct got_error *err = NULL;
325 free(blob->read_buf);
326 if (blob->f && fclose(blob->f) == EOF)
327 err = got_error_from_errno("fclose");
328 free(blob->data);
329 free(blob);
330 return err;
333 void
334 got_object_blob_rewind(struct got_blob_object *blob)
336 if (blob->f)
337 rewind(blob->f);
340 char *
341 got_object_blob_id_str(struct got_blob_object *blob, char *buf, size_t size)
343 return got_object_id_hex(&blob->id, buf, size);
346 size_t
347 got_object_blob_get_hdrlen(struct got_blob_object *blob)
349 return blob->hdrlen;
352 const uint8_t *
353 got_object_blob_get_read_buf(struct got_blob_object *blob)
355 return blob->read_buf;
358 const struct got_error *
359 got_object_blob_read_block(size_t *outlenp, struct got_blob_object *blob)
361 size_t n;
363 n = fread(blob->read_buf, 1, blob->blocksize, blob->f);
364 if (n == 0 && ferror(blob->f))
365 return got_ferror(blob->f, GOT_ERR_IO);
366 *outlenp = n;
367 return NULL;
370 const struct got_error *
371 got_object_blob_is_binary(int *binary, struct got_blob_object *blob)
373 const struct got_error *err;
374 size_t hdrlen, len;
376 *binary = 0;
377 hdrlen = got_object_blob_get_hdrlen(blob);
379 if (fseeko(blob->f, hdrlen, SEEK_SET) == -1)
380 return got_error_from_errno("fseeko");
382 err = got_object_blob_read_block(&len, blob);
383 if (err)
384 return err;
386 *binary = memchr(blob->read_buf, '\0', len) != NULL;
388 if (fseeko(blob->f, hdrlen, SEEK_SET) == -1)
389 return got_error_from_errno("fseeko");
390 return NULL;
393 const struct got_error *
394 got_object_blob_getline(char **line, ssize_t *linelen, size_t *linesize,
395 struct got_blob_object *blob)
397 *linelen = getline(line, linesize, blob->f);
398 if (*linelen == -1 && !feof(blob->f))
399 return got_error_from_errno("getline");
400 return NULL;
403 const struct got_error *
404 got_object_blob_dump_to_file(off_t *filesize, int *nlines,
405 off_t **line_offsets, FILE *outfile, struct got_blob_object *blob)
407 const struct got_error *err = NULL;
408 size_t n, len, hdrlen;
409 const uint8_t *buf;
410 int i;
411 const int alloc_chunksz = 512;
412 size_t nalloc = 0;
413 off_t off = 0, total_len = 0;
415 if (line_offsets)
416 *line_offsets = NULL;
417 if (filesize)
418 *filesize = 0;
419 if (nlines)
420 *nlines = 0;
422 hdrlen = got_object_blob_get_hdrlen(blob);
423 do {
424 err = got_object_blob_read_block(&len, blob);
425 if (err)
426 return err;
427 if (len == 0)
428 break;
429 buf = got_object_blob_get_read_buf(blob);
430 i = hdrlen;
431 if (nlines) {
432 if (line_offsets && *line_offsets == NULL) {
433 /* Have some data but perhaps no '\n'. */
434 *nlines = 1;
435 nalloc = alloc_chunksz;
436 *line_offsets = calloc(nalloc,
437 sizeof(**line_offsets));
438 if (*line_offsets == NULL)
439 return got_error_from_errno("calloc");
441 /* Skip forward over end of first line. */
442 while (i < len) {
443 if (buf[i] == '\n')
444 break;
445 i++;
448 /* Scan '\n' offsets in remaining chunk of data. */
449 while (i < len) {
450 if (buf[i] != '\n') {
451 i++;
452 continue;
454 (*nlines)++;
455 if (line_offsets && nalloc < *nlines) {
456 size_t n = *nlines + alloc_chunksz;
457 off_t *o = recallocarray(*line_offsets,
458 nalloc, n, sizeof(**line_offsets));
459 if (o == NULL) {
460 free(*line_offsets);
461 *line_offsets = NULL;
462 return got_error_from_errno(
463 "recallocarray");
465 *line_offsets = o;
466 nalloc = n;
468 if (line_offsets) {
469 off = total_len + i - hdrlen + 1;
470 (*line_offsets)[*nlines - 1] = off;
472 i++;
475 /* Skip blob object header first time around. */
476 n = fwrite(buf + hdrlen, 1, len - hdrlen, outfile);
477 if (n != len - hdrlen)
478 return got_ferror(outfile, GOT_ERR_IO);
479 total_len += len - hdrlen;
480 hdrlen = 0;
481 } while (len != 0);
483 if (fflush(outfile) != 0)
484 return got_error_from_errno("fflush");
485 rewind(outfile);
487 if (filesize)
488 *filesize = total_len;
490 return NULL;
493 const char *
494 got_object_tag_get_name(struct got_tag_object *tag)
496 return tag->tag;
499 int
500 got_object_tag_get_object_type(struct got_tag_object *tag)
502 return tag->obj_type;
505 struct got_object_id *
506 got_object_tag_get_object_id(struct got_tag_object *tag)
508 return &tag->id;
511 time_t
512 got_object_tag_get_tagger_time(struct got_tag_object *tag)
514 return tag->tagger_time;
517 time_t
518 got_object_tag_get_tagger_gmtoff(struct got_tag_object *tag)
520 return tag->tagger_gmtoff;
523 const char *
524 got_object_tag_get_tagger(struct got_tag_object *tag)
526 return tag->tagger;
529 const char *
530 got_object_tag_get_message(struct got_tag_object *tag)
532 return tag->tagmsg;
535 static struct got_tree_entry *
536 find_entry_by_name(struct got_tree_object *tree, const char *name, size_t len)
538 int i;
540 /* Note that tree entries are sorted in strncmp() order. */
541 for (i = 0; i < tree->nentries; i++) {
542 struct got_tree_entry *te = &tree->entries[i];
543 int cmp = strncmp(te->name, name, len);
544 if (cmp < 0)
545 continue;
546 if (cmp > 0)
547 break;
548 if (te->name[len] == '\0')
549 return te;
551 return NULL;
554 struct got_tree_entry *
555 got_object_tree_find_entry(struct got_tree_object *tree, const char *name)
557 return find_entry_by_name(tree, name, strlen(name));
560 const struct got_error *
561 got_object_tree_find_path(struct got_object_id **id, mode_t *mode,
562 struct got_repository *repo, struct got_tree_object *tree,
563 const char *path)
565 const struct got_error *err = NULL;
566 struct got_tree_object *subtree = NULL;
567 struct got_tree_entry *te = NULL;
568 const char *seg, *s;
569 size_t seglen;
571 *id = NULL;
573 s = path;
574 while (s[0] == '/')
575 s++;
576 seg = s;
577 seglen = 0;
578 subtree = tree;
579 while (*s) {
580 struct got_tree_object *next_tree;
582 if (*s != '/') {
583 s++;
584 seglen++;
585 if (*s)
586 continue;
589 te = find_entry_by_name(subtree, seg, seglen);
590 if (te == NULL) {
591 err = got_error_path(path, GOT_ERR_NO_TREE_ENTRY);
592 goto done;
595 if (*s == '\0')
596 break;
598 seg = s + 1;
599 seglen = 0;
600 s++;
601 if (*s) {
602 err = got_object_open_as_tree(&next_tree, repo,
603 &te->id);
604 te = NULL;
605 if (err)
606 goto done;
607 if (subtree != tree)
608 got_object_tree_close(subtree);
609 subtree = next_tree;
613 if (te) {
614 *id = got_object_id_dup(&te->id);
615 if (*id == NULL)
616 return got_error_from_errno("got_object_id_dup");
617 if (mode)
618 *mode = te->mode;
619 } else
620 err = got_error_path(path, GOT_ERR_NO_TREE_ENTRY);
621 done:
622 if (subtree && subtree != tree)
623 got_object_tree_close(subtree);
624 return err;
627 const struct got_error *
628 got_object_id_by_path(struct got_object_id **id, struct got_repository *repo,
629 struct got_commit_object *commit, const char *path)
631 const struct got_error *err = NULL;
632 struct got_tree_object *tree = NULL;
634 *id = NULL;
636 /* Handle opening of root of commit's tree. */
637 if (got_path_is_root_dir(path)) {
638 *id = got_object_id_dup(commit->tree_id);
639 if (*id == NULL)
640 err = got_error_from_errno("got_object_id_dup");
641 } else {
642 err = got_object_open_as_tree(&tree, repo, commit->tree_id);
643 if (err)
644 goto done;
645 err = got_object_tree_find_path(id, NULL, repo, tree, path);
647 done:
648 if (tree)
649 got_object_tree_close(tree);
650 return err;
653 /*
654 * Normalize file mode bits to avoid false positive tree entry differences
655 * in case tree entries have unexpected mode bits set.
656 */
657 static mode_t
658 normalize_mode_for_comparison(mode_t mode)
660 /*
661 * For directories, the only relevant bit is the IFDIR bit.
662 * This allows us to detect paths changing from a directory
663 * to a file and vice versa.
664 */
665 if (S_ISDIR(mode))
666 return mode & S_IFDIR;
668 /*
669 * For symlinks, the only relevant bit is the IFLNK bit.
670 * This allows us to detect paths changing from a symlinks
671 * to a file or directory and vice versa.
672 */
673 if (S_ISLNK(mode))
674 return mode & S_IFLNK;
676 /* For files, the only change we care about is the executable bit. */
677 return mode & S_IXUSR;
680 const struct got_error *
681 got_object_tree_path_changed(int *changed,
682 struct got_tree_object *tree01, struct got_tree_object *tree02,
683 const char *path, struct got_repository *repo)
685 const struct got_error *err = NULL;
686 struct got_tree_object *tree1 = NULL, *tree2 = NULL;
687 struct got_tree_entry *te1 = NULL, *te2 = NULL;
688 const char *seg, *s;
689 size_t seglen;
691 *changed = 0;
693 /* We not do support comparing the root path. */
694 if (got_path_is_root_dir(path))
695 return got_error_path(path, GOT_ERR_BAD_PATH);
697 tree1 = tree01;
698 tree2 = tree02;
699 s = path;
700 while (*s == '/')
701 s++;
702 seg = s;
703 seglen = 0;
704 while (*s) {
705 struct got_tree_object *next_tree1, *next_tree2;
706 mode_t mode1, mode2;
708 if (*s != '/') {
709 s++;
710 seglen++;
711 if (*s)
712 continue;
715 te1 = find_entry_by_name(tree1, seg, seglen);
716 if (te1 == NULL) {
717 err = got_error(GOT_ERR_NO_OBJ);
718 goto done;
721 if (tree2)
722 te2 = find_entry_by_name(tree2, seg, seglen);
724 if (te2) {
725 mode1 = normalize_mode_for_comparison(te1->mode);
726 mode2 = normalize_mode_for_comparison(te2->mode);
727 if (mode1 != mode2) {
728 *changed = 1;
729 goto done;
732 if (got_object_id_cmp(&te1->id, &te2->id) == 0) {
733 *changed = 0;
734 goto done;
738 if (*s == '\0') { /* final path element */
739 *changed = 1;
740 goto done;
743 seg = s + 1;
744 s++;
745 seglen = 0;
746 if (*s) {
747 err = got_object_open_as_tree(&next_tree1, repo,
748 &te1->id);
749 te1 = NULL;
750 if (err)
751 goto done;
752 if (tree1 != tree01)
753 got_object_tree_close(tree1);
754 tree1 = next_tree1;
756 if (te2) {
757 err = got_object_open_as_tree(&next_tree2, repo,
758 &te2->id);
759 te2 = NULL;
760 if (err)
761 goto done;
762 if (tree2 != tree02)
763 got_object_tree_close(tree2);
764 tree2 = next_tree2;
765 } else if (tree2) {
766 if (tree2 != tree02)
767 got_object_tree_close(tree2);
768 tree2 = NULL;
772 done:
773 if (tree1 && tree1 != tree01)
774 got_object_tree_close(tree1);
775 if (tree2 && tree2 != tree02)
776 got_object_tree_close(tree2);
777 return err;
780 const struct got_error *
781 got_object_tree_entry_dup(struct got_tree_entry **new_te,
782 struct got_tree_entry *te)
784 const struct got_error *err = NULL;
786 *new_te = calloc(1, sizeof(**new_te));
787 if (*new_te == NULL)
788 return got_error_from_errno("calloc");
790 (*new_te)->mode = te->mode;
791 memcpy((*new_te)->name, te->name, sizeof((*new_te)->name));
792 memcpy(&(*new_te)->id, &te->id, sizeof((*new_te)->id));
793 return err;
796 int
797 got_object_tree_entry_is_submodule(struct got_tree_entry *te)
799 return (te->mode & S_IFMT) == (S_IFDIR | S_IFLNK);
802 int
803 got_object_tree_entry_is_symlink(struct got_tree_entry *te)
805 /* S_IFDIR check avoids confusing symlinks with submodules. */
806 return ((te->mode & (S_IFDIR | S_IFLNK)) == S_IFLNK);
809 static const struct got_error *
810 resolve_symlink(char **link_target, const char *path,
811 struct got_commit_object *commit, struct got_repository *repo)
813 const struct got_error *err = NULL;
814 char buf[PATH_MAX];
815 char *name, *parent_path = NULL;
816 struct got_object_id *tree_obj_id = NULL;
817 struct got_tree_object *tree = NULL;
818 struct got_tree_entry *te = NULL;
820 *link_target = NULL;
822 if (strlcpy(buf, path, sizeof(buf)) >= sizeof(buf))
823 return got_error(GOT_ERR_NO_SPACE);
825 name = basename(buf);
826 if (name == NULL)
827 return got_error_from_errno2("basename", path);
829 err = got_path_dirname(&parent_path, path);
830 if (err)
831 return err;
833 err = got_object_id_by_path(&tree_obj_id, repo, commit,
834 parent_path);
835 if (err) {
836 if (err->code == GOT_ERR_NO_TREE_ENTRY) {
837 /* Display the complete path in error message. */
838 err = got_error_path(path, err->code);
840 goto done;
843 err = got_object_open_as_tree(&tree, repo, tree_obj_id);
844 if (err)
845 goto done;
847 te = got_object_tree_find_entry(tree, name);
848 if (te == NULL) {
849 err = got_error_path(path, GOT_ERR_NO_TREE_ENTRY);
850 goto done;
853 if (got_object_tree_entry_is_symlink(te)) {
854 err = got_tree_entry_get_symlink_target(link_target, te, repo);
855 if (err)
856 goto done;
857 if (!got_path_is_absolute(*link_target)) {
858 char *abspath;
859 if (asprintf(&abspath, "%s/%s", parent_path,
860 *link_target) == -1) {
861 err = got_error_from_errno("asprintf");
862 goto done;
864 free(*link_target);
865 *link_target = malloc(PATH_MAX);
866 if (*link_target == NULL) {
867 err = got_error_from_errno("malloc");
868 goto done;
870 err = got_canonpath(abspath, *link_target, PATH_MAX);
871 free(abspath);
872 if (err)
873 goto done;
876 done:
877 free(parent_path);
878 free(tree_obj_id);
879 if (tree)
880 got_object_tree_close(tree);
881 if (err) {
882 free(*link_target);
883 *link_target = NULL;
885 return err;
888 const struct got_error *
889 got_object_resolve_symlinks(char **link_target, const char *path,
890 struct got_commit_object *commit, struct got_repository *repo)
892 const struct got_error *err = NULL;
893 char *next_target = NULL;
894 int max_recursion = 40; /* matches Git */
896 *link_target = NULL;
898 do {
899 err = resolve_symlink(&next_target,
900 *link_target ? *link_target : path, commit, repo);
901 if (err)
902 break;
903 if (next_target) {
904 free(*link_target);
905 if (--max_recursion == 0) {
906 err = got_error_path(path, GOT_ERR_RECURSION);
907 *link_target = NULL;
908 break;
910 *link_target = next_target;
912 } while (next_target);
914 return err;
917 void
918 got_object_commit_retain(struct got_commit_object *commit)
920 commit->refcnt++;
923 const struct got_error *
924 got_object_raw_alloc(struct got_raw_object **obj, uint8_t *outbuf, int *outfd,
925 size_t max_in_mem_size, size_t hdrlen, off_t size)
927 const struct got_error *err = NULL;
928 off_t tot;
930 tot = hdrlen + size;
932 *obj = calloc(1, sizeof(**obj));
933 if (*obj == NULL) {
934 err = got_error_from_errno("calloc");
935 goto done;
937 (*obj)->fd = -1;
938 (*obj)->tempfile_idx = -1;
940 if (outbuf) {
941 (*obj)->data = outbuf;
942 } else {
943 struct stat sb;
944 if (fstat(*outfd, &sb) == -1) {
945 err = got_error_from_errno("fstat");
946 goto done;
949 if (sb.st_size != tot) {
950 err = got_error_msg(GOT_ERR_BAD_OBJ_HDR,
951 "raw object has unexpected size");
952 goto done;
954 #ifndef GOT_PACK_NO_MMAP
955 if (tot > 0 && tot <= max_in_mem_size) {
956 (*obj)->data = mmap(NULL, tot, PROT_READ,
957 MAP_PRIVATE, *outfd, 0);
958 if ((*obj)->data == MAP_FAILED) {
959 if (errno != ENOMEM) {
960 err = got_error_from_errno("mmap");
961 goto done;
963 (*obj)->data = NULL;
964 } else {
965 (*obj)->fd = *outfd;
966 *outfd = -1;
969 #endif
970 if (*outfd != -1) {
971 (*obj)->f = fdopen(*outfd, "r");
972 if ((*obj)->f == NULL) {
973 err = got_error_from_errno("fdopen");
974 goto done;
976 *outfd = -1;
979 (*obj)->hdrlen = hdrlen;
980 (*obj)->size = size;
981 done:
982 if (err) {
983 if (*obj) {
984 got_object_raw_close(*obj);
985 *obj = NULL;
987 } else
988 (*obj)->refcnt++;
989 return err;