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 <unistd.h>
31 #include <zlib.h>
32 #include <libgen.h>
33 #include <limits.h>
34 #include <imsg.h>
36 #include "got_error.h"
37 #include "got_object.h"
38 #include "got_repository.h"
39 #include "got_opentemp.h"
40 #include "got_path.h"
42 #include "got_lib_sha1.h"
43 #include "got_lib_delta.h"
44 #include "got_lib_inflate.h"
45 #include "got_lib_object.h"
46 #include "got_lib_object_idcache.h"
47 #include "got_lib_object_cache.h"
48 #include "got_lib_object_parse.h"
49 #include "got_lib_pack.h"
50 #include "got_lib_repository.h"
52 #ifndef MIN
53 #define MIN(_a,_b) ((_a) < (_b) ? (_a) : (_b))
54 #endif
56 #ifndef nitems
57 #define nitems(_a) (sizeof((_a)) / sizeof((_a)[0]))
58 #endif
60 struct got_object_id *
61 got_object_get_id(struct got_object *obj)
62 {
63 return &obj->id;
64 }
66 const struct got_error *
67 got_object_get_id_str(char **outbuf, struct got_object *obj)
68 {
69 return got_object_id_str(outbuf, &obj->id);
70 }
72 const struct got_error *
73 got_object_get_type(int *type, struct got_repository *repo,
74 struct got_object_id *id)
75 {
76 const struct got_error *err = NULL;
77 struct got_object *obj;
79 err = got_object_open(&obj, repo, id);
80 if (err)
81 return err;
83 switch (obj->type) {
84 case GOT_OBJ_TYPE_COMMIT:
85 case GOT_OBJ_TYPE_TREE:
86 case GOT_OBJ_TYPE_BLOB:
87 case GOT_OBJ_TYPE_TAG:
88 *type = obj->type;
89 break;
90 default:
91 err = got_error(GOT_ERR_OBJ_TYPE);
92 break;
93 }
95 got_object_close(obj);
96 return err;
97 }
99 const struct got_error *
100 got_object_get_path(char **path, struct got_object_id *id,
101 struct got_repository *repo)
103 const struct got_error *err = NULL;
104 char *hex = NULL;
105 char *path_objects;
107 *path = NULL;
109 path_objects = got_repo_get_path_objects(repo);
110 if (path_objects == NULL)
111 return got_error_from_errno("got_repo_get_path_objects");
113 err = got_object_id_str(&hex, id);
114 if (err)
115 goto done;
117 if (asprintf(path, "%s/%.2x/%s", path_objects,
118 id->sha1[0], hex + 2) == -1)
119 err = got_error_from_errno("asprintf");
121 done:
122 free(hex);
123 free(path_objects);
124 return err;
127 const struct got_error *
128 got_object_open_loose_fd(int *fd, struct got_object_id *id,
129 struct got_repository *repo)
131 const struct got_error *err = NULL;
132 char *path;
134 err = got_object_get_path(&path, id, repo);
135 if (err)
136 return err;
137 *fd = open(path, O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
138 if (*fd == -1) {
139 err = got_error_from_errno2("open", path);
140 goto done;
142 done:
143 free(path);
144 return err;
147 const struct got_error *
148 got_object_open_by_id_str(struct got_object **obj, struct got_repository *repo,
149 const char *id_str)
151 struct got_object_id id;
153 if (!got_parse_sha1_digest(id.sha1, id_str))
154 return got_error_path(id_str, GOT_ERR_BAD_OBJ_ID_STR);
156 return got_object_open(obj, repo, &id);
159 const struct got_error *
160 got_object_resolve_id_str(struct got_object_id **id,
161 struct got_repository *repo, const char *id_str)
163 const struct got_error *err = NULL;
164 struct got_object *obj;
166 err = got_object_open_by_id_str(&obj, repo, id_str);
167 if (err)
168 return err;
170 *id = got_object_id_dup(got_object_get_id(obj));
171 got_object_close(obj);
172 if (*id == NULL)
173 return got_error_from_errno("got_object_id_dup");
175 return NULL;
178 const struct got_error *
179 got_object_qid_alloc(struct got_object_qid **qid, struct got_object_id *id)
181 *qid = calloc(1, sizeof(**qid));
182 if (*qid == NULL)
183 return got_error_from_errno("calloc");
185 memcpy(&(*qid)->id, id, sizeof((*qid)->id));
186 return NULL;
189 const struct got_error *
190 got_object_id_queue_copy(const struct got_object_id_queue *src,
191 struct got_object_id_queue *dest)
193 const struct got_error *err;
194 struct got_object_qid *qid;
196 STAILQ_FOREACH(qid, src, entry) {
197 struct got_object_qid *new;
198 /*
199 * Deep-copy the object ID only. Let the caller deal
200 * with setting up the new->data pointer if needed.
201 */
202 err = got_object_qid_alloc(&new, &qid->id);
203 if (err) {
204 got_object_id_queue_free(dest);
205 return err;
207 STAILQ_INSERT_TAIL(dest, new, entry);
210 return NULL;
213 int
214 got_object_tree_get_nentries(struct got_tree_object *tree)
216 return tree->nentries;
219 struct got_tree_entry *
220 got_object_tree_get_first_entry(struct got_tree_object *tree)
222 return got_object_tree_get_entry(tree, 0);
225 struct got_tree_entry *
226 got_object_tree_get_last_entry(struct got_tree_object *tree)
228 return got_object_tree_get_entry(tree, tree->nentries - 1);
231 struct got_tree_entry *
232 got_object_tree_get_entry(struct got_tree_object *tree, int i)
234 if (i < 0 || i >= tree->nentries)
235 return NULL;
236 return &tree->entries[i];
239 mode_t
240 got_tree_entry_get_mode(struct got_tree_entry *te)
242 return te->mode;
245 const char *
246 got_tree_entry_get_name(struct got_tree_entry *te)
248 return &te->name[0];
251 struct got_object_id *
252 got_tree_entry_get_id(struct got_tree_entry *te)
254 return &te->id;
257 const struct got_error *
258 got_object_blob_read_to_str(char **s, struct got_blob_object *blob)
260 const struct got_error *err = NULL;
261 size_t len, totlen, hdrlen, offset;
263 *s = NULL;
265 hdrlen = got_object_blob_get_hdrlen(blob);
266 totlen = 0;
267 offset = 0;
268 do {
269 char *p;
271 err = got_object_blob_read_block(&len, blob);
272 if (err)
273 return err;
275 if (len == 0)
276 break;
278 totlen += len - hdrlen;
279 p = realloc(*s, totlen + 1);
280 if (p == NULL) {
281 err = got_error_from_errno("realloc");
282 free(*s);
283 *s = NULL;
284 return err;
286 *s = p;
287 /* Skip blob object header first time around. */
288 memcpy(*s + offset,
289 got_object_blob_get_read_buf(blob) + hdrlen, len - hdrlen);
290 hdrlen = 0;
291 offset = totlen;
292 } while (len > 0);
294 (*s)[totlen] = '\0';
295 return NULL;
298 const struct got_error *
299 got_tree_entry_get_symlink_target(char **link_target, struct got_tree_entry *te,
300 struct got_repository *repo)
302 const struct got_error *err = NULL;
303 struct got_blob_object *blob = NULL;
304 int fd = -1;
306 *link_target = NULL;
308 if (!got_object_tree_entry_is_symlink(te))
309 return got_error(GOT_ERR_TREE_ENTRY_TYPE);
311 fd = got_opentempfd();
312 if (fd == -1) {
313 err = got_error_from_errno("got_opentempfd");
314 goto done;
317 err = got_object_open_as_blob(&blob, repo,
318 got_tree_entry_get_id(te), PATH_MAX, fd);
319 if (err)
320 goto done;
322 err = got_object_blob_read_to_str(link_target, blob);
323 done:
324 if (fd != -1 && close(fd) == -1 && err == NULL)
325 err = got_error_from_errno("close");
326 if (blob)
327 got_object_blob_close(blob);
328 if (err) {
329 free(*link_target);
330 *link_target = NULL;
332 return err;
335 int
336 got_tree_entry_get_index(struct got_tree_entry *te)
338 return te->idx;
341 struct got_tree_entry *
342 got_tree_entry_get_next(struct got_tree_object *tree,
343 struct got_tree_entry *te)
345 return got_object_tree_get_entry(tree, te->idx + 1);
348 struct got_tree_entry *
349 got_tree_entry_get_prev(struct got_tree_object *tree,
350 struct got_tree_entry *te)
352 return got_object_tree_get_entry(tree, te->idx - 1);
355 const struct got_error *
356 got_object_blob_close(struct got_blob_object *blob)
358 const struct got_error *err = NULL;
359 free(blob->read_buf);
360 if (blob->f && fclose(blob->f) == EOF)
361 err = got_error_from_errno("fclose");
362 free(blob->data);
363 free(blob);
364 return err;
367 void
368 got_object_blob_rewind(struct got_blob_object *blob)
370 if (blob->f)
371 rewind(blob->f);
374 char *
375 got_object_blob_id_str(struct got_blob_object *blob, char *buf, size_t size)
377 return got_sha1_digest_to_str(blob->id.sha1, buf, size);
380 size_t
381 got_object_blob_get_hdrlen(struct got_blob_object *blob)
383 return blob->hdrlen;
386 const uint8_t *
387 got_object_blob_get_read_buf(struct got_blob_object *blob)
389 return blob->read_buf;
392 const struct got_error *
393 got_object_blob_read_block(size_t *outlenp, struct got_blob_object *blob)
395 size_t n;
397 n = fread(blob->read_buf, 1, blob->blocksize, blob->f);
398 if (n == 0 && ferror(blob->f))
399 return got_ferror(blob->f, GOT_ERR_IO);
400 *outlenp = n;
401 return NULL;
404 const struct got_error *
405 got_object_blob_dump_to_file(off_t *filesize, int *nlines,
406 off_t **line_offsets, FILE *outfile, struct got_blob_object *blob)
408 const struct got_error *err = NULL;
409 size_t n, len, hdrlen;
410 const uint8_t *buf;
411 int i;
412 const int alloc_chunksz = 512;
413 size_t nalloc = 0;
414 off_t off = 0, total_len = 0;
416 if (line_offsets)
417 *line_offsets = NULL;
418 if (filesize)
419 *filesize = 0;
420 if (nlines)
421 *nlines = 0;
423 hdrlen = got_object_blob_get_hdrlen(blob);
424 do {
425 err = got_object_blob_read_block(&len, blob);
426 if (err)
427 return err;
428 if (len == 0)
429 break;
430 buf = got_object_blob_get_read_buf(blob);
431 i = hdrlen;
432 if (nlines) {
433 if (line_offsets && *line_offsets == NULL) {
434 /* Have some data but perhaps no '\n'. */
435 *nlines = 1;
436 nalloc = alloc_chunksz;
437 *line_offsets = calloc(nalloc,
438 sizeof(**line_offsets));
439 if (*line_offsets == NULL)
440 return got_error_from_errno("calloc");
442 /* Skip forward over end of first line. */
443 while (i < len) {
444 if (buf[i] == '\n')
445 break;
446 i++;
449 /* Scan '\n' offsets in remaining chunk of data. */
450 while (i < len) {
451 if (buf[i] != '\n') {
452 i++;
453 continue;
455 (*nlines)++;
456 if (line_offsets && nalloc < *nlines) {
457 size_t n = *nlines + alloc_chunksz;
458 off_t *o = recallocarray(*line_offsets,
459 nalloc, n, sizeof(**line_offsets));
460 if (o == NULL) {
461 free(*line_offsets);
462 *line_offsets = NULL;
463 return got_error_from_errno(
464 "recallocarray");
466 *line_offsets = o;
467 nalloc = n;
469 if (line_offsets) {
470 off = total_len + i - hdrlen + 1;
471 (*line_offsets)[*nlines - 1] = off;
473 i++;
476 /* Skip blob object header first time around. */
477 n = fwrite(buf + hdrlen, 1, len - hdrlen, outfile);
478 if (n != len - hdrlen)
479 return got_ferror(outfile, GOT_ERR_IO);
480 total_len += len - hdrlen;
481 hdrlen = 0;
482 } while (len != 0);
484 if (fflush(outfile) != 0)
485 return got_error_from_errno("fflush");
486 rewind(outfile);
488 if (filesize)
489 *filesize = total_len;
491 return NULL;
494 const char *
495 got_object_tag_get_name(struct got_tag_object *tag)
497 return tag->tag;
500 int
501 got_object_tag_get_object_type(struct got_tag_object *tag)
503 return tag->obj_type;
506 struct got_object_id *
507 got_object_tag_get_object_id(struct got_tag_object *tag)
509 return &tag->id;
512 time_t
513 got_object_tag_get_tagger_time(struct got_tag_object *tag)
515 return tag->tagger_time;
518 time_t
519 got_object_tag_get_tagger_gmtoff(struct got_tag_object *tag)
521 return tag->tagger_gmtoff;
524 const char *
525 got_object_tag_get_tagger(struct got_tag_object *tag)
527 return tag->tagger;
530 const char *
531 got_object_tag_get_message(struct got_tag_object *tag)
533 return tag->tagmsg;
536 static struct got_tree_entry *
537 find_entry_by_name(struct got_tree_object *tree, const char *name, size_t len)
539 int i;
541 /* Note that tree entries are sorted in strncmp() order. */
542 for (i = 0; i < tree->nentries; i++) {
543 struct got_tree_entry *te = &tree->entries[i];
544 int cmp = strncmp(te->name, name, len);
545 if (cmp < 0)
546 continue;
547 if (cmp > 0)
548 break;
549 if (te->name[len] == '\0')
550 return te;
552 return NULL;
555 struct got_tree_entry *
556 got_object_tree_find_entry(struct got_tree_object *tree, const char *name)
558 return find_entry_by_name(tree, name, strlen(name));
561 const struct got_error *
562 got_object_tree_find_path(struct got_object_id **id, mode_t *mode,
563 struct got_repository *repo, struct got_tree_object *tree,
564 const char *path)
566 const struct got_error *err = NULL;
567 struct got_tree_object *subtree = NULL;
568 struct got_tree_entry *te = NULL;
569 const char *seg, *s;
570 size_t seglen;
572 *id = NULL;
574 s = path;
575 while (s[0] == '/')
576 s++;
577 seg = s;
578 seglen = 0;
579 subtree = tree;
580 while (*s) {
581 struct got_tree_object *next_tree;
583 if (*s != '/') {
584 s++;
585 seglen++;
586 if (*s)
587 continue;
590 te = find_entry_by_name(subtree, seg, seglen);
591 if (te == NULL) {
592 err = got_error_path(path, GOT_ERR_NO_TREE_ENTRY);
593 goto done;
596 if (*s == '\0')
597 break;
599 seg = s + 1;
600 seglen = 0;
601 s++;
602 if (*s) {
603 err = got_object_open_as_tree(&next_tree, repo,
604 &te->id);
605 te = NULL;
606 if (err)
607 goto done;
608 if (subtree != tree)
609 got_object_tree_close(subtree);
610 subtree = next_tree;
614 if (te) {
615 *id = got_object_id_dup(&te->id);
616 if (*id == NULL)
617 return got_error_from_errno("got_object_id_dup");
618 if (mode)
619 *mode = te->mode;
620 } else
621 err = got_error_path(path, GOT_ERR_NO_TREE_ENTRY);
622 done:
623 if (subtree && subtree != tree)
624 got_object_tree_close(subtree);
625 return err;
628 const struct got_error *
629 got_object_id_by_path(struct got_object_id **id, struct got_repository *repo,
630 struct got_commit_object *commit, const char *path)
632 const struct got_error *err = NULL;
633 struct got_tree_object *tree = NULL;
635 *id = NULL;
637 /* Handle opening of root of commit's tree. */
638 if (got_path_is_root_dir(path)) {
639 *id = got_object_id_dup(commit->tree_id);
640 if (*id == NULL)
641 err = got_error_from_errno("got_object_id_dup");
642 } else {
643 err = got_object_open_as_tree(&tree, repo, commit->tree_id);
644 if (err)
645 goto done;
646 err = got_object_tree_find_path(id, NULL, repo, tree, path);
648 done:
649 if (tree)
650 got_object_tree_close(tree);
651 return err;
654 /*
655 * Normalize file mode bits to avoid false positive tree entry differences
656 * in case tree entries have unexpected mode bits set.
657 */
658 static mode_t
659 normalize_mode_for_comparison(mode_t mode)
661 /*
662 * For directories, the only relevant bit is the IFDIR bit.
663 * This allows us to detect paths changing from a directory
664 * to a file and vice versa.
665 */
666 if (S_ISDIR(mode))
667 return mode & S_IFDIR;
669 /*
670 * For symlinks, the only relevant bit is the IFLNK bit.
671 * This allows us to detect paths changing from a symlinks
672 * to a file or directory and vice versa.
673 */
674 if (S_ISLNK(mode))
675 return mode & S_IFLNK;
677 /* For files, the only change we care about is the executable bit. */
678 return mode & S_IXUSR;
681 const struct got_error *
682 got_object_tree_path_changed(int *changed,
683 struct got_tree_object *tree01, struct got_tree_object *tree02,
684 const char *path, struct got_repository *repo)
686 const struct got_error *err = NULL;
687 struct got_tree_object *tree1 = NULL, *tree2 = NULL;
688 struct got_tree_entry *te1 = NULL, *te2 = NULL;
689 const char *seg, *s;
690 size_t seglen;
692 *changed = 0;
694 /* We not do support comparing the root path. */
695 if (got_path_is_root_dir(path))
696 return got_error_path(path, GOT_ERR_BAD_PATH);
698 tree1 = tree01;
699 tree2 = tree02;
700 s = path;
701 while (*s == '/')
702 s++;
703 seg = s;
704 seglen = 0;
705 while (*s) {
706 struct got_tree_object *next_tree1, *next_tree2;
707 mode_t mode1, mode2;
709 if (*s != '/') {
710 s++;
711 seglen++;
712 if (*s)
713 continue;
716 te1 = find_entry_by_name(tree1, seg, seglen);
717 if (te1 == NULL) {
718 err = got_error(GOT_ERR_NO_OBJ);
719 goto done;
722 if (tree2)
723 te2 = find_entry_by_name(tree2, seg, seglen);
725 if (te2) {
726 mode1 = normalize_mode_for_comparison(te1->mode);
727 mode2 = normalize_mode_for_comparison(te2->mode);
728 if (mode1 != mode2) {
729 *changed = 1;
730 goto done;
733 if (got_object_id_cmp(&te1->id, &te2->id) == 0) {
734 *changed = 0;
735 goto done;
739 if (*s == '\0') { /* final path element */
740 *changed = 1;
741 goto done;
744 seg = s + 1;
745 s++;
746 seglen = 0;
747 if (*s) {
748 err = got_object_open_as_tree(&next_tree1, repo,
749 &te1->id);
750 te1 = NULL;
751 if (err)
752 goto done;
753 if (tree1 != tree01)
754 got_object_tree_close(tree1);
755 tree1 = next_tree1;
757 if (te2) {
758 err = got_object_open_as_tree(&next_tree2, repo,
759 &te2->id);
760 te2 = NULL;
761 if (err)
762 goto done;
763 if (tree2 != tree02)
764 got_object_tree_close(tree2);
765 tree2 = next_tree2;
766 } else if (tree2) {
767 if (tree2 != tree02)
768 got_object_tree_close(tree2);
769 tree2 = NULL;
773 done:
774 if (tree1 && tree1 != tree01)
775 got_object_tree_close(tree1);
776 if (tree2 && tree2 != tree02)
777 got_object_tree_close(tree2);
778 return err;
781 const struct got_error *
782 got_object_tree_entry_dup(struct got_tree_entry **new_te,
783 struct got_tree_entry *te)
785 const struct got_error *err = NULL;
787 *new_te = calloc(1, sizeof(**new_te));
788 if (*new_te == NULL)
789 return got_error_from_errno("calloc");
791 (*new_te)->mode = te->mode;
792 memcpy((*new_te)->name, te->name, sizeof((*new_te)->name));
793 memcpy(&(*new_te)->id, &te->id, sizeof((*new_te)->id));
794 return err;
797 int
798 got_object_tree_entry_is_submodule(struct got_tree_entry *te)
800 return (te->mode & S_IFMT) == (S_IFDIR | S_IFLNK);
803 int
804 got_object_tree_entry_is_symlink(struct got_tree_entry *te)
806 /* S_IFDIR check avoids confusing symlinks with submodules. */
807 return ((te->mode & (S_IFDIR | S_IFLNK)) == S_IFLNK);
810 static const struct got_error *
811 resolve_symlink(char **link_target, const char *path,
812 struct got_commit_object *commit, struct got_repository *repo)
814 const struct got_error *err = NULL;
815 char buf[PATH_MAX];
816 char *name, *parent_path = NULL;
817 struct got_object_id *tree_obj_id = NULL;
818 struct got_tree_object *tree = NULL;
819 struct got_tree_entry *te = NULL;
821 *link_target = NULL;
823 if (strlcpy(buf, path, sizeof(buf)) >= sizeof(buf))
824 return got_error(GOT_ERR_NO_SPACE);
826 name = basename(buf);
827 if (name == NULL)
828 return got_error_from_errno2("basename", path);
830 err = got_path_dirname(&parent_path, path);
831 if (err)
832 return err;
834 err = got_object_id_by_path(&tree_obj_id, repo, commit,
835 parent_path);
836 if (err) {
837 if (err->code == GOT_ERR_NO_TREE_ENTRY) {
838 /* Display the complete path in error message. */
839 err = got_error_path(path, err->code);
841 goto done;
844 err = got_object_open_as_tree(&tree, repo, tree_obj_id);
845 if (err)
846 goto done;
848 te = got_object_tree_find_entry(tree, name);
849 if (te == NULL) {
850 err = got_error_path(path, GOT_ERR_NO_TREE_ENTRY);
851 goto done;
854 if (got_object_tree_entry_is_symlink(te)) {
855 err = got_tree_entry_get_symlink_target(link_target, te, repo);
856 if (err)
857 goto done;
858 if (!got_path_is_absolute(*link_target)) {
859 char *abspath;
860 if (asprintf(&abspath, "%s/%s", parent_path,
861 *link_target) == -1) {
862 err = got_error_from_errno("asprintf");
863 goto done;
865 free(*link_target);
866 *link_target = malloc(PATH_MAX);
867 if (*link_target == NULL) {
868 err = got_error_from_errno("malloc");
869 goto done;
871 err = got_canonpath(abspath, *link_target, PATH_MAX);
872 free(abspath);
873 if (err)
874 goto done;
877 done:
878 free(parent_path);
879 free(tree_obj_id);
880 if (tree)
881 got_object_tree_close(tree);
882 if (err) {
883 free(*link_target);
884 *link_target = NULL;
886 return err;
889 const struct got_error *
890 got_object_resolve_symlinks(char **link_target, const char *path,
891 struct got_commit_object *commit, struct got_repository *repo)
893 const struct got_error *err = NULL;
894 char *next_target = NULL;
895 int max_recursion = 40; /* matches Git */
897 *link_target = NULL;
899 do {
900 err = resolve_symlink(&next_target,
901 *link_target ? *link_target : path, commit, repo);
902 if (err)
903 break;
904 if (next_target) {
905 free(*link_target);
906 if (--max_recursion == 0) {
907 err = got_error_path(path, GOT_ERR_RECURSION);
908 *link_target = NULL;
909 break;
911 *link_target = next_target;
913 } while (next_target);
915 return err;
918 void
919 got_object_commit_retain(struct got_commit_object *commit)
921 commit->refcnt++;
924 const struct got_error *
925 got_object_raw_alloc(struct got_raw_object **obj, uint8_t *outbuf, int *outfd,
926 size_t hdrlen, off_t size)
928 const struct got_error *err = NULL;
929 off_t tot;
931 tot = hdrlen + size;
933 *obj = calloc(1, sizeof(**obj));
934 if (*obj == NULL) {
935 err = got_error_from_errno("calloc");
936 goto done;
938 (*obj)->fd = -1;
939 (*obj)->tempfile_idx = -1;
941 if (outbuf) {
942 (*obj)->data = outbuf;
943 } else {
944 struct stat sb;
945 if (fstat(*outfd, &sb) == -1) {
946 err = got_error_from_errno("fstat");
947 goto done;
950 if (sb.st_size != tot) {
951 err = got_error(GOT_ERR_PRIVSEP_LEN);
952 goto done;
954 #ifndef GOT_PACK_NO_MMAP
955 if (tot > 0 && tot <= SIZE_MAX) {
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;