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->hash[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 = { .algo = repo->algo };
154 if (repo->algo == GOT_HASH_SHA256) {
155 if (!got_parse_sha256_digest(id.hash, id_str))
156 return got_error_path(id_str, GOT_ERR_BAD_OBJ_ID_STR);
157 } else if (repo->algo == GOT_HASH_SHA1) {
158 if (!got_parse_sha1_digest(id.hash, id_str))
159 return got_error_path(id_str, GOT_ERR_BAD_OBJ_ID_STR);
160 } else
161 return got_error_path(id_str, GOT_ERR_BAD_OBJ_ID_STR);
163 return got_object_open(obj, repo, &id);
166 const struct got_error *
167 got_object_resolve_id_str(struct got_object_id **id,
168 struct got_repository *repo, const char *id_str)
170 const struct got_error *err = NULL;
171 struct got_object *obj;
173 err = got_object_open_by_id_str(&obj, repo, id_str);
174 if (err)
175 return err;
177 *id = got_object_id_dup(got_object_get_id(obj));
178 got_object_close(obj);
179 if (*id == NULL)
180 return got_error_from_errno("got_object_id_dup");
182 return NULL;
185 const struct got_error *
186 got_object_qid_alloc(struct got_object_qid **qid, struct got_object_id *id)
188 *qid = calloc(1, sizeof(**qid));
189 if (*qid == NULL)
190 return got_error_from_errno("calloc");
192 memcpy(&(*qid)->id, id, sizeof((*qid)->id));
193 return NULL;
196 const struct got_error *
197 got_object_id_queue_copy(const struct got_object_id_queue *src,
198 struct got_object_id_queue *dest)
200 const struct got_error *err;
201 struct got_object_qid *qid;
203 STAILQ_FOREACH(qid, src, entry) {
204 struct got_object_qid *new;
205 /*
206 * Deep-copy the object ID only. Let the caller deal
207 * with setting up the new->data pointer if needed.
208 */
209 err = got_object_qid_alloc(&new, &qid->id);
210 if (err) {
211 got_object_id_queue_free(dest);
212 return err;
214 STAILQ_INSERT_TAIL(dest, new, entry);
217 return NULL;
220 int
221 got_object_tree_get_nentries(struct got_tree_object *tree)
223 return tree->nentries;
226 struct got_tree_entry *
227 got_object_tree_get_first_entry(struct got_tree_object *tree)
229 return got_object_tree_get_entry(tree, 0);
232 struct got_tree_entry *
233 got_object_tree_get_last_entry(struct got_tree_object *tree)
235 return got_object_tree_get_entry(tree, tree->nentries - 1);
238 struct got_tree_entry *
239 got_object_tree_get_entry(struct got_tree_object *tree, int i)
241 if (i < 0 || i >= tree->nentries)
242 return NULL;
243 return &tree->entries[i];
246 mode_t
247 got_tree_entry_get_mode(struct got_tree_entry *te)
249 return te->mode;
252 const char *
253 got_tree_entry_get_name(struct got_tree_entry *te)
255 return &te->name[0];
258 struct got_object_id *
259 got_tree_entry_get_id(struct got_tree_entry *te)
261 return &te->id;
264 const struct got_error *
265 got_object_blob_read_to_str(char **s, struct got_blob_object *blob)
267 const struct got_error *err = NULL;
268 size_t len, totlen, hdrlen, offset;
270 *s = NULL;
272 hdrlen = got_object_blob_get_hdrlen(blob);
273 totlen = 0;
274 offset = 0;
275 do {
276 char *p;
278 err = got_object_blob_read_block(&len, blob);
279 if (err)
280 return err;
282 if (len == 0)
283 break;
285 totlen += len - hdrlen;
286 p = realloc(*s, totlen + 1);
287 if (p == NULL) {
288 err = got_error_from_errno("realloc");
289 free(*s);
290 *s = NULL;
291 return err;
293 *s = p;
294 /* Skip blob object header first time around. */
295 memcpy(*s + offset,
296 got_object_blob_get_read_buf(blob) + hdrlen, len - hdrlen);
297 hdrlen = 0;
298 offset = totlen;
299 } while (len > 0);
301 (*s)[totlen] = '\0';
302 return NULL;
305 const struct got_error *
306 got_tree_entry_get_symlink_target(char **link_target, struct got_tree_entry *te,
307 struct got_repository *repo)
309 const struct got_error *err = NULL;
310 struct got_blob_object *blob = NULL;
311 int fd = -1;
313 *link_target = NULL;
315 if (!got_object_tree_entry_is_symlink(te))
316 return got_error(GOT_ERR_TREE_ENTRY_TYPE);
318 fd = got_opentempfd();
319 if (fd == -1) {
320 err = got_error_from_errno("got_opentempfd");
321 goto done;
324 err = got_object_open_as_blob(&blob, repo,
325 got_tree_entry_get_id(te), PATH_MAX, fd);
326 if (err)
327 goto done;
329 err = got_object_blob_read_to_str(link_target, blob);
330 done:
331 if (fd != -1 && close(fd) == -1 && err == NULL)
332 err = got_error_from_errno("close");
333 if (blob)
334 got_object_blob_close(blob);
335 if (err) {
336 free(*link_target);
337 *link_target = NULL;
339 return err;
342 int
343 got_tree_entry_get_index(struct got_tree_entry *te)
345 return te->idx;
348 struct got_tree_entry *
349 got_tree_entry_get_next(struct got_tree_object *tree,
350 struct got_tree_entry *te)
352 return got_object_tree_get_entry(tree, te->idx + 1);
355 struct got_tree_entry *
356 got_tree_entry_get_prev(struct got_tree_object *tree,
357 struct got_tree_entry *te)
359 return got_object_tree_get_entry(tree, te->idx - 1);
362 const struct got_error *
363 got_object_blob_close(struct got_blob_object *blob)
365 const struct got_error *err = NULL;
366 free(blob->read_buf);
367 if (blob->f && fclose(blob->f) == EOF)
368 err = got_error_from_errno("fclose");
369 free(blob->data);
370 free(blob);
371 return err;
374 void
375 got_object_blob_rewind(struct got_blob_object *blob)
377 if (blob->f)
378 rewind(blob->f);
381 char *
382 got_object_blob_id_str(struct got_blob_object *blob, char *buf, size_t size)
384 return got_object_id_hex(&blob->id, buf, size);
387 size_t
388 got_object_blob_get_hdrlen(struct got_blob_object *blob)
390 return blob->hdrlen;
393 const uint8_t *
394 got_object_blob_get_read_buf(struct got_blob_object *blob)
396 return blob->read_buf;
399 const struct got_error *
400 got_object_blob_read_block(size_t *outlenp, struct got_blob_object *blob)
402 size_t n;
404 n = fread(blob->read_buf, 1, blob->blocksize, blob->f);
405 if (n == 0 && ferror(blob->f))
406 return got_ferror(blob->f, GOT_ERR_IO);
407 *outlenp = n;
408 return NULL;
411 const struct got_error *
412 got_object_blob_is_binary(int *binary, struct got_blob_object *blob)
414 const struct got_error *err;
415 size_t hdrlen, len;
417 *binary = 0;
418 hdrlen = got_object_blob_get_hdrlen(blob);
420 if (fseeko(blob->f, hdrlen, SEEK_SET) == -1)
421 return got_error_from_errno("fseeko");
423 err = got_object_blob_read_block(&len, blob);
424 if (err)
425 return err;
427 *binary = memchr(blob->read_buf, '\0', len) != NULL;
429 if (fseeko(blob->f, hdrlen, SEEK_SET) == -1)
430 return got_error_from_errno("fseeko");
431 return NULL;
434 const struct got_error *
435 got_object_blob_getline(char **line, ssize_t *linelen, size_t *linesize,
436 struct got_blob_object *blob)
438 *linelen = getline(line, linesize, blob->f);
439 if (*linelen == -1 && !feof(blob->f))
440 return got_error_from_errno("getline");
441 return NULL;
444 const struct got_error *
445 got_object_blob_dump_to_file(off_t *filesize, int *nlines,
446 off_t **line_offsets, FILE *outfile, struct got_blob_object *blob)
448 const struct got_error *err = NULL;
449 size_t n, len, hdrlen;
450 const uint8_t *buf;
451 int i;
452 const int alloc_chunksz = 512;
453 size_t nalloc = 0;
454 off_t off = 0, total_len = 0;
456 if (line_offsets)
457 *line_offsets = NULL;
458 if (filesize)
459 *filesize = 0;
460 if (nlines)
461 *nlines = 0;
463 hdrlen = got_object_blob_get_hdrlen(blob);
464 do {
465 err = got_object_blob_read_block(&len, blob);
466 if (err)
467 return err;
468 if (len == 0)
469 break;
470 buf = got_object_blob_get_read_buf(blob);
471 i = hdrlen;
472 if (nlines) {
473 if (line_offsets && *line_offsets == NULL) {
474 /* Have some data but perhaps no '\n'. */
475 *nlines = 1;
476 nalloc = alloc_chunksz;
477 *line_offsets = calloc(nalloc,
478 sizeof(**line_offsets));
479 if (*line_offsets == NULL)
480 return got_error_from_errno("calloc");
482 /* Skip forward over end of first line. */
483 while (i < len) {
484 if (buf[i] == '\n')
485 break;
486 i++;
489 /* Scan '\n' offsets in remaining chunk of data. */
490 while (i < len) {
491 if (buf[i] != '\n') {
492 i++;
493 continue;
495 (*nlines)++;
496 if (line_offsets && nalloc < *nlines) {
497 size_t n = *nlines + alloc_chunksz;
498 off_t *o = recallocarray(*line_offsets,
499 nalloc, n, sizeof(**line_offsets));
500 if (o == NULL) {
501 free(*line_offsets);
502 *line_offsets = NULL;
503 return got_error_from_errno(
504 "recallocarray");
506 *line_offsets = o;
507 nalloc = n;
509 if (line_offsets) {
510 off = total_len + i - hdrlen + 1;
511 (*line_offsets)[*nlines - 1] = off;
513 i++;
516 /* Skip blob object header first time around. */
517 n = fwrite(buf + hdrlen, 1, len - hdrlen, outfile);
518 if (n != len - hdrlen)
519 return got_ferror(outfile, GOT_ERR_IO);
520 total_len += len - hdrlen;
521 hdrlen = 0;
522 } while (len != 0);
524 if (fflush(outfile) != 0)
525 return got_error_from_errno("fflush");
526 rewind(outfile);
528 if (filesize)
529 *filesize = total_len;
531 return NULL;
534 const char *
535 got_object_tag_get_name(struct got_tag_object *tag)
537 return tag->tag;
540 int
541 got_object_tag_get_object_type(struct got_tag_object *tag)
543 return tag->obj_type;
546 struct got_object_id *
547 got_object_tag_get_object_id(struct got_tag_object *tag)
549 return &tag->id;
552 time_t
553 got_object_tag_get_tagger_time(struct got_tag_object *tag)
555 return tag->tagger_time;
558 time_t
559 got_object_tag_get_tagger_gmtoff(struct got_tag_object *tag)
561 return tag->tagger_gmtoff;
564 const char *
565 got_object_tag_get_tagger(struct got_tag_object *tag)
567 return tag->tagger;
570 const char *
571 got_object_tag_get_message(struct got_tag_object *tag)
573 return tag->tagmsg;
576 static struct got_tree_entry *
577 find_entry_by_name(struct got_tree_object *tree, const char *name, size_t len)
579 int i;
581 /* Note that tree entries are sorted in strncmp() order. */
582 for (i = 0; i < tree->nentries; i++) {
583 struct got_tree_entry *te = &tree->entries[i];
584 int cmp = strncmp(te->name, name, len);
585 if (cmp < 0)
586 continue;
587 if (cmp > 0)
588 break;
589 if (te->name[len] == '\0')
590 return te;
592 return NULL;
595 struct got_tree_entry *
596 got_object_tree_find_entry(struct got_tree_object *tree, const char *name)
598 return find_entry_by_name(tree, name, strlen(name));
601 const struct got_error *
602 got_object_tree_find_path(struct got_object_id **id, mode_t *mode,
603 struct got_repository *repo, struct got_tree_object *tree,
604 const char *path)
606 const struct got_error *err = NULL;
607 struct got_tree_object *subtree = NULL;
608 struct got_tree_entry *te = NULL;
609 const char *seg, *s;
610 size_t seglen;
612 *id = NULL;
614 s = path;
615 while (s[0] == '/')
616 s++;
617 seg = s;
618 seglen = 0;
619 subtree = tree;
620 while (*s) {
621 struct got_tree_object *next_tree;
623 if (*s != '/') {
624 s++;
625 seglen++;
626 if (*s)
627 continue;
630 te = find_entry_by_name(subtree, seg, seglen);
631 if (te == NULL) {
632 err = got_error_path(path, GOT_ERR_NO_TREE_ENTRY);
633 goto done;
636 if (*s == '\0')
637 break;
639 seg = s + 1;
640 seglen = 0;
641 s++;
642 if (*s) {
643 err = got_object_open_as_tree(&next_tree, repo,
644 &te->id);
645 te = NULL;
646 if (err)
647 goto done;
648 if (subtree != tree)
649 got_object_tree_close(subtree);
650 subtree = next_tree;
654 if (te) {
655 *id = got_object_id_dup(&te->id);
656 if (*id == NULL)
657 return got_error_from_errno("got_object_id_dup");
658 if (mode)
659 *mode = te->mode;
660 } else
661 err = got_error_path(path, GOT_ERR_NO_TREE_ENTRY);
662 done:
663 if (subtree && subtree != tree)
664 got_object_tree_close(subtree);
665 return err;
668 const struct got_error *
669 got_object_id_by_path(struct got_object_id **id, struct got_repository *repo,
670 struct got_commit_object *commit, const char *path)
672 const struct got_error *err = NULL;
673 struct got_tree_object *tree = NULL;
675 *id = NULL;
677 /* Handle opening of root of commit's tree. */
678 if (got_path_is_root_dir(path)) {
679 *id = got_object_id_dup(commit->tree_id);
680 if (*id == NULL)
681 err = got_error_from_errno("got_object_id_dup");
682 } else {
683 err = got_object_open_as_tree(&tree, repo, commit->tree_id);
684 if (err)
685 goto done;
686 err = got_object_tree_find_path(id, NULL, repo, tree, path);
688 done:
689 if (tree)
690 got_object_tree_close(tree);
691 return err;
694 /*
695 * Normalize file mode bits to avoid false positive tree entry differences
696 * in case tree entries have unexpected mode bits set.
697 */
698 static mode_t
699 normalize_mode_for_comparison(mode_t mode)
701 /*
702 * For directories, the only relevant bit is the IFDIR bit.
703 * This allows us to detect paths changing from a directory
704 * to a file and vice versa.
705 */
706 if (S_ISDIR(mode))
707 return mode & S_IFDIR;
709 /*
710 * For symlinks, the only relevant bit is the IFLNK bit.
711 * This allows us to detect paths changing from a symlinks
712 * to a file or directory and vice versa.
713 */
714 if (S_ISLNK(mode))
715 return mode & S_IFLNK;
717 /* For files, the only change we care about is the executable bit. */
718 return mode & S_IXUSR;
721 const struct got_error *
722 got_object_tree_path_changed(int *changed,
723 struct got_tree_object *tree01, struct got_tree_object *tree02,
724 const char *path, struct got_repository *repo)
726 const struct got_error *err = NULL;
727 struct got_tree_object *tree1 = NULL, *tree2 = NULL;
728 struct got_tree_entry *te1 = NULL, *te2 = NULL;
729 const char *seg, *s;
730 size_t seglen;
732 *changed = 0;
734 /* We not do support comparing the root path. */
735 if (got_path_is_root_dir(path))
736 return got_error_path(path, GOT_ERR_BAD_PATH);
738 tree1 = tree01;
739 tree2 = tree02;
740 s = path;
741 while (*s == '/')
742 s++;
743 seg = s;
744 seglen = 0;
745 while (*s) {
746 struct got_tree_object *next_tree1, *next_tree2;
747 mode_t mode1, mode2;
749 if (*s != '/') {
750 s++;
751 seglen++;
752 if (*s)
753 continue;
756 te1 = find_entry_by_name(tree1, seg, seglen);
757 if (te1 == NULL) {
758 err = got_error(GOT_ERR_NO_OBJ);
759 goto done;
762 if (tree2)
763 te2 = find_entry_by_name(tree2, seg, seglen);
765 if (te2) {
766 mode1 = normalize_mode_for_comparison(te1->mode);
767 mode2 = normalize_mode_for_comparison(te2->mode);
768 if (mode1 != mode2) {
769 *changed = 1;
770 goto done;
773 if (got_object_id_cmp(&te1->id, &te2->id) == 0) {
774 *changed = 0;
775 goto done;
779 if (*s == '\0') { /* final path element */
780 *changed = 1;
781 goto done;
784 seg = s + 1;
785 s++;
786 seglen = 0;
787 if (*s) {
788 err = got_object_open_as_tree(&next_tree1, repo,
789 &te1->id);
790 te1 = NULL;
791 if (err)
792 goto done;
793 if (tree1 != tree01)
794 got_object_tree_close(tree1);
795 tree1 = next_tree1;
797 if (te2) {
798 err = got_object_open_as_tree(&next_tree2, repo,
799 &te2->id);
800 te2 = NULL;
801 if (err)
802 goto done;
803 if (tree2 != tree02)
804 got_object_tree_close(tree2);
805 tree2 = next_tree2;
806 } else if (tree2) {
807 if (tree2 != tree02)
808 got_object_tree_close(tree2);
809 tree2 = NULL;
813 done:
814 if (tree1 && tree1 != tree01)
815 got_object_tree_close(tree1);
816 if (tree2 && tree2 != tree02)
817 got_object_tree_close(tree2);
818 return err;
821 const struct got_error *
822 got_object_tree_entry_dup(struct got_tree_entry **new_te,
823 struct got_tree_entry *te)
825 const struct got_error *err = NULL;
827 *new_te = calloc(1, sizeof(**new_te));
828 if (*new_te == NULL)
829 return got_error_from_errno("calloc");
831 (*new_te)->mode = te->mode;
832 memcpy((*new_te)->name, te->name, sizeof((*new_te)->name));
833 memcpy(&(*new_te)->id, &te->id, sizeof((*new_te)->id));
834 return err;
837 int
838 got_object_tree_entry_is_submodule(struct got_tree_entry *te)
840 return (te->mode & S_IFMT) == (S_IFDIR | S_IFLNK);
843 int
844 got_object_tree_entry_is_symlink(struct got_tree_entry *te)
846 /* S_IFDIR check avoids confusing symlinks with submodules. */
847 return ((te->mode & (S_IFDIR | S_IFLNK)) == S_IFLNK);
850 static const struct got_error *
851 resolve_symlink(char **link_target, const char *path,
852 struct got_commit_object *commit, struct got_repository *repo)
854 const struct got_error *err = NULL;
855 char buf[PATH_MAX];
856 char *name, *parent_path = NULL;
857 struct got_object_id *tree_obj_id = NULL;
858 struct got_tree_object *tree = NULL;
859 struct got_tree_entry *te = NULL;
861 *link_target = NULL;
863 if (strlcpy(buf, path, sizeof(buf)) >= sizeof(buf))
864 return got_error(GOT_ERR_NO_SPACE);
866 name = basename(buf);
867 if (name == NULL)
868 return got_error_from_errno2("basename", path);
870 err = got_path_dirname(&parent_path, path);
871 if (err)
872 return err;
874 err = got_object_id_by_path(&tree_obj_id, repo, commit,
875 parent_path);
876 if (err) {
877 if (err->code == GOT_ERR_NO_TREE_ENTRY) {
878 /* Display the complete path in error message. */
879 err = got_error_path(path, err->code);
881 goto done;
884 err = got_object_open_as_tree(&tree, repo, tree_obj_id);
885 if (err)
886 goto done;
888 te = got_object_tree_find_entry(tree, name);
889 if (te == NULL) {
890 err = got_error_path(path, GOT_ERR_NO_TREE_ENTRY);
891 goto done;
894 if (got_object_tree_entry_is_symlink(te)) {
895 err = got_tree_entry_get_symlink_target(link_target, te, repo);
896 if (err)
897 goto done;
898 if (!got_path_is_absolute(*link_target)) {
899 char *abspath;
900 if (asprintf(&abspath, "%s/%s", parent_path,
901 *link_target) == -1) {
902 err = got_error_from_errno("asprintf");
903 goto done;
905 free(*link_target);
906 *link_target = malloc(PATH_MAX);
907 if (*link_target == NULL) {
908 err = got_error_from_errno("malloc");
909 goto done;
911 err = got_canonpath(abspath, *link_target, PATH_MAX);
912 free(abspath);
913 if (err)
914 goto done;
917 done:
918 free(parent_path);
919 free(tree_obj_id);
920 if (tree)
921 got_object_tree_close(tree);
922 if (err) {
923 free(*link_target);
924 *link_target = NULL;
926 return err;
929 const struct got_error *
930 got_object_resolve_symlinks(char **link_target, const char *path,
931 struct got_commit_object *commit, struct got_repository *repo)
933 const struct got_error *err = NULL;
934 char *next_target = NULL;
935 int max_recursion = 40; /* matches Git */
937 *link_target = NULL;
939 do {
940 err = resolve_symlink(&next_target,
941 *link_target ? *link_target : path, commit, repo);
942 if (err)
943 break;
944 if (next_target) {
945 free(*link_target);
946 if (--max_recursion == 0) {
947 err = got_error_path(path, GOT_ERR_RECURSION);
948 *link_target = NULL;
949 break;
951 *link_target = next_target;
953 } while (next_target);
955 return err;
958 void
959 got_object_commit_retain(struct got_commit_object *commit)
961 commit->refcnt++;
964 const struct got_error *
965 got_object_raw_alloc(struct got_raw_object **obj, uint8_t *outbuf, int *outfd,
966 size_t max_in_mem_size, size_t hdrlen, off_t size)
968 const struct got_error *err = NULL;
969 off_t tot;
971 tot = hdrlen + size;
973 *obj = calloc(1, sizeof(**obj));
974 if (*obj == NULL) {
975 err = got_error_from_errno("calloc");
976 goto done;
978 (*obj)->fd = -1;
979 (*obj)->tempfile_idx = -1;
981 if (outbuf) {
982 (*obj)->data = outbuf;
983 } else {
984 struct stat sb;
985 if (fstat(*outfd, &sb) == -1) {
986 err = got_error_from_errno("fstat");
987 goto done;
990 if (sb.st_size != tot) {
991 err = got_error_msg(GOT_ERR_BAD_OBJ_HDR,
992 "raw object has unexpected size");
993 goto done;
995 #ifndef GOT_PACK_NO_MMAP
996 if (tot > 0 && tot <= max_in_mem_size) {
997 (*obj)->data = mmap(NULL, tot, PROT_READ,
998 MAP_PRIVATE, *outfd, 0);
999 if ((*obj)->data == MAP_FAILED) {
1000 if (errno != ENOMEM) {
1001 err = got_error_from_errno("mmap");
1002 goto done;
1004 (*obj)->data = NULL;
1005 } else {
1006 (*obj)->fd = *outfd;
1007 *outfd = -1;
1010 #endif
1011 if (*outfd != -1) {
1012 (*obj)->f = fdopen(*outfd, "r");
1013 if ((*obj)->f == NULL) {
1014 err = got_error_from_errno("fdopen");
1015 goto done;
1017 *outfd = -1;
1020 (*obj)->hdrlen = hdrlen;
1021 (*obj)->size = size;
1022 done:
1023 if (err) {
1024 if (*obj) {
1025 got_object_raw_close(*obj);
1026 *obj = NULL;
1028 } else
1029 (*obj)->refcnt++;
1030 return err;