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 <sys/types.h>
19 #include <sys/queue.h>
37 #include "got_error.h"
38 #include "got_object.h"
39 #include "got_repository.h"
40 #include "got_opentemp.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"
54 #define MIN(_a,_b) ((_a) < (_b) ? (_a) : (_b))
58 #define nitems(_a) (sizeof((_a)) / sizeof((_a)[0]))
61 struct got_object_id *
62 got_object_get_id(struct got_object *obj)
67 const struct got_error *
68 got_object_get_id_str(char **outbuf, struct got_object *obj)
70 return got_object_id_str(outbuf, &obj->id);
73 const struct got_error *
74 got_object_get_type(int *type, struct got_repository *repo,
75 struct got_object_id *id)
77 const struct got_error *err = NULL;
78 struct got_object *obj;
80 err = got_object_open(&obj, repo, id);
85 case GOT_OBJ_TYPE_COMMIT:
86 case GOT_OBJ_TYPE_TREE:
87 case GOT_OBJ_TYPE_BLOB:
88 case GOT_OBJ_TYPE_TAG:
92 err = got_error(GOT_ERR_OBJ_TYPE);
96 got_object_close(obj);
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;
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);
118 if (asprintf(path, "%s/%.2x/%s", path_objects,
119 id->sha1[0], hex + 2) == -1)
120 err = got_error_from_errno("asprintf");
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;
135 err = got_object_get_path(&path, id, repo);
138 *fd = open(path, O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
140 err = got_error_from_errno2("open", path);
148 const struct got_error *
149 got_object_open_by_id_str(struct got_object **obj, struct got_repository *repo,
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);
171 *id = got_object_id_dup(got_object_get_id(obj));
172 got_object_close(obj);
174 return got_error_from_errno("got_object_id_dup");
180 got_object_tree_get_nentries(struct got_tree_object *tree)
182 return tree->nentries;
185 struct got_tree_entry *
186 got_object_tree_get_first_entry(struct got_tree_object *tree)
188 return got_object_tree_get_entry(tree, 0);
191 struct got_tree_entry *
192 got_object_tree_get_last_entry(struct got_tree_object *tree)
194 return got_object_tree_get_entry(tree, tree->nentries - 1);
197 struct got_tree_entry *
198 got_object_tree_get_entry(struct got_tree_object *tree, int i)
200 if (i < 0 || i >= tree->nentries)
202 return &tree->entries[i];
206 got_tree_entry_get_mode(struct got_tree_entry *te)
212 got_tree_entry_get_name(struct got_tree_entry *te)
217 struct got_object_id *
218 got_tree_entry_get_id(struct got_tree_entry *te)
223 const struct got_error *
224 got_object_blob_read_to_str(char **s, struct got_blob_object *blob)
226 const struct got_error *err = NULL;
227 size_t len, totlen, hdrlen, offset;
231 hdrlen = got_object_blob_get_hdrlen(blob);
237 err = got_object_blob_read_block(&len, blob);
244 totlen += len - hdrlen;
245 p = realloc(*s, totlen + 1);
247 err = got_error_from_errno("realloc");
253 /* Skip blob object header first time around. */
255 got_object_blob_get_read_buf(blob) + hdrlen, len - hdrlen);
264 const struct got_error *
265 got_tree_entry_get_symlink_target(char **link_target, struct got_tree_entry *te,
266 struct got_repository *repo)
268 const struct got_error *err = NULL;
269 struct got_blob_object *blob = NULL;
274 if (!got_object_tree_entry_is_symlink(te))
275 return got_error(GOT_ERR_TREE_ENTRY_TYPE);
277 fd = got_opentempfd();
279 err = got_error_from_errno("got_opentempfd");
283 err = got_object_open_as_blob(&blob, repo,
284 got_tree_entry_get_id(te), PATH_MAX, fd);
288 err = got_object_blob_read_to_str(link_target, blob);
290 if (fd != -1 && close(fd) == -1 && err == NULL)
291 err = got_error_from_errno("close");
293 got_object_blob_close(blob);
302 got_tree_entry_get_index(struct got_tree_entry *te)
307 struct got_tree_entry *
308 got_tree_entry_get_next(struct got_tree_object *tree,
309 struct got_tree_entry *te)
311 return got_object_tree_get_entry(tree, te->idx + 1);
314 struct got_tree_entry *
315 got_tree_entry_get_prev(struct got_tree_object *tree,
316 struct got_tree_entry *te)
318 return got_object_tree_get_entry(tree, te->idx - 1);
321 const struct got_error *
322 got_object_blob_close(struct got_blob_object *blob)
324 const struct got_error *err = NULL;
325 free(blob->read_buf);
326 if (blob->f && fclose(blob->f) == EOF)
327 err = got_error_from_errno("fclose");
334 got_object_blob_rewind(struct got_blob_object *blob)
341 got_object_blob_id_str(struct got_blob_object *blob, char *buf, size_t size)
343 return got_object_id_hex(&blob->id, buf, size);
347 got_object_blob_get_hdrlen(struct got_blob_object *blob)
353 got_object_blob_get_read_buf(struct got_blob_object *blob)
355 return blob->read_buf;
358 const struct got_error *
359 got_object_blob_read_block(size_t *outlenp, struct got_blob_object *blob)
363 n = fread(blob->read_buf, 1, blob->blocksize, blob->f);
364 if (n == 0 && ferror(blob->f))
365 return got_ferror(blob->f, GOT_ERR_IO);
370 const struct got_error *
371 got_object_blob_is_binary(int *binary, struct got_blob_object *blob)
373 const struct got_error *err;
377 hdrlen = got_object_blob_get_hdrlen(blob);
379 if (fseeko(blob->f, hdrlen, SEEK_SET) == -1)
380 return got_error_from_errno("fseeko");
382 err = got_object_blob_read_block(&len, blob);
386 *binary = memchr(blob->read_buf, '\0', len) != NULL;
388 if (fseeko(blob->f, hdrlen, SEEK_SET) == -1)
389 return got_error_from_errno("fseeko");
393 const struct got_error *
394 got_object_blob_getline(char **line, ssize_t *linelen, size_t *linesize,
395 struct got_blob_object *blob)
397 *linelen = getline(line, linesize, blob->f);
398 if (*linelen == -1 && !feof(blob->f))
399 return got_error_from_errno("getline");
403 const struct got_error *
404 got_object_blob_dump_to_file(off_t *filesize, int *nlines,
405 off_t **line_offsets, FILE *outfile, struct got_blob_object *blob)
407 const struct got_error *err = NULL;
408 size_t n, len, hdrlen;
411 const int alloc_chunksz = 512;
413 off_t off = 0, total_len = 0;
416 *line_offsets = NULL;
422 hdrlen = got_object_blob_get_hdrlen(blob);
424 err = got_object_blob_read_block(&len, blob);
429 buf = got_object_blob_get_read_buf(blob);
432 if (line_offsets && *line_offsets == NULL) {
433 /* Have some data but perhaps no '\n'. */
435 nalloc = alloc_chunksz;
436 *line_offsets = calloc(nalloc,
437 sizeof(**line_offsets));
438 if (*line_offsets == NULL)
439 return got_error_from_errno("calloc");
441 /* Skip forward over end of first line. */
448 /* Scan '\n' offsets in remaining chunk of data. */
450 if (buf[i] != '\n') {
455 if (line_offsets && nalloc < *nlines) {
456 size_t n = *nlines + alloc_chunksz;
457 off_t *o = recallocarray(*line_offsets,
458 nalloc, n, sizeof(**line_offsets));
461 *line_offsets = NULL;
462 return got_error_from_errno(
469 off = total_len + i - hdrlen + 1;
470 (*line_offsets)[*nlines - 1] = off;
475 /* Skip blob object header first time around. */
476 n = fwrite(buf + hdrlen, 1, len - hdrlen, outfile);
477 if (n != len - hdrlen)
478 return got_ferror(outfile, GOT_ERR_IO);
479 total_len += len - hdrlen;
483 if (fflush(outfile) != 0)
484 return got_error_from_errno("fflush");
488 *filesize = total_len;
494 got_object_tag_get_name(struct got_tag_object *tag)
500 got_object_tag_get_object_type(struct got_tag_object *tag)
502 return tag->obj_type;
505 struct got_object_id *
506 got_object_tag_get_object_id(struct got_tag_object *tag)
512 got_object_tag_get_tagger_time(struct got_tag_object *tag)
514 return tag->tagger_time;
518 got_object_tag_get_tagger_gmtoff(struct got_tag_object *tag)
520 return tag->tagger_gmtoff;
524 got_object_tag_get_tagger(struct got_tag_object *tag)
530 got_object_tag_get_message(struct got_tag_object *tag)
535 static struct got_tree_entry *
536 find_entry_by_name(struct got_tree_object *tree, const char *name, size_t len)
540 /* Note that tree entries are sorted in strncmp() order. */
541 for (i = 0; i < tree->nentries; i++) {
542 struct got_tree_entry *te = &tree->entries[i];
543 int cmp = strncmp(te->name, name, len);
548 if (te->name[len] == '\0')
554 struct got_tree_entry *
555 got_object_tree_find_entry(struct got_tree_object *tree, const char *name)
557 return find_entry_by_name(tree, name, strlen(name));
560 const struct got_error *
561 got_object_tree_find_path(struct got_object_id **id, mode_t *mode,
562 struct got_repository *repo, struct got_tree_object *tree,
565 const struct got_error *err = NULL;
566 struct got_tree_object *subtree = NULL;
567 struct got_tree_entry *te = NULL;
580 struct got_tree_object *next_tree;
589 te = find_entry_by_name(subtree, seg, seglen);
591 err = got_error_path(path, GOT_ERR_NO_TREE_ENTRY);
602 err = got_object_open_as_tree(&next_tree, repo,
608 got_object_tree_close(subtree);
614 *id = got_object_id_dup(&te->id);
616 return got_error_from_errno("got_object_id_dup");
620 err = got_error_path(path, GOT_ERR_NO_TREE_ENTRY);
622 if (subtree && subtree != tree)
623 got_object_tree_close(subtree);
627 const struct got_error *
628 got_object_id_by_path(struct got_object_id **id, struct got_repository *repo,
629 struct got_commit_object *commit, const char *path)
631 const struct got_error *err = NULL;
632 struct got_tree_object *tree = NULL;
636 /* Handle opening of root of commit's tree. */
637 if (got_path_is_root_dir(path)) {
638 *id = got_object_id_dup(commit->tree_id);
640 err = got_error_from_errno("got_object_id_dup");
642 err = got_object_open_as_tree(&tree, repo, commit->tree_id);
645 err = got_object_tree_find_path(id, NULL, repo, tree, path);
649 got_object_tree_close(tree);
654 * Normalize file mode bits to avoid false positive tree entry differences
655 * in case tree entries have unexpected mode bits set.
658 normalize_mode_for_comparison(mode_t mode)
661 * For directories, the only relevant bit is the IFDIR bit.
662 * This allows us to detect paths changing from a directory
663 * to a file and vice versa.
666 return mode & S_IFDIR;
669 * For symlinks, the only relevant bit is the IFLNK bit.
670 * This allows us to detect paths changing from a symlinks
671 * to a file or directory and vice versa.
674 return mode & S_IFLNK;
676 /* For files, the only change we care about is the executable bit. */
677 return mode & S_IXUSR;
680 const struct got_error *
681 got_object_tree_path_changed(int *changed,
682 struct got_tree_object *tree01, struct got_tree_object *tree02,
683 const char *path, struct got_repository *repo)
685 const struct got_error *err = NULL;
686 struct got_tree_object *tree1 = NULL, *tree2 = NULL;
687 struct got_tree_entry *te1 = NULL, *te2 = NULL;
693 /* We not do support comparing the root path. */
694 if (got_path_is_root_dir(path))
695 return got_error_path(path, GOT_ERR_BAD_PATH);
705 struct got_tree_object *next_tree1, *next_tree2;
715 te1 = find_entry_by_name(tree1, seg, seglen);
717 err = got_error(GOT_ERR_NO_OBJ);
722 te2 = find_entry_by_name(tree2, seg, seglen);
725 mode1 = normalize_mode_for_comparison(te1->mode);
726 mode2 = normalize_mode_for_comparison(te2->mode);
727 if (mode1 != mode2) {
732 if (got_object_id_cmp(&te1->id, &te2->id) == 0) {
738 if (*s == '\0') { /* final path element */
747 err = got_object_open_as_tree(&next_tree1, repo,
753 got_object_tree_close(tree1);
757 err = got_object_open_as_tree(&next_tree2, repo,
763 got_object_tree_close(tree2);
767 got_object_tree_close(tree2);
773 if (tree1 && tree1 != tree01)
774 got_object_tree_close(tree1);
775 if (tree2 && tree2 != tree02)
776 got_object_tree_close(tree2);
780 const struct got_error *
781 got_object_tree_entry_dup(struct got_tree_entry **new_te,
782 struct got_tree_entry *te)
784 const struct got_error *err = NULL;
786 *new_te = calloc(1, sizeof(**new_te));
788 return got_error_from_errno("calloc");
790 (*new_te)->mode = te->mode;
791 memcpy((*new_te)->name, te->name, sizeof((*new_te)->name));
792 memcpy(&(*new_te)->id, &te->id, sizeof((*new_te)->id));
797 got_object_tree_entry_is_submodule(struct got_tree_entry *te)
799 return (te->mode & S_IFMT) == (S_IFDIR | S_IFLNK);
803 got_object_tree_entry_is_symlink(struct got_tree_entry *te)
805 /* S_IFDIR check avoids confusing symlinks with submodules. */
806 return ((te->mode & (S_IFDIR | S_IFLNK)) == S_IFLNK);
809 static const struct got_error *
810 resolve_symlink(char **link_target, const char *path,
811 struct got_commit_object *commit, struct got_repository *repo)
813 const struct got_error *err = NULL;
815 char *name, *parent_path = NULL;
816 struct got_object_id *tree_obj_id = NULL;
817 struct got_tree_object *tree = NULL;
818 struct got_tree_entry *te = NULL;
822 if (strlcpy(buf, path, sizeof(buf)) >= sizeof(buf))
823 return got_error(GOT_ERR_NO_SPACE);
825 name = basename(buf);
827 return got_error_from_errno2("basename", path);
829 err = got_path_dirname(&parent_path, path);
833 err = got_object_id_by_path(&tree_obj_id, repo, commit,
836 if (err->code == GOT_ERR_NO_TREE_ENTRY) {
837 /* Display the complete path in error message. */
838 err = got_error_path(path, err->code);
843 err = got_object_open_as_tree(&tree, repo, tree_obj_id);
847 te = got_object_tree_find_entry(tree, name);
849 err = got_error_path(path, GOT_ERR_NO_TREE_ENTRY);
853 if (got_object_tree_entry_is_symlink(te)) {
854 err = got_tree_entry_get_symlink_target(link_target, te, repo);
857 if (!got_path_is_absolute(*link_target)) {
859 if (asprintf(&abspath, "%s/%s", parent_path,
860 *link_target) == -1) {
861 err = got_error_from_errno("asprintf");
865 *link_target = malloc(PATH_MAX);
866 if (*link_target == NULL) {
867 err = got_error_from_errno("malloc");
870 err = got_canonpath(abspath, *link_target, PATH_MAX);
880 got_object_tree_close(tree);
888 const struct got_error *
889 got_object_resolve_symlinks(char **link_target, const char *path,
890 struct got_commit_object *commit, struct got_repository *repo)
892 const struct got_error *err = NULL;
893 char *next_target = NULL;
894 int max_recursion = 40; /* matches Git */
899 err = resolve_symlink(&next_target,
900 *link_target ? *link_target : path, commit, repo);
905 if (--max_recursion == 0) {
906 err = got_error_path(path, GOT_ERR_RECURSION);
910 *link_target = next_target;
912 } while (next_target);
918 got_object_commit_retain(struct got_commit_object *commit)
923 const struct got_error *
924 got_object_raw_alloc(struct got_raw_object **obj, uint8_t *outbuf, int *outfd,
925 size_t max_in_mem_size, size_t hdrlen, off_t size)
927 const struct got_error *err = NULL;
932 *obj = calloc(1, sizeof(**obj));
934 err = got_error_from_errno("calloc");
938 (*obj)->tempfile_idx = -1;
941 (*obj)->data = outbuf;
944 if (fstat(*outfd, &sb) == -1) {
945 err = got_error_from_errno("fstat");
949 if (sb.st_size != tot) {
950 err = got_error_msg(GOT_ERR_BAD_OBJ_HDR,
951 "raw object has unexpected size");
954 #ifndef GOT_PACK_NO_MMAP
955 if (tot > 0 && tot <= max_in_mem_size) {
956 (*obj)->data = mmap(NULL, tot, PROT_READ,
957 MAP_PRIVATE, *outfd, 0);
958 if ((*obj)->data == MAP_FAILED) {
959 if (errno != ENOMEM) {
960 err = got_error_from_errno("mmap");
971 (*obj)->f = fdopen(*outfd, "r");
972 if ((*obj)->f == NULL) {
973 err = got_error_from_errno("fdopen");
979 (*obj)->hdrlen = hdrlen;
984 got_object_raw_close(*obj);