Blob


1 /*
2 * Copyright (c) 2018, 2019 Stefan Sperling <stsp@openbsd.org>
3 *
4 * Permission to use, copy, modify, and distribute this software for any
5 * purpose with or without fee is hereby granted, provided that the above
6 * copyright notice and this permission notice appear in all copies.
7 *
8 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
15 */
17 #include <sys/types.h>
18 #include <sys/stat.h>
19 #include <sys/queue.h>
20 #include <sys/tree.h>
21 #include <sys/uio.h>
22 #include <sys/mman.h>
24 #include <errno.h>
25 #include <fcntl.h>
26 #include <stdio.h>
27 #include <stdlib.h>
28 #include <string.h>
29 #include <sha1.h>
30 #include <sha2.h>
31 #include <unistd.h>
32 #include <zlib.h>
33 #include <libgen.h>
34 #include <limits.h>
35 #include <imsg.h>
37 #include "got_error.h"
38 #include "got_object.h"
39 #include "got_repository.h"
40 #include "got_opentemp.h"
41 #include "got_path.h"
43 #include "got_lib_hash.h"
44 #include "got_lib_delta.h"
45 #include "got_lib_inflate.h"
46 #include "got_lib_object.h"
47 #include "got_lib_object_idcache.h"
48 #include "got_lib_object_cache.h"
49 #include "got_lib_object_parse.h"
50 #include "got_lib_pack.h"
51 #include "got_lib_repository.h"
53 #ifndef MIN
54 #define MIN(_a,_b) ((_a) < (_b) ? (_a) : (_b))
55 #endif
57 #ifndef nitems
58 #define nitems(_a) (sizeof((_a)) / sizeof((_a)[0]))
59 #endif
61 struct got_object_id *
62 got_object_get_id(struct got_object *obj)
63 {
64 return &obj->id;
65 }
67 const struct got_error *
68 got_object_get_id_str(char **outbuf, struct got_object *obj)
69 {
70 return got_object_id_str(outbuf, &obj->id);
71 }
73 const struct got_error *
74 got_object_get_type(int *type, struct got_repository *repo,
75 struct got_object_id *id)
76 {
77 const struct got_error *err = NULL;
78 struct got_object *obj;
80 err = got_object_open(&obj, repo, id);
81 if (err)
82 return err;
84 switch (obj->type) {
85 case GOT_OBJ_TYPE_COMMIT:
86 case GOT_OBJ_TYPE_TREE:
87 case GOT_OBJ_TYPE_BLOB:
88 case GOT_OBJ_TYPE_TAG:
89 *type = obj->type;
90 break;
91 default:
92 err = got_error(GOT_ERR_OBJ_TYPE);
93 break;
94 }
96 got_object_close(obj);
97 return err;
98 }
100 const struct got_error *
101 got_object_get_path(char **path, struct got_object_id *id,
102 struct got_repository *repo)
104 const struct got_error *err = NULL;
105 char *hex = NULL;
106 char *path_objects;
108 *path = NULL;
110 path_objects = got_repo_get_path_objects(repo);
111 if (path_objects == NULL)
112 return got_error_from_errno("got_repo_get_path_objects");
114 err = got_object_id_str(&hex, id);
115 if (err)
116 goto done;
118 if (asprintf(path, "%s/%.2x/%s", path_objects,
119 id->sha1[0], hex + 2) == -1)
120 err = got_error_from_errno("asprintf");
122 done:
123 free(hex);
124 free(path_objects);
125 return err;
128 const struct got_error *
129 got_object_open_loose_fd(int *fd, struct got_object_id *id,
130 struct got_repository *repo)
132 const struct got_error *err = NULL;
133 char *path;
135 err = got_object_get_path(&path, id, repo);
136 if (err)
137 return err;
138 *fd = open(path, O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
139 if (*fd == -1) {
140 err = got_error_from_errno2("open", path);
141 goto done;
143 done:
144 free(path);
145 return err;
148 const struct got_error *
149 got_object_open_by_id_str(struct got_object **obj, struct got_repository *repo,
150 const char *id_str)
152 struct got_object_id id;
154 if (!got_parse_object_id(&id, id_str, GOT_HASH_SHA1))
155 return got_error_path(id_str, GOT_ERR_BAD_OBJ_ID_STR);
157 return got_object_open(obj, repo, &id);
160 const struct got_error *
161 got_object_resolve_id_str(struct got_object_id **id,
162 struct got_repository *repo, const char *id_str)
164 const struct got_error *err = NULL;
165 struct got_object *obj;
167 err = got_object_open_by_id_str(&obj, repo, id_str);
168 if (err)
169 return err;
171 *id = got_object_id_dup(got_object_get_id(obj));
172 got_object_close(obj);
173 if (*id == NULL)
174 return got_error_from_errno("got_object_id_dup");
176 return NULL;
179 const struct got_error *
180 got_object_qid_alloc(struct got_object_qid **qid, struct got_object_id *id)
182 *qid = calloc(1, sizeof(**qid));
183 if (*qid == NULL)
184 return got_error_from_errno("calloc");
186 memcpy(&(*qid)->id, id, sizeof((*qid)->id));
187 return NULL;
190 const struct got_error *
191 got_object_id_queue_copy(const struct got_object_id_queue *src,
192 struct got_object_id_queue *dest)
194 const struct got_error *err;
195 struct got_object_qid *qid;
197 STAILQ_FOREACH(qid, src, entry) {
198 struct got_object_qid *new;
199 /*
200 * Deep-copy the object ID only. Let the caller deal
201 * with setting up the new->data pointer if needed.
202 */
203 err = got_object_qid_alloc(&new, &qid->id);
204 if (err) {
205 got_object_id_queue_free(dest);
206 return err;
208 STAILQ_INSERT_TAIL(dest, new, entry);
211 return NULL;
214 int
215 got_object_tree_get_nentries(struct got_tree_object *tree)
217 return tree->nentries;
220 struct got_tree_entry *
221 got_object_tree_get_first_entry(struct got_tree_object *tree)
223 return got_object_tree_get_entry(tree, 0);
226 struct got_tree_entry *
227 got_object_tree_get_last_entry(struct got_tree_object *tree)
229 return got_object_tree_get_entry(tree, tree->nentries - 1);
232 struct got_tree_entry *
233 got_object_tree_get_entry(struct got_tree_object *tree, int i)
235 if (i < 0 || i >= tree->nentries)
236 return NULL;
237 return &tree->entries[i];
240 mode_t
241 got_tree_entry_get_mode(struct got_tree_entry *te)
243 return te->mode;
246 const char *
247 got_tree_entry_get_name(struct got_tree_entry *te)
249 return &te->name[0];
252 struct got_object_id *
253 got_tree_entry_get_id(struct got_tree_entry *te)
255 return &te->id;
258 const struct got_error *
259 got_object_blob_read_to_str(char **s, struct got_blob_object *blob)
261 const struct got_error *err = NULL;
262 size_t len, totlen, hdrlen, offset;
264 *s = NULL;
266 hdrlen = got_object_blob_get_hdrlen(blob);
267 totlen = 0;
268 offset = 0;
269 do {
270 char *p;
272 err = got_object_blob_read_block(&len, blob);
273 if (err)
274 return err;
276 if (len == 0)
277 break;
279 totlen += len - hdrlen;
280 p = realloc(*s, totlen + 1);
281 if (p == NULL) {
282 err = got_error_from_errno("realloc");
283 free(*s);
284 *s = NULL;
285 return err;
287 *s = p;
288 /* Skip blob object header first time around. */
289 memcpy(*s + offset,
290 got_object_blob_get_read_buf(blob) + hdrlen, len - hdrlen);
291 hdrlen = 0;
292 offset = totlen;
293 } while (len > 0);
295 (*s)[totlen] = '\0';
296 return NULL;
299 const struct got_error *
300 got_tree_entry_get_symlink_target(char **link_target, struct got_tree_entry *te,
301 struct got_repository *repo)
303 const struct got_error *err = NULL;
304 struct got_blob_object *blob = NULL;
305 int fd = -1;
307 *link_target = NULL;
309 if (!got_object_tree_entry_is_symlink(te))
310 return got_error(GOT_ERR_TREE_ENTRY_TYPE);
312 fd = got_opentempfd();
313 if (fd == -1) {
314 err = got_error_from_errno("got_opentempfd");
315 goto done;
318 err = got_object_open_as_blob(&blob, repo,
319 got_tree_entry_get_id(te), PATH_MAX, fd);
320 if (err)
321 goto done;
323 err = got_object_blob_read_to_str(link_target, blob);
324 done:
325 if (fd != -1 && close(fd) == -1 && err == NULL)
326 err = got_error_from_errno("close");
327 if (blob)
328 got_object_blob_close(blob);
329 if (err) {
330 free(*link_target);
331 *link_target = NULL;
333 return err;
336 int
337 got_tree_entry_get_index(struct got_tree_entry *te)
339 return te->idx;
342 struct got_tree_entry *
343 got_tree_entry_get_next(struct got_tree_object *tree,
344 struct got_tree_entry *te)
346 return got_object_tree_get_entry(tree, te->idx + 1);
349 struct got_tree_entry *
350 got_tree_entry_get_prev(struct got_tree_object *tree,
351 struct got_tree_entry *te)
353 return got_object_tree_get_entry(tree, te->idx - 1);
356 const struct got_error *
357 got_object_blob_close(struct got_blob_object *blob)
359 const struct got_error *err = NULL;
360 free(blob->read_buf);
361 if (blob->f && fclose(blob->f) == EOF)
362 err = got_error_from_errno("fclose");
363 free(blob->data);
364 free(blob);
365 return err;
368 void
369 got_object_blob_rewind(struct got_blob_object *blob)
371 if (blob->f)
372 rewind(blob->f);
375 char *
376 got_object_blob_id_str(struct got_blob_object *blob, char *buf, size_t size)
378 return got_object_id_hex(&blob->id, buf, size);
381 size_t
382 got_object_blob_get_hdrlen(struct got_blob_object *blob)
384 return blob->hdrlen;
387 const uint8_t *
388 got_object_blob_get_read_buf(struct got_blob_object *blob)
390 return blob->read_buf;
393 const struct got_error *
394 got_object_blob_read_block(size_t *outlenp, struct got_blob_object *blob)
396 size_t n;
398 n = fread(blob->read_buf, 1, blob->blocksize, blob->f);
399 if (n == 0 && ferror(blob->f))
400 return got_ferror(blob->f, GOT_ERR_IO);
401 *outlenp = n;
402 return NULL;
405 const struct got_error *
406 got_object_blob_is_binary(int *binary, struct got_blob_object *blob)
408 const struct got_error *err;
409 size_t hdrlen, len;
411 *binary = 0;
412 hdrlen = got_object_blob_get_hdrlen(blob);
414 if (fseeko(blob->f, hdrlen, SEEK_SET) == -1)
415 return got_error_from_errno("fseeko");
417 err = got_object_blob_read_block(&len, blob);
418 if (err)
419 return err;
421 *binary = memchr(blob->read_buf, '\0', len) != NULL;
423 if (fseeko(blob->f, hdrlen, SEEK_SET) == -1)
424 return got_error_from_errno("fseeko");
425 return NULL;
428 const struct got_error *
429 got_object_blob_getline(char **line, ssize_t *linelen, size_t *linesize,
430 struct got_blob_object *blob)
432 *linelen = getline(line, linesize, blob->f);
433 if (*linelen == -1 && !feof(blob->f))
434 return got_error_from_errno("getline");
435 return NULL;
438 const struct got_error *
439 got_object_blob_dump_to_file(off_t *filesize, int *nlines,
440 off_t **line_offsets, FILE *outfile, struct got_blob_object *blob)
442 const struct got_error *err = NULL;
443 size_t n, len, hdrlen;
444 const uint8_t *buf;
445 int i;
446 const int alloc_chunksz = 512;
447 size_t nalloc = 0;
448 off_t off = 0, total_len = 0;
450 if (line_offsets)
451 *line_offsets = NULL;
452 if (filesize)
453 *filesize = 0;
454 if (nlines)
455 *nlines = 0;
457 hdrlen = got_object_blob_get_hdrlen(blob);
458 do {
459 err = got_object_blob_read_block(&len, blob);
460 if (err)
461 return err;
462 if (len == 0)
463 break;
464 buf = got_object_blob_get_read_buf(blob);
465 i = hdrlen;
466 if (nlines) {
467 if (line_offsets && *line_offsets == NULL) {
468 /* Have some data but perhaps no '\n'. */
469 *nlines = 1;
470 nalloc = alloc_chunksz;
471 *line_offsets = calloc(nalloc,
472 sizeof(**line_offsets));
473 if (*line_offsets == NULL)
474 return got_error_from_errno("calloc");
476 /* Skip forward over end of first line. */
477 while (i < len) {
478 if (buf[i] == '\n')
479 break;
480 i++;
483 /* Scan '\n' offsets in remaining chunk of data. */
484 while (i < len) {
485 if (buf[i] != '\n') {
486 i++;
487 continue;
489 (*nlines)++;
490 if (line_offsets && nalloc < *nlines) {
491 size_t n = *nlines + alloc_chunksz;
492 off_t *o = recallocarray(*line_offsets,
493 nalloc, n, sizeof(**line_offsets));
494 if (o == NULL) {
495 free(*line_offsets);
496 *line_offsets = NULL;
497 return got_error_from_errno(
498 "recallocarray");
500 *line_offsets = o;
501 nalloc = n;
503 if (line_offsets) {
504 off = total_len + i - hdrlen + 1;
505 (*line_offsets)[*nlines - 1] = off;
507 i++;
510 /* Skip blob object header first time around. */
511 n = fwrite(buf + hdrlen, 1, len - hdrlen, outfile);
512 if (n != len - hdrlen)
513 return got_ferror(outfile, GOT_ERR_IO);
514 total_len += len - hdrlen;
515 hdrlen = 0;
516 } while (len != 0);
518 if (fflush(outfile) != 0)
519 return got_error_from_errno("fflush");
520 rewind(outfile);
522 if (filesize)
523 *filesize = total_len;
525 return NULL;
528 const char *
529 got_object_tag_get_name(struct got_tag_object *tag)
531 return tag->tag;
534 int
535 got_object_tag_get_object_type(struct got_tag_object *tag)
537 return tag->obj_type;
540 struct got_object_id *
541 got_object_tag_get_object_id(struct got_tag_object *tag)
543 return &tag->id;
546 time_t
547 got_object_tag_get_tagger_time(struct got_tag_object *tag)
549 return tag->tagger_time;
552 time_t
553 got_object_tag_get_tagger_gmtoff(struct got_tag_object *tag)
555 return tag->tagger_gmtoff;
558 const char *
559 got_object_tag_get_tagger(struct got_tag_object *tag)
561 return tag->tagger;
564 const char *
565 got_object_tag_get_message(struct got_tag_object *tag)
567 return tag->tagmsg;
570 static struct got_tree_entry *
571 find_entry_by_name(struct got_tree_object *tree, const char *name, size_t len)
573 int i;
575 /* Note that tree entries are sorted in strncmp() order. */
576 for (i = 0; i < tree->nentries; i++) {
577 struct got_tree_entry *te = &tree->entries[i];
578 int cmp = strncmp(te->name, name, len);
579 if (cmp < 0)
580 continue;
581 if (cmp > 0)
582 break;
583 if (te->name[len] == '\0')
584 return te;
586 return NULL;
589 struct got_tree_entry *
590 got_object_tree_find_entry(struct got_tree_object *tree, const char *name)
592 return find_entry_by_name(tree, name, strlen(name));
595 const struct got_error *
596 got_object_tree_find_path(struct got_object_id **id, mode_t *mode,
597 struct got_repository *repo, struct got_tree_object *tree,
598 const char *path)
600 const struct got_error *err = NULL;
601 struct got_tree_object *subtree = NULL;
602 struct got_tree_entry *te = NULL;
603 const char *seg, *s;
604 size_t seglen;
606 *id = NULL;
608 s = path;
609 while (s[0] == '/')
610 s++;
611 seg = s;
612 seglen = 0;
613 subtree = tree;
614 while (*s) {
615 struct got_tree_object *next_tree;
617 if (*s != '/') {
618 s++;
619 seglen++;
620 if (*s)
621 continue;
624 te = find_entry_by_name(subtree, seg, seglen);
625 if (te == NULL) {
626 err = got_error_path(path, GOT_ERR_NO_TREE_ENTRY);
627 goto done;
630 if (*s == '\0')
631 break;
633 seg = s + 1;
634 seglen = 0;
635 s++;
636 if (*s) {
637 err = got_object_open_as_tree(&next_tree, repo,
638 &te->id);
639 te = NULL;
640 if (err)
641 goto done;
642 if (subtree != tree)
643 got_object_tree_close(subtree);
644 subtree = next_tree;
648 if (te) {
649 *id = got_object_id_dup(&te->id);
650 if (*id == NULL)
651 return got_error_from_errno("got_object_id_dup");
652 if (mode)
653 *mode = te->mode;
654 } else
655 err = got_error_path(path, GOT_ERR_NO_TREE_ENTRY);
656 done:
657 if (subtree && subtree != tree)
658 got_object_tree_close(subtree);
659 return err;
662 const struct got_error *
663 got_object_id_by_path(struct got_object_id **id, struct got_repository *repo,
664 struct got_commit_object *commit, const char *path)
666 const struct got_error *err = NULL;
667 struct got_tree_object *tree = NULL;
669 *id = NULL;
671 /* Handle opening of root of commit's tree. */
672 if (got_path_is_root_dir(path)) {
673 *id = got_object_id_dup(commit->tree_id);
674 if (*id == NULL)
675 err = got_error_from_errno("got_object_id_dup");
676 } else {
677 err = got_object_open_as_tree(&tree, repo, commit->tree_id);
678 if (err)
679 goto done;
680 err = got_object_tree_find_path(id, NULL, repo, tree, path);
682 done:
683 if (tree)
684 got_object_tree_close(tree);
685 return err;
688 /*
689 * Normalize file mode bits to avoid false positive tree entry differences
690 * in case tree entries have unexpected mode bits set.
691 */
692 static mode_t
693 normalize_mode_for_comparison(mode_t mode)
695 /*
696 * For directories, the only relevant bit is the IFDIR bit.
697 * This allows us to detect paths changing from a directory
698 * to a file and vice versa.
699 */
700 if (S_ISDIR(mode))
701 return mode & S_IFDIR;
703 /*
704 * For symlinks, the only relevant bit is the IFLNK bit.
705 * This allows us to detect paths changing from a symlinks
706 * to a file or directory and vice versa.
707 */
708 if (S_ISLNK(mode))
709 return mode & S_IFLNK;
711 /* For files, the only change we care about is the executable bit. */
712 return mode & S_IXUSR;
715 const struct got_error *
716 got_object_tree_path_changed(int *changed,
717 struct got_tree_object *tree01, struct got_tree_object *tree02,
718 const char *path, struct got_repository *repo)
720 const struct got_error *err = NULL;
721 struct got_tree_object *tree1 = NULL, *tree2 = NULL;
722 struct got_tree_entry *te1 = NULL, *te2 = NULL;
723 const char *seg, *s;
724 size_t seglen;
726 *changed = 0;
728 /* We not do support comparing the root path. */
729 if (got_path_is_root_dir(path))
730 return got_error_path(path, GOT_ERR_BAD_PATH);
732 tree1 = tree01;
733 tree2 = tree02;
734 s = path;
735 while (*s == '/')
736 s++;
737 seg = s;
738 seglen = 0;
739 while (*s) {
740 struct got_tree_object *next_tree1, *next_tree2;
741 mode_t mode1, mode2;
743 if (*s != '/') {
744 s++;
745 seglen++;
746 if (*s)
747 continue;
750 te1 = find_entry_by_name(tree1, seg, seglen);
751 if (te1 == NULL) {
752 err = got_error(GOT_ERR_NO_OBJ);
753 goto done;
756 if (tree2)
757 te2 = find_entry_by_name(tree2, seg, seglen);
759 if (te2) {
760 mode1 = normalize_mode_for_comparison(te1->mode);
761 mode2 = normalize_mode_for_comparison(te2->mode);
762 if (mode1 != mode2) {
763 *changed = 1;
764 goto done;
767 if (got_object_id_cmp(&te1->id, &te2->id) == 0) {
768 *changed = 0;
769 goto done;
773 if (*s == '\0') { /* final path element */
774 *changed = 1;
775 goto done;
778 seg = s + 1;
779 s++;
780 seglen = 0;
781 if (*s) {
782 err = got_object_open_as_tree(&next_tree1, repo,
783 &te1->id);
784 te1 = NULL;
785 if (err)
786 goto done;
787 if (tree1 != tree01)
788 got_object_tree_close(tree1);
789 tree1 = next_tree1;
791 if (te2) {
792 err = got_object_open_as_tree(&next_tree2, repo,
793 &te2->id);
794 te2 = NULL;
795 if (err)
796 goto done;
797 if (tree2 != tree02)
798 got_object_tree_close(tree2);
799 tree2 = next_tree2;
800 } else if (tree2) {
801 if (tree2 != tree02)
802 got_object_tree_close(tree2);
803 tree2 = NULL;
807 done:
808 if (tree1 && tree1 != tree01)
809 got_object_tree_close(tree1);
810 if (tree2 && tree2 != tree02)
811 got_object_tree_close(tree2);
812 return err;
815 const struct got_error *
816 got_object_tree_entry_dup(struct got_tree_entry **new_te,
817 struct got_tree_entry *te)
819 const struct got_error *err = NULL;
821 *new_te = calloc(1, sizeof(**new_te));
822 if (*new_te == NULL)
823 return got_error_from_errno("calloc");
825 (*new_te)->mode = te->mode;
826 memcpy((*new_te)->name, te->name, sizeof((*new_te)->name));
827 memcpy(&(*new_te)->id, &te->id, sizeof((*new_te)->id));
828 return err;
831 int
832 got_object_tree_entry_is_submodule(struct got_tree_entry *te)
834 return (te->mode & S_IFMT) == (S_IFDIR | S_IFLNK);
837 int
838 got_object_tree_entry_is_symlink(struct got_tree_entry *te)
840 /* S_IFDIR check avoids confusing symlinks with submodules. */
841 return ((te->mode & (S_IFDIR | S_IFLNK)) == S_IFLNK);
844 static const struct got_error *
845 resolve_symlink(char **link_target, const char *path,
846 struct got_commit_object *commit, struct got_repository *repo)
848 const struct got_error *err = NULL;
849 char buf[PATH_MAX];
850 char *name, *parent_path = NULL;
851 struct got_object_id *tree_obj_id = NULL;
852 struct got_tree_object *tree = NULL;
853 struct got_tree_entry *te = NULL;
855 *link_target = NULL;
857 if (strlcpy(buf, path, sizeof(buf)) >= sizeof(buf))
858 return got_error(GOT_ERR_NO_SPACE);
860 name = basename(buf);
861 if (name == NULL)
862 return got_error_from_errno2("basename", path);
864 err = got_path_dirname(&parent_path, path);
865 if (err)
866 return err;
868 err = got_object_id_by_path(&tree_obj_id, repo, commit,
869 parent_path);
870 if (err) {
871 if (err->code == GOT_ERR_NO_TREE_ENTRY) {
872 /* Display the complete path in error message. */
873 err = got_error_path(path, err->code);
875 goto done;
878 err = got_object_open_as_tree(&tree, repo, tree_obj_id);
879 if (err)
880 goto done;
882 te = got_object_tree_find_entry(tree, name);
883 if (te == NULL) {
884 err = got_error_path(path, GOT_ERR_NO_TREE_ENTRY);
885 goto done;
888 if (got_object_tree_entry_is_symlink(te)) {
889 err = got_tree_entry_get_symlink_target(link_target, te, repo);
890 if (err)
891 goto done;
892 if (!got_path_is_absolute(*link_target)) {
893 char *abspath;
894 if (asprintf(&abspath, "%s/%s", parent_path,
895 *link_target) == -1) {
896 err = got_error_from_errno("asprintf");
897 goto done;
899 free(*link_target);
900 *link_target = malloc(PATH_MAX);
901 if (*link_target == NULL) {
902 err = got_error_from_errno("malloc");
903 goto done;
905 err = got_canonpath(abspath, *link_target, PATH_MAX);
906 free(abspath);
907 if (err)
908 goto done;
911 done:
912 free(parent_path);
913 free(tree_obj_id);
914 if (tree)
915 got_object_tree_close(tree);
916 if (err) {
917 free(*link_target);
918 *link_target = NULL;
920 return err;
923 const struct got_error *
924 got_object_resolve_symlinks(char **link_target, const char *path,
925 struct got_commit_object *commit, struct got_repository *repo)
927 const struct got_error *err = NULL;
928 char *next_target = NULL;
929 int max_recursion = 40; /* matches Git */
931 *link_target = NULL;
933 do {
934 err = resolve_symlink(&next_target,
935 *link_target ? *link_target : path, commit, repo);
936 if (err)
937 break;
938 if (next_target) {
939 free(*link_target);
940 if (--max_recursion == 0) {
941 err = got_error_path(path, GOT_ERR_RECURSION);
942 *link_target = NULL;
943 break;
945 *link_target = next_target;
947 } while (next_target);
949 return err;
952 void
953 got_object_commit_retain(struct got_commit_object *commit)
955 commit->refcnt++;
958 const struct got_error *
959 got_object_raw_alloc(struct got_raw_object **obj, uint8_t *outbuf, int *outfd,
960 size_t max_in_mem_size, size_t hdrlen, off_t size)
962 const struct got_error *err = NULL;
963 off_t tot;
965 tot = hdrlen + size;
967 *obj = calloc(1, sizeof(**obj));
968 if (*obj == NULL) {
969 err = got_error_from_errno("calloc");
970 goto done;
972 (*obj)->fd = -1;
973 (*obj)->tempfile_idx = -1;
975 if (outbuf) {
976 (*obj)->data = outbuf;
977 } else {
978 struct stat sb;
979 if (fstat(*outfd, &sb) == -1) {
980 err = got_error_from_errno("fstat");
981 goto done;
984 if (sb.st_size != tot) {
985 err = got_error_msg(GOT_ERR_BAD_OBJ_HDR,
986 "raw object has unexpected size");
987 goto done;
989 #ifndef GOT_PACK_NO_MMAP
990 if (tot > 0 && tot <= max_in_mem_size) {
991 (*obj)->data = mmap(NULL, tot, PROT_READ,
992 MAP_PRIVATE, *outfd, 0);
993 if ((*obj)->data == MAP_FAILED) {
994 if (errno != ENOMEM) {
995 err = got_error_from_errno("mmap");
996 goto done;
998 (*obj)->data = NULL;
999 } else {
1000 (*obj)->fd = *outfd;
1001 *outfd = -1;
1004 #endif
1005 if (*outfd != -1) {
1006 (*obj)->f = fdopen(*outfd, "r");
1007 if ((*obj)->f == NULL) {
1008 err = got_error_from_errno("fdopen");
1009 goto done;
1011 *outfd = -1;
1014 (*obj)->hdrlen = hdrlen;
1015 (*obj)->size = size;
1016 done:
1017 if (err) {
1018 if (*obj) {
1019 got_object_raw_close(*obj);
1020 *obj = NULL;
1022 } else
1023 (*obj)->refcnt++;
1024 return err;