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_is_binary(int *binary, struct got_blob_object *blob)
407 const struct got_error *err;
408 size_t hdrlen, len;
410 *binary = 0;
411 hdrlen = got_object_blob_get_hdrlen(blob);
413 if (fseeko(blob->f, hdrlen, SEEK_SET) == -1)
414 return got_error_from_errno("fseeko");
416 err = got_object_blob_read_block(&len, blob);
417 if (err)
418 return err;
420 *binary = memchr(blob->read_buf, '\0', len) != NULL;
422 if (fseeko(blob->f, hdrlen, SEEK_SET) == -1)
423 return got_error_from_errno("fseeko");
424 return NULL;
427 const struct got_error *
428 got_object_blob_dump_to_file(off_t *filesize, int *nlines,
429 off_t **line_offsets, FILE *outfile, struct got_blob_object *blob)
431 const struct got_error *err = NULL;
432 size_t n, len, hdrlen;
433 const uint8_t *buf;
434 int i;
435 const int alloc_chunksz = 512;
436 size_t nalloc = 0;
437 off_t off = 0, total_len = 0;
439 if (line_offsets)
440 *line_offsets = NULL;
441 if (filesize)
442 *filesize = 0;
443 if (nlines)
444 *nlines = 0;
446 hdrlen = got_object_blob_get_hdrlen(blob);
447 do {
448 err = got_object_blob_read_block(&len, blob);
449 if (err)
450 return err;
451 if (len == 0)
452 break;
453 buf = got_object_blob_get_read_buf(blob);
454 i = hdrlen;
455 if (nlines) {
456 if (line_offsets && *line_offsets == NULL) {
457 /* Have some data but perhaps no '\n'. */
458 *nlines = 1;
459 nalloc = alloc_chunksz;
460 *line_offsets = calloc(nalloc,
461 sizeof(**line_offsets));
462 if (*line_offsets == NULL)
463 return got_error_from_errno("calloc");
465 /* Skip forward over end of first line. */
466 while (i < len) {
467 if (buf[i] == '\n')
468 break;
469 i++;
472 /* Scan '\n' offsets in remaining chunk of data. */
473 while (i < len) {
474 if (buf[i] != '\n') {
475 i++;
476 continue;
478 (*nlines)++;
479 if (line_offsets && nalloc < *nlines) {
480 size_t n = *nlines + alloc_chunksz;
481 off_t *o = recallocarray(*line_offsets,
482 nalloc, n, sizeof(**line_offsets));
483 if (o == NULL) {
484 free(*line_offsets);
485 *line_offsets = NULL;
486 return got_error_from_errno(
487 "recallocarray");
489 *line_offsets = o;
490 nalloc = n;
492 if (line_offsets) {
493 off = total_len + i - hdrlen + 1;
494 (*line_offsets)[*nlines - 1] = off;
496 i++;
499 /* Skip blob object header first time around. */
500 n = fwrite(buf + hdrlen, 1, len - hdrlen, outfile);
501 if (n != len - hdrlen)
502 return got_ferror(outfile, GOT_ERR_IO);
503 total_len += len - hdrlen;
504 hdrlen = 0;
505 } while (len != 0);
507 if (fflush(outfile) != 0)
508 return got_error_from_errno("fflush");
509 rewind(outfile);
511 if (filesize)
512 *filesize = total_len;
514 return NULL;
517 const char *
518 got_object_tag_get_name(struct got_tag_object *tag)
520 return tag->tag;
523 int
524 got_object_tag_get_object_type(struct got_tag_object *tag)
526 return tag->obj_type;
529 struct got_object_id *
530 got_object_tag_get_object_id(struct got_tag_object *tag)
532 return &tag->id;
535 time_t
536 got_object_tag_get_tagger_time(struct got_tag_object *tag)
538 return tag->tagger_time;
541 time_t
542 got_object_tag_get_tagger_gmtoff(struct got_tag_object *tag)
544 return tag->tagger_gmtoff;
547 const char *
548 got_object_tag_get_tagger(struct got_tag_object *tag)
550 return tag->tagger;
553 const char *
554 got_object_tag_get_message(struct got_tag_object *tag)
556 return tag->tagmsg;
559 static struct got_tree_entry *
560 find_entry_by_name(struct got_tree_object *tree, const char *name, size_t len)
562 int i;
564 /* Note that tree entries are sorted in strncmp() order. */
565 for (i = 0; i < tree->nentries; i++) {
566 struct got_tree_entry *te = &tree->entries[i];
567 int cmp = strncmp(te->name, name, len);
568 if (cmp < 0)
569 continue;
570 if (cmp > 0)
571 break;
572 if (te->name[len] == '\0')
573 return te;
575 return NULL;
578 struct got_tree_entry *
579 got_object_tree_find_entry(struct got_tree_object *tree, const char *name)
581 return find_entry_by_name(tree, name, strlen(name));
584 const struct got_error *
585 got_object_tree_find_path(struct got_object_id **id, mode_t *mode,
586 struct got_repository *repo, struct got_tree_object *tree,
587 const char *path)
589 const struct got_error *err = NULL;
590 struct got_tree_object *subtree = NULL;
591 struct got_tree_entry *te = NULL;
592 const char *seg, *s;
593 size_t seglen;
595 *id = NULL;
597 s = path;
598 while (s[0] == '/')
599 s++;
600 seg = s;
601 seglen = 0;
602 subtree = tree;
603 while (*s) {
604 struct got_tree_object *next_tree;
606 if (*s != '/') {
607 s++;
608 seglen++;
609 if (*s)
610 continue;
613 te = find_entry_by_name(subtree, seg, seglen);
614 if (te == NULL) {
615 err = got_error_path(path, GOT_ERR_NO_TREE_ENTRY);
616 goto done;
619 if (*s == '\0')
620 break;
622 seg = s + 1;
623 seglen = 0;
624 s++;
625 if (*s) {
626 err = got_object_open_as_tree(&next_tree, repo,
627 &te->id);
628 te = NULL;
629 if (err)
630 goto done;
631 if (subtree != tree)
632 got_object_tree_close(subtree);
633 subtree = next_tree;
637 if (te) {
638 *id = got_object_id_dup(&te->id);
639 if (*id == NULL)
640 return got_error_from_errno("got_object_id_dup");
641 if (mode)
642 *mode = te->mode;
643 } else
644 err = got_error_path(path, GOT_ERR_NO_TREE_ENTRY);
645 done:
646 if (subtree && subtree != tree)
647 got_object_tree_close(subtree);
648 return err;
651 const struct got_error *
652 got_object_id_by_path(struct got_object_id **id, struct got_repository *repo,
653 struct got_commit_object *commit, const char *path)
655 const struct got_error *err = NULL;
656 struct got_tree_object *tree = NULL;
658 *id = NULL;
660 /* Handle opening of root of commit's tree. */
661 if (got_path_is_root_dir(path)) {
662 *id = got_object_id_dup(commit->tree_id);
663 if (*id == NULL)
664 err = got_error_from_errno("got_object_id_dup");
665 } else {
666 err = got_object_open_as_tree(&tree, repo, commit->tree_id);
667 if (err)
668 goto done;
669 err = got_object_tree_find_path(id, NULL, repo, tree, path);
671 done:
672 if (tree)
673 got_object_tree_close(tree);
674 return err;
677 /*
678 * Normalize file mode bits to avoid false positive tree entry differences
679 * in case tree entries have unexpected mode bits set.
680 */
681 static mode_t
682 normalize_mode_for_comparison(mode_t mode)
684 /*
685 * For directories, the only relevant bit is the IFDIR bit.
686 * This allows us to detect paths changing from a directory
687 * to a file and vice versa.
688 */
689 if (S_ISDIR(mode))
690 return mode & S_IFDIR;
692 /*
693 * For symlinks, the only relevant bit is the IFLNK bit.
694 * This allows us to detect paths changing from a symlinks
695 * to a file or directory and vice versa.
696 */
697 if (S_ISLNK(mode))
698 return mode & S_IFLNK;
700 /* For files, the only change we care about is the executable bit. */
701 return mode & S_IXUSR;
704 const struct got_error *
705 got_object_tree_path_changed(int *changed,
706 struct got_tree_object *tree01, struct got_tree_object *tree02,
707 const char *path, struct got_repository *repo)
709 const struct got_error *err = NULL;
710 struct got_tree_object *tree1 = NULL, *tree2 = NULL;
711 struct got_tree_entry *te1 = NULL, *te2 = NULL;
712 const char *seg, *s;
713 size_t seglen;
715 *changed = 0;
717 /* We not do support comparing the root path. */
718 if (got_path_is_root_dir(path))
719 return got_error_path(path, GOT_ERR_BAD_PATH);
721 tree1 = tree01;
722 tree2 = tree02;
723 s = path;
724 while (*s == '/')
725 s++;
726 seg = s;
727 seglen = 0;
728 while (*s) {
729 struct got_tree_object *next_tree1, *next_tree2;
730 mode_t mode1, mode2;
732 if (*s != '/') {
733 s++;
734 seglen++;
735 if (*s)
736 continue;
739 te1 = find_entry_by_name(tree1, seg, seglen);
740 if (te1 == NULL) {
741 err = got_error(GOT_ERR_NO_OBJ);
742 goto done;
745 if (tree2)
746 te2 = find_entry_by_name(tree2, seg, seglen);
748 if (te2) {
749 mode1 = normalize_mode_for_comparison(te1->mode);
750 mode2 = normalize_mode_for_comparison(te2->mode);
751 if (mode1 != mode2) {
752 *changed = 1;
753 goto done;
756 if (got_object_id_cmp(&te1->id, &te2->id) == 0) {
757 *changed = 0;
758 goto done;
762 if (*s == '\0') { /* final path element */
763 *changed = 1;
764 goto done;
767 seg = s + 1;
768 s++;
769 seglen = 0;
770 if (*s) {
771 err = got_object_open_as_tree(&next_tree1, repo,
772 &te1->id);
773 te1 = NULL;
774 if (err)
775 goto done;
776 if (tree1 != tree01)
777 got_object_tree_close(tree1);
778 tree1 = next_tree1;
780 if (te2) {
781 err = got_object_open_as_tree(&next_tree2, repo,
782 &te2->id);
783 te2 = NULL;
784 if (err)
785 goto done;
786 if (tree2 != tree02)
787 got_object_tree_close(tree2);
788 tree2 = next_tree2;
789 } else if (tree2) {
790 if (tree2 != tree02)
791 got_object_tree_close(tree2);
792 tree2 = NULL;
796 done:
797 if (tree1 && tree1 != tree01)
798 got_object_tree_close(tree1);
799 if (tree2 && tree2 != tree02)
800 got_object_tree_close(tree2);
801 return err;
804 const struct got_error *
805 got_object_tree_entry_dup(struct got_tree_entry **new_te,
806 struct got_tree_entry *te)
808 const struct got_error *err = NULL;
810 *new_te = calloc(1, sizeof(**new_te));
811 if (*new_te == NULL)
812 return got_error_from_errno("calloc");
814 (*new_te)->mode = te->mode;
815 memcpy((*new_te)->name, te->name, sizeof((*new_te)->name));
816 memcpy(&(*new_te)->id, &te->id, sizeof((*new_te)->id));
817 return err;
820 int
821 got_object_tree_entry_is_submodule(struct got_tree_entry *te)
823 return (te->mode & S_IFMT) == (S_IFDIR | S_IFLNK);
826 int
827 got_object_tree_entry_is_symlink(struct got_tree_entry *te)
829 /* S_IFDIR check avoids confusing symlinks with submodules. */
830 return ((te->mode & (S_IFDIR | S_IFLNK)) == S_IFLNK);
833 static const struct got_error *
834 resolve_symlink(char **link_target, const char *path,
835 struct got_commit_object *commit, struct got_repository *repo)
837 const struct got_error *err = NULL;
838 char buf[PATH_MAX];
839 char *name, *parent_path = NULL;
840 struct got_object_id *tree_obj_id = NULL;
841 struct got_tree_object *tree = NULL;
842 struct got_tree_entry *te = NULL;
844 *link_target = NULL;
846 if (strlcpy(buf, path, sizeof(buf)) >= sizeof(buf))
847 return got_error(GOT_ERR_NO_SPACE);
849 name = basename(buf);
850 if (name == NULL)
851 return got_error_from_errno2("basename", path);
853 err = got_path_dirname(&parent_path, path);
854 if (err)
855 return err;
857 err = got_object_id_by_path(&tree_obj_id, repo, commit,
858 parent_path);
859 if (err) {
860 if (err->code == GOT_ERR_NO_TREE_ENTRY) {
861 /* Display the complete path in error message. */
862 err = got_error_path(path, err->code);
864 goto done;
867 err = got_object_open_as_tree(&tree, repo, tree_obj_id);
868 if (err)
869 goto done;
871 te = got_object_tree_find_entry(tree, name);
872 if (te == NULL) {
873 err = got_error_path(path, GOT_ERR_NO_TREE_ENTRY);
874 goto done;
877 if (got_object_tree_entry_is_symlink(te)) {
878 err = got_tree_entry_get_symlink_target(link_target, te, repo);
879 if (err)
880 goto done;
881 if (!got_path_is_absolute(*link_target)) {
882 char *abspath;
883 if (asprintf(&abspath, "%s/%s", parent_path,
884 *link_target) == -1) {
885 err = got_error_from_errno("asprintf");
886 goto done;
888 free(*link_target);
889 *link_target = malloc(PATH_MAX);
890 if (*link_target == NULL) {
891 err = got_error_from_errno("malloc");
892 goto done;
894 err = got_canonpath(abspath, *link_target, PATH_MAX);
895 free(abspath);
896 if (err)
897 goto done;
900 done:
901 free(parent_path);
902 free(tree_obj_id);
903 if (tree)
904 got_object_tree_close(tree);
905 if (err) {
906 free(*link_target);
907 *link_target = NULL;
909 return err;
912 const struct got_error *
913 got_object_resolve_symlinks(char **link_target, const char *path,
914 struct got_commit_object *commit, struct got_repository *repo)
916 const struct got_error *err = NULL;
917 char *next_target = NULL;
918 int max_recursion = 40; /* matches Git */
920 *link_target = NULL;
922 do {
923 err = resolve_symlink(&next_target,
924 *link_target ? *link_target : path, commit, repo);
925 if (err)
926 break;
927 if (next_target) {
928 free(*link_target);
929 if (--max_recursion == 0) {
930 err = got_error_path(path, GOT_ERR_RECURSION);
931 *link_target = NULL;
932 break;
934 *link_target = next_target;
936 } while (next_target);
938 return err;
941 void
942 got_object_commit_retain(struct got_commit_object *commit)
944 commit->refcnt++;
947 const struct got_error *
948 got_object_raw_alloc(struct got_raw_object **obj, uint8_t *outbuf, int *outfd,
949 size_t hdrlen, off_t size)
951 const struct got_error *err = NULL;
952 off_t tot;
954 tot = hdrlen + size;
956 *obj = calloc(1, sizeof(**obj));
957 if (*obj == NULL) {
958 err = got_error_from_errno("calloc");
959 goto done;
961 (*obj)->fd = -1;
962 (*obj)->tempfile_idx = -1;
964 if (outbuf) {
965 (*obj)->data = outbuf;
966 } else {
967 struct stat sb;
968 if (fstat(*outfd, &sb) == -1) {
969 err = got_error_from_errno("fstat");
970 goto done;
973 if (sb.st_size != tot) {
974 err = got_error(GOT_ERR_PRIVSEP_LEN);
975 goto done;
977 #ifndef GOT_PACK_NO_MMAP
978 if (tot > 0 && tot <= SIZE_MAX) {
979 (*obj)->data = mmap(NULL, tot, PROT_READ,
980 MAP_PRIVATE, *outfd, 0);
981 if ((*obj)->data == MAP_FAILED) {
982 if (errno != ENOMEM) {
983 err = got_error_from_errno("mmap");
984 goto done;
986 (*obj)->data = NULL;
987 } else {
988 (*obj)->fd = *outfd;
989 *outfd = -1;
992 #endif
993 if (*outfd != -1) {
994 (*obj)->f = fdopen(*outfd, "r");
995 if ((*obj)->f == NULL) {
996 err = got_error_from_errno("fdopen");
997 goto done;
999 *outfd = -1;
1002 (*obj)->hdrlen = hdrlen;
1003 (*obj)->size = size;
1004 done:
1005 if (err) {
1006 if (*obj) {
1007 got_object_raw_close(*obj);
1008 *obj = NULL;
1010 } else
1011 (*obj)->refcnt++;
1012 return err;