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;
154 memset(&id, 0, sizeof(id));
155 id.algo = got_repo_get_object_format(repo);
156 if (!got_parse_hash_digest(id.hash, id_str, id.algo))
157 return got_error_path(id_str, GOT_ERR_BAD_OBJ_ID_STR);
159 return got_object_open(obj, repo, &id);
162 const struct got_error *
163 got_object_resolve_id_str(struct got_object_id **id,
164 struct got_repository *repo, const char *id_str)
166 const struct got_error *err = NULL;
167 struct got_object *obj;
169 err = got_object_open_by_id_str(&obj, repo, id_str);
170 if (err)
171 return err;
173 *id = got_object_id_dup(got_object_get_id(obj));
174 got_object_close(obj);
175 if (*id == NULL)
176 return got_error_from_errno("got_object_id_dup");
178 return NULL;
181 const struct got_error *
182 got_object_qid_alloc(struct got_object_qid **qid, struct got_object_id *id)
184 *qid = calloc(1, sizeof(**qid));
185 if (*qid == NULL)
186 return got_error_from_errno("calloc");
188 memcpy(&(*qid)->id, id, sizeof((*qid)->id));
189 return NULL;
192 const struct got_error *
193 got_object_id_queue_copy(const struct got_object_id_queue *src,
194 struct got_object_id_queue *dest)
196 const struct got_error *err;
197 struct got_object_qid *qid;
199 STAILQ_FOREACH(qid, src, entry) {
200 struct got_object_qid *new;
201 /*
202 * Deep-copy the object ID only. Let the caller deal
203 * with setting up the new->data pointer if needed.
204 */
205 err = got_object_qid_alloc(&new, &qid->id);
206 if (err) {
207 got_object_id_queue_free(dest);
208 return err;
210 STAILQ_INSERT_TAIL(dest, new, entry);
213 return NULL;
216 int
217 got_object_tree_get_nentries(struct got_tree_object *tree)
219 return tree->nentries;
222 struct got_tree_entry *
223 got_object_tree_get_first_entry(struct got_tree_object *tree)
225 return got_object_tree_get_entry(tree, 0);
228 struct got_tree_entry *
229 got_object_tree_get_last_entry(struct got_tree_object *tree)
231 return got_object_tree_get_entry(tree, tree->nentries - 1);
234 struct got_tree_entry *
235 got_object_tree_get_entry(struct got_tree_object *tree, int i)
237 if (i < 0 || i >= tree->nentries)
238 return NULL;
239 return &tree->entries[i];
242 mode_t
243 got_tree_entry_get_mode(struct got_tree_entry *te)
245 return te->mode;
248 const char *
249 got_tree_entry_get_name(struct got_tree_entry *te)
251 return &te->name[0];
254 struct got_object_id *
255 got_tree_entry_get_id(struct got_tree_entry *te)
257 return &te->id;
260 const struct got_error *
261 got_object_blob_read_to_str(char **s, struct got_blob_object *blob)
263 const struct got_error *err = NULL;
264 size_t len, totlen, hdrlen, offset;
266 *s = NULL;
268 hdrlen = got_object_blob_get_hdrlen(blob);
269 totlen = 0;
270 offset = 0;
271 do {
272 char *p;
274 err = got_object_blob_read_block(&len, blob);
275 if (err)
276 return err;
278 if (len == 0)
279 break;
281 totlen += len - hdrlen;
282 p = realloc(*s, totlen + 1);
283 if (p == NULL) {
284 err = got_error_from_errno("realloc");
285 free(*s);
286 *s = NULL;
287 return err;
289 *s = p;
290 /* Skip blob object header first time around. */
291 memcpy(*s + offset,
292 got_object_blob_get_read_buf(blob) + hdrlen, len - hdrlen);
293 hdrlen = 0;
294 offset = totlen;
295 } while (len > 0);
297 (*s)[totlen] = '\0';
298 return NULL;
301 const struct got_error *
302 got_tree_entry_get_symlink_target(char **link_target, struct got_tree_entry *te,
303 struct got_repository *repo)
305 const struct got_error *err = NULL;
306 struct got_blob_object *blob = NULL;
307 int fd = -1;
309 *link_target = NULL;
311 if (!got_object_tree_entry_is_symlink(te))
312 return got_error(GOT_ERR_TREE_ENTRY_TYPE);
314 fd = got_opentempfd();
315 if (fd == -1) {
316 err = got_error_from_errno("got_opentempfd");
317 goto done;
320 err = got_object_open_as_blob(&blob, repo,
321 got_tree_entry_get_id(te), PATH_MAX, fd);
322 if (err)
323 goto done;
325 err = got_object_blob_read_to_str(link_target, blob);
326 done:
327 if (fd != -1 && close(fd) == -1 && err == NULL)
328 err = got_error_from_errno("close");
329 if (blob)
330 got_object_blob_close(blob);
331 if (err) {
332 free(*link_target);
333 *link_target = NULL;
335 return err;
338 int
339 got_tree_entry_get_index(struct got_tree_entry *te)
341 return te->idx;
344 struct got_tree_entry *
345 got_tree_entry_get_next(struct got_tree_object *tree,
346 struct got_tree_entry *te)
348 return got_object_tree_get_entry(tree, te->idx + 1);
351 struct got_tree_entry *
352 got_tree_entry_get_prev(struct got_tree_object *tree,
353 struct got_tree_entry *te)
355 return got_object_tree_get_entry(tree, te->idx - 1);
358 const struct got_error *
359 got_object_blob_close(struct got_blob_object *blob)
361 const struct got_error *err = NULL;
362 free(blob->read_buf);
363 if (blob->f && fclose(blob->f) == EOF)
364 err = got_error_from_errno("fclose");
365 free(blob->data);
366 free(blob);
367 return err;
370 void
371 got_object_blob_rewind(struct got_blob_object *blob)
373 if (blob->f)
374 rewind(blob->f);
377 char *
378 got_object_blob_id_str(struct got_blob_object *blob, char *buf, size_t size)
380 return got_object_id_hex(&blob->id, buf, size);
383 size_t
384 got_object_blob_get_hdrlen(struct got_blob_object *blob)
386 return blob->hdrlen;
389 const uint8_t *
390 got_object_blob_get_read_buf(struct got_blob_object *blob)
392 return blob->read_buf;
395 const struct got_error *
396 got_object_blob_read_block(size_t *outlenp, struct got_blob_object *blob)
398 size_t n;
400 n = fread(blob->read_buf, 1, blob->blocksize, blob->f);
401 if (n == 0 && ferror(blob->f))
402 return got_ferror(blob->f, GOT_ERR_IO);
403 *outlenp = n;
404 return NULL;
407 const struct got_error *
408 got_object_blob_is_binary(int *binary, struct got_blob_object *blob)
410 const struct got_error *err;
411 size_t hdrlen, len;
413 *binary = 0;
414 hdrlen = got_object_blob_get_hdrlen(blob);
416 if (fseeko(blob->f, hdrlen, SEEK_SET) == -1)
417 return got_error_from_errno("fseeko");
419 err = got_object_blob_read_block(&len, blob);
420 if (err)
421 return err;
423 *binary = memchr(blob->read_buf, '\0', len) != NULL;
425 if (fseeko(blob->f, hdrlen, SEEK_SET) == -1)
426 return got_error_from_errno("fseeko");
427 return NULL;
430 const struct got_error *
431 got_object_blob_getline(char **line, ssize_t *linelen, size_t *linesize,
432 struct got_blob_object *blob)
434 *linelen = getline(line, linesize, blob->f);
435 if (*linelen == -1 && !feof(blob->f))
436 return got_error_from_errno("getline");
437 return NULL;
440 const struct got_error *
441 got_object_blob_dump_to_file(off_t *filesize, int *nlines,
442 off_t **line_offsets, FILE *outfile, struct got_blob_object *blob)
444 const struct got_error *err = NULL;
445 size_t n, len, hdrlen;
446 const uint8_t *buf;
447 int i;
448 const int alloc_chunksz = 512;
449 size_t nalloc = 0;
450 off_t off = 0, total_len = 0;
452 if (line_offsets)
453 *line_offsets = NULL;
454 if (filesize)
455 *filesize = 0;
456 if (nlines)
457 *nlines = 0;
459 hdrlen = got_object_blob_get_hdrlen(blob);
460 do {
461 err = got_object_blob_read_block(&len, blob);
462 if (err)
463 return err;
464 if (len == 0)
465 break;
466 buf = got_object_blob_get_read_buf(blob);
467 i = hdrlen;
468 if (nlines) {
469 if (line_offsets && *line_offsets == NULL) {
470 /* Have some data but perhaps no '\n'. */
471 *nlines = 1;
472 nalloc = alloc_chunksz;
473 *line_offsets = calloc(nalloc,
474 sizeof(**line_offsets));
475 if (*line_offsets == NULL)
476 return got_error_from_errno("calloc");
478 /* Skip forward over end of first line. */
479 while (i < len) {
480 if (buf[i] == '\n')
481 break;
482 i++;
485 /* Scan '\n' offsets in remaining chunk of data. */
486 while (i < len) {
487 if (buf[i] != '\n') {
488 i++;
489 continue;
491 (*nlines)++;
492 if (line_offsets && nalloc < *nlines) {
493 size_t n = *nlines + alloc_chunksz;
494 off_t *o = recallocarray(*line_offsets,
495 nalloc, n, sizeof(**line_offsets));
496 if (o == NULL) {
497 free(*line_offsets);
498 *line_offsets = NULL;
499 return got_error_from_errno(
500 "recallocarray");
502 *line_offsets = o;
503 nalloc = n;
505 if (line_offsets) {
506 off = total_len + i - hdrlen + 1;
507 (*line_offsets)[*nlines - 1] = off;
509 i++;
512 /* Skip blob object header first time around. */
513 n = fwrite(buf + hdrlen, 1, len - hdrlen, outfile);
514 if (n != len - hdrlen)
515 return got_ferror(outfile, GOT_ERR_IO);
516 total_len += len - hdrlen;
517 hdrlen = 0;
518 } while (len != 0);
520 if (fflush(outfile) != 0)
521 return got_error_from_errno("fflush");
522 rewind(outfile);
524 if (filesize)
525 *filesize = total_len;
527 return NULL;
530 const char *
531 got_object_tag_get_name(struct got_tag_object *tag)
533 return tag->tag;
536 int
537 got_object_tag_get_object_type(struct got_tag_object *tag)
539 return tag->obj_type;
542 struct got_object_id *
543 got_object_tag_get_object_id(struct got_tag_object *tag)
545 return &tag->id;
548 time_t
549 got_object_tag_get_tagger_time(struct got_tag_object *tag)
551 return tag->tagger_time;
554 time_t
555 got_object_tag_get_tagger_gmtoff(struct got_tag_object *tag)
557 return tag->tagger_gmtoff;
560 const char *
561 got_object_tag_get_tagger(struct got_tag_object *tag)
563 return tag->tagger;
566 const char *
567 got_object_tag_get_message(struct got_tag_object *tag)
569 return tag->tagmsg;
572 static struct got_tree_entry *
573 find_entry_by_name(struct got_tree_object *tree, const char *name, size_t len)
575 int i;
577 /* Note that tree entries are sorted in strncmp() order. */
578 for (i = 0; i < tree->nentries; i++) {
579 struct got_tree_entry *te = &tree->entries[i];
580 int cmp = strncmp(te->name, name, len);
581 if (cmp < 0)
582 continue;
583 if (cmp > 0)
584 break;
585 if (te->name[len] == '\0')
586 return te;
588 return NULL;
591 struct got_tree_entry *
592 got_object_tree_find_entry(struct got_tree_object *tree, const char *name)
594 return find_entry_by_name(tree, name, strlen(name));
597 const struct got_error *
598 got_object_tree_find_path(struct got_object_id **id, mode_t *mode,
599 struct got_repository *repo, struct got_tree_object *tree,
600 const char *path)
602 const struct got_error *err = NULL;
603 struct got_tree_object *subtree = NULL;
604 struct got_tree_entry *te = NULL;
605 const char *seg, *s;
606 size_t seglen;
608 *id = NULL;
610 s = path;
611 while (s[0] == '/')
612 s++;
613 seg = s;
614 seglen = 0;
615 subtree = tree;
616 while (*s) {
617 struct got_tree_object *next_tree;
619 if (*s != '/') {
620 s++;
621 seglen++;
622 if (*s)
623 continue;
626 te = find_entry_by_name(subtree, seg, seglen);
627 if (te == NULL) {
628 err = got_error_path(path, GOT_ERR_NO_TREE_ENTRY);
629 goto done;
632 if (*s == '\0')
633 break;
635 seg = s + 1;
636 seglen = 0;
637 s++;
638 if (*s) {
639 err = got_object_open_as_tree(&next_tree, repo,
640 &te->id);
641 te = NULL;
642 if (err)
643 goto done;
644 if (subtree != tree)
645 got_object_tree_close(subtree);
646 subtree = next_tree;
650 if (te) {
651 *id = got_object_id_dup(&te->id);
652 if (*id == NULL)
653 return got_error_from_errno("got_object_id_dup");
654 if (mode)
655 *mode = te->mode;
656 } else
657 err = got_error_path(path, GOT_ERR_NO_TREE_ENTRY);
658 done:
659 if (subtree && subtree != tree)
660 got_object_tree_close(subtree);
661 return err;
664 const struct got_error *
665 got_object_id_by_path(struct got_object_id **id, struct got_repository *repo,
666 struct got_commit_object *commit, const char *path)
668 const struct got_error *err = NULL;
669 struct got_tree_object *tree = NULL;
671 *id = NULL;
673 /* Handle opening of root of commit's tree. */
674 if (got_path_is_root_dir(path)) {
675 *id = got_object_id_dup(commit->tree_id);
676 if (*id == NULL)
677 err = got_error_from_errno("got_object_id_dup");
678 } else {
679 err = got_object_open_as_tree(&tree, repo, commit->tree_id);
680 if (err)
681 goto done;
682 err = got_object_tree_find_path(id, NULL, repo, tree, path);
684 done:
685 if (tree)
686 got_object_tree_close(tree);
687 return err;
690 /*
691 * Normalize file mode bits to avoid false positive tree entry differences
692 * in case tree entries have unexpected mode bits set.
693 */
694 static mode_t
695 normalize_mode_for_comparison(mode_t mode)
697 /*
698 * For directories, the only relevant bit is the IFDIR bit.
699 * This allows us to detect paths changing from a directory
700 * to a file and vice versa.
701 */
702 if (S_ISDIR(mode))
703 return mode & S_IFDIR;
705 /*
706 * For symlinks, the only relevant bit is the IFLNK bit.
707 * This allows us to detect paths changing from a symlinks
708 * to a file or directory and vice versa.
709 */
710 if (S_ISLNK(mode))
711 return mode & S_IFLNK;
713 /* For files, the only change we care about is the executable bit. */
714 return mode & S_IXUSR;
717 const struct got_error *
718 got_object_tree_path_changed(int *changed,
719 struct got_tree_object *tree01, struct got_tree_object *tree02,
720 const char *path, struct got_repository *repo)
722 const struct got_error *err = NULL;
723 struct got_tree_object *tree1 = NULL, *tree2 = NULL;
724 struct got_tree_entry *te1 = NULL, *te2 = NULL;
725 const char *seg, *s;
726 size_t seglen;
728 *changed = 0;
730 /* We not do support comparing the root path. */
731 if (got_path_is_root_dir(path))
732 return got_error_path(path, GOT_ERR_BAD_PATH);
734 tree1 = tree01;
735 tree2 = tree02;
736 s = path;
737 while (*s == '/')
738 s++;
739 seg = s;
740 seglen = 0;
741 while (*s) {
742 struct got_tree_object *next_tree1, *next_tree2;
743 mode_t mode1, mode2;
745 if (*s != '/') {
746 s++;
747 seglen++;
748 if (*s)
749 continue;
752 te1 = find_entry_by_name(tree1, seg, seglen);
753 if (te1 == NULL) {
754 err = got_error(GOT_ERR_NO_OBJ);
755 goto done;
758 if (tree2)
759 te2 = find_entry_by_name(tree2, seg, seglen);
761 if (te2) {
762 mode1 = normalize_mode_for_comparison(te1->mode);
763 mode2 = normalize_mode_for_comparison(te2->mode);
764 if (mode1 != mode2) {
765 *changed = 1;
766 goto done;
769 if (got_object_id_cmp(&te1->id, &te2->id) == 0) {
770 *changed = 0;
771 goto done;
775 if (*s == '\0') { /* final path element */
776 *changed = 1;
777 goto done;
780 seg = s + 1;
781 s++;
782 seglen = 0;
783 if (*s) {
784 err = got_object_open_as_tree(&next_tree1, repo,
785 &te1->id);
786 te1 = NULL;
787 if (err)
788 goto done;
789 if (tree1 != tree01)
790 got_object_tree_close(tree1);
791 tree1 = next_tree1;
793 if (te2) {
794 err = got_object_open_as_tree(&next_tree2, repo,
795 &te2->id);
796 te2 = NULL;
797 if (err)
798 goto done;
799 if (tree2 != tree02)
800 got_object_tree_close(tree2);
801 tree2 = next_tree2;
802 } else if (tree2) {
803 if (tree2 != tree02)
804 got_object_tree_close(tree2);
805 tree2 = NULL;
809 done:
810 if (tree1 && tree1 != tree01)
811 got_object_tree_close(tree1);
812 if (tree2 && tree2 != tree02)
813 got_object_tree_close(tree2);
814 return err;
817 const struct got_error *
818 got_object_tree_entry_dup(struct got_tree_entry **new_te,
819 struct got_tree_entry *te)
821 const struct got_error *err = NULL;
823 *new_te = calloc(1, sizeof(**new_te));
824 if (*new_te == NULL)
825 return got_error_from_errno("calloc");
827 (*new_te)->mode = te->mode;
828 memcpy((*new_te)->name, te->name, sizeof((*new_te)->name));
829 memcpy(&(*new_te)->id, &te->id, sizeof((*new_te)->id));
830 return err;
833 int
834 got_object_tree_entry_is_submodule(struct got_tree_entry *te)
836 return (te->mode & S_IFMT) == (S_IFDIR | S_IFLNK);
839 int
840 got_object_tree_entry_is_symlink(struct got_tree_entry *te)
842 /* S_IFDIR check avoids confusing symlinks with submodules. */
843 return ((te->mode & (S_IFDIR | S_IFLNK)) == S_IFLNK);
846 static const struct got_error *
847 resolve_symlink(char **link_target, const char *path,
848 struct got_commit_object *commit, struct got_repository *repo)
850 const struct got_error *err = NULL;
851 char buf[PATH_MAX];
852 char *name, *parent_path = NULL;
853 struct got_object_id *tree_obj_id = NULL;
854 struct got_tree_object *tree = NULL;
855 struct got_tree_entry *te = NULL;
857 *link_target = NULL;
859 if (strlcpy(buf, path, sizeof(buf)) >= sizeof(buf))
860 return got_error(GOT_ERR_NO_SPACE);
862 name = basename(buf);
863 if (name == NULL)
864 return got_error_from_errno2("basename", path);
866 err = got_path_dirname(&parent_path, path);
867 if (err)
868 return err;
870 err = got_object_id_by_path(&tree_obj_id, repo, commit,
871 parent_path);
872 if (err) {
873 if (err->code == GOT_ERR_NO_TREE_ENTRY) {
874 /* Display the complete path in error message. */
875 err = got_error_path(path, err->code);
877 goto done;
880 err = got_object_open_as_tree(&tree, repo, tree_obj_id);
881 if (err)
882 goto done;
884 te = got_object_tree_find_entry(tree, name);
885 if (te == NULL) {
886 err = got_error_path(path, GOT_ERR_NO_TREE_ENTRY);
887 goto done;
890 if (got_object_tree_entry_is_symlink(te)) {
891 err = got_tree_entry_get_symlink_target(link_target, te, repo);
892 if (err)
893 goto done;
894 if (!got_path_is_absolute(*link_target)) {
895 char *abspath;
896 if (asprintf(&abspath, "%s/%s", parent_path,
897 *link_target) == -1) {
898 err = got_error_from_errno("asprintf");
899 goto done;
901 free(*link_target);
902 *link_target = malloc(PATH_MAX);
903 if (*link_target == NULL) {
904 err = got_error_from_errno("malloc");
905 goto done;
907 err = got_canonpath(abspath, *link_target, PATH_MAX);
908 free(abspath);
909 if (err)
910 goto done;
913 done:
914 free(parent_path);
915 free(tree_obj_id);
916 if (tree)
917 got_object_tree_close(tree);
918 if (err) {
919 free(*link_target);
920 *link_target = NULL;
922 return err;
925 const struct got_error *
926 got_object_resolve_symlinks(char **link_target, const char *path,
927 struct got_commit_object *commit, struct got_repository *repo)
929 const struct got_error *err = NULL;
930 char *next_target = NULL;
931 int max_recursion = 40; /* matches Git */
933 *link_target = NULL;
935 do {
936 err = resolve_symlink(&next_target,
937 *link_target ? *link_target : path, commit, repo);
938 if (err)
939 break;
940 if (next_target) {
941 free(*link_target);
942 if (--max_recursion == 0) {
943 err = got_error_path(path, GOT_ERR_RECURSION);
944 *link_target = NULL;
945 break;
947 *link_target = next_target;
949 } while (next_target);
951 return err;
954 void
955 got_object_commit_retain(struct got_commit_object *commit)
957 commit->refcnt++;
960 const struct got_error *
961 got_object_raw_alloc(struct got_raw_object **obj, uint8_t *outbuf, int *outfd,
962 size_t max_in_mem_size, size_t hdrlen, off_t size)
964 const struct got_error *err = NULL;
965 off_t tot;
967 tot = hdrlen + size;
969 *obj = calloc(1, sizeof(**obj));
970 if (*obj == NULL) {
971 err = got_error_from_errno("calloc");
972 goto done;
974 (*obj)->fd = -1;
975 (*obj)->tempfile_idx = -1;
977 if (outbuf) {
978 (*obj)->data = outbuf;
979 } else {
980 struct stat sb;
981 if (fstat(*outfd, &sb) == -1) {
982 err = got_error_from_errno("fstat");
983 goto done;
986 if (sb.st_size != tot) {
987 err = got_error_msg(GOT_ERR_BAD_OBJ_HDR,
988 "raw object has unexpected size");
989 goto done;
991 #ifndef GOT_PACK_NO_MMAP
992 if (tot > 0 && tot <= max_in_mem_size) {
993 (*obj)->data = mmap(NULL, tot, PROT_READ,
994 MAP_PRIVATE, *outfd, 0);
995 if ((*obj)->data == MAP_FAILED) {
996 if (errno != ENOMEM) {
997 err = got_error_from_errno("mmap");
998 goto done;
1000 (*obj)->data = NULL;
1001 } else {
1002 (*obj)->fd = *outfd;
1003 *outfd = -1;
1006 #endif
1007 if (*outfd != -1) {
1008 (*obj)->f = fdopen(*outfd, "r");
1009 if ((*obj)->f == NULL) {
1010 err = got_error_from_errno("fdopen");
1011 goto done;
1013 *outfd = -1;
1016 (*obj)->hdrlen = hdrlen;
1017 (*obj)->size = size;
1018 done:
1019 if (err) {
1020 if (*obj) {
1021 got_object_raw_close(*obj);
1022 *obj = NULL;
1024 } else
1025 (*obj)->refcnt++;
1026 return err;