free gotwebd servers in gotwebd_shutdown()
[got-portable.git] / lib / object.c
blobeab399777a9e609d5c47a051ccb6670648eb8fb3
1 /*
2 * Copyright (c) 2018, 2019 Stefan Sperling <stsp@openbsd.org>
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.
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.
17 #include "got_compat.h"
19 #include <sys/types.h>
20 #include <sys/stat.h>
21 #include <sys/queue.h>
22 #include <sys/uio.h>
23 #include <sys/mman.h>
25 #include <errno.h>
26 #include <fcntl.h>
27 #include <stdio.h>
28 #include <stdlib.h>
29 #include <string.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_hash.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)
63 return &obj->id;
66 const struct got_error *
67 got_object_get_id_str(char **outbuf, struct got_object *obj)
69 return got_object_id_str(outbuf, &obj->id);
72 const struct got_error *
73 got_object_get_type(int *type, struct got_repository *repo,
74 struct got_object_id *id)
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;
95 got_object_close(obj);
96 return err;
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->hash[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 if (errno == ENOENT)
140 err = got_error_no_obj(id);
141 else
142 err = got_error_from_errno2("open", path);
143 goto done;
145 done:
146 free(path);
147 return err;
150 const struct got_error *
151 got_object_open_by_id_str(struct got_object **obj, struct got_repository *repo,
152 const char *id_str)
154 struct got_object_id id;
156 if (!got_parse_object_id(&id, id_str, repo->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;
182 got_object_tree_get_nentries(struct got_tree_object *tree)
184 return tree->nentries;
187 struct got_tree_entry *
188 got_object_tree_get_first_entry(struct got_tree_object *tree)
190 return got_object_tree_get_entry(tree, 0);
193 struct got_tree_entry *
194 got_object_tree_get_last_entry(struct got_tree_object *tree)
196 return got_object_tree_get_entry(tree, tree->nentries - 1);
199 struct got_tree_entry *
200 got_object_tree_get_entry(struct got_tree_object *tree, int i)
202 if (i < 0 || i >= tree->nentries)
203 return NULL;
204 return &tree->entries[i];
207 mode_t
208 got_tree_entry_get_mode(struct got_tree_entry *te)
210 return te->mode;
213 const char *
214 got_tree_entry_get_name(struct got_tree_entry *te)
216 return &te->name[0];
219 struct got_object_id *
220 got_tree_entry_get_id(struct got_tree_entry *te)
222 return &te->id;
225 const struct got_error *
226 got_object_blob_read_to_str(char **s, struct got_blob_object *blob)
228 const struct got_error *err = NULL;
229 size_t len, totlen, hdrlen, offset;
231 *s = NULL;
233 hdrlen = got_object_blob_get_hdrlen(blob);
234 totlen = 0;
235 offset = 0;
236 do {
237 char *p;
239 err = got_object_blob_read_block(&len, blob);
240 if (err)
241 return err;
243 if (len == 0)
244 break;
246 totlen += len - hdrlen;
247 p = realloc(*s, totlen + 1);
248 if (p == NULL) {
249 err = got_error_from_errno("realloc");
250 free(*s);
251 *s = NULL;
252 return err;
254 *s = p;
255 /* Skip blob object header first time around. */
256 memcpy(*s + offset,
257 got_object_blob_get_read_buf(blob) + hdrlen, len - hdrlen);
258 hdrlen = 0;
259 offset = totlen;
260 } while (len > 0);
262 (*s)[totlen] = '\0';
263 return NULL;
266 const struct got_error *
267 got_tree_entry_get_symlink_target(char **link_target, struct got_tree_entry *te,
268 struct got_repository *repo)
270 const struct got_error *err = NULL;
271 struct got_blob_object *blob = NULL;
272 int fd = -1;
274 *link_target = NULL;
276 if (!got_object_tree_entry_is_symlink(te))
277 return got_error(GOT_ERR_TREE_ENTRY_TYPE);
279 fd = got_opentempfd();
280 if (fd == -1) {
281 err = got_error_from_errno("got_opentempfd");
282 goto done;
285 err = got_object_open_as_blob(&blob, repo,
286 got_tree_entry_get_id(te), PATH_MAX, fd);
287 if (err)
288 goto done;
290 err = got_object_blob_read_to_str(link_target, blob);
291 done:
292 if (fd != -1 && close(fd) == -1 && err == NULL)
293 err = got_error_from_errno("close");
294 if (blob)
295 got_object_blob_close(blob);
296 if (err) {
297 free(*link_target);
298 *link_target = NULL;
300 return err;
304 got_tree_entry_get_index(struct got_tree_entry *te)
306 return te->idx;
309 struct got_tree_entry *
310 got_tree_entry_get_next(struct got_tree_object *tree,
311 struct got_tree_entry *te)
313 return got_object_tree_get_entry(tree, te->idx + 1);
316 struct got_tree_entry *
317 got_tree_entry_get_prev(struct got_tree_object *tree,
318 struct got_tree_entry *te)
320 return got_object_tree_get_entry(tree, te->idx - 1);
323 const struct got_error *
324 got_object_blob_close(struct got_blob_object *blob)
326 const struct got_error *err = NULL;
327 free(blob->read_buf);
328 if (blob->f && fclose(blob->f) == EOF)
329 err = got_error_from_errno("fclose");
330 free(blob->data);
331 free(blob);
332 return err;
335 void
336 got_object_blob_rewind(struct got_blob_object *blob)
338 if (blob->f)
339 rewind(blob->f);
342 char *
343 got_object_blob_id_str(struct got_blob_object *blob, char *buf, size_t size)
345 return got_object_id_hex(&blob->id, buf, size);
348 size_t
349 got_object_blob_get_hdrlen(struct got_blob_object *blob)
351 return blob->hdrlen;
354 const uint8_t *
355 got_object_blob_get_read_buf(struct got_blob_object *blob)
357 return blob->read_buf;
360 const struct got_error *
361 got_object_blob_read_block(size_t *outlenp, struct got_blob_object *blob)
363 size_t n;
365 n = fread(blob->read_buf, 1, blob->blocksize, blob->f);
366 if (n == 0 && ferror(blob->f))
367 return got_ferror(blob->f, GOT_ERR_IO);
368 *outlenp = n;
369 return NULL;
372 const struct got_error *
373 got_object_blob_is_binary(int *binary, struct got_blob_object *blob)
375 const struct got_error *err;
376 size_t hdrlen, len;
378 *binary = 0;
379 hdrlen = got_object_blob_get_hdrlen(blob);
381 if (fseeko(blob->f, hdrlen, SEEK_SET) == -1)
382 return got_error_from_errno("fseeko");
384 err = got_object_blob_read_block(&len, blob);
385 if (err)
386 return err;
388 *binary = memchr(blob->read_buf, '\0', len) != NULL;
390 if (fseeko(blob->f, hdrlen, SEEK_SET) == -1)
391 return got_error_from_errno("fseeko");
392 return NULL;
395 const struct got_error *
396 got_object_blob_getline(char **line, ssize_t *linelen, size_t *linesize,
397 struct got_blob_object *blob)
399 *linelen = getline(line, linesize, blob->f);
400 if (*linelen == -1 && !feof(blob->f))
401 return got_error_from_errno("getline");
402 return NULL;
405 const struct got_error *
406 got_object_blob_dump_to_file(off_t *filesize, int *nlines,
407 off_t **line_offsets, FILE *outfile, struct got_blob_object *blob)
409 const struct got_error *err = NULL;
410 size_t n, len, hdrlen;
411 const uint8_t *buf;
412 int i;
413 const int alloc_chunksz = 512;
414 size_t nalloc = 0;
415 off_t off = 0, total_len = 0;
417 if (line_offsets)
418 *line_offsets = NULL;
419 if (filesize)
420 *filesize = 0;
421 if (nlines)
422 *nlines = 0;
424 hdrlen = got_object_blob_get_hdrlen(blob);
425 do {
426 err = got_object_blob_read_block(&len, blob);
427 if (err)
428 return err;
429 if (len == 0)
430 break;
431 buf = got_object_blob_get_read_buf(blob);
432 i = hdrlen;
433 if (nlines) {
434 if (line_offsets && *line_offsets == NULL) {
435 /* Have some data but perhaps no '\n'. */
436 *nlines = 1;
437 nalloc = alloc_chunksz;
438 *line_offsets = calloc(nalloc,
439 sizeof(**line_offsets));
440 if (*line_offsets == NULL)
441 return got_error_from_errno("calloc");
443 /* Skip forward over end of first line. */
444 while (i < len) {
445 if (buf[i] == '\n')
446 break;
447 i++;
450 /* Scan '\n' offsets in remaining chunk of data. */
451 while (i < len) {
452 if (buf[i] != '\n') {
453 i++;
454 continue;
456 (*nlines)++;
457 if (line_offsets && nalloc < *nlines) {
458 size_t n = *nlines + alloc_chunksz;
459 off_t *o = recallocarray(*line_offsets,
460 nalloc, n, sizeof(**line_offsets));
461 if (o == NULL) {
462 free(*line_offsets);
463 *line_offsets = NULL;
464 return got_error_from_errno(
465 "recallocarray");
467 *line_offsets = o;
468 nalloc = n;
470 if (line_offsets) {
471 off = total_len + i - hdrlen + 1;
472 (*line_offsets)[*nlines - 1] = off;
474 i++;
477 /* Skip blob object header first time around. */
478 n = fwrite(buf + hdrlen, 1, len - hdrlen, outfile);
479 if (n != len - hdrlen)
480 return got_ferror(outfile, GOT_ERR_IO);
481 total_len += len - hdrlen;
482 hdrlen = 0;
483 } while (len != 0);
485 if (fflush(outfile) != 0)
486 return got_error_from_errno("fflush");
487 rewind(outfile);
489 if (filesize)
490 *filesize = total_len;
492 return NULL;
495 const char *
496 got_object_tag_get_name(struct got_tag_object *tag)
498 return tag->tag;
502 got_object_tag_get_object_type(struct got_tag_object *tag)
504 return tag->obj_type;
507 struct got_object_id *
508 got_object_tag_get_object_id(struct got_tag_object *tag)
510 return &tag->id;
513 time_t
514 got_object_tag_get_tagger_time(struct got_tag_object *tag)
516 return tag->tagger_time;
519 time_t
520 got_object_tag_get_tagger_gmtoff(struct got_tag_object *tag)
522 return tag->tagger_gmtoff;
525 const char *
526 got_object_tag_get_tagger(struct got_tag_object *tag)
528 return tag->tagger;
531 const char *
532 got_object_tag_get_message(struct got_tag_object *tag)
534 return tag->tagmsg;
537 static struct got_tree_entry *
538 find_entry_by_name(struct got_tree_object *tree, const char *name, size_t len)
540 int i;
542 /* Note that tree entries are sorted in strncmp() order. */
543 for (i = 0; i < tree->nentries; i++) {
544 struct got_tree_entry *te = &tree->entries[i];
545 int cmp = strncmp(te->name, name, len);
546 if (cmp < 0)
547 continue;
548 if (cmp > 0)
549 break;
550 if (te->name[len] == '\0')
551 return te;
553 return NULL;
556 struct got_tree_entry *
557 got_object_tree_find_entry(struct got_tree_object *tree, const char *name)
559 return find_entry_by_name(tree, name, strlen(name));
562 const struct got_error *
563 got_object_tree_find_path(struct got_object_id **id, mode_t *mode,
564 struct got_repository *repo, struct got_tree_object *tree,
565 const char *path)
567 const struct got_error *err = NULL;
568 struct got_tree_object *subtree = NULL;
569 struct got_tree_entry *te = NULL;
570 const char *seg, *s;
571 size_t seglen;
573 *id = NULL;
575 s = path;
576 while (s[0] == '/')
577 s++;
578 seg = s;
579 seglen = 0;
580 subtree = tree;
581 while (*s) {
582 struct got_tree_object *next_tree;
584 if (*s != '/') {
585 s++;
586 seglen++;
587 if (*s)
588 continue;
591 te = find_entry_by_name(subtree, seg, seglen);
592 if (te == NULL) {
593 err = got_error_path(path, GOT_ERR_NO_TREE_ENTRY);
594 goto done;
597 if (*s == '\0')
598 break;
600 seg = s + 1;
601 seglen = 0;
602 s++;
603 if (*s) {
604 err = got_object_open_as_tree(&next_tree, repo,
605 &te->id);
606 te = NULL;
607 if (err)
608 goto done;
609 if (subtree != tree)
610 got_object_tree_close(subtree);
611 subtree = next_tree;
615 if (te) {
616 *id = got_object_id_dup(&te->id);
617 if (*id == NULL)
618 return got_error_from_errno("got_object_id_dup");
619 if (mode)
620 *mode = te->mode;
621 } else
622 err = got_error_path(path, GOT_ERR_NO_TREE_ENTRY);
623 done:
624 if (subtree && subtree != tree)
625 got_object_tree_close(subtree);
626 return err;
629 const struct got_error *
630 got_object_id_by_path(struct got_object_id **id, struct got_repository *repo,
631 struct got_commit_object *commit, const char *path)
633 const struct got_error *err = NULL;
634 struct got_tree_object *tree = NULL;
636 *id = NULL;
638 /* Handle opening of root of commit's tree. */
639 if (got_path_is_root_dir(path)) {
640 *id = got_object_id_dup(commit->tree_id);
641 if (*id == NULL)
642 err = got_error_from_errno("got_object_id_dup");
643 } else {
644 err = got_object_open_as_tree(&tree, repo, commit->tree_id);
645 if (err)
646 goto done;
647 err = got_object_tree_find_path(id, NULL, repo, tree, path);
649 done:
650 if (tree)
651 got_object_tree_close(tree);
652 return err;
656 * Normalize file mode bits to avoid false positive tree entry differences
657 * in case tree entries have unexpected mode bits set.
659 static mode_t
660 normalize_mode_for_comparison(mode_t mode)
663 * For directories, the only relevant bit is the IFDIR bit.
664 * This allows us to detect paths changing from a directory
665 * to a file and vice versa.
667 if (S_ISDIR(mode))
668 return mode & S_IFDIR;
671 * For symlinks, the only relevant bit is the IFLNK bit.
672 * This allows us to detect paths changing from a symlinks
673 * to a file or directory and vice versa.
675 if (S_ISLNK(mode))
676 return mode & S_IFLNK;
678 /* For files, the only change we care about is the executable bit. */
679 return mode & S_IXUSR;
682 const struct got_error *
683 got_object_tree_path_changed(int *changed,
684 struct got_tree_object *tree01, struct got_tree_object *tree02,
685 const char *path, struct got_repository *repo)
687 const struct got_error *err = NULL;
688 struct got_tree_object *tree1 = NULL, *tree2 = NULL;
689 struct got_tree_entry *te1 = NULL, *te2 = NULL;
690 const char *seg, *s;
691 size_t seglen;
693 *changed = 0;
695 /* We not do support comparing the root path. */
696 if (got_path_is_root_dir(path))
697 return got_error_path(path, GOT_ERR_BAD_PATH);
699 tree1 = tree01;
700 tree2 = tree02;
701 s = path;
702 while (*s == '/')
703 s++;
704 seg = s;
705 seglen = 0;
706 while (*s) {
707 struct got_tree_object *next_tree1, *next_tree2;
708 mode_t mode1, mode2;
710 if (*s != '/') {
711 s++;
712 seglen++;
713 if (*s)
714 continue;
717 te1 = find_entry_by_name(tree1, seg, seglen);
718 if (te1 == NULL) {
719 err = got_error(GOT_ERR_NO_OBJ);
720 goto done;
723 if (tree2)
724 te2 = find_entry_by_name(tree2, seg, seglen);
726 if (te2) {
727 mode1 = normalize_mode_for_comparison(te1->mode);
728 mode2 = normalize_mode_for_comparison(te2->mode);
729 if (mode1 != mode2) {
730 *changed = 1;
731 goto done;
734 if (got_object_id_cmp(&te1->id, &te2->id) == 0) {
735 *changed = 0;
736 goto done;
740 if (*s == '\0') { /* final path element */
741 *changed = 1;
742 goto done;
745 seg = s + 1;
746 s++;
747 seglen = 0;
748 if (*s) {
749 err = got_object_open_as_tree(&next_tree1, repo,
750 &te1->id);
751 te1 = NULL;
752 if (err)
753 goto done;
754 if (tree1 != tree01)
755 got_object_tree_close(tree1);
756 tree1 = next_tree1;
758 if (te2) {
759 err = got_object_open_as_tree(&next_tree2, repo,
760 &te2->id);
761 te2 = NULL;
762 if (err)
763 goto done;
764 if (tree2 != tree02)
765 got_object_tree_close(tree2);
766 tree2 = next_tree2;
767 } else if (tree2) {
768 if (tree2 != tree02)
769 got_object_tree_close(tree2);
770 tree2 = NULL;
774 done:
775 if (tree1 && tree1 != tree01)
776 got_object_tree_close(tree1);
777 if (tree2 && tree2 != tree02)
778 got_object_tree_close(tree2);
779 return err;
782 const struct got_error *
783 got_object_tree_entry_dup(struct got_tree_entry **new_te,
784 struct got_tree_entry *te)
786 const struct got_error *err = NULL;
788 *new_te = calloc(1, sizeof(**new_te));
789 if (*new_te == NULL)
790 return got_error_from_errno("calloc");
792 (*new_te)->mode = te->mode;
793 memcpy((*new_te)->name, te->name, sizeof((*new_te)->name));
794 memcpy(&(*new_te)->id, &te->id, sizeof((*new_te)->id));
795 return err;
799 got_object_tree_entry_is_submodule(struct got_tree_entry *te)
801 return (te->mode & S_IFMT) == (S_IFDIR | S_IFLNK);
805 got_object_tree_entry_is_symlink(struct got_tree_entry *te)
807 /* S_IFDIR check avoids confusing symlinks with submodules. */
808 return ((te->mode & (S_IFDIR | S_IFLNK)) == S_IFLNK);
811 static const struct got_error *
812 resolve_symlink(char **link_target, const char *path,
813 struct got_commit_object *commit, struct got_repository *repo)
815 const struct got_error *err = NULL;
816 char buf[PATH_MAX];
817 char *name, *parent_path = NULL;
818 struct got_object_id *tree_obj_id = NULL;
819 struct got_tree_object *tree = NULL;
820 struct got_tree_entry *te = NULL;
822 *link_target = NULL;
824 if (strlcpy(buf, path, sizeof(buf)) >= sizeof(buf))
825 return got_error(GOT_ERR_NO_SPACE);
827 name = basename(buf);
828 if (name == NULL)
829 return got_error_from_errno2("basename", path);
831 err = got_path_dirname(&parent_path, path);
832 if (err)
833 return err;
835 err = got_object_id_by_path(&tree_obj_id, repo, commit,
836 parent_path);
837 if (err) {
838 if (err->code == GOT_ERR_NO_TREE_ENTRY) {
839 /* Display the complete path in error message. */
840 err = got_error_path(path, err->code);
842 goto done;
845 err = got_object_open_as_tree(&tree, repo, tree_obj_id);
846 if (err)
847 goto done;
849 te = got_object_tree_find_entry(tree, name);
850 if (te == NULL) {
851 err = got_error_path(path, GOT_ERR_NO_TREE_ENTRY);
852 goto done;
855 if (got_object_tree_entry_is_symlink(te)) {
856 err = got_tree_entry_get_symlink_target(link_target, te, repo);
857 if (err)
858 goto done;
859 if (!got_path_is_absolute(*link_target)) {
860 char *abspath;
861 if (asprintf(&abspath, "%s/%s", parent_path,
862 *link_target) == -1) {
863 err = got_error_from_errno("asprintf");
864 goto done;
866 free(*link_target);
867 *link_target = malloc(PATH_MAX);
868 if (*link_target == NULL) {
869 err = got_error_from_errno("malloc");
870 goto done;
872 err = got_canonpath(abspath, *link_target, PATH_MAX);
873 free(abspath);
874 if (err)
875 goto done;
878 done:
879 free(parent_path);
880 free(tree_obj_id);
881 if (tree)
882 got_object_tree_close(tree);
883 if (err) {
884 free(*link_target);
885 *link_target = NULL;
887 return err;
890 const struct got_error *
891 got_object_resolve_symlinks(char **link_target, const char *path,
892 struct got_commit_object *commit, struct got_repository *repo)
894 const struct got_error *err = NULL;
895 char *next_target = NULL;
896 int max_recursion = 40; /* matches Git */
898 *link_target = NULL;
900 do {
901 err = resolve_symlink(&next_target,
902 *link_target ? *link_target : path, commit, repo);
903 if (err)
904 break;
905 if (next_target) {
906 free(*link_target);
907 if (--max_recursion == 0) {
908 err = got_error_path(path, GOT_ERR_RECURSION);
909 *link_target = NULL;
910 break;
912 *link_target = next_target;
914 } while (next_target);
916 return err;
919 void
920 got_object_commit_retain(struct got_commit_object *commit)
922 commit->refcnt++;
925 const struct got_error *
926 got_object_raw_alloc(struct got_raw_object **obj, uint8_t *outbuf, int *outfd,
927 size_t max_in_mem_size, size_t hdrlen, off_t size)
929 const struct got_error *err = NULL;
930 off_t tot;
932 tot = hdrlen + size;
934 *obj = calloc(1, sizeof(**obj));
935 if (*obj == NULL) {
936 err = got_error_from_errno("calloc");
937 goto done;
939 (*obj)->fd = -1;
940 (*obj)->tempfile_idx = -1;
942 if (outbuf) {
943 (*obj)->data = outbuf;
944 } else {
945 struct stat sb;
946 if (fstat(*outfd, &sb) == -1) {
947 err = got_error_from_errno("fstat");
948 goto done;
951 if (sb.st_size != tot) {
952 err = got_error_msg(GOT_ERR_BAD_OBJ_HDR,
953 "raw object has unexpected size");
954 goto done;
956 #ifndef GOT_PACK_NO_MMAP
957 if (tot > 0 && tot <= max_in_mem_size) {
958 (*obj)->data = mmap(NULL, tot, PROT_READ,
959 MAP_PRIVATE, *outfd, 0);
960 if ((*obj)->data == MAP_FAILED) {
961 if (errno != ENOMEM) {
962 err = got_error_from_errno("mmap");
963 goto done;
965 (*obj)->data = NULL;
966 } else {
967 (*obj)->fd = *outfd;
968 *outfd = -1;
971 #endif
972 if (*outfd != -1) {
973 (*obj)->f = fdopen(*outfd, "r");
974 if ((*obj)->f == NULL) {
975 err = got_error_from_errno("fdopen");
976 goto done;
978 *outfd = -1;
981 (*obj)->hdrlen = hdrlen;
982 (*obj)->size = size;
983 done:
984 if (err) {
985 if (*obj) {
986 got_object_raw_close(*obj);
987 *obj = NULL;
989 } else
990 (*obj)->refcnt++;
991 return err;