portable: enable gotctl (via --enable-gotd)
[got-portable.git] / lib / object.c
blobc8f77ee93a078ac6e44faf47dced3ce65da6c463
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->sha1[0], hex + 2) == -1)
119 err = got_error_from_errno("asprintf");
121 done:
122 free(hex);
123 free(path_objects);
124 return err;
127 const struct got_error *
128 got_object_open_loose_fd(int *fd, struct got_object_id *id,
129 struct got_repository *repo)
131 const struct got_error *err = NULL;
132 char *path;
134 err = got_object_get_path(&path, id, repo);
135 if (err)
136 return err;
137 *fd = open(path, O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
138 if (*fd == -1) {
139 err = got_error_from_errno2("open", path);
140 goto done;
142 done:
143 free(path);
144 return err;
147 const struct got_error *
148 got_object_open_by_id_str(struct got_object **obj, struct got_repository *repo,
149 const char *id_str)
151 struct got_object_id id;
153 if (!got_parse_object_id(&id, id_str, GOT_HASH_SHA1))
154 return got_error_path(id_str, GOT_ERR_BAD_OBJ_ID_STR);
156 return got_object_open(obj, repo, &id);
159 const struct got_error *
160 got_object_resolve_id_str(struct got_object_id **id,
161 struct got_repository *repo, const char *id_str)
163 const struct got_error *err = NULL;
164 struct got_object *obj;
166 err = got_object_open_by_id_str(&obj, repo, id_str);
167 if (err)
168 return err;
170 *id = got_object_id_dup(got_object_get_id(obj));
171 got_object_close(obj);
172 if (*id == NULL)
173 return got_error_from_errno("got_object_id_dup");
175 return NULL;
179 got_object_tree_get_nentries(struct got_tree_object *tree)
181 return tree->nentries;
184 struct got_tree_entry *
185 got_object_tree_get_first_entry(struct got_tree_object *tree)
187 return got_object_tree_get_entry(tree, 0);
190 struct got_tree_entry *
191 got_object_tree_get_last_entry(struct got_tree_object *tree)
193 return got_object_tree_get_entry(tree, tree->nentries - 1);
196 struct got_tree_entry *
197 got_object_tree_get_entry(struct got_tree_object *tree, int i)
199 if (i < 0 || i >= tree->nentries)
200 return NULL;
201 return &tree->entries[i];
204 mode_t
205 got_tree_entry_get_mode(struct got_tree_entry *te)
207 return te->mode;
210 const char *
211 got_tree_entry_get_name(struct got_tree_entry *te)
213 return &te->name[0];
216 struct got_object_id *
217 got_tree_entry_get_id(struct got_tree_entry *te)
219 return &te->id;
222 const struct got_error *
223 got_object_blob_read_to_str(char **s, struct got_blob_object *blob)
225 const struct got_error *err = NULL;
226 size_t len, totlen, hdrlen, offset;
228 *s = NULL;
230 hdrlen = got_object_blob_get_hdrlen(blob);
231 totlen = 0;
232 offset = 0;
233 do {
234 char *p;
236 err = got_object_blob_read_block(&len, blob);
237 if (err)
238 return err;
240 if (len == 0)
241 break;
243 totlen += len - hdrlen;
244 p = realloc(*s, totlen + 1);
245 if (p == NULL) {
246 err = got_error_from_errno("realloc");
247 free(*s);
248 *s = NULL;
249 return err;
251 *s = p;
252 /* Skip blob object header first time around. */
253 memcpy(*s + offset,
254 got_object_blob_get_read_buf(blob) + hdrlen, len - hdrlen);
255 hdrlen = 0;
256 offset = totlen;
257 } while (len > 0);
259 (*s)[totlen] = '\0';
260 return NULL;
263 const struct got_error *
264 got_tree_entry_get_symlink_target(char **link_target, struct got_tree_entry *te,
265 struct got_repository *repo)
267 const struct got_error *err = NULL;
268 struct got_blob_object *blob = NULL;
269 int fd = -1;
271 *link_target = NULL;
273 if (!got_object_tree_entry_is_symlink(te))
274 return got_error(GOT_ERR_TREE_ENTRY_TYPE);
276 fd = got_opentempfd();
277 if (fd == -1) {
278 err = got_error_from_errno("got_opentempfd");
279 goto done;
282 err = got_object_open_as_blob(&blob, repo,
283 got_tree_entry_get_id(te), PATH_MAX, fd);
284 if (err)
285 goto done;
287 err = got_object_blob_read_to_str(link_target, blob);
288 done:
289 if (fd != -1 && close(fd) == -1 && err == NULL)
290 err = got_error_from_errno("close");
291 if (blob)
292 got_object_blob_close(blob);
293 if (err) {
294 free(*link_target);
295 *link_target = NULL;
297 return err;
301 got_tree_entry_get_index(struct got_tree_entry *te)
303 return te->idx;
306 struct got_tree_entry *
307 got_tree_entry_get_next(struct got_tree_object *tree,
308 struct got_tree_entry *te)
310 return got_object_tree_get_entry(tree, te->idx + 1);
313 struct got_tree_entry *
314 got_tree_entry_get_prev(struct got_tree_object *tree,
315 struct got_tree_entry *te)
317 return got_object_tree_get_entry(tree, te->idx - 1);
320 const struct got_error *
321 got_object_blob_close(struct got_blob_object *blob)
323 const struct got_error *err = NULL;
324 free(blob->read_buf);
325 if (blob->f && fclose(blob->f) == EOF)
326 err = got_error_from_errno("fclose");
327 free(blob->data);
328 free(blob);
329 return err;
332 void
333 got_object_blob_rewind(struct got_blob_object *blob)
335 if (blob->f)
336 rewind(blob->f);
339 char *
340 got_object_blob_id_str(struct got_blob_object *blob, char *buf, size_t size)
342 return got_object_id_hex(&blob->id, buf, size);
345 size_t
346 got_object_blob_get_hdrlen(struct got_blob_object *blob)
348 return blob->hdrlen;
351 const uint8_t *
352 got_object_blob_get_read_buf(struct got_blob_object *blob)
354 return blob->read_buf;
357 const struct got_error *
358 got_object_blob_read_block(size_t *outlenp, struct got_blob_object *blob)
360 size_t n;
362 n = fread(blob->read_buf, 1, blob->blocksize, blob->f);
363 if (n == 0 && ferror(blob->f))
364 return got_ferror(blob->f, GOT_ERR_IO);
365 *outlenp = n;
366 return NULL;
369 const struct got_error *
370 got_object_blob_is_binary(int *binary, struct got_blob_object *blob)
372 const struct got_error *err;
373 size_t hdrlen, len;
375 *binary = 0;
376 hdrlen = got_object_blob_get_hdrlen(blob);
378 if (fseeko(blob->f, hdrlen, SEEK_SET) == -1)
379 return got_error_from_errno("fseeko");
381 err = got_object_blob_read_block(&len, blob);
382 if (err)
383 return err;
385 *binary = memchr(blob->read_buf, '\0', len) != NULL;
387 if (fseeko(blob->f, hdrlen, SEEK_SET) == -1)
388 return got_error_from_errno("fseeko");
389 return NULL;
392 const struct got_error *
393 got_object_blob_getline(char **line, ssize_t *linelen, size_t *linesize,
394 struct got_blob_object *blob)
396 *linelen = getline(line, linesize, blob->f);
397 if (*linelen == -1 && !feof(blob->f))
398 return got_error_from_errno("getline");
399 return NULL;
402 const struct got_error *
403 got_object_blob_dump_to_file(off_t *filesize, int *nlines,
404 off_t **line_offsets, FILE *outfile, struct got_blob_object *blob)
406 const struct got_error *err = NULL;
407 size_t n, len, hdrlen;
408 const uint8_t *buf;
409 int i;
410 const int alloc_chunksz = 512;
411 size_t nalloc = 0;
412 off_t off = 0, total_len = 0;
414 if (line_offsets)
415 *line_offsets = NULL;
416 if (filesize)
417 *filesize = 0;
418 if (nlines)
419 *nlines = 0;
421 hdrlen = got_object_blob_get_hdrlen(blob);
422 do {
423 err = got_object_blob_read_block(&len, blob);
424 if (err)
425 return err;
426 if (len == 0)
427 break;
428 buf = got_object_blob_get_read_buf(blob);
429 i = hdrlen;
430 if (nlines) {
431 if (line_offsets && *line_offsets == NULL) {
432 /* Have some data but perhaps no '\n'. */
433 *nlines = 1;
434 nalloc = alloc_chunksz;
435 *line_offsets = calloc(nalloc,
436 sizeof(**line_offsets));
437 if (*line_offsets == NULL)
438 return got_error_from_errno("calloc");
440 /* Skip forward over end of first line. */
441 while (i < len) {
442 if (buf[i] == '\n')
443 break;
444 i++;
447 /* Scan '\n' offsets in remaining chunk of data. */
448 while (i < len) {
449 if (buf[i] != '\n') {
450 i++;
451 continue;
453 (*nlines)++;
454 if (line_offsets && nalloc < *nlines) {
455 size_t n = *nlines + alloc_chunksz;
456 off_t *o = recallocarray(*line_offsets,
457 nalloc, n, sizeof(**line_offsets));
458 if (o == NULL) {
459 free(*line_offsets);
460 *line_offsets = NULL;
461 return got_error_from_errno(
462 "recallocarray");
464 *line_offsets = o;
465 nalloc = n;
467 if (line_offsets) {
468 off = total_len + i - hdrlen + 1;
469 (*line_offsets)[*nlines - 1] = off;
471 i++;
474 /* Skip blob object header first time around. */
475 n = fwrite(buf + hdrlen, 1, len - hdrlen, outfile);
476 if (n != len - hdrlen)
477 return got_ferror(outfile, GOT_ERR_IO);
478 total_len += len - hdrlen;
479 hdrlen = 0;
480 } while (len != 0);
482 if (fflush(outfile) != 0)
483 return got_error_from_errno("fflush");
484 rewind(outfile);
486 if (filesize)
487 *filesize = total_len;
489 return NULL;
492 const char *
493 got_object_tag_get_name(struct got_tag_object *tag)
495 return tag->tag;
499 got_object_tag_get_object_type(struct got_tag_object *tag)
501 return tag->obj_type;
504 struct got_object_id *
505 got_object_tag_get_object_id(struct got_tag_object *tag)
507 return &tag->id;
510 time_t
511 got_object_tag_get_tagger_time(struct got_tag_object *tag)
513 return tag->tagger_time;
516 time_t
517 got_object_tag_get_tagger_gmtoff(struct got_tag_object *tag)
519 return tag->tagger_gmtoff;
522 const char *
523 got_object_tag_get_tagger(struct got_tag_object *tag)
525 return tag->tagger;
528 const char *
529 got_object_tag_get_message(struct got_tag_object *tag)
531 return tag->tagmsg;
534 static struct got_tree_entry *
535 find_entry_by_name(struct got_tree_object *tree, const char *name, size_t len)
537 int i;
539 /* Note that tree entries are sorted in strncmp() order. */
540 for (i = 0; i < tree->nentries; i++) {
541 struct got_tree_entry *te = &tree->entries[i];
542 int cmp = strncmp(te->name, name, len);
543 if (cmp < 0)
544 continue;
545 if (cmp > 0)
546 break;
547 if (te->name[len] == '\0')
548 return te;
550 return NULL;
553 struct got_tree_entry *
554 got_object_tree_find_entry(struct got_tree_object *tree, const char *name)
556 return find_entry_by_name(tree, name, strlen(name));
559 const struct got_error *
560 got_object_tree_find_path(struct got_object_id **id, mode_t *mode,
561 struct got_repository *repo, struct got_tree_object *tree,
562 const char *path)
564 const struct got_error *err = NULL;
565 struct got_tree_object *subtree = NULL;
566 struct got_tree_entry *te = NULL;
567 const char *seg, *s;
568 size_t seglen;
570 *id = NULL;
572 s = path;
573 while (s[0] == '/')
574 s++;
575 seg = s;
576 seglen = 0;
577 subtree = tree;
578 while (*s) {
579 struct got_tree_object *next_tree;
581 if (*s != '/') {
582 s++;
583 seglen++;
584 if (*s)
585 continue;
588 te = find_entry_by_name(subtree, seg, seglen);
589 if (te == NULL) {
590 err = got_error_path(path, GOT_ERR_NO_TREE_ENTRY);
591 goto done;
594 if (*s == '\0')
595 break;
597 seg = s + 1;
598 seglen = 0;
599 s++;
600 if (*s) {
601 err = got_object_open_as_tree(&next_tree, repo,
602 &te->id);
603 te = NULL;
604 if (err)
605 goto done;
606 if (subtree != tree)
607 got_object_tree_close(subtree);
608 subtree = next_tree;
612 if (te) {
613 *id = got_object_id_dup(&te->id);
614 if (*id == NULL)
615 return got_error_from_errno("got_object_id_dup");
616 if (mode)
617 *mode = te->mode;
618 } else
619 err = got_error_path(path, GOT_ERR_NO_TREE_ENTRY);
620 done:
621 if (subtree && subtree != tree)
622 got_object_tree_close(subtree);
623 return err;
626 const struct got_error *
627 got_object_id_by_path(struct got_object_id **id, struct got_repository *repo,
628 struct got_commit_object *commit, const char *path)
630 const struct got_error *err = NULL;
631 struct got_tree_object *tree = NULL;
633 *id = NULL;
635 /* Handle opening of root of commit's tree. */
636 if (got_path_is_root_dir(path)) {
637 *id = got_object_id_dup(commit->tree_id);
638 if (*id == NULL)
639 err = got_error_from_errno("got_object_id_dup");
640 } else {
641 err = got_object_open_as_tree(&tree, repo, commit->tree_id);
642 if (err)
643 goto done;
644 err = got_object_tree_find_path(id, NULL, repo, tree, path);
646 done:
647 if (tree)
648 got_object_tree_close(tree);
649 return err;
653 * Normalize file mode bits to avoid false positive tree entry differences
654 * in case tree entries have unexpected mode bits set.
656 static mode_t
657 normalize_mode_for_comparison(mode_t mode)
660 * For directories, the only relevant bit is the IFDIR bit.
661 * This allows us to detect paths changing from a directory
662 * to a file and vice versa.
664 if (S_ISDIR(mode))
665 return mode & S_IFDIR;
668 * For symlinks, the only relevant bit is the IFLNK bit.
669 * This allows us to detect paths changing from a symlinks
670 * to a file or directory and vice versa.
672 if (S_ISLNK(mode))
673 return mode & S_IFLNK;
675 /* For files, the only change we care about is the executable bit. */
676 return mode & S_IXUSR;
679 const struct got_error *
680 got_object_tree_path_changed(int *changed,
681 struct got_tree_object *tree01, struct got_tree_object *tree02,
682 const char *path, struct got_repository *repo)
684 const struct got_error *err = NULL;
685 struct got_tree_object *tree1 = NULL, *tree2 = NULL;
686 struct got_tree_entry *te1 = NULL, *te2 = NULL;
687 const char *seg, *s;
688 size_t seglen;
690 *changed = 0;
692 /* We not do support comparing the root path. */
693 if (got_path_is_root_dir(path))
694 return got_error_path(path, GOT_ERR_BAD_PATH);
696 tree1 = tree01;
697 tree2 = tree02;
698 s = path;
699 while (*s == '/')
700 s++;
701 seg = s;
702 seglen = 0;
703 while (*s) {
704 struct got_tree_object *next_tree1, *next_tree2;
705 mode_t mode1, mode2;
707 if (*s != '/') {
708 s++;
709 seglen++;
710 if (*s)
711 continue;
714 te1 = find_entry_by_name(tree1, seg, seglen);
715 if (te1 == NULL) {
716 err = got_error(GOT_ERR_NO_OBJ);
717 goto done;
720 if (tree2)
721 te2 = find_entry_by_name(tree2, seg, seglen);
723 if (te2) {
724 mode1 = normalize_mode_for_comparison(te1->mode);
725 mode2 = normalize_mode_for_comparison(te2->mode);
726 if (mode1 != mode2) {
727 *changed = 1;
728 goto done;
731 if (got_object_id_cmp(&te1->id, &te2->id) == 0) {
732 *changed = 0;
733 goto done;
737 if (*s == '\0') { /* final path element */
738 *changed = 1;
739 goto done;
742 seg = s + 1;
743 s++;
744 seglen = 0;
745 if (*s) {
746 err = got_object_open_as_tree(&next_tree1, repo,
747 &te1->id);
748 te1 = NULL;
749 if (err)
750 goto done;
751 if (tree1 != tree01)
752 got_object_tree_close(tree1);
753 tree1 = next_tree1;
755 if (te2) {
756 err = got_object_open_as_tree(&next_tree2, repo,
757 &te2->id);
758 te2 = NULL;
759 if (err)
760 goto done;
761 if (tree2 != tree02)
762 got_object_tree_close(tree2);
763 tree2 = next_tree2;
764 } else if (tree2) {
765 if (tree2 != tree02)
766 got_object_tree_close(tree2);
767 tree2 = NULL;
771 done:
772 if (tree1 && tree1 != tree01)
773 got_object_tree_close(tree1);
774 if (tree2 && tree2 != tree02)
775 got_object_tree_close(tree2);
776 return err;
779 const struct got_error *
780 got_object_tree_entry_dup(struct got_tree_entry **new_te,
781 struct got_tree_entry *te)
783 const struct got_error *err = NULL;
785 *new_te = calloc(1, sizeof(**new_te));
786 if (*new_te == NULL)
787 return got_error_from_errno("calloc");
789 (*new_te)->mode = te->mode;
790 memcpy((*new_te)->name, te->name, sizeof((*new_te)->name));
791 memcpy(&(*new_te)->id, &te->id, sizeof((*new_te)->id));
792 return err;
796 got_object_tree_entry_is_submodule(struct got_tree_entry *te)
798 return (te->mode & S_IFMT) == (S_IFDIR | S_IFLNK);
802 got_object_tree_entry_is_symlink(struct got_tree_entry *te)
804 /* S_IFDIR check avoids confusing symlinks with submodules. */
805 return ((te->mode & (S_IFDIR | S_IFLNK)) == S_IFLNK);
808 static const struct got_error *
809 resolve_symlink(char **link_target, const char *path,
810 struct got_commit_object *commit, struct got_repository *repo)
812 const struct got_error *err = NULL;
813 char buf[PATH_MAX];
814 char *name, *parent_path = NULL;
815 struct got_object_id *tree_obj_id = NULL;
816 struct got_tree_object *tree = NULL;
817 struct got_tree_entry *te = NULL;
819 *link_target = NULL;
821 if (strlcpy(buf, path, sizeof(buf)) >= sizeof(buf))
822 return got_error(GOT_ERR_NO_SPACE);
824 name = basename(buf);
825 if (name == NULL)
826 return got_error_from_errno2("basename", path);
828 err = got_path_dirname(&parent_path, path);
829 if (err)
830 return err;
832 err = got_object_id_by_path(&tree_obj_id, repo, commit,
833 parent_path);
834 if (err) {
835 if (err->code == GOT_ERR_NO_TREE_ENTRY) {
836 /* Display the complete path in error message. */
837 err = got_error_path(path, err->code);
839 goto done;
842 err = got_object_open_as_tree(&tree, repo, tree_obj_id);
843 if (err)
844 goto done;
846 te = got_object_tree_find_entry(tree, name);
847 if (te == NULL) {
848 err = got_error_path(path, GOT_ERR_NO_TREE_ENTRY);
849 goto done;
852 if (got_object_tree_entry_is_symlink(te)) {
853 err = got_tree_entry_get_symlink_target(link_target, te, repo);
854 if (err)
855 goto done;
856 if (!got_path_is_absolute(*link_target)) {
857 char *abspath;
858 if (asprintf(&abspath, "%s/%s", parent_path,
859 *link_target) == -1) {
860 err = got_error_from_errno("asprintf");
861 goto done;
863 free(*link_target);
864 *link_target = malloc(PATH_MAX);
865 if (*link_target == NULL) {
866 err = got_error_from_errno("malloc");
867 goto done;
869 err = got_canonpath(abspath, *link_target, PATH_MAX);
870 free(abspath);
871 if (err)
872 goto done;
875 done:
876 free(parent_path);
877 free(tree_obj_id);
878 if (tree)
879 got_object_tree_close(tree);
880 if (err) {
881 free(*link_target);
882 *link_target = NULL;
884 return err;
887 const struct got_error *
888 got_object_resolve_symlinks(char **link_target, const char *path,
889 struct got_commit_object *commit, struct got_repository *repo)
891 const struct got_error *err = NULL;
892 char *next_target = NULL;
893 int max_recursion = 40; /* matches Git */
895 *link_target = NULL;
897 do {
898 err = resolve_symlink(&next_target,
899 *link_target ? *link_target : path, commit, repo);
900 if (err)
901 break;
902 if (next_target) {
903 free(*link_target);
904 if (--max_recursion == 0) {
905 err = got_error_path(path, GOT_ERR_RECURSION);
906 *link_target = NULL;
907 break;
909 *link_target = next_target;
911 } while (next_target);
913 return err;
916 void
917 got_object_commit_retain(struct got_commit_object *commit)
919 commit->refcnt++;
922 const struct got_error *
923 got_object_raw_alloc(struct got_raw_object **obj, uint8_t *outbuf, int *outfd,
924 size_t max_in_mem_size, size_t hdrlen, off_t size)
926 const struct got_error *err = NULL;
927 off_t tot;
929 tot = hdrlen + size;
931 *obj = calloc(1, sizeof(**obj));
932 if (*obj == NULL) {
933 err = got_error_from_errno("calloc");
934 goto done;
936 (*obj)->fd = -1;
937 (*obj)->tempfile_idx = -1;
939 if (outbuf) {
940 (*obj)->data = outbuf;
941 } else {
942 struct stat sb;
943 if (fstat(*outfd, &sb) == -1) {
944 err = got_error_from_errno("fstat");
945 goto done;
948 if (sb.st_size != tot) {
949 err = got_error_msg(GOT_ERR_BAD_OBJ_HDR,
950 "raw object has unexpected size");
951 goto done;
953 #ifndef GOT_PACK_NO_MMAP
954 if (tot > 0 && tot <= max_in_mem_size) {
955 (*obj)->data = mmap(NULL, tot, PROT_READ,
956 MAP_PRIVATE, *outfd, 0);
957 if ((*obj)->data == MAP_FAILED) {
958 if (errno != ENOMEM) {
959 err = got_error_from_errno("mmap");
960 goto done;
962 (*obj)->data = NULL;
963 } else {
964 (*obj)->fd = *outfd;
965 *outfd = -1;
968 #endif
969 if (*outfd != -1) {
970 (*obj)->f = fdopen(*outfd, "r");
971 if ((*obj)->f == NULL) {
972 err = got_error_from_errno("fdopen");
973 goto done;
975 *outfd = -1;
978 (*obj)->hdrlen = hdrlen;
979 (*obj)->size = size;
980 done:
981 if (err) {
982 if (*obj) {
983 got_object_raw_close(*obj);
984 *obj = NULL;
986 } else
987 (*obj)->refcnt++;
988 return err;